具有输入历史记录的Elisp交互式功能 [英] Elisp interactive function with input history
问题描述
有一堆将字符串输入作为参数的交互函数:
(defun zb / run-cmd- X(arg1 argN)
(交互式 Marg1:Marg2:)
;;;某些逻辑
如何制作每个这样的函数 zb / run-cmd-1
.. zb / run-cmd-N
具有拥有输入参数的独立历史记录 arg1 ... argN
?如果此历史记录在Emacs之间保持不变,那将是完美的启动(最好是在外部文件中的某个位置;用于同步)。
有没有为此准备好的解决方案?
谢谢
基本上,您想阅读有关 read-from-minibuffer $ c的文档有关每个函数都接受的
HIST
参数的$ c>和完成阅读
。当然支持,但是这两个是标准/基本选项。
持久性由 savehist
库提供,该库写入 savehist-file
(默认为〜/ .emacs.d / history
,但是如果该文件存在,则将使用旧的〜/ .emacs-history
-在这种情况下,您可能需要将其重命名为现代的首选路径。
这里有一个例子:
< pre class = lang-el prettyprint-override>
(defvar my-ssh-history nil)
(加载后评估保存
'(添加到列表'savehist-additional-variables'my-ssh-history))
(defun my-ssh(args)
通过SSH连接到远程主机。
(交互式
(列表(从minibuffer读取``ssh nil nil nil'my-ssh-history)))
(let *((switches(split(split-string-and-unquote args))
(名称(concat ssh args))
(termbuf(应用'make-term name ssh nil开关)))
(设置缓冲区termbuf)
(项模式)
(项字符模式)
(切换到缓冲区t ermbuf)))
(保存模式1)
there is a bunch of interactive functions which take string input as an argument:
(defun zb/run-cmd-X (arg1 argN)
(interactive "Marg1: Marg2: ")
;;; some logic
How to make each of such functions zb/run-cmd-1
..zb/run-cmd-N
have own independent history of input arguments arg1...argN
? And it would be perfect if this history was persistent between Emacs launches (ideally somewhere in an external file; for sync).
Is there any ready solution for this?
Thanks
Basically you want to read the documentation for read-from-minibuffer
and completing-read
regarding the HIST
argument which each of those functions accepts. There are other functions with history support of course, but these two are the standard/basic options.
Persistence is provided for by the savehist
library, which writes to the file in savehist-file
(which by default is ~/.emacs.d/history
, but the old ~/.emacs-history
will be used instead if that file exists -- in which case you might want to rename it to the modern preferred path).
Here's an example:
(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)
这篇关于具有输入历史记录的Elisp交互式功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!