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