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