]> code.delx.au - gnu-emacs/blob - lisp/progmodes/ada-stmt.el
(ada-template-map): Initialize and bind it to C-c t in ada-mode-map.
[gnu-emacs] / lisp / progmodes / ada-stmt.el
1 ;;; ada-stmt.el --- An extension to Ada mode for inserting statement templates.
2
3 ;; Copyright(C) 1987, 1993, 1994, 1996, 1997, 1998, 1999
4 ;; Free Software Foundation, Inc.
5
6 ;; Ada Core Technologies's version: $Revision: 1.7 $
7
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
12
13 ;;; Commentary:
14
15 ;;
16 ;; put the following statement in your .emacs:
17 ;; (require 'ada-stmt)
18 ;;
19
20 ;;; History:
21
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.)
26 ;;
27 ;; Sep 1993. Daniel Pfeiffer <pfeiffer@cict.fr> (DP)
28 ;; Introduced statement.el for smaller code and user configurability.
29 ;;
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
37 ;;
38 ;; 1994/12/02 Christian Egli <cegli@hcsd.hac.com>
39 ;; General cleanup and bug fixes.
40 ;;
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.
44
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 ;;;>
55
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 :-)
61
62 \f
63 ;;; Code:
64
65 (eval-when-compile
66 (condition-case nil (require 'skeleton)
67 (error nil)))
68
69 (require 'easymenu)
70
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))
117
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"))
123 t))))
124
125
126
127 \f
128 (defun ada-func-or-proc-name ()
129 ;; Get the name of the current function or procedure."
130 (save-excursion
131 (let ((case-fold-search t))
132 (if (re-search-backward ada-procedure-start-regexp nil t)
133 (buffer-substring (match-beginning 2) (match-end 2))
134 "NAME?"))))
135
136 (defvar ada-template-map
137 (let ((map (make-sparse-keymap)))
138 (define-key map "h" 'ada-header)
139 (define-key map "\C-a" 'ada-array)
140 (define-key map "b" 'ada-exception-block)
141 (define-key map "d" 'ada-declare-block)
142 (define-key map "c" 'ada-case)
143 (define-key map "\C-e" 'ada-elsif)
144 (define-key map "e" 'ada-else)
145 (define-key map "\C-k" 'ada-package-spec)
146 (define-key map "k" 'ada-package-body)
147 (define-key map "\C-p" 'ada-procedure-spec)
148 (define-key map "p" 'ada-subprogram-body)
149 (define-key map "\C-f" 'ada-function-spec)
150 (define-key map "f" 'ada-for-loop)
151 (define-key map "i" 'ada-if)
152 (define-key map "l" 'ada-loop)
153 (define-key map "\C-r" 'ada-record)
154 (define-key map "\C-s" 'ada-subtype)
155 (define-key map "S" 'ada-tabsize)
156 (define-key map "\C-t" 'ada-task-spec)
157 (define-key map "t" 'ada-task-body)
158 (define-key map "\C-y" 'ada-type)
159 (define-key map "\C-v" 'ada-private)
160 (define-key map "u" 'ada-use)
161 (define-key map "\C-u" 'ada-with)
162 (define-key map "\C-w" 'ada-when)
163 (define-key map "w" 'ada-while-loop)
164 (define-key map "\C-x" 'ada-exception)
165 (define-key map "x" 'ada-exit)
166 map)
167 "Keymap used in Ada mode for smart template operations.")
168
169 (define-key ada-mode-map "\C-ct" ada-template-map)
170
171 ;;; ---- statement skeletons ------------------------------------------
172
173 (define-skeleton ada-array
174 "Insert array type definition.
175 Prompt for component type and index subtypes."
176 ()
177 "array (" ("index definition: " str ", " ) -2 ") of " _ ?\;)
178
179
180 (define-skeleton ada-case
181 "Build skeleton case statement.
182 Prompt for the selector expression. Also builds the first when clause."
183 "[selector expression]: "
184 "case " str " is" \n
185 > "when " ("discrete choice: " str " | ") -3 " =>" \n
186 > _ \n
187 < < "end case;")
188
189
190 (define-skeleton ada-when
191 "Start a case statement alternative with a when clause."
192 ()
193 < "when " ("discrete choice: " str " | ") -3 " =>" \n
194 >)
195
196
197 (define-skeleton ada-declare-block
198 "Insert a block with a declare part.
199 Indent for the first declaration."
200 "[block name]: "
201 < str & ?: & \n
202 > "declare" \n
203 > _ \n
204 < "begin" \n
205 > \n
206 < "end " str | -1 ?\;)
207
208
209 (define-skeleton ada-exception-block
210 "Insert a block with an exception part.
211 Indent for the first line of code."
212 "[block name]: "
213 < str & ?: & \n
214 > "begin" \n
215 > _ \n
216 < "exception" \n
217 > \n
218 < "end " str | -1 ?\;)
219
220
221 (define-skeleton ada-exception
222 "Insert an indented exception part into a block."
223 ()
224 < "exception" \n
225 >)
226
227
228 (define-skeleton ada-exit-1
229 "Insert then exit condition of the exit statement, prompting for condition."
230 "[exit condition]: "
231 "when " str | -5)
232
233
234 (define-skeleton ada-exit
235 "Insert an exit statement, prompting for loop name and condition."
236 "[name of loop to exit]: "
237 "exit " str & ?\ (ada-exit-1) | -1 ?\;)
238
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))))
248
249
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")
263
264
265 (define-skeleton ada-display-comment
266 "Inserts three comment lines, making a display comment."
267 ()
268 "--\n" ada-fill-comment-prefix _ "\n--")
269
270
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;")
277
278
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
284 >)
285
286
287 (define-skeleton ada-else
288 "Add an else clause inside an if-then-end-if clause."
289 ()
290 < "else" \n
291 >)
292
293
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 ?\;)
301
302
303 (define-skeleton ada-for-loop-prompt-variable
304 "Prompt for the loop variable."
305 "[loop variable]: "
306 str)
307
308
309 (define-skeleton ada-for-loop-prompt-range
310 "Prompt for the loop range."
311 "[loop range]: "
312 str)
313
314
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 ?\;)
326
327
328 (define-skeleton ada-while-loop-prompt-entry-condition
329 "Prompt for the loop entry condition."
330 "[entry condition]: "
331 str)
332
333
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 ?\;)
343
344
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 ?\;)
351
352
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 ?\;)
360
361
362 (define-skeleton ada-private
363 "Undent and start a private section of a package spec. Reindent."
364 ()
365 < "private" \n
366 >)
367
368
369 (define-skeleton ada-function-spec-prompt-return
370 "Prompts for function result type."
371 "[result type]: "
372 str)
373
374
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 )
383
384
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 )
391
392
393 (define-skeleton ada-subprogram-body
394 "Insert frame for subprogram body.
395 Invoke right after `ada-function-spec' or `ada-procedure-spec'."
396 ()
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 (when (ada-search-ignore-string-comment "(" nil pos t 'search-forward)
402 (backward-char 1)
403 (forward-sexp 1)))
404 (if (looking-at ";")
405 (delete-char 1)))
406 " is" \n
407 _ \n
408 < "begin" \n
409 \n
410 < "exception" \n
411 "when others => null;" \n
412 < < "end "
413 (ada-func-or-proc-name)
414 ";" \n)
415
416
417 (define-skeleton ada-separate
418 "Finish a body stub with `separate'."
419 ()
420 > "separate;" \n
421 <)
422
423
424 ;(define-skeleton ada-with
425 ; "Inserts a with clause, prompting for the list of units depended upon."
426 ; "[list of units depended upon]: "
427 ; "with " str ?\;)
428
429 ;(define-skeleton ada-use
430 ; "Inserts a use clause, prompting for the list of packages used."
431 ; "[list of packages used]: "
432 ; "use " str ?\;)
433
434
435 (define-skeleton ada-record
436 "Insert a skeleton record type declaration."
437 ()
438 "record" \n
439 > _ \n
440 < "end record;")
441
442
443 (define-skeleton ada-subtype
444 "Start insertion of a subtype declaration, prompting for the subtype name."
445 "[subtype name]: "
446 "subtype " str " is " _ ?\;
447 (not (message "insert subtype indication.")))
448
449
450 (define-skeleton ada-type
451 "Start insertion of a type declaration, prompting for the type name."
452 "[type name]: "
453 "type " str ?\(
454 ("[discriminant specs]: " str " ")
455 | (backward-delete-char 1) | ?\)
456 " is "
457 (not (message "insert type definition.")))
458
459
460 (define-skeleton ada-task-body
461 "Insert a task body, prompting for the task name."
462 "[task name]: "
463 "task body " str " is\n"
464 "begin\n"
465 > _ \n
466 < "end " str ";" )
467
468
469 (define-skeleton ada-task-spec
470 "Insert a task specification, prompting for the task name."
471 "[task name]: "
472 "task " str
473 " (" ("[discriminant]: " str "; ") ") is\n"
474 > "entry " _ \n
475 <"end " str ";" )
476
477
478 (define-skeleton ada-get-param1
479 "Prompt for arguments and if any enclose them in brackets."
480 ()
481 ("[parameter_specification]: " str "; " ) & -2 & ")")
482
483
484 (define-skeleton ada-get-param
485 "Prompt for arguments and if any enclose them in brackets."
486 ()
487 " ("
488 (ada-get-param1) | -2)
489
490
491 (define-skeleton ada-entry
492 "Insert a task entry, prompting for the entry name."
493 "[entry name]: "
494 "entry " str
495 (ada-get-param)
496 ";" \n)
497
498
499 (define-skeleton ada-entry-family-prompt-discriminant
500 "Insert a entry specification, prompting for the entry name."
501 "[discriminant name]: "
502 str)
503
504
505 (define-skeleton ada-entry-family
506 "Insert a entry specification, prompting for the entry name."
507 "[entry name]: "
508 "entry " str
509 " (" (ada-entry-family-prompt-discriminant) ")"
510 (ada-get-param)
511 ";" \n)
512
513
514 (define-skeleton ada-select
515 "Insert a select block."
516 ()
517 "select\n"
518 > _ \n
519 < "end select;")
520
521
522 (define-skeleton ada-accept-1
523 "Insert a condition statement, prompting for the condition name."
524 "[condition]: "
525 "when " str | -5 )
526
527
528 (define-skeleton ada-accept-2
529 "Insert an accept statement, prompting for the name and arguments."
530 "[accept name]: "
531 > "accept " str
532 (ada-get-param)
533 ;;; " (" ("[parameter_specification]: " str "; ") -2 ")"
534 " do" \n
535 > _ \n
536 < "end " str ";" )
537
538
539 (define-skeleton ada-accept
540 "Insert an accept statement (prompt for condition, name and arguments)."
541 ()
542 > (ada-accept-1) & " =>\n"
543 (ada-accept-2))
544
545
546 (define-skeleton ada-or-accept
547 "Insert an or statement, prompting for the condition name."
548 ()
549 < "or\n"
550 (ada-accept))
551
552
553 (define-skeleton ada-or-delay
554 "Insert a delay statement, prompting for the delay value."
555 "[delay value]: "
556 < "or\n"
557 > "delay " str ";")
558
559
560 (define-skeleton ada-or-terminate
561 "Insert a terminate statement."
562 ()
563 < "or\n"
564 > "terminate;")
565
566
567 ;; ----
568 (defun ada-adjust-case-skeleton ()
569 "Adjust the case of the text inserted by a skeleton."
570 (save-excursion
571 (let ((aa-end (point)))
572 (ada-adjust-case-region
573 (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
574 (goto-char aa-end)))))
575
576 (defun ada-stmt-mode-hook ()
577 (set (make-local-variable 'skeleton-further-elements)
578 '((< '(backward-delete-char-untabify
579 (min ada-indent (current-column))))))
580 (add-hook 'skeleton-end-hook
581 'ada-adjust-case-skeleton nil t)
582 (ada-stmt-add-to-ada-menu))
583
584 (add-hook 'ada-mode-hook 'ada-stmt-mode-hook)
585
586 (provide 'ada-stmt)
587
588 ;;; ada-stmt.el ends here