Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various code cleanups #1693

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*.pg
*.tp
.depend
evil-autoloads.el
/evil-autoloads.el
/evil-pkg.el
test-results.txt
typescript

Expand Down
1 change: 1 addition & 0 deletions Eask
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
(depends-on "emacs" "24.1")
(depends-on "cl-lib")
(depends-on "goto-chg")
(depends-on "nadvice")

(setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432

Expand Down
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
SHELL = /bin/sh
EMACS ?= emacs
SED ?= sed
FILES = $(filter-out evil-test-helpers.el evil-tests.el evil-pkg.el,$(wildcard evil*.el))
VERSION := $(shell $(SED) -ne '/define-package/,$$p' evil-pkg.el | $(SED) -ne '/^\s*"[[:digit:]]\+\(\.[[:digit:]]\+\)*"\s*$$/ s/^.*"\(.*\)".*$$/\1/p')
VERSION := $(shell $(SED) -n '/^;\+ \+Version: *\(.*\)/{s//\1/p;q}' evil.el)
PROFILER =
DOC = doc
TAG =
EASK ?= eask

Expand Down Expand Up @@ -52,8 +50,7 @@ emacs:
--eval "(evil-tests-initialize '(${TAG}) '(${PROFILER}) t)"

# Load Evil in a terminal Emacs and run all tests.
term: terminal
terminal:
term terminal:
$(EASK) run command terminal

# Run all tests with profiler.
Expand All @@ -66,8 +63,12 @@ profiler:
indent: clean
$(EASK) run command indent

evil-pkg.el: evil.el
$(EMACS) --batch -l package $^ \
--eval "(package-generate-description-file (package-buffer-info) \"$@\")"

# Create an ELPA package.
elpa:
elpa: evil-pkg.el
$(EASK) package

# Change the version using make VERSION=x.y.z
Expand Down
3 changes: 3 additions & 0 deletions evil-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ If LOCAL is non-nil, the buffer-local value of HOOK is modified."
(macroexp-let2* nil
((fun-name `(make-symbol
,(or name (format "evil-delay-in-%s" hook-sym))))
;; `apply-partially' is used in case this macro is expanded
;; (via `evil-define-key') in a file which still does not
;; activate `lexical-binding'.
(fun `(apply-partially
(lambda (name &rest _)
(when ,(or condition t)
Expand Down
54 changes: 32 additions & 22 deletions evil-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@

;;; Code:

(require 'advice)
(require 'evil-common)

(declare-function evil-emacs-state-p "evil-states")
Expand Down Expand Up @@ -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
Expand All @@ -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."
Expand Down Expand Up @@ -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))))

Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions evil-ex.el
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,13 @@ and function:
(func (pop body)))
(cond
((eq key :runner)
(setq runner func))
(setq runner `#',func))
((eq key :collection)
(setq completer (cons 'collection func)))
(setq completer `(cons 'collection #',func)))
((eq key :completion-at-point)
(setq completer (cons 'completion-at-point func))))))
(setq completer `(cons 'completion-at-point #',func))))))
`(evil--add-to-alist evil-ex-argument-types
',arg-type '(,runner ,completer))))
',arg-type (list ,runner ,completer))))

(evil-ex-define-argument-type file
"Handle a file argument."
Expand All @@ -668,8 +668,8 @@ argument handler that requires shell completion."
(require 'shell)
;; Set up Comint for Shell mode, except
;; `comint-completion-at-point' will be called manually.
(let (completion-at-point-functions)
(shell-completion-vars))))
(shell-completion-vars)
(remove-hook 'completion-at-point-functions #'comint-completion-at-point t)))

(evil-ex-define-argument-type shell
"Shell argument type, supports completion."
Expand Down
94 changes: 35 additions & 59 deletions evil-integration.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,22 @@
;;; key-binding

;; Calling `keyboard-quit' should cancel repeat
(defadvice keyboard-quit (before evil activate)
(when (fboundp 'evil-repeat-abort)
(evil-repeat-abort)))
(advice-add 'keyboard-quit :before #'evil-repeat-abort)

(eval-after-load 'wdired
'(progn
(add-hook 'wdired-mode-hook #'evil-change-to-initial-state)
(defadvice wdired-change-to-dired-mode (after evil activate)
(evil-change-to-initial-state nil t))))
(add-hook 'wdired-mode-hook #'evil-change-to-initial-state)
(advice-add 'wdired-change-to-dired-mode :after
#'evil--change-to-initial-state-with-msg)
(defun evil--change-to-initial-state-with-msg (&rest _)
(evil-change-to-initial-state nil t))

;;; Parentheses

(defadvice show-paren-function (around evil disable)
(evil--advice-add 'show-paren-function :around #'evil--match-paren-in-normal-state)
(defun evil--match-paren-in-normal-state (orig-fun &rest args)
"Match parentheses in Normal state."
(if (eq (not (memq 'not evil-highlight-closing-paren-at-point-states))
(not (memq evil-state evil-highlight-closing-paren-at-point-states)))
ad-do-it
(apply orig-fun args)
(let* ((orig-spdf show-paren-data-function)
(show-paren-data-function
(lambda ()
Expand All @@ -124,7 +123,7 @@
(save-restriction
(when narrow (narrow-to-region narrow (point-max)))
(funcall orig-spdf))))))))
ad-do-it)))
(apply orig-fun args))))

;;; Undo tree
(eval-after-load 'undo-tree
Expand Down Expand Up @@ -209,50 +208,27 @@
company-search-candidates
company-filter-candidates))))

;; Eval last sexp
(cond
((version< emacs-version "25")
(defadvice preceding-sexp (around evil activate)
"In normal-state or motion-state, last sexp ends at point."
(if (and (not evil-move-beyond-eol)
(or (evil-normal-state-p) (evil-motion-state-p)))
(save-excursion
(unless (or (eobp) (eolp)) (forward-char))
ad-do-it)
ad-do-it))

(defadvice pp-last-sexp (around evil activate)
"In normal-state or motion-state, last sexp ends at point."
(if (and (not evil-move-beyond-eol)
(or (evil-normal-state-p) (evil-motion-state-p)))
(save-excursion
(unless (or (eobp) (eolp)) (forward-char))
ad-do-it)
ad-do-it)))
(t
(defun evil--preceding-sexp (command &rest args)
"In normal-state or motion-state, last sexp ends at point."
(if (and (not evil-move-beyond-eol)
(or (evil-normal-state-p) (evil-motion-state-p)))
(save-excursion
(unless (or (eobp) (eolp)) (forward-char))
(apply command args))
(apply command args)))

(advice-add 'elisp--preceding-sexp :around 'evil--preceding-sexp '((name . evil)))
(advice-add 'pp-last-sexp :around 'evil--preceding-sexp '((name . evil)))
(advice-add 'lisp-eval-last-sexp :around 'evil--preceding-sexp '((name . evil)))))

;; Show key
(defadvice quail-show-key (around evil activate)
"Temporarily go to Emacs state"
(evil-with-state emacs ad-do-it))

(defadvice describe-char (around evil activate)
"Temporarily go to Emacs state"
(evil-with-state emacs ad-do-it))

;; ace-jump-mode
;;; Eval last sexp
(defun evil--preceding-sexp (command &rest args)
"In Normal- or Motion-state, allow last sexp to end at point."
(save-excursion
(or evil-move-beyond-eol
(eolp)
(not (or (evil-normal-state-p) (evil-motion-state-p)))
(forward-char))
(apply command args)))
(advice-add 'elisp--preceding-sexp :around #'evil--preceding-sexp)
(advice-add 'pp-last-sexp :around #'evil--preceding-sexp)
(advice-add 'lisp-eval-last-sexp :around #'evil--preceding-sexp)

;;; Show key
(advice-add 'quail-show-key :around #'evil--in-emacs-state)
(advice-add 'describe-char :around #'evil--in-emacs-state)
(defun evil--in-emacs-state (orig-fun &rest args)
"Temporarily enter Emacs-state and apply ORIG-FUN to ARGS."
(evil-with-state emacs (apply orig-fun args)))

;;; ace-jump-mode
(declare-function ace-jump-char-mode "ext:ace-jump-mode")
(declare-function ace-jump-word-mode "ext:ace-jump-mode")
(declare-function ace-jump-line-mode "ext:ace-jump-mode")
Expand Down Expand Up @@ -294,10 +270,10 @@ the mark and entering `recursive-edit'."
(set-mark old-mark))
(push-mark old-mark)))))

