home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / multi-shell.el < prev    next >
Encoding:
Text File  |  1991-08-04  |  6.1 KB  |  154 lines

  1. ; Path: dg-rtp!rock!mcnc!stanford.edu!snorkelwacker.mit.edu!think.com!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sane
  2. ; From: sane@cs.uiuc.edu (Aamod Sane)
  3. ; Newsgroups: gnu.emacs.help,comp.emacs
  4. ; Subject: Tracking-shell (Re: multiple concurrent emacs shells?)
  5. ; Date: 21 Jul 91 21:12:29 GMT
  6. ; References: <1991Jul20.014343.11662@milton.u.washington.edu>
  7. ; Organization: University of Illinois, Dept. of Comp. Sci., Urbana, IL
  8. ; Nntp-Posting-Host: clitus.cs.uiuc.edu
  9. ; narf@milton.u.washington.edu (Francis Taylor) writes:
  10. ; >Does anyone out there know of any way to have multiple running shells
  11. ; >in one emacs session?  Thanks.
  12. ; Here is a lisp file that will do it automatically for you. REquires
  13. ; cmushell , which is a better shell than normal shell. You will
  14. ; be find it at any archive, prep.ai.mit.edu or tut.cis.ohio-state.edu etc.
  15. ; multi-shell makes multiple shells , tracking-shell  when invoked from
  16. ; a buffer will already do a cd to the buffer disrectory. Usually,
  17. ; you do not require multiple shells at all in this case.
  18. ; You can change this to use shell if you want
  19. ; From tracking shell comments:
  20. ;; When invoked from a buffer, Tracking-Shell cd's to that buffers directory.
  21. ;; Thus to do something in the current directory of a buffer, pop up
  22. ;; a tracking-shell, do whatever, and close the shell. Automatic cd'ing
  23. ;; allows direct usage of shell commands; complicated key sequences are
  24. ;; not neccessary for file manipulation.
  25.  
  26.  
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. ;;
  29. ;;    Multiple Simultaneous shell 
  30. ;;    Trakcing shell - track the directory of buffer every time
  31. ;;
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. ;; Copyright (C) Aamod Sane (sane@cs.uiuc.edu)
  34. ;; Author: Aamod Sane
  35. ;; This file is not part of GNU Emacs yet.
  36. ;; The FSF copyright below applies to this file.
  37.  
  38. ;; LCD Archive Entry:
  39. ;; multi-shell|Aamod Sane|sane@cs.uiuc.edu
  40. ;; |Multiple simultaneous shells
  41. ;; |91-07-21||~/misc/multi-shell.el.Z|
  42.  
  43. ;; GNU Emacs is distributed in the hope that it will be useful,
  44. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  45. ;; accepts responsibility to anyone for the consequences of using it
  46. ;; or for whether it serves any particular purpose or works at all,
  47. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  48. ;; License for full details.
  49.  
  50. ;; Everyone is granted permission to copy, modify and redistribute
  51. ;; GNU Emacs, but only under the conditions described in the
  52. ;; GNU Emacs General Public License.   A copy of this license is
  53. ;; supposed to have been given to you along with GNU Emacs so you
  54. ;; can know your rights and responsibilities.  It should be in a
  55. ;; file named COPYING.  Among other things, the copyright notice
  56. ;; and this notice must be preserved on all copies.
  57.  
  58. (require 'cmushell)
  59.  
  60. (defun multi-shell(&optional n)
  61.   "Each time you invoke this, a new shell will be popped.
  62. See the documentation for cmushell for other details."
  63.   (interactive)
  64.   (let* ((name (if (null n) "MultiShell" n)) 
  65.      (buf (generate-new-buffer name)) 
  66.      (bufnam (buffer-name buf)))
  67.     (cond ((not (comint-check-proc bufnam))
  68.        (let* ((prog (or explicit-shell-file-name
  69.                 (getenv "ESHELL")
  70.                 (getenv "SHELL")
  71.                 "/bin/sh"))             
  72.           (name (file-name-nondirectory prog))
  73.           (startfile (concat "~/.emacs_" name))
  74.           (xargs-name (intern-soft (concat "explicit-" name "-args"))))
  75.          (save-excursion 
  76.            (set-buffer (apply 'multi-comint bufnam prog
  77.                   (if (file-exists-p startfile) startfile)
  78.                   (if (and xargs-name (boundp xargs-name))
  79.                       (symbol-value xargs-name)
  80.                     '("-i"))))
  81.            (cmushell-mode)))))
  82.     bufnam))
  83.  
  84.  
  85. (defun multi-comint (name program &optional startfile &rest switches)
  86.   (let* ((buffer (get-buffer-create name))
  87.      (proc (get-buffer-process buffer)))
  88.     ;; If no process, or nuked process, crank up a new one and put buffer in
  89.     ;; comint mode. Otherwise, leave buffer and existing process alone.
  90.     (cond ((or (not proc) (not (memq (process-status proc) '(run stop))))
  91.        (save-excursion
  92.          (set-buffer buffer)
  93.          (comint-mode)) ; Install local vars, mode, keymap, ...
  94.        (comint-exec buffer name program startfile switches)))
  95.     buffer))
  96.  
  97. (defconst tracking-shell-name "*Tracking-Shell*")
  98. (defconst tracking-shell-name-nostar "Tracking-Shell")
  99.  
  100. (defvar tracking-shell-other-window t
  101.   "*Whether tracking shell should appear in current window or not")
  102.  
  103. ;; When invoked from a buffer, Tracking-Shell cd's to that buffers directory.
  104. ;; Thus to do something in the current directory of a buffer, pop up
  105. ;; a tracking-shell, do whatever, and close the shell. Automatic cd'ing
  106. ;; allows direct usage of shell commands; complicated key sequences are
  107. ;; not neccessary for file manipulation.
  108.  
  109. (defun tracking-shell()
  110.   "When invoked from a buffer, if Tracking-Shell does
  111. not exist, it is created. If it exists, a cd is sent to the shell
  112. to synchronize the directory to that of the buffer in which this function is
  113. called. See the documentation of cmushell for other details."
  114.   (interactive)
  115.   (cond ((not (comint-check-proc tracking-shell-name))
  116.      (let* ((prog (or explicit-shell-file-name
  117.               (getenv "ESHELL")
  118.               (getenv "SHELL")
  119.               "/bin/sh"))             
  120.         (name (file-name-nondirectory prog))
  121.         (startfile (concat "~/.emacs_" name))
  122.         (xargs-name (intern-soft (concat "explicit-" name "-args"))))
  123.        (set-buffer (apply 'make-comint tracking-shell-name-nostar prog
  124.                   (if (file-exists-p startfile) startfile)
  125.                   (if (and xargs-name (boundp xargs-name))
  126.                   (symbol-value xargs-name)
  127.                 '("-i"))))
  128.        (cmushell-mode)))
  129.     (t 
  130.      (let ((dir default-directory))
  131.        (comint-send-string 
  132.         (get-buffer-process tracking-shell-name)
  133.         (concat "cd " dir "\n"))
  134.        (save-excursion
  135.          (set-buffer tracking-shell-name)
  136.          (cd dir)))))
  137.   (if tracking-shell-other-window
  138.       (switch-to-buffer-other-window tracking-shell-name)
  139.   (switch-to-buffer tracking-shell-name)))
  140.  
  141. ;; example of using tracking shell
  142. ;; (global-set-key "\C-c\C-s" 'tracking-shell)
  143. ;;            Open tracking shell window
  144. ;; (global-set-key "\C-c\C-t" 'delete-window) ;; could use C-x 0
  145. ;;           Close tracking shell window
  146.  
  147. (provide 'multishell)
  148.