1 ;; Ada mode cross-reference functionality provided by the 'gnat xref'
2 ;; tool. Includes related functions, such as gnatprep support.
4 ;; These tools are all Ada-specific; see gnat-inspect for
5 ;; multi-language GNAT cross-reference tools.
7 ;; GNAT is provided by AdaCore; see http://libre.adacore.com/
9 ;;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
11 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
12 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
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/>.
31 ;; Emacs should enter Ada mode automatically when you load an Ada
32 ;; file, based on the file extension.
34 ;; By default, ada-mode is configured to load this file, so nothing
35 ;; special needs to done to use it.
37 (require 'ada-fix-error)
43 ;;;; uses of gnat tools
45 (defconst ada-gnat-file-line-col-regexp "\\(.*\\):\\([0-9]+\\):\\([0-9]+\\)")
47 (defun ada-gnat-xref-other (identifier file line col)
48 "For `ada-xref-other-function', using 'gnat find', which is Ada-specific."
50 (when (eq ?\" (aref identifier 0))
51 ;; gnat find wants the quotes on operators, but the column is after the first quote.
55 (let* ((arg (format "%s:%s:%d:%d" identifier file line col))
56 (switches (when (ada-prj-get 'gpr_ext) (concat "--ext=" (ada-prj-get 'gpr_ext))))
59 (with-current-buffer (gnat-run-buffer)
60 (gnat-run-gnat "find" (list switches arg))
62 (goto-char (point-min))
63 (forward-line 2); skip ADA_PROJECT_PATH, 'gnat find'
65 ;; gnat find returns two items; the starting point, and the 'other' point
66 (unless (looking-at (concat ada-gnat-file-line-col-regexp ":"))
68 (error "'%s' not found in cross-reference files; recompile?" identifier))
71 (looking-at (concat ada-gnat-file-line-col-regexp "\\(: warning:\\)?"))
73 ;; error in *.gpr; ignore here.
76 (let ((found-file (match-string 1))
77 (found-line (string-to-number (match-string 2)))
78 (found-col (string-to-number (match-string 3))))
81 (equal file found-file)
85 (setq result (list found-file found-line (1- found-col)))
90 (pop-to-buffer (current-buffer))
91 (error "gnat find did not return other item"))
95 (defun ada-gnat-xref-parents (identifier file line col)
96 "For `ada-xref-parents-function', using 'gnat find', which is Ada-specific."
98 (let* ((arg (format "%s:%s:%d:%d" identifier file line col))
101 (when (ada-prj-get 'gpr_ext) (concat "--ext=" (ada-prj-get 'gpr_ext)))
104 (with-current-buffer (gnat-run-buffer)
105 (gnat-run-gnat "find" (list switches arg))
107 (goto-char (point-min))
108 (forward-line 2); skip GPR_PROJECT_PATH, 'gnat find'
110 ;; gnat find returns two items; the starting point, and the 'other' point
111 (unless (looking-at (concat ada-gnat-file-line-col-regexp ":"))
113 (error "'%s' not found in cross-reference files; recompile?" identifier))
116 (looking-at (concat ada-gnat-file-line-col-regexp "\\(: warning:\\)?"))
118 ;; error in *.gpr; ignore here.
121 (let ((found-file (match-string 1))
122 (found-line (string-to-number (match-string 2)))
123 (found-col (string-to-number (match-string 3))))
125 (skip-syntax-forward "^ ")
126 (skip-syntax-forward " ")
127 (if (looking-at (concat "derived from .* (" ada-gnat-file-line-col-regexp ")"))
129 (setq result (list (match-string 1)
130 (string-to-number (match-string 2))
131 (1- (string-to-number (match-string 3)))))
135 (pop-to-buffer (current-buffer))
136 (error "gnat find did not return parent types"))
139 (ada-goto-source (nth 0 result)
146 (defun ada-gnat-xref-all (identifier file line col)
147 "For `ada-xref-all-function'."
148 ;; we use `compilation-start' to run gnat, not `gnat-run', so it
149 ;; is asynchronous, and automatically runs the compilation error
152 (let* ((cmd (format "gnat find -r %s:%s:%d:%d" identifier file line col)))
154 (with-current-buffer (gnat-run-buffer); for default-directory
155 (let ((compilation-environment (ada-prj-get 'proc_env))
156 (compilation-error "reference")
157 ;; gnat find uses standard gnu format for output, so don't
158 ;; need to set compilation-error-regexp-alist
160 (when (ada-prj-get 'gpr_file)
161 (setq cmd (concat cmd " -P" (file-name-nondirectory (ada-prj-get 'gpr_file)))))
163 (compilation-start cmd
165 (lambda (mode-name) (concat mode-name "-gnatfind")))
170 (defun ada-gnat-xref-select-prj ()
171 (setq ada-file-name-from-ada-name 'ada-gnat-file-name-from-ada-name)
172 (setq ada-ada-name-from-file-name 'ada-gnat-ada-name-from-file-name)
173 (setq ada-make-package-body 'ada-gnat-make-package-body)
175 (add-hook 'ada-syntax-propertize-hook 'gnatprep-syntax-propertize)
177 ;; must be after indentation engine setup, because that resets the
178 ;; indent function list.
179 (add-hook 'ada-mode-hook 'ada-gnat-xref-setup t)
181 (setq ada-xref-other-function 'ada-gnat-xref-other)
182 (setq ada-xref-parent-function 'ada-gnat-xref-parents)
183 (setq ada-xref-all-function 'ada-gnat-xref-all)
185 ;; gnatmake -gnatD generates files with .dg extensions. But we don't
186 ;; need to navigate between them.
188 ;; There is no common convention for a file extension for gnatprep files.
190 (add-to-list 'completion-ignored-extensions ".ali") ;; gnat library files, used for cross reference
191 (add-to-list 'compilation-error-regexp-alist 'gnat)
194 (defun ada-gnat-xref-deselect-prj ()
195 (setq ada-file-name-from-ada-name nil)
196 (setq ada-ada-name-from-file-name nil)
197 (setq ada-make-package-body nil)
199 (setq ada-syntax-propertize-hook (delq 'gnatprep-syntax-propertize ada-syntax-propertize-hook))
200 (setq ada-mode-hook (delq 'ada-gnat-xref-setup ada-mode-hook))
202 (setq ada-xref-other-function nil)
203 (setq ada-xref-parent-function nil)
204 (setq ada-xref-all-function nil)
206 (setq completion-ignored-extensions (delete ".ali" completion-ignored-extensions))
207 (setq compilation-error-regexp-alist (delete 'gnat compilation-error-regexp-alist))
210 (defun ada-gnat-xref-setup ()
211 (when (boundp 'wisi-indent-calculate-functions)
212 (add-to-list 'wisi-indent-calculate-functions 'gnatprep-indent))
215 (defun ada-gnat-xref ()
216 "Set Ada mode global vars to use 'gnat xref'"
217 (add-to-list 'ada-prj-file-ext-extra "gpr")
218 (add-to-list 'ada-prj-parser-alist '("gpr" . gnat-parse-gpr))
219 (add-to-list 'ada-select-prj-xref-tool '(gnat . ada-gnat-xref-select-prj))
220 (add-to-list 'ada-deselect-prj-xref-tool '(gnat . ada-gnat-xref-deselect-prj))
222 ;; no parse-*-xref yet
224 (font-lock-add-keywords 'ada-mode
225 ;; gnatprep preprocessor line
226 (list (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))))
228 (add-hook 'ada-gnat-fix-error-hook 'ada-gnat-fix-error))
232 (provide 'ada-gnat-xref)
233 (provide 'ada-xref-tool)
235 (unless (default-value 'ada-xref-tool)
236 (set-default 'ada-xref-tool 'gnat))