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