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