def-advice!->defadvice! & conform to new advice conventions

This commit does two things:

- Renames def-advice! to defadvice!, in the spirit of naming convenience
  macros after the function/macro they enhance or replace.
- Correct the names of advice functions to indicate visibility and
  intent. A public advice function like doom-set-jump-a is meant to be
  used elsewhere. A private one like +dired--cleanup-header-line-a
  shouldn't -- it likely won't work anywhere but the function(s) it was
  made to advise.
This commit is contained in:
Henrik Lissner 2019-07-23 17:24:56 +02:00
parent 8aa7772e4e
commit 82ae3a73f3
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
43 changed files with 126 additions and 121 deletions

View file

@ -168,7 +168,7 @@ successfully sets indent_style/indent_size.")
(setq save-place-file (concat doom-cache-dir "saveplace")
save-place-forget-unreadable-files t
save-place-limit 200)
(def-advice! doom--recenter-on-load-saveplace-a (&rest _)
(defadvice! doom--recenter-on-load-saveplace-a (&rest _)
"Recenter on cursor when loading a saved place."
:after-while #'save-place-find-file-hook
(if buffer-file-name (ignore-errors (recenter))))
@ -199,14 +199,14 @@ successfully sets indent_style/indent_size.")
(better-jumper-mode +1)
(add-hook 'better-jumper-post-jump-hook #'recenter)
(defun doom-set-jump-a (orig-fn &rest args)
(defadvice! doom-set-jump-a (orig-fn &rest args)
"Set a jump point and ensure ORIG-FN doesn't set any new jump points."
(better-jumper-set-jump (if (markerp (car args)) (car args)))
(let ((evil--jumps-jumping t)
(better-jumper--jumping t))
(apply orig-fn args)))
(defun doom-set-jump-maybe-a (orig-fn &rest args)
(defadvice! doom-set-jump-maybe-a (orig-fn &rest args)
"Set a jump point if ORIG-FN returns non-nil."
(let ((origin (point-marker))
(result
@ -257,7 +257,7 @@ successfully sets indent_style/indent_size.")
(push '(t tab-width) dtrt-indent-hook-generic-mapping-list)
(defvar dtrt-indent-run-after-smie)
(def-advice! doom--fix-broken-smie-modes-a (orig-fn arg)
(defadvice! doom--fix-broken-smie-modes-a (orig-fn arg)
"Some smie modes throw errors when trying to guess their indentation, like
`nim-mode'. This prevents them from leaving Emacs in a broken state."
:around #'dtrt-indent-mode
@ -381,11 +381,11 @@ successfully sets indent_style/indent_size.")
`(("." . ,(concat doom-cache-dir "undo-tree-hist/"))))
(when (executable-find "zstd")
(def-advice! doom-undo-tree-make-history-save-file-name-a (file)
(defadvice! doom--undo-tree-make-history-save-file-name-a (file)
:filter-return #'undo-tree-make-history-save-file-name
(concat file ".zst")))
(def-advice! doom-undo-tree-strip-text-properties-a (&rest _)
(defadvice! doom--undo-tree-strip-text-properties-a (&rest _)
:before #'undo-list-transfer-to-tree
(dolist (item buffer-undo-list)
(and (consp item)

View file

@ -357,31 +357,6 @@ If N and M = 1, there's no benefit to using this macro over `remove-hook'.
(cl-loop for (_var _val hook fn) in (doom--setq-hook-fns hooks vars 'singles)
collect `(remove-hook ',hook #',fn))))
(defmacro def-advice! (symbol arglist docstring where places &rest body)
"Define an advice called NAME and add it to PLACES.
ARGLIST is as in `defun'. WHERE is a keyword as passed to `advice-add', and
PLACE is the function to which to add the advice, like in `advice-add'.
DOCSTRING and BODY are as in `defun'."
(declare (doc-string 3) (indent defun))
(unless (stringp docstring)
(push places body)
(setq places where
where docstring
docstring nil))
`(progn
(fset ',symbol (lambda ,arglist ,@body))
(put ',symbol 'function-documentation
(format "%sThis is %s advice for the following functions: %s"
,(if docstring (concat docstring "\n\n") "")
,where
(mapconcat (lambda (p) (format "`%s'" p))
(doom-enlist ,places) ", ")))
(dolist (target (doom-enlist ,places))
(if (eq ,where :remove)
(advice-remove target #',symbol)
(advice-add target ,where #',symbol)))))
(defmacro file-exists-p! (spec &optional directory)
"Returns non-nil if the files in SPEC all exist.
@ -490,5 +465,33 @@ writes to `standard-output'."
(save-silently t))
(prog1 ,@forms (message ""))))))
;;
;;; Definers
(define-obsolete-function-alias 'def-advice! 'defadvice!)
(defmacro defadvice! (symbol arglist &optional docstring &rest body)
"Define an advice called NAME and add it to PLACES.
ARGLIST is as in `defun'. WHERE is a keyword as passed to `advice-add', and
PLACE is the function to which to add the advice, like in `advice-add'.
DOCSTRING and BODY are as in `defun'.
\(fn SYMBOL ARGLIST &optional DOCSTRING &rest [WHERE PLACES...] BODY\)"
(declare (doc-string 3) (indent defun))
(unless (stringp docstring)
(push docstring body)
(setq docstring nil))
(let (where-alist)
(while (keywordp (car body))
(push `(cons ,(pop body) (doom-enlist ,(pop body)))
where-alist))
`(progn
(defun ,symbol ,arglist ,docstring ,@body)
,(when where-alist
`(dolist (targets (list ,@(nreverse where-alist)))
(dolist (target (cdr targets))
(advice-add target (car targets) #',symbol)))))))
(provide 'core-lib)
;;; core-lib.el ends here

View file

@ -85,7 +85,7 @@ missing) and shouldn't be deleted.")
("org" . ,(concat proto "://orgmode.org/elpa/")))))
;; Don't save `package-selected-packages' to `custom-file'
(def-advice! doom--package-inhibit-custom-file-a (&optional value)
(defadvice! doom--package-inhibit-custom-file-a (&optional value)
:override #'package--save-selected-packages
(if value (setq package-selected-packages value)))
@ -111,7 +111,7 @@ missing) and shouldn't be deleted.")
autoload-compute-prefixes nil)
;; Straight is hardcoded to operate out of ~/.emacs.d/straight. Not on my watch!
(def-advice! doom--straight-use-local-dir-a (orig-fn &rest args)
(defadvice! doom--straight-use-local-dir-a (orig-fn &rest args)
:around #'straight--emacs-dir
(let ((user-emacs-directory doom-local-dir))
(apply orig-fn args)))

View file

@ -118,7 +118,7 @@ c) are not valid projectile projects."
(unless (executable-find "tr")
(setq projectile-git-submodule-command nil))))
(def-advice! doom--projectile-cache-timers-a ()
(defadvice! doom--projectile-cache-timers-a ()
"Persist `projectile-projects-cache-time' across sessions, so that
`projectile-files-cache-expire' checks won't reset when restarting Emacs."
:before #'projectile-serialize-cache
@ -128,7 +128,7 @@ c) are not valid projectile projects."
(setq projectile-projects-cache-time
(projectile-unserialize doom-projectile-cache-timer-file)))
(def-advice! doom--projectile-default-generic-command-a (orig-fn &rest args)
(defadvice! doom--projectile-default-generic-command-a (orig-fn &rest args)
"If projectile can't tell what kind of project you're in, it issues an error
when using many of projectile's command, e.g. `projectile-compile-command',
`projectile-run-project', `projectile-test-project', and
@ -142,7 +142,7 @@ the command instead."
;; Projectile root-searching functions can cause an infinite loop on TRAMP
;; connections, so disable them.
;; TODO Is this still necessary?
(def-advice! doom--projectile-locate-dominating-file-a (orig-fn file name)
(defadvice! doom--projectile-locate-dominating-file-a (orig-fn file name)
"Don't traverse the file system if on a remote connection."
:around #'projectile-locate-dominating-file
(when (and (stringp file)

View file

@ -217,7 +217,7 @@ read-only or not file-visiting."
(setq confirm-nonexistent-file-or-buffer t)
(def-advice! doom-switch-to-fallback-buffer-maybe-a (orig-fn)
(defadvice! doom--switch-to-fallback-buffer-maybe-a (orig-fn)
"Switch to `doom-fallback-buffer' if on last real buffer.
Advice for `kill-current-buffer'. If in a dedicated window, delete it. If there
@ -457,8 +457,8 @@ treat Emacs as a non-application window."
all-the-icons-material
all-the-icons-alltheicon)
:init
(def-advice! doom--disable-all-the-icons-in-tty-a (orig-fn &rest args)
"all-the-icons doesn't work in the terminal, so we \"disable\" them."
(defadvice! doom--disable-all-the-icons-in-tty-a (orig-fn &rest args)
"Return a blank string in tty Emacs, which doesn't support multiple fonts."
:around '(all-the-icons-octicon all-the-icons-material
all-the-icons-faicon all-the-icons-fileicon
all-the-icons-wicon all-the-icons-alltheicon)
@ -620,14 +620,14 @@ Fonts are specified by `doom-font', `doom-variable-pitch-font',
(let ((doom--prefer-theme-elc t))
(load-theme doom-theme t)))))
(def-advice! doom--run-load-theme-hooks-a (theme &optional _no-confirm no-enable)
(defadvice! doom--run-load-theme-hooks-a (theme &optional _no-confirm no-enable)
"Set up `doom-load-theme-hook' to run after `load-theme' is called."
:after #'load-theme
(unless no-enable
(setq doom-theme theme)
(run-hooks 'doom-load-theme-hook)))
(def-advice! doom--prefer-compiled-theme-a (orig-fn &rest args)
(defadvice! doom--prefer-compiled-theme-a (orig-fn &rest args)
"Make `load-theme' prioritize the byte-compiled theme for a moderate boost in
startup (or theme switch) time, so long as `doom--prefer-theme-elc' is non-nil."
:around #'load-theme