1 ;; ada-xref.el --- for lookup and completion in Ada mode
3 ;; Copyright (C) 1994-2016 Free Software Foundation, Inc.
5 ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
6 ;; Rolf Ebert <ebert@inf.enst.fr>
7 ;; Emmanuel Briot <briot@gnat.com>
8 ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
9 ;; Keywords: languages ada xref
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; This Package provides a set of functions to use the output of the
29 ;;; cross reference capabilities of the GNAT Ada compiler
30 ;;; for lookup and completion in Ada mode.
32 ;;; If a file *.`adp' exists in the ada-file directory, then it is
33 ;;; read for configuration information. It is read only the first
34 ;;; time a cross-reference is asked for, and is not read later.
36 ;;; You need Emacs >= 20.2 to run this package
44 ;; ----- Requirements -----------------------------------------------------
51 ;; ------ User variables
52 (defcustom ada-xref-other-buffer t
53 "If nil, always display the cross-references in the same buffer.
54 Otherwise create either a new buffer or a new frame."
55 :type
'boolean
:group
'ada
)
57 (defcustom ada-xref-create-ali nil
58 "If non-nil, run gcc whenever the cross-references are not up-to-date.
59 If nil, the cross-reference mode never runs gcc."
60 :type
'boolean
:group
'ada
)
62 (defcustom ada-xref-confirm-compile nil
63 "If non-nil, ask for confirmation before compiling or running the application."
64 :type
'boolean
:group
'ada
)
66 (defcustom ada-krunch-args
"0"
67 "Maximum number of characters for filenames created by `gnatkr'.
68 Set to 0, if you don't use crunched filenames. This should be a string."
69 :type
'string
:group
'ada
)
71 (defcustom ada-gnat-cmd
"gnat"
72 "Default GNAT project file parser.
73 Will be run with args \"list -v -Pfile.gpr\".
74 Default is standard GNAT distribution; alternate \"gnatpath\"
75 is faster, available from Ada mode web site."
76 :type
'string
:group
'ada
)
78 (defcustom ada-gnatls-args
'("-v")
79 "Arguments to pass to `gnatls' to find location of the runtime.
80 Typical use is to pass `--RTS=soft-floats' on some systems that support it.
82 You can also add `-I-' if you do not want the current directory to be included.
83 Otherwise, going from specs to bodies and back will first look for files in the
84 current directory. This only has an impact if you are not using project files,
85 but only ADA_INCLUDE_PATH."
86 :type
'(repeat string
) :group
'ada
)
88 (defcustom ada-prj-default-comp-opt
"-gnatq -gnatQ"
89 "Default compilation options."
90 :type
'string
:group
'ada
)
92 (defcustom ada-prj-default-bind-opt
""
93 "Default binder options."
94 :type
'string
:group
'ada
)
96 (defcustom ada-prj-default-link-opt
""
97 "Default linker options."
98 :type
'string
:group
'ada
)
100 (defcustom ada-prj-default-gnatmake-opt
"-g"
101 "Default options for `gnatmake'."
102 :type
'string
:group
'ada
)
104 (defcustom ada-prj-default-gpr-file
""
105 "Default GNAT project file.
106 If non-empty, this file is parsed to set the source and object directories for
107 the Ada mode project."
108 :type
'string
:group
'ada
)
110 (defcustom ada-prj-ada-project-path-sep
111 (cond ((boundp 'path-separator
) path-separator
) ; 20.3+
112 ((memq system-type
'(windows-nt ms-dos
)) ";")
114 "Default separator for ada_project_path project variable."
115 :type
'string
:group
'ada
)
117 (defcustom ada-prj-gnatfind-switches
"-rf"
118 "Default switches to use for `gnatfind'.
119 You should modify this variable, for instance to add `-a', if you are working
120 in an environment where most ALI files are write-protected.
121 The command `gnatfind' is used every time you choose the menu
122 \"Show all references\"."
123 :type
'string
:group
'ada
)
125 (defcustom ada-prj-default-check-cmd
126 (concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}"
127 " -cargs ${comp_opt}")
128 "Default command to be used to compile a single file.
129 Emacs will substitute the current filename for ${full_current}, or add
130 the filename at the end. This is the same syntax as in the project file."
131 :type
'string
:group
'ada
)
133 (defcustom ada-prj-default-comp-cmd
134 (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
136 "Default command to be used to compile a single file.
137 Emacs will substitute the current filename for ${full_current}, or add
138 the filename at the end. This is the same syntax as in the project file."
139 :type
'string
:group
'ada
)
141 (defcustom ada-prj-default-debugger
"${cross_prefix}gdb"
142 "Default name of the debugger."
143 :type
'string
:group
'ada
)
145 (defcustom ada-prj-default-make-cmd
146 (concat "${cross_prefix}gnatmake -o ${main} ${main} ${gnatmake_opt} "
147 "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
148 "Default command to be used to compile the application.
149 This is the same syntax as in the project file."
150 :type
'string
:group
'ada
)
152 (defcustom ada-prj-default-project-file
""
153 "Name of the current project file.
154 Emacs will not try to use the search algorithm to find the project file if
155 this string is not empty. It is set whenever a project file is found."
156 :type
'(file :must-match t
) :group
'ada
)
158 (defcustom ada-gnatstub-opts
"-q -I${src_dir}"
159 "Options to pass to `gnatsub' to generate the body of a package.
160 This has the same syntax as in the project file (with variable substitution)."
161 :type
'string
:group
'ada
)
163 (defcustom ada-always-ask-project nil
164 "If nil, use default values when no project file was found.
165 Otherwise, ask the user for the name of the project file to use."
166 :type
'boolean
:group
'ada
)
168 (defconst ada-on-ms-windows
(memq system-type
'(windows-nt))
169 "True if we are running on Windows.")
171 (defcustom ada-tight-gvd-integration nil
172 "If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
173 If GVD is not the debugger used, nothing happens."
174 :type
'boolean
:group
'ada
)
176 (defcustom ada-xref-search-with-egrep t
177 "If non-nil, use grep -E to find the possible declarations for an entity.
178 This alternate method is used when the exact location was not found in the
179 information provided by GNAT. However, it might be expensive if you have a lot
180 of sources, since it will search in all the files in your project."
181 :type
'boolean
:group
'ada
)
183 (defvar ada-load-project-hook nil
184 "Hook that is run when loading a project file.
185 Each function in this hook takes one argument FILENAME, that is the name of
186 the project file to load.
187 This hook should be used to support new formats for the project files.
189 If the function can load the file with the given filename, it should create a
190 buffer that contains a conversion of the file to the standard format of the
191 project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\"
192 lines.) It should return nil if it doesn't know how to convert that project
196 ;; ------- Nothing to be modified by the user below this
197 (defvar ada-last-prj-file
""
198 "Name of the last project file entered by the user.")
200 (defconst ada-prj-file-extension
".adp"
201 "The extension used for project files.")
203 (defvar ada-xref-runtime-library-specs-path
'()
204 "Directories where the specs for the standard library is found.
205 This is used for cross-references.")
207 (defvar ada-xref-runtime-library-ali-path
'()
208 "Directories where the ali for the standard library is found.
209 This is used for cross-references.")
211 (defvar ada-xref-pos-ring
'()
212 "List of positions selected by the cross-references functions.
213 Used to go back to these positions.")
215 (defvar ada-cd-command
216 (if (string-match "cmdproxy.exe" shell-file-name
)
219 "Command to use to change to a specific directory.
220 On Windows systems using `cmdproxy.exe' as the shell,
221 we need to use `/d' or the drive is never changed.")
223 (defvar ada-command-separator
(if ada-on-ms-windows
" && " "\n")
224 "Separator to use between multiple commands to `compile' or `start-process'.
225 `cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use
228 (defconst ada-xref-pos-ring-max
16
229 "Number of positions kept in the list `ada-xref-pos-ring'.")
231 (defvar ada-operator-re
232 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
233 "Regexp to match for operators.")
235 (defvar ada-xref-project-files
'()
236 "Associative list of project files with properties.
237 It has the format: (project project ...)
238 A project has the format: (project-file . project-plist)
239 \(See `apropos plist' for operations on property lists).
240 See `ada-default-prj-properties' for the list of valid properties.
241 The current project is retrieved with `ada-xref-current-project'.
242 Properties are retrieved with `ada-xref-get-project-field', set with
243 `ada-xref-set-project-field'. If project properties are accessed with no
244 project file, a (nil . default-properties) entry is created.")
247 ;; ----- Identlist manipulation -------------------------------------------
248 ;; An identlist is a vector that is used internally to reference an identifier
249 ;; To facilitate its use, we provide the following macros
251 (defmacro ada-make-identlist
() (make-vector 8 nil
))
252 (defmacro ada-name-of
(identlist) (list 'aref identlist
0))
253 (defmacro ada-line-of
(identlist) (list 'aref identlist
1))
254 (defmacro ada-column-of
(identlist) (list 'aref identlist
2))
255 (defmacro ada-file-of
(identlist) (list 'aref identlist
3))
256 (defmacro ada-ali-index-of
(identlist) (list 'aref identlist
4))
257 (defmacro ada-declare-file-of
(identlist) (list 'aref identlist
5))
258 (defmacro ada-references-of
(identlist) (list 'aref identlist
6))
259 (defmacro ada-on-declaration
(identlist) (list 'aref identlist
7))
261 (defmacro ada-set-name
(identlist name
) (list 'aset identlist
0 name
))
262 (defmacro ada-set-line
(identlist line
) (list 'aset identlist
1 line
))
263 (defmacro ada-set-column
(identlist col
) (list 'aset identlist
2 col
))
264 (defmacro ada-set-file
(identlist file
) (list 'aset identlist
3 file
))
265 (defmacro ada-set-ali-index
(identlist index
) (list 'aset identlist
4 index
))
266 (defmacro ada-set-declare-file
(identlist file
) (list 'aset identlist
5 file
))
267 (defmacro ada-set-references
(identlist ref
) (list 'aset identlist
6 ref
))
268 (defmacro ada-set-on-declaration
(ident value
) (list 'aset ident
7 value
))
270 (defsubst ada-get-ali-buffer
(file)
271 "Read the ali file FILE into a new buffer, and return the buffer's name."
272 (find-file-noselect (ada-get-ali-file-name file
)))
275 ;; -----------------------------------------------------------------------
277 (defun ada-quote-cmd (cmd)
278 "Duplicate all `\\' characters in CMD so that it can be passed to `compile'."
279 (mapconcat 'identity
(split-string cmd
"\\\\") "\\\\"))
281 (defun ada-find-executable (exec-name)
282 "Find the full path to the executable file EXEC-NAME.
283 If not found, throw an error.
284 On Windows systems, this will properly handle .exe extension as well."
285 (let ((result (or (ada-find-file-in-dir exec-name exec-path
)
286 (ada-find-file-in-dir (concat exec-name
".exe") exec-path
))))
289 (error "`%s' not found in path" exec-name
))))
291 (defun ada-initialize-runtime-library (cross-prefix)
292 "Initialize the variables for the runtime library location.
293 CROSS-PREFIX is the prefix to use for the `gnatls' command."
296 ;; if gnatls not found, just give up (may not be using GNAT)
297 (ada-find-executable (concat cross-prefix
"gnatls"))
301 (setq ada-xref-runtime-library-specs-path
'()
302 ada-xref-runtime-library-ali-path
'())
303 (set-buffer (get-buffer-create "*gnatls*"))
306 ;; Even if we get an error, delete the *gnatls* buffer
308 (let ((status (apply 'call-process gnatls
(append '(nil t nil
) ada-gnatls-args
))))
309 (goto-char (point-min))
311 ;; Since we didn't provide all the inputs gnatls expects, it returns status 4
313 (error (buffer-substring (point) (line-end-position))))
317 (search-forward "Source Search Path:")
319 (while (not (looking-at "^$"))
320 (back-to-indentation)
321 (if (looking-at "<Current_Directory>")
322 (add-to-list 'ada-xref-runtime-library-specs-path
".")
323 (add-to-list 'ada-xref-runtime-library-specs-path
324 (buffer-substring-no-properties
331 (search-forward "Object Search Path:")
333 (while (not (looking-at "^$"))
334 (back-to-indentation)
335 (if (looking-at "<Current_Directory>")
336 (add-to-list 'ada-xref-runtime-library-ali-path
".")
337 (add-to-list 'ada-xref-runtime-library-ali-path
338 (buffer-substring-no-properties
345 (setq ada-xref-runtime-library-specs-path
346 (reverse ada-xref-runtime-library-specs-path
))
347 (setq ada-xref-runtime-library-ali-path
348 (reverse ada-xref-runtime-library-ali-path
))
351 (defun ada-gnat-parse-gpr (plist gpr-file
)
352 "Set gpr_file, src_dir and obj_dir properties in PLIST by parsing GPR-FILE.
353 Return new value of PLIST.
354 GPR_FILE must be full path to file, normalized.
355 src_dir, obj_dir will include compiler runtime.
356 Assumes environment variable ADA_PROJECT_PATH is set properly."
357 (with-current-buffer (get-buffer-create "*gnatls*")
360 ;; this can take a long time; let the user know what's up
361 (message "Parsing %s ..." gpr-file
)
363 ;; Even if we get an error, delete the *gnatls* buffer
365 (let* ((cross-prefix (plist-get plist
'cross_prefix
))
366 (gnat (concat cross-prefix ada-gnat-cmd
))
367 ;; Putting quotes around gpr-file confuses gnatpath on Lynx; not clear why
368 (gpr-opt (concat "-P" gpr-file
))
371 (status (call-process gnat nil t nil
"list" "-v" gpr-opt
)))
372 (goto-char (point-min))
375 (error (buffer-substring (point) (line-end-position))))
379 (search-forward "Source Search Path:")
380 (forward-line 1) ; first directory in list
381 (while (not (looking-at "^$")) ; terminate on blank line
382 (back-to-indentation) ; skip whitespace
383 (add-to-list 'src-dir
384 (if (looking-at "<Current_Directory>")
387 (buffer-substring-no-properties
388 (point) (line-end-position)))))
393 (search-forward "Object Search Path:")
395 (while (not (looking-at "^$"))
396 (back-to-indentation)
397 (add-to-list 'obj-dir
398 (if (looking-at "<Current_Directory>")
401 (buffer-substring-no-properties
402 (point) (line-end-position)))))
406 (setq plist
(plist-put plist
'gpr_file gpr-file
))
407 (setq plist
(plist-put plist
'src_dir src-dir
))
408 (plist-put plist
'obj_dir obj-dir
)
411 (message "Parsing %s ... done" gpr-file
)
415 (defun ada-treat-cmd-string (cmd-string)
416 "Replace variable references ${var} in CMD-STRING with the appropriate value.
417 Also replace standard environment variables $var.
418 Assumes project exists.
419 As a special case, ${current} is replaced with the name of the current
420 file, minus extension but with directory, and ${full_current} is
421 replaced by the name including the extension."
423 (while (string-match "\\(-[^-$IO]*[IO]\\)?${\\([^}]+\\)}" cmd-string
)
425 (name (match-string 2 cmd-string
)))
427 ((string= name
"current")
428 (setq value
(file-name-sans-extension (buffer-file-name))))
429 ((string= name
"full_current")
430 (setq value
(buffer-file-name)))
433 (setq value
(ada-xref-get-project-field (intern name
))))))
435 ;; Check if there is an environment variable with the same name
437 (if (not (setq value
(getenv name
)))
438 (message "%s" (concat "No project or environment variable " name
" found"))))
442 (setq cmd-string
(replace-match "" t t cmd-string
)))
444 (setq cmd-string
(replace-match value t t cmd-string
)))
446 (let ((prefix (match-string 1 cmd-string
)))
447 (setq cmd-string
(replace-match
448 (mapconcat (lambda(x) (concat prefix x
)) value
" ")
451 (substitute-in-file-name cmd-string
))
454 (defun ada-xref-get-project-field (field)
455 "Extract the value of FIELD from the current project file.
456 Project variables are substituted.
458 Note that for src_dir and obj_dir, you should rather use
459 `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field'
460 which will in addition return the default paths."
462 (let* ((project-plist (cdr (ada-xref-current-project)))
463 (value (plist-get project-plist field
)))
466 ((eq field
'gnatmake_opt
)
467 (let ((gpr-file (plist-get project-plist
'gpr_file
)))
468 (if (not (string= gpr-file
""))
469 (setq value
(concat "-P\"" gpr-file
"\" " value
)))))
471 ;; FIXME: check for src_dir, obj_dir here, rather than requiring user to do it
475 ;; Substitute the ${...} constructs in all the strings, including
479 (ada-treat-cmd-string value
))
483 (mapcar (lambda(x) (if x
(ada-treat-cmd-string x
) x
)) value
))
489 (defun ada-xref-get-src-dir-field ()
490 "Return the full value for src_dir, including the default directories.
491 All the directories are returned as absolute directories."
493 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
495 ;; Add ${build_dir} in front of the path
498 (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir
)
501 ;; Add the standard runtime at the end
502 ada-xref-runtime-library-specs-path
)))
504 (defun ada-xref-get-obj-dir-field ()
505 "Return the full value for obj_dir, including the default directories.
506 All the directories are returned as absolute directories."
508 (let ((build-dir (ada-xref-get-project-field 'build_dir
)))
510 ;; Add ${build_dir} in front of the path
513 (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir
)
516 ;; Add the standard runtime at the end
517 ada-xref-runtime-library-ali-path
)))
519 (defun ada-xref-set-project-field (field value
)
520 "Set FIELD to VALUE in current project. Assumes project exists."
521 ;; same algorithm to find project-plist as ada-xref-current-project
522 (let* ((file-name (ada-xref-current-project-file))
523 (project-plist (cdr (assoc file-name ada-xref-project-files
))))
525 (setq project-plist
(plist-put project-plist field value
))
526 (setcdr (assoc file-name ada-xref-project-files
) project-plist
)))
528 (defun ada-xref-update-project-menu ()
529 "Update the menu Ada->Project, with the list of available project files."
530 ;; Create the standard items.
533 ["Load..." ada-set-default-project-file t
]
534 ["New..." ada-prj-new t
]
535 ["Edit..." ada-prj-edit t
]
537 ;; Add the project files
540 (let* ((name (or (car x
) "<default>"))
542 "Select the current project file."
544 (ada-select-prj-file ,name
))))
546 (file-name-nondirectory name
)
550 (equal ada-prj-default-project-file
554 (or ada-xref-project-files
'(nil))))))
556 (easy-menu-add-item ada-mode-menu
'() submenu
)))
559 ;;-------------------------------------------------------------
560 ;;-- Searching a file anywhere on the source path.
562 ;;-- The following functions provide support for finding a file anywhere
563 ;;-- on the source path, without providing an explicit directory.
564 ;;-- They also provide file name completion in the minibuffer.
566 ;;-- Public subprograms: ada-find-file
568 ;;-------------------------------------------------------------
570 (defun ada-do-file-completion (string predicate flag
)
571 "Completion function when reading a file from the minibuffer.
572 Completion is attempted in all the directories in the source path,
573 as defined in the project file."
574 ;; FIXME: doc arguments
576 ;; This function is not itself interactive, but it is called as part
577 ;; of the prompt of interactive functions, so we require a project
579 (ada-require-project-file)
581 (dirs (ada-xref-get-src-dir-field)))
584 (if (file-directory-p (car dirs
))
585 (setq list
(append list
(file-name-all-completions string
(car dirs
)))))
586 (setq dirs
(cdr dirs
)))
587 (cond ((equal flag
'lambda
)
592 (try-completion string
593 (mapcar (lambda (x) (cons x
1)) list
)
597 (defun ada-find-file (filename)
598 "Open FILENAME, from anywhere in the source path.
599 Completion is available."
601 (list (completing-read "File: " 'ada-do-file-completion
)))
602 (let ((file (ada-find-src-file-in-dir filename
)))
605 (error "%s not found in src_dir" filename
))))
608 ;; ----- Utilities -------------------------------------------------
610 (defun ada-require-project-file ()
611 "If the current project does not exist, load or create a default one.
612 Should only be called from interactive functions."
613 (if (string= "" ada-prj-default-project-file
)
614 (ada-reread-prj-file (ada-prj-find-prj-file t
))))
616 (defun ada-xref-current-project-file ()
617 "Return the current project file name; never nil.
618 Call `ada-require-project-file' first if a project must exist."
619 (if (not (string= "" ada-prj-default-project-file
))
620 ada-prj-default-project-file
621 (ada-prj-find-prj-file t
)))
623 (defun ada-xref-current-project ()
624 "Return the current project.
625 Call `ada-require-project-file' first to ensure a project exists."
626 (let ((file-name (ada-xref-current-project-file)))
627 (assoc file-name ada-xref-project-files
)))
629 (defun ada-show-current-project ()
630 "Display current project file name in message buffer."
632 (message (ada-xref-current-project-file)))
634 (defun ada-show-current-main ()
635 "Display current main file name in message buffer."
637 (message "ada-mode main: %s" (ada-xref-get-project-field 'main
)))
639 (defun ada-xref-push-pos (filename position
)
640 "Push (FILENAME, POSITION) on the position ring for cross-references."
641 (setq ada-xref-pos-ring
(cons (list position filename
) ada-xref-pos-ring
))
642 (if (> (length ada-xref-pos-ring
) ada-xref-pos-ring-max
)
643 (setcdr (nthcdr (1- ada-xref-pos-ring-max
) ada-xref-pos-ring
) nil
)))
645 (defun ada-xref-goto-previous-reference ()
646 "Go to the previous cross-reference we were on."
648 (if ada-xref-pos-ring
649 (let ((pos (car ada-xref-pos-ring
)))
650 (setq ada-xref-pos-ring
(cdr ada-xref-pos-ring
))
651 (find-file (car (cdr pos
)))
652 (goto-char (car pos
)))))
654 (defun ada-set-default-project-file (file)
655 "Set FILE as the current project file."
656 (interactive "fProject file:")
657 (ada-parse-prj-file file
)
658 (ada-select-prj-file file
))
660 ;; ------ Handling the project file -----------------------------
662 (defun ada-prj-find-prj-file (&optional no-user-question
)
663 "Find the project file associated with the current buffer.
664 If the buffer is not in Ada mode, or not associated with a file,
665 return `ada-prj-default-project-file'. Otherwise, search for a file with
666 the same base name as the Ada file, but extension given by
667 `ada-prj-file-extension' (default .adp). If not found, search for *.adp
668 in the current directory; if several are found, and NO-USER-QUESTION
669 is non-nil, prompt the user to select one. If none are found, return
674 (if (not (and (derived-mode-p 'ada-mode
)
677 ;; Not in an Ada buffer, or current buffer not associated
678 ;; with a file (for instance an emerge buffer)
681 ;; other cases: use a more complex algorithm
683 (let* ((current-file (buffer-file-name))
684 (first-choice (concat
685 (file-name-sans-extension current-file
)
686 ada-prj-file-extension
))
687 (dir (file-name-directory current-file
))
689 (prj-files (directory-files
691 (concat ".*" (regexp-quote
692 ada-prj-file-extension
) "$")))
697 ((file-exists-p first-choice
)
699 (setq selected first-choice
))
701 ((= (length prj-files
) 1)
702 ;; Exactly one project file was found in the current directory
703 (setq selected
(car prj-files
)))
705 ((and (> (length prj-files
) 1) (not no-user-question
))
706 ;; multiple project files in current directory, ask the user
707 (save-window-excursion
708 (with-output-to-temp-buffer "*choice list*"
709 (princ "There are more than one possible project file.\n")
710 (princ "Which one should we use ?\n\n")
711 (princ " no. file name \n")
712 (princ " --- ------------------------\n")
714 (while (<= counter
(length prj-files
))
715 (princ (format " %2d) %s\n"
717 (nth (1- counter
) prj-files
)))
718 (setq counter
(1+ counter
))
720 ))) ; end of with-output-to ...
724 (not (integerp choice
))
726 (> choice
(length prj-files
)))
727 (setq choice
(string-to-number
728 (read-from-minibuffer "Enter No. of your choice: "))))
729 (setq selected
(nth (1- choice
) prj-files
))))
731 ((= (length prj-files
) 0)
732 ;; No project file in the current directory; ask user
733 (unless (or no-user-question
(not ada-always-ask-project
))
734 (setq ada-last-prj-file
736 (concat "project file [" ada-last-prj-file
"]:")
737 nil ada-last-prj-file
))
738 (unless (string= ada-last-prj-file
"")
739 (setq selected ada-last-prj-file
))))
742 (or selected
"default.adp")
745 (defun ada-default-prj-properties ()
746 "Return the default project properties list with the current buffer as main."
748 (let ((file (buffer-file-name nil
)))
750 ;; variable name alphabetical order
751 'ada_project_path
(or (getenv "ADA_PROJECT_PATH") "")
752 'ada_project_path_sep ada-prj-ada-project-path-sep
753 'bind_opt ada-prj-default-bind-opt
754 'build_dir default-directory
755 'casing
(if (listp ada-case-exception-file
)
756 ada-case-exception-file
757 (list ada-case-exception-file
))
758 'check_cmd
(list ada-prj-default-check-cmd
) ;; FIXME: should not a list
759 'comp_cmd
(list ada-prj-default-comp-cmd
) ;; FIXME: should not a list
760 'comp_opt ada-prj-default-comp-opt
762 'debug_cmd
(concat ada-prj-default-debugger
763 " ${main}" (if ada-on-ms-windows
".exe")) ;; FIXME: don't need .exe?
764 'debug_post_cmd
(list nil
)
765 'debug_pre_cmd
(list (concat ada-cd-command
" ${build_dir}"))
766 'gnatmake_opt ada-prj-default-gnatmake-opt
767 'gnatfind_opt ada-prj-gnatfind-switches
768 'gpr_file ada-prj-default-gpr-file
769 'link_opt ada-prj-default-link-opt
771 (file-name-nondirectory
772 (file-name-sans-extension file
))
774 'make_cmd
(list ada-prj-default-make-cmd
) ;; FIXME: should not a list
777 'run_cmd
(list (concat "./${main}" (if ada-on-ms-windows
".exe")))
778 ;; FIXME: should not a list
779 ;; FIXME: don't need .exe?
783 (defun ada-parse-prj-file (prj-file)
784 "Read PRJ-FILE, set project properties in `ada-xref-project-files'."
785 (let ((project (ada-default-prj-properties)))
787 (setq prj-file
(expand-file-name prj-file
))
788 (if (string= (file-name-extension prj-file
) "gpr")
789 (setq project
(ada-gnat-parse-gpr project prj-file
))
791 (setq project
(ada-parse-prj-file-1 prj-file project
))
794 ;; Store the project properties
795 (if (assoc prj-file ada-xref-project-files
)
796 (setcdr (assoc prj-file ada-xref-project-files
) project
)
797 (add-to-list 'ada-xref-project-files
(cons prj-file project
)))
799 (ada-xref-update-project-menu)
802 (defun ada-parse-prj-file-1 (prj-file project
)
803 "Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
804 Return new value of PROJECT."
805 (let ((ada-buffer (current-buffer))
806 ;; fields that are lists or otherwise require special processing
807 ada_project_path casing comp_cmd check_cmd
808 debug_pre_cmd debug_post_cmd gpr_file make_cmd obj_dir src_dir run_cmd
)
810 ;; Give users a chance to use compiler-specific project file formats
811 (let ((buffer (run-hook-with-args-until-success
812 'ada-load-project-hook prj-file
)))
814 ;; we load the project file with no warnings; if it does not
815 ;; exist, we stay in the Ada buffer; no project variable
816 ;; settings will be found. That works for the default
817 ;; "default.adp", which does not exist as a file.
818 (setq buffer
(find-file-noselect prj-file nil
)))
822 (goto-char (point-min))
827 ;; ignore lines that don't have the format "name=value", put
828 ;; 'name', 'value' in match-string.
829 (if (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
831 ;; FIXME: strip trailing spaces
832 ;; variable name alphabetical order
833 ((string= (match-string 1) "ada_project_path")
834 (add-to-list 'ada_project_path
836 (substitute-in-file-name (match-string 2)))))
838 ((string= (match-string 1) "build_dir")
840 (plist-put project
'build_dir
841 (file-name-as-directory (match-string 2)))))
843 ((string= (match-string 1) "casing")
845 (expand-file-name (substitute-in-file-name (match-string 2)))))
847 ((string= (match-string 1) "check_cmd")
848 (add-to-list 'check_cmd
(match-string 2)))
850 ((string= (match-string 1) "comp_cmd")
851 (add-to-list 'comp_cmd
(match-string 2)))
853 ((string= (match-string 1) "debug_post_cmd")
854 (add-to-list 'debug_post_cmd
(match-string 2)))
856 ((string= (match-string 1) "debug_pre_cmd")
857 (add-to-list 'debug_pre_cmd
(match-string 2)))
859 ((string= (match-string 1) "gpr_file")
860 ;; expand now; path is relative to Emacs project file
861 (setq gpr_file
(expand-file-name (match-string 2))))
863 ((string= (match-string 1) "make_cmd")
864 (add-to-list 'make_cmd
(match-string 2)))
866 ((string= (match-string 1) "obj_dir")
867 (add-to-list 'obj_dir
868 (file-name-as-directory
869 (expand-file-name (match-string 2)))))
871 ((string= (match-string 1) "run_cmd")
872 (add-to-list 'run_cmd
(match-string 2)))
874 ((string= (match-string 1) "src_dir")
875 (add-to-list 'src_dir
876 (file-name-as-directory
877 (expand-file-name (match-string 2)))))
880 ;; any other field in the file is just copied
881 (setq project
(plist-put project
882 (intern (match-string 1))
883 (match-string 2))))))
889 ;; back to the user buffer
890 (set-buffer ada-buffer
)
892 ;; process accumulated lists
894 (let ((sep (plist-get project
'ada_project_path_sep
)))
895 (setq ada_project_path
(reverse ada_project_path
))
896 (setq ada_project_path
(mapconcat 'identity ada_project_path sep
))
897 (setq project
(plist-put project
'ada_project_path ada_project_path
))
898 ;; env var needed now for ada-gnat-parse-gpr
899 (setenv "ADA_PROJECT_PATH" ada_project_path
)))
901 (if debug_post_cmd
(setq project
(plist-put project
'debug_post_cmd
(reverse debug_post_cmd
))))
902 (if debug_pre_cmd
(setq project
(plist-put project
'debug_pre_cmd
(reverse debug_pre_cmd
))))
903 (if casing
(setq project
(plist-put project
'casing
(reverse casing
))))
904 (if check_cmd
(setq project
(plist-put project
'check_cmd
(reverse check_cmd
))))
905 (if comp_cmd
(setq project
(plist-put project
'comp_cmd
(reverse comp_cmd
))))
906 (if make_cmd
(setq project
(plist-put project
'make_cmd
(reverse make_cmd
))))
907 (if run_cmd
(setq project
(plist-put project
'run_cmd
(reverse run_cmd
))))
911 (setq project
(ada-gnat-parse-gpr project gpr_file
))
912 ;; append Ada source and object directories to others from Emacs project file
913 (setq src_dir
(append (plist-get project
'src_dir
) src_dir
))
914 (setq obj_dir
(append (plist-get project
'obj_dir
) obj_dir
))
915 (setq ada-xref-runtime-library-specs-path
'()
916 ada-xref-runtime-library-ali-path
'()))
919 ;; FIXME: gnatpath.exe doesn't output the runtime libraries, so always call ada-initialize-runtime-library
920 ;; if using a gpr_file, the runtime library directories are
921 ;; included in src_dir and obj_dir; otherwise they are in the
922 ;; 'runtime-library' variables.
923 ;; FIXME: always append to src_dir, obj_dir
924 (ada-initialize-runtime-library (or (ada-xref-get-project-field 'cross_prefix
) ""))
927 (if obj_dir
(setq project
(plist-put project
'obj_dir
(reverse obj_dir
))))
928 (if src_dir
(setq project
(plist-put project
'src_dir
(reverse src_dir
))))
933 (defun ada-select-prj-file (file)
934 "Select FILE as the current project file."
936 (setq ada-prj-default-project-file
(expand-file-name file
))
938 (let ((casing (ada-xref-get-project-field 'casing
)))
941 ;; FIXME: use ada-get-absolute-dir here
942 (setq ada-case-exception-file casing
)
943 (ada-case-read-exceptions))))
945 (let ((ada_project_path (ada-xref-get-project-field 'ada_project_path
)))
947 ;; FIXME: use ada-get-absolute-dir, mapconcat here
948 (setenv "ADA_PROJECT_PATH" ada_project_path
)))
950 (setq compilation-search-path
(ada-xref-get-src-dir-field))
952 (setq ada-search-directories-internal
953 ;; FIXME: why do we need directory-file-name here?
954 (append (mapcar 'directory-file-name compilation-search-path
)
955 ada-search-directories
))
957 ;; return t, for decent display in message buffer when called interactively
960 (defun ada-find-references (&optional pos arg local-only
)
961 "Find all references to the entity under POS.
962 Calls gnatfind to find the references.
963 If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved.
964 If LOCAL-ONLY is non-nil, only declarations in the current file are returned."
966 (ada-require-project-file)
968 (let* ((identlist (ada-read-identifier pos
))
969 (alifile (ada-get-ali-file-name (ada-file-of identlist
)))
970 (process-environment (ada-set-environment)))
972 (set-buffer (get-file-buffer (ada-file-of identlist
)))
974 ;; if the file is more recent than the executable
975 (if (or (buffer-modified-p (current-buffer))
976 (file-newer-than-file-p (ada-file-of identlist
) alifile
))
977 (ada-find-any-references (ada-name-of identlist
)
978 (ada-file-of identlist
)
979 nil nil local-only arg
)
980 (ada-find-any-references (ada-name-of identlist
)
981 (ada-file-of identlist
)
982 (ada-line-of identlist
)
983 (ada-column-of identlist
) local-only arg
)))
986 (defun ada-find-local-references (&optional pos arg
)
987 "Find all references to the entity under POS.
988 Calls `gnatfind' to find the references.
989 If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved."
991 (ada-find-references pos arg t
))
993 (defconst ada-gnatfind-buffer-name
"*gnatfind*")
995 (defun ada-find-any-references
996 (entity &optional file line column local-only append
)
997 "Search for references to any entity whose name is ENTITY.
998 ENTITY was first found the location given by FILE, LINE and COLUMN.
999 If LOCAL-ONLY is non-nil, then list only the references in FILE,
1000 which is much faster.
1001 If APPEND is non-nil, then append the output of the command to the
1002 existing buffer `*gnatfind*', if there is one."
1003 (interactive "sEntity name: ")
1004 (ada-require-project-file)
1006 ;; Prepare the gnatfind command. Note that we must protect the quotes
1007 ;; around operators, so that they are correctly handled and can be
1008 ;; processed (gnatfind \"+\":...).
1009 (let* ((quote-entity
1010 (if (= (aref entity
0) ?
\")
1011 (if ada-on-ms-windows
1012 (concat "\\\"" (substring entity
1 -
1) "\\\"")
1013 (concat "'\"" (substring entity
1 -
1) "\"'"))
1015 (switches (ada-xref-get-project-field 'gnatfind_opt
))
1016 ;; FIXME: use gpr_file
1017 (cross-prefix (ada-xref-get-project-field 'cross_prefix
))
1018 (command (concat cross-prefix
"gnat find " switches
" "
1020 (if file
(concat ":" (file-name-nondirectory file
)))
1021 (if line
(concat ":" line
))
1022 (if column
(concat ":" column
))
1023 (if local-only
(concat " " (file-name-nondirectory file
)))
1027 ;; If a project file is defined, use it
1028 (if (and ada-prj-default-project-file
1029 (not (string= ada-prj-default-project-file
"")))
1030 (if (string-equal (file-name-extension ada-prj-default-project-file
)
1032 (setq command
(concat command
" -P\"" ada-prj-default-project-file
"\""))
1033 (setq command
(concat command
" -p\"" ada-prj-default-project-file
"\""))))
1035 (if (and append
(get-buffer ada-gnatfind-buffer-name
))
1036 (with-current-buffer "*gnatfind*"
1037 (setq old-contents
(buffer-string))))
1039 (let ((compilation-error "reference"))
1040 (compilation-start command
'compilation-mode
(lambda (_mode) ada-gnatfind-buffer-name
)))
1042 ;; Hide the "Compilation" menu
1043 (with-current-buffer ada-gnatfind-buffer-name
1044 (local-unset-key [menu-bar compilation-menu
])
1049 (setq buffer-read-only nil
)
1050 (insert old-contents
)
1051 (setq buffer-read-only t
)
1052 (goto-char (point-max)))))
1056 (defalias 'ada-change-prj
(symbol-function 'ada-set-default-project-file
))
1058 ;; ----- Identifier Completion --------------------------------------------
1059 (defun ada-complete-identifier (pos)
1060 "Try to complete the identifier around POS, using compiler cross-reference information."
1062 (ada-require-project-file)
1064 ;; Initialize function-local variables and jump to the .ali buffer
1065 ;; Note that for regexp search is case insensitive too
1066 (let* ((curbuf (current-buffer))
1067 (identlist (ada-read-identifier pos
))
1068 (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
1069 (regexp-quote (ada-name-of identlist
))
1070 "[a-zA-Z0-9_]*\\)"))
1074 ;; Open the .ali file
1075 (set-buffer (ada-get-ali-buffer (buffer-file-name)))
1076 (goto-char (point-max))
1078 ;; build an alist of possible completions
1079 (while (re-search-backward sofar nil t
)
1080 (setq symalist
(cons (cons (match-string 1) nil
) symalist
)))
1082 (setq completed
(try-completion "" symalist
))
1084 ;; kills .ali buffer
1087 ;; deletes the incomplete identifier in the buffer
1089 (looking-at "[a-zA-Z0-9_]+")
1091 ;; inserts the completed symbol
1095 ;; ----- Cross-referencing ----------------------------------------
1097 (defun ada-point-and-xref ()
1098 "Jump to the declaration of the entity below the cursor."
1100 (mouse-set-point last-input-event
)
1101 (ada-goto-declaration (point)))
1103 (defun ada-point-and-xref-body ()
1104 "Jump to the body of the entity under the cursor."
1106 (mouse-set-point last-input-event
)
1107 (ada-goto-body (point)))
1109 (defun ada-goto-body (pos &optional other-frame
)
1110 "Display the body of the entity around POS.
1111 OTHER-FRAME non-nil means display in another frame.
1112 If the entity doesn't have a body, display its declaration.
1113 As a side effect, the buffer for the declaration is also open."
1115 (ada-goto-declaration pos other-frame
)
1117 ;; Temporarily force the display in the same buffer, since we
1118 ;; already changed previously
1119 (let ((ada-xref-other-buffer nil
))
1120 (ada-goto-declaration (point) nil
)))
1122 (defun ada-goto-declaration (pos &optional other-frame
)
1123 "Display the declaration of the identifier around POS.
1124 The declaration is shown in another buffer if `ada-xref-other-buffer' is
1126 If OTHER-FRAME is non-nil, display the cross-reference in another frame."
1128 (ada-require-project-file)
1130 (ada-xref-push-pos (buffer-file-name) pos
)
1132 ;; First try the standard algorithm by looking into the .ali file, but if
1133 ;; that file was too old or even did not exist, try to look in the whole
1134 ;; object path for a possible location.
1135 (let ((identlist (ada-read-identifier pos
)))
1137 (ada-find-in-ali identlist other-frame
)
1138 ;; File not found: print explicit error message
1139 (ada-error-file-not-found
1140 (message (concat (error-message-string err
)
1144 (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist
))))
1146 ;; If the ALI file was up-to-date, then we probably have a predefined
1147 ;; entity, whose references are not given by GNAT
1148 (if (and (file-exists-p ali-file
)
1149 (file-newer-than-file-p ali-file
(ada-file-of identlist
)))
1150 (message "No cross-reference found -- may be a predefined entity.")
1152 ;; Else, look in every ALI file, except if the user doesn't want that
1153 (if ada-xref-search-with-egrep
1154 (ada-find-in-src-path identlist other-frame
)
1155 (message "Cross-referencing information is not up-to-date; please recompile.")
1158 (defun ada-goto-declaration-other-frame (pos)
1159 "Display the declaration of the identifier around POS.
1160 The declaration is shown in another frame if `ada-xref-other-buffer' is
1163 (ada-goto-declaration pos t
))
1165 (defun ada-remote (command)
1166 "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
1167 (let ((machine (ada-xref-get-project-field 'remote_machine
)))
1168 (if (or (not machine
) (string= machine
""))
1170 (format "%s %s '(%s)'"
1171 remote-shell-program
1175 (defun ada-get-absolute-dir-list (dir-list root-dir
)
1176 "Return the list of absolute directories found in DIR-LIST.
1177 If a directory is a relative directory, ROOT-DIR is prepended.
1178 Project and environment variables are substituted."
1179 (mapcar (lambda (x) (expand-file-name x
(ada-treat-cmd-string root-dir
))) dir-list
))
1181 (defun ada-set-environment ()
1182 "Prepare an environment for Ada compilation.
1183 This returns a new value to use for `process-environment',
1184 but does not actually put it into use.
1185 It modifies the source path and object path with the values found in the
1187 (let ((include (getenv "ADA_INCLUDE_PATH"))
1188 (objects (getenv "ADA_OBJECTS_PATH"))
1189 (build-dir (ada-xref-get-project-field 'build_dir
)))
1191 (setq include
(concat path-separator include
)))
1193 (setq objects
(concat path-separator objects
)))
1195 (concat "ADA_INCLUDE_PATH="
1196 (mapconcat (lambda(x) (expand-file-name x build-dir
))
1197 (ada-xref-get-project-field 'src_dir
)
1201 (concat "ADA_OBJECTS_PATH="
1202 (mapconcat (lambda(x) (expand-file-name x build-dir
))
1203 (ada-xref-get-project-field 'obj_dir
)
1206 process-environment
))))
1208 (defun ada-compile-application (&optional arg
)
1209 "Compile the application, using the command found in the project file.
1210 If ARG is not nil, ask for user confirmation."
1212 (ada-require-project-file)
1213 (let ((cmd (ada-xref-get-project-field 'make_cmd
))
1214 (process-environment (ada-set-environment))
1215 (compilation-scroll-output t
))
1217 (setq compilation-search-path
(ada-xref-get-src-dir-field))
1219 ;; If no project file was found, ask the user
1221 (setq cmd
'("") arg t
))
1223 ;; Make a single command from the list of commands, including the
1224 ;; commands to run it on a remote machine.
1225 (setq cmd
(ada-remote (mapconcat 'identity cmd ada-command-separator
)))
1227 (if (or ada-xref-confirm-compile arg
)
1228 (setq cmd
(read-from-minibuffer "enter command to compile: " cmd
)))
1230 ;; Insert newlines so as to separate the name of the commands to run
1231 ;; and the output of the commands. This doesn't work with cmdproxy.exe,
1232 ;; which gets confused by newline characters.
1233 (if (not (string-match ".exe" shell-file-name
))
1234 (setq cmd
(concat cmd
"\n\n")))
1236 (compile (ada-quote-cmd cmd
))))
1238 (defun ada-set-main-compile-application ()
1239 "Set main project variable to current buffer, build main."
1241 (ada-require-project-file)
1242 (let* ((file (buffer-file-name (current-buffer)))
1245 (error "No file for current buffer")
1249 (file-name-nondirectory
1250 (file-name-sans-extension file
))
1252 (ada-xref-set-project-field 'main main
)
1253 (ada-compile-application))))
1255 (defun ada-compile-current (&optional arg prj-field
)
1256 "Recompile the current file.
1257 If ARG is non-nil, ask for user confirmation of the command.
1258 PRJ-FIELD is the name of the field to use in the project file to get the
1259 command, and should be either `comp_cmd' (default) or `check_cmd'."
1261 (ada-require-project-file)
1262 (let* ((field (if prj-field prj-field
'comp_cmd
))
1263 (cmd (ada-xref-get-project-field field
))
1264 (process-environment (ada-set-environment))
1265 (compilation-scroll-output t
))
1268 (setq cmd
'("") arg t
))
1270 ;; Make a single command from the list of commands, including the
1271 ;; commands to run it on a remote machine.
1272 (setq cmd
(ada-remote (mapconcat 'identity cmd ada-command-separator
)))
1274 ;; If no project file was found, ask the user
1275 (if (or ada-xref-confirm-compile arg
)
1276 (setq cmd
(read-from-minibuffer "enter command to compile: " cmd
)))
1278 (compile (ada-quote-cmd cmd
))))
1280 (defun ada-check-current (&optional arg
)
1281 "Check the current file for syntax errors.
1282 If ARG is non-nil, ask for user confirmation of the command."
1284 (ada-compile-current arg
'check_cmd
))
1286 (defun ada-run-application (&optional arg
)
1287 "Run the application.
1288 If ARG is non-nil, ask for user confirmation."
1290 (ada-require-project-file)
1292 (let ((machine (ada-xref-get-project-field 'cross_prefix
)))
1293 (if (and machine
(not (string= machine
"")))
1294 (error "This feature is not supported yet for cross environments")))
1296 (let ((command (ada-xref-get-project-field 'run_cmd
)))
1298 ;; Guess the command if it wasn't specified
1300 (setq command
(list (file-name-sans-extension (buffer-name)))))
1302 ;; Modify the command to run remotely
1303 (setq command
(ada-remote (mapconcat 'identity command
1304 ada-command-separator
)))
1306 ;; Ask for the arguments to the command if required
1307 (if (or ada-xref-confirm-compile arg
)
1308 (setq command
(read-from-minibuffer "Enter command to execute: "
1312 (with-current-buffer (get-buffer-create "*run*")
1313 (setq buffer-read-only nil
)
1316 (start-process "run" (current-buffer) shell-file-name
1319 ;; Set these two variables to their default values, since otherwise
1320 ;; the output buffer is scrolled so that only the last output line
1321 ;; is visible at the top of the buffer.
1322 (set (make-local-variable 'scroll-step
) 0)
1323 (set (make-local-variable 'scroll-conservatively
) 0)
1325 (display-buffer "*run*")
1327 ;; change to buffer *run* for interactive programs
1329 (switch-to-buffer "*run*")
1332 (defun ada-gdb-application (&optional arg executable-name
)
1333 "Start the debugger on the application.
1334 If ARG is non-nil, ask the user to confirm the command.
1335 EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
1338 (ada-require-project-file)
1339 (let ((buffer (current-buffer))
1340 cmd pre-cmd post-cmd
)
1341 (setq cmd
(if executable-name
1342 (concat ada-prj-default-debugger
" " executable-name
)
1343 (ada-xref-get-project-field 'debug_cmd
))
1344 pre-cmd
(ada-xref-get-project-field 'debug_pre_cmd
)
1345 post-cmd
(ada-xref-get-project-field 'debug_post_cmd
))
1347 ;; If the command was not given in the project file, start a bare gdb
1349 (setq cmd
(concat ada-prj-default-debugger
1352 (file-name-sans-extension (buffer-file-name))))))
1354 ;; For gvd, add an extra switch so that the Emacs window is completely
1355 ;; swallowed inside the Gvd one
1356 (if (and ada-tight-gvd-integration
1357 (string-match "^[^ \t]*gvd" cmd
))
1358 ;; Start a new frame, so that when gvd exists we do not kill Emacs
1359 ;; We make sure that gvd swallows the new frame, not the one the
1360 ;; user has been using until now
1361 ;; The frame is made invisible initially, so that GtkPlug gets a
1362 ;; chance to fully manage it. Then it works fine with Enlightenment
1364 (let ((frame (make-frame '((visibility . nil
)))))
1366 cmd
" --editor-window="
1367 (cdr (assoc 'outer-window-id
(frame-parameters frame
)))))
1368 (select-frame frame
)))
1370 ;; Add a -fullname switch
1371 ;; Use the remote machine
1372 (setq cmd
(ada-remote (concat cmd
" -fullname ")))
1374 ;; Ask for confirmation if required
1375 (if (or arg ada-xref-confirm-compile
)
1376 (setq cmd
(read-from-minibuffer "enter command to debug: " cmd
)))
1378 (let ((old-comint-exec (symbol-function 'comint-exec
)))
1380 ;; Do not add -fullname, since we can have a 'rsh' command in front.
1381 ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef
1382 (fset 'gud-gdb-massage-args
(lambda (_file args
) args
))
1384 (setq pre-cmd
(mapconcat 'identity pre-cmd ada-command-separator
))
1385 (if (not (equal pre-cmd
""))
1386 (setq pre-cmd
(concat pre-cmd ada-command-separator
)))
1388 (setq post-cmd
(mapconcat 'identity post-cmd
"\n"))
1390 (setq post-cmd
(concat post-cmd
"\n")))
1393 ;; Temporarily replaces the definition of `comint-exec' so that we
1394 ;; can execute commands before running gdb.
1395 ;; FIXME: This is evil and not temporary !!! -stef
1397 `(lambda (buffer name command startfile switches
)
1398 (let (compilation-buffer-name-function)
1400 (setq compilation-buffer-name-function
1401 (lambda(x) (buffer-name buffer
)))
1402 (compile (ada-quote-cmd
1405 (mapconcat 'identity switches
" "))))))
1408 ;; Tight integration should force the tty mode
1409 (if (and (string-match "gvd" (comint-arguments cmd
0 0))
1410 ada-tight-gvd-integration
1411 (not (string-match "--tty" cmd
)))
1412 (setq cmd
(concat cmd
"--tty")))
1414 (if (and (string-match "jdb" (comint-arguments cmd
0 0))
1416 (funcall (symbol-function 'jdb
) cmd
)
1419 ;; Restore the standard fset command (or for instance C-U M-x shell
1420 ;; wouldn't work anymore
1422 (fset 'comint-exec old-comint-exec
)
1424 ;; Send post-commands to the debugger
1425 (process-send-string (get-buffer-process (current-buffer)) post-cmd
)
1427 ;; Move to the end of the debugger buffer, so that it is automatically
1428 ;; scrolled from then on.
1429 (goto-char (point-max))
1431 ;; Display both the source window and the debugger window (the former
1432 ;; above the latter). No need to show the debugger window unless it
1433 ;; is going to have some relevant information.
1434 (if (or (not (string-match "gvd" (comint-arguments cmd
0 0)))
1435 (string-match "--tty" cmd
))
1436 (split-window-below))
1437 (switch-to-buffer buffer
)
1440 (defun ada-reread-prj-file (&optional filename
)
1441 "Reread either the current project, or FILENAME if non-nil.
1442 If FILENAME is non-nil, set it as current project."
1445 (setq filename ada-prj-default-project-file
))
1446 (ada-parse-prj-file filename
)
1447 (ada-select-prj-file filename
))
1449 ;; ------ Private routines
1451 (defun ada-xref-current (file &optional ali-file-name
)
1452 "Update the cross-references for FILE.
1453 This in fact recompiles FILE to create ALI-FILE-NAME.
1454 This function returns the name of the file that was recompiled to generate
1455 the cross-reference information. Note that the ali file can then be deduced
1456 by replacing the file extension with `.ali'."
1458 (if (and ali-file-name
1459 (get-file-buffer ali-file-name
))
1460 (kill-buffer (get-file-buffer ali-file-name
)))
1462 (let* ((name (convert-standard-filename file
))
1463 (body-name (or (ada-get-body-name name
) name
)))
1465 ;; Always recompile the body when we can. We thus temporarily switch to a
1466 ;; buffer than contains the body of the unit
1468 (let ((body-visible (find-buffer-visiting body-name
))
1471 (set-buffer body-visible
)
1472 (find-file body-name
))
1474 ;; Execute the compilation. Note that we must wait for the end of the
1475 ;; process, or the ALI file would still not be available.
1476 ;; Unfortunately, the underlying `compile' command that we use is
1478 (ada-compile-current)
1479 (setq process
(get-buffer-process "*compilation*"))
1482 (not (equal (process-status process
) 'exit
)))
1485 ;; remove the buffer for the body if it wasn't there before
1486 (unless body-visible
1487 (kill-buffer (find-buffer-visiting body-name
)))
1491 (defun ada-find-file-in-dir (file dir-list
)
1492 "Search for FILE in DIR-LIST."
1494 (while (and (not found
) dir-list
)
1495 (setq found
(concat (file-name-as-directory (car dir-list
))
1496 (file-name-nondirectory file
)))
1498 (unless (file-exists-p found
)
1500 (setq dir-list
(cdr dir-list
)))
1503 (defun ada-find-ali-file-in-dir (file)
1504 "Find the ali file FILE, searching obj_dir for the current project.
1505 Adds build_dir in front of the search path to conform to gnatmake's behavior,
1506 and the standard runtime location at the end."
1507 (ada-find-file-in-dir file
(ada-xref-get-obj-dir-field)))
1509 (defun ada-find-src-file-in-dir (file)
1510 "Find the source file FILE, searching src_dir for the current project.
1511 Adds the standard runtime location at the end of the search path to conform
1512 to gnatmake's behavior."
1513 (ada-find-file-in-dir file
(ada-xref-get-src-dir-field)))
1515 (defun ada-get-ali-file-name (file)
1516 "Create the ali file name for the Ada file FILE.
1517 The file is searched for in every directory shown in the obj_dir lines of
1520 ;; This function has to handle the special case of non-standard
1521 ;; file names (i.e. not .adb or .ads)
1522 ;; The trick is the following:
1523 ;; 1- replace the extension of the current file with .ali,
1524 ;; and look for this file
1525 ;; 2- If this file is found:
1526 ;; grep the "^U" lines, and make sure we are not reading the
1527 ;; .ali file for a spec file. If we are, go to step 3.
1528 ;; 3- If the file is not found or step 2 failed:
1529 ;; find the name of the "other file", ie the body, and look
1530 ;; for its associated .ali file by substituting the extension
1532 ;; We must also handle the case of separate packages and subprograms:
1533 ;; 4- If no ali file was found, we try to modify the file name by removing
1534 ;; everything after the last '-' or '.' character, so as to get the
1535 ;; ali file for the parent unit. If we found an ali file, we check that
1536 ;; it indeed contains the definition for the separate entity by checking
1537 ;; the 'D' lines. This is done repeatedly, in case the direct parent is
1540 (with-current-buffer (get-file-buffer file
)
1541 (let ((short-ali-file-name (concat (file-name-base file
) ".ali"))
1545 ;; If we have a non-standard file name, and this is a spec, we first
1546 ;; look for the .ali file of the body, since this is the one that
1547 ;; contains the most complete information. If not found, we will do what
1548 ;; we can with the .ali file for the spec...
1550 (if (not (string= (file-name-extension file
) "ads"))
1551 (let ((specs ada-spec-suffixes
))
1553 (if (string-match (concat (regexp-quote (car specs
)) "$")
1556 (setq specs
(cdr specs
)))))
1560 (ada-find-ali-file-in-dir
1561 (concat (file-name-base (ada-other-file-name)) ".ali"))))
1567 ;; Else we take the .ali file associated with the unit
1568 (ada-find-ali-file-in-dir short-ali-file-name
)
1571 ;; else we did not find the .ali file Second chance: in case
1572 ;; the files do not have standard names (such as for instance
1573 ;; file_s.ada and file_b.ada), try to go to the other file
1574 ;; and look for its ali file
1575 (ada-find-ali-file-in-dir
1576 (concat (file-name-base (ada-other-file-name)) ".ali"))
1579 ;; If we still don't have an ali file, try to get the one
1580 ;; from the parent unit, in case we have a separate entity.
1581 (let ((parent-name (file-name-base file
)))
1583 (while (and (not ali-file-name
)
1584 (string-match "^\\(.*\\)[.-][^.-]*" parent-name
))
1586 (setq parent-name
(match-string 1 parent-name
))
1587 (setq ali-file-name
(ada-find-ali-file-in-dir
1588 (concat parent-name
".ali")))
1592 ;; If still not found, try to recompile the file
1593 (if (not ali-file-name
)
1594 ;; Recompile only if the user asked for this, and search the ali
1595 ;; filename again. We avoid a possible infinite recursion by
1596 ;; temporarily disabling the automatic compilation.
1598 (if ada-xref-create-ali
1600 (concat (file-name-sans-extension (ada-xref-current file
))
1603 (error "`.ali' file not found; recompile your source file"))
1606 ;; same if the .ali file is too old and we must recompile it
1607 (if (and (file-newer-than-file-p file ali-file-name
)
1608 ada-xref-create-ali
)
1609 (ada-xref-current file ali-file-name
)))
1611 ;; Always return the correct absolute file name
1612 (expand-file-name ali-file-name
))
1615 (defun ada-get-ada-file-name (file original-file
)
1616 "Create the complete file name (+directory) for FILE.
1617 The original file (where the user was) is ORIGINAL-FILE.
1618 Search in project file for possible paths."
1622 ;; If the buffer for original-file, use it to get the values from the
1623 ;; project file, otherwise load the file and its project file
1624 (let ((buffer (get-file-buffer original-file
)))
1627 (find-file original-file
)))
1629 ;; we choose the first possible completion and we
1630 ;; return the absolute file name
1631 (let ((filename (ada-find-src-file-in-dir file
)))
1633 (expand-file-name filename
)
1634 (signal 'ada-error-file-not-found
(file-name-nondirectory file
)))
1637 (defun ada-find-file-number-in-ali (file)
1638 "Return the file number for FILE in the associated ali file."
1639 (set-buffer (ada-get-ali-buffer file
))
1640 (goto-char (point-min))
1642 (let ((begin (re-search-forward "^D")))
1644 (re-search-forward (concat "^D " (file-name-nondirectory file
)))
1645 (count-lines begin
(point))))
1647 (defun ada-read-identifier (pos)
1648 "Return the identlist around POS and switch to the .ali buffer.
1649 The returned list represents the entity, and can be manipulated through the
1650 macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
1652 ;; If at end of buffer (e.g the buffer is empty), error
1653 (if (>= (point) (point-max))
1654 (error "No identifier on point"))
1656 ;; goto first character of the identifier/operator (skip backward < and >
1657 ;; since they are part of multiple character operators
1659 (skip-chars-backward "a-zA-Z0-9_<>")
1661 ;; check if it really is an identifier
1662 (if (ada-in-comment-p)
1663 (error "Inside comment"))
1665 (let (identifier identlist
)
1666 ;; Just in front of a string => we could have an operator declaration,
1667 ;; as in "+", "-", ..
1668 (if (= (char-after) ?
\")
1671 ;; if looking at an operator
1672 ;; This is only true if:
1673 ;; - the symbol is +, -, ...
1674 ;; - the symbol is made of letters, and not followed by _ or a letter
1675 (if (and (looking-at ada-operator-re
)
1676 (or (not (= (char-syntax (char-after)) ?w
))
1677 (not (or (= (char-syntax (char-after (match-end 0))) ?w
)
1678 (= (char-after (match-end 0)) ?_
)))))
1680 (if (and (= (char-before) ?
\")
1681 (= (char-after (+ (length (match-string 0)) (point))) ?
\"))
1683 (setq identifier
(regexp-quote (concat "\"" (match-string 0) "\""))))
1685 (if (ada-in-string-p)
1686 (error "Inside string or character constant"))
1687 (if (looking-at (concat ada-keywords
"[^a-zA-Z_]"))
1688 (error "No cross-reference available for reserved keyword"))
1689 (if (looking-at "[a-zA-Z0-9_]+")
1690 (setq identifier
(match-string 0))
1691 (error "No identifier around")))
1693 ;; Build the identlist
1694 (setq identlist
(ada-make-identlist))
1695 (ada-set-name identlist
(downcase identifier
))
1696 (ada-set-line identlist
1697 (number-to-string (count-lines 1 (point))))
1698 (ada-set-column identlist
1699 (number-to-string (1+ (current-column))))
1700 (ada-set-file identlist
(buffer-file-name))
1704 (defun ada-get-all-references (identlist)
1705 "Complete IDENTLIST with definition file and places where it is referenced.
1706 Information is extracted from the ali file."
1708 (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist
)))
1710 (set-buffer ali-buffer
)
1711 (goto-char (point-min))
1712 (ada-set-on-declaration identlist nil
)
1714 ;; First attempt: we might already be on the declaration of the identifier
1715 ;; We want to look for the declaration only in a definite interval (after
1716 ;; the "^X ..." line for the current file, and before the next "^X" line
1718 (if (re-search-forward
1719 (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist
)))
1721 (let ((bound (save-excursion (re-search-forward "^X " nil t
))))
1722 (setq declaration-found
1724 (concat "^" (ada-line-of identlist
)
1725 "." (ada-column-of identlist
)
1726 "[ *]" (ada-name-of identlist
)
1727 "[{[(<= ]?\\(.*\\)$") bound t
))
1728 (if declaration-found
1729 (ada-set-on-declaration identlist t
))
1732 ;; If declaration is still nil, then we were not on a declaration, and
1733 ;; have to fall back on other algorithms
1735 (unless declaration-found
1737 ;; Since we already know the number of the file, search for a direct
1739 (goto-char (point-min))
1740 (setq declaration-found t
)
1743 (number-to-string (ada-find-file-number-in-ali
1744 (ada-file-of identlist
))))
1745 (unless (re-search-forward (concat (ada-ali-index-of identlist
)
1746 "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
1747 (ada-line-of identlist
)
1749 (ada-column-of identlist
) "\\>")
1752 ;; if we did not find it, it may be because the first reference
1753 ;; is not required to have a 'unit_number|' item included.
1754 ;; Or maybe we are already on the declaration...
1755 (unless (re-search-forward
1757 "^[0-9]+.[0-9]+[ *]"
1758 (ada-name-of identlist
)
1759 "[ <{=([]\\(.\\|\n\\.\\)*\\<"
1760 (ada-line-of identlist
)
1762 (ada-column-of identlist
) "\\>")
1765 ;; If still not found, then either the declaration is unknown
1766 ;; or the source file has been modified since the ali file was
1768 (setq declaration-found nil
)
1772 ;; Last check to be completely sure we have found the correct line (the
1773 ;; ali might not be up to date for instance)
1774 (if declaration-found
1777 ;; while we have a continuation line, go up one line
1778 (while (looking-at "^\\.")
1780 (beginning-of-line))
1781 (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
1782 (ada-name-of identlist
) "[ <{=([]"))
1783 (setq declaration-found nil
))))
1785 ;; Still no success ! The ali file must be too old, and we need to
1786 ;; use a basic algorithm based on guesses. Note that this only happens
1787 ;; if the user does not want us to automatically recompile files
1789 (unless declaration-found
1790 (if (ada-xref-find-in-modified-ali identlist
)
1791 (setq declaration-found t
)
1792 ;; No more idea to find the declaration. Give up
1794 (kill-buffer ali-buffer
)
1796 (error "No declaration of %s found" (ada-name-of identlist
))
1801 ;; Now that we have found a suitable line in the .ali file, get the
1802 ;; information available
1804 (if declaration-found
1805 (let ((current-line (buffer-substring
1806 (point) (point-at-eol))))
1810 (while (looking-at "^\\.\\(.*\\)")
1811 (setq current-line
(concat current-line
(match-string 1)))
1815 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t
)
1817 ;; If we can find the file
1819 (ada-set-declare-file
1821 (ada-get-ada-file-name (match-string 1)
1822 (ada-file-of identlist
)))
1824 ;; Else clean up the ali file
1825 (ada-error-file-not-found
1826 (signal (car err
) (cdr err
)))
1828 (kill-buffer ali-buffer
)
1829 (error (error-message-string err
)))
1832 (ada-set-references identlist current-line
)
1836 (defun ada-xref-find-in-modified-ali (identlist)
1837 "Find the matching position for IDENTLIST in the current ali buffer.
1838 This function is only called when the file was not up-to-date, so we need
1839 to make some guesses.
1840 This function is disabled for operators, and only works for identifiers."
1842 (unless (= (string-to-char (ada-name-of identlist
)) ?
\")
1844 (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
1845 (my-regexp (concat "[ *]"
1846 (regexp-quote (ada-name-of identlist
)) " "))
1852 (ali-buffer (current-buffer)))
1854 (goto-char (point-max))
1855 (while (re-search-backward my-regexp nil t
)
1857 (setq line-ali
(count-lines 1 (point)))
1859 ;; have a look at the line and column numbers
1860 (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
1862 (setq line-ada
(match-string 1))
1863 (setq col-ada
(match-string 2)))
1864 (setq line-ada
"--")
1867 ;; construct a list with the file names and the positions within
1868 (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t
)
1870 'declist
(list line-ali
(match-string 1) line-ada col-ada
))
1875 ;; how many possible declarations have we found ?
1876 (setq len
(length declist
))
1880 (kill-buffer (current-buffer))
1881 (error "No declaration of %s recorded in .ali file"
1882 (ada-name-of identlist
)))
1883 ;; one => should be the right one
1885 (goto-char (point-min))
1886 (forward-line (1- (caar declist
))))
1888 ;; more than one => display choice list
1890 (save-window-excursion
1891 (with-output-to-temp-buffer "*choice list*"
1893 (princ "Identifier is overloaded and Xref information is not up to date.\n")
1894 (princ "Possible declarations are:\n\n")
1895 (princ " no. in file at line col\n")
1896 (princ " --- --------------------- ---- ----\n")
1898 (while (< counter len
)
1899 (princ (format " %2d) %-21s %4s %4s\n"
1901 (ada-get-ada-file-name
1902 (nth 1 (nth counter declist
))
1903 (ada-file-of identlist
))
1904 (nth 2 (nth counter declist
))
1905 (nth 3 (nth counter declist
))
1907 (setq counter
(1+ counter
))
1910 ) ; end of with-output-to ...
1914 (not (integerp choice
))
1919 (read-from-minibuffer "Enter No. of your choice: "))))
1921 (set-buffer ali-buffer
)
1922 (goto-char (point-min))
1923 (forward-line (1- (car (nth (1- choice
) declist
))))
1927 (defun ada-find-in-ali (identlist &optional other-frame
)
1928 "Look in the .ali file for the definition of the identifier in IDENTLIST.
1929 If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil,
1930 opens a new window to show the declaration."
1932 (ada-get-all-references identlist
)
1933 (let ((ali-line (ada-references-of identlist
))
1938 ;; Note: in some cases, an entity can have multiple references to the
1939 ;; bodies (this is for instance the case for a separate subprogram, that
1940 ;; has a reference both to the stub and to the real body).
1941 ;; In that case, we simply go to each one in turn.
1943 ;; Get all the possible locations
1944 (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line
)
1945 (setq locations
(list (list (match-string 1 ali-line
) ;; line
1946 (match-string 2 ali-line
) ;; column
1947 (ada-declare-file-of identlist
))))
1948 (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)"
1950 (setq line
(match-string 1 ali-line
)
1951 col
(match-string 3 ali-line
)
1952 start
(match-end 3))
1954 ;; it there was a file number in the same line
1955 ;; Make sure we correctly handle the case where the first file reference
1956 ;; on the line is the type reference.
1957 ;; 1U2 T(2|2r3) 34r23
1958 (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?"
1959 (match-string 0 ali-line
))
1961 (let ((file-number (match-string 1 ali-line
)))
1962 (goto-char (point-min))
1963 (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
1964 (string-to-number file-number
))
1965 (setq file
(match-string 1))
1967 ;; Else get the nearest file
1968 (setq file
(ada-declare-file-of identlist
)))
1970 (setq locations
(append locations
(list (list line col file
)))))
1972 ;; Add the specs at the end again, so that from the last body we go to
1974 (setq locations
(append locations
(list (car locations
))))
1976 ;; Find the new location we want to go to.
1977 ;; If we are on none of the locations listed, we simply go to the specs.
1979 (setq line
(caar locations
)
1980 col
(nth 1 (car locations
))
1981 file
(nth 2 (car locations
)))
1984 (if (and (string= (caar locations
) (ada-line-of identlist
))
1985 (string= (nth 1 (car locations
)) (ada-column-of identlist
))
1986 (string= (file-name-nondirectory (nth 2 (car locations
)))
1987 (file-name-nondirectory (ada-file-of identlist
))))
1988 (setq locations
(cadr locations
)
1989 line
(car locations
)
1990 col
(nth 1 locations
)
1991 file
(nth 2 locations
)
1993 (setq locations
(cdr locations
))))
1995 ;; Find the file in the source path
1996 (setq file
(ada-get-ada-file-name file
(ada-file-of identlist
)))
1998 ;; Kill the .ali buffer
1999 (kill-buffer (current-buffer))
2001 ;; Now go to the buffer
2002 (ada-xref-change-buffer file
2003 (string-to-number line
)
2004 (1- (string-to-number col
))
2009 (defun ada-find-in-src-path (identlist &optional other-frame
)
2010 "More general function for cross-references.
2011 This function should be used when the standard algorithm that parses the
2012 .ali file has failed, either because that file was too old or even did not
2014 This function attempts to find the possible declarations for the identifier
2015 anywhere in the object path.
2016 This command requires the external `grep' program to be available.
2018 This works well when one is using an external library and wants to find
2019 the declaration and documentation of the subprograms one is using."
2020 ;; FIXME: what does this function do?
2022 (dirs (ada-xref-get-obj-dir-field))
2023 (regexp (concat "[ *]" (ada-name-of identlist
)))
2028 ;; Do the grep in all the directories. We do multiple shell
2029 ;; commands instead of one in case there is no .ali file in one
2030 ;; of the directory and the shell stops because of that.
2032 (with-current-buffer (get-buffer-create "*grep*")
2034 (insert (shell-command-to-string
2037 (shell-quote-argument (concat "^X|" regexp
"( |$)"))
2039 (shell-quote-argument (file-name-as-directory (car dirs
)))
2041 (setq dirs
(cdr dirs
)))
2043 ;; Now parse the output
2044 (setq case-fold-search t
)
2045 (goto-char (point-min))
2046 (while (re-search-forward regexp nil t
)
2049 (if (not (= (char-after) ?X
))
2051 (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
2052 (setq line
(match-string 1)
2053 column
(match-string 2))
2054 (re-search-backward "^X [0-9]+ \\(.*\\)$")
2055 (setq file
(list (match-string 1) line column
))
2057 ;; There could be duplicate choices, because of the structure
2058 ;; of the .ali files
2059 (unless (member file list
)
2060 (setq list
(append list
(list file
))))))))
2062 ;; Current buffer is still "*grep*"
2063 (kill-buffer "*grep*")
2066 ;; Now display the list of possible matches
2069 ;; No choice found => Error
2071 (error "No cross-reference found, please recompile your file"))
2073 ;; Only one choice => Do the cross-reference
2074 ((= (length list
) 1)
2075 (setq file
(ada-find-src-file-in-dir (caar list
)))
2077 (ada-xref-change-buffer file
2078 (string-to-number (nth 1 (car list
)))
2079 (string-to-number (nth 2 (car list
)))
2082 (error "%s not found in src_dir" (caar list
)))
2083 (message "This is only a (good) guess at the cross-reference.")
2086 ;; Else, ask the user
2088 (save-window-excursion
2089 (with-output-to-temp-buffer "*choice list*"
2091 (princ "Identifier is overloaded and Xref information is not up to date.\n")
2092 (princ "Possible declarations are:\n\n")
2093 (princ " no. in file at line col\n")
2094 (princ " --- --------------------- ---- ----\n")
2096 (while (< counter
(length list
))
2097 (princ (format " %2d) %-21s %4s %4s\n"
2099 (nth 0 (nth counter list
))
2100 (nth 1 (nth counter list
))
2101 (nth 2 (nth counter list
))
2103 (setq counter
(1+ counter
))
2106 (while (or (not choice
)
2107 (not (integerp choice
))
2109 (> choice
(length list
)))
2112 (read-from-minibuffer "Enter No. of your choice: "))))
2114 (setq choice
(1- choice
))
2115 (kill-buffer "*choice list*")
2117 (setq file
(ada-find-src-file-in-dir (car (nth choice list
))))
2119 (ada-xref-change-buffer file
2120 (string-to-number (nth 1 (nth choice list
)))
2121 (string-to-number (nth 2 (nth choice list
)))
2124 (signal 'ada-error-file-not-found
(car (nth choice list
))))
2125 (message "This is only a (good) guess at the cross-reference.")
2128 (defun ada-xref-change-buffer
2129 (file line column identlist
&optional other-frame
)
2130 "Select and display FILE, at LINE and COLUMN.
2131 If we do not end on the same identifier as IDENTLIST, find the
2132 closest match. Kills the .ali buffer at the end.
2133 If OTHER-FRAME is non-nil, creates a new frame to show the file."
2135 (let (declaration-buffer)
2137 ;; Select and display the destination buffer
2138 (if ada-xref-other-buffer
2140 (find-file-other-frame file
)
2141 (setq declaration-buffer
(find-file-noselect file
))
2142 (set-buffer declaration-buffer
)
2143 (switch-to-buffer-other-window declaration-buffer
)
2148 ;; move the cursor to the correct position
2150 (goto-char (point-min))
2151 (forward-line (1- line
))
2152 (move-to-column column
)
2154 ;; If we are not on the identifier, the ali file was not up-to-date.
2155 ;; Try to find the nearest position where the identifier is found,
2156 ;; this is probably the right one.
2157 (unless (looking-at (ada-name-of identlist
))
2158 (ada-xref-search-nearest (ada-name-of identlist
)))
2162 (defun ada-xref-search-nearest (name)
2163 "Search for NAME nearest to the position recorded in the Xref file.
2164 Return the position of the declaration in the buffer, or nil if not found."
2165 (let ((orgpos (point))
2169 (goto-char (point-max))
2171 ;; loop - look for all declarations of name in this file
2172 (while (search-backward name nil t
)
2174 ;; check if it really is a complete Ada identifier
2176 (not (save-excursion
2177 (goto-char (match-end 0))
2179 (not (ada-in-string-or-comment-p))
2181 ;; variable declaration ?
2183 (skip-chars-forward "a-zA-Z_0-9" )
2184 (ada-goto-next-non-ws)
2185 (looking-at ":[^=]"))
2186 ;; procedure, function, task or package declaration ?
2188 (ada-goto-previous-word)
2189 (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]\\>"))))
2191 ;; check if it is nearer than the ones before if any
2193 (< (abs (- (point) orgpos
)) diff
))
2195 (setq newpos
(point)
2196 diff
(abs (- newpos orgpos
))))))
2201 (message "ATTENTION: this declaration is only a (good) guess ...")
2206 ;; Find the parent library file of the current file
2207 (defun ada-goto-parent ()
2208 "Go to the parent library file."
2210 (ada-require-project-file)
2212 (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
2216 (with-current-buffer buffer
2217 (goto-char (point-min))
2218 (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
2219 (setq unit-name
(match-string 1))
2220 (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name
))
2222 (kill-buffer buffer
)
2223 (error "No parent unit !"))
2224 (setq unit-name
(match-string 1 unit-name
))
2227 ;; look for the file name for the parent unit specification
2228 (goto-char (point-min))
2229 (re-search-forward (concat "^W " unit-name
2230 "%s[ \t]+\\([^ \t]+\\)[ \t]+"
2232 (setq body-name
(match-string 1))
2233 (setq ali-name
(match-string 2))
2234 (kill-buffer buffer
)
2237 (setq ali-name
(ada-find-ali-file-in-dir ali-name
))
2240 ;; Tries to open the new ali file to find the spec file
2243 (find-file ali-name
)
2244 (goto-char (point-min))
2245 (re-search-forward (concat "^U " unit-name
"%s[ \t]+"
2247 (setq body-name
(match-string 1))
2248 (kill-buffer (current-buffer))
2253 (find-file body-name
)
2256 (defun ada-make-filename-from-adaname (adaname)
2257 "Determine the filename in which ADANAME is found.
2258 This is a GNAT specific function that uses gnatkrunch."
2259 (let ((krunch-buf (generate-new-buffer "*gkrunch*"))
2260 (cross-prefix (plist-get (cdr (ada-xref-current-project)) 'cross_prefix
)))
2261 (with-current-buffer krunch-buf
2262 ;; send adaname to external process `gnatkr'.
2263 ;; Add a dummy extension, since gnatkr versions have two different
2264 ;; behaviors depending on the version:
2265 ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc
2266 ;; After: "AA.BB.CC" => aa-bb.cc
2267 (call-process (concat cross-prefix
"gnatkr") nil krunch-buf nil
2268 (concat adaname
".adb") ada-krunch-args
)
2269 ;; fetch output of that process
2270 (setq adaname
(buffer-substring
2273 (goto-char (point-min))
2276 ;; Remove the extra extension we added above
2277 (setq adaname
(substring adaname
0 -
4))
2279 (kill-buffer krunch-buf
)))
2283 (defun ada-make-body-gnatstub (&optional interactive
)
2284 "Create an Ada package body in the current buffer.
2285 This function uses the `gnat stub' program to create the body.
2286 This function typically is to be hooked into `ff-file-created-hook'.
2287 If INTERACTIVE is nil, assume this is called from `ff-file-created-hook'."
2289 (ada-require-project-file)
2291 ;; If not interactive, assume we are being called from
2292 ;; ff-file-created-hook. Then the current buffer is for the body
2293 ;; file, but we will create a new one after gnat stub runs
2295 (set-buffer-modified-p nil
)
2296 (kill-buffer (current-buffer)))
2298 (save-some-buffers nil nil
)
2300 ;; Make sure the current buffer is the spec, so gnat stub gets the
2301 ;; right package parameter (this might not be the case if for
2302 ;; instance the user was asked for a project file)
2304 (unless (buffer-file-name (car (buffer-list)))
2305 (set-buffer (cadr (buffer-list))))
2307 ;; Call the external process
2308 (let* ((project-plist (cdr (ada-xref-current-project)))
2309 (gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts
))
2310 (gpr-file (plist-get project-plist
'gpr_file
))
2311 (filename (buffer-file-name (car (buffer-list))))
2312 (output (concat (file-name-sans-extension filename
) ".adb"))
2313 (cross-prefix (plist-get project-plist
'cross_prefix
))
2314 (gnatstub-cmd (concat cross-prefix
"gnat stub"
2315 (if (not (string= gpr-file
""))
2316 (concat " -P\"" gpr-file
"\""))
2317 " " gnatstub-opts
" " filename
))
2318 (buffer (get-buffer-create "*gnat stub*")))
2320 (with-current-buffer buffer
2321 (compilation-minor-mode 1)
2323 (insert gnatstub-cmd
)
2327 (call-process shell-file-name nil buffer nil
"-c" gnatstub-cmd
)
2329 ;; clean up the output
2331 (if (file-exists-p output
)
2334 (kill-buffer buffer
))
2336 ;; file not created; display the error message
2337 (display-buffer buffer
))))
2339 (defun ada-xref-initialize ()
2340 "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
2341 For instance, it creates the gnat-specific menus, sets some hooks for
2343 (remove-hook 'ff-file-created-hook
'ada-make-body
) ; from global hook
2344 (remove-hook 'ff-file-created-hook
'ada-make-body t
) ; from local hook
2345 (add-hook 'ff-file-created-hook
'ada-make-body-gnatstub nil t
)
2347 ;; Completion for file names in the mini buffer should ignore .ali files
2348 (add-to-list 'completion-ignored-extensions
".ali")
2350 (ada-xref-update-project-menu)
2353 ;; ----- Add to ada-mode-hook ---------------------------------------------
2355 ;; This must be done before initializing the Ada menu.
2356 (add-hook 'ada-mode-hook
'ada-xref-initialize
)
2358 ;; Define a new error type
2359 (define-error 'ada-error-file-not-found
2360 "File not found in src-dir (check project file): " 'ada-mode-errors
)
2364 ;;; ada-xref.el ends here