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