Add 2013 to yet more FSF copyright years
[emacs.git] / lisp / progmodes / simula.el
blobd71c9adbef4d65b082dd23f85d5c7d6a3576169c
1 ;;; simula.el --- SIMULA 87 code editing commands for Emacs
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2013 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Hans Henrik Eriksen <hhe@ifi.uio.no>
7 ;; Maintainer: simula-mode@ifi.uio.no
8 ;; (above email addresses invalid as of April 2008)
9 ;; Adapted-By: ESR
10 ;; Keywords: languages
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; A major mode for editing the Simula language. It knows about Simula
30 ;; syntax and standard indentation commands. It also provides convenient
31 ;; abbrevs for Simula keywords.
33 ;; Hans Henrik Eriksen (the author) may be reached at:
34 ;; Institutt for informatikk,
35 ;; Universitetet i Oslo
37 ;;; Code:
40 (defgroup simula nil
41 "Major mode for editing Simula code."
42 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
43 :prefix "simula-"
44 :group 'languages)
46 (defconst simula-tab-always-indent-default nil
47 "Non-nil means TAB in SIMULA mode should always reindent the current line.
48 Otherwise TAB indents only when point is within
49 the run of whitespace at the beginning of the line.")
51 (defcustom simula-tab-always-indent simula-tab-always-indent-default
52 "Non-nil means TAB in SIMULA mode should always reindent the current line.
53 Otherwise TAB indents only when point is within
54 the run of whitespace at the beginning of the line."
55 :type 'boolean
56 :group 'simula)
58 (defconst simula-indent-level-default 3
59 "Indentation of SIMULA statements with respect to containing block.")
61 (defcustom simula-indent-level simula-indent-level-default
62 "Indentation of SIMULA statements with respect to containing block."
63 :type 'integer
64 :group 'simula)
67 (defconst simula-substatement-offset-default 3
68 "Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.")
70 (defcustom simula-substatement-offset simula-substatement-offset-default
71 "Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE."
72 :type 'integer
73 :group 'simula)
75 (defconst simula-continued-statement-offset-default 3
76 "Extra indentation for lines not starting a statement or substatement.
77 If value is a list, each line in a multipleline continued statement
78 will have the car of the list extra indentation with respect to
79 the previous line of the statement.")
81 (defcustom simula-continued-statement-offset
82 simula-continued-statement-offset-default
83 "Extra indentation for lines not starting a statement or substatement.
84 If value is a list, each line in a multipleline continued statement
85 will have the car of the list extra indentation with respect to
86 the previous line of the statement."
87 :type 'integer
88 :group 'simula)
90 (defconst simula-label-offset-default -4711
91 "Offset of SIMULA label lines relative to usual indentation.")
93 (defcustom simula-label-offset simula-label-offset-default
94 "Offset of SIMULA label lines relative to usual indentation."
95 :type 'integer
96 :group 'simula)
98 (defconst simula-if-indent-default '(0 . 0)
99 "Extra indentation of THEN and ELSE with respect to the starting IF.
100 Value is a cons cell, the car is extra THEN indentation and the cdr
101 extra ELSE indentation. IF after ELSE is indented as the starting IF.")
103 (defcustom simula-if-indent simula-if-indent-default
104 "Extra indentation of THEN and ELSE with respect to the starting IF.
105 Value is a cons cell, the car is extra THEN indentation and the cdr
106 extra ELSE indentation. IF after ELSE is indented as the starting IF."
107 :type '(cons integer integer)
108 :group 'simula)
110 (defconst simula-inspect-indent-default '(0 . 0)
111 "Extra indentation of WHEN and OTHERWISE with respect to the INSPECT.
112 Value is a cons cell, the car is extra WHEN indentation
113 and the cdr extra OTHERWISE indentation.")
115 (defcustom simula-inspect-indent simula-inspect-indent-default
116 "Extra indentation of WHEN and OTHERWISE with respect to the INSPECT.
117 Value is a cons cell, the car is extra WHEN indentation
118 and the cdr extra OTHERWISE indentation."
119 :type '(cons integer integer)
120 :group 'simula)
122 (defconst simula-electric-indent-default nil
123 "Non-nil means `simula-indent-line' function may reindent previous line.")
125 (defcustom simula-electric-indent simula-electric-indent-default
126 "Non-nil means `simula-indent-line' function may reindent previous line."
127 :type 'boolean
128 :group 'simula)
130 (defconst simula-abbrev-keyword-default 'upcase
131 "Specify how to convert case for SIMULA keywords.
132 Value is one of the symbols `upcase', `downcase', `capitalize',
133 \(as in) `abbrev-table' or nil if they should not be changed.")
135 (defcustom simula-abbrev-keyword simula-abbrev-keyword-default
136 "Specify how to convert case for SIMULA keywords.
137 Value is one of the symbols `upcase', `downcase', `capitalize',
138 \(as in) `abbrev-table' or nil if they should not be changed."
139 :type '(choice (const upcase) (const downcase) (const capitalize)(const nil))
140 :group 'simula)
142 (defconst simula-abbrev-stdproc-default 'abbrev-table
143 "Specify how to convert case for standard SIMULA procedure and class names.
144 Value is one of the symbols `upcase', `downcase', `capitalize',
145 \(as in) `abbrev-table', or nil if they should not be changed.")
147 (defcustom simula-abbrev-stdproc simula-abbrev-stdproc-default
148 "Specify how to convert case for standard SIMULA procedure and class names.
149 Value is one of the symbols `upcase', `downcase', `capitalize',
150 \(as in) `abbrev-table', or nil if they should not be changed."
151 :type '(choice (const upcase) (const downcase) (const capitalize)
152 (const abbrev-table) (const nil))
153 :group 'simula)
155 (defcustom simula-abbrev-file nil
156 "File with extra abbrev definitions for use in SIMULA mode.
157 These are used together with the standard abbrev definitions for SIMULA.
158 Please note that the standard definitions are required
159 for SIMULA mode to function correctly."
160 :type '(choice file (const nil))
161 :group 'simula)
163 (defvar simula-mode-syntax-table nil
164 "Syntax table in SIMULA mode buffers.")
166 (defconst simula-syntax-propertize-function
167 (syntax-propertize-rules
168 ;; `comment' directive.
169 ("\\<\\(c\\)omment\\>" (1 "<"))
170 ;; end comments
171 ((concat "\\<end\\>\\([^;\n]\\).*?\\(\n\\|\\(.\\)\\(;\\|"
172 (regexp-opt '("end" "else" "when" "otherwise"))
173 "\\)\\)")
174 (1 "< b")
175 (3 "> b"))
176 ;; non-quoted single-quote char.
177 ("'\\('\\)'" (1 "."))))
179 ;; Regexps written with help from Alf-Ivar Holm <alfh@ifi.uio.no>.
180 (defconst simula-font-lock-keywords-1
181 '(;;
182 ;; Compiler directives.
183 ("^%\\([^ \t\n].*\\)" 1 font-lock-constant-face t)
185 ;; Class and procedure names.
186 ("\\<\\(class\\|procedure\\)\\>[ \t]*\\(\\sw+\\)?"
187 (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t)))
188 "Subdued level highlighting for Simula mode.")
190 (defconst simula-font-lock-keywords-2
191 (append simula-font-lock-keywords-1
192 (list
194 ;; Constants.
195 '("\\<\\(false\\|none\\|notext\\|true\\)\\>" . font-lock-constant-face)
197 ;; Keywords.
198 (regexp-opt
199 '("activate" "after" "and" "at" "before" "begin" "delay" "do"
200 "else" "end" "eq" "eqv" "external" "for" "ge" "go" "goto" "gt"
201 "hidden" "if" "imp" "in" "inner" "inspect" "is" "label" "le"
202 "lt" "ne" "new" "not" "or" "otherwise" "prior" "protected"
203 "qua" "reactivate" "step" "switch" "then" "this" "to" "until"
204 "virtual" "when" "while") 'words)
206 ;; Types.
207 (cons (regexp-opt
208 '("array" "boolean" "character" "integer"
209 "long" "name" "real" "short" "text" "value" "ref") 'words)
210 'font-lock-type-face)))
211 "Medium level highlighting for Simula mode.")
213 (defconst simula-font-lock-keywords-3
214 (append simula-font-lock-keywords-2
215 (list
217 ;; Super-class names and super-slow.
218 '("\\<\\(\\sw+\\)[ \t]+class\\>" 1 font-lock-function-name-face)
220 ;; Types and their declarations.
221 (list (concat "\\<\\(array\\|boolean\\|character\\|integer\\|"
222 "long\\|name\\|real\\|short\\|text\\|value\\)\\>"
223 "\\([ \t]+\\sw+\\>\\)*")
224 '(font-lock-match-c-style-declaration-item-and-skip-to-next
225 ;; Start with point after all type specifiers.
226 (goto-char (or (match-beginning 2) (match-end 1)))
227 ;; Finish with point after first type specifier.
228 (goto-char (match-end 1))
229 ;; Fontify as a variable name.
230 (1 font-lock-variable-name-face)))
232 ;; Object references and their declarations.
233 '("\\<\\(ref\\)\\>[ \t]*\\((\\(\\sw+\\))\\)?"
234 (3 font-lock-function-name-face nil t)
235 (font-lock-match-c-style-declaration-item-and-skip-to-next nil nil
236 (1 font-lock-variable-name-face)))
238 "Gaudy level highlighting for Simula mode.")
240 (defvar simula-font-lock-keywords simula-font-lock-keywords-1
241 "Default expressions to highlight in Simula mode.")
243 ; The following function is taken from cc-mode.el,
244 ; it determines the flavor of the Emacs running
246 (defvar simula-mode-menu
247 '(["Report Bug" simula-submit-bug-report t]
248 ["Indent Line" simula-indent-line t]
249 ["Backward Statement" simula-previous-statement t]
250 ["Forward Statement" simula-next-statement t]
251 ["Backward Up Level" simula-backward-up-level t]
252 ["Forward Down Statement" simula-forward-down-level t])
253 "Lucid Emacs menu for SIMULA mode.")
255 (if simula-mode-syntax-table
257 (setq simula-mode-syntax-table (copy-syntax-table (standard-syntax-table)))
258 (modify-syntax-entry ?! "<" simula-mode-syntax-table)
259 (modify-syntax-entry ?$ "." simula-mode-syntax-table)
260 (modify-syntax-entry ?% "< b" simula-mode-syntax-table)
261 (modify-syntax-entry ?\n "> b" simula-mode-syntax-table)
262 (modify-syntax-entry ?' "\"" simula-mode-syntax-table)
263 (modify-syntax-entry ?\( "()" simula-mode-syntax-table)
264 (modify-syntax-entry ?\) ")(" simula-mode-syntax-table)
265 (modify-syntax-entry ?\; ">" simula-mode-syntax-table)
266 (modify-syntax-entry ?\[ "." simula-mode-syntax-table)
267 (modify-syntax-entry ?\\ "." simula-mode-syntax-table)
268 (modify-syntax-entry ?\] "." simula-mode-syntax-table)
269 (modify-syntax-entry ?_ "_" simula-mode-syntax-table)
270 (modify-syntax-entry ?\| "." simula-mode-syntax-table)
271 (modify-syntax-entry ?\{ "." simula-mode-syntax-table)
272 (modify-syntax-entry ?\} "." simula-mode-syntax-table))
274 (defvar simula-mode-map
275 (let ((map (make-sparse-keymap)))
276 (define-key map "\C-c\C-u" 'simula-backward-up-level)
277 (define-key map "\C-c\C-p" 'simula-previous-statement)
278 (define-key map "\C-c\C-d" 'simula-forward-down-level)
279 (define-key map "\C-c\C-n" 'simula-next-statement)
280 ;; (define-key map "\C-c\C-g" 'simula-goto-definition)
281 ;; (define-key map "\C-c\C-h" 'simula-standard-help)
282 (define-key map "\177" 'backward-delete-char-untabify)
283 (define-key map ":" 'simula-electric-label)
284 (define-key map "\e\C-q" 'simula-indent-exp)
285 (define-key map "\t" 'simula-indent-command)
286 ;; Emacs 19 defines menus in the mode map
287 (define-key map [menu-bar simula]
288 (cons "SIMULA" (make-sparse-keymap "SIMULA")))
289 (define-key map [menu-bar simula bug-report]
290 '("Submit Bug Report" . simula-submit-bug-report))
291 (define-key map [menu-bar simula separator-indent]
292 '("--"))
293 (define-key map [menu-bar simula indent-exp]
294 '("Indent Expression" . simula-indent-exp))
295 (define-key map [menu-bar simula indent-line]
296 '("Indent Line" . simula-indent-command))
297 (define-key map [menu-bar simula separator-navigate]
298 '("--"))
299 (define-key map [menu-bar simula backward-stmt]
300 '("Previous Statement" . simula-previous-statement))
301 (define-key map [menu-bar simula forward-stmt]
302 '("Next Statement" . simula-next-statement))
303 (define-key map [menu-bar simula backward-up]
304 '("Backward Up Level" . simula-backward-up-level))
305 (define-key map [menu-bar simula forward-down]
306 '("Forward Down Statement" . simula-forward-down-level))
308 (put 'simula-next-statement 'menu-enable '(not (eobp)))
309 (put 'simula-previous-statement 'menu-enable '(not (bobp)))
310 (put 'simula-forward-down-level 'menu-enable '(not (eobp)))
311 (put 'simula-backward-up-level 'menu-enable '(not (bobp)))
312 (put 'simula-indent-command 'menu-enable '(not buffer-read-only))
313 (put 'simula-indent-exp 'menu-enable '(not buffer-read-only))
315 ;; RMS: mouse-3 should not select this menu. mouse-3's global
316 ;; definition is useful in SIMULA mode and we should not interfere
317 ;; with that. The menu is mainly for beginners, and for them,
318 ;; the menubar requires less memory than a special click.
319 ;; in Lucid Emacs, we want the menu to popup when the 3rd button is
320 ;; hit. In 19.10 and beyond this is done automatically if we put
321 ;; the menu on mode-popup-menu variable, see c-common-init [cc-mode.el]
322 ;;(if (not (boundp 'mode-popup-menu))
323 ;; (define-key simula-mode-map 'button3 'simula-popup-menu))
324 map)
325 "Keymap used in `simula-mode'.")
327 ;; menus for Lucid
328 (defun simula-popup-menu (_e)
329 "Pops up the SIMULA menu."
330 (interactive "@e")
331 (popup-menu (cons (concat mode-name " Mode Commands") simula-mode-menu)))
333 ;;;###autoload
334 (define-derived-mode simula-mode prog-mode "Simula"
335 "Major mode for editing SIMULA code.
336 \\{simula-mode-map}
337 Variables controlling indentation style:
338 `simula-tab-always-indent'
339 Non-nil means TAB in SIMULA mode should always reindent the current line,
340 regardless of where in the line point is when the TAB command is used.
341 `simula-indent-level'
342 Indentation of SIMULA statements with respect to containing block.
343 `simula-substatement-offset'
344 Extra indentation after DO, THEN, ELSE, WHEN and OTHERWISE.
345 `simula-continued-statement-offset' 3
346 Extra indentation for lines not starting a statement or substatement,
347 e.g. a nested FOR-loop. If value is a list, each line in a multiple-
348 line continued statement will have the car of the list extra indentation
349 with respect to the previous line of the statement.
350 `simula-label-offset' -4711
351 Offset of SIMULA label lines relative to usual indentation.
352 `simula-if-indent' '(0 . 0)
353 Extra indentation of THEN and ELSE with respect to the starting IF.
354 Value is a cons cell, the car is extra THEN indentation and the cdr
355 extra ELSE indentation. IF after ELSE is indented as the starting IF.
356 `simula-inspect-indent' '(0 . 0)
357 Extra indentation of WHEN and OTHERWISE with respect to the
358 corresponding INSPECT. Value is a cons cell, the car is
359 extra WHEN indentation and the cdr extra OTHERWISE indentation.
360 `simula-electric-indent' nil
361 If this variable is non-nil, `simula-indent-line'
362 will check the previous line to see if it has to be reindented.
363 `simula-abbrev-keyword' 'upcase
364 Determine how SIMULA keywords will be expanded. Value is one of
365 the symbols `upcase', `downcase', `capitalize', (as in) `abbrev-table',
366 or nil if they should not be changed.
367 `simula-abbrev-stdproc' 'abbrev-table
368 Determine how standard SIMULA procedure and class names will be
369 expanded. Value is one of the symbols `upcase', `downcase', `capitalize',
370 (as in) `abbrev-table', or nil if they should not be changed.
372 Turning on SIMULA mode calls the value of the variable simula-mode-hook
373 with no arguments, if that value is non-nil."
374 (set (make-local-variable 'comment-column) 40)
375 ;; (set (make-local-variable 'end-comment-column) 75)
376 (set (make-local-variable 'paragraph-start) "[ \t]*$\\|\\f")
377 (set (make-local-variable 'paragraph-separate) paragraph-start)
378 (set (make-local-variable 'indent-line-function) 'simula-indent-line)
379 (set (make-local-variable 'comment-start) "! ")
380 (set (make-local-variable 'comment-end) " ;")
381 (set (make-local-variable 'comment-start-skip) "!+ *")
382 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
383 (set (make-local-variable 'comment-multi-line) t)
384 (set (make-local-variable 'font-lock-defaults)
385 '((simula-font-lock-keywords simula-font-lock-keywords-1
386 simula-font-lock-keywords-2 simula-font-lock-keywords-3)
387 nil t ((?_ . "w"))))
388 (set (make-local-variable 'syntax-propertize-function)
389 simula-syntax-propertize-function)
390 (abbrev-mode 1))
392 (defun simula-indent-exp ()
393 "Indent SIMULA expression following point."
394 (interactive)
395 (let ((here (point))
396 (simula-electric-indent nil)
397 end)
398 (simula-skip-comment-forward)
399 (if (eobp)
400 (goto-char here)
401 (unwind-protect
402 (progn
403 (simula-next-statement 1)
404 (setq end (point-marker))
405 (simula-previous-statement 1)
406 (beginning-of-line)
407 (while (< (point) end)
408 (if (not (looking-at "[ \t]*$"))
409 (simula-indent-line))
410 (forward-line 1)))
411 (and end (set-marker end nil))))))
414 (defun simula-indent-line ()
415 "Indent this line as SIMULA code.
416 If `simula-electric-indent' is non-nil, indent previous line if necessary."
417 (let ((origin (- (point-max) (point)))
418 (indent (simula-calculate-indent))
419 (case-fold-search t))
420 (unwind-protect
421 (if simula-electric-indent
422 (progn
424 ;; manually expand abbrev on last line, if any
426 (end-of-line 0)
427 (expand-abbrev)
428 ;; now maybe we should reindent that line
429 (beginning-of-line)
430 (skip-chars-forward " \t\f")
431 (if (and
432 (looking-at
433 "\\(end\\|if\\|then\\|else\\|when\\|otherwise\\)\\>")
434 (not (simula-context)))
435 ;; yes - reindent
436 (let ((post-indent (simula-calculate-indent)))
437 (if (eq (current-indentation) post-indent)
439 (delete-horizontal-space)
440 (indent-to post-indent))))))
441 (goto-char (- (point-max) origin))
442 (if (eq (current-indentation) indent)
443 (back-to-indentation)
444 (delete-horizontal-space)
445 (indent-to indent)))))
448 (defun simula-indent-command (&optional whole-exp)
449 "Indent current line as SIMULA code, or insert TAB character.
450 If `simula-tab-always-indent' is non-nil, always indent current line.
451 Otherwise, indent only if point is before any non-whitespace
452 character on the line.
454 A numeric argument, regardless of its value, means indent rigidly
455 all the lines of the SIMULA statement after point so that this line
456 becomes properly indented.
457 The relative indentation among the lines of the statement are preserved."
458 (interactive "P")
459 (let ((case-fold-search t))
460 (if (or whole-exp simula-tab-always-indent
461 (save-excursion
462 (skip-chars-backward " \t\f")
463 (bolp)))
464 ;; reindent current line
465 (let ((indent (save-excursion
466 (beginning-of-line)
467 (simula-calculate-indent)))
468 (current (current-indentation))
469 (origin (- (point-max) (point)))
470 (bol (save-excursion
471 (skip-chars-backward " \t\f")
472 (bolp)))
473 beg end)
474 (unwind-protect
475 (if (eq current indent)
476 (if (save-excursion
477 (skip-chars-backward " \t\f")
478 (bolp))
479 (back-to-indentation))
480 (beginning-of-line)
481 (delete-horizontal-space)
482 (indent-to indent))
483 (if (not bol)
484 (goto-char (- (point-max) origin))))
485 (setq origin (point))
486 (if whole-exp
487 (save-excursion
488 (beginning-of-line 2)
489 (setq beg (point))
490 (goto-char origin)
491 (simula-next-statement 1)
492 (setq end (point))
493 (if (and (> end beg) (not (eq indent current)))
494 (indent-code-rigidly beg end (- indent current) "%")))))
495 (insert-tab))))
498 (defun simula-context ()
499 "Return value according to syntactic SIMULA context of point.
500 0 point inside COMMENT comment
501 1 point on SIMULA-compiler directive line
502 2 point inside END comment
503 3 point inside string
504 4 point inside character constant
505 nil otherwise."
506 ;; first, find out if this is a compiler directive line
507 (if (save-excursion
508 (beginning-of-line)
509 (eq (following-char) ?%))
510 ;; YES - return 1
512 (save-excursion
513 ;; The current line is NOT a compiler directive line.
514 ;; Now, the strategy is to search backward to find a semicolon
515 ;; that is NOT inside a string. The point after semicolon MUST be
516 ;; outside a comment, since semicolons are comment-ending and
517 ;; comments are non-recursive. We take advantage of the fact
518 ;; that strings MUST end on the same line as they started, so
519 ;; that we can easily decide whether we are inside a string or not.
520 (let (return-value (origin (point)))
521 (skip-chars-backward "^;" (point-min))
522 ;; found semicolon or beginning of buffer
523 (let (loopvalue (saved-point origin))
524 (while (and (not (bobp))
525 (if (progn
526 (beginning-of-line)
527 ;; compiler directive line? If so, cont searching..
528 (eq (following-char) ?%))
530 (while (< (point) saved-point)
531 (skip-chars-forward "^;\"'")
532 (forward-char 1)
533 (cond
534 ((eq (preceding-char) ?\;)
535 (setq saved-point (point)))
536 ((eq (preceding-char) ?\")
537 (skip-chars-forward "^\";")
538 (if (eq (following-char) ?\;)
539 (setq saved-point (point) loopvalue t)
540 (forward-char 1)))
542 (if (eq (following-char) ?')
543 (forward-char 1))
544 (skip-chars-forward "^';")
545 (if (eq (following-char) ?\;)
546 (setq saved-point (point) loopvalue t)
547 (forward-char 1)))))
548 loopvalue))
549 (backward-char 1)
550 (skip-chars-backward "^;")
551 (setq saved-point (point) loopvalue nil)))
552 ;; Now we are CERTAIN that we are outside comments and strings.
553 ;; The job now is to search forward again towards the origin
554 ;; skipping directives, comments and strings correctly,
555 ;; so that we know what context we are in when we find the origin.
556 (while (and
557 (< (point) origin)
558 (re-search-forward
559 "\\<end\\>\\|!\\|\"\\|'\\|^%\\|\\<comment\\>" origin 'move))
560 (cond
561 ((memq (preceding-char) '(?d ?D))
562 (setq return-value 2)
563 (while (and (re-search-forward
564 ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>\\|^%"
565 origin 'move)
566 ;; found another END?
567 (or (memq (preceding-char) '(?d ?D))
568 ;; if directive, skip line
569 (and (eq (preceding-char) ?%)
570 (beginning-of-line 2))
571 ;; found other keyword, out of END comment
572 (setq return-value nil))))
573 (if (and (eq (char-syntax (preceding-char)) ?w)
574 (eq (char-syntax (following-char)) ?w))
575 (save-excursion
576 (backward-word 1)
577 (if (looking-at "end\\>\\|else\\>\\|otherwise\\>\\|when\\>")
578 (setq return-value nil)))))
579 ((memq (preceding-char) '(?! ?t ?T))
580 ; skip comment
581 (setq return-value 0)
582 (skip-chars-forward "^%;" origin)
583 (while (and return-value (< (point) origin))
584 (if (eq (following-char) ?\;)
585 (setq return-value nil)
586 (if (bolp)
587 (beginning-of-line 2) ; skip directive inside comment
588 (forward-char 1)) ; or single '%'
589 (skip-chars-forward "^%;" origin))))
590 ((eq (preceding-char) ?\")
591 (if (not (search-forward "\"" origin 'move))
592 (setq return-value 3)))
593 ((eq (preceding-char) ?\')
594 (if (or (eq (point) origin) (eobp))
595 (setq return-value 4)
596 (forward-char 1)
597 (if (not (search-forward "'" origin 'move))
598 (setq return-value 4))))
599 ;; compiler directive line - skip
600 (t (beginning-of-line 2))))
601 return-value)
605 (defun simula-electric-label ()
606 "If this is a label that starts the line, reindent the line."
607 (interactive)
608 (expand-abbrev)
609 (insert ?:)
610 (let ((origin (- (point-max) (point)))
611 (case-fold-search t)
612 ;; don't mix a label with an assignment operator := :-
613 ;; therefore take a peek at next typed character...
614 (next-char (read-event)))
615 (unwind-protect
616 (setq unread-command-events (append unread-command-events
617 (list next-char)))
618 ;; Problem: find out if character just read is a command char
619 ;; that would insert something after ':' making it a label.
620 ;; At least \n, \r (and maybe \t) falls into this category.
621 ;; This is a real crock, it depends on traditional keymap
622 ;; bindings, that is, printing characters doing self-insert,
623 ;; and no other command sequence inserting '-' or '='.
624 ;; simula-electric-label can be easily fooled...
625 (if (and (not (memq next-char '(?= ?-)))
626 (or (memq next-char '(?\n ?\r))
627 (and (eq next-char ?\t)
628 simula-tab-always-indent)
629 (not (memq (following-char) '(?= ?-))))
630 (not (simula-context))
631 ;; label?
632 (progn
633 (backward-char 1)
634 (skip-chars-backward " \t\f")
635 (skip-chars-backward "a-zA-Z0-9_")
636 (if (looking-at "virtual\\>")
638 (skip-chars-backward " \t\f")
639 (bolp))))
640 (let ((amount (simula-calculate-indent)))
641 (delete-horizontal-space)
642 (indent-to amount)))
643 (goto-char (- (point-max) origin)))))
646 (defun simula-backward-up-level (count)
647 "Move backward up COUNT block levels.
648 If COUNT is negative, move forward up block level instead."
649 (interactive "p")
650 (let ((origin (point))
651 (case-fold-search t))
652 (condition-case ()
653 (if (> count 0)
654 (while (> count 0)
655 (re-search-backward "\\<begin\\>\\|\\<end\\>")
656 (if (not (simula-context))
657 (setq count (if (memq (following-char) '(?b ?B))
658 (1- count)
659 (1+ count)))))
660 (while (< count 0)
661 (re-search-forward "\\<begin\\>\\|\\<end\\>")
662 (backward-word 1)
663 (if (not (simula-context))
664 (setq count (if (memq (following-char) '(?e ?E))
665 (1+ count)
666 (1- count))))
667 (backward-word -1)))
668 ;; If block level not found, jump back to origin and signal an error
669 (error (progn
670 (goto-char origin)
671 (error "No higher block level")))
672 (quit (progn
673 (goto-char origin)
674 (signal 'quit nil))))))
677 (defun simula-forward-down-level (count)
678 "Move forward down COUNT block levels.
679 If COUNT is negative, move backward down block level instead."
680 (interactive "p")
681 ;; When we search for a deeper block level, we must never
682 ;; get out of the block where we started -> count >= start-count
683 (let ((start-count count)
684 (origin (point))
685 (case-fold-search t))
686 (condition-case ()
687 (if (< count 0)
688 (while (< count 0)
689 (re-search-backward "\\<begin\\>\\|\\<end\\>")
690 (if (not (simula-context))
691 (setq count (if (memq (following-char) '(?e ?E))
692 (1+ count)
693 (1- count))))
694 (if (< count start-count) (signal 'error nil)))
695 (while (> count 0)
696 (re-search-forward "\\<begin\\>\\|\\<end\\>")
697 (backward-word 1)
698 (if (not (simula-context))
699 (setq count (if (memq (following-char) '(?b ?B))
700 (1- count)
701 (1+ count))))
702 (backward-word -1)
703 ;; deeper level has to be found within starting block
704 (if (> count start-count) (signal 'error nil))))
705 ;; If block level not found, jump back to origin and signal an error
706 (error (progn
707 (goto-char origin)
708 (error "No containing block level")))
709 (quit (progn
710 (goto-char origin)
711 (signal 'quit nil))))))
714 (defun simula-previous-statement (count)
715 "Move backward COUNT statements.
716 If COUNT is negative, move forward instead."
717 (interactive "p")
718 (if (< count 0)
719 (simula-next-statement (- count))
720 (let (status
721 (case-fold-search t)
722 (origin (point)))
723 (condition-case ()
725 (progn
726 (simula-skip-comment-backward)
727 (if (memq (preceding-char) '(?n ?N))
728 (progn
729 (backward-word 1)
730 (if (not (looking-at "\\<begin\\>"))
731 (backward-word -1)))
732 (if (eq (preceding-char) ?\;)
733 (backward-char 1))
735 (while (and (natnump (setq count (1- count)))
736 (setq status (simula-search-backward
737 ";\\|\\<begin\\>" nil 'move))))
738 (if status
739 (progn
740 (if (eq (following-char) ?\;)
741 (forward-char 1)
742 (backward-word -1))))
743 (simula-skip-comment-forward))
744 (error (progn (goto-char origin)
745 (error "Incomplete statement (too many ENDs)")))
746 (quit (progn (goto-char origin) (signal 'quit nil)))))))
749 (defun simula-next-statement (count)
750 "Move forward COUNT statements.
751 If COUNT is negative, move backward instead."
752 (interactive "p")
753 (if (< count 0)
754 (simula-previous-statement (- count))
755 (let (status
756 (case-fold-search t)
757 (origin (point)))
758 (condition-case ()
759 (progn
760 (simula-skip-comment-forward)
761 (if (looking-at "\\<end\\>") (forward-word 1))
762 (while (and (natnump (setq count (1- count)))
763 (setq status (simula-search-forward
764 ";\\|\\<end\\>" (point-max) 'move))))
765 (if (and status (/= (preceding-char) ?\;))
766 (progn
767 (backward-word 1)
768 (simula-skip-comment-backward))))
769 (error (progn (goto-char origin)
770 (error "Incomplete statement (too few ENDs)")))
771 (quit (progn (goto-char origin) (signal 'quit nil)))))))
774 (defun simula-skip-comment-backward (&optional stop-at-end)
775 "Search towards bob to find first char that is outside a comment."
776 (interactive)
777 (catch 'simula-out
778 (let (context)
779 (while t
780 (skip-chars-backward " \t\n\f")
781 (if (eq (preceding-char) ?\;)
782 (save-excursion
783 (backward-char 1)
784 (setq context (simula-context))
785 (if (and stop-at-end (eq context 2))
786 (setq context nil)))
787 (setq context (simula-context)))
788 (cond
789 ((memq context '(nil 3 4))
790 ;; check to see if we found a label
791 (if (and (eq (preceding-char) ?:)
792 (not (memq (following-char) '(?- ?=)))
793 (save-excursion
794 (skip-chars-backward ": \t\fa-zA-Z0-9_")
795 (not (looking-at "virtual\\>"))))
796 (skip-chars-backward ": \t\fa-zA-Z0-9_")
797 (throw 'simula-out nil)))
798 ((eq context 0)
799 ;; since we are inside a comment, it must start somewhere!
800 (while (and (re-search-backward "!\\|\\<comment\\>")
801 (memq (simula-context) '(0 1)))))
802 ((eq context 1)
803 (beginning-of-line)
804 (if (bobp)
805 (throw 'simula-out nil)
806 (backward-char)))
807 ((eq context 2)
808 ;; an END-comment must belong to an END
809 (re-search-backward "\\<end\\>")
810 (forward-word 1)
811 (throw 'simula-out nil))
812 ;; should be impossible to get here..
813 )))))
816 (defun simula-skip-comment-forward ()
817 "Search towards eob to find first char that is outside a comment."
818 ;; this function assumes we start with point .outside a comment
819 (interactive)
820 (catch 'simula-out
821 (while t
822 (skip-chars-forward " \t\n\f")
823 ;; BUG: the following (0 2) branches don't take into account intermixing
824 ;; directive lines
825 (cond
826 ((looking-at "!\\|\\<comment\\>")
827 (search-forward ";" nil 'move))
828 ((and (bolp) (eq (following-char) ?%))
829 (beginning-of-line 2))
830 ((and (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]")
831 (not (looking-at "virtual\\>")))
832 (skip-chars-forward "a-zA-Z0-9_ \t\f:"))
834 (throw 'simula-out t))))))
837 (defun simula-forward-up-level ()
838 (let ((continue-loop t)
839 (origin (point))
840 (case-fold-search t)
841 return-value
842 temp)
843 (while continue-loop
844 (if (re-search-backward "\\<begin\\>\\|\\<end\\>" (point-min) 'move)
845 (setq temp (simula-context)
846 return-value (and (memq (preceding-char) '(?d ?D))
847 (memq temp '(nil 2)))
848 continue-loop (and (not return-value)
849 (simula-forward-up-level)))
850 (setq continue-loop nil)))
851 (if return-value
853 (goto-char origin)
854 nil)))
857 (defun simula-calculate-indent ()
858 (save-excursion
859 (let ((where (simula-context))
860 (origin (point))
861 (indent 0)
862 continued
863 start-line
864 temp
865 found-end
866 prev-cont)
867 (cond
868 ((eq where 0)
870 ;; Comment.
871 ;; If comment started on previous non-blank line, indent to the
872 ;; column where the comment started, else indent as that line.
874 (skip-chars-backward " \t\n\f")
875 (while (and (not (bolp)) (eq (simula-context) 0))
876 (re-search-backward "^\\|!\\|\\<comment\\>"))
877 (skip-chars-forward " \t\n\f")
878 (prog1
879 (current-column)
880 (goto-char origin)))
881 ((eq where 1)
883 ;; Directive. Always 0.
887 ;; Detect missing string delimiters
889 ((eq where 3)
890 (error "Inside string"))
891 ((eq where 4)
892 (error "Inside character constant"))
894 ;; check to see if inside ()'s
896 ((setq temp (simula-inside-parens))
897 temp)
899 ;; Calculate non-comment indentation
901 ;; first, find out if this line starts with something that needs
902 ;; special indentation (END/IF/THEN/ELSE/WHEN/OTHERWISE or label)
904 (skip-chars-forward " \t\f")
905 (cond
907 ;; END
909 ((looking-at "end\\>")
910 (setq indent (- simula-indent-level)
911 found-end t))
913 ;; IF/THEN/ELSE
915 ((looking-at "if\\>\\|then\\>\\|else\\>")
916 ;; search for the *starting* IF
917 (cond
918 ((memq (following-char) '(?T ?t))
919 (setq indent (car simula-if-indent)))
920 ((memq (following-char) '(?E ?e))
921 (setq indent (cdr simula-if-indent)))
923 (forward-word 1)
924 (setq indent 0)))
925 (simula-find-if))
927 ;; WHEN/OTHERWISE
929 ((looking-at "when\\>\\|otherwise\\>")
930 ;; search for corresponding INSPECT
931 (if (memq (following-char) '(?W ?w))
932 (setq indent (car simula-inspect-indent))
933 (setq indent (cdr simula-inspect-indent)))
934 (simula-find-inspect))
936 ;; label:
938 ((and (not (looking-at "virtual\\>"))
939 (looking-at "[a-z0-9_]*[ \t\f]*:[^-=]"))
940 (setq indent simula-label-offset)))
941 ;; find line with non-comment text
942 (simula-skip-comment-backward 'dont-skip-end)
943 (if (and found-end
944 (not (eq (preceding-char) ?\;))
945 (if (memq (preceding-char) '(?N ?n))
946 (save-excursion
947 (backward-word 1)
948 (not (looking-at "begin\\>")))
950 (progn
951 (simula-previous-statement 1)
952 (simula-skip-comment-backward)))
953 (setq start-line
954 (line-beginning-position)
955 ;; - perhaps this is a continued statement
956 continued
957 (save-excursion
958 (and (not (bobp))
959 ;; (not found-end)
960 (if (eq (char-syntax (preceding-char)) ?w)
961 (progn
962 (backward-word 1)
963 (not (looking-at
964 "begin\\|then\\|else\\|when\\|otherwise\\|do"
966 (not (memq (preceding-char) '(?: ?\;)))))))
968 ;; MAIN calculation loop - count BEGIN/DO etc.
970 (while (not (bolp))
971 (if (re-search-backward
972 ";\\|\\<\\(begin\\|end\\|if\\|else\\|then\\|when\\|otherwise\\|do\\)\\>"
973 start-line 'move)
974 (if (simula-context)
975 ();; found something in a comment/string - ignore
976 (setq temp (following-char))
977 (cond
978 ((eq temp ?\;)
979 (simula-previous-statement 1))
980 ((looking-at "begin\\>")
981 (setq indent (+ indent simula-indent-level)))
982 ((looking-at "end\\>")
983 (forward-word 1)
984 (simula-previous-statement 1))
985 ((looking-at "do\\>")
986 (setq indent (+ indent simula-substatement-offset))
987 (simula-find-do-match))
988 ((looking-at "\\(if\\|then\\|else\\)\\>")
989 (if (memq temp '(?I ?i))
990 (forward-word 1)
991 (setq indent (+ indent
992 simula-substatement-offset
993 (if (memq temp '(?T ?t))
994 (car simula-if-indent)
995 (cdr simula-if-indent)))))
996 (simula-find-if))
997 ((looking-at "\\<when\\>\\|\\<otherwise\\>")
998 (setq indent (+ indent
999 simula-substatement-offset
1000 (if (memq temp '(?W ?w))
1001 (car simula-if-indent)
1002 (cdr simula-if-indent))))
1003 (simula-find-inspect)))
1004 ;; found the start of a [sub]statement
1005 ;; add indentation for continued statement
1006 (if continued
1007 (setq indent
1008 (+ indent
1009 (if (listp simula-continued-statement-offset)
1010 (car simula-continued-statement-offset)
1011 simula-continued-statement-offset))))
1012 (setq start-line
1013 (line-beginning-position)
1014 continued nil))
1015 ;; search failed .. point is at beginning of line
1016 ;; determine if we should continue searching
1017 ;; (at or before comment or label)
1018 ;; temp = t means finished
1019 (setq temp
1020 (and (not (simula-context))
1021 (save-excursion
1022 (skip-chars-forward " \t\f")
1023 (or (looking-at "virtual")
1024 (not
1025 (looking-at
1026 "!\\|comment\\>\\|[a-z0-9_]*[ \t\f]*:[^-=]")))))
1027 prev-cont continued)
1028 ;; if we are finished, find current line's indentation
1029 (if temp
1030 (setq indent (+ indent (current-indentation))))
1031 ;; find next line with non-comment SIMULA text
1032 ;; maybe indent extra if statement continues
1033 (simula-skip-comment-backward)
1034 (setq continued
1035 (and (not (bobp))
1036 (if (eq (char-syntax (preceding-char)) ?w)
1037 (save-excursion
1038 (backward-word 1)
1039 (not (looking-at
1040 "begin\\|then\\|else\\|when\\|otherwise\\|do")))
1041 (not (memq (preceding-char) '(?: ?\;))))))
1042 ;; if we the state of the continued-variable
1043 ;; changed, add indentation for continued statement
1044 (if (or (and prev-cont (not continued))
1045 (and continued
1046 (listp simula-continued-statement-offset)))
1047 (setq indent
1048 (+ indent
1049 (if (listp simula-continued-statement-offset)
1050 (car simula-continued-statement-offset)
1051 simula-continued-statement-offset))))
1052 ;; while ends if point is at beginning of line at loop test
1053 (if (not temp)
1054 (setq start-line (line-beginning-position))
1055 (beginning-of-line))))
1057 ;; return indentation
1059 indent)))))
1062 (defun simula-find-if ()
1063 "Find starting IF of a IF-THEN[-ELSE[-IF-THEN...]] statement."
1064 (catch 'simula-out
1065 (while t
1066 (if (and (simula-search-backward "\\<if\\>\\|;\\|\\<begin\\>"nil t)
1067 (memq (following-char) '(?I ?i)))
1068 (save-excursion
1070 ;; find out if this IF was really the start of the IF statement
1072 (simula-skip-comment-backward)
1073 (if (and (eq (char-syntax (preceding-char)) ?w)
1074 (progn
1075 (backward-word 1)
1076 (looking-at "else\\>")))
1078 (throw 'simula-out t)))
1079 (if (not (looking-at "\\<if\\>"))
1080 (error "Missing IF or misplaced BEGIN or ';' (can't find IF)")
1082 ;; we were at the starting IF in the first place..
1084 (throw 'simula-out t))))))
1087 (defun simula-find-inspect ()
1088 "Find INSPECT matching WHEN or OTHERWISE."
1089 (catch 'simula-out
1090 (let ((level 0))
1092 ;; INSPECTs can be nested, have to find the corresponding one
1094 (while t
1095 (if (and (simula-search-backward "\\<inspect\\>\\|\\<otherwise\\>\\|;"
1096 nil t)
1097 (/= (following-char) ?\;))
1098 (if (memq (following-char) '(?O ?o))
1099 (setq level (1+ level))
1100 (if (zerop level)
1101 (throw 'simula-out t)
1102 (setq level (1- level))))
1103 (error "Missing INSPECT or misplaced ';' (can't find INSPECT)"))))))
1106 (defun simula-find-do-match ()
1107 "Find keyword matching DO: FOR, WHILE, INSPECT or WHEN."
1108 (while (and (re-search-backward
1109 "\\<\\(do\\|for\\|while\\|inspect\\|when\\|end\\|begin\\)\\>\\|;"
1110 nil 'move)
1111 (simula-context)))
1112 (if (and (looking-at "\\<\\(for\\|while\\|inspect\\|when\\)\\>")
1113 (not (simula-context)))
1114 () ;; found match
1115 (error "No matching FOR, WHILE or INSPECT for DO, or misplaced ';'")))
1118 (defun simula-inside-parens ()
1119 "Return position after `(' on line if inside parentheses, nil otherwise."
1120 (save-excursion
1121 (let ((parlevel 0))
1122 (catch 'simula-out
1123 (while t
1124 (if (re-search-backward "(\\|)\\|;" nil t)
1125 (if (eq (simula-context) nil)
1126 ;; found something - check it out
1127 (cond
1128 ((eq (following-char) ?\;)
1129 (if (zerop parlevel)
1130 (throw 'simula-out nil)
1131 (error "Parenthesis mismatch or misplaced ';'")))
1132 ((eq (following-char) ?\()
1133 (if (zerop parlevel)
1134 (throw 'simula-out (1+ (current-column)))
1135 (setq parlevel (1- parlevel))))
1136 (t (setq parlevel (1+ parlevel))))
1137 );; nothing - inside comment or string
1138 ;; search failed
1139 (throw 'simula-out nil)))))))
1142 (defun simula-goto-definition ()
1143 "Goto point of definition of variable, procedure or class."
1144 (interactive))
1147 (defun simula-expand-stdproc ()
1148 (if (or (not simula-abbrev-stdproc) (simula-context))
1149 (unexpand-abbrev)
1150 (cond
1151 ((eq simula-abbrev-stdproc 'upcase) (upcase-word -1))
1152 ((eq simula-abbrev-stdproc 'downcase) (downcase-word -1))
1153 ((eq simula-abbrev-stdproc 'capitalize) (capitalize-word -1))
1154 ((eq simula-abbrev-stdproc 'abbrev-table)
1155 ;; If not in lowercase, expansions are always capitalized.
1156 ;; We then want to replace with the exact expansion.
1157 (if (equal (symbol-name last-abbrev) last-abbrev-text)
1159 (downcase-word -1)
1160 (expand-abbrev))))))
1163 (defun simula-expand-keyword ()
1164 (if (or (not simula-abbrev-keyword) (simula-context))
1165 (unexpand-abbrev)
1166 (cond
1167 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
1168 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
1169 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1))
1170 ((eq simula-abbrev-stdproc 'abbrev-table)
1171 (if (equal (symbol-name last-abbrev) last-abbrev-text)
1173 (downcase-word -1)
1174 (expand-abbrev))))))
1177 (defun simula-electric-keyword ()
1178 "Expand SIMULA keyword. If it starts the line, reindent."
1179 ;; redisplay
1180 (let ((show-char (eq this-command 'self-insert-command)))
1181 ;; If the abbrev expansion results in reindentation, the user may have
1182 ;; to wait some time before the character he typed is displayed
1183 ;; (the char causing the expansion is inserted AFTER the hook function
1184 ;; is called). This is annoying in case of normal characters.
1185 ;; However, if the user pressed a key bound to newline, it is better
1186 ;; to have the line inserted after the begin-end match.
1187 (if show-char
1188 (progn
1189 (insert-char last-command-event 1)
1190 (sit-for 0)
1191 (backward-char 1)))
1192 (if (let ((where (simula-context))
1193 (case-fold-search t))
1194 (if where
1195 (if (and (eq where 2) (eq (char-syntax (preceding-char)) ?w))
1196 (save-excursion
1197 (backward-word 1)
1198 (not (looking-at "end\\>"))))))
1199 (unexpand-abbrev)
1200 (cond
1201 ((not simula-abbrev-keyword) (unexpand-abbrev))
1202 ((eq simula-abbrev-keyword 'upcase) (upcase-word -1))
1203 ((eq simula-abbrev-keyword 'downcase) (downcase-word -1))
1204 ((eq simula-abbrev-keyword 'capitalize) (capitalize-word -1)))
1205 (let ((pos (- (point-max) (point)))
1206 (case-fold-search t))
1207 (condition-case nil
1208 (progn
1209 ;; check if the expanded word is on the beginning of the line.
1210 (if (and (eq (char-syntax (preceding-char)) ?w)
1211 (progn
1212 (backward-word 1)
1213 (if (looking-at "end\\>")
1214 (save-excursion
1215 (simula-backward-up-level 1)
1216 (if (pos-visible-in-window-p)
1217 (sit-for 1)
1218 (message "Matches %s"
1219 (buffer-substring
1220 (point)
1221 (+ (point) (window-width)))))))
1222 (skip-chars-backward " \t\f")
1223 (bolp)))
1224 (let ((indent (simula-calculate-indent)))
1225 (if (eq indent (current-indentation))
1227 (delete-horizontal-space)
1228 (indent-to indent)))
1229 (skip-chars-forward " \t\f"))
1230 ;; check for END - blow whistles and ring bells
1232 (goto-char (- (point-max) pos))
1233 (if show-char
1234 (delete-char 1)))
1235 (quit (goto-char (- (point-max) pos))))))))
1238 (defun simula-search-backward (regexp &optional bound noerror)
1239 "Search backward from point for regular expression REGEXP,
1240 ignoring matches found inside SIMULA comments, string literals,
1241 and BEGIN..END blocks.
1242 Set point to the end of the occurrence found, and return point.
1243 An optional second argument BOUND bounds the search, it is a buffer position.
1244 The match found must not extend after that position. Optional third argument
1245 NOERROR, if t, means if fail just return nil (no error).
1246 If not nil and not t, move to limit of search and return nil."
1247 (let ((comb-regexp (concat regexp "\\|\\<end\\>"))
1248 (start-point (point))
1249 context match)
1250 (catch 'simula-backward
1251 (while (re-search-backward comb-regexp bound 1)
1252 ;; We have a match, check SIMULA context at match-beginning
1253 ;; to see if we are outside comments etc.
1254 ;; Set MATCH to t if we found a true match,
1255 ;; set MATCH to 'BLOCK if we found a BEGIN..END block,
1256 ;; else set MATCH to nil.
1257 (save-match-data
1258 (setq context (simula-context))
1259 (cond
1260 ((eq context nil)
1261 (setq match (if (looking-at regexp) t 'BLOCK)))
1262 ;; A comment-ending `;' is part of the comment, and shouldn't match.
1263 ;; ((eq context 0)
1264 ;; (setq match (if (eq (following-char) ?\;) t nil)))
1265 ((eq context 2)
1266 (setq match (if (and (looking-at regexp)
1267 (looking-at ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>"))
1269 (if (looking-at "\\<end\\>") 'BLOCK nil))))
1270 (t (setq match nil))))
1271 ;; Exit if true match
1272 (if (eq match t) (throw 'simula-backward (point)))
1273 (if (eq match 'BLOCK)
1274 ;; We found the END of a block
1275 (let ((level 0))
1276 (while (natnump level)
1277 (if (re-search-backward "\\<begin\\>\\|\\<end\\>" bound 1)
1278 (let ((context (simula-context)))
1279 ;; We found a BEGIN -> decrease level count
1280 (cond ((and (eq context nil)
1281 (memq (following-char) '(?b ?B)))
1282 (setq level (1- level)))
1283 ;; END -> increase level count
1284 ((and (memq context '(nil 2))
1285 (memq (following-char) '(?e ?E)))
1286 (setq level (1+ level)))))
1287 ;; Block search failed. Action depends on noerror.
1288 (if (or (not noerror) (eq noerror t))
1289 (goto-char start-point))
1290 (if (not noerror)
1291 (signal 'search-failed (list regexp)))
1292 (throw 'simula-backward nil))))))
1293 ;; Search failed. Action depends on noerror.
1294 (if (or (not noerror) (eq noerror t))
1295 (goto-char start-point))
1296 (if noerror
1298 (signal 'search-failed (list regexp))))))
1301 (defun simula-search-forward (regexp &optional bound noerror)
1302 "Search forward from point for regular expression REGEXP,
1303 ignoring matches found inside SIMULA comments, string literals,
1304 and BEGIN..END blocks.
1305 Set point to the end of the occurrence found, and return point.
1306 An optional second argument BOUND bounds the search, it is a buffer position.
1307 The match found must not extend after that position. Optional third argument
1308 NOERROR, if t, means if fail just return nil (no error).
1309 If not nil and not t, move to limit of search and return nil."
1310 (let ((comb-regexp (concat regexp "\\|\\<begin\\>"))
1311 (start-point (point))
1312 context match)
1313 (catch 'simula-forward
1314 (while (re-search-forward comb-regexp bound 1)
1315 ;; We have a match, check SIMULA context at match-beginning
1316 ;; to see if we are outside comments.
1317 ;; Set MATCH to t if we found a true match,
1318 ;; set MATCH to 'BLOCK if we found a BEGIN..END block,
1319 ;; else set MATCH to nil.
1320 (save-match-data
1321 (save-excursion
1322 (goto-char (match-beginning 0))
1323 (setq context (simula-context))
1324 (cond
1325 ((not context)
1326 (setq match (if (looking-at regexp) t 'BLOCK)))
1327 ;; Comment-ending `;' is part of the comment, and shouldn't match.
1328 ;; ((eq context 0)
1329 ;; (setq match (if (eq (following-char) ?\;) t nil)))
1330 ((eq context 2)
1331 (setq match (if (and (looking-at regexp)
1332 (looking-at ";\\|\\<end\\>\\|\\<else\\>\\|\\<otherwise\\>\\|\\<when\\>")) t nil)))
1333 (t (setq match nil)))))
1334 ;; Exit if true match
1335 (if (eq match t) (throw 'simula-forward (point)))
1336 (if (eq match 'BLOCK)
1337 ;; We found the BEGINning of a block
1338 (let ((level 0))
1339 (while (natnump level)
1340 (if (re-search-forward "\\<begin\\>\\|\\<end\\>" bound 1)
1341 (let ((context (simula-context)))
1342 ;; We found a BEGIN -> increase level count
1343 (cond ((eq context nil) (setq level (1+ level)))
1344 ;; END -> decrease level count
1345 ((and (eq context 2)
1346 ;; Don't match BEGIN inside END comment
1347 (memq (preceding-char) '(?d ?D)))
1348 (setq level (1- level)))))
1349 ;; Block search failed. Action depends on noerror.
1350 (if (or (not noerror) (eq noerror t))
1351 (goto-char start-point))
1352 (if (not noerror)
1353 (signal 'search-failed (list regexp)))
1354 (throw 'simula-forward nil))))))
1355 ;; Search failed. Action depends on noerror.
1356 (if (or (not noerror) (eq noerror t))
1357 (goto-char start-point))
1358 (if noerror
1360 (signal 'search-failed (list regexp))))))
1363 (defun simula-install-standard-abbrevs ()
1364 "Define Simula keywords, procedures and classes in local abbrev table."
1365 ;; procedure and class names are as of the SIMULA 87 standard.
1366 (interactive)
1367 (dolist (args
1368 '(("abs" "Abs" simula-expand-stdproc)
1369 ("accum" "Accum" simula-expand-stdproc)
1370 ("activate" "ACTIVATE" simula-expand-keyword)
1371 ("addepsilon" "AddEpsilon" simula-expand-stdproc)
1372 ("after" "AFTER" simula-expand-keyword)
1373 ("and" "AND" simula-expand-keyword)
1374 ("arccos" "ArcCos" simula-expand-stdproc)
1375 ("arcsin" "ArcSin" simula-expand-stdproc)
1376 ("arctan" "ArcTan" simula-expand-stdproc)
1377 ("arctan2" "ArcTan2" simula-expand-stdproc)
1378 ("array" "ARRAY" simula-expand-keyword)
1379 ("at" "AT" simula-expand-keyword)
1380 ("before" "BEFORE" simula-expand-keyword)
1381 ("begin" "BEGIN" simula-expand-keyword)
1382 ("blanks" "Blanks" simula-expand-stdproc)
1383 ("boolean" "BOOLEAN" simula-expand-keyword)
1384 ("breakoutimage" "BreakOutImage" simula-expand-stdproc)
1385 ("bytefile" "ByteFile" simula-expand-stdproc)
1386 ("call" "Call" simula-expand-stdproc)
1387 ("cancel" "Cancel" simula-expand-stdproc)
1388 ("cardinal" "Cardinal" simula-expand-stdproc)
1389 ("char" "Char" simula-expand-stdproc)
1390 ("character" "CHARACTER" simula-expand-keyword)
1391 ("checkpoint" "CheckPoint" simula-expand-stdproc)
1392 ("class" "CLASS" simula-expand-keyword)
1393 ("clear" "Clear" simula-expand-stdproc)
1394 ("clocktime" "ClockTime" simula-expand-stdproc)
1395 ("close" "Close" simula-expand-stdproc)
1396 ("comment" "COMMENT" simula-expand-keyword)
1397 ("constant" "Constant" simula-expand-stdproc)
1398 ("copy" "Copy" simula-expand-stdproc)
1399 ("cos" "Cos" simula-expand-stdproc)
1400 ("cosh" "CosH" simula-expand-stdproc)
1401 ("cotan" "CoTan" simula-expand-stdproc)
1402 ("cputime" "CpuTime" simula-expand-stdproc)
1403 ("current" "Current" simula-expand-stdproc)
1404 ("datetime" "DateTime" simula-expand-stdproc)
1405 ("decimalmark" "DecimalMark" simula-expand-stdproc)
1406 ("delay" "DELAY" simula-expand-keyword)
1407 ("deleteimage" "DeleteImage" simula-expand-stdproc)
1408 ("detach" "Detach" simula-expand-stdproc)
1409 ("digit" "Digit" simula-expand-stdproc)
1410 ("directbytefile" "DirectByteFile" simula-expand-stdproc)
1411 ("directfile" "DirectFile" simula-expand-stdproc)
1412 ("discrete" "Discrete" simula-expand-stdproc)
1413 ("do" "DO" simula-expand-keyword)
1414 ("downcase" "Downcase" simula-expand-stdproc)
1415 ("draw" "Draw" simula-expand-stdproc)
1416 ("eject" "Eject" simula-expand-stdproc)
1417 ("else" "ELSE" simula-electric-keyword)
1418 ("empty" "Empty" simula-expand-stdproc)
1419 ("end" "END" simula-electric-keyword)
1420 ("endfile" "Endfile" simula-expand-stdproc)
1421 ("entier" "Entier" simula-expand-stdproc)
1422 ("eq" "EQ" simula-expand-keyword)
1423 ("eqv" "EQV" simula-expand-keyword)
1424 ("erlang" "Erlang" simula-expand-stdproc)
1425 ("error" "Error" simula-expand-stdproc)
1426 ("evtime" "EvTime" simula-expand-stdproc)
1427 ("exp" "Exp" simula-expand-stdproc)
1428 ("external" "EXTERNAL" simula-expand-keyword)
1429 ("false" "FALSE" simula-expand-keyword)
1430 ("field" "Field" simula-expand-stdproc)
1431 ("file" "File" simula-expand-stdproc)
1432 ("first" "First" simula-expand-stdproc)
1433 ("follow" "Follow" simula-expand-stdproc)
1434 ("for" "FOR" simula-expand-keyword)
1435 ("ge" "GE" simula-expand-keyword)
1436 ("getchar" "GetChar" simula-expand-stdproc)
1437 ("getfrac" "GetFrac" simula-expand-stdproc)
1438 ("getint" "GetInt" simula-expand-stdproc)
1439 ("getreal" "GetReal" simula-expand-stdproc)
1440 ("go" "GO" simula-expand-keyword)
1441 ("goto" "GOTO" simula-expand-keyword)
1442 ("gt" "GT" simula-expand-keyword)
1443 ("head" "Head" simula-expand-stdproc)
1444 ("hidden" "HIDDEN" simula-expand-keyword)
1445 ("histd" "HistD" simula-expand-stdproc)
1446 ("histo" "Histo" simula-expand-stdproc)
1447 ("hold" "Hold" simula-expand-stdproc)
1448 ("idle" "Idle" simula-expand-stdproc)
1449 ("if" "IF" simula-expand-keyword)
1450 ("image" "Image" simula-expand-stdproc)
1451 ("imagefile" "ImageFile" simula-expand-stdproc)
1452 ("imp" "IMP" simula-expand-keyword)
1453 ("in" "IN" simula-expand-keyword)
1454 ("inbyte" "InByte" simula-expand-stdproc)
1455 ("inbytefile" "InByteFile" simula-expand-stdproc)
1456 ("inchar" "InChar" simula-expand-stdproc)
1457 ("infile" "InFile" simula-expand-stdproc)
1458 ("infrac" "InFrac" simula-expand-stdproc)
1459 ("inimage" "InImage" simula-expand-stdproc)
1460 ("inint" "InInt" simula-expand-stdproc)
1461 ("inner" "INNER" simula-expand-keyword)
1462 ("inreal" "InReal" simula-expand-stdproc)
1463 ("inrecord" "InRecord" simula-expand-stdproc)
1464 ("inspect" "INSPECT" simula-expand-keyword)
1465 ("integer" "INTEGER" simula-expand-keyword)
1466 ("intext" "InText" simula-expand-stdproc)
1467 ("into" "Into" simula-expand-stdproc)
1468 ("is" "IS" simula-expand-keyword)
1469 ("isochar" "ISOChar" simula-expand-stdproc)
1470 ("isopen" "IsOpen" simula-expand-stdproc)
1471 ("isorank" "ISORank" simula-expand-stdproc)
1472 ("label" "LABEL" simula-expand-keyword)
1473 ("last" "Last" simula-expand-stdproc)
1474 ("lastitem" "LastItem" simula-expand-stdproc)
1475 ("lastloc" "LastLoc" simula-expand-stdproc)
1476 ("le" "LE" simula-expand-keyword)
1477 ("length" "Length" simula-expand-stdproc)
1478 ("letter" "Letter" simula-expand-stdproc)
1479 ("line" "Line" simula-expand-stdproc)
1480 ("linear" "Linear" simula-expand-stdproc)
1481 ("linesperpage" "LinesPerPage" simula-expand-stdproc)
1482 ("link" "Link" simula-expand-stdproc)
1483 ("linkage" "Linkage" simula-expand-stdproc)
1484 ("ln" "Ln" simula-expand-stdproc)
1485 ("locate" "Locate" simula-expand-stdproc)
1486 ("location" "Location" simula-expand-stdproc)
1487 ("lock" "Lock" simula-expand-stdproc)
1488 ("locked" "Locked" simula-expand-stdproc)
1489 ("log10" "Log10" simula-expand-stdproc)
1490 ("long" "LONG" simula-expand-keyword)
1491 ("lowcase" "LowCase" simula-expand-stdproc)
1492 ("lowerbound" "LowerBound" simula-expand-stdproc)
1493 ("lowten" "LowTen" simula-expand-stdproc)
1494 ("lt" "LT" simula-expand-keyword)
1495 ("main" "Main" simula-expand-stdproc)
1496 ("max" "Max" simula-expand-stdproc)
1497 ("maxint" "MaxInt" simula-expand-stdproc)
1498 ("maxlongreal" "MaxLongReal" simula-expand-stdproc)
1499 ("maxloc" "MaxLoc" simula-expand-stdproc)
1500 ("maxrank" "MaxRank" simula-expand-stdproc)
1501 ("maxreal" "MaxReal" simula-expand-stdproc)
1502 ("min" "Min" simula-expand-stdproc)
1503 ("minint" "MinInt" simula-expand-stdproc)
1504 ("minlongreal" "MinLongReal" simula-expand-stdproc)
1505 ("minrank" "MinRank" simula-expand-stdproc)
1506 ("minreal" "MinReal" simula-expand-stdproc)
1507 ("mod" "Mod" simula-expand-stdproc)
1508 ("more" "More" simula-expand-stdproc)
1509 ("name" "NAME" simula-expand-keyword)
1510 ("ne" "NE" simula-expand-keyword)
1511 ("negexp" "NegExp" simula-expand-stdproc)
1512 ("new" "NEW" simula-expand-keyword)
1513 ("nextev" "NextEv" simula-expand-stdproc)
1514 ("none" "NONE" simula-expand-keyword)
1515 ("normal" "Normal" simula-expand-stdproc)
1516 ("not" "NOT" simula-expand-keyword)
1517 ("notext" "NOTEXT" simula-expand-keyword)
1518 ("open" "Open" simula-expand-stdproc)
1519 ("or" "OR" simula-expand-keyword)
1520 ("otherwise" "OTHERWISE" simula-electric-keyword)
1521 ("out" "Out" simula-expand-stdproc)
1522 ("outbyte" "OutByte" simula-expand-stdproc)
1523 ("outbytefile" "OutByteFile" simula-expand-stdproc)
1524 ("outchar" "OutChar" simula-expand-stdproc)
1525 ("outfile" "OutFile" simula-expand-stdproc)
1526 ("outfix" "OutFix" simula-expand-stdproc)
1527 ("outfrac" "OutFrac" simula-expand-stdproc)
1528 ("outimage" "OutImage" simula-expand-stdproc)
1529 ("outint" "OutInt" simula-expand-stdproc)
1530 ("outreal" "OutReal" simula-expand-stdproc)
1531 ("outrecord" "OutRecord" simula-expand-stdproc)
1532 ("outtext" "OutText" simula-expand-stdproc)
1533 ("page" "Page" simula-expand-stdproc)
1534 ("passivate" "Passivate" simula-expand-stdproc)
1535 ("poisson" "Poisson" simula-expand-stdproc)
1536 ("pos" "Pos" simula-expand-stdproc)
1537 ("precede" "Precede" simula-expand-stdproc)
1538 ("pred" "Pred" simula-expand-stdproc)
1539 ("prev" "Prev" simula-expand-stdproc)
1540 ("printfile" "PrintFile" simula-expand-stdproc)
1541 ("prior" "PRIOR" simula-expand-keyword)
1542 ("procedure" "PROCEDURE" simula-expand-keyword)
1543 ("process" "Process" simula-expand-stdproc)
1544 ("protected" "PROTECTED" simula-expand-keyword)
1545 ("putchar" "PutChar" simula-expand-stdproc)
1546 ("putfix" "PutFix" simula-expand-stdproc)
1547 ("putfrac" "PutFrac" simula-expand-stdproc)
1548 ("putint" "PutInt" simula-expand-stdproc)
1549 ("putreal" "PutReal" simula-expand-stdproc)
1550 ("qua" "QUA" simula-expand-keyword)
1551 ("randint" "RandInt" simula-expand-stdproc)
1552 ("rank" "Rank" simula-expand-stdproc)
1553 ("reactivate" "REACTIVATE" simula-expand-keyword)
1554 ("real" "REAL" simula-expand-keyword)
1555 ("ref" "REF" simula-expand-keyword)
1556 ("resume" "Resume" simula-expand-stdproc)
1557 ("setaccess" "SetAccess" simula-expand-stdproc)
1558 ("setpos" "SetPos" simula-expand-stdproc)
1559 ("short" "SHORT" simula-expand-keyword)
1560 ("sign" "Sign" simula-expand-stdproc)
1561 ("simset" "SimSet" simula-expand-stdproc)
1562 ("simulaid" "SimulaId" simula-expand-stdproc)
1563 ("simulation" "Simulation" simula-expand-stdproc)
1564 ("sin" "Sin" simula-expand-stdproc)
1565 ("sinh" "SinH" simula-expand-stdproc)
1566 ("sourceline" "SourceLine" simula-expand-stdproc)
1567 ("spacing" "Spacing" simula-expand-stdproc)
1568 ("sqrt" "Sqrt" simula-expand-stdproc)
1569 ("start" "Start" simula-expand-stdproc)
1570 ("step" "STEP" simula-expand-keyword)
1571 ("strip" "Strip" simula-expand-stdproc)
1572 ("sub" "Sub" simula-expand-stdproc)
1573 ("subepsilon" "SubEpsilon" simula-expand-stdproc)
1574 ("suc" "Suc" simula-expand-stdproc)
1575 ("switch" "SWITCH" simula-expand-keyword)
1576 ("sysin" "SysIn" simula-expand-stdproc)
1577 ("sysout" "SysOut" simula-expand-stdproc)
1578 ("tan" "Tan" simula-expand-stdproc)
1579 ("tanh" "TanH" simula-expand-stdproc)
1580 ("terminate_program" "Terminate_Program" simula-expand-stdproc)
1581 ("terminated" "Terminated" simula-expand-stdproc)
1582 ("text" "TEXT" simula-expand-keyword)
1583 ("then" "THEN" simula-electric-keyword)
1584 ("this" "THIS" simula-expand-keyword)
1585 ("time" "Time" simula-expand-stdproc)
1586 ("to" "TO" simula-expand-keyword)
1587 ("true" "TRUE" simula-expand-keyword)
1588 ("uniform" "Uniform" simula-expand-stdproc)
1589 ("unlock" "Unlock" simula-expand-stdproc)
1590 ("until" "UNTIL" simula-expand-keyword)
1591 ("upcase" "Upcase" simula-expand-stdproc)
1592 ("upperbound" "UpperBound" simula-expand-stdproc)
1593 ("value" "VALUE" simula-expand-keyword)
1594 ("virtual" "VIRTUAL" simula-expand-keyword)
1595 ("wait" "Wait" simula-expand-stdproc)
1596 ("when" "WHEN" simula-electric-keyword)
1597 ("while" "WHILE" simula-expand-keyword)))
1598 (define-abbrev simula-mode-abbrev-table
1599 (nth 0 args) (nth 1 args) (nth 2 args) nil 'system)))
1601 (if simula-abbrev-file
1602 (read-abbrev-file simula-abbrev-file))
1603 (let (abbrevs-changed)
1604 (simula-install-standard-abbrevs))
1606 ;; Hilit mode support.
1607 (when (fboundp 'hilit-set-mode-patterns)
1608 (when (and (boundp 'hilit-patterns-alist)
1609 (not (assoc 'simula-mode hilit-patterns-alist)))
1610 (hilit-set-mode-patterns
1611 'simula-mode
1613 ("^%\\([ \t\f].*\\)?$" nil comment)
1614 ("^%include\\>" nil include)
1615 ("\"[^\"\n]*\"\\|'.'\\|'![0-9]+!'" nil string)
1616 ("\\<\\(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)
1617 ("!\\|\\<COMMENT\\>" ";" comment))
1618 nil 'case-insensitive)))
1620 ;; defuns for submitting bug reports
1622 (defconst simula-mode-help-address "bug-gnu-emacs@gnu.org"
1623 "Address accepting submission of `simula-mode' bug reports.")
1625 (defun simula-submit-bug-report ()
1626 "Submit via mail a bug report on `simula-mode'."
1627 (interactive)
1628 (and
1629 (y-or-n-p "Do you want to submit a report on simula-mode? ")
1630 (reporter-submit-bug-report
1631 simula-mode-help-address
1632 (concat "simula-mode from Emacs " emacs-version)
1633 (list
1634 ;; report only the vars that affect indentation
1635 'simula-indent-level
1636 'simula-substatement-offset
1637 'simula-continued-statement-offset
1638 'simula-label-offset
1639 'simula-if-indent
1640 'simula-inspect-indent
1641 'simula-electric-indent
1642 'simula-abbrev-keyword
1643 'simula-abbrev-stdproc
1644 'simula-abbrev-file
1645 'simula-tab-always-indent
1646 ))))
1648 (provide 'simula)
1650 ;;; simula.el ends here