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