forked from aziz/SublimeFileBrowser
/
dired.py
1319 lines (1145 loc) · 53.3 KB
/
dired.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sublime
from sublime import Region
from sublime_plugin import WindowCommand, TextCommand, EventListener
import os, re, shutil, tempfile, subprocess, itertools, sys, threading, glob
from os.path import basename, dirname, isdir, isfile, exists, join, isabs, normpath, normcase
ST3 = int(sublime.version()) >= 3000
if ST3:
from .common import RE_FILE, DiredBaseCommand
from . import prompt
from .show import show
from .show import set_proper_scheme
from .jumping import jump_names
MARK_OPTIONS = sublime.DRAW_NO_OUTLINE
try:
import Default.send2trash as send2trash
except ImportError:
send2trash = None
else: # ST2 imports
import locale
from common import RE_FILE, DiredBaseCommand
import prompt
from show import show
from show import set_proper_scheme
from jumping import jump_names
MARK_OPTIONS = 0
try:
import send2trash
except ImportError:
send2trash = None
PARENT_SYM = u"⠤"
def print(*args, **kwargs):
""" Redefine print() function; the reason is the inconsistent treatment of
unicode literals among Python versions used in ST2.
Redefining/tweaking built-in things is relatively safe; of course, when
ST2 will become irrelevant, this def might be removed undoubtedly.
"""
if not ST3 and sublime.platform() != 'windows':
args = (s.encode('utf-8') if isinstance(s, unicode) else str(s) for s in args)
else:
args = (s if isinstance(s, str if ST3 else unicode) else str(s) for s in args)
sep, end = kwargs.get('sep', ' '), kwargs.get('end', '\n')
sys.stdout.write(sep.join(s for s in args) + end)
def reuse_view():
return sublime.load_settings('dired.sublime-settings').get('dired_reuse_view', False)
def hijack_window():
settings = sublime.load_settings('dired.sublime-settings')
command = settings.get("dired_hijack_new_window")
if command:
if command == "jump_list":
sublime.set_timeout(lambda: sublime.windows()[-1].run_command("dired_jump_list"), 1)
else:
sublime.set_timeout(lambda: sublime.windows()[-1].run_command("dired", {"immediate": True}), 1)
def plugin_loaded():
if len(sublime.windows()) == 1 and len(sublime.windows()[0].views()) == 0:
hijack_window()
window = sublime.active_window()
if not ST3:
global recursive_plugin_loaded
# recursion limit is 1000 generally, so it will try to refresh for 100*1000 ms (100 s)
# if no active_window in 100 s, then no refresh
# if view still loading, refresh fail because view cant be edited
if not window or any(view.is_loading() for view in window.views()):
recursive_plugin_loaded += 1
try:
return sublime.set_timeout(plugin_loaded, 100)
except RuntimeError:
print('\ndired.plugin_loaded run recursively %d time(s); and failed to refresh\n'%recursive_plugin_loaded)
return
for v in window.views():
if v.settings() and v.settings().get("dired_path"):
v.run_command("dired_refresh")
# if not ST3:
# print('\ndired.plugin_loaded run recursively %d time(s); and call refresh command\n'%recursive_plugin_loaded)
if not ST3:
recursive_plugin_loaded = 1
plugin_loaded()
class DiredCommand(WindowCommand):
"""
Prompt for a directory to display and display it.
"""
def run(self, immediate=False, single_pane=False, project=False, other_group=False):
path, goto = self._determine_path()
if project:
folders = self.window.folders()
if len(folders) == 1:
path = folders[0]
elif folders:
folders = [ [basename(f), f] for f in folders]
self.window.show_quick_panel(folders, lambda i: self._show_folder(i, path, goto, single_pane, other_group))
return
if immediate:
show(self.window, path, goto=goto, single_pane=single_pane, other_group=other_group)
else:
prompt.start('Directory:', self.window, path, self._show)
def _show_folder(self, index, path, goto, single_pane, other_group):
if index != -1:
choice = self.window.folders()[index]
if path == choice:
show(self.window, path, goto=goto, single_pane=single_pane, other_group=other_group)
else:
show(self.window, choice, single_pane=single_pane, other_group=other_group)
def _show(self, path):
show(self.window, path)
def _determine_path(self):
'''Return (path, fname) so goto=fname to set cursor'''
# Use the current view's directory if it has one.
view = self.window.active_view()
path = view and view.file_name()
if path:
return os.path.split(path)
# Use the first project folder if there is one.
data = self.window.project_data() if ST3 else None
if data and 'folders' in data:
folders = data['folders']
if folders:
return (folders[0]['path'], None)
# Use window folder if possible
folders = self.window.folders()
if len(folders) > 0:
return (folders[0], None)
# Use the user's home directory.
return (os.path.expanduser('~'), None)
class DiredRefreshCommand(TextCommand, DiredBaseCommand):
"""
Populates or repopulates a dired view.
"""
def run(self, edit, goto=None, to_expand=None, toggle=None):
"""
goto
Optional filename to put the cursor on; used only from "dired_up"
to_expand
List of relative paths for direcories which shall be expanded
toggle
If true, marked/selected directories shall switch state,
i.e. expand/collapse
"""
path = self.path
self.sel = None
expanded = self.view.find_all(u'^\s*▾') if not goto else []
names = []
if path == 'ThisPC\\':
path = ''
for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
disk = '%s:' % s
if isdir(disk):
names.append(disk)
if expanded or to_expand:
self.re_populate_view(edit, path, names, expanded, to_expand, toggle)
else:
if not path:
self.continue_refreshing(edit, path, names)
else:
self.populate_view(edit, path, names, goto)
def re_populate_view(self, edit, path, names, expanded, to_expand, toggle):
root = path
for i, r in enumerate(expanded):
line = self.view.line(r)
full_name = self._remove_ui(self.get_parent(line, self.view.substr(line)))
expanded[i] = full_name.rstrip(os.sep)
if toggle and to_expand:
merged = list(set(expanded + to_expand))
expanded = [e for e in merged if not (e in expanded and e in to_expand)]
else:
expanded.extend(to_expand or [])
if any(e for e in expanded if e == path.rstrip(os.sep)):
# e.g. c:\ was expanded and user press enter on it
return self.populate_view(edit, path, names, goto=None)
self.show_hidden = self.view.settings().get('dired_show_hidden_files', True)
tree = self.traverse_tree(root, root, '', names, expanded)
if tree:
self.set_status()
text, _ = self.set_title(path)
if root and self.show_parent():
text.extend(PARENT_SYM)
text.extend(tree)
marked = set(self.get_marked())
sels = self.get_selected()
self.view.set_read_only(False)
self.view.erase(edit, Region(0, self.view.size()))
self.view.insert(edit, 0, '\n'.join(text))
self.view.set_read_only(True)
fileregion = self.fileregion()
count = len(self.view.lines(fileregion)) if fileregion else 0
self.view.settings().set('dired_count', count)
self.restore_marks(marked)
self.restore_sels(sels)
CallVCS(self.view, path)
else:
return self.populate_view(edit, path, names, goto=None)
def populate_view(self, edit, path, names, goto):
try:
if goto and goto[~0]==':':
# c:\\ valid path, c: not valid
goto += os.sep
names = os.listdir(path)
except OSError as e:
error = str(e).split(':')[0].replace('[Error 5] ', 'Access denied')
self.view.run_command("dired_up")
self.view.set_read_only(False)
self.view.insert(edit, self.view.line(self.view.sel()[0]).b,
'\t<%s>' % error)
self.view.set_read_only(True)
else:
self.continue_refreshing(edit, path, names, goto)
CallVCS(self.view, path)
def continue_refreshing(self, edit, path, names, goto=None, indent=''):
self.set_status()
f = self.prepare_filelist(names, path, '', '')
marked = set(self.get_marked())
text, header = self.set_title(path)
if path and (not f or self.show_parent()):
text.append(PARENT_SYM)
text.extend(f)
self.view.set_read_only(False)
self.view.erase(edit, Region(0, self.view.size()))
self.view.insert(edit, 0, '\n'.join(text))
self.view.settings().set('dired_count', len(f))
self.view.set_read_only(True)
self.restore_marks(marked)
# Place the cursor.
if f:
pt = self.fileregion(with_parent_link=True).a
if goto:
if isdir(join(path, goto)) and not goto.endswith(os.sep):
goto = u"▸ " + goto + os.sep
else:
goto = u"≡ " + goto
try:
line = f.index(goto) + (2 if header else 0) + (1 if self.show_parent() else 0)
pt = self.view.text_point(line, 2)
except ValueError:
pass
self.view.sel().clear()
self.view.sel().add(Region(pt, pt))
self.view.show_at_center(Region(pt, pt))
elif not f: # empty folder?
pt = self.view.text_point(2, 0)
self.view.sel().clear()
self.view.sel().add(Region(pt, pt))
else:
self.view.sel().clear()
self.view.sel().add(Region(name_point, name_point))
self.view.show_at_center(name_point)
def traverse_tree(self, root, path, indent, tree, expanded):
if not path:
# special case for ThisPC, path is empty string
files = [u'%s\\'%d for d in tree]
tree = []
else:
# basename return funny results for c:\\ so it is tricky
b = os.path.basename(os.path.abspath(path)) or path.rstrip(os.sep)
if root != path and b != os.path.basename(root.rstrip(os.sep)):
tree.append(indent[:-1] + u'▾ ' + b + os.sep)
try:
if not self.show_hidden:
files = [name for name in os.listdir(path) if not self.is_hidden(name, path)]
else:
files = os.listdir(path)
except OSError as e:
tree[~0] += '\t<%s>' % str(e).split(':')[0].replace('[Error 5] ', 'Access denied')
return
self.sort_nicely(files)
if tree and not files:
# expanding empty folder, so notify that it is empty
tree[~0] += '\t<empty>'
for f in files:
new_path = join(path, f)
check = isdir(new_path)
if check and new_path.replace(root, '', 1).strip(os.sep) in expanded:
self.traverse_tree(root, new_path, indent + '\t', tree, expanded)
elif check:
tree.append(u'%s▸ %s%s' % (indent, f, os.sep if f[~0] != os.sep else ''))
else:
continue
for f in files:
if not os.path.isdir(os.path.join(path, f)):
tree.append(indent + u'≡ ' + f)
return tree
def set_status(self):
status = u" 𝌆 [?: Help] "
# if view isnot focused, view.window() may be None
window = self.view.window() or sublime.active_window()
path_in_project = any(folder == self.path[:-1] for folder in window.folders())
status += 'Project root, ' if path_in_project else ''
show_hidden = self.view.settings().get('dired_show_hidden_files', True)
status += 'Hidden: On' if show_hidden else 'Hidden: Off'
self.view.set_status("__FileBrowser__", status)
def set_title(self, path):
header = self.view.settings().get('dired_header', False)
name = jump_names().get(path or self.path)
caption = u"{0} → {1}".format(name, path) if name else path or self.path
text = [ caption, len(caption)*(u'—') ] if header else []
view_name = self.view.name()[:2]
if not path:
title = u'%s%s' % (view_name, name or 'This PC')
else:
norm_path = path.rstrip(os.sep)
if self.view.settings().get('dired_show_full_path', False):
title = u'%s%s (%s)' % (view_name, name or basename(norm_path), norm_path)
else:
title = u'%s%s' % (view_name, name or basename(norm_path))
self.view.set_name(title)
return (text, header)
# NAVIGATION #####################################################
class DiredNextLineCommand(TextCommand, DiredBaseCommand):
def run(self, edit, forward=None):
self.move(forward)
class DiredMoveCommand(TextCommand, DiredBaseCommand):
def run(self, edit, **kwargs):
if kwargs and kwargs["to"]:
self.move_to_extreme(kwargs["to"])
return
elif kwargs and kwargs["duplicate"]:
self.items = self._get_items(self.path)
self.cursor = self.view.substr(self.view.line(self.view.sel()[0].a))[2:]
self._duplicate(duplicate=kwargs["duplicate"])
else:
files = self.get_marked() or self.get_selected()
if files:
prompt.start('Move to:', self.view.window(), self.path, self._move)
def _get_items(self, path):
files = self.get_marked() or self.get_selected()
path = normpath(normcase(path))
for filename in files:
fqn = normpath(normcase(join(self.path, filename)))
yield fqn
def _move(self, path):
if not isabs(path):
path = join(self.path, path)
if not isdir(path):
sublime.error_message(u'Not a valid directory: {0}'.format(path))
return
for fqn in self._get_items(path):
if fqn != path:
shutil.move(fqn, path)
self.view.run_command('dired_refresh')
def _duplicate(self, duplicate=''):
fqn = next(self.items)
for i in itertools.count(2):
p, n = os.path.split(fqn)
cfp = u"{1} {0}.{2}".format(i, join(p, n.split('.')[0]), '.'.join(n.split('.')[1:]))
if os.path.isfile(cfp) or os.path.isdir(cfp):
pass
else:
break
if duplicate == 'rename':
prompt.start('New name:', self.view.window(), os.path.basename(cfp), self._copy_duplicate, rename=(fqn, cfp, self.cursor))
else:
self._copy_duplicate(fqn, cfp, 0)
def _copy_duplicate(self, fqn, cfp, int):
if isdir(fqn):
if not isdir(cfp):
shutil.copytree(fqn, cfp)
else:
print(*("\nSkip! Folder with this name exists already:", cfp), sep='\n', end='\n\n')
else:
if not isfile(cfp):
shutil.copy2(fqn, cfp)
else:
print(*("\nSkip! File with this name exists already:", cfp), sep='\n', end='\n\n')
try:
if int == 0:
self._duplicate()
elif int == 1:
self._duplicate(duplicate='rename')
except StopIteration:
self.view.run_command('dired_refresh', {"goto": self.cursor})
class DiredSelect(TextCommand, DiredBaseCommand):
def run(self, edit, new_view=0, other_group=0, preview=0, and_close=0, inline=0, toggle=0):
path = self.path
if inline:
filenames = self.get_marked() or self.get_selected(parent=False)
if len(filenames) == 1 and filenames[0][~0] == os.sep:
return self.expand_single_folder(edit, path, filenames[0], toggle)
elif filenames:
# working with several selections at once is very tricky,
# thus for reliability we should recreate the entire tree
# despite it is slower
self.view.run_command('dired_refresh', {'to_expand': [f.rstrip(os.sep) for f in filenames], 'toggle': toggle})
return
else:
return sublime.status_message('Item cannot be expanded')
filenames = self.get_selected() if not (new_view or inline) else self.get_marked() or self.get_selected()
# If reuse view is turned on and the only item is a directory, refresh the existing view.
if not new_view and reuse_view():
fqn = join(path, filenames[0])
if len(filenames) == 1 and isdir(fqn):
show(self.view.window(), fqn, view_id=self.view.id())
return
elif len(filenames) == 1 and filenames[0] == PARENT_SYM:
self.view.window().run_command("dired_up")
return
w = self.view.window()
if other_group or preview or and_close:
dired_view = self.view
nag = self.view.window().active_group()
if not and_close:
target_group = self._other_group(w, nag)
# set_view_index and focus are not very reliable
# just focus target_group should do what we want
w.focus_group(target_group)
for filename in filenames:
fqn = join(path, filename)
if exists(fqn): # ignore 'item <error>'
if isdir(fqn):
show(w, fqn, ignore_existing=new_view)
else:
if preview:
w.open_file(fqn, sublime.TRANSIENT)
w.focus_view(dired_view)
return # preview is possible for a single file only
else:
v = w.open_file(fqn)
if and_close:
w.focus_view(dired_view)
w.run_command("close")
w.focus_view(v)
def _other_group(self, w, nag):
'''
creates new group if need and return index of the group where files
shall be opened
'''
groups = w.num_groups()
if groups == 1:
w.set_layout({"cols": [0.0, 0.3, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]})
if groups <= 4 and nag < 2:
group = 1 if nag == 0 else 0
elif groups == 4 and nag >= 2:
group = 3 if nag == 2 else 2
else:
group = nag - 1
return group
def expand_single_folder(self, edit, path, filename, toggle):
marked = set(self.get_marked())
sels = self.get_selected()
if toggle:
line = self.view.line(self.view.get_regions('marked')[0] if marked else
list(self.view.sel())[0])
content = self.view.substr(line).lstrip()[0]
if content == u'▾':
self.view.run_command('dired_fold')
return
self.view.run_command('dired_fold', {'update': True})
sel = self.view.get_regions('marked')[0] if marked else list(self.view.sel())[0]
line = self.view.line(sel)
fqn = join(path, filename)
if isdir(fqn):
self.sel = sel
try:
names = os.listdir(fqn)
except OSError as e:
error = str(e).split(':')[0].replace('[Error 5] ', 'Access denied')
replacement = '%s\t<%s>' % (self.view.substr(line), error)
else:
path = self.path if self.path != 'ThisPC\\' else ''
replacement = self.prepare_treeview(names, path, fqn, '\t')
else:
replacement = '%s\t<%s>' % (self.view.substr(line), 'Not exists, press r to refresh')
self.view.set_read_only(False)
self.view.replace(edit, line, replacement)
self.view.set_read_only(True)
self.restore_marks(marked)
self.restore_sels(sels)
CallVCS(self.view, path)
class DiredFold(TextCommand, DiredBaseCommand):
u'''
This command used to fold/erase/shrink (whatever you like to call it) content
of some [sub]directory (within current directory, see self.path).
There are two cases when this command would be fired:
1. User mean to fold (key ←)
2. User mean to unfold (key →)
In first case we just erase region and set dired_count; however, we need to
figure out which region to erase:
(a) if cursor placed on directory item and next line indented
(representing content of the directory) — erase indented line;
(b) next line is not indented, but the line of directory item is indented —
erase directory item itself;
(c) cursor placed on file item which is indented — erase file item.
In second case we need to decide if erasing needed or not:
(a) if directory was unfolded (as in 1.a) — erase that region, so then
it’ll be filled (basically it is like update/refresh), also set dired_count;
(b) directory was folded (as in 1.b) — do nothing
'''
def run(self, edit, update=''):
v = self.view
self.marked = None
self.seled = self.get_selected()
marks = self.view.get_regions('marked')
virt_sels = []
if marks:
for m in marks:
if 'directory' in self.view.scope_name(m.a):
virt_sels.append(Region(m.a, m.a))
self.marked = list(set(self.get_marked()))
sels = virt_sels
lines = [v.line(s.a) for s in reversed(sels or list(v.sel()))]
for line in lines:
self.fold(edit, v, line, update)
if self.marked:
self.restore_marks(self.marked)
if self.seled:
self.restore_sels(self.seled)
def fold(self, edit, v, line, update):
current_region = v.indented_region(line.b)
next_region = v.indented_region(line.b + 2)
is_folder = 'directory' in v.scope_name(line.a)
folded_subfolder = update and (next_region.contains(line) or next_region.empty() or next_region.contains(current_region))
folded_folder = update and current_region.empty() and next_region.empty()
file_item_in_root = not is_folder and current_region.empty()
if v.substr(line).endswith('<empty>'):
indented_region = v.extract_scope(line.b - 1)
elif folded_subfolder or folded_folder or file_item_in_root:
return # folding is not supposed to happen, so we exit
elif update or (is_folder and not next_region.empty() and not next_region.contains(line)):
indented_region = next_region
elif not current_region.empty():
indented_region = current_region
line = v.line(indented_region.a - 2)
else:
return # this is not supposed to happen, but it does sometimes
name_point = v.extract_scope(line.a).b
if 'name' in v.scope_name(name_point):
icon_region = Region(name_point - 2, name_point - 1)
else:
icon_region = Region(line.a, line.a + 1)
# do not set count on empty folder
if not line.contains(indented_region):
dired_count = v.settings().get('dired_count', 0)
v.settings().set('dired_count', int(dired_count) - len(v.lines(indented_region)))
if indented_region.b == v.size():
# MUST avoid new line at eof
indented_region = Region(indented_region.a - 1, indented_region.b)
if self.marked or self.seled:
folded_name = self._remove_ui(self.get_parent(line, self.view.substr(line)))
if self.marked:
self.marked.append(folded_name)
elif self.seled:
self.seled.append(folded_name)
v.set_read_only(False)
v.replace(edit, icon_region, u'▸')
v.erase(edit, indented_region)
v.set_read_only(True)
class DiredUpCommand(TextCommand, DiredBaseCommand):
def run(self, edit):
path = self.path
parent = dirname(path.rstrip(os.sep))
if parent != os.sep and parent[1:] != ':\\':
# need to avoid c:\\\\
parent += os.sep
if parent == path and sublime.platform()=='windows':
parent = 'ThisPC'
elif parent == path:
return
elif path == 'ThisPC\\':
self.view.run_command('dired_refresh')
return
view_id = (self.view.id() if reuse_view() else None)
show(self.view.window(), parent, view_id, goto=basename(path.rstrip(os.sep)))
class DiredGotoCommand(TextCommand, DiredBaseCommand):
"""
Prompt for a new directory.
"""
def run(self, edit):
prompt.start('Goto:', self.view.window(), self.path, self.goto)
def goto(self, path):
show(self.view.window(), path, view_id=self.view.id())
class DiredFindInFilesCommand(TextCommand, DiredBaseCommand):
def run(self, edit):
path = self.path
if path == 'ThisPC\\':
path = ''
items = self.get_marked() or self.get_selected()
else:
items = self.get_marked()
where = ', '.join(join(path, p) for p in items) or path or ''
args = {"panel": "find_in_files", "where": where, "replace": "", "reverse": "false"}
sublime.active_window().run_command("show_panel", args)
# MARKING ###########################################################
class DiredMarkExtensionCommand(TextCommand, DiredBaseCommand):
def run(self, edit):
filergn = self.fileregion()
if filergn.empty():
return
current_item = self.view.substr(self.view.line(self.view.sel()[0].a))
if current_item.endswith('/') or current_item == PARENT_SYM:
ext = ''
else:
ext = current_item.split('.')[-1]
pv = self.view.window().show_input_panel('Extension:', ext, self.on_done, None, None)
pv.run_command("select_all")
def on_done(self, ext):
ext = ext.strip()
if not ext:
return
if not ext.startswith('.'):
ext = '.' + ext
def _markfunc(oldmark, filename):
return filename.endswith(ext) and True or oldmark
self._mark(mark=_markfunc, regions=self.fileregion())
class DiredMarkCommand(TextCommand, DiredBaseCommand):
"""
Marks or unmarks files.
The mark can be set to '*' to mark a file, ' ' to unmark a file, or 't' to toggle the
mark.
By default only selected files are marked, but if markall is True all files are
marked/unmarked and the selection is ignored.
If there is no selection and mark is '*', the cursor is moved to the next line so
successive files can be marked by repeating the mark key binding (e.g. 'm').
"""
def run(self, edit, mark=True, markall=False, forward=True):
assert mark in (True, False, 'toggle')
filergn = self.fileregion()
if filergn.empty():
return
# If markall is set, mark/unmark all files. Otherwise only those that are selected.
if markall:
regions = [ filergn ]
else:
regions = self.view.sel()
def _toggle(oldmark, filename):
return not oldmark
if mark == 'toggle':
# Special internal case.
mark = _toggle
self._mark(mark=mark, regions=regions)
# If there is no selection, move the cursor forward so the user can keep pressing 'm'
# to mark successive files.
if not markall and len(self.view.sel()) == 1 and self.view.sel()[0].empty():
self.move(forward)
# MANIPULATION ######################################################
class DiredCreateCommand(TextCommand, DiredBaseCommand):
def run(self, edit, which=None):
assert which in ('file', 'directory'), "which: " + which
relative_path = self.get_selected(parent=False) or ""
if relative_path:
relative_path = relative_path[0]
if relative_path[~0] != os.sep:
relative_path = os.path.split(relative_path)[0] + os.sep
if relative_path == os.sep:
relative_path = ""
# Is there a better way to do this? Why isn't there some kind of context? I assume
# the command instance is global and really shouldn't have instance information.
callback = getattr(self, 'on_done_' + which, None)
pv = self.view.window().show_input_panel(which.capitalize() + ':', relative_path, callback, None, None)
pv.run_command('move_to', {'to': 'eol', 'extend': False})
def on_done_file(self, value):
self._on_done('file', value)
def on_done_directory(self, value):
self._on_done('directory', value)
def _on_done(self, which, value):
value = value.strip()
if not value:
return
fqn = join(self.path, value)
if exists(fqn):
sublime.error_message(u'{0} already exists'.format(fqn))
return
if which == 'directory':
os.makedirs(fqn)
else:
open(fqn, 'wb')
self.view.run_command('dired_refresh')
class DiredDeleteCommand(TextCommand, DiredBaseCommand):
def run(self, edit, trash=False):
files = self.get_marked() or self.get_selected(parent=False)
if files:
# Yes, I know this is English. Not sure how Sublime is translating.
if len(files) == 1:
msg = u"Delete {0}?".format(files[0])
else:
msg = u"Delete {0} items?".format(len(files))
if trash:
need_confirm = self.view.settings().get('dired_confirm_send2trash')
if trash and not send2trash:
msg = u"Cannot delete to trash.\nPermanently " + msg.replace('D', 'd', 1)
trash = False
elif trash and need_confirm:
msg = msg.replace('Delete', 'Delete to trash', 1)
if trash and send2trash:
if not need_confirm or (need_confirm and sublime.ok_cancel_dialog(msg)):
self._to_trash(files)
elif not trash and sublime.ok_cancel_dialog(msg):
self._delete(files)
else:
print("Cancel delete or something wrong in DiredDeleteCommand")
def _to_trash(self, files):
path = self.path
errors = []
def _status(filename='', done=False):
if done:
sublime.set_timeout(lambda: self.view.run_command('dired_refresh'), 1)
if errors:
sublime.error_message(u'Some files couldn’t be sent to trash (perhaps, they are being used by another process): \n\n'
+'\n'.join(errors).replace('Couldn\'t perform operation.', ''))
else:
status = u'Please, wait… Removing ' + filename
sublime.set_timeout(lambda: self.view.set_status("__FileBrowser__", status), 1)
def _sender(files, event_for_wait, event_for_set):
for filename in files:
event_for_wait.wait()
event_for_wait.clear()
if event_for_wait is remove_event:
try:
send2trash.send2trash(join(path, filename))
except OSError as e:
errors.append(u'{0}:\t{1}'.format(e, filename))
else:
_status(filename)
event_for_set.set()
if event_for_wait is remove_event:
_status(done=True)
remove_event = threading.Event()
report_event = threading.Event()
t1 = threading.Thread(target=_sender, args=(files, remove_event, report_event))
t2 = threading.Thread(target=_sender, args=(files, report_event, remove_event))
t1.start()
t2.start()
report_event.set()
def _delete(self, files):
errors = []
if ST3:
fail = (PermissionError, FileNotFoundError)
else:
fail = OSError
sys_enc = locale.getpreferredencoding(False)
for filename in files:
fqn = join(self.path, filename)
try:
if isdir(fqn):
shutil.rmtree(fqn)
else:
os.remove(fqn)
except fail as e:
e = str(e).split(':')[0].replace('[Error 5] ', 'Access denied')
if not ST3:
try:
e = str(e).decode(sys_enc)
except: # failed getpreferredencoding
e = 'Unknown error'
errors.append(u'{0}:\t{1}'.format(e, filename))
self.view.run_command('dired_refresh')
if errors:
sublime.error_message(u'Some files couldn’t be deleted: \n\n' + '\n'.join(errors))
class DiredRenameCommand(TextCommand, DiredBaseCommand):
def run(self, edit):
if self.filecount():
# Store the original filenames so we can compare later.
self.view.settings().set('rename', self.get_all())
self.view.settings().set('dired_rename_mode', True)
self.view.settings().set('color_scheme', 'Packages/FileBrowser/dired-rename-mode.hidden-tmTheme')
self.view.set_read_only(False)
self.set_ui_in_rename_mode(edit)
self.view.set_status("__FileBrowser__", u" 𝌆 [enter: Apply changes] [escape: Discard changes] %s" % (u'¡¡¡DO NOT RENAME DISKS!!! you can rename their children though ' if self.path == 'ThisPC\\' else ''))
# Mark the original filename lines so we can make sure they are in the same
# place.
r = self.fileregion()
self.view.add_regions('rename', [ r ], '', '', MARK_OPTIONS)
class DiredRenameCancelCommand(TextCommand, DiredBaseCommand):
"""
Cancel rename mode.
"""
def run(self, edit):
self.view.settings().erase('rename')
self.view.settings().set('color_scheme', 'Packages/FileBrowser/dired.hidden-tmTheme')
self.view.settings().set('dired_rename_mode', False)
self.view.run_command('dired_refresh')
class DiredRenameCommitCommand(TextCommand, DiredBaseCommand):
def run(self, edit):
if not self.view.settings().has('rename'):
# Shouldn't happen, but we want to cleanup when things go wrong.
self.view.run_command('dired_refresh')
return
before = self.view.settings().get('rename')
# We marked the set of files with a region. Make sure the region still has the same
# number of files.
after = []
for region in self.view.get_regions('rename'):
for line in self.view.lines(region):
after.append(self._remove_ui(self.get_parent(line, self.view.substr(line).strip())))
if len(after) != len(before):
sublime.error_message('You cannot add or remove lines')
return
if len(set(after)) != len(after):
sublime.error_message('There are duplicate filenames (see details in console)')
self.view.window().run_command("show_panel", {"panel": "console"})
print(*(u'\n Original name: {0}\nConflicting name: {1}'.format(b, a)
for (b, a) in zip(before, after) if b != a and a in before),
sep='\n', end='\n\n')
print('You can either resolve conflicts and apply changes or cancel renaming.\n')
return
diffs = [ (b, a) for (b, a) in zip(before, after) if b != a ]
if diffs:
existing = set(before)
while diffs:
b, a = diffs.pop(0)
if a in existing:
# There is already a file with this name. Give it a temporary name (in
# case of cycles like "x->z and z->x") and put it back on the list.
tmp = tempfile.NamedTemporaryFile(delete=False, dir=self.path).name
os.unlink(tmp)
diffs.append((tmp, a))
a = tmp
print(u'dired rename: {0} → {1}'.format(b, a))
orig = join(self.path, b)
if orig[~0] == '/' and os.path.islink(orig[:~0]):
# last slash shall be omitted; file has no last slash,
# thus it False and symlink to file shall be os.rename'd
dest = os.readlink(orig[:~0])
os.unlink(orig[:~0])
os.symlink(dest, join(self.path, a)[:~0])
else:
try:
os.rename(orig, join(self.path, a))
except OSError:
msg = (u'FileBrowser:\n\nError is occured during renaming.\n'
u'Please, fix it and apply changes or cancel renaming.\n\n'
u'\t {0} → {1}\n\n'
u'Don’t rename\n'
u' • parent and child at the same time\n'
u' • non-existed file (cancel renaming to refresh)\n'
u' • file if you’re not owner'
u' • disk letter on Windows\n'.format(b, a))
sublime.error_message(msg)
return
existing.remove(b)
existing.add(a)
self.view.erase_regions('rename')
self.view.settings().erase('rename')
self.view.settings().set('color_scheme', 'Packages/FileBrowser/dired.hidden-tmTheme')
self.view.settings().set('dired_rename_mode', False)
self.view.run_command('dired_refresh')
# HELP ##############################################################
class DiredHelpCommand(TextCommand):
def run(self, edit):
view = self.view.window().new_file()
view.settings().add_on_change('color_scheme', lambda: set_proper_scheme(view))
view.set_name("Browse: shortcuts")
view.set_scratch(True)
view.settings().set('color_scheme','Packages/FileBrowser/dired.hidden-tmTheme')
view.settings().set('syntax','Packages/FileBrowser/dired-help.hidden-tmLanguage')
view.settings().set('line_numbers',False)
view.run_command('dired_show_help')
sublime.active_window().focus_view(view)
class DiredShowHelpCommand(TextCommand):
def run(self, edit):
COMMANDS_HELP = sublime.load_resource('Packages/FileBrowser/shortcuts.md') if ST3 else ''
if not COMMANDS_HELP:
dest = dirname(__file__)
shortcuts = join(dest if dest!='.' else join(sublime.packages_path(), 'FileBrowser'), "shortcuts.md")
COMMANDS_HELP = open(shortcuts, "r").read()
self.view.erase(edit, Region(0, self.view.size()))
self.view.insert(edit, 0, COMMANDS_HELP)
self.view.sel().clear()
self.view.set_read_only(True)
# OTHER #############################################################
class DiredToggleHiddenFilesCommand(TextCommand):
def run(self, edit):
show = self.view.settings().get('dired_show_hidden_files', True)
self.view.settings().set('dired_show_hidden_files', not show)
self.view.run_command('dired_refresh')