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