Search code examples
emacselispgnu-global

Update gtag file in emacs for a single file once on saving


My configuration is copied from http://www.emacswiki.org/emacs/GnuGlobal#toc4 . The purpose is to update existing tag file once i save source file. But the result is the same as expected.

The full configuration is:

(gtags-mode 1)
(defun gtags-root-dir ()
  "Returns GTAGS root directory or nil if doesn't exist."
  (with-temp-buffer
    (if (zerop (call-process "global" nil t nil "-pr"))
    (buffer-substring (point-min) (1- (point-max)))
      nil)))

(defun gtags-update-single(filename)  
  "Update Gtags database for changes in a single file"
  (interactive)
  (start-process "update-gtags" "update-gtags" "bash" "-c" (concat "cd " (gtags-root-dir) " ; gtags --single-update " filename )))

(defun gtags-update-current-file()
  (interactive)
  (defvar filename)
  (setq filename (replace-regexp-in-string (gtags-root-dir) "." (buffer-file-name (current-buffer))))
  (gtags-update-single filename)
  (message "Gtags updated for %s" filename))

(defun gtags-update-hook()
      "Update GTAGS file (insert )ncrementally upon saving a file"
      (when gtags-mode     ;;It is copy past error..
        (when (gtags-root-dir)
          (gtags-update-current-file))))
(add-hook 'after-save-hook 'gtags-update-hook)

Update

My understanding is that tags will be update via command

  (gtags-update-single filename)
  (message "Gtags updated for %s" filename))

once file in buffer is saved. That means a new added or renamed or removed function will be updated to the tag file. In my test, I do see output message(The tags is in ededemo directory ):

 Wrote /other/projectbase/cplusproject/ededemo/src/main.cpp
 Gtags updated for ./src/main.cpp

each time function is renamed or added after c-x c-s. But M-x gtags-find-tag could not find my new added function. Is there any wrong in understanding?


Solution

  • This line is clearly responsible/broken:

    (when gtags-;-*- mode: ${1:mode} -*-
    

    Looking at the Wiki page, I can't fathom how you managed to end up with that.

    The docstring comment is also corrupted. Just copy the whole function again.