(POP_FAILURE_REG_OR_COUNT, re_match_2_internal)
[emacs.git] / lisp / progmodes / ada-stmt.el
blob7d09c68a10ef2c741f3589f372efc32aad03960c
1 ;;; ada-stmt.el --- An extension to Ada mode for inserting statement templates.
3 ;; Copyright(C) 1987, 1993, 1994, 1996, 1997, 1998, 1999
4 ;; Free Software Foundation, Inc.
6 ;; Ada Core Technologies's version: $Revision: 1.6 $
8 ;; Authors: Daniel Pfeiffer, Markus Heritsch, Rolf Ebert <ebert@waporo.muc.de>
9 ;; Maintainer: Rolf Ebert <ebert@waporo.muc.de>
10 ;; Keywords: languages, ada
11 ;; Rolf Ebert's version: 2.26
13 ;;; Commentary:
16 ;; put the following statement in your .emacs:
17 ;; (require 'ada-stmt)
20 ;;; History:
22 ;; Created May 1987.
23 ;; Original version from V. Bowman as in ada.el of Emacs-18
24 ;; (borrowed heavily from Mick Jordan's Modula-2 package for GNU,
25 ;; as modified by Peter Robinson, Michael Schmidt, and Tom Perrine.)
27 ;; Sep 1993. Daniel Pfeiffer <pfeiffer@cict.fr> (DP)
28 ;; Introduced statement.el for smaller code and user configurability.
30 ;; Nov 1993. Rolf Ebert <ebert@enpc.fr> (RE) Moved the
31 ;; skeleton generation into this separate file. The code still is
32 ;; essentially written by DP
33 ;;
34 ;; Adapted Jun 1994. Markus Heritsch
35 ;; <Markus.Heritsch@studbox.uni-stuttgart.de> (MH)
36 ;; added menu bar support for templates
38 ;; 1994/12/02 Christian Egli <cegli@hcsd.hac.com>
39 ;; General cleanup and bug fixes.
41 ;; 1995/12/20 John Hutchison <hutchiso@epi.syr.ge.com>
42 ;; made it work with skeleton.el from Emacs-19.30. Several
43 ;; enhancements and bug fixes.
45 ;; BUGS:
46 ;;;> I have the following suggestions for the function template: 1) I
47 ;;;> don't want it automatically assigning it a name for the return variable. I
48 ;;;> never want it to be called "Result" because that is nondescriptive. If you
49 ;;;> must define a variable, give me the ability to specify its name.
50 ;;;>
51 ;;;> 2) You do not provide a type for variable 'Result'. Its type is the same
52 ;;;> as the function's return type, which the template knows, so why force me
53 ;;;> to type it in?
54 ;;;>
56 ;;;It would be nice if one could configure such layout details separately
57 ;;;without patching the LISP code. Maybe the metalanguage used in ada-stmt.el
58 ;;;could be taken even further, providing the user with some nice syntax
59 ;;;for describing layout. Then my own hacks would survive the next
60 ;;;update of the package :-)
63 ;;; Code:
65 (eval-when-compile
66 (condition-case nil (require 'skeleton)
67 (error nil)))
69 (require 'easymenu)
71 (defun ada-stmt-add-to-ada-menu ()
72 "Add a new submenu to the Ada menu"
73 (interactive)
74 (let ((menu '(["Header" ada-header t]
75 ["-" nil nil]
76 ["Package Body" ada-package-body t]
77 ["Package Spec" ada-package-spec t]
78 ["Function Spec" ada-function-spec t]
79 ["Procedure Spec" ada-procedure-spec t]
80 ["Proc/func Body" ada-subprogram-body t]
81 ["Task Body" ada-task-body t]
82 ["Task Spec" ada-task-spec t]
83 ["Declare Block" ada-declare-block t]
84 ["Exception Block" ada-exception-block t]
85 ["--" nil nil]
86 ["Entry" ada-entry t]
87 ["Entry family" ada-entry-family t]
88 ["Select" ada-select t]
89 ["Accept" ada-accept t]
90 ["Or accept" ada-or-accep t]
91 ["Or delay" ada-or-delay t]
92 ["Or terminate" ada-or-terminate t]
93 ["---" nil nil]
94 ["Type" ada-type t]
95 ["Private" ada-private t]
96 ["Subtype" ada-subtype t]
97 ["Record" ada-record t]
98 ["Array" ada-array t]
99 ["----" nil nil]
100 ["If" ada-if t]
101 ["Else" ada-else t]
102 ["Elsif" ada-elsif t]
103 ["Case" ada-case t]
104 ["-----" nil nil]
105 ["While Loop" ada-while-loop t]
106 ["For Loop" ada-for-loop t]
107 ["Loop" ada-loop t]
108 ["------" nil nil]
109 ["Exception" ada-exception t]
110 ["Exit" ada-exit t]
111 ["When" ada-when t])))
112 (if ada-xemacs
113 (funcall (symbol-function 'add-submenu)
114 '("Ada") (append (list "Statements"
115 :included '(string= mode-name "Ada"))
116 menu))
118 (define-key-after (lookup-key ada-mode-map [menu-bar Ada]) [Statements]
119 (list 'menu-item
120 "Statements"
121 (easy-menu-create-menu "Statements" menu)
122 :visible '(string= mode-name "Ada"))
129 (defun ada-func-or-proc-name ()
130 ;; Get the name of the current function or procedure."
131 (save-excursion
132 (let ((case-fold-search t))
133 (if (re-search-backward ada-procedure-start-regexp nil t)
134 (buffer-substring (match-beginning 2) (match-end 2))
135 "NAME?"))))
137 (defvar ada-template-map nil
138 "Keymap used in Ada mode for smart template operations.")
140 (define-key ada-mode-map "\C-cth" 'ada-header)
141 (define-key ada-mode-map "\C-ct\C-a" 'ada-array)
142 (define-key ada-mode-map "\C-ctb" 'ada-exception-block)
143 (define-key ada-mode-map "\C-ctd" 'ada-declare-block)
144 (define-key ada-mode-map "\C-ctc" 'ada-case)
145 (define-key ada-mode-map "\C-ct\C-e" 'ada-elsif)
146 (define-key ada-mode-map "\C-cte" 'ada-else)
147 (define-key ada-mode-map "\C-ct\C-k" 'ada-package-spec)
148 (define-key ada-mode-map "\C-ctk" 'ada-package-body)
149 (define-key ada-mode-map "\C-ct\C-p" 'ada-procedure-spec)
150 (define-key ada-mode-map "\C-ctp" 'ada-subprogram-body)
151 (define-key ada-mode-map "\C-ct\C-f" 'ada-function-spec)
152 (define-key ada-mode-map "\C-ctf" 'ada-for-loop)
153 (define-key ada-mode-map "\C-cti" 'ada-if)
154 (define-key ada-mode-map "\C-ctl" 'ada-loop)
155 (define-key ada-mode-map "\C-ct\C-r" 'ada-record)
156 (define-key ada-mode-map "\C-ct\C-s" 'ada-subtype)
157 (define-key ada-mode-map "\C-ctS" 'ada-tabsize)
158 (define-key ada-mode-map "\C-ct\C-t" 'ada-task-spec)
159 (define-key ada-mode-map "\C-ctt" 'ada-task-body)
160 (define-key ada-mode-map "\C-ct\C-y" 'ada-type)
161 (define-key ada-mode-map "\C-ct\C-v" 'ada-private)
162 (define-key ada-mode-map "\C-ctu" 'ada-use)
163 (define-key ada-mode-map "\C-ct\C-u" 'ada-with)
164 (define-key ada-mode-map "\C-ct\C-w" 'ada-when)
165 (define-key ada-mode-map "\C-ctw" 'ada-while-loop)
166 (define-key ada-mode-map "\C-ct\C-x" 'ada-exception)
167 (define-key ada-mode-map "\C-ctx" 'ada-exit)
169 ;;; ---- statement skeletons ------------------------------------------
171 (define-skeleton ada-array
172 "Insert array type definition.
173 Prompt for component type and index subtypes."
175 "array (" ("index definition: " str ", " ) -2 ") of " _ ?\;)
178 (define-skeleton ada-case
179 "Build skeleton case statement.
180 Prompt for the selector expression. Also builds the first when clause."
181 "[selector expression]: "
182 "case " str " is" \n
183 > "when " ("discrete choice: " str " | ") -3 " =>" \n
184 > _ \n
185 < < "end case;")
188 (define-skeleton ada-when
189 "Start a case statement alternative with a when clause."
191 < "when " ("discrete choice: " str " | ") -3 " =>" \n
195 (define-skeleton ada-declare-block
196 "Insert a block with a declare part.
197 Indent for the first declaration."
198 "[block name]: "
199 < str & ?: & \n
200 > "declare" \n
201 > _ \n
202 < "begin" \n
203 > \n
204 < "end " str | -1 ?\;)
207 (define-skeleton ada-exception-block
208 "Insert a block with an exception part.
209 Indent for the first line of code."
210 "[block name]: "
211 < str & ?: & \n
212 > "begin" \n
213 > _ \n
214 < "exception" \n
215 > \n
216 < "end " str | -1 ?\;)
219 (define-skeleton ada-exception
220 "Insert an indented exception part into a block."
222 < "exception" \n
226 (define-skeleton ada-exit-1
227 "Insert then exit condition of the exit statement, prompting for condition."
228 "[exit condition]: "
229 "when " str | -5)
232 (define-skeleton ada-exit
233 "Insert an exit statement, prompting for loop name and condition."
234 "[name of loop to exit]: "
235 "exit " str & ?\
236 (ada-exit-1)
237 | -1 ?\;)
239 ;;;###autoload
240 (defun ada-header ()
241 "Insert a descriptive header at the top of the file."
242 (interactive "*")
243 (save-excursion
244 (goto-char (point-min))
245 (if (fboundp 'make-header)
246 (funcall (symbol-function 'make-header))
247 (ada-header-tmpl))))
250 (define-skeleton ada-header-tmpl
251 "Insert a comment block containing the module title, author, etc."
252 "[Description]: "
253 "-- -*- Mode: Ada -*-"
254 "\n" ada-fill-comment-prefix "Filename : " (buffer-name)
255 "\n" ada-fill-comment-prefix "Description : " str
256 "\n" ada-fill-comment-prefix "Author : " (user-full-name)
257 "\n" ada-fill-comment-prefix "Created On : " (current-time-string)
258 "\n" ada-fill-comment-prefix "Last Modified By: ."
259 "\n" ada-fill-comment-prefix "Last Modified On: ."
260 "\n" ada-fill-comment-prefix "Update Count : 0"
261 "\n" ada-fill-comment-prefix "Status : Unknown, Use with caution!"
262 "\n")
265 (define-skeleton ada-display-comment
266 "Inserts three comment lines, making a display comment."
268 "--\n" ada-fill-comment-prefix _ "\n--")
271 (define-skeleton ada-if
272 "Insert skeleton if statment, prompting for a boolean-expression."
273 "[condition]: "
274 "if " str " then" \n
275 > _ \n
276 < "end if;")
279 (define-skeleton ada-elsif
280 "Add an elsif clause to an if statement,
281 prompting for the boolean-expression."
282 "[condition]: "
283 < "elsif " str " then" \n
287 (define-skeleton ada-else
288 "Add an else clause inside an if-then-end-if clause."
290 < "else" \n
294 (define-skeleton ada-loop
295 "Insert a skeleton loop statement. The exit statement is added by hand."
296 "[loop name]: "
297 < str & ?: & \n
298 > "loop" \n
299 > _ \n
300 < "end loop " str | -1 ?\;)
303 (define-skeleton ada-for-loop-prompt-variable
304 "Prompt for the loop variable."
305 "[loop variable]: "
306 str)
309 (define-skeleton ada-for-loop-prompt-range
310 "Prompt for the loop range."
311 "[loop range]: "
312 str)
315 (define-skeleton ada-for-loop
316 "Build a skeleton for-loop statement, prompting for the loop parameters."
317 "[loop name]: "
318 < str & ?: & \n
319 > "for "
320 (ada-for-loop-prompt-variable)
321 " in "
322 (ada-for-loop-prompt-range)
323 " loop" \n
324 > _ \n
325 < "end loop " str | -1 ?\;)
328 (define-skeleton ada-while-loop-prompt-entry-condition
329 "Prompt for the loop entry condition."
330 "[entry condition]: "
331 str)
334 (define-skeleton ada-while-loop
335 "Insert a skeleton while loop statement."
336 "[loop name]: "
337 < str & ?: & \n
338 > "while "
339 (ada-while-loop-prompt-entry-condition)
340 " loop" \n
341 > _ \n
342 < "end loop " str | -1 ?\;)
345 (define-skeleton ada-package-spec
346 "Insert a skeleton package specification."
347 "[package name]: "
348 "package " str " is" \n
349 > _ \n
350 < "end " str ?\;)
353 (define-skeleton ada-package-body
354 "Insert a skeleton package body -- includes a begin statement."
355 "[package name]: "
356 "package body " str " is" \n
357 > _ \n
358 ; < "begin" \n
359 < "end " str ?\;)
362 (define-skeleton ada-private
363 "Undent and start a private section of a package spec. Reindent."
365 < "private" \n
369 (define-skeleton ada-function-spec-prompt-return
370 "Prompts for function result type."
371 "[result type]: "
372 str)
375 (define-skeleton ada-function-spec
376 "Insert a function specification. Prompts for name and arguments."
377 "[function name]: "
378 "function " str
379 " (" ("[parameter_specification]: " str "; " ) -2 ")"
380 " return "
381 (ada-function-spec-prompt-return)
382 ";" \n )
385 (define-skeleton ada-procedure-spec
386 "Insert a procedure specification, prompting for its name and arguments."
387 "[procedure name]: "
388 "procedure " str
389 " (" ("[parameter_specification]: " str "; " ) -2 ")"
390 ";" \n )
393 (define-skeleton ada-subprogram-body
394 "Insert frame for subprogram body.
395 Invoke right after `ada-function-spec' or `ada-procedure-spec'."
397 ;; Remove `;' from subprogram decl
398 (save-excursion
399 (let ((pos (1+ (point))))
400 (ada-search-ignore-string-comment ada-subprog-start-re t nil)
401 (if (ada-search-ignore-string-comment "(" nil pos t 'search-forward)
402 (progn
403 (backward-char 1)
404 (forward-sexp 1)))
406 (if (looking-at ";")
407 (delete-char 1)))
408 " is" \n
409 _ \n
410 < "begin" \n
412 < "exception" \n
413 "when others => null;" \n
414 < < "end "
415 (ada-func-or-proc-name)
416 ";" \n)
419 (define-skeleton ada-separate
420 "Finish a body stub with `separate'."
422 > "separate;" \n
426 ;(define-skeleton ada-with
427 ; "Inserts a with clause, prompting for the list of units depended upon."
428 ; "[list of units depended upon]: "
429 ; "with " str ?\;)
431 ;(define-skeleton ada-use
432 ; "Inserts a use clause, prompting for the list of packages used."
433 ; "[list of packages used]: "
434 ; "use " str ?\;)
437 (define-skeleton ada-record
438 "Insert a skeleton record type declaration."
440 "record" \n
441 > _ \n
442 < "end record;")
445 (define-skeleton ada-subtype
446 "Start insertion of a subtype declaration, prompting for the subtype name."
447 "[subtype name]: "
448 "subtype " str " is " _ ?\;
449 (not (message "insert subtype indication.")))
452 (define-skeleton ada-type
453 "Start insertion of a type declaration, prompting for the type name."
454 "[type name]: "
455 "type " str ?\(
456 ("[discriminant specs]: " str " ")
457 | (backward-delete-char 1) | ?\)
458 " is "
459 (not (message "insert type definition.")))
462 (define-skeleton ada-task-body
463 "Insert a task body, prompting for the task name."
464 "[task name]: "
465 "task body " str " is\n"
466 "begin\n"
467 > _ \n
468 < "end " str ";" )
471 (define-skeleton ada-task-spec
472 "Insert a task specification, prompting for the task name."
473 "[task name]: "
474 "task " str
475 " (" ("[discriminant]: " str "; ") ") is\n"
476 > "entry " _ \n
477 <"end " str ";" )
480 (define-skeleton ada-get-param1
481 "Prompt for arguments and if any enclose them in brackets."
483 ("[parameter_specification]: " str "; " ) & -2 & ")"
487 (define-skeleton ada-get-param
488 "Prompt for arguments and if any enclose them in brackets."
490 " ("
491 (ada-get-param1) | -2
495 (define-skeleton ada-entry
496 "Insert a task entry, prompting for the entry name."
497 "[entry name]: "
498 "entry " str
499 (ada-get-param)
500 ";" \n
501 ; (ada-indent-current)
505 (define-skeleton ada-entry-family-prompt-discriminant
506 "Insert a entry specification, prompting for the entry name."
507 "[discriminant name]: "
508 str)
511 (define-skeleton ada-entry-family
512 "Insert a entry specification, prompting for the entry name."
513 "[entry name]: "
514 "entry " str
515 " (" (ada-entry-family-prompt-discriminant) ")"
516 (ada-get-param)
517 ";" \n
518 ;(ada-indent-current)
522 (define-skeleton ada-select
523 "Insert a select block."
525 "select\n"
526 > _ \n
527 < "end select;")
530 (define-skeleton ada-accept-1
531 "Insert a condition statement, prompting for the condition name."
532 "[condition]: "
533 "when " str | -5 )
536 (define-skeleton ada-accept-2
537 "Insert an accept statement, prompting for the name and arguments."
538 "[accept name]: "
539 > "accept " str
540 (ada-get-param)
541 ; " (" ("[parameter_specification]: " str "; ") -2 ")"
542 " do" \n
543 > _ \n
544 < "end " str ";" )
547 (define-skeleton ada-accept
548 "Insert an accept statement (prompt for condition, name and arguments)."
550 > (ada-accept-1) & " =>\n"
551 (ada-accept-2)
555 (define-skeleton ada-or-accept
556 "Insert a or statement, prompting for the condition name."
558 < "or\n"
559 (ada-accept)
563 (define-skeleton ada-or-delay
564 "Insert a delay statement, prompting for the delay value."
565 "[delay value]: "
566 < "or\n"
567 > "delay " str ";")
570 (define-skeleton ada-or-terminate
571 "Insert a terminate statement."
573 < "or\n"
574 > "terminate;")
577 ;; ----
578 (defun ada-adjust-case-skeleton ()
579 "Adjusts the case of the text inserted by a skeleton."
580 (save-excursion
581 (let ((aa-end (point)))
582 (ada-adjust-case-region
583 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
584 (goto-char aa-end))
587 (add-hook 'ada-mode-hook '(lambda ()
588 (setq skeleton-further-elements
589 '((< '(backward-delete-char-untabify
590 (min ada-indent (current-column))))))
591 (add-hook 'skeleton-end-hook
592 'ada-adjust-case-skeleton)))
594 (add-hook 'ada-mode-hook 'ada-stmt-add-to-ada-menu)
596 (provide 'ada-stmt)
598 ;;; ada-stmt.el ends here