1 ;;; icon.el --- mode for editing Icon code
3 ;; Author: Chris Smith <convex!csmith>
5 ;; Last-Modified: 06 Mar 1991
8 ;; Copyright (C) 1989 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; (autoload 'icon-mode "icon" nil t)
30 ;; (setq auto-mode-alist (cons '("\\.icn$" . icon-mode) auto-mode-alist))
31 ;; if not permanently installed in your emacs
35 (defvar icon-mode-abbrev-table nil
36 "Abbrev table in use in Icon-mode buffers.")
37 (define-abbrev-table 'icon-mode-abbrev-table
())
39 (defvar icon-mode-map
()
40 "Keymap used in Icon mode.")
43 (setq icon-mode-map
(make-sparse-keymap))
44 (define-key icon-mode-map
"{" 'electric-icon-brace
)
45 (define-key icon-mode-map
"}" 'electric-icon-brace
)
46 (define-key icon-mode-map
"\e\C-h" 'mark-icon-function
)
47 (define-key icon-mode-map
"\e\C-a" 'beginning-of-icon-defun
)
48 (define-key icon-mode-map
"\e\C-e" 'end-of-icon-defun
)
49 (define-key icon-mode-map
"\e\C-q" 'indent-icon-exp
)
50 (define-key icon-mode-map
"\177" 'backward-delete-char-untabify
)
51 (define-key icon-mode-map
"\t" 'icon-indent-command
))
53 (defvar icon-mode-syntax-table nil
54 "Syntax table in use in Icon-mode buffers.")
56 (if icon-mode-syntax-table
58 (setq icon-mode-syntax-table
(make-syntax-table))
59 (modify-syntax-entry ?
\\ "\\" icon-mode-syntax-table
)
60 (modify-syntax-entry ?
# "<" icon-mode-syntax-table
)
61 (modify-syntax-entry ?
\n ">" icon-mode-syntax-table
)
62 (modify-syntax-entry ?$
"." icon-mode-syntax-table
)
63 (modify-syntax-entry ?
/ "." icon-mode-syntax-table
)
64 (modify-syntax-entry ?
* "." icon-mode-syntax-table
)
65 (modify-syntax-entry ?
+ "." icon-mode-syntax-table
)
66 (modify-syntax-entry ?-
"." icon-mode-syntax-table
)
67 (modify-syntax-entry ?
= "." icon-mode-syntax-table
)
68 (modify-syntax-entry ?%
"." icon-mode-syntax-table
)
69 (modify-syntax-entry ?
< "." icon-mode-syntax-table
)
70 (modify-syntax-entry ?
> "." 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
))
75 (defconst icon-indent-level
4
76 "*Indentation of Icon statements with respect to containing block.")
77 (defconst icon-brace-imaginary-offset
0
78 "*Imagined indentation of a Icon open brace that actually follows a statement.")
79 (defconst icon-brace-offset
0
80 "*Extra indentation for braces, compared with other text in same context.")
81 (defconst icon-continued-statement-offset
4
82 "*Extra indent for lines not starting new statements.")
83 (defconst icon-continued-brace-offset
0
84 "*Extra indent for substatements that start with open-braces.
85 This is in addition to icon-continued-statement-offset.")
87 (defconst icon-auto-newline nil
88 "*Non-nil means automatically newline before and after braces
89 inserted in Icon code.")
91 (defconst icon-tab-always-indent t
92 "*Non-nil means TAB in Icon mode should always reindent the current line,
93 regardless of where in the line point is when the TAB command is used.")
96 "Major mode for editing Icon code.
97 Expression and list commands understand all Icon brackets.
98 Tab indents for Icon code.
99 Paragraphs are separated by blank lines only.
100 Delete converts tabs to spaces as it moves back.
102 Variables controlling indentation style:
103 icon-tab-always-indent
104 Non-nil means TAB in Icon mode should always reindent the current line,
105 regardless of where in the line point is when the TAB command is used.
107 Non-nil means automatically newline before and after braces
108 inserted in Icon code.
110 Indentation of Icon statements within surrounding block.
111 The surrounding block's indentation is the indentation
112 of the line on which the open-brace appears.
113 icon-continued-statement-offset
114 Extra indentation given to a substatement, such as the
115 then-clause of an if or body of a while.
116 icon-continued-brace-offset
117 Extra indentation given to a brace that starts a substatement.
118 This is in addition to `icon-continued-statement-offset'.
120 Extra indentation for line if it starts with an open brace.
121 icon-brace-imaginary-offset
122 An open brace following other text is treated as if it were
123 this far to the right of the start of its line.
125 Turning on Icon mode calls the value of the variable `icon-mode-hook'
126 with no args, if that value is non-nil."
128 (kill-all-local-variables)
129 (use-local-map icon-mode-map
)
130 (setq major-mode
'icon-mode
)
131 (setq mode-name
"Icon")
132 (setq local-abbrev-table icon-mode-abbrev-table
)
133 (set-syntax-table icon-mode-syntax-table
)
134 (make-local-variable 'paragraph-start
)
135 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
136 (make-local-variable 'paragraph-separate
)
137 (setq paragraph-separate paragraph-start
)
138 (make-local-variable 'indent-line-function
)
139 (setq indent-line-function
'icon-indent-line
)
140 (make-local-variable 'require-final-newline
)
141 (setq require-final-newline t
)
142 (make-local-variable 'comment-start
)
143 (setq comment-start
"# ")
144 (make-local-variable 'comment-end
)
145 (setq comment-end
"")
146 (make-local-variable 'comment-column
)
147 (setq comment-column
32)
148 (make-local-variable 'comment-start-skip
)
149 (setq comment-start-skip
"# *")
150 (make-local-variable 'comment-indent-hook
)
151 (setq comment-indent-hook
'icon-comment-indent
)
152 (run-hooks 'icon-mode-hook
))
154 ;; This is used by indent-for-comment to decide how much to
155 ;; indent a comment in Icon code based on its context.
156 (defun icon-comment-indent ()
157 (if (looking-at "^#")
160 (skip-chars-backward " \t")
161 (max (if (bolp) 0 (1+ (current-column)))
164 (defun electric-icon-brace (arg)
165 "Insert character and correct line's indentation."
171 (skip-chars-backward " \t")
173 (if icon-auto-newline
174 (progn (icon-indent-line) (newline) t
)
177 (insert last-command-char
)
179 (if icon-auto-newline
182 ;; (newline) may have done auto-fill
183 (setq insertpos
(- (point) 2))
186 (if insertpos
(goto-char (1+ insertpos
)))
190 (goto-char insertpos
)
191 (self-insert-command (prefix-numeric-value arg
)))
192 (self-insert-command (prefix-numeric-value arg
)))))
194 (defun icon-indent-command (&optional whole-exp
)
196 "Indent current line as Icon code, or in some cases insert a tab character.
197 If `icon-tab-always-indent' is non-nil (the default), always indent current
198 line. Otherwise, indent the current line only if point is at the left margin
199 or in the line's indentation; otherwise insert a tab.
201 A numeric argument, regardless of its value, means indent rigidly all the
202 lines of the expression starting after point so that this line becomes
203 properly indented. The relative indentation among the lines of the
204 expression are preserved."
206 ;; If arg, always indent this line as Icon
207 ;; and shift remaining lines of expression the same amount.
208 (let ((shift-amt (icon-indent-line))
211 (if icon-tab-always-indent
220 (indent-code-rigidly beg end shift-amt
"#")))
221 (if (and (not icon-tab-always-indent
)
223 (skip-chars-backward " \t")
226 (icon-indent-line))))
228 (defun icon-indent-line ()
229 "Indent current line as Icon code.
230 Return the amount the indentation changed by."
231 (let ((indent (calculate-icon-indent nil
))
233 (case-fold-search nil
)
234 (pos (- (point-max) (point))))
237 (cond ((eq indent nil
)
238 (setq indent
(current-indentation)))
240 (setq indent
(calculate-icon-indent-within-comment)))
241 ((looking-at "[ \t]*#")
244 (skip-chars-forward " \t")
245 (if (listp indent
) (setq indent
(car indent
)))
246 (cond ((and (looking-at "else\\b")
247 (not (looking-at "else\\s_")))
248 (setq indent
(save-excursion
249 (icon-backward-to-start-of-if)
250 (current-indentation))))
251 ((or (= (following-char) ?
})
252 (looking-at "end\\b"))
253 (setq indent
(- indent icon-indent-level
)))
254 ((= (following-char) ?
{)
255 (setq indent
(+ indent icon-brace-offset
))))))
256 (skip-chars-forward " \t")
257 (setq shift-amt
(- indent
(current-column)))
258 (if (zerop shift-amt
)
259 (if (> (- (point-max) pos
) (point))
260 (goto-char (- (point-max) pos
)))
261 (delete-region beg
(point))
263 ;; If initial point was within line's indentation,
264 ;; position after the indentation. Else stay at same point in text.
265 (if (> (- (point-max) pos
) (point))
266 (goto-char (- (point-max) pos
))))
269 (defun calculate-icon-indent (&optional parse-start
)
270 "Return appropriate indentation for current line as Icon code.
271 In usual case returns an integer: the column to indent to.
272 Returns nil if line starts inside a string, t if in a comment."
275 (let ((indent-point (point))
276 (case-fold-search nil
)
281 (goto-char parse-start
)
282 (setq toplevel
(beginning-of-icon-defun)))
283 (while (< (point) indent-point
)
284 (setq parse-start
(point))
285 (setq state
(parse-partial-sexp (point) indent-point
0))
286 (setq containing-sexp
(car (cdr state
))))
287 (cond ((or (nth 3 state
) (nth 4 state
))
288 ;; return nil or t if should not change this line
290 ((and containing-sexp
291 (/= (char-after containing-sexp
) ?
{))
292 ;; line is expression, not statement:
293 ;; indent to just after the surrounding open.
294 (goto-char (1+ containing-sexp
))
298 ;; Outside any procedures.
299 (progn (icon-backward-to-noncomment (point-min))
300 (if (icon-is-continuation-line)
301 icon-continued-statement-offset
0))
303 (if (null containing-sexp
)
304 (progn (beginning-of-icon-defun)
305 (setq containing-sexp
(point))))
306 (goto-char indent-point
)
307 ;; Is it a continuation or a new statement?
308 ;; Find previous non-comment character.
309 (icon-backward-to-noncomment containing-sexp
)
310 ;; Now we get the answer.
311 (if (icon-is-continuation-line)
312 ;; This line is continuation of preceding line's statement;
313 ;; indent icon-continued-statement-offset more than the
314 ;; first line of the statement.
316 (icon-backward-to-start-of-continued-exp containing-sexp
)
317 (+ icon-continued-statement-offset
(current-column)
318 (if (save-excursion (goto-char indent-point
)
319 (skip-chars-forward " \t")
320 (eq (following-char) ?
{))
321 icon-continued-brace-offset
0)))
322 ;; This line starts a new statement.
323 ;; Position following last unclosed open.
324 (goto-char containing-sexp
)
325 ;; Is line first statement after an open-brace?
327 ;; If no, find that first statement and indent like it.
329 (if (looking-at "procedure\\s ")
332 (while (progn (skip-chars-forward " \t\n")
334 ;; Skip over comments following openbrace.
336 ;; The first following code counts
337 ;; if it is before the line we want to indent.
338 (and (< (point) indent-point
)
340 ;; If no previous statement,
341 ;; indent it relative to line brace is on.
342 ;; For open brace in column zero, don't let statement
343 ;; start there too. If icon-indent-level is zero,
344 ;; use icon-brace-offset + icon-continued-statement-offset
346 ;; For open-braces not the first thing in a line,
347 ;; add in icon-brace-imaginary-offset.
348 (+ (if (and (bolp) (zerop icon-indent-level
))
350 icon-continued-statement-offset
)
352 ;; Move back over whitespace before the openbrace.
353 ;; If openbrace is not first nonwhite thing on the line,
354 ;; add the icon-brace-imaginary-offset.
355 (progn (skip-chars-backward " \t")
356 (if (bolp) 0 icon-brace-imaginary-offset
))
357 ;; Get initial indentation of the line we are on.
358 (current-indentation))))))))))
360 ;; List of words to check for as the last thing on a line.
361 ;; If cdr is t, next line is a continuation of the same statement,
362 ;; if cdr is nil, next line starts a new (possibly indented) statement.
364 (defconst icon-resword-alist
365 '(("by" . t
) ("case" . t
) ("create") ("do") ("dynamic" . t
) ("else")
366 ("every" . t
) ("if" . t
) ("global" . t
) ("initial" . t
)
367 ("link" . t
) ("local" . t
) ("of") ("record" . t
) ("repeat" . t
)
368 ("static" . t
) ("then") ("to" . t
) ("until" . t
) ("while" . t
)))
370 (defun icon-is-continuation-line ()
371 (let* ((ch (preceding-char))
372 (ch-syntax (char-syntax ch
)))
373 (if (eq ch-syntax ?w
)
374 (assoc (buffer-substring
375 (progn (forward-word -
1) (point))
376 (progn (forward-word 1) (point)))
378 (not (memq ch
'(0 ?\
; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
380 (defun icon-backward-to-noncomment (lim)
383 (skip-chars-backward " \t\n\f" lim
)
384 (setq opoint
(point))
386 (if (and (nth 5 (parse-partial-sexp (point) opoint
))
388 (search-backward "#")
391 (defun icon-backward-to-start-of-continued-exp (lim)
392 (if (memq (preceding-char) '(?\
) ?\
]))
395 (skip-chars-forward " \t")
397 ((<= (point) lim
) (goto-char (1+ lim
)))
398 ((not (icon-is-continued-line)) 0)
399 ((and (eq (char-syntax (following-char)) ?w
)
401 (assoc (buffer-substring (point)
402 (save-excursion (forward-word 1) (point)))
403 icon-resword-alist
))) 0)
404 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim
))))
406 (defun icon-is-continued-line ()
409 (icon-is-continuation-line)))
411 (defun icon-backward-to-start-of-if (&optional limit
)
412 "Move to the start of the last \"unbalanced\" if."
413 (or limit
(setq limit
(save-excursion (beginning-of-icon-defun) (point))))
415 (case-fold-search nil
))
416 (while (not (zerop if-level
))
418 (cond ((looking-at "else\\b")
419 (setq if-level
(1+ if-level
)))
420 ((looking-at "if\\b")
421 (setq if-level
(1- if-level
)))
424 (goto-char limit
))))))
426 (defun mark-icon-function ()
427 "Put mark at end of Icon function, point at beginning."
432 (beginning-of-line 0)
433 (beginning-of-icon-defun))
435 (defun beginning-of-icon-defun ()
436 "Go to the start of the enclosing procedure; return t if at top level."
438 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move
)
442 (defun end-of-icon-defun ()
444 (if (not (bobp)) (forward-char -
1))
445 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move
)
449 (defun indent-icon-exp ()
450 "Indent each line of the Icon grouping following point."
452 (let ((indent-stack (list nil
))
453 (contain-stack (list (point)))
454 (case-fold-search nil
)
455 restart outer-loop-done inner-loop-done state ostate
456 this-indent last-sexp
457 at-else at-brace at-do
463 (setq outer-loop-done nil
)
464 (while (and (not (eobp)) (not outer-loop-done
))
465 (setq last-depth next-depth
)
466 ;; Compute how depth changes over this line
467 ;; plus enough other lines to get to one that
468 ;; does not end inside a comment or string.
469 ;; Meanwhile, do appropriate indentation on comment lines.
470 (setq innerloop-done nil
)
471 (while (and (not innerloop-done
)
472 (not (and (eobp) (setq outer-loop-done t
))))
474 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
476 (setq next-depth
(car state
))
477 (if (and (car (cdr (cdr state
)))
478 (>= (car (cdr (cdr state
))) 0))
479 (setq last-sexp
(car (cdr (cdr state
)))))
480 (if (or (nth 4 ostate
))
482 (if (or (nth 3 state
))
484 (setq innerloop-done t
)))
485 (if (<= next-depth
0)
486 (setq outer-loop-done t
))
489 (if (/= last-depth next-depth
)
490 (setq last-sexp nil
))
491 (while (> last-depth next-depth
)
492 (setq indent-stack
(cdr indent-stack
)
493 contain-stack
(cdr contain-stack
)
494 last-depth
(1- last-depth
)))
495 (while (< last-depth next-depth
)
496 (setq indent-stack
(cons nil indent-stack
)
497 contain-stack
(cons nil contain-stack
)
498 last-depth
(1+ last-depth
)))
499 (if (null (car contain-stack
))
500 (setcar contain-stack
(or (car (cdr state
))
501 (save-excursion (forward-sexp -
1)
504 (skip-chars-forward " \t")
507 (if (and (car indent-stack
)
508 (>= (car indent-stack
) 0))
509 ;; Line is on an existing nesting level.
510 ;; Lines inside parens are handled specially.
511 (if (/= (char-after (car contain-stack
)) ?
{)
512 (setq this-indent
(car indent-stack
))
513 ;; Line is at statement level.
514 ;; Is it a new statement? Is it an else?
515 ;; Find last non-comment character before this line
517 (setq at-else
(looking-at "else\\W"))
518 (setq at-brace
(= (following-char) ?
{))
519 (icon-backward-to-noncomment opoint
)
520 (if (icon-is-continuation-line)
521 ;; Preceding line did not end in comma or semi;
522 ;; indent this line icon-continued-statement-offset
523 ;; more than previous.
525 (icon-backward-to-start-of-continued-exp (car contain-stack
))
527 (+ icon-continued-statement-offset
(current-column)
528 (if at-brace icon-continued-brace-offset
0))))
529 ;; Preceding line ended in comma or semi;
530 ;; use the standard indent for this level.
532 (progn (icon-backward-to-start-of-if opoint
)
533 (setq this-indent
(current-indentation)))
534 (setq this-indent
(car indent-stack
))))))
535 ;; Just started a new nesting level.
536 ;; Compute the standard indent for this level.
537 (let ((val (calculate-icon-indent
538 (if (car indent-stack
)
539 (- (car indent-stack
))))))
541 (setq this-indent val
))))
542 ;; Adjust line indentation according to its contents
543 (if (or (= (following-char) ?
})
544 (looking-at "end\\b"))
545 (setq this-indent
(- this-indent icon-indent-level
)))
546 (if (= (following-char) ?
{)
547 (setq this-indent
(+ this-indent icon-brace-offset
)))
548 ;; Put chosen indentation into effect.
549 (or (= (current-column) this-indent
)
551 (delete-region (point) (progn (beginning-of-line) (point)))
552 (indent-to this-indent
)))
553 ;; Indent any comment following the text.
554 (or (looking-at comment-start-skip
)
555 (if (re-search-forward comment-start-skip
(save-excursion (end-of-line) (point)) t
)
556 (progn (indent-for-comment) (beginning-of-line))))))))))
558 ;;; icon.el ends here