]> code.delx.au - gnu-emacs/blob - lisp/progmodes/modula2.el
Delete M-TAB binding of complete-tag.
[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")) 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
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 ("<\\*.*\\*>" . font-lock-warning-face)
178 )
179 "Subdued level highlighting for Modula-3 modes.")
180
181 (defconst m3-font-lock-keywords-2
182 (append m3-font-lock-keywords-1
183 (eval-when-compile
184 (let ((m3-types
185 ; (make-regexp
186 ; '("INTEGER" "BITS" "BOOLEAN" "CARDINAL" "CHAR" "FLOAT"
187 ; "LONGREAL" "REAL" "REFANY" "ADDRESS" "ARRAY" "TEXT"))
188 (concat "A\\(DDRESS\\|RRAY\\)\\|B\\(ITS\\|OOLEAN\\)\\|"
189 "C\\(ARDINAL\\|HAR\\)\\|FLOAT\\|INTEGER\\|LONGREAL\\|"
190 "RE\\(AL\\|FANY\\)\\|TEXT"))
191 (m3-keywords
192 ; (make-regexp
193 ; '("AND" "ANY" "AS" "BEGIN" "BRANDED" "BY" "CASE" "CONST" "DIV"
194 ; "DO" "ELSE" "ELSIF" "EVAL" "EXCEPT" "EXIT" "EXTENDED" "FINALLY"
195 ; "FOR" "IF" "IN" "LOCK" "LOOP" "METHODS" "MOD" "MUTEX" "NOT"
196 ; "OBJECT" "OF" "OR" "OVERRIDES" "READONLY" "RECORD" "REF"
197 ; "REPEAT" "RETURN" "REVEAL" "ROOT" "SET" "THEN" "TO" "TRY"
198 ; "TYPE" "TYPECASE" "UNSAFE" "UNTIL" "UNTRACED" "VAR" "VALUE"
199 ; "WHILE" "WITH"))
200 (concat "A\\(N[DY]\\|S\\)\\|B\\(EGIN\\|RANDED\\|Y\\)\\|"
201 "C\\(ASE\\|ONST\\)\\|D\\(IV\\|O\\)\\|"
202 "E\\(LS\\(E\\|IF\\)\\|VAL\\|"
203 "X\\(CEPT\\|IT\\|TENDED\\)\\)\\|F\\(INALLY\\|OR\\)\\|"
204 "I[FN]\\|LO\\(CK\\|OP\\)\\|M\\(ETHODS\\|OD\\|UTEX\\)\\|"
205 "NOT\\|O\\([FR]\\|BJECT\\|VERRIDES\\)\\|"
206 "R\\(E\\(ADONLY\\|CORD\\|F\\|PEAT\\|TURN\\|VEAL\\)\\|"
207 "OOT\\)\\|SET\\|T\\(HEN\\|O\\|RY\\|YPE\\(\\|CASE\\)\\)\\|"
208 "UN\\(SAFE\\|T\\(IL\\|RACED\\)\\)\\|VA\\(LUE\\|R\\)\\|"
209 "W\\(HILE\\|ITH\\)"))
210 (m3-builtins
211 ; (make-regexp
212 ; '("ABS" "ADR" "ADRSIZE" "BITSIZE" "BYTESIZE" "CEILING"
213 ; "DEC" "DISPOSE" "FIRST" "FLOOR" "INC" "ISTYPE" "LAST"
214 ; "LOOPHOLE" "MAX" "MIN" "NARROW" "NEW" "NUMBER" "ORD"
215 ; "ROUND" "SUBARRAY" "TRUNC" "TYPECODE" "VAL"))
216 (concat "A\\(BS\\|DR\\(\\|SIZE\\)\\)\\|B\\(ITSIZE\\|YTESIZE\\)\\|"
217 "CEILING\\|D\\(EC\\|ISPOSE\\)\\|F\\(IRST\\|LOOR\\)\\|"
218 "I\\(NC\\|STYPE\\)\\|L\\(AST\\|OOPHOLE\\)\\|"
219 "M\\(AX\\|IN\\)\\|N\\(ARROW\\|EW\\|UMBER\\)\\|ORD\\|"
220 "ROUND\\|SUBARRAY\\|T\\(RUNC\\|YPECODE\\)\\|VAL"))
221 )
222 (list
223 ;;
224 ;; Keywords except those fontified elsewhere.
225 (concat "\\<\\(" m3-keywords "\\)\\>")
226 ;;
227 ;; Builtins.
228 (cons (concat "\\<\\(" m3-builtins "\\)\\>") 'font-lock-builtin-face)
229 ;;
230 ;; Type names.
231 (cons (concat "\\<\\(" m3-types "\\)\\>") 'font-lock-type-face)
232 ;;
233 ;; Fontify tokens as function names.
234 '("\\<\\(END\\|EXCEPTION\\|RAISES?\\)\\>[ \t{]*"
235 (1 font-lock-keyword-face)
236 (font-lock-match-c-style-declaration-item-and-skip-to-next nil nil
237 (1 font-lock-function-name-face)))
238 ;;
239 ;; Fontify constants as references.
240 '("\\<\\(FALSE\\|NIL\\|NULL\\|TRUE\\)\\>" . font-lock-reference-face)
241 ))))
242 "Gaudy level highlighting for Modula-3 modes.")
243
244 (defvar m3-font-lock-keywords m3-font-lock-keywords-1
245 "Default expressions to highlight in Modula-3 modes.")
246
247 ;; We don't actually have different keywords for Modula-2. Volunteers?
248 (defconst m2-font-lock-keywords-1 m3-font-lock-keywords-1
249 "Subdued level highlighting for Modula-2 modes.")
250
251 (defconst m2-font-lock-keywords-2 m3-font-lock-keywords-2
252 "Gaudy level highlighting for Modula-2 modes.")
253
254 (defvar m2-font-lock-keywords m2-font-lock-keywords-1
255 "Default expressions to highlight in Modula-2 modes.")
256 \f
257 (defun m2-newline ()
258 "Insert a newline and indent following line like previous line."
259 (interactive)
260 (let ((hpos (current-indentation)))
261 (newline)
262 (indent-to hpos)))
263
264 (defun m2-tab ()
265 "Indent to next tab stop."
266 (interactive)
267 (indent-to (* (1+ (/ (current-indentation) m2-indent)) m2-indent)))
268
269 (defun m2-begin ()
270 "Insert a BEGIN keyword and indent for the next line."
271 (interactive)
272 (insert "BEGIN")
273 (m2-newline)
274 (m2-tab))
275
276 (defun m2-case ()
277 "Build skeleton CASE statement, prompting for the <expression>."
278 (interactive)
279 (let ((name (read-string "Case-Expression: ")))
280 (insert "CASE " name " OF")
281 (m2-newline)
282 (m2-newline)
283 (insert "END (* case " name " *);"))
284 (end-of-line 0)
285 (m2-tab))
286
287 (defun m2-definition ()
288 "Build skeleton DEFINITION MODULE, prompting for the <module name>."
289 (interactive)
290 (insert "DEFINITION MODULE ")
291 (let ((name (read-string "Name: ")))
292 (insert name ";\n\n\n\nEND " name ".\n"))
293 (previous-line 3))
294
295 (defun m2-else ()
296 "Insert ELSE keyword and indent for next line."
297 (interactive)
298 (m2-newline)
299 (backward-delete-char-untabify m2-indent ())
300 (insert "ELSE")
301 (m2-newline)
302 (m2-tab))
303
304 (defun m2-for ()
305 "Build skeleton FOR loop statement, prompting for the loop parameters."
306 (interactive)
307 (insert "FOR ")
308 (let ((name (read-string "Loop Initialiser: ")) limit by)
309 (insert name " TO ")
310 (setq limit (read-string "Limit: "))
311 (insert limit)
312 (setq by (read-string "Step: "))
313 (if (not (string-equal by ""))
314 (insert " BY " by))
315 (insert " DO")
316 (m2-newline)
317 (m2-newline)
318 (insert "END (* for " name " to " limit " *);"))
319 (end-of-line 0)
320 (m2-tab))
321
322 (defun m2-header ()
323 "Insert a comment block containing the module title, author, etc."
324 (interactive)
325 (insert "(*\n Title: \t")
326 (insert (read-string "Title: "))
327 (insert "\n Created:\t")
328 (insert (current-time-string))
329 (insert "\n Author: \t")
330 (insert (user-full-name))
331 (insert (concat "\n\t\t<" (user-login-name) "@" (system-name) ">\n"))
332 (insert "*)\n\n"))
333
334 (defun m2-if ()
335 "Insert skeleton IF statement, prompting for <boolean-expression>."
336 (interactive)
337 (insert "IF ")
338 (let ((thecondition (read-string "<boolean-expression>: ")))
339 (insert thecondition " THEN")
340 (m2-newline)
341 (m2-newline)
342 (insert "END (* if " thecondition " *);"))
343 (end-of-line 0)
344 (m2-tab))
345
346 (defun m2-loop ()
347 "Build skeleton LOOP (with END)."
348 (interactive)
349 (insert "LOOP")
350 (m2-newline)
351 (m2-newline)
352 (insert "END (* loop *);")
353 (end-of-line 0)
354 (m2-tab))
355
356 (defun m2-module ()
357 "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
358 (interactive)
359 (insert "IMPLEMENTATION MODULE ")
360 (let ((name (read-string "Name: ")))
361 (insert name ";\n\n\n\nEND " name ".\n")
362 (previous-line 3)
363 (m2-header)
364 (m2-type)
365 (newline)
366 (m2-var)
367 (newline)
368 (m2-begin)
369 (m2-begin-comment)
370 (insert " Module " name " Initialisation Code "))
371 (m2-end-comment)
372 (newline)
373 (m2-tab))
374
375 (defun m2-or ()
376 (interactive)
377 (m2-newline)
378 (backward-delete-char-untabify m2-indent)
379 (insert "|")
380 (m2-newline)
381 (m2-tab))
382
383 (defun m2-procedure ()
384 (interactive)
385 (insert "PROCEDURE ")
386 (let ((name (read-string "Name: " ))
387 args)
388 (insert name " (")
389 (insert (read-string "Arguments: ") ")")
390 (setq args (read-string "Result Type: "))
391 (if (not (string-equal args ""))
392 (insert " : " args))
393 (insert ";")
394 (m2-newline)
395 (insert "BEGIN")
396 (m2-newline)
397 (m2-newline)
398 (insert "END ")
399 (insert name)
400 (insert ";")
401 (end-of-line 0)
402 (m2-tab)))
403
404 (defun m2-with ()
405 (interactive)
406 (insert "WITH ")
407 (let ((name (read-string "Record-Type: ")))
408 (insert name)
409 (insert " DO")
410 (m2-newline)
411 (m2-newline)
412 (insert "END (* with " name " *);"))
413 (end-of-line 0)
414 (m2-tab))
415
416 (defun m2-record ()
417 (interactive)
418 (insert "RECORD")
419 (m2-newline)
420 (m2-newline)
421 (insert "END (* record *);")
422 (end-of-line 0)
423 (m2-tab))
424
425 (defun m2-stdio ()
426 (interactive)
427 (insert "
428 FROM TextIO IMPORT
429 WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,
430 WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,
431 WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,
432 WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,
433 WriteString, ReadString, WhiteSpace, EndOfLine;
434
435 FROM SysStreams IMPORT sysIn, sysOut, sysErr;
436
437 "))
438
439 (defun m2-type ()
440 (interactive)
441 (insert "TYPE")
442 (m2-newline)
443 (m2-tab))
444
445 (defun m2-until ()
446 (interactive)
447 (insert "REPEAT")
448 (m2-newline)
449 (m2-newline)
450 (insert "UNTIL ")
451 (insert (read-string "<boolean-expression>: ") ";")
452 (end-of-line 0)
453 (m2-tab))
454
455 (defun m2-var ()
456 (interactive)
457 (m2-newline)
458 (insert "VAR")
459 (m2-newline)
460 (m2-tab))
461
462 (defun m2-while ()
463 (interactive)
464 (insert "WHILE ")
465 (let ((name (read-string "<boolean-expression>: ")))
466 (insert name " DO" )
467 (m2-newline)
468 (m2-newline)
469 (insert "END (* while " name " *);"))
470 (end-of-line 0)
471 (m2-tab))
472
473 (defun m2-export ()
474 (interactive)
475 (insert "EXPORT QUALIFIED "))
476
477 (defun m2-import ()
478 (interactive)
479 (insert "FROM ")
480 (insert (read-string "Module: "))
481 (insert " IMPORT "))
482
483 (defun m2-begin-comment ()
484 (interactive)
485 (if (not (bolp))
486 (indent-to comment-column 0))
487 (insert "(* "))
488
489 (defun m2-end-comment ()
490 (interactive)
491 (if (not (bolp))
492 (indent-to m2-end-comment-column))
493 (insert "*)"))
494
495 (defun m2-compile ()
496 (interactive)
497 (compile (concat m2-compile-command " " (buffer-name))))
498
499 (defun m2-link ()
500 (interactive)
501 (if m2-link-name
502 (compile (concat m2-link-command " " m2-link-name))
503 (compile (concat m2-link-command " "
504 (setq m2-link-name (read-string "Name of executable: "
505 (buffer-name)))))))
506
507 (defun m2-execute-monitor-command (command)
508 (let* ((shell shell-file-name)
509 (csh (equal (file-name-nondirectory shell) "csh")))
510 (call-process shell nil t t "-cf" (concat "exec " command))))
511
512 (defun m2-visit ()
513 (interactive)
514 (let ((deffile nil)
515 (modfile nil)
516 modulename)
517 (save-excursion
518 (setq modulename
519 (read-string "Module name: "))
520 (switch-to-buffer "*Command Execution*")
521 (m2-execute-monitor-command (concat "m2whereis " modulename))
522 (goto-char (point-min))
523 (condition-case ()
524 (progn (re-search-forward "\\(.*\\.def\\) *$")
525 (setq deffile (buffer-substring (match-beginning 1)
526 (match-end 1))))
527 (search-failed ()))
528 (condition-case ()
529 (progn (re-search-forward "\\(.*\\.mod\\) *$")
530 (setq modfile (buffer-substring (match-beginning 1)
531 (match-end 1))))
532 (search-failed ()))
533 (if (not (or deffile modfile))
534 (error "I can find neither definition nor implementation of %s"
535 modulename)))
536 (cond (deffile
537 (find-file deffile)
538 (if modfile
539 (save-excursion
540 (find-file modfile))))
541 (modfile
542 (find-file modfile)))))
543
544 (defun m2-toggle ()
545 "Toggle between .mod and .def files for the module."
546 (interactive)
547 (cond ((string-equal (substring (buffer-name) -4) ".def")
548 (find-file-other-window
549 (concat (substring (buffer-name) 0 -4) ".mod")))
550 ((string-equal (substring (buffer-name) -4) ".mod")
551 (find-file-other-window
552 (concat (substring (buffer-name) 0 -4) ".def")))
553 ((string-equal (substring (buffer-name) -3) ".mi")
554 (find-file-other-window
555 (concat (substring (buffer-name) 0 -3) ".md")))
556 ((string-equal (substring (buffer-name) -3) ".md")
557 (find-file-other-window
558 (concat (substring (buffer-name) 0 -3) ".mi")))))
559
560 ;;; modula2.el ends here