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