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