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 (and (lookup-key ada-mode-map
[menu-bar Ada
])
479 (lookup-key ada-mode-map
[menu-bar Ada Project
]))
480 (setcdr (lookup-key ada-mode-map
[menu-bar Ada Project
])
482 (if (lookup-key ada-mode-map
[menu-bar ada Project
])
483 (setcdr (lookup-key ada-mode-map
[menu-bar ada Project
])
488 ;;-------------------------------------------------------------
489 ;;-- Searching a file anywhere on the source path.
491 ;;-- The following functions provide support for finding a file anywhere
492 ;;-- on the source path, without providing an explicit directory.
493 ;;-- They also provide file name completion in the minibuffer.
495 ;;-- Public subprograms: ada-find-file
497 ;;-------------------------------------------------------------
499 (defun ada-do-file-completion (string predicate flag
)
500 "Completion function when reading a file from the minibuffer.
501 Completion is attempted in all the directories in the source path, as
502 defined in the project file."
504 (dirs (ada-xref-get-src-dir-field)))
507 (if (file-directory-p (car dirs
))
508 (set 'list
(append list
(file-name-all-completions string
(car dirs
)))))
509 (set 'dirs
(cdr dirs
)))
510 (cond ((equal flag
'lambda
)
515 (try-completion string
516 (mapcar (lambda (x) (cons x
1)) list
)
520 (defun ada-find-file (filename)
521 "Open a file anywhere in the source path.
522 Completion is available."
524 (list (completing-read "File: " 'ada-do-file-completion
)))
525 (let ((file (ada-find-src-file-in-dir filename
)))
528 (error (concat filename
" not found in src_dir")))))
531 ;; ----- Keybindings ------------------------------------------------------
533 (defun ada-add-keymap ()
534 "Add new key bindings when using `ada-xrel.el'."
538 (define-key ada-mode-map
'(shift button3
) 'ada-point-and-xref
)
539 (define-key ada-mode-map
'(control tab
) 'ada-complete-identifier
))
540 (define-key ada-mode-map
[C-tab
] 'ada-complete-identifier
)
541 (define-key ada-mode-map
[S-mouse-3
] 'ada-point-and-xref
))
543 (define-key ada-mode-map
"\C-co" 'ff-find-other-file
)
544 (define-key ada-mode-map
"\C-c5\C-d" 'ada-goto-declaration-other-frame
)
545 (define-key ada-mode-map
"\C-c\C-d" 'ada-goto-declaration
)
546 (define-key ada-mode-map
"\C-c\C-s" 'ada-xref-goto-previous-reference
)
547 (define-key ada-mode-map
"\C-c\C-c" 'ada-compile-application
)
548 (define-key ada-mode-map
"\C-cc" 'ada-change-prj
)
549 (define-key ada-mode-map
"\C-cd" 'ada-set-default-project-file
)
550 (define-key ada-mode-map
"\C-cg" 'ada-gdb-application
)
551 (define-key ada-mode-map
"\C-cr" 'ada-run-application
)
552 (define-key ada-mode-map
"\C-c\C-o" 'ada-goto-parent
)
553 (define-key ada-mode-map
"\C-c\C-r" 'ada-find-references
)
554 (define-key ada-mode-map
"\C-cl" 'ada-find-local-references
)
555 (define-key ada-mode-map
"\C-c\C-v" 'ada-check-current
)
556 (define-key ada-mode-map
"\C-cf" 'ada-find-file
)
559 ;; ----- Menus --------------------------------------------------------------
560 (defun ada-add-ada-menu ()
561 "Add some items to the standard Ada mode menu.
562 The items are added to the menu called NAME, which should be the same
563 name as was passed to `ada-create-menu'."
566 (let* ((menu-list '("Ada"))
567 (goto-menu '("Ada" "Goto"))
568 (edit-menu '("Ada" "Edit"))
569 (help-menu '("Ada" "Help"))
570 (options-menu (list "Ada" "Options")))
571 (funcall (symbol-function 'add-menu-button
)
572 menu-list
["Check file" ada-check-current
573 (string= mode-name
"Ada")] "Goto")
574 (funcall (symbol-function 'add-menu-button
)
575 menu-list
["Compile file" ada-compile-current
576 (string= mode-name
"Ada")] "Goto")
577 (funcall (symbol-function 'add-menu-button
)
578 menu-list
["Build" ada-compile-application t
] "Goto")
579 (funcall (symbol-function 'add-menu-button
)
580 menu-list
["Run" ada-run-application t
] "Goto")
581 (funcall (symbol-function 'add-menu-button
)
582 menu-list
["Debug" ada-gdb-application t
] "Goto")
583 (funcall (symbol-function 'add-menu-button
)
584 menu-list
["--" nil t
] "Goto")
585 (funcall (symbol-function 'add-menu-button
)
586 goto-menu
["Goto Parent Unit" ada-goto-parent t
]
587 "Next compilation error")
588 (funcall (symbol-function 'add-menu-button
)
589 goto-menu
["Goto References to any entity"
590 ada-find-any-references t
]
591 "Next compilation error")
592 (funcall (symbol-function 'add-menu-button
)
593 goto-menu
["List References" ada-find-references t
]
594 "Next compilation error")
595 (funcall (symbol-function 'add-menu-button
)
596 goto-menu
["List Local References" ada-find-local-references t
]
597 "Next compilation error")
598 (funcall (symbol-function 'add-menu-button
)
599 goto-menu
["Goto Declaration Other Frame"
600 ada-goto-declaration-other-frame t
]
601 "Next compilation error")
602 (funcall (symbol-function 'add-menu-button
)
603 goto-menu
["Goto Declaration/Body"
604 ada-goto-declaration t
]
605 "Next compilation error")
606 (funcall (symbol-function 'add-menu-button
)
607 goto-menu
["Goto Previous Reference"
608 ada-xref-goto-previous-reference t
]
609 "Next compilation error")
610 (funcall (symbol-function 'add-menu-button
)
611 goto-menu
["--" nil t
] "Next compilation error")
612 (funcall (symbol-function 'add-menu-button
)
613 edit-menu
["Complete Identifier"
614 ada-complete-identifier t
]
616 (funcall (symbol-function 'add-menu-button
)
617 edit-menu
["--------" nil t
] "Indent Line")
618 (funcall (symbol-function 'add-menu-button
)
619 help-menu
["Gnat User Guide" (info "gnat_ug")])
620 (funcall (symbol-function 'add-menu-button
)
621 help-menu
["Gnat Reference Manual" (info "gnat_rm")])
622 (funcall (symbol-function 'add-menu-button
)
623 help-menu
["Gcc Documentation" (info "gcc")])
624 (funcall (symbol-function 'add-menu-button
)
625 help-menu
["Gdb Documentation" (info "gdb")])
626 (funcall (symbol-function 'add-menu-button
)
627 help-menu
["Ada95 Reference Manual" (info "arm95")])
628 (funcall (symbol-function 'add-menu-button
)
630 ["Show Cross-References in Other Buffer"
631 (setq ada-xref-other-buffer
632 (not ada-xref-other-buffer
))
633 :style toggle
:selected ada-xref-other-buffer
])
634 (funcall (symbol-function 'add-menu-button
)
636 ["Automatically Recompile for Cross-References"
637 (setq ada-xref-create-ali
(not ada-xref-create-ali
))
638 :style toggle
:selected ada-xref-create-ali
])
639 (funcall (symbol-function 'add-menu-button
)
642 (setq ada-xref-confirm-compile
643 (not ada-xref-confirm-compile
))
644 :style toggle
:selected ada-xref-confirm-compile
])
645 (if (string-match "gvd" ada-prj-default-debugger
)
646 (funcall (symbol-function 'add-menu-button
)
648 ["Tight Integration With Gnu Visual Debugger"
649 (setq ada-tight-gvd-integration
650 (not ada-tight-gvd-integration
))
651 :style toggle
:selected ada-tight-gvd-integration
]))
655 (let* ((menu (or (lookup-key ada-mode-map
[menu-bar Ada
])
656 ;; Emacs-21.4's easymenu.el downcases the events.
657 (lookup-key ada-mode-map
[menu-bar ada
])))
658 (edit-menu (or (lookup-key menu
[Edit]) (lookup-key menu [edit])))
659 (help-menu (or (lookup-key menu [Help]) (lookup-key menu [help])))
660 (goto-menu (or (lookup-key menu [Goto]) (lookup-key menu [goto])))
661 (options-menu (or (lookup-key menu [Options])
662 (lookup-key menu [options]))))
664 (define-key-after menu [Check] '("Check file" . ada-check-current)
666 (define-key-after menu [Compile] '("Compile file" . ada-compile-current)
668 (define-key-after menu [Build] '("Build" . ada-compile-application)
670 (define-key-after menu [Run] '("Run" . ada-run-application) 'Build)
671 (define-key-after menu [Debug] '("Debug" . ada-gdb-application) 'Run)
672 (define-key-after menu [rem] '("--" . nil) 'Debug)
673 (define-key-after menu [Project]
674 (cons "Project" (make-sparse-keymap)) 'rem)
676 (define-key help-menu [Gnat_ug]
677 '("Gnat User Guide" . (lambda() (interactive) (info "gnat_ug"))))
678 (define-key help-menu [Gnat_rm]
679 '("Gnat Reference Manual" . (lambda() (interactive) (info "gnat_rm"))))
680 (define-key help-menu [Gcc]
681 '("Gcc Documentation" . (lambda() (interactive) (info "gcc"))))
682 (define-key help-menu [gdb]
683 '("Gdb Documentation" . (lambda() (interactive) (info "gdb"))))
684 (define-key help-menu [arm95]
685 '("Ada95 Reference Manual" . (lambda() (interactive) (info "arm95"))))
687 (define-key goto-menu [rem] '("----" . nil))
688 (define-key goto-menu [Parent] '("Goto Parent Unit"
690 (define-key goto-menu [References-any]
691 '("Goto References to any entity" . ada-find-any-references))
692 (define-key goto-menu [References]
693 '("List References" . ada-find-references))
694 (define-key goto-menu [Local-References]
695 '("List Local References" . ada-find-local-references))
696 (define-key goto-menu [Prev]
697 '("Goto Previous Reference" . ada-xref-goto-previous-reference))
698 (define-key goto-menu [Decl-other]
699 '("Goto Declaration Other Frame" . ada-goto-declaration-other-frame))
700 (define-key goto-menu [Decl]
701 '("Goto Declaration/Body" . ada-goto-declaration))
703 (define-key edit-menu [rem] '("----" . nil))
704 (define-key edit-menu [Complete] '("Complete Identifier"
705 . ada-complete-identifier))
707 (define-key-after options-menu [xrefrecompile]
708 '(menu-item "Automatically Recompile for Cross-References"
709 (lambda()(interactive)
710 (setq ada-xref-create-ali (not ada-xref-create-ali)))
711 :button (:toggle . ada-xref-create-ali)) t)
712 (define-key-after options-menu [xrefconfirm]
713 '(menu-item "Confirm Commands"
714 (lambda()(interactive)
715 (setq ada-xref-confirm-compile
716 (not ada-xref-confirm-compile)))
717 :button (:toggle . ada-xref-confirm-compile)) t)
718 (define-key-after options-menu [xrefother]
719 '(menu-item "Show Cross-References in Other Buffer"
720 (lambda()(interactive)
721 (setq ada-xref-other-buffer (not ada-xref-other-buffer)))
722 :button (:toggle . ada-xref-other-buffer)) t)
724 (if (string-match "gvd" ada-prj-default-debugger)
725 (define-key-after options-menu [tightgvd]
726 '(menu-item "Tight Integration With Gnu Visual Debugger"
727 (lambda()(interactive)
728 (setq ada-tight-gvd-integration
729 (not ada-tight-gvd-integration)))
730 :button (:toggle . ada-tight-gvd-integration)) t))
732 (define-key edit-menu [rem3] '("------------" . nil))
733 (define-key edit-menu [open-file-from-src-path]
734 '("Search File on source path..." . ada-find-file))
737 (ada-xref-update-project-menu)
740 ;; ----- Utilities -------------------------------------------------
742 (defun ada-require-project-file ()
743 "If no project file is currently active, load a default one."
744 (if (or (not ada-prj-default-project-file)
745 (not ada-xref-project-files)
746 (string= ada-prj-default-project-file ""))
747 (ada-reread-prj-file)))
749 (defun ada-xref-push-pos (filename position)
750 "Push (FILENAME, POSITION) on the position ring for cross-references."
751 (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
752 (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
753 (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
755 (defun ada-xref-goto-previous-reference ()
756 "Go to the previous cross-reference we were on."
758 (if ada-xref-pos-ring
759 (let ((pos (car ada-xref-pos-ring)))
760 (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
761 (find-file (car (cdr pos)))
762 (goto-char (car pos)))))
764 (defun ada-convert-file-name (name)
765 "Converts from NAME to a name that can be used by the compilation commands.
766 This is overriden on VMS to convert from VMS filenames to Unix filenames."
769 (defun ada-set-default-project-file (name)
770 "Set the file whose name is NAME as the default project file."
771 (interactive "fProject file:")
772 (setq ada-prj-default-project-file name)
773 (ada-reread-prj-file name)
776 ;; ------ Handling the project file -----------------------------
778 (defun ada-prj-find-prj-file (&optional no-user-question)
779 "Find the prj file associated with the current buffer.
780 If NO-USER-QUESTION is non-nil, use a default file if not project file was
781 found, and do not ask the user.
782 If the buffer is not an Ada buffer, associate it with the default project
783 file. If none is set, return nil."
787 ;; Use the active project file if there is one.
788 ;; This is also valid if we don't currently have an Ada buffer, or if
789 ;; the current buffer is not a real file (for instance an emerge buffer)
791 (if (or (not (string= mode-name "Ada"))
792 (not (buffer-file-name))
793 (and ada-prj-default-project-file
794 (not (string= ada-prj-default-project-file ""))))
795 (set 'selected ada-prj-default-project-file)
797 ;; other cases: use a more complex algorithm
799 (let* ((current-file (buffer-file-name))
800 (first-choice (concat
801 (file-name-sans-extension current-file)
802 ada-project-file-extension))
803 (dir (file-name-directory current-file))
805 ;; on Emacs 20.2, directory-files does not work if
806 ;; parse-sexp-lookup-properties is set
807 (parse-sexp-lookup-properties nil)
808 (prj-files (directory-files
810 (concat ".*" (regexp-quote
811 ada-project-file-extension) "$")))
816 ;; Else if there is a project file with the same name as the Ada
817 ;; file, but not the same extension.
818 ((file-exists-p first-choice)
819 (set 'selected first-choice))
821 ;; Else if only one project file was found in the current directory
822 ((= (length prj-files) 1)
823 (set 'selected (car prj-files)))
825 ;; Else if there are multiple files, ask the user
826 ((and (> (length prj-files) 1) (not no-user-question))
827 (save-window-excursion
828 (with-output-to-temp-buffer "*choice list*"
829 (princ "There are more than one possible project file.\n")
830 (princ "Which one should we use ?\n\n")
831 (princ " no. file name \n")
832 (princ " --- ------------------------\n")
834 (while (<= counter (length prj-files))
835 (princ (format " %2d) %s\n"
837 (nth (1- counter) prj-files)))
838 (setq counter (1+ counter))
839 ))) ; end of with-output-to ...
843 (not (integerp choice))
845 (> choice (length prj-files)))
846 (setq choice (string-to-int
847 (read-from-minibuffer "Enter No. of your choice: "))))
848 (set 'selected (nth (1- choice) prj-files))))
850 ;; Else if no project file was found in the directory, ask a name
851 ;; to the user, using as a default value the last one entered by
853 ((= (length prj-files) 0)
854 (unless (or no-user-question (not ada-always-ask-project))
855 (setq ada-last-prj-file
857 (concat "project file [" ada-last-prj-file "]:")
858 nil ada-last-prj-file))
859 (unless (string= ada-last-prj-file "")
860 (set 'selected ada-last-prj-file))))
866 (defun ada-parse-prj-file (prj-file)
867 "Reads and parses the PRJ-FILE file if it was found.
868 The current buffer should be the ada-file buffer."
870 (let (project src_dir obj_dir make_cmd comp_cmd check_cmd casing
871 run_cmd debug_pre_cmd debug_post_cmd
872 (ada-buffer (current-buffer)))
873 (setq prj-file (expand-file-name prj-file))
875 ;; Initialize the project with the default values
876 (ada-xref-set-default-prj-values 'project (current-buffer))
878 ;; Do not use find-file below, since we don't want to show this
879 ;; buffer. If the file is open through speedbar, we can't use
880 ;; find-file anyway, since the speedbar frame is special and does not
881 ;; allow the selection of a file in it.
883 (let* ((buffer (run-hook-with-args-until-success
884 'ada-load-project-hook prj-file)))
886 (setq buffer (find-file-noselect prj-file nil)))
890 (goto-char (point-min))
892 ;; Now overrides these values with the project file
894 (if (looking-at "^\\([^=]+\\)=\\(.*\\)")
896 ((string= (match-string 1) "src_dir")
897 (add-to-list 'src_dir
898 (file-name-as-directory (match-string 2))))
899 ((string= (match-string 1) "obj_dir")
900 (add-to-list 'obj_dir
901 (file-name-as-directory (match-string 2))))
902 ((string= (match-string 1) "casing")
903 (set 'casing (cons (match-string 2) casing)))
904 ((string= (match-string 1) "build_dir")
906 (plist-put project 'build_dir
907 (file-name-as-directory (match-string 2)))))
908 ((string= (match-string 1) "make_cmd")
909 (add-to-list 'make_cmd (match-string 2)))
910 ((string= (match-string 1) "comp_cmd")
911 (add-to-list 'comp_cmd (match-string 2)))
912 ((string= (match-string 1) "check_cmd")
913 (add-to-list 'check_cmd (match-string 2)))
914 ((string= (match-string 1) "run_cmd")
915 (add-to-list 'run_cmd (match-string 2)))
916 ((string= (match-string 1) "debug_pre_cmd")
917 (add-to-list 'debug_pre_cmd (match-string 2)))
918 ((string= (match-string 1) "debug_post_cmd")
919 (add-to-list 'debug_post_cmd (match-string 2)))
921 (set 'project (plist-put project (intern (match-string 1))
922 (match-string 2))))))
925 (if src_dir (set 'project (plist-put project 'src_dir
927 (if obj_dir (set 'project (plist-put project 'obj_dir
929 (if casing (set 'project (plist-put project 'casing
931 (if make_cmd (set 'project (plist-put project 'make_cmd
932 (reverse make_cmd))))
933 (if comp_cmd (set 'project (plist-put project 'comp_cmd
934 (reverse comp_cmd))))
935 (if check_cmd (set 'project (plist-put project 'check_cmd
936 (reverse check_cmd))))
937 (if run_cmd (set 'project (plist-put project 'run_cmd
939 (set 'project (plist-put project 'debug_post_cmd
940 (reverse debug_post_cmd)))
941 (set 'project (plist-put project 'debug_pre_cmd
942 (reverse debug_pre_cmd)))
944 ;; Delete the default project file from the list, if it is there.
945 ;; Note that in that case, this default project is the only one in
947 (if (assoc nil ada-xref-project-files)
948 (setq ada-xref-project-files nil))
950 ;; Memorize the newly read project file
951 (if (assoc prj-file ada-xref-project-files)
952 (setcdr (assoc prj-file ada-xref-project-files) project)
953 (add-to-list 'ada-xref-project-files (cons prj-file project)))
955 ;; Set the project file as the active one.
956 (setq ada-prj-default-project-file prj-file)
958 ;; Sets up the compilation-search-path so that Emacs is able to
959 ;; go to the source of the errors in a compilation buffer
960 (setq compilation-search-path (ada-xref-get-src-dir-field))
962 ;; Set the casing exceptions file list
965 (setq ada-case-exception-file (reverse casing))
966 (ada-case-read-exceptions)))
968 ;; Add the directories to the search path for ff-find-other-file
969 ;; Do not add the '/' or '\' at the end
970 (setq ada-search-directories
971 (append (mapcar 'directory-file-name compilation-search-path)
972 ada-search-directories))
974 ;; Kill the project buffer
976 (set-buffer ada-buffer)
978 (ada-xref-update-project-menu)
981 ;; No prj file ? => Setup default values
982 ;; Note that nil means that all compilation modes will first look in the
983 ;; current directory, and only then in the current file's directory. This
984 ;; current file is assumed at this point to be in the common source
986 (setq compilation-search-path (list nil default-directory))
990 (defun ada-find-references (&optional pos arg local-only)
991 "Find all references to the entity under POS.
992 Calls gnatfind to find the references.
993 if ARG is t, the contents of the old *gnatfind* buffer is preserved.
994 if LOCAL-ONLY is t, only the declarations in the current file are returned."
997 (ada-require-project-file)
999 (let* ((identlist (ada-read-identifier pos))
1000 (alifile (ada-get-ali-file-name (ada-file-of identlist)))
1001 (process-environment (ada-set-environment)))
1003 (set-buffer (get-file-buffer (ada-file-of identlist)))
1005 ;; if the file is more recent than the executable
1006 (if (or (buffer-modified-p (current-buffer))
1007 (file-newer-than-file-p (ada-file-of identlist) alifile))
1008 (ada-find-any-references (ada-name-of identlist)
1009 (ada-file-of identlist)
1010 nil nil local-only arg)
1011 (ada-find-any-references (ada-name-of identlist)
1012 (ada-file-of identlist)
1013 (ada-line-of identlist)
1014 (ada-column-of identlist) local-only arg)))
1017 (defun ada-find-local-references (&optional pos arg)
1018 "Find all references to the entity under POS.
1019 Calls gnatfind to find the references.
1020 if ARG is t, the contents of the old *gnatfind* buffer is preserved."
1023 (ada-find-references pos arg t))
1025 (defun ada-find-any-references
1026 (entity &optional file line column local-only append)
1027 "Search for references to any entity whose name is ENTITY.
1028 ENTITY was first found the location given by FILE, LINE and COLUMN.
1029 If LOCAL-ONLY is t, then only the references in file will be listed, which
1031 If APPEND is t, then the output of the command will be append to the existing
1032 buffer *gnatfind* if it exists."
1033 (interactive "sEntity name: ")
1034 (ada-require-project-file)
1036 ;; Prepare the gnatfind command. Note that we must protect the quotes
1037 ;; around operators, so that they are correctly handled and can be
1038 ;; processed (gnatfind \"+\":...).
1039 (let* ((quote-entity
1040 (if (= (aref entity 0) ?\")
1042 (concat "\\\"" (substring entity 1 -1) "\\\"")
1043 (concat "'\"" (substring entity 1 -1) "\"'"))
1045 (switches (ada-xref-get-project-field 'gnatfind_opt))
1046 (command (concat "gnatfind " switches " "
1048 (if file (concat ":" (file-name-nondirectory file)))
1049 (if line (concat ":" line))
1050 (if column (concat ":" column))
1051 (if local-only (concat " " (file-name-nondirectory file)))
1055 ;; If a project file is defined, use it
1056 (if (and ada-prj-default-project-file
1057 (not (string= ada-prj-default-project-file "")))
1058 (setq command (concat command " -p" ada-prj-default-project-file)))
1060 (if (and append (get-buffer "*gnatfind*"))
1062 (set-buffer "*gnatfind*")
1063 (setq old-contents (buffer-string))))
1065 (compile-internal command "No more references" "gnatfind")
1067 ;; Hide the "Compilation" menu
1069 (set-buffer "*gnatfind*")
1070 (local-unset-key [menu-bar compilation-menu])
1075 (insert old-contents)
1076 (goto-char (point-max)))))
1080 (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
1082 ;; ----- Identlist manipulation -------------------------------------------
1083 ;; An identlist is a vector that is used internally to reference an identifier
1084 ;; To facilitate its use, we provide the following macros
1086 (defmacro ada-make-identlist () (make-vector 8 nil))
1087 (defmacro ada-name-of (identlist) (list 'aref identlist 0))
1088 (defmacro ada-line-of (identlist) (list 'aref identlist 1))
1089 (defmacro ada-column-of (identlist) (list 'aref identlist 2))
1090 (defmacro ada-file-of (identlist) (list 'aref identlist 3))
1091 (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
1092 (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
1093 (defmacro ada-references-of (identlist) (list 'aref identlist 6))
1094 (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
1096 (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
1097 (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
1098 (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
1099 (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
1100 (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
1101 (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
1102 (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
1103 (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
1105 (defsubst ada-get-ali-buffer (file)
1106 "Reads the ali file into a new buffer, and returns this buffer's name"
1107 (find-file-noselect (ada-get-ali-file-name file)))
1111 ;; ----- Identifier Completion --------------------------------------------
1112 (defun ada-complete-identifier (pos)
1113 "Tries to complete the identifier around POS.
1114 The feature is only available if the files where compiled not using the -gnatx
1117 (ada-require-project-file)
1119 ;; Initialize function-local variables and jump to the .ali buffer
1120 ;; Note that for regexp search is case insensitive too
1121 (let* ((curbuf (current-buffer))
1122 (identlist (ada-read-identifier pos))
1123 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
1124 (regexp-quote (ada-name-of identlist))
1125 "[a-zA-Z0-9_]*\\)"))
1129 ;; Open the .ali file
1130 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
1131 (goto-char (point-max))
1133 ;; build an alist of possible completions
1134 (while (re-search-backward sofar nil t)
1135 (setq symalist (cons (cons (match-string 1) nil) symalist)))
1137 (setq completed (try-completion "" symalist))
1139 ;; kills .ali buffer
1142 ;; deletes the incomplete identifier in the buffer
1144 (looking-at "[a-zA-Z0-9_]+")
1146 ;; inserts the completed symbol
1150 ;; ----- Cross-referencing ----------------------------------------
1152 (defun ada-point-and-xref ()
1153 "Calls `mouse-set-point' and then `ada-goto-declaration'."
1155 (mouse-set-point last-input-event)
1156 (ada-goto-declaration (point)))
1158 (defun ada-goto-declaration (pos &optional other-frame)
1159 "Display the declaration of the identifier around POS.
1160 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1162 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1164 (ada-require-project-file)
1166 (ada-xref-push-pos (buffer-file-name) pos)
1168 ;; First try the standard algorithm by looking into the .ali file, but if
1169 ;; that file was too old or even did not exist, try to look in the whole
1170 ;; object path for a possible location.
1171 (let ((identlist (ada-read-identifier pos)))
1173 (ada-find-in-ali identlist other-frame)
1175 (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist))))
1177 ;; If the ALI file was up-to-date, then we probably have a predefined
1178 ;; entity, whose references are not given by GNAT
1179 (if (and (file-exists-p ali-file)
1180 (file-newer-than-file-p ali-file (ada-file-of identlist)))
1181 (message "No cross-reference found. It might be a predefined entity.")
1183 ;; Else, look in every ALI file, except if the user doesn't want that
1184 (if ada-xref-search-with-egrep
1185 (ada-find-in-src-path identlist other-frame)
1186 (message "Cross-referencing information is not up-to-date. Please recompile.")
1189 (defun ada-goto-declaration-other-frame (pos &optional other-frame)
1190 "Display the declaration of the identifier around POS.
1191 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1193 (ada-goto-declaration pos t))
1195 (defun ada-remote (command)
1196 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1197 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1198 (if (or (not machine) (string= machine ""))
1200 (format "%s %s '(%s)'"
1201 remote-shell-program
1205 (defun ada-get-absolute-dir-list (dir-list root-dir)
1206 "Returns the list of absolute directories found in dir-list.
1207 If a directory is a relative directory, the value of ROOT-DIR is added in
1209 (mapcar (lambda (x) (expand-file-name x root-dir)) dir-list))
1211 (defun ada-set-environment ()
1212 "Return the new value for process-environment.
1213 It modifies the source path and object path with the values found in the
1215 (let ((include (getenv "ADA_INCLUDE_PATH"))
1216 (objects (getenv "ADA_OBJECTS_PATH"))
1217 (build-dir (ada-xref-get-project-field 'build_dir)))
1219 (set 'include (concat path-separator include)))
1221 (set 'objects (concat path-separator objects)))
1223 (concat "ADA_INCLUDE_PATH="
1224 (mapconcat (lambda(x) (expand-file-name x build-dir))
1225 (ada-xref-get-project-field 'src_dir)
1229 (concat "ADA_OBJECTS_PATH="
1230 (mapconcat (lambda(x) (expand-file-name x build-dir))
1231 (ada-xref-get-project-field 'obj_dir)
1234 process-environment))))
1236 (defun ada-compile-application (&optional arg)
1237 "Compiles the application, using the command found in the project file.
1238 If ARG is not nil, ask for user confirmation."
1240 (ada-require-project-file)
1241 (let ((cmd (ada-xref-get-project-field 'make_cmd))
1242 (process-environment (ada-set-environment))
1243 (compilation-scroll-output t))
1245 (setq compilation-search-path (ada-xref-get-src-dir-field))
1247 ;; If no project file was found, ask the user
1249 (setq cmd '("") arg t))
1251 ;; Make a single command from the list of commands, including the
1252 ;; commands to run it on a remote machine.
1253 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1255 (if (or ada-xref-confirm-compile arg)
1256 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1258 ;; Insert newlines so as to separate the name of the commands to run
1259 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1260 ;; which gets confused by newline characters.
1261 (if (not (string-match "cmdproxy.exe" shell-file-name))
1262 (setq cmd (concat cmd "\n\n")))
1264 (compile (ada-quote-cmd cmd))))
1266 (defun ada-compile-current (&optional arg prj-field)
1267 "Recompile the current file.
1268 If ARG is not nil, ask for user confirmation of the command.
1269 PRJ-FIELD is the name of the field to use in the project file to get the
1270 command, and should be either comp_cmd (default) or check_cmd."
1272 (ada-require-project-file)
1273 (let* ((field (if prj-field prj-field 'comp_cmd))
1274 (cmd (ada-xref-get-project-field field))
1275 (process-environment (ada-set-environment))
1276 (compilation-scroll-output t))
1278 (setq compilation-search-path (ada-xref-get-src-dir-field))
1281 (setq cmd '("") arg t))
1283 ;; Make a single command from the list of commands, including the
1284 ;; commands to run it on a remote machine.
1285 (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
1287 ;; If no project file was found, ask the user
1288 (if (or ada-xref-confirm-compile arg)
1289 (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
1291 ;; Insert newlines so as to separate the name of the commands to run
1292 ;; and the output of the commands. this doesn't work with cmdproxy.exe,
1293 ;; which gets confused by newline characters.
1294 (if (not (string-match "cmdproxy.exe" shell-file-name))
1295 (setq cmd (concat cmd "\n\n")))
1297 (compile (ada-quote-cmd cmd))))
1299 (defun ada-check-current (&optional arg)
1300 "Recompile the current file.
1301 If ARG is not nil, ask for user confirmation of the command."
1303 (ada-compile-current arg 'check_cmd))
1305 (defun ada-run-application (&optional arg)
1306 "Run the application.
1307 if ARG is not-nil, asks for user confirmation."
1309 (ada-require-project-file)
1311 (let ((machine (ada-xref-get-project-field 'cross_prefix)))
1312 (if (and machine (not (string= machine "")))
1313 (error "This feature is not supported yet for cross environments")))
1315 (let ((command (ada-xref-get-project-field 'run_cmd)))
1317 ;; Guess the command if it wasn't specified
1319 (set 'command (list (file-name-sans-extension (buffer-name)))))
1321 ;; Modify the command to run remotely
1322 (setq command (ada-remote (mapconcat 'identity command
1323 ada-command-separator)))
1325 ;; Ask for the arguments to the command if required
1326 (if (or ada-xref-confirm-compile arg)
1327 (setq command (read-from-minibuffer "Enter command to execute: "
1332 (set-buffer (get-buffer-create "*run*"))
1333 (set 'buffer-read-only nil)
1336 (start-process "run" (current-buffer) shell-file-name
1339 ;; Set these two variables to their default values, since otherwise
1340 ;; the output buffer is scrolled so that only the last output line
1341 ;; is visible at the top of the buffer.
1342 (set (make-local-variable 'scroll-step) 0)
1343 (set (make-local-variable 'scroll-conservatively) 0)
1345 (display-buffer "*run*")
1347 ;; change to buffer *run* for interactive programs
1349 (switch-to-buffer "*run*")
1352 (defun ada-gdb-application (&optional arg executable-name)
1353 "Start the debugger on the application.
1354 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1356 If ARG is non-nil, ask the user to confirm the command."
1358 (let ((buffer (current-buffer))
1359 cmd pre-cmd post-cmd)
1360 (ada-require-project-file)
1361 (setq cmd (if executable-name
1362 (concat ada-prj-default-debugger " " executable-name)
1363 (ada-xref-get-project-field 'debug_cmd))
1364 pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
1365 post-cmd (ada-xref-get-project-field 'debug_post_cmd))
1367 ;; If the command was not given in the project file, start a bare gdb
1369 (set 'cmd (concat ada-prj-default-debugger
1372 (file-name-sans-extension (buffer-file-name))))))
1374 ;; For gvd, add an extra switch so that the Emacs window is completly
1375 ;; swallowed inside the Gvd one
1376 (if (and ada-tight-gvd-integration
1377 (string-match "^[^ \t]*gvd" cmd))
1378 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1379 ;; We make sure that gvd swallows the new frame, not the one the
1380 ;; user has been using until now
1381 ;; The frame is made invisible initially, so that GtkPlug gets a
1382 ;; chance to fully manage it. Then it works fine with Enlightenment
1384 (let ((frame (make-frame '((visibility . nil)))))
1386 cmd " --editor-window="
1387 (cdr (assoc 'outer-window-id (frame-parameters frame)))))
1388 (select-frame frame)))
1390 ;; Add a -fullname switch
1391 ;; Use the remote machine
1392 (set 'cmd (ada-remote (concat cmd " -fullname ")))
1394 ;; Ask for confirmation if required
1395 (if (or arg ada-xref-confirm-compile)
1396 (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
1400 gud-gdb-massage-args)
1402 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1403 (fset 'gud-gdb-massage-args (lambda (file args) args))
1405 (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
1406 (if (not (equal pre-cmd ""))
1407 (setq pre-cmd (concat pre-cmd ada-command-separator)))
1409 (set 'post-cmd (mapconcat 'identity post-cmd "\n"))
1411 (set 'post-cmd (concat post-cmd "\n")))
1413 ;; Temporarily replaces the definition of `comint-exec' so that we
1414 ;; can execute commands before running gdb.
1416 `(lambda (buffer name command startfile switches)
1417 (let (compilation-buffer-name-function)
1419 (set 'compilation-buffer-name-function
1420 (lambda(x) (buffer-name buffer)))
1421 (compile (ada-quote-cmd
1424 (mapconcat 'identity switches " "))))))
1427 ;; Tight integration should force the tty mode
1428 (if (and (string-match "gvd" (comint-arguments cmd 0 0))
1429 ada-tight-gvd-integration
1430 (not (string-match "--tty" cmd)))
1431 (setq cmd (concat cmd "--tty")))
1433 (if (and (string-match "jdb" (comint-arguments cmd 0 0))
1435 (funcall (symbol-function 'jdb) cmd)
1438 ;; Send post-commands to the debugger
1439 (process-send-string (get-buffer-process (current-buffer)) post-cmd)
1441 ;; Move to the end of the debugger buffer, so that it is automatically
1442 ;; scrolled from then on.
1445 ;; Display both the source window and the debugger window (the former
1446 ;; above the latter). No need to show the debugger window unless it
1447 ;; is going to have some relevant information.
1448 (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
1449 (string-match "--tty" cmd))
1450 (split-window-vertically))
1451 (switch-to-buffer buffer)
1455 (defun ada-reread-prj-file (&optional filename)
1456 "Forces Emacs to read either FILENAME or the project file associated
1457 with the current buffer.
1458 Otherwise, this file is only read once, and never read again.
1459 Since the information in the project file is shared between all buffers, this
1460 automatically modifies the setup for all the Ada buffer that use this file."
1463 (ada-parse-prj-file filename)
1464 (ada-parse-prj-file (ada-prj-find-prj-file)))
1466 ;; Reread the location of the standard runtime library
1467 (ada-initialize-runtime-library
1468 (or (ada-xref-get-project-field 'cross-prefix) ""))
1471 ;; ------ Private routines
1473 (defun ada-xref-current (file &optional ali-file-name)
1474 "Update the cross-references for FILE.
1475 This in fact recompiles FILE to create ALI-FILE-NAME.
1476 This function returns the name of the file that was recompiled to generate
1477 the cross-reference information. Note that the ali file can then be deduced by
1478 replacing the file extension with .ali"
1480 (if (and ali-file-name
1481 (get-file-buffer ali-file-name))
1482 (kill-buffer (get-file-buffer ali-file-name)))
1484 (let* ((name (ada-convert-file-name file))
1485 (body-name (or (ada-get-body-name name) name)))
1487 ;; Always recompile the body when we can. We thus temporarily switch to a
1488 ;; buffer than contains the body of the unit
1490 (let ((body-visible (find-buffer-visiting body-name))
1493 (set-buffer body-visible)
1494 (find-file body-name))
1496 ;; Execute the compilation. Note that we must wait for the end of the
1497 ;; process, or the ALI file would still not be available.
1498 ;; Unfortunately, the underlying `compile' command that we use is
1500 (ada-compile-current)
1501 (setq process (get-buffer-process "*compilation*"))
1504 (not (equal (process-status process) 'exit)))
1507 ;; remove the buffer for the body if it wasn't there before
1508 (unless body-visible
1509 (kill-buffer (find-buffer-visiting body-name)))
1513 (defun ada-find-file-in-dir (file dir-list)
1514 "Search for FILE in DIR-LIST."
1516 (while (and (not found) dir-list)
1517 (set 'found (concat (file-name-as-directory (car dir-list))
1518 (file-name-nondirectory file)))
1520 (unless (file-exists-p found)
1522 (set 'dir-list (cdr dir-list)))
1525 (defun ada-find-ali-file-in-dir (file)
1526 "Find an .ali file in obj_dir. The current buffer must be the Ada file.
1527 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1528 and the standard runtime location at the end."
1529 (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
1531 (defun ada-find-src-file-in-dir (file)
1532 "Find a source file in src_dir. The current buffer must be the Ada file.
1533 Adds src_dir in front of the search path to conform to gnatmake's behavior,
1534 and the standard runtime location at the end."
1535 (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
1537 (defun ada-get-ali-file-name (file)
1538 "Create the ali file name for the ada-file FILE.
1539 The file is searched for in every directory shown in the obj_dir lines of
1542 ;; This function has to handle the special case of non-standard
1543 ;; file names (i.e. not .adb or .ads)
1544 ;; The trick is the following:
1545 ;; 1- replace the extension of the current file with .ali,
1546 ;; and look for this file
1547 ;; 2- If this file is found:
1548 ;; grep the "^U" lines, and make sure we are not reading the
1549 ;; .ali file for a spec file. If we are, go to step 3.
1550 ;; 3- If the file is not found or step 2 failed:
1551 ;; find the name of the "other file", ie the body, and look
1552 ;; for its associated .ali file by subtituing the extension
1554 ;; We must also handle the case of separate packages and subprograms:
1555 ;; 4- If no ali file was found, we try to modify the file name by removing
1556 ;; everything after the last '-' or '.' character, so as to get the
1557 ;; ali file for the parent unit. If we found an ali file, we check that
1558 ;; it indeed contains the definition for the separate entity by checking
1559 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1563 (set-buffer (get-file-buffer file))
1564 (let ((short-ali-file-name
1565 (concat (file-name-sans-extension (file-name-nondirectory file))
1570 ;; If we have a non-standard file name, and this is a spec, we first
1571 ;; look for the .ali file of the body, since this is the one that
1572 ;; contains the most complete information. If not found, we will do what
1573 ;; we can with the .ali file for the spec...
1575 (if (not (string= (file-name-extension file) "ads"))
1576 (let ((specs ada-spec-suffixes))
1578 (if (string-match (concat (regexp-quote (car specs)) "$")
1581 (set 'specs (cdr specs)))))
1585 (ada-find-ali-file-in-dir
1586 (concat (file-name-sans-extension
1587 (file-name-nondirectory
1588 (ada-other-file-name)))
1595 ;; Else we take the .ali file associated with the unit
1596 (ada-find-ali-file-in-dir short-ali-file-name)
1599 ;; else we did not find the .ali file Second chance: in case
1600 ;; the files do not have standard names (such as for instance
1601 ;; file_s.ada and file_b.ada), try to go to the other file
1602 ;; and look for its ali file
1603 (ada-find-ali-file-in-dir
1604 (concat (file-name-sans-extension
1605 (file-name-nondirectory (ada-other-file-name)))
1609 ;; If we still don't have an ali file, try to get the one
1610 ;; from the parent unit, in case we have a separate entity.
1611 (let ((parent-name (file-name-sans-extension
1612 (file-name-nondirectory file))))
1614 (while (and (not ali-file-name)
1615 (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
1617 (set 'parent-name (match-string 1 parent-name))
1618 (set 'ali-file-name (ada-find-ali-file-in-dir
1619 (concat parent-name ".ali")))
1623 ;; If still not found, try to recompile the file
1624 (if (not ali-file-name)
1625 ;; recompile only if the user asked for this. and search the ali
1626 ;; filename again. We avoid a possible infinite recursion by
1627 ;; temporarily disabling the automatic compilation.
1629 (if ada-xref-create-ali
1631 (concat (file-name-sans-extension (ada-xref-current file))
1634 (error "Ali file not found. Recompile your file"))
1637 ;; same if the .ali file is too old and we must recompile it
1638 (if (and (file-newer-than-file-p file ali-file-name)
1639 ada-xref-create-ali)
1640 (ada-xref-current file ali-file-name)))
1642 ;; Always return the correct absolute file name
1643 (expand-file-name ali-file-name))
1646 (defun ada-get-ada-file-name (file original-file)
1647 "Create the complete file name (+directory) for FILE.
1648 The original file (where the user was) is ORIGINAL-FILE. Search in project
1649 file for possible paths."
1653 ;; If the buffer for original-file, use it to get the values from the
1654 ;; project file, otherwise load the file and its project file
1655 (let ((buffer (get-file-buffer original-file)))
1658 (find-file original-file)
1659 (ada-require-project-file)))
1661 ;; we choose the first possible completion and we
1662 ;; return the absolute file name
1663 (let ((filename (ada-find-src-file-in-dir file)))
1665 (expand-file-name filename)
1667 (file-name-nondirectory file)
1668 " not found in src_dir. Please check your project file")))
1672 (defun ada-find-file-number-in-ali (file)
1673 "Returns the file number for FILE in the associated ali file."
1674 (set-buffer (ada-get-ali-buffer file))
1675 (goto-char (point-min))
1677 (let ((begin (re-search-forward "^D")))
1679 (re-search-forward (concat "^D " (file-name-nondirectory file)))
1680 (count-lines begin (point))))
1682 (defun ada-read-identifier (pos)
1683 "Returns the identlist around POS and switch to the .ali buffer.
1684 The returned list represents the entity, and can be manipulated through the
1685 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1687 ;; If at end of buffer (e.g the buffer is empty), error
1688 (if (>= (point) (point-max))
1689 (error "No identifier on point"))
1691 ;; goto first character of the identifier/operator (skip backward < and >
1692 ;; since they are part of multiple character operators
1694 (skip-chars-backward "a-zA-Z0-9_<>")
1696 ;; check if it really is an identifier
1697 (if (ada-in-comment-p)
1698 (error "Inside comment"))
1700 (let (identifier identlist)
1701 ;; Just in front of a string => we could have an operator declaration,
1702 ;; as in "+", "-", ..
1703 (if (= (char-after) ?\")
1706 ;; if looking at an operator
1707 ;; This is only true if:
1708 ;; - the symbol is +, -, ...
1709 ;; - the symbol is made of letters, and not followed by _ or a letter
1710 (if (and (looking-at ada-operator-re)
1711 (or (not (= (char-syntax (char-after)) ?w))
1712 (not (or (= (char-syntax (char-after (match-end 0))) ?w)
1713 (= (char-after (match-end 0)) ?_)))))
1715 (if (and (= (char-before) ?\")
1716 (= (char-after (+ (length (match-string 0)) (point))) ?\"))
1718 (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
1720 (if (ada-in-string-p)
1721 (error "Inside string or character constant"))
1722 (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
1723 (error "No cross-reference available for reserved keyword"))
1724 (if (looking-at "[a-zA-Z0-9_]+")
1725 (set 'identifier (match-string 0))
1726 (error "No identifier around")))
1728 ;; Build the identlist
1729 (set 'identlist (ada-make-identlist))
1730 (ada-set-name identlist (downcase identifier))
1731 (ada-set-line identlist
1732 (number-to-string (count-lines 1 (point))))
1733 (ada-set-column identlist
1734 (number-to-string (1+ (current-column))))
1735 (ada-set-file identlist (buffer-file-name))
1739 (defun ada-get-all-references (identlist)
1740 "Completes and returns IDENTLIST with the information extracted
1741 from the ali file (definition file and places where it is referenced)."
1743 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
1745 (set-buffer ali-buffer)
1746 (goto-char (point-min))
1747 (ada-set-on-declaration identlist nil)
1749 ;; First attempt: we might already be on the declaration of the identifier
1750 ;; We want to look for the declaration only in a definite interval (after
1751 ;; the "^X ..." line for the current file, and before the next "^X" line
1753 (if (re-search-forward
1754 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
1756 (let ((bound (save-excursion (re-search-forward "^X " nil t))))
1757 (set 'declaration-found
1759 (concat "^" (ada-line-of identlist)
1760 "." (ada-column-of identlist)
1761 "[ *]" (ada-name-of identlist)
1762 "[{\(<= ]?\\(.*\\)$") bound t))
1763 (if declaration-found
1764 (ada-set-on-declaration identlist t))
1767 ;; If declaration is still nil, then we were not on a declaration, and
1768 ;; have to fall back on other algorithms
1770 (unless declaration-found
1772 ;; Since we alread know the number of the file, search for a direct
1774 (goto-char (point-min))
1775 (set 'declaration-found t)
1778 (number-to-string (ada-find-file-number-in-ali
1779 (ada-file-of identlist))))
1780 (unless (re-search-forward (concat (ada-ali-index-of identlist)
1781 "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
1782 (ada-line-of identlist)
1784 (ada-column-of identlist) "\\>")
1787 ;; if we did not find it, it may be because the first reference
1788 ;; is not required to have a 'unit_number|' item included.
1789 ;; Or maybe we are already on the declaration...
1790 (unless (re-search-forward
1792 "^[0-9]+.[0-9]+[ *]"
1793 (ada-name-of identlist)
1794 "[ <{=\(]\\(.\\|\n\\.\\)*\\<"
1795 (ada-line-of identlist)
1797 (ada-column-of identlist) "\\>")
1800 ;; If still not found, then either the declaration is unknown
1801 ;; or the source file has been modified since the ali file was
1803 (set 'declaration-found nil)
1807 ;; Last check to be completly sure we have found the correct line (the
1808 ;; ali might not be up to date for instance)
1809 (if declaration-found
1812 ;; while we have a continuation line, go up one line
1813 (while (looking-at "^\\.")
1815 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1816 (ada-name-of identlist) "[ <{=\(]"))
1817 (set 'declaration-found nil))))
1819 ;; Still no success ! The ali file must be too old, and we need to
1820 ;; use a basic algorithm based on guesses. Note that this only happens
1821 ;; if the user does not want us to automatically recompile files
1823 (unless declaration-found
1824 (if (ada-xref-find-in-modified-ali identlist)
1825 (set 'declaration-found t)
1826 ;; no more idea to find the declaration. Give up
1828 (kill-buffer ali-buffer)
1829 (error (concat "No declaration of " (ada-name-of identlist)
1835 ;; Now that we have found a suitable line in the .ali file, get the
1836 ;; information available
1838 (if declaration-found
1839 (let ((current-line (buffer-substring
1840 (point) (save-excursion (end-of-line) (point)))))
1844 (while (looking-at "^\\.\\(.*\\)")
1845 (set 'current-line (concat current-line (match-string 1)))
1849 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
1851 ;; If we can find the file
1853 (ada-set-declare-file
1855 (ada-get-ada-file-name (match-string 1)
1856 (ada-file-of identlist)))
1858 ;; Else clean up the ali file
1860 (kill-buffer ali-buffer)
1861 (error (error-message-string err)))
1864 (ada-set-references identlist current-line)
1868 (defun ada-xref-find-in-modified-ali (identlist)
1869 "Find the matching position for IDENTLIST in the current ali buffer.
1870 This function is only called when the file was not up-to-date, so we need
1871 to make some guesses.
1872 This function is disabled for operators, and only works for identifiers."
1874 (unless (= (string-to-char (ada-name-of identlist)) ?\")
1876 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1877 (my-regexp (concat "[ *]"
1878 (regexp-quote (ada-name-of identlist)) " "))
1884 (ali-buffer (current-buffer)))
1886 (goto-char (point-max))
1887 (while (re-search-backward my-regexp nil t)
1889 (setq line-ali (count-lines 1 (point)))
1891 ;; have a look at the line and column numbers
1892 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1894 (setq line-ada (match-string 1))
1895 (setq col-ada (match-string 2)))
1896 (setq line-ada "--")
1899 ;; construct a list with the file names and the positions within
1900 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
1902 'declist (list line-ali (match-string 1) line-ada col-ada))
1907 ;; how many possible declarations have we found ?
1908 (setq len (length declist))
1912 (kill-buffer (current-buffer))
1913 (error (concat "No declaration of "
1914 (ada-name-of identlist)
1915 " recorded in .ali file")))
1917 ;; one => should be the right one
1919 (goto-line (caar declist)))
1921 ;; more than one => display choice list
1923 (save-window-excursion
1924 (with-output-to-temp-buffer "*choice list*"
1926 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1927 (princ "Possible declarations are:\n\n")
1928 (princ " no. in file at line col\n")
1929 (princ " --- --------------------- ---- ----\n")
1931 (while (< counter len)
1932 (princ (format " %2d) %-21s %4s %4s\n"
1934 (ada-get-ada-file-name
1935 (nth 1 (nth counter declist))
1936 (ada-file-of identlist))
1937 (nth 2 (nth counter declist))
1938 (nth 3 (nth counter declist))
1940 (setq counter (1+ counter))
1943 ) ; end of with-output-to ...
1947 (not (integerp choice))
1952 (read-from-minibuffer "Enter No. of your choice: "))))
1954 (set-buffer ali-buffer)
1955 (goto-line (car (nth (1- choice) declist)))
1959 (defun ada-find-in-ali (identlist &optional other-frame)
1960 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1961 If OTHER-FRAME is non nil, and `ada-xref-other-buffer' is non nil,
1962 opens a new window to show the declaration."
1964 (ada-get-all-references identlist)
1965 (let ((ali-line (ada-references-of identlist))
1970 ;; Note: in some cases, an entity can have multiple references to the
1971 ;; bodies (this is for instance the case for a separate subprogram, that
1972 ;; has a reference both to the stub and to the real body).
1973 ;; In that case, we simply go to each one in turn.
1975 ;; Get all the possible locations
1976 (string-match "^\\([0-9]+\\)[a-zA-Z+]\\([0-9]+\\)[ *]" ali-line)
1977 (set 'locations (list (list (match-string 1 ali-line) ;; line
1978 (match-string 2 ali-line) ;; column
1979 (ada-declare-file-of identlist))))
1980 (while (string-match "\\([0-9]+\\)[bc]\\([0-9]+\\)" ali-line start)
1981 (setq line (match-string 1 ali-line)
1982 col (match-string 2 ali-line)
1983 start (match-end 2))
1985 ;; it there was a file number in the same line
1986 (if (string-match (concat "\\([0-9]+\\)|\\([^|bc]+\\)?"
1987 (match-string 0 ali-line))
1989 (let ((file-number (match-string 1 ali-line)))
1990 (goto-char (point-min))
1991 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1992 (string-to-number file-number))
1993 (set 'file (match-string 1))
1995 ;; Else get the nearest file
1996 (set 'file (ada-declare-file-of identlist)))
1998 (set 'locations (append locations (list (list line col file)))))
2000 ;; Add the specs at the end again, so that from the last body we go to
2002 (set 'locations (append locations (list (car locations))))
2004 ;; Find the new location we want to go to.
2005 ;; If we are on none of the locations listed, we simply go to the specs.
2007 (setq line (caar locations)
2008 col (nth 1 (car locations))
2009 file (nth 2 (car locations)))
2012 (if (and (string= (caar locations) (ada-line-of identlist))
2013 (string= (nth 1 (car locations)) (ada-column-of identlist))
2014 (string= (file-name-nondirectory (nth 2 (car locations)))
2015 (file-name-nondirectory (ada-file-of identlist))))
2016 (setq locations (cadr locations)
2017 line (car locations)
2018 col (nth 1 locations)
2019 file (nth 2 locations)
2021 (set 'locations (cdr locations))))
2023 ;; Find the file in the source path
2024 (set 'file (ada-get-ada-file-name file (ada-file-of identlist)))
2026 ;; Kill the .ali buffer
2027 (kill-buffer (current-buffer))
2029 ;; Now go to the buffer
2030 (ada-xref-change-buffer file
2031 (string-to-number line)
2032 (1- (string-to-number col))
2037 (defun ada-find-in-src-path (identlist &optional other-frame)
2038 "More general function for cross-references.
2039 This function should be used when the standard algorithm that parses the
2040 .ali file has failed, either because that file was too old or even did not
2042 This function attempts to find the possible declarations for the identifier
2043 anywhere in the object path.
2044 This command requires the external `egrep' program to be available.
2046 This works well when one is using an external librarie and wants
2047 to find the declaration and documentation of the subprograms one is
2051 (dirs (ada-xref-get-obj-dir-field))
2052 (regexp (concat "[ *]" (ada-name-of identlist)))
2059 ;; Do the grep in all the directories. We do multiple shell
2060 ;; commands instead of one in case there is no .ali file in one
2061 ;; of the directory and the shell stops because of that.
2063 (set-buffer (get-buffer-create "*grep*"))
2065 (insert (shell-command-to-string
2066 (concat "egrep -i -h '^X|" regexp "( |$)' "
2067 (file-name-as-directory (car dirs)) "*.ali")))
2068 (set 'dirs (cdr dirs)))
2070 ;; Now parse the output
2071 (set 'case-fold-search t)
2072 (goto-char (point-min))
2073 (while (re-search-forward regexp nil t)
2076 (if (not (= (char-after) ?X))
2078 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
2079 (setq line (match-string 1)
2080 column (match-string 2))
2081 (re-search-backward "^X [0-9]+ \\(.*\\)$")
2082 (set 'file (list (match-string 1) line column))
2084 ;; There could be duplicate choices, because of the structure
2085 ;; of the .ali files
2086 (unless (member file list)
2087 (set 'list (append list (list file))))))))
2089 ;; Current buffer is still "*grep*"
2090 (kill-buffer "*grep*")
2093 ;; Now display the list of possible matches
2096 ;; No choice found => Error
2098 (error "No cross-reference found, please recompile your file"))
2100 ;; Only one choice => Do the cross-reference
2101 ((= (length list) 1)
2102 (set 'file (ada-find-src-file-in-dir (caar list)))
2104 (ada-xref-change-buffer file
2105 (string-to-number (nth 1 (car list)))
2106 (string-to-number (nth 2 (car list)))
2109 (error (concat (caar list) " not found in src_dir")))
2110 (message "This is only a (good) guess at the cross-reference.")
2113 ;; Else, ask the user
2115 (save-window-excursion
2116 (with-output-to-temp-buffer "*choice list*"
2118 (princ "Identifier is overloaded and Xref information is not up to date.\n")
2119 (princ "Possible declarations are:\n\n")
2120 (princ " no. in file at line col\n")
2121 (princ " --- --------------------- ---- ----\n")
2123 (while (< counter (length list))
2124 (princ (format " %2d) %-21s %4s %4s\n"
2126 (nth 0 (nth counter list))
2127 (nth 1 (nth counter list))
2128 (nth 2 (nth counter list))
2130 (setq counter (1+ counter))
2133 (while (or (not choice)
2134 (not (integerp choice))
2136 (> choice (length list)))
2139 (read-from-minibuffer "Enter No. of your choice: "))))
2141 (set 'choice (1- choice))
2142 (kill-buffer "*choice list*")
2144 (set 'file (ada-find-src-file-in-dir (car (nth choice list))))
2146 (ada-xref-change-buffer file
2147 (string-to-number (nth 1 (nth choice list)))
2148 (string-to-number (nth 2 (nth choice list)))
2151 (error (concat (car (nth choice list)) " not found in src_dir")))
2152 (message "This is only a (good) guess at the cross-reference.")
2155 (defun ada-xref-change-buffer
2156 (file line column identlist &optional other-frame)
2157 "Select and display FILE, at LINE and COLUMN.
2158 If we do not end on the same identifier as IDENTLIST, find the closest
2159 match. Kills the .ali buffer at the end.
2160 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2162 (let (declaration-buffer)
2164 ;; Select and display the destination buffer
2165 (if ada-xref-other-buffer
2167 (find-file-other-frame file)
2168 (set 'declaration-buffer (find-file-noselect file))
2169 (set-buffer declaration-buffer)
2170 (switch-to-buffer-other-window declaration-buffer)
2175 ;; move the cursor to the correct position
2178 (move-to-column column)
2180 ;; If we are not on the identifier, the ali file was not up-to-date.
2181 ;; Try to find the nearest position where the identifier is found,
2182 ;; this is probably the right one.
2183 (unless (looking-at (ada-name-of identlist))
2184 (ada-xref-search-nearest (ada-name-of identlist)))
2188 (defun ada-xref-search-nearest (name)
2189 "Searches for NAME nearest to the position recorded in the Xref file.
2190 It returns the position of the declaration in the buffer or nil if not found."
2191 (let ((orgpos (point))
2195 (goto-char (point-max))
2197 ;; loop - look for all declarations of name in this file
2198 (while (search-backward name nil t)
2200 ;; check if it really is a complete Ada identifier
2202 (not (save-excursion
2203 (goto-char (match-end 0))
2205 (not (ada-in-string-or-comment-p))
2207 ;; variable declaration ?
2209 (skip-chars-forward "a-zA-Z_0-9" )
2210 (ada-goto-next-non-ws)
2211 (looking-at ":[^=]"))
2212 ;; procedure, function, task or package declaration ?
2214 (ada-goto-previous-word)
2215 (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]\\>"))))
2217 ;; check if it is nearer than the ones before if any
2219 (< (abs (- (point) orgpos)) diff))
2221 (setq newpos (point)
2222 diff (abs (- newpos orgpos))))))
2227 (message "ATTENTION: this declaration is only a (good) guess ...")
2232 ;; Find the parent library file of the current file
2233 (defun ada-goto-parent ()
2234 "Go to the parent library file."
2236 (ada-require-project-file)
2238 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2244 (goto-char (point-min))
2245 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2246 (setq unit-name (match-string 1))
2247 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
2249 (kill-buffer buffer)
2250 (error "No parent unit !"))
2251 (setq unit-name (match-string 1 unit-name))
2254 ;; look for the file name for the parent unit specification
2255 (goto-char (point-min))
2256 (re-search-forward (concat "^W " unit-name
2257 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2259 (setq body-name (match-string 1))
2260 (setq ali-name (match-string 2))
2261 (kill-buffer buffer)
2264 (setq ali-name (ada-find-ali-file-in-dir ali-name))
2267 ;; Tries to open the new ali file to find the spec file
2270 (find-file ali-name)
2271 (goto-char (point-min))
2272 (re-search-forward (concat "^U " unit-name "%s[ \t]+"
2274 (setq body-name (match-string 1))
2275 (kill-buffer (current-buffer))
2280 (find-file body-name)
2283 (defun ada-make-filename-from-adaname (adaname)
2284 "Determine the filename in which ADANAME is found.
2285 This is a GNAT specific function that uses gnatkrunch."
2287 (setq krunch-buf (generate-new-buffer "*gkrunch*"))
2289 (set-buffer krunch-buf)
2290 ;; send adaname to external process `gnatkr'.
2291 (call-process "gnatkr" nil krunch-buf nil
2292 adaname ada-krunch-args)
2293 ;; fetch output of that process
2294 (setq adaname (buffer-substring
2297 (goto-char (point-min))
2300 (kill-buffer krunch-buf)))
2304 (defun ada-make-body-gnatstub ()
2305 "Create an Ada package body in the current buffer.
2306 This function uses the `gnatstub' program to create the body.
2307 This function typically is to be hooked into `ff-file-created-hooks'."
2310 (save-some-buffers nil nil)
2312 ;; If the current buffer is the body (as is the case when calling this
2313 ;; function from ff-file-created-hooks), then kill this temporary buffer
2314 (unless (interactive-p)
2316 (set-buffer-modified-p nil)
2317 (kill-buffer (current-buffer))))
2320 ;; Make sure the current buffer is the spec (this might not be the case
2321 ;; if for instance the user was asked for a project file)
2323 (unless (buffer-file-name (car (buffer-list)))
2324 (set-buffer (cadr (buffer-list))))
2326 ;; Make sure we have a project file (for parameters to gnatstub). Note that
2327 ;; this might have already been done if we have been called from the hook,
2328 ;; but this is not an expensive call)
2329 (ada-require-project-file)
2331 ;; Call the external process gnatstub
2332 (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
2333 (filename (buffer-file-name (car (buffer-list))))
2334 (output (concat (file-name-sans-extension filename) ".adb"))
2335 (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename))
2336 (buffer (get-buffer-create "*gnatstub*")))
2340 (compilation-minor-mode 1)
2342 (insert gnatstub-cmd)
2345 ;; call gnatstub to create the body file
2346 (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
2350 (goto-char (point-min))
2351 (search-forward "command not found" nil t))
2353 (message "gnatstub was not found -- using the basic algorithm")
2355 (kill-buffer buffer)
2358 ;; Else clean up the output
2360 (if (file-exists-p output)
2363 (kill-buffer buffer))
2365 ;; display the error buffer
2366 (display-buffer buffer)
2370 (defun ada-xref-initialize ()
2371 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2372 For instance, it creates the gnat-specific menus, sets some hooks for
2374 (make-local-hook 'ff-file-created-hooks)
2375 ;; This should really be an `add-hook'. -stef
2376 (setq ff-file-created-hooks 'ada-make-body-gnatstub)
2378 ;; Completion for file names in the mini buffer should ignore .ali files
2379 (add-to-list 'completion-ignored-extensions ".ali")
2383 ;; ----- Add to ada-mode-hook ---------------------------------------------
2385 ;; Use gvd or ddd as the default debugger if it was found
2386 ;; On windows, do not use the --tty switch for GVD, since this is
2387 ;; not supported. Actually, we do not use this on Unix either, since otherwise
2388 ;; there is no console window left in GVD, and people have to use the
2390 ;; This must be done before initializing the Ada menu.
2391 (if (ada-find-file-in-dir "gvd" exec-path)
2392 (set 'ada-prj-default-debugger "gvd ")
2393 (if (ada-find-file-in-dir "gvd.exe" exec-path)
2394 (set 'ada-prj-default-debugger "gvd ")
2395 (if (ada-find-file-in-dir "ddd" exec-path)
2396 (set 'ada-prj-default-debugger "ddd --tty -fullname -toolbar"))))
2398 ;; Set the keymap once and for all, so that the keys set by the user in his
2399 ;; config file are not overwritten every time we open a new file.
2403 (add-hook 'ada-mode-hook 'ada-xref-initialize)
2405 ;; Initializes the cross references to the runtime library
2406 (ada-initialize-runtime-library "")
2408 ;; Add these standard directories to the search path
2409 (set 'ada-search-directories
2410 (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path)
2411 ada-search-directories))
2413 ;; Make sure that the files are always associated with a project file. Since
2414 ;; the project file has some fields that are used for the editor (like the
2415 ;; casing exceptions), it has to be read before the user edits a file).
2416 ;; (add-hook 'ada-mode-hook
2418 ;; (let ((file (ada-prj-find-prj-file t)))
2419 ;; (if file (ada-reread-prj-file file)))))
2423 ;;; ada-xref.el ends here