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