]> code.delx.au - gnu-emacs/blob - lisp/progmodes/modula2.el
(f90-break-before-delimiters): Fix customize type.
[gnu-emacs] / lisp / progmodes / modula2.el
1 ;;; modula2.el --- Modula-2 editing support package
2
3 ;; Author: Michael Schmidt <michael@pbinfo.UUCP>
4 ;; Tom Perrine <Perrin@LOGICON.ARPA>
5 ;; Maintainer: FSF
6 ;; Keywords: languages
7
8 ;; The authors distributed this without a copyright notice
9 ;; back in 1988, so it is in the public domain. The original included
10 ;; the following credit:
11
12 ;; Author Mick Jordan
13 ;; amended Peter Robinson
14
15 ;;; Commentary:
16
17 ;; A major mode for editing Modula-2 code. It provides convenient abbrevs
18 ;; for Modula-2 keywords, knows about the standard layout rules, and supports
19 ;; a native compile command.
20
21 ;;; Code:
22
23 ;;; Added by Tom Perrine (TEP)
24 (defvar m2-mode-syntax-table nil
25 "Syntax table in use in Modula-2 buffers.")
26
27 (defvar m2-compile-command "m2c"
28 "Command to compile Modula-2 programs")
29
30 (defvar m2-link-command "m2l"
31 "Command to link Modula-2 programs")
32
33 (defvar m2-link-name nil
34 "Name of the executable.")
35
36 (defvar m2-end-comment-column nil
37 "*Column for aligning the end of a comment, in Modula-2.")
38
39 (if m2-mode-syntax-table
40 ()
41 (let ((table (make-syntax-table)))
42 (modify-syntax-entry ?\\ "\\" table)
43 (modify-syntax-entry ?\( ". 1" table)
44 (modify-syntax-entry ?\) ". 4" table)
45 (modify-syntax-entry ?* ". 23" table)
46 (modify-syntax-entry ?+ "." table)
47 (modify-syntax-entry ?- "." table)
48 (modify-syntax-entry ?= "." table)
49 (modify-syntax-entry ?% "." table)
50 (modify-syntax-entry ?< "." table)
51 (modify-syntax-entry ?> "." table)
52 (modify-syntax-entry ?\' "\"" table)
53 (setq m2-mode-syntax-table table)))
54
55 ;;; Added by TEP
56 (defvar m2-mode-map nil
57 "Keymap used in Modula-2 mode.")
58
59 (if m2-mode-map ()
60 (let ((map (make-sparse-keymap)))
61 (define-key map "\^i" 'm2-tab)
62 (define-key map "\C-cb" 'm2-begin)
63 (define-key map "\C-cc" 'm2-case)
64 (define-key map "\C-cd" 'm2-definition)
65 (define-key map "\C-ce" 'm2-else)
66 (define-key map "\C-cf" 'm2-for)
67 (define-key map "\C-ch" 'm2-header)
68 (define-key map "\C-ci" 'm2-if)
69 (define-key map "\C-cm" 'm2-module)
70 (define-key map "\C-cl" 'm2-loop)
71 (define-key map "\C-co" 'm2-or)
72 (define-key map "\C-cp" 'm2-procedure)
73 (define-key map "\C-c\C-w" 'm2-with)
74 (define-key map "\C-cr" 'm2-record)
75 (define-key map "\C-cs" 'm2-stdio)
76 (define-key map "\C-ct" 'm2-type)
77 (define-key map "\C-cu" 'm2-until)
78 (define-key map "\C-cv" 'm2-var)
79 (define-key map "\C-cw" 'm2-while)
80 (define-key map "\C-cx" 'm2-export)
81 (define-key map "\C-cy" 'm2-import)
82 (define-key map "\C-c{" 'm2-begin-comment)
83 (define-key map "\C-c}" 'm2-end-comment)
84 (define-key map "\C-j" 'm2-newline)
85 (define-key map "\C-c\C-z" 'suspend-emacs)
86 (define-key map "\C-c\C-v" 'm2-visit)
87 (define-key map "\C-c\C-t" 'm2-toggle)
88 (define-key map "\C-c\C-l" 'm2-link)
89 (define-key map "\C-c\C-c" 'm2-compile)
90 (setq m2-mode-map map)))
91
92 (defvar m2-indent 5 "*This variable gives the indentation in Modula-2-Mode")
93
94 ;;;###autoload
95 (defun modula-2-mode ()
96 "This is a mode intended to support program development in Modula-2.
97 All control constructs of Modula-2 can be reached by typing C-c
98 followed by the first character of the construct.
99 \\<m2-mode-map>
100 \\[m2-begin] begin \\[m2-case] case
101 \\[m2-definition] definition \\[m2-else] else
102 \\[m2-for] for \\[m2-header] header
103 \\[m2-if] if \\[m2-module] module
104 \\[m2-loop] loop \\[m2-or] or
105 \\[m2-procedure] procedure Control-c Control-w with
106 \\[m2-record] record \\[m2-stdio] stdio
107 \\[m2-type] type \\[m2-until] until
108 \\[m2-var] var \\[m2-while] while
109 \\[m2-export] export \\[m2-import] import
110 \\[m2-begin-comment] begin-comment \\[m2-end-comment] end-comment
111 \\[suspend-emacs] suspend Emacs \\[m2-toggle] toggle
112 \\[m2-compile] compile \\[m2-next-error] next-error
113 \\[m2-link] link
114
115 `m2-indent' controls the number of spaces for each indentation.
116 `m2-compile-command' holds the command to compile a Modula-2 program.
117 `m2-link-command' holds the command to link a Modula-2 program."
118 (interactive)
119 (kill-all-local-variables)
120 (use-local-map m2-mode-map)
121 (setq major-mode 'modula-2-mode)
122 (setq mode-name "Modula-2")
123 (make-local-variable 'comment-column)
124 (setq comment-column 41)
125 (make-local-variable 'm2-end-comment-column)
126 (setq m2-end-comment-column 75)
127 (set-syntax-table m2-mode-syntax-table)
128 (make-local-variable 'paragraph-start)
129 (setq paragraph-start (concat "$\\|" page-delimiter))
130 (make-local-variable 'paragraph-separate)
131 (setq paragraph-separate paragraph-start)
132 (make-local-variable 'paragraph-ignore-fill-prefix)
133 (setq paragraph-ignore-fill-prefix t)
134 ; (make-local-variable 'indent-line-function)
135 ; (setq indent-line-function 'c-indent-line)
136 (make-local-variable 'require-final-newline)
137 (setq require-final-newline t)
138 (make-local-variable 'comment-start)
139 (setq comment-start "(* ")
140 (make-local-variable 'comment-end)
141 (setq comment-end " *)")
142 (make-local-variable 'comment-column)
143 (setq comment-column 41)
144 (make-local-variable 'comment-start-skip)
145 (setq comment-start-skip "/\\*+ *")
146 (make-local-variable 'comment-indent-function)
147 (setq comment-indent-function 'c-comment-indent)
148 (make-local-variable 'parse-sexp-ignore-comments)
149 (setq parse-sexp-ignore-comments t)
150 (make-local-variable 'font-lock-defaults)
151 (setq font-lock-defaults
152 '((m3-font-lock-keywords
153 m3-font-lock-keywords-1 m3-font-lock-keywords-2)
154 nil nil ((?_ . "w") (?. . "w") (?< . ". 1") (?> . ". 4")) nil
155 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
156 ;(font-lock-comment-start-regexp . "(\\*")
157 ))
158 (run-hooks 'm2-mode-hook))
159 \f
160 ;; Regexps written with help from Ron Forrester <ron@orcad.com>
161 ;; and Spencer Allain <sallain@teknowledge.com>.
162 (defconst m3-font-lock-keywords-1
163 '(
164 ;;
165 ;; Module definitions.
166 ("\\<\\(INTERFACE\\|MODULE\\|PROCEDURE\\)\\>[ \t]*\\(\\sw+\\)?"
167 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
168 ;;
169 ;; Import directives.
170 ("\\<\\(EXPORTS\\|FROM\\|IMPORT\\)\\>"
171 (1 font-lock-keyword-face)
172 (font-lock-match-c-style-declaration-item-and-skip-to-next
173 nil (goto-char (match-end 0))
174 (1 font-lock-reference-face)))
175 ;;
176 ;; Pragmas as warnings.
177 ;; Spencer Allain <sallain@teknowledge.com> says do them as comments...
178 ;; ("<\\*.*\\*>" . font-lock-warning-face)
179 ;; ... but instead we fontify the first word.
180 ("<\\*[ \t]*\\(\\sw+\\)" 1 font-lock-warning-face prepend)
181 )
182 "Subdued level highlighting for Modula-3 modes.")
183
184 (defconst m3-font-lock-keywords-2
185 (append m3-font-lock-keywords-1
186 (eval-when-compile
187 (let ((m3-types
188 (regexp-opt
189 '("INTEGER" "BITS" "BOOLEAN" "CARDINAL" "CHAR" "FLOAT" "REAL"
190 "LONGREAL" "REFANY" "ADDRESS" "ARRAY" "SET" "TEXT"
191 "MUTEX" "ROOT" "EXTENDED")))
192 (m3-keywords
193 (regexp-opt
194 '("AND" "ANY" "AS" "BEGIN" "BRANDED" "BY" "CASE" "CONST" "DIV"
195 "DO" "ELSE" "ELSIF" "EVAL" "EXCEPT" "EXIT" "FINALLY"
196 "FOR" "GENERIC" "IF" "IN" "LOCK" "LOOP" "METHODS" "MOD" "NOT"
197 "OBJECT" "OF" "OR" "OVERRIDES" "READONLY" "RECORD" "REF"
198 "REPEAT" "RETURN" "REVEAL" "THEN" "TO" "TRY"
199 "TYPE" "TYPECASE" "UNSAFE" "UNTIL" "UNTRACED" "VAR" "VALUE"
200 "WHILE" "WITH")))
201 (m3-builtins
202 (regexp-opt
203 '("ABS" "ADR" "ADRSIZE" "BITSIZE" "BYTESIZE" "CEILING"
204 "DEC" "DISPOSE" "FIRST" "FLOOR" "INC" "ISTYPE" "LAST"
205 "LOOPHOLE" "MAX" "MIN" "NARROW" "NEW" "NUMBER" "ORD"
206 "ROUND" "SUBARRAY" "TRUNC" "TYPECODE" "VAL")))
207 )
208 (list
209 ;;
210 ;; Keywords except those fontified elsewhere.
211 (concat "\\<\\(" m3-keywords "\\)\\>")
212 ;;
213 ;; Builtins.
214 (cons (concat "\\<\\(" m3-builtins "\\)\\>") 'font-lock-builtin-face)
215 ;;
216 ;; Type names.
217 (cons (concat "\\<\\(" m3-types "\\)\\>") 'font-lock-type-face)
218 ;;
219 ;; Fontify tokens as function names.
220 '("\\<\\(END\\|EXCEPTION\\|RAISES?\\)\\>[ \t{]*"
221 (1 font-lock-keyword-face)
222 (font-lock-match-c-style-declaration-item-and-skip-to-next
223 nil (goto-char (match-end 0))
224 (1 font-lock-function-name-face)))
225 ;;
226 ;; Fontify constants as references.
227 '("\\<\\(FALSE\\|NIL\\|NULL\\|TRUE\\)\\>" . font-lock-reference-face)
228 ))))
229 "Gaudy level highlighting for Modula-3 modes.")
230
231 (defvar m3-font-lock-keywords m3-font-lock-keywords-1
232 "Default expressions to highlight in Modula-3 modes.")
233
234 ;; We don't actually have different keywords for Modula-2. Volunteers?
235 (defconst m2-font-lock-keywords-1 m3-font-lock-keywords-1
236 "Subdued level highlighting for Modula-2 modes.")
237
238 (defconst m2-font-lock-keywords-2 m3-font-lock-keywords-2
239 "Gaudy level highlighting for Modula-2 modes.")
240
241 (defvar m2-font-lock-keywords m2-font-lock-keywords-1
242 "Default expressions to highlight in Modula-2 modes.")
243 \f
244 (defun m2-newline ()
245 "Insert a newline and indent following line like previous line."
246 (interactive)
247 (let ((hpos (current-indentation)))
248 (newline)
249 (indent-to hpos)))
250
251 (defun m2-tab ()
252 "Indent to next tab stop."
253 (interactive)
254 (indent-to (* (1+ (/ (current-indentation) m2-indent)) m2-indent)))
255
256 (defun m2-begin ()
257 "Insert a BEGIN keyword and indent for the next line."
258 (interactive)
259 (insert "BEGIN")
260 (m2-newline)
261 (m2-tab))
262
263 (defun m2-case ()
264 "Build skeleton CASE statement, prompting for the <expression>."
265 (interactive)
266 (let ((name (read-string "Case-Expression: ")))
267 (insert "CASE " name " OF")
268 (m2-newline)
269 (m2-newline)
270 (insert "END (* case " name " *);"))
271 (end-of-line 0)
272 (m2-tab))
273
274 (defun m2-definition ()
275 "Build skeleton DEFINITION MODULE, prompting for the <module name>."
276 (interactive)
277 (insert "DEFINITION MODULE ")
278 (let ((name (read-string "Name: ")))
279 (insert name ";\n\n\n\nEND " name ".\n"))
280 (previous-line 3))
281
282 (defun m2-else ()
283 "Insert ELSE keyword and indent for next line."
284 (interactive)
285 (m2-newline)
286 (backward-delete-char-untabify m2-indent ())
287 (insert "ELSE")
288 (m2-newline)
289 (m2-tab))
290
291 (defun m2-for ()
292 "Build skeleton FOR loop statement, prompting for the loop parameters."
293 (interactive)
294 (insert "FOR ")
295 (let ((name (read-string "Loop Initialiser: ")) limit by)
296 (insert name " TO ")
297 (setq limit (read-string "Limit: "))
298 (insert limit)
299 (setq by (read-string "Step: "))
300 (if (not (string-equal by ""))
301 (insert " BY " by))
302 (insert " DO")
303 (m2-newline)
304 (m2-newline)
305 (insert "END (* for " name " to " limit " *);"))
306 (end-of-line 0)
307 (m2-tab))
308
309 (defun m2-header ()
310 "Insert a comment block containing the module title, author, etc."
311 (interactive)
312 (insert "(*\n Title: \t")
313 (insert (read-string "Title: "))
314 (insert "\n Created:\t")
315 (insert (current-time-string))
316 (insert "\n Author: \t")
317 (insert (user-full-name))
318 (insert (concat "\n\t\t<" (user-login-name) "@" (system-name) ">\n"))
319 (insert "*)\n\n"))
320
321 (defun m2-if ()
322 "Insert skeleton IF statement, prompting for <boolean-expression>."
323 (interactive)
324 (insert "IF ")
325 (let ((thecondition (read-string "<boolean-expression>: ")))
326 (insert thecondition " THEN")
327 (m2-newline)
328 (m2-newline)
329 (insert "END (* if " thecondition " *);"))
330 (end-of-line 0)
331 (m2-tab))
332
333 (defun m2-loop ()
334 "Build skeleton LOOP (with END)."
335 (interactive)
336 (insert "LOOP")
337 (m2-newline)
338 (m2-newline)
339 (insert "END (* loop *);")
340 (end-of-line 0)
341 (m2-tab))
342
343 (defun m2-module ()
344 "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
345 (interactive)
346 (insert "IMPLEMENTATION MODULE ")
347 (let ((name (read-string "Name: ")))
348 (insert name ";\n\n\n\nEND " name ".\n")
349 (previous-line 3)
350 (m2-header)
351 (m2-type)
352 (newline)
353 (m2-var)
354 (newline)
355 (m2-begin)
356 (m2-begin-comment)
357 (insert " Module " name " Initialisation Code "))
358 (m2-end-comment)
359 (newline)
360 (m2-tab))
361
362 (defun m2-or ()
363 (interactive)
364 (m2-newline)
365 (backward-delete-char-untabify m2-indent)
366 (insert "|")
367 (m2-newline)
368 (m2-tab))
369
370 (defun m2-procedure ()
371 (interactive)
372 (insert "PROCEDURE ")
373 (let ((name (read-string "Name: " ))
374 args)
375 (insert name " (")
376 (insert (read-string "Arguments: ") ")")
377 (setq args (read-string "Result Type: "))
378 (if (not (string-equal args ""))
379 (insert " : " args))
380 (insert ";")
381 (m2-newline)
382 (insert "BEGIN")
383 (m2-newline)
384 (m2-newline)
385 (insert "END ")
386 (insert name)
387 (insert ";")
388 (end-of-line 0)
389 (m2-tab)))
390
391 (defun m2-with ()
392 (interactive)
393 (insert "WITH ")
394 (let ((name (read-string "Record-Type: ")))
395 (insert name)
396 (insert " DO")
397 (m2-newline)
398 (m2-newline)
399 (insert "END (* with " name " *);"))
400 (end-of-line 0)
401 (m2-tab))
402
403 (defun m2-record ()
404 (interactive)
405 (insert "RECORD")
406 (m2-newline)
407 (m2-newline)
408 (insert "END (* record *);")
409 (end-of-line 0)
410 (m2-tab))
411
412 (defun m2-stdio ()
413 (interactive)
414 (insert "
415 FROM TextIO IMPORT
416 WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,
417 WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,
418 WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,
419 WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,
420 WriteString, ReadString, WhiteSpace, EndOfLine;
421
422 FROM SysStreams IMPORT sysIn, sysOut, sysErr;
423
424 "))
425
426 (defun m2-type ()
427 (interactive)
428 (insert "TYPE")
429 (m2-newline)
430 (m2-tab))
431
432 (defun m2-until ()
433 (interactive)
434 (insert "REPEAT")
435 (m2-newline)
436 (m2-newline)
437 (insert "UNTIL ")
438 (insert (read-string "<boolean-expression>: ") ";")
439 (end-of-line 0)
440 (m2-tab))
441
442 (defun m2-var ()
443 (interactive)
444 (m2-newline)
445 (insert "VAR")
446 (m2-newline)
447 (m2-tab))
448
449 (defun m2-while ()
450 (interactive)
451 (insert "WHILE ")
452 (let ((name (read-string "<boolean-expression>: ")))
453 (insert name " DO" )
454 (m2-newline)
455 (m2-newline)
456 (insert "END (* while " name " *);"))
457 (end-of-line 0)
458 (m2-tab))
459
460 (defun m2-export ()
461 (interactive)
462 (insert "EXPORT QUALIFIED "))
463
464 (defun m2-import ()
465 (interactive)
466 (insert "FROM ")
467 (insert (read-string "Module: "))
468 (insert " IMPORT "))
469
470 (defun m2-begin-comment ()
471 (interactive)
472 (if (not (bolp))
473 (indent-to comment-column 0))
474 (insert "(* "))
475
476 (defun m2-end-comment ()
477 (interactive)
478 (if (not (bolp))
479 (indent-to m2-end-comment-column))
480 (insert "*)"))
481
482 (defun m2-compile ()
483 (interactive)
484 (compile (concat m2-compile-command " " (buffer-name))))
485
486 (defun m2-link ()
487 (interactive)
488 (if m2-link-name
489 (compile (concat m2-link-command " " m2-link-name))
490 (compile (concat m2-link-command " "
491 (setq m2-link-name (read-string "Name of executable: "
492 (buffer-name)))))))
493
494 (defun m2-execute-monitor-command (command)
495 (let* ((shell shell-file-name)
496 (csh (equal (file-name-nondirectory shell) "csh")))
497 (call-process shell nil t t "-cf" (concat "exec " command))))
498
499 (defun m2-visit ()
500 (interactive)
501 (let ((deffile nil)
502 (modfile nil)
503 modulename)
504 (save-excursion
505 (setq modulename
506 (read-string "Module name: "))
507 (switch-to-buffer "*Command Execution*")
508 (m2-execute-monitor-command (concat "m2whereis " modulename))
509 (goto-char (point-min))
510 (condition-case ()
511 (progn (re-search-forward "\\(.*\\.def\\) *$")
512 (setq deffile (buffer-substring (match-beginning 1)
513 (match-end 1))))
514 (search-failed ()))
515 (condition-case ()
516 (progn (re-search-forward "\\(.*\\.mod\\) *$")
517 (setq modfile (buffer-substring (match-beginning 1)
518 (match-end 1))))
519 (search-failed ()))
520 (if (not (or deffile modfile))
521 (error "I can find neither definition nor implementation of %s"
522 modulename)))
523 (cond (deffile
524 (find-file deffile)
525 (if modfile
526 (save-excursion
527 (find-file modfile))))
528 (modfile
529 (find-file modfile)))))
530
531 (defun m2-toggle ()
532 "Toggle between .mod and .def files for the module."
533 (interactive)
534 (cond ((string-equal (substring (buffer-name) -4) ".def")
535 (find-file-other-window
536 (concat (substring (buffer-name) 0 -4) ".mod")))
537 ((string-equal (substring (buffer-name) -4) ".mod")
538 (find-file-other-window
539 (concat (substring (buffer-name) 0 -4) ".def")))
540 ((string-equal (substring (buffer-name) -3) ".mi")
541 (find-file-other-window
542 (concat (substring (buffer-name) 0 -3) ".md")))
543 ((string-equal (substring (buffer-name) -3) ".md")
544 (find-file-other-window
545 (concat (substring (buffer-name) 0 -3) ".mi")))))
546
547 (provide 'modula2)
548
549 ;;; modula2.el ends here