modula2.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. ;;; modula2.el --- Modula-2 editing support package
  2. ;; Author: Michael Schmidt <michael@pbinfo.UUCP>
  3. ;; Tom Perrine <Perrin@LOGICON.ARPA>
  4. ;; Maintainer: FSF
  5. ;; Keywords: languages
  6. ;; This file is part of GNU Emacs.
  7. ;; The authors distributed this without a copyright notice
  8. ;; back in 1988, so it is in the public domain. The original included
  9. ;; the following credit:
  10. ;; Author Mick Jordan
  11. ;; amended Peter Robinson
  12. ;;; Commentary:
  13. ;; A major mode for editing Modula-2 code. It provides convenient abbrevs
  14. ;; for Modula-2 keywords, knows about the standard layout rules, and supports
  15. ;; a native compile command.
  16. ;;; Code:
  17. (require 'smie)
  18. (defgroup modula2 nil
  19. "Major mode for editing Modula-2 code."
  20. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  21. :prefix "m2-"
  22. :group 'languages)
  23. ;;; Added by Tom Perrine (TEP)
  24. (defvar m2-mode-syntax-table
  25. (let ((table (make-syntax-table)))
  26. (modify-syntax-entry ?\\ "\\" table)
  27. (modify-syntax-entry ?/ ". 12" table)
  28. (modify-syntax-entry ?\n ">" table)
  29. (modify-syntax-entry ?\( "()1" table)
  30. (modify-syntax-entry ?\) ")(4" table)
  31. (modify-syntax-entry ?* ". 23nb" 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. (modify-syntax-entry ?\' "\"" table)
  39. table)
  40. "Syntax table in use in Modula-2 buffers.")
  41. (defcustom m2-compile-command "m2c"
  42. "Command to compile Modula-2 programs."
  43. :type 'string
  44. :group 'modula2)
  45. (defcustom m2-link-command "m2l"
  46. "Command to link Modula-2 programs."
  47. :type 'string
  48. :group 'modula2)
  49. (defcustom m2-link-name nil
  50. "Name of the Modula-2 executable."
  51. :type '(choice (const nil) string)
  52. :group 'modula2)
  53. (defcustom m2-end-comment-column 75
  54. "*Column for aligning the end of a comment, in Modula-2."
  55. :type 'integer
  56. :group 'modula2)
  57. ;;; Added by TEP
  58. (defvar m2-mode-map
  59. (let ((map (make-sparse-keymap)))
  60. ;; FIXME: Many of those bindings are contrary to coding conventions.
  61. (define-key map "\C-cb" 'm2-begin)
  62. (define-key map "\C-cc" 'm2-case)
  63. (define-key map "\C-cd" 'm2-definition)
  64. (define-key map "\C-ce" 'm2-else)
  65. (define-key map "\C-cf" 'm2-for)
  66. (define-key map "\C-ch" 'm2-header)
  67. (define-key map "\C-ci" 'm2-if)
  68. (define-key map "\C-cm" 'm2-module)
  69. (define-key map "\C-cl" 'm2-loop)
  70. (define-key map "\C-co" 'm2-or)
  71. (define-key map "\C-cp" 'm2-procedure)
  72. (define-key map "\C-c\C-w" 'm2-with)
  73. (define-key map "\C-cr" 'm2-record)
  74. (define-key map "\C-cs" 'm2-stdio)
  75. (define-key map "\C-ct" 'm2-type)
  76. (define-key map "\C-cu" 'm2-until)
  77. (define-key map "\C-cv" 'm2-var)
  78. (define-key map "\C-cw" 'm2-while)
  79. (define-key map "\C-cx" 'm2-export)
  80. (define-key map "\C-cy" 'm2-import)
  81. (define-key map "\C-c{" 'm2-begin-comment)
  82. (define-key map "\C-c}" 'm2-end-comment)
  83. (define-key map "\C-c\C-z" 'suspend-emacs)
  84. (define-key map "\C-c\C-v" 'm2-visit)
  85. (define-key map "\C-c\C-t" 'm2-toggle)
  86. (define-key map "\C-c\C-l" 'm2-link)
  87. (define-key map "\C-c\C-c" 'm2-compile)
  88. map)
  89. "Keymap used in Modula-2 mode.")
  90. (defcustom m2-indent 5
  91. "*This variable gives the indentation in Modula-2-Mode."
  92. :type 'integer
  93. :group 'modula2)
  94. (put 'm2-indent 'safe-local-variable
  95. (lambda (v) (or (null v) (integerp v))))
  96. (defconst m2-smie-grammar
  97. ;; An official definition can be found as "M2R10.pdf". This grammar does
  98. ;; not really follow it, for lots of technical reasons, but it can still be
  99. ;; useful to refer to it.
  100. (smie-prec2->grammar
  101. (smie-merge-prec2s
  102. (smie-bnf->prec2
  103. '((range) (id) (epsilon)
  104. (fields (fields ";" fields) (ids ":" type))
  105. (proctype (id ":" type))
  106. (type ("RECORD" fields "END")
  107. ("POINTER" "TO" type)
  108. ;; The PROCEDURE type is indistinguishable from the beginning
  109. ;; of a PROCEDURE definition, so we need a "PROCEDURE-type" to
  110. ;; prevent SMIE from trying to find the matching END.
  111. ("PROCEDURE-type" proctype)
  112. ;; OF's right hand side should bind tighter than ; for array
  113. ;; types, but should bind less tight than | which itself binds
  114. ;; less tight than ;. So we use two distinct OFs.
  115. ("SET" "OF-type" id)
  116. ("ARRAY" range "OF-type" type))
  117. (args ("(" fargs ")"))
  118. ;; VAR has lower precedence than ";" in formal args, but not
  119. ;; in declarations. So we use "VAR-arg" for the formal arg case.
  120. (farg (ids ":" type) ("CONST-arg" farg) ("VAR-arg" farg))
  121. (fargs (fargs ";" fargs) (farg))
  122. ;; Handling of PROCEDURE in decls is problematic: we'd want
  123. ;; TYPE/CONST/VAR/PROCEDURE's parent to be any previous
  124. ;; CONST/TYPE/VAR/PROCEDURE, but we also want PROCEDURE to be an opener
  125. ;; (so that its END has PROCEDURE as its parent). So instead, we treat
  126. ;; the last ";" in those blocks as a separator (we call it ";-block").
  127. ;; FIXME: This means that "TYPE \n VAR" is not indented properly
  128. ;; because there's no ";-block" between the two.
  129. (decls (decls ";-block" decls)
  130. ("TYPE" typedecls) ("CONST" constdecls) ("VAR" vardecls)
  131. ;; END is usually a closer, but not quite for PROCEDURE...END.
  132. ;; We could use "END-proc" for the procedure case, but
  133. ;; I preferred to just pretend PROCEDURE's END is the closer.
  134. ("PROCEDURE" decls "BEGIN" insts "END") ;END-proc id
  135. ("PROCEDURE" decls "BEGIN" insts "FINALLY" insts "END")
  136. ("PROCEDURE" decls "FORWARD")
  137. ;; ("IMPLEMENTATION" epsilon "MODULE" decls
  138. ;; "BEGIN" insts "FINALLY" insts "END")
  139. )
  140. (typedecls (typedecls ";" typedecls) (id "=" type))
  141. (ids (ids "," ids))
  142. (vardecls (vardecls ";" vardecls) (ids ":" type))
  143. (constdecls (constdecls ";" constdecls) (id "=" exp))
  144. (exp (id "-anchor-" id) ("(" exp ")"))
  145. (caselabel (caselabel ".." caselabel) (caselabel "," caselabel))
  146. ;; : for types binds tighter than ;, but the : for case labels binds
  147. ;; less tight, so have to use two different :.
  148. (cases (cases "|" cases) (caselabel ":-case" insts))
  149. (forspec (exp "TO" exp))
  150. (insts (insts ";" insts)
  151. (id ":=" exp)
  152. ("CASE" exp "OF" cases "END")
  153. ("CASE" exp "OF" cases "ELSE" insts "END")
  154. ("LOOP" insts "END")
  155. ("WITH" exp "DO" insts "END")
  156. ("REPEAT" insts "UNTIL" exp)
  157. ("WHILE" exp "DO" insts "END")
  158. ("FOR" forspec "DO" insts "END")
  159. ("IF" exp "THEN" insts "END")
  160. ("IF" exp "THEN" insts "ELSE" insts "END")
  161. ("IF" exp "THEN" insts
  162. "ELSIF" exp "THEN" insts "ELSE" insts "END")
  163. ("IF" exp "THEN" insts
  164. "ELSIF" exp "THEN" insts
  165. "ELSIF" exp "THEN" insts "ELSE" insts "END"))
  166. ;; This category is not used anywhere, but it adds some constraints that
  167. ;; try to reduce the harm when an OF-type is not properly recognized.
  168. (error-OF ("ARRAY" range "OF" type) ("SET" "OF" id)))
  169. '((assoc ";")) '((assoc ";-block")) '((assoc "|"))
  170. ;; For case labels.
  171. '((assoc ",") (assoc ".."))
  172. ;; '((assoc "TYPE" "CONST" "VAR" "PROCEDURE"))
  173. )
  174. (smie-precs->prec2
  175. '((nonassoc "-anchor-" "=")
  176. (nonassoc "<" "<=" ">=" ">" "<>" "#" "IN")
  177. (assoc "OR" "+" "-")
  178. (assoc "AND" "MOD" "DIV" "REM" "*" "/" "&")
  179. (nonassoc "NOT" "~")
  180. (left "." "^")
  181. ))
  182. )))
  183. (defun m2-smie-refine-colon ()
  184. (let ((res nil))
  185. (while (not res)
  186. (let ((tok (smie-default-backward-token)))
  187. (cond
  188. ((zerop (length tok))
  189. (let ((forward-sexp-function nil))
  190. (condition-case nil
  191. (forward-sexp -1)
  192. (scan-error (setq res ":")))))
  193. ((member tok '("|" "OF" "..")) (setq res ":-case"))
  194. ((member tok '(":" "END" ";" "BEGIN" "VAR" "RECORD" "PROCEDURE"))
  195. (setq res ":")))))
  196. res))
  197. (defun m2-smie-refine-of ()
  198. (let ((tok (smie-default-backward-token)))
  199. (when (zerop (length tok))
  200. (let ((forward-sexp-function nil))
  201. (condition-case nil
  202. (backward-sexp 1)
  203. (scan-error nil))
  204. (setq tok (smie-default-backward-token))))
  205. (if (member tok '("ARRAY" "SET"))
  206. "OF-type" "OF")))
  207. (defun m2-smie-refine-semi ()
  208. (forward-comment (point-max))
  209. (if (looking-at (regexp-opt '("PROCEDURE" "TYPE" "VAR" "CONST" "BEGIN")))
  210. ";-block" ";"))
  211. ;; FIXME: "^." are two tokens, not one.
  212. (defun m2-smie-forward-token ()
  213. (pcase (smie-default-forward-token)
  214. (`"VAR" (if (zerop (car (syntax-ppss))) "VAR" "VAR-arg"))
  215. (`"CONST" (if (zerop (car (syntax-ppss))) "CONST" "CONST-arg"))
  216. (`";" (save-excursion (m2-smie-refine-semi)))
  217. (`"OF" (save-excursion (forward-char -2) (m2-smie-refine-of)))
  218. (`":" (save-excursion (forward-char -1) (m2-smie-refine-colon)))
  219. ;; (`"END" (if (and (looking-at "[ \t\n]*\\(\\(?:\\sw\\|\\s_\\)+\\)")
  220. ;; (not (assoc (match-string 1) m2-smie-grammar)))
  221. ;; "END-proc" "END"))
  222. (token token)))
  223. (defun m2-smie-backward-token ()
  224. (pcase (smie-default-backward-token)
  225. (`"VAR" (if (zerop (car (syntax-ppss))) "VAR" "VAR-arg"))
  226. (`"CONST" (if (zerop (car (syntax-ppss))) "CONST" "CONST-arg"))
  227. (`";" (save-excursion (forward-char 1) (m2-smie-refine-semi)))
  228. (`"OF" (save-excursion (m2-smie-refine-of)))
  229. (`":" (save-excursion (m2-smie-refine-colon)))
  230. ;; (`"END" (if (and (looking-at "\\sw+[ \t\n]+\\(\\(?:\\sw\\|\\s_\\)+\\)")
  231. ;; (not (assoc (match-string 1) m2-smie-grammar)))
  232. ;; "END-proc" "END"))
  233. (token token)))
  234. (defun m2-smie-rules (kind token)
  235. ;; FIXME: Apparently, the usual indentation convention is something like:
  236. ;;
  237. ;; TYPE t1 = bar;
  238. ;; VAR x : INTEGER;
  239. ;; PROCEDURE f ();
  240. ;; TYPE t2 = foo;
  241. ;; PROCEDURE g ();
  242. ;; BEGIN blabla END;
  243. ;; VAR y : type;
  244. ;; BEGIN blibli END
  245. ;;
  246. ;; This is inconsistent with the actual structure of the code in 2 ways:
  247. ;; - The inner VAR/TYPE are indented just like the outer VAR/TYPE.
  248. ;; - The inner PROCEDURE is not aligned with its VAR/TYPE siblings.
  249. (pcase (cons kind token)
  250. (`(:elem . basic) m2-indent)
  251. (`(:after . ":=") (or m2-indent smie-indent-basic))
  252. (`(:after . ,(or `"CONST" `"VAR" `"TYPE"))
  253. (or m2-indent smie-indent-basic))
  254. ;; (`(:before . ,(or `"VAR" `"TYPE" `"CONST"))
  255. ;; (if (smie-rule-parent-p "PROCEDURE") 0))
  256. (`(:after . ";-block")
  257. (if (smie-rule-parent-p "PROCEDURE")
  258. (smie-rule-parent (or m2-indent smie-indent-basic))))
  259. (`(:before . "|") (smie-rule-separator kind))
  260. ))
  261. ;;;###autoload
  262. (defalias 'modula-2-mode 'm2-mode)
  263. ;;;###autoload
  264. (define-derived-mode m2-mode prog-mode "Modula-2"
  265. "This is a mode intended to support program development in Modula-2.
  266. All control constructs of Modula-2 can be reached by typing C-c
  267. followed by the first character of the construct.
  268. \\<m2-mode-map>
  269. \\[m2-begin] begin \\[m2-case] case
  270. \\[m2-definition] definition \\[m2-else] else
  271. \\[m2-for] for \\[m2-header] header
  272. \\[m2-if] if \\[m2-module] module
  273. \\[m2-loop] loop \\[m2-or] or
  274. \\[m2-procedure] procedure Control-c Control-w with
  275. \\[m2-record] record \\[m2-stdio] stdio
  276. \\[m2-type] type \\[m2-until] until
  277. \\[m2-var] var \\[m2-while] while
  278. \\[m2-export] export \\[m2-import] import
  279. \\[m2-begin-comment] begin-comment \\[m2-end-comment] end-comment
  280. \\[suspend-emacs] suspend Emacs \\[m2-toggle] toggle
  281. \\[m2-compile] compile \\[m2-next-error] next-error
  282. \\[m2-link] link
  283. `m2-indent' controls the number of spaces for each indentation.
  284. `m2-compile-command' holds the command to compile a Modula-2 program.
  285. `m2-link-command' holds the command to link a Modula-2 program."
  286. (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
  287. (set (make-local-variable 'paragraph-separate) paragraph-start)
  288. (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  289. (set (make-local-variable 'comment-start) "(* ")
  290. (set (make-local-variable 'comment-end) " *)")
  291. (set (make-local-variable 'comment-start-skip) "\\(?:(\\*+\\|//+\\) *")
  292. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  293. (set (make-local-variable 'font-lock-defaults)
  294. '((m3-font-lock-keywords
  295. m3-font-lock-keywords-1 m3-font-lock-keywords-2)
  296. nil nil ((?_ . "w") (?. . "w") (?< . ". 1") (?> . ". 4")) nil
  297. ))
  298. (smie-setup m2-smie-grammar #'m2-smie-rules
  299. :forward-token #'m2-smie-forward-token
  300. :backward-token #'m2-smie-backward-token))
  301. ;; Regexps written with help from Ron Forrester <ron@orcad.com>
  302. ;; and Spencer Allain <sallain@teknowledge.com>.
  303. (defconst m3-font-lock-keywords-1
  304. '(
  305. ;;
  306. ;; Module definitions.
  307. ("\\<\\(INTERFACE\\|MODULE\\|PROCEDURE\\)\\>[ \t]*\\(\\sw+\\)?"
  308. (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  309. ;;
  310. ;; Import directives.
  311. ("\\<\\(EXPORTS\\|FROM\\|IMPORT\\)\\>"
  312. (1 font-lock-keyword-face)
  313. (font-lock-match-c-style-declaration-item-and-skip-to-next
  314. nil (goto-char (match-end 0))
  315. (1 font-lock-constant-face)))
  316. ;;
  317. ;; Pragmas as warnings.
  318. ;; Spencer Allain <sallain@teknowledge.com> says do them as comments...
  319. ;; ("<\\*.*\\*>" . font-lock-warning-face)
  320. ;; ... but instead we fontify the first word.
  321. ("<\\*[ \t]*\\(\\sw+\\)" 1 font-lock-warning-face prepend)
  322. )
  323. "Subdued level highlighting for Modula-3 modes.")
  324. (defconst m3-font-lock-keywords-2
  325. (append m3-font-lock-keywords-1
  326. (eval-when-compile
  327. (let ((m3-types
  328. (regexp-opt
  329. '("INTEGER" "BITS" "BOOLEAN" "CARDINAL" "CHAR" "FLOAT" "REAL"
  330. "LONGREAL" "REFANY" "ADDRESS" "ARRAY" "SET" "TEXT"
  331. "MUTEX" "ROOT" "EXTENDED")))
  332. (m3-keywords
  333. (regexp-opt
  334. '("AND" "ANY" "AS" "BEGIN" "BRANDED" "BY" "CASE" "CONST" "DIV"
  335. "DO" "ELSE" "ELSIF" "EVAL" "EXCEPT" "EXIT" "FINALLY"
  336. "FOR" "GENERIC" "IF" "IN" "LOCK" "LOOP" "METHODS" "MOD" "NOT"
  337. "OBJECT" "OF" "OR" "OVERRIDES" "READONLY" "RECORD" "REF"
  338. "REPEAT" "RETURN" "REVEAL" "THEN" "TO" "TRY"
  339. "TYPE" "TYPECASE" "UNSAFE" "UNTIL" "UNTRACED" "VAR" "VALUE"
  340. "WHILE" "WITH")))
  341. (m3-builtins
  342. (regexp-opt
  343. '("ABS" "ADR" "ADRSIZE" "BITSIZE" "BYTESIZE" "CEILING"
  344. "DEC" "DISPOSE" "FIRST" "FLOOR" "INC" "ISTYPE" "LAST"
  345. "LOOPHOLE" "MAX" "MIN" "NARROW" "NEW" "NUMBER" "ORD"
  346. "ROUND" "SUBARRAY" "TRUNC" "TYPECODE" "VAL")))
  347. )
  348. (list
  349. ;;
  350. ;; Keywords except those fontified elsewhere.
  351. (concat "\\<\\(" m3-keywords "\\)\\>")
  352. ;;
  353. ;; Builtins.
  354. (cons (concat "\\<\\(" m3-builtins "\\)\\>") 'font-lock-builtin-face)
  355. ;;
  356. ;; Type names.
  357. (cons (concat "\\<\\(" m3-types "\\)\\>") 'font-lock-type-face)
  358. ;;
  359. ;; Fontify tokens as function names.
  360. '("\\<\\(END\\|EXCEPTION\\|RAISES?\\)\\>[ \t{]*"
  361. (1 font-lock-keyword-face)
  362. (font-lock-match-c-style-declaration-item-and-skip-to-next
  363. nil (goto-char (match-end 0))
  364. (1 font-lock-function-name-face)))
  365. ;;
  366. ;; Fontify constants as references.
  367. '("\\<\\(FALSE\\|NIL\\|NULL\\|TRUE\\)\\>" . font-lock-constant-face)
  368. ))))
  369. "Gaudy level highlighting for Modula-3 modes.")
  370. (defvar m3-font-lock-keywords m3-font-lock-keywords-1
  371. "Default expressions to highlight in Modula-3 modes.")
  372. ;; We don't actually have different keywords for Modula-2. Volunteers?
  373. (defconst m2-font-lock-keywords-1 m3-font-lock-keywords-1
  374. "Subdued level highlighting for Modula-2 modes.")
  375. (defconst m2-font-lock-keywords-2 m3-font-lock-keywords-2
  376. "Gaudy level highlighting for Modula-2 modes.")
  377. (defvar m2-font-lock-keywords m2-font-lock-keywords-1
  378. "Default expressions to highlight in Modula-2 modes.")
  379. (define-skeleton m2-begin
  380. "Insert a BEGIN keyword and indent for the next line."
  381. nil
  382. \n "BEGIN" > \n)
  383. (define-skeleton m2-case
  384. "Build skeleton CASE statement, prompting for the <expression>."
  385. "Case-Expression: "
  386. \n "CASE " str " OF" > \n _ \n "END (* " str " *);" > \n)
  387. (define-skeleton m2-definition
  388. "Build skeleton DEFINITION MODULE, prompting for the <module name>."
  389. "Name: "
  390. \n "DEFINITION MODULE " str ";" > \n \n _ \n \n "END " str "." > \n)
  391. (define-skeleton m2-else
  392. "Insert ELSE keyword and indent for next line."
  393. nil
  394. \n "ELSE" > \n)
  395. (define-skeleton m2-for
  396. "Build skeleton FOR loop statement, prompting for the loop parameters."
  397. "Loop Initializer: "
  398. ;; FIXME: this seems to be lacking a "<var> :=".
  399. \n "FOR " str " TO "
  400. (setq v1 (read-string "Limit: "))
  401. (let ((by (read-string "Step: ")))
  402. (if (not (string-equal by ""))
  403. (concat " BY " by)))
  404. " DO" > \n _ \n "END (* for " str " to " v1 " *);" > \n)
  405. (define-skeleton m2-header
  406. "Insert a comment block containing the module title, author, etc."
  407. "Title: "
  408. "(*\n Title: \t" str
  409. "\n Created: \t" (current-time-string)
  410. "\n Author: \t" (user-full-name) " <" user-mail-address ">\n"
  411. "*)" > \n)
  412. (define-skeleton m2-if
  413. "Insert skeleton IF statement, prompting for <boolean-expression>."
  414. "<boolean-expression>: "
  415. \n "IF " str " THEN" > \n _ \n "END (* if " str " *);" > \n)
  416. (define-skeleton m2-loop
  417. "Build skeleton LOOP (with END)."
  418. nil
  419. \n "LOOP" > \n _ \n "END (* loop *);" > \n)
  420. (define-skeleton m2-module
  421. "Build skeleton IMPLEMENTATION MODULE, prompting for <module-name>."
  422. "Name: "
  423. \n "IMPLEMENTATION MODULE " str ";" > \n \n
  424. '(m2-header)
  425. '(m2-type) \n
  426. '(m2-var) \n _ \n \n
  427. '(m2-begin)
  428. '(m2-begin-comment)
  429. " Module " str " Initialization Code "
  430. '(m2-end-comment)
  431. \n \n "END " str "." > \n)
  432. (define-skeleton m2-or
  433. "No doc."
  434. nil
  435. \n "|" > \n)
  436. (define-skeleton m2-procedure
  437. "No doc."
  438. "Name: "
  439. \n "PROCEDURE " str " (" (read-string "Arguments: ") ")"
  440. (let ((args (read-string "Result Type: ")))
  441. (if (not (equal args "")) (concat " : " args)))
  442. ";" > \n "BEGIN" > \n _ \n "END " str ";" > \n)
  443. (define-skeleton m2-with
  444. "No doc."
  445. "Record-Type: "
  446. \n "WITH " str " DO" > \n _ \n "END (* with " str " *);" > \n)
  447. (define-skeleton m2-record
  448. "No doc."
  449. nil
  450. \n "RECORD" > \n _ \n "END (* record *);" > \n)
  451. (define-skeleton m2-stdio
  452. "No doc."
  453. nil
  454. \n "FROM TextIO IMPORT"
  455. > \n "WriteCHAR, ReadCHAR, WriteINTEGER, ReadINTEGER,"
  456. > \n "WriteCARDINAL, ReadCARDINAL, WriteBOOLEAN, ReadBOOLEAN,"
  457. > \n "WriteREAL, ReadREAL, WriteBITSET, ReadBITSET,"
  458. > \n "WriteBasedCARDINAL, ReadBasedCARDINAL, WriteChars, ReadChars,"
  459. > \n "WriteString, ReadString, WhiteSpace, EndOfLine;"
  460. > \n \n "FROM SysStreams IMPORT sysIn, sysOut, sysErr;" > \n \n)
  461. (define-skeleton m2-type
  462. "No doc."
  463. nil
  464. \n "TYPE" > \n ";" > \n)
  465. (define-skeleton m2-until
  466. "No doc."
  467. "<boolean-expression>: "
  468. \n "REPEAT" > \n _ \n "UNTIL " str ";" > \n)
  469. (define-skeleton m2-var
  470. "No doc."
  471. nil
  472. \n "VAR" > \n ";" > \n)
  473. (define-skeleton m2-while
  474. "No doc."
  475. "<boolean-expression>: "
  476. \n "WHILE " str " DO" > \n _ \n "END (* while " str " *);" > \n)
  477. (define-skeleton m2-export
  478. "No doc."
  479. nil
  480. \n "EXPORT QUALIFIED " > _ \n)
  481. (define-skeleton m2-import
  482. "No doc."
  483. "Module: "
  484. \n "FROM " str " IMPORT " > _ \n)
  485. (defun m2-begin-comment ()
  486. (interactive)
  487. (if (not (bolp))
  488. (indent-to comment-column 0))
  489. (insert "(* "))
  490. (defun m2-end-comment ()
  491. (interactive)
  492. (if (not (bolp))
  493. (indent-to m2-end-comment-column))
  494. (insert "*)"))
  495. (defun m2-compile ()
  496. (interactive)
  497. (compile (concat m2-compile-command " " (buffer-name))))
  498. (defun m2-link ()
  499. (interactive)
  500. (compile (concat m2-link-command " "
  501. (or m2-link-name
  502. (setq m2-link-name (read-string "Name of executable: "
  503. (buffer-name)))))))
  504. (defun m2-execute-monitor-command (command)
  505. (let* ((shell shell-file-name)
  506. ;; (csh (equal (file-name-nondirectory shell) "csh"))
  507. )
  508. (call-process shell nil t t "-cf" (concat "exec " command))))
  509. (defun m2-visit ()
  510. (interactive)
  511. (let ((deffile nil)
  512. (modfile nil)
  513. modulename)
  514. (save-excursion
  515. (setq modulename
  516. (read-string "Module name: "))
  517. (switch-to-buffer "*Command Execution*")
  518. (m2-execute-monitor-command (concat "m2whereis " modulename))
  519. (goto-char (point-min))
  520. (condition-case ()
  521. (progn (re-search-forward "\\(.*\\.def\\) *$")
  522. (setq deffile (buffer-substring (match-beginning 1)
  523. (match-end 1))))
  524. (search-failed ()))
  525. (condition-case ()
  526. (progn (re-search-forward "\\(.*\\.mod\\) *$")
  527. (setq modfile (buffer-substring (match-beginning 1)
  528. (match-end 1))))
  529. (search-failed ()))
  530. (if (not (or deffile modfile))
  531. (error "I can find neither definition nor implementation of %s"
  532. modulename)))
  533. (cond (deffile
  534. (find-file deffile)
  535. (if modfile
  536. (save-excursion
  537. (find-file modfile))))
  538. (modfile
  539. (find-file modfile)))))
  540. (defun m2-toggle ()
  541. "Toggle between .mod and .def files for the module."
  542. (interactive)
  543. (cond ((string-equal (substring (buffer-name) -4) ".def")
  544. (find-file-other-window
  545. (concat (substring (buffer-name) 0 -4) ".mod")))
  546. ((string-equal (substring (buffer-name) -4) ".mod")
  547. (find-file-other-window
  548. (concat (substring (buffer-name) 0 -4) ".def")))
  549. ((string-equal (substring (buffer-name) -3) ".mi")
  550. (find-file-other-window
  551. (concat (substring (buffer-name) 0 -3) ".md")))
  552. ((string-equal (substring (buffer-name) -3) ".md")
  553. (find-file-other-window
  554. (concat (substring (buffer-name) 0 -3) ".mi")))))
  555. (provide 'modula2)
  556. ;;; modula2.el ends here