Annotation of gforth/gforth.el, revision 1.78

1.48      pazsan      1: ;;; gforth.el --- major mode for editing (G)Forth sources
1.31      anton       2: 
1.76      anton       3: ;; Copyright (C) 1995,1996,1997,1998,2000,2001,2003,2004,2007,2008 Free Software Foundation, Inc.
1.31      anton       4: 
                      5: ;; This file is part of Gforth.
1.1       anton       6: 
1.17      anton       7: ;; GForth is distributed in the hope that it will be useful,
1.1       anton       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
1.17      anton      17: ;; supposed to have been given to you along with Gforth so you
1.1       anton      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.
1.31      anton      21: 
1.48      pazsan     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: 
1.31      anton      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.
1.1       anton      32: 
1.48      pazsan     33: ;; Changes by David
                     34: ;; Added a syntax-hilighting engine, rewrote auto-indentation engine.
                     35: ;; Added support for block files.
1.66      dvdkhlng   36: ;; Replaced forth-process code with comint-based implementation.
                     37: 
                     38: ;; Tested with Emacs 19.34, 20.5, 21 and XEmacs 21
1.48      pazsan     39:  
1.1       anton      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: 
1.48      pazsan     53: ;;; Code:
                     54: 
1.59      dvdkhlng   55: ;(setq debug-on-error t)
1.58      dvdkhlng   56: 
1.55      dvdkhlng   57: ;; Code ripped from `version.el' for compatability with Emacs versions
                     58: ;; prior to 19.23.
1.58      dvdkhlng   59: (if (not (boundp 'emacs-major-version))
                     60:     (defconst emacs-major-version
                     61:       (progn (string-match "^[0-9]+" emacs-version)
1.75      pazsan     62:             (string-to-number (match-string 0 emacs-version)))))
1.58      dvdkhlng   63: 
1.73      anton      64: ;; Code ripped from `subr.el' for compatability with Emacs versions
                     65: ;; prior to 20.1
                     66: (eval-when-compile 
1.58      dvdkhlng   67: (defun forth-emacs-older (major minor)
                     68:   (or (< emacs-major-version major)
                     69:       (and (= emacs-major-version major) (< emacs-minor-version minor))))
1.55      dvdkhlng   70: 
1.58      dvdkhlng   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 :-(
1.73      anton      82: (eval-and-compile
1.58      dvdkhlng   83: (defun forth-require (feature)
1.73      anton      84:     (condition-case err (require feature) (error nil))))
1.58      dvdkhlng   85: 
                     86: (require 'font-lock)
1.48      pazsan     87: 
1.58      dvdkhlng   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: 
1.59      dvdkhlng   97: ;; define `font-lock-constant-face' in XEmacs (just copy
                     98: ;; `font-lock-preprocessor-face')
                     99: (unless (boundp 'font-lock-constant-face)
1.61      dvdkhlng  100:   (copy-face font-lock-preprocessor-face 'font-lock-constant-face))
                    101: 
1.59      dvdkhlng  102: 
1.58      dvdkhlng  103: ;; define `regexp-opt' in emacs versions prior to 20.1 
                    104: ;; (this implementation is extremely inefficient, though)
1.59      dvdkhlng  105: (eval-and-compile (forth-require 'regexp-opt))
                    106: (unless (memq 'regexp-opt features)
1.58      dvdkhlng  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: 
1.57      dvdkhlng  116: ; todo:
                    117: ;
                    118: 
1.69      dvdkhlng  119: ; screen-height existiert nicht in XEmacs, frame-height ersetzen?
                    120: ; 
                    121: 
1.57      dvdkhlng  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: 
1.62      dvdkhlng  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)
1.48      pazsan    178: ;;;
1.55      dvdkhlng  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: 
1.48      pazsan    194: (defvar forth-words nil 
                    195:   "List of words for hilighting and recognition of parsed text areas. 
1.55      dvdkhlng  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.
1.48      pazsan    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))
1.77      dvdkhlng  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))
1.48      pazsan    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" 
1.60      anton     279:          "case" "of" "?dup-if" "?dup-0=-if" "then" "endif" "until"
1.64      pazsan    280:          "repeat" "again" "leave" "?leave"
1.48      pazsan    281:          "loop" "+loop" "-loop" "next" "endcase" "endof" "else" "while" "try"
1.72      anton     282:          "recover" "endtry" "iferror" "restore" "endtry-iferror"
                    283:          "assert(" "assert0(" "assert1(" "assert2("
1.48      pazsan    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))
1.56      dvdkhlng  290:        (("~~" "break:" "dbg") compile-only (font-lock-warning-face . 2))
1.55      dvdkhlng  291:        (("break\"") compile-only (font-lock-warning-face . 1)
                    292:         "[\"\n]" nil string (font-lock-string-face . 1))
1.48      pazsan    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))
1.56      dvdkhlng  298:        (("<is>" "'" "see") non-immediate (font-lock-keyword-face . 2)
1.48      pazsan    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))
1.54      dvdkhlng  312:        ("\\S-+%" non-immediate (font-lock-type-face . 2))
1.48      pazsan    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))
1.77      dvdkhlng  321:        ("-?\\([&#][0-9]+\\|\\(0x\\|\\$\\)[0-9a-f]+\\|%[01]+\\)"
                    322:         immediate (font-lock-constant-face . 3))
1.48      pazsan    323:        ))
                    324: 
1.51      dvdkhlng  325: (defvar forth-use-objects nil 
                    326:   "*Non-nil makes forth-mode also hilight words from the \"Objects\" package.")
1.57      dvdkhlng  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)))
1.51      dvdkhlng  356:   "Hilighting description for words of the \"Objects\" package")
1.57      dvdkhlng  357: 
1.48      pazsan    358: 
1.51      dvdkhlng  359: (defvar forth-use-oof nil 
                    360:   "*Non-nil makes forth-mode also hilight words from the \"OOF\" package.")
1.57      dvdkhlng  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)))
1.51      dvdkhlng  378:   "Hilighting description for words of the \"OOF\" package")
1.48      pazsan    379: 
1.49      dvdkhlng  380: (defvar forth-local-words nil 
                    381:   "List of Forth words to prepend to `forth-words'. Should be set by a 
1.51      dvdkhlng  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.")
1.49      dvdkhlng  388: 
1.48      pazsan    389: (defvar forth-hilight-level 3 "*Level of hilighting of Forth code.")
1.51      dvdkhlng  390: 
1.48      pazsan    391: (defvar forth-compiled-words nil "Compiled representation of `forth-words'.")
                    392: 
1.57      dvdkhlng  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: 
1.63      anton     407: INDENT1 specifies how to indent a word that's located at the beginning
                    408:    of a line, following any number of whitespaces.
1.57      dvdkhlng  409: 
1.63      anton     410: INDENT2 specifies how to indent words that are not located at the
                    411:    beginning of a line.
1.57      dvdkhlng  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"
1.72      anton     427:          "u-do" "?dup-if" "?dup-0=-if" "case" "of" "try" "iferror"
1.57      dvdkhlng  428:          "[if]" "[ifdef]" "[ifundef]" "[begin]" "[for]" "[do]" "[?do]")
                    429:         (0 . 2) (0 . 2))
1.77      dvdkhlng  430:        ((":" ":noname" "code" "abi-code" "struct" "m:" ":m" "class" 
                    431:          "interface")
1.57      dvdkhlng  432:         (0 . 2) (0 . 2) non-immediate)
                    433:        ("\\S-+%$" (0 . 2) (0 . 0) non-immediate)
                    434:        ((";" ";m") (-2 . 0) (0 . -2))
1.63      anton     435:        (("again" "then" "endif" "endtry" "endcase" "endof" 
1.57      dvdkhlng  436:          "[then]" "[endif]" "[loop]" "[+loop]" "[next]" 
1.63      anton     437:          "[until]" "[again]" "loop")
1.57      dvdkhlng  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))
1.72      anton     444:        (("else" "recover" "restore" "endtry-iferror" "[else]")
                    445:         (-2 . 2) (0 . 0))
1.63      anton     446:        (("does>") (-1 . 1) (0 . 0))
                    447:        (("while" "[while]") (-2 . 4) (0 . 2))
1.70      dvdkhlng  448:        (("repeat" "[repeat]") (-4 . 0) (0 . -4))))
1.57      dvdkhlng  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).")
1.51      dvdkhlng  455: 
1.57      dvdkhlng  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.")
1.48      pazsan    459: 
1.57      dvdkhlng  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)
1.48      pazsan    465: 
1.55      dvdkhlng  466: ;(setq debug-on-error t)
1.48      pazsan    467: 
1.50      dvdkhlng  468: ;; Filter list by predicate. This is a somewhat standard function for 
1.48      pazsan    469: ;; functional programming languages. So why isn't it already implemented 
                    470: ;; in Lisp??
