[gforth] / gforth / gforth.el  

gforth: gforth/gforth.el


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help