Commit | Line | Data |
---|---|---|
417451fe | 1 | ;; ada-xref.el --- for lookup and completion in Ada mode |
797aab3c | 2 | |
034babe1 | 3 | ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
d91362c9 | 4 | ;; 2004, 2005, 2006 Free Software Foundation, Inc. |
797aab3c GM |
5 | |
6 | ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de> | |
7 | ;; Rolf Ebert <ebert@inf.enst.fr> | |
8 | ;; Emmanuel Briot <briot@gnat.com> | |
417451fe | 9 | ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org> |
797aab3c GM |
10 | ;; Keywords: languages ada xref |
11 | ||
874d7995 | 12 | ;; This file is part of GNU Emacs. |
797aab3c | 13 | |
2be7dabc | 14 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
797aab3c GM |
15 | ;; it under the terms of the GNU General Public License as published by |
16 | ;; the Free Software Foundation; either version 2, or (at your option) | |
17 | ;; any later version. | |
18 | ||
2be7dabc | 19 | ;; GNU Emacs is distributed in the hope that it will be useful, |
797aab3c GM |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
2be7dabc | 25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
26 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
27 | ;; Boston, MA 02110-1301, USA. | |
797aab3c GM |
28 | |
29 | ;;; Commentary: | |
30 | ;;; This Package provides a set of functions to use the output of the | |
31 | ;;; cross reference capabilities of the GNAT Ada compiler | |
32 | ;;; for lookup and completion in Ada mode. | |
33 | ;;; | |
797aab3c | 34 | ;;; If a file *.`adp' exists in the ada-file directory, then it is |
ced16f5d | 35 | ;;; read for configuration informations. It is read only the first |
797aab3c GM |
36 | ;;; time a cross-reference is asked for, and is not read later. |
37 | ||
38 | ;;; You need Emacs >= 20.2 to run this package | |
39 | ||
417451fe JB |
40 | |
41 | ;;; History: | |
42 | ;; | |
43 | ||
3afbc435 PJ |
44 | ;;; Code: |
45 | ||
797aab3c GM |
46 | ;; ----- Requirements ----------------------------------------------------- |
47 | ||
48 | (require 'compile) | |
49 | (require 'comint) | |
7abe197c JB |
50 | (require 'find-file) |
51 | (require 'ada-mode) | |
797aab3c | 52 | |
417451fe | 53 | ;; ------ User variables |
797aab3c | 54 | (defcustom ada-xref-other-buffer t |
eec3232e GM |
55 | "*If nil, always display the cross-references in the same buffer. |
56 | Otherwise create either a new buffer or a new frame." | |
797aab3c GM |
57 | :type 'boolean :group 'ada) |
58 | ||
93cdce20 | 59 | (defcustom ada-xref-create-ali nil |
eec3232e | 60 | "*If non-nil, run gcc whenever the cross-references are not up-to-date. |
ced16f5d | 61 | If nil, the cross-reference mode never runs gcc." |
797aab3c GM |
62 | :type 'boolean :group 'ada) |
63 | ||
64 | (defcustom ada-xref-confirm-compile nil | |
417451fe | 65 | "*Non-nil means ask for confirmation before compiling or running the application." |
797aab3c GM |
66 | :type 'boolean :group 'ada) |
67 | ||
68 | (defcustom ada-krunch-args "0" | |
ced16f5d RS |
69 | "*Maximum number of characters for filenames created by `gnatkr'. |
70 | Set to 0, if you don't use crunched filenames. This should be a string." | |
797aab3c GM |
71 | :type 'string :group 'ada) |
72 | ||
c94ca9e0 | 73 | (defcustom ada-gnatls-args '("-v") |
ced16f5d RS |
74 | "*Arguments to pass to `gnatfind' to find location of the runtime. |
75 | Typical use is to pass `--RTS=soft-floats' on some systems that support it. | |
c94ca9e0 | 76 | |
ced16f5d | 77 | You can also add `-I-' if you do not want the current directory to be included. |
c94ca9e0 | 78 | Otherwise, going from specs to bodies and back will first look for files in the |
ced16f5d | 79 | current directory. This only has an impact if you are not using project files, |
c94ca9e0 JB |
80 | but only ADA_INCLUDE_PATH." |
81 | :type '(repeat string) :group 'ada) | |
82 | ||
4884c50b | 83 | (defcustom ada-prj-default-comp-opt "-gnatq -gnatQ" |
15ea3b67 GM |
84 | "Default compilation options." |
85 | :type 'string :group 'ada) | |
86 | ||
87 | (defcustom ada-prj-default-bind-opt "" | |
88 | "Default binder options." | |
89 | :type 'string :group 'ada) | |
90 | ||
91 | (defcustom ada-prj-default-link-opt "" | |
92 | "Default linker options." | |
93 | :type 'string :group 'ada) | |
94 | ||
95 | (defcustom ada-prj-default-gnatmake-opt "-g" | |
ced16f5d | 96 | "Default options for `gnatmake'." |
15ea3b67 GM |
97 | :type 'string :group 'ada) |
98 | ||
4884c50b | 99 | (defcustom ada-prj-gnatfind-switches "-rf" |
ced16f5d RS |
100 | "Default switches to use for `gnatfind'. |
101 | You should modify this variable, for instance to add `-a', if you are working | |
4884c50b | 102 | in an environment where most ALI files are write-protected. |
ced16f5d | 103 | The command `gnatfind' is used every time you choose the menu |
4884c50b SM |
104 | \"Show all references\"." |
105 | :type 'string :group 'ada) | |
106 | ||
0262d5e1 JB |
107 | (defcustom ada-prj-default-check-cmd |
108 | (concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}" | |
109 | " -cargs ${comp_opt}") | |
110 | "*Default command to be used to compile a single file. | |
111 | Emacs will substitute the current filename for ${full_current}, or add | |
112 | the filename at the end. This is the same syntax as in the project file." | |
113 | :type 'string :group 'ada) | |
114 | ||
eec3232e | 115 | (defcustom ada-prj-default-comp-cmd |
93cdce20 SM |
116 | (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs" |
117 | " ${comp_opt}") | |
797aab3c | 118 | "*Default command to be used to compile a single file. |
417451fe JB |
119 | Emacs will substitute the current filename for ${full_current}, or add |
120 | the filename at the end. This is the same syntax as in the project file." | |
15ea3b67 GM |
121 | :type 'string :group 'ada) |
122 | ||
123 | (defcustom ada-prj-default-debugger "${cross_prefix}gdb" | |
417451fe | 124 | "*Default name of the debugger." |
797aab3c GM |
125 | :type 'string :group 'ada) |
126 | ||
127 | (defcustom ada-prj-default-make-cmd | |
15ea3b67 | 128 | (concat "${cross_prefix}gnatmake -o ${main} ${main_unit} ${gnatmake_opt} " |
417451fe | 129 | "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}") |
797aab3c GM |
130 | "*Default command to be used to compile the application. |
131 | This is the same syntax as in the project file." | |
132 | :type 'string :group 'ada) | |
133 | ||
134 | (defcustom ada-prj-default-project-file "" | |
417451fe JB |
135 | "*Name of the current project file. |
136 | Emacs will not try to use the search algorithm to find the project file if | |
137 | this string is not empty. It is set whenever a project file is found." | |
797aab3c GM |
138 | :type '(file :must-match t) :group 'ada) |
139 | ||
140 | (defcustom ada-gnatstub-opts "-q -I${src_dir}" | |
ced16f5d | 141 | "*List of the options to pass to `gnatsub' to generate the body of a package. |
eec3232e | 142 | This has the same syntax as in the project file (with variable substitution)." |
797aab3c GM |
143 | :type 'string :group 'ada) |
144 | ||
145 | (defcustom ada-always-ask-project nil | |
eec3232e | 146 | "*If nil, use default values when no project file was found. |
15ea3b67 GM |
147 | Otherwise, ask the user for the name of the project file to use." |
148 | :type 'boolean :group 'ada) | |
797aab3c | 149 | |
4884c50b | 150 | (defconst is-windows (memq system-type (quote (windows-nt))) |
ced16f5d | 151 | "True if we are running on Windows NT or Windows 95.") |
4884c50b SM |
152 | |
153 | (defcustom ada-tight-gvd-integration nil | |
154 | "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging. | |
75497862 KS |
155 | If GVD is not the debugger used, nothing happens." |
156 | :type 'boolean :group 'ada) | |
4884c50b | 157 | |
93cdce20 SM |
158 | (defcustom ada-xref-search-with-egrep t |
159 | "*If non-nil, use egrep to find the possible declarations for an entity. | |
160 | This alternate method is used when the exact location was not found in the | |
ced16f5d | 161 | information provided by GNAT. However, it might be expensive if you have a lot |
93cdce20 SM |
162 | of sources, since it will search in all the files in your project." |
163 | :type 'boolean :group 'ada) | |
164 | ||
165 | (defvar ada-load-project-hook nil | |
166 | "Hook that is run when loading a project file. | |
167 | Each function in this hook takes one argument FILENAME, that is the name of | |
168 | the project file to load. | |
169 | This hook should be used to support new formats for the project files. | |
170 | ||
171 | If the function can load the file with the given filename, it should create a | |
172 | buffer that contains a conversion of the file to the standard format of the | |
ced16f5d RS |
173 | project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\" |
174 | lines.) It should return nil if it doesn't know how to convert that project | |
93cdce20 SM |
175 | file.") |
176 | ||
177 | ||
797aab3c GM |
178 | ;; ------- Nothing to be modified by the user below this |
179 | (defvar ada-last-prj-file "" | |
eec3232e | 180 | "Name of the last project file entered by the user.") |
797aab3c | 181 | |
0262d5e1 | 182 | (defconst ada-prj-file-extension ".adp" |
eec3232e | 183 | "The extension used for project files.") |
797aab3c | 184 | |
15ea3b67 GM |
185 | (defvar ada-xref-runtime-library-specs-path '() |
186 | "Directories where the specs for the standard library is found. | |
187 | This is used for cross-references.") | |
188 | ||
189 | (defvar ada-xref-runtime-library-ali-path '() | |
190 | "Directories where the ali for the standard library is found. | |
191 | This is used for cross-references.") | |
192 | ||
797aab3c | 193 | (defvar ada-xref-pos-ring '() |
eec3232e GM |
194 | "List of positions selected by the cross-references functions. |
195 | Used to go back to these positions.") | |
797aab3c | 196 | |
4884c50b SM |
197 | (defvar ada-cd-command |
198 | (if (string-match "cmdproxy.exe" shell-file-name) | |
199 | "cd /d" | |
200 | "cd") | |
ced16f5d RS |
201 | "Command to use to change to a specific directory. |
202 | On Windows systems using `cmdproxy.exe' as the shell, | |
203 | we need to use `/d' or the drive is never changed.") | |
4884c50b SM |
204 | |
205 | (defvar ada-command-separator (if is-windows " && " "\n") | |
ced16f5d RS |
206 | "Separator to use between multiple commands to `compile' or `start-process'. |
207 | `cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use | |
4884c50b SM |
208 | \"&&\" for now.") |
209 | ||
797aab3c | 210 | (defconst ada-xref-pos-ring-max 16 |
eec3232e | 211 | "Number of positions kept in the list ada-xref-pos-ring.") |
797aab3c GM |
212 | |
213 | (defvar ada-operator-re | |
15ea3b67 | 214 | "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>" |
eec3232e | 215 | "Regexp to match for operators.") |
797aab3c | 216 | |
15ea3b67 | 217 | (defvar ada-xref-project-files '() |
0262d5e1 JB |
218 | "Associative list of project files with properties. |
219 | It has the format: (project project ...) | |
220 | A project has the format: (project-file . project-plist) | |
221 | \(See 'apropos plist' for operations on property lists). See | |
222 | ada-xref-set-default-prj-values for the list of valid properties. The | |
223 | current project is retrieved with ada-xref-current-project. Properties | |
224 | are retrieved with ada-xref-get-project-field, set with | |
225 | ada-xref-set-project-field. If project properties are accessed with no | |
226 | project file, a (nil . default-properties) entry is created.") | |
15ea3b67 | 227 | |
c94ca9e0 JB |
228 | |
229 | ;; ----- Identlist manipulation ------------------------------------------- | |
230 | ;; An identlist is a vector that is used internally to reference an identifier | |
231 | ;; To facilitate its use, we provide the following macros | |
232 | ||
233 | (defmacro ada-make-identlist () (make-vector 8 nil)) | |
234 | (defmacro ada-name-of (identlist) (list 'aref identlist 0)) | |
235 | (defmacro ada-line-of (identlist) (list 'aref identlist 1)) | |
236 | (defmacro ada-column-of (identlist) (list 'aref identlist 2)) | |
237 | (defmacro ada-file-of (identlist) (list 'aref identlist 3)) | |
238 | (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4)) | |
239 | (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5)) | |
240 | (defmacro ada-references-of (identlist) (list 'aref identlist 6)) | |
241 | (defmacro ada-on-declaration (identlist) (list 'aref identlist 7)) | |
242 | ||
243 | (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name)) | |
244 | (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line)) | |
245 | (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col)) | |
246 | (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file)) | |
247 | (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index)) | |
248 | (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file)) | |
249 | (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref)) | |
250 | (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value)) | |
251 | ||
252 | (defsubst ada-get-ali-buffer (file) | |
417451fe | 253 | "Read the ali file FILE into a new buffer, and return the buffer's name." |
c94ca9e0 JB |
254 | (find-file-noselect (ada-get-ali-file-name file))) |
255 | ||
256 | ||
257 | ;; ----------------------------------------------------------------------- | |
258 | ||
4884c50b | 259 | (defun ada-quote-cmd (cmd) |
ced16f5d | 260 | "Duplicate all \\ characters in CMD so that it can be passed to `compile'." |
4884c50b | 261 | (mapconcat 'identity (split-string cmd "\\\\") "\\\\")) |
15ea3b67 | 262 | |
0262d5e1 JB |
263 | (defun ada-find-executable (exec-name) |
264 | "Find the full path to the executable file EXEC-NAME. | |
265 | On Windows systems, this will properly handle .exe extension as well" | |
266 | (or (ada-find-file-in-dir exec-name exec-path) | |
267 | (ada-find-file-in-dir (concat exec-name ".exe") exec-path) | |
268 | exec-name)) | |
269 | ||
4884c50b | 270 | (defun ada-initialize-runtime-library (cross-prefix) |
ced16f5d | 271 | "Initialize the variables for the runtime library location. |
417451fe | 272 | CROSS-PREFIX is the prefix to use for the `gnatls' command." |
15ea3b67 | 273 | (save-excursion |
4884c50b SM |
274 | (setq ada-xref-runtime-library-specs-path '() |
275 | ada-xref-runtime-library-ali-path '()) | |
15ea3b67 GM |
276 | (set-buffer (get-buffer-create "*gnatls*")) |
277 | (widen) | |
278 | (erase-buffer) | |
279 | ;; Catch any error in the following form (i.e gnatls was not found) | |
280 | (condition-case nil | |
281 | ;; Even if we get an error, delete the *gnatls* buffer | |
282 | (unwind-protect | |
283 | (progn | |
0262d5e1 JB |
284 | (let ((gnatls |
285 | (ada-find-executable (concat cross-prefix "gnatls")))) | |
286 | (apply 'call-process gnatls (append '(nil t nil) ada-gnatls-args))) | |
15ea3b67 GM |
287 | (goto-char (point-min)) |
288 | ||
289 | ;; Source path | |
a1506d29 | 290 | |
15ea3b67 GM |
291 | (search-forward "Source Search Path:") |
292 | (forward-line 1) | |
293 | (while (not (looking-at "^$")) | |
294 | (back-to-indentation) | |
c94ca9e0 JB |
295 | (if (looking-at "<Current_Directory>") |
296 | (add-to-list 'ada-xref-runtime-library-specs-path ".") | |
15ea3b67 GM |
297 | (add-to-list 'ada-xref-runtime-library-specs-path |
298 | (buffer-substring-no-properties | |
299 | (point) | |
300 | (save-excursion (end-of-line) (point))))) | |
301 | (forward-line 1)) | |
302 | ||
303 | ;; Object path | |
a1506d29 | 304 | |
15ea3b67 GM |
305 | (search-forward "Object Search Path:") |
306 | (forward-line 1) | |
307 | (while (not (looking-at "^$")) | |
308 | (back-to-indentation) | |
c94ca9e0 JB |
309 | (if (looking-at "<Current_Directory>") |
310 | (add-to-list 'ada-xref-runtime-library-ali-path ".") | |
15ea3b67 GM |
311 | (add-to-list 'ada-xref-runtime-library-ali-path |
312 | (buffer-substring-no-properties | |
313 | (point) | |
314 | (save-excursion (end-of-line) (point))))) | |
315 | (forward-line 1)) | |
316 | ) | |
317 | (kill-buffer nil)) | |
318 | (error nil)) | |
319 | (set 'ada-xref-runtime-library-specs-path | |
320 | (reverse ada-xref-runtime-library-specs-path)) | |
321 | (set 'ada-xref-runtime-library-ali-path | |
322 | (reverse ada-xref-runtime-library-ali-path)) | |
323 | )) | |
324 | ||
325 | ||
326 | (defun ada-treat-cmd-string (cmd-string) | |
327 | "Replace meta-sequences like ${...} in CMD-STRING with the appropriate value. | |
417451fe JB |
328 | Assumes project exists. |
329 | As a special case, ${current} is replaced with the name of the current | |
330 | file, minus extension but with directory, and ${full_current} is | |
4884c50b | 331 | replaced by the name including the extension." |
15ea3b67 GM |
332 | |
333 | (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string) | |
4884c50b SM |
334 | (let (value |
335 | (name (match-string 2 cmd-string))) | |
336 | (cond | |
337 | ((string= name "current") | |
338 | (setq value (file-name-sans-extension (buffer-file-name)))) | |
339 | ((string= name "full_current") | |
340 | (setq value (buffer-file-name))) | |
341 | (t | |
15ea3b67 | 342 | (save-match-data |
4884c50b SM |
343 | (setq value (ada-xref-get-project-field (intern name)))))) |
344 | ||
345 | ;; Check if there is an environment variable with the same name | |
346 | (if (null value) | |
347 | (if (not (setq value (getenv name))) | |
29a4e67d | 348 | (message "%s" (concat "No environment variable " name " found")))) |
a1506d29 | 349 | |
15ea3b67 GM |
350 | (cond |
351 | ((null value) | |
4884c50b | 352 | (setq cmd-string (replace-match "" t t cmd-string))) |
15ea3b67 | 353 | ((stringp value) |
4884c50b | 354 | (setq cmd-string (replace-match value t t cmd-string))) |
15ea3b67 GM |
355 | ((listp value) |
356 | (let ((prefix (match-string 1 cmd-string))) | |
4884c50b | 357 | (setq cmd-string (replace-match |
15ea3b67 GM |
358 | (mapconcat (lambda(x) (concat prefix x)) value " ") |
359 | t t cmd-string))))) | |
360 | )) | |
361 | cmd-string) | |
362 | ||
363 | (defun ada-xref-set-default-prj-values (symbol ada-buffer) | |
364 | "Reset the properties in SYMBOL to the default values for ADA-BUFFER." | |
365 | ||
366 | (let ((file (buffer-file-name ada-buffer)) | |
367 | plist) | |
368 | (save-excursion | |
369 | (set-buffer ada-buffer) | |
a1506d29 | 370 | |
15ea3b67 | 371 | (set 'plist |
417451fe JB |
372 | ;; Try hard to find a project file, even if the current |
373 | ;; buffer is not an Ada file or not associated with a file | |
4884c50b SM |
374 | (list 'filename (expand-file-name |
375 | (cond | |
4884c50b SM |
376 | (ada-prj-default-project-file |
377 | ada-prj-default-project-file) | |
c94ca9e0 | 378 | (file (ada-prj-find-prj-file file t)) |
4884c50b SM |
379 | (t |
380 | (message (concat "Not editing an Ada file," | |
381 | "and no default project " | |
382 | "file specified!")) | |
383 | ""))) | |
15ea3b67 GM |
384 | 'build_dir (file-name-as-directory (expand-file-name ".")) |
385 | 'src_dir (list ".") | |
386 | 'obj_dir (list ".") | |
387 | 'casing (if (listp ada-case-exception-file) | |
388 | ada-case-exception-file | |
389 | (list ada-case-exception-file)) | |
390 | 'comp_opt ada-prj-default-comp-opt | |
391 | 'bind_opt ada-prj-default-bind-opt | |
392 | 'link_opt ada-prj-default-link-opt | |
393 | 'gnatmake_opt ada-prj-default-gnatmake-opt | |
4884c50b | 394 | 'gnatfind_opt ada-prj-gnatfind-switches |
15ea3b67 | 395 | 'main (if file |
4884c50b SM |
396 | (file-name-nondirectory |
397 | (file-name-sans-extension file)) | |
15ea3b67 GM |
398 | "") |
399 | 'main_unit (if file | |
400 | (file-name-nondirectory | |
401 | (file-name-sans-extension file)) | |
402 | "") | |
403 | 'cross_prefix "" | |
404 | 'remote_machine "" | |
0262d5e1 JB |
405 | 'comp_cmd (list ada-prj-default-comp-cmd) |
406 | 'check_cmd (list ada-prj-default-check-cmd) | |
407 | 'make_cmd (list ada-prj-default-make-cmd) | |
408 | 'run_cmd (list (concat "./${main}" (if is-windows ".exe"))) | |
409 | 'debug_pre_cmd (list (concat ada-cd-command " ${build_dir}")) | |
15ea3b67 | 410 | 'debug_cmd (concat ada-prj-default-debugger |
0262d5e1 | 411 | " ${main}" (if is-windows ".exe")) |
4884c50b | 412 | 'debug_post_cmd (list nil))) |
15ea3b67 GM |
413 | ) |
414 | (set symbol plist))) | |
a1506d29 | 415 | |
15ea3b67 | 416 | (defun ada-xref-get-project-field (field) |
4884c50b | 417 | "Extract the value of FIELD from the current project file. |
417451fe | 418 | Project variables are substituted. |
4884c50b SM |
419 | |
420 | Note that for src_dir and obj_dir, you should rather use | |
421 | `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field' which will in | |
422 | addition return the default paths." | |
15ea3b67 | 423 | |
4884c50b | 424 | (let ((file-name ada-prj-default-project-file) |
15ea3b67 GM |
425 | file value) |
426 | ||
4884c50b SM |
427 | ;; Get the project file (either the current one, or a default one) |
428 | (setq file (or (assoc file-name ada-xref-project-files) | |
429 | (assoc nil ada-xref-project-files))) | |
a1506d29 | 430 | |
15ea3b67 GM |
431 | ;; If the file was not found, use the default values |
432 | (if file | |
433 | ;; Get the value from the file | |
434 | (set 'value (plist-get (cdr file) field)) | |
435 | ||
436 | ;; Create a default nil file that contains the default values | |
437 | (ada-xref-set-default-prj-values 'value (current-buffer)) | |
438 | (add-to-list 'ada-xref-project-files (cons nil value)) | |
4884c50b | 439 | (ada-xref-update-project-menu) |
15ea3b67 GM |
440 | (set 'value (plist-get value field)) |
441 | ) | |
4884c50b SM |
442 | |
443 | ;; Substitute the ${...} constructs in all the strings, including | |
444 | ;; inside lists | |
445 | (cond | |
446 | ((stringp value) | |
447 | (ada-treat-cmd-string value)) | |
448 | ((null value) | |
449 | nil) | |
450 | ((listp value) | |
451 | (mapcar (lambda(x) (if x (ada-treat-cmd-string x) x)) value)) | |
452 | (t | |
453 | value) | |
454 | ) | |
455 | )) | |
456 | ||
4884c50b SM |
457 | (defun ada-xref-get-src-dir-field () |
458 | "Return the full value for src_dir, including the default directories. | |
459 | All the directories are returned as absolute directories." | |
460 | ||
461 | (let ((build-dir (ada-xref-get-project-field 'build_dir))) | |
462 | (append | |
463 | ;; Add ${build_dir} in front of the path | |
464 | (list build-dir) | |
a1506d29 | 465 | |
4884c50b SM |
466 | (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir) |
467 | build-dir) | |
a1506d29 | 468 | |
4884c50b SM |
469 | ;; Add the standard runtime at the end |
470 | ada-xref-runtime-library-specs-path))) | |
471 | ||
472 | (defun ada-xref-get-obj-dir-field () | |
473 | "Return the full value for obj_dir, including the default directories. | |
474 | All the directories are returned as absolute directories." | |
475 | ||
476 | (let ((build-dir (ada-xref-get-project-field 'build_dir))) | |
477 | (append | |
478 | ;; Add ${build_dir} in front of the path | |
479 | (list build-dir) | |
a1506d29 | 480 | |
4884c50b SM |
481 | (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir) |
482 | build-dir) | |
a1506d29 | 483 | |
4884c50b SM |
484 | ;; Add the standard runtime at the end |
485 | ada-xref-runtime-library-ali-path))) | |
486 | ||
487 | (defun ada-xref-update-project-menu () | |
488 | "Update the menu Ada->Project, with the list of available project files." | |
96a483d9 SM |
489 | ;; Create the standard items. |
490 | (let ((submenu | |
491 | `("Project" | |
492 | ["Load..." ada-set-default-project-file t] | |
493 | ["New..." ada-prj-new t] | |
494 | ["Edit..." ada-prj-edit t] | |
495 | "---" | |
496 | ;; Add the new items | |
497 | ,@(mapcar | |
498 | (lambda (x) | |
499 | (let ((name (or (car x) "<default>")) | |
500 | (command `(lambda () | |
501 | "Change the active project file." | |
502 | (interactive) | |
503 | (ada-parse-prj-file ,(car x)) | |
504 | (set 'ada-prj-default-project-file ,(car x)) | |
505 | (ada-xref-update-project-menu)))) | |
506 | (vector | |
507 | (if (string= (file-name-extension name) | |
0262d5e1 | 508 | ada-prj-file-extension) |
96a483d9 SM |
509 | (file-name-sans-extension |
510 | (file-name-nondirectory name)) | |
511 | (file-name-nondirectory name)) | |
512 | command | |
513 | :button (cons | |
514 | :toggle | |
515 | (equal ada-prj-default-project-file | |
516 | (car x)) | |
517 | )))) | |
75497862 | 518 | |
96a483d9 SM |
519 | ;; Parses all the known project files, and insert at |
520 | ;; least the default one (in case | |
521 | ;; ada-xref-project-files is nil) | |
522 | (or ada-xref-project-files '(nil)))))) | |
75497862 | 523 | |
96a483d9 | 524 | (easy-menu-add-item ada-mode-menu '() submenu))) |
4884c50b SM |
525 | |
526 | ||
527 | ;;------------------------------------------------------------- | |
528 | ;;-- Searching a file anywhere on the source path. | |
529 | ;;-- | |
530 | ;;-- The following functions provide support for finding a file anywhere | |
531 | ;;-- on the source path, without providing an explicit directory. | |
532 | ;;-- They also provide file name completion in the minibuffer. | |
533 | ;;-- | |
534 | ;;-- Public subprograms: ada-find-file | |
535 | ;;-- | |
536 | ;;------------------------------------------------------------- | |
537 | ||
538 | (defun ada-do-file-completion (string predicate flag) | |
539 | "Completion function when reading a file from the minibuffer. | |
540 | Completion is attempted in all the directories in the source path, as | |
541 | defined in the project file." | |
417451fe | 542 | ;; FIXME: doc arguments |
4884c50b SM |
543 | (let (list |
544 | (dirs (ada-xref-get-src-dir-field))) | |
545 | ||
546 | (while dirs | |
547 | (if (file-directory-p (car dirs)) | |
548 | (set 'list (append list (file-name-all-completions string (car dirs))))) | |
549 | (set 'dirs (cdr dirs))) | |
550 | (cond ((equal flag 'lambda) | |
551 | (assoc string list)) | |
552 | (flag | |
553 | list) | |
554 | (t | |
555 | (try-completion string | |
556 | (mapcar (lambda (x) (cons x 1)) list) | |
557 | predicate))))) | |
558 | ||
559 | ;;;###autoload | |
560 | (defun ada-find-file (filename) | |
417451fe | 561 | "Open FILENAME, from anywhere in the source path. |
4884c50b SM |
562 | Completion is available." |
563 | (interactive | |
564 | (list (completing-read "File: " 'ada-do-file-completion))) | |
565 | (let ((file (ada-find-src-file-in-dir filename))) | |
566 | (if file | |
567 | (find-file file) | |
568 | (error (concat filename " not found in src_dir"))))) | |
569 | ||
797aab3c | 570 | |
797aab3c GM |
571 | ;; ----- Utilities ------------------------------------------------- |
572 | ||
573 | (defun ada-require-project-file () | |
4884c50b SM |
574 | "If no project file is currently active, load a default one." |
575 | (if (or (not ada-prj-default-project-file) | |
576 | (not ada-xref-project-files) | |
577 | (string= ada-prj-default-project-file "")) | |
15ea3b67 | 578 | (ada-reread-prj-file))) |
a1506d29 | 579 | |
797aab3c | 580 | (defun ada-xref-push-pos (filename position) |
eec3232e | 581 | "Push (FILENAME, POSITION) on the position ring for cross-references." |
797aab3c GM |
582 | (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring)) |
583 | (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max) | |
584 | (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil))) | |
585 | ||
586 | (defun ada-xref-goto-previous-reference () | |
eec3232e | 587 | "Go to the previous cross-reference we were on." |
797aab3c GM |
588 | (interactive) |
589 | (if ada-xref-pos-ring | |
eec3232e GM |
590 | (let ((pos (car ada-xref-pos-ring))) |
591 | (setq ada-xref-pos-ring (cdr ada-xref-pos-ring)) | |
592 | (find-file (car (cdr pos))) | |
593 | (goto-char (car pos))))) | |
797aab3c GM |
594 | |
595 | (defun ada-convert-file-name (name) | |
417451fe | 596 | "Convert from NAME to a name that can be used by the compilation commands. |
eec3232e | 597 | This is overriden on VMS to convert from VMS filenames to Unix filenames." |
797aab3c | 598 | name) |
417451fe | 599 | ;; FIXME: use convert-standard-filename instead |
797aab3c | 600 | |
c94ca9e0 JB |
601 | (defun ada-set-default-project-file (name &optional keep-existing) |
602 | "Set the file whose name is NAME as the default project file. | |
603 | If KEEP-EXISTING is true and a project file has already been loaded, nothing | |
ced16f5d | 604 | is done. This is meant to be used from `ada-mode-hook', for instance, to force |
c94ca9e0 | 605 | a project file unless the user has already loaded one." |
eec3232e | 606 | (interactive "fProject file:") |
c94ca9e0 JB |
607 | (if (or (not keep-existing) |
608 | (not ada-prj-default-project-file) | |
609 | (equal ada-prj-default-project-file "")) | |
610 | (progn | |
611 | (setq ada-prj-default-project-file name) | |
612 | (ada-reread-prj-file name)))) | |
15ea3b67 GM |
613 | |
614 | ;; ------ Handling the project file ----------------------------- | |
797aab3c | 615 | |
c94ca9e0 JB |
616 | (defun ada-prj-find-prj-file (&optional file no-user-question) |
617 | "Find the prj file associated with FILE (or the current buffer if nil). | |
15ea3b67 GM |
618 | If NO-USER-QUESTION is non-nil, use a default file if not project file was |
619 | found, and do not ask the user. | |
620 | If the buffer is not an Ada buffer, associate it with the default project | |
ced16f5d | 621 | file. If none is set, return nil." |
797aab3c | 622 | |
15ea3b67 | 623 | (let (selected) |
797aab3c | 624 | |
4884c50b SM |
625 | ;; Use the active project file if there is one. |
626 | ;; This is also valid if we don't currently have an Ada buffer, or if | |
627 | ;; the current buffer is not a real file (for instance an emerge buffer) | |
a1506d29 | 628 | |
15ea3b67 | 629 | (if (or (not (string= mode-name "Ada")) |
c94ca9e0 JB |
630 | (not (buffer-file-name))) |
631 | ||
632 | (if (and ada-prj-default-project-file | |
633 | (not (string= ada-prj-default-project-file ""))) | |
634 | (setq selected ada-prj-default-project-file) | |
635 | (setq selected nil)) | |
a1506d29 | 636 | |
4884c50b | 637 | ;; other cases: use a more complex algorithm |
a1506d29 | 638 | |
c94ca9e0 | 639 | (let* ((current-file (or file (buffer-file-name))) |
4884c50b SM |
640 | (first-choice (concat |
641 | (file-name-sans-extension current-file) | |
0262d5e1 | 642 | ada-prj-file-extension)) |
4884c50b | 643 | (dir (file-name-directory current-file)) |
a1506d29 | 644 | |
4884c50b SM |
645 | ;; on Emacs 20.2, directory-files does not work if |
646 | ;; parse-sexp-lookup-properties is set | |
647 | (parse-sexp-lookup-properties nil) | |
648 | (prj-files (directory-files | |
649 | dir t | |
650 | (concat ".*" (regexp-quote | |
0262d5e1 | 651 | ada-prj-file-extension) "$"))) |
4884c50b | 652 | (choice nil)) |
a1506d29 | 653 | |
4884c50b | 654 | (cond |
a1506d29 | 655 | |
4884c50b SM |
656 | ;; Else if there is a project file with the same name as the Ada |
657 | ;; file, but not the same extension. | |
658 | ((file-exists-p first-choice) | |
659 | (set 'selected first-choice)) | |
a1506d29 | 660 | |
4884c50b SM |
661 | ;; Else if only one project file was found in the current directory |
662 | ((= (length prj-files) 1) | |
663 | (set 'selected (car prj-files))) | |
a1506d29 | 664 | |
4884c50b SM |
665 | ;; Else if there are multiple files, ask the user |
666 | ((and (> (length prj-files) 1) (not no-user-question)) | |
667 | (save-window-excursion | |
668 | (with-output-to-temp-buffer "*choice list*" | |
669 | (princ "There are more than one possible project file.\n") | |
670 | (princ "Which one should we use ?\n\n") | |
671 | (princ " no. file name \n") | |
672 | (princ " --- ------------------------\n") | |
673 | (let ((counter 1)) | |
674 | (while (<= counter (length prj-files)) | |
675 | (princ (format " %2d) %s\n" | |
676 | counter | |
677 | (nth (1- counter) prj-files))) | |
678 | (setq counter (1+ counter)) | |
c94ca9e0 | 679 | |
4884c50b SM |
680 | ))) ; end of with-output-to ... |
681 | (setq choice nil) | |
682 | (while (or | |
683 | (not choice) | |
684 | (not (integerp choice)) | |
685 | (< choice 1) | |
686 | (> choice (length prj-files))) | |
027a4b6b | 687 | (setq choice (string-to-number |
4884c50b SM |
688 | (read-from-minibuffer "Enter No. of your choice: ")))) |
689 | (set 'selected (nth (1- choice) prj-files)))) | |
a1506d29 | 690 | |
4884c50b SM |
691 | ;; Else if no project file was found in the directory, ask a name |
692 | ;; to the user, using as a default value the last one entered by | |
693 | ;; the user | |
694 | ((= (length prj-files) 0) | |
695 | (unless (or no-user-question (not ada-always-ask-project)) | |
696 | (setq ada-last-prj-file | |
697 | (read-file-name | |
698 | (concat "project file [" ada-last-prj-file "]:") | |
699 | nil ada-last-prj-file)) | |
700 | (unless (string= ada-last-prj-file "") | |
701 | (set 'selected ada-last-prj-file)))) | |
702 | ))) | |
c94ca9e0 JB |
703 | |
704 | (or selected "default.adp") | |
797aab3c GM |
705 | )) |
706 | ||
707 | ||
15ea3b67 | 708 | (defun ada-parse-prj-file (prj-file) |
417451fe JB |
709 | "Read PRJ-FILE, set it as the active project." |
710 | ;; FIXME: doc nil, search, etc. | |
15ea3b67 | 711 | (if prj-file |
4884c50b SM |
712 | (let (project src_dir obj_dir make_cmd comp_cmd check_cmd casing |
713 | run_cmd debug_pre_cmd debug_post_cmd | |
417451fe | 714 | (ada-buffer (current-buffer))) |
4884c50b | 715 | (setq prj-file (expand-file-name prj-file)) |
15ea3b67 | 716 | |
c94ca9e0 JB |
717 | ;; Set the project file as the active one. |
718 | (setq ada-prj-default-project-file prj-file) | |
719 | ||
15ea3b67 GM |
720 | ;; Initialize the project with the default values |
721 | (ada-xref-set-default-prj-values 'project (current-buffer)) | |
722 | ||
723 | ;; Do not use find-file below, since we don't want to show this | |
ced16f5d | 724 | ;; buffer. If the file is open through speedbar, we can't use |
15ea3b67 GM |
725 | ;; find-file anyway, since the speedbar frame is special and does not |
726 | ;; allow the selection of a file in it. | |
727 | ||
c94ca9e0 JB |
728 | (if (file-exists-p prj-file) |
729 | (progn | |
730 | (let* ((buffer (run-hook-with-args-until-success | |
731 | 'ada-load-project-hook prj-file))) | |
732 | (unless buffer | |
7abe197c JB |
733 | (setq buffer (find-file-noselect prj-file nil))) |
734 | (set-buffer buffer)) | |
735 | ||
736 | (widen) | |
737 | (goto-char (point-min)) | |
738 | ||
739 | ;; Now overrides these values with the project file | |
740 | (while (not (eobp)) | |
741 | (if (looking-at "^\\([^=]+\\)=\\(.*\\)") | |
742 | (cond | |
417451fe JB |
743 | ;; fields that are lists or paths require special processing |
744 | ;; FIXME: strip trailing spaces | |
7abe197c JB |
745 | ((string= (match-string 1) "src_dir") |
746 | (add-to-list 'src_dir | |
747 | (file-name-as-directory (match-string 2)))) | |
748 | ((string= (match-string 1) "obj_dir") | |
749 | (add-to-list 'obj_dir | |
750 | (file-name-as-directory (match-string 2)))) | |
751 | ((string= (match-string 1) "casing") | |
752 | (set 'casing (cons (match-string 2) casing))) | |
753 | ((string= (match-string 1) "build_dir") | |
754 | (set 'project | |
755 | (plist-put project 'build_dir | |
756 | (file-name-as-directory (match-string 2))))) | |
757 | ((string= (match-string 1) "make_cmd") | |
758 | (add-to-list 'make_cmd (match-string 2))) | |
759 | ((string= (match-string 1) "comp_cmd") | |
760 | (add-to-list 'comp_cmd (match-string 2))) | |
761 | ((string= (match-string 1) "check_cmd") | |
762 | (add-to-list 'check_cmd (match-string 2))) | |
763 | ((string= (match-string 1) "run_cmd") | |
764 | (add-to-list 'run_cmd (match-string 2))) | |
765 | ((string= (match-string 1) "debug_pre_cmd") | |
766 | (add-to-list 'debug_pre_cmd (match-string 2))) | |
767 | ((string= (match-string 1) "debug_post_cmd") | |
768 | (add-to-list 'debug_post_cmd (match-string 2))) | |
769 | (t | |
417451fe | 770 | ;; any other field in the file is just copied |
7abe197c JB |
771 | (set 'project (plist-put project (intern (match-string 1)) |
772 | (match-string 2)))))) | |
773 | (forward-line 1)) | |
774 | ||
775 | (if src_dir (set 'project (plist-put project 'src_dir | |
776 | (reverse src_dir)))) | |
777 | (if obj_dir (set 'project (plist-put project 'obj_dir | |
778 | (reverse obj_dir)))) | |
779 | (if casing (set 'project (plist-put project 'casing | |
780 | (reverse casing)))) | |
781 | (if make_cmd (set 'project (plist-put project 'make_cmd | |
782 | (reverse make_cmd)))) | |
783 | (if comp_cmd (set 'project (plist-put project 'comp_cmd | |
784 | (reverse comp_cmd)))) | |
785 | (if check_cmd (set 'project (plist-put project 'check_cmd | |
786 | (reverse check_cmd)))) | |
787 | (if run_cmd (set 'project (plist-put project 'run_cmd | |
788 | (reverse run_cmd)))) | |
0262d5e1 JB |
789 | (if debug_post_cmd (set 'project (plist-put project 'debug_post_cmd |
790 | (reverse debug_post_cmd)))) | |
791 | (if debug_pre_cmd (set 'project (plist-put project 'debug_pre_cmd | |
792 | (reverse debug_pre_cmd)))) | |
c94ca9e0 JB |
793 | |
794 | ;; Kill the project buffer | |
795 | (kill-buffer nil) | |
796 | (set-buffer ada-buffer) | |
797 | ) | |
798 | ||
799 | ;; Else the file wasn't readable (probably the default project). | |
800 | ;; We initialize it with the current environment variables. | |
417451fe JB |
801 | ;; We need to add the startup directory in front so that |
802 | ;; files locally redefined are properly found. We cannot | |
803 | ;; add ".", which varies too much depending on what the | |
804 | ;; current buffer is. | |
c94ca9e0 JB |
805 | (set 'project |
806 | (plist-put project 'src_dir | |
807 | (append | |
417451fe | 808 | (list command-line-default-directory) |
c94ca9e0 JB |
809 | (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":") |
810 | (list "." default-directory)))) | |
811 | (set 'project | |
812 | (plist-put project 'obj_dir | |
813 | (append | |
417451fe | 814 | (list command-line-default-directory) |
c94ca9e0 JB |
815 | (split-string (or (getenv "ADA_OBJECTS_PATH") "") ":") |
816 | (list "." default-directory)))) | |
817 | ) | |
818 | ||
4884c50b SM |
819 | |
820 | ;; Delete the default project file from the list, if it is there. | |
821 | ;; Note that in that case, this default project is the only one in | |
822 | ;; the list | |
823 | (if (assoc nil ada-xref-project-files) | |
824 | (setq ada-xref-project-files nil)) | |
a1506d29 | 825 | |
15ea3b67 GM |
826 | ;; Memorize the newly read project file |
827 | (if (assoc prj-file ada-xref-project-files) | |
828 | (setcdr (assoc prj-file ada-xref-project-files) project) | |
829 | (add-to-list 'ada-xref-project-files (cons prj-file project))) | |
4884c50b | 830 | |
15ea3b67 GM |
831 | ;; Sets up the compilation-search-path so that Emacs is able to |
832 | ;; go to the source of the errors in a compilation buffer | |
4884c50b SM |
833 | (setq compilation-search-path (ada-xref-get-src-dir-field)) |
834 | ||
417451fe JB |
835 | ;; Set the casing exceptions file list |
836 | (if casing | |
837 | (progn | |
838 | (setq ada-case-exception-file (reverse casing)) | |
839 | (ada-case-read-exceptions))) | |
a1506d29 | 840 | |
15ea3b67 GM |
841 | ;; Add the directories to the search path for ff-find-other-file |
842 | ;; Do not add the '/' or '\' at the end | |
7abe197c | 843 | (setq ada-search-directories-internal |
15ea3b67 GM |
844 | (append (mapcar 'directory-file-name compilation-search-path) |
845 | ada-search-directories)) | |
a1506d29 | 846 | |
4884c50b | 847 | (ada-xref-update-project-menu) |
15ea3b67 | 848 | ) |
4884c50b SM |
849 | |
850 | ;; No prj file ? => Setup default values | |
851 | ;; Note that nil means that all compilation modes will first look in the | |
ced16f5d | 852 | ;; current directory, and only then in the current file's directory. This |
4884c50b SM |
853 | ;; current file is assumed at this point to be in the common source |
854 | ;; directory. | |
855 | (setq compilation-search-path (list nil default-directory)) | |
15ea3b67 | 856 | )) |
a1506d29 JB |
857 | |
858 | ||
93cdce20 | 859 | (defun ada-find-references (&optional pos arg local-only) |
eec3232e | 860 | "Find all references to the entity under POS. |
93cdce20 | 861 | Calls gnatfind to find the references. |
ced16f5d RS |
862 | If ARG is t, the contents of the old *gnatfind* buffer is preserved. |
863 | If LOCAL-ONLY is t, only the declarations in the current file are returned." | |
864 | (interactive "d\nP") | |
797aab3c GM |
865 | (ada-require-project-file) |
866 | ||
867 | (let* ((identlist (ada-read-identifier pos)) | |
417451fe | 868 | (alifile (ada-get-ali-file-name (ada-file-of identlist))) |
15ea3b67 | 869 | (process-environment (ada-set-environment))) |
797aab3c GM |
870 | |
871 | (set-buffer (get-file-buffer (ada-file-of identlist))) | |
872 | ||
873 | ;; if the file is more recent than the executable | |
874 | (if (or (buffer-modified-p (current-buffer)) | |
417451fe JB |
875 | (file-newer-than-file-p (ada-file-of identlist) alifile)) |
876 | (ada-find-any-references (ada-name-of identlist) | |
877 | (ada-file-of identlist) | |
878 | nil nil local-only arg) | |
797aab3c | 879 | (ada-find-any-references (ada-name-of identlist) |
417451fe JB |
880 | (ada-file-of identlist) |
881 | (ada-line-of identlist) | |
882 | (ada-column-of identlist) local-only arg))) | |
797aab3c GM |
883 | ) |
884 | ||
93cdce20 SM |
885 | (defun ada-find-local-references (&optional pos arg) |
886 | "Find all references to the entity under POS. | |
ced16f5d RS |
887 | Calls `gnatfind' to find the references. |
888 | If ARG is t, the contents of the old *gnatfind* buffer is preserved." | |
889 | (interactive "d\nP") | |
93cdce20 SM |
890 | (ada-find-references pos arg t)) |
891 | ||
892 | (defun ada-find-any-references | |
893 | (entity &optional file line column local-only append) | |
eec3232e | 894 | "Search for references to any entity whose name is ENTITY. |
93cdce20 | 895 | ENTITY was first found the location given by FILE, LINE and COLUMN. |
ced16f5d | 896 | If LOCAL-ONLY is t, then list only the references in FILE, which |
93cdce20 | 897 | is much faster. |
ced16f5d RS |
898 | If APPEND is t, then append the output of the command to the existing |
899 | buffer `*gnatfind*', if there is one." | |
797aab3c GM |
900 | (interactive "sEntity name: ") |
901 | (ada-require-project-file) | |
902 | ||
ced16f5d | 903 | ;; Prepare the gnatfind command. Note that we must protect the quotes |
4884c50b SM |
904 | ;; around operators, so that they are correctly handled and can be |
905 | ;; processed (gnatfind \"+\":...). | |
906 | (let* ((quote-entity | |
907 | (if (= (aref entity 0) ?\") | |
908 | (if is-windows | |
909 | (concat "\\\"" (substring entity 1 -1) "\\\"") | |
910 | (concat "'\"" (substring entity 1 -1) "\"'")) | |
911 | entity)) | |
912 | (switches (ada-xref-get-project-field 'gnatfind_opt)) | |
7abe197c | 913 | (command (concat "gnat find " switches " " |
4884c50b | 914 | quote-entity |
417451fe JB |
915 | (if file (concat ":" (file-name-nondirectory file))) |
916 | (if line (concat ":" line)) | |
917 | (if column (concat ":" column)) | |
93cdce20 SM |
918 | (if local-only (concat " " (file-name-nondirectory file))) |
919 | )) | |
920 | old-contents) | |
797aab3c GM |
921 | |
922 | ;; If a project file is defined, use it | |
4884c50b SM |
923 | (if (and ada-prj-default-project-file |
924 | (not (string= ada-prj-default-project-file ""))) | |
417451fe JB |
925 | (if (string-equal (file-name-extension ada-prj-default-project-file) |
926 | "gpr") | |
927 | (setq command (concat command " -P" ada-prj-default-project-file)) | |
928 | (setq command (concat command " -p" ada-prj-default-project-file)))) | |
797aab3c | 929 | |
93cdce20 SM |
930 | (if (and append (get-buffer "*gnatfind*")) |
931 | (save-excursion | |
932 | (set-buffer "*gnatfind*") | |
933 | (setq old-contents (buffer-string)))) | |
a1506d29 | 934 | |
ced16f5d RS |
935 | (let ((compilation-error "reference")) |
936 | (compilation-start command)) | |
797aab3c GM |
937 | |
938 | ;; Hide the "Compilation" menu | |
939 | (save-excursion | |
940 | (set-buffer "*gnatfind*") | |
93cdce20 SM |
941 | (local-unset-key [menu-bar compilation-menu]) |
942 | ||
943 | (if old-contents | |
944 | (progn | |
945 | (goto-char 1) | |
946 | (insert old-contents) | |
947 | (goto-char (point-max))))) | |
797aab3c GM |
948 | ) |
949 | ) | |
950 | ||
4884c50b | 951 | (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file)) |
797aab3c | 952 | |
797aab3c GM |
953 | ;; ----- Identifier Completion -------------------------------------------- |
954 | (defun ada-complete-identifier (pos) | |
417451fe | 955 | "Try to complete the identifier around POS, using compiler cross-reference information." |
797aab3c GM |
956 | (interactive "d") |
957 | (ada-require-project-file) | |
958 | ||
15ea3b67 | 959 | ;; Initialize function-local variables and jump to the .ali buffer |
797aab3c GM |
960 | ;; Note that for regexp search is case insensitive too |
961 | (let* ((curbuf (current-buffer)) | |
417451fe JB |
962 | (identlist (ada-read-identifier pos)) |
963 | (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\(" | |
964 | (regexp-quote (ada-name-of identlist)) | |
965 | "[a-zA-Z0-9_]*\\)")) | |
966 | (completed nil) | |
967 | (symalist nil)) | |
797aab3c | 968 | |
15ea3b67 GM |
969 | ;; Open the .ali file |
970 | (set-buffer (ada-get-ali-buffer (buffer-file-name))) | |
797aab3c GM |
971 | (goto-char (point-max)) |
972 | ||
973 | ;; build an alist of possible completions | |
974 | (while (re-search-backward sofar nil t) | |
975 | (setq symalist (cons (cons (match-string 1) nil) symalist))) | |
976 | ||
977 | (setq completed (try-completion "" symalist)) | |
978 | ||
979 | ;; kills .ali buffer | |
980 | (kill-buffer nil) | |
981 | ||
982 | ;; deletes the incomplete identifier in the buffer | |
983 | (set-buffer curbuf) | |
984 | (looking-at "[a-zA-Z0-9_]+") | |
985 | (replace-match "") | |
986 | ;; inserts the completed symbol | |
987 | (insert completed) | |
988 | )) | |
989 | ||
990 | ;; ----- Cross-referencing ---------------------------------------- | |
991 | ||
992 | (defun ada-point-and-xref () | |
c94ca9e0 | 993 | "Jump to the declaration of the entity below the cursor." |
797aab3c GM |
994 | (interactive) |
995 | (mouse-set-point last-input-event) | |
996 | (ada-goto-declaration (point))) | |
997 | ||
c94ca9e0 JB |
998 | (defun ada-point-and-xref-body () |
999 | "Jump to the body of the entity under the cursor." | |
1000 | (interactive) | |
1001 | (mouse-set-point last-input-event) | |
1002 | (ada-goto-body (point))) | |
1003 | ||
1004 | (defun ada-goto-body (pos &optional other-frame) | |
1005 | "Display the body of the entity around POS. | |
417451fe | 1006 | OTHER-FRAME non-nil means display in another frame. |
c94ca9e0 JB |
1007 | If the entity doesn't have a body, display its declaration. |
1008 | As a side effect, the buffer for the declaration is also open." | |
1009 | (interactive "d") | |
1010 | (ada-goto-declaration pos other-frame) | |
1011 | ||
1012 | ;; Temporarily force the display in the same buffer, since we | |
1013 | ;; already changed previously | |
1014 | (let ((ada-xref-other-buffer nil)) | |
1015 | (ada-goto-declaration (point) nil))) | |
1016 | ||
4884c50b | 1017 | (defun ada-goto-declaration (pos &optional other-frame) |
eec3232e GM |
1018 | "Display the declaration of the identifier around POS. |
1019 | The declaration is shown in another buffer if `ada-xref-other-buffer' is | |
4884c50b SM |
1020 | non-nil. |
1021 | If OTHER-FRAME is non-nil, display the cross-reference in another frame." | |
797aab3c GM |
1022 | (interactive "d") |
1023 | (ada-require-project-file) | |
1024 | (push-mark pos) | |
1025 | (ada-xref-push-pos (buffer-file-name) pos) | |
797aab3c | 1026 | |
4884c50b SM |
1027 | ;; First try the standard algorithm by looking into the .ali file, but if |
1028 | ;; that file was too old or even did not exist, try to look in the whole | |
1029 | ;; object path for a possible location. | |
1030 | (let ((identlist (ada-read-identifier pos))) | |
0262d5e1 | 1031 | (condition-case err |
4884c50b | 1032 | (ada-find-in-ali identlist other-frame) |
0262d5e1 JB |
1033 | ;; File not found: print explicit error message |
1034 | (error-file-not-found | |
1035 | (message (concat (error-message-string err) | |
1036 | (nthcdr 1 err)))) | |
1037 | ||
93cdce20 SM |
1038 | (error |
1039 | (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist)))) | |
1040 | ||
1041 | ;; If the ALI file was up-to-date, then we probably have a predefined | |
1042 | ;; entity, whose references are not given by GNAT | |
1043 | (if (and (file-exists-p ali-file) | |
1044 | (file-newer-than-file-p ali-file (ada-file-of identlist))) | |
417451fe | 1045 | (message "No cross-reference found -- may be a predefined entity.") |
93cdce20 SM |
1046 | |
1047 | ;; Else, look in every ALI file, except if the user doesn't want that | |
1048 | (if ada-xref-search-with-egrep | |
1049 | (ada-find-in-src-path identlist other-frame) | |
ced16f5d | 1050 | (message "Cross-referencing information is not up-to-date; please recompile.") |
93cdce20 | 1051 | ))))))) |
4884c50b | 1052 | |
96a483d9 | 1053 | (defun ada-goto-declaration-other-frame (pos) |
eec3232e GM |
1054 | "Display the declaration of the identifier around POS. |
1055 | The declation is shown in another frame if `ada-xref-other-buffer' is non-nil." | |
797aab3c | 1056 | (interactive "d") |
4884c50b | 1057 | (ada-goto-declaration pos t)) |
797aab3c | 1058 | |
15ea3b67 GM |
1059 | (defun ada-remote (command) |
1060 | "Return the remote version of COMMAND, or COMMAND if remote_machine is nil." | |
1061 | (let ((machine (ada-xref-get-project-field 'remote_machine))) | |
1062 | (if (or (not machine) (string= machine "")) | |
1063 | command | |
1064 | (format "%s %s '(%s)'" | |
1065 | remote-shell-program | |
1066 | machine | |
1067 | command)))) | |
1068 | ||
15ea3b67 | 1069 | (defun ada-get-absolute-dir-list (dir-list root-dir) |
417451fe JB |
1070 | "Return the list of absolute directories found in DIR-LIST. |
1071 | If a directory is a relative directory, ROOT-DIR is prepended." | |
4884c50b | 1072 | (mapcar (lambda (x) (expand-file-name x root-dir)) dir-list)) |
15ea3b67 GM |
1073 | |
1074 | (defun ada-set-environment () | |
ced16f5d RS |
1075 | "Prepare an environment for Ada compilation. |
1076 | This returns a new value to use for `process-environment', | |
1077 | but does not actually put it into use. | |
15ea3b67 GM |
1078 | It modifies the source path and object path with the values found in the |
1079 | project file." | |
1080 | (let ((include (getenv "ADA_INCLUDE_PATH")) | |
1081 | (objects (getenv "ADA_OBJECTS_PATH")) | |
1082 | (build-dir (ada-xref-get-project-field 'build_dir))) | |
1083 | (if include | |
4884c50b | 1084 | (set 'include (concat path-separator include))) |
15ea3b67 | 1085 | (if objects |
4884c50b | 1086 | (set 'objects (concat path-separator objects))) |
15ea3b67 GM |
1087 | (cons |
1088 | (concat "ADA_INCLUDE_PATH=" | |
4884c50b | 1089 | (mapconcat (lambda(x) (expand-file-name x build-dir)) |
15ea3b67 | 1090 | (ada-xref-get-project-field 'src_dir) |
4884c50b SM |
1091 | path-separator) |
1092 | include) | |
15ea3b67 GM |
1093 | (cons |
1094 | (concat "ADA_OBJECTS_PATH=" | |
4884c50b | 1095 | (mapconcat (lambda(x) (expand-file-name x build-dir)) |
15ea3b67 | 1096 | (ada-xref-get-project-field 'obj_dir) |
4884c50b SM |
1097 | path-separator) |
1098 | objects) | |
15ea3b67 GM |
1099 | process-environment)))) |
1100 | ||
1101 | (defun ada-compile-application (&optional arg) | |
ced16f5d | 1102 | "Compile the application, using the command found in the project file. |
15ea3b67 GM |
1103 | If ARG is not nil, ask for user confirmation." |
1104 | (interactive "P") | |
797aab3c | 1105 | (ada-require-project-file) |
15ea3b67 GM |
1106 | (let ((cmd (ada-xref-get-project-field 'make_cmd)) |
1107 | (process-environment (ada-set-environment)) | |
1108 | (compilation-scroll-output t)) | |
1109 | ||
4884c50b | 1110 | (setq compilation-search-path (ada-xref-get-src-dir-field)) |
15ea3b67 GM |
1111 | |
1112 | ;; If no project file was found, ask the user | |
1113 | (unless cmd | |
4884c50b | 1114 | (setq cmd '("") arg t)) |
15ea3b67 | 1115 | |
4884c50b SM |
1116 | ;; Make a single command from the list of commands, including the |
1117 | ;; commands to run it on a remote machine. | |
1118 | (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator))) | |
a1506d29 | 1119 | |
4884c50b SM |
1120 | (if (or ada-xref-confirm-compile arg) |
1121 | (setq cmd (read-from-minibuffer "enter command to compile: " cmd))) | |
1122 | ||
1123 | ;; Insert newlines so as to separate the name of the commands to run | |
ced16f5d | 1124 | ;; and the output of the commands. This doesn't work with cmdproxy.exe, |
4884c50b | 1125 | ;; which gets confused by newline characters. |
c94ca9e0 | 1126 | (if (not (string-match ".exe" shell-file-name)) |
4884c50b | 1127 | (setq cmd (concat cmd "\n\n"))) |
a1506d29 | 1128 | |
4884c50b | 1129 | (compile (ada-quote-cmd cmd)))) |
797aab3c | 1130 | |
15ea3b67 GM |
1131 | (defun ada-compile-current (&optional arg prj-field) |
1132 | "Recompile the current file. | |
1133 | If ARG is not nil, ask for user confirmation of the command. | |
1134 | PRJ-FIELD is the name of the field to use in the project file to get the | |
1135 | command, and should be either comp_cmd (default) or check_cmd." | |
1136 | (interactive "P") | |
797aab3c | 1137 | (ada-require-project-file) |
15ea3b67 GM |
1138 | (let* ((field (if prj-field prj-field 'comp_cmd)) |
1139 | (cmd (ada-xref-get-project-field field)) | |
1140 | (process-environment (ada-set-environment)) | |
1141 | (compilation-scroll-output t)) | |
a1506d29 | 1142 | |
4884c50b | 1143 | (setq compilation-search-path (ada-xref-get-src-dir-field)) |
15ea3b67 | 1144 | |
4884c50b SM |
1145 | (unless cmd |
1146 | (setq cmd '("") arg t)) | |
a1506d29 | 1147 | |
4884c50b SM |
1148 | ;; Make a single command from the list of commands, including the |
1149 | ;; commands to run it on a remote machine. | |
1150 | (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator))) | |
a1506d29 | 1151 | |
15ea3b67 | 1152 | ;; If no project file was found, ask the user |
4884c50b SM |
1153 | (if (or ada-xref-confirm-compile arg) |
1154 | (setq cmd (read-from-minibuffer "enter command to compile: " cmd))) | |
1155 | ||
4884c50b | 1156 | (compile (ada-quote-cmd cmd)))) |
15ea3b67 GM |
1157 | |
1158 | (defun ada-check-current (&optional arg) | |
1159 | "Recompile the current file. | |
1160 | If ARG is not nil, ask for user confirmation of the command." | |
1161 | (interactive "P") | |
1162 | (ada-compile-current arg 'check_cmd)) | |
797aab3c | 1163 | |
15ea3b67 GM |
1164 | (defun ada-run-application (&optional arg) |
1165 | "Run the application. | |
ced16f5d | 1166 | if ARG is not-nil, ask for user confirmation." |
797aab3c GM |
1167 | (interactive) |
1168 | (ada-require-project-file) | |
1169 | ||
15ea3b67 GM |
1170 | (let ((machine (ada-xref-get-project-field 'cross_prefix))) |
1171 | (if (and machine (not (string= machine ""))) | |
1172 | (error "This feature is not supported yet for cross environments"))) | |
797aab3c | 1173 | |
15ea3b67 | 1174 | (let ((command (ada-xref-get-project-field 'run_cmd))) |
797aab3c | 1175 | |
15ea3b67 | 1176 | ;; Guess the command if it wasn't specified |
4884c50b | 1177 | (if (not command) |
417451fe | 1178 | (set 'command (list (file-name-sans-extension (buffer-name))))) |
797aab3c | 1179 | |
4884c50b SM |
1180 | ;; Modify the command to run remotely |
1181 | (setq command (ada-remote (mapconcat 'identity command | |
1182 | ada-command-separator))) | |
a1506d29 | 1183 | |
15ea3b67 GM |
1184 | ;; Ask for the arguments to the command if required |
1185 | (if (or ada-xref-confirm-compile arg) | |
4884c50b SM |
1186 | (setq command (read-from-minibuffer "Enter command to execute: " |
1187 | command))) | |
797aab3c GM |
1188 | |
1189 | ;; Run the command | |
1190 | (save-excursion | |
1191 | (set-buffer (get-buffer-create "*run*")) | |
15ea3b67 | 1192 | (set 'buffer-read-only nil) |
4884c50b | 1193 | |
15ea3b67 | 1194 | (erase-buffer) |
4884c50b SM |
1195 | (start-process "run" (current-buffer) shell-file-name |
1196 | "-c" command) | |
1197 | (comint-mode) | |
1198 | ;; Set these two variables to their default values, since otherwise | |
1199 | ;; the output buffer is scrolled so that only the last output line | |
1200 | ;; is visible at the top of the buffer. | |
1201 | (set (make-local-variable 'scroll-step) 0) | |
1202 | (set (make-local-variable 'scroll-conservatively) 0) | |
797aab3c GM |
1203 | ) |
1204 | (display-buffer "*run*") | |
1205 | ||
1206 | ;; change to buffer *run* for interactive programs | |
1207 | (other-window 1) | |
1208 | (switch-to-buffer "*run*") | |
15ea3b67 | 1209 | )) |
797aab3c | 1210 | |
4884c50b | 1211 | (defun ada-gdb-application (&optional arg executable-name) |
15ea3b67 | 1212 | "Start the debugger on the application. |
417451fe | 1213 | If ARG is non-nil, ask the user to confirm the command. |
4884c50b | 1214 | EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the |
417451fe | 1215 | project file." |
15ea3b67 | 1216 | (interactive "P") |
797aab3c | 1217 | (let ((buffer (current-buffer)) |
4884c50b | 1218 | cmd pre-cmd post-cmd) |
797aab3c | 1219 | (ada-require-project-file) |
4884c50b SM |
1220 | (setq cmd (if executable-name |
1221 | (concat ada-prj-default-debugger " " executable-name) | |
1222 | (ada-xref-get-project-field 'debug_cmd)) | |
1223 | pre-cmd (ada-xref-get-project-field 'debug_pre_cmd) | |
1224 | post-cmd (ada-xref-get-project-field 'debug_post_cmd)) | |
15ea3b67 GM |
1225 | |
1226 | ;; If the command was not given in the project file, start a bare gdb | |
1227 | (if (not cmd) | |
1228 | (set 'cmd (concat ada-prj-default-debugger | |
1229 | " " | |
4884c50b SM |
1230 | (or executable-name |
1231 | (file-name-sans-extension (buffer-file-name)))))) | |
1232 | ||
1233 | ;; For gvd, add an extra switch so that the Emacs window is completly | |
1234 | ;; swallowed inside the Gvd one | |
1235 | (if (and ada-tight-gvd-integration | |
1236 | (string-match "^[^ \t]*gvd" cmd)) | |
1237 | ;; Start a new frame, so that when gvd exists we do not kill Emacs | |
1238 | ;; We make sure that gvd swallows the new frame, not the one the | |
1239 | ;; user has been using until now | |
1240 | ;; The frame is made invisible initially, so that GtkPlug gets a | |
ced16f5d | 1241 | ;; chance to fully manage it. Then it works fine with Enlightenment |
4884c50b SM |
1242 | ;; as well |
1243 | (let ((frame (make-frame '((visibility . nil))))) | |
1244 | (set 'cmd (concat | |
1245 | cmd " --editor-window=" | |
1246 | (cdr (assoc 'outer-window-id (frame-parameters frame))))) | |
1247 | (select-frame frame))) | |
1248 | ||
1249 | ;; Add a -fullname switch | |
1250 | ;; Use the remote machine | |
1251 | (set 'cmd (ada-remote (concat cmd " -fullname "))) | |
1252 | ||
1253 | ;; Ask for confirmation if required | |
15ea3b67 GM |
1254 | (if (or arg ada-xref-confirm-compile) |
1255 | (set 'cmd (read-from-minibuffer "enter command to debug: " cmd))) | |
1256 | ||
96a483d9 | 1257 | (let ((old-comint-exec (symbol-function 'comint-exec))) |
4884c50b SM |
1258 | |
1259 | ;; Do not add -fullname, since we can have a 'rsh' command in front. | |
96a483d9 | 1260 | ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef |
4884c50b SM |
1261 | (fset 'gud-gdb-massage-args (lambda (file args) args)) |
1262 | ||
1263 | (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator)) | |
1264 | (if (not (equal pre-cmd "")) | |
1265 | (setq pre-cmd (concat pre-cmd ada-command-separator))) | |
1266 | ||
1267 | (set 'post-cmd (mapconcat 'identity post-cmd "\n")) | |
1268 | (if post-cmd | |
96a483d9 | 1269 | (set 'post-cmd (concat post-cmd "\n"))) |
4884c50b | 1270 | |
c94ca9e0 | 1271 | |
4884c50b SM |
1272 | ;; Temporarily replaces the definition of `comint-exec' so that we |
1273 | ;; can execute commands before running gdb. | |
96a483d9 | 1274 | ;; FIXME: This is evil and not temporary !!! -stef |
a1506d29 | 1275 | (fset 'comint-exec |
4884c50b SM |
1276 | `(lambda (buffer name command startfile switches) |
1277 | (let (compilation-buffer-name-function) | |
1278 | (save-excursion | |
1279 | (set 'compilation-buffer-name-function | |
1280 | (lambda(x) (buffer-name buffer))) | |
1281 | (compile (ada-quote-cmd | |
1282 | (concat ,pre-cmd | |
1283 | command " " | |
1284 | (mapconcat 'identity switches " ")))))) | |
1285 | )) | |
1286 | ||
1287 | ;; Tight integration should force the tty mode | |
1288 | (if (and (string-match "gvd" (comint-arguments cmd 0 0)) | |
1289 | ada-tight-gvd-integration | |
1290 | (not (string-match "--tty" cmd))) | |
1291 | (setq cmd (concat cmd "--tty"))) | |
a1506d29 | 1292 | |
4884c50b SM |
1293 | (if (and (string-match "jdb" (comint-arguments cmd 0 0)) |
1294 | (boundp 'jdb)) | |
1295 | (funcall (symbol-function 'jdb) cmd) | |
1296 | (gdb cmd)) | |
1297 | ||
c94ca9e0 JB |
1298 | ;; Restore the standard fset command (or for instance C-U M-x shell |
1299 | ;; wouldn't work anymore | |
1300 | ||
1301 | (fset 'comint-exec old-comint-exec) | |
1302 | ||
4884c50b SM |
1303 | ;; Send post-commands to the debugger |
1304 | (process-send-string (get-buffer-process (current-buffer)) post-cmd) | |
1305 | ||
1306 | ;; Move to the end of the debugger buffer, so that it is automatically | |
1307 | ;; scrolled from then on. | |
254746ff | 1308 | (goto-char (point-max)) |
4884c50b SM |
1309 | |
1310 | ;; Display both the source window and the debugger window (the former | |
ced16f5d | 1311 | ;; above the latter). No need to show the debugger window unless it |
4884c50b SM |
1312 | ;; is going to have some relevant information. |
1313 | (if (or (not (string-match "gvd" (comint-arguments cmd 0 0))) | |
1314 | (string-match "--tty" cmd)) | |
1315 | (split-window-vertically)) | |
1316 | (switch-to-buffer buffer) | |
1317 | ))) | |
797aab3c | 1318 | |
15ea3b67 | 1319 | (defun ada-reread-prj-file (&optional filename) |
417451fe | 1320 | "Reread either the current project, or FILENAME if non-nil." |
797aab3c | 1321 | (interactive "P") |
15ea3b67 GM |
1322 | (if filename |
1323 | (ada-parse-prj-file filename) | |
797aab3c | 1324 | (ada-parse-prj-file (ada-prj-find-prj-file))) |
797aab3c | 1325 | |
4884c50b SM |
1326 | ;; Reread the location of the standard runtime library |
1327 | (ada-initialize-runtime-library | |
c94ca9e0 | 1328 | (or (ada-xref-get-project-field 'cross_prefix) "")) |
4884c50b | 1329 | ) |
15ea3b67 | 1330 | |
797aab3c GM |
1331 | ;; ------ Private routines |
1332 | ||
1333 | (defun ada-xref-current (file &optional ali-file-name) | |
eec3232e | 1334 | "Update the cross-references for FILE. |
4884c50b SM |
1335 | This in fact recompiles FILE to create ALI-FILE-NAME. |
1336 | This function returns the name of the file that was recompiled to generate | |
ced16f5d RS |
1337 | the cross-reference information. Note that the ali file can then be deduced by |
1338 | replacing the file extension with `.ali'." | |
797aab3c GM |
1339 | ;; kill old buffer |
1340 | (if (and ali-file-name | |
417451fe | 1341 | (get-file-buffer ali-file-name)) |
797aab3c | 1342 | (kill-buffer (get-file-buffer ali-file-name))) |
a1506d29 | 1343 | |
4884c50b SM |
1344 | (let* ((name (ada-convert-file-name file)) |
1345 | (body-name (or (ada-get-body-name name) name))) | |
15ea3b67 | 1346 | |
ced16f5d | 1347 | ;; Always recompile the body when we can. We thus temporarily switch to a |
4884c50b SM |
1348 | ;; buffer than contains the body of the unit |
1349 | (save-excursion | |
1350 | (let ((body-visible (find-buffer-visiting body-name)) | |
1351 | process) | |
1352 | (if body-visible | |
1353 | (set-buffer body-visible) | |
1354 | (find-file body-name)) | |
1355 | ||
ced16f5d | 1356 | ;; Execute the compilation. Note that we must wait for the end of the |
4884c50b SM |
1357 | ;; process, or the ALI file would still not be available. |
1358 | ;; Unfortunately, the underlying `compile' command that we use is | |
1359 | ;; asynchronous. | |
1360 | (ada-compile-current) | |
1361 | (setq process (get-buffer-process "*compilation*")) | |
1362 | ||
1363 | (while (and process | |
1364 | (not (equal (process-status process) 'exit))) | |
1365 | (sit-for 1)) | |
1366 | ||
1367 | ;; remove the buffer for the body if it wasn't there before | |
1368 | (unless body-visible | |
1369 | (kill-buffer (find-buffer-visiting body-name))) | |
1370 | )) | |
1371 | body-name)) | |
15ea3b67 GM |
1372 | |
1373 | (defun ada-find-file-in-dir (file dir-list) | |
1374 | "Search for FILE in DIR-LIST." | |
1375 | (let (found) | |
1376 | (while (and (not found) dir-list) | |
1377 | (set 'found (concat (file-name-as-directory (car dir-list)) | |
1378 | (file-name-nondirectory file))) | |
a1506d29 | 1379 | |
15ea3b67 GM |
1380 | (unless (file-exists-p found) |
1381 | (set 'found nil)) | |
1382 | (set 'dir-list (cdr dir-list))) | |
1383 | found)) | |
797aab3c GM |
1384 | |
1385 | (defun ada-find-ali-file-in-dir (file) | |
417451fe | 1386 | "Find the ali file FILE, searching obj_dir for the current project. |
15ea3b67 GM |
1387 | Adds build_dir in front of the search path to conform to gnatmake's behavior, |
1388 | and the standard runtime location at the end." | |
4884c50b | 1389 | (ada-find-file-in-dir file (ada-xref-get-obj-dir-field))) |
15ea3b67 GM |
1390 | |
1391 | (defun ada-find-src-file-in-dir (file) | |
417451fe JB |
1392 | "Find the source file FILE, searching src_dir for the current project. |
1393 | Adds the standard runtime location at the end of the search path to conform | |
1394 | to gnatmake's behavior." | |
4884c50b | 1395 | (ada-find-file-in-dir file (ada-xref-get-src-dir-field))) |
797aab3c GM |
1396 | |
1397 | (defun ada-get-ali-file-name (file) | |
eec3232e GM |
1398 | "Create the ali file name for the ada-file FILE. |
1399 | The file is searched for in every directory shown in the obj_dir lines of | |
1400 | the project file." | |
797aab3c GM |
1401 | |
1402 | ;; This function has to handle the special case of non-standard | |
1403 | ;; file names (i.e. not .adb or .ads) | |
1404 | ;; The trick is the following: | |
1405 | ;; 1- replace the extension of the current file with .ali, | |
1406 | ;; and look for this file | |
1407 | ;; 2- If this file is found: | |
1408 | ;; grep the "^U" lines, and make sure we are not reading the | |
ced16f5d | 1409 | ;; .ali file for a spec file. If we are, go to step 3. |
797aab3c GM |
1410 | ;; 3- If the file is not found or step 2 failed: |
1411 | ;; find the name of the "other file", ie the body, and look | |
1412 | ;; for its associated .ali file by subtituing the extension | |
4884c50b SM |
1413 | ;; |
1414 | ;; We must also handle the case of separate packages and subprograms: | |
1415 | ;; 4- If no ali file was found, we try to modify the file name by removing | |
1416 | ;; everything after the last '-' or '.' character, so as to get the | |
ced16f5d | 1417 | ;; ali file for the parent unit. If we found an ali file, we check that |
4884c50b | 1418 | ;; it indeed contains the definition for the separate entity by checking |
ced16f5d | 1419 | ;; the 'D' lines. This is done repeatedly, in case the direct parent is |
4884c50b | 1420 | ;; also a separate. |
797aab3c GM |
1421 | |
1422 | (save-excursion | |
1423 | (set-buffer (get-file-buffer file)) | |
1424 | (let ((short-ali-file-name | |
417451fe JB |
1425 | (concat (file-name-sans-extension (file-name-nondirectory file)) |
1426 | ".ali")) | |
1427 | ali-file-name | |
4884c50b SM |
1428 | is-spec) |
1429 | ||
1430 | ;; If we have a non-standard file name, and this is a spec, we first | |
1431 | ;; look for the .ali file of the body, since this is the one that | |
ced16f5d | 1432 | ;; contains the most complete information. If not found, we will do what |
4884c50b SM |
1433 | ;; we can with the .ali file for the spec... |
1434 | ||
1435 | (if (not (string= (file-name-extension file) "ads")) | |
1436 | (let ((specs ada-spec-suffixes)) | |
1437 | (while specs | |
1438 | (if (string-match (concat (regexp-quote (car specs)) "$") | |
1439 | file) | |
1440 | (set 'is-spec t)) | |
1441 | (set 'specs (cdr specs))))) | |
1442 | ||
1443 | (if is-spec | |
1444 | (set 'ali-file-name | |
1445 | (ada-find-ali-file-in-dir | |
1446 | (concat (file-name-sans-extension | |
1447 | (file-name-nondirectory | |
1448 | (ada-other-file-name))) | |
1449 | ".ali")))) | |
a1506d29 | 1450 | |
4884c50b SM |
1451 | |
1452 | (setq ali-file-name | |
1453 | (or ali-file-name | |
a1506d29 | 1454 | |
4884c50b SM |
1455 | ;; Else we take the .ali file associated with the unit |
1456 | (ada-find-ali-file-in-dir short-ali-file-name) | |
a1506d29 | 1457 | |
4884c50b SM |
1458 | |
1459 | ;; else we did not find the .ali file Second chance: in case | |
1460 | ;; the files do not have standard names (such as for instance | |
1461 | ;; file_s.ada and file_b.ada), try to go to the other file | |
1462 | ;; and look for its ali file | |
1463 | (ada-find-ali-file-in-dir | |
1464 | (concat (file-name-sans-extension | |
1465 | (file-name-nondirectory (ada-other-file-name))) | |
1466 | ".ali")) | |
1467 | ||
a1506d29 | 1468 | |
4884c50b SM |
1469 | ;; If we still don't have an ali file, try to get the one |
1470 | ;; from the parent unit, in case we have a separate entity. | |
1471 | (let ((parent-name (file-name-sans-extension | |
1472 | (file-name-nondirectory file)))) | |
a1506d29 | 1473 | |
4884c50b SM |
1474 | (while (and (not ali-file-name) |
1475 | (string-match "^\\(.*\\)[.-][^.-]*" parent-name)) | |
a1506d29 | 1476 | |
4884c50b SM |
1477 | (set 'parent-name (match-string 1 parent-name)) |
1478 | (set 'ali-file-name (ada-find-ali-file-in-dir | |
1479 | (concat parent-name ".ali"))) | |
1480 | ) | |
1481 | ali-file-name))) | |
a1506d29 | 1482 | |
4884c50b SM |
1483 | ;; If still not found, try to recompile the file |
1484 | (if (not ali-file-name) | |
ced16f5d RS |
1485 | ;; Recompile only if the user asked for this, and search the ali |
1486 | ;; filename again. We avoid a possible infinite recursion by | |
4884c50b | 1487 | ;; temporarily disabling the automatic compilation. |
a1506d29 | 1488 | |
4884c50b SM |
1489 | (if ada-xref-create-ali |
1490 | (setq ali-file-name | |
1491 | (concat (file-name-sans-extension (ada-xref-current file)) | |
1492 | ".ali")) | |
797aab3c | 1493 | |
ced16f5d | 1494 | (error "`.ali' file not found; recompile your source file")) |
a1506d29 JB |
1495 | |
1496 | ||
4884c50b SM |
1497 | ;; same if the .ali file is too old and we must recompile it |
1498 | (if (and (file-newer-than-file-p file ali-file-name) | |
1499 | ada-xref-create-ali) | |
1500 | (ada-xref-current file ali-file-name))) | |
797aab3c | 1501 | |
4884c50b | 1502 | ;; Always return the correct absolute file name |
797aab3c | 1503 | (expand-file-name ali-file-name)) |
4884c50b | 1504 | )) |
797aab3c GM |
1505 | |
1506 | (defun ada-get-ada-file-name (file original-file) | |
eec3232e | 1507 | "Create the complete file name (+directory) for FILE. |
ced16f5d | 1508 | The original file (where the user was) is ORIGINAL-FILE. Search in project |
eec3232e | 1509 | file for possible paths." |
797aab3c GM |
1510 | |
1511 | (save-excursion | |
15ea3b67 GM |
1512 | |
1513 | ;; If the buffer for original-file, use it to get the values from the | |
1514 | ;; project file, otherwise load the file and its project file | |
1515 | (let ((buffer (get-file-buffer original-file))) | |
1516 | (if buffer | |
1517 | (set-buffer buffer) | |
1518 | (find-file original-file) | |
1519 | (ada-require-project-file))) | |
a1506d29 | 1520 | |
797aab3c GM |
1521 | ;; we choose the first possible completion and we |
1522 | ;; return the absolute file name | |
15ea3b67 | 1523 | (let ((filename (ada-find-src-file-in-dir file))) |
797aab3c | 1524 | (if filename |
417451fe | 1525 | (expand-file-name filename) |
0262d5e1 | 1526 | (signal 'error-file-not-found (file-name-nondirectory file))) |
797aab3c GM |
1527 | ))) |
1528 | ||
1529 | (defun ada-find-file-number-in-ali (file) | |
417451fe | 1530 | "Return the file number for FILE in the associated ali file." |
797aab3c GM |
1531 | (set-buffer (ada-get-ali-buffer file)) |
1532 | (goto-char (point-min)) | |
1533 | ||
1534 | (let ((begin (re-search-forward "^D"))) | |
1535 | (beginning-of-line) | |
1536 | (re-search-forward (concat "^D " (file-name-nondirectory file))) | |
1537 | (count-lines begin (point)))) | |
1538 | ||
1539 | (defun ada-read-identifier (pos) | |
417451fe | 1540 | "Return the identlist around POS and switch to the .ali buffer. |
4884c50b SM |
1541 | The returned list represents the entity, and can be manipulated through the |
1542 | macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..." | |
797aab3c GM |
1543 | |
1544 | ;; If at end of buffer (e.g the buffer is empty), error | |
1545 | (if (>= (point) (point-max)) | |
1546 | (error "No identifier on point")) | |
a1506d29 | 1547 | |
797aab3c GM |
1548 | ;; goto first character of the identifier/operator (skip backward < and > |
1549 | ;; since they are part of multiple character operators | |
1550 | (goto-char pos) | |
1551 | (skip-chars-backward "a-zA-Z0-9_<>") | |
1552 | ||
1553 | ;; check if it really is an identifier | |
1554 | (if (ada-in-comment-p) | |
1555 | (error "Inside comment")) | |
1556 | ||
1557 | (let (identifier identlist) | |
1558 | ;; Just in front of a string => we could have an operator declaration, | |
1559 | ;; as in "+", "-", .. | |
1560 | (if (= (char-after) ?\") | |
417451fe | 1561 | (forward-char 1)) |
797aab3c GM |
1562 | |
1563 | ;; if looking at an operator | |
15ea3b67 GM |
1564 | ;; This is only true if: |
1565 | ;; - the symbol is +, -, ... | |
1566 | ;; - the symbol is made of letters, and not followed by _ or a letter | |
1567 | (if (and (looking-at ada-operator-re) | |
1568 | (or (not (= (char-syntax (char-after)) ?w)) | |
1569 | (not (or (= (char-syntax (char-after (match-end 0))) ?w) | |
1570 | (= (char-after (match-end 0)) ?_))))) | |
417451fe JB |
1571 | (progn |
1572 | (if (and (= (char-before) ?\") | |
1573 | (= (char-after (+ (length (match-string 0)) (point))) ?\")) | |
1574 | (forward-char -1)) | |
1575 | (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\"")))) | |
797aab3c GM |
1576 | |
1577 | (if (ada-in-string-p) | |
417451fe | 1578 | (error "Inside string or character constant")) |
797aab3c | 1579 | (if (looking-at (concat ada-keywords "[^a-zA-Z_]")) |
417451fe | 1580 | (error "No cross-reference available for reserved keyword")) |
797aab3c | 1581 | (if (looking-at "[a-zA-Z0-9_]+") |
417451fe JB |
1582 | (set 'identifier (match-string 0)) |
1583 | (error "No identifier around"))) | |
a1506d29 | 1584 | |
797aab3c GM |
1585 | ;; Build the identlist |
1586 | (set 'identlist (ada-make-identlist)) | |
1587 | (ada-set-name identlist (downcase identifier)) | |
1588 | (ada-set-line identlist | |
93cdce20 | 1589 | (number-to-string (count-lines 1 (point)))) |
797aab3c GM |
1590 | (ada-set-column identlist |
1591 | (number-to-string (1+ (current-column)))) | |
1592 | (ada-set-file identlist (buffer-file-name)) | |
1593 | identlist | |
1594 | )) | |
1595 | ||
1596 | (defun ada-get-all-references (identlist) | |
417451fe JB |
1597 | "Complete IDENTLIST with definition file and places where it is referenced. |
1598 | Information is extracted from the ali file." | |
a1506d29 | 1599 | |
797aab3c GM |
1600 | (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist))) |
1601 | declaration-found) | |
1602 | (set-buffer ali-buffer) | |
1603 | (goto-char (point-min)) | |
1604 | (ada-set-on-declaration identlist nil) | |
1605 | ||
1606 | ;; First attempt: we might already be on the declaration of the identifier | |
1607 | ;; We want to look for the declaration only in a definite interval (after | |
1608 | ;; the "^X ..." line for the current file, and before the next "^X" line | |
a1506d29 | 1609 | |
797aab3c GM |
1610 | (if (re-search-forward |
1611 | (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist))) | |
1612 | nil t) | |
417451fe JB |
1613 | (let ((bound (save-excursion (re-search-forward "^X " nil t)))) |
1614 | (set 'declaration-found | |
797aab3c GM |
1615 | (re-search-forward |
1616 | (concat "^" (ada-line-of identlist) | |
1617 | "." (ada-column-of identlist) | |
15ea3b67 | 1618 | "[ *]" (ada-name-of identlist) |
0262d5e1 | 1619 | "[{\[\(<= ]?\\(.*\\)$") bound t)) |
797aab3c GM |
1620 | (if declaration-found |
1621 | (ada-set-on-declaration identlist t)) | |
1622 | )) | |
1623 | ||
1624 | ;; If declaration is still nil, then we were not on a declaration, and | |
1625 | ;; have to fall back on other algorithms | |
1626 | ||
1627 | (unless declaration-found | |
a1506d29 | 1628 | |
797aab3c GM |
1629 | ;; Since we alread know the number of the file, search for a direct |
1630 | ;; reference to it | |
1631 | (goto-char (point-min)) | |
1632 | (set 'declaration-found t) | |
1633 | (ada-set-ali-index | |
1634 | identlist | |
1635 | (number-to-string (ada-find-file-number-in-ali | |
1636 | (ada-file-of identlist)))) | |
1637 | (unless (re-search-forward (concat (ada-ali-index-of identlist) | |
93cdce20 | 1638 | "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*" |
797aab3c | 1639 | (ada-line-of identlist) |
c94ca9e0 | 1640 | "[^etpzkd<>=^]" |
93cdce20 | 1641 | (ada-column-of identlist) "\\>") |
797aab3c GM |
1642 | nil t) |
1643 | ||
417451fe JB |
1644 | ;; if we did not find it, it may be because the first reference |
1645 | ;; is not required to have a 'unit_number|' item included. | |
1646 | ;; Or maybe we are already on the declaration... | |
1647 | (unless (re-search-forward | |
4884c50b | 1648 | (concat |
93cdce20 SM |
1649 | "^[0-9]+.[0-9]+[ *]" |
1650 | (ada-name-of identlist) | |
0262d5e1 | 1651 | "[ <{=\(\[]\\(.\\|\n\\.\\)*\\<" |
4884c50b SM |
1652 | (ada-line-of identlist) |
1653 | "[^0-9]" | |
93cdce20 | 1654 | (ada-column-of identlist) "\\>") |
4884c50b | 1655 | nil t) |
a1506d29 | 1656 | |
797aab3c GM |
1657 | ;; If still not found, then either the declaration is unknown |
1658 | ;; or the source file has been modified since the ali file was | |
1659 | ;; created | |
1660 | (set 'declaration-found nil) | |
417451fe | 1661 | ) |
797aab3c GM |
1662 | ) |
1663 | ||
1664 | ;; Last check to be completly sure we have found the correct line (the | |
1665 | ;; ali might not be up to date for instance) | |
1666 | (if declaration-found | |
1667 | (progn | |
1668 | (beginning-of-line) | |
1669 | ;; while we have a continuation line, go up one line | |
1670 | (while (looking-at "^\\.") | |
0262d5e1 JB |
1671 | (previous-line 1) |
1672 | (beginning-of-line)) | |
797aab3c | 1673 | (unless (looking-at (concat "[0-9]+.[0-9]+[ *]" |
0262d5e1 | 1674 | (ada-name-of identlist) "[ <{=\(\[]")) |
797aab3c GM |
1675 | (set 'declaration-found nil)))) |
1676 | ||
1677 | ;; Still no success ! The ali file must be too old, and we need to | |
ced16f5d | 1678 | ;; use a basic algorithm based on guesses. Note that this only happens |
797aab3c GM |
1679 | ;; if the user does not want us to automatically recompile files |
1680 | ;; automatically | |
1681 | (unless declaration-found | |
15ea3b67 GM |
1682 | (if (ada-xref-find-in-modified-ali identlist) |
1683 | (set 'declaration-found t) | |
ced16f5d | 1684 | ;; No more idea to find the declaration. Give up |
797aab3c GM |
1685 | (progn |
1686 | (kill-buffer ali-buffer) | |
1687 | (error (concat "No declaration of " (ada-name-of identlist) | |
1688 | " found.")) | |
1689 | ))) | |
1690 | ) | |
1691 | ||
a1506d29 | 1692 | |
797aab3c GM |
1693 | ;; Now that we have found a suitable line in the .ali file, get the |
1694 | ;; information available | |
1695 | (beginning-of-line) | |
1696 | (if declaration-found | |
417451fe | 1697 | (let ((current-line (buffer-substring |
797aab3c | 1698 | (point) (save-excursion (end-of-line) (point))))) |
417451fe JB |
1699 | (save-excursion |
1700 | (next-line 1) | |
1701 | (beginning-of-line) | |
1702 | (while (looking-at "^\\.\\(.*\\)") | |
1703 | (set 'current-line (concat current-line (match-string 1))) | |
1704 | (next-line 1)) | |
1705 | ) | |
797aab3c GM |
1706 | |
1707 | (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t) | |
4884c50b SM |
1708 | |
1709 | ;; If we can find the file | |
1710 | (condition-case err | |
1711 | (ada-set-declare-file | |
1712 | identlist | |
1713 | (ada-get-ada-file-name (match-string 1) | |
1714 | (ada-file-of identlist))) | |
a1506d29 | 1715 | |
4884c50b | 1716 | ;; Else clean up the ali file |
0262d5e1 JB |
1717 | (error-file-not-found |
1718 | (signal (car err) (cdr err))) | |
4884c50b SM |
1719 | (error |
1720 | (kill-buffer ali-buffer) | |
1721 | (error (error-message-string err))) | |
1722 | )) | |
a1506d29 | 1723 | |
797aab3c GM |
1724 | (ada-set-references identlist current-line) |
1725 | )) | |
1726 | )) | |
1727 | ||
1728 | (defun ada-xref-find-in-modified-ali (identlist) | |
1729 | "Find the matching position for IDENTLIST in the current ali buffer. | |
1730 | This function is only called when the file was not up-to-date, so we need | |
1731 | to make some guesses. | |
eec3232e | 1732 | This function is disabled for operators, and only works for identifiers." |
797aab3c GM |
1733 | |
1734 | (unless (= (string-to-char (ada-name-of identlist)) ?\") | |
1735 | (progn | |
417451fe | 1736 | (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... )) |
797aab3c GM |
1737 | (my-regexp (concat "[ *]" |
1738 | (regexp-quote (ada-name-of identlist)) " ")) | |
1739 | (line-ada "--") | |
1740 | (col-ada "--") | |
1741 | (line-ali 0) | |
1742 | (len 0) | |
15ea3b67 GM |
1743 | (choice 0) |
1744 | (ali-buffer (current-buffer))) | |
797aab3c | 1745 | |
417451fe JB |
1746 | (goto-char (point-max)) |
1747 | (while (re-search-backward my-regexp nil t) | |
1748 | (save-excursion | |
1749 | (set 'line-ali (count-lines 1 (point))) | |
1750 | (beginning-of-line) | |
1751 | ;; have a look at the line and column numbers | |
1752 | (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]") | |
1753 | (progn | |
1754 | (setq line-ada (match-string 1)) | |
1755 | (setq col-ada (match-string 2))) | |
1756 | (setq line-ada "--") | |
1757 | (setq col-ada "--") | |
1758 | ) | |
1759 | ;; construct a list with the file names and the positions within | |
1760 | (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t) | |
797aab3c GM |
1761 | (add-to-list |
1762 | 'declist (list line-ali (match-string 1) line-ada col-ada)) | |
417451fe JB |
1763 | ) |
1764 | ) | |
1765 | ) | |
1766 | ||
1767 | ;; how many possible declarations have we found ? | |
1768 | (setq len (length declist)) | |
1769 | (cond | |
1770 | ;; none => error | |
1771 | ((= len 0) | |
1772 | (kill-buffer (current-buffer)) | |
1773 | (error (concat "No declaration of " | |
1774 | (ada-name-of identlist) | |
1775 | " recorded in .ali file"))) | |
1776 | ||
1777 | ;; one => should be the right one | |
1778 | ((= len 1) | |
1779 | (goto-line (caar declist))) | |
1780 | ||
1781 | ;; more than one => display choice list | |
1782 | (t | |
4884c50b SM |
1783 | (save-window-excursion |
1784 | (with-output-to-temp-buffer "*choice list*" | |
a1506d29 | 1785 | |
4884c50b SM |
1786 | (princ "Identifier is overloaded and Xref information is not up to date.\n") |
1787 | (princ "Possible declarations are:\n\n") | |
1788 | (princ " no. in file at line col\n") | |
1789 | (princ " --- --------------------- ---- ----\n") | |
1790 | (let ((counter 0)) | |
1791 | (while (< counter len) | |
1792 | (princ (format " %2d) %-21s %4s %4s\n" | |
417451fe | 1793 | (1+ counter) |
797aab3c | 1794 | (ada-get-ada-file-name |
4884c50b | 1795 | (nth 1 (nth counter declist)) |
797aab3c | 1796 | (ada-file-of identlist)) |
417451fe JB |
1797 | (nth 2 (nth counter declist)) |
1798 | (nth 3 (nth counter declist)) | |
1799 | )) | |
4884c50b SM |
1800 | (setq counter (1+ counter)) |
1801 | ) ; end of while | |
1802 | ) ; end of let | |
1803 | ) ; end of with-output-to ... | |
1804 | (setq choice nil) | |
1805 | (while (or | |
1806 | (not choice) | |
1807 | (not (integerp choice)) | |
1808 | (< choice 1) | |
1809 | (> choice len)) | |
1810 | (setq choice | |
027a4b6b | 1811 | (string-to-number |
4884c50b SM |
1812 | (read-from-minibuffer "Enter No. of your choice: ")))) |
1813 | ) | |
15ea3b67 | 1814 | (set-buffer ali-buffer) |
417451fe JB |
1815 | (goto-line (car (nth (1- choice) declist))) |
1816 | )))))) | |
797aab3c GM |
1817 | |
1818 | ||
1819 | (defun ada-find-in-ali (identlist &optional other-frame) | |
eec3232e | 1820 | "Look in the .ali file for the definition of the identifier in IDENTLIST. |
09aba29c | 1821 | If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil, |
eec3232e | 1822 | opens a new window to show the declaration." |
797aab3c GM |
1823 | |
1824 | (ada-get-all-references identlist) | |
1825 | (let ((ali-line (ada-references-of identlist)) | |
4884c50b SM |
1826 | (locations nil) |
1827 | (start 0) | |
797aab3c | 1828 | file line col) |
4884c50b SM |
1829 | |
1830 | ;; Note: in some cases, an entity can have multiple references to the | |
1831 | ;; bodies (this is for instance the case for a separate subprogram, that | |
1832 | ;; has a reference both to the stub and to the real body). | |
1833 | ;; In that case, we simply go to each one in turn. | |
1834 | ||
1835 | ;; Get all the possible locations | |
0262d5e1 | 1836 | (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line) |
4884c50b SM |
1837 | (set 'locations (list (list (match-string 1 ali-line) ;; line |
1838 | (match-string 2 ali-line) ;; column | |
1839 | (ada-declare-file-of identlist)))) | |
c94ca9e0 JB |
1840 | (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)" |
1841 | ali-line start) | |
4884c50b | 1842 | (setq line (match-string 1 ali-line) |
c94ca9e0 JB |
1843 | col (match-string 3 ali-line) |
1844 | start (match-end 3)) | |
4884c50b SM |
1845 | |
1846 | ;; it there was a file number in the same line | |
0262d5e1 JB |
1847 | ;; Make sure we correctly handle the case where the first file reference |
1848 | ;; on the line is the type reference. | |
1849 | ;; 1U2 T(2|2r3) 34r23 | |
1850 | (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?" | |
4884c50b SM |
1851 | (match-string 0 ali-line)) |
1852 | ali-line) | |
1853 | (let ((file-number (match-string 1 ali-line))) | |
1854 | (goto-char (point-min)) | |
1855 | (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t | |
1856 | (string-to-number file-number)) | |
1857 | (set 'file (match-string 1)) | |
1858 | ) | |
1859 | ;; Else get the nearest file | |
1860 | (set 'file (ada-declare-file-of identlist))) | |
a1506d29 | 1861 | |
4884c50b SM |
1862 | (set 'locations (append locations (list (list line col file))))) |
1863 | ||
1864 | ;; Add the specs at the end again, so that from the last body we go to | |
1865 | ;; the specs | |
1866 | (set 'locations (append locations (list (car locations)))) | |
1867 | ||
1868 | ;; Find the new location we want to go to. | |
1869 | ;; If we are on none of the locations listed, we simply go to the specs. | |
1870 | ||
1871 | (setq line (caar locations) | |
1872 | col (nth 1 (car locations)) | |
1873 | file (nth 2 (car locations))) | |
a1506d29 | 1874 | |
4884c50b SM |
1875 | (while locations |
1876 | (if (and (string= (caar locations) (ada-line-of identlist)) | |
1877 | (string= (nth 1 (car locations)) (ada-column-of identlist)) | |
1878 | (string= (file-name-nondirectory (nth 2 (car locations))) | |
1879 | (file-name-nondirectory (ada-file-of identlist)))) | |
1880 | (setq locations (cadr locations) | |
1881 | line (car locations) | |
1882 | col (nth 1 locations) | |
1883 | file (nth 2 locations) | |
1884 | locations nil) | |
1885 | (set 'locations (cdr locations)))) | |
1886 | ||
1887 | ;; Find the file in the source path | |
1888 | (set 'file (ada-get-ada-file-name file (ada-file-of identlist))) | |
1889 | ||
1890 | ;; Kill the .ali buffer | |
1891 | (kill-buffer (current-buffer)) | |
797aab3c GM |
1892 | |
1893 | ;; Now go to the buffer | |
4884c50b SM |
1894 | (ada-xref-change-buffer file |
1895 | (string-to-number line) | |
1896 | (1- (string-to-number col)) | |
1897 | identlist | |
1898 | other-frame) | |
797aab3c GM |
1899 | )) |
1900 | ||
4884c50b SM |
1901 | (defun ada-find-in-src-path (identlist &optional other-frame) |
1902 | "More general function for cross-references. | |
1903 | This function should be used when the standard algorithm that parses the | |
1904 | .ali file has failed, either because that file was too old or even did not | |
1905 | exist. | |
1906 | This function attempts to find the possible declarations for the identifier | |
1907 | anywhere in the object path. | |
1908 | This command requires the external `egrep' program to be available. | |
1909 | ||
1910 | This works well when one is using an external librarie and wants | |
1911 | to find the declaration and documentation of the subprograms one is | |
1912 | is using." | |
417451fe | 1913 | ;; FIXME: what does this function do? |
4884c50b SM |
1914 | (let (list |
1915 | (dirs (ada-xref-get-obj-dir-field)) | |
1916 | (regexp (concat "[ *]" (ada-name-of identlist))) | |
1917 | line column | |
1918 | choice | |
1919 | file) | |
a1506d29 | 1920 | |
4884c50b | 1921 | (save-excursion |
a1506d29 | 1922 | |
ced16f5d | 1923 | ;; Do the grep in all the directories. We do multiple shell |
4884c50b SM |
1924 | ;; commands instead of one in case there is no .ali file in one |
1925 | ;; of the directory and the shell stops because of that. | |
a1506d29 | 1926 | |
4884c50b SM |
1927 | (set-buffer (get-buffer-create "*grep*")) |
1928 | (while dirs | |
1929 | (insert (shell-command-to-string | |
81fe5f4b KS |
1930 | (concat |
1931 | "grep -E -i -h " | |
1932 | (shell-quote-argument (concat "^X|" regexp "( |$)")) | |
1933 | " " | |
1934 | (shell-quote-argument (file-name-as-directory (car dirs))) | |
1935 | "*.ali"))) | |
4884c50b | 1936 | (set 'dirs (cdr dirs))) |
a1506d29 | 1937 | |
4884c50b SM |
1938 | ;; Now parse the output |
1939 | (set 'case-fold-search t) | |
1940 | (goto-char (point-min)) | |
1941 | (while (re-search-forward regexp nil t) | |
1942 | (save-excursion | |
1943 | (beginning-of-line) | |
1944 | (if (not (= (char-after) ?X)) | |
1945 | (progn | |
1946 | (looking-at "\\([0-9]+\\).\\([0-9]+\\)") | |
1947 | (setq line (match-string 1) | |
1948 | column (match-string 2)) | |
1949 | (re-search-backward "^X [0-9]+ \\(.*\\)$") | |
1950 | (set 'file (list (match-string 1) line column)) | |
a1506d29 | 1951 | |
4884c50b SM |
1952 | ;; There could be duplicate choices, because of the structure |
1953 | ;; of the .ali files | |
1954 | (unless (member file list) | |
1955 | (set 'list (append list (list file)))))))) | |
a1506d29 | 1956 | |
4884c50b SM |
1957 | ;; Current buffer is still "*grep*" |
1958 | (kill-buffer "*grep*") | |
1959 | ) | |
a1506d29 | 1960 | |
4884c50b SM |
1961 | ;; Now display the list of possible matches |
1962 | (cond | |
a1506d29 | 1963 | |
4884c50b SM |
1964 | ;; No choice found => Error |
1965 | ((null list) | |
1966 | (error "No cross-reference found, please recompile your file")) | |
a1506d29 | 1967 | |
4884c50b SM |
1968 | ;; Only one choice => Do the cross-reference |
1969 | ((= (length list) 1) | |
1970 | (set 'file (ada-find-src-file-in-dir (caar list))) | |
1971 | (if file | |
1972 | (ada-xref-change-buffer file | |
1973 | (string-to-number (nth 1 (car list))) | |
1974 | (string-to-number (nth 2 (car list))) | |
1975 | identlist | |
1976 | other-frame) | |
1977 | (error (concat (caar list) " not found in src_dir"))) | |
1978 | (message "This is only a (good) guess at the cross-reference.") | |
1979 | ) | |
a1506d29 | 1980 | |
4884c50b SM |
1981 | ;; Else, ask the user |
1982 | (t | |
1983 | (save-window-excursion | |
1984 | (with-output-to-temp-buffer "*choice list*" | |
a1506d29 | 1985 | |
4884c50b SM |
1986 | (princ "Identifier is overloaded and Xref information is not up to date.\n") |
1987 | (princ "Possible declarations are:\n\n") | |
1988 | (princ " no. in file at line col\n") | |
1989 | (princ " --- --------------------- ---- ----\n") | |
1990 | (let ((counter 0)) | |
1991 | (while (< counter (length list)) | |
1992 | (princ (format " %2d) %-21s %4s %4s\n" | |
1993 | (1+ counter) | |
1994 | (nth 0 (nth counter list)) | |
1995 | (nth 1 (nth counter list)) | |
1996 | (nth 2 (nth counter list)) | |
1997 | )) | |
1998 | (setq counter (1+ counter)) | |
1999 | ))) | |
2000 | (setq choice nil) | |
2001 | (while (or (not choice) | |
2002 | (not (integerp choice)) | |
2003 | (< choice 1) | |
2004 | (> choice (length list))) | |
2005 | (setq choice | |
027a4b6b | 2006 | (string-to-number |
4884c50b SM |
2007 | (read-from-minibuffer "Enter No. of your choice: ")))) |
2008 | ) | |
2009 | (set 'choice (1- choice)) | |
2010 | (kill-buffer "*choice list*") | |
2011 | ||
2012 | (set 'file (ada-find-src-file-in-dir (car (nth choice list)))) | |
2013 | (if file | |
2014 | (ada-xref-change-buffer file | |
2015 | (string-to-number (nth 1 (nth choice list))) | |
2016 | (string-to-number (nth 2 (nth choice list))) | |
2017 | identlist | |
2018 | other-frame) | |
0262d5e1 | 2019 | (signal 'error-file-not-found (car (nth choice list)))) |
4884c50b SM |
2020 | (message "This is only a (good) guess at the cross-reference.") |
2021 | )))) | |
2022 | ||
797aab3c GM |
2023 | (defun ada-xref-change-buffer |
2024 | (file line column identlist &optional other-frame) | |
4884c50b | 2025 | "Select and display FILE, at LINE and COLUMN. |
797aab3c | 2026 | If we do not end on the same identifier as IDENTLIST, find the closest |
ced16f5d | 2027 | match. Kills the .ali buffer at the end. |
eec3232e | 2028 | If OTHER-FRAME is non-nil, creates a new frame to show the file." |
797aab3c | 2029 | |
4884c50b | 2030 | (let (declaration-buffer) |
797aab3c GM |
2031 | |
2032 | ;; Select and display the destination buffer | |
2033 | (if ada-xref-other-buffer | |
417451fe JB |
2034 | (if other-frame |
2035 | (find-file-other-frame file) | |
2036 | (set 'declaration-buffer (find-file-noselect file)) | |
2037 | (set-buffer declaration-buffer) | |
2038 | (switch-to-buffer-other-window declaration-buffer) | |
2039 | ) | |
797aab3c GM |
2040 | (find-file file) |
2041 | ) | |
2042 | ||
797aab3c GM |
2043 | ;; move the cursor to the correct position |
2044 | (push-mark) | |
2045 | (goto-line line) | |
2046 | (move-to-column column) | |
2047 | ||
2048 | ;; If we are not on the identifier, the ali file was not up-to-date. | |
2049 | ;; Try to find the nearest position where the identifier is found, | |
2050 | ;; this is probably the right one. | |
2051 | (unless (looking-at (ada-name-of identlist)) | |
2052 | (ada-xref-search-nearest (ada-name-of identlist))) | |
4884c50b | 2053 | )) |
797aab3c GM |
2054 | |
2055 | ||
2056 | (defun ada-xref-search-nearest (name) | |
417451fe JB |
2057 | "Search for NAME nearest to the position recorded in the Xref file. |
2058 | Return the position of the declaration in the buffer, or nil if not found." | |
797aab3c | 2059 | (let ((orgpos (point)) |
417451fe JB |
2060 | (newpos nil) |
2061 | (diff nil)) | |
797aab3c GM |
2062 | |
2063 | (goto-char (point-max)) | |
2064 | ||
2065 | ;; loop - look for all declarations of name in this file | |
2066 | (while (search-backward name nil t) | |
2067 | ||
2068 | ;; check if it really is a complete Ada identifier | |
2069 | (if (and | |
417451fe JB |
2070 | (not (save-excursion |
2071 | (goto-char (match-end 0)) | |
2072 | (looking-at "_"))) | |
2073 | (not (ada-in-string-or-comment-p)) | |
2074 | (or | |
2075 | ;; variable declaration ? | |
2076 | (save-excursion | |
2077 | (skip-chars-forward "a-zA-Z_0-9" ) | |
2078 | (ada-goto-next-non-ws) | |
2079 | (looking-at ":[^=]")) | |
2080 | ;; procedure, function, task or package declaration ? | |
2081 | (save-excursion | |
2082 | (ada-goto-previous-word) | |
2083 | (looking-at "\\<[pP][rR][oO][cC][eE][dD][uU][rR][eE]\\>\\|\\<[fF][uU][nN][cC][tT][iI][oO][nN]\\>\\|\\<[tT][yY][pP][eE]\\>\\|\\<[tT][aA][sS][kK]\\>\\|\\<[pP][aA][cC][kK][aA][gG][eE]\\>\\|\\<[bB][oO][dD][yY]\\>")))) | |
2084 | ||
2085 | ;; check if it is nearer than the ones before if any | |
2086 | (if (or (not diff) | |
2087 | (< (abs (- (point) orgpos)) diff)) | |
2088 | (progn | |
2089 | (setq newpos (point) | |
797aab3c GM |
2090 | diff (abs (- newpos orgpos)))))) |
2091 | ) | |
2092 | ||
2093 | (if newpos | |
417451fe JB |
2094 | (progn |
2095 | (message "ATTENTION: this declaration is only a (good) guess ...") | |
2096 | (goto-char newpos)) | |
797aab3c GM |
2097 | nil))) |
2098 | ||
2099 | ||
2100 | ;; Find the parent library file of the current file | |
2101 | (defun ada-goto-parent () | |
eec3232e | 2102 | "Go to the parent library file." |
797aab3c GM |
2103 | (interactive) |
2104 | (ada-require-project-file) | |
2105 | ||
2106 | (let ((buffer (ada-get-ali-buffer (buffer-file-name))) | |
417451fe JB |
2107 | (unit-name nil) |
2108 | (body-name nil) | |
2109 | (ali-name nil)) | |
797aab3c GM |
2110 | (save-excursion |
2111 | (set-buffer buffer) | |
2112 | (goto-char (point-min)) | |
2113 | (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)") | |
2114 | (setq unit-name (match-string 1)) | |
2115 | (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name)) | |
417451fe JB |
2116 | (progn |
2117 | (kill-buffer buffer) | |
2118 | (error "No parent unit !")) | |
2119 | (setq unit-name (match-string 1 unit-name)) | |
2120 | ) | |
797aab3c GM |
2121 | |
2122 | ;; look for the file name for the parent unit specification | |
2123 | (goto-char (point-min)) | |
2124 | (re-search-forward (concat "^W " unit-name | |
417451fe JB |
2125 | "%s[ \t]+\\([^ \t]+\\)[ \t]+" |
2126 | "\\([^ \t\n]+\\)")) | |
797aab3c GM |
2127 | (setq body-name (match-string 1)) |
2128 | (setq ali-name (match-string 2)) | |
2129 | (kill-buffer buffer) | |
2130 | ) | |
2131 | ||
2132 | (setq ali-name (ada-find-ali-file-in-dir ali-name)) | |
2133 | ||
2134 | (save-excursion | |
2135 | ;; Tries to open the new ali file to find the spec file | |
2136 | (if ali-name | |
417451fe JB |
2137 | (progn |
2138 | (find-file ali-name) | |
2139 | (goto-char (point-min)) | |
2140 | (re-search-forward (concat "^U " unit-name "%s[ \t]+" | |
2141 | "\\([^ \t]+\\)")) | |
2142 | (setq body-name (match-string 1)) | |
2143 | (kill-buffer (current-buffer)) | |
2144 | ) | |
2145 | ) | |
797aab3c GM |
2146 | ) |
2147 | ||
2148 | (find-file body-name) | |
2149 | )) | |
2150 | ||
2151 | (defun ada-make-filename-from-adaname (adaname) | |
eec3232e GM |
2152 | "Determine the filename in which ADANAME is found. |
2153 | This is a GNAT specific function that uses gnatkrunch." | |
797aab3c GM |
2154 | (let (krunch-buf) |
2155 | (setq krunch-buf (generate-new-buffer "*gkrunch*")) | |
2156 | (save-excursion | |
2157 | (set-buffer krunch-buf) | |
2158 | ;; send adaname to external process `gnatkr'. | |
0262d5e1 JB |
2159 | ;; Add a dummy extension, since gnatkr versions have two different |
2160 | ;; behaviors depending on the version: | |
2161 | ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc | |
2162 | ;; After: "AA.BB.CC" => aa-bb.cc | |
797aab3c | 2163 | (call-process "gnatkr" nil krunch-buf nil |
0262d5e1 | 2164 | (concat adaname ".adb") ada-krunch-args) |
797aab3c GM |
2165 | ;; fetch output of that process |
2166 | (setq adaname (buffer-substring | |
417451fe JB |
2167 | (point-min) |
2168 | (progn | |
2169 | (goto-char (point-min)) | |
2170 | (end-of-line) | |
2171 | (point)))) | |
0262d5e1 JB |
2172 | ;; Remove the extra extension we added above |
2173 | (setq adaname (substring adaname 0 -4)) | |
2174 | ||
797aab3c GM |
2175 | (kill-buffer krunch-buf))) |
2176 | adaname | |
2177 | ) | |
2178 | ||
2dccd96f | 2179 | (defun ada-make-body-gnatstub (&optional interactive) |
797aab3c GM |
2180 | "Create an Ada package body in the current buffer. |
2181 | This function uses the `gnatstub' program to create the body. | |
2182 | This function typically is to be hooked into `ff-file-created-hooks'." | |
2dccd96f | 2183 | (interactive "p") |
797aab3c GM |
2184 | |
2185 | (save-some-buffers nil nil) | |
2186 | ||
4884c50b SM |
2187 | ;; If the current buffer is the body (as is the case when calling this |
2188 | ;; function from ff-file-created-hooks), then kill this temporary buffer | |
2dccd96f | 2189 | (unless interactive |
4884c50b SM |
2190 | (progn |
2191 | (set-buffer-modified-p nil) | |
2192 | (kill-buffer (current-buffer)))) | |
a1506d29 | 2193 | |
797aab3c | 2194 | |
4884c50b SM |
2195 | ;; Make sure the current buffer is the spec (this might not be the case |
2196 | ;; if for instance the user was asked for a project file) | |
2197 | ||
2198 | (unless (buffer-file-name (car (buffer-list))) | |
2199 | (set-buffer (cadr (buffer-list)))) | |
2200 | ||
ced16f5d | 2201 | ;; Make sure we have a project file (for parameters to gnatstub). Note that |
4884c50b SM |
2202 | ;; this might have already been done if we have been called from the hook, |
2203 | ;; but this is not an expensive call) | |
2204 | (ada-require-project-file) | |
797aab3c GM |
2205 | |
2206 | ;; Call the external process gnatstub | |
2207 | (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts)) | |
417451fe JB |
2208 | (filename (buffer-file-name (car (buffer-list)))) |
2209 | (output (concat (file-name-sans-extension filename) ".adb")) | |
2210 | (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename)) | |
2211 | (buffer (get-buffer-create "*gnatstub*"))) | |
797aab3c GM |
2212 | |
2213 | (save-excursion | |
2214 | (set-buffer buffer) | |
2215 | (compilation-minor-mode 1) | |
2216 | (erase-buffer) | |
2217 | (insert gnatstub-cmd) | |
2218 | (newline) | |
2219 | ) | |
2220 | ;; call gnatstub to create the body file | |
2221 | (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd) | |
2222 | ||
2223 | (if (save-excursion | |
417451fe JB |
2224 | (set-buffer buffer) |
2225 | (goto-char (point-min)) | |
2226 | (search-forward "command not found" nil t)) | |
2227 | (progn | |
2228 | (message "gnatstub was not found -- using the basic algorithm") | |
2229 | (sleep-for 2) | |
2230 | (kill-buffer buffer) | |
2231 | (ada-make-body)) | |
797aab3c GM |
2232 | |
2233 | ;; Else clean up the output | |
2234 | ||
797aab3c | 2235 | (if (file-exists-p output) |
417451fe JB |
2236 | (progn |
2237 | (find-file output) | |
2238 | (kill-buffer buffer)) | |
797aab3c | 2239 | |
417451fe JB |
2240 | ;; display the error buffer |
2241 | (display-buffer buffer) | |
2242 | ) | |
797aab3c GM |
2243 | ))) |
2244 | ||
797aab3c | 2245 | (defun ada-xref-initialize () |
fea24571 SM |
2246 | "Function called by `ada-mode-hook' to initialize the ada-xref.el package. |
2247 | For instance, it creates the gnat-specific menus, sets some hooks for | |
797aab3c | 2248 | find-file...." |
fea24571 | 2249 | ;; This should really be an `add-hook'. -stef |
7abe197c | 2250 | (setq ff-file-created-hook 'ada-make-body-gnatstub) |
797aab3c | 2251 | |
797aab3c GM |
2252 | ;; Completion for file names in the mini buffer should ignore .ali files |
2253 | (add-to-list 'completion-ignored-extensions ".ali") | |
c94ca9e0 JB |
2254 | |
2255 | (ada-xref-update-project-menu) | |
797aab3c GM |
2256 | ) |
2257 | ||
797aab3c GM |
2258 | ;; ----- Add to ada-mode-hook --------------------------------------------- |
2259 | ||
4884c50b | 2260 | ;; This must be done before initializing the Ada menu. |
797aab3c GM |
2261 | (add-hook 'ada-mode-hook 'ada-xref-initialize) |
2262 | ||
0262d5e1 JB |
2263 | ;; Define a new error type |
2264 | (put 'error-file-not-found | |
2265 | 'error-conditions | |
2266 | '(error ada-mode-errors error-file-not-found)) | |
2267 | (put 'error-file-not-found | |
2268 | 'error-message | |
2269 | "File not found in src-dir (check project file): ") | |
2270 | ||
15ea3b67 | 2271 | ;; Initializes the cross references to the runtime library |
4884c50b | 2272 | (ada-initialize-runtime-library "") |
15ea3b67 GM |
2273 | |
2274 | ;; Add these standard directories to the search path | |
7abe197c | 2275 | (set 'ada-search-directories-internal |
15ea3b67 GM |
2276 | (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path) |
2277 | ada-search-directories)) | |
2278 | ||
797aab3c GM |
2279 | (provide 'ada-xref) |
2280 | ||
ab5796a9 | 2281 | ;;; arch-tag: 415a39fe-577b-4676-b3b1-6ff6db7ca24e |
383d5bbb | 2282 | ;;; ada-xref.el ends here |