forked from bbatsov/projectile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.el
1164 lines (992 loc) · 45.1 KB
/
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
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
;;; projectile.el --- Manage and navigate projects in Emacs easily
;; Copyright © 2011-2013 Bozhidar Batsov <[email protected]>
;; Author: Bozhidar Batsov <[email protected]>
;; URL: https://github.com/bbatsov/projectile
;; Keywords: project, convenience
;; Version: 1.0.0-cvs
;; Package-Requires: ((s "1.6.0") (dash "1.5.0") (pkg-info "0.1"))
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, 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 'thingatpt)
(require 's)
(require 'dash)
(require 'grep) ; For `rgrep'
(require 'package) ; For `package-buffer-info' and `package-version-join'
;;; Customization
(defgroup projectile nil
"Manage and navigate projects easily."
:group 'tools
:group 'convenience)
(defcustom projectile-indexing-method (if (eq system-type 'windows-nt) 'native 'alien)
"Specifies the indexing method used by Projectile.
There are two indexing methods - native and alien.
The native method is implemented in Emacs Lisp (therefore it is
native to Emacs). It's advantage is that is portable and will
work everywhere that Emacs does. It's disadvantage is that is a
bit slow (especially for large projects). Generally it's a good
idea to pair the native indexing method with caching.
The alien indexing method uses external tools (e.g. git, find,
etc) to speed up the indexing process. The disadvantage of this
method is that it's not well supported on Windows systems.
By default alien indexing is the default on all operating
systems, except Windows."
:group 'projectile
:type 'symbol
:options '(native alien))
(defcustom projectile-enable-caching (eq projectile-indexing-method 'native)
"When t enables project files caching.
Project caching is automatically enabled by default if you're
using the native indexing method."
:group 'projectile
:type 'boolean)
(defcustom projectile-require-project-root t
"Require the presence of a project root to operate when true.
Otherwise consider the current directory the project root."
:group 'projectile
:type 'boolean)
(defcustom projectile-completion-system 'ido
"The completion system to be used by Projectile."
:group 'projectile
:type 'symbol
:options '(ido grizzl default))
(defcustom projectile-ack-function 'ack-and-a-half
"The ack function to use."
:group 'projectile
:type 'symbol
:options '(ack-and-a-half default))
(defcustom projectile-keymap-prefix (kbd "C-c p")
"Projectile keymap prefix."
:group 'projectile
:type 'string)
(defcustom projectile-cache-file
(expand-file-name "projectile.cache" user-emacs-directory)
"The name of Projectile's cache file."
:group 'projectile
:type 'string)
(defcustom projectile-tags-command "ctags -Re %s %s"
"The command Projectile's going to use to generate a TAGS file."
:group 'projectile
:type 'string)
(defcustom projectile-project-root-files
'(".projectile" ; projectile project marker
".git" ; Git VCS root dir
".hg" ; Mercurial VCS root dir
".fslckout" ; Fossil VCS root dir
".bzr" ; Bazaar VCS root dir
"_darcs" ; Darcs VCS root dir
"rebar.config" ; Rebar project file
"project.clj" ; Leiningen project file
"pom.xml" ; Maven project file
"build.sbt" ; SBT project file
"build.gradle" ; Gradle project file
"Gemfile" ; Bundler file
"Makefile" ; Make project file
)
"A list of files considered to mark the root of a project."
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-ignored-files
'("TAGS")
"A list of files globally ignored by projectile."
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-ignored-directories
'(".idea"
".eunit"
".git"
".hg"
".fslckout"
".bzr"
"_darcs")
"A list of directories globally ignored by projectile."
:group 'projectile
:type '(repeat string))
(defcustom projectile-find-file-hook nil
"Hooks run when a file is opened with `projectile-find-file'."
:group 'projectile
:type 'hook)
(defcustom projectile-find-dir-hook nil
"Hooks run when a directory is opened with `projectile-find-dir'."
:group 'projectile
:type 'hook)
(defcustom projectile-switch-project-action 'projectile-find-file
"Action invoked after switching projects with `projectile-switch-project'.
Any function that does not take arguments will do."
:group 'projectile
:type 'symbol)
;;; Serialization
(defun projectile-serialize (data filename)
"Serialize DATA to FILENAME.
The saved data can be restored with `projectile-unserialize'."
(when (file-writable-p filename)
(with-temp-file filename
(insert (prin1-to-string data)))))
(defun projectile-unserialize (filename)
"Read data serialized by `projectile-serialize' from FILENAME."
(when (file-exists-p filename)
(with-temp-buffer
(insert-file-contents filename)
(read (buffer-string)))))
(defvar projectile-projects-cache
(or (projectile-unserialize projectile-cache-file)
(make-hash-table :test 'equal))
"A hashmap used to cache project file names to speed up related operations.")
(defvar projectile-known-projects nil
"List of locations where we have previously seen projects.
The list of projects is ordered by the time they have been accessed.")
(defcustom projectile-known-projects-file
(expand-file-name "projectile-bookmarks.eld"
user-emacs-directory)
"Name and location of the Projectile's known projects file."
:group 'projectile
:type 'string)
;;; Version information
(defun projectile-library-version ()
"Get the version in the Projectile library header."
(-when-let (version (pkg-info-defining-library-version 'projectile-mode))
(pkg-info-format-version version)))
(defun projectile-package-version ()
"Get the package version of Projectile.
This is the version number of the installed Projectile package."
(-when-let (version (pkg-info-package-version 'projectile))
(pkg-info-format-version version)))
(defun projectile-version (&optional show-version)
"Get the Projectile version as string.
If called interactively or if SHOW-VERSION is non-nil, show the
version in the echo area and the messages buffer.
The returned string includes both, the version from package.el
and the library version, if both a present and different.
If the version number could not be determined, signal an error,
if called interactively, or if SHOW-VERSION is non-nil, otherwise
just return nil."
(interactive (list (not (or executing-kbd-macro noninteractive))))
(let* ((lib-version (projectile-library-version))
(pkg-version (projectile-package-version))
(version (cond
((and lib-version pkg-version
(not (string= lib-version pkg-version)))
(format "%s (package: %s)" lib-version pkg-version))
((or pkg-version lib-version)
(format "%s" (or pkg-version lib-version))))))
(when show-version
(unless version
(error "Could not find out Projectile version"))
(message "Projectile version: %s" version))
version))
;;; Caching
(defun projectile-invalidate-cache (arg)
"Remove the current project's files from `projectile-projects-cache'.
With a prefix argument ARG prompts for the name of the project whose cache
to invalidate."
(interactive "P")
(let ((project-root
(if arg
(completing-read "Remove cache for: "
(projectile-hash-keys projectile-projects-cache))
(projectile-project-root))))
(remhash project-root projectile-projects-cache)
(projectile-serialize-cache)
(message "Invalidated Projectile cache for %s."
(propertize project-root 'face 'font-lock-keyword-face))))
(defun projectile-cache-project (project files)
"Cache PROJECTs FILES.
The cache is created both in memory and on the hard drive."
(when projectile-enable-caching
(puthash project files projectile-projects-cache)
(projectile-serialize-cache)))
(defun projectile-file-cached-p (file project)
"Check if FILE is already in PROJECT cache."
(member file (gethash project projectile-projects-cache)))
(defun projectile-cache-current-file ()
"Add the currently visited file to the cache."
(interactive)
(let* ((current-project (projectile-project-root))
(current-file (file-relative-name (buffer-file-name (current-buffer)) current-project)))
(unless (projectile-file-cached-p current-file current-project)
(puthash current-project
(cons current-file (gethash current-project projectile-projects-cache))
projectile-projects-cache)
(projectile-serialize-cache)
(message "File %s added to project %s cache." current-file current-project))))
;; cache opened files automatically to reduce the need for cache invalidation
(defun projectile-cache-files-find-file-hook ()
"Function for caching files with `find-file-hook'."
(when (and (projectile-project-p) projectile-enable-caching)
(projectile-cache-current-file)))
(defun projectile-cache-projects-find-file-hook ()
"Function for caching projects with `find-file-hook'."
(when (projectile-project-p)
(projectile-add-known-project (projectile-project-root))
(projectile-save-known-projects)))
;;; Project root related utilities
(defun projectile-project-root ()
"Retrieves the root directory of a project if available.
The current directory is assumed to be the project's root otherwise."
(let ((project-root
(or (->> projectile-project-root-files
(--map (locate-dominating-file default-directory it))
(-remove #'null)
(car)
(projectile-file-truename))
(if projectile-require-project-root
(error "You're not into a project")
default-directory))))
project-root))
(defun projectile-file-truename (file-name)
"A thin wrapper around `expand-file-name' that handles nil.
Expand FILE-NAME using `default-directory'."
(when file-name
(file-truename file-name)))
(defun projectile-project-p ()
"Check if we're in a project."
(condition-case nil
(projectile-project-root)
(error nil)))
(defun projectile-project-name ()
"Return project name."
(let ((project-root
(condition-case nil
(projectile-project-root)
(error default-directory))))
(file-name-nondirectory (directory-file-name project-root))))
;;; Project indexing
(defun projectile-get-project-directories ()
"Get the list of project directories that are of interest to the user."
(-map (lambda (subdir) (concat (projectile-project-root) subdir))
(or (car (projectile-parse-dirconfig-file)) '(""))))
(defun projectile-dir-files (directory)
"List the files in DIRECTORY and in its sub-directories.
Files are returned as relative paths to the project root."
;; check for a cache hit first if caching is enabled
(let ((files-list (and projectile-enable-caching
(gethash directory projectile-projects-cache)))
(root (projectile-project-root)))
;; cache disabled or cache miss
(or files-list
(if (eq projectile-indexing-method 'native)
(projectile-dir-files-native root directory)
;; use external tools to get the project files
(projectile-remove-ignored (projectile-dir-files-external root directory))))))
(defun projectile-dir-files-native (root directory)
"Get the files for ROOT under DIRECTORY using just Emacs Lisp."
(message "Projectile is indexing %s. This may take a while."
(propertize directory 'face 'font-lock-keyword-face))
;; we need the files with paths relative to the project root
(-map (lambda (file) (s-chop-prefix root file))
(projectile-index-directory directory (projectile-patterns-to-ignore))))
(defun projectile-dir-files-external (root directory)
"Get the files for ROOT under DIRECTORY using external tools."
(let ((default-directory directory)
(files-list nil))
(setq files-list (-map (lambda (f)
(s-chop-prefix root (expand-file-name f directory)))
(projectile-get-repo-files)))
files-list))
(defcustom projectile-git-command "git ls-files -zco --exclude-standard"
"Command used by projectile to get the files in a git project."
:group 'projectile
:type 'string)
(defcustom projectile-hg-command "hg locate -0 -I ."
"Command used by projectile to get the files in a hg project."
:group 'projectile
:type 'string)
(defcustom projectile-fossil-command "fossil ls"
"Command used by projectile to get the files in a fossil project."
:group 'projectile
:type 'string)
(defcustom projectile-bzr-command "bzr ls --versioned -0"
"Command used by projectile to get the files in a bazaar project."
:group 'projectile
:type 'string)
(defcustom projectile-darcs-command "darcs show files -0 . "
"Command used by projectile to get the files in a darcs project."
:group 'projectile
:type 'string)
(defcustom projectile-svn-command "find . -type f -print0"
"Command used by projectile to get the files in a svn project."
:group 'projectile
:type 'string)
(defcustom projectile-generic-command "find . -type f -print0"
"Command used by projectile to get the files in a generic project."
:group 'projectile
:type 'string)
(defun projectile-get-ext-command ()
"Determine which external command to invoke based on the project's VCS."
(let ((vcs (projectile-project-vcs)))
(cond
((eq vcs 'git) projectile-git-command)
((eq vcs 'hg) projectile-hg-command)
((eq vcs 'fossil) projectile-fossil-command)
((eq vcs 'bzr) projectile-bzr-command)
((eq vcs 'darcs) projectile-darcs-command)
((eq vcs 'svn) projectile-svn-command)
(t projectile-generic-command))))
(defun projectile-get-repo-files ()
"Get a list of the files in the project."
(projectile-files-via-ext-command (projectile-get-ext-command)))
(defun projectile-files-via-ext-command (command)
"Get a list of relative file names in the project root by executing COMMAND."
(split-string (shell-command-to-string command) "\0" t))
(defun projectile-index-directory (directory patterns)
"Index DIRECTORY taking into account PATTERNS.
The function calls itself recursively until all sub-directories
have been indexed."
(let (files-list)
(dolist (current-file (file-name-all-completions "" directory) files-list)
(let ((absolute-file (expand-file-name current-file directory)))
(cond
;; check for directories that are not ignored
((and (s-ends-with-p "/" current-file)
;; avoid loops & ignore some well known directories
(not (-any? (lambda (file)
(string= (s-chop-suffix "/" current-file) file))
'("." ".." ".svn" ".cvs")))
(not (projectile-ignored-directory-p absolute-file))
(not (and patterns
(projectile-ignored-rel-p absolute-file
directory patterns))))
(setq files-list (append files-list
(projectile-index-directory
(expand-file-name current-file directory)
patterns))))
;; check for regular files that are not ignored
((and (not (s-ends-with-p "/" current-file))
(not (projectile-ignored-file-p absolute-file))
(not (and patterns
(projectile-ignored-rel-p absolute-file
directory patterns))))
(setq files-list (cons
(expand-file-name current-file directory)
files-list))))))))
(defun projectile-remove-ignored (files)
"Remove ignored files and folders from FILES.
Operates on filenames relative to the project root."
(let ((ignored (append (projectile-ignored-files-rel)
(projectile-ignored-directories-rel))))
(-remove (lambda (file)
(--any-p (s-starts-with-p it file) ignored))
files)))
(defun projectile-project-buffers ()
"Get a list of project buffers."
(let ((project-root (projectile-project-root)))
(-filter (lambda (buffer)
(projectile-project-buffer-p buffer project-root))
(buffer-list))))
(defun projectile-project-buffer-p (buffer project-root)
"Check if BUFFER is under PROJECT-ROOT."
(with-current-buffer buffer
(and (s-starts-with? project-root
(file-truename default-directory))
;; ignore hidden buffers
(not (s-starts-with? " " (buffer-name buffer))))))
(defun projectile-project-buffer-names ()
"Get a list of project buffer names."
(-map 'buffer-name (projectile-project-buffers)))
(defun projectile-prepend-project-name (string)
"Prepend the current project's name to STRING."
(format "[%s] %s" (projectile-project-name) string))
(defun projectile-switch-to-buffer ()
"Switch to a project buffer."
(interactive)
(switch-to-buffer
(projectile-completing-read
"Switch to buffer: "
(projectile-project-buffer-names))))
(defun projectile-multi-occur ()
"Do a `multi-occur' in the project's buffers."
(interactive)
(multi-occur (projectile-project-buffers)
(car (occur-read-primary-args))))
(defun projectile-ignored-directory-p (directory)
"Check if DIRECTORY should be ignored."
(member directory (projectile-ignored-directories)))
(defun projectile-ignored-file-p (file)
"Check if FILE should be ignored."
(member file (projectile-ignored-files)))
(defun projectile-ignored-rel-p (file directory patterns)
"Check if FILE should be ignored relative to DIRECTORY according to PATTERNS."
(let ((default-directory directory))
(-any? (lambda (pattern)
(or (s-ends-with? (s-chop-suffix "/" pattern)
(s-chop-suffix "/" file))
(member file (file-expand-wildcards pattern t))))
patterns)))
(defun projectile-ignored-files ()
"Return list of ignored files."
(-map
'projectile-expand-root
(append
projectile-globally-ignored-files
(projectile-project-ignored-files))))
(defun projectile-ignored-directories ()
"Return list of ignored directories."
(-map
'file-name-as-directory
(-map
'projectile-expand-root
(append
projectile-globally-ignored-directories
(projectile-project-ignored-directories)))))
(defun projectile-ignored-directories-rel ()
"Return list of ignored directories, relative to the root."
(--map (s-chop-prefix (projectile-project-root) it) (projectile-ignored-directories)))
(defun projectile-ignored-files-rel ()
"Return list of ignored files, relative to the root."
(--map (s-chop-prefix (projectile-project-root) it) (projectile-ignored-files)))
(defun projectile-project-ignored-files ()
"Return list of project ignored files."
(-remove 'file-directory-p (projectile-project-ignored)))
(defun projectile-project-ignored-directories ()
"Return list of project ignored directories."
(-filter 'file-directory-p (projectile-project-ignored)))
(defun projectile-paths-to-ignore ()
"Return a list of ignored project paths."
(-map (lambda (pattern)
(s-chop-prefix "/" pattern))
(-filter (lambda (pattern)
(s-starts-with? "/" pattern))
(cdr (projectile-parse-dirconfig-file)))))
(defun projectile-patterns-to-ignore ()
"Return a list of relative file patterns."
(-remove (lambda (pattern)
(s-starts-with? "/" pattern))
(cdr (projectile-parse-dirconfig-file))))
(defun projectile-project-ignored ()
"Return list of project ignored files/directories."
(let ((paths (projectile-paths-to-ignore))
(default-directory (projectile-project-root)))
(apply 'append
(-map
(lambda (pattern)
(file-expand-wildcards pattern t))
paths))))
(defun projectile-dirconfig-file ()
"Return the absolute path to the project's dirconfig file."
(expand-file-name ".projectile" (projectile-project-root)))
(defun projectile-parse-dirconfig-file ()
"Parse project ignore file and return directories to ignore and keep.
The return value will be a cons, the car being the list of
directories to keep, and the cdr being the list of files or
directories to ignore.
Strings starting with + will be added to the list of directories
to keep, and strings starting with - will be added to the list of
directories to ignore. For backward compatibility, without a
prefix the string will be assumed to be an ignore string."
(let ((dirconfig-file (projectile-dirconfig-file)))
(when (file-exists-p dirconfig-file)
(with-temp-buffer
(insert-file-contents-literally dirconfig-file)
(let* ((split-string-default-separators "[\r\n]")
(strings (-map 's-trim (delete "" (split-string (buffer-string)))))
(separated-vals (--separate (s-starts-with? "+" it) strings)))
(cons (-map 'projectile-strip-dir-prefix (car separated-vals))
(-map 'projectile-strip-dir-prefix (cadr separated-vals))))))))
(defun projectile-strip-dir-prefix (dir)
"Strip + or - prefix from DIR."
(s-chop-prefixes '("-" "+") dir))
(defun projectile-expand-root (name)
"Expand NAME to project root.
Never use on many files since it's going to recalculate the
project-root for every file."
(expand-file-name name (projectile-project-root)))
(defun projectile-completing-read (prompt choices)
"Present a project tailored PROMPT with CHOICES."
(let ((prompt (projectile-prepend-project-name prompt)))
(cond
((eq projectile-completion-system 'ido)
(ido-completing-read prompt choices))
((eq projectile-completion-system 'default)
(completing-read prompt choices))
((eq projectile-completion-system 'grizzl)
(if (and (fboundp 'grizzl-completing-read)
(fboundp 'grizzl-make-index))
(grizzl-completing-read prompt (grizzl-make-index choices))
(user-error "Please install grizzl from \
https://github.com/d11wtq/grizzl")))
(t (funcall projectile-completion-system prompt choices)))))
(defun projectile-current-project-files ()
"Return a list of files for the current project."
(let ((files (and projectile-enable-caching
(gethash (projectile-project-root) projectile-projects-cache))))
;; nothing is cached
(unless files
(setq files (-mapcat 'projectile-dir-files
(projectile-get-project-directories)))
;; cache the resulting list of files
(when projectile-enable-caching
(projectile-cache-project (projectile-project-root) files)))
files))
(defun projectile-current-project-dirs ()
"Return a list of dirs for the current project."
(-remove 'null (-distinct
(-map 'file-name-directory
(projectile-current-project-files)))))
(defun projectile-hash-keys (hash)
"Return a list of all HASH keys."
(let (allkeys)
(maphash (lambda (k v) (setq allkeys (cons k allkeys))) hash)
allkeys))
;;; Interactive commands
(defun projectile-find-file (&optional arg)
"Jump to a project's file using completion.
With a prefix ARG invalidates the cache first."
(interactive "P")
(when arg
(projectile-invalidate-cache nil))
(let ((file (projectile-completing-read "Find file: "
(projectile-current-project-files))))
(find-file (expand-file-name file (projectile-project-root)))
(run-hooks 'projectile-find-file-hook)))
(defun projectile-find-dir (arg)
"Jump to a project's directory using completion.
With a prefix ARG invalidates the cache first."
(interactive "P")
(when arg
(projectile-invalidate-cache nil))
(let ((dir (projectile-completing-read "Find dir: "
(projectile-current-project-dirs))))
(dired (expand-file-name dir (projectile-project-root)))
(run-hooks 'projectile-find-dir-hook)))
(defun projectile-find-test-file (arg)
"Jump to a project's test file using completion.
With a prefix ARG invalidates the cache first."
(interactive "P")
(when arg
(projectile-invalidate-cache nil))
(let ((file (projectile-completing-read "Find test file: "
(projectile-current-project-test-files))))
(find-file (expand-file-name file (projectile-project-root)))))
(defcustom projectile-test-files-suffices '("_test" "_spec" "Test" "-test")
"Some common suffices of test files."
:group 'projectile
:type '(repeat string))
(defun projectile-test-files (files)
"Return only the test FILES."
(-filter 'projectile-test-file-p files))
(defun projectile-test-file-p (file)
"Check if FILE is a test file."
(-any? (lambda (suffix)
(s-ends-with? suffix (file-name-sans-extension file)))
projectile-test-files-suffices))
(defun projectile-current-project-test-files ()
"Return a list of test files for the current project."
(projectile-test-files (projectile-current-project-files)))
(defvar projectile-rails-rspec '("Gemfile" "app" "lib" "db" "config" "spec"))
(defvar projectile-rails-test '("Gemfile" "app" "lib" "db" "config" "test"))
(defvar projectile-symfony '("composer.json" "app" "src" "vendor"))
(defvar projectile-ruby-rspec '("Gemfile" "lib" "spec"))
(defvar projectile-ruby-test '("Gemfile" "lib" "test"))
(defvar projectile-maven '("pom.xml"))
(defvar projectile-lein '("project.clj"))
(defvar projectile-rebar '("rebar"))
(defvar projectile-make '("Makefile"))
(defun projectile-project-type ()
"Determine the project's type based on its structure."
(let ((project-root (projectile-project-root)))
(cond
((projectile-verify-files projectile-rails-rspec) 'rails-rspec)
((projectile-verify-files projectile-rails-test) 'rails-test)
((projectile-verify-files projectile-ruby-rspec) 'ruby-rspec)
((projectile-verify-files projectile-ruby-test) 'ruby-test)
((projectile-verify-files projectile-symfony) 'symfony)
((projectile-verify-files projectile-maven) 'maven)
((projectile-verify-files projectile-lein) 'lein)
((projectile-verify-files projectile-rebar) 'rebar)
((projectile-verify-files projectile-make) 'make)
(t 'generic))))
(defun projectile-verify-files (files)
"Check whether all FILES exist in the current project."
(-all? 'projectile-verify-file files))
(defun projectile-verify-file (file)
"Check whether FILE exists in the current project."
(file-exists-p (projectile-expand-root file)))
(defun projectile-project-vcs ()
"Determine the VCS used by the project if any."
(let ((project-root (projectile-project-root)))
(cond
((file-exists-p (expand-file-name ".git" project-root)) 'git)
((file-exists-p (expand-file-name ".hg" project-root)) 'hg)
((file-exists-p (expand-file-name ".fossil" project-root)) 'fossil)
((file-exists-p (expand-file-name ".bzr" project-root)) 'bzr)
((file-exists-p (expand-file-name "_darcs" project-root)) 'darcs)
((file-exists-p (expand-file-name ".svn" project-root)) 'svn)
((locate-dominating-file project-root ".git") 'git)
((locate-dominating-file project-root ".hg") 'hg)
((locate-dominating-file project-root ".fossil") 'fossil)
((locate-dominating-file project-root ".bzr") 'bzr)
((locate-dominating-file project-root "_darcs") 'darcs)
((locate-dominating-file project-root ".svn") 'svn)
(t 'none))))
(defun projectile-toggle-between-implemenation-and-test ()
"Toggle between an implementation file and its test file."
(interactive)
(if (projectile-test-file-p (buffer-file-name))
;; find the matching impl file
(let ((impl-file (projectile-find-matching-file (buffer-file-name))))
(if impl-file
(find-file (projectile-expand-root impl-file))
(error "No matching source file found")))
;; find the matching test file
(let ((test-file (projectile-find-matching-test (buffer-file-name))))
(if test-file
(find-file (projectile-expand-root test-file))
(error "No matching test file found")))))
(defun projectile-test-suffix (project-type)
"Find test files suffix based on PROJECT-TYPE."
(cond
((member project-type '(rails-rspec ruby-rspec)) "_spec")
((member project-type '(rails-test ruby-test lein)) "_test")
((member project-type '(maven symfony)) "Test")
(t (error "Project type not supported!"))))
(defun projectile-find-matching-test (file)
"Compute the name of the test matching FILE."
(let ((basename (file-name-nondirectory (file-name-sans-extension file)))
(extension (file-name-extension file))
(test-suffix (projectile-test-suffix (projectile-project-type))))
(-first (lambda (current-file)
(s-equals? (file-name-nondirectory (file-name-sans-extension current-file))
(concat basename test-suffix)))
(projectile-current-project-files))))
(defun projectile-find-matching-file (test-file)
"Compute the name of a file matching TEST-FILE."
(let ((basename (file-name-nondirectory (file-name-sans-extension test-file)))
(extension (file-name-extension test-file))
(test-suffix (projectile-test-suffix (projectile-project-type))))
(-first (lambda (current-file)
(s-equals? (concat (file-name-nondirectory (file-name-sans-extension current-file)) test-suffix)
basename))
(projectile-current-project-files))))
(defun projectile-grep ()
"Perform rgrep in the project."
(interactive)
(let ((roots (projectile-get-project-directories))
(search-regexp (if (and transient-mark-mode mark-active)
(buffer-substring (region-beginning) (region-end))
(read-string (projectile-prepend-project-name "Grep for: ")
(projectile-symbol-at-point)))))
(dolist (root-dir roots)
(require 'grep)
;; paths for find-grep should relative and without trailing /
(let ((grep-find-ignored-directories (-union (-map (lambda (dir) (s-chop-suffix "/" (s-chop-prefix root-dir dir)))
(cdr (projectile-ignored-directories))) grep-find-ignored-directories))
(grep-find-ignored-files (-union (-map (lambda (file) (s-chop-prefix root-dir file)) (projectile-ignored-files)) grep-find-ignored-files)))
(grep-compute-defaults)
(rgrep search-regexp "* .*" root-dir)))))
(defun projectile-ack ()
"Run an `ack-and-a-half' search in the project."
(interactive)
(let ((ack-and-a-half-arguments
(-map
(lambda (path)
(concat "--ignore-dir=" (file-name-nondirectory (directory-file-name path))))
(projectile-ignored-directories))))
(call-interactively projectile-ack-function)))
(defun projectile-tags-exclude-patterns ()
"Return a string with exclude patterns for ctags."
(mapconcat (lambda (pattern) (format "--exclude=%s" pattern))
(projectile-project-ignored-directories) " "))
(defun projectile-regenerate-tags ()
"Regenerate the project's etags."
(interactive)
(let* ((project-root (projectile-project-root))
(tags-exclude (projectile-tags-exclude-patterns))
(default-directory project-root))
(shell-command (format projectile-tags-command tags-exclude project-root))
(visit-tags-table project-root)))
(defun projectile-files-in-project-directory (directory)
"Return a list of files in DIRECTORY."
(let ((dir (s-chop-prefix (projectile-project-root) (expand-file-name directory))))
(-filter (lambda (file) (s-starts-with-p dir file))
(projectile-current-project-files))))
(defun projectile-replace (arg)
"Replace a string in the project using `tags-query-replace'.
With a prefix argument ARG prompts you for a directory on which to run the replacement."
(interactive "P")
(let* ((old-text (read-string
(projectile-prepend-project-name "Replace: ")
(projectile-symbol-at-point)))
(new-text (read-string
(projectile-prepend-project-name
(format "Replace %s with: " old-text))))
(files (if arg
(-map 'projectile-expand-root (projectile-files-in-project-directory (read-directory-name "Replace in directory: ")))
(-map 'projectile-expand-root (projectile-current-project-files)))))
;; we have to reject directories as a workaround to work with git submodules
(tags-query-replace old-text new-text nil '(-reject 'file-directory-p files))))
(defun projectile-symbol-at-point ()
"Get the symbol at point and strip its properties."
(substring-no-properties (or (thing-at-point 'symbol) "")))
(defun projectile-kill-buffers ()
"Kill all project buffers."
(interactive)
(let* ((buffers (projectile-project-buffer-names))
(question
(format
"Are you sure you want to kill %d buffer(s) for '%s'? "
(length buffers)
(projectile-project-name))))
(if (yes-or-no-p question)
(mapc 'kill-buffer buffers))))
(defun projectile-dired ()
"Opens dired at the root of the project."
(interactive)
(dired (projectile-project-root)))
(defun projectile-recentf ()
"Show a list of recently visited files in a project."
(interactive)
(if (boundp 'recentf-list)
(find-file (projectile-expand-root (projectile-completing-read "Recently visited files: " (projectile-recentf-files)))))
(message "recentf is not enabled"))
(defun projectile-recentf-files ()
"Return a list of recently visited files in a project."
(if (boundp 'recentf-list)
(let ((project-root (projectile-project-root)))
(->> recentf-list
(-filter (lambda (file) (s-starts-with-p project-root file)))
(-map (lambda (file) (s-chop-prefix project-root file)))))
nil))
(defun projectile-serialize-cache ()
"Serializes the memory cache to the hard drive."
(projectile-serialize projectile-projects-cache projectile-cache-file))
(defvar projectile-rails-compile-cmd "bundle exec rails server")
(defvar projectile-ruby-compile-cmd "bundle exec rake build")
(defvar projectile-ruby-test-cmd "bundle exec rake test")
(defvar projectile-ruby-rspec-cmd "bundle exec rspec")
(defvar projectile-symfony-compile-cmd "app/console server:run")
(defvar projectile-symfony-test-cmd "phpunit -c app ")
(defvar projectile-maven-compile-cmd "mvn clean install")
(defvar projectile-maven-test-cmd "mvn test")
(defvar projectile-lein-compile-cmd "lein compile")
(defvar projectile-lein-test-cmd "lein test")
(defvar projectile-rebar-compile-cmd "rebar")
(defvar projectile-rebar-test-cmd "rebar eunit")
(defvar projectile-make-compile-cmd "make")
(defvar projectile-make-test-cmd "make test")
(defvar projectile-compilation-cmd-map
(make-hash-table :test 'equal)
"A mapping between projects and the last compilation command used on them.")
(defvar projectile-test-cmd-map
(make-hash-table :test 'equal)
"A mapping between projects and the last test command used on them.")
(defun projectile-default-compilation-command (project-type)
"Retrieve default compilation command for PROJECT-TYPE."
(cond
((member project-type '(rails-rspec rails-test)) projectile-rails-compile-cmd)
((member project-type '(ruby-rspec ruby-test)) projectile-ruby-compile-cmd)
((eq project-type 'symfony) projectile-symfony-compile-cmd)
((eq project-type 'lein) projectile-lein-compile-cmd)
((eq project-type 'make) projectile-make-compile-cmd)
((eq project-type 'rebar) projectile-rebar-compile-cmd)
((eq project-type 'maven) projectile-maven-compile-cmd)
(t projectile-make-compile-cmd)))
(defun projectile-default-test-command (project-type)
"Retrieve default test command for PROJECT-TYPE."
(cond
((member project-type '(rails-rspec ruby-rspec)) projectile-ruby-rspec-cmd)
((member project-type '(rails-test ruby-test)) projectile-ruby-test-cmd)
((eq project-type 'symfony) projectile-symfony-test-cmd)
((eq project-type 'lein) projectile-lein-test-cmd)
((eq project-type 'make) projectile-make-test-cmd)
((eq project-type 'rebar) projectile-rebar-test-cmd)
((eq project-type 'maven) projectile-maven-test-cmd)
(t projectile-make-test-cmd)))
(defun projectile-compilation-command (project)
"Retrieve the compilation command for PROJECT."
(or (gethash project projectile-compilation-cmd-map)
(projectile-default-compilation-command (projectile-project-type))))
(defun projectile-test-command (project)
"Retrieve the test command for PROJECT."
(or (gethash project projectile-test-cmd-map)
(projectile-default-test-command (projectile-project-type))))
(defun projectile-compile-project (arg)
"Run project compilation command.
Normally you'll be prompted for a compilation command, unless
variable `compilation-read-command'. You can force the prompt
with a prefix ARG."
(interactive "P")
(let* ((project-root (projectile-project-root))
(default-cmd (projectile-compilation-command project-root))
(compilation-cmd (if (or compilation-read-command arg)
(compilation-read-command default-cmd)
default-cmd))
(default-directory project-root))
(puthash project-root compilation-cmd projectile-compilation-cmd-map)
(compilation-start compilation-cmd)))
;; TODO - factor this duplication out
(defun projectile-test-project ()
"Run project test command."
(interactive)
(let* ((project-root (projectile-project-root))
(test-cmd (compilation-read-command (projectile-test-command project-root)))
(default-directory project-root))
(puthash project-root test-cmd projectile-test-cmd-map)
(compilation-start test-cmd)))
(defun projectile-relevant-known-projects ()
"Return a list of known projects except the current one (if present)."
(if (projectile-project-p)
(-difference projectile-known-projects
(list (abbreviate-file-name (projectile-project-root))))
projectile-known-projects))
(defun projectile-switch-project ()