home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / cmuscheme.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  18.3 KB  |  459 lines

  1. ;;; cmuscheme.el --- Scheme process in a buffer. Adapted from tea.el.
  2.  
  3. ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Olin Shivers <olin.shivers@cs.cmu.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: processes, lisp
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Synched up with: FSF 19.28.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;;;    This is a customisation of comint-mode (see comint.el)
  30. ;;;
  31. ;;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  32. ;;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  33. ;;; 8/88
  34. ;;;
  35. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  36. ;;; merge them into the master source.
  37. ;;;
  38. ;;; The changelog is at the end of this file.
  39. ;;;
  40. ;;; NOTE: MIT Cscheme, when invoked with the -emacs flag, has a special user
  41. ;;; interface that communicates process state back to the superior emacs by
  42. ;;; outputting special control sequences. The gnumacs package, xscheme.el, has
  43. ;;; lots and lots of special purpose code to read these control sequences, and
  44. ;;; so is very tightly integrated with the cscheme process. The cscheme
  45. ;;; interrupt handler and debugger read single character commands in cbreak
  46. ;;; mode; when this happens, xscheme.el switches to special keymaps that bind
  47. ;;; the single letter command keys to emacs functions that directly send the
  48. ;;; character to the scheme process.  Cmuscheme mode does *not* provide this
  49. ;;; functionality. If you are a cscheme user, you may prefer to use the
  50. ;;; xscheme.el/cscheme -emacs interaction.
  51. ;;; 
  52. ;;; Here's a summary of the pros and cons, as I see them.
  53. ;;; xscheme: Tightly integrated with inferior cscheme process!  A few commands
  54. ;;;         not in cmuscheme. But. Integration is a bit of a hack.  Input
  55. ;;;         history only keeps the immediately prior input. Bizarre
  56. ;;;         keybindings.
  57. ;;; 
  58. ;;; cmuscheme: Not tightly integrated with inferior cscheme process.  But.
  59. ;;;            Carefully integrated functionality with the entire suite of
  60. ;;;            comint-derived CMU process modes. Keybindings reminiscent of
  61. ;;;            Zwei and Hemlock. Good input history. A few commands not in
  62. ;;;            xscheme.
  63. ;;;  
  64. ;;; It's a tradeoff. Pay your money; take your choice. If you use a Scheme
  65. ;;; that isn't Cscheme, of course, there isn't a choice. Xscheme.el is *very*
  66. ;;; Cscheme-specific; you must use cmuscheme.el.  Interested parties are
  67. ;;; invited to port xscheme functionality on top of comint mode...
  68.  
  69. ;; YOUR .EMACS FILE
  70. ;;=============================================================================
  71. ;; Some suggestions for your .emacs file.
  72. ;;
  73. ;; ; If cmuscheme lives in some non-standard directory, you must tell emacs
  74. ;; ; where to get it. This may or may not be necessary.
  75. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  76. ;;
  77. ;; ; Autoload run-scheme from file cmuscheme.el
  78. ;; (autoload 'run-scheme "cmuscheme"
  79. ;;           "Run an inferior Scheme process."
  80. ;;           t)
  81. ;;
  82. ;; ; Files ending in ".scm" are Scheme source, 
  83. ;; ; so put their buffers in scheme-mode.
  84. ;; (setq auto-mode-alist 
  85. ;;       (cons '("\\.scm$" . scheme-mode)  
  86. ;;             auto-mode-alist))
  87. ;;
  88. ;; ; Define C-c t to run my favorite command in inferior scheme mode:
  89. ;; (setq cmuscheme-load-hook
  90. ;;       '((lambda () (define-key inferior-scheme-mode-map "\C-ct"
  91. ;;                                'favorite-cmd))))
  92. ;;;
  93. ;;; Unfortunately, scheme.el defines run-scheme to autoload from xscheme.el.
  94. ;;; This will womp your declaration to autoload run-scheme from cmuscheme.el
  95. ;;; if you haven't loaded cmuscheme in before scheme. Three fixes:
  96. ;;; - Put the autoload on your scheme mode hook and in your .emacs toplevel:
  97. ;;;   (setq scheme-mode-hook
  98. ;;;         '((lambda () (autoload 'run-scheme "cmuscheme"
  99. ;;;                                "Run an inferior Scheme" t))))
  100. ;;;   (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  101. ;;;   Now when scheme.el autoloads, it will restore the run-scheme autoload.
  102. ;;; - Load cmuscheme.el in your .emacs: (load-library 'cmuscheme)
  103. ;;; - Change autoload declaration in scheme.el to point to cmuscheme.el:
  104. ;;;   (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  105. ;;;   *or* just delete the autoload declaration from scheme.el altogether,
  106. ;;;   which will allow the autoload in your .emacs to have its say.
  107.  
  108. ;;; Code:
  109.  
  110. (require 'scheme)
  111. (require 'comint)
  112.  
  113. ;;; INFERIOR SCHEME MODE STUFF
  114. ;;;============================================================================
  115.  
  116. (defvar inferior-scheme-mode-hook nil
  117.   "*Hook for customising inferior-scheme mode.")
  118. (defvar inferior-scheme-mode-map nil)
  119.  
  120. (cond ((not inferior-scheme-mode-map)
  121.        (setq inferior-scheme-mode-map
  122.          (copy-keymap comint-mode-map))
  123.        (define-key inferior-scheme-mode-map "\M-\C-x" ;gnu convention
  124.                'scheme-send-definition)
  125.        (define-key inferior-scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp)
  126.        (define-key inferior-scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  127.        (define-key inferior-scheme-mode-map "\C-c\C-k" 'scheme-compile-file)
  128.        (scheme-mode-commands inferior-scheme-mode-map))) 
  129.  
  130. ;; Install the process communication commands in the scheme-mode keymap.
  131. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  132. (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
  133. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition)
  134. (define-key scheme-mode-map "\C-c\M-e" 'scheme-send-definition-and-go)
  135. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region)
  136. (define-key scheme-mode-map "\C-c\M-r" 'scheme-send-region-and-go)
  137. (define-key scheme-mode-map "\C-c\M-c" 'scheme-compile-definition)
  138. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  139. (define-key scheme-mode-map "\C-c\C-z" 'switch-to-scheme)
  140. (define-key scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  141. (define-key scheme-mode-map "\C-c\C-k" 'scheme-compile-file) ;k for "kompile"
  142.  
  143. (defvar scheme-buffer)
  144.  
  145. (defun inferior-scheme-mode ()
  146.   "Major mode for interacting with an inferior Scheme process.
  147.  
  148. The following commands are available:
  149. \\{inferior-scheme-mode-map}
  150.  
  151. A Scheme process can be fired up with M-x run-scheme.
  152.  
  153. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  154. inferior-scheme-mode-hook (in that order).
  155.  
  156. You can send text to the inferior Scheme process from other buffers containing
  157. Scheme source.  
  158.     switch-to-scheme switches the current buffer to the Scheme process buffer.
  159.     scheme-send-definition sends the current definition to the Scheme process.
  160.     scheme-compile-definition compiles the current definition.
  161.     scheme-send-region sends the current region to the Scheme process.
  162.     scheme-compile-region compiles the current region.
  163.  
  164.     scheme-send-definition-and-go, scheme-compile-definition-and-go,
  165.         scheme-send-region-and-go, and scheme-compile-region-and-go
  166.         switch to the Scheme process buffer after sending their text.
  167. For information on running multiple processes in multiple buffers, see
  168. documentation for variable scheme-buffer.
  169.  
  170. Commands:
  171. Return after the end of the process' output sends the text from the 
  172.     end of process to point.
  173. Return before the end of the process' output copies the sexp ending at point
  174.     to the end of the process' output, and sends it.
  175. Delete converts tabs to spaces as it moves back.
  176. Tab indents for Scheme; with argument, shifts rest
  177.     of expression rigidly with the current line.
  178. C-M-q does Tab on each line starting within following expression.
  179. Paragraphs are separated only by blank lines.  Semicolons start comments.
  180. If you accidentally suspend your process, use \\[comint-continue-subjob]
  181. to continue it."
  182.   (interactive)
  183.   (comint-mode)
  184.   ;; Customise in inferior-scheme-mode-hook
  185.   (setq comint-prompt-regexp "^[^>\n]*>+ *") ; OK for cscheme, oaklisp, T,...
  186.   (scheme-mode-variables)
  187.   (setq major-mode 'inferior-scheme-mode)
  188.   (setq mode-name "Inferior Scheme")
  189.   (setq mode-line-process '(":%s"))
  190.   (use-local-map inferior-scheme-mode-map)
  191.   (setq comint-input-filter (function scheme-input-filter))
  192.   (setq comint-get-old-input (function scheme-get-old-input))
  193.   (run-hooks 'inferior-scheme-mode-hook))
  194.  
  195. (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  196.   "*Input matching this regexp are not saved on the history list.
  197. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  198.  
  199. (defun scheme-input-filter (str)
  200.   "Don't save anything matching inferior-scheme-filter-regexp"
  201.   (not (string-match inferior-scheme-filter-regexp str)))
  202.  
  203. (defun scheme-get-old-input ()
  204.   "Snarf the sexp ending at point"
  205.   (save-excursion
  206.     (let ((end (point)))
  207.       (backward-sexp)
  208.       (buffer-substring (point) end))))
  209.  
  210. (defun scheme-args-to-list (string)
  211.   (let ((where (string-match "[ \t]" string)))
  212.     (cond ((null where) (list string))
  213.       ((not (= where 0))
  214.        (cons (substring string 0 where)
  215.          (scheme-args-to-list (substring string (+ 1 where)
  216.                          (length string)))))
  217.       (t (let ((pos (string-match "[^ \t]" string)))
  218.            (if (null pos)
  219.            nil
  220.          (scheme-args-to-list (substring string pos
  221.                          (length string)))))))))
  222.  
  223. (defvar scheme-program-name "scheme"
  224.   "*Program invoked by the run-scheme command")
  225.  
  226. ;;; Obsolete
  227. (defun scheme (&rest foo)
  228.   "Use run-scheme"
  229.   (interactive)
  230.   (message "Use run-scheme")
  231.   (ding))
  232.  
  233. (defun run-scheme (cmd)
  234.   "Run an inferior Scheme process, input and output via buffer *scheme*.
  235. If there is a process already running in *scheme*, just switch to that buffer.
  236. With argument, allows you to edit the command line (default is value
  237. of scheme-program-name).  Runs the hooks from inferior-scheme-mode-hook
  238. \(after the comint-mode-hook is run).
  239. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  240.  
  241.   (interactive (list (if current-prefix-arg
  242.              (read-string "Run Scheme: " scheme-program-name)
  243.              scheme-program-name)))
  244.   (if (not (comint-check-proc "*scheme*"))
  245.       (let ((cmdlist (scheme-args-to-list cmd)))
  246.     (set-buffer (apply 'make-comint "scheme" (car cmdlist)
  247.                nil (cdr cmdlist)))
  248.     (inferior-scheme-mode)))
  249.   (setq scheme-buffer "*scheme*")
  250.   (switch-to-buffer "*scheme*"))
  251.  
  252.  
  253. (defun scheme-send-region (start end)
  254.   "Send the current region to the inferior Scheme process."
  255.   (interactive "r")
  256.   (comint-send-region (scheme-proc) start end)
  257.   (comint-send-string (scheme-proc) "\n"))
  258.  
  259. (defun scheme-send-definition ()
  260.   "Send the current definition to the inferior Scheme process."
  261.   (interactive)
  262.   (save-excursion
  263.    (end-of-defun)
  264.    (let ((end (point)))
  265.      (beginning-of-defun)
  266.      (scheme-send-region (point) end))))
  267.  
  268. (defun scheme-send-last-sexp ()
  269.   "Send the previous sexp to the inferior Scheme process."
  270.   (interactive)
  271.   (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
  272.  
  273. (defvar scheme-compile-exp-command "(compile '%s)"
  274.   "*Template for issuing commands to compile arbitrary Scheme expressions.")
  275.  
  276. (defun scheme-compile-region (start end)
  277.   "Compile the current region in the inferior Scheme process.
  278. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  279.   (interactive "r")
  280.   (comint-send-string (scheme-proc) (format scheme-compile-exp-command
  281.                         (format "(begin %s)"
  282.                             (buffer-substring start end))))
  283.   (comint-send-string (scheme-proc) "\n"))
  284.  
  285. (defun scheme-compile-definition ()
  286.   "Compile the current definition in the inferior Scheme process."
  287.   (interactive)
  288.   (save-excursion
  289.    (end-of-defun)
  290.    (let ((end (point)))
  291.      (beginning-of-defun)
  292.      (scheme-compile-region (point) end))))
  293.  
  294. (defun switch-to-scheme (eob-p)
  295.   "Switch to the scheme process buffer.
  296. With argument, positions cursor at end of buffer."
  297.   (interactive "P")
  298.   (if (get-buffer scheme-buffer)
  299.       (pop-to-buffer scheme-buffer)
  300.       (error "No current process buffer. See variable scheme-buffer."))
  301.   (cond (eob-p
  302.      (push-mark)
  303.      (goto-char (point-max)))))
  304.  
  305. (defun scheme-send-region-and-go (start end)
  306.   "Send the current region to the inferior Scheme process.
  307. Then switch to the process buffer."
  308.   (interactive "r")
  309.   (scheme-send-region start end)
  310.   (switch-to-scheme t))
  311.  
  312. (defun scheme-send-definition-and-go ()
  313.   "Send the current definition to the inferior Scheme. 
  314. Then switch to the process buffer."
  315.   (interactive)
  316.   (scheme-send-definition)
  317.   (switch-to-scheme t))
  318.  
  319. (defun scheme-compile-definition-and-go ()
  320.   "Compile the current definition in the inferior Scheme. 
  321. Then switch to the process buffer."
  322.   (interactive)
  323.   (scheme-compile-definition)
  324.   (switch-to-scheme t))
  325.  
  326. (defun scheme-compile-region-and-go (start end)
  327.   "Compile the current region in the inferior Scheme. 
  328. Then switch to the process buffer."
  329.   (interactive "r")
  330.   (scheme-compile-region start end)
  331.   (switch-to-scheme t))
  332.  
  333. (defvar scheme-source-modes '(scheme-mode)
  334.   "*Used to determine if a buffer contains Scheme source code.
  335. If it's loaded into a buffer that is in one of these major modes, it's
  336. considered a scheme source file by scheme-load-file and scheme-compile-file.
  337. Used by these commands to determine defaults.")
  338.  
  339. (defvar scheme-prev-l/c-dir/file nil
  340.   "Caches the last (directory . file) pair.
  341. Caches the last pair used in the last scheme-load-file or
  342. scheme-compile-file command. Used for determining the default in the 
  343. next one.")
  344.  
  345. (defun scheme-load-file (file-name)
  346.   "Load a Scheme file into the inferior Scheme process."
  347.   (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  348.                   scheme-source-modes t)) ; T because LOAD 
  349.                                                           ; needs an exact name
  350.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  351.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  352.                        (file-name-nondirectory file-name)))
  353.   (comint-send-string (scheme-proc) (concat "(load \""
  354.                         file-name
  355.                         "\"\)\n")))
  356.  
  357. (defun scheme-compile-file (file-name)
  358.   "Compile a Scheme file in the inferior Scheme process."
  359.   (interactive (comint-get-source "Compile Scheme file: "
  360.                   scheme-prev-l/c-dir/file
  361.                   scheme-source-modes
  362.                   nil)) ; NIL because COMPILE doesn't
  363.                                         ; need an exact name.
  364.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  365.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  366.                        (file-name-nondirectory file-name)))
  367.   (comint-send-string (scheme-proc) (concat "(compile-file \""
  368.                         file-name
  369.                         "\"\)\n")))
  370.  
  371.  
  372. (defvar scheme-buffer nil "*The current scheme process buffer.
  373.  
  374. MULTIPLE PROCESS SUPPORT
  375. ===========================================================================
  376. Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
  377. processes. To run multiple Scheme processes, you start the first up with
  378. \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
  379. with \\[rename-buffer]. You may now start up a new process with another
  380. \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
  381. switch between the different process buffers with \\[switch-to-buffer].
  382.  
  383. Commands that send text from source buffers to Scheme processes --
  384. like scheme-send-definition or scheme-compile-region -- have to choose a
  385. process to send to, when you have more than one Scheme process around. This
  386. is determined by the global variable scheme-buffer. Suppose you
  387. have three inferior Schemes running:
  388.     Buffer    Process
  389.     foo        scheme
  390.     bar        scheme<2>
  391.     *scheme*    scheme<3>
  392. If you do a \\[scheme-send-definition-and-go] command on some Scheme source
  393. code, what process do you send it to?
  394.  
  395. - If you're in a process buffer (foo, bar, or *scheme*), 
  396.   you send it to that process.
  397. - If you're in some other buffer (e.g., a source file), you
  398.   send it to the process attached to buffer scheme-buffer.
  399. This process selection is performed by function scheme-proc.
  400.  
  401. Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
  402. to be the new process's buffer. If you only run one process, this will
  403. do the right thing. If you run multiple processes, you can change
  404. scheme-buffer to another process buffer with \\[set-variable].
  405.  
  406. More sophisticated approaches are, of course, possible. If you find yourself
  407. needing to switch back and forth between multiple processes frequently,
  408. you may wish to consider ilisp.el, a larger, more sophisticated package
  409. for running inferior Lisp and Scheme processes. The approach taken here is
  410. for a minimal, simple implementation. Feel free to extend it.")
  411.  
  412. (defun scheme-proc ()
  413.   "Returns the current scheme process. See variable scheme-buffer."
  414.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
  415.                       (current-buffer)
  416.                       scheme-buffer))))
  417.     (or proc
  418.     (error "No current process. See variable scheme-buffer"))))
  419.  
  420.  
  421. ;;; Do the user's customisation...
  422.  
  423. (defvar cmuscheme-load-hook nil
  424.   "This hook is run when cmuscheme is loaded in.
  425. This is a good place to put keybindings.")
  426.     
  427. (run-hooks 'cmuscheme-load-hook)
  428.  
  429.  
  430. ;;; CHANGE LOG
  431. ;;; ===========================================================================
  432. ;;; 8/88 Olin
  433. ;;; Created. 
  434. ;;;
  435. ;;; 2/15/89 Olin
  436. ;;; Removed -emacs flag from process invocation. It's only useful for
  437. ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
  438. ;;; which messes things up royally. A bug.
  439. ;;;
  440. ;;; 5/22/90 Olin
  441. ;;; - Upgraded to use comint-send-string and comint-send-region.
  442. ;;; - run-scheme now offers to let you edit the command line if
  443. ;;;   you invoke it with a prefix-arg. M-x scheme is redundant, and
  444. ;;;   has been removed.
  445. ;;; - Explicit references to process "scheme" have been replaced with
  446. ;;;   (scheme-proc). This allows better handling of multiple process bufs.
  447. ;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
  448. ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  449. ;;;   and friends, but interested hackers might find a useful application
  450. ;;;   of this facility.
  451. ;;;
  452. ;;; 3/12/90 Olin
  453. ;;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
  454. ;;;   Tale suggested this.
  455.  
  456. (provide 'cmuscheme)
  457.  
  458. ;;; cmuscheme.el ends here
  459.