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