Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ivy as a completion back end option #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions smex.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,37 @@
:group 'convenience
:link '(emacs-library-link :tag "Lisp File" "smex.el"))

(defcustom smex-completion-method 'ido
"Method to select a candidate from a list of strings."
:type '(choice
(const :tag "Ido" ido)
(const :tag "Ivy" ivy)))

(defcustom smex-auto-update t
"If non-nil, `Smex' checks for new commands each time it is run.
Turn it off for minor speed improvements on older systems."
:type 'boolean
:group 'smex)
:type 'boolean)

(defcustom smex-save-file (locate-user-emacs-file "smex-items" ".smex-items")
"File in which the smex state is saved between Emacs sessions.
Variables stored are: `smex-data', `smex-history'.
Must be set before initializing Smex."
:type 'string
:group 'smex)
:type 'string)

(defcustom smex-history-length 7
"Determines on how many recently executed commands
Smex should keep a record.
Must be set before initializing Smex."
:type 'integer
:group 'smex)
:type 'integer)

(defcustom smex-prompt-string "M-x "
"String to display in the Smex prompt."
:type 'string
:group 'smex)
:type 'string)

(defcustom smex-flex-matching t
"Enables Ido flex matching. On by default.
Set this to nil to disable fuzzy matching."
:type 'boolean
:group 'smex)
:type 'boolean)

(defvar smex-initialized-p nil)
(defvar smex-cache)
Expand Down Expand Up @@ -127,15 +128,37 @@ Set this to nil to disable fuzzy matching."
(setq commands (mapcar #'symbol-name commands))
(smex-read-and-run commands)))

(defvar smex-map (let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'minibuffer-complete)
(define-key map (kbd "C-h f") 'smex-describe-function)
(define-key map (kbd "C-h w") 'smex-where-is)
(define-key map (kbd "M-.") 'smex-find-function)
(define-key map (kbd "C-a") 'move-beginning-of-line)
map)
"Keymap used in the minibuffer.")

(defun smex-prepare-ido-bindings ()
(setq ido-completion-map
(make-composed-keymap '(smex-map ido-completion-map))))

(declare-function ivy-read "ext:ivy")
(declare-function ivy-done "ext:ivy")

(defun smex-completing-read (choices initial-input)
(let ((ido-completion-map ido-completion-map)
(ido-setup-hook (cons 'smex-prepare-ido-bindings ido-setup-hook))
(ido-enable-prefix nil)
(ido-enable-flex-matching smex-flex-matching)
(ido-max-prospects 10)
(minibuffer-completion-table choices))
(ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil
initial-input 'extended-command-history (car choices))))
(if (eq smex-completion-method 'ido)
(let ((ido-completion-map ido-completion-map)
(ido-setup-hook (cons 'smex-prepare-ido-bindings ido-setup-hook))
(ido-enable-prefix nil)
(ido-enable-flex-matching smex-flex-matching)
(ido-max-prospects 10)
(minibuffer-completion-table choices))
(ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil
initial-input 'extended-command-history (car choices)))
(require 'ivy)
(ivy-read (smex-prompt-with-prefix-arg) choices
:keymap smex-map
:history 'extended-command-history
:preselect (car choices))))

(defun smex-prompt-with-prefix-arg ()
(if (not current-prefix-arg)
Expand All @@ -150,13 +173,6 @@ Set this to nil to disable fuzzy matching."
(format "%d " (car current-prefix-arg)))))
smex-prompt-string)))

(defun smex-prepare-ido-bindings ()
(define-key ido-completion-map (kbd "TAB") 'minibuffer-complete)
(define-key ido-completion-map (kbd "C-h f") 'smex-describe-function)
(define-key ido-completion-map (kbd "C-h w") 'smex-where-is)
(define-key ido-completion-map (kbd "M-.") 'smex-find-function)
(define-key ido-completion-map (kbd "C-a") 'move-beginning-of-line))

;;--------------------------------------------------------------------------------
;; Cache and Maintenance

Expand Down Expand Up @@ -231,7 +247,8 @@ Set this to nil to disable fuzzy matching."
;;;###autoload
(defun smex-initialize ()
(interactive)
(unless ido-mode (smex-initialize-ido))
(when (eq smex-completion-method 'ido)
(unless ido-mode (smex-initialize-ido)))
(smex-load-save-file)
(smex-detect-new-commands)
(smex-rebuild-cache)
Expand All @@ -255,8 +272,8 @@ Set this to nil to disable fuzzy matching."
(error (if (smex-save-file-not-empty-p)
(error "Invalid data in smex-save-file (%s). Can't restore history."
smex-save-file)
(if (not (boundp 'smex-history)) (setq smex-history))
(if (not (boundp 'smex-data)) (setq smex-data))))))
(if (not (boundp 'smex-history)) (setq smex-history nil))
(if (not (boundp 'smex-data)) (setq smex-data nil))))))
(setq smex-history nil smex-data nil))))

(defsubst smex-save-file-not-empty-p ()
Expand Down Expand Up @@ -388,7 +405,9 @@ Returns nil when reaching the end of the list."

(defun smex-do-with-selected-item (fn)
(setq smex-custom-action fn)
(ido-exit-minibuffer))
(if (eq smex-completion-method 'ido)
(ido-exit-minibuffer)
(ivy-done)))

(defun smex-describe-function ()
(interactive)
Expand Down