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