*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / modula2.el
CommitLineData
6594deb0
ER
1;;; modula2.el --- Modula-2 editing support package
2
84176303
ER
3;; Author: Michael Schmidt <michael@pbinfo.UUCP>
4;; Tom Perrine <Perrin@LOGICON.ARPA>
5;; Last-Modified: 30 May 1992
fd7fa35a 6;; Keywords: languages
66f56525 7
84176303
ER
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:
66f56525 11
84176303
ER
12;; Author Mick Jordan
13;; amended Peter Robinson
14
15;;; Code:
16
17;;; Added by Tom Perrine (TEP)
66f56525 18(defvar m2-mode-syntax-table nil
5d11560e 19 "Syntax table in use in Modula-2 buffers.")
66f56525
RS
20
21(defvar m2-compile-command "m2c"
22 "Command to compile Modula-2 programs")
23
24(defvar m2-link-command "m2l"
25 "Command to link Modula-2 programs")
26
27(defvar m2-link-name nil
28 "Name of the executable.")
29
30
31(if m2-mode-syntax-table
32 ()
33 (let ((table (make-syntax-table)))
34 (modify-syntax-entry ?\\ "\\" table)
35 (modify-syntax-entry ?\( ". 1" table)
36 (modify-syntax-entry ?\) ". 4" table)
37 (modify-syntax-entry ?* ". 23" table)
38 (modify-syntax-entry ?+ "." table)
39 (modify-syntax-entry ?- "." table)
40 (modify-syntax-entry ?= "." table)
41 (modify-syntax-entry ?% "." table)
42 (modify-syntax-entry ?< "." table)
43 (modify-syntax-entry ?> "." table)
44 (modify-syntax-entry ?\' "\"" table)
45 (setq m2-mode-syntax-table table)))
46
47;;; Added by TEP
48(defvar m2-mode-map nil
49 "Keymap used in Modula-2 mode.")
50
51(if m2-mode-map ()
52 (let ((map (make-sparse-keymap)))
53 (define-key map "\^i" 'm2-tab)
54 (define-key map "\C-cb" 'm2-begin)
55 (define-key map "\C-cc" 'm2-case)
56 (define-key map "\C-cd" 'm2-definition)
57 (define-key map "\C-ce" 'm2-else)
58 (define-key map "\C-cf" 'm2-for)
59 (define-key map "\C-ch" 'm2-header)
60 (define-key map "\C-ci" 'm2-if)
61 (define-key map "\C-cm" 'm2-module)
62 (define-key map "\C-cl" 'm2-loop)
63 (define-key map "\C-co" 'm2-or)
64 (define-key map "\C-cp" 'm2-procedure)
65 (define-key map "\C-c\C-w" 'm2-with)
66 (define-key map "\C-cr" 'm2-record)
67 (define-key map "\C-cs" 'm2-stdio)
68 (define-key map "\C-ct" 'm2-type)
69 (define-key map "\C-cu" 'm2-until)
70 (define-key map "\C-cv" 'm2-var)
71 (define-key map "\C-cw" 'm2-while)
72 (define-key map "\C-cx" 'm2-export)
73 (define-key map "\C-cy" 'm2-import)
74 (define-key map "\C-c{" 'm2-begin-comment)
75 (define-key map "\C-c}" 'm2-end-comment)
76 (define-key map "\C-j" 'm2-newline)
77 (define-key map "\C-c\C-z" 'suspend-emacs)
78 (define-key map "\C-c\C-v" 'm2-visit)
79 (define-key map "\C-c\C-t" 'm2-toggle)
80 (define-key map "\C-c\C-l" 'm2-link)
81 (define-key map "\C-c\C-c" 'm2-compile)
82 (setq m2-mode-map map)))
83
84(defvar m2-indent 5 "*This variable gives the indentation in Modula-2-Mode")
85
f9f9507e 86;;;###autoload
66f56525 87(defun modula-2-mode ()
5d11560e
BP
88 "This is a mode intended to support program development in Modula-2.
89All control constructs of Modula-2 can be reached by typing C-c
90followed by the first character of the construct.
91\\<m2-mode-map>
92 \\[m2-begin] begin \\[m2-case] case
93 \\[m2-definition] definition \\[m2-else] else
94 \\[m2-for] for \\[m2-header] header
95 \\[m2-if] if \\[m2-module] module
96 \\[m2-loop] loop \\[m2-or] or
97 \\[m2-procedure] procedure Control-c Control-w with
98 \\[m2-record] record \\[m2-stdio] stdio
99 \\[m2-type] type \\[m2-until] until
100 \\[m2-var] var \\[m2-while] while
101 \\[m2-export] export \\[m2-import] import
102 \\[m2-begin-comment] begin-comment \\[m2-end-comment] end-comment
103 \\[suspend-emacs] suspend Emacs \\[m2-toggle] toggle
104 \\[m2-compile] compile \\[m2-next-error] next-error
105 \\[m2-link] link
66f56525 106
5d11560e
BP
107 `m2-indent' controls the number of spaces for each indentation.
108 `m2-compile-command' holds the command to compile a Modula-2 program.
109 `m2-link-command' holds the command to link a Modula-2 program."
66f56525
RS
110 (interactive)
111 (kill-all-local-variables)
112 (use-local-map m2-mode-map)
113 (setq major-mode 'modula-2-mode)
114 (setq mode-name "Modula-2")
115 (make-local-variable 'comment-column)
116 (setq comment-column 41)
117 (make-local-variable 'end-comment-column)
118 (setq end-comment-column 75)
119 (set-syntax-table m2-mode-syntax-table)
120 (make-local-variable 'paragraph-start)
121 (setq paragraph-start (concat "^$\\|" page-delimiter))
122 (make-local-variable 'paragraph-separate)
123 (setq paragraph-separate paragraph-start)
124 (make-local-variable 'paragraph-ignore-fill-prefix)
125 (setq paragraph-ignore-fill-prefix t)
126; (make-local-variable 'indent-line-function)
127; (setq indent-line-function 'c-indent-line)
128 (make-local-variable 'require-final-newline)
129 (setq require-final-newline t)
130 (make-local-variable 'comment-start)
131 (setq comment-start "(* ")
132 (make-local-variable 'comment-end)
133 (setq comment-end " *)")
134 (make-local-variable 'comment-column)
135 (setq comment-column 41)
136 (make-local-variable 'comment-start-skip)
137 (setq comment-start-skip "/\\*+ *")
138 (make-local-variable 'comment-indent-hook)
139 (setq comment-indent-hook 'c-comment-indent)
140 (make-local-variable 'parse-sexp-ignore-comments)
141 (setq parse-sexp-ignore-comments t)
142 (run-hooks 'm2-mode-hook))
143
144(defun m2-newline ()
145 "Insert a newline and indent following line like previous line."
146 (interactive)
147 (let ((hpos (current-indentation)))
148 (newline)
149 (indent-to hpos)))
150
151(defun m2-tab ()
152 "Indent to next tab stop."
153 (interactive)
154 (indent-to (* (1+ (/ (current-indentation) m2-indent)) m2-indent)))
155
156(defun m2-begin ()
157 "Insert a BEGIN keyword and indent for the next line."
158 (interactive)
159 (insert "BEGIN")
160 (m2-newline)
161 (m2-tab))
162
163(defun m2-case ()
164 "Build skeleton CASE statment, prompting for the <expression>."
165 (interactive)
166 (let ((name (read-string "Case-Expression: ")))
167 (insert "CASE " name " OF")
168 (m2-newline)
169 (m2-newline)
170 (insert "END (* case " name " *);"))
171 (end-of-line 0)
172 (m2-tab))
173
174(defun m2-definition ()
175 "Build skeleton DEFINITION MODULE, prompting for the <module name>."
176 (interactive)
177 (insert "DEFINITION MODULE ")
178 (let ((name (read-string "Name: ")))
179 (insert name ";\n\n\n\nEND " name ".\n"))
180 (previous-line 3))
181
182(defun m2-else ()
183 "Insert ELSE keyword and indent for next line."
184 (interactive)
185 (m2-newline)
186 (backward-delete-char-untabify m2-indent ())
187 (insert "ELSE")
188 (m2-newline)
189 (m2-tab))
190
191(defun m2-for ()
192 "Build skeleton FOR loop statment, prompting for the loop parameters."
193 (interactive)
194 (insert "FOR ")
195 (let ((name (read-string "Loop Initialiser: ")) limit by)
196 (insert name " TO ")
197 (setq limit (read-string "Limit: "))
198 (insert limit)
199 (setq by (read-string "Step: "))
200 (if (not (string-equal by ""))
201 (insert " BY " by))
202 (insert " DO")
203 (m2-newline)
204 (m2-newline)
205 (insert "END (* for " name " to " limit " *);"))
206 (end-of-line 0)
207 (m2-tab))
208
209(defun m2-header ()
210 "Insert a comment block containing the module title, author, etc."
211 (interactive)
212 (insert "(*\n Title: \t")
213 (insert (read-string "Title: "))
214 (insert "\n Created:\t")
215 (insert (current-time-string))
216 (insert "\n Author: \t")
217 (insert (user-full-name))
218 (insert (concat "\n\t\t<" (user-login-name) "@" (system-name) ">\n"))
219 (insert "*)\n\n"))
220
221(defun m2-if ()
222 "Insert skeleton IF statment, prompting for <boolean-expression>."
223 (interactive)
224 (insert "IF ")
225 (let ((thecondition (read-string "<boolean-expression>: ")))
226 (insert thecondition " THEN")
227 (m2-newline)
228 (m2-newline)
229 (insert "END (* if " thecondition " *);"))
230 (end-of-line 0)
231 (m2-tab))
232
233(defun m2-loop ()
234 "Build skeleton LOOP (with END)."
235 (interactive)
236 (insert "LOOP")
237 (m2-newline)
238 (m2-newline)
239 (insert "END (* loop *);")
240 (end-of-line 0)
241 (m2-tab))
242
243(defun m2-module ()
244 "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
245 (interactive)
246 (insert "IMPLEMENTATION MODULE ")
247 (let ((name (read-string "Name: ")))
248 (insert name ";\n\n\n\nEND " name ".\n")
249 (previous-line 3)
250 (m2-header)
251 (m2-type)
252 (newline)
253 (m2-var)
254 (newline)
255 (m2-begin)
256 (m2-begin-comment)
257 (insert " Module " name " Initialisation Code "))
258 (m2-end-comment)
259 (newline)
260 (m2-tab))
261
262(defun m2-or ()
263 (interactive)
264 (m2-newline)
265 (backward-delete-char-untabify m2-indent)
266 (insert "|")
267 (m2-newline)
268 (m2-tab))
269
270(defun m2-procedure ()
271 (interactive)
272 (insert "PROCEDURE ")
273 (let ((name (read-string "Name: " ))
274 args)
275 (insert name " (")
276 (insert (read-string "Arguments: ") ")")
277 (setq args (read-string "Result Type: "))
278 (if (not (string-equal args ""))
279 (insert " : " args))
280 (insert ";")
281 (m2-newline)
282 (insert "BEGIN")
283 (m2-newline)
284 (m2-newline)
285 (insert "END ")
286 (insert name)
287 (insert ";")
288 (end-of-line 0)
289 (m2-tab)))
290
291(defun m2-with ()
292 (interactive)
293 (insert "WITH ")
294 (let ((name (read-string "Record-Type: ")))
295 (insert name)
296 (insert " DO")
297 (m2-newline)
298 (m2-newline)
299 (insert "END (* with " name " *);"))
300 (end-of-line 0)
301 (m2-tab))
302
303(defun m2-record ()
304 (interactive)
305 (insert "RECORD")
306 (m2-newline)
307 (m2-newline)
308 (insert "END (* record *);")
309 (end-of-line 0)
310 (m2-tab))
311
312(defun m2-stdio ()
313 (interactive)
314 (insert "
5d11560e 315FROM TextIO IMPORT
66f56525
RS
316 WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,
317 WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,
318 WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,
319 WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,
320 WriteString, ReadString, WhiteSpace, EndOfLine;
321
5d11560e 322FROM SysStreams IMPORT sysIn, sysOut, sysErr;
66f56525
RS
323
324"))
325
326(defun m2-type ()
327 (interactive)
328 (insert "TYPE")
329 (m2-newline)
330 (m2-tab))
331
332(defun m2-until ()
333 (interactive)
334 (insert "REPEAT")
335 (m2-newline)
336 (m2-newline)
337 (insert "UNTIL ")
338 (insert (read-string "<boolean-expression>: ") ";")
339 (end-of-line 0)
340 (m2-tab))
341
342(defun m2-var ()
343 (interactive)
344 (m2-newline)
345 (insert "VAR")
346 (m2-newline)
347 (m2-tab))
348
349(defun m2-while ()
350 (interactive)
351 (insert "WHILE ")
352 (let ((name (read-string "<boolean-expression>: ")))
353 (insert name " DO" )
354 (m2-newline)
355 (m2-newline)
356 (insert "END (* while " name " *);"))
357 (end-of-line 0)
358 (m2-tab))
359
360(defun m2-export ()
361 (interactive)
362 (insert "EXPORT QUALIFIED "))
363
364(defun m2-import ()
365 (interactive)
366 (insert "FROM ")
367 (insert (read-string "Module: "))
368 (insert " IMPORT "))
369
370(defun m2-begin-comment ()
371 (interactive)
372 (if (not (bolp))
373 (indent-to comment-column 0))
374 (insert "(* "))
375
376(defun m2-end-comment ()
377 (interactive)
378 (if (not (bolp))
379 (indent-to end-comment-column))
380 (insert "*)"))
381
382(defun m2-compile ()
383 (interactive)
384 (setq modulename (buffer-name))
385 (compile (concat m2-compile-command " " modulename)))
386
387(defun m2-link ()
388 (interactive)
389 (setq modulename (buffer-name))
390 (if m2-link-name
391 (compile (concat m2-link-command " " m2-link-name))
392 (compile (concat m2-link-command " "
393 (setq m2-link-name (read-string "Name of executable: "
394 modulename))))))
395
396(defun execute-monitor-command (command)
397 (let* ((shell shell-file-name)
398 (csh (equal (file-name-nondirectory shell) "csh")))
399 (call-process shell nil t t "-cf" (concat "exec " command))))
400
401(defun m2-visit ()
402 (interactive)
403 (let ((deffile nil)
404 (modfile nil)
405 modulename)
406 (save-excursion
407 (setq modulename
408 (read-string "Module name: "))
409 (switch-to-buffer "*Command Execution*")
410 (execute-monitor-command (concat "m2whereis " modulename))
411 (goto-char (point-min))
412 (condition-case ()
413 (progn (re-search-forward "\\(.*\\.def\\) *$")
414 (setq deffile (buffer-substring (match-beginning 1)
415 (match-end 1))))
416 (search-failed ()))
417 (condition-case ()
418 (progn (re-search-forward "\\(.*\\.mod\\) *$")
419 (setq modfile (buffer-substring (match-beginning 1)
420 (match-end 1))))
421 (search-failed ()))
422 (if (not (or deffile modfile))
423 (error "I can find neither definition nor implementation of %s"
424 modulename)))
425 (cond (deffile
426 (find-file deffile)
427 (if modfile
428 (save-excursion
429 (find-file modfile))))
430 (modfile
431 (find-file modfile)))))
432
433(defun m2-toggle ()
434 "Toggle between .mod and .def files for the module."
435 (interactive)
436 (cond ((string-equal (substring (buffer-name) -4) ".def")
437 (find-file-other-window
438 (concat (substring (buffer-name) 0 -4) ".mod")))
439 ((string-equal (substring (buffer-name) -4) ".mod")
440 (find-file-other-window
441 (concat (substring (buffer-name) 0 -4) ".def")))
442 ((string-equal (substring (buffer-name) -3) ".mi")
443 (find-file-other-window
444 (concat (substring (buffer-name) 0 -3) ".md")))
445 ((string-equal (substring (buffer-name) -3) ".md")
446 (find-file-other-window
447 (concat (substring (buffer-name) 0 -3) ".mi")))))
6594deb0
ER
448
449;;; modula2.el ends here