2018-06-16 11:42:37 +02:00
|
|
|
;;; core/autoload/projects.el -*- lexical-binding: t; -*-
|
|
|
|
|
2019-04-11 15:15:05 -04:00
|
|
|
(defvar projectile-project-root nil)
|
|
|
|
|
2019-07-21 15:47:14 +02:00
|
|
|
;;;###autoload (autoload 'projectile-relevant-known-projects "projectile")
|
2019-03-02 01:08:56 -05:00
|
|
|
|
2019-06-27 17:16:40 +02:00
|
|
|
;;;###autodef
|
|
|
|
(cl-defun set-project-type! (name &key predicate compile run test configure dir)
|
|
|
|
"Add a project type to `projectile-project-type'."
|
|
|
|
(declare (indent 1))
|
|
|
|
(after! projectile
|
|
|
|
(add-to-list 'projectile-project-types
|
|
|
|
(list name
|
2019-07-09 01:04:36 +02:00
|
|
|
'marker-files predicate
|
2019-06-27 17:16:40 +02:00
|
|
|
'compilation-dir dir
|
|
|
|
'configure-command configure
|
|
|
|
'compile-command compile
|
|
|
|
'test-command test
|
|
|
|
'run-command run))))
|
|
|
|
|
2019-03-02 01:08:56 -05:00
|
|
|
|
2018-06-16 11:42:37 +02:00
|
|
|
;;
|
2019-06-27 17:16:40 +02:00
|
|
|
;;; Macros
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-08-14 22:36:19 -04:00
|
|
|
(defmacro project-file-exists-p! (files)
|
2018-06-16 11:42:37 +02:00
|
|
|
"Checks if the project has the specified FILES.
|
|
|
|
Paths are relative to the project root, unless they start with ./ or ../ (in
|
|
|
|
which case they're relative to `default-directory'). If they start with a slash,
|
|
|
|
they are absolute."
|
2019-08-14 22:36:19 -04:00
|
|
|
`(file-exists-p! ,files (doom-project-root)))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2019-06-27 17:16:40 +02:00
|
|
|
;;; Commands
|
2018-06-16 11:42:37 +02:00
|
|
|
|
2019-02-26 13:21:16 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/find-file-in-other-project (project-root)
|
|
|
|
"Preforms `projectile-find-file' in a known project of your choosing."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(completing-read "Find file in project: " (projectile-relevant-known-projects)
|
|
|
|
nil nil nil nil (doom-project-root))))
|
|
|
|
(unless (file-directory-p project-root)
|
|
|
|
(error "Project directory '%s' doesn't exist" project-root))
|
|
|
|
(doom-project-find-file project-root))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom/browse-in-other-project (project-root)
|
|
|
|
"Preforms `find-file' in a known project of your choosing."
|
|
|
|
(interactive
|
|
|
|
(list
|
|
|
|
(completing-read "Browse in project: " (projectile-relevant-known-projects)
|
|
|
|
nil nil nil nil (doom-project-root))))
|
|
|
|
(unless (file-directory-p project-root)
|
|
|
|
(error "Project directory '%s' doesn't exist" project-root))
|
|
|
|
(doom-project-browse project-root))
|
|
|
|
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;
|
2019-06-27 17:16:40 +02:00
|
|
|
;;; Library
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-04-09 03:13:18 -04:00
|
|
|
(defun doom-project-p (&optional dir)
|
|
|
|
"Return t if DIR (defaults to `default-directory') is a valid project."
|
|
|
|
(and (doom-project-root dir)
|
|
|
|
t))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2019-04-09 03:13:18 -04:00
|
|
|
(defun doom-project-root (&optional dir)
|
|
|
|
"Return the project root of DIR (defaults to `default-directory').
|
|
|
|
Returns nil if not in a project."
|
|
|
|
(let ((projectile-project-root (unless dir projectile-project-root))
|
|
|
|
projectile-require-project-root)
|
|
|
|
(projectile-project-root dir)))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2018-09-28 13:54:20 -04:00
|
|
|
(defun doom-project-name (&optional dir)
|
2019-04-09 03:13:18 -04:00
|
|
|
"Return the name of the current project.
|
|
|
|
|
|
|
|
Returns '-' if not in a valid project."
|
2019-08-06 14:50:42 -04:00
|
|
|
(if-let (project-root (or (doom-project-root dir)
|
|
|
|
(if dir (expand-file-name dir))))
|
2019-04-09 03:13:18 -04:00
|
|
|
(funcall projectile-project-name-function project-root)
|
|
|
|
"-"))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2018-09-28 13:54:20 -04:00
|
|
|
(defun doom-project-expand (name &optional dir)
|
|
|
|
"Expand NAME to project root."
|
2019-04-09 03:13:18 -04:00
|
|
|
(expand-file-name name (doom-project-root dir)))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-project-find-file (dir)
|
2019-04-19 17:30:47 -04:00
|
|
|
"Jump to a file in DIR (searched recursively).
|
2019-03-28 15:07:14 -04:00
|
|
|
|
2019-04-19 17:30:47 -04:00
|
|
|
If DIR is not a project, it will be indexed (but not cached)."
|
2019-03-26 03:38:37 -04:00
|
|
|
(unless (file-directory-p dir)
|
|
|
|
(error "Directory %S does not exist" dir))
|
2019-06-17 20:57:29 +02:00
|
|
|
(unless (file-readable-p dir)
|
|
|
|
(error "Directory %S isn't readable" dir))
|
2019-04-19 17:30:47 -04:00
|
|
|
(let* ((default-directory (file-truename (expand-file-name dir)))
|
|
|
|
(project-root (doom-project-root default-directory))
|
|
|
|
(projectile-project-root default-directory)
|
|
|
|
(projectile-enable-caching projectile-enable-caching))
|
|
|
|
(cond ((and project-root (file-equal-p project-root projectile-project-root))
|
|
|
|
(unless (doom-project-p projectile-project-root)
|
|
|
|
;; Disable caching if this is not a real project; caching
|
|
|
|
;; non-projects easily has the potential to inflate the projectile
|
|
|
|
;; cache beyond reason.
|
|
|
|
(setq projectile-enable-caching nil))
|
|
|
|
(call-interactively
|
|
|
|
;; Intentionally avoid `helm-projectile-find-file', because it runs
|
2019-08-06 19:42:05 -04:00
|
|
|
;; asynchronously, and thus doesn't see the lexical
|
|
|
|
;; `default-directory'
|
2019-07-22 22:28:43 +02:00
|
|
|
(if (doom-module-p :completion 'ivy)
|
2019-04-19 17:30:47 -04:00
|
|
|
#'counsel-projectile-find-file
|
|
|
|
#'projectile-find-file)))
|
|
|
|
((fboundp 'counsel-file-jump) ; ivy only
|
|
|
|
(call-interactively #'counsel-file-jump))
|
2019-08-06 19:42:05 -04:00
|
|
|
((and (fboundp 'project-find-file-in) ; emacs 26.1+ only
|
|
|
|
(project-current))
|
|
|
|
(project-find-file-in nil (list default-directory) nil))
|
2019-05-13 00:18:51 -04:00
|
|
|
((fboundp 'helm-find-files)
|
|
|
|
(call-interactively #'helm-find-files))
|
2019-04-19 17:30:47 -04:00
|
|
|
((call-interactively #'find-file)))))
|
2018-06-16 11:42:37 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-project-browse (dir)
|
|
|
|
"Traverse a file structure starting linearly from DIR."
|
2019-03-28 18:29:50 -04:00
|
|
|
(let ((default-directory (file-truename (expand-file-name dir))))
|
2018-06-16 11:42:37 +02:00
|
|
|
(call-interactively
|
2019-07-22 22:28:43 +02:00
|
|
|
(cond ((doom-module-p :completion 'ivy)
|
2019-03-26 03:38:37 -04:00
|
|
|
#'counsel-find-file)
|
2019-07-22 22:28:43 +02:00
|
|
|
((doom-module-p :completion 'helm)
|
2019-03-26 03:38:37 -04:00
|
|
|
#'helm-find-files)
|
|
|
|
(#'find-file)))))
|