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 XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Synched up with: FSF 19.30.
32 ;; A major mode for editing the Simula language. It knows about Simula
33 ;; syntax and standard indentation commands. It also provides convenient
34 ;; abbrevs for Simula keywords.
36 ;; Hans Henrik Eriksen (the author) may be reached at:
37 ;; Institutt for informatikk,
38 ;; Universitetet i Oslo
42 (provide 'simula-mode)
45 "SIMULA 87 code editing commands for Emacs."
49 (defcustom simula-tab-always-indent nil
50 "*Non-nil means TAB in SIMULA mode should always reindent the current line.
51 Otherwise TAB indents only when point is within
52 the run of whitespace at the beginning of the line."
56 (defcustom simula-indent-level 3
57 "*Indentation of SIMULA statements with respect to containing block."
61 (defcustom simula-substatement-offset 3
62 "*Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE."
66 (defcustom simula-continued-statement-offset 3
67 "*Extra indentation for lines not starting a statement or substatement.
68 If value is a list, each line in a multipleline continued statement
69 will have the car of the list extra indentation with respect to
70 the previous line of the statement."
74 (defcustom simula-label-offset -4711
75 "*Offset of SIMULA label lines relative to usual indentation."
79 (defcustom simula-if-indent '(0 . 0)
80 "*Extra indentation of THEN and ELSE with respect to the starting IF.
81 Value is a cons cell, the car is extra THEN indentation and the cdr
82 extra ELSE indentation. IF after ELSE is indented as the starting IF."
83 :type '(cons (integer :tag "THEN")
84 (integer :tag "ELSE"))
87 (defcustom simula-inspect-indent '(0 . 0)
88 "*Extra indentation of WHEN and OTHERWISE with respect to the INSPECT.
89 Value is a cons cell, the car is extra WHEN indentation
90 and the cdr extra OTHERWISE indentation."
91 :type '(cons (integer :tag "WHEN")
92 (integer :tag "OTHERWISE"))
95 (defcustom simula-electric-indent nil
96 "*Non-nil means `simula-indent-line' function may reindent previous line."
100 (defcustom simula-abbrev-keyword 'upcase
101 "*Specify how to convert case for SIMULA keywords.
102 Value is one of the symbols `upcase', `downcase', `capitalize',
103 \(as in) `abbrev-table' or nil if they should not be changed."
104 :type '(choice (const upcase) (const downcase) (const capitalize)
105 (const abbrev-table) (const nil))
108 (defcustom simula-abbrev-stdproc 'abbrev-table
109 "*Specify how to convert case for standard SIMULA procedure and class names.
110 Value is one of the symbols `upcase', `downcase', `capitalize',
111 \(as in) `abbrev-table', or nil if they should not be changed."
112 :type '(choice (const upcase) (const downcase) (const capitalize)
113 (const abbrev-table) (const nil))
116 (defcustom simula-abbrev-file nil
117 "*File with extra abbrev definitions for use in SIMULA mode.
118 These are used together with the standard abbrev definitions for SIMULA.
119 Please note that the standard definitions are required
120 for SIMULA mode to function correctly."
121 :type '(choice (const :tag "None")
125 (defvar simula-mode-syntax-table nil
126 "Syntax table in SIMULA mode buffers.")
128 (if simula-mode-syntax-table
130 (setq simula-mode-syntax-table (copy-syntax-table nil))
131 (modify-syntax-entry ?! "<" simula-mode-syntax-table)
132 (modify-syntax-entry ?$ "." simula-mode-syntax-table)
133 (modify-syntax-entry ?% "." simula-mode-syntax-table)
134 (modify-syntax-entry ?' "\"" simula-mode-syntax-table)
135 (modify-syntax-entry ?\( "()" simula-mode-syntax-table)
136 (modify-syntax-entry ?\) ")(" simula-mode-syntax-table)
137 (modify-syntax-entry ?\; ">" simula-mode-syntax-table)
138 (modify-syntax-entry ?\[ "." simula-mode-syntax-table)
139 (modify-syntax-entry ?\\ "." simula-mode-syntax-table)
140 (modify-syntax-entry ?\] "." simula-mode-syntax-table)
141 (modify-syntax-entry ?_ "w" simula-mode-syntax-table)
142 (modify-syntax-entry ?\| "." simula-mode-syntax-table)
143 (modify-syntax-entry ?\{ "." simula-mode-syntax-table)
144 (modify-syntax-entry ?\} "." simula-mode-syntax-table))
146 (defvar simula-mode-map ()
147 "Keymap used in SIMULA mode.")
151 (setq simula-mode-map (make-sparse-keymap))
152 (define-key simula-mode-map "\C-c\C-u" 'simula-backward-up-level)
153 (define-key simula-mode-map "\C-c\C-p" 'simula-previous-statement)
154 (define-key simula-mode-map "\C-c\C-d" 'simula-forward-down-level)
155 (define-key simula-mode-map "\C-c\C-n" 'simula-next-statement)
156 ;(define-key simula-mode-map "\C-c\C-g" 'simula-goto-definition)
157 ;(define-key simula-mode-map "\C-c\C-h" 'simula-standard-help)
158 (define-key simula-mode-map ":" 'simula-electric-label)
159 (define-key simula-mode-map "\t" 'simula-indent-command))
161 (defvar simula-mode-abbrev-table nil
162 "Abbrev table in SIMULA mode buffers")
165 (defun simula-mode ()
166 "Major mode for editing SIMULA code.
168 Variables controlling indentation style:
169 simula-tab-always-indent
170 Non-nil means TAB in SIMULA mode should always reindent the current line,
171 regardless of where in the line point is when the TAB command is used.
173 Indentation of SIMULA statements with respect to containing block.
174 simula-substatement-offset
175 Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.
176 simula-continued-statement-offset 3
177 Extra indentation for lines not starting a statement or substatement,
178 e.g. a nested FOR-loop. If value is a list, each line in a multiple-
179 line continued statement will have the car of the list extra indentation
180 with respect to the previous line of the statement.
181 simula-label-offset -4711
182 Offset of SIMULA label lines relative to usual indentation.
183 simula-if-indent '(0 . 0)
184 Extra indentation of THEN and ELSE with respect to the starting IF.
185 Value is a cons cell, the car is extra THEN indentation and the cdr
186 extra ELSE indentation. IF after ELSE is indented as the starting IF.
187 simula-inspect-indent '(0 . 0)
188 Extra indentation of WHEN and OTHERWISE with respect to the
189 corresponding INSPECT. Value is a cons cell, the car is
190 extra WHEN indentation and the cdr extra OTHERWISE indentation.
191 simula-electric-indent nil
192 If this variable is non-nil, `simula-indent-line'
193 will check the previous line to see if it has to be reindented.
194 simula-abbrev-keyword 'upcase
195 Determine how SIMULA keywords will be expanded. Value is one of
196 the symbols `upcase', `downcase', `capitalize', (as in) `abbrev-table',
197 or nil if they should not be changed.
198 simula-abbrev-stdproc 'abbrev-table
199 Determine how standard SIMULA procedure and class names will be
200 expanded. Value is one of the symbols `upcase', `downcase', `capitalize',
201 (as in) `abbrev-table', or nil if they should not be changed.
203 Turning on SIMULA mode calls the value of the variable simula-mode-hook
204 with no arguments, if that value is non-nil
206 Warning: simula-mode-hook should not read in an abbrev file without calling
207 the function simula-install-standard-abbrevs afterwards, preferably not
210 (kill-all-local-variables)
211 (use-local-map simula-mode-map)
212 (setq major-mode 'simula-mode)
213 (setq mode-name "SIMULA")
214 (make-local-variable 'comment-column)
215 (setq comment-column 40)
216 (make-local-variable 'end-comment-column)
217 (setq end-comment-column 75)
218 (set-syntax-table simula-mode-syntax-table)
219 (make-local-variable 'paragraph-start)
220 (setq paragraph-start "[ \t]*$\\|\\f")
221 (make-local-variable 'paragraph-separate)
222 (setq paragraph-separate paragraph-start)
223 (make-local-variable 'indent-line-function)
224 (setq indent-line-function 'simula-indent-line)
225 (make-local-variable 'require-final-newline)
226 (setq require-final-newline t)
227 (make-local-variable 'comment-start)
228 (setq comment-start "! ")
229 (make-local-variable 'comment-end)
230 (setq comment-end " ;")
231 (make-local-variable 'comment-start-skip)
232 (setq comment-start-skip "!+ *")
233 (make-local-variable 'parse-sexp-ignore-comments)
234 (setq parse-sexp-ignore-comments nil)
235 (make-local-variable 'comment-multi-line)
236 (setq comment-multi-line t)
237 (if simula-mode-abbrev-table
239 (if simula-abbrev-file
240 (read-abbrev-file simula-abbrev-file)
241 (define-abbrev-table 'simula-mode-abbrev-table ()))
242 (let (abbrevs-changed)
243 (simula-install-standard-abbrevs)))
244 (setq local-abbrev-table simula-mode-abbrev-table)
246 (run-hooks 'simula-mode-hook))
250 (defun simula-indent-line ()
251 "Indent this line as SIMULA code.
252 If `simula-electric-indent' is non-nil, indent previous line if necessary."
253 (let ((origin (- (point-max) (point)))
254 (indent (simula-calculate-indent))
255 (case-fold-search t))
259 ;; manually expand abbrev on last line, if any
263 ;; now maybe we should reindent that line
264 (if simula-electric-indent
267 (skip-chars-forward " \t\f")
270 "\\(end\\|if\\|then\\|else\\|when\\|otherwise\\)\\>")
271 (not (simula-context)))
273 (let ((post-indent (simula-calculate-indent)))
274 (if (eq (current-indentation) post-indent)
276 (delete-horizontal-space)
277 (indent-to post-indent)))))))
278 (goto-char (- (point-max) origin))
279 (if (eq (current-indentation) indent)
280 (back-to-indentation)
281 (delete-horizontal-space)
282 (indent-to indent)))))
285 (defun simula-indent-command (&optional whole-exp)
286 "Indent current line as SIMULA code, or insert TAB character.
287 If `simula-tab-always-indent' is non-nil, always indent current line.
288 Otherwise, indent only if point is before any non-whitespace
289 character on the line.
291 A numeric argument, regardless of its value, means indent rigidly
292 all the lines of the SIMULA statement after point so that this line
293 becomes properly indented.
294 The relative indentation among the lines of the statement are preserved."
296 (let ((case-fold-search t))
297 (if (or whole-exp simula-tab-always-indent
299 (skip-chars-backward " \t\f")
301 ;; reindent current line
302 (let ((indent (save-excursion
304 (simula-calculate-indent)))
305 (current (current-indentation))
306 (origin (- (point-max) (point)))
308 (skip-chars-backward " \t\f")
312 (if (eq current indent)
314 (skip-chars-backward " \t\f")
316 (back-to-indentation))
318 (delete-horizontal-space)
321 (goto-char (- (point-max) origin))))
322 (setq origin (point))
325 (beginning-of-line 2)
328 (simula-next-statement 1)
330 (if (and (> end beg) (not (eq indent current)))
331 (indent-code-rigidly beg end (- indent current) "%")))))
335 (defun simula-context ()
336 "Returns value according to syntactic SIMULA context of point.
337 0 point inside COMMENT comment
338 1 point on SIMULA-compiler directive line
339 2 point inside END comment
340 3 point inside string
341 4 point inside character constant
343 ;; first, find out if this is a compiler directive line
346 (eq (following-char) ?%))
350 ;; The current line is NOT a compiler directive line.
351 ;; Now, the strategy is to search backward to find a semicolon
352 ;; that is NOT inside a string. The point after semicolon MUST be
353 ;; outside a comment, since semicolons are comment-ending and
354 ;; comments are non-recursive. We take advantage of the fact
355 ;; that strings MUST end on the same line as they started, so
356 ;; that we can easily decide whether we are inside a string or not.
357 (let (return-value (origin (point)))
358 (skip-chars-backward "^;" (point-min))
359 ;; found semicolon or beginning of buffer
360 (let (loopvalue (saved-point origin))
361 (while (and (not (bobp))
364 ;; compiler directive line? If so, cont searching..
365 (eq (following-char) ?%))
367 (while (< (point) saved-point)
368 (skip-chars-forward "^;\"'")
371 ((eq (preceding-char) ?\;)
372 (setq saved-point (point)))
373 ((eq (preceding-char) ?\")
374 (skip-chars-forward "^\";")
375 (if (eq (following-char) ?\;)
376 (setq saved-point (point) loopvalue t)
379 (if (eq (following-char) ?')
381 (skip-chars-forward "^';")
382 (if (eq (following-char) ?\;)
383 (setq saved-point (point) loopvalue t)
387 (skip-chars-backward "^;")
388 (setq saved-point (point) loopvalue nil)))
389 ;; Now we are CERTAIN that we are outside comments and strings.
390 ;; The job now is to search forward again towards the origin
391 ;; skipping directives, comments and strings correctly,
392 ;; so that we know what context we are in when we find the origin.
396 "\\<end\\>\\|!\\|\"\\|'\\|^%\\|\\<comment\\>" origin 'move))
398 ((memq (preceding-char) '(?d ?D))
399 (setq return-value 2)
400 (while (and (memq (preceding-char) '(?d ?D)) (not return-value))
401 (while (and (re-search-forward
402 ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\|^%"
404 (eq (preceding-char) ?%))
405 (beginning-of-line 2)))
406 (if (looking-at "[ \t\n\f]*\\(;\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\)")
407 (setq return-value nil)))
408 ((memq (preceding-char) '(?! ?t ?T))
410 (setq return-value 0)
411 (skip-chars-forward "^%;" origin)
412 (while (and return-value (< (point) origin))
413 (if (eq (following-char) ?\;)
414 (setq return-value nil)
416 (beginning-of-line 2) ; skip directive inside comment
417 (forward-char 1)) ; or single '%'
418 (skip-chars-forward "^%;" origin))))
419 ((eq (preceding-char) ?\")
420 (if (not (search-forward "\"" origin 'move))
421 (setq return-value 3)))
422 ((eq (preceding-char) ?\')
423 (if (or (eq (point) origin) (eobp))
424 (setq return-value 4)
426 (if (not (search-forward "'" origin 'move))
427 (setq return-value 4))))
428 ;; compiler directive line - skip
429 (t (beginning-of-line 2))))
434 (defun simula-electric-label ()
435 "If this is a label that starts the line, reindent the line."
439 (let ((origin (- (point-max) (point)))
441 ;; don't mix a label with an assignment operator := :-
442 ;; therefore look at next typed character...
443 (next-char (if (fboundp 'next-command-event)
444 (event-to-character (setq unread-command-events
445 (list (next-command-event))))
447 (setq unread-command-events (list (read-event)))))
448 ;(com-char last-command-char) -- unused
451 ;; Problem: find out if character just read is a command char
452 ;; that would insert something after ':' making it a label.
453 ;; At least \n, \r (and maybe \t) falls into this category.
454 ;; This is a real crock, it depends on traditional keymap
455 ;; bindings, that is, printing characters doing self-insert,
456 ;; and no other command sequence inserting '-' or '='.
457 ;; simula-electric-label can be easily fooled...
458 (if (and (not (memq next-char '(?= ?-)))
459 (or (memq next-char '(?\n ?\r))
460 (and (eq next-char ?\t)
461 simula-tab-always-indent)
462 (not (memq (following-char) '(?= ?-))))
463 (not (simula-context))
467 (skip-chars-backward " \t\f")
468 (skip-chars-backward "a-zA-Z0-9_")
469 (if (looking-at "virtual\\>")
471 (skip-chars-backward " \t\f")
473 (let ((amount (simula-calculate-indent)))
474 (delete-horizontal-space)
476 (goto-char (- (point-max) origin)))))
479 (defun simula-backward-up-level (count)
480 "Move backward up COUNT block levels.
481 If COUNT is negative, move forward up block level instead."
483 (let ((origin (point))
484 (case-fold-search t))
488 (re-search-backward "\\<begin\\>\\|\\<end\\>")
489 (if (not (simula-context))
490 (setq count (if (memq (following-char) '(?b ?B))
494 (re-search-forward "\\<begin\\>\\|\\<end\\>")
496 (if (not (simula-context))
497 (setq count (if (memq (following-char) '(?e ?E))
501 ;; If block level not found, jump back to origin and signal an error
504 (error "No higher block level")))
507 (signal 'quit nil))))))
510 (defun simula-forward-down-level (count)
511 "Move forward down COUNT block levels.
512 If COUNT is negative, move backward down block level instead."
514 ;; When we search for a deeper block level, we must never
515 ;; get out of the block where we started -> count >= start-count
516 (let ((start-count count)
518 (case-fold-search t))
522 (re-search-backward "\\<begin\\>\\|\\<end\\>")
523 (if (not (simula-context))
524 (setq count (if (memq (following-char) '(?e ?E))
527 (if (< count start-count) (signal 'error nil)))
529 (re-search-forward "\\<begin\\>\\|\\<end\\>")
531 (if (not (simula-context))
532 (setq count (if (memq (following-char) '(?b ?B))
536 ;; deeper level has to be found within starting block
537 (if (> count start-count) (signal 'error nil))))
538 ;; If block level not found, jump back to origin and signal an error
541 (error "No containing block level")))
544 (signal 'quit nil))))))
547 (defun simula-previous-statement (count)
548 "Move backward COUNT statements.
549 If COUNT is negative, move forward instead."
552 (simula-next-statement (- count))
558 (simula-skip-comment-backward)
559 (if (memq (preceding-char) '(?n ?N))
562 (if (not (looking-at "\\<begin\\>"))
564 (if (eq (preceding-char) ?\;)
566 (while (and (natnump (setq count (1- count)))
567 (setq status (simula-search-backward
568 ";\\|\\<begin\\>" nil 'move))))
571 (if (eq (following-char) ?\;)
573 (backward-word -1))))
574 (simula-skip-comment-forward))
575 (error (progn (goto-char origin)
576 (error "Incomplete statement (too many ENDs)")))
577 (quit (progn (goto-char origin) (signal 'quit nil)))))))
580 (defun simula-next-statement (count)
581 "Move forward COUNT statements.
582 If COUNT is negative, move backward instead."
585 (simula-previous-statement (- count))
591 (simula-skip-comment-forward)
592 (if (looking-at "\\<end\\>") (forward-word 1))
593 (while (and (natnump (setq count (1- count)))
594 (setq status (simula-search-forward
595 ";\\|\\<end\\>" (point-max) 'move))))
596 (if (and status (/= (preceding-char) ?\;))
599 (simula-skip-comment-backward))))
600 (error (progn (goto-char origin)
601 (error "Incomplete statement (too few ENDs)")))
602 (quit (progn (goto-char origin) (signal 'quit nil)))))))
605 (defun simula-skip-comment-backward ()
606 "Search towards bob to find first char that is outside a comment."
611 (skip-chars-backward " \t\n\f")
612 (if (eq (preceding-char) ?\;)
615 (setq context (simula-context)))
616 (setq context (simula-context)))
618 ((memq context '(nil 3 4))
619 ;; check to see if we found a label
620 (if (and (eq (preceding-char) ?:)
621 (not (memq (following-char) '(?- ?=)))
623 (skip-chars-backward ": \t\fa-zA-Z0-9_")
624 (not (looking-at "virtual\\>"))))
625 (skip-chars-backward ": \t\fa-zA-Z0-9_")
626 (throw 'simula-out nil)))
628 ;; since we are inside a comment, it must start somewhere!
629 (while (and (re-search-backward "!\\|\\<comment\\>")
630 (memq (simula-context) '(0 1)))))
634 (throw 'simula-out nil)))
636 ;; an END-comment must belong to an END
637 (re-search-backward "\\<end\\>")
639 (throw 'simula-out nil))
640 ;; should be impossible to get here..
644 (defun simula-skip-comment-forward ()
645 "Search towards eob to find first char that is outside a comment."
646 ;; this function assumes we start with point .outside a comment
650 (skip-chars-forward " \t\n\f")
652 ((looking-at "!\\|\\<comment\\>")
653 (search-forward ";" nil 'move))
654 ((and (bolp) (eq (following-char) ?%))
655 (beginning-of-line 2))
656 ((and (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]")
657 (not (looking-at "virtual\\>")))
658 (skip-chars-forward "a-zA-Z0-9_ \t\f:"))
660 (throw 'simula-out t))))))
663 (defun simula-forward-up-level ()
664 (let ((continue-loop t)
670 (if (re-search-backward "\\<begin\\>\\|\\<end\\>" (point-min) 'move)
671 (setq temp (simula-context)
672 return-value (and (memq (preceding-char) '(?d ?D))
673 (memq temp '(nil 2)))
674 continue-loop (and (not return-value)
675 (simula-forward-up-level)))
676 (setq continue-loop nil)))
683 (defun simula-calculate-indent ()
685 (let ((where (simula-context))
697 ;; If comment started on previous non-blank line, indent to the
698 ;; column where the comment started, else indent as that line.
700 (skip-chars-backward " \t\n\f")
701 (while (and (not (bolp)) (eq (simula-context) 0))
702 (re-search-backward "^\\|!\\|\\<comment\\>"))
703 (skip-chars-forward " \t\n\f")
708 ;; Detect missing string delimiters
711 (error "Inside string"))
713 (error "Inside character constant"))
715 ;; check to see if inside ()'s
717 ((setq temp (simula-inside-parens))
720 ;; Calculate non-comment indentation
722 ;; first, find out if this line starts with something that needs
723 ;; special indentation (END/IF/THEN/ELSE/WHEN/OTHERWISE or label)
725 (skip-chars-forward " \t\f")
730 ((looking-at "end\\>")
731 (setq indent (- simula-indent-level)
736 ((looking-at "if\\>\\|then\\>\\|else\\>")
737 ;; search for the *starting* IF
739 ((memq (following-char) '(?T ?t))
740 (setq indent (car simula-if-indent)))
741 ((memq (following-char) '(?E ?e))
742 (setq indent (cdr simula-if-indent)))
750 ((looking-at "when\\>\\|otherwise\\>")
751 ;; search for corresponding INSPECT
752 (if (memq (following-char) '(?W ?w))
753 (setq indent (car simula-inspect-indent))
754 (setq indent (cdr simula-inspect-indent)))
755 (simula-find-inspect))
759 ((and (not (looking-at "virtual\\>"))
760 (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]"))
761 (setq indent simula-label-offset)))
762 ;; find line with non-comment text
763 (simula-skip-comment-backward)
765 (not (eq (preceding-char) ?\;))
766 (if (memq (preceding-char) '(?N ?n))
769 (not (looking-at "begin\\>")))
772 (simula-previous-statement 1)
773 (simula-skip-comment-backward)))
775 (save-excursion (beginning-of-line) (point))
776 ;; - perhaps this is a continued statement
781 (if (eq (char-syntax (preceding-char)) ?w)
785 "begin\\|then\\|else\\|when\\|otherwise\\|do"
787 (not (memq (preceding-char) '(?: ?\;)))))))
789 ;; MAIN calculation loop - count BEGIN/DO etc.
792 (if (re-search-backward
793 ";\\|\\<\\(begin\\|end\\|if\\|else\\|then\\|when\\|otherwise\\|do\\)\\>"
796 ();; found something in a comment/string - ignore
797 (setq temp (following-char))
800 (simula-previous-statement 1))
801 ((looking-at "begin\\>")
802 (setq indent (+ indent simula-indent-level)))
803 ((looking-at "end\\>")
805 (simula-previous-statement 1))
806 ((looking-at "do\\>")
807 (setq indent (+ indent simula-substatement-offset))
808 (simula-find-do-match))
809 ((looking-at "\\(if\\|then\\|else\\)\\>")
810 (if (memq temp '(?I ?i))
812 (setq indent (+ indent
813 simula-substatement-offset
814 (if (memq temp '(?T ?t))
815 (car simula-if-indent)
816 (cdr simula-if-indent)))))
818 ((looking-at "\\<when\\>\\|\\<otherwise\\>")
819 (setq indent (+ indent
820 simula-substatement-offset
821 (if (memq temp '(?W ?w))
822 (car simula-if-indent)
823 (cdr simula-if-indent))))
824 (simula-find-inspect)))
825 ;; found the start of a [sub]statement
826 ;; add indentation for continued statement
830 (if (listp simula-continued-statement-offset)
831 (car simula-continued-statement-offset)
832 simula-continued-statement-offset))))
834 (save-excursion (beginning-of-line) (point))
836 ;; search failed .. point is at beginning of line
837 ;; determine if we should continue searching
838 ;; (at or before comment or label)
839 ;; temp = t means finished
841 (and (not (simula-context))
843 (skip-chars-forward " \t\f")
844 (or (looking-at "virtual")
847 "!\\|comment\\>\\|[a-z0-9_]*[ \t\f]*:[^-=]")))))
849 ;; if we are finished, find current line's indentation
851 (setq indent (+ indent (current-indentation))))
852 ;; find next line with non-comment SIMULA text
853 ;; maybe indent extra if statement continues
854 (simula-skip-comment-backward)
857 (if (eq (char-syntax (preceding-char)) ?w)
861 "begin\\|then\\|else\\|when\\|otherwise\\|do")))
862 (not (memq (preceding-char) '(?: ?\;))))))
863 ;; if we the state of the continued-variable
864 ;; changed, add indentation for continued statement
865 (if (or (and prev-cont (not continued))
867 (listp simula-continued-statement-offset)))
870 (if (listp simula-continued-statement-offset)
871 (car simula-continued-statement-offset)
872 simula-continued-statement-offset))))
873 ;; while ends if point is at beginning of line at loop test
875 (setq start-line (save-excursion (beginning-of-line) (point)))
876 (beginning-of-line))))
878 ;; return indentation
883 (defun simula-find-if ()
884 "Find starting IF of a IF-THEN[-ELSE[-IF-THEN...]] statement."
887 (if (and (simula-search-backward "\\<if\\>\\|;\\|\\<begin\\>"nil t)
888 (memq (following-char) '(?I ?i)))
891 ;; find out if this IF was really the start of the IF statement
893 (simula-skip-comment-backward)
894 (if (and (eq (char-syntax (preceding-char)) ?w)
897 (looking-at "else\\>")))
899 (throw 'simula-out t)))
900 (if (not (looking-at "\\<if\\>"))
901 (error "Missing IF or misplaced BEGIN or ';' (can't find IF)")
903 ;; we were at the starting IF in the first place..
905 (throw 'simula-out t))))))
908 (defun simula-find-inspect ()
909 "Find INSPECT matching WHEN or OTHERWISE."
913 ;; INSPECTs can be nested, have to find the corresponding one
916 (if (and (simula-search-backward "\\<inspect\\>\\|\\<otherwise\\>\\|;"
918 (/= (following-char) ?\;))
919 (if (memq (following-char) '(?O ?o))
920 (setq level (1+ level))
922 (throw 'simula-out t)
923 (setq level (1- level))))
924 (error "Missing INSPECT or misplaced ';' (can't find INSPECT)"))))))
927 (defun simula-find-do-match ()
928 "Find keyword matching DO: FOR, WHILE, INSPECT or WHEN"
929 (while (and (re-search-backward
930 "\\<\\(do\\|for\\|while\\|inspect\\|when\\|end\\|begin\\)\\>\\|;"
933 (if (and (looking-at "\\<\\(for\\|while\\|inspect\\|when\\)\\>")
934 (not (simula-context)))
936 (error "No matching FOR, WHILE or INSPECT for DO, or misplaced ';'")))
939 (defun simula-inside-parens ()
940 "Return position after `(' on line if inside parentheses, nil otherwise."
945 (if (re-search-backward "(\\|)\\|;" nil t)
946 (if (eq (simula-context) nil)
947 ;; found something - check it out
949 ((eq (following-char) ?\;)
951 (throw 'simula-out nil)
952 (error "Parenthesis mismatch or misplaced ';'")))
953 ((eq (following-char) ?\()
955 (throw 'simula-out (1+ (current-column)))
956 (setq parlevel (1- parlevel))))
957 (t (setq parlevel (1+ parlevel))))
958 );; nothing - inside comment or string
960 (throw 'simula-out nil)))))))
963 (defun simula-goto-definition ()
964 "Goto point of definition of variable, procedure or class."
968 (defun simula-expand-stdproc ()
969 (if (or (not simula-abbrev-stdproc) (simula-context))
972 ((eq simula-abbrev-stdproc 'upcase) (upcase-word -1))
973 ((eq simula-abbrev-stdproc 'downcase) (downcase-word -1))
974 ((eq simula-abbrev-stdproc 'capitalize) (capitalize-word -1)))))
977 (defun simula-expand-keyword ()
978 (if (or (not simula-abbrev-keyword) (simula-context))
981 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
982 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
983 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))))
986 (defun simula-electric-keyword ()
987 "Expand SIMULA keyword. If it starts the line, reindent."
989 (let ((show-char (eq this-command 'self-insert-command)))
990 ;; If the abbrev expansion results in reindentation, the user may have
991 ;; to wait some time before the character he typed is displayed
992 ;; (the char causing the expansion is inserted AFTER the hook function
993 ;; is called). This is annoying in case of normal characters.
994 ;; However, if the user pressed a key bound to newline, it is better
995 ;; to have the line inserted after the begin-end match.
998 (insert-char last-command-char 1)
1001 (if (let ((where (simula-context))
1002 (case-fold-search t))
1004 (if (and (eq where 2) (eq (char-syntax (preceding-char)) ?w))
1007 (not (looking-at "end\\>"))))))
1010 ((not simula-abbrev-keyword) (unexpand-abbrev))
1011 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
1012 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
1013 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))
1014 (let ((pos (- (point-max) (point)))
1015 (case-fold-search t)
1019 ;; check if the expanded word is on the beginning of the line.
1020 (if (and (eq (char-syntax (preceding-char)) ?w)
1023 (if (looking-at "end\\>")
1025 (simula-backward-up-level 1)
1026 (if (pos-visible-in-window-p)
1032 (+ (point) (window-width))))))))
1033 (skip-chars-backward " \t\f")
1035 (let ((indent (simula-calculate-indent)))
1036 (if (eq indent (current-indentation))
1038 (delete-horizontal-space)
1039 (indent-to indent)))
1040 (skip-chars-forward " \t\f"))
1041 ;; check for END - blow whistles and ring bells
1043 (goto-char (- (point-max) pos))
1046 (quit (goto-char (- (point-max) pos))))))))
1049 (defun simula-search-backward (string &optional limit move)
1050 (setq string (concat string "\\|\\<end\\>"))
1053 (while (re-search-backward string limit move)
1054 (if (simula-context)
1056 (if (looking-at "\\<end\\>")
1059 (while (natnump level)
1060 (re-search-backward "\\<begin\\>\\|\\<end\\>")
1061 (if (simula-context)
1063 (setq level (if (memq (following-char) '(?b ?B))
1066 (throw 'simula-out t)))))))
1069 (defun simula-search-forward (string &optional limit move)
1070 (setq string (concat string "\\|\\<begin\\>"))
1073 (while (re-search-forward string limit move)
1074 (goto-char (match-beginning 0))
1075 (if (simula-context)
1076 (goto-char (1- (match-end 0)))
1077 (if (looking-at "\\<begin\\>")
1079 (goto-char (1- (match-end 0)))
1081 (while (natnump level)
1082 (re-search-forward "\\<begin\\>\\|\\<end\\>")
1084 (if (not (simula-context))
1085 (setq level (if (memq (following-char) '(?e ?E))
1088 (backward-word -1)))
1089 (goto-char (1- (match-end 0)))
1090 (throw 'exit t)))))))
1093 (defun simula-install-standard-abbrevs ()
1094 "Define Simula keywords, procedures and classes in local abbrev table."
1095 ;; procedure and class names are as of the SIMULA 87 standard.
1097 (mapcar (function (lambda (args)
1098 (apply 'define-abbrev simula-mode-abbrev-table args)))
1099 '(("abs" "Abs" simula-expand-stdproc)
1100 ("accum" "Accum" simula-expand-stdproc)
1101 ("activate" "ACTIVATE" simula-expand-keyword)
1102 ("addepsilon" "AddEpsilon" simula-expand-stdproc)
1103 ("after" "AFTER" simula-expand-keyword)
1104 ("and" "AND" simula-expand-keyword)
1105 ("arccos" "ArcCos" simula-expand-stdproc)
1106 ("arcsin" "ArcSin" simula-expand-stdproc)
1107 ("arctan" "ArcTan" simula-expand-stdproc)
1108 ("arctan2" "ArcTan2" simula-expand-stdproc)
1109 ("array" "ARRAY" simula-expand-keyword)
1110 ("at" "AT" simula-expand-keyword)
1111 ("before" "BEFORE" simula-expand-keyword)
1112 ("begin" "BEGIN" simula-expand-keyword)
1113 ("blanks" "Blanks" simula-expand-stdproc)
1114 ("boolean" "BOOLEAN" simula-expand-keyword)
1115 ("breakoutimage" "BreakOutImage" simula-expand-stdproc)
1116 ("bytefile" "ByteFile" simula-expand-stdproc)
1117 ("call" "Call" simula-expand-stdproc)
1118 ("cancel" "Cancel" simula-expand-stdproc)
1119 ("cardinal" "Cardinal" simula-expand-stdproc)
1120 ("char" "Char" simula-expand-stdproc)
1121 ("character" "CHARACTER" simula-expand-keyword)
1122 ("checkpoint" "CheckPoint" simula-expand-stdproc)
1123 ("class" "CLASS" simula-expand-keyword)
1124 ("clear" "Clear" simula-expand-stdproc)
1125 ("clocktime" "ClockTime" simula-expand-stdproc)
1126 ("close" "Close" simula-expand-stdproc)
1127 ("comment" "COMMENT" simula-expand-keyword)
1128 ("constant" "Constant" simula-expand-stdproc)
1129 ("copy" "Copy" simula-expand-stdproc)
1130 ("cos" "Cos" simula-expand-stdproc)
1131 ("cosh" "CosH" simula-expand-stdproc)
1132 ("cotan" "CoTan" simula-expand-stdproc)
1133 ("cputime" "CpuTime" simula-expand-stdproc)
1134 ("current" "Current" simula-expand-stdproc)
1135 ("datetime" "DateTime" simula-expand-stdproc)
1136 ("decimalmark" "DecimalMark" simula-expand-stdproc)
1137 ("delay" "DELAY" simula-expand-keyword)
1138 ("deleteimage" "DeleteImage" simula-expand-stdproc)
1139 ("detach" "Detach" simula-expand-stdproc)
1140 ("digit" "Digit" simula-expand-stdproc)
1141 ("directbytefile" "DirectByteFile" simula-expand-stdproc)
1142 ("directfile" "DirectFile" simula-expand-stdproc)
1143 ("discrete" "Discrete" simula-expand-stdproc)
1144 ("do" "DO" simula-expand-keyword)
1145 ("downcase" "Downcase" simula-expand-stdproc)
1146 ("draw" "Draw" simula-expand-stdproc)
1147 ("eject" "Eject" simula-expand-stdproc)
1148 ("else" "ELSE" simula-electric-keyword)
1149 ("empty" "Empty" simula-expand-stdproc)
1150 ("end" "END" simula-electric-keyword)
1151 ("endfile" "Endfile" simula-expand-stdproc)
1152 ("entier" "Entier" simula-expand-stdproc)
1153 ("eq" "EQ" simula-expand-keyword)
1154 ("eqv" "EQV" simula-expand-keyword)
1155 ("erlang" "Erlang" simula-expand-stdproc)
1156 ("error" "Error" simula-expand-stdproc)
1157 ("evtime" "EvTime" simula-expand-stdproc)
1158 ("exp" "Exp" simula-expand-stdproc)
1159 ("external" "EXTERNAL" simula-expand-keyword)
1160 ("false" "FALSE" simula-expand-keyword)
1161 ("field" "Field" simula-expand-stdproc)
1162 ("file" "File" simula-expand-stdproc)
1163 ("first" "First" simula-expand-stdproc)
1164 ("follow" "Follow" simula-expand-stdproc)
1165 ("for" "FOR" simula-expand-keyword)
1166 ("ge" "GE" simula-expand-keyword)
1167 ("getchar" "GetChar" simula-expand-stdproc)
1168 ("getfrac" "GetFrac" simula-expand-stdproc)
1169 ("getint" "GetInt" simula-expand-stdproc)
1170 ("getreal" "GetReal" simula-expand-stdproc)
1171 ("go" "GO" simula-expand-keyword)
1172 ("goto" "GOTO" simula-expand-keyword)
1173 ("gt" "GT" simula-expand-keyword)
1174 ("head" "Head" simula-expand-stdproc)
1175 ("hidden" "HIDDEN" simula-expand-keyword)
1176 ("histd" "HistD" simula-expand-stdproc)
1177 ("histo" "Histo" simula-expand-stdproc)
1178 ("hold" "Hold" simula-expand-stdproc)
1179 ("idle" "Idle" simula-expand-stdproc)
1180 ("if" "IF" simula-expand-keyword)
1181 ("image" "Image" simula-expand-stdproc)
1182 ("imagefile" "ImageFile" simula-expand-stdproc)
1183 ("imp" "IMP" simula-expand-keyword)
1184 ("in" "IN" simula-expand-keyword)
1185 ("inbyte" "InByte" simula-expand-stdproc)
1186 ("inbytefile" "InByteFile" simula-expand-stdproc)
1187 ("inchar" "InChar" simula-expand-stdproc)
1188 ("infile" "InFile" simula-expand-stdproc)
1189 ("infrac" "InFrac" simula-expand-stdproc)
1190 ("inimage" "InImage" simula-expand-stdproc)
1191 ("inint" "InInt" simula-expand-stdproc)
1192 ("inner" "INNER" simula-expand-keyword)
1193 ("inreal" "InReal" simula-expand-stdproc)
1194 ("inrecord" "InRecord" simula-expand-stdproc)
1195 ("inspect" "INSPECT" simula-expand-keyword)
1196 ("integer" "INTEGER" simula-expand-keyword)
1197 ("intext" "InText" simula-expand-stdproc)
1198 ("into" "Into" simula-expand-stdproc)
1199 ("is" "IS" simula-expand-keyword)
1200 ("isochar" "ISOChar" simula-expand-stdproc)
1201 ("isopen" "IsOpen" simula-expand-stdproc)
1202 ("isorank" "ISORank" simula-expand-stdproc)
1203 ("label" "LABEL" simula-expand-keyword)
1204 ("last" "Last" simula-expand-stdproc)
1205 ("lastitem" "LastItem" simula-expand-stdproc)
1206 ("lastloc" "LastLoc" simula-expand-stdproc)
1207 ("le" "LE" simula-expand-keyword)
1208 ("length" "Length" simula-expand-stdproc)
1209 ("letter" "Letter" simula-expand-stdproc)
1210 ("line" "Line" simula-expand-stdproc)
1211 ("linear" "Linear" simula-expand-stdproc)
1212 ("linesperpage" "LinesPerPage" simula-expand-stdproc)
1213 ("link" "Link" simula-expand-stdproc)
1214 ("linkage" "Linkage" simula-expand-stdproc)
1215 ("ln" "Ln" simula-expand-stdproc)
1216 ("locate" "Locate" simula-expand-stdproc)
1217 ("location" "Location" simula-expand-stdproc)
1218 ("lock" "Lock" simula-expand-stdproc)
1219 ("locked" "Locked" simula-expand-stdproc)
1220 ("log10" "Log10" simula-expand-stdproc)
1221 ("long" "LONG" simula-expand-keyword)
1222 ("lowcase" "LowCase" simula-expand-stdproc)
1223 ("lowerbound" "LowerBound" simula-expand-stdproc)
1224 ("lowten" "LowTen" simula-expand-stdproc)
1225 ("lt" "LT" simula-expand-keyword)
1226 ("main" "Main" simula-expand-stdproc)
1227 ("max" "Max" simula-expand-stdproc)
1228 ("maxint" "MaxInt" simula-expand-stdproc)
1229 ("maxlongreal" "MaxLongReal" simula-expand-stdproc)
1230 ("maxloc" "MaxLoc" simula-expand-stdproc)
1231 ("maxrank" "MaxRank" simula-expand-stdproc)
1232 ("maxreal" "MaxReal" simula-expand-stdproc)
1233 ("min" "Min" simula-expand-stdproc)
1234 ("minint" "MinInt" simula-expand-stdproc)
1235 ("minlongreal" "MinLongReal" simula-expand-stdproc)
1236 ("minrank" "MinRank" simula-expand-stdproc)
1237 ("minreal" "MinReal" simula-expand-stdproc)
1238 ("mod" "Mod" simula-expand-stdproc)
1239 ("more" "More" simula-expand-stdproc)
1240 ("name" "NAME" simula-expand-keyword)
1241 ("ne" "NE" simula-expand-keyword)
1242 ("negexp" "NegExp" simula-expand-stdproc)
1243 ("new" "NEW" simula-expand-keyword)
1244 ("nextev" "NextEv" simula-expand-stdproc)
1245 ("none" "NONE" simula-expand-keyword)
1246 ("normal" "Normal" simula-expand-stdproc)
1247 ("not" "NOT" simula-expand-keyword)
1248 ("notext" "NOTEXT" simula-expand-keyword)
1249 ("open" "Open" simula-expand-stdproc)
1250 ("or" "OR" simula-expand-keyword)
1251 ("otherwise" "OTHERWISE" simula-electric-keyword)
1252 ("out" "Out" simula-expand-stdproc)
1253 ("outbyte" "OutByte" simula-expand-stdproc)
1254 ("outbytefile" "OutByteFile" simula-expand-stdproc)
1255 ("outchar" "OutChar" simula-expand-stdproc)
1256 ("outfile" "OutFile" simula-expand-stdproc)
1257 ("outfix" "OutFix" simula-expand-stdproc)
1258 ("outfrac" "OutFrac" simula-expand-stdproc)
1259 ("outimage" "OutImage" simula-expand-stdproc)
1260 ("outint" "OutInt" simula-expand-stdproc)
1261 ("outreal" "OutReal" simula-expand-stdproc)
1262 ("outrecord" "OutRecord" simula-expand-stdproc)
1263 ("outtext" "OutText" simula-expand-stdproc)
1264 ("page" "Page" simula-expand-stdproc)
1265 ("passivate" "Passivate" simula-expand-stdproc)
1266 ("poisson" "Poisson" simula-expand-stdproc)
1267 ("pos" "Pos" simula-expand-stdproc)
1268 ("precede" "Precede" simula-expand-stdproc)
1269 ("pred" "Pred" simula-expand-stdproc)
1270 ("prev" "Prev" simula-expand-stdproc)
1271 ("printfile" "PrintFile" simula-expand-stdproc)
1272 ("prior" "PRIOR" simula-expand-keyword)
1273 ("procedure" "PROCEDURE" simula-expand-keyword)
1274 ("process" "Process" simula-expand-stdproc)
1275 ("protected" "PROTECTED" simula-expand-keyword)
1276 ("putchar" "PutChar" simula-expand-stdproc)
1277 ("putfix" "PutFix" simula-expand-stdproc)
1278 ("putfrac" "PutFrac" simula-expand-stdproc)
1279 ("putint" "PutInt" simula-expand-stdproc)
1280 ("putreal" "PutReal" simula-expand-stdproc)
1281 ("qua" "QUA" simula-expand-keyword)
1282 ("randint" "RandInt" simula-expand-stdproc)
1283 ("rank" "Rank" simula-expand-stdproc)
1284 ("reactivate" "REACTIVATE" simula-expand-keyword)
1285 ("real" "REAL" simula-expand-keyword)
1286 ("ref" "REF" simula-expand-keyword)
1287 ("resume" "Resume" simula-expand-stdproc)
1288 ("setaccess" "SetAccess" simula-expand-stdproc)
1289 ("setpos" "SetPos" simula-expand-stdproc)
1290 ("short" "SHORT" simula-expand-keyword)
1291 ("sign" "Sign" simula-expand-stdproc)
1292 ("simset" "SimSet" simula-expand-stdproc)
1293 ("simulaid" "SimulaId" simula-expand-stdproc)
1294 ("simulation" "Simulation" simula-expand-stdproc)
1295 ("sin" "Sin" simula-expand-stdproc)
1296 ("sinh" "SinH" simula-expand-stdproc)
1297 ("sourceline" "SourceLine" simula-expand-stdproc)
1298 ("spacing" "Spacing" simula-expand-stdproc)
1299 ("sqrt" "Sqrt" simula-expand-stdproc)
1300 ("start" "Start" simula-expand-stdproc)
1301 ("step" "STEP" simula-expand-keyword)
1302 ("strip" "Strip" simula-expand-stdproc)
1303 ("sub" "Sub" simula-expand-stdproc)
1304 ("subepsilon" "SubEpsilon" simula-expand-stdproc)
1305 ("suc" "Suc" simula-expand-stdproc)
1306 ("switch" "SWITCH" simula-expand-keyword)
1307 ("sysin" "SysIn" simula-expand-stdproc)
1308 ("sysout" "SysOut" simula-expand-stdproc)
1309 ("tan" "Tan" simula-expand-stdproc)
1310 ("tanh" "TanH" simula-expand-stdproc)
1311 ("terminate_program" "Terminate_Program" simula-expand-stdproc)
1312 ("terminated" "Terminated" simula-expand-stdproc)
1313 ("text" "TEXT" simula-expand-keyword)
1314 ("then" "THEN" simula-electric-keyword)
1315 ("this" "THIS" simula-expand-keyword)
1316 ("time" "Time" simula-expand-stdproc)
1317 ("to" "TO" simula-expand-keyword)
1318 ("true" "TRUE" simula-expand-keyword)
1319 ("uniform" "Uniform" simula-expand-stdproc)
1320 ("unlock" "Unlock" simula-expand-stdproc)
1321 ("until" "UNTIL" simula-expand-keyword)
1322 ("upcase" "Upcase" simula-expand-stdproc)
1323 ("upperbound" "UpperBound" simula-expand-stdproc)
1324 ("value" "VALUE" simula-expand-keyword)
1325 ("virtual" "VIRTUAL" simula-expand-keyword)
1326 ("wait" "Wait" simula-expand-stdproc)
1327 ("when" "WHEN" simula-electric-keyword)
1328 ("while" "WHILE" simula-expand-keyword))))
1330 ;;; simula.el ends here