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