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