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