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