Spelling fixes.
[emacs.git] / lisp / progmodes / ada-mode.el
blobb8bf62d90b4eba138fd6b71b87be3babda034399
1 ;;; ada-mode.el --- major-mode for editing Ada sources
3 ;; Copyright (C) 1994-1995, 1997-2011 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: Stephen Leake <stephen_leake@member.fsf.org>
9 ;; Keywords: languages ada
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
27 ;; This mode is a major mode for editing Ada code. This is a major
28 ;; rewrite of the file packaged with Emacs-20. The Ada mode is
29 ;; composed of four Lisp files: ada-mode.el, ada-xref.el, ada-prj.el
30 ;; and ada-stmt.el. Only this file (ada-mode.el) is completely
31 ;; independent from the GNU Ada compiler GNAT, distributed by Ada
32 ;; Core Technologies. All the other files rely heavily on features
33 ;; provided only by GNAT.
35 ;; Note: this mode will not work with Emacs 19. If you are on a VMS
36 ;; system, where the latest version of Emacs is 19.28, you will need
37 ;; another file, called ada-vms.el, that provides some required
38 ;; functions.
40 ;;; Usage:
41 ;; Emacs should enter Ada mode automatically when you load an Ada file.
42 ;; By default, the valid extensions for Ada files are .ads, .adb or .ada
43 ;; If the ada-mode does not start automatically, then simply type the
44 ;; following command :
45 ;; M-x ada-mode
47 ;; By default, ada-mode is configured to take full advantage of the GNAT
48 ;; compiler (the menus will include the cross-referencing features,...).
49 ;; If you are using another compiler, you might want to set the following
50 ;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
51 ;; won't work) :
52 ;; (setq ada-which-compiler 'generic)
54 ;; This mode requires find-file.el to be present on your system.
56 ;;; History:
57 ;; The first Ada mode for GNU Emacs was written by V. Broman in
58 ;; 1985. He based his work on the already existing Modula-2 mode.
59 ;; This was distributed as ada.el in versions of Emacs prior to 19.29.
61 ;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
62 ;; several files with support for dired commands and other nice
63 ;; things. It is currently available from the PAL
64 ;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
66 ;; The probably very first Ada mode (called electric-ada.el) was
67 ;; written by Steven D. Litvintchouk and Steven M. Rosen for the
68 ;; Gosling Emacs. L. Slater based his development on ada.el and
69 ;; electric-ada.el.
71 ;; A complete rewrite by M. Heritsch and R. Ebert has been done.
72 ;; Some ideas from the Ada mode mailing list have been
73 ;; added. Some of the functionality of L. Slater's mode has not
74 ;; (yet) been recoded in this new mode. Perhaps you prefer sticking
75 ;; to his version.
77 ;; A complete rewrite for Emacs-20 / GNAT-3.11 has been done by Ada Core
78 ;; Technologies.
80 ;;; Credits:
81 ;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
82 ;; many patches included in this package.
83 ;; Christian Egli <Christian.Egli@hcsd.hac.com>:
84 ;; ada-imenu-generic-expression
85 ;; Many thanks also to the following persons that have contributed
86 ;; to the ada-mode
87 ;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
88 ;; woodruff@stc.llnl.gov (John Woodruff)
89 ;; jj@ddci.dk (Jesper Joergensen)
90 ;; gse@ocsystems.com (Scott Evans)
91 ;; comar@gnat.com (Cyrille Comar)
92 ;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
93 ;; robin-reply@reagans.org
94 ;; and others for their valuable hints.
96 ;;; Code:
97 ;; Note: Every function in this package is compiler-independent.
98 ;; The names start with ada-
99 ;; The variables that the user can edit can all be modified through
100 ;; the customize mode. They are sorted in alphabetical order in this
101 ;; file.
103 ;; Supported packages.
104 ;; This package supports a number of other Emacs modes. These other modes
105 ;; should be loaded before the ada-mode, which will then setup some variables
106 ;; to improve the support for Ada code.
107 ;; Here is the list of these modes:
108 ;; `which-function-mode': Display in the modeline the name of the subprogram
109 ;; the cursor is in.
110 ;; `outline-mode': Provides the capability to collapse or expand the code
111 ;; for specific language constructs, for instance if you want to hide the
112 ;; code corresponding to a subprogram
113 ;; `align': This mode is now provided with Emacs 21, but can also be
114 ;; installed manually for older versions of Emacs. It provides the
115 ;; capability to automatically realign the selected region (for instance
116 ;; all ':=', ':' and '--' will be aligned on top of each other.
117 ;; `imenu': Provides a menu with the list of entities defined in the current
118 ;; buffer, and an easy way to jump to any of them
119 ;; `speedbar': Provides a separate file browser, and the capability for each
120 ;; file to see the list of entities defined in it and to jump to them
121 ;; easily
122 ;; `abbrev-mode': Provides the capability to define abbreviations, which
123 ;; are automatically expanded when you type them. See the Emacs manual.
125 (require 'find-file nil t)
126 (require 'align nil t)
127 (require 'which-func nil t)
128 (require 'compile nil t)
130 (defvar ispell-check-comments)
131 (defvar skeleton-further-elements)
133 (defun ada-mode-version ()
134 "Return Ada mode version."
135 (interactive)
136 (let ((version-string "4.00"))
137 (if (called-interactively-p 'interactive)
138 (message version-string)
139 version-string)))
141 (defvar ada-mode-hook nil
142 "*List of functions to call when Ada mode is invoked.
143 This hook is automatically executed after the `ada-mode' is
144 fully loaded.
145 This is a good place to add Ada environment specific bindings.")
147 (defgroup ada nil
148 "Major mode for editing and compiling Ada source in Emacs."
149 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
150 :group 'languages)
152 (defcustom ada-auto-case t
153 "*Non-nil means automatically change case of preceding word while typing.
154 Casing is done according to `ada-case-keyword', `ada-case-identifier'
155 and `ada-case-attribute'."
156 :type 'boolean :group 'ada)
158 (defcustom ada-broken-decl-indent 0
159 "*Number of columns to indent a broken declaration.
161 An example is :
162 declare
164 >>>>>B : Integer;"
165 :type 'integer :group 'ada)
167 (defcustom ada-broken-indent 2
168 "*Number of columns to indent the continuation of a broken line.
170 An example is :
171 My_Var : My_Type := (Field1 =>
172 >>>>>>>>>Value);"
173 :type 'integer :group 'ada)
175 (defcustom ada-continuation-indent ada-broken-indent
176 "*Number of columns to indent the continuation of broken lines in parenthesis.
178 An example is :
179 Func (Param1,
180 >>>>>Param2);"
181 :type 'integer :group 'ada)
183 (defcustom ada-case-attribute 'ada-capitalize-word
184 "*Function to call to adjust the case of Ada attributes.
185 It may be `downcase-word', `upcase-word', `ada-loose-case-word',
186 `ada-capitalize-word' or `ada-no-auto-case'."
187 :type '(choice (const downcase-word)
188 (const upcase-word)
189 (const ada-capitalize-word)
190 (const ada-loose-case-word)
191 (const ada-no-auto-case))
192 :group 'ada)
194 (defcustom ada-case-exception-file
195 (list (convert-standard-filename' "~/.emacs_case_exceptions"))
196 "*List of special casing exceptions dictionaries for identifiers.
197 The first file is the one where new exceptions will be saved by Emacs
198 when you call `ada-create-case-exception'.
200 These files should contain one word per line, that gives the casing
201 to be used for that word in Ada files. If the line starts with the
202 character *, then the exception will be used for substrings that either
203 start at the beginning of a word or after a _ character, and end either
204 at the end of the word or at a _ character. Each line can be terminated
205 by a comment."
206 :type '(repeat (file))
207 :group 'ada)
209 (defcustom ada-case-keyword 'downcase-word
210 "*Function to call to adjust the case of an Ada keywords.
211 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
212 `ada-capitalize-word'."
213 :type '(choice (const downcase-word)
214 (const upcase-word)
215 (const ada-capitalize-word)
216 (const ada-loose-case-word)
217 (const ada-no-auto-case))
218 :group 'ada)
220 (defcustom ada-case-identifier 'ada-loose-case-word
221 "*Function to call to adjust the case of an Ada identifier.
222 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
223 `ada-capitalize-word'."
224 :type '(choice (const downcase-word)
225 (const upcase-word)
226 (const ada-capitalize-word)
227 (const ada-loose-case-word)
228 (const ada-no-auto-case))
229 :group 'ada)
231 (defcustom ada-clean-buffer-before-saving t
232 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
233 :type 'boolean :group 'ada)
234 (make-obsolete-variable 'ada-clean-buffer-before-saving
235 "use the `write-file-functions' hook."
236 "23.2")
239 (defcustom ada-indent 3
240 "*Size of Ada indentation.
242 An example is :
243 procedure Foo is
244 begin
245 >>>>>>>>>>null;"
246 :type 'integer :group 'ada)
248 (defcustom ada-indent-after-return t
249 "*Non-nil means automatically indent after RET or LFD."
250 :type 'boolean :group 'ada)
252 (defcustom ada-indent-align-comments t
253 "*Non-nil means align comments on previous line comments, if any.
254 If nil, indentation is calculated as usual.
255 Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
257 For instance:
258 A := 1; -- A multi-line comment
259 -- aligned if `ada-indent-align-comments' is t"
260 :type 'boolean :group 'ada)
262 (defcustom ada-indent-comment-as-code t
263 "*Non-nil means indent comment lines as code.
264 A nil value means do not auto-indent comments."
265 :type 'boolean :group 'ada)
267 (defcustom ada-indent-handle-comment-special nil
268 "*Non-nil if comment lines should be handled specially inside parenthesis.
269 By default, if the line that contains the open parenthesis has some
270 text following it, then the following lines will be indented in the
271 same column as this text. This will not be true if the first line is
272 a comment and `ada-indent-handle-comment-special' is t.
274 type A is
275 ( Value_1, -- common behavior, when not a comment
276 Value_2);
278 type A is
279 ( -- `ada-indent-handle-comment-special' is nil
280 Value_1,
281 Value_2);
283 type A is
284 ( -- `ada-indent-handle-comment-special' is non-nil
285 Value_1,
286 Value_2);"
287 :type 'boolean :group 'ada)
289 (defcustom ada-indent-is-separate t
290 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
291 :type 'boolean :group 'ada)
293 (defcustom ada-indent-record-rel-type 3
294 "*Indentation for 'record' relative to 'type' or 'use'.
296 An example is:
297 type A is
298 >>>>>>>>>>>record"
299 :type 'integer :group 'ada)
301 (defcustom ada-indent-renames ada-broken-indent
302 "*Indentation for renames relative to the matching function statement.
303 If `ada-indent-return' is null or negative, the indentation is done relative to
304 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
306 An example is:
307 function A (B : Integer)
308 return C;
309 >>>renames Foo;"
310 :type 'integer :group 'ada)
312 (defcustom ada-indent-return 0
313 "*Indentation for 'return' relative to the matching 'function' statement.
314 If `ada-indent-return' is null or negative, the indentation is done relative to
315 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
317 An example is:
318 function A (B : Integer)
319 >>>>>return C;"
320 :type 'integer :group 'ada)
322 (defcustom ada-indent-to-open-paren t
323 "*Non-nil means indent according to the innermost open parenthesis."
324 :type 'boolean :group 'ada)
326 (defcustom ada-fill-comment-prefix "-- "
327 "*Text inserted in the first columns when filling a comment paragraph.
328 Note: if you modify this variable, you will have to invoke `ada-mode'
329 again to take account of the new value."
330 :type 'string :group 'ada)
332 (defcustom ada-fill-comment-postfix " --"
333 "*Text inserted at the end of each line when filling a comment paragraph.
334 Used by `ada-fill-comment-paragraph-postfix'."
335 :type 'string :group 'ada)
337 (defcustom ada-label-indent -4
338 "*Number of columns to indent a label.
340 An example is:
341 procedure Foo is
342 begin
343 >>>>Label:
345 This is also used for <<..>> labels"
346 :type 'integer :group 'ada)
348 (defcustom ada-language-version 'ada95
349 "*Ada language version; one of `ada83', `ada95', `ada2005'."
350 :type '(choice (const ada83) (const ada95) (const ada2005)) :group 'ada)
352 (defcustom ada-move-to-declaration nil
353 "*Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to 'begin'."
354 :type 'boolean :group 'ada)
356 (defcustom ada-popup-key '[down-mouse-3]
357 "*Key used for binding the contextual menu.
358 If nil, no contextual menu is available."
359 :type '(restricted-sexp :match-alternatives (stringp vectorp))
360 :group 'ada)
362 (defcustom ada-search-directories
363 (append '(".")
364 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
365 '("/usr/adainclude" "/usr/local/adainclude"
366 "/opt/gnu/adainclude"))
367 "*Default list of directories to search for Ada files.
368 See the description for the `ff-search-directories' variable. This variable
369 is the initial value of `ada-search-directories-internal'."
370 :type '(repeat (choice :tag "Directory"
371 (const :tag "default" nil)
372 (directory :format "%v")))
373 :group 'ada)
375 (defvar ada-search-directories-internal ada-search-directories
376 "Internal version of `ada-search-directories'.
377 Its value is the concatenation of the search path as read in the project file
378 and the standard runtime location, and the value of the user-defined
379 `ada-search-directories'.")
381 (defcustom ada-stmt-end-indent 0
382 "*Number of columns to indent the end of a statement on a separate line.
384 An example is:
385 if A = B
386 >>>>then"
387 :type 'integer :group 'ada)
389 (defcustom ada-tab-policy 'indent-auto
390 "*Control the behavior of the TAB key.
391 Must be one of :
392 `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
393 `indent-auto' : use indentation functions in this file.
394 `always-tab' : do `indent-relative'."
395 :type '(choice (const indent-auto)
396 (const indent-rigidly)
397 (const always-tab))
398 :group 'ada)
400 (defcustom ada-use-indent ada-broken-indent
401 "*Indentation for the lines in a 'use' statement.
403 An example is:
404 use Ada.Text_IO,
405 >>>>Ada.Numerics;"
406 :type 'integer :group 'ada)
408 (defcustom ada-when-indent 3
409 "*Indentation for 'when' relative to 'exception' or 'case'.
411 An example is:
412 case A is
413 >>>>when B =>"
414 :type 'integer :group 'ada)
416 (defcustom ada-with-indent ada-broken-indent
417 "*Indentation for the lines in a 'with' statement.
419 An example is:
420 with Ada.Text_IO,
421 >>>>Ada.Numerics;"
422 :type 'integer :group 'ada)
424 (defcustom ada-which-compiler 'gnat
425 "*Name of the compiler to use.
426 This will determine what features are made available through the Ada mode.
427 The possible choices are:
428 `gnat': Use Ada Core Technologies' GNAT compiler. Add some cross-referencing
429 features.
430 `generic': Use a generic compiler."
431 :type '(choice (const gnat)
432 (const generic))
433 :group 'ada)
436 ;;; ---- end of user configurable variables
439 (defvar ada-body-suffixes '(".adb")
440 "List of possible suffixes for Ada body files.
441 The extensions should include a `.' if needed.")
443 (defvar ada-spec-suffixes '(".ads")
444 "List of possible suffixes for Ada spec files.
445 The extensions should include a `.' if needed.")
447 (defvar ada-mode-menu (make-sparse-keymap "Ada")
448 "Menu for Ada mode.")
450 (defvar ada-mode-map (make-sparse-keymap)
451 "Local keymap used for Ada mode.")
453 (defvar ada-mode-extra-map (make-sparse-keymap)
454 "Keymap used for non-standard keybindings.")
456 ;; default is C-c C-q because it's free in ada-mode-map
457 (defvar ada-mode-extra-prefix "\C-c\C-q"
458 "Prefix key to access `ada-mode-extra-map' functions.")
460 (defvar ada-mode-abbrev-table nil
461 "Local abbrev table for Ada mode.")
462 (define-abbrev-table 'ada-mode-abbrev-table ())
464 (defvar ada-mode-syntax-table nil
465 "Syntax table to be used for editing Ada source code.")
467 (defvar ada-mode-symbol-syntax-table nil
468 "Syntax table for Ada, where `_' is a word constituent.")
470 (eval-when-compile
471 ;; These values are used in eval-when-compile expressions.
472 (defconst ada-83-string-keywords
473 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
474 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
475 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
476 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
477 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
478 "procedure" "raise" "range" "record" "rem" "renames" "return"
479 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
480 "type" "use" "when" "while" "with" "xor")
481 "List of Ada 83 keywords.
482 Used to define `ada-*-keywords'.")
484 (defconst ada-95-string-keywords
485 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
486 "List of keywords new in Ada 95.
487 Used to define `ada-*-keywords'.")
489 (defconst ada-2005-string-keywords
490 '("interface" "overriding" "synchronized")
491 "List of keywords new in Ada 2005.
492 Used to define `ada-*-keywords.'"))
494 (defvar ada-ret-binding nil
495 "Variable to save key binding of RET when casing is activated.")
497 (defvar ada-case-exception '()
498 "Alist of words (entities) that have special casing.")
500 (defvar ada-case-exception-substring '()
501 "Alist of substrings (entities) that have special casing.
502 The substrings are detected for word constituant when the word
503 is not itself in `ada-case-exception', and only for substrings that
504 either are at the beginning or end of the word, or start after '_'.")
506 (defvar ada-lfd-binding nil
507 "Variable to save key binding of LFD when casing is activated.")
509 (defvar ada-other-file-alist nil
510 "Variable used by `find-file' to find the name of the other package.
511 See `ff-other-file-alist'.")
513 (defvar ada-align-list
514 '(("[^:]\\(\\s-*\\):[^:]" 1 t)
515 ("[^=]\\(\\s-+\\)=[^=]" 1 t)
516 ("\\(\\s-*\\)use\\s-" 1)
517 ("\\(\\s-*\\)--" 1))
518 "Ada support for align.el <= 2.2.
519 This variable provides regular expressions on which to align different lines.
520 See `align-mode-alist' for more information.")
522 (defvar ada-align-modes
523 '((ada-declaration
524 (regexp . "[^:]\\(\\s-*\\):[^:]")
525 (valid . (lambda() (not (ada-in-comment-p))))
526 (modes . '(ada-mode)))
527 (ada-assignment
528 (regexp . "[^=]\\(\\s-+\\)=[^=]")
529 (valid . (lambda() (not (ada-in-comment-p))))
530 (modes . '(ada-mode)))
531 (ada-comment
532 (regexp . "\\(\\s-*\\)--")
533 (modes . '(ada-mode)))
534 (ada-use
535 (regexp . "\\(\\s-*\\)use\\s-")
536 (valid . (lambda() (not (ada-in-comment-p))))
537 (modes . '(ada-mode)))
539 "Ada support for align.el >= 2.8.
540 This variable defines several rules to use to align different lines.")
542 (defconst ada-align-region-separate
543 (eval-when-compile
544 (concat
545 "^\\s-*\\($\\|\\("
546 "begin\\|"
547 "declare\\|"
548 "else\\|"
549 "end\\|"
550 "exception\\|"
551 "for\\|"
552 "function\\|"
553 "generic\\|"
554 "if\\|"
555 "is\\|"
556 "procedure\\|"
557 "record\\|"
558 "return\\|"
559 "type\\|"
560 "when"
561 "\\)\\>\\)"))
562 "See the variable `align-region-separate' for more information.")
564 ;;; ---- Below are the regexp used in this package for parsing
566 (defconst ada-83-keywords
567 (eval-when-compile
568 (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
569 "Regular expression matching Ada83 keywords.")
571 (defconst ada-95-keywords
572 (eval-when-compile
573 (concat "\\<" (regexp-opt
574 (append
575 ada-95-string-keywords
576 ada-83-string-keywords) t) "\\>"))
577 "Regular expression matching Ada95 keywords.")
579 (defconst ada-2005-keywords
580 (eval-when-compile
581 (concat "\\<" (regexp-opt
582 (append
583 ada-2005-string-keywords
584 ada-83-string-keywords
585 ada-95-string-keywords) t) "\\>"))
586 "Regular expression matching Ada2005 keywords.")
588 (defvar ada-keywords ada-2005-keywords
589 "Regular expression matching Ada keywords.")
590 ;; FIXME: make this customizable
592 (defconst ada-ident-re
593 "[[:alpha:]]\\(?:[_[:alnum:]]\\)*"
594 ;; [:alnum:] matches any multibyte word constituent, as well as
595 ;; Latin-1 letters and numbers. This allows __ and trailing _;
596 ;; someone (emacs bug#1919) proposed [^\W_] to fix that, but \W does
597 ;; _not_ mean "not word constituent" inside a character alternative.
598 "Regexp matching an Ada identifier.")
600 (defconst ada-goto-label-re
601 (concat "<<" ada-ident-re ">>")
602 "Regexp matching a goto label.")
604 (defconst ada-block-label-re
605 (concat ada-ident-re "[ \t\n]*:[^=]")
606 "Regexp matching a block label.
607 Note that this also matches a variable declaration.")
609 (defconst ada-label-re
610 (concat "\\(?:" ada-block-label-re "\\)\\|\\(?:" ada-goto-label-re "\\)")
611 "Regexp matching a goto or block label.")
613 ;; "with" needs to be included in the regexp, to match generic subprogram parameters
614 ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
615 (defvar ada-procedure-start-regexp
616 (concat
617 "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
619 ;; subprogram name: operator ("[+/=*]")
620 "\\("
621 "\\(\"[^\"]+\"\\)"
623 ;; subprogram name: name
624 "\\|"
625 "\\(\\(\\sw\\|[_.]\\)+\\)"
626 "\\)")
627 "Regexp matching Ada subprogram start.
628 The actual start is at (match-beginning 4). The name is in (match-string 5).")
630 (defconst ada-name-regexp
631 "\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
632 "Regexp matching a fully qualified name (including attribute).")
634 (defconst ada-package-start-regexp
635 (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp)
636 "Regexp matching start of package.
637 The package name is in (match-string 4).")
639 (defconst ada-compile-goto-error-file-linenr-re
640 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
641 "Regexp matching filename:linenr[:column].")
644 ;;; ---- regexps for indentation functions
646 (defvar ada-block-start-re
647 (eval-when-compile
648 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
649 "exception" "generic" "loop" "or"
650 "private" "select" ))
651 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
652 "Regexp for keywords starting Ada blocks.")
654 (defvar ada-end-stmt-re
655 (eval-when-compile
656 (concat "\\("
657 ";" "\\|"
658 "=>[ \t]*$" "\\|"
659 "=>[ \t]*--.*$" "\\|"
660 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
661 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
662 "loop" "private" "record" "select"
663 "then abort" "then") t) "\\>" "\\|"
664 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
665 t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
666 "^[ \t]*exception\\>"
667 "\\)") )
668 "Regexp of possible ends for a non-broken statement.
669 A new statement starts after these.")
671 (defvar ada-matching-start-re
672 (eval-when-compile
673 (concat "\\<"
674 (regexp-opt
675 '("end" "loop" "select" "begin" "case" "do" "declare"
676 "if" "task" "package" "procedure" "function" "record" "protected") t)
677 "\\>"))
678 "Regexp used in `ada-goto-matching-start'.")
680 (defvar ada-loop-start-re
681 "\\<\\(for\\|while\\|loop\\)\\>"
682 "Regexp for the start of a loop.")
684 (defvar ada-subprog-start-re
685 (eval-when-compile
686 (concat "\\<" (regexp-opt '("accept" "entry" "function" "overriding" "package" "procedure"
687 "protected" "task") t) "\\>"))
688 "Regexp for the start of a subprogram.")
690 (defvar ada-contextual-menu-on-identifier nil
691 "Set to true when the right mouse button was clicked on an identifier.")
693 (defvar ada-contextual-menu-last-point nil
694 "Position of point just before displaying the menu.
695 This is a list (point buffer).
696 Since `ada-popup-menu' moves the point where the user clicked, the region
697 is modified. Therefore no command from the menu knows what the user selected
698 before displaying the contextual menu.
699 To get the original region, restore the point to this position before
700 calling `region-end' and `region-beginning'.
701 Modify this variable if you want to restore the point to another position.")
703 (easy-menu-define ada-contextual-menu nil
704 "Menu to use when the user presses the right mouse button.
705 The variable `ada-contextual-menu-on-identifier' will be set to t before
706 displaying the menu if point was on an identifier."
707 '("Ada"
708 ["Goto Declaration/Body" ada-point-and-xref
709 :included ada-contextual-menu-on-identifier]
710 ["Goto Body" ada-point-and-xref-body
711 :included ada-contextual-menu-on-identifier]
712 ["Goto Previous Reference" ada-xref-goto-previous-reference]
713 ["List References" ada-find-references
714 :included ada-contextual-menu-on-identifier]
715 ["List Local References" ada-find-local-references
716 :included ada-contextual-menu-on-identifier]
717 ["-" nil nil]
718 ["Other File" ff-find-other-file]
719 ["Goto Parent Unit" ada-goto-parent]))
722 ;;------------------------------------------------------------------
723 ;; Support for imenu (see imenu.el)
724 ;;------------------------------------------------------------------
726 (defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
728 (defconst ada-imenu-subprogram-menu-re
729 (concat "^[ \t]*\\(overriding[ \t]*\\)?\\(procedure\\|function\\)[ \t\n]+"
730 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
731 ada-imenu-comment-re
732 "\\)[ \t\n]*"
733 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
735 (defvar ada-imenu-generic-expression
736 (list
737 (list nil ada-imenu-subprogram-menu-re 3)
738 (list "*Specs*"
739 (concat
740 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
741 "\\("
742 "\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
743 ada-imenu-comment-re "\\)";; parameter list or simple space
744 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
745 "\\)?;") 2)
746 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
747 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
748 '("*Protected*"
749 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
750 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
751 "Imenu generic expression for Ada mode.
752 See `imenu-generic-expression'. This variable will create several submenus for
753 each type of entity that can be found in an Ada file.")
756 ;;------------------------------------------------------------
757 ;; Support for compile.el
758 ;;------------------------------------------------------------
760 (defun ada-compile-mouse-goto-error ()
761 "Mouse interface for `ada-compile-goto-error'."
762 (interactive)
763 (mouse-set-point last-input-event)
764 (ada-compile-goto-error (point))
767 (defun ada-compile-goto-error (pos)
768 "Replace `compile-goto-error' from compile.el.
769 If POS is on a file and line location, go to this position. It adds
770 to compile.el the capacity to go to a reference in an error message.
771 For instance, on these lines:
772 foo.adb:61:11: [...] in call to size declared at foo.ads:11
773 foo.adb:61:11: [...] in call to local declared at line 20
774 the 4 file locations can be clicked on and jumped to."
775 (interactive "d")
776 (goto-char pos)
778 (skip-chars-backward "-a-zA-Z0-9_:./\\")
779 (cond
780 ;; special case: looking at a filename:line not at the beginning of a line
781 ;; or a simple line reference "at line ..."
782 ((and (not (bolp))
783 (or (looking-at ada-compile-goto-error-file-linenr-re)
784 (and
785 (save-excursion
786 (beginning-of-line)
787 (looking-at ada-compile-goto-error-file-linenr-re))
788 (save-excursion
789 (if (looking-at "\\([0-9]+\\)") (backward-word 1))
790 (looking-at "line \\([0-9]+\\)"))))
792 (let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
793 (file (if (match-beginning 2) (match-string 1)
794 (save-excursion (beginning-of-line)
795 (looking-at ada-compile-goto-error-file-linenr-re)
796 (match-string 1))))
797 (error-pos (point-marker))
798 source)
800 ;; set source marker
801 (save-excursion
802 (compilation-find-file (point-marker) (match-string 1) "./")
803 (set-buffer file)
805 (when (stringp line)
806 (goto-char (point-min))
807 (forward-line (1- (string-to-number line))))
809 (setq source (point-marker)))
811 (compilation-goto-locus error-pos source nil)
815 ;; otherwise, default behavior
817 (compile-goto-error))
819 (recenter))
822 ;;-------------------------------------------------------------------------
823 ;; Grammar related function
824 ;; The functions below work with the syntax class of the characters in an Ada
825 ;; buffer. Two syntax tables are created, depending on whether we want '_'
826 ;; to be considered as part of a word or not.
827 ;; Some characters may have multiple meanings depending on the context:
828 ;; - ' is either the beginning of a constant character or an attribute
829 ;; - # is either part of a based literal or a gnatprep statement.
830 ;; - " starts a string, but not if inside a constant character.
831 ;; - ( and ) should be ignored if inside a constant character.
832 ;; Thus their syntax property is changed automatically, and we can still use
833 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
835 ;; On Emacs, this is done through the `syntax-table' text property. The
836 ;; corresponding action is applied automatically each time the buffer
837 ;; changes via syntax-propertize-function.
839 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
840 ;; slow advice to `parse-partial-sexp' to do the same thing.
841 ;; When executing parse-partial-sexp, we simply modify the strings before and
842 ;; after, so that the special constants '"', '(' and ')' do not interact
843 ;; with parse-partial-sexp.
844 ;; Note: this code is slow and needs to be rewritten as soon as something
845 ;; better is available on XEmacs.
846 ;;-------------------------------------------------------------------------
848 (defun ada-create-syntax-table ()
849 "Create the two syntax tables use in the Ada mode.
850 The standard table declares `_' as a symbol constituent, the second one
851 declares it as a word constituent."
852 (interactive)
853 (setq ada-mode-syntax-table (make-syntax-table))
855 ;; define string brackets (`%' is alternative string bracket, but
856 ;; almost never used as such and throws font-lock and indentation
857 ;; off the track.)
858 (modify-syntax-entry ?% "$" ada-mode-syntax-table)
859 (modify-syntax-entry ?\" "\"" ada-mode-syntax-table)
861 (modify-syntax-entry ?: "." ada-mode-syntax-table)
862 (modify-syntax-entry ?\; "." ada-mode-syntax-table)
863 (modify-syntax-entry ?& "." ada-mode-syntax-table)
864 (modify-syntax-entry ?\| "." ada-mode-syntax-table)
865 (modify-syntax-entry ?+ "." ada-mode-syntax-table)
866 (modify-syntax-entry ?* "." ada-mode-syntax-table)
867 (modify-syntax-entry ?/ "." ada-mode-syntax-table)
868 (modify-syntax-entry ?= "." ada-mode-syntax-table)
869 (modify-syntax-entry ?< "." ada-mode-syntax-table)
870 (modify-syntax-entry ?> "." ada-mode-syntax-table)
871 (modify-syntax-entry ?$ "." ada-mode-syntax-table)
872 (modify-syntax-entry ?\[ "." ada-mode-syntax-table)
873 (modify-syntax-entry ?\] "." ada-mode-syntax-table)
874 (modify-syntax-entry ?\{ "." ada-mode-syntax-table)
875 (modify-syntax-entry ?\} "." ada-mode-syntax-table)
876 (modify-syntax-entry ?. "." ada-mode-syntax-table)
877 (modify-syntax-entry ?\\ "." ada-mode-syntax-table)
878 (modify-syntax-entry ?\' "." ada-mode-syntax-table)
880 ;; a single hyphen is punctuation, but a double hyphen starts a comment
881 (modify-syntax-entry ?- ". 12" ada-mode-syntax-table)
883 ;; See the comment above on grammar related function for the special
884 ;; setup for '#'.
885 (if (featurep 'xemacs)
886 (modify-syntax-entry ?# "<" ada-mode-syntax-table)
887 (modify-syntax-entry ?# "$" ada-mode-syntax-table))
889 ;; and \f and \n end a comment
890 (modify-syntax-entry ?\f "> " ada-mode-syntax-table)
891 (modify-syntax-entry ?\n "> " ada-mode-syntax-table)
893 ;; define what belongs in Ada symbols
894 (modify-syntax-entry ?_ "_" ada-mode-syntax-table)
896 ;; define parentheses to match
897 (modify-syntax-entry ?\( "()" ada-mode-syntax-table)
898 (modify-syntax-entry ?\) ")(" ada-mode-syntax-table)
900 (setq ada-mode-symbol-syntax-table (copy-syntax-table ada-mode-syntax-table))
901 (modify-syntax-entry ?_ "w" ada-mode-symbol-syntax-table)
904 ;; Support of special characters in XEmacs (see the comments at the beginning
905 ;; of the section on Grammar related functions).
907 (if (featurep 'xemacs)
908 (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
909 "Handles special character constants and gnatprep statements."
910 (let (change)
911 (if (< to from)
912 (let ((tmp from))
913 (setq from to to tmp)))
914 (save-excursion
915 (goto-char from)
916 (while (re-search-forward "'\\([(\")#]\\)'" to t)
917 (setq change (cons (list (match-beginning 1)
919 (match-string 1))
920 change))
921 (replace-match "'A'"))
922 (goto-char from)
923 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
924 (setq change (cons (list (match-beginning 1)
925 (length (match-string 1))
926 (match-string 1))
927 change))
928 (replace-match (make-string (length (match-string 1)) ?@))))
929 ad-do-it
930 (save-excursion
931 (while change
932 (goto-char (caar change))
933 (delete-char (cadar change))
934 (insert (caddar change))
935 (setq change (cdr change)))))))
937 (unless (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
938 ;; Before `syntax-propertize', we had to use font-lock to apply syntax-table
939 ;; properties, and in some cases we even had to do it manually (in
940 ;; `ada-after-change-function'). `ada-handle-syntax-table-properties'
941 ;; decides which method to use.
943 (defun ada-set-syntax-table-properties ()
944 "Assign `syntax-table' properties in accessible part of buffer.
945 In particular, character constants are said to be strings, #...#
946 are treated as numbers instead of gnatprep comments."
947 (let ((modified (buffer-modified-p))
948 (buffer-undo-list t)
949 (inhibit-read-only t)
950 (inhibit-point-motion-hooks t)
951 (inhibit-modification-hooks t))
952 (remove-text-properties (point-min) (point-max) '(syntax-table nil))
953 (goto-char (point-min))
954 (while (re-search-forward
955 ;; The following regexp was adapted from
956 ;; `ada-font-lock-syntactic-keywords'.
957 "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
958 nil t)
959 (if (match-beginning 1)
960 (put-text-property
961 (match-beginning 1) (match-end 1) 'syntax-table '(11 . ?\n))
962 (put-text-property
963 (match-beginning 2) (match-end 2) 'syntax-table '(7 . ?'))
964 (put-text-property
965 (match-beginning 3) (match-end 3) 'syntax-table '(7 . ?'))))
966 (unless modified
967 (restore-buffer-modified-p nil))))
969 (defun ada-after-change-function (beg end _old-len)
970 "Called when the region between BEG and END was changed in the buffer.
971 OLD-LEN indicates what the length of the replaced text was."
972 (save-excursion
973 (save-restriction
974 (let ((from (progn (goto-char beg) (line-beginning-position)))
975 (to (progn (goto-char end) (line-end-position))))
976 (narrow-to-region from to)
977 (save-match-data
978 (ada-set-syntax-table-properties))))))
980 (defun ada-initialize-syntax-table-properties ()
981 "Assign `syntax-table' properties in current buffer."
982 (save-excursion
983 (save-restriction
984 (widen)
985 (save-match-data
986 (ada-set-syntax-table-properties))))
987 (add-hook 'after-change-functions 'ada-after-change-function nil t))
989 (defun ada-handle-syntax-table-properties ()
990 "Handle `syntax-table' properties."
991 (if font-lock-mode
992 ;; `font-lock-mode' will take care of `syntax-table' properties.
993 (remove-hook 'after-change-functions 'ada-after-change-function t)
994 ;; Take care of `syntax-table' properties manually.
995 (ada-initialize-syntax-table-properties)))
997 ) ;;(not (fboundp 'syntax-propertize))
999 ;;------------------------------------------------------------------
1000 ;; Testing the grammatical context
1001 ;;------------------------------------------------------------------
1003 (defsubst ada-in-comment-p (&optional parse-result)
1004 "Return t if inside a comment.
1005 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1006 (nth 4 (or parse-result
1007 (parse-partial-sexp
1008 (line-beginning-position) (point)))))
1010 (defsubst ada-in-string-p (&optional parse-result)
1011 "Return t if point is inside a string.
1012 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1013 (nth 3 (or parse-result
1014 (parse-partial-sexp
1015 (line-beginning-position) (point)))))
1017 (defsubst ada-in-string-or-comment-p (&optional parse-result)
1018 "Return t if inside a comment or string.
1019 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1020 (setq parse-result (or parse-result
1021 (parse-partial-sexp
1022 (line-beginning-position) (point))))
1023 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
1025 (defsubst ada-in-numeric-literal-p ()
1026 "Return t if point is after a prefix of a numeric literal."
1027 (looking-back "\\([0-9]+#[0-9a-fA-F_]+\\)"))
1029 ;;------------------------------------------------------------------
1030 ;; Contextual menus
1031 ;; The Ada mode comes with contextual menus, bound by default to the right
1032 ;; mouse button.
1033 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
1034 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
1035 ;; if the mouse button was pressed on an identifier.
1036 ;;------------------------------------------------------------------
1038 (defun ada-call-from-contextual-menu (function)
1039 "Execute FUNCTION when called from the contextual menu.
1040 It forces Emacs to change the cursor position."
1041 (interactive)
1042 (funcall function)
1043 (setq ada-contextual-menu-last-point
1044 (list (point) (current-buffer))))
1046 (defun ada-popup-menu (position)
1047 "Pops up a contextual menu, depending on where the user clicked.
1048 POSITION is the location the mouse was clicked on.
1049 Sets `ada-contextual-menu-last-point' to the current position before
1050 displaying the menu. When a function from the menu is called, the
1051 point is where the mouse button was clicked."
1052 (interactive "e")
1054 ;; declare this as a local variable, so that the function called
1055 ;; in the contextual menu does not hide the region in
1056 ;; transient-mark-mode.
1057 (let ((deactivate-mark nil))
1058 (setq ada-contextual-menu-last-point
1059 (list (point) (current-buffer)))
1060 (mouse-set-point last-input-event)
1062 (setq ada-contextual-menu-on-identifier
1063 (and (char-after)
1064 (or (= (char-syntax (char-after)) ?w)
1065 (= (char-after) ?_))
1066 (not (ada-in-string-or-comment-p))
1067 (save-excursion (skip-syntax-forward "w")
1068 (not (ada-after-keyword-p)))
1070 (if (fboundp 'popup-menu)
1071 (funcall (symbol-function 'popup-menu) ada-contextual-menu)
1072 (let (choice)
1073 (setq choice (x-popup-menu position ada-contextual-menu))
1074 (if choice
1075 (funcall (lookup-key ada-contextual-menu (vector (car choice)))))))
1077 (set-buffer (cadr ada-contextual-menu-last-point))
1078 (goto-char (car ada-contextual-menu-last-point))
1082 ;;------------------------------------------------------------------
1083 ;; Misc functions
1084 ;;------------------------------------------------------------------
1086 ;;;###autoload
1087 (defun ada-add-extensions (spec body)
1088 "Define SPEC and BODY as being valid extensions for Ada files.
1089 Going from body to spec with `ff-find-other-file' used these
1090 extensions.
1091 SPEC and BODY are two regular expressions that must match against
1092 the file name."
1093 (let* ((reg (concat (regexp-quote body) "$"))
1094 (tmp (assoc reg ada-other-file-alist)))
1095 (if tmp
1096 (setcdr tmp (list (cons spec (cadr tmp))))
1097 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
1099 (let* ((reg (concat (regexp-quote spec) "$"))
1100 (tmp (assoc reg ada-other-file-alist)))
1101 (if tmp
1102 (setcdr tmp (list (cons body (cadr tmp))))
1103 (add-to-list 'ada-other-file-alist (list reg (list body)))))
1105 (add-to-list 'auto-mode-alist
1106 (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
1107 (add-to-list 'auto-mode-alist
1108 (cons (concat (regexp-quote body) "\\'") 'ada-mode))
1110 (add-to-list 'ada-spec-suffixes spec)
1111 (add-to-list 'ada-body-suffixes body)
1113 ;; Support for speedbar (Specifies that we want to see these files in
1114 ;; speedbar)
1115 (if (fboundp 'speedbar-add-supported-extension)
1116 (progn
1117 (funcall (symbol-function 'speedbar-add-supported-extension)
1118 spec)
1119 (funcall (symbol-function 'speedbar-add-supported-extension)
1120 body))))
1122 (defvar ada-font-lock-syntactic-keywords) ; defined below
1124 ;;;###autoload
1125 (define-derived-mode ada-mode prog-mode "Ada"
1126 "Ada mode is the major mode for editing Ada code."
1128 ;; Set the paragraph delimiters so that one can select a whole block
1129 ;; simply with M-h
1130 (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
1131 (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
1133 ;; comment end must be set because it may hold a wrong value if
1134 ;; this buffer had been in another mode before. RE
1135 (set (make-local-variable 'comment-end) "")
1137 ;; used by autofill and indent-new-comment-line
1138 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
1140 ;; used by autofill to break a comment line and continue it on another line.
1141 ;; The reason we need this one is that the default behavior does not work
1142 ;; correctly with the definition of paragraph-start above when the comment
1143 ;; is right after a multi-line subprogram declaration (the comments are
1144 ;; aligned under the latest parameter, not under the declaration start).
1145 (set (make-local-variable 'comment-line-break-function)
1146 (lambda (&optional soft) (let ((fill-prefix nil))
1147 (indent-new-comment-line soft))))
1149 (set (make-local-variable 'indent-line-function)
1150 'ada-indent-current-function)
1152 (set (make-local-variable 'comment-column) 40)
1154 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1155 ;; the comment and the text. We do not want any, this is already
1156 ;; included in comment-start
1157 (unless (featurep 'xemacs)
1158 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1159 (set (make-local-variable 'comment-padding) 0)
1160 (set (make-local-variable 'parse-sexp-lookup-properties) t))
1162 (setq case-fold-search t)
1163 (if (boundp 'imenu-case-fold-search)
1164 (setq imenu-case-fold-search t))
1166 (set (make-local-variable 'fill-paragraph-function)
1167 'ada-fill-comment-paragraph)
1169 ;; Support for compile.el
1170 ;; We just substitute our own functions to go to the error.
1171 (add-hook 'compilation-mode-hook
1172 (lambda()
1173 ;; FIXME: This has global impact! -stef
1174 (define-key compilation-minor-mode-map [mouse-2]
1175 'ada-compile-mouse-goto-error)
1176 (define-key compilation-minor-mode-map "\C-c\C-c"
1177 'ada-compile-goto-error)
1178 (define-key compilation-minor-mode-map "\C-m"
1179 'ada-compile-goto-error)))
1181 ;; font-lock support :
1183 (set (make-local-variable 'font-lock-defaults)
1184 '(ada-font-lock-keywords
1185 nil t
1186 ((?\_ . "w") (?# . "."))
1187 beginning-of-line))
1189 (if (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
1190 (set (make-local-variable 'syntax-propertize-function)
1191 (syntax-propertize-via-font-lock ada-font-lock-syntactic-keywords))
1192 (set (make-local-variable 'font-lock-syntactic-keywords)
1193 ada-font-lock-syntactic-keywords))
1195 ;; Set up support for find-file.el.
1196 (set (make-local-variable 'ff-other-file-alist)
1197 'ada-other-file-alist)
1198 (set (make-local-variable 'ff-search-directories)
1199 'ada-search-directories-internal)
1200 (setq ff-post-load-hook 'ada-set-point-accordingly
1201 ff-file-created-hook 'ada-make-body)
1202 (add-hook 'ff-pre-load-hook 'ada-which-function-are-we-in)
1204 (make-local-variable 'ff-special-constructs)
1205 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair))
1206 (list
1207 ;; Top level child package declaration; go to the parent package.
1208 (cons (eval-when-compile
1209 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1210 "\\(body[ \t]+\\)?"
1211 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1212 (lambda ()
1213 (ff-get-file
1214 ada-search-directories-internal
1215 (ada-make-filename-from-adaname (match-string 3))
1216 ada-spec-suffixes)))
1218 ;; A "separate" clause.
1219 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1220 (lambda ()
1221 (ff-get-file
1222 ada-search-directories-internal
1223 (ada-make-filename-from-adaname (match-string 1))
1224 ada-spec-suffixes)))
1226 ;; A "with" clause.
1227 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1228 (lambda ()
1229 (ff-get-file
1230 ada-search-directories-internal
1231 (ada-make-filename-from-adaname (match-string 1))
1232 ada-spec-suffixes)))
1235 ;; Support for outline-minor-mode
1236 (set (make-local-variable 'outline-regexp)
1237 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1238 (set (make-local-variable 'outline-level) 'ada-outline-level)
1240 ;; Support for imenu : We want a sorted index
1241 (setq imenu-generic-expression ada-imenu-generic-expression)
1243 (setq imenu-sort-function 'imenu--sort-by-name)
1245 ;; Support for ispell : Check only comments
1246 (set (make-local-variable 'ispell-check-comments) 'exclusive)
1248 ;; Support for align
1249 (add-to-list 'align-dq-string-modes 'ada-mode)
1250 (add-to-list 'align-open-comment-modes 'ada-mode)
1251 (set (make-local-variable 'align-region-separate) ada-align-region-separate)
1253 ;; Exclude comments alone on line from alignment.
1254 (add-to-list 'align-exclude-rules-list
1255 '(ada-solo-comment
1256 (regexp . "^\\(\\s-*\\)--")
1257 (modes . '(ada-mode))))
1258 (add-to-list 'align-exclude-rules-list
1259 '(ada-solo-use
1260 (regexp . "^\\(\\s-*\\)\\<use\\>")
1261 (modes . '(ada-mode))))
1263 (setq ada-align-modes nil)
1265 (add-to-list 'ada-align-modes
1266 '(ada-declaration-assign
1267 (regexp . "[^:]\\(\\s-*\\):[^:]")
1268 (valid . (lambda() (not (ada-in-comment-p))))
1269 (repeat . t)
1270 (modes . '(ada-mode))))
1271 (add-to-list 'ada-align-modes
1272 '(ada-associate
1273 (regexp . "[^=]\\(\\s-*\\)=>")
1274 (valid . (lambda() (not (ada-in-comment-p))))
1275 (modes . '(ada-mode))))
1276 (add-to-list 'ada-align-modes
1277 '(ada-comment
1278 (regexp . "\\(\\s-*\\)--")
1279 (modes . '(ada-mode))))
1280 (add-to-list 'ada-align-modes
1281 '(ada-use
1282 (regexp . "\\(\\s-*\\)\\<use\\s-")
1283 (valid . (lambda() (not (ada-in-comment-p))))
1284 (modes . '(ada-mode))))
1285 (add-to-list 'ada-align-modes
1286 '(ada-at
1287 (regexp . "\\(\\s-+\\)at\\>")
1288 (modes . '(ada-mode))))
1290 (setq align-mode-rules-list ada-align-modes)
1292 ;; Set up the contextual menu
1293 (if ada-popup-key
1294 (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
1296 ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
1297 (setq local-abbrev-table ada-mode-abbrev-table)
1299 ;; Support for which-function mode
1300 (set (make-local-variable 'which-func-functions) '(ada-which-function))
1302 ;; Support for indent-new-comment-line (Especially for XEmacs)
1303 (set (make-local-variable 'comment-multi-line) nil)
1305 ;; Support for add-log
1306 (set (make-local-variable 'add-log-current-defun-function)
1307 'ada-which-function)
1309 (easy-menu-add ada-mode-menu ada-mode-map)
1311 (set (make-local-variable 'skeleton-further-elements)
1312 '((< '(backward-delete-char-untabify
1313 (min ada-indent (current-column))))))
1314 (add-hook 'skeleton-end-hook 'ada-adjust-case-skeleton nil t)
1316 ;; To be run after the hook, in case the user modified
1317 ;; ada-fill-comment-prefix
1318 (add-hook 'hack-local-variables-hook
1319 (lambda ()
1320 (set (make-local-variable 'comment-start)
1321 (or ada-fill-comment-prefix "-- "))
1323 ;; Run this after the hook to give the users a chance
1324 ;; to activate font-lock-mode.
1326 (unless (or (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
1327 (featurep 'xemacs))
1328 (ada-initialize-syntax-table-properties)
1329 (add-hook 'font-lock-mode-hook
1330 'ada-handle-syntax-table-properties nil t))
1332 ;; FIXME: ada-language-version might be set in the mode
1333 ;; hook or it might even be set later on via file-local
1334 ;; vars, so ada-keywords should be set lazily.
1335 (cond ((eq ada-language-version 'ada83)
1336 (setq ada-keywords ada-83-keywords))
1337 ((eq ada-language-version 'ada95)
1338 (setq ada-keywords ada-95-keywords))
1339 ((eq ada-language-version 'ada2005)
1340 (setq ada-keywords ada-2005-keywords)))
1342 (if ada-auto-case
1343 (ada-activate-keys-for-case)))
1344 nil 'local))
1346 (defun ada-adjust-case-skeleton ()
1347 "Adjust the case of the text inserted by a skeleton."
1348 (save-excursion
1349 (let ((aa-end (point)))
1350 (ada-adjust-case-region
1351 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
1352 (goto-char aa-end)))))
1354 (defun ada-region-selected ()
1355 "Should we operate on an active region?"
1356 (if (fboundp 'use-region-p)
1357 (use-region-p)
1358 (region-active-p)))
1360 ;;-----------------------------------------------------------------
1361 ;; auto-casing
1362 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1363 ;; functions to auto-case identifiers, keywords, ...
1364 ;; The basic rules for autocasing are defined through the variables
1365 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1366 ;; are references to the functions that will do the actual casing.
1368 ;; However, in most cases, the user will want to define some exceptions to
1369 ;; these casing rules. This is done through a list of files, that contain
1370 ;; one word per line. These files are stored in `ada-case-exception-file'.
1371 ;; For backward compatibility, this variable can also be a string.
1372 ;;-----------------------------------------------------------------
1374 (defun ada-save-exceptions-to-file (file-name)
1375 "Save the casing exception lists to the file FILE-NAME.
1376 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1377 (find-file (expand-file-name file-name))
1378 (erase-buffer)
1379 (mapc (lambda (x) (insert (car x) "\n"))
1380 (sort (copy-sequence ada-case-exception)
1381 (lambda(a b) (string< (car a) (car b)))))
1382 (mapc (lambda (x) (insert "*" (car x) "\n"))
1383 (sort (copy-sequence ada-case-exception-substring)
1384 (lambda(a b) (string< (car a) (car b)))))
1385 (save-buffer)
1386 (kill-buffer nil)
1389 (defun ada-create-case-exception (&optional word)
1390 "Define WORD as an exception for the casing system.
1391 If WORD is not given, then the current word in the buffer is used instead.
1392 The new word is added to the first file in `ada-case-exception-file'.
1393 The standard casing rules will no longer apply to this word."
1394 (interactive)
1395 (let ((file-name
1396 (cond ((stringp ada-case-exception-file)
1397 ada-case-exception-file)
1398 ((listp ada-case-exception-file)
1399 (car ada-case-exception-file))
1401 (error (concat "No exception file specified. "
1402 "See variable ada-case-exception-file"))))))
1404 (unless word
1405 (with-syntax-table ada-mode-symbol-syntax-table
1406 (save-excursion
1407 (skip-syntax-backward "w")
1408 (setq word (buffer-substring-no-properties
1409 (point) (save-excursion (forward-word 1) (point)))))))
1411 ;; Reread the exceptions file, in case it was modified by some other,
1412 (ada-case-read-exceptions-from-file file-name)
1414 ;; If the word is already in the list, even with a different casing
1415 ;; we simply want to replace it.
1416 (if (and (not (equal ada-case-exception '()))
1417 (assoc-string word ada-case-exception t))
1418 (setcar (assoc-string word ada-case-exception t) word)
1419 (add-to-list 'ada-case-exception (cons word t)))
1421 (ada-save-exceptions-to-file file-name)))
1423 (defun ada-create-case-exception-substring (&optional word)
1424 "Define the substring WORD as an exception for the casing system.
1425 If WORD is not given, then the current word in the buffer is used instead,
1426 or the selected region if any is active.
1427 The new word is added to the first file in `ada-case-exception-file'.
1428 When auto-casing a word, this substring will be special-cased, unless the
1429 word itself has a special casing."
1430 (interactive)
1431 (let ((file-name
1432 (cond ((stringp ada-case-exception-file)
1433 ada-case-exception-file)
1434 ((listp ada-case-exception-file)
1435 (car ada-case-exception-file))
1437 (error (concat "No exception file specified. "
1438 "See variable ada-case-exception-file"))))))
1440 ;; Find the substring to define as an exception. Order is: the parameter,
1441 ;; if any, or the selected region, or the word under the cursor
1442 (cond
1443 (word nil)
1445 ((ada-region-selected)
1446 (setq word (buffer-substring-no-properties
1447 (region-beginning) (region-end))))
1450 (let ((underscore-syntax (char-syntax ?_)))
1451 (unwind-protect
1452 (progn
1453 (modify-syntax-entry ?_ "." (syntax-table))
1454 (save-excursion
1455 (skip-syntax-backward "w")
1456 (setq word (buffer-substring-no-properties
1457 (point)
1458 (save-excursion (forward-word 1) (point))))))
1459 (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
1460 (syntax-table))))))
1462 ;; Reread the exceptions file, in case it was modified by some other,
1463 (ada-case-read-exceptions-from-file file-name)
1465 ;; If the word is already in the list, even with a different casing
1466 ;; we simply want to replace it.
1467 (if (and (not (equal ada-case-exception-substring '()))
1468 (assoc-string word ada-case-exception-substring t))
1469 (setcar (assoc-string word ada-case-exception-substring t) word)
1470 (add-to-list 'ada-case-exception-substring (cons word t))
1473 (ada-save-exceptions-to-file file-name)
1475 (message "%s" (concat "Defining " word " as a casing exception"))))
1477 (defun ada-case-read-exceptions-from-file (file-name)
1478 "Read the content of the casing exception file FILE-NAME."
1479 (if (file-readable-p (expand-file-name file-name))
1480 (let ((buffer (current-buffer)))
1481 (find-file (expand-file-name file-name))
1482 (set-syntax-table ada-mode-symbol-syntax-table)
1483 (widen)
1484 (goto-char (point-min))
1485 (while (not (eobp))
1487 ;; If the item is already in the list, even with an other casing,
1488 ;; do not add it again. This way, the user can easily decide which
1489 ;; priority should be applied to each casing exception
1490 (let ((word (buffer-substring-no-properties
1491 (point) (save-excursion (forward-word 1) (point)))))
1493 ;; Handling a substring ?
1494 (if (char-equal (string-to-char word) ?*)
1495 (progn
1496 (setq word (substring word 1))
1497 (unless (assoc-string word ada-case-exception-substring t)
1498 (add-to-list 'ada-case-exception-substring (cons word t))))
1499 (unless (assoc-string word ada-case-exception t)
1500 (add-to-list 'ada-case-exception (cons word t)))))
1502 (forward-line 1))
1503 (kill-buffer nil)
1504 (set-buffer buffer)))
1507 (defun ada-case-read-exceptions ()
1508 "Read all the casing exception files from `ada-case-exception-file'."
1509 (interactive)
1511 ;; Reinitialize the casing exception list
1512 (setq ada-case-exception '()
1513 ada-case-exception-substring '())
1515 (cond ((stringp ada-case-exception-file)
1516 (ada-case-read-exceptions-from-file ada-case-exception-file))
1518 ((listp ada-case-exception-file)
1519 (mapcar 'ada-case-read-exceptions-from-file
1520 ada-case-exception-file))))
1522 (defun ada-adjust-case-substring ()
1523 "Adjust case of substrings in the previous word."
1524 (interactive)
1525 (let ((substrings ada-case-exception-substring)
1526 (max (point))
1527 (case-fold-search t)
1528 (underscore-syntax (char-syntax ?_))
1531 (save-excursion
1532 (forward-word -1)
1534 (unwind-protect
1535 (progn
1536 (modify-syntax-entry ?_ "." (syntax-table))
1538 (while substrings
1539 (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
1541 (save-excursion
1542 (while (re-search-forward re max t)
1543 (replace-match (caar substrings) t)))
1544 (setq substrings (cdr substrings))
1547 (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
1550 (defun ada-adjust-case-identifier ()
1551 "Adjust case of the previous identifier.
1552 The auto-casing is done according to the value of `ada-case-identifier'
1553 and the exceptions defined in `ada-case-exception-file'."
1554 (interactive)
1555 (if (or (equal ada-case-exception '())
1556 (equal (char-after) ?_))
1557 (progn
1558 (funcall ada-case-identifier -1)
1559 (ada-adjust-case-substring))
1561 (progn
1562 (let ((end (point))
1563 (start (save-excursion (skip-syntax-backward "w")
1564 (point)))
1565 match)
1566 ;; If we have an exception, replace the word by the correct casing
1567 (if (setq match (assoc-string (buffer-substring start end)
1568 ada-case-exception t))
1570 (progn
1571 (delete-region start end)
1572 (insert (car match)))
1574 ;; Else simply re-case the word
1575 (funcall ada-case-identifier -1)
1576 (ada-adjust-case-substring))))))
1578 (defun ada-after-keyword-p ()
1579 "Return t if cursor is after a keyword that is not an attribute."
1580 (save-excursion
1581 (forward-word -1)
1582 (and (not (and (char-before)
1583 (or (= (char-before) ?_)
1584 (= (char-before) ?'))));; unless we have a _ or '
1585 (looking-at (concat ada-keywords "[^_]")))))
1587 (defun ada-adjust-case (&optional force-identifier)
1588 "Adjust the case of the word before the character just typed.
1589 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1590 (if (not (bobp))
1591 (progn
1592 (forward-char -1)
1593 (if (and (not (bobp))
1594 ;; or if at the end of a character constant
1595 (not (and (eq (following-char) ?')
1596 (eq (char-before (1- (point))) ?')))
1597 ;; or if the previous character was not part of a word
1598 (eq (char-syntax (char-before)) ?w)
1599 ;; if in a string or a comment
1600 (not (ada-in-string-or-comment-p))
1601 ;; if in a numeric literal
1602 (not (ada-in-numeric-literal-p))
1604 (if (save-excursion
1605 (forward-word -1)
1606 (or (= (point) (point-min))
1607 (backward-char 1))
1608 (= (following-char) ?'))
1609 (funcall ada-case-attribute -1)
1610 (if (and
1611 (not force-identifier) ; (MH)
1612 (ada-after-keyword-p))
1613 (funcall ada-case-keyword -1)
1614 (ada-adjust-case-identifier))))
1615 (forward-char 1)
1619 (defun ada-adjust-case-interactive (arg)
1620 "Adjust the case of the previous word, and process the character just typed.
1621 ARG is the prefix the user entered with \\[universal-argument]."
1622 (interactive "P")
1624 (if ada-auto-case
1625 (let ((lastk last-command-event))
1627 (with-syntax-table ada-mode-symbol-syntax-table
1628 (cond ((or (eq lastk ?\n)
1629 (eq lastk ?\r))
1630 ;; horrible kludge
1631 (insert " ")
1632 (ada-adjust-case)
1633 ;; horrible dekludge
1634 (delete-char -1)
1635 ;; some special keys and their bindings
1636 (cond
1637 ((eq lastk ?\n)
1638 (funcall ada-lfd-binding))
1639 ((eq lastk ?\r)
1640 (funcall ada-ret-binding))))
1641 ((eq lastk ?\C-i) (ada-tab))
1642 ;; Else just insert the character
1643 ((self-insert-command (prefix-numeric-value arg))))
1644 ;; if there is a keyword in front of the underscore
1645 ;; then it should be part of an identifier (MH)
1646 (if (eq lastk ?_)
1647 (ada-adjust-case t)
1648 (ada-adjust-case))))
1650 ;; Else, no auto-casing
1651 (cond
1652 ((eq last-command-event ?\n)
1653 (funcall ada-lfd-binding))
1654 ((eq last-command-event ?\r)
1655 (funcall ada-ret-binding))
1657 (self-insert-command (prefix-numeric-value arg))))))
1659 (defun ada-activate-keys-for-case ()
1660 ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
1661 "Modify the key bindings for all the keys that should readjust the casing."
1662 (interactive)
1663 ;; Save original key-bindings to allow swapping ret/lfd
1664 ;; when casing is activated.
1665 ;; The 'or ...' is there to be sure that the value will not
1666 ;; be changed again when Ada mode is called more than once
1667 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
1668 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
1670 ;; Call case modifying function after certain keys.
1671 (mapcar (function (lambda(key) (define-key
1672 ada-mode-map
1673 (char-to-string key)
1674 'ada-adjust-case-interactive)))
1675 '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
1676 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1678 (defun ada-loose-case-word (&optional _arg)
1679 "Upcase first letter and letters following `_' in the following word.
1680 No other letter is modified.
1681 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1682 (interactive)
1683 (save-excursion
1684 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1685 (first t))
1686 (skip-syntax-backward "w")
1687 (while (and (or first (search-forward "_" end t))
1688 (< (point) end))
1689 (and first
1690 (setq first nil))
1691 (insert-char (upcase (following-char)) 1)
1692 (delete-char 1)))))
1694 (defun ada-no-auto-case (&optional _arg)
1695 "Do nothing. ARG is ignored.
1696 This function can be used for the auto-casing variables in Ada mode, to
1697 adapt to unusal auto-casing schemes. Since it does nothing, you can for
1698 instance use it for `ada-case-identifier' if you don't want any special
1699 auto-casing for identifiers, whereas keywords have to be lower-cased.
1700 See also `ada-auto-case' to disable auto casing altogether."
1701 nil)
1703 (defun ada-capitalize-word (&optional _arg)
1704 "Upcase first letter and letters following '_', lower case other letters.
1705 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1706 (interactive)
1707 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1708 (begin (save-excursion (skip-syntax-backward "w") (point))))
1709 (modify-syntax-entry ?_ "_")
1710 (capitalize-region begin end)
1711 (modify-syntax-entry ?_ "w")))
1713 (defun ada-adjust-case-region (from to)
1714 "Adjust the case of all words in the region between FROM and TO.
1715 Attention: This function might take very long for big regions!"
1716 (interactive "*r")
1717 (let ((begin nil)
1718 (end nil)
1719 (keywordp nil)
1720 (attribp nil))
1721 (message "Adjusting case ...")
1722 (with-syntax-table ada-mode-symbol-syntax-table
1723 (save-excursion
1724 (goto-char to)
1726 ;; loop: look for all identifiers, keywords, and attributes
1728 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1729 (setq end (match-end 1))
1730 (setq attribp
1731 (and (> (point) from)
1732 (save-excursion
1733 (forward-char -1)
1734 (setq attribp (looking-at "'.[^']")))))
1736 ;; do nothing if it is a string or comment
1737 (ada-in-string-or-comment-p)
1738 (progn
1740 ;; get the identifier or keyword or attribute
1742 (setq begin (point))
1743 (setq keywordp (looking-at ada-keywords))
1744 (goto-char end)
1746 ;; casing according to user-option
1748 (if attribp
1749 (funcall ada-case-attribute -1)
1750 (if keywordp
1751 (funcall ada-case-keyword -1)
1752 (ada-adjust-case-identifier)))
1753 (goto-char begin))))
1754 (message "Adjusting case ... Done")))))
1756 (defun ada-adjust-case-buffer ()
1757 "Adjust the case of all words in the whole buffer.
1758 ATTENTION: This function might take very long for big buffers!"
1759 (interactive "*")
1760 (ada-adjust-case-region (point-min) (point-max)))
1763 ;;--------------------------------------------------------------
1764 ;; Format Parameter Lists
1765 ;; Some special algorithms are provided to indent the parameter lists in
1766 ;; subprogram declarations. This is done in two steps:
1767 ;; - First parses the parameter list. The returned list has the following
1768 ;; format:
1769 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1770 ;; ... )
1771 ;; This is done in `ada-scan-paramlist'.
1772 ;; - Delete and recreate the parameter list in function
1773 ;; `ada-insert-paramlist'.
1774 ;; Both steps are called from `ada-format-paramlist'.
1775 ;; Note: Comments inside the parameter list are lost.
1776 ;; The syntax has to be correct, or the reformating will fail.
1777 ;;--------------------------------------------------------------
1779 (defun ada-format-paramlist ()
1780 "Reformat the parameter list point is in."
1781 (interactive)
1782 (let ((begin nil)
1783 (end nil)
1784 (delend nil)
1785 (paramlist nil))
1786 (with-syntax-table ada-mode-symbol-syntax-table
1788 ;; check if really inside parameter list
1789 (or (ada-in-paramlist-p)
1790 (error "Not in parameter list"))
1792 ;; find start of current parameter-list
1793 (ada-search-ignore-string-comment
1794 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1795 (down-list 1)
1796 (backward-char 1)
1797 (setq begin (point))
1799 ;; find end of parameter-list
1800 (forward-sexp 1)
1801 (setq delend (point))
1802 (delete-char -1)
1803 (insert "\n")
1805 ;; find end of last parameter-declaration
1806 (forward-comment -1000)
1807 (setq end (point))
1809 ;; build a list of all elements of the parameter-list
1810 (setq paramlist (ada-scan-paramlist (1+ begin) end))
1812 ;; delete the original parameter-list
1813 (delete-region begin delend)
1815 ;; insert the new parameter-list
1816 (goto-char begin)
1817 (ada-insert-paramlist paramlist))))
1819 (defun ada-scan-paramlist (begin end)
1820 "Scan the parameter list found in between BEGIN and END.
1821 Return the equivalent internal parameter list."
1822 (let ((paramlist (list))
1823 (param (list))
1824 (notend t)
1825 (apos nil)
1826 (epos nil)
1827 (semipos nil)
1828 (match-cons nil))
1830 (goto-char begin)
1832 ;; loop until end of last parameter
1833 (while notend
1835 ;; find first character of parameter-declaration
1836 (ada-goto-next-non-ws)
1837 (setq apos (point))
1839 ;; find last character of parameter-declaration
1840 (if (setq match-cons
1841 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1842 (progn
1843 (setq epos (car match-cons))
1844 (setq semipos (cdr match-cons)))
1845 (setq epos end))
1847 ;; read name(s) of parameter(s)
1848 (goto-char apos)
1849 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1851 (setq param (list (match-string 1)))
1852 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1854 ;; look for 'in'
1855 (setq apos (point))
1856 (setq param
1857 (append param
1858 (list
1859 (consp
1860 (ada-search-ignore-string-comment
1861 "in" nil epos t 'word-search-forward)))))
1863 ;; look for 'out'
1864 (goto-char apos)
1865 (setq param
1866 (append param
1867 (list
1868 (consp
1869 (ada-search-ignore-string-comment
1870 "out" nil epos t 'word-search-forward)))))
1872 ;; look for 'access'
1873 (goto-char apos)
1874 (setq param
1875 (append param
1876 (list
1877 (consp
1878 (ada-search-ignore-string-comment
1879 "access" nil epos t 'word-search-forward)))))
1881 ;; skip 'in'/'out'/'access'
1882 (goto-char apos)
1883 (ada-goto-next-non-ws)
1884 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1885 (forward-word 1)
1886 (ada-goto-next-non-ws))
1888 ;; read type of parameter
1889 ;; We accept spaces in the name, since some software like Rose
1890 ;; generates something like: "A : B 'Class"
1891 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1892 (setq param
1893 (append param
1894 (list (match-string 0))))
1896 ;; read default-expression, if there is one
1897 (goto-char (setq apos (match-end 0)))
1898 (setq param
1899 (append param
1900 (list
1901 (if (setq match-cons
1902 (ada-search-ignore-string-comment
1903 ":=" nil epos t 'search-forward))
1904 (buffer-substring (car match-cons) epos)
1905 nil))))
1907 ;; add this parameter-declaration to the list
1908 (setq paramlist (append paramlist (list param)))
1910 ;; check if it was the last parameter
1911 (if (eq epos end)
1912 (setq notend nil)
1913 (goto-char semipos))
1915 (reverse paramlist)))
1917 (defun ada-insert-paramlist (paramlist)
1918 "Insert a formatted PARAMLIST in the buffer."
1919 (let ((i (length paramlist))
1920 (parlen 0)
1921 (typlen 0)
1922 (inp nil)
1923 (outp nil)
1924 (accessp nil)
1925 (column nil)
1926 (firstcol nil))
1928 ;; loop until last parameter
1929 (while (not (zerop i))
1930 (setq i (1- i))
1932 ;; get max length of parameter-name
1933 (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
1935 ;; get max length of type-name
1936 (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
1938 ;; is there any 'in' ?
1939 (setq inp (or inp (nth 1 (nth i paramlist))))
1941 ;; is there any 'out' ?
1942 (setq outp (or outp (nth 2 (nth i paramlist))))
1944 ;; is there any 'access' ?
1945 (setq accessp (or accessp (nth 3 (nth i paramlist))))
1948 ;; does paramlist already start on a separate line ?
1949 (if (save-excursion
1950 (re-search-backward "^.\\|[^ \t]" nil t)
1951 (looking-at "^."))
1952 ;; yes => re-indent it
1953 (progn
1954 (ada-indent-current)
1955 (save-excursion
1956 (if (looking-at "\\(is\\|return\\)")
1957 (replace-match " \\1"))))
1959 ;; no => insert it where we are after removing any whitespace
1960 (fixup-whitespace)
1961 (save-excursion
1962 (cond
1963 ((looking-at "[ \t]*\\(\n\\|;\\)")
1964 (replace-match "\\1"))
1965 ((looking-at "[ \t]*\\(is\\|return\\)")
1966 (replace-match " \\1"))))
1967 (insert " "))
1969 (insert "(")
1970 (ada-indent-current)
1972 (setq firstcol (current-column))
1973 (setq i (length paramlist))
1975 ;; loop until last parameter
1976 (while (not (zerop i))
1977 (setq i (1- i))
1978 (setq column firstcol)
1980 ;; insert parameter-name, space and colon
1981 (insert (nth 0 (nth i paramlist)))
1982 (indent-to (+ column parlen 1))
1983 (insert ": ")
1984 (setq column (current-column))
1986 ;; insert 'in' or space
1987 (if (nth 1 (nth i paramlist))
1988 (insert "in ")
1989 (if (and
1990 (or inp
1991 accessp)
1992 (not (nth 3 (nth i paramlist))))
1993 (insert " ")))
1995 ;; insert 'out' or space
1996 (if (nth 2 (nth i paramlist))
1997 (insert "out ")
1998 (if (and
1999 (or outp
2000 accessp)
2001 (not (nth 3 (nth i paramlist))))
2002 (insert " ")))
2004 ;; insert 'access'
2005 (if (nth 3 (nth i paramlist))
2006 (insert "access "))
2008 (setq column (current-column))
2010 ;; insert type-name and, if necessary, space and default-expression
2011 (insert (nth 4 (nth i paramlist)))
2012 (if (nth 5 (nth i paramlist))
2013 (progn
2014 (indent-to (+ column typlen 1))
2015 (insert (nth 5 (nth i paramlist)))))
2017 ;; check if it was the last parameter
2018 (if (zerop i)
2019 (insert ")")
2020 ;; no => insert ';' and newline and indent
2021 (insert ";")
2022 (newline)
2023 (indent-to firstcol))
2026 ;; if anything follows, except semicolon, newline, is or return
2027 ;; put it in a new line and indent it
2028 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2029 (ada-indent-newline-indent))
2034 ;;;----------------------------------------------------------------
2035 ;; Indentation Engine
2036 ;; All indentations are indicated as a two-element string:
2037 ;; - position of reference in the buffer
2038 ;; - offset to indent from this position (can also be a symbol or a list
2039 ;; that are evaluated)
2040 ;; Thus the total indentation for a line is the column number of the reference
2041 ;; position plus whatever value the evaluation of the second element provides.
2042 ;; This mechanism is used so that the Ada mode can "explain" how the
2043 ;; indentation was calculated, by showing which variables were used.
2045 ;; The indentation itself is done in only one pass: first we try to guess in
2046 ;; what context we are by looking at the following keyword or punctuation
2047 ;; sign. If nothing remarkable is found, just try to guess the indentation
2048 ;; based on previous lines.
2050 ;; The relevant functions for indentation are:
2051 ;; - `ada-indent-region': Re-indent a region of text
2052 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2053 ;; calculation that were done
2054 ;; - `ada-indent-current': Re-indent the current line
2055 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2056 ;; based on the context (see above).
2057 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2058 ;; For efficiency, these functions do not check they are in the correct
2059 ;; context.
2060 ;;;----------------------------------------------------------------
2062 (defun ada-indent-region (beg end)
2063 "Indent the region between BEG end END."
2064 (interactive "*r")
2065 (goto-char beg)
2066 (let ((block-done 0)
2067 (lines-remaining (count-lines beg end))
2068 (msg (format "%%4d out of %4d lines remaining ..."
2069 (count-lines beg end)))
2070 (endmark (copy-marker end)))
2071 ;; catch errors while indenting
2072 (while (< (point) endmark)
2073 (if (> block-done 39)
2074 (progn
2075 (setq lines-remaining (- lines-remaining block-done)
2076 block-done 0)
2077 (message msg lines-remaining)))
2078 (if (= (char-after) ?\n) nil
2079 (ada-indent-current))
2080 (forward-line 1)
2081 (setq block-done (1+ block-done)))
2082 (message "Indenting ... done")))
2084 (defun ada-indent-newline-indent ()
2085 "Indent the current line, insert a newline and then indent the new line."
2086 (interactive "*")
2087 (ada-indent-current)
2088 (newline)
2089 (ada-indent-current))
2091 (defun ada-indent-newline-indent-conditional ()
2092 "Insert a newline and indent it.
2093 The original line is re-indented if `ada-indent-after-return' is non-nil."
2094 (interactive "*")
2095 ;; If at end of buffer (entering brand new code), some indentation
2096 ;; fails. For example, a block label requires whitespace following
2097 ;; the : to be recognized. So we do the newline first, then
2098 ;; go back and indent the original line.
2099 (newline)
2100 (if ada-indent-after-return
2101 (progn
2102 (forward-char -1)
2103 (ada-indent-current)
2104 (forward-char 1)))
2105 (ada-indent-current))
2107 (defun ada-justified-indent-current ()
2108 "Indent the current line and explain how the calculation was done."
2109 (interactive)
2111 (let ((cur-indent (ada-indent-current)))
2113 (let ((line (save-excursion
2114 (goto-char (car cur-indent))
2115 (count-lines 1 (point)))))
2117 (if (equal (cdr cur-indent) '(0))
2118 (message (concat "same indentation as line " (number-to-string line)))
2119 (message "%s" (mapconcat (lambda(x)
2120 (cond
2121 ((symbolp x)
2122 (symbol-name x))
2123 ((numberp x)
2124 (number-to-string x))
2125 ((listp x)
2126 (concat "- " (symbol-name (cadr x))))
2128 (cdr cur-indent)
2129 " + "))))
2130 (save-excursion
2131 (goto-char (car cur-indent))
2132 (sit-for 1))))
2134 (defun ada-batch-reformat ()
2135 "Re-indent and re-case all the files found on the command line.
2136 This function should be used from the command line, with a
2137 command like:
2138 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2140 (while command-line-args-left
2141 (let ((source (car command-line-args-left)))
2142 (message "Formating %s" source)
2143 (find-file source)
2144 (ada-indent-region (point-min) (point-max))
2145 (ada-adjust-case-buffer)
2146 (write-file source))
2147 (setq command-line-args-left (cdr command-line-args-left)))
2148 (message "Done")
2149 (kill-emacs 0))
2151 (defsubst ada-goto-previous-word ()
2152 "Move point to the beginning of the previous word of Ada code.
2153 Return the new position of point or nil if not found."
2154 (ada-goto-next-word t))
2156 (defun ada-indent-current ()
2157 "Indent current line as Ada code.
2158 Return the calculation that was done, including the reference point
2159 and the offset."
2160 (interactive)
2161 (let ((orgpoint (point-marker))
2162 cur-indent tmp-indent
2163 prev-indent)
2165 (unwind-protect
2166 (with-syntax-table ada-mode-symbol-syntax-table
2168 ;; This need to be done here so that the advice is not always
2169 ;; activated (this might interact badly with other modes)
2170 (if (featurep 'xemacs)
2171 (ad-activate 'parse-partial-sexp t))
2173 (save-excursion
2174 (setq cur-indent
2176 ;; Not First line in the buffer ?
2177 (if (save-excursion (zerop (forward-line -1)))
2178 (progn
2179 (back-to-indentation)
2180 (ada-get-current-indent))
2182 ;; first line in the buffer
2183 (list (point-min) 0))))
2185 ;; Evaluate the list to get the column to indent to
2186 ;; prev-indent contains the column to indent to
2187 (if cur-indent
2188 (setq prev-indent (save-excursion (goto-char (car cur-indent))
2189 (current-column))
2190 tmp-indent (cdr cur-indent))
2191 (setq prev-indent 0 tmp-indent '()))
2193 (while (not (null tmp-indent))
2194 (cond
2195 ((numberp (car tmp-indent))
2196 (setq prev-indent (+ prev-indent (car tmp-indent))))
2198 (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
2200 (setq tmp-indent (cdr tmp-indent)))
2202 ;; only re-indent if indentation is different then the current
2203 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
2205 (beginning-of-line)
2206 (delete-horizontal-space)
2207 (indent-to prev-indent))
2209 ;; restore position of point
2211 (goto-char orgpoint)
2212 (if (< (current-column) (current-indentation))
2213 (back-to-indentation)))
2215 (if (featurep 'xemacs)
2216 (ad-deactivate 'parse-partial-sexp)))
2218 cur-indent))
2220 (defun ada-get-current-indent ()
2221 "Return the indentation to use for the current line."
2222 (let (column
2224 match-cons
2225 result
2226 (orgpoint (save-excursion
2227 (beginning-of-line)
2228 (forward-comment -10000)
2229 (forward-line 1)
2230 (point))))
2232 (setq result
2233 (cond
2235 ;;-----------------------------
2236 ;; in open parenthesis, but not in parameter-list
2237 ;;-----------------------------
2239 ((and ada-indent-to-open-paren
2240 (not (ada-in-paramlist-p))
2241 (setq column (ada-in-open-paren-p)))
2243 ;; check if we have something like this (Table_Component_Type =>
2244 ;; Source_File_Record)
2245 (save-excursion
2247 ;; Align the closing parenthesis on the opening one
2248 (if (= (following-char) ?\))
2249 (save-excursion
2250 (goto-char column)
2251 (skip-chars-backward " \t")
2252 (list (1- (point)) 0))
2254 (if (and (skip-chars-backward " \t")
2255 (= (char-before) ?\n)
2256 (not (forward-comment -10000))
2257 (= (char-before) ?>))
2258 ;; ??? Could use a different variable
2259 (list column 'ada-broken-indent)
2261 ;; We want all continuation lines to be indented the same
2262 ;; (ada-broken-line from the opening parenthesis. However, in
2263 ;; parameter list, each new parameter should be indented at the
2264 ;; column as the opening parenthesis.
2266 ;; A special case to handle nested boolean expressions, as in
2267 ;; ((B
2268 ;; and then C) -- indented by ada-broken-indent
2269 ;; or else D) -- indenting this line.
2270 ;; ??? This is really a hack, we should have a proper way to go to
2271 ;; ??? the beginning of the statement
2273 (if (= (char-before) ?\))
2274 (backward-sexp))
2276 (if (memq (char-before) '(?, ?\; ?\( ?\)))
2277 (list column 0)
2278 (list column 'ada-continuation-indent)
2279 )))))
2281 ;;---------------------------
2282 ;; at end of buffer
2283 ;;---------------------------
2285 ((not (char-after))
2286 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2288 ;;---------------------------
2289 ;; starting with e
2290 ;;---------------------------
2292 ((= (downcase (char-after)) ?e)
2293 (cond
2295 ;; ------- end ------
2297 ((looking-at "end\\>")
2298 (let ((label 0)
2299 limit)
2300 (save-excursion
2301 (ada-goto-matching-start 1)
2304 ;; found 'loop' => skip back to 'while' or 'for'
2305 ;; if 'loop' is not on a separate line
2306 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2308 (if (save-excursion
2309 (beginning-of-line)
2310 (looking-at ".+\\<loop\\>"))
2311 (progn
2312 (save-excursion
2313 (setq limit (car (ada-search-ignore-string-comment ";" t))))
2314 (if (save-excursion
2315 (and
2316 (setq match-cons
2317 (ada-search-ignore-string-comment ada-loop-start-re t limit))
2318 (not (looking-at "\\<loop\\>"))))
2319 (progn
2320 (goto-char (car match-cons))
2321 (save-excursion
2322 (back-to-indentation)
2323 (if (looking-at ada-block-label-re)
2324 (setq label (- ada-label-indent))))))))
2326 ;; found 'record' =>
2327 ;; if the keyword is found at the beginning of a line (or just
2328 ;; after limited, we indent on it, otherwise we indent on the
2329 ;; beginning of the type declaration)
2330 ;; type A is (B : Integer;
2331 ;; C : Integer) is record
2332 ;; end record; -- This is badly indented otherwise
2333 (if (looking-at "record")
2334 (if (save-excursion
2335 (beginning-of-line)
2336 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2337 (list (save-excursion (back-to-indentation) (point)) 0)
2338 (list (save-excursion
2339 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2342 ;; Else keep the same indentation as the beginning statement
2343 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
2345 ;; ------ exception ----
2347 ((looking-at "exception\\>")
2348 (save-excursion
2349 (ada-goto-matching-start 1)
2350 (list (save-excursion (back-to-indentation) (point)) 0)))
2352 ;; else
2354 ((looking-at "else\\>")
2355 (if (save-excursion (ada-goto-previous-word)
2356 (looking-at "\\<or\\>"))
2357 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2358 (save-excursion
2359 (ada-goto-matching-start 1 nil t)
2360 (list (progn (back-to-indentation) (point)) 0))))
2362 ;; elsif
2364 ((looking-at "elsif\\>")
2365 (save-excursion
2366 (ada-goto-matching-start 1 nil t)
2367 (list (progn (back-to-indentation) (point)) 0)))
2371 ;;---------------------------
2372 ;; starting with w (when)
2373 ;;---------------------------
2375 ((and (= (downcase (char-after)) ?w)
2376 (looking-at "when\\>"))
2377 (save-excursion
2378 (ada-goto-matching-start 1)
2379 (list (save-excursion (back-to-indentation) (point))
2380 'ada-when-indent)))
2382 ;;---------------------------
2383 ;; starting with t (then)
2384 ;;---------------------------
2386 ((and (= (downcase (char-after)) ?t)
2387 (looking-at "then\\>"))
2388 (if (save-excursion (ada-goto-previous-word)
2389 (looking-at "and\\>"))
2390 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2391 (save-excursion
2392 ;; Select has been added for the statement: "select ... then abort"
2393 (ada-search-ignore-string-comment
2394 "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2395 (list (progn (back-to-indentation) (point))
2396 'ada-stmt-end-indent))))
2398 ;;---------------------------
2399 ;; starting with l (loop)
2400 ;;---------------------------
2402 ((and (= (downcase (char-after)) ?l)
2403 (looking-at "loop\\>"))
2404 (setq pos (point))
2405 (save-excursion
2406 (goto-char (match-end 0))
2407 (ada-goto-stmt-start)
2408 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2409 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2410 (unless (looking-at ada-loop-start-re)
2411 (ada-search-ignore-string-comment ada-loop-start-re
2412 nil pos))
2413 (if (looking-at "\\<loop\\>")
2414 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2415 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2417 ;;----------------------------
2418 ;; starting with l (limited) or r (record)
2419 ;;----------------------------
2421 ((or (and (= (downcase (char-after)) ?l)
2422 (looking-at "limited\\>"))
2423 (and (= (downcase (char-after)) ?r)
2424 (looking-at "record\\>")))
2426 (save-excursion
2427 (ada-search-ignore-string-comment
2428 "\\<\\(type\\|use\\)\\>" t nil)
2429 (if (looking-at "\\<use\\>")
2430 (ada-search-ignore-string-comment "for" t nil nil
2431 'word-search-backward))
2432 (list (progn (back-to-indentation) (point))
2433 'ada-indent-record-rel-type)))
2435 ;;---------------------------
2436 ;; starting with b (begin)
2437 ;;---------------------------
2439 ((and (= (downcase (char-after)) ?b)
2440 (looking-at "begin\\>"))
2441 (save-excursion
2442 (if (ada-goto-decl-start t)
2443 (list (progn (back-to-indentation) (point)) 0)
2444 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2446 ;;---------------------------
2447 ;; starting with i (is)
2448 ;;---------------------------
2450 ((and (= (downcase (char-after)) ?i)
2451 (looking-at "is\\>"))
2453 (if (and ada-indent-is-separate
2454 (save-excursion
2455 (goto-char (match-end 0))
2456 (ada-goto-next-non-ws (point-at-eol))
2457 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2458 (save-excursion
2459 (ada-goto-stmt-start)
2460 (list (progn (back-to-indentation) (point)) 'ada-indent))
2461 (save-excursion
2462 (ada-goto-stmt-start)
2463 (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
2464 (list (progn (back-to-indentation) (point)) 0)
2465 (list (progn (back-to-indentation) (point)) 'ada-indent)))))
2467 ;;---------------------------
2468 ;; starting with r (return, renames)
2469 ;;---------------------------
2471 ((and (= (downcase (char-after)) ?r)
2472 (looking-at "re\\(turn\\|names\\)\\>"))
2474 (save-excursion
2475 (let ((var 'ada-indent-return))
2476 ;; If looking at a renames, skip the 'return' statement too
2477 (if (looking-at "renames")
2478 (let (pos)
2479 (save-excursion
2480 (setq pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
2481 (if (and pos
2482 (= (downcase (char-after (car pos))) ?r))
2483 (goto-char (car pos)))
2484 (setq var 'ada-indent-renames)))
2486 (forward-comment -1000)
2487 (if (= (char-before) ?\))
2488 (forward-sexp -1)
2489 (forward-word -1))
2491 ;; If there is a parameter list, and we have a function declaration
2492 ;; or a access to subprogram declaration
2493 (let ((num-back 1))
2494 (if (and (= (following-char) ?\()
2495 (save-excursion
2496 (or (progn
2497 (backward-word 1)
2498 (looking-at "\\(function\\|procedure\\)\\>"))
2499 (progn
2500 (backward-word 1)
2501 (setq num-back 2)
2502 (looking-at "\\(function\\|procedure\\)\\>")))))
2504 ;; The indentation depends of the value of ada-indent-return
2505 (if (<= (eval var) 0)
2506 (list (point) (list '- var))
2507 (list (progn (backward-word num-back) (point))
2508 var))
2510 ;; Else there is no parameter list, but we have a function
2511 ;; Only do something special if the user want to indent
2512 ;; relative to the "function" keyword
2513 (if (and (> (eval var) 0)
2514 (save-excursion (forward-word -1)
2515 (looking-at "function\\>")))
2516 (list (progn (forward-word -1) (point)) var)
2518 ;; Else...
2519 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
2521 ;;--------------------------------
2522 ;; starting with 'o' or 'p'
2523 ;; 'or' as statement-start
2524 ;; 'private' as statement-start
2525 ;;--------------------------------
2527 ((and (or (= (downcase (char-after)) ?o)
2528 (= (downcase (char-after)) ?p))
2529 (or (ada-looking-at-semi-or)
2530 (ada-looking-at-semi-private)))
2531 (save-excursion
2532 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2533 (ada-goto-matching-start 1)
2534 (list (progn (back-to-indentation) (point)) 0)))
2536 ;;--------------------------------
2537 ;; starting with 'd' (do)
2538 ;;--------------------------------
2540 ((and (= (downcase (char-after)) ?d)
2541 (looking-at "do\\>"))
2542 (save-excursion
2543 (ada-goto-stmt-start)
2544 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2546 ;;--------------------------------
2547 ;; starting with '-' (comment)
2548 ;;--------------------------------
2550 ((= (char-after) ?-)
2551 (if ada-indent-comment-as-code
2553 ;; Indent comments on previous line comments if required
2554 ;; We must use a search-forward (even if the code is more complex),
2555 ;; since we want to find the beginning of the comment.
2556 (let (pos)
2558 (if (and ada-indent-align-comments
2559 (save-excursion
2560 (forward-line -1)
2561 (beginning-of-line)
2562 (while (and (not pos)
2563 (search-forward "--" (point-at-eol) t))
2564 (unless (ada-in-string-p)
2565 (setq pos (point))))
2566 pos))
2567 (list (- pos 2) 0)
2569 ;; Else always on previous line
2570 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2572 ;; Else same indentation as the previous line
2573 (list (save-excursion (back-to-indentation) (point)) 0)))
2575 ;;--------------------------------
2576 ;; starting with '#' (preprocessor line)
2577 ;;--------------------------------
2579 ((and (= (char-after) ?#)
2580 (equal ada-which-compiler 'gnat)
2581 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2582 (list (point-at-bol) 0))
2584 ;;--------------------------------
2585 ;; starting with ')' (end of a parameter list)
2586 ;;--------------------------------
2588 ((and (not (eobp)) (= (char-after) ?\)))
2589 (save-excursion
2590 (forward-char 1)
2591 (backward-sexp 1)
2592 (list (point) 0)))
2594 ;;---------------------------------
2595 ;; new/abstract/separate
2596 ;;---------------------------------
2598 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2599 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2601 ;;---------------------------------
2602 ;; package/function/procedure
2603 ;;---------------------------------
2605 ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
2606 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2607 (save-excursion
2608 ;; Go up until we find either a generic section, or the end of the
2609 ;; previous subprogram/package, or 'overriding' for this function/procedure
2610 (let (found)
2611 (while (and (not found)
2612 (ada-search-ignore-string-comment
2613 "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t))
2615 ;; avoid "with procedure"... in generic parts
2616 (save-excursion
2617 (forward-word -1)
2618 (setq found (not (looking-at "with"))))))
2620 (cond
2621 ((looking-at "\\<generic\\|overriding\\>")
2622 (list (progn (back-to-indentation) (point)) 0))
2625 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
2627 ;;---------------------------------
2628 ;; label
2629 ;;---------------------------------
2631 ((looking-at ada-label-re)
2632 (if (ada-in-decl-p)
2633 ;; ada-block-label-re matches variable declarations
2634 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2635 (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
2636 '(ada-label-indent))))
2640 ;;---------------------------------
2641 ;; Other syntaxes
2642 ;;---------------------------------
2643 (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2645 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2646 "Calculate the indentation for the new line after ORGPOINT.
2647 The result list is based on the previous lines in the buffer.
2648 If NOMOVE is nil, moves point to the beginning of the current statement.
2649 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2650 (if initial-pos
2651 (goto-char initial-pos))
2652 (let ((oldpoint (point)))
2654 ;; Is inside a parameter-list ?
2655 (if (ada-in-paramlist-p)
2656 (ada-get-indent-paramlist)
2658 ;; Move to beginning of current statement. If already at a
2659 ;; statement start, move to beginning of enclosing statement.
2660 (unless nomove
2661 (ada-goto-stmt-start t))
2663 ;; no beginning found => don't change indentation
2664 (if (and (eq oldpoint (point))
2665 (not nomove))
2666 (ada-get-indent-nochange)
2668 (cond
2670 ((and
2671 ada-indent-to-open-paren
2672 (ada-in-open-paren-p))
2673 (ada-get-indent-open-paren))
2675 ((looking-at "end\\>")
2676 (ada-get-indent-end orgpoint))
2678 ((looking-at ada-loop-start-re)
2679 (ada-get-indent-loop orgpoint))
2681 ((looking-at ada-subprog-start-re)
2682 (ada-get-indent-subprog orgpoint))
2684 ((looking-at ada-block-start-re)
2685 (ada-get-indent-block-start orgpoint))
2687 ((looking-at ada-block-label-re) ; also variable declaration
2688 (ada-get-indent-block-label orgpoint))
2690 ((looking-at ada-goto-label-re)
2691 (ada-get-indent-goto-label orgpoint))
2693 ((looking-at "\\(sub\\)?type\\>")
2694 (ada-get-indent-type orgpoint))
2696 ;; "then" has to be included in the case of "select...then abort"
2697 ;; statements, since (goto-stmt-start) at the beginning of
2698 ;; the current function would leave the cursor on that position
2699 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2700 (ada-get-indent-if orgpoint))
2702 ((looking-at "case\\>")
2703 (ada-get-indent-case orgpoint))
2705 ((looking-at "when\\>")
2706 (ada-get-indent-when orgpoint))
2708 ((looking-at "separate\\>")
2709 (ada-get-indent-nochange))
2711 ((looking-at "with\\>\\|use\\>")
2712 ;; Are we still in that statement, or are we in fact looking at
2713 ;; the previous one ?
2714 (if (save-excursion (search-forward ";" oldpoint t))
2715 (list (progn (back-to-indentation) (point)) 0)
2716 (list (point) (if (looking-at "with")
2717 'ada-with-indent
2718 'ada-use-indent))))
2721 (ada-get-indent-noindent orgpoint)))))
2724 (defun ada-get-indent-open-paren ()
2725 "Calculate the indentation when point is behind an unclosed parenthesis."
2726 (list (ada-in-open-paren-p) 0))
2728 (defun ada-get-indent-nochange ()
2729 "Return the current indentation of the previous line."
2730 (save-excursion
2731 (forward-line -1)
2732 (back-to-indentation)
2733 (list (point) 0)))
2735 (defun ada-get-indent-paramlist ()
2736 "Calculate the indentation when point is inside a parameter list."
2737 (save-excursion
2738 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2739 (cond
2740 ;; in front of the first parameter
2741 ((= (char-after) ?\()
2742 (goto-char (match-end 0))
2743 (list (point) 0))
2745 ;; in front of another parameter
2746 ((= (char-after) ?\;)
2747 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2748 (ada-goto-next-non-ws)
2749 (list (point) 0))
2751 ;; After an affectation (default parameter value in subprogram
2752 ;; declaration)
2753 ((and (= (following-char) ?=) (= (preceding-char) ?:))
2754 (back-to-indentation)
2755 (list (point) 'ada-broken-indent))
2757 ;; inside a parameter declaration
2759 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2760 (ada-goto-next-non-ws)
2761 (list (point) 'ada-broken-indent)))))
2763 (defun ada-get-indent-end (orgpoint)
2764 "Calculate the indentation when point is just before an end statement.
2765 ORGPOINT is the limit position used in the calculation."
2766 (let ((defun-name nil)
2767 (indent nil))
2769 ;; is the line already terminated by ';' ?
2770 (if (save-excursion
2771 (ada-search-ignore-string-comment ";" nil orgpoint nil
2772 'search-forward))
2774 ;; yes, look what's following 'end'
2775 (progn
2776 (forward-word 1)
2777 (ada-goto-next-non-ws)
2778 (cond
2780 ;; loop/select/if/case/return
2782 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
2783 (save-excursion (ada-check-matching-start (match-string 0)))
2784 (list (save-excursion (back-to-indentation) (point)) 0))
2787 ;; record
2789 ((looking-at "\\<record\\>")
2790 (save-excursion
2791 (ada-check-matching-start (match-string 0))
2792 ;; we are now looking at the matching "record" statement
2793 (forward-word 1)
2794 (ada-goto-stmt-start)
2795 ;; now on the matching type declaration, or use clause
2796 (unless (looking-at "\\(for\\|type\\)\\>")
2797 (ada-search-ignore-string-comment "\\<type\\>" t))
2798 (list (progn (back-to-indentation) (point)) 0)))
2800 ;; a named block end
2802 ((looking-at ada-ident-re)
2803 (setq defun-name (match-string 0))
2804 (save-excursion
2805 (ada-goto-matching-start 0)
2806 (ada-check-defun-name defun-name))
2807 (list (progn (back-to-indentation) (point)) 0))
2809 ;; a block-end without name
2811 ((= (char-after) ?\;)
2812 (save-excursion
2813 (ada-goto-matching-start 0)
2814 (if (looking-at "\\<begin\\>")
2815 (progn
2816 (setq indent (list (point) 0))
2817 (if (ada-goto-decl-start t)
2818 (list (progn (back-to-indentation) (point)) 0)
2819 indent))
2820 (list (progn (back-to-indentation) (point)) 0)
2823 ;; anything else - should maybe signal an error ?
2826 (list (save-excursion (back-to-indentation) (point))
2827 'ada-broken-indent))))
2829 (list (save-excursion (back-to-indentation) (point))
2830 'ada-broken-indent))))
2832 (defun ada-get-indent-case (orgpoint)
2833 "Calculate the indentation when point is just before a case statement.
2834 ORGPOINT is the limit position used in the calculation."
2835 (let ((match-cons nil)
2836 (opos (point)))
2837 (cond
2839 ;; case..is..when..=>
2841 ((save-excursion
2842 (setq match-cons (and
2843 ;; the `=>' must be after the keyword `is'.
2844 (ada-search-ignore-string-comment
2845 "is" nil orgpoint nil 'word-search-forward)
2846 (ada-search-ignore-string-comment
2847 "[ \t\n]+=>" nil orgpoint))))
2848 (save-excursion
2849 (goto-char (car match-cons))
2850 (unless (ada-search-ignore-string-comment "when" t opos)
2851 (error "Missing 'when' between 'case' and '=>'"))
2852 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2854 ;; case..is..when
2856 ((save-excursion
2857 (setq match-cons (ada-search-ignore-string-comment
2858 "when" nil orgpoint nil 'word-search-forward)))
2859 (goto-char (cdr match-cons))
2860 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2862 ;; case..is
2864 ((save-excursion
2865 (setq match-cons (ada-search-ignore-string-comment
2866 "is" nil orgpoint nil 'word-search-forward)))
2867 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2869 ;; incomplete case
2872 (list (save-excursion (back-to-indentation) (point))
2873 'ada-broken-indent)))))
2875 (defun ada-get-indent-when (orgpoint)
2876 "Calculate the indentation when point is just before a when statement.
2877 ORGPOINT is the limit position used in the calculation."
2878 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2879 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2880 (list cur-indent 'ada-indent)
2881 (list cur-indent 'ada-broken-indent))))
2883 (defun ada-get-indent-if (orgpoint)
2884 "Calculate the indentation when point is just before an if statement.
2885 ORGPOINT is the limit position used in the calculation."
2886 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2887 (match-cons nil))
2889 ;; Move to the correct then (ignore all "and then")
2891 (while (and (setq match-cons (ada-search-ignore-string-comment
2892 "\\<\\(then\\|and[ \t]*then\\)\\>"
2893 nil orgpoint))
2894 (= (downcase (char-after (car match-cons))) ?a)))
2895 ;; If "then" was found (we are looking at it)
2896 (if match-cons
2897 (progn
2899 ;; 'then' first in separate line ?
2900 ;; => indent according to 'then',
2901 ;; => else indent according to 'if'
2903 (if (save-excursion
2904 (back-to-indentation)
2905 (looking-at "\\<then\\>"))
2906 (setq cur-indent (save-excursion (back-to-indentation) (point))))
2907 ;; skip 'then'
2908 (forward-word 1)
2909 (list cur-indent 'ada-indent))
2911 (list cur-indent 'ada-broken-indent))))
2913 (defun ada-get-indent-block-start (orgpoint)
2914 "Calculate the indentation when point is at the start of a block.
2915 ORGPOINT is the limit position used in the calculation."
2916 (let ((pos nil))
2917 (cond
2918 ((save-excursion
2919 (forward-word 1)
2920 (setq pos (ada-goto-next-non-ws orgpoint)))
2921 (goto-char pos)
2922 (save-excursion
2923 (ada-indent-on-previous-lines t orgpoint)))
2925 ;; Special case for record types, for instance for:
2926 ;; type A is (B : Integer;
2927 ;; C : Integer) is record
2928 ;; null; -- This is badly indented otherwise
2929 ((looking-at "record")
2931 ;; If record is at the beginning of the line, indent from there
2932 (if (save-excursion
2933 (beginning-of-line)
2934 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2935 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
2937 ;; else indent relative to the type command
2938 (list (save-excursion
2939 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2940 'ada-indent)))
2942 ;; Special case for label:
2943 ((looking-at ada-block-label-re)
2944 (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent) 'ada-indent))
2946 ;; nothing follows the block-start
2948 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
2950 (defun ada-get-indent-subprog (orgpoint)
2951 "Calculate the indentation when point is just before a subprogram.
2952 ORGPOINT is the limit position used in the calculation."
2953 (let ((match-cons nil)
2954 (cur-indent (save-excursion (back-to-indentation) (point)))
2955 (foundis nil))
2957 ;; is there an 'is' in front of point ?
2959 (if (save-excursion
2960 (setq match-cons
2961 (ada-search-ignore-string-comment
2962 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
2964 ;; yes, then skip to its end
2966 (progn
2967 (setq foundis t)
2968 (goto-char (cdr match-cons)))
2970 ;; no, then goto next non-ws, if there is one in front of point
2972 (progn
2973 (unless (ada-goto-next-non-ws orgpoint)
2974 (goto-char orgpoint))))
2976 (cond
2978 ;; nothing follows 'is'
2980 ((and
2981 foundis
2982 (save-excursion
2983 (not (ada-search-ignore-string-comment
2984 "[^ \t\n]" nil orgpoint t))))
2985 (list cur-indent 'ada-indent))
2987 ;; is abstract/separate/new ...
2989 ((and
2990 foundis
2991 (save-excursion
2992 (setq match-cons
2993 (ada-search-ignore-string-comment
2994 "\\<\\(separate\\|new\\|abstract\\)\\>"
2995 nil orgpoint))))
2996 (goto-char (car match-cons))
2997 (ada-search-ignore-string-comment ada-subprog-start-re t)
2998 (ada-get-indent-noindent orgpoint))
3000 ;; something follows 'is'
3002 ((and
3003 foundis
3004 (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
3005 (goto-char match-cons)
3006 (ada-indent-on-previous-lines t orgpoint)))
3008 ;; no 'is' but ';'
3010 ((save-excursion
3011 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
3012 (list cur-indent 0))
3014 ;; no 'is' or ';'
3017 (list cur-indent 'ada-broken-indent)))))
3019 (defun ada-get-indent-noindent (orgpoint)
3020 "Calculate the indentation when point is just before a 'noindent stmt'.
3021 ORGPOINT is the limit position used in the calculation."
3022 (let ((label 0))
3023 (save-excursion
3024 (beginning-of-line)
3026 (cond
3028 ;; This one is called when indenting a line preceded by a multi-line
3029 ;; subprogram declaration (in that case, we are at this point inside
3030 ;; the parameter declaration list)
3031 ((ada-in-paramlist-p)
3032 (ada-previous-procedure)
3033 (list (save-excursion (back-to-indentation) (point)) 0))
3035 ;; This one is called when indenting the second line of a multi-line
3036 ;; declaration section, in a declare block or a record declaration
3037 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3038 (list (save-excursion (back-to-indentation) (point))
3039 'ada-broken-decl-indent))
3041 ;; This one is called in every other case when indenting a line at the
3042 ;; top level
3044 (if (looking-at (concat "[ \t]*" ada-block-label-re))
3045 (setq label (- ada-label-indent))
3047 (let (p)
3049 ;; "with private" or "null record" cases
3050 (if (or (save-excursion
3051 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
3052 (setq p (point))
3053 (save-excursion (forward-char -7);; skip back "private"
3054 (ada-goto-previous-word)
3055 (looking-at "with"))))
3056 (save-excursion
3057 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
3058 (setq p (point))
3059 (save-excursion (forward-char -6);; skip back "record"
3060 (ada-goto-previous-word)
3061 (looking-at "null")))))
3062 (progn
3063 (goto-char p)
3064 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
3065 (list (save-excursion (back-to-indentation) (point)) 0)))))
3066 (if (save-excursion
3067 (ada-search-ignore-string-comment ";" nil orgpoint nil
3068 'search-forward))
3069 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
3070 (list (+ (save-excursion (back-to-indentation) (point)) label)
3071 'ada-broken-indent)))))))
3073 (defun ada-get-indent-block-label (orgpoint)
3074 "Calculate the indentation when before a label or variable declaration.
3075 ORGPOINT is the limit position used in the calculation."
3076 (let ((match-cons nil)
3077 (cur-indent (save-excursion (back-to-indentation) (point))))
3078 (ada-search-ignore-string-comment ":" nil)
3079 (cond
3080 ;; loop label
3081 ((save-excursion
3082 (setq match-cons (ada-search-ignore-string-comment
3083 ada-loop-start-re nil orgpoint)))
3084 (goto-char (car match-cons))
3085 (ada-get-indent-loop orgpoint))
3087 ;; declare label
3088 ((save-excursion
3089 (setq match-cons (ada-search-ignore-string-comment
3090 "\\<declare\\|begin\\>" nil orgpoint)))
3091 (goto-char (car match-cons))
3092 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3094 ;; variable declaration
3095 ((ada-in-decl-p)
3096 (if (save-excursion
3097 (ada-search-ignore-string-comment ";" nil orgpoint))
3098 (list cur-indent 0)
3099 (list cur-indent 'ada-broken-indent)))
3101 ;; nothing follows colon
3103 (list cur-indent '(- ada-label-indent))))))
3105 (defun ada-get-indent-goto-label (orgpoint)
3106 "Calculate the indentation when at a goto label."
3107 (search-forward ">>")
3108 (ada-goto-next-non-ws)
3109 (if (>= (point) orgpoint)
3110 ;; labeled statement is the one we need to indent
3111 (list (- (point) ada-label-indent))
3112 ;; else indentation is indent for labeled statement
3113 (ada-indent-on-previous-lines t orgpoint)))
3115 (defun ada-get-indent-loop (orgpoint)
3116 "Calculate the indentation when just before a loop or a for ... use.
3117 ORGPOINT is the limit position used in the calculation."
3118 (let ((match-cons nil)
3119 (pos (point))
3121 ;; If looking at a named block, skip the label
3122 (label (save-excursion
3123 (back-to-indentation)
3124 (if (looking-at ada-block-label-re)
3125 (- ada-label-indent)
3126 0))))
3128 (cond
3131 ;; statement complete
3133 ((save-excursion
3134 (ada-search-ignore-string-comment ";" nil orgpoint nil
3135 'search-forward))
3136 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
3138 ;; simple loop
3140 ((looking-at "loop\\>")
3141 (setq pos (ada-get-indent-block-start orgpoint))
3142 (if (equal label 0)
3144 (list (+ (car pos) label) (cadr pos))))
3147 ;; 'for'- loop (or also a for ... use statement)
3149 ((looking-at "for\\>")
3150 (cond
3152 ;; for ... use
3154 ((save-excursion
3155 (and
3156 (goto-char (match-end 0))
3157 (ada-goto-next-non-ws orgpoint)
3158 (forward-word 1)
3159 (if (= (char-after) ?') (forward-word 1) t)
3160 (ada-goto-next-non-ws orgpoint)
3161 (looking-at "\\<use\\>")
3163 ;; check if there is a 'record' before point
3165 (progn
3166 (setq match-cons (ada-search-ignore-string-comment
3167 "record" nil orgpoint nil 'word-search-forward))
3168 t)))
3169 (if match-cons
3170 (progn
3171 (goto-char (car match-cons))
3172 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3173 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3177 ;; for..loop
3179 ((save-excursion
3180 (setq match-cons (ada-search-ignore-string-comment
3181 "loop" nil orgpoint nil 'word-search-forward)))
3182 (goto-char (car match-cons))
3184 ;; indent according to 'loop', if it's first in the line;
3185 ;; otherwise to 'for'
3187 (unless (save-excursion
3188 (back-to-indentation)
3189 (looking-at "\\<loop\\>"))
3190 (goto-char pos))
3191 (list (+ (save-excursion (back-to-indentation) (point)) label)
3192 'ada-indent))
3194 ;; for-statement is broken
3197 (list (+ (save-excursion (back-to-indentation) (point)) label)
3198 'ada-broken-indent))))
3201 ;; 'while'-loop
3203 ((looking-at "while\\>")
3205 ;; while..loop ?
3207 (if (save-excursion
3208 (setq match-cons (ada-search-ignore-string-comment
3209 "loop" nil orgpoint nil 'word-search-forward)))
3211 (progn
3212 (goto-char (car match-cons))
3214 ;; indent according to 'loop', if it's first in the line;
3215 ;; otherwise to 'while'.
3217 (unless (save-excursion
3218 (back-to-indentation)
3219 (looking-at "\\<loop\\>"))
3220 (goto-char pos))
3221 (list (+ (save-excursion (back-to-indentation) (point)) label)
3222 'ada-indent))
3224 (list (+ (save-excursion (back-to-indentation) (point)) label)
3225 'ada-broken-indent))))))
3227 (defun ada-get-indent-type (orgpoint)
3228 "Calculate the indentation when before a type statement.
3229 ORGPOINT is the limit position used in the calculation."
3230 (let ((match-dat nil))
3231 (cond
3233 ;; complete record declaration
3235 ((save-excursion
3236 (and
3237 (setq match-dat (ada-search-ignore-string-comment
3238 "end" nil orgpoint nil 'word-search-forward))
3239 (ada-goto-next-non-ws)
3240 (looking-at "\\<record\\>")
3241 (forward-word 1)
3242 (ada-goto-next-non-ws)
3243 (= (char-after) ?\;)))
3244 (goto-char (car match-dat))
3245 (list (save-excursion (back-to-indentation) (point)) 0))
3247 ;; record type
3249 ((save-excursion
3250 (setq match-dat (ada-search-ignore-string-comment
3251 "record" nil orgpoint nil 'word-search-forward)))
3252 (goto-char (car match-dat))
3253 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3255 ;; complete type declaration
3257 ((save-excursion
3258 (ada-search-ignore-string-comment ";" nil orgpoint nil
3259 'search-forward))
3260 (list (save-excursion (back-to-indentation) (point)) 0))
3262 ;; "type ... is", but not "type ... is ...", which is broken
3264 ((save-excursion
3265 (and
3266 (ada-search-ignore-string-comment "is" nil orgpoint nil
3267 'word-search-forward)
3268 (not (ada-goto-next-non-ws orgpoint))))
3269 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3271 ;; broken statement
3274 (list (save-excursion (back-to-indentation) (point))
3275 'ada-broken-indent)))))
3278 ;; -----------------------------------------------------------
3279 ;; -- searching and matching
3280 ;; -----------------------------------------------------------
3282 (defun ada-goto-stmt-start (&optional ignore-goto-label)
3283 "Move point to the beginning of the statement that point is in or after.
3284 Return the new position of point.
3285 As a special case, if we are looking at a closing parenthesis, skip to the
3286 open parenthesis."
3287 (let ((match-dat nil)
3288 (orgpoint (point)))
3290 (setq match-dat (ada-search-prev-end-stmt))
3291 (if match-dat
3294 ;; found a previous end-statement => check if anything follows
3296 (unless (looking-at "declare")
3297 (progn
3298 (unless (save-excursion
3299 (goto-char (cdr match-dat))
3300 (ada-goto-next-non-ws orgpoint ignore-goto-label))
3302 ;; nothing follows => it's the end-statement directly in
3303 ;; front of point => search again
3305 (setq match-dat (ada-search-prev-end-stmt)))
3307 ;; if found the correct end-statement => goto next non-ws
3309 (if match-dat
3310 (goto-char (cdr match-dat)))
3311 (ada-goto-next-non-ws)
3315 ;; no previous end-statement => we are at the beginning of the
3316 ;; accessible part of the buffer
3318 (progn
3319 (goto-char (point-min))
3321 ;; skip to the very first statement, if there is one
3323 (unless (ada-goto-next-non-ws orgpoint)
3324 (goto-char orgpoint))))
3325 (point)))
3328 (defun ada-search-prev-end-stmt ()
3329 "Move point to previous end statement.
3330 Return a cons cell whose car is the beginning and whose cdr
3331 is the end of the match."
3332 (let ((match-dat nil)
3333 (found nil))
3335 ;; search until found or beginning-of-buffer
3336 (while
3337 (and
3338 (not found)
3339 (setq match-dat (ada-search-ignore-string-comment
3340 ada-end-stmt-re t)))
3342 (goto-char (car match-dat))
3343 (unless (ada-in-open-paren-p)
3344 (cond
3346 ((and (looking-at
3347 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3348 (save-excursion
3349 (ada-goto-previous-word)
3350 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3351 (forward-word -1))
3353 ((looking-at "is")
3354 (setq found
3355 (and (save-excursion (ada-goto-previous-word)
3356 (ada-goto-previous-word)
3357 (not (looking-at "subtype")))
3359 (save-excursion (goto-char (cdr match-dat))
3360 (ada-goto-next-non-ws)
3361 ;; words that can go after an 'is'
3362 (not (looking-at
3363 (eval-when-compile
3364 (concat "\\<"
3365 (regexp-opt
3366 '("separate" "access" "array"
3367 "private" "abstract" "new") t)
3368 "\\>\\|("))))))))
3370 ((looking-at "private")
3371 (save-excursion
3372 (backward-word 1)
3373 (setq found (not (looking-at "is")))))
3376 (setq found t))
3379 (if found
3380 match-dat
3381 nil)))
3383 (defun ada-goto-next-non-ws (&optional limit skip-goto-label)
3384 "Skip to next non-whitespace character.
3385 Skips spaces, newlines and comments, and possibly goto labels.
3386 Return `point' if moved, nil if not.
3387 Stop the search at LIMIT.
3388 Do not call this function from within a string."
3389 (unless limit
3390 (setq limit (point-max)))
3391 (while (and (<= (point) limit)
3392 (or (progn (forward-comment 10000)
3393 (if (and (not (eobp))
3394 (save-excursion (forward-char 1)
3395 (ada-in-string-p)))
3396 (progn (forward-sexp 1) t)))
3397 (and skip-goto-label
3398 (looking-at ada-goto-label-re)
3399 (progn
3400 (goto-char (match-end 0))
3401 t)))))
3402 (if (< (point) limit)
3403 (point)
3404 nil)
3408 (defun ada-goto-stmt-end (&optional limit)
3409 "Move point to the end of the statement that point is in or before.
3410 Return the new position of point or nil if not found.
3411 Stop the search at LIMIT."
3412 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
3413 (point)
3414 nil))
3417 (defun ada-goto-next-word (&optional backward)
3418 "Move point to the beginning of the next word of Ada code.
3419 If BACKWARD is non-nil, jump to the beginning of the previous word.
3420 Return the new position of point or nil if not found."
3421 (let ((match-cons nil)
3422 (orgpoint (point))
3423 (old-syntax (char-to-string (char-syntax ?_))))
3424 (modify-syntax-entry ?_ "w")
3425 (unless backward
3426 (skip-syntax-forward "w"))
3427 (if (setq match-cons
3428 (ada-search-ignore-string-comment "\\w" backward nil t))
3430 ;; move to the beginning of the word found
3432 (progn
3433 (goto-char (car match-cons))
3434 (skip-syntax-backward "w")
3435 (point))
3437 ;; if not found, restore old position of point
3439 (goto-char orgpoint)
3440 'nil)
3441 (modify-syntax-entry ?_ old-syntax))
3445 (defun ada-check-matching-start (keyword)
3446 "Signal an error if matching block start is not KEYWORD.
3447 Moves point to the matching block start."
3448 (ada-goto-matching-start 0)
3449 (unless (looking-at (concat "\\<" keyword "\\>"))
3450 (error "Matching start is not '%s'" keyword)))
3453 (defun ada-check-defun-name (defun-name)
3454 "Check if the name of the matching defun really is DEFUN-NAME.
3455 Assumes point to be already positioned by `ada-goto-matching-start'.
3456 Moves point to the beginning of the declaration."
3458 ;; named block without a `declare'; ada-goto-matching-start leaves
3459 ;; point at start of 'begin' for a block.
3460 (if (save-excursion
3461 (ada-goto-previous-word)
3462 (looking-at (concat "\\<" defun-name "\\> *:")))
3463 t ; name matches
3464 ;; else
3466 ;; 'accept' or 'package' ?
3468 (unless (looking-at ada-subprog-start-re)
3469 (ada-goto-decl-start))
3471 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3473 (save-excursion
3475 ;; a named 'declare'-block ? => jump to the label
3477 (if (looking-at "\\<declare\\>")
3478 (progn
3479 (forward-comment -1)
3480 (backward-word 1))
3482 ;; no, => 'procedure'/'function'/'task'/'protected'
3484 (progn
3485 (forward-word 2)
3486 (backward-word 1)
3488 ;; skip 'body' 'type'
3490 (if (looking-at "\\<\\(body\\|type\\)\\>")
3491 (forward-word 1))
3492 (forward-sexp 1)
3493 (backward-sexp 1)))
3495 ;; should be looking-at the correct name
3497 (unless (looking-at (concat "\\<" defun-name "\\>"))
3498 (error "Matching defun has different name: %s"
3499 (buffer-substring (point)
3500 (progn (forward-sexp 1) (point))))))))
3502 (defun ada-goto-decl-start (&optional noerror)
3503 "Move point to the declaration start of the current construct.
3504 If NOERROR is non-nil, return nil if no match was found;
3505 otherwise throw error."
3506 (let ((nest-count 1)
3507 (regexp (eval-when-compile
3508 (concat "\\<"
3509 (regexp-opt
3510 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
3511 "\\>")))
3513 ;; first should be set to t if we should stop at the first
3514 ;; "begin" we encounter.
3515 (first t)
3516 (count-generic nil)
3517 (stop-at-when nil)
3520 ;; Ignore "when" most of the time, except if we are looking at the
3521 ;; beginning of a block (structure: case .. is
3522 ;; when ... =>
3523 ;; begin ...
3524 ;; exception ... )
3525 (if (looking-at "begin")
3526 (setq stop-at-when t))
3528 (if (or
3529 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3530 (save-excursion
3531 (ada-search-ignore-string-comment
3532 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
3533 (looking-at "generic")))
3534 (setq count-generic t))
3536 ;; search backward for interesting keywords
3537 (while (and
3538 (not (zerop nest-count))
3539 (ada-search-ignore-string-comment regexp t))
3541 ;; calculate nest-depth
3543 (cond
3545 ((looking-at "end")
3546 (ada-goto-matching-start 1 noerror)
3548 ;; In some case, two begin..end block can follow each other closely,
3549 ;; which we have to detect, as in
3550 ;; procedure P is
3551 ;; procedure Q is
3552 ;; begin
3553 ;; end;
3554 ;; begin -- here we should go to procedure, not begin
3555 ;; end
3557 (if (looking-at "begin")
3558 (let ((loop-again t))
3559 (save-excursion
3560 (while loop-again
3561 ;; If begin was just there as the beginning of a block
3562 ;; (with no declare) then do nothing, otherwise just
3563 ;; register that we have to find the statement that
3564 ;; required the begin
3566 (ada-search-ignore-string-comment
3567 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3570 (if (looking-at "end")
3571 (ada-goto-matching-start 1 noerror t)
3573 (setq loop-again nil)
3574 (unless (looking-at "begin")
3575 (setq nest-count (1+ nest-count))))
3579 ((looking-at "generic")
3580 (if count-generic
3581 (progn
3582 (setq first nil)
3583 (setq nest-count (1- nest-count)))))
3585 ((looking-at "if")
3586 (save-excursion
3587 (forward-word -1)
3588 (unless (looking-at "\\<end[ \t\n]*if\\>")
3589 (progn
3590 (setq nest-count (1- nest-count))
3591 (setq first nil)))))
3594 ((looking-at "declare\\|generic")
3595 (setq nest-count (1- nest-count))
3596 (setq first t))
3598 ((looking-at "is")
3599 ;; look for things to ignore
3602 ;; generic formal parameter
3603 (looking-at "is[ t]+<>")
3605 ;; A type definition, or a case statement. Note that the
3606 ;; goto-matching-start above on 'end record' leaves us at
3607 ;; 'record', not at 'type'.
3609 ;; We get to a case statement here by calling
3610 ;; 'ada-move-to-end' from inside a case statement; then
3611 ;; we are not ignoring 'when'.
3612 (save-excursion
3613 ;; Skip type discriminants or case argument function call param list
3614 (forward-comment -10000)
3615 (forward-char -1)
3616 (if (= (char-after) ?\))
3617 (progn
3618 (forward-char 1)
3619 (backward-sexp 1)
3620 (forward-comment -10000)
3622 ;; skip type or case argument name
3623 (skip-chars-backward "a-zA-Z0-9_.'")
3624 (ada-goto-previous-word)
3625 (and
3626 ;; if it's a protected type, it's the decl start we
3627 ;; are looking for; since we didn't see the 'end'
3628 ;; above, we are inside it.
3629 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3630 (save-match-data
3631 (ada-goto-previous-word)
3632 (not (looking-at "\\<protected\\>"))))
3633 ) ; end of type definition p
3635 ;; null procedure declaration
3636 (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
3637 );; end or
3638 ;; skip this construct
3640 ;; this is the right "is"
3641 (setq nest-count (1- nest-count))
3642 (setq first nil)))
3645 ((looking-at "new")
3646 (if (save-excursion
3647 (ada-goto-previous-word)
3648 (looking-at "is"))
3649 (goto-char (match-beginning 0))))
3651 ((and first
3652 (looking-at "begin"))
3653 (setq nest-count 0))
3655 ((looking-at "when")
3656 (save-excursion
3657 (forward-word -1)
3658 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3659 (progn
3660 (if stop-at-when
3661 (setq nest-count (1- nest-count)))
3662 ))))
3664 ((looking-at "begin")
3665 (setq first nil))
3668 (setq nest-count (1+ nest-count))
3669 (setq first nil)))
3671 );; end of loop
3673 ;; check if declaration-start is really found
3674 (if (and
3675 (zerop nest-count)
3676 (if (looking-at "is")
3677 (ada-search-ignore-string-comment ada-subprog-start-re t)
3678 (looking-at "declare\\|generic")))
3680 (if noerror nil
3681 (error "No matching proc/func/task/declare/package/protected")))
3684 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3685 "Move point to the beginning of a block-start.
3686 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3687 If NOERROR is non-nil, it only returns nil if no matching start was found.
3688 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3689 (let ((nest-count (if nest-level nest-level 0))
3690 (found nil)
3692 (last-was-begin '())
3693 ;; List all keywords encountered while traversing
3694 ;; something like '("end" "end" "begin")
3695 ;; This is removed from the list when "package", "procedure",...
3696 ;; are seen. The goal is to find whether a package has an elaboration
3697 ;; part
3699 (pos nil))
3701 ;; search backward for interesting keywords
3702 (while (and
3703 (not found)
3704 (ada-search-ignore-string-comment ada-matching-start-re t))
3706 (unless (and (looking-at "\\<record\\>")
3707 (save-excursion
3708 (forward-word -1)
3709 (looking-at "\\<null\\>")))
3710 (progn
3711 ;; calculate nest-depth
3712 (cond
3713 ;; found block end => increase nest depth
3714 ((looking-at "end")
3715 (push nil last-was-begin)
3716 (setq nest-count (1+ nest-count)))
3718 ;; found loop/select/record/case/if => check if it starts or
3719 ;; ends a block
3720 ((looking-at "loop\\|select\\|record\\|case\\|if")
3721 (setq pos (point))
3722 (save-excursion
3723 ;; check if keyword follows 'end'
3724 (ada-goto-previous-word)
3725 (if (looking-at "\\<end\\>[ \t]*[^;]")
3726 (progn
3727 ;; it ends a block => increase nest depth
3728 (setq nest-count (1+ nest-count)
3729 pos (point))
3730 (push nil last-was-begin))
3732 ;; it starts a block => decrease nest depth
3733 (setq nest-count (1- nest-count))
3735 ;; Some nested "begin .. end" blocks with no "declare"?
3736 ;; => remove those entries
3737 (while (car last-was-begin)
3738 (setq last-was-begin (cdr (cdr last-was-begin))))
3740 (setq last-was-begin (cdr last-was-begin))
3742 (goto-char pos)
3745 ;; found package start => check if it really is a block
3746 ((looking-at "package")
3747 (save-excursion
3748 ;; ignore if this is just a renames statement
3749 (let ((current (point))
3750 (pos (ada-search-ignore-string-comment
3751 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3752 (if pos
3753 (goto-char (car pos))
3754 (error (concat
3755 "No matching 'is' or 'renames' for 'package' at"
3756 " line "
3757 (number-to-string (count-lines 1 (1+ current)))))))
3758 (unless (looking-at "renames")
3759 (progn
3760 (forward-word 1)
3761 (ada-goto-next-non-ws)
3762 ;; ignore it if it is only a declaration with 'new'
3763 ;; We could have package Foo is new ....
3764 ;; or package Foo is separate;
3765 ;; or package Foo is begin null; end Foo
3766 ;; for elaboration code (elaboration)
3767 (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3768 (not (car last-was-begin)))
3769 (setq nest-count (1- nest-count))))))
3771 (setq last-was-begin (cdr last-was-begin))
3773 ;; found task start => check if it has a body
3774 ((looking-at "task")
3775 (save-excursion
3776 (forward-word 1)
3777 (ada-goto-next-non-ws)
3778 (cond
3779 ((looking-at "\\<body\\>"))
3780 ((looking-at "\\<type\\>")
3781 ;; In that case, do nothing if there is a "is"
3782 (forward-word 2);; skip "type"
3783 (ada-goto-next-non-ws);; skip type name
3785 ;; Do nothing if we are simply looking at a simple
3786 ;; "task type name;" statement with no block
3787 (unless (looking-at ";")
3788 (progn
3789 ;; Skip the parameters
3790 (if (looking-at "(")
3791 (ada-search-ignore-string-comment ")" nil))
3792 (let ((tmp (ada-search-ignore-string-comment
3793 "\\<\\(is\\|;\\)\\>" nil)))
3794 (if tmp
3795 (progn
3796 (goto-char (car tmp))
3797 (if (looking-at "is")
3798 (setq nest-count (1- nest-count)))))))))
3800 ;; Check if that task declaration had a block attached to
3801 ;; it (i.e do nothing if we have just "task name;")
3802 (unless (progn (forward-word 1)
3803 (looking-at "[ \t]*;"))
3804 (setq nest-count (1- nest-count))))))
3805 (setq last-was-begin (cdr last-was-begin))
3808 ((looking-at "declare")
3809 ;; remove entry for begin and end (include nested begin..end
3810 ;; groups)
3811 (setq last-was-begin (cdr last-was-begin))
3812 (let ((count 1))
3813 (while (and (> count 0))
3814 (if (equal (car last-was-begin) t)
3815 (setq count (1+ count))
3816 (setq count (1- count)))
3817 (setq last-was-begin (cdr last-was-begin))
3820 ((looking-at "protected")
3821 ;; Ignore if this is just a declaration
3822 (save-excursion
3823 (let ((pos (ada-search-ignore-string-comment
3824 "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil)))
3825 (if pos
3826 (goto-char (car pos)))
3827 (if (looking-at "is")
3828 ;; remove entry for end
3829 (setq last-was-begin (cdr last-was-begin)))))
3830 (setq nest-count (1- nest-count)))
3832 ((or (looking-at "procedure")
3833 (looking-at "function"))
3834 ;; Ignore if this is just a declaration
3835 (save-excursion
3836 (let ((pos (ada-search-ignore-string-comment
3837 "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil)))
3838 (if pos
3839 (goto-char (car pos)))
3840 (if (looking-at "is")
3841 ;; remove entry for begin and end
3842 (setq last-was-begin (cdr (cdr last-was-begin))))))
3845 ;; all the other block starts
3847 (push (looking-at "begin") last-was-begin)
3848 (setq nest-count (1- nest-count)))
3852 ;; match is found, if nest-depth is zero
3853 (setq found (zerop nest-count))))) ; end of loop
3855 (if (bobp)
3856 (point)
3857 (if found
3859 ;; match found => is there anything else to do ?
3861 (progn
3862 (cond
3864 ;; found 'if' => skip to 'then', if it's on a separate line
3865 ;; and GOTOTHEN is non-nil
3867 ((and
3868 gotothen
3869 (looking-at "if")
3870 (save-excursion
3871 (ada-search-ignore-string-comment "then" nil nil nil
3872 'word-search-forward)
3873 (back-to-indentation)
3874 (looking-at "\\<then\\>")))
3875 (goto-char (match-beginning 0)))
3878 ;; found 'do' => skip back to 'accept' or 'return'
3880 ((looking-at "do")
3881 (unless (ada-search-ignore-string-comment
3882 "\\<accept\\|return\\>" t)
3883 (error "Missing 'accept' or 'return' in front of 'do'"))))
3884 (point))
3886 (if noerror
3888 (error "No matching start"))))))
3891 (defun ada-goto-matching-end (&optional nest-level noerror)
3892 "Move point to the end of a block.
3893 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3894 If NOERROR is non-nil, it only returns nil if no matching start found."
3895 (let ((nest-count (or nest-level 0))
3896 (regex (eval-when-compile
3897 (concat "\\<"
3898 (regexp-opt '("end" "loop" "select" "begin" "case"
3899 "if" "task" "package" "record" "do"
3900 "procedure" "function") t)
3901 "\\>")))
3902 found
3905 ;; First is used for subprograms: they are generally handled
3906 ;; recursively, but of course we do not want to do that the
3907 ;; first time (see comment below about subprograms)
3908 (first (not (looking-at "declare"))))
3910 ;; If we are already looking at one of the keywords, this shouldn't count
3911 ;; in the nesting loop below, so we just make sure we don't count it.
3912 ;; "declare" is a special case because we need to look after the "begin"
3913 ;; keyword
3914 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3915 (forward-char 1))
3918 ;; search forward for interesting keywords
3920 (while (and
3921 (not found)
3922 (ada-search-ignore-string-comment regex nil))
3925 ;; calculate nest-depth
3927 (backward-word 1)
3928 (cond
3929 ;; procedures and functions need to be processed recursively, in
3930 ;; case they are defined in a declare/begin block, as in:
3931 ;; declare -- NL 0 (nested level)
3932 ;; A : Boolean;
3933 ;; procedure B (C : D) is
3934 ;; begin -- NL 1
3935 ;; null;
3936 ;; end B; -- NL 0, and we would exit
3937 ;; begin
3938 ;; end; -- we should exit here
3939 ;; processing them recursively avoids the need for any special
3940 ;; handling.
3941 ;; Nothing should be done if we have only the specs or a
3942 ;; generic instantion.
3944 ((and (looking-at "\\<procedure\\|function\\>"))
3945 (if first
3946 (forward-word 1)
3948 (setq pos (point))
3949 (ada-search-ignore-string-comment "is\\|;")
3950 (if (= (char-before) ?s)
3951 (progn
3952 (ada-goto-next-non-ws)
3953 (unless (looking-at "\\<new\\>")
3954 (progn
3955 (goto-char pos)
3956 (ada-goto-matching-end 0 t)))))))
3958 ;; found block end => decrease nest depth
3959 ((looking-at "\\<end\\>")
3960 (setq nest-count (1- nest-count)
3961 found (<= nest-count 0))
3962 ;; skip the following keyword
3963 (if (progn
3964 (skip-chars-forward "end")
3965 (ada-goto-next-non-ws)
3966 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3967 (forward-word 1)))
3969 ;; found package start => check if it really starts a block, and is not
3970 ;; in fact a generic instantiation for instance
3971 ((looking-at "\\<package\\>")
3972 (ada-search-ignore-string-comment "is" nil nil nil
3973 'word-search-forward)
3974 (ada-goto-next-non-ws)
3975 ;; ignore and skip it if it is only a 'new' package
3976 (if (looking-at "\\<new\\>")
3977 (goto-char (match-end 0))
3978 (setq nest-count (1+ nest-count)
3979 found (<= nest-count 0))))
3981 ;; all the other block starts
3983 (if (not first)
3984 (setq nest-count (1+ nest-count)))
3985 (setq found (<= nest-count 0))
3986 (forward-word 1))) ; end of 'cond'
3988 (setq first nil))
3990 (if found
3992 (if noerror
3994 (error "No matching end")))
3998 (defun ada-search-ignore-string-comment
3999 (search-re &optional backward limit paramlists search-func)
4000 "Regexp-search for SEARCH-RE, ignoring comments, strings.
4001 Returns a cons cell of begin and end of match data or nil, if not found.
4002 If BACKWARD is non-nil, search backward; search forward otherwise.
4003 The search stops at pos LIMIT.
4004 If PARAMLISTS is nil, ignore parameter lists.
4005 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
4006 in case we are searching for a constant string.
4007 Point is moved at the beginning of the SEARCH-RE."
4008 (let (found
4009 begin
4011 parse-result)
4013 ;; FIXME: need to pass BACKWARD to search-func!
4014 (unless search-func
4015 (setq search-func (if backward 're-search-backward 're-search-forward)))
4018 ;; search until found or end-of-buffer
4019 ;; We have to test that we do not look further than limit
4021 (with-syntax-table ada-mode-symbol-syntax-table
4022 (while (and (not found)
4023 (or (not limit)
4024 (or (and backward (<= limit (point)))
4025 (>= limit (point))))
4026 (funcall search-func search-re limit 1))
4027 (setq begin (match-beginning 0))
4028 (setq end (match-end 0))
4029 (setq parse-result (parse-partial-sexp (point-at-bol) (point)))
4030 (cond
4032 ;; If inside a string, skip it (and the following comments)
4034 ((ada-in-string-p parse-result)
4035 (if (featurep 'xemacs)
4036 (search-backward "\"" nil t)
4037 (goto-char (nth 8 parse-result)))
4038 (unless backward (forward-sexp 1)))
4040 ;; If inside a comment, skip it (and the following comments)
4041 ;; There is a special code for comments at the end of the file
4043 ((ada-in-comment-p parse-result)
4044 (if (featurep 'xemacs)
4045 (progn
4046 (forward-line 1)
4047 (beginning-of-line)
4048 (forward-comment -1))
4049 (goto-char (nth 8 parse-result)))
4050 (unless backward
4051 ;; at the end of the file, it is not possible to skip a comment
4052 ;; so we just go at the end of the line
4053 (if (forward-comment 1)
4054 (progn
4055 (forward-comment 1000)
4056 (beginning-of-line))
4057 (end-of-line))))
4059 ;; directly in front of a comment => skip it, if searching forward
4061 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
4062 (unless backward (progn (forward-char -1) (forward-comment 1000))))
4065 ;; found a parameter-list but should ignore it => skip it
4067 ((and (not paramlists) (ada-in-paramlist-p))
4068 (if backward
4069 (search-backward "(" nil t)
4070 (search-forward ")" nil t)))
4072 ;; found what we were looking for
4075 (setq found t))))) ; end of loop
4077 (if found
4078 (cons begin end)
4079 nil)))
4081 ;; -------------------------------------------------------
4082 ;; -- Testing the position of the cursor
4083 ;; -------------------------------------------------------
4085 (defun ada-in-decl-p ()
4086 "Return t if point is inside a declarative part.
4087 Assumes point to be at the end of a statement."
4088 (or (ada-in-paramlist-p)
4089 (save-excursion
4090 (ada-goto-decl-start t))))
4093 (defun ada-looking-at-semi-or ()
4094 "Return t if looking at an 'or' following a semicolon."
4095 (save-excursion
4096 (and (looking-at "\\<or\\>")
4097 (progn
4098 (forward-word 1)
4099 (ada-goto-stmt-start)
4100 (looking-at "\\<or\\>")))))
4103 (defun ada-looking-at-semi-private ()
4104 "Return t if looking at the start of a private section in a package.
4105 Return nil if the private is part of the package name, as in
4106 'private package A is...' (this can only happen at top level)."
4107 (save-excursion
4108 (and (looking-at "\\<private\\>")
4109 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4111 ;; Make sure this is the start of a private section (ie after
4112 ;; a semicolon or just after the package declaration, but not
4113 ;; after a 'type ... is private' or 'is new ... with private'
4115 ;; Note that a 'private' statement at the beginning of the buffer
4116 ;; does not indicate a private section, since this is instead a
4117 ;; 'private procedure ...'
4118 (progn (forward-comment -1000)
4119 (and (not (bobp))
4120 (or (= (char-before) ?\;)
4121 (and (forward-word -3)
4122 (looking-at "\\<package\\>"))))))))
4125 (defun ada-in-paramlist-p ()
4126 "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
4127 (save-excursion
4128 (and
4129 (ada-search-ignore-string-comment "(\\|)" t nil t)
4130 ;; inside parentheses ?
4131 (= (char-after) ?\()
4133 ;; We could be looking at two things here:
4134 ;; operator definition: function "." (
4135 ;; subprogram definition: procedure .... (
4136 ;; Let's skip back over the first one
4137 (progn
4138 (skip-chars-backward " \t\n")
4139 (if (= (char-before) ?\")
4140 (backward-char 3)
4141 (backward-word 1))
4144 ;; and now over the second one
4145 (backward-word 1)
4147 ;; We should ignore the case when the reserved keyword is in a
4148 ;; comment (for instance, when we have:
4149 ;; -- .... package
4150 ;; Test (A)
4151 ;; we should return nil
4153 (not (ada-in-string-or-comment-p))
4155 ;; right keyword two words before parenthesis ?
4156 ;; Type is in this list because of discriminants
4157 ;; pragma is not, because the syntax is that of a subprogram call.
4158 (looking-at (eval-when-compile
4159 (concat "\\<\\("
4160 "procedure\\|function\\|body\\|"
4161 "task\\|entry\\|accept\\|"
4162 "access[ \t]+procedure\\|"
4163 "access[ \t]+function\\|"
4164 "type\\)\\>"))))))
4166 (defun ada-search-ignore-complex-boolean (regexp backwardp)
4167 "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
4168 If BACKWARDP is non-nil, search backward; search forward otherwise."
4169 (let (result)
4170 (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
4171 (save-excursion (forward-word -1)
4172 (looking-at "and then\\|or else"))))
4173 result))
4175 (defun ada-in-open-paren-p ()
4176 "Non-nil if in an open parenthesis.
4177 Return value is the position of the first non-ws behind the last unclosed
4178 parenthesis, or nil."
4179 (save-excursion
4180 (let ((parse (parse-partial-sexp
4181 (point)
4182 (or (car (ada-search-ignore-complex-boolean
4183 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4185 (point-min)))))
4187 (if (nth 1 parse)
4188 (progn
4189 (goto-char (1+ (nth 1 parse)))
4191 ;; Skip blanks, if they are not followed by a comment
4192 ;; See:
4193 ;; type A is ( Value_0,
4194 ;; Value_1);
4195 ;; type B is ( -- comment
4196 ;; Value_2);
4198 (if (or (not ada-indent-handle-comment-special)
4199 (not (looking-at "[ \t]+--")))
4200 (skip-chars-forward " \t"))
4202 (point))))))
4205 ;; -----------------------------------------------------------
4206 ;; -- Behavior Of TAB Key
4207 ;; -----------------------------------------------------------
4209 (defun ada-tab ()
4210 "Do indenting or tabbing according to `ada-tab-policy'.
4211 In Transient Mark mode, if the mark is active, operate on the contents
4212 of the region. Otherwise, operate only on the current line."
4213 (interactive)
4214 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
4215 ((eq ada-tab-policy 'indent-auto)
4216 (if (ada-region-selected)
4217 (ada-indent-region (region-beginning) (region-end))
4218 (ada-indent-current)))
4219 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4222 (defun ada-untab (_arg)
4223 "Delete leading indenting according to `ada-tab-policy'."
4224 ;; FIXME: ARG is ignored
4225 (interactive "P")
4226 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
4227 ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
4228 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4231 (defun ada-indent-current-function ()
4232 "Ada mode version of the `indent-line-function'."
4233 (interactive "*")
4234 (let ((starting-point (point-marker)))
4235 (beginning-of-line)
4236 (ada-tab)
4237 (if (< (point) starting-point)
4238 (goto-char starting-point))
4239 (set-marker starting-point nil)
4242 (defun ada-tab-hard ()
4243 "Indent current line to next tab stop."
4244 (interactive)
4245 (save-excursion
4246 (beginning-of-line)
4247 (insert-char ? ada-indent))
4248 (if (bolp) (forward-char ada-indent)))
4250 (defun ada-untab-hard ()
4251 "Indent current line to previous tab stop."
4252 (interactive)
4253 (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent)))
4256 ;; ------------------------------------------------------------
4257 ;; -- Miscellaneous
4258 ;; ------------------------------------------------------------
4260 ;; Not needed any more for Emacs 21.2, but still needed for backward
4261 ;; compatibility
4262 (defun ada-remove-trailing-spaces ()
4263 "Remove trailing spaces in the whole buffer."
4264 (interactive)
4265 (save-match-data
4266 (save-excursion
4267 (save-restriction
4268 (widen)
4269 (goto-char (point-min))
4270 (while (re-search-forward "[ \t]+$" (point-max) t)
4271 (replace-match "" nil nil))))))
4273 (defun ada-gnat-style ()
4274 "Clean up comments, `(' and `,' for GNAT style checking switch."
4275 (interactive)
4276 (save-excursion
4278 ;; The \n is required, or the line after an empty comment line is
4279 ;; simply ignored.
4280 (goto-char (point-min))
4281 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
4282 (replace-match "-- \\1")
4283 (forward-line 1)
4284 (beginning-of-line))
4286 (goto-char (point-min))
4287 (while (re-search-forward "\\>(" nil t)
4288 (if (not (ada-in-string-or-comment-p))
4289 (replace-match " (")))
4290 (goto-char (point-min))
4291 (while (re-search-forward ";--" nil t)
4292 (forward-char -1)
4293 (if (not (ada-in-string-or-comment-p))
4294 (replace-match "; --")))
4295 (goto-char (point-min))
4296 (while (re-search-forward "([ \t]+" nil t)
4297 (if (not (ada-in-string-or-comment-p))
4298 (replace-match "(")))
4299 (goto-char (point-min))
4300 (while (re-search-forward ")[ \t]+)" nil t)
4301 (if (not (ada-in-string-or-comment-p))
4302 (replace-match "))")))
4303 (goto-char (point-min))
4304 (while (re-search-forward "\\>:" nil t)
4305 (if (not (ada-in-string-or-comment-p))
4306 (replace-match " :")))
4308 ;; Make sure there is a space after a ','.
4309 ;; Always go back to the beginning of the match, since otherwise
4310 ;; a statement like ('F','D','E') is incorrectly modified.
4311 (goto-char (point-min))
4312 (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
4313 (if (not (save-excursion
4314 (goto-char (match-beginning 0))
4315 (ada-in-string-or-comment-p)))
4316 (replace-match ", \\1")))
4318 ;; Operators should be surrounded by spaces.
4319 (goto-char (point-min))
4320 (while (re-search-forward
4321 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4322 nil t)
4323 (goto-char (match-beginning 1))
4324 (if (or (looking-at "--")
4325 (ada-in-string-or-comment-p))
4326 (progn
4327 (forward-line 1)
4328 (beginning-of-line))
4329 (cond
4330 ((string= (match-string 1) "/=")
4331 (replace-match " /= "))
4332 ((string= (match-string 1) "..")
4333 (replace-match " .. "))
4334 ((string= (match-string 1) "**")
4335 (replace-match " ** "))
4336 ((string= (match-string 1) ":=")
4337 (replace-match " := "))
4339 (replace-match " \\1 ")))
4340 (forward-char 1)))
4345 ;; -------------------------------------------------------------
4346 ;; -- Moving To Procedures/Packages/Statements
4347 ;; -------------------------------------------------------------
4349 (defun ada-move-to-start ()
4350 "Move point to the matching start of the current Ada structure."
4351 (interactive)
4352 (let ((pos (point)))
4353 (with-syntax-table ada-mode-symbol-syntax-table
4355 (save-excursion
4357 ;; do nothing if in string or comment or not on 'end ...;'
4358 ;; or if an error occurs during processing
4361 (ada-in-string-or-comment-p)
4362 (and (progn
4363 (or (looking-at "[ \t]*\\<end\\>")
4364 (backward-word 1))
4365 (or (looking-at "[ \t]*\\<end\\>")
4366 (backward-word 1))
4367 (or (looking-at "[ \t]*\\<end\\>")
4368 (error "Not on end ...;")))
4369 (ada-goto-matching-start 1)
4370 (setq pos (point))
4373 ;; on 'begin' => go on, according to user option
4375 ada-move-to-declaration
4376 (looking-at "\\<begin\\>")
4377 (ada-goto-decl-start)
4378 (setq pos (point))))
4380 ) ; end of save-excursion
4382 ;; now really move to the found position
4383 (goto-char pos))))
4385 (defun ada-move-to-end ()
4386 "Move point to the end of the block around point.
4387 Moves to 'begin' if in a declarative part."
4388 (interactive)
4389 (let ((pos (point))
4390 decl-start)
4391 (with-syntax-table ada-mode-symbol-syntax-table
4393 (save-excursion
4395 (cond
4396 ;; Go to the beginning of the current word, and check if we are
4397 ;; directly on 'begin'
4398 ((save-excursion
4399 (skip-syntax-backward "w")
4400 (looking-at "\\<begin\\>"))
4401 (ada-goto-matching-end 1))
4403 ;; on first line of subprogram body
4404 ;; Do nothing for specs or generic instantion, since these are
4405 ;; handled as the general case (find the enclosing block)
4406 ;; We also need to make sure that we ignore nested subprograms
4407 ((save-excursion
4408 (and (skip-syntax-backward "w")
4409 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4410 (ada-search-ignore-string-comment "is\\|;")
4411 (not (= (char-before) ?\;))
4413 (skip-syntax-backward "w")
4414 (ada-goto-matching-end 0 t))
4416 ;; on first line of task declaration
4417 ((save-excursion
4418 (and (ada-goto-stmt-start)
4419 (looking-at "\\<task\\>" )
4420 (forward-word 1)
4421 (ada-goto-next-non-ws)
4422 (looking-at "\\<body\\>")))
4423 (ada-search-ignore-string-comment "begin" nil nil nil
4424 'word-search-forward))
4425 ;; accept block start
4426 ((save-excursion
4427 (and (ada-goto-stmt-start)
4428 (looking-at "\\<accept\\>" )))
4429 (ada-goto-matching-end 0))
4430 ;; package start
4431 ((save-excursion
4432 (setq decl-start (and (ada-goto-decl-start t) (point)))
4433 (and decl-start (looking-at "\\<package\\>")))
4434 (ada-goto-matching-end 1))
4436 ;; On a "declare" keyword
4437 ((save-excursion
4438 (skip-syntax-backward "w")
4439 (looking-at "\\<declare\\>"))
4440 (ada-goto-matching-end 0 t))
4442 ;; inside a 'begin' ... 'end' block
4443 (decl-start
4444 (goto-char decl-start)
4445 (ada-goto-matching-end 0 t))
4447 ;; (hopefully ;-) everything else
4449 (ada-goto-matching-end 1)))
4450 (setq pos (point))
4453 ;; now really move to the position found
4454 (goto-char pos))))
4456 (defun ada-next-procedure ()
4457 "Move point to next procedure."
4458 (interactive)
4459 (end-of-line)
4460 (if (re-search-forward ada-procedure-start-regexp nil t)
4461 (goto-char (match-beginning 4))
4462 (error "No more functions/procedures/tasks")))
4464 (defun ada-previous-procedure ()
4465 "Move point to previous procedure."
4466 (interactive)
4467 (beginning-of-line)
4468 (if (re-search-backward ada-procedure-start-regexp nil t)
4469 (goto-char (match-beginning 4))
4470 (error "No more functions/procedures/tasks")))
4472 (defun ada-next-package ()
4473 "Move point to next package."
4474 (interactive)
4475 (end-of-line)
4476 (if (re-search-forward ada-package-start-regexp nil t)
4477 (goto-char (match-beginning 1))
4478 (error "No more packages")))
4480 (defun ada-previous-package ()
4481 "Move point to previous package."
4482 (interactive)
4483 (beginning-of-line)
4484 (if (re-search-backward ada-package-start-regexp nil t)
4485 (goto-char (match-beginning 1))
4486 (error "No more packages")))
4489 ;; ------------------------------------------------------------
4490 ;; -- Define keymap and menus for Ada
4491 ;; -------------------------------------------------------------
4493 (defun ada-create-keymap ()
4494 "Create the keymap associated with the Ada mode."
4496 ;; All non-standard keys go into ada-mode-extra-map
4497 (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map)
4499 ;; Indentation and Formatting
4500 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
4501 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
4502 (define-key ada-mode-map "\t" 'ada-tab)
4503 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
4504 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
4505 (define-key ada-mode-map [(shift tab)] 'ada-untab)
4506 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
4507 ;; We don't want to make meta-characters case-specific.
4509 ;; Movement
4510 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
4511 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
4512 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
4513 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
4515 ;; Compilation
4516 (unless (lookup-key ada-mode-map "\C-c\C-c")
4517 (define-key ada-mode-map "\C-c\C-c" 'compile))
4519 ;; Casing
4520 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
4521 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
4522 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
4523 (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
4525 ;; On XEmacs, you can easily specify whether DEL should deletes
4526 ;; one character forward or one character backward. Take this into
4527 ;; account
4528 (define-key ada-mode-map
4529 (if (boundp 'delete-key-deletes-forward) [backspace] "\177")
4530 'backward-delete-char-untabify)
4532 ;; Make body
4533 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4535 ;; Use predefined function of Emacs19 for comments (RE)
4536 (define-key ada-mode-map "\C-c;" 'comment-region)
4537 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4539 ;; The following keys are bound to functions defined in ada-xref.el or
4540 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4541 ;; and activated only if the right compiler is used
4543 (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
4544 'ada-point-and-xref)
4545 (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
4547 (define-key ada-mode-extra-map "o" 'ff-find-other-file)
4548 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4549 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4550 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4551 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4552 (define-key ada-mode-extra-map "c" 'ada-change-prj)
4553 (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
4554 (define-key ada-mode-extra-map "g" 'ada-gdb-application)
4555 (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
4556 (define-key ada-mode-extra-map "r" 'ada-run-application)
4557 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4558 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4559 (define-key ada-mode-extra-map "l" 'ada-find-local-references)
4560 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4561 (define-key ada-mode-extra-map "f" 'ada-find-file)
4563 (define-key ada-mode-extra-map "u" 'ada-prj-edit)
4565 (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
4567 ;; The templates, defined in ada-stmt.el
4569 (let ((map (make-sparse-keymap)))
4570 (define-key map "h" 'ada-header)
4571 (define-key map "\C-a" 'ada-array)
4572 (define-key map "b" 'ada-exception-block)
4573 (define-key map "d" 'ada-declare-block)
4574 (define-key map "c" 'ada-case)
4575 (define-key map "\C-e" 'ada-elsif)
4576 (define-key map "e" 'ada-else)
4577 (define-key map "\C-k" 'ada-package-spec)
4578 (define-key map "k" 'ada-package-body)
4579 (define-key map "\C-p" 'ada-procedure-spec)
4580 (define-key map "p" 'ada-subprogram-body)
4581 (define-key map "\C-f" 'ada-function-spec)
4582 (define-key map "f" 'ada-for-loop)
4583 (define-key map "i" 'ada-if)
4584 (define-key map "l" 'ada-loop)
4585 (define-key map "\C-r" 'ada-record)
4586 (define-key map "\C-s" 'ada-subtype)
4587 (define-key map "S" 'ada-tabsize)
4588 (define-key map "\C-t" 'ada-task-spec)
4589 (define-key map "t" 'ada-task-body)
4590 (define-key map "\C-y" 'ada-type)
4591 (define-key map "\C-v" 'ada-private)
4592 (define-key map "u" 'ada-use)
4593 (define-key map "\C-u" 'ada-with)
4594 (define-key map "\C-w" 'ada-when)
4595 (define-key map "w" 'ada-while-loop)
4596 (define-key map "\C-x" 'ada-exception)
4597 (define-key map "x" 'ada-exit)
4598 (define-key ada-mode-extra-map "t" map))
4602 (defun ada-create-menu ()
4603 "Create the Ada menu as shown in the menu bar."
4604 (let ((m '("Ada"
4605 ("Help"
4606 ["Ada Mode" (info "ada-mode") t]
4607 ["GNAT User's Guide" (info "gnat_ugn")
4608 (eq ada-which-compiler 'gnat)]
4609 ["GNAT Reference Manual" (info "gnat_rm")
4610 (eq ada-which-compiler 'gnat)]
4611 ["Gcc Documentation" (info "gcc")
4612 (eq ada-which-compiler 'gnat)]
4613 ["Gdb Documentation" (info "gdb")
4614 (eq ada-which-compiler 'gnat)]
4615 ["Ada95 Reference Manual" (info "arm95") t])
4616 ("Options" :included (derived-mode-p 'ada-mode)
4617 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4618 :style toggle :selected ada-auto-case]
4619 ["Auto Indent After Return"
4620 (setq ada-indent-after-return (not ada-indent-after-return))
4621 :style toggle :selected ada-indent-after-return]
4622 ["Automatically Recompile For Cross-references"
4623 (setq ada-xref-create-ali (not ada-xref-create-ali))
4624 :style toggle :selected ada-xref-create-ali
4625 :included (eq ada-which-compiler 'gnat)]
4626 ["Confirm Commands"
4627 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4628 :style toggle :selected ada-xref-confirm-compile
4629 :included (eq ada-which-compiler 'gnat)]
4630 ["Show Cross-references In Other Buffer"
4631 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4632 :style toggle :selected ada-xref-other-buffer
4633 :included (eq ada-which-compiler 'gnat)]
4634 ["Tight Integration With GNU Visual Debugger"
4635 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4636 :style toggle :selected ada-tight-gvd-integration
4637 :included (string-match "gvd" ada-prj-default-debugger)])
4638 ["Customize" (customize-group 'ada)
4639 :included (fboundp 'customize-group)]
4640 ["Check file" ada-check-current t]
4641 ["Compile file" ada-compile-current t]
4642 ["Set main and Build" ada-set-main-compile-application t]
4643 ["Show main" ada-show-current-main t]
4644 ["Build" ada-compile-application t]
4645 ["Run" ada-run-application t]
4646 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4647 ["------" nil nil]
4648 ("Project"
4649 ["Show project" ada-show-current-project t]
4650 ["Load..." ada-set-default-project-file t]
4651 ["New..." ada-prj-new t]
4652 ["Edit..." ada-prj-edit t])
4653 ("Goto" :included (derived-mode-p 'ada-mode)
4654 ["Goto Declaration/Body" ada-goto-declaration
4655 (eq ada-which-compiler 'gnat)]
4656 ["Goto Body" ada-goto-body
4657 (eq ada-which-compiler 'gnat)]
4658 ["Goto Declaration Other Frame"
4659 ada-goto-declaration-other-frame
4660 (eq ada-which-compiler 'gnat)]
4661 ["Goto Previous Reference" ada-xref-goto-previous-reference
4662 (eq ada-which-compiler 'gnat)]
4663 ["List Local References" ada-find-local-references
4664 (eq ada-which-compiler 'gnat)]
4665 ["List References" ada-find-references
4666 (eq ada-which-compiler 'gnat)]
4667 ["Goto Reference To Any Entity" ada-find-any-references
4668 (eq ada-which-compiler 'gnat)]
4669 ["Goto Parent Unit" ada-goto-parent
4670 (eq ada-which-compiler 'gnat)]
4671 ["--" nil nil]
4672 ["Next compilation error" next-error t]
4673 ["Previous Package" ada-previous-package t]
4674 ["Next Package" ada-next-package t]
4675 ["Previous Procedure" ada-previous-procedure t]
4676 ["Next Procedure" ada-next-procedure t]
4677 ["Goto Start Of Statement" ada-move-to-start t]
4678 ["Goto End Of Statement" ada-move-to-end t]
4679 ["-" nil nil]
4680 ["Other File" ff-find-other-file t]
4681 ["Other File Other Window" ada-ff-other-window t])
4682 ("Edit" :included (derived-mode-p 'ada-mode)
4683 ["Search File On Source Path" ada-find-file t]
4684 ["------" nil nil]
4685 ["Complete Identifier" ada-complete-identifier t]
4686 ["-----" nil nil]
4687 ["Indent Line" ada-indent-current-function t]
4688 ["Justify Current Indentation" ada-justified-indent-current t]
4689 ["Indent Lines in Selection" ada-indent-region t]
4690 ["Indent Lines in File"
4691 (ada-indent-region (point-min) (point-max)) t]
4692 ["Format Parameter List" ada-format-paramlist t]
4693 ["-" nil nil]
4694 ["Comment Selection" comment-region t]
4695 ["Uncomment Selection" ada-uncomment-region t]
4696 ["--" nil nil]
4697 ["Fill Comment Paragraph" fill-paragraph t]
4698 ["Fill Comment Paragraph Justify"
4699 ada-fill-comment-paragraph-justify t]
4700 ["Fill Comment Paragraph Postfix"
4701 ada-fill-comment-paragraph-postfix t]
4702 ["---" nil nil]
4703 ["Adjust Case Selection" ada-adjust-case-region t]
4704 ["Adjust Case in File" ada-adjust-case-buffer t]
4705 ["Create Case Exception" ada-create-case-exception t]
4706 ["Create Case Exception Substring"
4707 ada-create-case-exception-substring t]
4708 ["Reload Case Exceptions" ada-case-read-exceptions t]
4709 ["----" nil nil]
4710 ["Make body for subprogram" ada-make-subprogram-body t]
4711 ["-----" nil nil]
4712 ["Narrow to subprogram" ada-narrow-to-defun t])
4713 ("Templates"
4714 :included (derived-mode-p 'ada-mode)
4715 ["Header" ada-header t]
4716 ["-" nil nil]
4717 ["Package Body" ada-package-body t]
4718 ["Package Spec" ada-package-spec t]
4719 ["Function Spec" ada-function-spec t]
4720 ["Procedure Spec" ada-procedure-spec t]
4721 ["Proc/func Body" ada-subprogram-body t]
4722 ["Task Body" ada-task-body t]
4723 ["Task Spec" ada-task-spec t]
4724 ["Declare Block" ada-declare-block t]
4725 ["Exception Block" ada-exception-block t]
4726 ["--" nil nil]
4727 ["Entry" ada-entry t]
4728 ["Entry family" ada-entry-family t]
4729 ["Select" ada-select t]
4730 ["Accept" ada-accept t]
4731 ["Or accept" ada-or-accep t]
4732 ["Or delay" ada-or-delay t]
4733 ["Or terminate" ada-or-terminate t]
4734 ["---" nil nil]
4735 ["Type" ada-type t]
4736 ["Private" ada-private t]
4737 ["Subtype" ada-subtype t]
4738 ["Record" ada-record t]
4739 ["Array" ada-array t]
4740 ["----" nil nil]
4741 ["If" ada-if t]
4742 ["Else" ada-else t]
4743 ["Elsif" ada-elsif t]
4744 ["Case" ada-case t]
4745 ["-----" nil nil]
4746 ["While Loop" ada-while-loop t]
4747 ["For Loop" ada-for-loop t]
4748 ["Loop" ada-loop t]
4749 ["------" nil nil]
4750 ["Exception" ada-exception t]
4751 ["Exit" ada-exit t]
4752 ["When" ada-when t])
4755 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4756 (if (featurep 'xemacs)
4757 (progn
4758 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4759 (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4762 ;; -------------------------------------------------------
4763 ;; Commenting/Uncommenting code
4764 ;; The following two calls are provided to enhance the standard
4765 ;; comment-region function, which only allows uncommenting if the
4766 ;; comment is at the beginning of a line. If the line have been re-indented,
4767 ;; we are unable to use comment-region, which makes no sense.
4769 ;; In addition, we provide an interface to the standard comment handling
4770 ;; function for justifying the comments.
4771 ;; -------------------------------------------------------
4773 (defadvice comment-region (before ada-uncomment-anywhere disable)
4774 (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
4775 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4776 (derived-mode-p 'ada-mode))
4777 (save-excursion
4778 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4779 (goto-char beg)
4780 (while (re-search-forward cs end t)
4781 (replace-match comment-start))
4782 ))))
4784 (defun ada-uncomment-region (beg end &optional arg)
4785 "Uncomment region BEG .. END.
4786 ARG gives number of comment characters."
4787 (interactive "r\nP")
4789 ;; This advice is not needed anymore with Emacs21. However, for older
4790 ;; versions, as well as for XEmacs, we still need to enable it.
4791 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4792 (progn
4793 (ad-activate 'comment-region)
4794 (comment-region beg end (- (or arg 2)))
4795 (ad-deactivate 'comment-region))
4796 (comment-region beg end (list (- (or arg 2))))
4797 (ada-indent-region beg end)))
4799 (defun ada-fill-comment-paragraph-justify ()
4800 "Fill current comment paragraph and justify each line as well."
4801 (interactive)
4802 (ada-fill-comment-paragraph 'full))
4804 (defun ada-fill-comment-paragraph-postfix ()
4805 "Fill current comment paragraph and justify each line as well.
4806 Adds `ada-fill-comment-postfix' at the end of each line."
4807 (interactive)
4808 (ada-fill-comment-paragraph 'full t))
4810 (defun ada-fill-comment-paragraph (&optional justify postfix)
4811 "Fill the current comment paragraph.
4812 If JUSTIFY is non-nil, each line is justified as well.
4813 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4814 to each line filled and justified.
4815 The paragraph is indented on the first line."
4816 (interactive "P")
4818 ;; check if inside comment or just in front a comment
4819 (if (and (not (ada-in-comment-p))
4820 (not (looking-at "[ \t]*--")))
4821 (error "Not inside comment"))
4823 (let* (indent from to
4824 (opos (point-marker))
4826 ;; Sets this variable to nil, otherwise it prevents
4827 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4828 (parse-sexp-lookup-properties nil)
4830 fill-prefix
4831 (fill-column (current-fill-column)))
4833 ;; Find end of paragraph
4834 (back-to-indentation)
4835 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4836 (forward-line 1)
4838 ;; If we were at the last line in the buffer, create a dummy empty
4839 ;; line at the end of the buffer.
4840 (if (eobp)
4841 (insert "\n")
4842 (back-to-indentation)))
4843 (beginning-of-line)
4844 (setq to (point-marker))
4845 (goto-char opos)
4847 ;; Find beginning of paragraph
4848 (back-to-indentation)
4849 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4850 (forward-line -1)
4851 (back-to-indentation))
4853 ;; We want one line above the first one, unless we are at the beginning
4854 ;; of the buffer
4855 (unless (bobp)
4856 (forward-line 1))
4857 (beginning-of-line)
4858 (setq from (point-marker))
4860 ;; Calculate the indentation we will need for the paragraph
4861 (back-to-indentation)
4862 (setq indent (current-column))
4863 ;; unindent the first line of the paragraph
4864 (delete-region from (point))
4866 ;; Remove the old postfixes
4867 (goto-char from)
4868 (while (re-search-forward "--\n" to t)
4869 (replace-match "\n"))
4871 (goto-char (1- to))
4872 (setq to (point-marker))
4874 ;; Indent and justify the paragraph
4875 (setq fill-prefix ada-fill-comment-prefix)
4876 (set-left-margin from to indent)
4877 (if postfix
4878 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4880 (fill-region-as-paragraph from to justify)
4882 ;; Add the postfixes if required
4883 (if postfix
4884 (save-restriction
4885 (goto-char from)
4886 (narrow-to-region from to)
4887 (while (not (eobp))
4888 (end-of-line)
4889 (insert-char ? (- fill-column (current-column)))
4890 (insert ada-fill-comment-postfix)
4891 (forward-line))
4894 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4895 ;; inserted at the end. Delete it
4896 (if (or (featurep 'xemacs)
4897 (<= emacs-major-version 19)
4898 (and (= emacs-major-version 20)
4899 (<= emacs-minor-version 2)))
4900 (progn
4901 (goto-char to)
4902 (end-of-line)
4903 (delete-char 1)))
4905 (goto-char opos)))
4908 ;; ---------------------------------------------------
4909 ;; support for find-file.el
4910 ;; These functions are used by find-file to guess the file names from
4911 ;; unit names, and to find the other file (spec or body) from the current
4912 ;; file (body or spec).
4913 ;; It is also used to find in which function we are, so as to put the
4914 ;; cursor at the correct position.
4915 ;; Standard Ada does not force any relation between unit names and file names,
4916 ;; so some of these functions can only be a good approximation. However, they
4917 ;; are also overridden in `ada-xref'.el when we know that the user is using
4918 ;; GNAT.
4919 ;; ---------------------------------------------------
4921 ;; Overridden when we work with GNAT, to use gnatkrunch
4922 (defun ada-make-filename-from-adaname (adaname)
4923 "Determine the filename in which ADANAME is found.
4924 This matches the GNAT default naming convention, except for
4925 pre-defined units."
4926 (while (string-match "\\." adaname)
4927 (setq adaname (replace-match "-" t t adaname)))
4928 (downcase adaname)
4931 (defun ada-other-file-name ()
4932 "Return the name of the other file.
4933 The name returned is the body if `current-buffer' is the spec,
4934 or the spec otherwise."
4936 (let ((is-spec nil)
4937 (is-body nil)
4938 (suffixes ada-spec-suffixes)
4939 (name (buffer-file-name)))
4941 ;; Guess whether we have a spec or a body, and get the basename of the
4942 ;; file. Since the extension may not start with '.', we can not use
4943 ;; file-name-extension
4944 (while (and (not is-spec)
4945 suffixes)
4946 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4947 (setq is-spec t
4948 name (match-string 1 name)))
4949 (setq suffixes (cdr suffixes)))
4951 (if (not is-spec)
4952 (progn
4953 (setq suffixes ada-body-suffixes)
4954 (while (and (not is-body)
4955 suffixes)
4956 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4957 (setq is-body t
4958 name (match-string 1 name)))
4959 (setq suffixes (cdr suffixes)))))
4961 ;; If this wasn't in either list, return name itself
4962 (if (not (or is-spec is-body))
4963 name
4965 ;; Else find the other possible names
4966 (if is-spec
4967 (setq suffixes ada-body-suffixes)
4968 (setq suffixes ada-spec-suffixes))
4969 (setq is-spec name)
4971 (while suffixes
4973 ;; If we are using project file, search for the other file in all
4974 ;; the possible src directories.
4976 (if (fboundp 'ada-find-src-file-in-dir)
4977 (let ((other
4978 (ada-find-src-file-in-dir
4979 (file-name-nondirectory (concat name (car suffixes))))))
4980 (if other
4981 (setq is-spec other)))
4983 ;; Else search in the current directory
4984 (if (file-exists-p (concat name (car suffixes)))
4985 (setq is-spec (concat name (car suffixes)))))
4986 (setq suffixes (cdr suffixes)))
4988 is-spec)))
4990 (defun ada-which-function-are-we-in ()
4991 "Return the name of the function whose definition/declaration point is in.
4992 Used in `ff-pre-load-hook'."
4993 (setq ff-function-name nil)
4994 (save-excursion
4995 (end-of-line);; make sure we get the complete name
4996 (or (if (re-search-backward ada-procedure-start-regexp nil t)
4997 (setq ff-function-name (match-string 5)))
4998 (if (re-search-backward ada-package-start-regexp nil t)
4999 (setq ff-function-name (match-string 4))))
5003 (defvar ada-last-which-function-line -1
5004 "Last line on which `ada-which-function' was called.")
5005 (defvar ada-last-which-function-subprog 0
5006 "Last subprogram name returned by `ada-which-function'.")
5007 (make-variable-buffer-local 'ada-last-which-function-subprog)
5008 (make-variable-buffer-local 'ada-last-which-function-line)
5011 (defun ada-which-function ()
5012 "Return the name of the function whose body the point is in.
5013 This function works even in the case of nested subprograms, whereas the
5014 standard Emacs function `which-function' does not.
5015 Since the search can be long, the results are cached."
5017 (let ((line (count-lines 1 (point)))
5018 (pos (point))
5019 end-pos
5020 func-name indent
5021 found)
5023 ;; If this is the same line as before, simply return the same result
5024 (if (= line ada-last-which-function-line)
5025 ada-last-which-function-subprog
5027 (save-excursion
5028 ;; In case the current line is also the beginning of the body
5029 (end-of-line)
5031 ;; Are we looking at "function Foo\n (paramlist)"
5032 (skip-chars-forward " \t\n(")
5034 (condition-case nil
5035 (up-list 1)
5036 (error nil))
5038 (skip-chars-forward " \t\n")
5039 (if (looking-at "return")
5040 (progn
5041 (forward-word 1)
5042 (skip-chars-forward " \t\n")
5043 (skip-chars-forward "a-zA-Z0-9_'")))
5045 ;; Can't simply do forward-word, in case the "is" is not on the
5046 ;; same line as the closing parenthesis
5047 (skip-chars-forward "is \t\n")
5049 ;; No look for the closest subprogram body that has not ended yet.
5050 ;; Not that we expect all the bodies to be finished by "end <name>",
5051 ;; or a simple "end;" indented in the same column as the start of
5052 ;; the subprogram. The goal is to be as efficient as possible.
5054 (while (and (not found)
5055 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5057 ;; Get the function name, but not the properties, or this changes
5058 ;; the face in the modeline on Emacs 21
5059 (setq func-name (match-string-no-properties 3))
5060 (if (and (not (ada-in-comment-p))
5061 (not (save-excursion
5062 (goto-char (match-end 0))
5063 (looking-at "[ \t\n]*new"))))
5064 (save-excursion
5065 (back-to-indentation)
5066 (setq indent (current-column))
5067 (if (ada-search-ignore-string-comment
5068 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5069 (make-string indent ? ) "end;"))
5070 (setq end-pos (point))
5071 (setq end-pos (point-max)))
5072 (if (>= end-pos pos)
5073 (setq found func-name))))
5075 (setq ada-last-which-function-line line
5076 ada-last-which-function-subprog found)
5077 found))))
5079 (defun ada-ff-other-window ()
5080 "Find other file in other window using `ff-find-other-file'."
5081 (interactive)
5082 (and (fboundp 'ff-find-other-file)
5083 (ff-find-other-file t)))
5085 (defun ada-set-point-accordingly ()
5086 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5087 (if ff-function-name
5088 (progn
5089 (goto-char (point-min))
5090 (unless (ada-search-ignore-string-comment
5091 (concat ff-function-name "\\b") nil)
5092 (goto-char (point-min))))))
5094 (defun ada-get-body-name (&optional spec-name)
5095 "Return the file name for the body of SPEC-NAME.
5096 If SPEC-NAME is nil, return the body for the current package.
5097 Return nil if no body was found."
5098 (interactive)
5100 (unless spec-name (setq spec-name (buffer-file-name)))
5102 ;; Remove the spec extension. We can not simply remove the file extension,
5103 ;; but we need to take into account the specific non-GNAT extensions that the
5104 ;; user might have specified.
5106 (let ((suffixes ada-spec-suffixes)
5107 end)
5108 (while suffixes
5109 (setq end (- (length spec-name) (length (car suffixes))))
5110 (if (string-equal (car suffixes) (substring spec-name end))
5111 (setq spec-name (substring spec-name 0 end)))
5112 (setq suffixes (cdr suffixes))))
5114 ;; If find-file.el was available, use its functions
5115 (if (fboundp 'ff-get-file-name)
5116 (ff-get-file-name ada-search-directories-internal
5117 (ada-make-filename-from-adaname
5118 (file-name-nondirectory
5119 (file-name-sans-extension spec-name)))
5120 ada-body-suffixes)
5121 ;; Else emulate it very simply
5122 (concat (ada-make-filename-from-adaname
5123 (file-name-nondirectory
5124 (file-name-sans-extension spec-name)))
5125 ".adb")))
5128 ;; ---------------------------------------------------
5129 ;; support for font-lock.el
5130 ;; Strings are a real pain in Ada because a single quote character is
5131 ;; overloaded as a string quote and type/instance delimiter. By default, a
5132 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5133 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5134 ;; syntax when the gods that created Ada determine them to be.
5136 ;; This only works in Emacs. See the comments before the grammar functions
5137 ;; at the beginning of this file for how this is done with XEmacs.
5138 ;; ----------------------------------------------------
5140 (defconst ada-font-lock-syntactic-keywords
5141 ;; Mark single quotes as having string quote syntax in 'c' instances.
5142 ;; We used to explicitly avoid ''' as a special case for fear the buffer
5143 ;; be highlighted as a string, but it seems this fear is unfounded.
5145 ;; This sets the properties of the characters, so that ada-in-string-p
5146 ;; correctly handles '"' too...
5147 '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5148 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5150 (defvar ada-font-lock-keywords
5151 (eval-when-compile
5152 (list
5154 ;; handle "type T is access function return S;"
5155 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5157 ;; preprocessor line
5158 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5161 ;; accept, entry, function, package (body), protected (body|type),
5162 ;; pragma, procedure, task (body) plus name.
5163 (list (concat
5164 "\\<\\("
5165 "accept\\|"
5166 "entry\\|"
5167 "function\\|"
5168 "package[ \t]+body\\|"
5169 "package\\|"
5170 "pragma\\|"
5171 "procedure\\|"
5172 "protected[ \t]+body\\|"
5173 "protected[ \t]+type\\|"
5174 "protected\\|"
5175 "task[ \t]+body\\|"
5176 "task[ \t]+type\\|"
5177 "task"
5178 "\\)\\>[ \t]*"
5179 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5180 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5182 ;; Optional keywords followed by a type name.
5183 (list (concat ; ":[ \t]*"
5184 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5185 "[ \t]*"
5186 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5187 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5190 ;; Main keywords, except those treated specially below.
5191 (concat "\\<"
5192 (regexp-opt
5193 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5194 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5195 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5196 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5197 "null" "or" "others" "overriding" "private" "protected" "raise"
5198 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5199 "select" "separate" "synchronized" "tagged" "task" "terminate"
5200 "then" "until" "when" "while" "with" "xor") t)
5201 "\\>")
5203 ;; Anything following end and not already fontified is a body name.
5204 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5205 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5207 ;; Keywords followed by a type or function name.
5208 (list (concat "\\<\\("
5209 "new\\|of\\|subtype\\|type"
5210 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5211 '(1 font-lock-keyword-face)
5212 '(2 (if (match-beginning 4)
5213 font-lock-function-name-face
5214 font-lock-type-face) nil t))
5216 ;; Keywords followed by a (comma separated list of) reference.
5217 ;; Note that font-lock only works on single lines, thus we can not
5218 ;; correctly highlight a with_clause that spans multiple lines.
5219 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5220 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5221 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
5224 ;; Goto tags.
5225 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
5227 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5228 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5230 ;; Ada unnamed numerical constants
5231 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5234 "Default expressions to highlight in Ada mode.")
5237 ;; ---------------------------------------------------------
5238 ;; Support for outline.el
5239 ;; ---------------------------------------------------------
5241 (defun ada-outline-level ()
5242 "This is so that `current-column' DTRT in otherwise-hidden text."
5243 ;; patch from Dave Love <fx@gnu.org>
5244 (let (buffer-invisibility-spec)
5245 (save-excursion
5246 (back-to-indentation)
5247 (current-column))))
5249 ;; ---------------------------------------------------------
5250 ;; Support for narrow-to-region
5251 ;; ---------------------------------------------------------
5253 (defun ada-narrow-to-defun (&optional _arg)
5254 "Make text outside current subprogram invisible.
5255 The subprogram visible is the one that contains or follow point.
5256 Optional ARG is ignored.
5257 Use \\[widen] to go back to the full visibility for the buffer."
5259 (interactive)
5260 (save-excursion
5261 (let (end)
5262 (widen)
5263 (forward-line 1)
5264 (ada-previous-procedure)
5265 (setq end (point-at-bol))
5266 (ada-move-to-end)
5267 (end-of-line)
5268 (narrow-to-region end (point))
5269 (message
5270 "Use M-x widen to get back to full visibility in the buffer"))))
5272 ;; ---------------------------------------------------------
5273 ;; Automatic generation of code
5274 ;; The Ada mode has a set of function to automatically generate a subprogram
5275 ;; or package body from its spec.
5276 ;; These function only use a primary and basic algorithm, this could use a
5277 ;; lot of improvement.
5278 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5279 ;; body.
5280 ;; ----------------------------------------------------------
5282 (defun ada-gen-treat-proc (match)
5283 "Make dummy body of a procedure/function specification.
5284 MATCH is a cons cell containing the start and end locations of the last search
5285 for `ada-procedure-start-regexp'."
5286 (goto-char (car match))
5287 (let (func-found procname functype)
5288 (cond
5289 ((or (looking-at "^[ \t]*procedure")
5290 (setq func-found (looking-at "^[ \t]*function")))
5291 ;; treat it as a proc/func
5292 (forward-word 2)
5293 (forward-word -1)
5294 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5296 ;; goto end of procname
5297 (goto-char (cdr match))
5299 ;; skip over parameterlist
5300 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5301 (forward-sexp))
5303 ;; if function, skip over 'return' and result type.
5304 (if func-found
5305 (progn
5306 (forward-word 1)
5307 (skip-chars-forward " \t\n")
5308 (setq functype (buffer-substring (point)
5309 (progn
5310 (skip-chars-forward
5311 "a-zA-Z0-9_\.")
5312 (point))))))
5313 ;; look for next non WS
5314 (cond
5315 ((looking-at "[ \t]*;")
5316 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5317 (ada-indent-newline-indent)
5318 (insert "is")
5319 (ada-indent-newline-indent)
5320 (if func-found
5321 (progn
5322 (insert "Result : " functype ";")
5323 (ada-indent-newline-indent)))
5324 (insert "begin")
5325 (ada-indent-newline-indent)
5326 (if func-found
5327 (insert "return Result;")
5328 (insert "null;"))
5329 (ada-indent-newline-indent)
5330 (insert "end " procname ";")
5331 (ada-indent-newline-indent)
5334 ((looking-at "[ \t\n]*is")
5335 ;; do nothing
5338 ((looking-at "[ \t\n]*rename")
5339 ;; do nothing
5343 (message "unknown syntax"))))
5345 (if (looking-at "^[ \t]*task")
5346 (progn
5347 (message "Task conversion is not yet implemented")
5348 (forward-word 2)
5349 (if (looking-at "[ \t]*;")
5350 (forward-line)
5351 (ada-move-to-end))
5352 ))))))
5354 (defun ada-make-body ()
5355 "Create an Ada package body in the current buffer.
5356 The spec must be the previously visited buffer.
5357 This function typically is to be hooked into `ff-file-created-hook'."
5358 (delete-region (point-min) (point-max))
5359 (insert-buffer-substring (car (cdr (buffer-list))))
5360 (goto-char (point-min))
5361 (ada-mode)
5363 (let (found ada-procedure-or-package-start-regexp)
5364 (if (setq found
5365 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5366 (progn (goto-char (cdr found))
5367 (insert " body")
5369 (error "No package"))
5371 (setq ada-procedure-or-package-start-regexp
5372 (concat ada-procedure-start-regexp
5373 "\\|"
5374 ada-package-start-regexp))
5376 (while (setq found
5377 (ada-search-ignore-string-comment
5378 ada-procedure-or-package-start-regexp nil))
5379 (progn
5380 (goto-char (car found))
5381 (if (looking-at ada-package-start-regexp)
5382 (progn (goto-char (cdr found))
5383 (insert " body"))
5384 (ada-gen-treat-proc found))))))
5387 (defun ada-make-subprogram-body ()
5388 "Create a dummy subprogram body in package body file from spec surrounding point."
5389 (interactive)
5390 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5391 (spec (match-beginning 0))
5392 body-file)
5393 (if found
5394 (progn
5395 (goto-char spec)
5396 (if (and (re-search-forward "(\\|;" nil t)
5397 (= (char-before) ?\())
5398 (progn
5399 (ada-search-ignore-string-comment ")" nil)
5400 (ada-search-ignore-string-comment ";" nil)))
5401 (setq spec (buffer-substring spec (point)))
5403 ;; If find-file.el was available, use its functions
5404 (setq body-file (ada-get-body-name))
5405 (if body-file
5406 (find-file body-file)
5407 (error "No body found for the package. Create it first"))
5409 (save-restriction
5410 (widen)
5411 (goto-char (point-max))
5412 (forward-comment -10000)
5413 (re-search-backward "\\<end\\>" nil t)
5414 ;; Move to the beginning of the elaboration part, if any
5415 (re-search-backward "^begin" nil t)
5416 (newline)
5417 (forward-char -1)
5418 (insert spec)
5419 (re-search-backward ada-procedure-start-regexp nil t)
5420 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5422 (error "Not in subprogram spec"))))
5424 ;; --------------------------------------------------------
5425 ;; Global initializations
5426 ;; --------------------------------------------------------
5428 ;; Create the keymap once and for all. If we do that in ada-mode,
5429 ;; the keys changed in the user's .emacs have to be modified
5430 ;; every time
5431 (ada-create-keymap)
5432 (ada-create-menu)
5434 ;; Create the syntax tables, but do not activate them
5435 (ada-create-syntax-table)
5437 ;; Add the default extensions (and set up speedbar)
5438 (ada-add-extensions ".ads" ".adb")
5439 ;; This two files are generated by GNAT when running with -gnatD
5440 (if (equal ada-which-compiler 'gnat)
5441 (ada-add-extensions ".ads.dg" ".adb.dg"))
5443 ;; Read the special cases for exceptions
5444 (ada-case-read-exceptions)
5446 ;; Setup auto-loading of the other Ada mode files.
5447 (autoload 'ada-change-prj "ada-xref" nil t)
5448 (autoload 'ada-check-current "ada-xref" nil t)
5449 (autoload 'ada-compile-application "ada-xref" nil t)
5450 (autoload 'ada-compile-current "ada-xref" nil t)
5451 (autoload 'ada-complete-identifier "ada-xref" nil t)
5452 (autoload 'ada-find-file "ada-xref" nil t)
5453 (autoload 'ada-find-any-references "ada-xref" nil t)
5454 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5455 (autoload 'ada-find-local-references "ada-xref" nil t)
5456 (autoload 'ada-find-references "ada-xref" nil t)
5457 (autoload 'ada-gdb-application "ada-xref" nil t)
5458 (autoload 'ada-goto-declaration "ada-xref" nil t)
5459 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5460 (autoload 'ada-goto-parent "ada-xref" nil t)
5461 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5462 (autoload 'ada-point-and-xref "ada-xref" nil t)
5463 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5464 (autoload 'ada-run-application "ada-xref" nil t)
5465 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5466 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5467 (autoload 'ada-set-main-compile-application "ada-xref" nil t)
5468 (autoload 'ada-show-current-main "ada-xref" nil t)
5470 (autoload 'ada-customize "ada-prj" nil t)
5471 (autoload 'ada-prj-edit "ada-prj" nil t)
5472 (autoload 'ada-prj-new "ada-prj" nil t)
5473 (autoload 'ada-prj-save "ada-prj" nil t)
5475 (autoload 'ada-array "ada-stmt" nil t)
5476 (autoload 'ada-case "ada-stmt" nil t)
5477 (autoload 'ada-declare-block "ada-stmt" nil t)
5478 (autoload 'ada-else "ada-stmt" nil t)
5479 (autoload 'ada-elsif "ada-stmt" nil t)
5480 (autoload 'ada-exception "ada-stmt" nil t)
5481 (autoload 'ada-exception-block "ada-stmt" nil t)
5482 (autoload 'ada-exit "ada-stmt" nil t)
5483 (autoload 'ada-for-loop "ada-stmt" nil t)
5484 (autoload 'ada-function-spec "ada-stmt" nil t)
5485 (autoload 'ada-header "ada-stmt" nil t)
5486 (autoload 'ada-if "ada-stmt" nil t)
5487 (autoload 'ada-loop "ada-stmt" nil t)
5488 (autoload 'ada-package-body "ada-stmt" nil t)
5489 (autoload 'ada-package-spec "ada-stmt" nil t)
5490 (autoload 'ada-private "ada-stmt" nil t)
5491 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5492 (autoload 'ada-record "ada-stmt" nil t)
5493 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5494 (autoload 'ada-subtype "ada-stmt" nil t)
5495 (autoload 'ada-tabsize "ada-stmt" nil t)
5496 (autoload 'ada-task-body "ada-stmt" nil t)
5497 (autoload 'ada-task-spec "ada-stmt" nil t)
5498 (autoload 'ada-type "ada-stmt" nil t)
5499 (autoload 'ada-use "ada-stmt" nil t)
5500 (autoload 'ada-when "ada-stmt" nil t)
5501 (autoload 'ada-while-loop "ada-stmt" nil t)
5502 (autoload 'ada-with "ada-stmt" nil t)
5504 ;;; provide ourselves
5505 (provide 'ada-mode)
5507 ;;; ada-mode.el ends here