Blob


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
11 ;; Version: 0
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)
19 ;; any later version.
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.
31 ;;; Commentary:
33 ;; A major mode for editing Algol 68 code.
34 ;;
35 ;; This is an improved and modernized version of the a68-mode written
36 ;; by Jose E. Marchesi. The original code was taken from
37 ;;
38 ;; https://github.com/lachrymology/me/blob/master/.emacs.d/extras/algol-mode.el
39 ;;
40 ;; TODO: support quote and dot stropping.
42 ;;; Code:
44 (require 'syntax)
45 (require 'font-lock)
47 (eval-when-compile
48 (require 'rx))
50 (defgroup a68 nil
51 "Major mode for editing Algol68 code."
52 :prefix "a68-"
53 :group 'languages)
55 (defcustom a68-indent-level 3
56 "Indentation step for Algol 68."
57 :type 'integer
58 :safe #'integerp)
60 (defcustom a68-comment-style '("#" . "#")
61 "Default comment style used by e.g. `comment-dwim'."
62 :type '(choice (const :tag "#" ("#" . "#"))
63 (const :tag "CO" ("CO" . "CO"))
64 (const :tag "COMMENT" ("COMMENT" . "COMMENT")))
65 :safe #'consp)
67 (defvar a68-mode-hook '()
68 "Hook run when entering Algol68 mode.")
70 (defvar a68-mode-map
71 (let ((map (make-sparse-keymap)))
72 (define-key map (kbd "C-j") #'newline-and-indent)
73 (define-key map (kbd "RET") #'a68-electric-terminate-line)
74 map)
75 "Keymap for Algol 68 major mode.")
77 (defconst a68-font-lock-keywords
78 (list
79 (cons (rx word-start
80 (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
81 "ALIEN"
82 "MODE" "OP" "PRIO" "PROC"
83 "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
84 "PR" "PRAGMAT"
85 "CASE" "IN" "OUSE" "OUT" "ESAC"
86 "FOR" "FORALL" "FROM" "TO" "BY" "WHILE" "DO" "OD"
87 "IF" "THEN" "ELIF" "THEN" "ELSE" "FI"
88 "PAR" "BEGIN" "END" "GOTO" "EXIT"
89 "LWB" "UPB" "NOT" "ABS" "BIN" "REPR" "LENG"
90 "SHORTEN" "ODD" "SIGN" "ROUND" "ENTIER" "AND" "OR"
91 "DIV" "OVER" "MOD" "ELEM" "SHL" "SHR" "OVERAB" "DIVAB" "MODAB"
92 "REF")
93 word-end)
94 'font-lock-keyword-face)
95 (cons (rx word-start
96 (or "TRUE" "FALSE")
97 word-end)
98 'font-lock-constant-face)
99 ;; only valid for bold stropping
100 (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
101 (cons "\\('\\w*'\\)"
102 'font-lock-variable-name-face))
103 "Highlighting expressions for Algol 68 mode.")
105 (defsubst a68-within-string ()
106 "Check if inside a string."
107 (nth 3 (syntax-ppss)))
109 (defsubst a68-within-comment ()
110 "Check if inside a comment."
111 (nth 4 (syntax-ppss)))
113 ;; Indentation rules:
114 ;;
115 ;; - If we are at the beginning of the buffer, or looking at some
116 ;; indent-0 content, indent to column 0.
117 ;;
118 ;; - If we are currently at an END, ), FI or OD, then de-indent
119 ;; relative to the previous line.
120 ;;
121 ;; - If we first see and "end line" before our current line,
122 ;; then we should indent our current line to the same indentation as
123 ;; the end line.
124 ;;
125 ;; - If we first see a "start line" like IF, then we need to increase
126 ;; our indentation relative to that start line.
127 ;;
128 ;; - If into a balanced expression, we should indent to the column
129 ;; where the start of the innermost parenthetical group.
130 ;;
131 ;; - If none of the above apply, then do not indent at all.
133 (defun a68-indent-line ()
134 "Indent current line as Algol 68 code."
135 (interactive)
136 (let ((case-fold-search nil))
137 (save-excursion
138 (beginning-of-line)
139 (if (nth 1 (syntax-ppss)) ; Check for rule 5
140 (let ((offset (save-excursion (goto-char (+ (nth 1 (syntax-ppss)) 1))
141 (current-column))))
142 (indent-line-to offset))
143 (if (or (bobp) ; Check for rule 1
144 (looking-at "^[ \t]*\\<\\(KEEP\\|FINISH\\|DECS\\|USE\\|PROGRAM\\)\\>"))
145 (indent-line-to 0)
146 (let ((not-indented t)
147 (prev-indent (current-indentation))
148 (begin-indent-re "^[ \t]*\\<\\(PAR\\|BEGIN\\|KEEP\\|IF\\|DO\\|ELSE\\|ELIF\\|THEN\\)")
149 (deindent-line-re "^[ \t]*\\<\\(END\\|FI\\|OD\\|ELSE\\|ELIF\\)\\>")
150 (eqindent-line-re "^[ \t]*\\<\\(THEN\\)\\>")
151 (end-line-re "^[ \t]*\\(END\\|FI\\|OD\\)")
152 cur-indent)
153 (if (looking-at eqindent-line-re)
154 (save-excursion
155 (forward-line -1)
156 (setq cur-indent (current-indentation)))
157 (if (looking-at deindent-line-re) ; Check for rule 2
158 (progn
159 (save-excursion
160 (forward-line -1)
161 (setq cur-indent (- (current-indentation) a68-indent-level)))
162 (when (< cur-indent 0)
163 (setq cur-indent 0)))
164 (save-excursion
165 (while not-indented
166 (forward-line -1)
167 (if (looking-at end-line-re) ; Check for rule 3
168 (progn
169 (setq cur-indent (current-indentation))
170 (setq not-indented nil))
171 ;; Check for rule 4
172 (if (looking-at begin-indent-re)
173 (progn
174 (setq cur-indent (+ (current-indentation) a68-indent-level))
175 (setq not-indented nil))
176 (when (bobp) ; Check for rule 5
177 (setq not-indented nil))))))))
178 (if cur-indent
179 (indent-line-to cur-indent)
180 ;; If we didn't see an indentation hint, then allow no
181 ;; indentation.
182 (indent-line-to 0)))))))
183 (when (< (current-column) (current-indentation))
184 (move-to-column (current-indentation))))
186 (defvar a68-mode-syntax-table
187 (let ((st (make-syntax-table)))
188 (modify-syntax-entry ?# "<" st)
189 (modify-syntax-entry ?# ">" st)
190 (modify-syntax-entry ?\\ "." st)
191 ;; define parentheses to match
192 (modify-syntax-entry ?\( "()" st)
193 (modify-syntax-entry ?\) ")(" st)
194 st))
196 (defconst a68-autoindent-lines-re
197 (rx word-start
198 (or "BEGIN" "END" "ELSE" "ELIF" "DO" "OD" "CASE" "ESAC" "IN" "OUT")
199 word-end))
201 (defun a68-electric-terminate-line ()
202 "Terminate line and indent next line."
203 (interactive)
204 ;; First, check if current line should be indented
205 (save-excursion
206 (beginning-of-line)
207 (skip-chars-forward " \t")
208 (when (looking-at a68-autoindent-lines-re)
209 (a68-indent-line)))
210 (delete-horizontal-space) ; Removes triling whitespaces
211 (newline)
212 ;; Indent next line if we are not in a string
213 (unless (a68-within-string)
214 (a68-indent-line)))
216 (defvar a68-mode-abbrev-table nil
217 "Abbreviation table used in `a68-mode' buffers.")
219 (define-abbrev-table 'a68-mode-abbrev-table
220 '())
222 ;;;###autoload
223 (define-derived-mode a68-mode prog-mode "Algol68"
224 "Major mode for editing Alogl68 files."
225 :abbrev-table a68-mode-abbrev-table
226 (setq-local font-lock-defaults '(a68-font-lock-keywords))
227 (setq-local indent-line-function #'a68-indent-line)
228 (setq-local comment-start (car a68-comment-style))
229 (setq-local comment-end (cdr a68-comment-style))
230 (setq-local syntax-propertize-function
231 (syntax-propertize-rules ((rx (group bow "COMMENT" eow)
232 (group (*? anychar))
233 (group bow "COMMENT" eow))
234 (1 "<")
235 (3 ">"))
236 ((rx (group bow "CO" eow)
237 (group (*? anychar))
238 (group bow "CO" eow))
239 (1 "<")
240 (3 ">"))
241 ((rx (group "#")
242 (group (*? anychar))
243 (group "#"))
244 (1 "<")
245 (3 ">")))))
247 ;;;###autoload
248 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
250 (provide 'a68-mode)
251 ;;; a68-mode.el ends here