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