1 ;;; simula.el --- SIMULA 87 code editing commands for Emacs
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Hans Henrik Eriksen <hhe@ifi.uio.no>
6 ;; Maintainer: simula-mode@ifi.uio.no
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 1, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; A major mode for editing the Simula language. It knows about Simula
30 ;; syntax and standard indentation commands. It also provides convenient
31 ;; abbrevs for Simula keywords.
33 ;; Hans Henrik Eriksen (the author) may be reached at:
34 ;; Institutt for informatikk,
35 ;; Universitetet i Oslo
39 (provide 'simula-mode)
41 (defconst simula-tab-always-indent nil
42 "*Non-nil means TAB in SIMULA mode should always reindent the current line,
43 regardless of where in the line point is when the TAB command is used.")
45 (defconst simula-indent-level 3
46 "*Indentation of SIMULA statements with respect to containing block.")
48 (defconst simula-substatement-offset 3
49 "*Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.")
51 (defconst simula-continued-statement-offset 3
52 "*Extra indentation for lines not starting a statement or substatement.
53 If value is a list, each line in a multipleline continued statement
54 will have the car of the list extra indentation with respect to
55 the previous line of the statement.")
57 (defconst simula-label-offset -4711
58 "*Offset of SIMULA label lines relative to usual indentation")
60 (defconst simula-if-indent '(0 . 0)
61 "*Extra indentation of THEN and ELSE with respect to the starting IF.
62 Value is a cons cell, the car is extra THEN indention and the cdr
63 extra ELSE indention. IF after ELSE is indented as the starting IF.")
65 (defconst simula-inspect-indent '(0 . 0)
66 "*Extra indentation of WHEN and OTHERWISE with respect to the
67 corresponding INSPECT. Value is a cons cell, the car is
68 extra WHEN indention and the cdr extra OTHERWISE indention.")
70 (defconst simula-electric-indent nil
71 "*If this variable is non-nil, the simula-indent-line function
72 will check the previous line to see if it has to be reindented.")
74 (defconst simula-abbrev-keyword 'upcase
75 "*Determine how SIMULA keywords will be expanded. Value is one of
76 the symbols upcase, downcase, capitalize, (as in) abbrev-table or
77 nil if they should not be changed.")
79 (defconst simula-abbrev-stdproc 'abbrev-table
80 "*Determine how standard SIMULA procedure and class names will be
81 expanded. Value is one of the symbols upcase, downcase, capitalize,
82 (as in) abbrev-table or nil if they should not be changed.")
84 (defvar simula-abbrev-file nil
85 "*File with abbrev definitions that are merged together with
86 the standard abbrev definitions. Please note that the standard
87 definitions are required for simula-mode to function correctly.")
89 (defvar simula-mode-syntax-table nil
90 "Syntax table in simula-mode buffers.")
92 (if simula-mode-syntax-table
94 (setq simula-mode-syntax-table (standard-syntax-table))
95 (modify-syntax-entry ?! "<" simula-mode-syntax-table)
96 (modify-syntax-entry ?$ "." simula-mode-syntax-table)
97 (modify-syntax-entry ?% "." simula-mode-syntax-table)
98 (modify-syntax-entry ?' "\"" simula-mode-syntax-table)
99 (modify-syntax-entry ?\( "()" simula-mode-syntax-table)
100 (modify-syntax-entry ?\) ")(" simula-mode-syntax-table)
101 (modify-syntax-entry ?\; ">" simula-mode-syntax-table)
102 (modify-syntax-entry ?\[ "." simula-mode-syntax-table)
103 (modify-syntax-entry ?\\ "." simula-mode-syntax-table)
104 (modify-syntax-entry ?\] "." simula-mode-syntax-table)
105 (modify-syntax-entry ?_ "w" simula-mode-syntax-table)
106 (modify-syntax-entry ?\| "." simula-mode-syntax-table)
107 (modify-syntax-entry ?\{ "." simula-mode-syntax-table)
108 (modify-syntax-entry ?\} "." simula-mode-syntax-table))
110 (defvar simula-mode-map ()
111 "Keymap used in simula mode.")
115 (setq simula-mode-map (make-sparse-keymap))
116 (define-key simula-mode-map "\C-c\C-u" 'simula-backward-up-level)
117 (define-key simula-mode-map "\C-c\C-p" 'simula-previous-statement)
118 (define-key simula-mode-map "\C-c\C-d" 'simula-forward-down-level)
119 (define-key simula-mode-map "\C-c\C-n" 'simula-next-statement)
120 ;(define-key simula-mode-map "\C-c\C-g" 'simula-goto-definition)
121 ;(define-key simula-mode-map "\C-c\C-h" 'simula-standard-help)
122 (define-key simula-mode-map "\177" 'backward-delete-char-untabify)
123 (define-key simula-mode-map ":" 'simula-electric-label)
124 (define-key simula-mode-map "\t" 'simula-indent-command))
126 (defvar simula-mode-abbrev-table nil
127 "Abbrev table in simula-mode buffers")
130 (defun simula-mode ()
131 "Major mode for editing SIMULA code.
133 Variables controlling indentation style:
134 simula-tab-always-indent
135 Non-nil means TAB in SIMULA mode should always reindent the current line,
136 regardless of where in the line point is when the TAB command is used.
138 Indentation of SIMULA statements with respect to containing block.
139 simula-substatement-offset
140 Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.
141 simula-continued-statement-offset 3
142 Extra indentation for lines not starting a statement or substatement,
143 e.g. a nested FOR-loop. If value is a list, each line in a multipple-
144 line continued statement will have the car of the list extra indentation
145 with respect to the previous line of the statement.
146 simula-label-offset -4711
147 Offset of SIMULA label lines relative to usual indentation
148 simula-if-indent '(0 . 0)
149 Extra indentation of THEN and ELSE with respect to the starting IF.
150 Value is a cons cell, the car is extra THEN indention and the cdr
151 extra ELSE indention. IF after ELSE is indented as the starting IF.
152 simula-inspect-indent '(0 . 0)
153 Extra indentation of WHEN and OTHERWISE with respect to the
154 corresponding INSPECT. Value is a cons cell, the car is
155 extra WHEN indention and the cdr extra OTHERWISE indention.
156 simula-electric-indent nil
157 If this variable non-nil value, simula-indent-line
158 will check the previous line to see if it has to be reindented.
159 simula-abbrev-keyword 'upcase
160 Determine how SIMULA keywords will be expanded. Value is one of
161 the symbols upcase, downcase, capitalize, (as in) abbrev-table or
162 nil if they should not be changed.
163 simula-abbrev-stdproc 'abbrev-table
164 Determine how standard SIMULA procedure and class names will be
165 expanded. Value is one of the symbols upcase, downcase, capitalize,
166 (as in) abbrev-table or nil if they should not be changed.
168 Turning on SIMULA mode calls the value of the variable simula-mode-hook
169 with no arguments, if that value is non-nil
171 Warning: simula-mode-hook should not read in an abbrev file without calling
172 the function simula-install-standard-abbrevs afterwards, preferrably not
175 (kill-all-local-variables)
176 (use-local-map simula-mode-map)
177 (setq major-mode 'simula-mode)
178 (setq mode-name "SIMULA")
179 (make-local-variable 'comment-column)
180 (setq comment-column 40)
181 (make-local-variable 'end-comment-column)
182 (setq end-comment-column 75)
183 (set-syntax-table simula-mode-syntax-table)
184 (make-local-variable 'paragraph-start)
185 (setq paragraph-start "^[ \t]*$\\|\\f")
186 (make-local-variable 'paragraph-separate)
187 (setq paragraph-separate paragraph-start)
188 (make-local-variable 'indent-line-function)
189 (setq indent-line-function 'simula-indent-line)
190 (make-local-variable 'require-final-newline)
191 (setq require-final-newline t)
192 (make-local-variable 'comment-start)
193 (setq comment-start "! ")
194 (make-local-variable 'comment-end)
195 (setq comment-end " ;")
196 (make-local-variable 'comment-start-skip)
197 (setq comment-start-skip "!+ *")
198 (make-local-variable 'parse-sexp-ignore-comments)
199 (setq parse-sexp-ignore-comments nil)
200 (make-local-variable 'comment-multi-line)
201 (setq comment-multi-line t)
202 (if simula-mode-abbrev-table
204 (if simula-abbrev-file
205 (read-abbrev-file simula-abbrev-file)
206 (define-abbrev-table 'simula-mode-abbrev-table ()))
207 (let (abbrevs-changed)
208 (simula-install-standard-abbrevs)))
209 (setq local-abbrev-table simula-mode-abbrev-table)
211 (run-hooks 'simula-mode-hook))
215 (defun simula-indent-line ()
216 "Indent this line as SIMULA code. If simula-electric-indent
217 is non-nil, indent previous line if necessary."
218 (let ((origin (- (point-max) (point)))
219 (indent (simula-calculate-indent))
220 (case-fold-search t))
224 ;; manually expand abbrev on last line, if any
228 ;; now maybe we should reindent that line
229 (if simula-electric-indent
232 (skip-chars-forward " \t\f")
235 "\\(end\\|if\\|then\\|else\\|when\\|otherwise\\)\\>")
236 (not (simula-context)))
238 (let ((post-indent (simula-calculate-indent)))
239 (if (eq (current-indentation) post-indent)
241 (delete-horizontal-space)
242 (indent-to post-indent)))))))
243 (goto-char (- (point-max) origin))
244 (if (eq (current-indentation) indent)
245 (back-to-indentation)
246 (delete-horizontal-space)
247 (indent-to indent)))))
250 (defun simula-indent-command (&optional whole-exp)
251 "Indent current line as SIMULA code, or insert TAB character.
252 If simula-tab-always-indent is non-nil, always indent current line.
253 Otherwise, indent only if point is before any non-whitespace
254 character on the line.
256 A numeric argument, regardless of its value, means indent rigidly
257 all the lines of the SIMULA statement after point so that this line
258 becomes properly indented.
259 The relative indentation among the lines of the statement are preserved."
261 (let ((case-fold-search t))
262 (if (or whole-exp simula-tab-always-indent
264 (skip-chars-backward " \t\f")
266 ;; reindent current line
267 (let ((indent (save-excursion
269 (simula-calculate-indent)))
270 (current (current-indentation))
271 (origin (- (point-max) (point)))
273 (skip-chars-backward " \t\f")
277 (if (eq current indent)
279 (skip-chars-backward " \t\f")
281 (back-to-indentation))
283 (delete-horizontal-space)
286 (goto-char (- (point-max) origin))))
287 (setq origin (point))
290 (beginning-of-line 2)
293 (simula-next-statement 1)
295 (if (and (> end beg) (not (eq indent current)))
296 (indent-code-rigidly beg end (- indent current) "%")))))
300 (defun simula-context ()
301 "Returns value according to position of point inside SIMULA text:
302 0 point inside COMMENT
303 1 point on SIMULA-compiler directive line
304 2 point inside END comment
305 3 point inside string
306 4 point inside character constant
308 ;; first, find out if this is a compiler directive line
311 (eq (following-char) ?%))
315 ;; The current line is NOT a compiler directive line.
316 ;; Now, the strategy is to search backward to find a semicolon
317 ;; that is NOT inside a string. The point after semicolon MUST be
318 ;; outside a comment, since semicolons are comment-ending and
319 ;; comments are non-recursive. We take advantage of the fact
320 ;; that strings MUST end on the same line as they started, so
321 ;; that we can easily decide whether we are inside a string or not.
322 (let (return-value (origin (point)))
323 (skip-chars-backward "^;" (point-min))
324 ;; found semicolon or beginning of buffer
325 (let (loopvalue (saved-point origin))
326 (while (and (not (bobp))
329 ;; compiler directive line? If so, cont searching..
330 (eq (following-char) ?%))
332 (while (< (point) saved-point)
333 (skip-chars-forward "^;\"'")
336 ((eq (preceding-char) ?\;)
337 (setq saved-point (point)))
338 ((eq (preceding-char) ?\")
339 (skip-chars-forward "^\";")
340 (if (eq (following-char) ?\;)
341 (setq saved-point (point) loopvalue t)
344 (if (eq (following-char) ?')
346 (skip-chars-forward "^';")
347 (if (eq (following-char) ?\;)
348 (setq saved-point (point) loopvalue t)
352 (skip-chars-backward "^;")
353 (setq saved-point (point) loopvalue nil)))
354 ;; Now we are CERTAIN that we are outside comments and strings.
355 ;; The job now is to search forward again towards the origin
356 ;; skipping directives, comments and strings correctly,
357 ;; so that we know what context we are in when we find the origin.
361 "\\<end\\>\\|!\\|\"\\|'\\|^%\\|\\<comment\\>" origin 'move))
363 ((memq (preceding-char) '(?d ?D))
364 (setq return-value 2)
365 (while (and (memq (preceding-char) '(?d ?D)) (not return-value))
366 (while (and (re-search-forward
367 ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\|^%"
369 (eq (preceding-char) ?%))
370 (beginning-of-line 2)))
371 (if (looking-at "[ \t\n\f]*\\(;\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\)")
372 (setq return-value nil)))
373 ((memq (preceding-char) '(?! ?t ?T))
375 (setq return-value 0)
376 (skip-chars-forward "^%;" origin)
377 (while (and return-value (< (point) origin))
378 (if (eq (following-char) ?\;)
379 (setq return-value nil)
381 (beginning-of-line 2) ; skip directive inside comment
382 (forward-char 1)) ; or single '%'
383 (skip-chars-forward "^%;" origin))))
384 ((eq (preceding-char) ?\")
385 (if (not (search-forward "\"" origin 'move))
386 (setq return-value 3)))
387 ((eq (preceding-char) ?\')
388 (if (or (eq (point) origin) (eobp))
389 (setq return-value 4)
391 (if (not (search-forward "'" origin 'move))
392 (setq return-value 4))))
393 ;; compiler directive line - skip
394 (t (beginning-of-line 2))))
399 (defun simula-electric-label ()
400 "If this is a label that starts the line, reindent the line"
404 (let ((origin (- (point-max) (point)))
406 ;; don't mix a label with an assignment operator := :-
407 ;; therefore look at next typed character...
408 (next-char (setq unread-command-events (list (read-event))))
409 (com-char last-command-char))
411 ;; Problem: find out if character just read is a command char
412 ;; that would insert something after ':' making it a label.
413 ;; At least \n, \r (and maybe \t) falls into this category.
414 ;; This is a real crock, it depends on traditional keymap
415 ;; bindings, that is, printing characters doing self-insert,
416 ;; and no other command sequence inserting '-' or '='.
417 ;; simula-electric-label can be easily fooled...
418 (if (and (not (memq next-char '(?= ?-)))
419 (or (memq next-char '(?\n ?\r))
420 (and (eq next-char ?\t)
421 simula-tab-always-indent)
422 (not (memq (following-char) '(?= ?-))))
423 (not (simula-context))
427 (skip-chars-backward " \t\f")
428 (skip-chars-backward "a-zA-Z0-9_")
429 (if (looking-at "virtual\\>")
431 (skip-chars-backward " \t\f")
433 (let ((amount (simula-calculate-indent)))
434 (delete-horizontal-space)
436 (goto-char (- (point-max) origin)))))
439 (defun simula-backward-up-level (count)
440 "Move backward up COUNT block levels.
441 If COUNT is negative, move forward up block level instead"
443 (let ((origin (point))
444 (case-fold-search t))
448 (re-search-backward "\\<begin\\>\\|\\<end\\>")
449 (if (not (simula-context))
450 (setq count (if (memq (following-char) '(?b ?B))
454 (re-search-forward "\\<begin\\>\\|\\<end\\>")
456 (if (not (simula-context))
457 (setq count (if (memq (following-char) '(?e ?E))
461 ;; If block level not found, jump back to origin and signal an error
464 (error "No higher block level")))
467 (signal 'quit nil))))))
470 (defun simula-forward-down-level (count)
471 "Move forward down COUNT block levels.
472 If COUNT is negative, move backward down block level instead"
474 ;; When we search for a deeper block level, we must never
475 ;; get out of the block where we started -> count >= start-count
476 (let ((start-count count)
478 (case-fold-search t))
482 (re-search-backward "\\<begin\\>\\|\\<end\\>")
483 (if (not (simula-context))
484 (setq count (if (memq (following-char) '(?e ?E))
487 (if (< count start-count) (signal 'error nil)))
489 (re-search-forward "\\<begin\\>\\|\\<end\\>")
491 (if (not (simula-context))
492 (setq count (if (memq (following-char) '(?b ?B))
496 ;; deeper level has to be found within starting block
497 (if (> count start-count) (signal 'error nil))))
498 ;; If block level not found, jump back to origin and signal an error
501 (error "No containing block level")))
504 (signal 'quit nil))))))
507 (defun simula-previous-statement (count)
508 "Move backward COUNT statements.
509 If COUNT is negative, move forward instead (simula-next-statement)"
512 (simula-next-statement (- count))
518 (simula-skip-comment-backward)
519 (if (memq (preceding-char) '(?n ?N))
522 (if (not (looking-at "\\<begin\\>"))
524 (if (eq (preceding-char) ?\;)
526 (while (and (natnump (setq count (1- count)))
527 (setq status (simula-search-backward
528 ";\\|\\<begin\\>" nil 'move))))
531 (if (eq (following-char) ?\;)
533 (backward-word -1))))
534 (simula-skip-comment-forward))
535 (error (progn (goto-char origin)
536 (error "Incomplete statement (too many ENDs)")))
537 (quit (progn (goto-char origin) (signal 'quit nil)))))))
540 (defun simula-next-statement (count)
541 "Move backward COUNT statements.
542 If COUNT is negative, move forward instead (simula-next-statement)"
545 (simula-previous-statement (- count))
551 (simula-skip-comment-forward)
552 (if (looking-at "\\<end\\>") (forward-word 1))
553 (while (and (natnump (setq count (1- count)))
554 (setq status (simula-search-forward
555 ";\\|\\<end\\>" (point-max) 'move))))
556 (if (and status (/= (preceding-char) ?\;))
559 (simula-skip-comment-backward))))
560 (error (progn (goto-char origin)
561 (error "Incomplete statement (too few ENDs)")))
562 (quit (progn (goto-char origin) (signal 'quit nil)))))))
565 (defun simula-skip-comment-backward ()
566 "Search towards bob to find first char that is outside a comment"
571 (skip-chars-backward " \t\n\f")
572 (if (eq (preceding-char) ?\;)
575 (setq context (simula-context)))
576 (setq context (simula-context)))
578 ((memq context '(nil 3 4))
579 ;; check to see if we found a label
580 (if (and (eq (preceding-char) ?:)
581 (not (memq (following-char) '(?- ?=)))
583 (skip-chars-backward ": \t\fa-zA-Z0-9_")
584 (not (looking-at "virtual\\>"))))
585 (skip-chars-backward ": \t\fa-zA-Z0-9_")
586 (throw 'simula-out nil)))
588 ;; since we are inside a comment, it must start somewhere!
589 (while (and (re-search-backward "!\\|\\<comment\\>")
590 (memq (simula-context) '(0 1)))))
594 (throw 'simula-out nil)))
596 ;; an END-comment must belong to an END
597 (re-search-backward "\\<end\\>")
599 (throw 'simula-out nil))
600 ;; should be impossible to get here..
604 (defun simula-skip-comment-forward ()
605 "Search towards eob to find first char that is outside a comment"
606 ;; this function assumes we start with point .outside a comment
610 (skip-chars-forward " \t\n\f")
612 ((looking-at "!\\|\\<comment\\>")
613 (search-forward ";" nil 'move))
614 ((and (bolp) (eq (following-char) ?%))
615 (beginning-of-line 2))
616 ((and (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]")
617 (not (looking-at "virtual\\>")))
618 (skip-chars-forward "a-zA-Z0-9_ \t\f:"))
620 (throw 'simula-out t))))))
623 (defun simula-forward-up-level ()
624 (let ((continue-loop t)
630 (if (re-search-backward "\\<begin\\>\\|\\<end\\>" (point-min) 'move)
631 (setq temp (simula-context)
632 return-value (and (memq (preceding-char) '(?d ?D))
633 (memq temp '(nil 2)))
634 continue-loop (and (not return-value)
635 (simula-forward-up-level)))
636 (setq continue-loop nil)))
643 (defun simula-calculate-indent ()
645 (let ((where (simula-context))
657 ;; If comment started on previous non-blank line, indent to the
658 ;; column where the comment started, else indent as that line.
660 (skip-chars-backward " \t\n\f")
661 (while (and (not (bolp)) (eq (simula-context) 0))
662 (re-search-backward "^\\|!\\|\\<comment\\>"))
663 (skip-chars-forward " \t\n\f")
668 ;; Detect missing string delimiters
671 (error "Inside string"))
673 (error "Inside character constant"))
675 ;; check to see if inside ()'s
677 ((setq temp (simula-inside-parens))
680 ;; Calculate non-comment indentation
682 ;; first, find out if this line starts with something that needs
683 ;; special indention (END/IF/THEN/ELSE/WHEN/OTHERWISE or label)
685 (skip-chars-forward " \t\f")
690 ((looking-at "end\\>")
691 (setq indent (- simula-indent-level)
696 ((looking-at "if\\>\\|then\\>\\|else\\>")
697 ;; search for the *starting* IF
699 ((memq (following-char) '(?T ?t))
700 (setq indent (car simula-if-indent)))
701 ((memq (following-char) '(?E ?e))
702 (setq indent (cdr simula-if-indent)))
710 ((looking-at "when\\>\\|otherwise\\>")
711 ;; search for corresponding INSPECT
712 (if (memq (following-char) '(?W ?w))
713 (setq indent (car simula-inspect-indent))
714 (setq indent (cdr simula-inspect-indent)))
715 (simula-find-inspect))
719 ((and (not (looking-at "virtual\\>"))
720 (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]"))
721 (setq indent simula-label-offset)))
722 ;; find line with non-comment text
723 (simula-skip-comment-backward)
725 (not (eq (preceding-char) ?\;))
726 (if (memq (preceding-char) '(?N ?n))
729 (not (looking-at "begin\\>")))
732 (simula-previous-statement 1)
733 (simula-skip-comment-backward)))
735 (save-excursion (beginning-of-line) (point))
736 ;; - perhaps this is a continued statement
741 (if (eq (char-syntax (preceding-char)) ?w)
745 "begin\\|then\\|else\\|when\\|otherwise\\|do"
747 (not (memq (preceding-char) '(?: ?\;)))))))
749 ;; MAIN calculation loop - count BEGIN/DO etc.
752 (if (re-search-backward
753 ";\\|\\<\\(begin\\|end\\|if\\|else\\|then\\|when\\|otherwise\\|do\\)\\>"
756 ();; found something in a comment/string - ignore
757 (setq temp (following-char))
760 (simula-previous-statement 1))
761 ((looking-at "begin\\>")
762 (setq indent (+ indent simula-indent-level)))
763 ((looking-at "end\\>")
765 (simula-previous-statement 1))
766 ((looking-at "do\\>")
767 (setq indent (+ indent simula-substatement-offset))
768 (simula-find-do-match))
769 ((looking-at "\\(if\\|then\\|else\\)\\>")
770 (if (memq temp '(?I ?i))
772 (setq indent (+ indent
773 simula-substatement-offset
774 (if (memq temp '(?T ?t))
775 (car simula-if-indent)
776 (cdr simula-if-indent)))))
778 ((looking-at "\\<when\\>\\|\\<otherwise\\>")
779 (setq indent (+ indent
780 simula-substatement-offset
781 (if (memq temp '(?W ?w))
782 (car simula-if-indent)
783 (cdr simula-if-indent))))
784 (simula-find-inspect)))
785 ;; found the start of a [sub]statement
786 ;; add indention for continued statement
790 (if (listp simula-continued-statement-offset)
791 (car simula-continued-statement-offset)
792 simula-continued-statement-offset))))
794 (save-excursion (beginning-of-line) (point))
796 ;; search failed .. point is at beginning of line
797 ;; determine if we should continue searching
798 ;; (at or before comment or label)
799 ;; temp = t means finished
801 (and (not (simula-context))
803 (skip-chars-forward " \t\f")
804 (or (looking-at "virtual")
807 "!\\|comment\\>\\|[a-z0-9_]*[ \t\f]*:[^-=]")))))
809 ;; if we are finished, find current line's indentation
811 (setq indent (+ indent (current-indentation))))
812 ;; find next line with non-comment SIMULA text
813 ;; maybe indent extra if statement continues
814 (simula-skip-comment-backward)
817 (if (eq (char-syntax (preceding-char)) ?w)
821 "begin\\|then\\|else\\|when\\|otherwise\\|do")))
822 (not (memq (preceding-char) '(?: ?\;))))))
823 ;; if we the state of the continued-variable
824 ;; changed, add indention for continued statement
825 (if (or (and prev-cont (not continued))
827 (listp simula-continued-statement-offset)))
830 (if (listp simula-continued-statement-offset)
831 (car simula-continued-statement-offset)
832 simula-continued-statement-offset))))
833 ;; while ends if point is at beginning of line at loop test
835 (setq start-line (save-excursion (beginning-of-line) (point)))
836 (beginning-of-line))))
843 (defun simula-find-if ()
844 "Find starting IF of a IF-THEN[-ELSE[-IF-THEN...]] statement"
847 (if (and (simula-search-backward "\\<if\\>\\|;\\|\\<begin\\>"nil t)
848 (memq (following-char) '(?I ?i)))
851 ;; find out if this IF was really the start of the IF statement
853 (simula-skip-comment-backward)
854 (if (and (eq (char-syntax (preceding-char)) ?w)
857 (looking-at "else\\>")))
859 (throw 'simula-out t)))
860 (if (not (looking-at "\\<if\\>"))
861 (error "Missing IF or misplaced BEGIN or ';' (can't find IF)")
863 ;; we were at the starting IF in the first place..
865 (throw 'simula-out t))))))
868 (defun simula-find-inspect ()
869 "Find INSPECT matching WHEN or OTHERWISE"
873 ;; INSPECTs can be nested, have to find the corresponding one
876 (if (and (simula-search-backward "\\<inspect\\>\\|\\<otherwise\\>\\|;"
878 (/= (following-char) ?\;))
879 (if (memq (following-char) '(?O ?o))
880 (setq level (1+ level))
882 (throw 'simula-out t)
883 (setq level (1- level))))
884 (error "Missing INSPECT or misplaced ';' (can't find INSPECT)"))))))
887 (defun simula-find-do-match ()
888 "Find keyword matching DO: FOR, WHILE, INSPECT or WHEN"
889 (while (and (re-search-backward
890 "\\<\\(do\\|for\\|while\\|inspect\\|when\\|end\\|begin\\)\\>\\|;"
893 (if (and (looking-at "\\<\\(for\\|while\\|inspect\\|when\\)\\>")
894 (not (simula-context)))
896 (error "No matching FOR, WHILE or INSPECT for DO, or misplaced ';'")))
899 (defun simula-inside-parens ()
900 "Return position after '(' on line if inside parentheses, nil otherwise."
905 (if (re-search-backward "(\\|)\\|;" nil t)
906 (if (eq (simula-context) nil)
907 ;; found something - check it out
909 ((eq (following-char) ?\;)
911 (throw 'simula-out nil)
912 (error "Parethesis mismatch or misplaced ';'")))
913 ((eq (following-char) ?\()
915 (throw 'simula-out (1+ (current-column)))
916 (setq parlevel (1- parlevel))))
917 (t (setq parlevel (1+ parlevel))))
918 );; nothing - inside comment or string
920 (throw 'simula-out nil)))))))
923 (defun simula-goto-definition ()
924 "Goto point of definition of variable, procedure or class."
928 (defun simula-expand-stdproc ()
929 (if (or (not simula-abbrev-stdproc) (simula-context))
932 ((eq simula-abbrev-stdproc 'upcase) (upcase-word -1))
933 ((eq simula-abbrev-stdproc 'downcase) (downcase-word -1))
934 ((eq simula-abbrev-stdproc 'capitalize) (capitalize-word -1)))))
937 (defun simula-expand-keyword ()
938 (if (or (not simula-abbrev-keyword) (simula-context))
941 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
942 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
943 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))))
946 (defun simula-electric-keyword ()
947 "Expand SIMULA keyword. If it starts the line, reindent."
949 (let ((show-char (eq this-command 'self-insert-command)))
950 ;; If the abbrev expansion results in reindention, the user may have
951 ;; to wait some time before the character he typed is displayed
952 ;; (the char causing the expansion is inserted AFTER the hook function
953 ;; is called). This is annoying in case of normal characters.
954 ;; However, if the user pressed a key bound to newline, it is better
955 ;; to have the line inserted after the begin-end match.
958 (insert-char last-command-char 1)
961 (if (let ((where (simula-context))
962 (case-fold-search t))
964 (if (and (eq where 2) (eq (char-syntax (preceding-char)) ?w))
967 (not (looking-at "end\\>"))))))
970 ((not simula-abbrev-keyword) (unexpand-abbrev))
971 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
972 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
973 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))
974 (let ((pos (- (point-max) (point)))
979 ;; check if the expanded word is on the beginning of the line.
980 (if (and (eq (char-syntax (preceding-char)) ?w)
983 (if (looking-at "end\\>")
985 (simula-backward-up-level 1)
986 (if (pos-visible-in-window-p)
992 (+ (point) (window-width))))))))
993 (skip-chars-backward " \t\f")
995 (let ((indent (simula-calculate-indent)))
996 (if (eq indent (current-indentation))
998 (delete-horizontal-space)
1000 (skip-chars-forward " \t\f"))
1001 ;; check for END - blow whistles and ring bells
1003 (goto-char (- (point-max) pos))
1006 (quit (goto-char (- (point-max) pos))))))))
1009 (defun simula-search-backward (string &optional limit move)
1010 (setq string (concat string "\\|\\<end\\>"))
1013 (while (re-search-backward string limit move)
1014 (if (simula-context)
1016 (if (looking-at "\\<end\\>")
1019 (while (natnump level)
1020 (re-search-backward "\\<begin\\>\\|\\<end\\>")
1021 (if (simula-context)
1023 (setq level (if (memq (following-char) '(?b ?B))
1026 (throw 'simula-out t)))))))
1029 (defun simula-search-forward (string &optional limit move)
1030 (setq string (concat string "\\|\\<begin\\>"))
1033 (while (re-search-forward string limit move)
1034 (goto-char (match-beginning 0))
1035 (if (simula-context)
1036 (goto-char (1- (match-end 0)))
1037 (if (looking-at "\\<begin\\>")
1039 (goto-char (1- (match-end 0)))
1041 (while (natnump level)
1042 (re-search-forward "\\<begin\\>\\|\\<end\\>")
1044 (if (not (simula-context))
1045 (setq level (if (memq (following-char) '(?e ?E))
1048 (backward-word -1)))
1049 (goto-char (1- (match-end 0)))
1050 (throw 'exit t)))))))
1053 (defun simula-install-standard-abbrevs ()
1054 "Define Simula keywords, standard procedures and classes in
1055 local abbrev table."
1056 ;; procedure and class names are as of the SIMULA 87 standard.
1058 (mapcar (function (lambda (args)
1059 (apply 'define-abbrev simula-mode-abbrev-table args)))
1060 '(("abs" "Abs" simula-expand-stdproc)
1061 ("accum" "Accum" simula-expand-stdproc)
1062 ("activate" "ACTIVATE" simula-expand-keyword)
1063 ("addepsilon" "AddEpsilon" simula-expand-stdproc)
1064 ("after" "AFTER" simula-expand-keyword)
1065 ("and" "AND" simula-expand-keyword)
1066 ("arccos" "ArcCos" simula-expand-stdproc)
1067 ("arcsin" "ArcSin" simula-expand-stdproc)
1068 ("arctan" "ArcTan" simula-expand-stdproc)
1069 ("arctan2" "ArcTan2" simula-expand-stdproc)
1070 ("array" "ARRAY" simula-expand-keyword)
1071 ("at" "AT" simula-expand-keyword)
1072 ("before" "BEFORE" simula-expand-keyword)
1073 ("begin" "BEGIN" simula-expand-keyword)
1074 ("blanks" "Blanks" simula-expand-stdproc)
1075 ("boolean" "BOOLEAN" simula-expand-keyword)
1076 ("breakoutimage" "BreakOutImage" simula-expand-stdproc)
1077 ("bytefile" "ByteFile" simula-expand-stdproc)
1078 ("call" "Call" simula-expand-stdproc)
1079 ("cancel" "Cancel" simula-expand-stdproc)
1080 ("cardinal" "Cardinal" simula-expand-stdproc)
1081 ("char" "Char" simula-expand-stdproc)
1082 ("character" "CHARACTER" simula-expand-keyword)
1083 ("checkpoint" "CheckPoint" simula-expand-stdproc)
1084 ("class" "CLASS" simula-expand-keyword)
1085 ("clear" "Clear" simula-expand-stdproc)
1086 ("clocktime" "ClockTime" simula-expand-stdproc)
1087 ("close" "Close" simula-expand-stdproc)
1088 ("comment" "COMMENT" simula-expand-keyword)
1089 ("constant" "Constant" simula-expand-stdproc)
1090 ("copy" "Copy" simula-expand-stdproc)
1091 ("cos" "Cos" simula-expand-stdproc)
1092 ("cosh" "CosH" simula-expand-stdproc)
1093 ("cotan" "CoTan" simula-expand-stdproc)
1094 ("cputime" "CpuTime" simula-expand-stdproc)
1095 ("current" "Current" simula-expand-stdproc)
1096 ("datetime" "DateTime" simula-expand-stdproc)
1097 ("decimalmark" "DecimalMark" simula-expand-stdproc)
1098 ("delay" "DELAY" simula-expand-keyword)
1099 ("deleteimage" "DeleteImage" simula-expand-stdproc)
1100 ("detach" "Detach" simula-expand-stdproc)
1101 ("digit" "Digit" simula-expand-stdproc)
1102 ("directbytefile" "DirectByteFile" simula-expand-stdproc)
1103 ("directfile" "DirectFile" simula-expand-stdproc)
1104 ("discrete" "Discrete" simula-expand-stdproc)
1105 ("do" "DO" simula-expand-keyword)
1106 ("downcase" "Downcase" simula-expand-stdproc)
1107 ("draw" "Draw" simula-expand-stdproc)
1108 ("eject" "Eject" simula-expand-stdproc)
1109 ("else" "ELSE" simula-electric-keyword)
1110 ("empty" "Empty" simula-expand-stdproc)
1111 ("end" "END" simula-electric-keyword)
1112 ("endfile" "Endfile" simula-expand-stdproc)
1113 ("entier" "Entier" simula-expand-stdproc)
1114 ("eq" "EQ" simula-expand-keyword)
1115 ("eqv" "EQV" simula-expand-keyword)
1116 ("erlang" "Erlang" simula-expand-stdproc)
1117 ("error" "Error" simula-expand-stdproc)
1118 ("evtime" "EvTime" simula-expand-stdproc)
1119 ("exp" "Exp" simula-expand-stdproc)
1120 ("external" "EXTERNAL" simula-expand-keyword)
1121 ("false" "FALSE" simula-expand-keyword)
1122 ("field" "Field" simula-expand-stdproc)
1123 ("file" "File" simula-expand-stdproc)
1124 ("first" "First" simula-expand-stdproc)
1125 ("follow" "Follow" simula-expand-stdproc)
1126 ("for" "FOR" simula-expand-keyword)
1127 ("ge" "GE" simula-expand-keyword)
1128 ("getchar" "GetChar" simula-expand-stdproc)
1129 ("getfrac" "GetFrac" simula-expand-stdproc)
1130 ("getint" "GetInt" simula-expand-stdproc)
1131 ("getreal" "GetReal" simula-expand-stdproc)
1132 ("go" "GO" simula-expand-keyword)
1133 ("goto" "GOTO" simula-expand-keyword)
1134 ("gt" "GT" simula-expand-keyword)
1135 ("head" "Head" simula-expand-stdproc)
1136 ("hidden" "HIDDEN" simula-expand-keyword)
1137 ("histd" "HistD" simula-expand-stdproc)
1138 ("histo" "Histo" simula-expand-stdproc)
1139 ("hold" "Hold" simula-expand-stdproc)
1140 ("idle" "Idle" simula-expand-stdproc)
1141 ("if" "IF" simula-expand-keyword)
1142 ("image" "Image" simula-expand-stdproc)
1143 ("imagefile" "ImageFile" simula-expand-stdproc)
1144 ("imp" "IMP" simula-expand-keyword)
1145 ("in" "IN" simula-expand-keyword)
1146 ("inbyte" "InByte" simula-expand-stdproc)
1147 ("inbytefile" "InByteFile" simula-expand-stdproc)
1148 ("inchar" "InChar" simula-expand-stdproc)
1149 ("infile" "InFile" simula-expand-stdproc)
1150 ("infrac" "InFrac" simula-expand-stdproc)
1151 ("inimage" "InImage" simula-expand-stdproc)
1152 ("inint" "InInt" simula-expand-stdproc)
1153 ("inner" "INNER" simula-expand-keyword)
1154 ("inreal" "InReal" simula-expand-stdproc)
1155 ("inrecord" "InRecord" simula-expand-stdproc)
1156 ("inspect" "INSPECT" simula-expand-keyword)
1157 ("integer" "INTEGER" simula-expand-keyword)
1158 ("intext" "InText" simula-expand-stdproc)
1159 ("into" "Into" simula-expand-stdproc)
1160 ("is" "IS" simula-expand-keyword)
1161 ("isochar" "ISOChar" simula-expand-stdproc)
1162 ("isopen" "IsOpen" simula-expand-stdproc)
1163 ("isorank" "ISORank" simula-expand-stdproc)
1164 ("label" "LABEL" simula-expand-keyword)
1165 ("last" "Last" simula-expand-stdproc)
1166 ("lastitem" "LastItem" simula-expand-stdproc)
1167 ("lastloc" "LastLoc" simula-expand-stdproc)
1168 ("le" "LE" simula-expand-keyword)
1169 ("length" "Length" simula-expand-stdproc)
1170 ("letter" "Letter" simula-expand-stdproc)
1171 ("line" "Line" simula-expand-stdproc)
1172 ("linear" "Linear" simula-expand-stdproc)
1173 ("linesperpage" "LinesPerPage" simula-expand-stdproc)
1174 ("link" "Link" simula-expand-stdproc)
1175 ("linkage" "Linkage" simula-expand-stdproc)
1176 ("ln" "Ln" simula-expand-stdproc)
1177 ("locate" "Locate" simula-expand-stdproc)
1178 ("location" "Location" simula-expand-stdproc)
1179 ("lock" "Lock" simula-expand-stdproc)
1180 ("locked" "Locked" simula-expand-stdproc)
1181 ("log10" "Log10" simula-expand-stdproc)
1182 ("long" "LONG" simula-expand-keyword)
1183 ("lowcase" "LowCase" simula-expand-stdproc)
1184 ("lowerbound" "LowerBound" simula-expand-stdproc)
1185 ("lowten" "LowTen" simula-expand-stdproc)
1186 ("lt" "LT" simula-expand-keyword)
1187 ("main" "Main" simula-expand-stdproc)
1188 ("max" "Max" simula-expand-stdproc)
1189 ("maxint" "MaxInt" simula-expand-stdproc)
1190 ("maxlongreal" "MaxLongReal" simula-expand-stdproc)
1191 ("maxloc" "MaxLoc" simula-expand-stdproc)
1192 ("maxrank" "MaxRank" simula-expand-stdproc)
1193 ("maxreal" "MaxReal" simula-expand-stdproc)
1194 ("min" "Min" simula-expand-stdproc)
1195 ("minint" "MinInt" simula-expand-stdproc)
1196 ("minlongreal" "MinLongReal" simula-expand-stdproc)
1197 ("minrank" "MinRank" simula-expand-stdproc)
1198 ("minreal" "MinReal" simula-expand-stdproc)
1199 ("mod" "Mod" simula-expand-stdproc)
1200 ("more" "More" simula-expand-stdproc)
1201 ("name" "NAME" simula-expand-keyword)
1202 ("ne" "NE" simula-expand-keyword)
1203 ("negexp" "NegExp" simula-expand-stdproc)
1204 ("new" "NEW" simula-expand-keyword)
1205 ("nextev" "NextEv" simula-expand-stdproc)
1206 ("none" "NONE" simula-expand-keyword)
1207 ("normal" "Normal" simula-expand-stdproc)
1208 ("not" "NOT" simula-expand-keyword)
1209 ("notext" "NOTEXT" simula-expand-keyword)
1210 ("open" "Open" simula-expand-stdproc)
1211 ("or" "OR" simula-expand-keyword)
1212 ("otherwise" "OTHERWISE" simula-electric-keyword)
1213 ("out" "Out" simula-expand-stdproc)
1214 ("outbyte" "OutByte" simula-expand-stdproc)
1215 ("outbytefile" "OutByteFile" simula-expand-stdproc)
1216 ("outchar" "OutChar" simula-expand-stdproc)
1217 ("outfile" "OutFile" simula-expand-stdproc)
1218 ("outfix" "OutFix" simula-expand-stdproc)
1219 ("outfrac" "OutFrac" simula-expand-stdproc)
1220 ("outimage" "OutImage" simula-expand-stdproc)
1221 ("outint" "OutInt" simula-expand-stdproc)
1222 ("outreal" "OutReal" simula-expand-stdproc)
1223 ("outrecord" "OutRecord" simula-expand-stdproc)
1224 ("outtext" "OutText" simula-expand-stdproc)
1225 ("page" "Page" simula-expand-stdproc)
1226 ("passivate" "Passivate" simula-expand-stdproc)
1227 ("poisson" "Poisson" simula-expand-stdproc)
1228 ("pos" "Pos" simula-expand-stdproc)
1229 ("precede" "Precede" simula-expand-stdproc)
1230 ("pred" "Pred" simula-expand-stdproc)
1231 ("prev" "Prev" simula-expand-stdproc)
1232 ("printfile" "PrintFile" simula-expand-stdproc)
1233 ("prior" "PRIOR" simula-expand-keyword)
1234 ("procedure" "PROCEDURE" simula-expand-keyword)
1235 ("process" "Process" simula-expand-stdproc)
1236 ("protected" "PROTECTED" simula-expand-keyword)
1237 ("putchar" "PutChar" simula-expand-stdproc)
1238 ("putfix" "PutFix" simula-expand-stdproc)
1239 ("putfrac" "PutFrac" simula-expand-stdproc)
1240 ("putint" "PutInt" simula-expand-stdproc)
1241 ("putreal" "PutReal" simula-expand-stdproc)
1242 ("qua" "QUA" simula-expand-keyword)
1243 ("randint" "RandInt" simula-expand-stdproc)
1244 ("rank" "Rank" simula-expand-stdproc)
1245 ("reactivate" "REACTIVATE" simula-expand-keyword)
1246 ("real" "REAL" simula-expand-keyword)
1247 ("ref" "REF" simula-expand-keyword)
1248 ("resume" "Resume" simula-expand-stdproc)
1249 ("setaccess" "SetAccess" simula-expand-stdproc)
1250 ("setpos" "SetPos" simula-expand-stdproc)
1251 ("short" "SHORT" simula-expand-keyword)
1252 ("sign" "Sign" simula-expand-stdproc)
1253 ("simset" "SimSet" simula-expand-stdproc)
1254 ("simulaid" "SimulaId" simula-expand-stdproc)
1255 ("simulation" "Simulation" simula-expand-stdproc)
1256 ("sin" "Sin" simula-expand-stdproc)
1257 ("sinh" "SinH" simula-expand-stdproc)
1258 ("sourceline" "SourceLine" simula-expand-stdproc)
1259 ("spacing" "Spacing" simula-expand-stdproc)
1260 ("sqrt" "Sqrt" simula-expand-stdproc)
1261 ("start" "Start" simula-expand-stdproc)
1262 ("step" "STEP" simula-expand-keyword)
1263 ("strip" "Strip" simula-expand-stdproc)
1264 ("sub" "Sub" simula-expand-stdproc)
1265 ("subepsilon" "SubEpsilon" simula-expand-stdproc)
1266 ("suc" "Suc" simula-expand-stdproc)
1267 ("switch" "SWITCH" simula-expand-keyword)
1268 ("sysin" "SysIn" simula-expand-stdproc)
1269 ("sysout" "SysOut" simula-expand-stdproc)
1270 ("tan" "Tan" simula-expand-stdproc)
1271 ("tanh" "TanH" simula-expand-stdproc)
1272 ("terminate_program" "Terminate_Program" simula-expand-stdproc)
1273 ("terminated" "Terminated" simula-expand-stdproc)
1274 ("text" "TEXT" simula-expand-keyword)
1275 ("then" "THEN" simula-electric-keyword)
1276 ("this" "THIS" simula-expand-keyword)
1277 ("time" "Time" simula-expand-stdproc)
1278 ("to" "TO" simula-expand-keyword)
1279 ("true" "TRUE" simula-expand-keyword)
1280 ("uniform" "Uniform" simula-expand-stdproc)
1281 ("unlock" "Unlock" simula-expand-stdproc)
1282 ("until" "UNTIL" simula-expand-keyword)
1283 ("upcase" "Upcase" simula-expand-stdproc)
1284 ("upperbound" "UpperBound" simula-expand-stdproc)
1285 ("value" "VALUE" simula-expand-keyword)
1286 ("virtual" "VIRTUAL" simula-expand-keyword)
1287 ("wait" "Wait" simula-expand-stdproc)
1288 ("when" "WHEN" simula-electric-keyword)
1289 ("while" "WHILE" simula-expand-keyword))))
1291 ;;; simula.el ends here