Skip to content

Instantly share code, notes, and snippets.

@tangentstorm
Created March 2, 2015 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangentstorm/26eed3acdc7a5c7df237 to your computer and use it in GitHub Desktop.
Save tangentstorm/26eed3acdc7a5c7df237 to your computer and use it in GitHub Desktop.
;;; oberon.el --- major mode for editing oberon source in Emacs
;; Copyright (C) 1993, 94, 95, 96, 97, 98, 1999, 2000 Free Software Foundation, Inc.
;; Copyright (C) 2003 Aaron Isotton. Based on Espen Skoglund's pascal-mode.
;; I wrote this not because I think that Oberon is a good (or even
;; usable) language, but because at ETHZ we're forced to use it. So
;; at least I can use emacs to edit the sources. Oberon is evil and
;; sucks.
;; Author: Aaron Isotton <aaron@isotton.com>
;; Keywords: languages
;; 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
;; the Free Software Foundation; either version 2, or (at your option)
;; 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
;; 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.
;;; Commentary:
;; USAGE
;; =====
;; Emacs should enter Oberon mode when you find a Oberon source file.
;; When you have entered Oberon mode, you may get more info by pressing
;; C-h m. You may also get online help describing various functions by:
;; C-h f <Name of function you want described>
;; If you want to customize Oberon mode to fit you better, you may add
;; these lines (the values of the variables presented here are the defaults):
;;
;; ;; User customization for Oberon mode
;; (setq oberon-indent-level 3
;; oberon-case-indent 2
;; oberon-auto-newline nil
;; oberon-tab-always-indent t
;; oberon-auto-lineup '(all)
;; oberon-toggle-completions nil
;; oberon-type-keywords '("ARRAY" "FILE" "PACKED" "CHAR"
;; "INTEGER" "REAL" "STRING" "RECORD")
;; oberon-start-keywords '("BEGIN" "END" "FUNCTION" "PROCEDURE"
;; "REPEAT" "UNTIL" "WHILE" "READ" "READLN"
;; "RESET" "REWRITE" "WRITE" "WRITELN")
;; oberon-separator-keywords '("DOWNTO" "ELSE" "MOD" "DIV" "THEN"))
;; KNOWN BUGS / BUGREPORTS
;; =======================
;; As far as I know, there are no bugs in the current version of this
;; package. This may not be true however, since I never use this mode
;; myself and therefore would never notice them anyway. If you do
;; find any bugs, you may submit them to: esk@gnu.org as well as to
;; bug-gnu-emacs@gnu.org.
;;; Code:
(defgroup oberon nil
"Major mode for editing Oberon source in Emacs"
:group 'languages)
(defvar oberon-mode-abbrev-table nil
"Abbrev table in use in Oberon-mode buffers.")
(define-abbrev-table 'oberon-mode-abbrev-table ())
(defvar oberon-mode-map ()
"Keymap used in Oberon mode.")
(if oberon-mode-map
()
(setq oberon-mode-map (make-sparse-keymap))
(define-key oberon-mode-map ";" 'electric-oberon-semi-or-dot)
(define-key oberon-mode-map "." 'electric-oberon-semi-or-dot)
(define-key oberon-mode-map ":" 'electric-oberon-colon)
(define-key oberon-mode-map "=" 'electric-oberon-equal)
(define-key oberon-mode-map "#" 'electric-oberon-hash)
(define-key oberon-mode-map "\r" 'electric-oberon-terminate-line)
(define-key oberon-mode-map "\t" 'electric-oberon-tab)
(define-key oberon-mode-map "\M-\t" 'oberon-complete-word)
(define-key oberon-mode-map "\M-?" 'oberon-show-completions)
(define-key oberon-mode-map "\177" 'backward-delete-char-untabify)
(define-key oberon-mode-map "\M-\C-h" 'oberon-mark-defun)
(define-key oberon-mode-map "\C-c\C-b" 'oberon-insert-block)
(define-key oberon-mode-map "\M-*" 'oberon-star-comment)
(define-key oberon-mode-map "\C-c\C-c" 'oberon-comment-area)
(define-key oberon-mode-map "\C-c\C-u" 'oberon-uncomment-area)
(define-key oberon-mode-map "\M-\C-a" 'oberon-beg-of-defun)
(define-key oberon-mode-map "\M-\C-e" 'oberon-end-of-defun)
(define-key oberon-mode-map "\C-c\C-d" 'oberon-goto-defun)
(define-key oberon-mode-map "\C-c\C-o" 'oberon-outline)
;;; A command to change the whole buffer won't be used terribly
;;; often, so no need for a key binding.
; (define-key oberon-mode-map "\C-cd" 'oberon-downcase-keywords)
; (define-key oberon-mode-map "\C-cu" 'oberon-upcase-keywords)
; (define-key oberon-mode-map "\C-cc" 'oberon-capitalize-keywords)
)
(defvar oberon-imenu-generic-expression
'((nil "^[ \t]*\\(FUNCTION\\|PROCEDURE\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" 2))
"Imenu expression for Oberon-mode. See `imenu-generic-expression'.")
(defvar oberon-keywords
'("ARRAY" "BEGIN" "BY" "CASE" "CONST" "DIV" "DO" "ELSE" "ELSIF" "END" "EXIT"
"FOR" "IF" "IMPORT" "IN" "IS" "LOOP" "MOD" "MODULE" "NIL" "OF" "OR"
"POINTER" "PROCEDURE" "RECORD" "REPEAT" "RETURN" "THEN" "TO" "TYPE" "UNTIL"
"VAR" "WHILE" "WITH" "ABS" "ASH" "CAP" "CHR" "COPY" "DEC" "ENTIER" "EXCL"
"FALSE" "HALT" "INC" "INCL" "LEN" "LONG" "MAX" "MIN" "NEW" "ODD" "ORD"
"SET" "SHORT" "SHORTINT" "SIZE" "TRUE"))
;;;
;;; Regular expressions used to calculate indent, etc.
;;;
(defconst oberon-symbol-re "\\<[A-Z_][A-Z_0-9.]*\\>")
(defconst oberon-beg-block-re "\\<\\(BEGIN\\|CASE\\|FOR\\|IF\\|LOOP\\|RECORD\\|REPEAT\\|WHILE\\|WITH\\)\\>")
(defconst oberon-end-block-re "\\<\\(END\\|UNTIL\\)\\>")
(defconst oberon-declaration-re "\\<\\(CONST\\|LABEL\\|TYPE\\|VAR\\)\\>")
(defconst oberon-progbeg-re "\\<MODULE\\>")
(defconst oberon-defun-re "\\<PROCEDURE\\>")
(defconst oberon-sub-block-re "\\<\\(CASE\\|ELSE\\|ELSIF\\|FOR\\|IF\\|LOOP\\|REPEAT\\|WITH\\)\\>")
(defconst oberon-noindent-re "\\<\\(BEGIN\\|END\\|ELSE\\|ELSIF\\|UNTIL\\)\\>")
(defconst oberon-nosemi-re "\\<\\(BEGIN\\|REPEAT\\|THEN\\|DO\\|ELSE\\|ELSIF\\)\\>")
(defconst oberon-autoindent-lines-re
"\\<\\(BEGIN\\|CONST\\|ELSE\\|ELSIF\\|END\\|FOR\\|IF\\|LOOP\\|REPEAT\\|TYPE\\|UNTIL\\|VAR\\|WHILE\\|WITH\\)\\>")
;;; Strings used to mark beginning and end of excluded text
(defconst oberon-exclude-str-start "{-----\\/----- EXCLUDED -----\\/-----")
(defconst oberon-exclude-str-end " -----/\\----- EXCLUDED -----/\\-----}")
(defvar oberon-mode-syntax-table nil
"Syntax table in use in Oberon-mode buffers.")
(if oberon-mode-syntax-table
()
(setq oberon-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\\ "." oberon-mode-syntax-table)
(modify-syntax-entry ?( "()1" oberon-mode-syntax-table)
(modify-syntax-entry ?) ")(4" oberon-mode-syntax-table)
(modify-syntax-entry ?* ". 23b" oberon-mode-syntax-table)
(modify-syntax-entry ?{ "<" oberon-mode-syntax-table)
(modify-syntax-entry ?} ">" oberon-mode-syntax-table)
(modify-syntax-entry ?+ "." oberon-mode-syntax-table)
(modify-syntax-entry ?- "." oberon-mode-syntax-table)
(modify-syntax-entry ?= "." oberon-mode-syntax-table)
(modify-syntax-entry ?% "." oberon-mode-syntax-table)
(modify-syntax-entry ?< "." oberon-mode-syntax-table)
(modify-syntax-entry ?> "." oberon-mode-syntax-table)
(modify-syntax-entry ?& "." oberon-mode-syntax-table)
(modify-syntax-entry ?| "." oberon-mode-syntax-table)
(modify-syntax-entry ?_ "_" oberon-mode-syntax-table)
(modify-syntax-entry ?\' "\"" oberon-mode-syntax-table))
(defconst oberon-font-lock-keywords (purecopy
(list
'("^[ \t]*\\(MODULE\\|PROCEDURE\\|END\\)\\>[ \t]*\\([A-Za-z]\\)"
1 font-lock-keyword-face)
'("^[ \t]*\\(MODULE\\|PROCEDURE\\|END\\)\\>[ \t]*\\([A-Za-z][A-Za-z0-9_]*\\)"
2 font-lock-function-name-face t)
(cons (concat "\\<\\(ARRAY\\|BOOLEAN\\|CONST\\|INTEGER\\|LONGINT\\|LONGREAL"
"\\|OF\\|POINTER\\|REAL\\|RECORD\\|SET\\|SHORT\\|TYPE\\|"
"VAR\\)\\>")
'font-lock-type-face)
'("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face)
(concat "\\<\\(BEGIN\\|BY\\|CASE\\|DIV\\|DO\\|ELSE\\|ELSIF\\|END\\|EXIT\\|"
"FOR\\|IF\\|IMPORT\\|IN\\|IS\\|LOOP\\|MOD\\|NIL\\|OR\\|REPEAT\\|"
"RETURN\\|THEN\\|TO\\|UNTIL\\|WHILE\\|WITH\\|ABS\\|ASH\\|CAP\\|"
"CHR\\|COPY\\|DEC\\|ENTIER\\|EXCL\\|FALSE\\|HALT\\|INC\\|INCL\\|"
"LEN\\|MAX\\|MIN\\|NEW\\|ODD\\|ORD\\|SIZE\\|TRUE\\)\\>")
'("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
1 font-lock-keyword-face)
'("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
2 font-lock-keyword-face t)))
"Additional expressions to highlight in Oberon mode.")
(put 'oberon-mode 'font-lock-defaults '(oberon-font-lock-keywords nil nil))
(defcustom oberon-indent-level 3
"*Indentation of Oberon statements with respect to containing block."
:type 'integer
:group 'oberon)
(defcustom oberon-case-indent 2
"*Indentation for case statements."
:type 'integer
:group 'oberon)
(defcustom oberon-auto-newline nil
"*Non-nil means automatically insert newlines in certain cases.
These include after semicolons and after the punctuation mark after an `end'."
:type 'boolean
:group 'oberon)
(defcustom oberon-indent-nested-functions t
"*Non-nil means nested functions are indented."
:type 'boolean
:group 'oberon)
(defcustom oberon-tab-always-indent t
"*Non-nil means TAB in Oberon mode should always reindent the current line.
If this is nil, TAB inserts a tab if it is at the end of the line
and follows non-whitespace text."
:type 'boolean
:group 'oberon)
(defcustom oberon-auto-lineup '(all)
"*List of contexts where auto lineup of :'s or ='s should be done.
Elements can be of type: 'paramlist', 'declaration' or 'case', which will
do auto lineup in parameterlist, declarations or case-statements
respectively. The word 'all' will do all lineups. '(case paramlist) for
instance will do lineup in case-statements and parameterlist, while '(all)
will do all lineups."
:type '(set :extra-offset 8
(const :tag "Everything" all)
(const :tag "Parameter lists" paramlist)
(const :tag "Decalrations" declaration)
(const :tag "Case statements" case))
:group 'oberon)
(defcustom oberon-toggle-completions nil
"*Non-nil means \\<oberon-mode-map>\\[oberon-complete-word] should try all possible completions one by one.
Repeated use of \\[oberon-complete-word] will show you all of them.
Normally, when there is more than one possible completion,
it displays a list of all possible completions."
:type 'boolean
:group 'oberon)
(defcustom oberon-type-keywords
'("ARRAY" "FILE" "PACKED" "CHAR" "INTEGER" "REAL" "STRING" "RECORD")
"*Keywords for types used when completing a word in a declaration or parmlist.
These include integer, real, char, etc.
The types defined within the Oberon program
are handled in another way, and should not be added to this list."
:type '(repeat (string :tag "Keyword"))
:group 'oberon)
(defcustom oberon-start-keywords
'("BEGIN" "END" "FUNCTION" "PROCEDURE" "REPEAT" "UNTIL" "WHILE"
"READ" "READLN" "RESET" "REWRITE" "WRITE" "WRITELN")
"*Keywords to complete when standing at the first word of a statement.
These are keywords such as begin, repeat, until, readln.
The procedures and variables defined within the Oberon program
are handled in another way, and should not be added to this list."
:type '(repeat (string :tag "Keyword"))
:group 'oberon)
(defcustom oberon-separator-keywords
'("DOWNTO" "ELSE" "MOD" "DIV" "THEN")
"*Keywords to complete when NOT standing at the first word of a statement.
These are keywords such as downto, else, mod, then.
Variables and function names defined within the Oberon program
are handled in another way, and should not be added to this list."
:type '(repeat (string :tag "Keyword"))
:group 'oberon)
;;;
;;; Macros
;;;
(defsubst oberon-get-beg-of-line (&optional arg)
(save-excursion
(beginning-of-line arg)
(point)))
(defsubst oberon-get-end-of-line (&optional arg)
(save-excursion
(end-of-line arg)
(point)))
(defun oberon-declaration-end ()
(let ((nest 1))
(while (and (> nest 0)
(re-search-forward
"[:=]\\|\\(\\<RECORD\\>\\)\\|\\(\\<END\\>\\)"
(save-excursion (end-of-line 2) (point)) t))
(cond ((match-beginning 1) (setq nest (1+ nest)))
((match-beginning 2) (setq nest (1- nest)))
((looking-at "[^(\n]+)") (setq nest 0))))))
(defun oberon-declaration-beg ()
(let ((nest 1))
(while (and (> nest 0)
(re-search-backward "[:=]\\|\\<\\(TYPE\\|VAR\\|LABEL\\|CONST\\)\\>\\|\\(\\<RECORD\\>\\)\\|\\(\\<END\\>\\)" (oberon-get-beg-of-line 0) t))
(cond ((match-beginning 1) (setq nest 0))
((match-beginning 2) (setq nest (1- nest)))
((match-beginning 3) (setq nest (1+ nest)))))
(= nest 0)))
(defsubst oberon-within-string ()
(save-excursion
(nth 3 (parse-partial-sexp (oberon-get-beg-of-line) (point)))))
;;;###autoload
(defun oberon-mode ()
"Major mode for editing Oberon code. \\<oberon-mode-map>
TAB indents for Oberon code. Delete converts tabs to spaces as it moves back.
\\[oberon-complete-word] completes the word around current point with respect \
to position in code
\\[oberon-show-completions] shows all possible completions at this point.
Other useful functions are:
\\[oberon-mark-defun]\t- Mark function.
\\[oberon-insert-block]\t- insert begin ... end;
\\[oberon-star-comment]\t- insert (* ... *)
\\[oberon-comment-area]\t- Put marked area in a comment, fixing nested comments.
\\[oberon-uncomment-area]\t- Uncomment an area commented with \
\\[oberon-comment-area].
\\[oberon-beg-of-defun]\t- Move to beginning of current function.
\\[oberon-end-of-defun]\t- Move to end of current function.
\\[oberon-goto-defun]\t- Goto function prompted for in the minibuffer.
\\[oberon-outline]\t- Enter oberon-outline-mode (see also oberon-outline).
Variables controlling indentation/edit style:
oberon-indent-level (default 3)
Indentation of Oberon statements with respect to containing block.
oberon-case-indent (default 2)
Indentation for case statements.
oberon-auto-newline (default nil)
Non-nil means automatically newline after semicolons and the punctuation
mark after an end.
oberon-indent-nested-functions (default t)
Non-nil means nested functions are indented.
oberon-tab-always-indent (default t)
Non-nil means TAB in Oberon mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.
oberon-auto-lineup (default t)
List of contexts where auto lineup of :'s or ='s should be done.
See also the user variables oberon-type-keywords, oberon-start-keywords and
oberon-separator-keywords.
Turning on Oberon mode calls the value of the variable oberon-mode-hook with
no args, if that value is non-nil."
(interactive)
(kill-all-local-variables)
(use-local-map oberon-mode-map)
(setq major-mode 'oberon-mode)
(setq mode-name "Oberon")
(setq local-abbrev-table oberon-mode-abbrev-table)
(set-syntax-table oberon-mode-syntax-table)
(make-local-variable 'indent-line-function)
(setq indent-line-function 'oberon-indent-line)
(make-local-variable 'comment-indent-function)
(setq comment-indent-function 'oberon-indent-comment)
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments nil)
(make-local-variable 'blink-matching-paren-dont-ignore-comments)
(setq blink-matching-paren-dont-ignore-comments t)
(make-local-variable 'case-fold-search)
(setq case-fold-search t)
(make-local-variable 'comment-start)
(setq comment-start "{")
(make-local-variable 'comment-start-skip)
(setq comment-start-skip "(\\*+ *\\|{ *")
(make-local-variable 'comment-end)
(setq comment-end "}")
;; Font lock support
(make-local-variable 'font-lock-defaults)
(setq font-lock-defaults '(oberon-font-lock-keywords nil nil))
;; Imenu support
(make-local-variable 'imenu-generic-expression)
(setq imenu-generic-expression oberon-imenu-generic-expression)
(setq imenu-case-fold-search t)
(run-hooks 'oberon-mode-hook))
;;;
;;; Electric functions
;;;
(defun electric-oberon-terminate-line ()
"Terminate line and indent next line."
(interactive)
;; First, check if current line should be indented
(save-excursion
(beginning-of-line)
(skip-chars-forward " \t")
(if (looking-at oberon-autoindent-lines-re)
(oberon-indent-line)))
(delete-horizontal-space) ; Removes trailing whitespaces
(newline)
;; Indent next line
(oberon-indent-line)
;; Check if we shall indent inside comment
(let ((setstar nil))
(save-excursion
(forward-line -1)
(skip-chars-forward " \t")
(cond ((looking-at "\\*[ \t]+)")
;; Delete region between `*' and `)' if there is only whitespaces.
(forward-char 1)
(delete-horizontal-space))
((and (looking-at "(\\*\\|\\*[^)]")
(not (save-excursion
(search-forward "*)" (oberon-get-end-of-line) t))))
(setq setstar t))))
;; If last line was a star comment line then this one shall be too.
(if (null setstar)
(oberon-indent-line)
(insert "* "))))
(defun electric-oberon-semi-or-dot ()
"Insert `;' or `.' character and reindent the line."
(interactive)
(insert last-command-char)
(save-excursion
(beginning-of-line)
(oberon-indent-line))
(if oberon-auto-newline
(electric-oberon-terminate-line)))
(defun electric-oberon-colon ()
"Insert `:' and do all indentions except line indent on this line."
(interactive)
(insert last-command-char)
;; Do nothing if within string.
(if (oberon-within-string)
()
(save-excursion
(beginning-of-line)
(oberon-indent-line))
(let ((oberon-tab-always-indent nil))
(oberon-indent-command))))
(defun electric-oberon-equal ()
"Insert `=', and do indention if within type declaration."
(interactive)
(insert last-command-char)
(if (eq (car (oberon-calculate-indent)) 'declaration)
(let ((oberon-tab-always-indent nil))
(oberon-indent-command))))
(defun electric-oberon-hash ()
"Insert `#', and indent to column 0 if this is a CPP directive."
(interactive)
(insert last-command-char)
(if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
(save-excursion (beginning-of-line)
(delete-horizontal-space))))
(defun electric-oberon-tab ()
"Function called when TAB is pressed in Oberon mode."
(interactive)
;; Do nothing if within a string or in a CPP directive.
(if (or (oberon-within-string)
(and (not (bolp))
(save-excursion (beginning-of-line) (eq (following-char) ?#))))
(insert "\t")
;; If oberon-tab-always-indent, indent the beginning of the line.
(if oberon-tab-always-indent
(save-excursion
(beginning-of-line)
(oberon-indent-line))
(if (save-excursion
(skip-chars-backward " \t")
(bolp))
(oberon-indent-line)
(insert "\t")))
(oberon-indent-command)))
;;;
;;; Interactive functions
;;;
(defun oberon-insert-block ()
"Insert Oberon BEGIN ... END; block in the code with right indentation."
(interactive)
(insert "BEGIN")
(electric-oberon-terminate-line)
(save-excursion
(newline)
(insert "END;")
(beginning-of-line)
(oberon-indent-line)))
(defun oberon-star-comment ()
"Insert Oberon star comment at point."
(interactive)
(oberon-indent-line)
(insert "(*")
(electric-oberon-terminate-line)
(save-excursion
(electric-oberon-terminate-line)
(delete-horizontal-space)
(insert ")"))
(insert " "))
(defun oberon-mark-defun ()
"Mark the current oberon function (or procedure).
This puts the mark at the end, and point at the beginning."
(interactive)
(push-mark (point))
(oberon-end-of-defun)
(push-mark (point))
(oberon-beg-of-defun)
(if (fboundp 'zmacs-activate-region)
(zmacs-activate-region)))
(defun oberon-comment-area (start end)
"Put the region into a Oberon comment.
The comments that are in this area are \"deformed\":
`*)' becomes `!(*' and `}' becomes `!{'.
These deformed comments are returned to normal if you use
\\[oberon-uncomment-area] to undo the commenting.
The commented area starts with `oberon-exclude-str-start', and ends with
`oberon-include-str-end'. But if you change these variables,
\\[oberon-uncomment-area] won't recognize the comments."
(interactive "r")
(save-excursion
;; Insert start and endcomments
(goto-char end)
(if (and (save-excursion (skip-chars-forward " \t") (eolp))
(not (save-excursion (skip-chars-backward " \t") (bolp))))
(forward-line 1)
(beginning-of-line))
(insert oberon-exclude-str-end)
(setq end (point))
(newline)
(goto-char start)
(beginning-of-line)
(insert oberon-exclude-str-start)
(newline)
;; Replace end-comments within commented area
(goto-char end)
(save-excursion
(while (re-search-backward "\\*)" start t)
(replace-match "!(*" t t)))
(save-excursion
(while (re-search-backward "}" start t)
(replace-match "!{" t t)))))
(defun oberon-uncomment-area ()
"Uncomment a commented area; change deformed comments back to normal.
This command does nothing if the pointer is not in a commented
area. See also `oberon-comment-area'."
(interactive)
(save-excursion
(let ((start (point))
(end (point)))
;; Find the boundaries of the comment
(save-excursion
(setq start (progn (search-backward oberon-exclude-str-start nil t)
(point)))
(setq end (progn (search-forward oberon-exclude-str-end nil t)
(point))))
;; Check if we're really inside a comment
(if (or (equal start (point)) (<= end (point)))
(message "Not standing within commented area.")
(progn
;; Remove endcomment
(goto-char end)
(beginning-of-line)
(let ((pos (point)))
(end-of-line)
(delete-region pos (1+ (point))))
;; Change comments back to normal
(save-excursion
(while (re-search-backward "!{" start t)
(replace-match "}" t t)))
(save-excursion
(while (re-search-backward "!(\\*" start t)
(replace-match "*)" t t)))
;; Remove startcomment
(goto-char start)
(beginning-of-line)
(let ((pos (point)))
(end-of-line)
(delete-region pos (1+ (point)))))))))
(defun oberon-beg-of-defun ()
"Move backward to the beginning of the current function or procedure."
(interactive)
(catch 'found
(if (not (looking-at (concat "\\s \\|\\s)\\|" oberon-defun-re)))
(forward-sexp 1))
(let ((nest 0) (max -1) (func 0)
(reg (concat oberon-beg-block-re "\\|"
oberon-end-block-re "\\|"
oberon-defun-re)))
(while (re-search-backward reg nil 'move)
(cond ((let ((state (save-excursion
(parse-partial-sexp (point-min) (point)))))
(or (nth 3 state) (nth 4 state))) ; Inside string or comment
())
((match-end 1) ; begin|case|record|repeat
(if (and (looking-at "\\<RECORD\\>") (>= max 0))
(setq func (1- func)))
(setq nest (1+ nest)
max (max nest max)))
((match-end 2) ; end|until
(if (and (= nest max) (>= max 0))
(setq func (1+ func)))
(setq nest (1- nest)))
((match-end 3) ; function|procedure
(if (= 0 func)
(throw 'found t)
(setq func (1- func)))))))
nil))
(defun oberon-end-of-defun ()
"Move forward to the end of the current function or procedure."
(interactive)
(if (looking-at "\\s ")
(forward-sexp 1))
(if (not (looking-at oberon-defun-re))
(oberon-beg-of-defun))
(forward-char 1)
(let ((nest 0) (func 1)
(reg (concat oberon-beg-block-re "\\|"
oberon-end-block-re "\\|"
oberon-defun-re)))
(while (and (/= func 0)
(re-search-forward reg nil 'move))
(cond ((let ((state (save-excursion
(parse-partial-sexp (point-min) (point)))))
(or (nth 3 state) (nth 4 state))) ; Inside string or comment
())
((match-end 1)
(setq nest (1+ nest))
(if (save-excursion
(goto-char (match-beginning 0))
(looking-at "\\<RECORD\\>"))
(setq func (1+ func))))
((match-end 2)
(setq nest (1- nest))
(if (= nest 0)
(setq func (1- func))))
((match-end 3)
(setq func (1+ func))))))
(forward-line 1))
(defun oberon-end-of-statement ()
"Move forward to end of current statement."
(interactive)
(let ((parse-sexp-ignore-comments t)
(nest 0) pos
(regexp (concat "\\(" oberon-beg-block-re "\\)\\|\\("
oberon-end-block-re "\\)")))
(if (not (looking-at "[ \t\n]")) (forward-sexp -1))
(or (looking-at oberon-beg-block-re)
;; Skip to end of statement
(setq pos (catch 'found
(while t
(forward-sexp 1)
(cond ((looking-at "[ \t]*;")
(skip-chars-forward "^;")
(forward-char 1)
(throw 'found (point)))
((save-excursion
(forward-sexp -1)
(looking-at oberon-beg-block-re))
(goto-char (match-beginning 0))
(throw 'found nil))
((eobp)
(throw 'found (point))))))))
(if (not pos)
;; Skip a whole block
(catch 'found
(while t
(re-search-forward regexp nil 'move)
(setq nest (if (match-end 1)
(1+ nest)
(1- nest)))
(cond ((eobp)
(throw 'found (point)))
((= 0 nest)
(throw 'found (oberon-end-of-statement))))))
pos)))
(defun oberon-downcase-keywords ()
"Downcase all Oberon keywords in the buffer."
(interactive)
(oberon-change-keywords 'downcase-word))
(defun oberon-upcase-keywords ()
"Upcase all Oberon keywords in the buffer."
(interactive)
(oberon-change-keywords 'upcase-word))
(defun oberon-capitalize-keywords ()
"Capitalize all Oberon keywords in the buffer."
(interactive)
(oberon-change-keywords 'capitalize-word))
;; Change the keywords according to argument.
(defun oberon-change-keywords (change-word)
(save-excursion
(let ((keyword-re (concat "\\<\\("
(mapconcat 'identity oberon-keywords "\\|")
"\\)\\>")))
(goto-char (point-min))
(while (re-search-forward keyword-re nil t)
(funcall change-word -1)))))
;;;
;;; Other functions
;;;
(defun oberon-set-auto-comments ()
"Insert `{ case }' or `{ NAME }' on this line if appropriate.
Insert `{ case }' if there is an `end' on the line which
ends a case block. Insert `{ NAME }' if there is an `end'
on the line which ends a function or procedure named NAME."
(save-excursion
(forward-line -1)
(skip-chars-forward " \t")
(if (and (looking-at "\\<END;")
(not (save-excursion
(end-of-line)
(search-backward "{" (oberon-get-beg-of-line) t))))
(let ((type (car (oberon-calculate-indent))))
(if (eq type 'declaration)
()
(if (eq type 'case)
;; This is a case block
(progn
(end-of-line)
(delete-horizontal-space)
(insert " { CASE }"))
(let ((nest 1))
;; Check if this is the end of a function
(save-excursion
(while (not (or (looking-at oberon-defun-re) (bobp)))
(backward-sexp 1)
(cond ((looking-at oberon-beg-block-re)
(setq nest (1- nest)))
((looking-at oberon-end-block-re)
(setq nest (1+ nest)))))
(if (bobp)
(setq nest 1)))
(if (zerop nest)
(progn
(end-of-line)
(delete-horizontal-space)
(insert " { ")
(let (b e)
(save-excursion
(setq b (progn (oberon-beg-of-defun)
(skip-chars-forward "^ \t")
(skip-chars-forward " \t")
(point))
e (progn (skip-chars-forward "a-zA-Z0-9_")
(point))))
(insert-buffer-substring (current-buffer) b e))
(insert " }"))))))))))
;;;
;;; Indentation
;;;
(defconst oberon-indent-alist
'((block . (+ ind oberon-indent-level))
(case . (+ ind oberon-case-indent))
(caseblock . ind) (cpp . 0)
(declaration . (+ ind oberon-indent-level))
(paramlist . (oberon-indent-paramlist t))
(comment . (oberon-indent-comment))
(defun . ind) (contexp . ind)
(unknown . ind) (string . 0) (progbeg . 0)))
(defun oberon-indent-command ()
"Indent for special part of code."
(let* ((indent-str (oberon-calculate-indent))
(type (car indent-str))
(ind (car (cdr indent-str))))
(cond ((and (eq type 'paramlist)
(or (memq 'all oberon-auto-lineup)
(memq 'paramlist oberon-auto-lineup)))
(oberon-indent-paramlist)
(oberon-indent-paramlist))
((and (eq type 'declaration)
(or (memq 'all oberon-auto-lineup)
(memq 'declaration oberon-auto-lineup)))
(oberon-indent-declaration))
((and (eq type 'case) (not (looking-at "^[ \t]*$"))
(or (memq 'all oberon-auto-lineup)
(memq 'case oberon-auto-lineup)))
(oberon-indent-case)))
(if (looking-at "[ \t]+$")
(skip-chars-forward " \t"))))
(defun oberon-indent-line ()
"Indent current line as a Oberon statement."
(let* ((indent-str (oberon-calculate-indent))
(type (car indent-str))
(ind (car (cdr indent-str))))
;; Labels should not be indented.
(if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
(not (eq type 'declaration)))
(search-forward ":" nil t))
(delete-horizontal-space)
(cond (; Some things should not be indented
(or (and (eq type 'declaration) (looking-at oberon-declaration-re))
(eq type 'cpp))
())
(; Other things should have no extra indent
(looking-at oberon-noindent-re)
(indent-to ind))
(; Nested functions should be indented
(looking-at oberon-defun-re)
(if (and oberon-indent-nested-functions
(eq type 'defun))
(indent-to (+ ind oberon-indent-level))
(indent-to ind)))
(; But most lines are treated this way
(indent-to (eval (cdr (assoc type oberon-indent-alist))))
))))
(defun oberon-calculate-indent ()
"Calculate the indent of the current Oberon line.
Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
(save-excursion
(let* ((parse-sexp-ignore-comments t)
(oldpos (point))
(state (save-excursion (parse-partial-sexp (point-min) (point))))
(nest 0) (par 0) (complete (looking-at "[ \t]*END\\>"))
(elsed (looking-at "[ \t]*ELSE\\>")) (funccnt 0)
(did-func (looking-at "[ \t]*\\(PROCEDURE\\|FUNCTION\\)\\>"))
(type (catch 'nesting
;; Check if inside a string, comment or parenthesis
(cond ((nth 3 state) (throw 'nesting 'string))
((nth 4 state) (throw 'nesting 'comment))
((> (car state) 0)
(goto-char (scan-lists (point) -1 (car state)))
(setq par (1+ (current-column))))
((save-excursion (beginning-of-line)
(eq (following-char) ?#))
(throw 'nesting 'cpp)))
;; Loop until correct indent is found
(while t
(backward-sexp 1)
(cond (;--Escape from case statements
(and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
(not complete)
(save-excursion (skip-chars-backward " \t")
(bolp))
(= (save-excursion
(end-of-line) (backward-sexp) (point))
(point))
(> (save-excursion (goto-char oldpos)
(beginning-of-line)
(point))
(point)))
(throw 'nesting 'caseblock))
(;--Beginning of program
(looking-at oberon-progbeg-re)
(throw 'nesting 'progbeg))
(;--No known statements
(bobp)
(throw 'nesting 'progbeg))
(;--Nest block outwards
(looking-at oberon-beg-block-re)
(if (= nest 0)
(cond ((looking-at "CASE\\>")
(throw 'nesting 'case))
((looking-at "RECORD\\>")
(throw 'nesting 'declaration))
(t (throw 'nesting 'block)))
(if (and (looking-at "RECORD\\>") (= nest 1))
(setq funccnt (1- funccnt)))
(setq nest (1- nest))))
(;--Nest block inwards
(looking-at oberon-end-block-re)
(if (and (looking-at "END\\s ")
elsed (not complete))
(throw 'nesting 'block))
(if (= nest 0)
(setq funccnt (1+ funccnt)))
(setq complete t
nest (1+ nest)))
(;--Defun (or parameter list)
(and (looking-at oberon-defun-re)
(progn (setq funccnt (1- funccnt)
did-func t)
(or (bolp) (< funccnt 0))))
;; Prevent searching whole buffer
(if (and (bolp) (>= funccnt 0))
(throw 'nesting 'progbeg))
(if (= 0 par)
(throw 'nesting 'defun)
(setq par 0)
(let ((n 0))
(while (re-search-forward
"\\(\\<RECORD\\>\\)\\|\\<END\\>"
oldpos t)
(if (match-end 1)
(setq n (1+ n)) (setq n (1- n))))
(if (> n 0)
(throw 'nesting 'declaration)
(throw 'nesting 'paramlist)))))
(;--Declaration part
(and (looking-at oberon-declaration-re)
(not did-func)
(= funccnt 0))
(if (save-excursion
(goto-char oldpos)
(forward-line -1)
(looking-at "^[ \t]*$"))
(throw 'nesting 'unknown)
(throw 'nesting 'declaration)))
(;--If, else or while statement
(and (not complete)
(looking-at oberon-sub-block-re))
(throw 'nesting 'block))
(;--Found complete statement
(save-excursion (forward-sexp 1)
(= (following-char) ?\;))
(setq complete t))
)))))
;; Return type of block and indent level.
(if (> par 0) ; Unclosed Parenthesis
(list 'contexp par)
(list type (oberon-indent-level))))))
(defun oberon-indent-level ()
"Return the indent-level the current statement has.
Do not count labels, case-statements or records."
(save-excursion
(beginning-of-line)
(if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
(search-forward ":" nil t)
(if (looking-at ".*=[ \t]*RECORD\\>")
(search-forward "=" nil t)))
(skip-chars-forward " \t")
(current-column)))
(defun oberon-indent-comment ()
"Return indent for current comment."
(save-excursion
(re-search-backward "\\((\\*\\)\\|{" nil t)
(if (match-beginning 1)
(1+ (current-column))
(current-column))))
(defun oberon-indent-case ()
"Indent within case statements."
(let ((savepos (point-marker))
(end (prog2
(end-of-line)
(point-marker)
(re-search-backward "\\<CASE\\>" nil t)))
(beg (point)) oldpos
(ind 0))
;; Get right indent
(while (< (point) end)
(if (re-search-forward
"^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
(marker-position end) 'move)
(forward-char -1))
(if (< (point) end)
(progn
(delete-horizontal-space)
(if (> (current-column) ind)
(setq ind (current-column)))
(oberon-end-of-statement))))
(goto-char beg)
(setq oldpos (marker-position end))
;; Indent all case statements
(while (< (point) end)
(if (re-search-forward
"^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
(marker-position end) 'move)
(forward-char -1))
(indent-to (1+ ind))
(if (/= (following-char) ?:)
()
(forward-char 1)
(delete-horizontal-space)
(insert " "))
(setq oldpos (point))
(oberon-end-of-statement))
(goto-char savepos)))
(defun oberon-indent-paramlist (&optional arg)
"Indent current line in parameterlist.
If optional arg is non-nil, just return the
indent of the current line in parameterlist."
(save-excursion
(let* ((oldpos (point))
(stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
(stcol (1+ (current-column)))
(edpos (progn (oberon-declaration-end)
(search-backward ")" (oberon-get-beg-of-line) t)
(point)))
(usevar (re-search-backward "\\<var\\>" stpos t)))
(if arg (progn
;; If arg, just return indent
(goto-char oldpos)
(beginning-of-line)
(if (or (not usevar) (looking-at "[ \t]*VAR\\>"))
stcol (+ 4 stcol)))
(goto-char stpos)
(forward-char 1)
(delete-horizontal-space)
(if (and usevar (not (looking-at "VAR\\>")))
(indent-to (+ 4 stcol)))
(oberon-indent-declaration nil stpos edpos)))))
(defun oberon-indent-declaration (&optional arg start end)
"Indent current lines as declaration, lining up the `:'s or `='s."
(let ((pos (point-marker)))
(if (and (not (or arg start)) (not (oberon-declaration-beg)))
()
(let ((lineup (if (or (looking-at "\\<VAR\\>\\|\\<RECORD\\>") arg start)
":" "="))
(stpos (if start start
(forward-word 2) (backward-word 1) (point)))
(edpos (set-marker (make-marker)
(if end end
(max (progn (oberon-declaration-end)
(point))
pos))))
ind)
(goto-char stpos)
;; Indent lines in record block
(if arg
(while (<= (point) edpos)
(beginning-of-line)
(delete-horizontal-space)
(if (looking-at "END\\>")
(indent-to arg)
(indent-to (+ arg oberon-indent-level)))
(forward-line 1)))
;; Do lineup
(setq ind (oberon-get-lineup-indent stpos edpos lineup))
(goto-char stpos)
(while (and (<= (point) edpos) (not (eobp)))
(if (search-forward lineup (oberon-get-end-of-line) 'move)
(forward-char -1))
(delete-horizontal-space)
(indent-to ind)
(if (not (looking-at lineup))
(forward-line 1) ; No more indent if there is no : or =
(forward-char 1)
(delete-horizontal-space)
(insert " ")
;; Indent record block
(if (looking-at "RECORD\\>")
(oberon-indent-declaration (current-column)))
(forward-line 1)))))
;; If arg - move point
(if arg (forward-line -1)
(goto-char pos))))
; "Return the indent level that will line up several lines within the region
;from b to e nicely. The lineup string is str."
(defun oberon-get-lineup-indent (b e str)
(save-excursion
(let ((ind 0)
(reg (concat str "\\|\\(\\<RECORD\\>\\)\\|" oberon-defun-re)))
(goto-char b)
;; Get rightmost position
(while (< (point) e)
(and (re-search-forward reg (min e (oberon-get-end-of-line 2)) 'move)
(cond ((match-beginning 1)
;; Skip record blocks
(oberon-declaration-end))
((match-beginning 2)
;; We have entered a new procedure. Exit.
(goto-char e))
(t
(goto-char (match-beginning 0))
(skip-chars-backward " \t")
(if (> (current-column) ind)
(setq ind (current-column)))
(goto-char (match-end 0))
(end-of-line)
))))
;; In case no lineup was found
(if (> ind 0)
(1+ ind)
;; No lineup-string found
(goto-char b)
(end-of-line)
(skip-chars-backward " \t")
(1+ (current-column))))))
;;;
;;; Completion
;;;
(defvar oberon-str nil)
(defvar oberon-all nil)
(defvar oberon-pred nil)
(defvar oberon-buffer-to-use nil)
(defvar oberon-flag nil)
(defun oberon-string-diff (str1 str2)
"Return index of first letter where STR1 and STR2 differs."
(catch 'done
(let ((diff 0))
(while t
(if (or (> (1+ diff) (length str1))
(> (1+ diff) (length str2)))
(throw 'done diff))
(or (equal (aref str1 diff) (aref str2 diff))
(throw 'done diff))
(setq diff (1+ diff))))))
;; Calculate all possible completions for functions if argument is `function',
;; completions for procedures if argument is `procedure' or both functions and
;; procedures otherwise.
(defun oberon-func-completion (type)
;; Build regular expression for function/procedure names
(if (string= oberon-str "")
(setq oberon-str "[a-zA-Z_]"))
(let ((oberon-str (concat (cond
((eq type 'procedure) "\\<\\(PROCEDURE\\)\\s +")
((eq type 'function) "\\<\\(FUNCTION\\)\\s +")
(t "\\<\\(FUNCTION\\|PROCEDURE\\)\\s +"))
"\\<\\(" oberon-str "[a-zA-Z0-9_.]*\\)\\>"))
match)
(if (not (looking-at "\\<\\(FUNCTION\\|PROCEDURE\\)\\>"))
(re-search-backward "\\<\\(FUNCTION\\|PROCEDURE\\)\\>" nil t))
(forward-char 1)
;; Search through all reachable functions
(while (oberon-beg-of-defun)
(if (re-search-forward oberon-str (oberon-get-end-of-line) t)
(progn (setq match (buffer-substring (match-beginning 2)
(match-end 2)))
(if (or (null oberon-pred)
(funcall oberon-pred match))
(setq oberon-all (cons match oberon-all)))))
(goto-char (match-beginning 0)))))
(defun oberon-get-completion-decl ()
;; Macro for searching through current declaration (var, type or const)
;; for matches of `str' and adding the occurrence to `all'
(let ((end (save-excursion (oberon-declaration-end)
(point)))
match)
;; Traverse lines
(while (< (point) end)
(if (re-search-forward "[:=]" (oberon-get-end-of-line) t)
;; Traverse current line
(while (and (re-search-backward
(concat "\\((\\|\\<\\(VAR\\|TYPE\\|CONST\\)\\>\\)\\|"
oberon-symbol-re)
(oberon-get-beg-of-line) t)
(not (match-end 1)))
(setq match (buffer-substring (match-beginning 0) (match-end 0)))
(if (string-match (concat "\\<" oberon-str) match)
(if (or (null oberon-pred)
(funcall oberon-pred match))
(setq oberon-all (cons match oberon-all))))))
(if (re-search-forward "\\<RECORD\\>" (oberon-get-end-of-line) t)
(oberon-declaration-end)
(forward-line 1)))))
(defun oberon-type-completion ()
"Calculate all possible completions for types."
(let ((start (point))
goon)
;; Search for all reachable type declarations
(while (or (oberon-beg-of-defun)
(setq goon (not goon)))
(save-excursion
(if (and (< start (prog1 (save-excursion (oberon-end-of-defun)
(point))
(forward-char 1)))
(re-search-forward
"\\<TYPE\\>\\|\\<\\(BEGIN\\|FUNCTION\\|PROCEDURE\\)\\>"
start t)
(not (match-end 1)))
;; Check current type declaration
(oberon-get-completion-decl))))))
(defun oberon-var-completion ()
"Calculate all possible completions for variables (or constants)."
(let ((start (point))
goon twice)
;; Search for all reachable var declarations
(while (or (oberon-beg-of-defun)
(setq goon (not goon)))
(save-excursion
(if (> start (prog1 (save-excursion (oberon-end-of-defun)
(point))))
() ; Declarations not reachable
(if (search-forward "(" (oberon-get-end-of-line) t)
;; Check parameterlist
(oberon-get-completion-decl))
(setq twice 2)
(while (>= (setq twice (1- twice)) 0)
(cond ((and (re-search-forward
(concat "\\<\\(VAR\\|CONST\\)\\>\\|"
"\\<\\(BEGIN\\|FUNCTION\\|PROCEDURE\\)\\>")
start t)
(not (match-end 2)))
;; Check var/const declarations
(oberon-get-completion-decl))
((match-end 2)
(setq twice 0)))))))))
(defun oberon-keyword-completion (keyword-list)
"Give list of all possible completions of keywords in KEYWORD-LIST."
(mapcar '(lambda (s)
(if (string-match (concat "\\<" oberon-str) s)
(if (or (null oberon-pred)
(funcall oberon-pred s))
(setq oberon-all (cons s oberon-all)))))
keyword-list))
;; Function passed to completing-read, try-completion or
;; all-completions to get completion on STR. If predicate is non-nil,
;; it must be a function to be called for every match to check if this
;; should really be a match. If flag is t, the function returns a list
;; of all possible completions. If it is nil it returns a string, the
;; longest possible completion, or t if STR is an exact match. If flag
;; is 'lambda, the function returns t if STR is an exact match, nil
;; otherwise.
(defun oberon-completion (oberon-str oberon-pred oberon-flag)
(save-excursion
(let ((oberon-all nil))
;; Set buffer to use for searching labels. This should be set
;; within functions which use oberon-completions
(set-buffer oberon-buffer-to-use)
;; Determine what should be completed
(let ((state (car (oberon-calculate-indent))))
(cond (;--Within a declaration or parameterlist
(or (eq state 'declaration) (eq state 'paramlist)
(and (eq state 'defun)
(save-excursion
(re-search-backward ")[ \t]*:"
(oberon-get-beg-of-line) t))))
(if (or (eq state 'paramlist) (eq state 'defun))
(oberon-beg-of-defun))
(oberon-type-completion)
(oberon-keyword-completion oberon-type-keywords))
(;--Starting a new statement
(and (not (eq state 'contexp))
(save-excursion
(skip-chars-backward "a-zA-Z0-9_.")
(backward-sexp 1)
(or (looking-at oberon-nosemi-re)
(progn
(forward-sexp 1)
(looking-at "\\s *\\(;\\|:[^=]\\)")))))
(save-excursion (oberon-var-completion))
(oberon-func-completion 'procedure)
(oberon-keyword-completion oberon-start-keywords))
(t;--Anywhere else
(save-excursion (oberon-var-completion))
(oberon-func-completion 'function)
(oberon-keyword-completion oberon-separator-keywords))))
;; Now we have built a list of all matches. Give response to caller
(oberon-completion-response))))
(defun oberon-completion-response ()
(cond ((or (equal oberon-flag 'lambda) (null oberon-flag))
;; This was not called by all-completions
(if (null oberon-all)
;; Return nil if there was no matching label
nil
;; Get longest string common in the labels
(let* ((elm (cdr oberon-all))
(match (car oberon-all))
(min (length match))
tmp)
(if (string= match oberon-str)
;; Return t if first match was an exact match
(setq match t)
(while (not (null elm))
;; Find longest common string
(if (< (setq tmp (oberon-string-diff match (car elm))) min)
(progn
(setq min tmp)
(setq match (substring match 0 min))))
;; Terminate with match=t if this is an exact match
(if (string= (car elm) oberon-str)
(progn
(setq match t)
(setq elm nil))
(setq elm (cdr elm)))))
;; If this is a test just for exact match, return nil ot t
(if (and (equal oberon-flag 'lambda) (not (equal match 't)))
nil
match))))
;; If flag is t, this was called by all-completions. Return
;; list of all possible completions
(oberon-flag
oberon-all)))
(defvar oberon-last-word-numb 0)
(defvar oberon-last-word-shown nil)
(defvar oberon-last-completions nil)
(defun oberon-complete-word ()
"Complete word at current point.
\(See also `oberon-toggle-completions', `oberon-type-keywords',
`oberon-start-keywords' and `oberon-separator-keywords'.)"
(interactive)
(let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
(e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
(oberon-str (buffer-substring b e))
;; The following variable is used in oberon-completion
(oberon-buffer-to-use (current-buffer))
(allcomp (if (and oberon-toggle-completions
(string= oberon-last-word-shown oberon-str))
oberon-last-completions
(all-completions oberon-str 'oberon-completion)))
(match (if oberon-toggle-completions
"" (try-completion
oberon-str (mapcar '(lambda (elm)
(cons elm 0)) allcomp)))))
;; Delete old string
(delete-region b e)
;; Toggle-completions inserts whole labels
(if oberon-toggle-completions
(progn
;; Update entry number in list
(setq oberon-last-completions allcomp
oberon-last-word-numb
(if (>= oberon-last-word-numb (1- (length allcomp)))
0
(1+ oberon-last-word-numb)))
(setq oberon-last-word-shown (elt allcomp oberon-last-word-numb))
;; Display next match or same string if no match was found
(if (not (null allcomp))
(insert "" oberon-last-word-shown)
(insert "" oberon-str)
(message "(No match)")))
;; The other form of completion does not necessarily do that.
;; Insert match if found, or the original string if no match
(if (or (null match) (equal match 't))
(progn (insert "" oberon-str)
(message "(No match)"))
(insert "" match))
;; Give message about current status of completion
(cond ((equal match 't)
(if (not (null (cdr allcomp)))
(message "(Complete but not unique)")
(message "(Sole completion)")))
;; Display buffer if the current completion didn't help
;; on completing the label.
((and (not (null (cdr allcomp))) (= (length oberon-str)
(length match)))
(with-output-to-temp-buffer "*Completions*"
(display-completion-list allcomp))
;; Wait for a keypress. Then delete *Completion* window
(momentary-string-display "" (point))
(delete-window (get-buffer-window (get-buffer "*Completions*")))
)))))
(defun oberon-show-completions ()
"Show all possible completions at current point."
(interactive)
(let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
(e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
(oberon-str (buffer-substring b e))
;; The following variable is used in oberon-completion
(oberon-buffer-to-use (current-buffer))
(allcomp (if (and oberon-toggle-completions
(string= oberon-last-word-shown oberon-str))
oberon-last-completions
(all-completions oberon-str 'oberon-completion))))
;; Show possible completions in a temporary buffer.
(with-output-to-temp-buffer "*Completions*"
(display-completion-list allcomp))
;; Wait for a keypress. Then delete *Completion* window
(momentary-string-display "" (point))
(delete-window (get-buffer-window (get-buffer "*Completions*")))))
(defun oberon-get-default-symbol ()
"Return symbol around current point as a string."
(save-excursion
(buffer-substring (progn
(skip-chars-backward " \t")
(skip-chars-backward "a-zA-Z0-9_")
(point))
(progn
(skip-chars-forward "a-zA-Z0-9_")
(point)))))
(defun oberon-build-defun-re (str &optional arg)
"Return function/procedure starting with STR as regular expression.
With optional second arg non-nil, STR is the complete name of the instruction."
(if arg
(concat "^\\(function\\|procedure\\)[ \t]+\\(" str "\\)\\>")
(concat "^\\(function\\|procedure\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
;; Function passed to completing-read, try-completion or
;; all-completions to get completion on any function name. If
;; predicate is non-nil, it must be a function to be called for every
;; match to check if this should really be a match. If flag is t, the
;; function returns a list of all possible completions. If it is nil
;; it returns a string, the longest possible completion, or t if STR
;; is an exact match. If flag is 'lambda, the function returns t if
;; STR is an exact match, nil otherwise.
(defun oberon-comp-defun (oberon-str oberon-pred oberon-flag)
(save-excursion
(let ((oberon-all nil)
match)
;; Set buffer to use for searching labels. This should be set
;; within functions which use oberon-completions
(set-buffer oberon-buffer-to-use)
(let ((oberon-str oberon-str))
;; Build regular expression for functions
(if (string= oberon-str "")
(setq oberon-str (oberon-build-defun-re "[a-zA-Z_]"))
(setq oberon-str (oberon-build-defun-re oberon-str)))
(goto-char (point-min))
;; Build a list of all possible completions
(while (re-search-forward oberon-str nil t)
(setq match (buffer-substring (match-beginning 2) (match-end 2)))
(if (or (null oberon-pred)
(funcall oberon-pred match))
(setq oberon-all (cons match oberon-all)))))
;; Now we have built a list of all matches. Give response to caller
(oberon-completion-response))))
(defun oberon-goto-defun ()
"Move to specified Oberon function/procedure.
The default is a name found in the buffer around point."
(interactive)
(let* ((default (oberon-get-default-symbol))
;; The following variable is used in oberon-comp-function
(oberon-buffer-to-use (current-buffer))
(default (if (oberon-comp-defun default nil 'lambda)
default ""))
(label (if (not (string= default ""))
;; Do completion with default
(completing-read (concat "Label: (default " default ") ")
'oberon-comp-defun nil t "")
;; There is no default value. Complete without it
(completing-read "Label: "
'oberon-comp-defun nil t ""))))
;; If there was no response on prompt, use default value
(if (string= label "")
(setq label default))
;; Goto right place in buffer if label is not an empty string
(or (string= label "")
(progn
(goto-char (point-min))
(re-search-forward (oberon-build-defun-re label t))
(beginning-of-line)))))
;;;
;;; Oberon-outline-mode
;;;
(defvar oberon-outline-map nil "Keymap used in Oberon Outline mode.")
(if oberon-outline-map
nil
(if (boundp 'set-keymap-name)
(set-keymap-name oberon-outline-map 'oberon-outline-map))
(if (not (boundp 'set-keymap-parent))
(setq oberon-outline-map (copy-keymap oberon-mode-map))
(setq oberon-outline-map (make-sparse-keymap))
(set-keymap-parent oberon-outline-map oberon-mode-map))
(define-key oberon-outline-map "\M-\C-a" 'oberon-outline-prev-defun)
(define-key oberon-outline-map "\M-\C-e" 'oberon-outline-next-defun)
(define-key oberon-outline-map "\C-c\C-d" 'oberon-outline-goto-defun)
(define-key oberon-outline-map "\C-c\C-s" 'oberon-show-all)
(define-key oberon-outline-map "\C-c\C-h" 'oberon-hide-other-defuns))
(defvar oberon-outline-mode nil "Non-nil while using Oberon Outline mode.")
(make-variable-buffer-local 'oberon-outline-mode)
(set-default 'oberon-outline-mode nil)
(if (not (assoc 'oberon-outline-mode minor-mode-alist))
(setq minor-mode-alist (append minor-mode-alist
(list '(oberon-outline-mode " Outl")))))
(defun oberon-outline (&optional arg)
"Outline-line minor mode for Oberon mode.
When in Oberon Outline mode, portions
of the text being edited may be made invisible. \\<oberon-outline-map>
Oberon Outline mode provides some additional commands.
\\[oberon-outline-prev-defun]\
\t- Move to previous function/procedure, hiding everything else.
\\[oberon-outline-next-defun]\
\t- Move to next function/procedure, hiding everything else.
\\[oberon-outline-goto-defun]\
\t- Goto function/procedure prompted for in minibuffer,
\t hide all other functions.
\\[oberon-show-all]\t- Show the whole buffer.
\\[oberon-hide-other-defuns]\
\t- Hide everything but the current function (function under the cursor).
\\[oberon-outline]\t- Leave oberon-outline-mode."
(interactive "P")
(setq oberon-outline-mode
(if (null arg) (not oberon-outline-mode) t))
(if (boundp 'redraw-mode-line)
(redraw-mode-line))
(if oberon-outline-mode
(progn
(setq selective-display t)
(use-local-map oberon-outline-map))
(progn
(setq selective-display nil)
(oberon-show-all)
(use-local-map oberon-mode-map))))
(defun oberon-outline-change (b e oberon-flag)
(let ((modp (buffer-modified-p)))
(unwind-protect
(subst-char-in-region b e (if (= oberon-flag ?\n)
?\^M ?\n) oberon-flag)
(set-buffer-modified-p modp))))
(defun oberon-show-all ()
"Show all of the text in the buffer."
(interactive)
(oberon-outline-change (point-min) (point-max) ?\n))
(defun oberon-hide-other-defuns ()
"Show only the current defun."
(interactive)
(save-excursion
(let ((beg (progn (if (not (looking-at "\\(FUNCTION\\|PROCEDURE\\)\\>"))
(oberon-beg-of-defun))
(point)))
(end (progn (oberon-end-of-defun)
(backward-sexp 1)
(search-forward "\n\\|\^M" nil t)
(point)))
(opoint (point-min)))
(goto-char (point-min))
;; Hide all functions before current function
(while (re-search-forward "^\\(FUNCTION\\|PROCEDURE\\)\\>" beg 'move)
(oberon-outline-change opoint (1- (match-beginning 0)) ?\^M)
(setq opoint (point))
;; Functions may be nested
(if (> (progn (oberon-end-of-defun) (point)) beg)
(goto-char opoint)))
(if (> beg opoint)
(oberon-outline-change opoint (1- beg) ?\^M))
;; Show current function
(oberon-outline-change beg end ?\n)
;; Hide nested functions
(forward-char 1)
(while (re-search-forward "^\\(FUNCTION\\|PROCEDURE\\)\\>" end 'move)
(setq opoint (point))
(oberon-end-of-defun)
(oberon-outline-change opoint (point) ?\^M))
(goto-char end)
(setq opoint end)
;; Hide all function after current function
(while (re-search-forward "^\\(FUNCTION\\|PROCEDURE\\)\\>" nil 'move)
(oberon-outline-change opoint (1- (match-beginning 0)) ?\^M)
(setq opoint (point))
(oberon-end-of-defun))
(oberon-outline-change opoint (point-max) ?\^M)
;; Hide main program
(if (< (progn (forward-line -1) (point)) end)
(progn
(goto-char beg)
(oberon-end-of-defun)
(backward-sexp 1)
(oberon-outline-change (point) (point-max) ?\^M))))))
(defun oberon-outline-next-defun ()
"Move to next function/procedure, hiding all others."
(interactive)
(oberon-end-of-defun)
(oberon-hide-other-defuns))
(defun oberon-outline-prev-defun ()
"Move to previous function/procedure, hiding all others."
(interactive)
(oberon-beg-of-defun)
(oberon-hide-other-defuns))
(defun oberon-outline-goto-defun ()
"Move to specified function/procedure, hiding all others."
(interactive)
(oberon-goto-defun)
(oberon-hide-other-defuns))
(provide 'oberon)
;;; oberon.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment