Annotation of gforth/gforth.el, revision 1.27
1.17 anton 1: ;; Forth mode for Emacs
2: ;; This file is part of GForth.
1.1 anton 3: ;; Changes by anton
1.6 anton 4: ;; This is a variant of forth.el that came with TILE.
1.5 anton 5: ;; I left most of this stuff untouched and made just a few changes for
1.6 anton 6: ;; the things I use (mainly indentation and syntax tables).
7: ;; So there is still a lot of work to do to adapt this to gforth.
1.1 anton 8:
1.17 anton 9: ;; GForth is distributed in the hope that it will be useful,
1.1 anton 10: ;; but WITHOUT ANY WARRANTY. No author or distributor
11: ;; accepts responsibility to anyone for the consequences of using it
12: ;; or for whether it serves any particular purpose or works at all,
13: ;; unless he says so in writing. Refer to the GNU Emacs General Public
14: ;; License for full details.
15:
16: ;; Everyone is granted permission to copy, modify and redistribute
17: ;; GNU Emacs, but only under the conditions described in the
18: ;; GNU Emacs General Public License. A copy of this license is
1.17 anton 19: ;; supposed to have been given to you along with Gforth so you
1.1 anton 20: ;; can know your rights and responsibilities. It should be in a
21: ;; file named COPYING. Among other things, the copyright notice
22: ;; and this notice must be preserved on all copies.
23:
24: ;;-------------------------------------------------------------------
25: ;; A Forth indentation, documentation search and interaction library
26: ;;-------------------------------------------------------------------
27: ;;
28: ;; Written by Goran Rydqvist, gorry@ida.liu.se, Summer 1988
29: ;; Started: 16 July 88
30: ;; Version: 2.10
31: ;; Last update: 5 December 1989 by Mikael Patel, mip@ida.liu.se
32: ;; Last update: 25 June 1990 by Goran Rydqvist, gorry@ida.liu.se
33: ;;
34: ;; Documentation: See forth-mode (^HF forth-mode)
35: ;;-------------------------------------------------------------------
36:
37:
38: (defvar forth-positives
1.23 anton 39: " : :noname code interpretation: ;code does> begin do ?do +do -do u+do u-do while if ?dup-if ?dup-0=-if else case of struct [if] [ifdef] [ifundef] [else] with public: private: class "
1.1 anton 40: "Contains all words which will cause the indent-level to be incremented
41: on the next line.
42: OBS! All words in forth-positives must be surrounded by spaces.")
43:
44: (defvar forth-negatives
1.21 pazsan 45: " ; end-code ;code does> until repeat while +loop loop -loop s+loop else then endif again endcase endof end-struct [then] [else] [endif] endwith class; how: "
1.1 anton 46: "Contains all words which will cause the indent-level to be decremented
47: on the current line.
48: OBS! All words in forth-negatives must be surrounded by spaces.")
49:
50: (defvar forth-zeroes
1.27 ! pazsan 51: " : :noname code interpretation: public: private: how: implements class class; "
1.1 anton 52: "Contains all words which causes the indent to go to zero")
53:
1.21 pazsan 54: (setq forth-zero 0)
55:
56: (defvar forth-zup
1.25 pazsan 57: " how: implements "
1.21 pazsan 58: "Contains all words which causes zero indent level to change")
59:
60: (defvar forth-zdown
61: " class; how: class public: private: "
62: "Contains all words which causes zero indent level to change")
63:
1.6 anton 64: (defvar forth-prefixes
65: " postpone [compile] ['] [char] "
66: "words that prefix and escape other words")
67:
1.1 anton 68: (defvar forth-mode-abbrev-table nil
69: "Abbrev table in use in Forth-mode buffers.")
70:
71: (define-abbrev-table 'forth-mode-abbrev-table ())
72:
73: (defvar forth-mode-map nil
74: "Keymap used in Forth mode.")
75:
76: (if (not forth-mode-map)
77: (setq forth-mode-map (make-sparse-keymap)))
78:
1.27 ! pazsan 79: (global-set-key "\e\C-m" 'forth-send-paragraph)
! 80: (global-set-key "\C-x\C-m" 'forth-split)
! 81: (global-set-key "\e " 'forth-reload)
! 82:
1.9 anton 83: ;(define-key forth-mode-map "\M-\C-x" 'compile)
1.7 anton 84: (define-key forth-mode-map "\C-x\\" 'comment-region)
85: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
1.1 anton 86: (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph)
87: (define-key forth-mode-map "\eo" 'forth-send-buffer)
88: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
89: (define-key forth-mode-map "\e " 'forth-reload)
90: (define-key forth-mode-map "\t" 'forth-indent-command)
91: (define-key forth-mode-map "\C-m" 'reindent-then-newline-and-indent)
1.7 anton 92: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
1.13 pazsan 93: (define-key forth-mode-map "\e." 'forth-find-tag)
1.1 anton 94:
1.22 anton 95: (load "etags")
1.13 pazsan 96:
97: (defun forth-find-tag (tagname &optional next-p regexp-p)
98: (interactive (find-tag-interactive "Find tag: "))
99: (switch-to-buffer
100: (find-tag-noselect (concat " " tagname " ") next-p regexp-p)))
101:
1.1 anton 102: (defvar forth-mode-syntax-table nil
103: "Syntax table in use in Forth-mode buffers.")
104:
105: (if (not forth-mode-syntax-table)
106: (progn
107: (setq forth-mode-syntax-table (make-syntax-table))
1.6 anton 108: (let ((char 0))
109: (while (< char ?!)
110: (modify-syntax-entry char " " forth-mode-syntax-table)
111: (setq char (1+ char)))
112: (while (< char 256)
113: (modify-syntax-entry char "w" forth-mode-syntax-table)
114: (setq char (1+ char))))
115: (modify-syntax-entry ?\" "\"" forth-mode-syntax-table)
116: (modify-syntax-entry ?\\ "<" forth-mode-syntax-table)
117: (modify-syntax-entry ?\n ">" forth-mode-syntax-table)
118: ))
119: ;I do not define '(' and ')' as comment delimiters, because emacs
120: ;only supports one comment syntax (and a hack to accomodate C++); I
121: ;use '\' for natural language comments and '(' for formal comments
122: ;like stack comments, so for me it's better to have emacs treat '\'
1.13 pazsan 123: ;comments as comments. If you want it different, make the appropriate
1.6 anton 124: ;changes (best in your .emacs file).
125: ;
126: ;Hmm, the C++ hack could be used to support both comment syntaxes: we
127: ;can have different comment styles, if both comments start with the
128: ;same character. we could use ' ' as first and '(' and '\' as second
129: ;character. However this would fail for G\ comments.
1.1 anton 130:
131: (defconst forth-indent-level 4
132: "Indentation of Forth statements.")
133:
134: (defun forth-mode-variables ()
135: (set-syntax-table forth-mode-syntax-table)
136: (setq local-abbrev-table forth-mode-abbrev-table)
137: (make-local-variable 'paragraph-start)
138: (setq paragraph-start (concat "^$\\|" page-delimiter))
139: (make-local-variable 'paragraph-separate)
140: (setq paragraph-separate paragraph-start)
141: (make-local-variable 'indent-line-function)
142: (setq indent-line-function 'forth-indent-line)
1.6 anton 143: ; (make-local-variable 'require-final-newline)
144: ; (setq require-final-newline t)
1.1 anton 145: (make-local-variable 'comment-start)
1.3 anton 146: (setq comment-start "\\ ")
147: ;(make-local-variable 'comment-end)
148: ;(setq comment-end " )")
1.1 anton 149: (make-local-variable 'comment-column)
150: (setq comment-column 40)
151: (make-local-variable 'comment-start-skip)
1.3 anton 152: (setq comment-start-skip "\\ ")
1.1 anton 153: (make-local-variable 'comment-indent-hook)
154: (setq comment-indent-hook 'forth-comment-indent)
155: (make-local-variable 'parse-sexp-ignore-comments)
156: (setq parse-sexp-ignore-comments t))
157:
1.2 anton 158: ;;;###autoload
1.1 anton 159: (defun forth-mode ()
160: "
161: Major mode for editing Forth code. Tab indents for Forth code. Comments
1.9 anton 162: are delimited with \\ and newline. Paragraphs are separated by blank lines
1.23 anton 163: only.
1.1 anton 164: \\{forth-mode-map}
165: Forth-split
166: Positions the current buffer on top and a forth-interaction window
167: below. The window size is controlled by the forth-percent-height
168: variable (see below).
169: Forth-reload
170: Reloads the forth library and restarts the forth process.
171: Forth-send-buffer
172: Sends the current buffer, in text representation, as input to the
173: forth process.
174: Forth-send-paragraph
175: Sends the previous or the current paragraph to the forth-process.
176: Note that the cursor only need to be with in the paragraph to be sent.
177: forth-documentation
178: Search for documentation of forward adjacent to cursor. Note! To use
179: this mode you have to add a line, to your .emacs file, defining the
180: directories to search through for documentation files (se variable
181: forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
182:
183: Variables controlling interaction and startup
184: forth-percent-height
185: Tells split how high to make the edit portion, in percent of the
186: current screen height.
187: forth-program-name
188: Tells the library which program name to execute in the interation
189: window.
190:
191: Variables controlling indentation style:
192: forth-positives
193: A string containing all words which causes the indent-level of the
194: following line to be incremented.
195: OBS! Each word must be surronded by spaces.
196: forth-negatives
197: A string containing all words which causes the indentation of the
198: current line to be decremented, if the word begin the line. These
199: words also has a cancelling effect on the indent-level of the
200: following line, independent of position.
201: OBS! Each word must be surronded by spaces.
202: forth-zeroes
203: A string containing all words which causes the indentation of the
204: current line to go to zero, if the word begin the line.
205: OBS! Each word must be surronded by spaces.
206: forth-indent-level
207: Indentation increment/decrement of Forth statements.
208:
209: Note! A word which decrements the indentation of the current line, may
210: also be mentioned in forth-positives to cause the indentation to
211: resume the previous level.
212:
213: Variables controling documentation search
214: forth-help-load-path
215: List of directories to search through to find *.doc
216: (forth-help-file-suffix) files. Nil means current default directory.
217: The specified directories must contain at least one .doc file. If it
218: does not and you still want the load-path to scan that directory, create
219: an empty file dummy.doc.
220: forth-help-file-suffix
221: The file names to search for in each directory specified by
222: forth-help-load-path. Defaulted to '*.doc'.
223: "
224: (interactive)
225: (kill-all-local-variables)
226: (use-local-map forth-mode-map)
227: (setq mode-name "Forth")
228: (setq major-mode 'forth-mode)
229: (forth-mode-variables)
230: ; (if (not (forth-process-running-p))
231: ; (run-forth forth-program-name))
232: (run-hooks 'forth-mode-hook))
233:
1.6 anton 234: (setq forth-mode-hook
1.9 anton 235: '(lambda ()
236: (make-local-variable 'compile-command)
237: (setq compile-command "gforth ")))
1.6 anton 238:
1.7 anton 239: (defun forth-fill-paragraph ()
240: "Fill comments (starting with '\'; do not fill code (block style
241: programmers who tend to fill code won't use emacs anyway:-)."
1.9 anton 242: ; Currently only comments at the start of the line are filled.
243: ; Something like lisp-fill-paragraph may be better. We cannot use
244: ; fill-paragraph, because it removes the \ from the first comment
245: ; line. Therefore we have to look for the first line of the comment
246: ; and use fill-region.
1.7 anton 247: (interactive)
248: (save-excursion
249: (beginning-of-line)
1.9 anton 250: (while (and
251: (= (forward-line -1) 0)
1.14 anton 252: (looking-at "[ \t]*\\\\g?[ \t]+")))
253: (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
1.9 anton 254: (forward-line 1))
255: (let ((from (point))
256: (to (save-excursion (forward-paragraph) (point))))
1.14 anton 257: (if (looking-at "[ \t]*\\\\g?[ \t]+")
1.9 anton 258: (progn (goto-char (match-end 0))
259: (set-fill-prefix)
260: (fill-region from to nil))))))
1.7 anton 261:
1.1 anton 262: (defun forth-comment-indent ()
263: (save-excursion
264: (beginning-of-line)
265: (if (looking-at ":[ \t]*")
266: (progn
267: (end-of-line)
268: (skip-chars-backward " \t\n")
269: (1+ (current-column)))
270: comment-column)))
271:
272: (defun forth-current-indentation ()
273: (save-excursion
274: (beginning-of-line)
275: (back-to-indentation)
276: (current-column)))
277:
278: (defun forth-delete-indentation ()
279: (let ((b nil) (m nil))
280: (save-excursion
281: (beginning-of-line)
282: (setq b (point))
283: (back-to-indentation)
284: (setq m (point)))
285: (delete-region b m)))
286:
287: (defun forth-indent-line (&optional flag)
288: "Correct indentation of the current Forth line."
289: (let ((x (forth-calculate-indent)))
290: (forth-indent-to x)))
291:
292: (defun forth-indent-command ()
293: (interactive)
294: (forth-indent-line t))
295:
296: (defun forth-indent-to (x)
297: (let ((p nil))
298: (setq p (- (current-column) (forth-current-indentation)))
299: (forth-delete-indentation)
300: (beginning-of-line)
301: (indent-to x)
302: (if (> p 0) (forward-char p))))
303:
304: ;;Calculate indent
305: (defun forth-calculate-indent ()
306: (let ((w1 nil) (indent 0) (centre 0))
307: (save-excursion
308: (beginning-of-line)
309: (skip-chars-backward " \t\n")
310: (beginning-of-line)
311: (back-to-indentation)
312: (setq indent (current-column))
313: (setq centre indent)
314: (setq indent (+ indent (forth-sum-line-indentation))))
315: (save-excursion
316: (beginning-of-line)
317: (back-to-indentation)
318: (let ((p (point)))
319: (skip-chars-forward "^ \t\n")
320: (setq w1 (buffer-substring p (point)))))
321: (if (> (- indent centre) forth-indent-level)
322: (setq indent (+ centre forth-indent-level)))
323: (if (> (- centre indent) forth-indent-level)
324: (setq indent (- centre forth-indent-level)))
325: (if (< indent 0) (setq indent 0))
326: (setq indent (- indent
327: (if (string-match
328: (regexp-quote (concat " " w1 " "))
329: forth-negatives)
330: forth-indent-level 0)))
1.21 pazsan 331: (if (string-match (regexp-quote (concat " " w1 " ")) forth-zdown)
332: (setq forth-zero 0))
1.1 anton 333: (if (string-match (regexp-quote (concat " " w1 " ")) forth-zeroes)
1.21 pazsan 334: (setq indent forth-zero))
335: (if (string-match (regexp-quote (concat " " w1 " ")) forth-zup)
336: (setq forth-zero 4))
1.1 anton 337: indent))
338:
339: (defun forth-sum-line-indentation ()
340: "Add upp the positive and negative weights of all words on the current line."
341: (let ((b (point)) (e nil) (sum 0) (w nil) (t1 nil) (t2 nil) (first t))
342: (end-of-line) (setq e (point))
343: (goto-char b)
344: (while (< (point) e)
345: (setq w (forth-next-word))
346: (setq t1 (string-match (regexp-quote (concat " " w " "))
347: forth-positives))
348: (setq t2 (string-match (regexp-quote (concat " " w " "))
349: forth-negatives))
350: (if t1
351: (setq sum (+ sum forth-indent-level)))
352: (if (and t2 (not first))
353: (setq sum (- sum forth-indent-level)))
354: (skip-chars-forward " \t")
355: (setq first nil))
356: sum))
357:
358:
359: (defun forth-next-word ()
1.6 anton 360: "Return the next forth-word. Skip anything that the forth-word takes from
361: the input stream (comments, arguments, etc.)"
362: ;actually, it would be better to use commands based on the
363: ;syntax-table or comment-start etc.
1.1 anton 364: (let ((w1 nil))
365: (while (not w1)
366: (skip-chars-forward " \t\n")
367: (let ((p (point)))
368: (skip-chars-forward "^ \t\n")
369: (setq w1 (buffer-substring p (point))))
370: (cond ((string-match "\"" w1)
371: (progn
1.6 anton 372: (skip-chars-forward "^\"\n")
373: (forward-char)))
374: ((string-match "\\\\" w1)
375: (progn
376: (end-of-line)
377: ))
378: ((or (equal "(" w1) (equal ".(" w1))
1.1 anton 379: (progn
1.6 anton 380: (skip-chars-forward "^)\n")
381: (forward-char)))
382: ((string-match (regexp-quote (concat " " w1 " ")) forth-prefixes)
383: (progn (skip-chars-forward " \t\n")
384: (skip-chars-forward "^ \t\n")))
1.1 anton 385: (t nil)))
386: w1))
387:
388:
389: ;; Forth commands
390:
1.7 anton 391: (defun forth-remove-tracers ()
392: "Remove tracers of the form `~~ '. Queries the user for each occurrence."
393: (interactive)
1.16 anton 394: (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
1.7 anton 395:
1.5 anton 396: (defvar forth-program-name "gforth"
1.1 anton 397: "*Program invoked by the `run-forth' command.")
398:
399: (defvar forth-band-name nil
400: "*Band loaded by the `run-forth' command.")
401:
402: (defvar forth-program-arguments nil
403: "*Arguments passed to the Forth program by the `run-forth' command.")
404:
405: (defun run-forth (command-line)
406: "Run an inferior Forth process. Output goes to the buffer `*forth*'.
407: With argument, asks for a command line. Split up screen and run forth
408: in the lower portion. The current-buffer when called will stay in the
409: upper portion of the screen, and all other windows are deleted.
410: Call run-forth again to make the *forth* buffer appear in the lower
411: part of the screen."
412: (interactive
413: (list (let ((default
414: (or forth-process-command-line
415: (forth-default-command-line))))
416: (if current-prefix-arg
417: (read-string "Run Forth: " default)
418: default))))
419: (setq forth-process-command-line command-line)
420: (forth-start-process command-line)
421: (forth-split)
422: (forth-set-runlight forth-runlight:input))
423:
424: (defun reset-forth ()
425: "Reset the Forth process."
426: (interactive)
427: (let ((process (get-process forth-program-name)))
428: (cond ((or (not process)
429: (not (eq (process-status process) 'run))
430: (yes-or-no-p
431: "The Forth process is running, are you SURE you want to reset it? "))
432: (message "Resetting Forth process...")
433: (forth-reload)
434: (message "Resetting Forth process...done")))))
435:
436: (defun forth-default-command-line ()
1.13 pazsan 437: (concat forth-program-name
1.1 anton 438: (if forth-program-arguments
439: (concat " " forth-program-arguments)
440: "")))
441:
442: ;;;; Internal Variables
443:
444: (defvar forth-process-command-line nil
445: "Command used to start the most recent Forth process.")
446:
447: (defvar forth-previous-send ""
448: "Most recent expression transmitted to the Forth process.")
449:
450: (defvar forth-process-filter-queue '()
451: "Queue used to synchronize filter actions properly.")
452:
453: (defvar forth-prompt "ok"
454: "The current forth prompt string.")
455:
456: (defvar forth-start-hook nil
457: "If non-nil, a procedure to call when the Forth process is started.
458: When called, the current buffer will be the Forth process-buffer.")
459:
460: (defvar forth-signal-death-message nil
461: "If non-nil, causes a message to be generated when the Forth process dies.")
462:
1.9 anton 463: (defvar forth-percent-height 50
1.1 anton 464: "Tells run-forth how high the upper window should be in percent.")
465:
466: (defconst forth-runlight:input ?I
467: "The character displayed when the Forth process is waiting for input.")
468:
469: (defvar forth-mode-string ""
470: "String displayed in the mode line when the Forth process is running.")
471:
472: ;;;; Evaluation Commands
473:
474: (defun forth-send-string (&rest strings)
475: "Send the string arguments to the Forth process.
476: The strings are concatenated and terminated by a newline."
477: (cond ((forth-process-running-p)
478: (forth-send-string-1 strings))
479: ((yes-or-no-p "The Forth process has died. Reset it? ")
480: (reset-forth)
481: (goto-char (point-max))
482: (forth-send-string-1 strings))))
483:
484: (defun forth-send-string-1 (strings)
485: (let ((string (apply 'concat strings)))
486: (forth-send-string-2 string)))
487:
488: (defun forth-send-string-2 (string)
489: (let ((process (get-process forth-program-name)))
490: (if (not (eq (current-buffer) (get-buffer forth-program-name)))
491: (progn
492: (forth-process-filter-output string)
493: (forth-process-filter:finish)))
494: (send-string process (concat string "\n"))
495: (if (eq (current-buffer) (process-buffer process))
496: (set-marker (process-mark process) (point)))))
497:
498:
499: (defun forth-send-region (start end)
500: "Send the current region to the Forth process.
501: The region is sent terminated by a newline."
502: (interactive "r")
503: (let ((process (get-process forth-program-name)))
504: (if (and process (eq (current-buffer) (process-buffer process)))
505: (progn (goto-char end)
506: (set-marker (process-mark process) end))))
507: (forth-send-string "\n" (buffer-substring start end) "\n"))
508:
509: (defun forth-end-of-paragraph ()
510: (if (looking-at "[\t\n ]+") (skip-chars-backward "\t\n "))
511: (if (not (re-search-forward "\n[ \t]*\n" nil t))
512: (goto-char (point-max))))
513:
514: (defun forth-send-paragraph ()
515: "Send the current or the previous paragraph to the Forth process"
516: (interactive)
517: (let (end)
518: (save-excursion
519: (forth-end-of-paragraph)
520: (skip-chars-backward "\t\n ")
521: (setq end (point))
522: (if (re-search-backward "\n[ \t]*\n" nil t)
523: (setq start (point))
524: (goto-char (point-min)))
525: (skip-chars-forward "\t\n ")
526: (forth-send-region (point) end))))
527:
528: (defun forth-send-buffer ()
529: "Send the current buffer to the Forth process."
530: (interactive)
531: (if (eq (current-buffer) (forth-process-buffer))
532: (error "Not allowed to send this buffer's contents to Forth"))
533: (forth-send-region (point-min) (point-max)))
534:
535:
536: ;;;; Basic Process Control
537:
538: (defun forth-start-process (command-line)
539: (let ((buffer (get-buffer-create "*forth*")))
540: (let ((process (get-buffer-process buffer)))
541: (save-excursion
542: (set-buffer buffer)
543: (progn (if process (delete-process process))
544: (goto-char (point-max))
545: (setq mode-line-process '(": %s"))
546: (add-to-global-mode-string 'forth-mode-string)
547: (setq process
548: (apply 'start-process
549: (cons forth-program-name
550: (cons buffer
551: (forth-parse-command-line
552: command-line)))))
553: (set-marker (process-mark process) (point-max))
554: (forth-process-filter-initialize t)
555: (forth-modeline-initialize)
556: (set-process-sentinel process 'forth-process-sentinel)
557: (set-process-filter process 'forth-process-filter)
558: (run-hooks 'forth-start-hook)))
559: buffer)))
560:
561: (defun forth-parse-command-line (string)
562: (setq string (substitute-in-file-name string))
563: (let ((start 0)
564: (result '()))
565: (while start
566: (let ((index (string-match "[ \t]" string start)))
567: (setq start
568: (cond ((not index)
569: (setq result
570: (cons (substring string start)
571: result))
572: nil)
573: ((= index start)
574: (string-match "[^ \t]" string start))
575: (t
576: (setq result
577: (cons (substring string start index)
578: result))
579: (1+ index))))))
580: (nreverse result)))
581:
582:
583: (defun forth-process-running-p ()
584: "True iff there is a Forth process whose status is `run'."
585: (let ((process (get-process forth-program-name)))
586: (and process
587: (eq (process-status process) 'run))))
588:
589: (defun forth-process-buffer ()
590: (let ((process (get-process forth-program-name)))
591: (and process (process-buffer process))))
592:
593: ;;;; Process Filter
594:
595: (defun forth-process-sentinel (proc reason)
596: (let ((inhibit-quit nil))
597: (forth-process-filter-initialize (eq reason 'run))
598: (if (eq reason 'run)
599: (forth-modeline-initialize)
600: (setq forth-mode-string "")))
601: (if (and (not (memq reason '(run stop)))
602: forth-signal-death-message)
603: (progn (beep)
604: (message
605: "The Forth process has died! Do M-x reset-forth to restart it"))))
606:
607: (defun forth-process-filter-initialize (running-p)
608: (setq forth-process-filter-queue (cons '() '()))
609: (setq forth-prompt "ok"))
610:
611:
612: (defun forth-process-filter (proc string)
613: (forth-process-filter-output string)
614: (forth-process-filter:finish))
615:
616: (defun forth-process-filter:enqueue (action)
617: (let ((next (cons action '())))
618: (if (cdr forth-process-filter-queue)
619: (setcdr (cdr forth-process-filter-queue) next)
620: (setcar forth-process-filter-queue next))
621: (setcdr forth-process-filter-queue next)))
622:
623: (defun forth-process-filter:finish ()
624: (while (car forth-process-filter-queue)
625: (let ((next (car forth-process-filter-queue)))
626: (setcar forth-process-filter-queue (cdr next))
627: (if (not (cdr next))
628: (setcdr forth-process-filter-queue '()))
629: (apply (car (car next)) (cdr (car next))))))
630:
631: ;;;; Process Filter Output
632:
633: (defun forth-process-filter-output (&rest args)
634: (if (not (and args
635: (null (cdr args))
636: (stringp (car args))
637: (string-equal "" (car args))))
638: (forth-process-filter:enqueue
639: (cons 'forth-process-filter-output-1 args))))
640:
641: (defun forth-process-filter-output-1 (&rest args)
642: (save-excursion
643: (forth-goto-output-point)
644: (apply 'insert-before-markers args)))
645:
646: (defun forth-guarantee-newlines (n)
647: (save-excursion
648: (forth-goto-output-point)
649: (let ((stop nil))
650: (while (and (not stop)
651: (bolp))
652: (setq n (1- n))
653: (if (bobp)
654: (setq stop t)
655: (backward-char))))
656: (forth-goto-output-point)
657: (while (> n 0)
658: (insert-before-markers ?\n)
659: (setq n (1- n)))))
660:
661: (defun forth-goto-output-point ()
662: (let ((process (get-process forth-program-name)))
663: (set-buffer (process-buffer process))
664: (goto-char (process-mark process))))
665:
666: (defun forth-modeline-initialize ()
667: (setq forth-mode-string " "))
668:
669: (defun forth-set-runlight (runlight)
670: (aset forth-mode-string 0 runlight)
671: (forth-modeline-redisplay))
672:
673: (defun forth-modeline-redisplay ()
674: (save-excursion (set-buffer (other-buffer)))
675: (set-buffer-modified-p (buffer-modified-p))
676: (sit-for 0))
677:
678: ;;;; Process Filter Operations
679:
680: (defun add-to-global-mode-string (x)
681: (cond ((null global-mode-string)
682: (setq global-mode-string (list "" x " ")))
683: ((not (memq x global-mode-string))
684: (setq global-mode-string
685: (cons ""
686: (cons x
687: (cons " "
688: (if (equal "" (car global-mode-string))
689: (cdr global-mode-string)
690: global-mode-string))))))))
691:
692:
693: ;; Misc
694:
695: (setq auto-mode-alist (append auto-mode-alist
1.4 pazsan 696: '(("\\.fs$" . forth-mode))))
1.1 anton 697:
698: (defun forth-split ()
699: (interactive)
700: (forth-split-1 "*forth*"))
701:
702: (defun forth-split-1 (buffer)
703: (if (not (eq (window-buffer) (get-buffer buffer)))
704: (progn
705: (delete-other-windows)
706: (split-window-vertically
707: (/ (* (screen-height) forth-percent-height) 100))
708: (other-window 1)
709: (switch-to-buffer buffer)
710: (goto-char (point-max))
711: (other-window 1))))
712:
713: (defun forth-reload ()
714: (interactive)
715: (let ((process (get-process forth-program-name)))
716: (if process (kill-process process t)))
1.27 ! pazsan 717: (sleep-for-millisecs 100)
1.1 anton 718: (forth-mode))
719:
720:
721: ;; Special section for forth-help
722:
723: (defvar forth-help-buffer "*Forth-help*"
724: "Buffer used to display the requested documentation.")
725:
726: (defvar forth-help-load-path nil
727: "List of directories to search through to find *.doc
728: (forth-help-file-suffix) files. Nil means current default directory.
729: The specified directories must contain at least one .doc file. If it
730: does not and you still want the load-path to scan that directory, create
731: an empty file dummy.doc.")
732:
733: (defvar forth-help-file-suffix "*.doc"
734: "The file names to search for in each directory.")
735:
736: (setq forth-search-command-prefix "grep -n \"^ [^(]* ")
737: (defvar forth-search-command-suffix "/dev/null")
738: (defvar forth-grep-error-regexp ": No such file or directory")
739:
740: (defun forth-function-called-at-point ()
741: "Return the space delimited word a point."
742: (save-excursion
743: (save-restriction
744: (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
745: (skip-chars-backward "^ \t\n" (point-min))
746: (if (looking-at "[ \t\n]")
747: (forward-char 1))
748: (let (obj (p (point)))
749: (skip-chars-forward "^ \t\n")
750: (buffer-substring p (point))))))
751:
752: (defun forth-help-names-extend-comp (path-list result)
753: (cond ((null path-list) result)
754: ((null (car path-list))
755: (forth-help-names-extend-comp (cdr path-list)
756: (concat result forth-help-file-suffix " ")))
757: (t (forth-help-names-extend-comp
758: (cdr path-list) (concat result
759: (expand-file-name (car path-list)) "/"
760: forth-help-file-suffix " ")))))
761:
762: (defun forth-help-names-extended ()
763: (if forth-help-load-path
764: (forth-help-names-extend-comp forth-help-load-path "")
765: (error "forth-help-load-path not specified")))
766:
767:
1.7 anton 768: ;(define-key forth-mode-map "\C-hf" 'forth-documentation)
1.1 anton 769:
770: (defun forth-documentation (function)
771: "Display the full documentation of FORTH word."
772: (interactive
773: (let ((fn (forth-function-called-at-point))
774: (enable-recursive-minibuffers t)
775: search-list
776: val)
777: (setq val (read-string (format "Describe forth word (default %s): " fn)))
778: (list (if (equal val "") fn val))))
779: (forth-get-doc (concat forth-search-command-prefix
780: (grep-regexp-quote (concat function " ("))
781: "[^)]*\-\-\" " (forth-help-names-extended)
782: forth-search-command-suffix))
783: (message "C-x C-m switches back to the forth interaction window"))
784:
785: (defun forth-get-doc (command)
786: "Display the full documentation of command."
787: (let ((curwin (get-buffer-window (window-buffer)))
788: reswin
789: pointmax)
790: (with-output-to-temp-buffer forth-help-buffer
791: (progn
792: (call-process "sh" nil forth-help-buffer t "-c" command)
793: (setq reswin (get-buffer-window forth-help-buffer))))
794: (setq reswin (get-buffer-window forth-help-buffer))
795: (select-window reswin)
796: (save-excursion
797: (goto-char (setq pointmax (point-max)))
798: (insert "--------------------\n\n"))
799: (let (fd doc)
800: (while (setq fd (forth-get-file-data pointmax))
801: (setq doc (forth-get-doc-string fd))
802: (save-excursion
803: (goto-char (point-max))
804: (insert (substring (car fd) (string-match "[^/]*$" (car fd)))
805: ":\n\n" doc "\n")))
806: (if (not doc)
807: (progn (goto-char (point-max)) (insert "Not found"))))
808: (select-window curwin)))
809:
810: (defun forth-skip-error-lines ()
811: (let ((lines 0))
812: (save-excursion
813: (while (re-search-forward forth-grep-error-regexp nil t)
814: (beginning-of-line)
815: (forward-line 1)
816: (setq lines (1+ lines))))
817: (forward-line lines)))
818:
819: (defun forth-get-doc-string (fd)
820: "Find file (car fd) and extract documentation from line (nth 1 fd)."
821: (let (result)
822: (save-window-excursion
823: (find-file (car fd))
824: (goto-line (nth 1 fd))
825: (if (not (eq (nth 1 fd) (1+ (count-lines (point-min) (point)))))
826: (error "forth-get-doc-string: serious error"))
827: (if (not (re-search-backward "\n[\t ]*\n" nil t))
828: (goto-char (point-min))
829: (goto-char (match-end 0)))
830: (let ((p (point)))
831: (if (not (re-search-forward "\n[\t ]*\n" nil t))
832: (goto-char (point-max)))
833: (setq result (buffer-substring p (point))))
834: (bury-buffer (current-buffer)))
835: result))
836:
837: (defun forth-get-file-data (limit)
838: "Parse grep output and return '(filename line#) list. Return nil when
839: passing limit."
840: (forth-skip-error-lines)
841: (if (< (point) limit)
842: (let ((result (forth-get-file-data-cont limit)))
843: (forward-line 1)
844: (beginning-of-line)
845: result)))
846:
847: (defun forth-get-file-data-cont (limit)
848: (let (result)
849: (let ((p (point)))
850: (skip-chars-forward "^:")
851: (setq result (buffer-substring p (point))))
852: (if (< (point) limit)
853: (let ((p (1+ (point))))
854: (forward-char 1)
855: (skip-chars-forward "^:")
856: (list result (string-to-int (buffer-substring p (point))))))))
857:
858: (defun grep-regexp-quote (str)
859: (let ((i 0) (m 1) (res ""))
860: (while (/= m 0)
861: (setq m (string-to-char (substring str i)))
862: (if (/= m 0)
863: (progn
864: (setq i (1+ i))
865: (if (string-match (regexp-quote (char-to-string m))
866: ".*\\^$[]")
867: (setq res (concat res "\\")))
868: (setq res (concat res (char-to-string m))))))
869: res))
870:
871:
1.9 anton 872: (define-key forth-mode-map "\C-x\C-e" 'compile)
1.1 anton 873: (define-key forth-mode-map "\C-x\C-n" 'next-error)
874: (require 'compile "compile")
875:
1.6 anton 876: (defvar forth-compile-command "gforth ")
1.9 anton 877: ;(defvar forth-compilation-window-percent-height 30)
1.1 anton 878:
879: (defun forth-compile (command)
880: (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
881: (forth-split-1 "*compilation*")
882: (setq ctools-compile-command command)
883: (compile1 ctools-compile-command "No more errors"))
884:
885:
1.12 pazsan 886: ;;; Forth menu
887: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
888:
889: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
890: (require 'func-menu)
891:
892: (defconst fume-function-name-regexp-forth
893: "^\\(:\\)[ \t]+\\([^ \t]*\\)"
894: "Expression to get word definitions in Forth.")
895:
896: (setq fume-function-name-regexp-alist
897: (append '((forth-mode . fume-function-name-regexp-forth)
898: ) fume-function-name-regexp-alist))
899:
900: ;; Find next forth word in the buffer
901: (defun fume-find-next-forth-function-name (buffer)
902: "Searches for the next forth word in BUFFER."
903: (set-buffer buffer)
904: (if (re-search-forward fume-function-name-regexp nil t)
905: (let ((beg (match-beginning 2))
906: (end (match-end 2)))
907: (cons (buffer-substring beg end) beg))))
908:
909: (setq fume-find-function-name-method-alist
910: (append '((forth-mode . fume-find-next-forth-function-name))))
911:
912: ))
913: ;;; End Forth menu
914:
915: ;;; File folding of forth-files
916: ;;; uses outline
917: ;;; Toggle activation with M-x fold-f (when editing a forth-file)
918: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
919: ;;;
920: ;;; Works most of the times but loses sync with the cursor occasionally
921: ;;; Could be improved by also folding on comments
922:
923: (require 'outline)
924:
1.27 ! pazsan 925: (defun f-outline-level ()
! 926: (cond ((looking-at "\\`\\\\")
! 927: 0)
! 928: ((looking-at "\\\\ SEC")
! 929: 0)
! 930: ((looking-at "\\\\ \\\\ .*")
! 931: 0)
! 932: ((looking-at "\\\\ DEFS")
! 933: 1)
! 934: ((looking-at "\\/\\* ")
! 935: 1)
! 936: ((looking-at ":")
! 937: 1)
! 938: ((looking-at "\\\\G")
! 939: 2)
! 940: ((looking-at "[ \t]+\\\\")
! 941: 3))
! 942: )
1.12 pazsan 943:
944: (defun fold-f ()
945: (interactive)
946: (add-hook 'outline-minor-mode-hook 'hide-body)
947:
948: ; outline mode header start, i.e. find word definitions
1.27 ! pazsan 949: ;;; (setq outline-regexp "^\\(:\\)[ \t]+\\([^ \t]*\\)")
! 950: (setq outline-regexp "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
! 951: (setq outline-level 'f-outline-level)
1.12 pazsan 952:
953: (outline-minor-mode)
1.27 ! pazsan 954: (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
! 955: (define-key outline-minor-mode-map '(shift right) 'show-children)
! 956: (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
! 957: (define-key outline-minor-mode-map '(shift down) 'show-subtree)
! 958:
1.12 pazsan 959: )
1.27 ! pazsan 960:
! 961: (define-key global-map '(shift up) 'fold-f)
! 962:
1.12 pazsan 963: ;;; end file folding
964:
965: ;;; func-menu is a package that scans your source file for function definitions
966: ;;; and makes a menubar entry that lets you jump to any particular function
967: ;;; definition by selecting it from the menu. The following code turns this on
968: ;;; for all of the recognized languages. Scanning the buffer takes some time,
969: ;;; but not much.
970: ;;;
971: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
972: (require 'func-menu)
973: ;; (define-key global-map 'f8 'function-menu)
974: (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
975: (define-key global-map "\C-cg" 'fume-prompt-function-goto)
976: (define-key global-map '(shift button3) 'mouse-function-menu)
1.27 ! pazsan 977: ))
! 978:
! 979: ;; end
! 980:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>