From f21d1e87ff5ebbd0f30a45a0b97e90bd5f160dcf Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Sat, 29 May 2021 05:56:59 +0900 Subject: [PATCH] Add smex-ido-M-x and smex-simple-M-x instead of old smex command Initially smex was designed as an alternative to the excellent IDO-based M-x(execute-extended-command) command. However, there is actually a demand for another complementary interface like helm-smex. These days, vertico and selectrum are examples, but they replace the completing-read command, so you don't need to create a separate package like helm-smex. Add smex-M-x, smex-ido-M-x and smex-simple-M-x in this commit. Traditional smex users believe that smex depends on IDO, so it's an alias for smex-ido-M-x. By binding the key to smex-M-x, the new user will automatically select either IDO or completing-read according to the state of ido-mode. --- smex.el | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/smex.el b/smex.el index 6d4a72b..c9dd395 100644 --- a/smex.el +++ b/smex.el @@ -20,7 +20,7 @@ ;; run (smex-initialize) ;; ;; Bind the following commands: -;; smex, smex-major-mode-commands +;; smex-M-x, smex-major-mode-commands ;; ;; For a detailed introduction see: ;; http://github.com/nonsequitur/smex/blob/master/README.markdown @@ -84,7 +84,8 @@ Set this to nil to disable fuzzy matching." ;; Smex Interface ;;;###autoload -(defun smex () +(defun smex-ido-M-x () + "Read and execute a command using IDO." (interactive) (unless smex-initialized-p (smex-initialize)) @@ -95,6 +96,29 @@ Set this to nil to disable fuzzy matching." (smex-update)) (smex-read-and-run smex-ido-cache))) +;;;###autoload +(define-obsolete-function-alias 'smex #'smex-ido-M-x "4.0") + +(defun smex-simple-M-x () + "Read and execute a command using `completing-read'. + +Simple function is compatible with `completing-read' extensions like vertico and Selectrum." + (unless smex-initialized-p + (smex-initialize)) + (and smex-auto-update + (smex-detect-new-commands) + (smex-update)) + (let ((command (completing-read "M-x " smex-ido-cache))) + (smex--execute-command command))) + +;;;###autoload +(defun smex-M-x () + "Read a command name and execute it." + (interactive) + (if ido-mode + (smex-ido-M-x) + (smex-simple-M-x))) + (defun smex-already-running () (and (boundp 'ido-choice-list) (eql ido-choice-list smex-ido-cache) @@ -105,16 +129,22 @@ Set this to nil to disable fuzzy matching." (lambda (_) (smex-update) (smex-read-and-run smex-ido-cache ido-text)))) (defun smex-read-and-run (commands &optional initial-input) - (let* ((chosen-item-name (smex-completing-read commands initial-input)) - (chosen-item (intern chosen-item-name))) + "Select a command name from COMMANDS with IDO and execute it. +INITIAL-INPUT is for initial input value of `ido-completing-read'." + (let ((chosen-item-name (smex-completing-read commands initial-input))) + (smex--execute-command chosen-item-name))) + +(defun smex--execute-command (command-name) + "Execute COMMAND-NAME and record Smex cache." + (let ((command (intern command-name))) (if smex-custom-action (let ((action smex-custom-action)) (setq smex-custom-action nil) - (funcall action chosen-item)) + (funcall action command)) (unwind-protect (with-no-warnings ; Don't warn about interactive use of `execute-extended-command' - (execute-extended-command current-prefix-arg chosen-item-name)) - (smex-rank chosen-item))))) + (execute-extended-command current-prefix-arg command-name)) + (smex-rank command))))) ;;;###autoload (defun smex-major-mode-commands ()