Skip to content

Instantly share code, notes, and snippets.

@mechairoi
Created January 30, 2014 05:13
Show Gist options
  • Save mechairoi/8702981 to your computer and use it in GitHub Desktop.
Save mechairoi/8702981 to your computer and use it in GitHub Desktop.
(require 'vc-git)
(provide 'auto-update-gtags)
(defvar auto-update-gtags/interval (* 60 60) "interval seconds")
(defvar auto-update-gtags/command "gtags")
(defun auto-update-gtags/file-need-update-p (file interval)
(and (file-writable-p file)
(or (not (file-exists-p file))
(> (- (float-time) (float-time (nth 5 (file-attributes file))))
interval))))
(defun auto-update-gtags/update-if-needed (file interval &rest shell-command-args)
(when (auto-update-gtags/file-need-update-p file interval)
(apply 'start-process-shell-command shell-command-args)))
(defun auto-update-gtags/find-git-root ()
(vc-git-root (or (buffer-file-name) default-directory)))
(defun auto-update-gtags/update-gtags-if-needed ()
(unless (and (fboundp 'tramp-tramp-file-p) (tramp-tramp-file-p buffer-file-name))
(let ((default-directory (auto-update-gtags/find-git-root)))
(when default-directory
(auto-update-gtags/update-if-needed
"GTAGS"
auto-update-gtags/interval
"update gtags" nil auto-update-gtags/command)))))
(add-to-list 'find-file-hook 'auto-update-gtags/update-gtags-if-needed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment