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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
;;; semantic/tag-file.el --- Routines that find files based on tags. -*- lexical-binding: t; -*-
;; Copyright (C) 1999-2005, 2007-2025 Free Software Foundation, Inc.
;; Author: Eric M. Ludlam <zappo@gnu.org>
;; Keywords: syntax
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; A tag, by itself, can have representations in several files.
;; These routines will find those files.
(require 'semantic/tag)
(defvar ede-minor-mode)
(declare-function semantic-dependency-find-file-on-path "semantic/dep")
(declare-function ede-toplevel "ede/base")
;;; Code:
;;; Location a TAG came from.
;;
(cl-defgeneric semantic-tag-parent-buffer (parent)
"Return the buffer in which a tag can be found, knowing its PARENT."
(cond ((and (semantic-tag-p parent) (semantic-tag-in-buffer-p parent))
;; We have a parent with a buffer, then go there.
(semantic-tag-buffer parent))
((and (semantic-tag-p parent) (semantic-tag-file-name parent))
;; The parent only has a file-name, then
;; find that file, and switch to that buffer.
(find-file-noselect (semantic-tag-file-name parent)))))
;;;###autoload
(defun semantic-go-to-tag (tag &optional parent)
"Go to the location of TAG.
TAG may be a stripped element, in which case PARENT specifies a
parent tag that has position information.
PARENT can also be a `semanticdb-table' object."
(save-match-data
(set-buffer
(cond ((semantic-tag-in-buffer-p tag)
;; We have a linked tag, go to that buffer.
(semantic-tag-buffer tag))
((semantic-tag-file-name tag)
;; If it didn't have a buffer, but does have a file
;; name, then we need to get to that file so the tag
;; location is made accurate.
(find-file-noselect (semantic-tag-file-name tag)))
((and parent (semantic-tag-parent-buffer parent)))
;; Well, just assume things are in the current buffer.
(t (current-buffer)))))
;; We should be in the correct buffer now, try and figure out
;; where the tag is.
(cond ((semantic-tag-with-position-p tag)
;; If it's a number, go there
(goto-char (semantic-tag-start tag)))
((semantic-tag-with-position-p parent)
;; Otherwise, it's a trimmed vector, such as a parameter,
;; or a structure part. If there is a parent, we can use it
;; as a bounds for searching.
(goto-char (semantic-tag-start parent))
;; Here we make an assumption that the text returned by
;; the parser and concocted by us actually exists
;; in the buffer.
(re-search-forward (semantic-tag-name tag)
(semantic-tag-end parent)
t))
((semantic-tag-get-attribute tag :line)
;; The tag has a line number in it. Go there.
(goto-char (point-min))
(forward-line (1- (semantic-tag-get-attribute tag :line))))
((and (semantic-tag-p parent) (semantic-tag-get-attribute parent :line))
;; The tag has a line number in it. Go there.
(goto-char (point-min))
(forward-line (1- (semantic-tag-get-attribute parent :line)))
(re-search-forward (semantic-tag-name tag) nil t))
(t
;; Take a guess that the tag has a unique name, and just
;; search for it from the beginning of the buffer.
(goto-char (point-min))
(re-search-forward (semantic-tag-name tag) nil t)))
)
;;; Dependencies
;;
;; A tag which is of type 'include specifies a dependency.
;; Dependencies usually represent a file of some sort.
;; Find the file described by a dependency.
;;;###autoload
(define-overloadable-function semantic-dependency-tag-file (&optional tag)
"Find the filename represented from TAG.
Depends on `semantic-dependency-include-path' for searching. Always searches
`.' first, then searches additional paths."
(or tag (setq tag (car (semantic-find-tag-by-overlay nil))))
(unless (semantic-tag-of-class-p tag 'include)
(signal 'wrong-type-argument (list tag 'include)))
(save-excursion
(let ((result nil)
(default-directory default-directory)
(edefind nil)
(tag-fname nil))
(cond ((semantic-tag-in-buffer-p tag)
;; If the tag has an overlay and buffer associated with it,
;; switch to that buffer so that we get the right override methods.
(set-buffer (semantic-tag-buffer tag)))
((semantic-tag-file-name tag)
;; If it didn't have a buffer, but does have a file
;; name, then we need to get to that file so the tag
;; location is made accurate.
;;(set-buffer (find-file-noselect (semantic-tag-file-name tag)))
;;
;; 2/3/08
;; The above causes unnecessary buffer loads all over the place. Ick!
;; All we really need is for 'default-directory' to be set correctly.
(setq default-directory (file-name-directory (semantic-tag-file-name tag)))
))
;; Setup the filename represented by this include
(setq tag-fname (semantic-tag-include-filename tag))
;; First, see if this file exists in the current EDE project
(if (and (fboundp 'ede-expand-filename) ede-minor-mode
(setq edefind
(condition-case nil
(let ((proj (ede-toplevel)))
(when proj
(ede-expand-filename proj tag-fname)))
(error nil))))
(setq result edefind))
(if (not result)
(setq result
;; I don't have a plan for refreshing tags with a dependency
;; stuck on them somehow. I'm thinking that putting a cache
;; onto the dependency finding with a hash table might be best.
;;(if (semantic--tag-get-property tag 'dependency-file)
;; (semantic--tag-get-property tag 'dependency-file)
(:override
(save-excursion
(require 'semantic/dep)
(semantic-dependency-find-file-on-path
tag-fname (semantic-tag-include-system-p tag))))
;; )
))
(if (stringp result)
(progn
(semantic--tag-put-property tag 'dependency-file result)
result)
;; @todo: Do something to make this get flushed w/
;; when the path is changed.
;; @undo: Just eliminate
;; (semantic--tag-put-property tag 'dependency-file 'none)
nil)
)))
;;; PROTOTYPE FILE
;;
;; In C, a function in the .c file often has a representation in a
;; corresponding .h file. This routine attempts to find the
;; prototype file a given source file would be associated with.
;; This can be used by prototype manager programs.
(define-overloadable-function semantic-prototype-file (buffer)
"Return a file in which prototypes belonging to BUFFER should be placed.
Default behavior (if not overridden) looks for a token specifying the
prototype file, or the existence of an EDE variable indicating which
file prototypes belong in."
(:override
;; Perform some default behaviors
(if (and (fboundp 'ede-header-file) ede-minor-mode)
(with-current-buffer buffer
(ede-header-file))
;; No EDE options for a quick answer. Search.
(with-current-buffer buffer
(if (re-search-forward "::Header:: \\([a-zA-Z0-9.]+\\)" nil t)
(match-string 1))))))
(provide 'semantic/tag-file)
;; Local variables:
;; generated-autoload-file: "loaddefs.el"
;; generated-autoload-load-name: "semantic/tag-file"
;; End:
;;; semantic/tag-file.el ends here
|