【问题标题】:How to find the files in TAGS file in emacs如何在emacs中找到TAGS文件中的文件
【发布时间】:2011-10-05 21:20:49
【问题描述】:

我已经使用 ctags 为目录中的 *.h 和 *.cpp 文件生成了 TAGS 文件。 如何在 TAGS 文件中找到文件。

假设我已经为文件 one.h two.h three.h 生成了 TAGS 文件。查找文件 one.h、two.h、three.h 而不是这些文件中的标签的命令是什么。

【问题讨论】:

  • "如何在 TAGS 文件中找到文件。"非常模糊。如果我的回答还不够,也许你可以澄清一下这个问题。
  • 也许这只是你的错字,但对于 emacs,你通常必须使用 etags 而不是 ctags
  • 你的意思是你想要一个 Emacs 函数来访问你在你的 TAGS 文件中有信息的文件?我不认为有一个内置的功能,但它应该很容易做到。
  • 我希望这些解决方案中的任何一个都能解释如何配置/使用它们。如果您足够聪明地添加它们,它们可能会起作用。就像 Bob 最近的回答(2019 年 11 月)一样,我记得 M-.刚刚工作。现在它没有,我很沮丧。 :P

标签: emacs elisp


【解决方案1】:

假设您只是想知道如何使用 TAGS 文件...

使用以下命令加载 TAGS 文件:
M-x visit-tags-table RET TAGS 文件或父目录 RET

然后你可以使用它:

  • M-.(即find-tag
  • M-x tags-search RET 模式 RET
    (用 M-, 移动到每个连续的匹配)
  • M-x tags-apropos RET 模式 RET
  • M-x tags-query-replace RET 模式 RET 替换 RET

这些是默认值。自然有可用的增强功能:
http://www.emacswiki.org/emacs/EmacsTags

我个人使用etags-select(您可以通过ELPA获得),并且我将M-.绑定到etags-select-find-tag

【讨论】:

    【解决方案2】:

    这是我几年前写的,不过我还没有时间发布它……享受吧!

    函数tags-extra-find-file 将允许您访问当前标签表中的文件,并完成文件名完成。如果您有许多源文件分布在大量目录中,这是完美的。 (老实说,我每天至少用一百次……)

    (defun tags-extra-get-all-tags-files ()
      "Return all, fully qualified, file names."
      (save-excursion
        (let ((first-time t))
          (while (visit-tags-table-buffer (not first-time))
            (setq first-time nil)
            (setq res
                  (append res (mapcar 'expand-file-name (tags-table-files)))))))
      res))
    
    
    (defun tags-extra-find-file (name)
      "Edit file named NAME that is part of the current tags table.
    The file name should not include parts of the path."
      (interactive
       (list
        (completing-read "Name of file: "
                         ;; Make an a-list of all files without path.
                         (mapcar
                          (lambda (file)
                            (cons (file-name-nondirectory file) nil))
                          (tags-extra-get-all-tags-files)))))
      (let ((files (tags-extra-get-all-tags-files))
            (done nil)
            (name-re (concat "^" (regexp-quote name) "$")))
        (while (and (not done)
                    files)
          (let ((case-fold-search t))
            (if (string-match name-re (file-name-nondirectory (car files)))
                (setq done t)
              (setq files (cdr files)))))
        (if files
            (find-file (car files))
          (error "File not found in the tags table."))))
    

    【讨论】:

    • tags-extra-get-all-tags-files 函数有错误。我在我的答案中更正了我与 ido 一起使用的地方。感谢您的回答解决了我的问题。
    【解决方案3】:

    这样的?它可能并不完全健壮。

    (defun visit-tags-table-and-files (file)                                        
      "Run `visit-tags-table FILE', then visit all the referenced files."           
      (interactive "fTags file: ")                                                  
      (visit-tags-table file)                                                       
      (save-excursion                                                               
        (set-buffer (get-file-buffer tags-file-name))                               
        (mapc #'find-file (tags-table-files)) ) )
    

    【讨论】:

      【解决方案4】:

      此更正适用于 emacs 26.3。带有旧的 etag,M-。将接受一个文件名,例如 Setup.cpp,并访问 etags 找到的文件。在许多目录中有很多文件非常方便。无需记住文件所在的目录即可访问它。我很惊讶这不是一个开箱即用的功能!

       (defun tags-extra-get-all-tags-files ()
        "Return all, fully qualified, file names."
        (setq res nil)
        (save-excursion
          (let ((first-time t))
            (while (visit-tags-table-buffer (not first-time))
              (setq first-time nil)
              (setq res
                    (append res (mapcar 'expand-file-name (tags-table-files)))))))
        res)
      

      【讨论】:

        【解决方案5】:

        感谢@Lindydancer 的回答和emacswiki ido。 emacswiki 版本只支持一个标签文件。结合它们,我可以跳转到所有 TAG 文件中的任何文件。小资接近崇高文本的goto任何东西。

        这里是代码。

        ;;; using ido find file in tag files
        (defun tags-extra-get-all-tags-files ()
          "Return all, fully qualified, file names."
          (save-excursion
            (let ((first-time t)
                  (res nil))
              (while (visit-tags-table-buffer (not first-time))
                (setq first-time nil)
                (setq res
                      (append res (mapcar 'expand-file-name (tags-table-files)))))
              res)))
        
        (defun ido-find-file-in-tag-files ()
          (interactive)
          (find-file
           (expand-file-name
            (ido-completing-read
             "Files: " (tags-extra-get-all-tags-files) nil t))))
        

        【讨论】:

          猜你喜欢
          • 2015-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-02
          • 1970-01-01
          • 2010-10-07
          • 1970-01-01
          • 2014-01-02
          相关资源
          最近更新 更多