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