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