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