Emacs迷你缓冲区完成

时间:2012-07-26 11:55:12

标签: emacs elisp launcher tab-completion minibuffer

我有一个异步启动程序的函数:

(defun app (app-name)
  (interactive "sRun application: ")
  (async-shell-command app-name))

我有一个可供选择的所有可执行文件的列表。我希望app函数的行为为switch-to-buffer,为用户提供TAB完成功能。我如何在Emacs中使用迷你缓冲区?

2 个答案:

答案 0 :(得分:8)

使用completing-read命令。该功能看起来像

(defun app ()
  (interactive)
  (let ((app-name (completing-read "Run application: " program-list)))
    (async-shell-command app-name)))

可能更惯用的是使用interactive而不是根据Emacs Lisp Idioms: Prompting for User Input分配给变量:

(defun app (app-name)
  (interactive (list (completing-read "Run application: " app-list)))
  (async-shell-command app-name))

如果您根据Run a program from Emacs and don't wait for output不关心流程输出,也可以使用(start-process app-name nil app-name)代替(async-shell-command app-name)


有关在Emacs中完成的更多建议,请参阅Minibuffer Completion,有关从Emacs调用进程的Asynchronous Processes,请参阅GNU手册。

答案 1 :(得分:4)

如果您希望完成可能的shell命令而无需自己维护列表,并且您使用的是Emacs 23或更新版本,则可以使用read-shell-command

(defun app (app-name)
  (interactive (list (read-shell-command "Run application: ")))
  (async-shell-command app-name))