New // naming convention + refactor doom management functions

This commit is contained in:
Henrik Lissner 2017-11-05 01:23:36 +01:00
parent 43a9acec28
commit 95a5b46dc5
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
8 changed files with 170 additions and 169 deletions

View file

@ -181,7 +181,7 @@ startup."
"Ensures that `doom-autoload-file' exists and is loaded. Otherwise run
`doom/reload-autoloads' to generate it."
(unless (file-exists-p doom-autoload-file)
(quiet! (doom/reload-autoloads))))
(quiet! (doom//reload-autoloads))))
(defun doom-initialize-packages (&optional force-p load-p)
"Crawls across your emacs.d to fill `doom-modules' (from init.el) and
@ -191,33 +191,38 @@ If FORCE-P is non-nil, do it even if they are.
This aggressively reloads core autoload files."
(doom-initialize force-p)
(let ((noninteractive t)
(load-prefer-newer t)
(load-fn
(lambda (file &optional noerror)
(condition-case-unless-debug ex
(load file noerror :nomessage :nosuffix)
('error
(error (format "(doom-initialize-packages) %s in %s: %s"
(car ex)
(file-relative-name file doom-emacs-dir)
(error-message-string ex))
:error))))))
(when (or force-p (not doom-modules))
(setq doom-modules nil)
(funcall load-fn (expand-file-name "init.el" doom-emacs-dir))
(when load-p
(with-temp-buffer ; prevent buffer-local settings from propagating
(let ((noninteractive t)
(load-prefer-newer t)
(load-fn
(lambda (file &optional noerror)
(condition-case-unless-debug ex
(load file noerror :nomessage :nosuffix)
('error
(error (format "(doom-initialize-packages) %s in %s: %s"
(car ex)
(file-relative-name file doom-emacs-dir)
(error-message-string ex))
:error))))))
(when (or force-p (not doom-modules))
(setq doom-modules nil)
(let (noninteractive)
(funcall load-fn (doom-module-path :private user-login-name "init.el") t)
(funcall load-fn (expand-file-name "core.el" doom-core-dir)))
(mapc load-fn (file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir))))
(doom|finalize))
(when (or force-p (not doom-packages))
(setq doom-packages nil)
(funcall load-fn (expand-file-name "packages.el" doom-core-dir))
(cl-loop for (module . submodule) in (doom--module-pairs)
for path = (doom-module-path module submodule "packages.el")
do (funcall load-fn path t)))))
(load (concat doom-core-dir "core.el") nil t))
(funcall load-fn (expand-file-name "init.el" doom-emacs-dir))
(when load-p
(let (noninteractive)
(funcall load-fn (doom-module-path :private user-login-name "init.el") t))
(mapc load-fn (file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir)))
(cl-loop for (module . submodule) in (doom--module-pairs)
for path = (doom-module-path module submodule "config.el")
do (funcall load-fn path t))))
(when (or force-p (not doom-packages))
(setq doom-packages nil)
(funcall load-fn (expand-file-name "packages.el" doom-core-dir))
(cl-loop for (module . submodule) in (doom--module-pairs)
for path = (doom-module-path module submodule "packages.el")
do (funcall load-fn path t)))))
(doom|finalize))
(defun doom-initialize-modules (modules)
"Adds MODULES to `doom-modules'. MODULES must be in mplist format.
@ -486,7 +491,15 @@ loads MODULE SUBMODULE's packages.el file."
;; Commands
;;
(defun doom/reload-load-path ()
(defsubst doom--read-cookie-pred (file)
"Returns the value of the ;;;###if predicate form in FILE."
(with-temp-buffer
(insert-file-contents-literally file nil 0 256)
(if (re-search-forward "^;;;###if " nil t)
(eval (sexp-at-point))
t)))
(defun doom//reload-load-path ()
"Reload `load-path' and recompile files (if necessary).
Use this when `load-path' is out of sync with your plugins. This should only
@ -496,20 +509,19 @@ an Emacs session is running.
This isn't necessary if you use Doom's package management commands because they
call `doom/reload-load-path' remotely (through emacsclient)."
(interactive)
(byte-recompile-file (expand-file-name "core.el" doom-core-dir) t)
(cond (noninteractive
(message "Reloading...")
(require 'server)
(unless (ignore-errors (server-eval-at "server" '(doom/reload-load-path)))
(message "Recompiling")
(doom/recompile)))
(when (file-exists-p (if server-use-tcp server-auth-dir server-socket-dir))
(server-eval-at "server" '(doom//reload-load-path))))
(t
(doom-initialize t)
(doom/recompile)
(message "Reloaded %d packages" (length doom--package-load-path))
(run-with-timer 1 nil #'redraw-display)
(run-hooks 'doom-reload-hook))))
(defun doom/reload-autoloads ()
(defun doom//reload-autoloads ()
"Refreshes the autoloads.el file, specified by `doom-autoload-file'.
It scans and reads core/autoload/*.el, modules/*/*/autoload.el and
@ -523,21 +535,20 @@ This should be run whenever init.el or an autoload file is modified. Running
;; This function must not use autoloaded functions or external dependencies.
;; It must assume nothing is set up!
(doom-initialize-packages (not noninteractive))
(let ((evil-p (doom-module-loaded-p :feature 'evil))
(targets
(let ((targets
(file-expand-wildcards
(expand-file-name "autoload/*.el" doom-core-dir))))
(dolist (path (doom--module-paths))
(let ((auto-dir (expand-file-name "autoload" path))
(auto-file (expand-file-name "autoload.el" path)))
(when (file-exists-p auto-file)
(when (and (file-exists-p auto-file)
(doom--read-cookie-pred auto-file))
(push auto-file targets))
(when (file-directory-p auto-dir)
(dolist (file (file-expand-wildcards (expand-file-name "*.el" auto-dir) t))
;; Make evil*.el autoload files a special case; don't load
;; them unless evil is enabled.
(unless (and (string-prefix-p "evil" (file-name-nondirectory file))
(not evil-p))
(when (doom--read-cookie-pred file)
(push file targets))))))
(when (file-exists-p doom-autoload-file)
(delete-file doom-autoload-file)
@ -569,109 +580,108 @@ This should be run whenever init.el or an autoload file is modified. Running
(car ex) (error-message-string ex))))
(kill-buffer buf)))))
(defun doom/compile (&optional lite-p only-recompile-p)
(defun doom//byte-compile (&optional modules recompile-p)
"Byte compiles your emacs configuration.
Specifically, this byte-compiles init.el, core/*.el, core/autoload/*.el &
modules/*/*/**.el. It ignores unit tests and files with `no-byte-compile'
enabled.
init.el is always byte-compiled by this.
DOOM Emacs was designed to benefit from byte-compilation, but the process may
take a while. Also, while your config files are byte-compiled, changes to them
will not take effect! Use `doom/clean-compiled' or `make clean' to undo
byte-compilation.
If MODULES is specified (a list of module strings, e.g. \"lang/php\"), those are
byte-compiled. Otherwise, all enabled modules are byte-compiled, including Doom
core. It always ignores unit tests and files with `no-byte-compile' enabled.
If LITE-P is non-nil, only compile the core DOOM files (init.el & core/**/*.el).
Doom was designed to benefit from byte-compilation, but the process may take a
while. Also, while your config files are byte-compiled, changes to them will not
take effect! Use `doom//clean-byte-compiled-files' or `make clean' to remove
these files.
If ONLY-RECOMPILE-P is non-nil, only recompile out-of-date files."
If RECOMPILE-P is non-nil, only recompile out-of-date files."
(interactive
(list nil current-prefix-arg))
(let ((default-directory doom-emacs-dir)
(recompile-p (or recompile-p
(and (member "-r" command-line-args) t))))
(if (not noninteractive)
(let ((compilation-filter-hook
(list (lambda () (ansi-color-apply-on-region compilation-filter-start (point))))))
(compile (format "%s --batch -l core/core.el -f doom//byte-compile %s %s"
(executable-find "emacs")
(if recompile-p "-r" "")
(if modules (string-join modules " ") ""))))
(let ((total-ok 0)
(total-fail 0)
(total-noop 0)
(modules (or modules command-line-args-left))
compile-targets)
(doom-initialize-packages t t)
(setq compile-targets
(cl-loop for target
in (or modules (append (list doom-core-dir) (doom--module-paths)))
if (equal target "core")
nconc (nreverse (directory-files-recursively doom-core-dir "\\.el$"))
else if (file-directory-p target)
nconc (nreverse (directory-files-recursively target "\\.el$"))
else if (file-directory-p (expand-file-name target doom-modules-dir))
nconc (nreverse (directory-files-recursively (expand-file-name target doom-modules-dir) "\\.el$"))
else if (file-exists-p target)
collect target
finally do (setq command-line-args-left nil)))
(unless compile-targets
(error "No targets to compile"))
(let ((use-package-expand-minimally t))
(push (expand-file-name "init.el" doom-emacs-dir) compile-targets)
(dolist (target compile-targets)
(when (or (not recompile-p)
(let ((elc-file (byte-compile-dest-file target)))
(and (file-exists-p elc-file)
(file-newer-than-file-p file elc-file))))
(let ((result (if (and (string-match-p "/autoload/.*\\.el$" target)
(not (doom--read-cookie-pred target)))
'no-byte-compile
(byte-compile-file target)))
(short-name (file-relative-name target doom-emacs-dir)))
(cl-incf
(cond ((eq result 'no-byte-compile)
(message! (dark (white "Ignored %s" short-name)))
total-noop)
((null result)
(message! (red "Failed to compile %s" short-name))
total-fail)
(t
(message! (green "Compiled %s" short-name))
(quiet! (load target t t))
total-ok))))))
(message!
(bold
(color (if (= total-fail 0) 'green 'red)
"%s %s file(s) %s"
(if recompile-p "Recompiled" "Compiled")
(format "%d/%d" total-ok (- (length compile-targets) total-noop))
(format "(%s ignored)" total-noop)))))))))
(defun doom//byte-compile-core (&optional recompile-p)
""
(interactive "P")
;; Ensure all relevant config files are loaded and up-to-date. This way we
;; don't need eval-when-compile and require blocks scattered all over.
(doom-initialize-packages t noninteractive)
(let ((targets
(cond ((equal (car command-line-args-left) "--")
(cl-loop for file in (cdr command-line-args-left)
if (file-exists-p file)
collect (expand-file-name file)
finally do (setq command-line-args-left nil)) )
(t
(append (list (expand-file-name "init.el" doom-emacs-dir)
doom-core-dir)
(unless lite-p (doom--module-paths))))))
(total-success 0)
(total-fail 0)
(total-nocomp 0)
(use-package-expand-minimally t))
(let ((el-files (cl-loop for path in targets
if (file-directory-p path)
nconc (nreverse (directory-files-recursively path "\\.el$"))
else if (file-exists-p path)
collect path)))
(dolist (file el-files)
(when (or (not only-recompile-p)
(let ((elc-file (byte-compile-dest-file file)))
(and (file-exists-p elc-file)
(file-newer-than-file-p file elc-file))))
(let ((result (if (string-match-p "/test/.+\\.el$" file)
'no-byte-compile
(byte-compile-file file)))
(short-name (file-relative-name file doom-emacs-dir)))
(cl-incf
(cond ((eq result 'no-byte-compile)
(message! (dark (white "Ignored %s" short-name)))
total-nocomp)
((null result)
(message! (red "Failed to compile %s" short-name))
total-fail)
(t
(message! (green "Compiled %s" short-name))
(quiet! (load file t t))
total-success))))))
(message!
(bold
(color (if (= total-fail 0) 'green 'red)
"%s %s file(s) %s"
(if only-recompile-p "Recompiled" "Compiled")
(format (if el-files "%d/%d" "%d")
total-success
(- (length el-files) total-nocomp))
(format "(%s ignored)" total-nocomp)))))))
(doom//byte-compile (list "core") recompile-p))
(defun doom/recompile ()
"Recompile any out-of-date compiled *.el files in your Emacs configuration."
(interactive)
(doom/compile nil :recompile)
;; Forcibly recompile core.el in case `load-path' has changed
(byte-recompile-file (expand-file-name "core.el" doom-core-dir) t))
(defun doom/recompile-packages ()
"Recompile all installed elpa packages. If you're getting odd errors after
upgrading Emacs, this may fix it."
(defun doom//byte-recompile-plugins ()
"Recompile all installed plugins. If you're getting odd errors after upgrading
(or downgrading) Emacs, this may fix it."
(interactive)
(byte-recompile-directory package-user-dir 0 t))
(defun doom/reset ()
"Clear the local cache completely (in `doom-cache-dir').
(defun doom//clean-byte-compiled-files ()
"Delete all the compiled elc files in your Emacs configuration.
This resets Emacs to a blank slate. You must restart Emacs for some components
to feel its effects."
(interactive)
(delete-directory doom-cache-dir t)
(make-directory doom-cache-dir t))
(defun doom/clean-compiled-files ()
"Delete all compiled elc files in your Emacs configuration.
This excludes compiled packages in `doom-packages-dir'."
This excludes compiled packages in `doom-packages-dir'.'"
(interactive)
(let ((targets (append (list (expand-file-name "init.elc" doom-emacs-dir))
(directory-files-recursively doom-core-dir "\\.elc$")
(directory-files-recursively doom-modules-dir "\\.elc$"))))
(unless (cl-loop for path in targets
if (file-exists-p path)
collect path
and do (delete-file path)
and do (message "Deleted %s" (file-relative-name path)))
collect path
and do (delete-file path)
and do (message "Deleted %s" (file-relative-name path)))
(message "Everything is clean"))))