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