1 ;;; ada-xref.el --- for lookup and completion in Ada mode
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2001
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.9 $
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 t
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 "${cross_prefix}gcc -x ada -c ${comp_opt} ${full_current}"
95 "*Default command to be used to compile a single file.
96 Emacs will add the filename at the end of this command. This is the same
97 syntax as in the project file."
98 :type
'string
:group
'ada
)
100 (defcustom ada-prj-default-debugger
"${cross_prefix}gdb"
101 "*Default name of the debugger. We recommend either `gdb',
102 `gdb --emacs_gdbtk' or `ddd --tty -fullname'."
103 :type
'string
:group
'ada
)
105 (defcustom ada-prj-default-make-cmd
106 (concat "${cross_prefix}gnatmake -o ${main} ${main_unit} ${gnatmake_opt} "
107 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
108 "*Default command to be used to compile the application.
109 This is the same syntax as in the project file."
110 :type
'string
:group
'ada
)
112 (defcustom ada-prj-default-project-file
""
113 "*Name of the project file to use for every Ada file.
114 Emacs will not try to use the standard algorithm to find the project file if
115 this string is not empty."
116 :type
'(file :must-match t
) :group
'ada
)
118 (defcustom ada-gnatstub-opts
"-q -I${src_dir}"
119 "*List of the options to pass to gnatsub to generate the body of a package.
120 This has the same syntax as in the project file (with variable substitution)."
121 :type
'string
:group
'ada
)
123 (defcustom ada-always-ask-project nil
124 "*If nil, use default values when no project file was found.
125 Otherwise, ask the user for the name of the project file to use."
126 :type
'boolean
:group
'ada
)
128 (defconst is-windows
(memq system-type
(quote (windows-nt)))
129 "True if we are running on windows NT or windows 95.")
131 (defcustom ada-tight-gvd-integration nil
132 "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
133 If GVD is not the debugger used, nothing happens.")
135 ;; ------- Nothing to be modified by the user below this
136 (defvar ada-last-prj-file
""
137 "Name of the last project file entered by the user.")
139 (defvar ada-check-switch
"-gnats"
140 "Switch added to the command line to check the current file.")
142 (defconst ada-project-file-extension
".adp"
143 "The extension used for project files.")
145 (defvar ada-xref-runtime-library-specs-path
'()
146 "Directories where the specs for the standard library is found.
147 This is used for cross-references.")
149 (defvar ada-xref-runtime-library-ali-path
'()
150 "Directories where the ali for the standard library is found.
151 This is used for cross-references.")
153 (defvar ada-xref-pos-ring
'()
154 "List of positions selected by the cross-references functions.
155 Used to go back to these positions.")
157 (defvar ada-cd-command
158 (if (string-match "cmdproxy.exe" shell-file-name
)
161 "Command to use to change to a specific directory. On windows systems
162 using cmdproxy.exe as the shell, we need to use /d or the drive is never
165 (defvar ada-command-separator
(if is-windows
" && " "\n")
166 "Separator to use when sending multiple commands to `compile' or
168 cmdproxy.exe doesn't recognize multiple-line commands, so we have to use
171 (defconst ada-xref-pos-ring-max
16
172 "Number of positions kept in the list ada-xref-pos-ring.")
174 (defvar ada-operator-re
175 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
176 "Regexp to match for operators.")
178 (defvar ada-xref-project-files
'()
179 "Associative list of project files.
180 It has the following format:
181 \((project_name . value) (project_name . value) ...)
182 As always, the values of the project file are defined through properties.")
184 (defun ada-quote-cmd (cmd)
185 "Duplicates all \\ characters in CMD so that it can be passed to `compile'"
186 (mapconcat 'identity
(split-string cmd
"\\\\") "\\\\"))
188 (defun ada-initialize-runtime-library (cross-prefix)
189 "Initializes the variables for the runtime library location.
190 CROSS-PREFIX is the prefix to use for the gnatls command"
192 (setq ada-xref-runtime-library-specs-path
'()
193 ada-xref-runtime-library-ali-path
'())
194 (set-buffer (get-buffer-create "*gnatls*"))
197 ;; Catch any error in the following form (i.e gnatls was not found)
199 ;; Even if we get an error, delete the *gnatls* buffer
202 (call-process (concat cross-prefix
"gnatls")
204 (goto-char (point-min))
208 (search-forward "Source Search Path:")
210 (while (not (looking-at "^$"))
211 (back-to-indentation)
212 (unless (looking-at "<Current_Directory>")
213 (add-to-list 'ada-xref-runtime-library-specs-path
214 (buffer-substring-no-properties
216 (save-excursion (end-of-line) (point)))))
221 (search-forward "Object Search Path:")
223 (while (not (looking-at "^$"))
224 (back-to-indentation)
225 (unless (looking-at "<Current_Directory>")
226 (add-to-list 'ada-xref-runtime-library-ali-path
227 (buffer-substring-no-properties
229 (save-excursion (end-of-line) (point)))))
234 (set 'ada-xref-runtime-library-specs-path
235 (reverse ada-xref-runtime-library-specs-path
))
236 (set 'ada-xref-runtime-library-ali-path
237 (reverse ada-xref-runtime-library-ali-path
))
241 (defun ada-treat-cmd-string (cmd-string)
242 "Replace meta-sequences like ${...} in CMD-STRING with the appropriate value.
243 The project file must have been loaded first.
244 As a special case, ${current} is replaced with the name of the currently
245 edited file, minus extension but with directory, and ${full_current} is
246 replaced by the name including the extension."
248 (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string
)
250 (name (match-string 2 cmd-string
)))
252 ((string= name
"current")
253 (setq value
(file-name-sans-extension (buffer-file-name))))
254 ((string= name
"full_current")
255 (setq value
(buffer-file-name)))
258 (setq value
(ada-xref-get-project-field (intern name
))))))
260 ;; Check if there is an environment variable with the same name
262 (if (not (setq value
(getenv name
)))
263 (message (concat "No environment variable " name
" found"))))
267 (setq cmd-string
(replace-match "" t t cmd-string
)))
269 (setq cmd-string
(replace-match value t t cmd-string
)))
271 (let ((prefix (match-string 1 cmd-string
)))
272 (setq cmd-string
(replace-match
273 (mapconcat (lambda(x) (concat prefix x
)) value
" ")
278 (defun ada-xref-set-default-prj-values (symbol ada-buffer
)
279 "Reset the properties in SYMBOL to the default values for ADA-BUFFER."
281 (let ((file (buffer-file-name ada-buffer
))
284 (set-buffer ada-buffer
)
287 ;; Try hard to find a default value for filename, so that the user
288 ;; can edit his project file even if the current buffer is not an
289 ;; Ada file or not even associated with a file
290 (list 'filename
(expand-file-name
293 (ada-prj-get-prj-dir file
))
294 (ada-prj-default-project-file
295 ada-prj-default-project-file
)
297 (message (concat "Not editing an Ada file,"
298 "and no default project "
301 'build_dir
(file-name-as-directory (expand-file-name "."))
304 'casing
(if (listp ada-case-exception-file
)
305 ada-case-exception-file
306 (list ada-case-exception-file
))
307 'comp_opt ada-prj-default-comp-opt
308 'bind_opt ada-prj-default-bind-opt
309 'link_opt ada-prj-default-link-opt
310 'gnatmake_opt ada-prj-default-gnatmake-opt
311 'gnatfind_opt ada-prj-gnatfind-switches
313 (file-name-nondirectory
314 (file-name-sans-extension file
))
317 (file-name-nondirectory
318 (file-name-sans-extension file
))
322 'comp_cmd
(list (concat ada-cd-command
" ${build_dir}")
323 ada-prj-default-comp-cmd
)
324 'check_cmd
(list (concat ada-prj-default-comp-cmd
" "
326 'make_cmd
(list (concat ada-cd-command
" ${build_dir}")
327 ada-prj-default-make-cmd
)
328 'run_cmd
(list (concat ada-cd-command
" ${build_dir}")
330 (if is-windows
".exe")))
331 'debug_pre_cmd
(list (concat ada-cd-command
333 'debug_cmd
(concat ada-prj-default-debugger
334 (if is-windows
" ${main}.exe"
336 'debug_post_cmd
(list nil
)))
340 (defun ada-xref-get-project-field (field)
341 "Extract the value of FIELD from the current project file.
342 The project file must have been loaded first.
343 A default value is returned if the file was not found.
345 Note that for src_dir and obj_dir, you should rather use
346 `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field' which will in
347 addition return the default paths."
349 (let ((file-name ada-prj-default-project-file
)
352 ;; Get the project file (either the current one, or a default one)
353 (setq file
(or (assoc file-name ada-xref-project-files
)
354 (assoc nil ada-xref-project-files
)))
356 ;; If the file was not found, use the default values
358 ;; Get the value from the file
359 (set 'value
(plist-get (cdr file
) field
))
361 ;; Create a default nil file that contains the default values
362 (ada-xref-set-default-prj-values 'value
(current-buffer))
363 (add-to-list 'ada-xref-project-files
(cons nil value
))
364 (ada-xref-update-project-menu)
365 (set 'value
(plist-get value field
))
368 ;; Substitute the ${...} constructs in all the strings, including
372 (ada-treat-cmd-string value
))
376 (mapcar (lambda(x) (if x
(ada-treat-cmd-string x
) x
)) value
))
383 (defun ada-xref-get-src-dir-field ()
384 "Return the full value for src_dir, including the default directories.
385 All the directories are returned as absolute directories."
387 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
389 ;; Add ${build_dir} in front of the path
392 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir
)
395 ;; Add the standard runtime at the end
396 ada-xref-runtime-library-specs-path
)))
398 (defun ada-xref-get-obj-dir-field ()
399 "Return the full value for obj_dir, including the default directories.
400 All the directories are returned as absolute directories."
402 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
404 ;; Add ${build_dir} in front of the path
407 (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir
)
410 ;; Add the standard runtime at the end
411 ada-xref-runtime-library-ali-path
)))
413 (defun ada-xref-update-project-menu ()
414 "Update the menu Ada->Project, with the list of available project files."
418 ;; Create the standard items
419 (set 'submenu
(list (cons 'Load
(cons "Load..."
420 'ada-set-default-project-file
))
421 (cons 'New
(cons "New..." 'ada-prj-new
))
422 (cons 'Edit
(cons "Edit..." 'ada-prj-edit
))
423 (cons 'sep
(cons "---" nil
))))
428 (let ((name (or (car x
) "<default>"))
430 "Change the active project file."
432 (ada-parse-prj-file ,(car x
))
433 (set 'ada-prj-default-project-file
,(car x
))
434 (ada-xref-update-project-menu))))
437 (list (cons (intern name
)
439 'menu-item
(file-name-sans-extension
440 (file-name-nondirectory name
))
444 (equal ada-prj-default-project-file
448 ;; Parses all the known project files, and insert at least the default
449 ;; one (in case ada-xref-project-files is nil)
450 (or ada-xref-project-files
'(nil)))
453 (if (lookup-key ada-mode-map
[menu-bar Ada Project
])
454 (setcdr (lookup-key ada-mode-map
[menu-bar Ada Project
])
459 ;;-------------------------------------------------------------
460 ;;-- Searching a file anywhere on the source path.
462 ;;-- The following functions provide support for finding a file anywhere
463 ;;-- on the source path, without providing an explicit directory.
464 ;;-- They also provide file name completion in the minibuffer.
466 ;;-- Public subprograms: ada-find-file
468 ;;-------------------------------------------------------------
470 (defun ada-do-file-completion (string predicate flag
)
471 "Completion function when reading a file from the minibuffer.
472 Completion is attempted in all the directories in the source path, as
473 defined in the project file."
475 (dirs (ada-xref-get-src-dir-field)))
478 (if (file-directory-p (car dirs
))
479 (set 'list
(append list
(file-name-all-completions string
(car dirs
)))))
480 (set 'dirs
(cdr dirs
)))
481 (cond ((equal flag
'lambda
)
486 (try-completion string
487 (mapcar (lambda (x) (cons x
1)) list
)
491 (defun ada-find-file (filename)
492 "Open a file anywhere in the source path.
493 Completion is available."
495 (list (completing-read "File: " 'ada-do-file-completion
)))
496 (let ((file (ada-find-src-file-in-dir filename
)))
499 (error (concat filename
" not found in src_dir")))))
502 ;; ----- Keybindings ------------------------------------------------------
504 (defun ada-add-keymap ()
505 "Add new key bindings when using `ada-xrel.el'."
509 (define-key ada-mode-map
'(shift button3
) 'ada-point-and-xref
)
510 (define-key ada-mode-map
'(control tab
) 'ada-complete-identifier
))
511 (define-key ada-mode-map
[C-tab
] 'ada-complete-identifier
)
512 (define-key ada-mode-map
[S-mouse-3
] 'ada-point-and-xref
))
514 (define-key ada-mode-map
"\C-co" 'ff-find-other-file
)
515 (define-key ada-mode-map
"\C-c5\C-d" 'ada-goto-declaration-other-frame
)
516 (define-key ada-mode-map
"\C-c\C-d" 'ada-goto-declaration
)
517 (define-key ada-mode-map
"\C-c\C-s" 'ada-xref-goto-previous-reference
)
518 (define-key ada-mode-map
"\C-c\C-x" 'ada-reread-prj-file
)
519 (define-key ada-mode-map
"\C-c\C-c" 'ada-compile-application
)
520 (define-key ada-mode-map
"\C-cc" 'ada-change-prj
)
521 (define-key ada-mode-map
"\C-cd" 'ada-set-default-project-file
)
522 (define-key ada-mode-map
"\C-cg" 'ada-gdb-application
)
523 (define-key ada-mode-map
"\C-cr" 'ada-run-application
)
524 (define-key ada-mode-map
"\C-c\C-o" 'ada-goto-parent
)
525 (define-key ada-mode-map
"\C-c\C-r" 'ada-find-references
)
526 (define-key ada-mode-map
"\C-c\C-v" 'ada-check-current
)
527 (define-key ada-mode-map
"\C-c\C-f" 'ada-find-file
)
530 ;; ----- Menus --------------------------------------------------------------
531 (defun ada-add-ada-menu ()
532 "Add some items to the standard Ada mode menu.
533 The items are added to the menu called NAME, which should be the same
534 name as was passed to `ada-create-menu'."
537 (let* ((menu-list '("Ada"))
538 (goto-menu '("Ada" "Goto"))
539 (edit-menu '("Ada" "Edit"))
540 (help-menu '("Ada" "Help"))
541 (options-menu (list "Ada" "Options")))
542 (funcall (symbol-function 'add-menu-button
)
543 menu-list
["Check file" ada-check-current
544 (string= mode-name
"Ada")] "Goto")
545 (funcall (symbol-function 'add-menu-button
)
546 menu-list
["Compile file" ada-compile-current
547 (string= mode-name
"Ada")] "Goto")
548 (funcall (symbol-function 'add-menu-button
)
549 menu-list
["Build" ada-compile-application t
] "Goto")
550 (funcall (symbol-function 'add-menu-button
)
551 menu-list
["Run" ada-run-application t
] "Goto")
552 (funcall (symbol-function 'add-menu-button
)
553 menu-list
["Debug" ada-gdb-application t
] "Goto")
554 (funcall (symbol-function 'add-menu-button
)
555 menu-list
["--" nil t
] "Goto")
556 (funcall (symbol-function 'add-menu-button
)
557 goto-menu
["Goto Parent Unit" ada-goto-parent t
]
558 "Next compilation error")
559 (funcall (symbol-function 'add-menu-button
)
560 goto-menu
["Goto References to any entity"
561 ada-find-any-references t
]
562 "Next compilation error")
563 (funcall (symbol-function 'add-menu-button
)
564 goto-menu
["List References" ada-find-references t
]
565 "Next compilation error")
566 (funcall (symbol-function 'add-menu-button
)
567 goto-menu
["Goto Declaration Other Frame"
568 ada-goto-declaration-other-frame t
]
569 "Next compilation error")
570 (funcall (symbol-function 'add-menu-button
)
571 goto-menu
["Goto Declaration/Body"
572 ada-goto-declaration t
]
573 "Next compilation error")
574 (funcall (symbol-function 'add-menu-button
)
575 goto-menu
["Goto Previous Reference"
576 ada-xref-goto-previous-reference t
]
577 "Next compilation error")
578 (funcall (symbol-function 'add-menu-button
)
579 goto-menu
["--" nil t
] "Next compilation error")
580 (funcall (symbol-function 'add-menu-button
)
581 edit-menu
["Complete Identifier"
582 ada-complete-identifier t
]
584 (funcall (symbol-function 'add-menu-button
)
585 edit-menu
["--------" nil t
] "Indent Line")
586 (funcall (symbol-function 'add-menu-button
)
587 help-menu
["Gnat User Guide" (info "gnat_ug")])
588 (funcall (symbol-function 'add-menu-button
)
589 help-menu
["Gnat Reference Manual" (info "gnat_rm")])
590 (funcall (symbol-function 'add-menu-button
)
591 help-menu
["Gcc Documentation" (info "gcc")])
592 (funcall (symbol-function 'add-menu-button
)
593 help-menu
["Gdb Documentation" (info "gdb")])
594 (funcall (symbol-function 'add-menu-button
)
595 help-menu
["Ada95 Reference Manual" (info "arm95")])
596 (funcall (symbol-function 'add-menu-button
)
598 ["Show Cross-References in Other Buffer"
599 (setq ada-xref-other-buffer
600 (not ada-xref-other-buffer
))
601 :style toggle
:selected ada-xref-other-buffer
])
602 (funcall (symbol-function 'add-menu-button
)
604 ["Automatically Recompile for Cross-References"
605 (setq ada-xref-create-ali
(not ada-xref-create-ali
))
606 :style toggle
:selected ada-xref-create-ali
])
607 (funcall (symbol-function 'add-menu-button
)
610 (setq ada-xref-confirm-compile
611 (not ada-xref-confirm-compile
))
612 :style toggle
:selected ada-xref-confirm-compile
])
613 (if (string-match "gvd" ada-prj-default-debugger
)
614 (funcall (symbol-function 'add-menu-button
)
616 ["Tight Integration With Gnu Visual Debugger"
617 (setq ada-tight-gvd-integration
618 (not ada-tight-gvd-integration
))
619 :style toggle
:selected ada-tight-gvd-integration
]))
623 (let* ((menu (lookup-key ada-mode-map
[menu-bar ada
]))
624 (edit-menu (lookup-key ada-mode-map
[menu-bar ada edit
]))
625 (help-menu (lookup-key ada-mode-map
[menu-bar ada help
]))
626 (goto-menu (lookup-key ada-mode-map
[menu-bar ada goto
]))
627 (options-menu (lookup-key ada-mode-map
[menu-bar ada options
])))
629 (define-key-after menu
[Check] '("Check file" . ada-check-current)
631 (define-key-after menu [Compile] '("Compile file" . ada-compile-current)
633 (define-key-after menu [Build] '("Build" . ada-compile-application)
635 (define-key-after menu [Run] '("Run" . ada-run-application) 'Build)
636 (define-key-after menu [Debug] '("Debug" . ada-gdb-application) 'Run)
637 (define-key-after menu [rem] '("--" . nil) 'Debug)
638 (define-key-after menu [Project]
639 (cons "Project" (make-sparse-keymap)) 'rem)
641 (define-key help-menu [Gnat_ug]
642 '("Gnat User Guide" . (lambda() (interactive) (info "gnat_ug"))))
643 (define-key help-menu [Gnat_rm]
644 '("Gnat Reference Manual" . (lambda() (interactive) (info "gnat_rm"))))
645 (define-key help-menu [Gcc]
646 '("Gcc Documentation" . (lambda() (interactive) (info "gcc"))))
647 (define-key help-menu [gdb]
648 '("Gdb Documentation" . (lambda() (interactive) (info "gdb"))))
649 (define-key help-menu [arm95]
650 '("Ada95 Reference Manual" . (lambda() (interactive) (info "arm95"))))
652 (define-key goto-menu [rem] '("----" . nil))
653 (define-key goto-menu [Parent] '("Goto Parent Unit"
655 (define-key goto-menu [References-any]
656 '("Goto References to any entity" . ada-find-any-references))
657 (define-key goto-menu [References]
658 '("List References" . ada-find-references))
659 (define-key goto-menu [Prev]
660 '("Goto Previous Reference" . ada-xref-goto-previous-reference))
661 (define-key goto-menu [Decl-other]
662 '("Goto Declaration Other Frame" . ada-goto-declaration-other-frame))
663 (define-key goto-menu [Decl]
664 '("Goto Declaration/Body" . ada-goto-declaration))
666 (define-key edit-menu [rem] '("----" . nil))
667 (define-key edit-menu [Complete] '("Complete Identifier"
668 . ada-complete-identifier))
670 (define-key-after options-menu [xrefrecompile]
671 '(menu-item "Automatically Recompile for Cross-References"
672 (lambda()(interactive)
673 (setq ada-xref-create-ali (not ada-xref-create-ali)))
674 :button (:toggle . ada-xref-create-ali)) t)
675 (define-key-after options-menu [xrefconfirm]
676 '(menu-item "Confirm Commands"
677 (lambda()(interactive)
678 (setq ada-xref-confirm-compile
679 (not ada-xref-confirm-compile)))
680 :button (:toggle . ada-xref-confirm-compile)) t)
681 (define-key-after options-menu [xrefother]
682 '(menu-item "Show Cross-References in Other Buffer"
683 (lambda()(interactive)
684 (setq ada-xref-other-buffer (not ada-xref-other-buffer)))
685 :button (:toggle . ada-xref-other-buffer)) t)
687 (if (string-match "gvd" ada-prj-default-debugger)
688 (define-key-after options-menu [tightgvd]
689 '(menu-item "Tight Integration With Gnu Visual Debugger"
690 (lambda()(interactive)
691 (setq ada-tight-gvd-integration
692 (not ada-tight-gvd-integration)))
693 :button (:toggle . ada-tight-gvd-integration)) t))
695 (define-key ada-mode-map [menu-bar Ada Edit rem3] '("------------" . nil))
696 (define-key ada-mode-map [menu-bar Ada Edit open-file-from-src-path]
697 '("Search File on source path..." . ada-find-file))
700 (ada-xref-update-project-menu)
703 ;; ----- Utilities -------------------------------------------------
705 (defun ada-require-project-file ()
706 "If no project file is currently active, load a default one."
707 (if (or (not ada-prj-default-project-file)
708 (not ada-xref-project-files)
709 (string= ada-prj-default-project-file ""))
710 (ada-reread-prj-file)))
712 (defun ada-xref-push-pos (filename position)
713 "Push (FILENAME, POSITION) on the position ring for cross-references."
714 (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
715 (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
716 (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
718 (defun ada-xref-goto-previous-reference ()
719 "Go to the previous cross-reference we were on."
721 (if ada-xref-pos-ring
722 (let ((pos (car ada-xref-pos-ring)))
723 (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
724 (find-file (car (cdr pos)))
725 (goto-char (car pos)))))
727 (defun ada-convert-file-name (name)
728 "Converts from NAME to a name that can be used by the compilation commands.
729 This is overriden on VMS to convert from VMS filenames to Unix filenames."
732 (defun ada-set-default-project-file (name)
733 "Set the file whose name is NAME as the default project file."
734 (interactive "fProject file:")
735 (set 'ada-prj-default-project-file name)
736 (ada-reread-prj-file name)
739 ;; ------ Handling the project file -----------------------------
741 (defun ada-prj-find-prj-file (&optional no-user-question)
742 "Find the prj file associated with the current buffer.
743 If NO-USER-QUESTION is non-nil, use a default file if not project file was
744 found, and do not ask the user.
745 If the buffer is not an Ada buffer, associate it with the default project
746 file. If none is set, return nil."
750 ;; Use the active project file if there is one.
751 ;; This is also valid if we don't currently have an Ada buffer, or if
752 ;; the current buffer is not a real file (for instance an emerge buffer)
754 (if (or (not (string= mode-name "Ada"))
755 (not (buffer-file-name))
756 (and ada-prj-default-project-file
757 (not (string= ada-prj-default-project-file ""))))
758 (set 'selected ada-prj-default-project-file)
760 ;; other cases: use a more complex algorithm
762 (let* ((current-file (buffer-file-name))
763 (first-choice (concat
764 (file-name-sans-extension current-file)
765 ada-project-file-extension))
766 (dir (file-name-directory current-file))
768 ;; on Emacs 20.2, directory-files does not work if
769 ;; parse-sexp-lookup-properties is set
770 (parse-sexp-lookup-properties nil)
771 (prj-files (directory-files
773 (concat ".*" (regexp-quote
774 ada-project-file-extension) "$")))
779 ;; Else if there is a project file with the same name as the Ada
780 ;; file, but not the same extension.
781 ((file-exists-p first-choice)
782 (set 'selected first-choice))
784 ;; Else if only one project file was found in the current directory
785 ((= (length prj-files) 1)
786 (set 'selected (car prj-files)))
788 ;; Else if there are multiple files, ask the user
789 ((and (> (length prj-files) 1) (not no-user-question))
790 (save-window-excursion
791 (with-output-to-temp-buffer "*choice list*"
792 (princ "There are more than one possible project file.\n")
793 (princ "Which one should we use ?\n\n")
794 (princ " no. file name \n")
795 (princ " --- ------------------------\n")
797 (while (<= counter (length prj-files))
798 (princ (format " %2d) %s\n"
800 (nth (1- counter) prj-files)))
801 (setq counter (1+ counter))
802 ))) ; end of with-output-to ...
806 (not (integerp choice))
808 (> choice (length prj-files)))
809 (setq choice (string-to-int
810 (read-from-minibuffer "Enter No. of your choice: "))))
811 (set 'selected (nth (1- choice) prj-files))))
813 ;; Else if no project file was found in the directory, ask a name
814 ;; to the user, using as a default value the last one entered by
816 ((= (length prj-files) 0)
817 (unless (or no-user-question (not ada-always-ask-project))
818 (setq ada-last-prj-file
820 (concat "project file [" ada-last-prj-file "]:")
821 nil ada-last-prj-file))
822 (unless (string= ada-last-prj-file "")
823 (set 'selected ada-last-prj-file))))
829 (defun ada-parse-prj-file (prj-file)
830 "Reads and parses the PRJ-FILE file if it was found.
831 The current buffer should be the ada-file buffer."
833 (let (project src_dir obj_dir make_cmd comp_cmd check_cmd casing
834 run_cmd debug_pre_cmd debug_post_cmd
835 (ada-buffer (current-buffer)))
836 (setq prj-file (expand-file-name prj-file))
838 ;; Initialize the project with the default values
839 (ada-xref-set-default-prj-values 'project (current-buffer))
841 ;; Do not use find-file below, since we don't want to show this
842 ;; buffer. If the file is open through speedbar, we can't use
843 ;; find-file anyway, since the speedbar frame is special and does not
844 ;; allow the selection of a file in it.
846 (set-buffer (find-file-noselect prj-file))
849 (goto-char (point-min))
851 ;; Now overrides these values with the project file
853 (if (looking-at "^\\([^=]+\\)=\\(.*\\)")
855 ((string= (match-string 1) "src_dir")
856 (add-to-list 'src_dir
857 (file-name-as-directory (match-string 2))))
858 ((string= (match-string 1) "obj_dir")
859 (add-to-list 'obj_dir
860 (file-name-as-directory (match-string 2))))
861 ((string= (match-string 1) "casing")
862 (set 'casing (cons (match-string 2) casing)))
863 ((string= (match-string 1) "build_dir")
865 (plist-put project 'build_dir
866 (file-name-as-directory (match-string 2)))))
867 ((string= (match-string 1) "make_cmd")
868 (add-to-list 'make_cmd (match-string 2)))
869 ((string= (match-string 1) "comp_cmd")
870 (add-to-list 'comp_cmd (match-string 2)))
871 ((string= (match-string 1) "check_cmd")
872 (add-to-list 'check_cmd (match-string 2)))
873 ((string= (match-string 1) "run_cmd")
874 (add-to-list 'run_cmd (match-string 2)))
875 ((string= (match-string 1) "debug_pre_cmd")
876 (add-to-list 'debug_pre_cmd (match-string 2)))
877 ((string= (match-string 1) "debug_post_cmd")
878 (add-to-list 'debug_post_cmd (match-string 2)))
880 (set 'project (plist-put project (intern (match-string 1))
881 (match-string 2))))))
884 (if src_dir (set 'project (plist-put project 'src_dir
886 (if obj_dir (set 'project (plist-put project 'obj_dir
888 (if casing (set 'project (plist-put project 'casing
890 (if make_cmd (set 'project (plist-put project 'make_cmd
891 (reverse make_cmd))))
892 (if comp_cmd (set 'project (plist-put project 'comp_cmd
893 (reverse comp_cmd))))
894 (if check_cmd (set 'project (plist-put project 'check_cmd
895 (reverse check_cmd))))
896 (if run_cmd (set 'project (plist-put project 'run_cmd
898 (set 'project (plist-put project 'debug_post_cmd
899 (reverse debug_post_cmd)))
900 (set 'project (plist-put project 'debug_pre_cmd
901 (reverse debug_pre_cmd)))
903 ;; Delete the default project file from the list, if it is there.
904 ;; Note that in that case, this default project is the only one in
906 (if (assoc nil ada-xref-project-files)
907 (setq ada-xref-project-files nil))
909 ;; Memorize the newly read project file
910 (if (assoc prj-file ada-xref-project-files)
911 (setcdr (assoc prj-file ada-xref-project-files) project)
912 (add-to-list 'ada-xref-project-files (cons prj-file project)))
914 ;; Set the project file as the active one.
915 (setq ada-prj-default-project-file prj-file)
917 ;; Sets up the compilation-search-path so that Emacs is able to
918 ;; go to the source of the errors in a compilation buffer
919 (setq compilation-search-path (ada-xref-get-src-dir-field))
921 ;; Set the casing exceptions file list
924 (setq ada-case-exception-file (reverse casing))
925 (ada-case-read-exceptions)))
927 ;; Add the directories to the search path for ff-find-other-file
928 ;; Do not add the '/' or '\' at the end
929 (setq ada-search-directories
930 (append (mapcar 'directory-file-name compilation-search-path)
931 ada-search-directories))
933 ;; Kill the .ali buffer
935 (set-buffer ada-buffer)
937 (ada-xref-update-project-menu)
940 ;; No prj file ? => Setup default values
941 ;; Note that nil means that all compilation modes will first look in the
942 ;; current directory, and only then in the current file's directory. This
943 ;; current file is assumed at this point to be in the common source
945 (setq compilation-search-path (list nil default-directory))
949 (defun ada-find-references (&optional pos)
950 "Find all references to the entity under POS.
951 Calls gnatfind to find the references."
955 (ada-require-project-file)
957 (let* ((identlist (ada-read-identifier pos))
958 (alifile (ada-get-ali-file-name (ada-file-of identlist)))
959 (process-environment (ada-set-environment)))
961 (set-buffer (get-file-buffer (ada-file-of identlist)))
963 ;; if the file is more recent than the executable
964 (if (or (buffer-modified-p (current-buffer))
965 (file-newer-than-file-p (ada-file-of identlist) alifile))
966 (ada-find-any-references (ada-name-of identlist)
967 (ada-file-of identlist)
969 (ada-find-any-references (ada-name-of identlist)
970 (ada-file-of identlist)
971 (ada-line-of identlist)
972 (ada-column-of identlist))))
975 (defun ada-find-any-references (entity &optional file line column)
976 "Search for references to any entity whose name is ENTITY.
977 ENTITY was first found the location given by FILE, LINE and COLUMN."
978 (interactive "sEntity name: ")
979 (ada-require-project-file)
981 ;; Prepare the gnatfind command. Note that we must protect the quotes
982 ;; around operators, so that they are correctly handled and can be
983 ;; processed (gnatfind \"+\":...).
985 (if (= (aref entity 0) ?\")
987 (concat "\\\"" (substring entity 1 -1) "\\\"")
988 (concat "'\"" (substring entity 1 -1) "\"'"))
990 (switches (ada-xref-get-project-field 'gnatfind_opt))
991 (command (concat "gnatfind " switches " "
993 (if file (concat ":" (file-name-nondirectory file)))
994 (if line (concat ":" line))
995 (if column (concat ":" column)))))
997 ;; If a project file is defined, use it
998 (if (and ada-prj-default-project-file
999 (not (string= ada-prj-default-project-file "")))
1000 (setq command (concat command " -p" ada-prj-default-project-file)))
1002 (compile-internal command "No more references" "gnatfind")
1004 ;; Hide the "Compilation" menu
1006 (set-buffer "*gnatfind*")
1007 (local-unset-key [menu-bar compilation-menu]))
1011 (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
1013 ;; ----- Identlist manipulation -------------------------------------------
1014 ;; An identlist is a vector that is used internally to reference an identifier
1015 ;; To facilitate its use, we provide the following macros
1017 (defmacro ada-make-identlist () (make-vector 8 nil))
1018 (defmacro ada-name-of (identlist) (list 'aref identlist 0))
1019 (defmacro ada-line-of (identlist) (list 'aref identlist 1))
1020 (defmacro ada-column-of (identlist) (list 'aref identlist 2))
1021 (defmacro ada-file-of (identlist) (list 'aref identlist 3))
1022 (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
1023 (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
1024 (defmacro ada-references-of (identlist) (list 'aref identlist 6))
1025 (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
1027 (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
1028 (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
1029 (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
1030 (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
1031 (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
1032 (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
1033 (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
1034 (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
1036 (defsubst ada-get-ali-buffer (file)
1037 "Reads the ali file into a new buffer, and returns this buffer's name"
1038 (find-file-noselect (ada-get-ali-file-name file)))
1042 ;; ----- Identifier Completion --------------------------------------------
1043 (defun ada-complete-identifier (pos)
1044 "Tries to complete the identifier around POS.
1045 The feature is only available if the files where compiled not using the -gnatx
1048 (ada-require-project-file)
1050 ;; Initialize function-local variables and jump to the .ali buffer
1051 ;; Note that for regexp search is case insensitive too
1052 (let* ((curbuf (current-buffer))
1053 (identlist (ada-read-identifier pos))
1054 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
1055 (regexp-quote (ada-name-of identlist))
1056 "[a-zA-Z0-9_]*\\)"))
1060 ;; Open the .ali file
1061 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
1062 (goto-char (point-max))
1064 ;; build an alist of possible completions
1065 (while (re-search-backward sofar nil t)
1066 (setq symalist (cons (cons (match-string 1) nil) symalist)))
1068 (setq completed (try-completion "" symalist))
1070 ;; kills .ali buffer
1073 ;; deletes the incomplete identifier in the buffer
1075 (looking-at "[a-zA-Z0-9_]+")
1077 ;; inserts the completed symbol
1081 ;; ----- Cross-referencing ----------------------------------------
1083 (defun ada-point-and-xref ()
1084 "Calls `mouse-set-point' and then `ada-goto-declaration'."
1086 (mouse-set-point last-input-event)
1087 (ada-goto-declaration (point)))
1089 (defun ada-goto-declaration (pos &optional other-frame)
1090 "Display the declaration of the identifier around POS.
1091 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1093 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1095 (ada-require-project-file)
1097 (ada-xref-push-pos (buffer-file-name) pos)
1099 ;; First try the standard algorithm by looking into the .ali file, but if
1100 ;; that file was too old or even did not exist, try to look in the whole
1101 ;; object path for a possible location.
1102 (let ((identlist (ada-read-identifier pos)))
1104 (ada-find-in-ali identlist other-frame)
1105 (error (ada-find-in-src-path identlist other-frame)))))
1107 (defun ada-goto-declaration-other-frame (pos &optional other-frame)
1108 "Display the declaration of the identifier around POS.
1109 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1111 (ada-goto-declaration pos t))
1113 (defun ada-remote (command)
1114 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1115 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1116 (if (or (not machine) (string= machine ""))
1118 (format "%s %s '(%s)'"
1119 remote-shell-program
1123 (defun ada-get-absolute-dir-list (dir-list root-dir)
1124 "Returns the list of absolute directories found in dir-list.
1125 If a directory is a relative directory, the value of ROOT-DIR is added in
1127 (mapcar (lambda (x) (expand-file-name x root-dir)) dir-list))
1129 (defun ada-set-environment ()
1130 "Return the new value for process-environment.
1131 It modifies the source path and object path with the values found in the
1133 (let ((include (getenv "ADA_INCLUDE_PATH"))
1134 (objects (getenv "ADA_OBJECTS_PATH"))
1135 (build-dir (ada-xref-get-project-field 'build_dir)))
1137 (set 'include (concat path-separator include)))
1139 (set 'objects (concat path-separator objects)))
1141 (concat "ADA_INCLUDE_PATH="
1142 (mapconcat (lambda(x) (expand-file-name x build-dir))
1143 (ada-xref-get-project-field 'src_dir)
1147 (concat "ADA_OBJECTS_PATH="
1148 (mapconcat (lambda(x) (expand-file-name x build-dir))
1149 (ada-xref-get-project-field 'obj_dir)
1152 process-environment))))
1154 (defun ada-compile-application (&optional arg)
1155 "Compiles the application, using the command found in the project file.
1156 If ARG is not nil, ask for user confirmation."
1158 (ada-require-project-file)
1159 (let ((cmd (ada-xref-get-project-field 'make_cmd))
1160 (process-environment (ada-set-environment))
1161 (compilation-scroll-output t))
1163 (setq compilation-search-path (ada-xref-get-src-dir-field))
1165 ;; If no project file was found, ask the user
1167 (setq cmd '("") arg t))
1169 ;; Make a single command from the list of commands, including the
1170 ;; commands to run it on a remote machine.
1171 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1173 (if (or ada-xref-confirm-compile arg)
1174 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1176 ;; Insert newlines so as to separate the name of the commands to run
1177 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1178 ;; which gets confused by newline characters.
1179 (if (not (string-match "cmdproxy.exe" shell-file-name))
1180 (setq cmd (concat cmd "\n\n")))
1182 (compile (ada-quote-cmd cmd))))
1184 (defun ada-compile-current (&optional arg prj-field)
1185 "Recompile the current file.
1186 If ARG is not nil, ask for user confirmation of the command.
1187 PRJ-FIELD is the name of the field to use in the project file to get the
1188 command, and should be either comp_cmd (default) or check_cmd."
1190 (ada-require-project-file)
1191 (let* ((field (if prj-field prj-field 'comp_cmd))
1192 (cmd (ada-xref-get-project-field field))
1193 (process-environment (ada-set-environment))
1194 (compilation-scroll-output t))
1196 (setq compilation-search-path (ada-xref-get-src-dir-field))
1199 (setq cmd '("") arg t))
1201 ;; Make a single command from the list of commands, including the
1202 ;; commands to run it on a remote machine.
1203 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1205 ;; If no project file was found, ask the user
1206 (if (or ada-xref-confirm-compile arg)
1207 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1209 ;; Insert newlines so as to separate the name of the commands to run
1210 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1211 ;; which gets confused by newline characters.
1212 (if (not (string-match "cmdproxy.exe" shell-file-name))
1213 (setq cmd (concat cmd "\n\n")))
1215 (compile (ada-quote-cmd cmd))))
1217 (defun ada-check-current (&optional arg)
1218 "Recompile the current file.
1219 If ARG is not nil, ask for user confirmation of the command."
1221 (ada-compile-current arg 'check_cmd))
1223 (defun ada-run-application (&optional arg)
1224 "Run the application.
1225 if ARG is not-nil, asks for user confirmation."
1227 (ada-require-project-file)
1229 (let ((machine (ada-xref-get-project-field 'cross_prefix)))
1230 (if (and machine (not (string= machine "")))
1231 (error "This feature is not supported yet for cross environments")))
1233 (let ((command (ada-xref-get-project-field 'run_cmd)))
1235 ;; Guess the command if it wasn't specified
1237 (set 'command (list (file-name-sans-extension (buffer-name)))))
1239 ;; Modify the command to run remotely
1240 (setq command (ada-remote (mapconcat 'identity command
1241 ada-command-separator)))
1243 ;; Ask for the arguments to the command if required
1244 (if (or ada-xref-confirm-compile arg)
1245 (setq command (read-from-minibuffer "Enter command to execute: "
1250 (set-buffer (get-buffer-create "*run*"))
1251 (set 'buffer-read-only nil)
1254 (start-process "run" (current-buffer) shell-file-name
1257 ;; Set these two variables to their default values, since otherwise
1258 ;; the output buffer is scrolled so that only the last output line
1259 ;; is visible at the top of the buffer.
1260 (set (make-local-variable 'scroll-step) 0)
1261 (set (make-local-variable 'scroll-conservatively) 0)
1263 (display-buffer "*run*")
1265 ;; change to buffer *run* for interactive programs
1267 (switch-to-buffer "*run*")
1270 (defun ada-gdb-application (&optional arg executable-name)
1271 "Start the debugger on the application.
1272 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1274 If ARG is non-nil, ask the user to confirm the command."
1276 (let ((buffer (current-buffer))
1277 cmd pre-cmd post-cmd)
1278 (ada-require-project-file)
1279 (setq cmd (if executable-name
1280 (concat ada-prj-default-debugger " " executable-name)
1281 (ada-xref-get-project-field 'debug_cmd))
1282 pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
1283 post-cmd (ada-xref-get-project-field 'debug_post_cmd))
1285 ;; If the command was not given in the project file, start a bare gdb
1287 (set 'cmd (concat ada-prj-default-debugger
1290 (file-name-sans-extension (buffer-file-name))))))
1292 ;; For gvd, add an extra switch so that the Emacs window is completly
1293 ;; swallowed inside the Gvd one
1294 (if (and ada-tight-gvd-integration
1295 (string-match "^[^ \t]*gvd" cmd))
1296 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1297 ;; We make sure that gvd swallows the new frame, not the one the
1298 ;; user has been using until now
1299 ;; The frame is made invisible initially, so that GtkPlug gets a
1300 ;; chance to fully manage it. Then it works fine with Enlightenment
1302 (let ((frame (make-frame '((visibility . nil)))))
1304 cmd " --editor-window="
1305 (cdr (assoc 'outer-window-id (frame-parameters frame)))))
1306 (select-frame frame)))
1308 ;; Add a -fullname switch
1309 ;; Use the remote machine
1310 (set 'cmd (ada-remote (concat cmd " -fullname ")))
1312 ;; Ask for confirmation if required
1313 (if (or arg ada-xref-confirm-compile)
1314 (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
1318 gud-gdb-massage-args)
1320 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1321 (fset 'gud-gdb-massage-args (lambda (file args) args))
1323 (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
1324 (if (not (equal pre-cmd ""))
1325 (setq pre-cmd (concat pre-cmd ada-command-separator)))
1327 (set 'post-cmd (mapconcat 'identity post-cmd "\n"))
1329 (set 'post-cmd (concat post-cmd "\n")))
1331 ;; Temporarily replaces the definition of `comint-exec' so that we
1332 ;; can execute commands before running gdb.
1334 `(lambda (buffer name command startfile switches)
1335 (let (compilation-buffer-name-function)
1337 (set 'compilation-buffer-name-function
1338 (lambda(x) (buffer-name buffer)))
1339 (compile (ada-quote-cmd
1342 (mapconcat 'identity switches " "))))))
1345 ;; Tight integration should force the tty mode
1346 (if (and (string-match "gvd" (comint-arguments cmd 0 0))
1347 ada-tight-gvd-integration
1348 (not (string-match "--tty" cmd)))
1349 (setq cmd (concat cmd "--tty")))
1351 (if (and (string-match "jdb" (comint-arguments cmd 0 0))
1353 (funcall (symbol-function 'jdb) cmd)
1356 ;; Send post-commands to the debugger
1357 (process-send-string (get-buffer-process (current-buffer)) post-cmd)
1359 ;; Move to the end of the debugger buffer, so that it is automatically
1360 ;; scrolled from then on.
1363 ;; Display both the source window and the debugger window (the former
1364 ;; above the latter). No need to show the debugger window unless it
1365 ;; is going to have some relevant information.
1366 (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
1367 (string-match "--tty" cmd))
1368 (split-window-vertically))
1369 (switch-to-buffer buffer)
1373 (defun ada-reread-prj-file (&optional filename)
1374 "Forces Emacs to read either FILENAME or the project file associated
1375 with the current buffer.
1376 Otherwise, this file is only read once, and never read again.
1377 Since the information in the project file is shared between all buffers, this
1378 automatically modifies the setup for all the Ada buffer that use this file."
1381 (ada-parse-prj-file filename)
1382 (ada-parse-prj-file (ada-prj-find-prj-file)))
1384 ;; Reread the location of the standard runtime library
1385 (ada-initialize-runtime-library
1386 (or (ada-xref-get-project-field 'cross-prefix) ""))
1389 ;; ------ Private routines
1391 (defun ada-xref-current (file &optional ali-file-name)
1392 "Update the cross-references for FILE.
1393 This in fact recompiles FILE to create ALI-FILE-NAME.
1394 This function returns the name of the file that was recompiled to generate
1395 the cross-reference information. Note that the ali file can then be deduced by
1396 replacing the file extension with .ali"
1398 (if (and ali-file-name
1399 (get-file-buffer ali-file-name))
1400 (kill-buffer (get-file-buffer ali-file-name)))
1402 (let* ((name (ada-convert-file-name file))
1403 (body-name (or (ada-get-body-name name) name)))
1405 ;; Always recompile the body when we can. We thus temporarily switch to a
1406 ;; buffer than contains the body of the unit
1408 (let ((body-visible (find-buffer-visiting body-name))
1411 (set-buffer body-visible)
1412 (find-file body-name))
1414 ;; Execute the compilation. Note that we must wait for the end of the
1415 ;; process, or the ALI file would still not be available.
1416 ;; Unfortunately, the underlying `compile' command that we use is
1418 (ada-compile-current)
1419 (setq process (get-buffer-process "*compilation*"))
1422 (not (equal (process-status process) 'exit)))
1425 ;; remove the buffer for the body if it wasn't there before
1426 (unless body-visible
1427 (kill-buffer (find-buffer-visiting body-name)))
1431 (defun ada-find-file-in-dir (file dir-list)
1432 "Search for FILE in DIR-LIST."
1434 (while (and (not found) dir-list)
1435 (set 'found (concat (file-name-as-directory (car dir-list))
1436 (file-name-nondirectory file)))
1438 (unless (file-exists-p found)
1440 (set 'dir-list (cdr dir-list)))
1443 (defun ada-find-ali-file-in-dir (file)
1444 "Find an .ali file in obj_dir. The current buffer must be the Ada file.
1445 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1446 and the standard runtime location at the end."
1447 (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
1449 (defun ada-find-src-file-in-dir (file)
1450 "Find a source file in src_dir. The current buffer must be the Ada file.
1451 Adds src_dir in front of the search path to conform to gnatmake's behavior,
1452 and the standard runtime location at the end."
1453 (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
1455 (defun ada-get-ali-file-name (file)
1456 "Create the ali file name for the ada-file FILE.
1457 The file is searched for in every directory shown in the obj_dir lines of
1460 ;; This function has to handle the special case of non-standard
1461 ;; file names (i.e. not .adb or .ads)
1462 ;; The trick is the following:
1463 ;; 1- replace the extension of the current file with .ali,
1464 ;; and look for this file
1465 ;; 2- If this file is found:
1466 ;; grep the "^U" lines, and make sure we are not reading the
1467 ;; .ali file for a spec file. If we are, go to step 3.
1468 ;; 3- If the file is not found or step 2 failed:
1469 ;; find the name of the "other file", ie the body, and look
1470 ;; for its associated .ali file by subtituing the extension
1472 ;; We must also handle the case of separate packages and subprograms:
1473 ;; 4- If no ali file was found, we try to modify the file name by removing
1474 ;; everything after the last '-' or '.' character, so as to get the
1475 ;; ali file for the parent unit. If we found an ali file, we check that
1476 ;; it indeed contains the definition for the separate entity by checking
1477 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1481 (set-buffer (get-file-buffer file))
1482 (let ((short-ali-file-name
1483 (concat (file-name-sans-extension (file-name-nondirectory file))
1488 ;; If we have a non-standard file name, and this is a spec, we first
1489 ;; look for the .ali file of the body, since this is the one that
1490 ;; contains the most complete information. If not found, we will do what
1491 ;; we can with the .ali file for the spec...
1493 (if (not (string= (file-name-extension file) "ads"))
1494 (let ((specs ada-spec-suffixes))
1496 (if (string-match (concat (regexp-quote (car specs)) "$")
1499 (set 'specs (cdr specs)))))
1503 (ada-find-ali-file-in-dir
1504 (concat (file-name-sans-extension
1505 (file-name-nondirectory
1506 (ada-other-file-name)))
1513 ;; Else we take the .ali file associated with the unit
1514 (ada-find-ali-file-in-dir short-ali-file-name)
1517 ;; else we did not find the .ali file Second chance: in case
1518 ;; the files do not have standard names (such as for instance
1519 ;; file_s.ada and file_b.ada), try to go to the other file
1520 ;; and look for its ali file
1521 (ada-find-ali-file-in-dir
1522 (concat (file-name-sans-extension
1523 (file-name-nondirectory (ada-other-file-name)))
1527 ;; If we still don't have an ali file, try to get the one
1528 ;; from the parent unit, in case we have a separate entity.
1529 (let ((parent-name (file-name-sans-extension
1530 (file-name-nondirectory file))))
1532 (while (and (not ali-file-name)
1533 (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
1535 (set 'parent-name (match-string 1 parent-name))
1536 (set 'ali-file-name (ada-find-ali-file-in-dir
1537 (concat parent-name ".ali")))
1541 ;; If still not found, try to recompile the file
1542 (if (not ali-file-name)
1543 ;; recompile only if the user asked for this. and search the ali
1544 ;; filename again. We avoid a possible infinite recursion by
1545 ;; temporarily disabling the automatic compilation.
1547 (if ada-xref-create-ali
1549 (concat (file-name-sans-extension (ada-xref-current file))
1552 (error "Ali file not found. Recompile your file"))
1555 ;; same if the .ali file is too old and we must recompile it
1556 (if (and (file-newer-than-file-p file ali-file-name)
1557 ada-xref-create-ali)
1558 (ada-xref-current file ali-file-name)))
1560 ;; Always return the correct absolute file name
1561 (expand-file-name ali-file-name))
1564 (defun ada-get-ada-file-name (file original-file)
1565 "Create the complete file name (+directory) for FILE.
1566 The original file (where the user was) is ORIGINAL-FILE. Search in project
1567 file for possible paths."
1571 ;; If the buffer for original-file, use it to get the values from the
1572 ;; project file, otherwise load the file and its project file
1573 (let ((buffer (get-file-buffer original-file)))
1576 (find-file original-file)
1577 (ada-require-project-file)))
1579 ;; we choose the first possible completion and we
1580 ;; return the absolute file name
1581 (let ((filename (ada-find-src-file-in-dir file)))
1583 (expand-file-name filename)
1585 (file-name-nondirectory file)
1586 " not found in src_dir. Please check your project file")))
1590 (defun ada-find-file-number-in-ali (file)
1591 "Returns the file number for FILE in the associated ali file."
1592 (set-buffer (ada-get-ali-buffer file))
1593 (goto-char (point-min))
1595 (let ((begin (re-search-forward "^D")))
1597 (re-search-forward (concat "^D " (file-name-nondirectory file)))
1598 (count-lines begin (point))))
1600 (defun ada-read-identifier (pos)
1601 "Returns the identlist around POS and switch to the .ali buffer.
1602 The returned list represents the entity, and can be manipulated through the
1603 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1605 ;; If at end of buffer (e.g the buffer is empty), error
1606 (if (>= (point) (point-max))
1607 (error "No identifier on point"))
1609 ;; goto first character of the identifier/operator (skip backward < and >
1610 ;; since they are part of multiple character operators
1612 (skip-chars-backward "a-zA-Z0-9_<>")
1614 ;; check if it really is an identifier
1615 (if (ada-in-comment-p)
1616 (error "Inside comment"))
1618 (let (identifier identlist)
1619 ;; Just in front of a string => we could have an operator declaration,
1620 ;; as in "+", "-", ..
1621 (if (= (char-after) ?\")
1624 ;; if looking at an operator
1625 ;; This is only true if:
1626 ;; - the symbol is +, -, ...
1627 ;; - the symbol is made of letters, and not followed by _ or a letter
1628 (if (and (looking-at ada-operator-re)
1629 (or (not (= (char-syntax (char-after)) ?w))
1630 (not (or (= (char-syntax (char-after (match-end 0))) ?w)
1631 (= (char-after (match-end 0)) ?_)))))
1633 (if (and (= (char-before) ?\")
1634 (= (char-after (+ (length (match-string 0)) (point))) ?\"))
1636 (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
1638 (if (ada-in-string-p)
1639 (error "Inside string or character constant"))
1640 (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
1641 (error "No cross-reference available for reserved keyword"))
1642 (if (looking-at "[a-zA-Z0-9_]+")
1643 (set 'identifier (match-string 0))
1644 (error "No identifier around")))
1646 ;; Build the identlist
1647 (set 'identlist (ada-make-identlist))
1648 (ada-set-name identlist (downcase identifier))
1649 (ada-set-line identlist
1650 (number-to-string (count-lines (point-min) (point))))
1651 (ada-set-column identlist
1652 (number-to-string (1+ (current-column))))
1653 (ada-set-file identlist (buffer-file-name))
1657 (defun ada-get-all-references (identlist)
1658 "Completes and returns IDENTLIST with the information extracted
1659 from the ali file (definition file and places where it is referenced)."
1661 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
1663 (set-buffer ali-buffer)
1664 (goto-char (point-min))
1665 (ada-set-on-declaration identlist nil)
1667 ;; First attempt: we might already be on the declaration of the identifier
1668 ;; We want to look for the declaration only in a definite interval (after
1669 ;; the "^X ..." line for the current file, and before the next "^X" line
1671 (if (re-search-forward
1672 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
1674 (let ((bound (save-excursion (re-search-forward "^X " nil t))))
1675 (set 'declaration-found
1677 (concat "^" (ada-line-of identlist)
1678 "." (ada-column-of identlist)
1679 "[ *]" (ada-name-of identlist)
1680 " \\(.*\\)$") bound t))
1681 (if declaration-found
1682 (ada-set-on-declaration identlist t))
1685 ;; If declaration is still nil, then we were not on a declaration, and
1686 ;; have to fall back on other algorithms
1688 (unless declaration-found
1690 ;; Since we alread know the number of the file, search for a direct
1692 (goto-char (point-min))
1693 (set 'declaration-found t)
1696 (number-to-string (ada-find-file-number-in-ali
1697 (ada-file-of identlist))))
1698 (unless (re-search-forward (concat (ada-ali-index-of identlist)
1699 "|\\([0-9]+.[0-9]+ \\)*"
1700 (ada-line-of identlist)
1702 (ada-column-of identlist))
1705 ;; if we did not find it, it may be because the first reference
1706 ;; is not required to have a 'unit_number|' item included.
1707 ;; Or maybe we are already on the declaration...
1708 (unless (re-search-forward
1710 "^\\(\\([a-zA-Z0-9_.]+\\|\"[<>=+*-/a-z]\"\\)[ *]\\)*"
1711 (ada-line-of identlist)
1713 (ada-column-of identlist))
1716 ;; If still not found, then either the declaration is unknown
1717 ;; or the source file has been modified since the ali file was
1719 (set 'declaration-found nil)
1723 ;; Last check to be completly sure we have found the correct line (the
1724 ;; ali might not be up to date for instance)
1725 (if declaration-found
1728 ;; while we have a continuation line, go up one line
1729 (while (looking-at "^\\.")
1731 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1732 (ada-name-of identlist) "[ <]"))
1733 (set 'declaration-found nil))))
1735 ;; Still no success ! The ali file must be too old, and we need to
1736 ;; use a basic algorithm based on guesses. Note that this only happens
1737 ;; if the user does not want us to automatically recompile files
1739 (unless declaration-found
1740 (if (ada-xref-find-in-modified-ali identlist)
1741 (set 'declaration-found t)
1742 ;; no more idea to find the declaration. Give up
1744 (kill-buffer ali-buffer)
1745 (error (concat "No declaration of " (ada-name-of identlist)
1751 ;; Now that we have found a suitable line in the .ali file, get the
1752 ;; information available
1754 (if declaration-found
1755 (let ((current-line (buffer-substring
1756 (point) (save-excursion (end-of-line) (point)))))
1760 (while (looking-at "^\\.\\(.*\\)")
1761 (set 'current-line (concat current-line (match-string 1)))
1765 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
1767 ;; If we can find the file
1769 (ada-set-declare-file
1771 (ada-get-ada-file-name (match-string 1)
1772 (ada-file-of identlist)))
1774 ;; Else clean up the ali file
1776 (kill-buffer ali-buffer)
1777 (error (error-message-string err)))
1780 (ada-set-references identlist current-line)
1784 (defun ada-xref-find-in-modified-ali (identlist)
1785 "Find the matching position for IDENTLIST in the current ali buffer.
1786 This function is only called when the file was not up-to-date, so we need
1787 to make some guesses.
1788 This function is disabled for operators, and only works for identifiers."
1790 (unless (= (string-to-char (ada-name-of identlist)) ?\")
1792 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1793 (my-regexp (concat "[ *]"
1794 (regexp-quote (ada-name-of identlist)) " "))
1800 (ali-buffer (current-buffer)))
1802 (goto-char (point-max))
1803 (while (re-search-backward my-regexp nil t)
1805 (set 'line-ali (count-lines (point-min) (point)))
1807 ;; have a look at the line and column numbers
1808 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1810 (setq line-ada (match-string 1))
1811 (setq col-ada (match-string 2)))
1812 (setq line-ada "--")
1815 ;; construct a list with the file names and the positions within
1816 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
1818 'declist (list line-ali (match-string 1) line-ada col-ada))
1823 ;; how many possible declarations have we found ?
1824 (setq len (length declist))
1828 (kill-buffer (current-buffer))
1829 (error (concat "No declaration of "
1830 (ada-name-of identlist)
1831 " recorded in .ali file")))
1833 ;; one => should be the right one
1835 (goto-line (caar declist)))
1837 ;; more than one => display choice list
1839 (save-window-excursion
1840 (with-output-to-temp-buffer "*choice list*"
1842 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1843 (princ "Possible declarations are:\n\n")
1844 (princ " no. in file at line col\n")
1845 (princ " --- --------------------- ---- ----\n")
1847 (while (< counter len)
1848 (princ (format " %2d) %-21s %4s %4s\n"
1850 (ada-get-ada-file-name
1851 (nth 1 (nth counter declist))
1852 (ada-file-of identlist))
1853 (nth 2 (nth counter declist))
1854 (nth 3 (nth counter declist))
1856 (setq counter (1+ counter))
1859 ) ; end of with-output-to ...
1863 (not (integerp choice))
1868 (read-from-minibuffer "Enter No. of your choice: "))))
1870 (set-buffer ali-buffer)
1871 (goto-line (car (nth (1- choice) declist)))
1875 (defun ada-find-in-ali (identlist &optional other-frame)
1876 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1877 If OTHER-FRAME is non nil, and `ada-xref-other-buffer' is non nil,
1878 opens a new window to show the declaration."
1880 (ada-get-all-references identlist)
1881 (let ((ali-line (ada-references-of identlist))
1886 ;; Note: in some cases, an entity can have multiple references to the
1887 ;; bodies (this is for instance the case for a separate subprogram, that
1888 ;; has a reference both to the stub and to the real body).
1889 ;; In that case, we simply go to each one in turn.
1891 ;; Get all the possible locations
1892 (string-match "^\\([0-9]+\\)[a-zA-Z+]\\([0-9]+\\)[ *]" ali-line)
1893 (set 'locations (list (list (match-string 1 ali-line) ;; line
1894 (match-string 2 ali-line) ;; column
1895 (ada-declare-file-of identlist))))
1896 (while (string-match "\\([0-9]+\\)[bc]\\([0-9]+\\)" ali-line start)
1897 (setq line (match-string 1 ali-line)
1898 col (match-string 2 ali-line)
1899 start (match-end 2))
1901 ;; it there was a file number in the same line
1902 (if (string-match (concat "\\([0-9]+\\)|\\([^|bc]+\\)?"
1903 (match-string 0 ali-line))
1905 (let ((file-number (match-string 1 ali-line)))
1906 (goto-char (point-min))
1907 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1908 (string-to-number file-number))
1909 (set 'file (match-string 1))
1911 ;; Else get the nearest file
1912 (set 'file (ada-declare-file-of identlist)))
1914 (set 'locations (append locations (list (list line col file)))))
1916 ;; Add the specs at the end again, so that from the last body we go to
1918 (set 'locations (append locations (list (car locations))))
1920 ;; Find the new location we want to go to.
1921 ;; If we are on none of the locations listed, we simply go to the specs.
1923 (setq line (caar locations)
1924 col (nth 1 (car locations))
1925 file (nth 2 (car locations)))
1928 (if (and (string= (caar locations) (ada-line-of identlist))
1929 (string= (nth 1 (car locations)) (ada-column-of identlist))
1930 (string= (file-name-nondirectory (nth 2 (car locations)))
1931 (file-name-nondirectory (ada-file-of identlist))))
1932 (setq locations (cadr locations)
1933 line (car locations)
1934 col (nth 1 locations)
1935 file (nth 2 locations)
1937 (set 'locations (cdr locations))))
1939 ;; Find the file in the source path
1940 (set 'file (ada-get-ada-file-name file (ada-file-of identlist)))
1942 ;; Kill the .ali buffer
1943 (kill-buffer (current-buffer))
1945 ;; Now go to the buffer
1946 (ada-xref-change-buffer file
1947 (string-to-number line)
1948 (1- (string-to-number col))
1953 (defun ada-find-in-src-path (identlist &optional other-frame)
1954 "More general function for cross-references.
1955 This function should be used when the standard algorithm that parses the
1956 .ali file has failed, either because that file was too old or even did not
1958 This function attempts to find the possible declarations for the identifier
1959 anywhere in the object path.
1960 This command requires the external `egrep' program to be available.
1962 This works well when one is using an external librarie and wants
1963 to find the declaration and documentation of the subprograms one is
1967 (dirs (ada-xref-get-obj-dir-field))
1968 (regexp (concat "[ *]" (ada-name-of identlist)))
1975 ;; Do the grep in all the directories. We do multiple shell
1976 ;; commands instead of one in case there is no .ali file in one
1977 ;; of the directory and the shell stops because of that.
1979 (set-buffer (get-buffer-create "*grep*"))
1981 (insert (shell-command-to-string
1982 (concat "egrep -i -h '^X|" regexp "( |$)' "
1983 (file-name-as-directory (car dirs)) "*.ali")))
1984 (set 'dirs (cdr dirs)))
1986 ;; Now parse the output
1987 (set 'case-fold-search t)
1988 (goto-char (point-min))
1989 (while (re-search-forward regexp nil t)
1992 (if (not (= (char-after) ?X))
1994 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
1995 (setq line (match-string 1)
1996 column (match-string 2))
1997 (re-search-backward "^X [0-9]+ \\(.*\\)$")
1998 (set 'file (list (match-string 1) line column))
2000 ;; There could be duplicate choices, because of the structure
2001 ;; of the .ali files
2002 (unless (member file list)
2003 (set 'list (append list (list file))))))))
2005 ;; Current buffer is still "*grep*"
2006 (kill-buffer "*grep*")
2009 ;; Now display the list of possible matches
2012 ;; No choice found => Error
2014 (error "No cross-reference found, please recompile your file"))
2016 ;; Only one choice => Do the cross-reference
2017 ((= (length list) 1)
2018 (set 'file (ada-find-src-file-in-dir (caar list)))
2020 (ada-xref-change-buffer file
2021 (string-to-number (nth 1 (car list)))
2022 (string-to-number (nth 2 (car list)))
2025 (error (concat (caar list) " not found in src_dir")))
2026 (message "This is only a (good) guess at the cross-reference.")
2029 ;; Else, ask the user
2031 (save-window-excursion
2032 (with-output-to-temp-buffer "*choice list*"
2034 (princ "Identifier is overloaded and Xref information is not up to date.\n")
2035 (princ "Possible declarations are:\n\n")
2036 (princ " no. in file at line col\n")
2037 (princ " --- --------------------- ---- ----\n")
2039 (while (< counter (length list))
2040 (princ (format " %2d) %-21s %4s %4s\n"
2042 (nth 0 (nth counter list))
2043 (nth 1 (nth counter list))
2044 (nth 2 (nth counter list))
2046 (setq counter (1+ counter))
2049 (while (or (not choice)
2050 (not (integerp choice))
2052 (> choice (length list)))
2055 (read-from-minibuffer "Enter No. of your choice: "))))
2057 (set 'choice (1- choice))
2058 (kill-buffer "*choice list*")
2060 (set 'file (ada-find-src-file-in-dir (car (nth choice list))))
2062 (ada-xref-change-buffer file
2063 (string-to-number (nth 1 (nth choice list)))
2064 (string-to-number (nth 2 (nth choice list)))
2067 (error (concat (car (nth choice list)) " not found in src_dir")))
2068 (message "This is only a (good) guess at the cross-reference.")
2071 (defun ada-xref-change-buffer
2072 (file line column identlist &optional other-frame)
2073 "Select and display FILE, at LINE and COLUMN.
2074 If we do not end on the same identifier as IDENTLIST, find the closest
2075 match. Kills the .ali buffer at the end.
2076 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2078 (let (declaration-buffer)
2080 ;; Select and display the destination buffer
2081 (if ada-xref-other-buffer
2083 (find-file-other-frame file)
2084 (set 'declaration-buffer (find-file-noselect file))
2085 (set-buffer declaration-buffer)
2086 (switch-to-buffer-other-window declaration-buffer)
2091 ;; move the cursor to the correct position
2094 (move-to-column column)
2096 ;; If we are not on the identifier, the ali file was not up-to-date.
2097 ;; Try to find the nearest position where the identifier is found,
2098 ;; this is probably the right one.
2099 (unless (looking-at (ada-name-of identlist))
2100 (ada-xref-search-nearest (ada-name-of identlist)))
2104 (defun ada-xref-search-nearest (name)
2105 "Searches for NAME nearest to the position recorded in the Xref file.
2106 It returns the position of the declaration in the buffer or nil if not found."
2107 (let ((orgpos (point))
2111 (goto-char (point-max))
2113 ;; loop - look for all declarations of name in this file
2114 (while (search-backward name nil t)
2116 ;; check if it really is a complete Ada identifier
2118 (not (save-excursion
2119 (goto-char (match-end 0))
2121 (not (ada-in-string-or-comment-p))
2123 ;; variable declaration ?
2125 (skip-chars-forward "a-zA-Z_0-9" )
2126 (ada-goto-next-non-ws)
2127 (looking-at ":[^=]"))
2128 ;; procedure, function, task or package declaration ?
2130 (ada-goto-previous-word)
2131 (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]\\>"))))
2133 ;; check if it is nearer than the ones before if any
2135 (< (abs (- (point) orgpos)) diff))
2137 (setq newpos (point)
2138 diff (abs (- newpos orgpos))))))
2143 (message "ATTENTION: this declaration is only a (good) guess ...")
2148 ;; Find the parent library file of the current file
2149 (defun ada-goto-parent ()
2150 "Go to the parent library file."
2152 (ada-require-project-file)
2154 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2160 (goto-char (point-min))
2161 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2162 (setq unit-name (match-string 1))
2163 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
2165 (kill-buffer buffer)
2166 (error "No parent unit !"))
2167 (setq unit-name (match-string 1 unit-name))
2170 ;; look for the file name for the parent unit specification
2171 (goto-char (point-min))
2172 (re-search-forward (concat "^W " unit-name
2173 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2175 (setq body-name (match-string 1))
2176 (setq ali-name (match-string 2))
2177 (kill-buffer buffer)
2180 (setq ali-name (ada-find-ali-file-in-dir ali-name))
2183 ;; Tries to open the new ali file to find the spec file
2186 (find-file ali-name)
2187 (goto-char (point-min))
2188 (re-search-forward (concat "^U " unit-name "%s[ \t]+"
2190 (setq body-name (match-string 1))
2191 (kill-buffer (current-buffer))
2196 (find-file body-name)
2199 (defun ada-make-filename-from-adaname (adaname)
2200 "Determine the filename in which ADANAME is found.
2201 This is a GNAT specific function that uses gnatkrunch."
2203 (setq krunch-buf (generate-new-buffer "*gkrunch*"))
2205 (set-buffer krunch-buf)
2206 ;; send adaname to external process `gnatkr'.
2207 (call-process "gnatkr" nil krunch-buf nil
2208 adaname ada-krunch-args)
2209 ;; fetch output of that process
2210 (setq adaname (buffer-substring
2213 (goto-char (point-min))
2216 (kill-buffer krunch-buf)))
2220 (defun ada-make-body-gnatstub ()
2221 "Create an Ada package body in the current buffer.
2222 This function uses the `gnatstub' program to create the body.
2223 This function typically is to be hooked into `ff-file-created-hooks'."
2226 (save-some-buffers nil nil)
2228 ;; If the current buffer is the body (as is the case when calling this
2229 ;; function from ff-file-created-hooks), then kill this temporary buffer
2230 (unless (interactive-p)
2232 (set-buffer-modified-p nil)
2233 (kill-buffer (current-buffer))))
2236 ;; Make sure the current buffer is the spec (this might not be the case
2237 ;; if for instance the user was asked for a project file)
2239 (unless (buffer-file-name (car (buffer-list)))
2240 (set-buffer (cadr (buffer-list))))
2242 ;; Make sure we have a project file (for parameters to gnatstub). Note that
2243 ;; this might have already been done if we have been called from the hook,
2244 ;; but this is not an expensive call)
2245 (ada-require-project-file)
2247 ;; Call the external process gnatstub
2248 (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
2249 (filename (buffer-file-name (car (buffer-list))))
2250 (output (concat (file-name-sans-extension filename) ".adb"))
2251 (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename))
2252 (buffer (get-buffer-create "*gnatstub*")))
2256 (compilation-minor-mode 1)
2258 (insert gnatstub-cmd)
2261 ;; call gnatstub to create the body file
2262 (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
2266 (goto-char (point-min))
2267 (search-forward "command not found" nil t))
2269 (message "gnatstub was not found -- using the basic algorithm")
2271 (kill-buffer buffer)
2274 ;; Else clean up the output
2276 (if (file-exists-p output)
2279 (kill-buffer buffer))
2281 ;; display the error buffer
2282 (display-buffer buffer)
2286 (defun ada-xref-initialize ()
2287 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2288 For instance, it creates the gnat-specific menus, sets some hooks for
2290 (make-local-hook 'ff-file-created-hooks)
2291 ;; This should really be an `add-hook'. -stef
2292 (setq ff-file-created-hooks 'ada-make-body-gnatstub)
2294 ;; Read the project file and update the search path
2295 ;; before looking for the other file
2296 (make-local-hook 'ff-pre-find-hooks)
2297 (add-hook 'ff-pre-find-hooks 'ada-require-project-file nil t)
2299 ;; Completion for file names in the mini buffer should ignore .ali files
2300 (add-to-list 'completion-ignored-extensions ".ali")
2304 ;; ----- Add to ada-mode-hook ---------------------------------------------
2306 ;; Use gvd or ddd as the default debugger if it was found
2307 ;; On windows, do not use the --tty switch for GVD, since this is
2308 ;; not supported. Actually, we do not use this on Unix either, since otherwise
2309 ;; there is no console window left in GVD, and people have to use the
2311 ;; This must be done before initializing the Ada menu.
2312 (if (ada-find-file-in-dir "gvd" exec-path)
2313 (set 'ada-prj-default-debugger "gvd ")
2314 (if (ada-find-file-in-dir "gvd.exe" exec-path)
2315 (set 'ada-prj-default-debugger "gvd ")
2316 (if (ada-find-file-in-dir "ddd" exec-path)
2317 (set 'ada-prj-default-debugger "ddd --tty -fullname -toolbar"))))
2319 ;; Set the keymap once and for all, so that the keys set by the user in his
2320 ;; config file are not overwritten every time we open a new file.
2324 (add-hook 'ada-mode-hook 'ada-xref-initialize)
2326 ;; Initializes the cross references to the runtime library
2327 (ada-initialize-runtime-library "")
2329 ;; Add these standard directories to the search path
2330 (set 'ada-search-directories
2331 (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path)
2332 ada-search-directories))
2334 ;; Make sure that the files are always associated with a project file. Since
2335 ;; the project file has some fields that are used for the editor (like the
2336 ;; casing exceptions), it has to be read before the user edits a file).
2337 (add-hook 'ada-mode-hook
2339 (let ((file (ada-prj-find-prj-file t)))
2340 (if file (ada-reread-prj-file file)))))
2344 ;;; ada-xref.el ends here