1 ;;; ada-mode.el --- major-mode for editing Ada sources
3 ;; Copyright (C) 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Rolf Ebert <ebert@inf.enst.fr>
8 ;; Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
9 ;; Emmanuel Briot <briot@gnat.com>
10 ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
11 ;; Keywords: languages ada
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This mode is a major mode for editing Ada code. This is a major
30 ;; rewrite of the file packaged with Emacs-20. The Ada mode is
31 ;; composed of four Lisp files: ada-mode.el, ada-xref.el, ada-prj.el
32 ;; and ada-stmt.el. Only this file (ada-mode.el) is completely
33 ;; independent from the GNU Ada compiler GNAT, distributed by Ada
34 ;; Core Technologies. All the other files rely heavily on features
35 ;; provided only by GNAT.
37 ;; Note: this mode will not work with Emacs 19. If you are on a VMS
38 ;; system, where the latest version of Emacs is 19.28, you will need
39 ;; another file, called ada-vms.el, that provides some required
43 ;; Emacs should enter Ada mode automatically when you load an Ada file.
44 ;; By default, the valid extensions for Ada files are .ads, .adb or .ada
45 ;; If the ada-mode does not start automatically, then simply type the
46 ;; following command :
49 ;; By default, ada-mode is configured to take full advantage of the GNAT
50 ;; compiler (the menus will include the cross-referencing features,...).
51 ;; If you are using another compiler, you might want to set the following
52 ;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
54 ;; (setq ada-which-compiler 'generic)
56 ;; This mode requires find-file.el to be present on your system.
59 ;; The first Ada mode for GNU Emacs was written by V. Broman in
60 ;; 1985. He based his work on the already existing Modula-2 mode.
61 ;; This was distributed as ada.el in versions of Emacs prior to 19.29.
63 ;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
64 ;; several files with support for dired commands and other nice
65 ;; things. It is currently available from the PAL
66 ;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
68 ;; The probably very first Ada mode (called electric-ada.el) was
69 ;; written by Steven D. Litvintchouk and Steven M. Rosen for the
70 ;; Gosling Emacs. L. Slater based his development on ada.el and
73 ;; A complete rewrite by M. Heritsch and R. Ebert has been done.
74 ;; Some ideas from the Ada mode mailing list have been
75 ;; added. Some of the functionality of L. Slater's mode has not
76 ;; (yet) been recoded in this new mode. Perhaps you prefer sticking
79 ;; A complete rewrite for Emacs-20 / GNAT-3.11 has been done by Ada Core
83 ;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
84 ;; many patches included in this package.
85 ;; Christian Egli <Christian.Egli@hcsd.hac.com>:
86 ;; ada-imenu-generic-expression
87 ;; Many thanks also to the following persons that have contributed
89 ;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
90 ;; woodruff@stc.llnl.gov (John Woodruff)
91 ;; jj@ddci.dk (Jesper Joergensen)
92 ;; gse@ocsystems.com (Scott Evans)
93 ;; comar@gnat.com (Cyrille Comar)
94 ;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
95 ;; robin-reply@reagans.org
96 ;; and others for their valuable hints.
99 ;; Note: Every function in this package is compiler-independent.
100 ;; The names start with ada-
101 ;; The variables that the user can edit can all be modified through
102 ;; the customize mode. They are sorted in alphabetical order in this
105 ;; Supported packages.
106 ;; This package supports a number of other Emacs modes. These other modes
107 ;; should be loaded before the ada-mode, which will then setup some variables
108 ;; to improve the support for Ada code.
109 ;; Here is the list of these modes:
110 ;; `which-function-mode': Display the name of the subprogram the cursor is
111 ;; in in the mode line.
112 ;; `outline-mode': Provides the capability to collapse or expand the code
113 ;; for specific language constructs, for instance if you want to hide the
114 ;; code corresponding to a subprogram
115 ;; `align': This mode is now provided with Emacs 21, but can also be
116 ;; installed manually for older versions of Emacs. It provides the
117 ;; capability to automatically realign the selected region (for instance
118 ;; all ':=', ':' and '--' will be aligned on top of each other.
119 ;; `imenu': Provides a menu with the list of entities defined in the current
120 ;; buffer, and an easy way to jump to any of them
121 ;; `speedbar': Provides a separate file browser, and the capability for each
122 ;; file to see the list of entities defined in it and to jump to them
124 ;; `abbrev-mode': Provides the capability to define abbreviations, which
125 ;; are automatically expanded when you type them. See the Emacs manual.
127 (require 'find-file nil t
)
128 (require 'align nil t
)
129 (require 'which-func nil t
)
130 (require 'compile nil t
)
132 (defvar ispell-check-comments
)
133 (defvar skeleton-further-elements
)
135 (defun ada-mode-version ()
136 "Return Ada mode version."
138 (let ((version-string "4.00"))
139 (if (called-interactively-p 'interactive
)
140 (message version-string
)
143 (defvar ada-mode-hook nil
144 "*List of functions to call when Ada mode is invoked.
145 This hook is automatically executed after the `ada-mode' is
147 This is a good place to add Ada environment specific bindings.")
150 "Major mode for editing and compiling Ada source in Emacs."
151 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
154 (defcustom ada-auto-case t
155 "*Non-nil means automatically change case of preceding word while typing.
156 Casing is done according to `ada-case-keyword', `ada-case-identifier'
157 and `ada-case-attribute'."
158 :type
'boolean
:group
'ada
)
160 (defcustom ada-broken-decl-indent
0
161 "*Number of columns to indent a broken declaration.
167 :type
'integer
:group
'ada
)
169 (defcustom ada-broken-indent
2
170 "*Number of columns to indent the continuation of a broken line.
173 My_Var : My_Type := (Field1 =>
175 :type
'integer
:group
'ada
)
177 (defcustom ada-continuation-indent ada-broken-indent
178 "*Number of columns to indent the continuation of broken lines in parenthesis.
183 :type
'integer
:group
'ada
)
185 (defcustom ada-case-attribute
'ada-capitalize-word
186 "*Function to call to adjust the case of Ada attributes.
187 It may be `downcase-word', `upcase-word', `ada-loose-case-word',
188 `ada-capitalize-word' or `ada-no-auto-case'."
189 :type
'(choice (const downcase-word
)
191 (const ada-capitalize-word
)
192 (const ada-loose-case-word
)
193 (const ada-no-auto-case
))
196 (defcustom ada-case-exception-file
197 (list (convert-standard-filename' "~/.emacs_case_exceptions"))
198 "*List of special casing exceptions dictionaries for identifiers.
199 The first file is the one where new exceptions will be saved by Emacs
200 when you call `ada-create-case-exception'.
202 These files should contain one word per line, that gives the casing
203 to be used for that word in Ada files. If the line starts with the
204 character *, then the exception will be used for substrings that either
205 start at the beginning of a word or after a _ character, and end either
206 at the end of the word or at a _ character. Each line can be terminated
208 :type
'(repeat (file))
211 (defcustom ada-case-keyword
'downcase-word
212 "*Function to call to adjust the case of an Ada keywords.
213 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
214 `ada-capitalize-word'."
215 :type
'(choice (const downcase-word
)
217 (const ada-capitalize-word
)
218 (const ada-loose-case-word
)
219 (const ada-no-auto-case
))
222 (defcustom ada-case-identifier
'ada-loose-case-word
223 "*Function to call to adjust the case of an Ada identifier.
224 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
225 `ada-capitalize-word'."
226 :type
'(choice (const downcase-word
)
228 (const ada-capitalize-word
)
229 (const ada-loose-case-word
)
230 (const ada-no-auto-case
))
233 (defcustom ada-clean-buffer-before-saving t
234 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
235 :type
'boolean
:group
'ada
)
236 (make-obsolete-variable 'ada-clean-buffer-before-saving
237 "use the `write-file-functions' hook."
241 (defcustom ada-indent
3
242 "*Size of Ada indentation.
248 :type
'integer
:group
'ada
)
250 (defcustom ada-indent-after-return t
251 "*Non-nil means automatically indent after RET or LFD."
252 :type
'boolean
:group
'ada
)
254 (defcustom ada-indent-align-comments t
255 "*Non-nil means align comments on previous line comments, if any.
256 If nil, indentation is calculated as usual.
257 Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
260 A := 1; -- A multi-line comment
261 -- aligned if `ada-indent-align-comments' is t"
262 :type
'boolean
:group
'ada
)
264 (defcustom ada-indent-comment-as-code t
265 "*Non-nil means indent comment lines as code.
266 A nil value means do not auto-indent comments."
267 :type
'boolean
:group
'ada
)
269 (defcustom ada-indent-handle-comment-special nil
270 "*Non-nil if comment lines should be handled specially inside parenthesis.
271 By default, if the line that contains the open parenthesis has some
272 text following it, then the following lines will be indented in the
273 same column as this text. This will not be true if the first line is
274 a comment and `ada-indent-handle-comment-special' is t.
277 ( Value_1, -- common behavior, when not a comment
281 ( -- `ada-indent-handle-comment-special' is nil
286 ( -- `ada-indent-handle-comment-special' is non-nil
289 :type
'boolean
:group
'ada
)
291 (defcustom ada-indent-is-separate t
292 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
293 :type
'boolean
:group
'ada
)
295 (defcustom ada-indent-record-rel-type
3
296 "*Indentation for 'record' relative to 'type' or 'use'.
301 :type
'integer
:group
'ada
)
303 (defcustom ada-indent-renames ada-broken-indent
304 "*Indentation for renames relative to the matching function statement.
305 If `ada-indent-return' is null or negative, the indentation is done relative to
306 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
309 function A (B : Integer)
312 :type
'integer
:group
'ada
)
314 (defcustom ada-indent-return
0
315 "*Indentation for 'return' relative to the matching 'function' statement.
316 If `ada-indent-return' is null or negative, the indentation is done relative to
317 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
320 function A (B : Integer)
322 :type
'integer
:group
'ada
)
324 (defcustom ada-indent-to-open-paren t
325 "*Non-nil means indent according to the innermost open parenthesis."
326 :type
'boolean
:group
'ada
)
328 (defcustom ada-fill-comment-prefix
"-- "
329 "*Text inserted in the first columns when filling a comment paragraph.
330 Note: if you modify this variable, you will have to invoke `ada-mode'
331 again to take account of the new value."
332 :type
'string
:group
'ada
)
334 (defcustom ada-fill-comment-postfix
" --"
335 "*Text inserted at the end of each line when filling a comment paragraph.
336 Used by `ada-fill-comment-paragraph-postfix'."
337 :type
'string
:group
'ada
)
339 (defcustom ada-label-indent -
4
340 "*Number of columns to indent a label.
347 This is also used for <<..>> labels"
348 :type
'integer
:group
'ada
)
350 (defcustom ada-language-version
'ada95
351 "*Ada language version; one of `ada83', `ada95', `ada2005'."
352 :type
'(choice (const ada83
) (const ada95
) (const ada2005
)) :group
'ada
)
354 (defcustom ada-move-to-declaration nil
355 "*Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to 'begin'."
356 :type
'boolean
:group
'ada
)
358 (defcustom ada-popup-key
'[down-mouse-3
]
359 "*Key used for binding the contextual menu.
360 If nil, no contextual menu is available."
361 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
))
364 (defcustom ada-search-directories
366 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
367 '("/usr/adainclude" "/usr/local/adainclude"
368 "/opt/gnu/adainclude"))
369 "*Default list of directories to search for Ada files.
370 See the description for the `ff-search-directories' variable. This variable
371 is the initial value of `ada-search-directories-internal'."
372 :type
'(repeat (choice :tag
"Directory"
373 (const :tag
"default" nil
)
374 (directory :format
"%v")))
377 (defvar ada-search-directories-internal ada-search-directories
378 "Internal version of `ada-search-directories'.
379 Its value is the concatenation of the search path as read in the project file
380 and the standard runtime location, and the value of the user-defined
381 `ada-search-directories'.")
383 (defcustom ada-stmt-end-indent
0
384 "*Number of columns to indent the end of a statement on a separate line.
389 :type
'integer
:group
'ada
)
391 (defcustom ada-tab-policy
'indent-auto
392 "*Control the behavior of the TAB key.
394 `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
395 `indent-auto' : use indentation functions in this file.
396 `always-tab' : do `indent-relative'."
397 :type
'(choice (const indent-auto
)
398 (const indent-rigidly
)
402 (defcustom ada-use-indent ada-broken-indent
403 "*Indentation for the lines in a 'use' statement.
408 :type
'integer
:group
'ada
)
410 (defcustom ada-when-indent
3
411 "*Indentation for 'when' relative to 'exception' or 'case'.
416 :type
'integer
:group
'ada
)
418 (defcustom ada-with-indent ada-broken-indent
419 "*Indentation for the lines in a 'with' statement.
424 :type
'integer
:group
'ada
)
426 (defcustom ada-which-compiler
'gnat
427 "*Name of the compiler to use.
428 This will determine what features are made available through the Ada mode.
429 The possible choices are:
430 `gnat': Use Ada Core Technologies' GNAT compiler. Add some cross-referencing
432 `generic': Use a generic compiler."
433 :type
'(choice (const gnat
)
438 ;;; ---- end of user configurable variables
441 (defvar ada-body-suffixes
'(".adb")
442 "List of possible suffixes for Ada body files.
443 The extensions should include a `.' if needed.")
445 (defvar ada-spec-suffixes
'(".ads")
446 "List of possible suffixes for Ada spec files.
447 The extensions should include a `.' if needed.")
449 (defvar ada-mode-menu
(make-sparse-keymap "Ada")
450 "Menu for Ada mode.")
452 (defvar ada-mode-map
(make-sparse-keymap)
453 "Local keymap used for Ada mode.")
455 (defvar ada-mode-extra-map
(make-sparse-keymap)
456 "Keymap used for non-standard keybindings.")
458 ;; default is C-c C-q because it's free in ada-mode-map
459 (defvar ada-mode-extra-prefix
"\C-c\C-q"
460 "Prefix key to access `ada-mode-extra-map' functions.")
462 (defvar ada-mode-abbrev-table nil
463 "Local abbrev table for Ada mode.")
464 (define-abbrev-table 'ada-mode-abbrev-table
())
466 (defvar ada-mode-syntax-table nil
467 "Syntax table to be used for editing Ada source code.")
469 (defvar ada-mode-symbol-syntax-table nil
470 "Syntax table for Ada, where `_' is a word constituent.")
473 ;; These values are used in eval-when-compile expressions.
474 (defconst ada-83-string-keywords
475 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
476 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
477 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
478 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
479 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
480 "procedure" "raise" "range" "record" "rem" "renames" "return"
481 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
482 "type" "use" "when" "while" "with" "xor")
483 "List of Ada 83 keywords.
484 Used to define `ada-*-keywords'.")
486 (defconst ada-95-string-keywords
487 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
488 "List of keywords new in Ada 95.
489 Used to define `ada-*-keywords'.")
491 (defconst ada-2005-string-keywords
492 '("interface" "overriding" "synchronized")
493 "List of keywords new in Ada 2005.
494 Used to define `ada-*-keywords.'"))
496 (defvar ada-ret-binding nil
497 "Variable to save key binding of RET when casing is activated.")
499 (defvar ada-case-exception
'()
500 "Alist of words (entities) that have special casing.")
502 (defvar ada-case-exception-substring
'()
503 "Alist of substrings (entities) that have special casing.
504 The substrings are detected for word constituant when the word
505 is not itself in `ada-case-exception', and only for substrings that
506 either are at the beginning or end of the word, or start after '_'.")
508 (defvar ada-lfd-binding nil
509 "Variable to save key binding of LFD when casing is activated.")
511 (defvar ada-other-file-alist nil
512 "Variable used by `find-file' to find the name of the other package.
513 See `ff-other-file-alist'.")
515 (defvar ada-align-list
516 '(("[^:]\\(\\s-*\\):[^:]" 1 t
)
517 ("[^=]\\(\\s-+\\)=[^=]" 1 t
)
518 ("\\(\\s-*\\)use\\s-" 1)
520 "Ada support for align.el <= 2.2.
521 This variable provides regular expressions on which to align different lines.
522 See `align-mode-alist' for more information.")
524 (defvar ada-align-modes
526 (regexp .
"[^:]\\(\\s-*\\):[^:]")
527 (valid .
(lambda() (not (ada-in-comment-p))))
528 (modes .
'(ada-mode)))
530 (regexp .
"[^=]\\(\\s-+\\)=[^=]")
531 (valid .
(lambda() (not (ada-in-comment-p))))
532 (modes .
'(ada-mode)))
534 (regexp .
"\\(\\s-*\\)--")
535 (modes .
'(ada-mode)))
537 (regexp .
"\\(\\s-*\\)use\\s-")
538 (valid .
(lambda() (not (ada-in-comment-p))))
539 (modes .
'(ada-mode)))
541 "Ada support for align.el >= 2.8.
542 This variable defines several rules to use to align different lines.")
544 (defconst ada-align-region-separate
564 "See the variable `align-region-separate' for more information.")
566 ;;; ---- Below are the regexp used in this package for parsing
568 (defconst ada-83-keywords
570 (concat "\\<" (regexp-opt ada-83-string-keywords t
) "\\>"))
571 "Regular expression matching Ada83 keywords.")
573 (defconst ada-95-keywords
575 (concat "\\<" (regexp-opt
577 ada-95-string-keywords
578 ada-83-string-keywords
) t
) "\\>"))
579 "Regular expression matching Ada95 keywords.")
581 (defconst ada-2005-keywords
583 (concat "\\<" (regexp-opt
585 ada-2005-string-keywords
586 ada-83-string-keywords
587 ada-95-string-keywords
) t
) "\\>"))
588 "Regular expression matching Ada2005 keywords.")
590 (defvar ada-keywords ada-2005-keywords
591 "Regular expression matching Ada keywords.")
592 ;; FIXME: make this customizable
594 (defconst ada-ident-re
595 "[[:alpha:]]\\(?:[_[:alnum:]]\\)*"
596 ;; [:alnum:] matches any multibyte word constituent, as well as
597 ;; Latin-1 letters and numbers. This allows __ and trailing _;
598 ;; someone (emacs bug#1919) proposed [^\W_] to fix that, but \W does
599 ;; _not_ mean "not word constituent" inside a character alternative.
600 "Regexp matching an Ada identifier.")
602 (defconst ada-goto-label-re
603 (concat "<<" ada-ident-re
">>")
604 "Regexp matching a goto label.")
606 (defconst ada-block-label-re
607 (concat ada-ident-re
"[ \t\n]*:[^=]")
608 "Regexp matching a block label.
609 Note that this also matches a variable declaration.")
611 (defconst ada-label-re
612 (concat "\\(?:" ada-block-label-re
"\\)\\|\\(?:" ada-goto-label-re
"\\)")
613 "Regexp matching a goto or block label.")
615 ;; "with" needs to be included in the regexp, to match generic subprogram parameters
616 ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
617 (defvar ada-procedure-start-regexp
619 "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
621 ;; subprogram name: operator ("[+/=*]")
625 ;; subprogram name: name
627 "\\(\\(\\sw\\|[_.]\\)+\\)"
629 "Regexp matching Ada subprogram start.
630 The actual start is at (match-beginning 4). The name is in (match-string 5).")
632 (defconst ada-name-regexp
633 "\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
634 "Regexp matching a fully qualified name (including attribute).")
636 (defconst ada-package-start-regexp
637 (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp
)
638 "Regexp matching start of package.
639 The package name is in (match-string 4).")
641 (defconst ada-compile-goto-error-file-linenr-re
642 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
643 "Regexp matching filename:linenr[:column].")
646 ;;; ---- regexps for indentation functions
648 (defvar ada-block-start-re
650 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
651 "exception" "generic" "loop" "or"
652 "private" "select" ))
653 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
654 "Regexp for keywords starting Ada blocks.")
656 (defvar ada-end-stmt-re
661 "=>[ \t]*--.*$" "\\|"
662 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
663 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
664 "loop" "private" "record" "select"
665 "then abort" "then") t
) "\\>" "\\|"
666 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
667 t
) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
668 "^[ \t]*exception\\>"
670 "Regexp of possible ends for a non-broken statement.
671 A new statement starts after these.")
673 (defvar ada-matching-start-re
677 '("end" "loop" "select" "begin" "case" "do" "declare"
678 "if" "task" "package" "procedure" "function" "record" "protected") t
)
680 "Regexp used in `ada-goto-matching-start'.")
682 (defvar ada-loop-start-re
683 "\\<\\(for\\|while\\|loop\\)\\>"
684 "Regexp for the start of a loop.")
686 (defvar ada-subprog-start-re
688 (concat "\\<" (regexp-opt '("accept" "entry" "function" "overriding" "package" "procedure"
689 "protected" "task") t
) "\\>"))
690 "Regexp for the start of a subprogram.")
692 (defvar ada-contextual-menu-on-identifier nil
693 "Set to true when the right mouse button was clicked on an identifier.")
695 (defvar ada-contextual-menu-last-point nil
696 "Position of point just before displaying the menu.
697 This is a list (point buffer).
698 Since `ada-popup-menu' moves the point where the user clicked, the region
699 is modified. Therefore no command from the menu knows what the user selected
700 before displaying the contextual menu.
701 To get the original region, restore the point to this position before
702 calling `region-end' and `region-beginning'.
703 Modify this variable if you want to restore the point to another position.")
705 (easy-menu-define ada-contextual-menu nil
706 "Menu to use when the user presses the right mouse button.
707 The variable `ada-contextual-menu-on-identifier' will be set to t before
708 displaying the menu if point was on an identifier."
710 ["Goto Declaration/Body" ada-point-and-xref
711 :included ada-contextual-menu-on-identifier
]
712 ["Goto Body" ada-point-and-xref-body
713 :included ada-contextual-menu-on-identifier
]
714 ["Goto Previous Reference" ada-xref-goto-previous-reference
]
715 ["List References" ada-find-references
716 :included ada-contextual-menu-on-identifier
]
717 ["List Local References" ada-find-local-references
718 :included ada-contextual-menu-on-identifier
]
720 ["Other File" ff-find-other-file
]
721 ["Goto Parent Unit" ada-goto-parent
]))
724 ;;------------------------------------------------------------------
725 ;; Support for imenu (see imenu.el)
726 ;;------------------------------------------------------------------
728 (defconst ada-imenu-comment-re
"\\([ \t]*--.*\\)?")
730 (defconst ada-imenu-subprogram-menu-re
731 (concat "^[ \t]*\\(overriding[ \t]*\\)?\\(procedure\\|function\\)[ \t\n]+"
732 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
735 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
737 (defvar ada-imenu-generic-expression
739 (list nil ada-imenu-subprogram-menu-re
3)
742 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
744 "\\(" ada-imenu-comment-re
"[ \t\n]+\\|[ \t\n]*([^)]+)"
745 ada-imenu-comment-re
"\\)";; parameter list or simple space
746 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
748 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
749 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
751 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
752 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
753 "Imenu generic expression for Ada mode.
754 See `imenu-generic-expression'. This variable will create several submenus for
755 each type of entity that can be found in an Ada file.")
758 ;;------------------------------------------------------------
759 ;; Support for compile.el
760 ;;------------------------------------------------------------
762 (defun ada-compile-mouse-goto-error ()
763 "Mouse interface for `ada-compile-goto-error'."
765 (mouse-set-point last-input-event
)
766 (ada-compile-goto-error (point))
769 (defun ada-compile-goto-error (pos)
770 "Replace `compile-goto-error' from compile.el.
771 If POS is on a file and line location, go to this position. It adds
772 to compile.el the capacity to go to a reference in an error message.
773 For instance, on these lines:
774 foo.adb:61:11: [...] in call to size declared at foo.ads:11
775 foo.adb:61:11: [...] in call to local declared at line 20
776 the 4 file locations can be clicked on and jumped to."
780 (skip-chars-backward "-a-zA-Z0-9_:./\\")
782 ;; special case: looking at a filename:line not at the beginning of a line
783 ;; or a simple line reference "at line ..."
785 (or (looking-at ada-compile-goto-error-file-linenr-re
)
789 (looking-at ada-compile-goto-error-file-linenr-re
))
791 (if (looking-at "\\([0-9]+\\)") (backward-word 1))
792 (looking-at "line \\([0-9]+\\)"))))
794 (let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
795 (file (if (match-beginning 2) (match-string 1)
796 (save-excursion (beginning-of-line)
797 (looking-at ada-compile-goto-error-file-linenr-re
)
799 (error-pos (point-marker))
804 (compilation-find-file (point-marker) (match-string 1) "./")
808 (goto-char (point-min))
809 (forward-line (1- (string-to-number line
))))
811 (setq source
(point-marker)))
813 (compilation-goto-locus error-pos source nil
)
817 ;; otherwise, default behavior
819 (compile-goto-error))
824 ;;-------------------------------------------------------------------------
825 ;; Grammar related function
826 ;; The functions below work with the syntax class of the characters in an Ada
827 ;; buffer. Two syntax tables are created, depending on whether we want '_'
828 ;; to be considered as part of a word or not.
829 ;; Some characters may have multiple meanings depending on the context:
830 ;; - ' is either the beginning of a constant character or an attribute
831 ;; - # is either part of a based litteral or a gnatprep statement.
832 ;; - " starts a string, but not if inside a constant character.
833 ;; - ( and ) should be ignored if inside a constant character.
834 ;; Thus their syntax property is changed automatically, and we can still use
835 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
837 ;; On Emacs, this is done through the `syntax-table' text property. The
838 ;; corresponding action is applied automatically each time the buffer
839 ;; changes via syntax-propertize-function.
841 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
842 ;; slow advice to `parse-partial-sexp' to do the same thing.
843 ;; When executing parse-partial-sexp, we simply modify the strings before and
844 ;; after, so that the special constants '"', '(' and ')' do not interact
845 ;; with parse-partial-sexp.
846 ;; Note: this code is slow and needs to be rewritten as soon as something
847 ;; better is available on XEmacs.
848 ;;-------------------------------------------------------------------------
850 (defun ada-create-syntax-table ()
851 "Create the two syntax tables use in the Ada mode.
852 The standard table declares `_' as a symbol constituent, the second one
853 declares it as a word constituent."
855 (setq ada-mode-syntax-table
(make-syntax-table))
857 ;; define string brackets (`%' is alternative string bracket, but
858 ;; almost never used as such and throws font-lock and indentation
860 (modify-syntax-entry ?%
"$" ada-mode-syntax-table
)
861 (modify-syntax-entry ?
\" "\"" ada-mode-syntax-table
)
863 (modify-syntax-entry ?
: "." ada-mode-syntax-table
)
864 (modify-syntax-entry ?\
; "." ada-mode-syntax-table)
865 (modify-syntax-entry ?
& "." ada-mode-syntax-table
)
866 (modify-syntax-entry ?\|
"." ada-mode-syntax-table
)
867 (modify-syntax-entry ?
+ "." ada-mode-syntax-table
)
868 (modify-syntax-entry ?
* "." ada-mode-syntax-table
)
869 (modify-syntax-entry ?
/ "." ada-mode-syntax-table
)
870 (modify-syntax-entry ?
= "." ada-mode-syntax-table
)
871 (modify-syntax-entry ?
< "." ada-mode-syntax-table
)
872 (modify-syntax-entry ?
> "." ada-mode-syntax-table
)
873 (modify-syntax-entry ?$
"." ada-mode-syntax-table
)
874 (modify-syntax-entry ?\
[ "." ada-mode-syntax-table
)
875 (modify-syntax-entry ?\
] "." ada-mode-syntax-table
)
876 (modify-syntax-entry ?\
{ "." ada-mode-syntax-table
)
877 (modify-syntax-entry ?\
} "." ada-mode-syntax-table
)
878 (modify-syntax-entry ?.
"." ada-mode-syntax-table
)
879 (modify-syntax-entry ?
\\ "." ada-mode-syntax-table
)
880 (modify-syntax-entry ?
\' "." ada-mode-syntax-table
)
882 ;; a single hyphen is punctuation, but a double hyphen starts a comment
883 (modify-syntax-entry ?-
". 12" ada-mode-syntax-table
)
885 ;; See the comment above on grammar related function for the special
887 (if (featurep 'xemacs
)
888 (modify-syntax-entry ?
# "<" ada-mode-syntax-table
)
889 (modify-syntax-entry ?
# "$" ada-mode-syntax-table
))
891 ;; and \f and \n end a comment
892 (modify-syntax-entry ?
\f "> " ada-mode-syntax-table
)
893 (modify-syntax-entry ?
\n "> " ada-mode-syntax-table
)
895 ;; define what belongs in Ada symbols
896 (modify-syntax-entry ?_
"_" ada-mode-syntax-table
)
898 ;; define parentheses to match
899 (modify-syntax-entry ?\
( "()" ada-mode-syntax-table
)
900 (modify-syntax-entry ?\
) ")(" ada-mode-syntax-table
)
902 (setq ada-mode-symbol-syntax-table
(copy-syntax-table ada-mode-syntax-table
))
903 (modify-syntax-entry ?_
"w" ada-mode-symbol-syntax-table
)
906 ;; Support of special characters in XEmacs (see the comments at the beginning
907 ;; of the section on Grammar related functions).
909 (if (featurep 'xemacs
)
910 (defadvice parse-partial-sexp
(around parse-partial-sexp-protect-constants
)
911 "Handles special character constants and gnatprep statements."
915 (setq from to to tmp
)))
918 (while (re-search-forward "'\\([(\")#]\\)'" to t
)
919 (setq change
(cons (list (match-beginning 1)
923 (replace-match "'A'"))
925 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t
)
926 (setq change
(cons (list (match-beginning 1)
927 (length (match-string 1))
930 (replace-match (make-string (length (match-string 1)) ?
@))))
934 (goto-char (caar change
))
935 (delete-char (cadar change
))
936 (insert (caddar change
))
937 (setq change
(cdr change
)))))))
939 (unless (eval-when-compile (fboundp 'syntax-propertize-via-font-lock
))
940 ;; Before `syntax-propertize', we had to use font-lock to apply syntax-table
941 ;; properties, and in some cases we even had to do it manually (in
942 ;; `ada-after-change-function'). `ada-handle-syntax-table-properties'
943 ;; decides which method to use.
945 (defun ada-set-syntax-table-properties ()
946 "Assign `syntax-table' properties in accessible part of buffer.
947 In particular, character constants are said to be strings, #...#
948 are treated as numbers instead of gnatprep comments."
949 (let ((modified (buffer-modified-p))
951 (inhibit-read-only t
)
952 (inhibit-point-motion-hooks t
)
953 (inhibit-modification-hooks t
))
954 (remove-text-properties (point-min) (point-max) '(syntax-table nil
))
955 (goto-char (point-min))
956 (while (re-search-forward
957 ;; The following regexp was adapted from
958 ;; `ada-font-lock-syntactic-keywords'.
959 "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
961 (if (match-beginning 1)
963 (match-beginning 1) (match-end 1) 'syntax-table
'(11 . ?
\n))
965 (match-beginning 2) (match-end 2) 'syntax-table
'(7 . ?
'))
967 (match-beginning 3) (match-end 3) 'syntax-table
'(7 . ?
'))))
969 (restore-buffer-modified-p nil
))))
971 (defun ada-after-change-function (beg end old-len
)
972 "Called when the region between BEG and END was changed in the buffer.
973 OLD-LEN indicates what the length of the replaced text was."
976 (let ((from (progn (goto-char beg
) (line-beginning-position)))
977 (to (progn (goto-char end
) (line-end-position))))
978 (narrow-to-region from to
)
980 (ada-set-syntax-table-properties))))))
982 (defun ada-initialize-syntax-table-properties ()
983 "Assign `syntax-table' properties in current buffer."
988 (ada-set-syntax-table-properties))))
989 (add-hook 'after-change-functions
'ada-after-change-function nil t
))
991 (defun ada-handle-syntax-table-properties ()
992 "Handle `syntax-table' properties."
994 ;; `font-lock-mode' will take care of `syntax-table' properties.
995 (remove-hook 'after-change-functions
'ada-after-change-function t
)
996 ;; Take care of `syntax-table' properties manually.
997 (ada-initialize-syntax-table-properties)))
999 ) ;;(not (fboundp 'syntax-propertize))
1001 ;;------------------------------------------------------------------
1002 ;; Testing the grammatical context
1003 ;;------------------------------------------------------------------
1005 (defsubst ada-in-comment-p
(&optional parse-result
)
1006 "Return t if inside a comment.
1007 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1008 (nth 4 (or parse-result
1010 (line-beginning-position) (point)))))
1012 (defsubst ada-in-string-p
(&optional parse-result
)
1013 "Return t if point is inside a string.
1014 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1015 (nth 3 (or parse-result
1017 (line-beginning-position) (point)))))
1019 (defsubst ada-in-string-or-comment-p
(&optional parse-result
)
1020 "Return t if inside a comment or string.
1021 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1022 (setq parse-result
(or parse-result
1024 (line-beginning-position) (point))))
1025 (or (ada-in-string-p parse-result
) (ada-in-comment-p parse-result
)))
1027 (defsubst ada-in-numeric-literal-p
()
1028 "Return t if point is after a prefix of a numeric literal."
1029 (looking-back "\\([0-9]+#[0-9a-fA-F_]+\\)"))
1031 ;;------------------------------------------------------------------
1033 ;; The Ada mode comes with contextual menus, bound by default to the right
1035 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
1036 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
1037 ;; if the mouse button was pressed on an identifier.
1038 ;;------------------------------------------------------------------
1040 (defun ada-call-from-contextual-menu (function)
1041 "Execute FUNCTION when called from the contextual menu.
1042 It forces Emacs to change the cursor position."
1045 (setq ada-contextual-menu-last-point
1046 (list (point) (current-buffer))))
1048 (defun ada-popup-menu (position)
1049 "Pops up a contextual menu, depending on where the user clicked.
1050 POSITION is the location the mouse was clicked on.
1051 Sets `ada-contextual-menu-last-point' to the current position before
1052 displaying the menu. When a function from the menu is called, the
1053 point is where the mouse button was clicked."
1056 ;; declare this as a local variable, so that the function called
1057 ;; in the contextual menu does not hide the region in
1058 ;; transient-mark-mode.
1059 (let ((deactivate-mark nil
))
1060 (setq ada-contextual-menu-last-point
1061 (list (point) (current-buffer)))
1062 (mouse-set-point last-input-event
)
1064 (setq ada-contextual-menu-on-identifier
1066 (or (= (char-syntax (char-after)) ?w
)
1067 (= (char-after) ?_
))
1068 (not (ada-in-string-or-comment-p))
1069 (save-excursion (skip-syntax-forward "w")
1070 (not (ada-after-keyword-p)))
1072 (if (fboundp 'popup-menu
)
1073 (funcall (symbol-function 'popup-menu
) ada-contextual-menu
)
1075 (setq choice
(x-popup-menu position ada-contextual-menu
))
1077 (funcall (lookup-key ada-contextual-menu
(vector (car choice
)))))))
1079 (set-buffer (cadr ada-contextual-menu-last-point
))
1080 (goto-char (car ada-contextual-menu-last-point
))
1084 ;;------------------------------------------------------------------
1086 ;;------------------------------------------------------------------
1089 (defun ada-add-extensions (spec body
)
1090 "Define SPEC and BODY as being valid extensions for Ada files.
1091 Going from body to spec with `ff-find-other-file' used these
1093 SPEC and BODY are two regular expressions that must match against
1095 (let* ((reg (concat (regexp-quote body
) "$"))
1096 (tmp (assoc reg ada-other-file-alist
)))
1098 (setcdr tmp
(list (cons spec
(cadr tmp
))))
1099 (add-to-list 'ada-other-file-alist
(list reg
(list spec
)))))
1101 (let* ((reg (concat (regexp-quote spec
) "$"))
1102 (tmp (assoc reg ada-other-file-alist
)))
1104 (setcdr tmp
(list (cons body
(cadr tmp
))))
1105 (add-to-list 'ada-other-file-alist
(list reg
(list body
)))))
1107 (add-to-list 'auto-mode-alist
1108 (cons (concat (regexp-quote spec
) "\\'") 'ada-mode
))
1109 (add-to-list 'auto-mode-alist
1110 (cons (concat (regexp-quote body
) "\\'") 'ada-mode
))
1112 (add-to-list 'ada-spec-suffixes spec
)
1113 (add-to-list 'ada-body-suffixes body
)
1115 ;; Support for speedbar (Specifies that we want to see these files in
1117 (if (fboundp 'speedbar-add-supported-extension
)
1119 (funcall (symbol-function 'speedbar-add-supported-extension
)
1121 (funcall (symbol-function 'speedbar-add-supported-extension
)
1124 (defvar ada-font-lock-syntactic-keywords
) ; defined below
1127 (define-derived-mode ada-mode prog-mode
"Ada"
1128 "Ada mode is the major mode for editing Ada code."
1130 ;; Set the paragraph delimiters so that one can select a whole block
1132 (set (make-local-variable 'paragraph-start
) "[ \t\n\f]*$")
1133 (set (make-local-variable 'paragraph-separate
) "[ \t\n\f]*$")
1135 ;; comment end must be set because it may hold a wrong value if
1136 ;; this buffer had been in another mode before. RE
1137 (set (make-local-variable 'comment-end
) "")
1139 ;; used by autofill and indent-new-comment-line
1140 (set (make-local-variable 'comment-start-skip
) "---*[ \t]*")
1142 ;; used by autofill to break a comment line and continue it on another line.
1143 ;; The reason we need this one is that the default behavior does not work
1144 ;; correctly with the definition of paragraph-start above when the comment
1145 ;; is right after a multi-line subprogram declaration (the comments are
1146 ;; aligned under the latest parameter, not under the declaration start).
1147 (set (make-local-variable 'comment-line-break-function
)
1148 (lambda (&optional soft
) (let ((fill-prefix nil
))
1149 (indent-new-comment-line soft
))))
1151 (set (make-local-variable 'indent-line-function
)
1152 'ada-indent-current-function
)
1154 (set (make-local-variable 'comment-column
) 40)
1156 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1157 ;; the comment and the text. We do not want any, this is already
1158 ;; included in comment-start
1159 (unless (featurep 'xemacs
)
1160 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
1161 (set (make-local-variable 'comment-padding
) 0)
1162 (set (make-local-variable 'parse-sexp-lookup-properties
) t
))
1164 (setq case-fold-search t
)
1165 (if (boundp 'imenu-case-fold-search
)
1166 (setq imenu-case-fold-search t
))
1168 (set (make-local-variable 'fill-paragraph-function
)
1169 'ada-fill-comment-paragraph
)
1171 ;; Support for compile.el
1172 ;; We just substitute our own functions to go to the error.
1173 (add-hook 'compilation-mode-hook
1175 ;; FIXME: This has global impact! -stef
1176 (define-key compilation-minor-mode-map
[mouse-2
]
1177 'ada-compile-mouse-goto-error
)
1178 (define-key compilation-minor-mode-map
"\C-c\C-c"
1179 'ada-compile-goto-error
)
1180 (define-key compilation-minor-mode-map
"\C-m"
1181 'ada-compile-goto-error
)))
1183 ;; font-lock support :
1185 (set (make-local-variable 'font-lock-defaults
)
1186 '(ada-font-lock-keywords
1188 ((?\_ .
"w") (?
# .
"."))
1191 (if (eval-when-compile (fboundp 'syntax-propertize-via-font-lock
))
1192 (set (make-local-variable 'syntax-propertize-function
)
1193 (syntax-propertize-via-font-lock ada-font-lock-syntactic-keywords
))
1194 (set (make-local-variable 'font-lock-syntactic-keywords
)
1195 ada-font-lock-syntactic-keywords
))
1197 ;; Set up support for find-file.el.
1198 (set (make-local-variable 'ff-other-file-alist
)
1199 'ada-other-file-alist
)
1200 (set (make-local-variable 'ff-search-directories
)
1201 'ada-search-directories-internal
)
1202 (setq ff-post-load-hook
'ada-set-point-accordingly
1203 ff-file-created-hook
'ada-make-body
)
1204 (add-hook 'ff-pre-load-hook
'ada-which-function-are-we-in
)
1206 (make-local-variable 'ff-special-constructs
)
1207 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair
))
1209 ;; Top level child package declaration; go to the parent package.
1210 (cons (eval-when-compile
1211 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1213 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1216 ada-search-directories-internal
1217 (ada-make-filename-from-adaname (match-string 3))
1218 ada-spec-suffixes
)))
1220 ;; A "separate" clause.
1221 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1224 ada-search-directories-internal
1225 (ada-make-filename-from-adaname (match-string 1))
1226 ada-spec-suffixes
)))
1229 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1232 ada-search-directories-internal
1233 (ada-make-filename-from-adaname (match-string 1))
1234 ada-spec-suffixes
)))
1237 ;; Support for outline-minor-mode
1238 (set (make-local-variable 'outline-regexp
)
1239 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1240 (set (make-local-variable 'outline-level
) 'ada-outline-level
)
1242 ;; Support for imenu : We want a sorted index
1243 (setq imenu-generic-expression ada-imenu-generic-expression
)
1245 (setq imenu-sort-function
'imenu--sort-by-name
)
1247 ;; Support for ispell : Check only comments
1248 (set (make-local-variable 'ispell-check-comments
) 'exclusive
)
1250 ;; Support for align
1251 (add-to-list 'align-dq-string-modes
'ada-mode
)
1252 (add-to-list 'align-open-comment-modes
'ada-mode
)
1253 (set (make-local-variable 'align-region-separate
) ada-align-region-separate
)
1255 ;; Exclude comments alone on line from alignment.
1256 (add-to-list 'align-exclude-rules-list
1258 (regexp .
"^\\(\\s-*\\)--")
1259 (modes .
'(ada-mode))))
1260 (add-to-list 'align-exclude-rules-list
1262 (regexp .
"^\\(\\s-*\\)\\<use\\>")
1263 (modes .
'(ada-mode))))
1265 (setq ada-align-modes nil
)
1267 (add-to-list 'ada-align-modes
1268 '(ada-declaration-assign
1269 (regexp .
"[^:]\\(\\s-*\\):[^:]")
1270 (valid .
(lambda() (not (ada-in-comment-p))))
1272 (modes .
'(ada-mode))))
1273 (add-to-list 'ada-align-modes
1275 (regexp .
"[^=]\\(\\s-*\\)=>")
1276 (valid .
(lambda() (not (ada-in-comment-p))))
1277 (modes .
'(ada-mode))))
1278 (add-to-list 'ada-align-modes
1280 (regexp .
"\\(\\s-*\\)--")
1281 (modes .
'(ada-mode))))
1282 (add-to-list 'ada-align-modes
1284 (regexp .
"\\(\\s-*\\)\\<use\\s-")
1285 (valid .
(lambda() (not (ada-in-comment-p))))
1286 (modes .
'(ada-mode))))
1287 (add-to-list 'ada-align-modes
1289 (regexp .
"\\(\\s-+\\)at\\>")
1290 (modes .
'(ada-mode))))
1292 (setq align-mode-rules-list ada-align-modes
)
1294 ;; Set up the contextual menu
1296 (define-key ada-mode-map ada-popup-key
'ada-popup-menu
))
1298 ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
1299 (setq local-abbrev-table ada-mode-abbrev-table
)
1301 ;; Support for which-function mode
1302 (set (make-local-variable 'which-func-functions
) '(ada-which-function))
1304 ;; Support for indent-new-comment-line (Especially for XEmacs)
1305 (set (make-local-variable 'comment-multi-line
) nil
)
1307 ;; Support for add-log
1308 (set (make-local-variable 'add-log-current-defun-function
)
1309 'ada-which-function
)
1311 (easy-menu-add ada-mode-menu ada-mode-map
)
1313 (set (make-local-variable 'skeleton-further-elements
)
1314 '((< '(backward-delete-char-untabify
1315 (min ada-indent
(current-column))))))
1316 (add-hook 'skeleton-end-hook
'ada-adjust-case-skeleton nil t
)
1318 ;; To be run after the hook, in case the user modified
1319 ;; ada-fill-comment-prefix
1320 (add-hook 'hack-local-variables-hook
1322 (set (make-local-variable 'comment-start
)
1323 (or ada-fill-comment-prefix
"-- "))
1325 ;; Run this after the hook to give the users a chance
1326 ;; to activate font-lock-mode.
1328 (unless (or (eval-when-compile (fboundp 'syntax-propertize-via-font-lock
))
1330 (ada-initialize-syntax-table-properties)
1331 (add-hook 'font-lock-mode-hook
1332 'ada-handle-syntax-table-properties nil t
))
1334 ;; FIXME: ada-language-version might be set in the mode
1335 ;; hook or it might even be set later on via file-local
1336 ;; vars, so ada-keywords should be set lazily.
1337 (cond ((eq ada-language-version
'ada83
)
1338 (setq ada-keywords ada-83-keywords
))
1339 ((eq ada-language-version
'ada95
)
1340 (setq ada-keywords ada-95-keywords
))
1341 ((eq ada-language-version
'ada2005
)
1342 (setq ada-keywords ada-2005-keywords
)))
1345 (ada-activate-keys-for-case)))
1348 (defun ada-adjust-case-skeleton ()
1349 "Adjust the case of the text inserted by a skeleton."
1351 (let ((aa-end (point)))
1352 (ada-adjust-case-region
1353 (progn (goto-char (symbol-value 'beg
)) (forward-word -
1) (point))
1354 (goto-char aa-end
)))))
1356 (defun ada-region-selected ()
1357 "Should we operate on an active region?"
1358 (if (fboundp 'use-region-p
)
1362 ;;-----------------------------------------------------------------
1364 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1365 ;; functions to auto-case identifiers, keywords, ...
1366 ;; The basic rules for autocasing are defined through the variables
1367 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1368 ;; are references to the functions that will do the actual casing.
1370 ;; However, in most cases, the user will want to define some exceptions to
1371 ;; these casing rules. This is done through a list of files, that contain
1372 ;; one word per line. These files are stored in `ada-case-exception-file'.
1373 ;; For backward compatibility, this variable can also be a string.
1374 ;;-----------------------------------------------------------------
1376 (defun ada-save-exceptions-to-file (file-name)
1377 "Save the casing exception lists to the file FILE-NAME.
1378 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1379 (find-file (expand-file-name file-name
))
1381 (mapc (lambda (x) (insert (car x
) "\n"))
1382 (sort (copy-sequence ada-case-exception
)
1383 (lambda(a b
) (string< (car a
) (car b
)))))
1384 (mapc (lambda (x) (insert "*" (car x
) "\n"))
1385 (sort (copy-sequence ada-case-exception-substring
)
1386 (lambda(a b
) (string< (car a
) (car b
)))))
1391 (defun ada-create-case-exception (&optional word
)
1392 "Define WORD as an exception for the casing system.
1393 If WORD is not given, then the current word in the buffer is used instead.
1394 The new word is added to the first file in `ada-case-exception-file'.
1395 The standard casing rules will no longer apply to this word."
1398 (cond ((stringp ada-case-exception-file
)
1399 ada-case-exception-file
)
1400 ((listp ada-case-exception-file
)
1401 (car ada-case-exception-file
))
1403 (error (concat "No exception file specified. "
1404 "See variable ada-case-exception-file"))))))
1407 (with-syntax-table ada-mode-symbol-syntax-table
1409 (skip-syntax-backward "w")
1410 (setq word
(buffer-substring-no-properties
1411 (point) (save-excursion (forward-word 1) (point)))))))
1413 ;; Reread the exceptions file, in case it was modified by some other,
1414 (ada-case-read-exceptions-from-file file-name
)
1416 ;; If the word is already in the list, even with a different casing
1417 ;; we simply want to replace it.
1418 (if (and (not (equal ada-case-exception
'()))
1419 (assoc-string word ada-case-exception t
))
1420 (setcar (assoc-string word ada-case-exception t
) word
)
1421 (add-to-list 'ada-case-exception
(cons word t
)))
1423 (ada-save-exceptions-to-file file-name
)))
1425 (defun ada-create-case-exception-substring (&optional word
)
1426 "Define the substring WORD as an exception for the casing system.
1427 If WORD is not given, then the current word in the buffer is used instead,
1428 or the selected region if any is active.
1429 The new word is added to the first file in `ada-case-exception-file'.
1430 When auto-casing a word, this substring will be special-cased, unless the
1431 word itself has a special casing."
1434 (cond ((stringp ada-case-exception-file
)
1435 ada-case-exception-file
)
1436 ((listp ada-case-exception-file
)
1437 (car ada-case-exception-file
))
1439 (error (concat "No exception file specified. "
1440 "See variable ada-case-exception-file"))))))
1442 ;; Find the substring to define as an exception. Order is: the parameter,
1443 ;; if any, or the selected region, or the word under the cursor
1447 ((ada-region-selected)
1448 (setq word
(buffer-substring-no-properties
1449 (region-beginning) (region-end))))
1452 (let ((underscore-syntax (char-syntax ?_
)))
1455 (modify-syntax-entry ?_
"." (syntax-table))
1457 (skip-syntax-backward "w")
1458 (setq word
(buffer-substring-no-properties
1460 (save-excursion (forward-word 1) (point))))))
1461 (modify-syntax-entry ?_
(make-string 1 underscore-syntax
)
1464 ;; Reread the exceptions file, in case it was modified by some other,
1465 (ada-case-read-exceptions-from-file file-name
)
1467 ;; If the word is already in the list, even with a different casing
1468 ;; we simply want to replace it.
1469 (if (and (not (equal ada-case-exception-substring
'()))
1470 (assoc-string word ada-case-exception-substring t
))
1471 (setcar (assoc-string word ada-case-exception-substring t
) word
)
1472 (add-to-list 'ada-case-exception-substring
(cons word t
))
1475 (ada-save-exceptions-to-file file-name
)
1477 (message "%s" (concat "Defining " word
" as a casing exception"))))
1479 (defun ada-case-read-exceptions-from-file (file-name)
1480 "Read the content of the casing exception file FILE-NAME."
1481 (if (file-readable-p (expand-file-name file-name
))
1482 (let ((buffer (current-buffer)))
1483 (find-file (expand-file-name file-name
))
1484 (set-syntax-table ada-mode-symbol-syntax-table
)
1486 (goto-char (point-min))
1489 ;; If the item is already in the list, even with an other casing,
1490 ;; do not add it again. This way, the user can easily decide which
1491 ;; priority should be applied to each casing exception
1492 (let ((word (buffer-substring-no-properties
1493 (point) (save-excursion (forward-word 1) (point)))))
1495 ;; Handling a substring ?
1496 (if (char-equal (string-to-char word
) ?
*)
1498 (setq word
(substring word
1))
1499 (unless (assoc-string word ada-case-exception-substring t
)
1500 (add-to-list 'ada-case-exception-substring
(cons word t
))))
1501 (unless (assoc-string word ada-case-exception t
)
1502 (add-to-list 'ada-case-exception
(cons word t
)))))
1506 (set-buffer buffer
)))
1509 (defun ada-case-read-exceptions ()
1510 "Read all the casing exception files from `ada-case-exception-file'."
1513 ;; Reinitialize the casing exception list
1514 (setq ada-case-exception
'()
1515 ada-case-exception-substring
'())
1517 (cond ((stringp ada-case-exception-file
)
1518 (ada-case-read-exceptions-from-file ada-case-exception-file
))
1520 ((listp ada-case-exception-file
)
1521 (mapcar 'ada-case-read-exceptions-from-file
1522 ada-case-exception-file
))))
1524 (defun ada-adjust-case-substring ()
1525 "Adjust case of substrings in the previous word."
1527 (let ((substrings ada-case-exception-substring
)
1529 (case-fold-search t
)
1530 (underscore-syntax (char-syntax ?_
))
1538 (modify-syntax-entry ?_
"." (syntax-table))
1541 (setq re
(concat "\\b" (regexp-quote (caar substrings
)) "\\b"))
1544 (while (re-search-forward re max t
)
1545 (replace-match (caar substrings
) t
)))
1546 (setq substrings
(cdr substrings
))
1549 (modify-syntax-entry ?_
(make-string 1 underscore-syntax
) (syntax-table)))
1552 (defun ada-adjust-case-identifier ()
1553 "Adjust case of the previous identifier.
1554 The auto-casing is done according to the value of `ada-case-identifier'
1555 and the exceptions defined in `ada-case-exception-file'."
1557 (if (or (equal ada-case-exception
'())
1558 (equal (char-after) ?_
))
1560 (funcall ada-case-identifier -
1)
1561 (ada-adjust-case-substring))
1565 (start (save-excursion (skip-syntax-backward "w")
1568 ;; If we have an exception, replace the word by the correct casing
1569 (if (setq match
(assoc-string (buffer-substring start end
)
1570 ada-case-exception t
))
1573 (delete-region start end
)
1574 (insert (car match
)))
1576 ;; Else simply re-case the word
1577 (funcall ada-case-identifier -
1)
1578 (ada-adjust-case-substring))))))
1580 (defun ada-after-keyword-p ()
1581 "Return t if cursor is after a keyword that is not an attribute."
1584 (and (not (and (char-before)
1585 (or (= (char-before) ?_
)
1586 (= (char-before) ?
'))));; unless we have a _ or '
1587 (looking-at (concat ada-keywords
"[^_]")))))
1589 (defun ada-adjust-case (&optional force-identifier
)
1590 "Adjust the case of the word before the character just typed.
1591 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1595 (if (and (not (bobp))
1596 ;; or if at the end of a character constant
1597 (not (and (eq (following-char) ?
')
1598 (eq (char-before (1- (point))) ?
')))
1599 ;; or if the previous character was not part of a word
1600 (eq (char-syntax (char-before)) ?w
)
1601 ;; if in a string or a comment
1602 (not (ada-in-string-or-comment-p))
1603 ;; if in a numeric literal
1604 (not (ada-in-numeric-literal-p))
1608 (or (= (point) (point-min))
1610 (= (following-char) ?
'))
1611 (funcall ada-case-attribute -
1)
1613 (not force-identifier
) ; (MH)
1614 (ada-after-keyword-p))
1615 (funcall ada-case-keyword -
1)
1616 (ada-adjust-case-identifier))))
1621 (defun ada-adjust-case-interactive (arg)
1622 "Adjust the case of the previous word, and process the character just typed.
1623 ARG is the prefix the user entered with \\[universal-argument]."
1627 (let ((lastk last-command-event
))
1629 (with-syntax-table ada-mode-symbol-syntax-table
1630 (cond ((or (eq lastk ?
\n)
1635 ;; horrible dekludge
1637 ;; some special keys and their bindings
1640 (funcall ada-lfd-binding
))
1642 (funcall ada-ret-binding
))))
1643 ((eq lastk ?\C-i
) (ada-tab))
1644 ;; Else just insert the character
1645 ((self-insert-command (prefix-numeric-value arg
))))
1646 ;; if there is a keyword in front of the underscore
1647 ;; then it should be part of an identifier (MH)
1650 (ada-adjust-case))))
1652 ;; Else, no auto-casing
1654 ((eq last-command-event ?
\n)
1655 (funcall ada-lfd-binding
))
1656 ((eq last-command-event ?
\r)
1657 (funcall ada-ret-binding
))
1659 (self-insert-command (prefix-numeric-value arg
))))))
1661 (defun ada-activate-keys-for-case ()
1662 ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
1663 "Modify the key bindings for all the keys that should readjust the casing."
1665 ;; Save original key-bindings to allow swapping ret/lfd
1666 ;; when casing is activated.
1667 ;; The 'or ...' is there to be sure that the value will not
1668 ;; be changed again when Ada mode is called more than once
1669 (or ada-ret-binding
(setq ada-ret-binding
(key-binding "\C-M")))
1670 (or ada-lfd-binding
(setq ada-lfd-binding
(key-binding "\C-j")))
1672 ;; Call case modifying function after certain keys.
1673 (mapcar (function (lambda(key) (define-key
1675 (char-to-string key
)
1676 'ada-adjust-case-interactive
)))
1677 '( ?
` ?_ ?
# ?% ?
& ?
* ?
( ?
) ?- ?
= ?
+
1678 ?| ?\
; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1680 (defun ada-loose-case-word (&optional arg
)
1681 "Upcase first letter and letters following `_' in the following word.
1682 No other letter is modified.
1683 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1686 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1688 (skip-syntax-backward "w")
1689 (while (and (or first
(search-forward "_" end t
))
1693 (insert-char (upcase (following-char)) 1)
1696 (defun ada-no-auto-case (&optional arg
)
1697 "Do nothing. ARG is ignored.
1698 This function can be used for the auto-casing variables in Ada mode, to
1699 adapt to unusal auto-casing schemes. Since it does nothing, you can for
1700 instance use it for `ada-case-identifier' if you don't want any special
1701 auto-casing for identifiers, whereas keywords have to be lower-cased.
1702 See also `ada-auto-case' to disable auto casing altogether."
1705 (defun ada-capitalize-word (&optional arg
)
1706 "Upcase first letter and letters following '_', lower case other letters.
1707 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1709 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1710 (begin (save-excursion (skip-syntax-backward "w") (point))))
1711 (modify-syntax-entry ?_
"_")
1712 (capitalize-region begin end
)
1713 (modify-syntax-entry ?_
"w")))
1715 (defun ada-adjust-case-region (from to
)
1716 "Adjust the case of all words in the region between FROM and TO.
1717 Attention: This function might take very long for big regions!"
1723 (message "Adjusting case ...")
1724 (with-syntax-table ada-mode-symbol-syntax-table
1728 ;; loop: look for all identifiers, keywords, and attributes
1730 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t
)
1731 (setq end
(match-end 1))
1733 (and (> (point) from
)
1736 (setq attribp
(looking-at "'.[^']")))))
1738 ;; do nothing if it is a string or comment
1739 (ada-in-string-or-comment-p)
1742 ;; get the identifier or keyword or attribute
1744 (setq begin
(point))
1745 (setq keywordp
(looking-at ada-keywords
))
1748 ;; casing according to user-option
1751 (funcall ada-case-attribute -
1)
1753 (funcall ada-case-keyword -
1)
1754 (ada-adjust-case-identifier)))
1755 (goto-char begin
))))
1756 (message "Adjusting case ... Done")))))
1758 (defun ada-adjust-case-buffer ()
1759 "Adjust the case of all words in the whole buffer.
1760 ATTENTION: This function might take very long for big buffers!"
1762 (ada-adjust-case-region (point-min) (point-max)))
1765 ;;--------------------------------------------------------------
1766 ;; Format Parameter Lists
1767 ;; Some special algorithms are provided to indent the parameter lists in
1768 ;; subprogram declarations. This is done in two steps:
1769 ;; - First parses the parameter list. The returned list has the following
1771 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1773 ;; This is done in `ada-scan-paramlist'.
1774 ;; - Delete and recreate the parameter list in function
1775 ;; `ada-insert-paramlist'.
1776 ;; Both steps are called from `ada-format-paramlist'.
1777 ;; Note: Comments inside the parameter list are lost.
1778 ;; The syntax has to be correct, or the reformating will fail.
1779 ;;--------------------------------------------------------------
1781 (defun ada-format-paramlist ()
1782 "Reformat the parameter list point is in."
1788 (with-syntax-table ada-mode-symbol-syntax-table
1790 ;; check if really inside parameter list
1791 (or (ada-in-paramlist-p)
1792 (error "Not in parameter list"))
1794 ;; find start of current parameter-list
1795 (ada-search-ignore-string-comment
1796 (concat ada-subprog-start-re
"\\|\\<body\\>" ) t nil
)
1799 (setq begin
(point))
1801 ;; find end of parameter-list
1803 (setq delend
(point))
1807 ;; find end of last parameter-declaration
1808 (forward-comment -
1000)
1811 ;; build a list of all elements of the parameter-list
1812 (setq paramlist
(ada-scan-paramlist (1+ begin
) end
))
1814 ;; delete the original parameter-list
1815 (delete-region begin delend
)
1817 ;; insert the new parameter-list
1819 (ada-insert-paramlist paramlist
))))
1821 (defun ada-scan-paramlist (begin end
)
1822 "Scan the parameter list found in between BEGIN and END.
1823 Return the equivalent internal parameter list."
1824 (let ((paramlist (list))
1834 ;; loop until end of last parameter
1837 ;; find first character of parameter-declaration
1838 (ada-goto-next-non-ws)
1841 ;; find last character of parameter-declaration
1842 (if (setq match-cons
1843 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t
))
1845 (setq epos
(car match-cons
))
1846 (setq semipos
(cdr match-cons
)))
1849 ;; read name(s) of parameter(s)
1851 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1853 (setq param
(list (match-string 1)))
1854 (ada-search-ignore-string-comment ":" nil epos t
'search-forward
)
1862 (ada-search-ignore-string-comment
1863 "in" nil epos t
'word-search-forward
)))))
1871 (ada-search-ignore-string-comment
1872 "out" nil epos t
'word-search-forward
)))))
1874 ;; look for 'access'
1880 (ada-search-ignore-string-comment
1881 "access" nil epos t
'word-search-forward
)))))
1883 ;; skip 'in'/'out'/'access'
1885 (ada-goto-next-non-ws)
1886 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1888 (ada-goto-next-non-ws))
1890 ;; read type of parameter
1891 ;; We accept spaces in the name, since some software like Rose
1892 ;; generates something like: "A : B 'Class"
1893 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1896 (list (match-string 0))))
1898 ;; read default-expression, if there is one
1899 (goto-char (setq apos
(match-end 0)))
1903 (if (setq match-cons
1904 (ada-search-ignore-string-comment
1905 ":=" nil epos t
'search-forward
))
1906 (buffer-substring (car match-cons
) epos
)
1909 ;; add this parameter-declaration to the list
1910 (setq paramlist
(append paramlist
(list param
)))
1912 ;; check if it was the last parameter
1915 (goto-char semipos
))
1917 (reverse paramlist
)))
1919 (defun ada-insert-paramlist (paramlist)
1920 "Insert a formatted PARAMLIST in the buffer."
1921 (let ((i (length paramlist
))
1930 ;; loop until last parameter
1931 (while (not (zerop i
))
1934 ;; get max length of parameter-name
1935 (setq parlen
(max parlen
(length (nth 0 (nth i paramlist
)))))
1937 ;; get max length of type-name
1938 (setq typlen
(max typlen
(length (nth 4 (nth i paramlist
)))))
1940 ;; is there any 'in' ?
1941 (setq inp
(or inp
(nth 1 (nth i paramlist
))))
1943 ;; is there any 'out' ?
1944 (setq outp
(or outp
(nth 2 (nth i paramlist
))))
1946 ;; is there any 'access' ?
1947 (setq accessp
(or accessp
(nth 3 (nth i paramlist
))))
1950 ;; does paramlist already start on a separate line ?
1952 (re-search-backward "^.\\|[^ \t]" nil t
)
1954 ;; yes => re-indent it
1956 (ada-indent-current)
1958 (if (looking-at "\\(is\\|return\\)")
1959 (replace-match " \\1"))))
1961 ;; no => insert it where we are after removing any whitespace
1965 ((looking-at "[ \t]*\\(\n\\|;\\)")
1966 (replace-match "\\1"))
1967 ((looking-at "[ \t]*\\(is\\|return\\)")
1968 (replace-match " \\1"))))
1972 (ada-indent-current)
1974 (setq firstcol
(current-column))
1975 (setq i
(length paramlist
))
1977 ;; loop until last parameter
1978 (while (not (zerop i
))
1980 (setq column firstcol
)
1982 ;; insert parameter-name, space and colon
1983 (insert (nth 0 (nth i paramlist
)))
1984 (indent-to (+ column parlen
1))
1986 (setq column
(current-column))
1988 ;; insert 'in' or space
1989 (if (nth 1 (nth i paramlist
))
1994 (not (nth 3 (nth i paramlist
))))
1997 ;; insert 'out' or space
1998 (if (nth 2 (nth i paramlist
))
2003 (not (nth 3 (nth i paramlist
))))
2007 (if (nth 3 (nth i paramlist
))
2010 (setq column
(current-column))
2012 ;; insert type-name and, if necessary, space and default-expression
2013 (insert (nth 4 (nth i paramlist
)))
2014 (if (nth 5 (nth i paramlist
))
2016 (indent-to (+ column typlen
1))
2017 (insert (nth 5 (nth i paramlist
)))))
2019 ;; check if it was the last parameter
2022 ;; no => insert ';' and newline and indent
2025 (indent-to firstcol
))
2028 ;; if anything follows, except semicolon, newline, is or return
2029 ;; put it in a new line and indent it
2030 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2031 (ada-indent-newline-indent))
2036 ;;;----------------------------------------------------------------
2037 ;; Indentation Engine
2038 ;; All indentations are indicated as a two-element string:
2039 ;; - position of reference in the buffer
2040 ;; - offset to indent from this position (can also be a symbol or a list
2041 ;; that are evaluated)
2042 ;; Thus the total indentation for a line is the column number of the reference
2043 ;; position plus whatever value the evaluation of the second element provides.
2044 ;; This mechanism is used so that the Ada mode can "explain" how the
2045 ;; indentation was calculated, by showing which variables were used.
2047 ;; The indentation itself is done in only one pass: first we try to guess in
2048 ;; what context we are by looking at the following keyword or punctuation
2049 ;; sign. If nothing remarkable is found, just try to guess the indentation
2050 ;; based on previous lines.
2052 ;; The relevant functions for indentation are:
2053 ;; - `ada-indent-region': Re-indent a region of text
2054 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2055 ;; calculation that were done
2056 ;; - `ada-indent-current': Re-indent the current line
2057 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2058 ;; based on the context (see above).
2059 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2060 ;; For efficiency, these functions do not check they are in the correct
2062 ;;;----------------------------------------------------------------
2064 (defun ada-indent-region (beg end
)
2065 "Indent the region between BEG end END."
2068 (let ((block-done 0)
2069 (lines-remaining (count-lines beg end
))
2070 (msg (format "%%4d out of %4d lines remaining ..."
2071 (count-lines beg end
)))
2072 (endmark (copy-marker end
)))
2073 ;; catch errors while indenting
2074 (while (< (point) endmark
)
2075 (if (> block-done
39)
2077 (setq lines-remaining
(- lines-remaining block-done
)
2079 (message msg lines-remaining
)))
2080 (if (= (char-after) ?
\n) nil
2081 (ada-indent-current))
2083 (setq block-done
(1+ block-done
)))
2084 (message "Indenting ... done")))
2086 (defun ada-indent-newline-indent ()
2087 "Indent the current line, insert a newline and then indent the new line."
2089 (ada-indent-current)
2091 (ada-indent-current))
2093 (defun ada-indent-newline-indent-conditional ()
2094 "Insert a newline and indent it.
2095 The original line is re-indented if `ada-indent-after-return' is non-nil."
2097 ;; If at end of buffer (entering brand new code), some indentation
2098 ;; fails. For example, a block label requires whitespace following
2099 ;; the : to be recognized. So we do the newline first, then
2100 ;; go back and indent the original line.
2102 (if ada-indent-after-return
2105 (ada-indent-current)
2107 (ada-indent-current))
2109 (defun ada-justified-indent-current ()
2110 "Indent the current line and explain how the calculation was done."
2113 (let ((cur-indent (ada-indent-current)))
2115 (let ((line (save-excursion
2116 (goto-char (car cur-indent
))
2117 (count-lines 1 (point)))))
2119 (if (equal (cdr cur-indent
) '(0))
2120 (message (concat "same indentation as line " (number-to-string line
)))
2121 (message "%s" (mapconcat (lambda(x)
2126 (number-to-string x
))
2128 (concat "- " (symbol-name (cadr x
))))
2133 (goto-char (car cur-indent
))
2136 (defun ada-batch-reformat ()
2137 "Re-indent and re-case all the files found on the command line.
2138 This function should be used from the command line, with a
2140 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2142 (while command-line-args-left
2143 (let ((source (car command-line-args-left
)))
2144 (message "Formating %s" source
)
2146 (ada-indent-region (point-min) (point-max))
2147 (ada-adjust-case-buffer)
2148 (write-file source
))
2149 (setq command-line-args-left
(cdr command-line-args-left
)))
2153 (defsubst ada-goto-previous-word
()
2154 "Move point to the beginning of the previous word of Ada code.
2155 Return the new position of point or nil if not found."
2156 (ada-goto-next-word t
))
2158 (defun ada-indent-current ()
2159 "Indent current line as Ada code.
2160 Return the calculation that was done, including the reference point
2163 (let ((orgpoint (point-marker))
2164 cur-indent tmp-indent
2168 (with-syntax-table ada-mode-symbol-syntax-table
2170 ;; This need to be done here so that the advice is not always
2171 ;; activated (this might interact badly with other modes)
2172 (if (featurep 'xemacs
)
2173 (ad-activate 'parse-partial-sexp t
))
2178 ;; Not First line in the buffer ?
2179 (if (save-excursion (zerop (forward-line -
1)))
2181 (back-to-indentation)
2182 (ada-get-current-indent))
2184 ;; first line in the buffer
2185 (list (point-min) 0))))
2187 ;; Evaluate the list to get the column to indent to
2188 ;; prev-indent contains the column to indent to
2190 (setq prev-indent
(save-excursion (goto-char (car cur-indent
))
2192 tmp-indent
(cdr cur-indent
))
2193 (setq prev-indent
0 tmp-indent
'()))
2195 (while (not (null tmp-indent
))
2197 ((numberp (car tmp-indent
))
2198 (setq prev-indent
(+ prev-indent
(car tmp-indent
))))
2200 (setq prev-indent
(+ prev-indent
(eval (car tmp-indent
)))))
2202 (setq tmp-indent
(cdr tmp-indent
)))
2204 ;; only re-indent if indentation is different then the current
2205 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent
)
2208 (delete-horizontal-space)
2209 (indent-to prev-indent
))
2211 ;; restore position of point
2213 (goto-char orgpoint
)
2214 (if (< (current-column) (current-indentation))
2215 (back-to-indentation)))
2217 (if (featurep 'xemacs
)
2218 (ad-deactivate 'parse-partial-sexp
)))
2222 (defun ada-get-current-indent ()
2223 "Return the indentation to use for the current line."
2228 (orgpoint (save-excursion
2230 (forward-comment -
10000)
2237 ;;-----------------------------
2238 ;; in open parenthesis, but not in parameter-list
2239 ;;-----------------------------
2241 ((and ada-indent-to-open-paren
2242 (not (ada-in-paramlist-p))
2243 (setq column
(ada-in-open-paren-p)))
2245 ;; check if we have something like this (Table_Component_Type =>
2246 ;; Source_File_Record)
2249 ;; Align the closing parenthesis on the opening one
2250 (if (= (following-char) ?\
))
2253 (skip-chars-backward " \t")
2254 (list (1- (point)) 0))
2256 (if (and (skip-chars-backward " \t")
2257 (= (char-before) ?
\n)
2258 (not (forward-comment -
10000))
2259 (= (char-before) ?
>))
2260 ;; ??? Could use a different variable
2261 (list column
'ada-broken-indent
)
2263 ;; We want all continuation lines to be indented the same
2264 ;; (ada-broken-line from the opening parenthesis. However, in
2265 ;; parameter list, each new parameter should be indented at the
2266 ;; column as the opening parenthesis.
2268 ;; A special case to handle nested boolean expressions, as in
2270 ;; and then C) -- indented by ada-broken-indent
2271 ;; or else D) -- indenting this line.
2272 ;; ??? This is really a hack, we should have a proper way to go to
2273 ;; ??? the beginning of the statement
2275 (if (= (char-before) ?\
))
2278 (if (memq (char-before) '(?
, ?\
; ?\( ?\)))
2280 (list column
'ada-continuation-indent
)
2283 ;;---------------------------
2285 ;;---------------------------
2288 (ada-indent-on-previous-lines nil orgpoint orgpoint
))
2290 ;;---------------------------
2292 ;;---------------------------
2294 ((= (downcase (char-after)) ?e
)
2297 ;; ------- end ------
2299 ((looking-at "end\\>")
2303 (ada-goto-matching-start 1)
2306 ;; found 'loop' => skip back to 'while' or 'for'
2307 ;; if 'loop' is not on a separate line
2308 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2312 (looking-at ".+\\<loop\\>"))
2315 (setq limit
(car (ada-search-ignore-string-comment ";" t
))))
2319 (ada-search-ignore-string-comment ada-loop-start-re t limit
))
2320 (not (looking-at "\\<loop\\>"))))
2322 (goto-char (car match-cons
))
2324 (back-to-indentation)
2325 (if (looking-at ada-block-label-re
)
2326 (setq label
(- ada-label-indent
))))))))
2328 ;; found 'record' =>
2329 ;; if the keyword is found at the beginning of a line (or just
2330 ;; after limited, we indent on it, otherwise we indent on the
2331 ;; beginning of the type declaration)
2332 ;; type A is (B : Integer;
2333 ;; C : Integer) is record
2334 ;; end record; -- This is badly indented otherwise
2335 (if (looking-at "record")
2338 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2339 (list (save-excursion (back-to-indentation) (point)) 0)
2340 (list (save-excursion
2341 (car (ada-search-ignore-string-comment "\\<type\\>" t
)))
2344 ;; Else keep the same indentation as the beginning statement
2345 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0)))))
2347 ;; ------ exception ----
2349 ((looking-at "exception\\>")
2351 (ada-goto-matching-start 1)
2352 (list (save-excursion (back-to-indentation) (point)) 0)))
2356 ((looking-at "else\\>")
2357 (if (save-excursion (ada-goto-previous-word)
2358 (looking-at "\\<or\\>"))
2359 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2361 (ada-goto-matching-start 1 nil t
)
2362 (list (progn (back-to-indentation) (point)) 0))))
2366 ((looking-at "elsif\\>")
2368 (ada-goto-matching-start 1 nil t
)
2369 (list (progn (back-to-indentation) (point)) 0)))
2373 ;;---------------------------
2374 ;; starting with w (when)
2375 ;;---------------------------
2377 ((and (= (downcase (char-after)) ?w
)
2378 (looking-at "when\\>"))
2380 (ada-goto-matching-start 1)
2381 (list (save-excursion (back-to-indentation) (point))
2384 ;;---------------------------
2385 ;; starting with t (then)
2386 ;;---------------------------
2388 ((and (= (downcase (char-after)) ?t
)
2389 (looking-at "then\\>"))
2390 (if (save-excursion (ada-goto-previous-word)
2391 (looking-at "and\\>"))
2392 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2394 ;; Select has been added for the statement: "select ... then abort"
2395 (ada-search-ignore-string-comment
2396 "\\<\\(elsif\\|if\\|select\\)\\>" t nil
)
2397 (list (progn (back-to-indentation) (point))
2398 'ada-stmt-end-indent
))))
2400 ;;---------------------------
2401 ;; starting with l (loop)
2402 ;;---------------------------
2404 ((and (= (downcase (char-after)) ?l
)
2405 (looking-at "loop\\>"))
2408 (goto-char (match-end 0))
2409 (ada-goto-stmt-start)
2410 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2411 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2412 (unless (looking-at ada-loop-start-re
)
2413 (ada-search-ignore-string-comment ada-loop-start-re
2415 (if (looking-at "\\<loop\\>")
2416 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2417 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent
)))))
2419 ;;----------------------------
2420 ;; starting with l (limited) or r (record)
2421 ;;----------------------------
2423 ((or (and (= (downcase (char-after)) ?l
)
2424 (looking-at "limited\\>"))
2425 (and (= (downcase (char-after)) ?r
)
2426 (looking-at "record\\>")))
2429 (ada-search-ignore-string-comment
2430 "\\<\\(type\\|use\\)\\>" t nil
)
2431 (if (looking-at "\\<use\\>")
2432 (ada-search-ignore-string-comment "for" t nil nil
2433 'word-search-backward
))
2434 (list (progn (back-to-indentation) (point))
2435 'ada-indent-record-rel-type
)))
2437 ;;---------------------------
2438 ;; starting with b (begin)
2439 ;;---------------------------
2441 ((and (= (downcase (char-after)) ?b
)
2442 (looking-at "begin\\>"))
2444 (if (ada-goto-decl-start t
)
2445 (list (progn (back-to-indentation) (point)) 0)
2446 (ada-indent-on-previous-lines nil orgpoint orgpoint
))))
2448 ;;---------------------------
2449 ;; starting with i (is)
2450 ;;---------------------------
2452 ((and (= (downcase (char-after)) ?i
)
2453 (looking-at "is\\>"))
2455 (if (and ada-indent-is-separate
2457 (goto-char (match-end 0))
2458 (ada-goto-next-non-ws (point-at-eol))
2459 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2461 (ada-goto-stmt-start)
2462 (list (progn (back-to-indentation) (point)) 'ada-indent
))
2464 (ada-goto-stmt-start)
2465 (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
2466 (list (progn (back-to-indentation) (point)) 0)
2467 (list (progn (back-to-indentation) (point)) 'ada-indent
)))))
2469 ;;---------------------------
2470 ;; starting with r (return, renames)
2471 ;;---------------------------
2473 ((and (= (downcase (char-after)) ?r
)
2474 (looking-at "re\\(turn\\|names\\)\\>"))
2477 (let ((var 'ada-indent-return
))
2478 ;; If looking at a renames, skip the 'return' statement too
2479 (if (looking-at "renames")
2482 (setq pos
(ada-search-ignore-string-comment ";\\|return\\>" t
)))
2484 (= (downcase (char-after (car pos
))) ?r
))
2485 (goto-char (car pos
)))
2486 (setq var
'ada-indent-renames
)))
2488 (forward-comment -
1000)
2489 (if (= (char-before) ?\
))
2493 ;; If there is a parameter list, and we have a function declaration
2494 ;; or a access to subprogram declaration
2496 (if (and (= (following-char) ?\
()
2500 (looking-at "\\(function\\|procedure\\)\\>"))
2504 (looking-at "\\(function\\|procedure\\)\\>")))))
2506 ;; The indentation depends of the value of ada-indent-return
2507 (if (<= (eval var
) 0)
2508 (list (point) (list '- var
))
2509 (list (progn (backward-word num-back
) (point))
2512 ;; Else there is no parameter list, but we have a function
2513 ;; Only do something special if the user want to indent
2514 ;; relative to the "function" keyword
2515 (if (and (> (eval var
) 0)
2516 (save-excursion (forward-word -
1)
2517 (looking-at "function\\>")))
2518 (list (progn (forward-word -
1) (point)) var
)
2521 (ada-indent-on-previous-lines nil orgpoint orgpoint
)))))))
2523 ;;--------------------------------
2524 ;; starting with 'o' or 'p'
2525 ;; 'or' as statement-start
2526 ;; 'private' as statement-start
2527 ;;--------------------------------
2529 ((and (or (= (downcase (char-after)) ?o
)
2530 (= (downcase (char-after)) ?p
))
2531 (or (ada-looking-at-semi-or)
2532 (ada-looking-at-semi-private)))
2534 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2535 (ada-goto-matching-start 1)
2536 (list (progn (back-to-indentation) (point)) 0)))
2538 ;;--------------------------------
2539 ;; starting with 'd' (do)
2540 ;;--------------------------------
2542 ((and (= (downcase (char-after)) ?d
)
2543 (looking-at "do\\>"))
2545 (ada-goto-stmt-start)
2546 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent
)))
2548 ;;--------------------------------
2549 ;; starting with '-' (comment)
2550 ;;--------------------------------
2552 ((= (char-after) ?-
)
2553 (if ada-indent-comment-as-code
2555 ;; Indent comments on previous line comments if required
2556 ;; We must use a search-forward (even if the code is more complex),
2557 ;; since we want to find the beginning of the comment.
2560 (if (and ada-indent-align-comments
2564 (while (and (not pos
)
2565 (search-forward "--" (point-at-eol) t
))
2566 (unless (ada-in-string-p)
2567 (setq pos
(point))))
2571 ;; Else always on previous line
2572 (ada-indent-on-previous-lines nil orgpoint orgpoint
)))
2574 ;; Else same indentation as the previous line
2575 (list (save-excursion (back-to-indentation) (point)) 0)))
2577 ;;--------------------------------
2578 ;; starting with '#' (preprocessor line)
2579 ;;--------------------------------
2581 ((and (= (char-after) ?
#)
2582 (equal ada-which-compiler
'gnat
)
2583 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2584 (list (point-at-bol) 0))
2586 ;;--------------------------------
2587 ;; starting with ')' (end of a parameter list)
2588 ;;--------------------------------
2590 ((and (not (eobp)) (= (char-after) ?\
)))
2596 ;;---------------------------------
2597 ;; new/abstract/separate
2598 ;;---------------------------------
2600 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2601 (ada-indent-on-previous-lines nil orgpoint orgpoint
))
2603 ;;---------------------------------
2604 ;; package/function/procedure
2605 ;;---------------------------------
2607 ((and (or (= (downcase (char-after)) ?p
) (= (downcase (char-after)) ?f
))
2608 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2610 ;; Go up until we find either a generic section, or the end of the
2611 ;; previous subprogram/package, or 'overriding' for this function/procedure
2613 (while (and (not found
)
2614 (ada-search-ignore-string-comment
2615 "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t
))
2617 ;; avoid "with procedure"... in generic parts
2620 (setq found
(not (looking-at "with"))))))
2623 ((looking-at "\\<generic\\|overriding\\>")
2624 (list (progn (back-to-indentation) (point)) 0))
2627 (ada-indent-on-previous-lines nil orgpoint orgpoint
)))))
2629 ;;---------------------------------
2631 ;;---------------------------------
2633 ((looking-at ada-label-re
)
2635 ;; ada-block-label-re matches variable declarations
2636 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2637 (append (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2638 '(ada-label-indent))))
2642 ;;---------------------------------
2644 ;;---------------------------------
2645 (or result
(ada-indent-on-previous-lines nil orgpoint orgpoint
))))
2647 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos
)
2648 "Calculate the indentation for the new line after ORGPOINT.
2649 The result list is based on the previous lines in the buffer.
2650 If NOMOVE is nil, moves point to the beginning of the current statement.
2651 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2653 (goto-char initial-pos
))
2654 (let ((oldpoint (point)))
2656 ;; Is inside a parameter-list ?
2657 (if (ada-in-paramlist-p)
2658 (ada-get-indent-paramlist)
2660 ;; Move to beginning of current statement. If already at a
2661 ;; statement start, move to beginning of enclosing statement.
2663 (ada-goto-stmt-start t
))
2665 ;; no beginning found => don't change indentation
2666 (if (and (eq oldpoint
(point))
2668 (ada-get-indent-nochange)
2673 ada-indent-to-open-paren
2674 (ada-in-open-paren-p))
2675 (ada-get-indent-open-paren))
2677 ((looking-at "end\\>")
2678 (ada-get-indent-end orgpoint
))
2680 ((looking-at ada-loop-start-re
)
2681 (ada-get-indent-loop orgpoint
))
2683 ((looking-at ada-subprog-start-re
)
2684 (ada-get-indent-subprog orgpoint
))
2686 ((looking-at ada-block-start-re
)
2687 (ada-get-indent-block-start orgpoint
))
2689 ((looking-at ada-block-label-re
) ; also variable declaration
2690 (ada-get-indent-block-label orgpoint
))
2692 ((looking-at ada-goto-label-re
)
2693 (ada-get-indent-goto-label orgpoint
))
2695 ((looking-at "\\(sub\\)?type\\>")
2696 (ada-get-indent-type orgpoint
))
2698 ;; "then" has to be included in the case of "select...then abort"
2699 ;; statements, since (goto-stmt-start) at the beginning of
2700 ;; the current function would leave the cursor on that position
2701 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2702 (ada-get-indent-if orgpoint
))
2704 ((looking-at "case\\>")
2705 (ada-get-indent-case orgpoint
))
2707 ((looking-at "when\\>")
2708 (ada-get-indent-when orgpoint
))
2710 ((looking-at "separate\\>")
2711 (ada-get-indent-nochange))
2713 ((looking-at "with\\>\\|use\\>")
2714 ;; Are we still in that statement, or are we in fact looking at
2715 ;; the previous one ?
2716 (if (save-excursion (search-forward ";" oldpoint t
))
2717 (list (progn (back-to-indentation) (point)) 0)
2718 (list (point) (if (looking-at "with")
2723 (ada-get-indent-noindent orgpoint
)))))
2726 (defun ada-get-indent-open-paren ()
2727 "Calculate the indentation when point is behind an unclosed parenthesis."
2728 (list (ada-in-open-paren-p) 0))
2730 (defun ada-get-indent-nochange ()
2731 "Return the current indentation of the previous line."
2734 (back-to-indentation)
2737 (defun ada-get-indent-paramlist ()
2738 "Calculate the indentation when point is inside a parameter list."
2740 (ada-search-ignore-string-comment "[^ \t\n]" t nil t
)
2742 ;; in front of the first parameter
2743 ((= (char-after) ?\
()
2744 (goto-char (match-end 0))
2747 ;; in front of another parameter
2748 ((= (char-after) ?\
;)
2749 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t
)))
2750 (ada-goto-next-non-ws)
2753 ;; After an affectation (default parameter value in subprogram
2755 ((and (= (following-char) ?
=) (= (preceding-char) ?
:))
2756 (back-to-indentation)
2757 (list (point) 'ada-broken-indent
))
2759 ;; inside a parameter declaration
2761 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t
)))
2762 (ada-goto-next-non-ws)
2763 (list (point) 'ada-broken-indent
)))))
2765 (defun ada-get-indent-end (orgpoint)
2766 "Calculate the indentation when point is just before an end statement.
2767 ORGPOINT is the limit position used in the calculation."
2768 (let ((defun-name nil
)
2771 ;; is the line already terminated by ';' ?
2773 (ada-search-ignore-string-comment ";" nil orgpoint nil
2776 ;; yes, look what's following 'end'
2779 (ada-goto-next-non-ws)
2782 ;; loop/select/if/case/return
2784 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
2785 (save-excursion (ada-check-matching-start (match-string 0)))
2786 (list (save-excursion (back-to-indentation) (point)) 0))
2791 ((looking-at "\\<record\\>")
2793 (ada-check-matching-start (match-string 0))
2794 ;; we are now looking at the matching "record" statement
2796 (ada-goto-stmt-start)
2797 ;; now on the matching type declaration, or use clause
2798 (unless (looking-at "\\(for\\|type\\)\\>")
2799 (ada-search-ignore-string-comment "\\<type\\>" t
))
2800 (list (progn (back-to-indentation) (point)) 0)))
2802 ;; a named block end
2804 ((looking-at ada-ident-re
)
2805 (setq defun-name
(match-string 0))
2807 (ada-goto-matching-start 0)
2808 (ada-check-defun-name defun-name
))
2809 (list (progn (back-to-indentation) (point)) 0))
2811 ;; a block-end without name
2813 ((= (char-after) ?\
;)
2815 (ada-goto-matching-start 0)
2816 (if (looking-at "\\<begin\\>")
2818 (setq indent
(list (point) 0))
2819 (if (ada-goto-decl-start t
)
2820 (list (progn (back-to-indentation) (point)) 0)
2822 (list (progn (back-to-indentation) (point)) 0)
2825 ;; anything else - should maybe signal an error ?
2828 (list (save-excursion (back-to-indentation) (point))
2829 'ada-broken-indent
))))
2831 (list (save-excursion (back-to-indentation) (point))
2832 'ada-broken-indent
))))
2834 (defun ada-get-indent-case (orgpoint)
2835 "Calculate the indentation when point is just before a case statement.
2836 ORGPOINT is the limit position used in the calculation."
2837 (let ((match-cons nil
)
2841 ;; case..is..when..=>
2844 (setq match-cons
(and
2845 ;; the `=>' must be after the keyword `is'.
2846 (ada-search-ignore-string-comment
2847 "is" nil orgpoint nil
'word-search-forward
)
2848 (ada-search-ignore-string-comment
2849 "[ \t\n]+=>" nil orgpoint
))))
2851 (goto-char (car match-cons
))
2852 (unless (ada-search-ignore-string-comment "when" t opos
)
2853 (error "Missing 'when' between 'case' and '=>'"))
2854 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)))
2859 (setq match-cons
(ada-search-ignore-string-comment
2860 "when" nil orgpoint nil
'word-search-forward
)))
2861 (goto-char (cdr match-cons
))
2862 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
2867 (setq match-cons
(ada-search-ignore-string-comment
2868 "is" nil orgpoint nil
'word-search-forward
)))
2869 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent
))
2874 (list (save-excursion (back-to-indentation) (point))
2875 'ada-broken-indent
)))))
2877 (defun ada-get-indent-when (orgpoint)
2878 "Calculate the indentation when point is just before a when statement.
2879 ORGPOINT is the limit position used in the calculation."
2880 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2881 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint
)
2882 (list cur-indent
'ada-indent
)
2883 (list cur-indent
'ada-broken-indent
))))
2885 (defun ada-get-indent-if (orgpoint)
2886 "Calculate the indentation when point is just before an if statement.
2887 ORGPOINT is the limit position used in the calculation."
2888 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2891 ;; Move to the correct then (ignore all "and then")
2893 (while (and (setq match-cons
(ada-search-ignore-string-comment
2894 "\\<\\(then\\|and[ \t]*then\\)\\>"
2896 (= (downcase (char-after (car match-cons
))) ?a
)))
2897 ;; If "then" was found (we are looking at it)
2901 ;; 'then' first in separate line ?
2902 ;; => indent according to 'then',
2903 ;; => else indent according to 'if'
2906 (back-to-indentation)
2907 (looking-at "\\<then\\>"))
2908 (setq cur-indent
(save-excursion (back-to-indentation) (point))))
2911 (list cur-indent
'ada-indent
))
2913 (list cur-indent
'ada-broken-indent
))))
2915 (defun ada-get-indent-block-start (orgpoint)
2916 "Calculate the indentation when point is at the start of a block.
2917 ORGPOINT is the limit position used in the calculation."
2922 (setq pos
(ada-goto-next-non-ws orgpoint
)))
2925 (ada-indent-on-previous-lines t orgpoint
)))
2927 ;; Special case for record types, for instance for:
2928 ;; type A is (B : Integer;
2929 ;; C : Integer) is record
2930 ;; null; -- This is badly indented otherwise
2931 ((looking-at "record")
2933 ;; If record is at the beginning of the line, indent from there
2936 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2937 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)
2939 ;; else indent relative to the type command
2940 (list (save-excursion
2941 (car (ada-search-ignore-string-comment "\\<type\\>" t
)))
2944 ;; Special case for label:
2945 ((looking-at ada-block-label-re
)
2946 (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent
) 'ada-indent
))
2948 ;; nothing follows the block-start
2950 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)))))
2952 (defun ada-get-indent-subprog (orgpoint)
2953 "Calculate the indentation when point is just before a subprogram.
2954 ORGPOINT is the limit position used in the calculation."
2955 (let ((match-cons nil
)
2956 (cur-indent (save-excursion (back-to-indentation) (point)))
2959 ;; is there an 'is' in front of point ?
2963 (ada-search-ignore-string-comment
2964 "\\<\\(is\\|do\\)\\>" nil orgpoint
)))
2966 ;; yes, then skip to its end
2970 (goto-char (cdr match-cons
)))
2972 ;; no, then goto next non-ws, if there is one in front of point
2975 (unless (ada-goto-next-non-ws orgpoint
)
2976 (goto-char orgpoint
))))
2980 ;; nothing follows 'is'
2985 (not (ada-search-ignore-string-comment
2986 "[^ \t\n]" nil orgpoint t
))))
2987 (list cur-indent
'ada-indent
))
2989 ;; is abstract/separate/new ...
2995 (ada-search-ignore-string-comment
2996 "\\<\\(separate\\|new\\|abstract\\)\\>"
2998 (goto-char (car match-cons
))
2999 (ada-search-ignore-string-comment ada-subprog-start-re t
)
3000 (ada-get-indent-noindent orgpoint
))
3002 ;; something follows 'is'
3006 (save-excursion (setq match-cons
(ada-goto-next-non-ws orgpoint
)))
3007 (goto-char match-cons
)
3008 (ada-indent-on-previous-lines t orgpoint
)))
3013 (ada-search-ignore-string-comment ";" nil orgpoint nil
'search-forward
))
3014 (list cur-indent
0))
3019 (list cur-indent
'ada-broken-indent
)))))
3021 (defun ada-get-indent-noindent (orgpoint)
3022 "Calculate the indentation when point is just before a 'noindent stmt'.
3023 ORGPOINT is the limit position used in the calculation."
3030 ;; This one is called when indenting a line preceded by a multi-line
3031 ;; subprogram declaration (in that case, we are at this point inside
3032 ;; the parameter declaration list)
3033 ((ada-in-paramlist-p)
3034 (ada-previous-procedure)
3035 (list (save-excursion (back-to-indentation) (point)) 0))
3037 ;; This one is called when indenting the second line of a multi-line
3038 ;; declaration section, in a declare block or a record declaration
3039 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3040 (list (save-excursion (back-to-indentation) (point))
3041 'ada-broken-decl-indent
))
3043 ;; This one is called in every other case when indenting a line at the
3046 (if (looking-at (concat "[ \t]*" ada-block-label-re
))
3047 (setq label
(- ada-label-indent
))
3051 ;; "with private" or "null record" cases
3052 (if (or (save-excursion
3053 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint
)
3055 (save-excursion (forward-char -
7);; skip back "private"
3056 (ada-goto-previous-word)
3057 (looking-at "with"))))
3059 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint
)
3061 (save-excursion (forward-char -
6);; skip back "record"
3062 (ada-goto-previous-word)
3063 (looking-at "null")))))
3066 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t
)
3067 (list (save-excursion (back-to-indentation) (point)) 0)))))
3069 (ada-search-ignore-string-comment ";" nil orgpoint nil
3071 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0)
3072 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3073 'ada-broken-indent
)))))))
3075 (defun ada-get-indent-block-label (orgpoint)
3076 "Calculate the indentation when before a label or variable declaration.
3077 ORGPOINT is the limit position used in the calculation."
3078 (let ((match-cons nil
)
3079 (cur-indent (save-excursion (back-to-indentation) (point))))
3080 (ada-search-ignore-string-comment ":" nil
)
3084 (setq match-cons
(ada-search-ignore-string-comment
3085 ada-loop-start-re nil orgpoint
)))
3086 (goto-char (car match-cons
))
3087 (ada-get-indent-loop orgpoint
))
3091 (setq match-cons
(ada-search-ignore-string-comment
3092 "\\<declare\\|begin\\>" nil orgpoint
)))
3093 (goto-char (car match-cons
))
3094 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3096 ;; variable declaration
3099 (ada-search-ignore-string-comment ";" nil orgpoint
))
3101 (list cur-indent
'ada-broken-indent
)))
3103 ;; nothing follows colon
3105 (list cur-indent
'(- ada-label-indent
))))))
3107 (defun ada-get-indent-goto-label (orgpoint)
3108 "Calculate the indentation when at a goto label."
3109 (search-forward ">>")
3110 (ada-goto-next-non-ws)
3111 (if (>= (point) orgpoint
)
3112 ;; labeled statement is the one we need to indent
3113 (list (- (point) ada-label-indent
))
3114 ;; else indentation is indent for labeled statement
3115 (ada-indent-on-previous-lines t orgpoint
)))
3117 (defun ada-get-indent-loop (orgpoint)
3118 "Calculate the indentation when just before a loop or a for ... use.
3119 ORGPOINT is the limit position used in the calculation."
3120 (let ((match-cons nil
)
3123 ;; If looking at a named block, skip the label
3124 (label (save-excursion
3125 (back-to-indentation)
3126 (if (looking-at ada-block-label-re
)
3127 (- ada-label-indent
)
3133 ;; statement complete
3136 (ada-search-ignore-string-comment ";" nil orgpoint nil
3138 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0))
3142 ((looking-at "loop\\>")
3143 (setq pos
(ada-get-indent-block-start orgpoint
))
3146 (list (+ (car pos
) label
) (cadr pos
))))
3149 ;; 'for'- loop (or also a for ... use statement)
3151 ((looking-at "for\\>")
3158 (goto-char (match-end 0))
3159 (ada-goto-next-non-ws orgpoint
)
3161 (if (= (char-after) ?
') (forward-word 1) t
)
3162 (ada-goto-next-non-ws orgpoint
)
3163 (looking-at "\\<use\\>")
3165 ;; check if there is a 'record' before point
3168 (setq match-cons
(ada-search-ignore-string-comment
3169 "record" nil orgpoint nil
'word-search-forward
))
3173 (goto-char (car match-cons
))
3174 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3175 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
3182 (setq match-cons
(ada-search-ignore-string-comment
3183 "loop" nil orgpoint nil
'word-search-forward
)))
3184 (goto-char (car match-cons
))
3186 ;; indent according to 'loop', if it's first in the line;
3187 ;; otherwise to 'for'
3189 (unless (save-excursion
3190 (back-to-indentation)
3191 (looking-at "\\<loop\\>"))
3193 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3196 ;; for-statement is broken
3199 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3200 'ada-broken-indent
))))
3205 ((looking-at "while\\>")
3210 (setq match-cons
(ada-search-ignore-string-comment
3211 "loop" nil orgpoint nil
'word-search-forward
)))
3214 (goto-char (car match-cons
))
3216 ;; indent according to 'loop', if it's first in the line;
3217 ;; otherwise to 'while'.
3219 (unless (save-excursion
3220 (back-to-indentation)
3221 (looking-at "\\<loop\\>"))
3223 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3226 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3227 'ada-broken-indent
))))))
3229 (defun ada-get-indent-type (orgpoint)
3230 "Calculate the indentation when before a type statement.
3231 ORGPOINT is the limit position used in the calculation."
3232 (let ((match-dat nil
))
3235 ;; complete record declaration
3239 (setq match-dat
(ada-search-ignore-string-comment
3240 "end" nil orgpoint nil
'word-search-forward
))
3241 (ada-goto-next-non-ws)
3242 (looking-at "\\<record\\>")
3244 (ada-goto-next-non-ws)
3245 (= (char-after) ?\
;)))
3246 (goto-char (car match-dat
))
3247 (list (save-excursion (back-to-indentation) (point)) 0))
3252 (setq match-dat
(ada-search-ignore-string-comment
3253 "record" nil orgpoint nil
'word-search-forward
)))
3254 (goto-char (car match-dat
))
3255 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3257 ;; complete type declaration
3260 (ada-search-ignore-string-comment ";" nil orgpoint nil
3262 (list (save-excursion (back-to-indentation) (point)) 0))
3264 ;; "type ... is", but not "type ... is ...", which is broken
3268 (ada-search-ignore-string-comment "is" nil orgpoint nil
3269 'word-search-forward
)
3270 (not (ada-goto-next-non-ws orgpoint
))))
3271 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
3276 (list (save-excursion (back-to-indentation) (point))
3277 'ada-broken-indent
)))))
3280 ;; -----------------------------------------------------------
3281 ;; -- searching and matching
3282 ;; -----------------------------------------------------------
3284 (defun ada-goto-stmt-start (&optional ignore-goto-label
)
3285 "Move point to the beginning of the statement that point is in or after.
3286 Return the new position of point.
3287 As a special case, if we are looking at a closing parenthesis, skip to the
3289 (let ((match-dat nil
)
3292 (setq match-dat
(ada-search-prev-end-stmt))
3296 ;; found a previous end-statement => check if anything follows
3298 (unless (looking-at "declare")
3300 (unless (save-excursion
3301 (goto-char (cdr match-dat
))
3302 (ada-goto-next-non-ws orgpoint ignore-goto-label
))
3304 ;; nothing follows => it's the end-statement directly in
3305 ;; front of point => search again
3307 (setq match-dat
(ada-search-prev-end-stmt)))
3309 ;; if found the correct end-statement => goto next non-ws
3312 (goto-char (cdr match-dat
)))
3313 (ada-goto-next-non-ws)
3317 ;; no previous end-statement => we are at the beginning of the
3318 ;; accessible part of the buffer
3321 (goto-char (point-min))
3323 ;; skip to the very first statement, if there is one
3325 (unless (ada-goto-next-non-ws orgpoint
)
3326 (goto-char orgpoint
))))
3330 (defun ada-search-prev-end-stmt ()
3331 "Move point to previous end statement.
3332 Return a cons cell whose car is the beginning and whose cdr
3333 is the end of the match."
3334 (let ((match-dat nil
)
3337 ;; search until found or beginning-of-buffer
3341 (setq match-dat
(ada-search-ignore-string-comment
3342 ada-end-stmt-re t
)))
3344 (goto-char (car match-dat
))
3345 (unless (ada-in-open-paren-p)
3349 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3351 (ada-goto-previous-word)
3352 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3357 (and (save-excursion (ada-goto-previous-word)
3358 (ada-goto-previous-word)
3359 (not (looking-at "subtype")))
3361 (save-excursion (goto-char (cdr match-dat
))
3362 (ada-goto-next-non-ws)
3363 ;; words that can go after an 'is'
3368 '("separate" "access" "array"
3369 "private" "abstract" "new") t
)
3372 ((looking-at "private")
3375 (setq found
(not (looking-at "is")))))
3385 (defun ada-goto-next-non-ws (&optional limit skip-goto-label
)
3386 "Skip to next non-whitespace character.
3387 Skips spaces, newlines and comments, and possibly goto labels.
3388 Return `point' if moved, nil if not.
3389 Stop the search at LIMIT.
3390 Do not call this function from within a string."
3392 (setq limit
(point-max)))
3393 (while (and (<= (point) limit
)
3394 (or (progn (forward-comment 10000)
3395 (if (and (not (eobp))
3396 (save-excursion (forward-char 1)
3398 (progn (forward-sexp 1) t
)))
3399 (and skip-goto-label
3400 (looking-at ada-goto-label-re
)
3402 (goto-char (match-end 0))
3404 (if (< (point) limit
)
3410 (defun ada-goto-stmt-end (&optional limit
)
3411 "Move point to the end of the statement that point is in or before.
3412 Return the new position of point or nil if not found.
3413 Stop the search at LIMIT."
3414 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit
)
3419 (defun ada-goto-next-word (&optional backward
)
3420 "Move point to the beginning of the next word of Ada code.
3421 If BACKWARD is non-nil, jump to the beginning of the previous word.
3422 Return the new position of point or nil if not found."
3423 (let ((match-cons nil
)
3425 (old-syntax (char-to-string (char-syntax ?_
))))
3426 (modify-syntax-entry ?_
"w")
3428 (skip-syntax-forward "w"))
3429 (if (setq match-cons
3430 (ada-search-ignore-string-comment "\\w" backward nil t
))
3432 ;; move to the beginning of the word found
3435 (goto-char (car match-cons
))
3436 (skip-syntax-backward "w")
3439 ;; if not found, restore old position of point
3441 (goto-char orgpoint
)
3443 (modify-syntax-entry ?_ old-syntax
))
3447 (defun ada-check-matching-start (keyword)
3448 "Signal an error if matching block start is not KEYWORD.
3449 Moves point to the matching block start."
3450 (ada-goto-matching-start 0)
3451 (unless (looking-at (concat "\\<" keyword
"\\>"))
3452 (error "Matching start is not '%s'" keyword
)))
3455 (defun ada-check-defun-name (defun-name)
3456 "Check if the name of the matching defun really is DEFUN-NAME.
3457 Assumes point to be already positioned by `ada-goto-matching-start'.
3458 Moves point to the beginning of the declaration."
3460 ;; named block without a `declare'; ada-goto-matching-start leaves
3461 ;; point at start of 'begin' for a block.
3463 (ada-goto-previous-word)
3464 (looking-at (concat "\\<" defun-name
"\\> *:")))
3468 ;; 'accept' or 'package' ?
3470 (unless (looking-at ada-subprog-start-re
)
3471 (ada-goto-decl-start))
3473 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3477 ;; a named 'declare'-block ? => jump to the label
3479 (if (looking-at "\\<declare\\>")
3481 (forward-comment -
1)
3484 ;; no, => 'procedure'/'function'/'task'/'protected'
3490 ;; skip 'body' 'type'
3492 (if (looking-at "\\<\\(body\\|type\\)\\>")
3497 ;; should be looking-at the correct name
3499 (unless (looking-at (concat "\\<" defun-name
"\\>"))
3500 (error "Matching defun has different name: %s"
3501 (buffer-substring (point)
3502 (progn (forward-sexp 1) (point))))))))
3504 (defun ada-goto-decl-start (&optional noerror
)
3505 "Move point to the declaration start of the current construct.
3506 If NOERROR is non-nil, return nil if no match was found;
3507 otherwise throw error."
3508 (let ((nest-count 1)
3509 (regexp (eval-when-compile
3512 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t
)
3515 ;; first should be set to t if we should stop at the first
3516 ;; "begin" we encounter.
3522 ;; Ignore "when" most of the time, except if we are looking at the
3523 ;; beginning of a block (structure: case .. is
3527 (if (looking-at "begin")
3528 (setq stop-at-when t
))
3531 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3533 (ada-search-ignore-string-comment
3534 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t
)
3535 (looking-at "generic")))
3536 (setq count-generic t
))
3538 ;; search backward for interesting keywords
3540 (not (zerop nest-count
))
3541 (ada-search-ignore-string-comment regexp t
))
3543 ;; calculate nest-depth
3548 (ada-goto-matching-start 1 noerror
)
3550 ;; In some case, two begin..end block can follow each other closely,
3551 ;; which we have to detect, as in
3556 ;; begin -- here we should go to procedure, not begin
3559 (if (looking-at "begin")
3560 (let ((loop-again t
))
3563 ;; If begin was just there as the beginning of a block
3564 ;; (with no declare) then do nothing, otherwise just
3565 ;; register that we have to find the statement that
3566 ;; required the begin
3568 (ada-search-ignore-string-comment
3569 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3572 (if (looking-at "end")
3573 (ada-goto-matching-start 1 noerror t
)
3575 (setq loop-again nil
)
3576 (unless (looking-at "begin")
3577 (setq nest-count
(1+ nest-count
))))
3581 ((looking-at "generic")
3585 (setq nest-count
(1- nest-count
)))))
3590 (unless (looking-at "\\<end[ \t\n]*if\\>")
3592 (setq nest-count
(1- nest-count
))
3593 (setq first nil
)))))
3596 ((looking-at "declare\\|generic")
3597 (setq nest-count
(1- nest-count
))
3601 ;; look for things to ignore
3604 ;; generic formal parameter
3605 (looking-at "is[ t]+<>")
3607 ;; A type definition, or a case statement. Note that the
3608 ;; goto-matching-start above on 'end record' leaves us at
3609 ;; 'record', not at 'type'.
3611 ;; We get to a case statement here by calling
3612 ;; 'ada-move-to-end' from inside a case statement; then
3613 ;; we are not ignoring 'when'.
3615 ;; Skip type discriminants or case argument function call param list
3616 (forward-comment -
10000)
3618 (if (= (char-after) ?\
))
3622 (forward-comment -
10000)
3624 ;; skip type or case argument name
3625 (skip-chars-backward "a-zA-Z0-9_.'")
3626 (ada-goto-previous-word)
3628 ;; if it's a protected type, it's the decl start we
3629 ;; are looking for; since we didn't see the 'end'
3630 ;; above, we are inside it.
3631 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3633 (ada-goto-previous-word)
3634 (not (looking-at "\\<protected\\>"))))
3635 ) ; end of type definition p
3637 ;; null procedure declaration
3638 (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
3640 ;; skip this construct
3642 ;; this is the right "is"
3643 (setq nest-count
(1- nest-count
))
3649 (ada-goto-previous-word)
3651 (goto-char (match-beginning 0))))
3654 (looking-at "begin"))
3655 (setq nest-count
0))
3657 ((looking-at "when")
3660 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3663 (setq nest-count
(1- nest-count
)))
3666 ((looking-at "begin")
3670 (setq nest-count
(1+ nest-count
))
3675 ;; check if declaration-start is really found
3678 (if (looking-at "is")
3679 (ada-search-ignore-string-comment ada-subprog-start-re t
)
3680 (looking-at "declare\\|generic")))
3683 (error "No matching proc/func/task/declare/package/protected")))
3686 (defun ada-goto-matching-start (&optional nest-level noerror gotothen
)
3687 "Move point to the beginning of a block-start.
3688 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3689 If NOERROR is non-nil, it only returns nil if no matching start was found.
3690 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3691 (let ((nest-count (if nest-level nest-level
0))
3694 (last-was-begin '())
3695 ;; List all keywords encountered while traversing
3696 ;; something like '("end" "end" "begin")
3697 ;; This is removed from the list when "package", "procedure",...
3698 ;; are seen. The goal is to find whether a package has an elaboration
3703 ;; search backward for interesting keywords
3706 (ada-search-ignore-string-comment ada-matching-start-re t
))
3708 (unless (and (looking-at "\\<record\\>")
3711 (looking-at "\\<null\\>")))
3713 ;; calculate nest-depth
3715 ;; found block end => increase nest depth
3717 (push nil last-was-begin
)
3718 (setq nest-count
(1+ nest-count
)))
3720 ;; found loop/select/record/case/if => check if it starts or
3722 ((looking-at "loop\\|select\\|record\\|case\\|if")
3725 ;; check if keyword follows 'end'
3726 (ada-goto-previous-word)
3727 (if (looking-at "\\<end\\>[ \t]*[^;]")
3729 ;; it ends a block => increase nest depth
3730 (setq nest-count
(1+ nest-count
)
3732 (push nil last-was-begin
))
3734 ;; it starts a block => decrease nest depth
3735 (setq nest-count
(1- nest-count
))
3737 ;; Some nested "begin .. end" blocks with no "declare"?
3738 ;; => remove those entries
3739 (while (car last-was-begin
)
3740 (setq last-was-begin
(cdr (cdr last-was-begin
))))
3742 (setq last-was-begin
(cdr last-was-begin
))
3747 ;; found package start => check if it really is a block
3748 ((looking-at "package")
3750 ;; ignore if this is just a renames statement
3751 (let ((current (point))
3752 (pos (ada-search-ignore-string-comment
3753 "\\<\\(is\\|renames\\|;\\)\\>" nil
)))
3755 (goto-char (car pos
))
3757 "No matching 'is' or 'renames' for 'package' at"
3759 (number-to-string (count-lines 1 (1+ current
)))))))
3760 (unless (looking-at "renames")
3763 (ada-goto-next-non-ws)
3764 ;; ignore it if it is only a declaration with 'new'
3765 ;; We could have package Foo is new ....
3766 ;; or package Foo is separate;
3767 ;; or package Foo is begin null; end Foo
3768 ;; for elaboration code (elaboration)
3769 (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3770 (not (car last-was-begin
)))
3771 (setq nest-count
(1- nest-count
))))))
3773 (setq last-was-begin
(cdr last-was-begin
))
3775 ;; found task start => check if it has a body
3776 ((looking-at "task")
3779 (ada-goto-next-non-ws)
3781 ((looking-at "\\<body\\>"))
3782 ((looking-at "\\<type\\>")
3783 ;; In that case, do nothing if there is a "is"
3784 (forward-word 2);; skip "type"
3785 (ada-goto-next-non-ws);; skip type name
3787 ;; Do nothing if we are simply looking at a simple
3788 ;; "task type name;" statement with no block
3789 (unless (looking-at ";")
3791 ;; Skip the parameters
3792 (if (looking-at "(")
3793 (ada-search-ignore-string-comment ")" nil
))
3794 (let ((tmp (ada-search-ignore-string-comment
3795 "\\<\\(is\\|;\\)\\>" nil
)))
3798 (goto-char (car tmp
))
3799 (if (looking-at "is")
3800 (setq nest-count
(1- nest-count
)))))))))
3802 ;; Check if that task declaration had a block attached to
3803 ;; it (i.e do nothing if we have just "task name;")
3804 (unless (progn (forward-word 1)
3805 (looking-at "[ \t]*;"))
3806 (setq nest-count
(1- nest-count
))))))
3807 (setq last-was-begin
(cdr last-was-begin
))
3810 ((looking-at "declare")
3811 ;; remove entry for begin and end (include nested begin..end
3813 (setq last-was-begin
(cdr last-was-begin
))
3815 (while (and (> count
0))
3816 (if (equal (car last-was-begin
) t
)
3817 (setq count
(1+ count
))
3818 (setq count
(1- count
)))
3819 (setq last-was-begin
(cdr last-was-begin
))
3822 ((looking-at "protected")
3823 ;; Ignore if this is just a declaration
3825 (let ((pos (ada-search-ignore-string-comment
3826 "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil
)))
3828 (goto-char (car pos
)))
3829 (if (looking-at "is")
3830 ;; remove entry for end
3831 (setq last-was-begin
(cdr last-was-begin
)))))
3832 (setq nest-count
(1- nest-count
)))
3834 ((or (looking-at "procedure")
3835 (looking-at "function"))
3836 ;; Ignore if this is just a declaration
3838 (let ((pos (ada-search-ignore-string-comment
3839 "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil
)))
3841 (goto-char (car pos
)))
3842 (if (looking-at "is")
3843 ;; remove entry for begin and end
3844 (setq last-was-begin
(cdr (cdr last-was-begin
))))))
3847 ;; all the other block starts
3849 (push (looking-at "begin") last-was-begin
)
3850 (setq nest-count
(1- nest-count
)))
3854 ;; match is found, if nest-depth is zero
3855 (setq found
(zerop nest-count
))))) ; end of loop
3861 ;; match found => is there anything else to do ?
3866 ;; found 'if' => skip to 'then', if it's on a separate line
3867 ;; and GOTOTHEN is non-nil
3873 (ada-search-ignore-string-comment "then" nil nil nil
3874 'word-search-forward
)
3875 (back-to-indentation)
3876 (looking-at "\\<then\\>")))
3877 (goto-char (match-beginning 0)))
3880 ;; found 'do' => skip back to 'accept' or 'return'
3883 (unless (ada-search-ignore-string-comment
3884 "\\<accept\\|return\\>" t
)
3885 (error "Missing 'accept' or 'return' in front of 'do'"))))
3890 (error "No matching start"))))))
3893 (defun ada-goto-matching-end (&optional nest-level noerror
)
3894 "Move point to the end of a block.
3895 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3896 If NOERROR is non-nil, it only returns nil if no matching start found."
3897 (let ((nest-count (or nest-level
0))
3898 (regex (eval-when-compile
3900 (regexp-opt '("end" "loop" "select" "begin" "case"
3901 "if" "task" "package" "record" "do"
3902 "procedure" "function") t
)
3907 ;; First is used for subprograms: they are generally handled
3908 ;; recursively, but of course we do not want to do that the
3909 ;; first time (see comment below about subprograms)
3910 (first (not (looking-at "declare"))))
3912 ;; If we are already looking at one of the keywords, this shouldn't count
3913 ;; in the nesting loop below, so we just make sure we don't count it.
3914 ;; "declare" is a special case because we need to look after the "begin"
3916 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3920 ;; search forward for interesting keywords
3924 (ada-search-ignore-string-comment regex nil
))
3927 ;; calculate nest-depth
3931 ;; procedures and functions need to be processed recursively, in
3932 ;; case they are defined in a declare/begin block, as in:
3933 ;; declare -- NL 0 (nested level)
3935 ;; procedure B (C : D) is
3938 ;; end B; -- NL 0, and we would exit
3940 ;; end; -- we should exit here
3941 ;; processing them recursively avoids the need for any special
3943 ;; Nothing should be done if we have only the specs or a
3944 ;; generic instantion.
3946 ((and (looking-at "\\<procedure\\|function\\>"))
3951 (ada-search-ignore-string-comment "is\\|;")
3952 (if (= (char-before) ?s
)
3954 (ada-goto-next-non-ws)
3955 (unless (looking-at "\\<new\\>")
3958 (ada-goto-matching-end 0 t
)))))))
3960 ;; found block end => decrease nest depth
3961 ((looking-at "\\<end\\>")
3962 (setq nest-count
(1- nest-count
)
3963 found
(<= nest-count
0))
3964 ;; skip the following keyword
3966 (skip-chars-forward "end")
3967 (ada-goto-next-non-ws)
3968 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3971 ;; found package start => check if it really starts a block, and is not
3972 ;; in fact a generic instantiation for instance
3973 ((looking-at "\\<package\\>")
3974 (ada-search-ignore-string-comment "is" nil nil nil
3975 'word-search-forward
)
3976 (ada-goto-next-non-ws)
3977 ;; ignore and skip it if it is only a 'new' package
3978 (if (looking-at "\\<new\\>")
3979 (goto-char (match-end 0))
3980 (setq nest-count
(1+ nest-count
)
3981 found
(<= nest-count
0))))
3983 ;; all the other block starts
3986 (setq nest-count
(1+ nest-count
)))
3987 (setq found
(<= nest-count
0))
3988 (forward-word 1))) ; end of 'cond'
3996 (error "No matching end")))
4000 (defun ada-search-ignore-string-comment
4001 (search-re &optional backward limit paramlists search-func
)
4002 "Regexp-search for SEARCH-RE, ignoring comments, strings.
4003 Returns a cons cell of begin and end of match data or nil, if not found.
4004 If BACKWARD is non-nil, search backward; search forward otherwise.
4005 The search stops at pos LIMIT.
4006 If PARAMLISTS is nil, ignore parameter lists.
4007 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
4008 in case we are searching for a constant string.
4009 Point is moved at the beginning of the SEARCH-RE."
4015 ;; FIXME: need to pass BACKWARD to search-func!
4017 (setq search-func
(if backward
're-search-backward
're-search-forward
)))
4020 ;; search until found or end-of-buffer
4021 ;; We have to test that we do not look further than limit
4023 (with-syntax-table ada-mode-symbol-syntax-table
4024 (while (and (not found
)
4026 (or (and backward
(<= limit
(point)))
4027 (>= limit
(point))))
4028 (funcall search-func search-re limit
1))
4029 (setq begin
(match-beginning 0))
4030 (setq end
(match-end 0))
4031 (setq parse-result
(parse-partial-sexp (point-at-bol) (point)))
4034 ;; If inside a string, skip it (and the following comments)
4036 ((ada-in-string-p parse-result
)
4037 (if (featurep 'xemacs
)
4038 (search-backward "\"" nil t
)
4039 (goto-char (nth 8 parse-result
)))
4040 (unless backward
(forward-sexp 1)))
4042 ;; If inside a comment, skip it (and the following comments)
4043 ;; There is a special code for comments at the end of the file
4045 ((ada-in-comment-p parse-result
)
4046 (if (featurep 'xemacs
)
4050 (forward-comment -
1))
4051 (goto-char (nth 8 parse-result
)))
4053 ;; at the end of the file, it is not possible to skip a comment
4054 ;; so we just go at the end of the line
4055 (if (forward-comment 1)
4057 (forward-comment 1000)
4058 (beginning-of-line))
4061 ;; directly in front of a comment => skip it, if searching forward
4063 ((and (= (char-after begin
) ?-
) (= (char-after (1+ begin
)) ?-
))
4064 (unless backward
(progn (forward-char -
1) (forward-comment 1000))))
4067 ;; found a parameter-list but should ignore it => skip it
4069 ((and (not paramlists
) (ada-in-paramlist-p))
4071 (search-backward "(" nil t
)
4072 (search-forward ")" nil t
)))
4074 ;; found what we were looking for
4077 (setq found t
))))) ; end of loop
4083 ;; -------------------------------------------------------
4084 ;; -- Testing the position of the cursor
4085 ;; -------------------------------------------------------
4087 (defun ada-in-decl-p ()
4088 "Return t if point is inside a declarative part.
4089 Assumes point to be at the end of a statement."
4090 (or (ada-in-paramlist-p)
4092 (ada-goto-decl-start t
))))
4095 (defun ada-looking-at-semi-or ()
4096 "Return t if looking at an 'or' following a semicolon."
4098 (and (looking-at "\\<or\\>")
4101 (ada-goto-stmt-start)
4102 (looking-at "\\<or\\>")))))
4105 (defun ada-looking-at-semi-private ()
4106 "Return t if looking at the start of a private section in a package.
4107 Return nil if the private is part of the package name, as in
4108 'private package A is...' (this can only happen at top level)."
4110 (and (looking-at "\\<private\\>")
4111 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4113 ;; Make sure this is the start of a private section (ie after
4114 ;; a semicolon or just after the package declaration, but not
4115 ;; after a 'type ... is private' or 'is new ... with private'
4117 ;; Note that a 'private' statement at the beginning of the buffer
4118 ;; does not indicate a private section, since this is instead a
4119 ;; 'private procedure ...'
4120 (progn (forward-comment -
1000)
4122 (or (= (char-before) ?\
;)
4123 (and (forward-word -
3)
4124 (looking-at "\\<package\\>"))))))))
4127 (defun ada-in-paramlist-p ()
4128 "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
4131 (ada-search-ignore-string-comment "(\\|)" t nil t
)
4132 ;; inside parentheses ?
4133 (= (char-after) ?\
()
4135 ;; We could be looking at two things here:
4136 ;; operator definition: function "." (
4137 ;; subprogram definition: procedure .... (
4138 ;; Let's skip back over the first one
4140 (skip-chars-backward " \t\n")
4141 (if (= (char-before) ?
\")
4146 ;; and now over the second one
4149 ;; We should ignore the case when the reserved keyword is in a
4150 ;; comment (for instance, when we have:
4153 ;; we should return nil
4155 (not (ada-in-string-or-comment-p))
4157 ;; right keyword two words before parenthesis ?
4158 ;; Type is in this list because of discriminants
4159 ;; pragma is not, because the syntax is that of a subprogram call.
4160 (looking-at (eval-when-compile
4162 "procedure\\|function\\|body\\|"
4163 "task\\|entry\\|accept\\|"
4164 "access[ \t]+procedure\\|"
4165 "access[ \t]+function\\|"
4168 (defun ada-search-ignore-complex-boolean (regexp backwardp
)
4169 "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
4170 If BACKWARDP is non-nil, search backward; search forward otherwise."
4172 (while (and (setq result
(ada-search-ignore-string-comment regexp backwardp
))
4173 (save-excursion (forward-word -
1)
4174 (looking-at "and then\\|or else"))))
4177 (defun ada-in-open-paren-p ()
4178 "Non-nil if in an open parenthesis.
4179 Return value is the position of the first non-ws behind the last unclosed
4180 parenthesis, or nil."
4182 (let ((parse (parse-partial-sexp
4184 (or (car (ada-search-ignore-complex-boolean
4185 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4191 (goto-char (1+ (nth 1 parse
)))
4193 ;; Skip blanks, if they are not followed by a comment
4195 ;; type A is ( Value_0,
4197 ;; type B is ( -- comment
4200 (if (or (not ada-indent-handle-comment-special
)
4201 (not (looking-at "[ \t]+--")))
4202 (skip-chars-forward " \t"))
4207 ;; -----------------------------------------------------------
4208 ;; -- Behavior Of TAB Key
4209 ;; -----------------------------------------------------------
4212 "Do indenting or tabbing according to `ada-tab-policy'.
4213 In Transient Mark mode, if the mark is active, operate on the contents
4214 of the region. Otherwise, operate only on the current line."
4216 (cond ((eq ada-tab-policy
'indent-rigidly
) (ada-tab-hard))
4217 ((eq ada-tab-policy
'indent-auto
)
4218 (if (ada-region-selected)
4219 (ada-indent-region (region-beginning) (region-end))
4220 (ada-indent-current)))
4221 ((eq ada-tab-policy
'always-tab
) (error "Not implemented"))
4224 (defun ada-untab (arg)
4225 "Delete leading indenting according to `ada-tab-policy'."
4226 ;; FIXME: ARG is ignored
4228 (cond ((eq ada-tab-policy
'indent-rigidly
) (ada-untab-hard))
4229 ((eq ada-tab-policy
'indent-auto
) (error "Not implemented"))
4230 ((eq ada-tab-policy
'always-tab
) (error "Not implemented"))
4233 (defun ada-indent-current-function ()
4234 "Ada mode version of the `indent-line-function'."
4236 (let ((starting-point (point-marker)))
4239 (if (< (point) starting-point
)
4240 (goto-char starting-point
))
4241 (set-marker starting-point nil
)
4244 (defun ada-tab-hard ()
4245 "Indent current line to next tab stop."
4249 (insert-char ? ada-indent
))
4250 (if (bolp) (forward-char ada-indent
)))
4252 (defun ada-untab-hard ()
4253 "Indent current line to previous tab stop."
4255 (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent
)))
4258 ;; ------------------------------------------------------------
4260 ;; ------------------------------------------------------------
4262 ;; Not needed any more for Emacs 21.2, but still needed for backward
4264 (defun ada-remove-trailing-spaces ()
4265 "Remove trailing spaces in the whole buffer."
4271 (goto-char (point-min))
4272 (while (re-search-forward "[ \t]+$" (point-max) t
)
4273 (replace-match "" nil nil
))))))
4275 (defun ada-gnat-style ()
4276 "Clean up comments, `(' and `,' for GNAT style checking switch."
4280 ;; The \n is required, or the line after an empty comment line is
4282 (goto-char (point-min))
4283 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t
)
4284 (replace-match "-- \\1")
4286 (beginning-of-line))
4288 (goto-char (point-min))
4289 (while (re-search-forward "\\>(" nil t
)
4290 (if (not (ada-in-string-or-comment-p))
4291 (replace-match " (")))
4292 (goto-char (point-min))
4293 (while (re-search-forward ";--" nil t
)
4295 (if (not (ada-in-string-or-comment-p))
4296 (replace-match "; --")))
4297 (goto-char (point-min))
4298 (while (re-search-forward "([ \t]+" nil t
)
4299 (if (not (ada-in-string-or-comment-p))
4300 (replace-match "(")))
4301 (goto-char (point-min))
4302 (while (re-search-forward ")[ \t]+)" nil t
)
4303 (if (not (ada-in-string-or-comment-p))
4304 (replace-match "))")))
4305 (goto-char (point-min))
4306 (while (re-search-forward "\\>:" nil t
)
4307 (if (not (ada-in-string-or-comment-p))
4308 (replace-match " :")))
4310 ;; Make sure there is a space after a ','.
4311 ;; Always go back to the beginning of the match, since otherwise
4312 ;; a statement like ('F','D','E') is incorrectly modified.
4313 (goto-char (point-min))
4314 (while (re-search-forward ",[ \t]*\\(.\\)" nil t
)
4315 (if (not (save-excursion
4316 (goto-char (match-beginning 0))
4317 (ada-in-string-or-comment-p)))
4318 (replace-match ", \\1")))
4320 ;; Operators should be surrounded by spaces.
4321 (goto-char (point-min))
4322 (while (re-search-forward
4323 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4325 (goto-char (match-beginning 1))
4326 (if (or (looking-at "--")
4327 (ada-in-string-or-comment-p))
4330 (beginning-of-line))
4332 ((string= (match-string 1) "/=")
4333 (replace-match " /= "))
4334 ((string= (match-string 1) "..")
4335 (replace-match " .. "))
4336 ((string= (match-string 1) "**")
4337 (replace-match " ** "))
4338 ((string= (match-string 1) ":=")
4339 (replace-match " := "))
4341 (replace-match " \\1 ")))
4347 ;; -------------------------------------------------------------
4348 ;; -- Moving To Procedures/Packages/Statements
4349 ;; -------------------------------------------------------------
4351 (defun ada-move-to-start ()
4352 "Move point to the matching start of the current Ada structure."
4354 (let ((pos (point)))
4355 (with-syntax-table ada-mode-symbol-syntax-table
4359 ;; do nothing if in string or comment or not on 'end ...;'
4360 ;; or if an error occurs during processing
4363 (ada-in-string-or-comment-p)
4365 (or (looking-at "[ \t]*\\<end\\>")
4367 (or (looking-at "[ \t]*\\<end\\>")
4369 (or (looking-at "[ \t]*\\<end\\>")
4370 (error "Not on end ...;")))
4371 (ada-goto-matching-start 1)
4375 ;; on 'begin' => go on, according to user option
4377 ada-move-to-declaration
4378 (looking-at "\\<begin\\>")
4379 (ada-goto-decl-start)
4380 (setq pos
(point))))
4382 ) ; end of save-excursion
4384 ;; now really move to the found position
4387 (defun ada-move-to-end ()
4388 "Move point to the end of the block around point.
4389 Moves to 'begin' if in a declarative part."
4393 (with-syntax-table ada-mode-symbol-syntax-table
4398 ;; Go to the beginning of the current word, and check if we are
4399 ;; directly on 'begin'
4401 (skip-syntax-backward "w")
4402 (looking-at "\\<begin\\>"))
4403 (ada-goto-matching-end 1))
4405 ;; on first line of subprogram body
4406 ;; Do nothing for specs or generic instantion, since these are
4407 ;; handled as the general case (find the enclosing block)
4408 ;; We also need to make sure that we ignore nested subprograms
4410 (and (skip-syntax-backward "w")
4411 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4412 (ada-search-ignore-string-comment "is\\|;")
4413 (not (= (char-before) ?\
;))
4415 (skip-syntax-backward "w")
4416 (ada-goto-matching-end 0 t
))
4418 ;; on first line of task declaration
4420 (and (ada-goto-stmt-start)
4421 (looking-at "\\<task\\>" )
4423 (ada-goto-next-non-ws)
4424 (looking-at "\\<body\\>")))
4425 (ada-search-ignore-string-comment "begin" nil nil nil
4426 'word-search-forward
))
4427 ;; accept block start
4429 (and (ada-goto-stmt-start)
4430 (looking-at "\\<accept\\>" )))
4431 (ada-goto-matching-end 0))
4434 (setq decl-start
(and (ada-goto-decl-start t
) (point)))
4435 (and decl-start
(looking-at "\\<package\\>")))
4436 (ada-goto-matching-end 1))
4438 ;; On a "declare" keyword
4440 (skip-syntax-backward "w")
4441 (looking-at "\\<declare\\>"))
4442 (ada-goto-matching-end 0 t
))
4444 ;; inside a 'begin' ... 'end' block
4446 (goto-char decl-start
)
4447 (ada-goto-matching-end 0 t
))
4449 ;; (hopefully ;-) everything else
4451 (ada-goto-matching-end 1)))
4455 ;; now really move to the position found
4458 (defun ada-next-procedure ()
4459 "Move point to next procedure."
4462 (if (re-search-forward ada-procedure-start-regexp nil t
)
4463 (goto-char (match-beginning 4))
4464 (error "No more functions/procedures/tasks")))
4466 (defun ada-previous-procedure ()
4467 "Move point to previous procedure."
4470 (if (re-search-backward ada-procedure-start-regexp nil t
)
4471 (goto-char (match-beginning 4))
4472 (error "No more functions/procedures/tasks")))
4474 (defun ada-next-package ()
4475 "Move point to next package."
4478 (if (re-search-forward ada-package-start-regexp nil t
)
4479 (goto-char (match-beginning 1))
4480 (error "No more packages")))
4482 (defun ada-previous-package ()
4483 "Move point to previous package."
4486 (if (re-search-backward ada-package-start-regexp nil t
)
4487 (goto-char (match-beginning 1))
4488 (error "No more packages")))
4491 ;; ------------------------------------------------------------
4492 ;; -- Define keymap and menus for Ada
4493 ;; -------------------------------------------------------------
4495 (defun ada-create-keymap ()
4496 "Create the keymap associated with the Ada mode."
4498 ;; All non-standard keys go into ada-mode-extra-map
4499 (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map
)
4501 ;; Indentation and Formatting
4502 (define-key ada-mode-map
"\C-j" 'ada-indent-newline-indent-conditional
)
4503 (define-key ada-mode-map
"\C-m" 'ada-indent-newline-indent-conditional
)
4504 (define-key ada-mode-map
"\t" 'ada-tab
)
4505 (define-key ada-mode-map
"\C-c\t" 'ada-justified-indent-current
)
4506 (define-key ada-mode-map
"\C-c\C-l" 'ada-indent-region
)
4507 (define-key ada-mode-map
[(shift tab
)] 'ada-untab
)
4508 (define-key ada-mode-map
"\C-c\C-f" 'ada-format-paramlist
)
4509 ;; We don't want to make meta-characters case-specific.
4512 (define-key ada-mode-map
"\M-\C-e" 'ada-next-procedure
)
4513 (define-key ada-mode-map
"\M-\C-a" 'ada-previous-procedure
)
4514 (define-key ada-mode-map
"\C-c\C-a" 'ada-move-to-start
)
4515 (define-key ada-mode-map
"\C-c\C-e" 'ada-move-to-end
)
4518 (unless (lookup-key ada-mode-map
"\C-c\C-c")
4519 (define-key ada-mode-map
"\C-c\C-c" 'compile
))
4522 (define-key ada-mode-map
"\C-c\C-b" 'ada-adjust-case-buffer
)
4523 (define-key ada-mode-map
"\C-c\C-t" 'ada-case-read-exceptions
)
4524 (define-key ada-mode-map
"\C-c\C-y" 'ada-create-case-exception
)
4525 (define-key ada-mode-map
"\C-c\C-\M-y" 'ada-create-case-exception-substring
)
4527 ;; On XEmacs, you can easily specify whether DEL should deletes
4528 ;; one character forward or one character backward. Take this into
4530 (define-key ada-mode-map
4531 (if (boundp 'delete-key-deletes-forward
) [backspace] "\177")
4532 'backward-delete-char-untabify)
4535 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4537 ;; Use predefined function of Emacs19 for comments (RE)
4538 (define-key ada-mode-map "\C-c;" 'comment-region)
4539 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4541 ;; The following keys are bound to functions defined in ada-xref.el or
4542 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4543 ;; and activated only if the right compiler is used
4545 (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
4546 'ada-point-and-xref)
4547 (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
4549 (define-key ada-mode-extra-map "o" 'ff-find-other-file)
4550 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4551 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4552 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4553 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4554 (define-key ada-mode-extra-map "c" 'ada-change-prj)
4555 (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
4556 (define-key ada-mode-extra-map "g" 'ada-gdb-application)
4557 (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
4558 (define-key ada-mode-extra-map "r" 'ada-run-application)
4559 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4560 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4561 (define-key ada-mode-extra-map "l" 'ada-find-local-references)
4562 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4563 (define-key ada-mode-extra-map "f" 'ada-find-file)
4565 (define-key ada-mode-extra-map "u" 'ada-prj-edit)
4567 (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
4569 ;; The templates, defined in ada-stmt.el
4571 (let ((map (make-sparse-keymap)))
4572 (define-key map "h" 'ada-header)
4573 (define-key map "\C-a" 'ada-array)
4574 (define-key map "b" 'ada-exception-block)
4575 (define-key map "d" 'ada-declare-block)
4576 (define-key map "c" 'ada-case)
4577 (define-key map "\C-e" 'ada-elsif)
4578 (define-key map "e" 'ada-else)
4579 (define-key map "\C-k" 'ada-package-spec)
4580 (define-key map "k" 'ada-package-body)
4581 (define-key map "\C-p" 'ada-procedure-spec)
4582 (define-key map "p" 'ada-subprogram-body)
4583 (define-key map "\C-f" 'ada-function-spec)
4584 (define-key map "f" 'ada-for-loop)
4585 (define-key map "i" 'ada-if)
4586 (define-key map "l" 'ada-loop)
4587 (define-key map "\C-r" 'ada-record)
4588 (define-key map "\C-s" 'ada-subtype)
4589 (define-key map "S" 'ada-tabsize)
4590 (define-key map "\C-t" 'ada-task-spec)
4591 (define-key map "t" 'ada-task-body)
4592 (define-key map "\C-y" 'ada-type)
4593 (define-key map "\C-v" 'ada-private)
4594 (define-key map "u" 'ada-use)
4595 (define-key map "\C-u" 'ada-with)
4596 (define-key map "\C-w" 'ada-when)
4597 (define-key map "w" 'ada-while-loop)
4598 (define-key map "\C-x" 'ada-exception)
4599 (define-key map "x" 'ada-exit)
4600 (define-key ada-mode-extra-map "t" map))
4604 (defun ada-create-menu ()
4605 "Create the Ada menu as shown in the menu bar."
4608 ["Ada Mode" (info "ada-mode") t]
4609 ["GNAT User's Guide" (info "gnat_ugn")
4610 (eq ada-which-compiler 'gnat)]
4611 ["GNAT Reference Manual" (info "gnat_rm")
4612 (eq ada-which-compiler 'gnat)]
4613 ["Gcc Documentation" (info "gcc")
4614 (eq ada-which-compiler 'gnat)]
4615 ["Gdb Documentation" (info "gdb")
4616 (eq ada-which-compiler 'gnat)]
4617 ["Ada95 Reference Manual" (info "arm95") t])
4618 ("Options" :included (derived-mode-p 'ada-mode)
4619 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4620 :style toggle :selected ada-auto-case]
4621 ["Auto Indent After Return"
4622 (setq ada-indent-after-return (not ada-indent-after-return))
4623 :style toggle :selected ada-indent-after-return]
4624 ["Automatically Recompile For Cross-references"
4625 (setq ada-xref-create-ali (not ada-xref-create-ali))
4626 :style toggle :selected ada-xref-create-ali
4627 :included (eq ada-which-compiler 'gnat)]
4629 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4630 :style toggle :selected ada-xref-confirm-compile
4631 :included (eq ada-which-compiler 'gnat)]
4632 ["Show Cross-references In Other Buffer"
4633 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4634 :style toggle :selected ada-xref-other-buffer
4635 :included (eq ada-which-compiler 'gnat)]
4636 ["Tight Integration With GNU Visual Debugger"
4637 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4638 :style toggle :selected ada-tight-gvd-integration
4639 :included (string-match "gvd" ada-prj-default-debugger)])
4640 ["Customize" (customize-group 'ada)
4641 :included (fboundp 'customize-group)]
4642 ["Check file" ada-check-current t]
4643 ["Compile file" ada-compile-current t]
4644 ["Set main and Build" ada-set-main-compile-application t]
4645 ["Show main" ada-show-current-main t]
4646 ["Build" ada-compile-application t]
4647 ["Run" ada-run-application t]
4648 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4651 ["Show project" ada-show-current-project t]
4652 ["Load..." ada-set-default-project-file t]
4653 ["New..." ada-prj-new t]
4654 ["Edit..." ada-prj-edit t])
4655 ("Goto" :included (derived-mode-p 'ada-mode)
4656 ["Goto Declaration/Body" ada-goto-declaration
4657 (eq ada-which-compiler 'gnat)]
4658 ["Goto Body" ada-goto-body
4659 (eq ada-which-compiler 'gnat)]
4660 ["Goto Declaration Other Frame"
4661 ada-goto-declaration-other-frame
4662 (eq ada-which-compiler 'gnat)]
4663 ["Goto Previous Reference" ada-xref-goto-previous-reference
4664 (eq ada-which-compiler 'gnat)]
4665 ["List Local References" ada-find-local-references
4666 (eq ada-which-compiler 'gnat)]
4667 ["List References" ada-find-references
4668 (eq ada-which-compiler 'gnat)]
4669 ["Goto Reference To Any Entity" ada-find-any-references
4670 (eq ada-which-compiler 'gnat)]
4671 ["Goto Parent Unit" ada-goto-parent
4672 (eq ada-which-compiler 'gnat)]
4674 ["Next compilation error" next-error t]
4675 ["Previous Package" ada-previous-package t]
4676 ["Next Package" ada-next-package t]
4677 ["Previous Procedure" ada-previous-procedure t]
4678 ["Next Procedure" ada-next-procedure t]
4679 ["Goto Start Of Statement" ada-move-to-start t]
4680 ["Goto End Of Statement" ada-move-to-end t]
4682 ["Other File" ff-find-other-file t]
4683 ["Other File Other Window" ada-ff-other-window t])
4684 ("Edit" :included (derived-mode-p 'ada-mode)
4685 ["Search File On Source Path" ada-find-file t]
4687 ["Complete Identifier" ada-complete-identifier t]
4689 ["Indent Line" ada-indent-current-function t]
4690 ["Justify Current Indentation" ada-justified-indent-current t]
4691 ["Indent Lines in Selection" ada-indent-region t]
4692 ["Indent Lines in File"
4693 (ada-indent-region (point-min) (point-max)) t]
4694 ["Format Parameter List" ada-format-paramlist t]
4696 ["Comment Selection" comment-region t]
4697 ["Uncomment Selection" ada-uncomment-region t]
4699 ["Fill Comment Paragraph" fill-paragraph t]
4700 ["Fill Comment Paragraph Justify"
4701 ada-fill-comment-paragraph-justify t]
4702 ["Fill Comment Paragraph Postfix"
4703 ada-fill-comment-paragraph-postfix t]
4705 ["Adjust Case Selection" ada-adjust-case-region t]
4706 ["Adjust Case in File" ada-adjust-case-buffer t]
4707 ["Create Case Exception" ada-create-case-exception t]
4708 ["Create Case Exception Substring"
4709 ada-create-case-exception-substring t]
4710 ["Reload Case Exceptions" ada-case-read-exceptions t]
4712 ["Make body for subprogram" ada-make-subprogram-body t]
4714 ["Narrow to subprogram" ada-narrow-to-defun t])
4716 :included (derived-mode-p 'ada-mode)
4717 ["Header" ada-header t]
4719 ["Package Body" ada-package-body t]
4720 ["Package Spec" ada-package-spec t]
4721 ["Function Spec" ada-function-spec t]
4722 ["Procedure Spec" ada-procedure-spec t]
4723 ["Proc/func Body" ada-subprogram-body t]
4724 ["Task Body" ada-task-body t]
4725 ["Task Spec" ada-task-spec t]
4726 ["Declare Block" ada-declare-block t]
4727 ["Exception Block" ada-exception-block t]
4729 ["Entry" ada-entry t]
4730 ["Entry family" ada-entry-family t]
4731 ["Select" ada-select t]
4732 ["Accept" ada-accept t]
4733 ["Or accept" ada-or-accep t]
4734 ["Or delay" ada-or-delay t]
4735 ["Or terminate" ada-or-terminate t]
4738 ["Private" ada-private t]
4739 ["Subtype" ada-subtype t]
4740 ["Record" ada-record t]
4741 ["Array" ada-array t]
4745 ["Elsif" ada-elsif t]
4748 ["While Loop" ada-while-loop t]
4749 ["For Loop" ada-for-loop t]
4752 ["Exception" ada-exception t]
4754 ["When" ada-when t])
4757 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4758 (if (featurep 'xemacs)
4760 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4761 (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4764 ;; -------------------------------------------------------
4765 ;; Commenting/Uncommenting code
4766 ;; The following two calls are provided to enhance the standard
4767 ;; comment-region function, which only allows uncommenting if the
4768 ;; comment is at the beginning of a line. If the line have been re-indented,
4769 ;; we are unable to use comment-region, which makes no sense.
4771 ;; In addition, we provide an interface to the standard comment handling
4772 ;; function for justifying the comments.
4773 ;; -------------------------------------------------------
4775 (defadvice comment-region (before ada-uncomment-anywhere disable)
4776 (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
4777 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4778 (derived-mode-p 'ada-mode))
4780 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4782 (while (re-search-forward cs end t)
4783 (replace-match comment-start))
4786 (defun ada-uncomment-region (beg end &optional arg)
4787 "Uncomment region BEG .. END.
4788 ARG gives number of comment characters."
4789 (interactive "r\nP")
4791 ;; This advice is not needed anymore with Emacs21. However, for older
4792 ;; versions, as well as for XEmacs, we still need to enable it.
4793 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4795 (ad-activate 'comment-region)
4796 (comment-region beg end (- (or arg 2)))
4797 (ad-deactivate 'comment-region))
4798 (comment-region beg end (list (- (or arg 2))))
4799 (ada-indent-region beg end)))
4801 (defun ada-fill-comment-paragraph-justify ()
4802 "Fill current comment paragraph and justify each line as well."
4804 (ada-fill-comment-paragraph 'full))
4806 (defun ada-fill-comment-paragraph-postfix ()
4807 "Fill current comment paragraph and justify each line as well.
4808 Adds `ada-fill-comment-postfix' at the end of each line."
4810 (ada-fill-comment-paragraph 'full t))
4812 (defun ada-fill-comment-paragraph (&optional justify postfix)
4813 "Fill the current comment paragraph.
4814 If JUSTIFY is non-nil, each line is justified as well.
4815 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4816 to each line filled and justified.
4817 The paragraph is indented on the first line."
4820 ;; check if inside comment or just in front a comment
4821 (if (and (not (ada-in-comment-p))
4822 (not (looking-at "[ \t]*--")))
4823 (error "Not inside comment"))
4825 (let* (indent from to
4826 (opos (point-marker))
4828 ;; Sets this variable to nil, otherwise it prevents
4829 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4830 (parse-sexp-lookup-properties nil)
4833 (fill-column (current-fill-column)))
4835 ;; Find end of paragraph
4836 (back-to-indentation)
4837 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4840 ;; If we were at the last line in the buffer, create a dummy empty
4841 ;; line at the end of the buffer.
4844 (back-to-indentation)))
4846 (setq to (point-marker))
4849 ;; Find beginning of paragraph
4850 (back-to-indentation)
4851 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4853 (back-to-indentation))
4855 ;; We want one line above the first one, unless we are at the beginning
4860 (setq from (point-marker))
4862 ;; Calculate the indentation we will need for the paragraph
4863 (back-to-indentation)
4864 (setq indent (current-column))
4865 ;; unindent the first line of the paragraph
4866 (delete-region from (point))
4868 ;; Remove the old postfixes
4870 (while (re-search-forward "--\n" to t)
4871 (replace-match "\n"))
4874 (setq to (point-marker))
4876 ;; Indent and justify the paragraph
4877 (setq fill-prefix ada-fill-comment-prefix)
4878 (set-left-margin from to indent)
4880 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4882 (fill-region-as-paragraph from to justify)
4884 ;; Add the postfixes if required
4888 (narrow-to-region from to)
4891 (insert-char ? (- fill-column (current-column)))
4892 (insert ada-fill-comment-postfix)
4896 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4897 ;; inserted at the end. Delete it
4898 (if (or (featurep 'xemacs)
4899 (<= emacs-major-version 19)
4900 (and (= emacs-major-version 20)
4901 (<= emacs-minor-version 2)))
4910 ;; ---------------------------------------------------
4911 ;; support for find-file.el
4912 ;; These functions are used by find-file to guess the file names from
4913 ;; unit names, and to find the other file (spec or body) from the current
4914 ;; file (body or spec).
4915 ;; It is also used to find in which function we are, so as to put the
4916 ;; cursor at the correct position.
4917 ;; Standard Ada does not force any relation between unit names and file names,
4918 ;; so some of these functions can only be a good approximation. However, they
4919 ;; are also overridden in `ada-xref'.el when we know that the user is using
4921 ;; ---------------------------------------------------
4923 ;; Overridden when we work with GNAT, to use gnatkrunch
4924 (defun ada-make-filename-from-adaname (adaname)
4925 "Determine the filename in which ADANAME is found.
4926 This matches the GNAT default naming convention, except for
4928 (while (string-match "\\." adaname)
4929 (setq adaname (replace-match "-" t t adaname)))
4933 (defun ada-other-file-name ()
4934 "Return the name of the other file.
4935 The name returned is the body if `current-buffer' is the spec,
4936 or the spec otherwise."
4940 (suffixes ada-spec-suffixes)
4941 (name (buffer-file-name)))
4943 ;; Guess whether we have a spec or a body, and get the basename of the
4944 ;; file. Since the extension may not start with '.', we can not use
4945 ;; file-name-extension
4946 (while (and (not is-spec)
4948 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4950 name (match-string 1 name)))
4951 (setq suffixes (cdr suffixes)))
4955 (setq suffixes ada-body-suffixes)
4956 (while (and (not is-body)
4958 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4960 name (match-string 1 name)))
4961 (setq suffixes (cdr suffixes)))))
4963 ;; If this wasn't in either list, return name itself
4964 (if (not (or is-spec is-body))
4967 ;; Else find the other possible names
4969 (setq suffixes ada-body-suffixes)
4970 (setq suffixes ada-spec-suffixes))
4975 ;; If we are using project file, search for the other file in all
4976 ;; the possible src directories.
4978 (if (fboundp 'ada-find-src-file-in-dir)
4980 (ada-find-src-file-in-dir
4981 (file-name-nondirectory (concat name (car suffixes))))))
4983 (setq is-spec other)))
4985 ;; Else search in the current directory
4986 (if (file-exists-p (concat name (car suffixes)))
4987 (setq is-spec (concat name (car suffixes)))))
4988 (setq suffixes (cdr suffixes)))
4992 (defun ada-which-function-are-we-in ()
4993 "Return the name of the function whose definition/declaration point is in.
4994 Used in `ff-pre-load-hook'."
4995 (setq ff-function-name nil)
4997 (end-of-line);; make sure we get the complete name
4998 (or (if (re-search-backward ada-procedure-start-regexp nil t)
4999 (setq ff-function-name (match-string 5)))
5000 (if (re-search-backward ada-package-start-regexp nil t)
5001 (setq ff-function-name (match-string 4))))
5005 (defvar ada-last-which-function-line -1
5006 "Last line on which `ada-which-function' was called.")
5007 (defvar ada-last-which-function-subprog 0
5008 "Last subprogram name returned by `ada-which-function'.")
5009 (make-variable-buffer-local 'ada-last-which-function-subprog)
5010 (make-variable-buffer-local 'ada-last-which-function-line)
5013 (defun ada-which-function ()
5014 "Return the name of the function whose body the point is in.
5015 This function works even in the case of nested subprograms, whereas the
5016 standard Emacs function `which-function' does not.
5017 Since the search can be long, the results are cached."
5019 (let ((line (count-lines 1 (point)))
5025 ;; If this is the same line as before, simply return the same result
5026 (if (= line ada-last-which-function-line)
5027 ada-last-which-function-subprog
5030 ;; In case the current line is also the beginning of the body
5033 ;; Are we looking at "function Foo\n (paramlist)"
5034 (skip-chars-forward " \t\n(")
5040 (skip-chars-forward " \t\n")
5041 (if (looking-at "return")
5044 (skip-chars-forward " \t\n")
5045 (skip-chars-forward "a-zA-Z0-9_'")))
5047 ;; Can't simply do forward-word, in case the "is" is not on the
5048 ;; same line as the closing parenthesis
5049 (skip-chars-forward "is \t\n")
5051 ;; No look for the closest subprogram body that has not ended yet.
5052 ;; Not that we expect all the bodies to be finished by "end <name>",
5053 ;; or a simple "end;" indented in the same column as the start of
5054 ;; the subprogram. The goal is to be as efficient as possible.
5056 (while (and (not found)
5057 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5059 ;; Get the function name, but not the properties, or this changes
5060 ;; the face in the modeline on Emacs 21
5061 (setq func-name (match-string-no-properties 3))
5062 (if (and (not (ada-in-comment-p))
5063 (not (save-excursion
5064 (goto-char (match-end 0))
5065 (looking-at "[ \t\n]*new"))))
5067 (back-to-indentation)
5068 (setq indent (current-column))
5069 (if (ada-search-ignore-string-comment
5070 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5071 (make-string indent ? ) "end;"))
5072 (setq end-pos (point))
5073 (setq end-pos (point-max)))
5074 (if (>= end-pos pos)
5075 (setq found func-name))))
5077 (setq ada-last-which-function-line line
5078 ada-last-which-function-subprog found)
5081 (defun ada-ff-other-window ()
5082 "Find other file in other window using `ff-find-other-file'."
5084 (and (fboundp 'ff-find-other-file)
5085 (ff-find-other-file t)))
5087 (defun ada-set-point-accordingly ()
5088 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5089 (if ff-function-name
5091 (goto-char (point-min))
5092 (unless (ada-search-ignore-string-comment
5093 (concat ff-function-name "\\b") nil)
5094 (goto-char (point-min))))))
5096 (defun ada-get-body-name (&optional spec-name)
5097 "Return the file name for the body of SPEC-NAME.
5098 If SPEC-NAME is nil, return the body for the current package.
5099 Return nil if no body was found."
5102 (unless spec-name (setq spec-name (buffer-file-name)))
5104 ;; Remove the spec extension. We can not simply remove the file extension,
5105 ;; but we need to take into account the specific non-GNAT extensions that the
5106 ;; user might have specified.
5108 (let ((suffixes ada-spec-suffixes)
5111 (setq end (- (length spec-name) (length (car suffixes))))
5112 (if (string-equal (car suffixes) (substring spec-name end))
5113 (setq spec-name (substring spec-name 0 end)))
5114 (setq suffixes (cdr suffixes))))
5116 ;; If find-file.el was available, use its functions
5117 (if (fboundp 'ff-get-file-name)
5118 (ff-get-file-name ada-search-directories-internal
5119 (ada-make-filename-from-adaname
5120 (file-name-nondirectory
5121 (file-name-sans-extension spec-name)))
5123 ;; Else emulate it very simply
5124 (concat (ada-make-filename-from-adaname
5125 (file-name-nondirectory
5126 (file-name-sans-extension spec-name)))
5130 ;; ---------------------------------------------------
5131 ;; support for font-lock.el
5132 ;; Strings are a real pain in Ada because a single quote character is
5133 ;; overloaded as a string quote and type/instance delimiter. By default, a
5134 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5135 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5136 ;; syntax when the gods that created Ada determine them to be.
5138 ;; This only works in Emacs. See the comments before the grammar functions
5139 ;; at the beginning of this file for how this is done with XEmacs.
5140 ;; ----------------------------------------------------
5142 (defconst ada-font-lock-syntactic-keywords
5143 ;; Mark single quotes as having string quote syntax in 'c' instances.
5144 ;; We used to explicitly avoid ''' as a special case for fear the buffer
5145 ;; be highlighted as a string, but it seems this fear is unfounded.
5147 ;; This sets the properties of the characters, so that ada-in-string-p
5148 ;; correctly handles '"' too...
5149 '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5150 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5152 (defvar ada-font-lock-keywords
5156 ;; handle "type T is access function return S;"
5157 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5159 ;; preprocessor line
5160 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5163 ;; accept, entry, function, package (body), protected (body|type),
5164 ;; pragma, procedure, task (body) plus name.
5170 "package[ \t]+body\\|"
5174 "protected[ \t]+body\\|"
5175 "protected[ \t]+type\\|"
5181 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5182 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5184 ;; Optional keywords followed by a type name.
5185 (list (concat ; ":[ \t]*"
5186 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5188 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5189 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5192 ;; Main keywords, except those treated specially below.
5195 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5196 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5197 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5198 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5199 "null" "or" "others" "overriding" "private" "protected" "raise"
5200 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5201 "select" "separate" "synchronized" "tagged" "task" "terminate"
5202 "then" "until" "when" "while" "with" "xor") t)
5205 ;; Anything following end and not already fontified is a body name.
5206 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5207 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5209 ;; Keywords followed by a type or function name.
5210 (list (concat "\\<\\("
5211 "new\\|of\\|subtype\\|type"
5212 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5213 '(1 font-lock-keyword-face)
5214 '(2 (if (match-beginning 4)
5215 font-lock-function-name-face
5216 font-lock-type-face) nil t))
5218 ;; Keywords followed by a (comma separated list of) reference.
5219 ;; Note that font-lock only works on single lines, thus we can not
5220 ;; correctly highlight a with_clause that spans multiple lines.
5221 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5222 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5223 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
5227 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
5229 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5230 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5232 ;; Ada unnamed numerical constants
5233 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5236 "Default expressions to highlight in Ada mode.")
5239 ;; ---------------------------------------------------------
5240 ;; Support for outline.el
5241 ;; ---------------------------------------------------------
5243 (defun ada-outline-level ()
5244 "This is so that `current-column' DTRT in otherwise-hidden text."
5245 ;; patch from Dave Love <fx@gnu.org>
5246 (let (buffer-invisibility-spec)
5248 (back-to-indentation)
5251 ;; ---------------------------------------------------------
5252 ;; Support for narrow-to-region
5253 ;; ---------------------------------------------------------
5255 (defun ada-narrow-to-defun (&optional arg)
5256 "Make text outside current subprogram invisible.
5257 The subprogram visible is the one that contains or follow point.
5258 Optional ARG is ignored.
5259 Use \\[widen] to go back to the full visibility for the buffer."
5266 (ada-previous-procedure)
5267 (setq end (point-at-bol))
5270 (narrow-to-region end (point))
5272 "Use M-x widen to get back to full visibility in the buffer"))))
5274 ;; ---------------------------------------------------------
5275 ;; Automatic generation of code
5276 ;; The Ada mode has a set of function to automatically generate a subprogram
5277 ;; or package body from its spec.
5278 ;; These function only use a primary and basic algorithm, this could use a
5279 ;; lot of improvement.
5280 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5282 ;; ----------------------------------------------------------
5284 (defun ada-gen-treat-proc (match)
5285 "Make dummy body of a procedure/function specification.
5286 MATCH is a cons cell containing the start and end locations of the last search
5287 for `ada-procedure-start-regexp'."
5288 (goto-char (car match))
5289 (let (func-found procname functype)
5291 ((or (looking-at "^[ \t]*procedure")
5292 (setq func-found (looking-at "^[ \t]*function")))
5293 ;; treat it as a proc/func
5296 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5298 ;; goto end of procname
5299 (goto-char (cdr match))
5301 ;; skip over parameterlist
5302 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5305 ;; if function, skip over 'return' and result type.
5309 (skip-chars-forward " \t\n")
5310 (setq functype (buffer-substring (point)
5315 ;; look for next non WS
5317 ((looking-at "[ \t]*;")
5318 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5319 (ada-indent-newline-indent)
5321 (ada-indent-newline-indent)
5324 (insert "Result : " functype ";")
5325 (ada-indent-newline-indent)))
5327 (ada-indent-newline-indent)
5329 (insert "return Result;")
5331 (ada-indent-newline-indent)
5332 (insert "end " procname ";")
5333 (ada-indent-newline-indent)
5336 ((looking-at "[ \t\n]*is")
5340 ((looking-at "[ \t\n]*rename")
5345 (message "unknown syntax"))))
5347 (if (looking-at "^[ \t]*task")
5349 (message "Task conversion is not yet implemented")
5351 (if (looking-at "[ \t]*;")
5356 (defun ada-make-body ()
5357 "Create an Ada package body in the current buffer.
5358 The spec must be the previously visited buffer.
5359 This function typically is to be hooked into `ff-file-created-hook'."
5360 (delete-region (point-min) (point-max))
5361 (insert-buffer-substring (car (cdr (buffer-list))))
5362 (goto-char (point-min))
5365 (let (found ada-procedure-or-package-start-regexp)
5367 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5368 (progn (goto-char (cdr found))
5371 (error "No package"))
5373 (setq ada-procedure-or-package-start-regexp
5374 (concat ada-procedure-start-regexp
5376 ada-package-start-regexp))
5379 (ada-search-ignore-string-comment
5380 ada-procedure-or-package-start-regexp nil))
5382 (goto-char (car found))
5383 (if (looking-at ada-package-start-regexp)
5384 (progn (goto-char (cdr found))
5386 (ada-gen-treat-proc found))))))
5389 (defun ada-make-subprogram-body ()
5390 "Create a dummy subprogram body in package body file from spec surrounding point."
5392 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5393 (spec (match-beginning 0))
5398 (if (and (re-search-forward "(\\|;" nil t)
5399 (= (char-before) ?\())
5401 (ada-search-ignore-string-comment ")" nil)
5402 (ada-search-ignore-string-comment ";" nil)))
5403 (setq spec (buffer-substring spec (point)))
5405 ;; If find-file.el was available, use its functions
5406 (setq body-file (ada-get-body-name))
5408 (find-file body-file)
5409 (error "No body found for the package. Create it first"))
5413 (goto-char (point-max))
5414 (forward-comment -10000)
5415 (re-search-backward "\\<end\\>" nil t)
5416 ;; Move to the beginning of the elaboration part, if any
5417 (re-search-backward "^begin" nil t)
5421 (re-search-backward ada-procedure-start-regexp nil t)
5422 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5424 (error "Not in subprogram spec"))))
5426 ;; --------------------------------------------------------
5427 ;; Global initializations
5428 ;; --------------------------------------------------------
5430 ;; Create the keymap once and for all. If we do that in ada-mode,
5431 ;; the keys changed in the user's .emacs have to be modified
5436 ;; Create the syntax tables, but do not activate them
5437 (ada-create-syntax-table)
5439 ;; Add the default extensions (and set up speedbar)
5440 (ada-add-extensions ".ads" ".adb")
5441 ;; This two files are generated by GNAT when running with -gnatD
5442 (if (equal ada-which-compiler 'gnat)
5443 (ada-add-extensions ".ads.dg" ".adb.dg"))
5445 ;; Read the special cases for exceptions
5446 (ada-case-read-exceptions)
5448 ;; Setup auto-loading of the other Ada mode files.
5449 (autoload 'ada-change-prj "ada-xref" nil t)
5450 (autoload 'ada-check-current "ada-xref" nil t)
5451 (autoload 'ada-compile-application "ada-xref" nil t)
5452 (autoload 'ada-compile-current "ada-xref" nil t)
5453 (autoload 'ada-complete-identifier "ada-xref" nil t)
5454 (autoload 'ada-find-file "ada-xref" nil t)
5455 (autoload 'ada-find-any-references "ada-xref" nil t)
5456 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5457 (autoload 'ada-find-local-references "ada-xref" nil t)
5458 (autoload 'ada-find-references "ada-xref" nil t)
5459 (autoload 'ada-gdb-application "ada-xref" nil t)
5460 (autoload 'ada-goto-declaration "ada-xref" nil t)
5461 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5462 (autoload 'ada-goto-parent "ada-xref" nil t)
5463 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5464 (autoload 'ada-point-and-xref "ada-xref" nil t)
5465 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5466 (autoload 'ada-run-application "ada-xref" nil t)
5467 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5468 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5469 (autoload 'ada-set-main-compile-application "ada-xref" nil t)
5470 (autoload 'ada-show-current-main "ada-xref" nil t)
5472 (autoload 'ada-customize "ada-prj" nil t)
5473 (autoload 'ada-prj-edit "ada-prj" nil t)
5474 (autoload 'ada-prj-new "ada-prj" nil t)
5475 (autoload 'ada-prj-save "ada-prj" nil t)
5477 (autoload 'ada-array "ada-stmt" nil t)
5478 (autoload 'ada-case "ada-stmt" nil t)
5479 (autoload 'ada-declare-block "ada-stmt" nil t)
5480 (autoload 'ada-else "ada-stmt" nil t)
5481 (autoload 'ada-elsif "ada-stmt" nil t)
5482 (autoload 'ada-exception "ada-stmt" nil t)
5483 (autoload 'ada-exception-block "ada-stmt" nil t)
5484 (autoload 'ada-exit "ada-stmt" nil t)
5485 (autoload 'ada-for-loop "ada-stmt" nil t)
5486 (autoload 'ada-function-spec "ada-stmt" nil t)
5487 (autoload 'ada-header "ada-stmt" nil t)
5488 (autoload 'ada-if "ada-stmt" nil t)
5489 (autoload 'ada-loop "ada-stmt" nil t)
5490 (autoload 'ada-package-body "ada-stmt" nil t)
5491 (autoload 'ada-package-spec "ada-stmt" nil t)
5492 (autoload 'ada-private "ada-stmt" nil t)
5493 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5494 (autoload 'ada-record "ada-stmt" nil t)
5495 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5496 (autoload 'ada-subtype "ada-stmt" nil t)
5497 (autoload 'ada-tabsize "ada-stmt" nil t)
5498 (autoload 'ada-task-body "ada-stmt" nil t)
5499 (autoload 'ada-task-spec "ada-stmt" nil t)
5500 (autoload 'ada-type "ada-stmt" nil t)
5501 (autoload 'ada-use "ada-stmt" nil t)
5502 (autoload 'ada-when "ada-stmt" nil t)
5503 (autoload 'ada-while-loop "ada-stmt" nil t)
5504 (autoload 'ada-with "ada-stmt" nil t)
5506 ;;; provide ourselves
5509 ;;; ada-mode.el ends here