1 ;;; ada-mode.el --- major-mode for editing Ada sources
3 ;;; Copyright (C) 1994, 1995, 1997 - 2014 Free Software Foundation, Inc.
5 ;; Author: Stephen Leake <stephen_leake@member.fsf.org>
6 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
7 ;; Keywords FIXME: languages, ada ELPA broken for multiple keywords
9 ;; package-requires: ((wisi "1.0.4") (cl-lib "0.4") (emacs "24.2"))
10 ;; url: http://stephe-leake.org/emacs/ada-mode/emacs-ada-mode.html
12 ;; (Gnu ELPA requires single digits between dots in versions)
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. The default extensions for Ada
33 ;; files are .ads, .adb; use ada-add-extensions to add other
36 ;; By default, ada-mode is configured to take full advantage of the
37 ;; GNAT compiler. If you are using another compiler, you
38 ;; should load that compiler's ada-* file first; that will define
39 ;; ada-compiler as a feature, so ada-gnat.el will not be loaded.
41 ;; See the user guide (info "ada-mode"), built from ada-mode.texi.
45 ;; In order to support multiple compilers, we use indirect function
46 ;; calls for all operations that depend on the compiler.
48 ;; We also support a cross reference tool (also called xref tool) that
49 ;; is different from the compiler. For example, you can use a local
50 ;; GNAT compiler to generate and access cross-reference information,
51 ;; while using a cross-compiler for compiling the final executable.
53 ;; Other functions are lumped with the choice of xref tool; mapping
54 ;; Ada names to file names, creating package bodies; any tool function
55 ;; that does not create executable code.
57 ;; The indentation engine and skeleton tools are also called
58 ;; indirectly, to allow parallel development of new versions of these
59 ;; tools (inspired by experience with ada-smie and ada-wisi).
61 ;; We also support using different compilers for different projects;
62 ;; `ada-compiler' can be set in Ada mode project files. Note that
63 ;; there is only one project active at a time; the most recently
64 ;; selected one. All Ada files are assumed to belong to this project
65 ;; (which is not correct, but works well in practice; the user is
66 ;; typically only concerned about files that belong to the current
69 ;; There are several styles of indirect calls:
71 ;; - scalar global variable set during load
73 ;; Appropriate when the choice of implementation is fixed at load
74 ;; time; it does not depend on the current Ada project. Used for
75 ;; indentation and skeleton functions.
77 ;; - scalar global variable set during project select
79 ;; Appropriate when the choice of implementation is determined by
80 ;; the choice of compiler or xref tool, which is per-project. The
81 ;; user sets the compiler choice in the project file, but not the
82 ;; lower-level redirect choice.
84 ;; For example, `ada-file-name-from-ada-name' depends on the naming
85 ;; convention used by the compiler. If the project file sets
86 ;; ada_compiler to 'gnat (either directly or by default),
87 ;; ada-gnat-select-prj sets `ada-file-name-from-ada-name' to
88 ;; `ada-gnat-file-name-from-ada-name'.
90 ;; - scalar buffer-local variable set during project select or file open
92 ;; Appropriate when choice of implementation is normally
93 ;; per-project, but can be per-buffer.
95 ;; For example, `ada-case-strict' will normally be set by the
96 ;; project, but some files may deviate from the project standard (if
97 ;; they are generated by -fdumpspec, for example). Those files set
98 ;; `ada-case-strict' in a file local variable comment.
100 ;; - scalar buffer-local variable set by ada-mode or ada-mode-hook
103 ;; Appropriate when the variable is a non-Ada mode variable, also
104 ;; used by other modes, and choice should not affect those modes.
106 ;; `indent-line-function', `comment-indent-function' use this style
108 ;; - alist global variable indexed by ada-compiler
110 ;; Appropriate when the choice of implementation is determined by
111 ;; the compiler, but the function is invoked during project parse,
112 ;; so we can't depend on a value set by project select.
114 ;; alist entries are set during load by the implementation elisp files.
116 ;; `ada-prj-default-compiler-alist' uses this style.
120 ;; The first Ada mode for GNU Emacs was written by V. Broman in
121 ;; 1985. He based his work on the already existing Modula-2 mode.
122 ;; This was distributed as ada.el in versions of Emacs prior to 19.29.
124 ;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
125 ;; several files with support for dired commands and other nice
128 ;; The probably very first Ada mode (called electric-ada.el) was
129 ;; written by Steven D. Litvintchouk and Steven M. Rosen for the
130 ;; Gosling Emacs. L. Slater based his development on ada.el and
133 ;; A complete rewrite by Rolf Ebert <ebert@inf.enst.fr> and Markus
134 ;; Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de> was done at
135 ;; some point. Some ideas from the Ada mode mailing list have been
136 ;; added. Some of the functionality of L. Slater's mode has not (yet)
137 ;; been recoded in this new mode.
139 ;; A complete rewrite for Emacs-20 / GNAT-3.11 was done by Emmanuel
140 ;; Briot <briot@gnat.com> at Ada Core Technologies.
142 ;; A complete rewrite, to restructure the code more orthogonally, and
143 ;; to use wisi for the indentation engine, was done in 2012 - 2013 by
144 ;; Stephen Leake <stephen_leake@stephe-leake.org>.
148 ;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
149 ;; many patches included in this package.
150 ;; Christian Egli <Christian.Egli@hcsd.hac.com>:
151 ;; ada-imenu-generic-expression
152 ;; Many thanks also to the following persons that have contributed
154 ;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
155 ;; woodruff@stc.llnl.gov (John Woodruff)
156 ;; jj@ddci.dk (Jesper Joergensen)
157 ;; gse@ocsystems.com (Scott Evans)
158 ;; comar@gnat.com (Cyrille Comar)
159 ;; robin-reply@reagans.org
160 ;; and others for their valuable hints.
167 (defun ada-mode-version ()
168 "Return Ada mode version."
170 (let ((version-string "5.1.3"))
176 (if (called-interactively-p 'interactive)
177 (message version-string)
182 (defvar ada-mode-hook nil
183 "List of functions to call when Ada mode is invoked.
184 This hook is executed after `ada-mode' is fully loaded, but
185 before file local variables are processed.")
188 "Major mode for editing Ada source code in Emacs."
191 (defcustom ada-auto-case t
193 "Buffer-local value that may override project variable `auto_case'.
194 Global value is default for project variable `auto_case'.
195 Non-nil means automatically change case of preceding word while typing.
196 Casing of Ada keywords is done according to `ada-case-keyword',
197 identifiers are Mixed_Case."
201 (make-variable-buffer-local 'ada-auto-case)
203 (defcustom ada-case-exception-file nil
204 "Default list of special casing exceptions dictionaries for identifiers.
205 Override with 'casing' project variable.
207 New exceptions may be added interactively via `ada-case-create-exception'.
208 If an exception is defined in multiple files, the first occurence is used.
210 The file format is one word per line, that gives the casing to be
211 used for that word in Ada source code. If the line starts with
212 the character *, then the exception will be used for partial
213 words that either start at the beginning of a word or after a _
214 character, and end either at the end of the word or at a _
215 character. Characters after the first word are ignored, and not
216 preserved when the list is written back to the file."
217 :type '(repeat (file))
221 (defcustom ada-case-keyword 'downcase-word
222 "Buffer-local value that may override project variable `case_keyword'.
223 Global value is default for project variable `case_keyword'.
224 Function to call to adjust the case of Ada keywords."
225 :type '(choice (const downcase-word)
229 (make-variable-buffer-local 'ada-case-keyword)
231 (defcustom ada-case-identifier 'ada-mixed-case
232 "Buffer-local value that may override project variable `case_keyword'.
233 Global value is default for project variable `case_keyword'.
234 Function to call to adjust the case of Ada keywords."
235 :type '(choice (const ada-mixed-case)
236 (const downcase-region)
237 (const upcase-region))
240 (make-variable-buffer-local 'ada-case-identifier)
242 (defcustom ada-case-strict t
243 "Buffer-local value that may override project variable `case_strict'.
244 Global value is default for project variable `case_strict'.
245 If non-nil, force Mixed_Case for identifiers.
246 Otherwise, allow UPPERCASE for identifiers."
250 (make-variable-buffer-local 'ada-case-strict)
252 (defcustom ada-language-version 'ada2012
253 "Ada language version; one of `ada83', `ada95', `ada2005', `ada2012'.
254 Only affects the keywords to highlight, not which version the
255 indentation parser accepts."
256 :type '(choice (const ada83)
262 (make-variable-buffer-local 'ada-language-version)
264 (defcustom ada-fill-comment-prefix "-- "
265 "Comment fill prefix."
268 (make-variable-buffer-local 'ada-language-version)
270 (defcustom ada-fill-comment-postfix " --"
271 "Comment fill postfix."
274 (make-variable-buffer-local 'ada-language-version)
276 (defcustom ada-prj-file-extensions '("adp" "prj")
277 "List of Emacs Ada mode project file extensions.
278 Used when searching for a project file.
279 Any file with one of these extensions will be parsed by `ada-prj-parse-file-1'."
283 (defcustom ada-prj-file-ext-extra nil
284 "List of secondary project file extensions.
285 Used when searching for a project file that can be a primary or
286 secondary project file (referenced from a primary). The user
287 must provide a parser for a file with one of these extensions."
291 ;;;;; end of user variables
293 (defconst ada-symbol-end
294 ;; we can't just add \> here; that might match _ in a user modified ada-mode-syntax-table
296 "Regexp to add to symbol name in `ada-which-function'.")
298 (defvar ada-compiler nil
299 "Default Ada compiler; can be overridden in project files.
300 Values defined by compiler packages.")
302 (defvar ada-xref-tool nil
303 "Default Ada cross reference tool; can be overridden in project files.
304 Values defined by cross reference packages.")
306 ;;;; keymap and menus
308 (defvar ada-ret-binding nil)
309 (defvar ada-lfd-binding nil)
311 (defun ada-case-activate-keys ()
312 "Modify the key bindings for all the keys that should adjust casing."
314 ;; We can't use post-self-insert-hook for \n, \r, because they are
317 ;; The 'or ...' is there to be sure that the value will not be
318 ;; changed again when this is called more than once, since we
319 ;; are rebinding the keys.
320 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
321 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
328 'ada-case-adjust-interactive)))
329 '( ?_ ?% ?& ?* ?( ?) ?- ?= ?+
330 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r ))
332 (define-key ada-mode-map [return] 'ada-case-adjust-interactive)
336 (let ((map (make-sparse-keymap)))
337 ;; C-c <letter> are reserved for users
339 ;; global-map has C-x ` 'next-error
340 (define-key map [return] 'ada-indent-newline-indent)
341 (define-key map "\C-c`" 'ada-show-secondary-error)
342 (define-key map "\C-c;" (lambda () (error "use M-; instead"))) ; comment-dwim
343 (define-key map "\C-c\M-`" 'ada-fix-compiler-error)
344 (define-key map "\C-c\C-a" 'ada-align)
345 (define-key map "\C-c\C-b" 'ada-make-subprogram-body)
346 (define-key map "\C-c\C-c" 'ada-build-make)
347 (define-key map "\C-c\C-d" 'ada-goto-declaration)
348 (define-key map "\C-c\M-d" 'ada-show-declaration-parents)
349 (define-key map "\C-c\C-e" 'ada-expand)
350 (define-key map "\C-c\C-f" 'ada-show-parse-error)
351 (define-key map "\C-c\C-i" 'ada-indent-statement)
352 (define-key map "\C-c\C-m" 'ada-build-set-make)
353 (define-key map "\C-c\C-n" 'ada-next-statement-keyword)
354 (define-key map "\C-c\C-o" 'ada-find-other-file)
355 (define-key map "\C-c\M-o" 'ada-find-other-file-noset)
356 (define-key map "\C-c\C-p" 'ada-prev-statement-keyword)
357 (define-key map "\C-c\C-q" 'ada-xref-refresh)
358 (define-key map "\C-c\C-r" 'ada-show-references)
359 (define-key map "\C-c\M-r" 'ada-build-run)
360 (define-key map "\C-c\C-v" 'ada-build-check)
361 (define-key map "\C-c\C-w" 'ada-case-adjust-at-point)
362 (define-key map "\C-c\C-x" 'ada-show-overriding)
363 (define-key map "\C-c\M-x" 'ada-show-overridden)
364 (define-key map "\C-c\C-y" 'ada-case-create-exception)
365 (define-key map "\C-c\M-y" 'ada-case-create-partial-exception)
366 (define-key map [C-down-mouse-3] 'ada-popup-menu)
369 ) "Local keymap used for Ada mode.")
371 (defvar ada-mode-menu (make-sparse-keymap "Ada"))
372 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode"
375 ["Ada Mode" (info "ada-mode") t]
376 ["Ada Reference Manual" (info "arm2012") t]
377 ["Key bindings" describe-bindings t]
379 ["Customize" (customize-group 'ada) t]
381 ["Find and select project ..." ada-build-prompt-select-prj-file t]
382 ["Select project ..." ada-prj-select t]
383 ["Show project" ada-prj-show t]
386 ["Next compilation error" next-error t]
387 ["Show secondary error" ada-show-secondary-error t]
388 ["Fix compilation error" ada-fix-compiler-error t]
389 ["Show last parse error" ada-show-parse-error t]
390 ["Check syntax" ada-build-check t]
391 ["Show main" ada-build-show-main t]
392 ["Build" ada-build-make t]
393 ["Set main and Build" ada-build-set-make t]
394 ["Run" ada-build-run t]
397 ["Other file" ada-find-other-file t]
398 ["Other file don't find decl" ada-find-other-file-noset t]
399 ["Goto declaration/body" ada-goto-declaration t]
400 ["Goto next statement keyword" ada-next-statement-keyword t]
401 ["Goto prev statement keyword" ada-next-statement-keyword t]
402 ["Show parent declarations" ada-show-declaration-parents t]
403 ["Show references" ada-show-references t]
404 ["Show overriding" ada-show-overriding t]
405 ["Show overridden" ada-show-overridden t]
408 ["Expand skeleton" ada-expand t]
409 ["Indent line or selection" indent-for-tab-command t]
410 ["Indent current statement" ada-indent-statement t]
411 ["Indent lines in file" (indent-region (point-min) (point-max)) t]
412 ["Align" ada-align t]
413 ["Comment/uncomment selection" comment-dwim t]
414 ["Fill comment paragraph" ada-fill-comment-paragraph t]
415 ["Fill comment paragraph justify" (ada-fill-comment-paragraph 'full) t]
416 ["Fill comment paragraph postfix" (ada-fill-comment-paragraph 'full t) t]
417 ["Make body for subprogram" ada-make-subprogram-body t]
420 ["Create full exception" ada-case-create-exception t]
421 ["Create partial exception" ada-case-create-partial-exception t]
422 ["Adjust case at point" ada-case-adjust-at-point t]
423 ["Adjust case region" ada-case-adjust-region t]
424 ["Adjust case buffer" ada-case-adjust-buffer t]
427 ["Show last parse error" ada-show-parse-error t]
428 ["Refresh cross reference cache" ada-xref-refresh t]
429 ["Reset parser" ada-reset-parser t]
431 (ada-case-activate-keys)
433 ;; This doesn't need to be buffer-local because there can be only one
434 ;; popup menu at a time.
435 (defvar ada-context-menu-on-identifier nil)
437 (easy-menu-define ada-context-menu nil
438 "Context menu keymap for Ada mode"
440 ["Make body for subprogram" ada-make-subprogram-body t]
441 ["Goto declaration/body" ada-goto-declaration :included ada-context-menu-on-identifier]
442 ["Show parent declarations" ada-show-declaration-parents :included ada-context-menu-on-identifier]
443 ["Show references" ada-show-references :included ada-context-menu-on-identifier]
444 ["Show overriding" ada-show-overriding :included ada-context-menu-on-identifier]
445 ["Show overridden" ada-show-overridden :included ada-context-menu-on-identifier]
446 ["Expand skeleton" ada-expand t]
447 ["Create full case exception" ada-case-create-exception t]
448 ["Create partial case exception" ada-case-create-partial-exception t]
451 ["Align" ada-align t]
452 ["Adjust case at point" ada-case-adjust-at-point (not (use-region-p))]
453 ["Adjust case region" ada-case-adjust-region (use-region-p)]
454 ["Indent current statement" ada-indent-statement t]
455 ["Goto next statement keyword" ada-next-statement-keyword t]
456 ["Goto prev statement keyword" ada-next-statement-keyword t]
457 ["Other File" ada-find-other-file t]
458 ["Other file don't find decl" ada-find-other-file-noset t]))
460 (defun ada-popup-menu (position)
461 "Pops up a `ada-context-menu', with `ada-context-menu-on-identifer' set appropriately.
462 POSITION is the location the mouse was clicked on.
463 Sets `ada-context-menu-last-point' to the current position before
464 displaying the menu. When a function from the menu is called,
465 point is where the mouse button was clicked."
468 (mouse-set-point last-input-event)
470 (setq ada-context-menu-on-identifier
472 (or (= (char-syntax (char-after)) ?w)
474 (not (ada-in-string-or-comment-p))
475 (save-excursion (skip-syntax-forward "w")
476 (not (ada-after-keyword-p)))
478 (popup-menu ada-context-menu)
481 (defun ada-indent-newline-indent ()
482 "insert a newline, indent the old and new lines."
484 ;; point may be in the middle of a word, so insert newline first,
485 ;; then go back and indent.
488 (funcall indent-line-function)
490 (funcall indent-line-function))
492 (defvar ada-indent-statement nil
493 ;; indentation function
494 "Function to indent the statement/declaration point is in or after.
495 Function is called with no arguments.")
497 (defun ada-indent-statement ()
498 "Indent current statement."
500 (when ada-indent-statement
501 (funcall ada-indent-statement)))
503 (defvar ada-expand nil
505 "Function to call to expand tokens (ie insert skeletons).")
508 "Expand previous word into a statement skeleton."
511 (funcall ada-expand)))
515 (defvar ada-mode-abbrev-table nil
516 "Local abbrev table for Ada mode.")
518 (defvar ada-align-rules
519 '((ada-declaration-assign
520 (regexp . "[^:]\\(\\s-*\\)\\(:\\)[^:]")
521 (valid . (lambda () (ada-align-valid)))
523 (modes . '(ada-mode)))
525 (regexp . "[^=]\\(\\s-*\\)\\(=>\\)")
526 (valid . (lambda () (ada-align-valid)))
527 (modes . '(ada-mode)))
529 (regexp . "\\(\\s-*\\)--")
530 (valid . (lambda () (ada-align-valid)))
531 (modes . '(ada-mode)))
533 (regexp . "\\(\\s-*\\)\\<\\(use\\s-\\)")
534 (valid . (lambda () (ada-align-valid)))
535 (modes . '(ada-mode)))
537 (regexp . "\\(\\s-+\\)\\(at\\)\\>")
538 (valid . (lambda () (ada-align-valid)))
539 (modes . '(ada-mode))))
540 "Rules to use to align different lines.")
542 (defun ada-align-valid ()
543 "See use in `ada-align-rules'."
545 ;; we don't put "when (match-beginning n)" here; missing a match
546 ;; is a bug in the regexp.
547 (goto-char (or (match-beginning 2) (match-beginning 1)))
548 (not (ada-in-string-or-comment-p))))
550 (defconst ada-align-region-separate
571 "See the variable `align-region-separate' for more information.")
574 "If region is active, apply 'align'. If not, attempt to align
579 (align (region-beginning) (region-end))
582 ;; else see if we are in a construct we know how to align
584 ((ada-in-paramlist-p)
585 (ada-format-paramlist))
591 (defvar ada-in-paramlist-p nil
592 ;; Supplied by indentation engine parser
593 "Function to return t if point is inside the parameter-list of a subprogram declaration.
594 Function is called with no arguments.")
596 (defun ada-in-paramlist-p ()
597 "Return t if point is inside the parameter-list of a subprogram declaration."
598 (when ada-in-paramlist-p
599 (funcall ada-in-paramlist-p)))
601 (defun ada-format-paramlist ()
602 "Reformat the parameter list point is in."
604 (ada-goto-open-paren)
605 (funcall indent-line-function); so new list is indented properly
607 (let* ((inibit-modification-hooks t)
609 (delend (progn (forward-sexp) (point))); just after matching closing paren
610 (end (progn (backward-char) (forward-comment (- (point))) (point))); end of last parameter-declaration
611 (multi-line (> end (save-excursion (goto-char begin) (line-end-position))))
612 (paramlist (ada-scan-paramlist (1+ begin) end)))
615 ;; delete the original parameter-list
616 (delete-region begin delend)
618 ;; insert the new parameter-list
621 (ada-insert-paramlist-multi-line paramlist)
622 (ada-insert-paramlist-single-line paramlist)))
625 (defvar ada-scan-paramlist nil
626 ;; Supplied by indentation engine parser
627 "Function to scan a region, return a list of subprogram parameter declarations (in inverse declaration order).
628 Function is called with two args BEGIN END (the region).
629 Each parameter declaration is represented by a list
630 '((identifier ...) in-p out-p not-null-p access-p constant-p protected-p type default)."
631 ;; mode is 'in | out | in out | [not null] access [constant | protected]'
632 ;; IMPROVEME: handle single-line trailing comments, or longer comments, in paramlist?
635 (defun ada-scan-paramlist (begin end)
636 (when ada-scan-paramlist
637 (funcall ada-scan-paramlist begin end)))
639 (defun ada-insert-paramlist-multi-line (paramlist)
640 "Insert a multi-line formatted PARAMLIST in the buffer."
641 (let ((i (length paramlist))
657 ;; accumulate info across all params
658 (while (not (zerop i))
660 (setq param (nth i paramlist))
665 (mapc (lambda (ident)
667 (setq len (+ len (length ident))))
669 (setq len (+ len (* 2 (1- j)))); space for commas
670 (setq ident-len (max ident-len len))
672 ;; we align the defaults after the types that have defaults, not after all types.
673 ;; "constant", "protected" are treated as part of 'type'
677 (+ (length (nth 7 param))
678 (if (nth 5 param) 10 0); "constant "
679 (if (nth 6 param) 10 0); protected
682 (setq in-p (or in-p (nth 1 param)))
683 (setq out-p (or out-p (nth 2 param)))
684 (setq not-null-p (or not-null-p (nth 3 param)))
685 (setq access-p (or access-p (nth 4 param)))
688 (let ((space-before-p (save-excursion (skip-chars-backward " \t") (not (bolp))))
689 (space-after-p (save-excursion (skip-chars-forward " \t") (not (eolp)))))
691 ;; paramlist starts on same line as subprogram identifier; clean
692 ;; up whitespace. Allow for code on same line as closing paren
693 ;; ('return' or ';').
694 (skip-syntax-forward " ")
695 (delete-char (- (skip-syntax-backward " ")))
706 (setq ident-col (current-column))
707 (setq colon-col (+ ident-col ident-len 1))
708 (setq out-col (+ colon-col (if in-p 5 0))); ": in "
712 (not-null-p 18); ": not null access "
713 (access-p 9); ": access"
714 ((and in-p out-p) 9); ": in out "
719 (setq default-col (+ 1 type-col type-len))
721 (setq i (length paramlist))
722 (while (not (zerop i))
724 (setq param (nth i paramlist))
726 ;; insert identifiers, space and colon
727 (mapc (lambda (ident)
731 (delete-char -2); last ", "
732 (indent-to colon-col)
743 (insert "not null "))
750 (insert "constant "))
752 (insert "protected "))
753 (insert (nth 7 param)); type
755 (when (nth 8 param); default
756 (indent-to default-col)
758 (insert (nth 8 param)))
764 (indent-to ident-col))
768 (defun ada-insert-paramlist-single-line (paramlist)
769 "Insert a single-line formatted PARAMLIST in the buffer."
770 (let ((i (length paramlist))
773 ;; clean up whitespace
774 (skip-syntax-forward " ")
775 (delete-char (- (skip-syntax-backward " ")))
778 (setq i (length paramlist))
779 (while (not (zerop i))
781 (setq param (nth i paramlist))
783 ;; insert identifiers, space and colon
784 (mapc (lambda (ident)
788 (delete-char -2); last ", "
799 (insert "not null "))
805 (insert "constant "))
807 (insert "protected "))
808 (insert (nth 7 param)); type
810 (when (nth 8 param); default
812 (insert (nth 8 param)))
815 (if (= (char-after) ?\;)
822 (defvar ada-reset-parser nil
823 ;; Supplied by indentation engine parser
824 "Function to reset parser, to clear confused state."
827 (defun ada-reset-parser ()
829 (when ada-reset-parser
830 (funcall ada-reset-parser)))
832 (defvar ada-show-parse-error nil
833 ;; Supplied by indentation engine parser
834 "Function to show last error reported by indentation parser."
837 (defun ada-show-parse-error ()
839 (when ada-show-parse-error
840 (funcall ada-show-parse-error)))
844 (defvar ada-case-full-exceptions '()
845 "Alist of words (entities) that have special casing, built from
846 `ada-case-exception-file' full word exceptions. Indexed by
847 properly cased word; value is t.")
849 (defvar ada-case-partial-exceptions '()
850 "Alist of partial words that have special casing, built from
851 `ada-case-exception-file' partial word exceptions. Indexed by
852 properly cased word; value is t.")
854 (defun ada-case-save-exceptions (full-exceptions partial-exceptions file-name)
855 "Save FULL-EXCEPTIONS, PARTIAL-EXCEPTIONS to the file FILE-NAME."
856 (with-temp-file (expand-file-name file-name)
857 (mapc (lambda (x) (insert (car x) "\n"))
858 (sort (copy-sequence full-exceptions)
859 (lambda(a b) (string< (car a) (car b)))))
860 (mapc (lambda (x) (insert "*" (car x) "\n"))
861 (sort (copy-sequence partial-exceptions)
862 (lambda(a b) (string< (car a) (car b)))))
865 (defun ada-case-read-exceptions (file-name)
866 "Read the content of the casing exception file FILE-NAME.
867 Return (cons full-exceptions partial-exceptions)."
868 (setq file-name (expand-file-name (substitute-in-file-name file-name)))
869 (if (file-readable-p file-name)
870 (let (full-exceptions partial-exceptions word)
872 (insert-file-contents file-name)
875 (setq word (buffer-substring-no-properties
876 (point) (save-excursion (skip-syntax-forward "w_") (point))))
878 (if (char-equal (string-to-char word) ?*)
879 ;; partial word exception
881 (setq word (substring word 1))
882 (unless (assoc-string word partial-exceptions t)
883 (add-to-list 'partial-exceptions (cons word t))))
885 ;; full word exception
886 (unless (assoc-string word full-exceptions t)
887 (add-to-list 'full-exceptions (cons word t))))
891 (cons full-exceptions partial-exceptions))
893 ;; else file not readable; might be a new project with no
894 ;; exceptions yet, so just warn user, return empty pair
895 (message "'%s' is not a readable file." file-name)
899 (defun ada-case-merge-exceptions (result new)
900 "Merge NEW exeptions into RESULT.
901 An item in both lists has the RESULT value."
903 (unless (assoc-string (car item) result t)
904 (add-to-list 'result item)))
907 (defun ada-case-merge-all-exceptions (exceptions)
908 "Merge EXCEPTIONS into `ada-case-full-exceptions', `ada-case-partial-exceptions'."
909 (setq ada-case-full-exceptions (ada-case-merge-exceptions ada-case-full-exceptions (car exceptions)))
910 (setq ada-case-partial-exceptions (ada-case-merge-exceptions ada-case-partial-exceptions (cdr exceptions))))
912 (defun ada-case-read-all-exceptions ()
913 "Read case exceptions from all files in `ada-case-exception-file',
914 replacing current values of `ada-case-full-exceptions', `ada-case-partial-exceptions'."
916 (setq ada-case-full-exceptions '()
917 ada-case-partial-exceptions '())
919 (when (ada-prj-get 'casing)
920 (dolist (file (ada-prj-get 'casing))
921 (ada-case-merge-all-exceptions (ada-case-read-exceptions file))))
924 (defun ada-case-add-exception (word exceptions)
925 "Add case exception WORD to EXCEPTIONS, replacing current entry, if any."
926 (if (assoc-string word exceptions t)
927 (setcar (assoc-string word exceptions t) word)
928 (add-to-list 'exceptions (cons word t)))
931 (defun ada-case-create-exception (&optional word file-name partial)
932 "Define WORD as an exception for the casing system, save it in FILE-NAME.
933 If PARTIAL is non-nil, create a partial word exception. WORD
934 defaults to the active region, or the word at point. User is
935 prompted to choose a file from project variable casing if it is a
938 (let ((casing (ada-prj-get 'casing)))
941 (file-name file-name)
943 ((< 1 (length casing))
944 (completing-read "case exception file: " casing
949 (car casing) ;; default
951 ((= 1 (length casing))
955 (if ada-prj-current-file
956 (error "No exception file specified; set `casing' in project file.")
957 ;; IMPROVEME: could prompt, but then need to write to actual project file
960 ;; "No exception file specified; adding to project. file: ")))
961 ;; (message "remember to add %s to project file" temp)
962 ;; (ada-prj-put 'casing temp)
964 (error "No exception file specified, and no project active. See variable `ada-case-exception-file'.")))
969 (setq word (buffer-substring-no-properties (region-beginning) (region-end)))
971 (skip-syntax-backward "w_")
973 (buffer-substring-no-properties
975 (progn (skip-syntax-forward "w_") (point))
978 (let* ((exceptions (ada-case-read-exceptions file-name))
979 (full-exceptions (car exceptions))
980 (partial-exceptions (cdr exceptions)))
984 (setq ada-case-full-exceptions (ada-case-add-exception word ada-case-full-exceptions))
985 (setq full-exceptions (ada-case-add-exception word full-exceptions)))
988 (setq ada-case-partial-exceptions (ada-case-add-exception word ada-case-partial-exceptions))
989 (setq partial-exceptions (ada-case-add-exception word partial-exceptions)))
991 (ada-case-save-exceptions full-exceptions partial-exceptions file-name)
992 (message "created %s case exception '%s' in file '%s'"
993 (if partial "partial" "full")
998 (defun ada-case-create-partial-exception ()
999 "Define active region or word at point as a partial word exception.
1000 User is prompted to choose a file from project variable casing if it is a list."
1002 (ada-case-create-exception nil nil t))
1004 (defun ada-in-numeric-literal-p ()
1005 "Return t if point is after a prefix of a numeric literal."
1006 (looking-back "\\([0-9]+#[0-9a-fA-F_]+\\)"))
1008 (defvar ada-keywords nil
1009 "List of Ada keywords for current `ada-language-version'.")
1011 (defun ada-after-keyword-p ()
1012 "Return non-nil if point is after an element of `ada-keywords'."
1013 (let ((word (buffer-substring-no-properties
1014 (save-excursion (skip-syntax-backward "w_") (point))
1016 (member (downcase word) ada-keywords)))
1018 (defun ada-mixed-case (start end)
1019 "Adjust case of region START END to Mixed_Case."
1023 (downcase-region start end))
1028 (save-excursion (when (search-forward "_" end t) (point-marker)))
1029 (copy-marker (1+ end))))
1031 ;; upcase first char
1032 (insert-char (upcase (following-char)) 1)
1037 (setq start (point))
1041 (defun ada-case-adjust-identifier ()
1042 "Adjust case of the previous word as an identifier.
1043 Uses `ada-case-identifier', with exceptions defined in
1044 `ada-case-full-exceptions', `ada-case-partial-exceptions'."
1047 (let ((end (point-marker))
1048 (start (progn (skip-syntax-backward "w_") (point)))
1053 (if (setq match (assoc-string (buffer-substring-no-properties start end) ada-case-full-exceptions t))
1054 ;; full word exception
1056 ;; 'save-excursion' puts a marker at 'end'; if we do
1057 ;; 'delete-region' first, it moves that marker to 'start',
1058 ;; then 'insert' inserts replacement text after the
1059 ;; marker, defeating 'save-excursion'. So we do 'insert' first.
1060 (insert (car match))
1061 (delete-region (point) end))
1063 ;; else apply ada-case-identifier
1064 (funcall ada-case-identifier start end)
1066 ;; apply partial-exceptions
1071 (save-excursion (when (search-forward "_" end t) (point-marker)))
1072 (copy-marker (1+ end))))
1074 (when (setq match (assoc-string (buffer-substring-no-properties start (1- next))
1075 ada-case-partial-exceptions t))
1076 ;; see comment above at 'full word exception' for why
1077 ;; we do insert first.
1078 (insert (car match))
1079 (delete-region (point) (1- next)))
1083 (setq start (point))
1087 (defun ada-case-adjust (&optional typed-char in-comment)
1088 "Adjust the case of the word before point.
1089 When invoked interactively, TYPED-CHAR must be
1090 `last-command-event', and it must not have been inserted yet.
1091 If IN-COMMENT is non-nil, adjust case of words in comments."
1093 (when (save-excursion
1094 (forward-char -1); back to last character in word
1096 (eq (char-syntax (char-after)) ?w); it can be capitalized
1098 (not (and (eq typed-char ?')
1099 (eq (char-before (point)) ?'))); character literal
1102 (not (ada-in-string-or-comment-p)))
1103 ;; we sometimes want to capitialize an Ada identifier
1104 ;; referenced in a comment, via
1105 ;; ada-case-adjust-at-point.
1107 (not (ada-in-numeric-literal-p))
1110 ;; The indentation engine may trigger a reparse on
1111 ;; non-whitespace changes, but we know we don't need to reparse
1112 ;; for this change (assuming the user has not abused case
1114 (let ((inhibit-modification-hooks t))
1116 ;; Some attributes are also keywords, but captialized as
1117 ;; attributes. So check for attribute first.
1121 (skip-syntax-backward "w_")
1122 (eq (char-before) ?')))
1123 (ada-case-adjust-identifier))
1127 (not (eq typed-char ?_))
1128 (ada-after-keyword-p))
1129 (funcall ada-case-keyword -1))
1131 (t (ada-case-adjust-identifier))
1135 (defun ada-case-adjust-at-point (&optional in-comment)
1136 "Adjust case of word at point, move to end of word.
1137 With prefix arg, adjust case even if in comment."
1141 ;; we use '(syntax-after (point))' here, not '(char-syntax
1142 ;; (char-after))', because the latter does not respect
1143 ;; ada-syntax-propertize.
1144 (memq (syntax-class (syntax-after (point))) '(2 3)))
1145 (skip-syntax-forward "w_"))
1146 (ada-case-adjust nil in-comment))
1148 (defun ada-case-adjust-region (begin end)
1149 "Adjust case of all words in region BEGIN END."
1151 (narrow-to-region begin end)
1155 (forward-comment (point-max))
1156 (skip-syntax-forward "^w_")
1157 (skip-syntax-forward "w_")
1161 (defun ada-case-adjust-buffer ()
1162 "Adjust case of current buffer."
1164 (ada-case-adjust-region (point-min) (point-max)))
1166 (defun ada-case-adjust-interactive (arg)
1167 "If `ada-auto-case' is non-nil, adjust the case of the previous word, and process the character just typed.
1168 To be bound to keys that should cause auto-casing.
1169 ARG is the prefix the user entered with \\[universal-argument]."
1172 ;; character typed has not been inserted yet
1173 (let ((lastk last-command-event))
1178 (ada-case-adjust lastk))
1179 (funcall ada-lfd-binding))
1181 ((memq lastk '(?\r return))
1183 (ada-case-adjust lastk))
1184 (funcall ada-ret-binding))
1188 (ada-case-adjust lastk))
1189 (self-insert-command (prefix-numeric-value arg)))
1194 ;; An Emacs Ada mode project file can specify several things:
1196 ;; - a compiler-specific project file
1198 ;; - compiler-specific environment variables
1200 ;; - other compiler-specific things (see the compiler support elisp code)
1202 ;; - a list of source directories (in addition to those specified in the compiler project file)
1204 ;; - a casing exception file
1206 ;; All of the data used by Emacs Ada mode functions specified in a
1207 ;; project file is stored in a property list. The property list is
1208 ;; stored in an alist indexed by the project file name, so multiple
1209 ;; project files can be selected without re-parsing them (some
1210 ;; compiler project files can take a long time to parse).
1212 (defvar ada-prj-alist nil
1213 "Alist holding currently parsed Emacs Ada project files. Indexed by absolute project file name.")
1215 (defvar ada-prj-current-file nil
1216 "Current Emacs Ada project file.")
1218 (defvar ada-prj-current-project nil
1219 "Current Emacs Ada mode project; a plist.")
1221 (defun ada-prj-get (prop &optional plist)
1222 "Return value of PROP in PLIST.
1223 Optional PLIST defaults to `ada-prj-current-project'."
1224 (let ((prj (or plist ada-prj-current-project)))
1226 (plist-get prj prop)
1228 ;; no project, just use default vars
1229 ;; must match code in ada-prj-default
1231 (ada_compiler ada-compiler)
1232 (auto_case ada-auto-case)
1233 (case_keyword ada-case-keyword)
1234 (case_identifier ada-case-identifier)
1235 (case_strict ada-case-strict)
1236 (casing (if (listp ada-case-exception-file)
1237 ada-case-exception-file
1238 (list ada-case-exception-file)))
1239 (path_sep path-separator)
1240 (proc_env process-environment)
1241 (src_dir (list "."))
1242 (xref_tool ada-xref-tool)
1245 (defun ada-prj-put (prop val &optional plist)
1246 "Set value of PROP in PLIST to VAL.
1247 Optional PLIST defaults to `ada-prj-current-project'."
1248 (plist-put (or plist ada-prj-current-project) prop val))
1250 (defun ada-require-project-file ()
1251 (unless ada-prj-current-file
1252 (error "no Emacs Ada project file specified")))
1254 (defvar ada-prj-default-list nil
1255 ;; project file parse
1256 "List of functions to add default project variables. Called
1257 with one argument; the default project properties list. Function
1258 should add to the properties list and return it.")
1260 (defvar ada-prj-default-compiler-alist nil
1261 ;; project file parse
1262 "Compiler-specific function to set default project variables.
1263 Indexed by ada-compiler. Called with one argument; the default
1264 project properties list. Function should add to the properties
1265 list and return it.")
1267 (defvar ada-prj-default-xref-alist nil
1268 ;; project file parse
1269 "Xref-tool-specific function to set default project variables.
1270 Indexed by ada-xref-tool. Called with one argument; the default
1271 project properties list. Function should add to the properties
1272 list and return it.")
1274 (defun ada-prj-default ()
1275 "Return the default project properties list.
1276 Include properties set via `ada-prj-default-compiler-alist',
1277 `ada-prj-default-xref-alist'."
1283 ;; variable name alphabetical order
1284 'ada_compiler ada-compiler
1285 'auto_case ada-auto-case
1286 'case_keyword ada-case-keyword
1287 'case_identifier ada-case-identifier
1288 'case_strict ada-case-strict
1289 'casing (if (listp ada-case-exception-file)
1290 ada-case-exception-file
1291 (list ada-case-exception-file))
1292 'path_sep path-separator;; prj variable so users can override it for their compiler
1293 'proc_env process-environment
1295 'xref_tool ada-xref-tool
1298 (cl-dolist (func ada-prj-default-list)
1299 (setq project (funcall func project)))
1301 (setq func (cdr (assq ada-compiler ada-prj-default-compiler-alist)))
1302 (when func (setq project (funcall func project)))
1303 (setq func (cdr (assq ada-xref-tool ada-prj-default-xref-alist)))
1304 (when func (setq project (funcall func project)))
1307 (defvar ada-prj-parser-alist
1309 (lambda (ext) (cons ext 'ada-prj-parse-file-1))
1310 ada-prj-file-extensions)
1311 ;; project file parse
1312 "Alist of parsers for project files, indexed by file extension.
1313 Default provides the minimal Ada mode parser; compiler support
1314 code may add other parsers. Parser is called with two arguments;
1315 the project file name and the current project property
1316 list. Parser must modify or add to the property list and return it.")
1318 ;; This autoloaded because it is often used in Makefiles, and thus
1319 ;; will be the first ada-mode function executed.
1321 (defun ada-parse-prj-file (prj-file)
1322 "Read Emacs Ada or compiler-specific project file PRJ-FILE, set project properties in `ada-prj-alist'."
1323 ;; Not called ada-prj-parse-file for Ada mode 4.01 compatibility
1324 (let ((project (ada-prj-default))
1325 (parser (cdr (assoc (file-name-extension prj-file) ada-prj-parser-alist))))
1327 (setq prj-file (expand-file-name prj-file))
1329 (unless (file-readable-p prj-file)
1330 (error "Project file '%s' is not readable" prj-file))
1333 ;; parser may reference the "current project", so bind that now.
1334 (let ((ada-prj-current-project project)
1335 (ada-prj-current-file prj-file))
1336 (setq project (funcall parser prj-file project)))
1337 (error "no project file parser defined for '%s'" prj-file))
1339 ;; Store the project properties
1340 (if (assoc prj-file ada-prj-alist)
1341 (setcdr (assoc prj-file ada-prj-alist) project)
1342 (add-to-list 'ada-prj-alist (cons prj-file project)))
1344 ;; return t for interactive use
1347 (defun ada-prj-reparse-select-current ()
1348 "Reparse the current project file, re-select it.
1349 Useful when the project file has been edited."
1350 (ada-parse-prj-file ada-prj-current-file)
1351 (ada-select-prj-file ada-prj-current-file))
1353 (defvar ada-prj-parse-one-compiler nil
1354 ;; project file parse
1355 "Compiler-specific function to process one Ada project property.
1356 Indexed by project variable ada_compiler.
1357 Called with three arguments; the property name, property value,
1358 and project properties list. Function should add to or modify the
1359 properties list and return it, or return nil if the name is not
1362 (defvar ada-prj-parse-one-xref nil
1363 ;; project file parse
1364 "Xref-tool-specific function to process one Ada project property.
1365 Indexed by project variable xref_tool.
1366 Called with three arguments; the property name, property value,
1367 and project properties list. Function should add to or modify the
1368 properties list and return it, or return nil if the name is not
1371 (defvar ada-prj-parse-final-compiler nil
1372 ;; project file parse
1373 "Alist of compiler-specific functions to finish processing Ada project properties.
1374 Indexed by project variable ada_compiler.
1375 Called with one argument; the project properties list. Function
1376 should add to or modify the list and return it.")
1378 (defvar ada-prj-parse-final-xref nil
1379 ;; project file parse
1380 "Alist of xref-tool-specific functions to finish processing Ada project properties.
1381 Indexed by project variable xref_tool.
1382 Called with one argument; the project properties list. Function
1383 should add to or modify the list and return it.")
1385 (defun ada-prj-parse-file-1 (prj-file project)
1386 "Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
1387 Return new value of PROJECT."
1388 (let (;; fields that are lists or that otherwise require special processing
1391 (parse-one-compiler (cdr (assoc ada-compiler ada-prj-parse-one-compiler)))
1392 (parse-final-compiler (cdr (assoc ada-compiler ada-prj-parse-final-compiler)))
1393 (parse-one-xref (cdr (assoc ada-xref-tool ada-prj-parse-one-xref)))
1394 (parse-final-xref (cdr (assoc ada-xref-tool ada-prj-parse-final-xref))))
1396 (with-current-buffer (find-file-noselect prj-file)
1397 (goto-char (point-min))
1399 ;; process each line
1402 ;; ignore lines that don't have the format "name=value", put
1403 ;; 'name', 'value' in match-string.
1404 (when (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
1406 ;; variable name alphabetical order
1408 ((string= (match-string 1) "ada_compiler")
1409 (let ((comp (intern (match-string 2))))
1410 (setq project (plist-put project 'ada_compiler comp))
1411 (setq parse-one-compiler (cdr (assq comp ada-prj-parse-one-compiler)))
1412 (setq parse-final-compiler (cdr (assq comp ada-prj-parse-final-compiler)))))
1414 ((string= (match-string 1) "auto_case")
1415 (setq project (plist-put project 'auto_case (intern (match-string 2)))))
1417 ((string= (match-string 1) "case_keyword")
1418 (setq project (plist-put project 'case_keyword (intern (match-string 2)))))
1420 ((string= (match-string 1) "case_identifier")
1421 (setq project (plist-put project 'case_identifier (intern (match-string 2)))))
1423 ((string= (match-string 1) "case_strict")
1424 (setq project (plist-put project 'case_strict (intern (match-string 2)))))
1426 ((string= (match-string 1) "casing")
1427 (add-to-list 'casing
1429 (substitute-in-file-name (match-string 2)))))
1431 ((string= (match-string 1) "el_file")
1432 (let ((file (expand-file-name (substitute-in-file-name (match-string 2)))))
1433 (setq project (plist-put project 'el_file file))
1434 ;; eval now as well as in select, since it might affect parsing
1437 ((string= (match-string 1) "src_dir")
1438 (add-to-list 'src_dir
1439 (file-name-as-directory
1440 (expand-file-name (match-string 2)))))
1442 ((string= (match-string 1) "xref_tool")
1443 (let ((xref (intern (match-string 2))))
1444 (setq project (plist-put project 'xref_tool xref))
1445 (setq parse-one-xref (cdr (assq xref ada-prj-parse-one-xref)))
1446 (setq parse-final-xref (cdr (assq xref ada-prj-parse-final-xref)))))
1450 (and parse-one-compiler
1451 (setq tmp-prj (funcall parse-one-compiler (match-string 1) (match-string 2) project)))
1453 (setq tmp-prj (funcall parse-one-xref (match-string 1) (match-string 2) project))))
1455 (setq project tmp-prj)
1457 ;; Any other field in the file is set as an environment
1458 ;; variable or a project file.
1459 (if (= ?$ (elt (match-string 1) 0))
1460 ;; process env var. We don't do expand-file-name
1461 ;; here because the application may be expecting a
1463 (let ((process-environment (plist-get project 'proc_env)))
1464 (setenv (substring (match-string 1) 1)
1465 (substitute-in-file-name (match-string 2)))
1467 (plist-put project 'proc_env process-environment)))
1469 ;; not recognized; assume it is a user-defined variable like "comp_opt"
1470 (setq project (plist-put project (intern (match-string 1)) (match-string 2)))
1476 );; done reading file
1478 ;; process accumulated lists
1479 (if casing (set 'project (plist-put project 'casing (reverse casing))))
1480 (if src_dir (set 'project (plist-put project 'src_dir (reverse src_dir))))
1482 (when parse-final-compiler
1483 ;; parse-final-compiler may reference the "current project", so
1484 ;; bind that now, to include the properties set above.
1485 (let ((ada-prj-current-project project)
1486 (ada-prj-current-file prj-file))
1487 (setq project (funcall parse-final-compiler project))))
1489 (when parse-final-xref
1490 (let ((ada-prj-current-project project)
1491 (ada-prj-current-file prj-file))
1492 (setq project (funcall parse-final-xref project))))
1497 (defvar ada-project-search-path nil
1498 "Search path for finding Ada project files")
1500 (defvar ada-select-prj-compiler nil
1501 "Alist of functions to call for compiler specific project file selection.
1502 Indexed by project variable ada_compiler.")
1504 (defvar ada-deselect-prj-compiler nil
1505 "Alist of functions to call for compiler specific project file deselection.
1506 Indexed by project variable ada_compiler.")
1508 (defvar ada-select-prj-xref-tool nil
1509 "Alist of functions to call for xref-tool specific project file selection.
1510 Indexed by project variable xref_tool.")
1512 (defvar ada-deselect-prj-xref-tool nil
1513 "Alist of functions to call for xref-tool specific project file deselection.
1514 Indexed by project variable xref_tool.")
1516 (defun ada-select-prj-file (prj-file)
1517 "Select PRJ-FILE as the current project file."
1519 (setq prj-file (expand-file-name prj-file))
1521 (setq ada-prj-current-project (cdr (assoc prj-file ada-prj-alist)))
1523 (when (null ada-prj-current-project)
1524 (setq ada-prj-current-file nil)
1525 (error "Project file '%s' was not previously parsed." prj-file))
1527 (let ((func (cdr (assq (ada-prj-get 'ada_compiler) ada-deselect-prj-compiler))))
1528 (when func (funcall func)))
1530 (let ((func (cdr (assq (ada-prj-get 'xref_tool) ada-deselect-prj-xref-tool))))
1531 (when func (funcall func)))
1533 (setq ada-prj-current-file prj-file)
1535 ;; Project file should fully specify what compilers are used,
1536 ;; including what compilation filters they need. There may be more
1537 ;; than just an Ada compiler.
1538 (setq compilation-error-regexp-alist nil)
1539 (setq compilation-filter-hook nil)
1541 (when (ada-prj-get 'el_file)
1542 (load-file (ada-prj-get 'el_file)))
1544 (ada-case-read-all-exceptions)
1546 (setq compilation-search-path (ada-prj-get 'src_dir))
1547 (setq ada-project-search-path (ada-prj-get 'prj_dir))
1549 (let ((func (cdr (assq (ada-prj-get 'ada_compiler) ada-select-prj-compiler))))
1550 (when func (funcall func)))
1552 (let ((func (cdr (assq (ada-prj-get 'xref_tool) ada-select-prj-xref-tool))))
1553 (when func (funcall func)))
1555 ;; return 't', for decent display in message buffer when called interactively
1558 (defun ada-prj-select ()
1559 "Select the current project file from the list of currently available project files."
1561 (ada-select-prj-file (completing-read "project: " ada-prj-alist nil t))
1564 (defun ada-prj-show ()
1565 "Show current Emacs Ada mode project file."
1567 (message "current Emacs Ada mode project file: %s" ada-prj-current-file))
1569 ;;;; syntax properties
1571 (defvar ada-mode-syntax-table
1572 (let ((table (make-syntax-table)))
1573 ;; (info "(elisp)Syntax Class Table" "*info syntax class table*")
1574 ;; make-syntax-table sets all alphanumeric to w, etc; so we only
1575 ;; have to add ada-specific things.
1577 ;; string brackets. `%' is the obsolete alternative string
1578 ;; bracket (arm J.2); if we make it syntax class ", it throws
1579 ;; font-lock and indentation off the track, so we use syntax class
1581 (modify-syntax-entry ?% "$" table)
1582 (modify-syntax-entry ?\" "\"" table)
1584 ;; punctuation; operators etc
1585 (modify-syntax-entry ?# "w" table); based number - word syntax, since we don't need the number
1586 (modify-syntax-entry ?& "." table)
1587 (modify-syntax-entry ?* "." table)
1588 (modify-syntax-entry ?+ "." table)
1589 (modify-syntax-entry ?- ". 12" table); operator; see ada-syntax-propertize for double hyphen as comment
1590 (modify-syntax-entry ?. "." table)
1591 (modify-syntax-entry ?/ "." table)
1592 (modify-syntax-entry ?: "." table)
1593 (modify-syntax-entry ?< "." table)
1594 (modify-syntax-entry ?= "." table)
1595 (modify-syntax-entry ?> "." table)
1596 (modify-syntax-entry ?\' "." table); attribute; see ada-syntax-propertize for character literal
1597 (modify-syntax-entry ?\; "." table)
1598 (modify-syntax-entry ?\\ "." table); default is escape; not correct for Ada strings
1599 (modify-syntax-entry ?\| "." table)
1601 ;; and \f and \n end a comment
1602 (modify-syntax-entry ?\f ">" table)
1603 (modify-syntax-entry ?\n ">" table)
1605 (modify-syntax-entry ?_ "_" table); symbol constituents, not word.
1607 (modify-syntax-entry ?\( "()" table)
1608 (modify-syntax-entry ?\) ")(" table)
1610 ;; skeleton placeholder delimiters; see ada-skel.el. We use generic
1611 ;; comment delimiter class, not comment starter/comment ender, so
1612 ;; these can be distinguished from line end.
1613 (modify-syntax-entry ?{ "!" table)
1614 (modify-syntax-entry ?} "!" table)
1618 "Syntax table to be used for editing Ada source code.")
1620 (defvar ada-syntax-propertize-hook nil
1621 ;; provided by preprocessor, lumped with xref-tool
1622 "Hook run from `ada-syntax-propertize'.
1623 Called by `syntax-propertize', which is called by font-lock in
1624 `after-change-functions'. Therefore, care must be taken to avoid
1625 race conditions with the grammar parser.")
1627 (defun ada-syntax-propertize (start end)
1628 "Assign `syntax-table' properties in accessible part of buffer.
1629 In particular, character constants are set to have string syntax."
1630 ;; (info "(elisp)Syntax Properties")
1632 ;; called from `syntax-propertize', inside save-excursion with-silent-modifications
1633 (let ((inhibit-read-only t)
1634 (inhibit-point-motion-hooks t))
1637 (while (re-search-forward
1639 "[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"; 1, 2: character literal, not attribute
1640 "\\|[^a-zA-Z0-9)]\\('''\\)"; 3: character literal '''
1641 "\\|\\(--\\)"; 4: comment start
1644 ;; The help for syntax-propertize-extend-region-functions
1645 ;; implies that 'start end' will always include whole lines, in
1646 ;; which case we don't need
1647 ;; syntax-propertize-extend-region-functions
1649 ((match-beginning 1)
1651 (match-beginning 1) (match-end 1) 'syntax-table '(7 . ?'))
1653 (match-beginning 2) (match-end 2) 'syntax-table '(7 . ?')))
1654 ((match-beginning 3)
1656 (match-beginning 3) (1+ (match-beginning 3)) 'syntax-table '(7 . ?'))
1658 (1- (match-end 3)) (match-end 3) 'syntax-table '(7 . ?')))
1659 ((match-beginning 4)
1661 (match-beginning 4) (match-end 4) 'syntax-table '(11 . nil)))
1663 (run-hook-with-args 'ada-syntax-propertize-hook start end))
1666 (defun ada-in-comment-p (&optional parse-result)
1667 "Return t if inside a comment.
1668 If PARSE-RESULT is non-nil, use it instead of calling `syntax-ppss'."
1669 (nth 4 (or parse-result (syntax-ppss))))
1671 (defun ada-in-string-p (&optional parse-result)
1672 "Return t if point is inside a string.
1673 If PARSE-RESULT is non-nil, use it instead of calling `syntax-ppss'."
1674 (nth 3 (or parse-result (syntax-ppss))))
1676 (defun ada-in-string-or-comment-p (&optional parse-result)
1677 "Return t if inside a comment or string.
1678 If PARSE-RESULT is non-nil, use it instead of calling `syntax-ppss'."
1679 (setq parse-result (or parse-result (syntax-ppss)))
1680 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
1682 (defun ada-in-paren-p (&optional parse-result)
1683 "Return t if point is inside a pair of parentheses.
1684 If PARSE-RESULT is non-nil, use it instead of calling `syntax-ppss'."
1685 (> (nth 0 (or parse-result (syntax-ppss))) 0))
1687 (defun ada-goto-open-paren (&optional offset parse-result)
1688 "Move point to innermost opening paren surrounding current point, plus OFFSET.
1689 Throw error if not in paren. If PARSE-RESULT is non-nil, use it
1690 instead of calling `syntax-ppss'."
1691 (goto-char (+ (or offset 0) (nth 1 (or parse-result (syntax-ppss))))))
1693 ;;;; navigation within and between files
1695 (defvar ada-body-suffixes '(".adb")
1696 "List of possible suffixes for Ada body files.
1697 The extensions should include a `.' if needed.")
1699 (defvar ada-spec-suffixes '(".ads")
1700 "List of possible suffixes for Ada spec files.
1701 The extensions should include a `.' if needed.")
1703 (defvar ada-other-file-alist
1704 '(("\\.ads$" (".adb"))
1705 ("\\.adb$" (".ads")))
1706 "Alist used by `find-file' to find the name of the other package.
1707 See `ff-other-file-alist'.")
1709 (defconst ada-name-regexp
1710 "\\(\\(?:\\sw\\|[_.]\\)+\\)")
1712 (defconst ada-parent-name-regexp
1713 "\\([a-zA-Z0-9_\\.]+\\)\\.[a-zA-Z0-9_]+"
1714 "Regexp for extracting the parent name from fully-qualified name.")
1716 (defvar ada-file-name-from-ada-name nil
1717 ;; determined by ada-xref-tool, set by *-select-prj
1718 "Function called with one parameter ADA-NAME, which is a library
1719 unit name; it should return the filename in which ADA-NAME is
1722 (defun ada-file-name-from-ada-name (ada-name)
1723 "Return the filename in which ADA-NAME is found."
1724 (funcall ada-file-name-from-ada-name ada-name))
1726 (defvar ada-ada-name-from-file-name nil
1727 ;; depends on ada-compiler, per-project
1728 "Function called with one parameter FILE-NAME, which is a library
1729 unit name; it should return the Ada name that should be found in FILE-NAME.")
1731 (defun ada-ada-name-from-file-name (file-name)
1732 "Return the ada-name that should be found in FILE-NAME."
1733 (funcall ada-ada-name-from-file-name file-name))
1735 (defun ada-ff-special-extract-parent ()
1736 (setq ff-function-name (match-string 1))
1737 (file-name-nondirectory
1740 compilation-search-path
1741 (ada-file-name-from-ada-name ff-function-name)
1743 (error "parent '%s' not found; set project file?" ff-function-name))))
1745 (defun ada-ff-special-extract-separate ()
1746 (let ((package-name (match-string 1)))
1748 (goto-char (match-end 0))
1749 (when (eolp) (forward-char 1))
1750 (skip-syntax-forward " ")
1752 (concat "\\(function\\|package body\\|procedure\\|protected body\\|task body\\)\\s +"
1754 (setq ff-function-name (match-string 0))
1756 (file-name-nondirectory
1759 compilation-search-path
1760 (ada-file-name-from-ada-name package-name)
1762 (error "package '%s' not found; set project file?" package-name)))))
1764 (defun ada-ff-special-with ()
1765 (let ((package-name (match-string 1)))
1766 (setq ff-function-name (concat "^package\\s-+" package-name "\\([^_]\\|$\\)"))
1767 (file-name-nondirectory
1770 compilation-search-path
1771 (ada-file-name-from-ada-name package-name)
1772 (append ada-spec-suffixes ada-body-suffixes))
1773 (error "package '%s' not found; set project file?" package-name)))
1776 (defun ada-set-ff-special-constructs ()
1777 "Add Ada-specific pairs to `ff-special-constructs'."
1778 (set (make-local-variable 'ff-special-constructs) nil)
1779 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair))
1780 ;; Each car is a regexp; if it matches at point, the cdr is invoked.
1781 ;; Each cdr should set ff-function-name to a string or regexp
1782 ;; for ada-set-point-accordingly, and return the file name
1783 ;; (sans directory, must include suffix) to go to.
1785 ;; Top level child package declaration (not body), or child
1786 ;; subprogram declaration or body; go to the parent package.
1787 (cons (concat "^\\(?:private[ \t]+\\)?\\(?:package\\|procedure\\|function\\)[ \t]+"
1788 ada-parent-name-regexp "\\(?:;\\|[ \t]+\\|$\\)")
1789 'ada-ff-special-extract-parent)
1791 ;; A "separate" clause.
1792 (cons (concat "^separate[ \t\n]*(" ada-name-regexp ")")
1793 'ada-ff-special-extract-separate)
1795 ;; A "with" clause. Note that it may refer to a procedure body, as well as a spec
1796 (cons (concat "^\\(?:limited[ \t]+\\)?\\(?:private[ \t]+\\)?with[ \t]+" ada-name-regexp)
1797 'ada-ff-special-with)
1800 (defvar ada-which-function nil
1801 ;; supplied by indentation engine
1803 ;; This is run from ff-pre-load-hook, so ff-function-name may have
1804 ;; been set by ff-treat-special; don't reset it.
1805 "Function called with no parameters; it should return the name
1806 of the package, protected type, subprogram, or task type whose
1807 definition/declaration point is in or just after, or nil. In
1808 addition, if ff-function-name is non-nil, store in
1809 ff-function-name a regexp that will find the function in the
1812 (defun ada-which-function ()
1813 "See `ada-which-function' variable."
1815 (when ada-which-function
1816 (funcall ada-which-function)))
1818 (defun ada-add-log-current-function ()
1819 "For `add-log-current-defun-function'; uses `ada-which-function'."
1820 ;; add-log-current-defun is typically called with point at the start
1821 ;; of an ediff change section, which is before the start of the
1822 ;; declaration of a new item. So go to the end of the current line
1823 ;; first, then call `ada-which-function'
1826 (ada-which-function)))
1828 (defun ada-set-point-accordingly ()
1829 "Move to the string specified in `ff-function-name', which may be a regexp,
1830 previously set by a file navigation command."
1831 (when ff-function-name
1834 (goto-char (point-min))
1835 ;; We are looking for an Ada declaration, so don't stop for strings or comments
1837 ;; This will still be confused by multiple references; we need
1838 ;; to use compiler cross reference info for more precision.
1840 (if (search-forward-regexp ff-function-name nil t)
1841 (setq found (match-beginning 0))
1842 ;; not in remainder of buffer
1844 (if (ada-in-string-or-comment-p)
1849 ;; different parsers find different points on the line; normalize here
1850 (back-to-indentation))
1851 (setq ff-function-name nil))))
1853 (defun ada-find-other-file-noset (other-window)
1854 "Same as `ada-find-other-file', but preserve point in the other file,
1855 don't move to corresponding declaration."
1857 (ada-find-other-file other-window t))
1859 (defun ada-find-other-file (other-window &optional no-set-point)
1860 "Move to the corresponding declaration in another file.
1862 - If region is active, assume it contains a package name;
1863 position point on that package declaration.
1865 - If point is in the start line of a non-nested child package or
1866 subprogram declaration, position point on the corresponding
1867 parent package specification.
1869 - If point is in the start line of a separate body,
1870 position point on the corresponding separate stub declaration.
1872 - If point is in a context clause line, position point on the
1873 first package declaration that is mentioned.
1875 - If point is in a subprogram body or specification, position point
1876 on the corresponding specification or body.
1878 If OTHER-WINDOW (set by interactive prefix) is non-nil, show the
1879 buffer in another window.
1881 If NO-SET-POINT is nil, set point in the other file on the
1882 corresponding declaration. If non-nil, preserve existing point in
1885 ;; ff-get-file, ff-find-other file first process
1886 ;; ff-special-constructs, then run the following hooks:
1888 ;; ff-pre-load-hook set to ada-which-function
1889 ;; ff-file-created-hook set to ada-ff-create-body
1890 ;; ff-post-load-hook set to ada-set-point-accordingly,
1891 ;; or to a compiler-specific function that
1892 ;; uses compiler-generated cross reference
1896 (when (null (car compilation-search-path))
1897 (error "no file search path defined; set project file?"))
1901 (setq ff-function-name (buffer-substring-no-properties (point) (mark)))
1903 compilation-search-path
1904 (ada-file-name-from-ada-name ff-function-name)
1909 ;; else use name at point
1910 (ff-find-other-file other-window)))
1912 (defvar ada-operator-re
1913 "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
1914 "Regexp matching Ada operator_symbol.")
1916 (defun ada-identifier-at-point ()
1917 "Return the identifier around point, move point to start of
1918 identifier. May be an Ada identifier or operator function name."
1920 (when (ada-in-comment-p)
1921 (error "Inside comment"))
1925 (skip-chars-backward "a-zA-Z0-9_<>=+\\-\\*/&")
1927 ;; Just in front of, or inside, a string => we could have an operator
1932 ((and (= (char-before) ?\")
1935 (looking-at (concat "\"\\(" ada-operator-re "\\)\""))))
1936 (setq identifier (concat "\"" (match-string-no-properties 1) "\"")))
1939 (error "Inside string or character constant"))
1942 ((and (= (char-after) ?\")
1943 (looking-at (concat "\"\\(" ada-operator-re "\\)\"")))
1944 (setq identifier (concat "\"" (match-string-no-properties 1) "\"")))
1946 ((looking-at "[a-zA-Z0-9_]+")
1947 (setq identifier (match-string-no-properties 0)))
1950 (error "No identifier around"))
1953 (defun ada-goto-source (file line column other-window)
1954 "Find and select FILE, at LINE and COLUMN.
1955 FILE may be absolute, or on `compilation-search-path'.
1957 If OTHER-WINDOW is non-nil, show the buffer in another window."
1959 (if (file-name-absolute-p file) file
1960 (ff-get-file-name compilation-search-path file))))
1963 (error "File %s not found; installed library, or set project?" file))
1966 (let ((buffer (get-file-buffer file)))
1970 ((null other-window)
1971 (switch-to-buffer buffer))
1973 (t (switch-to-buffer-other-window buffer))
1976 ((file-exists-p file)
1978 ((null other-window)
1982 (find-file-other-window file))
1986 (error "'%s' not found" file))))
1989 ;; move the cursor to the correct position
1991 (goto-char (point-min))
1992 (forward-line (1- line))
1993 (forward-char column)
1996 (defvar ada-xref-refresh-function nil
1997 ;; determined by xref_tool, set by *-select-prj-xref
1998 "Function that refreshes cross reference information cache.")
2000 (defun ada-xref-refresh ()
2001 "Refresh cross reference information cache, if any."
2004 (when (null ada-xref-refresh-function)
2005 (error "no cross reference information available"))
2007 (funcall ada-xref-refresh-function)
2010 (defvar ada-xref-other-function nil
2011 ;; determined by xref_tool, set by *-select-prj-xref
2012 "Function that returns cross reference information.
2013 Function is called with four arguments:
2014 - an Ada identifier or operator_symbol
2015 - filename containing the identifier (full path)
2016 - line number containing the identifier
2017 - column of the start of the identifier
2018 Returns a list '(file line column) giving the corresponding location.
2019 'file' may be absolute, or on `compilation-search-path'. If point is
2020 at the specification, the corresponding location is the body, and vice
2023 (defun ada-goto-declaration (other-window)
2024 "Move to the declaration or body of the identifier around point.
2025 If at the declaration, go to the body, and vice versa.
2027 If OTHER-WINDOW (set by interactive prefix) is non-nil, show the
2028 buffer in another window."
2031 (when (null ada-xref-other-function)
2032 (error "no cross reference information available"))
2035 (funcall ada-xref-other-function
2036 (ada-identifier-at-point)
2038 (line-number-at-pos)
2039 (1+ (current-column))
2042 (ada-goto-source (nth 0 target)
2048 (defvar ada-xref-parent-function nil
2049 ;; determined by xref_tool, set by *-select-prj-xref
2050 "Function that returns cross reference information.
2051 Function is called with four arguments:
2052 - an Ada identifier or operator_symbol
2053 - filename containing the identifier
2054 - line number containing the identifier
2055 - column of the start of the identifier
2056 Displays a buffer in compilation-mode giving locations of the parent type declarations.")
2058 (defun ada-show-declaration-parents ()
2059 "Display the locations of the parent type declarations of the type identifier around point."
2061 (when (null ada-xref-parent-function)
2062 (error "no cross reference information available"))
2064 (funcall ada-xref-parent-function
2065 (ada-identifier-at-point)
2066 (file-name-nondirectory (buffer-file-name))
2067 (line-number-at-pos)
2068 (1+ (current-column)))
2071 (defvar ada-xref-all-function nil
2072 ;; determined by xref_tool, set by *-select-prj-xref
2073 "Function that displays cross reference information.
2074 Called with four arguments:
2075 - an Ada identifier or operator_symbol
2076 - filename containing the identifier
2077 - line number containing the identifier
2078 - column of the start of the identifier
2079 Displays a buffer in compilation-mode giving locations where the
2080 identifier is declared or referenced.")
2082 (defun ada-show-references ()
2083 "Show all references of identifier at point."
2086 (when (null ada-xref-all-function)
2087 (error "no cross reference information available"))
2089 (funcall ada-xref-all-function
2090 (ada-identifier-at-point)
2091 (file-name-nondirectory (buffer-file-name))
2092 (line-number-at-pos)
2093 (1+ (current-column)))
2096 (defvar ada-xref-overriding-function nil
2097 ;; determined by ada-xref-tool, set by *-select-prj
2098 "Function that displays cross reference information for overriding subprograms.
2099 Called with four arguments:
2100 - an Ada identifier or operator_symbol
2101 - filename containing the identifier
2102 - line number containing the identifier
2103 - column of the start of the identifier
2104 Displays a buffer in compilation-mode giving locations of the overriding declarations.")
2106 (defun ada-show-overriding ()
2107 "Show all overridings of identifier at point."
2110 (when (null ada-xref-overriding-function)
2111 (error "no cross reference information available"))
2113 (funcall ada-xref-overriding-function
2114 (ada-identifier-at-point)
2115 (file-name-nondirectory (buffer-file-name))
2116 (line-number-at-pos)
2117 (1+ (current-column)))
2120 (defvar ada-xref-overridden-function nil
2121 ;; determined by ada-xref-tool, set by *-select-prj
2122 "Function that displays cross reference information for overridden subprogram.
2123 Called with four arguments:
2124 - an Ada identifier or operator_symbol
2125 - filename containing the identifier
2126 - line number containing the identifier
2127 - column of the start of the identifier
2128 Returns a list '(file line column) giving the corresponding location.
2129 'file' may be absolute, or on `compilation-search-path'.")
2131 (defun ada-show-overridden (other-window)
2132 "Show the overridden declaration of identifier at point."
2135 (when (null ada-xref-overridden-function)
2136 (error "'show overridden' not supported, or no cross reference information available"))
2139 (funcall ada-xref-overridden-function
2140 (ada-identifier-at-point)
2141 (file-name-nondirectory (buffer-file-name))
2142 (line-number-at-pos)
2143 (1+ (current-column)))))
2145 (ada-goto-source (nth 0 target)
2152 ;; This is autoloaded because it may be used in ~/.emacs
2154 (defun ada-add-extensions (spec body)
2155 "Define SPEC and BODY as being valid extensions for Ada files.
2156 SPEC and BODY are two regular expressions that must match against
2158 (let* ((reg (concat (regexp-quote body) "$"))
2159 (tmp (assoc reg ada-other-file-alist)))
2161 (setcdr tmp (list (cons spec (cadr tmp))))
2162 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
2164 (let* ((reg (concat (regexp-quote spec) "$"))
2165 (tmp (assoc reg ada-other-file-alist)))
2167 (setcdr tmp (list (cons body (cadr tmp))))
2168 (add-to-list 'ada-other-file-alist (list reg (list body)))))
2170 (add-to-list 'auto-mode-alist
2171 (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
2172 (add-to-list 'auto-mode-alist
2173 (cons (concat (regexp-quote body) "\\'") 'ada-mode))
2175 (add-to-list 'ada-spec-suffixes spec)
2176 (add-to-list 'ada-body-suffixes body)
2178 (when (fboundp 'speedbar-add-supported-extension)
2179 (speedbar-add-supported-extension spec)
2180 (speedbar-add-supported-extension body))
2183 (defun ada-show-secondary-error (other-window)
2184 "Show the next secondary file reference in the compilation buffer.
2185 A secondary file reference is defined by text having text
2186 property `ada-secondary-error'. These can be set by
2187 compiler-specific compilation filters.
2189 If OTHER-WINDOW (set by interactive prefix) is non-nil, show the
2190 buffer in another window."
2193 ;; preserving the current window works only if the frame
2194 ;; doesn't change, at least on Windows.
2195 (let ((start-buffer (current-buffer))
2196 (start-window (selected-window))
2198 (set-buffer compilation-last-buffer)
2199 (setq pos (next-single-property-change (point) 'ada-secondary-error))
2201 (setq item (get-text-property pos 'ada-secondary-error))
2202 ;; file-relative-name handles absolute Windows paths from
2203 ;; g++. Do this in compilation buffer to get correct
2204 ;; default-directory.
2205 (setq file (file-relative-name (nth 0 item)))
2207 ;; Set point in compilation buffer past this secondary error, so
2208 ;; user can easily go to the next one. For some reason, this
2209 ;; doesn't change the visible point!?
2212 (set-buffer start-buffer);; for windowing history
2217 (nth 2 item); column
2219 (select-window start-window)
2223 (defvar ada-goto-declaration-start nil
2224 ;; Supplied by indentation engine.
2226 ;; This is run from ff-pre-load-hook, so ff-function-name may have
2227 ;; been set by ff-treat-special; don't reset it.
2228 "Function to move point to start of the generic, package,
2229 protected, subprogram, or task declaration point is currently in
2230 or just after. Called with no parameters.")
2232 (defun ada-goto-declaration-start ()
2233 "Call `ada-goto-declaration-start'."
2234 (when ada-goto-declaration-start
2235 (funcall ada-goto-declaration-start)))
2237 (defvar ada-goto-declarative-region-start nil
2238 ;; Supplied by indentation engine
2239 "Function to move point to start of the declarative region of
2240 the subprogram, package, task, or declare block point
2241 is currently in. Called with no parameters.")
2243 (defun ada-goto-declarative-region-start ()
2244 "Call `ada-goto-declarative-region-start'."
2245 (when ada-goto-declarative-region-start
2246 (funcall ada-goto-declarative-region-start)))
2248 (defvar ada-next-statement-keyword nil
2249 ;; Supplied by indentation engine
2250 "Function called with no parameters; it should move forward to
2251 the next keyword in the statement following the one point is
2252 in (ie from 'if' to 'then'). If not in a keyword, move forward
2253 to the next keyword in the current statement. If at the last keyword,
2254 move forward to the first keyword in the next statement or next
2255 keyword in the containing statement.")
2257 (defvar ada-goto-end nil
2258 ;; Supplied by indentation engine
2259 "Function to move point to end of the declaration or statement point is in or before.
2260 Called with no parameters.")
2262 (defun ada-goto-end ()
2263 "Call `ada-goto-end'."
2265 (funcall ada-goto-end)))
2267 (defun ada-next-statement-keyword ()
2268 ;; Supplied by indentation engine
2269 "See `ada-next-statement-keyword' variable."
2271 (when ada-next-statement-keyword
2272 (funcall ada-next-statement-keyword)))
2274 (defvar ada-prev-statement-keyword nil
2275 ;; Supplied by indentation engine
2276 "Function called with no parameters; it should move to the previous
2277 keyword in the statement following the one point is in (ie from
2278 'then' to 'if'). If at the first keyword, move to the previous
2279 keyword in the previous statement or containing statement.")
2281 (defun ada-prev-statement-keyword ()
2282 "See `ada-prev-statement-keyword' variable."
2284 (when ada-prev-statement-keyword
2285 (funcall ada-prev-statement-keyword)))
2289 (defvar ada-make-subprogram-body nil
2290 ;; Supplied by indentation engine
2291 "Function to convert subprogram specification after point into a subprogram body stub.
2292 Called with no args, point at declaration start. Leave point in
2293 subprogram body, for user to add code.")
2295 (defun ada-make-subprogram-body ()
2296 "If point is in or after a subprogram specification, convert it
2297 into a subprogram body stub, by calling `ada-make-subprogram-body'."
2299 (ada-goto-declaration-start)
2300 (if ada-make-subprogram-body
2301 (funcall ada-make-subprogram-body)
2302 (error "`ada-make-subprogram-body' not set")))
2304 (defvar ada-make-package-body nil
2305 ;; Supplied by xref tool
2306 "Function to create a package body from a package spec.
2307 Called with one argument; the absolute path to the body
2308 file. Current buffer is the package spec. Should create the
2309 package body file, containing skeleton code that will compile.")
2311 (defun ada-make-package-body (body-file-name)
2312 ;; no error if not set; let ada-skel do its thing.
2313 (when ada-make-package-body
2314 (funcall ada-make-package-body body-file-name)))
2316 (defun ada-ff-create-body ()
2317 ;; no error if not set; let ada-skel do its thing.
2318 (when ada-make-package-body
2319 ;; ff-find-other-file calls us with point in an empty buffer for the
2320 ;; body file; ada-make-package-body expects to be in the spec. So go
2322 (let ((body-file-name (buffer-file-name)))
2323 (ff-find-the-other-file)
2325 (ada-make-package-body body-file-name)
2326 ;; FIXME (later): if 'ada-make-package-body' fails, delete the body buffer
2327 ;; so it doesn't get written to disk, and we can try again.
2329 ;; back to the body, read in from the disk.
2330 (ff-find-the-other-file)
2331 (revert-buffer t t))
2336 (defun ada-fill-comment-paragraph (&optional justify postfix)
2337 "Fill the current comment paragraph.
2338 If JUSTIFY is non-nil, each line is justified as well.
2339 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
2340 to each line filled and justified.
2341 The paragraph is indented on the first line."
2343 (if (and (not (ada-in-comment-p))
2344 (not (looking-at "[ \t]*--")))
2345 (error "Not inside comment"))
2347 (let* (indent from to
2348 (opos (point-marker))
2349 ;; we bind `fill-prefix' here rather than in ada-mode because
2350 ;; setting it in ada-mode causes indent-region to use it for
2352 (fill-prefix ada-fill-comment-prefix)
2353 (fill-column (current-fill-column)))
2355 ;; Find end of comment paragraph
2356 (back-to-indentation)
2357 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
2360 ;; If we were at the last line in the buffer, create a dummy empty
2361 ;; line at the end of the buffer.
2364 (back-to-indentation)))
2366 (setq to (point-marker))
2369 ;; Find beginning of paragraph
2370 (back-to-indentation)
2371 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
2373 (back-to-indentation))
2378 (setq from (point-marker))
2380 ;; Calculate the indentation we will need for the paragraph
2381 (back-to-indentation)
2382 (setq indent (current-column))
2383 ;; unindent the first line of the paragraph
2384 (delete-region from (point))
2386 ;; Remove the old postfixes
2388 (while (re-search-forward (concat "\\(" ada-fill-comment-postfix "\\)" "\n") to t)
2389 (delete-region (match-beginning 1) (match-end 1)))
2392 (setq to (point-marker))
2394 ;; Indent and justify the paragraph
2395 (set-left-margin from to indent)
2397 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
2399 (fill-region-as-paragraph from to justify)
2401 ;; Add the postfixes if required
2405 (narrow-to-region from to)
2408 (insert-char ? (- fill-column (current-column)))
2409 (insert ada-fill-comment-postfix)
2415 ;;;; support for font-lock.el
2417 ;; casing keywords defined here to keep the two lists together
2418 (defconst ada-83-keywords
2419 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
2420 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
2421 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
2422 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
2423 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
2424 "procedure" "raise" "range" "record" "rem" "renames" "return"
2425 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
2426 "type" "use" "when" "while" "with" "xor")
2427 "List of Ada 83 keywords.")
2429 (defconst ada-95-keywords
2430 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
2431 "List of keywords new in Ada 95.")
2433 (defconst ada-2005-keywords
2434 '("interface" "overriding" "synchronized")
2435 "List of keywords new in Ada 2005.")
2437 (defconst ada-2012-keywords
2439 "List of keywords new in Ada 2012.")
2441 (defun ada-font-lock-keywords ()
2442 "Return Ada mode value for `font-lock-keywords', depending on `ada-language-version'."
2445 ;; keywords followed by a name that should be in function-name-face.
2454 "package[ \t]+body\\|"
2462 (when (member ada-language-version '(ada95 ada2005 ada2012))
2464 "protected[ \t]+body\\|"
2465 "protected[ \t]+function\\|"
2466 "protected[ \t]+procedure\\|"
2467 "protected[ \t]+type\\|"
2472 ada-name-regexp "?")))
2473 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
2475 ;; keywords followed by a name that should be in type-face.
2478 "access[ \t]+all\\|"
2479 "access[ \t]+constant\\|"
2482 "in[ \t]+reverse\\|"; loop iterator
2483 "in[ \t]+not[ \t]+null\\|"
2484 "in[ \t]+out[ \t]+not[ \t]+null\\|"
2487 ;; "return\\|" can't distinguish between 'function ... return <type>;' and 'return ...;'
2488 ;; An indentation engine can, so a rule for this is added there
2489 "of[ \t]+reverse\\|"
2495 ada-name-regexp "?")
2496 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
2498 ;; Keywords not treated elsewhere. After above so it doesn't
2499 ;; override fontication of second or third word in those patterns.
2504 '("abort" "abs" "accept" "all"
2505 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
2506 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
2507 "generic" "if" "in" "limited" "loop" "mod" "not"
2508 "null" "or" "others" "private" "raise"
2509 "range" "record" "rem" "renames" "reverse"
2510 "select" "separate" "task" "terminate"
2511 "then" "when" "while" "xor")
2512 (when (member ada-language-version '(ada95 ada2005 ada2012))
2513 '("abstract" "aliased" "requeue" "tagged" "until"))
2514 (when (member ada-language-version '(ada2005 ada2012))
2515 '("interface" "overriding" "synchronized"))
2516 (when (member ada-language-version '(ada2012))
2521 '(0 font-lock-keyword-face))
2523 ;; object and parameter declarations; word after ":" should be in
2524 ;; type-face if not already fontified or an exception.
2529 '(1 (if (match-beginning 2)
2531 font-lock-type-face)
2534 ;; keywords followed by a name that should be in function-name-face if not already fontified
2539 ada-name-regexp "?")
2540 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
2542 ;; Keywords followed by a name that could be a type or a function (generic instantiation).
2547 ada-name-regexp "?[ \t]*\\((\\)?")
2548 '(1 font-lock-keyword-face)
2549 '(2 (if (match-beginning 3)
2550 font-lock-function-name-face
2551 font-lock-type-face)
2554 ;; keywords followed by a name that should be in type-face if not already fontified (for subtypes)
2555 ;; after "new" to handle "is new"
2560 ada-name-regexp "?")
2561 '(1 font-lock-keyword-face) '(2 font-lock-type-face nil t))
2563 ;; Keywords followed by a comma separated list of names which
2564 ;; should be in constant-face, unless already fontified. Ada mode 4.01 used this.
2569 ;; don't need "limited" "private" here; they are matched separately
2570 "with"; context clause
2572 "\\(\\(?:\\sw\\|[_., \t]\\)+\\>\\)?"; ada-name-regexp, plus ", \t"
2574 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
2577 '("<<\\(\\sw+\\)>>" 1 font-lock-constant-face)
2579 ;; based numberic literals
2580 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
2583 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
2589 ;; ada-mode does not derive from prog-mode, because we need to call
2590 ;; ada-mode-post-local-vars, and prog-mode does not provide a way to
2593 ;; autoload required by automatic mode setting
2596 "The major mode for editing Ada code."
2597 ;; the other ada-*.el files add to ada-mode-hook for their setup
2600 (kill-all-local-variables)
2601 (setq major-mode 'ada-mode)
2602 (setq mode-name "Ada")
2603 (use-local-map ada-mode-map)
2604 (set-syntax-table ada-mode-syntax-table)
2605 (define-abbrev-table 'ada-mode-abbrev-table ())
2606 (setq local-abbrev-table ada-mode-abbrev-table)
2608 (set (make-local-variable 'syntax-propertize-function) 'ada-syntax-propertize)
2609 (set (make-local-variable 'syntax-begin-function) nil)
2610 (set (make-local-variable 'parse-sexp-ignore-comments) t)
2611 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2612 (set 'case-fold-search t); Ada is case insensitive; the syntax parsing requires this setting
2613 (set (make-local-variable 'comment-start) "--")
2614 (set (make-local-variable 'comment-end) "")
2615 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
2616 (set (make-local-variable 'comment-multi-line) nil)
2618 ;; we _don't_ set `fill-prefix' here because that causes
2619 ;; indent-region to use it for all indentation. See
2620 ;; ada-fill-comment-paragraph.
2622 ;; AdaCore standard style (enforced by -gnaty) requires two spaces
2623 ;; after '--' in comments; this makes it easier to distinguish
2624 ;; special comments that have something else after '--'
2625 (set (make-local-variable 'comment-padding) " ")
2627 (set (make-local-variable 'require-final-newline) t)
2629 (setq font-lock-defaults
2630 '(ada-font-lock-keywords
2632 ((?\_ . "w")))); treat underscore as a word component
2634 (set (make-local-variable 'ff-other-file-alist)
2635 'ada-other-file-alist)
2636 (setq ff-post-load-hook 'ada-set-point-accordingly
2637 ff-file-created-hook 'ada-ff-create-body)
2638 (add-hook 'ff-pre-load-hook 'ada-which-function)
2639 (setq ff-search-directories 'compilation-search-path)
2640 (when (null (car compilation-search-path))
2641 ;; find-file doesn't handle nil in search path
2642 (setq compilation-search-path (list (file-name-directory (buffer-file-name)))))
2643 (ada-set-ff-special-constructs)
2645 (set (make-local-variable 'add-log-current-defun-function)
2646 'ada-add-log-current-function)
2648 (when (boundp 'which-func-functions)
2649 (add-hook 'which-func-functions 'ada-which-function nil t))
2651 ;; Support for align
2652 (add-to-list 'align-dq-string-modes 'ada-mode)
2653 (add-to-list 'align-open-comment-modes 'ada-mode)
2654 (set (make-local-variable 'align-region-separate) ada-align-region-separate)
2655 (set (make-local-variable 'align-indent-before-aligning) t)
2657 ;; Exclude comments alone on line from alignment.
2658 (add-to-list 'align-exclude-rules-list
2660 (regexp . "^\\(\\s-*\\)--")
2661 (modes . '(ada-mode))))
2662 (add-to-list 'align-exclude-rules-list
2664 (regexp . "^\\(\\s-*\\)\\<use\\>")
2665 (modes . '(ada-mode))))
2667 (setq align-mode-rules-list ada-align-rules)
2669 (easy-menu-add ada-mode-menu ada-mode-map)
2671 (run-mode-hooks 'ada-mode-hook)
2673 ;; If global-font-lock is not enabled, ada-syntax-propertize is
2674 ;; not run when the text is first loaded into the buffer. Recover
2676 (syntax-ppss-flush-cache (point-min))
2677 (syntax-propertize (point-max))
2679 (add-hook 'hack-local-variables-hook 'ada-mode-post-local-vars nil t)
2682 (defun ada-mode-post-local-vars ()
2683 ;; These are run after ada-mode-hook and file local variables
2684 ;; because users or other ada-* files might set the relevant
2685 ;; variable inside the hook or file local variables (file local
2686 ;; variables are processed after the mode is set, and thus after
2687 ;; ada-mode is run).
2689 ;; This means to fully set ada-mode interactively, user must
2690 ;; do M-x ada-mode M-; (hack-local-variables)
2692 (when global-font-lock-mode
2693 ;; This calls ada-font-lock-keywords, which depends on
2694 ;; ada-language-version
2695 (font-lock-refresh-defaults))
2697 (cl-case ada-language-version
2699 (setq ada-keywords ada-83-keywords))
2703 (append ada-83-keywords
2708 (append ada-83-keywords
2710 ada-2005-keywords)))
2713 (append ada-83-keywords
2716 ada-2012-keywords))))
2719 (put 'ada-mode 'custom-mode-group 'ada)
2723 ;;;;; Global initializations
2725 (require 'ada-build)
2727 (unless (featurep 'ada-indent-engine)
2728 (require 'ada-wisi))
2730 (unless (featurep 'ada-xref-tool)
2731 (cl-case ada-xref-tool
2732 ((nil 'gnat) (require 'ada-gnat-xref))
2733 ('gnat_inspect (require 'gnat-inspect))
2734 ('gpr_query (require 'gpr-query))
2737 (unless (featurep 'ada-compiler)
2738 (require 'ada-gnat-compile))
2740 (unless (featurep 'ada-skeletons)
2741 (require 'ada-skel))
2743 (when (featurep 'imenu)
2744 (require 'ada-imenu))