【问题标题】:elisp function help (learning elisp): popup list of servers, prompt for user input, then execute function to connectelisp函数帮助(学习elisp):弹出服务器列表,提示用户输入,然后执行函数连接
【发布时间】:2018-02-02 15:13:21
【问题描述】:

警告: 我是elisp世界的菜鸟。我正在寻找建议,从哪里开始使用以下查询寻找定向自学材料。我愿意:“这似乎是最愚蠢/最难/过于复杂/非口齿不清的方式,为什么不尝试 x”。谢谢大家的帮助。

查询: 我想实现类似于我使用但在 elisp 中的 shell 脚本的东西。如果这让我的问题更清楚,我可以分享 shell 脚本。我正在使用 emacs 在 windows 上使用 plink 连接到远程服务器。我有很多这样的功能,例如:

(defun server ()
  (setq explicit-shell-file-name "/bin/bash")
  (interactive)
  (let ((default-directory "/plink:un@ip:~/"))
    (shell)))

我想将所有这些组合成一个 Emacs 命令connect。我想调用M-x connect,弹出一个可能的连接列表,将连接号输入到minibuffer,然后根据minibuffer的输入执行相应的函数。

【问题讨论】:

  • interactive 需要在 defun 中排在第一位,紧跟在文档字符串之后(如果有的话)。

标签: shell emacs


【解决方案1】:

使用completing-read 从多个选项中进行选择,并完成。这就是我开始解决您的实际问题的方式:

(defvar jpk/connect-list '("foo" "bar" "127.0.0.1"))
(defun jpk/connect (host)
  (interactive (list (completing-read "Server: " jpk/connect-list nil t)))
  (let* ((explicit-shell-file-name "/bin/bash")
         (user "un")
         (method "plink")
         (default-directory (format "/%s:%s@%s:~/"
                                    method user host)))
    (shell (format "*shell:%s@%s*" user host))))

(旁白:find-file + tramp 已经在 ~/.ssh/config 中定义的主机上完成了。不过,我不知道它在 Windows 上的效果如何。)

如果你还想走多功能路线,首先意识到你已经在M-x完成了,也许这就足够了。如果不是,completing-read返回字符串,将字符串转为符号的方法是intern。然后使用call-interactively调用函数。

(defun connect-foo ()
  (interactive)
  (message "Connecting to foo..."))

(let ((host "foo")) ;; this could be from `completing-read` as above
  (call-interactively (intern (format "connect-%s" host)))) ;; run connect-foo

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多