-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathhelm-projectile.el
More file actions
1818 lines (1631 loc) · 87.7 KB
/
helm-projectile.el
File metadata and controls
1818 lines (1631 loc) · 87.7 KB
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-projectile.el --- Helm integration for Projectile -*- lexical-binding: t; -*-
;; Copyright (C) 2011-2025 Bozhidar Batsov
;; Author: Bozhidar Batsov
;; URL: https://github.com/bbatsov/helm-projectile
;; Maintainer: Przemysław Kryger
;; Created: 2011-31-07
;; Keywords: project, convenience
;; Version: 1.4.0
;; Package-Requires: ((emacs "26.1") (helm "3.0") (projectile "2.9"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; This library provides easy project management and navigation. The
;; concept of a project is pretty basic - just a folder containing
;; special file. Currently git, mercurial and bazaar repos are
;; considered projects by default. If you want to mark a folder
;; manually as a project just create an empty .projectile file in
;; it. See the README for more details.
;;
;;; Code:
;; built-in libraries
(require 'subr-x)
(require 'cl-lib)
(require 'grep) ;; TODO: Probably we should defer this require
(require 'helm-core)
(require 'helm-global-bindings)
(require 'helm-types)
(require 'helm-locate)
(require 'helm-buffers)
(require 'helm-files)
(require 'helm-grep)
(require 'projectile)
(declare-function eshell "eshell")
(declare-function dired-get-filename "dired")
(defvar grep-find-ignored-directories)
(defvar grep-find-ignored-files)
(defgroup helm-projectile nil
"Helm support for projectile."
:prefix "helm-projectile-"
:group 'projectile
:group 'helm
:link `(url-link :tag "GitHub" "https://github.com/bbatsov/helm-projectile"))
(defvar helm-projectile-current-project-root)
(defcustom helm-projectile-truncate-lines nil
"Truncate lines in helm projectile commands when non--nil.
Some `helm-projectile' commands have similar behavior with existing
Helms. In these cases their respective custom var for truncation
of lines will be honored. E.g. `helm-buffers-truncate-lines'
dictates the truncation in `helm-projectile-switch-to-buffer'."
:group 'helm-projectile
:type 'boolean)
(defcustom helm-projectile-remove-current-buffer nil
"Non-nil if current buffer should be removed from buffer sources.
For example from `helm-projectile' (when invoked from a project) as well
as from `helm-projectile-switch-to-buffer',
`helm-projectile-switch-to-buffer-other-window', and
`helm-projectile-switch-to-buffer-other-frame'."
:type 'boolean
:group 'helm-projectile)
;;;###autoload
(defcustom helm-projectile-fuzzy-match t
"Enable fuzzy matching for Helm Projectile commands.
This needs to be set before loading helm-projectile.el."
:group 'helm-projectile
:type 'boolean)
(defmacro helm-projectile-define-key (keymap &rest bindings)
"In KEYMAP, define BINDINGS.
BINDINS is a list in a form of (KEY1 DEF1 KEY2 DEF2 ...)."
(declare (indent defun))
(when (or (< (length bindings) 2)
(= 1 (% 2 (length bindings))))
(error "Expected BINDINGS to be KEY1 DEF1 KEY2 DEF2 ... "))
(let ((ret '(progn)))
(while-let ((key (car bindings))
(def (cadr bindings)))
(push
`(define-key ,keymap ,key
(lambda ()
(interactive)
(helm-exit-and-execute-action ,def)))
ret)
(setq bindings (cddr bindings)))
(reverse ret)))
(defun helm-projectile-hack-actions (actions &rest prescription)
"Given a Helm action list and a prescription, return a hacked Helm action list.
Optionally applies the PRESCRIPTION beforehand.
The Helm action list ACTIONS is of the form:
\(\(DESCRIPTION1 . FUNCTION1\)
\(DESCRIPTION2 . FUNCTION2\)
...
\(DESCRIPTIONn . FUNCTIONn\)\)
PRESCRIPTION is in the form:
\(INSTRUCTION1 INSTRUCTION2 ... INSTRUCTIONn\)
If an INSTRUCTION is a symbol, the action with function name
INSTRUCTION is deleted.
If an INSTRUCTION is of the form \(FUNCTION1 . FUNCTION2\), the
action with function name FUNCTION1 will change it's function to
FUNCTION2.
If an INSTRUCTION is of the form \(FUNCTION . DESCRIPTION\), and
if an action with function name FUNCTION exists in the original
Helm action list, the action in the Helm action list, with
function name FUNCTION will change it's description to
DESCRIPTION. Otherwise, (FUNCTION . DESCRIPTION) will be added to
the action list.
If an INSTRUCTION is of the form \(FUNCTION . :make-first\), and if the
an action with function name FUNCTION exists in the th Helm action list
concatenated with new actions from PRESCRIPTION, the action will become
the first (default) action.
Please check out how `helm-projectile-file-actions' is defined
for an example of how this function is being used."
(let* ((to-delete (cl-remove-if (lambda (entry) (listp entry)) prescription))
(actions (cl-delete-if (lambda (action) (memq (cdr action) to-delete))
(copy-alist actions)))
new)
(cl-dolist (action actions)
(when (setq new (cdr (assq (cdr action) prescription)))
(cond
((stringp new) (setcar action new))
((functionp new) (setcdr action new)))))
;; Add new actions from PRESCRIPTION
(setq new nil)
(cl-dolist (instruction prescription)
(when (and (listp instruction)
(null (rassq (car instruction) actions))
(symbolp (car instruction)) (stringp (cdr instruction)))
(push (cons (cdr instruction) (car instruction)) new)))
;; Push to front the desired action
(let ((actions (append actions (nreverse new))))
(if-let* ((first-function (car (rassq :make-first prescription)))
(first-action-p (lambda (action)
(eq (cdr action)
first-function)))
(first-action (cl-find-if first-action-p actions)))
(cons first-action
(cl-remove-if first-action-p actions))
actions))))
(defun helm-projectile-vc (dir)
"A Helm action for jumping to project root using `vc-dir' or Magit.
DIR is a directory to be switched"
(let ((projectile-require-project-root nil))
(projectile-vc dir)))
(defun helm-projectile-compile-project (dir)
"A Helm action for compile a project.
DIR is the project root."
(let ((helm--reading-passwd-or-string t)
(default-directory dir))
(projectile-compile-project helm-current-prefix-arg)))
(defun helm-projectile-test-project (dir)
"A Helm action for test a project.
DIR is the project root."
(let ((helm--reading-passwd-or-string t)
(default-directory dir))
(projectile-test-project helm-current-prefix-arg)))
(defun helm-projectile-run-project (dir)
"A Helm action for run a project.
DIR is the project root."
(let ((helm--reading-passwd-or-string t)
(default-directory dir))
(projectile-run-project helm-current-prefix-arg)))
(defun helm-projectile-remove-known-project (_ignore)
"Remove selected projects from projectile project list.
_IGNORE means the argument does not matter.
It is there because Helm requires it."
(let* ((projects (helm-marked-candidates :with-wildcard t))
(len (length projects)))
(with-helm-display-marked-candidates
helm-marked-buffer-name
projects
(if (not (y-or-n-p (format "Remove *%s projects(s)? " len)))
(message "(No removal performed)")
(progn
(mapc (lambda (p)
(setq projectile-known-projects (delete p projectile-known-projects)))
projects)
(projectile-save-known-projects))
(message "%s projects(s) removed" len)))))
(defun helm-projectile-switch-project-by-name (project)
"Switch to PROJECT and execute `projectile-switch-project-action' in it.
When `projectile-switch-project-action' is `projectile-find-file' a
`helm-projectile-find-file' will be used instead."
(let ((projectile-completion-system 'helm)
(projectile-switch-project-action
(if (eq projectile-switch-project-action 'projectile-find-file)
#'helm-projectile-find-file
projectile-switch-project-action)))
(projectile-switch-project-by-name project)))
(defun helm-projectile-switch-project-by-name-other-window (project)
"Switch to PROJECT and find file in it in other window."
(let ((projectile-completion-system 'helm)
(projectile-switch-project-action #'helm-projectile-find-file-other-window))
(projectile-switch-project-by-name project)))
(defun helm-projectile-switch-project-by-name-other-frame (project)
"Switch to PROJECT and find file in it in other frame."
(let ((projectile-completion-system 'helm)
(projectile-switch-project-action #'helm-projectile-find-file-other-frame))
(projectile-switch-project-by-name project)))
(defvar helm-projectile-projects-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(helm-projectile-define-key map
(kbd "C-d") #'dired
(kbd "C-c o") #'helm-projectile-switch-project-by-name-other-window
(kbd "C-c C-o") #'helm-projectile-switch-project-by-name-other-frame
(kbd "M-g") #'helm-projectile-vc
(kbd "M-e") #'helm-projectile-switch-to-shell
(kbd "C-s") #'helm-projectile-grep
(kbd "M-c") #'helm-projectile-compile-project
(kbd "M-t") #'helm-projectile-test-project
(kbd "M-r") #'helm-projectile-run-project
(kbd "M-D") #'helm-projectile-remove-known-project
(kbd "C-S-a") #'helm-projectile--switch-project-and-ag-action
(kbd "C-S-r") #'helm-projectile--switch-project-and-rg-action)
map)
"Mapping for known projectile projects.")
(defcustom helm-source-projectile-projects-actions
(helm-make-actions
"Switch to project" #'helm-projectile-switch-project-by-name
"Switch to project other window `C-c o'" #'helm-projectile-switch-project-by-name-other-window
"Switch to project other frame `C-c C-o'" #'helm-projectile-switch-project-by-name-other-frame
"Open Dired in project's directory `C-d'" #'dired
"Open project root in vc-dir or magit `M-g'" #'helm-projectile-vc
"Switch to Eshell `M-e'" #'helm-projectile-switch-to-shell
"Grep in projects `C-s'" #'helm-projectile-grep
"Compile project `M-c'. With C-u, new compile command" #'helm-projectile-compile-project
"Remove project(s) from project list `M-D'" #'helm-projectile-remove-known-project
"Silver searcher (ag) in project `C-S-a'" #'helm-projectile--switch-project-and-ag-action
"Ripgrep (rg) in project `C-S-r'" #'helm-projectile--switch-project-and-rg-action)
"Actions for `helm-source-projectile-projects'."
:group 'helm-projectile
:type '(alist :key-type string :value-type function))
(defclass helm-projectile-projects-source (helm-source-sync helm-type-file)
((candidates :initform (lambda () (with-helm-current-buffer
(mapcar #'copy-sequence
(projectile-known-projects)))))
(fuzzy-match :initform 'helm-projectile-fuzzy-match)
(keymap :initform 'helm-projectile-projects-map)
(mode-line :initform 'helm-read-file-name-mode-line-string)
(action :initform 'helm-source-projectile-projects-actions))
"Helm source for known projectile projects.")
(cl-defmethod helm-setup-user-source ((source helm-projectile-projects-source))
"Make SOURCE specific to project switching.
The `helm-projectile-projects-source` inherits from
`helm-type-file` (which see), which sets up actions, keymap, and
help message slots to file specific ones. Override these slots
to be specific to `helm-projectile-projects-source'."
(setf (slot-value source 'action) 'helm-source-projectile-projects-actions)
(setf (slot-value source 'keymap) helm-projectile-projects-map)
;; Use `ignore' as a persistent action, to actually keep `helm' session
;; when `helm-execute-persistent-action' is executed.
(setf (slot-value source 'persistent-action) #'ignore)
(let ((persistent-help "Do Nothing"))
(setf (slot-value source 'persistent-help) persistent-help)
(setf (slot-value source 'header-line)
(helm-source--persistent-help-string
persistent-help
source)))
(setf (slot-value source 'mode-line)
(list "Project(s)" helm-mode-line-string)))
(defvar helm-source-projectile-projects
(helm-make-source "Projectile projects" 'helm-projectile-projects-source))
(defclass helm-projectile-projects-other-window-source (helm-projectile-projects-source)
())
(cl-defmethod helm-setup-user-source :after ((source helm-projectile-projects-other-window-source))
"Set `helm-projectile-switch-project-by-name-other-window' as the first action."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-source-projectile-projects-actions
'(helm-projectile-switch-project-by-name-other-window . :make-first))))
(defclass helm-projectile-projects-other-frame-source (helm-projectile-projects-source)
())
(cl-defmethod helm-setup-user-source :after ((source helm-projectile-projects-other-frame-source))
"Set `helm-projectile-switch-project-by-name-other-frame' as the first action."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-source-projectile-projects-actions
'(helm-projectile-switch-project-by-name-other-frame . :make-first))))
(defvar helm-projectile-dirty-projects-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(helm-projectile-define-key map
(kbd "C-d") #'dired
(kbd "M-o") #'helm-projectile-switch-project-by-name
(kbd "C-c o") #'helm-projectile-switch-project-by-name-other-window
(kbd "C-c C-o") #'helm-projectile-switch-project-by-name-other-frame
(kbd "M-e") #'helm-projectile-switch-to-shell
(kbd "C-s") #'helm-projectile-grep
(kbd "M-c") #'helm-projectile-compile-project
(kbd "M-t") #'helm-projectile-test-project
(kbd "M-r") #'helm-projectile-run-project
(kbd "M-D") #'helm-projectile-remove-known-project
(kbd "C-S-a") #'helm-projectile--switch-project-and-ag-action
(kbd "C-S-r") #'helm-projectile--switch-project-and-rg-action)
map)
"Mapping for dirty projectile projects.")
(defvar helm-source-projectile-dirty-projects
(helm-build-sync-source "Projectile dirty projects"
:candidates (lambda () (with-helm-current-buffer (helm-projectile-get-dirty-projects)))
:fuzzy-match helm-projectile-fuzzy-match
:keymap helm-projectile-dirty-projects-map
:mode-line helm-read-file-name-mode-line-string
:action '(("Open project root in vc-dir or magit" . helm-projectile-vc)
("Switch to project `M-o'" . helm-projectile-switch-project-by-name)
("Switch to project other window `C-c o'" . helm-projectile-switch-project-by-name-other-window)
("Switch to project other frame `C-c C-o'" . helm-projectile-switch-project-by-name-other-frame)
("Open Dired in project's directory `C-d'" . dired)
("Switch to Eshell `M-e'" . helm-projectile-switch-to-shell)
("Grep in projects `C-s'" . helm-projectile-grep)
("Compile project `M-c'. With C-u, new compile command"
. helm-projectile-compile-project)
("Silver searcher (ag) in project `C-S-a'" . helm-projectile--switch-project-and-ag-action)
("Ripgrep (rg) in project `C-S-r'" . helm-projectile--switch-project-and-rg-action)))
"Helm source for dirty version controlled projectile projects.")
(defun helm-projectile-get-dirty-projects ()
"Return dirty version controlled known projects.
The value is returned as an alist to have a nice display in Helm."
(message "Checking for dirty known projects...")
(let* ((status (projectile-check-vcs-status-of-known-projects))
(proj-dir (cl-loop for stat in status
collect (car stat)))
(status-display (cl-loop for stat in status collect
(propertize (format "[%s]"
(mapconcat 'identity
(car (cdr stat)) ", "))
'face 'helm-match)))
(max-status-display-length (cl-loop for sd in status-display
maximize (length sd)))
(status-display (cl-loop for sd in status-display collect
(format "%s%s "
sd
(make-string
(- max-status-display-length (length sd)) ? ))))
(full-display (cl-mapcar 'concat
status-display
(mapcar (lambda (dir)
(propertize dir 'face 'helm-ff-directory))
proj-dir))))
(cl-pairlis full-display proj-dir)))
(define-key helm-etags-map (kbd "C-c p f")
(lambda ()
(interactive)
(helm-run-after-exit 'helm-projectile-find-file nil)))
(defun helm-projectile-file-persistent-action (candidate)
"Previews the contents of a file CANDIDATE in a temporary buffer.
This is a persistent action for file-related functionality."
(let ((buf (get-buffer-create " *helm-projectile persistent*")))
(cl-flet ((preview (candidate)
(switch-to-buffer buf)
(setq inhibit-read-only t)
(erase-buffer)
(insert-file-contents candidate)
(let ((buffer-file-name candidate))
(set-auto-mode))
(font-lock-ensure)
(setq inhibit-read-only nil)))
(if (and (helm-get-attr 'previewp)
(string= candidate (helm-get-attr 'current-candidate)))
(progn
(kill-buffer buf)
(helm-set-attr 'previewp nil))
(preview candidate)
(helm-set-attr 'previewp t)))
(helm-set-attr 'current-candidate candidate)))
(defun helm-projectile-find-files-eshell-command-on-file-action (candidate)
"Execute an eshell command on a file CANDIDATE."
(interactive)
(let* ((helm-ff-default-directory (file-name-directory candidate)))
(helm-find-files-eshell-command-on-file candidate)))
(defun helm-projectile-ff-etags-select-action (candidate)
"Jump to etags for file CANDIDATE.
See also `helm-etags-select'."
(interactive)
(let* ((helm-ff-default-directory (file-name-directory candidate)))
(helm-ff-etags-select candidate)))
(defun helm-projectile-switch-to-shell (dir)
"Within DIR, switch to a shell corresponding to `helm-ff-preferred-shell-mode'."
(interactive)
(let* ((projectile-require-project-root nil)
(helm-ff-default-directory (file-name-directory (projectile-expand-root dir))))
(helm-ff-switch-to-shell dir)))
(defun helm-projectile-files-in-current-dired-buffer ()
"Return a list of files (only) in the current Dired buffer."
(let (flist)
(cl-flet ((fpush (fname) (push fname flist)))
(save-excursion
(let (file buffer-read-only)
(goto-char (point-min))
(while (not (eobp))
(save-excursion
(and (not (eolp))
(setq file (dired-get-filename t t)) ; nil on non-file
(progn (end-of-line)
(funcall #'fpush file))))
(forward-line 1)))))
(mapcar 'file-truename (nreverse flist))))
(defun helm-projectile-all-dired-buffers ()
"Get all current Dired buffers."
(mapcar (lambda (b)
(with-current-buffer b (buffer-name)))
(cl-remove-if-not
(lambda (b)
(with-current-buffer b
(and (eq major-mode 'dired-mode)
(buffer-name))))
(buffer-list))))
(defvar helm-projectile-virtual-dired-remote-enable nil
"Enable virtual Dired manager on remote host.
Disabled by default.")
(defun helm-projectile-dired-files-new-action (candidate)
"Create a Dired buffer from chosen files.
CANDIDATE is the selected file, but choose the marked files if available."
(if (and (file-remote-p (projectile-project-root))
(not helm-projectile-virtual-dired-remote-enable))
(message "Virtual Dired manager is disabled in remote host. Enable with %s."
(propertize "helm-projectile-virtual-dired-remote-enable" 'face 'font-lock-keyword-face))
(let ((files (cl-remove-if-not
(lambda (f)
(not (string= f "")))
(mapcar (lambda (file)
(replace-regexp-in-string (projectile-project-root) "" file))
(helm-marked-candidates :with-wildcard t))))
(new-name (completing-read "Select or enter a new buffer name: "
(helm-projectile-all-dired-buffers)))
(helm--reading-passwd-or-string t)
(default-directory (projectile-project-root)))
;; create a unique buffer that is unique to any directory in default-directory
;; or opened buffer; when Dired is passed with a non-existence directory name,
;; it only creates a buffer and insert everything. If a new name user supplied
;; exists as default-directory, Dired throws error when insert anything that
;; does not exist in current directory.
(with-current-buffer (dired (cons (make-temp-name new-name)
(if files
files
(list candidate))))
(when (get-buffer new-name)
(kill-buffer new-name))
(rename-buffer new-name)))))
(defun helm-projectile-dired-files-add-action (candidate)
"Add files to a Dired buffer.
CANDIDATE is the selected file. Used when no file is explicitly marked."
(if (and (file-remote-p (projectile-project-root))
(not helm-projectile-virtual-dired-remote-enable))
(message "Virtual Dired manager is disabled in remote host. Enable with %s."
(propertize "helm-projectile-virtual-dired-remote-enable" 'face 'font-lock-keyword-face))
(if (eq (with-helm-current-buffer major-mode) 'dired-mode)
(let* ((marked-files (helm-marked-candidates :with-wildcard t))
(helm--reading-passwd-or-string t)
(root (projectile-project-root)) ; store root for later use
(dired-buffer-name (or (and (eq major-mode 'dired-mode) (buffer-name))
(completing-read "Select a Dired buffer:"
(helm-projectile-all-dired-buffers))))
(dired-files (with-current-buffer dired-buffer-name
(helm-projectile-files-in-current-dired-buffer)))
(files (sort (mapcar (lambda (file)
(replace-regexp-in-string (projectile-project-root) "" file))
(cl-nunion (if marked-files
marked-files
(list candidate))
dired-files
:test #'string-equal))
'string-lessp)))
(kill-buffer dired-buffer-name)
;; Rebind default-directory because after killing a buffer, we
;; could be in any buffer and default-directory is set to that
;; random buffer
;;
;; Also use saved root directory, because after killing a buffer,
;; we could be outside of current project
(let ((default-directory root))
(with-current-buffer (dired (cons (make-temp-name dired-buffer-name)
(if files
(mapcar (lambda (file)
(replace-regexp-in-string root "" file))
files)
(list candidate))))
(rename-buffer dired-buffer-name))))
(error "You're not in a Dired buffer to add"))))
(defun helm-projectile-dired-files-delete-action (candidate)
"Delete selected entries from a Dired buffer.
CANDIDATE is the selected file. Used when no file is explicitly marked."
(if (and (file-remote-p (projectile-project-root))
(not helm-projectile-virtual-dired-remote-enable))
(message "Virtual Dired manager is disabled in remote host. Enable with %s."
(propertize "helm-projectile-virtual-dired-remote-enable" 'face 'font-lock-keyword-face))
(let* ((helm--reading-passwd-or-string t)
(root (projectile-project-root))
(dired-buffer-name (with-helm-current-buffer (buffer-name)))
(dired-files (with-current-buffer dired-buffer-name
(helm-projectile-files-in-current-dired-buffer)))
(files (sort (cl-set-exclusive-or (helm-marked-candidates :with-wildcard t)
dired-files
:test #'string-equal) #'string-lessp)))
(kill-buffer dired-buffer-name)
;; similar reason to `helm-projectile-dired-files-add-action'
(let ((default-directory root))
(with-current-buffer (dired (cons (make-temp-name dired-buffer-name)
(if files
(mapcar (lambda (file)
(replace-regexp-in-string root "" file))
files)
(list candidate))))
(rename-buffer dired-buffer-name))))))
(defun helm-projectile-run-projectile-hooks-after-find-file (_orig-fun &rest _args)
"Run `projectile-find-file-hook' if using projectile."
(when (and projectile-mode (projectile-project-p))
(run-hooks 'projectile-find-file-hook)))
(advice-add 'helm-find-file-or-marked
:after #'helm-projectile-run-projectile-hooks-after-find-file)
(defvar helm-projectile-find-file-map
(let ((map (copy-keymap helm-find-files-map)))
(helm-projectile-define-key map
(kbd "C-c f") #'helm-projectile-dired-files-new-action
(kbd "C-c a") #'helm-projectile-dired-files-add-action
(kbd "M-e") #'helm-projectile-switch-to-shell
(kbd "M-.") #'helm-projectile-ff-etags-select-action
(kbd "M-!") #'helm-projectile-find-files-eshell-command-on-file-action)
(define-key map (kbd "<left>") #'helm-previous-source)
(define-key map (kbd "<right>") #'helm-next-source)
(dolist (cmd '(helm-find-files-up-one-level
helm-find-files-down-last-level))
(substitute-key-definition cmd nil map))
map)
"Mapping for file commands in Helm Projectile.")
(defvar helm-projectile-file-actions
(helm-projectile-hack-actions
helm-find-files-actions
;; Delete these actions
'helm-ff-browse-project
'helm-insert-file-name-completion-at-point
'helm-ff-find-sh-command
'helm-ff-cache-add-file
;; Substitute these actions
'(helm-ff-switch-to-shell . helm-projectile-switch-to-shell)
'(helm-ff-etags-select . helm-projectile-ff-etags-select-action)
'(helm-find-files-eshell-command-on-file
. helm-projectile-find-files-eshell-command-on-file-action)
;; Change action descriptions
'(helm-find-file-as-root . "Find file as root `C-c r'")
;; New actions
'(helm-projectile-dired-files-new-action
. "Create Dired buffer from files `C-c f'")
'(helm-projectile-dired-files-add-action
. "Add files to Dired buffer `C-c a'"))
"Action for files.")
(defun helm-projectile--move-selection-p (selection)
"Return non-nil if should move Helm selector after SELECTION.
SELECTION should be moved unless it's one of:
- Non-string
- Existing file
- Non-remote file that matches `helm-tramp-file-name-regexp'"
(not (or (not (stringp selection))
(file-exists-p selection)
(and (string-match helm-tramp-file-name-regexp selection)
(not (file-remote-p selection nil t))))))
(defun helm-projectile--move-to-real ()
"Move to first real candidate.
Similar to `helm-ff--move-to-first-real-candidate', but without
unnecessary complexity."
(while (let* ((src (helm-get-current-source))
(selection (and (not (helm-empty-source-p))
(helm-get-selection nil nil src))))
(and (not (helm-end-of-source-p))
(helm-projectile--move-selection-p selection)))
(helm-next-line)))
(defun helm-projectile--remove-move-to-real ()
"Hook function to remove `helm-projectile--move-to-real'.
Meant to be added to `helm-cleanup-hook', from which it removes
itself at the end."
(remove-hook 'helm-after-update-hook #'helm-projectile--move-to-real)
(remove-hook 'helm-cleanup-hook #'helm-projectile--remove-move-to-real))
(defvar helm-source-projectile-files-list-before-init-hook
(lambda ()
(add-hook 'helm-after-update-hook #'helm-projectile--move-to-real)
(add-hook 'helm-cleanup-hook #'helm-projectile--remove-move-to-real)))
(defclass helm-source-projectile-file (helm-source-sync)
((before-init-hook :initform 'helm-source-projectile-files-list-before-init-hook)
(candidates
:initform (lambda ()
(when (projectile-project-p)
(with-temp-buffer
(hack-dir-local-variables-non-file-buffer)
(helm-projectile--files-display-real (projectile-current-project-files)
(projectile-project-root))))))
(filtered-candidate-transformer
:initform (lambda (files _source)
(with-temp-buffer
(hack-dir-local-variables-non-file-buffer)
(let* ((root (projectile-project-root))
(file-at-root (file-relative-name (expand-file-name helm-pattern root))))
(if (or (string-empty-p helm-pattern)
(assoc helm-pattern files))
files
(if (equal helm-pattern file-at-root)
(cl-acons (helm-ff-prefix-filename helm-pattern nil t)
(expand-file-name helm-pattern)
files)
(cl-pairlis (list (helm-ff-prefix-filename helm-pattern nil t)
(helm-ff-prefix-filename file-at-root nil t))
(list (expand-file-name helm-pattern)
(expand-file-name helm-pattern root))
files)))))))
(fuzzy-match :initform 'helm-projectile-fuzzy-match)
(keymap :initform 'helm-projectile-find-file-map)
(help-message :initform 'helm-ff-help-message)
(mode-line :initform 'helm-read-file-name-mode-line-string)
(action :initform 'helm-projectile-file-actions)
(persistent-action :initform #'helm-projectile-file-persistent-action)
(persistent-help :initform "Preview file")))
(defvar helm-source-projectile-files-list
(helm-make-source "Projectile files" 'helm-source-projectile-file)
"Helm source definition for Projectile files.")
(defclass helm-source-projectile-file-other-window (helm-source-projectile-file)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-file-other-window))
"Make `helm-find-files-other-window' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-projectile-file-actions
'(helm-find-files-other-window . :make-first))))
(defvar helm-source-projectile-files-other-window-list
(helm-make-source "Projectile files" 'helm-source-projectile-file-other-window)
"Helm source definition for Projectile files.")
(defclass helm-source-projectile-file-other-frame (helm-source-projectile-file)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-file-other-frame))
"Make `find-file-other-frame' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-projectile-file-actions
'(find-file-other-frame . :make-first))))
(defvar helm-source-projectile-files-other-frame-list
(helm-make-source "Projectile files" 'helm-source-projectile-file-other-frame)
"Helm source definition for Projectile files.")
(defvar helm-source-projectile-files-in-all-projects-list
(helm-build-sync-source "Projectile files in all Projects"
:candidates (lambda ()
(with-helm-current-buffer
(let ((projectile-require-project-root nil))
(projectile-all-project-files))))
:keymap helm-projectile-find-file-map
:help-message 'helm-ff-help-message
:mode-line helm-read-file-name-mode-line-string
:action helm-projectile-file-actions
:persistent-action #'helm-projectile-file-persistent-action
:persistent-help "Preview file")
"Helm source definition for all Projectile files in all projects.")
(defvar helm-projectile-dired-file-actions
(helm-projectile-hack-actions
helm-projectile-file-actions
;; New actions
'(helm-projectile-dired-files-delete-action . "Remove entry(s) from Dired buffer `C-c d'")))
(defclass helm-source-projectile-dired-file (helm-source-in-buffer)
((data :initform (lambda ()
(if (and (file-remote-p (projectile-project-root))
(not helm-projectile-virtual-dired-remote-enable))
nil
(when (eq major-mode 'dired-mode)
(helm-projectile-files-in-current-dired-buffer)))))
(filter-one-by-one :initform (lambda (file)
(let ((helm-ff-transformer-show-only-basename t))
(helm-ff-filter-candidate-one-by-one file))))
(action-transformer :initform (lambda (actions candidate)
(let ((actions (helm-find-files-action-transformer actions candidate)))
(if (file-directory-p candidate)
(append
actions
'(("Silver searcher (ag) in directory `C-S-a'" . helm-projectile--switch-project-and-ag-action)
("Ripgrep (rg) in directory `C-S-r'" . helm-projectile--switch-project-and-rg-action)))
actions))))
(keymap :initform (let ((map (copy-keymap helm-projectile-find-file-map)))
(helm-projectile-define-key map
(kbd "C-c d") #'helm-projectile-dired-files-delete-action
(kbd "C-S-a") #'helm-projectile--switch-project-and-ag-action
(kbd "C-S-r") #'helm-projectile--switch-project-and-rg-action)
map))
(help-message :initform 'helm-ff-help-message)
(mode-line :initform 'helm-read-file-name-mode-line-string)
(action :initform 'helm-projectile-dired-file-actions)))
(defvar helm-source-projectile-dired-files-list
(helm-make-source "Projectile files in current Dired buffer"
'helm-source-projectile-dired-file)
"Helm source definition for Projectile delete files.")
(defclass helm-source-projectile-dired-file-other-window (helm-source-projectile-dired-file)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-dired-file-other-window))
"Make `helm-find-files-other-window' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-projectile-dired-file-actions
'(helm-find-files-other-window . :make-first))))
(defvar helm-source-projectile-dired-files-other-window-list
(helm-make-source "Projectile files in current Dired buffer"
'helm-source-projectile-dired-file-other-window)
"Helm source definition for Projectile delete files.")
(defclass helm-source-projectile-dired-file-other-frame (helm-source-projectile-dired-file)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-dired-file-other-frame))
"Make `find-file-other-frame' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-projectile-dired-file-actions
'(find-file-other-frame . :make-first))))
(defvar helm-source-projectile-dired-files-other-frame-list
(helm-make-source "Projectile files in current Dired buffer"
'helm-source-projectile-dired-file-other-frame)
"Helm source definition for Projectile delete files.")
(defun helm-projectile-dired-find-dir (dir)
"Jump to a selected directory DIR from `helm-projectile'."
(dired (expand-file-name dir (projectile-project-root)))
(run-hooks 'projectile-find-dir-hook))
(defun helm-projectile-dired-find-dir-other-window (dir)
"Jump to a selected directory DIR from `helm-projectile' (in other window)."
(dired-other-window (expand-file-name dir (projectile-project-root)))
(run-hooks 'projectile-find-dir-hook))
(defun helm-projectile-dired-find-dir-other-frame (dir)
"Jump to a selected directory DIR from `helm-projectile' (in other frame)."
(dired-other-frame (expand-file-name dir (projectile-project-root)))
(run-hooks 'projectile-find-dir-hook))
(defvar helm-source-projectile-directory-actions
'(("Open Dired" . helm-projectile-dired-find-dir)
("Open Dired in other window `C-c o'" . helm-projectile-dired-find-dir-other-window)
("Open Dired in other frame `C-c C-o'" . helm-projectile-dired-find-dir-other-frame)
("Switch to Eshell `M-e'" . helm-projectile-switch-to-shell)
("Grep in projects `C-s'" . helm-projectile-grep)
("Create Dired buffer from files `C-c f'" . helm-projectile-dired-files-new-action)
("Add files to Dired buffer `C-c a'" . helm-projectile-dired-files-add-action)
("Silver searcher (ag) in directory `C-S-a'" . helm-projectile--switch-project-and-ag-action)
("Ripgrep (rg) in directory `C-S-r'" . helm-projectile--switch-project-and-rg-action)))
(defclass helm-source-projectile-directory (helm-source-sync)
((candidates :initform (lambda ()
(when (projectile-project-p)
(with-temp-buffer
(hack-dir-local-variables-non-file-buffer)
(let ((dirs (if projectile-find-dir-includes-top-level
(append '("./") (projectile-current-project-dirs))
(projectile-current-project-dirs))))
(helm-projectile--files-display-real dirs (projectile-project-root)))))))
(fuzzy-match :initform 'helm-projectile-fuzzy-match)
(action-transformer :initform 'helm-find-files-action-transformer)
(keymap :initform (let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(helm-projectile-define-key map
(kbd "<left>") #'helm-previous-source
(kbd "<right>") #'helm-next-source
(kbd "C-c o") #'helm-projectile-dired-find-dir-other-window
(kbd "C-c C-o") #'helm-projectile-dired-find-dir-other-frame
(kbd "M-e") #'helm-projectile-switch-to-shell
(kbd "C-c f") #'helm-projectile-dired-files-new-action
(kbd "C-c a") #'helm-projectile-dired-files-add-action
(kbd "C-s") #'helm-projectile-grep
(kbd "C-S-a") #'helm-projectile--switch-project-and-ag-action
(kbd "C-S-r") #'helm-projectile--switch-project-and-rg-action)
map))
(help-message :initform 'helm-ff-help-message)
(mode-line :initform 'helm-read-file-name-mode-line-string)
(action :initform 'helm-source-projectile-directory-actions)))
(defvar helm-source-projectile-directories-list
(helm-make-source "Projectile directories" 'helm-source-projectile-directory)
"Helm source for listing project directories.")
(defclass helm-source-projectile-directory-other-window (helm-source-projectile-directory)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-directory-other-window))
"Make `helm-projectile-dired-find-dir-other-window' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-source-projectile-directory-actions
'(helm-projectile-dired-find-dir-other-window . :make-first))))
(defvar helm-source-projectile-directories-other-window-list
(helm-make-source "Projectile directories" 'helm-source-projectile-directory-other-window)
"Helm source for listing project directories.")
(defclass helm-source-projectile-directory-other-frame (helm-source-projectile-directory)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-directory-other-frame))
"Make `helm-projectile-dired-find-dir-other-frame' the first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-source-projectile-directory-actions
'(helm-projectile-dired-find-dir-other-frame . :make-first))))
(defvar helm-source-projectile-directories-other-frame-list
(helm-make-source "Projectile directories" 'helm-source-projectile-directory-other-frame)
"Helm source for listing project directories.")
(defvar helm-projectile-buffers-list-cache nil)
(defclass helm-source-projectile-buffer (helm-source-sync helm-type-buffer)
((init :initform (lambda ()
;; Issue #51 Create the list before `helm-buffer' creation.
(setq helm-projectile-buffers-list-cache
(ignore-errors
(let* ((buffers (projectile-project-buffer-names))
(current-buffer (buffer-name)))
(append (remove current-buffer buffers)
(unless helm-projectile-remove-current-buffer
;; Allow current buffer to be shown,
;; but push it to the end.
(list current-buffer))))))
(let ((result (cl-loop for b in helm-projectile-buffers-list-cache
maximize (length b) into len-buf
maximize (length (with-current-buffer b
(symbol-name major-mode)))
into len-mode
finally return (cons len-buf len-mode))))
(unless (default-value 'helm-buffer-max-length)
(helm-set-local-variable 'helm-buffer-max-length (car result)))
(unless (default-value 'helm-buffer-max-len-mode)
;; If a new buffer is longer that this value
;; this value will be updated
(helm-set-local-variable 'helm-buffer-max-len-mode (cdr result))))))
(candidates :initform 'helm-projectile-buffers-list-cache)
(matchplugin :initform nil)
(match :initform 'helm-buffers-match-function)
(persistent-action :initform 'helm-buffers-list-persistent-action)
(keymap :initform 'helm-buffer-map)
(volatile :initform t)
(persistent-help
:initform
"Show this buffer / C-u \\[helm-execute-persistent-action]: Kill this buffer")))
(defvar helm-source-projectile-buffers-list
(helm-make-source "Project buffers" 'helm-source-projectile-buffer))
(defclass helm-source-projectile-buffer-other-window (helm-source-projectile-buffer)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-buffer-other-window))
"Make `helm-buffer-switch-buffers-other-window' first action in SOURCE."
(setf (slot-value source 'action)
(helm-projectile-hack-actions
helm-type-buffer-actions
'(helm-buffer-switch-buffers-other-window . :make-first))))
(defvar helm-source-projectile-buffers-other-window-list
(helm-make-source "Project buffers" 'helm-source-projectile-buffer-other-window))
(defclass helm-source-projectile-buffer-other-frame (helm-source-projectile-buffer)
())
(cl-defmethod helm-setup-user-source ((source helm-source-projectile-buffer-other-frame))
"Make `helm-buffer-switch-to-buffer-other-frame' first action in SOURCE."
(setf
(slot-value source 'action)
(helm-projectile-hack-actions
helm-type-buffer-actions
'(helm-buffer-switch-to-buffer-other-frame . :make-first))))
(defvar helm-source-projectile-buffers-other-frame-list
(helm-make-source "Project buffers" 'helm-source-projectile-buffer-other-frame))
(defvar helm-source-projectile-recentf-list
(helm-build-sync-source "Projectile recent files"
:candidates (lambda ()
(when (projectile-project-p)
(with-temp-buffer
(hack-dir-local-variables-non-file-buffer)
(helm-projectile--files-display-real (projectile-recentf-files)
(projectile-project-root)))))
:fuzzy-match helm-projectile-fuzzy-match
:keymap helm-projectile-find-file-map
:help-message 'helm-ff-help-message
:mode-line helm-read-file-name-mode-line-string
:action helm-projectile-file-actions
:persistent-action #'helm-projectile-file-persistent-action
:persistent-help "Preview file")
"Helm source definition for recent files in current project.")
(defcustom helm-projectile-git-grep-command
"git --no-pager grep --no-color -n%c -e %p -- %f"
"Command to execute when performing `helm-grep' inside a projectile git project.
See documentation of `helm-grep-default-command' for the format."
:type 'string
:group 'helm-projectile)
(defcustom helm-projectile-grep-command
"grep -a -r %e -n%cH -e %p %f ."
"Command to execute when performing `helm-grep' outside a projectile git project.
See documentation of `helm-grep-default-command' for the format."
:type 'string
:group 'helm-projectile)
(defcustom helm-projectile-sources-list
'(helm-source-projectile-buffers-list
helm-source-projectile-files-list
helm-source-projectile-projects)
"Default sources for `helm-projectile'."