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, 2008, 2009 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 3 of the License, or
17 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
28 ;; This mode is a major mode for editing Ada code. This is a major
29 ;; rewrite of the file packaged with Emacs-20. The Ada mode is
30 ;; composed of four Lisp files: ada-mode.el, ada-xref.el, ada-prj.el
31 ;; and ada-stmt.el. Only this file (ada-mode.el) is completely
32 ;; independent from the GNU Ada compiler GNAT, distributed by Ada
33 ;; Core Technologies. All the other files rely heavily on features
34 ;; provided only by GNAT.
36 ;; Note: this mode will not work with Emacs 19. If you are on a VMS
37 ;; system, where the latest version of Emacs is 19.28, you will need
38 ;; another file, called ada-vms.el, that provides some required
42 ;; Emacs should enter Ada mode automatically when you load an Ada file.
43 ;; By default, the valid extensions for Ada files are .ads, .adb or .ada
44 ;; If the ada-mode does not start automatically, then simply type the
45 ;; following command :
48 ;; By default, ada-mode is configured to take full advantage of the GNAT
49 ;; compiler (the menus will include the cross-referencing features,...).
50 ;; If you are using another compiler, you might want to set the following
51 ;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
53 ;; (setq ada-which-compiler 'generic)
55 ;; This mode requires find-file.el to be present on your system.
58 ;; The first Ada mode for GNU Emacs was written by V. Broman in
59 ;; 1985. He based his work on the already existing Modula-2 mode.
60 ;; This was distributed as ada.el in versions of Emacs prior to 19.29.
62 ;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
63 ;; several files with support for dired commands and other nice
64 ;; things. It is currently available from the PAL
65 ;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
67 ;; The probably very first Ada mode (called electric-ada.el) was
68 ;; written by Steven D. Litvintchouk and Steven M. Rosen for the
69 ;; Gosling Emacs. L. Slater based his development on ada.el and
72 ;; A complete rewrite by M. Heritsch and R. Ebert has been done.
73 ;; Some ideas from the Ada mode mailing list have been
74 ;; added. Some of the functionality of L. Slater's mode has not
75 ;; (yet) been recoded in this new mode. Perhaps you prefer sticking
78 ;; A complete rewrite for Emacs-20 / GNAT-3.11 has been done by Ada Core
82 ;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
83 ;; many patches included in this package.
84 ;; Christian Egli <Christian.Egli@hcsd.hac.com>:
85 ;; ada-imenu-generic-expression
86 ;; Many thanks also to the following persons that have contributed
88 ;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
89 ;; woodruff@stc.llnl.gov (John Woodruff)
90 ;; jj@ddci.dk (Jesper Joergensen)
91 ;; gse@ocsystems.com (Scott Evans)
92 ;; comar@gnat.com (Cyrille Comar)
93 ;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
94 ;; robin-reply@reagans.org
95 ;; and others for their valuable hints.
98 ;; Note: Every function in this package is compiler-independent.
99 ;; The names start with ada-
100 ;; The variables that the user can edit can all be modified through
101 ;; the customize mode. They are sorted in alphabetical order in this
104 ;; Supported packages.
105 ;; This package supports a number of other Emacs modes. These other modes
106 ;; should be loaded before the ada-mode, which will then setup some variables
107 ;; to improve the support for Ada code.
108 ;; Here is the list of these modes:
109 ;; `which-function-mode': Display the name of the subprogram the cursor is
110 ;; in in the mode line.
111 ;; `outline-mode': Provides the capability to collapse or expand the code
112 ;; for specific language constructs, for instance if you want to hide the
113 ;; code corresponding to a subprogram
114 ;; `align': This mode is now provided with Emacs 21, but can also be
115 ;; installed manually for older versions of Emacs. It provides the
116 ;; capability to automatically realign the selected region (for instance
117 ;; all ':=', ':' and '--' will be aligned on top of each other.
118 ;; `imenu': Provides a menu with the list of entities defined in the current
119 ;; buffer, and an easy way to jump to any of them
120 ;; `speedbar': Provides a separate file browser, and the capability for each
121 ;; file to see the list of entities defined in it and to jump to them
123 ;; `abbrev-mode': Provides the capability to define abbreviations, which
124 ;; are automatically expanded when you type them. See the Emacs manual.
126 (require 'find-file nil t
)
127 (require 'align nil t
)
128 (require 'which-func nil t
)
129 (require 'compile nil t
)
131 (defvar ispell-check-comments
)
132 (defvar skeleton-further-elements
)
134 (defun ada-mode-version ()
135 "Return Ada mode version."
137 (let ((version-string "4.00"))
139 (message version-string
)
142 (defvar ada-mode-hook nil
143 "*List of functions to call when Ada mode is invoked.
144 This hook is automatically executed after the `ada-mode' is
146 This is a good place to add Ada environment specific bindings.")
149 "Major mode for editing and compiling Ada source in Emacs."
150 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
153 (defcustom ada-auto-case t
154 "*Non-nil means automatically change case of preceding word while typing.
155 Casing is done according to `ada-case-keyword', `ada-case-identifier'
156 and `ada-case-attribute'."
157 :type
'boolean
:group
'ada
)
159 (defcustom ada-broken-decl-indent
0
160 "*Number of columns to indent a broken declaration.
166 :type
'integer
:group
'ada
)
168 (defcustom ada-broken-indent
2
169 "*Number of columns to indent the continuation of a broken line.
172 My_Var : My_Type := (Field1 =>
174 :type
'integer
:group
'ada
)
176 (defcustom ada-continuation-indent ada-broken-indent
177 "*Number of columns to indent the continuation of broken lines in parenthesis.
182 :type
'integer
:group
'ada
)
184 (defcustom ada-case-attribute
'ada-capitalize-word
185 "*Function to call to adjust the case of Ada attributes.
186 It may be `downcase-word', `upcase-word', `ada-loose-case-word',
187 `ada-capitalize-word' or `ada-no-auto-case'."
188 :type
'(choice (const downcase-word
)
190 (const ada-capitalize-word
)
191 (const ada-loose-case-word
)
192 (const ada-no-auto-case
))
195 (defcustom ada-case-exception-file
196 (list (convert-standard-filename' "~/.emacs_case_exceptions"))
197 "*List of special casing exceptions dictionaries for identifiers.
198 The first file is the one where new exceptions will be saved by Emacs
199 when you call `ada-create-case-exception'.
201 These files should contain one word per line, that gives the casing
202 to be used for that word in Ada files. If the line starts with the
203 character *, then the exception will be used for substrings that either
204 start at the beginning of a word or after a _ character, and end either
205 at the end of the word or at a _ character. Each line can be terminated
207 :type
'(repeat (file))
210 (defcustom ada-case-keyword
'downcase-word
211 "*Function to call to adjust the case of an Ada keywords.
212 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
213 `ada-capitalize-word'."
214 :type
'(choice (const downcase-word
)
216 (const ada-capitalize-word
)
217 (const ada-loose-case-word
)
218 (const ada-no-auto-case
))
221 (defcustom ada-case-identifier
'ada-loose-case-word
222 "*Function to call to adjust the case of an Ada identifier.
223 It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
224 `ada-capitalize-word'."
225 :type
'(choice (const downcase-word
)
227 (const ada-capitalize-word
)
228 (const ada-loose-case-word
)
229 (const ada-no-auto-case
))
232 (defcustom ada-clean-buffer-before-saving t
233 "*Non-nil means remove trailing spaces and untabify the buffer before saving."
234 :type
'boolean
:group
'ada
)
236 (defcustom ada-indent
3
237 "*Size of Ada indentation.
243 :type
'integer
:group
'ada
)
245 (defcustom ada-indent-after-return t
246 "*Non-nil means automatically indent after RET or LFD."
247 :type
'boolean
:group
'ada
)
249 (defcustom ada-indent-align-comments t
250 "*Non-nil means align comments on previous line comments, if any.
251 If nil, indentation is calculated as usual.
252 Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
255 A := 1; -- A multi-line comment
256 -- aligned if ada-indent-align-comments is t"
257 :type
'boolean
:group
'ada
)
259 (defcustom ada-indent-comment-as-code t
260 "*Non-nil means indent comment lines as code.
261 A nil value means do not auto-indent comments."
262 :type
'boolean
:group
'ada
)
264 (defcustom ada-indent-handle-comment-special nil
265 "*Non-nil if comment lines should be handled specially inside parenthesis.
266 By default, if the line that contains the open parenthesis has some
267 text following it, then the following lines will be indented in the
268 same column as this text. This will not be true if the first line is
269 a comment and `ada-indent-handle-comment-special' is t.
272 ( Value_1, -- common behavior, when not a comment
276 ( -- `ada-indent-handle-comment-special' is nil
281 ( -- `ada-indent-handle-comment-special' is non-nil
284 :type
'boolean
:group
'ada
)
286 (defcustom ada-indent-is-separate t
287 "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
288 :type
'boolean
:group
'ada
)
290 (defcustom ada-indent-record-rel-type
3
291 "*Indentation for 'record' relative to 'type' or 'use'.
296 :type
'integer
:group
'ada
)
298 (defcustom ada-indent-renames ada-broken-indent
299 "*Indentation for renames relative to the matching function statement.
300 If `ada-indent-return' is null or negative, the indentation is done relative to
301 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
304 function A (B : Integer)
307 :type
'integer
:group
'ada
)
309 (defcustom ada-indent-return
0
310 "*Indentation for 'return' relative to the matching 'function' statement.
311 If `ada-indent-return' is null or negative, the indentation is done relative to
312 the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
315 function A (B : Integer)
317 :type
'integer
:group
'ada
)
319 (defcustom ada-indent-to-open-paren t
320 "*Non-nil means indent according to the innermost open parenthesis."
321 :type
'boolean
:group
'ada
)
323 (defcustom ada-fill-comment-prefix
"-- "
324 "*Text inserted in the first columns when filling a comment paragraph.
325 Note: if you modify this variable, you will have to invoke `ada-mode'
326 again to take account of the new value."
327 :type
'string
:group
'ada
)
329 (defcustom ada-fill-comment-postfix
" --"
330 "*Text inserted at the end of each line when filling a comment paragraph.
331 Used by `ada-fill-comment-paragraph-postfix'."
332 :type
'string
:group
'ada
)
334 (defcustom ada-label-indent -
4
335 "*Number of columns to indent a label.
342 This is also used for <<..>> labels"
343 :type
'integer
:group
'ada
)
345 (defcustom ada-language-version
'ada95
346 "*Ada language version; one of `ada83', `ada95', `ada2005'."
347 :type
'(choice (const ada83
) (const ada95
) (const ada2005
)) :group
'ada
)
349 (defcustom ada-move-to-declaration nil
350 "*Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to 'begin'."
351 :type
'boolean
:group
'ada
)
353 (defcustom ada-popup-key
'[down-mouse-3
]
354 "*Key used for binding the contextual menu.
355 If nil, no contextual menu is available."
356 :type
'(restricted-sexp :match-alternatives
(stringp vectorp
))
359 (defcustom ada-search-directories
361 (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
362 '("/usr/adainclude" "/usr/local/adainclude"
363 "/opt/gnu/adainclude"))
364 "*Default list of directories to search for Ada files.
365 See the description for the `ff-search-directories' variable. This variable
366 is the initial value of `ada-search-directories-internal'."
367 :type
'(repeat (choice :tag
"Directory"
368 (const :tag
"default" nil
)
369 (directory :format
"%v")))
372 (defvar ada-search-directories-internal ada-search-directories
373 "Internal version of `ada-search-directories'.
374 Its value is the concatenation of the search path as read in the project file
375 and the standard runtime location, and the value of the user-defined
376 `ada-search-directories'.")
378 (defcustom ada-stmt-end-indent
0
379 "*Number of columns to indent the end of a statement on a separate line.
384 :type
'integer
:group
'ada
)
386 (defcustom ada-tab-policy
'indent-auto
387 "*Control the behavior of the TAB key.
389 `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
390 `indent-auto' : use indentation functions in this file.
391 `always-tab' : do `indent-relative'."
392 :type
'(choice (const indent-auto
)
393 (const indent-rigidly
)
397 (defcustom ada-use-indent ada-broken-indent
398 "*Indentation for the lines in a 'use' statement.
403 :type
'integer
:group
'ada
)
405 (defcustom ada-when-indent
3
406 "*Indentation for 'when' relative to 'exception' or 'case'.
411 :type
'integer
:group
'ada
)
413 (defcustom ada-with-indent ada-broken-indent
414 "*Indentation for the lines in a 'with' statement.
419 :type
'integer
:group
'ada
)
421 (defcustom ada-which-compiler
'gnat
422 "*Name of the compiler to use.
423 This will determine what features are made available through the Ada mode.
424 The possible choices are:
425 `gnat': Use Ada Core Technologies' GNAT compiler. Add some cross-referencing
427 `generic': Use a generic compiler."
428 :type
'(choice (const gnat
)
433 ;;; ---- end of user configurable variables
436 (defvar ada-body-suffixes
'(".adb")
437 "List of possible suffixes for Ada body files.
438 The extensions should include a `.' if needed.")
440 (defvar ada-spec-suffixes
'(".ads")
441 "List of possible suffixes for Ada spec files.
442 The extensions should include a `.' if needed.")
444 (defvar ada-mode-menu
(make-sparse-keymap "Ada")
445 "Menu for Ada mode.")
447 (defvar ada-mode-map
(make-sparse-keymap)
448 "Local keymap used for Ada mode.")
450 (defvar ada-mode-extra-map
(make-sparse-keymap)
451 "Keymap used for non-standard keybindings.")
453 ;; default is C-c C-q because it's free in ada-mode-map
454 (defvar ada-mode-extra-prefix
"\C-c\C-q"
455 "Prefix key to access `ada-mode-extra-map' functions.")
457 (defvar ada-mode-abbrev-table nil
458 "Local abbrev table for Ada mode.")
460 (defvar ada-mode-syntax-table nil
461 "Syntax table to be used for editing Ada source code.")
463 (defvar ada-mode-symbol-syntax-table nil
464 "Syntax table for Ada, where `_' is a word constituent.")
467 ;; These values are used in eval-when-compile expressions.
468 (defconst ada-83-string-keywords
469 '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
470 "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
471 "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
472 "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
473 "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
474 "procedure" "raise" "range" "record" "rem" "renames" "return"
475 "reverse" "select" "separate" "subtype" "task" "terminate" "then"
476 "type" "use" "when" "while" "with" "xor")
477 "List of Ada 83 keywords.
478 Used to define `ada-*-keywords'.")
480 (defconst ada-95-string-keywords
481 '("abstract" "aliased" "protected" "requeue" "tagged" "until")
482 "List of keywords new in Ada 95.
483 Used to define `ada-*-keywords'.")
485 (defconst ada-2005-string-keywords
486 '("interface" "overriding" "synchronized")
487 "List of keywords new in Ada 2005.
488 Used to define `ada-*-keywords.'"))
490 (defvar ada-ret-binding nil
491 "Variable to save key binding of RET when casing is activated.")
493 (defvar ada-case-exception
'()
494 "Alist of words (entities) that have special casing.")
496 (defvar ada-case-exception-substring
'()
497 "Alist of substrings (entities) that have special casing.
498 The substrings are detected for word constituant when the word
499 is not itself in `ada-case-exception', and only for substrings that
500 either are at the beginning or end of the word, or start after '_'.")
502 (defvar ada-lfd-binding nil
503 "Variable to save key binding of LFD when casing is activated.")
505 (defvar ada-other-file-alist nil
506 "Variable used by `find-file' to find the name of the other package.
507 See `ff-other-file-alist'.")
509 (defvar ada-align-list
510 '(("[^:]\\(\\s-*\\):[^:]" 1 t
)
511 ("[^=]\\(\\s-+\\)=[^=]" 1 t
)
512 ("\\(\\s-*\\)use\\s-" 1)
514 "Ada support for align.el <= 2.2.
515 This variable provides regular expressions on which to align different lines.
516 See `align-mode-alist' for more information.")
518 (defvar ada-align-modes
520 (regexp .
"[^:]\\(\\s-*\\):[^:]")
521 (valid .
(lambda() (not (ada-in-comment-p))))
522 (modes .
'(ada-mode)))
524 (regexp .
"[^=]\\(\\s-+\\)=[^=]")
525 (valid .
(lambda() (not (ada-in-comment-p))))
526 (modes .
'(ada-mode)))
528 (regexp .
"\\(\\s-*\\)--")
529 (modes .
'(ada-mode)))
531 (regexp .
"\\(\\s-*\\)use\\s-")
532 (valid .
(lambda() (not (ada-in-comment-p))))
533 (modes .
'(ada-mode)))
535 "Ada support for align.el >= 2.8.
536 This variable defines several rules to use to align different lines.")
538 (defconst ada-align-region-separate
558 "See the variable `align-region-separate' for more information.")
560 ;;; ---- Below are the regexp used in this package for parsing
562 (defconst ada-83-keywords
564 (concat "\\<" (regexp-opt ada-83-string-keywords t
) "\\>"))
565 "Regular expression matching Ada83 keywords.")
567 (defconst ada-95-keywords
569 (concat "\\<" (regexp-opt
571 ada-95-string-keywords
572 ada-83-string-keywords
) t
) "\\>"))
573 "Regular expression matching Ada95 keywords.")
575 (defconst ada-2005-keywords
577 (concat "\\<" (regexp-opt
579 ada-2005-string-keywords
580 ada-83-string-keywords
581 ada-95-string-keywords
) t
) "\\>"))
582 "Regular expression matching Ada2005 keywords.")
584 (defvar ada-keywords ada-2005-keywords
585 "Regular expression matching Ada keywords.")
586 ;; FIXME: make this customizable
588 (defconst ada-ident-re
590 "Regexp matching Ada (qualified) identifiers.")
592 ;; "with" needs to be included in the regexp, to match generic subprogram parameters
593 ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
594 (defvar ada-procedure-start-regexp
596 "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
598 ;; subprogram name: operator ("[+/=*]")
602 ;; subprogram name: name
604 "\\(\\(\\sw\\|[_.]\\)+\\)"
606 "Regexp matching Ada subprogram start.
607 The actual start is at (match-beginning 4). The name is in (match-string 5).")
609 (defconst ada-name-regexp
610 "\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
611 "Regexp matching a fully qualified name (including attribute).")
613 (defconst ada-package-start-regexp
614 (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp
)
615 "Regexp matching start of package.
616 The package name is in (match-string 4).")
618 (defconst ada-compile-goto-error-file-linenr-re
619 "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
620 "Regexp matching filename:linenr[:column].")
623 ;;; ---- regexps for indentation functions
625 (defvar ada-block-start-re
627 (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
628 "exception" "generic" "loop" "or"
629 "private" "select" ))
630 "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
631 "Regexp for keywords starting Ada blocks.")
633 (defvar ada-end-stmt-re
638 "=>[ \t]*--.*$" "\\|"
639 "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
640 "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
641 "loop" "private" "record" "select"
642 "then abort" "then") t
) "\\>" "\\|"
643 "^[ \t]*" (regexp-opt '("function" "package" "procedure")
644 t
) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
645 "^[ \t]*exception\\>"
647 "Regexp of possible ends for a non-broken statement.
648 A new statement starts after these.")
650 (defvar ada-matching-start-re
654 '("end" "loop" "select" "begin" "case" "do" "declare"
655 "if" "task" "package" "procedure" "function" "record" "protected") t
)
657 "Regexp used in `ada-goto-matching-start'.")
659 (defvar ada-matching-decl-start-re
663 '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t
)
665 "Regexp used in `ada-goto-matching-decl-start'.")
667 (defvar ada-loop-start-re
668 "\\<\\(for\\|while\\|loop\\)\\>"
669 "Regexp for the start of a loop.")
671 (defvar ada-subprog-start-re
673 (concat "\\<" (regexp-opt '("accept" "entry" "function" "package" "procedure"
674 "protected" "task") t
) "\\>"))
675 "Regexp for the start of a subprogram.")
677 (defvar ada-named-block-re
678 "[ \t]*\\(\\sw\\|_\\)+[ \t]*:[^=]"
679 "Regexp of the name of a block or loop.")
681 (defvar ada-contextual-menu-on-identifier nil
682 "Set to true when the right mouse button was clicked on an identifier.")
684 (defvar ada-contextual-menu-last-point nil
685 "Position of point just before displaying the menu.
686 This is a list (point buffer).
687 Since `ada-popup-menu' moves the point where the user clicked, the region
688 is modified. Therefore no command from the menu knows what the user selected
689 before displaying the contextual menu.
690 To get the original region, restore the point to this position before
691 calling `region-end' and `region-beginning'.
692 Modify this variable if you want to restore the point to another position.")
694 (easy-menu-define ada-contextual-menu nil
695 "Menu to use when the user presses the right mouse button.
696 The variable `ada-contextual-menu-on-identifier' will be set to t before
697 displaying the menu if point was on an identifier."
699 ["Goto Declaration/Body" ada-point-and-xref
700 :included ada-contextual-menu-on-identifier
]
701 ["Goto Body" ada-point-and-xref-body
702 :included ada-contextual-menu-on-identifier
]
703 ["Goto Previous Reference" ada-xref-goto-previous-reference
]
704 ["List References" ada-find-references
705 :included ada-contextual-menu-on-identifier
]
706 ["List Local References" ada-find-local-references
707 :included ada-contextual-menu-on-identifier
]
709 ["Other File" ff-find-other-file
]
710 ["Goto Parent Unit" ada-goto-parent
]))
713 ;;------------------------------------------------------------------
714 ;; Support for imenu (see imenu.el)
715 ;;------------------------------------------------------------------
717 (defconst ada-imenu-comment-re
"\\([ \t]*--.*\\)?")
719 (defconst ada-imenu-subprogram-menu-re
720 (concat "^[ \t]*\\(procedure\\|function\\)[ \t\n]+"
721 "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
724 "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
726 (defvar ada-imenu-generic-expression
728 (list nil ada-imenu-subprogram-menu-re
2)
731 "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
733 "\\(" ada-imenu-comment-re
"[ \t\n]+\\|[ \t\n]*([^)]+)"
734 ada-imenu-comment-re
"\\)";; parameter list or simple space
735 "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
737 '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
738 '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
740 "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
741 '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
742 "Imenu generic expression for Ada mode.
743 See `imenu-generic-expression'. This variable will create several submenus for
744 each type of entity that can be found in an Ada file.")
747 ;;------------------------------------------------------------
748 ;; Support for compile.el
749 ;;------------------------------------------------------------
751 (defun ada-compile-mouse-goto-error ()
752 "Mouse interface for `ada-compile-goto-error'."
754 (mouse-set-point last-input-event
)
755 (ada-compile-goto-error (point))
758 (defun ada-compile-goto-error (pos)
759 "Replace `compile-goto-error' from compile.el.
760 If POS is on a file and line location, go to this position. It adds
761 to compile.el the capacity to go to a reference in an error message.
762 For instance, on these lines:
763 foo.adb:61:11: [...] in call to size declared at foo.ads:11
764 foo.adb:61:11: [...] in call to local declared at line 20
765 the 4 file locations can be clicked on and jumped to."
769 (skip-chars-backward "-a-zA-Z0-9_:./\\")
771 ;; special case: looking at a filename:line not at the beginning of a line
772 ;; or a simple line reference "at line ..."
774 (or (looking-at ada-compile-goto-error-file-linenr-re
)
778 (looking-at ada-compile-goto-error-file-linenr-re
))
780 (if (looking-at "\\([0-9]+\\)") (backward-word 1))
781 (looking-at "line \\([0-9]+\\)"))))
783 (let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
784 (file (if (match-beginning 2) (match-string 1)
785 (save-excursion (beginning-of-line)
786 (looking-at ada-compile-goto-error-file-linenr-re
)
788 (error-pos (point-marker))
793 (compilation-find-file (point-marker) (match-string 1) "./")
797 (goto-line (string-to-number line
)))
799 (setq source
(point-marker)))
801 (compilation-goto-locus error-pos source nil
)
805 ;; otherwise, default behavior
807 (compile-goto-error))
812 ;;-------------------------------------------------------------------------
813 ;; Grammar related function
814 ;; The functions below work with the syntax class of the characters in an Ada
815 ;; buffer. Two syntax tables are created, depending on whether we want '_'
816 ;; to be considered as part of a word or not.
817 ;; Some characters may have multiple meanings depending on the context:
818 ;; - ' is either the beginning of a constant character or an attribute
819 ;; - # is either part of a based litteral or a gnatprep statement.
820 ;; - " starts a string, but not if inside a constant character.
821 ;; - ( and ) should be ignored if inside a constant character.
822 ;; Thus their syntax property is changed automatically, and we can still use
823 ;; the standard Emacs functions for sexp (see `ada-in-string-p')
825 ;; On Emacs, this is done through the `syntax-table' text property. The
826 ;; corresponding action is applied automatically each time the buffer
827 ;; changes. If `font-lock-mode' is enabled (the default) the action is
828 ;; set up by `font-lock-syntactic-keywords'. Otherwise, we do it
829 ;; manually in `ada-after-change-function'. The proper method is
830 ;; installed by `ada-handle-syntax-table-properties'.
832 ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
833 ;; slow advice to `parse-partial-sexp' to do the same thing.
834 ;; When executing parse-partial-sexp, we simply modify the strings before and
835 ;; after, so that the special constants '"', '(' and ')' do not interact
836 ;; with parse-partial-sexp.
837 ;; Note: this code is slow and needs to be rewritten as soon as something
838 ;; better is available on XEmacs.
839 ;;-------------------------------------------------------------------------
841 (defun ada-create-syntax-table ()
842 "Create the two syntax tables use in the Ada mode.
843 The standard table declares `_' as a symbol constituent, the second one
844 declares it as a word constituent."
846 (setq ada-mode-syntax-table
(make-syntax-table))
848 ;; define string brackets (`%' is alternative string bracket, but
849 ;; almost never used as such and throws font-lock and indentation
851 (modify-syntax-entry ?%
"$" ada-mode-syntax-table
)
852 (modify-syntax-entry ?
\" "\"" ada-mode-syntax-table
)
854 (modify-syntax-entry ?
: "." ada-mode-syntax-table
)
855 (modify-syntax-entry ?\
; "." ada-mode-syntax-table)
856 (modify-syntax-entry ?
& "." ada-mode-syntax-table
)
857 (modify-syntax-entry ?\|
"." ada-mode-syntax-table
)
858 (modify-syntax-entry ?
+ "." ada-mode-syntax-table
)
859 (modify-syntax-entry ?
* "." ada-mode-syntax-table
)
860 (modify-syntax-entry ?
/ "." ada-mode-syntax-table
)
861 (modify-syntax-entry ?
= "." ada-mode-syntax-table
)
862 (modify-syntax-entry ?
< "." ada-mode-syntax-table
)
863 (modify-syntax-entry ?
> "." ada-mode-syntax-table
)
864 (modify-syntax-entry ?$
"." ada-mode-syntax-table
)
865 (modify-syntax-entry ?\
[ "." ada-mode-syntax-table
)
866 (modify-syntax-entry ?\
] "." ada-mode-syntax-table
)
867 (modify-syntax-entry ?\
{ "." ada-mode-syntax-table
)
868 (modify-syntax-entry ?\
} "." ada-mode-syntax-table
)
869 (modify-syntax-entry ?.
"." ada-mode-syntax-table
)
870 (modify-syntax-entry ?
\\ "." ada-mode-syntax-table
)
871 (modify-syntax-entry ?
\' "." ada-mode-syntax-table
)
873 ;; a single hyphen is punctuation, but a double hyphen starts a comment
874 (modify-syntax-entry ?-
". 12" ada-mode-syntax-table
)
876 ;; See the comment above on grammar related function for the special
878 (if (featurep 'xemacs
)
879 (modify-syntax-entry ?
# "<" ada-mode-syntax-table
)
880 (modify-syntax-entry ?
# "$" ada-mode-syntax-table
))
882 ;; and \f and \n end a comment
883 (modify-syntax-entry ?
\f "> " ada-mode-syntax-table
)
884 (modify-syntax-entry ?
\n "> " ada-mode-syntax-table
)
886 ;; define what belongs in Ada symbols
887 (modify-syntax-entry ?_
"_" ada-mode-syntax-table
)
889 ;; define parentheses to match
890 (modify-syntax-entry ?\
( "()" ada-mode-syntax-table
)
891 (modify-syntax-entry ?\
) ")(" ada-mode-syntax-table
)
893 (setq ada-mode-symbol-syntax-table
(copy-syntax-table ada-mode-syntax-table
))
894 (modify-syntax-entry ?_
"w" ada-mode-symbol-syntax-table
)
897 ;; Support of special characters in XEmacs (see the comments at the beginning
898 ;; of the section on Grammar related functions).
900 (if (featurep 'xemacs
)
901 (defadvice parse-partial-sexp
(around parse-partial-sexp-protect-constants
)
902 "Handles special character constants and gnatprep statements."
906 (setq from to to tmp
)))
909 (while (re-search-forward "'\\([(\")#]\\)'" to t
)
910 (setq change
(cons (list (match-beginning 1)
914 (replace-match "'A'"))
916 (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t
)
917 (setq change
(cons (list (match-beginning 1)
918 (length (match-string 1))
921 (replace-match (make-string (length (match-string 1)) ?
@))))
925 (goto-char (caar change
))
926 (delete-char (cadar change
))
927 (insert (caddar change
))
928 (setq change
(cdr change
)))))))
930 (defun ada-set-syntax-table-properties ()
931 "Assign `syntax-table' properties in accessible part of buffer.
932 In particular, character constants are said to be strings, #...#
933 are treated as numbers instead of gnatprep comments."
934 (let ((modified (buffer-modified-p))
936 (inhibit-read-only t
)
937 (inhibit-point-motion-hooks t
)
938 (inhibit-modification-hooks t
))
939 (remove-text-properties (point-min) (point-max) '(syntax-table nil
))
940 (goto-char (point-min))
941 (while (re-search-forward
942 ;; The following regexp was adapted from
943 ;; `ada-font-lock-syntactic-keywords'.
944 "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
946 (if (match-beginning 1)
948 (match-beginning 1) (match-end 1) 'syntax-table
'(11 . ?
\n))
950 (match-beginning 2) (match-end 2) 'syntax-table
'(7 . ?
'))
952 (match-beginning 3) (match-end 3) 'syntax-table
'(7 . ?
'))))
954 (restore-buffer-modified-p nil
))))
956 (defun ada-after-change-function (beg end old-len
)
957 "Called when the region between BEG and END was changed in the buffer.
958 OLD-LEN indicates what the length of the replaced text was."
961 (let ((from (progn (goto-char beg
) (line-beginning-position)))
962 (to (progn (goto-char end
) (line-end-position))))
963 (narrow-to-region from to
)
965 (ada-set-syntax-table-properties))))))
967 (defun ada-initialize-syntax-table-properties ()
968 "Assign `syntax-table' properties in current buffer."
973 (ada-set-syntax-table-properties))))
974 (add-hook 'after-change-functions
'ada-after-change-function nil t
))
976 (defun ada-handle-syntax-table-properties ()
977 "Handle `syntax-table' properties."
979 ;; `font-lock-mode' will take care of `syntax-table' properties.
980 (remove-hook 'after-change-functions
'ada-after-change-function t
)
981 ;; Take care of `syntax-table' properties manually.
982 (ada-initialize-syntax-table-properties)))
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
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
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
1007 (line-beginning-position) (point))))
1008 (or (ada-in-string-p parse-result
) (ada-in-comment-p parse-result
)))
1011 ;;------------------------------------------------------------------
1013 ;; The Ada mode comes with contextual menus, bound by default to the right
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."
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."
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
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
)
1055 (setq choice
(x-popup-menu position ada-contextual-menu
))
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 ;;------------------------------------------------------------------
1066 ;;------------------------------------------------------------------
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
1073 SPEC and BODY are two regular expressions that must match against
1075 (let* ((reg (concat (regexp-quote body
) "$"))
1076 (tmp (assoc reg ada-other-file-alist
)))
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
)))
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
1097 (if (fboundp 'speedbar-add-supported-extension
)
1099 (funcall (symbol-function 'speedbar-add-supported-extension
)
1101 (funcall (symbol-function 'speedbar-add-supported-extension
)
1108 "Ada mode is the major mode for editing Ada code."
1111 (kill-all-local-variables)
1113 (set-syntax-table ada-mode-syntax-table
)
1115 (set (make-local-variable 'require-final-newline
) mode-require-final-newline
)
1117 ;; Set the paragraph delimiters so that one can select a whole block
1119 (set (make-local-variable 'paragraph-start
) "[ \t\n\f]*$")
1120 (set (make-local-variable 'paragraph-separate
) "[ \t\n\f]*$")
1122 ;; comment end must be set because it may hold a wrong value if
1123 ;; this buffer had been in another mode before. RE
1124 (set (make-local-variable 'comment-end
) "")
1126 ;; used by autofill and indent-new-comment-line
1127 (set (make-local-variable 'comment-start-skip
) "---*[ \t]*")
1129 ;; used by autofill to break a comment line and continue it on another line.
1130 ;; The reason we need this one is that the default behavior does not work
1131 ;; correctly with the definition of paragraph-start above when the comment
1132 ;; is right after a multi-line subprogram declaration (the comments are
1133 ;; aligned under the latest parameter, not under the declaration start).
1134 (set (make-local-variable 'comment-line-break-function
)
1135 (lambda (&optional soft
) (let ((fill-prefix nil
))
1136 (indent-new-comment-line soft
))))
1138 (set (make-local-variable 'indent-line-function
)
1139 'ada-indent-current-function
)
1141 (set (make-local-variable 'comment-column
) 40)
1143 ;; Emacs 20.3 defines a comment-padding to insert spaces between
1144 ;; the comment and the text. We do not want any, this is already
1145 ;; included in comment-start
1146 (unless (featurep 'xemacs
)
1147 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
1148 (set (make-local-variable 'comment-padding
) 0)
1149 (set (make-local-variable 'parse-sexp-lookup-properties
) t
))
1151 (set 'case-fold-search t
)
1152 (if (boundp 'imenu-case-fold-search
)
1153 (set 'imenu-case-fold-search t
))
1155 (set (make-local-variable 'fill-paragraph-function
)
1156 'ada-fill-comment-paragraph
)
1158 ;; Support for compile.el
1159 ;; We just substitute our own functions to go to the error.
1160 (add-hook 'compilation-mode-hook
1162 ;; FIXME: This has global impact! -stef
1163 (define-key compilation-minor-mode-map
[mouse-2
]
1164 'ada-compile-mouse-goto-error
)
1165 (define-key compilation-minor-mode-map
"\C-c\C-c"
1166 'ada-compile-goto-error
)
1167 (define-key compilation-minor-mode-map
"\C-m"
1168 'ada-compile-goto-error
)))
1170 ;; font-lock support :
1172 (set (make-local-variable 'font-lock-defaults
)
1173 '(ada-font-lock-keywords
1175 ((?\_ .
"w") (?
# .
"."))
1177 (font-lock-syntactic-keywords . ada-font-lock-syntactic-keywords
)))
1179 ;; Set up support for find-file.el.
1180 (set (make-local-variable 'ff-other-file-alist
)
1181 'ada-other-file-alist
)
1182 (set (make-local-variable 'ff-search-directories
)
1183 'ada-search-directories-internal
)
1184 (setq ff-post-load-hook
'ada-set-point-accordingly
1185 ff-file-created-hook
'ada-make-body
)
1186 (add-hook 'ff-pre-load-hook
'ada-which-function-are-we-in
)
1188 (make-local-variable 'ff-special-constructs
)
1189 (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair
))
1191 ;; Top level child package declaration; go to the parent package.
1192 (cons (eval-when-compile
1193 (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
1195 "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
1198 ada-search-directories-internal
1199 (ada-make-filename-from-adaname (match-string 3))
1200 ada-spec-suffixes
)))
1202 ;; A "separate" clause.
1203 (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
1206 ada-search-directories-internal
1207 (ada-make-filename-from-adaname (match-string 1))
1208 ada-spec-suffixes
)))
1211 (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
1214 ada-search-directories-internal
1215 (ada-make-filename-from-adaname (match-string 1))
1216 ada-spec-suffixes
)))
1219 ;; Support for outline-minor-mode
1220 (set (make-local-variable 'outline-regexp
)
1221 "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
1222 (set (make-local-variable 'outline-level
) 'ada-outline-level
)
1224 ;; Support for imenu : We want a sorted index
1225 (setq imenu-generic-expression ada-imenu-generic-expression
)
1227 (setq imenu-sort-function
'imenu--sort-by-name
)
1229 ;; Support for ispell : Check only comments
1230 (set (make-local-variable 'ispell-check-comments
) 'exclusive
)
1232 ;; Support for align
1233 (add-to-list 'align-dq-string-modes
'ada-mode
)
1234 (add-to-list 'align-open-comment-modes
'ada-mode
)
1235 (set (make-local-variable 'align-region-separate
) ada-align-region-separate
)
1237 ;; Exclude comments alone on line from alignment.
1238 (add-to-list 'align-exclude-rules-list
1240 (regexp .
"^\\(\\s-*\\)--")
1241 (modes .
'(ada-mode))))
1242 (add-to-list 'align-exclude-rules-list
1244 (regexp .
"^\\(\\s-*\\)\\<use\\>")
1245 (modes .
'(ada-mode))))
1247 (setq ada-align-modes nil
)
1249 (add-to-list 'ada-align-modes
1250 '(ada-declaration-assign
1251 (regexp .
"[^:]\\(\\s-*\\):[^:]")
1252 (valid .
(lambda() (not (ada-in-comment-p))))
1254 (modes .
'(ada-mode))))
1255 (add-to-list 'ada-align-modes
1257 (regexp .
"[^=]\\(\\s-*\\)=>")
1258 (valid .
(lambda() (not (ada-in-comment-p))))
1259 (modes .
'(ada-mode))))
1260 (add-to-list 'ada-align-modes
1262 (regexp .
"\\(\\s-*\\)--")
1263 (modes .
'(ada-mode))))
1264 (add-to-list 'ada-align-modes
1266 (regexp .
"\\(\\s-*\\)\\<use\\s-")
1267 (valid .
(lambda() (not (ada-in-comment-p))))
1268 (modes .
'(ada-mode))))
1269 (add-to-list 'ada-align-modes
1271 (regexp .
"\\(\\s-+\\)at\\>")
1272 (modes .
'(ada-mode))))
1274 (setq align-mode-rules-list ada-align-modes
)
1276 ;; Set up the contextual menu
1278 (define-key ada-mode-map ada-popup-key
'ada-popup-menu
))
1280 ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
1281 (define-abbrev-table 'ada-mode-abbrev-table
())
1282 (setq local-abbrev-table ada-mode-abbrev-table
)
1284 ;; Support for which-function mode
1285 (make-local-variable 'which-func-functions
)
1286 (setq which-func-functions
'(ada-which-function))
1288 ;; Support for indent-new-comment-line (Especially for XEmacs)
1289 (set (make-local-variable 'comment-multi-line
) nil
)
1291 ;; Support for add-log
1292 (set (make-local-variable 'add-log-current-defun-function
) 'ada-which-function
)
1294 (setq major-mode
'ada-mode
1297 (use-local-map ada-mode-map
)
1299 (easy-menu-add ada-mode-menu ada-mode-map
)
1301 (set-syntax-table ada-mode-syntax-table
)
1303 (if ada-clean-buffer-before-saving
1305 ;; remove all spaces at the end of lines in the whole buffer.
1306 (add-hook 'local-write-file-hooks
'delete-trailing-whitespace
)
1307 ;; convert all tabs to the correct number of spaces.
1308 (add-hook 'local-write-file-hooks
1309 (lambda () (untabify (point-min) (point-max))))))
1311 (set (make-local-variable 'skeleton-further-elements
)
1312 '((< '(backward-delete-char-untabify
1313 (min ada-indent
(current-column))))))
1314 (add-hook 'skeleton-end-hook
'ada-adjust-case-skeleton nil t
)
1316 (run-mode-hooks 'ada-mode-hook
)
1318 ;; To be run after the hook, in case the user modified
1319 ;; ada-fill-comment-prefix
1320 (make-local-variable 'comment-start
)
1321 (if ada-fill-comment-prefix
1322 (set 'comment-start ada-fill-comment-prefix
)
1323 (set 'comment-start
"-- "))
1325 ;; Run this after the hook to give the users a chance to activate
1328 (unless (featurep 'xemacs
)
1329 (ada-initialize-syntax-table-properties)
1330 (add-hook 'font-lock-mode-hook
'ada-handle-syntax-table-properties nil t
))
1332 ;; the following has to be done after running the ada-mode-hook
1333 ;; because users might want to set the values of these variable
1336 (cond ((eq ada-language-version
'ada83
)
1337 (setq ada-keywords ada-83-keywords
))
1338 ((eq ada-language-version
'ada95
)
1339 (setq ada-keywords ada-95-keywords
))
1340 ((eq ada-language-version
'ada2005
)
1341 (setq ada-keywords ada-2005-keywords
)))
1344 (ada-activate-keys-for-case)))
1346 (defun ada-adjust-case-skeleton ()
1347 "Adjust the case of the text inserted by a skeleton."
1349 (let ((aa-end (point)))
1350 (ada-adjust-case-region
1351 (progn (goto-char (symbol-value 'beg
)) (forward-word -
1) (point))
1352 (goto-char aa-end
)))))
1354 (defun ada-region-selected ()
1355 "Should we operate on an active region?"
1356 (if (fboundp 'use-region-p
)
1360 ;;-----------------------------------------------------------------
1362 ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
1363 ;; functions to auto-case identifiers, keywords, ...
1364 ;; The basic rules for autocasing are defined through the variables
1365 ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
1366 ;; are references to the functions that will do the actual casing.
1368 ;; However, in most cases, the user will want to define some exceptions to
1369 ;; these casing rules. This is done through a list of files, that contain
1370 ;; one word per line. These files are stored in `ada-case-exception-file'.
1371 ;; For backward compatibility, this variable can also be a string.
1372 ;;-----------------------------------------------------------------
1374 (defun ada-save-exceptions-to-file (file-name)
1375 "Save the casing exception lists to the file FILE-NAME.
1376 Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
1377 (find-file (expand-file-name file-name
))
1379 (mapc (lambda (x) (insert (car x
) "\n"))
1380 (sort (copy-sequence ada-case-exception
)
1381 (lambda(a b
) (string< (car a
) (car b
)))))
1382 (mapc (lambda (x) (insert "*" (car x
) "\n"))
1383 (sort (copy-sequence ada-case-exception-substring
)
1384 (lambda(a b
) (string< (car a
) (car b
)))))
1389 (defun ada-create-case-exception (&optional word
)
1390 "Define WORD as an exception for the casing system.
1391 If WORD is not given, then the current word in the buffer is used instead.
1392 The new word is added to the first file in `ada-case-exception-file'.
1393 The standard casing rules will no longer apply to this word."
1395 (let ((previous-syntax-table (syntax-table))
1399 (cond ((stringp ada-case-exception-file
)
1400 (setq file-name ada-case-exception-file
))
1401 ((listp ada-case-exception-file
)
1402 (setq file-name
(car ada-case-exception-file
)))
1404 (error (concat "No exception file specified. "
1405 "See variable ada-case-exception-file"))))
1407 (set-syntax-table ada-mode-symbol-syntax-table
)
1410 (skip-syntax-backward "w")
1411 (setq word
(buffer-substring-no-properties
1412 (point) (save-excursion (forward-word 1) (point))))))
1413 (set-syntax-table previous-syntax-table
)
1415 ;; Reread the exceptions file, in case it was modified by some other,
1416 (ada-case-read-exceptions-from-file file-name
)
1418 ;; If the word is already in the list, even with a different casing
1419 ;; we simply want to replace it.
1420 (if (and (not (equal ada-case-exception
'()))
1421 (assoc-string word ada-case-exception t
))
1422 (setcar (assoc-string word ada-case-exception t
) word
)
1423 (add-to-list 'ada-case-exception
(cons word t
))
1426 (ada-save-exceptions-to-file file-name
)
1429 (defun ada-create-case-exception-substring (&optional word
)
1430 "Define the substring WORD as an exception for the casing system.
1431 If WORD is not given, then the current word in the buffer is used instead,
1432 or the selected region if any is active.
1433 The new word is added to the first file in `ada-case-exception-file'.
1434 When auto-casing a word, this substring will be special-cased, unless the
1435 word itself has a special casing."
1438 (cond ((stringp ada-case-exception-file
)
1439 ada-case-exception-file
)
1440 ((listp ada-case-exception-file
)
1441 (car ada-case-exception-file
))
1443 (error (concat "No exception file specified. "
1444 "See variable ada-case-exception-file"))))))
1446 ;; Find the substring to define as an exception. Order is: the parameter,
1447 ;; if any, or the selected region, or the word under the cursor
1451 ((ada-region-selected)
1452 (setq word
(buffer-substring-no-properties
1453 (region-beginning) (region-end))))
1456 (let ((underscore-syntax (char-syntax ?_
)))
1459 (modify-syntax-entry ?_
"." (syntax-table))
1461 (skip-syntax-backward "w")
1462 (set 'word
(buffer-substring-no-properties
1464 (save-excursion (forward-word 1) (point))))))
1465 (modify-syntax-entry ?_
(make-string 1 underscore-syntax
)
1468 ;; Reread the exceptions file, in case it was modified by some other,
1469 (ada-case-read-exceptions-from-file file-name
)
1471 ;; If the word is already in the list, even with a different casing
1472 ;; we simply want to replace it.
1473 (if (and (not (equal ada-case-exception-substring
'()))
1474 (assoc-string word ada-case-exception-substring t
))
1475 (setcar (assoc-string word ada-case-exception-substring t
) word
)
1476 (add-to-list 'ada-case-exception-substring
(cons word t
))
1479 (ada-save-exceptions-to-file file-name
)
1481 (message "%s" (concat "Defining " word
" as a casing exception"))))
1483 (defun ada-case-read-exceptions-from-file (file-name)
1484 "Read the content of the casing exception file FILE-NAME."
1485 (if (file-readable-p (expand-file-name file-name
))
1486 (let ((buffer (current-buffer)))
1487 (find-file (expand-file-name file-name
))
1488 (set-syntax-table ada-mode-symbol-syntax-table
)
1490 (goto-char (point-min))
1493 ;; If the item is already in the list, even with an other casing,
1494 ;; do not add it again. This way, the user can easily decide which
1495 ;; priority should be applied to each casing exception
1496 (let ((word (buffer-substring-no-properties
1497 (point) (save-excursion (forward-word 1) (point)))))
1499 ;; Handling a substring ?
1500 (if (char-equal (string-to-char word
) ?
*)
1502 (setq word
(substring word
1))
1503 (unless (assoc-string word ada-case-exception-substring t
)
1504 (add-to-list 'ada-case-exception-substring
(cons word t
))))
1505 (unless (assoc-string word ada-case-exception t
)
1506 (add-to-list 'ada-case-exception
(cons word t
)))))
1510 (set-buffer buffer
)))
1513 (defun ada-case-read-exceptions ()
1514 "Read all the casing exception files from `ada-case-exception-file'."
1517 ;; Reinitialize the casing exception list
1518 (setq ada-case-exception
'()
1519 ada-case-exception-substring
'())
1521 (cond ((stringp ada-case-exception-file
)
1522 (ada-case-read-exceptions-from-file ada-case-exception-file
))
1524 ((listp ada-case-exception-file
)
1525 (mapcar 'ada-case-read-exceptions-from-file
1526 ada-case-exception-file
))))
1528 (defun ada-adjust-case-substring ()
1529 "Adjust case of substrings in the previous word."
1531 (let ((substrings ada-case-exception-substring
)
1533 (case-fold-search t
)
1534 (underscore-syntax (char-syntax ?_
))
1542 (modify-syntax-entry ?_
"." (syntax-table))
1545 (setq re
(concat "\\b" (regexp-quote (caar substrings
)) "\\b"))
1548 (while (re-search-forward re max t
)
1549 (replace-match (caar substrings
) t
)))
1550 (setq substrings
(cdr substrings
))
1553 (modify-syntax-entry ?_
(make-string 1 underscore-syntax
) (syntax-table)))
1556 (defun ada-adjust-case-identifier ()
1557 "Adjust case of the previous identifier.
1558 The auto-casing is done according to the value of `ada-case-identifier'
1559 and the exceptions defined in `ada-case-exception-file'."
1561 (if (or (equal ada-case-exception
'())
1562 (equal (char-after) ?_
))
1564 (funcall ada-case-identifier -
1)
1565 (ada-adjust-case-substring))
1569 (start (save-excursion (skip-syntax-backward "w")
1572 ;; If we have an exception, replace the word by the correct casing
1573 (if (setq match
(assoc-string (buffer-substring start end
)
1574 ada-case-exception t
))
1577 (delete-region start end
)
1578 (insert (car match
)))
1580 ;; Else simply re-case the word
1581 (funcall ada-case-identifier -
1)
1582 (ada-adjust-case-substring))))))
1584 (defun ada-after-keyword-p ()
1585 "Return t if cursor is after a keyword that is not an attribute."
1588 (and (not (and (char-before)
1589 (or (= (char-before) ?_
)
1590 (= (char-before) ?
'))));; unless we have a _ or '
1591 (looking-at (concat ada-keywords
"[^_]")))))
1593 (defun ada-adjust-case (&optional force-identifier
)
1594 "Adjust the case of the word before the character just typed.
1595 If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
1599 (if (and (not (bobp))
1600 ;; or if at the end of a character constant
1601 (not (and (eq (following-char) ?
')
1602 (eq (char-before (1- (point))) ?
')))
1603 ;; or if the previous character was not part of a word
1604 (eq (char-syntax (char-before)) ?w
)
1605 ;; if in a string or a comment
1606 (not (ada-in-string-or-comment-p))
1610 (or (= (point) (point-min))
1612 (= (following-char) ?
'))
1613 (funcall ada-case-attribute -
1)
1615 (not force-identifier
) ; (MH)
1616 (ada-after-keyword-p))
1617 (funcall ada-case-keyword -
1)
1618 (ada-adjust-case-identifier))))
1623 (defun ada-adjust-case-interactive (arg)
1624 "Adjust the case of the previous word, and process the character just typed.
1625 ARG is the prefix the user entered with \\[universal-argument]."
1629 (let ((lastk last-command-event
)
1630 (previous-syntax-table (syntax-table)))
1634 (set-syntax-table ada-mode-symbol-syntax-table
)
1635 (cond ((or (eq lastk ?
\n)
1640 ;; horrible dekludge
1641 (delete-backward-char 1)
1642 ;; some special keys and their bindings
1645 (funcall ada-lfd-binding
))
1647 (funcall ada-ret-binding
))))
1648 ((eq lastk ?\C-i
) (ada-tab))
1649 ;; Else just insert the character
1650 ((self-insert-command (prefix-numeric-value arg
))))
1651 ;; if there is a keyword in front of the underscore
1652 ;; then it should be part of an identifier (MH)
1657 ;; Restore the syntax table
1658 (set-syntax-table previous-syntax-table
))
1661 ;; Else, no auto-casing
1663 ((eq last-command-event ?
\n)
1664 (funcall ada-lfd-binding
))
1665 ((eq last-command-event ?
\r)
1666 (funcall ada-ret-binding
))
1668 (self-insert-command (prefix-numeric-value arg
))))
1671 (defun ada-activate-keys-for-case ()
1672 "Modify the key bindings for all the keys that should readjust the casing."
1674 ;; Save original key-bindings to allow swapping ret/lfd
1675 ;; when casing is activated.
1676 ;; The 'or ...' is there to be sure that the value will not
1677 ;; be changed again when Ada mode is called more than once
1678 (or ada-ret-binding
(setq ada-ret-binding
(key-binding "\C-M")))
1679 (or ada-lfd-binding
(setq ada-lfd-binding
(key-binding "\C-j")))
1681 ;; Call case modifying function after certain keys.
1682 (mapcar (function (lambda(key) (define-key
1684 (char-to-string key
)
1685 'ada-adjust-case-interactive
)))
1686 '( ?
` ?_ ?
# ?% ?
& ?
* ?
( ?
) ?- ?
= ?
+
1687 ?| ?\
; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
1689 (defun ada-loose-case-word (&optional arg
)
1690 "Upcase first letter and letters following `_' in the following word.
1691 No other letter is modified.
1692 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1695 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1697 (skip-syntax-backward "w")
1698 (while (and (or first
(search-forward "_" end t
))
1702 (insert-char (upcase (following-char)) 1)
1705 (defun ada-no-auto-case (&optional arg
)
1706 "Do nothing. ARG is ignored.
1707 This function can be used for the auto-casing variables in Ada mode, to
1708 adapt to unusal auto-casing schemes. Since it does nothing, you can for
1709 instance use it for `ada-case-identifier' if you don't want any special
1710 auto-casing for identifiers, whereas keywords have to be lower-cased.
1711 See also `ada-auto-case' to disable auto casing altogether."
1714 (defun ada-capitalize-word (&optional arg
)
1715 "Upcase first letter and letters following '_', lower case other letters.
1716 ARG is ignored, and is there for compatibility with `capitalize-word' only."
1718 (let ((end (save-excursion (skip-syntax-forward "w") (point)))
1719 (begin (save-excursion (skip-syntax-backward "w") (point))))
1720 (modify-syntax-entry ?_
"_")
1721 (capitalize-region begin end
)
1722 (modify-syntax-entry ?_
"w")))
1724 (defun ada-adjust-case-region (from to
)
1725 "Adjust the case of all words in the region between FROM and TO.
1726 Attention: This function might take very long for big regions!"
1732 (previous-syntax-table (syntax-table)))
1733 (message "Adjusting case ...")
1736 (set-syntax-table ada-mode-symbol-syntax-table
)
1739 ;; loop: look for all identifiers, keywords, and attributes
1741 (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t
)
1742 (setq end
(match-end 1))
1744 (and (> (point) from
)
1747 (setq attribp
(looking-at "'.[^']")))))
1749 ;; do nothing if it is a string or comment
1750 (ada-in-string-or-comment-p)
1753 ;; get the identifier or keyword or attribute
1755 (setq begin
(point))
1756 (setq keywordp
(looking-at ada-keywords
))
1759 ;; casing according to user-option
1762 (funcall ada-case-attribute -
1)
1764 (funcall ada-case-keyword -
1)
1765 (ada-adjust-case-identifier)))
1766 (goto-char begin
))))
1767 (message "Adjusting case ... Done"))
1768 (set-syntax-table previous-syntax-table
))))
1770 (defun ada-adjust-case-buffer ()
1771 "Adjust the case of all words in the whole buffer.
1772 ATTENTION: This function might take very long for big buffers!"
1774 (ada-adjust-case-region (point-min) (point-max)))
1777 ;;--------------------------------------------------------------
1778 ;; Format Parameter Lists
1779 ;; Some special algorithms are provided to indent the parameter lists in
1780 ;; subprogram declarations. This is done in two steps:
1781 ;; - First parses the parameter list. The returned list has the following
1783 ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
1785 ;; This is done in `ada-scan-paramlist'.
1786 ;; - Delete and recreate the parameter list in function
1787 ;; `ada-insert-paramlist'.
1788 ;; Both steps are called from `ada-format-paramlist'.
1789 ;; Note: Comments inside the parameter list are lost.
1790 ;; The syntax has to be correct, or the reformating will fail.
1791 ;;--------------------------------------------------------------
1793 (defun ada-format-paramlist ()
1794 "Reformat the parameter list point is in."
1800 (previous-syntax-table (syntax-table)))
1803 (set-syntax-table ada-mode-symbol-syntax-table
)
1805 ;; check if really inside parameter list
1806 (or (ada-in-paramlist-p)
1807 (error "Not in parameter list"))
1809 ;; find start of current parameter-list
1810 (ada-search-ignore-string-comment
1811 (concat ada-subprog-start-re
"\\|\\<body\\>" ) t nil
)
1814 (setq begin
(point))
1816 ;; find end of parameter-list
1818 (setq delend
(point))
1822 ;; find end of last parameter-declaration
1823 (forward-comment -
1000)
1826 ;; build a list of all elements of the parameter-list
1827 (setq paramlist
(ada-scan-paramlist (1+ begin
) end
))
1829 ;; delete the original parameter-list
1830 (delete-region begin delend
)
1832 ;; insert the new parameter-list
1834 (ada-insert-paramlist paramlist
))
1836 ;; restore syntax-table
1837 (set-syntax-table previous-syntax-table
)
1840 (defun ada-scan-paramlist (begin end
)
1841 "Scan the parameter list found in between BEGIN and END.
1842 Return the equivalent internal parameter list."
1843 (let ((paramlist (list))
1853 ;; loop until end of last parameter
1856 ;; find first character of parameter-declaration
1857 (ada-goto-next-non-ws)
1860 ;; find last character of parameter-declaration
1861 (if (setq match-cons
1862 (ada-search-ignore-string-comment "[ \t\n]*;" nil end t
))
1864 (setq epos
(car match-cons
))
1865 (setq semipos
(cdr match-cons
)))
1868 ;; read name(s) of parameter(s)
1870 (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
1872 (setq param
(list (match-string 1)))
1873 (ada-search-ignore-string-comment ":" nil epos t
'search-forward
)
1881 (ada-search-ignore-string-comment
1882 "in" nil epos t
'word-search-forward
)))))
1890 (ada-search-ignore-string-comment
1891 "out" nil epos t
'word-search-forward
)))))
1893 ;; look for 'access'
1899 (ada-search-ignore-string-comment
1900 "access" nil epos t
'word-search-forward
)))))
1902 ;; skip 'in'/'out'/'access'
1904 (ada-goto-next-non-ws)
1905 (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
1907 (ada-goto-next-non-ws))
1909 ;; read type of parameter
1910 ;; We accept spaces in the name, since some software like Rose
1911 ;; generates something like: "A : B 'Class"
1912 (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
1915 (list (match-string 0))))
1917 ;; read default-expression, if there is one
1918 (goto-char (setq apos
(match-end 0)))
1922 (if (setq match-cons
1923 (ada-search-ignore-string-comment
1924 ":=" nil epos t
'search-forward
))
1925 (buffer-substring (car match-cons
) epos
)
1928 ;; add this parameter-declaration to the list
1929 (setq paramlist
(append paramlist
(list param
)))
1931 ;; check if it was the last parameter
1934 (goto-char semipos
))
1936 (reverse paramlist
)))
1938 (defun ada-insert-paramlist (paramlist)
1939 "Insert a formatted PARAMLIST in the buffer."
1940 (let ((i (length paramlist
))
1949 ;; loop until last parameter
1950 (while (not (zerop i
))
1953 ;; get max length of parameter-name
1954 (setq parlen
(max parlen
(length (nth 0 (nth i paramlist
)))))
1956 ;; get max length of type-name
1957 (setq typlen
(max typlen
(length (nth 4 (nth i paramlist
)))))
1959 ;; is there any 'in' ?
1960 (setq inp
(or inp
(nth 1 (nth i paramlist
))))
1962 ;; is there any 'out' ?
1963 (setq outp
(or outp
(nth 2 (nth i paramlist
))))
1965 ;; is there any 'access' ?
1966 (setq accessp
(or accessp
(nth 3 (nth i paramlist
))))
1969 ;; does paramlist already start on a separate line ?
1971 (re-search-backward "^.\\|[^ \t]" nil t
)
1973 ;; yes => re-indent it
1975 (ada-indent-current)
1977 (if (looking-at "\\(is\\|return\\)")
1978 (replace-match " \\1"))))
1980 ;; no => insert it where we are after removing any whitespace
1984 ((looking-at "[ \t]*\\(\n\\|;\\)")
1985 (replace-match "\\1"))
1986 ((looking-at "[ \t]*\\(is\\|return\\)")
1987 (replace-match " \\1"))))
1991 (ada-indent-current)
1993 (setq firstcol
(current-column))
1994 (setq i
(length paramlist
))
1996 ;; loop until last parameter
1997 (while (not (zerop i
))
1999 (setq column firstcol
)
2001 ;; insert parameter-name, space and colon
2002 (insert (nth 0 (nth i paramlist
)))
2003 (indent-to (+ column parlen
1))
2005 (setq column
(current-column))
2007 ;; insert 'in' or space
2008 (if (nth 1 (nth i paramlist
))
2013 (not (nth 3 (nth i paramlist
))))
2016 ;; insert 'out' or space
2017 (if (nth 2 (nth i paramlist
))
2022 (not (nth 3 (nth i paramlist
))))
2026 (if (nth 3 (nth i paramlist
))
2029 (setq column
(current-column))
2031 ;; insert type-name and, if necessary, space and default-expression
2032 (insert (nth 4 (nth i paramlist
)))
2033 (if (nth 5 (nth i paramlist
))
2035 (indent-to (+ column typlen
1))
2036 (insert (nth 5 (nth i paramlist
)))))
2038 ;; check if it was the last parameter
2041 ;; no => insert ';' and newline and indent
2044 (indent-to firstcol
))
2047 ;; if anything follows, except semicolon, newline, is or return
2048 ;; put it in a new line and indent it
2049 (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
2050 (ada-indent-newline-indent))
2055 ;;;----------------------------------------------------------------
2056 ;; Indentation Engine
2057 ;; All indentations are indicated as a two-element string:
2058 ;; - position of reference in the buffer
2059 ;; - offset to indent from this position (can also be a symbol or a list
2060 ;; that are evaluated)
2061 ;; Thus the total indentation for a line is the column number of the reference
2062 ;; position plus whatever value the evaluation of the second element provides.
2063 ;; This mechanism is used so that the Ada mode can "explain" how the
2064 ;; indentation was calculated, by showing which variables were used.
2066 ;; The indentation itself is done in only one pass: first we try to guess in
2067 ;; what context we are by looking at the following keyword or punctuation
2068 ;; sign. If nothing remarkable is found, just try to guess the indentation
2069 ;; based on previous lines.
2071 ;; The relevant functions for indentation are:
2072 ;; - `ada-indent-region': Re-indent a region of text
2073 ;; - `ada-justified-indent-current': Re-indent the current line and shows the
2074 ;; calculation that were done
2075 ;; - `ada-indent-current': Re-indent the current line
2076 ;; - `ada-get-current-indent': Calculate the indentation for the current line,
2077 ;; based on the context (see above).
2078 ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
2079 ;; For efficiency, these functions do not check they are in the correct
2081 ;;;----------------------------------------------------------------
2083 (defun ada-indent-region (beg end
)
2084 "Indent the region between BEG end END."
2087 (let ((block-done 0)
2088 (lines-remaining (count-lines beg end
))
2089 (msg (format "%%4d out of %4d lines remaining ..."
2090 (count-lines beg end
)))
2091 (endmark (copy-marker end
)))
2092 ;; catch errors while indenting
2093 (while (< (point) endmark
)
2094 (if (> block-done
39)
2096 (setq lines-remaining
(- lines-remaining block-done
)
2098 (message msg lines-remaining
)))
2099 (if (= (char-after) ?
\n) nil
2100 (ada-indent-current))
2102 (setq block-done
(1+ block-done
)))
2103 (message "Indenting ... done")))
2105 (defun ada-indent-newline-indent ()
2106 "Indent the current line, insert a newline and then indent the new line."
2108 (ada-indent-current)
2110 (ada-indent-current))
2112 (defun ada-indent-newline-indent-conditional ()
2113 "Insert a newline and indent it.
2114 The original line is indented first if `ada-indent-after-return' is non-nil."
2116 (if ada-indent-after-return
(ada-indent-current))
2118 (ada-indent-current))
2120 (defun ada-justified-indent-current ()
2121 "Indent the current line and explain how the calculation was done."
2124 (let ((cur-indent (ada-indent-current)))
2126 (let ((line (save-excursion
2127 (goto-char (car cur-indent
))
2128 (count-lines 1 (point)))))
2130 (if (equal (cdr cur-indent
) '(0))
2131 (message (concat "same indentation as line " (number-to-string line
)))
2132 (message "%s" (mapconcat (lambda(x)
2137 (number-to-string x
))
2139 (concat "- " (symbol-name (cadr x
))))
2144 (goto-char (car cur-indent
))
2147 (defun ada-batch-reformat ()
2148 "Re-indent and re-case all the files found on the command line.
2149 This function should be used from the command line, with a
2151 emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
2153 (while command-line-args-left
2154 (let ((source (car command-line-args-left
)))
2155 (message "Formating %s" source
)
2157 (ada-indent-region (point-min) (point-max))
2158 (ada-adjust-case-buffer)
2159 (write-file source
))
2160 (setq command-line-args-left
(cdr command-line-args-left
)))
2164 (defsubst ada-goto-previous-word
()
2165 "Move point to the beginning of the previous word of Ada code.
2166 Return the new position of point or nil if not found."
2167 (ada-goto-next-word t
))
2169 (defun ada-indent-current ()
2170 "Indent current line as Ada code.
2171 Return the calculation that was done, including the reference point
2174 (let ((previous-syntax-table (syntax-table))
2175 (orgpoint (point-marker))
2176 cur-indent tmp-indent
2181 (set-syntax-table ada-mode-symbol-syntax-table
)
2183 ;; This need to be done here so that the advice is not always
2184 ;; activated (this might interact badly with other modes)
2185 (if (featurep 'xemacs
)
2186 (ad-activate 'parse-partial-sexp t
))
2191 ;; Not First line in the buffer ?
2192 (if (save-excursion (zerop (forward-line -
1)))
2194 (back-to-indentation)
2195 (ada-get-current-indent))
2197 ;; first line in the buffer
2198 (list (point-min) 0))))
2200 ;; Evaluate the list to get the column to indent to
2201 ;; prev-indent contains the column to indent to
2203 (setq prev-indent
(save-excursion (goto-char (car cur-indent
))
2205 tmp-indent
(cdr cur-indent
))
2206 (setq prev-indent
0 tmp-indent
'()))
2208 (while (not (null tmp-indent
))
2210 ((numberp (car tmp-indent
))
2211 (setq prev-indent
(+ prev-indent
(car tmp-indent
))))
2213 (setq prev-indent
(+ prev-indent
(eval (car tmp-indent
)))))
2215 (setq tmp-indent
(cdr tmp-indent
)))
2217 ;; only re-indent if indentation is different then the current
2218 (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent
)
2221 (delete-horizontal-space)
2222 (indent-to prev-indent
))
2224 ;; restore position of point
2226 (goto-char orgpoint
)
2227 (if (< (current-column) (current-indentation))
2228 (back-to-indentation)))
2230 ;; restore syntax-table
2231 (set-syntax-table previous-syntax-table
)
2232 (if (featurep 'xemacs
)
2233 (ad-deactivate 'parse-partial-sexp
))
2239 (defun ada-get-current-indent ()
2240 "Return the indentation to use for the current line."
2245 (orgpoint (save-excursion
2247 (forward-comment -
10000)
2254 ;;-----------------------------
2255 ;; in open parenthesis, but not in parameter-list
2256 ;;-----------------------------
2258 ((and ada-indent-to-open-paren
2259 (not (ada-in-paramlist-p))
2260 (setq column
(ada-in-open-paren-p)))
2262 ;; check if we have something like this (Table_Component_Type =>
2263 ;; Source_File_Record)
2266 ;; Align the closing parenthesis on the opening one
2267 (if (= (following-char) ?\
))
2270 (skip-chars-backward " \t")
2271 (list (1- (point)) 0))
2273 (if (and (skip-chars-backward " \t")
2274 (= (char-before) ?
\n)
2275 (not (forward-comment -
10000))
2276 (= (char-before) ?
>))
2277 ;; ??? Could use a different variable
2278 (list column
'ada-broken-indent
)
2280 ;; We want all continuation lines to be indented the same
2281 ;; (ada-broken-line from the opening parenthesis. However, in
2282 ;; parameter list, each new parameter should be indented at the
2283 ;; column as the opening parenthesis.
2285 ;; A special case to handle nested boolean expressions, as in
2287 ;; and then C) -- indented by ada-broken-indent
2288 ;; or else D) -- indenting this line.
2289 ;; ??? This is really a hack, we should have a proper way to go to
2290 ;; ??? the beginning of the statement
2292 (if (= (char-before) ?\
))
2295 (if (memq (char-before) '(?
, ?\
; ?\( ?\)))
2297 (list column
'ada-continuation-indent
)
2300 ;;---------------------------
2302 ;;---------------------------
2305 (ada-indent-on-previous-lines nil orgpoint orgpoint
))
2307 ;;---------------------------
2309 ;;---------------------------
2311 ((= (downcase (char-after)) ?e
)
2314 ;; ------- end ------
2316 ((looking-at "end\\>")
2320 (ada-goto-matching-start 1)
2323 ;; found 'loop' => skip back to 'while' or 'for'
2324 ;; if 'loop' is not on a separate line
2325 ;; Stop the search for 'while' and 'for' when a ';' is encountered.
2329 (looking-at ".+\\<loop\\>"))
2332 (setq limit
(car (ada-search-ignore-string-comment ";" t
))))
2336 (ada-search-ignore-string-comment ada-loop-start-re t limit
))
2337 (not (looking-at "\\<loop\\>"))))
2339 (goto-char (car match-cons
))
2342 (if (looking-at ada-named-block-re
)
2343 (setq label
(- ada-label-indent
))))))))
2345 ;; found 'record' =>
2346 ;; if the keyword is found at the beginning of a line (or just
2347 ;; after limited, we indent on it, otherwise we indent on the
2348 ;; beginning of the type declaration)
2349 ;; type A is (B : Integer;
2350 ;; C : Integer) is record
2351 ;; end record; -- This is badly indented otherwise
2352 (if (looking-at "record")
2355 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2356 (list (save-excursion (back-to-indentation) (point)) 0)
2357 (list (save-excursion
2358 (car (ada-search-ignore-string-comment "\\<type\\>" t
)))
2361 ;; Else keep the same indentation as the beginning statement
2362 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0)))))
2364 ;; ------ exception ----
2366 ((looking-at "exception\\>")
2368 (ada-goto-matching-start 1)
2369 (list (save-excursion (back-to-indentation) (point)) 0)))
2373 ((looking-at "else\\>")
2374 (if (save-excursion (ada-goto-previous-word)
2375 (looking-at "\\<or\\>"))
2376 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2378 (ada-goto-matching-start 1 nil t
)
2379 (list (progn (back-to-indentation) (point)) 0))))
2383 ((looking-at "elsif\\>")
2385 (ada-goto-matching-start 1 nil t
)
2386 (list (progn (back-to-indentation) (point)) 0)))
2390 ;;---------------------------
2391 ;; starting with w (when)
2392 ;;---------------------------
2394 ((and (= (downcase (char-after)) ?w
)
2395 (looking-at "when\\>"))
2397 (ada-goto-matching-start 1)
2398 (list (save-excursion (back-to-indentation) (point))
2401 ;;---------------------------
2402 ;; starting with t (then)
2403 ;;---------------------------
2405 ((and (= (downcase (char-after)) ?t
)
2406 (looking-at "then\\>"))
2407 (if (save-excursion (ada-goto-previous-word)
2408 (looking-at "and\\>"))
2409 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2411 ;; Select has been added for the statement: "select ... then abort"
2412 (ada-search-ignore-string-comment
2413 "\\<\\(elsif\\|if\\|select\\)\\>" t nil
)
2414 (list (progn (back-to-indentation) (point))
2415 'ada-stmt-end-indent
))))
2417 ;;---------------------------
2418 ;; starting with l (loop)
2419 ;;---------------------------
2421 ((and (= (downcase (char-after)) ?l
)
2422 (looking-at "loop\\>"))
2425 (goto-char (match-end 0))
2426 (ada-goto-stmt-start)
2427 (if (looking-at "\\<\\(loop\\|if\\)\\>")
2428 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2429 (unless (looking-at ada-loop-start-re
)
2430 (ada-search-ignore-string-comment ada-loop-start-re
2432 (if (looking-at "\\<loop\\>")
2433 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2434 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent
)))))
2436 ;;----------------------------
2437 ;; starting with l (limited) or r (record)
2438 ;;----------------------------
2440 ((or (and (= (downcase (char-after)) ?l
)
2441 (looking-at "limited\\>"))
2442 (and (= (downcase (char-after)) ?r
)
2443 (looking-at "record\\>")))
2446 (ada-search-ignore-string-comment
2447 "\\<\\(type\\|use\\)\\>" t nil
)
2448 (if (looking-at "\\<use\\>")
2449 (ada-search-ignore-string-comment "for" t nil nil
2450 'word-search-backward
))
2451 (list (progn (back-to-indentation) (point))
2452 'ada-indent-record-rel-type
)))
2454 ;;---------------------------
2455 ;; starting with b (begin)
2456 ;;---------------------------
2458 ((and (= (downcase (char-after)) ?b
)
2459 (looking-at "begin\\>"))
2461 (if (ada-goto-matching-decl-start t
)
2462 (list (progn (back-to-indentation) (point)) 0)
2463 (ada-indent-on-previous-lines nil orgpoint orgpoint
))))
2465 ;;---------------------------
2466 ;; starting with i (is)
2467 ;;---------------------------
2469 ((and (= (downcase (char-after)) ?i
)
2470 (looking-at "is\\>"))
2472 (if (and ada-indent-is-separate
2474 (goto-char (match-end 0))
2475 (ada-goto-next-non-ws (save-excursion (end-of-line)
2477 (looking-at "\\<abstract\\>\\|\\<separate\\>")))
2479 (ada-goto-stmt-start)
2480 (list (progn (back-to-indentation) (point)) 'ada-indent
))
2482 (ada-goto-stmt-start)
2483 (if (looking-at "\\<package\\|procedure\\|function\\>")
2484 (list (progn (back-to-indentation) (point)) 0)
2485 (list (progn (back-to-indentation) (point)) 'ada-indent
)))))
2487 ;;---------------------------
2488 ;; starting with r (return, renames)
2489 ;;---------------------------
2491 ((and (= (downcase (char-after)) ?r
)
2492 (looking-at "re\\(turn\\|names\\)\\>"))
2495 (let ((var 'ada-indent-return
))
2496 ;; If looking at a renames, skip the 'return' statement too
2497 (if (looking-at "renames")
2500 (set 'pos
(ada-search-ignore-string-comment ";\\|return\\>" t
)))
2502 (= (downcase (char-after (car pos
))) ?r
))
2503 (goto-char (car pos
)))
2504 (set 'var
'ada-indent-renames
)))
2506 (forward-comment -
1000)
2507 (if (= (char-before) ?\
))
2511 ;; If there is a parameter list, and we have a function declaration
2512 ;; or a access to subprogram declaration
2514 (if (and (= (following-char) ?\
()
2518 (looking-at "\\(function\\|procedure\\)\\>"))
2522 (looking-at "\\(function\\|procedure\\)\\>")))))
2524 ;; The indentation depends of the value of ada-indent-return
2525 (if (<= (eval var
) 0)
2526 (list (point) (list '- var
))
2527 (list (progn (backward-word num-back
) (point))
2530 ;; Else there is no parameter list, but we have a function
2531 ;; Only do something special if the user want to indent
2532 ;; relative to the "function" keyword
2533 (if (and (> (eval var
) 0)
2534 (save-excursion (forward-word -
1)
2535 (looking-at "function\\>")))
2536 (list (progn (forward-word -
1) (point)) var
)
2539 (ada-indent-on-previous-lines nil orgpoint orgpoint
)))))))
2541 ;;--------------------------------
2542 ;; starting with 'o' or 'p'
2543 ;; 'or' as statement-start
2544 ;; 'private' as statement-start
2545 ;;--------------------------------
2547 ((and (or (= (downcase (char-after)) ?o
)
2548 (= (downcase (char-after)) ?p
))
2549 (or (ada-looking-at-semi-or)
2550 (ada-looking-at-semi-private)))
2552 ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
2553 (ada-goto-matching-start 1)
2554 (list (progn (back-to-indentation) (point)) 0)))
2556 ;;--------------------------------
2557 ;; starting with 'd' (do)
2558 ;;--------------------------------
2560 ((and (= (downcase (char-after)) ?d
)
2561 (looking-at "do\\>"))
2563 (ada-goto-stmt-start)
2564 (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent
)))
2566 ;;--------------------------------
2567 ;; starting with '-' (comment)
2568 ;;--------------------------------
2570 ((= (char-after) ?-
)
2571 (if ada-indent-comment-as-code
2573 ;; Indent comments on previous line comments if required
2574 ;; We must use a search-forward (even if the code is more complex),
2575 ;; since we want to find the beginning of the comment.
2578 (if (and ada-indent-align-comments
2582 (while (and (not pos
)
2583 (search-forward "--"
2585 (end-of-line) (point))
2587 (unless (ada-in-string-p)
2588 (setq pos
(point))))
2592 ;; Else always on previous line
2593 (ada-indent-on-previous-lines nil orgpoint orgpoint
)))
2595 ;; Else same indentation as the previous line
2596 (list (save-excursion (back-to-indentation) (point)) 0)))
2598 ;;--------------------------------
2599 ;; starting with '#' (preprocessor line)
2600 ;;--------------------------------
2602 ((and (= (char-after) ?
#)
2603 (equal ada-which-compiler
'gnat
)
2604 (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
2605 (list (save-excursion (beginning-of-line) (point)) 0))
2607 ;;--------------------------------
2608 ;; starting with ')' (end of a parameter list)
2609 ;;--------------------------------
2611 ((and (not (eobp)) (= (char-after) ?\
)))
2617 ;;---------------------------------
2618 ;; new/abstract/separate
2619 ;;---------------------------------
2621 ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
2622 (ada-indent-on-previous-lines nil orgpoint orgpoint
))
2624 ;;---------------------------------
2625 ;; package/function/procedure
2626 ;;---------------------------------
2628 ((and (or (= (downcase (char-after)) ?p
) (= (downcase (char-after)) ?f
))
2629 (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
2631 ;; Go up until we find either a generic section, or the end of the
2632 ;; previous subprogram/package
2634 (while (and (not found
)
2635 (ada-search-ignore-string-comment
2636 "\\<\\(generic\\|end\\|begin\\|package\\|procedure\\|function\\)\\>" t
))
2638 ;; avoid "with procedure"... in generic parts
2641 (setq found
(not (looking-at "with"))))))
2643 (if (looking-at "generic")
2644 (list (progn (back-to-indentation) (point)) 0)
2645 (ada-indent-on-previous-lines nil orgpoint orgpoint
))))
2647 ;;---------------------------------
2649 ;;---------------------------------
2651 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2653 (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2654 (append (ada-indent-on-previous-lines nil orgpoint orgpoint
)
2655 '(ada-label-indent))))
2659 ;;---------------------------------
2661 ;;---------------------------------
2662 (or result
(ada-indent-on-previous-lines nil orgpoint orgpoint
))))
2664 (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos
)
2665 "Calculate the indentation for the new line after ORGPOINT.
2666 The result list is based on the previous lines in the buffer.
2667 If NOMOVE is nil, moves point to the beginning of the current statement.
2668 if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
2670 (goto-char initial-pos
))
2671 (let ((oldpoint (point)))
2673 ;; Is inside a parameter-list ?
2674 (if (ada-in-paramlist-p)
2675 (ada-get-indent-paramlist)
2677 ;; move to beginning of current statement
2679 (ada-goto-stmt-start))
2681 ;; no beginning found => don't change indentation
2682 (if (and (eq oldpoint
(point))
2684 (ada-get-indent-nochange)
2689 ada-indent-to-open-paren
2690 (ada-in-open-paren-p))
2691 (ada-get-indent-open-paren))
2693 ((looking-at "end\\>")
2694 (ada-get-indent-end orgpoint
))
2696 ((looking-at ada-loop-start-re
)
2697 (ada-get-indent-loop orgpoint
))
2699 ((looking-at ada-subprog-start-re
)
2700 (ada-get-indent-subprog orgpoint
))
2702 ((looking-at ada-block-start-re
)
2703 (ada-get-indent-block-start orgpoint
))
2705 ((looking-at "\\(sub\\)?type\\>")
2706 (ada-get-indent-type orgpoint
))
2708 ;; "then" has to be included in the case of "select...then abort"
2709 ;; statements, since (goto-stmt-start) at the beginning of
2710 ;; the current function would leave the cursor on that position
2711 ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
2712 (ada-get-indent-if orgpoint
))
2714 ((looking-at "case\\>")
2715 (ada-get-indent-case orgpoint
))
2717 ((looking-at "when\\>")
2718 (ada-get-indent-when orgpoint
))
2720 ((looking-at "\\(\\sw\\|_\\)+[ \t\n]*:[^=]")
2721 (ada-get-indent-label orgpoint
))
2723 ((looking-at "separate\\>")
2724 (ada-get-indent-nochange))
2728 (list (+ (save-excursion (back-to-indentation) (point))
2729 (- ada-label-indent
))))
2732 ((looking-at "with\\>\\|use\\>")
2733 ;; Are we still in that statement, or are we in fact looking at
2734 ;; the previous one ?
2735 (if (save-excursion (search-forward ";" oldpoint t
))
2736 (list (progn (back-to-indentation) (point)) 0)
2737 (list (point) (if (looking-at "with")
2742 (ada-get-indent-noindent orgpoint
)))))
2745 (defun ada-get-indent-open-paren ()
2746 "Calculate the indentation when point is behind an unclosed parenthesis."
2747 (list (ada-in-open-paren-p) 0))
2749 (defun ada-get-indent-nochange ()
2750 "Return the current indentation of the previous line."
2753 (back-to-indentation)
2756 (defun ada-get-indent-paramlist ()
2757 "Calculate the indentation when point is inside a parameter list."
2759 (ada-search-ignore-string-comment "[^ \t\n]" t nil t
)
2761 ;; in front of the first parameter
2762 ((= (char-after) ?\
()
2763 (goto-char (match-end 0))
2766 ;; in front of another parameter
2767 ((= (char-after) ?\
;)
2768 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t
)))
2769 (ada-goto-next-non-ws)
2772 ;; After an affectation (default parameter value in subprogram
2774 ((and (= (following-char) ?
=) (= (preceding-char) ?
:))
2775 (back-to-indentation)
2776 (list (point) 'ada-broken-indent
))
2778 ;; inside a parameter declaration
2780 (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t
)))
2781 (ada-goto-next-non-ws)
2782 (list (point) 'ada-broken-indent
)))))
2784 (defun ada-get-indent-end (orgpoint)
2785 "Calculate the indentation when point is just before an end statement.
2786 ORGPOINT is the limit position used in the calculation."
2787 (let ((defun-name nil
)
2790 ;; is the line already terminated by ';' ?
2792 (ada-search-ignore-string-comment ";" nil orgpoint nil
2795 ;; yes, look what's following 'end'
2798 (ada-goto-next-non-ws)
2801 ;; loop/select/if/case/return
2803 ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
2804 (save-excursion (ada-check-matching-start (match-string 0)))
2805 (list (save-excursion (back-to-indentation) (point)) 0))
2810 ((looking-at "\\<record\\>")
2812 (ada-check-matching-start (match-string 0))
2813 ;; we are now looking at the matching "record" statement
2815 (ada-goto-stmt-start)
2816 ;; now on the matching type declaration, or use clause
2817 (unless (looking-at "\\(for\\|type\\)\\>")
2818 (ada-search-ignore-string-comment "\\<type\\>" t
))
2819 (list (progn (back-to-indentation) (point)) 0)))
2821 ;; a named block end
2823 ((looking-at ada-ident-re
)
2824 (setq defun-name
(match-string 0))
2826 (ada-goto-matching-start 0)
2827 (ada-check-defun-name defun-name
))
2828 (list (progn (back-to-indentation) (point)) 0))
2830 ;; a block-end without name
2832 ((= (char-after) ?\
;)
2834 (ada-goto-matching-start 0)
2835 (if (looking-at "\\<begin\\>")
2837 (setq indent
(list (point) 0))
2838 (if (ada-goto-matching-decl-start t
)
2839 (list (progn (back-to-indentation) (point)) 0)
2841 (list (progn (back-to-indentation) (point)) 0)
2844 ;; anything else - should maybe signal an error ?
2847 (list (save-excursion (back-to-indentation) (point))
2848 'ada-broken-indent
))))
2850 (list (save-excursion (back-to-indentation) (point))
2851 'ada-broken-indent
))))
2853 (defun ada-get-indent-case (orgpoint)
2854 "Calculate the indentation when point is just before a case statement.
2855 ORGPOINT is the limit position used in the calculation."
2856 (let ((match-cons nil
)
2860 ;; case..is..when..=>
2863 (setq match-cons
(and
2864 ;; the `=>' must be after the keyword `is'.
2865 (ada-search-ignore-string-comment
2866 "is" nil orgpoint nil
'word-search-forward
)
2867 (ada-search-ignore-string-comment
2868 "[ \t\n]+=>" nil orgpoint
))))
2870 (goto-char (car match-cons
))
2871 (unless (ada-search-ignore-string-comment "when" t opos
)
2872 (error "Missing 'when' between 'case' and '=>'"))
2873 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)))
2878 (setq match-cons
(ada-search-ignore-string-comment
2879 "when" nil orgpoint nil
'word-search-forward
)))
2880 (goto-char (cdr match-cons
))
2881 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
2886 (setq match-cons
(ada-search-ignore-string-comment
2887 "is" nil orgpoint nil
'word-search-forward
)))
2888 (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent
))
2893 (list (save-excursion (back-to-indentation) (point))
2894 'ada-broken-indent
)))))
2896 (defun ada-get-indent-when (orgpoint)
2897 "Calculate the indentation when point is just before a when statement.
2898 ORGPOINT is the limit position used in the calculation."
2899 (let ((cur-indent (save-excursion (back-to-indentation) (point))))
2900 (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint
)
2901 (list cur-indent
'ada-indent
)
2902 (list cur-indent
'ada-broken-indent
))))
2904 (defun ada-get-indent-if (orgpoint)
2905 "Calculate the indentation when point is just before an if statement.
2906 ORGPOINT is the limit position used in the calculation."
2907 (let ((cur-indent (save-excursion (back-to-indentation) (point)))
2910 ;; Move to the correct then (ignore all "and then")
2912 (while (and (setq match-cons
(ada-search-ignore-string-comment
2913 "\\<\\(then\\|and[ \t]*then\\)\\>"
2915 (= (downcase (char-after (car match-cons
))) ?a
)))
2916 ;; If "then" was found (we are looking at it)
2920 ;; 'then' first in separate line ?
2921 ;; => indent according to 'then',
2922 ;; => else indent according to 'if'
2925 (back-to-indentation)
2926 (looking-at "\\<then\\>"))
2927 (setq cur-indent
(save-excursion (back-to-indentation) (point))))
2930 (list cur-indent
'ada-indent
))
2932 (list cur-indent
'ada-broken-indent
))))
2934 (defun ada-get-indent-block-start (orgpoint)
2935 "Calculate the indentation when point is at the start of a block.
2936 ORGPOINT is the limit position used in the calculation."
2941 (setq pos
(ada-goto-next-non-ws orgpoint
)))
2944 (ada-indent-on-previous-lines t orgpoint
)))
2946 ;; Special case for record types, for instance for:
2947 ;; type A is (B : Integer;
2948 ;; C : Integer) is record
2949 ;; null; -- This is badly indented otherwise
2950 ((looking-at "record")
2952 ;; If record is at the beginning of the line, indent from there
2955 (looking-at "^[ \t]*\\(record\\|limited record\\)"))
2956 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)
2958 ;; else indent relative to the type command
2959 (list (save-excursion
2960 (car (ada-search-ignore-string-comment "\\<type\\>" t
)))
2963 ;; nothing follows the block-start
2965 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
)))))
2967 (defun ada-get-indent-subprog (orgpoint)
2968 "Calculate the indentation when point is just before a subprogram.
2969 ORGPOINT is the limit position used in the calculation."
2970 (let ((match-cons nil
)
2971 (cur-indent (save-excursion (back-to-indentation) (point)))
2974 ;; is there an 'is' in front of point ?
2978 (ada-search-ignore-string-comment
2979 "\\<\\(is\\|do\\)\\>" nil orgpoint
)))
2981 ;; yes, then skip to its end
2985 (goto-char (cdr match-cons
)))
2987 ;; no, then goto next non-ws, if there is one in front of point
2990 (unless (ada-goto-next-non-ws orgpoint
)
2991 (goto-char orgpoint
))))
2995 ;; nothing follows 'is'
3000 (not (ada-search-ignore-string-comment
3001 "[^ \t\n]" nil orgpoint t
))))
3002 (list cur-indent
'ada-indent
))
3004 ;; is abstract/separate/new ...
3010 (ada-search-ignore-string-comment
3011 "\\<\\(separate\\|new\\|abstract\\)\\>"
3013 (goto-char (car match-cons
))
3014 (ada-search-ignore-string-comment ada-subprog-start-re t
)
3015 (ada-get-indent-noindent orgpoint
))
3017 ;; something follows 'is'
3021 (save-excursion (setq match-cons
(ada-goto-next-non-ws orgpoint
)))
3022 (goto-char match-cons
)
3023 (ada-indent-on-previous-lines t orgpoint
)))
3028 (ada-search-ignore-string-comment ";" nil orgpoint nil
'search-forward
))
3029 (list cur-indent
0))
3034 (list cur-indent
'ada-broken-indent
)))))
3036 (defun ada-get-indent-noindent (orgpoint)
3037 "Calculate the indentation when point is just before a 'noindent stmt'.
3038 ORGPOINT is the limit position used in the calculation."
3045 ;; This one is called when indenting a line preceded by a multi-line
3046 ;; subprogram declaration (in that case, we are at this point inside
3047 ;; the parameter declaration list)
3048 ((ada-in-paramlist-p)
3049 (ada-previous-procedure)
3050 (list (save-excursion (back-to-indentation) (point)) 0))
3052 ;; This one is called when indenting the second line of a multi-line
3053 ;; declaration section, in a declare block or a record declaration
3054 ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
3055 (list (save-excursion (back-to-indentation) (point))
3056 'ada-broken-decl-indent
))
3058 ;; This one is called in every over case when indenting a line at the
3061 (if (looking-at ada-named-block-re
)
3062 (setq label
(- ada-label-indent
))
3066 ;; "with private" or "null record" cases
3067 (if (or (save-excursion
3068 (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint
)
3070 (save-excursion (forward-char -
7);; skip back "private"
3071 (ada-goto-previous-word)
3072 (looking-at "with"))))
3074 (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint
)
3076 (save-excursion (forward-char -
6);; skip back "record"
3077 (ada-goto-previous-word)
3078 (looking-at "null")))))
3081 (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t
)
3082 (list (save-excursion (back-to-indentation) (point)) 0)))))
3084 (ada-search-ignore-string-comment ";" nil orgpoint nil
3086 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0)
3087 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3088 'ada-broken-indent
)))))))
3090 (defun ada-get-indent-label (orgpoint)
3091 "Calculate the indentation when before a label or variable declaration.
3092 ORGPOINT is the limit position used in the calculation."
3093 (let ((match-cons nil
)
3094 (cur-indent (save-excursion (back-to-indentation) (point))))
3095 (ada-search-ignore-string-comment ":" nil
)
3099 (setq match-cons
(ada-search-ignore-string-comment
3100 ada-loop-start-re nil orgpoint
)))
3101 (goto-char (car match-cons
))
3102 (ada-get-indent-loop orgpoint
))
3106 (setq match-cons
(ada-search-ignore-string-comment
3107 "\\<declare\\|begin\\>" nil orgpoint
)))
3108 (goto-char (car match-cons
))
3109 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3111 ;; variable declaration
3114 (ada-search-ignore-string-comment ";" nil orgpoint
))
3116 (list cur-indent
'ada-broken-indent
)))
3118 ;; nothing follows colon
3120 (list cur-indent
'(- ada-label-indent
))))))
3122 (defun ada-get-indent-loop (orgpoint)
3123 "Calculate the indentation when just before a loop or a for ... use.
3124 ORGPOINT is the limit position used in the calculation."
3125 (let ((match-cons nil
)
3128 ;; If looking at a named block, skip the label
3129 (label (save-excursion
3131 (if (looking-at ada-named-block-re
)
3132 (- ada-label-indent
)
3138 ;; statement complete
3141 (ada-search-ignore-string-comment ";" nil orgpoint nil
3143 (list (+ (save-excursion (back-to-indentation) (point)) label
) 0))
3147 ((looking-at "loop\\>")
3148 (setq pos
(ada-get-indent-block-start orgpoint
))
3151 (list (+ (car pos
) label
) (cadr pos
))))
3154 ;; 'for'- loop (or also a for ... use statement)
3156 ((looking-at "for\\>")
3163 (goto-char (match-end 0))
3164 (ada-goto-next-non-ws orgpoint
)
3166 (if (= (char-after) ?
') (forward-word 1) t
)
3167 (ada-goto-next-non-ws orgpoint
)
3168 (looking-at "\\<use\\>")
3170 ;; check if there is a 'record' before point
3173 (setq match-cons
(ada-search-ignore-string-comment
3174 "record" nil orgpoint nil
'word-search-forward
))
3178 (goto-char (car match-cons
))
3179 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3180 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
3187 (setq match-cons
(ada-search-ignore-string-comment
3188 "loop" nil orgpoint nil
'word-search-forward
)))
3189 (goto-char (car match-cons
))
3191 ;; indent according to 'loop', if it's first in the line;
3192 ;; otherwise to 'for'
3194 (unless (save-excursion
3195 (back-to-indentation)
3196 (looking-at "\\<loop\\>"))
3198 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3201 ;; for-statement is broken
3204 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3205 'ada-broken-indent
))))
3210 ((looking-at "while\\>")
3215 (setq match-cons
(ada-search-ignore-string-comment
3216 "loop" nil orgpoint nil
'word-search-forward
)))
3219 (goto-char (car match-cons
))
3221 ;; indent according to 'loop', if it's first in the line;
3222 ;; otherwise to 'while'.
3224 (unless (save-excursion
3225 (back-to-indentation)
3226 (looking-at "\\<loop\\>"))
3228 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3231 (list (+ (save-excursion (back-to-indentation) (point)) label
)
3232 'ada-broken-indent
))))))
3234 (defun ada-get-indent-type (orgpoint)
3235 "Calculate the indentation when before a type statement.
3236 ORGPOINT is the limit position used in the calculation."
3237 (let ((match-dat nil
))
3240 ;; complete record declaration
3244 (setq match-dat
(ada-search-ignore-string-comment
3245 "end" nil orgpoint nil
'word-search-forward
))
3246 (ada-goto-next-non-ws)
3247 (looking-at "\\<record\\>")
3249 (ada-goto-next-non-ws)
3250 (= (char-after) ?\
;)))
3251 (goto-char (car match-dat
))
3252 (list (save-excursion (back-to-indentation) (point)) 0))
3257 (setq match-dat
(ada-search-ignore-string-comment
3258 "record" nil orgpoint nil
'word-search-forward
)))
3259 (goto-char (car match-dat
))
3260 (list (save-excursion (back-to-indentation) (point)) 'ada-indent
))
3262 ;; complete type declaration
3265 (ada-search-ignore-string-comment ";" nil orgpoint nil
3267 (list (save-excursion (back-to-indentation) (point)) 0))
3269 ;; "type ... is", but not "type ... is ...", which is broken
3273 (ada-search-ignore-string-comment "is" nil orgpoint nil
3274 'word-search-forward
)
3275 (not (ada-goto-next-non-ws orgpoint
))))
3276 (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent
))
3281 (list (save-excursion (back-to-indentation) (point))
3282 'ada-broken-indent
)))))
3285 ;; -----------------------------------------------------------
3286 ;; -- searching and matching
3287 ;; -----------------------------------------------------------
3289 (defun ada-goto-stmt-start ()
3290 "Move point to the beginning of the statement that point is in or after.
3291 Return the new position of point.
3292 As a special case, if we are looking at a closing parenthesis, skip to the
3294 (let ((match-dat nil
)
3297 (setq match-dat
(ada-search-prev-end-stmt))
3301 ;; found a previous end-statement => check if anything follows
3303 (unless (looking-at "declare")
3305 (unless (save-excursion
3306 (goto-char (cdr match-dat
))
3307 (ada-goto-next-non-ws orgpoint
))
3309 ;; nothing follows => it's the end-statement directly in
3310 ;; front of point => search again
3312 (setq match-dat
(ada-search-prev-end-stmt)))
3314 ;; if found the correct end-statement => goto next non-ws
3317 (goto-char (cdr match-dat
)))
3318 (ada-goto-next-non-ws)
3322 ;; no previous end-statement => we are at the beginning of the
3323 ;; accessible part of the buffer
3326 (goto-char (point-min))
3328 ;; skip to the very first statement, if there is one
3330 (unless (ada-goto-next-non-ws orgpoint
)
3331 (goto-char orgpoint
))))
3335 (defun ada-search-prev-end-stmt ()
3336 "Move point to previous end statement.
3337 Return a cons cell whose car is the beginning and whose cdr
3338 is the end of the match."
3339 (let ((match-dat nil
)
3342 ;; search until found or beginning-of-buffer
3346 (setq match-dat
(ada-search-ignore-string-comment
3347 ada-end-stmt-re t
)))
3349 (goto-char (car match-dat
))
3350 (unless (ada-in-open-paren-p)
3354 "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
3356 (ada-goto-previous-word)
3357 (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
3362 (and (save-excursion (ada-goto-previous-word)
3363 (ada-goto-previous-word)
3364 (not (looking-at "subtype")))
3366 (save-excursion (goto-char (cdr match-dat
))
3367 (ada-goto-next-non-ws)
3368 ;; words that can go after an 'is'
3373 '("separate" "access" "array"
3374 "private" "abstract" "new") t
)
3377 ((looking-at "private")
3380 (setq found
(not (looking-at "is")))))
3391 (defun ada-goto-next-non-ws (&optional limit
)
3392 "Skip white spaces, newlines and comments to next non-ws character.
3393 Stop the search at LIMIT.
3394 Do not call this function from within a string."
3396 (setq limit
(point-max)))
3397 (while (and (<= (point) limit
)
3398 (progn (forward-comment 10000)
3399 (if (and (not (eobp))
3400 (save-excursion (forward-char 1)
3402 (progn (forward-sexp 1) t
)))))
3403 (if (< (point) limit
)
3409 (defun ada-goto-stmt-end (&optional limit
)
3410 "Move point to the end of the statement that point is in or before.
3411 Return the new position of point or nil if not found.
3412 Stop the search at LIMIT."
3413 (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit
)
3418 (defun ada-goto-next-word (&optional backward
)
3419 "Move point to the beginning of the next word of Ada code.
3420 If BACKWARD is non-nil, jump to the beginning of the previous word.
3421 Return the new position of point or nil if not found."
3422 (let ((match-cons nil
)
3424 (old-syntax (char-to-string (char-syntax ?_
))))
3425 (modify-syntax-entry ?_
"w")
3427 (skip-syntax-forward "w"))
3428 (if (setq match-cons
3430 (ada-search-ignore-string-comment "\\w" t nil t
)
3431 (ada-search-ignore-string-comment "\\w" nil nil t
)))
3433 ;; move to the beginning of the word found
3436 (goto-char (car match-cons
))
3437 (skip-syntax-backward "w")
3440 ;; if not found, restore old position of point
3442 (goto-char orgpoint
)
3444 (modify-syntax-entry ?_ old-syntax
))
3448 (defun ada-check-matching-start (keyword)
3449 "Signal an error if matching block start is not KEYWORD.
3450 Moves point to the matching block start."
3451 (ada-goto-matching-start 0)
3452 (unless (looking-at (concat "\\<" keyword
"\\>"))
3453 (error "Matching start is not '%s'" keyword
)))
3456 (defun ada-check-defun-name (defun-name)
3457 "Check if the name of the matching defun really is DEFUN-NAME.
3458 Assumes point to be already positioned by `ada-goto-matching-start'.
3459 Moves point to the beginning of the declaration."
3461 ;; named block without a `declare'; ada-goto-matching-start leaves
3462 ;; point at start of 'begin' for a block.
3464 (ada-goto-previous-word)
3465 (looking-at (concat "\\<" defun-name
"\\> *:")))
3469 ;; 'accept' or 'package' ?
3471 (unless (looking-at ada-subprog-start-re
)
3472 (ada-goto-matching-decl-start))
3474 ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
3478 ;; a named 'declare'-block ? => jump to the label
3480 (if (looking-at "\\<declare\\>")
3482 (forward-comment -
1)
3485 ;; no, => 'procedure'/'function'/'task'/'protected'
3491 ;; skip 'body' 'type'
3493 (if (looking-at "\\<\\(body\\|type\\)\\>")
3498 ;; should be looking-at the correct name
3500 (unless (looking-at (concat "\\<" defun-name
"\\>"))
3501 (error "Matching defun has different name: %s"
3502 (buffer-substring (point)
3503 (progn (forward-sexp 1) (point))))))))
3505 (defun ada-goto-matching-decl-start (&optional noerror recursive
)
3506 "Move point to the matching declaration start of the current 'begin'.
3507 If NOERROR is non-nil, it only returns nil if no match was found."
3508 (let ((nest-count 1)
3510 ;; first should be set to t if we should stop at the first
3511 ;; "begin" we encounter.
3512 (first (not recursive
))
3517 ;; Ignore "when" most of the time, except if we are looking at the
3518 ;; beginning of a block (structure: case .. is
3522 (if (looking-at "begin")
3523 (setq stop-at-when t
))
3526 (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
3528 (ada-search-ignore-string-comment
3529 "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t
)
3530 (looking-at "generic")))
3531 (setq count-generic t
))
3533 ;; search backward for interesting keywords
3535 (not (zerop nest-count
))
3536 (ada-search-ignore-string-comment ada-matching-decl-start-re t
))
3538 ;; calculate nest-depth
3543 (ada-goto-matching-start 1 noerror
)
3545 ;; In some case, two begin..end block can follow each other closely,
3546 ;; which we have to detect, as in
3551 ;; begin -- here we should go to procedure, not begin
3554 (if (looking-at "begin")
3555 (let ((loop-again t
))
3558 ;; If begin was just there as the beginning of a block
3559 ;; (with no declare) then do nothing, otherwise just
3560 ;; register that we have to find the statement that
3561 ;; required the begin
3563 (ada-search-ignore-string-comment
3564 "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
3567 (if (looking-at "end")
3568 (ada-goto-matching-start 1 noerror t
)
3569 ;; (ada-goto-matching-decl-start noerror t)
3571 (setq loop-again nil
)
3572 (unless (looking-at "begin")
3573 (setq nest-count
(1+ nest-count
))))
3577 ((looking-at "generic")
3581 (setq nest-count
(1- nest-count
)))))
3586 (unless (looking-at "\\<end[ \t\n]*if\\>")
3588 (setq nest-count
(1- nest-count
))
3589 (setq first nil
)))))
3592 ((looking-at "declare\\|generic")
3593 (setq nest-count
(1- nest-count
))
3597 ;; check if it is only a type definition, but not a protected
3598 ;; type definition, which should be handled like a procedure.
3599 (if (or (looking-at "is[ \t]+<>")
3601 (forward-comment -
10000)
3604 ;; Detect if we have a closing parenthesis (Could be
3605 ;; either the end of subprogram parameters or (<>)
3606 ;; in a type definition
3607 (if (= (char-after) ?\
))
3611 (forward-comment -
10000)
3613 (skip-chars-backward "a-zA-Z0-9_.'")
3614 (ada-goto-previous-word)
3616 (looking-at "\\<\\(sub\\)?type\\|case\\>")
3618 (ada-goto-previous-word)
3619 (not (looking-at "\\<protected\\>"))))
3621 (goto-char (match-beginning 0))
3623 (setq nest-count
(1- nest-count
))
3629 (ada-goto-previous-word)
3631 (goto-char (match-beginning 0))))
3634 (looking-at "begin"))
3635 (setq nest-count
0))
3637 ((looking-at "when")
3640 (unless (looking-at "\\<exit[ \t\n]*when\\>")
3643 (setq nest-count
(1- nest-count
)))
3646 ((looking-at "begin")
3650 (setq nest-count
(1+ nest-count
))
3655 ;; check if declaration-start is really found
3658 (if (looking-at "is")
3659 (ada-search-ignore-string-comment ada-subprog-start-re t
)
3660 (looking-at "declare\\|generic")))
3663 (error "No matching proc/func/task/declare/package/protected")))
3666 (defun ada-goto-matching-start (&optional nest-level noerror gotothen
)
3667 "Move point to the beginning of a block-start.
3668 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3669 If NOERROR is non-nil, it only returns nil if no matching start was found.
3670 If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
3671 (let ((nest-count (if nest-level nest-level
0))
3674 (last-was-begin '())
3675 ;; List all keywords encountered while traversing
3676 ;; something like '("end" "end" "begin")
3677 ;; This is removed from the list when "package", "procedure",...
3678 ;; are seen. The goal is to find whether a package has an elaboration
3683 ;; search backward for interesting keywords
3686 (ada-search-ignore-string-comment ada-matching-start-re t
))
3688 (unless (and (looking-at "\\<record\\>")
3691 (looking-at "\\<null\\>")))
3693 ;; calculate nest-depth
3695 ;; found block end => increase nest depth
3697 (push nil last-was-begin
)
3698 (setq nest-count
(1+ nest-count
)))
3700 ;; found loop/select/record/case/if => check if it starts or
3702 ((looking-at "loop\\|select\\|record\\|case\\|if")
3705 ;; check if keyword follows 'end'
3706 (ada-goto-previous-word)
3707 (if (looking-at "\\<end\\>[ \t]*[^;]")
3709 ;; it ends a block => increase nest depth
3710 (setq nest-count
(1+ nest-count
)
3712 (push nil last-was-begin
))
3714 ;; it starts a block => decrease nest depth
3715 (setq nest-count
(1- nest-count
))
3717 ;; Some nested "begin .. end" blocks with no "declare"?
3718 ;; => remove those entries
3719 (while (car last-was-begin
)
3720 (setq last-was-begin
(cdr (cdr last-was-begin
))))
3722 (setq last-was-begin
(cdr last-was-begin
))
3727 ;; found package start => check if it really is a block
3728 ((looking-at "package")
3730 ;; ignore if this is just a renames statement
3731 (let ((current (point))
3732 (pos (ada-search-ignore-string-comment
3733 "\\<\\(is\\|renames\\|;\\)\\>" nil
)))
3735 (goto-char (car pos
))
3737 "No matching 'is' or 'renames' for 'package' at"
3739 (number-to-string (count-lines 1 (1+ current
)))))))
3740 (unless (looking-at "renames")
3743 (ada-goto-next-non-ws)
3744 ;; ignore it if it is only a declaration with 'new'
3745 ;; We could have package Foo is new ....
3746 ;; or package Foo is separate;
3747 ;; or package Foo is begin null; end Foo
3748 ;; for elaboration code (elaboration)
3749 (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
3750 (not (car last-was-begin
)))
3751 (setq nest-count
(1- nest-count
))))))
3753 (setq last-was-begin
(cdr last-was-begin
))
3755 ;; found task start => check if it has a body
3756 ((looking-at "task")
3759 (ada-goto-next-non-ws)
3761 ((looking-at "\\<body\\>"))
3762 ((looking-at "\\<type\\>")
3763 ;; In that case, do nothing if there is a "is"
3764 (forward-word 2);; skip "type"
3765 (ada-goto-next-non-ws);; skip type name
3767 ;; Do nothing if we are simply looking at a simple
3768 ;; "task type name;" statement with no block
3769 (unless (looking-at ";")
3771 ;; Skip the parameters
3772 (if (looking-at "(")
3773 (ada-search-ignore-string-comment ")" nil
))
3774 (let ((tmp (ada-search-ignore-string-comment
3775 "\\<\\(is\\|;\\)\\>" nil
)))
3778 (goto-char (car tmp
))
3779 (if (looking-at "is")
3780 (setq nest-count
(1- nest-count
)))))))))
3782 ;; Check if that task declaration had a block attached to
3783 ;; it (i.e do nothing if we have just "task name;")
3784 (unless (progn (forward-word 1)
3785 (looking-at "[ \t]*;"))
3786 (setq nest-count
(1- nest-count
))))))
3787 (setq last-was-begin
(cdr last-was-begin
))
3790 ((looking-at "declare")
3791 ;; remove entry for begin and end (include nested begin..end
3793 (setq last-was-begin
(cdr last-was-begin
))
3795 (while (and (> count
0))
3796 (if (equal (car last-was-begin
) t
)
3797 (setq count
(1+ count
))
3798 (setq count
(1- count
)))
3799 (setq last-was-begin
(cdr last-was-begin
))
3802 ((looking-at "protected")
3803 ;; Ignore if this is just a declaration
3805 (let ((pos (ada-search-ignore-string-comment
3806 "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil
)))
3808 (goto-char (car pos
)))
3809 (if (looking-at "is")
3810 ;; remove entry for end
3811 (setq last-was-begin
(cdr last-was-begin
)))))
3812 (setq nest-count
(1- nest-count
)))
3814 ((or (looking-at "procedure")
3815 (looking-at "function"))
3816 ;; Ignore if this is just a declaration
3818 (let ((pos (ada-search-ignore-string-comment
3819 "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil
)))
3821 (goto-char (car pos
)))
3822 (if (looking-at "is")
3823 ;; remove entry for begin and end
3824 (setq last-was-begin
(cdr (cdr last-was-begin
))))))
3827 ;; all the other block starts
3829 (push (looking-at "begin") last-was-begin
)
3830 (setq nest-count
(1- nest-count
)))
3834 ;; match is found, if nest-depth is zero
3835 (setq found
(zerop nest-count
))))) ; end of loop
3841 ;; match found => is there anything else to do ?
3846 ;; found 'if' => skip to 'then', if it's on a separate line
3847 ;; and GOTOTHEN is non-nil
3853 (ada-search-ignore-string-comment "then" nil nil nil
3854 'word-search-forward
)
3855 (back-to-indentation)
3856 (looking-at "\\<then\\>")))
3857 (goto-char (match-beginning 0)))
3860 ;; found 'do' => skip back to 'accept' or 'return'
3863 (unless (ada-search-ignore-string-comment
3864 "\\<accept\\|return\\>" t
)
3865 (error "Missing 'accept' or 'return' in front of 'do'"))))
3870 (error "No matching start"))))))
3873 (defun ada-goto-matching-end (&optional nest-level noerror
)
3874 "Move point to the end of a block.
3875 Which block depends on the value of NEST-LEVEL, which defaults to zero.
3876 If NOERROR is non-nil, it only returns nil if no matching start found."
3877 (let ((nest-count (or nest-level
0))
3878 (regex (eval-when-compile
3880 (regexp-opt '("end" "loop" "select" "begin" "case"
3881 "if" "task" "package" "record" "do"
3882 "procedure" "function") t
)
3887 ;; First is used for subprograms: they are generally handled
3888 ;; recursively, but of course we do not want to do that the
3889 ;; first time (see comment below about subprograms)
3890 (first (not (looking-at "declare"))))
3892 ;; If we are already looking at one of the keywords, this shouldn't count
3893 ;; in the nesting loop below, so we just make sure we don't count it.
3894 ;; "declare" is a special case because we need to look after the "begin"
3896 (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
3900 ;; search forward for interesting keywords
3904 (ada-search-ignore-string-comment regex nil
))
3907 ;; calculate nest-depth
3911 ;; procedures and functions need to be processed recursively, in
3912 ;; case they are defined in a declare/begin block, as in:
3913 ;; declare -- NL 0 (nested level)
3915 ;; procedure B (C : D) is
3918 ;; end B; -- NL 0, and we would exit
3920 ;; end; -- we should exit here
3921 ;; processing them recursively avoids the need for any special
3923 ;; Nothing should be done if we have only the specs or a
3924 ;; generic instantion.
3926 ((and (looking-at "\\<procedure\\|function\\>"))
3931 (ada-search-ignore-string-comment "is\\|;")
3932 (if (= (char-before) ?s
)
3934 (ada-goto-next-non-ws)
3935 (unless (looking-at "\\<new\\>")
3938 (ada-goto-matching-end 0 t
)))))))
3940 ;; found block end => decrease nest depth
3941 ((looking-at "\\<end\\>")
3942 (setq nest-count
(1- nest-count
)
3943 found
(<= nest-count
0))
3944 ;; skip the following keyword
3946 (skip-chars-forward "end")
3947 (ada-goto-next-non-ws)
3948 (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
3951 ;; found package start => check if it really starts a block, and is not
3952 ;; in fact a generic instantiation for instance
3953 ((looking-at "\\<package\\>")
3954 (ada-search-ignore-string-comment "is" nil nil nil
3955 'word-search-forward
)
3956 (ada-goto-next-non-ws)
3957 ;; ignore and skip it if it is only a 'new' package
3958 (if (looking-at "\\<new\\>")
3959 (goto-char (match-end 0))
3960 (setq nest-count
(1+ nest-count
)
3961 found
(<= nest-count
0))))
3963 ;; all the other block starts
3966 (setq nest-count
(1+ nest-count
)))
3967 (setq found
(<= nest-count
0))
3968 (forward-word 1))) ; end of 'cond'
3976 (error "No matching end")))
3980 (defun ada-search-ignore-string-comment
3981 (search-re &optional backward limit paramlists search-func
)
3982 "Regexp-search for SEARCH-RE, ignoring comments, strings.
3983 Returns a cons cell of begin and end of match data or nil, if not found.
3984 If BACKWARD is non-nil, search backward; search forward otherwise.
3985 The search stops at pos LIMIT.
3986 If PARAMLISTS is nil, ignore parameter lists.
3987 The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
3988 in case we are searching for a constant string.
3989 Point is moved at the beginning of the SEARCH-RE."
3994 (previous-syntax-table (syntax-table)))
3996 ;; FIXME: need to pass BACKWARD to search-func!
3998 (setq search-func
(if backward
're-search-backward
're-search-forward
)))
4001 ;; search until found or end-of-buffer
4002 ;; We have to test that we do not look further than limit
4004 (set-syntax-table ada-mode-symbol-syntax-table
)
4005 (while (and (not found
)
4007 (or (and backward
(<= limit
(point)))
4008 (>= limit
(point))))
4009 (funcall search-func search-re limit
1))
4010 (setq begin
(match-beginning 0))
4011 (setq end
(match-end 0))
4013 (setq parse-result
(parse-partial-sexp
4014 (save-excursion (beginning-of-line) (point))
4019 ;; If inside a string, skip it (and the following comments)
4021 ((ada-in-string-p parse-result
)
4022 (if (featurep 'xemacs
)
4023 (search-backward "\"" nil t
)
4024 (goto-char (nth 8 parse-result
)))
4025 (unless backward
(forward-sexp 1)))
4027 ;; If inside a comment, skip it (and the following comments)
4028 ;; There is a special code for comments at the end of the file
4030 ((ada-in-comment-p parse-result
)
4031 (if (featurep 'xemacs
)
4035 (forward-comment -
1))
4036 (goto-char (nth 8 parse-result
)))
4038 ;; at the end of the file, it is not possible to skip a comment
4039 ;; so we just go at the end of the line
4040 (if (forward-comment 1)
4042 (forward-comment 1000)
4043 (beginning-of-line))
4046 ;; directly in front of a comment => skip it, if searching forward
4048 ((and (= (char-after begin
) ?-
) (= (char-after (1+ begin
)) ?-
))
4049 (unless backward
(progn (forward-char -
1) (forward-comment 1000))))
4052 ;; found a parameter-list but should ignore it => skip it
4054 ((and (not paramlists
) (ada-in-paramlist-p))
4056 (search-backward "(" nil t
)
4057 (search-forward ")" nil t
)))
4059 ;; found what we were looking for
4062 (setq found t
)))) ; end of loop
4064 (set-syntax-table previous-syntax-table
)
4070 ;; -------------------------------------------------------
4071 ;; -- Testing the position of the cursor
4072 ;; -------------------------------------------------------
4074 (defun ada-in-decl-p ()
4075 "Return t if point is inside a declarative part.
4076 Assumes point to be at the end of a statement."
4077 (or (ada-in-paramlist-p)
4079 (ada-goto-matching-decl-start t
))))
4082 (defun ada-looking-at-semi-or ()
4083 "Return t if looking at an 'or' following a semicolon."
4085 (and (looking-at "\\<or\\>")
4088 (ada-goto-stmt-start)
4089 (looking-at "\\<or\\>")))))
4092 (defun ada-looking-at-semi-private ()
4093 "Return t if looking at the start of a private section in a package.
4094 Return nil if the private is part of the package name, as in
4095 'private package A is...' (this can only happen at top level)."
4097 (and (looking-at "\\<private\\>")
4098 (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
4100 ;; Make sure this is the start of a private section (ie after
4101 ;; a semicolon or just after the package declaration, but not
4102 ;; after a 'type ... is private' or 'is new ... with private'
4104 ;; Note that a 'private' statement at the beginning of the buffer
4105 ;; does not indicate a private section, since this is instead a
4106 ;; 'private procedure ...'
4107 (progn (forward-comment -
1000)
4109 (or (= (char-before) ?\
;)
4110 (and (forward-word -
3)
4111 (looking-at "\\<package\\>"))))))))
4114 (defun ada-in-paramlist-p ()
4115 "Return t if point is inside a parameter-list."
4118 (ada-search-ignore-string-comment "(\\|)" t nil t
)
4119 ;; inside parentheses ?
4120 (= (char-after) ?\
()
4122 ;; We could be looking at two things here:
4123 ;; operator definition: function "." (
4124 ;; subprogram definition: procedure .... (
4125 ;; Let's skip back over the first one
4127 (skip-chars-backward " \t\n")
4128 (if (= (char-before) ?
\")
4133 ;; and now over the second one
4136 ;; We should ignore the case when the reserved keyword is in a
4137 ;; comment (for instance, when we have:
4140 ;; we should return nil
4142 (not (ada-in-string-or-comment-p))
4144 ;; right keyword two words before parenthesis ?
4145 ;; Type is in this list because of discriminants
4146 (looking-at (eval-when-compile
4148 "procedure\\|function\\|body\\|"
4149 "task\\|entry\\|accept\\|"
4150 "access[ \t]+procedure\\|"
4151 "access[ \t]+function\\|"
4155 (defun ada-search-ignore-complex-boolean (regexp backwardp
)
4156 "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
4157 If BACKWARDP is non-nil, search backward; search forward otherwise."
4159 (while (and (setq result
(ada-search-ignore-string-comment regexp backwardp
))
4160 (save-excursion (forward-word -
1)
4161 (looking-at "and then\\|or else"))))
4164 (defun ada-in-open-paren-p ()
4165 "Non-nil if in an open parenthesis.
4166 Return value is the position of the first non-ws behind the last unclosed
4167 parenthesis, or nil."
4169 (let ((parse (parse-partial-sexp
4171 (or (car (ada-search-ignore-complex-boolean
4172 "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
4178 (goto-char (1+ (nth 1 parse
)))
4180 ;; Skip blanks, if they are not followed by a comment
4182 ;; type A is ( Value_0,
4184 ;; type B is ( -- comment
4187 (if (or (not ada-indent-handle-comment-special
)
4188 (not (looking-at "[ \t]+--")))
4189 (skip-chars-forward " \t"))
4194 ;; -----------------------------------------------------------
4195 ;; -- Behavior Of TAB Key
4196 ;; -----------------------------------------------------------
4199 "Do indenting or tabbing according to `ada-tab-policy'.
4200 In Transient Mark mode, if the mark is active, operate on the contents
4201 of the region. Otherwise, operate only on the current line."
4203 (cond ((eq ada-tab-policy
'indent-rigidly
) (ada-tab-hard))
4204 ((eq ada-tab-policy
'indent-auto
)
4205 (if (ada-region-selected)
4206 (ada-indent-region (region-beginning) (region-end))
4207 (ada-indent-current)))
4208 ((eq ada-tab-policy
'always-tab
) (error "Not implemented"))
4211 (defun ada-untab (arg)
4212 "Delete leading indenting according to `ada-tab-policy'."
4213 ;; FIXME: ARG is ignored
4215 (cond ((eq ada-tab-policy
'indent-rigidly
) (ada-untab-hard))
4216 ((eq ada-tab-policy
'indent-auto
) (error "Not implemented"))
4217 ((eq ada-tab-policy
'always-tab
) (error "Not implemented"))
4220 (defun ada-indent-current-function ()
4221 "Ada mode version of the `indent-line-function'."
4223 (let ((starting-point (point-marker)))
4226 (if (< (point) starting-point
)
4227 (goto-char starting-point
))
4228 (set-marker starting-point nil
)
4231 (defun ada-tab-hard ()
4232 "Indent current line to next tab stop."
4236 (insert-char ? ada-indent
))
4237 (if (save-excursion (= (point) (progn (beginning-of-line) (point))))
4238 (forward-char ada-indent
)))
4240 (defun ada-untab-hard ()
4241 "Indent current line to previous tab stop."
4243 (let ((bol (save-excursion (progn (beginning-of-line) (point))))
4244 (eol (save-excursion (progn (end-of-line) (point)))))
4245 (indent-rigidly bol eol
(- 0 ada-indent
))))
4249 ;; ------------------------------------------------------------
4251 ;; ------------------------------------------------------------
4253 ;; Not needed any more for Emacs 21.2, but still needed for backward
4255 (defun ada-remove-trailing-spaces ()
4256 "Remove trailing spaces in the whole buffer."
4262 (goto-char (point-min))
4263 (while (re-search-forward "[ \t]+$" (point-max) t
)
4264 (replace-match "" nil nil
))))))
4266 (defun ada-gnat-style ()
4267 "Clean up comments, `(' and `,' for GNAT style checking switch."
4271 ;; The \n is required, or the line after an empty comment line is
4273 (goto-char (point-min))
4274 (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t
)
4275 (replace-match "-- \\1")
4277 (beginning-of-line))
4279 (goto-char (point-min))
4280 (while (re-search-forward "\\>(" nil t
)
4281 (if (not (ada-in-string-or-comment-p))
4282 (replace-match " (")))
4283 (goto-char (point-min))
4284 (while (re-search-forward ";--" nil t
)
4286 (if (not (ada-in-string-or-comment-p))
4287 (replace-match "; --")))
4288 (goto-char (point-min))
4289 (while (re-search-forward "([ \t]+" nil t
)
4290 (if (not (ada-in-string-or-comment-p))
4291 (replace-match "(")))
4292 (goto-char (point-min))
4293 (while (re-search-forward ")[ \t]+)" nil t
)
4294 (if (not (ada-in-string-or-comment-p))
4295 (replace-match "))")))
4296 (goto-char (point-min))
4297 (while (re-search-forward "\\>:" nil t
)
4298 (if (not (ada-in-string-or-comment-p))
4299 (replace-match " :")))
4301 ;; Make sure there is a space after a ','.
4302 ;; Always go back to the beginning of the match, since otherwise
4303 ;; a statement like ('F','D','E') is incorrectly modified.
4304 (goto-char (point-min))
4305 (while (re-search-forward ",[ \t]*\\(.\\)" nil t
)
4306 (if (not (save-excursion
4307 (goto-char (match-beginning 0))
4308 (ada-in-string-or-comment-p)))
4309 (replace-match ", \\1")))
4311 ;; Operators should be surrounded by spaces.
4312 (goto-char (point-min))
4313 (while (re-search-forward
4314 "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
4316 (goto-char (match-beginning 1))
4317 (if (or (looking-at "--")
4318 (ada-in-string-or-comment-p))
4321 (beginning-of-line))
4323 ((string= (match-string 1) "/=")
4324 (replace-match " /= "))
4325 ((string= (match-string 1) "..")
4326 (replace-match " .. "))
4327 ((string= (match-string 1) "**")
4328 (replace-match " ** "))
4329 ((string= (match-string 1) ":=")
4330 (replace-match " := "))
4332 (replace-match " \\1 ")))
4338 ;; -------------------------------------------------------------
4339 ;; -- Moving To Procedures/Packages/Statements
4340 ;; -------------------------------------------------------------
4342 (defun ada-move-to-start ()
4343 "Move point to the matching start of the current Ada structure."
4346 (previous-syntax-table (syntax-table)))
4349 (set-syntax-table ada-mode-symbol-syntax-table
)
4353 ;; do nothing if in string or comment or not on 'end ...;'
4354 ;; or if an error occurs during processing
4357 (ada-in-string-or-comment-p)
4359 (or (looking-at "[ \t]*\\<end\\>")
4361 (or (looking-at "[ \t]*\\<end\\>")
4363 (or (looking-at "[ \t]*\\<end\\>")
4364 (error "Not on end ...;")))
4365 (ada-goto-matching-start 1)
4369 ;; on 'begin' => go on, according to user option
4371 ada-move-to-declaration
4372 (looking-at "\\<begin\\>")
4373 (ada-goto-matching-decl-start)
4374 (setq pos
(point))))
4376 ) ; end of save-excursion
4378 ;; now really move to the found position
4381 ;; restore syntax-table
4382 (set-syntax-table previous-syntax-table
))))
4384 (defun ada-move-to-end ()
4385 "Move point to the matching end of the block around point.
4386 Moves to 'begin' if in a declarative part."
4390 (previous-syntax-table (syntax-table)))
4393 (set-syntax-table ada-mode-symbol-syntax-table
)
4398 ;; Go to the beginning of the current word, and check if we are
4399 ;; directly on 'begin'
4401 (skip-syntax-backward "w")
4402 (looking-at "\\<begin\\>"))
4403 (ada-goto-matching-end 1)
4406 ;; on first line of subprogram body
4407 ;; Do nothing for specs or generic instantion, since these are
4408 ;; handled as the general case (find the enclosing block)
4409 ;; We also need to make sure that we ignore nested subprograms
4411 (and (skip-syntax-backward "w")
4412 (looking-at "\\<function\\>\\|\\<procedure\\>" )
4413 (ada-search-ignore-string-comment "is\\|;")
4414 (not (= (char-before) ?\
;))
4416 (skip-syntax-backward "w")
4417 (ada-goto-matching-end 0 t
))
4419 ;; on first line of task declaration
4421 (and (ada-goto-stmt-start)
4422 (looking-at "\\<task\\>" )
4424 (ada-goto-next-non-ws)
4425 (looking-at "\\<body\\>")))
4426 (ada-search-ignore-string-comment "begin" nil nil nil
4427 'word-search-forward
))
4428 ;; accept block start
4430 (and (ada-goto-stmt-start)
4431 (looking-at "\\<accept\\>" )))
4432 (ada-goto-matching-end 0))
4435 (setq decl-start
(and (ada-goto-matching-decl-start t
) (point)))
4436 (and decl-start
(looking-at "\\<package\\>")))
4437 (ada-goto-matching-end 1))
4439 ;; On a "declare" keyword
4441 (skip-syntax-backward "w")
4442 (looking-at "\\<declare\\>"))
4443 (ada-goto-matching-end 0 t
))
4445 ;; inside a 'begin' ... 'end' block
4447 (goto-char decl-start
)
4448 (ada-goto-matching-end 0 t
))
4450 ;; (hopefully ;-) everything else
4452 (ada-goto-matching-end 1)))
4456 ;; now really move to the position found
4459 ;; restore syntax-table
4460 (set-syntax-table previous-syntax-table
))))
4462 (defun ada-next-procedure ()
4463 "Move point to next procedure."
4466 (if (re-search-forward ada-procedure-start-regexp nil t
)
4467 (goto-char (match-beginning 4))
4468 (error "No more functions/procedures/tasks")))
4470 (defun ada-previous-procedure ()
4471 "Move point to previous procedure."
4474 (if (re-search-backward ada-procedure-start-regexp nil t
)
4475 (goto-char (match-beginning 4))
4476 (error "No more functions/procedures/tasks")))
4478 (defun ada-next-package ()
4479 "Move point to next package."
4482 (if (re-search-forward ada-package-start-regexp nil t
)
4483 (goto-char (match-beginning 1))
4484 (error "No more packages")))
4486 (defun ada-previous-package ()
4487 "Move point to previous package."
4490 (if (re-search-backward ada-package-start-regexp nil t
)
4491 (goto-char (match-beginning 1))
4492 (error "No more packages")))
4495 ;; ------------------------------------------------------------
4496 ;; -- Define keymap and menus for Ada
4497 ;; -------------------------------------------------------------
4499 (defun ada-create-keymap ()
4500 "Create the keymap associated with the Ada mode."
4502 ;; All non-standard keys go into ada-mode-extra-map
4503 (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map
)
4505 ;; Indentation and Formatting
4506 (define-key ada-mode-map
"\C-j" 'ada-indent-newline-indent-conditional
)
4507 (define-key ada-mode-map
"\C-m" 'ada-indent-newline-indent-conditional
)
4508 (define-key ada-mode-map
"\t" 'ada-tab
)
4509 (define-key ada-mode-map
"\C-c\t" 'ada-justified-indent-current
)
4510 (define-key ada-mode-map
"\C-c\C-l" 'ada-indent-region
)
4511 (define-key ada-mode-map
[(shift tab
)] 'ada-untab
)
4512 (define-key ada-mode-map
"\C-c\C-f" 'ada-format-paramlist
)
4513 ;; We don't want to make meta-characters case-specific.
4516 (define-key ada-mode-map
"\M-\C-e" 'ada-next-procedure
)
4517 (define-key ada-mode-map
"\M-\C-a" 'ada-previous-procedure
)
4518 (define-key ada-mode-map
"\C-c\C-a" 'ada-move-to-start
)
4519 (define-key ada-mode-map
"\C-c\C-e" 'ada-move-to-end
)
4522 (unless (lookup-key ada-mode-map
"\C-c\C-c")
4523 (define-key ada-mode-map
"\C-c\C-c" 'compile
))
4526 (define-key ada-mode-map
"\C-c\C-b" 'ada-adjust-case-buffer
)
4527 (define-key ada-mode-map
"\C-c\C-t" 'ada-case-read-exceptions
)
4528 (define-key ada-mode-map
"\C-c\C-y" 'ada-create-case-exception
)
4529 (define-key ada-mode-map
"\C-c\C-\M-y" 'ada-create-case-exception-substring
)
4531 ;; On XEmacs, you can easily specify whether DEL should deletes
4532 ;; one character forward or one character backward. Take this into
4534 (define-key ada-mode-map
4535 (if (boundp 'delete-key-deletes-forward
) [backspace] "\177")
4536 'backward-delete-char-untabify)
4539 (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
4541 ;; Use predefined function of Emacs19 for comments (RE)
4542 (define-key ada-mode-map "\C-c;" 'comment-region)
4543 (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
4545 ;; The following keys are bound to functions defined in ada-xref.el or
4546 ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
4547 ;; and activated only if the right compiler is used
4549 (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
4550 'ada-point-and-xref)
4551 (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
4553 (define-key ada-mode-extra-map "o" 'ff-find-other-file)
4554 (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
4555 (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
4556 (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
4557 (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
4558 (define-key ada-mode-extra-map "c" 'ada-change-prj)
4559 (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
4560 (define-key ada-mode-extra-map "g" 'ada-gdb-application)
4561 (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
4562 (define-key ada-mode-extra-map "r" 'ada-run-application)
4563 (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
4564 (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
4565 (define-key ada-mode-extra-map "l" 'ada-find-local-references)
4566 (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
4567 (define-key ada-mode-extra-map "f" 'ada-find-file)
4569 (define-key ada-mode-extra-map "u" 'ada-prj-edit)
4571 ;; The templates, defined in ada-stmt.el
4573 (let ((map (make-sparse-keymap)))
4574 (define-key map "h" 'ada-header)
4575 (define-key map "\C-a" 'ada-array)
4576 (define-key map "b" 'ada-exception-block)
4577 (define-key map "d" 'ada-declare-block)
4578 (define-key map "c" 'ada-case)
4579 (define-key map "\C-e" 'ada-elsif)
4580 (define-key map "e" 'ada-else)
4581 (define-key map "\C-k" 'ada-package-spec)
4582 (define-key map "k" 'ada-package-body)
4583 (define-key map "\C-p" 'ada-procedure-spec)
4584 (define-key map "p" 'ada-subprogram-body)
4585 (define-key map "\C-f" 'ada-function-spec)
4586 (define-key map "f" 'ada-for-loop)
4587 (define-key map "i" 'ada-if)
4588 (define-key map "l" 'ada-loop)
4589 (define-key map "\C-r" 'ada-record)
4590 (define-key map "\C-s" 'ada-subtype)
4591 (define-key map "S" 'ada-tabsize)
4592 (define-key map "\C-t" 'ada-task-spec)
4593 (define-key map "t" 'ada-task-body)
4594 (define-key map "\C-y" 'ada-type)
4595 (define-key map "\C-v" 'ada-private)
4596 (define-key map "u" 'ada-use)
4597 (define-key map "\C-u" 'ada-with)
4598 (define-key map "\C-w" 'ada-when)
4599 (define-key map "w" 'ada-while-loop)
4600 (define-key map "\C-x" 'ada-exception)
4601 (define-key map "x" 'ada-exit)
4602 (define-key ada-mode-extra-map "t" map))
4606 (defun ada-create-menu ()
4607 "Create the Ada menu as shown in the menu bar."
4610 ["Ada Mode" (info "ada-mode") t]
4611 ["GNAT User's Guide" (info "gnat_ugn")
4612 (eq ada-which-compiler 'gnat)]
4613 ["GNAT Reference Manual" (info "gnat_rm")
4614 (eq ada-which-compiler 'gnat)]
4615 ["Gcc Documentation" (info "gcc")
4616 (eq ada-which-compiler 'gnat)]
4617 ["Gdb Documentation" (info "gdb")
4618 (eq ada-which-compiler 'gnat)]
4619 ["Ada95 Reference Manual" (info "arm95") t])
4620 ("Options" :included (eq major-mode 'ada-mode)
4621 ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
4622 :style toggle :selected ada-auto-case]
4623 ["Auto Indent After Return"
4624 (setq ada-indent-after-return (not ada-indent-after-return))
4625 :style toggle :selected ada-indent-after-return]
4626 ["Automatically Recompile For Cross-references"
4627 (setq ada-xref-create-ali (not ada-xref-create-ali))
4628 :style toggle :selected ada-xref-create-ali
4629 :included (eq ada-which-compiler 'gnat)]
4631 (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
4632 :style toggle :selected ada-xref-confirm-compile
4633 :included (eq ada-which-compiler 'gnat)]
4634 ["Show Cross-references In Other Buffer"
4635 (setq ada-xref-other-buffer (not ada-xref-other-buffer))
4636 :style toggle :selected ada-xref-other-buffer
4637 :included (eq ada-which-compiler 'gnat)]
4638 ["Tight Integration With GNU Visual Debugger"
4639 (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
4640 :style toggle :selected ada-tight-gvd-integration
4641 :included (string-match "gvd" ada-prj-default-debugger)])
4642 ["Customize" (customize-group 'ada)
4643 :included (fboundp 'customize-group)]
4644 ["Check file" ada-check-current t]
4645 ["Compile file" ada-compile-current t]
4646 ["Set main and Build" ada-set-main-compile-application t]
4647 ["Show main" ada-show-current-main t]
4648 ["Build" ada-compile-application t]
4649 ["Run" ada-run-application t]
4650 ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
4653 ["Show project" ada-show-current-project t]
4654 ["Load..." ada-set-default-project-file t]
4655 ["New..." ada-prj-new t]
4656 ["Edit..." ada-prj-edit t])
4657 ("Goto" :included (eq major-mode 'ada-mode)
4658 ["Goto Declaration/Body" ada-goto-declaration
4659 (eq ada-which-compiler 'gnat)]
4660 ["Goto Body" ada-goto-body
4661 (eq ada-which-compiler 'gnat)]
4662 ["Goto Declaration Other Frame"
4663 ada-goto-declaration-other-frame
4664 (eq ada-which-compiler 'gnat)]
4665 ["Goto Previous Reference" ada-xref-goto-previous-reference
4666 (eq ada-which-compiler 'gnat)]
4667 ["List Local References" ada-find-local-references
4668 (eq ada-which-compiler 'gnat)]
4669 ["List References" ada-find-references
4670 (eq ada-which-compiler 'gnat)]
4671 ["Goto Reference To Any Entity" ada-find-any-references
4672 (eq ada-which-compiler 'gnat)]
4673 ["Goto Parent Unit" ada-goto-parent
4674 (eq ada-which-compiler 'gnat)]
4676 ["Next compilation error" next-error t]
4677 ["Previous Package" ada-previous-package t]
4678 ["Next Package" ada-next-package t]
4679 ["Previous Procedure" ada-previous-procedure t]
4680 ["Next Procedure" ada-next-procedure t]
4681 ["Goto Start Of Statement" ada-move-to-start t]
4682 ["Goto End Of Statement" ada-move-to-end t]
4684 ["Other File" ff-find-other-file t]
4685 ["Other File Other Window" ada-ff-other-window t])
4686 ("Edit" :included (eq major-mode 'ada-mode)
4687 ["Search File On Source Path" ada-find-file t]
4689 ["Complete Identifier" ada-complete-identifier t]
4691 ["Indent Line" ada-indent-current-function t]
4692 ["Justify Current Indentation" ada-justified-indent-current t]
4693 ["Indent Lines in Selection" ada-indent-region t]
4694 ["Indent Lines in File"
4695 (ada-indent-region (point-min) (point-max)) t]
4696 ["Format Parameter List" ada-format-paramlist t]
4698 ["Comment Selection" comment-region t]
4699 ["Uncomment Selection" ada-uncomment-region t]
4701 ["Fill Comment Paragraph" fill-paragraph t]
4702 ["Fill Comment Paragraph Justify"
4703 ada-fill-comment-paragraph-justify t]
4704 ["Fill Comment Paragraph Postfix"
4705 ada-fill-comment-paragraph-postfix t]
4707 ["Adjust Case Selection" ada-adjust-case-region t]
4708 ["Adjust Case in File" ada-adjust-case-buffer t]
4709 ["Create Case Exception" ada-create-case-exception t]
4710 ["Create Case Exception Substring"
4711 ada-create-case-exception-substring t]
4712 ["Reload Case Exceptions" ada-case-read-exceptions t]
4714 ["Make body for subprogram" ada-make-subprogram-body t]
4716 ["Narrow to subprogram" ada-narrow-to-defun t])
4718 :included (eq major-mode 'ada-mode)
4719 ["Header" ada-header t]
4721 ["Package Body" ada-package-body t]
4722 ["Package Spec" ada-package-spec t]
4723 ["Function Spec" ada-function-spec t]
4724 ["Procedure Spec" ada-procedure-spec t]
4725 ["Proc/func Body" ada-subprogram-body t]
4726 ["Task Body" ada-task-body t]
4727 ["Task Spec" ada-task-spec t]
4728 ["Declare Block" ada-declare-block t]
4729 ["Exception Block" ada-exception-block t]
4731 ["Entry" ada-entry t]
4732 ["Entry family" ada-entry-family t]
4733 ["Select" ada-select t]
4734 ["Accept" ada-accept t]
4735 ["Or accept" ada-or-accep t]
4736 ["Or delay" ada-or-delay t]
4737 ["Or terminate" ada-or-terminate t]
4740 ["Private" ada-private t]
4741 ["Subtype" ada-subtype t]
4742 ["Record" ada-record t]
4743 ["Array" ada-array t]
4747 ["Elsif" ada-elsif t]
4750 ["While Loop" ada-while-loop t]
4751 ["For Loop" ada-for-loop t]
4754 ["Exception" ada-exception t]
4756 ["When" ada-when t])
4759 (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
4760 (if (featurep 'xemacs)
4762 (define-key ada-mode-map [menu-bar] ada-mode-menu)
4763 (set 'mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
4766 ;; -------------------------------------------------------
4767 ;; Commenting/Uncommenting code
4768 ;; The following two calls are provided to enhance the standard
4769 ;; comment-region function, which only allows uncommenting if the
4770 ;; comment is at the beginning of a line. If the line have been re-indented,
4771 ;; we are unable to use comment-region, which makes no sense.
4773 ;; In addition, we provide an interface to the standard comment handling
4774 ;; function for justifying the comments.
4775 ;; -------------------------------------------------------
4777 (defadvice comment-region (before ada-uncomment-anywhere disable)
4778 (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
4779 ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
4780 (derived-mode-p 'ada-mode))
4782 (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
4784 (while (re-search-forward cs end t)
4785 (replace-match comment-start))
4788 (defun ada-uncomment-region (beg end &optional arg)
4789 "Uncomment region BEG .. END.
4790 ARG gives number of comment characters."
4791 (interactive "r\nP")
4793 ;; This advice is not needed anymore with Emacs21. However, for older
4794 ;; versions, as well as for XEmacs, we still need to enable it.
4795 (if (or (<= emacs-major-version 20) (featurep 'xemacs))
4797 (ad-activate 'comment-region)
4798 (comment-region beg end (- (or arg 2)))
4799 (ad-deactivate 'comment-region))
4800 (comment-region beg end (list (- (or arg 2))))
4801 (ada-indent-region beg end)))
4803 (defun ada-fill-comment-paragraph-justify ()
4804 "Fill current comment paragraph and justify each line as well."
4806 (ada-fill-comment-paragraph 'full))
4808 (defun ada-fill-comment-paragraph-postfix ()
4809 "Fill current comment paragraph and justify each line as well.
4810 Adds `ada-fill-comment-postfix' at the end of each line."
4812 (ada-fill-comment-paragraph 'full t))
4814 (defun ada-fill-comment-paragraph (&optional justify postfix)
4815 "Fill the current comment paragraph.
4816 If JUSTIFY is non-nil, each line is justified as well.
4817 If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
4818 to each line filled and justified.
4819 The paragraph is indented on the first line."
4822 ;; check if inside comment or just in front a comment
4823 (if (and (not (ada-in-comment-p))
4824 (not (looking-at "[ \t]*--")))
4825 (error "Not inside comment"))
4827 (let* (indent from to
4828 (opos (point-marker))
4830 ;; Sets this variable to nil, otherwise it prevents
4831 ;; fill-region-as-paragraph to work on Emacs <= 20.2
4832 (parse-sexp-lookup-properties nil)
4835 (fill-column (current-fill-column)))
4837 ;; Find end of paragraph
4838 (back-to-indentation)
4839 (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4842 ;; If we were at the last line in the buffer, create a dummy empty
4843 ;; line at the end of the buffer.
4846 (back-to-indentation)))
4848 (setq to (point-marker))
4851 ;; Find beginning of paragraph
4852 (back-to-indentation)
4853 (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
4855 (back-to-indentation))
4857 ;; We want one line above the first one, unless we are at the beginning
4862 (setq from (point-marker))
4864 ;; Calculate the indentation we will need for the paragraph
4865 (back-to-indentation)
4866 (setq indent (current-column))
4867 ;; unindent the first line of the paragraph
4868 (delete-region from (point))
4870 ;; Remove the old postfixes
4872 (while (re-search-forward "--\n" to t)
4873 (replace-match "\n"))
4876 (setq to (point-marker))
4878 ;; Indent and justify the paragraph
4879 (setq fill-prefix ada-fill-comment-prefix)
4880 (set-left-margin from to indent)
4882 (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
4884 (fill-region-as-paragraph from to justify)
4886 ;; Add the postfixes if required
4890 (narrow-to-region from to)
4893 (insert-char ? (- fill-column (current-column)))
4894 (insert ada-fill-comment-postfix)
4898 ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
4899 ;; inserted at the end. Delete it
4900 (if (or (featurep 'xemacs)
4901 (<= emacs-major-version 19)
4902 (and (= emacs-major-version 20)
4903 (<= emacs-minor-version 2)))
4912 ;; ---------------------------------------------------
4913 ;; support for find-file.el
4914 ;; These functions are used by find-file to guess the file names from
4915 ;; unit names, and to find the other file (spec or body) from the current
4916 ;; file (body or spec).
4917 ;; It is also used to find in which function we are, so as to put the
4918 ;; cursor at the correct position.
4919 ;; Standard Ada does not force any relation between unit names and file names,
4920 ;; so some of these functions can only be a good approximation. However, they
4921 ;; are also overridden in `ada-xref'.el when we know that the user is using
4923 ;; ---------------------------------------------------
4925 ;; Overridden when we work with GNAT, to use gnatkrunch
4926 (defun ada-make-filename-from-adaname (adaname)
4927 "Determine the filename in which ADANAME is found.
4928 This matches the GNAT default naming convention, except for
4930 (while (string-match "\\." adaname)
4931 (setq adaname (replace-match "-" t t adaname)))
4935 (defun ada-other-file-name ()
4936 "Return the name of the other file.
4937 The name returned is the body if `current-buffer' is the spec,
4938 or the spec otherwise."
4942 (suffixes ada-spec-suffixes)
4943 (name (buffer-file-name)))
4945 ;; Guess whether we have a spec or a body, and get the basename of the
4946 ;; file. Since the extension may not start with '.', we can not use
4947 ;; file-name-extension
4948 (while (and (not is-spec)
4950 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4952 name (match-string 1 name)))
4953 (setq suffixes (cdr suffixes)))
4957 (setq suffixes ada-body-suffixes)
4958 (while (and (not is-body)
4960 (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
4962 name (match-string 1 name)))
4963 (setq suffixes (cdr suffixes)))))
4965 ;; If this wasn't in either list, return name itself
4966 (if (not (or is-spec is-body))
4969 ;; Else find the other possible names
4971 (setq suffixes ada-body-suffixes)
4972 (setq suffixes ada-spec-suffixes))
4977 ;; If we are using project file, search for the other file in all
4978 ;; the possible src directories.
4980 (if (fboundp 'ada-find-src-file-in-dir)
4982 (ada-find-src-file-in-dir
4983 (file-name-nondirectory (concat name (car suffixes))))))
4985 (set 'is-spec other)))
4987 ;; Else search in the current directory
4988 (if (file-exists-p (concat name (car suffixes)))
4989 (setq is-spec (concat name (car suffixes)))))
4990 (setq suffixes (cdr suffixes)))
4994 (defun ada-which-function-are-we-in ()
4995 "Return the name of the function whose definition/declaration point is in.
4996 Used in `ff-pre-load-hook'."
4997 (setq ff-function-name nil)
4999 (end-of-line);; make sure we get the complete name
5000 (or (if (re-search-backward ada-procedure-start-regexp nil t)
5001 (setq ff-function-name (match-string 5)))
5002 (if (re-search-backward ada-package-start-regexp nil t)
5003 (setq ff-function-name (match-string 4))))
5007 (defvar ada-last-which-function-line -1
5008 "Last line on which `ada-which-function' was called.")
5009 (defvar ada-last-which-function-subprog 0
5010 "Last subprogram name returned by `ada-which-function'.")
5011 (make-variable-buffer-local 'ada-last-which-function-subprog)
5012 (make-variable-buffer-local 'ada-last-which-function-line)
5015 (defun ada-which-function ()
5016 "Return the name of the function whose body the point is in.
5017 This function works even in the case of nested subprograms, whereas the
5018 standard Emacs function `which-function' does not.
5019 Since the search can be long, the results are cached."
5021 (let ((line (count-lines 1 (point)))
5027 ;; If this is the same line as before, simply return the same result
5028 (if (= line ada-last-which-function-line)
5029 ada-last-which-function-subprog
5032 ;; In case the current line is also the beginning of the body
5035 ;; Are we looking at "function Foo\n (paramlist)"
5036 (skip-chars-forward " \t\n(")
5042 (skip-chars-forward " \t\n")
5043 (if (looking-at "return")
5046 (skip-chars-forward " \t\n")
5047 (skip-chars-forward "a-zA-Z0-9_'")))
5049 ;; Can't simply do forward-word, in case the "is" is not on the
5050 ;; same line as the closing parenthesis
5051 (skip-chars-forward "is \t\n")
5053 ;; No look for the closest subprogram body that has not ended yet.
5054 ;; Not that we expect all the bodies to be finished by "end <name>",
5055 ;; or a simple "end;" indented in the same column as the start of
5056 ;; the subprogram. The goal is to be as efficient as possible.
5058 (while (and (not found)
5059 (re-search-backward ada-imenu-subprogram-menu-re nil t))
5061 ;; Get the function name, but not the properties, or this changes
5062 ;; the face in the modeline on Emacs 21
5063 (setq func-name (match-string-no-properties 2))
5064 (if (and (not (ada-in-comment-p))
5065 (not (save-excursion
5066 (goto-char (match-end 0))
5067 (looking-at "[ \t\n]*new"))))
5069 (back-to-indentation)
5070 (setq indent (current-column))
5071 (if (ada-search-ignore-string-comment
5072 (concat "end[ \t]+" func-name "[ \t]*;\\|^"
5073 (make-string indent ? ) "end;"))
5074 (setq end-pos (point))
5075 (setq end-pos (point-max)))
5076 (if (>= end-pos pos)
5077 (setq found func-name))))
5079 (setq ada-last-which-function-line line
5080 ada-last-which-function-subprog found)
5083 (defun ada-ff-other-window ()
5084 "Find other file in other window using `ff-find-other-file'."
5086 (and (fboundp 'ff-find-other-file)
5087 (ff-find-other-file t)))
5089 (defun ada-set-point-accordingly ()
5090 "Move to the function declaration that was set by `ff-which-function-are-we-in'."
5091 (if ff-function-name
5093 (goto-char (point-min))
5094 (unless (ada-search-ignore-string-comment
5095 (concat ff-function-name "\\b") nil)
5096 (goto-char (point-min))))))
5098 (defun ada-get-body-name (&optional spec-name)
5099 "Return the file name for the body of SPEC-NAME.
5100 If SPEC-NAME is nil, return the body for the current package.
5101 Return nil if no body was found."
5104 (unless spec-name (setq spec-name (buffer-file-name)))
5106 ;; Remove the spec extension. We can not simply remove the file extension,
5107 ;; but we need to take into account the specific non-GNAT extensions that the
5108 ;; user might have specified.
5110 (let ((suffixes ada-spec-suffixes)
5113 (setq end (- (length spec-name) (length (car suffixes))))
5114 (if (string-equal (car suffixes) (substring spec-name end))
5115 (setq spec-name (substring spec-name 0 end)))
5116 (setq suffixes (cdr suffixes))))
5118 ;; If find-file.el was available, use its functions
5119 (if (fboundp 'ff-get-file-name)
5120 (ff-get-file-name ada-search-directories-internal
5121 (ada-make-filename-from-adaname
5122 (file-name-nondirectory
5123 (file-name-sans-extension spec-name)))
5125 ;; Else emulate it very simply
5126 (concat (ada-make-filename-from-adaname
5127 (file-name-nondirectory
5128 (file-name-sans-extension spec-name)))
5132 ;; ---------------------------------------------------
5133 ;; support for font-lock.el
5134 ;; Strings are a real pain in Ada because a single quote character is
5135 ;; overloaded as a string quote and type/instance delimiter. By default, a
5136 ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
5137 ;; So, for Font Lock mode purposes, we mark single quotes as having string
5138 ;; syntax when the gods that created Ada determine them to be.
5140 ;; This only works in Emacs. See the comments before the grammar functions
5141 ;; at the beginning of this file for how this is done with XEmacs.
5142 ;; ----------------------------------------------------
5144 (defconst ada-font-lock-syntactic-keywords
5145 ;; Mark single quotes as having string quote syntax in 'c' instances.
5146 ;; We used to explicitly avoid ''' as a special case for fear the buffer
5147 ;; be highlighted as a string, but it seems this fear is unfounded.
5149 ;; This sets the properties of the characters, so that ada-in-string-p
5150 ;; correctly handles '"' too...
5151 '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
5152 ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
5154 (defvar ada-font-lock-keywords
5158 ;; handle "type T is access function return S;"
5159 (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
5161 ;; preprocessor line
5162 (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
5165 ;; accept, entry, function, package (body), protected (body|type),
5166 ;; pragma, procedure, task (body) plus name.
5172 "package[ \t]+body\\|"
5176 "protected[ \t]+body\\|"
5177 "protected[ \t]+type\\|"
5183 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5184 '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
5186 ;; Optional keywords followed by a type name.
5187 (list (concat ; ":[ \t]*"
5188 "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
5190 "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
5191 '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
5194 ;; Main keywords, except those treated specially below.
5197 '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
5198 "and" "array" "at" "begin" "case" "declare" "delay" "delta"
5199 "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
5200 "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
5201 "null" "or" "others" "overriding" "private" "protected" "raise"
5202 "range" "record" "rem" "renames" "requeue" "return" "reverse"
5203 "select" "separate" "synchronized" "tagged" "task" "terminate"
5204 "then" "until" "when" "while" "with" "xor") t)
5207 ;; Anything following end and not already fontified is a body name.
5208 '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
5209 (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
5211 ;; Keywords followed by a type or function name.
5212 (list (concat "\\<\\("
5213 "new\\|of\\|subtype\\|type"
5214 "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
5215 '(1 font-lock-keyword-face)
5216 '(2 (if (match-beginning 4)
5217 font-lock-function-name-face
5218 font-lock-type-face) nil t))
5220 ;; Keywords followed by a (comma separated list of) reference.
5221 ;; Note that font-lock only works on single lines, thus we can not
5222 ;; correctly highlight a with_clause that spans multiple lines.
5223 (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
5224 "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
5225 '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
5229 '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
5231 ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
5232 (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
5234 ;; Ada unnamed numerical constants
5235 (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
5238 "Default expressions to highlight in Ada mode.")
5241 ;; ---------------------------------------------------------
5242 ;; Support for outline.el
5243 ;; ---------------------------------------------------------
5245 (defun ada-outline-level ()
5246 "This is so that `current-column' DTRT in otherwise-hidden text."
5247 ;; patch from Dave Love <fx@gnu.org>
5248 (let (buffer-invisibility-spec)
5250 (back-to-indentation)
5253 ;; ---------------------------------------------------------
5254 ;; Support for narrow-to-region
5255 ;; ---------------------------------------------------------
5257 (defun ada-narrow-to-defun (&optional arg)
5258 "Make text outside current subprogram invisible.
5259 The subprogram visible is the one that contains or follow point.
5260 Optional ARG is ignored.
5261 Use \\[widen] to go back to the full visibility for the buffer."
5268 (ada-previous-procedure)
5276 (narrow-to-region end (point))
5278 "Use M-x widen to get back to full visibility in the buffer"))))
5280 ;; ---------------------------------------------------------
5281 ;; Automatic generation of code
5282 ;; The Ada mode has a set of function to automatically generate a subprogram
5283 ;; or package body from its spec.
5284 ;; These function only use a primary and basic algorithm, this could use a
5285 ;; lot of improvement.
5286 ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
5288 ;; ----------------------------------------------------------
5290 (defun ada-gen-treat-proc (match)
5291 "Make dummy body of a procedure/function specification.
5292 MATCH is a cons cell containing the start and end locations of the last search
5293 for `ada-procedure-start-regexp'."
5294 (goto-char (car match))
5295 (let (func-found procname functype)
5297 ((or (looking-at "^[ \t]*procedure")
5298 (setq func-found (looking-at "^[ \t]*function")))
5299 ;; treat it as a proc/func
5302 (setq procname (buffer-substring (point) (cdr match))) ; store proc name
5304 ;; goto end of procname
5305 (goto-char (cdr match))
5307 ;; skip over parameterlist
5308 (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
5311 ;; if function, skip over 'return' and result type.
5315 (skip-chars-forward " \t\n")
5316 (setq functype (buffer-substring (point)
5321 ;; look for next non WS
5323 ((looking-at "[ \t]*;")
5324 (delete-region (match-beginning 0) (match-end 0));; delete the ';'
5325 (ada-indent-newline-indent)
5327 (ada-indent-newline-indent)
5330 (insert "Result : " functype ";")
5331 (ada-indent-newline-indent)))
5333 (ada-indent-newline-indent)
5335 (insert "return Result;")
5337 (ada-indent-newline-indent)
5338 (insert "end " procname ";")
5339 (ada-indent-newline-indent)
5342 ((looking-at "[ \t\n]*is")
5346 ((looking-at "[ \t\n]*rename")
5351 (message "unknown syntax"))))
5353 (if (looking-at "^[ \t]*task")
5355 (message "Task conversion is not yet implemented")
5357 (if (looking-at "[ \t]*;")
5362 (defun ada-make-body ()
5363 "Create an Ada package body in the current buffer.
5364 The spec must be the previously visited buffer.
5365 This function typically is to be hooked into `ff-file-created-hook'."
5366 (delete-region (point-min) (point-max))
5367 (insert-buffer-substring (car (cdr (buffer-list))))
5368 (goto-char (point-min))
5371 (let (found ada-procedure-or-package-start-regexp)
5373 (ada-search-ignore-string-comment ada-package-start-regexp nil))
5374 (progn (goto-char (cdr found))
5377 (error "No package"))
5379 (setq ada-procedure-or-package-start-regexp
5380 (concat ada-procedure-start-regexp
5382 ada-package-start-regexp))
5385 (ada-search-ignore-string-comment
5386 ada-procedure-or-package-start-regexp nil))
5388 (goto-char (car found))
5389 (if (looking-at ada-package-start-regexp)
5390 (progn (goto-char (cdr found))
5392 (ada-gen-treat-proc found))))))
5395 (defun ada-make-subprogram-body ()
5396 "Create a dummy subprogram body in package body file from spec surrounding point."
5398 (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
5399 (spec (match-beginning 0))
5404 (if (and (re-search-forward "(\\|;" nil t)
5405 (= (char-before) ?\())
5407 (ada-search-ignore-string-comment ")" nil)
5408 (ada-search-ignore-string-comment ";" nil)))
5409 (setq spec (buffer-substring spec (point)))
5411 ;; If find-file.el was available, use its functions
5412 (setq body-file (ada-get-body-name))
5414 (find-file body-file)
5415 (error "No body found for the package. Create it first"))
5419 (goto-char (point-max))
5420 (forward-comment -10000)
5421 (re-search-backward "\\<end\\>" nil t)
5422 ;; Move to the beginning of the elaboration part, if any
5423 (re-search-backward "^begin" nil t)
5427 (re-search-backward ada-procedure-start-regexp nil t)
5428 (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
5430 (error "Not in subprogram spec"))))
5432 ;; --------------------------------------------------------
5433 ;; Global initializations
5434 ;; --------------------------------------------------------
5436 ;; Create the keymap once and for all. If we do that in ada-mode,
5437 ;; the keys changed in the user's .emacs have to be modified
5442 ;; Create the syntax tables, but do not activate them
5443 (ada-create-syntax-table)
5445 ;; Add the default extensions (and set up speedbar)
5446 (ada-add-extensions ".ads" ".adb")
5447 ;; This two files are generated by GNAT when running with -gnatD
5448 (if (equal ada-which-compiler 'gnat)
5449 (ada-add-extensions ".ads.dg" ".adb.dg"))
5451 ;; Read the special cases for exceptions
5452 (ada-case-read-exceptions)
5454 ;; Setup auto-loading of the other Ada mode files.
5455 (autoload 'ada-change-prj "ada-xref" nil t)
5456 (autoload 'ada-check-current "ada-xref" nil t)
5457 (autoload 'ada-compile-application "ada-xref" nil t)
5458 (autoload 'ada-compile-current "ada-xref" nil t)
5459 (autoload 'ada-complete-identifier "ada-xref" nil t)
5460 (autoload 'ada-find-file "ada-xref" nil t)
5461 (autoload 'ada-find-any-references "ada-xref" nil t)
5462 (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
5463 (autoload 'ada-find-local-references "ada-xref" nil t)
5464 (autoload 'ada-find-references "ada-xref" nil t)
5465 (autoload 'ada-gdb-application "ada-xref" nil t)
5466 (autoload 'ada-goto-declaration "ada-xref" nil t)
5467 (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
5468 (autoload 'ada-goto-parent "ada-xref" nil t)
5469 (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
5470 (autoload 'ada-point-and-xref "ada-xref" nil t)
5471 (autoload 'ada-reread-prj-file "ada-xref" nil t)
5472 (autoload 'ada-run-application "ada-xref" nil t)
5473 (autoload 'ada-set-default-project-file "ada-xref" nil t)
5474 (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
5475 (autoload 'ada-set-main-compile-application "ada-xref" nil t)
5476 (autoload 'ada-show-current-main "ada-xref" nil t)
5478 (autoload 'ada-customize "ada-prj" nil t)
5479 (autoload 'ada-prj-edit "ada-prj" nil t)
5480 (autoload 'ada-prj-new "ada-prj" nil t)
5481 (autoload 'ada-prj-save "ada-prj" nil t)
5483 (autoload 'ada-array "ada-stmt" nil t)
5484 (autoload 'ada-case "ada-stmt" nil t)
5485 (autoload 'ada-declare-block "ada-stmt" nil t)
5486 (autoload 'ada-else "ada-stmt" nil t)
5487 (autoload 'ada-elsif "ada-stmt" nil t)
5488 (autoload 'ada-exception "ada-stmt" nil t)
5489 (autoload 'ada-exception-block "ada-stmt" nil t)
5490 (autoload 'ada-exit "ada-stmt" nil t)
5491 (autoload 'ada-for-loop "ada-stmt" nil t)
5492 (autoload 'ada-function-spec "ada-stmt" nil t)
5493 (autoload 'ada-header "ada-stmt" nil t)
5494 (autoload 'ada-if "ada-stmt" nil t)
5495 (autoload 'ada-loop "ada-stmt" nil t)
5496 (autoload 'ada-package-body "ada-stmt" nil t)
5497 (autoload 'ada-package-spec "ada-stmt" nil t)
5498 (autoload 'ada-private "ada-stmt" nil t)
5499 (autoload 'ada-procedure-spec "ada-stmt" nil t)
5500 (autoload 'ada-record "ada-stmt" nil t)
5501 (autoload 'ada-subprogram-body "ada-stmt" nil t)
5502 (autoload 'ada-subtype "ada-stmt" nil t)
5503 (autoload 'ada-tabsize "ada-stmt" nil t)
5504 (autoload 'ada-task-body "ada-stmt" nil t)
5505 (autoload 'ada-task-spec "ada-stmt" nil t)
5506 (autoload 'ada-type "ada-stmt" nil t)
5507 (autoload 'ada-use "ada-stmt" nil t)
5508 (autoload 'ada-when "ada-stmt" nil t)
5509 (autoload 'ada-while-loop "ada-stmt" nil t)
5510 (autoload 'ada-with "ada-stmt" nil t)
5512 ;;; provide ourselves
5515 ;; arch-tag: 1b7d45ec-1698-43b5-8d4a-e479ea023270
5516 ;;; ada-mode.el ends here