我有一个Elisp函数,它需要一个参数(目前为止还好)。这个参数应该是列表中的一个项,而且不能是其他任何东西。
是否有一种方法可以显示列表,类似于“选择缓冲区”(如dired),用户可以浏览到该项并通过按回车键选择它,而不必手动输入字符串?
completing-read
来实现。然后您可以使用minibuffer-with-setup-hook
,在其中调用minibuffer-completion-help
,以便立即弹出*Completions*
缓冲区,以便用户可以单击选择。(defun foo (list)
(interactive)
(let ((arg (ido-completing-read "Select from list: " list))))
...)
Emacs的选择过程不像dired,但是对于Emacs用户来说,使用ido
或其他类似的替代工具从列表中选择是很常见的。您可以缩小搜索范围,在各种选择之间移动等。如果您想了解可以自定义哪些偏好设置,请输入M-x customize-group RET ido。
我喜欢使用弹出式菜单来处理这种事情:
(x-popup-menu
(list '(50 50) (selected-frame)) ;; where to popup
(list "Please choose" ;; the menu itself
(cons "" (mapcar (function (lambda (item) (cons item item)))
your-list-of-strings))))
顺便说一下,有人想要使用类似于Common Lisp的(mapcar 'cons your-list-of-strings your-list-of-strings)
,但是elisp只接受mapcar中的一元函数 :-(