Emacs 递归项目搜索 [英] Emacs recursive project search

查看:24
本文介绍了Emacs 递归项目搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 TextMate 切换到 Emacs.我非常希望在 Emacs 中使用 TextMate 的一项功能是使用模糊匹配的在项目中查找"搜索框.Emacs 与 ido 有点类似,但 ido 不会递归搜索子目录.它只在一个目录中搜索.

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.

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

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

更新:

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

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

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

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

据我所知,project-local-variables 让我可以在我保存在项目根目录中的 .emacs-project 文件中定义内容.

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

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

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

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

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.

你能解释一下 fip-find-options 吗?来自文件:

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

(defvar fip-find-options""使用 find-file-in-project 时传递给 `find' 的额外选项.

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

使用它来排除项目​​的某些部分:"-not -regex \".vendor.\""")

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?

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

Could you share an example .emacs-project file?

推荐答案

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

查看 EmacsWiki 的TextMate 页面.他们提到的最有前途的可能是这个 Emacs Lisp 脚本,它提供了在一些变量引导下的项目目录"下的递归搜索.该文件以一个广泛的注释部分开始,描述了如何使用它.

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.

(顺便说一句,该脚本是 - 引用来自 GitHub 的描述 - Rinari 不是 Rails IDE(它是 Rails 的 Emacs 次要模式)"的一部分.如果你正在做任何 Rails 开发,你可能想看看整个事情.)

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

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

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.

好的,这里是如何设置 RINARI 的 find-file-in-project.el:

Ok, so here's how to set up RINARI's 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") 向其中添加新目录.

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

(require 'find-file-in-project) 添加到您的 .emacs 文件中.还要添加以下内容以使 Cx CMf 序列调出 find-file-in-project 提示: (global-set-key (kbd "Cx CMf") 'find-file-项目内).

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

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

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

C-x C-M-f 离开.

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

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.

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

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.

这篇关于Emacs 递归项目搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