如何使用 emacs 以编程方式创建/更新 TAGS 文件?

发布于 2024-07-13 14:17:38 字数 451 浏览 3 评论 0原文

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

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

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

还有其他想法吗?

Is there any plugin for emacs to automatically update the TAGS file in my C project (for example on buffer save or access) or create a new one if there is no TAGS file present?

I am running on Windows (without Cygwin), so all the fancy shell scripting does not help. I was hoping for a native emacs solution not using any external scripting.

I already tried build-tags.el and etags-table.el but none of these really worked (the way I wanted).

Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小红帽 2024-07-20 14:17:38

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

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

我在 用于构建 TAGS 文件的 Emacswiki 页面上找到了它

etags-update may help in your case. I didn't test it, but according to the readme:

etags-update.el is a Emacs global minor mode that updates your TAGS when saving a file.

I found it on the Emacswiki page for building TAGS files

孤星 2024-07-20 14:17:38

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

  1. Mx cd YOUR_DIRECTORY
  2. M-x compile
  3. find 。 -名称“*。[chCH]”-打印| etags -

这将在当前目录中为所有子目录和文件创建一个 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

Here is how I generate a TAGS file for a C project:

  1. M-x cd YOUR_DIRECTORY
  2. M-x compile
  3. find . -name "*.[chCH]" -print | etags -

That will create a TAGS file in the current directory for all sub directories and files.

Here is a emacs function that does the exact same thing:

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

NOTE: if you are on windows you'll need to install cygwin and make sure c:\cygwin\bin is in your path so that you get find in your path. Also make sure the emacs bin directory is in your path so that you can get etags as well.

狂之美人 2024-07-20 14:17:38

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

Why not add an execution of ctags to your build script? You really only need a new tags file when you compile (at the most). I tend to just write a scheduled task to build the tags file every night. Seems to work pretty well.

骷髅 2024-07-20 14:17:38

试试我的“ctags.el”[1] 模块。

配置示例:

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

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

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

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

Try out my `ctags.el'[1] module.

Configuration example:

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

Then just press <f5> to update or create your TAGS file. That function
look for a file TAGS in the current and its parent directories, if a TAG
file is not found it ask you where create a new one.

It is a new library and probably has bugs, etc, so any help is welcome.

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

深海里的那抹蓝 2024-07-20 14:17:38

我在日常工作中使用语义 + gnu 全局的组合。 GNU Global 的数据库每天更新一次,而语义使用它们进行基本导航,并动态重新解析更改的文件。

您可以在我关于 Cedet 的文章中找到有关这些软件包的更多信息

I use combination of semantic + gnu global for my day-to-day work. GNU Global's databases are updated once per day, while semantic use them for basic navigation, and re-parse changed files on the fly.

You can find more about these packages in my article about Cedet

等待圉鍢 2024-07-20 14:17:38

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

(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 文件(如果这甚至有效的话;)

This might get you close (untested):

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

It will only work (did I mention it's untested?) on files that are in TAGS files already. If you add a new file you'd have to regenerate the TAGS file the first time yourself. The call-process part should work asynchronously, so it might be a few moments until the TAGS file is actually rebuilt (if this even works ;)

疯了 2024-07-20 14:17:38

安装 find-and-ctags (https://github.com/redguardtoo/find-and-ctags< /a>),然后将以下代码插入到 ~/.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 仅包含相对路径。

Install find-and-ctags (https://github.com/redguardtoo/find-and-ctags), then insert below code into ~/.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)

It works on windows, all you need is replace "~/workspace/MYPROJ" with "C:/workspace/MYPROJ". CTags executable could be any version, because produced TAGS contains only relative path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文