unimpaired: turn functions into proper motions

Use evil-define-motion macro, which sets :motion repeat, set :jump t,
and add support for counts.
This commit is contained in:
Dominik Honnef 2021-12-16 13:55:29 +01:00 committed by Youmu
parent 88242de2a6
commit 30c851ef48
1 changed files with 27 additions and 31 deletions

View File

@ -56,39 +56,32 @@
"Turn on `evil-collection-unimpaired-mode'." "Turn on `evil-collection-unimpaired-mode'."
(evil-collection-unimpaired-mode 1)) (evil-collection-unimpaired-mode 1))
(defun evil-collection-unimpaired-next-error () (evil-define-motion evil-collection-unimpaired-next-error (count)
"Go to next error." "Go to next error."
(interactive) :jump t
(setq count (or count 1))
(cond (cond
((and (bound-and-true-p flycheck-mode) ((and (bound-and-true-p flycheck-mode)
(fboundp 'flycheck-next-error)) (fboundp 'flycheck-next-error))
(flycheck-next-error)) (flycheck-next-error count))
((and (bound-and-true-p flymake-mode) ((and (bound-and-true-p flymake-mode)
(fboundp 'flymake-goto-next-error)) (fboundp 'flymake-goto-next-error))
(flymake-goto-next-error)) (flymake-goto-next-error count))
(:default (:default
(message "No linting modes are on.")))) (message "No linting modes are on."))))
(defun evil-collection-unimpaired-previous-error () (evil-define-motion evil-collection-unimpaired-previous-error (count)
"Go to previous error." "Go to previous error."
(interactive) :jump t
(cond (evil-collection-unimpaired-next-error (- (or count 1))))
((and (bound-and-true-p flycheck-mode)
(fboundp 'flycheck-previous-error))
(flycheck-previous-error))
((and (bound-and-true-p flymake-mode)
(fboundp 'flymake-goto-prev-error))
(flymake-goto-prev-error))
(:default
(message "No linting modes are on."))))
(defun evil-collection-unimpaired--flycheck-count-errors () (defun evil-collection-unimpaired--flycheck-count-errors ()
"Count the number of flycheck errors." "Count the number of flycheck errors."
(length (delete-dups (mapcar 'flycheck-error-line flycheck-current-errors)))) (length (delete-dups (mapcar 'flycheck-error-line flycheck-current-errors))))
(defun evil-collection-unimpaired-first-error () (evil-define-motion evil-collection-unimpaired-first-error ()
"Go to the first error." "Go to the first error."
(interactive) :jump t
(cond (cond
((and (bound-and-true-p flycheck-mode) ((and (bound-and-true-p flycheck-mode)
(fboundp 'flycheck-first-error)) (fboundp 'flycheck-first-error))
@ -98,9 +91,9 @@
(:default (:default
(message "No linting modes are on.")))) (message "No linting modes are on."))))
(defun evil-collection-unimpaired-last-error () (evil-define-motion evil-collection-unimpaired-last-error ()
"Go to the last error." "Go to the last error."
(interactive) :jump t
(cond (cond
((and (bound-and-true-p flycheck-mode) ((and (bound-and-true-p flycheck-mode)
(fboundp 'flycheck-first-error)) (fboundp 'flycheck-first-error))
@ -113,19 +106,24 @@
(defconst evil-collection-unimpaired--SCM-conflict-marker "^\\(@@@ .* @@@\\|[<=>]\\{7\\}\\)" (defconst evil-collection-unimpaired--SCM-conflict-marker "^\\(@@@ .* @@@\\|[<=>]\\{7\\}\\)"
"A regexp to match SCM conflict marker.") "A regexp to match SCM conflict marker.")
(defun evil-collection-unimpaired-previous-SCM-conflict-marker () (evil-define-motion evil-collection-unimpaired-previous-SCM-conflict-marker (count)
"Go to the previous SCM conflict marker or diff/patch hunk." "Go to the previous SCM conflict marker or diff/patch hunk."
(interactive) :jump t
(search-backward-regexp evil-collection-unimpaired--SCM-conflict-marker nil t) (evil-collection-unimpaired-next-SCM-conflict-marker (- (or count 1))))
(move-beginning-of-line nil))
(defun evil-collection-unimpaired-next-SCM-conflict-marker () (evil-define-motion evil-collection-unimpaired-next-SCM-conflict-marker (count)
"Go to the next SCM conflict marker or diff/patch hunk." "Go to the next SCM conflict marker or diff/patch hunk."
(interactive) :jump t
(forward-line 1) (evil-motion-loop (dir (or count 1))
(when (not (search-forward-regexp evil-collection-unimpaired--SCM-conflict-marker nil t)) (cond
(forward-line -1)) ((> dir 0)
(move-beginning-of-line nil)) (forward-line 1)
(when (not (search-forward-regexp evil-collection-unimpaired--SCM-conflict-marker nil t))
(forward-line -1))
(move-beginning-of-line nil))
(t
(search-backward-regexp evil-collection-unimpaired--SCM-conflict-marker nil t)
(move-beginning-of-line nil)))))
(defun evil-collection-unimpaired-paste-above () (defun evil-collection-unimpaired-paste-above ()
"Paste above current line with preserving indentation." "Paste above current line with preserving indentation."
@ -230,8 +228,6 @@
(defun evil-collection-unimpaired-setup () (defun evil-collection-unimpaired-setup ()
"Set up unimpaired-like bindings." "Set up unimpaired-like bindings."
(global-evil-collection-unimpaired-mode 1) (global-evil-collection-unimpaired-mode 1)
(evil-add-command-properties 'evil-collection-unimpaired-next-error :repeat nil)
(evil-add-command-properties 'evil-collection-unimpaired-previous-error :repeat nil)
(evil-collection-define-key 'normal 'evil-collection-unimpaired-mode-map (evil-collection-define-key 'normal 'evil-collection-unimpaired-mode-map
"[b" 'evil-prev-buffer "[b" 'evil-prev-buffer
"]b" 'evil-next-buffer "]b" 'evil-next-buffer