【问题标题】:Invoking a complex piped find within Emacs eshell-command在 Emacs eshell-command 中调用复杂的管道查找
【发布时间】:2017-10-09 17:40:45
【问题描述】:

我正在尝试做一些看似简单的事情:创建一个 Emacs 函数来为我创建一个 TAGS 文件。有这样做的简单说明here

(defun create-tags (dir-name)
 "Create tags file."
 (interactive "DDirectory: ")
 (eshell-command 
  (format "find %s -type f -name \"*.[ch]\" | etags -" dir-name)))

问题是我需要“cpp”文件而不是“c”。这意味着我的 find 命令必须更改为:

find %s -type f -iname "*.cpp" -or -iname "*.h"

这在命令行上效果很好。我遇到的问题是 eshell 似乎根本不喜欢那样。当我执行这个函数时,我不断得到: File not found - "*.h": Invalid argument.

this question 的答案表明,正确使用 shell-quote-argument 可能会解决这些问题,但我无法找到可行的解决方案。例如,这会产生相同的错误:

(format "find %s -type f -iname %s -or -iname %s | etags -"
   dir-name
   (shell-quote-argument "*.cpp")
   (shell-quote-argument "*.h"))

【问题讨论】:

  • 使用更好的ctags 可能是最简单的解决方案。
  • 您是否尝试过使用常规的shell-command 而不是eshell-command
  • @sds - 刚试过shell-command。完全相同的结果。
  • 可能 - 我的 emacs 是 26.0.50。你的(20.0.50)可能是一个错字。我不相信您使用的是 20 年前的 开发 版本。
  • @T.E.D.你在 Windows 中运行 find 吗?

标签: windows emacs find interactive-shell


【解决方案1】:

您正在尝试将 posix 语法与 Windows find 命令一起使用。这是错误的,原因有两个:

  • 您当然不能希望它支持来自不同操作系统的语法。
  • Windows find 的作用类似于 grep,请改用 dir

希望对你有帮助。

【讨论】:

  • 刚刚弄清楚(并发布了答案)。然而,这里的所有信息都证明是错误的,需要做什么,是你在 cmets 中的提示帮助我来到这里,所以我接受了。
【解决方案2】:

在 cmets 的 sds 和 Daniele 的大力帮助下,我终于能够找出问题所在。

我所做的有两个问题:

  1. 我使用的是带有 ms-dos 命令 shell 的 bash 解决方案。 DOS“查找”是一个与 Unix 查找完全不同的命令,因此它抱怨其参数是完全有道理的。
  2. 常见的引用问题。我的 etags exe 在其路径中有一个空格。我尝试使用 shell-quote-argument 来解决这个问题,但是使用 MS-DOS shell 所做的只是在参数周围加上转义引号。您仍然需要手动转义任何反斜杠,并且 DOS shell 需要其文件路径中的反斜杠。

对于那些感兴趣的人,Windows下的工作命令是:

(defun create-tags (dir-name)
  "Create tags file."
  (interactive "DDirectory: ")
  (shell-command
   (format "cd %s & dir /b /s *.h *.cpp | %s -"
       dir-name
       (shell-quote-argument "C:\\Program Files\\Emacs\\emacs-25.0\\bin\\etags.exe"))))

唯一的怪癖是,当 Emacs 提示您输入目录时,您必须确保给它一个 DOS shell 可以处理的目录。 ~/dirname 将不起作用。可能有一个解决方案,对于 emacs-fu 比我关心的更好的人来说。

【讨论】:

  • 另请注意,将 emacs 的默认 shell 设置为更好的也可能会起到作用。但是,我需要它是 Dos shell,因为这是 vcvars.bat 使用的,我的 VisualStudio 编译需要它,所以这样做不是一个选项。
  • this question 也有助于弄清楚它有 DOS shell 解决方案。
  • @Daniele - 我把它看作是 Unix 上的 vi。当然,它很糟糕,但该操作系统的每台机器都有它。您需要至少了解它以在紧要关头使用它,直到您可以安装更好的东西。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多