1 ;;; ada-mode.el --- major-mode for editing Ada sources
3 ;; Copyright (C) 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Rolf Ebert <ebert@inf.enst.fr>
7 ;; Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
8 ;; Emmanuel Briot <briot@gnat.com>
9 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
10 ;; Ada Core Technologies's version: Revision: 1.188
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 2, or (at your option)
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; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
31 ;;; This mode is a major mode for editing Ada83 and Ada95 source code.
32 ;;; This is a major rewrite of the file packaged with Emacs-20. The
33 ;;; ada-mode is composed of four lisp files, ada-mode.el, ada-xref.el,
34 ;;; ada-prj.el and ada-stmt.el. Only this file (ada-mode.el) is
35 ;;; completely independent from the GNU Ada compiler Gnat, distributed
36 ;;; by Ada Core Technologies. All the other files rely heavily on
37 ;;; features provided only by Gnat.
39 ;;; Note: this mode will not work with Emacs 19. If you are on a VMS
40 ;;; system, where the latest version of Emacs is 19.28, you will need
41 ;;; another file, called ada-vms.el, that provides some required
45 ;;; Emacs should enter Ada mode automatically when you load an Ada file.
46 ;;; By default, the valid extensions for Ada files are .ads, .adb or .ada
47 ;;; If the ada-mode does not start automatically, then simply type the
48 ;;; following command :
51 ;;; By default, ada-mode is configured to take full advantage of the GNAT
52 ;;; compiler (the menus will include the cross-referencing features,...).
53 ;;; If you are using another compiler, you might want to set the following
54 ;;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
56 ;;; (setq ada-which-compiler 'generic)
58 ;;; This mode requires find-file.el to be present on your system.
61 ;;; The first Ada mode for GNU Emacs was written by V. Broman in
62 ;;; 1985. He based his work on the already existing Modula-2 mode.
63 ;;; This was distributed as ada.el in versions of Emacs prior to 19.29.
65 ;;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
66 ;;; several files with support for dired commands and other nice
67 ;;; things. It is currently available from the PAL
68 ;;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
70 ;;; The probably very first Ada mode (called electric-ada.el) was
71 ;;; written by Steven D. Litvintchouk and Steven M. Rosen for the
72 ;;; Gosling Emacs. L. Slater based his development on ada.el and
75 ;;; A complete rewrite by M. Heritsch and R. Ebert has been done.
76 ;;; Some ideas from the Ada mode mailing list have been
77 ;;; added. Some of the functionality of L. Slater's mode has not
78 ;;; (yet) been recoded in this new mode. Perhaps you prefer sticking
81 ;;; A complete rewrite for Emacs-20 / Gnat-3.11 has been done by Ada Core
82 ;;; Technologies. Please send bugs to briot@gnat.com
85 ;;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
86 ;;; many patches included in this package.
87 ;;; Christian Egli <Christian.Egli@hcsd.hac.com>:
88 ;;; ada-imenu-generic-expression
89 ;;; Many thanks also to the following persons that have contributed one day
91 ;;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
92 ;;; woodruff@stc.llnl.gov (John Woodruff)
93 ;;; jj@ddci.dk (Jesper Joergensen)
94 ;;; gse@ocsystems.com (Scott Evans)
95 ;;; comar@gnat.com (Cyrille Comar)
96 ;;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
97 ;;; robin-reply@reagans.org
98 ;;; and others for their valuable hints.
101 ;;; Note: Every function in this package is compiler-independent.
102 ;;; The names start with ada-
103 ;;; The variables that the user can edit can all be modified through
104 ;;; the customize mode. They are sorted in alphabetical order in this
107 ;;; Supported packages.
108 ;;; This package supports a number of other Emacs modes. These other modes
109 ;;; should be loaded before the ada-mode, which will then setup some variables
110 ;;; to improve the support for Ada code.
111 ;;; Here is the list of these modes:
112 ;;; `which-function-mode': Display the name of the subprogram the cursor is
113 ;;; in in the mode line.
114 ;;; `outline-mode': Provides the capability to collapse or expand the code
115 ;;; for specific language constructs, for instance if you want to hide the
116 ;;; code corresponding to a subprogram
117 ;;; `align': This mode is now provided with Emacs 21, but can also be
118 ;;; installed manually for older versions of Emacs. It provides the
119 ;;; capability to automatically realign the selected region (for instance
120 ;;; all ':=', ':' and '--' will be aligned on top of each other.
121 ;;; `imenu': Provides a menu with the list of entities defined in the current
122 ;;; buffer, and an easy way to jump to any of them
123 ;;; `speedbar': Provides a separate file browser, and the capability for each
124 ;;; file to see the list of entities defined in it and to jump to them
126 ;;; `abbrev-mode': Provides the capability to define abbreviations, which
127 ;;; are automatically expanded when you type them. See the Emacs manual.
130 (require 'ispell nil t)
131 (require 'find-file nil t)
132 (require 'align nil t)
133 (require 'which-func nil t)
134 (require 'compile nil t))
136 (defvar compile-auto-highlight)
137 (defvar skeleton-further-elements)
139 ;; this function is needed at compile time
141 (defun ada-check-emacs-version (major minor &optional is-xemacs)
142 "Return t if Emacs's version is greater or equal to MAJOR.MINOR.
143 If IS-XEMACS is non-nil, check for XEmacs instead of Emacs."
144 (let ((xemacs-running (or (string-match "Lucid" emacs-version)
145 (string-match "XEmacs" emacs-version))))
146 (and (or (and is-xemacs xemacs-running)
147 (not (or is-xemacs xemacs-running)))
148 (or (> emacs-major-version major)
149 (and (= emacs-major-version major)
150 (>= emacs-minor-version minor)))))))
153 ;; This call should not be made in the release that is done for the
154 ;; official Emacs, since it does nothing useful for the latest version
155 ;;(if (not (ada-check-emacs-version 21 1))
156 ;; (require 'ada-support))
158 (defvar ada-mode-hook nil
159 "*List of functions to call when Ada mode is invoked.
160 This hook is automatically executed after the `ada-mode' is
162 This is a good place to add Ada environment specific bindings.")
165 "Major mode for editing Ada source in Emacs."
168 (defcustom ada-auto-case t
169 "*Non-nil means automatically change case of preceding word while typing.
170 Casing is done according to `ada-case-keyword', `ada-case-identifier'
171 and `ada-case-attribute'."
172 :type 'boolean :group 'ada)
174 (defcustom ada-broken-decl-indent 0
175 "*Number of columns to indent a broken declaration.
180 >>>>>B : Integer; -- from ada-broken-decl-indent"
181 :type 'integer :group 'ada)
183 (defcustom ada-broken-indent 2
184 "*Number of columns to indent the continuation of a broken line.
187 My_Var : My_Type := (Field1 =>
188 >>>>>>>>>Value); -- from ada-broken-indent"
189 :type 'integer :group 'ada)
191 (defcustom ada-continuation-indent ada-broken-indent
192 "*Number of columns to indent the continuation of broken lines in parenthesis.
197 :type 'integer :group 'ada)
199 (defcustom ada-case-attribute 'ada-capitalize-word
200 "*Function to call to adjust the case of Ada attributes.
201 It may be `downcase-word', `upcase-word', `ada-loose-case-word',
202 `ada-capitalize-word' or `ada-no-auto-case'."
203 :type '(choice (const downcase-word)
205 (const ada-capitalize-word)
206 (const ada-loose-case-word)
207 (const ada-no-auto-case))
210 (defcustom ada-case-exception-file
211 (list (convert-standard-filename' "~/.emacs_case_exceptions"))
212 "*List of special casing exceptions dictionaries for identifiers.
213 The first file is the one where new exceptions will be saved by Emacs
214 when you call `ada-create-case-exception'.
216 These files should contain one word per line, that gives the casing
217 to be used for that word in Ada files. If the line starts with the
218 character *, then the exception will be used for substrings that either
219 start at the beginning of a word or after a _ character, and end either
220 at the end of the word or at a _ character. Each line can be terminated by
222 :type '(repeat (file))
225 (defcustom ada-case-keyword 'downcase-word
226 "*Function to call to adjust the case of an Ada keywords.
227 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
228 `ada-capitalize-word'."
229 :type '(choice (const downcase-word)
231 (const ada-capitalize-word)
232 (const ada-loose-case-word)
233 (const ada-no-auto-case))
236 (defcustom ada-case-identifier 'ada-loose-case-word
237 "*Function to call to adjust the case of an Ada identifier.
238 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
239 `ada-capitalize-word'."
240 :type '(choice (const downcase-word)
242 (const ada-capitalize-word)
243 (const ada-loose-case-word)
244 (const ada-no-auto-case))
247 (defcustom ada-clean-buffer-before-saving t
248 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
249 :type 'boolean :group 'ada)
251 (defcustom ada-indent 3
252 "*Size of Ada indentation.
257 >>>>>>>>>>null; -- from ada-indent"
258 :type 'integer :group 'ada)
260 (defcustom ada-indent-after-return t
261 "*Non-nil means automatically indent after RET or LFD."
262 :type 'boolean :group 'ada)
264 (defcustom ada-indent-align-comments t
265 "*Non-nil means align comments on previous line comments, if any.
266 If nil, indentation is calculated as usual.
267 Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
270 A := 1; -- A multi-line comment
271 -- aligned if ada-indent-align-comments is t"
272 :type 'boolean :group 'ada)
274 (defcustom ada-indent-comment-as-code t
275 "*Non-nil means indent comment lines as code.
276 A nil value means do not auto-indent comments."
277 :type 'boolean :group 'ada)
279 (defcustom ada-indent-handle-comment-special nil
280 "*Non-nil if comment lines should be handled specially inside parenthesis.
281 By default, if the line that contains the open parenthesis has some
282 text following it, then the following lines will be indented in the
283 same column as this text. This will not be true if the first line is
284 a comment and `ada-indent-handle-comment-special' is t.
287 ( Value_1, -- common behavior, when not a comment
291 ( -- `ada-indent-handle-comment-special' is nil
296 ( -- `ada-indent-handle-comment-special' is non-nil
299 :type 'boolean :group 'ada)
301 (defcustom ada-indent-is-separate t
302 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
303 :type 'boolean :group 'ada)
305 (defcustom ada-indent-record-rel-type 3
306 "*Indentation for 'record' relative to 'type' or 'use'.
310 >>>>>>>>>>>record -- from ada-indent-record-rel-type"
311 :type 'integer :group 'ada)
313 (defcustom ada-indent-renames ada-broken-indent
314 "*Indentation for renames relative to the matching function statement.
315 If `ada-indent-return' is null or negative, the indentation is done relative to
316 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
319 function A (B : Integer)
320 return C; -- from ada-indent-return
321 >>>renames Foo; -- from ada-indent-renames"
322 :type 'integer :group 'ada)
324 (defcustom ada-indent-return 0
325 "*Indentation for 'return' relative to the matching 'function' statement.
326 If `ada-indent-return' is null or negative, the indentation is done relative to
327 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
330 function A (B : Integer)
331 >>>>>return C; -- from ada-indent-return"
332 :type 'integer :group 'ada)
334 (defcustom ada-indent-to-open-paren t
335 "*Non-nil means indent according to the innermost open parenthesis."
336 :type 'boolean :group 'ada)
338 (defcustom ada-fill-comment-prefix "-- "
339 "*Text inserted in the first columns when filling a comment paragraph.
340 Note: if you modify this variable, you will have to invoke `ada-mode'
341 again to take account of the new value."
342 :type 'string :group 'ada)
344 (defcustom ada-fill-comment-postfix " --"
345 "*Text inserted at the end of each line when filling a comment paragraph.
346 Used by `ada-fill-comment-paragraph-postfix'."
347 :type 'string :group 'ada)
349 (defcustom ada-label-indent -4
350 "*Number of columns to indent a label.
355 >>>>>>>>>>>>Label: -- from ada-label-indent
357 This is also used for <<..>> labels"
358 :type 'integer :group 'ada)
360 (defcustom ada-language-version 'ada95
361 "*Do we program in `ada83' or `ada95'?"
362 :type '(choice (const ada83) (const ada95)) :group 'ada)
364 (defcustom ada-move-to-declaration nil
365 "*Non-nil means `ada-move-to-start' moves point to the subprogram declaration,
367 :type 'boolean :group 'ada)
369 (defcustom ada-popup-key '[down-mouse-3]
370 "*Key used for binding the contextual menu.
371 If nil, no contextual menu is available."
372 :type '(restricted-sexp :match-alternatives (stringp vectorp))
375 (defcustom ada-search-directories
377 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
378 '("/usr/adainclude" "/usr/local/adainclude"
379 "/opt/gnu/adainclude"))
380 "*List of directories to search for Ada files.
381 See the description for the `ff-search-directories' variable. This variable
382 is the initial value of this variable, and is copied and modified in
383 `ada-search-directories-internal'."
384 :type '(repeat (choice :tag "Directory"
385 (const :tag "default" nil)
386 (directory :format "%v")))
389 (defvar ada-search-directories-internal ada-search-directories
390 "Internal version of `ada-search-directories'.
391 Its value is the concatenation of the search path as read in the project file
392 and the standard runtime location, and the value of the user-defined
393 `ada-search-directories'.")
395 (defcustom ada-stmt-end-indent 0
396 "*Number of columns to indent the end of a statement on a separate line.
400 >>>>>>>>>>>then -- from ada-stmt-end-indent"
401 :type 'integer :group 'ada)
403 (defcustom ada-tab-policy 'indent-auto
404 "*Control the behavior of the TAB key.
406 `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
407 `indent-auto' : use indentation functions in this file.
408 `always-tab' : do indent-relative."
409 :type '(choice (const indent-auto)
410 (const indent-rigidly)
414 (defcustom ada-use-indent ada-broken-indent
415 "*Indentation for the lines in a 'use' statement.
419 >>>>>Ada.Numerics; -- from ada-use-indent"
420 :type 'integer :group 'ada)
422 (defcustom ada-when-indent 3
423 "*Indentation for 'when' relative to 'exception' or 'case'.
427 >>>>>>>>when B => -- from ada-when-indent"
428 :type 'integer :group 'ada)
430 (defcustom ada-with-indent ada-broken-indent
431 "*Indentation for the lines in a 'with' statement.
435 >>>>>Ada.Numerics; -- from ada-with-indent"
436 :type 'integer :group 'ada)
438 (defcustom ada-which-compiler 'gnat
439 "*Name of the compiler to use.
440 This will determine what features are made available through the ada-mode.
441 The possible choices are :
442 `gnat': Use Ada Core Technologies' Gnat compiler. Add some cross-referencing
444 `generic': Use a generic compiler"
445 :type '(choice (const gnat)
450 ;;; ---- end of user configurable variables
453 (defvar ada-body-suffixes '(".adb")
454 "List of possible suffixes for Ada body files.
455 The extensions should include a `.' if needed.")
457 (defvar ada-spec-suffixes '(".ads")
458 "List of possible suffixes for Ada spec files.
459 The extensions should include a `.' if needed.")
461 (defvar ada-mode-menu (make-sparse-keymap "Ada")
462 "Menu for ada-mode.")
464 (defvar ada-mode-map (make-sparse-keymap)
465 "Local keymap used for Ada mode.")
467 (defvar ada-mode-abbrev-table nil
468 "Local abbrev table for Ada mode.")
470 (defvar ada-mode-syntax-table nil
471 "Syntax table to be used for editing Ada source code.")
473 (defvar ada-mode-symbol-syntax-table nil
474 "Syntax table for Ada, where `_' is a word constituent.")
477 (defconst ada-83-string-keywords
478 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
479 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
480 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
481 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
482 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
483 "procedure" "raise" "range" "record" "rem" "renames" "return"
484 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
485 "type" "use" "when" "while" "with" "xor")
486 "List of Ada keywords.
487 This variable is used to define `ada-83-keywords' and `ada-95-keywords'"))
489 (defvar ada-ret-binding nil
490 "Variable to save key binding of RET when casing is activated.")
492 (defvar ada-case-exception '()
493 "Alist of words (entities) that have special casing.")
495 (defvar ada-case-exception-substring '()
496 "Alist of substrings (entities) that have special casing.
497 The substrings are detected for word constituant when the word
498 is not itself in `ada-case-exception', and only for substrings that
499 either are at the beginning or end of the word, or start after '_'.")
501 (defvar ada-lfd-binding nil
502 "Variable to save key binding of LFD when casing is activated.")
504 (defvar ada-other-file-alist nil
505 "Variable used by `find-file' to find the name of the other package.
506 See `ff-other-file-alist'.")
508 (defvar ada-align-list
509 '(("[^:]\\(\\s-*\\):[^:]" 1 t)
510 ("[^=]\\(\\s-+\\)=[^=]" 1 t)
511 ("\\(\\s-*\\)use\\s-" 1)
513 "Ada support for align.el <= 2.2
514 This variable provides regular expressions on which to align different lines.
515 See `align-mode-alist' for more information.")
517 (defvar ada-align-modes
519 (regexp . "[^:]\\(\\s-*\\):[^:]")
520 (valid . (lambda() (not (ada-in-comment-p))))
521 (modes . '(ada-mode)))
523 (regexp . "[^=]\\(\\s-+\\)=[^=]")
524 (valid . (lambda() (not (ada-in-comment-p))))
525 (modes . '(ada-mode)))
527 (regexp . "\\(\\s-*\\)--")
528 (modes . '(ada-mode)))
530 (regexp . "\\(\\s-*\\)use\\s-")
531 (valid . (lambda() (not (ada-in-comment-p))))
532 (modes . '(ada-mode)))
534 "Ada support for align.el >= 2.8
535 This variable defines several rules to use to align different lines.")
537 (defconst ada-align-region-separate
556 "See the variable `align-region-separate' for more information.")
558 ;;; ---- Below are the regexp used in this package for parsing
560 (defconst ada-83-keywords
562 (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
563 "Regular expression for looking at Ada83 keywords.")
565 (defconst ada-95-keywords
567 (concat "\\<" (regexp-opt
569 '("abstract" "aliased" "protected" "requeue"
571 ada-83-string-keywords) t) "\\>"))
572 "Regular expression for looking at Ada95 keywords.")
574 (defvar ada-keywords ada-95-keywords
575 "Regular expression for looking at Ada keywords.")
577 (defconst ada-ident-re
579 "Regexp matching Ada (qualified) identifiers.")
581 ;; "with" needs to be included in the regexp, so that we can insert new lines
582 ;; after the declaration of the parameter for a generic.
583 (defvar ada-procedure-start-regexp
585 "^[ \t]*\\(with[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
587 ;; subprogram name: operator ("[+/=*]")
591 ;; subprogram name: name
593 "\\(\\(\\sw\\|[_.]\\)+\\)"
595 "Regexp used to find Ada procedures/functions.")
597 (defvar ada-package-start-regexp
598 "^[ \t]*\\(package\\)"
599 "Regexp used to find Ada packages.")
602 ;;; ---- regexps for indentation functions
604 (defvar ada-block-start-re
606 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
607 "exception" "generic" "loop" "or"
608 "private" "select" ))
609 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
610 "Regexp for keywords starting Ada blocks.")
612 (defvar ada-end-stmt-re
617 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
618 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
619 "loop" "private" "record" "select"
620 "then abort" "then") t) "\\>" "\\|"
621 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
622 t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
623 "^[ \t]*exception\\>"
625 "Regexp of possible ends for a non-broken statement.
626 A new statement starts after these.")
628 (defvar ada-matching-start-re
632 '("end" "loop" "select" "begin" "case" "do"
633 "if" "task" "package" "record" "protected") t)
635 "Regexp used in `ada-goto-matching-start'.")
637 (defvar ada-matching-decl-start-re
641 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
643 "Regexp used in `ada-goto-matching-decl-start'.")
645 (defvar ada-loop-start-re
646 "\\<\\(for\\|while\\|loop\\)\\>"
647 "Regexp for the start of a loop.")
649 (defvar ada-subprog-start-re
651 (concat "\\<" (regexp-opt '("accept" "entry" "function" "package" "procedure"
652 "protected" "task") t) "\\>"))
653 "Regexp for the start of a subprogram.")
655 (defvar ada-named-block-re
656 "[ \t]*\\(\\sw\\|_\\)+[ \t]*:[^=]"
657 "Regexp of the name of a block or loop.")
659 (defvar ada-contextual-menu-on-identifier nil
660 "Set to true when the right mouse button was clicked on an identifier.")
662 (defvar ada-contextual-menu-last-point nil
663 "Position of point just before displaying the menu.
664 This is a list (point buffer).
665 Since `ada-popup-menu' moves the point where the user clicked, the region
666 is modified. Therefore no command from the menu knows what the user selected
667 before displaying the contextual menu.
668 To get the original region, restore the point to this position before
669 calling `region-end' and `region-beginning'.
670 Modify this variable if you want to restore the point to another position.")
672 (easy-menu-define ada-contextual-menu nil
673 "Menu to use when the user presses the right mouse button.
674 The variable `ada-contextual-menu-on-identifier' will be set to t before
675 displaying the menu if point was on an identifier."
677 ["Goto Declaration/Body" ada-point-and-xref
678 :included ada-contextual-menu-on-identifier]
679 ["Goto Body" ada-point-and-xref-body
680 :included ada-contextual-menu-on-identifier]
681 ["Goto Previous Reference" ada-xref-goto-previous-reference]
682 ["List References" ada-find-references
683 :included ada-contextual-menu-on-identifier]
684 ["List Local References" ada-find-local-references
685 :included ada-contextual-menu-on-identifier]
687 ["Other File" ff-find-other-file]
688 ["Goto Parent Unit" ada-goto-parent]))
691 ;;------------------------------------------------------------------
692 ;; Support for imenu (see imenu.el)
693 ;;------------------------------------------------------------------
695 (defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
697 (defconst ada-imenu-subprogram-menu-re
698 (concat "^[ \t]*\\(procedure\\|function\\)[ \t\n]+"
699 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
702 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
704 (defvar ada-imenu-generic-expression
706 (list nil ada-imenu-subprogram-menu-re 2)
709 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
711 "\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
712 ada-imenu-comment-re "\\)";; parameter list or simple space
713 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
715 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
716 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
718 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
719 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
720 "Imenu generic expression for Ada mode.
721 See `imenu-generic-expression'. This variable will create several submenus for
722 each type of entity that can be found in an Ada file.")
725 ;;------------------------------------------------------------
726 ;; Support for compile.el
727 ;;------------------------------------------------------------
729 (defun ada-compile-mouse-goto-error ()
730 "Mouse interface for ada-compile-goto-error."
732 (mouse-set-point last-input-event)
733 (ada-compile-goto-error (point))
736 (defun ada-compile-goto-error (pos)
737 "Replace `compile-goto-error' from compile.el.
738 If POS is on a file and line location, go to this position. It adds
739 to compile.el the capacity to go to a reference in an error message.
740 For instance, on this line:
741 foo.adb:61:11: [...] in call to size declared at foo.ads:11
742 both file locations can be clicked on and jumped to."
746 (skip-chars-backward "-a-zA-Z0-9_:./\\")
748 ;; special case: looking at a filename:line not at the beginning of a line
751 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"))
752 (let ((line (match-string 2))
754 (error-pos (point-marker))
759 ;; Use funcall so as to prevent byte-compiler warnings
760 ;; `ada-find-file' is not defined if ada-xref wasn't loaded. But
761 ;; if we can find it, we should use it instead of
762 ;; `compilation-find-file', since the latter doesn't know anything
763 ;; about source path.
765 (if (functionp 'ada-find-file)
766 (setq file (funcall (symbol-function 'ada-find-file)
768 (setq file (funcall (symbol-function 'compilation-find-file)
769 (point-marker) (match-string 1)
774 (goto-line (string-to-number line)))
775 (setq source (point-marker))))
776 (funcall (symbol-function 'compilation-goto-locus)
777 (cons source error-pos))
780 ;; otherwise, default behavior
782 (funcall (symbol-function 'compile-goto-error)))
787 ;;-------------------------------------------------------------------------
788 ;; Grammar related function
789 ;; The functions below work with the syntax class of the characters in an Ada
790 ;; buffer. Two syntax tables are created, depending on whether we want '_'
791 ;; to be considered as part of a word or not.
792 ;; Some characters may have multiple meanings depending on the context:
793 ;; - ' is either the beginning of a constant character or an attribute
794 ;; - # is either part of a based litteral or a gnatprep statement.
795 ;; - " starts a string, but not if inside a constant character.
796 ;; - ( and ) should be ignored if inside a constant character.
797 ;; Thus their syntax property is changed automatically, and we can still use
798 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
800 ;; On Emacs, this is done through the `syntax-table' text property. The
801 ;; modification is done automatically each time the user as typed a new
802 ;; character. This is already done in `font-lock-mode' (in
803 ;; `font-lock-syntactic-keywords', so we take advantage of the existing
804 ;; mechanism. If font-lock-mode is not activated, we do it by hand in
805 ;; `ada-after-change-function', thanks to `ada-deactivate-properties' and
806 ;; `ada-initialize-properties'.
808 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
809 ;; slow advice to `parse-partial-sexp' to do the same thing.
810 ;; When executing parse-partial-sexp, we simply modify the strings before and
811 ;; after, so that the special constants '"', '(' and ')' do not interact
812 ;; with parse-partial-sexp.
813 ;; Note: this code is slow and needs to be rewritten as soon as something
814 ;; better is available on XEmacs.
815 ;;-------------------------------------------------------------------------
817 (defun ada-create-syntax-table ()
818 "Create the two syntax tables use in the Ada mode.
819 The standard table declares `_' as a symbol constituent, the second one
820 declares it as a word constituent."
822 (setq ada-mode-syntax-table (make-syntax-table))
823 (set-syntax-table ada-mode-syntax-table)
825 ;; define string brackets (`%' is alternative string bracket, but
826 ;; almost never used as such and throws font-lock and indentation
828 (modify-syntax-entry ?% "$" ada-mode-syntax-table)
829 (modify-syntax-entry ?\" "\"" ada-mode-syntax-table)
831 (modify-syntax-entry ?: "." ada-mode-syntax-table)
832 (modify-syntax-entry ?\; "." ada-mode-syntax-table)
833 (modify-syntax-entry ?& "." ada-mode-syntax-table)
834 (modify-syntax-entry ?\| "." ada-mode-syntax-table)
835 (modify-syntax-entry ?+ "." ada-mode-syntax-table)
836 (modify-syntax-entry ?* "." ada-mode-syntax-table)
837 (modify-syntax-entry ?/ "." ada-mode-syntax-table)
838 (modify-syntax-entry ?= "." ada-mode-syntax-table)
839 (modify-syntax-entry ?< "." ada-mode-syntax-table)
840 (modify-syntax-entry ?> "." ada-mode-syntax-table)
841 (modify-syntax-entry ?$ "." ada-mode-syntax-table)
842 (modify-syntax-entry ?\[ "." ada-mode-syntax-table)
843 (modify-syntax-entry ?\] "." ada-mode-syntax-table)
844 (modify-syntax-entry ?\{ "." ada-mode-syntax-table)
845 (modify-syntax-entry ?\} "." ada-mode-syntax-table)
846 (modify-syntax-entry ?. "." ada-mode-syntax-table)
847 (modify-syntax-entry ?\\ "." ada-mode-syntax-table)
848 (modify-syntax-entry ?\' "." ada-mode-syntax-table)
850 ;; a single hyphen is punctuation, but a double hyphen starts a comment
851 (modify-syntax-entry ?- ". 12" ada-mode-syntax-table)
853 ;; See the comment above on grammar related function for the special
855 (if (featurep 'xemacs)
856 (modify-syntax-entry ?# "<" ada-mode-syntax-table)
857 (modify-syntax-entry ?# "$" ada-mode-syntax-table))
859 ;; and \f and \n end a comment
860 (modify-syntax-entry ?\f "> " ada-mode-syntax-table)
861 (modify-syntax-entry ?\n "> " ada-mode-syntax-table)
863 ;; define what belongs in Ada symbols
864 (modify-syntax-entry ?_ "_" ada-mode-syntax-table)
866 ;; define parentheses to match
867 (modify-syntax-entry ?\( "()" ada-mode-syntax-table)
868 (modify-syntax-entry ?\) ")(" ada-mode-syntax-table)
870 (setq ada-mode-symbol-syntax-table (copy-syntax-table ada-mode-syntax-table))
871 (modify-syntax-entry ?_ "w" ada-mode-symbol-syntax-table)
874 ;; Support of special characters in XEmacs (see the comments at the beginning
875 ;; of the section on Grammar related functions).
877 (if (featurep 'xemacs)
878 (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
879 "Handles special character constants and gnatprep statements."
883 (setq from to to tmp)))
886 (while (re-search-forward "'\\([(\")#]\\)'" to t)
887 (setq change (cons (list (match-beginning 1)
891 (replace-match "'A'"))
893 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
894 (setq change (cons (list (match-beginning 1)
895 (length (match-string 1))
898 (replace-match (make-string (length (match-string 1)) ?@))))
902 (goto-char (caar change))
903 (delete-char (cadar change))
904 (insert (caddar change))
905 (setq change (cdr change)))))))
907 (defun ada-deactivate-properties ()
908 "Deactivate ada-mode's properties handling.
909 This would be a duplicate of font-lock if both are used at the same time."
910 (remove-hook 'after-change-functions 'ada-after-change-function t))
912 (defun ada-initialize-properties ()
913 "Initialize some special text properties in the whole buffer.
914 In particular, character constants are said to be strings, #...# are treated
915 as numbers instead of gnatprep comments."
919 (goto-char (point-min))
920 (while (re-search-forward "'.'" nil t)
921 (add-text-properties (match-beginning 0) (match-end 0)
922 '(syntax-table ("'" . ?\"))))
923 (goto-char (point-min))
924 (while (re-search-forward "^[ \t]*#" nil t)
925 (add-text-properties (match-beginning 0) (match-end 0)
926 '(syntax-table (11 . 10))))
927 (set-buffer-modified-p nil)
929 ;; Setting this only if font-lock is not set won't work
930 ;; if the user activates or deactivates font-lock-mode,
931 ;; but will make things faster most of the time
932 (add-hook 'after-change-functions 'ada-after-change-function nil t)
935 (defun ada-after-change-function (beg end old-len)
936 "Called when the region between BEG and END was changed in the buffer.
937 OLD-LEN indicates what the length of the replaced text was."
938 (let ((inhibit-point-motion-hooks t)
943 (remove-text-properties (point) eol '(syntax-table nil))
944 (while (re-search-forward "'.'" eol t)
945 (add-text-properties (match-beginning 0) (match-end 0)
946 '(syntax-table ("'" . ?\"))))
948 (if (looking-at "^[ \t]*#")
949 (add-text-properties (match-beginning 0) (match-end 0)
950 '(syntax-table (11 . 10))))))))
952 ;;------------------------------------------------------------------
953 ;; Testing the grammatical context
954 ;;------------------------------------------------------------------
956 (defsubst ada-in-comment-p (&optional parse-result)
957 "Return t if inside a comment."
958 (nth 4 (or parse-result
960 (line-beginning-position) (point)))))
962 (defsubst ada-in-string-p (&optional parse-result)
963 "Return t if point is inside a string.
964 If parse-result is non-nil, use is instead of calling `parse-partial-sexp'."
965 (nth 3 (or parse-result
967 (line-beginning-position) (point)))))
969 (defsubst ada-in-string-or-comment-p (&optional parse-result)
970 "Return t if inside a comment or string."
971 (setq parse-result (or parse-result
973 (line-beginning-position) (point))))
974 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
977 ;;------------------------------------------------------------------
979 ;; The Ada-mode comes with contextual menus, bound by default to the right
981 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
982 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
983 ;; if the mouse button was pressed on an identifier.
984 ;;------------------------------------------------------------------
986 (defun ada-call-from-contextual-menu (function)
987 "Execute FUNCTION when called from the contextual menu.
988 It forces Emacs to change the cursor position."
991 (setq ada-contextual-menu-last-point
992 (list (point) (current-buffer))))
994 (defun ada-popup-menu (position)
995 "Pops up a contextual menu, depending on where the user clicked.
996 POSITION is the location the mouse was clicked on.
997 Sets `ada-contextual-menu-last-point' to the current position before
998 displaying the menu. When a function from the menu is called, the
999 point is where the mouse button was clicked."
1002 ;; declare this as a local variable, so that the function called
1003 ;; in the contextual menu does not hide the region in
1004 ;; transient-mark-mode.
1005 (let ((deactivate-mark nil))
1006 (setq ada-contextual-menu-last-point
1007 (list (point) (current-buffer)))
1008 (mouse-set-point last-input-event)
1010 (setq ada-contextual-menu-on-identifier
1012 (or (= (char-syntax (char-after)) ?w)
1013 (= (char-after) ?_))
1014 (not (ada-in-string-or-comment-p))
1015 (save-excursion (skip-syntax-forward "w")
1016 (not (ada-after-keyword-p)))
1018 (if (fboundp 'popup-menu)
1019 (funcall (symbol-function 'popup-menu) ada-contextual-menu)
1021 (setq choice (x-popup-menu position ada-contextual-menu))
1023 (funcall (lookup-key ada-contextual-menu (vector (car choice)))))))
1025 (set-buffer (cadr ada-contextual-menu-last-point))
1026 (goto-char (car ada-contextual-menu-last-point))
1030 ;;------------------------------------------------------------------
1032 ;;------------------------------------------------------------------
1035 (defun ada-add-extensions (spec body)
1036 "Define SPEC and BODY as being valid extensions for Ada files.
1037 Going from body to spec with `ff-find-other-file' used these
1039 SPEC and BODY are two regular expressions that must match against
1041 (let* ((reg (concat (regexp-quote body) "$"))
1042 (tmp (assoc reg ada-other-file-alist)))
1044 (setcdr tmp (list (cons spec (cadr tmp))))
1045 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
1047 (let* ((reg (concat (regexp-quote spec) "$"))
1048 (tmp (assoc reg ada-other-file-alist)))
1050 (setcdr tmp (list (cons body (cadr tmp))))
1051 (add-to-list 'ada-other-file-alist (list reg (list body)))))
1053 (add-to-list 'auto-mode-alist
1054 (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
1055 (add-to-list 'auto-mode-alist
1056 (cons (concat (regexp-quote body) "\\'") 'ada-mode))
1058 (add-to-list 'ada-spec-suffixes spec)
1059 (add-to-list 'ada-body-suffixes body)
1061 ;; Support for speedbar (Specifies that we want to see these files in
1063 (if (fboundp 'speedbar-add-supported-extension)
1065 (funcall (symbol-function 'speedbar-add-supported-extension)
1067 (funcall (symbol-function 'speedbar-add-supported-extension)
1074 "Ada mode is the major mode for editing Ada code.
1076 Bindings are as follows: (Note: 'LFD' is control-j.)
1079 Indent line '\\[ada-tab]'
1080 Indent line, insert newline and indent the new line. '\\[newline-and-indent]'
1082 Re-format the parameter-list point is in '\\[ada-format-paramlist]'
1083 Indent all lines in region '\\[ada-indent-region]'
1085 Adjust case of identifiers and keywords in region '\\[ada-adjust-case-region]'
1086 Adjust case of identifiers and keywords in buffer '\\[ada-adjust-case-buffer]'
1088 Fill comment paragraph, justify and append postfix '\\[fill-paragraph]'
1090 Next func/proc/task '\\[ada-next-procedure]' Previous func/proc/task '\\[ada-previous-procedure]'
1091 Next package '\\[ada-next-package]' Previous package '\\[ada-previous-package]'
1093 Goto matching start of current 'end ...;' '\\[ada-move-to-start]'
1094 Goto end of current block '\\[ada-move-to-end]'
1096 Comments are handled using standard GNU Emacs conventions, including:
1097 Start a comment '\\[indent-for-comment]'
1098 Comment region '\\[comment-region]'
1099 Uncomment region '\\[ada-uncomment-region]'
1100 Continue comment on next line '\\[indent-new-comment-line]'
1102 If you use imenu.el:
1103 Display index-menu of functions and procedures '\\[imenu]'
1105 If you use find-file.el:
1106 Switch to other file (Body <-> Spec) '\\[ff-find-other-file]'
1107 or '\\[ff-mouse-find-other-file]
1108 Switch to other file in other window '\\[ada-ff-other-window]'
1109 or '\\[ff-mouse-find-other-file-other-window]
1110 If you use this function in a spec and no body is available, it gets created with body stubs.
1112 If you use ada-xref.el:
1113 Goto declaration: '\\[ada-point-and-xref]' on the identifier
1114 or '\\[ada-goto-declaration]' with point on the identifier
1115 Complete identifier: '\\[ada-complete-identifier]'."
1118 (kill-all-local-variables)
1120 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
1122 ;; Set the paragraph delimiters so that one can select a whole block
1124 (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
1125 (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
1127 ;; comment end must be set because it may hold a wrong value if
1128 ;; this buffer had been in another mode before. RE
1129 (set (make-local-variable 'comment-end) "")
1131 ;; used by autofill and indent-new-comment-line
1132 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
1134 ;; used by autofill to break a comment line and continue it on another line.
1135 ;; The reason we need this one is that the default behavior does not work
1136 ;; correctly with the definition of paragraph-start above when the comment
1137 ;; is right after a multi-line subprogram declaration (the comments are
1138 ;; aligned under the latest parameter, not under the declaration start).
1139 (set (make-local-variable 'comment-line-break-function)
1140 (lambda (&optional soft) (let ((fill-prefix nil))
1141 (indent-new-comment-line soft))))
1143 (set (make-local-variable 'indent-line-function)
1144 'ada-indent-current-function)
1146 (set (make-local-variable 'comment-column) 40)
1148 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1149 ;; the comment and the text. We do not want any, this is already
1150 ;; included in comment-start
1151 (unless (featurep 'xemacs)
1153 (if (ada-check-emacs-version 20 3)
1155 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1156 (set (make-local-variable 'comment-padding) 0)))
1157 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1160 (set 'case-fold-search t)
1161 (if (boundp 'imenu-case-fold-search)
1162 (set 'imenu-case-fold-search t))
1164 (set (make-local-variable 'fill-paragraph-function)
1165 'ada-fill-comment-paragraph)
1167 (set (make-local-variable 'imenu-generic-expression)
1168 ada-imenu-generic-expression)
1170 ;; Support for compile.el
1171 ;; We just substitute our own functions to go to the error.
1172 (add-hook 'compilation-mode-hook
1174 (set (make-local-variable 'compile-auto-highlight) 40)
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 :
1184 ;; We need to set some properties for XEmacs, and define some variables
1187 (if (featurep 'xemacs)
1189 (put 'ada-mode 'font-lock-defaults
1190 '(ada-font-lock-keywords
1191 nil t ((?\_ . "w") (?# . ".")) beginning-of-line))
1193 (set (make-local-variable 'font-lock-defaults)
1194 '(ada-font-lock-keywords
1196 ((?\_ . "w") (?# . "."))
1198 (font-lock-syntactic-keywords . ada-font-lock-syntactic-keywords)))
1201 ;; Set up support for find-file.el.
1202 (set (make-local-variable 'ff-other-file-alist)
1203 'ada-other-file-alist)
1204 (set (make-local-variable 'ff-search-directories)
1205 'ada-search-directories-internal)
1206 (setq ff-post-load-hook 'ada-set-point-accordingly
1207 ff-file-created-hook 'ada-make-body)
1208 (add-hook 'ff-pre-load-hook 'ada-which-function-are-we-in)
1210 ;; Some special constructs for find-file.el
1211 ;; We do not need to add the construction for 'with', which is in the
1212 ;; standard find-file.el
1213 (make-local-variable 'ff-special-constructs)
1215 ;; Go to the parent package :
1216 (add-to-list 'ff-special-constructs
1217 (cons (eval-when-compile
1218 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1220 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1222 (if (fboundp 'ff-get-file)
1224 (set 'fname (ff-get-file
1225 ada-search-directories-internal
1226 (ada-make-filename-from-adaname
1228 ada-spec-suffixes)))))))
1229 ;; Another special construct for find-file.el : when in a separate clause,
1230 ;; go to the correct package.
1231 (add-to-list 'ff-special-constructs
1232 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1234 (if (fboundp 'ff-get-file)
1236 (setq fname (ff-get-file
1237 ada-search-directories-internal
1238 (ada-make-filename-from-adaname
1240 ada-spec-suffixes)))))))
1242 ;; Another special construct, that redefines the one in find-file.el. The
1243 ;; old one can handle only one possible type of extension for Ada files
1244 ;; remove from the list the standard "with..." that is put by find-file.el,
1245 ;; since it uses the old ada-spec-suffix variable
1246 ;; This one needs to replace the standard one defined in find-file.el (with
1247 ;; Emacs <= 20.4), since that one uses the old variable ada-spec-suffix
1248 (let ((old-construct
1249 (assoc "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" ff-special-constructs))
1252 (if (fboundp 'ff-get-file)
1254 (set 'fname (ff-get-file
1255 ada-search-directories-internal
1256 (ada-make-filename-from-adaname
1258 ada-spec-suffixes)))))))
1260 (setcdr old-construct new-cdr)
1261 (add-to-list 'ff-special-constructs
1262 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1265 ;; Support for outline-minor-mode
1266 (set (make-local-variable 'outline-regexp)
1267 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1268 (set (make-local-variable 'outline-level) 'ada-outline-level)
1270 ;; Support for imenu : We want a sorted index
1271 (setq imenu-sort-function 'imenu--sort-by-name)
1273 ;; Support for ispell : Check only comments
1274 (set (make-local-variable 'ispell-check-comments) 'exclusive)
1276 ;; Support for align.el <= 2.2, if present
1277 ;; align.el is distributed with Emacs 21, but not with earlier versions.
1278 (if (boundp 'align-mode-alist)
1279 (add-to-list 'align-mode-alist '(ada-mode . ada-align-list)))
1281 ;; Support for align.el >= 2.8, if present
1282 (if (boundp 'align-dq-string-modes)
1284 (add-to-list 'align-dq-string-modes 'ada-mode)
1285 (add-to-list 'align-open-comment-modes 'ada-mode)
1286 (set (make-local-variable 'align-region-separate)
1287 ada-align-region-separate)
1289 ;; Exclude comments alone on line from alignment.
1290 (add-to-list 'align-exclude-rules-list
1292 (regexp . "^\\(\\s-*\\)--")
1293 (modes . '(ada-mode))))
1294 (add-to-list 'align-exclude-rules-list
1296 (regexp . "^\\(\\s-*\\)\\<use\\>")
1297 (modes . '(ada-mode))))
1299 (setq ada-align-modes nil)
1301 (add-to-list 'ada-align-modes
1302 '(ada-declaration-assign
1303 (regexp . "[^:]\\(\\s-*\\):[^:]")
1304 (valid . (lambda() (not (ada-in-comment-p))))
1306 (modes . '(ada-mode))))
1307 (add-to-list 'ada-align-modes
1309 (regexp . "[^=]\\(\\s-*\\)=>")
1310 (valid . (lambda() (not (ada-in-comment-p))))
1311 (modes . '(ada-mode))))
1312 (add-to-list 'ada-align-modes
1314 (regexp . "\\(\\s-*\\)--")
1315 (modes . '(ada-mode))))
1316 (add-to-list 'ada-align-modes
1318 (regexp . "\\(\\s-*\\)\\<use\\s-")
1319 (valid . (lambda() (not (ada-in-comment-p))))
1320 (modes . '(ada-mode))))
1321 (add-to-list 'ada-align-modes
1323 (regexp . "\\(\\s-+\\)at\\>")
1324 (modes . '(ada-mode))))
1327 (setq align-mode-rules-list ada-align-modes)
1330 ;; Set up the contextual menu
1332 (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
1334 ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
1335 (define-abbrev-table 'ada-mode-abbrev-table ())
1336 (setq local-abbrev-table ada-mode-abbrev-table)
1338 ;; Support for which-function mode
1339 ;; which-function-mode does not work with nested subprograms, since it is
1340 ;; based only on the regexps generated by imenu, and thus can only detect the
1341 ;; beginning of subprograms, not the end.
1342 ;; Fix is: redefine a new function ada-which-function, and call it when the
1343 ;; major-mode is ada-mode.
1345 (make-local-variable 'which-func-functions)
1346 (setq which-func-functions '(ada-which-function))
1348 ;; Support for indent-new-comment-line (Especially for XEmacs)
1349 (setq comment-multi-line nil)
1351 (setq major-mode 'ada-mode
1354 (use-local-map ada-mode-map)
1356 (easy-menu-add ada-mode-menu ada-mode-map)
1358 (set-syntax-table ada-mode-syntax-table)
1360 (if ada-clean-buffer-before-saving
1362 ;; remove all spaces at the end of lines in the whole buffer.
1363 (add-hook 'local-write-file-hooks 'delete-trailing-whitespace)
1364 ;; convert all tabs to the correct number of spaces.
1365 (add-hook 'local-write-file-hooks
1366 (lambda () (untabify (point-min) (point-max))))))
1368 (set (make-local-variable 'skeleton-further-elements)
1369 '((< '(backward-delete-char-untabify
1370 (min ada-indent (current-column))))))
1371 (add-hook 'skeleton-end-hook 'ada-adjust-case-skeleton nil t)
1373 (run-mode-hooks 'ada-mode-hook)
1375 ;; To be run after the hook, in case the user modified
1376 ;; ada-fill-comment-prefix
1377 (make-local-variable 'comment-start)
1378 (if ada-fill-comment-prefix
1379 (set 'comment-start ada-fill-comment-prefix)
1380 (set 'comment-start "-- "))
1382 ;; Run this after the hook to give the users a chance to activate
1385 (unless (featurep 'xemacs)
1387 (ada-initialize-properties)
1388 (add-hook 'font-lock-mode-hook 'ada-deactivate-properties nil t)))
1390 ;; the following has to be done after running the ada-mode-hook
1391 ;; because users might want to set the values of these variable
1392 ;; inside the hook (MH)
1394 (cond ((eq ada-language-version 'ada83)
1395 (setq ada-keywords ada-83-keywords))
1396 ((eq ada-language-version 'ada95)
1397 (setq ada-keywords ada-95-keywords)))
1400 (ada-activate-keys-for-case)))
1402 (defun ada-adjust-case-skeleton ()
1403 "Adjust the case of the text inserted by a skeleton."
1405 (let ((aa-end (point)))
1406 (ada-adjust-case-region
1407 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
1408 (goto-char aa-end)))))
1410 ;; transient-mark-mode and mark-active are not defined in XEmacs
1411 (defun ada-region-selected ()
1412 "Return t if a region has been selected by the user and is still active."
1413 (or (and (featurep 'xemacs) (funcall (symbol-function 'region-active-p)))
1414 (and (not (featurep 'xemacs))
1415 (symbol-value 'transient-mark-mode)
1416 (symbol-value 'mark-active))))
1419 ;;-----------------------------------------------------------------
1421 ;; Since Ada is case-insensitive, the Ada-mode provides an extensive set of
1422 ;; functions to auto-case identifiers, keywords, ...
1423 ;; The basic rules for autocasing are defined through the variables
1424 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1425 ;; are references to the functions that will do the actual casing.
1427 ;; However, in most cases, the user will want to define some exceptions to
1428 ;; these casing rules. This is done through a list of files, that contain
1429 ;; one word per line. These files are stored in `ada-case-exception-file'.
1430 ;; For backward compatibility, this variable can also be a string.
1431 ;;-----------------------------------------------------------------
1433 (defun ada-save-exceptions-to-file (file-name)
1434 "Save the exception lists `ada-case-exception' and
1435 `ada-case-exception-substring' to the file FILE-NAME."
1437 ;; Save the list in the file
1438 (find-file (expand-file-name file-name))
1440 (mapcar (lambda (x) (insert (car x) "\n"))
1441 (sort (copy-sequence ada-case-exception)
1442 (lambda(a b) (string< (car a) (car b)))))
1443 (mapcar (lambda (x) (insert "*" (car x) "\n"))
1444 (sort (copy-sequence ada-case-exception-substring)
1445 (lambda(a b) (string< (car a) (car b)))))
1450 (defun ada-create-case-exception (&optional word)
1451 "Define WORD as an exception for the casing system.
1452 If WORD is not given, then the current word in the buffer is used instead.
1453 The new words is added to the first file in `ada-case-exception-file'.
1454 The standard casing rules will no longer apply to this word."
1456 (let ((previous-syntax-table (syntax-table))
1460 (cond ((stringp ada-case-exception-file)
1461 (setq file-name ada-case-exception-file))
1462 ((listp ada-case-exception-file)
1463 (setq file-name (car ada-case-exception-file)))
1465 (error (concat "No exception file specified. "
1466 "See variable ada-case-exception-file"))))
1468 (set-syntax-table ada-mode-symbol-syntax-table)
1471 (skip-syntax-backward "w")
1472 (setq word (buffer-substring-no-properties
1473 (point) (save-excursion (forward-word 1) (point))))))
1474 (set-syntax-table previous-syntax-table)
1476 ;; Reread the exceptions file, in case it was modified by some other,
1477 (ada-case-read-exceptions-from-file file-name)
1479 ;; If the word is already in the list, even with a different casing
1480 ;; we simply want to replace it.
1481 (if (and (not (equal ada-case-exception '()))
1482 (assoc-string word ada-case-exception t))
1483 (setcar (assoc-string word ada-case-exception t) word)
1484 (add-to-list 'ada-case-exception (cons word t))
1487 (ada-save-exceptions-to-file file-name)
1490 (defun ada-create-case-exception-substring (&optional word)
1491 "Define the substring WORD as an exception for the casing system.
1492 If WORD is not given, then the current word in the buffer is used instead,
1493 or the selected region if any is active.
1494 The new word is added to the first file in `ada-case-exception-file'.
1495 When auto-casing a word, this substring will be special-cased, unless the
1496 word itself has a special casing."
1499 (cond ((stringp ada-case-exception-file)
1500 ada-case-exception-file)
1501 ((listp ada-case-exception-file)
1502 (car ada-case-exception-file))
1504 (error (concat "No exception file specified. "
1505 "See variable ada-case-exception-file"))))))
1507 ;; Find the substring to define as an exception. Order is: the parameter,
1508 ;; if any, or the selected region, or the word under the cursor
1512 ((ada-region-selected)
1513 (setq word (buffer-substring-no-properties
1514 (region-beginning) (region-end))))
1517 (let ((underscore-syntax (char-syntax ?_)))
1520 (modify-syntax-entry ?_ "." (syntax-table))
1522 (skip-syntax-backward "w")
1523 (set 'word (buffer-substring-no-properties
1525 (save-excursion (forward-word 1) (point))))))
1526 (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
1529 ;; Reread the exceptions file, in case it was modified by some other,
1530 (ada-case-read-exceptions-from-file file-name)
1532 ;; If the word is already in the list, even with a different casing
1533 ;; we simply want to replace it.
1534 (if (and (not (equal ada-case-exception-substring '()))
1535 (assoc-string word ada-case-exception-substring t))
1536 (setcar (assoc-string word ada-case-exception-substring t) word)
1537 (add-to-list 'ada-case-exception-substring (cons word t))
1540 (ada-save-exceptions-to-file file-name)
1542 (message (concat "Defining " word " as a casing exception"))))
1544 (defun ada-case-read-exceptions-from-file (file-name)
1545 "Read the content of the casing exception file FILE-NAME."
1546 (if (file-readable-p (expand-file-name file-name))
1547 (let ((buffer (current-buffer)))
1548 (find-file (expand-file-name file-name))
1549 (set-syntax-table ada-mode-symbol-syntax-table)
1551 (goto-char (point-min))
1554 ;; If the item is already in the list, even with an other casing,
1555 ;; do not add it again. This way, the user can easily decide which
1556 ;; priority should be applied to each casing exception
1557 (let ((word (buffer-substring-no-properties
1558 (point) (save-excursion (forward-word 1) (point)))))
1560 ;; Handling a substring ?
1561 (if (char-equal (string-to-char word) ?*)
1563 (setq word (substring word 1))
1564 (unless (assoc-string word ada-case-exception-substring t)
1565 (add-to-list 'ada-case-exception-substring (cons word t))))
1566 (unless (assoc-string word ada-case-exception t)
1567 (add-to-list 'ada-case-exception (cons word t)))))
1571 (set-buffer buffer)))
1574 (defun ada-case-read-exceptions ()
1575 "Read all the casing exception files from `ada-case-exception-file'."
1578 ;; Reinitialize the casing exception list
1579 (setq ada-case-exception '()
1580 ada-case-exception-substring '())
1582 (cond ((stringp ada-case-exception-file)
1583 (ada-case-read-exceptions-from-file ada-case-exception-file))
1585 ((listp ada-case-exception-file)
1586 (mapcar 'ada-case-read-exceptions-from-file
1587 ada-case-exception-file))))
1589 (defun ada-adjust-case-substring ()
1590 "Adjust case of substrings in the previous word."
1592 (let ((substrings ada-case-exception-substring)
1594 (case-fold-search t)
1595 (underscore-syntax (char-syntax ?_))
1603 (modify-syntax-entry ?_ "." (syntax-table))
1606 (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
1609 (while (re-search-forward re max t)
1610 (replace-match (caar substrings) t)))
1611 (setq substrings (cdr substrings))
1614 (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
1617 (defun ada-adjust-case-identifier ()
1618 "Adjust case of the previous identifier.
1619 The auto-casing is done according to the value of `ada-case-identifier' and
1620 the exceptions defined in `ada-case-exception-file'."
1622 (if (or (equal ada-case-exception '())
1623 (equal (char-after) ?_))
1625 (funcall ada-case-identifier -1)
1626 (ada-adjust-case-substring))
1630 (start (save-excursion (skip-syntax-backward "w")
1633 ;; If we have an exception, replace the word by the correct casing
1634 (if (setq match (assoc-string (buffer-substring start end)
1635 ada-case-exception t))
1638 (delete-region start end)
1639 (insert (car match)))
1641 ;; Else simply re-case the word
1642 (funcall ada-case-identifier -1)
1643 (ada-adjust-case-substring))))))
1645 (defun ada-after-keyword-p ()
1646 "Return t if cursor is after a keyword that is not an attribute."
1649 (and (not (and (char-before)
1650 (or (= (char-before) ?_)
1651 (= (char-before) ?'))));; unless we have a _ or '
1652 (looking-at (concat ada-keywords "[^_]")))))
1654 (defun ada-adjust-case (&optional force-identifier)
1655 "Adjust the case of the word before the character just typed.
1656 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1660 (if (and (not (bobp))
1661 ;; or if at the end of a character constant
1662 (not (and (eq (following-char) ?')
1663 (eq (char-before (1- (point))) ?')))
1664 ;; or if the previous character was not part of a word
1665 (eq (char-syntax (char-before)) ?w)
1666 ;; if in a string or a comment
1667 (not (ada-in-string-or-comment-p))
1671 (or (= (point) (point-min))
1673 (= (following-char) ?'))
1674 (funcall ada-case-attribute -1)
1676 (not force-identifier) ; (MH)
1677 (ada-after-keyword-p))
1678 (funcall ada-case-keyword -1)
1679 (ada-adjust-case-identifier))))
1684 (defun ada-adjust-case-interactive (arg)
1685 "Adjust the case of the previous word, and process the character just typed.
1686 ARG is the prefix the user entered with \\[universal-argument]."
1690 (let ((lastk last-command-char)
1691 (previous-syntax-table (syntax-table)))
1695 (set-syntax-table ada-mode-symbol-syntax-table)
1696 (cond ((or (eq lastk ?\n)
1701 ;; horrible dekludge
1702 (delete-backward-char 1)
1703 ;; some special keys and their bindings
1706 (funcall ada-lfd-binding))
1708 (funcall ada-ret-binding))))
1709 ((eq lastk ?\C-i) (ada-tab))
1710 ;; Else just insert the character
1711 ((self-insert-command (prefix-numeric-value arg))))
1712 ;; if there is a keyword in front of the underscore
1713 ;; then it should be part of an identifier (MH)
1718 ;; Restore the syntax table
1719 (set-syntax-table previous-syntax-table))
1722 ;; Else, no auto-casing
1724 ((eq last-command-char ?\n)
1725 (funcall ada-lfd-binding))
1726 ((eq last-command-char ?\r)
1727 (funcall ada-ret-binding))
1729 (self-insert-command (prefix-numeric-value arg))))
1732 (defun ada-activate-keys-for-case ()
1733 "Modify the key bindings for all the keys that should readjust the casing."
1735 ;; Save original key-bindings to allow swapping ret/lfd
1736 ;; when casing is activated.
1737 ;; The 'or ...' is there to be sure that the value will not
1738 ;; be changed again when Ada mode is called more than once
1739 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
1740 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
1742 ;; Call case modifying function after certain keys.
1743 (mapcar (function (lambda(key) (define-key
1745 (char-to-string key)
1746 'ada-adjust-case-interactive)))
1747 '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
1748 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1750 (defun ada-loose-case-word (&optional arg)
1751 "Upcase first letter and letters following `_' in the following word.
1752 No other letter is modified.
1753 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1756 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1758 (skip-syntax-backward "w")
1759 (while (and (or first (search-forward "_" end t))
1763 (insert-char (upcase (following-char)) 1)
1766 (defun ada-no-auto-case (&optional arg)
1768 This function can be used for the auto-casing variables in the ada-mode, to
1769 adapt to unusal auto-casing schemes. Since it does nothing, you can for
1770 instance use it for `ada-case-identifier' if you don't want any special
1771 auto-casing for identifiers, whereas keywords have to be lower-cased.
1772 See also `ada-auto-case' to disable auto casing altogether."
1775 (defun ada-capitalize-word (&optional arg)
1776 "Upcase first letter and letters following '_', lower case other letters.
1777 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1779 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1780 (begin (save-excursion (skip-syntax-backward "w") (point))))
1781 (modify-syntax-entry ?_ "_")
1782 (capitalize-region begin end)
1783 (modify-syntax-entry ?_ "w")))
1785 (defun ada-adjust-case-region (from to)
1786 "Adjust the case of all words in the region between FROM and TO.
1787 Attention: This function might take very long for big regions!"
1793 (previous-syntax-table (syntax-table)))
1794 (message "Adjusting case ...")
1797 (set-syntax-table ada-mode-symbol-syntax-table)
1800 ;; loop: look for all identifiers, keywords, and attributes
1802 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1803 (setq end (match-end 1))
1805 (and (> (point) from)
1808 (setq attribp (looking-at "'.[^']")))))
1810 ;; do nothing if it is a string or comment
1811 (ada-in-string-or-comment-p)
1814 ;; get the identifier or keyword or attribute
1816 (setq begin (point))
1817 (setq keywordp (looking-at ada-keywords))
1820 ;; casing according to user-option
1823 (funcall ada-case-attribute -1)
1825 (funcall ada-case-keyword -1)
1826 (ada-adjust-case-identifier)))
1827 (goto-char begin))))
1828 (message "Adjusting case ... Done"))
1829 (set-syntax-table previous-syntax-table))))
1831 (defun ada-adjust-case-buffer ()
1832 "Adjusts the case of all words in the whole buffer.
1833 ATTENTION: This function might take very long for big buffers!"
1835 (ada-adjust-case-region (point-min) (point-max)))
1838 ;;--------------------------------------------------------------
1839 ;; Format Parameter Lists
1840 ;; Some special algorithms are provided to indent the parameter lists in
1841 ;; subprogram declarations. This is done in two steps:
1842 ;; - First parses the parameter list. The returned list has the following
1844 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1846 ;; This is done in `ada-scan-paramlist'.
1847 ;; - Delete and recreate the parameter list in function
1848 ;; `ada-insert-paramlist'.
1849 ;; Both steps are called from `ada-format-paramlist'.
1850 ;; Note: Comments inside the parameter list are lost.
1851 ;; The syntax has to be correct, or the reformating will fail.
1852 ;;--------------------------------------------------------------
1854 (defun ada-format-paramlist ()
1855 "Reformat the parameter list point is in."
1861 (previous-syntax-table (syntax-table)))
1864 (set-syntax-table ada-mode-symbol-syntax-table)
1866 ;; check if really inside parameter list
1867 (or (ada-in-paramlist-p)
1868 (error "Not in parameter list"))
1870 ;; find start of current parameter-list
1871 (ada-search-ignore-string-comment
1872 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1875 (setq begin (point))
1877 ;; find end of parameter-list
1879 (setq delend (point))
1883 ;; find end of last parameter-declaration
1884 (forward-comment -1000)
1887 ;; build a list of all elements of the parameter-list
1888 (setq paramlist (ada-scan-paramlist (1+ begin) end))
1890 ;; delete the original parameter-list
1891 (delete-region begin delend)
1893 ;; insert the new parameter-list
1895 (ada-insert-paramlist paramlist))
1897 ;; restore syntax-table
1898 (set-syntax-table previous-syntax-table)
1901 (defun ada-scan-paramlist (begin end)
1902 "Scan the parameter list found in between BEGIN and END.
1903 Return the equivalent internal parameter list."
1904 (let ((paramlist (list))
1914 ;; loop until end of last parameter
1917 ;; find first character of parameter-declaration
1918 (ada-goto-next-non-ws)
1921 ;; find last character of parameter-declaration
1922 (if (setq match-cons
1923 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1925 (setq epos (car match-cons))
1926 (setq semipos (cdr match-cons)))
1929 ;; read name(s) of parameter(s)
1931 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1933 (setq param (list (match-string 1)))
1934 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1942 (ada-search-ignore-string-comment
1943 "in" nil epos t 'word-search-forward)))))
1951 (ada-search-ignore-string-comment
1952 "out" nil epos t 'word-search-forward)))))
1954 ;; look for 'access'
1960 (ada-search-ignore-string-comment
1961 "access" nil epos t 'word-search-forward)))))
1963 ;; skip 'in'/'out'/'access'
1965 (ada-goto-next-non-ws)
1966 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1968 (ada-goto-next-non-ws))
1970 ;; read type of parameter
1971 ;; We accept spaces in the name, since some software like Rose
1972 ;; generates something like: "A : B 'Class"
1973 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1976 (list (match-string 0))))
1978 ;; read default-expression, if there is one
1979 (goto-char (setq apos (match-end 0)))
1983 (if (setq match-cons
1984 (ada-search-ignore-string-comment
1985 ":=" nil epos t 'search-forward))
1986 (buffer-substring (car match-cons) epos)
1989 ;; add this parameter-declaration to the list
1990 (setq paramlist (append paramlist (list param)))
1992 ;; check if it was the last parameter
1995 (goto-char semipos))
1997 (reverse paramlist)))
1999 (defun ada-insert-paramlist (paramlist)
2000 "Insert a formatted PARAMLIST in the buffer."
2001 (let ((i (length paramlist))
2010 ;; loop until last parameter
2011 (while (not (zerop i))
2014 ;; get max length of parameter-name
2015 (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
2017 ;; get max length of type-name
2018 (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
2020 ;; is there any 'in' ?
2021 (setq inp (or inp (nth 1 (nth i paramlist))))
2023 ;; is there any 'out' ?
2024 (setq outp (or outp (nth 2 (nth i paramlist))))
2026 ;; is there any 'access' ?
2027 (setq accessp (or accessp (nth 3 (nth i paramlist))))
2030 ;; does paramlist already start on a separate line ?
2032 (re-search-backward "^.\\|[^ \t]" nil t)
2034 ;; yes => re-indent it
2036 (ada-indent-current)
2038 (if (looking-at "\\(is\\|return\\)")
2039 (replace-match " \\1"))))
2041 ;; no => insert it where we are after removing any whitespace
2045 ((looking-at "[ \t]*\\(\n\\|;\\)")
2046 (replace-match "\\1"))
2047 ((looking-at "[ \t]*\\(is\\|return\\)")
2048 (replace-match " \\1"))))
2052 (ada-indent-current)
2054 (setq firstcol (current-column))
2055 (setq i (length paramlist))
2057 ;; loop until last parameter
2058 (while (not (zerop i))
2060 (setq column firstcol)
2062 ;; insert parameter-name, space and colon
2063 (insert (nth 0 (nth i paramlist)))
2064 (indent-to (+ column parlen 1))
2066 (setq column (current-column))
2068 ;; insert 'in' or space
2069 (if (nth 1 (nth i paramlist))
2074 (not (nth 3 (nth i paramlist))))
2077 ;; insert 'out' or space
2078 (if (nth 2 (nth i paramlist))
2083 (not (nth 3 (nth i paramlist))))
2087 (if (nth 3 (nth i paramlist))
2090 (setq column (current-column))
2092 ;; insert type-name and, if necessary, space and default-expression
2093 (insert (nth 4 (nth i paramlist)))
2094 (if (nth 5 (nth i paramlist))
2096 (indent-to (+ column typlen 1))
2097 (insert (nth 5 (nth i paramlist)))))
2099 ;; check if it was the last parameter
2102 ;; no => insert ';' and newline and indent
2105 (indent-to firstcol))
2108 ;; if anything follows, except semicolon, newline, is or return
2109 ;; put it in a new line and indent it
2110 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2111 (ada-indent-newline-indent))
2116 ;;;----------------------------------------------------------------
2117 ;; Indentation Engine
2118 ;; All indentations are indicated as a two-element string:
2119 ;; - position of reference in the buffer
2120 ;; - offset to indent from this position (can also be a symbol or a list
2121 ;; that are evaluated)
2122 ;; Thus the total indentation for a line is the column number of the reference
2123 ;; position plus whatever value the evaluation of the second element provides.
2124 ;; This mechanism is used so that the ada-mode can "explain" how the
2125 ;; indentation was calculated, by showing which variables were used.
2127 ;; The indentation itself is done in only one pass: first we try to guess in
2128 ;; what context we are by looking at the following keyword or punctuation
2129 ;; sign. If nothing remarkable is found, just try to guess the indentation
2130 ;; based on previous lines.
2132 ;; The relevant functions for indentation are:
2133 ;; - `ada-indent-region': Re-indent a region of text
2134 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2135 ;; calculation that were done
2136 ;; - `ada-indent-current': Re-indent the current line
2137 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2138 ;; based on the context (see above).
2139 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2140 ;; For efficiency, these functions do not check they are in the correct
2142 ;;;----------------------------------------------------------------
2144 (defun ada-indent-region (beg end)
2145 "Indent the region between BEG end END."
2148 (let ((block-done 0)
2149 (lines-remaining (count-lines beg end))
2150 (msg (format "%%4d out of %4d lines remaining ..."
2151 (count-lines beg end)))
2152 (endmark (copy-marker end)))
2153 ;; catch errors while indenting
2154 (while (< (point) endmark)
2155 (if (> block-done 39)
2157 (setq lines-remaining (- lines-remaining block-done)
2159 (message msg lines-remaining)))
2160 (if (= (char-after) ?\n) nil
2161 (ada-indent-current))
2163 (setq block-done (1+ block-done)))
2164 (message "indenting ... done")))
2166 (defun ada-indent-newline-indent ()
2167 "Indent the current line, insert a newline and then indent the new line."
2169 (ada-indent-current)
2171 (ada-indent-current))
2173 (defun ada-indent-newline-indent-conditional ()
2174 "Insert a newline and indent it.
2175 The original line is indented first if `ada-indent-after-return' is non-nil.
2176 This function is intended to be bound to the C-m and C-j keys."
2178 (if ada-indent-after-return (ada-indent-current))
2180 (ada-indent-current))
2182 (defun ada-justified-indent-current ()
2183 "Indent the current line and explain how the calculation was done."
2186 (let ((cur-indent (ada-indent-current)))
2188 (let ((line (save-excursion
2189 (goto-char (car cur-indent))
2190 (count-lines 1 (point)))))
2192 (if (equal (cdr cur-indent) '(0))
2193 (message (concat "same indentation as line " (number-to-string line)))
2194 (message (mapconcat (lambda(x)
2199 (number-to-string x))
2201 (concat "- " (symbol-name (cadr x))))
2206 (goto-char (car cur-indent))
2209 (defun ada-batch-reformat ()
2210 "Re-indent and re-case all the files found on the command line.
2211 This function should be used from the Unix/Windows command line, with a
2213 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2215 (while command-line-args-left
2216 (let ((source (car command-line-args-left)))
2217 (message (concat "formating " source))
2219 (ada-indent-region (point-min) (point-max))
2220 (ada-adjust-case-buffer)
2221 (write-file source))
2222 (setq command-line-args-left (cdr command-line-args-left)))
2226 (defsubst ada-goto-previous-word ()
2227 "Move point to the beginning of the previous word of Ada code.
2228 Return the new position of point or nil if not found."
2229 (ada-goto-next-word t))
2231 (defun ada-indent-current ()
2232 "Indent current line as Ada code.
2233 Return the calculation that was done, including the reference point and the
2236 (let ((previous-syntax-table (syntax-table))
2237 (orgpoint (point-marker))
2238 cur-indent tmp-indent
2243 (set-syntax-table ada-mode-symbol-syntax-table)
2245 ;; This need to be done here so that the advice is not always
2246 ;; activated (this might interact badly with other modes)
2247 (if (featurep 'xemacs)
2248 (ad-activate 'parse-partial-sexp t))
2253 ;; Not First line in the buffer ?
2254 (if (save-excursion (zerop (forward-line -1)))
2256 (back-to-indentation)
2257 (ada-get-current-indent))
2259 ;; first line in the buffer
2260 (list (point-min) 0))))
2262 ;; Evaluate the list to get the column to indent to
2263 ;; prev-indent contains the column to indent to
2265 (setq prev-indent (save-excursion (goto-char (car cur-indent))
2267 tmp-indent (cdr cur-indent))
2268 (setq prev-indent 0 tmp-indent '()))
2270 (while (not (null tmp-indent))
2272 ((numberp (car tmp-indent))
2273 (setq prev-indent (+ prev-indent (car tmp-indent))))
2275 (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
2277 (setq tmp-indent (cdr tmp-indent)))
2279 ;; only re-indent if indentation is different then the current
2280 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
2283 (delete-horizontal-space)
2284 (indent-to prev-indent))
2286 ;; restore position of point
2288 (goto-char orgpoint)
2289 (if (< (current-column) (current-indentation))
2290 (back-to-indentation)))
2292 ;; restore syntax-table
2293 (set-syntax-table previous-syntax-table)
2294 (if (featurep 'xemacs)
2295 (ad-deactivate 'parse-partial-sexp))
2301 (defun ada-get-current-indent ()
2302 "Return the indentation to use for the current line."
2307 (orgpoint (save-excursion
2309 (forward-comment -10000)
2316 ;;-----------------------------
2317 ;; in open parenthesis, but not in parameter-list
2318 ;;-----------------------------
2320 ((and ada-indent-to-open-paren
2321 (not (ada-in-paramlist-p))
2322 (setq column (ada-in-open-paren-p)))
2324 ;; check if we have something like this (Table_Component_Type =>
2325 ;; Source_File_Record)
2328 ;; Align the closing parenthesis on the opening one
2329 (if (= (following-char) ?\))
2332 (skip-chars-backward " \t")
2333 (list (1- (point)) 0))
2335 (if (and (skip-chars-backward " \t")
2336 (= (char-before) ?\n)
2337 (not (forward-comment -10000))
2338 (= (char-before) ?>))
2339 ;; ??? Could use a different variable
2340 (list column 'ada-broken-indent)
2342 ;; We want all continuation lines to be indented the same
2343 ;; (ada-broken-line from the opening parenthesis. However, in
2344 ;; parameter list, each new parameter should be indented at the
2345 ;; column as the opening parenthesis.
2347 ;; A special case to handle nested boolean expressions, as in
2349 ;; and then C) -- indented by ada-broken-indent
2350 ;; or else D) -- indenting this line.
2351 ;; ??? This is really a hack, we should have a proper way to go to
2352 ;; ??? the beginning of the statement
2354 (if (= (char-before) ?\))
2357 (if (memq (char-before) '(?, ?\; ?\( ?\)))
2359 (list column 'ada-continuation-indent)
2362 ;;---------------------------
2364 ;;---------------------------
2367 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2369 ;;---------------------------
2371 ;;---------------------------
2373 ((= (downcase (char-after)) ?e)
2376 ;; ------- end ------
2378 ((looking-at "end\\>")
2382 (ada-goto-matching-start 1)
2385 ;; found 'loop' => skip back to 'while' or 'for'
2386 ;; if 'loop' is not on a separate line
2387 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2391 (looking-at ".+\\<loop\\>"))
2394 (setq limit (car (ada-search-ignore-string-comment ";" t))))
2398 (ada-search-ignore-string-comment ada-loop-start-re t limit))
2399 (not (looking-at "\\<loop\\>"))))
2401 (goto-char (car match-cons))
2404 (if (looking-at ada-named-block-re)
2405 (setq label (- ada-label-indent))))))))
2407 ;; found 'record' =>
2408 ;; if the keyword is found at the beginning of a line (or just
2409 ;; after limited, we indent on it, otherwise we indent on the
2410 ;; beginning of the type declaration)
2411 ;; type A is (B : Integer;
2412 ;; C : Integer) is record
2413 ;; end record; -- This is badly indented otherwise
2414 (if (looking-at "record")
2417 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2418 (list (save-excursion (back-to-indentation) (point)) 0)
2419 (list (save-excursion
2420 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2423 ;; Else keep the same indentation as the beginning statement
2424 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
2426 ;; ------ exception ----
2428 ((looking-at "exception\\>")
2430 (ada-goto-matching-start 1)
2431 (list (save-excursion (back-to-indentation) (point)) 0)))
2435 ((looking-at "else\\>")
2436 (if (save-excursion (ada-goto-previous-word)
2437 (looking-at "\\<or\\>"))
2438 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2440 (ada-goto-matching-start 1 nil t)
2441 (list (progn (back-to-indentation) (point)) 0))))
2445 ((looking-at "elsif\\>")
2447 (ada-goto-matching-start 1 nil t)
2448 (list (progn (back-to-indentation) (point)) 0)))
2452 ;;---------------------------
2453 ;; starting with w (when)
2454 ;;---------------------------
2456 ((and (= (downcase (char-after)) ?w)
2457 (looking-at "when\\>"))
2459 (ada-goto-matching-start 1)
2460 (list (save-excursion (back-to-indentation) (point))
2463 ;;---------------------------
2464 ;; starting with t (then)
2465 ;;---------------------------
2467 ((and (= (downcase (char-after)) ?t)
2468 (looking-at "then\\>"))
2469 (if (save-excursion (ada-goto-previous-word)
2470 (looking-at "and\\>"))
2471 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2473 ;; Select has been added for the statement: "select ... then abort"
2474 (ada-search-ignore-string-comment
2475 "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2476 (list (progn (back-to-indentation) (point))
2477 'ada-stmt-end-indent))))
2479 ;;---------------------------
2480 ;; starting with l (loop)
2481 ;;---------------------------
2483 ((and (= (downcase (char-after)) ?l)
2484 (looking-at "loop\\>"))
2487 (goto-char (match-end 0))
2488 (ada-goto-stmt-start)
2489 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2490 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2491 (unless (looking-at ada-loop-start-re)
2492 (ada-search-ignore-string-comment ada-loop-start-re
2494 (if (looking-at "\\<loop\\>")
2495 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2496 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2498 ;;----------------------------
2499 ;; starting with l (limited) or r (record)
2500 ;;----------------------------
2502 ((or (and (= (downcase (char-after)) ?l)
2503 (looking-at "limited\\>"))
2504 (and (= (downcase (char-after)) ?r)
2505 (looking-at "record\\>")))
2508 (ada-search-ignore-string-comment
2509 "\\<\\(type\\|use\\)\\>" t nil)
2510 (if (looking-at "\\<use\\>")
2511 (ada-search-ignore-string-comment "for" t nil nil
2512 'word-search-backward))
2513 (list (progn (back-to-indentation) (point))
2514 'ada-indent-record-rel-type)))
2516 ;;---------------------------
2517 ;; starting with b (begin)
2518 ;;---------------------------
2520 ((and (= (downcase (char-after)) ?b)
2521 (looking-at "begin\\>"))
2523 (if (ada-goto-matching-decl-start t)
2524 (list (progn (back-to-indentation) (point)) 0)
2525 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2527 ;;---------------------------
2528 ;; starting with i (is)
2529 ;;---------------------------
2531 ((and (= (downcase (char-after)) ?i)
2532 (looking-at "is\\>"))
2534 (if (and ada-indent-is-separate
2536 (goto-char (match-end 0))
2537 (ada-goto-next-non-ws (save-excursion (end-of-line)
2539 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2541 (ada-goto-stmt-start)
2542 (list (progn (back-to-indentation) (point)) 'ada-indent))
2544 (ada-goto-stmt-start)
2545 (if (looking-at "\\<package\\|procedure\\|function\\>")
2546 (list (progn (back-to-indentation) (point)) 0)
2547 (list (progn (back-to-indentation) (point)) 'ada-indent)))))
2549 ;;---------------------------
2550 ;; starting with r (return, renames)
2551 ;;---------------------------
2553 ((and (= (downcase (char-after)) ?r)
2554 (looking-at "re\\(turn\\|names\\)\\>"))
2557 (let ((var 'ada-indent-return))
2558 ;; If looking at a renames, skip the 'return' statement too
2559 (if (looking-at "renames")
2562 (set 'pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
2564 (= (downcase (char-after (car pos))) ?r))
2565 (goto-char (car pos)))
2566 (set 'var 'ada-indent-renames)))
2568 (forward-comment -1000)
2569 (if (= (char-before) ?\))
2573 ;; If there is a parameter list, and we have a function declaration
2574 ;; or a access to subprogram declaration
2576 (if (and (= (following-char) ?\()
2580 (looking-at "\\(function\\|procedure\\)\\>"))
2584 (looking-at "\\(function\\|procedure\\)\\>")))))
2586 ;; The indentation depends of the value of ada-indent-return
2587 (if (<= (eval var) 0)
2588 (list (point) (list '- var))
2589 (list (progn (backward-word num-back) (point))
2592 ;; Else there is no parameter list, but we have a function
2593 ;; Only do something special if the user want to indent
2594 ;; relative to the "function" keyword
2595 (if (and (> (eval var) 0)
2596 (save-excursion (forward-word -1)
2597 (looking-at "function\\>")))
2598 (list (progn (forward-word -1) (point)) var)
2601 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
2603 ;;--------------------------------
2604 ;; starting with 'o' or 'p'
2605 ;; 'or' as statement-start
2606 ;; 'private' as statement-start
2607 ;;--------------------------------
2609 ((and (or (= (downcase (char-after)) ?o)
2610 (= (downcase (char-after)) ?p))
2611 (or (ada-looking-at-semi-or)
2612 (ada-looking-at-semi-private)))
2614 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2615 (ada-goto-matching-start 1)
2616 (list (progn (back-to-indentation) (point)) 0)))
2618 ;;--------------------------------
2619 ;; starting with 'd' (do)
2620 ;;--------------------------------
2622 ((and (= (downcase (char-after)) ?d)
2623 (looking-at "do\\>"))
2625 (ada-goto-stmt-start)
2626 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2628 ;;--------------------------------
2629 ;; starting with '-' (comment)
2630 ;;--------------------------------
2632 ((= (char-after) ?-)
2633 (if ada-indent-comment-as-code
2635 ;; Indent comments on previous line comments if required
2636 ;; We must use a search-forward (even if the code is more complex),
2637 ;; since we want to find the beginning of the comment.
2640 (if (and ada-indent-align-comments
2644 (while (and (not pos)
2645 (search-forward "--"
2647 (end-of-line) (point))
2649 (unless (ada-in-string-p)
2650 (setq pos (point))))
2654 ;; Else always on previous line
2655 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2657 ;; Else same indentation as the previous line
2658 (list (save-excursion (back-to-indentation) (point)) 0)))
2660 ;;--------------------------------
2661 ;; starting with '#' (preprocessor line)
2662 ;;--------------------------------
2664 ((and (= (char-after) ?#)
2665 (equal ada-which-compiler 'gnat)
2666 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2667 (list (save-excursion (beginning-of-line) (point)) 0))
2669 ;;--------------------------------
2670 ;; starting with ')' (end of a parameter list)
2671 ;;--------------------------------
2673 ((and (not (eobp)) (= (char-after) ?\)))
2679 ;;---------------------------------
2680 ;; new/abstract/separate
2681 ;;---------------------------------
2683 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2684 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2686 ;;---------------------------------
2687 ;; package/function/procedure
2688 ;;---------------------------------
2690 ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
2691 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2693 ;; Go up until we find either a generic section, or the end of the
2694 ;; previous subprogram/package
2696 (while (and (not found)
2697 (ada-search-ignore-string-comment
2698 "\\<\\(generic\\|end\\|begin\\|package\\|procedure\\|function\\)\\>" t))
2700 ;; avoid "with procedure"... in generic parts
2703 (setq found (not (looking-at "with"))))))
2705 (if (looking-at "generic")
2706 (list (progn (back-to-indentation) (point)) 0)
2707 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2709 ;;---------------------------------
2711 ;;---------------------------------
2713 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2715 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2716 (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
2717 '(ada-label-indent))))
2721 ;;---------------------------------
2723 ;;---------------------------------
2724 (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2726 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2727 "Calculate the indentation for the new line after ORGPOINT.
2728 The result list is based on the previous lines in the buffer.
2729 If NOMOVE is nil, moves point to the beginning of the current statement.
2730 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2732 (goto-char initial-pos))
2733 (let ((oldpoint (point)))
2735 ;; Is inside a parameter-list ?
2736 (if (ada-in-paramlist-p)
2737 (ada-get-indent-paramlist)
2739 ;; move to beginning of current statement
2741 (ada-goto-stmt-start))
2743 ;; no beginning found => don't change indentation
2744 (if (and (eq oldpoint (point))
2746 (ada-get-indent-nochange)
2751 ada-indent-to-open-paren
2752 (ada-in-open-paren-p))
2753 (ada-get-indent-open-paren))
2755 ((looking-at "end\\>")
2756 (ada-get-indent-end orgpoint))
2758 ((looking-at ada-loop-start-re)
2759 (ada-get-indent-loop orgpoint))
2761 ((looking-at ada-subprog-start-re)
2762 (ada-get-indent-subprog orgpoint))
2764 ((looking-at ada-block-start-re)
2765 (ada-get-indent-block-start orgpoint))
2767 ((looking-at "\\(sub\\)?type\\>")
2768 (ada-get-indent-type orgpoint))
2770 ;; "then" has to be included in the case of "select...then abort"
2771 ;; statements, since (goto-stmt-start) at the beginning of
2772 ;; the current function would leave the cursor on that position
2773 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2774 (ada-get-indent-if orgpoint))
2776 ((looking-at "case\\>")
2777 (ada-get-indent-case orgpoint))
2779 ((looking-at "when\\>")
2780 (ada-get-indent-when orgpoint))
2782 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2783 (ada-get-indent-label orgpoint))
2785 ((looking-at "separate\\>")
2786 (ada-get-indent-nochange))
2790 (list (+ (save-excursion (back-to-indentation) (point))
2791 (- ada-label-indent))))
2794 ((looking-at "with\\>\\|use\\>")
2795 ;; Are we still in that statement, or are we in fact looking at
2796 ;; the previous one ?
2797 (if (save-excursion (search-forward ";" oldpoint t))
2798 (list (progn (back-to-indentation) (point)) 0)
2799 (list (point) (if (looking-at "with")
2804 (ada-get-indent-noindent orgpoint)))))
2807 (defun ada-get-indent-open-paren ()
2808 "Calculate the indentation when point is behind an unclosed parenthesis."
2809 (list (ada-in-open-paren-p) 0))
2811 (defun ada-get-indent-nochange ()
2812 "Return the current indentation of the previous line."
2815 (back-to-indentation)
2818 (defun ada-get-indent-paramlist ()
2819 "Calculate the indentation when point is inside a parameter list."
2821 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2823 ;; in front of the first parameter
2824 ((= (char-after) ?\()
2825 (goto-char (match-end 0))
2828 ;; in front of another parameter
2829 ((= (char-after) ?\;)
2830 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2831 (ada-goto-next-non-ws)
2834 ;; After an affectation (default parameter value in subprogram
2836 ((and (= (following-char) ?=) (= (preceding-char) ?:))
2837 (back-to-indentation)
2838 (list (point) 'ada-broken-indent))
2840 ;; inside a parameter declaration
2842 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2843 (ada-goto-next-non-ws)
2844 (list (point) 0)))))
2846 (defun ada-get-indent-end (orgpoint)
2847 "Calculate the indentation when point is just before an end statement.
2848 ORGPOINT is the limit position used in the calculation."
2849 (let ((defun-name nil)
2852 ;; is the line already terminated by ';' ?
2854 (ada-search-ignore-string-comment ";" nil orgpoint nil
2857 ;; yes, look what's following 'end'
2860 (ada-goto-next-non-ws)
2862 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\)\\>")
2863 (save-excursion (ada-check-matching-start (match-string 0)))
2864 (list (save-excursion (back-to-indentation) (point)) 0))
2867 ;; loop/select/if/case/record/select
2869 ((looking-at "\\<record\\>")
2871 (ada-check-matching-start (match-string 0))
2872 ;; we are now looking at the matching "record" statement
2874 (ada-goto-stmt-start)
2875 ;; now on the matching type declaration, or use clause
2876 (unless (looking-at "\\(for\\|type\\)\\>")
2877 (ada-search-ignore-string-comment "\\<type\\>" t))
2878 (list (progn (back-to-indentation) (point)) 0)))
2880 ;; a named block end
2882 ((looking-at ada-ident-re)
2883 (setq defun-name (match-string 0))
2885 (ada-goto-matching-start 0)
2886 (ada-check-defun-name defun-name))
2887 (list (progn (back-to-indentation) (point)) 0))
2889 ;; a block-end without name
2891 ((= (char-after) ?\;)
2893 (ada-goto-matching-start 0)
2894 (if (looking-at "\\<begin\\>")
2896 (setq indent (list (point) 0))
2897 (if (ada-goto-matching-decl-start t)
2898 (list (progn (back-to-indentation) (point)) 0)
2900 (list (progn (back-to-indentation) (point)) 0)
2903 ;; anything else - should maybe signal an error ?
2906 (list (save-excursion (back-to-indentation) (point))
2907 'ada-broken-indent))))
2909 (list (save-excursion (back-to-indentation) (point))
2910 'ada-broken-indent))))
2912 (defun ada-get-indent-case (orgpoint)
2913 "Calculate the indentation when point is just before a case statement.
2914 ORGPOINT is the limit position used in the calculation."
2915 (let ((match-cons nil)
2919 ;; case..is..when..=>
2922 (setq match-cons (and
2923 ;; the `=>' must be after the keyword `is'.
2924 (ada-search-ignore-string-comment
2925 "is" nil orgpoint nil 'word-search-forward)
2926 (ada-search-ignore-string-comment
2927 "[ \t\n]+=>" nil orgpoint))))
2929 (goto-char (car match-cons))
2930 (unless (ada-search-ignore-string-comment "when" t opos)
2931 (error "Missing 'when' between 'case' and '=>'"))
2932 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2937 (setq match-cons (ada-search-ignore-string-comment
2938 "when" nil orgpoint nil 'word-search-forward)))
2939 (goto-char (cdr match-cons))
2940 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2945 (setq match-cons (ada-search-ignore-string-comment
2946 "is" nil orgpoint nil 'word-search-forward)))
2947 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2952 (list (save-excursion (back-to-indentation) (point))
2953 'ada-broken-indent)))))
2955 (defun ada-get-indent-when (orgpoint)
2956 "Calculate the indentation when point is just before a when statement.
2957 ORGPOINT is the limit position used in the calculation."
2958 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2959 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2960 (list cur-indent 'ada-indent)
2961 (list cur-indent 'ada-broken-indent))))
2963 (defun ada-get-indent-if (orgpoint)
2964 "Calculate the indentation when point is just before an if statement.
2965 ORGPOINT is the limit position used in the calculation."
2966 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2969 ;; Move to the correct then (ignore all "and then")
2971 (while (and (setq match-cons (ada-search-ignore-string-comment
2972 "\\<\\(then\\|and[ \t]*then\\)\\>"
2974 (= (downcase (char-after (car match-cons))) ?a)))
2975 ;; If "then" was found (we are looking at it)
2979 ;; 'then' first in separate line ?
2980 ;; => indent according to 'then',
2981 ;; => else indent according to 'if'
2984 (back-to-indentation)
2985 (looking-at "\\<then\\>"))
2986 (setq cur-indent (save-excursion (back-to-indentation) (point))))
2989 (list cur-indent 'ada-indent))
2991 (list cur-indent 'ada-broken-indent))))
2993 (defun ada-get-indent-block-start (orgpoint)
2994 "Calculate the indentation when point is at the start of a block.
2995 ORGPOINT is the limit position used in the calculation."
3000 (setq pos (ada-goto-next-non-ws orgpoint)))
3003 (ada-indent-on-previous-lines t orgpoint)))
3005 ;; Special case for record types, for instance for:
3006 ;; type A is (B : Integer;
3007 ;; C : Integer) is record
3008 ;; null; -- This is badly indented otherwise
3009 ((looking-at "record")
3011 ;; If record is at the beginning of the line, indent from there
3014 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
3015 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
3017 ;; else indent relative to the type command
3018 (list (save-excursion
3019 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
3022 ;; nothing follows the block-start
3024 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
3026 (defun ada-get-indent-subprog (orgpoint)
3027 "Calculate the indentation when point is just before a subprogram.
3028 ORGPOINT is the limit position used in the calculation."
3029 (let ((match-cons nil)
3030 (cur-indent (save-excursion (back-to-indentation) (point)))
3033 ;; is there an 'is' in front of point ?
3037 (ada-search-ignore-string-comment
3038 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
3040 ;; yes, then skip to its end
3044 (goto-char (cdr match-cons)))
3046 ;; no, then goto next non-ws, if there is one in front of point
3049 (unless (ada-goto-next-non-ws orgpoint)
3050 (goto-char orgpoint))))
3054 ;; nothing follows 'is'
3059 (not (ada-search-ignore-string-comment
3060 "[^ \t\n]" nil orgpoint t))))
3061 (list cur-indent 'ada-indent))
3063 ;; is abstract/separate/new ...
3069 (ada-search-ignore-string-comment
3070 "\\<\\(separate\\|new\\|abstract\\)\\>"
3072 (goto-char (car match-cons))
3073 (ada-search-ignore-string-comment ada-subprog-start-re t)
3074 (ada-get-indent-noindent orgpoint))
3076 ;; something follows 'is'
3080 (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
3081 (goto-char match-cons)
3082 (ada-indent-on-previous-lines t orgpoint)))
3087 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
3088 (list cur-indent 0))
3093 (list cur-indent 'ada-broken-indent)))))
3095 (defun ada-get-indent-noindent (orgpoint)
3096 "Calculate the indentation when point is just before a 'noindent stmt'.
3097 ORGPOINT is the limit position used in the calculation."
3104 ;; This one is called when indenting a line preceded by a multi-line
3105 ;; subprogram declaration (in that case, we are at this point inside
3106 ;; the parameter declaration list)
3107 ((ada-in-paramlist-p)
3108 (ada-previous-procedure)
3109 (list (save-excursion (back-to-indentation) (point)) 0))
3111 ;; This one is called when indenting the second line of a multi-line
3112 ;; declaration section, in a declare block or a record declaration
3113 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3114 (list (save-excursion (back-to-indentation) (point))
3115 'ada-broken-decl-indent))
3117 ;; This one is called in every over case when indenting a line at the
3120 (if (looking-at ada-named-block-re)
3121 (setq label (- ada-label-indent))
3125 ;; "with private" or "null record" cases
3126 (if (or (save-excursion
3127 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
3129 (save-excursion (forward-char -7);; skip back "private"
3130 (ada-goto-previous-word)
3131 (looking-at "with"))))
3133 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
3135 (save-excursion (forward-char -6);; skip back "record"
3136 (ada-goto-previous-word)
3137 (looking-at "null")))))
3140 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
3141 (list (save-excursion (back-to-indentation) (point)) 0)))))
3143 (ada-search-ignore-string-comment ";" nil orgpoint nil
3145 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
3146 (list (+ (save-excursion (back-to-indentation) (point)) label)
3147 'ada-broken-indent)))))))
3149 (defun ada-get-indent-label (orgpoint)
3150 "Calculate the indentation when before a label or variable declaration.
3151 ORGPOINT is the limit position used in the calculation."
3152 (let ((match-cons nil)
3153 (cur-indent (save-excursion (back-to-indentation) (point))))
3154 (ada-search-ignore-string-comment ":" nil)
3158 (setq match-cons (ada-search-ignore-string-comment
3159 ada-loop-start-re nil orgpoint)))
3160 (goto-char (car match-cons))
3161 (ada-get-indent-loop orgpoint))
3165 (setq match-cons (ada-search-ignore-string-comment
3166 "\\<declare\\|begin\\>" nil orgpoint)))
3167 (goto-char (car match-cons))
3168 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3170 ;; variable declaration
3173 (ada-search-ignore-string-comment ";" nil orgpoint))
3175 (list cur-indent 'ada-broken-indent)))
3177 ;; nothing follows colon
3179 (list cur-indent '(- ada-label-indent))))))
3181 (defun ada-get-indent-loop (orgpoint)
3182 "Calculate the indentation when just before a loop or a for ... use.
3183 ORGPOINT is the limit position used in the calculation."
3184 (let ((match-cons nil)
3187 ;; If looking at a named block, skip the label
3188 (label (save-excursion
3190 (if (looking-at ada-named-block-re)
3191 (- ada-label-indent)
3197 ;; statement complete
3200 (ada-search-ignore-string-comment ";" nil orgpoint nil
3202 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
3206 ((looking-at "loop\\>")
3207 (setq pos (ada-get-indent-block-start orgpoint))
3210 (list (+ (car pos) label) (cdr pos))))
3213 ;; 'for'- loop (or also a for ... use statement)
3215 ((looking-at "for\\>")
3222 (goto-char (match-end 0))
3223 (ada-goto-next-non-ws orgpoint)
3225 (if (= (char-after) ?') (forward-word 1) t)
3226 (ada-goto-next-non-ws orgpoint)
3227 (looking-at "\\<use\\>")
3229 ;; check if there is a 'record' before point
3232 (setq match-cons (ada-search-ignore-string-comment
3233 "record" nil orgpoint nil 'word-search-forward))
3237 (goto-char (car match-cons))
3238 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3239 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3246 (setq match-cons (ada-search-ignore-string-comment
3247 "loop" nil orgpoint nil 'word-search-forward)))
3248 (goto-char (car match-cons))
3250 ;; indent according to 'loop', if it's first in the line;
3251 ;; otherwise to 'for'
3253 (unless (save-excursion
3254 (back-to-indentation)
3255 (looking-at "\\<loop\\>"))
3257 (list (+ (save-excursion (back-to-indentation) (point)) label)
3260 ;; for-statement is broken
3263 (list (+ (save-excursion (back-to-indentation) (point)) label)
3264 'ada-broken-indent))))
3269 ((looking-at "while\\>")
3274 (setq match-cons (ada-search-ignore-string-comment
3275 "loop" nil orgpoint nil 'word-search-forward)))
3278 (goto-char (car match-cons))
3280 ;; indent according to 'loop', if it's first in the line;
3281 ;; otherwise to 'while'.
3283 (unless (save-excursion
3284 (back-to-indentation)
3285 (looking-at "\\<loop\\>"))
3287 (list (+ (save-excursion (back-to-indentation) (point)) label)
3290 (list (+ (save-excursion (back-to-indentation) (point)) label)
3291 'ada-broken-indent))))))
3293 (defun ada-get-indent-type (orgpoint)
3294 "Calculate the indentation when before a type statement.
3295 ORGPOINT is the limit position used in the calculation."
3296 (let ((match-dat nil))
3299 ;; complete record declaration
3303 (setq match-dat (ada-search-ignore-string-comment
3304 "end" nil orgpoint nil 'word-search-forward))
3305 (ada-goto-next-non-ws)
3306 (looking-at "\\<record\\>")
3308 (ada-goto-next-non-ws)
3309 (= (char-after) ?\;)))
3310 (goto-char (car match-dat))
3311 (list (save-excursion (back-to-indentation) (point)) 0))
3316 (setq match-dat (ada-search-ignore-string-comment
3317 "record" nil orgpoint nil 'word-search-forward)))
3318 (goto-char (car match-dat))
3319 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3321 ;; complete type declaration
3324 (ada-search-ignore-string-comment ";" nil orgpoint nil
3326 (list (save-excursion (back-to-indentation) (point)) 0))
3328 ;; "type ... is", but not "type ... is ...", which is broken
3332 (ada-search-ignore-string-comment "is" nil orgpoint nil
3333 'word-search-forward)
3334 (not (ada-goto-next-non-ws orgpoint))))
3335 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3340 (list (save-excursion (back-to-indentation) (point))
3341 'ada-broken-indent)))))
3344 ;; -----------------------------------------------------------
3345 ;; -- searching and matching
3346 ;; -----------------------------------------------------------
3348 (defun ada-goto-stmt-start ()
3349 "Move point to the beginning of the statement that point is in or after.
3350 Return the new position of point.
3351 As a special case, if we are looking at a closing parenthesis, skip to the
3353 (let ((match-dat nil)
3356 (setq match-dat (ada-search-prev-end-stmt))
3360 ;; found a previous end-statement => check if anything follows
3362 (unless (looking-at "declare")
3364 (unless (save-excursion
3365 (goto-char (cdr match-dat))
3366 (ada-goto-next-non-ws orgpoint))
3368 ;; nothing follows => it's the end-statement directly in
3369 ;; front of point => search again
3371 (setq match-dat (ada-search-prev-end-stmt)))
3373 ;; if found the correct end-statement => goto next non-ws
3376 (goto-char (cdr match-dat)))
3377 (ada-goto-next-non-ws)
3381 ;; no previous end-statement => we are at the beginning of the
3382 ;; accessible part of the buffer
3385 (goto-char (point-min))
3387 ;; skip to the very first statement, if there is one
3389 (unless (ada-goto-next-non-ws orgpoint)
3390 (goto-char orgpoint))))
3394 (defun ada-search-prev-end-stmt ()
3395 "Move point to previous end statement.
3396 Return a cons cell whose car is the beginning and whose cdr
3397 is the end of the match."
3398 (let ((match-dat nil)
3401 ;; search until found or beginning-of-buffer
3405 (setq match-dat (ada-search-ignore-string-comment
3406 ada-end-stmt-re t)))
3408 (goto-char (car match-dat))
3409 (unless (ada-in-open-paren-p)
3413 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3415 (ada-goto-previous-word)
3416 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3421 (and (save-excursion (ada-goto-previous-word)
3422 (ada-goto-previous-word)
3423 (not (looking-at "subtype")))
3425 (save-excursion (goto-char (cdr match-dat))
3426 (ada-goto-next-non-ws)
3427 ;; words that can go after an 'is'
3432 '("separate" "access" "array"
3433 "abstract" "new") t)
3445 (defun ada-goto-next-non-ws (&optional limit)
3446 "Skip white spaces, newlines and comments to next non-ws character.
3447 Stop the search at LIMIT.
3448 Do not call this function from within a string."
3450 (setq limit (point-max)))
3451 (while (and (<= (point) limit)
3452 (progn (forward-comment 10000)
3453 (if (and (not (eobp))
3454 (save-excursion (forward-char 1)
3456 (progn (forward-sexp 1) t)))))
3457 (if (< (point) limit)
3463 (defun ada-goto-stmt-end (&optional limit)
3464 "Move point to the end of the statement that point is in or before.
3465 Return the new position of point or nil if not found.
3466 Stop the search at LIMIT."
3467 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
3472 (defun ada-goto-next-word (&optional backward)
3473 "Move point to the beginning of the next word of Ada code.
3474 If BACKWARD is non-nil, jump to the beginning of the previous word.
3475 Return the new position of point or nil if not found."
3476 (let ((match-cons nil)
3478 (old-syntax (char-to-string (char-syntax ?_))))
3479 (modify-syntax-entry ?_ "w")
3481 (skip-syntax-forward "w"))
3482 (if (setq match-cons
3484 (ada-search-ignore-string-comment "\\w" t nil t)
3485 (ada-search-ignore-string-comment "\\w" nil nil t)))
3487 ;; move to the beginning of the word found
3490 (goto-char (car match-cons))
3491 (skip-syntax-backward "w")
3494 ;; if not found, restore old position of point
3496 (goto-char orgpoint)
3498 (modify-syntax-entry ?_ old-syntax))
3502 (defun ada-check-matching-start (keyword)
3503 "Signal an error if matching block start is not KEYWORD.
3504 Moves point to the matching block start."
3505 (ada-goto-matching-start 0)
3506 (unless (looking-at (concat "\\<" keyword "\\>"))
3507 (error "Matching start is not '%s'" keyword)))
3510 (defun ada-check-defun-name (defun-name)
3511 "Check if the name of the matching defun really is DEFUN-NAME.
3512 Assumes point to be already positioned by `ada-goto-matching-start'.
3513 Moves point to the beginning of the declaration."
3515 ;; named block without a `declare'
3517 (ada-goto-previous-word)
3518 (looking-at (concat "\\<" defun-name "\\> *:")))
3521 ;; 'accept' or 'package' ?
3523 (unless (looking-at ada-subprog-start-re)
3524 (ada-goto-matching-decl-start))
3526 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3530 ;; a named 'declare'-block ?
3532 (if (looking-at "\\<declare\\>")
3533 (ada-goto-stmt-start)
3535 ;; no, => 'procedure'/'function'/'task'/'protected'
3541 ;; skip 'body' 'type'
3543 (if (looking-at "\\<\\(body\\|type\\)\\>")
3548 ;; should be looking-at the correct name
3550 (unless (looking-at (concat "\\<" defun-name "\\>"))
3551 (error "Matching defun has different name: %s"
3552 (buffer-substring (point)
3553 (progn (forward-sexp 1) (point))))))))
3555 (defun ada-goto-matching-decl-start (&optional noerror recursive)
3556 "Move point to the matching declaration start of the current 'begin'.
3557 If NOERROR is non-nil, it only returns nil if no match was found."
3558 (let ((nest-count 1)
3560 ;; first should be set to t if we should stop at the first
3561 ;; "begin" we encounter.
3562 (first (not recursive))
3567 ;; Ignore "when" most of the time, except if we are looking at the
3568 ;; beginning of a block (structure: case .. is
3572 (if (looking-at "begin")
3573 (setq stop-at-when t))
3576 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3578 (ada-search-ignore-string-comment
3579 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
3580 (looking-at "generic")))
3581 (setq count-generic t))
3583 ;; search backward for interesting keywords
3585 (not (zerop nest-count))
3586 (ada-search-ignore-string-comment ada-matching-decl-start-re t))
3588 ;; calculate nest-depth
3593 (ada-goto-matching-start 1 noerror)
3595 ;; In some case, two begin..end block can follow each other closely,
3596 ;; which we have to detect, as in
3601 ;; begin -- here we should go to procedure, not begin
3604 (if (looking-at "begin")
3605 (let ((loop-again t))
3608 ;; If begin was just there as the beginning of a block
3609 ;; (with no declare) then do nothing, otherwise just
3610 ;; register that we have to find the statement that
3611 ;; required the begin
3613 (ada-search-ignore-string-comment
3614 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3617 (if (looking-at "end")
3618 (ada-goto-matching-start 1 noerror t)
3619 ;; (ada-goto-matching-decl-start noerror t)
3621 (setq loop-again nil)
3622 (unless (looking-at "begin")
3623 (setq nest-count (1+ nest-count))))
3627 ((looking-at "generic")
3631 (setq nest-count (1- nest-count)))))
3636 (unless (looking-at "\\<end[ \t\n]*if\\>")
3638 (setq nest-count (1- nest-count))
3639 (setq first nil)))))
3642 ((looking-at "declare\\|generic")
3643 (setq nest-count (1- nest-count))
3647 ;; check if it is only a type definition, but not a protected
3648 ;; type definition, which should be handled like a procedure.
3649 (if (or (looking-at "is[ \t]+<>")
3651 (forward-comment -10000)
3654 ;; Detect if we have a closing parenthesis (Could be
3655 ;; either the end of subprogram parameters or (<>)
3656 ;; in a type definition
3657 (if (= (char-after) ?\))
3661 (forward-comment -10000)
3663 (skip-chars-backward "a-zA-Z0-9_.'")
3664 (ada-goto-previous-word)
3666 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3668 (ada-goto-previous-word)
3669 (not (looking-at "\\<protected\\>"))))
3671 (goto-char (match-beginning 0))
3673 (setq nest-count (1- nest-count))
3679 (ada-goto-previous-word)
3681 (goto-char (match-beginning 0))))
3684 (looking-at "begin"))
3685 (setq nest-count 0))
3687 ((looking-at "when")
3690 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3693 (setq nest-count (1- nest-count)))
3696 ((looking-at "begin")
3700 (setq nest-count (1+ nest-count))
3705 ;; check if declaration-start is really found
3708 (if (looking-at "is")
3709 (ada-search-ignore-string-comment ada-subprog-start-re t)
3710 (looking-at "declare\\|generic")))
3713 (error "No matching proc/func/task/declare/package/protected")))
3716 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3717 "Move point to the beginning of a block-start.
3718 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3719 If NOERROR is non-nil, it only returns nil if no matching start was found.
3720 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3721 (let ((nest-count (if nest-level nest-level 0))
3726 ;; search backward for interesting keywords
3730 (ada-search-ignore-string-comment ada-matching-start-re t))
3732 (unless (and (looking-at "\\<record\\>")
3735 (looking-at "\\<null\\>")))
3738 ;; calculate nest-depth
3741 ;; found block end => increase nest depth
3743 (setq nest-count (1+ nest-count)))
3745 ;; found loop/select/record/case/if => check if it starts or
3747 ((looking-at "loop\\|select\\|record\\|case\\|if")
3751 ;; check if keyword follows 'end'
3753 (ada-goto-previous-word)
3754 (if (looking-at "\\<end\\>[ \t]*[^;]")
3755 ;; it ends a block => increase nest depth
3756 (setq nest-count (1+ nest-count)
3759 ;; it starts a block => decrease nest depth
3760 (setq nest-count (1- nest-count))))
3763 ;; found package start => check if it really is a block
3764 ((looking-at "package")
3766 ;; ignore if this is just a renames statement
3767 (let ((current (point))
3768 (pos (ada-search-ignore-string-comment
3769 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3771 (goto-char (car pos))
3773 "No matching 'is' or 'renames' for 'package' at"
3775 (number-to-string (count-lines 1 (1+ current)))))))
3776 (unless (looking-at "renames")
3779 (ada-goto-next-non-ws)
3780 ;; ignore it if it is only a declaration with 'new'
3781 ;; We could have package Foo is new ....
3782 ;; or package Foo is separate;
3783 ;; or package Foo is begin null; end Foo
3784 ;; for elaboration code (elaboration)
3785 (if (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3786 (setq nest-count (1- nest-count)))))))
3787 ;; found task start => check if it has a body
3788 ((looking-at "task")
3791 (ada-goto-next-non-ws)
3793 ((looking-at "\\<body\\>"))
3794 ((looking-at "\\<type\\>")
3795 ;; In that case, do nothing if there is a "is"
3796 (forward-word 2);; skip "type"
3797 (ada-goto-next-non-ws);; skip type name
3799 ;; Do nothing if we are simply looking at a simple
3800 ;; "task type name;" statement with no block
3801 (unless (looking-at ";")
3803 ;; Skip the parameters
3804 (if (looking-at "(")
3805 (ada-search-ignore-string-comment ")" nil))
3806 (let ((tmp (ada-search-ignore-string-comment
3807 "\\<\\(is\\|;\\)\\>" nil)))
3810 (goto-char (car tmp))
3811 (if (looking-at "is")
3812 (setq nest-count (1- nest-count)))))))))
3814 ;; Check if that task declaration had a block attached to
3815 ;; it (i.e do nothing if we have just "task name;")
3816 (unless (progn (forward-word 1)
3817 (looking-at "[ \t]*;"))
3818 (setq nest-count (1- nest-count)))))))
3819 ;; all the other block starts
3821 (setq nest-count (1- nest-count)))) ; end of 'cond'
3823 ;; match is found, if nest-depth is zero
3825 (setq found (zerop nest-count))))) ; end of loop
3831 ;; match found => is there anything else to do ?
3836 ;; found 'if' => skip to 'then', if it's on a separate line
3837 ;; and GOTOTHEN is non-nil
3843 (ada-search-ignore-string-comment "then" nil nil nil
3844 'word-search-forward)
3845 (back-to-indentation)
3846 (looking-at "\\<then\\>")))
3847 (goto-char (match-beginning 0)))
3850 ;; found 'do' => skip back to 'accept'
3853 (unless (ada-search-ignore-string-comment
3855 'word-search-backward)
3856 (error "Missing 'accept' in front of 'do'"))))
3861 (error "No matching start"))))))
3864 (defun ada-goto-matching-end (&optional nest-level noerror)
3865 "Move point to the end of a block.
3866 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3867 If NOERROR is non-nil, it only returns nil if found no matching start."
3868 (let ((nest-count (or nest-level 0))
3869 (regex (eval-when-compile
3871 (regexp-opt '("end" "loop" "select" "begin" "case"
3872 "if" "task" "package" "record" "do"
3873 "procedure" "function") t)
3878 ;; First is used for subprograms: they are generally handled
3879 ;; recursively, but of course we do not want to do that the
3880 ;; first time (see comment below about subprograms)
3881 (first (not (looking-at "declare"))))
3883 ;; If we are already looking at one of the keywords, this shouldn't count
3884 ;; in the nesting loop below, so we just make sure we don't count it.
3885 ;; "declare" is a special case because we need to look after the "begin"
3887 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3891 ;; search forward for interesting keywords
3895 (ada-search-ignore-string-comment regex nil))
3898 ;; calculate nest-depth
3902 ;; procedures and functions need to be processed recursively, in
3903 ;; case they are defined in a declare/begin block, as in:
3904 ;; declare -- NL 0 (nested level)
3906 ;; procedure B (C : D) is
3909 ;; end B; -- NL 0, and we would exit
3911 ;; end; -- we should exit here
3912 ;; processing them recursively avoids the need for any special
3914 ;; Nothing should be done if we have only the specs or a
3915 ;; generic instantion.
3917 ((and (looking-at "\\<procedure\\|function\\>"))
3922 (ada-search-ignore-string-comment "is\\|;")
3923 (if (= (char-before) ?s)
3925 (ada-goto-next-non-ws)
3926 (unless (looking-at "\\<new\\>")
3929 (ada-goto-matching-end 0 t)))))))
3931 ;; found block end => decrease nest depth
3932 ((looking-at "\\<end\\>")
3933 (setq nest-count (1- nest-count)
3934 found (<= nest-count 0))
3935 ;; skip the following keyword
3937 (skip-chars-forward "end")
3938 (ada-goto-next-non-ws)
3939 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3942 ;; found package start => check if it really starts a block, and is not
3943 ;; in fact a generic instantiation for instance
3944 ((looking-at "\\<package\\>")
3945 (ada-search-ignore-string-comment "is" nil nil nil
3946 'word-search-forward)
3947 (ada-goto-next-non-ws)
3948 ;; ignore and skip it if it is only a 'new' package
3949 (if (looking-at "\\<new\\>")
3950 (goto-char (match-end 0))
3951 (setq nest-count (1+ nest-count)
3952 found (<= nest-count 0))))
3954 ;; all the other block starts
3957 (setq nest-count (1+ nest-count)))
3958 (setq found (<= nest-count 0))
3959 (forward-word 1))) ; end of 'cond'
3967 (error "No matching end")))
3971 (defun ada-search-ignore-string-comment
3972 (search-re &optional backward limit paramlists search-func)
3973 "Regexp-search for SEARCH-RE, ignoring comments, strings.
3974 If PARAMLISTS is nil, ignore parameter lists. Returns a cons cell of
3975 begin and end of match data or nil, if not found.
3976 The search is done using SEARCH-FUNC, which should search backward if
3977 BACKWARD is non-nil, forward otherwise. SEARCH-FUNC can be optimized
3978 in case we are searching for a constant string.
3979 The search stops at pos LIMIT.
3980 Point is moved at the beginning of the SEARCH-RE."
3985 (previous-syntax-table (syntax-table)))
3988 (setq search-func (if backward 're-search-backward 're-search-forward)))
3991 ;; search until found or end-of-buffer
3992 ;; We have to test that we do not look further than limit
3994 (set-syntax-table ada-mode-symbol-syntax-table)
3995 (while (and (not found)
3997 (or (and backward (<= limit (point)))
3998 (>= limit (point))))
3999 (funcall search-func search-re limit 1))
4000 (setq begin (match-beginning 0))
4001 (setq end (match-end 0))
4003 (setq parse-result (parse-partial-sexp
4004 (save-excursion (beginning-of-line) (point))
4009 ;; If inside a string, skip it (and the following comments)
4011 ((ada-in-string-p parse-result)
4012 (if (featurep 'xemacs)
4013 (search-backward "\"" nil t)
4014 (goto-char (nth 8 parse-result)))
4015 (unless backward (forward-sexp 1)))
4017 ;; If inside a comment, skip it (and the following comments)
4018 ;; There is a special code for comments at the end of the file
4020 ((ada-in-comment-p parse-result)
4021 (if (featurep 'xemacs)
4025 (forward-comment -1))
4026 (goto-char (nth 8 parse-result)))
4028 ;; at the end of the file, it is not possible to skip a comment
4029 ;; so we just go at the end of the line
4030 (if (forward-comment 1)
4032 (forward-comment 1000)
4033 (beginning-of-line))
4036 ;; directly in front of a comment => skip it, if searching forward
4038 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
4039 (unless backward (progn (forward-char -1) (forward-comment 1000))))
4042 ;; found a parameter-list but should ignore it => skip it
4044 ((and (not paramlists) (ada-in-paramlist-p))
4046 (search-backward "(" nil t)
4047 (search-forward ")" nil t)))
4049 ;; found what we were looking for
4052 (setq found t)))) ; end of loop
4054 (set-syntax-table previous-syntax-table)
4060 ;; -------------------------------------------------------
4061 ;; -- Testing the position of the cursor
4062 ;; -------------------------------------------------------
4064 (defun ada-in-decl-p ()
4065 "Return t if point is inside a declarative part.
4066 Assumes point to be at the end of a statement."
4067 (or (ada-in-paramlist-p)
4069 (ada-goto-matching-decl-start t))))
4072 (defun ada-looking-at-semi-or ()
4073 "Return t if looking at an 'or' following a semicolon."
4075 (and (looking-at "\\<or\\>")
4078 (ada-goto-stmt-start)
4079 (looking-at "\\<or\\>")))))
4082 (defun ada-looking-at-semi-private ()
4083 "Return t if looking at the start of a private section in a package.
4084 Returns nil if the private is part of the package name, as in
4085 'private package A is...' (this can only happen at top level)."
4087 (and (looking-at "\\<private\\>")
4088 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4090 ;; Make sure this is the start of a private section (ie after
4091 ;; a semicolon or just after the package declaration, but not
4092 ;; after a 'type ... is private' or 'is new ... with private'
4094 ;; Note that a 'private' statement at the beginning of the buffer
4095 ;; does not indicate a private section, since this is instead a
4096 ;; 'private procedure ...'
4097 (progn (forward-comment -1000)
4099 (or (= (char-before) ?\;)
4100 (and (forward-word -3)
4101 (looking-at "\\<package\\>"))))))))
4104 (defun ada-in-paramlist-p ()
4105 "Return t if point is inside a parameter-list."
4108 (ada-search-ignore-string-comment "(\\|)" t nil t)
4109 ;; inside parentheses ?
4110 (= (char-after) ?\()
4112 ;; We could be looking at two things here:
4113 ;; operator definition: function "." (
4114 ;; subprogram definition: procedure .... (
4115 ;; Let's skip back over the first one
4117 (skip-chars-backward " \t\n")
4118 (if (= (char-before) ?\")
4123 ;; and now over the second one
4126 ;; We should ignore the case when the reserved keyword is in a
4127 ;; comment (for instance, when we have:
4130 ;; we should return nil
4132 (not (ada-in-string-or-comment-p))
4134 ;; right keyword two words before parenthesis ?
4135 ;; Type is in this list because of discriminants
4136 (looking-at (eval-when-compile
4138 "procedure\\|function\\|body\\|"
4139 "task\\|entry\\|accept\\|"
4140 "access[ \t]+procedure\\|"
4141 "access[ \t]+function\\|"
4145 (defun ada-search-ignore-complex-boolean (regexp backwardp)
4146 "Like `ada-search-ignore-string-comment', except that it also ignores
4147 boolean expressions 'and then' and 'or else'."
4149 (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
4150 (save-excursion (forward-word -1)
4151 (looking-at "and then\\|or else"))))
4154 (defun ada-in-open-paren-p ()
4155 "Return the position of the first non-ws behind the last unclosed
4156 parenthesis, or nil."
4158 (let ((parse (parse-partial-sexp
4160 (or (car (ada-search-ignore-complex-boolean
4161 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4167 (goto-char (1+ (nth 1 parse)))
4169 ;; Skip blanks, if they are not followed by a comment
4171 ;; type A is ( Value_0,
4173 ;; type B is ( -- comment
4176 (if (or (not ada-indent-handle-comment-special)
4177 (not (looking-at "[ \t]+--")))
4178 (skip-chars-forward " \t"))
4183 ;; -----------------------------------------------------------
4184 ;; -- Behavior Of TAB Key
4185 ;; -----------------------------------------------------------
4188 "Do indenting or tabbing according to `ada-tab-policy'.
4189 In Transient Mark mode, if the mark is active, operate on the contents
4190 of the region. Otherwise, operate only on the current line."
4192 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
4193 ((eq ada-tab-policy 'indent-auto)
4194 (if (ada-region-selected)
4195 (ada-indent-region (region-beginning) (region-end))
4196 (ada-indent-current)))
4197 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4200 (defun ada-untab (arg)
4201 "Delete leading indenting according to `ada-tab-policy'."
4203 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
4204 ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
4205 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4208 (defun ada-indent-current-function ()
4209 "Ada mode version of the `indent-line-function'."
4211 (let ((starting-point (point-marker)))
4214 (if (< (point) starting-point)
4215 (goto-char starting-point))
4216 (set-marker starting-point nil)
4219 (defun ada-tab-hard ()
4220 "Indent current line to next tab stop."
4224 (insert-char ? ada-indent))
4225 (if (save-excursion (= (point) (progn (beginning-of-line) (point))))
4226 (forward-char ada-indent)))
4228 (defun ada-untab-hard ()
4229 "Indent current line to previous tab stop."
4231 (let ((bol (save-excursion (progn (beginning-of-line) (point))))
4232 (eol (save-excursion (progn (end-of-line) (point)))))
4233 (indent-rigidly bol eol (- 0 ada-indent))))
4237 ;; ------------------------------------------------------------
4239 ;; ------------------------------------------------------------
4241 ;; Not needed any more for Emacs 21.2, but still needed for backward
4243 (defun ada-remove-trailing-spaces ()
4244 "Remove trailing spaces in the whole buffer."
4250 (goto-char (point-min))
4251 (while (re-search-forward "[ \t]+$" (point-max) t)
4252 (replace-match "" nil nil))))))
4254 (defun ada-gnat-style ()
4255 "Clean up comments, `(' and `,' for GNAT style checking switch."
4259 ;; The \n is required, or the line after an empty comment line is
4261 (goto-char (point-min))
4262 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
4263 (replace-match "-- \\1")
4265 (beginning-of-line))
4267 (goto-char (point-min))
4268 (while (re-search-forward "\\>(" nil t)
4269 (if (not (ada-in-string-or-comment-p))
4270 (replace-match " (")))
4271 (goto-char (point-min))
4272 (while (re-search-forward ";--" nil t)
4274 (if (not (ada-in-string-or-comment-p))
4275 (replace-match "; --")))
4276 (goto-char (point-min))
4277 (while (re-search-forward "([ \t]+" nil t)
4278 (if (not (ada-in-string-or-comment-p))
4279 (replace-match "(")))
4280 (goto-char (point-min))
4281 (while (re-search-forward ")[ \t]+)" nil t)
4282 (if (not (ada-in-string-or-comment-p))
4283 (replace-match "))")))
4284 (goto-char (point-min))
4285 (while (re-search-forward "\\>:" nil t)
4286 (if (not (ada-in-string-or-comment-p))
4287 (replace-match " :")))
4289 ;; Make sure there is a space after a ','.
4290 ;; Always go back to the beginning of the match, since otherwise
4291 ;; a statement like ('F','D','E') is incorrectly modified.
4292 (goto-char (point-min))
4293 (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
4294 (if (not (save-excursion
4295 (goto-char (match-beginning 0))
4296 (ada-in-string-or-comment-p)))
4297 (replace-match ", \\1")))
4299 ;; Operators should be surrounded by spaces.
4300 (goto-char (point-min))
4301 (while (re-search-forward
4302 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4304 (goto-char (match-beginning 1))
4305 (if (or (looking-at "--")
4306 (ada-in-string-or-comment-p))
4309 (beginning-of-line))
4311 ((string= (match-string 1) "/=")
4312 (replace-match " /= "))
4313 ((string= (match-string 1) "..")
4314 (replace-match " .. "))
4315 ((string= (match-string 1) "**")
4316 (replace-match " ** "))
4317 ((string= (match-string 1) ":=")
4318 (replace-match " := "))
4320 (replace-match " \\1 ")))
4326 ;; -------------------------------------------------------------
4327 ;; -- Moving To Procedures/Packages/Statements
4328 ;; -------------------------------------------------------------
4330 (defun ada-move-to-start ()
4331 "Move point to the matching start of the current Ada structure."
4334 (previous-syntax-table (syntax-table)))
4337 (set-syntax-table ada-mode-symbol-syntax-table)
4341 ;; do nothing if in string or comment or not on 'end ...;'
4342 ;; or if an error occurs during processing
4345 (ada-in-string-or-comment-p)
4347 (or (looking-at "[ \t]*\\<end\\>")
4349 (or (looking-at "[ \t]*\\<end\\>")
4351 (or (looking-at "[ \t]*\\<end\\>")
4352 (error "Not on end ...;")))
4353 (ada-goto-matching-start 1)
4357 ;; on 'begin' => go on, according to user option
4359 ada-move-to-declaration
4360 (looking-at "\\<begin\\>")
4361 (ada-goto-matching-decl-start)
4362 (setq pos (point))))
4364 ) ; end of save-excursion
4366 ;; now really move to the found position
4369 ;; restore syntax-table
4370 (set-syntax-table previous-syntax-table))))
4372 (defun ada-move-to-end ()
4373 "Move point to the matching end of the block around point.
4374 Moves to 'begin' if in a declarative part."
4378 (previous-syntax-table (syntax-table)))
4381 (set-syntax-table ada-mode-symbol-syntax-table)
4386 ;; Go to the beginning of the current word, and check if we are
4387 ;; directly on 'begin'
4389 (skip-syntax-backward "w")
4390 (looking-at "\\<begin\\>"))
4391 (ada-goto-matching-end 1)
4394 ;; on first line of subprogram body
4395 ;; Do nothing for specs or generic instantion, since these are
4396 ;; handled as the general case (find the enclosing block)
4397 ;; We also need to make sure that we ignore nested subprograms
4399 (and (skip-syntax-backward "w")
4400 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4401 (ada-search-ignore-string-comment "is\\|;")
4402 (not (= (char-before) ?\;))
4404 (skip-syntax-backward "w")
4405 (ada-goto-matching-end 0 t))
4407 ;; on first line of task declaration
4409 (and (ada-goto-stmt-start)
4410 (looking-at "\\<task\\>" )
4412 (ada-goto-next-non-ws)
4413 (looking-at "\\<body\\>")))
4414 (ada-search-ignore-string-comment "begin" nil nil nil
4415 'word-search-forward))
4416 ;; accept block start
4418 (and (ada-goto-stmt-start)
4419 (looking-at "\\<accept\\>" )))
4420 (ada-goto-matching-end 0))
4423 (setq decl-start (and (ada-goto-matching-decl-start t) (point)))
4424 (and decl-start (looking-at "\\<package\\>")))
4425 (ada-goto-matching-end 1))
4427 ;; On a "declare" keyword
4429 (skip-syntax-backward "w")
4430 (looking-at "\\<declare\\>"))
4431 (ada-goto-matching-end 0 t))
4433 ;; inside a 'begin' ... 'end' block
4435 (goto-char decl-start)
4436 (ada-goto-matching-end 0 t))
4438 ;; (hopefully ;-) everything else
4440 (ada-goto-matching-end 1)))
4444 ;; now really move to the position found
4447 ;; restore syntax-table
4448 (set-syntax-table previous-syntax-table))))
4450 (defun ada-next-procedure ()
4451 "Move point to next procedure."
4454 (if (re-search-forward ada-procedure-start-regexp nil t)
4455 (goto-char (match-beginning 2))
4456 (error "No more functions/procedures/tasks")))
4458 (defun ada-previous-procedure ()
4459 "Move point to previous procedure."
4462 (if (re-search-backward ada-procedure-start-regexp nil t)
4463 (goto-char (match-beginning 2))
4464 (error "No more functions/procedures/tasks")))
4466 (defun ada-next-package ()
4467 "Move point to next package."
4470 (if (re-search-forward ada-package-start-regexp nil t)
4471 (goto-char (match-beginning 1))
4472 (error "No more packages")))
4474 (defun ada-previous-package ()
4475 "Move point to previous package."
4478 (if (re-search-backward ada-package-start-regexp nil t)
4479 (goto-char (match-beginning 1))
4480 (error "No more packages")))
4483 ;; ------------------------------------------------------------
4484 ;; -- Define keymap and menus for Ada
4485 ;; -------------------------------------------------------------
4487 (defun ada-create-keymap ()
4488 "Create the keymap associated with the Ada mode."
4490 ;; Indentation and Formatting
4491 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
4492 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
4493 (define-key ada-mode-map "\t" 'ada-tab)
4494 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
4495 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
4496 (if (featurep 'xemacs)
4497 (define-key ada-mode-map '(shift tab) 'ada-untab)
4498 (define-key ada-mode-map [(shift tab)] 'ada-untab))
4499 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
4500 ;; We don't want to make meta-characters case-specific.
4503 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
4504 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
4505 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
4506 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
4509 (unless (lookup-key ada-mode-map "\C-c\C-c")
4510 (define-key ada-mode-map "\C-c\C-c" 'compile))
4513 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
4514 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
4515 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
4516 (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
4518 ;; On XEmacs, you can easily specify whether DEL should deletes
4519 ;; one character forward or one character backward. Take this into
4521 (if (boundp 'delete-key-deletes-forward)
4522 (define-key ada-mode-map [backspace] 'backward-delete-char-untabify)
4523 (define-key ada-mode-map "\177" 'backward-delete-char-untabify))
4526 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4528 ;; Use predefined function of Emacs19 for comments (RE)
4529 (define-key ada-mode-map "\C-c;" 'comment-region)
4530 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4532 ;; The following keys are bound to functions defined in ada-xref.el or
4533 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4534 ;; and activated only if the right compiler is used
4535 (if (featurep 'xemacs)
4537 (define-key ada-mode-map '(shift button3) 'ada-point-and-xref)
4538 (define-key ada-mode-map '(control tab) 'ada-complete-identifier))
4539 (define-key ada-mode-map [C-tab] 'ada-complete-identifier)
4540 (define-key ada-mode-map [S-mouse-3] 'ada-point-and-xref))
4542 (define-key ada-mode-map "\C-co" 'ff-find-other-file)
4543 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4544 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4545 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4546 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4547 (define-key ada-mode-map "\C-cc" 'ada-change-prj)
4548 (define-key ada-mode-map "\C-cd" 'ada-set-default-project-file)
4549 (define-key ada-mode-map "\C-cg" 'ada-gdb-application)
4550 (define-key ada-mode-map "\C-cr" 'ada-run-application)
4551 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4552 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4553 (define-key ada-mode-map "\C-cl" 'ada-find-local-references)
4554 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4555 (define-key ada-mode-map "\C-cf" 'ada-find-file)
4557 (define-key ada-mode-map "\C-cu" 'ada-prj-edit)
4559 ;; The templates, defined in ada-stmt.el
4561 (let ((map (make-sparse-keymap)))
4562 (define-key map "h" 'ada-header)
4563 (define-key map "\C-a" 'ada-array)
4564 (define-key map "b" 'ada-exception-block)
4565 (define-key map "d" 'ada-declare-block)
4566 (define-key map "c" 'ada-case)
4567 (define-key map "\C-e" 'ada-elsif)
4568 (define-key map "e" 'ada-else)
4569 (define-key map "\C-k" 'ada-package-spec)
4570 (define-key map "k" 'ada-package-body)
4571 (define-key map "\C-p" 'ada-procedure-spec)
4572 (define-key map "p" 'ada-subprogram-body)
4573 (define-key map "\C-f" 'ada-function-spec)
4574 (define-key map "f" 'ada-for-loop)
4575 (define-key map "i" 'ada-if)
4576 (define-key map "l" 'ada-loop)
4577 (define-key map "\C-r" 'ada-record)
4578 (define-key map "\C-s" 'ada-subtype)
4579 (define-key map "S" 'ada-tabsize)
4580 (define-key map "\C-t" 'ada-task-spec)
4581 (define-key map "t" 'ada-task-body)
4582 (define-key map "\C-y" 'ada-type)
4583 (define-key map "\C-v" 'ada-private)
4584 (define-key map "u" 'ada-use)
4585 (define-key map "\C-u" 'ada-with)
4586 (define-key map "\C-w" 'ada-when)
4587 (define-key map "w" 'ada-while-loop)
4588 (define-key map "\C-x" 'ada-exception)
4589 (define-key map "x" 'ada-exit)
4590 (define-key ada-mode-map "\C-ct" map))
4594 (defun ada-create-menu ()
4595 "Create the Ada menu as shown in the menu bar."
4598 ["Ada Mode" (info "ada-mode") t]
4599 ["GNAT User's Guide" (info "gnat_ugn")
4600 (eq ada-which-compiler 'gnat)]
4601 ["GNAT Reference Manual" (info "gnat_rm")
4602 (eq ada-which-compiler 'gnat)]
4603 ["Gcc Documentation" (info "gcc")
4604 (eq ada-which-compiler 'gnat)]
4605 ["Gdb Documentation" (info "gdb")
4606 (eq ada-which-compiler 'gnat)]
4607 ["Ada95 Reference Manual" (info "arm95")
4608 (eq ada-which-compiler 'gnat)])
4609 ("Options" :included (eq major-mode 'ada-mode)
4610 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4611 :style toggle :selected ada-auto-case]
4612 ["Auto Indent After Return"
4613 (setq ada-indent-after-return (not ada-indent-after-return))
4614 :style toggle :selected ada-indent-after-return]
4615 ["Automatically Recompile For Cross-references"
4616 (setq ada-xref-create-ali (not ada-xref-create-ali))
4617 :style toggle :selected ada-xref-create-ali
4618 :included (eq ada-which-compiler 'gnat)]
4620 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4621 :style toggle :selected ada-xref-confirm-compile
4622 :included (eq ada-which-compiler 'gnat)]
4623 ["Show Cross-references In Other Buffer"
4624 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4625 :style toggle :selected ada-xref-other-buffer
4626 :included (eq ada-which-compiler 'gnat)]
4627 ["Tight Integration With GNU Visual Debugger"
4628 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4629 :style toggle :selected ada-tight-gvd-integration
4630 :included (string-match "gvd" ada-prj-default-debugger)])
4631 ["Customize" (customize-group 'ada)
4632 :included (fboundp 'customize-group)]
4633 ["Check file" ada-check-current (eq ada-which-compiler 'gnat)]
4634 ["Compile file" ada-compile-current (eq ada-which-compiler 'gnat)]
4635 ["Build" ada-compile-application
4636 (eq ada-which-compiler 'gnat)]
4637 ["Run" ada-run-application t]
4638 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4641 :included (eq ada-which-compiler 'gnat)
4642 ["Load..." ada-set-default-project-file t]
4643 ["New..." ada-prj-new t]
4644 ["Edit..." ada-prj-edit t])
4645 ("Goto" :included (eq major-mode 'ada-mode)
4646 ["Goto Declaration/Body" ada-goto-declaration
4647 (eq ada-which-compiler 'gnat)]
4648 ["Goto Body" ada-goto-body
4649 (eq ada-which-compiler 'gnat)]
4650 ["Goto Declaration Other Frame"
4651 ada-goto-declaration-other-frame
4652 (eq ada-which-compiler 'gnat)]
4653 ["Goto Previous Reference" ada-xref-goto-previous-reference
4654 (eq ada-which-compiler 'gnat)]
4655 ["List Local References" ada-find-local-references
4656 (eq ada-which-compiler 'gnat)]
4657 ["List References" ada-find-references
4658 (eq ada-which-compiler 'gnat)]
4659 ["Goto Reference To Any Entity" ada-find-any-references
4660 (eq ada-which-compiler 'gnat)]
4661 ["Goto Parent Unit" ada-goto-parent
4662 (eq ada-which-compiler 'gnat)]
4664 ["Next compilation error" next-error t]
4665 ["Previous Package" ada-previous-package t]
4666 ["Next Package" ada-next-package t]
4667 ["Previous Procedure" ada-previous-procedure t]
4668 ["Next Procedure" ada-next-procedure t]
4669 ["Goto Start Of Statement" ada-move-to-start t]
4670 ["Goto End Of Statement" ada-move-to-end t]
4672 ["Other File" ff-find-other-file t]
4673 ["Other File Other Window" ada-ff-other-window t])
4674 ("Edit" :included (eq major-mode 'ada-mode)
4675 ["Search File On Source Path" ada-find-file t]
4677 ["Complete Identifier" ada-complete-identifier t]
4679 ["Indent Line" ada-indent-current-function t]
4680 ["Justify Current Indentation" ada-justified-indent-current t]
4681 ["Indent Lines in Selection" ada-indent-region t]
4682 ["Indent Lines in File"
4683 (ada-indent-region (point-min) (point-max)) t]
4684 ["Format Parameter List" ada-format-paramlist t]
4686 ["Comment Selection" comment-region t]
4687 ["Uncomment Selection" ada-uncomment-region t]
4689 ["Fill Comment Paragraph" fill-paragraph t]
4690 ["Fill Comment Paragraph Justify"
4691 ada-fill-comment-paragraph-justify t]
4692 ["Fill Comment Paragraph Postfix"
4693 ada-fill-comment-paragraph-postfix t]
4695 ["Adjust Case Selection" ada-adjust-case-region t]
4696 ["Adjust Case in File" ada-adjust-case-buffer t]
4697 ["Create Case Exception" ada-create-case-exception t]
4698 ["Create Case Exception Substring"
4699 ada-create-case-exception-substring t]
4700 ["Reload Case Exceptions" ada-case-read-exceptions t]
4702 ["Make body for subprogram" ada-make-subprogram-body t]
4704 ["Narrow to subprogram" ada-narrow-to-defun t])
4706 :included (eq major-mode 'ada-mode)
4707 ["Header" ada-header t]
4709 ["Package Body" ada-package-body t]
4710 ["Package Spec" ada-package-spec t]
4711 ["Function Spec" ada-function-spec t]
4712 ["Procedure Spec" ada-procedure-spec t]
4713 ["Proc/func Body" ada-subprogram-body t]
4714 ["Task Body" ada-task-body t]
4715 ["Task Spec" ada-task-spec t]
4716 ["Declare Block" ada-declare-block t]
4717 ["Exception Block" ada-exception-block t]
4719 ["Entry" ada-entry t]
4720 ["Entry family" ada-entry-family t]
4721 ["Select" ada-select t]
4722 ["Accept" ada-accept t]
4723 ["Or accept" ada-or-accep t]
4724 ["Or delay" ada-or-delay t]
4725 ["Or terminate" ada-or-terminate t]
4728 ["Private" ada-private t]
4729 ["Subtype" ada-subtype t]
4730 ["Record" ada-record t]
4731 ["Array" ada-array t]
4735 ["Elsif" ada-elsif t]
4738 ["While Loop" ada-while-loop t]
4739 ["For Loop" ada-for-loop t]
4742 ["Exception" ada-exception t]
4744 ["When" ada-when t])
4747 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4748 (if (featurep 'xemacs)
4750 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4751 (set 'mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4754 ;; -------------------------------------------------------
4755 ;; Commenting/Uncommenting code
4756 ;; The following two calls are provided to enhance the standard
4757 ;; comment-region function, which only allows uncommenting if the
4758 ;; comment is at the beginning of a line. If the line have been re-indented,
4759 ;; we are unable to use comment-region, which makes no sense.
4761 ;; In addition, we provide an interface to the standard comment handling
4762 ;; function for justifying the comments.
4763 ;; -------------------------------------------------------
4765 (defadvice comment-region (before ada-uncomment-anywhere disable)
4767 (listp arg) ;; a prefix with \C-u is of the form '(4), whereas
4768 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4769 (string= mode-name "Ada"))
4771 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4773 (while (re-search-forward cs end t)
4774 (replace-match comment-start))
4777 (defun ada-uncomment-region (beg end &optional arg)
4778 "Delete `comment-start' at the beginning of a line in the region."
4779 (interactive "r\nP")
4781 ;; This advice is not needed anymore with Emacs21. However, for older
4782 ;; versions, as well as for XEmacs, we still need to enable it.
4783 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4785 (ad-activate 'comment-region)
4786 (comment-region beg end (- (or arg 2)))
4787 (ad-deactivate 'comment-region))
4788 (comment-region beg end (list (- (or arg 2))))
4789 (ada-indent-region beg end)))
4791 (defun ada-fill-comment-paragraph-justify ()
4792 "Fill current comment paragraph and justify each line as well."
4794 (ada-fill-comment-paragraph 'full))
4796 (defun ada-fill-comment-paragraph-postfix ()
4797 "Fill current comment paragraph and justify each line as well.
4798 Adds `ada-fill-comment-postfix' at the end of each line."
4800 (ada-fill-comment-paragraph 'full t))
4802 (defun ada-fill-comment-paragraph (&optional justify postfix)
4803 "Fill the current comment paragraph.
4804 If JUSTIFY is non-nil, each line is justified as well.
4805 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4806 to each line filled and justified.
4807 The paragraph is indented on the first line."
4810 ;; check if inside comment or just in front a comment
4811 (if (and (not (ada-in-comment-p))
4812 (not (looking-at "[ \t]*--")))
4813 (error "Not inside comment"))
4815 (let* (indent from to
4816 (opos (point-marker))
4818 ;; Sets this variable to nil, otherwise it prevents
4819 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4820 (parse-sexp-lookup-properties nil)
4823 (fill-column (current-fill-column)))
4825 ;; Find end of paragraph
4826 (back-to-indentation)
4827 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4830 ;; If we were at the last line in the buffer, create a dummy empty
4831 ;; line at the end of the buffer.
4834 (back-to-indentation)))
4836 (setq to (point-marker))
4839 ;; Find beginning of paragraph
4840 (back-to-indentation)
4841 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4843 (back-to-indentation))
4845 ;; We want one line above the first one, unless we are at the beginning
4850 (setq from (point-marker))
4852 ;; Calculate the indentation we will need for the paragraph
4853 (back-to-indentation)
4854 (setq indent (current-column))
4855 ;; unindent the first line of the paragraph
4856 (delete-region from (point))
4858 ;; Remove the old postfixes
4860 (while (re-search-forward "--\n" to t)
4861 (replace-match "\n"))
4864 (setq to (point-marker))
4866 ;; Indent and justify the paragraph
4867 (setq fill-prefix ada-fill-comment-prefix)
4868 (set-left-margin from to indent)
4870 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4872 (fill-region-as-paragraph from to justify)
4874 ;; Add the postfixes if required
4878 (narrow-to-region from to)
4881 (insert-char ? (- fill-column (current-column)))
4882 (insert ada-fill-comment-postfix)
4886 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4887 ;; inserted at the end. Delete it
4888 (if (or (featurep 'xemacs)
4889 (<= emacs-major-version 19)
4890 (and (= emacs-major-version 20)
4891 (<= emacs-minor-version 2)))
4900 ;; ---------------------------------------------------
4901 ;; support for find-file.el
4902 ;; These functions are used by find-file to guess the file names from
4903 ;; unit names, and to find the other file (spec or body) from the current
4904 ;; file (body or spec).
4905 ;; It is also used to find in which function we are, so as to put the
4906 ;; cursor at the correct position.
4907 ;; Standard Ada does not force any relation between unit names and file names,
4908 ;; so some of these functions can only be a good approximation. However, they
4909 ;; are also overriden in `ada-xref'.el when we know that the user is using
4911 ;; ---------------------------------------------------
4913 ;; Overriden when we work with GNAT, to use gnatkrunch
4914 (defun ada-make-filename-from-adaname (adaname)
4915 "Determine the filename in which ADANAME is found.
4916 This is a generic function, independent from any compiler."
4917 (while (string-match "\\." adaname)
4918 (setq adaname (replace-match "-" t t adaname)))
4922 (defun ada-other-file-name ()
4923 "Return the name of the other file.
4924 The name returned is the body if `current-buffer' is the spec,
4925 or the spec otherwise."
4929 (suffixes ada-spec-suffixes)
4930 (name (buffer-file-name)))
4932 ;; Guess whether we have a spec or a body, and get the basename of the
4933 ;; file. Since the extension may not start with '.', we can not use
4934 ;; file-name-extension
4935 (while (and (not is-spec)
4937 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4939 name (match-string 1 name)))
4940 (setq suffixes (cdr suffixes)))
4944 (setq suffixes ada-body-suffixes)
4945 (while (and (not is-body)
4947 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4949 name (match-string 1 name)))
4950 (setq suffixes (cdr suffixes)))))
4952 ;; If this wasn't in either list, return name itself
4953 (if (not (or is-spec is-body))
4956 ;; Else find the other possible names
4958 (setq suffixes ada-body-suffixes)
4959 (setq suffixes ada-spec-suffixes))
4964 ;; If we are using project file, search for the other file in all
4965 ;; the possible src directories.
4967 (if (fboundp 'ada-find-src-file-in-dir)
4969 (ada-find-src-file-in-dir
4970 (file-name-nondirectory (concat name (car suffixes))))))
4972 (set 'is-spec other)))
4974 ;; Else search in the current directory
4975 (if (file-exists-p (concat name (car suffixes)))
4976 (setq is-spec (concat name (car suffixes)))))
4977 (setq suffixes (cdr suffixes)))
4981 (defun ada-which-function-are-we-in ()
4982 "Return the name of the function whose definition/declaration point is in.
4983 Redefines the function `ff-which-function-are-we-in'."
4984 (setq ff-function-name nil)
4986 (end-of-line);; make sure we get the complete name
4987 (if (or (re-search-backward ada-procedure-start-regexp nil t)
4988 (re-search-backward ada-package-start-regexp nil t))
4989 (setq ff-function-name (match-string 0)))
4993 (defvar ada-last-which-function-line -1
4994 "Last on which `ada-which-function' was called.")
4995 (defvar ada-last-which-function-subprog 0
4996 "Last subprogram name returned by `ada-which-function'.")
4997 (make-variable-buffer-local 'ada-last-which-function-subprog)
4998 (make-variable-buffer-local 'ada-last-which-function-line)
5001 (defun ada-which-function ()
5002 "Return the name of the function whose body the point is in.
5003 This function works even in the case of nested subprograms, whereas the
5004 standard Emacs function `which-function' does not.
5005 Since the search can be long, the results are cached."
5007 (let ((line (count-lines 1 (point)))
5013 ;; If this is the same line as before, simply return the same result
5014 (if (= line ada-last-which-function-line)
5015 ada-last-which-function-subprog
5018 ;; In case the current line is also the beginning of the body
5021 ;; Are we looking at "function Foo\n (paramlist)"
5022 (skip-chars-forward " \t\n(")
5028 (skip-chars-forward " \t\n")
5029 (if (looking-at "return")
5032 (skip-chars-forward " \t\n")
5033 (skip-chars-forward "a-zA-Z0-9_'")))
5035 ;; Can't simply do forward-word, in case the "is" is not on the
5036 ;; same line as the closing parenthesis
5037 (skip-chars-forward "is \t\n")
5039 ;; No look for the closest subprogram body that has not ended yet.
5040 ;; Not that we expect all the bodies to be finished by "end <name>",
5041 ;; or a simple "end;" indented in the same column as the start of
5042 ;; the subprogram. The goal is to be as efficient as possible.
5044 (while (and (not found)
5045 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5047 ;; Get the function name, but not the properties, or this changes
5048 ;; the face in the modeline on Emacs 21
5049 (setq func-name (match-string-no-properties 2))
5050 (if (and (not (ada-in-comment-p))
5051 (not (save-excursion
5052 (goto-char (match-end 0))
5053 (looking-at "[ \t\n]*new"))))
5055 (back-to-indentation)
5056 (setq indent (current-column))
5057 (if (ada-search-ignore-string-comment
5058 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5059 (make-string indent ? ) "end;"))
5060 (setq end-pos (point))
5061 (setq end-pos (point-max)))
5062 (if (>= end-pos pos)
5063 (setq found func-name))))
5065 (setq ada-last-which-function-line line
5066 ada-last-which-function-subprog found)
5069 (defun ada-ff-other-window ()
5070 "Find other file in other window using `ff-find-other-file'."
5072 (and (fboundp 'ff-find-other-file)
5073 (ff-find-other-file t)))
5075 (defun ada-set-point-accordingly ()
5076 "Move to the function declaration that was set by
5077 `ff-which-function-are-we-in'."
5078 (if ff-function-name
5080 (goto-char (point-min))
5081 (unless (ada-search-ignore-string-comment
5082 (concat ff-function-name "\\b") nil)
5083 (goto-char (point-min))))))
5085 (defun ada-get-body-name (&optional spec-name)
5086 "Return the file name for the body of SPEC-NAME.
5087 If SPEC-NAME is nil, return the body for the current package.
5088 Returns nil if no body was found."
5091 (unless spec-name (setq spec-name (buffer-file-name)))
5093 ;; Remove the spec extension. We can not simply remove the file extension,
5094 ;; but we need to take into account the specific non-GNAT extensions that the
5095 ;; user might have specified.
5097 (let ((suffixes ada-spec-suffixes)
5100 (setq end (- (length spec-name) (length (car suffixes))))
5101 (if (string-equal (car suffixes) (substring spec-name end))
5102 (setq spec-name (substring spec-name 0 end)))
5103 (setq suffixes (cdr suffixes))))
5105 ;; If find-file.el was available, use its functions
5106 (if (fboundp 'ff-get-file-name)
5107 (ff-get-file-name ada-search-directories-internal
5108 (ada-make-filename-from-adaname
5109 (file-name-nondirectory
5110 (file-name-sans-extension spec-name)))
5112 ;; Else emulate it very simply
5113 (concat (ada-make-filename-from-adaname
5114 (file-name-nondirectory
5115 (file-name-sans-extension spec-name)))
5119 ;; ---------------------------------------------------
5120 ;; support for font-lock.el
5121 ;; Strings are a real pain in Ada because a single quote character is
5122 ;; overloaded as a string quote and type/instance delimiter. By default, a
5123 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5124 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5125 ;; syntax when the gods that created Ada determine them to be.
5127 ;; This only works in Emacs. See the comments before the grammar functions
5128 ;; at the beginning of this file for how this is done with XEmacs.
5129 ;; ----------------------------------------------------
5131 (defconst ada-font-lock-syntactic-keywords
5132 ;; Mark single quotes as having string quote syntax in 'c' instances.
5133 ;; As a special case, ''' will not be highlighted, but if we do not
5134 ;; set this special case, then the rest of the buffer is highlighted as
5136 ;; This sets the properties of the characters, so that ada-in-string-p
5137 ;; correctly handles '"' too...
5138 '(("[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5139 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))
5142 (defvar ada-font-lock-keywords
5146 ;; handle "type T is access function return S;"
5147 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5149 ;; preprocessor line
5150 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5153 ;; accept, entry, function, package (body), protected (body|type),
5154 ;; pragma, procedure, task (body) plus name.
5160 "package[ \t]+body\\|"
5164 "protected[ \t]+body\\|"
5165 "protected[ \t]+type\\|"
5171 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5172 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5174 ;; Optional keywords followed by a type name.
5175 (list (concat ; ":[ \t]*"
5176 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5178 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5179 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5182 ;; Main keywords, except those treated specially below.
5185 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5186 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5187 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5188 "generic" "if" "in" "is" "limited" "loop" "mod" "not"
5189 "null" "or" "others" "private" "protected" "raise"
5190 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5191 "select" "separate" "tagged" "task" "terminate" "then" "until"
5192 "when" "while" "with" "xor") t)
5195 ;; Anything following end and not already fontified is a body name.
5196 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5197 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5199 ;; Keywords followed by a type or function name.
5200 (list (concat "\\<\\("
5201 "new\\|of\\|subtype\\|type"
5202 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5203 '(1 font-lock-keyword-face)
5204 '(2 (if (match-beginning 4)
5205 font-lock-function-name-face
5206 font-lock-type-face) nil t))
5208 ;; Keywords followed by a (comma separated list of) reference.
5209 ;; Note that font-lock only works on single lines, thus we can not
5210 ;; correctly highlight a with_clause that spans multiple lines.
5211 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5212 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5213 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
5217 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
5219 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5220 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5222 ;; Ada unnamed numerical constants
5223 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5226 "Default expressions to highlight in Ada mode.")
5229 ;; ---------------------------------------------------------
5230 ;; Support for outline.el
5231 ;; ---------------------------------------------------------
5233 (defun ada-outline-level ()
5234 "This is so that `current-column' DTRT in otherwise-hidden text."
5235 ;; patch from Dave Love <fx@gnu.org>
5236 (let (buffer-invisibility-spec)
5238 (back-to-indentation)
5241 ;; ---------------------------------------------------------
5242 ;; Support for narrow-to-region
5243 ;; ---------------------------------------------------------
5245 (defun ada-narrow-to-defun (&optional arg)
5246 "Make text outside current subprogram invisible.
5247 The subprogram visible is the one that contains or follow point.
5248 Optional ARG is ignored.
5249 Use `M-x widen' to go back to the full visibility for the buffer."
5256 (ada-previous-procedure)
5264 (narrow-to-region end (point))
5266 "Use M-x widen to get back to full visibility in the buffer"))))
5268 ;; ---------------------------------------------------------
5269 ;; Automatic generation of code
5270 ;; The Ada-mode has a set of function to automatically generate a subprogram
5271 ;; or package body from its spec.
5272 ;; These function only use a primary and basic algorithm, this could use a
5273 ;; lot of improvement.
5274 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5276 ;; ----------------------------------------------------------
5278 (defun ada-gen-treat-proc (match)
5279 "Make dummy body of a procedure/function specification.
5280 MATCH is a cons cell containing the start and end locations of the last search
5281 for `ada-procedure-start-regexp'."
5282 (goto-char (car match))
5283 (let (func-found procname functype)
5285 ((or (looking-at "^[ \t]*procedure")
5286 (setq func-found (looking-at "^[ \t]*function")))
5287 ;; treat it as a proc/func
5290 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5292 ;; goto end of procname
5293 (goto-char (cdr match))
5295 ;; skip over parameterlist
5296 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5299 ;; if function, skip over 'return' and result type.
5303 (skip-chars-forward " \t\n")
5304 (setq functype (buffer-substring (point)
5309 ;; look for next non WS
5311 ((looking-at "[ \t]*;")
5312 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5313 (ada-indent-newline-indent)
5315 (ada-indent-newline-indent)
5318 (insert "Result : " functype ";")
5319 (ada-indent-newline-indent)))
5321 (ada-indent-newline-indent)
5323 (insert "return Result;")
5325 (ada-indent-newline-indent)
5326 (insert "end " procname ";")
5327 (ada-indent-newline-indent)
5330 ((looking-at "[ \t\n]*is")
5333 ((looking-at "[ \t\n]*rename")
5337 (message "unknown syntax"))))
5339 (if (looking-at "^[ \t]*task")
5341 (message "Task conversion is not yet implemented")
5343 (if (looking-at "[ \t]*;")
5348 (defun ada-make-body ()
5349 "Create an Ada package body in the current buffer.
5350 The potential old buffer contents is deleted first, then we copy the
5351 spec buffer in here and modify it to make it a body.
5352 This function typically is to be hooked into `ff-file-created-hooks'."
5354 (delete-region (point-min) (point-max))
5355 (insert-buffer-substring (car (cdr (buffer-list))))
5356 (goto-char (point-min))
5359 (let (found ada-procedure-or-package-start-regexp)
5361 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5362 (progn (goto-char (cdr found))
5365 (error "No package"))
5367 (setq ada-procedure-or-package-start-regexp
5368 (concat ada-procedure-start-regexp
5370 ada-package-start-regexp))
5373 (ada-search-ignore-string-comment
5374 ada-procedure-or-package-start-regexp nil))
5376 (goto-char (car found))
5377 (if (looking-at ada-package-start-regexp)
5378 (progn (goto-char (cdr found))
5380 (ada-gen-treat-proc found))))))
5383 (defun ada-make-subprogram-body ()
5384 "Make one dummy subprogram body from spec surrounding point."
5386 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5387 (spec (match-beginning 0))
5392 (if (and (re-search-forward "(\\|;" nil t)
5393 (= (char-before) ?\())
5395 (ada-search-ignore-string-comment ")" nil)
5396 (ada-search-ignore-string-comment ";" nil)))
5397 (setq spec (buffer-substring spec (point)))
5399 ;; If find-file.el was available, use its functions
5400 (setq body-file (ada-get-body-name))
5402 (find-file body-file)
5403 (error "No body found for the package. Create it first"))
5407 (goto-char (point-max))
5408 (forward-comment -10000)
5409 (re-search-backward "\\<end\\>" nil t)
5410 ;; Move to the beginning of the elaboration part, if any
5411 (re-search-backward "^begin" nil t)
5415 (re-search-backward ada-procedure-start-regexp nil t)
5416 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5418 (error "Not in subprogram spec"))))
5420 ;; --------------------------------------------------------
5421 ;; Global initializations
5422 ;; --------------------------------------------------------
5424 ;; Create the keymap once and for all. If we do that in ada-mode,
5425 ;; the keys changed in the user's .emacs have to be modified
5430 ;; Create the syntax tables, but do not activate them
5431 (ada-create-syntax-table)
5433 ;; Add the default extensions (and set up speedbar)
5434 (ada-add-extensions ".ads" ".adb")
5435 ;; This two files are generated by GNAT when running with -gnatD
5436 (if (equal ada-which-compiler 'gnat)
5437 (ada-add-extensions ".ads.dg" ".adb.dg"))
5439 ;; Read the special cases for exceptions
5440 (ada-case-read-exceptions)
5442 ;; Setup auto-loading of the other ada-mode files.
5443 (if (equal ada-which-compiler 'gnat)
5445 (autoload 'ada-change-prj "ada-xref" nil t)
5446 (autoload 'ada-check-current "ada-xref" nil t)
5447 (autoload 'ada-compile-application "ada-xref" nil t)
5448 (autoload 'ada-compile-current "ada-xref" nil t)
5449 (autoload 'ada-complete-identifier "ada-xref" nil t)
5450 (autoload 'ada-find-file "ada-xref" nil t)
5451 (autoload 'ada-find-any-references "ada-xref" nil t)
5452 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5453 (autoload 'ada-find-local-references "ada-xref" nil t)
5454 (autoload 'ada-find-references "ada-xref" nil t)
5455 (autoload 'ada-gdb-application "ada-xref" nil t)
5456 (autoload 'ada-goto-declaration "ada-xref" nil t)
5457 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5458 (autoload 'ada-goto-parent "ada-xref" nil t)
5459 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5460 (autoload 'ada-point-and-xref "ada-xref" nil t)
5461 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5462 (autoload 'ada-run-application "ada-xref" nil t)
5463 (autoload 'ada-set-default-project-file "ada-xref" nil nil)
5464 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5465 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5467 (autoload 'ada-customize "ada-prj" nil t)
5468 (autoload 'ada-prj-edit "ada-prj" nil t)
5469 (autoload 'ada-prj-new "ada-prj" nil t)
5470 (autoload 'ada-prj-save "ada-prj" nil t)
5473 (autoload 'ada-array "ada-stmt" nil t)
5474 (autoload 'ada-case "ada-stmt" nil t)
5475 (autoload 'ada-declare-block "ada-stmt" nil t)
5476 (autoload 'ada-else "ada-stmt" nil t)
5477 (autoload 'ada-elsif "ada-stmt" nil t)
5478 (autoload 'ada-exception "ada-stmt" nil t)
5479 (autoload 'ada-exception-block "ada-stmt" nil t)
5480 (autoload 'ada-exit "ada-stmt" nil t)
5481 (autoload 'ada-for-loop "ada-stmt" nil t)
5482 (autoload 'ada-function-spec "ada-stmt" nil t)
5483 (autoload 'ada-header "ada-stmt" nil t)
5484 (autoload 'ada-if "ada-stmt" nil t)
5485 (autoload 'ada-loop "ada-stmt" nil t)
5486 (autoload 'ada-package-body "ada-stmt" nil t)
5487 (autoload 'ada-package-spec "ada-stmt" nil t)
5488 (autoload 'ada-private "ada-stmt" nil t)
5489 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5490 (autoload 'ada-record "ada-stmt" nil t)
5491 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5492 (autoload 'ada-subtype "ada-stmt" nil t)
5493 (autoload 'ada-tabsize "ada-stmt" nil t)
5494 (autoload 'ada-task-body "ada-stmt" nil t)
5495 (autoload 'ada-task-spec "ada-stmt" nil t)
5496 (autoload 'ada-type "ada-stmt" nil t)
5497 (autoload 'ada-use "ada-stmt" nil t)
5498 (autoload 'ada-when "ada-stmt" nil t)
5499 (autoload 'ada-while-loop "ada-stmt" nil t)
5500 (autoload 'ada-with "ada-stmt" nil t)
5502 ;;; provide ourselves
5505 ;;; arch-tag: 1b7d45ec-1698-43b5-8d4a-e479ea023270
5506 ;;; ada-mode.el ends here