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