Emacs 递归项目搜索

发布于 2024-08-23 12:52:07 字数 948 浏览 11 评论 0原文

我将从 TextMate 切换到 Emacs。我真正希望 Emacs 中具有的 TextMate 功能之一是使用模糊匹配的“在项目中查找”搜索框。 Emacs 与 ido 有点类似,但 ido 不会通过子目录递归搜索。它仅在一个目录内搜索。

有没有办法给 ido 一个根目录并搜索其下的所有内容?

更新:

以下问题与 Michał Marczyk 的回答中的 find-file-in-project.el 有关。

如果此消息中的任何内容听起来很明显,那是因为我使用 Emacs 还不到一周。 :-)

据我了解,project-local-variables 允许我在保存在项目根目录中的 .emacs-project 文件中定义内容。

如何将 find-file-in-project 指向我的项目根目录?

我不熟悉 Emacs Lisp 中的正则表达式语法。 ffip-regexp 的默认值是:

".*\\.\\(rb\\|js\\|css\\|yml\\|yaml\\|rhtml\\|erb\\|html\\|el\\)"

我认为我可以将扩展名切换为适合我的项目的扩展名。

您能解释一下 ffip-find-options 吗?从文件中:

(defvar ffip-find-options ”” “使用 find-file-in-project 时传递给‘find’的额外选项。

使用它可以排除项目的某些部分:\"-not -regex \\"。供应商。\\" \"")

这到底是什么意思以及如何使用它来排除文件/目录?

您能分享一个示例 .emacs-project 文件吗?

I am switching to Emacs from TextMate. One feature of TextMate that I would really like to have in Emacs is the "Find in Project" search box that uses fuzzy matching. Emacs sort of has this with ido, but ido does not search recursively through child directories. It searches only within one directory.

Is there a way to give ido a root directory and to search everything under it?

Update:

The questions below pertain to find-file-in-project.el from Michał Marczyk's answer.

If anything in this message sounds obvious it's because I have used Emacs for less than one week. :-)

As I understand it, project-local-variables lets me define things in a .emacs-project file that I keep in my project root.

How do I point find-file-in-project to my project root?

I am not familiar with regex syntax in Emacs Lisp. The default value for ffip-regexp is:

".*\\.\\(rb\\|js\\|css\\|yml\\|yaml\\|rhtml\\|erb\\|html\\|el\\)"

I presume that I can just switch the extensions to the ones appropriate for my project.

Could you explain the ffip-find-options? From the file:

(defvar ffip-find-options
""
"Extra options to pass to `find' when using find-file-in-project.

Use this to exclude portions of your project: \"-not -regex \\".vendor.\\"\"")

What does this mean exactly and how do I use it to exclude files/directories?

Could you share an example .emacs-project file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

旧情勿念 2024-08-30 12:52:08

您可以使用 GNU Global 或 IDUtils 来获得您想要的效果。它们不是 Emacs 特有的,但它们具有集成该效果的 Emacs 脚本。 (我自己对它们了解不多。)

您还可以选择使用 CEDET 和 EDE 项目系统。 EDE 可能有点重,但它有办法只标记项目的顶部。如果您还在项目中保留 GNU Global 或 IDUtils 索引文件,EDE 可以使用它在任何地方按名称查找文件,或者您可以使用“semantic-symref”查找对源文件中符号的引用。 CEDET 位于 http://cedet.sf.net

