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