File:  [gforth] / gforth / gforth.el
Revision 1.72: download - view: text, annotated - select for diffs
Thu Feb 22 19:37:06 2007 UTC (17 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
added support for new try words in gforth.el
added compatibility files endtry-iferror.fs recover-endtry.fs

    1: ;;; gforth.el --- major mode for editing (G)Forth sources
    2: 
    3: ;; Copyright (C) 1995,1996,1997,1998,2000,2001,2003,2004 Free Software Foundation, Inc.
    4: 
    5: ;; This file is part of Gforth.
    6: 
    7: ;; GForth is distributed in the hope that it will be useful,
    8: ;; but WITHOUT ANY WARRANTY.  No author or distributor
    9: ;; accepts responsibility to anyone for the consequences of using it
   10: ;; or for whether it serves any particular purpose or works at all,
   11: ;; unless he says so in writing.  Refer to the GNU Emacs General Public
   12: ;; License for full details.
   13: 
   14: ;; Everyone is granted permission to copy, modify and redistribute
   15: ;; GNU Emacs, but only under the conditions described in the
   16: ;; GNU Emacs General Public License.   A copy of this license is
   17: ;; supposed to have been given to you along with Gforth so you
   18: ;; can know your rights and responsibilities.  It should be in a
   19: ;; file named COPYING.  Among other things, the copyright notice
   20: ;; and this notice must be preserved on all copies.
   21: 
   22: ;; Author: Goran Rydqvist <gorry@ida.liu.se>
   23: ;; Maintainer: David Kühling <dvdkhlng@gmx.de>
   24: ;; Created: 16 July 88 by Goran Rydqvist
   25: ;; Keywords: forth, gforth
   26: 
   27: ;; Changes by anton
   28: ;; This is a variant of forth.el that came with TILE.
   29: ;; I left most of this stuff untouched and made just a few changes for 
   30: ;; the things I use (mainly indentation and syntax tables).
   31: ;; So there is still a lot of work to do to adapt this to gforth.
   32: 
   33: ;; Changes by David
   34: ;; Added a syntax-hilighting engine, rewrote auto-indentation engine.
   35: ;; Added support for block files.
   36: ;; Replaced forth-process code with comint-based implementation.
   37: 
   38: ;; Tested with Emacs 19.34, 20.5, 21 and XEmacs 21
   39:  
   40: ;;-------------------------------------------------------------------
   41: ;; A Forth indentation, documentation search and interaction library
   42: ;;-------------------------------------------------------------------
   43: ;;
   44: ;; Written by Goran Rydqvist, gorry@ida.liu.se, Summer 1988
   45: ;; Started:	16 July 88
   46: ;; Version:	2.10
   47: ;; Last update:	5 December 1989 by Mikael Patel, mip@ida.liu.se
   48: ;; Last update:	25 June 1990 by Goran Rydqvist, gorry@ida.liu.se
   49: ;;
   50: ;; Documentation: See forth-mode (^HF forth-mode)
   51: ;;-------------------------------------------------------------------
   52: 
   53: ;;; Code:
   54: 
   55: ;(setq debug-on-error t)
   56: 
   57: ;; Code ripped from `version.el' for compatability with Emacs versions
   58: ;; prior to 19.23.
   59: (if (not (boundp 'emacs-major-version))
   60:     (defconst emacs-major-version
   61:       (progn (string-match "^[0-9]+" emacs-version)
   62: 	     (string-to-int (match-string 0 emacs-version)))))
   63: 
   64: (defun forth-emacs-older (major minor)
   65:   (or (< emacs-major-version major)
   66:       (and (= emacs-major-version major) (< emacs-minor-version minor))))
   67: 
   68: ;; Code ripped from `subr.el' for compatability with Emacs versions
   69: ;; prior to 20.1
   70: (eval-when-compile 
   71:   (if (forth-emacs-older 20 1)
   72:       (progn
   73: 	(defmacro when (cond &rest body)
   74: 	  "If COND yields non-nil, do BODY, else return nil."
   75: 	  (list 'if cond (cons 'progn body)))
   76: 	(defmacro unless (cond &rest body)
   77: 	  "If COND yields nil, do BODY, else return nil."
   78: 	  (cons 'if (cons cond (cons nil body)))))))
   79: 
   80: ;; `no-error' argument of require not supported in Emacs versions
   81: ;; prior to 20.4 :-(
   82: (defun forth-require (feature)
   83:   (condition-case err (require feature) (error nil)))
   84: 
   85: (require 'font-lock)
   86: 
   87: ;; define `font-lock-warning-face' in emacs-versions prior to 20.1
   88: ;; (ripped from `font-lock.el')
   89: (unless (boundp 'font-lock-warning-face)
   90:   (message "defining font-lock-warning-face")
   91:   (make-face 'font-lock-warning-face)
   92:   (defvar font-lock-warning-face 'font-lock-warning-face)
   93:   (set-face-foreground font-lock-warning-face "red")
   94:   (make-face-bold font-lock-warning-face))
   95: 
   96: ;; define `font-lock-constant-face' in XEmacs (just copy
   97: ;; `font-lock-preprocessor-face')
   98: (unless (boundp 'font-lock-constant-face)
   99:   (copy-face font-lock-preprocessor-face 'font-lock-constant-face))
  100: 
  101: 
  102: ;; define `regexp-opt' in emacs versions prior to 20.1 
  103: ;; (this implementation is extremely inefficient, though)
  104: (eval-and-compile (forth-require 'regexp-opt))
  105: (unless (memq 'regexp-opt features)
  106:   (message (concat 
  107: 	    "Warning: your Emacs version doesn't support `regexp-opt'. "
  108:             "Hilighting will be slow."))
  109:   (defun regexp-opt (STRINGS &optional PAREN)
  110:     (let ((open (if PAREN "\\(" "")) (close (if PAREN "\\)" "")))
  111:       (concat open (mapconcat 'regexp-quote STRINGS "\\|") close)))
  112:   (defun regexp-opt-depth (re)
  113:     (if (string= (substring re 0 2) "\\(") 1 0)))
  114: 
  115: ; todo:
  116: ;
  117: 
  118: ; screen-height existiert nicht in XEmacs, frame-height ersetzen?
  119: ; 
  120: 
  121: ; Wörter ordentlich hilighten, die nicht auf Whitespace beginnen ( ..)IF
  122: ; -- mit aktueller Konzeption nicht möglich??
  123: ;
  124: ; Konfiguration über customization groups
  125: ;
  126: ; Bereich nur auf Wortanfang/ende ausweiten, wenn Anfang bzw Ende in einem 
  127: ; Wort liegen (?) -- speed!
  128: ;
  129: ; 'forth-word' property muss eindeutig sein!
  130: ;
  131: ; Forth-Menu 
  132: ;
  133: ; Interface zu GForth Prozessen (Patches von Michael Scholz)
  134: ;
  135: ; Byte-compile-Code rausschmeißen, Compilieren im Makefile über Emacs
  136: ; batch-Modus
  137: ;
  138: ; forth-help Kram rausschmeißen
  139: ;
  140: ; XEmacs Kompatibilität? imenu/speedbar -> fume?
  141: ; 
  142: ; Folding neuschreiben (neue Parser-Informationen benutzen)
  143: 
  144: ;;; Motion-hooking (dk)
  145: ;;;
  146: (defun forth-idle-function ()
  147:   "Function that is called when Emacs is idle to detect cursor motion
  148: in forth-block-mode buffers (which is mainly used for screen number
  149: display in).  Currently ignores forth-mode buffers but that may change
  150: in the future."
  151:   (if (eq major-mode 'forth-block-mode)
  152:       (forth-check-motion)))
  153: 
  154: (defvar forth-idle-function-timer nil 
  155:   "Timer that runs `forth-idle-function' or nil if no timer installed.")
  156: 
  157: (defun forth-install-motion-hook ()
  158:   "Install the motion-hooking mechanism.  Currently uses idle timers
  159: but might be transparently changed in the future."
  160:   (unless forth-idle-function-timer
  161:     ;; install idle function only once (first time forth-mode is used)
  162:     (setq forth-idle-function-timer 
  163: 	  (run-with-idle-timer .05 t 'forth-idle-function))))
  164: 
  165: (defvar forth-was-point nil)
  166: 
  167: (defun forth-check-motion ()
  168:   "Run `forth-motion-hooks', if `point' changed since last call.  This
  169: used to be called via `post-command-hook' but uses idle timers now as
  170: users complaint about lagging performance."
  171:   (when (or (eq forth-was-point nil) (/= forth-was-point (point)))
  172:     (setq forth-was-point (point))
  173:     (run-hooks 'forth-motion-hooks)))
  174: 
  175: 
  176: ;;; Hilighting and indentation engine (dk)
  177: ;;;
  178: (defvar forth-disable-parser nil
  179:   "*Non-nil means to disable on-the-fly parsing of Forth-code.
  180: 
  181: This will disable hilighting of forth-mode buffers and will decrease
  182: the smartness of the indentation engine. Only set it to non-nil, if
  183: your computer is very slow. To disable hilighting, set
  184: `forth-hilight-level' to zero.")
  185: 
  186: (defvar forth-jit-parser nil
  187:   "*Non-nil means to parse Forth-code just-in-time.
  188: 
  189: This eliminates the need for initially parsing forth-mode buffers and
  190: thus speeds up loading of Forth files. That feature is only available
  191: in Emacs21 (and newer versions).")
  192: 
  193: (defvar forth-words nil 
  194:   "List of words for hilighting and recognition of parsed text areas. 
  195: 
  196: Hilighting of object-oriented Forth code is achieved, by appending either
  197: `forth-objects-words' or `forth-oof-words' to the list, depending on the values of `forth-use-objects' or `forth-use-oof'.
  198: 
  199: After `forth-words' changed, `forth-compile-words' must be called to
  200: make the changes take effect.
  201: 
  202: Each item of `forth-words' has the form 
  203:    (MATCHER TYPE HILIGHT . &optional PARSED-TEXT ...)
  204: 
  205: MATCHER is either a list of strings to match, or a REGEXP.
  206:    If it's a REGEXP, it should not be surrounded by '\\<' or '\\>', since 
  207:    that'll be done automatically by the search routines.
  208: 
  209: TYPE should be one of 'definiton-starter', 'definition-ender', 'compile-only',
  210:    'immediate' or 'non-immediate'. Those information are required to determine
  211:    whether a word actually parses (and whether that parsed text needs to be
  212:    hilighted).
  213: 
  214: HILIGHT is a cons cell of the form (FACE . MINIMUM-LEVEL)
  215:    Where MINIMUM-LEVEL specifies the minimum value of `forth-hilight-level',
  216:    that's required for matching text to be hilighted.
  217: 
  218: PARSED-TEXT specifies whether and how a word parses following text. You can
  219:    specify as many subsequent PARSED-TEXT as you wish, but that shouldn't be
  220:    necessary very often. It has the following form:
  221:    (DELIM-REGEXP SKIP-LEADING-FLAG PARSED-TYPE HILIGHT)
  222: 
  223: DELIM-REGEXP is a regular expression that should match strings of length 1,
  224:    which are delimiters for the parsed text.
  225: 
  226: A non-nil value for PARSE-LEADING-FLAG means, that leading delimiter strings
  227:    before parsed text should be skipped. This is the parsing behaviour of the
  228:    Forth word WORD. Set it to t for name-parsing words, nil for comments and
  229:    strings.
  230: 
  231: PARSED-TYPE specifies what kind of text is parsed. It should be on of 'name',
  232:    'string' or 'comment'.")
  233: (setq forth-words
  234:       '(
  235: 	(("[") definition-ender (font-lock-keyword-face . 1))
  236: 	(("]" "]l") definition-starter (font-lock-keyword-face . 1))
  237: 	((":") definition-starter (font-lock-keyword-face . 1)
  238: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  239: 	(("immediate" "compile-only" "restrict")
  240: 	 immediate (font-lock-keyword-face . 1))
  241: 	(("does>") compile-only (font-lock-keyword-face . 1))
  242: 	((":noname") definition-starter (font-lock-keyword-face . 1))
  243: 	((";" ";code") definition-ender (font-lock-keyword-face . 1))
  244: 	(("include" "require" "needs" "use") 
  245: 	 non-immediate (font-lock-keyword-face . 1) 
  246: 	 "[\n\t ]" t string (font-lock-string-face . 1))
  247: 	(("included" "required" "thru" "load")
  248: 	 non-immediate (font-lock-keyword-face . 1))
  249: 	(("[char]") compile-only (font-lock-keyword-face . 1)
  250: 	 "[ \t\n]" t string (font-lock-string-face . 1))
  251: 	(("char") non-immediate (font-lock-keyword-face . 1)
  252: 	 "[ \t\n]" t string (font-lock-string-face . 1))
  253: 	(("s\"" "c\"") immediate (font-lock-string-face . 1)
  254: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  255: 	((".\"") compile-only (font-lock-string-face . 1)
  256: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  257: 	(("abort\"") compile-only (font-lock-keyword-face . 1)
  258: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  259: 	(("{") compile-only (font-lock-variable-name-face . 1)
  260: 	 "[\n}]" nil name (font-lock-variable-name-face . 1))
  261: 	((".(" "(") immediate (font-lock-comment-face . 1)
  262: 	  ")" nil comment (font-lock-comment-face . 1))
  263: 	(("\\" "\\G") immediate (font-lock-comment-face . 1)
  264: 	 "[\n]" nil comment (font-lock-comment-face . 1))
  265: 	  
  266: 	(("[if]" "[?do]" "[do]" "[for]" "[begin]" 
  267: 	  "[endif]" "[then]" "[loop]" "[+loop]" "[next]" "[until]" "[repeat]"
  268: 	  "[again]" "[while]" "[else]")
  269: 	 immediate (font-lock-keyword-face . 2))
  270: 	(("[ifdef]" "[ifundef]") immediate (font-lock-keyword-face . 2)
  271: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  272: 	(("if" "begin" "ahead" "do" "?do" "+do" "u+do" "-do" "u-do" "for" 
  273: 	  "case" "of" "?dup-if" "?dup-0=-if" "then" "endif" "until"
  274: 	  "repeat" "again" "leave" "?leave"
  275: 	  "loop" "+loop" "-loop" "next" "endcase" "endof" "else" "while" "try"
  276: 	  "recover" "endtry" "iferror" "restore" "endtry-iferror"
  277: 	  "assert(" "assert0(" "assert1(" "assert2("
  278: 	  "assert3(" ")" "<interpretation" "<compilation" "interpretation>" 
  279: 	  "compilation>")
  280: 	 compile-only (font-lock-keyword-face . 2))
  281: 
  282: 	(("true" "false" "c/l" "bl" "cell" "pi" "w/o" "r/o" "r/w") 
  283: 	 non-immediate (font-lock-constant-face . 2))
  284: 	(("~~" "break:" "dbg") compile-only (font-lock-warning-face . 2))
  285: 	(("break\"") compile-only (font-lock-warning-face . 1)
  286: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  287: 	(("postpone" "[is]" "defers" "[']" "[compile]") 
  288: 	 compile-only (font-lock-keyword-face . 2)
  289: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  290: 	(("is" "what's") immediate (font-lock-keyword-face . 2)
  291: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  292: 	(("<is>" "'" "see") non-immediate (font-lock-keyword-face . 2)
  293: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  294: 	(("[to]") compile-only (font-lock-keyword-face . 2)
  295: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  296: 	(("to") immediate (font-lock-keyword-face . 2)
  297: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  298: 	(("<to>") non-immediate (font-lock-keyword-face . 2)
  299: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  300: 
  301: 	(("create" "variable" "constant" "2variable" "2constant" "fvariable"
  302: 	  "fconstant" "value" "field" "user" "vocabulary" 
  303: 	  "create-interpret/compile")
  304: 	 non-immediate (font-lock-type-face . 2)
  305: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  306: 	("\\S-+%" non-immediate (font-lock-type-face . 2))
  307: 	(("defer" "alias" "create-interpret/compile:") 
  308: 	 non-immediate (font-lock-type-face . 1)
  309: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  310: 	(("end-struct") non-immediate (font-lock-keyword-face . 2)
  311: 	 "[ \t\n]" t name (font-lock-type-face . 3))
  312: 	(("struct") non-immediate (font-lock-keyword-face . 2))
  313: 	("-?[0-9]+\\(\\.[0-9]*e\\(-?[0-9]+\\)?\\|\\.?[0-9a-f]*\\)" 
  314: 	 immediate (font-lock-constant-face . 3))
  315: 	))
  316: 
  317: (defvar forth-use-objects nil 
  318:   "*Non-nil makes forth-mode also hilight words from the \"Objects\" package.")
  319: (defvar forth-objects-words
  320:   '(((":m") definition-starter (font-lock-keyword-face . 1)
  321:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  322:     (("m:") definition-starter (font-lock-keyword-face . 1))
  323:     ((";m") definition-ender (font-lock-keyword-face . 1))
  324:     (("[current]" "[parent]") compile-only (font-lock-keyword-face . 1)
  325:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  326:     (("current" "overrides") non-immediate (font-lock-keyword-face . 2)
  327:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  328:     (("[to-inst]") compile-only (font-lock-keyword-face . 2)
  329:      "[ \t\n]" t name (font-lock-variable-name-face . 3))
  330:     (("[bind]") compile-only (font-lock-keyword-face . 2)
  331:      "[ \t\n]" t name (font-lock-type-face . 3)
  332:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  333:     (("bind") non-immediate (font-lock-keyword-face . 2)
  334:      "[ \t\n]" t name (font-lock-type-face . 3)
  335:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  336:     (("inst-var" "inst-value") non-immediate (font-lock-type-face . 2)
  337:      "[ \t\n]" t name (font-lock-variable-name-face . 3))
  338:     (("method" "selector")
  339:      non-immediate (font-lock-type-face . 1)
  340:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  341:     (("end-class" "end-interface")
  342:      non-immediate (font-lock-keyword-face . 2)
  343:      "[ \t\n]" t name (font-lock-type-face . 3))
  344:     (("public" "protected" "class" "exitm" "implementation" "interface"
  345:       "methods" "end-methods" "this") 
  346:      non-immediate (font-lock-keyword-face . 2))
  347:     (("object") non-immediate (font-lock-type-face . 2)))
  348:   "Hilighting description for words of the \"Objects\" package")
  349: 
  350: 
  351: (defvar forth-use-oof nil 
  352:   "*Non-nil makes forth-mode also hilight words from the \"OOF\" package.")
  353: (defvar forth-oof-words 
  354:   '((("class") non-immediate (font-lock-keyword-face . 2)
  355:      "[ \t\n]" t name (font-lock-type-face . 3))
  356:     (("var") non-immediate (font-lock-type-face . 2)
  357:      "[ \t\n]" t name (font-lock-variable-name-face . 3))
  358:     (("method" "early") non-immediate (font-lock-type-face . 2)
  359:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  360:     (("::" "super" "bind" "bound" "link") 
  361:      immediate (font-lock-keyword-face . 2)
  362:      "[ \t\n]" t name (font-lock-function-name-face . 3))
  363:     (("ptr" "asptr" "[]") 
  364:      immediate (font-lock-keyword-face . 2)
  365:      "[ \t\n]" t name (font-lock-variable-name-face . 3))
  366:     (("class;" "how:" "self" "new" "new[]" "definitions" "class?" "with"
  367:       "endwith")
  368:      non-immediate (font-lock-keyword-face . 2))
  369:     (("object") non-immediate (font-lock-type-face . 2)))
  370:   "Hilighting description for words of the \"OOF\" package")
  371: 
  372: (defvar forth-local-words nil 
  373:   "List of Forth words to prepend to `forth-words'. Should be set by a 
  374:  forth source, using a local variables list at the end of the file 
  375:  (\"Local Variables: ... forth-local-words: ... End:\" construct).") 
  376: 
  377: (defvar forth-custom-words nil
  378:   "List of Forth words to prepend to `forth-words'. Should be set in your
  379:  .emacs.")
  380: 
  381: (defvar forth-hilight-level 3 "*Level of hilighting of Forth code.")
  382: 
  383: (defvar forth-compiled-words nil "Compiled representation of `forth-words'.")
  384: 
  385: (defvar forth-indent-words nil 
  386:   "List of words that have indentation behaviour.
  387: Each element of `forth-indent-words' should have the form
  388:    (MATCHER INDENT1 INDENT2 &optional TYPE) 
  389:   
  390: MATCHER is either a list of strings to match, or a REGEXP.
  391:    If it's a REGEXP, it should not be surrounded by `\\<` or `\\>`, since 
  392:    that'll be done automatically by the search routines.
  393: 
  394: TYPE might be omitted. If it's specified, the only allowed value is 
  395:    currently the symbol `non-immediate', meaning that the word will not 
  396:    have any effect on indentation inside definitions. (:NONAME is a good 
  397:    example for this kind of word).
  398: 
  399: INDENT1 specifies how to indent a word that's located at the beginning
  400:    of a line, following any number of whitespaces.
  401: 
  402: INDENT2 specifies how to indent words that are not located at the
  403:    beginning of a line.
  404: 
  405: INDENT1 and INDENT2 are indentation specifications of the form
  406:    (SELF-INDENT . NEXT-INDENT), where SELF-INDENT is a numerical value, 
  407:    specifying how the matching line and all following lines are to be 
  408:    indented, relative to previous lines. NEXT-INDENT specifies how to indent 
  409:    following lines, relative to the matching line.
  410:   
  411:    Even values of SELF-INDENT and NEXT-INDENT correspond to multiples of
  412:    `forth-indent-level'. Odd values get an additional 
  413:    `forth-minor-indent-level' added/substracted. Eg a value of -2 indents
  414:    1 * forth-indent-level  to the left, wheras 3 indents 
  415:    1 * forth-indent-level + forth-minor-indent-level  columns to the right.")
  416: 
  417: (setq forth-indent-words
  418:       '((("if" "begin" "do" "?do" "+do" "-do" "u+do"
  419: 	  "u-do" "?dup-if" "?dup-0=-if" "case" "of" "try" "iferror"
  420: 	  "[if]" "[ifdef]" "[ifundef]" "[begin]" "[for]" "[do]" "[?do]")
  421: 	 (0 . 2) (0 . 2))
  422: 	((":" ":noname" "code" "struct" "m:" ":m" "class" "interface")
  423: 	 (0 . 2) (0 . 2) non-immediate)
  424: 	("\\S-+%$" (0 . 2) (0 . 0) non-immediate)
  425: 	((";" ";m") (-2 . 0) (0 . -2))
  426: 	(("again" "then" "endif" "endtry" "endcase" "endof" 
  427: 	  "[then]" "[endif]" "[loop]" "[+loop]" "[next]" 
  428: 	  "[until]" "[again]" "loop")
  429: 	 (-2 . 0) (0 . -2))
  430: 	(("end-code" "end-class" "end-interface" "end-class-noname" 
  431: 	  "end-interface-noname" "end-struct" "class;")
  432: 	 (-2 . 0) (0 . -2) non-immediate)
  433: 	(("protected" "public" "how:") (-1 . 1) (0 . 0) non-immediate)
  434: 	(("+loop" "-loop" "until") (-2 . 0) (-2 . 0))
  435: 	(("else" "recover" "restore" "endtry-iferror" "[else]")
  436: 	 (-2 . 2) (0 . 0))
  437: 	(("does>") (-1 . 1) (0 . 0))
  438: 	(("while" "[while]") (-2 . 4) (0 . 2))
  439: 	(("repeat" "[repeat]") (-4 . 0) (0 . -4))))
  440: 
  441: (defvar forth-local-indent-words nil 
  442:   "List of Forth words to prepend to `forth-indent-words', when a forth-mode
  443: buffer is created. Should be set by a Forth source, using a local variables 
  444: list at the end of the file (\"Local Variables: ... forth-local-words: ... 
  445: End:\" construct).")
  446: 
  447: (defvar forth-custom-indent-words nil
  448:   "List of Forth words to prepend to `forth-indent-words'. Should be set in
  449:  your .emacs.")
  450: 
  451: (defvar forth-indent-level 4
  452:   "*Indentation of Forth statements.")
  453: (defvar forth-minor-indent-level 2
  454:   "*Minor indentation of Forth statements.")
  455: (defvar forth-compiled-indent-words nil)
  456: 
  457: ;(setq debug-on-error t)
  458: 
  459: ;; Filter list by predicate. This is a somewhat standard function for 
  460: ;; functional programming languages. So why isn't it already implemented 
  461: ;; in Lisp??
  462: (defun forth-filter (predicate list)
  463:   (let ((filtered nil))
  464:     (mapcar (lambda (item)
  465: 	      (when (funcall predicate item)
  466: 		(if filtered
  467: 		    (nconc filtered (list item))
  468: 		  (setq filtered (cons item nil))))
  469: 	      nil) list)
  470:     filtered))
  471: 
  472: ;; Helper function for `forth-compile-word': return whether word has to be
  473: ;; added to the compiled word list, for syntactic parsing and hilighting.
  474: (defun forth-words-filter (word)
  475:   (let* ((hilight (nth 2 word))
  476: 	 (level (cdr hilight))
  477: 	 (parsing-flag (nth 3 word)))
  478:     (or parsing-flag 
  479: 	(<= level forth-hilight-level))))
  480: 
  481: ;; Helper function for `forth-compile-word': translate one entry from 
  482: ;; `forth-words' into the form  (regexp regexp-depth word-description)
  483: (defun forth-compile-words-mapper (word)
  484:   ;; warning: we cannot rely on regexp-opt's PAREN argument, since
  485:   ;; XEmacs will use shy parens by default :-(
  486:   (let* ((matcher (car word))
  487: 	 (regexp 
  488: 	  (concat "\\(" (cond ((stringp matcher) matcher)
  489: 			      ((listp matcher) (regexp-opt matcher))
  490: 			      (t (error "Invalid matcher `%s'")))
  491: 		  "\\)"))
  492: 	 (depth (regexp-opt-depth regexp))
  493: 	 (description (cdr word)))
  494:     (list regexp depth description)))
  495: 
  496: ;; Read `words' and create a compiled representation suitable for efficient
  497: ;; parsing of the form  
  498: ;; (regexp (subexp-count word-description) (subexp-count2 word-description2)
  499: ;;  ...)
  500: (defun forth-compile-wordlist (words)
  501:   (let* ((mapped (mapcar 'forth-compile-words-mapper words))
  502: 	 (regexp (concat "\\<\\(" 
  503: 			 (mapconcat 'car mapped "\\|")
  504: 			 "\\)\\>"))
  505: 	 (sub-count 2)
  506: 	 (sub-list (mapcar 
  507: 		    (lambda (i) 
  508: 		      (let ((sub (cons sub-count (nth 2 i))))
  509: 			(setq sub-count (+ sub-count (nth 1 i)))
  510: 			sub 
  511: 			)) 
  512: 		    mapped)))
  513:     (let ((result (cons regexp sub-list)))
  514:       (byte-compile 'result)
  515:       result)))
  516: 
  517: (defun forth-compile-words ()
  518:   "Compile the the words from `forth-words' and `forth-indent-words' into
  519:  the format that's later used for doing the actual hilighting/indentation.
  520:  Store the resulting compiled wordlists in `forth-compiled-words' and 
  521: `forth-compiled-indent-words', respective"
  522:   (setq forth-compiled-words 
  523: 	(forth-compile-wordlist 
  524: 	 (forth-filter 'forth-words-filter forth-words)))
  525:   (setq forth-compiled-indent-words 
  526: 	(forth-compile-wordlist forth-indent-words)))
  527: 
  528: (defun forth-hack-local-variables ()
  529:   "Parse and bind local variables, set in the contents of the current 
  530:  forth-mode buffer. Prepend `forth-local-words' to `forth-words' and 
  531:  `forth-local-indent-words' to `forth-indent-words'."
  532:   (hack-local-variables)
  533:   (setq forth-words (append forth-local-words forth-words))
  534:   (setq forth-indent-words (append forth-local-indent-words 
  535: 				   forth-indent-words)))
  536: 
  537: (defun forth-customize-words ()
  538:   "Add the words from `forth-custom-words' and `forth-custom-indent-words'
  539:  to `forth-words' and `forth-indent-words', respective. Add 
  540:  `forth-objects-words' and/or `forth-oof-words' to `forth-words', if
  541:  `forth-use-objects' and/or `forth-use-oof', respective is set."
  542:   (setq forth-words (append forth-custom-words forth-words
  543: 			    (if forth-use-oof forth-oof-words nil)
  544: 			    (if forth-use-objects forth-objects-words nil)))
  545:   (setq forth-indent-words (append 
  546: 			    forth-custom-indent-words forth-indent-words)))
  547: 
  548: 
  549: 
  550: ;; get location of first character of previous forth word that's got 
  551: ;; properties
  552: (defun forth-previous-start (pos)
  553:   (let* ((word (get-text-property pos 'forth-word))
  554: 	 (prev (previous-single-property-change 
  555: 		(min (point-max) (1+ pos)) 'forth-word 
  556: 		(current-buffer) (point-min))))
  557:     (if (or (= (point-min) prev) word) prev
  558:       (if (get-text-property (1- prev) 'forth-word)
  559: 	  (previous-single-property-change 
  560: 	   prev 'forth-word (current-buffer) (point-min))
  561: 	(point-min)))))
  562: 
  563: ;; Get location of the last character of the current/next forth word that's
  564: ;; got properties, text that's parsed by the word is considered as parts of 
  565: ;; the word.
  566: (defun forth-next-end (pos)
  567:   (let* ((word (get-text-property pos 'forth-word))
  568: 	 (next (next-single-property-change pos 'forth-word 
  569: 					    (current-buffer) (point-max))))
  570:     (if word next
  571:       (if (get-text-property next 'forth-word)
  572: 	  (next-single-property-change 
  573: 	   next 'forth-word (current-buffer) (point-max))
  574: 	(point-max)))))
  575: 
  576: (defun forth-next-whitespace (pos)
  577:   (save-excursion
  578:     (goto-char pos)
  579:     (skip-syntax-forward "-" (point-max))
  580:     (point)))
  581: (defun forth-previous-word (pos)
  582:   (save-excursion
  583:     (goto-char pos)
  584:     (re-search-backward "\\<" pos (point-min) 1)
  585:     (point)))
  586: 
  587: ;; Delete all properties, used by Forth mode, from `from' to `to'.
  588: (defun forth-delete-properties (from to)
  589:   (remove-text-properties 
  590:    from to '(face nil fontified nil 
  591: 		  forth-parsed nil forth-word nil forth-state nil)))
  592: 
  593: ;; Get the index of the branch of the most recently evaluated regular 
  594: ;; expression that matched. (used for identifying branches "a\\|b\\|c...")
  595: (defun forth-get-regexp-branch ()
  596:   (let ((count 2))
  597:     (while (not (condition-case err (match-beginning count)
  598: 		  (args-out-of-range t)))  ; XEmacs requires error handling
  599:       (setq count (1+ count)))
  600:     count))
  601: 
  602: ;; seek to next forth-word and return its "word-description"
  603: (defun forth-next-known-forth-word (to)
  604:   (if (<= (point) to)
  605:       (progn
  606: 	(let* ((regexp (car forth-compiled-words))
  607: 	       (pos (re-search-forward regexp to t)))
  608: 	  (if pos (let ((branch (forth-get-regexp-branch))
  609: 			(descr (cdr forth-compiled-words)))
  610: 		    (goto-char (match-beginning 0))
  611: 		    (cdr (assoc branch descr)))
  612: 	    'nil)))
  613:     nil))
  614: 
  615: ;; Set properties of forth word at `point', eventually parsing subsequent 
  616: ;; words, and parsing all whitespaces. Set point to delimiter after word.
  617: ;; The word, including it's parsed text gets the `forth-word' property, whose 
  618: ;; value is unique, and may be used for getting the word's start/end 
  619: ;; positions.
  620: (defun forth-set-word-properties (state data)
  621:   (let* ((start (point))
  622: 	 (end (progn (re-search-forward "[ \t]\\|$" (point-max) 1)
  623: 		     (point)))
  624: 	 (type (car data))
  625: 	 (hilight (nth 1 data))
  626: 	 (bad-word (and (not state) (eq type 'compile-only)))
  627: 	 (hlface (if bad-word font-lock-warning-face
  628: 		   (if (<= (cdr hilight) forth-hilight-level)
  629: 		       (car hilight) nil))))
  630:     (when hlface (put-text-property start end 'face hlface))
  631:     ;; if word parses in current state, process parsed range of text
  632:     (when (or (not state) (eq type 'compile-only) (eq type 'immediate))
  633:       (let ((parse-data (nthcdr 2 data)))
  634: 	(while parse-data
  635: 	  (let ((delim (nth 0 parse-data))
  636: 		(skip-leading (nth 1 parse-data))
  637: 		(parse-type (nth 2 parse-data))
  638: 		(parsed-hilight (nth 3 parse-data))
  639: 		(parse-start (point))
  640: 		(parse-end))
  641: 	    (when skip-leading
  642: 	      (while (and (looking-at delim) (> (match-end 0) (point))
  643: 			  (not (looking-at "\n")))
  644: 		(forward-char)))
  645: 	    (re-search-forward delim (point-max) 1)
  646: 	    (setq parse-end (point))
  647: 	    (forth-delete-properties end parse-end)
  648: 	    (when (<= (cdr parsed-hilight) forth-hilight-level)
  649: 	      (put-text-property 
  650: 	       parse-start parse-end 'face (car parsed-hilight)))
  651: 	    (put-text-property 
  652: 	     parse-start parse-end 'forth-parsed parse-type)
  653: 	    (setq end parse-end)
  654: 	    (setq parse-data (nthcdr 4 parse-data))))))
  655:     (put-text-property start end 'forth-word start)))
  656: 
  657: ;; Search for known Forth words in the range `from' to `to', using 
  658: ;; `forth-next-known-forth-word' and set their properties via 
  659: ;; `forth-set-word-properties'.
  660: (defun forth-update-properties (from to &optional loudly)
  661:   (save-excursion
  662:     (let ((msg-count 0) (state) (word-descr) (last-location))
  663:       (goto-char (forth-previous-word (forth-previous-start 
  664: 				       (max (point-min) (1- from)))))
  665:       (setq to (forth-next-end (min (point-max) (1+ to))))
  666:       ;; `to' must be on a space delimiter, if a parsing word was changed
  667:       (setq to (forth-next-whitespace to))
  668:       (setq state (get-text-property (point) 'forth-state))
  669:       (setq last-location (point))
  670:       (forth-delete-properties (point) to)
  671:       (put-text-property (point) to 'fontified t)
  672:       ;; hilight loop...
  673:       (while (setq word-descr (forth-next-known-forth-word to))
  674: 	(when loudly
  675: 	  (when (equal 0 (% msg-count 100))
  676: 	    (message "Parsing Forth code...%s"
  677: 		     (make-string (/ msg-count 100) ?.)))
  678: 	  (setq msg-count (1+ msg-count)))
  679: 	(forth-set-word-properties state word-descr)
  680: 	(when state (put-text-property last-location (point) 'forth-state t))
  681: 	(let ((type (car word-descr)))
  682: 	  (if (eq type 'definition-starter) (setq state t))
  683: 	  (if (eq type 'definition-ender) (setq state nil))
  684: 	  (setq last-location (point))))
  685:       ;; update state property up to `to'
  686:       (if (and state (< (point) to))
  687: 	  (put-text-property last-location to 'forth-state t))
  688:       ;; extend search if following state properties differ from current state
  689:       (if (< to (point-max))
  690: 	  (if (not (equal state (get-text-property (1+ to) 'forth-state)))
  691: 	      (let ((extend-to (next-single-property-change 
  692: 				to 'forth-state (current-buffer) (point-max))))
  693: 		(forth-update-properties to extend-to))
  694: 	    ))
  695:       )))
  696: 
  697: ;; save-buffer-state borrowed from `font-lock.el'
  698: (eval-when-compile 
  699:   (defmacro forth-save-buffer-state (varlist &rest body)
  700:     "Bind variables according to VARLIST and eval BODY restoring buffer state."
  701:     (` (let* ((,@ (append varlist
  702: 		   '((modified (buffer-modified-p)) (buffer-undo-list t)
  703: 		     (inhibit-read-only t) (inhibit-point-motion-hooks t)
  704: 		     before-change-functions after-change-functions
  705: 		     deactivate-mark buffer-file-name buffer-file-truename))))
  706: 	 (,@ body)
  707: 	 (when (and (not modified) (buffer-modified-p))
  708: 	   (set-buffer-modified-p nil))))))
  709: 
  710: ;; Function that is added to the `change-functions' hook. Calls 
  711: ;; `forth-update-properties' and keeps care of disabling undo information
  712: ;; and stuff like that.
  713: (defun forth-change-function (from to len &optional loudly)
  714:   (save-match-data
  715:     (forth-save-buffer-state 
  716:      () 
  717:      (unless forth-disable-parser (forth-update-properties from to loudly))
  718:      (forth-update-warn-long-lines))))
  719: 
  720: (defun forth-fontification-function (from)
  721:   "Function to be called from `fontification-functions' of Emacs 21."
  722:   (save-match-data
  723:     (forth-save-buffer-state
  724:      ((to (min (point-max) (+ from 100))))
  725:      (unless (or forth-disable-parser (not forth-jit-parser)
  726: 		 (get-text-property from 'fontified))
  727:        (forth-update-properties from to)))))
  728: 
  729: (eval-when-compile
  730:   (byte-compile 'forth-set-word-properties)
  731:   (byte-compile 'forth-next-known-forth-word)
  732:   (byte-compile 'forth-update-properties)
  733:   (byte-compile 'forth-delete-properties)
  734:   (byte-compile 'forth-get-regexp-branch)) 
  735: 
  736: ;;; imenu support
  737: ;;;
  738: (defvar forth-defining-words 
  739:   '("VARIABLE" "CONSTANT" "2VARIABLE" "2CONSTANT" "FVARIABLE" "FCONSTANT"
  740:    "USER" "VALUE" "field" "end-struct" "VOCABULARY" "CREATE" ":" "CODE"
  741:    "DEFER" "ALIAS")
  742:   "List of words, that define the following word.
  743: Used for imenu index generation.")
  744: 
  745: (defvar forth-defining-words-regexp nil 
  746:   "Regexp that's generated for matching `forth-defining-words'")
  747:  
  748: (defun forth-next-definition-starter ()
  749:   (progn
  750:     (let* ((pos (re-search-forward forth-defining-words-regexp (point-max) t)))
  751:       (if pos
  752: 	  (if (or (text-property-not-all (match-beginning 0) (match-end 0) 
  753: 					 'forth-parsed nil)
  754: 		  (text-property-not-all (match-beginning 0) (match-end 0)
  755: 					 'forth-state nil)) 
  756: 	      (forth-next-definition-starter)
  757: 	    t)
  758: 	nil))))
  759: 
  760: (defun forth-create-index ()
  761:   (let* ((forth-defining-words-regexp 
  762: 	  (concat "\\<\\(" (regexp-opt forth-defining-words) "\\)\\>"))
  763: 	 (index nil))
  764:     (goto-char (point-min))
  765:     (while (forth-next-definition-starter)
  766:       (if (looking-at "[ \t]*\\([^ \t\n]+\\)")
  767: 	  (setq index (cons (cons (match-string 1) (point)) index))))
  768:     index))
  769: 
  770: ;; top-level require is executed at byte-compile and load time
  771: (eval-and-compile (forth-require 'speedbar))
  772: 
  773: ;; this code is executed at load-time only
  774: (when (memq 'speedbar features)
  775:   (speedbar-add-supported-extension ".fs")
  776:   (speedbar-add-supported-extension ".fb"))
  777: 
  778: ;; (require 'profile)
  779: ;; (setq profile-functions-list '(forth-set-word-properties forth-next-known-forth-word forth-update-properties forth-delete-properties forth-get-regexp-branch))
  780: 
  781: ;;; Indentation
  782: ;;;
  783: 
  784: ;; Return, whether `pos' is the first forth word on its line
  785: (defun forth-first-word-on-line-p (pos)
  786:   (save-excursion
  787:     (beginning-of-line)
  788:     (skip-chars-forward " \t")
  789:     (= pos (point))))
  790: 
  791: ;; Return indentation data (SELF-INDENT . NEXT-INDENT) of next known 
  792: ;; indentation word, or nil if there is no word up to `to'. 
  793: ;; Position `point' at location just after found word, or at `to'. Parsed 
  794: ;; ranges of text will not be taken into consideration!
  795: (defun forth-next-known-indent-word (to)
  796:   (if (<= (point) to)
  797:       (progn
  798: 	(let* ((regexp (car forth-compiled-indent-words))
  799: 	       (pos (re-search-forward regexp to t)))
  800: 	  (if pos
  801: 	      (let* ((start (match-beginning 0))
  802: 		     (end (match-end 0))
  803: 		     (branch (forth-get-regexp-branch))
  804: 		     (descr (cdr forth-compiled-indent-words))
  805: 		     (indent (cdr (assoc branch descr)))
  806: 		     (type (nth 2 indent)))
  807: 		;; skip words that are parsed (strings/comments) and 
  808: 		;; non-immediate words inside definitions
  809: 		(if (or (text-property-not-all start end 'forth-parsed nil)
  810: 			(and (eq type 'non-immediate) 
  811: 			     (text-property-not-all start end 
  812: 						    'forth-state nil)))
  813: 		    (forth-next-known-indent-word to)
  814: 		  (if (forth-first-word-on-line-p (match-beginning 0))
  815: 		      (nth 0 indent) (nth 1 indent))))
  816: 	    nil)))
  817:     nil))
  818:   
  819: ;; Translate indentation value `indent' to indentation column. Multiples of
  820: ;; 2 correspond to multiples of `forth-indent-level'. Odd numbers get an
  821: ;; additional `forth-minor-indent-level' added (or substracted).
  822: (defun forth-convert-to-column (indent)
  823:   (let* ((sign (if (< indent 0) -1 1))
  824: 	 (value (abs indent))
  825: 	 (major (* (/ value 2) forth-indent-level))
  826: 	 (minor (* (% value 2) forth-minor-indent-level)))
  827:     (* sign (+ major minor))))
  828: 
  829: ;; Return the column increment, that the current line of forth code does to
  830: ;; the current or following lines. `which' specifies which indentation values
  831: ;; to use. 1 means the indentation of following lines relative to current 
  832: ;; line, 0 means the indentation of the current line relative to the previous 
  833: ;; line. Return `nil', if there are no indentation words on the current line.
  834: (defun forth-get-column-incr (which)
  835:   (save-excursion
  836:     (let ((regexp (car forth-compiled-indent-words))
  837: 	  (word-indent)
  838: 	  (self-indent nil)
  839: 	  (next-indent nil)
  840: 	  (to (save-excursion (end-of-line) (point))))
  841:       (beginning-of-line)
  842:       (while (setq word-indent (forth-next-known-indent-word to))
  843: 	(let* ((self-incr (car word-indent))
  844: 	       (next-incr (cdr word-indent))
  845: 	       (self-column-incr (forth-convert-to-column self-incr))
  846: 	       (next-column-incr (forth-convert-to-column next-incr)))
  847: 	  (setq next-indent (if next-indent next-indent 0))
  848: 	  (setq self-indent (if self-indent self-indent 0))
  849: 	  (if (or (and (> next-indent 0) (< self-column-incr 0))
  850: 		  (and (< next-indent 0) (> self-column-incr 0)))
  851: 	      (setq next-indent (+ next-indent self-column-incr))
  852: 	    (setq self-indent (+ self-indent self-column-incr)))
  853: 	  (setq next-indent (+ next-indent next-column-incr))))
  854:       (nth which (list self-indent next-indent)))))
  855: 
  856: ;; Find previous line that contains indentation words, return the column,
  857: ;; to which following text should be indented to.
  858: (defun forth-get-anchor-column ()
  859:   (save-excursion
  860:     (if (/= 0 (forward-line -1)) 0
  861:       (let ((indent))
  862: 	(while (not (or (setq indent (forth-get-column-incr 1))
  863: 			(<= (point) (point-min))))
  864: 	  (forward-line -1))
  865: 	(+ (current-indentation) (if indent indent 0))))))
  866: 
  867: (defun forth-indent-line (&optional flag)
  868:   "Correct indentation of the current Forth line."
  869:   (let* ((anchor (forth-get-anchor-column))
  870: 	 (column-incr (forth-get-column-incr 0)))
  871:     (forth-indent-to (if column-incr (+ anchor column-incr) anchor))))
  872: 
  873: (defun forth-current-column ()
  874:   (- (point) (save-excursion (beginning-of-line) (point))))
  875: (defun forth-current-indentation ()
  876:   (- (save-excursion (beginning-of-line) (forward-to-indentation 0) (point))
  877:      (save-excursion (beginning-of-line) (point))))
  878: 
  879: (defun forth-indent-to (x)
  880:   (let ((p nil))
  881:     (setq p (- (forth-current-column) (forth-current-indentation)))
  882:     (forth-delete-indentation)
  883:     (beginning-of-line)
  884:     (indent-to x)
  885:     (if (> p 0) (forward-char p))))
  886: 
  887: (defun forth-delete-indentation ()
  888:   (save-excursion
  889:     (delete-region 
  890:      (progn (beginning-of-line) (point)) 
  891:      (progn (back-to-indentation) (point)))))
  892: 
  893: (defun forth-indent-command ()
  894:   (interactive)
  895:   (forth-indent-line t))
  896: 
  897: ;; remove trailing whitespaces in current line
  898: (defun forth-remove-trailing ()
  899:   (save-excursion
  900:     (end-of-line)
  901:     (delete-region (point) (progn (skip-chars-backward " \t") (point)))))
  902: 
  903: ;; insert newline, removing any trailing whitespaces in the current line
  904: (defun forth-newline-remove-trailing ()
  905:   (save-excursion
  906:     (delete-region (point) (progn (skip-chars-backward " \t") (point))))
  907:   (newline))
  908: ;  (let ((was-point (point-marker)))
  909: ;    (unwind-protect 
  910: ;	(progn (forward-line -1) (forth-remove-trailing))
  911: ;      (goto-char (was-point)))))
  912: 
  913: ;; workaround for bug in `reindent-then-newline-and-indent'
  914: (defun forth-reindent-then-newline-and-indent ()
  915:   (interactive "*")
  916:   (indent-according-to-mode)
  917:   (forth-newline-remove-trailing)
  918:   (indent-according-to-mode))
  919: 
  920: 
  921: ;;; Block file encoding/decoding  (dk)
  922: ;;;
  923: 
  924: (defconst forth-c/l 64 "Number of characters per block line")
  925: (defconst forth-l/b 16 "Number of lines per block")
  926: 
  927: ;; Check whether the unconverted block file line, point is in, does not
  928: ;; contain `\n' and `\t' characters.
  929: (defun forth-check-block-line (line)
  930:   (let ((end (save-excursion (beginning-of-line) (forward-char forth-c/l)
  931: 			     (point))))
  932:     (save-excursion 
  933:       (beginning-of-line)
  934:       (when (search-forward "\n" end t)
  935: 	(message "Warning: line %i contains newline character #10" line)
  936: 	(ding t))
  937:       (beginning-of-line)
  938:       (when (search-forward "\t" end t)
  939: 	(message "Warning: line %i contains tab character #8" line)
  940: 	(ding t)))))
  941: 
  942: (defun forth-convert-from-block (from to)
  943:   "Convert block file format to stream source in current buffer."
  944:   (let ((line (count-lines (point-min) from)))
  945:     (save-excursion
  946:       (goto-char from)
  947:       (set-mark to)
  948:       (while (< (+ (point) forth-c/l) (mark t))
  949: 	(setq line (1+ line))
  950: 	(forth-check-block-line line)
  951: 	(forward-char forth-c/l)
  952: 	(forth-newline-remove-trailing))
  953:       (when (= (+ (point) forth-c/l) (mark t))
  954: 	(forth-remove-trailing))
  955:       (mark t))))
  956: 
  957: ;; Pad a line of a block file up to `forth-c/l' characters, positioning `point'
  958: ;; at the end of line.
  959: (defun forth-pad-block-line ()
  960:   (save-excursion
  961:     (end-of-line)
  962:     (if (<= (current-column) forth-c/l)
  963: 	(move-to-column forth-c/l t)
  964:       (message "Line %i longer than %i characters, truncated"
  965: 	       (count-lines (point-min) (point)) forth-c/l)
  966:       (ding t)
  967:       (move-to-column forth-c/l t)
  968:       (delete-region (point) (progn (end-of-line) (point))))))
  969: 
  970: ;; Replace tab characters in current line by spaces.
  971: (defun forth-convert-tabs-in-line ()
  972:   (save-excursion
  973:     (beginning-of-line)
  974:     (while (search-forward "\t" (save-excursion (end-of-line) (point)) t)
  975:       (backward-char)
  976:       (delete-region (point) (1+ (point)))
  977:       (insert-char ?\  (- tab-width (% (current-column) tab-width))))))
  978: 
  979: ;; Delete newline at end of current line, concatenating it with the following
  980: ;; line. Place `point' at end of newly formed line.
  981: (defun forth-delete-newline ()
  982:   (end-of-line)
  983:   (delete-region (point) (progn (beginning-of-line 2) (point))))
  984: 
  985: (defun forth-convert-to-block (from to &optional original-buffer) 
  986:   "Convert range of text to block file format in current buffer."
  987:   (let* ((lines 0)) ; I have to count lines myself, since `count-lines' has
  988: 		    ; problems with trailing newlines...
  989:     (save-excursion
  990:       (goto-char from)
  991:       (set-mark to)
  992:       ;; pad lines to full length (`forth-c/l' characters per line)
  993:       (while (< (save-excursion (end-of-line) (point)) (mark t))
  994: 	(setq lines (1+ lines))
  995: 	(forth-pad-block-line)
  996: 	(forth-convert-tabs-in-line)
  997: 	(forward-line))
  998:       ;; also make sure the last line is padded, if `to' is at its end
  999:       (end-of-line)
 1000:       (when (= (point) (mark t))
 1001: 	(setq lines (1+ lines))
 1002: 	(forth-pad-block-line)
 1003: 	(forth-convert-tabs-in-line))
 1004:       ;; remove newlines between lines
 1005:       (goto-char from)
 1006:       (while (< (save-excursion (end-of-line) (point)) (mark t))
 1007: 	(forth-delete-newline))
 1008:       ;; append empty lines, until last block is complete
 1009:       (goto-char (mark t))
 1010:       (let* ((required (* (/ (+ lines (1- forth-l/b)) forth-l/b) forth-l/b))
 1011: 	     (pad-lines (- required lines)))
 1012: 	(while (> pad-lines 0)
 1013: 	  (insert-char ?\  forth-c/l)
 1014: 	  (setq pad-lines (1- pad-lines))))
 1015:       (point))))
 1016: 
 1017: (defun forth-detect-block-file-p ()
 1018:   "Return non-nil if the current buffer is in block file format. Detection is
 1019: done by checking whether the first line has 1024 characters or more."
 1020:   (save-restriction 
 1021:     (widen)
 1022:     (save-excursion
 1023:        (goto-char (point-min))
 1024:        (end-of-line)
 1025:        (>= (current-column) 1024))))
 1026: 
 1027: ;; add block file conversion routines to `format-alist'
 1028: (defconst forth-block-format-description
 1029:   '(forth-blocks "Forth block source file" nil 
 1030: 		 forth-convert-from-block forth-convert-to-block 
 1031: 		 t normal-mode))
 1032: (unless (memq forth-block-format-description format-alist)
 1033:   (setq format-alist (cons forth-block-format-description format-alist)))
 1034: 
 1035: ;;; End block file encoding/decoding
 1036: 
 1037: ;;; Block file editing
 1038: ;;;
 1039: (defvar forth-overlay-arrow-string ">>")
 1040: (defvar forth-block-base 1 "Number of first block in block file")
 1041: (defvar forth-show-screen nil
 1042:   "Non-nil means to show screen starts and numbers (for block files)")
 1043: (defvar forth-warn-long-lines nil
 1044:   "Non-nil means to warn about lines that are longer than 64 characters")
 1045: 
 1046: (defvar forth-screen-marker nil)
 1047: (defvar forth-screen-number-string nil)
 1048: 
 1049: (defun forth-update-show-screen ()
 1050:   "If `forth-show-screen' is non-nil, put overlay arrow to start of screen, 
 1051: `point' is in. If arrow now points to different screen than before, display 
 1052: screen number."
 1053:   (if (not forth-show-screen)
 1054:       (setq overlay-arrow-string nil)
 1055:     (save-excursion
 1056:       (let* ((line (count-lines (point-min) (min (point-max) (1+ (point)))))
 1057: 	     (first-line (1+ (* (/ (1- line) forth-l/b) forth-l/b)))
 1058: 	     (scr (+ forth-block-base (/ first-line forth-l/b))))
 1059: 	(setq overlay-arrow-string forth-overlay-arrow-string)
 1060: 	(goto-line first-line)
 1061: 	(setq overlay-arrow-position forth-screen-marker)
 1062: 	(set-marker forth-screen-marker 
 1063: 		    (save-excursion (goto-line first-line) (point)))
 1064: 	(setq forth-screen-number-string (format "%d" scr))))))
 1065: 
 1066: (add-hook 'forth-motion-hooks 'forth-update-show-screen)
 1067: 
 1068: (defun forth-update-warn-long-lines ()
 1069:   "If `forth-warn-long-lines' is non-nil, display a warning whenever a line
 1070: exceeds 64 characters."
 1071:   (when forth-warn-long-lines
 1072:     (when (> (save-excursion (end-of-line) (current-column)) forth-c/l)
 1073:       (message "Warning: current line exceeds %i characters"
 1074: 	       forth-c/l))))
 1075: 
 1076: (add-hook 'forth-motion-hooks 'forth-update-warn-long-lines)
 1077: 
 1078: ;;; End block file editing
 1079: 
 1080: 
 1081: (defvar forth-mode-abbrev-table nil
 1082:   "Abbrev table in use in Forth-mode buffers.")
 1083: 
 1084: (define-abbrev-table 'forth-mode-abbrev-table ())
 1085: 
 1086: (defvar forth-mode-map nil
 1087:   "Keymap used in Forth mode.")
 1088: 
 1089: (if (not forth-mode-map)
 1090:     (setq forth-mode-map (make-sparse-keymap)))
 1091: 
 1092: ;(define-key forth-mode-map "\M-\C-x" 'compile)
 1093: (define-key forth-mode-map "\C-x\\" 'comment-region)
 1094: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
 1095: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
 1096: (define-key forth-mode-map "\e " 'forth-reload)
 1097: (define-key forth-mode-map "\t" 'forth-indent-command)
 1098: (define-key forth-mode-map "\C-m" 'forth-reindent-then-newline-and-indent)
 1099: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
 1100: (define-key forth-mode-map "\e." 'forth-find-tag)
 1101: 
 1102: ;; setup for C-h C-i to work
 1103: (eval-and-compile (forth-require 'info-look))
 1104: (when (memq 'info-look features)
 1105:   (defvar forth-info-lookup '(symbol (forth-mode "\\S-+" t 
 1106: 						  (("(gforth)Word Index"))
 1107: 						  "\\S-+")))
 1108:   (unless (memq forth-info-lookup info-lookup-alist)
 1109:     (setq info-lookup-alist (cons forth-info-lookup info-lookup-alist)))
 1110:   ;; in X-Emacs C-h C-i is by default bound to Info-query
 1111:   (define-key forth-mode-map [?\C-h ?\C-i] 'info-lookup-symbol))
 1112: 
 1113: ;;   (info-lookup-add-help
 1114: ;;    :topic 'symbol
 1115: ;;    :mode 'forth-mode
 1116: ;;    :regexp "[^ 	
 1117: ;; ]+"
 1118: ;;    :ignore-case t
 1119: ;;    :doc-spec '(("(gforth)Name Index" nil "`" "'  "))))
 1120: 
 1121: (require 'etags)
 1122: 
 1123: (defun forth-find-tag (tagname &optional next-p regexp-p)
 1124:   (interactive (find-tag-interactive "Find tag: "))
 1125:   (unless (or regexp-p next-p)
 1126:     (setq tagname (concat "\\(^\\|\\s-+\\)\\(" (regexp-quote tagname) 
 1127: 			    "\\)\\s-*\x7f")))
 1128:   (switch-to-buffer
 1129:    (find-tag-noselect tagname next-p t)))
 1130: 
 1131: (defvar forth-mode-syntax-table nil
 1132:   "Syntax table in use in Forth-mode buffers.")
 1133: 
 1134: ;; Important: hilighting/indentation now depends on a correct syntax table.
 1135: ;; All characters, except whitespace *must* belong to the "word constituent"
 1136: ;; syntax class. If different behaviour is required, use of Categories might
 1137: ;; help.
 1138: (if (not forth-mode-syntax-table)
 1139:     (progn
 1140:       (setq forth-mode-syntax-table (make-syntax-table))
 1141:       (let ((char 0))
 1142: 	(while (< char ?!)
 1143: 	  (modify-syntax-entry char " " forth-mode-syntax-table)
 1144: 	  (setq char (1+ char)))
 1145: 	(while (< char 256)
 1146: 	  (modify-syntax-entry char "w" forth-mode-syntax-table)
 1147: 	  (setq char (1+ char))))
 1148:       ))
 1149: 
 1150: (defun forth-mode-variables ()
 1151:   (set-syntax-table forth-mode-syntax-table)
 1152:   (setq local-abbrev-table forth-mode-abbrev-table)
 1153:   (make-local-variable 'paragraph-start)
 1154:   (setq paragraph-start (concat "^$\\|" page-delimiter))
 1155:   (make-local-variable 'paragraph-separate)
 1156:   (setq paragraph-separate paragraph-start)
 1157:   (make-local-variable 'indent-line-function)
 1158:   (setq indent-line-function 'forth-indent-line)
 1159: ;  (make-local-variable 'require-final-newline)
 1160: ;  (setq require-final-newline t)
 1161:   (make-local-variable 'comment-start)
 1162:   (setq comment-start "\\ ")
 1163:   ;(make-local-variable 'comment-end)
 1164:   ;(setq comment-end " )")
 1165:   (make-local-variable 'comment-column)
 1166:   (setq comment-column 40)
 1167:   (make-local-variable 'comment-start-skip)
 1168:   (setq comment-start-skip "\\\\ ")
 1169:   (make-local-variable 'comment-indent-function)
 1170:   (setq comment-indent-function 'forth-comment-indent)
 1171:   (make-local-variable 'parse-sexp-ignore-comments)
 1172:   (setq parse-sexp-ignore-comments t)
 1173:   (setq case-fold-search t)
 1174:   (make-local-variable 'forth-was-point)
 1175:   (setq forth-was-point -1)
 1176:   (make-local-variable 'forth-words)
 1177:   (make-local-variable 'forth-compiled-words)
 1178:   (make-local-variable 'forth-compiled-indent-words)
 1179:   (make-local-variable 'forth-hilight-level)
 1180:   (make-local-variable 'after-change-functions)
 1181:   (make-local-variable 'forth-show-screen)
 1182:   (make-local-variable 'forth-screen-marker)
 1183:   (make-local-variable 'forth-warn-long-lines)
 1184:   (make-local-variable 'forth-screen-number-string)
 1185:   (make-local-variable 'forth-use-oof)
 1186:   (make-local-variable 'forth-use-objects) 
 1187:   (setq forth-screen-marker (copy-marker 0))
 1188:   (add-hook 'after-change-functions 'forth-change-function)
 1189:   (if (and forth-jit-parser (>= emacs-major-version 21))
 1190:       (add-hook 'fontification-functions 'forth-fontification-function))
 1191:   (setq imenu-create-index-function 'forth-create-index))
 1192: 
 1193: ;;;###autoload
 1194: (defun forth-mode ()
 1195:   "
 1196: Major mode for editing Forth code. Tab indents for Forth code. Comments
 1197: are delimited with \\ and newline. Paragraphs are separated by blank lines
 1198: only. Block files are autodetected, when read, and converted to normal 
 1199: stream source format. See also `forth-block-mode'.
 1200: \\{forth-mode-map}
 1201: 
 1202: Variables controlling syntax hilighting/recognition of parsed text:
 1203:  `forth-words'
 1204:     List of words that have a special parsing behaviour and/or should be
 1205:     hilighted. Add custom words by setting forth-custom-words in your
 1206:     .emacs, or by setting forth-local-words, in source-files' local 
 1207:     variables lists.
 1208:  forth-use-objects
 1209:     Set this variable to non-nil in your .emacs, or in a local variables 
 1210:     list, to hilight and recognize the words from the \"Objects\" package 
 1211:     for object-oriented programming.
 1212:  forth-use-oof
 1213:     Same as above, just for the \"OOF\" package.
 1214:  forth-custom-words
 1215:     List of custom Forth words to prepend to `forth-words'. Should be set
 1216:     in your .emacs.
 1217:  forth-local-words
 1218:     List of words to prepend to `forth-words', whenever a forth-mode
 1219:     buffer is created. That variable should be set by Forth sources, using
 1220:     a local variables list at the end of file, to get file-specific
 1221:     hilighting.
 1222:     0 [IF]
 1223:        Local Variables: ... 
 1224:        forth-local-words: ...
 1225:        End:
 1226:     [THEN]
 1227:  forth-hilight-level
 1228:     Controls how much syntax hilighting is done. Should be in the range 
 1229:     0..3
 1230: 
 1231: Variables controlling indentation style:
 1232:  `forth-indent-words'
 1233:     List of words that influence indentation.
 1234:  forth-local-indent-words
 1235:     List of words to prepend to `forth-indent-words', similar to 
 1236:     forth-local-words. Should be used for specifying file-specific 
 1237:     indentation, using a local variables list.
 1238:  forth-custom-indent-words
 1239:     List of words to prepend to `forth-indent-words'. Should be set in your
 1240:     .emacs.    
 1241:  forth-indent-level
 1242:     Indentation increment/decrement of Forth statements.
 1243:  forth-minor-indent-level
 1244:     Minor indentation increment/decrement of Forth statemens.
 1245: 
 1246: Variables controlling block-file editing:
 1247:  forth-show-screen
 1248:     Non-nil means, that the start of the current screen is marked by an
 1249:     overlay arrow, and screen numbers are displayed in the mode line.
 1250:     This variable is by default nil for `forth-mode' and t for 
 1251:     `forth-block-mode'.
 1252:  forth-overlay-arrow-string
 1253:     String to display as the overlay arrow, when `forth-show-screen' is t.
 1254:     Setting this variable to nil disables the overlay arrow.
 1255:  forth-block-base
 1256:     Screen number of the first block in a block file. Defaults to 1.
 1257:  forth-warn-long-lines
 1258:     Non-nil means that a warning message is displayed whenever you edit or
 1259:     move over a line that is longer than 64 characters (the maximum line
 1260:     length that can be stored into a block file). This variable defaults to
 1261:     t for `forth-block-mode' and to nil for `forth-mode'.
 1262: 
 1263: Variables controlling interaction with the Forth-process (also see
 1264: `run-forth'):
 1265:   forth-program-name
 1266:     Program invoked by the `run-forth' command (including arguments).
 1267:   inferior-forth-mode-hook
 1268:     Hook for customising inferior-forth-mode.
 1269:   forth-compile-command
 1270:     Default command to execute on `compile'.
 1271: " 
 1272:   (interactive)
 1273:   (kill-all-local-variables)
 1274:   (use-local-map forth-mode-map)
 1275:   (setq mode-name "Forth")
 1276:   (setq major-mode 'forth-mode)
 1277:   (forth-install-motion-hook)
 1278:   ;; convert buffer contents from block file format, if necessary
 1279:   (when (forth-detect-block-file-p)
 1280:     (widen)
 1281:     (message "Converting from Forth block source...")
 1282:     (forth-convert-from-block (point-min) (point-max))
 1283:     (message "Converting from Forth block source...done"))
 1284:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
 1285:   ;; is now stored as normal strem source
 1286:   (when (equal buffer-file-format '(forth-blocks))
 1287:     (setq buffer-file-format nil))
 1288:   (forth-mode-variables)
 1289: ;  (if (not (forth-process-running-p))
 1290: ;      (run-forth forth-program-name))
 1291:   (run-hooks 'forth-mode-hook))
 1292: 
 1293: ;;;###autoload
 1294: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
 1295:   "Major mode for editing Forth block source files, derived from 
 1296: `forth-mode'. Differences to `forth-mode' are:
 1297:  * files are converted to block format, when written (`buffer-file-format' 
 1298:    is set to `(forth-blocks)')
 1299:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
 1300:   
 1301: Note that the length of lines in block files is limited to 64 characters.
 1302: When writing longer lines to a block file, a warning is displayed in the
 1303: echo area and the line is truncated. 
 1304: 
 1305: Another problem is imposed by block files that contain newline or tab 
 1306: characters. When Emacs converts such files back to block file format, 
 1307: it'll translate those characters to a number of spaces. However, when
 1308: you read such a file, a warning message is displayed in the echo area,
 1309: including a line number that may help you to locate and fix the problem.
 1310: 
 1311: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
 1312: bell during block file read/write operations."
 1313:   (setq buffer-file-format '(forth-blocks))
 1314:   (setq forth-show-screen t)
 1315:   (setq forth-warn-long-lines t)
 1316:   (setq forth-screen-number-string (format "%d" forth-block-base))
 1317:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
 1318: 				 '("--S" forth-screen-number-string "-%-"))))
 1319: 
 1320: (add-hook 'forth-mode-hook
 1321:       '(lambda () 
 1322: 	 (make-local-variable 'compile-command)
 1323: 	 (setq compile-command "gforth ")
 1324: 	 (forth-hack-local-variables)
 1325: 	 (forth-customize-words)
 1326: 	 (forth-compile-words)
 1327: 	 (unless (and forth-jit-parser (>= emacs-major-version 21))
 1328: 	   (forth-change-function (point-min) (point-max) nil t))))
 1329: 
 1330: (defun forth-fill-paragraph () 
 1331:   "Fill comments (starting with '\'; do not fill code (block style
 1332: programmers who tend to fill code won't use emacs anyway:-)."
 1333:   ; Currently only comments at the start of the line are filled.
 1334:   ; Something like lisp-fill-paragraph may be better.  We cannot use
 1335:   ; fill-paragraph, because it removes the \ from the first comment
 1336:   ; line. Therefore we have to look for the first line of the comment
 1337:   ; and use fill-region.
 1338:   (interactive)
 1339:   (save-excursion
 1340:     (beginning-of-line)
 1341:     (while (and
 1342: 	     (= (forward-line -1) 0)
 1343: 	     (looking-at "[ \t]*\\\\g?[ \t]+")))
 1344:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
 1345: 	(forward-line 1))
 1346:     (let ((from (point))
 1347: 	  (to (save-excursion (forward-paragraph) (point))))
 1348:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
 1349: 	  (progn (goto-char (match-end 0))
 1350: 		 (set-fill-prefix)
 1351: 		 (fill-region from to nil))))))
 1352: 
 1353: (defun forth-comment-indent ()
 1354:   (save-excursion
 1355:     (beginning-of-line)
 1356:     (if (looking-at ":[ \t]*")
 1357: 	(progn
 1358: 	  (end-of-line)
 1359: 	  (skip-chars-backward " \t\n")
 1360: 	  (1+ (current-column)))
 1361:       comment-column)))
 1362: 
 1363: 
 1364: ;; Forth commands
 1365: 
 1366: (defun forth-remove-tracers ()
 1367:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
 1368:   (interactive)
 1369:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
 1370: 
 1371: (define-key forth-mode-map "\C-x\C-e" 'compile)
 1372: (define-key forth-mode-map "\C-x\C-n" 'next-error)
 1373: (require 'compile)
 1374: 
 1375: (defvar forth-compile-command "gforth ")
 1376: ;(defvar forth-compilation-window-percent-height 30)
 1377: 
 1378: (defun forth-split ()
 1379:   (interactive)
 1380:   (forth-split-1 "*forth*"))
 1381: 
 1382: (defun forth-split-1 (buffer)
 1383:   (if (not (eq (window-buffer) (get-buffer buffer)))
 1384:       (progn
 1385: 	(delete-other-windows)
 1386: 	(split-window-vertically
 1387: 	 (/ (* (screen-height) forth-percent-height) 100))
 1388: 	(other-window 1)
 1389: 	(switch-to-buffer buffer)
 1390: 	(goto-char (point-max))
 1391: 	(other-window 1))))
 1392: 
 1393: (defun forth-compile (command)
 1394:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
 1395:   (forth-split-1 "*compilation*")
 1396:   (setq ctools-compile-command command)
 1397:   (compile1 ctools-compile-command "No more errors"))
 1398: 
 1399: ;;; Forth menu
 1400: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
 1401: 
 1402: ;; (dk) code commented out due to complaints of XEmacs users.  After
 1403: ;; all, there's imenu/speedbar, which uses much smarter scanning
 1404: ;; rules.
 1405: 
 1406: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1407: ;;        (require 'func-menu)
 1408: 
 1409: ;;   (defconst fume-function-name-regexp-forth
 1410: ;;    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
 1411: ;;    "Expression to get word definitions in Forth.")
 1412: 
 1413: ;;   (setq fume-function-name-regexp-alist
 1414: ;;       (append '((forth-mode . fume-function-name-regexp-forth) 
 1415: ;;              ) fume-function-name-regexp-alist))
 1416: 
 1417: ;;   ;; Find next forth word in the buffer
 1418: ;;   (defun fume-find-next-forth-function-name (buffer)
 1419: ;;     "Searches for the next forth word in BUFFER."
 1420: ;;     (set-buffer buffer)
 1421: ;;     (if (re-search-forward fume-function-name-regexp nil t)
 1422: ;;       (let ((beg (match-beginning 2))
 1423: ;;             (end (match-end 2)))
 1424: ;;         (cons (buffer-substring beg end) beg))))
 1425: 
 1426: ;;   (setq fume-find-function-name-method-alist
 1427: ;;   (append '((forth-mode    . fume-find-next-forth-function-name))))
 1428: 
 1429: ;;   ))
 1430: ;;; End Forth menu
 1431: 
 1432: ;;; File folding of forth-files
 1433: ;;; uses outline
 1434: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
 1435: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
 1436: ;;;
 1437: ;;; Works most of the times but loses sync with the cursor occasionally 
 1438: ;;; Could be improved by also folding on comments
 1439: 
 1440: ;; (dk) This code needs a rewrite; just too ugly and doesn't use the
 1441: ;; newer and smarter scanning rules of `imenu'. Who needs it anyway??
 1442: 
 1443: ;; (require 'outline)
 1444: 
 1445: ;; (defun f-outline-level ()
 1446: ;;   (cond	((looking-at "\\`\\\\")
 1447: ;; 	 0)
 1448: ;; 	((looking-at "\\\\ SEC")
 1449: ;; 	 0)
 1450: ;; 	((looking-at "\\\\ \\\\ .*")
 1451: ;; 	 0)
 1452: ;; 	((looking-at "\\\\ DEFS")
 1453: ;; 	 1)
 1454: ;; 	((looking-at "\\/\\* ")
 1455: ;; 	 1)
 1456: ;; 	((looking-at ": .*")
 1457: ;; 	 1)
 1458: ;; 	((looking-at "\\\\G")
 1459: ;; 	 2)
 1460: ;; 	((looking-at "[ \t]+\\\\")
 1461: ;; 	 3)))
 1462:   
 1463: ;; (defun fold-f  ()
 1464: ;;    (interactive)
 1465: ;;    (add-hook 'outline-minor-mode-hook 'hide-body)
 1466: 
 1467: ;;    ; outline mode header start, i.e. find word definitions
 1468: ;; ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
 1469: ;;    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
 1470: ;;    (setq outline-level 'f-outline-level)
 1471: 
 1472: ;;    (outline-minor-mode)
 1473: ;;    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
 1474: ;;    (define-key outline-minor-mode-map '(shift right) 'show-children)
 1475: ;;    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
 1476: ;;    (define-key outline-minor-mode-map '(shift down) 'show-subtree))
 1477: 
 1478: 
 1479: ;;(define-key global-map '(shift up) 'fold-f)
 1480: 
 1481: ;;; end file folding
 1482: 
 1483: ;;; func-menu is a package that scans your source file for function definitions
 1484: ;;; and makes a menubar entry that lets you jump to any particular function
 1485: ;;; definition by selecting it from the menu.  The following code turns this on
 1486: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
 1487: ;;; but not much.
 1488: ;;;
 1489: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1490: ;;        (require 'func-menu)
 1491: ;; ;;       (define-key global-map 'f8 'function-menu)
 1492: ;;        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
 1493: ;; ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
 1494: ;; ;       (define-key global-map '(shift button3) 'mouse-function-menu)
 1495: ;; ))
 1496: 
 1497: ;;;
 1498: ;;; Inferior Forth interpreter 
 1499: ;;;	-- mostly copied from `cmuscheme.el' of Emacs 21.2
 1500: ;;;
 1501: 
 1502: (eval-and-compile (forth-require 'comint))
 1503: 
 1504: (when (memq 'comint features)
 1505: 
 1506:   (defvar forth-program-name "gforth"
 1507:     "*Program invoked by the `run-forth' command, including program arguments")
 1508: 
 1509:   (defcustom inferior-forth-mode-hook nil
 1510:     "*Hook for customising inferior-forth-mode."
 1511:     :type 'hook
 1512:     :group 'forth)
 1513: 
 1514:   (defvar inferior-forth-mode-map
 1515:     (let ((m (make-sparse-keymap)))
 1516:       (define-key m "\r" 'comint-send-input)
 1517:       (define-key m "\M-\C-x" 'forth-send-paragraph-and-go)
 1518:       (define-key m "\C-c\C-l" 'forth-load-file)
 1519:       m))
 1520:   ;; Install the process communication commands in the forth-mode keymap.
 1521:   (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph-and-go)
 1522:   (define-key forth-mode-map "\eo" 'forth-send-buffer-and-go)
 1523: 
 1524:   (define-key forth-mode-map "\M-\C-x" 'forth-send-paragraph-and-go)
 1525:   (define-key forth-mode-map "\C-c\C-r" 'forth-send-region)
 1526:   (define-key forth-mode-map "\C-c\M-r" 'forth-send-region-and-go)
 1527:   (define-key forth-mode-map "\C-c\C-z" 'forth-switch-to-interactive)
 1528:   (define-key forth-mode-map "\C-c\C-l" 'forth-load-file)
 1529: 
 1530:   (defvar forth-process-buffer)
 1531: 
 1532:   (define-derived-mode inferior-forth-mode comint-mode "Inferior Forth"
 1533:     "Major mode for interacting with an inferior Forth process.
 1534: 
 1535: The following commands are available:
 1536: \\{inferior-forth-mode-map}
 1537: 
 1538: A Forth process can be fired up with M-x run-forth.
 1539: 
 1540: Customisation: Entry to this mode runs the hooks on comint-mode-hook and
 1541: inferior-forth-mode-hook (in that order).
 1542: 
 1543: You can send text to the inferior Forth process from other buffers containing
 1544: Forth source.
 1545:     forth-switch-to-interactive switches the current buffer to the Forth
 1546:         process buffer. 
 1547:     forth-send-paragraph sends the current paragraph to the Forth process.
 1548:     forth-send-region sends the current region to the Forth process.
 1549:     forth-send-buffer sends the current buffer to the Forth process.
 1550: 
 1551:     forth-send-paragraph-and-go, forth-send-region-and-go,
 1552:         forth-send-buffer-and-go switch to the Forth process buffer after
 1553:         sending their text.
 1554: For information on running multiple processes in multiple buffers, see
 1555: documentation for variable `forth-process-buffer'.
 1556: 
 1557: Commands:
 1558: Return after the end of the process' output sends the text from the
 1559: end of process to point. If you accidentally suspend your process, use
 1560: \\[comint-continue-subjob] to continue it. "
 1561:     ;; Customise in inferior-forth-mode-hook
 1562:     (setq comint-prompt-regexp "^") 
 1563:     (setq mode-line-process '(":%s")))
 1564: 
 1565:   (defun forth-args-to-list (string)
 1566:     (let ((where (string-match "[ \t]" string)))
 1567:       (cond ((null where) (list string))
 1568: 	    ((not (= where 0))
 1569: 	     (cons (substring string 0 where)
 1570: 		   (forth-args-to-list (substring string (+ 1 where)
 1571: 						  (length string)))))
 1572: 	    (t (let ((pos (string-match "[^ \t]" string)))
 1573: 		 (if (null pos)
 1574: 		     nil
 1575: 		   (forth-args-to-list (substring string pos
 1576: 						  (length string)))))))))
 1577: 
 1578: ;;;###autoload
 1579:   (defun run-forth (cmd)
 1580:     "Run an inferior Forth process, input and output via buffer *forth*.
 1581: If there is a process already running in `*forth*', switch to that buffer.
 1582: With argument, allows you to edit the command line (default is value
 1583: of `forth-program-name').  Runs the hooks `inferior-forth-mode-hook'
 1584: \(after the `comint-mode-hook' is run).
 1585: \(Type \\[describe-mode] in the process buffer for a list of commands.)"
 1586: 
 1587:     (interactive (list (if current-prefix-arg
 1588: 			   (read-string "Run Forth: " forth-program-name)
 1589: 			 forth-program-name)))
 1590:     (if (not (comint-check-proc "*forth*"))
 1591: 	(let ((cmdlist (forth-args-to-list cmd)))
 1592: 	  (set-buffer (apply 'make-comint "forth" (car cmdlist)
 1593: 			     nil (cdr cmdlist)))
 1594: 	  (inferior-forth-mode)))
 1595:     (setq forth-program-name cmd)
 1596:     (setq forth-process-buffer "*forth*")
 1597:     (pop-to-buffer "*forth*"))
 1598: 
 1599:   (defun forth-send-region (start end)
 1600:     "Send the current region to the inferior Forth process."
 1601:     (interactive "r")
 1602:     (comint-send-region (forth-proc) start end)
 1603:     (comint-send-string (forth-proc) "\n"))
 1604: 
 1605:   (defun forth-end-of-paragraph ()
 1606:     (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
 1607:     (if (not (re-search-forward "\n[ \t]*\n" nil t))
 1608: 	(goto-char (point-max))))
 1609: 
 1610:   (defun forth-send-paragraph ()
 1611:     "Send the current or the previous paragraph to the Forth process"
 1612:     (interactive)
 1613:     (let (end)
 1614:       (save-excursion
 1615: 	(forth-end-of-paragraph)
 1616: 	(skip-chars-backward  "\t\n ")
 1617: 	(setq end (point))
 1618: 	(if (re-search-backward "\n[ \t]*\n" nil t)
 1619: 	    (setq start (point))
 1620: 	  (goto-char (point-min)))
 1621: 	(skip-chars-forward  "\t\n ")
 1622: 	(forth-send-region (point) end))))
 1623: 
 1624:   (defun forth-send-paragraph-and-go ()
 1625:     "Send the current or the previous paragraph to the Forth process.
 1626: Then switch to the process buffer."
 1627:     (interactive)
 1628:     (forth-send-paragraph)
 1629:     (forth-switch-to-interactive t))
 1630: 
 1631:   (defun forth-send-buffer ()
 1632:     "Send the current buffer to the Forth process."
 1633:     (interactive)
 1634:     (if (eq (current-buffer) forth-process-buffer)
 1635: 	(error "Not allowed to send this buffer's contents to Forth"))
 1636:     (forth-send-region (point-min) (point-max)))
 1637: 
 1638:   (defun forth-send-buffer-and-go ()
 1639:     "Send the current buffer to the Forth process.
 1640: Then switch to the process buffer."
 1641:     (interactive)
 1642:     (forth-send-buffer)
 1643:     (forth-switch-to-interactive t))
 1644: 
 1645: 
 1646:   (defun forth-switch-to-interactive (eob-p)
 1647:     "Switch to the Forth process buffer.
 1648: With argument, position cursor at end of buffer."
 1649:     (interactive "P")
 1650:     (if (get-buffer forth-process-buffer)
 1651: 	(pop-to-buffer forth-process-buffer)
 1652:       (error "No current process buffer.  See variable `forth-process-buffer'"))
 1653:     (cond (eob-p
 1654: 	   (push-mark)
 1655: 	   (goto-char (point-max)))))
 1656: 
 1657:   (defun forth-send-region-and-go (start end)
 1658:     "Send the current region to the inferior Forth process.
 1659: Then switch to the process buffer."
 1660:     (interactive "r")
 1661:     (forth-send-region start end)
 1662:     (forth-switch-to-interactive t))
 1663: 
 1664:   (defcustom forth-source-modes '(forth-mode forth-block-mode)
 1665:     "*Used to determine if a buffer contains Forth source code.
 1666: If it's loaded into a buffer that is in one of these major modes, it's
 1667: considered a Forth source file by `forth-load-file' and `forth-compile-file'.
 1668: Used by these commands to determine defaults."
 1669:     :type '(repeat function)
 1670:     :group 'forth)
 1671: 
 1672:   (defvar forth-prev-l/c-dir/file nil
 1673:     "Caches the last (directory . file) pair.
 1674: Caches the last pair used in the last `forth-load-file' or
 1675: `forth-compile-file' command. Used for determining the default in the
 1676: next one.")
 1677: 
 1678:   (defun forth-load-file (file-name)
 1679:     "Load a Forth file FILE-NAME into the inferior Forth process."
 1680:     (interactive (comint-get-source "Load Forth file: " forth-prev-l/c-dir/file
 1681: 				    forth-source-modes t)) ; T because LOAD
 1682: 					; needs an exact name
 1683:     (comint-check-source file-name) ; Check to see if buffer needs saved.
 1684:     (setq forth-prev-l/c-dir/file (cons (file-name-directory file-name)
 1685: 					(file-name-nondirectory file-name)))
 1686:     (comint-send-string (forth-proc)
 1687: 			(concat "s\" " file-name "\" included\n")))
 1688: 
 1689:   
 1690:   (defvar forth-process-buffer nil "*The current Forth process buffer.
 1691: 
 1692: See `scheme-buffer' for an explanation on how to run multiple Forth 
 1693: processes.")
 1694: 
 1695:   (defun forth-proc ()
 1696:     "Return the current Forth process.  See variable `forth-process-buffer'."
 1697:     (let ((proc (get-buffer-process (if (eq major-mode 'inferior-forth-mode)
 1698: 					(current-buffer)
 1699: 				      forth-process-buffer))))
 1700:       (or proc
 1701: 	  (error "No current process.  See variable `forth-process-buffer'"))))
 1702:   )  ; (memq 'comint features)
 1703: 
 1704: (provide 'forth-mode)
 1705: 
 1706: ;;; gforth.el ends here

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>