You can get the effect you want by using GNU Global or IDUtils. They are not Emacs specific, but they has Emacs scripts that integrate that effect. (I don't know too much about them myself.)

You could also opt to use CEDET and the EDE project system. EDE is probably a bit heavy weight, but it has a way to just mark the top of a project. If you also keep GNU Global or IDUtils index files with your project, EDE can use it to find a file by name anywhere, or you can use `semantic-symref' to find references to symbols in your source files. CEDET is at http://cedet.sf.net

顾冷 2024-08-30 12:52:08

为了获得纯粹、纯粹的速度,我强烈建议结合使用命令行工具 The Silver Searcher(又名 ' ag') 与 ag.el。如果您使用 git、hg 或 svn,ag-project 交互功能将对您的项目根目录进行有根据的猜测并搜索整个项目。

For pure, unadulterated speed, I highly recommend a combination of the command-line tool The Silver Searcher (a.k.a. 'ag') with ag.el. The ag-project interactive function will make an educated guess of your project root if you are using git, hg or svn and search the entire project.

云归处 2024-08-30 12:52:08

FileCache 也可能是一个选项。但是,您需要使用 file-cache-add-directory-recursively 手动添加项目目录。

FileCache may also be an option. However you would need to add your project directory manually with file-cache-add-directory-recursively.

自由范儿 2024-08-30 12:52:08

请参阅这些链接,了解有关 Icicles 如何提供帮助的信息此处:

Icicles 补全匹配可以是子字符串、正则表达式、模糊(各种),或这些的组合。您还可以组合简单的模式、相交匹配或补充(减去)它们的子集

See these links for info about how Icicles can help here:

Icicles completion matching can be substring, regexp, fuzzy (various kinds), or combinations of these. You can also combine simple patterns, intersecting the matches or complementing (subtracting) a subset of them

桃气十足 2024-08-30 12:52:07

我为此使用 Mx rgrep。它会自动跳过很多你不想要的东西,比如 .svn 目录。

I use M-x rgrep for this. It automatically skips a lot of things you don't want, like .svn directories.

过气美图社 2024-08-30 12:52:07

(更新主要是为了包含与下面提到的 RINARI 发行版中的 find-file-in-project.el 一起使用的实际设置说明。原始答案保持不变;新位出现在第二条水平规则之后。)< /em>


查看 EmacsWiki 的 TextMate 页面。他们提到的最有希望的事情可能是 这个 Emacs Lisp 脚本< /a>,它在一些变量引导的“项目目录”下提供递归搜索。该文件以描述如何使用它的大量注释部分开头。

它特别有前途的是以下几点:

;; If `ido-mode' is enabled, the menu will use `ido-completing-read'
;; instead of `completing-read'.

请注意,我自己还没有使用过它......尽管我现在很可能会尝试一下,因为我已经找到了它! :-)

呵呵。

(顺便说一句,该脚本是——引用 GitHub 上的描述——“Rinari 不是 Rails IDE(它是 Rails 的 Emacs 次要模式)”的一部分。如果您正在进行任何 Rails 开发,您可能想要查看整个内容。)


在继续之前,请配置 ido.el。说真的,它本身就是必备的,它将改善您在项目中查找文件的体验。请参阅 Stuart Halloway 的此截屏视频(我已经在对此答案的评论中提到过),了解为什么您需要使用它。此外,Stu 通过以自己的方式模拟 TextMate 的项目范围文件查找工具,展示了 ido 的灵活性;如果他的功能适合您的需要,则无需继续阅读。

好的,下面是如何设置 RINARI 的 find-file-in-project.el

  1. 获取 find-file-in-project.elproject-local-variables.el 来自 RINARI 发行版,并将其放在 Emacs 所在的位置可以找到它们(这意味着在 load-path 变量中的目录之一中;您可以使用 (add-to-list 'load-path "/path/to/some/directory ") 向其中添加新目录)。

  2. (require 'find-file-in-project) 添加到您的 .emacs 文件中。另请添加以下内容以使 Cx CMf 序列显示 find-file-in-project 提示: (global-set-key (kbd "Cx CMf") 'find-file- in-project)

  3. 在项目根目录中创建一个名为 .emacs-project 的文件。它至少应包含如下内容:(setl ffip-regexp ".*\\.\\(clj\\|py\\)$")。这将使得仅搜索名称为 clj 或 py 的文件;请调整正则表达式以满足您的需求。 (请注意,此正则表达式将传递给 Unix find 实用程序,并且应使用 find 的首选正则表达式语法。您仍然必须将每个反斜杠加倍正则表达式 就像 Emacs 中常见的那样;如果您想使用它们的“神奇”正则表达式含义,是否还必须在括号/管道 (|) 之前添加反斜杠取决于您的 find 的期望。上面给出的示例适用于 Ubuntu 机器,如有疑问,请查找有关正则表达式的其他信息。)(注意:本段已在上次编辑中进行了修改,以修复一些问题。正则表达式语法混淆。)

  4. Cx CMf 消失。

有许多可能的定制;特别是,您可以使用 (setl ffip-find-options "...") 将附加选项传递给 Unix find 命令,这就是 find -file-in-project.el 在幕后调用。

如果事情似乎不起作用,请检查并仔细检查您的拼写 - 我做了一次类似 (setl ffip-regex ...) 的事情(注意变量中缺少最后的“p”)名称),最初很困惑地发现没有找到任何文件。

(Updated primarily in order to include actual setup instructions for use with the below mentioned find-file-in-project.el from the RINARI distribution. Original answer left intact; the new bits come after the second horizontal rule.)


Have a look at the TextMate page of the EmacsWiki. The most promising thing they mention is probably this Emacs Lisp script, which provides recursive search under a "project directory" guided by some variables. That file begins with an extensive comments section describing how to use it.

What makes it particularly promising is the following bit:

;; If `ido-mode' is enabled, the menu will use `ido-completing-read'
;; instead of `completing-read'.

Note I haven't used it myself... Though I may very well give it a try now that I've found it! :-)

HTH.

(BTW, that script is part of -- to quote the description from GitHub -- "Rinari Is Not A Rails IDE (it is an Emacs minor mode for Rails)". If you're doing any Rails development, you might want to check out the whole thing.)


Before proceeding any further, configure ido.el. Seriously, it's a must-have on its own and it will improve your experience with find-file-in-project. See this screencast by Stuart Halloway (which I've already mentioned in a comment on this answer) to learn why you need to use it. Also, Stu demonstrates how flexible ido is by emulating TextMate's project-scoped file-finding facility in his own way; if his function suits your needs, read no further.

Ok, so here's how to set up RINARI's find-file-in-project.el:

  1. Obtain find-file-in-project.el and project-local-variables.el from the RINARI distribution and put someplace where Emacs can find them (which means in one of the directories in the load-path variable; you can use (add-to-list 'load-path "/path/to/some/directory") to add new directories to it).

  2. Add (require 'find-file-in-project) to your .emacs file. Also add the following to have the C-x C-M-f sequence bring up the find-file-in-project prompt: (global-set-key (kbd "C-x C-M-f") 'find-file-in-project).

  3. Create a file called .emacs-project in your projects root directory. At a minimum it should contain something like this: (setl ffip-regexp ".*\\.\\(clj\\|py\\)$"). This will make it so that only files whose names and in clj or py will be searched for; please adjust the regex to match your needs. (Note that this regular expression will be passed to the Unix find utility and should use find's preferred regular expression syntax. You still have to double every backslash in regexes as is usual in Emacs; whether you also have to put backslashes before parens / pipes (|) if you want to use their 'magic' regex meaning depends on your find's expectations. The example given above works for me on an Ubuntu box. Look up additional info on regexes in case of doubt.) (Note: this paragraph has been revised in the last edit to fix some confusion w.r.t. regular expression syntax.)

  4. C-x C-M-f away.

There's a number of possible customisations; in particular, you can use (setl ffip-find-options "...") to pass additional options to the Unix find command, which is what find-file-in-project.el calls out to under the hood.

If things appear not to work, please check and double check your spelling -- I did something like (setl ffip-regex ...) once (note the lack of the final 'p' in the variable name) and were initially quite puzzled to discover that no files were being found.

你对谁都笑 2024-08-30 12:52:07

令人惊讶的是没有人提到 https://github.com/defunkt/textmate.el (现在必须做到在 Windows 上工作...)

Surprised nobody mentioned https://github.com/defunkt/textmate.el (now gotta make it work on Windows...)

别把无礼当个性 2024-08-30 12:52:07

eproject 具有 eproject-grep,它正是您想要的。

有了正确的项目定义,它只会搜索项目文件;它将忽略版本控制、构建工件、生成的文件等等。唯一的缺点是它需要在您的系统上使用 grep 命令;这种依赖性很快就会消除。

eproject has eproject-grep, which does exactly what you want.

With the right project definition, it will only search project files; it will ignore version control, build artifacts, generated files, whatever. The only downside is that it requires a grep command on your system; this dependency will be eliminated soon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文