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, 2006 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: Stephen Leake <stephen_leake@member.fsf.org>
10 ;; Keywords: languages ada
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
30 ;;; This mode is a major mode for editing Ada83 and Ada95 source code.
31 ;;; This is a major rewrite of the file packaged with Emacs-20. The
32 ;;; ada-mode is composed of four Lisp files, ada-mode.el, ada-xref.el,
33 ;;; ada-prj.el and ada-stmt.el. Only this file (ada-mode.el) is
34 ;;; completely independent from the GNU Ada compiler Gnat, distributed
35 ;;; by Ada Core Technologies. All the other files rely heavily on
36 ;;; features provided only by Gnat.
38 ;;; Note: this mode will not work with Emacs 19. If you are on a VMS
39 ;;; system, where the latest version of Emacs is 19.28, you will need
40 ;;; another file, called ada-vms.el, that provides some required
44 ;;; Emacs should enter Ada mode automatically when you load an Ada file.
45 ;;; By default, the valid extensions for Ada files are .ads, .adb or .ada
46 ;;; If the ada-mode does not start automatically, then simply type the
47 ;;; following command :
50 ;;; By default, ada-mode is configured to take full advantage of the GNAT
51 ;;; compiler (the menus will include the cross-referencing features,...).
52 ;;; If you are using another compiler, you might want to set the following
53 ;;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
55 ;;; (setq ada-which-compiler 'generic)
57 ;;; This mode requires find-file.el to be present on your system.
60 ;;; The first Ada mode for GNU Emacs was written by V. Broman in
61 ;;; 1985. He based his work on the already existing Modula-2 mode.
62 ;;; This was distributed as ada.el in versions of Emacs prior to 19.29.
64 ;;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
65 ;;; several files with support for dired commands and other nice
66 ;;; things. It is currently available from the PAL
67 ;;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
69 ;;; The probably very first Ada mode (called electric-ada.el) was
70 ;;; written by Steven D. Litvintchouk and Steven M. Rosen for the
71 ;;; Gosling Emacs. L. Slater based his development on ada.el and
74 ;;; A complete rewrite by M. Heritsch and R. Ebert has been done.
75 ;;; Some ideas from the Ada mode mailing list have been
76 ;;; added. Some of the functionality of L. Slater's mode has not
77 ;;; (yet) been recoded in this new mode. Perhaps you prefer sticking
80 ;;; A complete rewrite for Emacs-20 / Gnat-3.11 has been done by Ada Core
84 ;;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
85 ;;; many patches included in this package.
86 ;;; Christian Egli <Christian.Egli@hcsd.hac.com>:
87 ;;; ada-imenu-generic-expression
88 ;;; Many thanks also to the following persons that have contributed
90 ;;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
91 ;;; woodruff@stc.llnl.gov (John Woodruff)
92 ;;; jj@ddci.dk (Jesper Joergensen)
93 ;;; gse@ocsystems.com (Scott Evans)
94 ;;; comar@gnat.com (Cyrille Comar)
95 ;;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
96 ;;; robin-reply@reagans.org
97 ;;; and others for their valuable hints.
100 ;;; Note: Every function in this package is compiler-independent.
101 ;;; The names start with ada-
102 ;;; The variables that the user can edit can all be modified through
103 ;;; the customize mode. They are sorted in alphabetical order in this
106 ;;; Supported packages.
107 ;;; This package supports a number of other Emacs modes. These other modes
108 ;;; should be loaded before the ada-mode, which will then setup some variables
109 ;;; to improve the support for Ada code.
110 ;;; Here is the list of these modes:
111 ;;; `which-function-mode': Display the name of the subprogram the cursor is
112 ;;; in in the mode line.
113 ;;; `outline-mode': Provides the capability to collapse or expand the code
114 ;;; for specific language constructs, for instance if you want to hide the
115 ;;; code corresponding to a subprogram
116 ;;; `align': This mode is now provided with Emacs 21, but can also be
117 ;;; installed manually for older versions of Emacs. It provides the
118 ;;; capability to automatically realign the selected region (for instance
119 ;;; all ':=', ':' and '--' will be aligned on top of each other.
120 ;;; `imenu': Provides a menu with the list of entities defined in the current
121 ;;; buffer, and an easy way to jump to any of them
122 ;;; `speedbar': Provides a separate file browser, and the capability for each
123 ;;; file to see the list of entities defined in it and to jump to them
125 ;;; `abbrev-mode': Provides the capability to define abbreviations, which
126 ;;; are automatically expanded when you type them. See the Emacs manual.
129 ;; ispell searches for the ispell executable when loaded; may not exist on some systems
130 (require 'ispell nil t)
133 (require 'find-file nil t)
134 (require 'align nil t)
135 (require 'which-func nil t)
136 (require 'compile nil t)
138 (defvar compile-auto-highlight)
139 (defvar skeleton-further-elements)
141 ;; this function is needed at compile time
143 (defun ada-check-emacs-version (major minor &optional is-xemacs)
144 "Return t if Emacs's version is greater or equal to MAJOR.MINOR.
145 If IS-XEMACS is non-nil, check for XEmacs instead of Emacs."
146 (let ((xemacs-running (or (string-match "Lucid" emacs-version)
147 (string-match "XEmacs" emacs-version))))
148 (and (or (and is-xemacs xemacs-running)
149 (not (or is-xemacs xemacs-running)))
150 (or (> emacs-major-version major)
151 (and (= emacs-major-version major)
152 (>= emacs-minor-version minor)))))))
154 (defun ada-mode-version ()
155 "Return Ada mode version."
157 (let ((version-string "3.6w"))
159 (message version-string)
162 (defvar ada-mode-hook nil
163 "*List of functions to call when Ada mode is invoked.
164 This hook is automatically executed after the `ada-mode' is
166 This is a good place to add Ada environment specific bindings.")
169 "Major mode for editing and compiling Ada source in Emacs."
170 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
173 (defcustom ada-auto-case t
174 "*Non-nil means automatically change case of preceding word while typing.
175 Casing is done according to `ada-case-keyword', `ada-case-identifier'
176 and `ada-case-attribute'."
177 :type 'boolean :group 'ada)
179 (defcustom ada-broken-decl-indent 0
180 "*Number of columns to indent a broken declaration.
186 :type 'integer :group 'ada)
188 (defcustom ada-broken-indent 2
189 "*Number of columns to indent the continuation of a broken line.
192 My_Var : My_Type := (Field1 =>
194 :type 'integer :group 'ada)
196 (defcustom ada-continuation-indent ada-broken-indent
197 "*Number of columns to indent the continuation of broken lines in parenthesis.
202 :type 'integer :group 'ada)
204 (defcustom ada-case-attribute 'ada-capitalize-word
205 "*Function to call to adjust the case of Ada attributes.
206 It may be `downcase-word', `upcase-word', `ada-loose-case-word',
207 `ada-capitalize-word' or `ada-no-auto-case'."
208 :type '(choice (const downcase-word)
210 (const ada-capitalize-word)
211 (const ada-loose-case-word)
212 (const ada-no-auto-case))
215 (defcustom ada-case-exception-file
216 (list (convert-standard-filename' "~/.emacs_case_exceptions"))
217 "*List of special casing exceptions dictionaries for identifiers.
218 The first file is the one where new exceptions will be saved by Emacs
219 when you call `ada-create-case-exception'.
221 These files should contain one word per line, that gives the casing
222 to be used for that word in Ada files. If the line starts with the
223 character *, then the exception will be used for substrings that either
224 start at the beginning of a word or after a _ character, and end either
225 at the end of the word or at a _ character. Each line can be terminated
227 :type '(repeat (file))
230 (defcustom ada-case-keyword 'downcase-word
231 "*Function to call to adjust the case of an Ada keywords.
232 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
233 `ada-capitalize-word'."
234 :type '(choice (const downcase-word)
236 (const ada-capitalize-word)
237 (const ada-loose-case-word)
238 (const ada-no-auto-case))
241 (defcustom ada-case-identifier 'ada-loose-case-word
242 "*Function to call to adjust the case of an Ada identifier.
243 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
244 `ada-capitalize-word'."
245 :type '(choice (const downcase-word)
247 (const ada-capitalize-word)
248 (const ada-loose-case-word)
249 (const ada-no-auto-case))
252 (defcustom ada-clean-buffer-before-saving t
253 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
254 :type 'boolean :group 'ada)
256 (defcustom ada-indent 3
257 "*Size of Ada indentation.
263 :type 'integer :group 'ada)
265 (defcustom ada-indent-after-return t
266 "*Non-nil means automatically indent after RET or LFD."
267 :type 'boolean :group 'ada)
269 (defcustom ada-indent-align-comments t
270 "*Non-nil means align comments on previous line comments, if any.
271 If nil, indentation is calculated as usual.
272 Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
275 A := 1; -- A multi-line comment
276 -- aligned if ada-indent-align-comments is t"
277 :type 'boolean :group 'ada)
279 (defcustom ada-indent-comment-as-code t
280 "*Non-nil means indent comment lines as code.
281 A nil value means do not auto-indent comments."
282 :type 'boolean :group 'ada)
284 (defcustom ada-indent-handle-comment-special nil
285 "*Non-nil if comment lines should be handled specially inside parenthesis.
286 By default, if the line that contains the open parenthesis has some
287 text following it, then the following lines will be indented in the
288 same column as this text. This will not be true if the first line is
289 a comment and `ada-indent-handle-comment-special' is t.
292 ( Value_1, -- common behavior, when not a comment
296 ( -- `ada-indent-handle-comment-special' is nil
301 ( -- `ada-indent-handle-comment-special' is non-nil
304 :type 'boolean :group 'ada)
306 (defcustom ada-indent-is-separate t
307 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
308 :type 'boolean :group 'ada)
310 (defcustom ada-indent-record-rel-type 3
311 "*Indentation for 'record' relative to 'type' or 'use'.
316 :type 'integer :group 'ada)
318 (defcustom ada-indent-renames ada-broken-indent
319 "*Indentation for renames relative to the matching function statement.
320 If `ada-indent-return' is null or negative, the indentation is done relative to
321 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
324 function A (B : Integer)
327 :type 'integer :group 'ada)
329 (defcustom ada-indent-return 0
330 "*Indentation for 'return' relative to the matching 'function' statement.
331 If `ada-indent-return' is null or negative, the indentation is done relative to
332 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
335 function A (B : Integer)
337 :type 'integer :group 'ada)
339 (defcustom ada-indent-to-open-paren t
340 "*Non-nil means indent according to the innermost open parenthesis."
341 :type 'boolean :group 'ada)
343 (defcustom ada-fill-comment-prefix "-- "
344 "*Text inserted in the first columns when filling a comment paragraph.
345 Note: if you modify this variable, you will have to invoke `ada-mode'
346 again to take account of the new value."
347 :type 'string :group 'ada)
349 (defcustom ada-fill-comment-postfix " --"
350 "*Text inserted at the end of each line when filling a comment paragraph.
351 Used by `ada-fill-comment-paragraph-postfix'."
352 :type 'string :group 'ada)
354 (defcustom ada-label-indent -4
355 "*Number of columns to indent a label.
362 This is also used for <<..>> labels"
363 :type 'integer :group 'ada)
365 (defcustom ada-language-version 'ada95
366 "*Ada language version; one of `ada83', `ada95', `ada05'."
367 :type '(choice (const ada83) (const ada95) (const ada05)) :group 'ada)
369 (defcustom ada-move-to-declaration nil
370 "*Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to 'begin'."
371 :type 'boolean :group 'ada)
373 (defcustom ada-popup-key '[down-mouse-3]
374 "*Key used for binding the contextual menu.
375 If nil, no contextual menu is available."
376 :type '(restricted-sexp :match-alternatives (stringp vectorp))
379 (defcustom ada-search-directories
381 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
382 '("/usr/adainclude" "/usr/local/adainclude"
383 "/opt/gnu/adainclude"))
384 "*Default list of directories to search for Ada files.
385 See the description for the `ff-search-directories' variable. This variable
386 is the initial value of `ada-search-directories-internal'."
387 :type '(repeat (choice :tag "Directory"
388 (const :tag "default" nil)
389 (directory :format "%v")))
392 (defvar ada-search-directories-internal ada-search-directories
393 "Internal version of `ada-search-directories'.
394 Its value is the concatenation of the search path as read in the project file
395 and the standard runtime location, and the value of the user-defined
396 `ada-search-directories'.")
398 (defcustom ada-stmt-end-indent 0
399 "*Number of columns to indent the end of a statement on a separate line.
404 :type 'integer :group 'ada)
406 (defcustom ada-tab-policy 'indent-auto
407 "*Control the behavior of the TAB key.
409 `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
410 `indent-auto' : use indentation functions in this file.
411 `always-tab' : do `indent-relative'."
412 :type '(choice (const indent-auto)
413 (const indent-rigidly)
417 (defcustom ada-use-indent ada-broken-indent
418 "*Indentation for the lines in a 'use' statement.
423 :type 'integer :group 'ada)
425 (defcustom ada-when-indent 3
426 "*Indentation for 'when' relative to 'exception' or 'case'.
431 :type 'integer :group 'ada)
433 (defcustom ada-with-indent ada-broken-indent
434 "*Indentation for the lines in a 'with' statement.
439 :type 'integer :group 'ada)
441 (defcustom ada-which-compiler 'gnat
442 "*Name of the compiler to use.
443 This will determine what features are made available through the Ada mode.
444 The possible choices are:
445 `gnat': Use Ada Core Technologies' Gnat compiler. Add some cross-referencing
447 `generic': Use a generic compiler."
448 :type '(choice (const gnat)
453 ;;; ---- end of user configurable variables
456 (defvar ada-body-suffixes '(".adb")
457 "List of possible suffixes for Ada body files.
458 The extensions should include a `.' if needed.")
460 (defvar ada-spec-suffixes '(".ads")
461 "List of possible suffixes for Ada spec files.
462 The extensions should include a `.' if needed.")
464 (defvar ada-mode-menu (make-sparse-keymap "Ada")
465 "Menu for Ada mode.")
467 (defvar ada-mode-map (make-sparse-keymap)
468 "Local keymap used for Ada mode.")
470 (defvar ada-mode-abbrev-table nil
471 "Local abbrev table for Ada mode.")
473 (defvar ada-mode-syntax-table nil
474 "Syntax table to be used for editing Ada source code.")
476 (defvar ada-mode-symbol-syntax-table nil
477 "Syntax table for Ada, where `_' is a word constituent.")
480 (defconst ada-83-string-keywords
481 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
482 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
483 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
484 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
485 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
486 "procedure" "raise" "range" "record" "rem" "renames" "return"
487 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
488 "type" "use" "when" "while" "with" "xor")
489 "List of Ada 83 keywords.
490 Used to define `ada-*-keywords'."))
493 (defconst ada-95-string-keywords
494 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
495 "List of keywords new in Ada 95.
496 Used to define `ada-*-keywords'."))
499 (defconst ada-2005-string-keywords
500 '("interface" "overriding" "synchronized")
501 "List of keywords new in Ada 2005.
502 Used to define `ada-*-keywords.'"))
504 (defvar ada-ret-binding nil
505 "Variable to save key binding of RET when casing is activated.")
507 (defvar ada-case-exception '()
508 "Alist of words (entities) that have special casing.")
510 (defvar ada-case-exception-substring '()
511 "Alist of substrings (entities) that have special casing.
512 The substrings are detected for word constituant when the word
513 is not itself in `ada-case-exception', and only for substrings that
514 either are at the beginning or end of the word, or start after '_'.")
516 (defvar ada-lfd-binding nil
517 "Variable to save key binding of LFD when casing is activated.")
519 (defvar ada-other-file-alist nil
520 "Variable used by `find-file' to find the name of the other package.
521 See `ff-other-file-alist'.")
523 (defvar ada-align-list
524 '(("[^:]\\(\\s-*\\):[^:]" 1 t)
525 ("[^=]\\(\\s-+\\)=[^=]" 1 t)
526 ("\\(\\s-*\\)use\\s-" 1)
528 "Ada support for align.el <= 2.2.
529 This variable provides regular expressions on which to align different lines.
530 See `align-mode-alist' for more information.")
532 (defvar ada-align-modes
534 (regexp . "[^:]\\(\\s-*\\):[^:]")
535 (valid . (lambda() (not (ada-in-comment-p))))
536 (modes . '(ada-mode)))
538 (regexp . "[^=]\\(\\s-+\\)=[^=]")
539 (valid . (lambda() (not (ada-in-comment-p))))
540 (modes . '(ada-mode)))
542 (regexp . "\\(\\s-*\\)--")
543 (modes . '(ada-mode)))
545 (regexp . "\\(\\s-*\\)use\\s-")
546 (valid . (lambda() (not (ada-in-comment-p))))
547 (modes . '(ada-mode)))
549 "Ada support for align.el >= 2.8.
550 This variable defines several rules to use to align different lines.")
552 (defconst ada-align-region-separate
571 "See the variable `align-region-separate' for more information.")
573 ;;; ---- Below are the regexp used in this package for parsing
575 (defconst ada-83-keywords
577 (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
578 "Regular expression matching Ada83 keywords.")
580 (defconst ada-95-keywords
582 (concat "\\<" (regexp-opt
584 ada-95-string-keywords
585 ada-83-string-keywords) t) "\\>"))
586 "Regular expression matching Ada95 keywords.")
588 (defconst ada-2005-keywords
590 (concat "\\<" (regexp-opt
592 ada-2005-string-keywords
593 ada-83-string-keywords
594 ada-95-string-keywords) t) "\\>"))
595 "Regular expression matching Ada2005 keywords.")
597 (defvar ada-keywords ada-2005-keywords
598 "Regular expression matching Ada keywords.")
599 ;; FIXME: make this customizable
601 (defconst ada-ident-re
603 "Regexp matching Ada (qualified) identifiers.")
605 ;; "with" needs to be included in the regexp, to match generic subprogram parameters
606 ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
607 (defvar ada-procedure-start-regexp
609 "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
611 ;; subprogram name: operator ("[+/=*]")
615 ;; subprogram name: name
617 "\\(\\(\\sw\\|[_.]\\)+\\)"
619 "Regexp matching Ada subprogram start.
620 The actual start is at (match-beginning 4). The name is in (match-string 5).")
622 (defconst ada-name-regexp
623 "\\([a-zA-Z][a-zA-Z0-9_\\.\\']*[a-zA-Z0-9]\\)"
624 "Regexp matching a fully qualified name (including attribute).")
626 (defconst ada-package-start-regexp
627 (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp)
628 "Regexp matching start of package.
629 The package name is in (match-string 4).")
631 ;;; ---- regexps for indentation functions
633 (defvar ada-block-start-re
635 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
636 "exception" "generic" "loop" "or"
637 "private" "select" ))
638 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
639 "Regexp for keywords starting Ada blocks.")
641 (defvar ada-end-stmt-re
646 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
647 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
648 "loop" "private" "record" "select"
649 "then abort" "then") t) "\\>" "\\|"
650 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
651 t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
652 "^[ \t]*exception\\>"
654 "Regexp of possible ends for a non-broken statement.
655 A new statement starts after these.")
657 (defvar ada-matching-start-re
661 '("end" "loop" "select" "begin" "case" "do"
662 "if" "task" "package" "record" "protected") t)
664 "Regexp used in `ada-goto-matching-start'.")
666 (defvar ada-matching-decl-start-re
670 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
672 "Regexp used in `ada-goto-matching-decl-start'.")
674 (defvar ada-loop-start-re
675 "\\<\\(for\\|while\\|loop\\)\\>"
676 "Regexp for the start of a loop.")
678 (defvar ada-subprog-start-re
680 (concat "\\<" (regexp-opt '("accept" "entry" "function" "package" "procedure"
681 "protected" "task") t) "\\>"))
682 "Regexp for the start of a subprogram.")
684 (defvar ada-named-block-re
685 "[ \t]*\\(\\sw\\|_\\)+[ \t]*:[^=]"
686 "Regexp of the name of a block or loop.")
688 (defvar ada-contextual-menu-on-identifier nil
689 "Set to true when the right mouse button was clicked on an identifier.")
691 (defvar ada-contextual-menu-last-point nil
692 "Position of point just before displaying the menu.
693 This is a list (point buffer).
694 Since `ada-popup-menu' moves the point where the user clicked, the region
695 is modified. Therefore no command from the menu knows what the user selected
696 before displaying the contextual menu.
697 To get the original region, restore the point to this position before
698 calling `region-end' and `region-beginning'.
699 Modify this variable if you want to restore the point to another position.")
701 (easy-menu-define ada-contextual-menu nil
702 "Menu to use when the user presses the right mouse button.
703 The variable `ada-contextual-menu-on-identifier' will be set to t before
704 displaying the menu if point was on an identifier."
706 ["Goto Declaration/Body" ada-point-and-xref
707 :included ada-contextual-menu-on-identifier]
708 ["Goto Body" ada-point-and-xref-body
709 :included ada-contextual-menu-on-identifier]
710 ["Goto Previous Reference" ada-xref-goto-previous-reference]
711 ["List References" ada-find-references
712 :included ada-contextual-menu-on-identifier]
713 ["List Local References" ada-find-local-references
714 :included ada-contextual-menu-on-identifier]
716 ["Other File" ff-find-other-file]
717 ["Goto Parent Unit" ada-goto-parent]))
720 ;;------------------------------------------------------------------
721 ;; Support for imenu (see imenu.el)
722 ;;------------------------------------------------------------------
724 (defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
726 (defconst ada-imenu-subprogram-menu-re
727 (concat "^[ \t]*\\(procedure\\|function\\)[ \t\n]+"
728 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
731 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
733 (defvar ada-imenu-generic-expression
735 (list nil ada-imenu-subprogram-menu-re 2)
738 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
740 "\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
741 ada-imenu-comment-re "\\)";; parameter list or simple space
742 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
744 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
745 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
747 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
748 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
749 "Imenu generic expression for Ada mode.
750 See `imenu-generic-expression'. This variable will create several submenus for
751 each type of entity that can be found in an Ada file.")
754 ;;------------------------------------------------------------
755 ;; Support for compile.el
756 ;;------------------------------------------------------------
758 (defun ada-compile-mouse-goto-error ()
759 "Mouse interface for `ada-compile-goto-error'."
761 (mouse-set-point last-input-event)
762 (ada-compile-goto-error (point))
765 (defun ada-compile-goto-error (pos)
766 "Replace `compile-goto-error' from compile.el.
767 If POS is on a file and line location, go to this position. It adds
768 to compile.el the capacity to go to a reference in an error message.
769 For instance, on these lines:
770 foo.adb:61:11: [...] in call to size declared at foo.ads:11
771 foo.adb:61:11: [...] in call to local declared at line 20
772 the 4 file locations can be clicked on and jumped to."
776 (skip-chars-backward "-a-zA-Z0-9_:./\\")
778 ;; special case: looking at a filename:line not at the beginning of a line
781 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"))
782 (let ((line (match-string 2))
784 (error-pos (point-marker))
789 ;; Use funcall so as to prevent byte-compiler warnings
790 ;; `ada-find-file' is not defined if ada-xref wasn't loaded. But
791 ;; if we can find it, we should use it instead of
792 ;; `compilation-find-file', since the latter doesn't know anything
793 ;; about source path.
795 (if (functionp 'ada-find-file)
796 (setq file (funcall (symbol-function 'ada-find-file)
798 (setq file (funcall (symbol-function 'compilation-find-file)
799 (point-marker) (match-string 1)
804 (goto-line (string-to-number line)))
805 (setq source (point-marker))))
806 (funcall (symbol-function 'compilation-goto-locus)
807 (cons source error-pos))
810 ;; otherwise, default behavior
812 (funcall (symbol-function 'compile-goto-error)))
817 ;;-------------------------------------------------------------------------
818 ;; Grammar related function
819 ;; The functions below work with the syntax class of the characters in an Ada
820 ;; buffer. Two syntax tables are created, depending on whether we want '_'
821 ;; to be considered as part of a word or not.
822 ;; Some characters may have multiple meanings depending on the context:
823 ;; - ' is either the beginning of a constant character or an attribute
824 ;; - # is either part of a based litteral or a gnatprep statement.
825 ;; - " starts a string, but not if inside a constant character.
826 ;; - ( and ) should be ignored if inside a constant character.
827 ;; Thus their syntax property is changed automatically, and we can still use
828 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
830 ;; On Emacs, this is done through the `syntax-table' text property. The
831 ;; modification is done automatically each time the user as typed a new
832 ;; character. This is already done in `font-lock-mode' (in
833 ;; `font-lock-syntactic-keywords', so we take advantage of the existing
834 ;; mechanism. If font-lock-mode is not activated, we do it by hand in
835 ;; `ada-after-change-function', thanks to `ada-deactivate-properties' and
836 ;; `ada-initialize-properties'.
838 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
839 ;; slow advice to `parse-partial-sexp' to do the same thing.
840 ;; When executing parse-partial-sexp, we simply modify the strings before and
841 ;; after, so that the special constants '"', '(' and ')' do not interact
842 ;; with parse-partial-sexp.
843 ;; Note: this code is slow and needs to be rewritten as soon as something
844 ;; better is available on XEmacs.
845 ;;-------------------------------------------------------------------------
847 (defun ada-create-syntax-table ()
848 "Create the two syntax tables use in the Ada mode.
849 The standard table declares `_' as a symbol constituent, the second one
850 declares it as a word constituent."
852 (setq ada-mode-syntax-table (make-syntax-table))
853 (set-syntax-table ada-mode-syntax-table)
855 ;; define string brackets (`%' is alternative string bracket, but
856 ;; almost never used as such and throws font-lock and indentation
858 (modify-syntax-entry ?% "$" ada-mode-syntax-table)
859 (modify-syntax-entry ?\" "\"" ada-mode-syntax-table)
861 (modify-syntax-entry ?: "." ada-mode-syntax-table)
862 (modify-syntax-entry ?\; "." ada-mode-syntax-table)
863 (modify-syntax-entry ?& "." ada-mode-syntax-table)
864 (modify-syntax-entry ?\| "." ada-mode-syntax-table)
865 (modify-syntax-entry ?+ "." ada-mode-syntax-table)
866 (modify-syntax-entry ?* "." ada-mode-syntax-table)
867 (modify-syntax-entry ?/ "." ada-mode-syntax-table)
868 (modify-syntax-entry ?= "." ada-mode-syntax-table)
869 (modify-syntax-entry ?< "." ada-mode-syntax-table)
870 (modify-syntax-entry ?> "." ada-mode-syntax-table)
871 (modify-syntax-entry ?$ "." ada-mode-syntax-table)
872 (modify-syntax-entry ?\[ "." ada-mode-syntax-table)
873 (modify-syntax-entry ?\] "." ada-mode-syntax-table)
874 (modify-syntax-entry ?\{ "." ada-mode-syntax-table)
875 (modify-syntax-entry ?\} "." ada-mode-syntax-table)
876 (modify-syntax-entry ?. "." ada-mode-syntax-table)
877 (modify-syntax-entry ?\\ "." ada-mode-syntax-table)
878 (modify-syntax-entry ?\' "." ada-mode-syntax-table)
880 ;; a single hyphen is punctuation, but a double hyphen starts a comment
881 (modify-syntax-entry ?- ". 12" ada-mode-syntax-table)
883 ;; See the comment above on grammar related function for the special
885 (if (featurep 'xemacs)
886 (modify-syntax-entry ?# "<" ada-mode-syntax-table)
887 (modify-syntax-entry ?# "$" ada-mode-syntax-table))
889 ;; and \f and \n end a comment
890 (modify-syntax-entry ?\f "> " ada-mode-syntax-table)
891 (modify-syntax-entry ?\n "> " ada-mode-syntax-table)
893 ;; define what belongs in Ada symbols
894 (modify-syntax-entry ?_ "_" ada-mode-syntax-table)
896 ;; define parentheses to match
897 (modify-syntax-entry ?\( "()" ada-mode-syntax-table)
898 (modify-syntax-entry ?\) ")(" ada-mode-syntax-table)
900 (setq ada-mode-symbol-syntax-table (copy-syntax-table ada-mode-syntax-table))
901 (modify-syntax-entry ?_ "w" ada-mode-symbol-syntax-table)
904 ;; Support of special characters in XEmacs (see the comments at the beginning
905 ;; of the section on Grammar related functions).
907 (if (featurep 'xemacs)
908 (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
909 "Handles special character constants and gnatprep statements."
913 (setq from to to tmp)))
916 (while (re-search-forward "'\\([(\")#]\\)'" to t)
917 (setq change (cons (list (match-beginning 1)
921 (replace-match "'A'"))
923 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
924 (setq change (cons (list (match-beginning 1)
925 (length (match-string 1))
928 (replace-match (make-string (length (match-string 1)) ?@))))
932 (goto-char (caar change))
933 (delete-char (cadar change))
934 (insert (caddar change))
935 (setq change (cdr change)))))))
937 (defun ada-deactivate-properties ()
938 "Deactivate Ada mode's properties handling.
939 This would be a duplicate of font-lock if both are used at the same time."
940 (remove-hook 'after-change-functions 'ada-after-change-function t))
942 (defun ada-initialize-properties ()
943 "Initialize some special text properties in the whole buffer.
944 In particular, character constants are said to be strings, #...# are treated
945 as numbers instead of gnatprep comments."
949 (goto-char (point-min))
950 (while (re-search-forward "'.'" nil t)
951 (add-text-properties (match-beginning 0) (match-end 0)
952 '(syntax-table ("'" . ?\"))))
953 (goto-char (point-min))
954 (while (re-search-forward "^[ \t]*#" nil t)
955 (add-text-properties (match-beginning 0) (match-end 0)
956 '(syntax-table (11 . 10))))
957 (set-buffer-modified-p nil)
959 ;; Setting this only if font-lock is not set won't work
960 ;; if the user activates or deactivates font-lock-mode,
961 ;; but will make things faster most of the time
962 (add-hook 'after-change-functions 'ada-after-change-function nil t)
965 (defun ada-after-change-function (beg end old-len)
966 "Called when the region between BEG and END was changed in the buffer.
967 OLD-LEN indicates what the length of the replaced text was."
968 (let ((inhibit-point-motion-hooks t)
973 (remove-text-properties (point) eol '(syntax-table nil))
974 (while (re-search-forward "'.'" eol t)
975 (add-text-properties (match-beginning 0) (match-end 0)
976 '(syntax-table ("'" . ?\"))))
978 (if (looking-at "^[ \t]*#")
979 (add-text-properties (match-beginning 0) (match-end 0)
980 '(syntax-table (11 . 10))))))))
982 ;;------------------------------------------------------------------
983 ;; Testing the grammatical context
984 ;;------------------------------------------------------------------
986 (defsubst ada-in-comment-p (&optional parse-result)
987 "Return t if inside a comment.
988 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
989 (nth 4 (or parse-result
991 (line-beginning-position) (point)))))
993 (defsubst ada-in-string-p (&optional parse-result)
994 "Return t if point is inside a string.
995 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
996 (nth 3 (or parse-result
998 (line-beginning-position) (point)))))
1000 (defsubst ada-in-string-or-comment-p (&optional parse-result)
1001 "Return t if inside a comment or string.
1002 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1003 (setq parse-result (or parse-result
1005 (line-beginning-position) (point))))
1006 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
1009 ;;------------------------------------------------------------------
1011 ;; The Ada mode comes with contextual menus, bound by default to the right
1013 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
1014 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
1015 ;; if the mouse button was pressed on an identifier.
1016 ;;------------------------------------------------------------------
1018 (defun ada-call-from-contextual-menu (function)
1019 "Execute FUNCTION when called from the contextual menu.
1020 It forces Emacs to change the cursor position."
1023 (setq ada-contextual-menu-last-point
1024 (list (point) (current-buffer))))
1026 (defun ada-popup-menu (position)
1027 "Pops up a contextual menu, depending on where the user clicked.
1028 POSITION is the location the mouse was clicked on.
1029 Sets `ada-contextual-menu-last-point' to the current position before
1030 displaying the menu. When a function from the menu is called, the
1031 point is where the mouse button was clicked."
1034 ;; declare this as a local variable, so that the function called
1035 ;; in the contextual menu does not hide the region in
1036 ;; transient-mark-mode.
1037 (let ((deactivate-mark nil))
1038 (setq ada-contextual-menu-last-point
1039 (list (point) (current-buffer)))
1040 (mouse-set-point last-input-event)
1042 (setq ada-contextual-menu-on-identifier
1044 (or (= (char-syntax (char-after)) ?w)
1045 (= (char-after) ?_))
1046 (not (ada-in-string-or-comment-p))
1047 (save-excursion (skip-syntax-forward "w")
1048 (not (ada-after-keyword-p)))
1050 (if (fboundp 'popup-menu)
1051 (funcall (symbol-function 'popup-menu) ada-contextual-menu)
1053 (setq choice (x-popup-menu position ada-contextual-menu))
1055 (funcall (lookup-key ada-contextual-menu (vector (car choice)))))))
1057 (set-buffer (cadr ada-contextual-menu-last-point))
1058 (goto-char (car ada-contextual-menu-last-point))
1062 ;;------------------------------------------------------------------
1064 ;;------------------------------------------------------------------
1067 (defun ada-add-extensions (spec body)
1068 "Define SPEC and BODY as being valid extensions for Ada files.
1069 Going from body to spec with `ff-find-other-file' used these
1071 SPEC and BODY are two regular expressions that must match against
1073 (let* ((reg (concat (regexp-quote body) "$"))
1074 (tmp (assoc reg ada-other-file-alist)))
1076 (setcdr tmp (list (cons spec (cadr tmp))))
1077 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
1079 (let* ((reg (concat (regexp-quote spec) "$"))
1080 (tmp (assoc reg ada-other-file-alist)))
1082 (setcdr tmp (list (cons body (cadr tmp))))
1083 (add-to-list 'ada-other-file-alist (list reg (list body)))))
1085 (add-to-list 'auto-mode-alist
1086 (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
1087 (add-to-list 'auto-mode-alist
1088 (cons (concat (regexp-quote body) "\\'") 'ada-mode))
1090 (add-to-list 'ada-spec-suffixes spec)
1091 (add-to-list 'ada-body-suffixes body)
1093 ;; Support for speedbar (Specifies that we want to see these files in
1095 (if (fboundp 'speedbar-add-supported-extension)
1097 (funcall (symbol-function 'speedbar-add-supported-extension)
1099 (funcall (symbol-function 'speedbar-add-supported-extension)
1106 "Ada mode is the major mode for editing Ada code.
1108 Bindings are as follows: (Note: 'LFD' is control-j.)
1111 Indent line '\\[ada-tab]'
1112 Indent line, insert newline and indent the new line. '\\[newline-and-indent]'
1114 Re-format the parameter-list point is in '\\[ada-format-paramlist]'
1115 Indent all lines in region '\\[ada-indent-region]'
1117 Adjust case of identifiers and keywords in region '\\[ada-adjust-case-region]'
1118 Adjust case of identifiers and keywords in buffer '\\[ada-adjust-case-buffer]'
1120 Fill comment paragraph, justify and append postfix '\\[fill-paragraph]'
1122 Next func/proc/task '\\[ada-next-procedure]' Previous func/proc/task '\\[ada-previous-procedure]'
1123 Next package '\\[ada-next-package]' Previous package '\\[ada-previous-package]'
1125 Goto matching start of current 'end ...;' '\\[ada-move-to-start]'
1126 Goto end of current block '\\[ada-move-to-end]'
1128 Comments are handled using standard GNU Emacs conventions, including:
1129 Start a comment '\\[indent-for-comment]'
1130 Comment region '\\[comment-region]'
1131 Uncomment region '\\[ada-uncomment-region]'
1132 Continue comment on next line '\\[indent-new-comment-line]'
1134 If you use imenu.el:
1135 Display index-menu of functions and procedures '\\[imenu]'
1137 If you use find-file.el:
1138 Switch to other file (Body <-> Spec) '\\[ff-find-other-file]'
1139 or '\\[ff-mouse-find-other-file]
1140 Switch to other file in other window '\\[ada-ff-other-window]'
1141 or '\\[ff-mouse-find-other-file-other-window]
1142 If you use this function in a spec and no body is available, it gets created with body stubs.
1144 If you use ada-xref.el:
1145 Goto declaration: '\\[ada-point-and-xref]' on the identifier
1146 or '\\[ada-goto-declaration]' with point on the identifier
1147 Complete identifier: '\\[ada-complete-identifier]'."
1150 (kill-all-local-variables)
1152 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
1154 ;; Set the paragraph delimiters so that one can select a whole block
1156 (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
1157 (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
1159 ;; comment end must be set because it may hold a wrong value if
1160 ;; this buffer had been in another mode before. RE
1161 (set (make-local-variable 'comment-end) "")
1163 ;; used by autofill and indent-new-comment-line
1164 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
1166 ;; used by autofill to break a comment line and continue it on another line.
1167 ;; The reason we need this one is that the default behavior does not work
1168 ;; correctly with the definition of paragraph-start above when the comment
1169 ;; is right after a multi-line subprogram declaration (the comments are
1170 ;; aligned under the latest parameter, not under the declaration start).
1171 (set (make-local-variable 'comment-line-break-function)
1172 (lambda (&optional soft) (let ((fill-prefix nil))
1173 (indent-new-comment-line soft))))
1175 (set (make-local-variable 'indent-line-function)
1176 'ada-indent-current-function)
1178 (set (make-local-variable 'comment-column) 40)
1180 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1181 ;; the comment and the text. We do not want any, this is already
1182 ;; included in comment-start
1183 (unless (featurep 'xemacs)
1185 (if (ada-check-emacs-version 20 3)
1187 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1188 (set (make-local-variable 'comment-padding) 0)))
1189 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1192 (set 'case-fold-search t)
1193 (if (boundp 'imenu-case-fold-search)
1194 (set 'imenu-case-fold-search t))
1196 (set (make-local-variable 'fill-paragraph-function)
1197 'ada-fill-comment-paragraph)
1199 (set (make-local-variable 'imenu-generic-expression)
1200 ada-imenu-generic-expression)
1202 ;; Support for compile.el
1203 ;; We just substitute our own functions to go to the error.
1204 (add-hook 'compilation-mode-hook
1206 (set (make-local-variable 'compile-auto-highlight) 40)
1207 ;; FIXME: This has global impact! -stef
1208 (define-key compilation-minor-mode-map [mouse-2]
1209 'ada-compile-mouse-goto-error)
1210 (define-key compilation-minor-mode-map "\C-c\C-c"
1211 'ada-compile-goto-error)
1212 (define-key compilation-minor-mode-map "\C-m"
1213 'ada-compile-goto-error)))
1215 ;; font-lock support :
1216 ;; We need to set some properties for XEmacs, and define some variables
1219 (if (featurep 'xemacs)
1221 (put 'ada-mode 'font-lock-defaults
1222 '(ada-font-lock-keywords
1223 nil t ((?\_ . "w") (?# . ".")) beginning-of-line))
1225 (set (make-local-variable 'font-lock-defaults)
1226 '(ada-font-lock-keywords
1228 ((?\_ . "w") (?# . "."))
1230 (font-lock-syntactic-keywords . ada-font-lock-syntactic-keywords)))
1233 ;; Set up support for find-file.el.
1234 (set (make-local-variable 'ff-other-file-alist)
1235 'ada-other-file-alist)
1236 (set (make-local-variable 'ff-search-directories)
1237 'ada-search-directories-internal)
1238 (setq ff-post-load-hook 'ada-set-point-accordingly
1239 ff-file-created-hook 'ada-make-body)
1240 (add-hook 'ff-pre-load-hook 'ada-which-function-are-we-in)
1242 ;; Some special constructs for find-file.el.
1243 (make-local-variable 'ff-special-constructs)
1244 (mapc (lambda (pair)
1245 (add-to-list 'ff-special-constructs pair))
1247 ;; Go to the parent package.
1248 (,(eval-when-compile
1249 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1251 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1254 ada-search-directories-internal
1255 (ada-make-filename-from-adaname (match-string 3))
1256 ada-spec-suffixes)))
1257 ;; A "separate" clause.
1258 ("^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1261 ada-search-directories-internal
1262 (ada-make-filename-from-adaname (match-string 1))
1263 ada-spec-suffixes)))
1265 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1268 ada-search-directories-internal
1269 (ada-make-filename-from-adaname (match-string 1))
1270 ada-spec-suffixes)))
1273 ;; Support for outline-minor-mode
1274 (set (make-local-variable 'outline-regexp)
1275 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1276 (set (make-local-variable 'outline-level) 'ada-outline-level)
1278 ;; Support for imenu : We want a sorted index
1279 (setq imenu-sort-function 'imenu--sort-by-name)
1281 ;; Support for ispell : Check only comments
1282 (set (make-local-variable 'ispell-check-comments) 'exclusive)
1284 ;; Support for align.el <= 2.2, if present
1285 ;; align.el is distributed with Emacs 21, but not with earlier versions.
1286 (if (boundp 'align-mode-alist)
1287 (add-to-list 'align-mode-alist '(ada-mode . ada-align-list)))
1289 ;; Support for align.el >= 2.8, if present
1290 (if (boundp 'align-dq-string-modes)
1292 (add-to-list 'align-dq-string-modes 'ada-mode)
1293 (add-to-list 'align-open-comment-modes 'ada-mode)
1294 (set (make-local-variable 'align-region-separate)
1295 ada-align-region-separate)
1297 ;; Exclude comments alone on line from alignment.
1298 (add-to-list 'align-exclude-rules-list
1300 (regexp . "^\\(\\s-*\\)--")
1301 (modes . '(ada-mode))))
1302 (add-to-list 'align-exclude-rules-list
1304 (regexp . "^\\(\\s-*\\)\\<use\\>")
1305 (modes . '(ada-mode))))
1307 (setq ada-align-modes nil)
1309 (add-to-list 'ada-align-modes
1310 '(ada-declaration-assign
1311 (regexp . "[^:]\\(\\s-*\\):[^:]")
1312 (valid . (lambda() (not (ada-in-comment-p))))
1314 (modes . '(ada-mode))))
1315 (add-to-list 'ada-align-modes
1317 (regexp . "[^=]\\(\\s-*\\)=>")
1318 (valid . (lambda() (not (ada-in-comment-p))))
1319 (modes . '(ada-mode))))
1320 (add-to-list 'ada-align-modes
1322 (regexp . "\\(\\s-*\\)--")
1323 (modes . '(ada-mode))))
1324 (add-to-list 'ada-align-modes
1326 (regexp . "\\(\\s-*\\)\\<use\\s-")
1327 (valid . (lambda() (not (ada-in-comment-p))))
1328 (modes . '(ada-mode))))
1329 (add-to-list 'ada-align-modes
1331 (regexp . "\\(\\s-+\\)at\\>")
1332 (modes . '(ada-mode))))
1335 (setq align-mode-rules-list ada-align-modes)
1338 ;; Set up the contextual menu
1340 (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
1342 ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
1343 (define-abbrev-table 'ada-mode-abbrev-table ())
1344 (setq local-abbrev-table ada-mode-abbrev-table)
1346 ;; Support for which-function mode
1347 ;; which-function-mode does not work with nested subprograms, since it is
1348 ;; based only on the regexps generated by imenu, and thus can only detect the
1349 ;; beginning of subprograms, not the end.
1350 ;; Fix is: redefine a new function ada-which-function, and call it when the
1351 ;; major-mode is ada-mode.
1353 (make-local-variable 'which-func-functions)
1354 (setq which-func-functions '(ada-which-function))
1356 ;; Support for indent-new-comment-line (Especially for XEmacs)
1357 (setq comment-multi-line nil)
1359 (setq major-mode 'ada-mode
1362 (use-local-map ada-mode-map)
1364 (easy-menu-add ada-mode-menu ada-mode-map)
1366 (set-syntax-table ada-mode-syntax-table)
1368 (if ada-clean-buffer-before-saving
1370 ;; remove all spaces at the end of lines in the whole buffer.
1371 (add-hook 'local-write-file-hooks 'delete-trailing-whitespace)
1372 ;; convert all tabs to the correct number of spaces.
1373 (add-hook 'local-write-file-hooks
1374 (lambda () (untabify (point-min) (point-max))))))
1376 (set (make-local-variable 'skeleton-further-elements)
1377 '((< '(backward-delete-char-untabify
1378 (min ada-indent (current-column))))))
1379 (add-hook 'skeleton-end-hook 'ada-adjust-case-skeleton nil t)
1381 (run-mode-hooks 'ada-mode-hook)
1383 ;; To be run after the hook, in case the user modified
1384 ;; ada-fill-comment-prefix
1385 (make-local-variable 'comment-start)
1386 (if ada-fill-comment-prefix
1387 (set 'comment-start ada-fill-comment-prefix)
1388 (set 'comment-start "-- "))
1390 ;; Run this after the hook to give the users a chance to activate
1393 (unless (featurep 'xemacs)
1395 (ada-initialize-properties)
1396 (add-hook 'font-lock-mode-hook 'ada-deactivate-properties nil t)))
1398 ;; the following has to be done after running the ada-mode-hook
1399 ;; because users might want to set the values of these variable
1402 (cond ((eq ada-language-version 'ada83)
1403 (setq ada-keywords ada-83-keywords))
1404 ((eq ada-language-version 'ada95)
1405 (setq ada-keywords ada-95-keywords))
1406 ((eq ada-language-version 'ada05)
1407 (setq ada-keywords ada-2005-keywords)))
1410 (ada-activate-keys-for-case)))
1412 (defun ada-adjust-case-skeleton ()
1413 "Adjust the case of the text inserted by a skeleton."
1415 (let ((aa-end (point)))
1416 (ada-adjust-case-region
1417 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
1418 (goto-char aa-end)))))
1420 ;; transient-mark-mode and mark-active are not defined in XEmacs
1421 (defun ada-region-selected ()
1422 "Return t if a region has been selected by the user and is still active."
1423 (or (and (featurep 'xemacs) (funcall (symbol-function 'region-active-p)))
1424 (and (not (featurep 'xemacs))
1425 (symbol-value 'transient-mark-mode)
1426 (symbol-value 'mark-active))))
1429 ;;-----------------------------------------------------------------
1431 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1432 ;; functions to auto-case identifiers, keywords, ...
1433 ;; The basic rules for autocasing are defined through the variables
1434 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1435 ;; are references to the functions that will do the actual casing.
1437 ;; However, in most cases, the user will want to define some exceptions to
1438 ;; these casing rules. This is done through a list of files, that contain
1439 ;; one word per line. These files are stored in `ada-case-exception-file'.
1440 ;; For backward compatibility, this variable can also be a string.
1441 ;;-----------------------------------------------------------------
1443 (defun ada-save-exceptions-to-file (file-name)
1444 "Save the casing exception lists to the file FILE-NAME.
1445 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1446 (find-file (expand-file-name file-name))
1448 (mapcar (lambda (x) (insert (car x) "\n"))
1449 (sort (copy-sequence ada-case-exception)
1450 (lambda(a b) (string< (car a) (car b)))))
1451 (mapcar (lambda (x) (insert "*" (car x) "\n"))
1452 (sort (copy-sequence ada-case-exception-substring)
1453 (lambda(a b) (string< (car a) (car b)))))
1458 (defun ada-create-case-exception (&optional word)
1459 "Define WORD as an exception for the casing system.
1460 If WORD is not given, then the current word in the buffer is used instead.
1461 The new words is added to the first file in `ada-case-exception-file'.
1462 The standard casing rules will no longer apply to this word."
1464 (let ((previous-syntax-table (syntax-table))
1468 (cond ((stringp ada-case-exception-file)
1469 (setq file-name ada-case-exception-file))
1470 ((listp ada-case-exception-file)
1471 (setq file-name (car ada-case-exception-file)))
1473 (error (concat "No exception file specified. "
1474 "See variable ada-case-exception-file"))))
1476 (set-syntax-table ada-mode-symbol-syntax-table)
1479 (skip-syntax-backward "w")
1480 (setq word (buffer-substring-no-properties
1481 (point) (save-excursion (forward-word 1) (point))))))
1482 (set-syntax-table previous-syntax-table)
1484 ;; Reread the exceptions file, in case it was modified by some other,
1485 (ada-case-read-exceptions-from-file file-name)
1487 ;; If the word is already in the list, even with a different casing
1488 ;; we simply want to replace it.
1489 (if (and (not (equal ada-case-exception '()))
1490 (assoc-string word ada-case-exception t))
1491 (setcar (assoc-string word ada-case-exception t) word)
1492 (add-to-list 'ada-case-exception (cons word t))
1495 (ada-save-exceptions-to-file file-name)
1498 (defun ada-create-case-exception-substring (&optional word)
1499 "Define the substring WORD as an exception for the casing system.
1500 If WORD is not given, then the current word in the buffer is used instead,
1501 or the selected region if any is active.
1502 The new word is added to the first file in `ada-case-exception-file'.
1503 When auto-casing a word, this substring will be special-cased, unless the
1504 word itself has a special casing."
1507 (cond ((stringp ada-case-exception-file)
1508 ada-case-exception-file)
1509 ((listp ada-case-exception-file)
1510 (car ada-case-exception-file))
1512 (error (concat "No exception file specified. "
1513 "See variable ada-case-exception-file"))))))
1515 ;; Find the substring to define as an exception. Order is: the parameter,
1516 ;; if any, or the selected region, or the word under the cursor
1520 ((ada-region-selected)
1521 (setq word (buffer-substring-no-properties
1522 (region-beginning) (region-end))))
1525 (let ((underscore-syntax (char-syntax ?_)))
1528 (modify-syntax-entry ?_ "." (syntax-table))
1530 (skip-syntax-backward "w")
1531 (set 'word (buffer-substring-no-properties
1533 (save-excursion (forward-word 1) (point))))))
1534 (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
1537 ;; Reread the exceptions file, in case it was modified by some other,
1538 (ada-case-read-exceptions-from-file file-name)
1540 ;; If the word is already in the list, even with a different casing
1541 ;; we simply want to replace it.
1542 (if (and (not (equal ada-case-exception-substring '()))
1543 (assoc-string word ada-case-exception-substring t))
1544 (setcar (assoc-string word ada-case-exception-substring t) word)
1545 (add-to-list 'ada-case-exception-substring (cons word t))
1548 (ada-save-exceptions-to-file file-name)
1550 (message "%s" (concat "Defining " word " as a casing exception"))))
1552 (defun ada-case-read-exceptions-from-file (file-name)
1553 "Read the content of the casing exception file FILE-NAME."
1554 (if (file-readable-p (expand-file-name file-name))
1555 (let ((buffer (current-buffer)))
1556 (find-file (expand-file-name file-name))
1557 (set-syntax-table ada-mode-symbol-syntax-table)
1559 (goto-char (point-min))
1562 ;; If the item is already in the list, even with an other casing,
1563 ;; do not add it again. This way, the user can easily decide which
1564 ;; priority should be applied to each casing exception
1565 (let ((word (buffer-substring-no-properties
1566 (point) (save-excursion (forward-word 1) (point)))))
1568 ;; Handling a substring ?
1569 (if (char-equal (string-to-char word) ?*)
1571 (setq word (substring word 1))
1572 (unless (assoc-string word ada-case-exception-substring t)
1573 (add-to-list 'ada-case-exception-substring (cons word t))))
1574 (unless (assoc-string word ada-case-exception t)
1575 (add-to-list 'ada-case-exception (cons word t)))))
1579 (set-buffer buffer)))
1582 (defun ada-case-read-exceptions ()
1583 "Read all the casing exception files from `ada-case-exception-file'."
1586 ;; Reinitialize the casing exception list
1587 (setq ada-case-exception '()
1588 ada-case-exception-substring '())
1590 (cond ((stringp ada-case-exception-file)
1591 (ada-case-read-exceptions-from-file ada-case-exception-file))
1593 ((listp ada-case-exception-file)
1594 (mapcar 'ada-case-read-exceptions-from-file
1595 ada-case-exception-file))))
1597 (defun ada-adjust-case-substring ()
1598 "Adjust case of substrings in the previous word."
1600 (let ((substrings ada-case-exception-substring)
1602 (case-fold-search t)
1603 (underscore-syntax (char-syntax ?_))
1611 (modify-syntax-entry ?_ "." (syntax-table))
1614 (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
1617 (while (re-search-forward re max t)
1618 (replace-match (caar substrings) t)))
1619 (setq substrings (cdr substrings))
1622 (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
1625 (defun ada-adjust-case-identifier ()
1626 "Adjust case of the previous identifier.
1627 The auto-casing is done according to the value of `ada-case-identifier'
1628 and the exceptions defined in `ada-case-exception-file'."
1630 (if (or (equal ada-case-exception '())
1631 (equal (char-after) ?_))
1633 (funcall ada-case-identifier -1)
1634 (ada-adjust-case-substring))
1638 (start (save-excursion (skip-syntax-backward "w")
1641 ;; If we have an exception, replace the word by the correct casing
1642 (if (setq match (assoc-string (buffer-substring start end)
1643 ada-case-exception t))
1646 (delete-region start end)
1647 (insert (car match)))
1649 ;; Else simply re-case the word
1650 (funcall ada-case-identifier -1)
1651 (ada-adjust-case-substring))))))
1653 (defun ada-after-keyword-p ()
1654 "Return t if cursor is after a keyword that is not an attribute."
1657 (and (not (and (char-before)
1658 (or (= (char-before) ?_)
1659 (= (char-before) ?'))));; unless we have a _ or '
1660 (looking-at (concat ada-keywords "[^_]")))))
1662 (defun ada-adjust-case (&optional force-identifier)
1663 "Adjust the case of the word before the character just typed.
1664 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1668 (if (and (not (bobp))
1669 ;; or if at the end of a character constant
1670 (not (and (eq (following-char) ?')
1671 (eq (char-before (1- (point))) ?')))
1672 ;; or if the previous character was not part of a word
1673 (eq (char-syntax (char-before)) ?w)
1674 ;; if in a string or a comment
1675 (not (ada-in-string-or-comment-p))
1679 (or (= (point) (point-min))
1681 (= (following-char) ?'))
1682 (funcall ada-case-attribute -1)
1684 (not force-identifier) ; (MH)
1685 (ada-after-keyword-p))
1686 (funcall ada-case-keyword -1)
1687 (ada-adjust-case-identifier))))
1692 (defun ada-adjust-case-interactive (arg)
1693 "Adjust the case of the previous word, and process the character just typed.
1694 ARG is the prefix the user entered with \\[universal-argument]."
1698 (let ((lastk last-command-char)
1699 (previous-syntax-table (syntax-table)))
1703 (set-syntax-table ada-mode-symbol-syntax-table)
1704 (cond ((or (eq lastk ?\n)
1709 ;; horrible dekludge
1710 (delete-backward-char 1)
1711 ;; some special keys and their bindings
1714 (funcall ada-lfd-binding))
1716 (funcall ada-ret-binding))))
1717 ((eq lastk ?\C-i) (ada-tab))
1718 ;; Else just insert the character
1719 ((self-insert-command (prefix-numeric-value arg))))
1720 ;; if there is a keyword in front of the underscore
1721 ;; then it should be part of an identifier (MH)
1726 ;; Restore the syntax table
1727 (set-syntax-table previous-syntax-table))
1730 ;; Else, no auto-casing
1732 ((eq last-command-char ?\n)
1733 (funcall ada-lfd-binding))
1734 ((eq last-command-char ?\r)
1735 (funcall ada-ret-binding))
1737 (self-insert-command (prefix-numeric-value arg))))
1740 (defun ada-activate-keys-for-case ()
1741 "Modify the key bindings for all the keys that should readjust the casing."
1743 ;; Save original key-bindings to allow swapping ret/lfd
1744 ;; when casing is activated.
1745 ;; The 'or ...' is there to be sure that the value will not
1746 ;; be changed again when Ada mode is called more than once
1747 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
1748 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
1750 ;; Call case modifying function after certain keys.
1751 (mapcar (function (lambda(key) (define-key
1753 (char-to-string key)
1754 'ada-adjust-case-interactive)))
1755 '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
1756 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1758 (defun ada-loose-case-word (&optional arg)
1759 "Upcase first letter and letters following `_' in the following word.
1760 No other letter is modified.
1761 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1764 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1766 (skip-syntax-backward "w")
1767 (while (and (or first (search-forward "_" end t))
1771 (insert-char (upcase (following-char)) 1)
1774 (defun ada-no-auto-case (&optional arg)
1775 "Do nothing. ARG is ignored.
1776 This function can be used for the auto-casing variables in Ada mode, to
1777 adapt to unusal auto-casing schemes. Since it does nothing, you can for
1778 instance use it for `ada-case-identifier' if you don't want any special
1779 auto-casing for identifiers, whereas keywords have to be lower-cased.
1780 See also `ada-auto-case' to disable auto casing altogether."
1783 (defun ada-capitalize-word (&optional arg)
1784 "Upcase first letter and letters following '_', lower case other letters.
1785 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1787 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1788 (begin (save-excursion (skip-syntax-backward "w") (point))))
1789 (modify-syntax-entry ?_ "_")
1790 (capitalize-region begin end)
1791 (modify-syntax-entry ?_ "w")))
1793 (defun ada-adjust-case-region (from to)
1794 "Adjust the case of all words in the region between FROM and TO.
1795 Attention: This function might take very long for big regions!"
1801 (previous-syntax-table (syntax-table)))
1802 (message "Adjusting case ...")
1805 (set-syntax-table ada-mode-symbol-syntax-table)
1808 ;; loop: look for all identifiers, keywords, and attributes
1810 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1811 (setq end (match-end 1))
1813 (and (> (point) from)
1816 (setq attribp (looking-at "'.[^']")))))
1818 ;; do nothing if it is a string or comment
1819 (ada-in-string-or-comment-p)
1822 ;; get the identifier or keyword or attribute
1824 (setq begin (point))
1825 (setq keywordp (looking-at ada-keywords))
1828 ;; casing according to user-option
1831 (funcall ada-case-attribute -1)
1833 (funcall ada-case-keyword -1)
1834 (ada-adjust-case-identifier)))
1835 (goto-char begin))))
1836 (message "Adjusting case ... Done"))
1837 (set-syntax-table previous-syntax-table))))
1839 (defun ada-adjust-case-buffer ()
1840 "Adjust the case of all words in the whole buffer.
1841 ATTENTION: This function might take very long for big buffers!"
1843 (ada-adjust-case-region (point-min) (point-max)))
1846 ;;--------------------------------------------------------------
1847 ;; Format Parameter Lists
1848 ;; Some special algorithms are provided to indent the parameter lists in
1849 ;; subprogram declarations. This is done in two steps:
1850 ;; - First parses the parameter list. The returned list has the following
1852 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1854 ;; This is done in `ada-scan-paramlist'.
1855 ;; - Delete and recreate the parameter list in function
1856 ;; `ada-insert-paramlist'.
1857 ;; Both steps are called from `ada-format-paramlist'.
1858 ;; Note: Comments inside the parameter list are lost.
1859 ;; The syntax has to be correct, or the reformating will fail.
1860 ;;--------------------------------------------------------------
1862 (defun ada-format-paramlist ()
1863 "Reformat the parameter list point is in."
1869 (previous-syntax-table (syntax-table)))
1872 (set-syntax-table ada-mode-symbol-syntax-table)
1874 ;; check if really inside parameter list
1875 (or (ada-in-paramlist-p)
1876 (error "Not in parameter list"))
1878 ;; find start of current parameter-list
1879 (ada-search-ignore-string-comment
1880 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1883 (setq begin (point))
1885 ;; find end of parameter-list
1887 (setq delend (point))
1891 ;; find end of last parameter-declaration
1892 (forward-comment -1000)
1895 ;; build a list of all elements of the parameter-list
1896 (setq paramlist (ada-scan-paramlist (1+ begin) end))
1898 ;; delete the original parameter-list
1899 (delete-region begin delend)
1901 ;; insert the new parameter-list
1903 (ada-insert-paramlist paramlist))
1905 ;; restore syntax-table
1906 (set-syntax-table previous-syntax-table)
1909 (defun ada-scan-paramlist (begin end)
1910 "Scan the parameter list found in between BEGIN and END.
1911 Return the equivalent internal parameter list."
1912 (let ((paramlist (list))
1922 ;; loop until end of last parameter
1925 ;; find first character of parameter-declaration
1926 (ada-goto-next-non-ws)
1929 ;; find last character of parameter-declaration
1930 (if (setq match-cons
1931 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1933 (setq epos (car match-cons))
1934 (setq semipos (cdr match-cons)))
1937 ;; read name(s) of parameter(s)
1939 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1941 (setq param (list (match-string 1)))
1942 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1950 (ada-search-ignore-string-comment
1951 "in" nil epos t 'word-search-forward)))))
1959 (ada-search-ignore-string-comment
1960 "out" nil epos t 'word-search-forward)))))
1962 ;; look for 'access'
1968 (ada-search-ignore-string-comment
1969 "access" nil epos t 'word-search-forward)))))
1971 ;; skip 'in'/'out'/'access'
1973 (ada-goto-next-non-ws)
1974 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1976 (ada-goto-next-non-ws))
1978 ;; read type of parameter
1979 ;; We accept spaces in the name, since some software like Rose
1980 ;; generates something like: "A : B 'Class"
1981 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1984 (list (match-string 0))))
1986 ;; read default-expression, if there is one
1987 (goto-char (setq apos (match-end 0)))
1991 (if (setq match-cons
1992 (ada-search-ignore-string-comment
1993 ":=" nil epos t 'search-forward))
1994 (buffer-substring (car match-cons) epos)
1997 ;; add this parameter-declaration to the list
1998 (setq paramlist (append paramlist (list param)))
2000 ;; check if it was the last parameter
2003 (goto-char semipos))
2005 (reverse paramlist)))
2007 (defun ada-insert-paramlist (paramlist)
2008 "Insert a formatted PARAMLIST in the buffer."
2009 (let ((i (length paramlist))
2018 ;; loop until last parameter
2019 (while (not (zerop i))
2022 ;; get max length of parameter-name
2023 (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
2025 ;; get max length of type-name
2026 (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
2028 ;; is there any 'in' ?
2029 (setq inp (or inp (nth 1 (nth i paramlist))))
2031 ;; is there any 'out' ?
2032 (setq outp (or outp (nth 2 (nth i paramlist))))
2034 ;; is there any 'access' ?
2035 (setq accessp (or accessp (nth 3 (nth i paramlist))))
2038 ;; does paramlist already start on a separate line ?
2040 (re-search-backward "^.\\|[^ \t]" nil t)
2042 ;; yes => re-indent it
2044 (ada-indent-current)
2046 (if (looking-at "\\(is\\|return\\)")
2047 (replace-match " \\1"))))
2049 ;; no => insert it where we are after removing any whitespace
2053 ((looking-at "[ \t]*\\(\n\\|;\\)")
2054 (replace-match "\\1"))
2055 ((looking-at "[ \t]*\\(is\\|return\\)")
2056 (replace-match " \\1"))))
2060 (ada-indent-current)
2062 (setq firstcol (current-column))
2063 (setq i (length paramlist))
2065 ;; loop until last parameter
2066 (while (not (zerop i))
2068 (setq column firstcol)
2070 ;; insert parameter-name, space and colon
2071 (insert (nth 0 (nth i paramlist)))
2072 (indent-to (+ column parlen 1))
2074 (setq column (current-column))
2076 ;; insert 'in' or space
2077 (if (nth 1 (nth i paramlist))
2082 (not (nth 3 (nth i paramlist))))
2085 ;; insert 'out' or space
2086 (if (nth 2 (nth i paramlist))
2091 (not (nth 3 (nth i paramlist))))
2095 (if (nth 3 (nth i paramlist))
2098 (setq column (current-column))
2100 ;; insert type-name and, if necessary, space and default-expression
2101 (insert (nth 4 (nth i paramlist)))
2102 (if (nth 5 (nth i paramlist))
2104 (indent-to (+ column typlen 1))
2105 (insert (nth 5 (nth i paramlist)))))
2107 ;; check if it was the last parameter
2110 ;; no => insert ';' and newline and indent
2113 (indent-to firstcol))
2116 ;; if anything follows, except semicolon, newline, is or return
2117 ;; put it in a new line and indent it
2118 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2119 (ada-indent-newline-indent))
2124 ;;;----------------------------------------------------------------
2125 ;; Indentation Engine
2126 ;; All indentations are indicated as a two-element string:
2127 ;; - position of reference in the buffer
2128 ;; - offset to indent from this position (can also be a symbol or a list
2129 ;; that are evaluated)
2130 ;; Thus the total indentation for a line is the column number of the reference
2131 ;; position plus whatever value the evaluation of the second element provides.
2132 ;; This mechanism is used so that the Ada mode can "explain" how the
2133 ;; indentation was calculated, by showing which variables were used.
2135 ;; The indentation itself is done in only one pass: first we try to guess in
2136 ;; what context we are by looking at the following keyword or punctuation
2137 ;; sign. If nothing remarkable is found, just try to guess the indentation
2138 ;; based on previous lines.
2140 ;; The relevant functions for indentation are:
2141 ;; - `ada-indent-region': Re-indent a region of text
2142 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2143 ;; calculation that were done
2144 ;; - `ada-indent-current': Re-indent the current line
2145 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2146 ;; based on the context (see above).
2147 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2148 ;; For efficiency, these functions do not check they are in the correct
2150 ;;;----------------------------------------------------------------
2152 (defun ada-indent-region (beg end)
2153 "Indent the region between BEG end END."
2156 (let ((block-done 0)
2157 (lines-remaining (count-lines beg end))
2158 (msg (format "%%4d out of %4d lines remaining ..."
2159 (count-lines beg end)))
2160 (endmark (copy-marker end)))
2161 ;; catch errors while indenting
2162 (while (< (point) endmark)
2163 (if (> block-done 39)
2165 (setq lines-remaining (- lines-remaining block-done)
2167 (message msg lines-remaining)))
2168 (if (= (char-after) ?\n) nil
2169 (ada-indent-current))
2171 (setq block-done (1+ block-done)))
2172 (message "Indenting ... done")))
2174 (defun ada-indent-newline-indent ()
2175 "Indent the current line, insert a newline and then indent the new line."
2177 (ada-indent-current)
2179 (ada-indent-current))
2181 (defun ada-indent-newline-indent-conditional ()
2182 "Insert a newline and indent it.
2183 The original line is indented first if `ada-indent-after-return' is non-nil."
2185 (if ada-indent-after-return (ada-indent-current))
2187 (ada-indent-current))
2189 (defun ada-justified-indent-current ()
2190 "Indent the current line and explain how the calculation was done."
2193 (let ((cur-indent (ada-indent-current)))
2195 (let ((line (save-excursion
2196 (goto-char (car cur-indent))
2197 (count-lines 1 (point)))))
2199 (if (equal (cdr cur-indent) '(0))
2200 (message (concat "same indentation as line " (number-to-string line)))
2201 (message "%s" (mapconcat (lambda(x)
2206 (number-to-string x))
2208 (concat "- " (symbol-name (cadr x))))
2213 (goto-char (car cur-indent))
2216 (defun ada-batch-reformat ()
2217 "Re-indent and re-case all the files found on the command line.
2218 This function should be used from the Unix/Windows command line, with a
2220 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2222 (while command-line-args-left
2223 (let ((source (car command-line-args-left)))
2224 (message "Formating %s" source)
2226 (ada-indent-region (point-min) (point-max))
2227 (ada-adjust-case-buffer)
2228 (write-file source))
2229 (setq command-line-args-left (cdr command-line-args-left)))
2233 (defsubst ada-goto-previous-word ()
2234 "Move point to the beginning of the previous word of Ada code.
2235 Return the new position of point or nil if not found."
2236 (ada-goto-next-word t))
2238 (defun ada-indent-current ()
2239 "Indent current line as Ada code.
2240 Return the calculation that was done, including the reference point and the
2243 (let ((previous-syntax-table (syntax-table))
2244 (orgpoint (point-marker))
2245 cur-indent tmp-indent
2250 (set-syntax-table ada-mode-symbol-syntax-table)
2252 ;; This need to be done here so that the advice is not always
2253 ;; activated (this might interact badly with other modes)
2254 (if (featurep 'xemacs)
2255 (ad-activate 'parse-partial-sexp t))
2260 ;; Not First line in the buffer ?
2261 (if (save-excursion (zerop (forward-line -1)))
2263 (back-to-indentation)
2264 (ada-get-current-indent))
2266 ;; first line in the buffer
2267 (list (point-min) 0))))
2269 ;; Evaluate the list to get the column to indent to
2270 ;; prev-indent contains the column to indent to
2272 (setq prev-indent (save-excursion (goto-char (car cur-indent))
2274 tmp-indent (cdr cur-indent))
2275 (setq prev-indent 0 tmp-indent '()))
2277 (while (not (null tmp-indent))
2279 ((numberp (car tmp-indent))
2280 (setq prev-indent (+ prev-indent (car tmp-indent))))
2282 (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
2284 (setq tmp-indent (cdr tmp-indent)))
2286 ;; only re-indent if indentation is different then the current
2287 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
2290 (delete-horizontal-space)
2291 (indent-to prev-indent))
2293 ;; restore position of point
2295 (goto-char orgpoint)
2296 (if (< (current-column) (current-indentation))
2297 (back-to-indentation)))
2299 ;; restore syntax-table
2300 (set-syntax-table previous-syntax-table)
2301 (if (featurep 'xemacs)
2302 (ad-deactivate 'parse-partial-sexp))
2308 (defun ada-get-current-indent ()
2309 "Return the indentation to use for the current line."
2314 (orgpoint (save-excursion
2316 (forward-comment -10000)
2323 ;;-----------------------------
2324 ;; in open parenthesis, but not in parameter-list
2325 ;;-----------------------------
2327 ((and ada-indent-to-open-paren
2328 (not (ada-in-paramlist-p))
2329 (setq column (ada-in-open-paren-p)))
2331 ;; check if we have something like this (Table_Component_Type =>
2332 ;; Source_File_Record)
2335 ;; Align the closing parenthesis on the opening one
2336 (if (= (following-char) ?\))
2339 (skip-chars-backward " \t")
2340 (list (1- (point)) 0))
2342 (if (and (skip-chars-backward " \t")
2343 (= (char-before) ?\n)
2344 (not (forward-comment -10000))
2345 (= (char-before) ?>))
2346 ;; ??? Could use a different variable
2347 (list column 'ada-broken-indent)
2349 ;; We want all continuation lines to be indented the same
2350 ;; (ada-broken-line from the opening parenthesis. However, in
2351 ;; parameter list, each new parameter should be indented at the
2352 ;; column as the opening parenthesis.
2354 ;; A special case to handle nested boolean expressions, as in
2356 ;; and then C) -- indented by ada-broken-indent
2357 ;; or else D) -- indenting this line.
2358 ;; ??? This is really a hack, we should have a proper way to go to
2359 ;; ??? the beginning of the statement
2361 (if (= (char-before) ?\))
2364 (if (memq (char-before) '(?, ?\; ?\( ?\)))
2366 (list column 'ada-continuation-indent)
2369 ;;---------------------------
2371 ;;---------------------------
2374 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2376 ;;---------------------------
2378 ;;---------------------------
2380 ((= (downcase (char-after)) ?e)
2383 ;; ------- end ------
2385 ((looking-at "end\\>")
2389 (ada-goto-matching-start 1)
2392 ;; found 'loop' => skip back to 'while' or 'for'
2393 ;; if 'loop' is not on a separate line
2394 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2398 (looking-at ".+\\<loop\\>"))
2401 (setq limit (car (ada-search-ignore-string-comment ";" t))))
2405 (ada-search-ignore-string-comment ada-loop-start-re t limit))
2406 (not (looking-at "\\<loop\\>"))))
2408 (goto-char (car match-cons))
2411 (if (looking-at ada-named-block-re)
2412 (setq label (- ada-label-indent))))))))
2414 ;; found 'record' =>
2415 ;; if the keyword is found at the beginning of a line (or just
2416 ;; after limited, we indent on it, otherwise we indent on the
2417 ;; beginning of the type declaration)
2418 ;; type A is (B : Integer;
2419 ;; C : Integer) is record
2420 ;; end record; -- This is badly indented otherwise
2421 (if (looking-at "record")
2424 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2425 (list (save-excursion (back-to-indentation) (point)) 0)
2426 (list (save-excursion
2427 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2430 ;; Else keep the same indentation as the beginning statement
2431 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
2433 ;; ------ exception ----
2435 ((looking-at "exception\\>")
2437 (ada-goto-matching-start 1)
2438 (list (save-excursion (back-to-indentation) (point)) 0)))
2442 ((looking-at "else\\>")
2443 (if (save-excursion (ada-goto-previous-word)
2444 (looking-at "\\<or\\>"))
2445 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2447 (ada-goto-matching-start 1 nil t)
2448 (list (progn (back-to-indentation) (point)) 0))))
2452 ((looking-at "elsif\\>")
2454 (ada-goto-matching-start 1 nil t)
2455 (list (progn (back-to-indentation) (point)) 0)))
2459 ;;---------------------------
2460 ;; starting with w (when)
2461 ;;---------------------------
2463 ((and (= (downcase (char-after)) ?w)
2464 (looking-at "when\\>"))
2466 (ada-goto-matching-start 1)
2467 (list (save-excursion (back-to-indentation) (point))
2470 ;;---------------------------
2471 ;; starting with t (then)
2472 ;;---------------------------
2474 ((and (= (downcase (char-after)) ?t)
2475 (looking-at "then\\>"))
2476 (if (save-excursion (ada-goto-previous-word)
2477 (looking-at "and\\>"))
2478 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2480 ;; Select has been added for the statement: "select ... then abort"
2481 (ada-search-ignore-string-comment
2482 "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2483 (list (progn (back-to-indentation) (point))
2484 'ada-stmt-end-indent))))
2486 ;;---------------------------
2487 ;; starting with l (loop)
2488 ;;---------------------------
2490 ((and (= (downcase (char-after)) ?l)
2491 (looking-at "loop\\>"))
2494 (goto-char (match-end 0))
2495 (ada-goto-stmt-start)
2496 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2497 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2498 (unless (looking-at ada-loop-start-re)
2499 (ada-search-ignore-string-comment ada-loop-start-re
2501 (if (looking-at "\\<loop\\>")
2502 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2503 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2505 ;;----------------------------
2506 ;; starting with l (limited) or r (record)
2507 ;;----------------------------
2509 ((or (and (= (downcase (char-after)) ?l)
2510 (looking-at "limited\\>"))
2511 (and (= (downcase (char-after)) ?r)
2512 (looking-at "record\\>")))
2515 (ada-search-ignore-string-comment
2516 "\\<\\(type\\|use\\)\\>" t nil)
2517 (if (looking-at "\\<use\\>")
2518 (ada-search-ignore-string-comment "for" t nil nil
2519 'word-search-backward))
2520 (list (progn (back-to-indentation) (point))
2521 'ada-indent-record-rel-type)))
2523 ;;---------------------------
2524 ;; starting with b (begin)
2525 ;;---------------------------
2527 ((and (= (downcase (char-after)) ?b)
2528 (looking-at "begin\\>"))
2530 (if (ada-goto-matching-decl-start t)
2531 (list (progn (back-to-indentation) (point)) 0)
2532 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2534 ;;---------------------------
2535 ;; starting with i (is)
2536 ;;---------------------------
2538 ((and (= (downcase (char-after)) ?i)
2539 (looking-at "is\\>"))
2541 (if (and ada-indent-is-separate
2543 (goto-char (match-end 0))
2544 (ada-goto-next-non-ws (save-excursion (end-of-line)
2546 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2548 (ada-goto-stmt-start)
2549 (list (progn (back-to-indentation) (point)) 'ada-indent))
2551 (ada-goto-stmt-start)
2552 (if (looking-at "\\<package\\|procedure\\|function\\>")
2553 (list (progn (back-to-indentation) (point)) 0)
2554 (list (progn (back-to-indentation) (point)) 'ada-indent)))))
2556 ;;---------------------------
2557 ;; starting with r (return, renames)
2558 ;;---------------------------
2560 ((and (= (downcase (char-after)) ?r)
2561 (looking-at "re\\(turn\\|names\\)\\>"))
2564 (let ((var 'ada-indent-return))
2565 ;; If looking at a renames, skip the 'return' statement too
2566 (if (looking-at "renames")
2569 (set 'pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
2571 (= (downcase (char-after (car pos))) ?r))
2572 (goto-char (car pos)))
2573 (set 'var 'ada-indent-renames)))
2575 (forward-comment -1000)
2576 (if (= (char-before) ?\))
2580 ;; If there is a parameter list, and we have a function declaration
2581 ;; or a access to subprogram declaration
2583 (if (and (= (following-char) ?\()
2587 (looking-at "\\(function\\|procedure\\)\\>"))
2591 (looking-at "\\(function\\|procedure\\)\\>")))))
2593 ;; The indentation depends of the value of ada-indent-return
2594 (if (<= (eval var) 0)
2595 (list (point) (list '- var))
2596 (list (progn (backward-word num-back) (point))
2599 ;; Else there is no parameter list, but we have a function
2600 ;; Only do something special if the user want to indent
2601 ;; relative to the "function" keyword
2602 (if (and (> (eval var) 0)
2603 (save-excursion (forward-word -1)
2604 (looking-at "function\\>")))
2605 (list (progn (forward-word -1) (point)) var)
2608 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
2610 ;;--------------------------------
2611 ;; starting with 'o' or 'p'
2612 ;; 'or' as statement-start
2613 ;; 'private' as statement-start
2614 ;;--------------------------------
2616 ((and (or (= (downcase (char-after)) ?o)
2617 (= (downcase (char-after)) ?p))
2618 (or (ada-looking-at-semi-or)
2619 (ada-looking-at-semi-private)))
2621 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2622 (ada-goto-matching-start 1)
2623 (list (progn (back-to-indentation) (point)) 0)))
2625 ;;--------------------------------
2626 ;; starting with 'd' (do)
2627 ;;--------------------------------
2629 ((and (= (downcase (char-after)) ?d)
2630 (looking-at "do\\>"))
2632 (ada-goto-stmt-start)
2633 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2635 ;;--------------------------------
2636 ;; starting with '-' (comment)
2637 ;;--------------------------------
2639 ((= (char-after) ?-)
2640 (if ada-indent-comment-as-code
2642 ;; Indent comments on previous line comments if required
2643 ;; We must use a search-forward (even if the code is more complex),
2644 ;; since we want to find the beginning of the comment.
2647 (if (and ada-indent-align-comments
2651 (while (and (not pos)
2652 (search-forward "--"
2654 (end-of-line) (point))
2656 (unless (ada-in-string-p)
2657 (setq pos (point))))
2661 ;; Else always on previous line
2662 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2664 ;; Else same indentation as the previous line
2665 (list (save-excursion (back-to-indentation) (point)) 0)))
2667 ;;--------------------------------
2668 ;; starting with '#' (preprocessor line)
2669 ;;--------------------------------
2671 ((and (= (char-after) ?#)
2672 (equal ada-which-compiler 'gnat)
2673 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2674 (list (save-excursion (beginning-of-line) (point)) 0))
2676 ;;--------------------------------
2677 ;; starting with ')' (end of a parameter list)
2678 ;;--------------------------------
2680 ((and (not (eobp)) (= (char-after) ?\)))
2686 ;;---------------------------------
2687 ;; new/abstract/separate
2688 ;;---------------------------------
2690 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2691 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2693 ;;---------------------------------
2694 ;; package/function/procedure
2695 ;;---------------------------------
2697 ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
2698 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2700 ;; Go up until we find either a generic section, or the end of the
2701 ;; previous subprogram/package
2703 (while (and (not found)
2704 (ada-search-ignore-string-comment
2705 "\\<\\(generic\\|end\\|begin\\|package\\|procedure\\|function\\)\\>" t))
2707 ;; avoid "with procedure"... in generic parts
2710 (setq found (not (looking-at "with"))))))
2712 (if (looking-at "generic")
2713 (list (progn (back-to-indentation) (point)) 0)
2714 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2716 ;;---------------------------------
2718 ;;---------------------------------
2720 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2722 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2723 (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
2724 '(ada-label-indent))))
2728 ;;---------------------------------
2730 ;;---------------------------------
2731 (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2733 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2734 "Calculate the indentation for the new line after ORGPOINT.
2735 The result list is based on the previous lines in the buffer.
2736 If NOMOVE is nil, moves point to the beginning of the current statement.
2737 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2739 (goto-char initial-pos))
2740 (let ((oldpoint (point)))
2742 ;; Is inside a parameter-list ?
2743 (if (ada-in-paramlist-p)
2744 (ada-get-indent-paramlist)
2746 ;; move to beginning of current statement
2748 (ada-goto-stmt-start))
2750 ;; no beginning found => don't change indentation
2751 (if (and (eq oldpoint (point))
2753 (ada-get-indent-nochange)
2758 ada-indent-to-open-paren
2759 (ada-in-open-paren-p))
2760 (ada-get-indent-open-paren))
2762 ((looking-at "end\\>")
2763 (ada-get-indent-end orgpoint))
2765 ((looking-at ada-loop-start-re)
2766 (ada-get-indent-loop orgpoint))
2768 ((looking-at ada-subprog-start-re)
2769 (ada-get-indent-subprog orgpoint))
2771 ((looking-at ada-block-start-re)
2772 (ada-get-indent-block-start orgpoint))
2774 ((looking-at "\\(sub\\)?type\\>")
2775 (ada-get-indent-type orgpoint))
2777 ;; "then" has to be included in the case of "select...then abort"
2778 ;; statements, since (goto-stmt-start) at the beginning of
2779 ;; the current function would leave the cursor on that position
2780 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2781 (ada-get-indent-if orgpoint))
2783 ((looking-at "case\\>")
2784 (ada-get-indent-case orgpoint))
2786 ((looking-at "when\\>")
2787 (ada-get-indent-when orgpoint))
2789 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2790 (ada-get-indent-label orgpoint))
2792 ((looking-at "separate\\>")
2793 (ada-get-indent-nochange))
2797 (list (+ (save-excursion (back-to-indentation) (point))
2798 (- ada-label-indent))))
2801 ((looking-at "with\\>\\|use\\>")
2802 ;; Are we still in that statement, or are we in fact looking at
2803 ;; the previous one ?
2804 (if (save-excursion (search-forward ";" oldpoint t))
2805 (list (progn (back-to-indentation) (point)) 0)
2806 (list (point) (if (looking-at "with")
2811 (ada-get-indent-noindent orgpoint)))))
2814 (defun ada-get-indent-open-paren ()
2815 "Calculate the indentation when point is behind an unclosed parenthesis."
2816 (list (ada-in-open-paren-p) 0))
2818 (defun ada-get-indent-nochange ()
2819 "Return the current indentation of the previous line."
2822 (back-to-indentation)
2825 (defun ada-get-indent-paramlist ()
2826 "Calculate the indentation when point is inside a parameter list."
2828 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2830 ;; in front of the first parameter
2831 ((= (char-after) ?\()
2832 (goto-char (match-end 0))
2835 ;; in front of another parameter
2836 ((= (char-after) ?\;)
2837 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2838 (ada-goto-next-non-ws)
2841 ;; After an affectation (default parameter value in subprogram
2843 ((and (= (following-char) ?=) (= (preceding-char) ?:))
2844 (back-to-indentation)
2845 (list (point) 'ada-broken-indent))
2847 ;; inside a parameter declaration
2849 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2850 (ada-goto-next-non-ws)
2851 (list (point) 0)))))
2853 (defun ada-get-indent-end (orgpoint)
2854 "Calculate the indentation when point is just before an end statement.
2855 ORGPOINT is the limit position used in the calculation."
2856 (let ((defun-name nil)
2859 ;; is the line already terminated by ';' ?
2861 (ada-search-ignore-string-comment ";" nil orgpoint nil
2864 ;; yes, look what's following 'end'
2867 (ada-goto-next-non-ws)
2869 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\)\\>")
2870 (save-excursion (ada-check-matching-start (match-string 0)))
2871 (list (save-excursion (back-to-indentation) (point)) 0))
2874 ;; loop/select/if/case/record/select
2876 ((looking-at "\\<record\\>")
2878 (ada-check-matching-start (match-string 0))
2879 ;; we are now looking at the matching "record" statement
2881 (ada-goto-stmt-start)
2882 ;; now on the matching type declaration, or use clause
2883 (unless (looking-at "\\(for\\|type\\)\\>")
2884 (ada-search-ignore-string-comment "\\<type\\>" t))
2885 (list (progn (back-to-indentation) (point)) 0)))
2887 ;; a named block end
2889 ((looking-at ada-ident-re)
2890 (setq defun-name (match-string 0))
2892 (ada-goto-matching-start 0)
2893 (ada-check-defun-name defun-name))
2894 (list (progn (back-to-indentation) (point)) 0))
2896 ;; a block-end without name
2898 ((= (char-after) ?\;)
2900 (ada-goto-matching-start 0)
2901 (if (looking-at "\\<begin\\>")
2903 (setq indent (list (point) 0))
2904 (if (ada-goto-matching-decl-start t)
2905 (list (progn (back-to-indentation) (point)) 0)
2907 (list (progn (back-to-indentation) (point)) 0)
2910 ;; anything else - should maybe signal an error ?
2913 (list (save-excursion (back-to-indentation) (point))
2914 'ada-broken-indent))))
2916 (list (save-excursion (back-to-indentation) (point))
2917 'ada-broken-indent))))
2919 (defun ada-get-indent-case (orgpoint)
2920 "Calculate the indentation when point is just before a case statement.
2921 ORGPOINT is the limit position used in the calculation."
2922 (let ((match-cons nil)
2926 ;; case..is..when..=>
2929 (setq match-cons (and
2930 ;; the `=>' must be after the keyword `is'.
2931 (ada-search-ignore-string-comment
2932 "is" nil orgpoint nil 'word-search-forward)
2933 (ada-search-ignore-string-comment
2934 "[ \t\n]+=>" nil orgpoint))))
2936 (goto-char (car match-cons))
2937 (unless (ada-search-ignore-string-comment "when" t opos)
2938 (error "Missing 'when' between 'case' and '=>'"))
2939 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2944 (setq match-cons (ada-search-ignore-string-comment
2945 "when" nil orgpoint nil 'word-search-forward)))
2946 (goto-char (cdr match-cons))
2947 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2952 (setq match-cons (ada-search-ignore-string-comment
2953 "is" nil orgpoint nil 'word-search-forward)))
2954 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2959 (list (save-excursion (back-to-indentation) (point))
2960 'ada-broken-indent)))))
2962 (defun ada-get-indent-when (orgpoint)
2963 "Calculate the indentation when point is just before a when statement.
2964 ORGPOINT is the limit position used in the calculation."
2965 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2966 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2967 (list cur-indent 'ada-indent)
2968 (list cur-indent 'ada-broken-indent))))
2970 (defun ada-get-indent-if (orgpoint)
2971 "Calculate the indentation when point is just before an if statement.
2972 ORGPOINT is the limit position used in the calculation."
2973 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2976 ;; Move to the correct then (ignore all "and then")
2978 (while (and (setq match-cons (ada-search-ignore-string-comment
2979 "\\<\\(then\\|and[ \t]*then\\)\\>"
2981 (= (downcase (char-after (car match-cons))) ?a)))
2982 ;; If "then" was found (we are looking at it)
2986 ;; 'then' first in separate line ?
2987 ;; => indent according to 'then',
2988 ;; => else indent according to 'if'
2991 (back-to-indentation)
2992 (looking-at "\\<then\\>"))
2993 (setq cur-indent (save-excursion (back-to-indentation) (point))))
2996 (list cur-indent 'ada-indent))
2998 (list cur-indent 'ada-broken-indent))))
3000 (defun ada-get-indent-block-start (orgpoint)
3001 "Calculate the indentation when point is at the start of a block.
3002 ORGPOINT is the limit position used in the calculation."
3007 (setq pos (ada-goto-next-non-ws orgpoint)))
3010 (ada-indent-on-previous-lines t orgpoint)))
3012 ;; Special case for record types, for instance for:
3013 ;; type A is (B : Integer;
3014 ;; C : Integer) is record
3015 ;; null; -- This is badly indented otherwise
3016 ((looking-at "record")
3018 ;; If record is at the beginning of the line, indent from there
3021 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
3022 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
3024 ;; else indent relative to the type command
3025 (list (save-excursion
3026 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
3029 ;; nothing follows the block-start
3031 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
3033 (defun ada-get-indent-subprog (orgpoint)
3034 "Calculate the indentation when point is just before a subprogram.
3035 ORGPOINT is the limit position used in the calculation."
3036 (let ((match-cons nil)
3037 (cur-indent (save-excursion (back-to-indentation) (point)))
3040 ;; is there an 'is' in front of point ?
3044 (ada-search-ignore-string-comment
3045 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
3047 ;; yes, then skip to its end
3051 (goto-char (cdr match-cons)))
3053 ;; no, then goto next non-ws, if there is one in front of point
3056 (unless (ada-goto-next-non-ws orgpoint)
3057 (goto-char orgpoint))))
3061 ;; nothing follows 'is'
3066 (not (ada-search-ignore-string-comment
3067 "[^ \t\n]" nil orgpoint t))))
3068 (list cur-indent 'ada-indent))
3070 ;; is abstract/separate/new ...
3076 (ada-search-ignore-string-comment
3077 "\\<\\(separate\\|new\\|abstract\\)\\>"
3079 (goto-char (car match-cons))
3080 (ada-search-ignore-string-comment ada-subprog-start-re t)
3081 (ada-get-indent-noindent orgpoint))
3083 ;; something follows 'is'
3087 (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
3088 (goto-char match-cons)
3089 (ada-indent-on-previous-lines t orgpoint)))
3094 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
3095 (list cur-indent 0))
3100 (list cur-indent 'ada-broken-indent)))))
3102 (defun ada-get-indent-noindent (orgpoint)
3103 "Calculate the indentation when point is just before a 'noindent stmt'.
3104 ORGPOINT is the limit position used in the calculation."
3111 ;; This one is called when indenting a line preceded by a multi-line
3112 ;; subprogram declaration (in that case, we are at this point inside
3113 ;; the parameter declaration list)
3114 ((ada-in-paramlist-p)
3115 (ada-previous-procedure)
3116 (list (save-excursion (back-to-indentation) (point)) 0))
3118 ;; This one is called when indenting the second line of a multi-line
3119 ;; declaration section, in a declare block or a record declaration
3120 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3121 (list (save-excursion (back-to-indentation) (point))
3122 'ada-broken-decl-indent))
3124 ;; This one is called in every over case when indenting a line at the
3127 (if (looking-at ada-named-block-re)
3128 (setq label (- ada-label-indent))
3132 ;; "with private" or "null record" cases
3133 (if (or (save-excursion
3134 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
3136 (save-excursion (forward-char -7);; skip back "private"
3137 (ada-goto-previous-word)
3138 (looking-at "with"))))
3140 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
3142 (save-excursion (forward-char -6);; skip back "record"
3143 (ada-goto-previous-word)
3144 (looking-at "null")))))
3147 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
3148 (list (save-excursion (back-to-indentation) (point)) 0)))))
3150 (ada-search-ignore-string-comment ";" nil orgpoint nil
3152 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
3153 (list (+ (save-excursion (back-to-indentation) (point)) label)
3154 'ada-broken-indent)))))))
3156 (defun ada-get-indent-label (orgpoint)
3157 "Calculate the indentation when before a label or variable declaration.
3158 ORGPOINT is the limit position used in the calculation."
3159 (let ((match-cons nil)
3160 (cur-indent (save-excursion (back-to-indentation) (point))))
3161 (ada-search-ignore-string-comment ":" nil)
3165 (setq match-cons (ada-search-ignore-string-comment
3166 ada-loop-start-re nil orgpoint)))
3167 (goto-char (car match-cons))
3168 (ada-get-indent-loop orgpoint))
3172 (setq match-cons (ada-search-ignore-string-comment
3173 "\\<declare\\|begin\\>" nil orgpoint)))
3174 (goto-char (car match-cons))
3175 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3177 ;; variable declaration
3180 (ada-search-ignore-string-comment ";" nil orgpoint))
3182 (list cur-indent 'ada-broken-indent)))
3184 ;; nothing follows colon
3186 (list cur-indent '(- ada-label-indent))))))
3188 (defun ada-get-indent-loop (orgpoint)
3189 "Calculate the indentation when just before a loop or a for ... use.
3190 ORGPOINT is the limit position used in the calculation."
3191 (let ((match-cons nil)
3194 ;; If looking at a named block, skip the label
3195 (label (save-excursion
3197 (if (looking-at ada-named-block-re)
3198 (- ada-label-indent)
3204 ;; statement complete
3207 (ada-search-ignore-string-comment ";" nil orgpoint nil
3209 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
3213 ((looking-at "loop\\>")
3214 (setq pos (ada-get-indent-block-start orgpoint))
3217 (list (+ (car pos) label) (cdr pos))))
3220 ;; 'for'- loop (or also a for ... use statement)
3222 ((looking-at "for\\>")
3229 (goto-char (match-end 0))
3230 (ada-goto-next-non-ws orgpoint)
3232 (if (= (char-after) ?') (forward-word 1) t)
3233 (ada-goto-next-non-ws orgpoint)
3234 (looking-at "\\<use\\>")
3236 ;; check if there is a 'record' before point
3239 (setq match-cons (ada-search-ignore-string-comment
3240 "record" nil orgpoint nil 'word-search-forward))
3244 (goto-char (car match-cons))
3245 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3246 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3253 (setq match-cons (ada-search-ignore-string-comment
3254 "loop" nil orgpoint nil 'word-search-forward)))
3255 (goto-char (car match-cons))
3257 ;; indent according to 'loop', if it's first in the line;
3258 ;; otherwise to 'for'
3260 (unless (save-excursion
3261 (back-to-indentation)
3262 (looking-at "\\<loop\\>"))
3264 (list (+ (save-excursion (back-to-indentation) (point)) label)
3267 ;; for-statement is broken
3270 (list (+ (save-excursion (back-to-indentation) (point)) label)
3271 'ada-broken-indent))))
3276 ((looking-at "while\\>")
3281 (setq match-cons (ada-search-ignore-string-comment
3282 "loop" nil orgpoint nil 'word-search-forward)))
3285 (goto-char (car match-cons))
3287 ;; indent according to 'loop', if it's first in the line;
3288 ;; otherwise to 'while'.
3290 (unless (save-excursion
3291 (back-to-indentation)
3292 (looking-at "\\<loop\\>"))
3294 (list (+ (save-excursion (back-to-indentation) (point)) label)
3297 (list (+ (save-excursion (back-to-indentation) (point)) label)
3298 'ada-broken-indent))))))
3300 (defun ada-get-indent-type (orgpoint)
3301 "Calculate the indentation when before a type statement.
3302 ORGPOINT is the limit position used in the calculation."
3303 (let ((match-dat nil))
3306 ;; complete record declaration
3310 (setq match-dat (ada-search-ignore-string-comment
3311 "end" nil orgpoint nil 'word-search-forward))
3312 (ada-goto-next-non-ws)
3313 (looking-at "\\<record\\>")
3315 (ada-goto-next-non-ws)
3316 (= (char-after) ?\;)))
3317 (goto-char (car match-dat))
3318 (list (save-excursion (back-to-indentation) (point)) 0))
3323 (setq match-dat (ada-search-ignore-string-comment
3324 "record" nil orgpoint nil 'word-search-forward)))
3325 (goto-char (car match-dat))
3326 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3328 ;; complete type declaration
3331 (ada-search-ignore-string-comment ";" nil orgpoint nil
3333 (list (save-excursion (back-to-indentation) (point)) 0))
3335 ;; "type ... is", but not "type ... is ...", which is broken
3339 (ada-search-ignore-string-comment "is" nil orgpoint nil
3340 'word-search-forward)
3341 (not (ada-goto-next-non-ws orgpoint))))
3342 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3347 (list (save-excursion (back-to-indentation) (point))
3348 'ada-broken-indent)))))
3351 ;; -----------------------------------------------------------
3352 ;; -- searching and matching
3353 ;; -----------------------------------------------------------
3355 (defun ada-goto-stmt-start ()
3356 "Move point to the beginning of the statement that point is in or after.
3357 Return the new position of point.
3358 As a special case, if we are looking at a closing parenthesis, skip to the
3360 (let ((match-dat nil)
3363 (setq match-dat (ada-search-prev-end-stmt))
3367 ;; found a previous end-statement => check if anything follows
3369 (unless (looking-at "declare")
3371 (unless (save-excursion
3372 (goto-char (cdr match-dat))
3373 (ada-goto-next-non-ws orgpoint))
3375 ;; nothing follows => it's the end-statement directly in
3376 ;; front of point => search again
3378 (setq match-dat (ada-search-prev-end-stmt)))
3380 ;; if found the correct end-statement => goto next non-ws
3383 (goto-char (cdr match-dat)))
3384 (ada-goto-next-non-ws)
3388 ;; no previous end-statement => we are at the beginning of the
3389 ;; accessible part of the buffer
3392 (goto-char (point-min))
3394 ;; skip to the very first statement, if there is one
3396 (unless (ada-goto-next-non-ws orgpoint)
3397 (goto-char orgpoint))))
3401 (defun ada-search-prev-end-stmt ()
3402 "Move point to previous end statement.
3403 Return a cons cell whose car is the beginning and whose cdr
3404 is the end of the match."
3405 (let ((match-dat nil)
3408 ;; search until found or beginning-of-buffer
3412 (setq match-dat (ada-search-ignore-string-comment
3413 ada-end-stmt-re t)))
3415 (goto-char (car match-dat))
3416 (unless (ada-in-open-paren-p)
3420 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3422 (ada-goto-previous-word)
3423 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3428 (and (save-excursion (ada-goto-previous-word)
3429 (ada-goto-previous-word)
3430 (not (looking-at "subtype")))
3432 (save-excursion (goto-char (cdr match-dat))
3433 (ada-goto-next-non-ws)
3434 ;; words that can go after an 'is'
3439 '("separate" "access" "array"
3440 "abstract" "new") t)
3452 (defun ada-goto-next-non-ws (&optional limit)
3453 "Skip white spaces, newlines and comments to next non-ws character.
3454 Stop the search at LIMIT.
3455 Do not call this function from within a string."
3457 (setq limit (point-max)))
3458 (while (and (<= (point) limit)
3459 (progn (forward-comment 10000)
3460 (if (and (not (eobp))
3461 (save-excursion (forward-char 1)
3463 (progn (forward-sexp 1) t)))))
3464 (if (< (point) limit)
3470 (defun ada-goto-stmt-end (&optional limit)
3471 "Move point to the end of the statement that point is in or before.
3472 Return the new position of point or nil if not found.
3473 Stop the search at LIMIT."
3474 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
3479 (defun ada-goto-next-word (&optional backward)
3480 "Move point to the beginning of the next word of Ada code.
3481 If BACKWARD is non-nil, jump to the beginning of the previous word.
3482 Return the new position of point or nil if not found."
3483 (let ((match-cons nil)
3485 (old-syntax (char-to-string (char-syntax ?_))))
3486 (modify-syntax-entry ?_ "w")
3488 (skip-syntax-forward "w"))
3489 (if (setq match-cons
3491 (ada-search-ignore-string-comment "\\w" t nil t)
3492 (ada-search-ignore-string-comment "\\w" nil nil t)))
3494 ;; move to the beginning of the word found
3497 (goto-char (car match-cons))
3498 (skip-syntax-backward "w")
3501 ;; if not found, restore old position of point
3503 (goto-char orgpoint)
3505 (modify-syntax-entry ?_ old-syntax))
3509 (defun ada-check-matching-start (keyword)
3510 "Signal an error if matching block start is not KEYWORD.
3511 Moves point to the matching block start."
3512 (ada-goto-matching-start 0)
3513 (unless (looking-at (concat "\\<" keyword "\\>"))
3514 (error "Matching start is not '%s'" keyword)))
3517 (defun ada-check-defun-name (defun-name)
3518 "Check if the name of the matching defun really is DEFUN-NAME.
3519 Assumes point to be already positioned by `ada-goto-matching-start'.
3520 Moves point to the beginning of the declaration."
3522 ;; named block without a `declare'
3524 (ada-goto-previous-word)
3525 (looking-at (concat "\\<" defun-name "\\> *:")))
3528 ;; 'accept' or 'package' ?
3530 (unless (looking-at ada-subprog-start-re)
3531 (ada-goto-matching-decl-start))
3533 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3537 ;; a named 'declare'-block ?
3539 (if (looking-at "\\<declare\\>")
3540 (ada-goto-stmt-start)
3542 ;; no, => 'procedure'/'function'/'task'/'protected'
3548 ;; skip 'body' 'type'
3550 (if (looking-at "\\<\\(body\\|type\\)\\>")
3555 ;; should be looking-at the correct name
3557 (unless (looking-at (concat "\\<" defun-name "\\>"))
3558 (error "Matching defun has different name: %s"
3559 (buffer-substring (point)
3560 (progn (forward-sexp 1) (point))))))))
3562 (defun ada-goto-matching-decl-start (&optional noerror recursive)
3563 "Move point to the matching declaration start of the current 'begin'.
3564 If NOERROR is non-nil, it only returns nil if no match was found."
3565 (let ((nest-count 1)
3567 ;; first should be set to t if we should stop at the first
3568 ;; "begin" we encounter.
3569 (first (not recursive))
3574 ;; Ignore "when" most of the time, except if we are looking at the
3575 ;; beginning of a block (structure: case .. is
3579 (if (looking-at "begin")
3580 (setq stop-at-when t))
3583 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3585 (ada-search-ignore-string-comment
3586 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
3587 (looking-at "generic")))
3588 (setq count-generic t))
3590 ;; search backward for interesting keywords
3592 (not (zerop nest-count))
3593 (ada-search-ignore-string-comment ada-matching-decl-start-re t))
3595 ;; calculate nest-depth
3600 (ada-goto-matching-start 1 noerror)
3602 ;; In some case, two begin..end block can follow each other closely,
3603 ;; which we have to detect, as in
3608 ;; begin -- here we should go to procedure, not begin
3611 (if (looking-at "begin")
3612 (let ((loop-again t))
3615 ;; If begin was just there as the beginning of a block
3616 ;; (with no declare) then do nothing, otherwise just
3617 ;; register that we have to find the statement that
3618 ;; required the begin
3620 (ada-search-ignore-string-comment
3621 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3624 (if (looking-at "end")
3625 (ada-goto-matching-start 1 noerror t)
3626 ;; (ada-goto-matching-decl-start noerror t)
3628 (setq loop-again nil)
3629 (unless (looking-at "begin")
3630 (setq nest-count (1+ nest-count))))
3634 ((looking-at "generic")
3638 (setq nest-count (1- nest-count)))))
3643 (unless (looking-at "\\<end[ \t\n]*if\\>")
3645 (setq nest-count (1- nest-count))
3646 (setq first nil)))))
3649 ((looking-at "declare\\|generic")
3650 (setq nest-count (1- nest-count))
3654 ;; check if it is only a type definition, but not a protected
3655 ;; type definition, which should be handled like a procedure.
3656 (if (or (looking-at "is[ \t]+<>")
3658 (forward-comment -10000)
3661 ;; Detect if we have a closing parenthesis (Could be
3662 ;; either the end of subprogram parameters or (<>)
3663 ;; in a type definition
3664 (if (= (char-after) ?\))
3668 (forward-comment -10000)
3670 (skip-chars-backward "a-zA-Z0-9_.'")
3671 (ada-goto-previous-word)
3673 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3675 (ada-goto-previous-word)
3676 (not (looking-at "\\<protected\\>"))))
3678 (goto-char (match-beginning 0))
3680 (setq nest-count (1- nest-count))
3686 (ada-goto-previous-word)
3688 (goto-char (match-beginning 0))))
3691 (looking-at "begin"))
3692 (setq nest-count 0))
3694 ((looking-at "when")
3697 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3700 (setq nest-count (1- nest-count)))
3703 ((looking-at "begin")
3707 (setq nest-count (1+ nest-count))
3712 ;; check if declaration-start is really found
3715 (if (looking-at "is")
3716 (ada-search-ignore-string-comment ada-subprog-start-re t)
3717 (looking-at "declare\\|generic")))
3720 (error "No matching proc/func/task/declare/package/protected")))
3723 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3724 "Move point to the beginning of a block-start.
3725 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3726 If NOERROR is non-nil, it only returns nil if no matching start was found.
3727 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3728 (let ((nest-count (if nest-level nest-level 0))
3732 ;; search backward for interesting keywords
3735 (ada-search-ignore-string-comment ada-matching-start-re t))
3737 (unless (and (looking-at "\\<record\\>")
3740 (looking-at "\\<null\\>")))
3742 ;; calculate nest-depth
3744 ;; found block end => increase nest depth
3746 (setq nest-count (1+ nest-count)))
3748 ;; found loop/select/record/case/if => check if it starts or
3750 ((looking-at "loop\\|select\\|record\\|case\\|if")
3753 ;; check if keyword follows 'end'
3754 (ada-goto-previous-word)
3755 (if (looking-at "\\<end\\>[ \t]*[^;]")
3756 ;; it ends a block => increase nest depth
3757 (setq nest-count (1+ nest-count)
3760 ;; it starts a block => decrease nest depth
3761 (setq nest-count (1- nest-count))))
3764 ;; found package start => check if it really is a block
3765 ((looking-at "package")
3767 ;; ignore if this is just a renames statement
3768 (let ((current (point))
3769 (pos (ada-search-ignore-string-comment
3770 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3772 (goto-char (car pos))
3774 "No matching 'is' or 'renames' for 'package' at"
3776 (number-to-string (count-lines 1 (1+ current)))))))
3777 (unless (looking-at "renames")
3780 (ada-goto-next-non-ws)
3781 ;; ignore it if it is only a declaration with 'new'
3782 ;; We could have package Foo is new ....
3783 ;; or package Foo is separate;
3784 ;; or package Foo is begin null; end Foo
3785 ;; for elaboration code (elaboration)
3786 (if (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3787 (setq nest-count (1- nest-count)))))))
3788 ;; found task start => check if it has a body
3789 ((looking-at "task")
3792 (ada-goto-next-non-ws)
3794 ((looking-at "\\<body\\>"))
3795 ((looking-at "\\<type\\>")
3796 ;; In that case, do nothing if there is a "is"
3797 (forward-word 2);; skip "type"
3798 (ada-goto-next-non-ws);; skip type name
3800 ;; Do nothing if we are simply looking at a simple
3801 ;; "task type name;" statement with no block
3802 (unless (looking-at ";")
3804 ;; Skip the parameters
3805 (if (looking-at "(")
3806 (ada-search-ignore-string-comment ")" nil))
3807 (let ((tmp (ada-search-ignore-string-comment
3808 "\\<\\(is\\|;\\)\\>" nil)))
3811 (goto-char (car tmp))
3812 (if (looking-at "is")
3813 (setq nest-count (1- nest-count)))))))))
3815 ;; Check if that task declaration had a block attached to
3816 ;; it (i.e do nothing if we have just "task name;")
3817 (unless (progn (forward-word 1)
3818 (looking-at "[ \t]*;"))
3819 (setq nest-count (1- nest-count)))))))
3820 ;; all the other block starts
3822 (setq nest-count (1- nest-count)))) ; end of 'cond'
3824 ;; 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 no matching start found."
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 Returns a cons cell of begin and end of match data or nil, if not found.
3975 If BACKWARD is non-nil, search backward; search forward otherwise.
3976 The search stops at pos LIMIT.
3977 If PARAMLISTS is nil, ignore parameter lists.
3978 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
3979 in case we are searching for a constant string.
3980 Point is moved at the beginning of the SEARCH-RE."
3985 (previous-syntax-table (syntax-table)))
3987 ;; FIXME: need to pass BACKWARD to search-func!
3989 (setq search-func (if backward 're-search-backward 're-search-forward)))
3992 ;; search until found or end-of-buffer
3993 ;; We have to test that we do not look further than limit
3995 (set-syntax-table ada-mode-symbol-syntax-table)
3996 (while (and (not found)
3998 (or (and backward (<= limit (point)))
3999 (>= limit (point))))
4000 (funcall search-func search-re limit 1))
4001 (setq begin (match-beginning 0))
4002 (setq end (match-end 0))
4004 (setq parse-result (parse-partial-sexp
4005 (save-excursion (beginning-of-line) (point))
4010 ;; If inside a string, skip it (and the following comments)
4012 ((ada-in-string-p parse-result)
4013 (if (featurep 'xemacs)
4014 (search-backward "\"" nil t)
4015 (goto-char (nth 8 parse-result)))
4016 (unless backward (forward-sexp 1)))
4018 ;; If inside a comment, skip it (and the following comments)
4019 ;; There is a special code for comments at the end of the file
4021 ((ada-in-comment-p parse-result)
4022 (if (featurep 'xemacs)
4026 (forward-comment -1))
4027 (goto-char (nth 8 parse-result)))
4029 ;; at the end of the file, it is not possible to skip a comment
4030 ;; so we just go at the end of the line
4031 (if (forward-comment 1)
4033 (forward-comment 1000)
4034 (beginning-of-line))
4037 ;; directly in front of a comment => skip it, if searching forward
4039 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
4040 (unless backward (progn (forward-char -1) (forward-comment 1000))))
4043 ;; found a parameter-list but should ignore it => skip it
4045 ((and (not paramlists) (ada-in-paramlist-p))
4047 (search-backward "(" nil t)
4048 (search-forward ")" nil t)))
4050 ;; found what we were looking for
4053 (setq found t)))) ; end of loop
4055 (set-syntax-table previous-syntax-table)
4061 ;; -------------------------------------------------------
4062 ;; -- Testing the position of the cursor
4063 ;; -------------------------------------------------------
4065 (defun ada-in-decl-p ()
4066 "Return t if point is inside a declarative part.
4067 Assumes point to be at the end of a statement."
4068 (or (ada-in-paramlist-p)
4070 (ada-goto-matching-decl-start t))))
4073 (defun ada-looking-at-semi-or ()
4074 "Return t if looking at an 'or' following a semicolon."
4076 (and (looking-at "\\<or\\>")
4079 (ada-goto-stmt-start)
4080 (looking-at "\\<or\\>")))))
4083 (defun ada-looking-at-semi-private ()
4084 "Return t if looking at the start of a private section in a package.
4085 Return nil if the private is part of the package name, as in
4086 'private package A is...' (this can only happen at top level)."
4088 (and (looking-at "\\<private\\>")
4089 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4091 ;; Make sure this is the start of a private section (ie after
4092 ;; a semicolon or just after the package declaration, but not
4093 ;; after a 'type ... is private' or 'is new ... with private'
4095 ;; Note that a 'private' statement at the beginning of the buffer
4096 ;; does not indicate a private section, since this is instead a
4097 ;; 'private procedure ...'
4098 (progn (forward-comment -1000)
4100 (or (= (char-before) ?\;)
4101 (and (forward-word -3)
4102 (looking-at "\\<package\\>"))))))))
4105 (defun ada-in-paramlist-p ()
4106 "Return t if point is inside a parameter-list."
4109 (ada-search-ignore-string-comment "(\\|)" t nil t)
4110 ;; inside parentheses ?
4111 (= (char-after) ?\()
4113 ;; We could be looking at two things here:
4114 ;; operator definition: function "." (
4115 ;; subprogram definition: procedure .... (
4116 ;; Let's skip back over the first one
4118 (skip-chars-backward " \t\n")
4119 (if (= (char-before) ?\")
4124 ;; and now over the second one
4127 ;; We should ignore the case when the reserved keyword is in a
4128 ;; comment (for instance, when we have:
4131 ;; we should return nil
4133 (not (ada-in-string-or-comment-p))
4135 ;; right keyword two words before parenthesis ?
4136 ;; Type is in this list because of discriminants
4137 (looking-at (eval-when-compile
4139 "procedure\\|function\\|body\\|"
4140 "task\\|entry\\|accept\\|"
4141 "access[ \t]+procedure\\|"
4142 "access[ \t]+function\\|"
4146 (defun ada-search-ignore-complex-boolean (regexp backwardp)
4147 "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
4148 If BACKWARDP is non-nil, search backward; search forward otherwise."
4150 (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
4151 (save-excursion (forward-word -1)
4152 (looking-at "and then\\|or else"))))
4155 (defun ada-in-open-paren-p ()
4156 "Non-nil if in an open parenthesis.
4157 Return value is the position of the first non-ws behind the last unclosed
4158 parenthesis, or nil."
4160 (let ((parse (parse-partial-sexp
4162 (or (car (ada-search-ignore-complex-boolean
4163 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4169 (goto-char (1+ (nth 1 parse)))
4171 ;; Skip blanks, if they are not followed by a comment
4173 ;; type A is ( Value_0,
4175 ;; type B is ( -- comment
4178 (if (or (not ada-indent-handle-comment-special)
4179 (not (looking-at "[ \t]+--")))
4180 (skip-chars-forward " \t"))
4185 ;; -----------------------------------------------------------
4186 ;; -- Behavior Of TAB Key
4187 ;; -----------------------------------------------------------
4190 "Do indenting or tabbing according to `ada-tab-policy'.
4191 In Transient Mark mode, if the mark is active, operate on the contents
4192 of the region. Otherwise, operate only on the current line."
4194 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
4195 ((eq ada-tab-policy 'indent-auto)
4196 (if (ada-region-selected)
4197 (ada-indent-region (region-beginning) (region-end))
4198 (ada-indent-current)))
4199 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4202 (defun ada-untab (arg)
4203 "Delete leading indenting according to `ada-tab-policy'."
4204 ;; FIXME: ARG is ignored
4206 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
4207 ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
4208 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4211 (defun ada-indent-current-function ()
4212 "Ada mode version of the `indent-line-function'."
4214 (let ((starting-point (point-marker)))
4217 (if (< (point) starting-point)
4218 (goto-char starting-point))
4219 (set-marker starting-point nil)
4222 (defun ada-tab-hard ()
4223 "Indent current line to next tab stop."
4227 (insert-char ? ada-indent))
4228 (if (save-excursion (= (point) (progn (beginning-of-line) (point))))
4229 (forward-char ada-indent)))
4231 (defun ada-untab-hard ()
4232 "Indent current line to previous tab stop."
4234 (let ((bol (save-excursion (progn (beginning-of-line) (point))))
4235 (eol (save-excursion (progn (end-of-line) (point)))))
4236 (indent-rigidly bol eol (- 0 ada-indent))))
4240 ;; ------------------------------------------------------------
4242 ;; ------------------------------------------------------------
4244 ;; Not needed any more for Emacs 21.2, but still needed for backward
4246 (defun ada-remove-trailing-spaces ()
4247 "Remove trailing spaces in the whole buffer."
4253 (goto-char (point-min))
4254 (while (re-search-forward "[ \t]+$" (point-max) t)
4255 (replace-match "" nil nil))))))
4257 (defun ada-gnat-style ()
4258 "Clean up comments, `(' and `,' for GNAT style checking switch."
4262 ;; The \n is required, or the line after an empty comment line is
4264 (goto-char (point-min))
4265 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
4266 (replace-match "-- \\1")
4268 (beginning-of-line))
4270 (goto-char (point-min))
4271 (while (re-search-forward "\\>(" nil t)
4272 (if (not (ada-in-string-or-comment-p))
4273 (replace-match " (")))
4274 (goto-char (point-min))
4275 (while (re-search-forward ";--" nil t)
4277 (if (not (ada-in-string-or-comment-p))
4278 (replace-match "; --")))
4279 (goto-char (point-min))
4280 (while (re-search-forward "([ \t]+" nil t)
4281 (if (not (ada-in-string-or-comment-p))
4282 (replace-match "(")))
4283 (goto-char (point-min))
4284 (while (re-search-forward ")[ \t]+)" nil t)
4285 (if (not (ada-in-string-or-comment-p))
4286 (replace-match "))")))
4287 (goto-char (point-min))
4288 (while (re-search-forward "\\>:" nil t)
4289 (if (not (ada-in-string-or-comment-p))
4290 (replace-match " :")))
4292 ;; Make sure there is a space after a ','.
4293 ;; Always go back to the beginning of the match, since otherwise
4294 ;; a statement like ('F','D','E') is incorrectly modified.
4295 (goto-char (point-min))
4296 (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
4297 (if (not (save-excursion
4298 (goto-char (match-beginning 0))
4299 (ada-in-string-or-comment-p)))
4300 (replace-match ", \\1")))
4302 ;; Operators should be surrounded by spaces.
4303 (goto-char (point-min))
4304 (while (re-search-forward
4305 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4307 (goto-char (match-beginning 1))
4308 (if (or (looking-at "--")
4309 (ada-in-string-or-comment-p))
4312 (beginning-of-line))
4314 ((string= (match-string 1) "/=")
4315 (replace-match " /= "))
4316 ((string= (match-string 1) "..")
4317 (replace-match " .. "))
4318 ((string= (match-string 1) "**")
4319 (replace-match " ** "))
4320 ((string= (match-string 1) ":=")
4321 (replace-match " := "))
4323 (replace-match " \\1 ")))
4329 ;; -------------------------------------------------------------
4330 ;; -- Moving To Procedures/Packages/Statements
4331 ;; -------------------------------------------------------------
4333 (defun ada-move-to-start ()
4334 "Move point to the matching start of the current Ada structure."
4337 (previous-syntax-table (syntax-table)))
4340 (set-syntax-table ada-mode-symbol-syntax-table)
4344 ;; do nothing if in string or comment or not on 'end ...;'
4345 ;; or if an error occurs during processing
4348 (ada-in-string-or-comment-p)
4350 (or (looking-at "[ \t]*\\<end\\>")
4352 (or (looking-at "[ \t]*\\<end\\>")
4354 (or (looking-at "[ \t]*\\<end\\>")
4355 (error "Not on end ...;")))
4356 (ada-goto-matching-start 1)
4360 ;; on 'begin' => go on, according to user option
4362 ada-move-to-declaration
4363 (looking-at "\\<begin\\>")
4364 (ada-goto-matching-decl-start)
4365 (setq pos (point))))
4367 ) ; end of save-excursion
4369 ;; now really move to the found position
4372 ;; restore syntax-table
4373 (set-syntax-table previous-syntax-table))))
4375 (defun ada-move-to-end ()
4376 "Move point to the matching end of the block around point.
4377 Moves to 'begin' if in a declarative part."
4381 (previous-syntax-table (syntax-table)))
4384 (set-syntax-table ada-mode-symbol-syntax-table)
4389 ;; Go to the beginning of the current word, and check if we are
4390 ;; directly on 'begin'
4392 (skip-syntax-backward "w")
4393 (looking-at "\\<begin\\>"))
4394 (ada-goto-matching-end 1)
4397 ;; on first line of subprogram body
4398 ;; Do nothing for specs or generic instantion, since these are
4399 ;; handled as the general case (find the enclosing block)
4400 ;; We also need to make sure that we ignore nested subprograms
4402 (and (skip-syntax-backward "w")
4403 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4404 (ada-search-ignore-string-comment "is\\|;")
4405 (not (= (char-before) ?\;))
4407 (skip-syntax-backward "w")
4408 (ada-goto-matching-end 0 t))
4410 ;; on first line of task declaration
4412 (and (ada-goto-stmt-start)
4413 (looking-at "\\<task\\>" )
4415 (ada-goto-next-non-ws)
4416 (looking-at "\\<body\\>")))
4417 (ada-search-ignore-string-comment "begin" nil nil nil
4418 'word-search-forward))
4419 ;; accept block start
4421 (and (ada-goto-stmt-start)
4422 (looking-at "\\<accept\\>" )))
4423 (ada-goto-matching-end 0))
4426 (setq decl-start (and (ada-goto-matching-decl-start t) (point)))
4427 (and decl-start (looking-at "\\<package\\>")))
4428 (ada-goto-matching-end 1))
4430 ;; On a "declare" keyword
4432 (skip-syntax-backward "w")
4433 (looking-at "\\<declare\\>"))
4434 (ada-goto-matching-end 0 t))
4436 ;; inside a 'begin' ... 'end' block
4438 (goto-char decl-start)
4439 (ada-goto-matching-end 0 t))
4441 ;; (hopefully ;-) everything else
4443 (ada-goto-matching-end 1)))
4447 ;; now really move to the position found
4450 ;; restore syntax-table
4451 (set-syntax-table previous-syntax-table))))
4453 (defun ada-next-procedure ()
4454 "Move point to next procedure."
4457 (if (re-search-forward ada-procedure-start-regexp nil t)
4458 (goto-char (match-beginning 4))
4459 (error "No more functions/procedures/tasks")))
4461 (defun ada-previous-procedure ()
4462 "Move point to previous procedure."
4465 (if (re-search-backward ada-procedure-start-regexp nil t)
4466 (goto-char (match-beginning 4))
4467 (error "No more functions/procedures/tasks")))
4469 (defun ada-next-package ()
4470 "Move point to next package."
4473 (if (re-search-forward ada-package-start-regexp nil t)
4474 (goto-char (match-beginning 1))
4475 (error "No more packages")))
4477 (defun ada-previous-package ()
4478 "Move point to previous package."
4481 (if (re-search-backward ada-package-start-regexp nil t)
4482 (goto-char (match-beginning 1))
4483 (error "No more packages")))
4486 ;; ------------------------------------------------------------
4487 ;; -- Define keymap and menus for Ada
4488 ;; -------------------------------------------------------------
4490 (defun ada-create-keymap ()
4491 "Create the keymap associated with the Ada mode."
4493 ;; Indentation and Formatting
4494 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
4495 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
4496 (define-key ada-mode-map "\t" 'ada-tab)
4497 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
4498 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
4499 (if (featurep 'xemacs)
4500 (define-key ada-mode-map '(shift tab) 'ada-untab)
4501 (define-key ada-mode-map [(shift tab)] 'ada-untab))
4502 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
4503 ;; We don't want to make meta-characters case-specific.
4506 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
4507 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
4508 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
4509 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
4512 (unless (lookup-key ada-mode-map "\C-c\C-c")
4513 (define-key ada-mode-map "\C-c\C-c" 'compile))
4516 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
4517 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
4518 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
4519 (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
4521 ;; On XEmacs, you can easily specify whether DEL should deletes
4522 ;; one character forward or one character backward. Take this into
4524 (if (boundp 'delete-key-deletes-forward)
4525 (define-key ada-mode-map [backspace] 'backward-delete-char-untabify)
4526 (define-key ada-mode-map "\177" 'backward-delete-char-untabify))
4529 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4531 ;; Use predefined function of Emacs19 for comments (RE)
4532 (define-key ada-mode-map "\C-c;" 'comment-region)
4533 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4535 ;; The following keys are bound to functions defined in ada-xref.el or
4536 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4537 ;; and activated only if the right compiler is used
4538 (if (featurep 'xemacs)
4540 (define-key ada-mode-map '(shift button3) 'ada-point-and-xref)
4541 (define-key ada-mode-map '(control tab) 'ada-complete-identifier))
4542 (define-key ada-mode-map [C-tab] 'ada-complete-identifier)
4543 (define-key ada-mode-map [S-mouse-3] 'ada-point-and-xref))
4545 (define-key ada-mode-map "\C-co" 'ff-find-other-file)
4546 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4547 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4548 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4549 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4550 (define-key ada-mode-map "\C-cc" 'ada-change-prj)
4551 (define-key ada-mode-map "\C-cd" 'ada-set-default-project-file)
4552 (define-key ada-mode-map "\C-cg" 'ada-gdb-application)
4553 (define-key ada-mode-map "\C-cr" 'ada-run-application)
4554 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4555 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4556 (define-key ada-mode-map "\C-cl" 'ada-find-local-references)
4557 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4558 (define-key ada-mode-map "\C-cf" 'ada-find-file)
4560 (define-key ada-mode-map "\C-cu" 'ada-prj-edit)
4562 ;; The templates, defined in ada-stmt.el
4564 (let ((map (make-sparse-keymap)))
4565 (define-key map "h" 'ada-header)
4566 (define-key map "\C-a" 'ada-array)
4567 (define-key map "b" 'ada-exception-block)
4568 (define-key map "d" 'ada-declare-block)
4569 (define-key map "c" 'ada-case)
4570 (define-key map "\C-e" 'ada-elsif)
4571 (define-key map "e" 'ada-else)
4572 (define-key map "\C-k" 'ada-package-spec)
4573 (define-key map "k" 'ada-package-body)
4574 (define-key map "\C-p" 'ada-procedure-spec)
4575 (define-key map "p" 'ada-subprogram-body)
4576 (define-key map "\C-f" 'ada-function-spec)
4577 (define-key map "f" 'ada-for-loop)
4578 (define-key map "i" 'ada-if)
4579 (define-key map "l" 'ada-loop)
4580 (define-key map "\C-r" 'ada-record)
4581 (define-key map "\C-s" 'ada-subtype)
4582 (define-key map "S" 'ada-tabsize)
4583 (define-key map "\C-t" 'ada-task-spec)
4584 (define-key map "t" 'ada-task-body)
4585 (define-key map "\C-y" 'ada-type)
4586 (define-key map "\C-v" 'ada-private)
4587 (define-key map "u" 'ada-use)
4588 (define-key map "\C-u" 'ada-with)
4589 (define-key map "\C-w" 'ada-when)
4590 (define-key map "w" 'ada-while-loop)
4591 (define-key map "\C-x" 'ada-exception)
4592 (define-key map "x" 'ada-exit)
4593 (define-key ada-mode-map "\C-ct" map))
4597 (defun ada-create-menu ()
4598 "Create the Ada menu as shown in the menu bar."
4601 ["Ada Mode" (info "ada-mode") t]
4602 ["GNAT User's Guide" (info "gnat_ugn")
4603 (eq ada-which-compiler 'gnat)]
4604 ["GNAT Reference Manual" (info "gnat_rm")
4605 (eq ada-which-compiler 'gnat)]
4606 ["Gcc Documentation" (info "gcc")
4607 (eq ada-which-compiler 'gnat)]
4608 ["Gdb Documentation" (info "gdb")
4609 (eq ada-which-compiler 'gnat)]
4610 ["Ada95 Reference Manual" (info "arm95")
4611 (eq ada-which-compiler 'gnat)])
4612 ("Options" :included (eq major-mode 'ada-mode)
4613 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4614 :style toggle :selected ada-auto-case]
4615 ["Auto Indent After Return"
4616 (setq ada-indent-after-return (not ada-indent-after-return))
4617 :style toggle :selected ada-indent-after-return]
4618 ["Automatically Recompile For Cross-references"
4619 (setq ada-xref-create-ali (not ada-xref-create-ali))
4620 :style toggle :selected ada-xref-create-ali
4621 :included (eq ada-which-compiler 'gnat)]
4623 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4624 :style toggle :selected ada-xref-confirm-compile
4625 :included (eq ada-which-compiler 'gnat)]
4626 ["Show Cross-references In Other Buffer"
4627 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4628 :style toggle :selected ada-xref-other-buffer
4629 :included (eq ada-which-compiler 'gnat)]
4630 ["Tight Integration With GNU Visual Debugger"
4631 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4632 :style toggle :selected ada-tight-gvd-integration
4633 :included (string-match "gvd" ada-prj-default-debugger)])
4634 ["Customize" (customize-group 'ada)
4635 :included (fboundp 'customize-group)]
4636 ["Check file" ada-check-current t]
4637 ["Compile file" ada-compile-current t]
4638 ["Build" ada-compile-application t]
4639 ["Run" ada-run-application t]
4640 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4643 ["Load..." ada-set-default-project-file t]
4644 ["New..." ada-prj-new t]
4645 ["Edit..." ada-prj-edit t])
4646 ("Goto" :included (eq major-mode 'ada-mode)
4647 ["Goto Declaration/Body" ada-goto-declaration
4648 (eq ada-which-compiler 'gnat)]
4649 ["Goto Body" ada-goto-body
4650 (eq ada-which-compiler 'gnat)]
4651 ["Goto Declaration Other Frame"
4652 ada-goto-declaration-other-frame
4653 (eq ada-which-compiler 'gnat)]
4654 ["Goto Previous Reference" ada-xref-goto-previous-reference
4655 (eq ada-which-compiler 'gnat)]
4656 ["List Local References" ada-find-local-references
4657 (eq ada-which-compiler 'gnat)]
4658 ["List References" ada-find-references
4659 (eq ada-which-compiler 'gnat)]
4660 ["Goto Reference To Any Entity" ada-find-any-references
4661 (eq ada-which-compiler 'gnat)]
4662 ["Goto Parent Unit" ada-goto-parent
4663 (eq ada-which-compiler 'gnat)]
4665 ["Next compilation error" next-error t]
4666 ["Previous Package" ada-previous-package t]
4667 ["Next Package" ada-next-package t]
4668 ["Previous Procedure" ada-previous-procedure t]
4669 ["Next Procedure" ada-next-procedure t]
4670 ["Goto Start Of Statement" ada-move-to-start t]
4671 ["Goto End Of Statement" ada-move-to-end t]
4673 ["Other File" ff-find-other-file t]
4674 ["Other File Other Window" ada-ff-other-window t])
4675 ("Edit" :included (eq major-mode 'ada-mode)
4676 ["Search File On Source Path" ada-find-file t]
4678 ["Complete Identifier" ada-complete-identifier t]
4680 ["Indent Line" ada-indent-current-function t]
4681 ["Justify Current Indentation" ada-justified-indent-current t]
4682 ["Indent Lines in Selection" ada-indent-region t]
4683 ["Indent Lines in File"
4684 (ada-indent-region (point-min) (point-max)) t]
4685 ["Format Parameter List" ada-format-paramlist t]
4687 ["Comment Selection" comment-region t]
4688 ["Uncomment Selection" ada-uncomment-region t]
4690 ["Fill Comment Paragraph" fill-paragraph t]
4691 ["Fill Comment Paragraph Justify"
4692 ada-fill-comment-paragraph-justify t]
4693 ["Fill Comment Paragraph Postfix"
4694 ada-fill-comment-paragraph-postfix t]
4696 ["Adjust Case Selection" ada-adjust-case-region t]
4697 ["Adjust Case in File" ada-adjust-case-buffer t]
4698 ["Create Case Exception" ada-create-case-exception t]
4699 ["Create Case Exception Substring"
4700 ada-create-case-exception-substring t]
4701 ["Reload Case Exceptions" ada-case-read-exceptions t]
4703 ["Make body for subprogram" ada-make-subprogram-body t]
4705 ["Narrow to subprogram" ada-narrow-to-defun t])
4707 :included (eq major-mode 'ada-mode)
4708 ["Header" ada-header t]
4710 ["Package Body" ada-package-body t]
4711 ["Package Spec" ada-package-spec t]
4712 ["Function Spec" ada-function-spec t]
4713 ["Procedure Spec" ada-procedure-spec t]
4714 ["Proc/func Body" ada-subprogram-body t]
4715 ["Task Body" ada-task-body t]
4716 ["Task Spec" ada-task-spec t]
4717 ["Declare Block" ada-declare-block t]
4718 ["Exception Block" ada-exception-block t]
4720 ["Entry" ada-entry t]
4721 ["Entry family" ada-entry-family t]
4722 ["Select" ada-select t]
4723 ["Accept" ada-accept t]
4724 ["Or accept" ada-or-accep t]
4725 ["Or delay" ada-or-delay t]
4726 ["Or terminate" ada-or-terminate t]
4729 ["Private" ada-private t]
4730 ["Subtype" ada-subtype t]
4731 ["Record" ada-record t]
4732 ["Array" ada-array t]
4736 ["Elsif" ada-elsif t]
4739 ["While Loop" ada-while-loop t]
4740 ["For Loop" ada-for-loop t]
4743 ["Exception" ada-exception t]
4745 ["When" ada-when t])
4748 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4749 (if (featurep 'xemacs)
4751 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4752 (set 'mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4755 ;; -------------------------------------------------------
4756 ;; Commenting/Uncommenting code
4757 ;; The following two calls are provided to enhance the standard
4758 ;; comment-region function, which only allows uncommenting if the
4759 ;; comment is at the beginning of a line. If the line have been re-indented,
4760 ;; we are unable to use comment-region, which makes no sense.
4762 ;; In addition, we provide an interface to the standard comment handling
4763 ;; function for justifying the comments.
4764 ;; -------------------------------------------------------
4766 (defadvice comment-region (before ada-uncomment-anywhere disable)
4768 (listp arg) ;; a prefix with \C-u is of the form '(4), whereas
4769 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4770 (string= mode-name "Ada"))
4772 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4774 (while (re-search-forward cs end t)
4775 (replace-match comment-start))
4778 (defun ada-uncomment-region (beg end &optional arg)
4779 "Uncomment region BEG .. END.
4780 ARG gives number of comment characters."
4781 (interactive "r\nP")
4783 ;; This advice is not needed anymore with Emacs21. However, for older
4784 ;; versions, as well as for XEmacs, we still need to enable it.
4785 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4787 (ad-activate 'comment-region)
4788 (comment-region beg end (- (or arg 2)))
4789 (ad-deactivate 'comment-region))
4790 (comment-region beg end (list (- (or arg 2))))
4791 (ada-indent-region beg end)))
4793 (defun ada-fill-comment-paragraph-justify ()
4794 "Fill current comment paragraph and justify each line as well."
4796 (ada-fill-comment-paragraph 'full))
4798 (defun ada-fill-comment-paragraph-postfix ()
4799 "Fill current comment paragraph and justify each line as well.
4800 Adds `ada-fill-comment-postfix' at the end of each line."
4802 (ada-fill-comment-paragraph 'full t))
4804 (defun ada-fill-comment-paragraph (&optional justify postfix)
4805 "Fill the current comment paragraph.
4806 If JUSTIFY is non-nil, each line is justified as well.
4807 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4808 to each line filled and justified.
4809 The paragraph is indented on the first line."
4812 ;; check if inside comment or just in front a comment
4813 (if (and (not (ada-in-comment-p))
4814 (not (looking-at "[ \t]*--")))
4815 (error "Not inside comment"))
4817 (let* (indent from to
4818 (opos (point-marker))
4820 ;; Sets this variable to nil, otherwise it prevents
4821 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4822 (parse-sexp-lookup-properties nil)
4825 (fill-column (current-fill-column)))
4827 ;; Find end of paragraph
4828 (back-to-indentation)
4829 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4832 ;; If we were at the last line in the buffer, create a dummy empty
4833 ;; line at the end of the buffer.
4836 (back-to-indentation)))
4838 (setq to (point-marker))
4841 ;; Find beginning of paragraph
4842 (back-to-indentation)
4843 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4845 (back-to-indentation))
4847 ;; We want one line above the first one, unless we are at the beginning
4852 (setq from (point-marker))
4854 ;; Calculate the indentation we will need for the paragraph
4855 (back-to-indentation)
4856 (setq indent (current-column))
4857 ;; unindent the first line of the paragraph
4858 (delete-region from (point))
4860 ;; Remove the old postfixes
4862 (while (re-search-forward "--\n" to t)
4863 (replace-match "\n"))
4866 (setq to (point-marker))
4868 ;; Indent and justify the paragraph
4869 (setq fill-prefix ada-fill-comment-prefix)
4870 (set-left-margin from to indent)
4872 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4874 (fill-region-as-paragraph from to justify)
4876 ;; Add the postfixes if required
4880 (narrow-to-region from to)
4883 (insert-char ? (- fill-column (current-column)))
4884 (insert ada-fill-comment-postfix)
4888 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4889 ;; inserted at the end. Delete it
4890 (if (or (featurep 'xemacs)
4891 (<= emacs-major-version 19)
4892 (and (= emacs-major-version 20)
4893 (<= emacs-minor-version 2)))
4902 ;; ---------------------------------------------------
4903 ;; support for find-file.el
4904 ;; These functions are used by find-file to guess the file names from
4905 ;; unit names, and to find the other file (spec or body) from the current
4906 ;; file (body or spec).
4907 ;; It is also used to find in which function we are, so as to put the
4908 ;; cursor at the correct position.
4909 ;; Standard Ada does not force any relation between unit names and file names,
4910 ;; so some of these functions can only be a good approximation. However, they
4911 ;; are also overriden in `ada-xref'.el when we know that the user is using
4913 ;; ---------------------------------------------------
4915 ;; Overriden when we work with GNAT, to use gnatkrunch
4916 (defun ada-make-filename-from-adaname (adaname)
4917 "Determine the filename in which ADANAME is found.
4918 This matches the GNAT default naming convention, except for
4920 (while (string-match "\\." adaname)
4921 (setq adaname (replace-match "-" t t adaname)))
4925 (defun ada-other-file-name ()
4926 "Return the name of the other file.
4927 The name returned is the body if `current-buffer' is the spec,
4928 or the spec otherwise."
4932 (suffixes ada-spec-suffixes)
4933 (name (buffer-file-name)))
4935 ;; Guess whether we have a spec or a body, and get the basename of the
4936 ;; file. Since the extension may not start with '.', we can not use
4937 ;; file-name-extension
4938 (while (and (not is-spec)
4940 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4942 name (match-string 1 name)))
4943 (setq suffixes (cdr suffixes)))
4947 (setq suffixes ada-body-suffixes)
4948 (while (and (not is-body)
4950 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4952 name (match-string 1 name)))
4953 (setq suffixes (cdr suffixes)))))
4955 ;; If this wasn't in either list, return name itself
4956 (if (not (or is-spec is-body))
4959 ;; Else find the other possible names
4961 (setq suffixes ada-body-suffixes)
4962 (setq suffixes ada-spec-suffixes))
4967 ;; If we are using project file, search for the other file in all
4968 ;; the possible src directories.
4970 (if (fboundp 'ada-find-src-file-in-dir)
4972 (ada-find-src-file-in-dir
4973 (file-name-nondirectory (concat name (car suffixes))))))
4975 (set 'is-spec other)))
4977 ;; Else search in the current directory
4978 (if (file-exists-p (concat name (car suffixes)))
4979 (setq is-spec (concat name (car suffixes)))))
4980 (setq suffixes (cdr suffixes)))
4984 (defun ada-which-function-are-we-in ()
4985 "Return the name of the function whose definition/declaration point is in.
4986 Used in `ff-pre-load-hook'."
4987 (setq ff-function-name nil)
4989 (end-of-line);; make sure we get the complete name
4990 (or (if (re-search-backward ada-procedure-start-regexp nil t)
4991 (setq ff-function-name (match-string 5)))
4992 (if (re-search-backward ada-package-start-regexp nil t)
4993 (setq ff-function-name (match-string 4))))
4997 (defvar ada-last-which-function-line -1
4998 "Last line on which `ada-which-function' was called.")
4999 (defvar ada-last-which-function-subprog 0
5000 "Last subprogram name returned by `ada-which-function'.")
5001 (make-variable-buffer-local 'ada-last-which-function-subprog)
5002 (make-variable-buffer-local 'ada-last-which-function-line)
5005 (defun ada-which-function ()
5006 "Return the name of the function whose body the point is in.
5007 This function works even in the case of nested subprograms, whereas the
5008 standard Emacs function `which-function' does not.
5009 Since the search can be long, the results are cached."
5011 (let ((line (count-lines 1 (point)))
5017 ;; If this is the same line as before, simply return the same result
5018 (if (= line ada-last-which-function-line)
5019 ada-last-which-function-subprog
5022 ;; In case the current line is also the beginning of the body
5025 ;; Are we looking at "function Foo\n (paramlist)"
5026 (skip-chars-forward " \t\n(")
5032 (skip-chars-forward " \t\n")
5033 (if (looking-at "return")
5036 (skip-chars-forward " \t\n")
5037 (skip-chars-forward "a-zA-Z0-9_'")))
5039 ;; Can't simply do forward-word, in case the "is" is not on the
5040 ;; same line as the closing parenthesis
5041 (skip-chars-forward "is \t\n")
5043 ;; No look for the closest subprogram body that has not ended yet.
5044 ;; Not that we expect all the bodies to be finished by "end <name>",
5045 ;; or a simple "end;" indented in the same column as the start of
5046 ;; the subprogram. The goal is to be as efficient as possible.
5048 (while (and (not found)
5049 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5051 ;; Get the function name, but not the properties, or this changes
5052 ;; the face in the modeline on Emacs 21
5053 (setq func-name (match-string-no-properties 2))
5054 (if (and (not (ada-in-comment-p))
5055 (not (save-excursion
5056 (goto-char (match-end 0))
5057 (looking-at "[ \t\n]*new"))))
5059 (back-to-indentation)
5060 (setq indent (current-column))
5061 (if (ada-search-ignore-string-comment
5062 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5063 (make-string indent ? ) "end;"))
5064 (setq end-pos (point))
5065 (setq end-pos (point-max)))
5066 (if (>= end-pos pos)
5067 (setq found func-name))))
5069 (setq ada-last-which-function-line line
5070 ada-last-which-function-subprog found)
5073 (defun ada-ff-other-window ()
5074 "Find other file in other window using `ff-find-other-file'."
5076 (and (fboundp 'ff-find-other-file)
5077 (ff-find-other-file t)))
5079 (defun ada-set-point-accordingly ()
5080 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5081 (if ff-function-name
5083 (goto-char (point-min))
5084 (unless (ada-search-ignore-string-comment
5085 (concat ff-function-name "\\b") nil)
5086 (goto-char (point-min))))))
5088 (defun ada-get-body-name (&optional spec-name)
5089 "Return the file name for the body of SPEC-NAME.
5090 If SPEC-NAME is nil, return the body for the current package.
5091 Return nil if no body was found."
5094 (unless spec-name (setq spec-name (buffer-file-name)))
5096 ;; Remove the spec extension. We can not simply remove the file extension,
5097 ;; but we need to take into account the specific non-GNAT extensions that the
5098 ;; user might have specified.
5100 (let ((suffixes ada-spec-suffixes)
5103 (setq end (- (length spec-name) (length (car suffixes))))
5104 (if (string-equal (car suffixes) (substring spec-name end))
5105 (setq spec-name (substring spec-name 0 end)))
5106 (setq suffixes (cdr suffixes))))
5108 ;; If find-file.el was available, use its functions
5109 (if (fboundp 'ff-get-file-name)
5110 (ff-get-file-name ada-search-directories-internal
5111 (ada-make-filename-from-adaname
5112 (file-name-nondirectory
5113 (file-name-sans-extension spec-name)))
5115 ;; Else emulate it very simply
5116 (concat (ada-make-filename-from-adaname
5117 (file-name-nondirectory
5118 (file-name-sans-extension spec-name)))
5122 ;; ---------------------------------------------------
5123 ;; support for font-lock.el
5124 ;; Strings are a real pain in Ada because a single quote character is
5125 ;; overloaded as a string quote and type/instance delimiter. By default, a
5126 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5127 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5128 ;; syntax when the gods that created Ada determine them to be.
5130 ;; This only works in Emacs. See the comments before the grammar functions
5131 ;; at the beginning of this file for how this is done with XEmacs.
5132 ;; ----------------------------------------------------
5134 (defconst ada-font-lock-syntactic-keywords
5135 ;; Mark single quotes as having string quote syntax in 'c' instances.
5136 ;; As a special case, ''' will not be highlighted, but if we do not
5137 ;; set this special case, then the rest of the buffer is highlighted as
5139 ;; This sets the properties of the characters, so that ada-in-string-p
5140 ;; correctly handles '"' too...
5141 '(("[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5142 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))
5145 (defvar ada-font-lock-keywords
5149 ;; handle "type T is access function return S;"
5150 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5152 ;; preprocessor line
5153 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5156 ;; accept, entry, function, package (body), protected (body|type),
5157 ;; pragma, procedure, task (body) plus name.
5163 "package[ \t]+body\\|"
5167 "protected[ \t]+body\\|"
5168 "protected[ \t]+type\\|"
5174 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5175 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5177 ;; Optional keywords followed by a type name.
5178 (list (concat ; ":[ \t]*"
5179 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5181 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5182 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5185 ;; Main keywords, except those treated specially below.
5188 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5189 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5190 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5191 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5192 "null" "or" "others" "overriding" "private" "protected" "raise"
5193 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5194 "select" "separate" "synchronized" "tagged" "task" "terminate"
5195 "then" "until" "when" "while" "with" "xor") t)
5198 ;; Anything following end and not already fontified is a body name.
5199 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5200 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5202 ;; Keywords followed by a type or function name.
5203 (list (concat "\\<\\("
5204 "new\\|of\\|subtype\\|type"
5205 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5206 '(1 font-lock-keyword-face)
5207 '(2 (if (match-beginning 4)
5208 font-lock-function-name-face
5209 font-lock-type-face) nil t))
5211 ;; Keywords followed by a (comma separated list of) reference.
5212 ;; Note that font-lock only works on single lines, thus we can not
5213 ;; correctly highlight a with_clause that spans multiple lines.
5214 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5215 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5216 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
5220 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
5222 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5223 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5225 ;; Ada unnamed numerical constants
5226 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5229 "Default expressions to highlight in Ada mode.")
5232 ;; ---------------------------------------------------------
5233 ;; Support for outline.el
5234 ;; ---------------------------------------------------------
5236 (defun ada-outline-level ()
5237 "This is so that `current-column' DTRT in otherwise-hidden text."
5238 ;; patch from Dave Love <fx@gnu.org>
5239 (let (buffer-invisibility-spec)
5241 (back-to-indentation)
5244 ;; ---------------------------------------------------------
5245 ;; Support for narrow-to-region
5246 ;; ---------------------------------------------------------
5248 (defun ada-narrow-to-defun (&optional arg)
5249 "Make text outside current subprogram invisible.
5250 The subprogram visible is the one that contains or follow point.
5251 Optional ARG is ignored.
5252 Use \\[widen] to go back to the full visibility for the buffer."
5259 (ada-previous-procedure)
5267 (narrow-to-region end (point))
5269 "Use M-x widen to get back to full visibility in the buffer"))))
5271 ;; ---------------------------------------------------------
5272 ;; Automatic generation of code
5273 ;; The Ada mode has a set of function to automatically generate a subprogram
5274 ;; or package body from its spec.
5275 ;; These function only use a primary and basic algorithm, this could use a
5276 ;; lot of improvement.
5277 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5279 ;; ----------------------------------------------------------
5281 (defun ada-gen-treat-proc (match)
5282 "Make dummy body of a procedure/function specification.
5283 MATCH is a cons cell containing the start and end locations of the last search
5284 for `ada-procedure-start-regexp'."
5285 (goto-char (car match))
5286 (let (func-found procname functype)
5288 ((or (looking-at "^[ \t]*procedure")
5289 (setq func-found (looking-at "^[ \t]*function")))
5290 ;; treat it as a proc/func
5293 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5295 ;; goto end of procname
5296 (goto-char (cdr match))
5298 ;; skip over parameterlist
5299 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5302 ;; if function, skip over 'return' and result type.
5306 (skip-chars-forward " \t\n")
5307 (setq functype (buffer-substring (point)
5312 ;; look for next non WS
5314 ((looking-at "[ \t]*;")
5315 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5316 (ada-indent-newline-indent)
5318 (ada-indent-newline-indent)
5321 (insert "Result : " functype ";")
5322 (ada-indent-newline-indent)))
5324 (ada-indent-newline-indent)
5326 (insert "return Result;")
5328 (ada-indent-newline-indent)
5329 (insert "end " procname ";")
5330 (ada-indent-newline-indent)
5333 ((looking-at "[ \t\n]*is")
5336 ((looking-at "[ \t\n]*rename")
5340 (message "unknown syntax"))))
5342 (if (looking-at "^[ \t]*task")
5344 (message "Task conversion is not yet implemented")
5346 (if (looking-at "[ \t]*;")
5351 (defun ada-make-body ()
5352 "Create an Ada package body in the current buffer.
5353 The spec must be the previously visited buffer.
5354 This function typically is to be hooked into `ff-file-created-hooks'."
5355 (delete-region (point-min) (point-max))
5356 (insert-buffer-substring (car (cdr (buffer-list))))
5357 (goto-char (point-min))
5360 (let (found ada-procedure-or-package-start-regexp)
5362 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5363 (progn (goto-char (cdr found))
5366 (error "No package"))
5368 (setq ada-procedure-or-package-start-regexp
5369 (concat ada-procedure-start-regexp
5371 ada-package-start-regexp))
5374 (ada-search-ignore-string-comment
5375 ada-procedure-or-package-start-regexp nil))
5377 (goto-char (car found))
5378 (if (looking-at ada-package-start-regexp)
5379 (progn (goto-char (cdr found))
5381 (ada-gen-treat-proc found))))))
5384 (defun ada-make-subprogram-body ()
5385 "Create a dummy subprogram body in package body file from spec surrounding point."
5387 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5388 (spec (match-beginning 0))
5393 (if (and (re-search-forward "(\\|;" nil t)
5394 (= (char-before) ?\())
5396 (ada-search-ignore-string-comment ")" nil)
5397 (ada-search-ignore-string-comment ";" nil)))
5398 (setq spec (buffer-substring spec (point)))
5400 ;; If find-file.el was available, use its functions
5401 (setq body-file (ada-get-body-name))
5403 (find-file body-file)
5404 (error "No body found for the package. Create it first"))
5408 (goto-char (point-max))
5409 (forward-comment -10000)
5410 (re-search-backward "\\<end\\>" nil t)
5411 ;; Move to the beginning of the elaboration part, if any
5412 (re-search-backward "^begin" nil t)
5416 (re-search-backward ada-procedure-start-regexp nil t)
5417 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5419 (error "Not in subprogram spec"))))
5421 ;; --------------------------------------------------------
5422 ;; Global initializations
5423 ;; --------------------------------------------------------
5425 ;; Create the keymap once and for all. If we do that in ada-mode,
5426 ;; the keys changed in the user's .emacs have to be modified
5431 ;; Create the syntax tables, but do not activate them
5432 (ada-create-syntax-table)
5434 ;; Add the default extensions (and set up speedbar)
5435 (ada-add-extensions ".ads" ".adb")
5436 ;; This two files are generated by GNAT when running with -gnatD
5437 (if (equal ada-which-compiler 'gnat)
5438 (ada-add-extensions ".ads.dg" ".adb.dg"))
5440 ;; Read the special cases for exceptions
5441 (ada-case-read-exceptions)
5443 ;; Setup auto-loading of the other Ada mode files.
5444 (autoload 'ada-change-prj "ada-xref" nil t)
5445 (autoload 'ada-check-current "ada-xref" nil t)
5446 (autoload 'ada-compile-application "ada-xref" nil t)
5447 (autoload 'ada-compile-current "ada-xref" nil t)
5448 (autoload 'ada-complete-identifier "ada-xref" nil t)
5449 (autoload 'ada-find-file "ada-xref" nil t)
5450 (autoload 'ada-find-any-references "ada-xref" nil t)
5451 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5452 (autoload 'ada-find-local-references "ada-xref" nil t)
5453 (autoload 'ada-find-references "ada-xref" nil t)
5454 (autoload 'ada-gdb-application "ada-xref" nil t)
5455 (autoload 'ada-goto-declaration "ada-xref" nil t)
5456 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5457 (autoload 'ada-goto-parent "ada-xref" nil t)
5458 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5459 (autoload 'ada-point-and-xref "ada-xref" nil t)
5460 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5461 (autoload 'ada-run-application "ada-xref" nil t)
5462 (autoload 'ada-set-default-project-file "ada-xref" nil nil)
5463 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5464 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5466 (autoload 'ada-customize "ada-prj" nil t)
5467 (autoload 'ada-prj-edit "ada-prj" nil t)
5468 (autoload 'ada-prj-new "ada-prj" nil t)
5469 (autoload 'ada-prj-save "ada-prj" nil t)
5471 (autoload 'ada-array "ada-stmt" nil t)
5472 (autoload 'ada-case "ada-stmt" nil t)
5473 (autoload 'ada-declare-block "ada-stmt" nil t)
5474 (autoload 'ada-else "ada-stmt" nil t)
5475 (autoload 'ada-elsif "ada-stmt" nil t)
5476 (autoload 'ada-exception "ada-stmt" nil t)
5477 (autoload 'ada-exception-block "ada-stmt" nil t)
5478 (autoload 'ada-exit "ada-stmt" nil t)
5479 (autoload 'ada-for-loop "ada-stmt" nil t)
5480 (autoload 'ada-function-spec "ada-stmt" nil t)
5481 (autoload 'ada-header "ada-stmt" nil t)
5482 (autoload 'ada-if "ada-stmt" nil t)
5483 (autoload 'ada-loop "ada-stmt" nil t)
5484 (autoload 'ada-package-body "ada-stmt" nil t)
5485 (autoload 'ada-package-spec "ada-stmt" nil t)
5486 (autoload 'ada-private "ada-stmt" nil t)
5487 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5488 (autoload 'ada-record "ada-stmt" nil t)
5489 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5490 (autoload 'ada-subtype "ada-stmt" nil t)
5491 (autoload 'ada-tabsize "ada-stmt" nil t)
5492 (autoload 'ada-task-body "ada-stmt" nil t)
5493 (autoload 'ada-task-spec "ada-stmt" nil t)
5494 (autoload 'ada-type "ada-stmt" nil t)
5495 (autoload 'ada-use "ada-stmt" nil t)
5496 (autoload 'ada-when "ada-stmt" nil t)
5497 (autoload 'ada-while-loop "ada-stmt" nil t)
5498 (autoload 'ada-with "ada-stmt" nil t)
5500 ;;; provide ourselves
5503 ;;; arch-tag: 1b7d45ec-1698-43b5-8d4a-e479ea023270
5504 ;;; ada-mode.el ends here