Preselect last command in menus; reverse universal arg behavior

+ The universal argument tells a menu to use the last run command, if
  available.
+ If a last run command exists, preselect it in the menu.
This commit is contained in:
Henrik Lissner 2018-03-19 02:02:11 -04:00
parent 46dbf8f490
commit ab180cd51e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -12,12 +12,12 @@ PROMPT (a string) and COMMAND (a list of command plists; see `def-menu!').")
(defun doom-menu-read-default (prompt commands) (defun doom-menu-read-default (prompt commands)
"Default method for displaying a completion-select prompt." "Default method for displaying a completion-select prompt."
(completing-read prompt (mapcar #'car commands))) (completing-read prompt (mapcar #'car commands) nil nil nil nil (car doom-menu-last-command)))
;;;###autoload ;;;###autoload
(defun doom--menu-read (prompt commands) (defun doom--menu-read (prompt commands)
(if-let* ((choice (funcall doom-menu-display-fn prompt commands))) (if-let* ((choice (funcall doom-menu-display-fn prompt commands)))
(cdr (assoc choice commands)) (assoc choice commands)
(user-error "Aborted"))) (user-error "Aborted")))
;;;###autoload ;;;###autoload
@ -82,18 +82,18 @@ lisp form.
`(cl-sort ,commands #'string-lessp :key #'car) `(cl-sort ,commands #'string-lessp :key #'car)
commands) commands)
,(format "Menu for %s" name)) ,(format "Menu for %s" name))
(defun ,name (arg) (defun ,name (arg command)
,(concat ,(concat
(if (stringp desc) (concat desc "\n\n")) (if (stringp desc) (concat desc "\n\n"))
"This is a command dispatcher. It will rerun the last command on\n" "This is a command dispatcher. It will rerun the last command on\n"
"consecutive executions. If ARG (universal argument) is non-nil\n" "consecutive executions. If ARG (universal argument) is non-nil\n"
"then it always prompt you.") "then it always prompt you.")
(interactive "P") (declare (interactive-only t))
(interactive
(list current-prefix-arg
(progn
(unless ,commands-var (unless ,commands-var
(user-error "The '%s' menu is empty" ',name)) (user-error "The '%s' menu is empty" ',name))
(doom--menu-exec
(or (unless arg doom-menu-last-command)
(setq doom-menu-last-command
(doom--menu-read (doom--menu-read
,prop-prompt ,prop-prompt
(or (cl-remove-if-not (or (cl-remove-if-not
@ -111,8 +111,12 @@ lisp form.
(or (or (not when) (eval when)) (or (or (not when) (eval when))
(or (not unless) (not (eval unless))) (or (not unless) (not (eval unless)))
(and (stringp project) (and (stringp project)
(file-in-directory-p buffer-file-name project-root)))))))) (file-in-directory-p (or buffer-file-name default-directory)
project-root))))))))
,commands-var) ,commands-var)
(user-error "No commands available here")))) (user-error "No commands available here"))))))
(user-error "No command selected"))))))) (doom--menu-exec
(cdr (or (when arg doom-menu-last-command)
(setq doom-menu-last-command command)
(user-error "No command selected"))))))))