How to programmatically create/update a TAGS file with emacs?

后端 未结 7 1104
我在风中等你
我在风中等你 2021-01-31 11:20

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?

7条回答
  •  太阳男子
    2021-01-31 11:37

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

提交回复
热议问题