]> code.delx.au - gnu-emacs/blob - lisp/progmodes/ada-xref.el
*** empty log message ***
[gnu-emacs] / lisp / progmodes / ada-xref.el
1 ;;; ada-xref.el --- for lookup and completion in Ada mode
2
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4
5 ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
6 ;; Rolf Ebert <ebert@inf.enst.fr>
7 ;; Emmanuel Briot <briot@gnat.com>
8 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
9 ;; Ada Core Technologies's version: $Revision: 1.7 $
10 ;; Keywords: languages ada xref
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30 ;;; This Package provides a set of functions to use the output of the
31 ;;; cross reference capabilities of the GNAT Ada compiler
32 ;;; for lookup and completion in Ada mode.
33 ;;;
34 ;;; The functions provided are the following ones :
35 ;;; - `ada-complete-identifier': completes the current identifier as much as
36 ;;; possible, depending of the known identifier in the unit
37 ;;; - `ada-point-and-xref': moves the mouse pointer and shows the declaration
38 ;;; of the selected identifier (either in the same buffer or in another
39 ;;; buffer
40 ;;; - `ada-goto-declaration': shows the declaration of the selected
41 ;;; identifier (the one under the cursor), either in the same buffer or in
42 ;;; another buffer
43 ;;; - `ada-goto-declaration-other-frame': same as previous, but opens a new
44 ;; frame to show the declaration
45 ;;; - `ada-compile-application': recompile your whole application, provided
46 ;;; that a project file exists in your directory
47 ;;; - `ada-run-application': run your application directly from Emacs
48 ;;; - `ada-reread-prj-file': force Emacs to read your project file again.
49 ;;; Otherwise, this file is only read the first time Emacs needs some
50 ;;; informations, which are then kept in memory
51 ;;; - `ada-change-prj': change the prj file associated with a buffer
52 ;;; - `ada-change-default-prj': change the default project file used for
53 ;;; every new buffer
54 ;;;
55 ;;; If a file *.`adp' exists in the ada-file directory, then it is
56 ;;; read for configuration informations. It is read only the first
57 ;;; time a cross-reference is asked for, and is not read later.
58
59 ;;; You need Emacs >= 20.2 to run this package
60
61 ;;; Code:
62
63 ;; ----- Requirements -----------------------------------------------------
64
65 (require 'compile)
66 (require 'comint)
67
68 ;; ------ Use variables
69 (defcustom ada-xref-other-buffer t
70 "*If nil, always display the cross-references in the same buffer.
71 Otherwise create either a new buffer or a new frame."
72 :type 'boolean :group 'ada)
73
74 (defcustom ada-xref-create-ali t
75 "*If non-nil, run gcc whenever the cross-references are not up-to-date.
76 If nil, the cross-reference mode will never run gcc."
77 :type 'boolean :group 'ada)
78
79 (defcustom ada-xref-confirm-compile nil
80 "*If non-nil, always ask for user confirmation before compiling or running
81 the application."
82 :type 'boolean :group 'ada)
83
84 (defcustom ada-krunch-args "0"
85 "*Maximum number of characters for filenames created by gnatkr.
86 Set to 0, if you don't use crunched filenames. This should be a string."
87 :type 'string :group 'ada)
88
89 (defcustom ada-prj-default-comp-opt "-gnatq"
90 "Default compilation options."
91 :type 'string :group 'ada)
92
93 (defcustom ada-prj-default-bind-opt ""
94 "Default binder options."
95 :type 'string :group 'ada)
96
97 (defcustom ada-prj-default-link-opt ""
98 "Default linker options."
99 :type 'string :group 'ada)
100
101 (defcustom ada-prj-default-gnatmake-opt "-g"
102 "Default options for gnatmake."
103 :type 'string :group 'ada)
104
105 (defcustom ada-prj-default-comp-cmd
106 "${cross_prefix}gcc -c ${comp_opt}"
107 "*Default command to be used to compile a single file.
108 Emacs will add the filename at the end of this command. This is the same
109 syntax as in the project file."
110 :type 'string :group 'ada)
111
112 (defcustom ada-prj-default-debugger "${cross_prefix}gdb"
113 "*Default name of the debugger. We recommend either `gdb',
114 `gdb --emacs_gdbtk' or `ddd --tty -fullname'."
115 :type 'string :group 'ada)
116
117 (defcustom ada-prj-default-make-cmd
118 (concat "${cross_prefix}gnatmake -o ${main} ${main_unit} ${gnatmake_opt} "
119 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
120 "*Default command to be used to compile the application.
121 This is the same syntax as in the project file."
122 :type 'string :group 'ada)
123
124 (defcustom ada-prj-default-project-file ""
125 "*Name of the project file to use for every Ada file.
126 Emacs will not try to use the standard algorithm to find the project file if
127 this string is not empty."
128 :type '(file :must-match t) :group 'ada)
129
130 (defcustom ada-gnatstub-opts "-q -I${src_dir}"
131 "*List of the options to pass to gnatsub to generate the body of a package.
132 This has the same syntax as in the project file (with variable substitution)."
133 :type 'string :group 'ada)
134
135 (defcustom ada-always-ask-project nil
136 "*If nil, use default values when no project file was found.
137 Otherwise, ask the user for the name of the project file to use."
138 :type 'boolean :group 'ada)
139
140 ;; ------- Nothing to be modified by the user below this
141 (defvar ada-last-prj-file ""
142 "Name of the last project file entered by the user.")
143
144 (defvar ada-check-switch "-gnats"
145 "Switch added to the command line to check the current file.")
146
147 (defvar ada-project-file-extension ".adp"
148 "The extension used for project files.")
149
150 (defconst is-windows (memq system-type (quote (windows-nt)))
151 "True if we are running on windows NT or windows 95.")
152
153 (defvar ada-xref-runtime-library-specs-path '()
154 "Directories where the specs for the standard library is found.
155 This is used for cross-references.")
156
157 (defvar ada-xref-runtime-library-ali-path '()
158 "Directories where the ali for the standard library is found.
159 This is used for cross-references.")
160
161 (defvar ada-xref-pos-ring '()
162 "List of positions selected by the cross-references functions.
163 Used to go back to these positions.")
164
165 (defconst ada-xref-pos-ring-max 16
166 "Number of positions kept in the list ada-xref-pos-ring.")
167
168 (defvar ada-operator-re
169 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
170 "Regexp to match for operators.")
171
172 (defvar ada-xref-default-prj-file nil
173 "Name of the default prj file, per directory.
174 Every directory is potentially associated with a default project file.
175 If it is nil, then the first prj file loaded will be the default for this
176 Emacs session.")
177
178
179 (defvar ada-xref-project-files '()
180 "Associative list of project files.
181 It has the following format:
182 \((project_name . value) (project_name . value) ...)
183 As always, the values of the project file are defined through properties.")
184
185 (defvar ada-prj-prj-file nil
186 "Buffer local variable that specifies the name of the project file.
187 Getting the project is done by looking up the key in ada-pxref-project-file.")
188
189 (defun my-local-variable-if-set-p (variable &optional buffer)
190 "Returns t if VARIABLE is local in BUFFER and is non-nil."
191 (and (local-variable-p variable buffer)
192 (save-excursion
193 (set-buffer buffer)
194 (symbol-value variable))))
195
196 (defun ada-initialize-runtime-library ()
197 "Initializes the variables for the runtime library location."
198 (save-excursion
199 (set 'ada-xref-runtime-library-specs-path '())
200 (set 'ada-xref-runtime-library-ali-path '())
201 (set-buffer (get-buffer-create "*gnatls*"))
202 (widen)
203 (erase-buffer)
204 ;; Catch any error in the following form (i.e gnatls was not found)
205 (condition-case nil
206 ;; Even if we get an error, delete the *gnatls* buffer
207 (unwind-protect
208 (progn
209 (call-process "gnatls" nil t nil "-v")
210 (goto-char (point-min))
211
212 ;; Source path
213
214 (search-forward "Source Search Path:")
215 (forward-line 1)
216 (while (not (looking-at "^$"))
217 (back-to-indentation)
218 (unless (looking-at "<Current_Directory>")
219 (add-to-list 'ada-xref-runtime-library-specs-path
220 (buffer-substring-no-properties
221 (point)
222 (save-excursion (end-of-line) (point)))))
223 (forward-line 1))
224
225 ;; Object path
226
227 (search-forward "Object Search Path:")
228 (forward-line 1)
229 (while (not (looking-at "^$"))
230 (back-to-indentation)
231 (unless (looking-at "<Current_Directory>")
232 (add-to-list 'ada-xref-runtime-library-ali-path
233 (buffer-substring-no-properties
234 (point)
235 (save-excursion (end-of-line) (point)))))
236 (forward-line 1))
237 )
238 (kill-buffer nil))
239 (error nil))
240 (set 'ada-xref-runtime-library-specs-path
241 (reverse ada-xref-runtime-library-specs-path))
242 (set 'ada-xref-runtime-library-ali-path
243 (reverse ada-xref-runtime-library-ali-path))
244 ))
245
246
247 (defun ada-treat-cmd-string (cmd-string)
248 "Replace meta-sequences like ${...} in CMD-STRING with the appropriate value.
249 The project file must have been loaded first.
250 As a special case, ${current} is replaced with the name of the currently
251 edited file, minus extension but with directory."
252
253 (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string)
254 (let (value)
255 (if (string= (match-string 2 cmd-string) "current")
256 (set 'value (file-name-sans-extension (buffer-file-name)))
257 (save-match-data
258 (set 'value (ada-xref-get-project-field
259 (intern (match-string 2 cmd-string))))))
260 (cond
261 ((null value)
262 (set 'cmd-string (replace-match "" t t cmd-string)))
263 ((stringp value)
264 (set 'cmd-string (replace-match value t t cmd-string)))
265 ((listp value)
266 (let ((prefix (match-string 1 cmd-string)))
267 (set 'cmd-string (replace-match
268 (mapconcat (lambda(x) (concat prefix x)) value " ")
269 t t cmd-string)))))
270 ))
271 cmd-string)
272
273 (defun ada-xref-set-default-prj-values (symbol ada-buffer)
274 "Reset the properties in SYMBOL to the default values for ADA-BUFFER."
275
276 (let ((file (buffer-file-name ada-buffer))
277 plist)
278 (save-excursion
279 (set-buffer ada-buffer)
280
281 (set 'plist
282 ;; Try hard to find a default value for filename, so that the user
283 ;; can edit his project file even if the current buffer is not an
284 ;; Ada file or not even associated with a file
285 (list 'filename (cond
286 (file
287 (ada-prj-get-prj-dir file))
288 (ada-prj-prj-file
289 ada-prj-prj-file)
290 (ada-xref-default-prj-file
291 ada-xref-default-prj-file)
292 (t
293 (error (concat "Not editing an Ada file,"
294 "and no default project "
295 "file specified!"))))
296 'build_dir (file-name-as-directory (expand-file-name "."))
297 'src_dir (list ".")
298 'obj_dir (list ".")
299 'casing (if (listp ada-case-exception-file)
300 ada-case-exception-file
301 (list ada-case-exception-file))
302 'comp_opt ada-prj-default-comp-opt
303 'bind_opt ada-prj-default-bind-opt
304 'link_opt ada-prj-default-link-opt
305 'gnatmake_opt ada-prj-default-gnatmake-opt
306 'main (if file
307 (file-name-sans-extension file)
308 "")
309 'main_unit (if file
310 (file-name-nondirectory
311 (file-name-sans-extension file))
312 "")
313 'cross_prefix ""
314 'remote_machine ""
315 'comp_cmd (concat "cd ${build_dir} && "
316 ada-prj-default-comp-cmd)
317 'check_cmd (concat ada-prj-default-comp-cmd " "
318 ada-check-switch)
319 'make_cmd (concat "cd ${build_dir} && "
320 ada-prj-default-make-cmd)
321 'run_cmd (concat "cd ${build_dir} && ${main}"
322 (if is-windows ".exe"))
323 'debug_cmd (concat ada-prj-default-debugger
324 (if is-windows " ${main}.exe"
325 " ${main}"))))
326 )
327 (set symbol plist)))
328
329 (defun ada-xref-get-project-field (field)
330 "Extract the value of FIELD from the project file of the current buffer.
331 The project file must have been loaded first.
332 A default value is returned if the file was not found."
333
334 (let ((file-name ada-prj-prj-file)
335 file value)
336
337 ;; If a default project file was set, use it if no other project
338 ;; file was specified for the buffer
339 (if (and (not file-name)
340 ada-prj-default-project-file
341 (not (string= ada-prj-default-project-file "")))
342 (set 'file-name ada-prj-default-project-file))
343
344 (set 'file (assoc file-name ada-xref-project-files))
345
346 ;; If the file was not found, use the default values
347 (if file
348 ;; Get the value from the file
349 (set 'value (plist-get (cdr file) field))
350
351 ;; Create a default nil file that contains the default values
352 (ada-xref-set-default-prj-values 'value (current-buffer))
353 (add-to-list 'ada-xref-project-files (cons nil value))
354 (set 'value (plist-get value field))
355 )
356 (if (stringp value)
357 (ada-treat-cmd-string value)
358 value))
359 )
360
361 ;; ----- Keybindings ------------------------------------------------------
362
363 (defun ada-add-keymap ()
364 "Add new key bindings when using `ada-xrel.el'."
365 (interactive)
366 (if ada-xemacs
367 (progn
368 (define-key ada-mode-map '(shift button3) 'ada-point-and-xref)
369 (define-key ada-mode-map '(control tab) 'ada-complete-identifier))
370 (define-key ada-mode-map [C-tab] 'ada-complete-identifier)
371 (define-key ada-mode-map [S-mouse-3] 'ada-point-and-xref))
372
373 (define-key ada-mode-map "\C-co" 'ff-find-other-file)
374 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
375 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
376 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
377 (define-key ada-mode-map "\C-c\C-x" 'ada-reread-prj-file)
378 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
379 (define-key ada-mode-map "\C-cb" 'ada-buffer-list)
380 (define-key ada-mode-map "\C-cc" 'ada-change-prj)
381 (define-key ada-mode-map "\C-cd" 'ada-change-default-prj)
382 (define-key ada-mode-map "\C-cg" 'ada-gdb-application)
383 (define-key ada-mode-map "\C-cr" 'ada-run-application)
384 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
385 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
386 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
387 )
388
389 ;; ----- Menus --------------------------------------------------------------
390 (defun ada-add-ada-menu ()
391 "Add some items to the standard Ada mode menu.
392 The items are added to the menu called NAME, which should be the same
393 name as was passed to `ada-create-menu'."
394 (interactive)
395 (if ada-xemacs
396 (let* ((menu-list '("Ada"))
397 (goto-menu '("Ada" "Goto"))
398 (edit-menu '("Ada" "Edit"))
399 (help-menu '("Ada" "Help"))
400 (options-menu (list "Ada" "Options")))
401 (funcall (symbol-function 'add-menu-button)
402 menu-list ["Check file" ada-check-current
403 (string= mode-name "Ada")] "Goto")
404 (funcall (symbol-function 'add-menu-button)
405 menu-list ["Compile file" ada-compile-current
406 (string= mode-name "Ada")] "Goto")
407 (funcall (symbol-function 'add-menu-button)
408 menu-list ["Build" ada-compile-application t] "Goto")
409 (funcall (symbol-function 'add-menu-button)
410 menu-list ["Run" ada-run-application t] "Goto")
411 (funcall (symbol-function 'add-menu-button)
412 menu-list ["Debug" ada-gdb-application t] "Goto")
413 (funcall (symbol-function 'add-menu-button)
414 menu-list ["--" nil t] "Goto")
415 (funcall (symbol-function 'add-submenu)
416 menu-list '("Project"
417 ["Associate" ada-change-prj t]
418 ["Set Default..." ada-set-default-project-file t]
419 ["List" ada-buffer-list t])
420 "Goto")
421 (funcall (symbol-function 'add-menu-button)
422 goto-menu ["Goto Parent Unit" ada-goto-parent t]
423 "Next compilation error")
424 (funcall (symbol-function 'add-menu-button)
425 goto-menu ["Goto References to any entity"
426 ada-find-any-references t]
427 "Next compilation error")
428 (funcall (symbol-function 'add-menu-button)
429 goto-menu ["List References" ada-find-references t]
430 "Next compilation error")
431 (funcall (symbol-function 'add-menu-button)
432 goto-menu ["Goto Declaration Other Frame"
433 ada-goto-declaration-other-frame t]
434 "Next compilation error")
435 (funcall (symbol-function 'add-menu-button)
436 goto-menu ["Goto Declaration/Body"
437 ada-goto-declaration t]
438 "Next compilation error")
439 (funcall (symbol-function 'add-menu-button)
440 goto-menu ["Goto Previous Reference"
441 ada-xref-goto-previous-reference t]
442 "Next compilation error")
443 (funcall (symbol-function 'add-menu-button)
444 goto-menu ["--" nil t] "Next compilation error")
445 (funcall (symbol-function 'add-menu-button)
446 edit-menu ["Complete Identifier"
447 ada-complete-identifier t]
448 "Indent Line")
449 (funcall (symbol-function 'add-menu-button)
450 edit-menu ["--------" nil t] "Indent Line")
451 (funcall (symbol-function 'add-menu-button)
452 help-menu ["Gnat User Guide" (info "gnat_ug")])
453 (funcall (symbol-function 'add-menu-button)
454 help-menu ["Gnat Reference Manual" (info "gnat_rm")])
455 (funcall (symbol-function 'add-menu-button)
456 help-menu ["Gcc Documentation" (info "gcc")])
457 (funcall (symbol-function 'add-menu-button)
458 help-menu ["Gdb Documentation" (info "gdb")])
459 (funcall (symbol-function 'add-menu-button)
460 help-menu ["Ada95 Reference Manual" (info "arm95")])
461 (funcall (symbol-function 'add-menu-button)
462 options-menu
463 ["Show Cross-References in Other Buffer"
464 (setq ada-xref-other-buffer
465 (not ada-xref-other-buffer))
466 :style toggle :selected ada-xref-other-buffer])
467 (funcall (symbol-function 'add-menu-button)
468 options-menu
469 ["Automatically Recompile for Cross-References"
470 (setq ada-xref-create-ali (not ada-xref-create-ali))
471 :style toggle :selected ada-xref-create-ali])
472 (funcall (symbol-function 'add-menu-button)
473 options-menu
474 ["Confirm Commands"
475 (setq ada-xref-confirm-compile
476 (not ada-xref-confirm-compile))
477 :style toggle :selected ada-xref-confirm-compile])
478 )
479
480 ;; for Emacs
481 (let* ((menu (lookup-key ada-mode-map [menu-bar Ada]))
482 (edit-menu (lookup-key ada-mode-map [menu-bar Ada Edit]))
483 (help-menu (lookup-key ada-mode-map [menu-bar Ada Help]))
484 (goto-menu (lookup-key ada-mode-map [menu-bar Ada Goto]))
485 (options-menu (lookup-key ada-mode-map [menu-bar Ada Options])))
486
487 (define-key-after menu [Check] '("Check file" . ada-check-current)
488 'Customize)
489 (define-key-after menu [Compile] '("Compile file" . ada-compile-current)
490 'Check)
491 (define-key-after menu [Build] '("Build" . ada-compile-application)
492 'Compile)
493 (define-key-after menu [Run] '("Run" . ada-run-application) 'Build)
494 (define-key-after menu [Debug] '("Debug" . ada-gdb-application) 'Run)
495 (define-key-after menu [rem] '("--" . nil) 'Debug)
496 (define-key-after menu [Project]
497 (cons "Project"
498 (funcall (symbol-function 'easy-menu-create-menu)
499 "Project"
500 '(["Associate..." ada-change-prj t
501 :included (string= mode-name "Ada")]
502 ["Set Default..." ada-set-default-project-file t]
503 ["List" ada-buffer-list t])))
504 'rem)
505
506 (define-key help-menu [Gnat_ug]
507 '("Gnat User Guide" . (lambda() (interactive) (info "gnat_ug"))))
508 (define-key help-menu [Gnat_rm]
509 '("Gnat Reference Manual" . (lambda() (interactive) (info "gnat_rm"))))
510 (define-key help-menu [Gcc]
511 '("Gcc Documentation" . (lambda() (interactive) (info "gcc"))))
512 (define-key help-menu [gdb]
513 '("Gdb Documentation" . (lambda() (interactive) (info "gdb"))))
514 (define-key help-menu [gdb]
515 '("Ada95 Reference Manual" . (lambda() (interactive) (info "arm95"))))
516
517 (define-key goto-menu [rem] '("----" . nil))
518 (define-key goto-menu [Parent] '("Goto Parent Unit"
519 . ada-goto-parent))
520 (define-key goto-menu [References-any]
521 '("Goto References to any entity" . ada-find-any-references))
522 (define-key goto-menu [References]
523 '("List References" . ada-find-references))
524 (define-key goto-menu [Prev]
525 '("Goto Previous Reference" . ada-xref-goto-previous-reference))
526 (define-key goto-menu [Decl-other]
527 '("Goto Declaration Other Frame" . ada-goto-declaration-other-frame))
528 (define-key goto-menu [Decl]
529 '("Goto Declaration/Body" . ada-goto-declaration))
530
531 (define-key edit-menu [rem] '("----" . nil))
532 (define-key edit-menu [Complete] '("Complete Identifier"
533 . ada-complete-identifier))
534
535 (define-key-after options-menu [xrefrecompile]
536 '(menu-item "Automatically Recompile for Cross-References"
537 (lambda()(interactive)
538 (setq ada-xref-create-ali (not ada-xref-create-ali)))
539 :button (:toggle . ada-xref-create-ali)) t)
540 (define-key-after options-menu [xrefconfirm]
541 '(menu-item "Confirm Commands"
542 (lambda()(interactive)
543 (setq ada-xref-confirm-compile
544 (not ada-xref-confirm-compile)))
545 :button (:toggle . ada-xref-confirm-compile)) t)
546 (define-key-after options-menu [xrefother]
547 '(menu-item "Show Cross-References in Other Buffer"
548 (lambda()(interactive)
549 (setq ada-xref-other-buffer (not ada-xref-other-buffer)))
550 :button (:toggle . ada-xref-other-buffer)) t)
551 )
552 )
553 )
554
555 ;; ----- Utilities -------------------------------------------------
556
557 (defun ada-require-project-file ()
558 "If no project file is assigned to this buffer, load one."
559 (if (not (my-local-variable-if-set-p 'ada-prj-prj-file (current-buffer)))
560 (ada-reread-prj-file)))
561
562 (defun ada-xref-push-pos (filename position)
563 "Push (FILENAME, POSITION) on the position ring for cross-references."
564 (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
565 (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
566 (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
567
568 (defun ada-xref-goto-previous-reference ()
569 "Go to the previous cross-reference we were on."
570 (interactive)
571 (if ada-xref-pos-ring
572 (let ((pos (car ada-xref-pos-ring)))
573 (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
574 (find-file (car (cdr pos)))
575 (goto-char (car pos)))))
576
577 (defun ada-convert-file-name (name)
578 "Converts from NAME to a name that can be used by the compilation commands.
579 This is overriden on VMS to convert from VMS filenames to Unix filenames."
580 name)
581
582 (defun ada-set-default-project-file (name)
583 "Set the file whose name is NAME as the default project file."
584 (interactive "fProject file:")
585
586 ;; All the directories should use this file as the default from now on,
587 ;; even if they were already associated with a file.
588 (set 'ada-xref-default-prj-file nil)
589
590 (set 'ada-prj-default-project-file name)
591
592 ;; Make sure that all the buffers see the new project file, even if they
593 ;; are not Ada buffers (for instance if we want to display the current
594 ;; project file in the frame title).
595 (setq-default ada-prj-prj-file name)
596
597 (ada-reread-prj-file name)
598 )
599
600 ;; ------ Handling the project file -----------------------------
601
602 (defun ada-prj-find-prj-file (&optional no-user-question)
603 "Find the prj file associated with the current buffer.
604 If NO-USER-QUESTION is non-nil, use a default file if not project file was
605 found, and do not ask the user.
606 If the buffer is not an Ada buffer, associate it with the default project
607 file. If none is set, return nil."
608
609 (let (selected)
610
611 ;; If we don't have an ada buffer, or the current buffer is not
612 ;; a real file (for instance an emerge buffer)
613
614 (if (or (not (string= mode-name "Ada"))
615 (not (buffer-file-name)))
616
617 ;; 1st case: not an Ada buffer
618 (if (and ada-prj-default-project-file
619 (not (string= ada-prj-default-project-file "")))
620 (set 'selected ada-prj-default-project-file))
621
622 ;; 2nd case: If the buffer already has a project file, use it
623 (if (my-local-variable-if-set-p 'ada-prj-prj-file (current-buffer))
624 (set 'selected ada-prj-prj-file)
625
626 (let* ((current-file (buffer-file-name))
627 (first-choice (concat
628 (file-name-sans-extension current-file)
629 ada-project-file-extension))
630 (dir (file-name-directory current-file))
631
632 ;; on Emacs 20.2, directory-files does not work if
633 ;; parse-sexp-lookup-properties is set
634 (parse-sexp-lookup-properties nil)
635 (prj-files (directory-files
636 dir t
637 (concat ".*" (regexp-quote ada-project-file-extension) "$")))
638 (choice nil)
639 (default (assoc dir ada-xref-default-prj-file)))
640
641 (cond
642
643 ;; 3rd case: a project file is already associated with the directory
644 (default
645 (set 'selected (cdr default)))
646
647 ;; 4th case: the user has set a default project file for every file
648 ((and ada-prj-default-project-file
649 (not (string= ada-prj-default-project-file "")))
650 (set 'selected ada-prj-default-project-file))
651
652 ;; 5th case: there is a project file with the same name as the Ada file,
653 ;; but not the same extension.
654 ((file-exists-p first-choice)
655 (set 'selected first-choice))
656
657 ;; 6th case: only one project file was found in the current directory
658 ((= (length prj-files) 1)
659 (set 'selected (car prj-files)))
660
661 ;; 7th case: if there are multiple files, ask the user
662 ((and (> (length prj-files) 1) (not no-user-question))
663 (save-window-excursion
664 (with-output-to-temp-buffer "*choice list*"
665 (princ "There are more than one possible project file. Which one should\n")
666 (princ "be used ?\n\n")
667 (princ " no. file name \n")
668 (princ " --- ------------------------\n")
669 (let ((counter 1))
670 (while (<= counter (length prj-files))
671 (princ (format " %2d) %s\n"
672 counter
673 (nth (1- counter) prj-files)))
674 (setq counter (1+ counter))
675 ))) ; end of with-output-to ...
676 (setq choice nil)
677 (while (or
678 (not choice)
679 (not (integerp choice))
680 (< choice 1)
681 (> choice (length prj-files)))
682 (setq choice (string-to-int
683 (read-from-minibuffer "Enter No. of your choice: "))))
684 (set 'selected (nth (1- choice) prj-files))))
685
686 ;; 8th case: no project file was found in the directory, ask a name to the
687 ;; user, using as a default value the last one entered by the user
688 ((= (length prj-files) 0)
689 (unless (or no-user-question (not ada-always-ask-project))
690 (setq ada-last-prj-file
691 (read-file-name "project file:" nil ada-last-prj-file))
692 (unless (string= ada-last-prj-file "")
693 (set 'selected ada-last-prj-file))))
694 ))))
695 selected
696 ))
697
698
699 (defun ada-parse-prj-file (prj-file)
700 "Reads and parses the PRJ-FILE file if it was found.
701 The current buffer should be the ada-file buffer."
702 (if prj-file
703 (let (project src_dir obj_dir casing
704 (ada-buffer (current-buffer)))
705 (set 'prj-file (expand-file-name prj-file))
706
707 ;; Initialize the project with the default values
708 (ada-xref-set-default-prj-values 'project (current-buffer))
709
710 ;; Do not use find-file below, since we don't want to show this
711 ;; buffer. If the file is open through speedbar, we can't use
712 ;; find-file anyway, since the speedbar frame is special and does not
713 ;; allow the selection of a file in it.
714
715 (set-buffer (find-file-noselect prj-file))
716
717 (widen)
718 (goto-char (point-min))
719
720 ;; Now overrides these values with the project file
721 (while (not (eobp))
722 (if (looking-at "^\\([^=]+\\)=\\(.*\\)")
723 (cond
724 ((string= (match-string 1) "src_dir")
725 (add-to-list 'src_dir
726 (file-name-as-directory (match-string 2))))
727 ((string= (match-string 1) "obj_dir")
728 (add-to-list 'obj_dir
729 (file-name-as-directory (match-string 2))))
730 ((string= (match-string 1) "casing")
731 (set 'casing (cons (match-string 2) casing)))
732 ((string= (match-string 1) "build_dir")
733 (set 'project
734 (plist-put project 'build_dir
735 (file-name-as-directory (match-string 2)))))
736 (t
737 (set 'project (plist-put project (intern (match-string 1))
738 (match-string 2))))))
739 (forward-line 1))
740
741 (if src_dir (set 'project (plist-put project 'src_dir
742 (reverse src_dir))))
743 (if obj_dir (set 'project (plist-put project 'obj_dir
744 (reverse obj_dir))))
745 (if casing (set 'project (plist-put project 'casing casing)))
746
747 ;; Memorize the newly read project file
748 (if (assoc prj-file ada-xref-project-files)
749 (setcdr (assoc prj-file ada-xref-project-files) project)
750 (add-to-list 'ada-xref-project-files (cons prj-file project)))
751
752 ;; Sets up the compilation-search-path so that Emacs is able to
753 ;; go to the source of the errors in a compilation buffer
754 (setq compilation-search-path (ada-get-absolute-dir-list
755 (plist-get project 'src_dir)
756 (plist-get project 'build_dir)))
757
758 ;; Associate each source directory in the project file with this file
759 (mapcar (lambda (x)
760 (if (not (assoc (expand-file-name x)
761 ada-xref-default-prj-file))
762 (setq ada-xref-default-prj-file
763 (cons (cons (expand-file-name x) prj-file)
764 ada-xref-default-prj-file))))
765 compilation-search-path)
766
767 ;; Add the directories to the search path for ff-find-other-file
768 ;; Do not add the '/' or '\' at the end
769 (set (make-local-variable 'ff-search-directories)
770 (append (mapcar 'directory-file-name compilation-search-path)
771 ada-search-directories))
772
773 ;; Kill the .ali buffer
774 (kill-buffer nil)
775 (set-buffer ada-buffer)
776
777 ;; Setup the project file for the current buffer
778 (set (make-local-variable 'ada-prj-prj-file) prj-file)
779
780 )
781 ))
782
783
784 (defun ada-find-references (&optional pos)
785 "Find all references to the entity under POS.
786 Calls gnatfind to find the references."
787 (interactive "")
788 (unless pos
789 (set 'pos (point)))
790 (ada-require-project-file)
791
792 (let* ((identlist (ada-read-identifier pos))
793 (alifile (ada-get-ali-file-name (ada-file-of identlist)))
794 (process-environment (ada-set-environment)))
795
796 (set-buffer (get-file-buffer (ada-file-of identlist)))
797
798 ;; if the file is more recent than the executable
799 (if (or (buffer-modified-p (current-buffer))
800 (file-newer-than-file-p (ada-file-of identlist) alifile))
801 (ada-find-any-references (ada-name-of identlist)
802 (ada-file-of identlist)
803 nil nil)
804 (ada-find-any-references (ada-name-of identlist)
805 (ada-file-of identlist)
806 (ada-line-of identlist)
807 (ada-column-of identlist))))
808 )
809
810 (defun ada-find-any-references (entity &optional file line column)
811 "Search for references to any entity whose name is ENTITY.
812 ENTITY was first found the location given by FILE, LINE and COLUMN."
813 (interactive "sEntity name: ")
814 (ada-require-project-file)
815
816 (let* ((command (concat "gnatfind -rf " entity
817 (if file (concat ":" (file-name-nondirectory file)))
818 (if line (concat ":" line))
819 (if column (concat ":" column)))))
820
821 ;; If a project file is defined, use it
822 (if (my-local-variable-if-set-p 'ada-prj-prj-file (current-buffer))
823 (setq command (concat command " -p" ada-prj-prj-file)))
824
825 (compile-internal command "No more references" "gnatfind")
826
827 ;; Hide the "Compilation" menu
828 (save-excursion
829 (set-buffer "*gnatfind*")
830 (local-unset-key [menu-bar compilation-menu]))
831 )
832 )
833
834 (defun ada-buffer-list ()
835 "Display a buffer with all the Ada buffers and their associated project."
836 (interactive)
837 (save-excursion
838 (set-buffer (get-buffer-create "*Buffer List*"))
839 (setq buffer-read-only nil)
840 (erase-buffer)
841 (setq standard-output (current-buffer))
842 (princ "The following line is a list showing the associations between
843 directories and project file. It has the format : ((directory_1 . project_file1)
844 (directory2 . project_file2)...)\n\n")
845 (princ ada-xref-default-prj-file)
846 (princ "\n
847 Buffer Mode Project file
848 ------ ---- ------------
849 \n")
850 (let ((bl (buffer-list)))
851 (while bl
852 (let* ((buffer (car bl))
853 (buffer-name (buffer-name buffer))
854 this-buffer-mode-name
855 this-buffer-project-file)
856 (save-excursion
857 (set-buffer buffer)
858 (setq this-buffer-mode-name
859 (if (eq buffer standard-output)
860 "Buffer Menu" mode-name))
861 (if (string= this-buffer-mode-name
862 "Ada")
863 (setq this-buffer-project-file
864 (if ( my-local-variable-if-set-p 'ada-prj-prj-file
865 (current-buffer))
866 (expand-file-name ada-prj-prj-file)
867 ""))))
868 (if (string= this-buffer-mode-name
869 "Ada")
870 (progn
871 (princ (format "%-19s " buffer-name))
872 (princ (format "%-6s " this-buffer-mode-name))
873 (princ this-buffer-project-file)
874 (princ "\n")
875 ))
876 ) ;; end let*
877 (setq bl (cdr bl))
878 ) ;; end while
879 );; end let
880 ) ;; end save-excursion
881 (display-buffer "*Buffer List*")
882 (other-window 1)
883 )
884
885 (defun ada-change-prj (filename)
886 "Set FILENAME to be the project file for current buffer."
887 (interactive "fproject file:")
888
889 ;; make sure we are using an Ada file
890 (if (not (string= mode-name "Ada"))
891 (error "You must be in ada-mode to use this function"))
892
893 (set (make-local-variable 'ada-prj-prj-file) filename)
894 (ada-parse-prj-file filename)
895 )
896
897 (defun ada-change-default-prj (filename)
898 "Set FILENAME to be the default project file for the current directory."
899 (interactive "ffile name:")
900 (let ((dir (file-name-directory (buffer-file-name)))
901 (prj (expand-file-name filename)))
902
903 ;; Associate the directory with a project file
904 (if (assoc dir ada-xref-default-prj-file)
905 (setcdr (assoc dir ada-xref-default-prj-file) prj)
906 (add-to-list 'ada-xref-default-prj-file (list dir prj)))
907
908 ;; Reparse the project file
909 (ada-parse-prj-file filename)))
910
911
912 ;; ----- Identlist manipulation -------------------------------------------
913 ;; An identlist is a vector that is used internally to reference an identifier
914 ;; To facilitate its use, we provide the following macros
915
916 (defmacro ada-make-identlist () (make-vector 8 nil))
917 (defmacro ada-name-of (identlist) (list 'aref identlist 0))
918 (defmacro ada-line-of (identlist) (list 'aref identlist 1))
919 (defmacro ada-column-of (identlist) (list 'aref identlist 2))
920 (defmacro ada-file-of (identlist) (list 'aref identlist 3))
921 (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
922 (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
923 (defmacro ada-references-of (identlist) (list 'aref identlist 6))
924 (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
925
926 (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
927 (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
928 (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
929 (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
930 (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
931 (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
932 (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
933 (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
934
935 (defsubst ada-get-ali-buffer (file)
936 "Reads the ali file into a new buffer, and returns this buffer's name"
937 (find-file-noselect (ada-get-ali-file-name file)))
938
939
940
941 ;; ----- Identifier Completion --------------------------------------------
942 (defun ada-complete-identifier (pos)
943 "Tries to complete the identifier around POS.
944 The feature is only available if the files where compiled not using the -gnatx
945 option."
946 (interactive "d")
947 (ada-require-project-file)
948
949 ;; Initialize function-local variables and jump to the .ali buffer
950 ;; Note that for regexp search is case insensitive too
951 (let* ((curbuf (current-buffer))
952 (identlist (ada-read-identifier pos))
953 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
954 (regexp-quote (ada-name-of identlist))
955 "[a-zA-Z0-9_]*\\)"))
956 (completed nil)
957 (symalist nil))
958
959 ;; Open the .ali file
960 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
961 (goto-char (point-max))
962
963 ;; build an alist of possible completions
964 (while (re-search-backward sofar nil t)
965 (setq symalist (cons (cons (match-string 1) nil) symalist)))
966
967 (setq completed (try-completion "" symalist))
968
969 ;; kills .ali buffer
970 (kill-buffer nil)
971
972 ;; deletes the incomplete identifier in the buffer
973 (set-buffer curbuf)
974 (looking-at "[a-zA-Z0-9_]+")
975 (replace-match "")
976 ;; inserts the completed symbol
977 (insert completed)
978 ))
979
980 ;; ----- Cross-referencing ----------------------------------------
981
982 (defun ada-point-and-xref ()
983 "Calls `mouse-set-point' and then `ada-goto-declaration'."
984 (interactive)
985 (mouse-set-point last-input-event)
986 (ada-goto-declaration (point)))
987
988 (defun ada-goto-declaration (pos)
989 "Display the declaration of the identifier around POS.
990 The declaration is shown in another buffer if `ada-xref-other-buffer' is
991 non-nil."
992 (interactive "d")
993 (ada-require-project-file)
994 (push-mark pos)
995 (ada-xref-push-pos (buffer-file-name) pos)
996 (ada-find-in-ali (ada-read-identifier pos)))
997
998 (defun ada-goto-declaration-other-frame (pos)
999 "Display the declaration of the identifier around POS.
1000 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1001 (interactive "d")
1002 (ada-require-project-file)
1003 (push-mark pos)
1004 (ada-xref-push-pos (buffer-file-name) pos)
1005 (ada-find-in-ali (ada-read-identifier pos) t))
1006
1007 (defun ada-remote (command)
1008 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1009 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1010 (if (or (not machine) (string= machine ""))
1011 command
1012 (format "%s %s '(%s)'"
1013 remote-shell-program
1014 machine
1015 command))))
1016
1017 (defun ada-get-absolute-dir (dir root-dir)
1018 "Returns the absolute directory corresponding to DIR.
1019 If DIR is a relative directory, the value of ROOT-DIR is added in front."
1020 (if (= (string-to-char dir) ?/)
1021 dir
1022 (concat root-dir dir)))
1023
1024 (defun ada-get-absolute-dir-list (dir-list root-dir)
1025 "Returns the list of absolute directories found in dir-list.
1026 If a directory is a relative directory, the value of ROOT-DIR is added in
1027 front."
1028 (mapcar (lambda (x) (ada-get-absolute-dir x root-dir)) dir-list))
1029
1030 (defun ada-set-environment ()
1031 "Return the new value for process-environment.
1032 It modifies the source path and object path with the values found in the
1033 project file."
1034 (let ((include (getenv "ADA_INCLUDE_PATH"))
1035 (objects (getenv "ADA_OBJECTS_PATH"))
1036 (build-dir (ada-xref-get-project-field 'build_dir)))
1037 (if include
1038 (set 'include (concat include path-separator)))
1039 (if objects
1040 (set 'objects (concat objects path-separator)))
1041 (cons
1042 (concat "ADA_INCLUDE_PATH="
1043 include
1044 (mapconcat (lambda(x) (ada-get-absolute-dir x build-dir))
1045 (ada-xref-get-project-field 'src_dir)
1046 path-separator))
1047 (cons
1048 (concat "ADA_OBJECTS_PATH="
1049 objects
1050 (mapconcat (lambda(x) (ada-get-absolute-dir x build-dir))
1051 (ada-xref-get-project-field 'obj_dir)
1052 path-separator))
1053 process-environment))))
1054
1055 (defun ada-compile-application (&optional arg)
1056 "Compiles the application, using the command found in the project file.
1057 If ARG is not nil, ask for user confirmation."
1058 (interactive "P")
1059 (ada-require-project-file)
1060 (let ((cmd (ada-xref-get-project-field 'make_cmd))
1061 (process-environment (ada-set-environment))
1062 (compilation-scroll-output t))
1063
1064 (set 'compilation-search-path
1065 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
1066 (ada-xref-get-project-field 'build_dir)))
1067
1068 ;; If no project file was found, ask the user
1069 (unless cmd
1070 (setq cmd "" arg t))
1071
1072 (compile (ada-remote
1073 (if (or ada-xref-confirm-compile arg)
1074 (read-from-minibuffer "enter command to compile: " cmd)
1075 cmd)))
1076 ))
1077
1078 (defun ada-compile-current (&optional arg prj-field)
1079 "Recompile the current file.
1080 If ARG is not nil, ask for user confirmation of the command.
1081 PRJ-FIELD is the name of the field to use in the project file to get the
1082 command, and should be either comp_cmd (default) or check_cmd."
1083 (interactive "P")
1084 (ada-require-project-file)
1085 (let* ((field (if prj-field prj-field 'comp_cmd))
1086 (cmd (ada-xref-get-project-field field))
1087 (process-environment (ada-set-environment))
1088 (compilation-scroll-output t))
1089
1090 (set 'compilation-search-path
1091 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
1092 (ada-xref-get-project-field 'build_dir)))
1093
1094 ;; If no project file was found, ask the user
1095 (if cmd
1096 (set 'cmd (concat cmd " " (ada-convert-file-name (buffer-file-name))))
1097 (setq cmd "" arg t))
1098
1099 (compile (ada-remote
1100 (if (or ada-xref-confirm-compile arg)
1101 (read-from-minibuffer "enter command to compile: " cmd)
1102 cmd)))))
1103
1104 (defun ada-check-current (&optional arg)
1105 "Recompile the current file.
1106 If ARG is not nil, ask for user confirmation of the command."
1107 (interactive "P")
1108 (ada-compile-current arg 'check_cmd))
1109
1110 (defun ada-run-application (&optional arg)
1111 "Run the application.
1112 if ARG is not-nil, asks for user confirmation."
1113 (interactive)
1114 (ada-require-project-file)
1115
1116 (let ((machine (ada-xref-get-project-field 'cross_prefix)))
1117 (if (and machine (not (string= machine "")))
1118 (error "This feature is not supported yet for cross environments")))
1119
1120 (let ((command (ada-xref-get-project-field 'run_cmd)))
1121
1122 ;; Guess the command if it wasn't specified
1123 (if (or (not command) (string= command ""))
1124 (set 'command (file-name-sans-extension (buffer-name))))
1125
1126 ;; Ask for the arguments to the command if required
1127 (if (or ada-xref-confirm-compile arg)
1128 (set 'command (read-from-minibuffer "Enter command to execute: " command)))
1129
1130 ;; Modify the command to run remotely
1131 (setq command (ada-remote command))
1132
1133 ;; Run the command
1134 (save-excursion
1135 (set-buffer (get-buffer-create "*run*"))
1136 (set 'buffer-read-only nil)
1137 (erase-buffer)
1138 (goto-char (point-min))
1139 (insert "\nRunning " command "\n\n")
1140 (start-process "run" (current-buffer) shell-file-name "-c" command)
1141 )
1142 (display-buffer "*run*")
1143
1144 ;; change to buffer *run* for interactive programs
1145 (other-window 1)
1146 (switch-to-buffer "*run*")
1147 ))
1148
1149
1150 (defun ada-gdb-application (&optional arg)
1151 "Start the debugger on the application.
1152 If ARG is non-nil, ask the user to confirm the command."
1153 (interactive "P")
1154 (let ((buffer (current-buffer))
1155 gdb-buffer
1156 cmd)
1157 (ada-require-project-file)
1158 (set 'cmd (ada-xref-get-project-field 'debug_cmd))
1159 (let ((machine (ada-xref-get-project-field 'remote_machine)))
1160 (if (and machine (not (string= machine "")))
1161 (error "This feature is not supported yet for remote environments")))
1162
1163 ;; If the command was not given in the project file, start a bare gdb
1164 (if (not cmd)
1165 (set 'cmd (concat ada-prj-default-debugger
1166 " "
1167 (file-name-sans-extension (buffer-file-name)))))
1168 (if (or arg ada-xref-confirm-compile)
1169 (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
1170
1171 ;; Set the variable gud-last-last-frame so that glide-debug can find
1172 ;; the name of the Ada file, and thus of the project file if needed.
1173 (if ada-prj-prj-file
1174 (set 'gud-last-last-frame (cons ada-prj-prj-file 1)))
1175
1176 (if (and (string-match "jdb" (comint-arguments cmd 0 0))
1177 (boundp 'jdb))
1178 (funcall (symbol-function 'jdb) cmd)
1179 (gdb cmd))
1180
1181 (set 'gdb-buffer (symbol-value 'gud-comint-buffer))
1182
1183 ;; Switch back to the source buffer
1184 ;; and Activate the debug part in the contextual menu
1185 (switch-to-buffer buffer)
1186
1187 (if (functionp 'gud-make-debug-menu)
1188 (funcall (symbol-function 'gud-make-debug-menu)))
1189
1190 ;; Warning: on Emacs >= 20.3.8, same-window-regexps includes gud-*,
1191 ;; so the following call to display buffer will select the
1192 ;; buffer instead of displaying it in another window
1193 ;; This is why the second argument to display-buffer is 't'
1194 (display-buffer gdb-buffer t)
1195 ))
1196
1197
1198 (defun ada-reread-prj-file (&optional filename)
1199 "Forces Emacs to read either FILENAME or the project file associated
1200 with the current buffer.
1201 Otherwise, this file is only read once, and never read again.
1202 Since the information in the project file is shared between all buffers, this
1203 automatically modifies the setup for all the Ada buffer that use this file."
1204 (interactive "P")
1205 (if filename
1206 (ada-parse-prj-file filename)
1207 (ada-parse-prj-file (ada-prj-find-prj-file)))
1208 )
1209
1210
1211 ;; ------ Private routines
1212
1213 (defun ada-xref-current (file &optional ali-file-name)
1214 "Update the cross-references for FILE.
1215 This in fact recompiles FILE to create ALI-FILE-NAME."
1216 ;; kill old buffer
1217 (if (and ali-file-name
1218 (get-file-buffer ali-file-name))
1219 (kill-buffer (get-file-buffer ali-file-name)))
1220 ;; read the project file
1221 (ada-require-project-file)
1222 (let* ((cmd (ada-xref-get-project-field 'comp_cmd))
1223 (process-environment (ada-set-environment))
1224 (compilation-scroll-output t)
1225 (name (ada-convert-file-name (buffer-file-name)))
1226 (body-name (ada-get-body-name name)))
1227
1228 ;; Always recompile the body when we can
1229 (set 'body-name (or body-name name))
1230
1231 ;; prompt for command to execute
1232 (set 'cmd (concat cmd " " body-name))
1233 (compile (ada-remote
1234 (if ada-xref-confirm-compile
1235 (read-from-minibuffer "enter command to compile: " cmd)
1236 cmd)))))
1237
1238 (defun ada-find-file-in-dir (file dir-list)
1239 "Search for FILE in DIR-LIST."
1240 (let (found)
1241 (while (and (not found) dir-list)
1242 (set 'found (concat (file-name-as-directory (car dir-list))
1243 (file-name-nondirectory file)))
1244
1245 (unless (file-exists-p found)
1246 (set 'found nil))
1247 (set 'dir-list (cdr dir-list)))
1248 found))
1249
1250 (defun ada-find-ali-file-in-dir (file)
1251 "Find an .ali file in obj_dir. The current buffer must be the Ada file.
1252 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1253 and the standard runtime location at the end."
1254 (ada-find-file-in-dir file
1255 (append
1256
1257 ;; Add ${build_dir} in front of the path
1258 (list (ada-xref-get-project-field 'build_dir))
1259
1260 (ada-get-absolute-dir-list
1261 (ada-xref-get-project-field 'obj_dir)
1262 (ada-xref-get-project-field 'build_dir))
1263
1264 ;; Add the standard runtime at the end
1265 ada-xref-runtime-library-ali-path)))
1266
1267 (defun ada-find-src-file-in-dir (file)
1268 "Find a source file in src_dir. The current buffer must be the Ada file.
1269 Adds src_dir in front of the search path to conform to gnatmake's behavior,
1270 and the standard runtime location at the end."
1271 (ada-find-file-in-dir file
1272 (append
1273
1274 ;; Add ${build_dir} in front of the path
1275 (list (ada-xref-get-project-field 'build_dir))
1276
1277 (ada-get-absolute-dir-list
1278 (ada-xref-get-project-field 'src_dir)
1279 (ada-xref-get-project-field 'build_dir))
1280
1281 ;; Add the standard runtime at the end
1282 ada-xref-runtime-library-specs-path)))
1283
1284
1285 (defun ada-get-ali-file-name (file)
1286 "Create the ali file name for the ada-file FILE.
1287 The file is searched for in every directory shown in the obj_dir lines of
1288 the project file."
1289
1290 ;; This function has to handle the special case of non-standard
1291 ;; file names (i.e. not .adb or .ads)
1292 ;; The trick is the following:
1293 ;; 1- replace the extension of the current file with .ali,
1294 ;; and look for this file
1295 ;; 2- If this file is found:
1296 ;; grep the "^U" lines, and make sure we are not reading the
1297 ;; .ali file for a spec file. If we are, go to step 3.
1298 ;; 3- If the file is not found or step 2 failed:
1299 ;; find the name of the "other file", ie the body, and look
1300 ;; for its associated .ali file by subtituing the extension
1301
1302 (save-excursion
1303 (set-buffer (get-file-buffer file))
1304 (let ((short-ali-file-name
1305 (concat (file-name-sans-extension (file-name-nondirectory file))
1306 ".ali"))
1307 ali-file-name)
1308 ;; First step
1309 ;; we take the first possible completion
1310 (setq ali-file-name (ada-find-ali-file-in-dir short-ali-file-name))
1311
1312 ;; If we have found the .ali file, but the source file was a spec
1313 ;; with a non-standard name, search the .ali file for the body if any,
1314 ;; since the xref information is more complete in that one
1315 (unless ali-file-name
1316 (if (not (string= (file-name-extension file) "ads"))
1317 (let ((is-spec nil)
1318 (specs ada-spec-suffixes)
1319 body-ali)
1320 (while specs
1321 (if (string-match (concat (regexp-quote (car specs)) "$")
1322 file)
1323 (set 'is-spec t))
1324 (set 'specs (cdr specs)))
1325
1326 (if is-spec
1327 (set 'body-ali
1328 (ada-find-ali-file-in-dir
1329 (concat (file-name-sans-extension
1330 (file-name-nondirectory
1331 (ada-other-file-name)))
1332 ".ali"))))
1333 (if body-ali
1334 (set 'ali-file-name body-ali))))
1335
1336 ;; else we did not find the .ali file
1337 ;; Second chance: in case the files do not have standard names (such
1338 ;; as for instance file_s.ada and file_b.ada), try to go to the
1339 ;; other file and look for its ali file
1340 (setq short-ali-file-name
1341 (concat (file-name-sans-extension
1342 (file-name-nondirectory (ada-other-file-name)))
1343 ".ali"))
1344 (setq ali-file-name (ada-find-ali-file-in-dir short-ali-file-name))
1345
1346 ;; If still not found, try to recompile the file
1347 (if (not ali-file-name)
1348 (progn
1349 ;; recompile only if the user asked for this
1350 (if ada-xref-create-ali
1351 (ada-xref-current file ali-file-name))
1352 (error "Ali file not found. Recompile your file")))
1353 )
1354
1355 ;; same if the .ali file is too old and we must recompile it
1356 (if (and (file-newer-than-file-p file ali-file-name)
1357 ada-xref-create-ali)
1358 (ada-xref-current file ali-file-name))
1359
1360 ;; else returns the correct absolute file name
1361 (expand-file-name ali-file-name))
1362 ))
1363
1364 (defun ada-get-ada-file-name (file original-file)
1365 "Create the complete file name (+directory) for FILE.
1366 The original file (where the user was) is ORIGINAL-FILE. Search in project
1367 file for possible paths."
1368
1369 (save-excursion
1370
1371 ;; If the buffer for original-file, use it to get the values from the
1372 ;; project file, otherwise load the file and its project file
1373 (let ((buffer (get-file-buffer original-file)))
1374 (if buffer
1375 (set-buffer buffer)
1376 (find-file original-file)
1377 (ada-require-project-file)))
1378
1379 ;; we choose the first possible completion and we
1380 ;; return the absolute file name
1381 (let ((filename (ada-find-src-file-in-dir file)))
1382 (if filename
1383 (expand-file-name filename)
1384 (error (concat
1385 (file-name-nondirectory file)
1386 " not found in src_dir. Please check your project file")))
1387
1388 )))
1389
1390 (defun ada-find-file-number-in-ali (file)
1391 "Returns the file number for FILE in the associated ali file."
1392 (set-buffer (ada-get-ali-buffer file))
1393 (goto-char (point-min))
1394
1395 (let ((begin (re-search-forward "^D")))
1396 (beginning-of-line)
1397 (re-search-forward (concat "^D " (file-name-nondirectory file)))
1398 (count-lines begin (point))))
1399
1400 (defun ada-read-identifier (pos)
1401 "Returns the identlist around POS and switch to the .ali buffer."
1402
1403 ;; If there's a compilation in progress, it's probably because the
1404 ;; .ali file didn't exist. So we should wait...
1405 (if compilation-in-progress
1406 (progn
1407 (message "Compilation in progress. Try again when it is finished")
1408 (set 'quit-flag t)))
1409
1410 ;; If at end of buffer (e.g the buffer is empty), error
1411 (if (>= (point) (point-max))
1412 (error "No identifier on point"))
1413
1414 ;; goto first character of the identifier/operator (skip backward < and >
1415 ;; since they are part of multiple character operators
1416 (goto-char pos)
1417 (skip-chars-backward "a-zA-Z0-9_<>")
1418
1419 ;; check if it really is an identifier
1420 (if (ada-in-comment-p)
1421 (error "Inside comment"))
1422
1423 (let (identifier identlist)
1424 ;; Just in front of a string => we could have an operator declaration,
1425 ;; as in "+", "-", ..
1426 (if (= (char-after) ?\")
1427 (forward-char 1))
1428
1429 ;; if looking at an operator
1430 ;; This is only true if:
1431 ;; - the symbol is +, -, ...
1432 ;; - the symbol is made of letters, and not followed by _ or a letter
1433 (if (and (looking-at ada-operator-re)
1434 (or (not (= (char-syntax (char-after)) ?w))
1435 (not (or (= (char-syntax (char-after (match-end 0))) ?w)
1436 (= (char-after (match-end 0)) ?_)))))
1437 (progn
1438 (if (and (= (char-before) ?\")
1439 (= (char-after (+ (length (match-string 0)) (point))) ?\"))
1440 (forward-char -1))
1441 (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
1442
1443 (if (ada-in-string-p)
1444 (error "Inside string or character constant"))
1445 (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
1446 (error "No cross-reference available for reserved keyword"))
1447 (if (looking-at "[a-zA-Z0-9_]+")
1448 (set 'identifier (match-string 0))
1449 (error "No identifier around")))
1450
1451 ;; Build the identlist
1452 (set 'identlist (ada-make-identlist))
1453 (ada-set-name identlist (downcase identifier))
1454 (ada-set-line identlist
1455 (number-to-string (count-lines (point-min) (point))))
1456 (ada-set-column identlist
1457 (number-to-string (1+ (current-column))))
1458 (ada-set-file identlist (buffer-file-name))
1459 identlist
1460 ))
1461
1462 (defun ada-get-all-references (identlist)
1463 "Completes and returns IDENTLIST with the information extracted
1464 from the ali file (definition file and places where it is referenced)."
1465
1466 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
1467 declaration-found)
1468 (set-buffer ali-buffer)
1469 (goto-char (point-min))
1470 (ada-set-on-declaration identlist nil)
1471
1472 ;; First attempt: we might already be on the declaration of the identifier
1473 ;; We want to look for the declaration only in a definite interval (after
1474 ;; the "^X ..." line for the current file, and before the next "^X" line
1475
1476 (if (re-search-forward
1477 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
1478 nil t)
1479 (let ((bound (save-excursion (re-search-forward "^X " nil t))))
1480 (set 'declaration-found
1481 (re-search-forward
1482 (concat "^" (ada-line-of identlist)
1483 "." (ada-column-of identlist)
1484 "[ *]" (ada-name-of identlist)
1485 " \\(.*\\)$") bound t))
1486 (if declaration-found
1487 (ada-set-on-declaration identlist t))
1488 ))
1489
1490 ;; If declaration is still nil, then we were not on a declaration, and
1491 ;; have to fall back on other algorithms
1492
1493 (unless declaration-found
1494
1495 ;; Since we alread know the number of the file, search for a direct
1496 ;; reference to it
1497 (goto-char (point-min))
1498 (set 'declaration-found t)
1499 (ada-set-ali-index
1500 identlist
1501 (number-to-string (ada-find-file-number-in-ali
1502 (ada-file-of identlist))))
1503 (unless (re-search-forward (concat (ada-ali-index-of identlist)
1504 "|\\([0-9]+.[0-9]+ \\)*"
1505 (ada-line-of identlist)
1506 "[^0-9]"
1507 (ada-column-of identlist))
1508 nil t)
1509
1510 ;; if we did not find it, it may be because the first reference
1511 ;; is not required to have a 'unit_number|' item included.
1512 ;; Or maybe we are already on the declaration...
1513 (unless (re-search-forward (concat "^\\([a-zA-Z0-9_.\"]+[ *]\\)*"
1514 (ada-line-of identlist)
1515 "[^0-9]"
1516 (ada-column-of identlist))
1517 nil t)
1518
1519 ;; If still not found, then either the declaration is unknown
1520 ;; or the source file has been modified since the ali file was
1521 ;; created
1522 (set 'declaration-found nil)
1523 )
1524 )
1525
1526 ;; Last check to be completly sure we have found the correct line (the
1527 ;; ali might not be up to date for instance)
1528 (if declaration-found
1529 (progn
1530 (beginning-of-line)
1531 ;; while we have a continuation line, go up one line
1532 (while (looking-at "^\\.")
1533 (previous-line 1))
1534 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1535 (ada-name-of identlist) "[ <]"))
1536 (set 'declaration-found nil))))
1537
1538 ;; Still no success ! The ali file must be too old, and we need to
1539 ;; use a basic algorithm based on guesses. Note that this only happens
1540 ;; if the user does not want us to automatically recompile files
1541 ;; automatically
1542 (unless declaration-found
1543 (if (ada-xref-find-in-modified-ali identlist)
1544 (set 'declaration-found t)
1545 ;; no more idea to find the declaration. Give up
1546 (progn
1547 (kill-buffer ali-buffer)
1548 (error (concat "No declaration of " (ada-name-of identlist)
1549 " found."))
1550 )))
1551 )
1552
1553
1554 ;; Now that we have found a suitable line in the .ali file, get the
1555 ;; information available
1556 (beginning-of-line)
1557 (if declaration-found
1558 (let ((current-line (buffer-substring
1559 (point) (save-excursion (end-of-line) (point)))))
1560 (save-excursion
1561 (next-line 1)
1562 (beginning-of-line)
1563 (while (looking-at "^\\.\\(.*\\)")
1564 (set 'current-line (concat current-line (match-string 1)))
1565 (next-line 1))
1566 )
1567
1568 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
1569 (ada-set-declare-file
1570 identlist
1571 (ada-get-ada-file-name (match-string 1)
1572 (ada-file-of identlist))))
1573
1574 (ada-set-references identlist current-line)
1575 ))
1576 ))
1577
1578 (defun ada-xref-find-in-modified-ali (identlist)
1579 "Find the matching position for IDENTLIST in the current ali buffer.
1580 This function is only called when the file was not up-to-date, so we need
1581 to make some guesses.
1582 This function is disabled for operators, and only works for identifiers."
1583
1584 (unless (= (string-to-char (ada-name-of identlist)) ?\")
1585 (progn
1586 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1587 (my-regexp (concat "[ *]"
1588 (regexp-quote (ada-name-of identlist)) " "))
1589 (line-ada "--")
1590 (col-ada "--")
1591 (line-ali 0)
1592 (len 0)
1593 (choice 0)
1594 (ali-buffer (current-buffer)))
1595
1596 (goto-char (point-max))
1597 (while (re-search-backward my-regexp nil t)
1598 (save-excursion
1599 (set 'line-ali (count-lines (point-min) (point)))
1600 (beginning-of-line)
1601 ;; have a look at the line and column numbers
1602 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1603 (progn
1604 (setq line-ada (match-string 1))
1605 (setq col-ada (match-string 2)))
1606 (setq line-ada "--")
1607 (setq col-ada "--")
1608 )
1609 ;; construct a list with the file names and the positions within
1610 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
1611 (add-to-list
1612 'declist (list line-ali (match-string 1) line-ada col-ada))
1613 )
1614 )
1615 )
1616
1617 ;; how many possible declarations have we found ?
1618 (setq len (length declist))
1619 (cond
1620 ;; none => error
1621 ((= len 0)
1622 (kill-buffer (current-buffer))
1623 (error (concat "No declaration of "
1624 (ada-name-of identlist)
1625 " recorded in .ali file")))
1626
1627 ;; one => should be the right one
1628 ((= len 1)
1629 (goto-line (caar declist)))
1630
1631 ;; more than one => display choice list
1632 (t
1633 (with-output-to-temp-buffer "*choice list*"
1634
1635 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1636 (princ "Possible declarations are:\n\n")
1637 (princ " no. in file at line col\n")
1638 (princ " --- --------------------- ---- ----\n")
1639 (let ((counter 1))
1640 (while (<= counter len)
1641 (princ (format " %2d) %-21s %4s %4s\n"
1642 counter
1643 (ada-get-ada-file-name
1644 (nth 1 (nth (1- counter) declist))
1645 (ada-file-of identlist))
1646 (nth 2 (nth (1- counter) declist))
1647 (nth 3 (nth (1- counter) declist))
1648 ))
1649 (setq counter (1+ counter))
1650 ) ; end of while
1651 ) ; end of let
1652 ) ; end of with-output-to ...
1653 (setq choice nil)
1654 (while (or
1655 (not choice)
1656 (not (integerp choice))
1657 (< choice 1)
1658 (> choice len))
1659 (setq choice (string-to-int
1660 (read-from-minibuffer "Enter No. of your choice: "))))
1661 (set-buffer ali-buffer)
1662 (goto-line (car (nth (1- choice) declist)))
1663 ))))))
1664
1665
1666 (defun ada-find-in-ali (identlist &optional other-frame)
1667 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1668 If OTHER-FRAME is non nil, and `ada-xref-other-buffer' is non nil,
1669 opens a new window to show the declaration."
1670
1671 (ada-get-all-references identlist)
1672 (let ((ali-line (ada-references-of identlist))
1673 file line col)
1674
1675 ;; If we were on a declaration, go to the body
1676 (if (ada-on-declaration identlist)
1677 (if (string-match "\\([0-9]+\\)[bc]\\([0-9]+\\)" ali-line)
1678 (progn
1679 (setq line (match-string 1 ali-line)
1680 col (match-string 2 ali-line))
1681 ;; it there was a file number in the same line
1682 (if (string-match "\\([0-9]+\\)|\\([^|bc]+\\)?[bc]" ali-line)
1683 (let ((file-number (match-string 1 ali-line)))
1684 (goto-char (point-min))
1685 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1686 (string-to-number file-number))
1687 (set 'file (match-string 1))
1688 )
1689 ;; Else get the nearest file
1690 (set 'file (ada-declare-file-of identlist))
1691 )
1692 )
1693 (error "No body found"))
1694
1695 ;; Else we were not on the declaration, find the place for it
1696 (string-match "\\([0-9]+\\)[a-zA-Z+]\\([0-9]+\\)[ *]" ali-line)
1697 (setq line (match-string 1 ali-line)
1698 col (match-string 2 ali-line)
1699 file (ada-declare-file-of identlist))
1700 )
1701
1702 ;; Now go to the buffer
1703 (ada-xref-change-buffer
1704 (ada-get-ada-file-name file (ada-file-of identlist))
1705 (string-to-number line)
1706 (1- (string-to-number col))
1707 identlist
1708 other-frame)
1709 ))
1710
1711 (defun ada-xref-change-buffer
1712 (file line column identlist &optional other-frame)
1713 "Select and display FILE, at LINE and COLUMN. The new file is
1714 associated with the same project file as the one for IDENTLIST.
1715 If we do not end on the same identifier as IDENTLIST, find the closest
1716 match. Kills the .ali buffer at the end.
1717 If OTHER-FRAME is non-nil, creates a new frame to show the file."
1718
1719 (let (prj-file
1720 declaration-buffer
1721 (ali-buffer (current-buffer)))
1722
1723 ;; get the current project file for the source ada file
1724 (save-excursion
1725 (set-buffer (get-file-buffer (ada-file-of identlist)))
1726 (set 'prj-file ada-prj-prj-file))
1727
1728 ;; Select and display the destination buffer
1729 (if ada-xref-other-buffer
1730 (if other-frame
1731 (find-file-other-frame file)
1732 (set 'declaration-buffer (find-file-noselect file))
1733 (set-buffer declaration-buffer)
1734 (switch-to-buffer-other-window declaration-buffer)
1735 )
1736 (find-file file)
1737 )
1738
1739 ;; If the new buffer is not already associated with a project file, do it
1740 (unless (my-local-variable-if-set-p 'ada-prj-prj-file (current-buffer))
1741 (set (make-local-variable 'ada-prj-prj-file) prj-file))
1742
1743 ;; move the cursor to the correct position
1744 (push-mark)
1745 (goto-line line)
1746 (move-to-column column)
1747
1748 ;; If we are not on the identifier, the ali file was not up-to-date.
1749 ;; Try to find the nearest position where the identifier is found,
1750 ;; this is probably the right one.
1751 (unless (looking-at (ada-name-of identlist))
1752 (ada-xref-search-nearest (ada-name-of identlist)))
1753
1754 (kill-buffer ali-buffer)))
1755
1756
1757 (defun ada-xref-search-nearest (name)
1758 "Searches for NAME nearest to the position recorded in the Xref file.
1759 It returns the position of the declaration in the buffer or nil if not found."
1760 (let ((orgpos (point))
1761 (newpos nil)
1762 (diff nil))
1763
1764 (goto-char (point-max))
1765
1766 ;; loop - look for all declarations of name in this file
1767 (while (search-backward name nil t)
1768
1769 ;; check if it really is a complete Ada identifier
1770 (if (and
1771 (not (save-excursion
1772 (goto-char (match-end 0))
1773 (looking-at "_")))
1774 (not (ada-in-string-or-comment-p))
1775 (or
1776 ;; variable declaration ?
1777 (save-excursion
1778 (skip-chars-forward "a-zA-Z_0-9" )
1779 (ada-goto-next-non-ws)
1780 (looking-at ":[^=]"))
1781 ;; procedure, function, task or package declaration ?
1782 (save-excursion
1783 (ada-goto-previous-word)
1784 (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]\\>"))))
1785
1786 ;; check if it is nearer than the ones before if any
1787 (if (or (not diff)
1788 (< (abs (- (point) orgpos)) diff))
1789 (progn
1790 (setq newpos (point)
1791 diff (abs (- newpos orgpos))))))
1792 )
1793
1794 (if newpos
1795 (progn
1796 (message "ATTENTION: this declaration is only a (good) guess ...")
1797 (goto-char newpos))
1798 nil)))
1799
1800
1801 ;; Find the parent library file of the current file
1802 (defun ada-goto-parent ()
1803 "Go to the parent library file."
1804 (interactive)
1805 (ada-require-project-file)
1806
1807 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
1808 (unit-name nil)
1809 (body-name nil)
1810 (ali-name nil))
1811 (save-excursion
1812 (set-buffer buffer)
1813 (goto-char (point-min))
1814 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
1815 (setq unit-name (match-string 1))
1816 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
1817 (progn
1818 (kill-buffer buffer)
1819 (error "No parent unit !"))
1820 (setq unit-name (match-string 1 unit-name))
1821 )
1822
1823 ;; look for the file name for the parent unit specification
1824 (goto-char (point-min))
1825 (re-search-forward (concat "^W " unit-name
1826 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
1827 "\\([^ \t\n]+\\)"))
1828 (setq body-name (match-string 1))
1829 (setq ali-name (match-string 2))
1830 (kill-buffer buffer)
1831 )
1832
1833 (setq ali-name (ada-find-ali-file-in-dir ali-name))
1834
1835 (save-excursion
1836 ;; Tries to open the new ali file to find the spec file
1837 (if ali-name
1838 (progn
1839 (find-file ali-name)
1840 (goto-char (point-min))
1841 (re-search-forward (concat "^U " unit-name "%s[ \t]+"
1842 "\\([^ \t]+\\)"))
1843 (setq body-name (match-string 1))
1844 (kill-buffer (current-buffer))
1845 )
1846 )
1847 )
1848
1849 (find-file body-name)
1850 ))
1851
1852 (defun ada-make-filename-from-adaname (adaname)
1853 "Determine the filename in which ADANAME is found.
1854 This is a GNAT specific function that uses gnatkrunch."
1855 (let (krunch-buf)
1856 (setq krunch-buf (generate-new-buffer "*gkrunch*"))
1857 (save-excursion
1858 (set-buffer krunch-buf)
1859 ;; send adaname to external process `gnatkr'.
1860 (call-process "gnatkr" nil krunch-buf nil
1861 adaname ada-krunch-args)
1862 ;; fetch output of that process
1863 (setq adaname (buffer-substring
1864 (point-min)
1865 (progn
1866 (goto-char (point-min))
1867 (end-of-line)
1868 (point))))
1869 (kill-buffer krunch-buf)))
1870 adaname
1871 )
1872
1873 (defun ada-make-body-gnatstub ()
1874 "Create an Ada package body in the current buffer.
1875 This function uses the `gnatstub' program to create the body.
1876 This function typically is to be hooked into `ff-file-created-hooks'."
1877 (interactive)
1878
1879 (save-some-buffers nil nil)
1880
1881 (ada-require-project-file)
1882
1883 (delete-region (point-min) (point-max))
1884
1885 ;; Call the external process gnatstub
1886 (let* ((gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
1887 (filename (buffer-file-name (car (cdr (buffer-list)))))
1888 (output (concat (file-name-sans-extension filename) ".adb"))
1889 (gnatstub-cmd (concat "gnatstub " gnatstub-opts " " filename))
1890 (buffer (get-buffer-create "*gnatstub*")))
1891
1892 (save-excursion
1893 (set-buffer buffer)
1894 (compilation-minor-mode 1)
1895 (erase-buffer)
1896 (insert gnatstub-cmd)
1897 (newline)
1898 )
1899 ;; call gnatstub to create the body file
1900 (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
1901
1902 (if (save-excursion
1903 (set-buffer buffer)
1904 (goto-char (point-min))
1905 (search-forward "command not found" nil t))
1906 (progn
1907 (message "gnatstub was not found -- using the basic algorithm")
1908 (sleep-for 2)
1909 (kill-buffer buffer)
1910 (ada-make-body))
1911
1912 ;; Else clean up the output
1913
1914 ;; Kill the temporary buffer created by find-file
1915 (set-buffer-modified-p nil)
1916 (kill-buffer (current-buffer))
1917
1918 (if (file-exists-p output)
1919 (progn
1920 (find-file output)
1921 (kill-buffer buffer))
1922
1923 ;; display the error buffer
1924 (display-buffer buffer)
1925 )
1926 )))
1927
1928
1929 (defun ada-xref-initialize ()
1930 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
1931 For instance, it creates the gnat-specific menus, sets some hooks for
1932 find-file...."
1933 (make-local-hook 'ff-file-created-hooks)
1934 ;; This should really be an `add-hook'. -stef
1935 (setq ff-file-created-hooks 'ada-make-body-gnatstub)
1936
1937 ;; Read the project file and update the search path
1938 ;; before looking for the other file
1939 (make-local-hook 'ff-pre-find-hooks)
1940 (add-hook 'ff-pre-find-hooks 'ada-require-project-file nil t)
1941
1942 ;; Completion for file names in the mini buffer should ignore .ali files
1943 (add-to-list 'completion-ignored-extensions ".ali")
1944 )
1945
1946
1947 ;; ----- Add to ada-mode-hook ---------------------------------------------
1948
1949 ;; Set the keymap once and for all, so that the keys set by the user in his
1950 ;; config file are not overwritten every time we open a new file.
1951 (ada-add-ada-menu)
1952 (ada-add-keymap)
1953
1954 (add-hook 'ada-mode-hook 'ada-xref-initialize)
1955
1956 ;; Use ddd as the default debugger if it was found
1957 (if (ada-find-file-in-dir "ddd" exec-path)
1958 (set 'ada-prj-default-debugger "ddd --tty -fullname -toolbar"))
1959
1960 ;; Initializes the cross references to the runtime library
1961 (ada-initialize-runtime-library)
1962
1963 ;; Add these standard directories to the search path
1964 (set 'ada-search-directories
1965 (append (mapcar 'directory-file-name ada-xref-runtime-library-specs-path)
1966 ada-search-directories))
1967
1968 ;; Make sure that the files are always associated with a project file. Since
1969 ;; the project file has some fields that are used for the editor (like the
1970 ;; casing exceptions), it has to be read before the user edits a file).
1971 (add-hook 'ada-mode-hook
1972 (lambda()
1973 (let ((file (ada-prj-find-prj-file t)))
1974 (if file (ada-reread-prj-file file)))))
1975
1976 (provide 'ada-xref)
1977
1978 ;;; ada-xref.el ends here