forked from sunrise-commander/sunrise-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunrise.el
5070 lines (4435 loc) · 206 KB
/
sunrise.el
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
;;; sunrise.el --- The Sunrise Commander: a two-pane file manager -*- lexical-binding: t -*-
;; Copyright (C) 2007-2015 José Alfredo Romero Latouche.
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Author: José Alfredo Romero Latouche <escherdragon@gmail.com>
;; Štěpán Němec <stepnem@gmail.com>
;; Maintainer: José Alfredo Romero Latouche <escherdragon@gmail.com>
;; Created: 24 Sep 2007
;; Version: 6
;; Package-Requires: ((emacs "24.3"))
;; Keywords: files, dired, midnight commander, norton, orthodox
;; URL: https://github.com/sunrise-commander/sunrise-commander
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation, either version 3 of the License, or (at your option) any later
;; version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more de-
;; tails.
;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The Sunrise Commander is a double-pane file manager for Emacs. It's built
;; atop of Dired and takes advantage of all its power, but also provides many
;; handy features of its own:
;; * Sunrise is implemented as a derived major mode confined inside the pane
;; buffers, so its buffers and Dired ones can live together without easymenu or
;; viper to avoid key binding collisions.
;; * It automatically closes unused buffers and tries to never keep open more
;; than the one or two used to display the panes, though this behavior may be
;; disabled if desired.
;; * Each pane has its own history stack: press M-y / M-u for moving backwards /
;; forwards in the history of directories.
;; * Press M-t to swap (transpose) the panes.
;; * Press C-= for "smart" file comparison using `ediff'. It compares together
;; the first two files marked on each pane or, if no files have been marked, it
;; assumes that the second pane contains a file with the same name as the
;; selected one and tries to compare these two. You can also mark whole lists of
;; files to be compared and then just press C-= for comparing the next pair.
;; * Press = for fast "smart" file comparison -- like above, but using regular
;; diff.
;; * Press C-M-= for directory comparison (by date / size / contents of files).
;; * Press C-c C-s to change the layout of the panes (horizontal/vertical/top)
;; * Press C-c / to interactively refine the contents of the current pane using
;; fuzzy (a.k.a. flex) matching, then:
;; - press Delete or Backspace to revert the buffer to its previous state
;; - press Return, C-n or C-p to exit and accept the current narrowed state
;; - press Esc or C-g to abort the operation and revert the buffer
;; - use ! to prefix characters that should NOT appear after a given position
;; Once narrowed and accepted, you can restore the original contents of the pane
;; by pressing g (revert-buffer).
;; * Sticky search: press C-c s to launch an interactive search that will remain
;; active from directory to directory, until you hit a regular file or press C-g
;; * Press C-x C-q to put the current pane in Editable Dired mode (allows to
;; edit the pane as if it were a regular file -- press C-c C-c to commit your
;; changes to the filesystem, or C-c C-k to abort).
;; * Press y to recursively calculate the total size (in bytes) of all files and
;; directories currently selected/marked in the active pane.
;; * Sunrise VIRTUAL mode integrates dired-virtual mode to Sunrise, allowing to
;; capture grep, find and locate results in regular files and to use them later
;; as if they were directories with all the Dired and Sunrise operations at your
;; fingertips.
;;
;; * The results of the following operations are displayed in VIRTUAL mode:
;; - find-name-dired (press C-c C-n),
;; - find-dired (press C-c C-f),
;; - grep (press C-c C-g),
;; - locate (press C-c C-l),
;; - list all recently visited files (press C-c C-r -- requires recentf),
;; - list all directories in active pane's history ring (press C-c C-d).
;; * Supports AVFS (http://avf.sourceforge.net/) for transparent navigation
;; inside compressed archives (*.zip, *.tgz, *.tar.bz2, *.deb, etc. etc.)
;; You need to have AVFS with coda or fuse installed and running on your system
;; for this to work, though.
;; * Opening terminals directly from Sunrise:
;; - Press C-c C-t to inconditionally open a new terminal into the currently
;; selected directory in the active pane.
;; - Use C-c t to switch to the last opened terminal, or (when already inside
;; a terminal) to cycle through all open terminals.
;; - Press C-c T to switch to the last opened terminal and change directory
;; to the one in the current directory.
;; - Press C-c M-t to be prompted for a program name, and then open a new
;; terminal using that program into the currently selected directory
;; (eshell is a valid value; if no program can be found with the given name
;; then the value of `sunrise-terminal-program' is used instead).
;; * Terminal integration and Command line expansion: integrates tightly with
;; `eshell' and `term-mode' to allow interaction between terminal emulators in
;; line mode (C-c C-j) and the panes: the most important navigation commands
;; (up, down, mark, unmark, go to parent dir) can be executed on the active pane
;; directly from the terminal by pressing the usual keys with Meta: <M-up>,
;; <M-down>, etc. Additionally, the following substitutions are automagically
;; performed in `eshell' and `term-line-mode':
;; %f - expands to the currently selected file in the left pane
;; %F - expands to the currently selected file in the right pane
;; %m - expands to the list of paths of all marked files in the left pane
;; %M - expands to the list of paths of all marked files in the right pane
;; %n - expands to the list of names of all marked files in the left pane
;; %N - expands to the list of names of all marked files in the right pane
;; %d - expands to the current directory in the left pane
;; %D - expands to the current directory in the right pane
;; %a - expands to the list of paths of all marked files in the active pane
;; %A - expands to the current directory in the active pane
;; %p - expands to the list of paths of all marked files in the passive pane
;; %P - expands to the current directory in the passive pane
;; * Cloning of complete directory trees: press K to clone the selected files
;; and directories into the passive pane. Cloning is a more general operation
;; than copying, in which all directories are recursively created with the same
;; names and structures at the destination, while what happens to the files
;; within them depends on the option you choose:
;; - "(F)ile System of..." clones the FS structure of paths in a VIRTUAL pane,
;; - "(D)irectories only" ignores all files, copies only directories,
;; - "(C)opies" performs a regular recursive copy of all files and dirs,
;; - "(H)ardlinks" makes every new file a (hard) link to the original one
;; - "(S)ymlinks" creates absolute symbolic links for all files in the tree,
;; - "(R)elative symlinks” creates relative symbolic links.
;; * Passive navigation: the usual navigation keys (n, p, Return, U, ;) combined
;; with Meta allow to move across the passive pane without actually having to
;; switch to it.
;; * Synchronized navigation: press C-c C-z to enable / disable synchronized
;; navigation. In this mode, the passive navigation keys (M-n, M-p, M-Return,
;; etc.) operate on both panes simultaneously. I've found this quite useful for
;; comparing hierarchically small to medium-sized directory trees (for large to
;; very large directory trees one needs something on the lines of diff -r
;; though).
;; * And much more -- press ? while in Sunrise mode for basic help, or h for a
;; complete list of all keybindings available (use C-e and C-y to scroll).
;; There is no help window like in MC, but if you really miss it, just get and
;; install the sunrise-buttons extension.
;; A lot of this code was once adapted from Kevin Burton's mc.el, but it has
;; evolved considerably since then. Another part (the code for file copying and
;; renaming) derives originally from the Dired extensions written by Kurt
;; Nørmark for LAML (http://www.cs.aau.dk/~normark/scheme/distribution/laml/).
;; It's written on GNU Emacs 25 on Linux and tested on GNU Emacs 22, 23, 24 and
;; 25 for Linux and on EmacsW32 (version 23) for Windows. I have also received
;; feedback from users reporting it works OK on the Mac. It does not work either
;; on GNU Emacs 21 or XEmacs -- please drop me a line if you would like to help
;; porting it. All contributions and/or bug reports will be very welcome.
;; For more details on the file manager, several available extensions and many
;; cool tips & tricks visit http://www.emacswiki.org/emacs/Sunrise_Commander
;;; Installation and Usage:
;; 1) Put this file somewhere in your Emacs `load-path'.
;; 2) Add a (require 'sunrise) to your .emacs file.
;; 3) Choose some unused extension for files to be opened in Sunrise VIRTUAL
;; mode and add it to `auto-mode-alist', e.g. if you want to name your virtual
;; directories like *.svrm just add to your .emacs file a line like the
;; following:
;;
;; (add-to-list 'auto-mode-alist '("\\.srvm\\'" . sunrise-virtual-mode))
;; 4) Evaluate the new lines, or reload your .emacs file, or restart Emacs.
;; 5) Type M-x sunrise to invoke the Sunrise Commander (or much better: bind the
;; function to your favorite key combination). The command `sunrise-cd' invokes
;; Sunrise and automatically selects the current file wherever it is in the
;; filesystem. Type h at any moment for information on available key bindings.
;; 6) Type M-x customize-group <RET> sunrise <RET> to customize options, fonts
;; and colors (activate AVFS support here, too).
;; 7) Enjoy :)
;;; Code:
(require 'advice)
(require 'cl-lib)
(require 'desktop)
(require 'dired)
(require 'dired-aux)
(require 'dired-x)
(require 'enriched)
(require 'esh-mode)
(require 'find-dired)
(require 'font-lock)
(require 'hl-line)
(require 'recentf)
(require 'sort)
(require 'term)
(require 'tramp)
(defgroup sunrise nil
"The Sunrise Commander: a two-pane file manager."
:group 'files)
(defcustom sunrise-show-file-attributes t
"Whether to initially display file attributes in Sunrise panes.
You can always toggle file attributes display pressing
\\<sunrise-mode-map>\\[sunrise-toggle-attributes]."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-show-hidden-files nil
"Whether to initially display hidden files in Sunrise panes.
You can always toggle hidden files display pressing
\\<sunrise-mode-map>\\[dired-omit-mode].
You can also customize what files are considered hidden by setting
`dired-omit-files' and `dired-omit-extensions' in your .emacs file."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-visit-buffer-function 'sunrise-visit-buffer-in-current-frame
"Determines how newly opened buffers are to be displayed.
The following options are supported:
* Visit in current frame - Quit Sunrise and display the new
buffer in the current frame.
* Visit in dedicated frame - Create a separate dedicated frame
and display the buffer in it. The frame will be automatically
destroyed when the buffer is killed.
* Other - Provide your own function to display the given buffer."
:group 'sunrise
:type '(choice
(function-item :tag "Visit in current frame" sunrise-visit-buffer-in-current-frame)
(function-item :tag "Visit in dedicated frame" special-display-popup-frame)
(function :tag "Other")))
(defcustom sunrise-terminal-kill-buffer-on-exit t
"If non-nil, kill terminal buffers after their shell process ends."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-terminal-program "eshell"
"The program to use for terminal emulation.
If this value is set to \"eshell\", the Emacs shell (`eshell')
will be used."
:group 'sunrise
:type 'string)
(defcustom sunrise-listing-switches "-al"
"Listing switches passed to `ls' when building Sunrise buffers.
\(Cf. `dired-listing-switches'.)
Most portable value: -al
Recommended value on GNU systems: \
--time-style=locale --group-directories-first -alDhgG"
:group 'sunrise
:type 'string)
(defcustom sunrise-virtual-listing-switches "-ald"
"Listing switches for building buffers in `sunrise-virtual-mode'.
Should not contain the -D option. See also `sunrise-listing-switches'."
:group 'sunrise
:type 'string)
(defun sunrise-set-cursor-follows-mouse (symbol value)
"Helper for `sunrise-cursor-follows-mouse' custom option.
Set SYMBOL to VALUE whenever the option is set."
(mapc (lambda (buf)
(with-current-buffer buf
(when (memq major-mode
'(sunrise-mode
sunrise-tree-mode
sunrise-virtual-mode))
(setq track-mouse value))))
(buffer-list))
(set-default symbol value))
(defcustom sunrise-cursor-follows-mouse t
"Whether text cursor inside Sunrise panes follows mouse cursor.
The mouse is only followed in graphical environments."
:group 'sunrise
:type 'boolean
:set 'sunrise-set-cursor-follows-mouse)
(defcustom sunrise-mouse-events-threshold 10
"Minimum number of mouse movement events before cursor follows mouse.
Helps avoid accidentally capturing the text cursor when Sunrise
is activated."
:group 'sunrise
:type 'integer)
(defcustom sunrise-avfs-root nil
"Root of AVFS virtual filesystem used to navigate compressed archives.
Setting this option activates AVFS support."
:group 'sunrise
:type '(choice
(const :tag "AVFS support disabled" nil)
(const :tag "~/.avfs (default mountavfs mount point)" "~/.avfs")
(directory :tag "Other AVFS root directory")))
(defcustom sunrise-avfs-handlers-alist '(("\\.[jwesh]ar$" . "#uzip/")
("\\.wsar$" . "#uzip/")
("\\.xpi$" . "#uzip/")
("\\.apk$" . "#uzip/")
("\\.iso$" . "#iso9660/")
("\\.patch$" . "#/")
("\\.txz$" . "#/")
("." . "#/"))
"List of AVFS handlers to manage specific file extensions."
:group 'sunrise
:type 'alist)
(defcustom sunrise-md5-shell-command "md5sum %f | cut -d' ' -f1 2>/dev/null"
"Shell command to use for calculating MD5 sums for files.
Used when comparing directories using the ``(c)ontents'' option.
Use %f as a placeholder for the name of the file."
:group 'sunrise
:type 'string)
(defcustom sunrise-window-split-style 'horizontal
"The current window split configuration.
May be `horizontal', `vertical' or `top'."
:group 'sunrise
:type '(choice
(const horizontal)
(const vertical)
(const top)))
(defcustom sunrise-windows-locked t
"When non-nil, vertical size of the panes will remain constant."
:group 'sunrise
:type 'boolean)
(defun sunrise-set-windows-default-ratio (symbol value)
"Helper for `sunrise-windows-default-ratio' custom option.
Set SYMBOL to VALUE whenever the option is set."
(if (and (integerp value) (>= value 0) (<= value 100))
(set-default symbol value)
(error "Invalid value: %s" value)))
(defcustom sunrise-windows-default-ratio 66
"Percentage of frame height to use for Sunrise panes by default."
:group 'sunrise
:type 'integer
:set 'sunrise-set-windows-default-ratio)
(defcustom sunrise-history-length 20
"Number of entries to keep in each pane's history rings."
:group 'sunrise
:type 'integer)
(defcustom sunrise-kill-unused-buffers t
"If non-nil, kill Sunrise buffers no longer shown in any pane."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-kill-quick-view-buffers t
"If non-nil, kill prior quick-view buffers when opening a new one."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-confirm-kill-viewer t
"If non-nil, confirm before killing a quick-view buffer."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-attributes-display-mask nil
"Contols hiding/transforming columns with `sunrise-toggle-attributes'.
If set, its value must be a list of symbols, one for each
attributes column. If the symbol is nil, then the corresponding
column will be hidden, and if it's not nil then the column will
be left untouched. The symbol may also be the name of a function
that takes one string argument and evaluates to a different
string -- in this case this function will be used to transform
the contents of the corresponding column and its result will be
displayed instead."
:group 'sunrise
:type '(repeat symbol))
(defcustom sunrise-fast-backup-extension ".bak"
"Extension for files created by `sunrise-fast-backup-files' (@!).
This can be either a string or an Lisp form to be evaluated at
run-time."
:group 'sunrise
:type '(choice
(string :tag "Literal text")
(sexp :tag "Symbolic expression")))
(defcustom sunrise-traditional-other-window nil
"If non-nil, focus selected pane when switching from non-Sunrise windows.
By default, Sunrise modifies the behavior of Emacs `other-window'
command so that focus is always given to the currently selected
pane when switching to any Sunrise window from a non-Sunrise
window. If you'd prefer the default Emacs behavior instead, set
this flag to t."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-fuzzy-negation-character ?!
"Character to use for negating patterns when fuzzy-narrowing a pane."
:group 'sunrise
:type '(choice
(const :tag "Fuzzy matching negation disabled" nil)
(character :tag "Fuzzy matching negation character" ?!)))
;;;###autoload
(defcustom sunrise-mode-hook nil
"Run at the very end of `sunrise-mode'."
:group 'sunrise
:type 'hook)
(defcustom sunrise-init-hook nil
"List of functions to be called before the Sunrise panes are displayed."
:group 'sunrise
:type 'hook
:options '(auto-insert))
(defcustom sunrise-start-hook nil
"List of functions to be called after the Sunrise panes are displayed."
:group 'sunrise
:type 'hook
:options '(auto-insert))
(defcustom sunrise-refresh-hook nil
"List of functions to be called every time a pane is refreshed."
:group 'sunrise
:type 'hook
:options '(auto-insert))
(defcustom sunrise-quit-hook nil
"List of functions to be called after the Sunrise panes are hidden."
:group 'sunrise
:type 'hook
:options '(auto-insert))
(defcustom sunrise-recursive-grep-supported t
"If non-nil, `sunrise-grep-command' supports the \"-r\" recursive flag."
:group 'sunrise
:type 'boolean)
(defcustom sunrise-grep-command "grep"
"Full path to the grep command for Sunrise to use.
In contrast to Emacs' own `grep-command', this one does not
support any options."
:group 'sunrise
:type 'string)
(defvar sunrise-restore-buffer nil
"Buffer to restore when Sunrise quits.")
(defvar sunrise-prior-window-configuration nil
"Window configuration before Sunrise was started.")
(defvar sunrise-synchronized nil
"Non-nil when synchronized navigation is on.")
(defvar sunrise-current-window-overlay nil
"Holds the current overlay which marks the current Dired buffer.")
(defvar sunrise-clex-hotchar-overlay nil
"Overlay used to highlight the hot character (%) during CLEX operations.")
(defvar sunrise-left-directory "~/"
"Dired directory for the left window. See variable `dired-directory'.")
(defvar sunrise-left-buffer nil
"Dired buffer for the left window.")
(defvar sunrise-right-directory "~/"
"Dired directory for the right window. See variable `dired-directory'.")
(defvar sunrise-right-buffer nil
"Dired buffer for the right window.")
(defvar sunrise-current-frame nil
"The frame Sunrise is active on (if any).")
(defvar sunrise-this-directory "~/"
"Dired directory in the active pane.
This isn't necessarily the same as `dired-directory'.")
(defvar sunrise-other-directory "~/"
"Dired directory in the passive pane.")
(defvar sunrise-selected-window 'a
"The window to select when Sunrise starts up.")
(defvar sunrise-selected-window-width nil
"The width the selected window should have on startup.")
(defvar sunrise-history-registry '((left) (right))
"Registry of visited directories for both panes.")
(defvar sunrise-history-stack '((left 0 . 0) (right 0 . 0))
"History stack counters.
The first counter on each side tracks (by value) the absolute
depth of the stack and (by sign) the direction it is currently
being traversed. The second counter points at the position of the
element that is immediately beneath the top of the stack.")
(defvar sunrise-ti-openterms nil
"Stack of currently open terminal buffers.")
(defvar sunrise-ediff-on nil
"Flag that indicates whether an `ediff' is being currently done.")
(defvar sunrise-clex-on nil
"Flag that indicates that a CLEX operation is taking place.")
(defvar-local sunrise-virtual-buffer nil
"If non-nil, the current buffer was originally in Sunrise virtual mode.")
(defvar sunrise-dired-directory ""
"Directory inside which `sunrise-mode' is currently active.")
(defvar sunrise-start-message
"Been coding all night? Enjoy the Sunrise! (or press q to quit)"
"Message to display when Sunrise is started.")
(defvar sunrise-panes-height nil
"Current height of the pane windows.
Initial value is 2/3 the viewport height.")
(defvar-local sunrise-current-path-faces nil
"List of faces to display the path in the current pane (first wins)")
(defvar sunrise-inhibit-highlight nil
"Special variable used to temporarily inhibit highlighting in panes.")
(defvar sunrise-inhibit-switch nil
"Special variable used to inhibit switching from one pane to the other.")
(defvar sunrise-find-items nil
"Special variable used by `sunrise-find' to control the scope of find operations.")
(defvar sunrise-desktop-save-handlers nil
"List of extension-defined handlers to save Sunrise buffers with desktop.")
(defvar sunrise-desktop-restore-handlers nil
"List of extension-defined handlers to restore Sunrise buffers from desktop.")
(defvar-local sunrise-backup-buffer nil
"Variable holding a buffer-local value of the backup buffer.")
(defvar sunrise-goto-dir-function nil
"Function to use to navigate to a particular directory.
Set to nil for default behavior. The function receives one
argument DIR which is the directory to go to.")
(defvar sunrise-mouse-events-count 0
"Number of mouse movement events received since switching to Sunrise.
Used with `sunrise-mouse-events-threshold' to selectively
activate `sunrise-cursor-follows-mouse'.")
(defconst sunrise-side-lookup (list '(left . right) '(right . left))
"Trivial alist used by the Sunrise Commander to lookup its own passive side.")
(defgroup sunrise-faces nil
"Faces used by Sunrise Commander"
:group 'sunrise)
(defface sunrise-active-path-face
'((((type tty) (class color) (min-colors 8))
:background "green" :foreground "yellow" :bold t)
(((type tty) (class mono)) :inverse-video t)
(t :background "#ace6ac" :foreground "yellow" :bold t :height 120))
"Face of the directory path in the active pane."
:group 'sunrise-faces)
(defface sunrise-passive-path-face
'((((type tty) (class color) (min-colors 8) (background dark))
:background "black" :foreground "cyan")
(((type tty) (class color) (min-colors 8) (background light))
:background "white" :foreground "cyan")
(t :background "white" :foreground "lightgray" :bold t :height 120))
"Face of the directory path in the passive pane."
:group 'sunrise-faces)
(defface sunrise-editing-path-face
'((t :background "red" :foreground "yellow" :bold t :height 120))
"Face of the directory path in the active pane while in editable pane mode."
:group 'sunrise-faces)
(defface sunrise-highlight-path-face
'((t :background "yellow" :foreground "#ace6ac" :bold t :height 120))
"Face of the directory path on mouse hover."
:group 'sunrise-faces)
(defface sunrise-clex-hotchar-face
'((t :foreground "red" :bold t))
"Face of the hot character (%) in CLEX mode.
Indicates that a CLEX substitution may be about to happen."
:group 'sunrise-faces)
;;; ============================================================================
;;; This is the core of Sunrise: the main idea is to apply `sunrise-mode' only inside
;;; Sunrise buffers while keeping all of `dired-mode' untouched.
;;; preserve this variable when switching from `dired-mode' to another mode
(put 'dired-subdir-alist 'permanent-local t)
;;;###autoload
(define-derived-mode sunrise-mode dired-mode "Sunrise Commander"
"Two-pane file manager for Emacs based on Dired and inspired by MC.
The following keybindings are available:
/, j .......... go to directory
p, n .......... move cursor up/down
M-p, M-n ...... move cursor up/down in passive pane
^, J .......... go to parent directory
M-^, M-J ...... go to parent directory in passive pane
Tab ........... switch to other pane
C-Tab.......... switch to viewer window
C-c Tab ....... switch to viewer window (console compatible)
RET, f ........ visit selected file/directory
M-RET, M-f .... visit selected file/directory in passive pane
C-c RET ....... visit selected in passive pane (console compatible)
b ............. visit selected file/directory in default browser
F ............. visit all marked files, each in its own window
C-u F ......... visit all marked files in the background
o,v ........... quick visit selected file (scroll with C-M-v, C-M-S-v)
C-u o, C-u v .. kill quick-visited buffer (restores normal scrolling)
X ............. execute selected file
C-u X.......... execute selected file with arguments
+ ............. create new directory
M-+ ........... create new empty file(s)
C ............. copy marked (or current) files and directories
R ............. rename marked (or current) files and directories
D ............. delete marked (or current) files and directories
S ............. soft-link selected file/directory to passive pane
Y ............. do relative soft-link of selected file in passive pane
H ............. hard-link selected file to passive pane
K ............. clone selected files and directories into passive pane
N ............. in place copy/rename/link marked (or current) entries
M-C ........... copy (using traditional dired-do-copy)
M-R ........... rename (using traditional dired-do-rename)
M-D ........... delete (using traditional dired-do-delete)
M-S............ soft-link (using traditional dired-do-symlink)
M-Y............ do relative soft-link (traditional dired-do-relsymlink)
M-H............ hard-link selected file/directory (dired-do-hardlink)
A ............. search marked files for regular expression
Q ............. perform query-replace-regexp on marked files
C-q ........... search occurrences of a string in marked files
C-c s ......... start a \"sticky\" interactive search in the current pane
M-a ........... move to beginning of current directory
M-e ........... move to end of current directory
M-y ........... go to previous directory in history
M-u ........... go to next directory in history
C-M-y ......... go to previous directory in history on passive pane
C-M-u ......... go to next directory in history on passive pane
g, C-c C-c .... refresh pane
s ............. sort entries (by name, number, size, time or extension)
r ............. reverse the order of entries in the active pane (sticky)
C-o ........... show/hide hidden files (requires dired-omit-mode)
C-Backspace ... hide/show file attributes in pane
C-c Backspace . hide/show file attributes in pane (console compatible)
y ............. show file type / size of selected files and directories.
M-l ........... truncate/continue long lines in pane
C-c v ......... put current panel in VIRTUAL mode
C-c C-v ....... create new pure VIRTUAL buffer
C-c C-w ....... browse directory tree using w3m
M-t ........... transpose panes
M-o ........... synchronize panes
C-c C-s ....... change panes layout (vertical/horizontal/top-only)
[ ............. enlarges the right pane by 5 columns
] ............. enlarges the left pane by 5 columns
} ............. enlarges the panes vertically by 1 row
C-} ........... enlarges the panes vertically as much as it can
C-c } ......... enlarges the panes vertically as much as it can
{ ............. shrinks the panes vertically by 1 row
C-{ ........... shrinks the panes vertically as much as it can
C-c { ......... shrinks the panes vertically as much as it can
\\ ............. restores the size of all windows back to «normal»
C-c C-z ....... enable/disable synchronized navigation
C-= ........... smart compare files (ediff)
C-c = ......... smart compare files (console compatible)
= ............. fast smart compare files (plain diff)
C-M-= ......... compare panes
C-x = ......... compare panes (console compatible)
C-c C-f ....... execute Find-dired in Sunrise VIRTUAL mode
C-c C-n ....... execute find-Name-dired in Sunrise VIRTUAL mode
C-u C-c C-g ... execute find-Grep-dired with additional grep options
C-c C-g ....... execute grep in Sunrise VIRTUAL mode
C-c C-l ....... execute Locate in Sunrise VIRTUAL mode
C-c C-r ....... browse list of Recently visited files (requires recentf)
C-c C-c ....... [after find, locate or recent] dismiss virtual buffer
C-c / ......... narrow the contents of current pane using fuzzy matching
C-c b ......... partial Branch view of selected items in current pane
C-c p ......... Prune paths matching regular expression from current pane
; ............. follow file (go to same directory as selected file)
M-; ........... follow file in passive pane
C-M-o ......... follow a projection of current directory in passive pane
C-> ........... save named checkpoint (a.k.a. \"bookmark panes\")
C-c > ......... save named checkpoint (console compatible)
C-. ........ restore named checkpoint
C-c . ........ restore named checkpoint
C-x C-q ....... put pane in Editable Dired mode (commit with C-c C-c)
@! ............ fast backup files (not dirs!), each to [filename].bak
C-c t ......... open new terminal or switch to already open one
C-c T ......... open terminal AND/OR change directory to current
C-c C-t ....... open always a new terminal in current directory
C-c M-t ....... open a new terminal using an alternative shell program
q, C-x k ...... quit Sunrise Commander, restore previous window setup
M-q ........... quit Sunrise Commander, don't restore previous windows
Additionally, the following traditional commander-style keybindings are provided
\(these may be disabled by customizing the `sunrise-use-commander-keys' option):
F2 ............ go to directory
F3 ............ quick visit selected file
F4 ............ visit selected file
F5 ............ copy marked (or current) files and directories
F6 ............ rename marked (or current) files and directories
F7 ............ create new directory
F8 ............ delete marked (or current) files and directories
F10 ........... quit Sunrise Commander
C-F3 .......... sort contents of current pane by name
C-F4 .......... sort contents of current pane by extension
C-F5 .......... sort contents of current pane by time
C-F6 .......... sort contents of current pane by size
C-F7 .......... sort contents of current pane numerically
S-F7 .......... soft-link selected file/directory to passive pane
Insert ........ mark file
C-PgUp ........ go to parent directory
Any other dired keybinding (not overridden by any of the above) can be used in
Sunrise, like G for changing group, M for changing mode and so on.
Some more bindings are available in terminals opened using any of the Sunrise
functions (i.e. one of: C-c t, C-c T, C-c C-t, C-c M-t):
C-c Tab ....... switch focus to the active pane
C-c t ......... cycle through all currently open terminals
C-c T ......... cd to the directory in the active pane
C-c C-t ....... open new terminal, cd to directory in the active pane
C-c ; ......... follow the current directory in the active pane
C-c { ......... shrink the panes vertically as much as possible
C-c } ......... enlarge the panes vertically as much as possible
C-c \\ ......... restore the size of all windows back to «normal»
C-c C-j ....... put terminal in line mode
C-c C-k ....... put terminal back in char mode
The following bindings are available only in line mode (eshell is considered to
be *always* in line mode):
M-<up>, M-P ... move cursor up in the active pane
M-<down>, M-N . move cursor down in the active pane
M-Return ...... visit selected file/directory in the active pane
M-J ........... go to parent directory in the active pane
M-G ........... refresh active pane
M-Tab ......... switch to passive pane (without leaving the terminal)
M-M ........... mark selected file/directory in the active pane
M-Backspace ... unmark previous file/directory in the active pane
M-U ........... remove all marks from the active pane
C-Tab ......... switch focus to the active pane
In a terminal in line mode the following substitutions are also performed
automatically:
%f - expands to the currently selected file in the left pane
%F - expands to the currently selected file in the right pane
%m - expands to the list of paths of all marked files in the left pane
%M - expands to the list of paths of all marked files in the right pane
%n - expands to the list of names of all marked files in the left pane
%N - expands to the list of names of all marked files in the right pane
%d - expands to the current directory in the left pane
%D - expands to the current directory in the right pane
%a - expands to the list of paths of all marked files in the active pane
%A - expands to the current directory in the active pane
%p - expands to the list of paths of all marked files in the passive pane
%P - expands to the current directory in the passive pane
%% - inserts a single % sign.
"
:group 'sunrise
(unless (string-match "\\(Sunrise\\)" (buffer-name))
(rename-buffer (concat (buffer-name) " (Sunrise)") t))
(set-keymap-parent sunrise-mode-map dired-mode-map)
(sunrise-highlight)
(dired-omit-mode dired-omit-mode)
(make-local-variable 'truncate-partial-width-windows)
(setq truncate-partial-width-windows (sunrise-truncate-v t))
(set (make-local-variable 'buffer-read-only) t)
(set (make-local-variable 'dired-header-face) 'sunrise-passive-path-face)
(set (make-local-variable 'truncate-lines) nil)
(set (make-local-variable 'desktop-save-buffer) 'sunrise-desktop-save-buffer)
(set (make-local-variable 'revert-buffer-function) 'sunrise-revert-buffer)
(set (make-local-variable 'buffer-quit-function) 'sunrise-quit)
(set (make-local-variable 'sunrise-show-file-attributes) sunrise-show-file-attributes)
(set (make-local-variable 'mouse-1-click-follows-link) nil)
(set (make-local-variable 'track-mouse) sunrise-cursor-follows-mouse)
(set (make-local-variable 'hl-line-sticky-flag) nil)
(hl-line-mode 1)
(run-mode-hooks 'sunrise-mode-hook))
;;;###autoload
(define-derived-mode sunrise-virtual-mode dired-virtual-mode "Sunrise VIRTUAL"
"Sunrise Commander Virtual Mode. Useful for reusing find and locate results."
:group 'sunrise
(set-keymap-parent sunrise-virtual-mode-map sunrise-mode-map)
(sunrise-highlight)
(enriched-mode -1)
(make-local-variable 'truncate-partial-width-windows)
(setq truncate-partial-width-windows (sunrise-truncate-v t))
(set (make-local-variable 'buffer-read-only) t)
(set (make-local-variable 'dired-header-face) 'sunrise-passive-path-face)
(set (make-local-variable 'truncate-lines) nil)
(set (make-local-variable 'desktop-save-buffer) 'sunrise-desktop-save-buffer)
(set (make-local-variable 'revert-buffer-function) 'sunrise-revert-buffer)
(set (make-local-variable 'buffer-quit-function) 'sunrise-quit)
(set (make-local-variable 'sunrise-show-file-attributes) sunrise-show-file-attributes)
(set (make-local-variable 'mouse-1-click-follows-link) nil)
(set (make-local-variable 'track-mouse) sunrise-cursor-follows-mouse)
(set (make-local-variable 'hl-line-sticky-flag) nil)
(hl-line-mode 1)
(define-key sunrise-virtual-mode-map "\C-c\C-c" 'sunrise-virtual-dismiss)
(define-key sunrise-virtual-mode-map "\C-cv" 'sunrise-backup-buffer))
(defmacro sunrise-within (dir form)
"Evaluate FORM in Sunrise Commander context in directory DIR."
`(unwind-protect
(progn
(setq sunrise-dired-directory
(file-name-as-directory (abbreviate-file-name ,dir)))
(ad-activate 'dired-find-buffer-nocreate)
,form)
(ad-deactivate 'dired-find-buffer-nocreate)
(setq sunrise-dired-directory "")))
(defmacro sunrise-save-aspect (&rest body)
"Restore omit mode, hidden attributes and point after a directory transition.
BODY is the code to evaluate within an implicit `progn'."
`(let ((inhibit-read-only t)
(omit (or dired-omit-mode -1))
(attrs (eval 'sunrise-show-file-attributes))
(path-faces sunrise-current-path-faces))
,@body
(dired-omit-mode omit)
(when path-faces
(setq sunrise-current-path-faces path-faces))
(when (string= "NUMBER" (get sunrise-selected-window 'sorting-order))
(sunrise-sort-by-operation 'sunrise-numerical-sort-op))
(when (get sunrise-selected-window 'sorting-reverse)
(sunrise-reverse-pane))
(setq sunrise-show-file-attributes attrs)
(sunrise-display-attributes (point-min) (point-max) sunrise-show-file-attributes)
(sunrise-restore-point-if-same-buffer)))
(defmacro sunrise-save-selected-window (&rest body)
"Execute BODY, then select the previously selected window.
During the operation, `sunrise-inhibit-switch' is set to t.
Uses `save-selected-window' internally."
`(let ((sunrise-inhibit-switch t))
(save-selected-window
,@body)))
(defmacro sunrise-alternate-buffer (form)
"Execute FORM in a new buffer, after killing the previous one."
`(let ((dispose nil))
(unless (or (not (or dired-directory (eq major-mode 'sunrise-tree-mode)))
(eq sunrise-left-buffer sunrise-right-buffer))
(setq dispose (current-buffer)))
,form
(setq sunrise-this-directory default-directory)
(sunrise-keep-buffer)
(sunrise-highlight)
(when (and sunrise-kill-unused-buffers (buffer-live-p dispose))
(with-current-buffer dispose
(bury-buffer)
(set-buffer-modified-p nil)
(unless (kill-buffer dispose)
(kill-local-variable 'sunrise-current-path-faces))))))
(defun sunrise-classify-buffer (buffer)
"Return 'pane or nil depending on Sunrise Commander type of BUFFER."
(let ((mode (and buffer (with-current-buffer buffer major-mode))))
(if (eq 'sunrise-mode mode) 'pane nil)))
(defun sunrise-classify-window (window)
"Return 'pane or nil depending on Sunrise Commander type of WINDOW."
(sunrise-classify-buffer (window-buffer window)))
(defun sunrise--directory-windows-p (window1 window2)
"Return t if WINDOW1 and WINDOW2 are two Sunrise directory panes."
(and (eq 'pane (sunrise-classify-window window1))
(eq 'pane (sunrise-classify-window window2))))
(defun sunrise--analyze-frame-or-nil (&optional frame)
"Return the Sunrise Commander state in FRAME.
Returns nil if Sunrise is not running in FRAME.
Otherwise returns a 3-element list:
\(left-window right-window viewer-window)
* left-window is never nil.
* right-window is nil if `sunrise-window-split-style' is 'top.
* viewer-window is nil if the viewer pane is not shown.
Use `window-buffer' to to get at the buffer of each window, and
consult `default-directory' in each directory buffer to find out
which directories the user is browsing."
(let ((root (frame-root-window frame)))
(cl-ecase sunrise-window-split-style
(horizontal
(and (= 2 (window-child-count root))
(let* ((top (window-top-child root))
(bot (window-next-sibling top)))
(and (= 2 (window-child-count top))
(let* ((top-l (window-left-child top))
(top-r (window-right top-l)))
(and (sunrise--directory-windows-p top-l top-r)
(window-live-p bot)
(list top-l top-r bot)))))))
(vertical
(and (= 3 (window-child-count root))
(let* ((top (window-top-child root))
(mid (window-next-sibling top))
(bot (window-next-sibling mid)))
(and (sunrise--directory-windows-p top mid)
(window-live-p bot)
(list top mid bot)))))
(top
(and (= 2 (window-child-count root))
(let* ((top (window-top-child root))
(bot (window-next-sibling top)))
(and (eq 'pane (sunrise-classify-window
(window-top-child root)))
(window-live-p bot)
(list top nil bot))))))))
(defun sunrise--analyze-frame (&optional frame)
"Helper to analyze Sunrise windows in FRAME."
(or (sunrise--analyze-frame-or-nil frame) (list nil nil nil)))
(defun sunrise--left-window-maybe (&optional frame)
"Helper to get Sunrise left pane in FRAME."
(cl-destructuring-bind (left-window _right-window _viewer-window)
(sunrise--analyze-frame frame)
left-window))
(defun sunrise--right-window-maybe (&optional frame)
"Helper to get Sunrise right pane in FRAME."
(cl-destructuring-bind (_left-window right-window _viewer-window)
(sunrise--analyze-frame frame)
right-window))
(defun sunrise-running-p (&optional frame)
"Return t if the Sunrise Commander is being displayed in FRAME.
FRAME defaults to the selected frame. The window layout in FRAME
is analyzed to find out whether it contains the Sunrise directory
panes and a viewer pane in the expected layout.