simula.el 61.2 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; simula.el --- SIMULA 87 code editing commands for Emacs

3
;; Copyright (C) 1992, 1994, 1996 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5 6 7 8 9
;; Author: Hans Henrik Eriksen <hhe@ifi.uio.no>
;; Maintainer: simula-mode@ifi.uio.no
;; Adapted-By: ESR
;; Keywords: languages

Eric S. Raymond's avatar
Eric S. Raymond committed
10 11 12 13
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Richard M. Stallman's avatar
Richard M. Stallman committed
14
;; the Free Software Foundation; either version 2, or (at your option)
Eric S. Raymond's avatar
Eric S. Raymond committed
15 16 17 18 19 20 21 22
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
23 24 25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Eric S. Raymond's avatar
Eric S. Raymond committed
26

Eric S. Raymond's avatar
Eric S. Raymond committed
27 28
;;; Commentary:

29 30 31 32 33
;; A major mode for editing the Simula language.  It knows about Simula
;; syntax and standard indentation commands.  It also provides convenient
;; abbrevs for Simula keywords.
;;
;; Hans Henrik Eriksen (the author) may be reached at:
Eric S. Raymond's avatar
Eric S. Raymond committed
34 35 36
;;         Institutt for informatikk,
;;         Universitetet i Oslo

Eric S. Raymond's avatar
Eric S. Raymond committed
37
;;; Code:
Eric S. Raymond's avatar
Eric S. Raymond committed
38

39 40 41 42 43

