File: eproject-tags.el

package info (click to toggle)
eproject-el 1.5+git20180312.068218d-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 204 kB
  • sloc: lisp: 1,092; sh: 10; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 4,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
;;; eproject-tags.el --- visit project-specific tags table and keep it up to date

;; Copyright (C) 2011  Jonathan Rockway

;; Author: Jonathan Rockway <jon@jrock.us>
;; Keywords: convenience, programming, tags

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(eval-when-compile
  (require 'cl))
(require 'eproject)
(require 'etags)

(defvar eproject-tags-etags "etags"
  "The command you want to run to generate the tags file.

It must accept a list of relative filenames and send its output
to a file named TAGS.")

(defvar eproject-tags-verbose t
  "Set to T if you want the verbose output of etags to stick around.

Only works with exuberant-ctags; this will cause standard etags to blow up in a giant ball of fire.")

(defvar eproject-tags-callback nil)
(defvar eproject-tags-state nil)
(make-variable-buffer-local 'eproject-tags-callback)
(make-variable-buffer-local 'eproject-tags-state)

(defun eproject-tags--buffer (root)
  (let* ((name (concat (eproject-attribute :name root) "-TAGS"))
         (buf  (get-buffer-create (format "*%s*" name))))
    buf))

(defun eproject-tags--debug-message (root format &rest rest)
  (when eproject-tags-verbose
    (with-current-buffer (eproject-tags--buffer root)
      (goto-char (point-max))
      (insert (apply #'format (concat "** " format) rest))
      (insert "\n"))))

(defun eproject-tags--generate (cb &optional state root)
  "Generate a tags table for this project (or the project in ROOT), calling CB with the project root and STATE upon completion.

All project-relevant files are considered and output goes to root/TAGS.  The tags table is not visited after generation."
  (let* ((root  (or root (eproject-root)))
         (default-directory root)
         (files (eproject-list-project-files-relative root))
         (name  (concat (eproject-attribute :name root) "-TAGS"))
         (buf   (eproject-tags--buffer root))
         (args  (append '("-o" ".TAGS-tmp")))
         (proc  (apply #'start-process name buf eproject-tags-etags
                       (append args files))))

    (with-current-buffer buf
      (setq eproject-tags-callback cb)
      (setq eproject-tags-state state)
      (setq eproject-root root))

    (set-process-sentinel proc #'eproject-tags--sentinel))
  nil)

(defun eproject-tags--sentinel (process event)
  (with-current-buffer (process-buffer process)
    (let ((default-directory eproject-root))
      (when (and eproject-tags-callback (equal event "finished\n"))
        (rename-file ".TAGS-tmp" "TAGS" t)
        (apply eproject-tags-callback eproject-tags-state)
        (delete-process process)
        (when (not eproject-tags-verbose)
          (kill-buffer))))))

;;; TODO: un-visit this table when all project buffers go away?
;;; TODO: make the tags table "project-local"

(defun eproject-tags (&optional root)
  "Update (or generate) and visit a tags table for the project in ROOT."
  (interactive)
  (let ((root (or root (eproject-root))))
    (eproject-tags--generate
     #'eproject-tags--visit-table (list root) root)))

(defun eproject-tags--visit-table (root &rest state)
  "Callback called by eproject-tags after tags table has been created."
  (let ((table (concat (file-name-as-directory root) "TAGS"))
        (tags-revert-without-query t))
    (add-to-list 'tags-table-list table t #'equal)
    (tags-verify-table table)))

(defun eproject-tags--from-hook ()
  (let ((root (ignore-errors (eproject-root)))
        (file (ignore-errors (buffer-file-name))))
    (when (and root file
               (eproject-classify-file file root)
               (not (eproject-attribute :suppress-tags root)))
      (eproject-tags--debug-message
       root "Regenerating tags table for %s because %s changed" root file)
      (eproject-tags root))))

(add-hook 'eproject-first-buffer-hook #'eproject-tags--from-hook)
(add-hook 'eproject-project-change-hook #'eproject-tags--from-hook)

(provide 'eproject-tags)
;;; eproject-tags.el ends here