Skip to content

Commit

Permalink
Various code cleanups
Browse files Browse the repository at this point in the history
The original motivation was to fix the misuse of `eval` in `evil-delay`
by turning it into a macro (name `evil-with-delay`).  But it includes
various generic changes such as prefering #' to quote function names and
fixing some incorrect uses of ' in docstrings (many warnings remain about
this).

The patch also enables `lexical-binding` in the remaining files.
`lexical-binding` in `evil-ex.el` has had a tumultuous life, the last commit
of which sets it explicitly to nil while stating confusingly in the commit
message that it re-enables it.  In any case, I still found some changes
needed to account for `lexical-binding`, so there might be more.

Detailed changes below.

* evil-common.el (evil-unquote): Delete function, not used (luckily:
it reeked of a bad hack to work around a misunderstood bug).
(evil--with-delay): New helper function.
(evil-with-delay): New macro to replace `evil-delay`.
(evil-delay): Rewrite using `evil-with-delay` and mark as obsolete.
(evil-signal-at-bob-or-eob): Fix typos in docstring.

* evil-core.el (window-configurakion-change-hook): Add FIXME.
(evil-define-key): Use `evil-with-delay`.

* evil-states.el (evil-visual-activate-hook): Use `evil-with-delay`.

* evil-commands.el (evil-match): Use `pcase` since the branch patterns
used are those of `pcase` rather than those of `cl-case`.
(evil-execute-in-normal-state): Use `evil-with-delay`.

* evil-digraphs.el (evil-digraphs-table-user): Remove redundant
`:group` arg.

