0

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

(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.

这个问题的答案表明,正确使用shell-quote-argument可能会解决这类问题,但我无法破解出有效的解决方案。例如,这会产生相同的错误:

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

2 回答 2

1

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

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

希望它会帮助你。

于 2017-05-31T19:30:25.913 回答
1

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

我正在做的事情有两个问题:

  1. 我正在使用带有 ms-dos 命令外壳的 bash 解决方案。DOS“find”是一个与 Unix find 完全不同的命令,所以它抱怨它的参数是完全有道理的。
  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 比我关心的更好的人来说,可能有一个解决方案。

于 2017-05-31T19:32:47.503 回答