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