1 ;;; ada-xref.el --- for lookup and completion in Ada mode
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2001, 2002
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.155.2.8 (GNAT 3.15)
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)
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.
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.
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
43 ;; ----- Requirements -----------------------------------------------------
48 ;; ------ Use variables
49 (defcustom ada-xref-other-buffer t
50 "*If nil, always display the cross-references in the same buffer.
51 Otherwise create either a new buffer or a new frame."
52 :type 'boolean :group 'ada)
54 (defcustom ada-xref-create-ali nil
55 "*If non-nil, run gcc whenever the cross-references are not up-to-date.
56 If nil, the cross-reference mode will never run gcc."
57 :type 'boolean :group 'ada)
59 (defcustom ada-xref-confirm-compile nil
60 "*If non-nil, always ask for user confirmation before compiling or running
62 :type 'boolean :group 'ada)
64 (defcustom ada-krunch-args "0"
65 "*Maximum number of characters for filenames created by gnatkr.
66 Set to 0, if you don't use crunched filenames. This should be a string."
67 :type 'string :group 'ada)
69 (defcustom ada-prj-default-comp-opt "-gnatq -gnatQ"
70 "Default compilation options."
71 :type 'string :group 'ada)
73 (defcustom ada-prj-default-bind-opt ""
74 "Default binder options."
75 :type 'string :group 'ada)
77 (defcustom ada-prj-default-link-opt ""
78 "Default linker options."
79 :type 'string :group 'ada)
81 (defcustom ada-prj-default-gnatmake-opt "-g"
82 "Default options for gnatmake."
83 :type 'string :group 'ada)
85 (defcustom ada-prj-gnatfind-switches "-rf"
86 "Default switches to use for gnatfind.
87 You should modify this variable, for instance to add -a, if you are working
88 in an environment where most ALI files are write-protected.
89 The command gnatfind is used every time you choose the menu
90 \"Show all references\"."
91 :type 'string :group 'ada)
93 (defcustom ada-prj-default-comp-cmd
94 (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
96 "*Default command to be used to compile a single file.
97 Emacs will add the filename at the end of this command. This is the same
98 syntax as in the project file."
99 :type 'string :group 'ada)
101 (defcustom ada-prj-default-debugger "${cross_prefix}gdb"
102 "*Default name of the debugger. We recommend either `gdb',
103 `gdb --emacs_gdbtk' or `ddd --tty -fullname'."
104 :type 'string :group 'ada)
106 (defcustom ada-prj-default-make-cmd
107 (concat "${cross_prefix}gnatmake -o ${main} ${main_unit} ${gnatmake_opt} "
108 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
109 "*Default command to be used to compile the application.
110 This is the same syntax as in the project file."
111 :type 'string :group 'ada)
113 (defcustom ada-prj-default-project-file ""
114 "*Name of the project file to use for every Ada file.
115 Emacs will not try to use the standard algorithm to find the project file if
116 this string is not empty."
117 :type '(file :must-match t) :group 'ada)
119 (defcustom ada-gnatstub-opts "-q -I${src_dir}"
120 "*List of the options to pass to gnatsub to generate the body of a package.
121 This has the same syntax as in the project file (with variable substitution)."
122 :type 'string :group 'ada)
124 (defcustom ada-always-ask-project nil
125 "*If nil, use default values when no project file was found.
126 Otherwise, ask the user for the name of the project file to use."
127 :type 'boolean :group 'ada)
129 (defconst is-windows (memq system-type (quote (windows-nt)))
130 "True if we are running on windows NT or windows 95.")
132 (defcustom ada-tight-gvd-integration nil
133 "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
134 If GVD is not the debugger used, nothing happens.")
136 (defcustom ada-xref-search-with-egrep t
137 "*If non-nil, use egrep to find the possible declarations for an entity.
138 This alternate method is used when the exact location was not found in the
139 information provided by GNAT. However, it might be expensive if you have a lot
140 of sources, since it will search in all the files in your project."
141 :type 'boolean :group 'ada)
143 (defvar ada-load-project-hook nil
144 "Hook that is run when loading a project file.
145 Each function in this hook takes one argument FILENAME, that is the name of
146 the project file to load.
147 This hook should be used to support new formats for the project files.
149 If the function can load the file with the given filename, it should create a
150 buffer that contains a conversion of the file to the standard format of the
151 project files, and return that buffer. (the usual \"src_dir=\" or \"obj_dir=\"
152 lines). It should return nil if it doesn't know how to convert that project
156 ;; ------- Nothing to be modified by the user below this
157 (defvar ada-last-prj-file ""
158 "Name of the last project file entered by the user.")
160 (defvar ada-check-switch "-gnats"
161 "Switch added to the command line to check the current file.")
163 (defconst ada-project-file-extension ".adp"
164 "The extension used for project files.")
166 (defvar ada-xref-runtime-library-specs-path '()
167 "Directories where the specs for the standard library is found.
168 This is used for cross-references.")
170 (defvar ada-xref-runtime-library-ali-path '()
171 "Directories where the ali for the standard library is found.
172 This is used for cross-references.")
174 (defvar ada-xref-pos-ring '()
175 "List of positions selected by the cross-references functions.
176 Used to go back to these positions.")
178 (defvar ada-cd-command
179 (if (string-match "cmdproxy.exe" shell-file-name)
182 "Command to use to change to a specific directory. On windows systems
183 using cmdproxy.exe as the shell, we need to use /d or the drive is never
186 (defvar ada-command-separator (if is-windows " && " "\n")
187 "Separator to use when sending multiple commands to `compile' or
189 cmdproxy.exe doesn't recognize multiple-line commands, so we have to use
192 (defconst ada-xref-pos-ring-max 16
193 "Number of positions kept in the list ada-xref-pos-ring.")
195 (defvar ada-operator-re
196 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
197 "Regexp to match for operators.")
199 (defvar ada-xref-project-files '()
200 "Associative list of project files.
201 It has the following format:
202 \((project_name . value) (project_name . value) ...)
203 As always, the values of the project file are defined through properties.")
205 (defun ada-quote-cmd (cmd)
206 "Duplicates all \\ characters in CMD so that it can be passed to `compile'"
207 (mapconcat 'identity (split-string cmd "\\\\") "\\\\"))
209 (defun ada-initialize-runtime-library (cross-prefix)
210 "Initializes the variables for the runtime library location.
211 CROSS-PREFIX is the prefix to use for the gnatls command"
213 (setq ada-xref-runtime-library-specs-path '()
214 ada-xref-runtime-library-ali-path '())
215 (set-buffer (get-buffer-create "*gnatls*"))
218 ;; Catch any error in the following form (i.e gnatls was not found)
220 ;; Even if we get an error, delete the *gnatls* buffer
223 (call-process (concat cross-prefix "gnatls")
225 (goto-char (point-min))
229 (search-forward "Source Search Path:")
231 (while (not (looking-at "^$"))
232 (back-to-indentation)
233 (unless (looking-at "<Current_Directory>")
234 (add-to-list 'ada-xref-runtime-library-specs-path
235 (buffer-substring-no-properties
237 (save-excursion (end-of-line) (point)))))
242 (search-forward "Object Search Path:")
244 (while (not (looking-at "^$"))
245 (back-to-indentation)
246 (unless (looking-at "<Current_Directory>")
247 (add-to-list 'ada-xref-runtime-library-ali-path
248 (buffer-substring-no-properties
250 (save-excursion (end-of-line) (point)))))
255 (set 'ada-xref-runtime-library-specs-path
256 (reverse ada-xref-runtime-library-specs-path))
257 (set 'ada-xref-runtime-library-ali-path
258 (reverse ada-xref-runtime-library-ali-path))
262 (defun ada-treat-cmd-string (cmd-string)
263 "Replace meta-sequences like ${...} in CMD-STRING with the appropriate value.
264 The project file must have been loaded first.
265 As a special case, ${current} is replaced with the name of the currently
266 edited file, minus extension but with directory, and ${full_current} is
267 replaced by the name including the extension."
269 (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string)
271 (name (match-string 2 cmd-string)))
273 ((string= name "current")
274 (setq value (file-name-sans-extension (buffer-file-name))))
275 ((string= name "full_current")
276 (setq value (buffer-file-name)))
279 (setq value (ada-xref-get-project-field (intern name))))))
281 ;; Check if there is an environment variable with the same name
283 (if (not (setq value (getenv name)))
284 (message (concat "No environment variable " name " found"))))
288 (setq cmd-string (replace-match "" t t cmd-string)))
290 (setq cmd-string (replace-match value t t cmd-string)))
292 (let ((prefix (match-string 1 cmd-string)))
293 (setq cmd-string (replace-match
294 (mapconcat (lambda(x) (concat prefix x)) value " ")
299 (defun ada-xref-set-default-prj-values (symbol ada-buffer)
300 "Reset the properties in SYMBOL to the default values for ADA-BUFFER."
302 (let ((file (buffer-file-name ada-buffer))
305 (set-buffer ada-buffer)
308 ;; Try hard to find a default value for filename, so that the user
309 ;; can edit his project file even if the current buffer is not an
310 ;; Ada file or not even associated with a file
311 (list 'filename (expand-file-name
313 (ada-prj-default-project-file
314 ada-prj-default-project-file)
316 (ada-prj-get-prj-dir file))
318 (message (concat "Not editing an Ada file,"
319 "and no default project "
322 'build_dir (file-name-as-directory (expand-file-name "."))
325 'casing (if (listp ada-case-exception-file)
326 ada-case-exception-file
327 (list ada-case-exception-file))
328 'comp_opt ada-prj-default-comp-opt
329 'bind_opt ada-prj-default-bind-opt
330 'link_opt ada-prj-default-link-opt
331 'gnatmake_opt ada-prj-default-gnatmake-opt
332 'gnatfind_opt ada-prj-gnatfind-switches
334 (file-name-nondirectory
335 (file-name-sans-extension file))
338 (file-name-nondirectory
339 (file-name-sans-extension file))
343 'comp_cmd (list (concat ada-cd-command " ${build_dir}")
344 ada-prj-default-comp-cmd)
345 'check_cmd (list (concat ada-prj-default-comp-cmd " "
347 'make_cmd (list (concat ada-cd-command " ${build_dir}")
348 ada-prj-default-make-cmd)
349 'run_cmd (list (concat ada-cd-command " ${build_dir}")
351 (if is-windows ".exe")))
352 'debug_pre_cmd (list (concat ada-cd-command
354 'debug_cmd (concat ada-prj-default-debugger
355 (if is-windows " ${main}.exe"
357 'debug_post_cmd (list nil)))
361 (defun ada-xref-get-project-field (field)
362 "Extract the value of FIELD from the current project file.
363 The project file must have been loaded first.
364 A default value is returned if the file was not found.
366 Note that for src_dir and obj_dir, you should rather use
367 `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field' which will in
368 addition return the default paths."
370 (let ((file-name ada-prj-default-project-file)
373 ;; Get the project file (either the current one, or a default one)
374 (setq file (or (assoc file-name ada-xref-project-files)
375 (assoc nil ada-xref-project-files)))
377 ;; If the file was not found, use the default values
379 ;; Get the value from the file
380 (set 'value (plist-get (cdr file) field))
382 ;; Create a default nil file that contains the default values
383 (ada-xref-set-default-prj-values 'value (current-buffer))
384 (add-to-list 'ada-xref-project-files (cons nil value))
385 (ada-xref-update-project-menu)
386 (set 'value (plist-get value field))
389 ;; Substitute the ${...} constructs in all the strings, including
393 (ada-treat-cmd-string value))
397 (mapcar (lambda(x) (if x (ada-treat-cmd-string x) x)) value))
404 (defun ada-xref-get-src-dir-field ()
405 "Return the full value for src_dir, including the default directories.
406 All the directories are returned as absolute directories."
408 (let ((build-dir (ada-xref-get-project-field 'build_dir)))
410 ;; Add ${build_dir} in front of the path
413 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
416 ;; Add the standard runtime at the end
417 ada-xref-runtime-library-specs-path)))
419 (defun ada-xref-get-obj-dir-field ()
420 "Return the full value for obj_dir, including the default directories.
421 All the directories are returned as absolute directories."
423 (let ((build-dir (ada-xref-get-project-field 'build_dir)))
425 ;; Add ${build_dir} in front of the path
428 (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir)
431 ;; Add the standard runtime at the end
432 ada-xref-runtime-library-ali-path)))
434 (defun ada-xref-update-project-menu ()
435 "Update the menu Ada->Project, with the list of available project files."
439 ;; Create the standard items
440 (set 'submenu (list (cons 'Load (cons "Load..."
441 'ada-set-default-project-file))
442 (cons 'New (cons "New..." 'ada-prj-new))
443 (cons 'Edit (cons "Edit..." 'ada-prj-edit))
444 (cons 'sep (cons "---" nil))))
449 (let ((name (or (car x) "<default>"))
451 "Change the active project file."
453 (ada-parse-prj-file ,(car x))
454 (set 'ada-prj-default-project-file ,(car x))
455 (ada-xref-update-project-menu))))
458 (list (cons (intern name)
461 (if (string= (file-name-extension name)
462 ada-project-file-extension)
463 (file-name-sans-extension
464 (file-name-nondirectory name))
465 (file-name-nondirectory name))
469 (equal ada-prj-default-project-file
473 ;; Parses all the known project files, and insert at least the default
474 ;; one (in case ada-xref-project-files is nil)
475 (or ada-xref-project-files '(nil)))
478 (if (lookup-key ada-mode-map [menu-bar Ada Project])
479 (setcdr (lookup-key ada-mode-map [menu-bar Ada Project])
484 ;;-------------------------------------------------------------
485 ;;-- Searching a file anywhere on the source path.
487 ;;-- The following functions provide support for finding a file anywhere
488 ;;-- on the source path, without providing an explicit directory.
489 ;;-- They also provide file name completion in the minibuffer.
491 ;;-- Public subprograms: ada-find-file
493 ;;-------------------------------------------------------------
495 (defun ada-do-file-completion (string predicate flag)
496 "Completion function when reading a file from the minibuffer.
497 Completion is attempted in all the directories in the source path, as
498 defined in the project file."
500 (dirs (ada-xref-get-src-dir-field)))
503 (if (file-directory-p (car dirs))
504 (set 'list (append list (file-name-all-completions string (car dirs)))))
505 (set 'dirs (cdr dirs)))
506 (cond ((equal flag 'lambda)
511 (try-completion string
512 (mapcar (lambda (x) (cons x 1)) list)
516 (defun ada-find-file (filename)
517 "Open a file anywhere in the source path.
518 Completion is available."
520 (list (completing-read "File: " 'ada-do-file-completion)))
521 (let ((file (ada-find-src-file-in-dir filename)))
524 (error (concat filename " not found in src_dir")))))
527 ;; ----- Keybindings ------------------------------------------------------
529 (defun ada-add-keymap ()
530 "Add new key bindings when using `ada-xrel.el'."
534 (define-key ada-mode-map '(shift button3) 'ada-point-and-xref)
535 (define-key ada-mode-map '(control tab) 'ada-complete-identifier))
536 (define-key ada-mode-map [C-tab] 'ada-complete-identifier)
537 (define-key ada-mode-map [S-mouse-3] 'ada-point-and-xref))
539 (define-key ada-mode-map "\C-co" 'ff-find-other-file)
540 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
541 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
542 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
543 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
544 (define-key ada-mode-map "\C-cc" 'ada-change-prj)
545 (define-key ada-mode-map "\C-cd" 'ada-set-default-project-file)
546 (define-key ada-mode-map "\C-cg" 'ada-gdb-application)
547 (define-key ada-mode-map "\C-cr" 'ada-run-application)
548 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
549 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
550 (define-key ada-mode-map "\C-cl" 'ada-find-local-references)
551 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
552 (define-key ada-mode-map "\C-cf" 'ada-find-file)
555 ;; ----- Menus --------------------------------------------------------------
556 (defun ada-add-ada-menu ()
557 "Add some items to the standard Ada mode menu.
558 The items are added to the menu called NAME, which should be the same
559 name as was passed to `ada-create-menu'."
562 (let* ((menu-list '("Ada"))
563 (goto-menu '("Ada" "Goto"))
564 (edit-menu '("Ada" "Edit"))
565 (help-menu '("Ada" "Help"))
566 (options-menu (list "Ada" "Options")))
567 (funcall (symbol-function 'add-menu-button)
568 menu-list ["Check file" ada-check-current
569 (string= mode-name "Ada")] "Goto")
570 (funcall (symbol-function 'add-menu-button)
571 menu-list ["Compile file" ada-compile-current
572 (string= mode-name "Ada")] "Goto")
573 (funcall (symbol-function 'add-menu-button)
574 menu-list ["Build" ada-compile-application t] "Goto")
575 (funcall (symbol-function 'add-menu-button)
576 menu-list ["Run" ada-run-application t] "Goto")
577 (funcall (symbol-function 'add-menu-button)
578 menu-list ["Debug" ada-gdb-application t] "Goto")
579 (funcall (symbol-function 'add-menu-button)
580 menu-list ["--" nil t] "Goto")
581 (funcall (symbol-function 'add-menu-button)
582 goto-menu ["Goto Parent Unit" ada-goto-parent t]
583 "Next compilation error")
584 (funcall (symbol-function 'add-menu-button)
585 goto-menu ["Goto References to any entity"
586 ada-find-any-references t]
587 "Next compilation error")
588 (funcall (symbol-function 'add-menu-button)
589 goto-menu ["List References" ada-find-references t]
590 "Next compilation error")
591 (funcall (symbol-function 'add-menu-button)
592 goto-menu ["List Local References" ada-find-local-references t]
593 "Next compilation error")
594 (funcall (symbol-function 'add-menu-button)
595 goto-menu ["Goto Declaration Other Frame"
596 ada-goto-declaration-other-frame t]
597 "Next compilation error")
598 (funcall (symbol-function 'add-menu-button)
599 goto-menu ["Goto Declaration/Body"
600 ada-goto-declaration t]
601 "Next compilation error")
602 (funcall (symbol-function 'add-menu-button)
603 goto-menu ["Goto Previous Reference"
604 ada-xref-goto-previous-reference t]
605 "Next compilation error")
606 (funcall (symbol-function 'add-menu-button)
607 goto-menu ["--" nil t] "Next compilation error")
608 (funcall (symbol-function 'add-menu-button)
609 edit-menu ["Complete Identifier"
610 ada-complete-identifier t]
612 (funcall (symbol-function 'add-menu-button)
613 edit-menu ["--------" nil t] "Indent Line")
614 (funcall (symbol-function 'add-menu-button)
615 help-menu ["Gnat User Guide" (info "gnat_ug")])
616 (funcall (symbol-function 'add-menu-button)
617 help-menu ["Gnat Reference Manual" (info "gnat_rm")])
618 (funcall (symbol-function 'add-menu-button)
619 help-menu ["Gcc Documentation" (info "gcc")])
620 (funcall (symbol-function 'add-menu-button)
621 help-menu ["Gdb Documentation" (info "gdb")])
622 (funcall (symbol-function 'add-menu-button)
623 help-menu ["Ada95 Reference Manual" (info "arm95")])
624 (funcall (symbol-function 'add-menu-button)
626 ["Show Cross-References in Other Buffer"
627 (setq ada-xref-other-buffer
628 (not ada-xref-other-buffer))
629 :style toggle :selected ada-xref-other-buffer])
630 (funcall (symbol-function 'add-menu-button)
632 ["Automatically Recompile for Cross-References"
633 (setq ada-xref-create-ali (not ada-xref-create-ali))
634 :style toggle :selected ada-xref-create-ali])
635 (funcall (symbol-function 'add-menu-button)
638 (setq ada-xref-confirm-compile
639 (not ada-xref-confirm-compile))
640 :style toggle :selected ada-xref-confirm-compile])
641 (if (string-match "gvd" ada-prj-default-debugger)
642 (funcall (symbol-function 'add-menu-button)
644 ["Tight Integration With Gnu Visual Debugger"
645 (setq ada-tight-gvd-integration
646 (not ada-tight-gvd-integration))
647 :style toggle :selected ada-tight-gvd-integration]))
651 (let* ((menu (or (lookup-key ada-mode-map [menu-bar Ada])
652 ;; Emacs-21.4's easymenu.el downcases the events.
653 (lookup-key ada-mode-map [menu-bar ada])))
654 (edit-menu (or (lookup-key menu [Edit]) (lookup-key menu [edit])))
655 (help-menu (or (lookup-key menu [Help]) (lookup-key menu [help])))
656 (goto-menu (or (lookup-key menu [Goto]) (lookup-key menu [goto])))
657 (options-menu (or (lookup-key menu [Options])
658 (lookup-key menu [options]))))
660 (define-key-after menu [Check] '("Check file" . ada-check-current)
662 (define-key-after menu [Compile] '("Compile file" . ada-compile-current)
664 (define-key-after menu [Build] '("Build" . ada-compile-application)
666 (define-key-after menu [Run] '("Run" . ada-run-application) 'Build)
667 (define-key-after menu [Debug] '("Debug" . ada-gdb-application) 'Run)
668 (define-key-after menu [rem] '("--" . nil) 'Debug)
669 (define-key-after menu [Project]
670 (cons "Project" (make-sparse-keymap)) 'rem)
672 (define-key help-menu [Gnat_ug]
673 '("Gnat User Guide" . (lambda() (interactive) (info "gnat_ug"))))
674 (define-key help-menu [Gnat_rm]
675 '("Gnat Reference Manual" . (lambda() (interactive) (info "gnat_rm"))))
676 (define-key help-menu [Gcc]
677 '("Gcc Documentation" . (lambda() (interactive) (info "gcc"))))
678 (define-key help-menu [gdb]
679 '("Gdb Documentation" . (lambda() (interactive) (info "gdb"))))
680 (define-key help-menu [arm95]
681 '("Ada95 Reference Manual" . (lambda() (interactive) (info "arm95"))))
683 (define-key goto-menu [rem] '("----" . nil))
684 (define-key goto-menu [Parent] '("Goto Parent Unit"
686 (define-key goto-menu [References-any]
687 '("Goto References to any entity" . ada-find-any-references))
688 (define-key goto-menu [References]
689 '("List References" . ada-find-references))
690 (define-key goto-menu [Local-References]
691 '("List Local References" . ada-find-local-references))
692 (define-key goto-menu [Prev]
693 '("Goto Previous Reference" . ada-xref-goto-previous-reference))
694 (define-key goto-menu [Decl-other]
695 '("Goto Declaration Other Frame" . ada-goto-declaration-other-frame))
696 (define-key goto-menu [Decl]
697 '("Goto Declaration/Body" . ada-goto-declaration))
699 (define-key edit-menu [rem] '("----" . nil))
700 (define-key edit-menu [Complete] '("Complete Identifier"
701 . ada-complete-identifier))
703 (define-key-after options-menu [xrefrecompile]
704 '(menu-item "Automatically Recompile for Cross-References"
705 (lambda()(interactive)
706 (setq ada-xref-create-ali (not ada-xref-create-ali)))
707 :button (:toggle . ada-xref-create-ali)) t)
708 (define-key-after options-menu [xrefconfirm]
709 '(menu-item "Confirm Commands"
710 (lambda()(interactive)
711 (setq ada-xref-confirm-compile
712 (not ada-xref-confirm-compile)))
713 :button (:toggle . ada-xref-confirm-compile)) t)
714 (define-key-after options-menu [xrefother]
715 '(menu-item "Show Cross-References in Other Buffer"
716 (lambda()(interactive)
717 (setq ada-xref-other-buffer (not ada-xref-other-buffer)))
718 :button (:toggle . ada-xref-other-buffer)) t)
720 (if (string-match "gvd" ada-prj-default-debugger)
721 (define-key-after options-menu [tightgvd]
722 '(menu-item "Tight Integration With Gnu Visual Debugger"
723 (lambda()(interactive)
724 (setq ada-tight-gvd-integration
725 (not ada-tight-gvd-integration)))
726 :button (:toggle . ada-tight-gvd-integration)) t))
728 (define-key ada-mode-map [menu-bar Ada Edit rem3] '("------------" . nil))
729 (define-key ada-mode-map [menu-bar Ada Edit open-file-from-src-path]
730 '("Search File on source path..." . ada-find-file))
733 (ada-xref-update-project-menu)
736 ;; ----- Utilities -------------------------------------------------
738 (defun ada-require-project-file ()
739 "If no project file is currently active, load a default one."
740 (if (or (not ada-prj-default-project-file)
741 (not ada-xref-project-files)
742 (string= ada-prj-default-project-file ""))
743 (ada-reread-prj-file)))
745 (defun ada-xref-push-pos (filename position)
746 "Push (FILENAME, POSITION) on the position ring for cross-references."
747 (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
748 (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
749 (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
751 (defun ada-xref-goto-previous-reference ()
752 "Go to the previous cross-reference we were on."
754 (if ada-xref-pos-ring
755 (let ((pos (car ada-xref-pos-ring)))
756 (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
757 (find-file (car (cdr pos)))
758 (goto-char (car pos)))))
760 (defun ada-convert-file-name (name)
761 "Converts from NAME to a name that can be used by the compilation commands.
762 This is overriden on VMS to convert from VMS filenames to Unix filenames."
765 (defun ada-set-default-project-file (name)
766 "Set the file whose name is NAME as the default project file."
767 (interactive "fProject file:")
768 (setq ada-prj-default-project-file name)
769 (ada-reread-prj-file name)
772 ;; ------ Handling the project file -----------------------------
774 (defun ada-prj-find-prj-file (&optional no-user-question)
775 "Find the prj file associated with the current buffer.
776 If NO-USER-QUESTION is non-nil, use a default file if not project file was
777 found, and do not ask the user.
778 If the buffer is not an Ada buffer, associate it with the default project
779 file. If none is set, return nil."
783 ;; Use the active project file if there is one.
784 ;; This is also valid if we don't currently have an Ada buffer, or if
785 ;; the current buffer is not a real file (for instance an emerge buffer)
787 (if (or (not (string= mode-name "Ada"))
788 (not (buffer-file-name))
789 (and ada-prj-default-project-file
790 (not (string= ada-prj-default-project-file ""))))
791 (set 'selected ada-prj-default-project-file)
793 ;; other cases: use a more complex algorithm
795 (let* ((current-file (buffer-file-name))
796 (first-choice (concat
797 (file-name-sans-extension current-file)
798 ada-project-file-extension))
799 (dir (file-name-directory current-file))
801 ;; on Emacs 20.2, directory-files does not work if
802 ;; parse-sexp-lookup-properties is set
803 (parse-sexp-lookup-properties nil)
804 (prj-files (directory-files
806 (concat ".*" (regexp-quote
807 ada-project-file-extension) "$")))
812 ;; Else if there is a project file with the same name as the Ada
813 ;; file, but not the same extension.
814 ((file-exists-p first-choice)
815 (set 'selected first-choice))
817 ;; Else if only one project file was found in the current directory
818 ((= (length prj-files) 1)
819 (set 'selected (car prj-files)))
821 ;; Else if there are multiple files, ask the user
822 ((and (> (length prj-files) 1) (not no-user-question))
823 (save-window-excursion
824 (with-output-to-temp-buffer "*choice list*"
825 (princ "There are more than one possible project file.\n")
826 (princ "Which one should we use ?\n\n")
827 (princ " no. file name \n")
828 (princ " --- ------------------------\n")
830 (while (<= counter (length prj-files))
831 (princ (format " %2d) %s\n"
833 (nth (1- counter) prj-files)))
834 (setq counter (1+ counter))
835 ))) ; end of with-output-to ...
839 (not (integerp choice))
841 (> choice (length prj-files)))
842 (setq choice (string-to-int
843 (read-from-minibuffer "Enter No. of your choice: "))))
844 (set 'selected (nth (1- choice) prj-files))))
846 ;; Else if no project file was found in the directory, ask a name
847 ;; to the user, using as a default value the last one entered by
849 ((= (length prj-files) 0)
850 (unless (or no-user-question (not ada-always-ask-project))
851 (setq ada-last-prj-file
853 (concat "project file [" ada-last-prj-file "]:")
854 nil ada-last-prj-file))
855 (unless (string= ada-last-prj-file "")
856 (set 'selected ada-last-prj-file))))
862 (defun ada-parse-prj-file (prj-file)
863 "Reads and parses the PRJ-FILE file if it was found.
864 The current buffer should be the ada-file buffer."
866 (let (project src_dir obj_dir make_cmd comp_cmd check_cmd casing
867 run_cmd debug_pre_cmd debug_post_cmd
868 (ada-buffer (current-buffer)))
869 (setq prj-file (expand-file-name prj-file))
871 ;; Initialize the project with the default values
872 (ada-xref-set-default-prj-values 'project (current-buffer))
874 ;; Do not use find-file below, since we don't want to show this
875 ;; buffer. If the file is open through speedbar, we can't use
876 ;; find-file anyway, since the speedbar frame is special and does not
877 ;; allow the selection of a file in it.
879 (let* ((buffer (run-hook-with-args-until-success
880 'ada-load-project-hook prj-file)))
882 (setq buffer (find-file-noselect prj-file nil)))
886 (goto-char (point-min))
888 ;; Now overrides these values with the project file
890 (if (looking-at "^\\([^=]+\\)=\\(.*\\)")
892 ((string= (match-string 1) "src_dir")
893 (add-to-list 'src_dir
894 (file-name-as-directory (match-string 2))))
895 ((string= (match-string 1) "obj_dir")
896 (add-to-list 'obj_dir
897 (file-name-as-directory (match-string 2))))
898 ((string= (match-string 1) "casing")
899 (set 'casing (cons (match-string 2) casing)))
900 ((string= (match-string 1) "build_dir")
902 (plist-put project 'build_dir
903 (file-name-as-directory (match-string 2)))))
904 ((string= (match-string 1) "make_cmd")
905 (add-to-list 'make_cmd (match-string 2)))
906 ((string= (match-string 1) "comp_cmd")
907 (add-to-list 'comp_cmd (match-string 2)))
908 ((string= (match-string 1) "check_cmd")
909 (add-to-list 'check_cmd (match-string 2)))
910 ((string= (match-string 1) "run_cmd")
911 (add-to-list 'run_cmd (match-string 2)))
912 ((string= (match-string 1) "debug_pre_cmd")
913 (add-to-list 'debug_pre_cmd (match-string 2)))
914 ((string= (match-string 1) "debug_post_cmd")
915 (add-to-list 'debug_post_cmd (match-string 2)))
917 (set 'project (plist-put project (intern (match-string 1))
918 (match-string 2))))))
921 (if src_dir (set 'project (plist-put project 'src_dir
923 (if obj_dir (set 'project (plist-put project 'obj_dir
925 (if casing (set 'project (plist-put project 'casing
927 (if make_cmd (set 'project (plist-put project 'make_cmd
928 (reverse make_cmd))))
929 (if comp_cmd (set 'project (plist-put project 'comp_cmd
930 (reverse comp_cmd))))
931 (if check_cmd (set 'project (plist-put project 'check_cmd
932 (reverse check_cmd))))
933 (if run_cmd (set 'project (plist-put project 'run_cmd
935 (set 'project (plist-put project 'debug_post_cmd
936 (reverse debug_post_cmd)))
937 (set 'project (plist-put project 'debug_pre_cmd
938 (reverse debug_pre_cmd)))
940 ;; Delete the default project file from the list, if it is there.
941 ;; Note that in that case, this default project is the only one in
943 (if (assoc nil ada-xref-project-files)
944 (setq ada-xref-project-files nil))
946 ;; Memorize the newly read project file
947 (if (assoc prj-file ada-xref-project-files)
948 (setcdr (assoc prj-file ada-xref-project-files) project)
949 (add-to-list 'ada-xref-project-files (cons prj-file project)))
951 ;; Set the project file as the active one.
952 (setq ada-prj-default-project-file prj-file)
954 ;; Sets up the compilation-search-path so that Emacs is able to
955 ;; go to the source of the errors in a compilation buffer
956 (setq compilation-search-path (ada-xref-get-src-dir-field))
958 ;; Set the casing exceptions file list
961 (setq ada-case-exception-file (reverse casing))
962 (ada-case-read-exceptions)))
964 ;; Add the directories to the search path for ff-find-other-file
965 ;; Do not add the '/' or '\' at the end
966 (setq ada-search-directories
967 (append (mapcar 'directory-file-name compilation-search-path)
968 ada-search-directories))
970 ;; Kill the project buffer
972 (set-buffer ada-buffer)
974 (ada-xref-update-project-menu)
977 ;; No prj file ? => Setup default values
978 ;; Note that nil means that all compilation modes will first look in the
979 ;; current directory, and only then in the current file's directory. This
980 ;; current file is assumed at this point to be in the common source
982 (setq compilation-search-path (list nil default-directory))
986 (defun ada-find-references (&optional pos arg local-only)
987 "Find all references to the entity under POS.
988 Calls gnatfind to find the references.
989 if ARG is t, the contents of the old *gnatfind* buffer is preserved.
990 if LOCAL-ONLY is t, only the declarations in the current file are returned."
993 (ada-require-project-file)
995 (let* ((identlist (ada-read-identifier pos))
996 (alifile (ada-get-ali-file-name (ada-file-of identlist)))
997 (process-environment (ada-set-environment)))
999 (set-buffer (get-file-buffer (ada-file-of identlist)))
1001 ;; if the file is more recent than the executable
1002 (if (or (buffer-modified-p (current-buffer))
1003 (file-newer-than-file-p (ada-file-of identlist) alifile))
1004 (ada-find-any-references (ada-name-of identlist)
1005 (ada-file-of identlist)
1006 nil nil local-only arg)
1007 (ada-find-any-references (ada-name-of identlist)
1008 (ada-file-of identlist)
1009 (ada-line-of identlist)
1010 (ada-column-of identlist) local-only arg)))
1013 (defun ada-find-local-references (&optional pos arg)
1014 "Find all references to the entity under POS.
1015 Calls gnatfind to find the references.
1016 if ARG is t, the contents of the old *gnatfind* buffer is preserved."
1019 (ada-find-references pos arg t))
1021 (defun ada-find-any-references
1022 (entity &optional file line column local-only append)
1023 "Search for references to any entity whose name is ENTITY.
1024 ENTITY was first found the location given by FILE, LINE and COLUMN.
1025 If LOCAL-ONLY is t, then only the references in file will be listed, which
1027 If APPEND is t, then the output of the command will be append to the existing
1028 buffer *gnatfind* if it exists."
1029 (interactive "sEntity name: ")
1030 (ada-require-project-file)
1032 ;; Prepare the gnatfind command. Note that we must protect the quotes
1033 ;; around operators, so that they are correctly handled and can be
1034 ;; processed (gnatfind \"+\":...).
1035 (let* ((quote-entity
1036 (if (= (aref entity 0) ?\")
1038 (concat "\\\"" (substring entity 1 -1) "\\\"")
1039 (concat "'\"" (substring entity 1 -1) "\"'"))
1041 (switches (ada-xref-get-project-field 'gnatfind_opt))
1042 (command (concat "gnatfind " switches " "
1044 (if file (concat ":" (file-name-nondirectory file)))
1045 (if line (concat ":" line))
1046 (if column (concat ":" column))
1047 (if local-only (concat " " (file-name-nondirectory file)))
1051 ;; If a project file is defined, use it
1052 (if (and ada-prj-default-project-file
1053 (not (string= ada-prj-default-project-file "")))
1054 (setq command (concat command " -p" ada-prj-default-project-file)))
1056 (if (and append (get-buffer "*gnatfind*"))
1058 (set-buffer "*gnatfind*")
1059 (setq old-contents (buffer-string))))
1061 (compile-internal command "No more references" "gnatfind")
1063 ;; Hide the "Compilation" menu
1065 (set-buffer "*gnatfind*")
1066 (local-unset-key [menu-bar compilation-menu])
1071 (insert old-contents)
1072 (goto-char (point-max)))))
1076 (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
1078 ;; ----- Identlist manipulation -------------------------------------------
1079 ;; An identlist is a vector that is used internally to reference an identifier
1080 ;; To facilitate its use, we provide the following macros
1082 (defmacro ada-make-identlist () (make-vector 8 nil))
1083 (defmacro ada-name-of (identlist) (list 'aref identlist 0))
1084 (defmacro ada-line-of (identlist) (list 'aref identlist 1))
1085 (defmacro ada-column-of (identlist) (list 'aref identlist 2))
1086 (defmacro ada-file-of (identlist) (list 'aref identlist 3))
1087 (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
1088 (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
1089 (defmacro ada-references-of (identlist) (list 'aref identlist 6))
1090 (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
1092 (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
1093 (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
1094 (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
1095 (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
1096 (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
1097 (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
1098 (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
1099 (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
1101 (defsubst ada-get-ali-buffer (file)
1102 "Reads the ali file into a new buffer, and returns this buffer's name"
1103 (find-file-noselect (ada-get-ali-file-name file)))
1107 ;; ----- Identifier Completion --------------------------------------------
1108 (defun ada-complete-identifier (pos)
1109 "Tries to complete the identifier around POS.
1110 The feature is only available if the files where compiled not using the -gnatx
1113 (ada-require-project-file)
1115 ;; Initialize function-local variables and jump to the .ali buffer
1116 ;; Note that for regexp search is case insensitive too
1117 (let* ((curbuf (current-buffer))
1118 (identlist (ada-read-identifier pos))
1119 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
1120 (regexp-quote (ada-name-of identlist))
1121 "[a-zA-Z0-9_]*\\)"))
1125 ;; Open the .ali file
1126 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
1127 (goto-char (point-max))
1129 ;; build an alist of possible completions
1130 (while (re-search-backward sofar nil t)
1131 (setq symalist (cons (cons (match-string 1) nil) symalist)))
1133 (setq completed (try-completion "" symalist))
1135 ;; kills .ali buffer
1138 ;; deletes the incomplete identifier in the buffer
1140 (looking-at "[a-zA-Z0-9_]+")
1142 ;; inserts the completed symbol
1146 ;; ----- Cross-referencing ----------------------------------------
1148 (defun ada-point-and-xref ()
1149 "Calls `mouse-set-point' and then `ada-goto-declaration'."
1151 (mouse-set-point last-input-event)
1152 (ada-goto-declaration (point)))
1154 (defun ada-goto-declaration (pos &optional other-frame)
1155 "Display the declaration of the identifier around POS.
1156 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1158 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1160 (ada-require-project-file)
1162 (ada-xref-push-pos (buffer-file-name) pos)
1164 ;; First try the standard algorithm by looking into the .ali file, but if
1165 ;; that file was too old or even did not exist, try to look in the whole
1166 ;; object path for a possible location.
1167 (let ((identlist (ada-read-identifier pos)))
1169 (ada-find-in-ali identlist other-frame)
1171 (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist))))
1173 ;; If the ALI file was up-to-date, then we probably have a predefined
1174 ;; entity, whose references are not given by GNAT
1175 (if (and (file-exists-p ali-file)
1176 (file-newer-than-file-p ali-file (ada-file-of identlist)))
1177 (message "No cross-reference found. It might be a predefined entity.")
1179 ;; Else, look in every ALI file, except if the user doesn't want that
1180 (if ada-xref-search-with-egrep
1181 (ada-find-in-src-path identlist other-frame)
1182 (message "Cross-referencing information is not up-to-date. Please recompile.")
1185 (defun ada-goto-declaration-other-frame (pos &optional other-frame)
1186 "Display the declaration of the identifier around POS.
1187 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1189 (ada-goto-declaration pos t))
1191 (defun ada-remote (command)
1192 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1193 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1194 (if (or (not machine) (string= machine ""))
1196 (format "%s %s '(%s)'"
1197 remote-shell-program
1201 (defun ada-get-absolute-dir-list (dir-list root-dir)
1202 "Returns the list of absolute directories found in dir-list.
1203 If a directory is a relative directory, the value of ROOT-DIR is added in
1205 (mapcar (lambda (x) (expand-file-name x root-dir)) dir-list))
1207 (defun ada-set-environment ()
1208 "Return the new value for process-environment.
1209 It modifies the source path and object path with the values found in the
1211 (let ((include (getenv "ADA_INCLUDE_PATH"))
1212 (objects (getenv "ADA_OBJECTS_PATH"))
1213 (build-dir (ada-xref-get-project-field 'build_dir)))
1215 (set 'include (concat path-separator include)))
1217 (set 'objects (concat path-separator objects)))
1219 (concat "ADA_INCLUDE_PATH="
1220 (mapconcat (lambda(x) (expand-file-name x build-dir))
1221 (ada-xref-get-project-field 'src_dir)
1225 (concat "ADA_OBJECTS_PATH="
1226 (mapconcat (lambda(x) (expand-file-name x build-dir))
1227 (ada-xref-get-project-field 'obj_dir)
1230 process-environment))))
1232 (defun ada-compile-application (&optional arg)
1233 "Compiles the application, using the command found in the project file.
1234 If ARG is not nil, ask for user confirmation."
1236 (ada-require-project-file)
1237 (let ((cmd (ada-xref-get-project-field 'make_cmd))
1238 (process-environment (ada-set-environment))
1239 (compilation-scroll-output t))
1241 (setq compilation-search-path (ada-xref-get-src-dir-field))
1243 ;; If no project file was found, ask the user
1245 (setq cmd '("") arg t))
1247 ;; Make a single command from the list of commands, including the
1248 ;; commands to run it on a remote machine.
1249 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1251 (if (or ada-xref-confirm-compile arg)
1252 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1254 ;; Insert newlines so as to separate the name of the commands to run
1255 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1256 ;; which gets confused by newline characters.
1257 (if (not (string-match "cmdproxy.exe" shell-file-name))
1258 (setq cmd (concat cmd "\n\n")))
1260 (compile (ada-quote-cmd cmd))))
1262 (defun ada-compile-current (&optional arg prj-field)
1263 "Recompile the current file.
1264 If ARG is not nil, ask for user confirmation of the command.
1265 PRJ-FIELD is the name of the field to use in the project file to get the
1266 command, and should be either comp_cmd (default) or check_cmd."
1268 (ada-require-project-file)
1269 (let* ((field (if prj-field prj-field 'comp_cmd))
1270 (cmd (ada-xref-get-project-field field))
1271 (process-environment (ada-set-environment))
1272 (compilation-scroll-output t))
1274 (setq compilation-search-path (ada-xref-get-src-dir-field))
1277 (setq cmd '("") arg t))
1279 ;; Make a single command from the list of commands, including the
1280 ;; commands to run it on a remote machine.
1281 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1283 ;; If no project file was found, ask the user
1284 (if (or ada-xref-confirm-compile arg)
1285 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1287 ;; Insert newlines so as to separate the name of the commands to run
1288 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1289 ;; which gets confused by newline characters.
1290 (if (not (string-match "cmdproxy.exe" shell-file-name))
1291 (setq cmd (concat cmd "\n\n")))
1293 (compile (ada-quote-cmd cmd))))
1295 (defun ada-check-current (&optional arg)
1296 "Recompile the current file.
1297 If ARG is not nil, ask for user confirmation of the command."
1299 (ada-compile-current arg 'check_cmd))
1301 (defun ada-run-application (&optional arg)
1302 "Run the application.
1303 if ARG is not-nil, asks for user confirmation."
1305 (ada-require-project-file)
1307 (let ((machine (ada-xref-get-project-field 'cross_prefix)))
1308 (if (and machine (not (string= machine "")))
1309 (error "This feature is not supported yet for cross environments")))
1311 (let ((command (ada-xref-get-project-field 'run_cmd)))
1313 ;; Guess the command if it wasn't specified
1315 (set 'command (list (file-name-sans-extension (buffer-name)))))
1317 ;; Modify the command to run remotely
1318 (setq command (ada-remote (mapconcat 'identity command
1319 ada-command-separator)))
1321 ;; Ask for the arguments to the command if required
1322 (if (or ada-xref-confirm-compile arg)
1323 (setq command (read-from-minibuffer "Enter command to execute: "
1328 (set-buffer (get-buffer-create "*run*"))
1329 (set 'buffer-read-only nil)
1332 (start-process "run" (current-buffer) shell-file-name
1335 ;; Set these two variables to their default values, since otherwise
1336 ;; the output buffer is scrolled so that only the last output line
1337 ;; is visible at the top of the buffer.
1338 (set (make-local-variable 'scroll-step) 0)
1339 (set (make-local-variable 'scroll-conservatively) 0)
1341 (display-buffer "*run*")
1343 ;; change to buffer *run* for interactive programs
1345 (switch-to-buffer "*run*")
1348 (defun ada-gdb-application (&optional arg executable-name)
1349 "Start the debugger on the application.
1350 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1352 If ARG is non-nil, ask the user to confirm the command."
1354 (let ((buffer (current-buffer))
1355 cmd pre-cmd post-cmd)
1356 (ada-require-project-file)
1357 (setq cmd (if executable-name
1358 (concat ada-prj-default-debugger " " executable-name)
1359 (ada-xref-get-project-field 'debug_cmd))
1360 pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
1361 post-cmd (ada-xref-get-project-field 'debug_post_cmd))
1363 ;; If the command was not given in the project file, start a bare gdb
1365 (set 'cmd (concat ada-prj-default-debugger
1368 (file-name-sans-extension (buffer-file-name))))))
1370 ;; For gvd, add an extra switch so that the Emacs window is completly
1371 ;; swallowed inside the Gvd one
1372 (if (and ada-tight-gvd-integration
1373 (string-match "^[^ \t]*gvd" cmd))
1374 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1375 ;; We make sure that gvd swallows the new frame, not the one the
1376 ;; user has been using until now
1377 ;; The frame is made invisible initially, so that GtkPlug gets a
1378 ;; chance to fully manage it. Then it works fine with Enlightenment
1380 (let ((frame (make-frame '((visibility . nil)))))
1382 cmd " --editor-window="
1383 (cdr (assoc 'outer-window-id (frame-parameters frame)))))
1384 (select-frame frame)))
1386 ;; Add a -fullname switch
1387 ;; Use the remote machine
1388 (set 'cmd (ada-remote (concat cmd " -fullname ")))
1390 ;; Ask for confirmation if required
1391 (if (or arg ada-xref-confirm-compile)
1392 (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
1396 gud-gdb-massage-args)
1398 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1399 (fset 'gud-gdb-massage-args (lambda (file args) args))
1401 (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
1402 (if (not (equal pre-cmd ""))
1403 (setq pre-cmd (concat pre-cmd ada-command-separator)))
1405 (set 'post-cmd (mapconcat 'identity post-cmd "\n"))
1407 (set 'post-cmd (concat post-cmd "\n")))
1409 ;; Temporarily replaces the definition of `comint-exec' so that we
1410 ;; can execute commands before running gdb.
1412 `(lambda (buffer name command startfile switches)
1413 (let (compilation-buffer-name-function)
1415 (set 'compilation-buffer-name-function
1416 (lambda(x) (buffer-name buffer)))
1417 (compile (ada-quote-cmd
1420 (mapconcat 'identity switches " "))))))
1423 ;; Tight integration should force the tty mode
1424 (if (and (string-match "gvd" (comint-arguments cmd 0 0))
1425 ada-tight-gvd-integration
1426 (not (string-match "--tty" cmd)))
1427 (setq cmd (concat cmd "--tty")))
1429 (if (and (string-match "jdb" (comint-arguments cmd 0 0))
1431 (funcall (symbol-function 'jdb) cmd)
1434 ;; Send post-commands to the debugger
1435 (process-send-string (get-buffer-process (current-buffer)) post-cmd)
1437 ;; Move to the end of the debugger buffer, so that it is automatically
1438 ;; scrolled from then on.
1441 ;; Display both the source window and the debugger window (the former
1442 ;; above the latter). No need to show the debugger window unless it
1443 ;; is going to have some relevant information.
1444 (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
1445 (string-match "--tty" cmd))
1446 (split-window-vertically))
1447 (switch-to-buffer buffer)
1451 (defun ada-reread-prj-file (&optional filename)
1452 "Forces Emacs to read either FILENAME or the project file associated
1453 with the current buffer.
1454 Otherwise, this file is only read once, and never read again.
1455 Since the information in the project file is shared between all buffers, this
1456 automatically modifies the setup for all the Ada buffer that use this file."
1459 (ada-parse-prj-file filename)
1460 (ada-parse-prj-file (ada-prj-find-prj-file)))
1462 ;; Reread the location of the standard runtime library
1463 (ada-initialize-runtime-library
1464 (or (ada-xref-get-project-field 'cross-prefix) ""))
1467 ;; ------ Private routines
1469 (defun ada-xref-current (file &optional ali-file-name)
1470 "Update the cross-references for FILE.
1471 This in fact recompiles FILE to create ALI-FILE-NAME.
1472 This function returns the name of the file that was recompiled to generate
1473 the cross-reference information. Note that the ali file can then be deduced by
1474 replacing the file extension with .ali"
1476 (if (and ali-file-name
1477 (get-file-buffer ali-file-name))
1478 (kill-buffer (get-file-buffer ali-file-name)))
1480 (let* ((name (ada-convert-file-name file))
1481 (body-name (or (ada-get-body-name name) name)))
1483 ;; Always recompile the body when we can. We thus temporarily switch to a
1484 ;; buffer than contains the body of the unit
1486 (let ((body-visible (find-buffer-visiting body-name))
1489 (set-buffer body-visible)
1490 (find-file body-name))
1492 ;; Execute the compilation. Note that we must wait for the end of the
1493 ;; process, or the ALI file would still not be available.
1494 ;; Unfortunately, the underlying `compile' command that we use is
1496 (ada-compile-current)
1497 (setq process (get-buffer-process "*compilation*"))
1500 (not (equal (process-status process) 'exit)))
1503 ;; remove the buffer for the body if it wasn't there before
1504 (unless body-visible
1505 (kill-buffer (find-buffer-visiting body-name)))
1509 (defun ada-find-file-in-dir (file dir-list)
1510 "Search for FILE in DIR-LIST."
1512 (while (and (not found) dir-list)
1513 (set 'found (concat (file-name-as-directory (car dir-list))
1514 (file-name-nondirectory file)))
1516 (unless (file-exists-p found)
1518 (set 'dir-list (cdr dir-list)))
1521 (defun ada-find-ali-file-in-dir (file)
1522 "Find an .ali file in obj_dir. The current buffer must be the Ada file.
1523 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1524 and the standard runtime location at the end."
1525 (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
1527 (defun ada-find-src-file-in-dir (file)
1528 "Find a source file in src_dir. The current buffer must be the Ada file.
1529 Adds src_dir in front of the search path to conform to gnatmake's behavior,
1530 and the standard runtime location at the end."
1531 (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
1533 (defun ada-get-ali-file-name (file)
1534 "Create the ali file name for the ada-file FILE.
1535 The file is searched for in every directory shown in the obj_dir lines of
1538 ;; This function has to handle the special case of non-standard
1539 ;; file names (i.e. not .adb or .ads)
1540 ;; The trick is the following:
1541 ;; 1- replace the extension of the current file with .ali,
1542 ;; and look for this file
1543 ;; 2- If this file is found:
1544 ;; grep the "^U" lines, and make sure we are not reading the
1545 ;; .ali file for a spec file. If we are, go to step 3.
1546 ;; 3- If the file is not found or step 2 failed:
1547 ;; find the name of the "other file", ie the body, and look
1548 ;; for its associated .ali file by subtituing the extension
1550 ;; We must also handle the case of separate packages and subprograms:
1551 ;; 4- If no ali file was found, we try to modify the file name by removing
1552 ;; everything after the last '-' or '.' character, so as to get the
1553 ;; ali file for the parent unit. If we found an ali file, we check that
1554 ;; it indeed contains the definition for the separate entity by checking
1555 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1559 (set-buffer (get-file-buffer file))
1560 (let ((short-ali-file-name
1561 (concat (file-name-sans-extension (file-name-nondirectory file))
1566 ;; If we have a non-standard file name, and this is a spec, we first
1567 ;; look for the .ali file of the body, since this is the one that
1568 ;; contains the most complete information. If not found, we will do what
1569 ;; we can with the .ali file for the spec...
1571 (if (not (string= (file-name-extension file) "ads"))
1572 (let ((specs ada-spec-suffixes))
1574 (if (string-match (concat (regexp-quote (car specs)) "$")
1577 (set 'specs (cdr specs)))))
1581 (ada-find-ali-file-in-dir
1582 (concat (file-name-sans-extension
1583 (file-name-nondirectory
1584 (ada-other-file-name)))
1591 ;; Else we take the .ali file associated with the unit
1592 (ada-find-ali-file-in-dir short-ali-file-name)
1595 ;; else we did not find the .ali file Second chance: in case
1596 ;; the files do not have standard names (such as for instance
1597 ;; file_s.ada and file_b.ada), try to go to the other file
1598 ;; and look for its ali file
1599 (ada-find-ali-file-in-dir
1600 (concat (file-name-sans-extension
1601 (file-name-nondirectory (ada-other-file-name)))
1605 ;; If we still don't have an ali file, try to get the one
1606 ;; from the parent unit, in case we have a separate entity.
1607 (let ((parent-name (file-name-sans-extension
1608 (file-name-nondirectory file))))
1610 (while (and (not ali-file-name)
1611 (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
1613 (set 'parent-name (match-string 1 parent-name))
1614 (set 'ali-file-name (ada-find-ali-file-in-dir
1615 (concat parent-name ".ali")))
1619 ;; If still not found, try to recompile the file
1620 (if (not ali-file-name)
1621 ;; recompile only if the user asked for this. and search the ali
1622 ;; filename again. We avoid a possible infinite recursion by
1623 ;; temporarily disabling the automatic compilation.
1625 (if ada-xref-create-ali
1627 (concat (file-name-sans-extension (ada-xref-current file))
1630 (error "Ali file not found. Recompile your file"))
1633 ;; same if the .ali file is too old and we must recompile it
1634 (if (and (file-newer-than-file-p file ali-file-name)
1635 ada-xref-create-ali)
1636 (ada-xref-current file ali-file-name)))
1638 ;; Always return the correct absolute file name
1639 (expand-file-name ali-file-name))
1642 (defun ada-get-ada-file-name (file original-file)
1643 "Create the complete file name (+directory) for FILE.
1644 The original file (where the user was) is ORIGINAL-FILE. Search in project
1645 file for possible paths."
1649 ;; If the buffer for original-file, use it to get the values from the
1650 ;; project file, otherwise load the file and its project file
1651 (let ((buffer (get-file-buffer original-file)))
1654 (find-file original-file)
1655 (ada-require-project-file)))
1657 ;; we choose the first possible completion and we
1658 ;; return the absolute file name
1659 (let ((filename (ada-find-src-file-in-dir file)))
1661 (expand-file-name filename)
1663 (file-name-nondirectory file)
1664 " not found in src_dir. Please check your project file")))
1668 (defun ada-find-file-number-in-ali (file)
1669 "Returns the file number for FILE in the associated ali file."
1670 (set-buffer (ada-get-ali-buffer file))
1671 (goto-char (point-min))
1673 (let ((begin (re-search-forward "^D")))
1675 (re-search-forward (concat "^D " (file-name-nondirectory file)))
1676 (count-lines begin (point))))
1678 (defun ada-read-identifier (pos)
1679 "Returns the identlist around POS and switch to the .ali buffer.
1680 The returned list represents the entity, and can be manipulated through the
1681 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1683 ;; If at end of buffer (e.g the buffer is empty), error
1684 (if (>= (point) (point-max))
1685 (error "No identifier on point"))
1687 ;; goto first character of the identifier/operator (skip backward < and >
1688 ;; since they are part of multiple character operators
1690 (skip-chars-backward "a-zA-Z0-9_<>")
1692 ;; check if it really is an identifier
1693 (if (ada-in-comment-p)
1694 (error "Inside comment"))
1696 (let (identifier identlist)
1697 ;; Just in front of a string => we could have an operator declaration,
1698 ;; as in "+", "-", ..
1699 (if (= (char-after) ?\")
1702 ;; if looking at an operator
1703 ;; This is only true if:
1704 ;; - the symbol is +, -, ...
1705 ;; - the symbol is made of letters, and not followed by _ or a letter
1706 (if (and (looking-at ada-operator-re)
1707 (or (not (= (char-syntax (char-after)) ?w))
1708 (not (or (= (char-syntax (char-after (match-end 0))) ?w)
1709 (= (char-after (match-end 0)) ?_)))))
1711 (if (and (= (char-before) ?\")
1712 (= (char-after (+ (length (match-string 0)) (point))) ?\"))
1714 (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
1716 (if (ada-in-string-p)
1717 (error "Inside string or character constant"))
1718 (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
1719 (error "No cross-reference available for reserved keyword"))
1720 (if (looking-at "[a-zA-Z0-9_]+")
1721 (set 'identifier (match-string 0))
1722 (error "No identifier around")))
1724 ;; Build the identlist
1725 (set 'identlist (ada-make-identlist))
1726 (ada-set-name identlist (downcase identifier))
1727 (ada-set-line identlist
1728 (number-to-string (count-lines 1 (point))))
1729 (ada-set-column identlist
1730 (number-to-string (1+ (current-column))))
1731 (ada-set-file identlist (buffer-file-name))
1735 (defun ada-get-all-references (identlist)
1736 "Completes and returns IDENTLIST with the information extracted
1737 from the ali file (definition file and places where it is referenced)."
1739 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
1741 (set-buffer ali-buffer)
1742 (goto-char (point-min))
1743 (ada-set-on-declaration identlist nil)
1745 ;; First attempt: we might already be on the declaration of the identifier
1746 ;; We want to look for the declaration only in a definite interval (after
1747 ;; the "^X ..." line for the current file, and before the next "^X" line
1749 (if (re-search-forward
1750 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
1752 (let ((bound (save-excursion (re-search-forward "^X " nil t))))
1753 (set 'declaration-found
1755 (concat "^" (ada-line-of identlist)
1756 "." (ada-column-of identlist)
1757 "[ *]" (ada-name-of identlist)
1758 "[{\(<= ]?\\(.*\\)$") bound t))
1759 (if declaration-found
1760 (ada-set-on-declaration identlist t))
1763 ;; If declaration is still nil, then we were not on a declaration, and
1764 ;; have to fall back on other algorithms
1766 (unless declaration-found
1768 ;; Since we alread know the number of the file, search for a direct
1770 (goto-char (point-min))
1771 (set 'declaration-found t)
1774 (number-to-string (ada-find-file-number-in-ali
1775 (ada-file-of identlist))))
1776 (unless (re-search-forward (concat (ada-ali-index-of identlist)
1777 "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
1778 (ada-line-of identlist)
1780 (ada-column-of identlist) "\\>")
1783 ;; if we did not find it, it may be because the first reference
1784 ;; is not required to have a 'unit_number|' item included.
1785 ;; Or maybe we are already on the declaration...
1786 (unless (re-search-forward
1788 "^[0-9]+.[0-9]+[ *]"
1789 (ada-name-of identlist)
1790 "[ <{=\(]\\(.\\|\n\\.\\)*\\<"
1791 (ada-line-of identlist)
1793 (ada-column-of identlist) "\\>")
1796 ;; If still not found, then either the declaration is unknown
1797 ;; or the source file has been modified since the ali file was
1799 (set 'declaration-found nil)
1803 ;; Last check to be completly sure we have found the correct line (the
1804 ;; ali might not be up to date for instance)
1805 (if declaration-found
1808 ;; while we have a continuation line, go up one line
1809 (while (looking-at "^\\.")
1811 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1812 (ada-name-of identlist) "[ <{=\(]"))
1813 (set 'declaration-found nil))))
1815 ;; Still no success ! The ali file must be too old, and we need to
1816 ;; use a basic algorithm based on guesses. Note that this only happens
1817 ;; if the user does not want us to automatically recompile files
1819 (unless declaration-found
1820 (if (ada-xref-find-in-modified-ali identlist)
1821 (set 'declaration-found t)
1822 ;; no more idea to find the declaration. Give up
1824 (kill-buffer ali-buffer)
1825 (error (concat "No declaration of " (ada-name-of identlist)
1831 ;; Now that we have found a suitable line in the .ali file, get the
1832 ;; information available
1834 (if declaration-found
1835 (let ((current-line (buffer-substring
1836 (point) (save-excursion (end-of-line) (point)))))
1840 (while (looking-at "^\\.\\(.*\\)")
1841 (set 'current-line (concat current-line (match-string 1)))
1845 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
1847 ;; If we can find the file
1849 (ada-set-declare-file
1851 (ada-get-ada-file-name (match-string 1)
1852 (ada-file-of identlist)))
1854 ;; Else clean up the ali file
1856 (kill-buffer ali-buffer)
1857 (error (error-message-string err)))
1860 (ada-set-references identlist current-line)
1864 (defun ada-xref-find-in-modified-ali (identlist)
1865 "Find the matching position for IDENTLIST in the current ali buffer.
1866 This function is only called when the file was not up-to-date, so we need
1867 to make some guesses.
1868 This function is disabled for operators, and only works for identifiers."
1870 (unless (= (string-to-char (ada-name-of identlist)) ?\")
1872 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1873 (my-regexp (concat "[ *]"
1874 (regexp-quote (ada-name-of identlist)) " "))
1880 (ali-buffer (current-buffer)))
1882 (goto-char (point-max))
1883 (while (re-search-backward my-regexp nil t)
1885 (setq line-ali (count-lines 1 (point)))
1887 ;; have a look at the line and column numbers
1888 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1890 (setq line-ada (match-string 1))
1891 (setq col-ada (match-string 2)))
1892 (setq line-ada "--")
1895 ;; construct a list with the file names and the positions within
1896 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
1898 'declist (list line-ali (match-string 1) line-ada col-ada))
1903 ;; how many possible declarations have we found ?
1904 (setq len (length declist))
1908 (kill-buffer (current-buffer))
1909 (error (concat "No declaration of "
1910 (ada-name-of identlist)
1911 " recorded in .ali file")))
1913 ;; one => should be the right one
1915 (goto-line (caar declist)))
1917 ;; more than one => display choice list
1919 (save-window-excursion
1920 (with-output-to-temp-buffer "*choice list*"
1922 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1923 (princ "Possible declarations are:\n\n")
1924 (princ " no. in file at line col\n")
1925 (princ " --- --------------------- ---- ----\n")
1927 (while (< counter len)
1928 (princ (format " %2d) %-21s %4s %4s\n"
1930 (ada-get-ada-file-name
1931 (nth 1 (nth counter declist))
1932 (ada-file-of identlist))
1933 (nth 2 (nth counter declist))
1934 (nth 3 (nth counter declist))
1936 (setq counter (1+ counter))
1939 ) ; end of with-output-to ...
1943 (not (integerp choice))
1948 (read-from-minibuffer "Enter No. of your choice: "))))
1950 (set-buffer ali-buffer)
1951 (goto-line (car (nth (1- choice) declist)))
1955 (defun ada-find-in-ali (identlist &optional other-frame)
1956 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1957 If OTHER-FRAME is non nil, and `ada-xref-other-buffer' is non nil,
1958 opens a new window to show the declaration."
1960 (ada-get-all-references identlist)
1961 (let ((ali-line (ada-references-of identlist))
1966 ;; Note: in some cases, an entity can have multiple references to the
1967 ;; bodies (this is for instance the case for a separate subprogram, that
1968 ;; has a reference both to the stub and to the real body).
1969 ;; In that case, we simply go to each one in turn.
1971 ;; Get all the possible locations
1972 (string-match "^\\([0-9]+\\)[a-zA-Z+]\\([0-9]+\\)[ *]" ali-line)
1973 (set 'locations (list (list (match-string 1 ali-line) ;; line
1974 (match-string 2 ali-line) ;; column
1975 (ada-declare-file-of identlist))))
1976 (while (string-match "\\([0-9]+\\)[bc]\\([0-9]+\\)" ali-line start)
1977 (setq line (match-string 1 ali-line)
1978 col (match-string 2 ali-line)
1979 start (match-end 2))
1981 ;; it there was a file number in the same line
1982 (if (string-match (concat "\\([0-9]+\\)|\\([^|bc]+\\)?"
1983 (match-string 0 ali-line))
1985 (let ((file-number (match-string 1 ali-line)))
1986 (goto-char (point-min))
1987 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1988 (string-to-number file-number))
1989 (set 'file (match-string 1))
1991 ;; Else get the nearest file
1992 (set 'file (ada-declare-file-of identlist)))
1994 (set 'locations (append locations (list (list line col file)))))
1996 ;; Add the specs at the end again, so that from the last body we go to
1998 (set 'locations (append locations (list (car locations))))
2000 ;; Find the new location we want to go to.
2001 ;; If we are on none of the locations listed, we simply go to the specs.
2003 (setq line (caar locations)
2004 col (nth 1 (car locations))
2005 file (nth 2 (car locations)))
2008 (if (and (string= (caar locations) (ada-line-of identlist))
2009 (string= (nth 1 (car locations)) (ada-column-of identlist))
2010 (string= (file-name-nondirectory (nth 2 (car locations)))
2011 (file-name-nondirectory (ada-file-of identlist))))
2012 (setq locations (cadr locations)
2013 line (car locations)
2014 col (nth 1 locations)
2015 file (nth 2 locations)
2017 (set 'locations (cdr locations))))
2019 ;; Find the file in the source path
2020 (set 'file (ada-get-ada-file-name file (ada-file-of identlist)))
2022 ;; Kill the .ali buffer
2023 (kill-buffer (current-buffer))
2025 ;; Now go to the buffer
2026 (ada-xref-change-buffer file
2027 (string-to-number line)
2028 (1- (string-to-number col))
2033 (defun ada-find-in-src-path (identlist &optional other-frame)
2034 "More general function for cross-references.
2035 This function should be used when the standard algorithm that parses the
2036 .ali file has failed, either because that file was too old or even did not
2038 This function attempts to find the possible declarations for the identifier
2039 anywhere in the object path.
2040 This command requires the external `egrep' program to be available.
2042 This works well when one is using an external librarie and wants
2043 to find the declaration and documentation of the subprograms one is
2047 (dirs (ada-xref-get-obj-dir-field))
2048 (regexp (concat "[ *]" (ada-name-of identlist)))
2055 ;; Do the grep in all the directories. We do multiple shell
2056 ;; commands instead of one in case there is no .ali file in one
2057 ;; of the directory and the shell stops because of that.
2059 (set-buffer (get-buffer-create "*grep*"))
2061 (insert (shell-command-to-string
2062 (concat "egrep -i -h '^X|" regexp "( |$)' "
2063 (file-name-as-directory (car dirs)) "*.ali")))
2064 (set 'dirs (cdr dirs)))
2066 ;; Now parse the output
2067 (set 'case-fold-search t)
2068 (goto-char (point-min))
2069 (while (re-search-forward regexp nil t)
2072 (if (not (= (char-after) ?X))
2074 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
2075 (setq line (match-string 1)
2076 column (match-string 2))
2077 (re-search-backward "^X [0-9]+ \\(.*\\)$")
2078 (set 'file (list (match-string 1) line column))
2080 ;; There could be duplicate choices, because of the structure
2081 ;; of the .ali files
2082 (unless (member file list)
2083 (set 'list (append list (list file))))))))
2085 ;; Current buffer is still "*grep*"
2086 (kill-buffer "*grep*")
2089 ;; Now display the list of possible matches
2092 ;; No choice found => Error
2094 (error "No cross-reference found, please recompile your file"))
2096 ;; Only one choice => Do the cross-reference
2097 ((= (length list) 1)
2098 (set 'file (ada-find-src-file-in-dir (caar list)))
2100 (ada-xref-change-buffer file
2101 (string-to-number (nth 1 (car list)))
2102 (string-to-number (nth 2 (car list)))
2105 (error (concat (caar list) " not found in src_dir")))
2106 (message "This is only a (good) guess at the cross-reference.")
2109 ;; Else, ask the user
2111 (save-window-excursion
2112 (with-output-to-temp-buffer "*choice list*"
2114 (princ "Identifier is overloaded and Xref information is not up to date.\n")
2115 (princ "Possible declarations are:\n\n")
2116 (princ " no. in file at line col\n")
2117 (princ " --- --------------------- ---- ----\n")
2119 (while (< counter (length list))
2120 (princ (format " %2d) %-21s %4s %4s\n"
2122 (nth 0 (nth counter list))
2123 (nth 1 (nth counter list))
2124 (nth 2 (nth counter list))
2126 (setq counter (1+ counter))
2129 (while (or (not choice)
2130 (not (integerp choice))
2132 (> choice (length list)))
2135 (read-from-minibuffer "Enter No. of your choice: "))))
2137 (set 'choice (1- choice))
2138 (kill-buffer "*choice list*")
2140 (set 'file (ada-find-src-file-in-dir (car (nth choice list))))
2142 (ada-xref-change-buffer file
2143 (string-to-number (nth 1 (nth choice list)))
2144 (string-to-number (nth 2 (nth choice list)))
2147 (error (concat (car (nth choice list)) " not found in src_dir")))
2148 (message "This is only a (good) guess at the cross-reference.")
2151 (defun ada-xref-change-buffer
2152 (file line column identlist &optional other-frame)
2153 "Select and display FILE, at LINE and COLUMN.
2154 If we do not end on the same identifier as IDENTLIST, find the closest
2155 match. Kills the .ali buffer at the end.
2156 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2158 (let (declaration-buffer)
2160 ;; Select and display the destination buffer
2161 (if ada-xref-other-buffer
2163 (find-file-other-frame file)
2164 (set 'declaration-buffer (find-file-noselect file))
2165 (set-buffer declaration-buffer)
2166 (switch-to-buffer-other-window declaration-buffer)
2171 ;; move the cursor to the correct position
2174 (move-to-column column)
2176 ;; If we are not on the identifier, the ali file was not up-to-date.
2177 ;; Try to find the nearest position where the identifier is found,
2178 ;; this is probably the right one.
2179 (unless (looking-at (ada-name-of identlist))
2180 (ada-xref-search-nearest (ada-name-of identlist)))
2184 (defun ada-xref-search-nearest (name)
2185 "Searches for NAME nearest to the position recorded in the Xref file.
2186 It returns the position of the declaration in the buffer or nil if not found."
2187 (let ((orgpos (point))
2191 (goto-char (point-max))
2193 ;; loop - look for all declarations of name in this file
2194 (while (search-backward name nil t)
2196 ;; check if it really is a complete Ada identifier
2198 (not (save-excursion
2199 (goto-char (match-end 0))
2201 (not (ada-in-string-or-comment-p))
2203 ;; variable declaration ?
2205 (skip-chars-forward "a-zA-Z_0-9" )
2206 (ada-goto-next-non-ws)
2207 (looking-at ":[^=]"))
2208 ;; procedure, function, task or package declaration ?
2210 (ada-goto-previous-word)
2211 (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]\\>"))))
2213 ;; check if it is nearer than the ones before if any
2215 (< (abs (- (point) orgpos)) diff))
2217 (setq newpos (point)
2218 diff (abs (- newpos orgpos))))))
2223 (message "ATTENTION: this declaration is only a (good) guess ...")
2228 ;; Find the parent library file of the current file
2229 (defun ada-goto-parent ()
2230 "Go to the parent library file."
2232 (ada-require-project-file)
2234 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2240 (goto-char (point-min))
2241 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2242 (setq unit-name (match-string 1))
2243 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
2245 (kill-buffer buffer)
2246 (error "No parent unit !"))
2247 (setq unit-name (match-string 1 unit-name))
2250 ;; look for the file name for the parent unit specification
2251 (goto-char (point-min))
2252 (re-search-forward (concat "^W " unit-name
2253 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2255 (setq body-name (match-string 1))
2256 (setq ali-name (match-string 2))
2257 (kill-buffer buffer)
2260 (setq ali-name (ada-find-ali-file-in-dir ali-name))
2263 ;; Tries to open the new ali file to find the spec file
2266 (find-file ali-name)
2267 (goto-char (point-min))
2268 (re-search-forward (concat "^U " unit-name "%s[ \t]+"
2270 (setq body-name (match-string 1))
2271 (kill-buffer (current-buffer))
2276 (find-file body-name)
2279 (defun ada-make-filename-from-adaname (adaname)
2280 "Determine the filename in which ADANAME is found.
2281 This is a GNAT specific function that uses gnatkrunch."
2283 (setq krunch-buf (generate-new-buffer "*gkrunch*"))
2285 (set-buffer krunch-buf)
2286 ;; send adaname to external process `gnatkr'.
2287 (call-process "gnatkr" nil krunch-buf nil
2288 adaname ada-krunch-args)
2289 ;; fetch output of that process
2290 (setq adaname (buffer-substring
2293 (goto-char (point-min))
2296 (kill-buffer krunch-buf)))
2300 (defun ada-make-body-gnatstub ()
2301 "Create an Ada package body in the current buffer.
2302 This function uses the `gnatstub' program to create the body.
2303 This function typically is to be hooked into `ff-file-created-hooks'."
2306 (save-some-buffers nil nil)
2308 ;; If the current buffer is the body (as is the case when calling this
2309 ;; function from ff-file-created-hooks), then kill this temporary buffer
2310 (unless (interactive-p)
2312 (set-buffer-modified-p nil)
2313 (kill-buffer (current-buffer))))
2316 ;; Make sure the current buffer is the spec (this might not be the case
2317 ;; if for instance the user was asked for a project file)
2319 (unless (buffer-file-name (car (buffer-list)))
2320 (set-buffer (cadr (buffer-list))))
2322 ;; Make sure we have a project file (for parameters to gnatstub). Note that
2323 ;; this might have already been done if we have been called from the hook,
2324 ;; but this is not an expensive call)
2325 (ada-require-project-file)
2327 ;; Call the external process gnatstub
2328 (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
2329 (filename (buffer-file-name (car (buffer-list))))
2330 (output (concat (file-name-sans-extension filename) ".adb"))
2331 (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename))
2332 (buffer (get-buffer-create "*gnatstub*")))
2336 (compilation-minor-mode 1)
2338 (insert gnatstub-cmd)
2341 ;; call gnatstub to create the body file
2342 (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
2346 (goto-char (point-min))
2347 (search-forward "command not found" nil t))
2349 (message "gnatstub was not found -- using the basic algorithm")
2351 (kill-buffer buffer)
2354 ;; Else clean up the output
2356 (if (file-exists-p output)
2359 (kill-buffer buffer))
2361 ;; display the error buffer
2362 (display-buffer buffer)
2366 (defun ada-xref-initialize ()
2367 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2368 For instance, it creates the gnat-specific menus, sets some hooks for
2370 (make-local-hook 'ff-file-created-hooks)
2371 ;; This should really be an `add-hook'. -stef
2372 (setq ff-file-created-hooks 'ada-make-body-gnatstub)
2374 ;; Completion for file names in the mini buffer should ignore .ali files
2375 (add-to-list 'completion-ignored-extensions ".ali")
2379 ;; ----- Add to ada-mode-hook ---------------------------------------------
2381 ;; Use gvd or ddd as the default debugger if it was found
2382 ;; On windows, do not use the --tty switch for GVD, since this is
2383 ;; not supported. Actually, we do not use this on Unix either, since otherwise
2384 ;; there is no console window left in GVD, and people have to use the
2386 ;; This must be done before initializing the Ada menu.
2387 (if (ada-find-file-in-dir "gvd" exec-path)
2388 (set 'ada-prj-default-debugger "gvd ")
2389 (if (ada-find-file-in-dir "gvd.exe" exec-path)
2390 (set 'ada-prj-default-debugger "gvd ")
2391 (if (ada-find-file-in-dir "ddd" exec-path)
2392 (set 'ada-prj-default-debugger "ddd --tty -fullname -toolbar"))))
2394 ;; Set the keymap once and for all, so that the keys set by the user in his
2395 ;; config file are not overwritten every time we open a new file.
2399 (add-hook 'ada-mode-hook 'ada-xref-initialize)
2401 ;; Initializes the cross references to the runtime library
2402 (ada-initialize-runtime-library "")
2404 ;; Add these standard directories to the search path
2405 (set 'ada-search-directories
2406 (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path)
2407 ada-search-directories))
2409 ;; Make sure that the files are always associated with a project file. Since
2410 ;; the project file has some fields that are used for the editor (like the
2411 ;; casing exceptions), it has to be read before the user edits a file).
2412 ;; (add-hook 'ada-mode-hook
2414 ;; (let ((file (ada-prj-find-prj-file t)))
2415 ;; (if file (ada-reread-prj-file file)))))
2419 ;;; ada-xref.el ends here