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