1 ;;; modula2.el --- Modula-2 editing support package
4 ; amended Peter Robinson
5 ; ported to GNU Michael Schmidt
6 ;;;From: "Michael Schmidt" <michael@pbinfo.UUCP>
7 ;;;Modified by Tom Perrine <Perrin@LOGICON.ARPA> (TEP)
11 (defvar m2-mode-syntax-table nil
12 "Syntax table in use in Modula-2 buffers.")
14 (defvar m2-compile-command
"m2c"
15 "Command to compile Modula-2 programs")
17 (defvar m2-link-command
"m2l"
18 "Command to link Modula-2 programs")
20 (defvar m2-link-name nil
21 "Name of the executable.")
24 (if m2-mode-syntax-table
26 (let ((table (make-syntax-table)))
27 (modify-syntax-entry ?
\\ "\\" table
)
28 (modify-syntax-entry ?\
( ". 1" table
)
29 (modify-syntax-entry ?\
) ". 4" table
)
30 (modify-syntax-entry ?
* ". 23" table
)
31 (modify-syntax-entry ?
+ "." table
)
32 (modify-syntax-entry ?-
"." table
)
33 (modify-syntax-entry ?
= "." table
)
34 (modify-syntax-entry ?%
"." table
)
35 (modify-syntax-entry ?
< "." table
)
36 (modify-syntax-entry ?
> "." table
)
37 (modify-syntax-entry ?
\' "\"" table
)
38 (setq m2-mode-syntax-table table
)))
41 (defvar m2-mode-map nil
42 "Keymap used in Modula-2 mode.")
45 (let ((map (make-sparse-keymap)))
46 (define-key map
"\^i" 'm2-tab
)
47 (define-key map
"\C-cb" 'm2-begin
)
48 (define-key map
"\C-cc" 'm2-case
)
49 (define-key map
"\C-cd" 'm2-definition
)
50 (define-key map
"\C-ce" 'm2-else
)
51 (define-key map
"\C-cf" 'm2-for
)
52 (define-key map
"\C-ch" 'm2-header
)
53 (define-key map
"\C-ci" 'm2-if
)
54 (define-key map
"\C-cm" 'm2-module
)
55 (define-key map
"\C-cl" 'm2-loop
)
56 (define-key map
"\C-co" 'm2-or
)
57 (define-key map
"\C-cp" 'm2-procedure
)
58 (define-key map
"\C-c\C-w" 'm2-with
)
59 (define-key map
"\C-cr" 'm2-record
)
60 (define-key map
"\C-cs" 'm2-stdio
)
61 (define-key map
"\C-ct" 'm2-type
)
62 (define-key map
"\C-cu" 'm2-until
)
63 (define-key map
"\C-cv" 'm2-var
)
64 (define-key map
"\C-cw" 'm2-while
)
65 (define-key map
"\C-cx" 'm2-export
)
66 (define-key map
"\C-cy" 'm2-import
)
67 (define-key map
"\C-c{" 'm2-begin-comment
)
68 (define-key map
"\C-c}" 'm2-end-comment
)
69 (define-key map
"\C-j" 'm2-newline
)
70 (define-key map
"\C-c\C-z" 'suspend-emacs
)
71 (define-key map
"\C-c\C-v" 'm2-visit
)
72 (define-key map
"\C-c\C-t" 'm2-toggle
)
73 (define-key map
"\C-c\C-l" 'm2-link
)
74 (define-key map
"\C-c\C-c" 'm2-compile
)
75 (setq m2-mode-map map
)))
77 (defvar m2-indent
5 "*This variable gives the indentation in Modula-2-Mode")
80 (defun modula-2-mode ()
81 "This is a mode intended to support program development in Modula-2.
82 All control constructs of Modula-2 can be reached by typing C-c
83 followed by the first character of the construct.
85 \\[m2-begin] begin \\[m2-case] case
86 \\[m2-definition] definition \\[m2-else] else
87 \\[m2-for] for \\[m2-header] header
88 \\[m2-if] if \\[m2-module] module
89 \\[m2-loop] loop \\[m2-or] or
90 \\[m2-procedure] procedure Control-c Control-w with
91 \\[m2-record] record \\[m2-stdio] stdio
92 \\[m2-type] type \\[m2-until] until
93 \\[m2-var] var \\[m2-while] while
94 \\[m2-export] export \\[m2-import] import
95 \\[m2-begin-comment] begin-comment \\[m2-end-comment] end-comment
96 \\[suspend-emacs] suspend Emacs \\[m2-toggle] toggle
97 \\[m2-compile] compile \\[m2-next-error] next-error
100 `m2-indent' controls the number of spaces for each indentation.
101 `m2-compile-command' holds the command to compile a Modula-2 program.
102 `m2-link-command' holds the command to link a Modula-2 program."
104 (kill-all-local-variables)
105 (use-local-map m2-mode-map
)
106 (setq major-mode
'modula-2-mode
)
107 (setq mode-name
"Modula-2")
108 (make-local-variable 'comment-column
)
109 (setq comment-column
41)
110 (make-local-variable 'end-comment-column
)
111 (setq end-comment-column
75)
112 (set-syntax-table m2-mode-syntax-table
)
113 (make-local-variable 'paragraph-start
)
114 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
115 (make-local-variable 'paragraph-separate
)
116 (setq paragraph-separate paragraph-start
)
117 (make-local-variable 'paragraph-ignore-fill-prefix
)
118 (setq paragraph-ignore-fill-prefix t
)
119 ; (make-local-variable 'indent-line-function)
120 ; (setq indent-line-function 'c-indent-line)
121 (make-local-variable 'require-final-newline
)
122 (setq require-final-newline t
)
123 (make-local-variable 'comment-start
)
124 (setq comment-start
"(* ")
125 (make-local-variable 'comment-end
)
126 (setq comment-end
" *)")
127 (make-local-variable 'comment-column
)
128 (setq comment-column
41)
129 (make-local-variable 'comment-start-skip
)
130 (setq comment-start-skip
"/\\*+ *")
131 (make-local-variable 'comment-indent-hook
)
132 (setq comment-indent-hook
'c-comment-indent
)
133 (make-local-variable 'parse-sexp-ignore-comments
)
134 (setq parse-sexp-ignore-comments t
)
135 (run-hooks 'm2-mode-hook
))
138 "Insert a newline and indent following line like previous line."
140 (let ((hpos (current-indentation)))
145 "Indent to next tab stop."
147 (indent-to (* (1+ (/ (current-indentation) m2-indent
)) m2-indent
)))
150 "Insert a BEGIN keyword and indent for the next line."
157 "Build skeleton CASE statment, prompting for the <expression>."
159 (let ((name (read-string "Case-Expression: ")))
160 (insert "CASE " name
" OF")
163 (insert "END (* case " name
" *);"))
167 (defun m2-definition ()
168 "Build skeleton DEFINITION MODULE, prompting for the <module name>."
170 (insert "DEFINITION MODULE ")
171 (let ((name (read-string "Name: ")))
172 (insert name
";\n\n\n\nEND " name
".\n"))
176 "Insert ELSE keyword and indent for next line."
179 (backward-delete-char-untabify m2-indent
())
185 "Build skeleton FOR loop statment, prompting for the loop parameters."
188 (let ((name (read-string "Loop Initialiser: ")) limit by
)
190 (setq limit
(read-string "Limit: "))
192 (setq by
(read-string "Step: "))
193 (if (not (string-equal by
""))
198 (insert "END (* for " name
" to " limit
" *);"))
203 "Insert a comment block containing the module title, author, etc."
205 (insert "(*\n Title: \t")
206 (insert (read-string "Title: "))
207 (insert "\n Created:\t")
208 (insert (current-time-string))
209 (insert "\n Author: \t")
210 (insert (user-full-name))
211 (insert (concat "\n\t\t<" (user-login-name) "@" (system-name) ">\n"))
215 "Insert skeleton IF statment, prompting for <boolean-expression>."
218 (let ((thecondition (read-string "<boolean-expression>: ")))
219 (insert thecondition
" THEN")
222 (insert "END (* if " thecondition
" *);"))
227 "Build skeleton LOOP (with END)."
232 (insert "END (* loop *);")
237 "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
239 (insert "IMPLEMENTATION MODULE ")
240 (let ((name (read-string "Name: ")))
241 (insert name
";\n\n\n\nEND " name
".\n")
250 (insert " Module " name
" Initialisation Code "))
258 (backward-delete-char-untabify m2-indent
)
263 (defun m2-procedure ()
265 (insert "PROCEDURE ")
266 (let ((name (read-string "Name: " ))
269 (insert (read-string "Arguments: ") ")")
270 (setq args
(read-string "Result Type: "))
271 (if (not (string-equal args
""))
287 (let ((name (read-string "Record-Type: ")))
292 (insert "END (* with " name
" *);"))
301 (insert "END (* record *);")
309 WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,
310 WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,
311 WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,
312 WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,
313 WriteString, ReadString, WhiteSpace, EndOfLine;
315 FROM SysStreams IMPORT sysIn, sysOut, sysErr;
331 (insert (read-string "<boolean-expression>: ") ";")
345 (let ((name (read-string "<boolean-expression>: ")))
349 (insert "END (* while " name
" *);"))
355 (insert "EXPORT QUALIFIED "))
360 (insert (read-string "Module: "))
363 (defun m2-begin-comment ()
366 (indent-to comment-column
0))
369 (defun m2-end-comment ()
372 (indent-to end-comment-column
))
377 (setq modulename
(buffer-name))
378 (compile (concat m2-compile-command
" " modulename
)))
382 (setq modulename
(buffer-name))
384 (compile (concat m2-link-command
" " m2-link-name
))
385 (compile (concat m2-link-command
" "
386 (setq m2-link-name
(read-string "Name of executable: "
389 (defun execute-monitor-command (command)
390 (let* ((shell shell-file-name
)
391 (csh (equal (file-name-nondirectory shell
) "csh")))
392 (call-process shell nil t t
"-cf" (concat "exec " command
))))
401 (read-string "Module name: "))
402 (switch-to-buffer "*Command Execution*")
403 (execute-monitor-command (concat "m2whereis " modulename
))
404 (goto-char (point-min))
406 (progn (re-search-forward "\\(.*\\.def\\) *$")
407 (setq deffile
(buffer-substring (match-beginning 1)
411 (progn (re-search-forward "\\(.*\\.mod\\) *$")
412 (setq modfile
(buffer-substring (match-beginning 1)
415 (if (not (or deffile modfile
))
416 (error "I can find neither definition nor implementation of %s"
422 (find-file modfile
))))
424 (find-file modfile
)))))
427 "Toggle between .mod and .def files for the module."
429 (cond ((string-equal (substring (buffer-name) -
4) ".def")
430 (find-file-other-window
431 (concat (substring (buffer-name) 0 -
4) ".mod")))
432 ((string-equal (substring (buffer-name) -
4) ".mod")
433 (find-file-other-window
434 (concat (substring (buffer-name) 0 -
4) ".def")))
435 ((string-equal (substring (buffer-name) -
3) ".mi")
436 (find-file-other-window
437 (concat (substring (buffer-name) 0 -
3) ".md")))
438 ((string-equal (substring (buffer-name) -
3) ".md")
439 (find-file-other-window
440 (concat (substring (buffer-name) 0 -
3) ".mi")))))
442 ;;; modula2.el ends here