forked from bbatsov/projectile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectile.el
3549 lines (3099 loc) · 143 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-2016 Bozhidar Batsov <[email protected]>
;; Author: Bozhidar Batsov <[email protected]>
;; URL: https://github.com/bbatsov/projectile
;; Keywords: project, convenience
;; Version: 0.15.0-cvs
;; Package-Requires: ((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 'cl-lib)
(require 'thingatpt)
(require 'ibuffer)
(require 'ibuf-ext)
(require 'compile)
(require 'grep)
(eval-when-compile
(defvar ag-ignore-list)
(defvar ggtags-completion-table)
(defvar tags-completion-table)
(defvar tags-loop-scan)
(defvar tags-loop-operate)
(defvar eshell-buffer-name)
(defvar explicit-shell-file-name))
(declare-function ggtags-ensure-project "ggtags")
(declare-function ggtags-update-tags "ggtags")
(declare-function pkg-info-version-info "pkg-info")
(declare-function tags-completion-table "etags")
(declare-function make-term "term")
(declare-function term-mode "term")
(declare-function term-char-mode "term")
(defvar grep-files-aliases)
(defvar grep-find-ignored-directories)
(defvar grep-find-ignored-files)
;;;; Compatibility
(eval-and-compile
;; Added in Emacs 24.3.
(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))))
;; Added in Emacs 24.4
(unless (fboundp 'string-suffix-p)
(defun string-suffix-p (suffix string &optional ignore-case)
"Return non-nil if SUFFIX is a suffix of STRING.
If IGNORE-CASE is non-nil, the comparison is done without paying
attention to case differences."
(let ((start-pos (- (length string) (length suffix))))
(and (>= start-pos 0)
(eq t (compare-strings suffix nil nil
string start-pos nil ignore-case))))))
;; Improved (no more stack overflows) in Emacs 24.5
(eval-after-load 'etags
'(when (< emacs-major-version 25)
(defvar etags--table-line-limit 500)
(defun etags-tags-completion-table ()
(let ((table (make-vector 511 0))
(progress-reporter
(make-progress-reporter
(format "Making tags completion table for %s..." buffer-file-name)
(point-min) (point-max))))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(if (not (re-search-forward
"[\f\t\n\r()=,; ]?\177\\\(?:\\([^\n\001]+\\)\001\\)?"
(+ (point) etags--table-line-limit) t))
(forward-line 1)
(intern (prog1 (if (match-beginning 1)
(buffer-substring (match-beginning 1) (match-end 1))
(goto-char (match-beginning 0))
(skip-chars-backward "^\f\t\n\r()=,; ")
(prog1
(buffer-substring (point) (match-beginning 0))
(goto-char (match-end 0))))
(progress-reporter-update progress-reporter (point)))
table))))
table)))))
(defun projectile-trim-string (string)
"Remove whitespace at the beginning and end of STRING."
(replace-regexp-in-string
"[ \n\r]+\\'"
""
(replace-regexp-in-string
"\\`[ \n\r]+"
""
string)))
;;; Customization
(defgroup projectile nil
"Manage and navigate projects easily."
:group 'tools
:group 'convenience
:link '(url-link :tag "Github" "https://github.com/bbatsov/projectile")
:link '(url-link :tag "Online Manual" "https://projectile.readthedocs.org")
:link '(emacs-commentary-link :tag "Commentary" "projectile"))
(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). Its advantage is that it is portable and will
work everywhere that Emacs does. Its disadvantage is that it 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 '(radio
(const :tag "Native" native)
(const :tag "Alien" 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 '(radio
(const :tag "Ido" ido)
(const :tag "Grizzl" grizzl)
(const :tag "Helm" helm)
(const :tag "Ivy" ivy)
(const :tag "Default" default)
(function :tag "Custom function")))
(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-tags-backend 'auto
"The tag backend that Projectile should use.
If set to 'auto', `projectile-find-tag' will automatically choose
which backend to use. Preference order is ggtags -> xref
-> etags-select -> `find-tag'. Variable can also be set to specify which
backend to use. If selected backend is unavailable, fall back to
`find-tag'.
If this variable is set to 'auto' and ggtags is available, or if
set to 'ggtags', then ggtags will be used for
`projectile-regenerate-tags'. For all other settings
`projectile-tags-command' will be used."
:group 'projectile
:type '(radio
(const :tag "auto" auto)
(const :tag "xref" xref)
(const :tag "ggtags" ggtags)
(const :tag "etags" etags-select)
(const :tag "standard" find-tag))
:package-version '(projectile . "0.14.0"))
(defcustom projectile-sort-order 'default
"The sort order used for a project's files."
:group 'projectile
:type '(radio
(const :tag "default" default)
(const :tag "recentf" recentf)
(const :tag "recently active" recently-active)
(const :tag "access time" access-time)
(const :tag "modification 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 'function)
(defcustom projectile-project-name nil
"If this value is non-nil, it will be used as project name.
It has precedence over function `projectile-project-name-function'."
:group 'projectile
:type 'string
:package-version '(projectile . "0.14.0"))
(defcustom projectile-project-name-function 'projectile-default-project-name
"A function that receives the project-root and returns the project name.
If variable `projectile-project-name' is non-nil, this function will not be used."
:group 'projectile
:type 'function
:package-version '(projectile . "0.14.0"))
(defcustom projectile-project-root-files
'("rebar.config" ; Rebar project file
"project.clj" ; Leiningen project file
"build.boot" ; Boot-clj project file
"SConstruct" ; Scons project file
"pom.xml" ; Maven project file
"build.sbt" ; SBT project file
"gradlew" ; Gradle wrapper script
"build.gradle" ; Gradle project file
".ensime" ; Ensime configuration file
"Gemfile" ; Bundler file
"requirements.txt" ; Pip file
"setup.py" ; Setuptools file
"tox.ini" ; Tox 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
"stack.yaml" ; Haskell's stack tool based project
"info.rkt" ; Racket package description file
"DESCRIPTION" ; R package description file
"TAGS" ; etags/ctags are usually in the root of project
"GTAGS" ; GNU Global tags
)
"A list of files considered to mark the root of a project.
The topmost match has precedence."
: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
"_FOSSIL_" ; Fossil VCS root DB on Windows
".bzr" ; Bazaar VCS root dir
"_darcs" ; Darcs VCS root dir
)
"A list of files considered to mark the root of a project.
The bottommost (parentmost) match has precedence."
:group 'projectile
:type '(repeat string))
(defcustom projectile-project-root-files-top-down-recurring
'(".svn" ; Svn VCS root dir
"CVS" ; Csv VCS root dir
"Makefile")
"A list of files considered to mark the root of a project.
The search starts at the top and descends down till a directory
that contains a match file but its parent does not. Thus, it's a
bottommost match in the topmost sequence of directories
containing a root file."
:group 'projectile
:type '(repeat string))
(defcustom projectile-project-root-files-functions
'(projectile-root-local
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-unignored-files nil
"A list of files globally unignored by projectile."
:group 'projectile
:type '(repeat string)
:package-version '(projectile . "0.14.0"))
(defcustom projectile-globally-ignored-file-suffixes
nil
"A list of file suffixes globally ignored by projectile."
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-ignored-directories
'(".idea"
".ensime_cache"
".eunit"
".git"
".hg"
".fslckout"
"_FOSSIL_"
".bzr"
"_darcs"
".tox"
".svn"
".stack-work")
"A list of directories globally ignored by projectile."
:safe (lambda (x) (not (remq t (mapcar #'stringp x))))
:group 'projectile
:type '(repeat string))
(defcustom projectile-globally-unignored-directories nil
"A list of directories globally unignored by projectile."
:group 'projectile
:type '(repeat string)
:package-version '(projectile . "0.14.0"))
(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 'function)
(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-grep-finished-hook nil
"Hooks run when `projectile-grep' finishes."
:group 'projectile
:type 'hook
:package-version '(projectile . "0.14.0"))
(defcustom projectile-test-prefix-function 'projectile-test-prefix
"Function to find test files prefix based on PROJECT-TYPE."
:group 'projectile
:type 'function)
(defcustom projectile-test-suffix-function 'projectile-test-suffix
"Function to find test files suffix based on PROJECT-TYPE."
:group 'projectile
:type 'function)
;;; Idle Timer
(defvar projectile-idle-timer nil
"The timer object created when `projectile-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."
(with-demoted-errors
"Error during file deserialization: %S"
(when (file-exists-p filename)
(with-temp-buffer
(insert-file-contents filename)
;; this will blow up if the contents of the file aren't
;; lisp data structures
(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-project-type-cache (make-hash-table :test 'equal)
"A hashmap used to cache project type 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.
See also `projectile-remove-known-project',
`projectile-cleanup-known-projects' and `projectile-clear-known-projects'.")
(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 '(repeat :tag "Project list" directory)
:package-version '(projectile . "0.11.0"))
(defcustom projectile-ignored-project-function nil
"Function to decide if a project is added to `projectile-known-projects'.
Can be either nil, or a function that takes the truename of the
project root as argument and returns non-nil if the project is to
be ignored or nil otherwise.
This function is only called if the project is not listed in
`projectile-ignored-projects'.
A suitable candidate would be `file-remote-p' to ignore remote
projects."
:group 'projectile
:type '(choice
(const :tag "Nothing" nil)
(const :tag "Remote files" file-remote-p)
function)
:package-version '(projectile . "0.13.0"))
(defcustom projectile-track-known-projects-automatically t
"Controls whether Projectile will automatically register known projects.
When set to nil you'll have always add projects explicitly with
`projectile-add-known-project'."
:group 'projectile
:type 'boolean
:package-version '(projectile . "0.15.0"))
;;; Version information
;;;###autoload
(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))
(if (require 'pkg-info nil t)
(let ((version (pkg-info-version-info 'projectile)))
(when show-version
(message "Projectile %s" version))
version)
(error "Cannot determine version without package pkg-info")))
;;; 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)))))
;;;###autoload
(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-project-type-cache)
(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))))
(when (fboundp 'recentf-cleanup)
(recentf-cleanup)))
(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)))
;;;###autoload
(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))))
;;;###autoload
(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
(cl-remove-if (lambda (str) (string-prefix-p dir str)) 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)))
;;;###autoload
(defun projectile-cache-current-file ()
"Add the currently visited file to the cache."
(interactive)
(let ((current-project (projectile-project-root)))
(when (and (buffer-file-name) (gethash (projectile-project-root) projectile-projects-cache))
(let* ((abs-current-file (file-truename (buffer-file-name)))
(current-file (file-relative-name abs-current-file current-project)))
(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)))))))
;; 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'."
(let ((project-root (projectile-project-p)))
(when (and projectile-enable-caching
project-root
(not (projectile-ignored-project-p project-root)))
(projectile-cache-current-file))))
(defun projectile-track-known-projects-find-file-hook ()
"Function for caching projects with `find-file-hook'."
(when (and projectile-track-known-projects-automatically (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)))
;;;###autoload
(defun projectile-discover-projects-in-directory (directory)
"Discover any projects in DIRECTORY and add them to the projectile cache.
This function is not recursive and only adds projects with roots
at the top level of DIRECTORY."
(interactive
(list (read-directory-name "Starting directory: ")))
(let ((subdirs (directory-files directory t)))
(mapcar
(lambda (dir)
(when (and (file-directory-p dir)
(not (member (file-name-nondirectory dir) '(".." "."))))
(let ((default-directory dir))
(when (projectile-project-p)
(projectile-add-known-project (projectile-project-root))))))
subdirs)))
(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))))
(and root (expand-file-name (file-name-as-directory root)))))
(defvar-local projectile-project-root nil
"Defines a custom Projectile project root.
This is intended to be used as a file local variable.")
(defun projectile-root-local (_dir)
"A simple wrapper around `projectile-project-root'."
projectile-project-root)
(defun projectile-root-top-down (dir &optional list)
"Identify a project root in DIR by top-down search for files in LIST.
If LIST is nil, use `projectile-project-root-files' instead.
Return the first (topmost) matched directory or nil if not found."
(projectile-locate-dominating-file
dir
(lambda (dir)
(cl-find-if (lambda (f) (projectile-file-exists-p (expand-file-name f dir)))
(or list projectile-project-root-files)))))
(defun projectile-root-bottom-up (dir &optional list)
"Identify a project root in DIR by bottom-up search for files in LIST.
If LIST is nil, use `projectile-project-root-files-bottom-up' instead.
Return the first (bottommost) matched directory or nil if not found."
(cl-some (lambda (name) (projectile-locate-dominating-file dir name))
(or list projectile-project-root-files-bottom-up)))
(defun projectile-root-top-down-recurring (dir &optional list)
"Identify a project root in DIR by recurring top-down search for files in LIST.
If LIST is nil, use `projectile-project-root-files-top-down-recurring'
instead. Return the last (bottommost) matched directory in the
topmost sequence of matched directories. Nil otherwise."
(cl-some
(lambda (f)
(projectile-locate-dominating-file
dir
(lambda (dir)
(and (projectile-file-exists-p (expand-file-name f dir))
(or (string-match locate-dominating-stop-dir-regexp (projectile-parent dir))
(not (projectile-file-exists-p (expand-file-name f (projectile-parent dir)))))))))
(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."
(let ((dir default-directory))
(or (cl-some
(lambda (func)
(let* ((cache-key (format "%s-%s" func dir))
(cache-value (gethash cache-key projectile-project-root-cache)))
(if (and cache-value (file-exists-p cache-value))
cache-value
(let ((value (funcall func (file-truename dir))))
(puthash cache-key value projectile-project-root-cache)
value))))
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-default-project-name (project-root)
"Default function used create project name to be displayed based on the value of PROJECT-ROOT."
(file-name-nondirectory (directory-file-name project-root)))
(defvar-local projectile-cached-project-name nil
"Cached name of the current Projectile project. If non-nil, it
is used as the return value of `projectile-project-name' for
performance (unless the variable `projectile-project-name' is
also set). If nil, it is recalculated the next time
`projectile-project-name' is called.
This variable is reset automatically when Projectile detects that
the `buffer-file-name' has changed. It can also be reset manually
by calling `projectile-reset-cached-project-name'.")
(defvar-local projectile-cached-buffer-file-name nil
"The last known value of `buffer-file-name' for the current
buffer. This is used to detect a change in `buffer-file-name',
which triggers a reset of `projectile-cached-project-name'.")
(defun projectile-reset-cached-project-name ()
"Reset the value of `projectile-cached-project-name' to nil.
This means that it is automatically recalculated the next time
function `projectile-project-name' is called."
(interactive)
(setq projectile-cached-project-name nil))
(defun projectile-project-name ()
"Return project name."
(or projectile-project-name
(and (equal projectile-cached-buffer-file-name buffer-file-name)
projectile-cached-project-name)
(progn
(setq projectile-cached-buffer-file-name buffer-file-name)
(setq projectile-cached-project-name
(let ((project-root
(condition-case nil
(projectile-project-root)
(error nil))))
(if project-root
(funcall projectile-project-name-function project-root)
"-"))))))
;;; Project indexing
(defun projectile-get-project-directories ()
"Get the list of project directories that are of interest to the user."
(mapcar (lambda (subdir) (concat (projectile-project-root) subdir))
(or (nth 0 (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-adjust-files (projectile-dir-files-external root directory))))))
(defun projectile-dir-files-native (root directory)
"Get the files for ROOT under DIRECTORY using just Emacs Lisp."
(let ((progress-reporter
(make-progress-reporter
(format "Projectile is indexing %s"
(propertize directory 'face 'font-lock-keyword-face)))))
;; we need the files with paths relative to the project root
(mapcar (lambda (file) (file-relative-name file root))
(projectile-index-directory directory (projectile-filtering-patterns)
progress-reporter))))
(defun projectile-dir-files-external (root directory)
"Get the files for ROOT under DIRECTORY using external tools."
(let ((default-directory directory))
(mapcar (lambda (file)
(file-relative-name (expand-file-name file directory) root))
(projectile-get-repo-files))))
(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-git-ignored-command "git ls-files -zcoi --exclude-standard"
"Command used by projectile to get the ignored files in a git project."
:group 'projectile
:type 'string
:package-version '(projectile . "0.14.0"))
(defcustom projectile-hg-command "hg locate -f -0 -I ."
"Command used by projectile to get the files in a hg project."
:group 'projectile
:type 'string)
(defcustom projectile-fossil-command (concat "fossil ls | "
(when (string-equal system-type
"windows-nt")
"dos2unix | ")