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