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