【问题标题】:Elisp interactive function with input history带有输入历史的 Elisp 交互功能
【发布时间】:2013-11-07 12:34:44
【问题描述】:

有一堆以字符串输入为参数的交互函数:

(defun zb/run-cmd-X (arg1 argN)
  (interactive "Marg1: Marg2: ")
  ;;; some logic

如何使每个这样的函数zb/run-cmd-1..zb/run-cmd-N拥有自己的输入参数arg1...argN的独立历史记录?如果这段历史在 Emacs 启动之间保持不变(理想情况下在外部文件的某个位置;用于同步),那将是完美的。

有没有现成的解决方案?

谢谢

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    基本上,您想阅读read-from-minibuffercompleting-read 的文档,了解每个函数都接受的HIST 参数。当然还有其他功能支持历史记录,但这两个是标准/基本选项。

    持久性由savehist 库提供,该库写入savehist-file 中的文件(默认为~/.emacs.d/history,但如果该文件存在,则将使用旧的~/.emacs-history 代替——其中如果您想将其重命名为现代首选路径)。

    这是一个例子:

    (defvar my-ssh-history nil)
    
    (eval-after-load "savehist"
      '(add-to-list 'savehist-additional-variables 'my-ssh-history))
    
    (defun my-ssh (args)
      "Connect to a remote host by SSH."
      (interactive
       (list (read-from-minibuffer "ssh " nil nil nil 'my-ssh-history)))
      (let* ((switches (split-string-and-unquote args))
             (name (concat "ssh " args))
             (termbuf (apply 'make-term name "ssh" nil switches)))
        (set-buffer termbuf)
        (term-mode)
        (term-char-mode)
        (switch-to-buffer termbuf)))
    
    (savehist-mode 1)
    

    【讨论】:

    • 我是否正确理解,对于每个函数的每个参数,我必须为历史定义一个自变量?作者最后不是来了amount-of-functions * amount-of-input-args变量吗?
    • 如果它是生成的代码,那么整个事情可以被包裹在一个宏中,或者历史变量可以在交互式调用中绑定以指向一个包含所有历史的不同结构。我不清楚实际的确切要求是什么,但肯定可以基于这种方法构建解决方案。
    • @phils,它不是生成的代码,但函数看起来非常相似:采用相同的输入参数,将它们传递给外部应用程序并显示输出;所以宏不是我想的那样。但是我已经理解了大致的想法,所以除此之外的一切都可以建立在这个例子之上。我看到read-from-minibuffer 的唯一问题可能不适用于非字符串参数(例如区域/缓冲区)。
    猜你喜欢
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2011-08-01
    • 2013-11-21
    • 2010-11-06
    • 2012-11-18
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多