Blame


1 7f103319 2021-11-06 op ;;; a68-mode.el --- Major mode for editing Algol 68 code
2 7f103319 2021-11-06 op
3 7f103319 2021-11-06 op ;; Copyright (C) 2011 Jose E. Marchesi
4 7f103319 2021-11-06 op
5 7f103319 2021-11-06 op ;; Maintainer: Jose E. Marchesi
6 7f103319 2021-11-06 op
7 7f103319 2021-11-06 op ;; This file is NOT part of GNU Emacs.
8 7f103319 2021-11-06 op
9 7f103319 2021-11-06 op ;; This program is free software; you can redistribute it and/or modify
10 7f103319 2021-11-06 op ;; it under the terms of the GNU General Public License as published by
11 7f103319 2021-11-06 op ;; the Free Software Foundation; either version 3, or (at your option)
12 7f103319 2021-11-06 op ;; any later version.
13 7f103319 2021-11-06 op
14 7f103319 2021-11-06 op ;; This program is distributed in the hope that it will be useful,
15 7f103319 2021-11-06 op ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 7f103319 2021-11-06 op ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 7f103319 2021-11-06 op ;; GNU General Public License for more details.
18 7f103319 2021-11-06 op
19 7f103319 2021-11-06 op ;; You should have received a copy of the GNU General Public License
20 7f103319 2021-11-06 op ;; along with this program; see the file COPYING. If not, write to the
21 7f103319 2021-11-06 op ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 7f103319 2021-11-06 op ;; Boston, MA 02110-1301, USA.
23 7f103319 2021-11-06 op
24 7f103319 2021-11-06 op ;;; Commentary:
25 7f103319 2021-11-06 op
26 7f103319 2021-11-06 op ;; A major mode for editing Algol 68 code.
27 7f103319 2021-11-06 op ;;
28 7f103319 2021-11-06 op ;; TODO: support quote and dot stropping.
29 7f103319 2021-11-06 op
30 7f103319 2021-11-06 op ;;; Code:
31 7f103319 2021-11-06 op
32 7f103319 2021-11-06 op (require 'syntax)
33 7f103319 2021-11-06 op (require 'font-lock)
34 7f103319 2021-11-06 op
35 7f103319 2021-11-06 op (defvar a68-indent-step 3
36 a6ba7179 2021-11-06 op "Indentation step for Algol 68.")
37 7f103319 2021-11-06 op
38 e5cc6178 2021-11-06 op (defvar a68-mode-hook '()
39 e5cc6178 2021-11-06 op "Hook run when entering Algol68 mode.")
40 7f103319 2021-11-06 op
41 7f103319 2021-11-06 op (defvar a68-mode-map
42 7f103319 2021-11-06 op (let ((map (make-keymap)))
43 4d11435a 2021-11-06 op (define-key map "\C-j" #'newline-and-indent)
44 23be3ba2 2021-11-06 op (define-key map "\r" #'a68-electric-terminate-line)
45 23be3ba2 2021-11-06 op (define-key map "\t" #'a68-electric-tab)
46 7f103319 2021-11-06 op map)
47 a6ba7179 2021-11-06 op "Keymap for Algol 68 major mode.")
48 7f103319 2021-11-06 op
49 7f103319 2021-11-06 op (defconst a68-font-lock-keywords
50 7f103319 2021-11-06 op (list
51 7f103319 2021-11-06 op (cons (concat "\\<\\("
52 7f103319 2021-11-06 op "DECS\\|PROGRAM\\|CONTEXT\\|USE\\|FINISH\\|KEEP"
53 7f103319 2021-11-06 op "\\|ALIEN"
54 7f103319 2021-11-06 op "\\|MODE\\|OP\\|PRIO\\|PROC"
55 7f103319 2021-11-06 op "\\|PROC"
56 7f103319 2021-11-06 op "\\|OF\\|AT\\|IS\\|ISNT\\|EMPTY\\|SKIP"
57 7f103319 2021-11-06 op "\\|PR\\|PRAGMAT"
58 7f103319 2021-11-06 op "\\|CASE\\|IN\\|OUSE\\|OUT\\|ESAC\\|"
59 7f103319 2021-11-06 op "\\|FOR\\|FORALL\\|FROM\\|TO\\|BY\\|WHILE\\|DO\\|OD"
60 7f103319 2021-11-06 op "\\|IF\\|THEN\\|ELIF\\|THEN\\|ELSE\\|FI"
61 7f103319 2021-11-06 op "\\|PAR\\|BEGIN\\|END\\|GOTO\\|EXIT"
62 7f103319 2021-11-06 op "\\|LWB\\|UPB\\|NOT\\|ABS\\|BIN\\|REPR\\|LENG\\|SHORTEN\\|ODD\\|SIGN\\|ROUND\\ENTIER"
63 7f103319 2021-11-06 op "\\|AND\\|OR\\|DIV\\|OVER\\|MOD\\|ELEM\\|SHL\\|SHR\\|IS\\|ISNT"
64 7f103319 2021-11-06 op "\\|OVERAB\\|DIVAB\\|MODAB"
65 7f103319 2021-11-06 op "\\|REF"
66 7f103319 2021-11-06 op "\\)\\>")
67 7f103319 2021-11-06 op 'font-lock-keyword-face)
68 7f103319 2021-11-06 op (cons (concat "\\<\\("
69 7f103319 2021-11-06 op "TRUE\\|\\FALSE"
70 7f103319 2021-11-06 op "\\)\\>")
71 7f103319 2021-11-06 op 'font-lock-constant-face)
72 7f103319 2021-11-06 op ;; Note that the following rule is only valid for bold stropping.
73 7f103319 2021-11-06 op (cons (concat "\\<[A-Z]+\\>") 'font-lock-type-face)
74 7f103319 2021-11-06 op
75 7f103319 2021-11-06 op (cons "\\('\\w*'\\)"
76 7f103319 2021-11-06 op font-lock-variable-name-face))
77 a6ba7179 2021-11-06 op "Highlighting expressions for Algol 68 mode.")
78 7f103319 2021-11-06 op
79 7f103319 2021-11-06 op (defun a68-within-string ()
80 a6ba7179 2021-11-06 op "Check if inside a string."
81 7f103319 2021-11-06 op (nth 3 (syntax-ppss)))
82 7f103319 2021-11-06 op
83 7f103319 2021-11-06 op (defun a68-within-comment ()
84 a6ba7179 2021-11-06 op "Check if inside a comment."
85 7f103319 2021-11-06 op (nth 4 (syntax-ppss)))
86 7f103319 2021-11-06 op
87 7f103319 2021-11-06 op ;; Indentation rules:
88 7f103319 2021-11-06 op ;;
89 7f103319 2021-11-06 op ;; - If we are at the beginning of the buffer, or looking at some
90 7f103319 2021-11-06 op ;; indent-0 content, indent to column 0.
91 7f103319 2021-11-06 op ;;
92 7f103319 2021-11-06 op ;; - If we are currently at an END, ), FI or OD, then de-indent
93 7f103319 2021-11-06 op ;; relative to the previous line.
94 7f103319 2021-11-06 op ;;
95 7f103319 2021-11-06 op ;; - If we first see and "end line" before our current line,
96 7f103319 2021-11-06 op ;; then we should indent our current line to the same indentation as
97 7f103319 2021-11-06 op ;; the end line.
98 7f103319 2021-11-06 op ;;
99 7f103319 2021-11-06 op ;; - If we first see a "start line" like IF, then we need to increase
100 7f103319 2021-11-06 op ;; our indentation relative to that start line.
101 7f103319 2021-11-06 op ;;
102 7f103319 2021-11-06 op ;; - If into a balanced expression, we should indent to the column
103 7f103319 2021-11-06 op ;; where the start of the innermost parenthetical group.
104 7f103319 2021-11-06 op ;;
105 7f103319 2021-11-06 op ;; - If none of the above apply, then do not indent at all.
106 7f103319 2021-11-06 op
107 7f103319 2021-11-06 op (defun a68-indent-line ()
108 a6ba7179 2021-11-06 op "Indent current line as Algol 68 code."
109 7f103319 2021-11-06 op (interactive)
110 7f103319 2021-11-06 op (let ((case-fold-search nil))
111 7f103319 2021-11-06 op (save-excursion
112 7f103319 2021-11-06 op (beginning-of-line)
113 7f103319 2021-11-06 op (if (nth 1 (syntax-ppss)) ; Check for rule 5
114 7f103319 2021-11-06 op (let ((offset (save-excursion (goto-char (+ (nth 1 (syntax-ppss)) 1))
115 7f103319 2021-11-06 op (current-column))))
116 7f103319 2021-11-06 op (indent-line-to offset))
117 7f103319 2021-11-06 op (if (or (bobp) ; Check for rule 1
118 7f103319 2021-11-06 op (looking-at "^[ \t]*\\<\\(KEEP\\|FINISH\\|DECS\\|USE\\|PROGRAM\\)\\>"))
119 7f103319 2021-11-06 op (indent-line-to 0)
120 7f103319 2021-11-06 op (let ((not-indented t)
121 7f103319 2021-11-06 op (prev-indent (current-indentation))
122 7f103319 2021-11-06 op (begin-indent-re "^[ \t]*\\<\\(PAR\\|BEGIN\\|KEEP\\|IF\\|DO\\|ELSE\\|ELIF\\|THEN\\)")
123 7f103319 2021-11-06 op (deindent-line-re "^[ \t]*\\<\\(END\\|FI\\|OD\\|ELSE\\|ELIF\\)\\>")
124 7f103319 2021-11-06 op (eqindent-line-re "^[ \t]*\\<\\(THEN\\)\\>")
125 7f103319 2021-11-06 op (end-line-re "^[ \t]*\\(END\\|FI\\|OD\\)")
126 7f103319 2021-11-06 op cur-indent)
127 7f103319 2021-11-06 op (if (looking-at eqindent-line-re)
128 7f103319 2021-11-06 op (save-excursion
129 7f103319 2021-11-06 op (forward-line -1)
130 7f103319 2021-11-06 op (setq cur-indent (current-indentation)))
131 7f103319 2021-11-06 op (if (looking-at deindent-line-re) ; Check for rule 2
132 7f103319 2021-11-06 op (progn
133 7f103319 2021-11-06 op (save-excursion
134 7f103319 2021-11-06 op (forward-line -1)
135 7f103319 2021-11-06 op (setq cur-indent (- (current-indentation) a68-indent-step)))
136 7f103319 2021-11-06 op (if (< cur-indent 0)
137 7f103319 2021-11-06 op (setq cur-indent 0)))
138 7f103319 2021-11-06 op (save-excursion
139 7f103319 2021-11-06 op (while not-indented
140 7f103319 2021-11-06 op (forward-line -1)
141 7f103319 2021-11-06 op (if (looking-at end-line-re) ; Check for rule 3
142 7f103319 2021-11-06 op (progn
143 7f103319 2021-11-06 op (setq cur-indent (current-indentation))
144 7f103319 2021-11-06 op (setq not-indented nil))
145 7f103319 2021-11-06 op ;; Check for rule 4
146 7f103319 2021-11-06 op (if (looking-at begin-indent-re)
147 7f103319 2021-11-06 op (progn
148 7f103319 2021-11-06 op (setq cur-indent (+ (current-indentation) a68-indent-step))
149 7f103319 2021-11-06 op (setq not-indented nil))
150 7f103319 2021-11-06 op (if (bobp) ; Check for rule 5
151 7f103319 2021-11-06 op (setq not-indented nil))))))))
152 7f103319 2021-11-06 op (if cur-indent
153 7f103319 2021-11-06 op (indent-line-to cur-indent)
154 7f103319 2021-11-06 op ;; If we didn't see an indentation hint, then allow no
155 7f103319 2021-11-06 op ;; indentation.
156 7f103319 2021-11-06 op (indent-line-to 0)))))))
157 7f103319 2021-11-06 op (when (< (current-column) (current-indentation))
158 7f103319 2021-11-06 op (move-to-column (current-indentation))))
159 7f103319 2021-11-06 op
160 7f103319 2021-11-06 op (defvar a68-mode-syntax-table
161 7f103319 2021-11-06 op (let ((st (make-syntax-table)))
162 7f103319 2021-11-06 op (modify-syntax-entry ?{ "<" st)
163 7f103319 2021-11-06 op (modify-syntax-entry ?# "<" st)
164 7f103319 2021-11-06 op (modify-syntax-entry ?} ">" st)
165 7f103319 2021-11-06 op (modify-syntax-entry ?# ">" st)
166 7f103319 2021-11-06 op (modify-syntax-entry ?\\ "." st)
167 7f103319 2021-11-06 op ;; (modify-syntax-entry ?C "< 13" st)
168 7f103319 2021-11-06 op ;; (modify-syntax-entry ?O "> 24" st)
169 7f103319 2021-11-06 op ;; define parentheses to match
170 7f103319 2021-11-06 op (modify-syntax-entry ?\( "()" st)
171 7f103319 2021-11-06 op (modify-syntax-entry ?\) ")(" st)
172 7f103319 2021-11-06 op st))
173 7f103319 2021-11-06 op
174 7f103319 2021-11-06 op ;;;
175 7f103319 2021-11-06 op ;;; Electric functions
176 7f103319 2021-11-06 op ;;;
177 7f103319 2021-11-06 op
178 7f103319 2021-11-06 op (defconst a68-autoindent-lines-re
179 7f103319 2021-11-06 op "\\<\\(BEGIN\\|END\\|ELSE\\|ELIF\\|DO\\|OD\\|CASE\\|ESAC\\|IN\\|OUT\\)\\>")
180 7f103319 2021-11-06 op
181 23be3ba2 2021-11-06 op (defun a68-electric-terminate-line ()
182 7f103319 2021-11-06 op "Terminate line and indent next line."
183 7f103319 2021-11-06 op (interactive)
184 7f103319 2021-11-06 op ;; First, check if current line should be indented
185 7f103319 2021-11-06 op (save-excursion
186 7f103319 2021-11-06 op (beginning-of-line)
187 7f103319 2021-11-06 op (skip-chars-forward " \t")
188 7f103319 2021-11-06 op (if (looking-at a68-autoindent-lines-re)
189 7f103319 2021-11-06 op (a68-indent-line)))
190 7f103319 2021-11-06 op (delete-horizontal-space) ; Removes triling whitespaces
191 7f103319 2021-11-06 op ;; Indent next line if we are not in a string
192 7f103319 2021-11-06 op (let ((in-string (a68-within-string)))
193 7f103319 2021-11-06 op (newline)
194 7f103319 2021-11-06 op (unless in-string
195 7f103319 2021-11-06 op (a68-indent-line))))
196 7f103319 2021-11-06 op
197 23be3ba2 2021-11-06 op (defun a68-electric-tab ()
198 7f103319 2021-11-06 op "Function called when TAB is pressed in Algol68 mode."
199 7f103319 2021-11-06 op (interactive)
200 7f103319 2021-11-06 op (unless (save-excursion
201 7f103319 2021-11-06 op (beginning-of-line)
202 7f103319 2021-11-06 op (a68-within-string))
203 7f103319 2021-11-06 op (a68-indent-line)))
204 7f103319 2021-11-06 op
205 962671ab 2021-11-06 op ;;;###autoload
206 962671ab 2021-11-06 op (define-derived-mode a68-mode prog-mode "Algol68"
207 962671ab 2021-11-06 op "Major mode for editing Alogl68 files."
208 962671ab 2021-11-06 op (setq-local font-lock-defaults '(a68-font-lock-keywords))
209 962671ab 2021-11-06 op (setq-local indent-line-function #'a68-indent-line)
210 962671ab 2021-11-06 op (setq-local comment-start "#")
211 64609eee 2021-11-06 op (setq-local comment-end "#"))
212 7f103319 2021-11-06 op
213 6c0a5a76 2021-11-06 op ;;;###autoload
214 6c0a5a76 2021-11-06 op (add-to-list 'auto-mode-alist '("\\.a68\\'" . a68-mode))
215 6c0a5a76 2021-11-06 op
216 7f103319 2021-11-06 op (provide 'algol-mode)
217 a6ba7179 2021-11-06 op ;;; algol-mode.el ends here