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