1;;; etags-select.el --- Select from multiple tags
3;; Copyright (C) 2007 Scott Frazer
5;; Author: Scott Frazer <frazer.scott@gmail.com>
6;; Maintainer: Scott Frazer <frazer.scott@gmail.com>
9;; Keywords: etags tags tag select
11;; This file is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
16;; This file is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
28;; Open a buffer with file/lines of exact-match tags shown. Select one by
29;; going to a line and pressing return. pop-tag-mark still works with this
32;; If there is only one match, you can skip opening the selection window by
33;; setting a custom variable. This means you could substitute the key binding
34;; for find-tag-at-point with etags-select-find-tag-at-point, although it
35;; won't play well with tags-loop-continue. On the other hand, if you like
36;; the behavior of tags-loop-continue you probably don't need this code.
39;; (global-set-key "\M-?" 'etags-select-find-tag-at-point)
41;; Contributers of ideas and/or code:
46;; 13 Jun 2007 -- Need to regexp-quote the searched-for string
53;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57(defgroup etags-select-mode nil
62(defcustom etags-select-no-select-for-one-match t
63 "*If non-nil, don't open the selection window if there is only one
65 :group 'etags-select-mode
69(defcustom etags-select-mode-hook nil
70 "*List of functions to call on entry to etags-select-mode mode."
71 :group 'etags-select-mode
74;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77(defvar etags-select-buffer-name "*etags-select*"
78 "etags-select buffer name.")
80(defvar etags-select-mode-font-lock-keywords nil
81 "etags-select font-lock-keywords.")
83(defvar etags-select-starting-buffer nil
84 "etags-select buffer tag is being found from.")
86(defconst etags-select-non-tag-regexp "\\(\\s-*$\\|In:\\|Finding tag:\\)"
87 "etags-select non-tag regex.")
89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92(if (string-match "XEmacs" emacs-version)
93 (fset 'etags-select-match-string 'match-string)
94 (fset 'etags-select-match-string 'match-string-no-properties))
96;; I use Emacs, but with a hacked version of XEmacs' etags.el, thus this variable
98(defvar etags-select-use-xemacs-etags-p (fboundp 'get-tag-table-buffer)
101(defun etags-select-insert-all-exact-matches (tagname tag-file)
102 (let ((tag-table-buffer (if etags-select-use-xemacs-etags-p
103 (get-tag-table-buffer tag-file)
104 (visit-tags-table-buffer tag-file)
105 (get-file-buffer tag-file)))
106 (tag-file-path (file-name-directory tag-file))
108 tag-line filename current-filename)
109 (set-buffer tag-table-buffer)
110 (goto-char (point-min))
111 (while (re-search-forward (concat "\^?" tagname "\^A") nil t)
112 (setq tags-found (1+ tags-found))
114 (re-search-forward "\\(.*\\)\^?")
115 (setq tag-line (etags-select-match-string 1))
118 (re-search-backward "\f")
119 (re-search-forward "^\\(.*?\\),")
120 (setq filename (concat tag-file-path (etags-select-match-string 1))))
121 (set-buffer etags-select-buffer-name)
122 (when (not (string= filename current-filename))
123 (insert "\nIn: " filename "\n")
124 (setq current-filename filename))
125 (insert tag-line "\n")
126 (set-buffer tag-table-buffer))
130(defun etags-select-find-tag-at-point ()
131 "Do a find-tag-at-point, and display all exact matches. If only one match is
132found, see the `etags-select-no-select-for-one-match' variable to decide what
135 (let ((tagname (find-tag-default))
136 (tag-files (if etags-select-use-xemacs-etags-p
137 (buffer-tag-table-list)
138 (mapcar 'tags-expand-table-name tags-table-list)))
140 (setq etags-select-starting-buffer (buffer-name))
141 (get-buffer-create etags-select-buffer-name)
142 (set-buffer etags-select-buffer-name)
143 (setq buffer-read-only nil)
145 (insert "Finding tag: " tagname "\n")
146 (mapcar (lambda (tag-file)
147 (setq tags-found (+ tags-found (etags-select-insert-all-exact-matches tagname tag-file))))
149 (cond ((= tags-found 0)
150 (message (concat "No exact match for tag \"" tagname "\""))
152 ((and (= tags-found 1) etags-select-no-select-for-one-match)
153 (set-buffer etags-select-buffer-name)
154 (goto-char (point-min))
155 (etags-select-next-tag)
156 (etags-select-goto-tag))
158 (set-buffer etags-select-buffer-name)
159 (goto-char (point-min))
160 (etags-select-next-tag)
161 (set-buffer-modified-p nil)
162 (setq buffer-read-only t)
163 (display-buffer etags-select-buffer-name)
164 (pop-to-buffer etags-select-buffer-name)
165 (force-mode-line-update)
166 (etags-select-mode tagname)))))
168(defun etags-select-goto-tag (&optional arg)
169 "Goto the file/line of the tag under the cursor. Use the C-u prefix to
170prevent the etags-select window from closing."
172 (let (tagname tag-point text-to-search-for filename filename-point (search-count 1))
174 (goto-char (point-min))
175 (re-search-forward "Finding tag: \\(.*\\)$")
176 (setq tagname (etags-select-match-string 1)))
178 (if (looking-at etags-select-non-tag-regexp)
179 (message "Please put the cursor on a line with the tag.")
180 (setq tag-point (point))
181 (setq overlay-arrow-position (point-marker))
182 (re-search-forward "\\s-*\\(.*\\)\\s-*$")
183 (setq text-to-search-for (concat "^\\s-*" (regexp-quote (etags-select-match-string 1))))
184 (goto-char tag-point)
185 (re-search-backward "^In: \\(.*\\)$")
186 (setq filename (etags-select-match-string 1))
187 (setq filename-point (point))
188 (goto-char tag-point)
189 (while (re-search-backward text-to-search-for filename-point t)
190 (setq search-count (1+ search-count)))
191 (goto-char tag-point)
193 (let ((window (get-buffer-window etags-select-starting-buffer)))
194 (when window (select-window window)))
195 (let ((window (get-buffer-window etags-select-buffer-name)))
196 (when window (delete-window window)))
197 (kill-buffer etags-select-buffer-name))
198 (display-buffer etags-select-starting-buffer)
199 (set-buffer etags-select-starting-buffer)
200 (if etags-select-use-xemacs-etags-p
202 (ring-insert find-tag-marker-ring (point-marker)))
204 (goto-char (point-min))
205 (while (> search-count 0)
206 (unless (re-search-forward text-to-search-for nil t)
207 (message "TAGS file out of date ... stopping at closest match")
208 (setq search-count 1))
209 (setq search-count (1- search-count)))
211 (re-search-forward tagname)
212 (goto-char (match-beginning 0)))))
214(defun etags-select-next-tag ()
219 (while (and (looking-at etags-select-non-tag-regexp) (not (eobp)))
224(defun etags-select-previous-tag ()
229 (while (and (looking-at etags-select-non-tag-regexp) (not (bobp)))
234(defun etags-select-quit ()
239;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242(defvar etags-select-mode-map nil "'etags-select-mode' keymap.")
243(if (not etags-select-mode-map)
244 (let ((map (make-keymap)))
245 (define-key map [(return)] 'etags-select-goto-tag)
246 (define-key map [(down)] 'etags-select-next-tag)
247 (define-key map [(up)] 'etags-select-previous-tag)
248 (define-key map [(q)] 'etags-select-quit)
249 (setq etags-select-mode-map map)))
251;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254(defun etags-select-mode (tagname)
255 "etags-select-mode is a mode for browsing through tags.\n\n
256\\{etags-select-mode-map}"
258 (kill-all-local-variables)
259 (setq major-mode 'etags-select-mode)
260 (setq mode-name "etags-select")
261 (set-syntax-table text-mode-syntax-table)
262 (use-local-map etags-select-mode-map)
263 (make-local-variable 'font-lock-defaults)
264 (setq etags-select-mode-font-lock-keywords
265 (list (list "^\\(Finding tag:\\)" '(1 font-lock-keyword-face))
266 (list "^\\(In:\\) \\(.*\\)" '(1 font-lock-keyword-face) '(2 font-lock-string-face))
267 (list tagname '(0 font-lock-function-name-face))))
268 (setq font-lock-defaults '(etags-select-mode-font-lock-keywords))
269 (setq overlay-arrow-position nil)
270 (run-hooks 'etags-select-mode-hook))
272(provide 'etags-select)
273;;; etags-select.el ends here