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.
31 (defvar icon-mode-abbrev-table nil
32 "Abbrev table in use in Icon-mode buffers.")
33 (define-abbrev-table 'icon-mode-abbrev-table
())
35 (defvar icon-mode-map
()
36 "Keymap used in Icon mode.")
39 (setq icon-mode-map
(make-sparse-keymap))
40 (define-key icon-mode-map
"{" 'electric-icon-brace
)
41 (define-key icon-mode-map
"}" 'electric-icon-brace
)
42 (define-key icon-mode-map
"\e\C-h" 'mark-icon-function
)
43 (define-key icon-mode-map
"\e\C-a" 'beginning-of-icon-defun
)
44 (define-key icon-mode-map
"\e\C-e" 'end-of-icon-defun
)
45 (define-key icon-mode-map
"\e\C-q" 'indent-icon-exp
)
46 (define-key icon-mode-map
"\177" 'backward-delete-char-untabify
)
47 (define-key icon-mode-map
"\t" 'icon-indent-command
))
49 (defvar icon-mode-syntax-table nil
50 "Syntax table in use in Icon-mode buffers.")
52 (if icon-mode-syntax-table
54 (setq icon-mode-syntax-table
(make-syntax-table))
55 (modify-syntax-entry ?
\\ "\\" icon-mode-syntax-table
)
56 (modify-syntax-entry ?
# "<" icon-mode-syntax-table
)
57 (modify-syntax-entry ?
\n ">" icon-mode-syntax-table
)
58 (modify-syntax-entry ?$
"." icon-mode-syntax-table
)
59 (modify-syntax-entry ?
/ "." icon-mode-syntax-table
)
60 (modify-syntax-entry ?
* "." icon-mode-syntax-table
)
61 (modify-syntax-entry ?
+ "." 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
))
71 (defconst icon-indent-level
4
72 "*Indentation of Icon statements with respect to containing block.")
73 (defconst icon-brace-imaginary-offset
0
74 "*Imagined indentation of a Icon open brace that actually follows a statement.")
75 (defconst icon-brace-offset
0
76 "*Extra indentation for braces, compared with other text in same context.")
77 (defconst icon-continued-statement-offset
4
78 "*Extra indent for lines not starting new statements.")
79 (defconst icon-continued-brace-offset
0
80 "*Extra indent for substatements that start with open-braces.
81 This is in addition to icon-continued-statement-offset.")
83 (defconst icon-auto-newline nil
84 "*Non-nil means automatically newline before and after braces
85 inserted in Icon code.")
87 (defconst icon-tab-always-indent t
88 "*Non-nil means TAB in Icon mode should always reindent the current line,
89 regardless of where in the line point is when the TAB command is used.")
93 "Major mode for editing Icon code.
94 Expression and list commands understand all Icon brackets.
95 Tab indents for Icon code.
96 Paragraphs are separated by blank lines only.
97 Delete converts tabs to spaces as it moves back.
99 Variables controlling indentation style:
100 icon-tab-always-indent
101 Non-nil means TAB in Icon mode should always reindent the current line,
102 regardless of where in the line point is when the TAB command is used.
104 Non-nil means automatically newline before and after braces
105 inserted in Icon code.
107 Indentation of Icon statements within surrounding block.
108 The surrounding block's indentation is the indentation
109 of the line on which the open-brace appears.
110 icon-continued-statement-offset
111 Extra indentation given to a substatement, such as the
112 then-clause of an if or body of a while.
113 icon-continued-brace-offset
114 Extra indentation given to a brace that starts a substatement.
115 This is in addition to `icon-continued-statement-offset'.
117 Extra indentation for line if it starts with an open brace.
118 icon-brace-imaginary-offset
119 An open brace following other text is treated as if it were
120 this far to the right of the start of its line.
122 Turning on Icon mode calls the value of the variable `icon-mode-hook'
123 with no args, if that value is non-nil."
125 (kill-all-local-variables)
126 (use-local-map icon-mode-map
)
127 (setq major-mode
'icon-mode
)
128 (setq mode-name
"Icon")
129 (setq local-abbrev-table icon-mode-abbrev-table
)
130 (set-syntax-table icon-mode-syntax-table
)
131 (make-local-variable 'paragraph-start
)
132 (setq paragraph-start
(concat "^$\\|" page-delimiter
))
133 (make-local-variable 'paragraph-separate
)
134 (setq paragraph-separate paragraph-start
)
135 (make-local-variable 'indent-line-function
)
136 (setq indent-line-function
'icon-indent-line
)
137 (make-local-variable 'require-final-newline
)
138 (setq require-final-newline t
)
139 (make-local-variable 'comment-start
)
140 (setq comment-start
"# ")
141 (make-local-variable 'comment-end
)
142 (setq comment-end
"")
143 (make-local-variable 'comment-column
)
144 (setq comment-column
32)
145 (make-local-variable 'comment-start-skip
)
146 (setq comment-start-skip
"# *")
147 (make-local-variable 'comment-indent-function
)
148 (setq comment-indent-function
'icon-comment-indent
)
149 (run-hooks 'icon-mode-hook
))
151 ;; This is used by indent-for-comment to decide how much to
152 ;; indent a comment in Icon code based on its context.
153 (defun icon-comment-indent ()
154 (if (looking-at "^#")
157 (skip-chars-backward " \t")
158 (max (if (bolp) 0 (1+ (current-column)))
161 (defun electric-icon-brace (arg)
162 "Insert character and correct line's indentation."
168 (skip-chars-backward " \t")
170 (if icon-auto-newline
171 (progn (icon-indent-line) (newline) t
)
174 (insert last-command-char
)
176 (if icon-auto-newline
179 ;; (newline) may have done auto-fill
180 (setq insertpos
(- (point) 2))
183 (if insertpos
(goto-char (1+ insertpos
)))
187 (goto-char insertpos
)
188 (self-insert-command (prefix-numeric-value arg
)))
189 (self-insert-command (prefix-numeric-value arg
)))))
191 (defun icon-indent-command (&optional whole-exp
)
193 "Indent current line as Icon code, or in some cases insert a tab character.
194 If `icon-tab-always-indent' is non-nil (the default), always indent current
195 line. Otherwise, indent the current line only if point is at the left margin
196 or in the line's indentation; otherwise insert a tab.
198 A numeric argument, regardless of its value, means indent rigidly all the
199 lines of the expression starting after point so that this line becomes
200 properly indented. The relative indentation among the lines of the
201 expression are preserved."
203 ;; If arg, always indent this line as Icon
204 ;; and shift remaining lines of expression the same amount.
205 (let ((shift-amt (icon-indent-line))
208 (if icon-tab-always-indent
217 (indent-code-rigidly beg end shift-amt
"#")))
218 (if (and (not icon-tab-always-indent
)
220 (skip-chars-backward " \t")
223 (icon-indent-line))))
225 (defun icon-indent-line ()
226 "Indent current line as Icon code.
227 Return the amount the indentation changed by."
228 (let ((indent (calculate-icon-indent nil
))
230 (case-fold-search nil
)
231 (pos (- (point-max) (point))))
234 (cond ((eq indent nil
)
235 (setq indent
(current-indentation)))
237 (setq indent
(calculate-icon-indent-within-comment)))
238 ((looking-at "[ \t]*#")
241 (skip-chars-forward " \t")
242 (if (listp indent
) (setq indent
(car indent
)))
243 (cond ((and (looking-at "else\\b")
244 (not (looking-at "else\\s_")))
245 (setq indent
(save-excursion
246 (icon-backward-to-start-of-if)
247 (current-indentation))))
248 ((or (= (following-char) ?
})
249 (looking-at "end\\b"))
250 (setq indent
(- indent icon-indent-level
)))
251 ((= (following-char) ?
{)
252 (setq indent
(+ indent icon-brace-offset
))))))
253 (skip-chars-forward " \t")
254 (setq shift-amt
(- indent
(current-column)))
255 (if (zerop shift-amt
)
256 (if (> (- (point-max) pos
) (point))
257 (goto-char (- (point-max) pos
)))
258 (delete-region beg
(point))
260 ;; If initial point was within line's indentation,
261 ;; position after the indentation. Else stay at same point in text.
262 (if (> (- (point-max) pos
) (point))
263 (goto-char (- (point-max) pos
))))
266 (defun calculate-icon-indent (&optional parse-start
)
267 "Return appropriate indentation for current line as Icon code.
268 In usual case returns an integer: the column to indent to.
269 Returns nil if line starts inside a string, t if in a comment."
272 (let ((indent-point (point))
273 (case-fold-search nil
)
278 (goto-char parse-start
)
279 (setq toplevel
(beginning-of-icon-defun)))
280 (while (< (point) indent-point
)
281 (setq parse-start
(point))
282 (setq state
(parse-partial-sexp (point) indent-point
0))
283 (setq containing-sexp
(car (cdr state
))))
284 (cond ((or (nth 3 state
) (nth 4 state
))
285 ;; return nil or t if should not change this line
287 ((and containing-sexp
288 (/= (char-after containing-sexp
) ?
{))
289 ;; line is expression, not statement:
290 ;; indent to just after the surrounding open.
291 (goto-char (1+ containing-sexp
))
295 ;; Outside any procedures.
296 (progn (icon-backward-to-noncomment (point-min))
297 (if (icon-is-continuation-line)
298 icon-continued-statement-offset
0))
300 (if (null containing-sexp
)
301 (progn (beginning-of-icon-defun)
302 (setq containing-sexp
(point))))
303 (goto-char indent-point
)
304 ;; Is it a continuation or a new statement?
305 ;; Find previous non-comment character.
306 (icon-backward-to-noncomment containing-sexp
)
307 ;; Now we get the answer.
308 (if (icon-is-continuation-line)
309 ;; This line is continuation of preceding line's statement;
310 ;; indent icon-continued-statement-offset more than the
311 ;; first line of the statement.
313 (icon-backward-to-start-of-continued-exp containing-sexp
)
314 (+ icon-continued-statement-offset
(current-column)
315 (if (save-excursion (goto-char indent-point
)
316 (skip-chars-forward " \t")
317 (eq (following-char) ?
{))
318 icon-continued-brace-offset
0)))
319 ;; This line starts a new statement.
320 ;; Position following last unclosed open.
321 (goto-char containing-sexp
)
322 ;; Is line first statement after an open-brace?
324 ;; If no, find that first statement and indent like it.
326 (if (looking-at "procedure\\s ")
329 (while (progn (skip-chars-forward " \t\n")
331 ;; Skip over comments following openbrace.
333 ;; The first following code counts
334 ;; if it is before the line we want to indent.
335 (and (< (point) indent-point
)
337 ;; If no previous statement,
338 ;; indent it relative to line brace is on.
339 ;; For open brace in column zero, don't let statement
340 ;; start there too. If icon-indent-level is zero,
341 ;; use icon-brace-offset + icon-continued-statement-offset
343 ;; For open-braces not the first thing in a line,
344 ;; add in icon-brace-imaginary-offset.
345 (+ (if (and (bolp) (zerop icon-indent-level
))
347 icon-continued-statement-offset
)
349 ;; Move back over whitespace before the openbrace.
350 ;; If openbrace is not first nonwhite thing on the line,
351 ;; add the icon-brace-imaginary-offset.
352 (progn (skip-chars-backward " \t")
353 (if (bolp) 0 icon-brace-imaginary-offset
))
354 ;; Get initial indentation of the line we are on.
355 (current-indentation))))))))))
357 ;; List of words to check for as the last thing on a line.
358 ;; If cdr is t, next line is a continuation of the same statement,
359 ;; if cdr is nil, next line starts a new (possibly indented) statement.
361 (defconst icon-resword-alist
362 '(("by" . t
) ("case" . t
) ("create") ("do") ("dynamic" . t
) ("else")
363 ("every" . t
) ("if" . t
) ("global" . t
) ("initial" . t
)
364 ("link" . t
) ("local" . t
) ("of") ("record" . t
) ("repeat" . t
)
365 ("static" . t
) ("then") ("to" . t
) ("until" . t
) ("while" . t
)))
367 (defun icon-is-continuation-line ()
368 (let* ((ch (preceding-char))
369 (ch-syntax (char-syntax ch
)))
370 (if (eq ch-syntax ?w
)
371 (assoc (buffer-substring
372 (progn (forward-word -
1) (point))
373 (progn (forward-word 1) (point)))
375 (not (memq ch
'(0 ?\
; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
377 (defun icon-backward-to-noncomment (lim)
380 (skip-chars-backward " \t\n\f" lim
)
381 (setq opoint
(point))
383 (if (and (nth 5 (parse-partial-sexp (point) opoint
))
385 (search-backward "#")
388 (defun icon-backward-to-start-of-continued-exp (lim)
389 (if (memq (preceding-char) '(?\
) ?\
]))
392 (skip-chars-forward " \t")
394 ((<= (point) lim
) (goto-char (1+ lim
)))
395 ((not (icon-is-continued-line)) 0)
396 ((and (eq (char-syntax (following-char)) ?w
)
398 (assoc (buffer-substring (point)
399 (save-excursion (forward-word 1) (point)))
400 icon-resword-alist
))) 0)
401 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim
))))
403 (defun icon-is-continued-line ()
406 (icon-is-continuation-line)))
408 (defun icon-backward-to-start-of-if (&optional limit
)
409 "Move to the start of the last \"unbalanced\" if."
410 (or limit
(setq limit
(save-excursion (beginning-of-icon-defun) (point))))
412 (case-fold-search nil
))
413 (while (not (zerop if-level
))
415 (cond ((looking-at "else\\b")
416 (setq if-level
(1+ if-level
)))
417 ((looking-at "if\\b")
418 (setq if-level
(1- if-level
)))
421 (goto-char limit
))))))
423 (defun mark-icon-function ()
424 "Put mark at end of Icon function, point at beginning."
429 (beginning-of-line 0)
430 (beginning-of-icon-defun))
432 (defun beginning-of-icon-defun ()
433 "Go to the start of the enclosing procedure; return t if at top level."
435 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move
)
439 (defun end-of-icon-defun ()
441 (if (not (bobp)) (forward-char -
1))
442 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move
)
446 (defun indent-icon-exp ()
447 "Indent each line of the Icon grouping following point."
449 (let ((indent-stack (list nil
))
450 (contain-stack (list (point)))
451 (case-fold-search nil
)
452 restart outer-loop-done inner-loop-done state ostate
453 this-indent last-sexp
454 at-else at-brace at-do
460 (setq outer-loop-done nil
)
461 (while (and (not (eobp)) (not outer-loop-done
))
462 (setq last-depth next-depth
)
463 ;; Compute how depth changes over this line
464 ;; plus enough other lines to get to one that
465 ;; does not end inside a comment or string.
466 ;; Meanwhile, do appropriate indentation on comment lines.
467 (setq innerloop-done nil
)
468 (while (and (not innerloop-done
)
469 (not (and (eobp) (setq outer-loop-done t
))))
471 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
473 (setq next-depth
(car state
))
474 (if (and (car (cdr (cdr state
)))
475 (>= (car (cdr (cdr state
))) 0))
476 (setq last-sexp
(car (cdr (cdr state
)))))
477 (if (or (nth 4 ostate
))
479 (if (or (nth 3 state
))
481 (setq innerloop-done t
)))
482 (if (<= next-depth
0)
483 (setq outer-loop-done t
))
486 (if (/= last-depth next-depth
)
487 (setq last-sexp nil
))
488 (while (> last-depth next-depth
)
489 (setq indent-stack
(cdr indent-stack
)
490 contain-stack
(cdr contain-stack
)
491 last-depth
(1- last-depth
)))
492 (while (< last-depth next-depth
)
493 (setq indent-stack
(cons nil indent-stack
)
494 contain-stack
(cons nil contain-stack
)
495 last-depth
(1+ last-depth
)))
496 (if (null (car contain-stack
))
497 (setcar contain-stack
(or (car (cdr state
))
498 (save-excursion (forward-sexp -
1)
501 (skip-chars-forward " \t")
504 (if (and (car indent-stack
)
505 (>= (car indent-stack
) 0))
506 ;; Line is on an existing nesting level.
507 ;; Lines inside parens are handled specially.
508 (if (/= (char-after (car contain-stack
)) ?
{)
509 (setq this-indent
(car indent-stack
))
510 ;; Line is at statement level.
511 ;; Is it a new statement? Is it an else?
512 ;; Find last non-comment character before this line
514 (setq at-else
(looking-at "else\\W"))
515 (setq at-brace
(= (following-char) ?
{))
516 (icon-backward-to-noncomment opoint
)
517 (if (icon-is-continuation-line)
518 ;; Preceding line did not end in comma or semi;
519 ;; indent this line icon-continued-statement-offset
520 ;; more than previous.
522 (icon-backward-to-start-of-continued-exp (car contain-stack
))
524 (+ icon-continued-statement-offset
(current-column)
525 (if at-brace icon-continued-brace-offset
0))))
526 ;; Preceding line ended in comma or semi;
527 ;; use the standard indent for this level.
529 (progn (icon-backward-to-start-of-if opoint
)
530 (setq this-indent
(current-indentation)))
531 (setq this-indent
(car indent-stack
))))))
532 ;; Just started a new nesting level.
533 ;; Compute the standard indent for this level.
534 (let ((val (calculate-icon-indent
535 (if (car indent-stack
)
536 (- (car indent-stack
))))))
538 (setq this-indent val
))))
539 ;; Adjust line indentation according to its contents
540 (if (or (= (following-char) ?
})
541 (looking-at "end\\b"))
542 (setq this-indent
(- this-indent icon-indent-level
)))
543 (if (= (following-char) ?
{)
544 (setq this-indent
(+ this-indent icon-brace-offset
)))
545 ;; Put chosen indentation into effect.
546 (or (= (current-column) this-indent
)
548 (delete-region (point) (progn (beginning-of-line) (point)))
549 (indent-to this-indent
)))
550 ;; Indent any comment following the text.
551 (or (looking-at comment-start-skip
)
552 (if (re-search-forward comment-start-skip
(save-excursion (end-of-line) (point)) t
)
553 (progn (indent-for-comment) (beginning-of-line))))))))))
555 ;;; icon.el ends here