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