【问题标题】:How to define an alternate command for emacs eshell如何为 emacs eshell 定义备用命令
【发布时间】:2012-02-25 01:59:51
【问题描述】:

我正在尝试开始在 emacs 中使用 eshell 代替 bash,但我严重依赖这些年来编写的 bash 函数。我想将 eshell 配置为在“找不到命令”条件发生时调用 bash,以防相关命令被实现为 bash 函数。

有一个变量名为 eshell-alternate-command-hook,听起来像是定制的,但我认为我缺乏 elisp 技能会影响我的成功。

这是我最大的努力:

(add-hook 'eshell-alternate-command-hook 'invoke-bash t t)
(defun invoke-bash (command args)
  (throw 'eshell-replace-command
     (list "bash -c" command args)))

但是当我测试它时,它不起作用:

c:/temp $ lsd
Wrong number of arguments: (lambda (command args) (throw (quote eshell-replace-command) (list "bash -c" command args))), 1
c:/temp $ 

【问题讨论】:

    标签: emacs eshell


    【解决方案1】:

    这是我最终想到的:

    (defun invoke-bash (command)
    (progn 
      (setq invoke-bash-cmd (concat "bash -c \"" command " " (mapconcat 'identity eshell-last-arguments " ") "\""))
      (message invoke-bash-cmd)
      (throw 'eshell-replace-command
         (eshell-parse-command invoke-bash-cmd))))
    

    【讨论】:

    • 试试这样的:gist.github.com/1903146,虽然我没试过
    • 谢谢,我看到 progn 是退化的并且没有必要,尽管当我使用 'let' 而不是 'setq' 时它似乎不起作用。 (也许我做错了什么)
    • 奇怪let 不适合你。 let 的主要目的是在本地上下文中创建变量。在您的代码中,因为 invoke-bash-cmd 没有在本地上下文中声明,所以它将是全局变量
    【解决方案2】:

    我不是 eshell 大师,但是在使用这个钩子的地方,我看到它只接收一个参数 - 你试图执行的命令,所以你的代码可能看起来像

    (add-hook 'eshell-alternate-command-hook 'invoke-bash)
    (defun invoke-bash (command)
      (throw 'eshell-replace-command
         (list "bash -c" command)))
    

    但它不起作用,因为您需要返回 elisp 函数,而不是命令名称(根据文档)。如果要运行 bash,则需要返回带有完整路径的字符串,但我还没有找到如何将其他参数传递给 bash。或许您可以在corresponding section on Emacs Wiki 中找到更多信息?

    【讨论】:

    • 这有帮助;我已经走得更远了,但又被卡住了:(defun invoke-bash (command) (throw 'eshell-replace-command (eshell-parse-command (concat "bash -c " command eshell-last-arguments)))) 如果参数是“pom.xml”,我会收到消息“错误类型参数:characterp,“pom.xml””,所以我觉得我现在很接近了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多