1 ;; ada-xref.el --- for lookup and completion in Ada mode
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
7 ;; Rolf Ebert <ebert@inf.enst.fr>
8 ;; Emmanuel Briot <briot@gnat.com>
9 ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
10 ;; Keywords: languages ada xref
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 2, or (at your option)
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; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
30 ;;; This Package provides a set of functions to use the output of the
31 ;;; cross reference capabilities of the GNAT Ada compiler
32 ;;; for lookup and completion in Ada mode.
34 ;;; If a file *.`adp' exists in the ada-file directory, then it is
35 ;;; read for configuration informations. It is read only the first
36 ;;; time a cross-reference is asked for, and is not read later.
38 ;;; You need Emacs >= 20.2 to run this package
46 ;; ----- Requirements -----------------------------------------------------
53 ;; ------ User variables
54 (defcustom ada-xref-other-buffer t
55 "*If nil, always display the cross-references in the same buffer.
56 Otherwise create either a new buffer or a new frame."
57 :type 'boolean :group 'ada)
59 (defcustom ada-xref-create-ali nil
60 "*If non-nil, run gcc whenever the cross-references are not up-to-date.
61 If nil, the cross-reference mode never runs gcc."
62 :type 'boolean :group 'ada)
64 (defcustom ada-xref-confirm-compile nil
65 "*Non-nil means ask for confirmation before compiling or running the application."
66 :type 'boolean :group 'ada)
68 (defcustom ada-krunch-args "0"
69 "*Maximum number of characters for filenames created by `gnatkr'.
70 Set to 0, if you don't use crunched filenames. This should be a string."
71 :type 'string :group 'ada)
73 (defcustom ada-gnatls-args '("-v")
74 "*Arguments to pass to `gnatfind' to find location of the runtime.
75 Typical use is to pass `--RTS=soft-floats' on some systems that support it.
77 You can also add `-I-' if you do not want the current directory to be included.
78 Otherwise, going from specs to bodies and back will first look for files in the
79 current directory. This only has an impact if you are not using project files,
80 but only ADA_INCLUDE_PATH."
81 :type '(repeat string) :group 'ada)
83 (defcustom ada-prj-default-comp-opt "-gnatq -gnatQ"
84 "Default compilation options."
85 :type 'string :group 'ada)
87 (defcustom ada-prj-default-bind-opt ""
88 "Default binder options."
89 :type 'string :group 'ada)
91 (defcustom ada-prj-default-link-opt ""
92 "Default linker options."
93 :type 'string :group 'ada)
95 (defcustom ada-prj-default-gnatmake-opt "-g"
96 "Default options for `gnatmake'."
97 :type 'string :group 'ada)
99 (defcustom ada-prj-gnatfind-switches "-rf"
100 "Default switches to use for `gnatfind'.
101 You should modify this variable, for instance to add `-a', if you are working
102 in an environment where most ALI files are write-protected.
103 The command `gnatfind' is used every time you choose the menu
104 \"Show all references\"."
105 :type 'string :group 'ada)
107 (defcustom ada-prj-default-check-cmd
108 (concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}"
109 " -cargs ${comp_opt}")
110 "*Default command to be used to compile a single file.
111 Emacs will substitute the current filename for ${full_current}, or add
112 the filename at the end. This is the same syntax as in the project file."
113 :type 'string :group 'ada)
115 (defcustom ada-prj-default-comp-cmd
116 (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
118 "*Default command to be used to compile a single file.
119 Emacs will substitute the current filename for ${full_current}, or add
120 the filename at the end. This is the same syntax as in the project file."
121 :type 'string :group 'ada)
123 (defcustom ada-prj-default-debugger "${cross_prefix}gdb"
124 "*Default name of the debugger."
125 :type 'string :group 'ada)
127 (defcustom ada-prj-default-make-cmd
128 (concat "${cross_prefix}gnatmake -o ${main} ${main_unit} ${gnatmake_opt} "
129 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
130 "*Default command to be used to compile the application.
131 This is the same syntax as in the project file."
132 :type 'string :group 'ada)
134 (defcustom ada-prj-default-project-file ""
135 "*Name of the current project file.
136 Emacs will not try to use the search algorithm to find the project file if
137 this string is not empty. It is set whenever a project file is found."
138 :type '(file :must-match t) :group 'ada)
140 (defcustom ada-gnatstub-opts "-q -I${src_dir}"
141 "*List of the options to pass to `gnatsub' to generate the body of a package.
142 This has the same syntax as in the project file (with variable substitution)."
143 :type 'string :group 'ada)
145 (defcustom ada-always-ask-project nil
146 "*If nil, use default values when no project file was found.
147 Otherwise, ask the user for the name of the project file to use."
148 :type 'boolean :group 'ada)
150 (defconst is-windows (memq system-type (quote (windows-nt)))
151 "True if we are running on Windows NT or Windows 95.")
153 (defcustom ada-tight-gvd-integration nil
154 "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
155 If GVD is not the debugger used, nothing happens."
156 :type 'boolean :group 'ada)
158 (defcustom ada-xref-search-with-egrep t
159 "*If non-nil, use egrep to find the possible declarations for an entity.
160 This alternate method is used when the exact location was not found in the
161 information provided by GNAT. However, it might be expensive if you have a lot
162 of sources, since it will search in all the files in your project."
163 :type 'boolean :group 'ada)
165 (defvar ada-load-project-hook nil
166 "Hook that is run when loading a project file.
167 Each function in this hook takes one argument FILENAME, that is the name of
168 the project file to load.
169 This hook should be used to support new formats for the project files.
171 If the function can load the file with the given filename, it should create a
172 buffer that contains a conversion of the file to the standard format of the
173 project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\"
174 lines.) It should return nil if it doesn't know how to convert that project
178 ;; ------- Nothing to be modified by the user below this
179 (defvar ada-last-prj-file ""
180 "Name of the last project file entered by the user.")
182 (defconst ada-prj-file-extension ".adp"
183 "The extension used for project files.")
185 (defvar ada-xref-runtime-library-specs-path '()
186 "Directories where the specs for the standard library is found.
187 This is used for cross-references.")
189 (defvar ada-xref-runtime-library-ali-path '()
190 "Directories where the ali for the standard library is found.
191 This is used for cross-references.")
193 (defvar ada-xref-pos-ring '()
194 "List of positions selected by the cross-references functions.
195 Used to go back to these positions.")
197 (defvar ada-cd-command
198 (if (string-match "cmdproxy.exe" shell-file-name)
201 "Command to use to change to a specific directory.
202 On Windows systems using `cmdproxy.exe' as the shell,
203 we need to use `/d' or the drive is never changed.")
205 (defvar ada-command-separator (if is-windows " && " "\n")
206 "Separator to use between multiple commands to `compile' or `start-process'.
207 `cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use
210 (defconst ada-xref-pos-ring-max 16
211 "Number of positions kept in the list ada-xref-pos-ring.")
213 (defvar ada-operator-re
214 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
215 "Regexp to match for operators.")
217 (defvar ada-xref-project-files '()
218 "Associative list of project files with properties.
219 It has the format: (project project ...)
220 A project has the format: (project-file . project-plist)
221 \(See 'apropos plist' for operations on property lists). See
222 ada-xref-set-default-prj-values for the list of valid properties. The
223 current project is retrieved with ada-xref-current-project. Properties
224 are retrieved with ada-xref-get-project-field, set with
225 ada-xref-set-project-field. If project properties are accessed with no
226 project file, a (nil . default-properties) entry is created.")
229 ;; ----- Identlist manipulation -------------------------------------------
230 ;; An identlist is a vector that is used internally to reference an identifier
231 ;; To facilitate its use, we provide the following macros
233 (defmacro ada-make-identlist () (make-vector 8 nil))
234 (defmacro ada-name-of (identlist) (list 'aref identlist 0))
235 (defmacro ada-line-of (identlist) (list 'aref identlist 1))
236 (defmacro ada-column-of (identlist) (list 'aref identlist 2))
237 (defmacro ada-file-of (identlist) (list 'aref identlist 3))
238 (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
239 (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
240 (defmacro ada-references-of (identlist) (list 'aref identlist 6))
241 (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
243 (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
244 (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
245 (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
246 (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
247 (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
248 (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
249 (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
250 (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
252 (defsubst ada-get-ali-buffer (file)
253 "Read the ali file FILE into a new buffer, and return the buffer's name."
254 (find-file-noselect (ada-get-ali-file-name file)))
257 ;; -----------------------------------------------------------------------
259 (defun ada-quote-cmd (cmd)
260 "Duplicate all \\ characters in CMD so that it can be passed to `compile'."
261 (mapconcat 'identity (split-string cmd "\\\\") "\\\\"))
263 (defun ada-find-executable (exec-name)
264 "Find the full path to the executable file EXEC-NAME.
265 On Windows systems, this will properly handle .exe extension as well"
266 (or (ada-find-file-in-dir exec-name exec-path)
267 (ada-find-file-in-dir (concat exec-name ".exe") exec-path)
270 (defun ada-initialize-runtime-library (cross-prefix)
271 "Initialize the variables for the runtime library location.
272 CROSS-PREFIX is the prefix to use for the `gnatls' command."
274 (setq ada-xref-runtime-library-specs-path '()
275 ada-xref-runtime-library-ali-path '())
276 (set-buffer (get-buffer-create "*gnatls*"))
279 ;; Catch any error in the following form (i.e gnatls was not found)
281 ;; Even if we get an error, delete the *gnatls* buffer
285 (ada-find-executable (concat cross-prefix "gnatls"))))
286 (apply 'call-process gnatls (append '(nil t nil) ada-gnatls-args)))
287 (goto-char (point-min))
291 (search-forward "Source Search Path:")
293 (while (not (looking-at "^$"))
294 (back-to-indentation)
295 (if (looking-at "<Current_Directory>")
296 (add-to-list 'ada-xref-runtime-library-specs-path ".")
297 (add-to-list 'ada-xref-runtime-library-specs-path
298 (buffer-substring-no-properties
300 (save-excursion (end-of-line) (point)))))
305 (search-forward "Object Search Path:")
307 (while (not (looking-at "^$"))
308 (back-to-indentation)
309 (if (looking-at "<Current_Directory>")
310 (add-to-list 'ada-xref-runtime-library-ali-path ".")
311 (add-to-list 'ada-xref-runtime-library-ali-path
312 (buffer-substring-no-properties
314 (save-excursion (end-of-line) (point)))))
319 (set 'ada-xref-runtime-library-specs-path
320 (reverse ada-xref-runtime-library-specs-path))
321 (set 'ada-xref-runtime-library-ali-path
322 (reverse ada-xref-runtime-library-ali-path))
326 (defun ada-treat-cmd-string (cmd-string)
327 "Replace meta-sequences like ${...} in CMD-STRING with the appropriate value.
328 Assumes project exists.
329 As a special case, ${current} is replaced with the name of the current
330 file, minus extension but with directory, and ${full_current} is
331 replaced by the name including the extension."
333 (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string)
335 (name (match-string 2 cmd-string)))
337 ((string= name "current")
338 (setq value (file-name-sans-extension (buffer-file-name))))
339 ((string= name "full_current")
340 (setq value (buffer-file-name)))
343 (setq value (ada-xref-get-project-field (intern name))))))
345 ;; Check if there is an environment variable with the same name
347 (if (not (setq value (getenv name)))
348 (message "%s" (concat "No environment variable " name " found"))))
352 (setq cmd-string (replace-match "" t t cmd-string)))
354 (setq cmd-string (replace-match value t t cmd-string)))
356 (let ((prefix (match-string 1 cmd-string)))
357 (setq cmd-string (replace-match
358 (mapconcat (lambda(x) (concat prefix x)) value " ")
363 (defun ada-xref-set-default-prj-values (symbol ada-buffer)
364 "Reset the properties in SYMBOL to the default values for ADA-BUFFER."
366 (let ((file (buffer-file-name ada-buffer))
369 (set-buffer ada-buffer)
372 ;; Try hard to find a project file, even if the current
373 ;; buffer is not an Ada file or not associated with a file
374 (list 'filename (expand-file-name
376 (ada-prj-default-project-file
377 ada-prj-default-project-file)
378 (file (ada-prj-find-prj-file file t))
380 (message (concat "Not editing an Ada file,"
381 "and no default project "
384 'build_dir (file-name-as-directory (expand-file-name "."))
387 'casing (if (listp ada-case-exception-file)
388 ada-case-exception-file
389 (list ada-case-exception-file))
390 'comp_opt ada-prj-default-comp-opt
391 'bind_opt ada-prj-default-bind-opt
392 'link_opt ada-prj-default-link-opt
393 'gnatmake_opt ada-prj-default-gnatmake-opt
394 'gnatfind_opt ada-prj-gnatfind-switches
396 (file-name-nondirectory
397 (file-name-sans-extension file))
400 (file-name-nondirectory
401 (file-name-sans-extension file))
405 'comp_cmd (list ada-prj-default-comp-cmd)
406 'check_cmd (list ada-prj-default-check-cmd)
407 'make_cmd (list ada-prj-default-make-cmd)
408 'run_cmd (list (concat "./${main}" (if is-windows ".exe")))
409 'debug_pre_cmd (list (concat ada-cd-command " ${build_dir}"))
410 'debug_cmd (concat ada-prj-default-debugger
411 " ${main}" (if is-windows ".exe"))
412 'debug_post_cmd (list nil)))
416 (defun ada-xref-get-project-field (field)
417 "Extract the value of FIELD from the current project file.
418 Project variables are substituted.
420 Note that for src_dir and obj_dir, you should rather use
421 `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field' which will in
422 addition return the default paths."
424 (let ((file-name ada-prj-default-project-file)
427 ;; Get the project file (either the current one, or a default one)
428 (setq file (or (assoc file-name ada-xref-project-files)
429 (assoc nil ada-xref-project-files)))
431 ;; If the file was not found, use the default values
433 ;; Get the value from the file
434 (set 'value (plist-get (cdr file) field))
436 ;; Create a default nil file that contains the default values
437 (ada-xref-set-default-prj-values 'value (current-buffer))
438 (add-to-list 'ada-xref-project-files (cons nil value))
439 (ada-xref-update-project-menu)
440 (set 'value (plist-get value field))
443 ;; Substitute the ${...} constructs in all the strings, including
447 (ada-treat-cmd-string value))
451 (mapcar (lambda(x) (if x (ada-treat-cmd-string x) x)) value))
457 (defun ada-xref-get-src-dir-field ()
458 "Return the full value for src_dir, including the default directories.
459 All the directories are returned as absolute directories."
461 (let ((build-dir (ada-xref-get-project-field 'build_dir)))
463 ;; Add ${build_dir} in front of the path
466 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
469 ;; Add the standard runtime at the end
470 ada-xref-runtime-library-specs-path)))
472 (defun ada-xref-get-obj-dir-field ()
473 "Return the full value for obj_dir, including the default directories.
474 All the directories are returned as absolute directories."
476 (let ((build-dir (ada-xref-get-project-field 'build_dir)))
478 ;; Add ${build_dir} in front of the path
481 (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir)
484 ;; Add the standard runtime at the end
485 ada-xref-runtime-library-ali-path)))
487 (defun ada-xref-update-project-menu ()
488 "Update the menu Ada->Project, with the list of available project files."
489 ;; Create the standard items.
492 ["Load..." ada-set-default-project-file t]
493 ["New..." ada-prj-new t]
494 ["Edit..." ada-prj-edit t]
499 (let ((name (or (car x) "<default>"))
501 "Change the active project file."
503 (ada-parse-prj-file ,(car x))
504 (set 'ada-prj-default-project-file ,(car x))
505 (ada-xref-update-project-menu))))
507 (if (string= (file-name-extension name)
508 ada-prj-file-extension)
509 (file-name-sans-extension
510 (file-name-nondirectory name))
511 (file-name-nondirectory name))
515 (equal ada-prj-default-project-file
519 ;; Parses all the known project files, and insert at
520 ;; least the default one (in case
521 ;; ada-xref-project-files is nil)
522 (or ada-xref-project-files '(nil))))))
524 (easy-menu-add-item ada-mode-menu '() submenu)))
527 ;;-------------------------------------------------------------
528 ;;-- Searching a file anywhere on the source path.
530 ;;-- The following functions provide support for finding a file anywhere
531 ;;-- on the source path, without providing an explicit directory.
532 ;;-- They also provide file name completion in the minibuffer.
534 ;;-- Public subprograms: ada-find-file
536 ;;-------------------------------------------------------------
538 (defun ada-do-file-completion (string predicate flag)
539 "Completion function when reading a file from the minibuffer.
540 Completion is attempted in all the directories in the source path, as
541 defined in the project file."
542 ;; FIXME: doc arguments
544 (dirs (ada-xref-get-src-dir-field)))
547 (if (file-directory-p (car dirs))
548 (set 'list (append list (file-name-all-completions string (car dirs)))))
549 (set 'dirs (cdr dirs)))
550 (cond ((equal flag 'lambda)
555 (try-completion string
556 (mapcar (lambda (x) (cons x 1)) list)
560 (defun ada-find-file (filename)
561 "Open FILENAME, from anywhere in the source path.
562 Completion is available."
564 (list (completing-read "File: " 'ada-do-file-completion)))
565 (let ((file (ada-find-src-file-in-dir filename)))
568 (error (concat filename " not found in src_dir")))))
571 ;; ----- Utilities -------------------------------------------------
573 (defun ada-require-project-file ()
574 "If no project file is currently active, load a default one."
575 (if (or (not ada-prj-default-project-file)
576 (not ada-xref-project-files)
577 (string= ada-prj-default-project-file ""))
578 (ada-reread-prj-file)))
580 (defun ada-xref-push-pos (filename position)
581 "Push (FILENAME, POSITION) on the position ring for cross-references."
582 (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
583 (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
584 (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
586 (defun ada-xref-goto-previous-reference ()
587 "Go to the previous cross-reference we were on."
589 (if ada-xref-pos-ring
590 (let ((pos (car ada-xref-pos-ring)))
591 (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
592 (find-file (car (cdr pos)))
593 (goto-char (car pos)))))
595 (defun ada-convert-file-name (name)
596 "Convert from NAME to a name that can be used by the compilation commands.
597 This is overriden on VMS to convert from VMS filenames to Unix filenames."
599 ;; FIXME: use convert-standard-filename instead
601 (defun ada-set-default-project-file (name &optional keep-existing)
602 "Set the file whose name is NAME as the default project file.
603 If KEEP-EXISTING is true and a project file has already been loaded, nothing
604 is done. This is meant to be used from `ada-mode-hook', for instance, to force
605 a project file unless the user has already loaded one."
606 (interactive "fProject file:")
607 (if (or (not keep-existing)
608 (not ada-prj-default-project-file)
609 (equal ada-prj-default-project-file ""))
611 (setq ada-prj-default-project-file name)
612 (ada-reread-prj-file name))))
614 ;; ------ Handling the project file -----------------------------
616 (defun ada-prj-find-prj-file (&optional file no-user-question)
617 "Find the prj file associated with FILE (or the current buffer if nil).
618 If NO-USER-QUESTION is non-nil, use a default file if not project file was
619 found, and do not ask the user.
620 If the buffer is not an Ada buffer, associate it with the default project
621 file. If none is set, return nil."
625 ;; Use the active project file if there is one.
626 ;; This is also valid if we don't currently have an Ada buffer, or if
627 ;; the current buffer is not a real file (for instance an emerge buffer)
629 (if (or (not (string= mode-name "Ada"))
630 (not (buffer-file-name)))
632 (if (and ada-prj-default-project-file
633 (not (string= ada-prj-default-project-file "")))
634 (setq selected ada-prj-default-project-file)
637 ;; other cases: use a more complex algorithm
639 (let* ((current-file (or file (buffer-file-name)))
640 (first-choice (concat
641 (file-name-sans-extension current-file)
642 ada-prj-file-extension))
643 (dir (file-name-directory current-file))
645 ;; on Emacs 20.2, directory-files does not work if
646 ;; parse-sexp-lookup-properties is set
647 (parse-sexp-lookup-properties nil)
648 (prj-files (directory-files
650 (concat ".*" (regexp-quote
651 ada-prj-file-extension) "$")))
656 ;; Else if there is a project file with the same name as the Ada
657 ;; file, but not the same extension.
658 ((file-exists-p first-choice)
659 (set 'selected first-choice))
661 ;; Else if only one project file was found in the current directory
662 ((= (length prj-files) 1)
663 (set 'selected (car prj-files)))
665 ;; Else if there are multiple files, ask the user
666 ((and (> (length prj-files) 1) (not no-user-question))
667 (save-window-excursion
668 (with-output-to-temp-buffer "*choice list*"
669 (princ "There are more than one possible project file.\n")
670 (princ "Which one should we use ?\n\n")
671 (princ " no. file name \n")
672 (princ " --- ------------------------\n")
674 (while (<= counter (length prj-files))
675 (princ (format " %2d) %s\n"
677 (nth (1- counter) prj-files)))
678 (setq counter (1+ counter))
680 ))) ; end of with-output-to ...
684 (not (integerp choice))
686 (> choice (length prj-files)))
687 (setq choice (string-to-number
688 (read-from-minibuffer "Enter No. of your choice: "))))
689 (set 'selected (nth (1- choice) prj-files))))
691 ;; Else if no project file was found in the directory, ask a name
692 ;; to the user, using as a default value the last one entered by
694 ((= (length prj-files) 0)
695 (unless (or no-user-question (not ada-always-ask-project))
696 (setq ada-last-prj-file
698 (concat "project file [" ada-last-prj-file "]:")
699 nil ada-last-prj-file))
700 (unless (string= ada-last-prj-file "")
701 (set 'selected ada-last-prj-file))))
704 (or selected "default.adp")
708 (defun ada-parse-prj-file (prj-file)
709 "Read PRJ-FILE, set it as the active project."
710 ;; FIXME: doc nil, search, etc.
712 (let (project src_dir obj_dir make_cmd comp_cmd check_cmd casing
713 run_cmd debug_pre_cmd debug_post_cmd
714 (ada-buffer (current-buffer)))
715 (setq prj-file (expand-file-name prj-file))
717 ;; Set the project file as the active one.
718 (setq ada-prj-default-project-file prj-file)
720 ;; Initialize the project with the default values
721 (ada-xref-set-default-prj-values 'project (current-buffer))
723 ;; Do not use find-file below, since we don't want to show this
724 ;; buffer. If the file is open through speedbar, we can't use
725 ;; find-file anyway, since the speedbar frame is special and does not
726 ;; allow the selection of a file in it.
728 (if (file-exists-p prj-file)
730 (let* ((buffer (run-hook-with-args-until-success
731 'ada-load-project-hook prj-file)))
733 (setq buffer (find-file-noselect prj-file nil)))
737 (goto-char (point-min))
739 ;; Now overrides these values with the project file
741 (if (looking-at "^\\([^=]+\\)=\\(.*\\)")
743 ;; fields that are lists or paths require special processing
744 ;; FIXME: strip trailing spaces
745 ((string= (match-string 1) "src_dir")
746 (add-to-list 'src_dir
747 (file-name-as-directory (match-string 2))))
748 ((string= (match-string 1) "obj_dir")
749 (add-to-list 'obj_dir
750 (file-name-as-directory (match-string 2))))
751 ((string= (match-string 1) "casing")
752 (set 'casing (cons (match-string 2) casing)))
753 ((string= (match-string 1) "build_dir")
755 (plist-put project 'build_dir
756 (file-name-as-directory (match-string 2)))))
757 ((string= (match-string 1) "make_cmd")
758 (add-to-list 'make_cmd (match-string 2)))
759 ((string= (match-string 1) "comp_cmd")
760 (add-to-list 'comp_cmd (match-string 2)))
761 ((string= (match-string 1) "check_cmd")
762 (add-to-list 'check_cmd (match-string 2)))
763 ((string= (match-string 1) "run_cmd")
764 (add-to-list 'run_cmd (match-string 2)))
765 ((string= (match-string 1) "debug_pre_cmd")
766 (add-to-list 'debug_pre_cmd (match-string 2)))
767 ((string= (match-string 1) "debug_post_cmd")
768 (add-to-list 'debug_post_cmd (match-string 2)))
770 ;; any other field in the file is just copied
771 (set 'project (plist-put project (intern (match-string 1))
772 (match-string 2))))))
775 (if src_dir (set 'project (plist-put project 'src_dir
777 (if obj_dir (set 'project (plist-put project 'obj_dir
779 (if casing (set 'project (plist-put project 'casing
781 (if make_cmd (set 'project (plist-put project 'make_cmd
782 (reverse make_cmd))))
783 (if comp_cmd (set 'project (plist-put project 'comp_cmd
784 (reverse comp_cmd))))
785 (if check_cmd (set 'project (plist-put project 'check_cmd
786 (reverse check_cmd))))
787 (if run_cmd (set 'project (plist-put project 'run_cmd
789 (if debug_post_cmd (set 'project (plist-put project 'debug_post_cmd
790 (reverse debug_post_cmd))))
791 (if debug_pre_cmd (set 'project (plist-put project 'debug_pre_cmd
792 (reverse debug_pre_cmd))))
794 ;; Kill the project buffer
796 (set-buffer ada-buffer)
799 ;; Else the file wasn't readable (probably the default project).
800 ;; We initialize it with the current environment variables.
801 ;; We need to add the startup directory in front so that
802 ;; files locally redefined are properly found. We cannot
803 ;; add ".", which varies too much depending on what the
804 ;; current buffer is.
806 (plist-put project 'src_dir
808 (list command-line-default-directory)
809 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
810 (list "." default-directory))))
812 (plist-put project 'obj_dir
814 (list command-line-default-directory)
815 (split-string (or (getenv "ADA_OBJECTS_PATH") "") ":")
816 (list "." default-directory))))
820 ;; Delete the default project file from the list, if it is there.
821 ;; Note that in that case, this default project is the only one in
823 (if (assoc nil ada-xref-project-files)
824 (setq ada-xref-project-files nil))
826 ;; Memorize the newly read project file
827 (if (assoc prj-file ada-xref-project-files)
828 (setcdr (assoc prj-file ada-xref-project-files) project)
829 (add-to-list 'ada-xref-project-files (cons prj-file project)))
831 ;; Sets up the compilation-search-path so that Emacs is able to
832 ;; go to the source of the errors in a compilation buffer
833 (setq compilation-search-path (ada-xref-get-src-dir-field))
835 ;; Set the casing exceptions file list
838 (setq ada-case-exception-file (reverse casing))
839 (ada-case-read-exceptions)))
841 ;; Add the directories to the search path for ff-find-other-file
842 ;; Do not add the '/' or '\' at the end
843 (setq ada-search-directories-internal
844 (append (mapcar 'directory-file-name compilation-search-path)
845 ada-search-directories))
847 (ada-xref-update-project-menu)
850 ;; No prj file ? => Setup default values
851 ;; Note that nil means that all compilation modes will first look in the
852 ;; current directory, and only then in the current file's directory. This
853 ;; current file is assumed at this point to be in the common source
855 (setq compilation-search-path (list nil default-directory))
859 (defun ada-find-references (&optional pos arg local-only)
860 "Find all references to the entity under POS.
861 Calls gnatfind to find the references.
862 If ARG is t, the contents of the old *gnatfind* buffer is preserved.
863 If LOCAL-ONLY is t, only the declarations in the current file are returned."
865 (ada-require-project-file)
867 (let* ((identlist (ada-read-identifier pos))
868 (alifile (ada-get-ali-file-name (ada-file-of identlist)))
869 (process-environment (ada-set-environment)))
871 (set-buffer (get-file-buffer (ada-file-of identlist)))
873 ;; if the file is more recent than the executable
874 (if (or (buffer-modified-p (current-buffer))
875 (file-newer-than-file-p (ada-file-of identlist) alifile))
876 (ada-find-any-references (ada-name-of identlist)
877 (ada-file-of identlist)
878 nil nil local-only arg)
879 (ada-find-any-references (ada-name-of identlist)
880 (ada-file-of identlist)
881 (ada-line-of identlist)
882 (ada-column-of identlist) local-only arg)))
885 (defun ada-find-local-references (&optional pos arg)
886 "Find all references to the entity under POS.
887 Calls `gnatfind' to find the references.
888 If ARG is t, the contents of the old *gnatfind* buffer is preserved."
890 (ada-find-references pos arg t))
892 (defun ada-find-any-references
893 (entity &optional file line column local-only append)
894 "Search for references to any entity whose name is ENTITY.
895 ENTITY was first found the location given by FILE, LINE and COLUMN.
896 If LOCAL-ONLY is t, then list only the references in FILE, which
898 If APPEND is t, then append the output of the command to the existing
899 buffer `*gnatfind*', if there is one."
900 (interactive "sEntity name: ")
901 (ada-require-project-file)
903 ;; Prepare the gnatfind command. Note that we must protect the quotes
904 ;; around operators, so that they are correctly handled and can be
905 ;; processed (gnatfind \"+\":...).
907 (if (= (aref entity 0) ?\")
909 (concat "\\\"" (substring entity 1 -1) "\\\"")
910 (concat "'\"" (substring entity 1 -1) "\"'"))
912 (switches (ada-xref-get-project-field 'gnatfind_opt))
913 (command (concat "gnat find " switches " "
915 (if file (concat ":" (file-name-nondirectory file)))
916 (if line (concat ":" line))
917 (if column (concat ":" column))
918 (if local-only (concat " " (file-name-nondirectory file)))
922 ;; If a project file is defined, use it
923 (if (and ada-prj-default-project-file
924 (not (string= ada-prj-default-project-file "")))
925 (if (string-equal (file-name-extension ada-prj-default-project-file)
927 (setq command (concat command " -P" ada-prj-default-project-file))
928 (setq command (concat command " -p" ada-prj-default-project-file))))
930 (if (and append (get-buffer "*gnatfind*"))
932 (set-buffer "*gnatfind*")
933 (setq old-contents (buffer-string))))
935 (let ((compilation-error "reference"))
936 (compilation-start command))
938 ;; Hide the "Compilation" menu
940 (set-buffer "*gnatfind*")
941 (local-unset-key [menu-bar compilation-menu])
946 (insert old-contents)
947 (goto-char (point-max)))))
951 (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
953 ;; ----- Identifier Completion --------------------------------------------
954 (defun ada-complete-identifier (pos)
955 "Try to complete the identifier around POS, using compiler cross-reference information."
957 (ada-require-project-file)
959 ;; Initialize function-local variables and jump to the .ali buffer
960 ;; Note that for regexp search is case insensitive too
961 (let* ((curbuf (current-buffer))
962 (identlist (ada-read-identifier pos))
963 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
964 (regexp-quote (ada-name-of identlist))
969 ;; Open the .ali file
970 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
971 (goto-char (point-max))
973 ;; build an alist of possible completions
974 (while (re-search-backward sofar nil t)
975 (setq symalist (cons (cons (match-string 1) nil) symalist)))
977 (setq completed (try-completion "" symalist))
982 ;; deletes the incomplete identifier in the buffer
984 (looking-at "[a-zA-Z0-9_]+")
986 ;; inserts the completed symbol
990 ;; ----- Cross-referencing ----------------------------------------
992 (defun ada-point-and-xref ()
993 "Jump to the declaration of the entity below the cursor."
995 (mouse-set-point last-input-event)
996 (ada-goto-declaration (point)))
998 (defun ada-point-and-xref-body ()
999 "Jump to the body of the entity under the cursor."
1001 (mouse-set-point last-input-event)
1002 (ada-goto-body (point)))
1004 (defun ada-goto-body (pos &optional other-frame)
1005 "Display the body of the entity around POS.
1006 OTHER-FRAME non-nil means display in another frame.
1007 If the entity doesn't have a body, display its declaration.
1008 As a side effect, the buffer for the declaration is also open."
1010 (ada-goto-declaration pos other-frame)
1012 ;; Temporarily force the display in the same buffer, since we
1013 ;; already changed previously
1014 (let ((ada-xref-other-buffer nil))
1015 (ada-goto-declaration (point) nil)))
1017 (defun ada-goto-declaration (pos &optional other-frame)
1018 "Display the declaration of the identifier around POS.
1019 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1021 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1023 (ada-require-project-file)
1025 (ada-xref-push-pos (buffer-file-name) pos)
1027 ;; First try the standard algorithm by looking into the .ali file, but if
1028 ;; that file was too old or even did not exist, try to look in the whole
1029 ;; object path for a possible location.
1030 (let ((identlist (ada-read-identifier pos)))
1032 (ada-find-in-ali identlist other-frame)
1033 ;; File not found: print explicit error message
1034 (error-file-not-found
1035 (message (concat (error-message-string err)
1039 (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist))))
1041 ;; If the ALI file was up-to-date, then we probably have a predefined
1042 ;; entity, whose references are not given by GNAT
1043 (if (and (file-exists-p ali-file)
1044 (file-newer-than-file-p ali-file (ada-file-of identlist)))
1045 (message "No cross-reference found -- may be a predefined entity.")
1047 ;; Else, look in every ALI file, except if the user doesn't want that
1048 (if ada-xref-search-with-egrep
1049 (ada-find-in-src-path identlist other-frame)
1050 (message "Cross-referencing information is not up-to-date; please recompile.")
1053 (defun ada-goto-declaration-other-frame (pos)
1054 "Display the declaration of the identifier around POS.
1055 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1057 (ada-goto-declaration pos t))
1059 (defun ada-remote (command)
1060 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1061 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1062 (if (or (not machine) (string= machine ""))
1064 (format "%s %s '(%s)'"
1065 remote-shell-program
1069 (defun ada-get-absolute-dir-list (dir-list root-dir)
1070 "Return the list of absolute directories found in DIR-LIST.
1071 If a directory is a relative directory, ROOT-DIR is prepended."
1072 (mapcar (lambda (x) (expand-file-name x root-dir)) dir-list))
1074 (defun ada-set-environment ()
1075 "Prepare an environment for Ada compilation.
1076 This returns a new value to use for `process-environment',
1077 but does not actually put it into use.
1078 It modifies the source path and object path with the values found in the
1080 (let ((include (getenv "ADA_INCLUDE_PATH"))
1081 (objects (getenv "ADA_OBJECTS_PATH"))
1082 (build-dir (ada-xref-get-project-field 'build_dir)))
1084 (set 'include (concat path-separator include)))
1086 (set 'objects (concat path-separator objects)))
1088 (concat "ADA_INCLUDE_PATH="
1089 (mapconcat (lambda(x) (expand-file-name x build-dir))
1090 (ada-xref-get-project-field 'src_dir)
1094 (concat "ADA_OBJECTS_PATH="
1095 (mapconcat (lambda(x) (expand-file-name x build-dir))
1096 (ada-xref-get-project-field 'obj_dir)
1099 process-environment))))
1101 (defun ada-compile-application (&optional arg)
1102 "Compile the application, using the command found in the project file.
1103 If ARG is not nil, ask for user confirmation."
1105 (ada-require-project-file)
1106 (let ((cmd (ada-xref-get-project-field 'make_cmd))
1107 (process-environment (ada-set-environment))
1108 (compilation-scroll-output t))
1110 (setq compilation-search-path (ada-xref-get-src-dir-field))
1112 ;; If no project file was found, ask the user
1114 (setq cmd '("") arg t))
1116 ;; Make a single command from the list of commands, including the
1117 ;; commands to run it on a remote machine.
1118 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1120 (if (or ada-xref-confirm-compile arg)
1121 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1123 ;; Insert newlines so as to separate the name of the commands to run
1124 ;; and the output of the commands. This doesn't work with cmdproxy.exe,
1125 ;; which gets confused by newline characters.
1126 (if (not (string-match ".exe" shell-file-name))
1127 (setq cmd (concat cmd "\n\n")))
1129 (compile (ada-quote-cmd cmd))))
1131 (defun ada-compile-current (&optional arg prj-field)
1132 "Recompile the current file.
1133 If ARG is not nil, ask for user confirmation of the command.
1134 PRJ-FIELD is the name of the field to use in the project file to get the
1135 command, and should be either comp_cmd (default) or check_cmd."
1137 (ada-require-project-file)
1138 (let* ((field (if prj-field prj-field 'comp_cmd))
1139 (cmd (ada-xref-get-project-field field))
1140 (process-environment (ada-set-environment))
1141 (compilation-scroll-output t))
1143 (setq compilation-search-path (ada-xref-get-src-dir-field))
1146 (setq cmd '("") arg t))
1148 ;; Make a single command from the list of commands, including the
1149 ;; commands to run it on a remote machine.
1150 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1152 ;; If no project file was found, ask the user
1153 (if (or ada-xref-confirm-compile arg)
1154 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1156 (compile (ada-quote-cmd cmd))))
1158 (defun ada-check-current (&optional arg)
1159 "Recompile the current file.
1160 If ARG is not nil, ask for user confirmation of the command."
1162 (ada-compile-current arg 'check_cmd))
1164 (defun ada-run-application (&optional arg)
1165 "Run the application.
1166 if ARG is not-nil, ask for user confirmation."
1168 (ada-require-project-file)
1170 (let ((machine (ada-xref-get-project-field 'cross_prefix)))
1171 (if (and machine (not (string= machine "")))
1172 (error "This feature is not supported yet for cross environments")))
1174 (let ((command (ada-xref-get-project-field 'run_cmd)))
1176 ;; Guess the command if it wasn't specified
1178 (set 'command (list (file-name-sans-extension (buffer-name)))))
1180 ;; Modify the command to run remotely
1181 (setq command (ada-remote (mapconcat 'identity command
1182 ada-command-separator)))
1184 ;; Ask for the arguments to the command if required
1185 (if (or ada-xref-confirm-compile arg)
1186 (setq command (read-from-minibuffer "Enter command to execute: "
1191 (set-buffer (get-buffer-create "*run*"))
1192 (set 'buffer-read-only nil)
1195 (start-process "run" (current-buffer) shell-file-name
1198 ;; Set these two variables to their default values, since otherwise
1199 ;; the output buffer is scrolled so that only the last output line
1200 ;; is visible at the top of the buffer.
1201 (set (make-local-variable 'scroll-step) 0)
1202 (set (make-local-variable 'scroll-conservatively) 0)
1204 (display-buffer "*run*")
1206 ;; change to buffer *run* for interactive programs
1208 (switch-to-buffer "*run*")
1211 (defun ada-gdb-application (&optional arg executable-name)
1212 "Start the debugger on the application.
1213 If ARG is non-nil, ask the user to confirm the command.
1214 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1217 (let ((buffer (current-buffer))
1218 cmd pre-cmd post-cmd)
1219 (ada-require-project-file)
1220 (setq cmd (if executable-name
1221 (concat ada-prj-default-debugger " " executable-name)
1222 (ada-xref-get-project-field 'debug_cmd))
1223 pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
1224 post-cmd (ada-xref-get-project-field 'debug_post_cmd))
1226 ;; If the command was not given in the project file, start a bare gdb
1228 (set 'cmd (concat ada-prj-default-debugger
1231 (file-name-sans-extension (buffer-file-name))))))
1233 ;; For gvd, add an extra switch so that the Emacs window is completly
1234 ;; swallowed inside the Gvd one
1235 (if (and ada-tight-gvd-integration
1236 (string-match "^[^ \t]*gvd" cmd))
1237 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1238 ;; We make sure that gvd swallows the new frame, not the one the
1239 ;; user has been using until now
1240 ;; The frame is made invisible initially, so that GtkPlug gets a
1241 ;; chance to fully manage it. Then it works fine with Enlightenment
1243 (let ((frame (make-frame '((visibility . nil)))))
1245 cmd " --editor-window="
1246 (cdr (assoc 'outer-window-id (frame-parameters frame)))))
1247 (select-frame frame)))
1249 ;; Add a -fullname switch
1250 ;; Use the remote machine
1251 (set 'cmd (ada-remote (concat cmd " -fullname ")))
1253 ;; Ask for confirmation if required
1254 (if (or arg ada-xref-confirm-compile)
1255 (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
1257 (let ((old-comint-exec (symbol-function 'comint-exec)))
1259 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1260 ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef
1261 (fset 'gud-gdb-massage-args (lambda (file args) args))
1263 (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
1264 (if (not (equal pre-cmd ""))
1265 (setq pre-cmd (concat pre-cmd ada-command-separator)))
1267 (set 'post-cmd (mapconcat 'identity post-cmd "\n"))
1269 (set 'post-cmd (concat post-cmd "\n")))
1272 ;; Temporarily replaces the definition of `comint-exec' so that we
1273 ;; can execute commands before running gdb.
1274 ;; FIXME: This is evil and not temporary !!! -stef
1276 `(lambda (buffer name command startfile switches)
1277 (let (compilation-buffer-name-function)
1279 (set 'compilation-buffer-name-function
1280 (lambda(x) (buffer-name buffer)))
1281 (compile (ada-quote-cmd
1284 (mapconcat 'identity switches " "))))))
1287 ;; Tight integration should force the tty mode
1288 (if (and (string-match "gvd" (comint-arguments cmd 0 0))
1289 ada-tight-gvd-integration
1290 (not (string-match "--tty" cmd)))
1291 (setq cmd (concat cmd "--tty")))
1293 (if (and (string-match "jdb" (comint-arguments cmd 0 0))
1295 (funcall (symbol-function 'jdb) cmd)
1298 ;; Restore the standard fset command (or for instance C-U M-x shell
1299 ;; wouldn't work anymore
1301 (fset 'comint-exec old-comint-exec)
1303 ;; Send post-commands to the debugger
1304 (process-send-string (get-buffer-process (current-buffer)) post-cmd)
1306 ;; Move to the end of the debugger buffer, so that it is automatically
1307 ;; scrolled from then on.
1308 (goto-char (point-max))
1310 ;; Display both the source window and the debugger window (the former
1311 ;; above the latter). No need to show the debugger window unless it
1312 ;; is going to have some relevant information.
1313 (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
1314 (string-match "--tty" cmd))
1315 (split-window-vertically))
1316 (switch-to-buffer buffer)
1319 (defun ada-reread-prj-file (&optional filename)
1320 "Reread either the current project, or FILENAME if non-nil."
1323 (ada-parse-prj-file filename)
1324 (ada-parse-prj-file (ada-prj-find-prj-file)))
1326 ;; Reread the location of the standard runtime library
1327 (ada-initialize-runtime-library
1328 (or (ada-xref-get-project-field 'cross_prefix) ""))
1331 ;; ------ Private routines
1333 (defun ada-xref-current (file &optional ali-file-name)
1334 "Update the cross-references for FILE.
1335 This in fact recompiles FILE to create ALI-FILE-NAME.
1336 This function returns the name of the file that was recompiled to generate
1337 the cross-reference information. Note that the ali file can then be deduced by
1338 replacing the file extension with `.ali'."
1340 (if (and ali-file-name
1341 (get-file-buffer ali-file-name))
1342 (kill-buffer (get-file-buffer ali-file-name)))
1344 (let* ((name (ada-convert-file-name file))
1345 (body-name (or (ada-get-body-name name) name)))
1347 ;; Always recompile the body when we can. We thus temporarily switch to a
1348 ;; buffer than contains the body of the unit
1350 (let ((body-visible (find-buffer-visiting body-name))
1353 (set-buffer body-visible)
1354 (find-file body-name))
1356 ;; Execute the compilation. Note that we must wait for the end of the
1357 ;; process, or the ALI file would still not be available.
1358 ;; Unfortunately, the underlying `compile' command that we use is
1360 (ada-compile-current)
1361 (setq process (get-buffer-process "*compilation*"))
1364 (not (equal (process-status process) 'exit)))
1367 ;; remove the buffer for the body if it wasn't there before
1368 (unless body-visible
1369 (kill-buffer (find-buffer-visiting body-name)))
1373 (defun ada-find-file-in-dir (file dir-list)
1374 "Search for FILE in DIR-LIST."
1376 (while (and (not found) dir-list)
1377 (set 'found (concat (file-name-as-directory (car dir-list))
1378 (file-name-nondirectory file)))
1380 (unless (file-exists-p found)
1382 (set 'dir-list (cdr dir-list)))
1385 (defun ada-find-ali-file-in-dir (file)
1386 "Find the ali file FILE, searching obj_dir for the current project.
1387 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1388 and the standard runtime location at the end."
1389 (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
1391 (defun ada-find-src-file-in-dir (file)
1392 "Find the source file FILE, searching src_dir for the current project.
1393 Adds the standard runtime location at the end of the search path to conform
1394 to gnatmake's behavior."
1395 (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
1397 (defun ada-get-ali-file-name (file)
1398 "Create the ali file name for the ada-file FILE.
1399 The file is searched for in every directory shown in the obj_dir lines of
1402 ;; This function has to handle the special case of non-standard
1403 ;; file names (i.e. not .adb or .ads)
1404 ;; The trick is the following:
1405 ;; 1- replace the extension of the current file with .ali,
1406 ;; and look for this file
1407 ;; 2- If this file is found:
1408 ;; grep the "^U" lines, and make sure we are not reading the
1409 ;; .ali file for a spec file. If we are, go to step 3.
1410 ;; 3- If the file is not found or step 2 failed:
1411 ;; find the name of the "other file", ie the body, and look
1412 ;; for its associated .ali file by subtituing the extension
1414 ;; We must also handle the case of separate packages and subprograms:
1415 ;; 4- If no ali file was found, we try to modify the file name by removing
1416 ;; everything after the last '-' or '.' character, so as to get the
1417 ;; ali file for the parent unit. If we found an ali file, we check that
1418 ;; it indeed contains the definition for the separate entity by checking
1419 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1423 (set-buffer (get-file-buffer file))
1424 (let ((short-ali-file-name
1425 (concat (file-name-sans-extension (file-name-nondirectory file))
1430 ;; If we have a non-standard file name, and this is a spec, we first
1431 ;; look for the .ali file of the body, since this is the one that
1432 ;; contains the most complete information. If not found, we will do what
1433 ;; we can with the .ali file for the spec...
1435 (if (not (string= (file-name-extension file) "ads"))
1436 (let ((specs ada-spec-suffixes))
1438 (if (string-match (concat (regexp-quote (car specs)) "$")
1441 (set 'specs (cdr specs)))))
1445 (ada-find-ali-file-in-dir
1446 (concat (file-name-sans-extension
1447 (file-name-nondirectory
1448 (ada-other-file-name)))
1455 ;; Else we take the .ali file associated with the unit
1456 (ada-find-ali-file-in-dir short-ali-file-name)
1459 ;; else we did not find the .ali file Second chance: in case
1460 ;; the files do not have standard names (such as for instance
1461 ;; file_s.ada and file_b.ada), try to go to the other file
1462 ;; and look for its ali file
1463 (ada-find-ali-file-in-dir
1464 (concat (file-name-sans-extension
1465 (file-name-nondirectory (ada-other-file-name)))
1469 ;; If we still don't have an ali file, try to get the one
1470 ;; from the parent unit, in case we have a separate entity.
1471 (let ((parent-name (file-name-sans-extension
1472 (file-name-nondirectory file))))
1474 (while (and (not ali-file-name)
1475 (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
1477 (set 'parent-name (match-string 1 parent-name))
1478 (set 'ali-file-name (ada-find-ali-file-in-dir
1479 (concat parent-name ".ali")))
1483 ;; If still not found, try to recompile the file
1484 (if (not ali-file-name)
1485 ;; Recompile only if the user asked for this, and search the ali
1486 ;; filename again. We avoid a possible infinite recursion by
1487 ;; temporarily disabling the automatic compilation.
1489 (if ada-xref-create-ali
1491 (concat (file-name-sans-extension (ada-xref-current file))
1494 (error "`.ali' file not found; recompile your source file"))
1497 ;; same if the .ali file is too old and we must recompile it
1498 (if (and (file-newer-than-file-p file ali-file-name)
1499 ada-xref-create-ali)
1500 (ada-xref-current file ali-file-name)))
1502 ;; Always return the correct absolute file name
1503 (expand-file-name ali-file-name))
1506 (defun ada-get-ada-file-name (file original-file)
1507 "Create the complete file name (+directory) for FILE.
1508 The original file (where the user was) is ORIGINAL-FILE. Search in project
1509 file for possible paths."
1513 ;; If the buffer for original-file, use it to get the values from the
1514 ;; project file, otherwise load the file and its project file
1515 (let ((buffer (get-file-buffer original-file)))
1518 (find-file original-file)
1519 (ada-require-project-file)))
1521 ;; we choose the first possible completion and we
1522 ;; return the absolute file name
1523 (let ((filename (ada-find-src-file-in-dir file)))
1525 (expand-file-name filename)
1526 (signal 'error-file-not-found (file-name-nondirectory file)))
1529 (defun ada-find-file-number-in-ali (file)
1530 "Return the file number for FILE in the associated ali file."
1531 (set-buffer (ada-get-ali-buffer file))
1532 (goto-char (point-min))
1534 (let ((begin (re-search-forward "^D")))
1536 (re-search-forward (concat "^D " (file-name-nondirectory file)))
1537 (count-lines begin (point))))
1539 (defun ada-read-identifier (pos)
1540 "Return the identlist around POS and switch to the .ali buffer.
1541 The returned list represents the entity, and can be manipulated through the
1542 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1544 ;; If at end of buffer (e.g the buffer is empty), error
1545 (if (>= (point) (point-max))
1546 (error "No identifier on point"))
1548 ;; goto first character of the identifier/operator (skip backward < and >
1549 ;; since they are part of multiple character operators
1551 (skip-chars-backward "a-zA-Z0-9_<>")
1553 ;; check if it really is an identifier
1554 (if (ada-in-comment-p)
1555 (error "Inside comment"))
1557 (let (identifier identlist)
1558 ;; Just in front of a string => we could have an operator declaration,
1559 ;; as in "+", "-", ..
1560 (if (= (char-after) ?\")
1563 ;; if looking at an operator
1564 ;; This is only true if:
1565 ;; - the symbol is +, -, ...
1566 ;; - the symbol is made of letters, and not followed by _ or a letter
1567 (if (and (looking-at ada-operator-re)
1568 (or (not (= (char-syntax (char-after)) ?w))
1569 (not (or (= (char-syntax (char-after (match-end 0))) ?w)
1570 (= (char-after (match-end 0)) ?_)))))
1572 (if (and (= (char-before) ?\")
1573 (= (char-after (+ (length (match-string 0)) (point))) ?\"))
1575 (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
1577 (if (ada-in-string-p)
1578 (error "Inside string or character constant"))
1579 (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
1580 (error "No cross-reference available for reserved keyword"))
1581 (if (looking-at "[a-zA-Z0-9_]+")
1582 (set 'identifier (match-string 0))
1583 (error "No identifier around")))
1585 ;; Build the identlist
1586 (set 'identlist (ada-make-identlist))
1587 (ada-set-name identlist (downcase identifier))
1588 (ada-set-line identlist
1589 (number-to-string (count-lines 1 (point))))
1590 (ada-set-column identlist
1591 (number-to-string (1+ (current-column))))
1592 (ada-set-file identlist (buffer-file-name))
1596 (defun ada-get-all-references (identlist)
1597 "Complete IDENTLIST with definition file and places where it is referenced.
1598 Information is extracted from the ali file."
1600 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
1602 (set-buffer ali-buffer)
1603 (goto-char (point-min))
1604 (ada-set-on-declaration identlist nil)
1606 ;; First attempt: we might already be on the declaration of the identifier
1607 ;; We want to look for the declaration only in a definite interval (after
1608 ;; the "^X ..." line for the current file, and before the next "^X" line
1610 (if (re-search-forward
1611 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
1613 (let ((bound (save-excursion (re-search-forward "^X " nil t))))
1614 (set 'declaration-found
1616 (concat "^" (ada-line-of identlist)
1617 "." (ada-column-of identlist)
1618 "[ *]" (ada-name-of identlist)
1619 "[{\[\(<= ]?\\(.*\\)$") bound t))
1620 (if declaration-found
1621 (ada-set-on-declaration identlist t))
1624 ;; If declaration is still nil, then we were not on a declaration, and
1625 ;; have to fall back on other algorithms
1627 (unless declaration-found
1629 ;; Since we alread know the number of the file, search for a direct
1631 (goto-char (point-min))
1632 (set 'declaration-found t)
1635 (number-to-string (ada-find-file-number-in-ali
1636 (ada-file-of identlist))))
1637 (unless (re-search-forward (concat (ada-ali-index-of identlist)
1638 "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
1639 (ada-line-of identlist)
1641 (ada-column-of identlist) "\\>")
1644 ;; if we did not find it, it may be because the first reference
1645 ;; is not required to have a 'unit_number|' item included.
1646 ;; Or maybe we are already on the declaration...
1647 (unless (re-search-forward
1649 "^[0-9]+.[0-9]+[ *]"
1650 (ada-name-of identlist)
1651 "[ <{=\(\[]\\(.\\|\n\\.\\)*\\<"
1652 (ada-line-of identlist)
1654 (ada-column-of identlist) "\\>")
1657 ;; If still not found, then either the declaration is unknown
1658 ;; or the source file has been modified since the ali file was
1660 (set 'declaration-found nil)
1664 ;; Last check to be completly sure we have found the correct line (the
1665 ;; ali might not be up to date for instance)
1666 (if declaration-found
1669 ;; while we have a continuation line, go up one line
1670 (while (looking-at "^\\.")
1672 (beginning-of-line))
1673 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1674 (ada-name-of identlist) "[ <{=\(\[]"))
1675 (set 'declaration-found nil))))
1677 ;; Still no success ! The ali file must be too old, and we need to
1678 ;; use a basic algorithm based on guesses. Note that this only happens
1679 ;; if the user does not want us to automatically recompile files
1681 (unless declaration-found
1682 (if (ada-xref-find-in-modified-ali identlist)
1683 (set 'declaration-found t)
1684 ;; No more idea to find the declaration. Give up
1686 (kill-buffer ali-buffer)
1687 (error (concat "No declaration of " (ada-name-of identlist)
1693 ;; Now that we have found a suitable line in the .ali file, get the
1694 ;; information available
1696 (if declaration-found
1697 (let ((current-line (buffer-substring
1698 (point) (save-excursion (end-of-line) (point)))))
1702 (while (looking-at "^\\.\\(.*\\)")
1703 (set 'current-line (concat current-line (match-string 1)))
1707 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
1709 ;; If we can find the file
1711 (ada-set-declare-file
1713 (ada-get-ada-file-name (match-string 1)
1714 (ada-file-of identlist)))
1716 ;; Else clean up the ali file
1717 (error-file-not-found
1718 (signal (car err) (cdr err)))
1720 (kill-buffer ali-buffer)
1721 (error (error-message-string err)))
1724 (ada-set-references identlist current-line)
1728 (defun ada-xref-find-in-modified-ali (identlist)
1729 "Find the matching position for IDENTLIST in the current ali buffer.
1730 This function is only called when the file was not up-to-date, so we need
1731 to make some guesses.
1732 This function is disabled for operators, and only works for identifiers."
1734 (unless (= (string-to-char (ada-name-of identlist)) ?\")
1736 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1737 (my-regexp (concat "[ *]"
1738 (regexp-quote (ada-name-of identlist)) " "))
1744 (ali-buffer (current-buffer)))
1746 (goto-char (point-max))
1747 (while (re-search-backward my-regexp nil t)
1749 (set 'line-ali (count-lines 1 (point)))
1751 ;; have a look at the line and column numbers
1752 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1754 (setq line-ada (match-string 1))
1755 (setq col-ada (match-string 2)))
1756 (setq line-ada "--")
1759 ;; construct a list with the file names and the positions within
1760 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
1762 'declist (list line-ali (match-string 1) line-ada col-ada))
1767 ;; how many possible declarations have we found ?
1768 (setq len (length declist))
1772 (kill-buffer (current-buffer))
1773 (error (concat "No declaration of "
1774 (ada-name-of identlist)
1775 " recorded in .ali file")))
1777 ;; one => should be the right one
1779 (goto-line (caar declist)))
1781 ;; more than one => display choice list
1783 (save-window-excursion
1784 (with-output-to-temp-buffer "*choice list*"
1786 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1787 (princ "Possible declarations are:\n\n")
1788 (princ " no. in file at line col\n")
1789 (princ " --- --------------------- ---- ----\n")
1791 (while (< counter len)
1792 (princ (format " %2d) %-21s %4s %4s\n"
1794 (ada-get-ada-file-name
1795 (nth 1 (nth counter declist))
1796 (ada-file-of identlist))
1797 (nth 2 (nth counter declist))
1798 (nth 3 (nth counter declist))
1800 (setq counter (1+ counter))
1803 ) ; end of with-output-to ...
1807 (not (integerp choice))
1812 (read-from-minibuffer "Enter No. of your choice: "))))
1814 (set-buffer ali-buffer)
1815 (goto-line (car (nth (1- choice) declist)))
1819 (defun ada-find-in-ali (identlist &optional other-frame)
1820 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1821 If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil,
1822 opens a new window to show the declaration."
1824 (ada-get-all-references identlist)
1825 (let ((ali-line (ada-references-of identlist))
1830 ;; Note: in some cases, an entity can have multiple references to the
1831 ;; bodies (this is for instance the case for a separate subprogram, that
1832 ;; has a reference both to the stub and to the real body).
1833 ;; In that case, we simply go to each one in turn.
1835 ;; Get all the possible locations
1836 (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line)
1837 (set 'locations (list (list (match-string 1 ali-line) ;; line
1838 (match-string 2 ali-line) ;; column
1839 (ada-declare-file-of identlist))))
1840 (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)"
1842 (setq line (match-string 1 ali-line)
1843 col (match-string 3 ali-line)
1844 start (match-end 3))
1846 ;; it there was a file number in the same line
1847 ;; Make sure we correctly handle the case where the first file reference
1848 ;; on the line is the type reference.
1849 ;; 1U2 T(2|2r3) 34r23
1850 (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?"
1851 (match-string 0 ali-line))
1853 (let ((file-number (match-string 1 ali-line)))
1854 (goto-char (point-min))
1855 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1856 (string-to-number file-number))
1857 (set 'file (match-string 1))
1859 ;; Else get the nearest file
1860 (set 'file (ada-declare-file-of identlist)))
1862 (set 'locations (append locations (list (list line col file)))))
1864 ;; Add the specs at the end again, so that from the last body we go to
1866 (set 'locations (append locations (list (car locations))))
1868 ;; Find the new location we want to go to.
1869 ;; If we are on none of the locations listed, we simply go to the specs.
1871 (setq line (caar locations)
1872 col (nth 1 (car locations))
1873 file (nth 2 (car locations)))
1876 (if (and (string= (caar locations) (ada-line-of identlist))
1877 (string= (nth 1 (car locations)) (ada-column-of identlist))
1878 (string= (file-name-nondirectory (nth 2 (car locations)))
1879 (file-name-nondirectory (ada-file-of identlist))))
1880 (setq locations (cadr locations)
1881 line (car locations)
1882 col (nth 1 locations)
1883 file (nth 2 locations)
1885 (set 'locations (cdr locations))))
1887 ;; Find the file in the source path
1888 (set 'file (ada-get-ada-file-name file (ada-file-of identlist)))
1890 ;; Kill the .ali buffer
1891 (kill-buffer (current-buffer))
1893 ;; Now go to the buffer
1894 (ada-xref-change-buffer file
1895 (string-to-number line)
1896 (1- (string-to-number col))
1901 (defun ada-find-in-src-path (identlist &optional other-frame)
1902 "More general function for cross-references.
1903 This function should be used when the standard algorithm that parses the
1904 .ali file has failed, either because that file was too old or even did not
1906 This function attempts to find the possible declarations for the identifier
1907 anywhere in the object path.
1908 This command requires the external `egrep' program to be available.
1910 This works well when one is using an external librarie and wants
1911 to find the declaration and documentation of the subprograms one is
1913 ;; FIXME: what does this function do?
1915 (dirs (ada-xref-get-obj-dir-field))
1916 (regexp (concat "[ *]" (ada-name-of identlist)))
1923 ;; Do the grep in all the directories. We do multiple shell
1924 ;; commands instead of one in case there is no .ali file in one
1925 ;; of the directory and the shell stops because of that.
1927 (set-buffer (get-buffer-create "*grep*"))
1929 (insert (shell-command-to-string
1932 (shell-quote-argument (concat "^X|" regexp "( |$)"))
1934 (shell-quote-argument (file-name-as-directory (car dirs)))
1936 (set 'dirs (cdr dirs)))
1938 ;; Now parse the output
1939 (set 'case-fold-search t)
1940 (goto-char (point-min))
1941 (while (re-search-forward regexp nil t)
1944 (if (not (= (char-after) ?X))
1946 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
1947 (setq line (match-string 1)
1948 column (match-string 2))
1949 (re-search-backward "^X [0-9]+ \\(.*\\)$")
1950 (set 'file (list (match-string 1) line column))
1952 ;; There could be duplicate choices, because of the structure
1953 ;; of the .ali files
1954 (unless (member file list)
1955 (set 'list (append list (list file))))))))
1957 ;; Current buffer is still "*grep*"
1958 (kill-buffer "*grep*")
1961 ;; Now display the list of possible matches
1964 ;; No choice found => Error
1966 (error "No cross-reference found, please recompile your file"))
1968 ;; Only one choice => Do the cross-reference
1969 ((= (length list) 1)
1970 (set 'file (ada-find-src-file-in-dir (caar list)))
1972 (ada-xref-change-buffer file
1973 (string-to-number (nth 1 (car list)))
1974 (string-to-number (nth 2 (car list)))
1977 (error (concat (caar list) " not found in src_dir")))
1978 (message "This is only a (good) guess at the cross-reference.")
1981 ;; Else, ask the user
1983 (save-window-excursion
1984 (with-output-to-temp-buffer "*choice list*"
1986 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1987 (princ "Possible declarations are:\n\n")
1988 (princ " no. in file at line col\n")
1989 (princ " --- --------------------- ---- ----\n")
1991 (while (< counter (length list))
1992 (princ (format " %2d) %-21s %4s %4s\n"
1994 (nth 0 (nth counter list))
1995 (nth 1 (nth counter list))
1996 (nth 2 (nth counter list))
1998 (setq counter (1+ counter))
2001 (while (or (not choice)
2002 (not (integerp choice))
2004 (> choice (length list)))
2007 (read-from-minibuffer "Enter No. of your choice: "))))
2009 (set 'choice (1- choice))
2010 (kill-buffer "*choice list*")
2012 (set 'file (ada-find-src-file-in-dir (car (nth choice list))))
2014 (ada-xref-change-buffer file
2015 (string-to-number (nth 1 (nth choice list)))
2016 (string-to-number (nth 2 (nth choice list)))
2019 (signal 'error-file-not-found (car (nth choice list))))
2020 (message "This is only a (good) guess at the cross-reference.")
2023 (defun ada-xref-change-buffer
2024 (file line column identlist &optional other-frame)
2025 "Select and display FILE, at LINE and COLUMN.
2026 If we do not end on the same identifier as IDENTLIST, find the closest
2027 match. Kills the .ali buffer at the end.
2028 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2030 (let (declaration-buffer)
2032 ;; Select and display the destination buffer
2033 (if ada-xref-other-buffer
2035 (find-file-other-frame file)
2036 (set 'declaration-buffer (find-file-noselect file))
2037 (set-buffer declaration-buffer)
2038 (switch-to-buffer-other-window declaration-buffer)
2043 ;; move the cursor to the correct position
2046 (move-to-column column)
2048 ;; If we are not on the identifier, the ali file was not up-to-date.
2049 ;; Try to find the nearest position where the identifier is found,
2050 ;; this is probably the right one.
2051 (unless (looking-at (ada-name-of identlist))
2052 (ada-xref-search-nearest (ada-name-of identlist)))
2056 (defun ada-xref-search-nearest (name)
2057 "Search for NAME nearest to the position recorded in the Xref file.
2058 Return the position of the declaration in the buffer, or nil if not found."
2059 (let ((orgpos (point))
2063 (goto-char (point-max))
2065 ;; loop - look for all declarations of name in this file
2066 (while (search-backward name nil t)
2068 ;; check if it really is a complete Ada identifier
2070 (not (save-excursion
2071 (goto-char (match-end 0))
2073 (not (ada-in-string-or-comment-p))
2075 ;; variable declaration ?
2077 (skip-chars-forward "a-zA-Z_0-9" )
2078 (ada-goto-next-non-ws)
2079 (looking-at ":[^=]"))
2080 ;; procedure, function, task or package declaration ?
2082 (ada-goto-previous-word)
2083 (looking-at "\\<[pP][rR][oO][cC][eE][dD][uU][rR][eE]\\>\\|\\<[fF][uU][nN][cC][tT][iI][oO][nN]\\>\\|\\<[tT][yY][pP][eE]\\>\\|\\<[tT][aA][sS][kK]\\>\\|\\<[pP][aA][cC][kK][aA][gG][eE]\\>\\|\\<[bB][oO][dD][yY]\\>"))))
2085 ;; check if it is nearer than the ones before if any
2087 (< (abs (- (point) orgpos)) diff))
2089 (setq newpos (point)
2090 diff (abs (- newpos orgpos))))))
2095 (message "ATTENTION: this declaration is only a (good) guess ...")
2100 ;; Find the parent library file of the current file
2101 (defun ada-goto-parent ()
2102 "Go to the parent library file."
2104 (ada-require-project-file)
2106 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2112 (goto-char (point-min))
2113 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2114 (setq unit-name (match-string 1))
2115 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
2117 (kill-buffer buffer)
2118 (error "No parent unit !"))
2119 (setq unit-name (match-string 1 unit-name))
2122 ;; look for the file name for the parent unit specification
2123 (goto-char (point-min))
2124 (re-search-forward (concat "^W " unit-name
2125 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2127 (setq body-name (match-string 1))
2128 (setq ali-name (match-string 2))
2129 (kill-buffer buffer)
2132 (setq ali-name (ada-find-ali-file-in-dir ali-name))
2135 ;; Tries to open the new ali file to find the spec file
2138 (find-file ali-name)
2139 (goto-char (point-min))
2140 (re-search-forward (concat "^U " unit-name "%s[ \t]+"
2142 (setq body-name (match-string 1))
2143 (kill-buffer (current-buffer))
2148 (find-file body-name)
2151 (defun ada-make-filename-from-adaname (adaname)
2152 "Determine the filename in which ADANAME is found.
2153 This is a GNAT specific function that uses gnatkrunch."
2155 (setq krunch-buf (generate-new-buffer "*gkrunch*"))
2157 (set-buffer krunch-buf)
2158 ;; send adaname to external process `gnatkr'.
2159 ;; Add a dummy extension, since gnatkr versions have two different
2160 ;; behaviors depending on the version:
2161 ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc
2162 ;; After: "AA.BB.CC" => aa-bb.cc
2163 (call-process "gnatkr" nil krunch-buf nil
2164 (concat adaname ".adb") ada-krunch-args)
2165 ;; fetch output of that process
2166 (setq adaname (buffer-substring
2169 (goto-char (point-min))
2172 ;; Remove the extra extension we added above
2173 (setq adaname (substring adaname 0 -4))
2175 (kill-buffer krunch-buf)))
2179 (defun ada-make-body-gnatstub (&optional interactive)
2180 "Create an Ada package body in the current buffer.
2181 This function uses the `gnatstub' program to create the body.
2182 This function typically is to be hooked into `ff-file-created-hooks'."
2185 (save-some-buffers nil nil)
2187 ;; If the current buffer is the body (as is the case when calling this
2188 ;; function from ff-file-created-hooks), then kill this temporary buffer
2191 (set-buffer-modified-p nil)
2192 (kill-buffer (current-buffer))))
2195 ;; Make sure the current buffer is the spec (this might not be the case
2196 ;; if for instance the user was asked for a project file)
2198 (unless (buffer-file-name (car (buffer-list)))
2199 (set-buffer (cadr (buffer-list))))
2201 ;; Make sure we have a project file (for parameters to gnatstub). Note that
2202 ;; this might have already been done if we have been called from the hook,
2203 ;; but this is not an expensive call)
2204 (ada-require-project-file)
2206 ;; Call the external process gnatstub
2207 (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
2208 (filename (buffer-file-name (car (buffer-list))))
2209 (output (concat (file-name-sans-extension filename) ".adb"))
2210 (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename))
2211 (buffer (get-buffer-create "*gnatstub*")))
2215 (compilation-minor-mode 1)
2217 (insert gnatstub-cmd)
2220 ;; call gnatstub to create the body file
2221 (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
2225 (goto-char (point-min))
2226 (search-forward "command not found" nil t))
2228 (message "gnatstub was not found -- using the basic algorithm")
2230 (kill-buffer buffer)
2233 ;; Else clean up the output
2235 (if (file-exists-p output)
2238 (kill-buffer buffer))
2240 ;; display the error buffer
2241 (display-buffer buffer)
2245 (defun ada-xref-initialize ()
2246 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2247 For instance, it creates the gnat-specific menus, sets some hooks for
2249 ;; This should really be an `add-hook'. -stef
2250 (setq ff-file-created-hook 'ada-make-body-gnatstub)
2252 ;; Completion for file names in the mini buffer should ignore .ali files
2253 (add-to-list 'completion-ignored-extensions ".ali")
2255 (ada-xref-update-project-menu)
2258 ;; ----- Add to ada-mode-hook ---------------------------------------------
2260 ;; This must be done before initializing the Ada menu.
2261 (add-hook 'ada-mode-hook 'ada-xref-initialize)
2263 ;; Define a new error type
2264 (put 'error-file-not-found
2266 '(error ada-mode-errors error-file-not-found))
2267 (put 'error-file-not-found
2269 "File not found in src-dir (check project file): ")
2271 ;; Initializes the cross references to the runtime library
2272 (ada-initialize-runtime-library "")
2274 ;; Add these standard directories to the search path
2275 (set 'ada-search-directories-internal
2276 (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path)
2277 ada-search-directories))
2281 ;;; arch-tag: 415a39fe-577b-4676-b3b1-6ff6db7ca24e
2282 ;;; ada-xref.el ends here