Skip to content
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
84 changes: 47 additions & 37 deletions smex.el
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

;;; Commentary:

;; Quick start:
;; run (smex-initialize)
;; Smex is a global minor mode which replaces `execute-extended-command' with
;; `smex', which uses `ido-mode' to provide enhanced completion.
;;
;; Bind the following commands:
;; smex, smex-major-mode-commands
;; To enable, set the custom variable `smex-mode' to a non-nil value.
;;
;; Smex can also be enabled manually by executing the function `smex-mode'.
;;
;; For a detailed introduction see:
;; http://github.com/nonsequitur/smex/blob/master/README.markdown
Expand All @@ -35,25 +36,23 @@
:group 'extensions
:group 'convenience
:version "1.1"
:prefix "smex-"
:link '(emacs-library-link :tag "Lisp File" "smex.el"))

(defcustom smex-auto-update t
"If non-nil, `Smex' checks for new commands each time it is run.
"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)

(defcustom smex-save-file "~/.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
Variables stored are: `smex-data', `smex-history'."
:type 'file
:group 'smex)

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

Expand All @@ -63,13 +62,12 @@ Must be set before initializing Smex."
:group 'smex)

(defcustom smex-key-advice-ignore-menu-bar nil
"If non-nil, `smex-key-advice' ignores `menu-bar' bindings"
"If non-nil, `smex-key-advice' ignores `menu-bar' bindings."
:type 'boolean
:group 'smex)

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

Expand All @@ -83,7 +81,40 @@ Set this to nil to disable fuzzy matching."
;;--------------------------------------------------------------------------------
;; Smex Interface

;;;###autoload
(define-minor-mode smex-mode "Minor mode to supercharge M-x.
When `smex-mode' is active, `execute-extended-command' is
remapped to `smex'. Built on top of Ido, it provides a convenient
interface to your recently and most frequently used commands. And
to all the other commands, too.

No longer supports legacy save files; if you are still using the
old-style save file (\"~/smex.save\"), you will need to copy it
to the location in `smex-save-file'."
:group 'smex
:global t
:require 'smex
:keymap '(([remap execute-extended-command] . smex)
([?\M-X] . smex-major-mode-commands))
(if smex-mode
(progn
(unless ido-mode (ido-common-initialization))
(let ((save-file (expand-file-name smex-save-file)))
(if (file-readable-p save-file)
(with-temp-buffer
(insert-file-contents save-file)
(setq smex-history (read (current-buffer))
smex-data (read (current-buffer))))
(setq smex-history nil
smex-data nil))
(smex-detect-new-commands)
(smex-rebuild-cache)
(add-hook 'kill-emacs-hook 'smex-save-to-file))))
(smex-save-to-file)
(remove-hook 'kill-emacs-hook 'smex-save-to-file))

(defun smex ()
"Read a function name using ido-enhanced search and call it."
(interactive)
(if (smex-already-running)
(smex-update-and-rerun)
Expand Down Expand Up @@ -227,27 +258,6 @@ This function provides temporary means to aid the transition."
smex-save-file legacy-save-file))
(setq smex-save-file legacy-save-file)))))

;;;###autoload
(defun smex-initialize ()
(interactive)
(unless ido-mode (smex-initialize-ido))
(smex-detect-legacy-save-file)
(let ((save-file (expand-file-name smex-save-file)))
(if (file-readable-p save-file)
(with-temp-buffer
(insert-file-contents save-file)
(setq smex-history (read (current-buffer))
smex-data (read (current-buffer))))
(setq smex-history nil smex-data nil))
(smex-detect-new-commands)
(smex-rebuild-cache)
(add-hook 'kill-emacs-hook 'smex-save-to-file)))

(defun smex-initialize-ido ()
"Sets up a minimal Ido environment for `ido-completing-read'."
(ido-init-completion-maps)
(add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup))

(defun smex-save-history ()
"Updates `smex-history'"
(setq smex-history nil)
Expand Down Expand Up @@ -436,13 +446,13 @@ Returns nil when reaching the end of the list."
(let ((library-path (symbol-file mode))
(mode-name (symbol-name mode))
commands)

(string-match "\\(.+?\\)\\(-mode\\)?$" mode-name)
;; 'lisp-mode' -> 'lisp'
(setq mode-name (match-string 1 mode-name))
(if (string= mode-name "c") (setq mode-name "cc"))
(setq mode-name (regexp-quote mode-name))

(dolist (feature load-history)
(let ((feature-path (car feature)))
(when (and feature-path (or (equal feature-path library-path)
Expand Down