Blob


1 ;;; a68-mode.el --- Major mode for editing Algol 68 code
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 (defvar a68-mode-hook '()
61 "Hook run when entering Algol68 mode.")
63 (defvar a68-mode-map
64 (let ((map (make-sparse-keymap)))
65 (define-key map (kbd "C-j") #'newline-and-indent)
66 (define-key map (kbd "RET") #'a68-electric-terminate-line)
67 map)
68 "Keymap for Algol 68 major mode.")
70 (defconst a68-font-lock-keywords
71 (list
72 (cons (rx word-start
73 (or "DECS" "PROGRAM" "CONTEXT" "USE" "FINISH" "KEEP"
74 "ALIEN"
75 "MODE" "OP" "PRIO" "PROC"
76 "OF" "AT" "IS" "ISNT" "EMPTY" "SKIP"
77 "PR" "PRAGMAT"
78 "CASE" "IN" "OUSE" "OUT" "ESAC"
79 "FOR" "FORALL" "FROM" "TO" "BY" "WHILE" "DO" "OD"
80 "IF" "THEN" "ELIF" "THEN" "ELSE" "FI"
81 "PAR" "BEGIN" "END" "GOTO" "EXIT"
82 "LWB" "UPB" "NOT" "ABS" "BIN" "REPR" "LENG"
83 "SHORTEN" "ODD" "SIGN" "ROUND" "ENTIER" "AND" "OR"
84 "DIV" "OVER" "MOD" "ELEM" "SHL" "SHR" "OVERAB" "DIVAB" "MODAB"
85 "REF")
86 word-end)
87 'font-lock-keyword-face)
88 (cons (rx word-start
89 (or "TRUE" "FALSE")
90 word-end)
91 'font-lock-constant-face)
92 ;; only valid for bold stropping
93 (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
94 (cons "\\('\\w*'\\)"
95 'font-lock-variable-name-face))
96 "Highlighting expressions for Algol 68 mode.")
98 (defsubst a68-within-string ()
99 "Check if inside a string."
100 (nth 3 (syntax-ppss)))
102 (defsubst a68-within-comment ()
103 "Check if inside a comment."
104 (nth 4 (syntax-ppss)))
106 ;; Indentation rules:
107 ;;
108 ;; - If we are at the beginning of the buffer, or looking at some
109 ;; indent-0 content, indent to column 0.
110 ;;
111 ;; - If we are currently at an END, ), FI or OD, then de-indent
112 ;; relative to the previous line.
113 ;;
114 ;; - If we first see and "end line" before our current line,
115 ;; then we should indent our current line to the same indentation as
116 ;; the end line.
117 ;;
118 ;; - If we first see a "start line" like IF, then we need to increase
119 ;; our indentation relative to that start line.
120 ;;
121 ;; - If into a balanced expression, we should indent to the column
122 ;; where the start of the innermost parenthetical group.
123 ;;
124 ;; - If none of the above apply, then do not indent at all.
126 (defun a68-indent-line ()
127 "Indent current line as Algol 68 code."
128 (interactive)
129 (let ((case-fold-search nil))
130 (save-excursion
131 (beginning-of-line)
132 (if (nth 1 (syntax-ppss)) ; Check for rule 5
133 (let ((offset (save-excursion (goto-char (+ (nth 1 (syntax-ppss)) 1))
134 (current-column))))
135 (indent-line-to offset))
136 (if (or (bobp) ; Check for rule 1
137 (looking-at "^[ \t]*\\<\\(KEEP\\|FINISH\\|DECS\\|USE\\|PROGRAM\\)\\>"))
138 (indent-line-to 0)
139 (let ((not-indented t)
140 (prev-indent (current-indentation))
141 (begin-indent-re "^[ \t]*\\<\\(PAR\\|BEGIN\\|KEEP\\|IF\\|DO\\|ELSE\\|ELIF\\|THEN\\)")
142 (deindent-line-re "^[ \t]*\\<\\(END\\|FI\\|OD\\|ELSE\\|ELIF\\)\\>")
143 (eqindent-line-re "^[ \t]*\\<\\(THEN\\)\\>")
144 (end-line-re "^[ \t]*\\(END\\|FI\\|OD\\)")
145 cur-indent)
146 (if (looking-at eqindent-line-re)
147 (save-excursion
148 (forward-line -1)
149 (setq cur-indent (current-indentation)))
150 (if (looking-at deindent-line-re) ; Check for rule 2
151 (progn
152 (save-excursion
153 (forward-line -1)
154 (setq cur-indent (- (current-indentation) a68-indent-level)))
155 (when (< cur-indent 0)
156 (setq cur-indent 0)))
157 (save-excursion
158 (while not-indented
159 (forward-line -1)
160 (if (looking-at end-line-re) ; Check for rule 3
161 (progn
162 (setq cur-indent (current-indentation))
163 (setq not-indented nil))
164 ;; Check for rule 4
165 (if (looking-at begin-indent-re)
166 (progn
167 (setq cur-indent (+ (current-indentation) a68-indent-level))
168 (setq not-indented nil))
169 (when (bobp) ; Check for rule 5
170 (setq not-indented nil))))))))
171 (if cur-indent
172 (indent-line-to cur-indent)
173 ;; If we didn't see an indentation hint, then allow no
174 ;; indentation.
175 (indent-line-to 0)))))))
176 (when (< (current-column) (current-indentation))
177 (move-to-column (current-indentation))))
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 ;; (modify-syntax-entry ?C "< 13" st)
187 ;; (modify-syntax-entry ?O "> 24" st)
188 ;; define parentheses to match
189 (modify-syntax-entry ?\( "()" st)
190 (modify-syntax-entry ?\) ")(" st)
191 st))
193 (defconst a68-autoindent-lines-re
194 (rx word-start
195 (or "BEGIN" "END" "ELSE" "ELIF" "DO" "OD" "CASE" "ESAC" "IN" "OUT")
196 word-end))
198 (defun a68-electric-terminate-line ()
199 "Terminate line and indent next line."
200 (interactive)
201 ;; First, check if current line should be indented
202 (save-excursion
203 (beginning-of-line)
204 (skip-chars-forward " \t")
205 (when (looking-at a68-autoindent-lines-re)
206 (a68-indent-line)))
207 (delete-horizontal-space) ; Removes triling whitespaces
208 (newline)
209 ;; Indent next line if we are not in a string
210 (unless (a68-within-string)
211 (a68-indent-line)))
213 (defvar a68-mode-abbrev-table nil
214 "Abbreviation table used in `a68-mode' buffers.")
216 (define-abbrev-table 'a68-mode-abbrev-table
217 '())
219 ;;;###autoload
220 (define-derived-mode a68-mode prog-mode "Algol68"
221 "Major mode for editing Alogl68 files."
222 :abbrev-table a68-mode-abbrev-table
223 (setq-local font-lock-defaults '(a68-font-lock-keywords))
224 (setq-local indent-line-function #'a68-indent-line)
225 (setq-local comment-start "#")
226 (setq-local comment-end "#"))
228 ;;;###autoload
229 (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
231 (provide 'a68-mode)
232 ;;; a68-mode.el ends here