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