home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / cmushell.shar.1 / text0000.txt < prev   
Encoding:
Text File  |  1993-01-18  |  52.5 KB  |  1,272 lines

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