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

@ -18,16 +18,16 @@ ce: compile-elpa
## Package management ## Package management
install: init.el .local/autoloads.el install: init.el .local/autoloads.el
@$(EMACS) -f doom/packages-install @$(EMACS) -f doom//packages-install
update: init.el .local/autoloads.el update: init.el .local/autoloads.el
@$(EMACS) -f doom/packages-update @$(EMACS) -f doom//packages-update
autoremove: init.el .local/autoloads.el autoremove: init.el .local/autoloads.el
@$(EMACS) -f doom/packages-autoremove @$(EMACS) -f doom//packages-autoremove
autoloads: init.el autoloads: init.el
@$(EMACS) -f doom/reload-autoloads @$(EMACS) -f doom//reload-autoloads
## Byte compilation ## Byte compilation
@ -36,29 +36,22 @@ autoloads: init.el
# compile-module # compile-module
# compile-module/submodule # compile-module/submodule
compile: init.el clean compile: init.el clean
@$(EMACS) -f doom/compile @$(EMACS) -f doom//byte-compile
compile-core: init.el clean compile-core: init.el clean
@$(EMACS) -f doom/compile -- init.el core @$(EMACS) -f doom//byte-compile-core
compile-elpa: init.el compile-elpa: init.el
@$(EMACS) -f doom/recompile-packages @$(EMACS) -f doom//byte-recompile-plugins
$(patsubst %, compile-%, $(MODULES)): init.el .local/autoloads.el $(patsubst %, compile-%, $(MODULES)): init.el .local/autoloads.el
@rm -fv $(shell find $(patsubst compile-%, modules/%, $@) -type f -name '*.elc') @$(EMACS) -f doom//byte-compile $(patsubst compile-%, %, $@)
@$(EMACS) -f doom/compile -- $(patsubst compile-%, modules/%, $@)
recompile: init.el recompile: init.el
@$(EMACS) -f doom/recompile @$(EMACS) -f doom//byte-compile -r
clean: clean:
@$(EMACS) -f doom/clean-compiled-files @$(EMACS) -f doom//clean-byte-compiled-files
clean-pcache:
@$(EMACS) -l persistent-soft --eval '(delete-directory pcache-directory t)'
reset:
@$(EMACS) -f doom/reset
## Unit tests ## Unit tests
@ -67,14 +60,14 @@ reset:
# test-module # test-module
# test-module/submodule # test-module/submodule
test: init.el .local/autoloads.el test: init.el .local/autoloads.el
@$(EMACS) -f doom-run-tests @$(EMACS) -f doom//run-tests
test-core $(patsubst %, test-%, $(MODULES)): init.el .local/autoloads.el test-core $(patsubst %, test-%, $(MODULES)): init.el .local/autoloads.el
@$(EMACS) -f doom-run-tests -- $(subst test-, , $@) @$(EMACS) -f doom//run-tests -- $(subst test-, , $@)
# run tests interactively # run tests interactively
testi: init.el .local/autoloads.el testi: init.el .local/autoloads.el
@$(EMACSI) -f doom-run-tests -f ert @$(EMACSI) -f doom//run-tests -f ert
## Utility tasks ## Utility tasks
@ -93,7 +86,4 @@ init.el:
.local/autoloads.el: .local/autoloads.el:
@$(EMACS) -f doom-initialize-autoloads @$(EMACS) -f doom-initialize-autoloads
%.elc: %.el .PHONY: all compile test testi clean
@$(EMACS) -f doom/compile -- $<
.PHONY: all compile test testi

View file

