帮助编写emacs的eacs搜索引擎 [英] Help writing emacs lisp for emacs etags search

查看:108
本文介绍了帮助编写emacs的eacs搜索引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些帮助,开发我认为应该是一个简单的程序。



我想要类似于Emacs标签搜索命令,但我想将所有搜索结果收集到缓冲区中。 (我想看到M的所有结果,)



我认为这个python样式的伪代码应该可以工作,但我不知道如何在emacs口齿不清?任何帮助将不胜感激。

  def myTagsGrep(searchValue):
在标签列表中的aFile:
result = grep aFile seachValue
if len(result)> 0:
打印aFile#到缓冲区
打印结果#到缓冲区

我想能够通过缓冲区浏览具有相同功能的标签 - apropos。请注意,以前曾经提到过类似的问题:
到缓冲区?

解决方案

由于我是一个 igrep ,我将其用作构建块。从那里有两个简单的例程,你完成了。使用该库和这两个功能,您只需要执行以下操作:

  Mx igrep-tags ^ SomeRegexp。*这里RET 

这是代码:

 $ ig ig ig ig ig-------)))))))))))))))))))))))))))))))))))))))))))))))))))))))名称)))

(defun tags-file-names()
(save-excursion
(visit-tags-table-buffer)
(mapcar (f)(file-truename f))
(tags-table-files))))

而且,由于文件列表可以很长时间,您可能不在乎该列表是什么,您可以添加这两个代码,使grep完成后,文件名不可见:

 (add-hook'compilation-finish-functions'igrep-tags-hide-filenames)

(defun igrep-tags-hide-filenames(buffer stat)
隐藏文件名b / c他们可以长
(save-excursi
(set-buffer buffer)
(save-match-data
(goto-char(point-min))
(if(search-forward quote-strings(tags-file-names))
nil
(save-excursion(forward-line 10)(point)))
(let((display-string ;来自TAGS> ..的文件))
(put-text-property(match-beginning 0)(match-end 0)'invisible t)
(put-text-property 0)(match-end 0)'display display-string))))))

真正的长命令行,您可以使用以下代码(它创建一个临时文件,其中包含来自TAGS文件的所有文件名称,并使用该代码):

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ -a-containing-files t))
(igrep igrep-program regex nil)))

(defvar igrep-use-file-as-c ontain-files nil)

(defadvice igrep-format-find-command(围绕igrep-format-find-command-use-filename-instead activate)
将第二个参数用作包含文件名
(如果igrep-use-file-as-contains-files
(progn(with-temp-file
(setq igrep-use-file-as-contains-files (make-temp-filetags-files))
(insert(combine-and-quote-strings(tags-file-names))))
(setq ad-return-value 猫%s | xargs -e%s
igrep-use-file-as-contains-files
(ad-get-arg 0))))
ad-do-it))$ b $对于那些使用Emacs 22或更早版本的用户,您需要Emacs 23附带的例程(来自< a href =http://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/subr.el =nofollow> subr.el )

 (defun combine-and-quote-strings(strings& optional separator)
连接STRINGS,添加SEPARATOR(默认\ \)
这将尝试引用字符串以避免歧义,使
(split-string-and-unquote(combine-and-quote-strings strs))== strs
只有一些SEPARATOR可以正常工作。
(let *((sep(或separator))
(re(concat[\\\\]\\ |(regexp-quote sep))))
(mapconcat
(lambda(str)
(if(string-match re str)
(concat\(replace-regexp-in-string[\\\]\\\\\\\\&str)\)
str))
strings sep))


I'm looking for some help developing what I think should be an easy program.

I want something similar to Emacs tags-search command, but I want to collect all search results into a buffer. (I want to see all results of M-,)

I'm thinking this python style pseudo code should work, but I have no idea how to do this in emacs lisp? Any help would be greatly appreciated.

def myTagsGrep(searchValue):
    for aFile in the tag list:
        result = grep aFile seachValue
        if len(result) > 0:
            print aFile  # to the buffer
            print result # to the buffer

I would like to be able to browse through the buffer with the same features tags-apropos does.

Note that a similar question has been asked before: Is there a way to get emacs tag-search command to output all results to a buffer?

解决方案

Since I'm such a fan of igrep, I'd use it as the building block. From there it's two simple routines and you're done. With that library and these two functions, all you have to do is:

M-x igrep-tags ^SomeRegexp.*Here RET

Here's the code:

(require 'igrep)
(defun igrep-tags (regex)
  (interactive "sTAGS Regexp: ")
  (igrep igrep-program regex (tags-file-names)))

(defun tags-file-names ()
  (save-excursion
    (visit-tags-table-buffer)
    (mapcar (lambda (f) (file-truename f))
            (tags-table-files))))

And, because the list of files can get really long, and you likely don't care what that list is, you can add these two pieces of code which will make the filenames invisible after the grep has finished:

(add-hook 'compilation-finish-functions 'igrep-tags-hide-filenames)

(defun igrep-tags-hide-filenames (buffer stat)
  "hide the filenames b/c they can get long"
  (save-excursion
    (set-buffer buffer)
    (save-match-data 
      (goto-char (point-min))
      (if (search-forward (combine-and-quote-strings (tags-file-names))
                          nil
                          (save-excursion (forward-line 10) (point)))
          (let ((display-string "..<files from TAGS>.."))
            (put-text-property (match-beginning 0) (match-end 0) 'invisible t)
            (put-text-property (match-beginning 0) (match-end 0) 'display display-string))))))

To avoid the really long command line, you can use the following code (which creates a temporary file containing all the names of files from TAGS file and uses that instead):

(defun igrep-tags (regex)
  (interactive "sTAGS Regexp: ")
  (let ((igrep-find t)
        (igrep-use-file-as-containing-files t))
    (igrep igrep-program regex nil)))

(defvar igrep-use-file-as-containing-files nil)

(defadvice igrep-format-find-command (around igrep-format-find-command-use-filename-instead activate)
  "use the second argument as a file containing filenames"
  (if igrep-use-file-as-containing-files
      (progn (with-temp-file
                 (setq igrep-use-file-as-containing-files (make-temp-file "tags-files"))
               (insert (combine-and-quote-strings (tags-file-names))))
             (setq ad-return-value (format "cat %s | xargs -e %s"
                                           igrep-use-file-as-containing-files
                                           (ad-get-arg 0))))
    ad-do-it))

And, for those using Emacs 22 or earlier, you'll need the routine that's shipped with Emacs 23 (from subr.el)

(defun combine-and-quote-strings (strings &optional separator)
  "Concatenate the STRINGS, adding the SEPARATOR (default \" \").
This tries to quote the strings to avoid ambiguity such that
  (split-string-and-unquote (combine-and-quote-strings strs)) == strs
Only some SEPARATORs will work properly."
  (let* ((sep (or separator " "))
         (re (concat "[\\\"]" "\\|" (regexp-quote sep))))
    (mapconcat
     (lambda (str)
       (if (string-match re str)
           (concat "\"" (replace-regexp-in-string "[\\\"]" "\\\\\\&" str) "\"")
         str))
     strings sep)))

这篇关于帮助编写emacs的eacs搜索引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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