doomemacs/core/autoload/projects.el
Henrik Lissner 2dc52bc9be
💥 Replace exec-path-from-shell w/ 'bin/doom env'
IMPORTANT: This is a breaking update for Mac users, as your shell
environment will no longer be inherited correctly (with the removal of
exec-path-from-shell). The quick fix is: 'bin/doom env refresh'. Also,
the set-env! autodef now does nothing (and is deprecated), be sure to
remove calls to it in your config.

Smaller changes:
+ This update also adds --no-* switches to doom quickstart
+ Includes general improvements to the documentation of several bin/doom
  commands.
+ Moves doom/reload* commands to core/autoload/config.el
+ doom/reload-project has been removed (it didn't actually do anything)

The breaking change:
This update adds an "envvar file" to Doom Emacs. This file is generated
by `doom env refresh`, populated with variables scraped from your shell
environment (from both non-interactive and interactive sessions). This
file is then (inexpensively) loaded at startup, if it exists.

+ The file is manually generated with `doom env refresh`.
+ It can be regenerated automatically whenever `doom refresh` is run by
  running `doom env enable` (`doom env clear` will reverse this and
  delete the env file).
+ `doom quickstart` will ask if you want to auto-generate this envvar
  file. You won't need it if you're confident Emacs will always be
  started from the correct environment, however.
+ Your env file can be reloaded from a running Emacs session with `M-x
  doom/reload-env`. Note: this won't work if the Emacs session you're
  running it in doesn't have a correct SHELL set. i.e. don't use this to
  create your first env file!

The idea isn't mine -- it's borrowed from Spacemacs -- and was
introduced to me in #1053 by @yurimx. I was impressed with it. Prior to
this, I was unhappy with exec-path-from-shell (no hate to the dev, I
understand its necessity), and 'doom patch-macos' wasn't ideal for mac
users (needed to be reapplied every time you update Emacs). What's more,
many users (even Linux users) had to install exec-path-from-shell
anyway.

This solution suffers from none of their shortcomings. More reliable
than patch-macos, more performant and complete than
exec-path-from-shell, and easily handled by bin/doom.
2019-03-28 01:56:09 -04:00

100 lines
3.2 KiB
EmacsLisp

;;; core/autoload/projects.el -*- lexical-binding: t; -*-
;;;###autoload
(autoload 'projectile-relevant-known-projects "projectile")
;;
;; Macros
;;;###autoload
(defmacro without-project-cache! (&rest body)
"Run BODY with projectile's project-root cache disabled. This is necessary if
you want to interactive with a project other than the one you're in."
`(let ((projectile-project-root-cache (make-hash-table :test 'equal))
projectile-project-name
projectile-require-project-root)
,@body))
;;;###autoload
(defmacro project-file-exists-p! (files)
"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."
`(file-exists-p! ,files (doom-project-root)))
;;
;; Commands
;;;###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))
;;
;; Library
;;;###autoload
(defalias 'doom-project-p #'projectile-project-p)
;;;###autoload
(defalias 'doom-project-root #'projectile-project-root)
;;;###autoload
(defun doom-project-name (&optional dir)
"Return the name of the current project."
(let ((project-root (or (projectile-project-root dir)
(if dir (expand-file-name dir)))))
(if project-root
(funcall projectile-project-name-function project-root)
"-")))
;;;###autoload
(defun doom-project-expand (name &optional dir)
"Expand NAME to project root."
(expand-file-name name (projectile-project-root dir)))
;;;###autoload
(defun doom-project-find-file (dir)
"Fuzzy-find a file under DIR."
(unless (file-directory-p dir)
(error "Directory %S does not exist" dir))
(let ((default-directory dir)
projectile-project-root)
(call-interactively
;; Intentionally avoid `helm-projectile-find-file', because it runs
;; asynchronously, and thus doesn't see the lexical `default-directory'
(if (featurep! :completion ivy)
#'counsel-projectile-find-file
#'projectile-find-file))))
;;;###autoload
(defun doom-project-browse (dir)
"Traverse a file structure starting linearly from DIR."
(let ((default-directory (file-truename dir)))
(call-interactively
(cond ((featurep! :completion ivy)
#'counsel-find-file)
((featurep! :completion helm)
#'helm-find-files)
(#'find-file)))))