*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / icon.el
CommitLineData
1a06eabd
ER
1;;; icon.el --- mode for editing Icon code
2
e5167999
ER
3;; Author: Chris Smith <convex!csmith>
4;; Created: 15 Feb 89
5;; Last-Modified: 06 Mar 1991
a2535589 6
a2535589
JA
7;; Copyright (C) 1989 Free Software Foundation, Inc.
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
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
e5167999
ER
25;;; Commentary:
26
27;; Note: use
28;; (autoload 'icon-mode "icon" nil t)
29;; (setq auto-mode-alist (cons '("\\.icn$" . icon-mode) auto-mode-alist))
30;; if not permanently installed in your emacs
31
32;;; Code:
a2535589
JA
33
34(defvar icon-mode-abbrev-table nil
35 "Abbrev table in use in Icon-mode buffers.")
36(define-abbrev-table 'icon-mode-abbrev-table ())
37
38(defvar icon-mode-map ()
39 "Keymap used in Icon mode.")
40(if icon-mode-map
41 ()
42 (setq icon-mode-map (make-sparse-keymap))
43 (define-key icon-mode-map "{" 'electric-icon-brace)
44 (define-key icon-mode-map "}" 'electric-icon-brace)
45 (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
46 (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
47 (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
48 (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
49 (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
50 (define-key icon-mode-map "\t" 'icon-indent-command))
51
52(defvar icon-mode-syntax-table nil
53 "Syntax table in use in Icon-mode buffers.")
54
55(if icon-mode-syntax-table
56 ()
57 (setq icon-mode-syntax-table (make-syntax-table))
58 (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
59 (modify-syntax-entry ?# "<" icon-mode-syntax-table)
60 (modify-syntax-entry ?\n ">" 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)
71 (modify-syntax-entry ?| "." icon-mode-syntax-table)
72 (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
73
74(defconst icon-indent-level 4
75 "*Indentation of Icon statements with respect to containing block.")
76(defconst icon-brace-imaginary-offset 0
77 "*Imagined indentation of a Icon open brace that actually follows a statement.")
78(defconst icon-brace-offset 0
79 "*Extra indentation for braces, compared with other text in same context.")
80(defconst icon-continued-statement-offset 4
81 "*Extra indent for lines not starting new statements.")
82(defconst icon-continued-brace-offset 0
83 "*Extra indent for substatements that start with open-braces.
84This is in addition to icon-continued-statement-offset.")
85
86(defconst icon-auto-newline nil
87 "*Non-nil means automatically newline before and after braces
88inserted in Icon code.")
89
90(defconst icon-tab-always-indent t
91 "*Non-nil means TAB in Icon mode should always reindent the current line,
92regardless of where in the line point is when the TAB command is used.")
93\f
94(defun icon-mode ()
95 "Major mode for editing Icon code.
96Expression and list commands understand all Icon brackets.
97Tab indents for Icon code.
98Paragraphs are separated by blank lines only.
99Delete converts tabs to spaces as it moves back.
100\\{icon-mode-map}
101Variables controlling indentation style:
102 icon-tab-always-indent
103 Non-nil means TAB in Icon mode should always reindent the current line,
104 regardless of where in the line point is when the TAB command is used.
105 icon-auto-newline
106 Non-nil means automatically newline before and after braces
107 inserted in Icon code.
108 icon-indent-level
109 Indentation of Icon statements within surrounding block.
110 The surrounding block's indentation is the indentation
111 of the line on which the open-brace appears.
112 icon-continued-statement-offset
113 Extra indentation given to a substatement, such as the
114 then-clause of an if or body of a while.
115 icon-continued-brace-offset
116 Extra indentation given to a brace that starts a substatement.
073c9531 117 This is in addition to `icon-continued-statement-offset'.
a2535589
JA
118 icon-brace-offset
119 Extra indentation for line if it starts with an open brace.
120 icon-brace-imaginary-offset
121 An open brace following other text is treated as if it were
122 this far to the right of the start of its line.
123
073c9531
JB
124Turning on Icon mode calls the value of the variable `icon-mode-hook'
125with no args, if that value is non-nil."
a2535589
JA
126 (interactive)
127 (kill-all-local-variables)
128 (use-local-map icon-mode-map)
129 (setq major-mode 'icon-mode)
130 (setq mode-name "Icon")
131 (setq local-abbrev-table icon-mode-abbrev-table)
132 (set-syntax-table icon-mode-syntax-table)
133 (make-local-variable 'paragraph-start)
134 (setq paragraph-start (concat "^$\\|" page-delimiter))
135 (make-local-variable 'paragraph-separate)
136 (setq paragraph-separate paragraph-start)
137 (make-local-variable 'indent-line-function)
138 (setq indent-line-function 'icon-indent-line)
139 (make-local-variable 'require-final-newline)
140 (setq require-final-newline t)
141 (make-local-variable 'comment-start)
142 (setq comment-start "# ")
143 (make-local-variable 'comment-end)
144 (setq comment-end "")
145 (make-local-variable 'comment-column)
146 (setq comment-column 32)
147 (make-local-variable 'comment-start-skip)
148 (setq comment-start-skip "# *")
149 (make-local-variable 'comment-indent-hook)
150 (setq comment-indent-hook 'icon-comment-indent)
151 (run-hooks 'icon-mode-hook))
152\f
073c9531
JB
153;; This is used by indent-for-comment to decide how much to
154;; indent a comment in Icon code based on its context.
a2535589
JA
155(defun icon-comment-indent ()
156 (if (looking-at "^#")
157 0
158 (save-excursion
159 (skip-chars-backward " \t")
160 (max (if (bolp) 0 (1+ (current-column)))
161 comment-column))))
162
163(defun electric-icon-brace (arg)
164 "Insert character and correct line's indentation."
165 (interactive "P")
166 (let (insertpos)
167 (if (and (not arg)
168 (eolp)
169 (or (save-excursion
170 (skip-chars-backward " \t")
171 (bolp))
172 (if icon-auto-newline
173 (progn (icon-indent-line) (newline) t)
174 nil)))
175 (progn
176 (insert last-command-char)
177 (icon-indent-line)
178 (if icon-auto-newline
179 (progn
180 (newline)
181 ;; (newline) may have done auto-fill
182 (setq insertpos (- (point) 2))
183 (icon-indent-line)))
184 (save-excursion
185 (if insertpos (goto-char (1+ insertpos)))
186 (delete-char -1))))
187 (if insertpos
188 (save-excursion
189 (goto-char insertpos)
190 (self-insert-command (prefix-numeric-value arg)))
191 (self-insert-command (prefix-numeric-value arg)))))
192\f
193(defun icon-indent-command (&optional whole-exp)
194 (interactive "P")
195 "Indent current line as Icon code, or in some cases insert a tab character.
073c9531
JB
196If `icon-tab-always-indent' is non-nil (the default), always indent current
197line. Otherwise, indent the current line only if point is at the left margin
a2535589
JA
198or in the line's indentation; otherwise insert a tab.
199
073c9531
JB
200A numeric argument, regardless of its value, means indent rigidly all the
201lines of the expression starting after point so that this line becomes
202properly indented. The relative indentation among the lines of the
203expression are preserved."
a2535589
JA
204 (if whole-exp
205 ;; If arg, always indent this line as Icon
206 ;; and shift remaining lines of expression the same amount.
207 (let ((shift-amt (icon-indent-line))
208 beg end)
209 (save-excursion
210 (if icon-tab-always-indent
211 (beginning-of-line))
212 (setq beg (point))
213 (forward-sexp 1)
214 (setq end (point))
215 (goto-char beg)
216 (forward-line 1)
217 (setq beg (point)))
218 (if (> end beg)
219 (indent-code-rigidly beg end shift-amt "#")))
220 (if (and (not icon-tab-always-indent)
221 (save-excursion
222 (skip-chars-backward " \t")
223 (not (bolp))))
224 (insert-tab)
225 (icon-indent-line))))
226
227(defun icon-indent-line ()
228 "Indent current line as Icon code.
229Return the amount the indentation changed by."
230 (let ((indent (calculate-icon-indent nil))
231 beg shift-amt
232 (case-fold-search nil)
233 (pos (- (point-max) (point))))
234 (beginning-of-line)
235 (setq beg (point))
236 (cond ((eq indent nil)
237 (setq indent (current-indentation)))
238 ((eq indent t)
239 (setq indent (calculate-icon-indent-within-comment)))
240 ((looking-at "[ \t]*#")
241 (setq indent 0))
242 (t
243 (skip-chars-forward " \t")
244 (if (listp indent) (setq indent (car indent)))
245 (cond ((and (looking-at "else\\b")
246 (not (looking-at "else\\s_")))
247 (setq indent (save-excursion
248 (icon-backward-to-start-of-if)
249 (current-indentation))))
250 ((or (= (following-char) ?})
251 (looking-at "end\\b"))
252 (setq indent (- indent icon-indent-level)))
253 ((= (following-char) ?{)
254 (setq indent (+ indent icon-brace-offset))))))
255 (skip-chars-forward " \t")
256 (setq shift-amt (- indent (current-column)))
257 (if (zerop shift-amt)
258 (if (> (- (point-max) pos) (point))
259 (goto-char (- (point-max) pos)))
260 (delete-region beg (point))
261 (indent-to indent)
262 ;; If initial point was within line's indentation,
263 ;; position after the indentation. Else stay at same point in text.
264 (if (> (- (point-max) pos) (point))
265 (goto-char (- (point-max) pos))))
266 shift-amt))
267
268(defun calculate-icon-indent (&optional parse-start)
269 "Return appropriate indentation for current line as Icon code.
270In usual case returns an integer: the column to indent to.
271Returns nil if line starts inside a string, t if in a comment."
272 (save-excursion
273 (beginning-of-line)
274 (let ((indent-point (point))
275 (case-fold-search nil)
276 state
277 containing-sexp
278 toplevel)
279 (if parse-start
280 (goto-char parse-start)
281 (setq toplevel (beginning-of-icon-defun)))
282 (while (< (point) indent-point)
283 (setq parse-start (point))
284 (setq state (parse-partial-sexp (point) indent-point 0))
285 (setq containing-sexp (car (cdr state))))
286 (cond ((or (nth 3 state) (nth 4 state))
287 ;; return nil or t if should not change this line
288 (nth 4 state))
289 ((and containing-sexp
290 (/= (char-after containing-sexp) ?{))
291 ;; line is expression, not statement:
292 ;; indent to just after the surrounding open.
293 (goto-char (1+ containing-sexp))
294 (current-column))
295 (t
296 (if toplevel
297 ;; Outside any procedures.
298 (progn (icon-backward-to-noncomment (point-min))
299 (if (icon-is-continuation-line)
300 icon-continued-statement-offset 0))
301 ;; Statement level.
302 (if (null containing-sexp)
303 (progn (beginning-of-icon-defun)
304 (setq containing-sexp (point))))
305 (goto-char indent-point)
306 ;; Is it a continuation or a new statement?
307 ;; Find previous non-comment character.
308 (icon-backward-to-noncomment containing-sexp)
309 ;; Now we get the answer.
310 (if (icon-is-continuation-line)
311 ;; This line is continuation of preceding line's statement;
312 ;; indent icon-continued-statement-offset more than the
313 ;; first line of the statement.
314 (progn
315 (icon-backward-to-start-of-continued-exp containing-sexp)
316 (+ icon-continued-statement-offset (current-column)
317 (if (save-excursion (goto-char indent-point)
318 (skip-chars-forward " \t")
319 (eq (following-char) ?{))
320 icon-continued-brace-offset 0)))
321 ;; This line starts a new statement.
322 ;; Position following last unclosed open.
323 (goto-char containing-sexp)
324 ;; Is line first statement after an open-brace?
325 (or
326 ;; If no, find that first statement and indent like it.
327 (save-excursion
328 (if (looking-at "procedure\\s ")
329 (forward-sexp 3)
330 (forward-char 1))
331 (while (progn (skip-chars-forward " \t\n")
332 (looking-at "#"))
333 ;; Skip over comments following openbrace.
334 (forward-line 1))
335 ;; The first following code counts
336 ;; if it is before the line we want to indent.
337 (and (< (point) indent-point)
338 (current-column)))
339 ;; If no previous statement,
340 ;; indent it relative to line brace is on.
341 ;; For open brace in column zero, don't let statement
342 ;; start there too. If icon-indent-level is zero,
343 ;; use icon-brace-offset + icon-continued-statement-offset
344 ;; instead.
345 ;; For open-braces not the first thing in a line,
346 ;; add in icon-brace-imaginary-offset.
347 (+ (if (and (bolp) (zerop icon-indent-level))
348 (+ icon-brace-offset
349 icon-continued-statement-offset)
350 icon-indent-level)
351 ;; Move back over whitespace before the openbrace.
352 ;; If openbrace is not first nonwhite thing on the line,
353 ;; add the icon-brace-imaginary-offset.
354 (progn (skip-chars-backward " \t")
355 (if (bolp) 0 icon-brace-imaginary-offset))
356 ;; Get initial indentation of the line we are on.
357 (current-indentation))))))))))
358
359;; List of words to check for as the last thing on a line.
360;; If cdr is t, next line is a continuation of the same statement,
361;; if cdr is nil, next line starts a new (possibly indented) statement.
362
363(defconst icon-resword-alist
364 '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
365 ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
366 ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
367 ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
368
369(defun icon-is-continuation-line ()
370 (let* ((ch (preceding-char))
371 (ch-syntax (char-syntax ch)))
372 (if (eq ch-syntax ?w)
373 (assoc (buffer-substring
374 (progn (forward-word -1) (point))
375 (progn (forward-word 1) (point)))
376 icon-resword-alist)
377 (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
378
379(defun icon-backward-to-noncomment (lim)
380 (let (opoint stop)
381 (while (not stop)
382 (skip-chars-backward " \t\n\f" lim)
383 (setq opoint (point))
384 (beginning-of-line)
385 (if (and (nth 5 (parse-partial-sexp (point) opoint))
386 (< lim (point)))
387 (search-backward "#")
388 (setq stop t)))))
389
390(defun icon-backward-to-start-of-continued-exp (lim)
391 (if (memq (preceding-char) '(?\) ?\]))
392 (forward-sexp -1))
393 (beginning-of-line)
394 (skip-chars-forward " \t")
395 (cond
396 ((<= (point) lim) (goto-char (1+ lim)))
397 ((not (icon-is-continued-line)) 0)
398 ((and (eq (char-syntax (following-char)) ?w)
399 (cdr
400 (assoc (buffer-substring (point)
401 (save-excursion (forward-word 1) (point)))
402 icon-resword-alist))) 0)
403 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
404
405(defun icon-is-continued-line ()
406 (save-excursion
407 (end-of-line 0)
408 (icon-is-continuation-line)))
409
410(defun icon-backward-to-start-of-if (&optional limit)
073c9531 411 "Move to the start of the last \"unbalanced\" if."
a2535589
JA
412 (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
413 (let ((if-level 1)
414 (case-fold-search nil))
415 (while (not (zerop if-level))
416 (backward-sexp 1)
417 (cond ((looking-at "else\\b")
418 (setq if-level (1+ if-level)))
419 ((looking-at "if\\b")
420 (setq if-level (1- if-level)))
421 ((< (point) limit)
422 (setq if-level 0)
423 (goto-char limit))))))
424\f
425(defun mark-icon-function ()
426 "Put mark at end of Icon function, point at beginning."
427 (interactive)
428 (push-mark (point))
429 (end-of-icon-defun)
430 (push-mark (point))
431 (beginning-of-line 0)
432 (beginning-of-icon-defun))
433
434(defun beginning-of-icon-defun ()
435 "Go to the start of the enclosing procedure; return t if at top level."
436 (interactive)
437 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
438 (looking-at "e")
439 t))
440
441(defun end-of-icon-defun ()
442 (interactive)
443 (if (not (bobp)) (forward-char -1))
444 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
445 (forward-word -1)
446 (forward-line 1))
447\f
448(defun indent-icon-exp ()
449 "Indent each line of the Icon grouping following point."
450 (interactive)
451 (let ((indent-stack (list nil))
452 (contain-stack (list (point)))
453 (case-fold-search nil)
454 restart outer-loop-done inner-loop-done state ostate
455 this-indent last-sexp
456 at-else at-brace at-do
457 (opoint (point))
458 (next-depth 0))
459 (save-excursion
460 (forward-sexp 1))
461 (save-excursion
462 (setq outer-loop-done nil)
463 (while (and (not (eobp)) (not outer-loop-done))
464 (setq last-depth next-depth)
465 ;; Compute how depth changes over this line
466 ;; plus enough other lines to get to one that
467 ;; does not end inside a comment or string.
468 ;; Meanwhile, do appropriate indentation on comment lines.
469 (setq innerloop-done nil)
470 (while (and (not innerloop-done)
471 (not (and (eobp) (setq outer-loop-done t))))
472 (setq ostate state)
473 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
474 nil nil state))
475 (setq next-depth (car state))
476 (if (and (car (cdr (cdr state)))
477 (>= (car (cdr (cdr state))) 0))
478 (setq last-sexp (car (cdr (cdr state)))))
479 (if (or (nth 4 ostate))
480 (icon-indent-line))
481 (if (or (nth 3 state))
482 (forward-line 1)
483 (setq innerloop-done t)))
484 (if (<= next-depth 0)
485 (setq outer-loop-done t))
486 (if outer-loop-done
487 nil
488 (if (/= last-depth next-depth)
489 (setq last-sexp nil))
490 (while (> last-depth next-depth)
491 (setq indent-stack (cdr indent-stack)
492 contain-stack (cdr contain-stack)
493 last-depth (1- last-depth)))
494 (while (< last-depth next-depth)
495 (setq indent-stack (cons nil indent-stack)
496 contain-stack (cons nil contain-stack)
497 last-depth (1+ last-depth)))
498 (if (null (car contain-stack))
499 (setcar contain-stack (or (car (cdr state))
500 (save-excursion (forward-sexp -1)
501 (point)))))
502 (forward-line 1)
503 (skip-chars-forward " \t")
504 (if (eolp)
505 nil
506 (if (and (car indent-stack)
507 (>= (car indent-stack) 0))
508 ;; Line is on an existing nesting level.
509 ;; Lines inside parens are handled specially.
510 (if (/= (char-after (car contain-stack)) ?{)
511 (setq this-indent (car indent-stack))
512 ;; Line is at statement level.
513 ;; Is it a new statement? Is it an else?
514 ;; Find last non-comment character before this line
515 (save-excursion
516 (setq at-else (looking-at "else\\W"))
517 (setq at-brace (= (following-char) ?{))
518 (icon-backward-to-noncomment opoint)
519 (if (icon-is-continuation-line)
520 ;; Preceding line did not end in comma or semi;
521 ;; indent this line icon-continued-statement-offset
522 ;; more than previous.
523 (progn
524 (icon-backward-to-start-of-continued-exp (car contain-stack))
525 (setq this-indent
526 (+ icon-continued-statement-offset (current-column)
527 (if at-brace icon-continued-brace-offset 0))))
528 ;; Preceding line ended in comma or semi;
529 ;; use the standard indent for this level.
530 (if at-else
531 (progn (icon-backward-to-start-of-if opoint)
532 (setq this-indent (current-indentation)))
533 (setq this-indent (car indent-stack))))))
534 ;; Just started a new nesting level.
535 ;; Compute the standard indent for this level.
536 (let ((val (calculate-icon-indent
537 (if (car indent-stack)
538 (- (car indent-stack))))))
539 (setcar indent-stack
540 (setq this-indent val))))
541 ;; Adjust line indentation according to its contents
542 (if (or (= (following-char) ?})
543 (looking-at "end\\b"))
544 (setq this-indent (- this-indent icon-indent-level)))
545 (if (= (following-char) ?{)
546 (setq this-indent (+ this-indent icon-brace-offset)))
547 ;; Put chosen indentation into effect.
548 (or (= (current-column) this-indent)
549 (progn
550 (delete-region (point) (progn (beginning-of-line) (point)))
551 (indent-to this-indent)))
552 ;; Indent any comment following the text.
553 (or (looking-at comment-start-skip)
554 (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
555 (progn (indent-for-comment) (beginning-of-line))))))))))
556
1a06eabd 557;;; icon.el ends here