(defconst simula-tab-always-indent-default nil
  "Non-nil means TAB in SIMULA mode should always reindent the current line.
Otherwise TAB indents only when point is within
the run of whitespace at the beginning of the line.")
Eric S. Raymond's avatar
Eric S. Raymond committed
44

45
(defvar simula-tab-always-indent simula-tab-always-indent-default
Richard M. Stallman's avatar
Richard M. Stallman committed
46 47 48
  "*Non-nil means TAB in SIMULA mode should always reindent the current line.
Otherwise TAB indents only when point is within
the run of whitespace at the beginning of the line.")
Eric S. Raymond's avatar
Eric S. Raymond committed
49

50 51 52 53
(defconst simula-indent-level-default 3
  "Indentation of SIMULA statements with respect to containing block.")

(defvar simula-indent-level simula-indent-level-default
Eric S. Raymond's avatar
Eric S. Raymond committed
54 55
  "*Indentation of SIMULA statements with respect to containing block.")

56 57 58 59
(defconst simula-substatement-offset-default 3
  "Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.")

(defvar simula-substatement-offset simula-substatement-offset-default
Eric S. Raymond's avatar
Eric S. Raymond committed
60 61
  "*Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.")

62 63 64 65 66 67 68
(defconst simula-continued-statement-offset-default 3
  "Extra indentation for lines not starting a statement or substatement.
If value is a list, each line in a multipleline continued statement
will have the car of the list extra indentation with respect to
the previous line of the statement.")

(defvar simula-continued-statement-offset simula-continued-statement-offset-default
Eric S. Raymond's avatar
Eric S. Raymond committed
69 70 71 72 73
  "*Extra indentation for lines not starting a statement or substatement.
If value is a list, each line in a multipleline continued statement
will have the car of the list extra indentation with respect to
the previous line of the statement.")

74 75 76 77
(defconst simula-label-offset-default -4711
  "Offset of SIMULA label lines relative to usual indentation.")

(defvar simula-label-offset simula-label-offset-default
Richard M. Stallman's avatar
Richard M. Stallman committed
78
  "*Offset of SIMULA label lines relative to usual indentation.")
Eric S. Raymond's avatar
Eric S. Raymond committed
79

80 81 82 83 84 85
(defconst simula-if-indent-default '(0 . 0)
  "Extra indentation of THEN and ELSE with respect to the starting IF.
Value is a cons cell, the car is extra THEN indentation and the cdr
extra ELSE indentation.  IF after ELSE is indented as the starting IF.")

(defvar simula-if-indent simula-if-indent-default
Eric S. Raymond's avatar
Eric S. Raymond committed
86
  "*Extra indentation of THEN and ELSE with respect to the starting IF.
87
Value is a cons cell, the car is extra THEN indentation and the cdr
Richard M. Stallman's avatar
Richard M. Stallman committed
88
extra ELSE indentation.  IF after ELSE is indented as the starting IF.")
Eric S. Raymond's avatar
Eric S. Raymond committed
89

90 91 92 93 94 95
(defconst simula-inspect-indent-default '(0 . 0)
  "Extra indentation of WHEN and OTHERWISE with respect to the INSPECT.
Value is a cons cell, the car is extra WHEN indentation
and the cdr extra OTHERWISE indentation.")

(defvar simula-inspect-indent simula-inspect-indent-default
Richard M. Stallman's avatar
Richard M. Stallman committed
96 97 98
  "*Extra indentation of WHEN and OTHERWISE with respect to the INSPECT.
Value is a cons cell, the car is extra WHEN indentation
and the cdr extra OTHERWISE indentation.")
Eric S. Raymond's avatar
Eric S. Raymond committed
99

100 101 102 103
(defconst simula-electric-indent-default nil
  "Non-nil means `simula-indent-line' function may reindent previous line.")

(defvar simula-electric-indent simula-electric-indent-default
Richard M. Stallman's avatar
Richard M. Stallman committed
104
  "*Non-nil means `simula-indent-line' function may reindent previous line.")
Eric S. Raymond's avatar
Eric S. Raymond committed
105

106 107 108 109 110 111
(defconst simula-abbrev-keyword-default 'upcase
  "Specify how to convert case for SIMULA keywords.
Value is one of the symbols `upcase', `downcase', `capitalize',
(as in) `abbrev-table' or nil if they should not be changed.")

(defvar simula-abbrev-keyword simula-abbrev-keyword-default
Richard M. Stallman's avatar
Richard M. Stallman committed
112 113
  "*Specify how to convert case for SIMULA keywords.
Value is one of the symbols `upcase', `downcase', `capitalize',
114 115 116 117 118 119
(as in) `abbrev-table' or nil if they should not be changed.")

(defconst simula-abbrev-stdproc-default 'abbrev-table
  "Specify how to convert case for standard SIMULA procedure and class names.
Value is one of the symbols `upcase', `downcase', `capitalize',
(as in) `abbrev-table', or nil if they should not be changed.")
Eric S. Raymond's avatar
Eric S. Raymond committed
120

121
(defvar simula-abbrev-stdproc simula-abbrev-stdproc-default
Richard M. Stallman's avatar
Richard M. Stallman committed
122 123
  "*Specify how to convert case for standard SIMULA procedure and class names.
Value is one of the symbols `upcase', `downcase', `capitalize',
124
(as in) `abbrev-table', or nil if they should not be changed.")
Eric S. Raymond's avatar
Eric S. Raymond committed
125 126

(defvar simula-abbrev-file nil
Richard M. Stallman's avatar
Richard M. Stallman committed
127 128 129 130
  "*File with extra abbrev definitions for use in SIMULA mode.
These are used together with the standard abbrev definitions for SIMULA.
Please note that the standard definitions are required
for SIMULA mode to function correctly.")
Eric S. Raymond's avatar
Eric S. Raymond committed
131 132

(defvar simula-mode-syntax-table nil
Richard M. Stallman's avatar
Richard M. Stallman committed
133
  "Syntax table in SIMULA mode buffers.")
Eric S. Raymond's avatar
Eric S. Raymond committed
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
; The following function is taken from cc-mode.el,
; it determines the flavor of the Emacs running
(defconst simula-emacs-features
  (let ((major (and (boundp 'emacs-major-version)
		    emacs-major-version))
	(minor (and (boundp 'emacs-minor-version)
		    emacs-minor-version))
	flavor comments)
    ;; figure out version numbers if not already discovered
    (and (or (not major) (not minor))
	 (string-match "\\([0-9]+\\).\\([0-9]+\\)" emacs-version)
	 (setq major (string-to-int (substring emacs-version
					       (match-beginning 1)
					       (match-end 1)))
	       minor (string-to-int (substring emacs-version
					       (match-beginning 2)
					       (match-end 2)))))
    (if (not (and major minor))
	(error "Cannot figure out the major and minor version numbers."))
    ;; calculate the major version
    (cond
     ((= major 18) (setq major 'v18))	;Emacs 18
     ((= major 4)  (setq major 'v18))	;Epoch 4
     ((= major 19) (setq major 'v19	;Emacs 19
			 flavor (if (string-match "Lucid" emacs-version)
				    'Lucid 'FSF)))
     ;; I don't know
     (t (error "Cannot recognize major version number: %s" major)))
        (list major flavor comments))
  "A list of features extant in the Emacs you are using.
There are many flavors of Emacs out there, each with different
features supporting those needed by simula-mode.  Here's the current
supported list, along with the values for this variable:

 Emacs 19:                  (v19 FSF 1-bit)
 Vanilla Emacs 18/Epoch 4:  (v18 no-dual-comments)
 Emacs 18/Epoch 4 (patch2): (v18 8-bit)
 Lucid Emacs 19:            (v19 Lucid 8-bit).")

(defvar simula-mode-menu
  '(["Report Bug"	      simula-submit-bug-report t]
    ["Indent Line"	      simula-indent-line t]
    ["Backward Statement"     simula-previous-statement t]
    ["Forward Statement"      simula-next-statement t]
    ["Backward Up Level"      simula-backward-up-level t]
    ["Forward Down Statement" simula-forward-down-level t]
    )
  "Lucid Emacs menu for SIMULA mode.")

Eric S. Raymond's avatar
Eric S. Raymond committed
184 185
(if simula-mode-syntax-table
    ()
Richard M. Stallman's avatar
Richard M. Stallman committed
186
  (setq simula-mode-syntax-table (copy-syntax-table (standard-syntax-table)))
Eric S. Raymond's avatar
Eric S. Raymond committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  (modify-syntax-entry ?!  "<"    simula-mode-syntax-table)
  (modify-syntax-entry ?$  "."    simula-mode-syntax-table)
  (modify-syntax-entry ?%  "."    simula-mode-syntax-table)
  (modify-syntax-entry ?'  "\""   simula-mode-syntax-table)
  (modify-syntax-entry ?\( "()"   simula-mode-syntax-table)
  (modify-syntax-entry ?\) ")("   simula-mode-syntax-table)
  (modify-syntax-entry ?\; ">"    simula-mode-syntax-table)
  (modify-syntax-entry ?\[ "."    simula-mode-syntax-table)
  (modify-syntax-entry ?\\ "."    simula-mode-syntax-table)
  (modify-syntax-entry ?\] "."    simula-mode-syntax-table)
  (modify-syntax-entry ?_  "w"    simula-mode-syntax-table)
  (modify-syntax-entry ?\| "."    simula-mode-syntax-table)
  (modify-syntax-entry ?\{ "."    simula-mode-syntax-table)
  (modify-syntax-entry ?\} "."    simula-mode-syntax-table))

(defvar simula-mode-map ()
Richard M. Stallman's avatar
Richard M. Stallman committed
203
  "Keymap used in SIMULA mode.")
Eric S. Raymond's avatar
Eric S. Raymond committed
204 205 206 207 208 209 210 211 212 213 214 215

(if simula-mode-map
    ()
  (setq simula-mode-map (make-sparse-keymap))
  (define-key simula-mode-map "\C-c\C-u"   'simula-backward-up-level)
  (define-key simula-mode-map "\C-c\C-p"   'simula-previous-statement)
  (define-key simula-mode-map "\C-c\C-d"   'simula-forward-down-level)
  (define-key simula-mode-map "\C-c\C-n"   'simula-next-statement)
  ;(define-key simula-mode-map "\C-c\C-g"   'simula-goto-definition)
  ;(define-key simula-mode-map "\C-c\C-h"   'simula-standard-help)
  (define-key simula-mode-map "\177"       'backward-delete-char-untabify)
  (define-key simula-mode-map ":"          'simula-electric-label)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  (define-key simula-mode-map "\e\C-q"     'simula-indent-exp)
  (define-key simula-mode-map "\t"         'simula-indent-command)
  ;; Emacs 19 defines menus in the mode map
  (if (memq 'FSF simula-emacs-features)
      (progn
	(define-key simula-mode-map [menu-bar] (make-sparse-keymap))

	(define-key simula-mode-map [menu-bar simula]
	  (cons "SIMULA" (make-sparse-keymap "SIMULA")))
	(define-key simula-mode-map [menu-bar simula bug-report]
	  '("Submit Bug Report" . simula-submit-bug-report))
	(define-key simula-mode-map [menu-bar simula separator-indent]
	  '("--"))
	(define-key simula-mode-map [menu-bar simula indent-exp]
	  '("Indent Expression" . simula-indent-exp))
	(define-key simula-mode-map [menu-bar simula indent-line]
	  '("Indent Line" . simula-indent-command))
	(define-key simula-mode-map [menu-bar simula separator-navigate]
	  '("--"))
	(define-key simula-mode-map [menu-bar simula backward-stmt]
	  '("Previous Statement" . simula-previous-statement))
	(define-key simula-mode-map [menu-bar simula forward-stmt]
	  '("Next Statement" . simula-next-statement))
	(define-key simula-mode-map [menu-bar simula backward-up]
	  '("Backward Up Level" . simula-backward-up-level))
	(define-key simula-mode-map [menu-bar simula forward-down]
	  '("Forward Down Statement" . simula-forward-down-level))

	(put 'simula-next-statement 'menu-enable '(not (eobp)))
	(put 'simula-previous-statement 'menu-enable '(not (bobp)))
	(put 'simula-forward-down-level 'menu-enable '(not (eobp)))
	(put 'simula-backward-up-level 'menu-enable '(not (bobp)))
	(put 'simula-indent-command 'menu-enable '(not buffer-read-only))
	(put 'simula-indent-exp 'menu-enable '(not buffer-read-only))))

  ;; RMS: mouse-3 should not select this menu.  mouse-3's global
  ;; definition is useful in SIMULA mode and we should not interfere
  ;; with that.  The menu is mainly for beginners, and for them,
  ;; the menubar requires less memory than a special click.
  ;; in Lucid Emacs, we want the menu to popup when the 3rd button is
  ;; hit.  In 19.10 and beyond this is done automatically if we put
  ;; the menu on mode-popup-menu variable, see c-common-init [cc-mode.el]
  (if (memq 'Lucid simula-emacs-features)
      (if (not (boundp 'mode-popup-menu))
	  (define-key simula-mode-map 'button3 'simula-popup-menu))))

;; menus for Lucid
(defun simula-popup-menu (e)
  "Pops up the SIMULA menu."
  (interactive "@e")
  (popup-menu (cons (concat mode-name " Mode Commands") simula-mode-menu))
  (simula-keep-region-active))

;; active regions, and auto-newline/hungry delete key
(defun simula-keep-region-active ()
  ;; do whatever is necessary to keep the region active in
  ;; Lucid. ignore byte-compiler warnings you might see
  (and (boundp 'zmacs-region-stays)
       (setq zmacs-region-stays t)))
Eric S. Raymond's avatar
Eric S. Raymond committed
275 276

(defvar simula-mode-abbrev-table nil
Richard M. Stallman's avatar
Richard M. Stallman committed
277
  "Abbrev table in SIMULA mode buffers")
Eric S. Raymond's avatar
Eric S. Raymond committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292


(defun simula-mode ()
  "Major mode for editing SIMULA code.
\\{simula-mode-map}
Variables controlling indentation style:
 simula-tab-always-indent
    Non-nil means TAB in SIMULA mode should always reindent the current line,
    regardless of where in the line point is when the TAB command is used.
 simula-indent-level
    Indentation of SIMULA statements with respect to containing block.
 simula-substatement-offset
    Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.
 simula-continued-statement-offset 3
    Extra indentation for lines not starting a statement or substatement,
Richard M. Stallman's avatar
Richard M. Stallman committed
293
    e.g. a nested FOR-loop.  If value is a list, each line in a multiple-
Eric S. Raymond's avatar
Eric S. Raymond committed
294 295 296
    line continued statement will have the car of the list extra indentation
    with respect to the previous line of the statement.
 simula-label-offset -4711
Richard M. Stallman's avatar
Richard M. Stallman committed
297
    Offset of SIMULA label lines relative to usual indentation.
Eric S. Raymond's avatar
Eric S. Raymond committed
298 299
 simula-if-indent '(0 . 0)
    Extra indentation of THEN and ELSE with respect to the starting IF.
300
    Value is a cons cell, the car is extra THEN indentation and the cdr
Richard M. Stallman's avatar
Richard M. Stallman committed
301
    extra ELSE indentation.  IF after ELSE is indented as the starting IF.
Eric S. Raymond's avatar
Eric S. Raymond committed
302 303
 simula-inspect-indent '(0 . 0)
    Extra indentation of WHEN and OTHERWISE with respect to the
Richard M. Stallman's avatar
Richard M. Stallman committed
304
    corresponding INSPECT.  Value is a cons cell, the car is
305
    extra WHEN indentation and the cdr extra OTHERWISE indentation.
Eric S. Raymond's avatar
Eric S. Raymond committed
306
 simula-electric-indent nil
Richard M. Stallman's avatar
Richard M. Stallman committed
307
    If this variable is non-nil, `simula-indent-line'
Eric S. Raymond's avatar
Eric S. Raymond committed
308 309
    will check the previous line to see if it has to be reindented.
 simula-abbrev-keyword 'upcase
Richard M. Stallman's avatar
Richard M. Stallman committed
310 311 312
    Determine how SIMULA keywords will be expanded.  Value is one of
    the symbols `upcase', `downcase', `capitalize', (as in) `abbrev-table',
    or nil if they should not be changed.
Eric S. Raymond's avatar
Eric S. Raymond committed
313 314
 simula-abbrev-stdproc 'abbrev-table
    Determine how standard SIMULA procedure and class names will be
Richard M. Stallman's avatar
Richard M. Stallman committed
315 316
    expanded.  Value is one of the symbols `upcase', `downcase', `capitalize',
    (as in) `abbrev-table', or nil if they should not be changed.
Eric S. Raymond's avatar
Eric S. Raymond committed
317 318 319 320 321

Turning on SIMULA mode calls the value of the variable simula-mode-hook
with no arguments, if that value is non-nil

Warning: simula-mode-hook should not read in an abbrev file without calling
322
the function simula-install-standard-abbrevs afterwards, preferably not
Eric S. Raymond's avatar
Eric S. Raymond committed
323 324 325 326 327 328 329 330
at all."
  (interactive)
  (kill-all-local-variables)
  (use-local-map simula-mode-map)
  (setq major-mode 'simula-mode)
  (setq mode-name "SIMULA")
  (make-local-variable 'comment-column)
  (setq comment-column 40)
331 332
;  (make-local-variable 'end-comment-column)
;  (setq end-comment-column 75)
Eric S. Raymond's avatar
Eric S. Raymond committed
333 334
  (set-syntax-table simula-mode-syntax-table)
  (make-local-variable 'paragraph-start)
335
  (setq paragraph-start "[ \t]*$\\|\\f")
Eric S. Raymond's avatar
Eric S. Raymond committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  (make-local-variable 'paragraph-separate)
  (setq paragraph-separate paragraph-start)
  (make-local-variable 'indent-line-function)
  (setq indent-line-function 'simula-indent-line)
  (make-local-variable 'require-final-newline)
  (setq require-final-newline t)
  (make-local-variable 'comment-start)
  (setq comment-start "! ")
  (make-local-variable 'comment-end)
  (setq comment-end " ;")
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "!+ *")
  (make-local-variable 'parse-sexp-ignore-comments)
  (setq parse-sexp-ignore-comments nil)
  (make-local-variable 'comment-multi-line)
  (setq comment-multi-line t)
  (if simula-mode-abbrev-table
      ()
    (if simula-abbrev-file
	(read-abbrev-file simula-abbrev-file)
      (define-abbrev-table 'simula-mode-abbrev-table ()))
    (let (abbrevs-changed)
      (simula-install-standard-abbrevs)))
  (setq local-abbrev-table simula-mode-abbrev-table)
  (abbrev-mode 1)
  (run-hooks 'simula-mode-hook))


364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
(defun simula-indent-exp ()
  "Indent SIMULA expression following point."
  (interactive)
  (let ((here (point))
	(simula-electric-indent nil)
	end)
    (simula-skip-comment-forward)
    (if (eobp)
	(goto-char here)
      (unwind-protect
	  (progn
	    (simula-next-statement 1)
	    (setq end (point-marker))
	    (simula-previous-statement 1)
	    (beginning-of-line)
	    (while (< (point) end)
	      (if (not (looking-at "[ \t]*$"))
		  (simula-indent-line))
	      (forward-line 1)))
	(and end (set-marker end nil))))))
      
Eric S. Raymond's avatar
Eric S. Raymond committed
385 386

(defun simula-indent-line ()
Richard M. Stallman's avatar
Richard M. Stallman committed
387 388
  "Indent this line as SIMULA code.
If `simula-electric-indent' is non-nil, indent previous line if necessary."
Eric S. Raymond's avatar
Eric S. Raymond committed
389 390 391 392
  (let ((origin (- (point-max) (point)))
	(indent (simula-calculate-indent))
	(case-fold-search t))
    (unwind-protect
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	(if simula-electric-indent
	    (progn
	      ;;
	      ;; manually expand abbrev on last line, if any
	      ;;
	      (end-of-line 0)
	      (expand-abbrev)
	      ;; now maybe we should reindent that line
	      (beginning-of-line)
	      (skip-chars-forward " \t\f")
	      (if (and
		   (looking-at
		    "\\(end\\|if\\|then\\|else\\|when\\|otherwise\\)\\>")
		   (not (simula-context)))
		  ;; yes - reindent
		  (let ((post-indent (simula-calculate-indent)))
		    (if (eq (current-indentation) post-indent)
			()
		      (delete-horizontal-space)
		      (indent-to post-indent))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
413 414 415 416 417 418 419 420 421
      (goto-char (- (point-max) origin))
      (if (eq (current-indentation) indent)
	  (back-to-indentation)
	(delete-horizontal-space)
	(indent-to indent)))))


(defun simula-indent-command (&optional whole-exp)
  "Indent current line as SIMULA code, or insert TAB character.
Richard M. Stallman's avatar
Richard M. Stallman committed
422
If `simula-tab-always-indent' is non-nil, always indent current line.
Eric S. Raymond's avatar
Eric S. Raymond committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
Otherwise, indent only if point is before any non-whitespace
character on the line.

A numeric argument, regardless of its value, means indent rigidly
all the lines of the SIMULA statement after point so that this line
becomes properly indented.
The relative indentation among the lines of the statement are preserved."
  (interactive "P")
  (let ((case-fold-search t))
    (if (or whole-exp simula-tab-always-indent
	    (save-excursion
	      (skip-chars-backward " \t\f")
	      (bolp)))
	;; reindent current line
	(let ((indent (save-excursion
			(beginning-of-line)
			(simula-calculate-indent)))
	      (current (current-indentation))
	      (origin (- (point-max) (point)))
	      (bol (save-excursion
		     (skip-chars-backward " \t\f")
		     (bolp)))
	      beg end)
	  (unwind-protect
	      (if (eq current indent)
		  (if (save-excursion
			(skip-chars-backward " \t\f")
			(bolp))
		      (back-to-indentation))
		(beginning-of-line)
		(delete-horizontal-space)
		(indent-to indent))
	    (if (not bol)
		(goto-char (- (point-max) origin))))
	  (setq origin (point))
	  (if whole-exp
	      (save-excursion
		(beginning-of-line 2)
		(setq beg (point))
		(goto-char origin)
		(simula-next-statement 1)
		(setq end (point))
		(if (and (> end beg) (not (eq indent current)))
		    (indent-code-rigidly beg end (- indent current) "%")))))
      (insert-tab))))


(defun simula-context ()
Richard M. Stallman's avatar
Richard M. Stallman committed
471 472
  "Returns value according to syntactic SIMULA context of point.
    0    point inside COMMENT comment
Eric S. Raymond's avatar
Eric S. Raymond committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    1    point on SIMULA-compiler directive line
    2    point inside END comment
    3    point inside string
    4    point inside character constant
  nil    otherwise."
  ;; first, find out if this is a compiler directive line
  (if (save-excursion
	(beginning-of-line)
	(eq (following-char) ?%))
      ;; YES - return 1
      1
    (save-excursion
      ;; The current line is NOT a compiler directive line.
      ;; Now, the strategy is to search backward to find a semicolon
      ;; that is NOT inside a string. The point after semicolon MUST be
      ;; outside a comment, since semicolons are comment-ending and
      ;; comments are non-recursive. We take advantage of the fact
      ;; that strings MUST end on the same line as they started, so
      ;; that we can easily decide whether we are inside a string or not.
      (let (return-value (origin (point)))
	(skip-chars-backward "^;" (point-min))
	;; found semicolon or beginning of buffer
	(let (loopvalue (saved-point origin))
	  (while (and (not (bobp))
		      (if (progn
			    (beginning-of-line)
			    ;; compiler directive line? If so, cont searching..
			    (eq (following-char) ?%))
			  t
			(while (< (point) saved-point)
			  (skip-chars-forward "^;\"'")
			  (forward-char 1)
			  (cond
			   ((eq (preceding-char) ?\;)
			    (setq saved-point (point)))
			   ((eq (preceding-char) ?\")
			    (skip-chars-forward "^\";")
			    (if (eq (following-char) ?\;)
				(setq saved-point (point) loopvalue t)
			      (forward-char 1)))
			   (t
			    (if (eq (following-char) ?')
				(forward-char 1))
			    (skip-chars-forward "^';")
			    (if (eq (following-char) ?\;)
				(setq saved-point (point) loopvalue t)
			      (forward-char 1)))))
			loopvalue))
	    (backward-char 1)
	    (skip-chars-backward "^;")
	    (setq saved-point (point) loopvalue nil)))
	;; Now we are CERTAIN that we are outside comments and strings.
	;; The job now is to search forward again towards the origin
	;; skipping directives, comments and strings correctly,
	;; so that we know what context we are in when we find the origin.
	(while (and
		(< (point) origin)
		(re-search-forward
		 "\\<end\\>\\|!\\|\"\\|'\\|^%\\|\\<comment\\>" origin 'move))
	  (cond
	   ((memq (preceding-char) '(?d ?D))
	    (setq return-value 2)
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	    (while (and (re-search-forward
			 ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\|^%"
			 origin 'move)
			;; found another END?
			(or (memq (preceding-char) '(?d ?D))
			    ;; if directive, skip line
			    (and (eq (preceding-char) ?%)
				 (beginning-of-line 2))
			    ;; found other keyword, out of END comment
			    (setq return-value nil))))
	    (if (and (eq (char-syntax (preceding-char)) ?w)
		     (eq (char-syntax (following-char)) ?w))
		(save-excursion
		  (backward-word 1)
		  (if (looking-at "end\\>\\|else\\>\\|otherwise\\>\\|when\\>")
		      (setq return-value nil)))))
Eric S. Raymond's avatar
Eric S. Raymond committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	   ((memq (preceding-char) '(?! ?t ?T))
	    ; skip comment
	    (setq return-value 0)
	    (skip-chars-forward "^%;" origin)
	    (while (and return-value (< (point) origin))
	      (if (eq (following-char) ?\;)
		  (setq return-value nil)
		(if (bolp)
		    (beginning-of-line 2)	; skip directive inside comment
		  (forward-char 1))		; or single '%'
		(skip-chars-forward "^%;" origin))))
	   ((eq (preceding-char) ?\")
	    (if (not (search-forward "\"" origin 'move))
		(setq return-value 3)))
	   ((eq (preceding-char) ?\')
	    (if (or (eq (point) origin) (eobp))
		(setq return-value 4)
	      (forward-char 1)
	      (if (not (search-forward "'" origin 'move))
		  (setq return-value 4))))
	   ;; compiler directive line - skip
	   (t (beginning-of-line 2))))
	return-value)
      )))


(defun simula-electric-label ()
Richard M. Stallman's avatar
Richard M. Stallman committed
578
  "If this is a label that starts the line, reindent the line."
Eric S. Raymond's avatar
Eric S. Raymond committed
579 580 581 582 583 584
  (interactive)
  (expand-abbrev)
  (insert ?:)
  (let ((origin (- (point-max) (point)))
	(case-fold-search t)
	;; don't mix a label with an assignment operator := :-
585 586
	;; therefore take a peek at next typed character...
	(next-char (read-event)))
Eric S. Raymond's avatar
Eric S. Raymond committed
587
    (unwind-protect
588 589
	(setq unread-command-events (append unread-command-events
					    (list next-char)))
Eric S. Raymond's avatar
Eric S. Raymond committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	;; Problem: find out if character just read is a command char
	;; that would insert something after ':' making it a label.
	;; At least \n, \r (and maybe \t) falls into this category.
	;; This is a real crock, it depends on traditional keymap
	;; bindings, that is, printing characters doing self-insert,
	;; and no other command sequence inserting '-' or '='.
	;; simula-electric-label can be easily fooled...
	(if (and (not (memq next-char '(?= ?-)))
		 (or (memq next-char '(?\n ?\r))
		     (and (eq next-char ?\t)
			  simula-tab-always-indent)
		     (not (memq (following-char) '(?= ?-))))
		 (not (simula-context))
		 ;; label?
		 (progn
		   (backward-char 1)
		   (skip-chars-backward " \t\f")
		   (skip-chars-backward "a-zA-Z0-9_")
		   (if (looking-at "virtual\\>")
		       nil
		     (skip-chars-backward " \t\f")
		     (bolp))))
	    (let ((amount (simula-calculate-indent)))
	      (delete-horizontal-space)
	      (indent-to amount)))
      (goto-char (- (point-max) origin)))))
	

(defun simula-backward-up-level (count)
  "Move backward up COUNT block levels.
Richard M. Stallman's avatar
Richard M. Stallman committed
620
If COUNT is negative, move forward up block level instead."
Eric S. Raymond's avatar
Eric S. Raymond committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
  (interactive "p")
  (let ((origin (point))
	(case-fold-search t))
    (condition-case ()
	(if (> count 0)
	    (while (> count 0)
	      (re-search-backward "\\<begin\\>\\|\\<end\\>")
	      (if (not (simula-context))
		  (setq count (if (memq (following-char) '(?b ?B))
				  (1- count)
				(1+ count)))))
	  (while (< count 0)
	    (re-search-forward "\\<begin\\>\\|\\<end\\>")
	    (backward-word 1)
	    (if (not (simula-context))
		(setq count (if (memq (following-char) '(?e ?E))
				(1+ count)
			      (1- count))))
	    (backward-word -1)))
      ;; If block level not found, jump back to origin and signal an error
      (error (progn
	       (goto-char origin)
	       (error "No higher block level")))
      (quit (progn
	      (goto-char origin)
	      (signal 'quit nil))))))


(defun simula-forward-down-level (count)
  "Move forward down COUNT block levels.
Richard M. Stallman's avatar
Richard M. Stallman committed
651
If COUNT is negative, move backward down block level instead."
Eric S. Raymond's avatar
Eric S. Raymond committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
  (interactive "p")
  ;; When we search for a deeper block level, we must never
  ;; get out of the block where we started -> count >= start-count
  (let ((start-count count)
	(origin (point))
	(case-fold-search t))
    (condition-case ()
	(if (< count 0)
	    (while (< count 0)
	      (re-search-backward "\\<begin\\>\\|\\<end\\>")
	      (if (not (simula-context))
		  (setq count (if (memq (following-char) '(?e ?E))
				  (1+ count)
				(1- count))))
	      (if (< count start-count) (signal 'error nil)))
	  (while (> count 0)
	    (re-search-forward "\\<begin\\>\\|\\<end\\>")
	    (backward-word 1)
	    (if (not (simula-context))
		(setq count (if (memq (following-char) '(?b ?B))
				(1- count)
			      (1+ count))))
	    (backward-word -1)
	    ;; deeper level has to be found within starting block
	    (if (> count start-count) (signal 'error nil))))
      ;; If block level not found, jump back to origin and signal an error
      (error (progn
	       (goto-char origin)
	       (error "No containing block level")))
      (quit (progn
	      (goto-char origin)
	      (signal 'quit nil))))))

     
(defun simula-previous-statement (count)
  "Move backward COUNT statements.
Richard M. Stallman's avatar
Richard M. Stallman committed
688
If COUNT is negative, move forward instead."
Eric S. Raymond's avatar
Eric S. Raymond committed
689 690 691 692 693 694 695
  (interactive "p")
  (if (< count 0)
      (simula-next-statement (- count))
    (let (status
	  (case-fold-search t)
	  (origin (point)))
      (condition-case ()
696
	  ;; 
Eric S. Raymond's avatar
Eric S. Raymond committed
697 698 699 700 701 702 703 704
	  (progn
	    (simula-skip-comment-backward)
	    (if (memq (preceding-char) '(?n ?N))
		(progn
		  (backward-word 1)
		  (if (not (looking-at "\\<begin\\>"))
		      (backward-word -1)))
	      (if (eq (preceding-char) ?\;)
705 706
		  (backward-char 1))
	      )
Eric S. Raymond's avatar
Eric S. Raymond committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	    (while (and (natnump (setq count (1- count)))
			(setq status (simula-search-backward
				      ";\\|\\<begin\\>" nil 'move))))
	    (if status
		(progn
		  (if (eq (following-char) ?\;)
		      (forward-char 1)
		    (backward-word -1))))
	    (simula-skip-comment-forward))
	(error (progn (goto-char origin)
		      (error "Incomplete statement (too many ENDs)")))
	(quit (progn (goto-char origin) (signal 'quit nil)))))))


(defun simula-next-statement (count)
Richard M. Stallman's avatar
Richard M. Stallman committed
722 723
  "Move forward COUNT statements.
If COUNT is negative, move backward instead."
Eric S. Raymond's avatar
Eric S. Raymond committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  (interactive "p")
  (if (< count 0)
      (simula-previous-statement (- count))
    (let (status
	  (case-fold-search t)
	  (origin (point)))
      (condition-case ()
	  (progn
	    (simula-skip-comment-forward)
	    (if (looking-at "\\<end\\>") (forward-word 1))
	    (while (and (natnump (setq count (1- count)))
			(setq status (simula-search-forward
				      ";\\|\\<end\\>" (point-max) 'move))))
	    (if (and status (/= (preceding-char) ?\;))
		(progn
		  (backward-word 1)
		  (simula-skip-comment-backward))))
	(error (progn (goto-char origin)
		      (error "Incomplete statement (too few ENDs)")))
 	(quit (progn (goto-char origin) (signal 'quit nil)))))))


746
(defun simula-skip-comment-backward (&optional stop-at-end)
Richard M. Stallman's avatar
Richard M. Stallman committed
747
  "Search towards bob to find first char that is outside a comment."
Eric S. Raymond's avatar
Eric S. Raymond committed
748 749 750 751 752 753 754 755
  (interactive)
  (catch 'simula-out
    (let (context)
      (while t
	(skip-chars-backward " \t\n\f")
	(if (eq (preceding-char) ?\;)
	    (save-excursion
	      (backward-char 1)
756 757 758
	      (setq context (simula-context))
	      (if (and stop-at-end (eq context 2))
		  (setq context nil)))
Eric S. Raymond's avatar
Eric S. Raymond committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	  (setq context (simula-context)))
	(cond
	 ((memq context '(nil 3 4))
	  ;; check to see if we found a label
	  (if (and (eq (preceding-char) ?:)
		   (not (memq (following-char) '(?- ?=)))
		   (save-excursion
		     (skip-chars-backward ": \t\fa-zA-Z0-9_")
		     (not (looking-at "virtual\\>"))))
	      (skip-chars-backward ": \t\fa-zA-Z0-9_")
	    (throw 'simula-out nil)))
	 ((eq context 0)
	  ;; since we are inside a comment, it must start somewhere!
	  (while (and (re-search-backward "!\\|\\<comment\\>")
		      (memq (simula-context) '(0 1)))))
	 ((eq context 1)
775
	  (beginning-of-line)
Eric S. Raymond's avatar
Eric S. Raymond committed
776
	  (if (bobp)
777 778
	      (throw 'simula-out nil)
	    (backward-char)))
Eric S. Raymond's avatar
Eric S. Raymond committed
779 780 781 782 783 784 785 786 787 788
	 ((eq context 2)
	  ;; an END-comment must belong to an END
	  (re-search-backward "\\<end\\>")
	  (forward-word 1)
	  (throw 'simula-out nil))
	 ;; should be impossible to get here..
	 )))))


(defun simula-skip-comment-forward ()
Richard M. Stallman's avatar
Richard M. Stallman committed
789
  "Search towards eob to find first char that is outside a comment."
Eric S. Raymond's avatar
Eric S. Raymond committed
790 791 792 793 794
  ;; this function assumes we start with point .outside a comment
  (interactive)
  (catch 'simula-out
    (while t
      (skip-chars-forward " \t\n\f")
795 796
      ;; BUG: the following (0 2) branches don't take into account intermixing
      ;; directive lines
Eric S. Raymond's avatar
Eric S. Raymond committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
      (cond
       ((looking-at "!\\|\\<comment\\>")
	(search-forward ";" nil 'move))
       ((and (bolp) (eq (following-char) ?%))
	(beginning-of-line 2))
       ((and (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]")
	     (not (looking-at "virtual\\>")))
	(skip-chars-forward "a-zA-Z0-9_ \t\f:"))
       (t
	(throw 'simula-out t))))))


(defun simula-forward-up-level ()
  (let ((continue-loop t)
	(origin (point))
	(case-fold-search t)
	return-value
	temp)
    (while continue-loop
      (if (re-search-backward "\\<begin\\>\\|\\<end\\>" (point-min) 'move)
	  (setq temp (simula-context)
	      return-value (and (memq (preceding-char) '(?d ?D))
				(memq temp '(nil 2)))
	      continue-loop (and (not return-value)
				 (simula-forward-up-level)))
	(setq continue-loop nil)))
    (if return-value
	t
      (goto-char origin)
      nil)))


(defun simula-calculate-indent ()
  (save-excursion
    (let ((where (simula-context))
	  (origin (point))
	  (indent 0)
	  continued
	  start-line
	  temp
	  found-end
	  prev-cont)
      (cond
       ((eq where 0)
	;;
	;; Comment.
	;; If comment started on previous non-blank line, indent to the
	;; column where the comment started, else indent as that line.
	;;
	(skip-chars-backward " \t\n\f")
	(while (and (not (bolp)) (eq (simula-context) 0))
	  (re-search-backward "^\\|!\\|\\<comment\\>"))
	(skip-chars-forward " \t\n\f")
	(prog1
	    (current-column)
	  (goto-char origin)))
853 854 855 856 857
       ((eq where 1)
	;;
	;; Directive. Always 0.
	;;
	0)
Eric S. Raymond's avatar
Eric S. Raymond committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
       ;;
       ;; Detect missing string delimiters
       ;;
       ((eq where 3)
	(error "Inside string"))
       ((eq where 4)
	(error "Inside character constant"))
       ;;
       ;; check to see if inside ()'s
       ;;
       ((setq temp (simula-inside-parens))
	temp)
       ;;
       ;; Calculate non-comment indentation
       (t
	;; first, find out if this line starts with something that needs
874
	;; special indentation (END/IF/THEN/ELSE/WHEN/OTHERWISE or label)
Eric S. Raymond's avatar
Eric S. Raymond committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
	;;
	(skip-chars-forward " \t\f")
	(cond
	 ;;
	 ;; END
	 ;;
	 ((looking-at "end\\>")
	  (setq indent (- simula-indent-level)
		found-end t))
	 ;;
	 ;; IF/THEN/ELSE
	 ;;
	 ((looking-at "if\\>\\|then\\>\\|else\\>")
	  ;; search for the *starting* IF
	  (cond
	   ((memq (following-char) '(?T ?t))
	    (setq indent (car simula-if-indent)))
	   ((memq (following-char) '(?E ?e))
	    (setq indent (cdr simula-if-indent)))
	   (t
	    (forward-word 1)
	    (setq indent 0)))
	  (simula-find-if))
	 ;;
	 ;; WHEN/OTHERWISE
	 ;;
	 ((looking-at "when\\>\\|otherwise\\>")
	  ;; search for corresponding INSPECT
	  (if (memq (following-char) '(?W ?w))
	      (setq indent (car simula-inspect-indent))
	    (setq indent (cdr simula-inspect-indent)))
	  (simula-find-inspect))
	 ;;
	 ;; label:
	 ;;
	 ((and (not (looking-at "virtual\\>"))
	       (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]"))
	  (setq indent simula-label-offset)))
	;; find line with non-comment text
914
	(simula-skip-comment-backward 'dont-skip-end)
Eric S. Raymond's avatar
Eric S. Raymond committed
915 916 917 918
	(if (and found-end
		 (not (eq (preceding-char) ?\;))
		 (if (memq (preceding-char) '(?N ?n))
		     (save-excursion
919
		       (backward-word 1)
Eric S. Raymond's avatar
Eric S. Raymond committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		       (not (looking-at "begin\\>")))
		   t))
	    (progn
	      (simula-previous-statement 1)
	      (simula-skip-comment-backward)))
	(setq start-line
	      (save-excursion (beginning-of-line) (point))
	      ;; - perhaps this is a continued statement
	      continued
	      (save-excursion
		(and (not (bobp))
		     ;; (not found-end)
		     (if (eq (char-syntax (preceding-char)) ?w)
			 (progn
			   (backward-word 1)
			   (not (looking-at
				 "begin\\|then\\|else\\|when\\|otherwise\\|do"
				 )))
		       (not (memq (preceding-char) '(?: ?\;)))))))
	;;
	;; MAIN calculation loop - count BEGIN/DO etc.
	;;
	(while (not (bolp))
	  (if (re-search-backward
	       ";\\|\\<\\(begin\\|end\\|if\\|else\\|then\\|when\\|otherwise\\|do\\)\\>"
	       start-line 'move)
	      (if (simula-context)
		  ();; found something in a comment/string - ignore
		(setq temp (following-char))
		(cond
		 ((eq temp ?\;)
		  (simula-previous-statement 1))
		 ((looking-at "begin\\>")
		  (setq indent (+ indent simula-indent-level)))
		 ((looking-at "end\\>")
		  (forward-word 1)
		  (simula-previous-statement 1))
		 ((looking-at "do\\>")
		  (setq indent (+ indent simula-substatement-offset))
		  (simula-find-do-match))
		 ((looking-at "\\(if\\|then\\|else\\)\\>")
		  (if (memq temp '(?I ?i))
		      (forward-word 1)
		    (setq indent (+ indent
				    simula-substatement-offset
				    (if (memq temp '(?T ?t))
					(car simula-if-indent)
				      (cdr simula-if-indent)))))
		  (simula-find-if))
		 ((looking-at "\\<when\\>\\|\\<otherwise\\>")
		  (setq indent (+ indent
				  simula-substatement-offset
				  (if (memq temp '(?W ?w))
				      (car simula-if-indent)
				    (cdr simula-if-indent))))
		  (simula-find-inspect)))
		;; found the start of a [sub]statement
977
		;; add indentation for continued statement
Eric S. Raymond's avatar
Eric S. Raymond committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
		(if continued
		    (setq indent
			  (+ indent
			     (if (listp simula-continued-statement-offset)
				 (car simula-continued-statement-offset)
			       simula-continued-statement-offset))))
		(setq start-line
		      (save-excursion (beginning-of-line) (point))
		      continued nil))
	    ;; search failed .. point is at beginning of line
	    ;; determine if we should continue searching
	    ;; (at or before comment or label)
	    ;; temp = t means finished
	    (setq temp
		  (and (not (simula-context))			
		       (save-excursion
			 (skip-chars-forward " \t\f")
			 (or (looking-at "virtual")
			     (not
			      (looking-at
			       "!\\|comment\\>\\|[a-z0-9_]*[ \t\f]*:[^-=]")))))
		  prev-cont continued)
	    ;; if we are finished, find current line's indentation
	    (if temp
		(setq indent (+ indent (current-indentation))))
	    ;; find next line with non-comment SIMULA text
	    ;; maybe indent extra if statement continues
	    (simula-skip-comment-backward)
	    (setq continued
		  (and (not (bobp))
		       (if (eq (char-syntax (preceding-char)) ?w)
			   (save-excursion
			     (backward-word 1)
			     (not (looking-at
				   "begin\\|then\\|else\\|when\\|otherwise\\|do")))
			 (not (memq (preceding-char) '(?: ?\;))))))
	    ;; if we the state of the continued-variable
1015
	    ;; changed, add indentation for continued statement
Eric S. Raymond's avatar
Eric S. Raymond committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	    (if (or (and prev-cont (not continued))
		    (and continued
			 (listp simula-continued-statement-offset)))
		(setq indent
		      (+ indent
			 (if (listp simula-continued-statement-offset)
			     (car simula-continued-statement-offset)
			   simula-continued-statement-offset))))
	    ;; while ends if point is at beginning of line at loop test
	    (if (not temp)
		(setq start-line (save-excursion (beginning-of-line) (point)))
	      (beginning-of-line))))
        ;;
1029
	;; return indentation
Eric S. Raymond's avatar
Eric S. Raymond committed
1030 1031 1032 1033 1034
	;;
	indent)))))


(defun simula-find-if ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1035
  "Find starting IF of a IF-THEN[-ELSE[-IF-THEN...]] statement."
Eric S. Raymond's avatar
Eric S. Raymond committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
  (catch 'simula-out
    (while t
      (if (and (simula-search-backward "\\<if\\>\\|;\\|\\<begin\\>"nil t)
	       (memq (following-char) '(?I ?i)))
	  (save-excursion
	    ;;
	    ;; find out if this IF was really the start of the IF statement
	    ;;
	    (simula-skip-comment-backward)
	    (if (and (eq (char-syntax (preceding-char)) ?w)
		     (progn
		       (backward-word 1)
		       (looking-at "else\\>")))
		()
	      (throw 'simula-out t)))
	(if (not (looking-at "\\<if\\>"))
	    (error "Missing IF or misplaced BEGIN or ';' (can't find IF)")
	  ;;
	  ;; we were at the starting IF in the first place..
	  ;;
	  (throw 'simula-out t))))))


(defun simula-find-inspect ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1060
  "Find INSPECT matching WHEN or OTHERWISE."
Eric S. Raymond's avatar
Eric S. Raymond committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
  (catch 'simula-out
    (let ((level 0))
      ;;
      ;; INSPECTs can be nested, have to find the corresponding one
      ;;
      (while t
	(if (and (simula-search-backward "\\<inspect\\>\\|\\<otherwise\\>\\|;"
					  nil t)
		 (/= (following-char) ?\;))
	    (if (memq (following-char) '(?O ?o))
		(setq level (1+ level))
	      (if (zerop level)
		  (throw 'simula-out t)
		(setq level (1- level))))
	  (error "Missing INSPECT or misplaced ';' (can't find INSPECT)"))))))


(defun simula-find-do-match ()
  "Find keyword matching DO: FOR, WHILE, INSPECT or WHEN"
  (while (and (re-search-backward
	       "\\<\\(do\\|for\\|while\\|inspect\\|when\\|end\\|begin\\)\\>\\|;"
	       nil 'move)
	      (simula-context)))
  (if (and (looking-at "\\<\\(for\\|while\\|inspect\\|when\\)\\>")
	   (not (simula-context)))
      () ;; found match
    (error "No matching FOR, WHILE or INSPECT for DO, or misplaced ';'")))


(defun simula-inside-parens ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1091
  "Return position after `(' on line if inside parentheses, nil otherwise."
Eric S. Raymond's avatar
Eric S. Raymond committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  (save-excursion
    (let ((parlevel 0))
      (catch 'simula-out
	(while t
	  (if (re-search-backward "(\\|)\\|;" nil t)
	      (if (eq (simula-context) nil)
		  ;; found something - check it out
		  (cond
		   ((eq (following-char) ?\;)
		    (if (zerop parlevel)
			(throw 'simula-out nil)
1103
		      (error "Parenthesis mismatch or misplaced ';'")))
Eric S. Raymond's avatar
Eric S. Raymond committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
		   ((eq (following-char) ?\()
		    (if (zerop parlevel)
			(throw 'simula-out (1+ (current-column)))
		      (setq parlevel (1- parlevel))))
		   (t (setq parlevel (1+ parlevel))))
		);; nothing - inside comment or string
	    ;; search failed
	    (throw 'simula-out nil)))))))


(defun simula-goto-definition ()
  "Goto point of definition of variable, procedure or class."
  (interactive))


(defun simula-expand-stdproc ()
  (if (or (not simula-abbrev-stdproc) (simula-context))
      (unexpand-abbrev)
    (cond
     ((eq simula-abbrev-stdproc 'upcase) (upcase-word -1))
     ((eq simula-abbrev-stdproc 'downcase) (downcase-word -1))
1125 1126 1127 1128 1129 1130 1131 1132
     ((eq simula-abbrev-stdproc 'capitalize) (capitalize-word -1))
     ((eq simula-abbrev-stdproc 'abbrev-table)
      ;; If not in lowercase, expansions are always capitalized.
      ;; We then want to replace with the exact expansion.
      (if (equal (symbol-name last-abbrev) last-abbrev-text)
	  ()
	(downcase-word -1)
	(expand-abbrev))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
1133 1134 1135 1136 1137 1138 1139 1140


(defun simula-expand-keyword ()
  (if (or (not simula-abbrev-keyword) (simula-context))
      (unexpand-abbrev)
    (cond
     ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
     ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
1141 1142 1143 1144 1145 1146
     ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1))
     ((eq simula-abbrev-stdproc 'abbrev-table)
      (if (equal (symbol-name last-abbrev) last-abbrev-text)
	  ()
	(downcase-word -1)
	(expand-abbrev))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
1147 1148 1149


(defun simula-electric-keyword ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1150
  "Expand SIMULA keyword.  If it starts the line, reindent."
Eric S. Raymond's avatar
Eric S. Raymond committed
1151 1152
  ;; redisplay
  (let ((show-char (eq this-command 'self-insert-command)))
1153
    ;; If the abbrev expansion results in reindentation, the user may have
Eric S. Raymond's avatar
Eric S. Raymond committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    ;; to wait some time before the character he typed is displayed
    ;; (the char causing the expansion is inserted AFTER the hook function
    ;; is called). This is annoying in case of normal characters.
    ;; However, if the user pressed a key bound to newline, it is better
    ;; to have the line inserted after the begin-end match.
    (if show-char
	(progn
	  (insert-char last-command-char 1)
	  (sit-for 0)
	  (backward-char 1)))
    (if (let ((where (simula-context))
	      (case-fold-search t))
	  (if where
	      (if (and (eq where 2) (eq (char-syntax (preceding-char)) ?w))
		  (save-excursion
		    (backward-word 1)
		    (not (looking-at "end\\>"))))))
	(unexpand-abbrev)
      (cond
       ((not simula-abbrev-keyword) (unexpand-abbrev))
       ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
       ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
       ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))
      (let ((pos (- (point-max) (point)))
	    (case-fold-search t)
	    null)
	(condition-case null
	    (progn
	      ;; check if the expanded word is on the beginning of the line.
	      (if (and (eq (char-syntax (preceding-char)) ?w)
		       (progn
			 (backward-word 1)
			 (if (looking-at "end\\>")
			     (save-excursion
			       (simula-backward-up-level 1)
			       (if (pos-visible-in-window-p)
				   (sit-for 1)
1191
				 (message "Matches %s"
Eric S. Raymond's avatar
Eric S. Raymond committed
1192 1193
					  (buffer-substring
					   (point)
1194
					   (+ (point) (window-width)))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
			 (skip-chars-backward " \t\f")
			 (bolp)))
		  (let ((indent (simula-calculate-indent)))
		    (if (eq indent (current-indentation))
			()
		      (delete-horizontal-space)
		      (indent-to indent)))
		(skip-chars-forward " \t\f"))
	      ;; check for END - blow whistles and ring bells

	      (goto-char (- (point-max) pos))
	      (if show-char
		  (delete-char 1)))
	  (quit (goto-char (- (point-max) pos))))))))


1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
(defun simula-search-backward (regexp &optional bound noerror)
  "Search backward from point for regular expression REGEXP, ignoring matches
found inside SIMULA comments, string literals, and BEGIN..END blocks.
Set point to the end of the occurrence found, and return point.
An optional second argument BOUND bounds the search, it is a buffer position.
The match found must not extend after that position.  Optional third argument
NOERROR, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil."
  (let (begin end context (comb-regexp (concat regexp "\\|\\<end\\>"))
	      match (start-point (point)))
    (catch 'simula-backward
      (while (re-search-backward comb-regexp bound 1)
	;; We have a match, check SIMULA context at match-beginning
	;; to see if we are outside comments etc.
	;; Set MATCH to t if we found a true match,
	;; set MATCH to 'BLOCK if we found a BEGIN..END block,
	;; else set MATCH to nil.
	(save-match-data
	  (setq context (simula-context))
	  (cond
	   ((eq context nil)
	    (setq match (if (looking-at regexp) t 'BLOCK)))
;;; A comment-ending semicolon is part of the comment, and shouldn't match.
;;;	     ((eq context 0)
;;;	      (setq match (if (eq (following-char) ?\;) t nil)))
	   ((eq context 2)
	    (setq match (if (and (looking-at regexp)
				 (looking-at ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>"))
			    t
			  (if (looking-at "\\<end\\>") 'BLOCK nil))))
	   (t (setq match nil))))
	;; Exit if true match
	(if (eq match t) (throw 'simula-backward (point)))
	(if (eq match 'BLOCK)
	;; We found the END of a block
	    (let ((level 0))
	      (while (natnump level)
		  (if (re-search-backward "\\<begin\\>\\|\\<end\\>" bound 1)
		      (let ((context (simula-context)))
			;;    We found a BEGIN -> decrease level count
			(cond ((and (eq context nil)
				   (memq (following-char) '(?b ?B)))
			       (setq level (1- level)))
			      ;; END -> increase level count
			      ((and (memq context '(nil 2))
				    (memq (following-char) '(?e ?E)))
			       (setq level (1+ level)))))
		    ;; Block search failed.  Action depends on noerror.
		    (if (or (not noerror) (eq noerror t))
			(goto-char start-point))
		    (if (not noerror)
			(signal 'search-failed (list regexp)))
		    (throw 'simula-backward nil))))))
      ;; Search failed.  Action depends on noerror.
      (if (or (not noerror) (eq noerror t))
	  (goto-char start-point))
      (if noerror
	  nil
	(signal 'search-failed (list regexp))))))


(defun simula-search-forward (regexp &optional bound noerror)
  "Search forward from point for regular expression REGEXP, ignoring matches
found inside SIMULA comments, string literals, and BEGIN..END blocks.
Set point to the end of the occurrence found, and return point.
An optional second argument BOUND bounds the search, it is a buffer position.
The match found must not extend after that position.  Optional third argument
NOERROR, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil."
  (let (begin end context (comb-regexp (concat regexp "\\|\\<begin\\>"))
	      match (start-point (point)))
    (catch 'simula-forward
      (while (re-search-forward comb-regexp bound 1)
	;; We have a match, check SIMULA context at match-beginning
	;; to see if we are outside comments.
	;; Set MATCH to t if we found a true match,
	;; set MATCH to 'BLOCK if we found a BEGIN..END block,
	;; else set MATCH to nil.
	(save-match-data
	  (save-excursion
	    (goto-char (match-beginning 0))
	    (setq context (simula-context))
	    (cond
	     ((not context)
	      (setq match (if (looking-at regexp) t 'BLOCK)))
;;; A comment-ending semicolon is part of the comment, and shouldn't match.
;;;	     ((eq context 0)
;;;	      (setq match (if (eq (following-char) ?\;) t nil)))
	     ((eq context 2)
	      (setq match (if (and (looking-at regexp)
				   (looking-at ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>")) t nil)))
	     (t (setq match nil)))))
	;; Exit if true match
	(if (eq match t) (throw 'simula-forward (point)))
	(if (eq match 'BLOCK)
	;; We found the BEGINning of a block
	    (let ((level 0))
	      (while (natnump level)
		  (if (re-search-forward "\\<begin\\>\\|\\<end\\>" bound 1)
		      (let ((context (simula-context)))
			;;    We found a BEGIN -> increase level count
			(cond ((eq context nil) (setq level (1+ level)))
			      ;; END -> decrease level count
			      ((and (eq context 2)
				    ;; Don't match BEGIN inside END comment
				    (memq (preceding-char) '(?d ?D)))
			       (setq level (1- level)))))
		    ;; Block search failed.  Action depends on noerror.
		    (if (or (not noerror) (eq noerror t))
			(goto-char start-point))
		    (if (not noerror)
			(signal 'search-failed (list regexp)))
		    (throw 'simula-forward nil))))))
      ;; Search failed.  Action depends on noerror.
      (if (or (not noerror) (eq noerror t))
	  (goto-char start-point))
      (if noerror
	  nil
	(signal 'search-failed (list regexp))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
1330 1331 1332

  
(defun simula-install-standard-abbrevs ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1333
  "Define Simula keywords, procedures and classes in local abbrev table."
Eric S. Raymond's avatar
Eric S. Raymond committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
  ;; procedure and class names are as of the SIMULA 87 standard.
  (interactive)
  (mapcar (function (lambda (args)
		      (apply 'define-abbrev simula-mode-abbrev-table args)))
	  '(("abs" "Abs" simula-expand-stdproc)
	    ("accum" "Accum" simula-expand-stdproc)
	    ("activate" "ACTIVATE" simula-expand-keyword)
	    ("addepsilon" "AddEpsilon" simula-expand-stdproc)
	    ("after" "AFTER" simula-expand-keyword)
	    ("and" "AND" simula-expand-keyword)
	    ("arccos" "ArcCos" simula-expand-stdproc)
	    ("arcsin" "ArcSin" simula-expand-stdproc)
	    ("arctan" "ArcTan" simula-expand-stdproc)
	    ("arctan2" "ArcTan2" simula-expand-stdproc)
	    ("array" "ARRAY" simula-expand-keyword)
	    ("at" "AT" simula-expand-keyword)
	    ("before" "BEFORE" simula-expand-keyword)
	    ("begin" "BEGIN" simula-expand-keyword)
	    ("blanks" "Blanks" simula-expand-stdproc)
	    ("boolean" "BOOLEAN" simula-expand-keyword)
	    ("breakoutimage" "BreakOutImage" simula-expand-stdproc)
	    ("bytefile" "ByteFile" simula-expand-stdproc)
	    ("call" "Call" simula-expand-stdproc)
	    ("cancel" "Cancel" simula-expand-stdproc)
	    ("cardinal" "Cardinal" simula-expand-stdproc)
	    ("char" "Char" simula-expand-stdproc)
	    ("character" "CHARACTER" simula-expand-keyword)
	    ("checkpoint" "CheckPoint" simula-expand-stdproc)
	    ("class" "CLASS" simula-expand-keyword)
	    ("clear" "Clear" simula-expand-stdproc)
	    ("clocktime" "ClockTime" simula-expand-stdproc)
	    ("close" "Close" simula-expand-stdproc)
	    ("comment" "COMMENT" simula-expand-keyword)
	    ("constant" "Constant" simula-expand-stdproc)
	    ("copy" "Copy" simula-expand-stdproc)
	    ("cos" "Cos" simula-expand-stdproc)
	    ("cosh" "CosH" simula-expand-stdproc)
	    ("cotan" "CoTan" simula-expand-stdproc)
	    ("cputime" "CpuTime" simula-expand-stdproc)
	    ("current" "Current" simula-expand-stdproc)
	    ("datetime" "DateTime" simula-expand-stdproc)
	    ("decimalmark" "DecimalMark" simula-expand-stdproc)
	    ("delay" "DELAY" simula-expand-keyword)
	    ("deleteimage" "DeleteImage" simula-expand-stdproc)
	    ("detach" "Detach" simula-expand-stdproc)
	    ("digit" "Digit" simula-expand-stdproc)
	    ("directbytefile" "DirectByteFile" simula-expand-stdproc)
	    ("directfile" "DirectFile" simula-expand-stdproc)
	    ("discrete" "Discrete" simula-expand-stdproc)
	    ("do" "DO" simula-expand-keyword)
	    ("downcase" "Downcase" simula-expand-stdproc)
	    ("draw" "Draw" simula-expand-stdproc)
	    ("eject" "Eject" simula-expand-stdproc)
	    ("else" "ELSE" simula-electric-keyword)
	    ("empty" "Empty" simula-expand-stdproc)
	    ("end" "END" simula-electric-keyword)
	    ("endfile" "Endfile" simula-expand-stdproc)
	    ("entier" "Entier" simula-expand-stdproc)
	    ("eq" "EQ" simula-expand-keyword)
	    ("eqv" "EQV" simula-expand-keyword)
	    ("erlang" "Erlang" simula-expand-stdproc)
	    ("error" "Error" simula-expand-stdproc)
	    ("evtime" "EvTime" simula-expand-stdproc)
	    ("exp" "Exp" simula-expand-stdproc)
	    ("external" "EXTERNAL" simula-expand-keyword)
	    ("false" "FALSE" simula-expand-keyword)
	    ("field" "Field" simula-expand-stdproc)
	    ("file" "File" simula-expand-stdproc)
	    ("first" "First" simula-expand-stdproc)
	    ("follow" "Follow" simula-expand-stdproc)
	    ("for" "FOR" simula-expand-keyword)
	    ("ge" "GE" simula-expand-keyword)
	    ("getchar" "GetChar" simula-expand-stdproc)
	    ("getfrac" "GetFrac" simula-expand-stdproc)
	    ("getint" "GetInt" simula-expand-stdproc)
	    ("getreal" "GetReal" simula-expand-stdproc)
	    ("go" "GO" simula-expand-keyword)
	    ("goto" "GOTO" simula-expand-keyword)
	    ("gt" "GT" simula-expand-keyword)
	    ("head" "Head" simula-expand-stdproc)
	    ("hidden" "HIDDEN" simula-expand-keyword)
	    ("histd" "HistD" simula-expand-stdproc)
	    ("histo" "Histo" simula-expand-stdproc)
	    ("hold" "Hold" simula-expand-stdproc)
	    ("idle" "Idle" simula-expand-stdproc)
	    ("if" "IF" simula-expand-keyword)
	    ("image" "Image" simula-expand-stdproc)
	    ("imagefile" "ImageFile" simula-expand-stdproc)
	    ("imp" "IMP" simula-expand-keyword)
	    ("in" "IN" simula-expand-keyword)
	    ("inbyte" "InByte" simula-expand-stdproc)
	    ("inbytefile" "InByteFile" simula-expand-stdproc)
	    ("inchar" "InChar" simula-expand-stdproc)
	    ("infile" "InFile" simula-expand-stdproc)
	    ("infrac" "InFrac" simula-expand-stdproc)
	    ("inimage" "InImage" simula-expand-stdproc)
	    ("inint" "InInt" simula-expand-stdproc)
	    ("inner" "INNER" simula-expand-keyword)
	    ("inreal" "InReal" simula-expand-stdproc)
	    ("inrecord" "InRecord" simula-expand-stdproc)
	    ("inspect" "INSPECT" simula-expand-keyword)
	    ("integer" "INTEGER" simula-expand-keyword)
	    ("intext" "InText" simula-expand-stdproc)
	    ("into" "Into" simula-expand-stdproc)
	    ("is" "IS" simula-expand-keyword)
	    ("isochar" "ISOChar" simula-expand-stdproc)
	    ("isopen" "IsOpen" simula-expand-stdproc)
	    ("isorank" "ISORank" simula-expand-stdproc)
	    ("label" "LABEL" simula-expand-keyword)
	    ("last" "Last" simula-expand-stdproc)
	    ("lastitem" "LastItem" simula-expand-stdproc)
	    ("lastloc" "LastLoc" simula-expand-stdproc)
	    ("le" "LE" simula-expand-keyword)
	    ("length" "Length" simula-expand-stdproc)
	    ("letter" "Letter" simula-expand-stdproc)
	    ("line" "Line" simula-expand-stdproc)
	    ("linear" "Linear" simula-expand-stdproc)
	    ("linesperpage" "LinesPerPage" simula-expand-stdproc)
	    ("link" "Link" simula-expand-stdproc)
	    ("linkage" "Linkage" simula-expand-stdproc)
	    ("ln" "Ln" simula-expand-stdproc)
	    ("locate" "Locate" simula-expand-stdproc)
	    ("location" "Location" simula-expand-stdproc)
	    ("lock" "Lock" simula-expand-stdproc)
	    ("locked" "Locked" simula-expand-stdproc)
	    ("log10" "Log10" simula-expand-stdproc)
	    ("long" "LONG" simula-expand-keyword)
	    ("lowcase" "LowCase" simula-expand-stdproc)
	    ("lowerbound" "LowerBound" simula-expand-stdproc)
	    ("lowten" "LowTen" simula-expand-stdproc)
	    ("lt" "LT" simula-expand-keyword)
	    ("main" "Main" simula-expand-stdproc)
	    ("max" "Max" simula-expand-stdproc)
	    ("maxint" "MaxInt" simula-expand-stdproc)
	    ("maxlongreal" "MaxLongReal" simula-expand-stdproc)
	    ("maxloc" "MaxLoc" simula-expand-stdproc)
	    ("maxrank" "MaxRank" simula-expand-stdproc)
	    ("maxreal" "MaxReal" simula-expand-stdproc)
	    ("min" "Min" simula-expand-stdproc)
	    ("minint" "MinInt" simula-expand-stdproc)
	    ("minlongreal" "MinLongReal" simula-expand-stdproc)
	    ("minrank" "MinRank" simula-expand-stdproc)
	    ("minreal" "MinReal" simula-expand-stdproc)
	    ("mod" "Mod" simula-expand-stdproc)
	    ("more" "More" simula-expand-stdproc)
	    ("name" "NAME" simula-expand-keyword)
	    ("ne" "NE" simula-expand-keyword)
	    ("negexp" "NegExp" simula-expand-stdproc)
	    ("new" "NEW" simula-expand-keyword)
	    ("nextev" "NextEv" simula-expand-stdproc)
	    ("none" "NONE" simula-expand-keyword)
	    ("normal" "Normal" simula-expand-stdproc)
	    ("not" "NOT" simula-expand-keyword)
	    ("notext" "NOTEXT" simula-expand-keyword)
	    ("open" "Open" simula-expand-stdproc)
	    ("or" "OR" simula-expand-keyword)
	    ("otherwise" "OTHERWISE" simula-electric-keyword)
	    ("out" "Out" simula-expand-stdproc)
	    ("outbyte" "OutByte" simula-expand-stdproc)
	    ("outbytefile" "OutByteFile" simula-expand-stdproc)
	    ("outchar" "OutChar" simula-expand-stdproc)
	    ("outfile" "OutFile" simula-expand-stdproc)
	    ("outfix" "OutFix" simula-expand-stdproc)
	    ("outfrac" "OutFrac" simula-expand-stdproc)
	    ("outimage" "OutImage" simula-expand-stdproc)
	    ("outint" "OutInt" simula-expand-stdproc)
	    ("outreal" "OutReal" simula-expand-stdproc)
	    ("outrecord" "OutRecord" simula-expand-stdproc)
	    ("outtext" "OutText" simula-expand-stdproc)
	    ("page" "Page" simula-expand-stdproc)
	    ("passivate" "Passivate" simula-expand-stdproc)
	    ("poisson" "Poisson" simula-expand-stdproc)
	    ("pos" "Pos" simula-expand-stdproc)
	    ("precede" "Precede" simula-expand-stdproc)
	    ("pred" "Pred" simula-expand-stdproc)
	    ("prev" "Prev" simula-expand-stdproc)
	    ("printfile" "PrintFile" simula-expand-stdproc)
	    ("prior" "PRIOR" simula-expand-keyword)
	    ("procedure" "PROCEDURE" simula-expand-keyword)
	    ("process" "Process" simula-expand-stdproc)
	    ("protected" "PROTECTED" simula-expand-keyword)
	    ("putchar" "PutChar" simula-expand-stdproc)
	    ("putfix" "PutFix" simula-expand-stdproc)
	    ("putfrac" "PutFrac" simula-expand-stdproc)
	    ("putint" "PutInt" simula-expand-stdproc)
	    ("putreal" "PutReal" simula-expand-stdproc)
	    ("qua" "QUA" simula-expand-keyword)
	    ("randint" "RandInt" simula-expand-stdproc)
	    ("rank" "Rank" simula-expand-stdproc)
	    ("reactivate" "REACTIVATE" simula-expand-keyword)
	    ("real" "REAL" simula-expand-keyword)
	    ("ref" "REF" simula-expand-keyword)
	    ("resume" "Resume" simula-expand-stdproc)
	    ("setaccess" "SetAccess" simula-expand-stdproc)
	    ("setpos" "SetPos" simula-expand-stdproc)
	    ("short" "SHORT" simula-expand-keyword)
	    ("sign" "Sign" simula-expand-stdproc)
	    ("simset" "SimSet" simula-expand-stdproc)
	    ("simulaid" "SimulaId" simula-expand-stdproc)
	    ("simulation" "Simulation" simula-expand-stdproc)
	    ("sin" "Sin" simula-expand-stdproc)
	    ("sinh" "SinH" simula-expand-stdproc)
	    ("sourceline" "SourceLine" simula-expand-stdproc)
	    ("spacing" "Spacing" simula-expand-stdproc)
	    ("sqrt" "Sqrt" simula-expand-stdproc)
	    ("start" "Start" simula-expand-stdproc)
	    ("step" "STEP" simula-expand-keyword)
	    ("strip" "Strip" simula-expand-stdproc)
	    ("sub" "Sub" simula-expand-stdproc)
	    ("subepsilon" "SubEpsilon" simula-expand-stdproc)
	    ("suc" "Suc" simula-expand-stdproc)
	    ("switch" "SWITCH" simula-expand-keyword)
	    ("sysin" "SysIn" simula-expand-stdproc)
	    ("sysout" "SysOut" simula-expand-stdproc)
	    ("tan" "Tan" simula-expand-stdproc)
	    ("tanh" "TanH" simula-expand-stdproc)
	    ("terminate_program" "Terminate_Program" simula-expand-stdproc)
	    ("terminated" "Terminated" simula-expand-stdproc)
	    ("text" "TEXT" simula-expand-keyword)
	    ("then" "THEN" simula-electric-keyword)
	    ("this" "THIS" simula-expand-keyword)
	    ("time" "Time" simula-expand-stdproc)
	    ("to" "TO" simula-expand-keyword)
	    ("true" "TRUE" simula-expand-keyword)
	    ("uniform" "Uniform" simula-expand-stdproc)
	    ("unlock" "Unlock" simula-expand-stdproc)
	    ("until" "UNTIL" simula-expand-keyword)
	    ("upcase" "Upcase" simula-expand-stdproc)
	    ("upperbound" "UpperBound" simula-expand-stdproc)
	    ("value" "VALUE" simula-expand-keyword)
	    ("virtual" "VIRTUAL" simula-expand-keyword)
	    ("wait" "Wait" simula-expand-stdproc)
	    ("when" "WHEN" simula-electric-keyword)
	    ("while" "WHILE" simula-expand-keyword))))

1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
(if (and (fboundp 'hilit-set-mode-patterns)
	 (boundp 'hilit-patterns-alist)
	 (not (assoc 'simula-mode hilit-patterns-alist)))
    (hilit-set-mode-patterns
     'simula-mode
     '(
       ("^%\\([ \t\f].*\\)?$" nil comment)
       ("^%include\\>" nil include)
       ("\"[^\"\n]*\"\\|'.'\\|'![0-9]+!'" nil string)
       ("\\<\\(ACTIVATE\\|AFTER\\|AND\\|ARRAY\\|AT\\|BEFORE\\|BEGIN\\|BOOLEAN\\|CHARACTER\\|CLASS\\|DELAY\\|DO\\|ELSE\\|END\\|EQ\\|EQV\\|EXTERNAL\\|FALSE\\|FOR\\|GE\\|GO\\|GOTO\\|GT\\|HIDDEN\\|IF\\|IMP\\|IN\\|INNER\\|INSPECT\\|INTEGER\\|IS\\|LABEL\\|LE\\|LONG\\|LT\\|NAME\\|NE\\|NEW\\|NONE\\|NOT\\|NOTEXT\\|OR\\|OTHERWISE\\|PRIOR\\|PROCEDURE\\|PROTECTED\\|QUA\\|REACTIVATE\\|REAL\\|REF\\|SHORT\\|STEP\\|SWITCH\\|TEXT\\|THEN\\|THIS\\|TO\\|TRUE\\|UNTIL\\|VALUE\\|VIRTUAL\\|WHEN\\|WHILE\\)\\>" nil keyword)
       ("!\\|\\<COMMENT\\>" ";" comment))
     nil 'case-insensitive))

(setq simula-find-comment-point -1
      simula-find-comment-context nil)

;; function used by hilit19
(defun simula-find-next-comment-region (param)
  "Return region (start end) cons of comment after point, or NIL"
  (let (start end)
    ;; This function is called repeatedly, check if point is
    ;; where we left it in the last call
    (if (not (eq simula-find-comment-point (point)))
	(setq simula-find-comment-point (point)
	      simula-find-comment-context (simula-context)))
    ;; loop as long as we haven't found the end of a comment
    (if (memq simula-find-comment-context '(0 1 2))
	(setq start (point))
      (if (re-search-forward "\\<end\\>\\|!\\|\"\\|'\\|^%\\|\\<comment\\>"
			     nil 'move)
	  (let ((previous-char (preceding-char)))
	    (cond
	     ((memq previous-char '(?d ?D))
	      (setq start (point)
		    simula-find-comment-context 2))
	     ((memq previous-char '(?t ?T ?\!))
	      (setq start (point)
		    simula-find-comment-context 0))
	     ((eq previous-char ?%)
	      (setq start (point)
		    simula-find-comment-context 0))))))
    ;; BUG: the following (0 2) branches don't take into account intermixing
    ;; directive lines
    (cond
     ((eq simula-find-comment-context 0)
      (search-forward ";" nil 'move))
     ((eq simula-find-comment-context 1)
      (beginning-of-line 2))
     ((eq simula-find-comment-context 2)
      (re-search-forward ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\" (point-max) 'move)))
    (if start
	(setq end (point)))
    ;; save point for later calls to this function
    (setq simula-find-comment-point (if end (point) -1))
    (and end (cons start end))))

(if (not (fboundp 'save-match-data))
    (defmacro save-match-data (&rest body)
      "Execute the BODY forms, restoring the global value of the match data."
      (let ((original (make-symbol "match-data")))
	(list
	 'let (list (list original '(match-data)))
	 (list 'unwind-protect
	       (cons 'progn body)
	       (list 'store-match-data original))))))


;; defuns for submitting bug reports

(defconst simula-mode-help-address "simula-mode@ifi.uio.no"
  "Address accepting submission of simula-mode bug reports.")

;; get reporter-submit-bug-report when byte-compiling
(and (fboundp 'eval-when-compile)
     (eval-when-compile
      (require 'reporter)))

(defun simula-submit-bug-report ()
  "Submit via mail a bug report on simula-mode."
  (interactive)
  (and
   (y-or-n-p "Do you want to submit a report on simula-mode? ")
   (require 'reporter)
   (reporter-submit-bug-report
    simula-mode-help-address
1654
    (concat "simula-mode from Emacs " emacs-version)
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
    (list
     ;; report only the vars that affect indentation
     'simula-emacs-features
     'simula-indent-level
     'simula-substatement-offset
     'simula-continued-statement-offset
     'simula-label-offset
     'simula-if-indent
     'simula-inspect-indent
     'simula-electric-indent
     'simula-abbrev-keyword
     'simula-abbrev-stdproc
     'simula-abbrev-file
     'simula-tab-always-indent
     ))))

(provide 'simula-mode)

Eric S. Raymond's avatar
Eric S. Raymond committed
1673
;;; simula.el ends here