1 ;;; modula2.el --- Modula-2 editing support package
3 ;; Author: Michael Schmidt <michael@pbinfo.UUCP>
4 ;; Tom Perrine <Perrin@LOGICON.ARPA>
8 ;; This file is part of GNU Emacs.
10 ;; The authors distributed this without a copyright notice
11 ;; back in 1988, so it is in the public domain. The original included
12 ;; the following credit:
15 ;; amended Peter Robinson
19 ;; A major mode for editing Modula-2 code. It provides convenient abbrevs
20 ;; for Modula-2 keywords, knows about the standard layout rules, and supports
21 ;; a native compile command.
28 "Major mode for editing Modula-2 code."
29 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
33 ;;; Added by Tom Perrine (TEP)
34 (defvar m2-mode-syntax-table
35 (let ((table (make-syntax-table)))
36 (modify-syntax-entry ?
\\ "\\" table
)
37 (modify-syntax-entry ?
/ ". 12" table
)
38 (modify-syntax-entry ?
\n ">" table
)
39 (modify-syntax-entry ?\
( "()1" table
)
40 (modify-syntax-entry ?\
) ")(4" table
)
41 (modify-syntax-entry ?
* ". 23nb" table
)
42 (modify-syntax-entry ?
+ "." table
)
43 (modify-syntax-entry ?-
"." table
)
44 (modify-syntax-entry ?
= "." table
)
45 (modify-syntax-entry ?%
"." table
)
46 (modify-syntax-entry ?
< "." table
)
47 (modify-syntax-entry ?
> "." table
)
48 (modify-syntax-entry ?
\' "\"" table
)
50 "Syntax table in use in Modula-2 buffers.")
52 (defcustom m2-compile-command
"m2c"
53 "Command to compile Modula-2 programs."
57 (defcustom m2-link-command
"m2l"
58 "Command to link Modula-2 programs."
62 (defcustom m2-link-name nil
63 "Name of the Modula-2 executable."
64 :type
'(choice (const nil
) string
)
67 (defcustom m2-end-comment-column
75
68 "*Column for aligning the end of a comment, in Modula-2."
74 (let ((map (make-sparse-keymap)))
75 ;; FIXME: Many of those bindings are contrary to coding conventions.
76 (define-key map
"\C-cb" 'm2-begin
)
77 (define-key map
"\C-cc" 'm2-case
)
78 (define-key map
"\C-cd" 'm2-definition
)
79 (define-key map
"\C-ce" 'm2-else
)
80 (define-key map
"\C-cf" 'm2-for
)
81 (define-key map
"\C-ch" 'm2-header
)
82 (define-key map
"\C-ci" 'm2-if
)
83 (define-key map
"\C-cm" 'm2-module
)
84 (define-key map
"\C-cl" 'm2-loop
)
85 (define-key map
"\C-co" 'm2-or
)
86 (define-key map
"\C-cp" 'm2-procedure
)
87 (define-key map
"\C-c\C-w" 'm2-with
)
88 (define-key map
"\C-cr" 'm2-record
)
89 (define-key map
"\C-cs" 'm2-stdio
)
90 (define-key map
"\C-ct" 'm2-type
)
91 (define-key map
"\C-cu" 'm2-until
)
92 (define-key map
"\C-cv" 'm2-var
)
93 (define-key map
"\C-cw" 'm2-while
)
94 (define-key map
"\C-cx" 'm2-export
)
95 (define-key map
"\C-cy" 'm2-import
)
96 (define-key map
"\C-c{" 'm2-begin-comment
)
97 (define-key map
"\C-c}" 'm2-end-comment
)
98 (define-key map
"\C-c\C-z" 'suspend-emacs
)
99 (define-key map
"\C-c\C-v" 'm2-visit
)
100 (define-key map
"\C-c\C-t" 'm2-toggle
)
101 (define-key map
"\C-c\C-l" 'm2-link
)
102 (define-key map
"\C-c\C-c" 'm2-compile
)
104 "Keymap used in Modula-2 mode.")
106 (defcustom m2-indent
5
107 "*This variable gives the indentation in Modula-2-Mode."
110 (put 'm2-indent
'safe-local-variable
111 (lambda (v) (or (null v
) (integerp v
))))
113 (defconst m2-smie-grammar
114 ;; An official definition can be found as "M2R10.pdf". This grammar does
115 ;; not really follow it, for lots of technical reasons, but it can still be
116 ;; useful to refer to it.
120 '((range) (id) (epsilon)
121 (fields (fields ";" fields
) (ids ":" type
))
122 (proctype (id ":" type
))
123 (type ("RECORD" fields
"END")
124 ("POINTER" "TO" type
)
125 ;; The PROCEDURE type is indistinguishable from the beginning
126 ;; of a PROCEDURE definition, so we need a "PROCEDURE-type" to
127 ;; prevent SMIE from trying to find the matching END.
128 ("PROCEDURE-type" proctype
)
129 ;; OF's right hand side should bind tighter than ; for array
130 ;; types, but should bind less tight than | which itself binds
131 ;; less tight than ;. So we use two distinct OFs.
133 ("ARRAY" range
"OF-type" type
))
134 (args ("(" fargs
")"))
135 ;; VAR has lower precedence than ";" in formal args, but not
136 ;; in declarations. So we use "VAR-arg" for the formal arg case.
137 (farg (ids ":" type
) ("CONST-arg" farg
) ("VAR-arg" farg
))
138 (fargs (fargs ";" fargs
) (farg))
139 ;; Handling of PROCEDURE in decls is problematic: we'd want
140 ;; TYPE/CONST/VAR/PROCEDURE's parent to be any previous
141 ;; CONST/TYPE/VAR/PROCEDURE, but we also want PROCEDURE to be an opener
142 ;; (so that its END has PROCEDURE as its parent). So instead, we treat
143 ;; the last ";" in those blocks as a separator (we call it ";-block").
144 ;; FIXME: This means that "TYPE \n VAR" is not indented properly
145 ;; because there's no ";-block" between the two.
146 (decls (decls ";-block" decls
)
147 ("TYPE" typedecls
) ("CONST" constdecls
) ("VAR" vardecls
)
148 ;; END is usually a closer, but not quite for PROCEDURE...END.
149 ;; We could use "END-proc" for the procedure case, but
150 ;; I preferred to just pretend PROCEDURE's END is the closer.
151 ("PROCEDURE" decls
"BEGIN" insts
"END") ;END-proc id
152 ("PROCEDURE" decls
"BEGIN" insts
"FINALLY" insts
"END")
153 ("PROCEDURE" decls
"FORWARD")
154 ;; ("IMPLEMENTATION" epsilon "MODULE" decls
155 ;; "BEGIN" insts "FINALLY" insts "END")
157 (typedecls (typedecls ";" typedecls
) (id "=" type
))
159 (vardecls (vardecls ";" vardecls
) (ids ":" type
))
160 (constdecls (constdecls ";" constdecls
) (id "=" exp
))
161 (exp (id "-anchor-" id
) ("(" exp
")"))
162 (caselabel (caselabel ".." caselabel
) (caselabel "," caselabel
))
163 ;; : for types binds tighter than ;, but the : for case labels binds
164 ;; less tight, so have to use two different :.
165 (cases (cases "|" cases
) (caselabel ":-case" insts
))
166 (forspec (exp "TO" exp
))
167 (insts (insts ";" insts
)
169 ("CASE" exp
"OF" cases
"END")
170 ("CASE" exp
"OF" cases
"ELSE" insts
"END")
172 ("WITH" exp
"DO" insts
"END")
173 ("REPEAT" insts
"UNTIL" exp
)
174 ("WHILE" exp
"DO" insts
"END")
175 ("FOR" forspec
"DO" insts
"END")
176 ("IF" exp
"THEN" insts
"END")
177 ("IF" exp
"THEN" insts
"ELSE" insts
"END")
178 ("IF" exp
"THEN" insts
179 "ELSIF" exp
"THEN" insts
"ELSE" insts
"END")
180 ("IF" exp
"THEN" insts
181 "ELSIF" exp
"THEN" insts
182 "ELSIF" exp
"THEN" insts
"ELSE" insts
"END"))
183 ;; This category is not used anywhere, but it adds some constraints that
184 ;; try to reduce the harm when an OF-type is not properly recognized.
185 (error-OF ("ARRAY" range
"OF" type
) ("SET" "OF" id
)))
186 '((assoc ";")) '((assoc ";-block")) '((assoc "|"))
188 '((assoc ",") (assoc ".."))
189 ;; '((assoc "TYPE" "CONST" "VAR" "PROCEDURE"))
192 '((nonassoc "-anchor-" "=")
193 (nonassoc "<" "<=" ">=" ">" "<>" "#" "IN")
195 (assoc "AND" "MOD" "DIV" "REM" "*" "/" "&")
201 (defun m2-smie-refine-colon ()
204 (let ((tok (smie-default-backward-token)))
206 ((zerop (length tok
))
207 (let ((forward-sexp-function nil
))
210 (scan-error (setq res
":")))))
211 ((member tok
'("|" "OF" "..")) (setq res
":-case"))
212 ((member tok
'(":" "END" ";" "BEGIN" "VAR" "RECORD" "PROCEDURE"))
216 (defun m2-smie-refine-of ()
217 (let ((tok (smie-default-backward-token)))
218 (when (zerop (length tok
))
219 (let ((forward-sexp-function nil
))
223 (setq tok
(smie-default-backward-token))))
224 (if (member tok
'("ARRAY" "SET"))
227 (defun m2-smie-refine-semi ()
228 (forward-comment (point-max))
229 (if (looking-at (regexp-opt '("PROCEDURE" "TYPE" "VAR" "CONST" "BEGIN")))
232 ;; FIXME: "^." are two tokens, not one.
233 (defun m2-smie-forward-token ()
234 (pcase (smie-default-forward-token)
235 (`"VAR" (if (zerop (car (syntax-ppss))) "VAR" "VAR-arg"))
236 (`"CONST" (if (zerop (car (syntax-ppss))) "CONST" "CONST-arg"))
237 (`";" (save-excursion (m2-smie-refine-semi)))
238 (`"OF" (save-excursion (forward-char -
2) (m2-smie-refine-of)))
239 (`":" (save-excursion (forward-char -
1) (m2-smie-refine-colon)))
240 ;; (`"END" (if (and (looking-at "[ \t\n]*\\(\\(?:\\sw\\|\\s_\\)+\\)")
241 ;; (not (assoc (match-string 1) m2-smie-grammar)))
242 ;; "END-proc" "END"))
245 (defun m2-smie-backward-token ()
246 (pcase (smie-default-backward-token)
247 (`"VAR" (if (zerop (car (syntax-ppss))) "VAR" "VAR-arg"))
248 (`"CONST" (if (zerop (car (syntax-ppss))) "CONST" "CONST-arg"))
249 (`";" (save-excursion (forward-char 1) (m2-smie-refine-semi)))
250 (`"OF" (save-excursion (m2-smie-refine-of)))
251 (`":" (save-excursion (m2-smie-refine-colon)))
252 ;; (`"END" (if (and (looking-at "\\sw+[ \t\n]+\\(\\(?:\\sw\\|\\s_\\)+\\)")
253 ;; (not (assoc (match-string 1) m2-smie-grammar)))
254 ;; "END-proc" "END"))
257 (defun m2-smie-rules (kind token
)
258 ;; FIXME: Apparently, the usual indentation convention is something like:
269 ;; This is inconsistent with the actual structure of the code in 2 ways:
270 ;; - The inner VAR/TYPE are indented just like the outer VAR/TYPE.
271 ;; - The inner PROCEDURE is not aligned with its VAR/TYPE siblings.
272 (pcase (cons kind token
)
273 (`(:elem . basic
) m2-indent
)
274 (`(:after .
":=") (or m2-indent smie-indent-basic
))
275 (`(:after .
,(or `"CONST" `"VAR" `"TYPE"))
276 (or m2-indent smie-indent-basic
))
277 ;; (`(:before . ,(or `"VAR" `"TYPE" `"CONST"))
278 ;; (if (smie-rule-parent-p "PROCEDURE") 0))
279 (`(:after .
";-block")
280 (if (smie-rule-parent-p "PROCEDURE")
281 (smie-rule-parent (or m2-indent smie-indent-basic
))))
282 (`(:before .
"|") (smie-rule-separator kind
))
286 (defalias 'modula-2-mode
'm2-mode
)
288 (define-derived-mode m2-mode prog-mode
"Modula-2"
289 "This is a mode intended to support program development in Modula-2.
290 All control constructs of Modula-2 can be reached by typing C-c
291 followed by the first character of the construct.
293 \\[m2-begin] begin \\[m2-case] case
294 \\[m2-definition] definition \\[m2-else] else
295 \\[m2-for] for \\[m2-header] header
296 \\[m2-if] if \\[m2-module] module
297 \\[m2-loop] loop \\[m2-or] or
298 \\[m2-procedure] procedure Control-c Control-w with
299 \\[m2-record] record \\[m2-stdio] stdio
300 \\[m2-type] type \\[m2-until] until
301 \\[m2-var] var \\[m2-while] while
302 \\[m2-export] export \\[m2-import] import
303 \\[m2-begin-comment] begin-comment \\[m2-end-comment] end-comment
304 \\[suspend-emacs] suspend Emacs \\[m2-toggle] toggle
305 \\[m2-compile] compile \\[m2-next-error] next-error
308 `m2-indent' controls the number of spaces for each indentation.
309 `m2-compile-command' holds the command to compile a Modula-2 program.
310 `m2-link-command' holds the command to link a Modula-2 program."
311 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
312 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
313 (set (make-local-variable 'paragraph-ignore-fill-prefix
) t
)
314 (set (make-local-variable 'comment-start
) "(* ")
315 (set (make-local-variable 'comment-end
) " *)")
316 (set (make-local-variable 'comment-start-skip
) "\\(?:(\\*+\\|//+\\) *")
317 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
318 (set (make-local-variable 'font-lock-defaults
)
319 '((m3-font-lock-keywords
320 m3-font-lock-keywords-1 m3-font-lock-keywords-2
)
321 nil nil
((?_ .
"w") (?. .
"w") (?
< .
". 1") (?
> .
". 4")) nil
323 (smie-setup m2-smie-grammar
#'m2-smie-rules
324 :forward-token
#'m2-smie-forward-token
325 :backward-token
#'m2-smie-backward-token
))
327 ;; Regexps written with help from Ron Forrester <ron@orcad.com>
328 ;; and Spencer Allain <sallain@teknowledge.com>.
329 (defconst m3-font-lock-keywords-1
332 ;; Module definitions.
333 ("\\<\\(INTERFACE\\|MODULE\\|PROCEDURE\\)\\>[ \t]*\\(\\sw+\\)?"
334 (1 font-lock-keyword-face
) (2 font-lock-function-name-face nil t
))
336 ;; Import directives.
337 ("\\<\\(EXPORTS\\|FROM\\|IMPORT\\)\\>"
338 (1 font-lock-keyword-face
)
339 (font-lock-match-c-style-declaration-item-and-skip-to-next
340 nil
(goto-char (match-end 0))
341 (1 font-lock-constant-face
)))
343 ;; Pragmas as warnings.
344 ;; Spencer Allain <sallain@teknowledge.com> says do them as comments...
345 ;; ("<\\*.*\\*>" . font-lock-warning-face)
346 ;; ... but instead we fontify the first word.
347 ("<\\*[ \t]*\\(\\sw+\\)" 1 font-lock-warning-face prepend
)
349 "Subdued level highlighting for Modula-3 modes.")
351 (defconst m3-font-lock-keywords-2
352 (append m3-font-lock-keywords-1
356 '("INTEGER" "BITS" "BOOLEAN" "CARDINAL" "CHAR" "FLOAT" "REAL"
357 "LONGREAL" "REFANY" "ADDRESS" "ARRAY" "SET" "TEXT"
358 "MUTEX" "ROOT" "EXTENDED")))
361 '("AND" "ANY" "AS" "BEGIN" "BRANDED" "BY" "CASE" "CONST" "DIV"
362 "DO" "ELSE" "ELSIF" "EVAL" "EXCEPT" "EXIT" "FINALLY"
363 "FOR" "GENERIC" "IF" "IN" "LOCK" "LOOP" "METHODS" "MOD" "NOT"
364 "OBJECT" "OF" "OR" "OVERRIDES" "READONLY" "RECORD" "REF"
365 "REPEAT" "RETURN" "REVEAL" "THEN" "TO" "TRY"
366 "TYPE" "TYPECASE" "UNSAFE" "UNTIL" "UNTRACED" "VAR" "VALUE"
370 '("ABS" "ADR" "ADRSIZE" "BITSIZE" "BYTESIZE" "CEILING"
371 "DEC" "DISPOSE" "FIRST" "FLOOR" "INC" "ISTYPE" "LAST"
372 "LOOPHOLE" "MAX" "MIN" "NARROW" "NEW" "NUMBER" "ORD"
373 "ROUND" "SUBARRAY" "TRUNC" "TYPECODE" "VAL")))
377 ;; Keywords except those fontified elsewhere.
378 (concat "\\<\\(" m3-keywords
"\\)\\>")
381 (cons (concat "\\<\\(" m3-builtins
"\\)\\>") 'font-lock-builtin-face
)
384 (cons (concat "\\<\\(" m3-types
"\\)\\>") 'font-lock-type-face
)
386 ;; Fontify tokens as function names.
387 '("\\<\\(END\\|EXCEPTION\\|RAISES?\\)\\>[ \t{]*"
388 (1 font-lock-keyword-face
)
389 (font-lock-match-c-style-declaration-item-and-skip-to-next
390 nil
(goto-char (match-end 0))
391 (1 font-lock-function-name-face
)))
393 ;; Fontify constants as references.
394 '("\\<\\(FALSE\\|NIL\\|NULL\\|TRUE\\)\\>" . font-lock-constant-face
)
396 "Gaudy level highlighting for Modula-3 modes.")
398 (defvar m3-font-lock-keywords m3-font-lock-keywords-1
399 "Default expressions to highlight in Modula-3 modes.")
401 ;; We don't actually have different keywords for Modula-2. Volunteers?
402 (defconst m2-font-lock-keywords-1 m3-font-lock-keywords-1
403 "Subdued level highlighting for Modula-2 modes.")
405 (defconst m2-font-lock-keywords-2 m3-font-lock-keywords-2
406 "Gaudy level highlighting for Modula-2 modes.")
408 (defvar m2-font-lock-keywords m2-font-lock-keywords-1
409 "Default expressions to highlight in Modula-2 modes.")
411 (define-skeleton m2-begin
412 "Insert a BEGIN keyword and indent for the next line."
416 (define-skeleton m2-case
417 "Build skeleton CASE statement, prompting for the <expression>."
419 \n "CASE " str
" OF" > \n _
\n "END (* " str
" *);" > \n)
421 (define-skeleton m2-definition
422 "Build skeleton DEFINITION MODULE, prompting for the <module name>."
424 \n "DEFINITION MODULE " str
";" > \n \n _
\n \n "END " str
"." > \n)
426 (define-skeleton m2-else
427 "Insert ELSE keyword and indent for next line."
431 (define-skeleton m2-for
432 "Build skeleton FOR loop statement, prompting for the loop parameters."
434 ;; FIXME: this seems to be lacking a "<var> :=".
436 (setq v1
(read-string "Limit: "))
437 (let ((by (read-string "Step: ")))
438 (if (not (string-equal by
""))
440 " DO" > \n _
\n "END (* for " str
" to " v1
" *);" > \n)
442 (define-skeleton m2-header
443 "Insert a comment block containing the module title, author, etc."
446 "\n Created: \t" (current-time-string)
447 "\n Author: \t" (user-full-name) " <" user-mail-address
">\n"
450 (define-skeleton m2-if
451 "Insert skeleton IF statement, prompting for <boolean-expression>."
452 "<boolean-expression>: "
453 \n "IF " str
" THEN" > \n _
\n "END (* if " str
" *);" > \n)
455 (define-skeleton m2-loop
456 "Build skeleton LOOP (with END)."
458 \n "LOOP" > \n _
\n "END (* loop *);" > \n)
460 (define-skeleton m2-module
461 "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
463 \n "IMPLEMENTATION MODULE " str
";" > \n \n
469 " Module " str
" Initialisation Code "
471 \n \n "END " str
"." > \n)
473 (define-skeleton m2-or
478 (define-skeleton m2-procedure
481 \n "PROCEDURE " str
" (" (read-string "Arguments: ") ")"
482 (let ((args (read-string "Result Type: ")))
483 (if (not (equal args
"")) (concat " : " args
)))
484 ";" > \n "BEGIN" > \n _
\n "END " str
";" > \n)
486 (define-skeleton m2-with
489 \n "WITH " str
" DO" > \n _
\n "END (* with " str
" *);" > \n)
491 (define-skeleton m2-record
494 \n "RECORD" > \n _
\n "END (* record *);" > \n)
496 (define-skeleton m2-stdio
499 \n "FROM TextIO IMPORT"
500 > \n "WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,"
501 > \n "WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,"
502 > \n "WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,"
503 > \n "WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,"
504 > \n "WriteString, ReadString, WhiteSpace, EndOfLine;"
505 > \n \n "FROM SysStreams IMPORT sysIn, sysOut, sysErr;" > \n \n)
507 (define-skeleton m2-type
510 \n "TYPE" > \n ";" > \n)
512 (define-skeleton m2-until
514 "<boolean-expression>: "
515 \n "REPEAT" > \n _
\n "UNTIL " str
";" > \n)
517 (define-skeleton m2-var
520 \n "VAR" > \n ";" > \n)
522 (define-skeleton m2-while
524 "<boolean-expression>: "
525 \n "WHILE " str
" DO" > \n _
\n "END (* while " str
" *);" > \n)
527 (define-skeleton m2-export
530 \n "EXPORT QUALIFIED " > _
\n)
532 (define-skeleton m2-import
535 \n "FROM " str
" IMPORT " > _
\n)
537 (defun m2-begin-comment ()
540 (indent-to comment-column
0))
543 (defun m2-end-comment ()
546 (indent-to m2-end-comment-column
))
551 (compile (concat m2-compile-command
" " (buffer-name))))
555 (compile (concat m2-link-command
" "
557 (setq m2-link-name
(read-string "Name of executable: "
560 (defun m2-execute-monitor-command (command)
561 (let* ((shell shell-file-name
)
562 ;; (csh (equal (file-name-nondirectory shell) "csh"))
564 (call-process shell nil t t
"-cf" (concat "exec " command
))))
573 (read-string "Module name: "))
574 (switch-to-buffer "*Command Execution*")
575 (m2-execute-monitor-command (concat "m2whereis " modulename
))
576 (goto-char (point-min))
578 (progn (re-search-forward "\\(.*\\.def\\) *$")
579 (setq deffile
(buffer-substring (match-beginning 1)
583 (progn (re-search-forward "\\(.*\\.mod\\) *$")
584 (setq modfile
(buffer-substring (match-beginning 1)
587 (if (not (or deffile modfile
))
588 (error "I can find neither definition nor implementation of %s"
594 (find-file modfile
))))
596 (find-file modfile
)))))
599 "Toggle between .mod and .def files for the module."
601 (cond ((string-equal (substring (buffer-name) -
4) ".def")
602 (find-file-other-window
603 (concat (substring (buffer-name) 0 -
4) ".mod")))
604 ((string-equal (substring (buffer-name) -
4) ".mod")
605 (find-file-other-window
606 (concat (substring (buffer-name) 0 -
4) ".def")))
607 ((string-equal (substring (buffer-name) -
3) ".mi")
608 (find-file-other-window
609 (concat (substring (buffer-name) 0 -
3) ".md")))
610 ((string-equal (substring (buffer-name) -
3) ".md")
611 (find-file-other-window
612 (concat (substring (buffer-name) 0 -
3) ".mi")))))
616 ;;; modula2.el ends here