1 ;;; a68-mode.el --- Major mode for editing Algol 68 code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011 Jose E. Marchesi
4 ;; Copyright (C) 2021 Omar Polo <op@omarpolo.com>
6 ;; Author: Jose E. Marchesi
7 ;; Omar Polo <op@omarpolo.com>
8 ;; Maintainer: Omar Polo
9 ;; URL: https://git.omarpolo.com/a68-mode
10 ;; Keywords: languages
12 ;; Package-Requires: ((emacs "24.3"))
14 ;; This file is NOT part of GNU Emacs.
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 3, or (at your option)
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
33 ;; A major mode for editing Algol 68 code.
35 ;; This is an improved and modernized version of the a68-mode written
36 ;; by Jose E. Marchesi. The original code was taken from
38 ;; https://github.com/lachrymology/me/blob/master/.emacs.d/extras/algol-mode.el
40 ;; TODO: support quote and dot stropping.
52 "Major mode for editing Algol68 code."
56 (defcustom a68-indent-level 3
57 "Indentation step for Algol 68."
61 (defcustom a68-comment-style "#"
62 "Default comment style used by e.g. `comment-dwim'."
63 :type '(choice (const "#")
68 (defvar a68-mode-hook '()
69 "Hook run when entering Algol68 mode.")
72 (let ((map (make-sparse-keymap)))
73 (define-key map (kbd "C-j") #'newline-and-indent)
74 ;; (define-key map (kbd "RET") #'a68-electric-terminate-line)
76 "Keymap for Algol 68 major mode.")
78 (defconst a68-font-lock-keywords
81 (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
83 "MODE" "OP" "PRIO" "PROC"
84 "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
86 "CASE" "IN" "OUSE" "OUT" "ESAC"
87 "FOR" "FORALL" "FROM" "TO" "BY" "WHILE" "DO" "OD"
88 "IF" "THEN" "ELIF" "THEN" "ELSE" "FI"
89 "PAR" "BEGIN" "END" "GOTO" "EXIT"
90 "LWB" "UPB" "NOT" "ABS" "BIN" "REPR" "LENG"
91 "SHORTEN" "ODD" "SIGN" "ROUND" "ENTIER" "AND" "OR"
92 "DIV" "OVER" "MOD" "ELEM" "SHL" "SHR" "OVERAB" "DIVAB" "MODAB"
95 'font-lock-keyword-face)
99 'font-lock-constant-face)
100 ;; only valid for bold stropping
101 (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
103 'font-lock-variable-name-face))
104 "Highlighting expressions for Algol 68 mode.")
106 (defvar a68--keywords-regexp
107 (regexp-opt '("+" "*" ";" ">" "<" ":=" "=" "," ":")))
109 (defvar a68--smie-grammar
111 (smie-bnf->prec2 '((id)
121 ("BEGIN" exprs "END")
123 ("IF" exprs "THEN" exprs "FI")
124 ("IF" exprs "THEN" exprs "ELSE" exprs "FI")
125 ("CASE" exprs "IN" exprs "ESAC")
126 ("CASE" exprs "IN" exprs "OUT" exprs "ESAC"))
127 (exprs (exprs "," exprs)
131 (loop ("FOR" exprs "FROM" exprs "TO" exprs
132 "BY" exprs "DO" exprs "OD")
133 ("FOR" exprs "FROM" exprs "TO" exprs
135 ("FOR" exprs "BY" exprs "TO" exprs
137 ;; ("TO" exprs "DO" exprs "OD")
138 ("WHILE" exprs "DO" exprs "OD")))
139 '((assoc ";" "," ":"))
140 '((assoc "=" "/=" ":=" ":=:" ":/=:"
143 (defun a68--smie-rules (kind token)
144 (pcase (cons kind token)
145 (`(:elem . basic) a68-indent-level)
146 ;; (`(,_ . ",") (smie-rule-separator kind))
147 (`(,_ . ",") (smie-rule-separator kind))
148 (`(,_ . ";") (when (smie-rule-parent-p)
150 (`(:after . ":=") a68-indent-level)
151 (`(:after . "=") a68-indent-level)
152 (`(:before . ,(or `"BEGIN" '"(")) (when (smie-rule-hanging-p)
155 (and (not (smie-rule-bolp))
156 (smie-rule-prev-p "ELSE")
157 (smie-rule-parent)))))
159 (defun a68--smie-forward-token ()
160 (forward-comment (point-max))
162 ((looking-at a68--keywords-regexp)
163 (goto-char (match-end 0))
164 (match-string-no-properties 0))
165 (t (buffer-substring-no-properties (point)
166 (progn (skip-syntax-forward "w_")
169 (defun a68--smie-backward-token ()
170 (forward-comment (- (point)))
172 ((looking-back a68--keywords-regexp (- (point) 2) t)
173 (goto-char (match-beginning 0))
174 (match-string-no-properties 0))
175 (t (buffer-substring-no-properties (point)
176 (progn (skip-syntax-backward "w_")
179 (defvar a68-mode-syntax-table
180 (let ((st (make-syntax-table)))
181 (modify-syntax-entry ?# "<" st)
182 (modify-syntax-entry ?# ">" st)
183 (modify-syntax-entry ?\\ "." st)
184 (modify-syntax-entry ?, "." st)
185 (modify-syntax-entry ?: "." st)
186 ;; define parentheses to match
187 (modify-syntax-entry ?\( "()" st)
188 (modify-syntax-entry ?\) ")(" st)
191 (defvar a68-mode-abbrev-table nil
192 "Abbreviation table used in `a68-mode' buffers.")
194 (define-abbrev-table 'a68-mode-abbrev-table
198 (define-derived-mode a68-mode prog-mode "Algol68"
199 "Major mode for editing Alogl68 files."
200 :abbrev-table a68-mode-abbrev-table
201 (setq-local font-lock-defaults '(a68-font-lock-keywords))
202 (smie-setup a68--smie-grammar #'a68--smie-rules
203 :forward-token #'a68--smie-forward-token
204 :backward-token #'a68--smie-backward-token)
205 (setq-local comment-start a68-comment-style)
206 (setq-local comment-end a68-comment-style)
207 (setq-local syntax-propertize-function
208 (syntax-propertize-rules ((rx (group bow "COMMENT" eow)
210 (group bow "COMMENT" eow))
213 ((rx (group bow "CO" eow)
215 (group bow "CO" eow))
225 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
228 ;;; a68-mode.el ends here