1 ;; ada-xref.el --- for lookup and completion in Ada mode
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
8 ;; Rolf Ebert <ebert@inf.enst.fr>
9 ;; Emmanuel Briot <briot@gnat.com>
10 ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
11 ;; Keywords: languages ada xref
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;;; This Package provides a set of functions to use the output of the
31 ;;; cross reference capabilities of the GNAT Ada compiler
32 ;;; for lookup and completion in Ada mode.
34 ;;; If a file *.`adp' exists in the ada-file directory, then it is
35 ;;; read for configuration informations. It is read only the first
36 ;;; time a cross-reference is asked for, and is not read later.
38 ;;; You need Emacs >= 20.2 to run this package
46 ;; ----- Requirements -----------------------------------------------------
53 ;; ------ User variables
54 (defcustom ada-xref-other-buffer t
55 "*If nil, always display the cross-references in the same buffer.
56 Otherwise create either a new buffer or a new frame."
57 :type
'boolean
:group
'ada
)
59 (defcustom ada-xref-create-ali nil
60 "*If non-nil, run gcc whenever the cross-references are not up-to-date.
61 If nil, the cross-reference mode never runs gcc."
62 :type
'boolean
:group
'ada
)
64 (defcustom ada-xref-confirm-compile nil
65 "*If non-nil, ask for confirmation before compiling or running the application."
66 :type
'boolean
:group
'ada
)
68 (defcustom ada-krunch-args
"0"
69 "*Maximum number of characters for filenames created by `gnatkr'.
70 Set to 0, if you don't use crunched filenames. This should be a string."
71 :type
'string
:group
'ada
)
73 (defcustom ada-gnat-cmd
"gnat"
74 "Default GNAT project file parser.
75 Will be run with args \"list -v -Pfile.gpr\".
76 Default is standard GNAT distribution; alternate \"gnatpath\"
77 is faster, available from Ada mode web site."
78 :type
'string
:group
'ada
)
80 (defcustom ada-gnatls-args
'("-v")
81 "*Arguments to pass to `gnatls' to find location of the runtime.
82 Typical use is to pass `--RTS=soft-floats' on some systems that support it.
84 You can also add `-I-' if you do not want the current directory to be included.
85 Otherwise, going from specs to bodies and back will first look for files in the
86 current directory. This only has an impact if you are not using project files,
87 but only ADA_INCLUDE_PATH."
88 :type
'(repeat string
) :group
'ada
)
90 (defcustom ada-prj-default-comp-opt
"-gnatq -gnatQ"
91 "Default compilation options."
92 :type
'string
:group
'ada
)
94 (defcustom ada-prj-default-bind-opt
""
95 "Default binder options."
96 :type
'string
:group
'ada
)
98 (defcustom ada-prj-default-link-opt
""
99 "Default linker options."
100 :type
'string
:group
'ada
)
102 (defcustom ada-prj-default-gnatmake-opt
"-g"
103 "Default options for `gnatmake'."
104 :type
'string
:group
'ada
)
106 (defcustom ada-prj-default-gpr-file
""
107 "Default GNAT project file.
108 If non-empty, this file is parsed to set the source and object directories for
109 the Ada mode project."
110 :type
'string
:group
'ada
)
112 (defcustom ada-prj-ada-project-path-sep
113 (cond ((boundp 'path-separator
) path-separator
) ; 20.3+
114 ((memq system-type
'(windows-nt ms-dos
)) ";")
116 "Default separator for ada_project_path project variable."
117 :type
'string
:group
'ada
)
119 (defcustom ada-prj-gnatfind-switches
"-rf"
120 "Default switches to use for `gnatfind'.
121 You should modify this variable, for instance to add `-a', if you are working
122 in an environment where most ALI files are write-protected.
123 The command `gnatfind' is used every time you choose the menu
124 \"Show all references\"."
125 :type
'string
:group
'ada
)
127 (defcustom ada-prj-default-check-cmd
128 (concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}"
129 " -cargs ${comp_opt}")
130 "*Default command to be used to compile a single file.
131 Emacs will substitute the current filename for ${full_current}, or add
132 the filename at the end. This is the same syntax as in the project file."
133 :type
'string
:group
'ada
)
135 (defcustom ada-prj-default-comp-cmd
136 (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
138 "*Default command to be used to compile a single file.
139 Emacs will substitute the current filename for ${full_current}, or add
140 the filename at the end. This is the same syntax as in the project file."
141 :type
'string
:group
'ada
)
143 (defcustom ada-prj-default-debugger
"${cross_prefix}gdb"
144 "*Default name of the debugger."
145 :type
'string
:group
'ada
)
147 (defcustom ada-prj-default-make-cmd
148 (concat "${cross_prefix}gnatmake -o ${main} ${main} ${gnatmake_opt} "
149 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
150 "*Default command to be used to compile the application.
151 This is the same syntax as in the project file."
152 :type
'string
:group
'ada
)
154 (defcustom ada-prj-default-project-file
""
155 "*Name of the current project file.
156 Emacs will not try to use the search algorithm to find the project file if
157 this string is not empty. It is set whenever a project file is found."
158 :type
'(file :must-match t
) :group
'ada
)
160 (defcustom ada-gnatstub-opts
"-q -I${src_dir}"
161 "*Options to pass to `gnatsub' to generate the body of a package.
162 This has the same syntax as in the project file (with variable substitution)."
163 :type
'string
:group
'ada
)
165 (defcustom ada-always-ask-project nil
166 "*If nil, use default values when no project file was found.
167 Otherwise, ask the user for the name of the project file to use."
168 :type
'boolean
:group
'ada
)
170 (defconst ada-on-ms-windows
(memq system-type
'(windows-nt))
171 "True if we are running on Windows.")
173 (defcustom ada-tight-gvd-integration nil
174 "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
175 If GVD is not the debugger used, nothing happens."
176 :type
'boolean
:group
'ada
)
178 (defcustom ada-xref-search-with-egrep t
179 "*If non-nil, use egrep to find the possible declarations for an entity.
180 This alternate method is used when the exact location was not found in the
181 information provided by GNAT. However, it might be expensive if you have a lot
182 of sources, since it will search in all the files in your project."
183 :type
'boolean
:group
'ada
)
185 (defvar ada-load-project-hook nil
186 "Hook that is run when loading a project file.
187 Each function in this hook takes one argument FILENAME, that is the name of
188 the project file to load.
189 This hook should be used to support new formats for the project files.
191 If the function can load the file with the given filename, it should create a
192 buffer that contains a conversion of the file to the standard format of the
193 project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\"
194 lines.) It should return nil if it doesn't know how to convert that project
198 ;; ------- Nothing to be modified by the user below this
199 (defvar ada-last-prj-file
""
200 "Name of the last project file entered by the user.")
202 (defconst ada-prj-file-extension
".adp"
203 "The extension used for project files.")
205 (defvar ada-xref-runtime-library-specs-path
'()
206 "Directories where the specs for the standard library is found.
207 This is used for cross-references.")
209 (defvar ada-xref-runtime-library-ali-path
'()
210 "Directories where the ali for the standard library is found.
211 This is used for cross-references.")
213 (defvar ada-xref-pos-ring
'()
214 "List of positions selected by the cross-references functions.
215 Used to go back to these positions.")
217 (defvar ada-cd-command
218 (if (string-match "cmdproxy.exe" shell-file-name
)
221 "Command to use to change to a specific directory.
222 On Windows systems using `cmdproxy.exe' as the shell,
223 we need to use `/d' or the drive is never changed.")
225 (defvar ada-command-separator
(if ada-on-ms-windows
" && " "\n")
226 "Separator to use between multiple commands to `compile' or `start-process'.
227 `cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use
230 (defconst ada-xref-pos-ring-max
16
231 "Number of positions kept in the list `ada-xref-pos-ring'.")
233 (defvar ada-operator-re
234 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
235 "Regexp to match for operators.")
237 (defvar ada-xref-project-files
'()
238 "Associative list of project files with properties.
239 It has the format: (project project ...)
240 A project has the format: (project-file . project-plist)
241 \(See 'apropos plist' for operations on property lists).
242 See `ada-default-prj-properties' for the list of valid properties.
243 The current project is retrieved with `ada-xref-current-project'.
244 Properties are retrieved with `ada-xref-get-project-field', set with
245 `ada-xref-set-project-field'. If project properties are accessed with no
246 project file, a (nil . default-properties) entry is created.")
249 ;; ----- Identlist manipulation -------------------------------------------
250 ;; An identlist is a vector that is used internally to reference an identifier
251 ;; To facilitate its use, we provide the following macros
253 (defmacro ada-make-identlist
() (make-vector 8 nil
))
254 (defmacro ada-name-of
(identlist) (list 'aref identlist
0))
255 (defmacro ada-line-of
(identlist) (list 'aref identlist
1))
256 (defmacro ada-column-of
(identlist) (list 'aref identlist
2))
257 (defmacro ada-file-of
(identlist) (list 'aref identlist
3))
258 (defmacro ada-ali-index-of
(identlist) (list 'aref identlist
4))
259 (defmacro ada-declare-file-of
(identlist) (list 'aref identlist
5))
260 (defmacro ada-references-of
(identlist) (list 'aref identlist
6))
261 (defmacro ada-on-declaration
(identlist) (list 'aref identlist
7))
263 (defmacro ada-set-name
(identlist name
) (list 'aset identlist
0 name
))
264 (defmacro ada-set-line
(identlist line
) (list 'aset identlist
1 line
))
265 (defmacro ada-set-column
(identlist col
) (list 'aset identlist
2 col
))
266 (defmacro ada-set-file
(identlist file
) (list 'aset identlist
3 file
))
267 (defmacro ada-set-ali-index
(identlist index
) (list 'aset identlist
4 index
))
268 (defmacro ada-set-declare-file
(identlist file
) (list 'aset identlist
5 file
))
269 (defmacro ada-set-references
(identlist ref
) (list 'aset identlist
6 ref
))
270 (defmacro ada-set-on-declaration
(ident value
) (list 'aset ident
7 value
))
272 (defsubst ada-get-ali-buffer
(file)
273 "Read the ali file FILE into a new buffer, and return the buffer's name."
274 (find-file-noselect (ada-get-ali-file-name file
)))
277 ;; -----------------------------------------------------------------------
279 (defun ada-quote-cmd (cmd)
280 "Duplicate all `\\' characters in CMD so that it can be passed to `compile'."
281 (mapconcat 'identity
(split-string cmd
"\\\\") "\\\\"))
283 (defun ada-find-executable (exec-name)
284 "Find the full path to the executable file EXEC-NAME.
285 If not found, throw an error.
286 On Windows systems, this will properly handle .exe extension as well."
287 (let ((result (or (ada-find-file-in-dir exec-name exec-path
)
288 (ada-find-file-in-dir (concat exec-name
".exe") exec-path
))))
291 (error "'%s' not found in path" exec-name
))))
293 (defun ada-initialize-runtime-library (cross-prefix)
294 "Initialize the variables for the runtime library location.
295 CROSS-PREFIX is the prefix to use for the `gnatls' command."
298 ;; if gnatls not found, just give up (may not be using GNAT)
299 (ada-find-executable (concat cross-prefix
"gnatls"))
303 (setq ada-xref-runtime-library-specs-path
'()
304 ada-xref-runtime-library-ali-path
'())
305 (set-buffer (get-buffer-create "*gnatls*"))
308 ;; Even if we get an error, delete the *gnatls* buffer
310 (let ((status (apply 'call-process gnatls
(append '(nil t nil
) ada-gnatls-args
))))
311 (goto-char (point-min))
313 ;; Since we didn't provide all the inputs gnatls expects, it returns status 4
315 (error (buffer-substring (point) (line-end-position))))
319 (search-forward "Source Search Path:")
321 (while (not (looking-at "^$"))
322 (back-to-indentation)
323 (if (looking-at "<Current_Directory>")
324 (add-to-list 'ada-xref-runtime-library-specs-path
".")
325 (add-to-list 'ada-xref-runtime-library-specs-path
326 (buffer-substring-no-properties
333 (search-forward "Object Search Path:")
335 (while (not (looking-at "^$"))
336 (back-to-indentation)
337 (if (looking-at "<Current_Directory>")
338 (add-to-list 'ada-xref-runtime-library-ali-path
".")
339 (add-to-list 'ada-xref-runtime-library-ali-path
340 (buffer-substring-no-properties
347 (set 'ada-xref-runtime-library-specs-path
348 (reverse ada-xref-runtime-library-specs-path
))
349 (set 'ada-xref-runtime-library-ali-path
350 (reverse ada-xref-runtime-library-ali-path
))
353 (defun ada-gnat-parse-gpr (plist gpr-file
)
354 "Set gpr_file, src_dir and obj_dir properties in PLIST by parsing GPR-FILE.
355 Return new value of PLIST.
356 GPR_FILE must be full path to file, normalized.
357 src_dir, obj_dir will include compiler runtime.
358 Assumes environment variable ADA_PROJECT_PATH is set properly."
359 (with-current-buffer (get-buffer-create "*gnatls*")
362 ;; this can take a long time; let the user know what's up
363 (message "Parsing %s ..." gpr-file
)
365 ;; Even if we get an error, delete the *gnatls* buffer
367 (let* ((cross-prefix (plist-get plist
'cross_prefix
))
368 (gnat (concat cross-prefix ada-gnat-cmd
))
369 ;; Putting quotes around gpr-file confuses gnatpath on Lynx; not clear why
370 (gpr-opt (concat "-P" gpr-file
))
373 (status (call-process gnat nil t nil
"list" "-v" gpr-opt
)))
374 (goto-char (point-min))
377 (error (buffer-substring (point) (line-end-position))))
381 (search-forward "Source Search Path:")
382 (forward-line 1) ; first directory in list
383 (while (not (looking-at "^$")) ; terminate on blank line
384 (back-to-indentation) ; skip whitespace
385 (add-to-list 'src-dir
386 (if (looking-at "<Current_Directory>")
389 (buffer-substring-no-properties
390 (point) (line-end-position)))))
395 (search-forward "Object Search Path:")
397 (while (not (looking-at "^$"))
398 (back-to-indentation)
399 (add-to-list 'obj-dir
400 (if (looking-at "<Current_Directory>")
403 (buffer-substring-no-properties
404 (point) (line-end-position)))))
408 (setq plist
(plist-put plist
'gpr_file gpr-file
))
409 (setq plist
(plist-put plist
'src_dir src-dir
))
410 (plist-put plist
'obj_dir obj-dir
)
413 (message "Parsing %s ... done" gpr-file
)
417 (defun ada-treat-cmd-string (cmd-string)
418 "Replace variable references ${var} in CMD-STRING with the appropriate value.
419 Also replace standard environment variables $var.
420 Assumes project exists.
421 As a special case, ${current} is replaced with the name of the current
422 file, minus extension but with directory, and ${full_current} is
423 replaced by the name including the extension."
425 (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string
)
427 (name (match-string 2 cmd-string
)))
429 ((string= name
"current")
430 (setq value
(file-name-sans-extension (buffer-file-name))))
431 ((string= name
"full_current")
432 (setq value
(buffer-file-name)))
435 (setq value
(ada-xref-get-project-field (intern name
))))))
437 ;; Check if there is an environment variable with the same name
439 (if (not (setq value
(getenv name
)))
440 (message "%s" (concat "No project or environment variable " name
" found"))))
444 (setq cmd-string
(replace-match "" t t cmd-string
)))
446 (setq cmd-string
(replace-match value t t cmd-string
)))
448 (let ((prefix (match-string 1 cmd-string
)))
449 (setq cmd-string
(replace-match
450 (mapconcat (lambda(x) (concat prefix x
)) value
" ")
453 (substitute-in-file-name cmd-string
))
456 (defun ada-xref-get-project-field (field)
457 "Extract the value of FIELD from the current project file.
458 Project variables are substituted.
460 Note that for src_dir and obj_dir, you should rather use
461 `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field'
462 which will in addition return the default paths."
464 (let* ((project-plist (cdr (ada-xref-current-project)))
465 (value (plist-get project-plist field
)))
468 ((eq field
'gnatmake_opt
)
469 (let ((gpr-file (plist-get project-plist
'gpr_file
)))
470 (if (not (string= gpr-file
""))
471 (setq value
(concat "-P\"" gpr-file
"\" " value
)))))
473 ;; FIXME: check for src_dir, obj_dir here, rather than requiring user to do it
477 ;; Substitute the ${...} constructs in all the strings, including
481 (ada-treat-cmd-string value
))
485 (mapcar (lambda(x) (if x
(ada-treat-cmd-string x
) x
)) value
))
491 (defun ada-xref-get-src-dir-field ()
492 "Return the full value for src_dir, including the default directories.
493 All the directories are returned as absolute directories."
495 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
497 ;; Add ${build_dir} in front of the path
500 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir
)
503 ;; Add the standard runtime at the end
504 ada-xref-runtime-library-specs-path
)))
506 (defun ada-xref-get-obj-dir-field ()
507 "Return the full value for obj_dir, including the default directories.
508 All the directories are returned as absolute directories."
510 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
512 ;; Add ${build_dir} in front of the path
515 (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir
)
518 ;; Add the standard runtime at the end
519 ada-xref-runtime-library-ali-path
)))
521 (defun ada-xref-set-project-field (field value
)
522 "Set FIELD to VALUE in current project. Assumes project exists."
523 ;; same algorithm to find project-plist as ada-xref-current-project
524 (let* ((file-name (ada-xref-current-project-file))
525 (project-plist (cdr (assoc file-name ada-xref-project-files
))))
527 (setq project-plist
(plist-put project-plist field value
))
528 (setcdr (assoc file-name ada-xref-project-files
) project-plist
)))
530 (defun ada-xref-update-project-menu ()
531 "Update the menu Ada->Project, with the list of available project files."
532 ;; Create the standard items.
535 ["Load..." ada-set-default-project-file t
]
536 ["New..." ada-prj-new t
]
537 ["Edit..." ada-prj-edit t
]
539 ;; Add the project files
542 (let* ((name (or (car x
) "<default>"))
544 "Select the current project file."
546 (ada-select-prj-file ,name
))))
548 (file-name-nondirectory name
)
552 (equal ada-prj-default-project-file
556 (or ada-xref-project-files
'(nil))))))
558 (easy-menu-add-item ada-mode-menu
'() submenu
)))
561 ;;-------------------------------------------------------------
562 ;;-- Searching a file anywhere on the source path.
564 ;;-- The following functions provide support for finding a file anywhere
565 ;;-- on the source path, without providing an explicit directory.
566 ;;-- They also provide file name completion in the minibuffer.
568 ;;-- Public subprograms: ada-find-file
570 ;;-------------------------------------------------------------
572 (defun ada-do-file-completion (string predicate flag
)
573 "Completion function when reading a file from the minibuffer.
574 Completion is attempted in all the directories in the source path,
575 as defined in the project file."
576 ;; FIXME: doc arguments
578 ;; This function is not itself interactive, but it is called as part
579 ;; of the prompt of interactive functions, so we require a project
581 (ada-require-project-file)
583 (dirs (ada-xref-get-src-dir-field)))
586 (if (file-directory-p (car dirs
))
587 (set 'list
(append list
(file-name-all-completions string
(car dirs
)))))
588 (set 'dirs
(cdr dirs
)))
589 (cond ((equal flag
'lambda
)
594 (try-completion string
595 (mapcar (lambda (x) (cons x
1)) list
)
599 (defun ada-find-file (filename)
600 "Open FILENAME, from anywhere in the source path.
601 Completion is available."
603 (list (completing-read "File: " 'ada-do-file-completion
)))
604 (let ((file (ada-find-src-file-in-dir filename
)))
607 (error "%s not found in src_dir" filename
))))
610 ;; ----- Utilities -------------------------------------------------
612 (defun ada-require-project-file ()
613 "If the current project does not exist, load or create a default one.
614 Should only be called from interactive functions."
615 (if (string= "" ada-prj-default-project-file
)
616 (ada-reread-prj-file (ada-prj-find-prj-file t
))))
618 (defun ada-xref-current-project-file ()
619 "Return the current project file name; never nil.
620 Call `ada-require-project-file' first if a project must exist."
621 (if (not (string= "" ada-prj-default-project-file
))
622 ada-prj-default-project-file
623 (ada-prj-find-prj-file t
)))
625 (defun ada-xref-current-project ()
626 "Return the current project.
627 Call `ada-require-project-file' first to ensure a project exists."
628 (let ((file-name (ada-xref-current-project-file)))
629 (assoc file-name ada-xref-project-files
)))
631 (defun ada-show-current-project ()
632 "Display current project file name in message buffer."
634 (message (ada-xref-current-project-file)))
636 (defun ada-show-current-main ()
637 "Display current main file name in message buffer."
639 (message "ada-mode main: %s" (ada-xref-get-project-field 'main
)))
641 (defun ada-xref-push-pos (filename position
)
642 "Push (FILENAME, POSITION) on the position ring for cross-references."
643 (setq ada-xref-pos-ring
(cons (list position filename
) ada-xref-pos-ring
))
644 (if (> (length ada-xref-pos-ring
) ada-xref-pos-ring-max
)
645 (setcdr (nthcdr (1- ada-xref-pos-ring-max
) ada-xref-pos-ring
) nil
)))
647 (defun ada-xref-goto-previous-reference ()
648 "Go to the previous cross-reference we were on."
650 (if ada-xref-pos-ring
651 (let ((pos (car ada-xref-pos-ring
)))
652 (setq ada-xref-pos-ring
(cdr ada-xref-pos-ring
))
653 (find-file (car (cdr pos
)))
654 (goto-char (car pos
)))))
656 (defun ada-convert-file-name (name)
657 "Convert from NAME to a name that can be used by the compilation commands.
658 This is overridden on VMS to convert from VMS filenames to Unix filenames."
660 ;; FIXME: use convert-standard-filename instead
662 (defun ada-set-default-project-file (file)
663 "Set FILE as the current project file."
664 (interactive "fProject file:")
665 (ada-parse-prj-file file
)
666 (ada-select-prj-file file
))
668 ;; ------ Handling the project file -----------------------------
670 (defun ada-prj-find-prj-file (&optional no-user-question
)
671 "Find the project file associated with the current buffer.
672 If the buffer is not in Ada mode, or not associated with a file,
673 return `ada-prj-default-project-file'. Otherwise, search for a file with
674 the same base name as the Ada file, but extension given by
675 `ada-prj-file-extension' (default .adp). If not found, search for *.adp
676 in the current directory; if several are found, and NO-USER-QUESTION
677 is non-nil, prompt the user to select one. If none are found, return
682 (if (not (and (derived-mode-p 'ada-mode
)
685 ;; Not in an Ada buffer, or current buffer not associated
686 ;; with a file (for instance an emerge buffer)
689 ;; other cases: use a more complex algorithm
691 (let* ((current-file (buffer-file-name))
692 (first-choice (concat
693 (file-name-sans-extension current-file
)
694 ada-prj-file-extension
))
695 (dir (file-name-directory current-file
))
697 (prj-files (directory-files
699 (concat ".*" (regexp-quote
700 ada-prj-file-extension
) "$")))
705 ((file-exists-p first-choice
)
707 (set 'selected first-choice
))
709 ((= (length prj-files
) 1)
710 ;; Exactly one project file was found in the current directory
711 (set 'selected
(car prj-files
)))
713 ((and (> (length prj-files
) 1) (not no-user-question
))
714 ;; multiple project files in current directory, ask the user
715 (save-window-excursion
716 (with-output-to-temp-buffer "*choice list*"
717 (princ "There are more than one possible project file.\n")
718 (princ "Which one should we use ?\n\n")
719 (princ " no. file name \n")
720 (princ " --- ------------------------\n")
722 (while (<= counter
(length prj-files
))
723 (princ (format " %2d) %s\n"
725 (nth (1- counter
) prj-files
)))
726 (setq counter
(1+ counter
))
728 ))) ; end of with-output-to ...
732 (not (integerp choice
))
734 (> choice
(length prj-files
)))
735 (setq choice
(string-to-number
736 (read-from-minibuffer "Enter No. of your choice: "))))
737 (set 'selected
(nth (1- choice
) prj-files
))))
739 ((= (length prj-files
) 0)
740 ;; No project file in the current directory; ask user
741 (unless (or no-user-question
(not ada-always-ask-project
))
742 (setq ada-last-prj-file
744 (concat "project file [" ada-last-prj-file
"]:")
745 nil ada-last-prj-file
))
746 (unless (string= ada-last-prj-file
"")
747 (set 'selected ada-last-prj-file
))))
750 (or selected
"default.adp")
753 (defun ada-default-prj-properties ()
754 "Return the default project properties list with the current buffer as main."
756 (let ((file (buffer-file-name nil
)))
758 ;; variable name alphabetical order
759 'ada_project_path
(or (getenv "ADA_PROJECT_PATH") "")
760 'ada_project_path_sep ada-prj-ada-project-path-sep
761 'bind_opt ada-prj-default-bind-opt
762 'build_dir default-directory
763 'casing
(if (listp ada-case-exception-file
)
764 ada-case-exception-file
765 (list ada-case-exception-file
))
766 'check_cmd
(list ada-prj-default-check-cmd
) ;; FIXME: should not a list
767 'comp_cmd
(list ada-prj-default-comp-cmd
) ;; FIXME: should not a list
768 'comp_opt ada-prj-default-comp-opt
770 'debug_cmd
(concat ada-prj-default-debugger
771 " ${main}" (if ada-on-ms-windows
".exe")) ;; FIXME: don't need .exe?
772 'debug_post_cmd
(list nil
)
773 'debug_pre_cmd
(list (concat ada-cd-command
" ${build_dir}"))
774 'gnatmake_opt ada-prj-default-gnatmake-opt
775 'gnatfind_opt ada-prj-gnatfind-switches
776 'gpr_file ada-prj-default-gpr-file
777 'link_opt ada-prj-default-link-opt
779 (file-name-nondirectory
780 (file-name-sans-extension file
))
782 'make_cmd
(list ada-prj-default-make-cmd
) ;; FIXME: should not a list
785 'run_cmd
(list (concat "./${main}" (if ada-on-ms-windows
".exe")))
786 ;; FIXME: should not a list
787 ;; FIXME: don't need .exe?
791 (defun ada-parse-prj-file (prj-file)
792 "Read PRJ-FILE, set project properties in `ada-xref-project-files'."
793 (let ((project (ada-default-prj-properties)))
795 (setq prj-file
(expand-file-name prj-file
))
796 (if (string= (file-name-extension prj-file
) "gpr")
797 (set 'project
(ada-gnat-parse-gpr project prj-file
))
799 (set 'project
(ada-parse-prj-file-1 prj-file project
))
802 ;; Store the project properties
803 (if (assoc prj-file ada-xref-project-files
)
804 (setcdr (assoc prj-file ada-xref-project-files
) project
)
805 (add-to-list 'ada-xref-project-files
(cons prj-file project
)))
807 (ada-xref-update-project-menu)
810 (defun ada-parse-prj-file-1 (prj-file project
)
811 "Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
812 Return new value of PROJECT."
813 (let ((ada-buffer (current-buffer))
814 ;; fields that are lists or otherwise require special processing
815 ada_project_path casing comp_cmd check_cmd
816 debug_pre_cmd debug_post_cmd gpr_file make_cmd obj_dir src_dir run_cmd
)
818 ;; Give users a chance to use compiler-specific project file formats
819 (let ((buffer (run-hook-with-args-until-success
820 'ada-load-project-hook prj-file
)))
822 ;; we load the project file with no warnings; if it does not
823 ;; exist, we stay in the Ada buffer; no project variable
824 ;; settings will be found. That works for the default
825 ;; "default.adp", which does not exist as a file.
826 (setq buffer
(find-file-noselect prj-file nil
)))
830 (goto-char (point-min))
835 ;; ignore lines that don't have the format "name=value", put
836 ;; 'name', 'value' in match-string.
837 (if (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
839 ;; FIXME: strip trailing spaces
840 ;; variable name alphabetical order
841 ((string= (match-string 1) "ada_project_path")
842 (add-to-list 'ada_project_path
844 (substitute-in-file-name (match-string 2)))))
846 ((string= (match-string 1) "build_dir")
848 (plist-put project
'build_dir
849 (file-name-as-directory (match-string 2)))))
851 ((string= (match-string 1) "casing")
853 (expand-file-name (substitute-in-file-name (match-string 2)))))
855 ((string= (match-string 1) "check_cmd")
856 (add-to-list 'check_cmd
(match-string 2)))
858 ((string= (match-string 1) "comp_cmd")
859 (add-to-list 'comp_cmd
(match-string 2)))
861 ((string= (match-string 1) "debug_post_cmd")
862 (add-to-list 'debug_post_cmd
(match-string 2)))
864 ((string= (match-string 1) "debug_pre_cmd")
865 (add-to-list 'debug_pre_cmd
(match-string 2)))
867 ((string= (match-string 1) "gpr_file")
868 ;; expand now; path is relative to Emacs project file
869 (setq gpr_file
(expand-file-name (match-string 2))))
871 ((string= (match-string 1) "make_cmd")
872 (add-to-list 'make_cmd
(match-string 2)))
874 ((string= (match-string 1) "obj_dir")
875 (add-to-list 'obj_dir
876 (file-name-as-directory
877 (expand-file-name (match-string 2)))))
879 ((string= (match-string 1) "run_cmd")
880 (add-to-list 'run_cmd
(match-string 2)))
882 ((string= (match-string 1) "src_dir")
883 (add-to-list 'src_dir
884 (file-name-as-directory
885 (expand-file-name (match-string 2)))))
888 ;; any other field in the file is just copied
889 (set 'project
(plist-put project
890 (intern (match-string 1))
891 (match-string 2))))))
897 ;; back to the user buffer
898 (set-buffer ada-buffer
)
900 ;; process accumulated lists
902 (let ((sep (plist-get project
'ada_project_path_sep
)))
903 (setq ada_project_path
(reverse ada_project_path
))
904 (setq ada_project_path
(mapconcat 'identity ada_project_path sep
))
905 (set 'project
(plist-put project
'ada_project_path ada_project_path
))
906 ;; env var needed now for ada-gnat-parse-gpr
907 (setenv "ADA_PROJECT_PATH" ada_project_path
)))
909 (if debug_post_cmd
(set 'project
(plist-put project
'debug_post_cmd
(reverse debug_post_cmd
))))
910 (if debug_pre_cmd
(set 'project
(plist-put project
'debug_pre_cmd
(reverse debug_pre_cmd
))))
911 (if casing
(set 'project
(plist-put project
'casing
(reverse casing
))))
912 (if check_cmd
(set 'project
(plist-put project
'check_cmd
(reverse check_cmd
))))
913 (if comp_cmd
(set 'project
(plist-put project
'comp_cmd
(reverse comp_cmd
))))
914 (if make_cmd
(set 'project
(plist-put project
'make_cmd
(reverse make_cmd
))))
915 (if run_cmd
(set 'project
(plist-put project
'run_cmd
(reverse run_cmd
))))
919 (set 'project
(ada-gnat-parse-gpr project gpr_file
))
920 ;; append Ada source and object directories to others from Emacs project file
921 (setq src_dir
(append (plist-get project
'src_dir
) src_dir
))
922 (setq obj_dir
(append (plist-get project
'obj_dir
) obj_dir
))
923 (setq ada-xref-runtime-library-specs-path
'()
924 ada-xref-runtime-library-ali-path
'()))
927 ;; FIXME: gnatpath.exe doesn't output the runtime libraries, so always call ada-initialize-runtime-library
928 ;; if using a gpr_file, the runtime library directories are
929 ;; included in src_dir and obj_dir; otherwise they are in the
930 ;; 'runtime-library' variables.
931 ;; FIXME: always append to src_dir, obj_dir
932 (ada-initialize-runtime-library (or (ada-xref-get-project-field 'cross_prefix
) ""))
935 (if obj_dir
(set 'project
(plist-put project
'obj_dir
(reverse obj_dir
))))
936 (if src_dir
(set 'project
(plist-put project
'src_dir
(reverse src_dir
))))
941 (defun ada-select-prj-file (file)
942 "Select FILE as the current project file."
944 (setq ada-prj-default-project-file
(expand-file-name file
))
946 (let ((casing (ada-xref-get-project-field 'casing
)))
949 ;; FIXME: use ada-get-absolute-dir here
950 (setq ada-case-exception-file casing
)
951 (ada-case-read-exceptions))))
953 (let ((ada_project_path (ada-xref-get-project-field 'ada_project_path
)))
955 ;; FIXME: use ada-get-absolute-dir, mapconcat here
956 (setenv "ADA_PROJECT_PATH" ada_project_path
)))
958 (setq compilation-search-path
(ada-xref-get-src-dir-field))
960 (setq ada-search-directories-internal
961 ;; FIXME: why do we need directory-file-name here?
962 (append (mapcar 'directory-file-name compilation-search-path
)
963 ada-search-directories
))
965 ;; return 't', for decent display in message buffer when called interactively
968 (defun ada-find-references (&optional pos arg local-only
)
969 "Find all references to the entity under POS.
970 Calls gnatfind to find the references.
971 If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved.
972 If LOCAL-ONLY is non-nil, only declarations in the current file are returned."
974 (ada-require-project-file)
976 (let* ((identlist (ada-read-identifier pos
))
977 (alifile (ada-get-ali-file-name (ada-file-of identlist
)))
978 (process-environment (ada-set-environment)))
980 (set-buffer (get-file-buffer (ada-file-of identlist
)))
982 ;; if the file is more recent than the executable
983 (if (or (buffer-modified-p (current-buffer))
984 (file-newer-than-file-p (ada-file-of identlist
) alifile
))
985 (ada-find-any-references (ada-name-of identlist
)
986 (ada-file-of identlist
)
987 nil nil local-only arg
)
988 (ada-find-any-references (ada-name-of identlist
)
989 (ada-file-of identlist
)
990 (ada-line-of identlist
)
991 (ada-column-of identlist
) local-only arg
)))
994 (defun ada-find-local-references (&optional pos arg
)
995 "Find all references to the entity under POS.
996 Calls `gnatfind' to find the references.
997 If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved."
999 (ada-find-references pos arg t
))
1001 (defconst ada-gnatfind-buffer-name
"*gnatfind*")
1003 (defun ada-find-any-references
1004 (entity &optional file line column local-only append
)
1005 "Search for references to any entity whose name is ENTITY.
1006 ENTITY was first found the location given by FILE, LINE and COLUMN.
1007 If LOCAL-ONLY is non-nil, then list only the references in FILE,
1008 which is much faster.
1009 If APPEND is non-nil, then append the output of the command to the
1010 existing buffer `*gnatfind*', if there is one."
1011 (interactive "sEntity name: ")
1012 (ada-require-project-file)
1014 ;; Prepare the gnatfind command. Note that we must protect the quotes
1015 ;; around operators, so that they are correctly handled and can be
1016 ;; processed (gnatfind \"+\":...).
1017 (let* ((quote-entity
1018 (if (= (aref entity
0) ?
\")
1019 (if ada-on-ms-windows
1020 (concat "\\\"" (substring entity
1 -
1) "\\\"")
1021 (concat "'\"" (substring entity
1 -
1) "\"'"))
1023 (switches (ada-xref-get-project-field 'gnatfind_opt
))
1024 ;; FIXME: use gpr_file
1025 (cross-prefix (ada-xref-get-project-field 'cross_prefix
))
1026 (command (concat cross-prefix
"gnat find " switches
" "
1028 (if file
(concat ":" (file-name-nondirectory file
)))
1029 (if line
(concat ":" line
))
1030 (if column
(concat ":" column
))
1031 (if local-only
(concat " " (file-name-nondirectory file
)))
1035 ;; If a project file is defined, use it
1036 (if (and ada-prj-default-project-file
1037 (not (string= ada-prj-default-project-file
"")))
1038 (if (string-equal (file-name-extension ada-prj-default-project-file
)
1040 (setq command
(concat command
" -P\"" ada-prj-default-project-file
"\""))
1041 (setq command
(concat command
" -p\"" ada-prj-default-project-file
"\""))))
1043 (if (and append
(get-buffer ada-gnatfind-buffer-name
))
1044 (with-current-buffer "*gnatfind*"
1045 (setq old-contents
(buffer-string))))
1047 (let ((compilation-error "reference"))
1048 (compilation-start command
'compilation-mode
(lambda (mode) ada-gnatfind-buffer-name
)))
1050 ;; Hide the "Compilation" menu
1051 (with-current-buffer ada-gnatfind-buffer-name
1052 (local-unset-key [menu-bar compilation-menu
])
1057 (set 'buffer-read-only nil
)
1058 (insert old-contents
)
1059 (set 'buffer-read-only t
)
1060 (goto-char (point-max)))))
1064 (defalias 'ada-change-prj
(symbol-function 'ada-set-default-project-file
))
1066 ;; ----- Identifier Completion --------------------------------------------
1067 (defun ada-complete-identifier (pos)
1068 "Try to complete the identifier around POS, using compiler cross-reference information."
1070 (ada-require-project-file)
1072 ;; Initialize function-local variables and jump to the .ali buffer
1073 ;; Note that for regexp search is case insensitive too
1074 (let* ((curbuf (current-buffer))
1075 (identlist (ada-read-identifier pos
))
1076 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
1077 (regexp-quote (ada-name-of identlist
))
1078 "[a-zA-Z0-9_]*\\)"))
1082 ;; Open the .ali file
1083 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
1084 (goto-char (point-max))
1086 ;; build an alist of possible completions
1087 (while (re-search-backward sofar nil t
)
1088 (setq symalist
(cons (cons (match-string 1) nil
) symalist
)))
1090 (setq completed
(try-completion "" symalist
))
1092 ;; kills .ali buffer
1095 ;; deletes the incomplete identifier in the buffer
1097 (looking-at "[a-zA-Z0-9_]+")
1099 ;; inserts the completed symbol
1103 ;; ----- Cross-referencing ----------------------------------------
1105 (defun ada-point-and-xref ()
1106 "Jump to the declaration of the entity below the cursor."
1108 (mouse-set-point last-input-event
)
1109 (ada-goto-declaration (point)))
1111 (defun ada-point-and-xref-body ()
1112 "Jump to the body of the entity under the cursor."
1114 (mouse-set-point last-input-event
)
1115 (ada-goto-body (point)))
1117 (defun ada-goto-body (pos &optional other-frame
)
1118 "Display the body of the entity around POS.
1119 OTHER-FRAME non-nil means display in another frame.
1120 If the entity doesn't have a body, display its declaration.
1121 As a side effect, the buffer for the declaration is also open."
1123 (ada-goto-declaration pos other-frame
)
1125 ;; Temporarily force the display in the same buffer, since we
1126 ;; already changed previously
1127 (let ((ada-xref-other-buffer nil
))
1128 (ada-goto-declaration (point) nil
)))
1130 (defun ada-goto-declaration (pos &optional other-frame
)
1131 "Display the declaration of the identifier around POS.
1132 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1134 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1136 (ada-require-project-file)
1138 (ada-xref-push-pos (buffer-file-name) pos
)
1140 ;; First try the standard algorithm by looking into the .ali file, but if
1141 ;; that file was too old or even did not exist, try to look in the whole
1142 ;; object path for a possible location.
1143 (let ((identlist (ada-read-identifier pos
)))
1145 (ada-find-in-ali identlist other-frame
)
1146 ;; File not found: print explicit error message
1147 (error-file-not-found
1148 (message (concat (error-message-string err
)
1152 (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist
))))
1154 ;; If the ALI file was up-to-date, then we probably have a predefined
1155 ;; entity, whose references are not given by GNAT
1156 (if (and (file-exists-p ali-file
)
1157 (file-newer-than-file-p ali-file
(ada-file-of identlist
)))
1158 (message "No cross-reference found -- may be a predefined entity.")
1160 ;; Else, look in every ALI file, except if the user doesn't want that
1161 (if ada-xref-search-with-egrep
1162 (ada-find-in-src-path identlist other-frame
)
1163 (message "Cross-referencing information is not up-to-date; please recompile.")
1166 (defun ada-goto-declaration-other-frame (pos)
1167 "Display the declaration of the identifier around POS.
1168 The declation is shown in another frame if `ada-xref-other-buffer' is non-nil."
1170 (ada-goto-declaration pos t
))
1172 (defun ada-remote (command)
1173 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1174 (let ((machine (ada-xref-get-project-field 'remote_machine
)))
1175 (if (or (not machine
) (string= machine
""))
1177 (format "%s %s '(%s)'"
1178 remote-shell-program
1182 (defun ada-get-absolute-dir-list (dir-list root-dir
)
1183 "Return the list of absolute directories found in DIR-LIST.
1184 If a directory is a relative directory, ROOT-DIR is prepended.
1185 Project and environment variables are substituted."
1186 (mapcar (lambda (x) (expand-file-name x
(ada-treat-cmd-string root-dir
))) dir-list
))
1188 (defun ada-set-environment ()
1189 "Prepare an environment for Ada compilation.
1190 This returns a new value to use for `process-environment',
1191 but does not actually put it into use.
1192 It modifies the source path and object path with the values found in the
1194 (let ((include (getenv "ADA_INCLUDE_PATH"))
1195 (objects (getenv "ADA_OBJECTS_PATH"))
1196 (build-dir (ada-xref-get-project-field 'build_dir
)))
1198 (set 'include
(concat path-separator include
)))
1200 (set 'objects
(concat path-separator objects
)))
1202 (concat "ADA_INCLUDE_PATH="
1203 (mapconcat (lambda(x) (expand-file-name x build-dir
))
1204 (ada-xref-get-project-field 'src_dir
)
1208 (concat "ADA_OBJECTS_PATH="
1209 (mapconcat (lambda(x) (expand-file-name x build-dir
))
1210 (ada-xref-get-project-field 'obj_dir
)
1213 process-environment
))))
1215 (defun ada-compile-application (&optional arg
)
1216 "Compile the application, using the command found in the project file.
1217 If ARG is not nil, ask for user confirmation."
1219 (ada-require-project-file)
1220 (let ((cmd (ada-xref-get-project-field 'make_cmd
))
1221 (process-environment (ada-set-environment))
1222 (compilation-scroll-output t
))
1224 (setq compilation-search-path
(ada-xref-get-src-dir-field))
1226 ;; If no project file was found, ask the user
1228 (setq cmd
'("") arg t
))
1230 ;; Make a single command from the list of commands, including the
1231 ;; commands to run it on a remote machine.
1232 (setq cmd
(ada-remote (mapconcat 'identity cmd ada-command-separator
)))
1234 (if (or ada-xref-confirm-compile arg
)
1235 (setq cmd
(read-from-minibuffer "enter command to compile: " cmd
)))
1237 ;; Insert newlines so as to separate the name of the commands to run
1238 ;; and the output of the commands. This doesn't work with cmdproxy.exe,
1239 ;; which gets confused by newline characters.
1240 (if (not (string-match ".exe" shell-file-name
))
1241 (setq cmd
(concat cmd
"\n\n")))
1243 (compile (ada-quote-cmd cmd
))))
1245 (defun ada-set-main-compile-application ()
1246 "Set main project variable to current buffer, build main."
1248 (ada-require-project-file)
1249 (let* ((file (buffer-file-name (current-buffer)))
1252 (error "No file for current buffer")
1256 (file-name-nondirectory
1257 (file-name-sans-extension file
))
1259 (ada-xref-set-project-field 'main main
)
1260 (ada-compile-application))))
1262 (defun ada-compile-current (&optional arg prj-field
)
1263 "Recompile the current file.
1264 If ARG is non-nil, ask for user confirmation of the command.
1265 PRJ-FIELD is the name of the field to use in the project file to get the
1266 command, and should be either `comp_cmd' (default) or `check_cmd'."
1268 (ada-require-project-file)
1269 (let* ((field (if prj-field prj-field
'comp_cmd
))
1270 (cmd (ada-xref-get-project-field field
))
1271 (process-environment (ada-set-environment))
1272 (compilation-scroll-output t
))
1275 (setq cmd
'("") arg t
))
1277 ;; Make a single command from the list of commands, including the
1278 ;; commands to run it on a remote machine.
1279 (setq cmd
(ada-remote (mapconcat 'identity cmd ada-command-separator
)))
1281 ;; If no project file was found, ask the user
1282 (if (or ada-xref-confirm-compile arg
)
1283 (setq cmd
(read-from-minibuffer "enter command to compile: " cmd
)))
1285 (compile (ada-quote-cmd cmd
))))
1287 (defun ada-check-current (&optional arg
)
1288 "Check the current file for syntax errors.
1289 If ARG is non-nil, ask for user confirmation of the command."
1291 (ada-compile-current arg
'check_cmd
))
1293 (defun ada-run-application (&optional arg
)
1294 "Run the application.
1295 If ARG is non-nil, ask for user confirmation."
1297 (ada-require-project-file)
1299 (let ((machine (ada-xref-get-project-field 'cross_prefix
)))
1300 (if (and machine
(not (string= machine
"")))
1301 (error "This feature is not supported yet for cross environments")))
1303 (let ((command (ada-xref-get-project-field 'run_cmd
)))
1305 ;; Guess the command if it wasn't specified
1307 (set 'command
(list (file-name-sans-extension (buffer-name)))))
1309 ;; Modify the command to run remotely
1310 (setq command
(ada-remote (mapconcat 'identity command
1311 ada-command-separator
)))
1313 ;; Ask for the arguments to the command if required
1314 (if (or ada-xref-confirm-compile arg
)
1315 (setq command
(read-from-minibuffer "Enter command to execute: "
1319 (with-current-buffer (get-buffer-create "*run*")
1320 (set 'buffer-read-only nil
)
1323 (start-process "run" (current-buffer) shell-file-name
1326 ;; Set these two variables to their default values, since otherwise
1327 ;; the output buffer is scrolled so that only the last output line
1328 ;; is visible at the top of the buffer.
1329 (set (make-local-variable 'scroll-step
) 0)
1330 (set (make-local-variable 'scroll-conservatively
) 0)
1332 (display-buffer "*run*")
1334 ;; change to buffer *run* for interactive programs
1336 (switch-to-buffer "*run*")
1339 (defun ada-gdb-application (&optional arg executable-name
)
1340 "Start the debugger on the application.
1341 If ARG is non-nil, ask the user to confirm the command.
1342 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1345 (ada-require-project-file)
1346 (let ((buffer (current-buffer))
1347 cmd pre-cmd post-cmd
)
1348 (setq cmd
(if executable-name
1349 (concat ada-prj-default-debugger
" " executable-name
)
1350 (ada-xref-get-project-field 'debug_cmd
))
1351 pre-cmd
(ada-xref-get-project-field 'debug_pre_cmd
)
1352 post-cmd
(ada-xref-get-project-field 'debug_post_cmd
))
1354 ;; If the command was not given in the project file, start a bare gdb
1356 (set 'cmd
(concat ada-prj-default-debugger
1359 (file-name-sans-extension (buffer-file-name))))))
1361 ;; For gvd, add an extra switch so that the Emacs window is completly
1362 ;; swallowed inside the Gvd one
1363 (if (and ada-tight-gvd-integration
1364 (string-match "^[^ \t]*gvd" cmd
))
1365 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1366 ;; We make sure that gvd swallows the new frame, not the one the
1367 ;; user has been using until now
1368 ;; The frame is made invisible initially, so that GtkPlug gets a
1369 ;; chance to fully manage it. Then it works fine with Enlightenment
1371 (let ((frame (make-frame '((visibility . nil
)))))
1373 cmd
" --editor-window="
1374 (cdr (assoc 'outer-window-id
(frame-parameters frame
)))))
1375 (select-frame frame
)))
1377 ;; Add a -fullname switch
1378 ;; Use the remote machine
1379 (set 'cmd
(ada-remote (concat cmd
" -fullname ")))
1381 ;; Ask for confirmation if required
1382 (if (or arg ada-xref-confirm-compile
)
1383 (set 'cmd
(read-from-minibuffer "enter command to debug: " cmd
)))
1385 (let ((old-comint-exec (symbol-function 'comint-exec
)))
1387 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1388 ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef
1389 (fset 'gud-gdb-massage-args
(lambda (file args
) args
))
1391 (set 'pre-cmd
(mapconcat 'identity pre-cmd ada-command-separator
))
1392 (if (not (equal pre-cmd
""))
1393 (setq pre-cmd
(concat pre-cmd ada-command-separator
)))
1395 (set 'post-cmd
(mapconcat 'identity post-cmd
"\n"))
1397 (set 'post-cmd
(concat post-cmd
"\n")))
1400 ;; Temporarily replaces the definition of `comint-exec' so that we
1401 ;; can execute commands before running gdb.
1402 ;; FIXME: This is evil and not temporary !!! -stef
1404 `(lambda (buffer name command startfile switches
)
1405 (let (compilation-buffer-name-function)
1407 (set 'compilation-buffer-name-function
1408 (lambda(x) (buffer-name buffer
)))
1409 (compile (ada-quote-cmd
1412 (mapconcat 'identity switches
" "))))))
1415 ;; Tight integration should force the tty mode
1416 (if (and (string-match "gvd" (comint-arguments cmd
0 0))
1417 ada-tight-gvd-integration
1418 (not (string-match "--tty" cmd
)))
1419 (setq cmd
(concat cmd
"--tty")))
1421 (if (and (string-match "jdb" (comint-arguments cmd
0 0))
1423 (funcall (symbol-function 'jdb
) cmd
)
1426 ;; Restore the standard fset command (or for instance C-U M-x shell
1427 ;; wouldn't work anymore
1429 (fset 'comint-exec old-comint-exec
)
1431 ;; Send post-commands to the debugger
1432 (process-send-string (get-buffer-process (current-buffer)) post-cmd
)
1434 ;; Move to the end of the debugger buffer, so that it is automatically
1435 ;; scrolled from then on.
1436 (goto-char (point-max))
1438 ;; Display both the source window and the debugger window (the former
1439 ;; above the latter). No need to show the debugger window unless it
1440 ;; is going to have some relevant information.
1441 (if (or (not (string-match "gvd" (comint-arguments cmd
0 0)))
1442 (string-match "--tty" cmd
))
1443 (split-window-vertically))
1444 (switch-to-buffer buffer
)
1447 (defun ada-reread-prj-file (&optional filename
)
1448 "Reread either the current project, or FILENAME if non-nil.
1449 If FILENAME is non-nil, set it as current project."
1452 (setq filename ada-prj-default-project-file
))
1453 (ada-parse-prj-file filename
)
1454 (ada-select-prj-file filename
))
1456 ;; ------ Private routines
1458 (defun ada-xref-current (file &optional ali-file-name
)
1459 "Update the cross-references for FILE.
1460 This in fact recompiles FILE to create ALI-FILE-NAME.
1461 This function returns the name of the file that was recompiled to generate
1462 the cross-reference information. Note that the ali file can then be deduced
1463 by replacing the file extension with `.ali'."
1465 (if (and ali-file-name
1466 (get-file-buffer ali-file-name
))
1467 (kill-buffer (get-file-buffer ali-file-name
)))
1469 (let* ((name (ada-convert-file-name file
))
1470 (body-name (or (ada-get-body-name name
) name
)))
1472 ;; Always recompile the body when we can. We thus temporarily switch to a
1473 ;; buffer than contains the body of the unit
1475 (let ((body-visible (find-buffer-visiting body-name
))
1478 (set-buffer body-visible
)
1479 (find-file body-name
))
1481 ;; Execute the compilation. Note that we must wait for the end of the
1482 ;; process, or the ALI file would still not be available.
1483 ;; Unfortunately, the underlying `compile' command that we use is
1485 (ada-compile-current)
1486 (setq process
(get-buffer-process "*compilation*"))
1489 (not (equal (process-status process
) 'exit
)))
1492 ;; remove the buffer for the body if it wasn't there before
1493 (unless body-visible
1494 (kill-buffer (find-buffer-visiting body-name
)))
1498 (defun ada-find-file-in-dir (file dir-list
)
1499 "Search for FILE in DIR-LIST."
1501 (while (and (not found
) dir-list
)
1502 (set 'found
(concat (file-name-as-directory (car dir-list
))
1503 (file-name-nondirectory file
)))
1505 (unless (file-exists-p found
)
1507 (set 'dir-list
(cdr dir-list
)))
1510 (defun ada-find-ali-file-in-dir (file)
1511 "Find the ali file FILE, searching obj_dir for the current project.
1512 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1513 and the standard runtime location at the end."
1514 (ada-find-file-in-dir file
(ada-xref-get-obj-dir-field)))
1516 (defun ada-find-src-file-in-dir (file)
1517 "Find the source file FILE, searching src_dir for the current project.
1518 Adds the standard runtime location at the end of the search path to conform
1519 to gnatmake's behavior."
1520 (ada-find-file-in-dir file
(ada-xref-get-src-dir-field)))
1522 (defun ada-get-ali-file-name (file)
1523 "Create the ali file name for the Ada file FILE.
1524 The file is searched for in every directory shown in the obj_dir lines of
1527 ;; This function has to handle the special case of non-standard
1528 ;; file names (i.e. not .adb or .ads)
1529 ;; The trick is the following:
1530 ;; 1- replace the extension of the current file with .ali,
1531 ;; and look for this file
1532 ;; 2- If this file is found:
1533 ;; grep the "^U" lines, and make sure we are not reading the
1534 ;; .ali file for a spec file. If we are, go to step 3.
1535 ;; 3- If the file is not found or step 2 failed:
1536 ;; find the name of the "other file", ie the body, and look
1537 ;; for its associated .ali file by subtituing the extension
1539 ;; We must also handle the case of separate packages and subprograms:
1540 ;; 4- If no ali file was found, we try to modify the file name by removing
1541 ;; everything after the last '-' or '.' character, so as to get the
1542 ;; ali file for the parent unit. If we found an ali file, we check that
1543 ;; it indeed contains the definition for the separate entity by checking
1544 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1547 (with-current-buffer (get-file-buffer file
)
1548 (let ((short-ali-file-name
1549 (concat (file-name-sans-extension (file-name-nondirectory file
))
1554 ;; If we have a non-standard file name, and this is a spec, we first
1555 ;; look for the .ali file of the body, since this is the one that
1556 ;; contains the most complete information. If not found, we will do what
1557 ;; we can with the .ali file for the spec...
1559 (if (not (string= (file-name-extension file
) "ads"))
1560 (let ((specs ada-spec-suffixes
))
1562 (if (string-match (concat (regexp-quote (car specs
)) "$")
1565 (set 'specs
(cdr specs
)))))
1569 (ada-find-ali-file-in-dir
1570 (concat (file-name-sans-extension
1571 (file-name-nondirectory
1572 (ada-other-file-name)))
1579 ;; Else we take the .ali file associated with the unit
1580 (ada-find-ali-file-in-dir short-ali-file-name
)
1583 ;; else we did not find the .ali file Second chance: in case
1584 ;; the files do not have standard names (such as for instance
1585 ;; file_s.ada and file_b.ada), try to go to the other file
1586 ;; and look for its ali file
1587 (ada-find-ali-file-in-dir
1588 (concat (file-name-sans-extension
1589 (file-name-nondirectory (ada-other-file-name)))
1593 ;; If we still don't have an ali file, try to get the one
1594 ;; from the parent unit, in case we have a separate entity.
1595 (let ((parent-name (file-name-sans-extension
1596 (file-name-nondirectory file
))))
1598 (while (and (not ali-file-name
)
1599 (string-match "^\\(.*\\)[.-][^.-]*" parent-name
))
1601 (set 'parent-name
(match-string 1 parent-name
))
1602 (set 'ali-file-name
(ada-find-ali-file-in-dir
1603 (concat parent-name
".ali")))
1607 ;; If still not found, try to recompile the file
1608 (if (not ali-file-name
)
1609 ;; Recompile only if the user asked for this, and search the ali
1610 ;; filename again. We avoid a possible infinite recursion by
1611 ;; temporarily disabling the automatic compilation.
1613 (if ada-xref-create-ali
1615 (concat (file-name-sans-extension (ada-xref-current file
))
1618 (error "`.ali' file not found; recompile your source file"))
1621 ;; same if the .ali file is too old and we must recompile it
1622 (if (and (file-newer-than-file-p file ali-file-name
)
1623 ada-xref-create-ali
)
1624 (ada-xref-current file ali-file-name
)))
1626 ;; Always return the correct absolute file name
1627 (expand-file-name ali-file-name
))
1630 (defun ada-get-ada-file-name (file original-file
)
1631 "Create the complete file name (+directory) for FILE.
1632 The original file (where the user was) is ORIGINAL-FILE.
1633 Search in project file for possible paths."
1637 ;; If the buffer for original-file, use it to get the values from the
1638 ;; project file, otherwise load the file and its project file
1639 (let ((buffer (get-file-buffer original-file
)))
1642 (find-file original-file
)))
1644 ;; we choose the first possible completion and we
1645 ;; return the absolute file name
1646 (let ((filename (ada-find-src-file-in-dir file
)))
1648 (expand-file-name filename
)
1649 (signal 'error-file-not-found
(file-name-nondirectory file
)))
1652 (defun ada-find-file-number-in-ali (file)
1653 "Return the file number for FILE in the associated ali file."
1654 (set-buffer (ada-get-ali-buffer file
))
1655 (goto-char (point-min))
1657 (let ((begin (re-search-forward "^D")))
1659 (re-search-forward (concat "^D " (file-name-nondirectory file
)))
1660 (count-lines begin
(point))))
1662 (defun ada-read-identifier (pos)
1663 "Return the identlist around POS and switch to the .ali buffer.
1664 The returned list represents the entity, and can be manipulated through the
1665 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1667 ;; If at end of buffer (e.g the buffer is empty), error
1668 (if (>= (point) (point-max))
1669 (error "No identifier on point"))
1671 ;; goto first character of the identifier/operator (skip backward < and >
1672 ;; since they are part of multiple character operators
1674 (skip-chars-backward "a-zA-Z0-9_<>")
1676 ;; check if it really is an identifier
1677 (if (ada-in-comment-p)
1678 (error "Inside comment"))
1680 (let (identifier identlist
)
1681 ;; Just in front of a string => we could have an operator declaration,
1682 ;; as in "+", "-", ..
1683 (if (= (char-after) ?
\")
1686 ;; if looking at an operator
1687 ;; This is only true if:
1688 ;; - the symbol is +, -, ...
1689 ;; - the symbol is made of letters, and not followed by _ or a letter
1690 (if (and (looking-at ada-operator-re
)
1691 (or (not (= (char-syntax (char-after)) ?w
))
1692 (not (or (= (char-syntax (char-after (match-end 0))) ?w
)
1693 (= (char-after (match-end 0)) ?_
)))))
1695 (if (and (= (char-before) ?
\")
1696 (= (char-after (+ (length (match-string 0)) (point))) ?
\"))
1698 (set 'identifier
(regexp-quote (concat "\"" (match-string 0) "\""))))
1700 (if (ada-in-string-p)
1701 (error "Inside string or character constant"))
1702 (if (looking-at (concat ada-keywords
"[^a-zA-Z_]"))
1703 (error "No cross-reference available for reserved keyword"))
1704 (if (looking-at "[a-zA-Z0-9_]+")
1705 (set 'identifier
(match-string 0))
1706 (error "No identifier around")))
1708 ;; Build the identlist
1709 (set 'identlist
(ada-make-identlist))
1710 (ada-set-name identlist
(downcase identifier
))
1711 (ada-set-line identlist
1712 (number-to-string (count-lines 1 (point))))
1713 (ada-set-column identlist
1714 (number-to-string (1+ (current-column))))
1715 (ada-set-file identlist
(buffer-file-name))
1719 (defun ada-get-all-references (identlist)
1720 "Complete IDENTLIST with definition file and places where it is referenced.
1721 Information is extracted from the ali file."
1723 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist
)))
1725 (set-buffer ali-buffer
)
1726 (goto-char (point-min))
1727 (ada-set-on-declaration identlist nil
)
1729 ;; First attempt: we might already be on the declaration of the identifier
1730 ;; We want to look for the declaration only in a definite interval (after
1731 ;; the "^X ..." line for the current file, and before the next "^X" line
1733 (if (re-search-forward
1734 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist
)))
1736 (let ((bound (save-excursion (re-search-forward "^X " nil t
))))
1737 (set 'declaration-found
1739 (concat "^" (ada-line-of identlist
)
1740 "." (ada-column-of identlist
)
1741 "[ *]" (ada-name-of identlist
)
1742 "[{\[\(<= ]?\\(.*\\)$") bound t
))
1743 (if declaration-found
1744 (ada-set-on-declaration identlist t
))
1747 ;; If declaration is still nil, then we were not on a declaration, and
1748 ;; have to fall back on other algorithms
1750 (unless declaration-found
1752 ;; Since we alread know the number of the file, search for a direct
1754 (goto-char (point-min))
1755 (set 'declaration-found t
)
1758 (number-to-string (ada-find-file-number-in-ali
1759 (ada-file-of identlist
))))
1760 (unless (re-search-forward (concat (ada-ali-index-of identlist
)
1761 "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
1762 (ada-line-of identlist
)
1764 (ada-column-of identlist
) "\\>")
1767 ;; if we did not find it, it may be because the first reference
1768 ;; is not required to have a 'unit_number|' item included.
1769 ;; Or maybe we are already on the declaration...
1770 (unless (re-search-forward
1772 "^[0-9]+.[0-9]+[ *]"
1773 (ada-name-of identlist
)
1774 "[ <{=\(\[]\\(.\\|\n\\.\\)*\\<"
1775 (ada-line-of identlist
)
1777 (ada-column-of identlist
) "\\>")
1780 ;; If still not found, then either the declaration is unknown
1781 ;; or the source file has been modified since the ali file was
1783 (set 'declaration-found nil
)
1787 ;; Last check to be completly sure we have found the correct line (the
1788 ;; ali might not be up to date for instance)
1789 (if declaration-found
1792 ;; while we have a continuation line, go up one line
1793 (while (looking-at "^\\.")
1795 (beginning-of-line))
1796 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1797 (ada-name-of identlist
) "[ <{=\(\[]"))
1798 (set 'declaration-found nil
))))
1800 ;; Still no success ! The ali file must be too old, and we need to
1801 ;; use a basic algorithm based on guesses. Note that this only happens
1802 ;; if the user does not want us to automatically recompile files
1804 (unless declaration-found
1805 (if (ada-xref-find-in-modified-ali identlist
)
1806 (set 'declaration-found t
)
1807 ;; No more idea to find the declaration. Give up
1809 (kill-buffer ali-buffer
)
1811 (error "No declaration of %s found" (ada-name-of identlist
))
1816 ;; Now that we have found a suitable line in the .ali file, get the
1817 ;; information available
1819 (if declaration-found
1820 (let ((current-line (buffer-substring
1821 (point) (point-at-eol))))
1825 (while (looking-at "^\\.\\(.*\\)")
1826 (set 'current-line
(concat current-line
(match-string 1)))
1830 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t
)
1832 ;; If we can find the file
1834 (ada-set-declare-file
1836 (ada-get-ada-file-name (match-string 1)
1837 (ada-file-of identlist
)))
1839 ;; Else clean up the ali file
1840 (error-file-not-found
1841 (signal (car err
) (cdr err
)))
1843 (kill-buffer ali-buffer
)
1844 (error (error-message-string err
)))
1847 (ada-set-references identlist current-line
)
1851 (defun ada-xref-find-in-modified-ali (identlist)
1852 "Find the matching position for IDENTLIST in the current ali buffer.
1853 This function is only called when the file was not up-to-date, so we need
1854 to make some guesses.
1855 This function is disabled for operators, and only works for identifiers."
1857 (unless (= (string-to-char (ada-name-of identlist
)) ?
\")
1859 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1860 (my-regexp (concat "[ *]"
1861 (regexp-quote (ada-name-of identlist
)) " "))
1867 (ali-buffer (current-buffer)))
1869 (goto-char (point-max))
1870 (while (re-search-backward my-regexp nil t
)
1872 (set 'line-ali
(count-lines 1 (point)))
1874 ;; have a look at the line and column numbers
1875 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1877 (setq line-ada
(match-string 1))
1878 (setq col-ada
(match-string 2)))
1879 (setq line-ada
"--")
1882 ;; construct a list with the file names and the positions within
1883 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t
)
1885 'declist
(list line-ali
(match-string 1) line-ada col-ada
))
1890 ;; how many possible declarations have we found ?
1891 (setq len
(length declist
))
1895 (kill-buffer (current-buffer))
1896 (error "No declaration of %s recorded in .ali file"
1897 (ada-name-of identlist
)))
1898 ;; one => should be the right one
1900 (goto-char (point-min))
1901 (forward-line (1- (caar declist
))))
1903 ;; more than one => display choice list
1905 (save-window-excursion
1906 (with-output-to-temp-buffer "*choice list*"
1908 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1909 (princ "Possible declarations are:\n\n")
1910 (princ " no. in file at line col\n")
1911 (princ " --- --------------------- ---- ----\n")
1913 (while (< counter len
)
1914 (princ (format " %2d) %-21s %4s %4s\n"
1916 (ada-get-ada-file-name
1917 (nth 1 (nth counter declist
))
1918 (ada-file-of identlist
))
1919 (nth 2 (nth counter declist
))
1920 (nth 3 (nth counter declist
))
1922 (setq counter
(1+ counter
))
1925 ) ; end of with-output-to ...
1929 (not (integerp choice
))
1934 (read-from-minibuffer "Enter No. of your choice: "))))
1936 (set-buffer ali-buffer
)
1937 (goto-char (point-min))
1938 (forward-line (1- (car (nth (1- choice
) declist
))))
1942 (defun ada-find-in-ali (identlist &optional other-frame
)
1943 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1944 If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil,
1945 opens a new window to show the declaration."
1947 (ada-get-all-references identlist
)
1948 (let ((ali-line (ada-references-of identlist
))
1953 ;; Note: in some cases, an entity can have multiple references to the
1954 ;; bodies (this is for instance the case for a separate subprogram, that
1955 ;; has a reference both to the stub and to the real body).
1956 ;; In that case, we simply go to each one in turn.
1958 ;; Get all the possible locations
1959 (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line
)
1960 (set 'locations
(list (list (match-string 1 ali-line
) ;; line
1961 (match-string 2 ali-line
) ;; column
1962 (ada-declare-file-of identlist
))))
1963 (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)"
1965 (setq line
(match-string 1 ali-line
)
1966 col
(match-string 3 ali-line
)
1967 start
(match-end 3))
1969 ;; it there was a file number in the same line
1970 ;; Make sure we correctly handle the case where the first file reference
1971 ;; on the line is the type reference.
1972 ;; 1U2 T(2|2r3) 34r23
1973 (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?"
1974 (match-string 0 ali-line
))
1976 (let ((file-number (match-string 1 ali-line
)))
1977 (goto-char (point-min))
1978 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1979 (string-to-number file-number
))
1980 (set 'file
(match-string 1))
1982 ;; Else get the nearest file
1983 (set 'file
(ada-declare-file-of identlist
)))
1985 (set 'locations
(append locations
(list (list line col file
)))))
1987 ;; Add the specs at the end again, so that from the last body we go to
1989 (set 'locations
(append locations
(list (car locations
))))
1991 ;; Find the new location we want to go to.
1992 ;; If we are on none of the locations listed, we simply go to the specs.
1994 (setq line
(caar locations
)
1995 col
(nth 1 (car locations
))
1996 file
(nth 2 (car locations
)))
1999 (if (and (string= (caar locations
) (ada-line-of identlist
))
2000 (string= (nth 1 (car locations
)) (ada-column-of identlist
))
2001 (string= (file-name-nondirectory (nth 2 (car locations
)))
2002 (file-name-nondirectory (ada-file-of identlist
))))
2003 (setq locations
(cadr locations
)
2004 line
(car locations
)
2005 col
(nth 1 locations
)
2006 file
(nth 2 locations
)
2008 (set 'locations
(cdr locations
))))
2010 ;; Find the file in the source path
2011 (set 'file
(ada-get-ada-file-name file
(ada-file-of identlist
)))
2013 ;; Kill the .ali buffer
2014 (kill-buffer (current-buffer))
2016 ;; Now go to the buffer
2017 (ada-xref-change-buffer file
2018 (string-to-number line
)
2019 (1- (string-to-number col
))
2024 (defun ada-find-in-src-path (identlist &optional other-frame
)
2025 "More general function for cross-references.
2026 This function should be used when the standard algorithm that parses the
2027 .ali file has failed, either because that file was too old or even did not
2029 This function attempts to find the possible declarations for the identifier
2030 anywhere in the object path.
2031 This command requires the external `egrep' program to be available.
2033 This works well when one is using an external library and wants to find
2034 the declaration and documentation of the subprograms one is using."
2035 ;; FIXME: what does this function do?
2037 (dirs (ada-xref-get-obj-dir-field))
2038 (regexp (concat "[ *]" (ada-name-of identlist
)))
2043 ;; Do the grep in all the directories. We do multiple shell
2044 ;; commands instead of one in case there is no .ali file in one
2045 ;; of the directory and the shell stops because of that.
2047 (with-current-buffer (get-buffer-create "*grep*")
2049 (insert (shell-command-to-string
2052 (shell-quote-argument (concat "^X|" regexp
"( |$)"))
2054 (shell-quote-argument (file-name-as-directory (car dirs
)))
2056 (set 'dirs
(cdr dirs
)))
2058 ;; Now parse the output
2059 (set 'case-fold-search t
)
2060 (goto-char (point-min))
2061 (while (re-search-forward regexp nil t
)
2064 (if (not (= (char-after) ?X
))
2066 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
2067 (setq line
(match-string 1)
2068 column
(match-string 2))
2069 (re-search-backward "^X [0-9]+ \\(.*\\)$")
2070 (set 'file
(list (match-string 1) line column
))
2072 ;; There could be duplicate choices, because of the structure
2073 ;; of the .ali files
2074 (unless (member file list
)
2075 (set 'list
(append list
(list file
))))))))
2077 ;; Current buffer is still "*grep*"
2078 (kill-buffer "*grep*")
2081 ;; Now display the list of possible matches
2084 ;; No choice found => Error
2086 (error "No cross-reference found, please recompile your file"))
2088 ;; Only one choice => Do the cross-reference
2089 ((= (length list
) 1)
2090 (set 'file
(ada-find-src-file-in-dir (caar list
)))
2092 (ada-xref-change-buffer file
2093 (string-to-number (nth 1 (car list
)))
2094 (string-to-number (nth 2 (car list
)))
2097 (error "%s not found in src_dir" (caar list
)))
2098 (message "This is only a (good) guess at the cross-reference.")
2101 ;; Else, ask the user
2103 (save-window-excursion
2104 (with-output-to-temp-buffer "*choice list*"
2106 (princ "Identifier is overloaded and Xref information is not up to date.\n")
2107 (princ "Possible declarations are:\n\n")
2108 (princ " no. in file at line col\n")
2109 (princ " --- --------------------- ---- ----\n")
2111 (while (< counter
(length list
))
2112 (princ (format " %2d) %-21s %4s %4s\n"
2114 (nth 0 (nth counter list
))
2115 (nth 1 (nth counter list
))
2116 (nth 2 (nth counter list
))
2118 (setq counter
(1+ counter
))
2121 (while (or (not choice
)
2122 (not (integerp choice
))
2124 (> choice
(length list
)))
2127 (read-from-minibuffer "Enter No. of your choice: "))))
2129 (set 'choice
(1- choice
))
2130 (kill-buffer "*choice list*")
2132 (set 'file
(ada-find-src-file-in-dir (car (nth choice list
))))
2134 (ada-xref-change-buffer file
2135 (string-to-number (nth 1 (nth choice list
)))
2136 (string-to-number (nth 2 (nth choice list
)))
2139 (signal 'error-file-not-found
(car (nth choice list
))))
2140 (message "This is only a (good) guess at the cross-reference.")
2143 (defun ada-xref-change-buffer
2144 (file line column identlist
&optional other-frame
)
2145 "Select and display FILE, at LINE and COLUMN.
2146 If we do not end on the same identifier as IDENTLIST, find the
2147 closest match. Kills the .ali buffer at the end.
2148 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2150 (let (declaration-buffer)
2152 ;; Select and display the destination buffer
2153 (if ada-xref-other-buffer
2155 (find-file-other-frame file
)
2156 (set 'declaration-buffer
(find-file-noselect file
))
2157 (set-buffer declaration-buffer
)
2158 (switch-to-buffer-other-window declaration-buffer
)
2163 ;; move the cursor to the correct position
2165 (goto-char (point-min))
2166 (forward-line (1- line
))
2167 (move-to-column column
)
2169 ;; If we are not on the identifier, the ali file was not up-to-date.
2170 ;; Try to find the nearest position where the identifier is found,
2171 ;; this is probably the right one.
2172 (unless (looking-at (ada-name-of identlist
))
2173 (ada-xref-search-nearest (ada-name-of identlist
)))
2177 (defun ada-xref-search-nearest (name)
2178 "Search for NAME nearest to the position recorded in the Xref file.
2179 Return the position of the declaration in the buffer, or nil if not found."
2180 (let ((orgpos (point))
2184 (goto-char (point-max))
2186 ;; loop - look for all declarations of name in this file
2187 (while (search-backward name nil t
)
2189 ;; check if it really is a complete Ada identifier
2191 (not (save-excursion
2192 (goto-char (match-end 0))
2194 (not (ada-in-string-or-comment-p))
2196 ;; variable declaration ?
2198 (skip-chars-forward "a-zA-Z_0-9" )
2199 (ada-goto-next-non-ws)
2200 (looking-at ":[^=]"))
2201 ;; procedure, function, task or package declaration ?
2203 (ada-goto-previous-word)
2204 (looking-at "\\<[pP][rR][oO][cC][eE][dD][uU][rR][eE]\\>\\|\\<[fF][uU][nN][cC][tT][iI][oO][nN]\\>\\|\\<[tT][yY][pP][eE]\\>\\|\\<[tT][aA][sS][kK]\\>\\|\\<[pP][aA][cC][kK][aA][gG][eE]\\>\\|\\<[bB][oO][dD][yY]\\>"))))
2206 ;; check if it is nearer than the ones before if any
2208 (< (abs (- (point) orgpos
)) diff
))
2210 (setq newpos
(point)
2211 diff
(abs (- newpos orgpos
))))))
2216 (message "ATTENTION: this declaration is only a (good) guess ...")
2221 ;; Find the parent library file of the current file
2222 (defun ada-goto-parent ()
2223 "Go to the parent library file."
2225 (ada-require-project-file)
2227 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2231 (with-current-buffer buffer
2232 (goto-char (point-min))
2233 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2234 (setq unit-name
(match-string 1))
2235 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name
))
2237 (kill-buffer buffer
)
2238 (error "No parent unit !"))
2239 (setq unit-name
(match-string 1 unit-name
))
2242 ;; look for the file name for the parent unit specification
2243 (goto-char (point-min))
2244 (re-search-forward (concat "^W " unit-name
2245 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2247 (setq body-name
(match-string 1))
2248 (setq ali-name
(match-string 2))
2249 (kill-buffer buffer
)
2252 (setq ali-name
(ada-find-ali-file-in-dir ali-name
))
2255 ;; Tries to open the new ali file to find the spec file
2258 (find-file ali-name
)
2259 (goto-char (point-min))
2260 (re-search-forward (concat "^U " unit-name
"%s[ \t]+"
2262 (setq body-name
(match-string 1))
2263 (kill-buffer (current-buffer))
2268 (find-file body-name
)
2271 (defun ada-make-filename-from-adaname (adaname)
2272 "Determine the filename in which ADANAME is found.
2273 This is a GNAT specific function that uses gnatkrunch."
2274 (let ((krunch-buf (generate-new-buffer "*gkrunch*"))
2275 (cross-prefix (plist-get (cdr (ada-xref-current-project)) 'cross_prefix
)))
2276 (with-current-buffer krunch-buf
2277 ;; send adaname to external process `gnatkr'.
2278 ;; Add a dummy extension, since gnatkr versions have two different
2279 ;; behaviors depending on the version:
2280 ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc
2281 ;; After: "AA.BB.CC" => aa-bb.cc
2282 (call-process (concat cross-prefix
"gnatkr") nil krunch-buf nil
2283 (concat adaname
".adb") ada-krunch-args
)
2284 ;; fetch output of that process
2285 (setq adaname
(buffer-substring
2288 (goto-char (point-min))
2291 ;; Remove the extra extension we added above
2292 (setq adaname
(substring adaname
0 -
4))
2294 (kill-buffer krunch-buf
)))
2298 (defun ada-make-body-gnatstub (&optional interactive
)
2299 "Create an Ada package body in the current buffer.
2300 This function uses the `gnat stub' program to create the body.
2301 This function typically is to be hooked into `ff-file-created-hook'.
2302 If INTERACTIVE is nil, assume this is called from `ff-file-created-hook'."
2304 (ada-require-project-file)
2306 ;; If not interactive, assume we are being called from
2307 ;; ff-file-created-hook. Then the current buffer is for the body
2308 ;; file, but we will create a new one after gnat stub runs
2310 (set-buffer-modified-p nil
)
2311 (kill-buffer (current-buffer)))
2313 (save-some-buffers nil nil
)
2315 ;; Make sure the current buffer is the spec, so gnat stub gets the
2316 ;; right package parameter (this might not be the case if for
2317 ;; instance the user was asked for a project file)
2319 (unless (buffer-file-name (car (buffer-list)))
2320 (set-buffer (cadr (buffer-list))))
2322 ;; Call the external process
2323 (let* ((project-plist (cdr (ada-xref-current-project)))
2324 (gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts
))
2325 (gpr-file (plist-get project-plist
'gpr_file
))
2326 (filename (buffer-file-name (car (buffer-list))))
2327 (output (concat (file-name-sans-extension filename
) ".adb"))
2328 (cross-prefix (plist-get project-plist
'cross_prefix
))
2329 (gnatstub-cmd (concat cross-prefix
"gnat stub"
2330 (if (not (string= gpr-file
""))
2331 (concat " -P\"" gpr-file
"\""))
2332 " " gnatstub-opts
" " filename
))
2333 (buffer (get-buffer-create "*gnat stub*")))
2335 (with-current-buffer buffer
2336 (compilation-minor-mode 1)
2338 (insert gnatstub-cmd
)
2342 (call-process shell-file-name nil buffer nil
"-c" gnatstub-cmd
)
2344 ;; clean up the output
2346 (if (file-exists-p output
)
2349 (kill-buffer buffer
))
2351 ;; file not created; display the error message
2352 (display-buffer buffer
))))
2354 (defun ada-xref-initialize ()
2355 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2356 For instance, it creates the gnat-specific menus, sets some hooks for
2358 (remove-hook 'ff-file-created-hook
'ada-make-body
) ; from global hook
2359 (remove-hook 'ff-file-created-hook
'ada-make-body t
) ; from local hook
2360 (add-hook 'ff-file-created-hook
'ada-make-body-gnatstub nil t
)
2362 ;; Completion for file names in the mini buffer should ignore .ali files
2363 (add-to-list 'completion-ignored-extensions
".ali")
2365 (ada-xref-update-project-menu)
2368 ;; ----- Add to ada-mode-hook ---------------------------------------------
2370 ;; This must be done before initializing the Ada menu.
2371 (add-hook 'ada-mode-hook
'ada-xref-initialize
)
2373 ;; Define a new error type
2374 (put 'error-file-not-found
2376 '(error ada-mode-errors error-file-not-found
))
2377 (put 'error-file-not-found
2379 "File not found in src-dir (check project file): ")
2383 ;;; ada-xref.el ends here