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