【问题标题】:How to programmatically create/update a TAGS file with emacs?如何使用 emacs 以编程方式创建/更新 TAGS 文件?
【发布时间】:2010-10-07 14:21:59
【问题描述】:

是否有任何 emacs 插件可以自动更新我的 C 项目中的 TAGS 文件(例如在缓冲区保存或访问时)或在不存在 TAGS 文件时创建一个新的?

我在 Windows 上运行(没有 Cygwin),所以所有花哨的 shell 脚本都无济于事。我希望有一个不使用任何外部脚本的原生 emacs 解决方案。

我已经尝试过build-tags.eletags-table.el,但这些都没有真正奏效(我想要的方式)。

还有其他想法吗?

【问题讨论】:

    标签: emacs ctags


    【解决方案1】:

    etags-update 可能对您的情况有所帮助。我没有测试它,但是根据自述文件:

    etags-update.el 是一种 Emacs 全局次要模式,可在保存文件时更新您的 TAGS。

    我在Emacswiki page for building TAGS files找到它

    【讨论】:

      【解决方案2】:

      以下是我为 C 项目生成 TAGS 文件的方法:

      1. M-x cd YOUR_DIRECTORY
      2. M-x 编译
      3. 找到 . -name "*.[chCH]" -print |电子标签 -

      这将在当前目录中为所有子目录和文件创建一个 TAGS 文件。

      这是一个做同样事情的 emacs 函数:

      (defun compile-tags ()
        "compile etags for the current project"
        (interactive)
        (cd "YOUR_DIRECTORY")
        (compile "find . -name \"*.[chCH]\" -print | etags -"))
      

      注意:如果您在 Windows 上,则需要安装 cygwin 并确保 c:\cygwin\bin 在您的路径中,以便您在路径中获得 find。还要确保 emacs bin 目录在你的路径中,这样你也可以得到etags

      【讨论】:

      • 你也可以使用原生windows版本的gnu utils,包括find等。我发现它们比 cygwin 快。 gnuwin32.sourceforge.net
      【解决方案3】:

      为什么不将 ctags 的执行添加到您的构建脚本中?编译时(最多)你真的只需要一个新的标签文件。我倾向于每天晚上编写一个计划任务来构建标签文件。看起来效果不错。

      【讨论】:

      • 我希望在源代码中引入新符号后立即更新 TAGS 文件。在开发从文件跳转到文件时,我广泛使用 TAGS 浏览功能。一晚一次是不够的。在我们的多开发人员环境中,个人构建脚本模块是不可能的。
      • 如何构建代码?用执行构建的脚本包装该命令,然后运行 ​​ctags 命令。
      • 我不同意——您希望在编辑缓冲区后立即添加标签。
      • @Steve Rowe:是的,我可能可以一起编写脚本。但我不喜欢它。 @jrockway:我同意。我立即需要它,不一定要每次都构建。
      • @cschol 为 .c 和 .h 文件编写一些花哨的保存钩子怎么样?
      【解决方案4】:

      试试我的 `ctags.el'[1] 模块。

      配置示例:

      (setq tags-revert-without-query t)
      (global-set-key (kbd "<f5>") 'ctags-create-or-update-tags-table)
      

      然后只需按&lt;f5&gt; 即可更新或创建您的 TAGS 文件。那个功能 在当前及其父目录中查找文件 TAGS,如果 TAG 找不到文件,它会询问您在哪里创建一个新文件。

      这是一个新库,可能存在错误等,因此欢迎任何帮助。

      [1]https://bitbucket.org/semente/ctags.el/

      【讨论】:

        【解决方案5】:

        我在日常工作中使用语义 + gnu global 的组合。 GNU Global 的数据库每天更新一次,而语义将它们用于基本导航,并即时重新解析更改的文件。

        你可以在my article about Cedet找到更多关于这些包的信息

        【讨论】:

        • 在服务器上找不到 URL!
        【解决方案6】:

        这可能会让你接近(未经测试):

        (defvar my-auto-update-tags-alist
          (list '("/some/path/to/TAGS" "command_to_build_tags")
                '("/another/path/to/TAGS" "another_build_command")))
        
        (defun my-auto-update-tags ()
          "Automatically update TAGS files"
          (tags-table-check-computed-list)
          (let ((filename (buffer-file-name))
                build-cmd)
            (mapc (lambda (tag-file)
                    (set-buffer tag-file)
                    (when (member filename (tags-table-files))
                      (setq build-cmd (cdr (assoc tag-file my-auto-update-tags-alist)))
                      (when build-cmd
                        (call-process build-cmd nil 0))))
                  tags-table-computed-list)))
        
        (add-hook 'after-save-hook 'my-auto-update-tags)
        

        它只适用于已经在 TAGS 文件中的文件(我是否提到它未经测试?)。如果您添加一个新文件,您必须自己第一次重新生成 TAGS 文件。调用过程部分应该异步工作,因此可能需要片刻才能真正重建 TAGS 文件(如果这甚至可以工作;)

        【讨论】:

        • 你会努力测试一下吗?
        【解决方案7】:

        安装 find-and-ctags (https://github.com/redguardtoo/find-and-ctags),然后将以下代码插入 ~/.emacs,

        (defun my-setup-develop-environment ()
          (interactive)
        
          ;; you can use `find-and-ctags-current-full-filename-match-pattern-p' instead
          (when (find-and-ctags-current-path-match-pattern-p "/MYPROJ")
            (setq-local tags-table-list
                        (list (find-and-ctags-run-ctags-if-needed "~/workspace/MYPROJ" ; project directory
                       '(("-not -size +64k" "--exclude=*.min.js") ; (find-opts ctags-opts)
                                                                    ;; you may add more find-opts ctags-opts pair HERE to run find&ctags again to APPEND to same TAGS file
                                                                    ;; ctags-opts must contain "-a" to append
                                                                    ;; (find-opts "-a")
                                                                    )))))
        
          ;; for other projects
          ;; insert NEW `when' statements here
          )
        (add-hook 'prog-mode-hook 'my-setup-develop-environment) ; prog-mode require emacs24+
        (add-hook 'lua-mode-hook 'my-setup-develop-environment) ; lua-mode does NOT inherit from prog-mode
        
        ;; OPTIONAL
        (add-hook 'after-save-hook 'find-and-ctags-auto-update-tags)
        

        它适用于 Windows,您只需将“~/workspace/MYPROJ”替换为“C:/workspace/MYPROJ”。 CTags 可执行文件可以是任何版本,因为生成的 TAGS 只包含相对路径。

        【讨论】:

        • 我认为这与我的方法有些相似。您是否每次都强制替换现有的 TAGS 文件?如果是这样,您可以通过在生成后比较新旧 TAGS 文件来使其变得更好,并且只有在它们不同时才对其进行破坏,这样 Emacs 就不会看到变化。此外,您可以通过使用与当前 TAGS 文件相同的标准以及 -newer 运行初始 find 来决定是否生成标签。如果没有更新的文件,不要做任何事情。我通过head -1-newer 命令通过管道传输,以便它在找到单个匹配文件时立即中止。
        • 是的,我每次都强行更改TAGS。我的项目是网络前端项目。 “-newer”是个好主意。我一定会尝试的。你能给我看一些代码示例吗?
        猜你喜欢
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        • 2011-02-26
        • 2011-01-18
        • 2010-12-15
        • 1970-01-01
        相关资源
        最近更新 更多