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