2017-06-08 11:47:56 +02:00
|
|
|
;;; core-projects.el -*- lexical-binding: t; -*-
|
2017-02-08 02:02:51 -05:00
|
|
|
|
2019-03-28 18:27:45 -04:00
|
|
|
(defvar doom-projectile-cache-limit 25000
|
|
|
|
"If any project cache surpasses this many files it is purged when quitting
|
|
|
|
Emacs.")
|
|
|
|
|
|
|
|
(defvar doom-projectile-cache-blacklist '("~" "/tmp" "/")
|
|
|
|
"Directories that should never be cached.")
|
|
|
|
|
|
|
|
(defvar doom-projectile-cache-purge-non-projects nil
|
|
|
|
"If non-nil, non-projects are purged from the cache on `kill-emacs-hook'.")
|
|
|
|
|
2019-04-01 12:18:12 +05:30
|
|
|
(defvar doom-projectile-fd-binary "fd"
|
|
|
|
"name of `fd-find' executable binary")
|
2019-03-28 18:27:45 -04:00
|
|
|
|
|
|
|
;;
|
|
|
|
;;; Packages
|
|
|
|
|
2017-06-08 11:47:56 +02:00
|
|
|
(def-package! projectile
|
2019-03-16 14:20:07 -04:00
|
|
|
:after-call (after-find-file dired-before-readin-hook minibuffer-setup-hook)
|
2019-04-20 15:29:34 -04:00
|
|
|
:commands (projectile-project-root
|
|
|
|
projectile-project-name
|
|
|
|
projectile-project-p
|
|
|
|
projectile-add-known-project) ; TODO PR autoload upstream
|
2017-10-08 13:09:57 +02:00
|
|
|
:init
|
2017-02-19 18:09:26 -05:00
|
|
|
(setq projectile-cache-file (concat doom-cache-dir "projectile.cache")
|
2017-01-31 19:50:11 -05:00
|
|
|
projectile-enable-caching (not noninteractive)
|
2017-02-19 18:09:26 -05:00
|
|
|
projectile-known-projects-file (concat doom-cache-dir "projectile.projects")
|
2018-09-28 14:09:48 -04:00
|
|
|
projectile-require-project-root t
|
2017-09-20 01:39:15 +02:00
|
|
|
projectile-globally-ignored-files '(".DS_Store" "Icon
" "TAGS")
|
2018-05-07 18:14:04 +02:00
|
|
|
projectile-globally-ignored-file-suffixes '(".elc" ".pyc" ".o")
|
2018-12-02 02:02:56 -05:00
|
|
|
projectile-ignored-projects '("~/" "/tmp")
|
2019-03-28 18:27:45 -04:00
|
|
|
projectile-kill-buffers-filter 'kill-only-files
|
2019-05-12 22:12:08 -04:00
|
|
|
projectile-files-cache-expire 604800 ; expire after a week
|
|
|
|
projectile-sort-order 'recentf
|
|
|
|
projectile-use-git-grep t) ; use git-grep for text searches
|
2017-09-20 01:39:15 +02:00
|
|
|
|
2017-10-08 13:09:57 +02:00
|
|
|
:config
|
2017-12-31 11:09:36 -05:00
|
|
|
(add-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook)
|
2019-03-16 14:20:07 -04:00
|
|
|
(projectile-mode +1)
|
2017-12-31 11:09:36 -05:00
|
|
|
|
2019-03-10 08:12:04 -04:00
|
|
|
(global-set-key [remap evil-jump-to-tag] #'projectile-find-tag)
|
|
|
|
(global-set-key [remap find-tag] #'projectile-find-tag)
|
2019-03-09 02:42:03 -05:00
|
|
|
|
2017-09-20 01:39:15 +02:00
|
|
|
;; a more generic project root file
|
2017-09-19 15:08:24 +02:00
|
|
|
(push ".project" projectile-project-root-files-bottom-up)
|
2019-04-08 23:01:30 -04:00
|
|
|
(push (abbreviate-file-name doom-local-dir) projectile-globally-ignored-directories)
|
2017-09-20 01:39:15 +02:00
|
|
|
|
2019-03-28 18:27:45 -04:00
|
|
|
;; Accidentally indexing big directories like $HOME or / will massively bloat
|
|
|
|
;; projectile's cache (into the hundreds of MBs). This purges those entries
|
|
|
|
;; when exiting Emacs to prevent slowdowns/freezing when cache files are
|
|
|
|
;; loaded or written to.
|
|
|
|
(defun doom|cleanup-project-cache ()
|
|
|
|
"Purge projectile cache entries that:
|
|
|
|
|
|
|
|
a) have too many files (see `doom-projectile-cache-limit'),
|
2019-04-08 23:02:50 -04:00
|
|
|
b) represent blacklisted directories that are too big, change too often or are
|
2019-03-28 18:27:45 -04:00
|
|
|
private. (see `doom-projectile-cache-blacklist'),
|
|
|
|
c) are not valid projectile projects."
|
2019-04-08 23:02:50 -04:00
|
|
|
(when (bound-and-true-p projectile-projects-cache)
|
|
|
|
(cl-loop with blacklist = (mapcar #'file-truename doom-projectile-cache-blacklist)
|
|
|
|
for proot in (hash-table-keys projectile-projects-cache)
|
2019-05-06 17:12:28 -04:00
|
|
|
if (or (not (stringp proot))
|
|
|
|
(>= (length (gethash proot projectile-projects-cache))
|
|
|
|
doom-projectile-cache-limit)
|
2019-04-08 23:02:50 -04:00
|
|
|
(member (substring proot 0 -1) blacklist)
|
|
|
|
(and doom-projectile-cache-purge-non-projects
|
|
|
|
(not (doom-project-p proot))))
|
|
|
|
do (doom-log "Removed %S from projectile cache" proot)
|
|
|
|
and do (remhash proot projectile-projects-cache)
|
|
|
|
and do (remhash proot projectile-projects-cache-time)
|
|
|
|
and do (remhash proot projectile-project-type-cache))
|
|
|
|
(projectile-serialize-cache)))
|
2019-04-24 18:04:17 -04:00
|
|
|
(unless noninteractive
|
|
|
|
(add-hook 'kill-emacs-hook #'doom|cleanup-project-cache))
|
2019-03-28 18:27:45 -04:00
|
|
|
|
2018-08-26 12:19:47 +02:00
|
|
|
;; It breaks projectile's project root resolution if HOME is a project (e.g.
|
|
|
|
;; it's a git repo). In that case, we disable bottom-up root searching to
|
2018-09-27 23:04:16 -04:00
|
|
|
;; prevent issues. This makes project resolution a little slower and less
|
|
|
|
;; accurate in some cases.
|
2018-08-26 12:19:47 +02:00
|
|
|
(let ((default-directory "~"))
|
|
|
|
(when (cl-find-if #'projectile-file-exists-p
|
|
|
|
projectile-project-root-files-bottom-up)
|
|
|
|
(message "HOME appears to be a project. Disabling bottom-up root search.")
|
|
|
|
(setq projectile-project-root-files
|
|
|
|
(append projectile-project-root-files-bottom-up
|
|
|
|
projectile-project-root-files)
|
|
|
|
projectile-project-root-files-bottom-up nil)))
|
|
|
|
|
2017-07-26 18:42:33 +02:00
|
|
|
;; Projectile root-searching functions can cause an infinite loop on TRAMP
|
|
|
|
;; connections, so disable them.
|
2018-09-28 13:54:20 -04:00
|
|
|
;; TODO Is this still necessary?
|
2018-09-07 21:43:32 -04:00
|
|
|
(defun doom*projectile-locate-dominating-file (orig-fn file name)
|
2017-09-20 01:39:15 +02:00
|
|
|
"Don't traverse the file system if on a remote connection."
|
2019-03-28 16:43:04 -04:00
|
|
|
(when (and (stringp file)
|
|
|
|
(not (file-remote-p file)))
|
2018-09-07 21:43:32 -04:00
|
|
|
(funcall orig-fn file name)))
|
2018-12-04 20:09:44 +11:00
|
|
|
(advice-add #'projectile-locate-dominating-file :around #'doom*projectile-locate-dominating-file)
|
|
|
|
|
2019-05-12 22:12:04 -04:00
|
|
|
(cond
|
|
|
|
;; If fd exists, use it for git and generic projects fd is a rust program
|
|
|
|
;; that is significantly faster. It also respects .gitignore. This is
|
|
|
|
;; recommended in the projectile docs
|
|
|
|
((executable-find doom-projectile-fd-binary)
|
2019-04-01 12:18:12 +05:30
|
|
|
(setq projectile-git-command (concat
|
|
|
|
doom-projectile-fd-binary
|
|
|
|
" . --type f -0 -H -E .git")
|
2019-05-12 22:12:04 -04:00
|
|
|
projectile-generic-command projectile-git-command))
|
|
|
|
|
|
|
|
;; Otherwise, resort to ripgrep, which is also faster than find.
|
|
|
|
((executable-find "rg")
|
|
|
|
(setq projectile-generic-command
|
|
|
|
(concat "rg -0 --files --color=never --hidden"
|
|
|
|
(cl-loop for dir in projectile-globally-ignored-directories
|
|
|
|
concat (format " --glob '!%s'" dir))))
|
|
|
|
(when IS-WINDOWS
|
|
|
|
(setq projectile-indexing-method 'alien
|
|
|
|
projectile-enable-caching nil)))))
|
2017-01-31 19:50:11 -05:00
|
|
|
|
2017-03-02 18:16:52 -05:00
|
|
|
;;
|
2018-09-07 19:36:16 -04:00
|
|
|
;; Project-based minor modes
|
2017-03-02 18:16:52 -05:00
|
|
|
|
2017-12-31 11:21:53 -05:00
|
|
|
(defvar doom-project-hook nil
|
|
|
|
"Hook run when a project is enabled. The name of the project's mode and its
|
|
|
|
state are passed in.")
|
2017-03-02 18:16:52 -05:00
|
|
|
|
2018-06-16 11:34:19 +02:00
|
|
|
(cl-defmacro def-project-mode! (name &key
|
|
|
|
modes
|
|
|
|
files
|
|
|
|
when
|
|
|
|
match
|
|
|
|
add-hooks
|
|
|
|
on-load
|
|
|
|
on-enter
|
|
|
|
on-exit)
|
2017-09-20 01:39:15 +02:00
|
|
|
"Define a project minor-mode named NAME (a symbol) and declare where and how
|
|
|
|
it is activated. Project modes allow you to configure 'sub-modes' for
|
2019-03-02 01:12:48 -05:00
|
|
|
major-modes that are specific to a folder, project structure, framework or
|
|
|
|
whatever arbitrary context you define. These project modes can have their own
|
|
|
|
settings, keymaps, hooks, snippets, etc.
|
2017-03-02 18:16:52 -05:00
|
|
|
|
|
|
|
This creates NAME-hook and NAME-map as well.
|
|
|
|
|
2017-12-31 11:21:53 -05:00
|
|
|
A project can be enabled through .dir-locals.el too, by setting `doom-project'.
|
2017-03-02 18:16:52 -05:00
|
|
|
|
2017-06-16 14:34:28 +02:00
|
|
|
PLIST may contain any of these properties, which are all checked to see if NAME
|
|
|
|
should be activated. If they are *all* true, NAME is activated.
|
2017-03-02 18:16:52 -05:00
|
|
|
|
|
|
|
:modes MODES -- if buffers are derived from MODES (one or a list of symbols).
|
|
|
|
|
2017-06-16 14:34:28 +02:00
|
|
|
:files FILES -- if project contains FILES; takes a string or a form comprised
|
|
|
|
of nested (and ...) and/or (or ...) forms. Each path is relative to the
|
2017-03-15 22:42:05 -04:00
|
|
|
project root, however, if prefixed with a '.' or '..', it is relative to the
|
|
|
|
current buffer.
|
2017-03-02 18:16:52 -05:00
|
|
|
|
|
|
|
:match REGEXP -- if file name matches REGEXP
|
|
|
|
|
|
|
|
:when PREDICATE -- if PREDICATE returns true (can be a form or the symbol of a
|
|
|
|
function)
|
|
|
|
|
2017-10-04 17:42:37 +02:00
|
|
|
:add-hooks HOOKS -- HOOKS is a list of hooks to add this mode's hook.
|
|
|
|
|
|
|
|
:on-load FORM -- FORM to run the first time this project mode is enabled.
|
|
|
|
|
|
|
|
:on-enter FORM -- FORM is run each time the mode is activated.
|
2017-06-16 14:34:28 +02:00
|
|
|
|
2017-10-04 17:42:37 +02:00
|
|
|
:on-exit FORM -- FORM is run each time the mode is disabled.
|
2017-10-03 03:02:53 +02:00
|
|
|
|
2017-06-16 14:34:28 +02:00
|
|
|
Relevant: `doom-project-hook'."
|
2018-06-16 11:34:19 +02:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((init-var (intern (format "%s-init" name))))
|
2017-03-02 18:16:52 -05:00
|
|
|
`(progn
|
2018-06-16 11:34:19 +02:00
|
|
|
,(if on-load `(defvar ,init-var nil))
|
2017-03-02 18:16:52 -05:00
|
|
|
(define-minor-mode ,name
|
2018-06-16 11:34:19 +02:00
|
|
|
"A project minor mode generated by `def-project-mode!'."
|
2017-03-02 18:16:52 -05:00
|
|
|
:init-value nil
|
2017-10-04 17:42:37 +02:00
|
|
|
:lighter ""
|
|
|
|
:keymap (make-sparse-keymap)
|
|
|
|
(if (not ,name)
|
2018-06-16 11:34:19 +02:00
|
|
|
,on-exit
|
2017-12-31 11:21:53 -05:00
|
|
|
(run-hook-with-args 'doom-project-hook ',name ,name)
|
2018-06-16 11:34:19 +02:00
|
|
|
,(when on-load
|
2017-10-04 17:42:37 +02:00
|
|
|
`(unless ,init-var
|
2018-06-16 11:34:19 +02:00
|
|
|
,on-load
|
2017-10-04 17:42:37 +02:00
|
|
|
(setq ,init-var t)))
|
2018-06-16 11:34:19 +02:00
|
|
|
,on-enter))
|
2018-08-10 20:37:12 +02:00
|
|
|
,@(cl-loop for hook in add-hooks
|
|
|
|
collect `(add-hook ',(intern (format "%s-hook" name))
|
|
|
|
#',hook))
|
2017-03-02 18:16:52 -05:00
|
|
|
,(when (or modes match files when)
|
|
|
|
`(associate! ,name
|
|
|
|
:modes ,modes
|
|
|
|
:match ,match
|
|
|
|
:files ,files
|
2017-10-04 17:42:37 +02:00
|
|
|
:when ,when)))))
|
2017-01-31 19:50:11 -05:00
|
|
|
|
|
|
|
(provide 'core-projects)
|
|
|
|
;;; core-projects.el ends here
|