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