home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cmu-sh1.zip / CMU-SH1.SHR next >
Internet Message Format  |  1993-03-25  |  54KB

  1. From utkcs2!emory!swrinde!ucsd!tut.cis.ohio-state.edu!pt.cs.cmu.edu!a.gp.cs.cmu.edu!shivers Tue Jun 12 10:07:39 EDT 1990
  2. Article 4422 of comp.emacs:
  3. Xref: utkcs2 comp.emacs:4422 gnu.emacs:2944
  4. Path: utkcs2!emory!swrinde!ucsd!tut.cis.ohio-state.edu!pt.cs.cmu.edu!a.gp.cs.cmu.edu!shivers
  5. >From: shivers@a.gp.cs.cmu.edu (Olin Shivers)
  6. Newsgroups: comp.emacs,gnu.emacs
  7. Subject: Release two of CMU process modes  (1 of 4)
  8. Keywords: comint
  9. Message-ID: <9599@pt.cs.cmu.edu>
  10. Date: 12 Jun 90 06:14:36 GMT
  11. Organization: Carnegie-Mellon University, CS/RI
  12. Lines: 1269
  13.  
  14.  
  15. This message contains:
  16. 1. The standard anouncement header
  17. 2. A shar file for cmushell.el and tea.el
  18. (The entire package must be split -- otherwise notesfiles sites will not
  19. be able to handle it.)
  20. -------------------------------------------------------------------------------
  21. I am posting release two of the CMU process modes. These are a suite of
  22. gnu-emacs packages for running various command interpreters in buffers. They
  23. are all built on top of a single base mode (comint mode) that provides a
  24. consistent set of base functionality and keybindings.
  25.  
  26. These modes have the following advantages:
  27. - Input history is kept in all modes, traversed with M-p and M-n, just
  28.   like on LispM's and various fancy shells.
  29. - Filename completion and query is available in all modes.
  30. - Keybindings are cross-compatible among all modes.
  31. - Keybindings are compatible with Zwei and Hemlock.
  32. - Directory tracking is more robust in shell mode, and is *only* active in
  33.   shell mode. (Try entering "cd /" to an inferior lisp in the old package:
  34.   Lisp barfs, but the inferior lisp mode goes ahead and changes the 
  35.   buffer's directory.)
  36. - Non-echoed text entry (for entering passwords) is available in all modes.
  37. - Shell and inferior Lisp modes are backwards compatible with the standard
  38.   gnu modes.
  39. - A full set of eval/compile-region/defun commands for the inferior Lisp,
  40.   Scheme, and T modes.
  41. - New modes are easily created for new types of processes.
  42.  
  43. The comint source has a section giving hints on converting older
  44. shell.el-based modes to use comint. I am told that the FSF will be
  45. using comint in the next release of gnumacs.
  46.  
  47.  
  48. The current set of modes are:
  49.     background.el       - run process in background, a la csh.
  50.     cmulisp.el            - simple lisp-in-a-buffer mode.
  51.     cmuscheme.el        - Scheme-in-a-buffer.
  52.     cmushell.el            - shell-in-a-buffer.
  53.     cmutex.el            - for TeX and LaTeX editing. Process support for
  54.                           running tex and latex, and printing and previewing
  55.                           .dvi files.
  56.     comint.el            - base mode. Used by everyone else.
  57.     tea.el              - T-in-a-buffer
  58.  
  59. These packages can be used singly or together. They are careful not to
  60. interfere with other modes. Where possible, I have tried to make them
  61. compatible with other similar modes, in terms of keybindings, functionality,
  62. and variable names. Users familiar with Hemlock or the LispM's Zemacs will
  63. find the commands in cmulisp are bound to similar keys. Cmushell mode pays
  64. attention to the variables that customise shell.el. And so forth.
  65.  
  66.  
  67. Also separately available are:
  68.     ilisp.el            - Powerful tightly-integrated lisp-in-a-buffer mode.
  69.     comint-ipc.el       - Facility for piggy-backing emacs/subprocess
  70.                           communications on top of the user's pipe or pty
  71.                           communications. Unavoidably somewhat fragile, but
  72.                           nontheless useful. Used by ilisp.el.
  73. These files should be released imminently by Chris McConnell.
  74.  
  75.  
  76. MAJOR CHANGES FROM RELEASE ONE
  77. ===============================================================================
  78. - An extremely hairy Lisp/Scheme interface by Chris Mconnell (ccm@cs.cmu.edu).
  79.   This package, ilisp.el, allows emacs to query the running process for useful
  80.   state. Example commands are:
  81.     lisp-arglist    display the arguments taken by the current function.
  82.     lisp-describe   call the Common Lisp DESCRIBE function on the symbol
  83.                     the cursor is over, and put the results in a window.
  84.     lisp-macroexpand macroexpand the current form and display the results.
  85.     lisp-find-source Edit the file that defines the function the point is on.
  86.     edit-next-caller Edit the next place this function is called.
  87.  
  88.    Ilisp.el allows tight integration with the lisp subprocess by
  89.    sending messages to the subprocess "behind-the-scenes" over the
  90.    user's connection. It does not assume a multithreaded lisp (as in
  91.    Franz Inc.'s Allegro Common Lisp). (However, it could certainly be
  92.    ported to take advantage of a multithreaded lisp, providing greater
  93.    communications robustness without altering the interface functionality
  94.    at all. Anyone interested in doing so should contact Chris.)
  95.  
  96.    Ilisp can be customised for most major dialects of Lisp and Scheme. This
  97.    package is so massive it is being released in a separate post by Chris.
  98.    I've included a dumb, simple inferior-lisp package in this release, as well
  99.    (cmulisp.el).
  100.  
  101. - A new package, background.el that allows you to run background processes in 
  102.   buffers. This was adapted for comint.el from Joe Keane's background.el.
  103.  
  104. - A very hairy TeX and LaTeX package. This includes buffer validation,
  105.   command entry commands, process interfaces to TeX, LaTeX, dvi printers and
  106.   previewers. Pieces of this have been contributed by many people,
  107.   notably Steve Anderson, Stephen Gildea, Dorab Patel, and Ashwin Ram.
  108.  
  109. - Lots of tweaks to the basic comint package, many contributed by other
  110.   people.
  111.   + A new input history command comint-previous-similar-input (M-s) taken
  112.     from some LispM interface. This is *quite* useful.
  113.   + The other input history commands have been improved.
  114.   + Filename completion has been improved.
  115.   + By popular demand, return now jumps to the end of line before 
  116.     submitting input (this is settable, but defaults to the eol behavior).
  117.   + New hooks for mode hackers.
  118.   + comint-exec no longer dependent on etc/env program.
  119.   + Process communication via pipes supported better.
  120.   + Simple process-query facility available.
  121.   + New procedures for sending input to processes that avoid hanging
  122.     on large text blocks.
  123.  
  124. - Tweaks to the other packages:
  125.   + Improved directory tracking in cmushell.el. It now handles
  126.     all forms of pushd and popd. New commands dirtrack-toggle and
  127.     dirs toggle the directory tracking machinery off and on, and
  128.     resync the mode's directory stack with the shell's.
  129.   + cmushell and cmulisp modes now reside in separate files
  130.     cmushell.el and cmulisp.el. Not only is this a good idea, 
  131.     it's apparently the way it'll be rel 19. N.B.: CMULISP USERS
  132.     MUST CHANGE THEIR .EMACS AUTOLOAD'S ACCORDINGLY.
  133.   + better support for multiple processes in cmulisp, cmuscheme 
  134.     and tea modes.
  135.   + cmulisp has some simple query commands, for function and
  136.     variable documentation, describing symbols, and getting
  137.     function arglists. This is a simple facility; Chris McConnell's
  138.     ilisp package is much more featureful. It was based on some code
  139.     contributed by Doug Roberts.
  140.   + cmulisp, cmuscheme, and tea now have a command (c-x c-e) to eval the
  141.     last sexp. The keybinding is compatible with emacs lisp and Hemlock.
  142.   + Filename defaults in Scheme and Lisp modes will use the string
  143.     the cursor is over, if it's an existing filename. This is a
  144.     debatable decision. Feedback appreciated.
  145.  
  146. GENERAL NOTES
  147. ===============================================================================
  148. Directory tracking:
  149. The new directory tracking code will handle
  150.     pushd [+n | <dir>]
  151.     popd [+n]
  152.     cd [<dir>]
  153. The emacs command M-x dirs will stuff a "dirs" at the process, wait for
  154. a line of output, parse it up, and reset the buffer's directory
  155. stack accordingly. The emacs command M-x dirtrack-toggle will toggle
  156. the directory tracking machinery on and off. This is useful, for example,
  157. when you are running ftp, and don't want ftp's "cd" command to throw
  158. emacs off.
  159.  
  160. The new code was based on source contributed by Vince Broman, Mike Coffin,
  161. Jeff Peck, and Barry Warsaw. Its structure should allow zealous shell hackers
  162. to extend it pretty easily.
  163.  
  164. The commands that directory tracking looks for are customisable.
  165. If you define aliases for cd or pushd or popd, you can set the right
  166. emacs variables and win.
  167.  
  168. Note that in general, complex directory tracking is a losing game.
  169. You can't win. If you have a programmable shell, an excellent hack
  170. is to program the directory-changing commands to type code sequences
  171. at the terminal, and write a process filter to watch for these sequences.
  172. A sequence like "ESC D <length> <directory>" where <directory> is <length>
  173. characters long would do fine. (Using a <length> parameter instead of
  174. string delimeters prevents emacs from gobbling up unbounded amounts of shell
  175. output in the event the terminating delimiter gets lost.)
  176.  
  177.  
  178. Multiple process buffers:
  179. Many people suggest ways to run multiple shell buffers. This does not require
  180. extra features. Suppose you want two shell buffers.  Simply make a shell
  181. buffer with M-x cmushell, and then rename it with M-x rename-buffer. Make the
  182. next shell with M-x cmushell, and now you've got two. Use M-x cmushell to get
  183. to the newest one, and C-x b <bufname> to get to the other.
  184.  
  185. Multiple Lisp and Scheme buffers work similarly. Commands that send text
  186. from a source buffer to the process buffer will always send to the
  187. buffer with the "official" name unless you explicitly override this
  188. (see variable lisp/scheme/tea-buffer). I don't see any real good ways to
  189. extend this. You could associate each source buffer with some process
  190. buffer, but this gets messy. I've tried to keep it simple. If you find
  191. yourself switching between multiple process buffers a lot, consider using
  192. ilisp.el. Or write a little code to soup up cmulisp.el.
  193.  
  194.  
  195. Documentation:
  196. There is no manual or info node for any of this stuff. I am not going to
  197. write one. Feel free to write one if you like, and submit it to me or the FSF.
  198. It would be a good thing.
  199.  
  200. The .el files are copiously documented, however. If you want to use any of
  201. these modes, I strongly suggest that you read the source files.  Really. Do
  202. it. They contain many useful sections of comments. Skip anything you don't
  203. understand; read the easy parts.  You may find some useful commands you didn't
  204. know about.  The top of each file contains a clearly-marked, friendly
  205. "suggestions for your .emacs file" section that will show you exactly how to
  206. get set up using the package. Also, pay attention to the DEFVAR'd variables.
  207. These are typically the means by which you can customise the package for your
  208. local environment.
  209.  
  210. The usual on-line documentation on modes, variables and commands
  211. is also available via c-h m, c-h v, and c-h d.
  212.  
  213.  
  214. ===============================================================================
  215. A shar file for the source files follows. This code is available under the
  216. terms of the Gnu Copyright agreement: you can do whatever you like with it as
  217. long as you don't charge for it or hold me responsible for anything it does.
  218. Please send any comments, improvements, bug reports, fixes or suggestions to
  219. olin.shivers@cs.cmu.edu.
  220.  
  221. I would like to thank all the people who took the time to send me the code
  222. that went into this second release.
  223.         -Olin
  224.         June 12, 1990
  225. ===============================================================================
  226. #
  227. # type    sh /afs/cs.cmu.edu/user/shivers/lib/emacs/sh2   to unpack this archive.
  228. #
  229. echo extracting cmushell.el...
  230. cat >cmushell.el <<'!E!O!F!'
  231. ;;; -*-Emacs-Lisp-*- General command interpreter in a window stuff
  232. ;;; Copyright Olin Shivers (1988).
  233. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  234. ;;; notice appearing here to the effect that you may use this code any
  235. ;;; way you like, as long as you don't charge money for it, remove this
  236. ;;; notice, or hold me liable for its results.
  237.  
  238. ;;; The changelog is at the end of file.
  239.  
  240. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  241. ;;; merge them into the master source.
  242. ;;;     - Olin Shivers (shivers@cs.cmu.edu)
  243.  
  244. ;;; This file defines a a shell-in-a-buffer package (cmushell mode) built on
  245. ;;; top of comint mode.  Cmushell mode is similar to, and intended to replace,
  246. ;;; its counterpart in the standard gnu emacs release. This replacement is
  247. ;;; more featureful, robust, and uniform than the released version.
  248.  
  249. ;;; Since this mode is built on top of the general command-interpreter-in-
  250. ;;; a-buffer mode (comint mode), it shares a common base functionality, 
  251. ;;; and a common set of bindings, with all modes derived from comint mode.
  252. ;;; This makes these modes easier to use.
  253.  
  254. ;;; For documentation on the functionality provided by comint mode, and
  255. ;;; the hooks available for customising it, see the file comint.el.
  256. ;;; For further information on cmushell mode, see the comments below.
  257.  
  258. ;;; Needs fixin:
  259. ;;; When sending text from a source file to a subprocess, the process-mark can 
  260. ;;; move off the window, so you can lose sight of the process interactions.
  261. ;;; Maybe I should ensure the process mark is in the window when I send
  262. ;;; text to the process? Switch selectable?
  263.  
  264. (require 'comint)
  265. (provide 'cmushell)
  266.  
  267. ;; YOUR .EMACS FILE
  268. ;;=============================================================================
  269. ;; Some suggestions for your .emacs file.
  270. ;;
  271. ;; ; If cmushell lives in some non-standard directory, you must tell emacs
  272. ;; ; where to get it. This may or may not be necessary.
  273. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  274. ;;
  275. ;; ; Autoload cmushell from file cmushell.el
  276. ;; (autoload 'cmushell "cmushell"
  277. ;;           "Run an inferior shell process."
  278. ;;           t)
  279. ;;
  280. ;; ; Define C-c C-t to run my favorite command in cmushell mode:
  281. ;; (setq cmushell-load-hook
  282. ;;       '((lambda () 
  283. ;;           (define-key cmushell-mode-map "\C-c\C-t" 'favorite-cmd))))
  284.  
  285.  
  286. ;;; Brief Command Documentation:
  287. ;;;============================================================================
  288. ;;; Comint Mode Commands: (common to cmushell and all comint-derived modes)
  289. ;;;
  290. ;;; m-p        comint-previous-input            Cycle backwards in input history
  291. ;;; m-n        comint-next-input                  Cycle forwards
  292. ;;; c-c r   comint-previous-input-matching  Search backwards in input history
  293. ;;; return  comint-send-input
  294. ;;; c-a     comint-bol                      Beginning of line; skip prompt.
  295. ;;; c-d        comint-delchar-or-maybe-eof        Delete char unless at end of buff.
  296. ;;; c-c c-u comint-kill-input                ^u
  297. ;;; c-c c-w backward-kill-word            ^w
  298. ;;; c-c c-c comint-interrupt-subjob         ^c
  299. ;;; c-c c-z comint-stop-subjob                ^z
  300. ;;; c-c c-\ comint-quit-subjob                ^\
  301. ;;; c-c c-o comint-kill-output            Delete last batch of process output
  302. ;;; c-c c-r comint-show-output            Show last batch of process output
  303. ;;;         send-invisible                  Read line w/o echo & send to proc
  304. ;;;         comint-continue-subjob        Useful if you accidentally suspend
  305. ;;;                            top-level job.
  306. ;;; comint-mode-hook is the comint mode hook.
  307.  
  308. ;;; Shell Mode Commands:
  309. ;;;         cmushell                Fires up the shell process.
  310. ;;; m-tab   comint-dynamic-complete        Complete a partial file name
  311. ;;; m-?     comint-dynamic-list-completions List completions in help buffer
  312. ;;;         dirs                    Resync the buffer's dir stack.
  313. ;;;         dirtrack-toggle                 Turn dir tracking on/off.
  314. ;;;
  315. ;;; The cmushell mode hook is cmushell-mode-hook
  316. ;;; The cmushell-load-hook is run after this file is loaded.
  317. ;;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards
  318. ;;; compatibility.
  319.  
  320. ;;; Read the rest of this file for more information.
  321.  
  322. ;;; SHELL.EL COMPATIBILITY
  323. ;;;============================================================================
  324. ;;; In brief: this package should have no trouble coexisting with shell.el.
  325. ;;; 
  326. ;;; Most customising variables -- e.g., explicit-shell-file-name -- are the
  327. ;;; same, so the users shouldn't have much trouble. Hooks have different
  328. ;;; names, however, so you can customise shell mode differently from cmushell
  329. ;;; mode. You basically just have to remember to type M-x cmushell instead of
  330. ;;; M-x shell.
  331. ;;; 
  332. ;;; It would be nice if this file was completely plug-compatible with the old
  333. ;;; shell package -- if you could just name this file shell.el, and have it
  334. ;;; transparently replace the old one. But you can't.  Several other packages
  335. ;;; (tex-mode, background, dbx, gdb, kermit, monkey, prolog, telnet) are also
  336. ;;; clients of shell mode. These packages assume detailed knowledge of shell
  337. ;;; mode internals in ways that are incompatible with cmushell mode (mostly
  338. ;;; because of cmushell mode's greater functionality).  So, unless we are
  339. ;;; willing to port all of these packages, we can't have this file be a
  340. ;;; complete replacement for shell.el -- that is, we can't name this file
  341. ;;; shell.el, and its main entry point (shell), because dbx.el will break
  342. ;;; when it loads it in and tries to use it.
  343. ;;; 
  344. ;;; There are two ways to fix this. One: rewrite these other modes to use the
  345. ;;; new package. This is a win, but can't be assumed. The other, backwards
  346. ;;; compatible route, is to make this package non-conflict with shell.el, so
  347. ;;; both files can be loaded in at the same time. And *that* is why some
  348. ;;; functions and variables have different names: (cmushell),
  349. ;;; cmushell-mode-map, that sort of thing. All the names have been carefully
  350. ;;; chosen so that shell.el and cmushell.el won't tromp on each other.
  351.  
  352. ;;; Customisation and Buffer Variables
  353. ;;; ===========================================================================
  354. ;;; 
  355.  
  356. ;In loaddefs.el now.
  357. ;(defconst shell-prompt-pattern
  358. ;  "^[^#$%>]*[#$%>] *"
  359. ;  "*Regexp used by Newline command to match subshell prompts.
  360. ;;; Change the doc string for shell-prompt-pattern:
  361. (put 'shell-prompt-pattern 'variable-documentation
  362.   "Regexp to match prompts in the inferior shell.
  363. Defaults to \"^[^#$%>]*[#$%>] *\", which works pretty well.
  364. This variable is used to initialise comint-prompt-regexp in the 
  365. shell buffer.
  366.  
  367. This is a fine thing to set in your .emacs file.")
  368.  
  369. (defvar shell-popd-regexp "popd"
  370.   "*Regexp to match subshell commands equivalent to popd.")
  371.  
  372. (defvar shell-pushd-regexp "pushd"
  373.   "*Regexp to match subshell commands equivalent to pushd.")
  374.  
  375. (defvar shell-cd-regexp "cd"
  376.   "*Regexp to match subshell commands equivalent to cd.")
  377.  
  378. (defvar explicit-shell-file-name nil
  379.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  380.  
  381. (defvar explicit-csh-args
  382.   (if (eq system-type 'hpux)
  383.       ;; -T persuades HP's csh not to think it is smarter
  384.       ;; than us about what terminal modes to use.
  385.       '("-i" "-T")
  386.     '("-i"))
  387.   "*Args passed to inferior shell by M-x cmushell, if the shell is csh.
  388. Value is a list of strings, which may be nil.")
  389.  
  390. ;;; All the above vars aren't prefixed "cmushell-" to make them
  391. ;;; backwards compatible w/shell.el and old .emacs files.
  392.  
  393. (defvar cmushell-dirstack nil
  394.   "List of directories saved by pushd in this buffer's shell.")
  395.  
  396. (defvar cmushell-dirstack-query "dirs"
  397.   "Command used by shell-resync-dirlist to query shell.")
  398.  
  399. (defvar cmushell-mode-map '())
  400. (cond ((not cmushell-mode-map)
  401.        (setq cmushell-mode-map (full-copy-sparse-keymap comint-mode-map))
  402.        (define-key cmushell-mode-map "\M-\t" 'comint-dynamic-complete)
  403.        (define-key cmushell-mode-map "\M-?"  'comint-dynamic-list-completions)))
  404.  
  405. (defvar cmushell-mode-hook '()
  406.   "*Hook for customising cmushell mode")
  407.  
  408.  
  409. ;;; Basic Procedures
  410. ;;; ===========================================================================
  411. ;;;
  412.  
  413. (defun cmushell-mode ()
  414.   "Major mode for interacting with an inferior shell.
  415. Return after the end of the process' output sends the text from the 
  416.     end of process to the end of the current line.
  417. Return before end of process output copies rest of line to end (skipping
  418.     the prompt) and sends it.
  419. M-x send-invisible reads a line of text without echoing it, and sends it to
  420.     the shell.
  421.  
  422. If you accidentally suspend your process, use \\[comint-continue-subjob]
  423. to continue it.
  424.  
  425. cd, pushd and popd commands given to the shell are watched by Emacs to keep
  426. this buffer's default directory the same as the shell's working directory.
  427. M-x dirs queries the shell and resyncs Emacs' idea of what the current 
  428.     directory stack is.
  429. M-x dirtrack-toggle turns directory tracking on and off.
  430.  
  431. \\{cmushell-mode-map}
  432. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  433. cmushell-mode-hook (in that order).
  434.  
  435. Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp are used
  436. to match their respective commands."
  437.   (interactive)
  438.   (comint-mode)
  439.   (setq comint-prompt-regexp shell-prompt-pattern)
  440.   (setq major-mode 'cmushell-mode)
  441.   (setq mode-name "CMU shell")
  442.   (use-local-map cmushell-mode-map)
  443.   (make-local-variable 'cmushell-dirstack)
  444.   (setq cmushell-dirstack nil)
  445.   (make-local-variable 'cmushell-dirtrackp)
  446.   (setq cmushell-dirtrackp t)
  447.   (setq comint-input-sentinel 'cmushell-directory-tracker)
  448.   (run-hooks 'cmushell-mode-hook))
  449.  
  450.  
  451. (defun cmushell ()
  452.   "Run an inferior shell, with I/O through buffer *cmushell*.
  453. If buffer exists but shell process is not running, make new shell.
  454. If buffer exists and shell process is running, 
  455.  just switch to buffer *cmushell*.
  456. Program used comes from variable explicit-shell-file-name,
  457.  or (if that is nil) from the ESHELL environment variable,
  458.  or else from SHELL if there is no ESHELL.
  459. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  460.  (Note that this may lose due to a timing error if the shell
  461.   discards input when it starts up.)
  462. The buffer is put in cmushell-mode, giving commands for sending input
  463. and controlling the subjobs of the shell.  See cmushell-mode.
  464. See also variable shell-prompt-pattern.
  465.  
  466. The shell file name (sans directories) is used to make a symbol name
  467. such as `explicit-csh-arguments'.  If that symbol is a variable,
  468. its value is used as a list of arguments when invoking the shell.
  469. Otherwise, one argument `-i' is passed to the shell.
  470.  
  471. \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
  472.   (interactive)
  473.   (cond ((not (comint-check-proc "*cmushell*"))
  474.      (let* ((prog (or explicit-shell-file-name
  475.               (getenv "ESHELL")
  476.               (getenv "SHELL")
  477.               "/bin/sh"))             
  478.         (name (file-name-nondirectory prog))
  479.         (startfile (concat "~/.emacs_" name))
  480.         (xargs-name (intern-soft (concat "explicit-" name "-args"))))
  481.        (set-buffer (apply 'make-comint "cmushell" prog
  482.                   (if (file-exists-p startfile) startfile)
  483.                   (if (and xargs-name (boundp xargs-name))
  484.                   (symbol-value xargs-name)
  485.                   '("-i"))))
  486.        (cmushell-mode))))
  487.   (switch-to-buffer "*cmushell*"))
  488.  
  489.  
  490. ;;; Directory tracking
  491. ;;; ===========================================================================
  492. ;;; This code provides the cmushell mode input sentinel
  493. ;;;     CMUSHELL-DIRECTORY-TRACKER
  494. ;;; that tracks cd, pushd, and popd commands issued to the shell, and
  495. ;;; changes the current directory of the shell buffer accordingly.
  496. ;;;
  497. ;;; This is basically a fragile hack, although it's more accurate than
  498. ;;; the released version in shell.el. It has the following failings:
  499. ;;; 1. It doesn't know about the cdpath shell variable.
  500. ;;; 2. It only spots the first command in a command sequence. E.g., it will
  501. ;;;    miss the cd in "ls; cd foo"
  502. ;;; 3. More generally, any complex command (like ";" sequencing) is going to
  503. ;;;    throw it. Otherwise, you'd have to build an entire shell interpreter in
  504. ;;;    emacs lisp.  Failing that, there's no way to catch shell commands where
  505. ;;;    cd's are buried inside conditional expressions, aliases, and so forth.
  506. ;;;
  507. ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
  508. ;;; messes it up. You run other processes under the shell; these each have
  509. ;;; separate working directories, and some have commands for manipulating
  510. ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
  511. ;;; commands that do *not* effect the current w.d. at all, but look like they
  512. ;;; do (e.g., the cd command in ftp).  In shells that allow you job
  513. ;;; control, you can switch between jobs, all having different w.d.'s. So
  514. ;;; simply saying %3 can shift your w.d..
  515. ;;;
  516. ;;; The solution is to relax, not stress out about it, and settle for
  517. ;;; a hack that works pretty well in typical circumstances. Remember
  518. ;;; that a half-assed solution is more in keeping with the spirit of Unix, 
  519. ;;; anyway. Blech.
  520. ;;;
  521. ;;; One good hack not implemented here for users of programmable shells
  522. ;;; is to program up the shell w.d. manipulation commands to output
  523. ;;; a coded command sequence to the tty. Something like
  524. ;;;     ESC | <cwd> |
  525. ;;; where <cwd> is the new current working directory. Then trash the
  526. ;;; directory tracking machinery currently used in this package, and
  527. ;;; replace it with a process filter that watches for and strips out
  528. ;;; these messages.
  529.  
  530. ;;; REGEXP is a regular expression. STR is a string. START is a fixnum.
  531. ;;; Returns T if REGEXP matches STR where the match is anchored to start
  532. ;;; at position START in STR. Sort of like LOOKING-AT for strings.
  533. (defun cmushell-front-match (regexp str start)
  534.   (eq start (string-match regexp str start)))
  535.  
  536. (defun cmushell-directory-tracker (str)
  537.   "Tracks cd, pushd and popd commands issued to the shell.
  538. This function is called on each input passed to the shell.
  539. It watches for cd, pushd and popd commands and sets the buffer's
  540. default directory to track these commands.
  541.  
  542. You may toggle this tracking on and off with M-x dirtrack-toggle.
  543. If emacs gets confused, you can resync with the shell with M-x dirs.
  544.  
  545. See variables shell-cd-regexp, shell-pushd-regexp, and shell-popd-regexp."
  546.   (cond (cmushell-dirtrackp
  547.      (string-match "^\\s *" str) ; skip whitespace
  548.      (let ((bos (match-end 0))
  549.            (x nil))
  550.        (cond ((setq x (cmushell-match-cmd-w/optional-arg shell-popd-regexp
  551.                                  str bos))
  552.           (cmushell-process-popd x))
  553.          ((setq x (cmushell-match-cmd-w/optional-arg shell-pushd-regexp
  554.                                  str bos))
  555.           (cmushell-process-pushd x))
  556.          ((setq x (cmushell-match-cmd-w/optional-arg shell-cd-regexp
  557.                                  str bos))
  558.           (cmushell-process-cd x)))))))
  559.  
  560.  
  561. ;;; Try to match regexp CMD to string, anchored at position START.
  562. ;;; CMD may be followed by a single argument. If a match, then return
  563. ;;; the argument, if there is one, or the empty string if not. If
  564. ;;; no match, return nil.
  565.  
  566. (defun cmushell-match-cmd-w/optional-arg (cmd str start)
  567.   (and (cmushell-front-match cmd str start)
  568.        (let ((eoc (match-end 0))) ; end of command
  569.      (cond ((cmushell-front-match "\\s *\\(\;\\|$\\)" str eoc)
  570.         "")            ; no arg
  571.            ((cmushell-front-match "\\s +\\([^ \t\;]+\\)\\s *\\(\;\\|$\\)"
  572.                       str eoc)
  573.         (substring str (match-beginning 1) (match-end 1))) ; arg
  574.            (t nil))))) ; something else.
  575. ;;; The first regexp is [optional whitespace, (";" or the end of string)].
  576. ;;; The second regexp is [whitespace, (an arg), optional whitespace,
  577. ;;;     (";" or end of string)].
  578.  
  579.  
  580. ;;; popd [+n]
  581. (defun cmushell-process-popd (arg)
  582.   (let ((num (if (zerop (length arg)) 0 ; no arg means +0
  583.          (cmushell-extract-num arg))))
  584.     (if (and num (< num (length cmushell-dirstack)))
  585.     (if (= num 0) ; condition-case because the CD could lose.
  586.         (condition-case nil (progn (cd (car cmushell-dirstack))
  587.                        (setq cmushell-dirstack
  588.                          (cdr cmushell-dirstack))
  589.                        (cmushell-dirstack-message))
  590.           (error (message "Couldn't cd.")))
  591.         (let* ((ds (cons nil cmushell-dirstack))
  592.            (cell (nthcdr (- num 1) ds)))
  593.           (rplacd cell (cdr (cdr cell)))
  594.           (setq cmushell-dirstack (cdr ds))
  595.           (cmushell-dirstack-message)))
  596.     (message "Bad popd."))))
  597.  
  598.  
  599. ;;; cd [dir]
  600. (defun cmushell-process-cd (arg)
  601.   (condition-case nil (progn (cd (if (zerop (length arg)) (getenv "HOME")
  602.                      arg))
  603.                  (cmushell-dirstack-message))
  604.        (error (message "Couldn't cd."))))
  605.  
  606.  
  607. ;;; pushd [+n | dir]
  608. (defun cmushell-process-pushd (arg)
  609.   (if (zerop (length arg))
  610.       ;; no arg -- swap pwd and car of shell stack
  611.       (condition-case nil (if cmushell-dirstack
  612.                   (let ((old default-directory))
  613.                 (cd (car cmushell-dirstack))
  614.                 (setq cmushell-dirstack
  615.                       (cons old (cdr cmushell-dirstack)))
  616.                 (cmushell-dirstack-message))
  617.                   (message "Directory stack empty."))
  618.     (message "Couldn't cd."))
  619.  
  620.       (let ((num (cmushell-extract-num arg)))
  621.     (if num                ; pushd +n
  622.         (if (> num (length cmushell-dirstack))
  623.         (message "Directory stack not that deep.")
  624.         (let* ((ds (cons default-directory cmushell-dirstack))
  625.                (dslen (length ds))
  626.                (front (nthcdr num ds))
  627.                (back (reverse (nthcdr (- dslen num) (reverse ds))))
  628.                (new-ds (append front back)))
  629.           (condition-case nil
  630.               (progn (cd (car new-ds))
  631.                  (setq cmushell-dirstack (cdr new-ds))
  632.                  (cmushell-dirstack-message))
  633.             (error (message "Couldn't cd.")))))
  634.            
  635.         ;; pushd <dir>
  636.         (let ((old-wd default-directory))
  637.           (condition-case nil
  638.           (progn (cd arg)
  639.              (setq cmushell-dirstack
  640.                    (cons old-wd cmushell-dirstack))
  641.              (cmushell-dirstack-message))
  642.         (error (message "Couldn't cd."))))))))
  643.  
  644. ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
  645. (defun cmushell-extract-num (str)
  646.   (and (string-match "^\\+[1-9][0-9]*$" str)
  647.        (string-to-int str)))
  648.  
  649.  
  650. (defun cmushell-dirtrack-toggle ()
  651.   "Turn directory tracking on and off in a cmushell buffer."
  652.   (interactive)
  653.   (setq cmushell-dirtrackp (not cmushell-dirtrackp))
  654.   (message "directory tracking %s."
  655.        (if cmushell-dirtrackp "ON" "OFF")))
  656.  
  657. ;;; For your typing convenience:
  658. (fset 'dirtrack-toggle 'cmushell-dirtrack-toggle)
  659.  
  660.  
  661. (defun cmushell-resync-dirs ()
  662.   "Resync the buffer's idea of the current directory stack.
  663. This command queries the shell with the command bound to 
  664. cmushell-dirstack-query (default \"dirs\"), reads the next
  665. line output and parses it to form the new directory stack.
  666. DON'T issue this command unless the buffer is at a shell prompt.
  667. Also, note that if some other subprocess decides to do output
  668. immediately after the query, its output will be taken as the
  669. new directory stack -- you lose. If this happens, just do the
  670. command again."
  671.   (interactive)
  672.   (let* ((proc (get-buffer-process (current-buffer)))
  673.      (pmark (process-mark proc)))
  674.     (goto-char pmark)
  675.     (insert cmushell-dirstack-query) (insert "\n")
  676.     (sit-for 0) ; force redisplay
  677.     (comint-send-string proc cmushell-dirstack-query) 
  678.     (comint-send-string proc "\n")
  679.     (set-marker pmark (point))
  680.     (let ((pt (point))) ; wait for 1 line
  681.       ;; This extra newline prevents the user's pending input from spoofing us.
  682.       (insert "\n") (backward-char 1)
  683.       (while (not (looking-at ".+\n"))
  684.     (accept-process-output proc)
  685.     (goto-char pt)))
  686.     (goto-char pmark) (delete-char 1) ; remove the extra newline
  687.     ;; That's the dirlist. grab it & parse it.
  688.     (let* ((dl (buffer-substring (match-beginning 0) (- (match-end 0) 1)))
  689.        (dl-len (length dl))
  690.        (ds '())            ; new dir stack
  691.        (i 0))
  692.       (while (< i dl-len)
  693.     ;; regexp = optional whitespace, (non-whitespace), optional whitespace
  694.     (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
  695.     (setq ds (cons (substring dl (match-beginning 1) (match-end 1))
  696.                ds))
  697.     (setq i (match-end 0)))
  698.       (let ((ds (reverse ds)))
  699.     (condition-case nil
  700.         (progn (cd (car ds))
  701.            (setq cmushell-dirstack (cdr ds))
  702.            (cmushell-dirstack-message))
  703.       (error (message "Couldn't cd.")))))))
  704.  
  705. ;;; For your typing convenience:
  706. (fset 'dirs 'cmushell-resync-dirs)
  707.  
  708.  
  709. ;;; Show the current dirstack on the message line.
  710. ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
  711. ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
  712. ;;; All the commands that mung the buffer's dirstack finish by calling
  713. ;;; this guy.
  714. (defun cmushell-dirstack-message ()
  715.   (let ((msg "")
  716.     (ds (cons default-directory cmushell-dirstack)))
  717.     (while ds
  718.       (let ((dir (car ds)))
  719.     (if (string-match (format "^%s\\(/\\|$\\)" (getenv "HOME")) dir)
  720.         (setq dir (concat "~/" (substring dir (match-end 0)))))
  721.     (if (string-equal dir "~/") (setq dir "~"))
  722.     (setq msg (concat msg dir " "))
  723.     (setq ds (cdr ds))))
  724.     (message msg)))
  725.  
  726.  
  727.  
  728. ;;; Interfacing to client packages (and converting them)
  729. ;;;============================================================================
  730. ;;; Several gnu packages (tex-mode, background, dbx, gdb, kermit, prolog, 
  731. ;;; telnet are some) use the shell package as clients. Most of them would
  732. ;;; be better off using the comint package directly, but they predate it.
  733. ;;; The catch is that most of these packages (dbx, gdb, prolog, telnet)
  734. ;;; assume total knowledge of all the local variables that shell mode
  735. ;;; functions depend on. So they (kill-all-local-variables), then create
  736. ;;; the few local variables that shell.el functions depend on. Alas,
  737. ;;; cmushell.el functions depend on a different set of vars (for example,
  738. ;;; the input history ring is a local variable in cmushell.el's shell mode,
  739. ;;; whereas there is no input history ring in shell.el's shell mode).
  740. ;;; So we have a situation where the greater functionality of cmushell.el
  741. ;;; is biting us -- you can't just replace shell will cmushell.
  742. ;;;
  743. ;;; Altering these packages to use comint mode directly should *greatly*
  744. ;;; improve their functionality, and is actually pretty easy. It's
  745. ;;; mostly a matter of renaming a few variable names. See comint.el for more.
  746. ;;;     -Olin
  747.  
  748.  
  749.  
  750. ;;; Do the user's customisation...
  751. ;;;===============================
  752. (defvar cmushell-load-hook nil
  753.   "This hook is run when cmushell is loaded in.
  754. This is a good place to put keybindings.")
  755.     
  756. (run-hooks 'cmushell-load-hook)
  757.  
  758. ;;; Change Log
  759. ;;; ===========================================================================
  760. ;;; Olin 8/88
  761. ;;; Created.
  762. ;;;
  763. ;;; Olin 5/26/90
  764. ;;; - Split cmulisp and cmushell modes into separate files. 
  765. ;;;   Not only is this a good idea, it's apparently the way it'll be rel 19.
  766. ;;; - Souped up the directory tracking; it now can handle pushd, pushd +n, 
  767. ;;;   and popd +n.
  768. ;;; - Added cmushell-dirtrack-toggle command to toggle the directory
  769. ;;;   tracking that cmushell tries to do. This is useful, for example,
  770. ;;;   when you are running ftp -- it prevents the ftp "cd" command from
  771. ;;;   spoofing the tracking machinery. This command is also named 
  772. ;;;   dirtrack-toggle, so you need only type M-x dirtrack to run it.
  773. ;;; - Added cmushell-resync-dirs command. This queries the shell
  774. ;;;   for the current directory stack, and resets the buffer's stack
  775. ;;;   accordingly. This command is also named dirs, so you need only type
  776. ;;;   M-x dirs to run it.
  777. ;;; - Bits of the new directory tracking code were adapted from source
  778. ;;;   contributed by Vince Broman, Jeff Peck, and Barry Warsaw.
  779. ;;; - See also the improvements made to comint.el at the same time.
  780. ;;; - Renamed several variables. Mostly this comprised changing "shell"
  781. ;;;   to "cmushell" in the names. The only variables that are not prefixed
  782. ;;;   with "cmushell-" are the ones that are common with shell.el:
  783. ;;;       explicit-shell-file-name shell-prompt-pattern explicit-csh-args 
  784. ;;;       and shell-cd/popd/pushd-regexp
  785. ;;;   The variables and functions that were changed to have "cmushell-" 
  786. ;;;   prefixes are:
  787. ;;;       shell-directory-stack (v), shell-directory-tracker (f)
  788. ;;;   This should not affect users, only elisp hackers. Hopefully
  789. ;;;   one day shell.el will just go away, and we can drop all this
  790. ;;;   "cmushell" bullshit.
  791. ;;; - Upgraded process sends to use comint-send-string instead of
  792. ;;;   process-send-string.
  793. !E!O!F!
  794. #
  795. # type    sh /afs/cs.cmu.edu/user/shivers/lib/emacs/sh2   to unpack this archive.
  796. #
  797. echo extracting tea.el...
  798. cat >tea.el <<'!E!O!F!'
  799. ;;; tea.el -- Teach emacs about T.
  800. ;;; Copyright Olin Shivers (1988)
  801. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  802. ;;; notice appearing here to the effect that you may use this code any
  803. ;;; way you like, as long as you don't charge money for it, remove this
  804. ;;; notice, or hold me liable for its results.
  805. ;;;
  806. ;;; 1. Major mode for editing T source: t-mode
  807. ;;;    This is just a variant of scheme-mode, tweaked for T.
  808. ;;; 2. Major mode for running T in a buffer: run-tea
  809. ;;;    This is a customisation of comint-mode.
  810. ;;;
  811. ;;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  812. ;;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  813. ;;; 8/88
  814. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  815. ;;; merge them into the master source.
  816. ;;;
  817. ;;; Change log at end of file.
  818.  
  819.  
  820. ;; YOUR .EMACS FILE
  821. ;;=============================================================================
  822. ;; Some suggestions for your .emacs file.
  823. ;;
  824. ;; ; If tea.el lives in some non-standard directory, you must tell emacs
  825. ;; ; where to get it. This may or may not be necessary.
  826. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  827. ;;
  828. ;; ; Autoload run-tea and t-mode from file tea.el
  829. ;; (autoload 'run-tea "tea"
  830. ;;           "Run an inferior T process."
  831. ;;           t)
  832. ;;
  833. ;; (autoload 't-mode "tea"
  834. ;;           "Major mode for editing T source. Just Scheme mode, tuned a bit."
  835. ;;           t)
  836. ;;
  837. ;; ; Files ending in ".t" are T source, so put their buffers in t-mode.
  838. ;; (setq auto-mode-alist
  839. ;;       (cons '("\\.t$" . t-mode) 
  840. ;;           auto-mode-alist))   
  841. ;;
  842. ;; ; Define C-c C-t to run my favorite command in inferior T mode:
  843. ;; (setq tea-load-hook
  844. ;;       '((lambda () (define-key inferior-t-mode-map "\C-c\C-t"
  845. ;;                                'favorite-cmd))))
  846.  
  847. ;; ETAGS
  848. ;;=============================================================================
  849. ;; A suggestion for modifying the etags program so that it knows about T.
  850. ;; You should modify the few lines that allow etags to conclude that
  851. ;; files that end with ".t" are lisp or scheme source code.  
  852. ;; Find a line that looks like
  853. ;; /* .scm or .sm or .scheme implies scheme source code */
  854. ;; and add a 
  855. ;;         !strcmp (cp + 1, "t") ||
  856. ;; suffix check to the following clauses that check filename suffixes.
  857. ;; This is already done for some versions of etags. Have a look, or try it 
  858. ;; & see.
  859.  
  860. (setq scheme-mit-dialect nil) ; Give me a break.
  861. (require 'scheme)
  862. (require 'cmushell)
  863.  
  864.  
  865. ;;; T mode stuff
  866. ;;;============================================================================
  867.  
  868. ;;; Note: T mode alters the scheme-mode syntax table and indentation
  869. ;;; hooks slightly. If you were using scheme-mode and t-mode simultaneously
  870. ;;; this might be a problem, except that the alterations are fairly 
  871. ;;; innocuous.
  872.  
  873. ;; This adds [] and {} as matching delimiters. So emacs will treat #[Char 0]
  874. ;; or #{Procedure 1 ADD} as an s-exp with a quote sign in front.
  875. (modify-syntax-entry ?[ "(]" scheme-mode-syntax-table)
  876. (modify-syntax-entry ?] ")[" scheme-mode-syntax-table)
  877. (modify-syntax-entry ?{ "(}" scheme-mode-syntax-table)
  878. (modify-syntax-entry ?} "){" scheme-mode-syntax-table)
  879.  
  880. ;; Modify scheme-indent-hook for T.
  881. (put 'labels 'scheme-indent-hook 1)
  882. (put 'block 'scheme-indent-hook 0)
  883. (put 'block0 'scheme-indent-hook 0)
  884. (put 'object 'scheme-indent-hook 1)
  885. (put 'lset 'scheme-indent-hook 1)
  886. (put 'xcase 'scheme-indent-hook 1)
  887. (put 'select 'scheme-indent-hook 1)
  888. (put 'xselect 'scheme-indent-hook 1)
  889. (put 'iterate 'scheme-indent-hook 2)
  890. (put 'cond 'scheme-indent-hook 0)
  891. (put 'xcond 'scheme-indent-hook 0)
  892. (put 'catch 'scheme-indent-hook 1)
  893. (put 'bind 'scheme-indent-hook 1)
  894. (put 'define-operation 'scheme-indent-hook 1)
  895. (put 'operation 'scheme-indent-hook 1)
  896. (put 'object 'scheme-indent-hook 1)
  897. (put 'join 'scheme-indent-hook 0)
  898. (put 'destructure 'scheme-indent-hook 1)
  899. (put 'destructure* 'scheme-indent-hook 1)
  900. (put 'define-integrable 'scheme-indent-hook 1)
  901. (put 'define-constant 'scheme-indent-hook 1)
  902. (put 'define-syntax 'scheme-indent-hook 1)
  903. (put 'let-syntax 'scheme-indent-hook 1)
  904. (put 'define-local-syntax 'scheme-indent-hook 1)
  905. (put 'macro-expander 'scheme-indent-hook 1)
  906. (put 'with-open-streams 'scheme-indent-hook 1)
  907. (put 'with-open-ports 'scheme-indent-hook 1)
  908. (put 'with-input-from-string 'scheme-indent-hook 1)
  909. (put 'with-output-to-string 'scheme-indent-hook 1)
  910. (put 'with-output-width-string 'scheme-indent-hook 1)
  911. (put 'receive 'scheme-indent-hook 2)
  912. (put 'receive-values 'scheme-indent-hook 1)
  913.  
  914. (defvar t-mode-hook nil
  915.  "*Hook for customising T mode")
  916.  
  917. (defvar t-mode-map (full-copy-sparse-keymap scheme-mode-map))
  918.  
  919. (defun t-mode ()
  920.   "Major mode for editing T code. 
  921. This is Scheme mode, slightly tuned for T. Editing commands are similar
  922. to those of Lisp mode.
  923.  
  924. In addition, if an inferior T process is running, some additional
  925. commands will be defined, for evaluating expressions and controlling
  926. the interpreter, and the state of the process will be displayed in the
  927. modeline of all T buffers.  The names of commands that interact
  928. with the T process start with \"tea-\".  For more information
  929. see the documentation for inferior-t-mode.
  930.  
  931. Commands:
  932. Delete converts tabs to spaces as it moves back.
  933. Blank lines separate paragraphs.  Semicolons start comments.
  934. \\{t-mode-map}
  935. Customisation: Entry to this mode runs the hooks on t-mode-hook"
  936.   (interactive)
  937.   (kill-all-local-variables)
  938.   (use-local-map t-mode-map)
  939.   (scheme-mode-variables)
  940.   (setq major-mode 't-mode)
  941.   (setq mode-name "T")
  942.   (run-hooks 't-mode-hook))
  943.  
  944.  
  945. ;;; INFERIOR T MODE STUFF
  946. ;;;============================================================================
  947.  
  948. (defvar inferior-t-mode-map nil)
  949. (cond ((not inferior-t-mode-map)
  950.        (setq inferior-t-mode-map (full-copy-sparse-keymap comint-mode-map))
  951.        (scheme-mode-commands inferior-t-mode-map)
  952.        (define-key inferior-t-mode-map "\M-\C-x"  'tea-send-definition)
  953.        (define-key inferior-t-mode-map "\C-x\C-e" 'tea-send-last-sexp)
  954.        (define-key inferior-t-mode-map "\C-cl"    'tea-load-file)
  955.        (define-key inferior-t-mode-map "\C-ck"    'tea-compile-file) ;"kompile"
  956.        ))
  957.  
  958. ;; Install the process communication commands in the scheme-mode keymap.
  959. (define-key t-mode-map "\M-\C-x"  'tea-send-definition) ; gnu convention
  960. (define-key t-mode-map "\C-x\C-e" 'tea-send-last-sexp)  ; gnu convention
  961. (define-key t-mode-map "\C-ce"    'tea-send-definition)
  962. (define-key t-mode-map "\C-c\C-e" 'tea-send-definition-and-go)
  963. (define-key t-mode-map "\C-cr"    'tea-send-region)
  964. (define-key t-mode-map "\C-c\C-r" 'tea-send-region-and-go)
  965. (define-key t-mode-map "\C-cc"    'tea-compile-definition)
  966. (define-key t-mode-map "\C-c\C-c" 'tea-compile-definition-and-go)
  967. (define-key t-mode-map "\C-cz"    'switch-to-tea)
  968. (define-key t-mode-map "\C-cl"    'tea-load-file)
  969. (define-key t-mode-map "\C-ck"    'tea-compile-file)
  970.  
  971. (defvar inferior-t-mode-hook nil
  972.   "*Hook for customising inferior-T mode")
  973.  
  974. (defun inferior-t-mode ()
  975.   "Major mode for interacting with an inferior T process.
  976.  
  977. The following commands are available:
  978. \\{inferior-t-mode-map}
  979.  
  980. A T process can be fired up with M-x run-tea.
  981.  
  982. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  983. inferior-t-mode-hook (in that order).
  984.  
  985. You can send text to the inferior T process from other buffers containing
  986. T source.  
  987.     switch-to-tea switches the current buffer to the T process buffer.
  988.     tea-send-definition sends the current definition to the T process.
  989.     tea-compile-definition compiles the current definition.
  990.     tea-send-region sends the current region to the T process.
  991.     tea-compile-region compiles the current region.
  992.  
  993.     tea-send-definition-and-go, tea-compile-definition-and-go,
  994.         tea-send-region-and-go, and tea-compile-region-and-go
  995.         switch to the T process buffer after sending their text.
  996. For information on running multiple processes in multiple buffers, see
  997. documentation for variable tea-buffer.
  998.  
  999. Commands:
  1000. Return after the end of the process' output sends the text from the 
  1001.     end of process to point.
  1002. Return before the end of the process' output copies the sexp ending at point
  1003.     to the end of the process' output, and sends it.
  1004. Delete converts tabs to spaces as it moves back.
  1005. Tab indents for T; with argument, shifts rest
  1006.     of expression rigidly with the current line.
  1007. C-M-q does Tab on each line starting within following expression.
  1008. Paragraphs are separated only by blank lines.  Semicolons start comments.
  1009. If you accidentally suspend your process, use \\[comint-continue-subjob]
  1010. to continue it."
  1011.   (interactive)
  1012.   (comint-mode)
  1013.   (setq comint-prompt-regexp "^>+ *") ; Customise in inferior-t-mode-hook
  1014.   (scheme-mode-variables)
  1015.  
  1016.   (setq major-mode 'inferior-t-mode)
  1017.   (setq mode-name "Inferior T")
  1018.   (setq mode-line-process '(": %s"))
  1019.   (use-local-map inferior-t-mode-map)
  1020.   (setq comint-input-filter 'tea-input-filter)
  1021.   (setq comint-input-sentinel 'ignore)
  1022.   (setq comint-get-old-input 'tea-get-old-input)
  1023.   (run-hooks 'inferior-t-mode-hook))
  1024.  
  1025. (defun tea-input-filter (str)
  1026.   "Don't save anything matching tea-filter-regexp"
  1027.   (not (string-match tea-filter-regexp str)))
  1028.  
  1029. (defvar tea-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  1030.   "*Input matching this regexp are not saved on the history list.
  1031. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  1032.  
  1033. (defun tea-get-old-input ()
  1034.   "Snarf the sexp ending at point"
  1035.   (save-excursion
  1036.     (let ((end (point)))
  1037.       (backward-sexp)
  1038.       (buffer-substring (point) end))))
  1039.  
  1040. ;;; This will break if you have an argument with whitespace, as in
  1041. ;;; string = "-ab +c -x 'you lose'".
  1042. (defun tea-args-to-list (string)
  1043.   (let ((where (string-match "[ \t]" string)))
  1044.     (cond ((null where) (list string))
  1045.       ((not (= where 0))
  1046.        (cons (substring string 0 where)
  1047.          (tea-args-to-list (substring string (+ 1 where)
  1048.                           (length string)))))
  1049.       (t (let ((pos (string-match "[^ \t]" string)))
  1050.            (if (null pos)
  1051.            nil
  1052.          (tea-args-to-list (substring string pos
  1053.                           (length string)))))))))
  1054.  
  1055. (defvar tea-program-name "t"
  1056.   "*Program invoked by the run-tea command")
  1057.  
  1058. ;;; Obsolete
  1059. (defun tea (&rest foo) (message "Use run-tea"))
  1060.  
  1061. (defun run-tea (cmd)
  1062.   "Run an inferior T process, input and output via buffer *tea*.
  1063. If there is a process already running in *tea*, just switch to that buffer.
  1064. With argument, allows you to edit the command line (default is value
  1065. of tea-program-name).  Runs the hooks from inferior-t-mode-hook (after the
  1066. comint-mode-hook is run).
  1067. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  1068.  
  1069.   (interactive (list (if current-prefix-arg
  1070.              (read-string "Run T: " tea-program-name)
  1071.              tea-program-name)))
  1072.   (if (not (comint-check-proc "*tea*"))
  1073.       (let ((cmdlist (tea-args-to-list cmd)))
  1074.     (set-buffer (apply 'make-comint "tea" (car cmdlist)
  1075.                nil (cdr cmdlist)))
  1076.     (inferior-t-mode)))
  1077.   (setq tea-buffer "*tea*")
  1078.   (switch-to-buffer "*tea*"))
  1079.  
  1080. (defun tea-send-region (start end)
  1081.   "Send the current region to the inferior T process"
  1082.   (interactive "r")
  1083.   (comint-send-region (tea-proc) start end)
  1084.   (comint-send-string (tea-proc) "\n"))
  1085.  
  1086. (defun tea-send-definition ()
  1087.   "Send the current definition to the inferior T process."
  1088.   (interactive)
  1089.   (save-excursion
  1090.    (end-of-defun)
  1091.    (let ((end (point)))
  1092.      (beginning-of-defun)
  1093.      (tea-send-region (point) end))))
  1094.  
  1095. (defun tea-send-last-sexp ()
  1096.   "Send the previous sexp to the inferior T process."
  1097.   (interactive)
  1098.   (tea-send-region (save-excursion (backward-sexp) (point)) (point)))
  1099.  
  1100. (defun tea-compile-region (start end)
  1101.   "Compile the current region in the inferior T process.
  1102. \(A BLOCK is wrapped around the region: (BLOCK <region>)"
  1103.   (interactive "r")
  1104.   (comint-send-string (tea-proc) "(orbit '(block ")
  1105.   (comint-send-region (tea-proc) start end)
  1106.   (comint-send-string (tea-proc) "))\n"))
  1107.  
  1108. (defun tea-compile-definition ()
  1109.   "Compile the current definition in the inferior T process."
  1110.   (interactive)
  1111.   (save-excursion
  1112.    (end-of-defun)
  1113.    (let ((end (point)))
  1114.      (beginning-of-defun)
  1115.      (tea-compile-region (point) end))))
  1116.  
  1117. (defun switch-to-tea (eob-p)
  1118.   "Switch to the T process buffer.
  1119. With argument, positions cursor at end of buffer."
  1120.   (interactive "P")
  1121.   (if (get-buffer tea-buffer)
  1122.       (pop-to-buffer tea-buffer)
  1123.       (error "No current process buffer. See variable tea-buffer."))
  1124.   (cond (eob-p
  1125.      (push-mark)
  1126.      (goto-char (point-max)))))
  1127.  
  1128. (defun tea-send-region-and-go (start end)
  1129.   "Send the current region to the inferior T process,
  1130. and switch to the process buffer."
  1131.   (interactive "r")
  1132.   (tea-send-region start end)
  1133.   (switch-to-tea t))
  1134.  
  1135. (defun tea-send-definition-and-go ()
  1136.   "Send the current definition to the inferior T process, 
  1137. and switch to the process buffer."
  1138.   (interactive)
  1139.   (tea-send-definition)
  1140.   (switch-to-tea t))
  1141.  
  1142. (defun tea-compile-region-and-go (start end)
  1143.   "Compile the current region in the inferior T process, 
  1144. and switch to process buffer."
  1145.   (interactive "r")
  1146.   (tea-compile-region start end)
  1147.   (switch-to-tea t))
  1148.  
  1149. (defun tea-compile-definition-and-go ()
  1150.   "Compile the current definition in the inferior T process,
  1151. and switch to process buffer."
  1152.   (interactive)
  1153.   (tea-compile-definition)
  1154.   (switch-to-tea t))
  1155.  
  1156. (defvar tea-source-modes '(t-mode)
  1157.   "*Used to determine if a buffer contains T source code.
  1158. If it's loaded into a buffer that is in one of these major modes, it's
  1159. considered a T source file by tea-load-file and tea-compile-file.
  1160. Used by these commands to determine defaults.")
  1161.  
  1162. (defvar tea-prev-l/c-dir/file nil
  1163.   "Caches the (directory . file) pair used in the last tea-load-file or
  1164. tea-compile-file command. Used for determining the default in the next one.")
  1165.  
  1166. (defun tea-load-file (file-name)
  1167.   "Load a T file into the inferior T process."
  1168.   (interactive (comint-get-source "Load T file: " tea-prev-l/c-dir/file
  1169.                   tea-source-modes t)) ; T because LOAD needs
  1170.                                                        ; an exact name.
  1171.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  1172.   (setq tea-prev-l/c-dir/file (cons (file-name-directory    file-name)
  1173.                     (file-name-nondirectory file-name)))
  1174.   (comint-send-string (tea-proc) (concat "(load \""
  1175.                      file-name
  1176.                      "\"\)\n"))
  1177.   (switch-to-tea t))
  1178.  
  1179. (defun tea-compile-file (file-name)
  1180.   "Compile a T file in the inferior T process."
  1181.   (interactive (comint-get-source "Compile T file: " tea-prev-l/c-dir/file
  1182.                   tea-source-modes
  1183.                   nil)) ; NIL because COMPILE doesn't
  1184.                                         ; need an exact name.
  1185.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  1186.   (setq tea-prev-l/c-dir/file (cons (file-name-directory    file-name)
  1187.                     (file-name-nondirectory file-name)))
  1188.   (comint-send-string (tea-proc) (concat "(compile-file \""
  1189.                      file-name
  1190.                      "\"\)\n"))
  1191.   (switch-to-tea t))
  1192.  
  1193.  
  1194. ;;; This helps when you run more than one T process at a time.
  1195.  
  1196. ;;; If we're in some inferior T buffer, return its process,
  1197. ;;; even if the buffer's been renamed. If we're elsewhere, assume
  1198. ;;; the standard process named "tea".
  1199. (defun tea-proc ()
  1200.   (let ((proc (get-buffer-process (current-buffer))))
  1201.     (if (and proc (eq major-mode 'inferior-t-mode)) proc
  1202.     (get-process "tea"))))
  1203.  
  1204. (defvar tea-buffer nil "*The current T process buffer.
  1205.  
  1206. MULTIPLE PROCESS SUPPORT
  1207. ===========================================================================
  1208. Tea.el supports, in a fairly simple fashion, running multiple T
  1209. processes. To run multiple T processes, you start the first up with
  1210. \\[run-tea]. It will be in a buffer named *tea*. Rename this buffer
  1211. with \\[rename-buffer]. You may now start up a new process with another
  1212. \\[run-tea]. It will be in a new buffer, named *tea*. You can
  1213. switch between the different process buffers with \\[switch-to-buffer].
  1214.  
  1215. Commands that send text from source buffers to T processes --
  1216. like tea-send-definition or tea-compile-region -- have to choose a
  1217. process to send to, when you have more than one T process around. This
  1218. is determined by the global variable tea-buffer. Suppose you
  1219. have three inferior T's running:
  1220.     Buffer    Process
  1221.     foo        tea
  1222.     bar        tea<2>
  1223.     *tea*       tea<3>
  1224. If you do a \\[tea-send-definition-and-go] command on some T source code,
  1225. what process do you send it to?
  1226.  
  1227. - If you're in a process buffer (foo, bar, or *tea*), 
  1228.   you send it to that process.
  1229. - If you're in some other buffer (e.g., a source file), you
  1230.   send it to the process attached to buffer tea-buffer.
  1231. This process selection is performed by function tea-proc.
  1232.  
  1233. Whenever \\[run-tea] fires up a new process, it resets tea-buffer
  1234. to be the new process's buffer. If you only run one process, this will
  1235. do the right thing. If you run multiple processes, you can change
  1236. tea-buffer to another process buffer with \\[set-variable].
  1237.  
  1238. More sophisticated approaches are, of course, possible. If you find youself
  1239. needing to switch back and forth between multiple processes frequently,
  1240. you may wish to consider ilisp.el, a larger, more sophisticated package
  1241. for running inferior Lisp and Scheme processes. The approach taken here is
  1242. for a minimal, simple implementation. Feel free to extend it.")
  1243.  
  1244. (defun tea-proc ()
  1245.   "Returns the current T process. See variable tea-buffer."
  1246.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-t-mode)
  1247.                       (current-buffer)
  1248.                       tea-buffer))))
  1249.     (or proc
  1250.     (error "No current process. See variable tea-buffer"))))
  1251.  
  1252.  
  1253. ;;; Do the user's customisation...
  1254.  
  1255. (defvar tea-load-hook nil
  1256.   "This hook is run when tea is loaded in.
  1257. This is a good place to put keybindings.")
  1258.     
  1259. (run-hooks 'tea-load-hook)
  1260.  
  1261. ;;; CHANGE LOG
  1262. ;;; ===========================================================================
  1263. ;;; 8/88 Olin
  1264. ;;; Created.
  1265. ;;;
  1266. ;;; 2/15/89 Olin
  1267. ;;; Removed -emacs flag from process invocation. It's only useful for
  1268. ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
  1269. ;;; which messes things up royally. A bug.
  1270. ;;;
  1271. ;;; 5/22/90 Olin
  1272. ;;; Upgraded to use comint-send-string and comint-send-region.
  1273. ;;; - run-tea now offers to let you edit the command line if
  1274. ;;;   you invoke it with a prefix-arg. M-x tea is redundant, and
  1275. ;;;   has been removed.
  1276. ;;; - Explicit references to process "tea" have been replaced with
  1277. ;;;   (tea-proc). This allows better handling of multiple process bufs.
  1278. ;;; - Added tea-send-last-sexp, bound to C-x C-e. A gnu convention.
  1279. ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  1280. ;;;   and friends, but interested hackers might find a useful application
  1281. ;;;   of this facility.
  1282. !E!O!F!
  1283.  
  1284.  
  1285.