1.50      dvdkhlng  471: (defun forth-filter (predicate list)
1.48      pazsan    472:   (let ((filtered nil))
1.75      pazsan    473:     (dolist (item list)
1.50      dvdkhlng  474:              (when (funcall predicate item)
1.48      pazsan    475:                (if filtered
                    476:                    (nconc filtered (list item))
1.75      pazsan    477:           (setq filtered (cons item nil)))))
1.48      pazsan    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)
1.59      dvdkhlng  492:   ;; warning: we cannot rely on regexp-opt's PAREN argument, since
                    493:   ;; XEmacs will use shy parens by default :-(
1.48      pazsan    494:   (let* ((matcher (car word))
1.59      dvdkhlng  495:         (regexp 
                    496:          (concat "\\(" (cond ((stringp matcher) matcher)
                    497:                              ((listp matcher) (regexp-opt matcher))
1.75      pazsan    498:                              (t (error "Invalid matcher")))
1.59      dvdkhlng  499:                  "\\)"))
1.48      pazsan    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: ;;  ...)
1.49      dvdkhlng  508: (defun forth-compile-wordlist (words)
1.48      pazsan    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: 
1.49      dvdkhlng  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.
1.51      dvdkhlng  528:  Store the resulting compiled wordlists in `forth-compiled-words' and 
1.49      dvdkhlng  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 ()
1.51      dvdkhlng  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'."
1.78    ! dvdkhlng  540:   (put 'forth-local-indent-words 'safe-local-variable 'listp)
        !           541:   (put 'forth-local-words 'safe-local-variable 'listp)
1.49      dvdkhlng  542:   (hack-local-variables)
                    543:   (setq forth-words (append forth-local-words forth-words))
                    544:   (setq forth-indent-words (append forth-local-indent-words 
                    545:                                   forth-indent-words)))
                    546: 
1.51      dvdkhlng  547: (defun forth-customize-words ()
                    548:   "Add the words from `forth-custom-words' and `forth-custom-indent-words'
                    549:  to `forth-words' and `forth-indent-words', respective. Add 
                    550:  `forth-objects-words' and/or `forth-oof-words' to `forth-words', if
                    551:  `forth-use-objects' and/or `forth-use-oof', respective is set."
                    552:   (setq forth-words (append forth-custom-words forth-words
                    553:                            (if forth-use-oof forth-oof-words nil)
                    554:                            (if forth-use-objects forth-objects-words nil)))
                    555:   (setq forth-indent-words (append 
                    556:                            forth-custom-indent-words forth-indent-words)))
                    557: 
                    558: 
                    559: 
1.48      pazsan    560: ;; get location of first character of previous forth word that's got 
                    561: ;; properties
                    562: (defun forth-previous-start (pos)
                    563:   (let* ((word (get-text-property pos 'forth-word))
                    564:         (prev (previous-single-property-change 
                    565:                (min (point-max) (1+ pos)) 'forth-word 
                    566:                (current-buffer) (point-min))))
                    567:     (if (or (= (point-min) prev) word) prev
                    568:       (if (get-text-property (1- prev) 'forth-word)
                    569:          (previous-single-property-change 
                    570:           prev 'forth-word (current-buffer) (point-min))
                    571:        (point-min)))))
                    572: 
                    573: ;; Get location of the last character of the current/next forth word that's
                    574: ;; got properties, text that's parsed by the word is considered as parts of 
                    575: ;; the word.
                    576: (defun forth-next-end (pos)
                    577:   (let* ((word (get-text-property pos 'forth-word))
                    578:         (next (next-single-property-change pos 'forth-word 
                    579:                                            (current-buffer) (point-max))))
                    580:     (if word next
                    581:       (if (get-text-property next 'forth-word)
                    582:          (next-single-property-change 
                    583:           next 'forth-word (current-buffer) (point-max))
                    584:        (point-max)))))
                    585: 
                    586: (defun forth-next-whitespace (pos)
                    587:   (save-excursion
                    588:     (goto-char pos)
                    589:     (skip-syntax-forward "-" (point-max))
                    590:     (point)))
                    591: (defun forth-previous-word (pos)
                    592:   (save-excursion
                    593:     (goto-char pos)
                    594:     (re-search-backward "\\<" pos (point-min) 1)
                    595:     (point)))
                    596: 
                    597: ;; Delete all properties, used by Forth mode, from `from' to `to'.
                    598: (defun forth-delete-properties (from to)
                    599:   (remove-text-properties 
1.55      dvdkhlng  600:    from to '(face nil fontified nil 
                    601:                  forth-parsed nil forth-word nil forth-state nil)))
1.48      pazsan    602: 
                    603: ;; Get the index of the branch of the most recently evaluated regular 
                    604: ;; expression that matched. (used for identifying branches "a\\|b\\|c...")
                    605: (defun forth-get-regexp-branch ()
                    606:   (let ((count 2))
1.59      dvdkhlng  607:     (while (not (condition-case err (match-beginning count)
                    608:                  (args-out-of-range t)))  ; XEmacs requires error handling
1.48      pazsan    609:       (setq count (1+ count)))
                    610:     count))
                    611: 
                    612: ;; seek to next forth-word and return its "word-description"
                    613: (defun forth-next-known-forth-word (to)
                    614:   (if (<= (point) to)
                    615:       (progn
                    616:        (let* ((regexp (car forth-compiled-words))
                    617:               (pos (re-search-forward regexp to t)))
                    618:          (if pos (let ((branch (forth-get-regexp-branch))
                    619:                        (descr (cdr forth-compiled-words)))
                    620:                    (goto-char (match-beginning 0))
                    621:                    (cdr (assoc branch descr)))
                    622:            'nil)))
                    623:     nil))
                    624: 
                    625: ;; Set properties of forth word at `point', eventually parsing subsequent 
                    626: ;; words, and parsing all whitespaces. Set point to delimiter after word.
                    627: ;; The word, including it's parsed text gets the `forth-word' property, whose 
                    628: ;; value is unique, and may be used for getting the word's start/end 
                    629: ;; positions.
                    630: (defun forth-set-word-properties (state data)
                    631:   (let* ((start (point))
                    632:         (end (progn (re-search-forward "[ \t]\\|$" (point-max) 1)
                    633:                     (point)))
                    634:         (type (car data))
                    635:         (hilight (nth 1 data))
                    636:         (bad-word (and (not state) (eq type 'compile-only)))
                    637:         (hlface (if bad-word font-lock-warning-face
                    638:                   (if (<= (cdr hilight) forth-hilight-level)
                    639:                       (car hilight) nil))))
                    640:     (when hlface (put-text-property start end 'face hlface))
                    641:     ;; if word parses in current state, process parsed range of text
                    642:     (when (or (not state) (eq type 'compile-only) (eq type 'immediate))
                    643:       (let ((parse-data (nthcdr 2 data)))
                    644:        (while parse-data
                    645:          (let ((delim (nth 0 parse-data))
                    646:                (skip-leading (nth 1 parse-data))
                    647:                (parse-type (nth 2 parse-data))
                    648:                (parsed-hilight (nth 3 parse-data))
                    649:                (parse-start (point))
                    650:                (parse-end))
                    651:            (when skip-leading
                    652:              (while (and (looking-at delim) (> (match-end 0) (point))
                    653:                          (not (looking-at "\n")))
                    654:                (forward-char)))
                    655:            (re-search-forward delim (point-max) 1)
                    656:            (setq parse-end (point))
                    657:            (forth-delete-properties end parse-end)
                    658:            (when (<= (cdr parsed-hilight) forth-hilight-level)
                    659:              (put-text-property 
                    660:               parse-start parse-end 'face (car parsed-hilight)))
                    661:            (put-text-property 
                    662:             parse-start parse-end 'forth-parsed parse-type)
                    663:            (setq end parse-end)
                    664:            (setq parse-data (nthcdr 4 parse-data))))))
                    665:     (put-text-property start end 'forth-word start)))
                    666: 
                    667: ;; Search for known Forth words in the range `from' to `to', using 
                    668: ;; `forth-next-known-forth-word' and set their properties via 
                    669: ;; `forth-set-word-properties'.
1.51      dvdkhlng  670: (defun forth-update-properties (from to &optional loudly)
1.48      pazsan    671:   (save-excursion
1.51      dvdkhlng  672:     (let ((msg-count 0) (state) (word-descr) (last-location))
1.48      pazsan    673:       (goto-char (forth-previous-word (forth-previous-start 
                    674:                                       (max (point-min) (1- from)))))
                    675:       (setq to (forth-next-end (min (point-max) (1+ to))))
                    676:       ;; `to' must be on a space delimiter, if a parsing word was changed
                    677:       (setq to (forth-next-whitespace to))
                    678:       (setq state (get-text-property (point) 'forth-state))
                    679:       (setq last-location (point))
                    680:       (forth-delete-properties (point) to)
1.55      dvdkhlng  681:       (put-text-property (point) to 'fontified t)
1.48      pazsan    682:       ;; hilight loop...
                    683:       (while (setq word-descr (forth-next-known-forth-word to))
1.51      dvdkhlng  684:        (when loudly
                    685:          (when (equal 0 (% msg-count 100))
                    686:            (message "Parsing Forth code...%s"
                    687:                     (make-string (/ msg-count 100) ?.)))
                    688:          (setq msg-count (1+ msg-count)))
1.48      pazsan    689:        (forth-set-word-properties state word-descr)
                    690:        (when state (put-text-property last-location (point) 'forth-state t))
                    691:        (let ((type (car word-descr)))
                    692:          (if (eq type 'definition-starter) (setq state t))
                    693:          (if (eq type 'definition-ender) (setq state nil))
                    694:          (setq last-location (point))))
                    695:       ;; update state property up to `to'
                    696:       (if (and state (< (point) to))
                    697:          (put-text-property last-location to 'forth-state t))
                    698:       ;; extend search if following state properties differ from current state
                    699:       (if (< to (point-max))
                    700:          (if (not (equal state (get-text-property (1+ to) 'forth-state)))
                    701:              (let ((extend-to (next-single-property-change 
                    702:                                to 'forth-state (current-buffer) (point-max))))
                    703:                (forth-update-properties to extend-to))
                    704:            ))
                    705:       )))
                    706: 
                    707: ;; save-buffer-state borrowed from `font-lock.el'
                    708: (eval-when-compile 
                    709:   (defmacro forth-save-buffer-state (varlist &rest body)
                    710:     "Bind variables according to VARLIST and eval BODY restoring buffer state."
1.75      pazsan    711:     `(let* (,@(append varlist
1.48      pazsan    712:                   '((modified (buffer-modified-p)) (buffer-undo-list t)
                    713:                     (inhibit-read-only t) (inhibit-point-motion-hooks t)
                    714:                     before-change-functions after-change-functions
1.75      pazsan    715:                         deactivate-mark buffer-file-name buffer-file-truename)))
                    716:        ,@body
1.48      pazsan    717:         (when (and (not modified) (buffer-modified-p))
1.75      pazsan    718:          (set-buffer-modified-p nil)))))
1.48      pazsan    719: 
                    720: ;; Function that is added to the `change-functions' hook. Calls 
                    721: ;; `forth-update-properties' and keeps care of disabling undo information
                    722: ;; and stuff like that.
1.51      dvdkhlng  723: (defun forth-change-function (from to len &optional loudly)
1.48      pazsan    724:   (save-match-data
1.55      dvdkhlng  725:     (forth-save-buffer-state 
                    726:      () 
                    727:      (unless forth-disable-parser (forth-update-properties from to loudly))
                    728:      (forth-update-warn-long-lines))))
                    729: 
                    730: (defun forth-fontification-function (from)
                    731:   "Function to be called from `fontification-functions' of Emacs 21."
                    732:   (save-match-data
                    733:     (forth-save-buffer-state
                    734:      ((to (min (point-max) (+ from 100))))
                    735:      (unless (or forth-disable-parser (not forth-jit-parser)
                    736:                 (get-text-property from 'fontified))
                    737:        (forth-update-properties from to)))))
1.48      pazsan    738: 
                    739: (eval-when-compile
                    740:   (byte-compile 'forth-set-word-properties)
                    741:   (byte-compile 'forth-next-known-forth-word)
                    742:   (byte-compile 'forth-update-properties)
                    743:   (byte-compile 'forth-delete-properties)
                    744:   (byte-compile 'forth-get-regexp-branch)) 
                    745: 
1.51      dvdkhlng  746: ;;; imenu support
                    747: ;;;
1.52      dvdkhlng  748: (defvar forth-defining-words 
                    749:   '("VARIABLE" "CONSTANT" "2VARIABLE" "2CONSTANT" "FVARIABLE" "FCONSTANT"
                    750:    "USER" "VALUE" "field" "end-struct" "VOCABULARY" "CREATE" ":" "CODE"
                    751:    "DEFER" "ALIAS")
1.53      dvdkhlng  752:   "List of words, that define the following word.
1.55      dvdkhlng  753: Used for imenu index generation.")
1.52      dvdkhlng  754: 
1.57      dvdkhlng  755: (defvar forth-defining-words-regexp nil 
                    756:   "Regexp that's generated for matching `forth-defining-words'")
1.52      dvdkhlng  757:  
1.51      dvdkhlng  758: (defun forth-next-definition-starter ()
                    759:   (progn
1.52      dvdkhlng  760:     (let* ((pos (re-search-forward forth-defining-words-regexp (point-max) t)))
1.51      dvdkhlng  761:       (if pos
                    762:          (if (or (text-property-not-all (match-beginning 0) (match-end 0) 
1.52      dvdkhlng  763:                                         'forth-parsed nil)
                    764:                  (text-property-not-all (match-beginning 0) (match-end 0)
                    765:                                         'forth-state nil)) 
1.51      dvdkhlng  766:              (forth-next-definition-starter)
                    767:            t)
                    768:        nil))))
                    769: 
                    770: (defun forth-create-index ()
1.52      dvdkhlng  771:   (let* ((forth-defining-words-regexp 
                    772:          (concat "\\<\\(" (regexp-opt forth-defining-words) "\\)\\>"))
1.51      dvdkhlng  773:         (index nil))
                    774:     (goto-char (point-min))
                    775:     (while (forth-next-definition-starter)
                    776:       (if (looking-at "[ \t]*\\([^ \t\n]+\\)")
                    777:          (setq index (cons (cons (match-string 1) (point)) index))))
                    778:     index))
                    779: 
1.57      dvdkhlng  780: ;; top-level require is executed at byte-compile and load time
1.58      dvdkhlng  781: (eval-and-compile (forth-require 'speedbar))
1.57      dvdkhlng  782: 
                    783: ;; this code is executed at load-time only
1.58      dvdkhlng  784: (when (memq 'speedbar features)
1.57      dvdkhlng  785:   (speedbar-add-supported-extension ".fs")
                    786:   (speedbar-add-supported-extension ".fb"))
1.51      dvdkhlng  787: 
1.48      pazsan    788: ;; (require 'profile)
                    789: ;; (setq profile-functions-list '(forth-set-word-properties forth-next-known-forth-word forth-update-properties forth-delete-properties forth-get-regexp-branch))
                    790: 
                    791: ;;; Indentation
                    792: ;;;
                    793: 
                    794: ;; Return, whether `pos' is the first forth word on its line
                    795: (defun forth-first-word-on-line-p (pos)
                    796:   (save-excursion
                    797:     (beginning-of-line)
                    798:     (skip-chars-forward " \t")
                    799:     (= pos (point))))
                    800: 
                    801: ;; Return indentation data (SELF-INDENT . NEXT-INDENT) of next known 
                    802: ;; indentation word, or nil if there is no word up to `to'. 
                    803: ;; Position `point' at location just after found word, or at `to'. Parsed 
                    804: ;; ranges of text will not be taken into consideration!
                    805: (defun forth-next-known-indent-word (to)
                    806:   (if (<= (point) to)
                    807:       (progn
                    808:        (let* ((regexp (car forth-compiled-indent-words))
                    809:               (pos (re-search-forward regexp to t)))
                    810:          (if pos
1.54      dvdkhlng  811:              (let* ((start (match-beginning 0))
                    812:                     (end (match-end 0))
                    813:                     (branch (forth-get-regexp-branch))
                    814:                     (descr (cdr forth-compiled-indent-words))
                    815:                     (indent (cdr (assoc branch descr)))
                    816:                     (type (nth 2 indent)))
                    817:                ;; skip words that are parsed (strings/comments) and 
                    818:                ;; non-immediate words inside definitions
                    819:                (if (or (text-property-not-all start end 'forth-parsed nil)
                    820:                        (and (eq type 'non-immediate) 
                    821:                             (text-property-not-all start end 
                    822:                                                    'forth-state nil)))
                    823:                    (forth-next-known-indent-word to)
1.48      pazsan    824:                  (if (forth-first-word-on-line-p (match-beginning 0))
                    825:                      (nth 0 indent) (nth 1 indent))))
                    826:            nil)))
                    827:     nil))
                    828:   
                    829: ;; Translate indentation value `indent' to indentation column. Multiples of
                    830: ;; 2 correspond to multiples of `forth-indent-level'. Odd numbers get an
                    831: ;; additional `forth-minor-indent-level' added (or substracted).
                    832: (defun forth-convert-to-column (indent)
                    833:   (let* ((sign (if (< indent 0) -1 1))
                    834:         (value (abs indent))
                    835:         (major (* (/ value 2) forth-indent-level))
                    836:         (minor (* (% value 2) forth-minor-indent-level)))
                    837:     (* sign (+ major minor))))
                    838: 
                    839: ;; Return the column increment, that the current line of forth code does to
                    840: ;; the current or following lines. `which' specifies which indentation values
1.70      dvdkhlng  841: ;; to use. 1 means the indentation of following lines relative to current 
                    842: ;; line, 0 means the indentation of the current line relative to the previous 
1.48      pazsan    843: ;; line. Return `nil', if there are no indentation words on the current line.
                    844: (defun forth-get-column-incr (which)
                    845:   (save-excursion
                    846:     (let ((regexp (car forth-compiled-indent-words))
                    847:          (word-indent)
                    848:          (self-indent nil)
                    849:          (next-indent nil)
                    850:          (to (save-excursion (end-of-line) (point))))
                    851:       (beginning-of-line)
                    852:       (while (setq word-indent (forth-next-known-indent-word to))
                    853:        (let* ((self-incr (car word-indent))
                    854:               (next-incr (cdr word-indent))
                    855:               (self-column-incr (forth-convert-to-column self-incr))
                    856:               (next-column-incr (forth-convert-to-column next-incr)))
                    857:          (setq next-indent (if next-indent next-indent 0))
                    858:          (setq self-indent (if self-indent self-indent 0))
                    859:          (if (or (and (> next-indent 0) (< self-column-incr 0))
                    860:                  (and (< next-indent 0) (> self-column-incr 0)))
                    861:              (setq next-indent (+ next-indent self-column-incr))
                    862:            (setq self-indent (+ self-indent self-column-incr)))
                    863:          (setq next-indent (+ next-indent next-column-incr))))
                    864:       (nth which (list self-indent next-indent)))))
                    865: 
                    866: ;; Find previous line that contains indentation words, return the column,
                    867: ;; to which following text should be indented to.
                    868: (defun forth-get-anchor-column ()
                    869:   (save-excursion
                    870:     (if (/= 0 (forward-line -1)) 0
1.54      dvdkhlng  871:       (let ((indent))
1.48      pazsan    872:        (while (not (or (setq indent (forth-get-column-incr 1))
                    873:                        (<= (point) (point-min))))
                    874:          (forward-line -1))
                    875:        (+ (current-indentation) (if indent indent 0))))))
                    876: 
                    877: (defun forth-indent-line (&optional flag)
                    878:   "Correct indentation of the current Forth line."
                    879:   (let* ((anchor (forth-get-anchor-column))
                    880:         (column-incr (forth-get-column-incr 0)))
                    881:     (forth-indent-to (if column-incr (+ anchor column-incr) anchor))))
                    882: 
1.49      dvdkhlng  883: (defun forth-current-column ()
                    884:   (- (point) (save-excursion (beginning-of-line) (point))))
                    885: (defun forth-current-indentation ()
                    886:   (- (save-excursion (beginning-of-line) (forward-to-indentation 0) (point))
                    887:      (save-excursion (beginning-of-line) (point))))
                    888: 
1.48      pazsan    889: (defun forth-indent-to (x)
                    890:   (let ((p nil))
1.49      dvdkhlng  891:     (setq p (- (forth-current-column) (forth-current-indentation)))
1.48      pazsan    892:     (forth-delete-indentation)
                    893:     (beginning-of-line)
                    894:     (indent-to x)
                    895:     (if (> p 0) (forward-char p))))
                    896: 
                    897: (defun forth-delete-indentation ()
                    898:   (save-excursion
                    899:     (delete-region 
                    900:      (progn (beginning-of-line) (point)) 
                    901:      (progn (back-to-indentation) (point)))))
                    902: 
                    903: (defun forth-indent-command ()
                    904:   (interactive)
                    905:   (forth-indent-line t))
                    906: 
                    907: ;; remove trailing whitespaces in current line
                    908: (defun forth-remove-trailing ()
                    909:   (save-excursion
                    910:     (end-of-line)
                    911:     (delete-region (point) (progn (skip-chars-backward " \t") (point)))))
                    912: 
                    913: ;; insert newline, removing any trailing whitespaces in the current line
                    914: (defun forth-newline-remove-trailing ()
                    915:   (save-excursion
1.49      dvdkhlng  916:     (delete-region (point) (progn (skip-chars-backward " \t") (point))))
                    917:   (newline))
                    918: ;  (let ((was-point (point-marker)))
                    919: ;    (unwind-protect 
                    920: ;      (progn (forward-line -1) (forth-remove-trailing))
                    921: ;      (goto-char (was-point)))))
1.48      pazsan    922: 
                    923: ;; workaround for bug in `reindent-then-newline-and-indent'
                    924: (defun forth-reindent-then-newline-and-indent ()
                    925:   (interactive "*")
                    926:   (indent-according-to-mode)
                    927:   (forth-newline-remove-trailing)
                    928:   (indent-according-to-mode))
                    929: 
                    930: 
                    931: ;;; Block file encoding/decoding  (dk)
                    932: ;;;
                    933: 
                    934: (defconst forth-c/l 64 "Number of characters per block line")
                    935: (defconst forth-l/b 16 "Number of lines per block")
                    936: 
                    937: ;; Check whether the unconverted block file line, point is in, does not
                    938: ;; contain `\n' and `\t' characters.
                    939: (defun forth-check-block-line (line)
                    940:   (let ((end (save-excursion (beginning-of-line) (forward-char forth-c/l)
                    941:                             (point))))
                    942:     (save-excursion 
                    943:       (beginning-of-line)
                    944:       (when (search-forward "\n" end t)
                    945:        (message "Warning: line %i contains newline character #10" line)
                    946:        (ding t))
                    947:       (beginning-of-line)
                    948:       (when (search-forward "\t" end t)
                    949:        (message "Warning: line %i contains tab character #8" line)
                    950:        (ding t)))))
                    951: 
                    952: (defun forth-convert-from-block (from to)
                    953:   "Convert block file format to stream source in current buffer."
                    954:   (let ((line (count-lines (point-min) from)))
                    955:     (save-excursion
                    956:       (goto-char from)
                    957:       (set-mark to)
                    958:       (while (< (+ (point) forth-c/l) (mark t))
                    959:        (setq line (1+ line))
                    960:        (forth-check-block-line line)
                    961:        (forward-char forth-c/l)
                    962:        (forth-newline-remove-trailing))
                    963:       (when (= (+ (point) forth-c/l) (mark t))
                    964:        (forth-remove-trailing))
                    965:       (mark t))))
                    966: 
                    967: ;; Pad a line of a block file up to `forth-c/l' characters, positioning `point'
                    968: ;; at the end of line.
                    969: (defun forth-pad-block-line ()
                    970:   (save-excursion
                    971:     (end-of-line)
                    972:     (if (<= (current-column) forth-c/l)
                    973:        (move-to-column forth-c/l t)
                    974:       (message "Line %i longer than %i characters, truncated"
                    975:               (count-lines (point-min) (point)) forth-c/l)
                    976:       (ding t)
                    977:       (move-to-column forth-c/l t)
                    978:       (delete-region (point) (progn (end-of-line) (point))))))
                    979: 
                    980: ;; Replace tab characters in current line by spaces.
                    981: (defun forth-convert-tabs-in-line ()
                    982:   (save-excursion
                    983:     (beginning-of-line)
                    984:     (while (search-forward "\t" (save-excursion (end-of-line) (point)) t)
                    985:       (backward-char)
                    986:       (delete-region (point) (1+ (point)))
                    987:       (insert-char ?\  (- tab-width (% (current-column) tab-width))))))
                    988: 
                    989: ;; Delete newline at end of current line, concatenating it with the following
                    990: ;; line. Place `point' at end of newly formed line.
                    991: (defun forth-delete-newline ()
                    992:   (end-of-line)
                    993:   (delete-region (point) (progn (beginning-of-line 2) (point))))
                    994: 
                    995: (defun forth-convert-to-block (from to &optional original-buffer) 
                    996:   "Convert range of text to block file format in current buffer."
                    997:   (let* ((lines 0)) ; I have to count lines myself, since `count-lines' has
                    998:                    ; problems with trailing newlines...
                    999:     (save-excursion
                   1000:       (goto-char from)
                   1001:       (set-mark to)
                   1002:       ;; pad lines to full length (`forth-c/l' characters per line)
                   1003:       (while (< (save-excursion (end-of-line) (point)) (mark t))
                   1004:        (setq lines (1+ lines))
                   1005:        (forth-pad-block-line)
                   1006:        (forth-convert-tabs-in-line)
                   1007:        (forward-line))
                   1008:       ;; also make sure the last line is padded, if `to' is at its end
                   1009:       (end-of-line)
                   1010:       (when (= (point) (mark t))
                   1011:        (setq lines (1+ lines))
                   1012:        (forth-pad-block-line)
                   1013:        (forth-convert-tabs-in-line))
                   1014:       ;; remove newlines between lines
                   1015:       (goto-char from)
                   1016:       (while (< (save-excursion (end-of-line) (point)) (mark t))
                   1017:        (forth-delete-newline))
                   1018:       ;; append empty lines, until last block is complete
                   1019:       (goto-char (mark t))
                   1020:       (let* ((required (* (/ (+ lines (1- forth-l/b)) forth-l/b) forth-l/b))
                   1021:             (pad-lines (- required lines)))
                   1022:        (while (> pad-lines 0)
                   1023:          (insert-char ?\  forth-c/l)
                   1024:          (setq pad-lines (1- pad-lines))))
                   1025:       (point))))
                   1026: 
                   1027: (defun forth-detect-block-file-p ()
                   1028:   "Return non-nil if the current buffer is in block file format. Detection is
                   1029: done by checking whether the first line has 1024 characters or more."
                   1030:   (save-restriction 
                   1031:     (widen)
                   1032:     (save-excursion
1.54      dvdkhlng 1033:        (goto-char (point-min))
1.48      pazsan   1034:        (end-of-line)
                   1035:        (>= (current-column) 1024))))
                   1036: 
                   1037: ;; add block file conversion routines to `format-alist'
                   1038: (defconst forth-block-format-description
                   1039:   '(forth-blocks "Forth block source file" nil 
                   1040:                 forth-convert-from-block forth-convert-to-block 
                   1041:                 t normal-mode))
                   1042: (unless (memq forth-block-format-description format-alist)
                   1043:   (setq format-alist (cons forth-block-format-description format-alist)))
                   1044: 
                   1045: ;;; End block file encoding/decoding
                   1046: 
                   1047: ;;; Block file editing
                   1048: ;;;
                   1049: (defvar forth-overlay-arrow-string ">>")
                   1050: (defvar forth-block-base 1 "Number of first block in block file")
                   1051: (defvar forth-show-screen nil
                   1052:   "Non-nil means to show screen starts and numbers (for block files)")
                   1053: (defvar forth-warn-long-lines nil
                   1054:   "Non-nil means to warn about lines that are longer than 64 characters")
                   1055: 
                   1056: (defvar forth-screen-marker nil)
1.57      dvdkhlng 1057: (defvar forth-screen-number-string nil)
1.48      pazsan   1058: 
                   1059: (defun forth-update-show-screen ()
                   1060:   "If `forth-show-screen' is non-nil, put overlay arrow to start of screen, 
                   1061: `point' is in. If arrow now points to different screen than before, display 
                   1062: screen number."
                   1063:   (if (not forth-show-screen)
                   1064:       (setq overlay-arrow-string nil)
                   1065:     (save-excursion
                   1066:       (let* ((line (count-lines (point-min) (min (point-max) (1+ (point)))))
                   1067:             (first-line (1+ (* (/ (1- line) forth-l/b) forth-l/b)))
                   1068:             (scr (+ forth-block-base (/ first-line forth-l/b))))
                   1069:        (setq overlay-arrow-string forth-overlay-arrow-string)
                   1070:        (goto-line first-line)
                   1071:        (setq overlay-arrow-position forth-screen-marker)
1.50      dvdkhlng 1072:        (set-marker forth-screen-marker 
                   1073:                    (save-excursion (goto-line first-line) (point)))
                   1074:        (setq forth-screen-number-string (format "%d" scr))))))
1.48      pazsan   1075: 
                   1076: (add-hook 'forth-motion-hooks 'forth-update-show-screen)
                   1077: 
                   1078: (defun forth-update-warn-long-lines ()
                   1079:   "If `forth-warn-long-lines' is non-nil, display a warning whenever a line
                   1080: exceeds 64 characters."
                   1081:   (when forth-warn-long-lines
                   1082:     (when (> (save-excursion (end-of-line) (current-column)) forth-c/l)
                   1083:       (message "Warning: current line exceeds %i characters"
                   1084:               forth-c/l))))
                   1085: 
                   1086: (add-hook 'forth-motion-hooks 'forth-update-warn-long-lines)
1.55      dvdkhlng 1087: 
1.48      pazsan   1088: ;;; End block file editing
1.1       anton    1089: 
1.6       anton    1090: 
1.1       anton    1091: (defvar forth-mode-abbrev-table nil
                   1092:   "Abbrev table in use in Forth-mode buffers.")
                   1093: 
                   1094: (define-abbrev-table 'forth-mode-abbrev-table ())
                   1095: 
                   1096: (defvar forth-mode-map nil
                   1097:   "Keymap used in Forth mode.")
                   1098: 
                   1099: (if (not forth-mode-map)
                   1100:     (setq forth-mode-map (make-sparse-keymap)))
                   1101: 
1.9       anton    1102: ;(define-key forth-mode-map "\M-\C-x" 'compile)
1.7       anton    1103: (define-key forth-mode-map "\C-x\\" 'comment-region)
                   1104: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
1.1       anton    1105: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
                   1106: (define-key forth-mode-map "\e " 'forth-reload)
                   1107: (define-key forth-mode-map "\t" 'forth-indent-command)
1.48      pazsan   1108: (define-key forth-mode-map "\C-m" 'forth-reindent-then-newline-and-indent)
1.7       anton    1109: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
1.13      pazsan   1110: (define-key forth-mode-map "\e." 'forth-find-tag)
1.1       anton    1111: 
1.61      dvdkhlng 1112: ;; setup for C-h C-i to work
1.58      dvdkhlng 1113: (eval-and-compile (forth-require 'info-look))
                   1114: (when (memq 'info-look features)
1.61      dvdkhlng 1115:   (defvar forth-info-lookup '(symbol (forth-mode "\\S-+" t 
1.59      dvdkhlng 1116:                                                  (("(gforth)Word Index"))
1.61      dvdkhlng 1117:                                                  "\\S-+")))
1.59      dvdkhlng 1118:   (unless (memq forth-info-lookup info-lookup-alist)
1.61      dvdkhlng 1119:     (setq info-lookup-alist (cons forth-info-lookup info-lookup-alist)))
                   1120:   ;; in X-Emacs C-h C-i is by default bound to Info-query
1.69      dvdkhlng 1121:   (define-key forth-mode-map [?\C-h ?\C-i] 'info-lookup-symbol))
1.59      dvdkhlng 1122: 
                   1123: ;;   (info-lookup-add-help
                   1124: ;;    :topic 'symbol
                   1125: ;;    :mode 'forth-mode
                   1126: ;;    :regexp "[^      
                   1127: ;; ]+"
                   1128: ;;    :ignore-case t
                   1129: ;;    :doc-spec '(("(gforth)Name Index" nil "`" "'  "))))
1.35      anton    1130: 
1.57      dvdkhlng 1131: (require 'etags)
1.13      pazsan   1132: 
                   1133: (defun forth-find-tag (tagname &optional next-p regexp-p)
                   1134:   (interactive (find-tag-interactive "Find tag: "))
1.53      dvdkhlng 1135:   (unless (or regexp-p next-p)
1.67      dvdkhlng 1136:     (setq tagname (concat "\\(^\\|\\s-+\\)\\(" (regexp-quote tagname) 
                   1137:                            "\\)\\s-*\x7f")))
1.13      pazsan   1138:   (switch-to-buffer
1.53      dvdkhlng 1139:    (find-tag-noselect tagname next-p t)))
1.13      pazsan   1140: 
1.1       anton    1141: (defvar forth-mode-syntax-table nil
                   1142:   "Syntax table in use in Forth-mode buffers.")
                   1143: 
1.48      pazsan   1144: ;; Important: hilighting/indentation now depends on a correct syntax table.
                   1145: ;; All characters, except whitespace *must* belong to the "word constituent"
                   1146: ;; syntax class. If different behaviour is required, use of Categories might
                   1147: ;; help.
                   1148: (if (not forth-mode-syntax-table)
1.1       anton    1149:     (progn
                   1150:       (setq forth-mode-syntax-table (make-syntax-table))
1.6       anton    1151:       (let ((char 0))
                   1152:        (while (< char ?!)
                   1153:          (modify-syntax-entry char " " forth-mode-syntax-table)
                   1154:          (setq char (1+ char)))
                   1155:        (while (< char 256)
                   1156:          (modify-syntax-entry char "w" forth-mode-syntax-table)
                   1157:          (setq char (1+ char))))
                   1158:       ))
1.1       anton    1159: 
                   1160: (defun forth-mode-variables ()
                   1161:   (set-syntax-table forth-mode-syntax-table)
                   1162:   (setq local-abbrev-table forth-mode-abbrev-table)
                   1163:   (make-local-variable 'paragraph-start)
                   1164:   (setq paragraph-start (concat "^$\\|" page-delimiter))
                   1165:   (make-local-variable 'paragraph-separate)
                   1166:   (setq paragraph-separate paragraph-start)
                   1167:   (make-local-variable 'indent-line-function)
                   1168:   (setq indent-line-function 'forth-indent-line)
1.6       anton    1169: ;  (make-local-variable 'require-final-newline)
                   1170: ;  (setq require-final-newline t)
1.1       anton    1171:   (make-local-variable 'comment-start)
1.3       anton    1172:   (setq comment-start "\\ ")
                   1173:   ;(make-local-variable 'comment-end)
                   1174:   ;(setq comment-end " )")
1.1       anton    1175:   (make-local-variable 'comment-column)
                   1176:   (setq comment-column 40)
                   1177:   (make-local-variable 'comment-start-skip)
1.65      dvdkhlng 1178:   (setq comment-start-skip "\\\\ ")
1.57      dvdkhlng 1179:   (make-local-variable 'comment-indent-function)
                   1180:   (setq comment-indent-function 'forth-comment-indent)
1.1       anton    1181:   (make-local-variable 'parse-sexp-ignore-comments)
1.47      pazsan   1182:   (setq parse-sexp-ignore-comments t)
1.48      pazsan   1183:   (setq case-fold-search t)
1.55      dvdkhlng 1184:   (make-local-variable 'forth-was-point)
                   1185:   (setq forth-was-point -1)
1.48      pazsan   1186:   (make-local-variable 'forth-words)
                   1187:   (make-local-variable 'forth-compiled-words)
                   1188:   (make-local-variable 'forth-compiled-indent-words)
                   1189:   (make-local-variable 'forth-hilight-level)
                   1190:   (make-local-variable 'after-change-functions)
                   1191:   (make-local-variable 'forth-show-screen)
                   1192:   (make-local-variable 'forth-screen-marker)
                   1193:   (make-local-variable 'forth-warn-long-lines)
1.50      dvdkhlng 1194:   (make-local-variable 'forth-screen-number-string)
1.51      dvdkhlng 1195:   (make-local-variable 'forth-use-oof)
                   1196:   (make-local-variable 'forth-use-objects) 
1.48      pazsan   1197:   (setq forth-screen-marker (copy-marker 0))
1.51      dvdkhlng 1198:   (add-hook 'after-change-functions 'forth-change-function)
1.62      dvdkhlng 1199:   (if (and forth-jit-parser (>= emacs-major-version 21))
1.55      dvdkhlng 1200:       (add-hook 'fontification-functions 'forth-fontification-function))
1.51      dvdkhlng 1201:   (setq imenu-create-index-function 'forth-create-index))
1.47      pazsan   1202: 
1.2       anton    1203: ;;;###autoload
1.1       anton    1204: (defun forth-mode ()
                   1205:   "
                   1206: Major mode for editing Forth code. Tab indents for Forth code. Comments
1.9       anton    1207: are delimited with \\ and newline. Paragraphs are separated by blank lines
1.49      dvdkhlng 1208: only. Block files are autodetected, when read, and converted to normal 
                   1209: stream source format. See also `forth-block-mode'.
1.1       anton    1210: \\{forth-mode-map}
                   1211: 
1.48      pazsan   1212: Variables controlling syntax hilighting/recognition of parsed text:
                   1213:  `forth-words'
                   1214:     List of words that have a special parsing behaviour and/or should be
1.51      dvdkhlng 1215:     hilighted. Add custom words by setting forth-custom-words in your
                   1216:     .emacs, or by setting forth-local-words, in source-files' local 
                   1217:     variables lists.
                   1218:  forth-use-objects
1.55      dvdkhlng 1219:     Set this variable to non-nil in your .emacs, or in a local variables 
1.51      dvdkhlng 1220:     list, to hilight and recognize the words from the \"Objects\" package 
                   1221:     for object-oriented programming.
                   1222:  forth-use-oof
                   1223:     Same as above, just for the \"OOF\" package.
                   1224:  forth-custom-words
                   1225:     List of custom Forth words to prepend to `forth-words'. Should be set
                   1226:     in your .emacs.
1.49      dvdkhlng 1227:  forth-local-words
                   1228:     List of words to prepend to `forth-words', whenever a forth-mode
                   1229:     buffer is created. That variable should be set by Forth sources, using
                   1230:     a local variables list at the end of file, to get file-specific
                   1231:     hilighting.
                   1232:     0 [IF]
                   1233:        Local Variables: ... 
                   1234:        forth-local-words: ...
                   1235:        End:
                   1236:     [THEN]
1.48      pazsan   1237:  forth-hilight-level
                   1238:     Controls how much syntax hilighting is done. Should be in the range 
1.51      dvdkhlng 1239:     0..3
1.48      pazsan   1240: 
1.1       anton    1241: Variables controlling indentation style:
1.48      pazsan   1242:  `forth-indent-words'
                   1243:     List of words that influence indentation.
1.51      dvdkhlng 1244:  forth-local-indent-words
1.49      dvdkhlng 1245:     List of words to prepend to `forth-indent-words', similar to 
1.51      dvdkhlng 1246:     forth-local-words. Should be used for specifying file-specific 
1.49      dvdkhlng 1247:     indentation, using a local variables list.
1.51      dvdkhlng 1248:  forth-custom-indent-words
                   1249:     List of words to prepend to `forth-indent-words'. Should be set in your
                   1250:     .emacs.    
1.1       anton    1251:  forth-indent-level
                   1252:     Indentation increment/decrement of Forth statements.
1.48      pazsan   1253:  forth-minor-indent-level
                   1254:     Minor indentation increment/decrement of Forth statemens.
1.1       anton    1255: 
1.48      pazsan   1256: Variables controlling block-file editing:
1.51      dvdkhlng 1257:  forth-show-screen
1.48      pazsan   1258:     Non-nil means, that the start of the current screen is marked by an
1.50      dvdkhlng 1259:     overlay arrow, and screen numbers are displayed in the mode line.
                   1260:     This variable is by default nil for `forth-mode' and t for 
                   1261:     `forth-block-mode'.
1.51      dvdkhlng 1262:  forth-overlay-arrow-string
1.48      pazsan   1263:     String to display as the overlay arrow, when `forth-show-screen' is t.
                   1264:     Setting this variable to nil disables the overlay arrow.
1.51      dvdkhlng 1265:  forth-block-base
1.48      pazsan   1266:     Screen number of the first block in a block file. Defaults to 1.
1.51      dvdkhlng 1267:  forth-warn-long-lines
1.48      pazsan   1268:     Non-nil means that a warning message is displayed whenever you edit or
                   1269:     move over a line that is longer than 64 characters (the maximum line
                   1270:     length that can be stored into a block file). This variable defaults to
                   1271:     t for `forth-block-mode' and to nil for `forth-mode'.
1.1       anton    1272: 
1.66      dvdkhlng 1273: Variables controlling interaction with the Forth-process (also see
                   1274: `run-forth'):
                   1275:   forth-program-name
                   1276:     Program invoked by the `run-forth' command (including arguments).
                   1277:   inferior-forth-mode-hook
                   1278:     Hook for customising inferior-forth-mode.
                   1279:   forth-compile-command
                   1280:     Default command to execute on `compile'.
                   1281: " 
1.1       anton    1282:   (interactive)
                   1283:   (kill-all-local-variables)
                   1284:   (use-local-map forth-mode-map)
                   1285:   (setq mode-name "Forth")
                   1286:   (setq major-mode 'forth-mode)
1.62      dvdkhlng 1287:   (forth-install-motion-hook)
1.48      pazsan   1288:   ;; convert buffer contents from block file format, if necessary
                   1289:   (when (forth-detect-block-file-p)
                   1290:     (widen)
                   1291:     (message "Converting from Forth block source...")
                   1292:     (forth-convert-from-block (point-min) (point-max))
                   1293:     (message "Converting from Forth block source...done"))
                   1294:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
                   1295:   ;; is now stored as normal strem source
                   1296:   (when (equal buffer-file-format '(forth-blocks))
                   1297:     (setq buffer-file-format nil))
1.1       anton    1298:   (forth-mode-variables)
                   1299: ;  (if (not (forth-process-running-p))
                   1300: ;      (run-forth forth-program-name))
1.49      dvdkhlng 1301:   (run-hooks 'forth-mode-hook))
1.48      pazsan   1302: 
1.50      dvdkhlng 1303: ;;;###autoload
1.48      pazsan   1304: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
                   1305:   "Major mode for editing Forth block source files, derived from 
                   1306: `forth-mode'. Differences to `forth-mode' are:
                   1307:  * files are converted to block format, when written (`buffer-file-format' 
                   1308:    is set to `(forth-blocks)')
                   1309:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
                   1310:   
                   1311: Note that the length of lines in block files is limited to 64 characters.
                   1312: When writing longer lines to a block file, a warning is displayed in the
                   1313: echo area and the line is truncated. 
                   1314: 
                   1315: Another problem is imposed by block files that contain newline or tab 
                   1316: characters. When Emacs converts such files back to block file format, 
1.50      dvdkhlng 1317: it'll translate those characters to a number of spaces. However, when
1.48      pazsan   1318: you read such a file, a warning message is displayed in the echo area,
                   1319: including a line number that may help you to locate and fix the problem.
                   1320: 
                   1321: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
                   1322: bell during block file read/write operations."
                   1323:   (setq buffer-file-format '(forth-blocks))
                   1324:   (setq forth-show-screen t)
1.50      dvdkhlng 1325:   (setq forth-warn-long-lines t)
                   1326:   (setq forth-screen-number-string (format "%d" forth-block-base))
                   1327:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
                   1328:                                 '("--S" forth-screen-number-string "-%-"))))
1.1       anton    1329: 
1.44      anton    1330: (add-hook 'forth-mode-hook
1.9       anton    1331:       '(lambda () 
                   1332:         (make-local-variable 'compile-command)
1.49      dvdkhlng 1333:         (setq compile-command "gforth ")
                   1334:         (forth-hack-local-variables)
1.51      dvdkhlng 1335:         (forth-customize-words)
1.49      dvdkhlng 1336:         (forth-compile-words)
1.55      dvdkhlng 1337:         (unless (and forth-jit-parser (>= emacs-major-version 21))
                   1338:           (forth-change-function (point-min) (point-max) nil t))))
1.6       anton    1339: 
1.7       anton    1340: (defun forth-fill-paragraph () 
                   1341:   "Fill comments (starting with '\'; do not fill code (block style
                   1342: programmers who tend to fill code won't use emacs anyway:-)."
1.9       anton    1343:   ; Currently only comments at the start of the line are filled.
                   1344:   ; Something like lisp-fill-paragraph may be better.  We cannot use
                   1345:   ; fill-paragraph, because it removes the \ from the first comment
                   1346:   ; line. Therefore we have to look for the first line of the comment
                   1347:   ; and use fill-region.
1.7       anton    1348:   (interactive)
                   1349:   (save-excursion
                   1350:     (beginning-of-line)
1.9       anton    1351:     (while (and
                   1352:             (= (forward-line -1) 0)
1.14      anton    1353:             (looking-at "[ \t]*\\\\g?[ \t]+")))
                   1354:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
1.9       anton    1355:        (forward-line 1))
                   1356:     (let ((from (point))
                   1357:          (to (save-excursion (forward-paragraph) (point))))
1.14      anton    1358:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
1.9       anton    1359:          (progn (goto-char (match-end 0))
                   1360:                 (set-fill-prefix)
                   1361:                 (fill-region from to nil))))))
1.7       anton    1362: 
1.1       anton    1363: (defun forth-comment-indent ()
                   1364:   (save-excursion
                   1365:     (beginning-of-line)
                   1366:     (if (looking-at ":[ \t]*")
                   1367:        (progn
                   1368:          (end-of-line)
                   1369:          (skip-chars-backward " \t\n")
                   1370:          (1+ (current-column)))
                   1371:       comment-column)))
                   1372: 
                   1373: 
                   1374: ;; Forth commands
                   1375: 
1.7       anton    1376: (defun forth-remove-tracers ()
                   1377:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
                   1378:   (interactive)
1.16      anton    1379:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
1.7       anton    1380: 
1.66      dvdkhlng 1381: (define-key forth-mode-map "\C-x\C-e" 'compile)
                   1382: (define-key forth-mode-map "\C-x\C-n" 'next-error)
                   1383: (require 'compile)
1.1       anton    1384: 
1.66      dvdkhlng 1385: (defvar forth-compile-command "gforth ")
                   1386: ;(defvar forth-compilation-window-percent-height 30)
1.1       anton    1387: 
                   1388: (defun forth-split ()
                   1389:   (interactive)
                   1390:   (forth-split-1 "*forth*"))
                   1391: 
                   1392: (defun forth-split-1 (buffer)
                   1393:   (if (not (eq (window-buffer) (get-buffer buffer)))
                   1394:       (progn
                   1395:        (delete-other-windows)
                   1396:        (split-window-vertically
1.75      pazsan   1397:         (/ (frame-height) 2))
1.1       anton    1398:        (other-window 1)
                   1399:        (switch-to-buffer buffer)
                   1400:        (goto-char (point-max))
                   1401:        (other-window 1))))
                   1402: 
1.12      pazsan   1403: ;;; Forth menu
                   1404: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
                   1405: 
1.61      dvdkhlng 1406: ;; (dk) code commented out due to complaints of XEmacs users.  After
                   1407: ;; all, there's imenu/speedbar, which uses much smarter scanning
                   1408: ;; rules.
                   1409: 
                   1410: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
                   1411: ;;        (require 'func-menu)
                   1412: 
                   1413: ;;   (defconst fume-function-name-regexp-forth
                   1414: ;;    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
                   1415: ;;    "Expression to get word definitions in Forth.")
                   1416: 
                   1417: ;;   (setq fume-function-name-regexp-alist
                   1418: ;;       (append '((forth-mode . fume-function-name-regexp-forth) 
                   1419: ;;              ) fume-function-name-regexp-alist))
                   1420: 
                   1421: ;;   ;; Find next forth word in the buffer
                   1422: ;;   (defun fume-find-next-forth-function-name (buffer)
                   1423: ;;     "Searches for the next forth word in BUFFER."
                   1424: ;;     (set-buffer buffer)
                   1425: ;;     (if (re-search-forward fume-function-name-regexp nil t)
                   1426: ;;       (let ((beg (match-beginning 2))
                   1427: ;;             (end (match-end 2)))
                   1428: ;;         (cons (buffer-substring beg end) beg))))
1.12      pazsan   1429: 
1.61      dvdkhlng 1430: ;;   (setq fume-find-function-name-method-alist
                   1431: ;;   (append '((forth-mode    . fume-find-next-forth-function-name))))
1.12      pazsan   1432: 
1.61      dvdkhlng 1433: ;;   ))
1.12      pazsan   1434: ;;; End Forth menu
                   1435: 
                   1436: ;;; File folding of forth-files
                   1437: ;;; uses outline
                   1438: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
                   1439: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
                   1440: ;;;
                   1441: ;;; Works most of the times but loses sync with the cursor occasionally 
                   1442: ;;; Could be improved by also folding on comments
                   1443: 
1.61      dvdkhlng 1444: ;; (dk) This code needs a rewrite; just too ugly and doesn't use the
                   1445: ;; newer and smarter scanning rules of `imenu'. Who needs it anyway??
1.12      pazsan   1446: 
1.61      dvdkhlng 1447: ;; (require 'outline)
                   1448: 
                   1449: ;; (defun f-outline-level ()
                   1450: ;;   (cond     ((looking-at "\\`\\\\")
                   1451: ;;      0)
                   1452: ;;     ((looking-at "\\\\ SEC")
                   1453: ;;      0)
                   1454: ;;     ((looking-at "\\\\ \\\\ .*")
                   1455: ;;      0)
                   1456: ;;     ((looking-at "\\\\ DEFS")
                   1457: ;;      1)
                   1458: ;;     ((looking-at "\\/\\* ")
                   1459: ;;      1)
                   1460: ;;     ((looking-at ": .*")
                   1461: ;;      1)
                   1462: ;;     ((looking-at "\\\\G")
                   1463: ;;      2)
                   1464: ;;     ((looking-at "[ \t]+\\\\")
                   1465: ;;      3)))
1.57      dvdkhlng 1466:   
1.61      dvdkhlng 1467: ;; (defun fold-f  ()
                   1468: ;;    (interactive)
                   1469: ;;    (add-hook 'outline-minor-mode-hook 'hide-body)
                   1470: 
                   1471: ;;    ; outline mode header start, i.e. find word definitions
                   1472: ;; ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
                   1473: ;;    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
                   1474: ;;    (setq outline-level 'f-outline-level)
                   1475: 
                   1476: ;;    (outline-minor-mode)
                   1477: ;;    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
                   1478: ;;    (define-key outline-minor-mode-map '(shift right) 'show-children)
                   1479: ;;    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
                   1480: ;;    (define-key outline-minor-mode-map '(shift down) 'show-subtree))
                   1481: 
1.29      pazsan   1482: 
                   1483: ;;(define-key global-map '(shift up) 'fold-f)
                   1484: 
1.12      pazsan   1485: ;;; end file folding
                   1486: 
                   1487: ;;; func-menu is a package that scans your source file for function definitions
                   1488: ;;; and makes a menubar entry that lets you jump to any particular function
                   1489: ;;; definition by selecting it from the menu.  The following code turns this on
                   1490: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
                   1491: ;;; but not much.
                   1492: ;;;
1.61      dvdkhlng 1493: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
                   1494: ;;        (require 'func-menu)
                   1495: ;; ;;       (define-key global-map 'f8 'function-menu)
                   1496: ;;        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
                   1497: ;; ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
                   1498: ;; ;       (define-key global-map '(shift button3) 'mouse-function-menu)
                   1499: ;; ))
1.66      dvdkhlng 1500: 
                   1501: ;;;
                   1502: ;;; Inferior Forth interpreter 
                   1503: ;;;    -- mostly copied from `cmuscheme.el' of Emacs 21.2
                   1504: ;;;
                   1505: 
                   1506: (eval-and-compile (forth-require 'comint))
                   1507: 
                   1508: (when (memq 'comint features)
                   1509: 
                   1510:   (defvar forth-program-name "gforth"
                   1511:     "*Program invoked by the `run-forth' command, including program arguments")
                   1512: 
                   1513:   (defcustom inferior-forth-mode-hook nil
                   1514:     "*Hook for customising inferior-forth-mode."
                   1515:     :type 'hook
                   1516:     :group 'forth)
                   1517: 
                   1518:   (defvar inferior-forth-mode-map
                   1519:     (let ((m (make-sparse-keymap)))
                   1520:       (define-key m "\r" 'comint-send-input)
                   1521:       (define-key m "\M-\C-x" 'forth-send-paragraph-and-go)
                   1522:       (define-key m "\C-c\C-l" 'forth-load-file)
                   1523:       m))
                   1524:   ;; Install the process communication commands in the forth-mode keymap.
                   1525:   (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph-and-go)
                   1526:   (define-key forth-mode-map "\eo" 'forth-send-buffer-and-go)
                   1527: 
                   1528:   (define-key forth-mode-map "\M-\C-x" 'forth-send-paragraph-and-go)
                   1529:   (define-key forth-mode-map "\C-c\C-r" 'forth-send-region)
                   1530:   (define-key forth-mode-map "\C-c\M-r" 'forth-send-region-and-go)
                   1531:   (define-key forth-mode-map "\C-c\C-z" 'forth-switch-to-interactive)
                   1532:   (define-key forth-mode-map "\C-c\C-l" 'forth-load-file)
                   1533: 
                   1534:   (defvar forth-process-buffer)
                   1535: 
                   1536:   (define-derived-mode inferior-forth-mode comint-mode "Inferior Forth"
                   1537:     "Major mode for interacting with an inferior Forth process.
                   1538: 
                   1539: The following commands are available:
                   1540: \\{inferior-forth-mode-map}
                   1541: 
                   1542: A Forth process can be fired up with M-x run-forth.
                   1543: 
                   1544: Customisation: Entry to this mode runs the hooks on comint-mode-hook and
                   1545: inferior-forth-mode-hook (in that order).
                   1546: 
                   1547: You can send text to the inferior Forth process from other buffers containing
                   1548: Forth source.
                   1549:     forth-switch-to-interactive switches the current buffer to the Forth
                   1550:         process buffer. 
                   1551:     forth-send-paragraph sends the current paragraph to the Forth process.
                   1552:     forth-send-region sends the current region to the Forth process.
                   1553:     forth-send-buffer sends the current buffer to the Forth process.
                   1554: 
                   1555:     forth-send-paragraph-and-go, forth-send-region-and-go,
                   1556:         forth-send-buffer-and-go switch to the Forth process buffer after
                   1557:         sending their text.
                   1558: For information on running multiple processes in multiple buffers, see
                   1559: documentation for variable `forth-process-buffer'.
                   1560: 
                   1561: Commands:
                   1562: Return after the end of the process' output sends the text from the
                   1563: end of process to point. If you accidentally suspend your process, use
                   1564: \\[comint-continue-subjob] to continue it. "
                   1565:     ;; Customise in inferior-forth-mode-hook
                   1566:     (setq comint-prompt-regexp "^") 
                   1567:     (setq mode-line-process '(":%s")))
                   1568: 
                   1569:   (defun forth-args-to-list (string)
                   1570:     (let ((where (string-match "[ \t]" string)))
                   1571:       (cond ((null where) (list string))
                   1572:            ((not (= where 0))
                   1573:             (cons (substring string 0 where)
                   1574:                   (forth-args-to-list (substring string (+ 1 where)
                   1575:                                                  (length string)))))
                   1576:            (t (let ((pos (string-match "[^ \t]" string)))
                   1577:                 (if (null pos)
                   1578:                     nil
                   1579:                   (forth-args-to-list (substring string pos
                   1580:                                                  (length string)))))))))
                   1581: 
                   1582: ;;;###autoload
                   1583:   (defun run-forth (cmd)
                   1584:     "Run an inferior Forth process, input and output via buffer *forth*.
                   1585: If there is a process already running in `*forth*', switch to that buffer.
                   1586: With argument, allows you to edit the command line (default is value
                   1587: of `forth-program-name').  Runs the hooks `inferior-forth-mode-hook'
                   1588: \(after the `comint-mode-hook' is run).
                   1589: \(Type \\[describe-mode] in the process buffer for a list of commands.)"
                   1590: 
                   1591:     (interactive (list (if current-prefix-arg
                   1592:                           (read-string "Run Forth: " forth-program-name)
                   1593:                         forth-program-name)))
                   1594:     (if (not (comint-check-proc "*forth*"))
                   1595:        (let ((cmdlist (forth-args-to-list cmd)))
                   1596:          (set-buffer (apply 'make-comint "forth" (car cmdlist)
                   1597:                             nil (cdr cmdlist)))
                   1598:          (inferior-forth-mode)))
                   1599:     (setq forth-program-name cmd)
                   1600:     (setq forth-process-buffer "*forth*")
                   1601:     (pop-to-buffer "*forth*"))
                   1602: 
                   1603:   (defun forth-send-region (start end)
                   1604:     "Send the current region to the inferior Forth process."
                   1605:     (interactive "r")
                   1606:     (comint-send-region (forth-proc) start end)
                   1607:     (comint-send-string (forth-proc) "\n"))
                   1608: 
                   1609:   (defun forth-end-of-paragraph ()
                   1610:     (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
                   1611:     (if (not (re-search-forward "\n[ \t]*\n" nil t))
                   1612:        (goto-char (point-max))))
                   1613: 
                   1614:   (defun forth-send-paragraph ()
                   1615:     "Send the current or the previous paragraph to the Forth process"
                   1616:     (interactive)
                   1617:     (let (end)
                   1618:       (save-excursion
                   1619:        (forth-end-of-paragraph)
                   1620:        (skip-chars-backward  "\t\n ")
                   1621:        (setq end (point))
1.75      pazsan   1622:        (if (null (re-search-backward "\n[ \t]*\n" nil t))
1.66      dvdkhlng 1623:          (goto-char (point-min)))
                   1624:        (skip-chars-forward  "\t\n ")
                   1625:        (forth-send-region (point) end))))
                   1626: 
                   1627:   (defun forth-send-paragraph-and-go ()
                   1628:     "Send the current or the previous paragraph to the Forth process.
                   1629: Then switch to the process buffer."
                   1630:     (interactive)
                   1631:     (forth-send-paragraph)
                   1632:     (forth-switch-to-interactive t))
                   1633: 
                   1634:   (defun forth-send-buffer ()
                   1635:     "Send the current buffer to the Forth process."
                   1636:     (interactive)
                   1637:     (if (eq (current-buffer) forth-process-buffer)
                   1638:        (error "Not allowed to send this buffer's contents to Forth"))
                   1639:     (forth-send-region (point-min) (point-max)))
                   1640: 
                   1641:   (defun forth-send-buffer-and-go ()
                   1642:     "Send the current buffer to the Forth process.
                   1643: Then switch to the process buffer."
                   1644:     (interactive)
                   1645:     (forth-send-buffer)
                   1646:     (forth-switch-to-interactive t))
                   1647: 
                   1648: 
                   1649:   (defun forth-switch-to-interactive (eob-p)
                   1650:     "Switch to the Forth process buffer.
                   1651: With argument, position cursor at end of buffer."
                   1652:     (interactive "P")
                   1653:     (if (get-buffer forth-process-buffer)
                   1654:        (pop-to-buffer forth-process-buffer)
                   1655:       (error "No current process buffer.  See variable `forth-process-buffer'"))
                   1656:     (cond (eob-p
                   1657:           (push-mark)
                   1658:           (goto-char (point-max)))))
                   1659: 
1.75      pazsan   1660:   (defun forth-send-region-and-go (my-start end)
1.66      dvdkhlng 1661:     "Send the current region to the inferior Forth process.
                   1662: Then switch to the process buffer."
                   1663:     (interactive "r")
1.75      pazsan   1664:     (forth-send-region my-start end)
1.66      dvdkhlng 1665:     (forth-switch-to-interactive t))
                   1666: 
                   1667:   (defcustom forth-source-modes '(forth-mode forth-block-mode)
                   1668:     "*Used to determine if a buffer contains Forth source code.
                   1669: If it's loaded into a buffer that is in one of these major modes, it's
                   1670: considered a Forth source file by `forth-load-file' and `forth-compile-file'.
                   1671: Used by these commands to determine defaults."
                   1672:     :type '(repeat function)
                   1673:     :group 'forth)
                   1674: 
                   1675:   (defvar forth-prev-l/c-dir/file nil
                   1676:     "Caches the last (directory . file) pair.
                   1677: Caches the last pair used in the last `forth-load-file' or
                   1678: `forth-compile-file' command. Used for determining the default in the
                   1679: next one.")
                   1680: 
                   1681:   (defun forth-load-file (file-name)
                   1682:     "Load a Forth file FILE-NAME into the inferior Forth process."
                   1683:     (interactive (comint-get-source "Load Forth file: " forth-prev-l/c-dir/file
                   1684:                                    forth-source-modes t)) ; T because LOAD
                   1685:                                        ; needs an exact name
                   1686:     (comint-check-source file-name) ; Check to see if buffer needs saved.
1.70      dvdkhlng 1687:     (setq forth-prev-l/c-dir/file (cons (file-name-directory file-name)
1.66      dvdkhlng 1688:                                        (file-name-nondirectory file-name)))
1.70      dvdkhlng 1689:     (comint-send-string (forth-proc)
                   1690:                        (concat "s\" " file-name "\" included\n")))
1.66      dvdkhlng 1691: 
                   1692:   
                   1693:   (defvar forth-process-buffer nil "*The current Forth process buffer.
                   1694: 
                   1695: See `scheme-buffer' for an explanation on how to run multiple Forth 
                   1696: processes.")
                   1697: 
                   1698:   (defun forth-proc ()
                   1699:     "Return the current Forth process.  See variable `forth-process-buffer'."
                   1700:     (let ((proc (get-buffer-process (if (eq major-mode 'inferior-forth-mode)
                   1701:                                        (current-buffer)
                   1702:                                      forth-process-buffer))))
                   1703:       (or proc
                   1704:          (error "No current process.  See variable `forth-process-buffer'"))))
                   1705:   )  ; (memq 'comint features)
1.57      dvdkhlng 1706: 
                   1707: (provide 'forth-mode)
1.29      pazsan   1708: 
1.48      pazsan   1709: ;;; gforth.el ends here

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