* evil-ex.el: Enable `lexical-binding` like the last commit that
touched this cookie said that it was doing (even though it didn't).
(evil-ex-info-string): Declare.
(evil-ex-update): Mark `end` and  `len` as ignored.
(evil-ex-init-shell-argument-completion): Mark `arg` as ignored.
(evil-flatten-syntax-tree): Mark `char` as ignored.
(evil-parser): Rename `context` to `evil--context` and declare it
as dynbound.  Remove unused var `last`.
Move shared `setq result` out of some `if`s and `cond`s.

* evil-jumps.el: Remove redundant  :group` arguments.

* evil-macros.el (evil-define-interactive-code): Move shared `setq func`
out of `cond`.  Move the insertion of quote around `func` to the `cond`
so the `quote` is not incorrectly added around lambda forms.

* evil-pkg.el: Remove file.  Move its contents to the pseudo headers of
`evil.el` so (M|NonGNU)ELPA can auto-generate this file appropriately.

* evil.el: Enable `lexical-binding`.  Synchronize metadata with what
was in `evil-pkg.el`.

* evil-tests.el: Enable `lexical-binding`.
(evil-test-change-state): Move `let` to obviate the need for `setq`.
Remove unused vars `keymap` and `local-keymap`.
(evil-test-auxiliary-maps): Rename `map` to `evil--map` and declare it
as dynbound so `evil-define-key` can access it.
(evil-test-exclusive-type): Mark `third-line` as unused.
(evil-test-text-object): Mark `type` arg as unused.
(evil-with-both-search-modules): Move macro before its first use.
(evil-test-properties): Rename `alist` to `evil--alist` and declare it
as dynbound so `evil-put-property` can access it.

* evil-command-window.el (evil-command-window-draw-prefix):
Mark `ignored` as, well, ignored.
  • Loading branch information
monnier authored and tomdl89 committed Oct 5, 2022
1 parent b46acf2 commit ba5b322
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 280 deletions.
2 changes: 1 addition & 1 deletion evil-command-window.el
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function to execute."
(push result evil-search-backward-history)))
(evil-search result forward evil-regexp-search))))

(defun evil-command-window-draw-prefix (&rest ignored)
(defun evil-command-window-draw-prefix (&rest _ignored)
"Display `evil-command-window-cmd-key' as a prefix to the current line.
Parameters passed in through IGNORED are ignored."
(let ((prefix (propertize evil-command-window-cmd-key
Expand Down
76 changes: 39 additions & 37 deletions evil-commands.el
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ If called after a change operator, i.e. cw or cW,
then both behave like ce or cE.

If point is at the end of the buffer and cannot be moved signal
'end-of-buffer is raised.
"
`end-of-buffer' is raised."
:type exclusive
(let ((thing (if bigword 'evil-WORD 'evil-word))
(orig (point))
Expand Down Expand Up @@ -645,7 +644,7 @@ and jump to the corresponding one."
(evil--next-flyspell-error nil)))

(evil-define-motion evil-previous-open-paren (count)
"Go to [count] previous unmatched '('."
"Go to [count] previous unmatched \"(\"."
:type exclusive
(evil-up-paren ?\( ?\) (- (or count 1))))

Expand Down Expand Up @@ -2944,7 +2943,7 @@ Calls `evil-complete-previous-line-func'."

(defun evil-repeat-search (flag)
"Called to record a search command.
FLAG is either 'pre or 'post if the function is called before resp.
FLAG is either `pre' or `post' if the function is called before resp.
after executing the command."
(cond
((and (evil-operator-state-p) (eq flag 'pre))
Expand Down Expand Up @@ -3129,7 +3128,7 @@ not interfere with another."
(if (null list)
(user-error
"Enable one of the following modes for folding to work: %s"
(mapconcat 'symbol-name (mapcar 'caar evil-fold-list) ", "))
(mapconcat #'symbol-name (mapcar #'caar evil-fold-list) ", "))
(let* ((modes (caar list)))
(if (evil--mode-p modes)
(let* ((actions (cdar list))
Expand Down Expand Up @@ -4872,44 +4871,47 @@ This var stores the eol position, so it can be restored when necessary.")
"No insert-state repeat info is recorded after executing in normal state.
Restore the disabled repeat hooks on insert-state exit."
(evil-repeat-stop)
(add-hook 'pre-command-hook 'evil-repeat-pre-hook)
(add-hook 'post-command-hook 'evil-repeat-post-hook)
(remove-hook 'evil-insert-state-exit-hook 'evil--restore-repeat-hooks))
(add-hook 'pre-command-hook #'evil-repeat-pre-hook)
(add-hook 'post-command-hook #'evil-repeat-post-hook)
(remove-hook 'evil-insert-state-exit-hook #'evil--restore-repeat-hooks))

(defvar evil--execute-normal-return-state nil
"The state to return to after executing in normal state.")

(defun evil-execute-in-normal-state ()
"Execute the next command in Normal state."
(interactive)
(evil-delay '(not (memq this-command
'(nil
evil-execute-in-normal-state
evil-replace-state
evil-use-register
digit-argument
negative-argument
universal-argument
universal-argument-minus
universal-argument-more
universal-argument-other-key)))
`(progn
(with-current-buffer ,(current-buffer)
(when (and evil--execute-normal-eol-pos
(= (point) (1- evil--execute-normal-eol-pos))
(not (memq this-command '(evil-insert
evil-goto-mark))))
(forward-char))
(unless (memq evil-state '(replace insert))
(evil-change-state ',evil-state))
(when (eq 'insert evil-state)
(remove-hook 'pre-command-hook 'evil-repeat-pre-hook)
(remove-hook 'post-command-hook 'evil-repeat-post-hook)
(add-hook 'evil-insert-state-exit-hook 'evil--restore-repeat-hooks))
(setq evil-move-cursor-back ',evil-move-cursor-back
evil-move-beyond-eol ',evil-move-beyond-eol
evil-execute-normal-keys nil)))
'post-command-hook)
(let ((buf (current-buffer))
(estate evil-state)
(emcb evil-move-cursor-back)
(embe evil-move-beyond-eol))
(evil-with-delay (not (memq this-command
'(nil
evil-execute-in-normal-state
evil-replace-state
evil-use-register
digit-argument
negative-argument
universal-argument
universal-argument-minus
universal-argument-more
universal-argument-other-key)))
post-command-hook
(with-current-buffer buf
(when (and evil--execute-normal-eol-pos
(= (point) (1- evil--execute-normal-eol-pos))
(not (memq this-command '(evil-insert
evil-goto-mark))))
(forward-char))
(unless (memq evil-state '(replace insert))
(evil-change-state estate))
(when (eq 'insert evil-state)
(remove-hook 'pre-command-hook #'evil-repeat-pre-hook)
(remove-hook 'post-command-hook #'evil-repeat-post-hook)
(add-hook 'evil-insert-state-exit-hook #'evil--restore-repeat-hooks))
(setq evil-move-cursor-back emcb
evil-move-beyond-eol embe
evil-execute-normal-keys nil))))
(setq evil-insert-count nil
evil--execute-normal-return-state evil-state
evil--execute-normal-eol-pos (when (eolp) (point))
Expand All @@ -4922,7 +4924,7 @@ Restore the disabled repeat hooks on insert-state exit."
(defun evil-stop-execute-in-emacs-state ()
(when (and (not (eq this-command #'evil-execute-in-emacs-state))
(not (minibufferp)))
(remove-hook 'post-command-hook 'evil-stop-execute-in-emacs-state)
(remove-hook 'post-command-hook #'evil-stop-execute-in-emacs-state)
(when (buffer-live-p evil-execute-in-emacs-state-buffer)
(with-current-buffer evil-execute-in-emacs-state-buffer
(if (and (eq evil-previous-state 'visual)
Expand Down
65 changes: 37 additions & 28 deletions evil-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,38 @@ no arguments. In Emacs 23.2 and newer, it takes one argument."
"Git commit 222b791")

;; macro helper
(eval-and-compile
(defun evil-unquote (exp)
"Return EXP unquoted."
(while (eq (car-safe exp) 'quote)
(setq exp (cadr exp)))
exp))
(defun evil--with-delay (cond-fun body-fun hook &optional append local name)
(if (and cond-fun (funcall cond-fun))
(funcall body-fun)
(let* ((name (or name (format "evil-delay-in-%s" hook)))
(fun (make-symbol name)))
(fset fun (lambda (&rest _)
(when (or (null cond-fun) (funcall cond-fun))
(remove-hook hook fun local)
(funcall body-fun))))
(put fun 'permanent-local-hook t)
(add-hook hook fun append local))))

(defmacro evil-with-delay (condition hook &rest body)
"Execute BODY when CONDITION becomes true, checking with HOOK.
HOOK can be a simple symbol or it can be of the form
\(HOOK APPEND LOCAL NAME) where:
NAME specifies the name of the entry added to HOOK.
If APPEND is non-nil, the entry is appended to the hook.
If LOCAL is non-nil, the buffer-local value of HOOK is modified."
(declare (debug (form sexp body)) (indent 2))
(unless (consp hook) (setq hook (list hook)))
`(evil--with-delay ,(if condition `(lambda () ,condition))
(lambda () ,@body)
,@(mapcar #'macroexp-quote hook)))

(defun evil-delay (condition form hook &optional append local name)
"Execute FORM when CONDITION becomes true, checking with HOOK.
NAME specifies the name of the entry added to HOOK. If APPEND is
non-nil, the entry is appended to the hook. If LOCAL is non-nil,
the buffer-local value of HOOK is modified."
(if (and (not (booleanp condition)) (eval condition))
(eval form)
(let* ((name (or name (format "evil-delay-form-in-%s" hook)))
(fun (make-symbol name))
(condition (or condition t)))
(fset fun `(lambda (&rest args)
(when ,condition
(remove-hook ',hook #',fun ',local)
,form)))
(put fun 'permanent-local-hook t)
(add-hook hook fun append local))))
(declare (obsolete evil-with-delay "2022"))
(eval `(evil-with-delay ,condition (,hook ,append ,local ,name) ,form) t))
(put 'evil-delay 'lisp-indent-function 2)

;;; List functions
Expand Down Expand Up @@ -139,7 +148,7 @@ otherwise add at the end of the list."
"Delete by side-effect all items satisfying PREDICATE in LIST.
Stop when reaching POINTER. If the first item satisfies PREDICATE,
there is no way to remove it by side-effect; therefore, write
\(setq foo (evil-filter-list 'predicate foo)) to be sure of
\(setq foo (evil-filter-list #\\='predicate foo)) to be sure of
changing the value of `foo'."
(let ((tail list) elt head)
(while (and tail (not (eq tail pointer)))
Expand Down Expand Up @@ -305,7 +314,7 @@ If three or more arguments are given, place the smallest
value in the first argument and the largest in the last,
sorting in between."
(let ((sorted (make-symbol "sortvar")))
`(let ((,sorted (sort (list ,min ,max ,@vars) '<)))
`(let ((,sorted (sort (list ,min ,max ,@vars) #'<)))
(setq ,min (pop ,sorted)
,max (pop ,sorted)
,@(apply #'append
Expand Down Expand Up @@ -374,11 +383,11 @@ sorting in between."
,(when (and command doc-form)
`(put ',command 'function-documentation ,doc-form))
;; set command properties for symbol or lambda function
(let ((func ',(if (and (null command) body)
`(lambda ,args
,interactive
,@body)
command)))
(let ((func #',(if (and (null command) body)
`(lambda ,args
,interactive
,@body)
command)))
(apply #'evil-set-command-properties func ',keys)
func))))

Expand Down Expand Up @@ -413,7 +422,7 @@ See also `evil-get-command-property'."
To set multiple properties at once, see
`evil-set-command-properties' and `evil-add-command-properties'."
(evil-put-property 'evil-command-properties command property value))
(defalias 'evil-put-command-property 'evil-set-command-property)
(defalias 'evil-put-command-property #'evil-set-command-property)

(defun evil-add-command-properties (command &rest properties)
"Add PROPERTIES to COMMAND.
Expand Down Expand Up @@ -1203,9 +1212,9 @@ the loop immediately quits. See also `evil-loop'.

(defun evil-signal-at-bob-or-eob (&optional count)
"Signal error if `point' is at boundaries.
If `point' is at bob and COUNT is negative this function signal
'beginning-of-buffer. If `point' is at eob and COUNT is positive
this function singal 'end-of-buffer. This function should be used
If `point' is at bob and COUNT is negative this function signals
`beginning-of-buffer'. If `point' is at eob and COUNT is positive
this function signals `end-of-buffer'. This function should be used
in motions. COUNT defaults to 1."
(setq count (or count 1))
(cond
Expand Down
58 changes: 30 additions & 28 deletions evil-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
emulation-mode-map-alists))
(evil-initialize-local-keymaps)
;; restore the proper value of `major-mode' in Fundamental buffers
(when (eq major-mode 'turn-on-evil-mode)
(setq major-mode 'fundamental-mode))
(when (eq major-mode #'turn-on-evil-mode)
(setq major-mode #'fundamental-mode))
(when (minibufferp)
(setq-local evil-default-state 'insert)
(setq-local evil-echo-state nil))
Expand All @@ -138,16 +138,16 @@
;; initialization is only for the case when `evil-local-mode' is
;; called directly for the first time in a buffer.
(unless evil-state (evil-initialize-state))
(add-hook 'input-method-activate-hook 'evil-activate-input-method t t)
(add-hook 'input-method-deactivate-hook 'evil-deactivate-input-method t t)
(add-hook 'activate-mark-hook 'evil-visual-activate-hook nil t)
(add-hook 'pre-command-hook 'evil-repeat-pre-hook)
(add-hook 'post-command-hook 'evil-repeat-post-hook))
(add-hook 'input-method-activate-hook #'evil-activate-input-method t t)
(add-hook 'input-method-deactivate-hook #'evil-deactivate-input-method t t)
(add-hook 'activate-mark-hook #'evil-visual-activate-hook nil t)
(add-hook 'pre-command-hook #'evil-repeat-pre-hook)
(add-hook 'post-command-hook #'evil-repeat-post-hook))
(t
(evil-refresh-mode-line)
(remove-hook 'activate-mark-hook 'evil-visual-activate-hook t)
(remove-hook 'input-method-activate-hook 'evil-activate-input-method t)
(remove-hook 'input-method-deactivate-hook 'evil-deactivate-input-method t)
(remove-hook 'activate-mark-hook #'evil-visual-activate-hook t)
(remove-hook 'input-method-activate-hook #'evil-activate-input-method t)
(remove-hook 'input-method-deactivate-hook #'evil-deactivate-input-method t)
(evil-change-state nil))))

;; Make the variable permanent local. This is particular useful in
Expand Down Expand Up @@ -375,7 +375,8 @@ then this function does nothing."

;; Refresh cursor color.
;; Cursor color can only be set for each frame but not for each buffer.
(add-hook 'window-configuration-change-hook 'evil-refresh-cursor)
;; FIXME: Shouldn't this belong in `evil-(local-)mode'?
(add-hook 'window-configuration-change-hook #'evil-refresh-cursor)
(defadvice select-window (after evil activate)
(evil-refresh-cursor))

Expand Down Expand Up @@ -453,14 +454,14 @@ then this function does nothing."
This allows input methods to be used in normal-state."
`(unwind-protect
(progn
(remove-hook 'input-method-activate-hook 'evil-activate-input-method t)
(remove-hook 'input-method-activate-hook #'evil-activate-input-method t)
(remove-hook 'input-method-deactivate-hook
'evil-deactivate-input-method t)
#'evil-deactivate-input-method t)
,@body)
(progn
(add-hook 'input-method-activate-hook 'evil-activate-input-method nil t)
(add-hook 'input-method-activate-hook #'evil-activate-input-method nil t)
(add-hook 'input-method-deactivate-hook
'evil-deactivate-input-method nil t))))
#'evil-deactivate-input-method nil t))))

(defadvice toggle-input-method (around evil)
"Refresh `evil-input-method'."
Expand Down Expand Up @@ -1048,18 +1049,19 @@ mode, in which case `evil-define-minor-mode-key' is used."
((and (consp keymap) (eq (car keymap) 'quote))
`(evil-define-minor-mode-key ,state ,keymap ,key ,def ,@bindings))
(t
`(evil-delay ',(if (symbolp keymap)
`(and (boundp ',keymap) (keymapp ,keymap))
`(keymapp ,keymap))
'(condition-case-unless-debug err
(evil-define-key* ,state ,keymap ,key ,def ,@bindings)
(error
(message "error in evil-define-key: %s"
(error-message-string err))))
'after-load-functions t nil
(format "evil-define-key-in-%s"
',(if (symbolp keymap) keymap 'keymap))))))
(defalias 'evil-declare-key 'evil-define-key)
`(evil-with-delay ,(if (symbolp keymap)
`(and (boundp ',keymap) (keymapp ,keymap))
`(keymapp ,keymap))
(after-load-functions t nil
,(format "evil-define-key-in-%s"
(if (symbolp keymap) keymap
'keymap)))
(condition-case-unless-debug err
(evil-define-key* ,state ,keymap ,key ,def ,@bindings)
(error
(message "error in evil-define-key: %s"
(error-message-string err))))))))
(defalias 'evil-declare-key #'evil-define-key)

(defun evil-define-key* (state keymap key def &rest bindings)
"Create a STATE binding from KEY to DEF for KEYMAP.
Expand Down Expand Up @@ -1101,7 +1103,7 @@ The use is nearly identical to `evil-define-key' with the
exception that this is a function and not a macro (and so will
not be expanded when compiled which can have unintended
consequences). `evil-define-key*' also does not defer any
bindings like `evil-define-key' does using `evil-delay'. This
bindings like `evil-define-key' does using `evil-with-delay'. This
allows errors in the bindings to be caught immediately, and makes
its behavior more predictable."
(declare (indent defun))
Expand Down
3 changes: 1 addition & 2 deletions evil-digraphs.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ where the digraph is a list of two characters.
See also `evil-digraphs-table'."
:type '(alist :key-type (list character character)
:value-type character)
:require 'evil-digraphs
:group 'evil-digraphs)
:require 'evil-digraphs)

(defconst evil-digraphs-table
'(((?N ?U) . ?\x00)
Expand Down
Loading

0 comments on commit ba5b322

Please sign in to comment.