Commit | Line | Data |
---|---|---|
1a06eabd ER |
1 | ;;; icon.el --- mode for editing Icon code |
2 | ||
9750e079 ER |
3 | ;; Copyright (C) 1989 Free Software Foundation, Inc. |
4 | ||
33069f58 | 5 | ;; Author: Chris Smith <csmith@convex.com> |
e5167999 | 6 | ;; Created: 15 Feb 89 |
fd7fa35a | 7 | ;; Keywords: languages |
a2535589 | 8 | |
a2535589 JA |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
a2535589 | 25 | |
e5167999 ER |
26 | ;;; Commentary: |
27 | ||
e41b2db1 | 28 | ;; A major mode for editing the Icon programming language. |
e5167999 ER |
29 | |
30 | ;;; Code: | |
a2535589 JA |
31 | |
32 | (defvar icon-mode-abbrev-table nil | |
33 | "Abbrev table in use in Icon-mode buffers.") | |
34 | (define-abbrev-table 'icon-mode-abbrev-table ()) | |
35 | ||
36 | (defvar icon-mode-map () | |
37 | "Keymap used in Icon mode.") | |
38 | (if icon-mode-map | |
39 | () | |
78d7cf68 RS |
40 | (let ((map (make-sparse-keymap "Icon"))) |
41 | (setq icon-mode-map (make-sparse-keymap)) | |
42 | (define-key icon-mode-map "{" 'electric-icon-brace) | |
43 | (define-key icon-mode-map "}" 'electric-icon-brace) | |
44 | (define-key icon-mode-map "\e\C-h" 'mark-icon-function) | |
45 | (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun) | |
46 | (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun) | |
47 | (define-key icon-mode-map "\e\C-q" 'indent-icon-exp) | |
48 | (define-key icon-mode-map "\177" 'backward-delete-char-untabify) | |
a1506d29 | 49 | |
26ee4ca3 | 50 | (define-key icon-mode-map [menu-bar] (make-sparse-keymap "Icon")) |
78d7cf68 RS |
51 | (define-key icon-mode-map [menu-bar icon] |
52 | (cons "Icon" map)) | |
53 | (define-key map [beginning-of-icon-defun] '("Beginning of function" . beginning-of-icon-defun)) | |
54 | (define-key map [end-of-icon-defun] '("End of function" . end-of-icon-defun)) | |
55 | (define-key map [comment-region] '("Comment Out Region" . comment-region)) | |
56 | (define-key map [indent-region] '("Indent Region" . indent-region)) | |
57 | (define-key map [indent-line] '("Indent Line" . icon-indent-command)) | |
58 | (put 'eval-region 'menu-enable 'mark-active) | |
59 | (put 'comment-region 'menu-enable 'mark-active) | |
60 | (put 'indent-region 'menu-enable 'mark-active))) | |
a2535589 JA |
61 | |
62 | (defvar icon-mode-syntax-table nil | |
63 | "Syntax table in use in Icon-mode buffers.") | |
64 | ||
65 | (if icon-mode-syntax-table | |
66 | () | |
67 | (setq icon-mode-syntax-table (make-syntax-table)) | |
68 | (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table) | |
69 | (modify-syntax-entry ?# "<" icon-mode-syntax-table) | |
70 | (modify-syntax-entry ?\n ">" icon-mode-syntax-table) | |
71 | (modify-syntax-entry ?$ "." icon-mode-syntax-table) | |
72 | (modify-syntax-entry ?/ "." icon-mode-syntax-table) | |
73 | (modify-syntax-entry ?* "." icon-mode-syntax-table) | |
74 | (modify-syntax-entry ?+ "." icon-mode-syntax-table) | |
75 | (modify-syntax-entry ?- "." icon-mode-syntax-table) | |
76 | (modify-syntax-entry ?= "." icon-mode-syntax-table) | |
77 | (modify-syntax-entry ?% "." icon-mode-syntax-table) | |
78 | (modify-syntax-entry ?< "." icon-mode-syntax-table) | |
79 | (modify-syntax-entry ?> "." icon-mode-syntax-table) | |
80 | (modify-syntax-entry ?& "." icon-mode-syntax-table) | |
81 | (modify-syntax-entry ?| "." icon-mode-syntax-table) | |
82 | (modify-syntax-entry ?\' "\"" icon-mode-syntax-table)) | |
83 | ||
da37a9b4 RS |
84 | (defgroup icon nil |
85 | "Mode for editing Icon code." | |
86 | :group 'languages) | |
87 | ||
88 | (defcustom icon-indent-level 4 | |
89 | "*Indentation of Icon statements with respect to containing block." | |
90 | :type 'integer | |
91 | :group 'icon) | |
92 | ||
93 | (defcustom icon-brace-imaginary-offset 0 | |
94 | "*Imagined indentation of a Icon open brace that actually follows a statement." | |
95 | :type 'integer | |
96 | :group 'icon) | |
97 | ||
98 | (defcustom icon-brace-offset 0 | |
99 | "*Extra indentation for braces, compared with other text in same context." | |
100 | :type 'integer | |
101 | :group 'icon) | |
102 | ||
103 | (defcustom icon-continued-statement-offset 4 | |
104 | "*Extra indent for Icon lines not starting new statements." | |
105 | :type 'integer | |
106 | :group 'icon) | |
107 | ||
108 | (defcustom icon-continued-brace-offset 0 | |
109 | "*Extra indent for Icon substatements that start with open-braces. | |
110 | This is in addition to `icon-continued-statement-offset'." | |
111 | :type 'integer | |
112 | :group 'icon) | |
113 | ||
114 | (defcustom icon-auto-newline nil | |
115 | "*Non-nil means automatically newline before and after braces Icon code. | |
116 | This applies when braces are inserted." | |
117 | :type 'boolean | |
118 | :group 'icon) | |
119 | ||
120 | (defcustom icon-tab-always-indent t | |
121 | "*Non-nil means TAB in Icon mode should always reindent the current line. | |
122 | It will then reindent, regardless of where in the line point is | |
123 | when the TAB command is used." | |
8c36e648 | 124 | :type 'boolean |
da37a9b4 | 125 | :group 'icon) |
78d7cf68 RS |
126 | |
127 | (defvar icon-imenu-generic-expression | |
b0edcc39 | 128 | '((nil "^[ \t]*procedure[ \t]+\\(\\sw+\\)[ \t]*(" 1)) |
da37a9b4 | 129 | "Imenu expression for Icon mode. See `imenu-generic-expression'.") |
78d7cf68 RS |
130 | |
131 | ||
a2535589 | 132 | \f |
e3ead21b | 133 | ;;;###autoload |
a2535589 JA |
134 | (defun icon-mode () |
135 | "Major mode for editing Icon code. | |
136 | Expression and list commands understand all Icon brackets. | |
137 | Tab indents for Icon code. | |
138 | Paragraphs are separated by blank lines only. | |
139 | Delete converts tabs to spaces as it moves back. | |
140 | \\{icon-mode-map} | |
141 | Variables controlling indentation style: | |
142 | icon-tab-always-indent | |
143 | Non-nil means TAB in Icon mode should always reindent the current line, | |
144 | regardless of where in the line point is when the TAB command is used. | |
145 | icon-auto-newline | |
146 | Non-nil means automatically newline before and after braces | |
147 | inserted in Icon code. | |
148 | icon-indent-level | |
149 | Indentation of Icon statements within surrounding block. | |
150 | The surrounding block's indentation is the indentation | |
151 | of the line on which the open-brace appears. | |
152 | icon-continued-statement-offset | |
153 | Extra indentation given to a substatement, such as the | |
154 | then-clause of an if or body of a while. | |
155 | icon-continued-brace-offset | |
156 | Extra indentation given to a brace that starts a substatement. | |
073c9531 | 157 | This is in addition to `icon-continued-statement-offset'. |
a2535589 JA |
158 | icon-brace-offset |
159 | Extra indentation for line if it starts with an open brace. | |
160 | icon-brace-imaginary-offset | |
161 | An open brace following other text is treated as if it were | |
162 | this far to the right of the start of its line. | |
163 | ||
073c9531 JB |
164 | Turning on Icon mode calls the value of the variable `icon-mode-hook' |
165 | with no args, if that value is non-nil." | |
a2535589 JA |
166 | (interactive) |
167 | (kill-all-local-variables) | |
168 | (use-local-map icon-mode-map) | |
169 | (setq major-mode 'icon-mode) | |
170 | (setq mode-name "Icon") | |
171 | (setq local-abbrev-table icon-mode-abbrev-table) | |
172 | (set-syntax-table icon-mode-syntax-table) | |
173 | (make-local-variable 'paragraph-start) | |
67468628 | 174 | (setq paragraph-start (concat "$\\|" page-delimiter)) |
a2535589 JA |
175 | (make-local-variable 'paragraph-separate) |
176 | (setq paragraph-separate paragraph-start) | |
177 | (make-local-variable 'indent-line-function) | |
178 | (setq indent-line-function 'icon-indent-line) | |
179 | (make-local-variable 'require-final-newline) | |
180 | (setq require-final-newline t) | |
181 | (make-local-variable 'comment-start) | |
182 | (setq comment-start "# ") | |
183 | (make-local-variable 'comment-end) | |
184 | (setq comment-end "") | |
a2535589 JA |
185 | (make-local-variable 'comment-start-skip) |
186 | (setq comment-start-skip "# *") | |
e41b2db1 ER |
187 | (make-local-variable 'comment-indent-function) |
188 | (setq comment-indent-function 'icon-comment-indent) | |
7b371301 | 189 | (set (make-local-variable 'indent-line-function) 'icon-indent-line) |
78d7cf68 | 190 | ;; font-lock support |
a1506d29 | 191 | (setq font-lock-defaults |
78d7cf68 | 192 | '((icon-font-lock-keywords |
b0edcc39 | 193 | icon-font-lock-keywords-1 icon-font-lock-keywords-2) |
78d7cf68 | 194 | nil nil ((?_ . "w")) beginning-of-defun |
b0edcc39 RS |
195 | ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP. |
196 | ;(font-lock-comment-start-regexp . "#") | |
78d7cf68 RS |
197 | (font-lock-mark-block-function . mark-defun))) |
198 | ;; imenu support | |
199 | (make-local-variable 'imenu-generic-expression) | |
200 | (setq imenu-generic-expression icon-imenu-generic-expression) | |
201 | ;; hideshow support | |
202 | ;; we start from the assertion that `hs-special-modes-alist' is autoloaded. | |
b0edcc39 RS |
203 | (unless (assq 'icon-mode hs-special-modes-alist) |
204 | (setq hs-special-modes-alist | |
a1506d29 | 205 | (cons '(icon-mode "\\<procedure\\>" "\\<end\\>" nil |
b0edcc39 RS |
206 | icon-forward-sexp-function) |
207 | hs-special-modes-alist))) | |
a2535589 JA |
208 | (run-hooks 'icon-mode-hook)) |
209 | \f | |
073c9531 JB |
210 | ;; This is used by indent-for-comment to decide how much to |
211 | ;; indent a comment in Icon code based on its context. | |
a2535589 | 212 | (defun icon-comment-indent () |
7b371301 | 213 | (if (looking-at "^#") 0 comment-column)) |
a2535589 JA |
214 | |
215 | (defun electric-icon-brace (arg) | |
216 | "Insert character and correct line's indentation." | |
217 | (interactive "P") | |
218 | (let (insertpos) | |
219 | (if (and (not arg) | |
220 | (eolp) | |
221 | (or (save-excursion | |
222 | (skip-chars-backward " \t") | |
223 | (bolp)) | |
224 | (if icon-auto-newline | |
225 | (progn (icon-indent-line) (newline) t) | |
226 | nil))) | |
227 | (progn | |
228 | (insert last-command-char) | |
229 | (icon-indent-line) | |
230 | (if icon-auto-newline | |
231 | (progn | |
232 | (newline) | |
233 | ;; (newline) may have done auto-fill | |
234 | (setq insertpos (- (point) 2)) | |
235 | (icon-indent-line))) | |
236 | (save-excursion | |
237 | (if insertpos (goto-char (1+ insertpos))) | |
238 | (delete-char -1)))) | |
239 | (if insertpos | |
240 | (save-excursion | |
241 | (goto-char insertpos) | |
242 | (self-insert-command (prefix-numeric-value arg))) | |
243 | (self-insert-command (prefix-numeric-value arg))))) | |
244 | \f | |
245 | (defun icon-indent-command (&optional whole-exp) | |
a2535589 | 246 | "Indent current line as Icon code, or in some cases insert a tab character. |
073c9531 JB |
247 | If `icon-tab-always-indent' is non-nil (the default), always indent current |
248 | line. Otherwise, indent the current line only if point is at the left margin | |
a2535589 JA |
249 | or in the line's indentation; otherwise insert a tab. |
250 | ||
073c9531 JB |
251 | A numeric argument, regardless of its value, means indent rigidly all the |
252 | lines of the expression starting after point so that this line becomes | |
253 | properly indented. The relative indentation among the lines of the | |
254 | expression are preserved." | |
6b61353c | 255 | (interactive "P") |
a2535589 JA |
256 | (if whole-exp |
257 | ;; If arg, always indent this line as Icon | |
258 | ;; and shift remaining lines of expression the same amount. | |
259 | (let ((shift-amt (icon-indent-line)) | |
260 | beg end) | |
261 | (save-excursion | |
262 | (if icon-tab-always-indent | |
263 | (beginning-of-line)) | |
264 | (setq beg (point)) | |
265 | (forward-sexp 1) | |
266 | (setq end (point)) | |
267 | (goto-char beg) | |
268 | (forward-line 1) | |
269 | (setq beg (point))) | |
270 | (if (> end beg) | |
271 | (indent-code-rigidly beg end shift-amt "#"))) | |
272 | (if (and (not icon-tab-always-indent) | |
273 | (save-excursion | |
274 | (skip-chars-backward " \t") | |
275 | (not (bolp)))) | |
276 | (insert-tab) | |
277 | (icon-indent-line)))) | |
278 | ||
279 | (defun icon-indent-line () | |
280 | "Indent current line as Icon code. | |
281 | Return the amount the indentation changed by." | |
282 | (let ((indent (calculate-icon-indent nil)) | |
283 | beg shift-amt | |
284 | (case-fold-search nil) | |
285 | (pos (- (point-max) (point)))) | |
286 | (beginning-of-line) | |
287 | (setq beg (point)) | |
288 | (cond ((eq indent nil) | |
289 | (setq indent (current-indentation))) | |
0c1a49d3 | 290 | ((looking-at "^#") |
a2535589 JA |
291 | (setq indent 0)) |
292 | (t | |
293 | (skip-chars-forward " \t") | |
294 | (if (listp indent) (setq indent (car indent))) | |
295 | (cond ((and (looking-at "else\\b") | |
296 | (not (looking-at "else\\s_"))) | |
297 | (setq indent (save-excursion | |
298 | (icon-backward-to-start-of-if) | |
299 | (current-indentation)))) | |
300 | ((or (= (following-char) ?}) | |
301 | (looking-at "end\\b")) | |
302 | (setq indent (- indent icon-indent-level))) | |
303 | ((= (following-char) ?{) | |
304 | (setq indent (+ indent icon-brace-offset)))))) | |
305 | (skip-chars-forward " \t") | |
306 | (setq shift-amt (- indent (current-column))) | |
307 | (if (zerop shift-amt) | |
308 | (if (> (- (point-max) pos) (point)) | |
309 | (goto-char (- (point-max) pos))) | |
310 | (delete-region beg (point)) | |
311 | (indent-to indent) | |
312 | ;; If initial point was within line's indentation, | |
313 | ;; position after the indentation. Else stay at same point in text. | |
314 | (if (> (- (point-max) pos) (point)) | |
315 | (goto-char (- (point-max) pos)))) | |
316 | shift-amt)) | |
317 | ||
318 | (defun calculate-icon-indent (&optional parse-start) | |
319 | "Return appropriate indentation for current line as Icon code. | |
320 | In usual case returns an integer: the column to indent to. | |
321 | Returns nil if line starts inside a string, t if in a comment." | |
322 | (save-excursion | |
323 | (beginning-of-line) | |
324 | (let ((indent-point (point)) | |
325 | (case-fold-search nil) | |
326 | state | |
327 | containing-sexp | |
328 | toplevel) | |
329 | (if parse-start | |
330 | (goto-char parse-start) | |
331 | (setq toplevel (beginning-of-icon-defun))) | |
332 | (while (< (point) indent-point) | |
333 | (setq parse-start (point)) | |
334 | (setq state (parse-partial-sexp (point) indent-point 0)) | |
335 | (setq containing-sexp (car (cdr state)))) | |
336 | (cond ((or (nth 3 state) (nth 4 state)) | |
337 | ;; return nil or t if should not change this line | |
338 | (nth 4 state)) | |
339 | ((and containing-sexp | |
340 | (/= (char-after containing-sexp) ?{)) | |
341 | ;; line is expression, not statement: | |
342 | ;; indent to just after the surrounding open. | |
343 | (goto-char (1+ containing-sexp)) | |
344 | (current-column)) | |
345 | (t | |
346 | (if toplevel | |
347 | ;; Outside any procedures. | |
348 | (progn (icon-backward-to-noncomment (point-min)) | |
349 | (if (icon-is-continuation-line) | |
350 | icon-continued-statement-offset 0)) | |
351 | ;; Statement level. | |
352 | (if (null containing-sexp) | |
353 | (progn (beginning-of-icon-defun) | |
354 | (setq containing-sexp (point)))) | |
355 | (goto-char indent-point) | |
356 | ;; Is it a continuation or a new statement? | |
357 | ;; Find previous non-comment character. | |
358 | (icon-backward-to-noncomment containing-sexp) | |
359 | ;; Now we get the answer. | |
360 | (if (icon-is-continuation-line) | |
361 | ;; This line is continuation of preceding line's statement; | |
362 | ;; indent icon-continued-statement-offset more than the | |
363 | ;; first line of the statement. | |
364 | (progn | |
365 | (icon-backward-to-start-of-continued-exp containing-sexp) | |
366 | (+ icon-continued-statement-offset (current-column) | |
367 | (if (save-excursion (goto-char indent-point) | |
368 | (skip-chars-forward " \t") | |
369 | (eq (following-char) ?{)) | |
370 | icon-continued-brace-offset 0))) | |
371 | ;; This line starts a new statement. | |
372 | ;; Position following last unclosed open. | |
373 | (goto-char containing-sexp) | |
374 | ;; Is line first statement after an open-brace? | |
375 | (or | |
376 | ;; If no, find that first statement and indent like it. | |
377 | (save-excursion | |
378 | (if (looking-at "procedure\\s ") | |
379 | (forward-sexp 3) | |
380 | (forward-char 1)) | |
381 | (while (progn (skip-chars-forward " \t\n") | |
382 | (looking-at "#")) | |
383 | ;; Skip over comments following openbrace. | |
384 | (forward-line 1)) | |
385 | ;; The first following code counts | |
386 | ;; if it is before the line we want to indent. | |
387 | (and (< (point) indent-point) | |
388 | (current-column))) | |
389 | ;; If no previous statement, | |
390 | ;; indent it relative to line brace is on. | |
391 | ;; For open brace in column zero, don't let statement | |
392 | ;; start there too. If icon-indent-level is zero, | |
393 | ;; use icon-brace-offset + icon-continued-statement-offset | |
394 | ;; instead. | |
395 | ;; For open-braces not the first thing in a line, | |
396 | ;; add in icon-brace-imaginary-offset. | |
397 | (+ (if (and (bolp) (zerop icon-indent-level)) | |
398 | (+ icon-brace-offset | |
399 | icon-continued-statement-offset) | |
400 | icon-indent-level) | |
401 | ;; Move back over whitespace before the openbrace. | |
402 | ;; If openbrace is not first nonwhite thing on the line, | |
403 | ;; add the icon-brace-imaginary-offset. | |
404 | (progn (skip-chars-backward " \t") | |
405 | (if (bolp) 0 icon-brace-imaginary-offset)) | |
406 | ;; Get initial indentation of the line we are on. | |
407 | (current-indentation)))))))))) | |
408 | ||
409 | ;; List of words to check for as the last thing on a line. | |
410 | ;; If cdr is t, next line is a continuation of the same statement, | |
411 | ;; if cdr is nil, next line starts a new (possibly indented) statement. | |
412 | ||
413 | (defconst icon-resword-alist | |
414 | '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else") | |
415 | ("every" . t) ("if" . t) ("global" . t) ("initial" . t) | |
416 | ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t) | |
417 | ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t))) | |
418 | ||
419 | (defun icon-is-continuation-line () | |
420 | (let* ((ch (preceding-char)) | |
421 | (ch-syntax (char-syntax ch))) | |
422 | (if (eq ch-syntax ?w) | |
423 | (assoc (buffer-substring | |
424 | (progn (forward-word -1) (point)) | |
425 | (progn (forward-word 1) (point))) | |
426 | icon-resword-alist) | |
0c1a49d3 | 427 | (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\# ?\, ?\. ?\n)))))) |
a2535589 JA |
428 | |
429 | (defun icon-backward-to-noncomment (lim) | |
430 | (let (opoint stop) | |
431 | (while (not stop) | |
432 | (skip-chars-backward " \t\n\f" lim) | |
433 | (setq opoint (point)) | |
434 | (beginning-of-line) | |
435 | (if (and (nth 5 (parse-partial-sexp (point) opoint)) | |
436 | (< lim (point))) | |
437 | (search-backward "#") | |
438 | (setq stop t))))) | |
439 | ||
440 | (defun icon-backward-to-start-of-continued-exp (lim) | |
441 | (if (memq (preceding-char) '(?\) ?\])) | |
442 | (forward-sexp -1)) | |
443 | (beginning-of-line) | |
444 | (skip-chars-forward " \t") | |
445 | (cond | |
446 | ((<= (point) lim) (goto-char (1+ lim))) | |
447 | ((not (icon-is-continued-line)) 0) | |
448 | ((and (eq (char-syntax (following-char)) ?w) | |
449 | (cdr | |
450 | (assoc (buffer-substring (point) | |
451 | (save-excursion (forward-word 1) (point))) | |
452 | icon-resword-alist))) 0) | |
453 | (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim)))) | |
454 | ||
455 | (defun icon-is-continued-line () | |
456 | (save-excursion | |
457 | (end-of-line 0) | |
458 | (icon-is-continuation-line))) | |
459 | ||
460 | (defun icon-backward-to-start-of-if (&optional limit) | |
073c9531 | 461 | "Move to the start of the last \"unbalanced\" if." |
a2535589 JA |
462 | (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point)))) |
463 | (let ((if-level 1) | |
464 | (case-fold-search nil)) | |
465 | (while (not (zerop if-level)) | |
466 | (backward-sexp 1) | |
467 | (cond ((looking-at "else\\b") | |
468 | (setq if-level (1+ if-level))) | |
469 | ((looking-at "if\\b") | |
470 | (setq if-level (1- if-level))) | |
471 | ((< (point) limit) | |
472 | (setq if-level 0) | |
473 | (goto-char limit)))))) | |
474 | \f | |
475 | (defun mark-icon-function () | |
476 | "Put mark at end of Icon function, point at beginning." | |
477 | (interactive) | |
478 | (push-mark (point)) | |
479 | (end-of-icon-defun) | |
480 | (push-mark (point)) | |
481 | (beginning-of-line 0) | |
482 | (beginning-of-icon-defun)) | |
483 | ||
484 | (defun beginning-of-icon-defun () | |
485 | "Go to the start of the enclosing procedure; return t if at top level." | |
486 | (interactive) | |
487 | (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move) | |
488 | (looking-at "e") | |
489 | t)) | |
490 | ||
491 | (defun end-of-icon-defun () | |
492 | (interactive) | |
493 | (if (not (bobp)) (forward-char -1)) | |
494 | (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move) | |
495 | (forward-word -1) | |
496 | (forward-line 1)) | |
497 | \f | |
498 | (defun indent-icon-exp () | |
499 | "Indent each line of the Icon grouping following point." | |
500 | (interactive) | |
501 | (let ((indent-stack (list nil)) | |
502 | (contain-stack (list (point))) | |
503 | (case-fold-search nil) | |
504 | restart outer-loop-done inner-loop-done state ostate | |
5b1a29ab | 505 | this-indent last-sexp last-depth |
a2535589 JA |
506 | at-else at-brace at-do |
507 | (opoint (point)) | |
508 | (next-depth 0)) | |
509 | (save-excursion | |
510 | (forward-sexp 1)) | |
511 | (save-excursion | |
512 | (setq outer-loop-done nil) | |
513 | (while (and (not (eobp)) (not outer-loop-done)) | |
514 | (setq last-depth next-depth) | |
515 | ;; Compute how depth changes over this line | |
516 | ;; plus enough other lines to get to one that | |
517 | ;; does not end inside a comment or string. | |
518 | ;; Meanwhile, do appropriate indentation on comment lines. | |
5b1a29ab RS |
519 | (setq inner-loop-done nil) |
520 | (while (and (not inner-loop-done) | |
a2535589 JA |
521 | (not (and (eobp) (setq outer-loop-done t)))) |
522 | (setq ostate state) | |
523 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
524 | nil nil state)) | |
525 | (setq next-depth (car state)) | |
526 | (if (and (car (cdr (cdr state))) | |
527 | (>= (car (cdr (cdr state))) 0)) | |
528 | (setq last-sexp (car (cdr (cdr state))))) | |
529 | (if (or (nth 4 ostate)) | |
530 | (icon-indent-line)) | |
531 | (if (or (nth 3 state)) | |
532 | (forward-line 1) | |
5b1a29ab | 533 | (setq inner-loop-done t))) |
a2535589 JA |
534 | (if (<= next-depth 0) |
535 | (setq outer-loop-done t)) | |
536 | (if outer-loop-done | |
537 | nil | |
538 | (if (/= last-depth next-depth) | |
539 | (setq last-sexp nil)) | |
540 | (while (> last-depth next-depth) | |
541 | (setq indent-stack (cdr indent-stack) | |
542 | contain-stack (cdr contain-stack) | |
543 | last-depth (1- last-depth))) | |
544 | (while (< last-depth next-depth) | |
545 | (setq indent-stack (cons nil indent-stack) | |
546 | contain-stack (cons nil contain-stack) | |
547 | last-depth (1+ last-depth))) | |
548 | (if (null (car contain-stack)) | |
549 | (setcar contain-stack (or (car (cdr state)) | |
550 | (save-excursion (forward-sexp -1) | |
551 | (point))))) | |
552 | (forward-line 1) | |
553 | (skip-chars-forward " \t") | |
554 | (if (eolp) | |
555 | nil | |
556 | (if (and (car indent-stack) | |
557 | (>= (car indent-stack) 0)) | |
558 | ;; Line is on an existing nesting level. | |
559 | ;; Lines inside parens are handled specially. | |
560 | (if (/= (char-after (car contain-stack)) ?{) | |
561 | (setq this-indent (car indent-stack)) | |
562 | ;; Line is at statement level. | |
563 | ;; Is it a new statement? Is it an else? | |
564 | ;; Find last non-comment character before this line | |
565 | (save-excursion | |
566 | (setq at-else (looking-at "else\\W")) | |
567 | (setq at-brace (= (following-char) ?{)) | |
568 | (icon-backward-to-noncomment opoint) | |
569 | (if (icon-is-continuation-line) | |
570 | ;; Preceding line did not end in comma or semi; | |
571 | ;; indent this line icon-continued-statement-offset | |
572 | ;; more than previous. | |
573 | (progn | |
574 | (icon-backward-to-start-of-continued-exp (car contain-stack)) | |
575 | (setq this-indent | |
576 | (+ icon-continued-statement-offset (current-column) | |
577 | (if at-brace icon-continued-brace-offset 0)))) | |
578 | ;; Preceding line ended in comma or semi; | |
579 | ;; use the standard indent for this level. | |
580 | (if at-else | |
581 | (progn (icon-backward-to-start-of-if opoint) | |
582 | (setq this-indent (current-indentation))) | |
583 | (setq this-indent (car indent-stack)))))) | |
584 | ;; Just started a new nesting level. | |
585 | ;; Compute the standard indent for this level. | |
586 | (let ((val (calculate-icon-indent | |
587 | (if (car indent-stack) | |
588 | (- (car indent-stack)))))) | |
589 | (setcar indent-stack | |
590 | (setq this-indent val)))) | |
591 | ;; Adjust line indentation according to its contents | |
592 | (if (or (= (following-char) ?}) | |
593 | (looking-at "end\\b")) | |
594 | (setq this-indent (- this-indent icon-indent-level))) | |
595 | (if (= (following-char) ?{) | |
596 | (setq this-indent (+ this-indent icon-brace-offset))) | |
597 | ;; Put chosen indentation into effect. | |
598 | (or (= (current-column) this-indent) | |
599 | (progn | |
600 | (delete-region (point) (progn (beginning-of-line) (point))) | |
601 | (indent-to this-indent))) | |
602 | ;; Indent any comment following the text. | |
603 | (or (looking-at comment-start-skip) | |
604 | (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t) | |
605 | (progn (indent-for-comment) (beginning-of-line)))))))))) | |
606 | ||
78d7cf68 RS |
607 | (defconst icon-font-lock-keywords-1 |
608 | (eval-when-compile | |
609 | (list | |
610 | ;; Fontify procedure name definitions. | |
32632f66 | 611 | '("^[ \t]*\\(procedure\\)\\>[ \t]*\\(\\sw+\\)?" |
78d7cf68 | 612 | (1 font-lock-builtin-face) (2 font-lock-function-name-face nil t)))) |
b0edcc39 | 613 | "Subdued level highlighting for Icon mode.") |
78d7cf68 RS |
614 | |
615 | (defconst icon-font-lock-keywords-2 | |
a1506d29 | 616 | (append |
78d7cf68 RS |
617 | icon-font-lock-keywords-1 |
618 | (eval-when-compile | |
619 | (list | |
620 | ;; Fontify all type specifiers. | |
7b371301 SM |
621 | (cons |
622 | (regexp-opt '("null" "string" "co-expression" "table" "integer" | |
623 | "cset" "set" "real" "file" "list") 'words) | |
cd63e73b | 624 | 'font-lock-type-face) |
78d7cf68 | 625 | ;; Fontify all keywords. |
cd63e73b | 626 | ;; |
a1506d29 JB |
627 | (cons |
628 | (regexp-opt | |
629 | '("break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return" | |
630 | "until" "case" "of" "while" "create" "every" "suspend" "default" | |
7b371301 | 631 | "fail" "record" "then") 'words) |
cd63e73b | 632 | 'font-lock-keyword-face) |
a1506d29 | 633 | ;; "end" "initial" |
7b371301 | 634 | (cons (regexp-opt '("end" "initial") 'words) |
cd63e73b | 635 | 'font-lock-builtin-face) |
78d7cf68 | 636 | ;; Fontify all system variables. |
a1506d29 JB |
637 | (cons |
638 | (regexp-opt | |
639 | '("&allocated" "&ascii" "&clock" "&col" "&collections" "&column" | |
cd63e73b | 640 | "&control" "&cset" "¤t" "&date" "&dateline" "&digits" "&dump" |
a1506d29 JB |
641 | "&e" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout" |
642 | "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features" | |
643 | "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters" | |
644 | "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta" | |
645 | "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos" | |
646 | "&progname" "&random" "&rdrag" "®ions" "&resize" "&row" | |
647 | "&rpress" "&rrelease" "&shift" "&source" "&storage" "&subject" | |
cd63e73b | 648 | "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y") t) |
883212ce | 649 | 'font-lock-constant-face) |
cd63e73b | 650 | (cons ;; global local static declarations and link files |
a1506d29 | 651 | (concat |
cd63e73b RS |
652 | "^[ \t]*" |
653 | (regexp-opt '("global" "link" "local" "static") t) | |
654 | "\\(\\sw+\\>\\)*") | |
655 | '((1 font-lock-builtin-face) | |
656 | (font-lock-match-c-style-declaration-item-and-skip-to-next | |
657 | (goto-char (or (match-beginning 2) (match-end 1))) nil | |
658 | (1 (if (match-beginning 2) | |
659 | font-lock-function-name-face | |
660 | font-lock-variable-name-face))))) | |
661 | ||
32632f66 | 662 | (cons ;; $define $elif $ifdef $ifndef $undef |
a1506d29 | 663 | (concat "^" |
b0edcc39 RS |
664 | (regexp-opt'("$define" "$elif" "$ifdef" "$ifndef" "$undef") t) |
665 | "\\>[ \t]*\\([^ \t\n]+\\)?") | |
a1506d29 | 666 | '((1 font-lock-builtin-face) |
cd63e73b | 667 | (4 font-lock-variable-name-face nil t))) |
a1506d29 JB |
668 | (cons ;; $dump $endif $else $include |
669 | (concat | |
cd63e73b RS |
670 | "^" (regexp-opt'("$dump" "$endif" "$else" "$include") t) "\\>" ) |
671 | 'font-lock-builtin-face) | |
672 | (cons ;; $warning $error | |
b0edcc39 RS |
673 | (concat "^" (regexp-opt '("$warning" "$error") t) |
674 | "\\>[ \t]*\\(.+\\)?") | |
675 | '((1 font-lock-builtin-face) (3 font-lock-warning-face nil t)))))) | |
676 | "Gaudy level highlighting for Icon mode.") | |
78d7cf68 RS |
677 | |
678 | (defvar icon-font-lock-keywords icon-font-lock-keywords-1 | |
679 | "Default expressions to highlight in `icon-mode'.") | |
680 | ||
681 | ;;;used by hs-minor-mode | |
682 | (defun icon-forward-sexp-function (arg) | |
5547fcff RS |
683 | (if (< arg 0) |
684 | (beginning-of-icon-defun) | |
685 | (end-of-icon-defun) | |
686 | (forward-char -1))) | |
78d7cf68 | 687 | |
69d92b8a RS |
688 | (provide 'icon) |
689 | ||
6b61353c | 690 | ;;; arch-tag: 8abf8c99-e7df-44af-a58f-ef5ed2ee52cb |
1a06eabd | 691 | ;;; icon.el ends here |