]> code.delx.au - gnu-emacs/blob - lisp/progmodes/icon.el
(makefile-gnumake-functions-alist): Add `addprefix'.
[gnu-emacs] / 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 <csmith@convex.com>
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; A major mode for editing the Icon programming language.
29
30 ;;; Code:
31
32 (defvar icon-mode-abbrev-table nil
33 "Abbrev table in use in Icon-mode buffers.")
34 (define-abbrev-table 'icon-mode-abbrev-table ())
35
36 (defvar icon-mode-map ()
37 "Keymap used in Icon mode.")
38 (if icon-mode-map
39 ()
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))
49
50 (defvar icon-mode-syntax-table nil
51 "Syntax table in use in Icon-mode buffers.")
52
53 (if icon-mode-syntax-table
54 ()
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))
71
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.")
83
84 (defvar icon-auto-newline nil
85 "*Non-nil means automatically newline before and after braces
86 inserted in Icon code.")
87
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.")
91 \f
92 ;;;###autoload
93 (defun icon-mode ()
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.
99 \\{icon-mode-map}
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.
104 icon-auto-newline
105 Non-nil means automatically newline before and after braces
106 inserted in Icon code.
107 icon-indent-level
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'.
117 icon-brace-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.
122
123 Turning on Icon mode calls the value of the variable `icon-mode-hook'
124 with no args, if that value is non-nil."
125 (interactive)
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))
151 \f
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 "^#")
156 0
157 (save-excursion
158 (skip-chars-backward " \t")
159 (max (if (bolp) 0 (1+ (current-column)))
160 comment-column))))
161
162 (defun electric-icon-brace (arg)
163 "Insert character and correct line's indentation."
164 (interactive "P")
165 (let (insertpos)
166 (if (and (not arg)
167 (eolp)
168 (or (save-excursion
169 (skip-chars-backward " \t")
170 (bolp))
171 (if icon-auto-newline
172 (progn (icon-indent-line) (newline) t)
173 nil)))
174 (progn
175 (insert last-command-char)
176 (icon-indent-line)
177 (if icon-auto-newline
178 (progn
179 (newline)
180 ;; (newline) may have done auto-fill
181 (setq insertpos (- (point) 2))
182 (icon-indent-line)))
183 (save-excursion
184 (if insertpos (goto-char (1+ insertpos)))
185 (delete-char -1))))
186 (if insertpos
187 (save-excursion
188 (goto-char insertpos)
189 (self-insert-command (prefix-numeric-value arg)))
190 (self-insert-command (prefix-numeric-value arg)))))
191 \f
192 (defun icon-indent-command (&optional whole-exp)
193 (interactive "P")
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.
198
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."
203 (if whole-exp
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))
207 beg end)
208 (save-excursion
209 (if icon-tab-always-indent
210 (beginning-of-line))
211 (setq beg (point))
212 (forward-sexp 1)
213 (setq end (point))
214 (goto-char beg)
215 (forward-line 1)
216 (setq beg (point)))
217 (if (> end beg)
218 (indent-code-rigidly beg end shift-amt "#")))
219 (if (and (not icon-tab-always-indent)
220 (save-excursion
221 (skip-chars-backward " \t")
222 (not (bolp))))
223 (insert-tab)
224 (icon-indent-line))))
225
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))
230 beg shift-amt
231 (case-fold-search nil)
232 (pos (- (point-max) (point))))
233 (beginning-of-line)
234 (setq beg (point))
235 (cond ((eq indent nil)
236 (setq indent (current-indentation)))
237 ((eq indent t)
238 (setq indent (calculate-icon-indent-within-comment)))
239 ((looking-at "[ \t]*#")
240 (setq indent 0))
241 (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))
260 (indent-to indent)
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))))
265 shift-amt))
266
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."
271 (save-excursion
272 (beginning-of-line)
273 (let ((indent-point (point))
274 (case-fold-search nil)
275 state
276 containing-sexp
277 toplevel)
278 (if parse-start
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
287 (nth 4 state))
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))
293 (current-column))
294 (t
295 (if toplevel
296 ;; Outside any procedures.
297 (progn (icon-backward-to-noncomment (point-min))
298 (if (icon-is-continuation-line)
299 icon-continued-statement-offset 0))
300 ;; Statement level.
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.
313 (progn
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?
324 (or
325 ;; If no, find that first statement and indent like it.
326 (save-excursion
327 (if (looking-at "procedure\\s ")
328 (forward-sexp 3)
329 (forward-char 1))
330 (while (progn (skip-chars-forward " \t\n")
331 (looking-at "#"))
332 ;; Skip over comments following openbrace.
333 (forward-line 1))
334 ;; The first following code counts
335 ;; if it is before the line we want to indent.
336 (and (< (point) indent-point)
337 (current-column)))
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
343 ;; instead.
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))
347 (+ icon-brace-offset
348 icon-continued-statement-offset)
349 icon-indent-level)
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))))))))))
357
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.
361
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)))
367
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)))
375 icon-resword-alist)
376 (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
377
378 (defun icon-backward-to-noncomment (lim)
379 (let (opoint stop)
380 (while (not stop)
381 (skip-chars-backward " \t\n\f" lim)
382 (setq opoint (point))
383 (beginning-of-line)
384 (if (and (nth 5 (parse-partial-sexp (point) opoint))
385 (< lim (point)))
386 (search-backward "#")
387 (setq stop t)))))
388
389 (defun icon-backward-to-start-of-continued-exp (lim)
390 (if (memq (preceding-char) '(?\) ?\]))
391 (forward-sexp -1))
392 (beginning-of-line)
393 (skip-chars-forward " \t")
394 (cond
395 ((<= (point) lim) (goto-char (1+ lim)))
396 ((not (icon-is-continued-line)) 0)
397 ((and (eq (char-syntax (following-char)) ?w)
398 (cdr
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))))
403
404 (defun icon-is-continued-line ()
405 (save-excursion
406 (end-of-line 0)
407 (icon-is-continuation-line)))
408
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))))
412 (let ((if-level 1)
413 (case-fold-search nil))
414 (while (not (zerop if-level))
415 (backward-sexp 1)
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)))
420 ((< (point) limit)
421 (setq if-level 0)
422 (goto-char limit))))))
423 \f
424 (defun mark-icon-function ()
425 "Put mark at end of Icon function, point at beginning."
426 (interactive)
427 (push-mark (point))
428 (end-of-icon-defun)
429 (push-mark (point))
430 (beginning-of-line 0)
431 (beginning-of-icon-defun))
432
433 (defun beginning-of-icon-defun ()
434 "Go to the start of the enclosing procedure; return t if at top level."
435 (interactive)
436 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
437 (looking-at "e")
438 t))
439
440 (defun end-of-icon-defun ()
441 (interactive)
442 (if (not (bobp)) (forward-char -1))
443 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
444 (forward-word -1)
445 (forward-line 1))
446 \f
447 (defun indent-icon-exp ()
448 "Indent each line of the Icon grouping following point."
449 (interactive)
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
456 (opoint (point))
457 (next-depth 0))
458 (save-excursion
459 (forward-sexp 1))
460 (save-excursion
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))))
471 (setq ostate state)
472 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
473 nil nil state))
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))
479 (icon-indent-line))
480 (if (or (nth 3 state))
481 (forward-line 1)
482 (setq innerloop-done t)))
483 (if (<= next-depth 0)
484 (setq outer-loop-done t))
485 (if outer-loop-done
486 nil
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)
500 (point)))))
501 (forward-line 1)
502 (skip-chars-forward " \t")
503 (if (eolp)
504 nil
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
514 (save-excursion
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.
522 (progn
523 (icon-backward-to-start-of-continued-exp (car contain-stack))
524 (setq this-indent
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.
529 (if at-else
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))))))
538 (setcar 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)
548 (progn
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))))))))))
555
556 ;;; icon.el ends here