@ -303,7 +303,7 @@ package.el as appropriate."
;; ;;
;;;###autoload ;;;###autoload
(defun doom/packages-install () (defun doom//packages-install ()
"Interactive command for installing missing packages." "Interactive command for installing missing packages."
(interactive) (interactive)
(let ((packages (doom-get-missing-packages))) (let ((packages (doom-get-missing-packages)))
@ -341,10 +341,10 @@ package.el as appropriate."
""))))) "")))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload-load-path)))) (doom//reload-load-path))))
;;;###autoload ;;;###autoload
(defun doom/packages-update () (defun doom//packages-update ()
"Interactive command for updating packages." "Interactive command for updating packages."
(interactive) (interactive)
(doom-refresh-packages doom-debug-mode) (doom-refresh-packages doom-debug-mode)
@ -382,10 +382,10 @@ package.el as appropriate."
(if result "DONE" "FAILED")))))) (if result "DONE" "FAILED"))))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload-load-path))))) (doom//reload-load-path)))))
;;;###autoload ;;;###autoload
(defun doom/packages-autoremove () (defun doom//packages-autoremove ()
"Interactive command for auto-removing orphaned packages." "Interactive command for auto-removing orphaned packages."
(interactive) (interactive)
(let ((packages (doom-get-orphaned-packages))) (let ((packages (doom-get-orphaned-packages)))
@ -415,7 +415,7 @@ package.el as appropriate."
pkg))))) pkg)))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload-load-path))))) (doom//reload-load-path)))))
;;;###autoload ;;;###autoload
(defalias 'doom/install-package #'package-install) (defalias 'doom/install-package #'package-install)

View file

@ -1,7 +1,7 @@
;;; core/autoload/test.el -*- lexical-binding: t; -*- ;;; core/autoload/test.el -*- lexical-binding: t; no-byte-compile: t; -*-
;;;###autoload ;;;###autoload
(defun doom-run-tests (&optional modules) (defun doom//run-tests (&optional modules)
"Run all loaded tests, specified by MODULES (a list of module cons cells) or "Run all loaded tests, specified by MODULES (a list of module cons cells) or
command line args following a double dash (each arg should be in the command line args following a double dash (each arg should be in the
'module/submodule' format). 'module/submodule' format).

View file

@ -68,17 +68,6 @@ window changes before then, the undo expires."
(delete-frame)) (delete-frame))
(save-buffers-kill-emacs))) (save-buffers-kill-emacs)))
;;;###autoload
(defun doom/reload-theme ()
"Reset the color theme currently in use."
(interactive)
(let ((theme (or (car-safe custom-enabled-themes) doom-theme)))
(when theme
(mapc #'disable-theme custom-enabled-themes))
(run-hooks 'doom-pre-reload-theme-hook)
(doom|init-ui)
(run-hooks 'doom-post-reload-theme-hook)))
;;;###autoload ;;;###autoload
(define-minor-mode doom-big-font-mode (define-minor-mode doom-big-font-mode
"A global mode that resizes the font, for streams, screen-sharing and "A global mode that resizes the font, for streams, screen-sharing and
@ -91,3 +80,14 @@ presentations."
(if doom-big-font-mode (if doom-big-font-mode
(set-frame-font doom-big-font t t) (set-frame-font doom-big-font t t)
(set-frame-font doom-font t t))) (set-frame-font doom-font t t)))
;;;###autoload
(defun doom//reload-theme ()
"Reset the color theme currently in use."
(interactive)
(let ((theme (or (car-safe custom-enabled-themes) doom-theme)))
(when theme
(mapc #'disable-theme custom-enabled-themes))
(run-hooks 'doom-pre-reload-theme-hook)
(doom|init-ui)
(run-hooks 'doom-post-reload-theme-hook)))

View file

@ -181,7 +181,7 @@ startup."
"Ensures that `doom-autoload-file' exists and is loaded. Otherwise run "Ensures that `doom-autoload-file' exists and is loaded. Otherwise run
`doom/reload-autoloads' to generate it." `doom/reload-autoloads' to generate it."
(unless (file-exists-p doom-autoload-file) (unless (file-exists-p doom-autoload-file)
(quiet! (doom/reload-autoloads)))) (quiet! (doom//reload-autoloads))))
(defun doom-initialize-packages (&optional force-p load-p) (defun doom-initialize-packages (&optional force-p load-p)
"Crawls across your emacs.d to fill `doom-modules' (from init.el) and "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." This aggressively reloads core autoload files."
(doom-initialize force-p) (doom-initialize force-p)
(let ((noninteractive t) (with-temp-buffer ; prevent buffer-local settings from propagating
(load-prefer-newer t) (let ((noninteractive t)
(load-fn (load-prefer-newer t)
(lambda (file &optional noerror) (load-fn
(condition-case-unless-debug ex (lambda (file &optional noerror)
(load file noerror :nomessage :nosuffix) (condition-case-unless-debug ex
('error (load file noerror :nomessage :nosuffix)
(error (format "(doom-initialize-packages) %s in %s: %s" ('error
(car ex) (error (format "(doom-initialize-packages) %s in %s: %s"
(file-relative-name file doom-emacs-dir) (car ex)
(error-message-string ex)) (file-relative-name file doom-emacs-dir)
:error)))))) (error-message-string ex))
(when (or force-p (not doom-modules)) :error))))))
(setq doom-modules nil) (when (or force-p (not doom-modules))
(funcall load-fn (expand-file-name "init.el" doom-emacs-dir)) (setq doom-modules nil)
(when load-p
(let (noninteractive) (let (noninteractive)
(funcall load-fn (doom-module-path :private user-login-name "init.el") t) (load (concat doom-core-dir "core.el") nil t))
(funcall load-fn (expand-file-name "core.el" doom-core-dir))) (funcall load-fn (expand-file-name "init.el" doom-emacs-dir))
(mapc load-fn (file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir)))) (when load-p
(doom|finalize)) (let (noninteractive)
(when (or force-p (not doom-packages)) (funcall load-fn (doom-module-path :private user-login-name "init.el") t))
(setq doom-packages nil) (mapc load-fn (file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir)))
(funcall load-fn (expand-file-name "packages.el" doom-core-dir)) (cl-loop for (module . submodule) in (doom--module-pairs)
(cl-loop for (module . submodule) in (doom--module-pairs) for path = (doom-module-path module submodule "config.el")
for path = (doom-module-path module submodule "packages.el") do (funcall load-fn path t))))
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) (defun doom-initialize-modules (modules)
"Adds MODULES to `doom-modules'. MODULES must be in mplist format. "Adds MODULES to `doom-modules'. MODULES must be in mplist format.
@ -486,7 +491,15 @@ loads MODULE SUBMODULE's packages.el file."
;; Commands ;; 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). "Reload `load-path' and recompile files (if necessary).
Use this when `load-path' is out of sync with your plugins. This should only 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 This isn't necessary if you use Doom's package management commands because they
call `doom/reload-load-path' remotely (through emacsclient)." call `doom/reload-load-path' remotely (through emacsclient)."
(interactive) (interactive)
(byte-recompile-file (expand-file-name "core.el" doom-core-dir) t)
(cond (noninteractive (cond (noninteractive
(message "Reloading...") (message "Reloading...")
(require 'server) (require 'server)
(unless (ignore-errors (server-eval-at "server" '(doom/reload-load-path))) (when (file-exists-p (if server-use-tcp server-auth-dir server-socket-dir))
(message "Recompiling") (server-eval-at "server" '(doom//reload-load-path))))
(doom/recompile)))
(t (t
(doom-initialize t) (doom-initialize t)
(doom/recompile)
(message "Reloaded %d packages" (length doom--package-load-path)) (message "Reloaded %d packages" (length doom--package-load-path))
(run-with-timer 1 nil #'redraw-display) (run-with-timer 1 nil #'redraw-display)
(run-hooks 'doom-reload-hook)))) (run-hooks 'doom-reload-hook))))
(defun doom/reload-autoloads () (defun doom//reload-autoloads ()
"Refreshes the autoloads.el file, specified by `doom-autoload-file'. "Refreshes the autoloads.el file, specified by `doom-autoload-file'.
It scans and reads core/autoload/*.el, modules/*/*/autoload.el and 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. ;; This function must not use autoloaded functions or external dependencies.
;; It must assume nothing is set up! ;; It must assume nothing is set up!
(doom-initialize-packages (not noninteractive)) (doom-initialize-packages (not noninteractive))
(let ((evil-p (doom-module-loaded-p :feature 'evil)) (let ((targets
(targets
(file-expand-wildcards (file-expand-wildcards
(expand-file-name "autoload/*.el" doom-core-dir)))) (expand-file-name "autoload/*.el" doom-core-dir))))
(dolist (path (doom--module-paths)) (dolist (path (doom--module-paths))
(let ((auto-dir (expand-file-name "autoload" path)) (let ((auto-dir (expand-file-name "autoload" path))
(auto-file (expand-file-name "autoload.el" 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)) (push auto-file targets))
(when (file-directory-p auto-dir) (when (file-directory-p auto-dir)
(dolist (file (file-expand-wildcards (expand-file-name "*.el" auto-dir) t)) (dolist (file (file-expand-wildcards (expand-file-name "*.el" auto-dir) t))
;; Make evil*.el autoload files a special case; don't load ;; Make evil*.el autoload files a special case; don't load
;; them unless evil is enabled. ;; them unless evil is enabled.
(unless (and (string-prefix-p "evil" (file-name-nondirectory file)) (when (doom--read-cookie-pred file)
(not evil-p))
(push file targets)))))) (push file targets))))))
(when (file-exists-p doom-autoload-file) (when (file-exists-p doom-autoload-file)
(delete-file 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)))) (car ex) (error-message-string ex))))
(kill-buffer buf))))) (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. "Byte compiles your emacs configuration.
Specifically, this byte-compiles init.el, core/*.el, core/autoload/*.el & init.el is always byte-compiled by this.
modules/*/*/**.el. It ignores unit tests and files with `no-byte-compile'
enabled.
DOOM Emacs was designed to benefit from byte-compilation, but the process may If MODULES is specified (a list of module strings, e.g. \"lang/php\"), those are
take a while. Also, while your config files are byte-compiled, changes to them byte-compiled. Otherwise, all enabled modules are byte-compiled, including Doom
will not take effect! Use `doom/clean-compiled' or `make clean' to undo core. It always ignores unit tests and files with `no-byte-compile' enabled.
byte-compilation.
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") (interactive "P")
;; Ensure all relevant config files are loaded and up-to-date. This way we (doom//byte-compile (list "core") recompile-p))
;; 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)))))))
(defun doom/recompile () (defun doom//byte-recompile-plugins ()
"Recompile any out-of-date compiled *.el files in your Emacs configuration." "Recompile all installed plugins. If you're getting odd errors after upgrading
(interactive) (or downgrading) Emacs, this may fix it."
(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."
(interactive) (interactive)
(byte-recompile-directory package-user-dir 0 t)) (byte-recompile-directory package-user-dir 0 t))
(defun doom/reset () (defun doom//clean-byte-compiled-files ()
"Clear the local cache completely (in `doom-cache-dir'). "Delete all the compiled elc files in your Emacs configuration.
This resets Emacs to a blank slate. You must restart Emacs for some components This excludes compiled packages in `doom-packages-dir'.'"
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'."
(interactive) (interactive)
(let ((targets (append (list (expand-file-name "init.elc" doom-emacs-dir)) (let ((targets (append (list (expand-file-name "init.elc" doom-emacs-dir))
(directory-files-recursively doom-core-dir "\\.elc$") (directory-files-recursively doom-core-dir "\\.elc$")
(directory-files-recursively doom-modules-dir "\\.elc$")))) (directory-files-recursively doom-modules-dir "\\.elc$"))))
(unless (cl-loop for path in targets (unless (cl-loop for path in targets
if (file-exists-p path) if (file-exists-p path)
collect path collect path
and do (delete-file path) and do (delete-file path)
and do (message "Deleted %s" (file-relative-name path))) and do (message "Deleted %s" (file-relative-name path)))
(message "Everything is clean")))) (message "Everything is clean"))))

View file

@ -48,7 +48,7 @@ state are passed in.")
;; Library ;; Library
;; ;;
(defun doom/reload-project () (defun doom//reload-project ()
"Reload the project root cache." "Reload the project root cache."
(interactive) (interactive)
(projectile-invalidate-cache nil) (projectile-invalidate-cache nil)

View file

@ -29,7 +29,7 @@ shorter major mode name in the mode-line. See `doom|set-mode-name'.")
;; Hook(s) ;; Hook(s)
(defvar doom-init-ui-hook nil (defvar doom-init-ui-hook nil
"List of hooks to run when the theme and font is initialized (or reloaded with "List of hooks to run when the theme and font is initialized (or reloaded with
`doom/reload-theme').") `doom//reload-theme').")
;; Settings ;; Settings

View file

@ -5,6 +5,7 @@
;; doom-... public variables or non-interactive functions ;; doom-... public variables or non-interactive functions
;; doom--... private anything (non-interactive), not safe for direct use ;; doom--... private anything (non-interactive), not safe for direct use
;; doom/... an interactive function; safe for M-x or keybinding ;; doom/... an interactive function; safe for M-x or keybinding
;; doom//... an interactive function for managing/maintaining Doom itself
;; doom:... an evil operator, motion or command ;; doom:... an evil operator, motion or command
;; doom|... hook function ;; doom|... hook function
;; doom*... advising functions ;; doom*... advising functions