branch: elpa/evil commit 81b813e7ac229b0eb82d0a9adcf6e3950773b558 Author: Axel Forsman <axels...@gmail.com> Commit: Axel Forsman <axels...@gmail.com>
Specialize evil-sort for case of two arguments All current uses of evil-sort only pass two arguments, for which a conditional swap suffices. Also replace the implementation of evil-swap with cl-rotatef, since that is built-in and produces marginally better code for three or more variables. --- evil-commands.el | 8 +++---- evil-common.el | 70 +++++++++++++++++++++++--------------------------------- evil-core.el | 4 ++-- evil-tests.el | 22 ++++++++---------- 4 files changed, 43 insertions(+), 61 deletions(-) diff --git a/evil-commands.el b/evil-commands.el index eec7038604..6e35689e6c 100644 --- a/evil-commands.el +++ b/evil-commands.el @@ -4590,25 +4590,25 @@ and open a new buffer name or edit a certain FILE." "Increase current window height by COUNT." :repeat nil (interactive "p") - (evil-resize-window (+ (window-height) count))) + (enlarge-window count)) (evil-define-command evil-window-decrease-height (count) "Decrease current window height by COUNT." :repeat nil (interactive "p") - (evil-resize-window (- (window-height) count))) + (enlarge-window (- count))) (evil-define-command evil-window-increase-width (count) "Increase current window width by COUNT." :repeat nil (interactive "p") - (evil-resize-window (+ (window-width) count) t)) + (enlarge-window count t)) (evil-define-command evil-window-decrease-width (count) "Decrease current window width by COUNT." :repeat nil (interactive "p") - (evil-resize-window (- (window-width) count) t)) + (enlarge-window (- count) t)) (evil-define-command evil-window-set-height (count) "Set the height of the current window to COUNT." diff --git a/evil-common.el b/evil-common.el index b991b8697f..f8c3bbbaa6 100644 --- a/evil-common.el +++ b/evil-common.el @@ -286,36 +286,23 @@ return the value of that variable." (symbol-value val) val))) -(defmacro evil-swap (this that &rest vars) - "Swap the values of variables THIS and THAT. -If three or more arguments are given, the values are rotated. -E.g., (evil-swap A B C) sets A to B, B to C, and C to A." - `(progn - (setq ,this (prog1 ,that - (setq ,that ,this))) - ,@(when vars - `((evil-swap ,that ,@vars))))) - -(defmacro evil-sort (min max &rest vars) - "Place the smallest value in MIN and the largest in MAX. -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) '<))) - (setq ,min (pop ,sorted) - ,max (pop ,sorted) - ,@(apply #'append - (mapcar #'(lambda (var) - (list var `(pop ,sorted))) - vars)))))) +(eval-and-compile (defalias 'evil-swap #'cl-rotatef)) + +(defmacro evil-sort (&rest vars) + "Sort the symbol values of VARS. +Place the smallest value in the first argument and the largest in the +last, sorting in between." + (if (= (length vars) 2) + `(when (> ,@vars) (evil-swap ,@vars)) + (let ((sorted (make-symbol "sortvar"))) + `(let ((,sorted (sort (list ,@vars) #'<))) + (setq ,@(apply #'nconc + (mapcar (lambda (var) (list var `(pop ,sorted))) + vars))))))) (defun evil-vector-to-string (vector) - "Turn vector into a string, changing <escape> to '\\e'" - (mapconcat (lambda (c) - (if (equal c 'escape) - "\e" - (make-string 1 c))) + "Turn VECTOR into a string, changing <escape> to \"\\e\"." + (mapconcat (lambda (c) (if (eq c 'escape) "\e" (list c))) vector "")) @@ -2932,10 +2919,10 @@ a property list." (let ((beg (evil-normalize-position beg)) (end (evil-normalize-position end))) (when (and (numberp beg) (numberp end)) - (append (list (min beg end) (max beg end)) - (when (evil-type-p type) - (list type)) - properties)))) + (evil-sort beg end) + (nconc (list beg end) + (when (evil-type-p type) (list type)) + properties)))) (defun evil-range-p (object) "Whether OBJECT is a range." @@ -4024,18 +4011,17 @@ should be left-aligned for left justification." (define-key evil-list-view-mode-map [return] #'evil-list-view-goto-entry) (defmacro evil-with-view-list (&rest properties) - "Open new list view buffer. - + "Open a new list view buffer. PROPERTIES is a property-list which supports the following properties: -:name (required) The name of the buffer. -:mode-name (required) The name for the mode line. -:format (required) The value for `tabulated-list-format'. -:entries (required) The value for `tabulated-list-entries'. -:select-action (optional) A function for row selection. - It takes in a single parameter, which is the selected row's - vector value that is passed into `:entries'. -" +:name (required) The name of the buffer. +:mode-name (required) The name for the mode line. +:format (required) The value for `tabulated-list-format'. +:entries (required) The value for `tabulated-list-entries'. +:select-action (optional) A function for row selection. + It takes a single parameter, which is the + selected row's vector value that is + passed into `:entries'." (declare (indent defun) (debug t)) `(let ((bufname (concat "*" ,(plist-get properties :name) "*")) (inhibit-read-only t)) diff --git a/evil-core.el b/evil-core.el index 2205ea525c..3216c7a9ad 100644 --- a/evil-core.el +++ b/evil-core.el @@ -992,8 +992,8 @@ mode, in which case `evil-define-minor-mode-key' is used." `(keymapp ,keymap)) '(condition-case-unless-debug err (evil-define-key* ,state ,keymap ,key ,def ,@bindings) - (error "error in evil-define-key: %s" - (error-message-string err))) + (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)))))) diff --git a/evil-tests.el b/evil-tests.el index a32954d1cb..1dc6b9f64a 100644 --- a/evil-tests.el +++ b/evil-tests.el @@ -9229,25 +9229,21 @@ parameter set." '((a . b)))))) (ert-deftest evil-test-sort () - "Test `evil-sort' and `evil-swap'" + "Test `evil-sort'." :tags '(evil util) - (let (a b c d) - (ert-info ("Two elements") - (setq a 2 b 1) + (ert-info ("Two elements") + (let ((a 2) (b 1)) (evil-sort a b) (should (= a 1)) - (should (= b 2)) - (evil-swap a b) - (should (= a 2)) - (should (= b 1))) - (ert-info ("Three elements") - (setq a 3 b 1 c 2) + (should (= b 2)))) + (ert-info ("Three elements") + (let ((a 3) (b 1) (c 2)) (evil-sort a b c) (should (= a 1)) (should (= b 2)) - (should (= c 3))) - (ert-info ("Four elements") - (setq a 4 b 3 c 2 d 1) + (should (= c 3)))) + (ert-info ("Four elements") + (let ((a 4) (b 3) (c 2) (d 1)) (evil-sort a b c d) (should (= a 1)) (should (= b 2))