From b16f9bbd223f92b5a928a28d7bbf9b6dd925d9e3 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Thu, 11 Jul 2019 23:20:16 +0300 Subject: [PATCH] Automatically run ctags command on save --- 08ctags.el | 41 ++++++++++++++++++++++++++++++----------- 20clang.el | 2 +- 20erlang.el | 7 +++++-- 20ruby.el | 8 ++++++-- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/08ctags.el b/08ctags.el index 8535c9a..3b7407a 100644 --- a/08ctags.el +++ b/08ctags.el @@ -1,9 +1,14 @@ ;; Do not mix ctags between folders +(provide 'my-ctags-config) + (setq tags-add-tables nil) +(setq ctags/refresh-command + (format "ctags -e -R -f %sTAGS %s." + default-directory default-directory)) ;; Sentinel function for capturing ctags -(defun ctags-process-callback (process event) - "Show status of asynchronous ctags-process after it finishes." +(defun ctags/process-callback (process event) + "Show status of asynchronous ctags/process after it finishes." (cond ((string-equal event "finished\n") (message "Creating tag files...completed") @@ -14,17 +19,31 @@ (pop-to-buffer (get-buffer "*ctags*")) ))) -(setq ctags-refresh-command - (format "ctags -e -R -f %sTAGS %s." - default-directory default-directory)) - -(defun refresh-ctags () +(cl-defun ctags/refresh-ctags (&key silent) "Refresh ctags according to currently set command." (interactive) - (message "Starting ctags process") - (start-process-shell-command "ctags" "*ctags*" ctags-refresh-command) - (set-process-sentinel (get-process "ctags") 'ctags-process-callback)) + ;; Print message if not silent + (when (not silent) (message "Starting ctags process...")) + + ;; Return if a version of the process is already running + (when (not (get-process "ctags")) + (start-process-shell-command "ctags" "*ctags*" ctags/refresh-command) + (set-process-sentinel (get-process "ctags") 'ctags/process-callback))) ;; Ctags bindings -(define-key prog-mode-map (kbd "C-c E") 'refresh-ctags) +(define-key prog-mode-map (kbd "C-c E") 'ctags/refresh-ctags) + +;; Automatically update tags on save, but be silent about it. +(setq ctags/major-modes-to-update-on-save '()) +(defun ctags/update-tags-on-save () + "Update tags if current major mode is part of the list." + (interactive) + (when (member major-mode ctags/major-modes-to-update-on-save) + (ctags/refresh-ctags :silent t))) + +(defun ctags/update-this-mode-on-save (mode) + "Update MODE on save." + (add-to-list (make-local-variable 'ctags/major-modes-to-update-on-save) mode)) + +(add-hook 'after-save-hook 'ctags/update-tags-on-save) diff --git a/20clang.el b/20clang.el index 9c0a972..2ab385f 100644 --- a/20clang.el +++ b/20clang.el @@ -7,7 +7,7 @@ '(company-etags company-yasnippet)) (setq-local - ctags-refresh-command + ctags/refresh-command (format "ctags -e -R --languages=C -f %sTAGS %s." (projectile-project-root) (projectile-project-root) diff --git a/20erlang.el b/20erlang.el index c8c2ac2..a5cb68e 100644 --- a/20erlang.el +++ b/20erlang.el @@ -27,7 +27,7 @@ ;; Set specific ctags command (setq-local - ctags-refresh-command + ctags/refresh-command (format "ctags -e -R --languages=erlang -f %sTAGS %s. %slib/stdlib-* %slib/kernel-*" (projectile-project-root) (projectile-project-root) @@ -44,4 +44,7 @@ (activate-erlang-mode) ;; Enable flycheck - (flycheck-select-checker 'erlang-otp)) + (flycheck-select-checker 'erlang-otp) + + ;; Automatically update tags on save + (ctags/update-this-mode-on-save 'erlang-mode)) diff --git a/20ruby.el b/20ruby.el index 1f2f402..7da8e77 100644 --- a/20ruby.el +++ b/20ruby.el @@ -26,10 +26,14 @@ (setq ruby-insert-encoding-magic-comment nil) ;; Company list override - (add-to-list (make-local-variable 'company-backends) '(company-etags company-yasnippet)) + (add-to-list (make-local-variable 'company-backends) + '(company-etags company-yasnippet)) + + ;; Automatically update tags on save + (ctags/update-this-mode-on-save 'enh-ruby-mode) ;; Set specific ctags command - (setq-local ctags-refresh-command + (setq-local ctags/refresh-command (format "ctags -e -R --languages=ruby -f %sTAGS %s. $(bundle list --paths)" (projectile-project-root) (projectile-project-root))))