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