aboutsummaryrefslogtreecommitdiff
path: root/evil-core.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2023-07-01 16:30:28 -0400
committerAxel Forsman <axel@axelf.se>2024-09-15 10:32:14 +0200
commitc17f03cf876f754f6249e616947f2485036ca7f8 (patch)
treec3a800452906d1163c7abe52edcfe664ec56774f /evil-core.el
parent1b49c159eb634bc35d65afdc324be4392cc8b6e0 (diff)
Replace uses of `defadvice` with `advice-add`
This either requires a dependency on the `nadvice` package, or bumping the minimum Emacs version to 24.4. I went with the `nadvice` package, but maybe bumping up to 24.4 would be better. * evil.el: Require `nadvice`. * evil-core.el (evil--advices): New var. (evil-mode): Use it instead of `ad-dis/enable`. (evil--advice-add): New function. (set-window-buffer, select-window, toggle-input-method, use-global-map): * evil-search.el (isearch-message-prefix, isearch-delete-char) (isearch-lazy-highlight-search): * evil-integration.el (keyboard-quit, wdired-change-to-dired-mode) (show-paren-function, quail-show-key, describe-char, ace-jump-done): Use `(evil--)advice-add` instead of `defadvice`. (preceding-sexp, pp-last-sexp): Remove old code for when `advice-add` is not available. * evil-repeat.el (evil--read-key-sequence-advice): Adapt to use in `advice-add`. (read-key-sequence, read-key-sequence-vector): Use `advice-add`. * evil-keybindings.el (elp-results): Use `advice-add` and move outside of `eval-after-load`.
Diffstat (limited to 'evil-core.el')
-rw-r--r--evil-core.el54
1 files changed, 32 insertions, 22 deletions
diff --git a/evil-core.el b/evil-core.el
index 4966491..80e84c8 100644
--- a/evil-core.el
+++ b/evil-core.el
@@ -106,7 +106,6 @@
;;; Code:
-(require 'advice)
(require 'evil-common)
(declare-function evil-emacs-state-p "evil-states")
@@ -168,11 +167,15 @@ To enable Evil globally, do (evil-mode)."
(defalias 'evil--fundamental-mode #'fundamental-mode)
+(defvar evil--advices ())
+
;;;###autoload (autoload 'evil-mode "evil" nil t)
(define-globalized-minor-mode evil-mode evil-local-mode evil-initialize
:group 'evil)
-(defadvice evil-mode (after start-evil activate)
+;; `define-globalized-minor-mode' supports a BODY argument but only since
+;; GNU Emacs 27.1, so resort to this ugly advice in the mean time.
+(define-advice evil-mode (:after (&optional _arg) body)
;; Hooks used to not run in Fundamental buffers (bug#23827), so
;; other measures are necessary to initialize Evil there. When Evil
;; is enabled globally, the default value of `major-mode' is set to
@@ -181,16 +184,20 @@ To enable Evil globally, do (evil-mode)."
(progn
(and (eval-when-compile (version< emacs-version "26.1"))
(eq (default-value 'major-mode) 'fundamental-mode)
- (setq-default major-mode 'evil--fundamental-mode))
- (ad-enable-regexp "^evil")
- (ad-activate-regexp "^evil")
+ (setq-default major-mode #'evil--fundamental-mode))
+ (dolist (advice evil--advices) (apply #'advice-add advice))
(with-no-warnings (evil-esc-mode 1)))
- (when (eq (default-value 'major-mode) 'evil--fundamental-mode)
+ (when (eq (default-value 'major-mode) #'evil--fundamental-mode)
(setq-default major-mode 'fundamental-mode))
- (ad-disable-regexp "^evil")
- (ad-update-regexp "^evil")
+ (pcase-dolist (`(,funname ,_where ,adfun) evil--advices)
+ (advice-remove funname adfun))
(with-no-warnings (evil-esc-mode -1))))
+(defun evil--advice-add (&rest args)
+ "Like `advice-add' for advices active only in `evil-mode'."
+ (when evil-mode (apply #'advice-add args))
+ (cl-pushnew args evil--advices :test #'equal))
+
(defun evil-change-state (state &optional message)
"Change the state to STATE.
If STATE is nil, disable all states."
@@ -303,8 +310,8 @@ This is the state the buffer came up in. If Evil is not activated
then this function does nothing."
:keep-visual t
:suppress-operator t
- (with-current-buffer (or buffer (current-buffer))
- (when evil-local-mode
+ (when evil-local-mode
+ (with-current-buffer (or buffer (current-buffer))
(evil-change-state (evil-initial-state-for-buffer buffer)
message))))
@@ -329,17 +336,20 @@ then this function does nothing."
;; run. This is appropriate since many buffers are used for throwaway
;; purposes. Passing the buffer to `set-window-buffer' indicates
;; otherwise, though, so advise this function to initialize Evil.
-(defadvice set-window-buffer (before evil)
+(evil--advice-add 'set-window-buffer :before #'evil--swb-initialize)
+(defun evil--swb-initialize (_window buffer &rest _)
"Initialize Evil in the displayed buffer."
- (when (and evil-mode (get-buffer (ad-get-arg 1)))
- (with-current-buffer (ad-get-arg 1)
+ (when (and evil-mode (get-buffer buffer))
+ (with-current-buffer buffer
(unless evil-local-mode
(save-match-data (evil-initialize))))))
;; Refresh cursor color.
;; Cursor color can only be set for each frame but not for each buffer.
+;; FIXME: Shouldn't this belong in `evil-(local-)mode'?
(add-hook 'window-configuration-change-hook #'evil-refresh-cursor)
-(defadvice select-window (after evil activate)
+(advice-add 'select-window :after #'evil--sw-refresh-cursor)
+(defun evil--sw-refresh-cursor (&rest _)
(evil-refresh-cursor))
(defun evil-generate-mode-line-tag (&optional state)
@@ -422,16 +432,17 @@ This allows input methods to be used in normal-state."
(add-hook 'input-method-activate-hook #'evil-activate-input-method nil t)
(add-hook 'input-method-deactivate-hook #'evil-deactivate-input-method nil t)))
-(defadvice toggle-input-method (around evil)
+(evil--advice-add 'toggle-input-method :around #'evil--refresh-input-method)
+(defun evil--refresh-input-method (orig-fun &rest args)
"Refresh `evil-input-method'."
(cond
((not evil-local-mode)
- ad-do-it)
+ (apply orig-fun args))
((evil-state-property evil-state :input-method)
- ad-do-it)
+ (apply orig-fun args))
(t
(let ((current-input-method evil-input-method))
- ad-do-it))))
+ (apply orig-fun args)))))
;; Local keymaps are implemented using buffer-local variables.
;; However, unless a buffer-local value already exists,
@@ -1105,13 +1116,12 @@ Add additional BINDINGS if specified."
;; Advise these functions as they may activate an overriding keymap or
;; a keymap with state bindings; if so, refresh `evil-mode-map-alist'.
-(defadvice use-global-map (after evil activate)
+(advice-add 'use-global-map :after #'evil--do-normalize-keymaps)
+(advice-add 'use-local-map :after #'evil--do-normalize-keymaps)
+(defun evil--do-normalize-keymaps (&rest _)
"Refresh Evil keymaps."
(evil-normalize-keymaps))
-(defadvice use-local-map (after evil activate)
- "Refresh Evil keymaps."
- (evil-normalize-keymaps))
(defmacro evil-define-state (state doc &rest body)
"Define an Evil state STATE.