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