File:  [gforth] / gforth / gforth.el
Revision 1.59: download - view: text, annotated - select for diffs
Sat Jan 26 13:05:26 2002 UTC (19 years, 6 months ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Fixed everything that prevented `gforth.el' from working with XEmacs
21.1. Fixed info-lookup.

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

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