lisp/desktop.el (desktop-clear): Fix previous change.
[emacs.git] / lisp / progmodes / ada-mode.el
blob805444d08b9e719b7d58b56055015830f85a6b3e
1 ;;; ada-mode.el --- major-mode for editing Ada sources
3 ;; Copyright (C) 1994-1995, 1997-2013 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 mode line 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 (define-abbrev-table 'ada-mode-abbrev-table ()
461 "Local abbrev table for Ada mode.")
463 (eval-when-compile
464 ;; These values are used in eval-when-compile expressions.
465 (defconst ada-83-string-keywords
466 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
467 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
468 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
469 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
470 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
471 "procedure" "raise" "range" "record" "rem" "renames" "return"
472 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
473 "type" "use" "when" "while" "with" "xor")
474 "List of Ada 83 keywords.
475 Used to define `ada-*-keywords'.")
477 (defconst ada-95-string-keywords
478 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
479 "List of keywords new in Ada 95.
480 Used to define `ada-*-keywords'.")
482 (defconst ada-2005-string-keywords
483 '("interface" "overriding" "synchronized")
484 "List of keywords new in Ada 2005.
485 Used to define `ada-*-keywords.'"))
487 (defvar ada-ret-binding nil
488 "Variable to save key binding of RET when casing is activated.")
490 (defvar ada-case-exception '()
491 "Alist of words (entities) that have special casing.")
493 (defvar ada-case-exception-substring '()
494 "Alist of substrings (entities) that have special casing.
495 The substrings are detected for word constituent when the word
496 is not itself in `ada-case-exception', and only for substrings that
497 either are at the beginning or end of the word, or start after '_'.")
499 (defvar ada-lfd-binding nil
500 "Variable to save key binding of LFD when casing is activated.")
502 (defvar ada-other-file-alist nil
503 "Variable used by `find-file' to find the name of the other package.
504 See `ff-other-file-alist'.")
506 (defvar ada-align-list
507 '(("[^:]\\(\\s-*\\):[^:]" 1 t)
508 ("[^=]\\(\\s-+\\)=[^=]" 1 t)
509 ("\\(\\s-*\\)use\\s-" 1)
510 ("\\(\\s-*\\)--" 1))
511 "Ada support for align.el <= 2.2.
512 This variable provides regular expressions on which to align different lines.
513 See `align-mode-alist' for more information.")
515 (defvar ada-align-modes
516 '((ada-declaration
517 (regexp . "[^:]\\(\\s-*\\):[^:]")
518 (valid . (lambda() (not (ada-in-comment-p))))
519 (modes . '(ada-mode)))
520 (ada-assignment
521 (regexp . "[^=]\\(\\s-+\\)=[^=]")
522 (valid . (lambda() (not (ada-in-comment-p))))
523 (modes . '(ada-mode)))
524 (ada-comment
525 (regexp . "\\(\\s-*\\)--")
526 (modes . '(ada-mode)))
527 (ada-use
528 (regexp . "\\(\\s-*\\)use\\s-")
529 (valid . (lambda() (not (ada-in-comment-p))))
530 (modes . '(ada-mode)))
532 "Ada support for align.el >= 2.8.
533 This variable defines several rules to use to align different lines.")
535 (defconst ada-align-region-separate
536 (eval-when-compile
537 (concat
538 "^\\s-*\\($\\|\\("
539 "begin\\|"
540 "declare\\|"
541 "else\\|"
542 "end\\|"
543 "exception\\|"
544 "for\\|"
545 "function\\|"
546 "generic\\|"
547 "if\\|"
548 "is\\|"
549 "procedure\\|"
550 "record\\|"
551 "return\\|"
552 "type\\|"
553 "when"
554 "\\)\\>\\)"))
555 "See the variable `align-region-separate' for more information.")
557 ;;; ---- Below are the regexp used in this package for parsing
559 (defconst ada-83-keywords
560 (eval-when-compile
561 (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
562 "Regular expression matching Ada83 keywords.")
564 (defconst ada-95-keywords
565 (eval-when-compile
566 (concat "\\<" (regexp-opt
567 (append
568 ada-95-string-keywords
569 ada-83-string-keywords) t) "\\>"))
570 "Regular expression matching Ada95 keywords.")
572 (defconst ada-2005-keywords
573 (eval-when-compile
574 (concat "\\<" (regexp-opt
575 (append
576 ada-2005-string-keywords
577 ada-83-string-keywords
578 ada-95-string-keywords) t) "\\>"))
579 "Regular expression matching Ada2005 keywords.")
581 (defvar ada-keywords ada-2005-keywords
582 "Regular expression matching Ada keywords.")
583 ;; FIXME: make this customizable
585 (defconst ada-ident-re
586 "[[:alpha:]]\\(?:[_[:alnum:]]\\)*"
587 ;; [:alnum:] matches any multibyte word constituent, as well as
588 ;; Latin-1 letters and numbers. This allows __ and trailing _;
589 ;; someone (emacs bug#1919) proposed [^\W_] to fix that, but \W does
590 ;; _not_ mean "not word constituent" inside a character alternative.
591 "Regexp matching an Ada identifier.")
593 (defconst ada-goto-label-re
594 (concat "<<" ada-ident-re ">>")
595 "Regexp matching a goto label.")
597 (defconst ada-block-label-re
598 (concat ada-ident-re "[ \t\n]*:[^=]")
599 "Regexp matching a block label.
600 Note that this also matches a variable declaration.")
602 (defconst ada-label-re
603 (concat "\\(?:" ada-block-label-re "\\)\\|\\(?:" ada-goto-label-re "\\)")
604 "Regexp matching a goto or block label.")
606 ;; "with" needs to be included in the regexp, to match generic subprogram parameters
607 ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
608 (defvar ada-procedure-start-regexp
609 (concat
610 "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
612 ;; subprogram name: operator ("[+/=*]")
613 "\\("
614 "\\(\"[^\"]+\"\\)"
616 ;; subprogram name: name
617 "\\|"
618 "\\(\\(\\sw\\|[_.]\\)+\\)"
619 "\\)")
620 "Regexp matching Ada subprogram start.
621 The actual start is at (match-beginning 4). The name is in (match-string 5).")
623 (defconst ada-name-regexp
624 "\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
625 "Regexp matching a fully qualified name (including attribute).")
627 (defconst ada-package-start-regexp
628 (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp)
629 "Regexp matching start of package.
630 The package name is in (match-string 4).")
632 (defconst ada-compile-goto-error-file-linenr-re
633 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
634 "Regexp matching filename:linenr[:column].")
637 ;;; ---- regexps for indentation functions
639 (defvar ada-block-start-re
640 (eval-when-compile
641 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
642 "exception" "generic" "loop" "or"
643 "private" "select" ))
644 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
645 "Regexp for keywords starting Ada blocks.")
647 (defvar ada-end-stmt-re
648 (eval-when-compile
649 (concat "\\("
650 ";" "\\|"
651 "=>[ \t]*$" "\\|"
652 "=>[ \t]*--.*$" "\\|"
653 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
654 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
655 "loop" "private" "record" "select"
656 "then abort" "then") t) "\\>" "\\|"
657 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
658 t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
659 "^[ \t]*exception\\>"
660 "\\)") )
661 "Regexp of possible ends for a non-broken statement.
662 A new statement starts after these.")
664 (defvar ada-matching-start-re
665 (eval-when-compile
666 (concat "\\<"
667 (regexp-opt
668 '("end" "loop" "select" "begin" "case" "do" "declare"
669 "if" "task" "package" "procedure" "function" "record" "protected") t)
670 "\\>"))
671 "Regexp used in `ada-goto-matching-start'.")
673 (defvar ada-loop-start-re
674 "\\<\\(for\\|while\\|loop\\)\\>"
675 "Regexp for the start of a loop.")
677 (defvar ada-subprog-start-re
678 (eval-when-compile
679 (concat "\\<" (regexp-opt '("accept" "entry" "function" "overriding" "package" "procedure"
680 "protected" "task") t) "\\>"))
681 "Regexp for the start of a subprogram.")
683 (defvar ada-contextual-menu-on-identifier nil
684 "Set to true when the right mouse button was clicked on an identifier.")
686 (defvar ada-contextual-menu-last-point nil
687 "Position of point just before displaying the menu.
688 This is a list (point buffer).
689 Since `ada-popup-menu' moves the point where the user clicked, the region
690 is modified. Therefore no command from the menu knows what the user selected
691 before displaying the contextual menu.
692 To get the original region, restore the point to this position before
693 calling `region-end' and `region-beginning'.
694 Modify this variable if you want to restore the point to another position.")
696 (easy-menu-define ada-contextual-menu nil
697 "Menu to use when the user presses the right mouse button.
698 The variable `ada-contextual-menu-on-identifier' will be set to t before
699 displaying the menu if point was on an identifier."
700 '("Ada"
701 ["Goto Declaration/Body" ada-point-and-xref
702 :included ada-contextual-menu-on-identifier]
703 ["Goto Body" ada-point-and-xref-body
704 :included ada-contextual-menu-on-identifier]
705 ["Goto Previous Reference" ada-xref-goto-previous-reference]
706 ["List References" ada-find-references
707 :included ada-contextual-menu-on-identifier]
708 ["List Local References" ada-find-local-references
709 :included ada-contextual-menu-on-identifier]
710 ["-" nil nil]
711 ["Other File" ff-find-other-file]
712 ["Goto Parent Unit" ada-goto-parent]))
715 ;;------------------------------------------------------------------
716 ;; Support for imenu (see imenu.el)
717 ;;------------------------------------------------------------------
719 (defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
721 (defconst ada-imenu-subprogram-menu-re
722 (concat "^[ \t]*\\(overriding[ \t]*\\)?\\(procedure\\|function\\)[ \t\n]+"
723 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
724 ada-imenu-comment-re
725 "\\)[ \t\n]*"
726 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
728 (defvar ada-imenu-generic-expression
729 (list
730 (list nil ada-imenu-subprogram-menu-re 3)
731 (list "*Specs*"
732 (concat
733 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
734 "\\("
735 "\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
736 ada-imenu-comment-re "\\)";; parameter list or simple space
737 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
738 "\\)?;") 2)
739 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
740 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
741 '("*Protected*"
742 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
743 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
744 "Imenu generic expression for Ada mode.
745 See `imenu-generic-expression'. This variable will create several submenus for
746 each type of entity that can be found in an Ada file.")
749 ;;------------------------------------------------------------
750 ;; Support for compile.el
751 ;;------------------------------------------------------------
753 (defun ada-compile-mouse-goto-error ()
754 "Mouse interface for `ada-compile-goto-error'."
755 (interactive)
756 (mouse-set-point last-input-event)
757 (ada-compile-goto-error (point))
760 (defun ada-compile-goto-error (pos)
761 "Replace `compile-goto-error' from compile.el.
762 If POS is on a file and line location, go to this position. It adds
763 to compile.el the capacity to go to a reference in an error message.
764 For instance, on these lines:
765 foo.adb:61:11: [...] in call to size declared at foo.ads:11
766 foo.adb:61:11: [...] in call to local declared at line 20
767 the 4 file locations can be clicked on and jumped to."
768 (interactive "d")
769 (goto-char pos)
771 (skip-chars-backward "-a-zA-Z0-9_:./\\")
772 (cond
773 ;; special case: looking at a filename:line not at the beginning of a line
774 ;; or a simple line reference "at line ..."
775 ((and (not (bolp))
776 (or (looking-at ada-compile-goto-error-file-linenr-re)
777 (and
778 (save-excursion
779 (beginning-of-line)
780 (looking-at ada-compile-goto-error-file-linenr-re))
781 (save-excursion
782 (if (looking-at "\\([0-9]+\\)") (backward-word 1))
783 (looking-at "line \\([0-9]+\\)"))))
785 (let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
786 (file (if (match-beginning 2) (match-string 1)
787 (save-excursion (beginning-of-line)
788 (looking-at ada-compile-goto-error-file-linenr-re)
789 (match-string 1))))
790 (error-pos (point-marker))
791 source)
793 ;; set source marker
794 (save-excursion
795 (compilation-find-file (point-marker) (match-string 1) "./")
796 (set-buffer file)
798 (when (stringp line)
799 (goto-char (point-min))
800 (forward-line (1- (string-to-number line))))
802 (setq source (point-marker)))
804 (compilation-goto-locus error-pos source nil)
808 ;; otherwise, default behavior
810 (compile-goto-error))
812 (recenter))
815 ;;-------------------------------------------------------------------------
816 ;; Grammar related function
817 ;; The functions below work with the syntax class of the characters in an Ada
818 ;; buffer. Two syntax tables are created, depending on whether we want '_'
819 ;; to be considered as part of a word or not.
820 ;; Some characters may have multiple meanings depending on the context:
821 ;; - ' is either the beginning of a constant character or an attribute
822 ;; - # is either part of a based literal or a gnatprep statement.
823 ;; - " starts a string, but not if inside a constant character.
824 ;; - ( and ) should be ignored if inside a constant character.
825 ;; Thus their syntax property is changed automatically, and we can still use
826 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
828 ;; On Emacs, this is done through the `syntax-table' text property. The
829 ;; corresponding action is applied automatically each time the buffer
830 ;; changes via syntax-propertize-function.
832 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
833 ;; slow advice to `parse-partial-sexp' to do the same thing.
834 ;; When executing parse-partial-sexp, we simply modify the strings before and
835 ;; after, so that the special constants '"', '(' and ')' do not interact
836 ;; with parse-partial-sexp.
837 ;; Note: this code is slow and needs to be rewritten as soon as something
838 ;; better is available on XEmacs.
839 ;;-------------------------------------------------------------------------
841 (defvar ada-mode-syntax-table
842 (let ((st (make-syntax-table)))
843 ;; Define string brackets (`%' is alternative string bracket, but
844 ;; almost never used as such and throws font-lock and indentation
845 ;; off the track.)
846 (modify-syntax-entry ?% "$" st)
847 (modify-syntax-entry ?\" "\"" st)
849 (modify-syntax-entry ?: "." st)
850 (modify-syntax-entry ?\; "." st)
851 (modify-syntax-entry ?& "." st)
852 (modify-syntax-entry ?\| "." st)
853 (modify-syntax-entry ?+ "." st)
854 (modify-syntax-entry ?* "." st)
855 (modify-syntax-entry ?/ "." st)
856 (modify-syntax-entry ?= "." st)
857 (modify-syntax-entry ?< "." st)
858 (modify-syntax-entry ?> "." st)
859 (modify-syntax-entry ?$ "." st)
860 (modify-syntax-entry ?\[ "." st)
861 (modify-syntax-entry ?\] "." st)
862 (modify-syntax-entry ?\{ "." st)
863 (modify-syntax-entry ?\} "." st)
864 (modify-syntax-entry ?. "." st)
865 (modify-syntax-entry ?\\ "." st)
866 (modify-syntax-entry ?\' "." st)
868 ;; A single hyphen is punctuation, but a double hyphen starts a comment.
869 (modify-syntax-entry ?- ". 12" st)
871 ;; See the comment above on grammar related function for the special
872 ;; setup for '#'.
873 (modify-syntax-entry ?# (if (featurep 'xemacs) "<" "$") st)
875 ;; And \f and \n end a comment.
876 (modify-syntax-entry ?\f "> " st)
877 (modify-syntax-entry ?\n "> " st)
879 ;; Define what belongs in Ada symbols.
880 (modify-syntax-entry ?_ "_" st)
882 ;; Define parentheses to match.
883 (modify-syntax-entry ?\( "()" st)
884 (modify-syntax-entry ?\) ")(" st)
886 "Syntax table to be used for editing Ada source code.")
888 (defvar ada-mode-symbol-syntax-table
889 (let ((st (make-syntax-table ada-mode-syntax-table)))
890 (modify-syntax-entry ?_ "w" st)
892 "Syntax table for Ada, where `_' is a word constituent.")
894 ;; Support of special characters in XEmacs (see the comments at the beginning
895 ;; of the section on Grammar related functions).
897 (if (featurep 'xemacs)
898 (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
899 "Handles special character constants and gnatprep statements."
900 (let (change)
901 (if (< to from)
902 (let ((tmp from))
903 (setq from to to tmp)))
904 (save-excursion
905 (goto-char from)
906 (while (re-search-forward "'\\([(\")#]\\)'" to t)
907 (setq change (cons (list (match-beginning 1)
909 (match-string 1))
910 change))
911 (replace-match "'A'"))
912 (goto-char from)
913 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
914 (setq change (cons (list (match-beginning 1)
915 (length (match-string 1))
916 (match-string 1))
917 change))
918 (replace-match (make-string (length (match-string 1)) ?@))))
919 ad-do-it
920 (save-excursion
921 (while change
922 (goto-char (caar change))
923 (delete-char (cadar change))
924 (insert (caddar change))
925 (setq change (cdr change)))))))
927 (unless (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
928 ;; Before `syntax-propertize', we had to use font-lock to apply syntax-table
929 ;; properties, and in some cases we even had to do it manually (in
930 ;; `ada-after-change-function'). `ada-handle-syntax-table-properties'
931 ;; decides which method to use.
933 (defun ada-set-syntax-table-properties ()
934 "Assign `syntax-table' properties in accessible part of buffer.
935 In particular, character constants are said to be strings, #...#
936 are treated as numbers instead of gnatprep comments."
937 (let ((modified (buffer-modified-p))
938 (buffer-undo-list t)
939 (inhibit-read-only t)
940 (inhibit-point-motion-hooks t)
941 (inhibit-modification-hooks t))
942 (remove-text-properties (point-min) (point-max) '(syntax-table nil))
943 (goto-char (point-min))
944 (while (re-search-forward
945 ;; The following regexp was adapted from
946 ;; `ada-font-lock-syntactic-keywords'.
947 "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
948 nil t)
949 (if (match-beginning 1)
950 (put-text-property
951 (match-beginning 1) (match-end 1) 'syntax-table '(11 . ?\n))
952 (put-text-property
953 (match-beginning 2) (match-end 2) 'syntax-table '(7 . ?'))
954 (put-text-property
955 (match-beginning 3) (match-end 3) 'syntax-table '(7 . ?'))))
956 (unless modified
957 (restore-buffer-modified-p nil))))
959 (defun ada-after-change-function (beg end _old-len)
960 "Called when the region between BEG and END was changed in the buffer.
961 OLD-LEN indicates what the length of the replaced text was."
962 (save-excursion
963 (save-restriction
964 (let ((from (progn (goto-char beg) (line-beginning-position)))
965 (to (progn (goto-char end) (line-end-position))))
966 (narrow-to-region from to)
967 (save-match-data
968 (ada-set-syntax-table-properties))))))
970 (defun ada-initialize-syntax-table-properties ()
971 "Assign `syntax-table' properties in current buffer."
972 (save-excursion
973 (save-restriction
974 (widen)
975 (save-match-data
976 (ada-set-syntax-table-properties))))
977 (add-hook 'after-change-functions 'ada-after-change-function nil t))
979 (defun ada-handle-syntax-table-properties ()
980 "Handle `syntax-table' properties."
981 (if font-lock-mode
982 ;; `font-lock-mode' will take care of `syntax-table' properties.
983 (remove-hook 'after-change-functions 'ada-after-change-function t)
984 ;; Take care of `syntax-table' properties manually.
985 (ada-initialize-syntax-table-properties)))
987 ) ;;(not (fboundp 'syntax-propertize))
989 ;;------------------------------------------------------------------
990 ;; Testing the grammatical context
991 ;;------------------------------------------------------------------
993 (defsubst ada-in-comment-p (&optional parse-result)
994 "Return t if inside a comment.
995 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
996 (nth 4 (or parse-result
997 (parse-partial-sexp
998 (line-beginning-position) (point)))))
1000 (defsubst ada-in-string-p (&optional parse-result)
1001 "Return t if point is inside a string.
1002 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1003 (nth 3 (or parse-result
1004 (parse-partial-sexp
1005 (line-beginning-position) (point)))))
1007 (defsubst ada-in-string-or-comment-p (&optional parse-result)
1008 "Return t if inside a comment or string.
1009 If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
1010 (setq parse-result (or parse-result
1011 (parse-partial-sexp
1012 (line-beginning-position) (point))))
1013 (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
1015 (defsubst ada-in-numeric-literal-p ()
1016 "Return t if point is after a prefix of a numeric literal."
1017 (looking-back "\\([0-9]+#[0-9a-fA-F_]+\\)"))
1019 ;;------------------------------------------------------------------
1020 ;; Contextual menus
1021 ;; The Ada mode comes with contextual menus, bound by default to the right
1022 ;; mouse button.
1023 ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
1024 ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
1025 ;; if the mouse button was pressed on an identifier.
1026 ;;------------------------------------------------------------------
1028 (defun ada-call-from-contextual-menu (function)
1029 "Execute FUNCTION when called from the contextual menu.
1030 It forces Emacs to change the cursor position."
1031 (interactive)
1032 (funcall function)
1033 (setq ada-contextual-menu-last-point
1034 (list (point) (current-buffer))))
1036 (defun ada-popup-menu (position)
1037 "Pops up a contextual menu, depending on where the user clicked.
1038 POSITION is the location the mouse was clicked on.
1039 Sets `ada-contextual-menu-last-point' to the current position before
1040 displaying the menu. When a function from the menu is called, the
1041 point is where the mouse button was clicked."
1042 (interactive "e")
1044 ;; declare this as a local variable, so that the function called
1045 ;; in the contextual menu does not hide the region in
1046 ;; transient-mark-mode.
1047 (let ((deactivate-mark nil))
1048 (setq ada-contextual-menu-last-point
1049 (list (point) (current-buffer)))
1050 (mouse-set-point last-input-event)
1052 (setq ada-contextual-menu-on-identifier
1053 (and (char-after)
1054 (or (= (char-syntax (char-after)) ?w)
1055 (= (char-after) ?_))
1056 (not (ada-in-string-or-comment-p))
1057 (save-excursion (skip-syntax-forward "w")
1058 (not (ada-after-keyword-p)))
1060 (if (fboundp 'popup-menu)
1061 (funcall (symbol-function 'popup-menu) ada-contextual-menu)
1062 (let (choice)
1063 (setq choice (x-popup-menu position ada-contextual-menu))
1064 (if choice
1065 (funcall (lookup-key ada-contextual-menu (vector (car choice)))))))
1067 (set-buffer (cadr ada-contextual-menu-last-point))
1068 (goto-char (car ada-contextual-menu-last-point))
1072 ;;------------------------------------------------------------------
1073 ;; Misc functions
1074 ;;------------------------------------------------------------------
1076 ;;;###autoload
1077 (defun ada-add-extensions (spec body)
1078 "Define SPEC and BODY as being valid extensions for Ada files.
1079 Going from body to spec with `ff-find-other-file' used these
1080 extensions.
1081 SPEC and BODY are two regular expressions that must match against
1082 the file name."
1083 (let* ((reg (concat (regexp-quote body) "$"))
1084 (tmp (assoc reg ada-other-file-alist)))
1085 (if tmp
1086 (setcdr tmp (list (cons spec (cadr tmp))))
1087 (add-to-list 'ada-other-file-alist (list reg (list spec)))))
1089 (let* ((reg (concat (regexp-quote spec) "$"))
1090 (tmp (assoc reg ada-other-file-alist)))
1091 (if tmp
1092 (setcdr tmp (list (cons body (cadr tmp))))
1093 (add-to-list 'ada-other-file-alist (list reg (list body)))))
1095 (add-to-list 'auto-mode-alist
1096 (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
1097 (add-to-list 'auto-mode-alist
1098 (cons (concat (regexp-quote body) "\\'") 'ada-mode))
1100 (add-to-list 'ada-spec-suffixes spec)
1101 (add-to-list 'ada-body-suffixes body)
1103 ;; Support for speedbar (Specifies that we want to see these files in
1104 ;; speedbar)
1105 (if (fboundp 'speedbar-add-supported-extension)
1106 (progn
1107 (funcall (symbol-function 'speedbar-add-supported-extension)
1108 spec)
1109 (funcall (symbol-function 'speedbar-add-supported-extension)
1110 body))))
1112 (defvar ada-font-lock-syntactic-keywords) ; defined below
1114 ;;;###autoload
1115 (define-derived-mode ada-mode prog-mode "Ada"
1116 "Ada mode is the major mode for editing Ada code."
1118 ;; Set the paragraph delimiters so that one can select a whole block
1119 ;; simply with M-h
1120 (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
1121 (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
1123 ;; comment end must be set because it may hold a wrong value if
1124 ;; this buffer had been in another mode before. RE
1125 (set (make-local-variable 'comment-end) "")
1127 ;; used by autofill and indent-new-comment-line
1128 (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
1130 ;; used by autofill to break a comment line and continue it on another line.
1131 ;; The reason we need this one is that the default behavior does not work
1132 ;; correctly with the definition of paragraph-start above when the comment
1133 ;; is right after a multi-line subprogram declaration (the comments are
1134 ;; aligned under the latest parameter, not under the declaration start).
1135 (set (make-local-variable 'comment-line-break-function)
1136 (lambda (&optional soft) (let ((fill-prefix nil))
1137 (indent-new-comment-line soft))))
1139 (set (make-local-variable 'indent-line-function)
1140 'ada-indent-current-function)
1142 (set (make-local-variable 'comment-column) 40)
1144 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1145 ;; the comment and the text. We do not want any, this is already
1146 ;; included in comment-start
1147 (unless (featurep 'xemacs)
1148 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1149 (set (make-local-variable 'comment-padding) 0)
1150 (set (make-local-variable 'parse-sexp-lookup-properties) t))
1152 (setq case-fold-search t)
1153 (if (boundp 'imenu-case-fold-search)
1154 (setq imenu-case-fold-search t))
1156 (set (make-local-variable 'fill-paragraph-function)
1157 'ada-fill-comment-paragraph)
1159 ;; Support for compile.el
1160 ;; We just substitute our own functions to go to the error.
1161 (add-hook 'compilation-mode-hook
1162 (lambda()
1163 ;; FIXME: This has global impact! -stef
1164 (define-key compilation-minor-mode-map [mouse-2]
1165 'ada-compile-mouse-goto-error)
1166 (define-key compilation-minor-mode-map "\C-c\C-c"
1167 'ada-compile-goto-error)
1168 (define-key compilation-minor-mode-map "\C-m"
1169 'ada-compile-goto-error)))
1171 ;; font-lock support :
1173 (set (make-local-variable 'font-lock-defaults)
1174 '(ada-font-lock-keywords
1175 nil t
1176 ((?\_ . "w") (?# . "."))
1177 beginning-of-line))
1179 (if (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
1180 (set (make-local-variable 'syntax-propertize-function)
1181 (syntax-propertize-via-font-lock ada-font-lock-syntactic-keywords))
1182 (set (make-local-variable 'font-lock-syntactic-keywords)
1183 ada-font-lock-syntactic-keywords))
1185 ;; Set up support for find-file.el.
1186 (set (make-local-variable 'ff-other-file-alist)
1187 'ada-other-file-alist)
1188 (set (make-local-variable 'ff-search-directories)
1189 'ada-search-directories-internal)
1190 (setq ff-post-load-hook 'ada-set-point-accordingly
1191 ff-file-created-hook 'ada-make-body)
1192 (add-hook 'ff-pre-load-hook 'ada-which-function-are-we-in)
1194 (make-local-variable 'ff-special-constructs)
1195 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair))
1196 (list
1197 ;; Top level child package declaration; go to the parent package.
1198 (cons (eval-when-compile
1199 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1200 "\\(body[ \t]+\\)?"
1201 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1202 (lambda ()
1203 (ff-get-file
1204 ada-search-directories-internal
1205 (ada-make-filename-from-adaname (match-string 3))
1206 ada-spec-suffixes)))
1208 ;; A "separate" clause.
1209 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1210 (lambda ()
1211 (ff-get-file
1212 ada-search-directories-internal
1213 (ada-make-filename-from-adaname (match-string 1))
1214 ada-spec-suffixes)))
1216 ;; A "with" clause.
1217 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1218 (lambda ()
1219 (ff-get-file
1220 ada-search-directories-internal
1221 (ada-make-filename-from-adaname (match-string 1))
1222 ada-spec-suffixes)))
1225 ;; Support for outline-minor-mode
1226 (set (make-local-variable 'outline-regexp)
1227 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1228 (set (make-local-variable 'outline-level) 'ada-outline-level)
1230 ;; Support for imenu : We want a sorted index
1231 (setq imenu-generic-expression ada-imenu-generic-expression)
1233 (setq imenu-sort-function 'imenu--sort-by-name)
1235 ;; Support for ispell : Check only comments
1236 (set (make-local-variable 'ispell-check-comments) 'exclusive)
1238 ;; Support for align
1239 (add-to-list 'align-dq-string-modes 'ada-mode)
1240 (add-to-list 'align-open-comment-modes 'ada-mode)
1241 (set (make-local-variable 'align-region-separate) ada-align-region-separate)
1243 ;; Exclude comments alone on line from alignment.
1244 (add-to-list 'align-exclude-rules-list
1245 '(ada-solo-comment
1246 (regexp . "^\\(\\s-*\\)--")
1247 (modes . '(ada-mode))))
1248 (add-to-list 'align-exclude-rules-list
1249 '(ada-solo-use
1250 (regexp . "^\\(\\s-*\\)\\<use\\>")
1251 (modes . '(ada-mode))))
1253 (setq ada-align-modes nil)
1255 (add-to-list 'ada-align-modes
1256 '(ada-declaration-assign
1257 (regexp . "[^:]\\(\\s-*\\):[^:]")
1258 (valid . (lambda() (not (ada-in-comment-p))))
1259 (repeat . t)
1260 (modes . '(ada-mode))))
1261 (add-to-list 'ada-align-modes
1262 '(ada-associate
1263 (regexp . "[^=]\\(\\s-*\\)=>")
1264 (valid . (lambda() (not (ada-in-comment-p))))
1265 (modes . '(ada-mode))))
1266 (add-to-list 'ada-align-modes
1267 '(ada-comment
1268 (regexp . "\\(\\s-*\\)--")
1269 (modes . '(ada-mode))))
1270 (add-to-list 'ada-align-modes
1271 '(ada-use
1272 (regexp . "\\(\\s-*\\)\\<use\\s-")
1273 (valid . (lambda() (not (ada-in-comment-p))))
1274 (modes . '(ada-mode))))
1275 (add-to-list 'ada-align-modes
1276 '(ada-at
1277 (regexp . "\\(\\s-+\\)at\\>")
1278 (modes . '(ada-mode))))
1280 (setq align-mode-rules-list ada-align-modes)
1282 ;; Set up the contextual menu
1283 (if ada-popup-key
1284 (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
1286 ;; Support for Abbreviations (the user still needs to "M-x abbrev-mode").
1287 (setq local-abbrev-table ada-mode-abbrev-table)
1289 ;; Support for which-function mode
1290 (set (make-local-variable 'which-func-functions) '(ada-which-function))
1292 ;; Support for indent-new-comment-line (Especially for XEmacs)
1293 (set (make-local-variable 'comment-multi-line) nil)
1295 ;; Support for add-log
1296 (set (make-local-variable 'add-log-current-defun-function)
1297 'ada-which-function)
1299 (easy-menu-add ada-mode-menu ada-mode-map)
1301 (set (make-local-variable 'skeleton-further-elements)
1302 '((< '(backward-delete-char-untabify
1303 (min ada-indent (current-column))))))
1304 (add-hook 'skeleton-end-hook 'ada-adjust-case-skeleton nil t)
1306 ;; To be run after the hook, in case the user modified
1307 ;; ada-fill-comment-prefix
1308 (add-hook 'hack-local-variables-hook
1309 (lambda ()
1310 (set (make-local-variable 'comment-start)
1311 (or ada-fill-comment-prefix "-- "))
1313 ;; Run this after the hook to give the users a chance
1314 ;; to activate font-lock-mode.
1316 (unless (or (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
1317 (featurep 'xemacs))
1318 (ada-initialize-syntax-table-properties)
1319 (add-hook 'font-lock-mode-hook
1320 'ada-handle-syntax-table-properties nil t))
1322 ;; FIXME: ada-language-version might be set in the mode
1323 ;; hook or it might even be set later on via file-local
1324 ;; vars, so ada-keywords should be set lazily.
1325 (cond ((eq ada-language-version 'ada83)
1326 (setq ada-keywords ada-83-keywords))
1327 ((eq ada-language-version 'ada95)
1328 (setq ada-keywords ada-95-keywords))
1329 ((eq ada-language-version 'ada2005)
1330 (setq ada-keywords ada-2005-keywords)))
1332 (if ada-auto-case
1333 (ada-activate-keys-for-case)))
1334 nil 'local))
1336 (defun ada-adjust-case-skeleton ()
1337 "Adjust the case of the text inserted by a skeleton."
1338 (save-excursion
1339 (let ((aa-end (point)))
1340 (ada-adjust-case-region
1341 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
1342 (goto-char aa-end)))))
1344 (defun ada-region-selected ()
1345 "Should we operate on an active region?"
1346 (if (fboundp 'use-region-p)
1347 (use-region-p)
1348 (region-active-p)))
1350 ;;-----------------------------------------------------------------
1351 ;; auto-casing
1352 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1353 ;; functions to auto-case identifiers, keywords, ...
1354 ;; The basic rules for autocasing are defined through the variables
1355 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1356 ;; are references to the functions that will do the actual casing.
1358 ;; However, in most cases, the user will want to define some exceptions to
1359 ;; these casing rules. This is done through a list of files, that contain
1360 ;; one word per line. These files are stored in `ada-case-exception-file'.
1361 ;; For backward compatibility, this variable can also be a string.
1362 ;;-----------------------------------------------------------------
1364 (defun ada-save-exceptions-to-file (file-name)
1365 "Save the casing exception lists to the file FILE-NAME.
1366 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1367 (find-file (expand-file-name file-name))
1368 (erase-buffer)
1369 (mapc (lambda (x) (insert (car x) "\n"))
1370 (sort (copy-sequence ada-case-exception)
1371 (lambda(a b) (string< (car a) (car b)))))
1372 (mapc (lambda (x) (insert "*" (car x) "\n"))
1373 (sort (copy-sequence ada-case-exception-substring)
1374 (lambda(a b) (string< (car a) (car b)))))
1375 (save-buffer)
1376 (kill-buffer nil)
1379 (defun ada-create-case-exception (&optional word)
1380 "Define WORD as an exception for the casing system.
1381 If WORD is not given, then the current word in the buffer is used instead.
1382 The new word is added to the first file in `ada-case-exception-file'.
1383 The standard casing rules will no longer apply to this word."
1384 (interactive)
1385 (let ((file-name
1386 (cond ((stringp ada-case-exception-file)
1387 ada-case-exception-file)
1388 ((listp ada-case-exception-file)
1389 (car ada-case-exception-file))
1391 (error (concat "No exception file specified. "
1392 "See variable ada-case-exception-file"))))))
1394 (unless word
1395 (with-syntax-table ada-mode-symbol-syntax-table
1396 (save-excursion
1397 (skip-syntax-backward "w")
1398 (setq word (buffer-substring-no-properties
1399 (point) (save-excursion (forward-word 1) (point)))))))
1401 ;; Reread the exceptions file, in case it was modified by some other,
1402 (ada-case-read-exceptions-from-file file-name)
1404 ;; If the word is already in the list, even with a different casing
1405 ;; we simply want to replace it.
1406 (if (and (not (equal ada-case-exception '()))
1407 (assoc-string word ada-case-exception t))
1408 (setcar (assoc-string word ada-case-exception t) word)
1409 (add-to-list 'ada-case-exception (cons word t)))
1411 (ada-save-exceptions-to-file file-name)))
1413 (defun ada-create-case-exception-substring (&optional word)
1414 "Define the substring WORD as an exception for the casing system.
1415 If WORD is not given, then the current word in the buffer is used instead,
1416 or the selected region if any is active.
1417 The new word is added to the first file in `ada-case-exception-file'.
1418 When auto-casing a word, this substring will be special-cased, unless the
1419 word itself has a special casing."
1420 (interactive)
1421 (let ((file-name
1422 (cond ((stringp ada-case-exception-file)
1423 ada-case-exception-file)
1424 ((listp ada-case-exception-file)
1425 (car ada-case-exception-file))
1427 (error (concat "No exception file specified. "
1428 "See variable ada-case-exception-file"))))))
1430 ;; Find the substring to define as an exception. Order is: the parameter,
1431 ;; if any, or the selected region, or the word under the cursor
1432 (cond
1433 (word nil)
1435 ((ada-region-selected)
1436 (setq word (buffer-substring-no-properties
1437 (region-beginning) (region-end))))
1440 (let ((underscore-syntax (char-syntax ?_)))
1441 (unwind-protect
1442 (progn
1443 (modify-syntax-entry ?_ "." (syntax-table))
1444 (save-excursion
1445 (skip-syntax-backward "w")
1446 (setq word (buffer-substring-no-properties
1447 (point)
1448 (save-excursion (forward-word 1) (point))))))
1449 (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
1450 (syntax-table))))))
1452 ;; Reread the exceptions file, in case it was modified by some other,
1453 (ada-case-read-exceptions-from-file file-name)
1455 ;; If the word is already in the list, even with a different casing
1456 ;; we simply want to replace it.
1457 (if (and (not (equal ada-case-exception-substring '()))
1458 (assoc-string word ada-case-exception-substring t))
1459 (setcar (assoc-string word ada-case-exception-substring t) word)
1460 (add-to-list 'ada-case-exception-substring (cons word t))
1463 (ada-save-exceptions-to-file file-name)
1465 (message "%s" (concat "Defining " word " as a casing exception"))))
1467 (defun ada-case-read-exceptions-from-file (file-name)
1468 "Read the content of the casing exception file FILE-NAME."
1469 (if (file-readable-p (expand-file-name file-name))
1470 (let ((buffer (current-buffer)))
1471 (find-file (expand-file-name file-name))
1472 (set-syntax-table ada-mode-symbol-syntax-table)
1473 (widen)
1474 (goto-char (point-min))
1475 (while (not (eobp))
1477 ;; If the item is already in the list, even with an other casing,
1478 ;; do not add it again. This way, the user can easily decide which
1479 ;; priority should be applied to each casing exception
1480 (let ((word (buffer-substring-no-properties
1481 (point) (save-excursion (forward-word 1) (point)))))
1483 ;; Handling a substring ?
1484 (if (char-equal (string-to-char word) ?*)
1485 (progn
1486 (setq word (substring word 1))
1487 (unless (assoc-string word ada-case-exception-substring t)
1488 (add-to-list 'ada-case-exception-substring (cons word t))))
1489 (unless (assoc-string word ada-case-exception t)
1490 (add-to-list 'ada-case-exception (cons word t)))))
1492 (forward-line 1))
1493 (kill-buffer nil)
1494 (set-buffer buffer)))
1497 (defun ada-case-read-exceptions ()
1498 "Read all the casing exception files from `ada-case-exception-file'."
1499 (interactive)
1501 ;; Reinitialize the casing exception list
1502 (setq ada-case-exception '()
1503 ada-case-exception-substring '())
1505 (cond ((stringp ada-case-exception-file)
1506 (ada-case-read-exceptions-from-file ada-case-exception-file))
1508 ((listp ada-case-exception-file)
1509 (mapcar 'ada-case-read-exceptions-from-file
1510 ada-case-exception-file))))
1512 (defun ada-adjust-case-substring ()
1513 "Adjust case of substrings in the previous word."
1514 (interactive)
1515 (let ((substrings ada-case-exception-substring)
1516 (max (point))
1517 (case-fold-search t)
1518 (underscore-syntax (char-syntax ?_))
1521 (save-excursion
1522 (forward-word -1)
1524 (unwind-protect
1525 (progn
1526 (modify-syntax-entry ?_ "." (syntax-table))
1528 (while substrings
1529 (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
1531 (save-excursion
1532 (while (re-search-forward re max t)
1533 (replace-match (caar substrings) t)))
1534 (setq substrings (cdr substrings))
1537 (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
1540 (defun ada-adjust-case-identifier ()
1541 "Adjust case of the previous identifier.
1542 The auto-casing is done according to the value of `ada-case-identifier'
1543 and the exceptions defined in `ada-case-exception-file'."
1544 (interactive)
1545 (if (or (equal ada-case-exception '())
1546 (equal (char-after) ?_))
1547 (progn
1548 (funcall ada-case-identifier -1)
1549 (ada-adjust-case-substring))
1551 (progn
1552 (let ((end (point))
1553 (start (save-excursion (skip-syntax-backward "w")
1554 (point)))
1555 match)
1556 ;; If we have an exception, replace the word by the correct casing
1557 (if (setq match (assoc-string (buffer-substring start end)
1558 ada-case-exception t))
1560 (progn
1561 (delete-region start end)
1562 (insert (car match)))
1564 ;; Else simply re-case the word
1565 (funcall ada-case-identifier -1)
1566 (ada-adjust-case-substring))))))
1568 (defun ada-after-keyword-p ()
1569 "Return t if cursor is after a keyword that is not an attribute."
1570 (save-excursion
1571 (forward-word -1)
1572 (and (not (and (char-before)
1573 (or (= (char-before) ?_)
1574 (= (char-before) ?'))));; unless we have a _ or '
1575 (looking-at (concat ada-keywords "[^_]")))))
1577 (defun ada-adjust-case (&optional force-identifier)
1578 "Adjust the case of the word before the character just typed.
1579 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1580 (if (not (bobp))
1581 (progn
1582 (forward-char -1)
1583 (if (and (not (bobp))
1584 ;; or if at the end of a character constant
1585 (not (and (eq (following-char) ?')
1586 (eq (char-before (1- (point))) ?')))
1587 ;; or if the previous character was not part of a word
1588 (eq (char-syntax (char-before)) ?w)
1589 ;; if in a string or a comment
1590 (not (ada-in-string-or-comment-p))
1591 ;; if in a numeric literal
1592 (not (ada-in-numeric-literal-p))
1594 (if (save-excursion
1595 (forward-word -1)
1596 (or (= (point) (point-min))
1597 (backward-char 1))
1598 (= (following-char) ?'))
1599 (funcall ada-case-attribute -1)
1600 (if (and
1601 (not force-identifier) ; (MH)
1602 (ada-after-keyword-p))
1603 (funcall ada-case-keyword -1)
1604 (ada-adjust-case-identifier))))
1605 (forward-char 1)
1609 (defun ada-adjust-case-interactive (arg)
1610 "Adjust the case of the previous word, and process the character just typed.
1611 ARG is the prefix the user entered with \\[universal-argument]."
1612 (interactive "P")
1614 (if ada-auto-case
1615 (let ((lastk last-command-event))
1617 (with-syntax-table ada-mode-symbol-syntax-table
1618 (cond ((memq lastk '(?\n ?\r))
1619 ;; Horrible kludge.
1620 (insert " ")
1621 (ada-adjust-case)
1622 ;; horrible dekludge
1623 (delete-char -1)
1624 ;; some special keys and their bindings
1625 (cond
1626 ((eq lastk ?\n)
1627 (funcall ada-lfd-binding))
1628 ((eq lastk ?\r)
1629 (funcall ada-ret-binding))))
1630 ((eq lastk ?\C-i) (ada-tab))
1631 ;; Else just insert the character
1632 ((self-insert-command (prefix-numeric-value arg))))
1633 ;; if there is a keyword in front of the underscore
1634 ;; then it should be part of an identifier (MH)
1635 (if (eq lastk ?_)
1636 (ada-adjust-case t)
1637 (ada-adjust-case))))
1639 ;; Else, no auto-casing
1640 (cond
1641 ((eq last-command-event ?\n)
1642 (funcall ada-lfd-binding))
1643 ((eq last-command-event ?\r)
1644 (funcall ada-ret-binding))
1646 (self-insert-command (prefix-numeric-value arg))))))
1648 (defun ada-activate-keys-for-case ()
1649 ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
1650 "Modify the key bindings for all the keys that should readjust the casing."
1651 (interactive)
1652 ;; Save original key-bindings to allow swapping ret/lfd
1653 ;; when casing is activated.
1654 ;; The 'or ...' is there to be sure that the value will not
1655 ;; be changed again when Ada mode is called more than once
1656 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
1657 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
1659 ;; Call case modifying function after certain keys.
1660 (mapcar (function (lambda(key) (define-key
1661 ada-mode-map
1662 (char-to-string key)
1663 'ada-adjust-case-interactive)))
1664 '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
1665 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1667 (defun ada-loose-case-word (&optional _arg)
1668 "Upcase first letter and letters following `_' in the following word.
1669 No other letter is modified.
1670 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1671 (interactive)
1672 (save-excursion
1673 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1674 (first t))
1675 (skip-syntax-backward "w")
1676 (while (and (or first (search-forward "_" end t))
1677 (< (point) end))
1678 (and first
1679 (setq first nil))
1680 (insert-char (upcase (following-char)) 1)
1681 (delete-char 1)))))
1683 (defun ada-no-auto-case (&optional _arg)
1684 "Do nothing. ARG is ignored.
1685 This function can be used for the auto-casing variables in Ada mode, to
1686 adapt to unusual auto-casing schemes. Since it does nothing, you can for
1687 instance use it for `ada-case-identifier' if you don't want any special
1688 auto-casing for identifiers, whereas keywords have to be lower-cased.
1689 See also `ada-auto-case' to disable auto casing altogether."
1690 nil)
1692 (defun ada-capitalize-word (&optional _arg)
1693 "Upcase first letter and letters following '_', lower case other letters.
1694 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1695 (interactive)
1696 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1697 (begin (save-excursion (skip-syntax-backward "w") (point))))
1698 (capitalize-region begin end)))
1700 (defun ada-adjust-case-region (from to)
1701 "Adjust the case of all words in the region between FROM and TO.
1702 Attention: This function might take very long for big regions!"
1703 (interactive "*r")
1704 (let ((begin nil)
1705 (end nil)
1706 (keywordp nil)
1707 (attribp nil))
1708 (message "Adjusting case ...")
1709 (with-syntax-table ada-mode-symbol-syntax-table
1710 (save-excursion
1711 (goto-char to)
1713 ;; loop: look for all identifiers, keywords, and attributes
1715 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1716 (setq end (match-end 1))
1717 (setq attribp
1718 (and (> (point) from)
1719 (save-excursion
1720 (forward-char -1)
1721 (setq attribp (looking-at "'.[^']")))))
1723 ;; do nothing if it is a string or comment
1724 (ada-in-string-or-comment-p)
1725 (progn
1727 ;; get the identifier or keyword or attribute
1729 (setq begin (point))
1730 (setq keywordp (looking-at ada-keywords))
1731 (goto-char end)
1733 ;; casing according to user-option
1735 (if attribp
1736 (funcall ada-case-attribute -1)
1737 (if keywordp
1738 (funcall ada-case-keyword -1)
1739 (ada-adjust-case-identifier)))
1740 (goto-char begin))))
1741 (message "Adjusting case ... Done")))))
1743 (defun ada-adjust-case-buffer ()
1744 "Adjust the case of all words in the whole buffer.
1745 ATTENTION: This function might take very long for big buffers!"
1746 (interactive "*")
1747 (ada-adjust-case-region (point-min) (point-max)))
1750 ;;--------------------------------------------------------------
1751 ;; Format Parameter Lists
1752 ;; Some special algorithms are provided to indent the parameter lists in
1753 ;; subprogram declarations. This is done in two steps:
1754 ;; - First parses the parameter list. The returned list has the following
1755 ;; format:
1756 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1757 ;; ... )
1758 ;; This is done in `ada-scan-paramlist'.
1759 ;; - Delete and recreate the parameter list in function
1760 ;; `ada-insert-paramlist'.
1761 ;; Both steps are called from `ada-format-paramlist'.
1762 ;; Note: Comments inside the parameter list are lost.
1763 ;; The syntax has to be correct, or the reformatting will fail.
1764 ;;--------------------------------------------------------------
1766 (defun ada-format-paramlist ()
1767 "Reformat the parameter list point is in."
1768 (interactive)
1769 (let ((begin nil)
1770 (end nil)
1771 (delend nil)
1772 (paramlist nil))
1773 (with-syntax-table ada-mode-symbol-syntax-table
1775 ;; check if really inside parameter list
1776 (or (ada-in-paramlist-p)
1777 (error "Not in parameter list"))
1779 ;; find start of current parameter-list
1780 (ada-search-ignore-string-comment
1781 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1782 (down-list 1)
1783 (backward-char 1)
1784 (setq begin (point))
1786 ;; find end of parameter-list
1787 (forward-sexp 1)
1788 (setq delend (point))
1789 (delete-char -1)
1790 (insert "\n")
1792 ;; find end of last parameter-declaration
1793 (forward-comment -1000)
1794 (setq end (point))
1796 ;; build a list of all elements of the parameter-list
1797 (setq paramlist (ada-scan-paramlist (1+ begin) end))
1799 ;; delete the original parameter-list
1800 (delete-region begin delend)
1802 ;; insert the new parameter-list
1803 (goto-char begin)
1804 (ada-insert-paramlist paramlist))))
1806 (defun ada-scan-paramlist (begin end)
1807 "Scan the parameter list found in between BEGIN and END.
1808 Return the equivalent internal parameter list."
1809 (let ((paramlist (list))
1810 (param (list))
1811 (notend t)
1812 (apos nil)
1813 (epos nil)
1814 (semipos nil)
1815 (match-cons nil))
1817 (goto-char begin)
1819 ;; loop until end of last parameter
1820 (while notend
1822 ;; find first character of parameter-declaration
1823 (ada-goto-next-non-ws)
1824 (setq apos (point))
1826 ;; find last character of parameter-declaration
1827 (if (setq match-cons
1828 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1829 (progn
1830 (setq epos (car match-cons))
1831 (setq semipos (cdr match-cons)))
1832 (setq epos end))
1834 ;; read name(s) of parameter(s)
1835 (goto-char apos)
1836 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1838 (setq param (list (match-string 1)))
1839 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1841 ;; look for 'in'
1842 (setq apos (point))
1843 (setq param
1844 (append param
1845 (list
1846 (consp
1847 (ada-search-ignore-string-comment
1848 "in" nil epos t 'word-search-forward)))))
1850 ;; look for 'out'
1851 (goto-char apos)
1852 (setq param
1853 (append param
1854 (list
1855 (consp
1856 (ada-search-ignore-string-comment
1857 "out" nil epos t 'word-search-forward)))))
1859 ;; look for 'access'
1860 (goto-char apos)
1861 (setq param
1862 (append param
1863 (list
1864 (consp
1865 (ada-search-ignore-string-comment
1866 "access" nil epos t 'word-search-forward)))))
1868 ;; skip 'in'/'out'/'access'
1869 (goto-char apos)
1870 (ada-goto-next-non-ws)
1871 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1872 (forward-word 1)
1873 (ada-goto-next-non-ws))
1875 ;; read type of parameter
1876 ;; We accept spaces in the name, since some software like Rose
1877 ;; generates something like: "A : B 'Class"
1878 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1879 (setq param
1880 (append param
1881 (list (match-string 0))))
1883 ;; read default-expression, if there is one
1884 (goto-char (setq apos (match-end 0)))
1885 (setq param
1886 (append param
1887 (list
1888 (if (setq match-cons
1889 (ada-search-ignore-string-comment
1890 ":=" nil epos t 'search-forward))
1891 (buffer-substring (car match-cons) epos)
1892 nil))))
1894 ;; add this parameter-declaration to the list
1895 (setq paramlist (append paramlist (list param)))
1897 ;; check if it was the last parameter
1898 (if (eq epos end)
1899 (setq notend nil)
1900 (goto-char semipos))
1902 (reverse paramlist)))
1904 (defun ada-insert-paramlist (paramlist)
1905 "Insert a formatted PARAMLIST in the buffer."
1906 (let ((i (length paramlist))
1907 (parlen 0)
1908 (typlen 0)
1909 (inp nil)
1910 (outp nil)
1911 (accessp nil)
1912 (column nil)
1913 (firstcol nil))
1915 ;; loop until last parameter
1916 (while (not (zerop i))
1917 (setq i (1- i))
1919 ;; get max length of parameter-name
1920 (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
1922 ;; get max length of type-name
1923 (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
1925 ;; is there any 'in' ?
1926 (setq inp (or inp (nth 1 (nth i paramlist))))
1928 ;; is there any 'out' ?
1929 (setq outp (or outp (nth 2 (nth i paramlist))))
1931 ;; is there any 'access' ?
1932 (setq accessp (or accessp (nth 3 (nth i paramlist))))
1935 ;; does paramlist already start on a separate line ?
1936 (if (save-excursion
1937 (re-search-backward "^.\\|[^ \t]" nil t)
1938 (looking-at "^."))
1939 ;; yes => re-indent it
1940 (progn
1941 (ada-indent-current)
1942 (save-excursion
1943 (if (looking-at "\\(is\\|return\\)")
1944 (replace-match " \\1"))))
1946 ;; no => insert it where we are after removing any whitespace
1947 (fixup-whitespace)
1948 (save-excursion
1949 (cond
1950 ((looking-at "[ \t]*\\(\n\\|;\\)")
1951 (replace-match "\\1"))
1952 ((looking-at "[ \t]*\\(is\\|return\\)")
1953 (replace-match " \\1"))))
1954 (insert " "))
1956 (insert "(")
1957 (ada-indent-current)
1959 (setq firstcol (current-column))
1960 (setq i (length paramlist))
1962 ;; loop until last parameter
1963 (while (not (zerop i))
1964 (setq i (1- i))
1965 (setq column firstcol)
1967 ;; insert parameter-name, space and colon
1968 (insert (nth 0 (nth i paramlist)))
1969 (indent-to (+ column parlen 1))
1970 (insert ": ")
1971 (setq column (current-column))
1973 ;; insert 'in' or space
1974 (if (nth 1 (nth i paramlist))
1975 (insert "in ")
1976 (if (and
1977 (or inp
1978 accessp)
1979 (not (nth 3 (nth i paramlist))))
1980 (insert " ")))
1982 ;; insert 'out' or space
1983 (if (nth 2 (nth i paramlist))
1984 (insert "out ")
1985 (if (and
1986 (or outp
1987 accessp)
1988 (not (nth 3 (nth i paramlist))))
1989 (insert " ")))
1991 ;; insert 'access'
1992 (if (nth 3 (nth i paramlist))
1993 (insert "access "))
1995 (setq column (current-column))
1997 ;; insert type-name and, if necessary, space and default-expression
1998 (insert (nth 4 (nth i paramlist)))
1999 (if (nth 5 (nth i paramlist))
2000 (progn
2001 (indent-to (+ column typlen 1))
2002 (insert (nth 5 (nth i paramlist)))))
2004 ;; check if it was the last parameter
2005 (if (zerop i)
2006 (insert ")")
2007 ;; no => insert ';' and newline and indent
2008 (insert ";")
2009 (newline)
2010 (indent-to firstcol))
2013 ;; if anything follows, except semicolon, newline, is or return
2014 ;; put it in a new line and indent it
2015 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2016 (ada-indent-newline-indent))
2021 ;;;----------------------------------------------------------------
2022 ;; Indentation Engine
2023 ;; All indentations are indicated as a two-element string:
2024 ;; - position of reference in the buffer
2025 ;; - offset to indent from this position (can also be a symbol or a list
2026 ;; that are evaluated)
2027 ;; Thus the total indentation for a line is the column number of the reference
2028 ;; position plus whatever value the evaluation of the second element provides.
2029 ;; This mechanism is used so that the Ada mode can "explain" how the
2030 ;; indentation was calculated, by showing which variables were used.
2032 ;; The indentation itself is done in only one pass: first we try to guess in
2033 ;; what context we are by looking at the following keyword or punctuation
2034 ;; sign. If nothing remarkable is found, just try to guess the indentation
2035 ;; based on previous lines.
2037 ;; The relevant functions for indentation are:
2038 ;; - `ada-indent-region': Re-indent a region of text
2039 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2040 ;; calculation that were done
2041 ;; - `ada-indent-current': Re-indent the current line
2042 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2043 ;; based on the context (see above).
2044 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2045 ;; For efficiency, these functions do not check they are in the correct
2046 ;; context.
2047 ;;;----------------------------------------------------------------
2049 (defun ada-indent-region (beg end)
2050 "Indent the region between BEG end END."
2051 (interactive "*r")
2052 (goto-char beg)
2053 (let ((block-done 0)
2054 (lines-remaining (count-lines beg end))
2055 (msg (format "%%4d out of %4d lines remaining ..."
2056 (count-lines beg end)))
2057 (endmark (copy-marker end)))
2058 ;; catch errors while indenting
2059 (while (< (point) endmark)
2060 (if (> block-done 39)
2061 (progn
2062 (setq lines-remaining (- lines-remaining block-done)
2063 block-done 0)
2064 (message msg lines-remaining)))
2065 (if (= (char-after) ?\n) nil
2066 (ada-indent-current))
2067 (forward-line 1)
2068 (setq block-done (1+ block-done)))
2069 (message "Indenting ... done")))
2071 (defun ada-indent-newline-indent ()
2072 "Indent the current line, insert a newline and then indent the new line."
2073 (interactive "*")
2074 (ada-indent-current)
2075 (newline)
2076 (ada-indent-current))
2078 (defun ada-indent-newline-indent-conditional ()
2079 "Insert a newline and indent it.
2080 The original line is re-indented if `ada-indent-after-return' is non-nil."
2081 (interactive "*")
2082 ;; If at end of buffer (entering brand new code), some indentation
2083 ;; fails. For example, a block label requires whitespace following
2084 ;; the : to be recognized. So we do the newline first, then
2085 ;; go back and indent the original line.
2086 (newline)
2087 (if ada-indent-after-return
2088 (progn
2089 (forward-char -1)
2090 (ada-indent-current)
2091 (forward-char 1)))
2092 (ada-indent-current))
2094 (defun ada-justified-indent-current ()
2095 "Indent the current line and explain how the calculation was done."
2096 (interactive)
2098 (let ((cur-indent (ada-indent-current)))
2100 (let ((line (save-excursion
2101 (goto-char (car cur-indent))
2102 (count-lines 1 (point)))))
2104 (if (equal (cdr cur-indent) '(0))
2105 (message (concat "same indentation as line " (number-to-string line)))
2106 (message "%s" (mapconcat (lambda(x)
2107 (cond
2108 ((symbolp x)
2109 (symbol-name x))
2110 ((numberp x)
2111 (number-to-string x))
2112 ((listp x)
2113 (concat "- " (symbol-name (cadr x))))
2115 (cdr cur-indent)
2116 " + "))))
2117 (save-excursion
2118 (goto-char (car cur-indent))
2119 (sit-for 1))))
2121 (defun ada-batch-reformat ()
2122 "Re-indent and re-case all the files found on the command line.
2123 This function should be used from the command line, with a
2124 command like:
2125 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2127 (while command-line-args-left
2128 (let ((source (car command-line-args-left)))
2129 (message "Formatting %s" source)
2130 (find-file source)
2131 (ada-indent-region (point-min) (point-max))
2132 (ada-adjust-case-buffer)
2133 (write-file source))
2134 (setq command-line-args-left (cdr command-line-args-left)))
2135 (message "Done")
2136 (kill-emacs 0))
2138 (defsubst ada-goto-previous-word ()
2139 "Move point to the beginning of the previous word of Ada code.
2140 Return the new position of point or nil if not found."
2141 (ada-goto-next-word t))
2143 (defun ada-indent-current ()
2144 "Indent current line as Ada code.
2145 Return the calculation that was done, including the reference point
2146 and the offset."
2147 (interactive)
2148 (let ((orgpoint (point-marker))
2149 cur-indent tmp-indent
2150 prev-indent)
2152 (unwind-protect
2153 (with-syntax-table ada-mode-symbol-syntax-table
2155 ;; This needs to be done here so that the advice is not always
2156 ;; activated (this might interact badly with other modes)
2157 (if (featurep 'xemacs)
2158 (ad-activate 'parse-partial-sexp t))
2160 (save-excursion
2161 (setq cur-indent
2163 ;; Not First line in the buffer ?
2164 (if (save-excursion (zerop (forward-line -1)))
2165 (progn
2166 (back-to-indentation)
2167 (ada-get-current-indent))
2169 ;; first line in the buffer
2170 (list (point-min) 0))))
2172 ;; Evaluate the list to get the column to indent to
2173 ;; prev-indent contains the column to indent to
2174 (if cur-indent
2175 (setq prev-indent (save-excursion (goto-char (car cur-indent))
2176 (current-column))
2177 tmp-indent (cdr cur-indent))
2178 (setq prev-indent 0 tmp-indent '()))
2180 (while (not (null tmp-indent))
2181 (cond
2182 ((numberp (car tmp-indent))
2183 (setq prev-indent (+ prev-indent (car tmp-indent))))
2185 (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
2187 (setq tmp-indent (cdr tmp-indent)))
2189 ;; only re-indent if indentation is different then the current
2190 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
2192 (beginning-of-line)
2193 (delete-horizontal-space)
2194 (indent-to prev-indent))
2196 ;; restore position of point
2198 (goto-char orgpoint)
2199 (if (< (current-column) (current-indentation))
2200 (back-to-indentation)))
2202 (if (featurep 'xemacs)
2203 (ad-deactivate 'parse-partial-sexp)))
2205 cur-indent))
2207 (defun ada-get-current-indent ()
2208 "Return the indentation to use for the current line."
2209 (let (column
2211 match-cons
2212 result
2213 (orgpoint (save-excursion
2214 (beginning-of-line)
2215 (forward-comment -10000)
2216 (forward-line 1)
2217 (point))))
2219 (setq result
2220 (cond
2222 ;;-----------------------------
2223 ;; in open parenthesis, but not in parameter-list
2224 ;;-----------------------------
2226 ((and ada-indent-to-open-paren
2227 (not (ada-in-paramlist-p))
2228 (setq column (ada-in-open-paren-p)))
2230 ;; check if we have something like this (Table_Component_Type =>
2231 ;; Source_File_Record)
2232 (save-excursion
2234 ;; Align the closing parenthesis on the opening one
2235 (if (= (following-char) ?\))
2236 (save-excursion
2237 (goto-char column)
2238 (skip-chars-backward " \t")
2239 (list (1- (point)) 0))
2241 (if (and (skip-chars-backward " \t")
2242 (= (char-before) ?\n)
2243 (not (forward-comment -10000))
2244 (= (char-before) ?>))
2245 ;; ??? Could use a different variable
2246 (list column 'ada-broken-indent)
2248 ;; We want all continuation lines to be indented the same
2249 ;; (ada-broken-line from the opening parenthesis. However, in
2250 ;; parameter list, each new parameter should be indented at the
2251 ;; column as the opening parenthesis.
2253 ;; A special case to handle nested boolean expressions, as in
2254 ;; ((B
2255 ;; and then C) -- indented by ada-broken-indent
2256 ;; or else D) -- indenting this line.
2257 ;; ??? This is really a hack, we should have a proper way to go to
2258 ;; ??? the beginning of the statement
2260 (if (= (char-before) ?\))
2261 (backward-sexp))
2263 (if (memq (char-before) '(?, ?\; ?\( ?\)))
2264 (list column 0)
2265 (list column 'ada-continuation-indent)
2266 )))))
2268 ;;---------------------------
2269 ;; at end of buffer
2270 ;;---------------------------
2272 ((not (char-after))
2273 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2275 ;;---------------------------
2276 ;; starting with e
2277 ;;---------------------------
2279 ((= (downcase (char-after)) ?e)
2280 (cond
2282 ;; ------- end ------
2284 ((looking-at "end\\>")
2285 (let ((label 0)
2286 limit)
2287 (save-excursion
2288 (ada-goto-matching-start 1)
2291 ;; found 'loop' => skip back to 'while' or 'for'
2292 ;; if 'loop' is not on a separate line
2293 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2295 (if (save-excursion
2296 (beginning-of-line)
2297 (looking-at ".+\\<loop\\>"))
2298 (progn
2299 (save-excursion
2300 (setq limit (car (ada-search-ignore-string-comment ";" t))))
2301 (if (save-excursion
2302 (and
2303 (setq match-cons
2304 (ada-search-ignore-string-comment ada-loop-start-re t limit))
2305 (not (looking-at "\\<loop\\>"))))
2306 (progn
2307 (goto-char (car match-cons))
2308 (save-excursion
2309 (back-to-indentation)
2310 (if (looking-at ada-block-label-re)
2311 (setq label (- ada-label-indent))))))))
2313 ;; found 'record' =>
2314 ;; if the keyword is found at the beginning of a line (or just
2315 ;; after limited, we indent on it, otherwise we indent on the
2316 ;; beginning of the type declaration)
2317 ;; type A is (B : Integer;
2318 ;; C : Integer) is record
2319 ;; end record; -- This is badly indented otherwise
2320 (if (looking-at "record")
2321 (if (save-excursion
2322 (beginning-of-line)
2323 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2324 (list (save-excursion (back-to-indentation) (point)) 0)
2325 (list (save-excursion
2326 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2329 ;; Else keep the same indentation as the beginning statement
2330 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
2332 ;; ------ exception ----
2334 ((looking-at "exception\\>")
2335 (save-excursion
2336 (ada-goto-matching-start 1)
2337 (list (save-excursion (back-to-indentation) (point)) 0)))
2339 ;; else
2341 ((looking-at "else\\>")
2342 (if (save-excursion (ada-goto-previous-word)
2343 (looking-at "\\<or\\>"))
2344 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2345 (save-excursion
2346 (ada-goto-matching-start 1 nil t)
2347 (list (progn (back-to-indentation) (point)) 0))))
2349 ;; elsif
2351 ((looking-at "elsif\\>")
2352 (save-excursion
2353 (ada-goto-matching-start 1 nil t)
2354 (list (progn (back-to-indentation) (point)) 0)))
2358 ;;---------------------------
2359 ;; starting with w (when)
2360 ;;---------------------------
2362 ((and (= (downcase (char-after)) ?w)
2363 (looking-at "when\\>"))
2364 (save-excursion
2365 (ada-goto-matching-start 1)
2366 (list (save-excursion (back-to-indentation) (point))
2367 'ada-when-indent)))
2369 ;;---------------------------
2370 ;; starting with t (then)
2371 ;;---------------------------
2373 ((and (= (downcase (char-after)) ?t)
2374 (looking-at "then\\>"))
2375 (if (save-excursion (ada-goto-previous-word)
2376 (looking-at "and\\>"))
2377 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2378 (save-excursion
2379 ;; Select has been added for the statement: "select ... then abort"
2380 (ada-search-ignore-string-comment
2381 "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2382 (list (progn (back-to-indentation) (point))
2383 'ada-stmt-end-indent))))
2385 ;;---------------------------
2386 ;; starting with l (loop)
2387 ;;---------------------------
2389 ((and (= (downcase (char-after)) ?l)
2390 (looking-at "loop\\>"))
2391 (setq pos (point))
2392 (save-excursion
2393 (goto-char (match-end 0))
2394 (ada-goto-stmt-start)
2395 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2396 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2397 (unless (looking-at ada-loop-start-re)
2398 (ada-search-ignore-string-comment ada-loop-start-re
2399 nil pos))
2400 (if (looking-at "\\<loop\\>")
2401 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2402 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2404 ;;----------------------------
2405 ;; starting with l (limited) or r (record)
2406 ;;----------------------------
2408 ((or (and (= (downcase (char-after)) ?l)
2409 (looking-at "limited\\>"))
2410 (and (= (downcase (char-after)) ?r)
2411 (looking-at "record\\>")))
2413 (save-excursion
2414 (ada-search-ignore-string-comment
2415 "\\<\\(type\\|use\\)\\>" t nil)
2416 (if (looking-at "\\<use\\>")
2417 (ada-search-ignore-string-comment "for" t nil nil
2418 'word-search-backward))
2419 (list (progn (back-to-indentation) (point))
2420 'ada-indent-record-rel-type)))
2422 ;;---------------------------
2423 ;; starting with b (begin)
2424 ;;---------------------------
2426 ((and (= (downcase (char-after)) ?b)
2427 (looking-at "begin\\>"))
2428 (save-excursion
2429 (if (ada-goto-decl-start t)
2430 (list (progn (back-to-indentation) (point)) 0)
2431 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2433 ;;---------------------------
2434 ;; starting with i (is)
2435 ;;---------------------------
2437 ((and (= (downcase (char-after)) ?i)
2438 (looking-at "is\\>"))
2440 (if (and ada-indent-is-separate
2441 (save-excursion
2442 (goto-char (match-end 0))
2443 (ada-goto-next-non-ws (point-at-eol))
2444 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2445 (save-excursion
2446 (ada-goto-stmt-start)
2447 (list (progn (back-to-indentation) (point)) 'ada-indent))
2448 (save-excursion
2449 (ada-goto-stmt-start)
2450 (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
2451 (list (progn (back-to-indentation) (point)) 0)
2452 (list (progn (back-to-indentation) (point)) 'ada-indent)))))
2454 ;;---------------------------
2455 ;; starting with r (return, renames)
2456 ;;---------------------------
2458 ((and (= (downcase (char-after)) ?r)
2459 (looking-at "re\\(turn\\|names\\)\\>"))
2461 (save-excursion
2462 (let ((var 'ada-indent-return))
2463 ;; If looking at a renames, skip the 'return' statement too
2464 (if (looking-at "renames")
2465 (let (pos)
2466 (save-excursion
2467 (setq pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
2468 (if (and pos
2469 (= (downcase (char-after (car pos))) ?r))
2470 (goto-char (car pos)))
2471 (setq var 'ada-indent-renames)))
2473 (forward-comment -1000)
2474 (if (= (char-before) ?\))
2475 (forward-sexp -1)
2476 (forward-word -1))
2478 ;; If there is a parameter list, and we have a function declaration
2479 ;; or a access to subprogram declaration
2480 (let ((num-back 1))
2481 (if (and (= (following-char) ?\()
2482 (save-excursion
2483 (or (progn
2484 (backward-word 1)
2485 (looking-at "\\(function\\|procedure\\)\\>"))
2486 (progn
2487 (backward-word 1)
2488 (setq num-back 2)
2489 (looking-at "\\(function\\|procedure\\)\\>")))))
2491 ;; The indentation depends of the value of ada-indent-return
2492 (if (<= (eval var) 0)
2493 (list (point) (list '- var))
2494 (list (progn (backward-word num-back) (point))
2495 var))
2497 ;; Else there is no parameter list, but we have a function
2498 ;; Only do something special if the user want to indent
2499 ;; relative to the "function" keyword
2500 (if (and (> (eval var) 0)
2501 (save-excursion (forward-word -1)
2502 (looking-at "function\\>")))
2503 (list (progn (forward-word -1) (point)) var)
2505 ;; Else...
2506 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
2508 ;;--------------------------------
2509 ;; starting with 'o' or 'p'
2510 ;; 'or' as statement-start
2511 ;; 'private' as statement-start
2512 ;;--------------------------------
2514 ((and (or (= (downcase (char-after)) ?o)
2515 (= (downcase (char-after)) ?p))
2516 (or (ada-looking-at-semi-or)
2517 (ada-looking-at-semi-private)))
2518 (save-excursion
2519 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2520 (ada-goto-matching-start 1)
2521 (list (progn (back-to-indentation) (point)) 0)))
2523 ;;--------------------------------
2524 ;; starting with 'd' (do)
2525 ;;--------------------------------
2527 ((and (= (downcase (char-after)) ?d)
2528 (looking-at "do\\>"))
2529 (save-excursion
2530 (ada-goto-stmt-start)
2531 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2533 ;;--------------------------------
2534 ;; starting with '-' (comment)
2535 ;;--------------------------------
2537 ((= (char-after) ?-)
2538 (if ada-indent-comment-as-code
2540 ;; Indent comments on previous line comments if required
2541 ;; We must use a search-forward (even if the code is more complex),
2542 ;; since we want to find the beginning of the comment.
2543 (let (pos)
2545 (if (and ada-indent-align-comments
2546 (save-excursion
2547 (forward-line -1)
2548 (beginning-of-line)
2549 (while (and (not pos)
2550 (search-forward "--" (point-at-eol) t))
2551 (unless (ada-in-string-p)
2552 (setq pos (point))))
2553 pos))
2554 (list (- pos 2) 0)
2556 ;; Else always on previous line
2557 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2559 ;; Else same indentation as the previous line
2560 (list (save-excursion (back-to-indentation) (point)) 0)))
2562 ;;--------------------------------
2563 ;; starting with '#' (preprocessor line)
2564 ;;--------------------------------
2566 ((and (= (char-after) ?#)
2567 (equal ada-which-compiler 'gnat)
2568 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2569 (list (point-at-bol) 0))
2571 ;;--------------------------------
2572 ;; starting with ')' (end of a parameter list)
2573 ;;--------------------------------
2575 ((and (not (eobp)) (= (char-after) ?\)))
2576 (save-excursion
2577 (forward-char 1)
2578 (backward-sexp 1)
2579 (list (point) 0)))
2581 ;;---------------------------------
2582 ;; new/abstract/separate
2583 ;;---------------------------------
2585 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2586 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2588 ;;---------------------------------
2589 ;; package/function/procedure
2590 ;;---------------------------------
2592 ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
2593 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2594 (save-excursion
2595 ;; Go up until we find either a generic section, or the end of the
2596 ;; previous subprogram/package, or 'overriding' for this function/procedure
2597 (let (found)
2598 (while (and (not found)
2599 (ada-search-ignore-string-comment
2600 "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t))
2602 ;; avoid "with procedure"... in generic parts
2603 (save-excursion
2604 (forward-word -1)
2605 (setq found (not (looking-at "with"))))))
2607 (cond
2608 ((looking-at "\\<generic\\|overriding\\>")
2609 (list (progn (back-to-indentation) (point)) 0))
2612 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
2614 ;;---------------------------------
2615 ;; label
2616 ;;---------------------------------
2618 ((looking-at ada-label-re)
2619 (if (ada-in-decl-p)
2620 ;; ada-block-label-re matches variable declarations
2621 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2622 (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
2623 '(ada-label-indent))))
2627 ;;---------------------------------
2628 ;; Other syntaxes
2629 ;;---------------------------------
2630 (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2632 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2633 "Calculate the indentation for the new line after ORGPOINT.
2634 The result list is based on the previous lines in the buffer.
2635 If NOMOVE is nil, moves point to the beginning of the current statement.
2636 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2637 (if initial-pos
2638 (goto-char initial-pos))
2639 (let ((oldpoint (point)))
2641 ;; Is inside a parameter-list ?
2642 (if (ada-in-paramlist-p)
2643 (ada-get-indent-paramlist)
2645 ;; Move to beginning of current statement. If already at a
2646 ;; statement start, move to beginning of enclosing statement.
2647 (unless nomove
2648 (ada-goto-stmt-start t))
2650 ;; no beginning found => don't change indentation
2651 (if (and (eq oldpoint (point))
2652 (not nomove))
2653 (ada-get-indent-nochange)
2655 (cond
2657 ((and
2658 ada-indent-to-open-paren
2659 (ada-in-open-paren-p))
2660 (ada-get-indent-open-paren))
2662 ((looking-at "end\\>")
2663 (ada-get-indent-end orgpoint))
2665 ((looking-at ada-loop-start-re)
2666 (ada-get-indent-loop orgpoint))
2668 ((looking-at ada-subprog-start-re)
2669 (ada-get-indent-subprog orgpoint))
2671 ((looking-at ada-block-start-re)
2672 (ada-get-indent-block-start orgpoint))
2674 ((looking-at ada-block-label-re) ; also variable declaration
2675 (ada-get-indent-block-label orgpoint))
2677 ((looking-at ada-goto-label-re)
2678 (ada-get-indent-goto-label orgpoint))
2680 ((looking-at "\\(sub\\)?type\\>")
2681 (ada-get-indent-type orgpoint))
2683 ;; "then" has to be included in the case of "select...then abort"
2684 ;; statements, since (goto-stmt-start) at the beginning of
2685 ;; the current function would leave the cursor on that position
2686 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2687 (ada-get-indent-if orgpoint))
2689 ((looking-at "case\\>")
2690 (ada-get-indent-case orgpoint))
2692 ((looking-at "when\\>")
2693 (ada-get-indent-when orgpoint))
2695 ((looking-at "separate\\>")
2696 (ada-get-indent-nochange))
2698 ((looking-at "with\\>\\|use\\>")
2699 ;; Are we still in that statement, or are we in fact looking at
2700 ;; the previous one ?
2701 (if (save-excursion (search-forward ";" oldpoint t))
2702 (list (progn (back-to-indentation) (point)) 0)
2703 (list (point) (if (looking-at "with")
2704 'ada-with-indent
2705 'ada-use-indent))))
2708 (ada-get-indent-noindent orgpoint)))))
2711 (defun ada-get-indent-open-paren ()
2712 "Calculate the indentation when point is behind an unclosed parenthesis."
2713 (list (ada-in-open-paren-p) 0))
2715 (defun ada-get-indent-nochange ()
2716 "Return the current indentation of the previous line."
2717 (save-excursion
2718 (forward-line -1)
2719 (back-to-indentation)
2720 (list (point) 0)))
2722 (defun ada-get-indent-paramlist ()
2723 "Calculate the indentation when point is inside a parameter list."
2724 (save-excursion
2725 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2726 (cond
2727 ;; in front of the first parameter
2728 ((= (char-after) ?\()
2729 (goto-char (match-end 0))
2730 (list (point) 0))
2732 ;; in front of another parameter
2733 ((= (char-after) ?\;)
2734 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2735 (ada-goto-next-non-ws)
2736 (list (point) 0))
2738 ;; After an affectation (default parameter value in subprogram
2739 ;; declaration)
2740 ((and (= (following-char) ?=) (= (preceding-char) ?:))
2741 (back-to-indentation)
2742 (list (point) 'ada-broken-indent))
2744 ;; inside a parameter declaration
2746 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2747 (ada-goto-next-non-ws)
2748 (list (point) 'ada-broken-indent)))))
2750 (defun ada-get-indent-end (orgpoint)
2751 "Calculate the indentation when point is just before an end statement.
2752 ORGPOINT is the limit position used in the calculation."
2753 (let ((defun-name nil)
2754 (indent nil))
2756 ;; is the line already terminated by ';' ?
2757 (if (save-excursion
2758 (ada-search-ignore-string-comment ";" nil orgpoint nil
2759 'search-forward))
2761 ;; yes, look what's following 'end'
2762 (progn
2763 (forward-word 1)
2764 (ada-goto-next-non-ws)
2765 (cond
2767 ;; loop/select/if/case/return
2769 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
2770 (save-excursion (ada-check-matching-start (match-string 0)))
2771 (list (save-excursion (back-to-indentation) (point)) 0))
2774 ;; record
2776 ((looking-at "\\<record\\>")
2777 (save-excursion
2778 (ada-check-matching-start (match-string 0))
2779 ;; we are now looking at the matching "record" statement
2780 (forward-word 1)
2781 (ada-goto-stmt-start)
2782 ;; now on the matching type declaration, or use clause
2783 (unless (looking-at "\\(for\\|type\\)\\>")
2784 (ada-search-ignore-string-comment "\\<type\\>" t))
2785 (list (progn (back-to-indentation) (point)) 0)))
2787 ;; a named block end
2789 ((looking-at ada-ident-re)
2790 (setq defun-name (match-string 0))
2791 (save-excursion
2792 (ada-goto-matching-start 0)
2793 (ada-check-defun-name defun-name))
2794 (list (progn (back-to-indentation) (point)) 0))
2796 ;; a block-end without name
2798 ((= (char-after) ?\;)
2799 (save-excursion
2800 (ada-goto-matching-start 0)
2801 (if (looking-at "\\<begin\\>")
2802 (progn
2803 (setq indent (list (point) 0))
2804 (if (ada-goto-decl-start t)
2805 (list (progn (back-to-indentation) (point)) 0)
2806 indent))
2807 (list (progn (back-to-indentation) (point)) 0)
2810 ;; anything else - should maybe signal an error ?
2813 (list (save-excursion (back-to-indentation) (point))
2814 'ada-broken-indent))))
2816 (list (save-excursion (back-to-indentation) (point))
2817 'ada-broken-indent))))
2819 (defun ada-get-indent-case (orgpoint)
2820 "Calculate the indentation when point is just before a case statement.
2821 ORGPOINT is the limit position used in the calculation."
2822 (let ((match-cons nil)
2823 (opos (point)))
2824 (cond
2826 ;; case..is..when..=>
2828 ((save-excursion
2829 (setq match-cons (and
2830 ;; the `=>' must be after the keyword `is'.
2831 (ada-search-ignore-string-comment
2832 "is" nil orgpoint nil 'word-search-forward)
2833 (ada-search-ignore-string-comment
2834 "[ \t\n]+=>" nil orgpoint))))
2835 (save-excursion
2836 (goto-char (car match-cons))
2837 (unless (ada-search-ignore-string-comment "when" t opos)
2838 (error "Missing 'when' between 'case' and '=>'"))
2839 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2841 ;; case..is..when
2843 ((save-excursion
2844 (setq match-cons (ada-search-ignore-string-comment
2845 "when" nil orgpoint nil 'word-search-forward)))
2846 (goto-char (cdr match-cons))
2847 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2849 ;; case..is
2851 ((save-excursion
2852 (setq match-cons (ada-search-ignore-string-comment
2853 "is" nil orgpoint nil 'word-search-forward)))
2854 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2856 ;; incomplete case
2859 (list (save-excursion (back-to-indentation) (point))
2860 'ada-broken-indent)))))
2862 (defun ada-get-indent-when (orgpoint)
2863 "Calculate the indentation when point is just before a when statement.
2864 ORGPOINT is the limit position used in the calculation."
2865 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2866 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2867 (list cur-indent 'ada-indent)
2868 (list cur-indent 'ada-broken-indent))))
2870 (defun ada-get-indent-if (orgpoint)
2871 "Calculate the indentation when point is just before an if statement.
2872 ORGPOINT is the limit position used in the calculation."
2873 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2874 (match-cons nil))
2876 ;; Move to the correct then (ignore all "and then")
2878 (while (and (setq match-cons (ada-search-ignore-string-comment
2879 "\\<\\(then\\|and[ \t]*then\\)\\>"
2880 nil orgpoint))
2881 (= (downcase (char-after (car match-cons))) ?a)))
2882 ;; If "then" was found (we are looking at it)
2883 (if match-cons
2884 (progn
2886 ;; 'then' first in separate line ?
2887 ;; => indent according to 'then',
2888 ;; => else indent according to 'if'
2890 (if (save-excursion
2891 (back-to-indentation)
2892 (looking-at "\\<then\\>"))
2893 (setq cur-indent (save-excursion (back-to-indentation) (point))))
2894 ;; skip 'then'
2895 (forward-word 1)
2896 (list cur-indent 'ada-indent))
2898 (list cur-indent 'ada-broken-indent))))
2900 (defun ada-get-indent-block-start (orgpoint)
2901 "Calculate the indentation when point is at the start of a block.
2902 ORGPOINT is the limit position used in the calculation."
2903 (let ((pos nil))
2904 (cond
2905 ((save-excursion
2906 (forward-word 1)
2907 (setq pos (ada-goto-next-non-ws orgpoint)))
2908 (goto-char pos)
2909 (save-excursion
2910 (ada-indent-on-previous-lines t orgpoint)))
2912 ;; Special case for record types, for instance for:
2913 ;; type A is (B : Integer;
2914 ;; C : Integer) is record
2915 ;; null; -- This is badly indented otherwise
2916 ((looking-at "record")
2918 ;; If record is at the beginning of the line, indent from there
2919 (if (save-excursion
2920 (beginning-of-line)
2921 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2922 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
2924 ;; else indent relative to the type command
2925 (list (save-excursion
2926 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2927 'ada-indent)))
2929 ;; Special case for label:
2930 ((looking-at ada-block-label-re)
2931 (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent) 'ada-indent))
2933 ;; nothing follows the block-start
2935 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
2937 (defun ada-get-indent-subprog (orgpoint)
2938 "Calculate the indentation when point is just before a subprogram.
2939 ORGPOINT is the limit position used in the calculation."
2940 (let ((match-cons nil)
2941 (cur-indent (save-excursion (back-to-indentation) (point)))
2942 (foundis nil))
2944 ;; is there an 'is' in front of point ?
2946 (if (save-excursion
2947 (setq match-cons
2948 (ada-search-ignore-string-comment
2949 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
2951 ;; yes, then skip to its end
2953 (progn
2954 (setq foundis t)
2955 (goto-char (cdr match-cons)))
2957 ;; no, then goto next non-ws, if there is one in front of point
2959 (progn
2960 (unless (ada-goto-next-non-ws orgpoint)
2961 (goto-char orgpoint))))
2963 (cond
2965 ;; nothing follows 'is'
2967 ((and
2968 foundis
2969 (save-excursion
2970 (not (ada-search-ignore-string-comment
2971 "[^ \t\n]" nil orgpoint t))))
2972 (list cur-indent 'ada-indent))
2974 ;; is abstract/separate/new ...
2976 ((and
2977 foundis
2978 (save-excursion
2979 (setq match-cons
2980 (ada-search-ignore-string-comment
2981 "\\<\\(separate\\|new\\|abstract\\)\\>"
2982 nil orgpoint))))
2983 (goto-char (car match-cons))
2984 (ada-search-ignore-string-comment ada-subprog-start-re t)
2985 (ada-get-indent-noindent orgpoint))
2987 ;; something follows 'is'
2989 ((and
2990 foundis
2991 (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
2992 (goto-char match-cons)
2993 (ada-indent-on-previous-lines t orgpoint)))
2995 ;; no 'is' but ';'
2997 ((save-excursion
2998 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
2999 (list cur-indent 0))
3001 ;; no 'is' or ';'
3004 (list cur-indent 'ada-broken-indent)))))
3006 (defun ada-get-indent-noindent (orgpoint)
3007 "Calculate the indentation when point is just before a 'noindent stmt'.
3008 ORGPOINT is the limit position used in the calculation."
3009 (let ((label 0))
3010 (save-excursion
3011 (beginning-of-line)
3013 (cond
3015 ;; This one is called when indenting a line preceded by a multi-line
3016 ;; subprogram declaration (in that case, we are at this point inside
3017 ;; the parameter declaration list)
3018 ((ada-in-paramlist-p)
3019 (ada-previous-procedure)
3020 (list (save-excursion (back-to-indentation) (point)) 0))
3022 ;; This one is called when indenting the second line of a multi-line
3023 ;; declaration section, in a declare block or a record declaration
3024 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3025 (list (save-excursion (back-to-indentation) (point))
3026 'ada-broken-decl-indent))
3028 ;; This one is called in every other case when indenting a line at the
3029 ;; top level
3031 (if (looking-at (concat "[ \t]*" ada-block-label-re))
3032 (setq label (- ada-label-indent))
3034 (let (p)
3036 ;; "with private" or "null record" cases
3037 (if (or (save-excursion
3038 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
3039 (setq p (point))
3040 (save-excursion (forward-char -7);; skip back "private"
3041 (ada-goto-previous-word)
3042 (looking-at "with"))))
3043 (save-excursion
3044 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
3045 (setq p (point))
3046 (save-excursion (forward-char -6);; skip back "record"
3047 (ada-goto-previous-word)
3048 (looking-at "null")))))
3049 (progn
3050 (goto-char p)
3051 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
3052 (list (save-excursion (back-to-indentation) (point)) 0)))))
3053 (if (save-excursion
3054 (ada-search-ignore-string-comment ";" nil orgpoint nil
3055 'search-forward))
3056 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
3057 (list (+ (save-excursion (back-to-indentation) (point)) label)
3058 'ada-broken-indent)))))))
3060 (defun ada-get-indent-block-label (orgpoint)
3061 "Calculate the indentation when before a label or variable declaration.
3062 ORGPOINT is the limit position used in the calculation."
3063 (let ((match-cons nil)
3064 (cur-indent (save-excursion (back-to-indentation) (point))))
3065 (ada-search-ignore-string-comment ":" nil)
3066 (cond
3067 ;; loop label
3068 ((save-excursion
3069 (setq match-cons (ada-search-ignore-string-comment
3070 ada-loop-start-re nil orgpoint)))
3071 (goto-char (car match-cons))
3072 (ada-get-indent-loop orgpoint))
3074 ;; declare label
3075 ((save-excursion
3076 (setq match-cons (ada-search-ignore-string-comment
3077 "\\<declare\\|begin\\>" nil orgpoint)))
3078 (goto-char (car match-cons))
3079 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3081 ;; variable declaration
3082 ((ada-in-decl-p)
3083 (if (save-excursion
3084 (ada-search-ignore-string-comment ";" nil orgpoint))
3085 (list cur-indent 0)
3086 (list cur-indent 'ada-broken-indent)))
3088 ;; nothing follows colon
3090 (list cur-indent '(- ada-label-indent))))))
3092 (defun ada-get-indent-goto-label (orgpoint)
3093 "Calculate the indentation when at a goto label."
3094 (search-forward ">>")
3095 (ada-goto-next-non-ws)
3096 (if (>= (point) orgpoint)
3097 ;; labeled statement is the one we need to indent
3098 (list (- (point) ada-label-indent))
3099 ;; else indentation is indent for labeled statement
3100 (ada-indent-on-previous-lines t orgpoint)))
3102 (defun ada-get-indent-loop (orgpoint)
3103 "Calculate the indentation when just before a loop or a for ... use.
3104 ORGPOINT is the limit position used in the calculation."
3105 (let ((match-cons nil)
3106 (pos (point))
3108 ;; If looking at a named block, skip the label
3109 (label (save-excursion
3110 (back-to-indentation)
3111 (if (looking-at ada-block-label-re)
3112 (- ada-label-indent)
3113 0))))
3115 (cond
3118 ;; statement complete
3120 ((save-excursion
3121 (ada-search-ignore-string-comment ";" nil orgpoint nil
3122 'search-forward))
3123 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
3125 ;; simple loop
3127 ((looking-at "loop\\>")
3128 (setq pos (ada-get-indent-block-start orgpoint))
3129 (if (equal label 0)
3131 (list (+ (car pos) label) (cadr pos))))
3134 ;; 'for'- loop (or also a for ... use statement)
3136 ((looking-at "for\\>")
3137 (cond
3139 ;; for ... use
3141 ((save-excursion
3142 (and
3143 (goto-char (match-end 0))
3144 (ada-goto-next-non-ws orgpoint)
3145 (forward-word 1)
3146 (if (= (char-after) ?') (forward-word 1) t)
3147 (ada-goto-next-non-ws orgpoint)
3148 (looking-at "\\<use\\>")
3150 ;; check if there is a 'record' before point
3152 (progn
3153 (setq match-cons (ada-search-ignore-string-comment
3154 "record" nil orgpoint nil 'word-search-forward))
3155 t)))
3156 (if match-cons
3157 (progn
3158 (goto-char (car match-cons))
3159 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3160 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3164 ;; for..loop
3166 ((save-excursion
3167 (setq match-cons (ada-search-ignore-string-comment
3168 "loop" nil orgpoint nil 'word-search-forward)))
3169 (goto-char (car match-cons))
3171 ;; indent according to 'loop', if it's first in the line;
3172 ;; otherwise to 'for'
3174 (unless (save-excursion
3175 (back-to-indentation)
3176 (looking-at "\\<loop\\>"))
3177 (goto-char pos))
3178 (list (+ (save-excursion (back-to-indentation) (point)) label)
3179 'ada-indent))
3181 ;; for-statement is broken
3184 (list (+ (save-excursion (back-to-indentation) (point)) label)
3185 'ada-broken-indent))))
3188 ;; 'while'-loop
3190 ((looking-at "while\\>")
3192 ;; while..loop ?
3194 (if (save-excursion
3195 (setq match-cons (ada-search-ignore-string-comment
3196 "loop" nil orgpoint nil 'word-search-forward)))
3198 (progn
3199 (goto-char (car match-cons))
3201 ;; indent according to 'loop', if it's first in the line;
3202 ;; otherwise to 'while'.
3204 (unless (save-excursion
3205 (back-to-indentation)
3206 (looking-at "\\<loop\\>"))
3207 (goto-char pos))
3208 (list (+ (save-excursion (back-to-indentation) (point)) label)
3209 'ada-indent))
3211 (list (+ (save-excursion (back-to-indentation) (point)) label)
3212 'ada-broken-indent))))))
3214 (defun ada-get-indent-type (orgpoint)
3215 "Calculate the indentation when before a type statement.
3216 ORGPOINT is the limit position used in the calculation."
3217 (let ((match-dat nil))
3218 (cond
3220 ;; complete record declaration
3222 ((save-excursion
3223 (and
3224 (setq match-dat (ada-search-ignore-string-comment
3225 "end" nil orgpoint nil 'word-search-forward))
3226 (ada-goto-next-non-ws)
3227 (looking-at "\\<record\\>")
3228 (forward-word 1)
3229 (ada-goto-next-non-ws)
3230 (= (char-after) ?\;)))
3231 (goto-char (car match-dat))
3232 (list (save-excursion (back-to-indentation) (point)) 0))
3234 ;; record type
3236 ((save-excursion
3237 (setq match-dat (ada-search-ignore-string-comment
3238 "record" nil orgpoint nil 'word-search-forward)))
3239 (goto-char (car match-dat))
3240 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3242 ;; complete type declaration
3244 ((save-excursion
3245 (ada-search-ignore-string-comment ";" nil orgpoint nil
3246 'search-forward))
3247 (list (save-excursion (back-to-indentation) (point)) 0))
3249 ;; "type ... is", but not "type ... is ...", which is broken
3251 ((save-excursion
3252 (and
3253 (ada-search-ignore-string-comment "is" nil orgpoint nil
3254 'word-search-forward)
3255 (not (ada-goto-next-non-ws orgpoint))))
3256 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3258 ;; broken statement
3261 (list (save-excursion (back-to-indentation) (point))
3262 'ada-broken-indent)))))
3265 ;; -----------------------------------------------------------
3266 ;; -- searching and matching
3267 ;; -----------------------------------------------------------
3269 (defun ada-goto-stmt-start (&optional ignore-goto-label)
3270 "Move point to the beginning of the statement that point is in or after.
3271 Return the new position of point.
3272 As a special case, if we are looking at a closing parenthesis, skip to the
3273 open parenthesis."
3274 (let ((match-dat nil)
3275 (orgpoint (point)))
3277 (setq match-dat (ada-search-prev-end-stmt))
3278 (if match-dat
3281 ;; found a previous end-statement => check if anything follows
3283 (unless (looking-at "declare")
3284 (progn
3285 (unless (save-excursion
3286 (goto-char (cdr match-dat))
3287 (ada-goto-next-non-ws orgpoint ignore-goto-label))
3289 ;; nothing follows => it's the end-statement directly in
3290 ;; front of point => search again
3292 (setq match-dat (ada-search-prev-end-stmt)))
3294 ;; if found the correct end-statement => goto next non-ws
3296 (if match-dat
3297 (goto-char (cdr match-dat)))
3298 (ada-goto-next-non-ws)
3302 ;; no previous end-statement => we are at the beginning of the
3303 ;; accessible part of the buffer
3305 (progn
3306 (goto-char (point-min))
3308 ;; skip to the very first statement, if there is one
3310 (unless (ada-goto-next-non-ws orgpoint)
3311 (goto-char orgpoint))))
3312 (point)))
3315 (defun ada-search-prev-end-stmt ()
3316 "Move point to previous end statement.
3317 Return a cons cell whose car is the beginning and whose cdr
3318 is the end of the match."
3319 (let ((match-dat nil)
3320 (found nil))
3322 ;; search until found or beginning-of-buffer
3323 (while
3324 (and
3325 (not found)
3326 (setq match-dat (ada-search-ignore-string-comment
3327 ada-end-stmt-re t)))
3329 (goto-char (car match-dat))
3330 (unless (ada-in-open-paren-p)
3331 (cond
3333 ((and (looking-at
3334 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3335 (save-excursion
3336 (ada-goto-previous-word)
3337 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3338 (forward-word -1))
3340 ((looking-at "is")
3341 (setq found
3342 (and (save-excursion (ada-goto-previous-word)
3343 (ada-goto-previous-word)
3344 (not (looking-at "subtype")))
3346 (save-excursion (goto-char (cdr match-dat))
3347 (ada-goto-next-non-ws)
3348 ;; words that can go after an 'is'
3349 (not (looking-at
3350 (eval-when-compile
3351 (concat "\\<"
3352 (regexp-opt
3353 '("separate" "access" "array"
3354 "private" "abstract" "new") t)
3355 "\\>\\|("))))))))
3357 ((looking-at "private")
3358 (save-excursion
3359 (backward-word 1)
3360 (setq found (not (looking-at "is")))))
3363 (setq found t))
3366 (if found
3367 match-dat
3368 nil)))
3370 (defun ada-goto-next-non-ws (&optional limit skip-goto-label)
3371 "Skip to next non-whitespace character.
3372 Skips spaces, newlines and comments, and possibly goto labels.
3373 Return `point' if moved, nil if not.
3374 Stop the search at LIMIT.
3375 Do not call this function from within a string."
3376 (unless limit
3377 (setq limit (point-max)))
3378 (while (and (<= (point) limit)
3379 (or (progn (forward-comment 10000)
3380 (if (and (not (eobp))
3381 (save-excursion (forward-char 1)
3382 (ada-in-string-p)))
3383 (progn (forward-sexp 1) t)))
3384 (and skip-goto-label
3385 (looking-at ada-goto-label-re)
3386 (progn
3387 (goto-char (match-end 0))
3388 t)))))
3389 (if (< (point) limit)
3390 (point)
3391 nil)
3395 (defun ada-goto-stmt-end (&optional limit)
3396 "Move point to the end of the statement that point is in or before.
3397 Return the new position of point or nil if not found.
3398 Stop the search at LIMIT."
3399 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
3400 (point)
3401 nil))
3404 (defun ada-goto-next-word (&optional backward)
3405 "Move point to the beginning of the next word of Ada code.
3406 If BACKWARD is non-nil, jump to the beginning of the previous word.
3407 Return the new position of point or nil if not found."
3408 (let ((match-cons nil)
3409 (orgpoint (point)))
3410 (unless backward
3411 (skip-syntax-forward "w_"))
3412 (if (setq match-cons
3413 (ada-search-ignore-string-comment "\\sw\\|\\s_" backward nil t))
3415 ;; move to the beginning of the word found
3417 (progn
3418 (goto-char (car match-cons))
3419 (skip-syntax-backward "w_")
3420 (point))
3422 ;; if not found, restore old position of point
3424 (goto-char orgpoint)
3425 'nil)))
3428 (defun ada-check-matching-start (keyword)
3429 "Signal an error if matching block start is not KEYWORD.
3430 Moves point to the matching block start."
3431 (ada-goto-matching-start 0)
3432 (unless (looking-at (concat "\\<" keyword "\\>"))
3433 (error "Matching start is not '%s'" keyword)))
3436 (defun ada-check-defun-name (defun-name)
3437 "Check if the name of the matching defun really is DEFUN-NAME.
3438 Assumes point to be already positioned by `ada-goto-matching-start'.
3439 Moves point to the beginning of the declaration."
3441 ;; named block without a `declare'; ada-goto-matching-start leaves
3442 ;; point at start of 'begin' for a block.
3443 (if (save-excursion
3444 (ada-goto-previous-word)
3445 (looking-at (concat "\\<" defun-name "\\> *:")))
3446 t ; name matches
3447 ;; else
3449 ;; 'accept' or 'package' ?
3451 (unless (looking-at ada-subprog-start-re)
3452 (ada-goto-decl-start))
3454 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3456 (save-excursion
3458 ;; a named 'declare'-block ? => jump to the label
3460 (if (looking-at "\\<declare\\>")
3461 (progn
3462 (forward-comment -1)
3463 (backward-word 1))
3465 ;; no, => 'procedure'/'function'/'task'/'protected'
3467 (progn
3468 (forward-word 2)
3469 (backward-word 1)
3471 ;; skip 'body' 'type'
3473 (if (looking-at "\\<\\(body\\|type\\)\\>")
3474 (forward-word 1))
3475 (forward-sexp 1)
3476 (backward-sexp 1)))
3478 ;; should be looking-at the correct name
3480 (unless (looking-at (concat "\\<" defun-name "\\>"))
3481 (error "Matching defun has different name: %s"
3482 (buffer-substring (point)
3483 (progn (forward-sexp 1) (point))))))))
3485 (defun ada-goto-decl-start (&optional noerror)
3486 "Move point to the declaration start of the current construct.
3487 If NOERROR is non-nil, return nil if no match was found;
3488 otherwise throw error."
3489 (let ((nest-count 1)
3490 (regexp (eval-when-compile
3491 (concat "\\<"
3492 (regexp-opt
3493 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
3494 "\\>")))
3496 ;; first should be set to t if we should stop at the first
3497 ;; "begin" we encounter.
3498 (first t)
3499 (count-generic nil)
3500 (stop-at-when nil)
3503 ;; Ignore "when" most of the time, except if we are looking at the
3504 ;; beginning of a block (structure: case .. is
3505 ;; when ... =>
3506 ;; begin ...
3507 ;; exception ... )
3508 (if (looking-at "begin")
3509 (setq stop-at-when t))
3511 (if (or
3512 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3513 (save-excursion
3514 (ada-search-ignore-string-comment
3515 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
3516 (looking-at "generic")))
3517 (setq count-generic t))
3519 ;; search backward for interesting keywords
3520 (while (and
3521 (not (zerop nest-count))
3522 (ada-search-ignore-string-comment regexp t))
3524 ;; calculate nest-depth
3526 (cond
3528 ((looking-at "end")
3529 (ada-goto-matching-start 1 noerror)
3531 ;; In some case, two begin..end block can follow each other closely,
3532 ;; which we have to detect, as in
3533 ;; procedure P is
3534 ;; procedure Q is
3535 ;; begin
3536 ;; end;
3537 ;; begin -- here we should go to procedure, not begin
3538 ;; end
3540 (if (looking-at "begin")
3541 (let ((loop-again t))
3542 (save-excursion
3543 (while loop-again
3544 ;; If begin was just there as the beginning of a block
3545 ;; (with no declare) then do nothing, otherwise just
3546 ;; register that we have to find the statement that
3547 ;; required the begin
3549 (ada-search-ignore-string-comment
3550 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3553 (if (looking-at "end")
3554 (ada-goto-matching-start 1 noerror t)
3556 (setq loop-again nil)
3557 (unless (looking-at "begin")
3558 (setq nest-count (1+ nest-count))))
3562 ((looking-at "generic")
3563 (if count-generic
3564 (progn
3565 (setq first nil)
3566 (setq nest-count (1- nest-count)))))
3568 ((looking-at "if")
3569 (save-excursion
3570 (forward-word -1)
3571 (unless (looking-at "\\<end[ \t\n]*if\\>")
3572 (progn
3573 (setq nest-count (1- nest-count))
3574 (setq first nil)))))
3577 ((looking-at "declare\\|generic")
3578 (setq nest-count (1- nest-count))
3579 (setq first t))
3581 ((looking-at "is")
3582 ;; look for things to ignore
3585 ;; generic formal parameter
3586 (looking-at "is[ t]+<>")
3588 ;; A type definition, or a case statement. Note that the
3589 ;; goto-matching-start above on 'end record' leaves us at
3590 ;; 'record', not at 'type'.
3592 ;; We get to a case statement here by calling
3593 ;; 'ada-move-to-end' from inside a case statement; then
3594 ;; we are not ignoring 'when'.
3595 (save-excursion
3596 ;; Skip type discriminants or case argument function call param list
3597 (forward-comment -10000)
3598 (forward-char -1)
3599 (if (= (char-after) ?\))
3600 (progn
3601 (forward-char 1)
3602 (backward-sexp 1)
3603 (forward-comment -10000)
3605 ;; skip type or case argument name
3606 (skip-chars-backward "a-zA-Z0-9_.'")
3607 (ada-goto-previous-word)
3608 (and
3609 ;; if it's a protected type, it's the decl start we
3610 ;; are looking for; since we didn't see the 'end'
3611 ;; above, we are inside it.
3612 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3613 (save-match-data
3614 (ada-goto-previous-word)
3615 (not (looking-at "\\<protected\\>"))))
3616 ) ; end of type definition p
3618 ;; null procedure declaration
3619 (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
3620 );; end or
3621 ;; skip this construct
3623 ;; this is the right "is"
3624 (setq nest-count (1- nest-count))
3625 (setq first nil)))
3628 ((looking-at "new")
3629 (if (save-excursion
3630 (ada-goto-previous-word)
3631 (looking-at "is"))
3632 (goto-char (match-beginning 0))))
3634 ((and first
3635 (looking-at "begin"))
3636 (setq nest-count 0))
3638 ((looking-at "when")
3639 (save-excursion
3640 (forward-word -1)
3641 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3642 (progn
3643 (if stop-at-when
3644 (setq nest-count (1- nest-count)))
3645 ))))
3647 ((looking-at "begin")
3648 (setq first nil))
3651 (setq nest-count (1+ nest-count))
3652 (setq first nil)))
3654 );; end of loop
3656 ;; check if declaration-start is really found
3657 (if (and
3658 (zerop nest-count)
3659 (if (looking-at "is")
3660 (ada-search-ignore-string-comment ada-subprog-start-re t)
3661 (looking-at "declare\\|generic")))
3663 (if noerror nil
3664 (error "No matching proc/func/task/declare/package/protected")))
3667 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3668 "Move point to the beginning of a block-start.
3669 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3670 If NOERROR is non-nil, it only returns nil if no matching start was found.
3671 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3672 (let ((nest-count (if nest-level nest-level 0))
3673 (found nil)
3675 (last-was-begin '())
3676 ;; List all keywords encountered while traversing
3677 ;; something like '("end" "end" "begin")
3678 ;; This is removed from the list when "package", "procedure",...
3679 ;; are seen. The goal is to find whether a package has an elaboration
3680 ;; part
3682 (pos nil))
3684 ;; search backward for interesting keywords
3685 (while (and
3686 (not found)
3687 (ada-search-ignore-string-comment ada-matching-start-re t))
3689 (unless (and (looking-at "\\<record\\>")
3690 (save-excursion
3691 (forward-word -1)
3692 (looking-at "\\<null\\>")))
3693 (progn
3694 ;; calculate nest-depth
3695 (cond
3696 ;; found block end => increase nest depth
3697 ((looking-at "end")
3698 (push nil last-was-begin)
3699 (setq nest-count (1+ nest-count)))
3701 ;; found loop/select/record/case/if => check if it starts or
3702 ;; ends a block
3703 ((looking-at "loop\\|select\\|record\\|case\\|if")
3704 (setq pos (point))
3705 (save-excursion
3706 ;; check if keyword follows 'end'
3707 (ada-goto-previous-word)
3708 (if (looking-at "\\<end\\>[ \t]*[^;]")
3709 (progn
3710 ;; it ends a block => increase nest depth
3711 (setq nest-count (1+ nest-count)
3712 pos (point))
3713 (push nil last-was-begin))
3715 ;; it starts a block => decrease nest depth
3716 (setq nest-count (1- nest-count))
3718 ;; Some nested "begin .. end" blocks with no "declare"?
3719 ;; => remove those entries
3720 (while (car last-was-begin)
3721 (setq last-was-begin (cdr (cdr last-was-begin))))
3723 (setq last-was-begin (cdr last-was-begin))
3725 (goto-char pos)
3728 ;; found package start => check if it really is a block
3729 ((looking-at "package")
3730 (save-excursion
3731 ;; ignore if this is just a renames statement
3732 (let ((current (point))
3733 (pos (ada-search-ignore-string-comment
3734 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3735 (if pos
3736 (goto-char (car pos))
3737 (error (concat
3738 "No matching 'is' or 'renames' for 'package' at"
3739 " line "
3740 (number-to-string (count-lines 1 (1+ current)))))))
3741 (unless (looking-at "renames")
3742 (progn
3743 (forward-word 1)
3744 (ada-goto-next-non-ws)
3745 ;; ignore it if it is only a declaration with 'new'
3746 ;; We could have package Foo is new ....
3747 ;; or package Foo is separate;
3748 ;; or package Foo is begin null; end Foo
3749 ;; for elaboration code (elaboration)
3750 (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3751 (not (car last-was-begin)))
3752 (setq nest-count (1- nest-count))))))
3754 (setq last-was-begin (cdr last-was-begin))
3756 ;; found task start => check if it has a body
3757 ((looking-at "task")
3758 (save-excursion
3759 (forward-word 1)
3760 (ada-goto-next-non-ws)
3761 (cond
3762 ((looking-at "\\<body\\>"))
3763 ((looking-at "\\<type\\>")
3764 ;; In that case, do nothing if there is a "is"
3765 (forward-word 2);; skip "type"
3766 (ada-goto-next-non-ws);; skip type name
3768 ;; Do nothing if we are simply looking at a simple
3769 ;; "task type name;" statement with no block
3770 (unless (looking-at ";")
3771 (progn
3772 ;; Skip the parameters
3773 (if (looking-at "(")
3774 (ada-search-ignore-string-comment ")" nil))
3775 (let ((tmp (ada-search-ignore-string-comment
3776 "\\<\\(is\\|;\\)\\>" nil)))
3777 (if tmp
3778 (progn
3779 (goto-char (car tmp))
3780 (if (looking-at "is")
3781 (setq nest-count (1- nest-count)))))))))
3783 ;; Check if that task declaration had a block attached to
3784 ;; it (i.e do nothing if we have just "task name;")
3785 (unless (progn (forward-word 1)
3786 (looking-at "[ \t]*;"))
3787 (setq nest-count (1- nest-count))))))
3788 (setq last-was-begin (cdr last-was-begin))
3791 ((looking-at "declare")
3792 ;; remove entry for begin and end (include nested begin..end
3793 ;; groups)
3794 (setq last-was-begin (cdr last-was-begin))
3795 (let ((count 1))
3796 (while (and (> count 0))
3797 (if (equal (car last-was-begin) t)
3798 (setq count (1+ count))
3799 (setq count (1- count)))
3800 (setq last-was-begin (cdr last-was-begin))
3803 ((looking-at "protected")
3804 ;; Ignore if this is just a declaration
3805 (save-excursion
3806 (let ((pos (ada-search-ignore-string-comment
3807 "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil)))
3808 (if pos
3809 (goto-char (car pos)))
3810 (if (looking-at "is")
3811 ;; remove entry for end
3812 (setq last-was-begin (cdr last-was-begin)))))
3813 (setq nest-count (1- nest-count)))
3815 ((or (looking-at "procedure")
3816 (looking-at "function"))
3817 ;; Ignore if this is just a declaration
3818 (save-excursion
3819 (let ((pos (ada-search-ignore-string-comment
3820 "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil)))
3821 (if pos
3822 (goto-char (car pos)))
3823 (if (looking-at "is")
3824 ;; remove entry for begin and end
3825 (setq last-was-begin (cdr (cdr last-was-begin))))))
3828 ;; all the other block starts
3830 (push (looking-at "begin") last-was-begin)
3831 (setq nest-count (1- nest-count)))
3835 ;; match is found, if nest-depth is zero
3836 (setq found (zerop nest-count))))) ; end of loop
3838 (if (bobp)
3839 (point)
3840 (if found
3842 ;; match found => is there anything else to do ?
3844 (progn
3845 (cond
3847 ;; found 'if' => skip to 'then', if it's on a separate line
3848 ;; and GOTOTHEN is non-nil
3850 ((and
3851 gotothen
3852 (looking-at "if")
3853 (save-excursion
3854 (ada-search-ignore-string-comment "then" nil nil nil
3855 'word-search-forward)
3856 (back-to-indentation)
3857 (looking-at "\\<then\\>")))
3858 (goto-char (match-beginning 0)))
3861 ;; found 'do' => skip back to 'accept' or 'return'
3863 ((looking-at "do")
3864 (unless (ada-search-ignore-string-comment
3865 "\\<accept\\|return\\>" t)
3866 (error "Missing 'accept' or 'return' in front of 'do'"))))
3867 (point))
3869 (if noerror
3871 (error "No matching start"))))))
3874 (defun ada-goto-matching-end (&optional nest-level noerror)
3875 "Move point to the end of a block.
3876 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3877 If NOERROR is non-nil, it only returns nil if no matching start found."
3878 (let ((nest-count (or nest-level 0))
3879 (regex (eval-when-compile
3880 (concat "\\<"
3881 (regexp-opt '("end" "loop" "select" "begin" "case"
3882 "if" "task" "package" "record" "do"
3883 "procedure" "function") t)
3884 "\\>")))
3885 found
3888 ;; First is used for subprograms: they are generally handled
3889 ;; recursively, but of course we do not want to do that the
3890 ;; first time (see comment below about subprograms)
3891 (first (not (looking-at "declare"))))
3893 ;; If we are already looking at one of the keywords, this shouldn't count
3894 ;; in the nesting loop below, so we just make sure we don't count it.
3895 ;; "declare" is a special case because we need to look after the "begin"
3896 ;; keyword
3897 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3898 (forward-char 1))
3901 ;; search forward for interesting keywords
3903 (while (and
3904 (not found)
3905 (ada-search-ignore-string-comment regex nil))
3908 ;; calculate nest-depth
3910 (backward-word 1)
3911 (cond
3912 ;; procedures and functions need to be processed recursively, in
3913 ;; case they are defined in a declare/begin block, as in:
3914 ;; declare -- NL 0 (nested level)
3915 ;; A : Boolean;
3916 ;; procedure B (C : D) is
3917 ;; begin -- NL 1
3918 ;; null;
3919 ;; end B; -- NL 0, and we would exit
3920 ;; begin
3921 ;; end; -- we should exit here
3922 ;; processing them recursively avoids the need for any special
3923 ;; handling.
3924 ;; Nothing should be done if we have only the specs or a
3925 ;; generic instantiation.
3927 ((and (looking-at "\\<procedure\\|function\\>"))
3928 (if first
3929 (forward-word 1)
3931 (setq pos (point))
3932 (ada-search-ignore-string-comment "is\\|;")
3933 (if (= (char-before) ?s)
3934 (progn
3935 (ada-goto-next-non-ws)
3936 (unless (looking-at "\\<new\\>")
3937 (progn
3938 (goto-char pos)
3939 (ada-goto-matching-end 0 t)))))))
3941 ;; found block end => decrease nest depth
3942 ((looking-at "\\<end\\>")
3943 (setq nest-count (1- nest-count)
3944 found (<= nest-count 0))
3945 ;; skip the following keyword
3946 (if (progn
3947 (skip-chars-forward "end")
3948 (ada-goto-next-non-ws)
3949 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3950 (forward-word 1)))
3952 ;; found package start => check if it really starts a block, and is not
3953 ;; in fact a generic instantiation for instance
3954 ((looking-at "\\<package\\>")
3955 (ada-search-ignore-string-comment "is" nil nil nil
3956 'word-search-forward)
3957 (ada-goto-next-non-ws)
3958 ;; ignore and skip it if it is only a 'new' package
3959 (if (looking-at "\\<new\\>")
3960 (goto-char (match-end 0))
3961 (setq nest-count (1+ nest-count)
3962 found (<= nest-count 0))))
3964 ;; all the other block starts
3966 (if (not first)
3967 (setq nest-count (1+ nest-count)))
3968 (setq found (<= nest-count 0))
3969 (forward-word 1))) ; end of 'cond'
3971 (setq first nil))
3973 (if found
3975 (if noerror
3977 (error "No matching end")))
3981 (defun ada-search-ignore-string-comment
3982 (search-re &optional backward limit paramlists search-func)
3983 "Regexp-search for SEARCH-RE, ignoring comments, strings.
3984 Returns a cons cell of begin and end of match data or nil, if not found.
3985 If BACKWARD is non-nil, search backward; search forward otherwise.
3986 The search stops at pos LIMIT.
3987 If PARAMLISTS is nil, ignore parameter lists.
3988 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
3989 in case we are searching for a constant string.
3990 Point is moved at the beginning of the SEARCH-RE."
3991 (let (found
3992 begin
3994 parse-result)
3996 ;; FIXME: need to pass BACKWARD to search-func!
3997 (unless search-func
3998 (setq search-func (if backward 're-search-backward 're-search-forward)))
4001 ;; search until found or end-of-buffer
4002 ;; We have to test that we do not look further than limit
4004 (with-syntax-table ada-mode-symbol-syntax-table
4005 (while (and (not found)
4006 (or (not limit)
4007 (or (and backward (<= limit (point)))
4008 (>= limit (point))))
4009 (funcall search-func search-re limit 1))
4010 (setq begin (match-beginning 0))
4011 (setq end (match-end 0))
4012 (setq parse-result (parse-partial-sexp (point-at-bol) (point)))
4013 (cond
4015 ;; If inside a string, skip it (and the following comments)
4017 ((ada-in-string-p parse-result)
4018 (if (featurep 'xemacs)
4019 (search-backward "\"" nil t)
4020 (goto-char (nth 8 parse-result)))
4021 (unless backward (forward-sexp 1)))
4023 ;; If inside a comment, skip it (and the following comments)
4024 ;; There is a special code for comments at the end of the file
4026 ((ada-in-comment-p parse-result)
4027 (if (featurep 'xemacs)
4028 (progn
4029 (forward-line 1)
4030 (beginning-of-line)
4031 (forward-comment -1))
4032 (goto-char (nth 8 parse-result)))
4033 (unless backward
4034 ;; at the end of the file, it is not possible to skip a comment
4035 ;; so we just go at the end of the line
4036 (if (forward-comment 1)
4037 (progn
4038 (forward-comment 1000)
4039 (beginning-of-line))
4040 (end-of-line))))
4042 ;; directly in front of a comment => skip it, if searching forward
4044 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
4045 (unless backward (progn (forward-char -1) (forward-comment 1000))))
4048 ;; found a parameter-list but should ignore it => skip it
4050 ((and (not paramlists) (ada-in-paramlist-p))
4051 (if backward
4052 (search-backward "(" nil t)
4053 (search-forward ")" nil t)))
4055 ;; found what we were looking for
4058 (setq found t))))) ; end of loop
4060 (if found
4061 (cons begin end)
4062 nil)))
4064 ;; -------------------------------------------------------
4065 ;; -- Testing the position of the cursor
4066 ;; -------------------------------------------------------
4068 (defun ada-in-decl-p ()
4069 "Return t if point is inside a declarative part.
4070 Assumes point to be at the end of a statement."
4071 (or (ada-in-paramlist-p)
4072 (save-excursion
4073 (ada-goto-decl-start t))))
4076 (defun ada-looking-at-semi-or ()
4077 "Return t if looking at an 'or' following a semicolon."
4078 (save-excursion
4079 (and (looking-at "\\<or\\>")
4080 (progn
4081 (forward-word 1)
4082 (ada-goto-stmt-start)
4083 (looking-at "\\<or\\>")))))
4086 (defun ada-looking-at-semi-private ()
4087 "Return t if looking at the start of a private section in a package.
4088 Return nil if the private is part of the package name, as in
4089 'private package A is...' (this can only happen at top level)."
4090 (save-excursion
4091 (and (looking-at "\\<private\\>")
4092 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4094 ;; Make sure this is the start of a private section (ie after
4095 ;; a semicolon or just after the package declaration, but not
4096 ;; after a 'type ... is private' or 'is new ... with private'
4098 ;; Note that a 'private' statement at the beginning of the buffer
4099 ;; does not indicate a private section, since this is instead a
4100 ;; 'private procedure ...'
4101 (progn (forward-comment -1000)
4102 (and (not (bobp))
4103 (or (= (char-before) ?\;)
4104 (and (forward-word -3)
4105 (looking-at "\\<package\\>"))))))))
4108 (defun ada-in-paramlist-p ()
4109 "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
4110 (save-excursion
4111 (and
4112 (ada-search-ignore-string-comment "(\\|)" t nil t)
4113 ;; inside parentheses ?
4114 (= (char-after) ?\()
4116 ;; We could be looking at two things here:
4117 ;; operator definition: function "." (
4118 ;; subprogram definition: procedure .... (
4119 ;; Let's skip back over the first one
4120 (progn
4121 (skip-chars-backward " \t\n")
4122 (if (= (char-before) ?\")
4123 (backward-char 3)
4124 (backward-word 1))
4127 ;; and now over the second one
4128 (backward-word 1)
4130 ;; We should ignore the case when the reserved keyword is in a
4131 ;; comment (for instance, when we have:
4132 ;; -- .... package
4133 ;; Test (A)
4134 ;; we should return nil
4136 (not (ada-in-string-or-comment-p))
4138 ;; right keyword two words before parenthesis ?
4139 ;; Type is in this list because of discriminants
4140 ;; pragma is not, because the syntax is that of a subprogram call.
4141 (looking-at (eval-when-compile
4142 (concat "\\<\\("
4143 "procedure\\|function\\|body\\|"
4144 "task\\|entry\\|accept\\|"
4145 "access[ \t]+procedure\\|"
4146 "access[ \t]+function\\|"
4147 "type\\)\\>"))))))
4149 (defun ada-search-ignore-complex-boolean (regexp backwardp)
4150 "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
4151 If BACKWARDP is non-nil, search backward; search forward otherwise."
4152 (let (result)
4153 (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
4154 (save-excursion (forward-word -1)
4155 (looking-at "and then\\|or else"))))
4156 result))
4158 (defun ada-in-open-paren-p ()
4159 "Non-nil if in an open parenthesis.
4160 Return value is the position of the first non-ws behind the last unclosed
4161 parenthesis, or nil."
4162 (save-excursion
4163 (let ((parse (parse-partial-sexp
4164 (point)
4165 (or (car (ada-search-ignore-complex-boolean
4166 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4168 (point-min)))))
4170 (if (nth 1 parse)
4171 (progn
4172 (goto-char (1+ (nth 1 parse)))
4174 ;; Skip blanks, if they are not followed by a comment
4175 ;; See:
4176 ;; type A is ( Value_0,
4177 ;; Value_1);
4178 ;; type B is ( -- comment
4179 ;; Value_2);
4181 (if (or (not ada-indent-handle-comment-special)
4182 (not (looking-at "[ \t]+--")))
4183 (skip-chars-forward " \t"))
4185 (point))))))
4188 ;; -----------------------------------------------------------
4189 ;; -- Behavior Of TAB Key
4190 ;; -----------------------------------------------------------
4192 (defun ada-tab ()
4193 "Do indenting or tabbing according to `ada-tab-policy'.
4194 In Transient Mark mode, if the mark is active, operate on the contents
4195 of the region. Otherwise, operate only on the current line."
4196 (interactive)
4197 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
4198 ((eq ada-tab-policy 'indent-auto)
4199 (if (ada-region-selected)
4200 (ada-indent-region (region-beginning) (region-end))
4201 (ada-indent-current)))
4202 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4205 (defun ada-untab (_arg)
4206 "Delete leading indenting according to `ada-tab-policy'."
4207 ;; FIXME: ARG is ignored
4208 (interactive "P")
4209 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
4210 ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
4211 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4214 (defun ada-indent-current-function ()
4215 "Ada mode version of the `indent-line-function'."
4216 (interactive "*")
4217 (let ((starting-point (point-marker)))
4218 (beginning-of-line)
4219 (ada-tab)
4220 (if (< (point) starting-point)
4221 (goto-char starting-point))
4222 (set-marker starting-point nil)
4225 (defun ada-tab-hard ()
4226 "Indent current line to next tab stop."
4227 (interactive)
4228 (save-excursion
4229 (beginning-of-line)
4230 (insert-char ? ada-indent))
4231 (if (bolp) (forward-char ada-indent)))
4233 (defun ada-untab-hard ()
4234 "Indent current line to previous tab stop."
4235 (interactive)
4236 (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent)))
4239 ;; ------------------------------------------------------------
4240 ;; -- Miscellaneous
4241 ;; ------------------------------------------------------------
4243 ;; Not needed any more for Emacs 21.2, but still needed for backward
4244 ;; compatibility
4245 (defun ada-remove-trailing-spaces ()
4246 "Remove trailing spaces in the whole buffer."
4247 (interactive)
4248 (save-match-data
4249 (save-excursion
4250 (save-restriction
4251 (widen)
4252 (goto-char (point-min))
4253 (while (re-search-forward "[ \t]+$" (point-max) t)
4254 (replace-match "" nil nil))))))
4256 (defun ada-gnat-style ()
4257 "Clean up comments, `(' and `,' for GNAT style checking switch."
4258 (interactive)
4259 (save-excursion
4261 ;; The \n is required, or the line after an empty comment line is
4262 ;; simply ignored.
4263 (goto-char (point-min))
4264 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
4265 (replace-match "-- \\1")
4266 (forward-line 1)
4267 (beginning-of-line))
4269 (goto-char (point-min))
4270 (while (re-search-forward "\\>(" nil t)
4271 (if (not (ada-in-string-or-comment-p))
4272 (replace-match " (")))
4273 (goto-char (point-min))
4274 (while (re-search-forward ";--" nil t)
4275 (forward-char -1)
4276 (if (not (ada-in-string-or-comment-p))
4277 (replace-match "; --")))
4278 (goto-char (point-min))
4279 (while (re-search-forward "([ \t]+" nil t)
4280 (if (not (ada-in-string-or-comment-p))
4281 (replace-match "(")))
4282 (goto-char (point-min))
4283 (while (re-search-forward ")[ \t]+)" nil t)
4284 (if (not (ada-in-string-or-comment-p))
4285 (replace-match "))")))
4286 (goto-char (point-min))
4287 (while (re-search-forward "\\>:" nil t)
4288 (if (not (ada-in-string-or-comment-p))
4289 (replace-match " :")))
4291 ;; Make sure there is a space after a ','.
4292 ;; Always go back to the beginning of the match, since otherwise
4293 ;; a statement like ('F','D','E') is incorrectly modified.
4294 (goto-char (point-min))
4295 (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
4296 (if (not (save-excursion
4297 (goto-char (match-beginning 0))
4298 (ada-in-string-or-comment-p)))
4299 (replace-match ", \\1")))
4301 ;; Operators should be surrounded by spaces.
4302 (goto-char (point-min))
4303 (while (re-search-forward
4304 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4305 nil t)
4306 (goto-char (match-beginning 1))
4307 (if (or (looking-at "--")
4308 (ada-in-string-or-comment-p))
4309 (progn
4310 (forward-line 1)
4311 (beginning-of-line))
4312 (cond
4313 ((string= (match-string 1) "/=")
4314 (replace-match " /= "))
4315 ((string= (match-string 1) "..")
4316 (replace-match " .. "))
4317 ((string= (match-string 1) "**")
4318 (replace-match " ** "))
4319 ((string= (match-string 1) ":=")
4320 (replace-match " := "))
4322 (replace-match " \\1 ")))
4323 (forward-char 1)))
4328 ;; -------------------------------------------------------------
4329 ;; -- Moving To Procedures/Packages/Statements
4330 ;; -------------------------------------------------------------
4332 (defun ada-move-to-start ()
4333 "Move point to the matching start of the current Ada structure."
4334 (interactive)
4335 (let ((pos (point)))
4336 (with-syntax-table ada-mode-symbol-syntax-table
4338 (save-excursion
4340 ;; do nothing if in string or comment or not on 'end ...;'
4341 ;; or if an error occurs during processing
4344 (ada-in-string-or-comment-p)
4345 (and (progn
4346 (or (looking-at "[ \t]*\\<end\\>")
4347 (backward-word 1))
4348 (or (looking-at "[ \t]*\\<end\\>")
4349 (backward-word 1))
4350 (or (looking-at "[ \t]*\\<end\\>")
4351 (error "Not on end ...;")))
4352 (ada-goto-matching-start 1)
4353 (setq pos (point))
4356 ;; on 'begin' => go on, according to user option
4358 ada-move-to-declaration
4359 (looking-at "\\<begin\\>")
4360 (ada-goto-decl-start)
4361 (setq pos (point))))
4363 ) ; end of save-excursion
4365 ;; now really move to the found position
4366 (goto-char pos))))
4368 (defun ada-move-to-end ()
4369 "Move point to the end of the block around point.
4370 Moves to 'begin' if in a declarative part."
4371 (interactive)
4372 (let ((pos (point))
4373 decl-start)
4374 (with-syntax-table ada-mode-symbol-syntax-table
4376 (save-excursion
4378 (cond
4379 ;; Go to the beginning of the current word, and check if we are
4380 ;; directly on 'begin'
4381 ((save-excursion
4382 (skip-syntax-backward "w")
4383 (looking-at "\\<begin\\>"))
4384 (ada-goto-matching-end 1))
4386 ;; on first line of subprogram body
4387 ;; Do nothing for specs or generic instantiation, since these are
4388 ;; handled as the general case (find the enclosing block)
4389 ;; We also need to make sure that we ignore nested subprograms
4390 ((save-excursion
4391 (and (skip-syntax-backward "w")
4392 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4393 (ada-search-ignore-string-comment "is\\|;")
4394 (not (= (char-before) ?\;))
4396 (skip-syntax-backward "w")
4397 (ada-goto-matching-end 0 t))
4399 ;; on first line of task declaration
4400 ((save-excursion
4401 (and (ada-goto-stmt-start)
4402 (looking-at "\\<task\\>" )
4403 (forward-word 1)
4404 (ada-goto-next-non-ws)
4405 (looking-at "\\<body\\>")))
4406 (ada-search-ignore-string-comment "begin" nil nil nil
4407 'word-search-forward))
4408 ;; accept block start
4409 ((save-excursion
4410 (and (ada-goto-stmt-start)
4411 (looking-at "\\<accept\\>" )))
4412 (ada-goto-matching-end 0))
4413 ;; package start
4414 ((save-excursion
4415 (setq decl-start (and (ada-goto-decl-start t) (point)))
4416 (and decl-start (looking-at "\\<package\\>")))
4417 (ada-goto-matching-end 1))
4419 ;; On a "declare" keyword
4420 ((save-excursion
4421 (skip-syntax-backward "w")
4422 (looking-at "\\<declare\\>"))
4423 (ada-goto-matching-end 0 t))
4425 ;; inside a 'begin' ... 'end' block
4426 (decl-start
4427 (goto-char decl-start)
4428 (ada-goto-matching-end 0 t))
4430 ;; (hopefully ;-) everything else
4432 (ada-goto-matching-end 1)))
4433 (setq pos (point))
4436 ;; now really move to the position found
4437 (goto-char pos))))
4439 (defun ada-next-procedure ()
4440 "Move point to next procedure."
4441 (interactive)
4442 (end-of-line)
4443 (if (re-search-forward ada-procedure-start-regexp nil t)
4444 (goto-char (match-beginning 4))
4445 (error "No more functions/procedures/tasks")))
4447 (defun ada-previous-procedure ()
4448 "Move point to previous procedure."
4449 (interactive)
4450 (beginning-of-line)
4451 (if (re-search-backward ada-procedure-start-regexp nil t)
4452 (goto-char (match-beginning 4))
4453 (error "No more functions/procedures/tasks")))
4455 (defun ada-next-package ()
4456 "Move point to next package."
4457 (interactive)
4458 (end-of-line)
4459 (if (re-search-forward ada-package-start-regexp nil t)
4460 (goto-char (match-beginning 1))
4461 (error "No more packages")))
4463 (defun ada-previous-package ()
4464 "Move point to previous package."
4465 (interactive)
4466 (beginning-of-line)
4467 (if (re-search-backward ada-package-start-regexp nil t)
4468 (goto-char (match-beginning 1))
4469 (error "No more packages")))
4472 ;; ------------------------------------------------------------
4473 ;; -- Define keymap and menus for Ada
4474 ;; -------------------------------------------------------------
4476 (defun ada-create-keymap ()
4477 "Create the keymap associated with the Ada mode."
4479 ;; All non-standard keys go into ada-mode-extra-map
4480 (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map)
4482 ;; Indentation and Formatting
4483 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
4484 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
4485 (define-key ada-mode-map "\t" 'ada-tab)
4486 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
4487 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
4488 (define-key ada-mode-map [(shift tab)] 'ada-untab)
4489 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
4490 ;; We don't want to make meta-characters case-specific.
4492 ;; Movement
4493 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
4494 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
4495 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
4496 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
4498 ;; Compilation
4499 (unless (lookup-key ada-mode-map "\C-c\C-c")
4500 (define-key ada-mode-map "\C-c\C-c" 'compile))
4502 ;; Casing
4503 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
4504 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
4505 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
4506 (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
4508 ;; On XEmacs, you can easily specify whether DEL should deletes
4509 ;; one character forward or one character backward. Take this into
4510 ;; account
4511 (define-key ada-mode-map
4512 (if (boundp 'delete-key-deletes-forward) [backspace] "\177")
4513 'backward-delete-char-untabify)
4515 ;; Make body
4516 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4518 ;; Use predefined function of Emacs19 for comments (RE)
4519 (define-key ada-mode-map "\C-c;" 'comment-region)
4520 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4522 ;; The following keys are bound to functions defined in ada-xref.el or
4523 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4524 ;; and activated only if the right compiler is used
4526 (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
4527 'ada-point-and-xref)
4528 (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
4530 (define-key ada-mode-extra-map "o" 'ff-find-other-file)
4531 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4532 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4533 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4534 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4535 (define-key ada-mode-extra-map "c" 'ada-change-prj)
4536 (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
4537 (define-key ada-mode-extra-map "g" 'ada-gdb-application)
4538 (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
4539 (define-key ada-mode-extra-map "r" 'ada-run-application)
4540 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4541 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4542 (define-key ada-mode-extra-map "l" 'ada-find-local-references)
4543 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4544 (define-key ada-mode-extra-map "f" 'ada-find-file)
4546 (define-key ada-mode-extra-map "u" 'ada-prj-edit)
4548 (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
4550 ;; The templates, defined in ada-stmt.el
4552 (let ((map (make-sparse-keymap)))
4553 (define-key map "h" 'ada-header)
4554 (define-key map "\C-a" 'ada-array)
4555 (define-key map "b" 'ada-exception-block)
4556 (define-key map "d" 'ada-declare-block)
4557 (define-key map "c" 'ada-case)
4558 (define-key map "\C-e" 'ada-elsif)
4559 (define-key map "e" 'ada-else)
4560 (define-key map "\C-k" 'ada-package-spec)
4561 (define-key map "k" 'ada-package-body)
4562 (define-key map "\C-p" 'ada-procedure-spec)
4563 (define-key map "p" 'ada-subprogram-body)
4564 (define-key map "\C-f" 'ada-function-spec)
4565 (define-key map "f" 'ada-for-loop)
4566 (define-key map "i" 'ada-if)
4567 (define-key map "l" 'ada-loop)
4568 (define-key map "\C-r" 'ada-record)
4569 (define-key map "\C-s" 'ada-subtype)
4570 (define-key map "S" 'ada-tabsize)
4571 (define-key map "\C-t" 'ada-task-spec)
4572 (define-key map "t" 'ada-task-body)
4573 (define-key map "\C-y" 'ada-type)
4574 (define-key map "\C-v" 'ada-private)
4575 (define-key map "u" 'ada-use)
4576 (define-key map "\C-u" 'ada-with)
4577 (define-key map "\C-w" 'ada-when)
4578 (define-key map "w" 'ada-while-loop)
4579 (define-key map "\C-x" 'ada-exception)
4580 (define-key map "x" 'ada-exit)
4581 (define-key ada-mode-extra-map "t" map))
4585 (defun ada-create-menu ()
4586 "Create the Ada menu as shown in the menu bar."
4587 (let ((m '("Ada"
4588 ("Help"
4589 ["Ada Mode" (info "ada-mode") t]
4590 ["GNAT User's Guide" (info "gnat_ugn")
4591 (eq ada-which-compiler 'gnat)]
4592 ["GNAT Reference Manual" (info "gnat_rm")
4593 (eq ada-which-compiler 'gnat)]
4594 ["Gcc Documentation" (info "gcc")
4595 (eq ada-which-compiler 'gnat)]
4596 ["Gdb Documentation" (info "gdb")
4597 (eq ada-which-compiler 'gnat)]
4598 ["Ada95 Reference Manual" (info "arm95") t])
4599 ("Options" :included (derived-mode-p 'ada-mode)
4600 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4601 :style toggle :selected ada-auto-case]
4602 ["Auto Indent After Return"
4603 (setq ada-indent-after-return (not ada-indent-after-return))
4604 :style toggle :selected ada-indent-after-return]
4605 ["Automatically Recompile For Cross-references"
4606 (setq ada-xref-create-ali (not ada-xref-create-ali))
4607 :style toggle :selected ada-xref-create-ali
4608 :included (eq ada-which-compiler 'gnat)]
4609 ["Confirm Commands"
4610 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4611 :style toggle :selected ada-xref-confirm-compile
4612 :included (eq ada-which-compiler 'gnat)]
4613 ["Show Cross-references In Other Buffer"
4614 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4615 :style toggle :selected ada-xref-other-buffer
4616 :included (eq ada-which-compiler 'gnat)]
4617 ["Tight Integration With GNU Visual Debugger"
4618 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4619 :style toggle :selected ada-tight-gvd-integration
4620 :included (string-match "gvd" ada-prj-default-debugger)])
4621 ["Customize" (customize-group 'ada)
4622 :included (fboundp 'customize-group)]
4623 ["Check file" ada-check-current t]
4624 ["Compile file" ada-compile-current t]
4625 ["Set main and Build" ada-set-main-compile-application t]
4626 ["Show main" ada-show-current-main t]
4627 ["Build" ada-compile-application t]
4628 ["Run" ada-run-application t]
4629 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4630 ["------" nil nil]
4631 ("Project"
4632 ["Show project" ada-show-current-project t]
4633 ["Load..." ada-set-default-project-file t]
4634 ["New..." ada-prj-new t]
4635 ["Edit..." ada-prj-edit t])
4636 ("Goto" :included (derived-mode-p 'ada-mode)
4637 ["Goto Declaration/Body" ada-goto-declaration
4638 (eq ada-which-compiler 'gnat)]
4639 ["Goto Body" ada-goto-body
4640 (eq ada-which-compiler 'gnat)]
4641 ["Goto Declaration Other Frame"
4642 ada-goto-declaration-other-frame
4643 (eq ada-which-compiler 'gnat)]
4644 ["Goto Previous Reference" ada-xref-goto-previous-reference
4645 (eq ada-which-compiler 'gnat)]
4646 ["List Local References" ada-find-local-references
4647 (eq ada-which-compiler 'gnat)]
4648 ["List References" ada-find-references
4649 (eq ada-which-compiler 'gnat)]
4650 ["Goto Reference To Any Entity" ada-find-any-references
4651 (eq ada-which-compiler 'gnat)]
4652 ["Goto Parent Unit" ada-goto-parent
4653 (eq ada-which-compiler 'gnat)]
4654 ["--" nil nil]
4655 ["Next compilation error" next-error t]
4656 ["Previous Package" ada-previous-package t]
4657 ["Next Package" ada-next-package t]
4658 ["Previous Procedure" ada-previous-procedure t]
4659 ["Next Procedure" ada-next-procedure t]
4660 ["Goto Start Of Statement" ada-move-to-start t]
4661 ["Goto End Of Statement" ada-move-to-end t]
4662 ["-" nil nil]
4663 ["Other File" ff-find-other-file t]
4664 ["Other File Other Window" ada-ff-other-window t])
4665 ("Edit" :included (derived-mode-p 'ada-mode)
4666 ["Search File On Source Path" ada-find-file t]
4667 ["------" nil nil]
4668 ["Complete Identifier" ada-complete-identifier t]
4669 ["-----" nil nil]
4670 ["Indent Line" ada-indent-current-function t]
4671 ["Justify Current Indentation" ada-justified-indent-current t]
4672 ["Indent Lines in Selection" ada-indent-region t]
4673 ["Indent Lines in File"
4674 (ada-indent-region (point-min) (point-max)) t]
4675 ["Format Parameter List" ada-format-paramlist t]
4676 ["-" nil nil]
4677 ["Comment Selection" comment-region t]
4678 ["Uncomment Selection" ada-uncomment-region t]
4679 ["--" nil nil]
4680 ["Fill Comment Paragraph" fill-paragraph t]
4681 ["Fill Comment Paragraph Justify"
4682 ada-fill-comment-paragraph-justify t]
4683 ["Fill Comment Paragraph Postfix"
4684 ada-fill-comment-paragraph-postfix t]
4685 ["---" nil nil]
4686 ["Adjust Case Selection" ada-adjust-case-region t]
4687 ["Adjust Case in File" ada-adjust-case-buffer t]
4688 ["Create Case Exception" ada-create-case-exception t]
4689 ["Create Case Exception Substring"
4690 ada-create-case-exception-substring t]
4691 ["Reload Case Exceptions" ada-case-read-exceptions t]
4692 ["----" nil nil]
4693 ["Make body for subprogram" ada-make-subprogram-body t]
4694 ["-----" nil nil]
4695 ["Narrow to subprogram" ada-narrow-to-defun t])
4696 ("Templates"
4697 :included (derived-mode-p 'ada-mode)
4698 ["Header" ada-header t]
4699 ["-" nil nil]
4700 ["Package Body" ada-package-body t]
4701 ["Package Spec" ada-package-spec t]
4702 ["Function Spec" ada-function-spec t]
4703 ["Procedure Spec" ada-procedure-spec t]
4704 ["Proc/func Body" ada-subprogram-body t]
4705 ["Task Body" ada-task-body t]
4706 ["Task Spec" ada-task-spec t]
4707 ["Declare Block" ada-declare-block t]
4708 ["Exception Block" ada-exception-block t]
4709 ["--" nil nil]
4710 ["Entry" ada-entry t]
4711 ["Entry family" ada-entry-family t]
4712 ["Select" ada-select t]
4713 ["Accept" ada-accept t]
4714 ["Or accept" ada-or-accept t]
4715 ["Or delay" ada-or-delay t]
4716 ["Or terminate" ada-or-terminate t]
4717 ["---" nil nil]
4718 ["Type" ada-type t]
4719 ["Private" ada-private t]
4720 ["Subtype" ada-subtype t]
4721 ["Record" ada-record t]
4722 ["Array" ada-array t]
4723 ["----" nil nil]
4724 ["If" ada-if t]
4725 ["Else" ada-else t]
4726 ["Elsif" ada-elsif t]
4727 ["Case" ada-case t]
4728 ["-----" nil nil]
4729 ["While Loop" ada-while-loop t]
4730 ["For Loop" ada-for-loop t]
4731 ["Loop" ada-loop t]
4732 ["------" nil nil]
4733 ["Exception" ada-exception t]
4734 ["Exit" ada-exit t]
4735 ["When" ada-when t])
4738 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4739 (if (featurep 'xemacs)
4740 (progn
4741 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4742 (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4745 ;; -------------------------------------------------------
4746 ;; Commenting/Uncommenting code
4747 ;; The following two calls are provided to enhance the standard
4748 ;; comment-region function, which only allows uncommenting if the
4749 ;; comment is at the beginning of a line. If the line have been re-indented,
4750 ;; we are unable to use comment-region, which makes no sense.
4752 ;; In addition, we provide an interface to the standard comment handling
4753 ;; function for justifying the comments.
4754 ;; -------------------------------------------------------
4756 (defadvice comment-region (before ada-uncomment-anywhere disable)
4757 (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
4758 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4759 (derived-mode-p 'ada-mode))
4760 (save-excursion
4761 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4762 (goto-char beg)
4763 (while (re-search-forward cs end t)
4764 (replace-match comment-start))
4765 ))))
4767 (defun ada-uncomment-region (beg end &optional arg)
4768 "Uncomment region BEG .. END.
4769 ARG gives number of comment characters."
4770 (interactive "r\nP")
4772 ;; This advice is not needed anymore with Emacs21. However, for older
4773 ;; versions, as well as for XEmacs, we still need to enable it.
4774 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4775 (progn
4776 (ad-activate 'comment-region)
4777 (comment-region beg end (- (or arg 2)))
4778 (ad-deactivate 'comment-region))
4779 (comment-region beg end (list (- (or arg 2))))
4780 (ada-indent-region beg end)))
4782 (defun ada-fill-comment-paragraph-justify ()
4783 "Fill current comment paragraph and justify each line as well."
4784 (interactive)
4785 (ada-fill-comment-paragraph 'full))
4787 (defun ada-fill-comment-paragraph-postfix ()
4788 "Fill current comment paragraph and justify each line as well.
4789 Adds `ada-fill-comment-postfix' at the end of each line."
4790 (interactive)
4791 (ada-fill-comment-paragraph 'full t))
4793 (defun ada-fill-comment-paragraph (&optional justify postfix)
4794 "Fill the current comment paragraph.
4795 If JUSTIFY is non-nil, each line is justified as well.
4796 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4797 to each line filled and justified.
4798 The paragraph is indented on the first line."
4799 (interactive "P")
4801 ;; check if inside comment or just in front a comment
4802 (if (and (not (ada-in-comment-p))
4803 (not (looking-at "[ \t]*--")))
4804 (error "Not inside comment"))
4806 (let* (indent from to
4807 (opos (point-marker))
4809 ;; Sets this variable to nil, otherwise it prevents
4810 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4811 (parse-sexp-lookup-properties nil)
4813 fill-prefix
4814 (fill-column (current-fill-column)))
4816 ;; Find end of paragraph
4817 (back-to-indentation)
4818 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4819 (forward-line 1)
4821 ;; If we were at the last line in the buffer, create a dummy empty
4822 ;; line at the end of the buffer.
4823 (if (eobp)
4824 (insert "\n")
4825 (back-to-indentation)))
4826 (beginning-of-line)
4827 (setq to (point-marker))
4828 (goto-char opos)
4830 ;; Find beginning of paragraph
4831 (back-to-indentation)
4832 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4833 (forward-line -1)
4834 (back-to-indentation))
4836 ;; We want one line above the first one, unless we are at the beginning
4837 ;; of the buffer
4838 (unless (bobp)
4839 (forward-line 1))
4840 (beginning-of-line)
4841 (setq from (point-marker))
4843 ;; Calculate the indentation we will need for the paragraph
4844 (back-to-indentation)
4845 (setq indent (current-column))
4846 ;; unindent the first line of the paragraph
4847 (delete-region from (point))
4849 ;; Remove the old postfixes
4850 (goto-char from)
4851 (while (re-search-forward "--\n" to t)
4852 (replace-match "\n"))
4854 (goto-char (1- to))
4855 (setq to (point-marker))
4857 ;; Indent and justify the paragraph
4858 (setq fill-prefix ada-fill-comment-prefix)
4859 (set-left-margin from to indent)
4860 (if postfix
4861 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4863 (fill-region-as-paragraph from to justify)
4865 ;; Add the postfixes if required
4866 (if postfix
4867 (save-restriction
4868 (goto-char from)
4869 (narrow-to-region from to)
4870 (while (not (eobp))
4871 (end-of-line)
4872 (insert-char ? (- fill-column (current-column)))
4873 (insert ada-fill-comment-postfix)
4874 (forward-line))
4877 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4878 ;; inserted at the end. Delete it
4879 (if (or (featurep 'xemacs)
4880 (<= emacs-major-version 19)
4881 (and (= emacs-major-version 20)
4882 (<= emacs-minor-version 2)))
4883 (progn
4884 (goto-char to)
4885 (end-of-line)
4886 (delete-char 1)))
4888 (goto-char opos)))
4891 ;; ---------------------------------------------------
4892 ;; support for find-file.el
4893 ;; These functions are used by find-file to guess the file names from
4894 ;; unit names, and to find the other file (spec or body) from the current
4895 ;; file (body or spec).
4896 ;; It is also used to find in which function we are, so as to put the
4897 ;; cursor at the correct position.
4898 ;; Standard Ada does not force any relation between unit names and file names,
4899 ;; so some of these functions can only be a good approximation. However, they
4900 ;; are also overridden in `ada-xref'.el when we know that the user is using
4901 ;; GNAT.
4902 ;; ---------------------------------------------------
4904 ;; Overridden when we work with GNAT, to use gnatkrunch
4905 (defun ada-make-filename-from-adaname (adaname)
4906 "Determine the filename in which ADANAME is found.
4907 This matches the GNAT default naming convention, except for
4908 pre-defined units."
4909 (while (string-match "\\." adaname)
4910 (setq adaname (replace-match "-" t t adaname)))
4911 (downcase adaname)
4914 (defun ada-other-file-name ()
4915 "Return the name of the other file.
4916 The name returned is the body if `current-buffer' is the spec,
4917 or the spec otherwise."
4919 (let ((is-spec nil)
4920 (is-body nil)
4921 (suffixes ada-spec-suffixes)
4922 (name (buffer-file-name)))
4924 ;; Guess whether we have a spec or a body, and get the basename of the
4925 ;; file. Since the extension may not start with '.', we can not use
4926 ;; file-name-extension
4927 (while (and (not is-spec)
4928 suffixes)
4929 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4930 (setq is-spec t
4931 name (match-string 1 name)))
4932 (setq suffixes (cdr suffixes)))
4934 (if (not is-spec)
4935 (progn
4936 (setq suffixes ada-body-suffixes)
4937 (while (and (not is-body)
4938 suffixes)
4939 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4940 (setq is-body t
4941 name (match-string 1 name)))
4942 (setq suffixes (cdr suffixes)))))
4944 ;; If this wasn't in either list, return name itself
4945 (if (not (or is-spec is-body))
4946 name
4948 ;; Else find the other possible names
4949 (if is-spec
4950 (setq suffixes ada-body-suffixes)
4951 (setq suffixes ada-spec-suffixes))
4952 (setq is-spec name)
4954 (while suffixes
4956 ;; If we are using project file, search for the other file in all
4957 ;; the possible src directories.
4959 (if (fboundp 'ada-find-src-file-in-dir)
4960 (let ((other
4961 (ada-find-src-file-in-dir
4962 (file-name-nondirectory (concat name (car suffixes))))))
4963 (if other
4964 (setq is-spec other)))
4966 ;; Else search in the current directory
4967 (if (file-exists-p (concat name (car suffixes)))
4968 (setq is-spec (concat name (car suffixes)))))
4969 (setq suffixes (cdr suffixes)))
4971 is-spec)))
4973 (defun ada-which-function-are-we-in ()
4974 "Return the name of the function whose definition/declaration point is in.
4975 Used in `ff-pre-load-hook'."
4976 (setq ff-function-name nil)
4977 (save-excursion
4978 (end-of-line);; make sure we get the complete name
4979 (or (if (re-search-backward ada-procedure-start-regexp nil t)
4980 (setq ff-function-name (match-string 5)))
4981 (if (re-search-backward ada-package-start-regexp nil t)
4982 (setq ff-function-name (match-string 4))))
4986 (defvar ada-last-which-function-line -1
4987 "Last line on which `ada-which-function' was called.")
4988 (defvar ada-last-which-function-subprog 0
4989 "Last subprogram name returned by `ada-which-function'.")
4990 (make-variable-buffer-local 'ada-last-which-function-subprog)
4991 (make-variable-buffer-local 'ada-last-which-function-line)
4994 (defun ada-which-function ()
4995 "Return the name of the function whose body the point is in.
4996 This function works even in the case of nested subprograms, whereas the
4997 standard Emacs function `which-function' does not.
4998 Since the search can be long, the results are cached."
5000 (let ((line (count-lines 1 (point)))
5001 (pos (point))
5002 end-pos
5003 func-name indent
5004 found)
5006 ;; If this is the same line as before, simply return the same result
5007 (if (= line ada-last-which-function-line)
5008 ada-last-which-function-subprog
5010 (save-excursion
5011 ;; In case the current line is also the beginning of the body
5012 (end-of-line)
5014 ;; Are we looking at "function Foo\n (paramlist)"
5015 (skip-chars-forward " \t\n(")
5017 (condition-case nil
5018 (up-list 1)
5019 (error nil))
5021 (skip-chars-forward " \t\n")
5022 (if (looking-at "return")
5023 (progn
5024 (forward-word 1)
5025 (skip-chars-forward " \t\n")
5026 (skip-chars-forward "a-zA-Z0-9_'")))
5028 ;; Can't simply do forward-word, in case the "is" is not on the
5029 ;; same line as the closing parenthesis
5030 (skip-chars-forward "is \t\n")
5032 ;; No look for the closest subprogram body that has not ended yet.
5033 ;; Not that we expect all the bodies to be finished by "end <name>",
5034 ;; or a simple "end;" indented in the same column as the start of
5035 ;; the subprogram. The goal is to be as efficient as possible.
5037 (while (and (not found)
5038 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5040 ;; Get the function name, but not the properties, or this changes
5041 ;; the face in the mode line on Emacs 21
5042 (setq func-name (match-string-no-properties 3))
5043 (if (and (not (ada-in-comment-p))
5044 (not (save-excursion
5045 (goto-char (match-end 0))
5046 (looking-at "[ \t\n]*new"))))
5047 (save-excursion
5048 (back-to-indentation)
5049 (setq indent (current-column))
5050 (if (ada-search-ignore-string-comment
5051 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5052 (make-string indent ? ) "end;"))
5053 (setq end-pos (point))
5054 (setq end-pos (point-max)))
5055 (if (>= end-pos pos)
5056 (setq found func-name))))
5058 (setq ada-last-which-function-line line
5059 ada-last-which-function-subprog found)
5060 found))))
5062 (defun ada-ff-other-window ()
5063 "Find other file in other window using `ff-find-other-file'."
5064 (interactive)
5065 (and (fboundp 'ff-find-other-file)
5066 (ff-find-other-file t)))
5068 (defun ada-set-point-accordingly ()
5069 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5070 (if ff-function-name
5071 (progn
5072 (goto-char (point-min))
5073 (unless (ada-search-ignore-string-comment
5074 (concat ff-function-name "\\b") nil)
5075 (goto-char (point-min))))))
5077 (defun ada-get-body-name (&optional spec-name)
5078 "Return the file name for the body of SPEC-NAME.
5079 If SPEC-NAME is nil, return the body for the current package.
5080 Return nil if no body was found."
5081 (interactive)
5083 (unless spec-name (setq spec-name (buffer-file-name)))
5085 ;; Remove the spec extension. We can not simply remove the file extension,
5086 ;; but we need to take into account the specific non-GNAT extensions that the
5087 ;; user might have specified.
5089 (let ((suffixes ada-spec-suffixes)
5090 end)
5091 (while suffixes
5092 (setq end (- (length spec-name) (length (car suffixes))))
5093 (if (string-equal (car suffixes) (substring spec-name end))
5094 (setq spec-name (substring spec-name 0 end)))
5095 (setq suffixes (cdr suffixes))))
5097 ;; If find-file.el was available, use its functions
5098 (if (fboundp 'ff-get-file-name)
5099 (ff-get-file-name ada-search-directories-internal
5100 (ada-make-filename-from-adaname
5101 (file-name-nondirectory
5102 (file-name-sans-extension spec-name)))
5103 ada-body-suffixes)
5104 ;; Else emulate it very simply
5105 (concat (ada-make-filename-from-adaname
5106 (file-name-nondirectory
5107 (file-name-sans-extension spec-name)))
5108 ".adb")))
5111 ;; ---------------------------------------------------
5112 ;; support for font-lock.el
5113 ;; Strings are a real pain in Ada because a single quote character is
5114 ;; overloaded as a string quote and type/instance delimiter. By default, a
5115 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5116 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5117 ;; syntax when the gods that created Ada determine them to be.
5119 ;; This only works in Emacs. See the comments before the grammar functions
5120 ;; at the beginning of this file for how this is done with XEmacs.
5121 ;; ----------------------------------------------------
5123 (defconst ada-font-lock-syntactic-keywords
5124 ;; Mark single quotes as having string quote syntax in 'c' instances.
5125 ;; We used to explicitly avoid ''' as a special case for fear the buffer
5126 ;; be highlighted as a string, but it seems this fear is unfounded.
5128 ;; This sets the properties of the characters, so that ada-in-string-p
5129 ;; correctly handles '"' too...
5130 '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5131 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5133 (defvar ada-font-lock-keywords
5134 (eval-when-compile
5135 (list
5137 ;; handle "type T is access function return S;"
5138 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5140 ;; preprocessor line
5141 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5144 ;; accept, entry, function, package (body), protected (body|type),
5145 ;; pragma, procedure, task (body) plus name.
5146 (list (concat
5147 "\\<\\("
5148 "accept\\|"
5149 "entry\\|"
5150 "function\\|"
5151 "package[ \t]+body\\|"
5152 "package\\|"
5153 "pragma\\|"
5154 "procedure\\|"
5155 "protected[ \t]+body\\|"
5156 "protected[ \t]+type\\|"
5157 "protected\\|"
5158 "task[ \t]+body\\|"
5159 "task[ \t]+type\\|"
5160 "task"
5161 "\\)\\>[ \t]*"
5162 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5163 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5165 ;; Optional keywords followed by a type name.
5166 (list (concat ; ":[ \t]*"
5167 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5168 "[ \t]*"
5169 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5170 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5173 ;; Main keywords, except those treated specially below.
5174 (concat "\\<"
5175 (regexp-opt
5176 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5177 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5178 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5179 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5180 "null" "or" "others" "overriding" "private" "protected" "raise"
5181 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5182 "select" "separate" "synchronized" "tagged" "task" "terminate"
5183 "then" "until" "when" "while" "with" "xor") t)
5184 "\\>")
5186 ;; Anything following end and not already fontified is a body name.
5187 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5188 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5190 ;; Keywords followed by a type or function name.
5191 (list (concat "\\<\\("
5192 "new\\|of\\|subtype\\|type"
5193 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5194 '(1 font-lock-keyword-face)
5195 '(2 (if (match-beginning 4)
5196 font-lock-function-name-face
5197 font-lock-type-face) nil t))
5199 ;; Keywords followed by a (comma separated list of) reference.
5200 ;; Note that font-lock only works on single lines, thus we can not
5201 ;; correctly highlight a with_clause that spans multiple lines.
5202 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5203 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5204 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
5207 ;; Goto tags.
5208 '("<<\\(\\sw+\\)>>" 1 font-lock-constant-face)
5210 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5211 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5213 ;; Ada unnamed numerical constants
5214 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5217 "Default expressions to highlight in Ada mode.")
5220 ;; ---------------------------------------------------------
5221 ;; Support for outline.el
5222 ;; ---------------------------------------------------------
5224 (defun ada-outline-level ()
5225 "This is so that `current-column' DTRT in otherwise-hidden text."
5226 ;; patch from Dave Love <fx@gnu.org>
5227 (let (buffer-invisibility-spec)
5228 (save-excursion
5229 (back-to-indentation)
5230 (current-column))))
5232 ;; ---------------------------------------------------------
5233 ;; Support for narrow-to-region
5234 ;; ---------------------------------------------------------
5236 (defun ada-narrow-to-defun (&optional _arg)
5237 "Make text outside current subprogram invisible.
5238 The subprogram visible is the one that contains or follow point.
5239 Optional ARG is ignored.
5240 Use \\[widen] to go back to the full visibility for the buffer."
5242 (interactive)
5243 (save-excursion
5244 (let (end)
5245 (widen)
5246 (forward-line 1)
5247 (ada-previous-procedure)
5248 (setq end (point-at-bol))
5249 (ada-move-to-end)
5250 (end-of-line)
5251 (narrow-to-region end (point))
5252 (message
5253 "Use M-x widen to get back to full visibility in the buffer"))))
5255 ;; ---------------------------------------------------------
5256 ;; Automatic generation of code
5257 ;; The Ada mode has a set of function to automatically generate a subprogram
5258 ;; or package body from its spec.
5259 ;; These function only use a primary and basic algorithm, this could use a
5260 ;; lot of improvement.
5261 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5262 ;; body.
5263 ;; ----------------------------------------------------------
5265 (defun ada-gen-treat-proc (match)
5266 "Make dummy body of a procedure/function specification.
5267 MATCH is a cons cell containing the start and end locations of the last search
5268 for `ada-procedure-start-regexp'."
5269 (goto-char (car match))
5270 (let (func-found procname functype)
5271 (cond
5272 ((or (looking-at "^[ \t]*procedure")
5273 (setq func-found (looking-at "^[ \t]*function")))
5274 ;; treat it as a proc/func
5275 (forward-word 2)
5276 (forward-word -1)
5277 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5279 ;; goto end of procname
5280 (goto-char (cdr match))
5282 ;; skip over parameterlist
5283 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5284 (forward-sexp))
5286 ;; if function, skip over 'return' and result type.
5287 (if func-found
5288 (progn
5289 (forward-word 1)
5290 (skip-chars-forward " \t\n")
5291 (setq functype (buffer-substring (point)
5292 (progn
5293 (skip-chars-forward
5294 "a-zA-Z0-9_\.")
5295 (point))))))
5296 ;; look for next non WS
5297 (cond
5298 ((looking-at "[ \t]*;")
5299 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5300 (ada-indent-newline-indent)
5301 (insert "is")
5302 (ada-indent-newline-indent)
5303 (if func-found
5304 (progn
5305 (insert "Result : " functype ";")
5306 (ada-indent-newline-indent)))
5307 (insert "begin")
5308 (ada-indent-newline-indent)
5309 (if func-found
5310 (insert "return Result;")
5311 (insert "null;"))
5312 (ada-indent-newline-indent)
5313 (insert "end " procname ";")
5314 (ada-indent-newline-indent)
5317 ((looking-at "[ \t\n]*is")
5318 ;; do nothing
5321 ((looking-at "[ \t\n]*rename")
5322 ;; do nothing
5326 (message "unknown syntax"))))
5328 (if (looking-at "^[ \t]*task")
5329 (progn
5330 (message "Task conversion is not yet implemented")
5331 (forward-word 2)
5332 (if (looking-at "[ \t]*;")
5333 (forward-line)
5334 (ada-move-to-end))
5335 ))))))
5337 (defun ada-make-body ()
5338 "Create an Ada package body in the current buffer.
5339 The spec must be the previously visited buffer.
5340 This function typically is to be hooked into `ff-file-created-hook'."
5341 (delete-region (point-min) (point-max))
5342 (insert-buffer-substring (car (cdr (buffer-list))))
5343 (goto-char (point-min))
5344 (ada-mode)
5346 (let (found ada-procedure-or-package-start-regexp)
5347 (if (setq found
5348 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5349 (progn (goto-char (cdr found))
5350 (insert " body")
5352 (error "No package"))
5354 (setq ada-procedure-or-package-start-regexp
5355 (concat ada-procedure-start-regexp
5356 "\\|"
5357 ada-package-start-regexp))
5359 (while (setq found
5360 (ada-search-ignore-string-comment
5361 ada-procedure-or-package-start-regexp nil))
5362 (progn
5363 (goto-char (car found))
5364 (if (looking-at ada-package-start-regexp)
5365 (progn (goto-char (cdr found))
5366 (insert " body"))
5367 (ada-gen-treat-proc found))))))
5370 (defun ada-make-subprogram-body ()
5371 "Create a dummy subprogram body in package body file from spec surrounding point."
5372 (interactive)
5373 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5374 (spec (match-beginning 0))
5375 body-file)
5376 (if found
5377 (progn
5378 (goto-char spec)
5379 (if (and (re-search-forward "(\\|;" nil t)
5380 (= (char-before) ?\())
5381 (progn
5382 (ada-search-ignore-string-comment ")" nil)
5383 (ada-search-ignore-string-comment ";" nil)))
5384 (setq spec (buffer-substring spec (point)))
5386 ;; If find-file.el was available, use its functions
5387 (setq body-file (ada-get-body-name))
5388 (if body-file
5389 (find-file body-file)
5390 (error "No body found for the package. Create it first"))
5392 (save-restriction
5393 (widen)
5394 (goto-char (point-max))
5395 (forward-comment -10000)
5396 (re-search-backward "\\<end\\>" nil t)
5397 ;; Move to the beginning of the elaboration part, if any
5398 (re-search-backward "^begin" nil t)
5399 (newline)
5400 (forward-char -1)
5401 (insert spec)
5402 (re-search-backward ada-procedure-start-regexp nil t)
5403 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5405 (error "Not in subprogram spec"))))
5407 ;; --------------------------------------------------------
5408 ;; Global initializations
5409 ;; --------------------------------------------------------
5411 ;; Create the keymap once and for all. If we do that in ada-mode,
5412 ;; the keys changed in the user's .emacs have to be modified
5413 ;; every time
5414 (ada-create-keymap)
5415 (ada-create-menu)
5417 ;; Add the default extensions (and set up speedbar)
5418 (ada-add-extensions ".ads" ".adb")
5419 ;; This two files are generated by GNAT when running with -gnatD
5420 (if (equal ada-which-compiler 'gnat)
5421 (ada-add-extensions ".ads.dg" ".adb.dg"))
5423 ;; Read the special cases for exceptions
5424 (ada-case-read-exceptions)
5426 ;; Setup auto-loading of the other Ada mode files.
5427 (autoload 'ada-change-prj "ada-xref" nil t)
5428 (autoload 'ada-check-current "ada-xref" nil t)
5429 (autoload 'ada-compile-application "ada-xref" nil t)
5430 (autoload 'ada-compile-current "ada-xref" nil t)
5431 (autoload 'ada-complete-identifier "ada-xref" nil t)
5432 (autoload 'ada-find-file "ada-xref" nil t)
5433 (autoload 'ada-find-any-references "ada-xref" nil t)
5434 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5435 (autoload 'ada-find-local-references "ada-xref" nil t)
5436 (autoload 'ada-find-references "ada-xref" nil t)
5437 (autoload 'ada-gdb-application "ada-xref" nil t)
5438 (autoload 'ada-goto-declaration "ada-xref" nil t)
5439 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5440 (autoload 'ada-goto-parent "ada-xref" nil t)
5441 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5442 (autoload 'ada-point-and-xref "ada-xref" nil t)
5443 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5444 (autoload 'ada-run-application "ada-xref" nil t)
5445 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5446 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5447 (autoload 'ada-set-main-compile-application "ada-xref" nil t)
5448 (autoload 'ada-show-current-main "ada-xref" nil t)
5450 (autoload 'ada-customize "ada-prj" nil t)
5451 (autoload 'ada-prj-edit "ada-prj" nil t)
5452 (autoload 'ada-prj-new "ada-prj" nil t)
5453 (autoload 'ada-prj-save "ada-prj" nil t)
5455 (autoload 'ada-array "ada-stmt" nil t)
5456 (autoload 'ada-case "ada-stmt" nil t)
5457 (autoload 'ada-declare-block "ada-stmt" nil t)
5458 (autoload 'ada-else "ada-stmt" nil t)
5459 (autoload 'ada-elsif "ada-stmt" nil t)
5460 (autoload 'ada-exception "ada-stmt" nil t)
5461 (autoload 'ada-exception-block "ada-stmt" nil t)
5462 (autoload 'ada-exit "ada-stmt" nil t)
5463 (autoload 'ada-for-loop "ada-stmt" nil t)
5464 (autoload 'ada-function-spec "ada-stmt" nil t)
5465 (autoload 'ada-header "ada-stmt" nil t)
5466 (autoload 'ada-if "ada-stmt" nil t)
5467 (autoload 'ada-loop "ada-stmt" nil t)
5468 (autoload 'ada-package-body "ada-stmt" nil t)
5469 (autoload 'ada-package-spec "ada-stmt" nil t)
5470 (autoload 'ada-private "ada-stmt" nil t)
5471 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5472 (autoload 'ada-record "ada-stmt" nil t)
5473 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5474 (autoload 'ada-subtype "ada-stmt" nil t)
5475 (autoload 'ada-tabsize "ada-stmt" nil t)
5476 (autoload 'ada-task-body "ada-stmt" nil t)
5477 (autoload 'ada-task-spec "ada-stmt" nil t)
5478 (autoload 'ada-type "ada-stmt" nil t)
5479 (autoload 'ada-use "ada-stmt" nil t)
5480 (autoload 'ada-when "ada-stmt" nil t)
5481 (autoload 'ada-while-loop "ada-stmt" nil t)
5482 (autoload 'ada-with "ada-stmt" nil t)
5484 ;;; provide ourselves
5485 (provide 'ada-mode)
5487 ;;; ada-mode.el ends here