(eval-after-load 'ace-jump-mode
`(defadvice ace-jump-done (after evil activate)
(when evil-ace-jump-active
(add-hook 'post-command-hook #'evil-ace-jump-exit-recursive-edit))))
(advice-add 'ace-jump-done :after #'evil--after-ace-jump-done)
(defun evil--after-ace-jump-done (&rest _)
(when evil-ace-jump-active
(add-hook 'post-command-hook #'evil-ace-jump-exit-recursive-edit)))

(defun evil-ace-jump-exit-recursive-edit ()
"Exit a recursive edit caused by an evil jump."
Expand Down
6 changes: 3 additions & 3 deletions evil-keybindings.el
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@

;;; ELP

(eval-after-load 'elp
'(defadvice elp-results (after evil activate)
(evil-motion-state)))
(advice-add 'elp-results :after #'evil--set-motion-state)
(defun evil--set-motion-state (&rest _)
(evil-motion-state))

(provide 'evil-keybindings)

Expand Down
8 changes: 4 additions & 4 deletions evil-macros.el
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ via KEY-VALUE pairs. BODY should evaluate to a list of values.
'(("(\\(evil-\\(?:ex-\\)?define-\
\\(?:[^ k][^ e][^ y]\\|[-[:word:]]\\{4,\\}\\)\\)\
\\>[ \f\t\n\r\v]*\\(\\(?:\\sw\\|\\s_\\)+\\)?"
(1 font-lock-keyword-face)
(2 font-lock-function-name-face nil t))
(1 'font-lock-keyword-face)
(2 'font-lock-function-name-face nil t))
("(\\(evil-\\(?:delay\\|narrow\\|signal\\|save\\|with\\(?:out\\)?\\)\
\\(?:-[-[:word:]]+\\)?\\)\\>\[ \f\t\n\r\v]+"
1 font-lock-keyword-face)
1 'font-lock-keyword-face)
("(\\(evil-\\(?:[-[:word:]]\\)*loop\\)\\>[ \f\t\n\r\v]+"
1 font-lock-keyword-face))))
1 'font-lock-keyword-face))))

(provide 'evil-macros)

Expand Down
9 changes: 0 additions & 9 deletions evil-pkg.el

This file was deleted.

Loading