]> code.delx.au - gnu-emacs/blob - lisp/progmodes/ada-mode.el
Fix problems caused by new implementation of sub-word mode
[gnu-emacs] / lisp / progmodes / ada-mode.el
1 ;;; ada-mode.el --- major-mode for editing Ada sources
2
3 ;; Copyright (C) 1994-1995, 1997-2016 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-strictly 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-strictly -1)
1341 (point))
1342 (goto-char aa-end)))))
1343
1344 (defun ada-region-selected ()
1345 "Should we operate on an active region?"
1346 (if (fboundp 'use-region-p)
1347 (use-region-p)
1348 (region-active-p)))
1349 \f
1350 ;;-----------------------------------------------------------------
1351 ;; auto-casing
1352 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1353 ;; functions to auto-case identifiers, keywords, ...
1354 ;; The basic rules for autocasing are defined through the variables
1355 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1356 ;; are references to the functions that will do the actual casing.
1357 ;;
1358 ;; However, in most cases, the user will want to define some exceptions to
1359 ;; these casing rules. This is done through a list of files, that contain
1360 ;; one word per line. These files are stored in `ada-case-exception-file'.
1361 ;; For backward compatibility, this variable can also be a string.
1362 ;;-----------------------------------------------------------------
1363
1364 (defun ada-save-exceptions-to-file (file-name)
1365 "Save the casing exception lists to the file FILE-NAME.
1366 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1367 (find-file (expand-file-name file-name))
1368 (erase-buffer)
1369 (mapc (lambda (x) (insert (car x) "\n"))
1370 (sort (copy-sequence ada-case-exception)
1371 (lambda(a b) (string< (car a) (car b)))))
1372 (mapc (lambda (x) (insert "*" (car x) "\n"))
1373 (sort (copy-sequence ada-case-exception-substring)
1374 (lambda(a b) (string< (car a) (car b)))))
1375 (save-buffer)
1376 (kill-buffer nil)
1377 )
1378
1379 (defun ada-create-case-exception (&optional word)
1380 "Define WORD as an exception for the casing system.
1381 If WORD is not given, then the current word in the buffer is used instead.
1382 The new word is added to the first file in `ada-case-exception-file'.
1383 The standard casing rules will no longer apply to this word."
1384 (interactive)
1385 (let ((file-name
1386 (cond ((stringp ada-case-exception-file)
1387 ada-case-exception-file)
1388 ((listp ada-case-exception-file)
1389 (car ada-case-exception-file))
1390 (t
1391 (error (concat "No exception file specified. "
1392 "See variable ada-case-exception-file"))))))
1393
1394 (unless word
1395 (with-syntax-table ada-mode-symbol-syntax-table
1396 (save-excursion
1397 (skip-syntax-backward "w")
1398 (setq word (buffer-substring-no-properties
1399 (point) (save-excursion (forward-word-strictly 1)
1400 (point)))))))
1401
1402 ;; Reread the exceptions file, in case it was modified by some other,
1403 (ada-case-read-exceptions-from-file file-name)
1404
1405 ;; If the word is already in the list, even with a different casing
1406 ;; we simply want to replace it.
1407 (if (and (not (equal ada-case-exception '()))
1408 (assoc-string word ada-case-exception t))
1409 (setcar (assoc-string word ada-case-exception t) word)
1410 (add-to-list 'ada-case-exception (cons word t)))
1411
1412 (ada-save-exceptions-to-file file-name)))
1413
1414 (defun ada-create-case-exception-substring (&optional word)
1415 "Define the substring WORD as an exception for the casing system.
1416 If WORD is not given, then the current word in the buffer is used instead,
1417 or the selected region if any is active.
1418 The new word is added to the first file in `ada-case-exception-file'.
1419 When auto-casing a word, this substring will be special-cased, unless the
1420 word itself has a special casing."
1421 (interactive)
1422 (let ((file-name
1423 (cond ((stringp ada-case-exception-file)
1424 ada-case-exception-file)
1425 ((listp ada-case-exception-file)
1426 (car ada-case-exception-file))
1427 (t
1428 (error (concat "No exception file specified. "
1429 "See variable ada-case-exception-file"))))))
1430
1431 ;; Find the substring to define as an exception. Order is: the parameter,
1432 ;; if any, or the selected region, or the word under the cursor
1433 (cond
1434 (word nil)
1435
1436 ((ada-region-selected)
1437 (setq word (buffer-substring-no-properties
1438 (region-beginning) (region-end))))
1439
1440 (t
1441 (let ((underscore-syntax (char-syntax ?_)))
1442 (unwind-protect
1443 (progn
1444 (modify-syntax-entry ?_ "." (syntax-table))
1445 (save-excursion
1446 (skip-syntax-backward "w")
1447 (setq word (buffer-substring-no-properties
1448 (point)
1449 (save-excursion (forward-word-strictly 1)
1450 (point))))))
1451 (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
1452 (syntax-table))))))
1453
1454 ;; Reread the exceptions file, in case it was modified by some other,
1455 (ada-case-read-exceptions-from-file file-name)
1456
1457 ;; If the word is already in the list, even with a different casing
1458 ;; we simply want to replace it.
1459 (if (and (not (equal ada-case-exception-substring '()))
1460 (assoc-string word ada-case-exception-substring t))
1461 (setcar (assoc-string word ada-case-exception-substring t) word)
1462 (add-to-list 'ada-case-exception-substring (cons word t))
1463 )
1464
1465 (ada-save-exceptions-to-file file-name)
1466
1467 (message "%s" (concat "Defining " word " as a casing exception"))))
1468
1469 (defun ada-case-read-exceptions-from-file (file-name)
1470 "Read the content of the casing exception file FILE-NAME."
1471 (if (file-readable-p (expand-file-name file-name))
1472 (let ((buffer (current-buffer)))
1473 (find-file (expand-file-name file-name))
1474 (set-syntax-table ada-mode-symbol-syntax-table)
1475 (widen)
1476 (goto-char (point-min))
1477 (while (not (eobp))
1478
1479 ;; If the item is already in the list, even with an other casing,
1480 ;; do not add it again. This way, the user can easily decide which
1481 ;; priority should be applied to each casing exception
1482 (let ((word (buffer-substring-no-properties
1483 (point) (save-excursion (forward-word-strictly 1)
1484 (point)))))
1485
1486 ;; Handling a substring ?
1487 (if (char-equal (string-to-char word) ?*)
1488 (progn
1489 (setq word (substring word 1))
1490 (unless (assoc-string word ada-case-exception-substring t)
1491 (add-to-list 'ada-case-exception-substring (cons word t))))
1492 (unless (assoc-string word ada-case-exception t)
1493 (add-to-list 'ada-case-exception (cons word t)))))
1494
1495 (forward-line 1))
1496 (kill-buffer nil)
1497 (set-buffer buffer)))
1498 )
1499
1500 (defun ada-case-read-exceptions ()
1501 "Read all the casing exception files from `ada-case-exception-file'."
1502 (interactive)
1503
1504 ;; Reinitialize the casing exception list
1505 (setq ada-case-exception '()
1506 ada-case-exception-substring '())
1507
1508 (cond ((stringp ada-case-exception-file)
1509 (ada-case-read-exceptions-from-file ada-case-exception-file))
1510
1511 ((listp ada-case-exception-file)
1512 (mapcar 'ada-case-read-exceptions-from-file
1513 ada-case-exception-file))))
1514
1515 (defun ada-adjust-case-substring ()
1516 "Adjust case of substrings in the previous word."
1517 (interactive)
1518 (let ((substrings ada-case-exception-substring)
1519 (max (point))
1520 (case-fold-search t)
1521 (underscore-syntax (char-syntax ?_))
1522 re)
1523
1524 (save-excursion
1525 (forward-word -1)
1526
1527 (unwind-protect
1528 (progn
1529 (modify-syntax-entry ?_ "." (syntax-table))
1530
1531 (while substrings
1532 (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
1533
1534 (save-excursion
1535 (while (re-search-forward re max t)
1536 (replace-match (caar substrings) t)))
1537 (setq substrings (cdr substrings))
1538 )
1539 )
1540 (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
1541 )))
1542
1543 (defun ada-adjust-case-identifier ()
1544 "Adjust case of the previous identifier.
1545 The auto-casing is done according to the value of `ada-case-identifier'
1546 and the exceptions defined in `ada-case-exception-file'."
1547 (interactive)
1548 (if (or (equal ada-case-exception '())
1549 (equal (char-after) ?_))
1550 (progn
1551 (funcall ada-case-identifier -1)
1552 (ada-adjust-case-substring))
1553
1554 (progn
1555 (let ((end (point))
1556 (start (save-excursion (skip-syntax-backward "w")
1557 (point)))
1558 match)
1559 ;; If we have an exception, replace the word by the correct casing
1560 (if (setq match (assoc-string (buffer-substring start end)
1561 ada-case-exception t))
1562
1563 (progn
1564 (delete-region start end)
1565 (insert (car match)))
1566
1567 ;; Else simply re-case the word
1568 (funcall ada-case-identifier -1)
1569 (ada-adjust-case-substring))))))
1570
1571 (defun ada-after-keyword-p ()
1572 "Return t if cursor is after a keyword that is not an attribute."
1573 (save-excursion
1574 (forward-word-strictly -1)
1575 (and (not (and (char-before)
1576 (or (= (char-before) ?_)
1577 (= (char-before) ?'))));; unless we have a _ or '
1578 (looking-at (concat ada-keywords "[^_]")))))
1579
1580 (defun ada-adjust-case (&optional force-identifier)
1581 "Adjust the case of the word before the character just typed.
1582 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1583 (if (not (bobp))
1584 (progn
1585 (forward-char -1)
1586 (if (and (not (bobp))
1587 ;; or if at the end of a character constant
1588 (not (and (eq (following-char) ?')
1589 (eq (char-before (1- (point))) ?')))
1590 ;; or if the previous character was not part of a word
1591 (eq (char-syntax (char-before)) ?w)
1592 ;; if in a string or a comment
1593 (not (ada-in-string-or-comment-p))
1594 ;; if in a numeric literal
1595 (not (ada-in-numeric-literal-p))
1596 )
1597 (if (save-excursion
1598 (forward-word -1)
1599 (or (= (point) (point-min))
1600 (backward-char 1))
1601 (= (following-char) ?'))
1602 (funcall ada-case-attribute -1)
1603 (if (and
1604 (not force-identifier) ; (MH)
1605 (ada-after-keyword-p))
1606 (funcall ada-case-keyword -1)
1607 (ada-adjust-case-identifier))))
1608 (forward-char 1)
1609 ))
1610 )
1611
1612 (defun ada-adjust-case-interactive (arg)
1613 "Adjust the case of the previous word, and process the character just typed.
1614 ARG is the prefix the user entered with \\[universal-argument]."
1615 (interactive "P")
1616
1617 (if ada-auto-case
1618 (let ((lastk last-command-event))
1619
1620 (with-syntax-table ada-mode-symbol-syntax-table
1621 (cond ((memq lastk '(?\n ?\r))
1622 ;; Horrible kludge.
1623 (insert " ")
1624 (ada-adjust-case)
1625 ;; horrible dekludge
1626 (delete-char -1)
1627 ;; some special keys and their bindings
1628 (cond
1629 ((eq lastk ?\n)
1630 (funcall ada-lfd-binding))
1631 ((eq lastk ?\r)
1632 (funcall ada-ret-binding))))
1633 ((eq lastk ?\C-i) (ada-tab))
1634 ;; Else just insert the character
1635 ((self-insert-command (prefix-numeric-value arg))))
1636 ;; if there is a keyword in front of the underscore
1637 ;; then it should be part of an identifier (MH)
1638 (if (eq lastk ?_)
1639 (ada-adjust-case t)
1640 (ada-adjust-case))))
1641
1642 ;; Else, no auto-casing
1643 (cond
1644 ((eq last-command-event ?\n)
1645 (funcall ada-lfd-binding))
1646 ((eq last-command-event ?\r)
1647 (funcall ada-ret-binding))
1648 (t
1649 (self-insert-command (prefix-numeric-value arg))))))
1650
1651 (defun ada-activate-keys-for-case ()
1652 ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
1653 "Modify the key bindings for all the keys that should readjust the casing."
1654 (interactive)
1655 ;; Save original key-bindings to allow swapping ret/lfd
1656 ;; when casing is activated.
1657 ;; The 'or ...' is there to be sure that the value will not
1658 ;; be changed again when Ada mode is called more than once
1659 (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
1660 (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
1661
1662 ;; Call case modifying function after certain keys.
1663 (mapcar (function (lambda(key) (define-key
1664 ada-mode-map
1665 (char-to-string key)
1666 'ada-adjust-case-interactive)))
1667 '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
1668 ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1669
1670 (defun ada-loose-case-word (&optional _arg)
1671 "Upcase first letter and letters following `_' in the following word.
1672 No other letter is modified.
1673 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1674 (interactive)
1675 (save-excursion
1676 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1677 (first t))
1678 (skip-syntax-backward "w")
1679 (while (and (or first (search-forward "_" end t))
1680 (< (point) end))
1681 (and first
1682 (setq first nil))
1683 (insert-char (upcase (following-char)) 1)
1684 (delete-char 1)))))
1685
1686 (defun ada-no-auto-case (&optional _arg)
1687 "Do nothing. ARG is ignored.
1688 This function can be used for the auto-casing variables in Ada mode, to
1689 adapt to unusual auto-casing schemes. Since it does nothing, you can for
1690 instance use it for `ada-case-identifier' if you don't want any special
1691 auto-casing for identifiers, whereas keywords have to be lower-cased.
1692 See also `ada-auto-case' to disable auto casing altogether."
1693 nil)
1694
1695 (defun ada-capitalize-word (&optional _arg)
1696 "Upcase first letter and letters following `_', lower case other letters.
1697 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1698 (interactive)
1699 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1700 (begin (save-excursion (skip-syntax-backward "w") (point))))
1701 (capitalize-region begin end)))
1702
1703 (defun ada-adjust-case-region (from to)
1704 "Adjust the case of all words in the region between FROM and TO.
1705 Attention: This function might take very long for big regions!"
1706 (interactive "*r")
1707 (let ((begin nil)
1708 (end nil)
1709 (keywordp nil)
1710 (attribp nil))
1711 (message "Adjusting case ...")
1712 (with-syntax-table ada-mode-symbol-syntax-table
1713 (save-excursion
1714 (goto-char to)
1715 ;;
1716 ;; loop: look for all identifiers, keywords, and attributes
1717 ;;
1718 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
1719 (setq end (match-end 1))
1720 (setq attribp
1721 (and (> (point) from)
1722 (save-excursion
1723 (forward-char -1)
1724 (setq attribp (looking-at "'.[^']")))))
1725 (or
1726 ;; do nothing if it is a string or comment
1727 (ada-in-string-or-comment-p)
1728 (progn
1729 ;;
1730 ;; get the identifier or keyword or attribute
1731 ;;
1732 (setq begin (point))
1733 (setq keywordp (looking-at ada-keywords))
1734 (goto-char end)
1735 ;;
1736 ;; casing according to user-option
1737 ;;
1738 (if attribp
1739 (funcall ada-case-attribute -1)
1740 (if keywordp
1741 (funcall ada-case-keyword -1)
1742 (ada-adjust-case-identifier)))
1743 (goto-char begin))))
1744 (message "Adjusting case ... Done")))))
1745
1746 (defun ada-adjust-case-buffer ()
1747 "Adjust the case of all words in the whole buffer.
1748 ATTENTION: This function might take very long for big buffers!"
1749 (interactive "*")
1750 (ada-adjust-case-region (point-min) (point-max)))
1751
1752 \f
1753 ;;--------------------------------------------------------------
1754 ;; Format Parameter Lists
1755 ;; Some special algorithms are provided to indent the parameter lists in
1756 ;; subprogram declarations. This is done in two steps:
1757 ;; - First parses the parameter list. The returned list has the following
1758 ;; format:
1759 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1760 ;; ... )
1761 ;; This is done in `ada-scan-paramlist'.
1762 ;; - Delete and recreate the parameter list in function
1763 ;; `ada-insert-paramlist'.
1764 ;; Both steps are called from `ada-format-paramlist'.
1765 ;; Note: Comments inside the parameter list are lost.
1766 ;; The syntax has to be correct, or the reformatting will fail.
1767 ;;--------------------------------------------------------------
1768
1769 (defun ada-format-paramlist ()
1770 "Reformat the parameter list point is in."
1771 (interactive)
1772 (let ((begin nil)
1773 (end nil)
1774 (delend nil)
1775 (paramlist nil))
1776 (with-syntax-table ada-mode-symbol-syntax-table
1777
1778 ;; check if really inside parameter list
1779 (or (ada-in-paramlist-p)
1780 (error "Not in parameter list"))
1781
1782 ;; find start of current parameter-list
1783 (ada-search-ignore-string-comment
1784 (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
1785 (down-list 1)
1786 (backward-char 1)
1787 (setq begin (point))
1788
1789 ;; find end of parameter-list
1790 (forward-sexp 1)
1791 (setq delend (point))
1792 (delete-char -1)
1793 (insert "\n")
1794
1795 ;; find end of last parameter-declaration
1796 (forward-comment -1000)
1797 (setq end (point))
1798
1799 ;; build a list of all elements of the parameter-list
1800 (setq paramlist (ada-scan-paramlist (1+ begin) end))
1801
1802 ;; delete the original parameter-list
1803 (delete-region begin delend)
1804
1805 ;; insert the new parameter-list
1806 (goto-char begin)
1807 (ada-insert-paramlist paramlist))))
1808
1809 (defun ada-scan-paramlist (begin end)
1810 "Scan the parameter list found in between BEGIN and END.
1811 Return the equivalent internal parameter list."
1812 (let ((paramlist (list))
1813 (param (list))
1814 (notend t)
1815 (apos nil)
1816 (epos nil)
1817 (semipos nil)
1818 (match-cons nil))
1819
1820 (goto-char begin)
1821
1822 ;; loop until end of last parameter
1823 (while notend
1824
1825 ;; find first character of parameter-declaration
1826 (ada-goto-next-non-ws)
1827 (setq apos (point))
1828
1829 ;; find last character of parameter-declaration
1830 (if (setq match-cons
1831 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
1832 (progn
1833 (setq epos (car match-cons))
1834 (setq semipos (cdr match-cons)))
1835 (setq epos end))
1836
1837 ;; read name(s) of parameter(s)
1838 (goto-char apos)
1839 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1840
1841 (setq param (list (match-string 1)))
1842 (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
1843
1844 ;; look for 'in'
1845 (setq apos (point))
1846 (setq param
1847 (append param
1848 (list
1849 (consp
1850 (ada-search-ignore-string-comment
1851 "in" nil epos t 'word-search-forward)))))
1852
1853 ;; look for 'out'
1854 (goto-char apos)
1855 (setq param
1856 (append param
1857 (list
1858 (consp
1859 (ada-search-ignore-string-comment
1860 "out" nil epos t 'word-search-forward)))))
1861
1862 ;; look for 'access'
1863 (goto-char apos)
1864 (setq param
1865 (append param
1866 (list
1867 (consp
1868 (ada-search-ignore-string-comment
1869 "access" nil epos t 'word-search-forward)))))
1870
1871 ;; skip 'in'/'out'/'access'
1872 (goto-char apos)
1873 (ada-goto-next-non-ws)
1874 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1875 (forward-word-strictly 1)
1876 (ada-goto-next-non-ws))
1877
1878 ;; read type of parameter
1879 ;; We accept spaces in the name, since some software like Rose
1880 ;; generates something like: "A : B 'Class"
1881 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1882 (setq param
1883 (append param
1884 (list (match-string 0))))
1885
1886 ;; read default-expression, if there is one
1887 (goto-char (setq apos (match-end 0)))
1888 (setq param
1889 (append param
1890 (list
1891 (if (setq match-cons
1892 (ada-search-ignore-string-comment
1893 ":=" nil epos t 'search-forward))
1894 (buffer-substring (car match-cons) epos)
1895 nil))))
1896
1897 ;; add this parameter-declaration to the list
1898 (setq paramlist (append paramlist (list param)))
1899
1900 ;; check if it was the last parameter
1901 (if (eq epos end)
1902 (setq notend nil)
1903 (goto-char semipos))
1904 )
1905 (reverse paramlist)))
1906
1907 (defun ada-insert-paramlist (paramlist)
1908 "Insert a formatted PARAMLIST in the buffer."
1909 (let ((i (length paramlist))
1910 (parlen 0)
1911 (typlen 0)
1912 (inp nil)
1913 (outp nil)
1914 (accessp nil)
1915 (column nil)
1916 (firstcol nil))
1917
1918 ;; loop until last parameter
1919 (while (not (zerop i))
1920 (setq i (1- i))
1921
1922 ;; get max length of parameter-name
1923 (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
1924
1925 ;; get max length of type-name
1926 (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
1927
1928 ;; is there any 'in' ?
1929 (setq inp (or inp (nth 1 (nth i paramlist))))
1930
1931 ;; is there any 'out' ?
1932 (setq outp (or outp (nth 2 (nth i paramlist))))
1933
1934 ;; is there any 'access' ?
1935 (setq accessp (or accessp (nth 3 (nth i paramlist))))
1936 )
1937
1938 ;; does paramlist already start on a separate line ?
1939 (if (save-excursion
1940 (re-search-backward "^.\\|[^ \t]" nil t)
1941 (looking-at "^."))
1942 ;; yes => re-indent it
1943 (progn
1944 (ada-indent-current)
1945 (save-excursion
1946 (if (looking-at "\\(is\\|return\\)")
1947 (replace-match " \\1"))))
1948
1949 ;; no => insert it where we are after removing any whitespace
1950 (fixup-whitespace)
1951 (save-excursion
1952 (cond
1953 ((looking-at "[ \t]*\\(\n\\|;\\)")
1954 (replace-match "\\1"))
1955 ((looking-at "[ \t]*\\(is\\|return\\)")
1956 (replace-match " \\1"))))
1957 (insert " "))
1958
1959 (insert "(")
1960 (ada-indent-current)
1961
1962 (setq firstcol (current-column))
1963 (setq i (length paramlist))
1964
1965 ;; loop until last parameter
1966 (while (not (zerop i))
1967 (setq i (1- i))
1968 (setq column firstcol)
1969
1970 ;; insert parameter-name, space and colon
1971 (insert (nth 0 (nth i paramlist)))
1972 (indent-to (+ column parlen 1))
1973 (insert ": ")
1974 (setq column (current-column))
1975
1976 ;; insert 'in' or space
1977 (if (nth 1 (nth i paramlist))
1978 (insert "in ")
1979 (if (and
1980 (or inp
1981 accessp)
1982 (not (nth 3 (nth i paramlist))))
1983 (insert " ")))
1984
1985 ;; insert 'out' or space
1986 (if (nth 2 (nth i paramlist))
1987 (insert "out ")
1988 (if (and
1989 (or outp
1990 accessp)
1991 (not (nth 3 (nth i paramlist))))
1992 (insert " ")))
1993
1994 ;; insert 'access'
1995 (if (nth 3 (nth i paramlist))
1996 (insert "access "))
1997
1998 (setq column (current-column))
1999
2000 ;; insert type-name and, if necessary, space and default-expression
2001 (insert (nth 4 (nth i paramlist)))
2002 (if (nth 5 (nth i paramlist))
2003 (progn
2004 (indent-to (+ column typlen 1))
2005 (insert (nth 5 (nth i paramlist)))))
2006
2007 ;; check if it was the last parameter
2008 (if (zerop i)
2009 (insert ")")
2010 ;; no => insert ';' and newline and indent
2011 (insert ";")
2012 (newline)
2013 (indent-to firstcol))
2014 )
2015
2016 ;; if anything follows, except semicolon, newline, is or return
2017 ;; put it in a new line and indent it
2018 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2019 (ada-indent-newline-indent))
2020 ))
2021
2022
2023 \f
2024 ;;;----------------------------------------------------------------
2025 ;; Indentation Engine
2026 ;; All indentations are indicated as a two-element string:
2027 ;; - position of reference in the buffer
2028 ;; - offset to indent from this position (can also be a symbol or a list
2029 ;; that are evaluated)
2030 ;; Thus the total indentation for a line is the column number of the reference
2031 ;; position plus whatever value the evaluation of the second element provides.
2032 ;; This mechanism is used so that the Ada mode can "explain" how the
2033 ;; indentation was calculated, by showing which variables were used.
2034 ;;
2035 ;; The indentation itself is done in only one pass: first we try to guess in
2036 ;; what context we are by looking at the following keyword or punctuation
2037 ;; sign. If nothing remarkable is found, just try to guess the indentation
2038 ;; based on previous lines.
2039 ;;
2040 ;; The relevant functions for indentation are:
2041 ;; - `ada-indent-region': Re-indent a region of text
2042 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2043 ;; calculation that were done
2044 ;; - `ada-indent-current': Re-indent the current line
2045 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2046 ;; based on the context (see above).
2047 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2048 ;; For efficiency, these functions do not check they are in the correct
2049 ;; context.
2050 ;;;----------------------------------------------------------------
2051
2052 (defun ada-indent-region (beg end)
2053 "Indent the region between BEG end END."
2054 (interactive "*r")
2055 (goto-char beg)
2056 (let ((block-done 0)
2057 (lines-remaining (count-lines beg end))
2058 (msg (format "%%4d out of %4d lines remaining ..."
2059 (count-lines beg end)))
2060 (endmark (copy-marker end)))
2061 ;; catch errors while indenting
2062 (while (< (point) endmark)
2063 (if (> block-done 39)
2064 (progn
2065 (setq lines-remaining (- lines-remaining block-done)
2066 block-done 0)
2067 (message msg lines-remaining)))
2068 (if (= (char-after) ?\n) nil
2069 (ada-indent-current))
2070 (forward-line 1)
2071 (setq block-done (1+ block-done)))
2072 (message "Indenting ... done")))
2073
2074 (defun ada-indent-newline-indent ()
2075 "Indent the current line, insert a newline and then indent the new line."
2076 (interactive "*")
2077 (ada-indent-current)
2078 (newline)
2079 (ada-indent-current))
2080
2081 (defun ada-indent-newline-indent-conditional ()
2082 "Insert a newline and indent it.
2083 The original line is re-indented if `ada-indent-after-return' is non-nil."
2084 (interactive "*")
2085 ;; If at end of buffer (entering brand new code), some indentation
2086 ;; fails. For example, a block label requires whitespace following
2087 ;; the : to be recognized. So we do the newline first, then
2088 ;; go back and indent the original line.
2089 (newline)
2090 (if ada-indent-after-return
2091 (progn
2092 (forward-char -1)
2093 (ada-indent-current)
2094 (forward-char 1)))
2095 (ada-indent-current))
2096
2097 (defun ada-justified-indent-current ()
2098 "Indent the current line and explain how the calculation was done."
2099 (interactive)
2100
2101 (let ((cur-indent (ada-indent-current)))
2102
2103 (let ((line (save-excursion
2104 (goto-char (car cur-indent))
2105 (count-lines 1 (point)))))
2106
2107 (if (equal (cdr cur-indent) '(0))
2108 (message (concat "same indentation as line " (number-to-string line)))
2109 (message "%s" (mapconcat (lambda(x)
2110 (cond
2111 ((symbolp x)
2112 (symbol-name x))
2113 ((numberp x)
2114 (number-to-string x))
2115 ((listp x)
2116 (concat "- " (symbol-name (cadr x))))
2117 ))
2118 (cdr cur-indent)
2119 " + "))))
2120 (save-excursion
2121 (goto-char (car cur-indent))
2122 (sit-for 1))))
2123
2124 (defun ada-batch-reformat ()
2125 "Re-indent and re-case all the files found on the command line.
2126 This function should be used from the command line, with a
2127 command like:
2128 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2129
2130 (while command-line-args-left
2131 (let ((source (car command-line-args-left)))
2132 (message "Formatting %s" source)
2133 (find-file source)
2134 (ada-indent-region (point-min) (point-max))
2135 (ada-adjust-case-buffer)
2136 (write-file source))
2137 (setq command-line-args-left (cdr command-line-args-left)))
2138 (message "Done")
2139 (kill-emacs 0))
2140
2141 (defsubst ada-goto-previous-word ()
2142 "Move point to the beginning of the previous word of Ada code.
2143 Return the new position of point or nil if not found."
2144 (ada-goto-next-word t))
2145
2146 (defun ada-indent-current ()
2147 "Indent current line as Ada code.
2148 Return the calculation that was done, including the reference point
2149 and the offset."
2150 (interactive)
2151 (let ((orgpoint (point-marker))
2152 cur-indent tmp-indent
2153 prev-indent)
2154
2155 (unwind-protect
2156 (with-syntax-table ada-mode-symbol-syntax-table
2157
2158 ;; This needs to be done here so that the advice is not always
2159 ;; activated (this might interact badly with other modes)
2160 (if (featurep 'xemacs)
2161 (ad-activate 'parse-partial-sexp t))
2162
2163 (save-excursion
2164 (setq cur-indent
2165
2166 ;; Not First line in the buffer ?
2167 (if (save-excursion (zerop (forward-line -1)))
2168 (progn
2169 (back-to-indentation)
2170 (ada-get-current-indent))
2171
2172 ;; first line in the buffer
2173 (list (point-min) 0))))
2174
2175 ;; Evaluate the list to get the column to indent to
2176 ;; prev-indent contains the column to indent to
2177 (if cur-indent
2178 (setq prev-indent (save-excursion (goto-char (car cur-indent))
2179 (current-column))
2180 tmp-indent (cdr cur-indent))
2181 (setq prev-indent 0 tmp-indent '()))
2182
2183 (while (not (null tmp-indent))
2184 (cond
2185 ((numberp (car tmp-indent))
2186 (setq prev-indent (+ prev-indent (car tmp-indent))))
2187 (t
2188 (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
2189 )
2190 (setq tmp-indent (cdr tmp-indent)))
2191
2192 ;; only re-indent if indentation is different then the current
2193 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
2194 nil
2195 (beginning-of-line)
2196 (delete-horizontal-space)
2197 (indent-to prev-indent))
2198 ;;
2199 ;; restore position of point
2200 ;;
2201 (goto-char orgpoint)
2202 (if (< (current-column) (current-indentation))
2203 (back-to-indentation)))
2204
2205 (if (featurep 'xemacs)
2206 (ad-deactivate 'parse-partial-sexp)))
2207
2208 cur-indent))
2209
2210 (defun ada-get-current-indent ()
2211 "Return the indentation to use for the current line."
2212 (let (column
2213 pos
2214 match-cons
2215 result
2216 (orgpoint (save-excursion
2217 (beginning-of-line)
2218 (forward-comment -10000)
2219 (forward-line 1)
2220 (point))))
2221
2222 (setq result
2223 (cond
2224
2225 ;;-----------------------------
2226 ;; in open parenthesis, but not in parameter-list
2227 ;;-----------------------------
2228
2229 ((and ada-indent-to-open-paren
2230 (not (ada-in-paramlist-p))
2231 (setq column (ada-in-open-paren-p)))
2232
2233 ;; check if we have something like this (Table_Component_Type =>
2234 ;; Source_File_Record)
2235 (save-excursion
2236
2237 ;; Align the closing parenthesis on the opening one
2238 (if (= (following-char) ?\))
2239 (save-excursion
2240 (goto-char column)
2241 (skip-chars-backward " \t")
2242 (list (1- (point)) 0))
2243
2244 (if (and (skip-chars-backward " \t")
2245 (= (char-before) ?\n)
2246 (not (forward-comment -10000))
2247 (= (char-before) ?>))
2248 ;; ??? Could use a different variable
2249 (list column 'ada-broken-indent)
2250
2251 ;; We want all continuation lines to be indented the same
2252 ;; (ada-broken-line from the opening parenthesis. However, in
2253 ;; parameter list, each new parameter should be indented at the
2254 ;; column as the opening parenthesis.
2255
2256 ;; A special case to handle nested boolean expressions, as in
2257 ;; ((B
2258 ;; and then C) -- indented by ada-broken-indent
2259 ;; or else D) -- indenting this line.
2260 ;; ??? This is really a hack, we should have a proper way to go to
2261 ;; ??? the beginning of the statement
2262
2263 (if (= (char-before) ?\))
2264 (backward-sexp))
2265
2266 (if (memq (char-before) '(?, ?\; ?\( ?\)))
2267 (list column 0)
2268 (list column 'ada-continuation-indent)
2269 )))))
2270
2271 ;;---------------------------
2272 ;; at end of buffer
2273 ;;---------------------------
2274
2275 ((not (char-after))
2276 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2277
2278 ;;---------------------------
2279 ;; starting with e
2280 ;;---------------------------
2281
2282 ((= (downcase (char-after)) ?e)
2283 (cond
2284
2285 ;; ------- end ------
2286
2287 ((looking-at "end\\>")
2288 (let ((label 0)
2289 limit)
2290 (save-excursion
2291 (ada-goto-matching-start 1)
2292
2293 ;;
2294 ;; found 'loop' => skip back to 'while' or 'for'
2295 ;; if 'loop' is not on a separate line
2296 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2297 ;;
2298 (if (save-excursion
2299 (beginning-of-line)
2300 (looking-at ".+\\<loop\\>"))
2301 (progn
2302 (save-excursion
2303 (setq limit (car (ada-search-ignore-string-comment ";" t))))
2304 (if (save-excursion
2305 (and
2306 (setq match-cons
2307 (ada-search-ignore-string-comment ada-loop-start-re t limit))
2308 (not (looking-at "\\<loop\\>"))))
2309 (progn
2310 (goto-char (car match-cons))
2311 (save-excursion
2312 (back-to-indentation)
2313 (if (looking-at ada-block-label-re)
2314 (setq label (- ada-label-indent))))))))
2315
2316 ;; found 'record' =>
2317 ;; if the keyword is found at the beginning of a line (or just
2318 ;; after limited, we indent on it, otherwise we indent on the
2319 ;; beginning of the type declaration)
2320 ;; type A is (B : Integer;
2321 ;; C : Integer) is record
2322 ;; end record; -- This is badly indented otherwise
2323 (if (looking-at "record")
2324 (if (save-excursion
2325 (beginning-of-line)
2326 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2327 (list (save-excursion (back-to-indentation) (point)) 0)
2328 (list (save-excursion
2329 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2330 0))
2331
2332 ;; Else keep the same indentation as the beginning statement
2333 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
2334
2335 ;; ------ exception ----
2336
2337 ((looking-at "exception\\>")
2338 (save-excursion
2339 (ada-goto-matching-start 1)
2340 (list (save-excursion (back-to-indentation) (point)) 0)))
2341
2342 ;; else
2343
2344 ((looking-at "else\\>")
2345 (if (save-excursion (ada-goto-previous-word)
2346 (looking-at "\\<or\\>"))
2347 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2348 (save-excursion
2349 (ada-goto-matching-start 1 nil t)
2350 (list (progn (back-to-indentation) (point)) 0))))
2351
2352 ;; elsif
2353
2354 ((looking-at "elsif\\>")
2355 (save-excursion
2356 (ada-goto-matching-start 1 nil t)
2357 (list (progn (back-to-indentation) (point)) 0)))
2358
2359 ))
2360
2361 ;;---------------------------
2362 ;; starting with w (when)
2363 ;;---------------------------
2364
2365 ((and (= (downcase (char-after)) ?w)
2366 (looking-at "when\\>"))
2367 (save-excursion
2368 (ada-goto-matching-start 1)
2369 (list (save-excursion (back-to-indentation) (point))
2370 'ada-when-indent)))
2371
2372 ;;---------------------------
2373 ;; starting with t (then)
2374 ;;---------------------------
2375
2376 ((and (= (downcase (char-after)) ?t)
2377 (looking-at "then\\>"))
2378 (if (save-excursion (ada-goto-previous-word)
2379 (looking-at "and\\>"))
2380 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2381 (save-excursion
2382 ;; Select has been added for the statement: "select ... then abort"
2383 (ada-search-ignore-string-comment
2384 "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
2385 (list (progn (back-to-indentation) (point))
2386 'ada-stmt-end-indent))))
2387
2388 ;;---------------------------
2389 ;; starting with l (loop)
2390 ;;---------------------------
2391
2392 ((and (= (downcase (char-after)) ?l)
2393 (looking-at "loop\\>"))
2394 (setq pos (point))
2395 (save-excursion
2396 (goto-char (match-end 0))
2397 (ada-goto-stmt-start)
2398 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2399 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2400 (unless (looking-at ada-loop-start-re)
2401 (ada-search-ignore-string-comment ada-loop-start-re
2402 nil pos))
2403 (if (looking-at "\\<loop\\>")
2404 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2405 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
2406
2407 ;;----------------------------
2408 ;; starting with l (limited) or r (record)
2409 ;;----------------------------
2410
2411 ((or (and (= (downcase (char-after)) ?l)
2412 (looking-at "limited\\>"))
2413 (and (= (downcase (char-after)) ?r)
2414 (looking-at "record\\>")))
2415
2416 (save-excursion
2417 (ada-search-ignore-string-comment
2418 "\\<\\(type\\|use\\)\\>" t nil)
2419 (if (looking-at "\\<use\\>")
2420 (ada-search-ignore-string-comment "for" t nil nil
2421 'word-search-backward))
2422 (list (progn (back-to-indentation) (point))
2423 'ada-indent-record-rel-type)))
2424
2425 ;;---------------------------
2426 ;; starting with b (begin)
2427 ;;---------------------------
2428
2429 ((and (= (downcase (char-after)) ?b)
2430 (looking-at "begin\\>"))
2431 (save-excursion
2432 (if (ada-goto-decl-start t)
2433 (list (progn (back-to-indentation) (point)) 0)
2434 (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2435
2436 ;;---------------------------
2437 ;; starting with i (is)
2438 ;;---------------------------
2439
2440 ((and (= (downcase (char-after)) ?i)
2441 (looking-at "is\\>"))
2442
2443 (if (and ada-indent-is-separate
2444 (save-excursion
2445 (goto-char (match-end 0))
2446 (ada-goto-next-non-ws (point-at-eol))
2447 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2448 (save-excursion
2449 (ada-goto-stmt-start)
2450 (list (progn (back-to-indentation) (point)) 'ada-indent))
2451 (save-excursion
2452 (ada-goto-stmt-start)
2453 (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
2454 (list (progn (back-to-indentation) (point)) 0)
2455 (list (progn (back-to-indentation) (point)) 'ada-indent)))))
2456
2457 ;;---------------------------
2458 ;; starting with r (return, renames)
2459 ;;---------------------------
2460
2461 ((and (= (downcase (char-after)) ?r)
2462 (looking-at "re\\(turn\\|names\\)\\>"))
2463
2464 (save-excursion
2465 (let ((var 'ada-indent-return))
2466 ;; If looking at a renames, skip the 'return' statement too
2467 (if (looking-at "renames")
2468 (let (pos)
2469 (save-excursion
2470 (setq pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
2471 (if (and pos
2472 (= (downcase (char-after (car pos))) ?r))
2473 (goto-char (car pos)))
2474 (setq var 'ada-indent-renames)))
2475
2476 (forward-comment -1000)
2477 (if (= (char-before) ?\))
2478 (forward-sexp -1)
2479 (forward-word-strictly -1))
2480
2481 ;; If there is a parameter list, and we have a function declaration
2482 ;; or a access to subprogram declaration
2483 (let ((num-back 1))
2484 (if (and (= (following-char) ?\()
2485 (save-excursion
2486 (or (progn
2487 (backward-word-strictly 1)
2488 (looking-at "\\(function\\|procedure\\)\\>"))
2489 (progn
2490 (backward-word-strictly 1)
2491 (setq num-back 2)
2492 (looking-at "\\(function\\|procedure\\)\\>")))))
2493
2494 ;; The indentation depends of the value of ada-indent-return
2495 (if (<= (eval var) 0)
2496 (list (point) (list '- var))
2497 (list (progn (backward-word-strictly num-back) (point))
2498 var))
2499
2500 ;; Else there is no parameter list, but we have a function
2501 ;; Only do something special if the user want to indent
2502 ;; relative to the "function" keyword
2503 (if (and (> (eval var) 0)
2504 (save-excursion (forward-word-strictly -1)
2505 (looking-at "function\\>")))
2506 (list (progn (forward-word-strictly -1) (point)) var)
2507
2508 ;; Else...
2509 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
2510
2511 ;;--------------------------------
2512 ;; starting with 'o' or 'p'
2513 ;; 'or' as statement-start
2514 ;; 'private' as statement-start
2515 ;;--------------------------------
2516
2517 ((and (or (= (downcase (char-after)) ?o)
2518 (= (downcase (char-after)) ?p))
2519 (or (ada-looking-at-semi-or)
2520 (ada-looking-at-semi-private)))
2521 (save-excursion
2522 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2523 (ada-goto-matching-start 1)
2524 (list (progn (back-to-indentation) (point)) 0)))
2525
2526 ;;--------------------------------
2527 ;; starting with 'd' (do)
2528 ;;--------------------------------
2529
2530 ((and (= (downcase (char-after)) ?d)
2531 (looking-at "do\\>"))
2532 (save-excursion
2533 (ada-goto-stmt-start)
2534 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
2535
2536 ;;--------------------------------
2537 ;; starting with '-' (comment)
2538 ;;--------------------------------
2539
2540 ((= (char-after) ?-)
2541 (if ada-indent-comment-as-code
2542
2543 ;; Indent comments on previous line comments if required
2544 ;; We must use a search-forward (even if the code is more complex),
2545 ;; since we want to find the beginning of the comment.
2546 (let (pos)
2547
2548 (if (and ada-indent-align-comments
2549 (save-excursion
2550 (forward-line -1)
2551 (beginning-of-line)
2552 (while (and (not pos)
2553 (search-forward "--" (point-at-eol) t))
2554 (unless (ada-in-string-p)
2555 (setq pos (point))))
2556 pos))
2557 (list (- pos 2) 0)
2558
2559 ;; Else always on previous line
2560 (ada-indent-on-previous-lines nil orgpoint orgpoint)))
2561
2562 ;; Else same indentation as the previous line
2563 (list (save-excursion (back-to-indentation) (point)) 0)))
2564
2565 ;;--------------------------------
2566 ;; starting with '#' (preprocessor line)
2567 ;;--------------------------------
2568
2569 ((and (= (char-after) ?#)
2570 (equal ada-which-compiler 'gnat)
2571 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2572 (list (point-at-bol) 0))
2573
2574 ;;--------------------------------
2575 ;; starting with ')' (end of a parameter list)
2576 ;;--------------------------------
2577
2578 ((and (not (eobp)) (= (char-after) ?\)))
2579 (save-excursion
2580 (forward-char 1)
2581 (backward-sexp 1)
2582 (list (point) 0)))
2583
2584 ;;---------------------------------
2585 ;; new/abstract/separate
2586 ;;---------------------------------
2587
2588 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2589 (ada-indent-on-previous-lines nil orgpoint orgpoint))
2590
2591 ;;---------------------------------
2592 ;; package/function/procedure
2593 ;;---------------------------------
2594
2595 ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
2596 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2597 (save-excursion
2598 ;; Go up until we find either a generic section, or the end of the
2599 ;; previous subprogram/package, or 'overriding' for this function/procedure
2600 (let (found)
2601 (while (and (not found)
2602 (ada-search-ignore-string-comment
2603 "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t))
2604
2605 ;; avoid "with procedure"... in generic parts
2606 (save-excursion
2607 (forward-word-strictly -1)
2608 (setq found (not (looking-at "with"))))))
2609
2610 (cond
2611 ((looking-at "\\<generic\\|overriding\\>")
2612 (list (progn (back-to-indentation) (point)) 0))
2613
2614 (t
2615 (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
2616
2617 ;;---------------------------------
2618 ;; label
2619 ;;---------------------------------
2620
2621 ((looking-at ada-label-re)
2622 (if (ada-in-decl-p)
2623 ;; ada-block-label-re matches variable declarations
2624 (ada-indent-on-previous-lines nil orgpoint orgpoint)
2625 (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
2626 '(ada-label-indent))))
2627
2628 ))
2629
2630 ;;---------------------------------
2631 ;; Other syntaxes
2632 ;;---------------------------------
2633 (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
2634
2635 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
2636 "Calculate the indentation for the new line after ORGPOINT.
2637 The result list is based on the previous lines in the buffer.
2638 If NOMOVE is nil, moves point to the beginning of the current statement.
2639 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2640 (if initial-pos
2641 (goto-char initial-pos))
2642 (let ((oldpoint (point)))
2643
2644 ;; Is inside a parameter-list ?
2645 (if (ada-in-paramlist-p)
2646 (ada-get-indent-paramlist)
2647
2648 ;; Move to beginning of current statement. If already at a
2649 ;; statement start, move to beginning of enclosing statement.
2650 (unless nomove
2651 (ada-goto-stmt-start t))
2652
2653 ;; no beginning found => don't change indentation
2654 (if (and (eq oldpoint (point))
2655 (not nomove))
2656 (ada-get-indent-nochange)
2657
2658 (cond
2659 ;;
2660 ((and
2661 ada-indent-to-open-paren
2662 (ada-in-open-paren-p))
2663 (ada-get-indent-open-paren))
2664 ;;
2665 ((looking-at "end\\>")
2666 (ada-get-indent-end orgpoint))
2667 ;;
2668 ((looking-at ada-loop-start-re)
2669 (ada-get-indent-loop orgpoint))
2670 ;;
2671 ((looking-at ada-subprog-start-re)
2672 (ada-get-indent-subprog orgpoint))
2673 ;;
2674 ((looking-at ada-block-start-re)
2675 (ada-get-indent-block-start orgpoint))
2676 ;;
2677 ((looking-at ada-block-label-re) ; also variable declaration
2678 (ada-get-indent-block-label orgpoint))
2679 ;;
2680 ((looking-at ada-goto-label-re)
2681 (ada-get-indent-goto-label orgpoint))
2682 ;;
2683 ((looking-at "\\(sub\\)?type\\>")
2684 (ada-get-indent-type orgpoint))
2685 ;;
2686 ;; "then" has to be included in the case of "select...then abort"
2687 ;; statements, since (goto-stmt-start) at the beginning of
2688 ;; the current function would leave the cursor on that position
2689 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\>")
2690 (ada-get-indent-if orgpoint))
2691 ;;
2692 ((looking-at "case\\>")
2693 (ada-get-indent-case orgpoint))
2694 ;;
2695 ((looking-at "when\\>")
2696 (ada-get-indent-when orgpoint))
2697 ;;
2698 ((looking-at "separate\\>")
2699 (ada-get-indent-nochange))
2700 ;;
2701 ((looking-at "with\\>\\|use\\>")
2702 ;; Are we still in that statement, or are we in fact looking at
2703 ;; the previous one ?
2704 (if (save-excursion (search-forward ";" oldpoint t))
2705 (list (progn (back-to-indentation) (point)) 0)
2706 (list (point) (if (looking-at "with")
2707 'ada-with-indent
2708 'ada-use-indent))))
2709 ;;
2710 (t
2711 (ada-get-indent-noindent orgpoint)))))
2712 ))
2713
2714 (defun ada-get-indent-open-paren ()
2715 "Calculate the indentation when point is behind an unclosed parenthesis."
2716 (list (ada-in-open-paren-p) 0))
2717
2718 (defun ada-get-indent-nochange ()
2719 "Return the current indentation of the previous line."
2720 (save-excursion
2721 (forward-line -1)
2722 (back-to-indentation)
2723 (list (point) 0)))
2724
2725 (defun ada-get-indent-paramlist ()
2726 "Calculate the indentation when point is inside a parameter list."
2727 (save-excursion
2728 (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
2729 (cond
2730 ;; in front of the first parameter
2731 ((= (char-after) ?\()
2732 (goto-char (match-end 0))
2733 (list (point) 0))
2734
2735 ;; in front of another parameter
2736 ((= (char-after) ?\;)
2737 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2738 (ada-goto-next-non-ws)
2739 (list (point) 0))
2740
2741 ;; After an affectation (default parameter value in subprogram
2742 ;; declaration)
2743 ((and (= (following-char) ?=) (= (preceding-char) ?:))
2744 (back-to-indentation)
2745 (list (point) 'ada-broken-indent))
2746
2747 ;; inside a parameter declaration
2748 (t
2749 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
2750 (ada-goto-next-non-ws)
2751 (list (point) 'ada-broken-indent)))))
2752
2753 (defun ada-get-indent-end (orgpoint)
2754 "Calculate the indentation when point is just before an end statement.
2755 ORGPOINT is the limit position used in the calculation."
2756 (let ((defun-name nil)
2757 (indent nil))
2758
2759 ;; is the line already terminated by ';' ?
2760 (if (save-excursion
2761 (ada-search-ignore-string-comment ";" nil orgpoint nil
2762 'search-forward))
2763
2764 ;; yes, look what's following 'end'
2765 (progn
2766 (forward-word-strictly 1)
2767 (ada-goto-next-non-ws)
2768 (cond
2769 ;;
2770 ;; loop/select/if/case/return
2771 ;;
2772 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
2773 (save-excursion (ada-check-matching-start (match-string 0)))
2774 (list (save-excursion (back-to-indentation) (point)) 0))
2775
2776 ;;
2777 ;; record
2778 ;;
2779 ((looking-at "\\<record\\>")
2780 (save-excursion
2781 (ada-check-matching-start (match-string 0))
2782 ;; we are now looking at the matching "record" statement
2783 (forward-word-strictly 1)
2784 (ada-goto-stmt-start)
2785 ;; now on the matching type declaration, or use clause
2786 (unless (looking-at "\\(for\\|type\\)\\>")
2787 (ada-search-ignore-string-comment "\\<type\\>" t))
2788 (list (progn (back-to-indentation) (point)) 0)))
2789 ;;
2790 ;; a named block end
2791 ;;
2792 ((looking-at ada-ident-re)
2793 (setq defun-name (match-string 0))
2794 (save-excursion
2795 (ada-goto-matching-start 0)
2796 (ada-check-defun-name defun-name))
2797 (list (progn (back-to-indentation) (point)) 0))
2798 ;;
2799 ;; a block-end without name
2800 ;;
2801 ((= (char-after) ?\;)
2802 (save-excursion
2803 (ada-goto-matching-start 0)
2804 (if (looking-at "\\<begin\\>")
2805 (progn
2806 (setq indent (list (point) 0))
2807 (if (ada-goto-decl-start t)
2808 (list (progn (back-to-indentation) (point)) 0)
2809 indent))
2810 (list (progn (back-to-indentation) (point)) 0)
2811 )))
2812 ;;
2813 ;; anything else - should maybe signal an error ?
2814 ;;
2815 (t
2816 (list (save-excursion (back-to-indentation) (point))
2817 'ada-broken-indent))))
2818
2819 (list (save-excursion (back-to-indentation) (point))
2820 'ada-broken-indent))))
2821
2822 (defun ada-get-indent-case (orgpoint)
2823 "Calculate the indentation when point is just before a case statement.
2824 ORGPOINT is the limit position used in the calculation."
2825 (let ((match-cons nil)
2826 (opos (point)))
2827 (cond
2828 ;;
2829 ;; case..is..when..=>
2830 ;;
2831 ((save-excursion
2832 (setq match-cons (and
2833 ;; the `=>' must be after the keyword `is'.
2834 (ada-search-ignore-string-comment
2835 "is" nil orgpoint nil 'word-search-forward)
2836 (ada-search-ignore-string-comment
2837 "[ \t\n]+=>" nil orgpoint))))
2838 (save-excursion
2839 (goto-char (car match-cons))
2840 (unless (ada-search-ignore-string-comment "when" t opos)
2841 (error "Missing `when' between `case' and `=>'"))
2842 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
2843 ;;
2844 ;; case..is..when
2845 ;;
2846 ((save-excursion
2847 (setq match-cons (ada-search-ignore-string-comment
2848 "when" nil orgpoint nil 'word-search-forward)))
2849 (goto-char (cdr match-cons))
2850 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
2851 ;;
2852 ;; case..is
2853 ;;
2854 ((save-excursion
2855 (setq match-cons (ada-search-ignore-string-comment
2856 "is" nil orgpoint nil 'word-search-forward)))
2857 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
2858 ;;
2859 ;; incomplete case
2860 ;;
2861 (t
2862 (list (save-excursion (back-to-indentation) (point))
2863 'ada-broken-indent)))))
2864
2865 (defun ada-get-indent-when (orgpoint)
2866 "Calculate the indentation when point is just before a when statement.
2867 ORGPOINT is the limit position used in the calculation."
2868 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2869 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
2870 (list cur-indent 'ada-indent)
2871 (list cur-indent 'ada-broken-indent))))
2872
2873 (defun ada-get-indent-if (orgpoint)
2874 "Calculate the indentation when point is just before an if statement.
2875 ORGPOINT is the limit position used in the calculation."
2876 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2877 (match-cons nil))
2878 ;;
2879 ;; Move to the correct then (ignore all "and then")
2880 ;;
2881 (while (and (setq match-cons (ada-search-ignore-string-comment
2882 "\\<\\(then\\|and[ \t]*then\\)\\>"
2883 nil orgpoint))
2884 (= (downcase (char-after (car match-cons))) ?a)))
2885 ;; If "then" was found (we are looking at it)
2886 (if match-cons
2887 (progn
2888 ;;
2889 ;; 'then' first in separate line ?
2890 ;; => indent according to 'then',
2891 ;; => else indent according to 'if'
2892 ;;
2893 (if (save-excursion
2894 (back-to-indentation)
2895 (looking-at "\\<then\\>"))
2896 (setq cur-indent (save-excursion (back-to-indentation) (point))))
2897 ;; skip 'then'
2898 (forward-word-strictly 1)
2899 (list cur-indent 'ada-indent))
2900
2901 (list cur-indent 'ada-broken-indent))))
2902
2903 (defun ada-get-indent-block-start (orgpoint)
2904 "Calculate the indentation when point is at the start of a block.
2905 ORGPOINT is the limit position used in the calculation."
2906 (let ((pos nil))
2907 (cond
2908 ((save-excursion
2909 (forward-word-strictly 1)
2910 (setq pos (ada-goto-next-non-ws orgpoint)))
2911 (goto-char pos)
2912 (save-excursion
2913 (ada-indent-on-previous-lines t orgpoint)))
2914
2915 ;; Special case for record types, for instance for:
2916 ;; type A is (B : Integer;
2917 ;; C : Integer) is record
2918 ;; null; -- This is badly indented otherwise
2919 ((looking-at "record")
2920
2921 ;; If record is at the beginning of the line, indent from there
2922 (if (save-excursion
2923 (beginning-of-line)
2924 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2925 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
2926
2927 ;; else indent relative to the type command
2928 (list (save-excursion
2929 (car (ada-search-ignore-string-comment "\\<type\\>" t)))
2930 'ada-indent)))
2931
2932 ;; Special case for label:
2933 ((looking-at ada-block-label-re)
2934 (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent) 'ada-indent))
2935
2936 ;; nothing follows the block-start
2937 (t
2938 (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
2939
2940 (defun ada-get-indent-subprog (orgpoint)
2941 "Calculate the indentation when point is just before a subprogram.
2942 ORGPOINT is the limit position used in the calculation."
2943 (let ((match-cons nil)
2944 (cur-indent (save-excursion (back-to-indentation) (point)))
2945 (foundis nil))
2946 ;;
2947 ;; is there an 'is' in front of point ?
2948 ;;
2949 (if (save-excursion
2950 (setq match-cons
2951 (ada-search-ignore-string-comment
2952 "\\<\\(is\\|do\\)\\>" nil orgpoint)))
2953 ;;
2954 ;; yes, then skip to its end
2955 ;;
2956 (progn
2957 (setq foundis t)
2958 (goto-char (cdr match-cons)))
2959 ;;
2960 ;; no, then goto next non-ws, if there is one in front of point
2961 ;;
2962 (progn
2963 (unless (ada-goto-next-non-ws orgpoint)
2964 (goto-char orgpoint))))
2965
2966 (cond
2967 ;;
2968 ;; nothing follows 'is'
2969 ;;
2970 ((and
2971 foundis
2972 (save-excursion
2973 (not (ada-search-ignore-string-comment
2974 "[^ \t\n]" nil orgpoint t))))
2975 (list cur-indent 'ada-indent))
2976 ;;
2977 ;; is abstract/separate/new ...
2978 ;;
2979 ((and
2980 foundis
2981 (save-excursion
2982 (setq match-cons
2983 (ada-search-ignore-string-comment
2984 "\\<\\(separate\\|new\\|abstract\\)\\>"
2985 nil orgpoint))))
2986 (goto-char (car match-cons))
2987 (ada-search-ignore-string-comment ada-subprog-start-re t)
2988 (ada-get-indent-noindent orgpoint))
2989 ;;
2990 ;; something follows 'is'
2991 ;;
2992 ((and
2993 foundis
2994 (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
2995 (goto-char match-cons)
2996 (ada-indent-on-previous-lines t orgpoint)))
2997 ;;
2998 ;; no 'is' but ';'
2999 ;;
3000 ((save-excursion
3001 (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
3002 (list cur-indent 0))
3003 ;;
3004 ;; no 'is' or ';'
3005 ;;
3006 (t
3007 (list cur-indent 'ada-broken-indent)))))
3008
3009 (defun ada-get-indent-noindent (orgpoint)
3010 "Calculate the indentation when point is just before a `noindent stmt'.
3011 ORGPOINT is the limit position used in the calculation."
3012 (let ((label 0))
3013 (save-excursion
3014 (beginning-of-line)
3015
3016 (cond
3017
3018 ;; This one is called when indenting a line preceded by a multi-line
3019 ;; subprogram declaration (in that case, we are at this point inside
3020 ;; the parameter declaration list)
3021 ((ada-in-paramlist-p)
3022 (ada-previous-procedure)
3023 (list (save-excursion (back-to-indentation) (point)) 0))
3024
3025 ;; This one is called when indenting the second line of a multi-line
3026 ;; declaration section, in a declare block or a record declaration
3027 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3028 (list (save-excursion (back-to-indentation) (point))
3029 'ada-broken-decl-indent))
3030
3031 ;; This one is called in every other case when indenting a line at the
3032 ;; top level
3033 (t
3034 (if (looking-at (concat "[ \t]*" ada-block-label-re))
3035 (setq label (- ada-label-indent))
3036
3037 (let (p)
3038
3039 ;; "with private" or "null record" cases
3040 (if (or (save-excursion
3041 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
3042 (setq p (point))
3043 (save-excursion (forward-char -7);; skip back "private"
3044 (ada-goto-previous-word)
3045 (looking-at "with"))))
3046 (save-excursion
3047 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
3048 (setq p (point))
3049 (save-excursion (forward-char -6);; skip back "record"
3050 (ada-goto-previous-word)
3051 (looking-at "null")))))
3052 (progn
3053 (goto-char p)
3054 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
3055 (list (save-excursion (back-to-indentation) (point)) 0)))))
3056 (if (save-excursion
3057 (ada-search-ignore-string-comment ";" nil orgpoint nil
3058 'search-forward))
3059 (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
3060 (list (+ (save-excursion (back-to-indentation) (point)) label)
3061 'ada-broken-indent)))))))
3062
3063 (defun ada-get-indent-block-label (orgpoint)
3064 "Calculate the indentation when before a label or variable declaration.
3065 ORGPOINT is the limit position used in the calculation."
3066 (let ((match-cons nil)
3067 (cur-indent (save-excursion (back-to-indentation) (point))))
3068 (ada-search-ignore-string-comment ":" nil)
3069 (cond
3070 ;; loop label
3071 ((save-excursion
3072 (setq match-cons (ada-search-ignore-string-comment
3073 ada-loop-start-re nil orgpoint)))
3074 (goto-char (car match-cons))
3075 (ada-get-indent-loop orgpoint))
3076
3077 ;; declare label
3078 ((save-excursion
3079 (setq match-cons (ada-search-ignore-string-comment
3080 "\\<declare\\|begin\\>" nil orgpoint)))
3081 (goto-char (car match-cons))
3082 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3083
3084 ;; variable declaration
3085 ((ada-in-decl-p)
3086 (if (save-excursion
3087 (ada-search-ignore-string-comment ";" nil orgpoint))
3088 (list cur-indent 0)
3089 (list cur-indent 'ada-broken-indent)))
3090
3091 ;; nothing follows colon
3092 (t
3093 (list cur-indent '(- ada-label-indent))))))
3094
3095 (defun ada-get-indent-goto-label (orgpoint)
3096 "Calculate the indentation when at a goto label."
3097 (search-forward ">>")
3098 (ada-goto-next-non-ws)
3099 (if (>= (point) orgpoint)
3100 ;; labeled statement is the one we need to indent
3101 (list (- (point) ada-label-indent))
3102 ;; else indentation is indent for labeled statement
3103 (ada-indent-on-previous-lines t orgpoint)))
3104
3105 (defun ada-get-indent-loop (orgpoint)
3106 "Calculate the indentation when just before a loop or a for ... use.
3107 ORGPOINT is the limit position used in the calculation."
3108 (let ((match-cons nil)
3109 (pos (point))
3110
3111 ;; If looking at a named block, skip the label
3112 (label (save-excursion
3113 (back-to-indentation)
3114 (if (looking-at ada-block-label-re)
3115 (- ada-label-indent)
3116 0))))
3117
3118 (cond
3119
3120 ;;
3121 ;; statement complete
3122 ;;
3123 ((save-excursion
3124 (ada-search-ignore-string-comment ";" nil orgpoint nil
3125 'search-forward))
3126 (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
3127 ;;
3128 ;; simple loop
3129 ;;
3130 ((looking-at "loop\\>")
3131 (setq pos (ada-get-indent-block-start orgpoint))
3132 (if (equal label 0)
3133 pos
3134 (list (+ (car pos) label) (cadr pos))))
3135
3136 ;;
3137 ;; 'for'- loop (or also a for ... use statement)
3138 ;;
3139 ((looking-at "for\\>")
3140 (cond
3141 ;;
3142 ;; for ... use
3143 ;;
3144 ((save-excursion
3145 (and
3146 (goto-char (match-end 0))
3147 (ada-goto-next-non-ws orgpoint)
3148 (forward-word-strictly 1)
3149 (if (= (char-after) ?') (forward-word-strictly 1) t)
3150 (ada-goto-next-non-ws orgpoint)
3151 (looking-at "\\<use\\>")
3152 ;;
3153 ;; check if there is a 'record' before point
3154 ;;
3155 (progn
3156 (setq match-cons (ada-search-ignore-string-comment
3157 "record" nil orgpoint nil 'word-search-forward))
3158 t)))
3159 (if match-cons
3160 (progn
3161 (goto-char (car match-cons))
3162 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3163 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3164 )
3165
3166 ;;
3167 ;; for..loop
3168 ;;
3169 ((save-excursion
3170 (setq match-cons (ada-search-ignore-string-comment
3171 "loop" nil orgpoint nil 'word-search-forward)))
3172 (goto-char (car match-cons))
3173 ;;
3174 ;; indent according to 'loop', if it's first in the line;
3175 ;; otherwise to 'for'
3176 ;;
3177 (unless (save-excursion
3178 (back-to-indentation)
3179 (looking-at "\\<loop\\>"))
3180 (goto-char pos))
3181 (list (+ (save-excursion (back-to-indentation) (point)) label)
3182 'ada-indent))
3183 ;;
3184 ;; for-statement is broken
3185 ;;
3186 (t
3187 (list (+ (save-excursion (back-to-indentation) (point)) label)
3188 'ada-broken-indent))))
3189
3190 ;;
3191 ;; 'while'-loop
3192 ;;
3193 ((looking-at "while\\>")
3194 ;;
3195 ;; while..loop ?
3196 ;;
3197 (if (save-excursion
3198 (setq match-cons (ada-search-ignore-string-comment
3199 "loop" nil orgpoint nil 'word-search-forward)))
3200
3201 (progn
3202 (goto-char (car match-cons))
3203 ;;
3204 ;; indent according to 'loop', if it's first in the line;
3205 ;; otherwise to 'while'.
3206 ;;
3207 (unless (save-excursion
3208 (back-to-indentation)
3209 (looking-at "\\<loop\\>"))
3210 (goto-char pos))
3211 (list (+ (save-excursion (back-to-indentation) (point)) label)
3212 'ada-indent))
3213
3214 (list (+ (save-excursion (back-to-indentation) (point)) label)
3215 'ada-broken-indent))))))
3216
3217 (defun ada-get-indent-type (orgpoint)
3218 "Calculate the indentation when before a type statement.
3219 ORGPOINT is the limit position used in the calculation."
3220 (let ((match-dat nil))
3221 (cond
3222 ;;
3223 ;; complete record declaration
3224 ;;
3225 ((save-excursion
3226 (and
3227 (setq match-dat (ada-search-ignore-string-comment
3228 "end" nil orgpoint nil 'word-search-forward))
3229 (ada-goto-next-non-ws)
3230 (looking-at "\\<record\\>")
3231 (forward-word-strictly 1)
3232 (ada-goto-next-non-ws)
3233 (= (char-after) ?\;)))
3234 (goto-char (car match-dat))
3235 (list (save-excursion (back-to-indentation) (point)) 0))
3236 ;;
3237 ;; record type
3238 ;;
3239 ((save-excursion
3240 (setq match-dat (ada-search-ignore-string-comment
3241 "record" nil orgpoint nil 'word-search-forward)))
3242 (goto-char (car match-dat))
3243 (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
3244 ;;
3245 ;; complete type declaration
3246 ;;
3247 ((save-excursion
3248 (ada-search-ignore-string-comment ";" nil orgpoint nil
3249 'search-forward))
3250 (list (save-excursion (back-to-indentation) (point)) 0))
3251 ;;
3252 ;; "type ... is", but not "type ... is ...", which is broken
3253 ;;
3254 ((save-excursion
3255 (and
3256 (ada-search-ignore-string-comment "is" nil orgpoint nil
3257 'word-search-forward)
3258 (not (ada-goto-next-non-ws orgpoint))))
3259 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
3260 ;;
3261 ;; broken statement
3262 ;;
3263 (t
3264 (list (save-excursion (back-to-indentation) (point))
3265 'ada-broken-indent)))))
3266
3267 \f
3268 ;; -----------------------------------------------------------
3269 ;; -- searching and matching
3270 ;; -----------------------------------------------------------
3271
3272 (defun ada-goto-stmt-start (&optional ignore-goto-label)
3273 "Move point to the beginning of the statement that point is in or after.
3274 Return the new position of point.
3275 As a special case, if we are looking at a closing parenthesis, skip to the
3276 open parenthesis."
3277 (let ((match-dat nil)
3278 (orgpoint (point)))
3279
3280 (setq match-dat (ada-search-prev-end-stmt))
3281 (if match-dat
3282
3283 ;;
3284 ;; found a previous end-statement => check if anything follows
3285 ;;
3286 (unless (looking-at "declare")
3287 (progn
3288 (unless (save-excursion
3289 (goto-char (cdr match-dat))
3290 (ada-goto-next-non-ws orgpoint ignore-goto-label))
3291 ;;
3292 ;; nothing follows => it's the end-statement directly in
3293 ;; front of point => search again
3294 ;;
3295 (setq match-dat (ada-search-prev-end-stmt)))
3296 ;;
3297 ;; if found the correct end-statement => goto next non-ws
3298 ;;
3299 (if match-dat
3300 (goto-char (cdr match-dat)))
3301 (ada-goto-next-non-ws)
3302 ))
3303
3304 ;;
3305 ;; no previous end-statement => we are at the beginning of the
3306 ;; accessible part of the buffer
3307 ;;
3308 (progn
3309 (goto-char (point-min))
3310 ;;
3311 ;; skip to the very first statement, if there is one
3312 ;;
3313 (unless (ada-goto-next-non-ws orgpoint)
3314 (goto-char orgpoint))))
3315 (point)))
3316
3317
3318 (defun ada-search-prev-end-stmt ()
3319 "Move point to previous end statement.
3320 Return a cons cell whose car is the beginning and whose cdr
3321 is the end of the match."
3322 (let ((match-dat nil)
3323 (found nil))
3324
3325 ;; search until found or beginning-of-buffer
3326 (while
3327 (and
3328 (not found)
3329 (setq match-dat (ada-search-ignore-string-comment
3330 ada-end-stmt-re t)))
3331
3332 (goto-char (car match-dat))
3333 (unless (ada-in-open-paren-p)
3334 (cond
3335
3336 ((and (looking-at
3337 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3338 (save-excursion
3339 (ada-goto-previous-word)
3340 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3341 (forward-word-strictly -1))
3342
3343 ((looking-at "is")
3344 (setq found
3345 (and (save-excursion (ada-goto-previous-word)
3346 (ada-goto-previous-word)
3347 (not (looking-at "subtype")))
3348
3349 (save-excursion (goto-char (cdr match-dat))
3350 (ada-goto-next-non-ws)
3351 ;; words that can go after an 'is'
3352 (not (looking-at
3353 (eval-when-compile
3354 (concat "\\<"
3355 (regexp-opt
3356 '("separate" "access" "array"
3357 "private" "abstract" "new") t)
3358 "\\>\\|("))))))))
3359
3360 ((looking-at "private")
3361 (save-excursion
3362 (backward-word-strictly 1)
3363 (setq found (not (looking-at "is")))))
3364
3365 (t
3366 (setq found t))
3367 )))
3368
3369 (if found
3370 match-dat
3371 nil)))
3372
3373 (defun ada-goto-next-non-ws (&optional limit skip-goto-label)
3374 "Skip to next non-whitespace character.
3375 Skips spaces, newlines and comments, and possibly goto labels.
3376 Return `point' if moved, nil if not.
3377 Stop the search at LIMIT.
3378 Do not call this function from within a string."
3379 (unless limit
3380 (setq limit (point-max)))
3381 (while (and (<= (point) limit)
3382 (or (progn (forward-comment 10000)
3383 (if (and (not (eobp))
3384 (save-excursion (forward-char 1)
3385 (ada-in-string-p)))
3386 (progn (forward-sexp 1) t)))
3387 (and skip-goto-label
3388 (looking-at ada-goto-label-re)
3389 (progn
3390 (goto-char (match-end 0))
3391 t)))))
3392 (if (< (point) limit)
3393 (point)
3394 nil)
3395 )
3396
3397
3398 (defun ada-goto-stmt-end (&optional limit)
3399 "Move point to the end of the statement that point is in or before.
3400 Return the new position of point or nil if not found.
3401 Stop the search at LIMIT."
3402 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
3403 (point)
3404 nil))
3405
3406
3407 (defun ada-goto-next-word (&optional backward)
3408 "Move point to the beginning of the next word of Ada code.
3409 If BACKWARD is non-nil, jump to the beginning of the previous word.
3410 Return the new position of point or nil if not found."
3411 (let ((match-cons nil)
3412 (orgpoint (point)))
3413 (unless backward
3414 (skip-syntax-forward "w_"))
3415 (if (setq match-cons
3416 (ada-search-ignore-string-comment "\\sw\\|\\s_" backward nil t))
3417 ;;
3418 ;; move to the beginning of the word found
3419 ;;
3420 (progn
3421 (goto-char (car match-cons))
3422 (skip-syntax-backward "w_")
3423 (point))
3424 ;;
3425 ;; if not found, restore old position of point
3426 ;;
3427 (goto-char orgpoint)
3428 'nil)))
3429
3430
3431 (defun ada-check-matching-start (keyword)
3432 "Signal an error if matching block start is not KEYWORD.
3433 Moves point to the matching block start."
3434 (ada-goto-matching-start 0)
3435 (unless (looking-at (concat "\\<" keyword "\\>"))
3436 (error "Matching start is not `%s'" keyword)))
3437
3438
3439 (defun ada-check-defun-name (defun-name)
3440 "Check if the name of the matching defun really is DEFUN-NAME.
3441 Assumes point to be already positioned by `ada-goto-matching-start'.
3442 Moves point to the beginning of the declaration."
3443
3444 ;; named block without a `declare'; ada-goto-matching-start leaves
3445 ;; point at start of 'begin' for a block.
3446 (if (save-excursion
3447 (ada-goto-previous-word)
3448 (looking-at (concat "\\<" defun-name "\\> *:")))
3449 t ; name matches
3450 ;; else
3451 ;;
3452 ;; 'accept' or 'package' ?
3453 ;;
3454 (unless (looking-at ada-subprog-start-re)
3455 (ada-goto-decl-start))
3456 ;;
3457 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3458 ;;
3459 (save-excursion
3460 ;;
3461 ;; a named 'declare'-block ? => jump to the label
3462 ;;
3463 (if (looking-at "\\<declare\\>")
3464 (progn
3465 (forward-comment -1)
3466 (backward-word-strictly 1))
3467 ;;
3468 ;; no, => 'procedure'/'function'/'task'/'protected'
3469 ;;
3470 (progn
3471 (forward-word-strictly 2)
3472 (backward-word-strictly 1)
3473 ;;
3474 ;; skip 'body' 'type'
3475 ;;
3476 (if (looking-at "\\<\\(body\\|type\\)\\>")
3477 (forward-word-strictly 1))
3478 (forward-sexp 1)
3479 (backward-sexp 1)))
3480 ;;
3481 ;; should be looking-at the correct name
3482 ;;
3483 (unless (looking-at (concat "\\<" defun-name "\\>"))
3484 (error "Matching defun has different name: %s"
3485 (buffer-substring (point)
3486 (progn (forward-sexp 1) (point))))))))
3487
3488 (defun ada-goto-decl-start (&optional noerror)
3489 "Move point to the declaration start of the current construct.
3490 If NOERROR is non-nil, return nil if no match was found;
3491 otherwise throw error."
3492 (let ((nest-count 1)
3493 (regexp (eval-when-compile
3494 (concat "\\<"
3495 (regexp-opt
3496 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
3497 "\\>")))
3498
3499 ;; first should be set to t if we should stop at the first
3500 ;; "begin" we encounter.
3501 (first t)
3502 (count-generic nil)
3503 (stop-at-when nil)
3504 )
3505
3506 ;; Ignore "when" most of the time, except if we are looking at the
3507 ;; beginning of a block (structure: case .. is
3508 ;; when ... =>
3509 ;; begin ...
3510 ;; exception ... )
3511 (if (looking-at "begin")
3512 (setq stop-at-when t))
3513
3514 (if (or
3515 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3516 (save-excursion
3517 (ada-search-ignore-string-comment
3518 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
3519 (looking-at "generic")))
3520 (setq count-generic t))
3521
3522 ;; search backward for interesting keywords
3523 (while (and
3524 (not (zerop nest-count))
3525 (ada-search-ignore-string-comment regexp t))
3526 ;;
3527 ;; calculate nest-depth
3528 ;;
3529 (cond
3530 ;;
3531 ((looking-at "end")
3532 (ada-goto-matching-start 1 noerror)
3533
3534 ;; In some case, two begin..end block can follow each other closely,
3535 ;; which we have to detect, as in
3536 ;; procedure P is
3537 ;; procedure Q is
3538 ;; begin
3539 ;; end;
3540 ;; begin -- here we should go to procedure, not begin
3541 ;; end
3542
3543 (if (looking-at "begin")
3544 (let ((loop-again t))
3545 (save-excursion
3546 (while loop-again
3547 ;; If begin was just there as the beginning of a block
3548 ;; (with no declare) then do nothing, otherwise just
3549 ;; register that we have to find the statement that
3550 ;; required the begin
3551
3552 (ada-search-ignore-string-comment
3553 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3554 t)
3555
3556 (if (looking-at "end")
3557 (ada-goto-matching-start 1 noerror t)
3558
3559 (setq loop-again nil)
3560 (unless (looking-at "begin")
3561 (setq nest-count (1+ nest-count))))
3562 ))
3563 )))
3564 ;;
3565 ((looking-at "generic")
3566 (if count-generic
3567 (progn
3568 (setq first nil)
3569 (setq nest-count (1- nest-count)))))
3570 ;;
3571 ((looking-at "if")
3572 (save-excursion
3573 (forward-word-strictly -1)
3574 (unless (looking-at "\\<end[ \t\n]*if\\>")
3575 (progn
3576 (setq nest-count (1- nest-count))
3577 (setq first nil)))))
3578
3579 ;;
3580 ((looking-at "declare\\|generic")
3581 (setq nest-count (1- nest-count))
3582 (setq first t))
3583 ;;
3584 ((looking-at "is")
3585 ;; look for things to ignore
3586 (if
3587 (or
3588 ;; generic formal parameter
3589 (looking-at "is[ t]+<>")
3590
3591 ;; A type definition, or a case statement. Note that the
3592 ;; goto-matching-start above on 'end record' leaves us at
3593 ;; 'record', not at 'type'.
3594 ;;
3595 ;; We get to a case statement here by calling
3596 ;; 'ada-move-to-end' from inside a case statement; then
3597 ;; we are not ignoring 'when'.
3598 (save-excursion
3599 ;; Skip type discriminants or case argument function call param list
3600 (forward-comment -10000)
3601 (forward-char -1)
3602 (if (= (char-after) ?\))
3603 (progn
3604 (forward-char 1)
3605 (backward-sexp 1)
3606 (forward-comment -10000)
3607 ))
3608 ;; skip type or case argument name
3609 (skip-chars-backward "a-zA-Z0-9_.'")
3610 (ada-goto-previous-word)
3611 (and
3612 ;; if it's a protected type, it's the decl start we
3613 ;; are looking for; since we didn't see the 'end'
3614 ;; above, we are inside it.
3615 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3616 (save-match-data
3617 (ada-goto-previous-word)
3618 (not (looking-at "\\<protected\\>"))))
3619 ) ; end of type definition p
3620
3621 ;; null procedure declaration
3622 (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
3623 );; end or
3624 ;; skip this construct
3625 nil
3626 ;; this is the right "is"
3627 (setq nest-count (1- nest-count))
3628 (setq first nil)))
3629
3630 ;;
3631 ((looking-at "new")
3632 (if (save-excursion
3633 (ada-goto-previous-word)
3634 (looking-at "is"))
3635 (goto-char (match-beginning 0))))
3636 ;;
3637 ((and first
3638 (looking-at "begin"))
3639 (setq nest-count 0))
3640 ;;
3641 ((looking-at "when")
3642 (save-excursion
3643 (forward-word-strictly -1)
3644 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3645 (progn
3646 (if stop-at-when
3647 (setq nest-count (1- nest-count)))
3648 ))))
3649 ;;
3650 ((looking-at "begin")
3651 (setq first nil))
3652 ;;
3653 (t
3654 (setq nest-count (1+ nest-count))
3655 (setq first nil)))
3656
3657 );; end of loop
3658
3659 ;; check if declaration-start is really found
3660 (if (and
3661 (zerop nest-count)
3662 (if (looking-at "is")
3663 (ada-search-ignore-string-comment ada-subprog-start-re t)
3664 (looking-at "declare\\|generic")))
3665 t
3666 (if noerror nil
3667 (error "No matching proc/func/task/declare/package/protected")))
3668 ))
3669
3670 (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
3671 "Move point to the beginning of a block-start.
3672 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3673 If NOERROR is non-nil, it only returns nil if no matching start was found.
3674 If GOTOTHEN is non-nil, point moves to the `then' following `if'."
3675 (let ((nest-count (if nest-level nest-level 0))
3676 (found nil)
3677
3678 (last-was-begin '())
3679 ;; List all keywords encountered while traversing
3680 ;; something like '("end" "end" "begin")
3681 ;; This is removed from the list when "package", "procedure",...
3682 ;; are seen. The goal is to find whether a package has an elaboration
3683 ;; part
3684
3685 (pos nil))
3686
3687 ;; search backward for interesting keywords
3688 (while (and
3689 (not found)
3690 (ada-search-ignore-string-comment ada-matching-start-re t))
3691
3692 (unless (and (looking-at "\\<record\\>")
3693 (save-excursion
3694 (forward-word-strictly -1)
3695 (looking-at "\\<null\\>")))
3696 (progn
3697 ;; calculate nest-depth
3698 (cond
3699 ;; found block end => increase nest depth
3700 ((looking-at "end")
3701 (push nil last-was-begin)
3702 (setq nest-count (1+ nest-count)))
3703
3704 ;; found loop/select/record/case/if => check if it starts or
3705 ;; ends a block
3706 ((looking-at "loop\\|select\\|record\\|case\\|if")
3707 (setq pos (point))
3708 (save-excursion
3709 ;; check if keyword follows 'end'
3710 (ada-goto-previous-word)
3711 (if (looking-at "\\<end\\>[ \t]*[^;]")
3712 (progn
3713 ;; it ends a block => increase nest depth
3714 (setq nest-count (1+ nest-count)
3715 pos (point))
3716 (push nil last-was-begin))
3717
3718 ;; it starts a block => decrease nest depth
3719 (setq nest-count (1- nest-count))
3720
3721 ;; Some nested "begin .. end" blocks with no "declare"?
3722 ;; => remove those entries
3723 (while (car last-was-begin)
3724 (setq last-was-begin (cdr (cdr last-was-begin))))
3725
3726 (setq last-was-begin (cdr last-was-begin))
3727 ))
3728 (goto-char pos)
3729 )
3730
3731 ;; found package start => check if it really is a block
3732 ((looking-at "package")
3733 (save-excursion
3734 ;; ignore if this is just a renames statement
3735 (let ((current (point))
3736 (pos (ada-search-ignore-string-comment
3737 "\\<\\(is\\|renames\\|;\\)\\>" nil)))
3738 (if pos
3739 (goto-char (car pos))
3740 (error (concat
3741 "No matching `is' or `renames' for `package' at"
3742 " line "
3743 (number-to-string (count-lines 1 (1+ current)))))))
3744 (unless (looking-at "renames")
3745 (progn
3746 (forward-word-strictly 1)
3747 (ada-goto-next-non-ws)
3748 ;; ignore it if it is only a declaration with 'new'
3749 ;; We could have package Foo is new ....
3750 ;; or package Foo is separate;
3751 ;; or package Foo is begin null; end Foo
3752 ;; for elaboration code (elaboration)
3753 (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3754 (not (car last-was-begin)))
3755 (setq nest-count (1- nest-count))))))
3756
3757 (setq last-was-begin (cdr last-was-begin))
3758 )
3759 ;; found task start => check if it has a body
3760 ((looking-at "task")
3761 (save-excursion
3762 (forward-word-strictly 1)
3763 (ada-goto-next-non-ws)
3764 (cond
3765 ((looking-at "\\<body\\>"))
3766 ((looking-at "\\<type\\>")
3767 ;; In that case, do nothing if there is a "is"
3768 (forward-word-strictly 2);; skip "type"
3769 (ada-goto-next-non-ws);; skip type name
3770
3771 ;; Do nothing if we are simply looking at a simple
3772 ;; "task type name;" statement with no block
3773 (unless (looking-at ";")
3774 (progn
3775 ;; Skip the parameters
3776 (if (looking-at "(")
3777 (ada-search-ignore-string-comment ")" nil))
3778 (let ((tmp (ada-search-ignore-string-comment
3779 "\\<\\(is\\|;\\)\\>" nil)))
3780 (if tmp
3781 (progn
3782 (goto-char (car tmp))
3783 (if (looking-at "is")
3784 (setq nest-count (1- nest-count)))))))))
3785 (t
3786 ;; Check if that task declaration had a block attached to
3787 ;; it (i.e do nothing if we have just "task name;")
3788 (unless (progn (forward-word-strictly 1)
3789 (looking-at "[ \t]*;"))
3790 (setq nest-count (1- nest-count))))))
3791 (setq last-was-begin (cdr last-was-begin))
3792 )
3793
3794 ((looking-at "declare")
3795 ;; remove entry for begin and end (include nested begin..end
3796 ;; groups)
3797 (setq last-was-begin (cdr last-was-begin))
3798 (let ((count 1))
3799 (while (and (> count 0))
3800 (if (equal (car last-was-begin) t)
3801 (setq count (1+ count))
3802 (setq count (1- count)))
3803 (setq last-was-begin (cdr last-was-begin))
3804 )))
3805
3806 ((looking-at "protected")
3807 ;; Ignore if this is just a declaration
3808 (save-excursion
3809 (let ((pos (ada-search-ignore-string-comment
3810 "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil)))
3811 (if pos
3812 (goto-char (car pos)))
3813 (if (looking-at "is")
3814 ;; remove entry for end
3815 (setq last-was-begin (cdr last-was-begin)))))
3816 (setq nest-count (1- nest-count)))
3817
3818 ((or (looking-at "procedure")
3819 (looking-at "function"))
3820 ;; Ignore if this is just a declaration
3821 (save-excursion
3822 (let ((pos (ada-search-ignore-string-comment
3823 "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil)))
3824 (if pos
3825 (goto-char (car pos)))
3826 (if (looking-at "is")
3827 ;; remove entry for begin and end
3828 (setq last-was-begin (cdr (cdr last-was-begin))))))
3829 )
3830
3831 ;; all the other block starts
3832 (t
3833 (push (looking-at "begin") last-was-begin)
3834 (setq nest-count (1- nest-count)))
3835
3836 )
3837
3838 ;; match is found, if nest-depth is zero
3839 (setq found (zerop nest-count))))) ; end of loop
3840
3841 (if (bobp)
3842 (point)
3843 (if found
3844 ;;
3845 ;; match found => is there anything else to do ?
3846 ;;
3847 (progn
3848 (cond
3849 ;;
3850 ;; found 'if' => skip to 'then', if it's on a separate line
3851 ;; and GOTOTHEN is non-nil
3852 ;;
3853 ((and
3854 gotothen
3855 (looking-at "if")
3856 (save-excursion
3857 (ada-search-ignore-string-comment "then" nil nil nil
3858 'word-search-forward)
3859 (back-to-indentation)
3860 (looking-at "\\<then\\>")))
3861 (goto-char (match-beginning 0)))
3862
3863 ;;
3864 ;; found 'do' => skip back to 'accept' or 'return'
3865 ;;
3866 ((looking-at "do")
3867 (unless (ada-search-ignore-string-comment
3868 "\\<accept\\|return\\>" t)
3869 (error "Missing `accept' or `return' in front of `do'"))))
3870 (point))
3871
3872 (if noerror
3873 nil
3874 (error "No matching start"))))))
3875
3876
3877 (defun ada-goto-matching-end (&optional nest-level noerror)
3878 "Move point to the end of a block.
3879 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3880 If NOERROR is non-nil, it only returns nil if no matching start found."
3881 (let ((nest-count (or nest-level 0))
3882 (regex (eval-when-compile
3883 (concat "\\<"
3884 (regexp-opt '("end" "loop" "select" "begin" "case"
3885 "if" "task" "package" "record" "do"
3886 "procedure" "function") t)
3887 "\\>")))
3888 found
3889 pos
3890
3891 ;; First is used for subprograms: they are generally handled
3892 ;; recursively, but of course we do not want to do that the
3893 ;; first time (see comment below about subprograms)
3894 (first (not (looking-at "declare"))))
3895
3896 ;; If we are already looking at one of the keywords, this shouldn't count
3897 ;; in the nesting loop below, so we just make sure we don't count it.
3898 ;; "declare" is a special case because we need to look after the "begin"
3899 ;; keyword
3900 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3901 (forward-char 1))
3902
3903 ;;
3904 ;; search forward for interesting keywords
3905 ;;
3906 (while (and
3907 (not found)
3908 (ada-search-ignore-string-comment regex nil))
3909
3910 ;;
3911 ;; calculate nest-depth
3912 ;;
3913 (backward-word-strictly 1)
3914 (cond
3915 ;; procedures and functions need to be processed recursively, in
3916 ;; case they are defined in a declare/begin block, as in:
3917 ;; declare -- NL 0 (nested level)
3918 ;; A : Boolean;
3919 ;; procedure B (C : D) is
3920 ;; begin -- NL 1
3921 ;; null;
3922 ;; end B; -- NL 0, and we would exit
3923 ;; begin
3924 ;; end; -- we should exit here
3925 ;; processing them recursively avoids the need for any special
3926 ;; handling.
3927 ;; Nothing should be done if we have only the specs or a
3928 ;; generic instantiation.
3929
3930 ((and (looking-at "\\<procedure\\|function\\>"))
3931 (if first
3932 (forward-word-strictly 1)
3933
3934 (setq pos (point))
3935 (ada-search-ignore-string-comment "is\\|;")
3936 (if (= (char-before) ?s)
3937 (progn
3938 (ada-goto-next-non-ws)
3939 (unless (looking-at "\\<new\\>")
3940 (progn
3941 (goto-char pos)
3942 (ada-goto-matching-end 0 t)))))))
3943
3944 ;; found block end => decrease nest depth
3945 ((looking-at "\\<end\\>")
3946 (setq nest-count (1- nest-count)
3947 found (<= nest-count 0))
3948 ;; skip the following keyword
3949 (if (progn
3950 (skip-chars-forward "end")
3951 (ada-goto-next-non-ws)
3952 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3953 (forward-word-strictly 1)))
3954
3955 ;; found package start => check if it really starts a block, and is not
3956 ;; in fact a generic instantiation for instance
3957 ((looking-at "\\<package\\>")
3958 (ada-search-ignore-string-comment "is" nil nil nil
3959 'word-search-forward)
3960 (ada-goto-next-non-ws)
3961 ;; ignore and skip it if it is only a 'new' package
3962 (if (looking-at "\\<new\\>")
3963 (goto-char (match-end 0))
3964 (setq nest-count (1+ nest-count)
3965 found (<= nest-count 0))))
3966
3967 ;; all the other block starts
3968 (t
3969 (if (not first)
3970 (setq nest-count (1+ nest-count)))
3971 (setq found (<= nest-count 0))
3972 (forward-word-strictly 1))) ; end of 'cond'
3973
3974 (setq first nil))
3975
3976 (if found
3977 t
3978 (if noerror
3979 nil
3980 (error "No matching end")))
3981 ))
3982
3983
3984 (defun ada-search-ignore-string-comment
3985 (search-re &optional backward limit paramlists search-func)
3986 "Regexp-search for SEARCH-RE, ignoring comments, strings.
3987 Returns a cons cell of begin and end of match data or nil, if not found.
3988 If BACKWARD is non-nil, search backward; search forward otherwise.
3989 The search stops at pos LIMIT.
3990 If PARAMLISTS is nil, ignore parameter lists.
3991 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
3992 in case we are searching for a constant string.
3993 Point is moved at the beginning of the SEARCH-RE."
3994 (let (found
3995 begin
3996 end
3997 parse-result)
3998
3999 ;; FIXME: need to pass BACKWARD to search-func!
4000 (unless search-func
4001 (setq search-func (if backward 're-search-backward 're-search-forward)))
4002
4003 ;;
4004 ;; search until found or end-of-buffer
4005 ;; We have to test that we do not look further than limit
4006 ;;
4007 (with-syntax-table ada-mode-symbol-syntax-table
4008 (while (and (not found)
4009 (or (not limit)
4010 (or (and backward (<= limit (point)))
4011 (>= limit (point))))
4012 (funcall search-func search-re limit 1))
4013 (setq begin (match-beginning 0))
4014 (setq end (match-end 0))
4015 (setq parse-result (parse-partial-sexp (point-at-bol) (point)))
4016 (cond
4017 ;;
4018 ;; If inside a string, skip it (and the following comments)
4019 ;;
4020 ((ada-in-string-p parse-result)
4021 (if (featurep 'xemacs)
4022 (search-backward "\"" nil t)
4023 (goto-char (nth 8 parse-result)))
4024 (unless backward (forward-sexp 1)))
4025 ;;
4026 ;; If inside a comment, skip it (and the following comments)
4027 ;; There is a special code for comments at the end of the file
4028 ;;
4029 ((ada-in-comment-p parse-result)
4030 (if (featurep 'xemacs)
4031 (progn
4032 (forward-line 1)
4033 (beginning-of-line)
4034 (forward-comment -1))
4035 (goto-char (nth 8 parse-result)))
4036 (unless backward
4037 ;; at the end of the file, it is not possible to skip a comment
4038 ;; so we just go at the end of the line
4039 (if (forward-comment 1)
4040 (progn
4041 (forward-comment 1000)
4042 (beginning-of-line))
4043 (end-of-line))))
4044 ;;
4045 ;; directly in front of a comment => skip it, if searching forward
4046 ;;
4047 ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
4048 (unless backward (progn (forward-char -1) (forward-comment 1000))))
4049
4050 ;;
4051 ;; found a parameter-list but should ignore it => skip it
4052 ;;
4053 ((and (not paramlists) (ada-in-paramlist-p))
4054 (if backward
4055 (search-backward "(" nil t)
4056 (search-forward ")" nil t)))
4057 ;;
4058 ;; found what we were looking for
4059 ;;
4060 (t
4061 (setq found t))))) ; end of loop
4062
4063 (if found
4064 (cons begin end)
4065 nil)))
4066
4067 ;; -------------------------------------------------------
4068 ;; -- Testing the position of the cursor
4069 ;; -------------------------------------------------------
4070
4071 (defun ada-in-decl-p ()
4072 "Return t if point is inside a declarative part.
4073 Assumes point to be at the end of a statement."
4074 (or (ada-in-paramlist-p)
4075 (save-excursion
4076 (ada-goto-decl-start t))))
4077
4078
4079 (defun ada-looking-at-semi-or ()
4080 "Return t if looking at an `or' following a semicolon."
4081 (save-excursion
4082 (and (looking-at "\\<or\\>")
4083 (progn
4084 (forward-word-strictly 1)
4085 (ada-goto-stmt-start)
4086 (looking-at "\\<or\\>")))))
4087
4088
4089 (defun ada-looking-at-semi-private ()
4090 "Return t if looking at the start of a private section in a package.
4091 Return nil if the private is part of the package name, as in
4092 'private package A is...' (this can only happen at top level)."
4093 (save-excursion
4094 (and (looking-at "\\<private\\>")
4095 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4096
4097 ;; Make sure this is the start of a private section (ie after
4098 ;; a semicolon or just after the package declaration, but not
4099 ;; after a 'type ... is private' or 'is new ... with private'
4100 ;;
4101 ;; Note that a 'private' statement at the beginning of the buffer
4102 ;; does not indicate a private section, since this is instead a
4103 ;; 'private procedure ...'
4104 (progn (forward-comment -1000)
4105 (and (not (bobp))
4106 (or (= (char-before) ?\;)
4107 (and (forward-word-strictly -3)
4108 (looking-at "\\<package\\>"))))))))
4109
4110
4111 (defun ada-in-paramlist-p ()
4112 "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
4113 (save-excursion
4114 (and
4115 (ada-search-ignore-string-comment "(\\|)" t nil t)
4116 ;; inside parentheses ?
4117 (= (char-after) ?\()
4118
4119 ;; We could be looking at two things here:
4120 ;; operator definition: function "." (
4121 ;; subprogram definition: procedure .... (
4122 ;; Let's skip back over the first one
4123 (progn
4124 (skip-chars-backward " \t\n")
4125 (if (= (char-before) ?\")
4126 (backward-char 3)
4127 (backward-word-strictly 1))
4128 t)
4129
4130 ;; and now over the second one
4131 (backward-word-strictly 1)
4132
4133 ;; We should ignore the case when the reserved keyword is in a
4134 ;; comment (for instance, when we have:
4135 ;; -- .... package
4136 ;; Test (A)
4137 ;; we should return nil
4138
4139 (not (ada-in-string-or-comment-p))
4140
4141 ;; right keyword two words before parenthesis ?
4142 ;; Type is in this list because of discriminants
4143 ;; pragma is not, because the syntax is that of a subprogram call.
4144 (looking-at (eval-when-compile
4145 (concat "\\<\\("
4146 "procedure\\|function\\|body\\|"
4147 "task\\|entry\\|accept\\|"
4148 "access[ \t]+procedure\\|"
4149 "access[ \t]+function\\|"
4150 "type\\)\\>"))))))
4151
4152 (defun ada-search-ignore-complex-boolean (regexp backwardp)
4153 "Search for REGEXP, ignoring comments, strings, `and then', `or else'.
4154 If BACKWARDP is non-nil, search backward; search forward otherwise."
4155 (let (result)
4156 (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
4157 (save-excursion (forward-word-strictly -1)
4158 (looking-at "and then\\|or else"))))
4159 result))
4160
4161 (defun ada-in-open-paren-p ()
4162 "Non-nil if in an open parenthesis.
4163 Return value is the position of the first non-ws behind the last unclosed
4164 parenthesis, or nil."
4165 (save-excursion
4166 (let ((parse (parse-partial-sexp
4167 (point)
4168 (or (car (ada-search-ignore-complex-boolean
4169 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4170 t))
4171 (point-min)))))
4172
4173 (if (nth 1 parse)
4174 (progn
4175 (goto-char (1+ (nth 1 parse)))
4176
4177 ;; Skip blanks, if they are not followed by a comment
4178 ;; See:
4179 ;; type A is ( Value_0,
4180 ;; Value_1);
4181 ;; type B is ( -- comment
4182 ;; Value_2);
4183
4184 (if (or (not ada-indent-handle-comment-special)
4185 (not (looking-at "[ \t]+--")))
4186 (skip-chars-forward " \t"))
4187
4188 (point))))))
4189
4190 \f
4191 ;; -----------------------------------------------------------
4192 ;; -- Behavior Of TAB Key
4193 ;; -----------------------------------------------------------
4194
4195 (defun ada-tab ()
4196 "Do indenting or tabbing according to `ada-tab-policy'.
4197 In Transient Mark mode, if the mark is active, operate on the contents
4198 of the region. Otherwise, operate only on the current line."
4199 (interactive)
4200 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
4201 ((eq ada-tab-policy 'indent-auto)
4202 (if (ada-region-selected)
4203 (ada-indent-region (region-beginning) (region-end))
4204 (ada-indent-current)))
4205 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4206 ))
4207
4208 (defun ada-untab (_arg)
4209 "Delete leading indenting according to `ada-tab-policy'."
4210 ;; FIXME: ARG is ignored
4211 (interactive "P")
4212 (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
4213 ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
4214 ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
4215 ))
4216
4217 (defun ada-indent-current-function ()
4218 "Ada mode version of the `indent-line-function'."
4219 (interactive "*")
4220 (let ((starting-point (point-marker)))
4221 (beginning-of-line)
4222 (ada-tab)
4223 (if (< (point) starting-point)
4224 (goto-char starting-point))
4225 (set-marker starting-point nil)
4226 ))
4227
4228 (defun ada-tab-hard ()
4229 "Indent current line to next tab stop."
4230 (interactive)
4231 (save-excursion
4232 (beginning-of-line)
4233 (insert-char ? ada-indent))
4234 (if (bolp) (forward-char ada-indent)))
4235
4236 (defun ada-untab-hard ()
4237 "Indent current line to previous tab stop."
4238 (interactive)
4239 (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent)))
4240
4241 \f
4242 ;; ------------------------------------------------------------
4243 ;; -- Miscellaneous
4244 ;; ------------------------------------------------------------
4245
4246 ;; Not needed any more for Emacs 21.2, but still needed for backward
4247 ;; compatibility
4248 (defun ada-remove-trailing-spaces ()
4249 "Remove trailing spaces in the whole buffer."
4250 (interactive)
4251 (save-match-data
4252 (save-excursion
4253 (save-restriction
4254 (widen)
4255 (goto-char (point-min))
4256 (while (re-search-forward "[ \t]+$" (point-max) t)
4257 (replace-match "" nil nil))))))
4258
4259 (defun ada-gnat-style ()
4260 "Clean up comments, `(' and `,' for GNAT style checking switch."
4261 (interactive)
4262 (save-excursion
4263
4264 ;; The \n is required, or the line after an empty comment line is
4265 ;; simply ignored.
4266 (goto-char (point-min))
4267 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
4268 (replace-match "-- \\1")
4269 (forward-line 1)
4270 (beginning-of-line))
4271
4272 (goto-char (point-min))
4273 (while (re-search-forward "\\>(" nil t)
4274 (if (not (ada-in-string-or-comment-p))
4275 (replace-match " (")))
4276 (goto-char (point-min))
4277 (while (re-search-forward ";--" nil t)
4278 (forward-char -1)
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 ")[ \t]+)" nil t)
4287 (if (not (ada-in-string-or-comment-p))
4288 (replace-match "))")))
4289 (goto-char (point-min))
4290 (while (re-search-forward "\\>:" nil t)
4291 (if (not (ada-in-string-or-comment-p))
4292 (replace-match " :")))
4293
4294 ;; Make sure there is a space after a ','.
4295 ;; Always go back to the beginning of the match, since otherwise
4296 ;; a statement like ('F','D','E') is incorrectly modified.
4297 (goto-char (point-min))
4298 (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
4299 (if (not (save-excursion
4300 (goto-char (match-beginning 0))
4301 (ada-in-string-or-comment-p)))
4302 (replace-match ", \\1")))
4303
4304 ;; Operators should be surrounded by spaces.
4305 (goto-char (point-min))
4306 (while (re-search-forward
4307 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4308 nil t)
4309 (goto-char (match-beginning 1))
4310 (if (or (looking-at "--")
4311 (ada-in-string-or-comment-p))
4312 (progn
4313 (forward-line 1)
4314 (beginning-of-line))
4315 (cond
4316 ((string= (match-string 1) "/=")
4317 (replace-match " /= "))
4318 ((string= (match-string 1) "..")
4319 (replace-match " .. "))
4320 ((string= (match-string 1) "**")
4321 (replace-match " ** "))
4322 ((string= (match-string 1) ":=")
4323 (replace-match " := "))
4324 (t
4325 (replace-match " \\1 ")))
4326 (forward-char 1)))
4327 ))
4328
4329
4330 \f
4331 ;; -------------------------------------------------------------
4332 ;; -- Moving To Procedures/Packages/Statements
4333 ;; -------------------------------------------------------------
4334
4335 (defun ada-move-to-start ()
4336 "Move point to the matching start of the current Ada structure."
4337 (interactive)
4338 (let ((pos (point)))
4339 (with-syntax-table ada-mode-symbol-syntax-table
4340
4341 (save-excursion
4342 ;;
4343 ;; do nothing if in string or comment or not on 'end ...;'
4344 ;; or if an error occurs during processing
4345 ;;
4346 (or
4347 (ada-in-string-or-comment-p)
4348 (and (progn
4349 (or (looking-at "[ \t]*\\<end\\>")
4350 (backward-word-strictly 1))
4351 (or (looking-at "[ \t]*\\<end\\>")
4352 (backward-word-strictly 1))
4353 (or (looking-at "[ \t]*\\<end\\>")
4354 (error "Not on end ...;")))
4355 (ada-goto-matching-start 1)
4356 (setq pos (point))
4357
4358 ;;
4359 ;; on 'begin' => go on, according to user option
4360 ;;
4361 ada-move-to-declaration
4362 (looking-at "\\<begin\\>")
4363 (ada-goto-decl-start)
4364 (setq pos (point))))
4365
4366 ) ; end of save-excursion
4367
4368 ;; now really move to the found position
4369 (goto-char pos))))
4370
4371 (defun ada-move-to-end ()
4372 "Move point to the end of the block around point.
4373 Moves to `begin' if in a declarative part."
4374 (interactive)
4375 (let ((pos (point))
4376 decl-start)
4377 (with-syntax-table ada-mode-symbol-syntax-table
4378
4379 (save-excursion
4380
4381 (cond
4382 ;; Go to the beginning of the current word, and check if we are
4383 ;; directly on 'begin'
4384 ((save-excursion
4385 (skip-syntax-backward "w")
4386 (looking-at "\\<begin\\>"))
4387 (ada-goto-matching-end 1))
4388
4389 ;; on first line of subprogram body
4390 ;; Do nothing for specs or generic instantiation, since these are
4391 ;; handled as the general case (find the enclosing block)
4392 ;; We also need to make sure that we ignore nested subprograms
4393 ((save-excursion
4394 (and (skip-syntax-backward "w")
4395 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4396 (ada-search-ignore-string-comment "is\\|;")
4397 (not (= (char-before) ?\;))
4398 ))
4399 (skip-syntax-backward "w")
4400 (ada-goto-matching-end 0 t))
4401
4402 ;; on first line of task declaration
4403 ((save-excursion
4404 (and (ada-goto-stmt-start)
4405 (looking-at "\\<task\\>" )
4406 (forward-word-strictly 1)
4407 (ada-goto-next-non-ws)
4408 (looking-at "\\<body\\>")))
4409 (ada-search-ignore-string-comment "begin" nil nil nil
4410 'word-search-forward))
4411 ;; accept block start
4412 ((save-excursion
4413 (and (ada-goto-stmt-start)
4414 (looking-at "\\<accept\\>" )))
4415 (ada-goto-matching-end 0))
4416 ;; package start
4417 ((save-excursion
4418 (setq decl-start (and (ada-goto-decl-start t) (point)))
4419 (and decl-start (looking-at "\\<package\\>")))
4420 (ada-goto-matching-end 1))
4421
4422 ;; On a "declare" keyword
4423 ((save-excursion
4424 (skip-syntax-backward "w")
4425 (looking-at "\\<declare\\>"))
4426 (ada-goto-matching-end 0 t))
4427
4428 ;; inside a 'begin' ... 'end' block
4429 (decl-start
4430 (goto-char decl-start)
4431 (ada-goto-matching-end 0 t))
4432
4433 ;; (hopefully ;-) everything else
4434 (t
4435 (ada-goto-matching-end 1)))
4436 (setq pos (point))
4437 )
4438
4439 ;; now really move to the position found
4440 (goto-char pos))))
4441
4442 (defun ada-next-procedure ()
4443 "Move point to next procedure."
4444 (interactive)
4445 (end-of-line)
4446 (if (re-search-forward ada-procedure-start-regexp nil t)
4447 (goto-char (match-beginning 4))
4448 (error "No more functions/procedures/tasks")))
4449
4450 (defun ada-previous-procedure ()
4451 "Move point to previous procedure."
4452 (interactive)
4453 (beginning-of-line)
4454 (if (re-search-backward ada-procedure-start-regexp nil t)
4455 (goto-char (match-beginning 4))
4456 (error "No more functions/procedures/tasks")))
4457
4458 (defun ada-next-package ()
4459 "Move point to next package."
4460 (interactive)
4461 (end-of-line)
4462 (if (re-search-forward ada-package-start-regexp nil t)
4463 (goto-char (match-beginning 1))
4464 (error "No more packages")))
4465
4466 (defun ada-previous-package ()
4467 "Move point to previous package."
4468 (interactive)
4469 (beginning-of-line)
4470 (if (re-search-backward ada-package-start-regexp nil t)
4471 (goto-char (match-beginning 1))
4472 (error "No more packages")))
4473
4474 \f
4475 ;; ------------------------------------------------------------
4476 ;; -- Define keymap and menus for Ada
4477 ;; -------------------------------------------------------------
4478
4479 (defun ada-create-keymap ()
4480 "Create the keymap associated with the Ada mode."
4481
4482 ;; All non-standard keys go into ada-mode-extra-map
4483 (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map)
4484
4485 ;; Indentation and Formatting
4486 (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
4487 (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
4488 (define-key ada-mode-map "\t" 'ada-tab)
4489 (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
4490 (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
4491 (define-key ada-mode-map [(shift tab)] 'ada-untab)
4492 (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
4493 ;; We don't want to make meta-characters case-specific.
4494
4495 ;; Movement
4496 (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
4497 (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
4498 (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
4499 (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
4500
4501 ;; Compilation
4502 (unless (lookup-key ada-mode-map "\C-c\C-c")
4503 (define-key ada-mode-map "\C-c\C-c" 'compile))
4504
4505 ;; Casing
4506 (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
4507 (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
4508 (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
4509 (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
4510
4511 ;; On XEmacs, you can easily specify whether DEL should deletes
4512 ;; one character forward or one character backward. Take this into
4513 ;; account
4514 (define-key ada-mode-map
4515 (if (boundp 'delete-key-deletes-forward) [backspace] "\177")
4516 'backward-delete-char-untabify)
4517
4518 ;; Make body
4519 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4520
4521 ;; Use predefined function of Emacs19 for comments (RE)
4522 (define-key ada-mode-map "\C-c;" 'comment-region)
4523 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4524
4525 ;; The following keys are bound to functions defined in ada-xref.el or
4526 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4527 ;; and activated only if the right compiler is used
4528
4529 (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
4530 'ada-point-and-xref)
4531 (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
4532
4533 (define-key ada-mode-extra-map "o" 'ff-find-other-file)
4534 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4535 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4536 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4537 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4538 (define-key ada-mode-extra-map "c" 'ada-change-prj)
4539 (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
4540 (define-key ada-mode-extra-map "g" 'ada-gdb-application)
4541 (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
4542 (define-key ada-mode-extra-map "r" 'ada-run-application)
4543 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4544 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4545 (define-key ada-mode-extra-map "l" 'ada-find-local-references)
4546 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4547 (define-key ada-mode-extra-map "f" 'ada-find-file)
4548
4549 (define-key ada-mode-extra-map "u" 'ada-prj-edit)
4550
4551 (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
4552
4553 ;; The templates, defined in ada-stmt.el
4554
4555 (let ((map (make-sparse-keymap)))
4556 (define-key map "h" 'ada-header)
4557 (define-key map "\C-a" 'ada-array)
4558 (define-key map "b" 'ada-exception-block)
4559 (define-key map "d" 'ada-declare-block)
4560 (define-key map "c" 'ada-case)
4561 (define-key map "\C-e" 'ada-elsif)
4562 (define-key map "e" 'ada-else)
4563 (define-key map "\C-k" 'ada-package-spec)
4564 (define-key map "k" 'ada-package-body)
4565 (define-key map "\C-p" 'ada-procedure-spec)
4566 (define-key map "p" 'ada-subprogram-body)
4567 (define-key map "\C-f" 'ada-function-spec)
4568 (define-key map "f" 'ada-for-loop)
4569 (define-key map "i" 'ada-if)
4570 (define-key map "l" 'ada-loop)
4571 (define-key map "\C-r" 'ada-record)
4572 (define-key map "\C-s" 'ada-subtype)
4573 (define-key map "S" 'ada-tabsize)
4574 (define-key map "\C-t" 'ada-task-spec)
4575 (define-key map "t" 'ada-task-body)
4576 (define-key map "\C-y" 'ada-type)
4577 (define-key map "\C-v" 'ada-private)
4578 (define-key map "u" 'ada-use)
4579 (define-key map "\C-u" 'ada-with)
4580 (define-key map "\C-w" 'ada-when)
4581 (define-key map "w" 'ada-while-loop)
4582 (define-key map "\C-x" 'ada-exception)
4583 (define-key map "x" 'ada-exit)
4584 (define-key ada-mode-extra-map "t" map))
4585 )
4586
4587
4588 (defun ada-create-menu ()
4589 "Create the Ada menu as shown in the menu bar."
4590 (let ((m '("Ada"
4591 ("Help"
4592 ["Ada Mode" (info "ada-mode") t]
4593 ["GNAT User's Guide" (info "gnat_ugn")
4594 (eq ada-which-compiler 'gnat)]
4595 ["GNAT Reference Manual" (info "gnat_rm")
4596 (eq ada-which-compiler 'gnat)]
4597 ["Gcc Documentation" (info "gcc")
4598 (eq ada-which-compiler 'gnat)]
4599 ["Gdb Documentation" (info "gdb")
4600 (eq ada-which-compiler 'gnat)]
4601 ["Ada95 Reference Manual" (info "arm95") t])
4602 ("Options" :included (derived-mode-p 'ada-mode)
4603 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4604 :style toggle :selected ada-auto-case]
4605 ["Auto Indent After Return"
4606 (setq ada-indent-after-return (not ada-indent-after-return))
4607 :style toggle :selected ada-indent-after-return]
4608 ["Automatically Recompile For Cross-references"
4609 (setq ada-xref-create-ali (not ada-xref-create-ali))
4610 :style toggle :selected ada-xref-create-ali
4611 :included (eq ada-which-compiler 'gnat)]
4612 ["Confirm Commands"
4613 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4614 :style toggle :selected ada-xref-confirm-compile
4615 :included (eq ada-which-compiler 'gnat)]
4616 ["Show Cross-references In Other Buffer"
4617 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4618 :style toggle :selected ada-xref-other-buffer
4619 :included (eq ada-which-compiler 'gnat)]
4620 ["Tight Integration With GNU Visual Debugger"
4621 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4622 :style toggle :selected ada-tight-gvd-integration
4623 :included (string-match "gvd" ada-prj-default-debugger)])
4624 ["Customize" (customize-group 'ada)
4625 :included (fboundp 'customize-group)]
4626 ["Check file" ada-check-current t]
4627 ["Compile file" ada-compile-current t]
4628 ["Set main and Build" ada-set-main-compile-application t]
4629 ["Show main" ada-show-current-main t]
4630 ["Build" ada-compile-application t]
4631 ["Run" ada-run-application t]
4632 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4633 ["------" nil nil]
4634 ("Project"
4635 ["Show project" ada-show-current-project t]
4636 ["Load..." ada-set-default-project-file t]
4637 ["New..." ada-prj-new t]
4638 ["Edit..." ada-prj-edit t])
4639 ("Goto" :included (derived-mode-p 'ada-mode)
4640 ["Goto Declaration/Body" ada-goto-declaration
4641 (eq ada-which-compiler 'gnat)]
4642 ["Goto Body" ada-goto-body
4643 (eq ada-which-compiler 'gnat)]
4644 ["Goto Declaration Other Frame"
4645 ada-goto-declaration-other-frame
4646 (eq ada-which-compiler 'gnat)]
4647 ["Goto Previous Reference" ada-xref-goto-previous-reference
4648 (eq ada-which-compiler 'gnat)]
4649 ["List Local References" ada-find-local-references
4650 (eq ada-which-compiler 'gnat)]
4651 ["List References" ada-find-references
4652 (eq ada-which-compiler 'gnat)]
4653 ["Goto Reference To Any Entity" ada-find-any-references
4654 (eq ada-which-compiler 'gnat)]
4655 ["Goto Parent Unit" ada-goto-parent
4656 (eq ada-which-compiler 'gnat)]
4657 ["--" nil nil]
4658 ["Next compilation error" next-error t]
4659 ["Previous Package" ada-previous-package t]
4660 ["Next Package" ada-next-package t]
4661 ["Previous Procedure" ada-previous-procedure t]
4662 ["Next Procedure" ada-next-procedure t]
4663 ["Goto Start Of Statement" ada-move-to-start t]
4664 ["Goto End Of Statement" ada-move-to-end t]
4665 ["-" nil nil]
4666 ["Other File" ff-find-other-file t]
4667 ["Other File Other Window" ada-ff-other-window t])
4668 ("Edit" :included (derived-mode-p 'ada-mode)
4669 ["Search File On Source Path" ada-find-file t]
4670 ["------" nil nil]
4671 ["Complete Identifier" ada-complete-identifier t]
4672 ["-----" nil nil]
4673 ["Indent Line" ada-indent-current-function t]
4674 ["Justify Current Indentation" ada-justified-indent-current t]
4675 ["Indent Lines in Selection" ada-indent-region t]
4676 ["Indent Lines in File"
4677 (ada-indent-region (point-min) (point-max)) t]
4678 ["Format Parameter List" ada-format-paramlist t]
4679 ["-" nil nil]
4680 ["Comment Selection" comment-region t]
4681 ["Uncomment Selection" ada-uncomment-region t]
4682 ["--" nil nil]
4683 ["Fill Comment Paragraph" fill-paragraph t]
4684 ["Fill Comment Paragraph Justify"
4685 ada-fill-comment-paragraph-justify t]
4686 ["Fill Comment Paragraph Postfix"
4687 ada-fill-comment-paragraph-postfix t]
4688 ["---" nil nil]
4689 ["Adjust Case Selection" ada-adjust-case-region t]
4690 ["Adjust Case in File" ada-adjust-case-buffer t]
4691 ["Create Case Exception" ada-create-case-exception t]
4692 ["Create Case Exception Substring"
4693 ada-create-case-exception-substring t]
4694 ["Reload Case Exceptions" ada-case-read-exceptions t]
4695 ["----" nil nil]
4696 ["Make body for subprogram" ada-make-subprogram-body t]
4697 ["-----" nil nil]
4698 ["Narrow to subprogram" ada-narrow-to-defun t])
4699 ("Templates"
4700 :included (derived-mode-p 'ada-mode)
4701 ["Header" ada-header t]
4702 ["-" nil nil]
4703 ["Package Body" ada-package-body t]
4704 ["Package Spec" ada-package-spec t]
4705 ["Function Spec" ada-function-spec t]
4706 ["Procedure Spec" ada-procedure-spec t]
4707 ["Proc/func Body" ada-subprogram-body t]
4708 ["Task Body" ada-task-body t]
4709 ["Task Spec" ada-task-spec t]
4710 ["Declare Block" ada-declare-block t]
4711 ["Exception Block" ada-exception-block t]
4712 ["--" nil nil]
4713 ["Entry" ada-entry t]
4714 ["Entry family" ada-entry-family t]
4715 ["Select" ada-select t]
4716 ["Accept" ada-accept t]
4717 ["Or accept" ada-or-accept t]
4718 ["Or delay" ada-or-delay t]
4719 ["Or terminate" ada-or-terminate t]
4720 ["---" nil nil]
4721 ["Type" ada-type t]
4722 ["Private" ada-private t]
4723 ["Subtype" ada-subtype t]
4724 ["Record" ada-record t]
4725 ["Array" ada-array t]
4726 ["----" nil nil]
4727 ["If" ada-if t]
4728 ["Else" ada-else t]
4729 ["Elsif" ada-elsif t]
4730 ["Case" ada-case t]
4731 ["-----" nil nil]
4732 ["While Loop" ada-while-loop t]
4733 ["For Loop" ada-for-loop t]
4734 ["Loop" ada-loop t]
4735 ["------" nil nil]
4736 ["Exception" ada-exception t]
4737 ["Exit" ada-exit t]
4738 ["When" ada-when t])
4739 )))
4740
4741 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4742 (if (featurep 'xemacs)
4743 (progn
4744 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4745 (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4746
4747 \f
4748 ;; -------------------------------------------------------
4749 ;; Commenting/Uncommenting code
4750 ;; The following two calls are provided to enhance the standard
4751 ;; comment-region function, which only allows uncommenting if the
4752 ;; comment is at the beginning of a line. If the line have been re-indented,
4753 ;; we are unable to use comment-region, which makes no sense.
4754 ;;
4755 ;; In addition, we provide an interface to the standard comment handling
4756 ;; function for justifying the comments.
4757 ;; -------------------------------------------------------
4758
4759 (defadvice comment-region (before ada-uncomment-anywhere disable)
4760 (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
4761 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4762 (derived-mode-p 'ada-mode))
4763 (save-excursion
4764 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4765 (goto-char beg)
4766 (while (re-search-forward cs end t)
4767 (replace-match comment-start))
4768 ))))
4769
4770 (defun ada-uncomment-region (beg end &optional arg)
4771 "Uncomment region BEG .. END.
4772 ARG gives number of comment characters."
4773 (interactive "r\nP")
4774
4775 ;; This advice is not needed anymore with Emacs21. However, for older
4776 ;; versions, as well as for XEmacs, we still need to enable it.
4777 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4778 (progn
4779 (ad-activate 'comment-region)
4780 (comment-region beg end (- (or arg 2)))
4781 (ad-deactivate 'comment-region))
4782 (comment-region beg end (list (- (or arg 2))))
4783 (ada-indent-region beg end)))
4784
4785 (defun ada-fill-comment-paragraph-justify ()
4786 "Fill current comment paragraph and justify each line as well."
4787 (interactive)
4788 (ada-fill-comment-paragraph 'full))
4789
4790 (defun ada-fill-comment-paragraph-postfix ()
4791 "Fill current comment paragraph and justify each line as well.
4792 Adds `ada-fill-comment-postfix' at the end of each line."
4793 (interactive)
4794 (ada-fill-comment-paragraph 'full t))
4795
4796 (defun ada-fill-comment-paragraph (&optional justify postfix)
4797 "Fill the current comment paragraph.
4798 If JUSTIFY is non-nil, each line is justified as well.
4799 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4800 to each line filled and justified.
4801 The paragraph is indented on the first line."
4802 (interactive "P")
4803
4804 ;; check if inside comment or just in front a comment
4805 (if (and (not (ada-in-comment-p))
4806 (not (looking-at "[ \t]*--")))
4807 (error "Not inside comment"))
4808
4809 (let* (indent from to
4810 (opos (point-marker))
4811
4812 ;; Sets this variable to nil, otherwise it prevents
4813 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4814 (parse-sexp-lookup-properties nil)
4815
4816 fill-prefix
4817 (fill-column (current-fill-column)))
4818
4819 ;; Find end of paragraph
4820 (back-to-indentation)
4821 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4822 (forward-line 1)
4823
4824 ;; If we were at the last line in the buffer, create a dummy empty
4825 ;; line at the end of the buffer.
4826 (if (eobp)
4827 (insert "\n")
4828 (back-to-indentation)))
4829 (beginning-of-line)
4830 (setq to (point-marker))
4831 (goto-char opos)
4832
4833 ;; Find beginning of paragraph
4834 (back-to-indentation)
4835 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4836 (forward-line -1)
4837 (back-to-indentation))
4838
4839 ;; We want one line above the first one, unless we are at the beginning
4840 ;; of the buffer
4841 (unless (bobp)
4842 (forward-line 1))
4843 (beginning-of-line)
4844 (setq from (point-marker))
4845
4846 ;; Calculate the indentation we will need for the paragraph
4847 (back-to-indentation)
4848 (setq indent (current-column))
4849 ;; unindent the first line of the paragraph
4850 (delete-region from (point))
4851
4852 ;; Remove the old postfixes
4853 (goto-char from)
4854 (while (re-search-forward "--\n" to t)
4855 (replace-match "\n"))
4856
4857 (goto-char (1- to))
4858 (setq to (point-marker))
4859
4860 ;; Indent and justify the paragraph
4861 (setq fill-prefix ada-fill-comment-prefix)
4862 (set-left-margin from to indent)
4863 (if postfix
4864 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4865
4866 (fill-region-as-paragraph from to justify)
4867
4868 ;; Add the postfixes if required
4869 (if postfix
4870 (save-restriction
4871 (goto-char from)
4872 (narrow-to-region from to)
4873 (while (not (eobp))
4874 (end-of-line)
4875 (insert-char ? (- fill-column (current-column)))
4876 (insert ada-fill-comment-postfix)
4877 (forward-line))
4878 ))
4879
4880 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4881 ;; inserted at the end. Delete it
4882 (if (or (featurep 'xemacs)
4883 (<= emacs-major-version 19)
4884 (and (= emacs-major-version 20)
4885 (<= emacs-minor-version 2)))
4886 (progn
4887 (goto-char to)
4888 (end-of-line)
4889 (delete-char 1)))
4890
4891 (goto-char opos)))
4892
4893
4894 ;; ---------------------------------------------------
4895 ;; support for find-file.el
4896 ;; These functions are used by find-file to guess the file names from
4897 ;; unit names, and to find the other file (spec or body) from the current
4898 ;; file (body or spec).
4899 ;; It is also used to find in which function we are, so as to put the
4900 ;; cursor at the correct position.
4901 ;; Standard Ada does not force any relation between unit names and file names,
4902 ;; so some of these functions can only be a good approximation. However, they
4903 ;; are also overridden in `ada-xref'.el when we know that the user is using
4904 ;; GNAT.
4905 ;; ---------------------------------------------------
4906
4907 ;; Overridden when we work with GNAT, to use gnatkrunch
4908 (defun ada-make-filename-from-adaname (adaname)
4909 "Determine the filename in which ADANAME is found.
4910 This matches the GNAT default naming convention, except for
4911 pre-defined units."
4912 (while (string-match "\\." adaname)
4913 (setq adaname (replace-match "-" t t adaname)))
4914 (downcase adaname)
4915 )
4916
4917 (defun ada-other-file-name ()
4918 "Return the name of the other file.
4919 The name returned is the body if `current-buffer' is the spec,
4920 or the spec otherwise."
4921
4922 (let ((is-spec nil)
4923 (is-body nil)
4924 (suffixes ada-spec-suffixes)
4925 (name (buffer-file-name)))
4926
4927 ;; Guess whether we have a spec or a body, and get the basename of the
4928 ;; file. Since the extension may not start with '.', we can not use
4929 ;; file-name-extension
4930 (while (and (not is-spec)
4931 suffixes)
4932 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4933 (setq is-spec t
4934 name (match-string 1 name)))
4935 (setq suffixes (cdr suffixes)))
4936
4937 (if (not is-spec)
4938 (progn
4939 (setq suffixes ada-body-suffixes)
4940 (while (and (not is-body)
4941 suffixes)
4942 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4943 (setq is-body t
4944 name (match-string 1 name)))
4945 (setq suffixes (cdr suffixes)))))
4946
4947 ;; If this wasn't in either list, return name itself
4948 (if (not (or is-spec is-body))
4949 name
4950
4951 ;; Else find the other possible names
4952 (if is-spec
4953 (setq suffixes ada-body-suffixes)
4954 (setq suffixes ada-spec-suffixes))
4955 (setq is-spec name)
4956
4957 (while suffixes
4958
4959 ;; If we are using project file, search for the other file in all
4960 ;; the possible src directories.
4961
4962 (if (fboundp 'ada-find-src-file-in-dir)
4963 (let ((other
4964 (ada-find-src-file-in-dir
4965 (file-name-nondirectory (concat name (car suffixes))))))
4966 (if other
4967 (setq is-spec other)))
4968
4969 ;; Else search in the current directory
4970 (if (file-exists-p (concat name (car suffixes)))
4971 (setq is-spec (concat name (car suffixes)))))
4972 (setq suffixes (cdr suffixes)))
4973
4974 is-spec)))
4975
4976 (defun ada-which-function-are-we-in ()
4977 "Return the name of the function whose definition/declaration point is in.
4978 Used in `ff-pre-load-hook'."
4979 (setq ff-function-name nil)
4980 (save-excursion
4981 (end-of-line);; make sure we get the complete name
4982 (or (if (re-search-backward ada-procedure-start-regexp nil t)
4983 (setq ff-function-name (match-string 5)))
4984 (if (re-search-backward ada-package-start-regexp nil t)
4985 (setq ff-function-name (match-string 4))))
4986 ))
4987
4988
4989 (defvar ada-last-which-function-line -1
4990 "Last line on which `ada-which-function' was called.")
4991 (defvar ada-last-which-function-subprog 0
4992 "Last subprogram name returned by `ada-which-function'.")
4993 (make-variable-buffer-local 'ada-last-which-function-subprog)
4994 (make-variable-buffer-local 'ada-last-which-function-line)
4995
4996
4997 (defun ada-which-function ()
4998 "Return the name of the function whose body the point is in.
4999 This function works even in the case of nested subprograms, whereas the
5000 standard Emacs function `which-function' does not.
5001 Since the search can be long, the results are cached."
5002
5003 (let ((line (count-lines 1 (point)))
5004 (pos (point))
5005 end-pos
5006 func-name indent
5007 found)
5008
5009 ;; If this is the same line as before, simply return the same result
5010 (if (= line ada-last-which-function-line)
5011 ada-last-which-function-subprog
5012
5013 (save-excursion
5014 ;; In case the current line is also the beginning of the body
5015 (end-of-line)
5016
5017 ;; Are we looking at "function Foo\n (paramlist)"
5018 (skip-chars-forward " \t\n(")
5019
5020 (condition-case nil
5021 (up-list 1)
5022 (error nil))
5023
5024 (skip-chars-forward " \t\n")
5025 (if (looking-at "return")
5026 (progn
5027 (forward-word-strictly 1)
5028 (skip-chars-forward " \t\n")
5029 (skip-chars-forward "a-zA-Z0-9_'")))
5030
5031 ;; Can't simply do forward-word, in case the "is" is not on the
5032 ;; same line as the closing parenthesis
5033 (skip-chars-forward "is \t\n")
5034
5035 ;; No look for the closest subprogram body that has not ended yet.
5036 ;; Not that we expect all the bodies to be finished by "end <name>",
5037 ;; or a simple "end;" indented in the same column as the start of
5038 ;; the subprogram. The goal is to be as efficient as possible.
5039
5040 (while (and (not found)
5041 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5042
5043 ;; Get the function name, but not the properties, or this changes
5044 ;; the face in the mode line on Emacs 21
5045 (setq func-name (match-string-no-properties 3))
5046 (if (and (not (ada-in-comment-p))
5047 (not (save-excursion
5048 (goto-char (match-end 0))
5049 (looking-at "[ \t\n]*new"))))
5050 (save-excursion
5051 (back-to-indentation)
5052 (setq indent (current-column))
5053 (if (ada-search-ignore-string-comment
5054 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5055 (make-string indent ? ) "end;"))
5056 (setq end-pos (point))
5057 (setq end-pos (point-max)))
5058 (if (>= end-pos pos)
5059 (setq found func-name))))
5060 )
5061 (setq ada-last-which-function-line line
5062 ada-last-which-function-subprog found)
5063 found))))
5064
5065 (defun ada-ff-other-window ()
5066 "Find other file in other window using `ff-find-other-file'."
5067 (interactive)
5068 (and (fboundp 'ff-find-other-file)
5069 (ff-find-other-file t)))
5070
5071 (defun ada-set-point-accordingly ()
5072 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5073 (if ff-function-name
5074 (progn
5075 (goto-char (point-min))
5076 (unless (ada-search-ignore-string-comment
5077 (concat ff-function-name "\\b") nil)
5078 (goto-char (point-min))))))
5079
5080 (defun ada-get-body-name (&optional spec-name)
5081 "Return the file name for the body of SPEC-NAME.
5082 If SPEC-NAME is nil, return the body for the current package.
5083 Return nil if no body was found."
5084 (interactive)
5085
5086 (unless spec-name (setq spec-name (buffer-file-name)))
5087
5088 ;; Remove the spec extension. We can not simply remove the file extension,
5089 ;; but we need to take into account the specific non-GNAT extensions that the
5090 ;; user might have specified.
5091
5092 (let ((suffixes ada-spec-suffixes)
5093 end)
5094 (while suffixes
5095 (setq end (- (length spec-name) (length (car suffixes))))
5096 (if (string-equal (car suffixes) (substring spec-name end))
5097 (setq spec-name (substring spec-name 0 end)))
5098 (setq suffixes (cdr suffixes))))
5099
5100 ;; If find-file.el was available, use its functions
5101 (if (fboundp 'ff-get-file-name)
5102 (ff-get-file-name ada-search-directories-internal
5103 (ada-make-filename-from-adaname
5104 (file-name-nondirectory
5105 (file-name-sans-extension spec-name)))
5106 ada-body-suffixes)
5107 ;; Else emulate it very simply
5108 (concat (ada-make-filename-from-adaname
5109 (file-name-nondirectory
5110 (file-name-sans-extension spec-name)))
5111 ".adb")))
5112
5113 \f
5114 ;; ---------------------------------------------------
5115 ;; support for font-lock.el
5116 ;; Strings are a real pain in Ada because a single quote character is
5117 ;; overloaded as a string quote and type/instance delimiter. By default, a
5118 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5119 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5120 ;; syntax when the gods that created Ada determine them to be.
5121 ;;
5122 ;; This only works in Emacs. See the comments before the grammar functions
5123 ;; at the beginning of this file for how this is done with XEmacs.
5124 ;; ----------------------------------------------------
5125
5126 (defconst ada-font-lock-syntactic-keywords
5127 ;; Mark single quotes as having string quote syntax in 'c' instances.
5128 ;; We used to explicitly avoid ''' as a special case for fear the buffer
5129 ;; be highlighted as a string, but it seems this fear is unfounded.
5130 ;;
5131 ;; This sets the properties of the characters, so that ada-in-string-p
5132 ;; correctly handles '"' too...
5133 '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5134 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5135
5136 (defvar ada-font-lock-keywords
5137 (eval-when-compile
5138 (list
5139 ;;
5140 ;; handle "type T is access function return S;"
5141 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5142
5143 ;; preprocessor line
5144 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5145
5146 ;;
5147 ;; accept, entry, function, package (body), protected (body|type),
5148 ;; pragma, procedure, task (body) plus name.
5149 (list (concat
5150 "\\<\\("
5151 "accept\\|"
5152 "entry\\|"
5153 "function\\|"
5154 "package[ \t]+body\\|"
5155 "package\\|"
5156 "pragma\\|"
5157 "procedure\\|"
5158 "protected[ \t]+body\\|"
5159 "protected[ \t]+type\\|"
5160 "protected\\|"
5161 "task[ \t]+body\\|"
5162 "task[ \t]+type\\|"
5163 "task"
5164 "\\)\\>[ \t]*"
5165 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5166 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5167 ;;
5168 ;; Optional keywords followed by a type name.
5169 (list (concat ; ":[ \t]*"
5170 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5171 "[ \t]*"
5172 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5173 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5174
5175 ;;
5176 ;; Main keywords, except those treated specially below.
5177 (concat "\\<"
5178 (regexp-opt
5179 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5180 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5181 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5182 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5183 "null" "or" "others" "overriding" "private" "protected" "raise"
5184 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5185 "select" "separate" "synchronized" "tagged" "task" "terminate"
5186 "then" "until" "when" "while" "with" "xor") t)
5187 "\\>")
5188 ;;
5189 ;; Anything following end and not already fontified is a body name.
5190 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5191 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5192 ;;
5193 ;; Keywords followed by a type or function name.
5194 (list (concat "\\<\\("
5195 "new\\|of\\|subtype\\|type"
5196 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5197 '(1 font-lock-keyword-face)
5198 '(2 (if (match-beginning 4)
5199 font-lock-function-name-face
5200 font-lock-type-face) nil t))
5201 ;;
5202 ;; Keywords followed by a (comma separated list of) reference.
5203 ;; Note that font-lock only works on single lines, thus we can not
5204 ;; correctly highlight a with_clause that spans multiple lines.
5205 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5206 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5207 '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
5208
5209 ;;
5210 ;; Goto tags.
5211 '("<<\\(\\sw+\\)>>" 1 font-lock-constant-face)
5212
5213 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5214 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5215
5216 ;; Ada unnamed numerical constants
5217 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5218
5219 ))
5220 "Default expressions to highlight in Ada mode.")
5221
5222
5223 ;; ---------------------------------------------------------
5224 ;; Support for outline.el
5225 ;; ---------------------------------------------------------
5226
5227 (defun ada-outline-level ()
5228 "This is so that `current-column' DTRT in otherwise-hidden text."
5229 ;; patch from Dave Love <fx@gnu.org>
5230 (let (buffer-invisibility-spec)
5231 (save-excursion
5232 (back-to-indentation)
5233 (current-column))))
5234
5235 ;; ---------------------------------------------------------
5236 ;; Support for narrow-to-region
5237 ;; ---------------------------------------------------------
5238
5239 (defun ada-narrow-to-defun (&optional _arg)
5240 "Make text outside current subprogram invisible.
5241 The subprogram visible is the one that contains or follow point.
5242 Optional ARG is ignored.
5243 Use \\[widen] to go back to the full visibility for the buffer."
5244
5245 (interactive)
5246 (save-excursion
5247 (let (end)
5248 (widen)
5249 (forward-line 1)
5250 (ada-previous-procedure)
5251 (setq end (point-at-bol))
5252 (ada-move-to-end)
5253 (end-of-line)
5254 (narrow-to-region end (point))
5255 (message
5256 "Use M-x widen to get back to full visibility in the buffer"))))
5257
5258 ;; ---------------------------------------------------------
5259 ;; Automatic generation of code
5260 ;; The Ada mode has a set of function to automatically generate a subprogram
5261 ;; or package body from its spec.
5262 ;; These function only use a primary and basic algorithm, this could use a
5263 ;; lot of improvement.
5264 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5265 ;; body.
5266 ;; ----------------------------------------------------------
5267
5268 (defun ada-gen-treat-proc (match)
5269 "Make dummy body of a procedure/function specification.
5270 MATCH is a cons cell containing the start and end locations of the last search
5271 for `ada-procedure-start-regexp'."
5272 (goto-char (car match))
5273 (let (func-found procname functype)
5274 (cond
5275 ((or (looking-at "^[ \t]*procedure")
5276 (setq func-found (looking-at "^[ \t]*function")))
5277 ;; treat it as a proc/func
5278 (forward-word-strictly 2)
5279 (forward-word-strictly -1)
5280 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5281
5282 ;; goto end of procname
5283 (goto-char (cdr match))
5284
5285 ;; skip over parameterlist
5286 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5287 (forward-sexp))
5288
5289 ;; if function, skip over 'return' and result type.
5290 (if func-found
5291 (progn
5292 (forward-word-strictly 1)
5293 (skip-chars-forward " \t\n")
5294 (setq functype (buffer-substring (point)
5295 (progn
5296 (skip-chars-forward
5297 "a-zA-Z0-9_.")
5298 (point))))))
5299 ;; look for next non WS
5300 (cond
5301 ((looking-at "[ \t]*;")
5302 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5303 (ada-indent-newline-indent)
5304 (insert "is")
5305 (ada-indent-newline-indent)
5306 (if func-found
5307 (progn
5308 (insert "Result : " functype ";")
5309 (ada-indent-newline-indent)))
5310 (insert "begin")
5311 (ada-indent-newline-indent)
5312 (if func-found
5313 (insert "return Result;")
5314 (insert "null;"))
5315 (ada-indent-newline-indent)
5316 (insert "end " procname ";")
5317 (ada-indent-newline-indent)
5318 )
5319
5320 ((looking-at "[ \t\n]*is")
5321 ;; do nothing
5322 )
5323
5324 ((looking-at "[ \t\n]*rename")
5325 ;; do nothing
5326 )
5327
5328 (t
5329 (message "unknown syntax"))))
5330 (t
5331 (if (looking-at "^[ \t]*task")
5332 (progn
5333 (message "Task conversion is not yet implemented")
5334 (forward-word-strictly 2)
5335 (if (looking-at "[ \t]*;")
5336 (forward-line)
5337 (ada-move-to-end))
5338 ))))))
5339
5340 (defun ada-make-body ()
5341 "Create an Ada package body in the current buffer.
5342 The spec must be the previously visited buffer.
5343 This function typically is to be hooked into `ff-file-created-hook'."
5344 (delete-region (point-min) (point-max))
5345 (insert-buffer-substring (car (cdr (buffer-list))))
5346 (goto-char (point-min))
5347 (ada-mode)
5348
5349 (let (found ada-procedure-or-package-start-regexp)
5350 (if (setq found
5351 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5352 (progn (goto-char (cdr found))
5353 (insert " body")
5354 )
5355 (error "No package"))
5356
5357 (setq ada-procedure-or-package-start-regexp
5358 (concat ada-procedure-start-regexp
5359 "\\|"
5360 ada-package-start-regexp))
5361
5362 (while (setq found
5363 (ada-search-ignore-string-comment
5364 ada-procedure-or-package-start-regexp nil))
5365 (progn
5366 (goto-char (car found))
5367 (if (looking-at ada-package-start-regexp)
5368 (progn (goto-char (cdr found))
5369 (insert " body"))
5370 (ada-gen-treat-proc found))))))
5371
5372
5373 (defun ada-make-subprogram-body ()
5374 "Create a dummy subprogram body in package body file from spec surrounding point."
5375 (interactive)
5376 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5377 (spec (match-beginning 0))
5378 body-file)
5379 (if found
5380 (progn
5381 (goto-char spec)
5382 (if (and (re-search-forward "(\\|;" nil t)
5383 (= (char-before) ?\())
5384 (progn
5385 (ada-search-ignore-string-comment ")" nil)
5386 (ada-search-ignore-string-comment ";" nil)))
5387 (setq spec (buffer-substring spec (point)))
5388
5389 ;; If find-file.el was available, use its functions
5390 (setq body-file (ada-get-body-name))
5391 (if body-file
5392 (find-file body-file)
5393 (error "No body found for the package. Create it first"))
5394
5395 (save-restriction
5396 (widen)
5397 (goto-char (point-max))
5398 (forward-comment -10000)
5399 (re-search-backward "\\<end\\>" nil t)
5400 ;; Move to the beginning of the elaboration part, if any
5401 (re-search-backward "^begin" nil t)
5402 (newline)
5403 (forward-char -1)
5404 (insert spec)
5405 (re-search-backward ada-procedure-start-regexp nil t)
5406 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5407 ))
5408 (error "Not in subprogram spec"))))
5409
5410 ;; --------------------------------------------------------
5411 ;; Global initializations
5412 ;; --------------------------------------------------------
5413
5414 ;; Create the keymap once and for all. If we do that in ada-mode,
5415 ;; the keys changed in the user's .emacs have to be modified
5416 ;; every time
5417 (ada-create-keymap)
5418 (ada-create-menu)
5419
5420 ;; Add the default extensions (and set up speedbar)
5421 (ada-add-extensions ".ads" ".adb")
5422 ;; This two files are generated by GNAT when running with -gnatD
5423 (if (equal ada-which-compiler 'gnat)
5424 (ada-add-extensions ".ads.dg" ".adb.dg"))
5425
5426 ;; Read the special cases for exceptions
5427 (ada-case-read-exceptions)
5428
5429 ;; Setup auto-loading of the other Ada mode files.
5430 (autoload 'ada-change-prj "ada-xref" nil t)
5431 (autoload 'ada-check-current "ada-xref" nil t)
5432 (autoload 'ada-compile-application "ada-xref" nil t)
5433 (autoload 'ada-compile-current "ada-xref" nil t)
5434 (autoload 'ada-complete-identifier "ada-xref" nil t)
5435 (autoload 'ada-find-file "ada-xref" nil t)
5436 (autoload 'ada-find-any-references "ada-xref" nil t)
5437 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5438 (autoload 'ada-find-local-references "ada-xref" nil t)
5439 (autoload 'ada-find-references "ada-xref" nil t)
5440 (autoload 'ada-gdb-application "ada-xref" nil t)
5441 (autoload 'ada-goto-declaration "ada-xref" nil t)
5442 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5443 (autoload 'ada-goto-parent "ada-xref" nil t)
5444 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5445 (autoload 'ada-point-and-xref "ada-xref" nil t)
5446 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5447 (autoload 'ada-run-application "ada-xref" nil t)
5448 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5449 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5450 (autoload 'ada-set-main-compile-application "ada-xref" nil t)
5451 (autoload 'ada-show-current-main "ada-xref" nil t)
5452
5453 (autoload 'ada-customize "ada-prj" nil t)
5454 (autoload 'ada-prj-edit "ada-prj" nil t)
5455 (autoload 'ada-prj-new "ada-prj" nil t)
5456 (autoload 'ada-prj-save "ada-prj" nil t)
5457
5458 (autoload 'ada-array "ada-stmt" nil t)
5459 (autoload 'ada-case "ada-stmt" nil t)
5460 (autoload 'ada-declare-block "ada-stmt" nil t)
5461 (autoload 'ada-else "ada-stmt" nil t)
5462 (autoload 'ada-elsif "ada-stmt" nil t)
5463 (autoload 'ada-exception "ada-stmt" nil t)
5464 (autoload 'ada-exception-block "ada-stmt" nil t)
5465 (autoload 'ada-exit "ada-stmt" nil t)
5466 (autoload 'ada-for-loop "ada-stmt" nil t)
5467 (autoload 'ada-function-spec "ada-stmt" nil t)
5468 (autoload 'ada-header "ada-stmt" nil t)
5469 (autoload 'ada-if "ada-stmt" nil t)
5470 (autoload 'ada-loop "ada-stmt" nil t)
5471 (autoload 'ada-package-body "ada-stmt" nil t)
5472 (autoload 'ada-package-spec "ada-stmt" nil t)
5473 (autoload 'ada-private "ada-stmt" nil t)
5474 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5475 (autoload 'ada-record "ada-stmt" nil t)
5476 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5477 (autoload 'ada-subtype "ada-stmt" nil t)
5478 (autoload 'ada-tabsize "ada-stmt" nil t)
5479 (autoload 'ada-task-body "ada-stmt" nil t)
5480 (autoload 'ada-task-spec "ada-stmt" nil t)
5481 (autoload 'ada-type "ada-stmt" nil t)
5482 (autoload 'ada-use "ada-stmt" nil t)
5483 (autoload 'ada-when "ada-stmt" nil t)
5484 (autoload 'ada-while-loop "ada-stmt" nil t)
5485 (autoload 'ada-with "ada-stmt" nil t)
5486
5487 ;;; provide ourselves
5488 (provide 'ada-mode)
5489
5490 ;;; ada-mode.el ends here