如何定义“M-x查找”以与“M-x grep”相同的方式 [英] How to define "M-x find" in the same way as "M-x grep"?

查看:286
本文介绍了如何定义“M-x查找”以与“M-x grep”相同的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Emacs中找到一个命令 Mx find ,其行为与 Mx grep (允许修改该命令很好地打印输出,包括找到的文件的链接,...),并执行 find。 -iname'* | *'(光标放置在垂直条上 - 用于插入搜索模式 - 如果不是太复杂,不能实现)。以前有人实现了吗? [我知道 Mx find-grep ]

I am looking for a command M-x find in Emacs, which behave exactly like M-x grep (allows to modify the command, prints the output nicely including links to the found files, ...) and which executes find . -iname '*|*' (with the cursor placed at the vertical bar -- for inserting a search pattern -- if not too complicated to implement). Has anyone implemented this before? [I am aware of M-x find-grep]

推荐答案

使用 Mx find-dired 几乎可以做到这一点:它从minibuffer读取目录,默认为当前目录,然后读取其他查找参数。结果在 dired 模式中打开,我认为这样做很好,如果你认为 dired 太冗长了,请在 dired-details ,可能 dired-details + /melpa.milkbox.net/rel =nofollow> MELPA )。

Let's start with M-xfind-dired that does almost what you want: it reads directory from minibuffer, defaulting to current directory, and then reads other find arguments. The result is opened in dired mode, and I think it's as nicely as it can get (if you think that dired is too verbose, check out dired-details and maybe dired-details+ packages at MELPA).

现在让我们从 -iname开始** 在星号要求选项之间有一个光标。看看 find-dired source,我们可以看到它使用 find-args 的值作为初始输入参数到 read-string 。这个论点已经过时,已经过时,但非常有用。其中一个功能(正如我们在 read-from-minibuffer 描述中所读)在提供字符串和整数的缺点时提供默认点位置。


Now let's make it start with -iname ** with a cursor between the stars when it's asking for options. Looking at find-dired source, we can see that it uses the value of find-args as an initial input argument to read-string. This argument is obsolete and deprecated but awfully useful. One of its features (as we read in read-from-minibuffer description) is providing a default point position when a cons of a string and an integer is given.

(defun marius/find-dired ()
  (interactive)
  (let ((find-args '("-iname '**'" . 10)))
    (call-interactively 'find-dired)))

我们在'**'中添加了单引号,因为参数需要扩展shell。

We added single quotes around stars in '**' because the arguments are subject to shell expansion.

我们只是从
重新绑定 find-args 而不是从minibuffer中读取我们自己的参数,并将所有其余的代码委托给发现-dired 。通常,
find-dired 记住您在 find-args 中输入的最后一个参数,以便
成为新的默认值。重新绑定它与 let 确保这个
修改从我们调用 find-dired 将被丢弃,所以
常规 find-dired 将使用给最新的
常规 找到-dired 。如果您不使用常规的 find-dired 可能不重要。如果您希望找到给我们的包装器的参数,以便定期使用 find-dired ,请改用以下定义:

Instead of reading our own arguments from the minibuffer, we just rebind find-args and delegate all the rest to find-dired. Normally find-dired remembers last arguments you enter in find-args so they become the new default. Rebinding it with let ensures that this modification from our call to find-dired will be thrown away, so regular find-dired will use the arguments given to the latest regular find-dired. It probably doesn't matter if you don't use regular find-dired. If you want find arguments given to our wrapper to be used by regular find-dired, use the following definition instead:

(defun marius/find-dired ()
  (interactive)
  (setq find-args '("-iname '**'" . 10))
  (call-interactively 'find-dired))

这篇关于如何定义“M-x查找”以与“M-x grep”相同的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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