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