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