1 ;;; icon.el --- mode for editing Icon code
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
5 ;; Author: Chris Smith <csmith@convex.com>
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; A major mode for editing the Icon programming language.
32 (defvar icon-mode-abbrev-table nil
33 "Abbrev table in use in Icon-mode buffers.")
34 (define-abbrev-table 'icon-mode-abbrev-table ())
36 (defvar icon-mode-map ()
37 "Keymap used in Icon mode.")
40 (setq icon-mode-map (make-sparse-keymap))
41 (define-key icon-mode-map "{" 'electric-icon-brace)
42 (define-key icon-mode-map "}" 'electric-icon-brace)
43 (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
44 (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
45 (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
46 (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
47 (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
48 (define-key icon-mode-map "\t" 'icon-indent-command))
50 (defvar icon-mode-syntax-table nil
51 "Syntax table in use in Icon-mode buffers.")
53 (if icon-mode-syntax-table
55 (setq icon-mode-syntax-table (make-syntax-table))
56 (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
57 (modify-syntax-entry ?# "<" icon-mode-syntax-table)
58 (modify-syntax-entry ?\n ">" 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)
70 (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
72 (defvar icon-indent-level 4
73 "*Indentation of Icon statements with respect to containing block.")
74 (defvar icon-brace-imaginary-offset 0
75 "*Imagined indentation of a Icon open brace that actually follows a statement.")
76 (defvar icon-brace-offset 0
77 "*Extra indentation for braces, compared with other text in same context.")
78 (defvar icon-continued-statement-offset 4
79 "*Extra indent for lines not starting new statements.")
80 (defvar icon-continued-brace-offset 0
81 "*Extra indent for substatements that start with open-braces.
82 This is in addition to icon-continued-statement-offset.")
84 (defvar icon-auto-newline nil
85 "*Non-nil means automatically newline before and after braces
86 inserted in Icon code.")
88 (defvar icon-tab-always-indent t
89 "*Non-nil means TAB in Icon mode should always reindent the current line,
90 regardless of where in the line point is when the TAB command is used.")
94 "Major mode for editing Icon code.
95 Expression and list commands understand all Icon brackets.
96 Tab indents for Icon code.
97 Paragraphs are separated by blank lines only.
98 Delete converts tabs to spaces as it moves back.
100 Variables controlling indentation style:
101 icon-tab-always-indent
102 Non-nil means TAB in Icon mode should always reindent the current line,
103 regardless of where in the line point is when the TAB command is used.
105 Non-nil means automatically newline before and after braces
106 inserted in Icon code.
108 Indentation of Icon statements within surrounding block.
109 The surrounding block's indentation is the indentation
110 of the line on which the open-brace appears.
111 icon-continued-statement-offset
112 Extra indentation given to a substatement, such as the
113 then-clause of an if or body of a while.
114 icon-continued-brace-offset
115 Extra indentation given to a brace that starts a substatement.
116 This is in addition to `icon-continued-statement-offset'.
118 Extra indentation for line if it starts with an open brace.
119 icon-brace-imaginary-offset
120 An open brace following other text is treated as if it were
121 this far to the right of the start of its line.
123 Turning on Icon mode calls the value of the variable `icon-mode-hook'
124 with no args, if that value is non-nil."
126 (kill-all-local-variables)
127 (use-local-map icon-mode-map)
128 (setq major-mode 'icon-mode)
129 (setq mode-name "Icon")
130 (setq local-abbrev-table icon-mode-abbrev-table)
131 (set-syntax-table icon-mode-syntax-table)
132 (make-local-variable 'paragraph-start)
133 (setq paragraph-start (concat "$\\|" page-delimiter))
134 (make-local-variable 'paragraph-separate)
135 (setq paragraph-separate paragraph-start)
136 (make-local-variable 'indent-line-function)
137 (setq indent-line-function 'icon-indent-line)
138 (make-local-variable 'require-final-newline)
139 (setq require-final-newline t)
140 (make-local-variable 'comment-start)
141 (setq comment-start "# ")
142 (make-local-variable 'comment-end)
143 (setq comment-end "")
144 (make-local-variable 'comment-column)
145 (setq comment-column 32)
146 (make-local-variable 'comment-start-skip)
147 (setq comment-start-skip "# *")
148 (make-local-variable 'comment-indent-function)
149 (setq comment-indent-function 'icon-comment-indent)
150 (run-hooks 'icon-mode-hook))
152 ;; This is used by indent-for-comment to decide how much to
153 ;; indent a comment in Icon code based on its context.
154 (defun icon-comment-indent ()
155 (if (looking-at "^#")
158 (skip-chars-backward " \t")
159 (max (if (bolp) 0 (1+ (current-column)))
162 (defun electric-icon-brace (arg)
163 "Insert character and correct line's indentation."
169 (skip-chars-backward " \t")
171 (if icon-auto-newline
172 (progn (icon-indent-line) (newline) t)
175 (insert last-command-char)
177 (if icon-auto-newline
180 ;; (newline) may have done auto-fill
181 (setq insertpos (- (point) 2))
184 (if insertpos (goto-char (1+ insertpos)))
188 (goto-char insertpos)
189 (self-insert-command (prefix-numeric-value arg)))
190 (self-insert-command (prefix-numeric-value arg)))))
192 (defun icon-indent-command (&optional whole-exp)
194 "Indent current line as Icon code, or in some cases insert a tab character.
195 If `icon-tab-always-indent' is non-nil (the default), always indent current
196 line. Otherwise, indent the current line only if point is at the left margin
197 or in the line's indentation; otherwise insert a tab.
199 A numeric argument, regardless of its value, means indent rigidly all the
200 lines of the expression starting after point so that this line becomes
201 properly indented. The relative indentation among the lines of the
202 expression are preserved."
204 ;; If arg, always indent this line as Icon
205 ;; and shift remaining lines of expression the same amount.
206 (let ((shift-amt (icon-indent-line))
209 (if icon-tab-always-indent
218 (indent-code-rigidly beg end shift-amt "#")))
219 (if (and (not icon-tab-always-indent)
221 (skip-chars-backward " \t")
224 (icon-indent-line))))
226 (defun icon-indent-line ()
227 "Indent current line as Icon code.
228 Return the amount the indentation changed by."
229 (let ((indent (calculate-icon-indent nil))
231 (case-fold-search nil)
232 (pos (- (point-max) (point))))
235 (cond ((eq indent nil)
236 (setq indent (current-indentation)))
238 (setq indent (calculate-icon-indent-within-comment)))
239 ((looking-at "[ \t]*#")
242 (skip-chars-forward " \t")
243 (if (listp indent) (setq indent (car indent)))
244 (cond ((and (looking-at "else\\b")
245 (not (looking-at "else\\s_")))
246 (setq indent (save-excursion
247 (icon-backward-to-start-of-if)
248 (current-indentation))))
249 ((or (= (following-char) ?})
250 (looking-at "end\\b"))
251 (setq indent (- indent icon-indent-level)))
252 ((= (following-char) ?{)
253 (setq indent (+ indent icon-brace-offset))))))
254 (skip-chars-forward " \t")
255 (setq shift-amt (- indent (current-column)))
256 (if (zerop shift-amt)
257 (if (> (- (point-max) pos) (point))
258 (goto-char (- (point-max) pos)))
259 (delete-region beg (point))
261 ;; If initial point was within line's indentation,
262 ;; position after the indentation. Else stay at same point in text.
263 (if (> (- (point-max) pos) (point))
264 (goto-char (- (point-max) pos))))
267 (defun calculate-icon-indent (&optional parse-start)
268 "Return appropriate indentation for current line as Icon code.
269 In usual case returns an integer: the column to indent to.
270 Returns nil if line starts inside a string, t if in a comment."
273 (let ((indent-point (point))
274 (case-fold-search nil)
279 (goto-char parse-start)
280 (setq toplevel (beginning-of-icon-defun)))
281 (while (< (point) indent-point)
282 (setq parse-start (point))
283 (setq state (parse-partial-sexp (point) indent-point 0))
284 (setq containing-sexp (car (cdr state))))
285 (cond ((or (nth 3 state) (nth 4 state))
286 ;; return nil or t if should not change this line
288 ((and containing-sexp
289 (/= (char-after containing-sexp) ?{))
290 ;; line is expression, not statement:
291 ;; indent to just after the surrounding open.
292 (goto-char (1+ containing-sexp))
296 ;; Outside any procedures.
297 (progn (icon-backward-to-noncomment (point-min))
298 (if (icon-is-continuation-line)
299 icon-continued-statement-offset 0))
301 (if (null containing-sexp)
302 (progn (beginning-of-icon-defun)
303 (setq containing-sexp (point))))
304 (goto-char indent-point)
305 ;; Is it a continuation or a new statement?
306 ;; Find previous non-comment character.
307 (icon-backward-to-noncomment containing-sexp)
308 ;; Now we get the answer.
309 (if (icon-is-continuation-line)
310 ;; This line is continuation of preceding line's statement;
311 ;; indent icon-continued-statement-offset more than the
312 ;; first line of the statement.
314 (icon-backward-to-start-of-continued-exp containing-sexp)
315 (+ icon-continued-statement-offset (current-column)
316 (if (save-excursion (goto-char indent-point)
317 (skip-chars-forward " \t")
318 (eq (following-char) ?{))
319 icon-continued-brace-offset 0)))
320 ;; This line starts a new statement.
321 ;; Position following last unclosed open.
322 (goto-char containing-sexp)
323 ;; Is line first statement after an open-brace?
325 ;; If no, find that first statement and indent like it.
327 (if (looking-at "procedure\\s ")
330 (while (progn (skip-chars-forward " \t\n")
332 ;; Skip over comments following openbrace.
334 ;; The first following code counts
335 ;; if it is before the line we want to indent.
336 (and (< (point) indent-point)
338 ;; If no previous statement,
339 ;; indent it relative to line brace is on.
340 ;; For open brace in column zero, don't let statement
341 ;; start there too. If icon-indent-level is zero,
342 ;; use icon-brace-offset + icon-continued-statement-offset
344 ;; For open-braces not the first thing in a line,
345 ;; add in icon-brace-imaginary-offset.
346 (+ (if (and (bolp) (zerop icon-indent-level))
348 icon-continued-statement-offset)
350 ;; Move back over whitespace before the openbrace.
351 ;; If openbrace is not first nonwhite thing on the line,
352 ;; add the icon-brace-imaginary-offset.
353 (progn (skip-chars-backward " \t")
354 (if (bolp) 0 icon-brace-imaginary-offset))
355 ;; Get initial indentation of the line we are on.
356 (current-indentation))))))))))
358 ;; List of words to check for as the last thing on a line.
359 ;; If cdr is t, next line is a continuation of the same statement,
360 ;; if cdr is nil, next line starts a new (possibly indented) statement.
362 (defconst icon-resword-alist
363 '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
364 ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
365 ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
366 ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
368 (defun icon-is-continuation-line ()
369 (let* ((ch (preceding-char))
370 (ch-syntax (char-syntax ch)))
371 (if (eq ch-syntax ?w)
372 (assoc (buffer-substring
373 (progn (forward-word -1) (point))
374 (progn (forward-word 1) (point)))
376 (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
378 (defun icon-backward-to-noncomment (lim)
381 (skip-chars-backward " \t\n\f" lim)
382 (setq opoint (point))
384 (if (and (nth 5 (parse-partial-sexp (point) opoint))
386 (search-backward "#")
389 (defun icon-backward-to-start-of-continued-exp (lim)
390 (if (memq (preceding-char) '(?\) ?\]))
393 (skip-chars-forward " \t")
395 ((<= (point) lim) (goto-char (1+ lim)))
396 ((not (icon-is-continued-line)) 0)
397 ((and (eq (char-syntax (following-char)) ?w)
399 (assoc (buffer-substring (point)
400 (save-excursion (forward-word 1) (point)))
401 icon-resword-alist))) 0)
402 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
404 (defun icon-is-continued-line ()
407 (icon-is-continuation-line)))
409 (defun icon-backward-to-start-of-if (&optional limit)
410 "Move to the start of the last \"unbalanced\" if."
411 (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
413 (case-fold-search nil))
414 (while (not (zerop if-level))
416 (cond ((looking-at "else\\b")
417 (setq if-level (1+ if-level)))
418 ((looking-at "if\\b")
419 (setq if-level (1- if-level)))
422 (goto-char limit))))))
424 (defun mark-icon-function ()
425 "Put mark at end of Icon function, point at beginning."
430 (beginning-of-line 0)
431 (beginning-of-icon-defun))
433 (defun beginning-of-icon-defun ()
434 "Go to the start of the enclosing procedure; return t if at top level."
436 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
440 (defun end-of-icon-defun ()
442 (if (not (bobp)) (forward-char -1))
443 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
447 (defun indent-icon-exp ()
448 "Indent each line of the Icon grouping following point."
450 (let ((indent-stack (list nil))
451 (contain-stack (list (point)))
452 (case-fold-search nil)
453 restart outer-loop-done inner-loop-done state ostate
454 this-indent last-sexp
455 at-else at-brace at-do
461 (setq outer-loop-done nil)
462 (while (and (not (eobp)) (not outer-loop-done))
463 (setq last-depth next-depth)
464 ;; Compute how depth changes over this line
465 ;; plus enough other lines to get to one that
466 ;; does not end inside a comment or string.
467 ;; Meanwhile, do appropriate indentation on comment lines.
468 (setq innerloop-done nil)
469 (while (and (not innerloop-done)
470 (not (and (eobp) (setq outer-loop-done t))))
472 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
474 (setq next-depth (car state))
475 (if (and (car (cdr (cdr state)))
476 (>= (car (cdr (cdr state))) 0))
477 (setq last-sexp (car (cdr (cdr state)))))
478 (if (or (nth 4 ostate))
480 (if (or (nth 3 state))
482 (setq innerloop-done t)))
483 (if (<= next-depth 0)
484 (setq outer-loop-done t))
487 (if (/= last-depth next-depth)
488 (setq last-sexp nil))
489 (while (> last-depth next-depth)
490 (setq indent-stack (cdr indent-stack)
491 contain-stack (cdr contain-stack)
492 last-depth (1- last-depth)))
493 (while (< last-depth next-depth)
494 (setq indent-stack (cons nil indent-stack)
495 contain-stack (cons nil contain-stack)
496 last-depth (1+ last-depth)))
497 (if (null (car contain-stack))
498 (setcar contain-stack (or (car (cdr state))
499 (save-excursion (forward-sexp -1)
502 (skip-chars-forward " \t")
505 (if (and (car indent-stack)
506 (>= (car indent-stack) 0))
507 ;; Line is on an existing nesting level.
508 ;; Lines inside parens are handled specially.
509 (if (/= (char-after (car contain-stack)) ?{)
510 (setq this-indent (car indent-stack))
511 ;; Line is at statement level.
512 ;; Is it a new statement? Is it an else?
513 ;; Find last non-comment character before this line
515 (setq at-else (looking-at "else\\W"))
516 (setq at-brace (= (following-char) ?{))
517 (icon-backward-to-noncomment opoint)
518 (if (icon-is-continuation-line)
519 ;; Preceding line did not end in comma or semi;
520 ;; indent this line icon-continued-statement-offset
521 ;; more than previous.
523 (icon-backward-to-start-of-continued-exp (car contain-stack))
525 (+ icon-continued-statement-offset (current-column)
526 (if at-brace icon-continued-brace-offset 0))))
527 ;; Preceding line ended in comma or semi;
528 ;; use the standard indent for this level.
530 (progn (icon-backward-to-start-of-if opoint)
531 (setq this-indent (current-indentation)))
532 (setq this-indent (car indent-stack))))))
533 ;; Just started a new nesting level.
534 ;; Compute the standard indent for this level.
535 (let ((val (calculate-icon-indent
536 (if (car indent-stack)
537 (- (car indent-stack))))))
539 (setq this-indent val))))
540 ;; Adjust line indentation according to its contents
541 (if (or (= (following-char) ?})
542 (looking-at "end\\b"))
543 (setq this-indent (- this-indent icon-indent-level)))
544 (if (= (following-char) ?{)
545 (setq this-indent (+ this-indent icon-brace-offset)))
546 ;; Put chosen indentation into effect.
547 (or (= (current-column) this-indent)
549 (delete-region (point) (progn (beginning-of-line) (point)))
550 (indent-to this-indent)))
551 ;; Indent any comment following the text.
552 (or (looking-at comment-start-skip)
553 (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
554 (progn (indent-for-comment) (beginning-of-line))))))))))
556 ;;; icon.el ends here