1 ;;; icon.el --- mode for editing Icon code
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
5 ;; Author: Chris Smith <convex!csmith>
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; A major mode for editing the Icon programming language.
30 ;; (autoload 'icon-mode "icon" nil t)
31 ;; (setq auto-mode-alist (cons '("\\.icn$" . icon-mode) auto-mode-alist))
32 ;; if not permanently installed in your emacs
36 (defvar icon-mode-abbrev-table nil
37 "Abbrev table in use in Icon-mode buffers.")
38 (define-abbrev-table 'icon-mode-abbrev-table
())
40 (defvar icon-mode-map
()
41 "Keymap used in Icon mode.")
44 (setq icon-mode-map
(make-sparse-keymap))
45 (define-key icon-mode-map
"{" 'electric-icon-brace
)
46 (define-key icon-mode-map
"}" 'electric-icon-brace
)
47 (define-key icon-mode-map
"\e\C-h" 'mark-icon-function
)
48 (define-key icon-mode-map
"\e\C-a" 'beginning-of-icon-defun
)
49 (define-key icon-mode-map
"\e\C-e" 'end-of-icon-defun
)
50 (define-key icon-mode-map
"\e\C-q" 'indent-icon-exp
)
51 (define-key icon-mode-map
"\177" 'backward-delete-char-untabify
)
52 (define-key icon-mode-map
"\t" 'icon-indent-command
))
54 (defvar icon-mode-syntax-table nil
55 "Syntax table in use in Icon-mode buffers.")
57 (if icon-mode-syntax-table
59 (setq icon-mode-syntax-table
(make-syntax-table))
60 (modify-syntax-entry ?
\\ "\\" icon-mode-syntax-table
)
61 (modify-syntax-entry ?
# "<" icon-mode-syntax-table
)
62 (modify-syntax-entry ?
\n ">" 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
)
74 (modify-syntax-entry ?
\' "\"" icon-mode-syntax-table
))
76 (defconst icon-indent-level
4
77 "*Indentation of Icon statements with respect to containing block.")
78 (defconst icon-brace-imaginary-offset
0
79 "*Imagined indentation of a Icon open brace that actually follows a statement.")
80 (defconst icon-brace-offset
0
81 "*Extra indentation for braces, compared with other text in same context.")
82 (defconst icon-continued-statement-offset
4
83 "*Extra indent for lines not starting new statements.")
84 (defconst icon-continued-brace-offset
0
85 "*Extra indent for substatements that start with open-braces.
86 This is in addition to icon-continued-statement-offset.")
88 (defconst icon-auto-newline nil
89 "*Non-nil means automatically newline before and after braces
90 inserted in Icon code.")
92 (defconst icon-tab-always-indent t
93 "*Non-nil means TAB in Icon mode should always reindent the current line,
94 regardless of where in the line point is when the TAB command is used.")
97 "Major mode for editing Icon code.
98 Expression and list commands understand all Icon brackets.
99 Tab indents for Icon code.
100 Paragraphs are separated by blank lines only.
101 Delete converts tabs to spaces as it moves back.
103 Variables controlling indentation style:
104 icon-tab-always-indent
105 Non-nil means TAB in Icon mode should always reindent the current line,
106 regardless of where in the line point is when the TAB command is used.
108 Non-nil means automatically newline before and after braces
109 inserted in Icon code.
111 Indentation of Icon statements within surrounding block.
112 The surrounding block's indentation is the indentation
113 of the line on which the open-brace appears.
114 icon-continued-statement-offset
115 Extra indentation given to a substatement, such as the
116 then-clause of an if or body of a while.
117 icon-continued-brace-offset
118 Extra indentation given to a brace that starts a substatement.
119 This is in addition to `icon-continued-statement-offset'.
121 Extra indentation for line if it starts with an open brace.
122 icon-brace-imaginary-offset
123 An open brace following other text is treated as if it were
124 this far to the right of the start of its line.
126 Turning on Icon mode calls the value of the variable `icon-mode-hook'
127 with no args, if that value is non-nil."
129 (kill-all-local-variables)
130 (use-local-map icon-mode-map
)
131 (setq major-mode
'icon-mode
)
132 (setq mode-name
"Icon")
133 (setq local-abbrev-table icon-mode-abbrev-table
)
134 (set-syntax-table icon-mode-syntax-table
)
135 (make-local-variable 'paragraph-start
)
136 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
137 (make-local-variable 'paragraph-separate
)
138 (setq paragraph-separate paragraph-start
)
139 (make-local-variable 'indent-line-function
)
140 (setq indent-line-function
'icon-indent-line
)
141 (make-local-variable 'require-final-newline
)
142 (setq require-final-newline t
)
143 (make-local-variable 'comment-start
)
144 (setq comment-start
"# ")
145 (make-local-variable 'comment-end
)
146 (setq comment-end
"")
147 (make-local-variable 'comment-column
)
148 (setq comment-column
32)
149 (make-local-variable 'comment-start-skip
)
150 (setq comment-start-skip
"# *")
151 (make-local-variable 'comment-indent-function
)
152 (setq comment-indent-function
'icon-comment-indent
)
153 (run-hooks 'icon-mode-hook
))
155 ;; This is used by indent-for-comment to decide how much to
156 ;; indent a comment in Icon code based on its context.
157 (defun icon-comment-indent ()
158 (if (looking-at "^#")
161 (skip-chars-backward " \t")
162 (max (if (bolp) 0 (1+ (current-column)))
165 (defun electric-icon-brace (arg)
166 "Insert character and correct line's indentation."
172 (skip-chars-backward " \t")
174 (if icon-auto-newline
175 (progn (icon-indent-line) (newline) t
)
178 (insert last-command-char
)
180 (if icon-auto-newline
183 ;; (newline) may have done auto-fill
184 (setq insertpos
(- (point) 2))
187 (if insertpos
(goto-char (1+ insertpos
)))
191 (goto-char insertpos
)
192 (self-insert-command (prefix-numeric-value arg
)))
193 (self-insert-command (prefix-numeric-value arg
)))))
195 (defun icon-indent-command (&optional whole-exp
)
197 "Indent current line as Icon code, or in some cases insert a tab character.
198 If `icon-tab-always-indent' is non-nil (the default), always indent current
199 line. Otherwise, indent the current line only if point is at the left margin
200 or in the line's indentation; otherwise insert a tab.
202 A numeric argument, regardless of its value, means indent rigidly all the
203 lines of the expression starting after point so that this line becomes
204 properly indented. The relative indentation among the lines of the
205 expression are preserved."
207 ;; If arg, always indent this line as Icon
208 ;; and shift remaining lines of expression the same amount.
209 (let ((shift-amt (icon-indent-line))
212 (if icon-tab-always-indent
221 (indent-code-rigidly beg end shift-amt
"#")))
222 (if (and (not icon-tab-always-indent
)
224 (skip-chars-backward " \t")
227 (icon-indent-line))))
229 (defun icon-indent-line ()
230 "Indent current line as Icon code.
231 Return the amount the indentation changed by."
232 (let ((indent (calculate-icon-indent nil
))
234 (case-fold-search nil
)
235 (pos (- (point-max) (point))))
238 (cond ((eq indent nil
)
239 (setq indent
(current-indentation)))
241 (setq indent
(calculate-icon-indent-within-comment)))
242 ((looking-at "[ \t]*#")
245 (skip-chars-forward " \t")
246 (if (listp indent
) (setq indent
(car indent
)))
247 (cond ((and (looking-at "else\\b")
248 (not (looking-at "else\\s_")))
249 (setq indent
(save-excursion
250 (icon-backward-to-start-of-if)
251 (current-indentation))))
252 ((or (= (following-char) ?
})
253 (looking-at "end\\b"))
254 (setq indent
(- indent icon-indent-level
)))
255 ((= (following-char) ?
{)
256 (setq indent
(+ indent icon-brace-offset
))))))
257 (skip-chars-forward " \t")
258 (setq shift-amt
(- indent
(current-column)))
259 (if (zerop shift-amt
)
260 (if (> (- (point-max) pos
) (point))
261 (goto-char (- (point-max) pos
)))
262 (delete-region beg
(point))
264 ;; If initial point was within line's indentation,
265 ;; position after the indentation. Else stay at same point in text.
266 (if (> (- (point-max) pos
) (point))
267 (goto-char (- (point-max) pos
))))
270 (defun calculate-icon-indent (&optional parse-start
)
271 "Return appropriate indentation for current line as Icon code.
272 In usual case returns an integer: the column to indent to.
273 Returns nil if line starts inside a string, t if in a comment."
276 (let ((indent-point (point))
277 (case-fold-search nil
)
282 (goto-char parse-start
)
283 (setq toplevel
(beginning-of-icon-defun)))
284 (while (< (point) indent-point
)
285 (setq parse-start
(point))
286 (setq state
(parse-partial-sexp (point) indent-point
0))
287 (setq containing-sexp
(car (cdr state
))))
288 (cond ((or (nth 3 state
) (nth 4 state
))
289 ;; return nil or t if should not change this line
291 ((and containing-sexp
292 (/= (char-after containing-sexp
) ?
{))
293 ;; line is expression, not statement:
294 ;; indent to just after the surrounding open.
295 (goto-char (1+ containing-sexp
))
299 ;; Outside any procedures.
300 (progn (icon-backward-to-noncomment (point-min))
301 (if (icon-is-continuation-line)
302 icon-continued-statement-offset
0))
304 (if (null containing-sexp
)
305 (progn (beginning-of-icon-defun)
306 (setq containing-sexp
(point))))
307 (goto-char indent-point
)
308 ;; Is it a continuation or a new statement?
309 ;; Find previous non-comment character.
310 (icon-backward-to-noncomment containing-sexp
)
311 ;; Now we get the answer.
312 (if (icon-is-continuation-line)
313 ;; This line is continuation of preceding line's statement;
314 ;; indent icon-continued-statement-offset more than the
315 ;; first line of the statement.
317 (icon-backward-to-start-of-continued-exp containing-sexp
)
318 (+ icon-continued-statement-offset
(current-column)
319 (if (save-excursion (goto-char indent-point
)
320 (skip-chars-forward " \t")
321 (eq (following-char) ?
{))
322 icon-continued-brace-offset
0)))
323 ;; This line starts a new statement.
324 ;; Position following last unclosed open.
325 (goto-char containing-sexp
)
326 ;; Is line first statement after an open-brace?
328 ;; If no, find that first statement and indent like it.
330 (if (looking-at "procedure\\s ")
333 (while (progn (skip-chars-forward " \t\n")
335 ;; Skip over comments following openbrace.
337 ;; The first following code counts
338 ;; if it is before the line we want to indent.
339 (and (< (point) indent-point
)
341 ;; If no previous statement,
342 ;; indent it relative to line brace is on.
343 ;; For open brace in column zero, don't let statement
344 ;; start there too. If icon-indent-level is zero,
345 ;; use icon-brace-offset + icon-continued-statement-offset
347 ;; For open-braces not the first thing in a line,
348 ;; add in icon-brace-imaginary-offset.
349 (+ (if (and (bolp) (zerop icon-indent-level
))
351 icon-continued-statement-offset
)
353 ;; Move back over whitespace before the openbrace.
354 ;; If openbrace is not first nonwhite thing on the line,
355 ;; add the icon-brace-imaginary-offset.
356 (progn (skip-chars-backward " \t")
357 (if (bolp) 0 icon-brace-imaginary-offset
))
358 ;; Get initial indentation of the line we are on.
359 (current-indentation))))))))))
361 ;; List of words to check for as the last thing on a line.
362 ;; If cdr is t, next line is a continuation of the same statement,
363 ;; if cdr is nil, next line starts a new (possibly indented) statement.
365 (defconst icon-resword-alist
366 '(("by" . t
) ("case" . t
) ("create") ("do") ("dynamic" . t
) ("else")
367 ("every" . t
) ("if" . t
) ("global" . t
) ("initial" . t
)
368 ("link" . t
) ("local" . t
) ("of") ("record" . t
) ("repeat" . t
)
369 ("static" . t
) ("then") ("to" . t
) ("until" . t
) ("while" . t
)))
371 (defun icon-is-continuation-line ()
372 (let* ((ch (preceding-char))
373 (ch-syntax (char-syntax ch
)))
374 (if (eq ch-syntax ?w
)
375 (assoc (buffer-substring
376 (progn (forward-word -
1) (point))
377 (progn (forward-word 1) (point)))
379 (not (memq ch
'(0 ?\
; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
381 (defun icon-backward-to-noncomment (lim)
384 (skip-chars-backward " \t\n\f" lim
)
385 (setq opoint
(point))
387 (if (and (nth 5 (parse-partial-sexp (point) opoint
))
389 (search-backward "#")
392 (defun icon-backward-to-start-of-continued-exp (lim)
393 (if (memq (preceding-char) '(?\
) ?\
]))
396 (skip-chars-forward " \t")
398 ((<= (point) lim
) (goto-char (1+ lim
)))
399 ((not (icon-is-continued-line)) 0)
400 ((and (eq (char-syntax (following-char)) ?w
)
402 (assoc (buffer-substring (point)
403 (save-excursion (forward-word 1) (point)))
404 icon-resword-alist
))) 0)
405 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim
))))
407 (defun icon-is-continued-line ()
410 (icon-is-continuation-line)))
412 (defun icon-backward-to-start-of-if (&optional limit
)
413 "Move to the start of the last \"unbalanced\" if."
414 (or limit
(setq limit
(save-excursion (beginning-of-icon-defun) (point))))
416 (case-fold-search nil
))
417 (while (not (zerop if-level
))
419 (cond ((looking-at "else\\b")
420 (setq if-level
(1+ if-level
)))
421 ((looking-at "if\\b")
422 (setq if-level
(1- if-level
)))
425 (goto-char limit
))))))
427 (defun mark-icon-function ()
428 "Put mark at end of Icon function, point at beginning."
433 (beginning-of-line 0)
434 (beginning-of-icon-defun))
436 (defun beginning-of-icon-defun ()
437 "Go to the start of the enclosing procedure; return t if at top level."
439 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move
)
443 (defun end-of-icon-defun ()
445 (if (not (bobp)) (forward-char -
1))
446 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move
)
450 (defun indent-icon-exp ()
451 "Indent each line of the Icon grouping following point."
453 (let ((indent-stack (list nil
))
454 (contain-stack (list (point)))
455 (case-fold-search nil
)
456 restart outer-loop-done inner-loop-done state ostate
457 this-indent last-sexp
458 at-else at-brace at-do
464 (setq outer-loop-done nil
)
465 (while (and (not (eobp)) (not outer-loop-done
))
466 (setq last-depth next-depth
)
467 ;; Compute how depth changes over this line
468 ;; plus enough other lines to get to one that
469 ;; does not end inside a comment or string.
470 ;; Meanwhile, do appropriate indentation on comment lines.
471 (setq innerloop-done nil
)
472 (while (and (not innerloop-done
)
473 (not (and (eobp) (setq outer-loop-done t
))))
475 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
477 (setq next-depth
(car state
))
478 (if (and (car (cdr (cdr state
)))
479 (>= (car (cdr (cdr state
))) 0))
480 (setq last-sexp
(car (cdr (cdr state
)))))
481 (if (or (nth 4 ostate
))
483 (if (or (nth 3 state
))
485 (setq innerloop-done t
)))
486 (if (<= next-depth
0)
487 (setq outer-loop-done t
))
490 (if (/= last-depth next-depth
)
491 (setq last-sexp nil
))
492 (while (> last-depth next-depth
)
493 (setq indent-stack
(cdr indent-stack
)
494 contain-stack
(cdr contain-stack
)
495 last-depth
(1- last-depth
)))
496 (while (< last-depth next-depth
)
497 (setq indent-stack
(cons nil indent-stack
)
498 contain-stack
(cons nil contain-stack
)
499 last-depth
(1+ last-depth
)))
500 (if (null (car contain-stack
))
501 (setcar contain-stack
(or (car (cdr state
))
502 (save-excursion (forward-sexp -
1)
505 (skip-chars-forward " \t")
508 (if (and (car indent-stack
)
509 (>= (car indent-stack
) 0))
510 ;; Line is on an existing nesting level.
511 ;; Lines inside parens are handled specially.
512 (if (/= (char-after (car contain-stack
)) ?
{)
513 (setq this-indent
(car indent-stack
))
514 ;; Line is at statement level.
515 ;; Is it a new statement? Is it an else?
516 ;; Find last non-comment character before this line
518 (setq at-else
(looking-at "else\\W"))
519 (setq at-brace
(= (following-char) ?
{))
520 (icon-backward-to-noncomment opoint
)
521 (if (icon-is-continuation-line)
522 ;; Preceding line did not end in comma or semi;
523 ;; indent this line icon-continued-statement-offset
524 ;; more than previous.
526 (icon-backward-to-start-of-continued-exp (car contain-stack
))
528 (+ icon-continued-statement-offset
(current-column)
529 (if at-brace icon-continued-brace-offset
0))))
530 ;; Preceding line ended in comma or semi;
531 ;; use the standard indent for this level.
533 (progn (icon-backward-to-start-of-if opoint
)
534 (setq this-indent
(current-indentation)))
535 (setq this-indent
(car indent-stack
))))))
536 ;; Just started a new nesting level.
537 ;; Compute the standard indent for this level.
538 (let ((val (calculate-icon-indent
539 (if (car indent-stack
)
540 (- (car indent-stack
))))))
542 (setq this-indent val
))))
543 ;; Adjust line indentation according to its contents
544 (if (or (= (following-char) ?
})
545 (looking-at "end\\b"))
546 (setq this-indent
(- this-indent icon-indent-level
)))
547 (if (= (following-char) ?
{)
548 (setq this-indent
(+ this-indent icon-brace-offset
)))
549 ;; Put chosen indentation into effect.
550 (or (= (current-column) this-indent
)
552 (delete-region (point) (progn (beginning-of-line) (point)))
553 (indent-to this-indent
)))
554 ;; Indent any comment following the text.
555 (or (looking-at comment-start-skip
)
556 (if (re-search-forward comment-start-skip
(save-excursion (end-of-line) (point)) t
)
557 (progn (indent-for-comment) (beginning-of-line))))))))))
559 ;;; icon.el ends here