forked from bbatsov/projectile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-projectile.el
800 lines (723 loc) · 36.6 KB
/
helm-projectile.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
;;; helm-projectile.el --- Helm integration for Projectile
;; Copyright (C) 2011-2013 Bozhidar Batsov
;; Author: Bozhidar Batsov
;; URL: https://github.com/bbatsov/projectile
;; Created: 2011-31-07
;; Keywords: project, convenience
;; Version: 0.11.0
;; Package-Requires: ((helm "1.4.0") (projectile "0.11.0") (cl-lib "0.3"))
;; 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:
(require 'projectile)
(require 'helm-config)
(require 'helm-locate)
(require 'helm-buffers)
(require 'helm-files)
(require 'cl-lib)
(declare-function eshell "eshell")
(defgroup helm-projectile nil
"Helm support for projectile."
:prefix "helm-projectile-"
:group 'projectile
:link `(url-link :tag "helm-projectile homepage" "https://github.com/bbatsov/projectile"))
(defvar helm-projectile-current-project-root)
(defun helm-projectile-coerce-file (candidate)
(with-current-buffer (helm-candidate-buffer)
(expand-file-name candidate (projectile-project-root))))
(defmacro helm-projectile-define-key (keymap key def &rest bindings)
"In KEYMAP, define key sequence KEY1 as DEF1, KEY2 as DEF2 ..."
(declare (indent defun))
(let ((ret '(progn)))
(while key
(push
`(define-key ,keymap ,key
(lambda ()
(interactive)
(helm-quit-and-execute-action ,def)))
ret)
(setq key (pop bindings)
def (pop 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, after applying the PRESCRIPTION.
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.
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)))
(if (stringp new)
(setcar action 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)))
(append actions (nreverse new))))
(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 dir)))
(defun helm-projectile-remove-known-project (_ignore)
"Delete selected projects.
_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 "Delete *%s projects(s)" len)))
(message "(No deletion performed)")
(progn
(mapc (lambda (p)
(delete p projectile-known-projects))
projects)
(projectile-save-known-projects))
(message "%s projects(s) deleted" len)))))
(defvar helm-source-projectile-projects
(helm-build-in-buffer-source "Projectile projects"
:data (lambda ()
(if (projectile-project-p)
(cons (abbreviate-file-name (projectile-project-root))
(projectile-relevant-known-projects))
projectile-known-projects))
:fuzzy-match t
:keymap (let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(helm-projectile-define-key map
(kbd "C-d") 'dired
(kbd "M-g") 'helm-projectile-vc
(kbd "M-e") 'helm-projectile-switch-to-eshell
(kbd "C-s") 'helm-find-files-grep
(kbd "M-c") 'helm-projectile-compile-project
(kbd "M-D") 'helm-projectile-remove-known-project)
map)
:mode-line helm-ff-mode-line-string
:action '(("Switch to project" .
(lambda (project)
(let ((projectile-completion-system 'helm))
(projectile-switch-project-by-name project))))
("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-eshell)
("Grep in projects `C-s'. With C-u, recurse" . helm-find-files-grep)
("Compile project `M-c'. With C-u, new compile command"
. helm-projectile-compile-project)
("Remove project(s) `M-D'" . helm-projectile-remove-known-project)))
"Helm source for known projectile projects.")
(define-key helm-etags-map (kbd "C-c p f") (lambda ()
(interactive)
(helm-run-after-quit 'helm-projectile-find-file nil)))
(defun helm-projectile-find-files-eshell-command-on-file-action (_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)
(interactive)
(let* ((helm-ff-default-directory (file-name-directory _candidate)))
(helm-ff-etags-select _candidate)))
(defun helm-projectile-switch-to-eshell (dir)
(interactive)
(let* ((helm-ff-default-directory (file-name-directory dir)))
(helm-ff-switch-to-eshell 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)))
(-filter (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 in 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 ((new-name (completing-read "Select a Dired buffer:"
(helm-projectile-all-dired-buffers)))
(helm--reading-passwd-or-string t)
(files (mapcar (lambda (file)
(replace-regexp-in-string (projectile-project-root) "" file))
(helm-marked-candidates :with-wildcard 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* ((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)))
(marked-files (helm-marked-candidates :with-wildcard t))
(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))))))
(defvar helm-projectile-find-file-map
(let ((map (copy-keymap helm-find-files-map)))
(define-key map (kbd "<left>") 'helm-previous-source)
(define-key map (kbd "<right>") 'helm-next-source)
(helm-projectile-define-key map
(kbd "C-c f") 'helm-projectile-dired-from-files
(kbd "M-e") 'helm-projectile-switch-to-eshell
(kbd "M-.") 'helm-projectile-ff-etags-select-action
(kbd "M-!") 'helm-projectile-find-files-eshell-command-on-file-action)
map))
(defvar helm-projectile-file-actions
(helm-projectile-hack-actions
(cdr (assq 'action (or helm-source-find-files
(helm-make-source "Find files" 'helm-source-ffiles))))
;; 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-eshell . helm-projectile-switch-to-eshell)
'(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.")
(defvar helm-source-projectile-files-dwim-list
(helm-build-in-buffer-source "Projectile files"
:data (lambda ()
(let* ((project-files (projectile-current-project-files))
(files (projectile-select-files project-files)))
(cond
((= (length files) 1)
(find-file (expand-file-name (car files) (projectile-project-root)))
(helm-exit-minibuffer))
((> (length files) 1) files)
(t project-files))))
:coerce 'helm-projectile-coerce-file
:action-transformer 'helm-find-files-action-transformer
:keymap (let ((map (copy-keymap helm-find-files-map)))
(define-key map (kbd "<left>") 'helm-previous-source)
(define-key map (kbd "<right>") 'helm-next-source)
map)
:help-message helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-file-actions)
"Helm source definition for Projectile files based on context.")
(defvar helm-source-projectile-files-list
(helm-build-in-buffer-source "Projectile files"
:data (lambda ()
(projectile-current-project-files))
:coerce 'helm-projectile-coerce-file
:keymap (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)
map)
:help-message 'helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-file-actions
)
"Helm source definition for Projectile files.")
(defvar helm-source-projectile-files-in-all-projects-list
(helm-build-in-buffer-source "Projectile files in all Projects"
:data (lambda ()
(let ((projectile-require-project-root nil))
(projectile-all-project-files)))
:coerce 'helm-projectile-coerce-file
:keymap helm-projectile-find-file-map
:help-message 'helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-file-actions
)
"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'")))
(defvar helm-source-projectile-dired-files-list
(helm-build-in-buffer-source "Projectile files in current Dired buffer"
:data (lambda ()
(if (and (file-remote-p (projectile-project-root))
(not helm-projectile-virtual-dired-remote-enable))
nil
(let ((default-directory (projectile-project-root)))
(when (eq major-mode 'dired-mode)
(mapcar (lambda (file)
(replace-regexp-in-string default-directory "" file))
(helm-projectile-files-in-current-dired-buffer))))))
:coerce 'helm-projectile-coerce-file
:filter-one-by-one (lambda (file)
(let ((default-directory (projectile-project-root)))
(helm-ff-filter-candidate-one-by-one file)))
:action-transformer 'helm-find-files-action-transformer
:keymap (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 "C-c d") 'helm-projectile-dired-files-delete-action)
map)
:help-message 'helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-dired-file-actions)
"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."
(dired-other-window (expand-file-name dir (projectile-project-root)))
(run-hooks 'projectile-find-dir-hook))
(defvar helm-source-projectile-directories-list
(helm-build-in-buffer-source "Projectile directories"
:data (lambda ()
(if projectile-find-dir-includes-top-level
(append '("./") (projectile-current-project-dirs))
(projectile-current-project-dirs)))
:coerce 'helm-projectile-coerce-file
:action-transformer 'helm-find-files-action-transformer
:keymap (let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(helm-projectile-define-key map
(kbd "C-c o") 'helm-projectile-dired-find-dir-other-window
(kbd "M-e") 'helm-projectile-switch-to-eshell
(kbd "C-c f") 'helm-projectile-dired-files-new-action
(kbd "C-c a") 'helm-projectile-dired-files-add-action
(kbd "C-s") 'helm-find-files-grep)
map)
:help-message 'helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action '(("Open Dired" . helm-projectile-dired-find-dir)
("Open Dired in other window `C-c o'" . helm-projectile-dired-find-dir)
("Switch to Eshell `M-e'" . helm-projectile-switch-to-eshell)
("Grep in projects `C-s C-u Recurse'" . helm-find-files-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)))
"Helm source for listing project directories.")
(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-buffers-list-cache (projectile-project-buffer-names))
(let ((result (cl-loop for b in helm-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 helm-buffer-max-length
(setq helm-buffer-max-length (car result)))
(unless helm-buffer-max-len-mode
;; If a new buffer is longer that this value
;; this value will be updated
(setq helm-buffer-max-len-mode (cdr result))))))
(candidates :initform helm-buffers-list-cache)
(matchplugin :initform nil)
(match :initform 'helm-buffers-list--match-fn)
(persistent-action :initform 'helm-buffers-list-persistent-action)
(keymap :initform helm-buffer-map)
(volatile :initform t)
(mode-line :initform helm-buffer-mode-line-string)
(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))
(defvar helm-source-projectile-recentf-list
(helm-build-in-buffer-source "Projectile recent files"
:data (lambda ()
(projectile-recentf-files))
:coerce 'helm-projectile-coerce-file
:keymap helm-projectile-find-file-map
:help-message 'helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-file-actions
)
"Helm source definition for recent files in current project.")
(defvar helm-source-projectile-files-and-dired-list
'(helm-source-projectile-dired-files-list
helm-source-projectile-files-list))
(defvar helm-source-projectile-directories-and-dired-list
'(helm-source-projectile-dired-files-list
helm-source-projectile-directories-list))
(defcustom helm-projectile-sources-list
'(helm-source-projectile-buffers-list
helm-source-projectile-files-list
helm-source-projectile-projects
)
"Default sources for `helm-projectile'."
:group 'helm-projectile)
(defmacro helm-projectile-command (command source prompt)
"Template for generic helm-projectile commands.
COMMAND is a command name to be appended with \"helm-projectile\" prefix.
SOURCE is a Helm source that should be Projectile specific.
PROMPT is a string for displaying as a prompt."
`(defun ,(intern (concat "helm-projectile-" command)) (&optional arg)
"Use projectile with Helm for finding files in project
With a prefix ARG invalidates the cache first."
(interactive "P")
(if (projectile-project-p)
(projectile-maybe-invalidate-cache arg))
(let ((helm-ff-transformer-show-only-basename nil)
;; for consistency, we should just let Projectile take care of ignored files
(helm-boring-file-regexp-list nil))
(helm :sources ,source
:buffer "*helm projectile*"
:prompt (projectile-prepend-project-name ,prompt)))))
(helm-projectile-command "switch-project" 'helm-source-projectile-projects "Switch to project: ")
(helm-projectile-command "find-file" helm-source-projectile-files-and-dired-list "Find file: ")
(helm-projectile-command "find-file-in-known-projects" 'helm-source-projectile-files-in-all-projects-list "Find file in projects: ")
(helm-projectile-command "find-file-dwim" 'helm-source-projectile-files-dwim-list "Find file: ")
(helm-projectile-command "find-dir" helm-source-projectile-directories-and-dired-list "Find dir: ")
(helm-projectile-command "recentf" 'helm-source-projectile-recentf-list "Recently visited file: ")
(helm-projectile-command "switch-to-buffer" 'helm-source-projectile-buffers-list "Switch to buffer: ")
(defun helm-projectile-find-other-file (&optional flex-matching)
"Switch between files with the same name but different extensions using Helm.
With FLEX-MATCHING, match any file that contains the base name of current file.
Other file extensions can be customized with the variable `projectile-other-file-alist'."
(interactive "P")
(-if-let (other-files (projectile-get-other-files (buffer-file-name)
(projectile-current-project-files)
flex-matching))
(if (= (length other-files) 1)
(find-file (expand-file-name (car other-files) (projectile-project-root)))
(progn
(let* ((helm-ff-transformer-show-only-basename nil))
(helm :sources (helm-build-in-buffer-source "Projectile other files"
:data other-files
:coerce 'helm-projectile-coerce-file
:keymap (let ((map (copy-keymap helm-find-files-map)))
(define-key map (kbd "<left>") 'helm-previous-source)
(define-key map (kbd "<right>") 'helm-next-source)
map)
:help-message helm-ff-help-message
:mode-line helm-ff-mode-line-string
:action helm-projectile-file-actions)
:buffer "*helm projectile*"
:prompt (projectile-prepend-project-name "Find other file: ")))))
(error "No other file found")))
(defun helm-projectile-grep-or-ack (&optional use-ack-p ack-ignored-pattern ack-executable)
"Perform helm-grep at project root.
USE-ACK-P indicates whether to use ack or not.
ACK-IGNORED-PATTERN is a file regex to exclude from searching.
ACK-EXECUTABLE is the actual ack binary name.
It is usually \"ack\" or \"ack-grep\".
If it is nil, or ack/ack-grep not found then use default grep command."
(let* ((default-directory (projectile-project-root))
(helm-ff-default-directory (projectile-project-root))
(follow (and helm-follow-mode-persistent
(assoc-default 'follow helm-source-grep)))
(helm-grep-in-recurse t)
(grep-find-ignored-files (-union projectile-globally-ignored-files grep-find-ignored-files))
(grep-find-ignored-directories (-union projectile-globally-ignored-directories grep-find-ignored-directories))
(helm-grep-default-command (if use-ack-p
(concat ack-executable " -H --smart-case --no-group --no-color " ack-ignored-pattern " %p %f")
(concat "grep -a -d recurse %e -n%cH -e %p %f " (projectile-project-root))))
(helm-grep-default-recurse-command helm-grep-default-command)
(helm-source-grep
(helm-build-async-source
(capitalize (helm-grep-command t))
:header-name (lambda (name)
(let ((name (if use-ack-p
"Ack"
"Grep")))
(concat name " " "(C-c ? Help)")))
:candidates-process 'helm-grep-collect-candidates
:filter-one-by-one 'helm-grep-filter-one-by-one
:candidate-number-limit 9999
:nohighlight t
:mode-line helm-grep-mode-line-string
;; We need to specify keymap here and as :keymap arg [1]
;; to make it available in further resuming.
:keymap helm-grep-map
:history 'helm-grep-history
:action (helm-make-actions
"Find file" 'helm-grep-action
"Find file other frame" 'helm-grep-other-frame
(lambda () (and (locate-library "elscreen")
"Find file in Elscreen"))
'helm-grep-jump-elscreen
"Save results in grep buffer" 'helm-grep-save-results
"Find file other window" 'helm-grep-other-window)
:persistent-action 'helm-grep-persistent-action
:persistent-help "Jump to line (`C-u' Record in mark ring)"
:requires-pattern 2)))
(helm
:sources 'helm-source-grep
:input (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'symbol))
:buffer (format "*helm %s*" (if use-ack-p
"ack"
"grep"))
:default-directory (projectile-project-root)
:keymap helm-grep-map
:history 'helm-grep-history
:truncate-lines t)))
(defun helm-projectile-on ()
"Turn on helm-projectile key bindings."
(interactive)
(message "Turn on helm-projectile key bindings")
(helm-projectile-toggle 1))
(defun helm-projectile-off ()
"Turn off helm-projectile key bindings."
(interactive)
(message "Turn off helm-projectile key bindings")
(helm-projectile-toggle -1))
(defun helm-projectile-grep ()
"Helm version of projectile-grep."
(interactive)
(funcall'run-with-timer 0.01 nil
#'helm-projectile-grep-or-ack nil))
(defun helm-projectile-ack ()
"Helm version of projectile-ack."
(interactive)
(let ((ack-ignored (mapconcat
'identity
(-union (-map (lambda (path)
(concat "--ignore-dir=" (file-name-nondirectory (directory-file-name path))))
projectile-globally-ignored-directories)
(-map (lambda (path)
(concat "--ignore-file=match:" (shell-quote-argument path)))
projectile-globally-ignored-files)) " "))
(helm-ack-grep-executable (cond
((executable-find "ack") "ack")
((executable-find "ack-grep") "ack-grep")
(t (error "ack or ack-grep is not available.")))))
(funcall 'run-with-timer 0.01 nil
#'helm-projectile-grep-or-ack t ack-ignored helm-ack-grep-executable)))
(defun helm-projectile-ag ()
"Helm version of projectile-ag."
(interactive)
(unless (executable-find "ag")
(error "ag not available"))
(if (require 'helm-ag nil 'noerror)
(let* ((helm-ag-insert-at-point 'symbol)
(grep-find-ignored-files (-union projectile-globally-ignored-files grep-find-ignored-files))
(grep-find-ignored-directories (-union projectile-globally-ignored-directories grep-find-ignored-directories))
(ignored (mapconcat (lambda (i)
(concat "--ignore " i))
(append grep-find-ignored-files grep-find-ignored-directories)
" "))
(helm-ag-base-command (concat helm-ag-base-command " " ignored)))
(helm-do-ag (projectile-project-root)))
(error "helm-ag not available")))
(defun helm-projectile-commander-bindings ()
(def-projectile-commander-method ?a
"Run ack on project."
(call-interactively 'helm-projectile-ack))
(def-projectile-commander-method ?A
"Find ag on project."
(call-interactively 'helm-projectile-ag))
(def-projectile-commander-method ?f
"Find file in project."
(helm-projectile-find-file))
(def-projectile-commander-method ?b
"Switch to project buffer."
(helm-projectile-switch-to-buffer))
(def-projectile-commander-method ?d
"Find directory in project."
(helm-projectile-find-dir))
(def-projectile-commander-method ?g
"Run grep on project."
(helm-projectile-grep))
(def-projectile-commander-method ?s
"Switch project."
(helm-projectile-switch-project))
(def-projectile-commander-method ?e
"Find recently visited file in project."
(helm-projectile-recentf)))
(defun helm-projectile-toggle (toggle)
"Toggle Helm version of Projectile commands."
(if (> toggle 0)
(progn
(define-key projectile-command-map (kbd "a") 'helm-projectile-find-other-file)
(define-key projectile-command-map (kbd "f") 'helm-projectile-find-file)
(define-key projectile-command-map (kbd "F") 'helm-projectile-find-file-in-known-projects)
(define-key projectile-command-map (kbd "g") 'helm-projectile-find-file-dwim)
(define-key projectile-command-map (kbd "d") 'helm-projectile-find-dir)
(define-key projectile-command-map (kbd "p") 'helm-projectile-switch-project)
(define-key projectile-command-map (kbd "e") 'helm-projectile-recentf)
(define-key projectile-command-map (kbd "b") 'helm-projectile-switch-to-buffer)
(define-key projectile-command-map (kbd "s a") 'helm-projectile-ack)
(define-key projectile-command-map (kbd "s g") 'helm-projectile-grep)
(define-key projectile-command-map (kbd "s s") 'helm-projectile-ag)
(helm-projectile-commander-bindings))
(progn
(define-key projectile-command-map (kbd "a") 'projectile-find-other-file)
(define-key projectile-command-map (kbd "f") 'projectile-find-file)
(define-key projectile-command-map (kbd "F") 'projectile-find-file-in-known-projects)
(define-key projectile-command-map (kbd "g") 'helm-projectile-find-file-dwim)
(define-key projectile-command-map (kbd "d") 'projectile-find-dir)
(define-key projectile-command-map (kbd "p") 'projectile-switch-project)
(define-key projectile-command-map (kbd "e") 'projectile-recentf)
(define-key projectile-command-map (kbd "b") 'projectile-switch-to-buffer)
(define-key projectile-command-map (kbd "s a") 'projectile-ack)
(define-key projectile-command-map (kbd "s g") 'projectile-grep)
(define-key projectile-command-map (kbd "s s") 'projectile-ag)
(projectile-commander-bindings))))
;;;###autoload
(defun helm-projectile (&optional arg)
"Use projectile with Helm instead of ido.
With a prefix ARG invalidates the cache first.
If invoked outside of a project, displays a list of known projects to jump."
(interactive "P")
(if (projectile-project-p)
(projectile-maybe-invalidate-cache arg))
(let ((helm-ff-transformer-show-only-basename nil)
(src (if (projectile-project-p)
helm-projectile-sources-list
helm-source-projectile-projects)))
(helm :sources src
:buffer "*helm projectile*"
:prompt (projectile-prepend-project-name (if (projectile-project-p)
"pattern: "
"Switch to project: ")))))
;;;###autoload
(eval-after-load 'projectile
'(progn
(define-key projectile-command-map (kbd "h") 'helm-projectile)))
(provide 'helm-projectile)
;;; helm-projectile.el ends here