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