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