【问题标题】:Elisp function select argument from listElisp 函数从列表中选择参数
【发布时间】:2013-11-04 16:34:18
【问题描述】:

我有一个 Elisp 函数,它接受一个参数(到目前为止一切都很好)。这一个参数应该是列表中的一个项目,没有别的。

有没有一种方法可以以“选择缓冲区”(如 dired)的形式显示列表,用户可以在其中导航到项目并通过按 Enter 选择它,而不必手动输入字符串?

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    通常的方法是通过completing-read。然后,您可以在调用minibuffer-completion-help 的地方使用minibuffer-with-setup-hook,以便立即弹出*Completions* 缓冲区,以便用户单击他的选择。

    【讨论】:

      【解决方案2】:

      你要找的是completing-read:

      (defun foo (arg)
        (interactive (list (completing-read ...)))
        ....)
      

      【讨论】:

        【解决方案3】:

        如果我正确理解了这个问题,您正在寻找这样的东西:

        (defun foo (list)
          (interactive)
          (let ((arg (ido-completing-read "Select from list: " list))))
             ...)
        

        选择过程并不像 dired,但 emacs 用户通常使用 ido 或其他类似替代方法从列表中进行选择。您可以缩小搜索范围、在替代选项和长选项之间移动等。如果您想了解可以自定义哪些偏好,请键入 M-x customize-group RET ido。

        【讨论】:

          【解决方案4】:

          我喜欢在这类事情上使用弹出菜单:

          (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))))
          

          顺便说一句,有人想使用(mapcar 'cons your-list-of-strings your-list-of-strings) à la Common Lisp,但 elisp 只在 mapcar 中使用一元函数 :-(

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-13
            • 1970-01-01
            • 2021-12-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多