File:  [gforth] / gforth / gforth.el
Revision 1.29: download - view: text, annotated - select for diffs
Sat May 9 21:27:16 1998 UTC (23 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    1: ;; Forth mode for Emacs
    2: ;; This file is part of GForth.
    3: ;; Changes by anton
    4: ;; This is a variant of forth.el that came with TILE.
    5: ;; I left most of this stuff untouched and made just a few changes for 
    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.
    8: 
    9: ;; GForth is distributed in the hope that it will be useful,
   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
   19: ;; supposed to have been given to you along with Gforth so you
   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
   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 "
   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
   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: "
   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
   51:   " : :noname code interpretation: public: private: how: implements class class; "
   52:   "Contains all words which causes the indent to go to zero")
   53: 
   54: (setq forth-zero 0)
   55: 
   56: (defvar forth-zup
   57:   " how: implements "
   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: 
   64: (defvar forth-prefixes
   65:   " postpone [compile] ['] [char] "
   66:   "words that prefix and escape other words")
   67: 
   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: 
   79: ;(define-key forth-mode-map "\M-\C-x" 'compile)
   80: (define-key forth-mode-map "\C-x\\" 'comment-region)
   81: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
   82: (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph)
   83: (define-key forth-mode-map "\eo" 'forth-send-buffer)
   84: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
   85: (define-key forth-mode-map "\e " 'forth-reload)
   86: (define-key forth-mode-map "\t" 'forth-indent-command)
   87: (define-key forth-mode-map "\C-m" 'reindent-then-newline-and-indent)
   88: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
   89: (define-key forth-mode-map "\e." 'forth-find-tag)
   90: 
   91: (load "etags")
   92: 
   93: (defun forth-find-tag (tagname &optional next-p regexp-p)
   94:   (interactive (find-tag-interactive "Find tag: "))
   95:   (switch-to-buffer
   96:    (find-tag-noselect (concat " " tagname " ") next-p regexp-p)))
   97: 
   98: (defvar forth-mode-syntax-table nil
   99:   "Syntax table in use in Forth-mode buffers.")
  100: 
  101: (if (not forth-mode-syntax-table)
  102:     (progn
  103:       (setq forth-mode-syntax-table (make-syntax-table))
  104:       (let ((char 0))
  105: 	(while (< char ?!)
  106: 	  (modify-syntax-entry char " " forth-mode-syntax-table)
  107: 	  (setq char (1+ char)))
  108: 	(while (< char 256)
  109: 	  (modify-syntax-entry char "w" forth-mode-syntax-table)
  110: 	  (setq char (1+ char))))
  111:       (modify-syntax-entry ?\" "\"" forth-mode-syntax-table)
  112:       (modify-syntax-entry ?\\ "<" forth-mode-syntax-table)
  113:       (modify-syntax-entry ?\n ">" forth-mode-syntax-table)
  114:       ))
  115: ;I do not define '(' and ')' as comment delimiters, because emacs
  116: ;only supports one comment syntax (and a hack to accomodate C++); I
  117: ;use '\' for natural language comments and '(' for formal comments
  118: ;like stack comments, so for me it's better to have emacs treat '\'
  119: ;comments as comments. If you want it different, make the appropriate
  120: ;changes (best in your .emacs file).
  121: ;
  122: ;Hmm, the C++ hack could be used to support both comment syntaxes: we
  123: ;can have different comment styles, if both comments start with the
  124: ;same character. we could use ' ' as first and '(' and '\' as second
  125: ;character. However this would fail for G\ comments.
  126: 
  127: (defconst forth-indent-level 4
  128:   "Indentation of Forth statements.")
  129: 
  130: (defun forth-mode-variables ()
  131:   (set-syntax-table forth-mode-syntax-table)
  132:   (setq local-abbrev-table forth-mode-abbrev-table)
  133:   (make-local-variable 'paragraph-start)
  134:   (setq paragraph-start (concat "^$\\|" page-delimiter))
  135:   (make-local-variable 'paragraph-separate)
  136:   (setq paragraph-separate paragraph-start)
  137:   (make-local-variable 'indent-line-function)
  138:   (setq indent-line-function 'forth-indent-line)
  139: ;  (make-local-variable 'require-final-newline)
  140: ;  (setq require-final-newline t)
  141:   (make-local-variable 'comment-start)
  142:   (setq comment-start "\\ ")
  143:   ;(make-local-variable 'comment-end)
  144:   ;(setq comment-end " )")
  145:   (make-local-variable 'comment-column)
  146:   (setq comment-column 40)
  147:   (make-local-variable 'comment-start-skip)
  148:   (setq comment-start-skip "\\ ")
  149:   (make-local-variable 'comment-indent-hook)
  150:   (setq comment-indent-hook 'forth-comment-indent)
  151:   (make-local-variable 'parse-sexp-ignore-comments)
  152:   (setq parse-sexp-ignore-comments t))
  153:   
  154: ;;;###autoload
  155: (defun forth-mode ()
  156:   "
  157: Major mode for editing Forth code. Tab indents for Forth code. Comments
  158: are delimited with \\ and newline. Paragraphs are separated by blank lines
  159: only.
  160: \\{forth-mode-map}
  161:  Forth-split
  162:     Positions the current buffer on top and a forth-interaction window
  163:     below. The window size is controlled by the forth-percent-height
  164:     variable (see below).
  165:  Forth-reload
  166:     Reloads the forth library and restarts the forth process.
  167:  Forth-send-buffer
  168:     Sends the current buffer, in text representation, as input to the
  169:     forth process.
  170:  Forth-send-paragraph
  171:     Sends the previous or the current paragraph to the forth-process.
  172:     Note that the cursor only need to be with in the paragraph to be sent.
  173: forth-documentation
  174:     Search for documentation of forward adjacent to cursor. Note! To use
  175:     this mode you have to add a line, to your .emacs file, defining the
  176:     directories to search through for documentation files (se variable
  177:     forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
  178: 
  179: Variables controlling interaction and startup
  180:  forth-percent-height
  181:     Tells split how high to make the edit portion, in percent of the
  182:     current screen height.
  183:  forth-program-name
  184:     Tells the library which program name to execute in the interation
  185:     window.
  186: 
  187: Variables controlling indentation style:
  188:  forth-positives
  189:     A string containing all words which causes the indent-level of the
  190:     following line to be incremented.
  191:     OBS! Each word must be surronded by spaces.
  192:  forth-negatives
  193:     A string containing all words which causes the indentation of the
  194:     current line to be decremented, if the word begin the line. These
  195:     words also has a cancelling effect on the indent-level of the
  196:     following line, independent of position.
  197:     OBS! Each word must be surronded by spaces.
  198:  forth-zeroes
  199:     A string containing all words which causes the indentation of the
  200:     current line to go to zero, if the word begin the line.
  201:     OBS! Each word must be surronded by spaces.
  202:  forth-indent-level
  203:     Indentation increment/decrement of Forth statements.
  204: 
  205:  Note! A word which decrements the indentation of the current line, may
  206:     also be mentioned in forth-positives to cause the indentation to
  207:     resume the previous level.
  208: 
  209: Variables controling documentation search
  210:  forth-help-load-path
  211:     List of directories to search through to find *.doc
  212:     (forth-help-file-suffix) files. Nil means current default directory.
  213:     The specified directories must contain at least one .doc file. If it
  214:     does not and you still want the load-path to scan that directory, create
  215:     an empty file dummy.doc.
  216:  forth-help-file-suffix
  217:     The file names to search for in each directory specified by
  218:     forth-help-load-path. Defaulted to '*.doc'. 
  219: "
  220:   (interactive)
  221:   (kill-all-local-variables)
  222:   (use-local-map forth-mode-map)
  223:   (setq mode-name "Forth")
  224:   (setq major-mode 'forth-mode)
  225:   (forth-mode-variables)
  226: ;  (if (not (forth-process-running-p))
  227: ;      (run-forth forth-program-name))
  228:   (run-hooks 'forth-mode-hook))
  229: 
  230: (setq forth-mode-hook
  231:       '(lambda () 
  232: 	 (make-local-variable 'compile-command)
  233: 	 (setq compile-command "gforth ")))
  234: 
  235: (defun forth-fill-paragraph () 
  236:   "Fill comments (starting with '\'; do not fill code (block style
  237: programmers who tend to fill code won't use emacs anyway:-)."
  238:   ; Currently only comments at the start of the line are filled.
  239:   ; Something like lisp-fill-paragraph may be better.  We cannot use
  240:   ; fill-paragraph, because it removes the \ from the first comment
  241:   ; line. Therefore we have to look for the first line of the comment
  242:   ; and use fill-region.
  243:   (interactive)
  244:   (save-excursion
  245:     (beginning-of-line)
  246:     (while (and
  247: 	     (= (forward-line -1) 0)
  248: 	     (looking-at "[ \t]*\\\\g?[ \t]+")))
  249:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
  250: 	(forward-line 1))
  251:     (let ((from (point))
  252: 	  (to (save-excursion (forward-paragraph) (point))))
  253:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
  254: 	  (progn (goto-char (match-end 0))
  255: 		 (set-fill-prefix)
  256: 		 (fill-region from to nil))))))
  257: 
  258: (defun forth-comment-indent ()
  259:   (save-excursion
  260:     (beginning-of-line)
  261:     (if (looking-at ":[ \t]*")
  262: 	(progn
  263: 	  (end-of-line)
  264: 	  (skip-chars-backward " \t\n")
  265: 	  (1+ (current-column)))
  266:       comment-column)))
  267: 
  268: (defun forth-current-indentation ()
  269:   (save-excursion
  270:     (beginning-of-line)
  271:     (back-to-indentation)
  272:     (current-column)))
  273: 
  274: (defun forth-delete-indentation ()
  275:   (let ((b nil) (m nil))
  276:     (save-excursion
  277:       (beginning-of-line)
  278:       (setq b (point))
  279:       (back-to-indentation)
  280:       (setq m (point)))
  281:     (delete-region b m)))
  282: 
  283: (defun forth-indent-line (&optional flag)
  284:   "Correct indentation of the current Forth line."
  285:   (let ((x (forth-calculate-indent)))
  286:     (forth-indent-to x)))
  287:   
  288: (defun forth-indent-command ()
  289:   (interactive)
  290:   (forth-indent-line t))
  291: 
  292: (defun forth-indent-to (x)
  293:   (let ((p nil))
  294:     (setq p (- (current-column) (forth-current-indentation)))
  295:     (forth-delete-indentation)
  296:     (beginning-of-line)
  297:     (indent-to x)
  298:     (if (> p 0) (forward-char p))))
  299: 
  300: ;;Calculate indent
  301: (defun forth-calculate-indent ()
  302:   (let ((w1 nil) (indent 0) (centre 0))
  303:     (save-excursion
  304:       (beginning-of-line)
  305:       (skip-chars-backward " \t\n")
  306:       (beginning-of-line)
  307:       (back-to-indentation)
  308:       (setq indent (current-column))
  309:       (setq centre indent)
  310:       (setq indent (+ indent (forth-sum-line-indentation))))
  311:     (save-excursion
  312:       (beginning-of-line)
  313:       (back-to-indentation)
  314:       (let ((p (point)))
  315: 	(skip-chars-forward "^ \t\n")
  316: 	(setq w1 (buffer-substring p (point)))))
  317:     (if (> (- indent centre) forth-indent-level)
  318: 	(setq indent (+ centre forth-indent-level)))
  319:     (if (> (- centre indent) forth-indent-level)
  320: 	(setq indent (- centre forth-indent-level)))
  321:     (if (< indent 0) (setq indent 0))
  322:     (setq indent (- indent
  323: 		    (if (string-match 
  324: 			 (regexp-quote (concat " " w1 " "))
  325: 			 forth-negatives)
  326: 			forth-indent-level 0)))
  327:     (if (string-match (regexp-quote (concat " " w1 " ")) forth-zdown)
  328: 	(setq forth-zero 0))
  329:     (if (string-match (regexp-quote (concat " " w1 " ")) forth-zeroes)
  330: 	(setq indent forth-zero))
  331:     (if (string-match (regexp-quote (concat " " w1 " ")) forth-zup)
  332: 	(setq forth-zero 4))
  333:     indent))
  334: 
  335: (defun forth-sum-line-indentation ()
  336:   "Add upp the positive and negative weights of all words on the current line."
  337:   (let ((b (point)) (e nil) (sum 0) (w nil) (t1 nil) (t2 nil) (first t))
  338:     (end-of-line) (setq e (point))
  339:     (goto-char b)
  340:     (while (< (point) e)
  341:       (setq w (forth-next-word))
  342:       (setq t1 (string-match (regexp-quote (concat " " w " "))
  343: 			     forth-positives))
  344:       (setq t2 (string-match (regexp-quote (concat " " w " "))
  345: 			     forth-negatives))
  346:       (if t1
  347: 	  (setq sum (+ sum forth-indent-level)))
  348:       (if (and t2 (not first))
  349: 	  (setq sum (- sum forth-indent-level)))
  350:       (skip-chars-forward " \t")
  351:       (setq first nil))
  352:     sum))
  353: 
  354: 
  355: (defun forth-next-word ()
  356:   "Return the next forth-word. Skip anything that the forth-word takes from
  357: the input stream (comments, arguments, etc.)"
  358: ;actually, it would be better to use commands based on the
  359: ;syntax-table or comment-start etc.
  360:   (let ((w1 nil))
  361:     (while (not w1)
  362:       (skip-chars-forward " \t\n")
  363:       (let ((p (point)))
  364: 	(skip-chars-forward "^ \t\n")
  365: 	(setq w1 (buffer-substring p (point))))
  366:       (cond ((string-match "\"" w1)
  367: 	     (progn
  368: 	       (skip-chars-forward "^\"\n")
  369: 	       (forward-char)))
  370: 	    ((string-match "\\\\" w1)
  371: 	     (progn
  372: 	       (end-of-line)
  373: 	       ))
  374: 	    ((or (equal "(" w1) (equal ".(" w1))
  375: 	     (progn
  376: 	       (skip-chars-forward "^)\n")
  377: 	       (forward-char)))
  378: 	    ((string-match (regexp-quote (concat " " w1 " ")) forth-prefixes)
  379: 	     (progn (skip-chars-forward " \t\n")
  380: 		    (skip-chars-forward "^ \t\n")))
  381: 	    (t nil)))
  382:     w1))
  383:       
  384: 
  385: ;; Forth commands
  386: 
  387: (defun forth-remove-tracers ()
  388:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
  389:   (interactive)
  390:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
  391: 
  392: (defvar forth-program-name "gforth"
  393:   "*Program invoked by the `run-forth' command.")
  394: 
  395: (defvar forth-band-name nil
  396:   "*Band loaded by the `run-forth' command.")
  397: 
  398: (defvar forth-program-arguments nil
  399:   "*Arguments passed to the Forth program by the `run-forth' command.")
  400: 
  401: (defun run-forth (command-line)
  402:   "Run an inferior Forth process. Output goes to the buffer `*forth*'.
  403: With argument, asks for a command line. Split up screen and run forth 
  404: in the lower portion. The current-buffer when called will stay in the
  405: upper portion of the screen, and all other windows are deleted.
  406: Call run-forth again to make the *forth* buffer appear in the lower
  407: part of the screen."
  408:   (interactive
  409:    (list (let ((default
  410: 		 (or forth-process-command-line
  411: 		     (forth-default-command-line))))
  412: 	   (if current-prefix-arg
  413: 	       (read-string "Run Forth: " default)
  414: 	       default))))
  415:   (setq forth-process-command-line command-line)
  416:   (forth-start-process command-line)
  417:   (forth-split)
  418:   (forth-set-runlight forth-runlight:input))
  419: 
  420: (defun run-forth-if-not ()
  421:   (if (not (forth-process-running-p))
  422:       (run-forth forth-program-name)))
  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 ()
  437:   (concat forth-program-name
  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: 
  463: (defvar forth-percent-height 50
  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
  696: 				'(("\\.fs$" . forth-mode))))
  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)))
  717:   (sleep-for 0 100)
  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: 
  768: ;(define-key forth-mode-map "\C-hf" 'forth-documentation)
  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: 
  872: (define-key forth-mode-map "\C-x\C-e" 'compile)
  873: (define-key forth-mode-map "\C-x\C-n" 'next-error)
  874: (require 'compile "compile")
  875: 
  876: (defvar forth-compile-command "gforth ")
  877: ;(defvar forth-compilation-window-percent-height 30)
  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: 
  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: 
  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: )			
  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
  949: ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
  950:    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
  951:    (setq outline-level 'f-outline-level)
  952: 
  953:    (outline-minor-mode)
  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: 
  959: )
  960: 
  961: ;;(define-key global-map '(shift up) 'fold-f)
  962: 
  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)
  977: ))
  978: 
  979: ;; end
  980: 

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