2012-04-13

emacs lisp: Getting User Input with Completion and Input History

Perm URL with updates: http://xahlee.org/emacs/elisp_idioms.html

Some commands provide input history, so that user can use key to enter previous input. (e.g. shell-commandAlt+!】) Also, some commands provide name completion (e.g. “dired” 【Ctrl+x d】)

The most useful are: read-string, read-file-name, read-regexp. Examples:

(defun test-fun (arg)
  "Prompt user to enter a string, with input history support."
  (interactive (list (read-string "Your name is:")) )
  (message "String is 「%s」." arg) )

(defun test-fun (arg)
  "Prompt user to enter a file path, with file name completion and input history support."
  (interactive (list (read-file-name "Open directory:")) )
  (message "Path is 「%s」." arg) )

(defun test-fun (arg)
  "Prompt user to enter a elisp regex and input history support."
  (interactive (list (read-regexp "Type a regex:")) )
  (message "Regex is 「%s」." arg) )

The most general command is read-from-minibuffer. All the above are implemented on top of it.

(info "(elisp) Minibuffers")