1 ;; @(#) ada-mode.el --- major-mode for editing Ada sources.
3 ;; Copyright (C) 1994, 1995, 1997, 1998, 1999 Free Software Foundation, Inc.
5 ;; Author: Rolf Ebert <ebert@inf.enst.fr>
6 ;; Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
7 ;; Emmanuel Briot <briot@gnat.com>
8 ;; Maintainer: Emmanuel Briot <briot@gnat.com>
9 ;; Ada Core Technologies's version: $Revision: 1.70 $
10 ;; Keywords: languages ada
12 ;; This file is not part of GNU Emacs
14 ;; This program 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 ;; This program 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
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;;; This mode is a major mode for editing Ada83 and Ada95 source code.
30 ;;; This is a major rewrite of the file packaged with Emacs-20.2. The
31 ;;; ada-mode is composed of four lisp file, ada-mode.el, ada-xref.el,
32 ;;; ada-prj.el and ada-stmt.el. Only this file (ada-mode.el) is
33 ;;; completely independent from the GNU Ada compiler Gnat, distributed
34 ;;; by Ada Core Technologies. All the other files rely heavily on
35 ;;; features provides only by Gnat.
37 ;;; Note: this mode will not work with Emacs 19. If you are on a VMS
38 ;;; system, where the latest version of Emacs is 19.28, you will need
39 ;;; another file, called ada-vms.el, that provides some required
43 ;;; Emacs should enter Ada mode automatically when you load an Ada file.
44 ;;; By default, the valid extensions for Ada files are .ads, .adb or .ada
45 ;;; If the ada-mode does not start automatically, then simply type the
46 ;;; following command :
49 ;;; By default, ada-mode is configured to take full advantage of the GNAT
50 ;;; compiler (the menus will include the cross-referencing features,...).
51 ;;; If you are using another compiler, you might want to set the following
52 ;;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
54 ;;; (setq ada-which-compiler 'generic)
56 ;;; This mode requires find-file.el to be present on your system.
59 ;;; The first Ada mode for GNU Emacs was written by V. Broman in
60 ;;; 1985. He based his work on the already existing Modula-2 mode.
61 ;;; This was distributed as ada.el in versions of Emacs prior to 19.29.
63 ;;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
64 ;;; several files with support for dired commands and other nice
65 ;;; things. It is currently available from the PAL
66 ;;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
68 ;;; The probably very first Ada mode (called electric-ada.el) was
69 ;;; written by Steven D. Litvintchouk and Steven M. Rosen for the
70 ;;; Gosling Emacs. L. Slater based his development on ada.el and
73 ;;; A complete rewrite by M. Heritsch and R. Ebert has been done.
74 ;;; Some ideas from the Ada mode mailing list have been
75 ;;; added. Some of the functionality of L. Slater's mode has not
76 ;;; (yet) been recoded in this new mode. Perhaps you prefer sticking
79 ;;; A complete rewrite for Emacs-20 / Gnat-3.11 has been done by Ada Core
80 ;;; Technologies. Please send bugs to briot@gnat.com
83 ;;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
84 ;;; many patches included in this package.
85 ;;; Christian Egli <Christian.Egli@hcsd.hac.com>:
86 ;;; ada-imenu-generic-expression
87 ;;; Many thanks also to the following persons that have contributed one day
89 ;;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
90 ;;; woodruff@stc.llnl.gov (John Woodruff)
91 ;;; jj@ddci.dk (Jesper Joergensen)
92 ;;; gse@ocsystems.com (Scott Evans)
93 ;;; comar@gnat.com (Cyrille Comar)
94 ;;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
95 ;;; and others for their valuable hints.
98 ;;; Note: Every function is this package is compiler-independent.
99 ;;; The names start with ada-
100 ;;; The variables that the user can edit can all be modified through
101 ;;; the customize mode. They are sorted in alphabetical order in this
105 ;; this function is needed at compile time
107 (defun ada-check-emacs-version (major minor &optional is-xemacs)
108 "Returns t if Emacs's version is greater or equal to MAJOR.MINOR.
109 If IS-XEMACS is non-nil, check for XEmacs instead of Emacs."
110 (let ((xemacs-running (or (string-match "Lucid" emacs-version)
111 (string-match "XEmacs" emacs-version))))
112 (and (or (and is-xemacs xemacs-running)
113 (not (or is-xemacs xemacs-running)))
114 (or (> emacs-major-version major)
115 (and (= emacs-major-version major)
116 (>= emacs-minor-version minor)))))))
119 ;; We create a constant for that, for efficiency only
120 ;; This should not be evaluated at compile time, only a runtime
121 (defconst ada-xemacs (boundp 'running-xemacs)
122 "Return t if we are using XEmacs.")
128 (condition-case nil (require 'find-file) (error nil)))
130 ;; This call should not be made in the release that is done for the
131 ;; official FSF Emacs, since it does nothing useful for the latest version
132 ;; (require 'ada-support)
134 (defvar ada-mode-hook nil
135 "*List of functions to call when Ada mode is invoked.
136 This hook is automatically executed after the ada-mode is
138 This is a good place to add Ada environment specific bindings.")
141 "Major mode for editing Ada source in Emacs."
144 (defcustom ada-auto-case t
145 "*Non-nil means automatically change case of preceding word while typing.
146 Casing is done according to `ada-case-keyword', `ada-case-identifier'
147 and `ada-case-attribute'."
148 :type 'boolean :group 'ada)
150 (defcustom ada-broken-decl-indent 0
151 "*Number of columns to indent a broken declaration.
156 >>>>>B : Integer; -- from ada-broken-decl-indent"
157 :type 'integer :group 'ada)
159 (defcustom ada-broken-indent 2
160 "*Number of columns to indent the continuation of a broken line.
163 My_Var : My_Type := (Field1 =>
164 >>>>>>>>>Value); -- from ada-broken-indent"
165 :type 'integer :group 'ada)
167 (defcustom ada-case-attribute 'ada-capitalize-word
168 "*Function to call to adjust the case of Ada attributes.
169 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
170 `ada-capitalize-word'."
171 :type '(choice (const downcase-word)
173 (const ada-capitalize-word)
174 (const ada-loose-case-word))
177 (defcustom ada-case-exception-file "~/.emacs_case_exceptions"
178 "*File name for the dictionary of special casing exceptions for identifiers.
179 This file should contain one word per line, that gives the casing
180 to be used for that words in Ada files."
181 :type 'file :group 'ada)
183 (defcustom ada-case-keyword 'downcase-word
184 "*Function to call to adjust the case of an Ada keywords.
185 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
186 `ada-capitalize-word'."
187 :type '(choice (const downcase-word)
189 (const ada-capitalize-word)
190 (const ada-loose-case-word))
193 (defcustom ada-case-identifier 'ada-loose-case-word
194 "*Function to call to adjust the case of an Ada identifier.
195 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
196 `ada-capitalize-word'."
197 :type '(choice (const downcase-word)
199 (const ada-capitalize-word)
200 (const ada-loose-case-word))
203 (defcustom ada-clean-buffer-before-saving t
204 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
205 :type 'boolean :group 'ada)
207 (defcustom ada-indent 3
208 "*Size of Ada indentation.
213 >>>>>>>>>>null; -- from ada-indent"
214 :type 'integer :group 'ada)
216 (defcustom ada-indent-after-return t
217 "*Non-nil means automatically indent after RET or LFD."
218 :type 'boolean :group 'ada)
220 (defcustom ada-indent-comment-as-code t
221 "*Non-nil means indent comment lines as code."
222 :type 'boolean :group 'ada)
224 (defcustom ada-indent-is-separate t
225 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
226 :type 'boolean :group 'ada)
228 (defcustom ada-indent-record-rel-type 3
229 "*Indentation for 'record' relative to 'type' or 'use'.
233 >>>>>>>>>>>record -- from ada-indent-record-rel-type"
234 :type 'integer :group 'ada)
236 (defcustom ada-indent-return 0
237 "*Indentation for 'return' relative to the matching 'function' statement.
238 If ada-indent-return is null or negative, the indentation is done relative to
239 the open parenthesis (if there is no parenthesis, ada-broken-indent is used).
242 function A (B : Integer)
243 >>>>>return C; -- from ada-indent-return"
244 :type 'integer :group 'ada)
246 (defcustom ada-indent-to-open-paren t
247 "*Non-nil means indent according to the innermost open parenthesis."
248 :type 'boolean :group 'ada)
250 (defcustom ada-fill-comment-prefix "-- "
251 "*Text inserted in the first columns when filling a comment paragraph.
252 Note: if you modify this variable, you will have to restart the ada-mode to
253 reread this variable."
254 :type 'string :group 'ada)
256 (defcustom ada-fill-comment-postfix " --"
257 "*Text inserted at the end of each line when filling a comment paragraph.
258 with `ada-fill-comment-paragraph-postfix'."
259 :type 'string :group 'ada)
261 (defcustom ada-label-indent -4
262 "*Number of columns to indent a label.
267 >>>>>>>>>>>>Label: -- from ada-label-indent"
268 :type 'integer :group 'ada)
270 (defcustom ada-language-version 'ada95
271 "*Do we program in `ada83' or `ada95'?"
272 :type '(choice (const ada83) (const ada95)) :group 'ada)
274 (defcustom ada-move-to-declaration nil
275 "*Non-nil means `ada-move-to-start' moves point to the subprogram declaration,
277 :type 'boolean :group 'ada)
279 (defcustom ada-popup-key '[down-mouse-3]
280 "*Key used for binding the contextual menu.
281 If nil, no contextual menu is available.")
283 (defcustom ada-search-directories
284 '("." "$ADA_INCLUDE_PATH" "/usr/adainclude" "/usr/local/adainclude"
285 "/opt/gnu/adainclude")
286 "*List of directories to search for Ada files.
287 See the description for the `ff-search-directories' variable.
288 Emacs will automatically add the paths defined in your project file, and if you
289 are using the GNAT compiler the output of the gnatls command to find where the
291 :type '(repeat (choice :tag "Directory"
292 (const :tag "default" nil)
293 (directory :format "%v")))
296 (defcustom ada-stmt-end-indent 0
297 "*Number of columns to indent the end of a statement on a separate line.
301 >>>>>>>>>>>then -- from ada-stmt-end-indent"
302 :type 'integer :group 'ada)
304 (defcustom ada-tab-policy 'indent-auto
305 "*Control the behavior of the TAB key.
307 `indent-rigidly' : always adds ada-indent blanks at the beginning of the line.
308 `indent-auto' : use indentation functions in this file.
309 `always-tab' : do indent-relative."
310 :type '(choice (const indent-auto)
311 (const indent-rigidly)
315 (defcustom ada-when-indent 3
316 "*Indentation for 'when' relative to 'exception' or 'case'.
320 >>>>>>>>when B => -- from ada-when-indent"
321 :type 'integer :group 'ada)
323 (defcustom ada-which-compiler 'gnat
324 "*Name of the compiler to use.
325 This will determine what features are made available through the ada-mode.
326 The possible choices are :
327 `gnat': Use Ada Core Technologies' Gnat compiler. Add some cross-referencing
329 `generic': Use a generic compiler"
330 :type '(choice (const gnat)
335 ;;; ---- end of user configurable variables
338 (defvar ada-body-suffixes '(".adb")
339 "List of possible suffixes for Ada body files.
340 The extensions should include a `.' if needed.")
342 (defvar ada-spec-suffixes '(".ads")
343 "List of possible suffixes for Ada spec files.
344 The extensions should include a `.' if needed.")
346 (defvar ada-mode-menu (make-sparse-keymap)
347 "Menu for ada-mode.")
349 (defvar ada-mode-map (make-sparse-keymap)
350 "Local keymap used for Ada mode.")
352 (defvar ada-mode-syntax-table nil
353 "Syntax table to be used for editing Ada source code.")
355 (defvar ada-mode-symbol-syntax-table nil
356 "Syntax table for Ada, where `_' is a word constituent.")
359 (defconst ada-83-string-keywords
360 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
361 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
362 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
363 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
364 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
365 "procedure" "raise" "range" "record" "rem" "renames" "return"
366 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
367 "type" "use" "when" "while" "with" "xor")
368 "List of Ada keywords.
369 This variable is used to define `ada-83-keywords' and `ada-95-keywords'"))
371 (defvar ada-ret-binding nil
372 "Variable to save key binding of RET when casing is activated.")
374 (defvar ada-case-exception '()
375 "Alist of words (entities) that have special casing.")
377 (defvar ada-lfd-binding nil
378 "Variable to save key binding of LFD when casing is activated.")
380 (defvar ada-other-file-alist nil
381 "Variable used by find-file to find the name of the other package.
382 See `ff-other-file-alist'.")
384 ;;; ---- Below are the regexp used in this package for parsing
386 (defconst ada-83-keywords
388 (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
389 "Regular expression for looking at Ada83 keywords.")
391 (defconst ada-95-keywords
393 (concat "\\<" (regexp-opt
395 '("abstract" "aliased" "protected" "requeue"
397 ada-83-string-keywords) t) "\\>"))
398 "Regular expression for looking at Ada95 keywords.")
400 (defvar ada-keywords ada-95-keywords
401 "Regular expression for looking at Ada keywords.")
403 (defconst ada-ident-re
405 "Regexp matching Ada (qualified) identifiers.")
407 (defvar ada-procedure-start-regexp
408 "^[ \t]*\\(procedure\\|function\\|task\\)[ \t\n]+\\(\\(\\sw\\|[_.]\\)+\\)"
409 "Regexp used to find Ada procedures/functions.")
411 (defvar ada-package-start-regexp
412 "^[ \t]*\\(package\\)"
413 "Regexp used to find Ada packages.")
416 ;;; ---- regexps for indentation functions
418 (defvar ada-block-start-re
420 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
421 "exception" "generic" "loop" "or"
422 "private" "select" ))
423 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
424 "Regexp for keywords starting Ada blocks.")
426 (defvar ada-end-stmt-re
431 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
432 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic" "loop"
433 "private" "record" "select" "then") t) "\\>" "\\|"
434 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
435 t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
436 "^[ \t]*exception\\>"
438 "Regexp of possible ends for a non-broken statement.
439 A new statement starts after these.")
441 (defvar ada-matching-start-re
445 '("end" "loop" "select" "begin" "case" "do"
446 "if" "task" "package" "record" "protected") t)
448 "Regexp used in ada-goto-matching-start.")
450 (defvar ada-matching-decl-start-re
454 '("is" "separate" "end" "declare" "if" "new" "begin" "generic") t)
456 "Regexp used in ada-goto-matching-decl-start.")
459 (defvar ada-loop-start-re
460 "\\<\\(for\\|while\\|loop\\)\\>"
461 "Regexp for the start of a loop.")
463 (defvar ada-subprog-start-re
465 (concat "\\<" (regexp-opt '("accept" "entry" "function" "package" "procedure"
466 "protected" "task") t) "\\>"))
467 "Regexp for the start of a subprogram.")
469 (defvar ada-named-block-re
470 "[ \t]*\\(\\sw\\|_\\)+[ \t]*:[^=]"
471 "Regexp of the name of a block or loop.")
473 (defvar ada-contextual-menu-on-identifier nil
474 "Set to true when the right mouse button was clicked on an identifier.")
476 (defvar ada-contextual-menu
477 "Defines the menu to use when the user presses the right mouse button.
478 The variable `ada-contextual-menu-on-identifier' will be set to t before
479 displaying the menu if point was on an identifier."
482 ["Goto Declaration/Body" ada-goto-declaration
483 :included ada-contextual-menu-on-identifier]
484 ["Goto Previous Reference" ada-xref-goto-previous-reference]
485 ["List References" ada-find-references
486 :included ada-contextual-menu-on-identifier]
488 ["Other File" ff-find-other-file]
489 ["Goto Parent Unit" ada-goto-parent]
492 (let ((map (make-sparse-keymap "Ada")))
493 ;; The identifier part
494 (if (equal ada-which-compiler 'gnat)
496 (define-key-after map [Ref]
497 '(menu-item "Goto Declaration/Body"
499 :visible ada-contextual-menu-on-identifier
501 (define-key-after map [Prev]
502 '("Goto Previous Reference" .ada-xref-goto-previous-reference) t)
503 (define-key-after map [List]
504 '(menu-item "List References"
506 :visible ada-contextual-menu-on-identifier) t)
507 (define-key-after map [-] '("-" nil) t)
509 (define-key-after map [Other] '("Other file" . ff-find-other-file) t)
510 (define-key-after map [Parent] '("Goto Parent Unit" . ada-goto-parent)t)
515 ;;------------------------------------------------------------------
516 ;; Support for imenu (see imenu.el)
517 ;;------------------------------------------------------------------
519 (defvar ada-imenu-generic-expression
521 '(nil "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)\\)[ \t\n]*\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]" 2)
524 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
526 "\\([ \t\n]+\\|[ \t\n]*([^)]+)\\)";; parameter list or simple space
527 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
529 '("*Tasks*" "^[ \t]*task[ \t]+\\(\\(body\\|type\\)[ \t]+\\)?\\(\\(\\sw\\|_\\)+\\)" 3)
530 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
531 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
532 "Imenu generic expression for Ada mode.
533 See `imenu-generic-expression'. This variable will create two submenus, one
534 for type and subtype definitions, the other for subprograms declarations.
535 The main menu will reference the bodies of the subprograms.")
538 ;;------------------------------------------------------------
539 ;; Support for compile.el
540 ;;------------------------------------------------------------
542 (defun ada-compile-mouse-goto-error ()
543 "Mouse interface for `ada-compile-goto-error'."
545 (mouse-set-point last-input-event)
546 (ada-compile-goto-error (point))
549 (defun ada-compile-goto-error (pos)
550 "Replaces `compile-goto-error' from compile.el.
551 If POS is on a file and line location, go to this position. It adds to
552 compile.el the capacity to go to a reference in an error message.
553 For instance, on this line:
554 foo.adb:61:11: [...] in call to size declared at foo.ads:11
555 both file locations can be clicked on and jumped to."
559 (skip-chars-backward "-a-zA-Z0-9_:./\\")
561 ;; special case: looking at a filename:line not at the beginning of a line
564 "\\(\\(\\sw\\|[_-.]\\)+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"))
565 (let ((line (match-string 3))
566 (error-pos (point-marker))
571 (set-buffer (compilation-find-file (point-marker) (match-string 1)
574 (goto-line (string-to-number line)))
575 (set 'source (point-marker))))
576 (compilation-goto-locus (cons source error-pos))
579 ;; otherwise, default behavior
581 (compile-goto-error))
585 ;;-------------------------------------------------------------------------
586 ;; Grammar related function
587 ;; The functions below work with the syntax class of the characters in an Ada
588 ;; buffer. Two syntax tables are created, depending on whether we want '_'
589 ;; to be considered as part of a word or not.
590 ;; Some characters may have multiple meanings depending on the context:
591 ;; - ' is either the beginning of a constant character or an attribute
592 ;; - # is either part of a based litteral or a gnatprep statement.
593 ;; - " starts a string, but not if inside a constant character.
594 ;; - ( and ) should be ignored if inside a constant character.
595 ;; Thus their syntax property is changed automatically, and we can still use
596 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
598 ;; On Emacs, this is done through the `syntax-table' text property. The
599 ;; modification is done automatically each time the user as typed a new
600 ;; character. This is already done in `font-lock-mode' (in
601 ;; `font-lock-syntactic-keywords', so we take advantage of the existing
602 ;; mechanism. If font-lock-mode is not activated, we do it by hand in
603 ;; `ada-after-change-function', thanks to `ada-deactivate-properties' and
604 ;; `ada-initialize-properties'.
606 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
607 ;; slow advice to `parse-partial-sexp' to do the same thing.
608 ;; When executing parse-partial-sexp, we simply modify the strings before and
609 ;; after, so that the special constants '"', '(' and ')' do not interact
610 ;; with parse-partial-sexp.
611 ;; Note: this code is slow and needs to be rewritten as soon as something
612 ;; better is available on XEmacs.
613 ;;-------------------------------------------------------------------------
615 (defun ada-create-syntax-table ()
616 "Create the two syntax tables use in the Ada mode.
617 The standard table declares `_' as a symbol constituent, the second one
618 declares it as a word constituent."
620 (set 'ada-mode-syntax-table (make-syntax-table))
621 (set-syntax-table ada-mode-syntax-table)
623 ;; define string brackets (`%' is alternative string bracket, but
624 ;; almost never used as such and throws font-lock and indentation
626 (modify-syntax-entry ?% "$" ada-mode-syntax-table)
627 (modify-syntax-entry ?\" "\"" ada-mode-syntax-table)
629 (modify-syntax-entry ?: "." ada-mode-syntax-table)
630 (modify-syntax-entry ?\; "." ada-mode-syntax-table)
631 (modify-syntax-entry ?& "." ada-mode-syntax-table)
632 (modify-syntax-entry ?\| "." ada-mode-syntax-table)
633 (modify-syntax-entry ?+ "." ada-mode-syntax-table)
634 (modify-syntax-entry ?* "." ada-mode-syntax-table)
635 (modify-syntax-entry ?/ "." ada-mode-syntax-table)
636 (modify-syntax-entry ?= "." ada-mode-syntax-table)
637 (modify-syntax-entry ?< "." ada-mode-syntax-table)
638 (modify-syntax-entry ?> "." ada-mode-syntax-table)
639 (modify-syntax-entry ?$ "." ada-mode-syntax-table)
640 (modify-syntax-entry ?\[ "." ada-mode-syntax-table)
641 (modify-syntax-entry ?\] "." ada-mode-syntax-table)
642 (modify-syntax-entry ?\{ "." ada-mode-syntax-table)
643 (modify-syntax-entry ?\} "." ada-mode-syntax-table)
644 (modify-syntax-entry ?. "." ada-mode-syntax-table)
645 (modify-syntax-entry ?\\ "." ada-mode-syntax-table)
646 (modify-syntax-entry ?\' "." ada-mode-syntax-table)
648 ;; a single hyphen is punctuation, but a double hyphen starts a comment
649 (modify-syntax-entry ?- ". 12" ada-mode-syntax-table)
651 ;; See the comment above on grammar related function for the special
654 (modify-syntax-entry ?# "<" ada-mode-syntax-table)
655 (modify-syntax-entry ?# "$" ada-mode-syntax-table))
657 ;; and \f and \n end a comment
658 (modify-syntax-entry ?\f "> " ada-mode-syntax-table)
659 (modify-syntax-entry ?\n "> " ada-mode-syntax-table)
661 ;; define what belongs in Ada symbols
662 (modify-syntax-entry ?_ "_" ada-mode-syntax-table)
664 ;; define parentheses to match
665 (modify-syntax-entry ?\( "()" ada-mode-syntax-table)
666 (modify-syntax-entry ?\) ")(" ada-mode-syntax-table)
668 (set 'ada-mode-symbol-syntax-table (copy-syntax-table ada-mode-syntax-table))
669 (modify-syntax-entry ?_ "w" ada-mode-symbol-syntax-table)
672 ;; Support of special characters in XEmacs (see the comments at the beginning
673 ;; of the section on Grammar related functions).
676 (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
677 "Handles special character constants and gnatprep statements."
681 (setq from to to tmp)))
684 (while (re-search-forward "'\\([(\")#]\\)'" to t)
685 (set 'change (cons (list (match-beginning 1)
689 (replace-match "'A'"))
691 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
692 (set 'change (cons (list (match-beginning 1)
693 (length (match-string 1))
696 (replace-match (make-string (length (match-string 1)) ?@))))
700 (goto-char (caar change))
701 (delete-char (cadar change))
702 (insert (caddar change))
703 (set 'change (cdr change)))))))
705 (defun ada-deactivate-properties ()
706 "Deactivate ada-mode's properties handling.
707 This would be a duplicate of font-lock if both are used at the same time."
708 (remove-hook 'after-change-functions 'ada-after-change-function t))
710 (defun ada-initialize-properties ()
711 "Initialize some special text properties in the whole buffer.
712 In particular, character constants are said to be strings, #...# are treated
713 as numbers instead of gnatprep comments."
717 (goto-char (point-min))
718 (while (re-search-forward "'.'" nil t)
719 (add-text-properties (match-beginning 0) (match-end 0)
720 '(syntax-table ("'" . ?\"))))
721 (goto-char (point-min))
722 (while (re-search-forward "^[ \t]*#" nil t)
723 (add-text-properties (match-beginning 0) (match-end 0)
724 '(syntax-table (11 . 10))))
725 (set-buffer-modified-p nil)
727 ;; Setting this only if font-lock is not set won't work
728 ;; if the user activates or deactivates font-lock-mode,
729 ;; but will make things faster most of the time
730 (make-local-hook 'after-change-functions)
731 (add-hook 'after-change-functions 'ada-after-change-function nil t)
734 (defun ada-after-change-function (beg end old-len)
735 "Called when the region between BEG and END was changed in the buffer.
736 OLD-LEN indicates what the length of the replaced text was."
737 (let ((inhibit-point-motion-hooks t)
742 (remove-text-properties (point) eol '(syntax-table nil))
743 (while (re-search-forward "'.'" eol t)
744 (add-text-properties (match-beginning 0) (match-end 0)
745 '(syntax-table ("'" . ?\"))))
747 (if (looking-at "^[ \t]*#")
748 (add-text-properties (match-beginning 0) (match-end 0)
749 '(syntax-table (11 . 10))))
753 ;;------------------------------------------------------------------
755 ;; The Ada-mode comes with fully contextual menus, bound by default
756 ;; on the right mouse button.
757 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
758 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
759 ;; if the mouse button was pressed on an identifier.
760 ;;------------------------------------------------------------------
762 (defun ada-popup-menu (position)
763 "Pops up a contextual menu, depending on where the user clicked.
764 POSITION is the location the mouse was clicked on."
767 (mouse-set-point last-input-event)
769 (setq ada-contextual-menu-on-identifier
771 (or (= (char-syntax (char-after)) ?w)
773 (not (ada-in-string-or-comment-p))
774 (save-excursion (skip-syntax-forward "w")
775 (not (ada-after-keyword-p)))
779 (set 'choice (popup-menu ada-contextual-menu))
780 (set 'choice (x-popup-menu position ada-contextual-menu)))
782 (funcall (lookup-key ada-contextual-menu (vector (car choice))))))))
784 ;;------------------------------------------------------------------
786 ;;------------------------------------------------------------------
789 (defun ada-add-extensions (spec body)
790 "Define SPEC and BODY as being valid extensions for Ada files.
791 Going from body to spec with `ff-find-other-file' used these
793 SPEC and BODY are two regular expressions that must match against the file
795 (let* ((reg (concat (regexp-quote body) "$"))
796 (tmp (assoc reg ada-other-file-alist)))
798 (setcdr tmp (list (cons spec (cadr tmp))))
799 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
801 (let* ((reg (concat (regexp-quote spec) "$"))
802 (tmp (assoc reg ada-other-file-alist)))
804 (setcdr tmp (list (cons body (cadr tmp))))
805 (add-to-list 'ada-other-file-alist (list reg (list body)))))
807 (add-to-list 'auto-mode-alist (cons spec 'ada-mode))
808 (add-to-list 'auto-mode-alist (cons body 'ada-mode))
810 (add-to-list 'ada-spec-suffixes spec)
811 (add-to-list 'ada-body-suffixes body)
813 ;; Support for speedbar (Specifies that we want to see these files in
818 (speedbar-add-supported-extension spec)
819 (speedbar-add-supported-extension body)))
826 "Ada mode is the major mode for editing Ada code.
828 Bindings are as follows: (Note: 'LFD' is control-j.)
830 Indent line '\\[ada-tab]'
831 Indent line, insert newline and indent the new line. '\\[newline-and-indent]'
833 Re-format the parameter-list point is in '\\[ada-format-paramlist]'
834 Indent all lines in region '\\[ada-indent-region]'
836 Adjust case of identifiers and keywords in region '\\[ada-adjust-case-region]'
837 Adjust case of identifiers and keywords in buffer '\\[ada-adjust-case-buffer]'
839 Fill comment paragraph, justify and append postfix '\\[fill-paragraph]'
841 Next func/proc/task '\\[ada-next-procedure]' Previous func/proc/task '\\[ada-previous-procedure]'
842 Next package '\\[ada-next-package]' Previous package '\\[ada-previous-package]'
844 Goto matching start of current 'end ...;' '\\[ada-move-to-start]'
845 Goto end of current block '\\[ada-move-to-end]'
847 Comments are handled using standard GNU Emacs conventions, including:
848 Start a comment '\\[indent-for-comment]'
849 Comment region '\\[comment-region]'
850 Uncomment region '\\[ada-uncomment-region]'
851 Continue comment on next line '\\[indent-new-comment-line]'
854 Display index-menu of functions & procedures '\\[imenu]'
856 If you use find-file.el:
857 Switch to other file (Body <-> Spec) '\\[ff-find-other-file]'
858 or '\\[ff-mouse-find-other-file]
859 Switch to other file in other window '\\[ada-ff-other-window]'
860 or '\\[ff-mouse-find-other-file-other-window]
861 If you use this function in a spec and no body is available, it gets created with body stubs.
863 If you use ada-xref.el:
864 Goto declaration: '\\[ada-point-and-xref]' on the identifier
865 or '\\[ada-goto-declaration]' with point on the identifier
866 Complete identifier: '\\[ada-complete-identifier]'"
869 (kill-all-local-variables)
871 (set (make-local-variable 'require-final-newline) t)
873 (make-local-variable 'comment-start)
874 (if ada-fill-comment-prefix
875 (set 'comment-start ada-fill-comment-prefix)
876 (set 'comment-start "-- "))
878 ;; Set the paragraph delimiters so that one can select a whole block
880 (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
881 (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
883 ;; comment end must be set because it may hold a wrong value if
884 ;; this buffer had been in another mode before. RE
885 (set (make-local-variable 'comment-end) "")
887 ;; used by autofill and indent-new-comment-line
888 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
890 ;; used by autofill to break a comment line and continue it on another line.
891 ;; The reason we need this one is that the default behavior does not work
892 ;; correctly with the definition of paragraph-start above when the comment
893 ;; is right after a multi-line subprogram declaration (the comments are
894 ;; aligned under the latest parameter, not under the declaration start).
895 (set (make-local-variable 'comment-line-break-function)
896 (lambda (&optional soft) (let ((fill-prefix nil))
897 (indent-new-comment-line soft))))
899 (set (make-local-variable 'indent-line-function)
900 'ada-indent-current-function)
902 (set (make-local-variable 'comment-column) 40)
904 ;; Emacs 20.3 defines a comment-padding to insert spaces between
905 ;; the comment and the text. We do not want any, this is already
906 ;; included in comment-start
909 (if (ada-check-emacs-version 20 3)
911 (set (make-local-variable 'parse-sexp-ignore-comments) t)
912 (set (make-local-variable 'comment-padding) 0)))
913 (set (make-local-variable 'parse-sexp-lookup-properties) t)
916 (set 'case-fold-search t)
917 (if (boundp 'imenu-case-fold-search)
918 (set 'imenu-case-fold-search t))
920 (set (make-local-variable 'fill-paragraph-function)
921 'ada-fill-comment-paragraph)
923 (set (make-local-variable 'imenu-generic-expression)
924 ada-imenu-generic-expression)
926 ;; Support for compile.el
927 ;; We just substitute our own functions to go to the error.
928 (add-hook 'compilation-mode-hook
930 (set 'compile-auto-highlight 40)
931 (define-key compilation-minor-mode-map [mouse-2]
932 'ada-compile-mouse-goto-error)
933 (define-key compilation-minor-mode-map "\C-c\C-c"
934 'ada-compile-goto-error)
935 (define-key compilation-minor-mode-map "\C-m"
936 'ada-compile-goto-error)
939 ;; font-lock support :
940 ;; We need to set some properties for XEmacs, and define some variables
945 (put 'ada-mode 'font-lock-defaults
946 '(ada-font-lock-keywords
947 nil t ((?\_ . "w") (?# . ".")) beginning-of-line))
949 (set (make-local-variable 'font-lock-defaults)
950 '(ada-font-lock-keywords
952 ((?\_ . "w") (?# . "."))
954 (font-lock-syntactic-keywords . ada-font-lock-syntactic-keywords)))
957 ;; Set up support for find-file.el.
958 (set (make-variable-buffer-local 'ff-other-file-alist)
959 'ada-other-file-alist)
960 (set (make-variable-buffer-local 'ff-search-directories)
961 'ada-search-directories)
962 (setq ff-post-load-hooks 'ada-set-point-accordingly
963 ff-file-created-hooks 'ada-make-body)
964 (add-hook 'ff-pre-load-hooks 'ada-which-function-are-we-in)
966 ;; Some special constructs for find-file.el
967 ;; We do not need to add the construction for 'with', which is in the
968 ;; standard find-file.el
969 ;; Go to the parent package :
970 (make-local-variable 'ff-special-constructs)
971 (add-to-list 'ff-special-constructs
972 (cons (eval-when-compile
973 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
975 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
977 (set 'fname (ff-get-file
978 ff-search-directories
979 (ada-make-filename-from-adaname
981 ada-spec-suffixes)))))
982 ;; Another special construct for find-file.el : when in a separate clause,
983 ;; go to the correct package.
984 (add-to-list 'ff-special-constructs
985 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
987 (set 'fname (ff-get-file
988 ff-search-directories
989 (ada-make-filename-from-adaname
991 ada-spec-suffixes)))))
992 ;; Another special construct, that redefines the one in find-file.el. The
993 ;; old one can handle only one possible type of extension for Ada files
994 (add-to-list 'ff-special-constructs
995 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
997 (set 'fname (ff-get-file
998 ff-search-directories
999 (ada-make-filename-from-adaname
1001 ada-spec-suffixes)))))
1003 ;; Support for outline-minor-mode
1004 (set (make-local-variable 'outline-regexp)
1005 "\\([ \t]*\\(procedure\\|function\\|package\\|with\\|use\\)\\|--\\|end\\)")
1006 (set (make-local-variable 'outline-level) 'ada-outline-level)
1008 ;; Support for imenu : We want a sorted index
1009 (set 'imenu-sort-function 'imenu--sort-by-name)
1011 ;; Set up the contextual menu
1013 (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
1015 ;; Support for indent-new-comment-line (Especially for XEmacs)
1016 (set 'comment-multi-line nil)
1017 (defconst comment-indent-function (lambda () comment-column))
1019 (set 'major-mode 'ada-mode)
1020 (set 'mode-name "Ada")
1022 (use-local-map ada-mode-map)
1025 (easy-menu-add ada-mode-menu ada-mode-map))
1027 (set-syntax-table ada-mode-syntax-table)
1029 (if ada-clean-buffer-before-saving
1031 ;; remove all spaces at the end of lines in the whole buffer.
1032 (add-hook 'local-write-file-hooks 'ada-remove-trailing-spaces)
1033 ;; convert all tabs to the correct number of spaces.
1034 (add-hook 'local-write-file-hooks
1035 '(lambda () (untabify (point-min) (point-max))))))
1037 (run-hooks 'ada-mode-hook)
1039 ;; Run this after the hook to give the users a chance to activate
1044 (ada-initialize-properties)
1045 (make-local-hook 'font-lock-mode-hook)
1046 (add-hook 'font-lock-mode-hook 'ada-deactivate-properties nil t)))
1048 ;; the following has to be done after running the ada-mode-hook
1049 ;; because users might want to set the values of these variable
1050 ;; inside the hook (MH)
1051 ;; Note that we add the new elements at the end of ada-other-file-alist
1052 ;; since some user might want to give priority to some other extensions
1053 ;; first (for instance, a .adb file could be associated with a .ads
1054 ;; or a .ads.gp (gnatprep)).
1055 ;; This is why we can't use add-to-list here.
1057 (cond ((eq ada-language-version 'ada83)
1058 (set 'ada-keywords ada-83-keywords))
1059 ((eq ada-language-version 'ada95)
1060 (set 'ada-keywords ada-95-keywords)))
1063 (ada-activate-keys-for-case)))
1066 ;;-----------------------------------------------------------------
1068 ;; Since Ada is case-insensitive, the Ada-mode provides an extensive set of
1069 ;; functions to auto-case identifiers, keywords, ...
1070 ;; The basic rules for autocasing are defined through the variables
1071 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1072 ;; are references to the functions that will do the actual casing.
1074 ;; However, in most cases, the user will want to define some exceptions to
1075 ;; these casing rules. This is done through a list of files, that contain
1076 ;; one word per line. These files are stored in `ada-case-exception-file'.
1077 ;;-----------------------------------------------------------------
1079 (defun ada-create-case-exception (&optional word)
1080 "Defines WORD as an exception for the casing system.
1081 If WORD is not given, then the current word in the buffer is used instead.
1082 The new words is added to the first file in `ada-case-exception-file'.
1083 The standard casing rules will no longer apply to this word."
1085 (let ((previous-syntax-table (syntax-table))
1086 (exception-list '()))
1087 (set-syntax-table ada-mode-symbol-syntax-table)
1090 (skip-syntax-backward "w")
1091 (set 'word (buffer-substring-no-properties
1092 (point) (save-excursion (forward-word 1) (point))))))
1094 ;; Reread the exceptions file, in case it was modified by some other,
1095 ;; and to keep the end-of-line comments that may exist in it.
1096 (if (file-readable-p (expand-file-name ada-case-exception-file))
1097 (let ((buffer (current-buffer)))
1098 (find-file (expand-file-name ada-case-exception-file))
1099 (set-syntax-table ada-mode-symbol-syntax-table)
1101 (goto-char (point-min))
1103 (add-to-list 'exception-list
1105 (buffer-substring-no-properties
1106 (point) (save-excursion (forward-word 1) (point)))
1107 (buffer-substring-no-properties
1108 (save-excursion (forward-word 1) (point))
1109 (save-excursion (end-of-line) (point)))
1113 (set-buffer buffer)))
1115 ;; If the word is already in the list, even with a different casing
1116 ;; we simply want to replace it.
1117 (if (and (not (equal exception-list '()))
1118 (assoc-ignore-case word exception-list))
1119 (setcar (assoc-ignore-case word exception-list)
1121 (add-to-list 'exception-list (list word "" t))
1124 (if (and (not (equal ada-case-exception '()))
1125 (assoc-ignore-case word ada-case-exception))
1126 (setcar (assoc-ignore-case word ada-case-exception)
1128 (add-to-list 'ada-case-exception (cons word t))
1131 ;; Save the list in the file
1132 (find-file (expand-file-name ada-case-exception-file))
1134 (mapcar '(lambda (x) (insert (car x) (nth 1 x) "\n"))
1135 (sort exception-list
1136 (lambda(a b) (string< (car a) (car b)))))
1139 (set-syntax-table previous-syntax-table)
1142 (defun ada-case-read-exceptions ()
1143 "Parse `ada-case-exception-file' for the dictionary of casing exceptions."
1145 (set 'ada-case-exception '())
1146 (if (file-readable-p (expand-file-name ada-case-exception-file))
1147 (let ((buffer (current-buffer)))
1148 (find-file (expand-file-name ada-case-exception-file))
1149 (set-syntax-table ada-mode-symbol-syntax-table)
1151 (goto-char (point-min))
1153 (add-to-list 'ada-case-exception
1155 (buffer-substring-no-properties
1156 (point) (save-excursion (forward-word 1) (point)))
1163 (defun ada-adjust-case-identifier ()
1164 "Adjust case of the previous identifier.
1165 The auto-casing is done according to the value of `ada-case-identifier' and
1166 the exceptions defined in `ada-case-exception-file'."
1167 (if (or (equal ada-case-exception '())
1168 (equal (char-after) ?_))
1169 (funcall ada-case-identifier -1)
1173 (start (save-excursion (skip-syntax-backward "w")
1176 ;; If we have an exception, replace the word by the correct casing
1177 (if (set 'match (assoc-ignore-case (buffer-substring start end)
1178 ada-case-exception))
1181 (delete-region start end)
1182 (insert (car match)))
1184 ;; Else simply re-case the word
1185 (funcall ada-case-identifier -1))))))
1187 (defun ada-after-keyword-p ()
1188 "Returns t if cursor is after a keyword."
1191 (and (not (and (char-before) (= (char-before) ?_)));; unless we have a _
1192 (looking-at (concat ada-keywords "[^_]")))))
1194 (defun ada-adjust-case (&optional force-identifier)
1195 "Adjust the case of the word before the just typed character.
1196 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1197 (let ((previous-syntax-table (syntax-table)))
1198 (set-syntax-table ada-mode-symbol-syntax-table)
1202 ;; Do nothing in some cases
1203 (if (and (> (point) 1)
1205 ;; or if at the end of a character constant
1206 (not (and (eq (char-after) ?')
1207 (eq (char-before (1- (point))) ?')))
1209 ;; or if the previous character was not part of a word
1210 (eq (char-syntax (char-before)) ?w)
1212 ;; if in a string or a comment
1213 (not (ada-in-string-or-comment-p))
1218 (or (= (point) (point-min))
1220 (= (char-after) ?'))
1221 (funcall ada-case-attribute -1)
1223 (not force-identifier) ; (MH)
1224 (ada-after-keyword-p))
1225 (funcall ada-case-keyword -1)
1226 (ada-adjust-case-identifier))))
1228 (set-syntax-table previous-syntax-table)
1232 (defun ada-adjust-case-interactive (arg)
1233 "Adjust the case of the previous word, and process the character just typed.
1234 ARG is the prefix the user entered with \C-u."
1236 (let ((lastk last-command-char))
1237 (cond ((or (eq lastk ?\n)
1242 ;; horrible De-kludge
1243 (delete-backward-char 1)
1244 ;; some special keys and their bindings
1247 (funcall ada-lfd-binding))
1249 (funcall ada-ret-binding))))
1250 ((eq lastk ?\C-i) (ada-tab))
1251 ((self-insert-command (prefix-numeric-value arg))))
1252 ;; if there is a keyword in front of the underscore
1253 ;; then it should be part of an identifier (MH)
1256 (ada-adjust-case))))
1259 (defun ada-activate-keys-for-case ()
1260 "Modifies the key bindings for all the keys that should readjust the casing."
1262 ;; save original key bindings to allow swapping ret/lfd
1263 ;; when casing is activated
1264 ;; the 'or ...' is there to be sure that the value will not
1265 ;; be changed again when Ada mode is called more than once (MH)
1267 (set 'ada-ret-binding (key-binding "\C-M")))
1269 (set 'ada-lfd-binding (key-binding "\C-j")))
1270 ;; call case modifying function after certain keys.
1271 (mapcar (function (lambda(key) (define-key
1273 (char-to-string key)
1274 'ada-adjust-case-interactive)))
1275 '( ?` ?~ ?! ?_ ?@ ?# ?$ ?% ?^ ?& ?* ?( ?) ?- ?= ?+ ?[ ?{ ?] ?}
1276 ?\\ ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?? ?/ ?\n 32 ?\r )))
1278 (defun ada-loose-case-word (&optional arg)
1279 "Upcase first letter and letters following `_' in the following word.
1280 No other letter is modified.
1281 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1285 (skip-syntax-backward "w")
1287 (search-forward "_" pos t))
1290 (insert-char (upcase (following-char)) 1)
1294 (defun ada-capitalize-word (&optional arg)
1295 "Upcase first letter and letters following '_', lower case other letters.
1296 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1298 (let ((pos (point)))
1299 (skip-syntax-backward "w")
1300 (modify-syntax-entry ?_ "_")
1301 (capitalize-region (point) pos)
1303 (modify-syntax-entry ?_ "w")))
1305 (defun ada-adjust-case-region (from to)
1306 "Adjusts the case of all words in the region between FROM and TO.
1307 Attention: This function might take very long for big regions !"
1313 (previous-syntax-table (syntax-table)))
1314 (message "Adjusting case ...")
1317 (set-syntax-table ada-mode-symbol-syntax-table)
1320 ;; loop: look for all identifiers, keywords, and attributes
1322 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1323 (set 'end (match-end 1))
1325 (and (> (point) from)
1328 (set 'attribp (looking-at "'.[^']")))))
1330 ;; do nothing if it is a string or comment
1331 (ada-in-string-or-comment-p)
1334 ;; get the identifier or keyword or attribute
1336 (set 'begin (point))
1337 (set 'keywordp (looking-at ada-keywords))
1340 ;; casing according to user-option
1343 (funcall ada-case-attribute -1)
1345 (funcall ada-case-keyword -1)
1346 (ada-adjust-case-identifier)))
1347 (goto-char begin))))
1348 (message "Adjusting case ... Done"))
1349 (set-syntax-table previous-syntax-table))))
1351 (defun ada-adjust-case-buffer ()
1352 "Adjusts the case of all words in the whole buffer.
1353 ATTENTION: This function might take very long for big buffers !"
1355 (ada-adjust-case-region (point-min) (point-max)))
1358 ;;--------------------------------------------------------------
1359 ;; Format Parameter Lists
1360 ;; Some special algorithms are provided to indent the parameter lists in
1361 ;; subprogram declarations. This is done in two steps:
1362 ;; - First parses the parameter list. The returned list has the following
1364 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1366 ;; This is done in `ada-scan-paramlist'.
1367 ;; - Delete and recreate the parameter list in function
1368 ;; `ada-format-paramlist'.
1369 ;; Note: Comments inside the parameter list are lost.
1370 ;; The syntax has to be correct, or the reformating will fail.
1371 ;;--------------------------------------------------------------
1373 (defun ada-format-paramlist ()
1374 "Reformats the parameter list point is in."
1380 (previous-syntax-table (syntax-table)))
1383 (set-syntax-table ada-mode-symbol-syntax-table)
1385 ;; check if really inside parameter list
1386 (or (ada-in-paramlist-p)
1387 (error "not in parameter list"))
1389 ;; find start of current parameter-list
1390 (ada-search-ignore-string-comment
1391 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1394 (set 'begin (point))
1396 ;; find end of parameter-list
1398 (set 'delend (point))
1401 ;; find end of last parameter-declaration
1402 (forward-comment -1000)
1405 ;; build a list of all elements of the parameter-list
1406 (set 'paramlist (ada-scan-paramlist (1+ begin) end))
1408 ;; delete the original parameter-list
1409 (delete-region begin (1- delend))
1411 ;; insert the new parameter-list
1413 (ada-insert-paramlist paramlist))
1415 ;; restore syntax-table
1416 (set-syntax-table previous-syntax-table)
1419 (defun ada-scan-paramlist (begin end)
1420 "Scan the parameter list found in between BEGIN and END.
1421 Returns the equivalent internal parameter list."
1422 (let ((paramlist (list))
1432 ;; loop until end of last parameter
1435 ;; find first character of parameter-declaration
1436 (ada-goto-next-non-ws)
1439 ;; find last character of parameter-declaration
1440 (if (set 'match-cons
1441 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1443 (set 'epos (car match-cons))
1444 (set 'semipos (cdr match-cons)))
1447 ;; read name(s) of parameter(s)
1449 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1451 (set 'param (list (match-string 1)))
1452 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1460 (ada-search-ignore-string-comment
1461 "in" nil epos t 'word-search-forward)))))
1469 (ada-search-ignore-string-comment
1470 "out" nil epos t 'word-search-forward)))))
1472 ;; look for 'access'
1478 (ada-search-ignore-string-comment
1479 "access" nil epos t 'word-search-forward)))))
1481 ;; skip 'in'/'out'/'access'
1483 (ada-goto-next-non-ws)
1484 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1486 (ada-goto-next-non-ws))
1488 ;; read type of parameter
1489 (looking-at "\\<\\(\\sw\\|[_.']\\)+\\>")
1492 (list (match-string 0))))
1494 ;; read default-expression, if there is one
1495 (goto-char (set 'apos (match-end 0)))
1499 (if (set 'match-cons
1500 (ada-search-ignore-string-comment
1501 ":=" nil epos t 'search-forward))
1502 (buffer-substring (car match-cons) epos)
1505 ;; add this parameter-declaration to the list
1506 (set 'paramlist (append paramlist (list param)))
1508 ;; check if it was the last parameter
1511 (goto-char semipos))
1513 (reverse paramlist)))
1515 (defun ada-insert-paramlist (paramlist)
1516 "Inserts a formatted PARAMLIST in the buffer."
1517 (let ((i (length paramlist))
1527 ;; loop until last parameter
1528 (while (not (zerop i))
1531 ;; get max length of parameter-name
1532 (set 'parlen (max parlen (length (nth 0 (nth i paramlist)))))
1534 ;; get max length of type-name
1535 (set 'typlen (max typlen (length (nth 4 (nth i paramlist)))))
1537 ;; is there any 'in' ?
1538 (set 'inp (or inp (nth 1 (nth i paramlist))))
1540 ;; is there any 'out' ?
1541 (set 'outp (or outp (nth 2 (nth i paramlist))))
1543 ;; is there any 'access' ?
1544 (set 'accessp (or accessp (nth 3 (nth i paramlist))))
1547 ;; does paramlist already start on a separate line ?
1549 (re-search-backward "^.\\|[^ \t]" nil t)
1551 ;; yes => re-indent it
1553 (ada-indent-current)
1555 (if (looking-at "\\(is\\|return\\)")
1556 (replace-match " \\1"))))
1558 ;; no => insert it where we are after removing any whitespace
1562 ((looking-at "[ \t]*\\(\n\\|;\\)")
1563 (replace-match "\\1"))
1564 ((looking-at "[ \t]*\\(is\\|return\\)")
1565 (replace-match " \\1"))))
1569 (ada-indent-current)
1571 (set 'firstcol (current-column))
1572 (set 'i (length paramlist))
1574 ;; loop until last parameter
1575 (while (not (zerop i))
1577 (set 'column firstcol)
1579 ;; insert parameter-name, space and colon
1580 (insert (nth 0 (nth i paramlist)))
1581 (indent-to (+ column parlen 1))
1583 (set 'column (current-column))
1585 ;; insert 'in' or space
1586 (if (nth 1 (nth i paramlist))
1591 (not (nth 3 (nth i paramlist))))
1594 ;; insert 'out' or space
1595 (if (nth 2 (nth i paramlist))
1600 (not (nth 3 (nth i paramlist))))
1604 (if (nth 3 (nth i paramlist))
1607 (set 'column (current-column))
1609 ;; insert type-name and, if necessary, space and default-expression
1610 (insert (nth 4 (nth i paramlist)))
1611 (if (nth 5 (nth i paramlist))
1613 (indent-to (+ column typlen 1))
1614 (insert (nth 5 (nth i paramlist)))))
1616 ;; check if it was the last parameter
1619 ;; no => insert ';' and newline and indent
1622 (indent-to firstcol))
1625 ;; if anything follows, except semicolon, newline, is or return
1626 ;; put it in a new line and indent it
1627 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
1628 (ada-indent-newline-indent))
1632 ;;;----------------------------;;;
1633 ;;; Move To Matching Start/End ;;;
1634 ;;;----------------------------;;;
1635 (defun ada-move-to-start ()
1636 "Moves point to the matching start of the current Ada structure."
1639 (previous-syntax-table (syntax-table)))
1642 (set-syntax-table ada-mode-symbol-syntax-table)
1644 (message "searching for block start ...")
1647 ;; do nothing if in string or comment or not on 'end ...;'
1648 ;; or if an error occurs during processing
1651 (ada-in-string-or-comment-p)
1653 (or (looking-at "[ \t]*\\<end\\>")
1655 (or (looking-at "[ \t]*\\<end\\>")
1657 (or (looking-at "[ \t]*\\<end\\>")
1658 (error "not on end ...;")))
1659 (ada-goto-matching-start 1)
1663 ;; on 'begin' => go on, according to user option
1665 ada-move-to-declaration
1666 (looking-at "\\<begin\\>")
1667 (ada-goto-matching-decl-start)
1668 (set 'pos (point))))
1670 ) ; end of save-excursion
1672 ;; now really move to the found position
1674 (message "searching for block start ... done"))
1677 ;; restore syntax-table
1679 (set-syntax-table previous-syntax-table))))
1681 (defun ada-move-to-end ()
1682 "Moves point to the matching end of the current block around point.
1683 Moves to 'begin' if in a declarative part."
1686 (previous-syntax-table (syntax-table)))
1689 (set-syntax-table ada-mode-symbol-syntax-table)
1691 (message "searching for block end ...")
1696 ;; directly on 'begin'
1698 (ada-goto-previous-word)
1699 (looking-at "\\<begin\\>"))
1700 (ada-goto-matching-end 1))
1701 ;; on first line of defun declaration
1703 (and (ada-goto-stmt-start)
1704 (looking-at "\\<function\\>\\|\\<procedure\\>" )))
1705 (ada-search-ignore-string-comment "begin" nil nil nil
1706 'word-search-forward))
1707 ;; on first line of task declaration
1709 (and (ada-goto-stmt-start)
1710 (looking-at "\\<task\\>" )
1712 (ada-goto-next-non-ws)
1713 (looking-at "\\<body\\>")))
1714 (ada-search-ignore-string-comment "begin" nil nil nil
1715 'word-search-forward))
1716 ;; accept block start
1718 (and (ada-goto-stmt-start)
1719 (looking-at "\\<accept\\>" )))
1720 (ada-goto-matching-end 0))
1723 (and (ada-goto-matching-decl-start t)
1724 (looking-at "\\<package\\>")))
1725 (ada-goto-matching-end 1))
1726 ;; inside a 'begin' ... 'end' block
1728 (ada-goto-matching-decl-start t))
1729 (ada-search-ignore-string-comment "begin" nil nil nil
1730 'word-search-forward))
1731 ;; (hopefully ;-) everything else
1733 (ada-goto-matching-end 1)))
1737 ;; now really move to the found position
1739 (message "searching for block end ... done"))
1741 ;; restore syntax-table
1742 (set-syntax-table previous-syntax-table))))
1745 ;;;----------------------------------------------------------------
1746 ;; Indentation Engine
1747 ;; All indentations are indicated as a two-element string:
1748 ;; - position of reference in the buffer
1749 ;; - offset to indent from this position (can also be a symbol or a list
1750 ;; that are evaluated)
1751 ;; Thus the total indentation for a line is the column number of the reference
1752 ;; position plus whatever value the evaluation of the second element provides.
1753 ;; This mechanism is used so that the ada-mode can "explain" how the
1754 ;; indentation was calculated, by showing which variables were used.
1756 ;; The indentation itself is done in only one pass: first we try to guess in
1757 ;; what context we are by looking at the following keyword or punctuation
1758 ;; sign. If nothing remarkable is found, just try to guess the indentation
1759 ;; based on previous lines.
1761 ;; The relevant functions for indentation are:
1762 ;; - `ada-indent-region': Re-indent a region of text
1763 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
1764 ;; calculation that were done
1765 ;; - `ada-indent-current': Re-indent the current line
1766 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
1767 ;; based on the context (see above).
1768 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
1769 ;; For efficiency, these functions do not check the correct context.
1770 ;;;----------------------------------------------------------------
1772 (defun ada-indent-region (beg end)
1773 "Indent the region between BEG and END."
1776 (let ((block-done 0)
1777 (lines-remaining (count-lines beg end))
1778 (msg (format "indenting %4d lines %%4d lines remaining ..."
1779 (count-lines beg end)))
1780 (endmark (copy-marker end)))
1781 ;; catch errors while indenting
1782 (while (< (point) endmark)
1783 (if (> block-done 39)
1784 (progn (message msg lines-remaining)
1785 (set 'block-done 0)))
1786 (if (looking-at "^$") nil
1787 (ada-indent-current))
1789 (set 'block-done (1+ block-done))
1790 (set 'lines-remaining (1- lines-remaining)))
1791 (message "indenting ... done")))
1793 (defun ada-indent-newline-indent ()
1794 "Indents the current line, inserts a newline and then indents the new line."
1796 (ada-indent-current)
1798 (ada-indent-current))
1800 (defun ada-indent-newline-indent-conditional ()
1801 "Insert a newline and indent it.
1802 The original line is indented first if `ada-indent-after-return' is non-nil.
1803 This function is intended to be bound to the \C-m and \C-j keys."
1805 (if ada-indent-after-return (ada-indent-current))
1807 (ada-indent-current))
1809 (defun ada-justified-indent-current ()
1810 "Indent the current line and explains how the calculation was done."
1813 (let ((cur-indent (ada-indent-current)))
1816 (if (equal (cdr cur-indent) '(0))
1817 (message "same indentation")
1818 (message (mapconcat (lambda(x)
1823 (number-to-string x))
1825 (concat "- " (symbol-name (cadr x))))
1830 (goto-char (car cur-indent))
1833 (defun ada-indent-current ()
1834 "Indent current line as Ada code.
1835 Returns the calculation that was done, including the reference point and the
1838 (let ((previous-syntax-table (syntax-table))
1839 (orgpoint (point-marker))
1840 cur-indent tmp-indent
1843 (set-syntax-table ada-mode-symbol-syntax-table)
1845 ;; This need to be done here so that the advice is not always activated
1846 ;; (this might interact badly with other modes)
1848 (ad-activate 'parse-partial-sexp t))
1855 ;; Not First line in the buffer ?
1857 (if (save-excursion (zerop (forward-line -1)))
1859 (back-to-indentation)
1860 (ada-get-current-indent))
1862 ;; first line in the buffer
1863 (list (point-min) 0))))
1865 ;; Evaluate the list to get the column to indent to
1866 ;; prev-indent contains the column to indent to
1867 (set 'prev-indent (save-excursion (goto-char (car cur-indent))
1869 (set 'tmp-indent (cdr cur-indent))
1870 (while (not (null tmp-indent))
1872 ((numberp (car tmp-indent))
1873 (set 'prev-indent (+ prev-indent (car tmp-indent))))
1875 (set 'prev-indent (+ prev-indent (eval (car tmp-indent)))))
1877 (set 'tmp-indent (cdr tmp-indent)))
1879 ;; only re-indent if indentation is different then the current
1880 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
1883 (delete-horizontal-space)
1884 (indent-to prev-indent))
1886 ;; restore position of point
1888 (goto-char orgpoint)
1889 (if (< (current-column) (current-indentation))
1890 (back-to-indentation))))
1892 ;; restore syntax-table
1894 (ad-deactivate 'parse-partial-sexp))
1895 (set-syntax-table previous-syntax-table)
1899 (defun ada-get-current-indent ()
1900 "Returns the indentation to use for the current line."
1904 (orgpoint (save-excursion
1906 (forward-comment -10000)
1911 ;; preprocessor line (gnatprep)
1913 ((and (equal ada-which-compiler 'gnat)
1914 (looking-at "#[ \t]*\\(if\\|else\\|elsif\\|end[ \t]*if\\)"))
1915 (list (save-excursion (beginning-of-line) (point)) 0))
1918 ;; in open parenthesis, but not in parameter-list
1921 ada-indent-to-open-paren
1922 (not (ada-in-paramlist-p))
1923 (set 'column (ada-in-open-paren-p)))
1924 ;; check if we have something like this (Table_Component_Type =>
1925 ;; Source_File_Record)
1927 (if (and (skip-chars-backward " \t")
1928 (= (char-before) ?\n)
1929 (not (forward-comment -10000))
1930 (= (char-before) ?>))
1931 (list column 'ada-broken-indent);; ??? Could use a different variable
1937 ((looking-at "\\<end\\>")
1940 (ada-goto-matching-start 1)
1943 ;; found 'loop' => skip back to 'while' or 'for'
1944 ;; if 'loop' is not on a separate line
1948 (looking-at ".+\\<loop\\>"))
1952 (ada-search-ignore-string-comment ada-loop-start-re t))
1953 (not (looking-at "\\<loop\\>"))))
1955 (goto-char (car match-cons))
1958 (if (looking-at ada-named-block-re)
1959 (set 'label (- ada-label-indent)))))))
1961 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))))
1965 ((looking-at "\\<exception\\>")
1967 (ada-goto-matching-start 1)
1968 (list (save-excursion (back-to-indentation) (point)) 0)))
1972 ((looking-at "\\<when\\>")
1974 (ada-goto-matching-start 1)
1975 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent)))
1979 ((looking-at "\\<else\\>")
1980 (if (save-excursion (ada-goto-previous-word)
1981 (looking-at "\\<or\\>"))
1982 (ada-indent-on-previous-lines nil orgpoint orgpoint)
1984 (ada-goto-matching-start 1 nil t)
1985 (list (progn (back-to-indentation) (point)) 0))))
1989 ((looking-at "\\<elsif\\>")
1991 (ada-goto-matching-start 1 nil t)
1992 (list (progn (back-to-indentation) (point)) 0)))
1996 ((looking-at "\\<then\\>")
1997 (if (save-excursion (ada-goto-previous-word)
1998 (looking-at "\\<and\\>"))
1999 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2001 ;; Select has been added for the statement: "select ... then abort"
2002 (ada-search-ignore-string-comment "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2003 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent))))
2007 ((looking-at "\\<loop\\>")
2010 (goto-char (match-end 0))
2011 (ada-goto-stmt-start)
2012 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2013 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2014 (unless (looking-at ada-loop-start-re)
2015 (ada-search-ignore-string-comment ada-loop-start-re
2017 (if (looking-at "\\<loop\\>")
2018 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2019 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2023 ((looking-at "\\<begin\\>")
2025 (if (ada-goto-matching-decl-start t)
2026 (list (progn (back-to-indentation) (point)) 0)
2027 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2031 ((looking-at "\\<is\\>")
2032 (if (and ada-indent-is-separate
2034 (goto-char (match-end 0))
2035 (ada-goto-next-non-ws (save-excursion (end-of-line)
2037 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2039 (ada-goto-stmt-start)
2040 (list (progn (back-to-indentation) (point)) 'ada-indent))
2042 (ada-goto-stmt-start)
2043 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent))))
2047 ((looking-at "\\<record\\>")
2049 (ada-search-ignore-string-comment
2050 "\\<\\(type\\|use\\)\\>" t nil)
2051 (if (looking-at "\\<use\\>")
2052 (ada-search-ignore-string-comment "for" t nil nil 'word-search-backward))
2053 (list (progn (back-to-indentation) (point)) 'ada-indent-record-rel-type)))
2055 ;; 'or' as statement-start
2056 ;; 'private' as statement-start
2058 ((or (ada-looking-at-semi-or)
2059 (ada-looking-at-semi-private))
2061 (ada-goto-matching-start 1)
2062 (list (progn (back-to-indentation) (point)) 0)))
2064 ;; new/abstract/separate
2066 ((looking-at "\\<\\(new\\|abstract\\|separate\\)\\>")
2067 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2071 ((looking-at "\\<return\\>")
2073 (forward-comment -1000)
2074 (if (= (char-before) ?\))
2078 ;; If there is a parameter list, and we have a function declaration
2079 (if (and (= (char-after) ?\()
2082 (looking-at "\\<function\\>")))
2084 ;; The indentation depends of the value of ada-indent-return
2085 (if (<= ada-indent-return 0)
2086 (list (point) (- ada-indent-return))
2087 (list (progn (backward-sexp 2) (point)) ada-indent-return))
2089 ;; Else there is no parameter list, but we have a function
2090 ;; Only do something special if the user want to indent relative
2091 ;; to the "function" keyword
2092 (if (and (> ada-indent-return 0)
2093 (save-excursion (forward-word -1)
2094 (looking-at "\\<function\\>")))
2095 (list (progn (forward-word -1) (point)) ada-indent-return)
2098 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
2102 ((looking-at "\\<do\\>")
2104 (ada-goto-stmt-start)
2105 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2107 ;; package/function/procedure
2109 ((and (looking-at "\\<\\(package\\|function\\|procedure\\)\\>")
2112 (ada-goto-stmt-start)
2113 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>")))
2115 ;; look for 'generic'
2116 (if (and (ada-goto-matching-decl-start t)
2117 (looking-at "generic"))
2118 (list (progn (back-to-indentation) (point)) 0)
2119 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2123 ((looking-at "\\<\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2125 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2126 (set 'pos (ada-indent-on-previous-lines nil orgpoint orgpoint))
2129 'ada-label-indent)))
2131 ;; identifier and other noindent-statements
2133 ((looking-at "\\<\\(\\sw\\|_\\)+[ \t\n]*")
2134 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2136 ;; beginning of a parameter list
2138 ((and (not (eobp)) (= (char-after) ?\())
2139 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2141 ;; end of a parameter list
2143 ((and (not (eobp)) (= (char-after) ?\)))
2152 (if ada-indent-comment-as-code
2153 ;; If previous line is a comment, indent likewise
2157 (if (looking-at "[ \t]*--")
2158 (list (progn (back-to-indentation) (point)) 0)
2159 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2160 (list (save-excursion (back-to-indentation) (point)) 0)))
2165 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
2167 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2168 "Calculate the indentation for the new line after ORGPOINT.
2169 The result list is based on the previous lines in the buffer.
2170 If NOMOVE is nil, moves point to the beginning of the current statement.
2171 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2173 (goto-char initial-pos))
2174 (let ((oldpoint (point))
2177 ;; Is inside a parameter-list ?
2179 (if (ada-in-paramlist-p)
2180 (set 'result (ada-get-indent-paramlist))
2183 ;; move to beginning of current statement
2186 (ada-goto-stmt-start))
2191 ;; no beginning found => don't change indentation
2193 (if (and (eq oldpoint (point))
2195 (set 'result (ada-get-indent-nochange))
2200 ada-indent-to-open-paren
2201 (ada-in-open-paren-p))
2202 (set 'result (ada-get-indent-open-paren)))
2204 ((looking-at "end\\>")
2205 (set 'result (ada-get-indent-end orgpoint)))
2207 ((looking-at ada-loop-start-re)
2208 (set 'result (ada-get-indent-loop orgpoint)))
2210 ((looking-at ada-subprog-start-re)
2211 (set 'result (ada-get-indent-subprog orgpoint)))
2213 ((looking-at ada-block-start-re)
2214 (set 'result (ada-get-indent-block-start orgpoint)))
2216 ((looking-at "\\(sub\\)?type\\>")
2217 (set 'result (ada-get-indent-type orgpoint)))
2219 ((looking-at "\\(els\\)?if\\>")
2220 (set 'result (ada-get-indent-if orgpoint)))
2222 ((looking-at "case\\>")
2223 (set 'result (ada-get-indent-case orgpoint)))
2225 ((looking-at "when\\>")
2226 (set 'result (ada-get-indent-when orgpoint)))
2228 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2229 (set 'result (ada-get-indent-label orgpoint)))
2231 ((looking-at "separate\\>")
2232 (set 'result (ada-get-indent-nochange)))
2234 (set 'result (ada-get-indent-noindent orgpoint))))))))
2238 (defun ada-get-indent-open-paren ()
2239 "Calculates the indentation when point is behind an unclosed parenthesis."
2240 (list (ada-in-open-paren-p) 0))
2242 (defun ada-get-indent-nochange ()
2243 "Return the current indentation of the previous line."
2246 (back-to-indentation)
2249 (defun ada-get-indent-paramlist ()
2250 "Calculates the indentation when point is inside a parameter list."
2252 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2254 ;; in front of the first parameter
2255 ((= (char-after) ?\()
2256 (goto-char (match-end 0))
2259 ;; in front of another parameter
2260 ((= (char-after) ?\;)
2261 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2262 (ada-goto-next-non-ws)
2265 ;; inside a parameter declaration
2267 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2268 (ada-goto-next-non-ws)
2269 (list (point) 'ada-broken-indent)))))
2271 (defun ada-get-indent-end (orgpoint)
2272 "Calculates the indentation when point is just before an end_statement.
2273 ORGPOINT is the limit position used in the calculation."
2274 (let ((defun-name nil)
2278 ;; is the line already terminated by ';' ?
2281 (ada-search-ignore-string-comment ";" nil orgpoint nil
2284 ;; yes, look what's following 'end'
2288 (ada-goto-next-non-ws)
2290 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\)\\>")
2291 (save-excursion (ada-check-matching-start (match-string 0)))
2292 (list (save-excursion (back-to-indentation) (point)) 0))
2295 ;; loop/select/if/case/record/select
2297 ((looking-at "\\<record\\>")
2299 (ada-check-matching-start (match-string 0))
2300 ;; we are now looking at the matching "record" statement
2302 (ada-goto-stmt-start)
2303 ;; now on the matching type declaration, or use clause
2304 (unless (looking-at "\\(for\\|type\\)\\>")
2305 (ada-search-ignore-string-comment "\\<type\\>" t))
2306 (list (progn (back-to-indentation) (point)) 0)))
2308 ;; a named block end
2310 ((looking-at ada-ident-re)
2311 (set 'defun-name (match-string 0))
2313 (ada-goto-matching-start 0)
2314 (ada-check-defun-name defun-name))
2315 (list (progn (back-to-indentation) (point)) 0))
2317 ;; a block-end without name
2319 ((= (char-after) ?\;)
2321 (ada-goto-matching-start 0)
2322 (if (looking-at "\\<begin\\>")
2324 (set 'indent (list (point) 0))
2325 (if (ada-goto-matching-decl-start t)
2326 (list (progn (back-to-indentation) (point)) 0)
2329 ;; anything else - should maybe signal an error ?
2332 (list (save-excursion (back-to-indentation) (point))
2333 'ada-broken-indent))))
2335 (list (save-excursion (back-to-indentation) (point))
2336 'ada-broken-indent))))
2338 (defun ada-get-indent-case (orgpoint)
2339 "Calculates the indentation when point is just before a case statement.
2340 ORGPOINT is the limit position used in the calculation."
2341 (let ((match-cons nil)
2345 ;; case..is..when..=>
2348 (set 'match-cons (and
2349 ;; the `=>' must be after the keyword `is'.
2350 (ada-search-ignore-string-comment
2351 "is" nil orgpoint nil 'word-search-forward)
2352 (ada-search-ignore-string-comment
2353 "[ \t\n]+=>" nil orgpoint))))
2355 (goto-char (car match-cons))
2356 (unless (ada-search-ignore-string-comment "when" t opos)
2357 (error "missing 'when' between 'case' and '=>'"))
2358 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2363 (set 'match-cons (ada-search-ignore-string-comment
2364 "when" nil orgpoint nil 'word-search-forward)))
2365 (goto-char (cdr match-cons))
2366 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2371 (set 'match-cons (ada-search-ignore-string-comment
2372 "is" nil orgpoint nil 'word-search-forward)))
2373 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2378 (list (save-excursion (back-to-indentation) (point))
2379 'ada-broken-indent)))))
2381 (defun ada-get-indent-when (orgpoint)
2382 "Calcules the indentation when point is just before a when statement.
2383 ORGPOINT is the limit position used in the calculation."
2384 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2385 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2386 (list cur-indent 'ada-indent)
2387 (list cur-indent 'ada-broken-indent))))
2389 (defun ada-get-indent-if (orgpoint)
2390 "Calculates the indentation when point is just before an if statement.
2391 ORGPOINT is the limit position used in the calculation."
2392 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2395 ;; Move to the correct then (ignore all "and then")
2397 (while (and (set 'match-cons (ada-search-ignore-string-comment
2398 "\\<\\(then\\|and[ \t]*then\\)\\>"
2400 (= (char-after (car match-cons)) ?a)))
2401 ;; If "then" was found (we are looking at it)
2405 ;; 'then' first in separate line ?
2406 ;; => indent according to 'then',
2407 ;; => else indent according to 'if'
2410 (back-to-indentation)
2411 (looking-at "\\<then\\>"))
2412 (set 'cur-indent (save-excursion (back-to-indentation) (point))))
2415 (list cur-indent 'ada-indent))
2417 (list cur-indent 'ada-broken-indent))))
2419 (defun ada-get-indent-block-start (orgpoint)
2420 "Calculates the indentation when point is at the start of a block.
2421 ORGPOINT is the limit position used in the calculation."
2426 (set 'pos (ada-goto-next-non-ws orgpoint)))
2429 (ada-indent-on-previous-lines t orgpoint)))
2431 ;; nothing follows the block-start
2433 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
2435 (defun ada-get-indent-subprog (orgpoint)
2436 "Calculates the indentation when point is just before a subprogram.
2437 ORGPOINT is the limit position used in the calculation."
2438 (let ((match-cons nil)
2439 (cur-indent (save-excursion (back-to-indentation) (point)))
2442 ;; is there an 'is' in front of point ?
2446 (ada-search-ignore-string-comment
2447 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
2449 ;; yes, then skip to its end
2453 (goto-char (cdr match-cons)))
2455 ;; no, then goto next non-ws, if there is one in front of point
2458 (unless (ada-goto-next-non-ws orgpoint)
2459 (goto-char orgpoint))))
2463 ;; nothing follows 'is'
2468 (not (ada-search-ignore-string-comment
2469 "[^ \t\n]" nil orgpoint t))))
2470 (list cur-indent 'ada-indent))
2472 ;; is abstract/separate/new ...
2478 (ada-search-ignore-string-comment
2479 "\\<\\(separate\\|new\\|abstract\\)\\>"
2481 (goto-char (car match-cons))
2482 (ada-search-ignore-string-comment ada-subprog-start-re t)
2483 (ada-get-indent-noindent orgpoint))
2485 ;; something follows 'is'
2489 (save-excursion (set 'match-cons (ada-goto-next-non-ws orgpoint)))
2490 (goto-char match-cons)
2491 (ada-indent-on-previous-lines t orgpoint)))
2496 (ada-search-ignore-string-comment ";" nil orgpoint nil
2498 (list cur-indent 0))
2503 (list cur-indent 'ada-broken-indent)))))
2505 (defun ada-get-indent-noindent (orgpoint)
2506 "Calculates the indentation when point is just before a 'noindent stmt'.
2507 ORGPOINT is the limit position used in the calculation."
2514 ;; This one is called when indenting a line preceded by a multiline
2515 ;; subprogram declaration (in that case, we are at this point inside
2516 ;; the parameter declaration list)
2517 ((ada-in-paramlist-p)
2518 (ada-previous-procedure)
2519 (list (save-excursion (back-to-indentation) (point)) 0))
2521 ;; This one is called when indenting the second line of a multi-line
2522 ;; declaration section, in a declare block or a record declaration
2523 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
2524 (list (save-excursion (back-to-indentation) (point))
2525 'ada-broken-decl-indent))
2527 ;; This one is called in every over case when indenting a line at the
2530 (if (looking-at ada-named-block-re)
2531 (set 'label (- ada-label-indent))
2533 ;; "with private" or "null record" cases
2534 (if (or (and (re-search-forward "\\<private\\>" orgpoint t)
2535 (save-excursion (forward-char -7);; skip back "private"
2536 (ada-goto-previous-word)
2537 (looking-at "with")))
2538 (and (re-search-forward "\\<record\\>" orgpoint t)
2539 (save-excursion (forward-char -6);; skip back "record"
2540 (ada-goto-previous-word)
2541 (looking-at "null"))))
2543 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
2544 (list (save-excursion (back-to-indentation) (point)) 0))))
2546 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
2547 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
2548 (list (+ (save-excursion (back-to-indentation) (point)) label)
2549 'ada-broken-indent)))))))
2551 (defun ada-get-indent-label (orgpoint)
2552 "Calculates the indentation when before a label or variable declaration.
2553 ORGPOINT is the limit position used in the calculation."
2554 (let ((match-cons nil)
2555 (cur-indent (save-excursion (back-to-indentation) (point))))
2556 (ada-search-ignore-string-comment ":" nil)
2560 (set 'match-cons (ada-search-ignore-string-comment
2561 ada-loop-start-re nil orgpoint)))
2562 (goto-char (car match-cons))
2563 (ada-get-indent-loop orgpoint))
2567 (set 'match-cons (ada-search-ignore-string-comment
2568 "\\<declare\\|begin\\>" nil orgpoint)))
2569 (goto-char (car match-cons))
2570 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
2572 ;; variable declaration
2575 (ada-search-ignore-string-comment ";" nil orgpoint))
2577 (list cur-indent 'ada-broken-indent)))
2579 ;; nothing follows colon
2581 (list cur-indent '(- ada-label-indent))))))
2583 (defun ada-get-indent-loop (orgpoint)
2584 "Calculates the indentation when just before a loop or a for ... use.
2585 ORGPOINT is the limit position used in the calculation."
2586 (let ((match-cons nil)
2589 ;; If looking at a named block, skip the label
2590 (label (save-excursion
2592 (if (looking-at ada-named-block-re)
2593 (- ada-label-indent)
2599 ;; statement complete
2602 (ada-search-ignore-string-comment ";" nil orgpoint nil
2604 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
2608 ((looking-at "loop\\>")
2609 (set 'pos (ada-get-indent-block-start orgpoint))
2612 (list (+ (car pos) label) (cdr pos))))
2615 ;; 'for'- loop (or also a for ... use statement)
2617 ((looking-at "for\\>")
2624 (goto-char (match-end 0))
2625 (ada-goto-next-non-ws orgpoint)
2627 (if (= (char-after) ?') (forward-word 1) t)
2628 (ada-goto-next-non-ws orgpoint)
2629 (looking-at "\\<use\\>")
2631 ;; check if there is a 'record' before point
2634 (set 'match-cons (ada-search-ignore-string-comment
2635 "record" nil orgpoint nil 'word-search-forward))
2638 (goto-char (car match-cons)))
2639 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
2644 (set 'match-cons (ada-search-ignore-string-comment
2645 "loop" nil orgpoint nil 'word-search-forward)))
2646 (goto-char (car match-cons))
2648 ;; indent according to 'loop', if it's first in the line;
2649 ;; otherwise to 'for'
2651 (unless (save-excursion
2652 (back-to-indentation)
2653 (looking-at "\\<loop\\>"))
2655 (list (+ (save-excursion (back-to-indentation) (point)) label)
2658 ;; for-statement is broken
2661 (list (+ (save-excursion (back-to-indentation) (point)) label)
2662 'ada-broken-indent))))
2667 ((looking-at "while\\>")
2672 (set 'match-cons (ada-search-ignore-string-comment
2673 "loop" nil orgpoint nil 'word-search-forward)))
2676 (goto-char (car match-cons))
2678 ;; indent according to 'loop', if it's first in the line;
2679 ;; otherwise to 'while'.
2681 (unless (save-excursion
2682 (back-to-indentation)
2683 (looking-at "\\<loop\\>"))
2685 (list (+ (save-excursion (back-to-indentation) (point)) label)
2688 (list (+ (save-excursion (back-to-indentation) (point)) label)
2689 'ada-broken-indent))))))
2692 (defun ada-get-indent-type (orgpoint)
2693 "Calculates the indentation when before a type statement.
2694 ORGPOINT is the limit position used in the calculation."
2695 (let ((match-dat nil))
2698 ;; complete record declaration
2702 (set 'match-dat (ada-search-ignore-string-comment
2703 "end" nil orgpoint nil 'word-search-forward))
2704 (ada-goto-next-non-ws)
2705 (looking-at "\\<record\\>")
2707 (ada-goto-next-non-ws)
2708 (= (char-after) ?\;)))
2709 (goto-char (car match-dat))
2710 (list (save-excursion (back-to-indentation) (point)) 0))
2715 (set 'match-dat (ada-search-ignore-string-comment
2716 "record" nil orgpoint nil 'word-search-forward)))
2717 (goto-char (car match-dat))
2718 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
2720 ;; complete type declaration
2723 (ada-search-ignore-string-comment ";" nil orgpoint nil
2725 (list (save-excursion (back-to-indentation) (point)) 0))
2727 ;; "type ... is", but not "type ... is ...", which is broken
2731 (ada-search-ignore-string-comment "is" nil orgpoint nil
2732 'word-search-forward)
2733 (not (ada-goto-next-non-ws orgpoint))))
2734 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2739 (list (save-excursion (back-to-indentation) (point))
2740 'ada-broken-indent)))))
2743 ;; -----------------------------------------------------------
2744 ;; -- searching and matching
2745 ;; -----------------------------------------------------------
2747 (defun ada-goto-stmt-start ()
2748 "Moves point to the beginning of the statement that point is in or after.
2749 Returns the new position of point.
2750 As a special case, if we are looking at a closing parenthesis, skip to the
2752 (let ((match-dat nil)
2755 (set 'match-dat (ada-search-prev-end-stmt))
2759 ;; found a previous end-statement => check if anything follows
2761 (unless (looking-at "declare")
2763 (unless (save-excursion
2764 (goto-char (cdr match-dat))
2765 (ada-goto-next-non-ws orgpoint))
2767 ;; nothing follows => it's the end-statement directly in
2768 ;; front of point => search again
2770 (set 'match-dat (ada-search-prev-end-stmt)))
2772 ;; if found the correct end-statement => goto next non-ws
2775 (goto-char (cdr match-dat)))
2776 (ada-goto-next-non-ws)
2780 ;; no previous end-statement => we are at the beginning of the
2781 ;; accessible part of the buffer
2784 (goto-char (point-min))
2786 ;; skip to the very first statement, if there is one
2788 (unless (ada-goto-next-non-ws orgpoint)
2789 (goto-char orgpoint))))
2794 (defun ada-search-prev-end-stmt ()
2795 "Moves point to previous end-statement.
2796 Returns a cons cell whose car is the beginning and whose cdr the end of the
2798 (let ((match-dat nil)
2803 ;; search until found or beginning-of-buffer
2808 (set 'match-dat (ada-search-ignore-string-comment
2809 ada-end-stmt-re t)))
2811 (goto-char (car match-dat))
2812 (unless (ada-in-open-paren-p)
2813 (if (and (looking-at
2814 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
2816 (ada-goto-previous-word)
2817 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
2821 (goto-char (cdr match-dat))
2822 (ada-goto-next-non-ws)
2824 ;; words that can go after an 'is'
2828 (regexp-opt '("separate" "access" "array"
2829 "abstract" "new") t)
2839 (defun ada-goto-next-non-ws (&optional limit)
2840 "Skips white spaces, newlines and comments to next non-ws character.
2841 Stop the search at LIMIT.
2842 Do not call this function from within a string."
2844 (set 'limit (point-max)))
2845 (while (and (<= (point) limit)
2846 (progn (forward-comment 10000)
2847 (if (and (not (eobp))
2848 (save-excursion (forward-char 1)
2850 (progn (forward-sexp 1) t)))))
2851 (if (< (point) limit)
2857 (defun ada-goto-stmt-end (&optional limit)
2858 "Moves point to the end of the statement that point is in or before.
2859 Returns the new position of point or nil if not found.
2860 Stop the search at LIMIT."
2861 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
2866 (defun ada-goto-next-word (&optional backward)
2867 "Moves point to the beginning of the next word of Ada code.
2868 If BACKWARD is non-nil, jump to the beginning of the previous word.
2869 Returns the new position of point or nil if not found."
2870 (let ((match-cons nil)
2872 (old-syntax (char-to-string (char-syntax ?_))))
2873 (modify-syntax-entry ?_ "w")
2875 (skip-syntax-forward "w"));; ??? Used to have . too
2876 (if (set 'match-cons
2878 (ada-search-ignore-string-comment "\\w" t nil t)
2879 (ada-search-ignore-string-comment "\\w" nil nil t)))
2881 ;; move to the beginning of the word found
2884 (goto-char (car match-cons))
2885 (skip-syntax-backward "w")
2888 ;; if not found, restore old position of point
2890 (goto-char orgpoint)
2892 (modify-syntax-entry ?_ old-syntax))
2896 (defsubst ada-goto-previous-word ()
2897 "Moves point to the beginning of the previous word of Ada code.
2898 Returns the new position of point or nil if not found."
2899 (ada-goto-next-word t))
2902 (defun ada-check-matching-start (keyword)
2903 "Signals an error if matching block start is not KEYWORD.
2904 Moves point to the matching block start."
2905 (ada-goto-matching-start 0)
2906 (unless (looking-at (concat "\\<" keyword "\\>"))
2907 (error "matching start is not '%s'" keyword)))
2910 (defun ada-check-defun-name (defun-name)
2911 "Checks if the name of the matching defun really is DEFUN-NAME.
2912 Assumes point to be already positioned by 'ada-goto-matching-start'.
2913 Moves point to the beginning of the declaration."
2915 ;; named block without a `declare'
2917 (ada-goto-previous-word)
2918 (looking-at (concat "\\<" defun-name "\\> *:")))
2921 ;; 'accept' or 'package' ?
2923 (unless (looking-at "\\<\\(accept\\|package\\|task\\|protected\\)\\>")
2924 (ada-goto-matching-decl-start))
2926 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
2930 ;; a named 'declare'-block ?
2932 (if (looking-at "\\<declare\\>")
2933 (ada-goto-stmt-start)
2935 ;; no, => 'procedure'/'function'/'task'/'protected'
2941 ;; skip 'body' 'type'
2943 (if (looking-at "\\<\\(body\\|type\\)\\>")
2948 ;; should be looking-at the correct name
2950 (unless (looking-at (concat "\\<" defun-name "\\>"))
2951 (error "matching defun has different name: %s"
2952 (buffer-substring (point)
2953 (progn (forward-sexp 1) (point))))))))
2955 (defun ada-goto-matching-decl-start (&optional noerror)
2956 "Moves point to the matching declaration start of the current 'begin'.
2957 If NOERROR is non-nil, it only returns nil if no match was found."
2958 (let ((nest-count 1)
2965 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
2967 (ada-search-ignore-string-comment
2968 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
2969 (looking-at "generic")))
2970 (set 'count-generic t))
2972 ;; search backward for interesting keywords
2974 (not (zerop nest-count))
2975 (ada-search-ignore-string-comment ada-matching-decl-start-re t))
2977 ;; calculate nest-depth
2982 (ada-goto-matching-start 1 noerror)
2984 ;; In some case, two begin..end block can follow each other closely,
2985 ;; which we have to detect, as in
2990 ;; begin -- here we should go to procedure, not begin
2993 (let ((loop-again 0))
2994 (if (looking-at "begin")
2995 (set 'loop-again 1))
2998 (while (not (= loop-again 0))
3000 ;; If begin was just there as the beginning of a block (with no
3001 ;; declare) then do nothing, otherwise just register that we
3002 ;; have to find the statement that required the begin
3004 (ada-search-ignore-string-comment
3005 "declare\\|begin\\|end\\|procedure\\|function\\|task\\|package"
3008 (if (looking-at "end")
3009 (set 'loop-again (1+ loop-again))
3011 (set 'loop-again (1- loop-again))
3012 (unless (looking-at "begin")
3013 (set 'nest-count (1+ nest-count))))
3017 ((looking-at "generic")
3021 (set 'nest-count (1- nest-count)))))
3023 ((looking-at "declare\\|generic\\|if")
3024 (set 'nest-count (1- nest-count))
3028 ;; check if it is only a type definition, but not a protected
3029 ;; type definition, which should be handled like a procedure.
3030 (if (or (looking-at "is[ \t]+<>")
3032 (forward-comment -10000)
3035 ;; Detect if we have a closing parenthesis (Could be
3036 ;; either the end of subprogram parameters or (<>)
3037 ;; in a type definition
3038 (if (= (char-after) ?\))
3042 (forward-comment -10000)
3044 (skip-chars-backward "a-zA-Z0-9_.'")
3045 (ada-goto-previous-word)
3047 (looking-at "\\<\\(sub\\)?type\\>")
3049 (ada-goto-previous-word)
3050 (not (looking-at "\\<protected\\>"))))
3052 (goto-char (match-beginning 0))
3054 (set 'nest-count (1- nest-count))
3060 (ada-goto-previous-word)
3062 (goto-char (match-beginning 0))))
3065 (looking-at "begin"))
3070 (set 'nest-count (1+ nest-count))
3075 ;; check if declaration-start is really found
3079 (if (looking-at "is")
3080 (ada-search-ignore-string-comment ada-subprog-start-re t)
3081 (looking-at "declare\\|generic")))
3084 (error "no matching proc/func/task/declare/package/protected")))
3087 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3088 "Moves point to the beginning of a block-start.
3089 Which block depends on the value of NEST-LEVEL, which defaults to zero. If
3090 NOERROR is non-nil, it only returns nil if no matching start was found.
3091 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3092 (let ((nest-count (if nest-level nest-level 0))
3097 ;; search backward for interesting keywords
3101 (ada-search-ignore-string-comment ada-matching-start-re t))
3103 (unless (and (looking-at "\\<record\\>")
3106 (looking-at "\\<null\\>")))
3109 ;; calculate nest-depth
3112 ;; found block end => increase nest depth
3114 (set 'nest-count (1+ nest-count)))
3116 ;; found loop/select/record/case/if => check if it starts or
3118 ((looking-at "loop\\|select\\|record\\|case\\|if")
3122 ;; check if keyword follows 'end'
3124 (ada-goto-previous-word)
3125 (if (looking-at "\\<end\\>[ \t]*[^;]")
3126 ;; it ends a block => increase nest depth
3128 (set 'nest-count (1+ nest-count))
3130 ;; it starts a block => decrease nest depth
3131 (set 'nest-count (1- nest-count))))
3134 ;; found package start => check if it really is a block
3135 ((looking-at "package")
3137 ;; ignore if this is just a renames statement
3138 (let ((current (point))
3139 (pos (ada-search-ignore-string-comment
3140 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3142 (goto-char (car pos))
3144 "No matching 'is' or 'renames' for 'package' at"
3146 (number-to-string (count-lines (point-min)
3148 (unless (looking-at "renames")
3151 (ada-goto-next-non-ws)
3152 ;; ignore it if it is only a declaration with 'new'
3153 (if (not (looking-at "\\<\\(new\\|separate\\)\\>"))
3154 (set 'nest-count (1- nest-count)))))))
3155 ;; found task start => check if it has a body
3156 ((looking-at "task")
3159 (ada-goto-next-non-ws)
3161 ((looking-at "\\<body\\>"))
3162 ((looking-at "\\<type\\>")
3163 ;; In that case, do nothing if there is a "is"
3164 (forward-word 2);; skip "type"
3165 (ada-goto-next-non-ws);; skip type name
3167 ;; Do nothing if we are simply looking at a simple
3168 ;; "task type name;" statement with no block
3169 (unless (looking-at ";")
3171 ;; Skip the parameters
3172 (if (looking-at "(")
3173 (ada-search-ignore-string-comment ")" nil))
3174 (let ((tmp (ada-search-ignore-string-comment
3175 "\\<\\(is\\|;\\)\\>" nil)))
3178 (goto-char (car tmp))
3179 (if (looking-at "is")
3180 (set 'nest-count (1- nest-count)))))))))
3182 ;; Check if that task declaration had a block attached to
3183 ;; it (i.e do nothing if we have just "task name;")
3184 (unless (progn (forward-word 1)
3185 (looking-at "[ \t]*;"))
3186 (set 'nest-count (1- nest-count)))))))
3187 ;; all the other block starts
3189 (set 'nest-count (1- nest-count)))) ; end of 'cond'
3191 ;; match is found, if nest-depth is zero
3193 (set 'found (zerop nest-count))))) ; end of loop
3197 ;; match found => is there anything else to do ?
3202 ;; found 'if' => skip to 'then', if it's on a separate line
3203 ;; and GOTOTHEN is non-nil
3209 (ada-search-ignore-string-comment "then" nil nil nil
3210 'word-search-forward)
3211 (back-to-indentation)
3212 (looking-at "\\<then\\>")))
3213 (goto-char (match-beginning 0)))
3215 ;; found 'do' => skip back to 'accept'
3218 (unless (ada-search-ignore-string-comment "accept" t nil nil
3219 'word-search-backward)
3220 (error "missing 'accept' in front of 'do'"))))
3225 (error "no matching start")))))
3228 (defun ada-goto-matching-end (&optional nest-level noerror)
3229 "Moves point to the end of a block.
3230 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3231 If NOERROR is non-nil, it only returns nil if found no matching start."
3232 (let ((nest-count (if nest-level nest-level 0))
3236 ;; search forward for interesting keywords
3240 (ada-search-ignore-string-comment
3243 (regexp-opt '("end" "loop" "select" "begin" "case"
3244 "if" "task" "package" "record" "do") t)
3248 ;; calculate nest-depth
3252 ;; found block end => decrease nest depth
3253 ((looking-at "\\<end\\>")
3254 (set 'nest-count (1- nest-count))
3255 ;; skip the following keyword
3257 (skip-chars-forward "end")
3258 (ada-goto-next-non-ws)
3259 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3261 ;; found package start => check if it really starts a block
3262 ((looking-at "\\<package\\>")
3263 (ada-search-ignore-string-comment "is" nil nil nil
3264 'word-search-forward)
3265 (ada-goto-next-non-ws)
3266 ;; ignore and skip it if it is only a 'new' package
3267 (if (looking-at "\\<new\\>")
3268 (goto-char (match-end 0))
3269 (set 'nest-count (1+ nest-count))))
3270 ;; all the other block starts
3272 (set 'nest-count (1+ nest-count))
3273 (forward-word 1))) ; end of 'cond'
3275 ;; match is found, if nest-depth is zero
3277 (set 'found (zerop nest-count))) ; end of loop
3283 (error "no matching end")))
3287 (defun ada-search-ignore-string-comment
3288 (search-re &optional backward limit paramlists search-func )
3289 "Regexp-search for SEARCH-RE, ignoring comments, strings.
3290 If PARAMLISTS is nil, ignore parameter lists. Returns a cons cell of
3291 begin and end of match data or nil, if not found.
3292 The search is done using SEARCH-FUNC, which should search backward if
3293 BACKWARD is non-nil, forward otherwise. SEARCH-FUNC can be optimized in case
3294 we are searching for a constant string.
3295 The search stops at pos LIMIT.
3296 Point is moved at the beginning of the search-re."
3301 (previous-syntax-table (syntax-table)))
3304 (set 'search-func (if backward 're-search-backward 're-search-forward)))
3307 ;; search until found or end-of-buffer
3308 ;; We have to test that we do not look further than limit
3310 (set-syntax-table ada-mode-symbol-syntax-table)
3311 (while (and (not found)
3313 (or (and backward (<= limit (point)))
3314 (>= limit (point))))
3315 (funcall search-func search-re limit 1))
3316 (set 'begin (match-beginning 0))
3317 (set 'end (match-end 0))
3319 (set 'parse-result (parse-partial-sexp
3320 (save-excursion (beginning-of-line) (point))
3325 ;; If inside a string, skip it (and the following comments)
3327 ((ada-in-string-p parse-result)
3329 (search-backward "\"" nil t)
3330 (goto-char (nth 8 parse-result)))
3331 (unless backward (forward-sexp 1)))
3333 ;; If inside a comment, skip it (and the following comments)
3334 ;; There is a special code for comments at the end of the file
3336 ((ada-in-comment-p parse-result)
3341 (forward-comment -1))
3342 (goto-char (nth 8 parse-result)))
3344 ;; at the end of the file, it is not possible to skip a comment
3345 ;; so we just go at the end of the line
3346 (if (forward-comment 1)
3348 (forward-comment 1000)
3349 (beginning-of-line))
3352 ;; directly in front of a comment => skip it, if searching forward
3354 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
3355 (unless backward (progn (forward-char -1) (forward-comment 1000))))
3358 ;; found a parameter-list but should ignore it => skip it
3360 ((and (not paramlists) (ada-in-paramlist-p))
3362 (search-backward "(" nil t)
3363 (search-forward ")" nil t)))
3365 ;; found what we were looking for
3368 (set 'found t)))) ; end of loop
3370 (set-syntax-table previous-syntax-table)
3376 ;; -------------------------------------------------------
3377 ;; -- Testing the position of the cursor
3378 ;; -------------------------------------------------------
3380 (defun ada-in-decl-p ()
3381 "Returns t if point is inside a declarative part.
3382 Assumes point to be at the end of a statement."
3383 (or (ada-in-paramlist-p)
3385 (ada-goto-matching-decl-start t))))
3388 (defun ada-looking-at-semi-or ()
3389 "Returns t if looking-at an 'or' following a semicolon."
3391 (and (looking-at "\\<or\\>")
3394 (ada-goto-stmt-start)
3395 (looking-at "\\<or\\>")))))
3398 (defun ada-looking-at-semi-private ()
3399 "Returns t if looking-at an 'private' following a semicolon.
3400 Returns nil if the private is part of the package name, as in
3401 'private package A is...' (this can only happen at top level)."
3403 (and (looking-at "\\<private\\>")
3404 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
3405 (progn (forward-comment -1000)
3406 (= (char-before) ?\;)))))
3408 (defsubst ada-in-comment-p (&optional parse-result)
3409 "Returns t if inside a comment."
3410 (nth 4 (or parse-result
3412 (save-excursion (beginning-of-line) (point)) (point)))))
3414 (defsubst ada-in-string-p (&optional parse-result)
3415 "Returns t if point is inside a string.
3416 If parse-result is non-nil, use is instead of calling parse-partial-sexp."
3417 (nth 3 (or parse-result
3419 (save-excursion (beginning-of-line) (point)) (point)))))
3421 (defsubst ada-in-string-or-comment-p (&optional parse-result)
3422 "Returns t if inside a comment or string."
3423 (set 'parse-result (or parse-result
3425 (save-excursion (beginning-of-line) (point)) (point))))
3426 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
3428 (defun ada-in-paramlist-p ()
3429 "Returns t if point is inside a parameter-list."
3432 (re-search-backward "(\\|)" nil t)
3433 ;; inside parentheses ?
3434 (= (char-after) ?\()
3437 ;; We should ignore the case when the reserved keyword is in a
3438 ;; comment (for instance, when we have:
3441 ;; we should return nil
3443 (not (ada-in-string-or-comment-p))
3445 ;; right keyword two words before parenthesis ?
3446 ;; Type is in this list because of discriminants
3447 (looking-at (eval-when-compile
3449 "procedure\\|function\\|body\\|"
3450 "task\\|entry\\|accept\\|"
3451 "access[ \t]+procedure\\|"
3452 "access[ \t]+function\\|"
3456 (defun ada-in-open-paren-p ()
3457 "Returns the position of the first non-ws behind the last unclosed
3458 parenthesis, or nil."
3460 (let ((parse (parse-partial-sexp
3462 (or (car (ada-search-ignore-string-comment
3463 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
3469 (goto-char (1+ (nth 1 parse)))
3470 (skip-chars-forward " \t")
3474 ;;;-----------------------------------------------------------
3475 ;;; Behavior Of TAB Key
3476 ;;;-----------------------------------------------------------
3479 "Do indenting or tabbing according to `ada-tab-policy'.
3480 In Transient Mark mode, if the mark is active, operate on the contents
3481 of the region. Otherwise, operates only on the current line."
3483 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
3484 ((eq ada-tab-policy 'indent-auto)
3485 ;; transient-mark-mode and mark-active are not defined in XEmacs
3486 (if (or (and ada-xemacs (region-active-p))
3487 (and (not ada-xemacs)
3490 (ada-indent-region (region-beginning) (region-end))
3491 (ada-indent-current)))
3492 ((eq ada-tab-policy 'always-tab) (error "not implemented"))
3495 (defun ada-untab (arg)
3496 "Delete leading indenting according to `ada-tab-policy'."
3498 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
3499 ((eq ada-tab-policy 'indent-auto) (error "not implemented"))
3500 ((eq ada-tab-policy 'always-tab) (error "not implemented"))
3503 (defun ada-indent-current-function ()
3504 "Ada mode version of the indent-line-function."
3506 (let ((starting-point (point-marker)))
3509 (if (< (point) starting-point)
3510 (goto-char starting-point))
3511 (set-marker starting-point nil)
3514 (defun ada-tab-hard ()
3515 "Indent current line to next tab stop."
3519 (insert-char ? ada-indent))
3520 (if (save-excursion (= (point) (progn (beginning-of-line) (point))))
3521 (forward-char ada-indent)))
3523 (defun ada-untab-hard ()
3524 "indent current line to previous tab stop."
3526 (let ((bol (save-excursion (progn (beginning-of-line) (point))))
3527 (eol (save-excursion (progn (end-of-line) (point)))))
3528 (indent-rigidly bol eol (- 0 ada-indent))))
3532 ;; ------------------------------------------------------------
3534 ;; ------------------------------------------------------------
3536 (defun ada-remove-trailing-spaces ()
3537 "Remove trailing spaces in the whole buffer."
3543 (goto-char (point-min))
3544 (while (re-search-forward "[ \t]+$" (point-max) t)
3545 (replace-match "" nil nil))))))
3547 (defun ada-ff-other-window ()
3548 "Find other file in other window using `ff-find-other-file'."
3550 (and (fboundp 'ff-find-other-file)
3551 (ff-find-other-file t)))
3553 (defun ada-gnat-style ()
3554 "Clean up comments, `(' and `,' for GNAT style checking switch."
3557 (goto-char (point-min))
3558 (while (re-search-forward "-- ?\\([^ -]\\)" nil t)
3559 (replace-match "-- \\1"))
3560 (goto-char (point-min))
3561 (while (re-search-forward "\\>(" nil t)
3562 (replace-match " ("))
3563 (goto-char (point-min))
3564 (while (re-search-forward ",\\<" nil t)
3565 (replace-match ", "))
3570 ;; -------------------------------------------------------------
3571 ;; -- Moving To Procedures/Packages
3572 ;; -------------------------------------------------------------
3574 (defun ada-next-procedure ()
3575 "Moves point to next procedure."
3578 (if (re-search-forward ada-procedure-start-regexp nil t)
3579 (goto-char (match-beginning 1))
3580 (error "No more functions/procedures/tasks")))
3582 (defun ada-previous-procedure ()
3583 "Moves point to previous procedure."
3586 (if (re-search-backward ada-procedure-start-regexp nil t)
3587 (goto-char (match-beginning 1))
3588 (error "No more functions/procedures/tasks")))
3590 (defun ada-next-package ()
3591 "Moves point to next package."
3594 (if (re-search-forward ada-package-start-regexp nil t)
3595 (goto-char (match-beginning 1))
3596 (error "No more packages")))
3598 (defun ada-previous-package ()
3599 "Moves point to previous package."
3602 (if (re-search-backward ada-package-start-regexp nil t)
3603 (goto-char (match-beginning 1))
3604 (error "No more packages")))
3607 ;; ------------------------------------------------------------
3608 ;; -- Define keymap and menus for Ada
3609 ;; -------------------------------------------------------------
3611 (defun ada-create-keymap ()
3612 "Create the keymap associated with the Ada mode."
3614 ;; Indentation and Formatting
3615 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
3616 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
3617 (define-key ada-mode-map "\t" 'ada-tab)
3618 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
3619 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
3621 (define-key ada-mode-map '(shift tab) 'ada-untab)
3622 (define-key ada-mode-map [S-tab] 'ada-untab))
3623 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
3624 ;; We don't want to make meta-characters case-specific.
3627 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
3628 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
3629 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
3630 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
3633 (unless (lookup-key ada-mode-map "\C-c\C-c")
3634 (define-key ada-mode-map "\C-c\C-c" 'compile))
3637 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
3638 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
3639 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
3641 (define-key ada-mode-map "\177" 'backward-delete-char-untabify)
3644 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
3646 ;; Use predefined function of Emacs19 for comments (RE)
3647 (define-key ada-mode-map "\C-c;" 'comment-region)
3648 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
3652 (defun ada-create-menu ()
3653 "Create the ada menu as shown in the menu bar.
3654 This function is designed to be extensible, so that each compiler-specific file
3655 can add its own items."
3657 ;; Note that the separators must have different length in the submenus
3658 (autoload 'easy-menu-define "easymenu")
3659 (autoload 'imenu "imenu")
3661 ada-mode-menu ada-mode-map "Menu keymap for Ada mode"
3664 ["Ada Mode" (info "ada-mode") t])
3665 ["Customize" (customize-group 'ada) (>= emacs-major-version 20)]
3667 ["Next compilation error" next-error t]
3668 ["Previous Package" ada-previous-package t]
3669 ["Next Package" ada-next-package t]
3670 ["Previous Procedure" ada-previous-procedure t]
3671 ["Next Procedure" ada-next-procedure t]
3672 ["Goto Start Of Statement" ada-move-to-start t]
3673 ["Goto End Of Statement" ada-move-to-end t]
3675 ["Other File" ff-find-other-file t]
3676 ["Other File Other Window" ada-ff-other-window t])
3678 ["Indent Line" ada-indent-current-function t]
3679 ["Justify Current Indentation" ada-justified-indent-current t]
3680 ["Indent Lines in Selection" ada-indent-region t]
3681 ["Indent Lines in File" (ada-indent-region (point-min) (point-max)) t]
3682 ["Format Parameter List" ada-format-paramlist t]
3684 ["Comment Selection" comment-region t]
3685 ["Uncomment Selection" ada-uncomment-region t]
3687 ["Fill Comment Paragraph" fill-paragraph t]
3688 ["Fill Comment Paragraph Justify" ada-fill-comment-paragraph-justify t]
3689 ["Fill Comment Paragraph Postfix" ada-fill-comment-paragraph-postfix t]
3691 ["Adjust Case Selection" ada-adjust-case-region t]
3692 ["Adjust Case Buffer" ada-adjust-case-buffer t]
3693 ["Create Case Exception" ada-create-case-exception t]
3694 ["Reload Case Exceptions" ada-case-read-exceptions t]
3696 ["Make body for subprogram" ada-make-subprogram-body t]
3703 (easy-menu-add ada-mode-menu ada-mode-map)
3704 (define-key ada-mode-map [menu-bar] ada-mode-menu)
3705 (set 'mode-popup-menu (cons "Ada mode" ada-mode-menu))
3711 ;; -------------------------------------------------------
3712 ;; Commenting/Uncommenting code
3713 ;; The two following calls are provided to enhance the standard
3714 ;; comment-region function, which only allows uncommenting if the
3715 ;; comment is at the beginning of a line. If the line have been re-indented,
3716 ;; we are unable to use comment-region, which makes no sense.
3718 ;; In addition, we provide an interface to the standard comment handling
3719 ;; function for justifying the comments.
3720 ;; -------------------------------------------------------
3722 (defadvice comment-region (before ada-uncomment-anywhere)
3725 (string= mode-name "Ada"))
3727 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
3729 (while (re-search-forward cs end t)
3730 (replace-match comment-start))
3733 (defun ada-uncomment-region (beg end &optional arg)
3734 "Delete `comment-start' at the beginning of a line in the region."
3735 (interactive "r\nP")
3736 (ad-activate 'comment-region)
3737 (comment-region beg end (- (or arg 1)))
3738 (ad-deactivate 'comment-region))
3740 (defun ada-fill-comment-paragraph-justify ()
3741 "Fills current comment paragraph and justifies each line as well."
3743 (ada-fill-comment-paragraph 'full))
3745 (defun ada-fill-comment-paragraph-postfix ()
3746 "Fills current comment paragraph and justifies each line as well.
3747 Adds `ada-fill-comment-postfix' at the end of each line."
3749 (ada-fill-comment-paragraph 'full t))
3751 (defun ada-fill-comment-paragraph (&optional justify postfix)
3752 "Fills the current comment paragraph.
3753 If JUSTIFY is non-nil, each line is justified as well.
3754 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
3755 to each filled and justified line.
3756 The paragraph is indented on the first line."
3759 ;; check if inside comment or just in front a comment
3760 (if (and (not (ada-in-comment-p))
3761 (not (looking-at "[ \t]*--")))
3762 (error "not inside comment"))
3767 (opos (point-marker))
3769 ;; Sets this variable to nil, otherwise it prevents
3770 ;; fill-region-as-paragraph to work on Emacs <= 20.2
3771 (parse-sexp-lookup-properties nil)
3774 (fill-column (current-fill-column)))
3776 ;; Find end of paragraph
3777 (back-to-indentation)
3778 (while (and (not (eobp)) (looking-at "--[ \t]*[^ \t\n]"))
3780 (back-to-indentation))
3782 (set 'to (point-marker))
3785 ;; Find beginning of paragraph
3786 (back-to-indentation)
3787 (while (and (not (bobp)) (looking-at "--[ \t]*[^ \t\n]"))
3789 (back-to-indentation))
3792 (set 'from (point-marker))
3794 ;; Calculate the indentation we will need for the paragraph
3795 (back-to-indentation)
3796 (set 'indent (current-column))
3797 ;; unindent the first line of the paragraph
3798 (delete-region from (point))
3800 ;; Remove the old postfixes
3802 (while (re-search-forward (concat ada-fill-comment-postfix "\n") to t)
3803 (replace-match "\n"))
3806 (set 'to (point-marker))
3808 ;; Indent and justify the paragraph
3809 (set 'fill-prefix ada-fill-comment-prefix)
3810 (set-left-margin from to indent)
3812 (set 'fill-column (- fill-column (length ada-fill-comment-postfix))))
3814 (fill-region-as-paragraph from to justify)
3816 ;; Add the postfixes if required
3820 (narrow-to-region from to)
3823 (insert-char ? (- fill-column (current-column)))
3824 (insert ada-fill-comment-postfix)
3828 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
3829 ;; inserted at the end. Delete it
3831 (<= emacs-major-version 19)
3832 (and (= emacs-major-version 20)
3833 (<= emacs-minor-version 2)))
3841 ;; ---------------------------------------------------
3842 ;; support for find-file.el
3843 ;; These functions are used by find-file to guess the file names from
3844 ;; unit names, and to find the other file (spec or body) from the current
3845 ;; file (body or spec).
3846 ;; It is also used to find in which function we are, so as to put the
3847 ;; cursor at the correct position.
3848 ;; Standard Ada does not force any relation between unit names and file names,
3849 ;; so some of these functions can only be a good approximation. However, they
3850 ;; are also overriden in `ada-xref'.el when we know that the user is using
3852 ;; ---------------------------------------------------
3854 ;; Overriden when we work with GNAT, to use gnatkrunch
3855 (defun ada-make-filename-from-adaname (adaname)
3856 "Determine the filename in which ADANAME is found.
3857 This is a generic function, independent from any compiler."
3858 (while (string-match "\\." adaname)
3859 (set 'adaname (replace-match "-" t t adaname)))
3863 (defun ada-other-file-name ()
3864 "Return the name of the other file (the body if current-buffer is the spec,
3865 or the spec otherwise."
3866 (let ((ff-always-try-to-create nil)
3867 (buffer (current-buffer))
3869 (ff-find-other-file nil t) ;; same window, ignore 'with' lines
3871 ;; If the other file was not found, return an empty string
3872 (if (equal buffer (current-buffer))
3874 (set 'name (buffer-file-name))
3875 (switch-to-buffer buffer)
3878 (defun ada-which-function-are-we-in ()
3879 "Return the name of the function whose definition/declaration point is in.
3880 Redefines the function `ff-which-function-are-we-in'."
3881 (set 'ff-function-name nil)
3883 (end-of-line) ;; make sure we get the complete name
3884 (if (or (re-search-backward ada-procedure-start-regexp nil t)
3885 (re-search-backward ada-package-start-regexp nil t))
3886 (set 'ff-function-name (match-string 0)))
3889 (defun ada-set-point-accordingly ()
3890 "Move to the function declaration that was set by
3891 `ff-which-function-are-we-in'."
3892 (if ff-function-name
3894 (goto-char (point-min))
3895 (unless (ada-search-ignore-string-comment
3896 (concat ff-function-name "\\b") nil)
3897 (goto-char (point-min))))))
3900 ;; ---------------------------------------------------
3901 ;; support for font-lock.el
3902 ;; Strings are a real pain in Ada because a single quote character is
3903 ;; overloaded as a string quote and type/instance delimiter. By default, a
3904 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
3905 ;; So, for Font Lock mode purposes, we mark single quotes as having string
3906 ;; syntax when the gods that created Ada determine them to be.
3908 ;; This only works in Emacs. See the comments before the grammar functions
3909 ;; at the beginning of this file for how this is done with XEmacs.
3910 ;; ----------------------------------------------------
3912 (defconst ada-font-lock-syntactic-keywords
3913 ;; Mark single quotes as having string quote syntax in 'c' instances.
3914 ;; As a special case, ''' will not be highlighted, but if we do not
3915 ;; set this special case, then the rest of the buffer is highlighted as
3917 ;; This sets the properties of the characters, so that ada-in-string-p
3918 ;; correctly handles '"' too...
3919 '(("\\('\\)[^'\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
3920 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))
3923 (defvar ada-font-lock-keywords
3927 ;; handle "type T is access function return S;"
3928 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
3930 ;; preprocessor line
3931 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
3934 ;; accept, entry, function, package (body), protected (body|type),
3935 ;; pragma, procedure, task (body) plus name.
3941 "package[ \t]+body\\|"
3945 "protected[ \t]+body\\|"
3946 "protected[ \t]+type\\|"
3952 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
3953 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
3955 ;; Optional keywords followed by a type name.
3956 (list (concat ; ":[ \t]*"
3957 "\\<\\(access[ \t]+all\\|access\\|constant\\|in[ \t]+out\\|in\\|out\\)\\>"
3959 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
3960 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
3963 ;; Main keywords, except those treated specially below.
3966 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
3967 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
3968 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
3969 "generic" "if" "in" "is" "limited" "loop" "mod" "not"
3970 "null" "or" "others" "private" "protected" "raise"
3971 "range" "record" "rem" "renames" "requeue" "return" "reverse"
3972 "select" "separate" "tagged" "task" "terminate" "then" "until"
3973 "when" "while" "xor") t)
3976 ;; Anything following end and not already fontified is a body name.
3977 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
3978 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
3980 ;; Keywords followed by a type or function name.
3981 (list (concat "\\<\\("
3982 "new\\|of\\|subtype\\|type"
3983 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
3984 '(1 font-lock-keyword-face)
3985 '(2 (if (match-beginning 4)
3986 font-lock-function-name-face
3987 font-lock-type-face) nil t))
3989 ;; Keywords followed by a (comma separated list of) reference.
3990 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)\\>" ; "when" removed
3991 "[ \t\n]*\\(\\(\\sw\\|[_.|, \t\n]\\)+\\)\\W")
3992 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
3995 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
3997 "Default expressions to highlight in Ada mode.")
3999 ;; ---------------------------------------------------------
4000 ;; Support for outline.el
4001 ;; ---------------------------------------------------------
4003 (defun ada-outline-level ()
4004 "This is so that `current-column` DTRT in otherwise-hidden text"
4005 ;; patch from Dave Love <fx@gnu.org>
4006 (let (buffer-invisibility-spec)
4008 (back-to-indentation)
4011 ;; ---------------------------------------------------------
4012 ;; Automatic generation of code
4013 ;; The Ada-mode has a set of function to automatically generate a subprogram
4014 ;; or package body from its spec.
4015 ;; These function only use a primary and basic algorithm, this could use a
4016 ;; lot of improvement.
4017 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
4019 ;; ----------------------------------------------------------
4021 (defun ada-gen-treat-proc (match)
4022 "Make dummy body of a procedure/function specification.
4023 MATCH is a cons cell containing the start and end location of the last search
4024 for ada-procedure-start-regexp."
4025 (goto-char (car match))
4026 (let (func-found procname functype)
4028 ((or (looking-at "^[ \t]*procedure")
4029 (set 'func-found (looking-at "^[ \t]*function")))
4030 ;; treat it as a proc/func
4033 (set 'procname (buffer-substring (point) (cdr match))) ; store proc name
4035 ;; goto end of procname
4036 (goto-char (cdr match))
4038 ;; skip over parameterlist
4039 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
4042 ;; if function, skip over 'return' and result type.
4046 (skip-chars-forward " \t\n")
4047 (set 'functype (buffer-substring (point)
4052 ;; look for next non WS
4054 ((looking-at "[ \t]*;")
4055 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
4056 (ada-indent-newline-indent)
4058 (ada-indent-newline-indent)
4061 (insert "Result : " functype ";")
4062 (ada-indent-newline-indent)))
4064 (ada-indent-newline-indent)
4066 (insert "return Result;")
4068 (ada-indent-newline-indent)
4069 (insert "end " procname ";")
4070 (ada-indent-newline-indent)
4073 ((looking-at "[ \t\n]*is")
4076 ((looking-at "[ \t\n]*rename")
4080 (message "unknown syntax"))))
4082 (if (looking-at "^[ \t]*task")
4084 (message "Task conversion is not yet implemented")
4086 (if (looking-at "[ \t]*;")
4091 (defun ada-make-body ()
4092 "Create an Ada package body in the current buffer.
4093 The potential old buffer contents is deleted first, then we copy the
4094 spec buffer in here and modify it to make it a body.
4095 This function typically is to be hooked into `ff-file-created-hooks'."
4097 (delete-region (point-min) (point-max))
4098 (insert-buffer (car (cdr (buffer-list))))
4101 (let (found ada-procedure-or-package-start-regexp)
4103 (ada-search-ignore-string-comment ada-package-start-regexp nil))
4104 (progn (goto-char (cdr found))
4107 (error "No package"))
4109 (set 'ada-procedure-or-package-start-regexp
4110 (concat ada-procedure-start-regexp
4112 ada-package-start-regexp))
4115 (ada-search-ignore-string-comment
4116 ada-procedure-or-package-start-regexp nil))
4118 (goto-char (car found))
4119 (if (looking-at ada-package-start-regexp)
4120 (progn (goto-char (cdr found))
4122 (ada-gen-treat-proc found))))))
4124 (defun ada-make-subprogram-body ()
4125 "Make one dummy subprogram body from spec surrounding point."
4127 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
4128 (spec (match-beginning 0)))
4132 (if (and (re-search-forward "(\\|;" nil t)
4133 (= (char-before) ?\())
4135 (ada-search-ignore-string-comment ")" nil)
4136 (ada-search-ignore-string-comment ";" nil)))
4137 (set 'spec (buffer-substring spec (point)))
4139 ;; If find-file.el was available, use its functions
4140 (if (functionp 'ff-get-file)
4141 (find-file (ff-get-file
4142 ff-search-directories
4143 (ada-make-filename-from-adaname
4144 (file-name-nondirectory
4145 (file-name-sans-extension (buffer-name))))
4147 ;; Else emulate it very simply
4148 (find-file (concat (ada-make-filename-from-adaname
4149 (file-name-nondirectory
4150 (file-name-sans-extension (buffer-name))))
4155 (goto-char (point-max))
4156 (forward-comment -10000)
4157 (re-search-backward "\\<end\\>" nil t)
4158 ;; Move to the beginning of the elaboration part, if any
4159 (re-search-backward "^begin" nil t)
4163 (re-search-backward ada-procedure-start-regexp nil t)
4164 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
4166 (error "Not in subprogram spec"))))
4168 ;; --------------------------------------------------------
4169 ;; Global initializations
4170 ;; --------------------------------------------------------
4172 ;; Create the keymap once and for all. If we do that in ada-mode,
4173 ;; the keys changed in the user's .emacs have to be modified
4178 ;; Create the syntax tables, but do not activate them
4179 (ada-create-syntax-table)
4181 ;; Add the default extensions (and set up speedbar)
4182 (ada-add-extensions ".ads" ".adb")
4183 ;; This two files are generated by GNAT when running with -gnatD
4184 (if (equal ada-which-compiler 'gnat)
4185 (ada-add-extensions ".ads.dg" ".adb.dg"))
4187 ;; Read the special cases for exceptions
4188 (ada-case-read-exceptions)
4190 ;; include the other ada-mode files
4192 (if (equal ada-which-compiler 'gnat)
4194 ;; The order here is important: ada-xref defines the Project
4195 ;; submenu, and ada-prj adds to it.
4196 (condition-case nil (require 'ada-prj) (error nil))
4199 (condition-case nil (require 'ada-stmt) (error nil))
4201 ;;; provide ourselves
4204 ;;; ada-mode.el ends here