forked from emacs-helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-files.el
3393 lines (3106 loc) · 146 KB
/
helm-files.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
;;; helm-files.el --- helm file browser and related. -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2015 Thierry Volpiatto <thierry.volpiatto@gmail.com>
;; 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 details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-types)
(require 'helm-utils)
(require 'helm-external)
(require 'helm-grep)
(require 'helm-help)
(require 'helm-locate)
(require 'helm-bookmark)
(require 'helm-tags)
(require 'helm-buffers)
(require 'thingatpt)
(require 'ffap)
(require 'dired-aux)
(require 'dired-x)
(require 'tramp)
(require 'image-dired)
(declare-function find-library-name "find-func.el" (library))
(declare-function w32-shell-execute "ext:w32fns.c" (operation document &optional parameters show-flag))
(declare-function gnus-dired-attach "ext:gnus-dired.el" (files-to-attach))
(declare-function image-dired-display-image "image-dired.el" (file &optional original-size))
(declare-function image-dired-update-property "image-dired.el" (prop value))
(declare-function eshell-read-aliases-list "em-alias")
(declare-function eshell-send-input "esh-mode" (&optional use-region queue-p no-newline))
(declare-function eshell-kill-input "esh-mode")
(declare-function eshell-bol "esh-mode")
(declare-function eshell-quote-argument "esh-arg.el")
(declare-function helm-ls-git-ls "ext:helm-ls-git")
(declare-function helm-hg-find-files-in-project "ext:helm-ls-hg")
(declare-function helm-gid "helm-id-utils.el")
(declare-function helm-ls-svn-ls "ext:helm-ls-svn")
(defvar recentf-list)
(defvar helm-mm-matching-method)
;;; Type attributes
;;
;;
(define-helm-type-attribute 'file
(helm-build-type-file)
"File name.")
(defgroup helm-files nil
"Files applications and libraries for Helm."
:group 'helm)
(defcustom helm-boring-file-regexp-list
(mapcar (lambda (f)
(concat
(rx-to-string
(replace-regexp-in-string
"/$" "" f) t) "$"))
completion-ignored-extensions)
"The regexp list matching boring files."
:group 'helm-files
:type '(repeat (choice regexp)))
(defcustom helm-for-files-preferred-list
'(helm-source-buffers-list
helm-source-recentf
helm-source-bookmarks
helm-source-file-cache
helm-source-files-in-current-dir
helm-source-locate)
"Your preferred sources to find files."
:type '(repeat (choice symbol))
:group 'helm-files)
(defcustom helm-tramp-verbose 0
"Just like `tramp-verbose' but specific to helm.
When set to 0 don't show tramp messages in helm.
If you want to have the default tramp messages set it to 3."
:type 'integer
:group 'helm-files)
(defcustom helm-ff-auto-update-initial-value nil
"Auto update when only one candidate directory is matched.
Default value when starting `helm-find-files' is nil because
it prevent using <backspace> to delete char backward and by the way
confuse beginners.
For a better experience with `helm-find-files' set this to non--nil
and use C-<backspace> to toggle it."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-lynx-style-map t
"Use arrow keys to navigate with `helm-find-files'.
You will have to restart Emacs or reeval `helm-find-files-map'
and `helm-read-file-map' for this take effect."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-history-max-length 100
"Number of elements shown in `helm-find-files' history."
:group 'helm-files
:type 'integer)
(defcustom helm-ff-smart-completion t
"Try to complete filenames smarter when non--nil.
See `helm-ff--transform-pattern-for-completion' for more info."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-tramp-not-fancy t
"No colors when listing remote files when set to non--nil.
This make listing much faster, specially on slow machines."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-exif-data-program "exiftran"
"Program used to extract exif data of an image file."
:group 'helm-files
:type 'string)
(defcustom helm-ff-exif-data-program-args "-d"
"Arguments used for `helm-ff-exif-data-program'."
:group 'helm-files
:type 'string)
(defcustom helm-ff-newfile-prompt-p t
"Whether Prompt or not when creating new file.
This set `ffap-newfile-prompt'."
:type 'boolean
:group 'helm-files)
(defcustom helm-ff-avfs-directory "~/.avfs"
"The default avfs directory, usually '~/.avfs'.
When this is set you will be able to expand archive filenames with `C-j'
inside an avfs directory mounted with mountavfs.
See <http://sourceforge.net/projects/avf/>."
:type 'string
:group 'helm-files)
(defcustom helm-ff-file-compressed-list '("gz" "bz2" "zip" "7z")
"Minimal list of compressed files extension."
:type '(repeat (choice string))
:group 'helm-files)
(defcustom helm-ff-printer-list nil
"A list of available printers on your system.
When non--nil let you choose a printer to print file.
Otherwise when nil the variable `printer-name' will be used.
On Unix based systems (lpstat command needed) you don't need to set this,
`helm-ff-find-printers' will find a list of available printers for you."
:type '(repeat (choice string))
:group 'helm-files)
(defcustom helm-ff-transformer-show-only-basename t
"Show only basename of candidates in `helm-find-files'.
This can be toggled at anytime from `helm-find-files' with \
\\<helm-find-files-map>\\[helm-ff-run-toggle-basename]."
:type 'boolean
:group 'helm-files)
(defcustom helm-ff-signal-error-on-dot-files t
"Signal error when file is `.' or `..' on file deletion when non--nil.
Default is non--nil.
WARNING: Setting this to nil is unsafe and can cause deletion of a whole tree."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-search-library-in-sexp nil
"Search for library in `require' and `declare-function' sexp."
:group 'helm-files
:type 'boolean)
(defcustom helm-tooltip-hide-delay 25
"Hide tooltips automatically after this many seconds."
:group 'helm-files
:type 'integer)
(defcustom helm-ff-file-name-history-use-recentf nil
"Use `recentf-list' instead of `file-name-history' in `helm-find-files'."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-skip-boring-files nil
"Non--nil to skip files matching regexps in `helm-boring-file-regexp-list'.
This take effect in `helm-find-files' and file completion used by `helm-mode'
i.e `helm-read-file-name'."
:group 'helm-files
:type 'boolean)
(defcustom helm-ff-candidate-number-limit 5000
"The `helm-candidate-number-limit' for `helm-find-files', `read-file-name' and friends."
:group 'helm-files
:type 'integer)
(defcustom helm-findutils-skip-boring-files t
"Ignore files matching regexps in `completion-ignored-extensions'."
:group 'helm-files
:type 'boolean)
(defcustom helm-findutils-search-full-path nil
"Search in full path with shell command find when non--nil.
I.e use the -path/ipath arguments of find instead of -name/iname."
:group 'helm-files
:type 'boolean)
(defcustom helm-files-save-history-extra-sources
'("Find" "Locate" "Recentf"
"Files from Current Directory" "File Cache")
"Extras source that save candidate to `file-name-history'."
:group 'helm-files
:type '(repeat (choice string)))
(defcustom helm-find-files-before-init-hook nil
"Hook that run before initialization of `helm-find-files'."
:group 'helm-files
:type 'hook)
(defcustom helm-find-files-after-init-hook nil
"Hook that run after initialization of `helm-find-files'."
:group 'helm-files
:type 'hook)
(defcustom helm-multi-files-toggle-locate-binding "C-c p"
"Default binding to switch back and forth locate in `helm-multi-files'."
:group 'helm-files
:type 'string)
(defcustom helm-find-files-bookmark-prefix "Helm-find-files: "
"bookmark name prefix of `helm-find-files' sessions."
:group 'helm-files
:type 'string)
;;; Faces
;;
;;
(defgroup helm-files-faces nil
"Customize the appearance of helm-files."
:prefix "helm-"
:group 'helm-files
:group 'helm-faces)
(defface helm-ff-prefix
'((t (:background "yellow" :foreground "black")))
"Face used to prefix new file or url paths in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-executable
'((t (:foreground "green")))
"Face used for executable files in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-directory
'((t (:foreground "DarkRed" :background "LightGray")))
"Face used for directories in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-dotted-directory
'((t (:foreground "black" :background "DimGray")))
"Face used for dotted directories in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-dotted-symlink-directory
'((t (:foreground "DarkOrange" :background "DimGray")))
"Face used for dotted symlinked directories in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-symlink
'((t (:foreground "DarkOrange")))
"Face used for symlinks in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-invalid-symlink
'((t (:foreground "black" :background "red")))
"Face used for invalid symlinks in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-ff-file
'((t (:inherit font-lock-builtin-face)))
"Face used for file names in `helm-find-files'."
:group 'helm-files-faces)
(defface helm-history-deleted
'((t (:inherit helm-ff-invalid-symlink)))
"Face used for deleted files in `file-name-history'."
:group 'helm-files-faces)
(defface helm-history-remote
'((t (:foreground "Indianred1")))
"Face used for remote files in `file-name-history'."
:group 'helm-files-faces)
;;; Helm-find-files - The helm file browser.
;;
;; Keymaps
(defvar helm-find-files-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
(define-key map (kbd "C-x C-f") 'helm-ff-run-locate)
(define-key map (kbd "C-x C-d") 'helm-ff-run-browse-project)
(define-key map (kbd "C-x r m") 'helm-ff-bookmark-set)
(define-key map (kbd "C-x r b") 'helm-find-files-toggle-to-bookmark)
(define-key map (kbd "C-s") 'helm-ff-run-grep)
(define-key map (kbd "M-g s") 'helm-ff-run-grep)
(define-key map (kbd "M-g p") 'helm-ff-run-pdfgrep)
(define-key map (kbd "M-g z") 'helm-ff-run-zgrep)
(define-key map (kbd "M-g a") 'helm-ff-run-grep-ag)
(define-key map (kbd "C-c g") 'helm-ff-run-gid)
(define-key map (kbd "M-.") 'helm-ff-run-etags)
(define-key map (kbd "M-R") 'helm-ff-run-rename-file)
(define-key map (kbd "M-C") 'helm-ff-run-copy-file)
(define-key map (kbd "M-B") 'helm-ff-run-byte-compile-file)
(define-key map (kbd "M-L") 'helm-ff-run-load-file)
(define-key map (kbd "M-S") 'helm-ff-run-symlink-file)
(define-key map (kbd "M-H") 'helm-ff-run-hardlink-file)
(define-key map (kbd "M-D") 'helm-ff-run-delete-file)
(define-key map (kbd "M-K") 'helm-ff-run-kill-buffer-persistent)
(define-key map (kbd "C-c d") 'helm-ff-persistent-delete)
(define-key map (kbd "M-e") 'helm-ff-run-switch-to-eshell)
(define-key map (kbd "C-c i") 'helm-ff-run-complete-fn-at-point)
(define-key map (kbd "C-c o") 'helm-ff-run-switch-other-window)
(define-key map (kbd "C-c C-o") 'helm-ff-run-switch-other-frame)
(define-key map (kbd "C-c C-x") 'helm-ff-run-open-file-externally)
(define-key map (kbd "C-c X") 'helm-ff-run-open-file-with-default-tool)
(define-key map (kbd "M-!") 'helm-ff-run-eshell-command-on-file)
(define-key map (kbd "M-%") 'helm-ff-run-query-replace-on-marked)
(define-key map (kbd "C-c =") 'helm-ff-run-ediff-file)
(define-key map (kbd "M-=") 'helm-ff-run-ediff-merge-file)
(define-key map (kbd "M-p") 'helm-ff-run-switch-to-history)
(define-key map (kbd "C-c h") 'helm-ff-file-name-history)
(define-key map (kbd "M-i") 'helm-ff-properties-persistent)
(define-key map (kbd "C-}") 'helm-narrow-window)
(define-key map (kbd "C-{") 'helm-enlarge-window)
(define-key map (kbd "C-<backspace>") 'helm-ff-run-toggle-auto-update)
(define-key map (kbd "C-c <DEL>") 'helm-ff-run-toggle-auto-update)
(define-key map (kbd "C-c C-a") 'helm-ff-run-gnus-attach-files)
(define-key map (kbd "C-c p") 'helm-ff-run-print-file)
(define-key map (kbd "C-c /") 'helm-ff-run-find-sh-command)
;; Next 2 have no effect if candidate is not an image file.
(define-key map (kbd "M-l") 'helm-ff-rotate-left-persistent)
(define-key map (kbd "M-r") 'helm-ff-rotate-right-persistent)
(define-key map (kbd "C-l") 'helm-find-files-up-one-level)
(define-key map (kbd "C-r") 'helm-find-files-down-last-level)
(define-key map (kbd "C-c r") 'helm-ff-run-find-file-as-root)
(define-key map (kbd "C-c @") 'helm-ff-run-insert-org-link)
(helm-define-key-with-subkeys map (kbd "DEL") ?\d 'helm-ff-delete-char-backward
nil nil 'helm-ff-delete-char-backward--exit-fn)
(when helm-ff-lynx-style-map
(define-key map (kbd "<left>") 'helm-find-files-up-one-level)
(define-key map (kbd "<right>") 'helm-execute-persistent-action))
(delq nil map))
"Keymap for `helm-find-files'.")
(defvar helm-read-file-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "<C-return>") 'helm-cr-empty-string)
(define-key map (kbd "<M-RET>") 'helm-cr-empty-string)
(define-key map (kbd "C-]") 'helm-ff-run-toggle-basename)
(define-key map (kbd "C-.") 'helm-find-files-up-one-level)
(define-key map (kbd "C-l") 'helm-find-files-up-one-level)
(define-key map (kbd "C-r") 'helm-find-files-down-last-level)
(define-key map (kbd "C-c h") 'helm-ff-file-name-history)
(define-key map (kbd "C-<backspace>") 'helm-ff-run-toggle-auto-update)
(define-key map (kbd "C-c <DEL>") 'helm-ff-run-toggle-auto-update)
(helm-define-key-with-subkeys map (kbd "DEL") ?\d 'helm-ff-delete-char-backward
nil nil 'helm-ff-delete-char-backward--exit-fn)
(when helm-ff-lynx-style-map
(define-key map (kbd "<left>") 'helm-find-files-up-one-level)
(define-key map (kbd "<right>") 'helm-execute-persistent-action)
(define-key map (kbd "<M-left>") 'helm-previous-source)
(define-key map (kbd "<M-right>") 'helm-next-source))
(delq nil map))
"Keymap for `helm-read-file-name'.")
;; Internal.
(defvar helm-find-files-doc-header " (\\<helm-find-files-map>\\[helm-find-files-up-one-level]: Go up one level)"
"*The doc that is inserted in the Name header of a find-files or dired source.")
(defvar helm-ff-auto-update-flag nil
"Internal, flag to turn on/off auto-update in `helm-find-files'.
Don't set it directly, use instead `helm-ff-auto-update-initial-value'.")
(defvar helm-ff-last-expanded nil
"Store last expanded directory or file.")
(defvar helm-ff-default-directory nil)
(defvar helm-ff-history nil)
(defvar helm-ff-cand-to-mark nil)
(defvar helm-ff-url-regexp
"\\`\\(news\\(post\\)?:\\|nntp:\\|mailto:\\|file:\\|\\(ftp\\|https?\\|telnet\\|gopher\\|www\\|wais\\):/?/?\\).*"
"Same as `ffap-url-regexp' but match earlier possible url.")
(defvar helm-tramp-file-name-regexp "\\`/\\([^[/:]+\\|[^/]+]\\):")
(defvar helm-marked-buffer-name "*helm marked*")
(defvar helm-ff--auto-update-state nil)
(defvar helm-ff--deleting-char-backward nil)
(defvar helm-multi-files--toggle-locate nil)
(defvar helm-ff--move-to-first-real-candidate t)
(defvar helm-find-files--toggle-bookmark nil)
;;; Helm-find-files
;;
;;
(defcustom helm-find-files-actions
(helm-make-actions
"Find File" 'helm-find-file-or-marked
"Find file in Dired" 'helm-point-file-in-dired
(lambda () (and (locate-library "elscreen") "Find file in Elscreen"))
'helm-elscreen-find-file
"View file" 'view-file
"Checksum File" 'helm-ff-checksum
"Query replace fnames on marked" 'helm-ff-query-replace-on-marked
"Query replace contents on marked" 'helm-ff-query-replace
"Query replace regexp contents on marked" 'helm-ff-query-replace-regexp
"Serial rename files" 'helm-ff-serial-rename
"Serial rename by symlinking files" 'helm-ff-serial-rename-by-symlink
"Serial rename by copying files" 'helm-ff-serial-rename-by-copying
"Open file with default tool" 'helm-open-file-with-default-tool
"Find file in hex dump" 'hexl-find-file
"Browse project" 'helm-ff-browse-project
"Complete at point `C-c i'" 'helm-insert-file-name-completion-at-point
"Insert as org link `C-c @'" 'helm-files-insert-as-org-link
"Find shell command `C-c /'" 'helm-ff-find-sh-command
"Add marked files to file-cache" 'helm-ff-cache-add-file
"Open file externally `C-c C-x, C-u to choose'" 'helm-open-file-externally
"Grep File(s) `C-s, C-u Recurse'" 'helm-find-files-grep
"Grep current directory with AG" 'helm-find-files-ag
"Zgrep File(s) `M-g z, C-u Recurse'" 'helm-ff-zgrep
"Gid" 'helm-ff-gid
"Switch to Eshell `M-e'" 'helm-ff-switch-to-eshell
"Etags `M-., C-u reload tag file'" 'helm-ff-etags-select
"Eshell command on file(s) `M-!, C-u take all marked as arguments.'"
'helm-find-files-eshell-command-on-file
"Find file as root `C-c r'" 'helm-find-file-as-root
"Ediff File `C-='" 'helm-find-files-ediff-files
"Ediff Merge File `C-c ='" 'helm-find-files-ediff-merge-files
"Delete File(s) `M-D'" 'helm-delete-marked-files
"Copy file(s) `M-C, C-u to follow'" 'helm-find-files-copy
"Rename file(s) `M-R, C-u to follow'" 'helm-find-files-rename
"Symlink files(s) `M-S, C-u to follow'" 'helm-find-files-symlink
"Relsymlink file(s) `C-u to follow'" 'helm-find-files-relsymlink
"Hardlink file(s) `M-H, C-u to follow'" 'helm-find-files-hardlink
"Find file other window `C-c o'" 'find-file-other-window
"Switch to history `M-p'" 'helm-find-files-switch-to-hist
"Find file other frame `C-c C-o'" 'find-file-other-frame
"Print File `C-c p, C-u to refresh'" 'helm-ff-print
"Locate `C-x C-f, C-u to specify locate db'" 'helm-ff-locate)
"Actions for `helm-find-files'."
:group 'helm-files
:type '(alist :key-type string :value-type function))
(defvar helm-source-find-files nil
"The main source to browse files.
Should not be used among other sources.")
(defclass helm-source-ffiles (helm-source-sync)
((header-name
:initform (lambda (name)
(concat name (substitute-command-keys
helm-find-files-doc-header))))
(init
:initform (lambda ()
(setq helm-ff-auto-update-flag
helm-ff-auto-update-initial-value)
(setq helm-ff--auto-update-state
helm-ff-auto-update-flag)
(helm-set-local-variable 'bookmark-make-record-function
#'helm-ff-make-bookmark-record)))
(candidates :initform 'helm-find-files-get-candidates)
(filtered-candidate-transformer :initform 'helm-ff-sort-candidates)
(filter-one-by-one :initform 'helm-ff-filter-candidate-one-by-one)
(persistent-action :initform 'helm-find-files-persistent-action)
(persistent-help :initform "Hit1 Expand Candidate, Hit2 or (C-u) Find file")
(help-message :initform 'helm-ff-help-message)
(volatile :initform t)
(nohighlight :initform t)
(keymap :initform helm-find-files-map)
(candidate-number-limit :initform 'helm-ff-candidate-number-limit)
(action-transformer
:initform 'helm-find-files-action-transformer)
(action :initform 'helm-find-files-actions)
(before-init-hook :initform 'helm-find-files-before-init-hook)
(after-init-hook :initform 'helm-find-files-after-init-hook)))
;; Bookmark handlers.
;;
(defun helm-ff-make-bookmark-record ()
"The `bookmark-make-record-function' for `helm-find-files'."
(with-helm-buffer
`((filename . ,helm-ff-default-directory)
(presel . ,(helm-get-selection))
(handler . helm-ff-bookmark-jump))))
(defun helm-ff-bookmark-jump (bookmark)
"bookmark handler for `helm-find-files'."
(let ((fname (bookmark-prop-get bookmark 'filename))
(presel (bookmark-prop-get bookmark 'presel)))
(helm-find-files-1 fname (if helm-ff-transformer-show-only-basename
(helm-basename presel)
presel))))
(defun helm-ff-bookmark-set ()
"Record `helm-find-files' session in bookmarks."
(interactive)
(with-helm-buffer
(bookmark-set
(concat helm-find-files-bookmark-prefix
(abbreviate-file-name helm-ff-default-directory))))
(message "Helm find files session bookmarked! "))
(defun helm-dwim-target-directory ()
"Return value of `default-directory' of buffer in other window.
If there is only one window return the value ot `default-directory'
for current buffer."
(with-helm-current-buffer
(let ((num-windows (length (remove (get-buffer-window helm-marked-buffer-name)
(window-list)))))
(expand-file-name
(if (> num-windows 1)
(save-selected-window
(other-window 1)
default-directory)
;; Using the car of *ff-history allow
;; allow staying in the directory visited instead of current.
(or (car-safe helm-ff-history) default-directory))))))
(defun helm-find-files-do-action (action)
"Generic function for creating actions from `helm-source-find-files'.
ACTION must be an action supported by `helm-dired-action'."
(let* ((ifiles (mapcar 'expand-file-name ; Allow modify '/foo/.' -> '/foo'
(helm-marked-candidates :with-wildcard t)))
(cand (helm-get-selection)) ; Target
(prompt (format "%s %s file(s) to: "
(capitalize (symbol-name action))
(length ifiles)))
helm-ff--move-to-first-real-candidate
(helm-always-two-windows t)
(helm-reuse-last-window-split-state t)
(helm-split-window-default-side
(eq helm-split-window-default-side 'same))
helm-split-window-in-side-p
(parg helm-current-prefix-arg)
helm-display-source-at-screen-top ; prevent setting window-start.
helm-ff-auto-update-initial-value
(dest (with-helm-display-marked-candidates
helm-marked-buffer-name
(mapcar #'(lambda (f)
(if (file-directory-p f)
(concat (helm-basename f) "/")
(helm-basename f)))
ifiles)
(with-helm-current-buffer
(helm-read-file-name
prompt
:preselect (unless (cdr ifiles)
(if helm-ff-transformer-show-only-basename
(helm-basename cand) cand))
:initial-input (helm-dwim-target-directory)
:history (helm-find-files-history :comp-read nil))))))
(helm-dired-action
dest :files ifiles :action action :follow parg)))
(defun helm-find-files-copy (_candidate)
"Copy files from `helm-find-files'."
(helm-find-files-do-action 'copy))
(defun helm-find-files-rename (_candidate)
"Rename files from `helm-find-files'."
(helm-find-files-do-action 'rename))
(defun helm-find-files-symlink (_candidate)
"Symlink files from `helm-find-files'."
(helm-find-files-do-action 'symlink))
(defun helm-find-files-relsymlink (_candidate)
"Relsymlink files from `helm-find-files'."
(helm-find-files-do-action 'relsymlink))
(defun helm-find-files-hardlink (_candidate)
"Hardlink files from `helm-find-files'."
(helm-find-files-do-action 'hardlink))
(defun helm-find-files-byte-compile (_candidate)
"Byte compile elisp files from `helm-find-files'."
(let ((files (helm-marked-candidates :with-wildcard t))
(parg helm-current-prefix-arg))
(cl-loop for fname in files
do (byte-compile-file fname parg))))
(defun helm-find-files-load-files (_candidate)
"Load elisp files from `helm-find-files'."
(let ((files (helm-marked-candidates :with-wildcard t)))
(cl-loop for fname in files
do (load fname))))
(defun helm-find-files-ediff-files-1 (candidate &optional merge)
"Generic function to ediff/merge files in `helm-find-files'."
(let* ((bname (helm-basename candidate))
(marked (helm-marked-candidates :with-wildcard t))
(prompt (if merge "Ediff Merge `%s' With File: "
"Ediff `%s' With File: "))
(fun (if merge 'ediff-merge-files 'ediff-files))
(input (helm-dwim-target-directory))
(presel (if helm-ff-transformer-show-only-basename
(helm-basename candidate)
(expand-file-name
(helm-basename candidate)
input))))
(if (= (length marked) 2)
(funcall fun (car marked) (cadr marked))
(funcall fun candidate (helm-read-file-name
(format prompt bname)
:initial-input input
:preselect presel)))))
(defun helm-find-files-ediff-files (candidate)
(helm-find-files-ediff-files-1 candidate))
(defun helm-find-files-ediff-merge-files (candidate)
(helm-find-files-ediff-files-1 candidate 'merge))
(defun helm-find-files-grep (_candidate)
"Default action to grep files from `helm-find-files'."
(helm-do-grep-1 (helm-marked-candidates :with-wildcard t)
helm-current-prefix-arg))
(defun helm-find-files-ag (_candidate)
(helm-grep-ag-1 helm-ff-default-directory))
(defun helm-ff-zgrep (_candidate)
"Default action to zgrep files from `helm-find-files'."
(helm-ff-zgrep-1 (helm-marked-candidates :with-wildcard t) helm-current-prefix-arg))
(defun helm-ff-pdfgrep (_candidate)
"Default action to pdfgrep files from `helm-find-files'."
(let ((cands (cl-loop for file in (helm-marked-candidates :with-wildcard t)
if (or (string= (file-name-extension file) "pdf")
(string= (file-name-extension file) "PDF"))
collect file))
(helm-pdfgrep-default-function 'helm-pdfgrep-init))
(when cands
(helm-do-pdfgrep-1 cands))))
(defun helm-ff-etags-select (candidate)
"Default action to jump to etags from `helm-find-files'."
(when (get-buffer helm-action-buffer)
(kill-buffer helm-action-buffer))
(let* ((source-name (assoc-default 'name (helm-get-current-source)))
(default-directory (if (string= source-name "Find Files")
helm-ff-default-directory
(file-name-directory candidate))))
(helm-etags-select helm-current-prefix-arg)))
(defun helm-find-files-switch-to-hist (_candidate)
"Switch to helm-find-files history."
(helm-find-files t))
(defvar eshell-command-aliases-list nil)
(defvar helm-eshell-command-on-file-input-history nil)
(defun helm-find-files-eshell-command-on-file-1 (&optional map)
"Run `eshell-command' on CANDIDATE or marked candidates.
This is done possibly with an eshell alias, if no alias found, you can type in
an eshell command.
Basename of CANDIDATE can be a wild-card.
e.g you can do \"eshell-command command *.el\"
Where \"*.el\" is the CANDIDATE.
It is possible to do eshell-command command <CANDIDATE> <some more args>
like this: \"command %s some more args\".
If MAP is given run `eshell-command' on all marked files at once,
Otherwise, run `eshell-command' on each marked files.
In other terms, with a prefix arg do on the three marked files
\"foo\" \"bar\" \"baz\":
\"eshell-command command foo bar baz\"
otherwise do
\"eshell-command command foo\"
\"eshell-command command bar\"
\"eshell-command command baz\"
Note:
If `eshell' or `eshell-command' have not been run once,
or if you have no eshell aliases `eshell-command-aliases-list'
will not be loaded first time you use this."
(when (or eshell-command-aliases-list
(y-or-n-p "Eshell is not loaded, run eshell-command without alias anyway? "))
(and eshell-command-aliases-list (eshell-read-aliases-list))
(let* ((cand-list (helm-marked-candidates))
(default-directory (or helm-ff-default-directory
;; If candidate is an url *-ff-default-directory is nil
;; so keep value of default-directory.
default-directory))
(command (helm-comp-read
"Command: "
(cl-loop for (a . c) in eshell-command-aliases-list
when (string-match "\\(\\$1\\|\\$\\*\\)$" (car c))
collect (propertize a 'help-echo (car c)) into ls
finally return (sort ls 'string<))
:buffer "*helm eshell on file*"
:name "Eshell command"
:mode-line
'("Eshell alias"
"C-h m: Help, \\[universal-argument]: Insert output at point")
:help-message 'helm-esh-help-message
:input-history
'helm-eshell-command-on-file-input-history))
(alias-value (car (assoc-default command eshell-command-aliases-list)))
cmd-line)
(if (or (equal helm-current-prefix-arg '(16))
(equal map '(16)))
;; Two time C-u from `helm-comp-read' mean print to current-buffer.
;; i.e `eshell-command' will use this value.
(setq current-prefix-arg '(16))
;; Else reset the value of `current-prefix-arg'
;; to avoid printing in current-buffer.
(setq current-prefix-arg nil))
(if (and (or
;; One prefix-arg have been passed before `helm-comp-read'.
;; If map have been set with C-u C-u (value == '(16))
;; ignore it.
(and map (equal map '(4)))
;; One C-u from `helm-comp-read'.
(equal helm-current-prefix-arg '(4))
;; An alias that finish with $*
(and alias-value
;; If command is an alias be sure it accept
;; more than one arg i.e $*.
(string-match "\\$\\*$" alias-value)))
(cdr cand-list))
;; Run eshell-command with ALL marked files as arguments.
;; This wont work on remote files, because tramp handlers depends
;; on `default-directory' (limitation).
(let ((mapfiles (mapconcat 'eshell-quote-argument cand-list " ")))
(if (string-match "'%s'\\|\"%s\"\\|%s" command)
(setq cmd-line (format command mapfiles)) ; See [1]
(setq cmd-line (format "%s %s" command mapfiles)))
(helm-log "%S" cmd-line)
(eshell-command cmd-line))
;; Run eshell-command on EACH marked files.
;; To work with tramp handler we have to call
;; COMMAND on basename of each file, using
;; its basedir as `default-directory'.
(cl-loop for f in cand-list
for dir = (and (not (string-match ffap-url-regexp f))
(helm-basedir f))
for file = (eshell-quote-argument
(format "%s" (if (and dir (file-remote-p dir))
(helm-basename f) f)))
for com = (if (string-match "'%s'\\|\"%s\"\\|%s" command)
;; [1] This allow to enter other args AFTER filename
;; i.e <command %s some_more_args>
(format command file)
(format "%s %s" command file))
do (let ((default-directory (or dir default-directory)))
(eshell-command com)))))))
(defun helm-find-files-eshell-command-on-file (_candidate)
"Run `eshell-command' on CANDIDATE or marked candidates.
See `helm-find-files-eshell-command-on-file-1' for more info."
(helm-find-files-eshell-command-on-file-1 helm-current-prefix-arg))
(defun helm-ff-switch-to-eshell (_candidate)
"Switch to eshell and cd to `helm-ff-default-directory'."
(let ((cd-eshell #'(lambda ()
(eshell-kill-input)
(goto-char (point-max))
(insert
(format "cd '%s'" helm-ff-default-directory))
(eshell-send-input))))
(if (get-buffer "*eshell*")
(switch-to-buffer "*eshell*")
(call-interactively 'eshell))
(unless (get-buffer-process (current-buffer))
(funcall cd-eshell))))
(defun helm-ff-serial-rename-action (method)
"Rename all marked files in `helm-ff-default-directory' with METHOD.
See `helm-ff-serial-rename-1'."
(let* ((helm--reading-passwd-or-string t)
(cands (helm-marked-candidates :with-wildcard t))
(def-name (car cands))
(name (helm-read-string "NewName: "
(replace-regexp-in-string
"[0-9]+$" ""
(helm-basename
def-name
(file-name-extension def-name)))))
(start (read-number "StartAtNumber: "))
(extension (helm-read-string "Extension: "
(file-name-extension (car cands))))
(dir (expand-file-name
(helm-read-file-name
"Serial Rename to directory: "
:initial-input
(expand-file-name helm-ff-default-directory)
:test 'file-directory-p
:must-match t)))
done)
(with-helm-display-marked-candidates
helm-marked-buffer-name (mapcar 'helm-basename cands)
(if (y-or-n-p
(format "Rename %s file(s) to <%s> like this ?\n%s "
(length cands) dir (format "%s <-> %s%s.%s"
(helm-basename (car cands))
name start extension)))
(progn
(helm-ff-serial-rename-1
dir cands name start extension :method method)
(setq done t)
(message nil))))
(if done
(with-helm-current-buffer (helm-find-files-1 dir))
(message "Operation aborted"))))
(defun helm-ff-member-directory-p (file directory)
(let ((dir-file (expand-file-name
(file-name-as-directory (file-name-directory file))))
(cur-dir (expand-file-name (file-name-as-directory directory))))
(string= dir-file cur-dir)))
(cl-defun helm-ff-serial-rename-1
(directory collection new-name start-at-num extension &key (method 'rename))
"rename files in COLLECTION to DIRECTORY with the prefix name NEW-NAME.
Rename start at number START-AT-NUM - ex: prefixname-01.jpg.
EXTENSION is the file extension to use, in empty prompt,
reuse the original extension of file.
METHOD can be one of rename, copy or symlink.
Files will be renamed if they are files of current directory, otherwise they
will be treated with METHOD.
Default METHOD is rename."
;; Maybe remove directories selected by error in collection.
(setq collection (cl-remove-if 'file-directory-p collection))
(let* ((tmp-dir (file-name-as-directory
(concat (file-name-as-directory directory)
(symbol-name (cl-gensym "tmp")))))
(fn (cl-case method
(copy 'copy-file)
(symlink 'make-symbolic-link)
(rename 'rename-file)
(t (error "Error: Unknown method %s" method)))))
(make-directory tmp-dir)
(unwind-protect
(progn
;; Rename all files to tmp-dir with new-name.
;; If files are not from start directory, use method
;; to move files to tmp-dir.
(cl-loop for i in collection
for count from start-at-num
for fnum = (if (< count 10) "0%s" "%s")
for nname = (concat tmp-dir new-name (format fnum count)
(if (not (string= extension ""))
(format ".%s" (replace-regexp-in-string
"[.]" "" extension))
(file-name-extension i 'dot)))
do (if (helm-ff-member-directory-p i directory)
(rename-file i nname)
(funcall fn i nname)))
;; Now move all from tmp-dir to destination.
(cl-loop with dirlist = (directory-files
tmp-dir t directory-files-no-dot-files-regexp)
for f in dirlist do
(if (file-symlink-p f)
(make-symbolic-link (file-truename f)
(concat (file-name-as-directory directory)
(helm-basename f)))
(rename-file f directory))))
(delete-directory tmp-dir t))))
(defun helm-ff-serial-rename (_candidate)
"Serial rename all marked files to `helm-ff-default-directory'.
Rename only file of current directory, and symlink files coming from
other directories.
See `helm-ff-serial-rename-1'."
(helm-ff-serial-rename-action 'rename))
(defun helm-ff-serial-rename-by-symlink (_candidate)
"Serial rename all marked files to `helm-ff-default-directory'.
Rename only file of current directory, and symlink files coming from
other directories.
See `helm-ff-serial-rename-1'."
(helm-ff-serial-rename-action 'symlink))
(defun helm-ff-serial-rename-by-copying (_candidate)
"Serial rename all marked files to `helm-ff-default-directory'.
Rename only file of current directory, and copy files coming from
other directories.
See `helm-ff-serial-rename-1'."
(helm-ff-serial-rename-action 'copy))
(defun helm-ff-query-replace-on-marked-1 (candidates)
"Query replace on filenames of CANDIDATES.
This doesn't replace inside the files, only modify filenames."
(with-helm-display-marked-candidates
helm-marked-buffer-name
(mapcar 'helm-basename candidates)
(let* ((regexp (read-string "Replace regexp on filename(s): "))
(str (read-string (format "Replace regexp `%s' with: " regexp))))
(cl-loop with query = "y"
with count = 0
for old in candidates
for new = (concat (helm-basedir old)
(replace-regexp-in-string
regexp str
(helm-basename old)))
;; If `regexp' is not matched in `old'
;; `replace-regexp-in-string' will
;; return `old' unmodified.
unless (string= old new)
do (progn
(unless (string= query "!")
(while (not (member
(setq query
(string
(read-key
(propertize
(format
"Replace `%s' by `%s' [!,y,n,q]"
old new)
'face 'minibuffer-prompt))))
'("y" "!" "n" "q")))
(message "Please answer by y,n,! or q") (sit-for 1)))
(when (string= query "q")
(cl-return (message "Operation aborted")))
(unless (string= query "n")
(rename-file old new)
(cl-incf count)))
finally (message "%d Files renamed" count))))
;; This fix the emacs bug where "Emacs-Lisp:" is sent
;; in minibuffer (not the echo area).
(sit-for 0.1)
(with-current-buffer (window-buffer (minibuffer-window))
(delete-minibuffer-contents)))
;; The action.
(defun helm-ff-query-replace-on-marked (_candidate)
(let ((marked (helm-marked-candidates)))
(helm-ff-query-replace-on-marked-1 marked)))
;; The command for `helm-find-files-map'.
(defun helm-ff-run-query-replace-on-marked ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-ff-query-replace-on-marked)))
(defun helm-ff-query-replace (_candidate)
(let ((bufs (cl-loop for f in (helm-marked-candidates)
collect (buffer-name (find-file-noselect f)))))
(helm-buffer-query-replace-1 nil bufs)))
(defun helm-ff-query-replace-regexp (_candidate)
(let ((bufs (cl-loop for f in (helm-marked-candidates)
collect (buffer-name (find-file-noselect f)))))
(helm-buffer-query-replace-1 'regexp bufs)))
(defun helm-ff-run-query-replace ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-ff-query-replace)))
(defun helm-ff-run-query-replace-regexp ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'helm-ff-query-replace-regexp)))
(defun helm-ff-toggle-auto-update (_candidate)
(setq helm-ff-auto-update-flag (not helm-ff-auto-update-flag))
(setq helm-ff--auto-update-state helm-ff-auto-update-flag)
(message "[Auto expansion %s]"
(if helm-ff-auto-update-flag "enabled" "disabled")))
(defun helm-ff-run-toggle-auto-update ()
(interactive)
(with-helm-alive-p
(helm-attrset 'toggle-auto-update '(helm-ff-toggle-auto-update . never-split))
(helm-execute-persistent-action 'toggle-auto-update)))
(defun helm-ff-delete-char-backward ()