File:  [gforth] / gforth / gforth.el
Revision 1.77: download - view: text, annotated - select for diffs
Sat Apr 10 13:03:01 2010 UTC (11 years, 1 month ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Properly indent after 'abi-code'.   Highlight code, abi-code, end-code.
Highlight gforth-specific constant syntax $123 %0101 etc. (for
forth-hilight-level >= 3)

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

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