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