Commit | Line | Data |
---|---|---|
55b522b2 | 1 | ;;; semantic/tag-file.el --- Routines that find files based on tags. |
f273dfc6 CY |
2 | |
3 | ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, | |
4 | ;;; 2008, 2009 Free Software Foundation, Inc. | |
5 | ||
6 | ;; Author: Eric M. Ludlam <zappo@gnu.org> | |
7 | ;; Keywords: syntax | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs 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 3 of the License, or | |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs 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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ;; | |
26 | ;; A tag, by itself, can have representations in several files. | |
27 | ;; These routines will find those files. | |
28 | ||
29 | (require 'semantic/tag) | |
30 | ||
a4556861 | 31 | (defvar ede-minor-mode) |
55b522b2 CY |
32 | (declare-function semanticdb-table-child-p "semantic/db") |
33 | (declare-function semanticdb-get-buffer "semantic/db") | |
34 | (declare-function semantic-dependency-find-file-on-path "semantic/dep") | |
a4556861 | 35 | (declare-function ede-toplevel "ede/files") |
55b522b2 | 36 | |
f273dfc6 CY |
37 | ;;; Code: |
38 | ||
39 | ;;; Location a TAG came from. | |
40 | ;; | |
3d9d8486 | 41 | ;;;###autoload |
f273dfc6 CY |
42 | (define-overloadable-function semantic-go-to-tag (tag &optional parent) |
43 | "Go to the location of TAG. | |
44 | TAG may be a stripped element, in which case PARENT specifies a | |
45 | parent tag that has position information. | |
46 | PARENT can also be a `semanticdb-table' object." | |
47 | (:override | |
48 | (cond ((semantic-tag-in-buffer-p tag) | |
49 | ;; We have a linked tag, go to that buffer. | |
50 | (set-buffer (semantic-tag-buffer tag))) | |
51 | ((semantic-tag-file-name tag) | |
52 | ;; If it didn't have a buffer, but does have a file | |
53 | ;; name, then we need to get to that file so the tag | |
54 | ;; location is made accurate. | |
55 | (set-buffer (find-file-noselect (semantic-tag-file-name tag)))) | |
56 | ((and parent (semantic-tag-p parent) (semantic-tag-in-buffer-p parent)) | |
57 | ;; The tag had nothing useful, but we have a parent with | |
58 | ;; a buffer, then go there. | |
59 | (set-buffer (semantic-tag-buffer parent))) | |
60 | ((and parent (semantic-tag-p parent) (semantic-tag-file-name parent)) | |
61 | ;; Tag had nothing, and the parent only has a file-name, then | |
62 | ;; find that file, and switch to that buffer. | |
63 | (set-buffer (find-file-noselect (semantic-tag-file-name parent)))) | |
55b522b2 CY |
64 | ((and parent (featurep 'semantic/db) |
65 | (semanticdb-table-child-p parent)) | |
f273dfc6 CY |
66 | (set-buffer (semanticdb-get-buffer parent))) |
67 | (t | |
68 | ;; Well, just assume things are in the current buffer. | |
69 | nil | |
70 | )) | |
71 | ;; We should be in the correct buffer now, try and figure out | |
72 | ;; where the tag is. | |
73 | (cond ((semantic-tag-with-position-p tag) | |
74 | ;; If it's a number, go there | |
75 | (goto-char (semantic-tag-start tag))) | |
76 | ((semantic-tag-with-position-p parent) | |
77 | ;; Otherwise, it's a trimmed vector, such as a parameter, | |
78 | ;; or a structure part. If there is a parent, we can use it | |
79 | ;; as a bounds for searching. | |
80 | (goto-char (semantic-tag-start parent)) | |
81 | ;; Here we make an assumption that the text returned by | |
82 | ;; the parser and concocted by us actually exists | |
83 | ;; in the buffer. | |
84 | (re-search-forward (semantic-tag-name tag) | |
85 | (semantic-tag-end parent) | |
86 | t)) | |
87 | ((semantic-tag-get-attribute tag :line) | |
88 | ;; The tag has a line number in it. Go there. | |
89 | (goto-line (semantic-tag-get-attribute tag :line))) | |
90 | ((and (semantic-tag-p parent) (semantic-tag-get-attribute parent :line)) | |
91 | ;; The tag has a line number in it. Go there. | |
92 | (goto-line (semantic-tag-get-attribute parent :line)) | |
93 | (re-search-forward (semantic-tag-name tag) nil t) | |
94 | ) | |
95 | (t | |
96 | ;; Take a guess that the tag has a unique name, and just | |
97 | ;; search for it from the beginning of the buffer. | |
98 | (goto-char (point-min)) | |
99 | (re-search-forward (semantic-tag-name tag) nil t))) | |
100 | ) | |
101 | ) | |
102 | ||
103 | (make-obsolete-overload 'semantic-find-nonterminal | |
104 | 'semantic-go-to-tag) | |
105 | ||
106 | ;;; Dependencies | |
107 | ;; | |
108 | ;; A tag which is of type 'include specifies a dependency. | |
109 | ;; Dependencies usually represent a file of some sort. | |
110 | ;; Find the file described by a dependency. | |
111 | ||
3d9d8486 | 112 | ;;;###autoload |
f273dfc6 CY |
113 | (define-overloadable-function semantic-dependency-tag-file (&optional tag) |
114 | "Find the filename represented from TAG. | |
115 | Depends on `semantic-dependency-include-path' for searching. Always searches | |
116 | `.' first, then searches additional paths." | |
117 | (or tag (setq tag (car (semantic-find-tag-by-overlay nil)))) | |
118 | (unless (semantic-tag-of-class-p tag 'include) | |
119 | (signal 'wrong-type-argument (list tag 'include))) | |
120 | (save-excursion | |
121 | (let ((result nil) | |
122 | (default-directory default-directory) | |
123 | (edefind nil) | |
124 | (tag-fname nil)) | |
125 | (cond ((semantic-tag-in-buffer-p tag) | |
126 | ;; If the tag has an overlay and buffer associated with it, | |
127 | ;; switch to that buffer so that we get the right override metohds. | |
128 | (set-buffer (semantic-tag-buffer tag))) | |
129 | ((semantic-tag-file-name tag) | |
130 | ;; If it didn't have a buffer, but does have a file | |
131 | ;; name, then we need to get to that file so the tag | |
132 | ;; location is made accurate. | |
133 | ;;(set-buffer (find-file-noselect (semantic-tag-file-name tag))) | |
134 | ;; | |
135 | ;; 2/3/08 | |
136 | ;; The above causes unnecessary buffer loads all over the place. Ick! | |
137 | ;; All we really need is for 'default-directory' to be set correctly. | |
138 | (setq default-directory (file-name-directory (semantic-tag-file-name tag))) | |
139 | )) | |
140 | ;; Setup the filename represented by this include | |
141 | (setq tag-fname (semantic-tag-include-filename tag)) | |
142 | ||
143 | ;; First, see if this file exists in the current EDE project | |
144 | (if (and (fboundp 'ede-expand-filename) ede-minor-mode | |
145 | (setq edefind | |
146 | (condition-case nil | |
147 | (let ((proj (ede-toplevel))) | |
148 | (when proj | |
149 | (ede-expand-filename proj tag-fname))) | |
150 | (error nil)))) | |
151 | (setq result edefind)) | |
152 | (if (not result) | |
153 | (setq result | |
154 | ;; I don't have a plan for refreshing tags with a dependency | |
155 | ;; stuck on them somehow. I'm thinking that putting a cache | |
156 | ;; onto the dependancy finding with a hash table might be best. | |
157 | ;;(if (semantic--tag-get-property tag 'dependency-file) | |
158 | ;; (semantic--tag-get-property tag 'dependency-file) | |
159 | (:override | |
160 | (save-excursion | |
55b522b2 | 161 | (require 'semantic/dep) |
f273dfc6 CY |
162 | (semantic-dependency-find-file-on-path |
163 | tag-fname (semantic-tag-include-system-p tag)))) | |
164 | ;; ) | |
165 | )) | |
166 | (if (stringp result) | |
167 | (progn | |
168 | (semantic--tag-put-property tag 'dependency-file result) | |
169 | result) | |
170 | ;; @todo: Do something to make this get flushed w/ | |
171 | ;; when the path is changed. | |
172 | ;; @undo: Just eliminate | |
173 | ;; (semantic--tag-put-property tag 'dependency-file 'none) | |
174 | nil) | |
175 | ))) | |
176 | ||
177 | (make-obsolete-overload 'semantic-find-dependency | |
178 | 'semantic-dependency-tag-file) | |
179 | ||
180 | ;;; PROTOTYPE FILE | |
181 | ;; | |
182 | ;; In C, a function in the .c file often has a representation in a | |
183 | ;; corresponding .h file. This routine attempts to find the | |
184 | ;; prototype file a given source file would be associated with. | |
185 | ;; This can be used by prototype manager programs. | |
186 | (define-overloadable-function semantic-prototype-file (buffer) | |
187 | "Return a file in which prototypes belonging to BUFFER should be placed. | |
188 | Default behavior (if not overridden) looks for a token specifying the | |
189 | prototype file, or the existence of an EDE variable indicating which | |
190 | file prototypes belong in." | |
191 | (:override | |
192 | ;; Perform some default behaviors | |
193 | (if (and (fboundp 'ede-header-file) ede-minor-mode) | |
194 | (save-excursion | |
195 | (set-buffer buffer) | |
196 | (ede-header-file)) | |
197 | ;; No EDE options for a quick answer. Search. | |
198 | (save-excursion | |
199 | (set-buffer buffer) | |
200 | (if (re-search-forward "::Header:: \\([a-zA-Z0-9.]+\\)" nil t) | |
201 | (match-string 1)))))) | |
202 | ||
203 | (semantic-alias-obsolete 'semantic-find-nonterminal | |
204 | 'semantic-go-to-tag) | |
205 | ||
206 | (semantic-alias-obsolete 'semantic-find-dependency | |
207 | 'semantic-dependency-tag-file) | |
208 | ||
209 | ||
210 | (provide 'semantic/tag-file) | |
211 | ||
3d9d8486 CY |
212 | ;; Local variables: |
213 | ;; generated-autoload-file: "loaddefs.el" | |
214 | ;; generated-autoload-feature: semantic/loaddefs | |
996bc9bf | 215 | ;; generated-autoload-load-name: "semantic/tag-file" |
3d9d8486 CY |
216 | ;; End: |
217 | ||
55b522b2 | 218 | ;;; semantic/tag-file.el ends here |