forked from bbatsov/projectile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.el
2685 lines (2324 loc) · 110 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 -*- lexical-binding: t -*-
;; Copyright © 2011-2014 Bozhidar Batsov <[email protected]>
;; Author: Bozhidar Batsov <[email protected]>
;; URL: https://github.com/bbatsov/projectile
;; Keywords: project, convenience
;; Version: 0.11.0
;; Package-Requires: ((s "1.6.0") (f "0.17.1") (dash "1.5.0") (pkg-info "0.4"))
;; 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 'f)
(require 'dash)
(require 'grep) ; For `rgrep'
(require 'pkg-info) ; For `pkg-info-version-info'
(require 'ibuffer)
(require 'ibuf-ext)
(eval-when-compile
(defvar ack-and-a-half-arguments)
(defvar ggtags-completion-table)
(defvar tags-completion-table))
(declare-function ack-and-a-half "ack-and-a-half")
(declare-function ggtags-ensure-project "ggtags")
(declare-function ggtags-update-tags "ggtags")
(declare-function tags-completion-table "etags")
;;;; Compatibility
(eval-and-compile
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
`(progn
(defvar ,var ,val ,docstring)
(make-variable-buffer-local ',var)))))
;;; 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-file-exists-local-cache-expire nil
"Number of seconds before file existence cache expires for a
file on a local file system.
A value of nil disables this cache."
:group 'projectile
:type '(choice (const :tag "Disabled" nil)
(integer :tag "Seconds")))
(defcustom projectile-file-exists-remote-cache-expire (* 5 60)
"Number of seconds before file existence cache expires for a
file on a remote file system such as tramp.
A value of nil disables this cache."
:group 'projectile
:type '(choice (const :tag "Disabled" nil)
(integer :tag "Seconds")))
(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 helm 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-file-name "TAGS"
"The tags filename Projectile's going to use."
:group 'projectile
:type 'string)
(defcustom projectile-tags-command "ctags -Re -f %s %s"
"The command Projectile's going to use to generate a TAGS file."
:group 'projectile
:type 'string)
(defcustom projectile-sort-order 'default
"The sort order used for a project's files."
:group 'projectile
:type 'symbol
:options '(default recentf recently-active access-time modification-time))
(defcustom projectile-verbose t
"Echo messages that are not errors."
:group 'projectile
:type 'boolean)
(defcustom projectile-buffers-filter-function nil
"A function used to filter the buffers in `projectile-project-buffers'.
The function should accept and return a list of Emacs buffers.
Two example filter functions are shipped by default - `projectile-buffers-with-file'
and `projectile-buffers-with-file-or-process'."
:group 'projectile
:type 'symbol)
(defcustom projectile-project-root-files
'("rebar.config" ; Rebar project file
"project.clj" ; Leiningen project file
"SConstruct" ; Scons project file
"pom.xml" ; Maven project file
"build.sbt" ; SBT project file
"build.gradle" ; Gradle project file
"Gemfile" ; Bundler file
"requirements.txt" ; Pip file
"tox.ini" ; Tox file
"package.json" ; npm package file
"gulpfile.js" ; Gulp build file
"Gruntfile.js" ; Grunt project file
"bower.json" ; Bower project file
"composer.json" ; Composer project file
"Cargo.toml" ; Cargo project file
"mix.exs" ; Elixir mix project file
)
"A list of files considered to mark the root of a project."
:group 'projectile
:type '(repeat string))
(defcustom projectile-project-root-files-top-down-recurring
'(".svn" ; Svn VCS root dir
"CVS" ; Csv VCS root dir
)
"A list of files considered to mark the root of a project.
This root files pattern stops at the parentmost match."
:group 'projectile
:type '(repeat string))
(defcustom projectile-project-root-files-bottom-up
'(".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
)
"A list of files considered to mark the root of a project.
This root files pattern overrides discovery of any root files
pattern that would have found a project root in a subdirectory."
:group 'projectile
:type '(repeat string))
(defcustom projectile-project-root-files-functions
'(projectile-root-bottom-up
projectile-root-top-down
projectile-root-top-down-recurring)
"A list of functions for finding project roots."
:group 'projectile
:type '(repeat function))
(defcustom projectile-globally-ignored-files
(list projectile-tags-file-name)
"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"
".tox"
".svn"
"build")
"A list of directories globally ignored by projectile."
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-ignored-modes
'("erc-mode"
"help-mode"
"completion-list-mode"
"Buffer-menu-mode"
"gnus-.*-mode"
"occur-mode")
"A list of regular expressions for major modes ignored by projectile.
If a buffer is using a given major mode, projectile will ignore
it for functions working with buffers."
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-ignored-buffers nil
"A list of buffer-names ignored by projectile.
If a buffer is in the list projectile will ignore
it for functions working with buffers."
:group 'projectile
:type '(repeat string)
:package-version '(projectile . "0.12.0"))
(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)
(defcustom projectile-find-dir-includes-top-level nil
"If true, add top-level dir to options offered by `projectile-find-dir'."
:group 'projectile
:type 'boolean)
(defcustom projectile-use-git-grep nil
"If true, use `vc-git-grep' in git projects."
:group 'projectile
:type 'boolean)
(defcustom projectile-remember-window-configs nil
"If true, restore the last window configuration when switching projects.
If no configuration exists, just run `projectile-switch-project-action' as usual."
:group 'projectile
:type 'boolean)
;;; Idle Timer
(defvar projectile-idle-timer nil
"The timer object created when `project-enable-idle-timer' is non-nil.")
(defcustom projectile-idle-timer-seconds 30
"The idle period to use when `projectile-enable-idle-timer' is non-nil."
:group 'projectile
:type 'number)
(defcustom projectile-idle-timer-hook '(projectile-regenerate-tags)
"The hook run when `projectile-enable-idle-timer' is non-nil."
:group 'projectile
:type '(repeat symbol))
(defcustom projectile-enable-idle-timer nil
"Enables idle timer hook `projectile-idle-timer-functions'.
When `projectile-enable-idle-timer' is non-nil, the hook
`projectile-idle-timer-hook' is run each time Emacs has been idle
for `projectile-idle-timer-seconds' seconds and we're in a
project."
:group 'projectile
:set (lambda (symbol value)
(set symbol value)
(when projectile-idle-timer
(cancel-timer projectile-idle-timer))
(setq projectile-idle-timer nil)
(when projectile-enable-idle-timer
(setq projectile-idle-timer (run-with-idle-timer
projectile-idle-timer-seconds t
(lambda ()
(when (projectile-project-p)
(run-hooks 'projectile-idle-timer-hook)))))))
:type 'boolean)
;;; 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 (let (print-length) (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 nil
"A hashmap used to cache project file names to speed up related operations.")
(defvar projectile-project-root-cache (make-hash-table :test 'equal)
"Cached value of function `projectile-project-root`.")
(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.")
(defvar projectile-known-projects-on-file nil
"List of known projects reference point.
Contains a copy of `projectile-known-projects' when it was last
synchronized with `projectile-known-projects-file'.")
(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)
(defcustom projectile-ignored-projects nil
"A list of projects not to be added to `projectile-known-projects'."
:group 'projectile
:type 'list
:package-version '(projectile . "0.11.0"))
;;; Version information
(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 t))
(let ((version (pkg-info-version-info 'projectile)))
(when show-version
(message "Projectile version: %s" version))
version))
;;; Caching
(defvar projectile-file-exists-cache
(make-hash-table :test 'equal)
"Cached `projectile-file-exists-p' results.")
(defvar projectile-file-exists-cache-timer nil
"Timer for scheduling`projectile-file-exists-cache-cleanup'.")
(defun projectile-file-exists-cache-cleanup ()
"Removed timed out cache entries and reschedules or remove the
timer if no more items are in the cache."
(let ((now (current-time)))
(maphash (lambda (key value)
(if (time-less-p (cdr value) now)
(remhash key projectile-file-exists-cache)))
projectile-file-exists-cache)
(setq projectile-file-exists-cache-timer
(if (> (hash-table-count projectile-file-exists-cache) 0)
(run-with-timer 10 nil 'projectile-file-exists-cache-cleanup)))))
(defun projectile-file-exists-p (filename)
"Return t if file FILENAME exist.
A wrapper around `file-exists-p' with additional caching support."
(let* ((file-remote (file-remote-p filename))
(expire-seconds
(if file-remote
(and projectile-file-exists-remote-cache-expire
(> projectile-file-exists-remote-cache-expire 0)
projectile-file-exists-remote-cache-expire)
(and projectile-file-exists-local-cache-expire
(> projectile-file-exists-local-cache-expire 0)
projectile-file-exists-local-cache-expire)))
(remote-file-name-inhibit-cache (if expire-seconds
expire-seconds
remote-file-name-inhibit-cache)))
(if (not expire-seconds)
(file-exists-p filename)
(let* ((current-time (current-time))
(cached (gethash filename projectile-file-exists-cache))
(cached-value (if cached (car cached)))
(cached-expire (if cached (cdr cached)))
(cached-expired (if cached (time-less-p cached-expire current-time) t))
(value (or (and (not cached-expired) cached-value)
(if (file-exists-p filename) 'found 'notfound))))
(when (or (not cached) cached-expired)
(puthash filename
(cons value (time-add current-time (seconds-to-time expire-seconds)))
projectile-file-exists-cache))
(unless projectile-file-exists-cache-timer
(setq projectile-file-exists-cache-timer
(run-with-timer 10 nil 'projectile-file-exists-cache-cleanup)))
(equal value 'found)))))
(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))))
(setq projectile-project-root-cache (make-hash-table :test 'equal))
(remhash project-root projectile-projects-cache)
(projectile-serialize-cache)
(when projectile-verbose
(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-purge-file-from-cache (file)
"Purge FILE from the cache of the current project."
(interactive
(list (projectile-completing-read
"Remove file from cache: "
(projectile-current-project-files))))
(let* ((project-root (projectile-project-root))
(project-cache (gethash project-root projectile-projects-cache)))
(if (projectile-file-cached-p file project-root)
(progn
(puthash project-root (remove file project-cache) projectile-projects-cache)
(projectile-serialize-cache)
(when projectile-verbose
(message "%s removed from cache" file)))
(error "%s is not in the cache" file))))
(defun projectile-purge-dir-from-cache (dir)
"Purge DIR from the cache of the current project."
(interactive
(list (projectile-completing-read
"Remove directory from cache: "
(projectile-current-project-dirs))))
(let* ((project-root (projectile-project-root))
(project-cache (gethash project-root projectile-projects-cache)))
(puthash project-root
(-filter (lambda (file)
(s-starts-with-p dir file))
project-cache)
projectile-projects-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))
(abs-current-file (buffer-file-name (current-buffer)))
(current-file (file-relative-name abs-current-file current-project)))
(if (gethash (projectile-project-root) projectile-projects-cache)
(unless (or (projectile-file-cached-p current-file current-project)
(projectile-ignored-directory-p (file-name-directory abs-current-file))
(projectile-ignored-file-p abs-current-file))
(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."
(propertize current-file 'face 'font-lock-keyword-face)
(propertize current-project 'face 'font-lock-keyword-face)))
(message "Empty cache. Projectile is initializing cache...")
(projectile-current-project-files))))
;; 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-enable-caching (projectile-project-p))
(projectile-cache-current-file)))
(defun projectile-cache-projects-find-file-hook ()
"Function for caching projects with `find-file-hook'."
(when (projectile-project-p)
(let ((known-projects (and (sequencep projectile-known-projects)
(copy-sequence projectile-known-projects))))
(projectile-add-known-project (projectile-project-root))
(unless (equal known-projects projectile-known-projects)
(projectile-merge-known-projects)))))
(defun projectile-maybe-invalidate-cache (force)
"Invalidate if FORCE or project's dirconfig newer than cache."
(when (or force (file-newer-than-file-p (projectile-dirconfig-file)
projectile-cache-file))
(projectile-invalidate-cache nil)))
;;; Window configurations
(defvar projectile-window-config-map
(make-hash-table :test 'equal)
"A mapping from project names to their latest window configurations.")
(defun projectile-save-window-config (project-name)
"Save PROJECT-NAME's configuration to `projectile-window-config-map'."
(puthash project-name (current-window-configuration) projectile-window-config-map))
(defun projectile-get-window-config (project-name)
"Return the window configuration corresponding to PROJECT-NAME.
If no such window configuration exists,
returns nil."
(gethash project-name projectile-window-config-map))
(defun projectile-restore-window-config (project-name)
"Restore the window configuration corresponding to the PROJECT-NAME.
Returns nil if no window configuration was found"
(let ((window-config (projectile-get-window-config project-name)))
(when window-config
(set-window-configuration window-config))))
(defadvice delete-file (before purge-from-projectile-cache (filename &optional trash))
(if (and projectile-enable-caching (projectile-project-p))
(let* ((project-root (projectile-project-root))
(true-filename (file-truename filename))
(relative-filename (file-relative-name true-filename project-root)))
(if (projectile-file-cached-p relative-filename project-root)
(projectile-purge-file-from-cache relative-filename)))))
;;; Project root related utilities
(defun projectile-parent (path)
"Return the parent directory of PATH.
PATH may be a file or directory and directory paths may end with a slash."
(directory-file-name (file-name-directory (directory-file-name (expand-file-name path)))))
(defun projectile-locate-dominating-file (file name)
"Look up the directory hierarchy from FILE for a directory containing NAME.
Stop at the first parent directory containing a file NAME,
and return the directory. Return nil if not found.
Instead of a string, NAME can also be a predicate taking one argument
\(a directory) and returning a non-nil value if that directory is the one for
which we're looking."
;; copied from files.el (stripped comments) emacs-24 bzr branch 2014-03-28 10:20
(setq file (abbreviate-file-name file))
(let ((root nil)
try)
(while (not (or root
(null file)
(string-match locate-dominating-stop-dir-regexp file)))
(setq try (if (stringp name)
(projectile-file-exists-p (expand-file-name name file))
(funcall name file)))
(cond (try (setq root file))
((equal file (setq file (file-name-directory
(directory-file-name file))))
(setq file nil))))
(if root (file-name-as-directory root))))
(defun projectile-root-bottom-up (dir &optional list)
"Identify a project root in DIR by looking at `projectile-project-root-files-bottom-up'.
Returns a project root directory path or nil if not found."
(--reduce-from
(or acc
(projectile-locate-dominating-file dir it))
nil
(or list projectile-project-root-files-bottom-up)))
(defun projectile-root-top-down (dir &optional list)
"Identify a project root in DIR by looking at `projectile-project-root-files'.
Returns a project root directory path or nil if not found."
(projectile-locate-dominating-file
dir
(lambda (dir)
(--first (projectile-file-exists-p (expand-file-name it dir))
(or list projectile-project-root-files)))))
(defun projectile-root-top-down-recurring (dir &optional list)
"Identify a project root in DIR by looking at `projectile-project-root-files-top-down-recurring'.
Returns a project root directory path or nil if not found."
(--reduce-from
(or acc
(projectile-locate-dominating-file
dir
(lambda (dir)
(and (projectile-file-exists-p (expand-file-name it dir))
(or (string-match locate-dominating-stop-dir-regexp (projectile-parent dir))
(not (projectile-file-exists-p (expand-file-name it (projectile-parent dir)))))))))
nil
(or list projectile-project-root-files-top-down-recurring)))
(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."
(file-truename
(let ((dir (file-truename default-directory)))
(or (--reduce-from
(or acc
(let* ((cache-key (format "%s-%s" it dir))
(cache-value (gethash cache-key projectile-project-root-cache)))
(if cache-value
(if (eq cache-value 'no-project-root)
nil
cache-value)
(let ((value (funcall it dir)))
(puthash cache-key (or value 'no-project-root) projectile-project-root-cache)
value))))
nil
projectile-project-root-files-functions)
(if projectile-require-project-root
(error "You're not in a project")
default-directory)))))
(defun projectile-file-truename (file-name)
"Return the truename of FILE-NAME.
A thin wrapper around `file-truename' that handles nil."
(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) (file-relative-name file root))
(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)
(file-relative-name (expand-file-name f directory) root))
(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-git-submodule-command "git submodule --quiet foreach 'echo $path' | tr '\\n' '\\0'"
"Command used by projectile to get the files in git submodules."
: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 -R --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 "svn list -R . | grep -v '$/' | tr '\\n' '\\0'"
"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)
(defcustom projectile-get-ext-commands
'((git . projectile-git-command)
(hg . projectile-hg-command)
(fossil . projectile-fossil-command)
(bzr . projectile-bzr-command)
(darcs . projectile-darcs-command)
(svn . projectile-svn-command))
"Determine which external command to invoke based on the project's VCS."
:group 'projectile)
(defun projectile-get-ext-command ()
"Determine which external command to invoke based on the project's VCS."
(let* ((vcs (projectile-project-vcs))
(ext-command (cdr (assoc vcs projectile-get-ext-commands))))
(or ext-command
projectile-generic-command)))
(defun projectile-get-sub-projects-command ()
(let ((vcs (projectile-project-vcs)))
(cond
((eq vcs 'git) projectile-git-submodule-command)
(t ""))))
(defun projectile-get-all-sub-projects (project &optional known-projects)
"Get all sub-projects for a given projects.
PROJECT is base directory to start search recursively.
KNOWN-PROJECTS is all the currently known sub-projects. It is used for testing
already traversed sub-projects.
The function handles also the case that a project has sub-projects that
includes themselves as modules and could create a infinite loop when traverse
the sub-project tree. A sub-project is a another project inside current project
that could also be consider a project when standing on its own. Git submodule
is an example of a sub-project. This function is designed to extend with
more VCS in general, since it uses the function `projectile-get-sub-projects-command'
to return appropriate command for a VCS to list sup-project.
Now, consider a project tree like this:
root/
sub-project1/
sub-project2/
sub-project3/
.... main project's files and directories....
Each such sub-project can also have sub-projects. In the normal
case, we can walk the directory tree all they way down. The problem is,
sub-projects can include itself as a sub-project! To solve this problem,
the function checks that if it encounters existing sub-projects already
processed before (this means the function walks up the directory tree again),
it simply ignores to avoid infinite loop.
An example is `yasnippet' Git project at the time of writing this:
yasnippet/
snippets/ -> submodule
yasmate/ -> submodule
Running \"git submodule\" inside `yasnippet/' returns this result:
16154e1462a8bbb2c5cf48e1101bd3f2c090e0fc snippets (remotes/origin/cleanup_html_tags-7-g16154e1)
0543618bd34a6715918992f01161c118f136bb37 yasmate (0543618)
Running \"git submodule\" inside `yasnippet/yasmate' returns this result:
-993588a35d665427209936618a9e524679480e95 bundles/html-tmbundle
-d0c1ae22d326d310edaf126acf93588b7958f682 bundles/objc-tmbundle
-8091f39a6efd288c8793321e8822a639db3cc940 bundles/rails-tmbundle
-da63813a86d46f17abf0a9303de1149ca7cee60a bundles/ruby-tmbundle
We change into the directory of either of those directory, for example:
cd bundles/ruby-tmbundle
Running \"git submodule\" any of those submodule returns this result:
-993588a35d665427209936618a9e524679480e95 ../html-tmbundle
-d0c1ae22d326d310edaf126acf93588b7958f682 ../objc-tmbundle
-8091f39a6efd288c8793321e8822a639db3cc940 ../rails-tmbundle
-da63813a86d46f17abf0a9303de1149ca7cee60a ../ruby-tmbundle
So, each of those modules is point to itself! We must only check to avoid
looping at a single point. The sub-project listing command should be able
to handle such case."
(let* ((default-directory project)
;; search for sub-projects under current project `project'
(submodules (mapcar
(lambda (s)
(file-name-as-directory (expand-file-name s default-directory)))
(projectile-files-via-ext-command (projectile-get-sub-projects-command)))))
(cond
((null submodules)
nil)
(t
(nconc submodules (-flatten
;; recursively get sub-projects of each sub-project
(mapcar (lambda (s)
(projectile-get-all-sub-projects s submodules)) submodules)))))))
(defun projectile-get-sub-projects-files ()
"Get files from sub-projects recursively."
(-flatten
(mapcar (lambda (s)
(let ((default-directory s))
(mapcar (lambda (f)
(concat s f))
(projectile-files-via-ext-command projectile-git-command))))
(condition-case nil
(projectile-get-all-sub-projects (projectile-project-root))
(error nil)))))
(defun projectile-get-repo-files ()
"Get a list of the files in the project, including sub-projects."
(cond
((eq (projectile-project-vcs) 'git)
(nconc (projectile-files-via-ext-command (projectile-get-ext-command))
(projectile-get-sub-projects-files)))
(t (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-buffers-with-file (buffers)
"Return only those BUFFERS backed by files."
(--filter (buffer-file-name it) buffers))
(defun projectile-buffers-with-file-or-process (buffers)
"Return only those BUFFERS backed by files or processes."
(--filter (or (buffer-file-name it)
(get-buffer-process it)) buffers))
(defun projectile-project-buffers ()
"Get a list of project buffers."
(let* ((project-root (projectile-project-root))
(all-buffers (-filter (lambda (buffer)
(projectile-project-buffer-p buffer project-root))
(buffer-list))))
(if projectile-buffers-filter-function
(funcall projectile-buffers-filter-function all-buffers)
all-buffers)))
(defun projectile-process-current-project-buffers (action)
"Process the current project's buffers using ACTION."
(let ((project-buffers (projectile-project-buffers)))
(dolist (buffer project-buffers)
(funcall action buffer))))
(defun projectile-project-buffer-files ()
"Get a list of project buffer files."
(let ((project-root (projectile-project-root)))
(->> (projectile-buffers-with-file (projectile-project-buffers))
(-map (lambda (buffer)
(file-relative-name (buffer-file-name buffer) project-root))))))
(defun projectile-project-buffer-p (buffer project-root)
"Check if BUFFER is under PROJECT-ROOT."
(with-current-buffer buffer
(and (not (s-starts-with? " " (buffer-name buffer)))
(not (projectile-ignored-buffer-p buffer))
(s-equals? (file-remote-p default-directory) (file-remote-p project-root))
(not (s-matches? "^http\\(s\\)?://" default-directory))
(s-starts-with? project-root (file-truename default-directory)))))
(defun projectile-ignored-buffer-p (buffer)
"Check if BUFFER should be ignored."
(or
(member (buffer-name buffer) projectile-globally-ignored-buffers)
(with-current-buffer buffer
(--any-p (s-matches? (concat "^" it "$")
(symbol-name major-mode))
projectile-globally-ignored-modes))))
(defun projectile-recently-active-files ()
"Get list of recently active files.
Files are ordered by recently active buffers, and then recently
opened through use of recentf."
(let ((project-buffer-files (projectile-project-buffer-files)))
(append project-buffer-files