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