Added or corrected Commentary sections
[bpt/emacs.git] / lisp / progmodes / icon.el
1 ;;; icon.el --- mode for editing Icon code
2
3 ;; Copyright (C) 1989 Free Software Foundation, Inc.
4
5 ;; Author: Chris Smith <convex!csmith>
6 ;; Created: 15 Feb 89
7 ;; Keywords: languages
8
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
24
25 ;;; Commentary:
26
27 ;; A major mode for editing the Icon programming language.
28 ;;
29 ;; Note: use
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
33
34 ;;; Code:
35
36 (defvar icon-mode-abbrev-table nil
37 "Abbrev table in use in Icon-mode buffers.")
38 (define-abbrev-table 'icon-mode-abbrev-table ())
39
40 (defvar icon-mode-map ()
41 "Keymap used in Icon mode.")
42 (if icon-mode-map
43 ()
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))
53
54 (defvar icon-mode-syntax-table nil
55 "Syntax table in use in Icon-mode buffers.")
56
57 (if icon-mode-syntax-table
58 ()
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))
75
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.")
87
88 (defconst icon-auto-newline nil
89 "*Non-nil means automatically newline before and after braces
90 inserted in Icon code.")
91
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.")
95 \f
96 (defun icon-mode ()
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.
102 \\{icon-mode-map}
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.
107 icon-auto-newline
108 Non-nil means automatically newline before and after braces
109 inserted in Icon code.
110 icon-indent-level
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'.
120 icon-brace-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.
125
126 Turning on Icon mode calls the value of the variable `icon-mode-hook'
127 with no args, if that value is non-nil."
128 (interactive)
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))
154 \f
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 "^#")
159 0
160 (save-excursion
161 (skip-chars-backward " \t")
162 (max (if (bolp) 0 (1+ (current-column)))
163 comment-column))))
164
165 (defun electric-icon-brace (arg)
166 "Insert character and correct line's indentation."
167 (interactive "P")
168 (let (insertpos)
169 (if (and (not arg)
170 (eolp)
171 (or (save-excursion
172 (skip-chars-backward " \t")
173 (bolp))
174 (if icon-auto-newline
175 (progn (icon-indent-line) (newline) t)
176 nil)))
177 (progn
178 (insert last-command-char)
179 (icon-indent-line)
180 (if icon-auto-newline
181 (progn
182 (newline)
183 ;; (newline) may have done auto-fill
184 (setq insertpos (- (point) 2))
185 (icon-indent-line)))
186 (save-excursion
187 (if insertpos (goto-char (1+ insertpos)))
188 (delete-char -1))))
189 (if insertpos
190 (save-excursion
191 (goto-char insertpos)
192 (self-insert-command (prefix-numeric-value arg)))
193 (self-insert-command (prefix-numeric-value arg)))))
194 \f
195 (defun icon-indent-command (&optional whole-exp)
196 (interactive "P")
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.
201
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."
206 (if whole-exp
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))
210 beg end)
211 (save-excursion
212 (if icon-tab-always-indent
213 (beginning-of-line))
214 (setq beg (point))
215 (forward-sexp 1)
216 (setq end (point))
217 (goto-char beg)
218 (forward-line 1)
219 (setq beg (point)))
220 (if (> end beg)
221 (indent-code-rigidly beg end shift-amt "#")))
222 (if (and (not icon-tab-always-indent)
223 (save-excursion
224 (skip-chars-backward " \t")
225 (not (bolp))))
226 (insert-tab)
227 (icon-indent-line))))
228
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))
233 beg shift-amt
234 (case-fold-search nil)
235 (pos (- (point-max) (point))))
236 (beginning-of-line)
237 (setq beg (point))
238 (cond ((eq indent nil)
239 (setq indent (current-indentation)))
240 ((eq indent t)
241 (setq indent (calculate-icon-indent-within-comment)))
242 ((looking-at "[ \t]*#")
243 (setq indent 0))
244 (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))
263 (indent-to indent)
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))))
268 shift-amt))
269
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."
274 (save-excursion
275 (beginning-of-line)
276 (let ((indent-point (point))
277 (case-fold-search nil)
278 state
279 containing-sexp
280 toplevel)
281 (if parse-start
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
290 (nth 4 state))
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))
296 (current-column))
297 (t
298 (if toplevel
299 ;; Outside any procedures.
300 (progn (icon-backward-to-noncomment (point-min))
301 (if (icon-is-continuation-line)
302 icon-continued-statement-offset 0))
303 ;; Statement level.
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.
316 (progn
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?
327 (or
328 ;; If no, find that first statement and indent like it.
329 (save-excursion
330 (if (looking-at "procedure\\s ")
331 (forward-sexp 3)
332 (forward-char 1))
333 (while (progn (skip-chars-forward " \t\n")
334 (looking-at "#"))
335 ;; Skip over comments following openbrace.
336 (forward-line 1))
337 ;; The first following code counts
338 ;; if it is before the line we want to indent.
339 (and (< (point) indent-point)
340 (current-column)))
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
346 ;; instead.
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))
350 (+ icon-brace-offset
351 icon-continued-statement-offset)
352 icon-indent-level)
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))))))))))
360
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.
364
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)))
370
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)))
378 icon-resword-alist)
379 (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
380
381 (defun icon-backward-to-noncomment (lim)
382 (let (opoint stop)
383 (while (not stop)
384 (skip-chars-backward " \t\n\f" lim)
385 (setq opoint (point))
386 (beginning-of-line)
387 (if (and (nth 5 (parse-partial-sexp (point) opoint))
388 (< lim (point)))
389 (search-backward "#")
390 (setq stop t)))))
391
392 (defun icon-backward-to-start-of-continued-exp (lim)
393 (if (memq (preceding-char) '(?\) ?\]))
394 (forward-sexp -1))
395 (beginning-of-line)
396 (skip-chars-forward " \t")
397 (cond
398 ((<= (point) lim) (goto-char (1+ lim)))
399 ((not (icon-is-continued-line)) 0)
400 ((and (eq (char-syntax (following-char)) ?w)
401 (cdr
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))))
406
407 (defun icon-is-continued-line ()
408 (save-excursion
409 (end-of-line 0)
410 (icon-is-continuation-line)))
411
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))))
415 (let ((if-level 1)
416 (case-fold-search nil))
417 (while (not (zerop if-level))
418 (backward-sexp 1)
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)))
423 ((< (point) limit)
424 (setq if-level 0)
425 (goto-char limit))))))
426 \f
427 (defun mark-icon-function ()
428 "Put mark at end of Icon function, point at beginning."
429 (interactive)
430 (push-mark (point))
431 (end-of-icon-defun)
432 (push-mark (point))
433 (beginning-of-line 0)
434 (beginning-of-icon-defun))
435
436 (defun beginning-of-icon-defun ()
437 "Go to the start of the enclosing procedure; return t if at top level."
438 (interactive)
439 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
440 (looking-at "e")
441 t))
442
443 (defun end-of-icon-defun ()
444 (interactive)
445 (if (not (bobp)) (forward-char -1))
446 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
447 (forward-word -1)
448 (forward-line 1))
449 \f
450 (defun indent-icon-exp ()
451 "Indent each line of the Icon grouping following point."
452 (interactive)
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
459 (opoint (point))
460 (next-depth 0))
461 (save-excursion
462 (forward-sexp 1))
463 (save-excursion
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))))
474 (setq ostate state)
475 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
476 nil nil state))
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))
482 (icon-indent-line))
483 (if (or (nth 3 state))
484 (forward-line 1)
485 (setq innerloop-done t)))
486 (if (<= next-depth 0)
487 (setq outer-loop-done t))
488 (if outer-loop-done
489 nil
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)
503 (point)))))
504 (forward-line 1)
505 (skip-chars-forward " \t")
506 (if (eolp)
507 nil
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
517 (save-excursion
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.
525 (progn
526 (icon-backward-to-start-of-continued-exp (car contain-stack))
527 (setq this-indent
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.
532 (if at-else
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))))))
541 (setcar 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)
551 (progn
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))))))))))
558
559 ;;; icon.el ends here