Posts Tagged ‘Smex’

Spacebar replacement for ido

Wednesday, March 18th, 2009

As a follow-up to my previous post, if the behaviour of space in ido-mode annoys you then put the following in your .emacs file:

(defun expand-fuzzy-match (str reg)
  "Returns the string starting at the beginning and containing all of
the characters in 'reg' in order.  Returns the smallest such string."
  (let ((new-reg (concat
                   "\\("
		   (apply 'concat
	           (mapcar (lambda (x) (concat ".*?" (string x))) reg)) "\\)")))
    (string-match new-reg str)
    (match-string 1 str)))
 
 
(defun ido-expand-match ()
  "Uses expand-fuzzy-match to expand the string in the minibuffer in ido"
  (interactive)
  (if (= 1 (length ido-matches))
      (ido-complete)
    (let ((full-str (expand-fuzzy-match (car ido-matches) ido-text)))
      (if (string-equal full-str ido-text) (ido-complete)
	(progn
	  (delete-region (minibuffer-prompt-end) (point))
	  (insert full-str))))))
 
(add-hook 'ido-setup-hook 'ido-my-keys)
 
(defun ido-my-keys ()
  "Add my keybindings for ido."
  (define-key ido-completion-map " " 'ido-expand-match))

This will change ’space’ from always using ido-complete to first expanding your search using the currently-selected option and then calls ido-complete if used again or if the expansion is the same as the original text.