Blob


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