home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / multishell.el < prev    next >
Encoding:
Text File  |  1991-08-04  |  3.7 KB  |  96 lines

  1. ; Path: dg-rtp!rock!mcnc!stanford.edu!snorkelwacker.mit.edu!spool.mu.edu!think.com!compass!worley
  2. ; From: worley@compass.com (Dale Worley)
  3. ; Newsgroups: gnu.emacs.help,comp.emacs,gnu.emacs.sources
  4. ; Subject: Re: multiple concurrent emacs shells?
  5. ; Date: 22 Jul 91 15:10:56 GMT
  6. ; Organization: Compass, Inc., Wakefield, MA, U.S.A.
  7. ; In article <1991Jul20.014343.11662@milton.u.washington.edu> narf@milton.u.washington.edu (Francis Taylor) writes:
  8. ;    Does anyone out there know of any way to have multiple running shells
  9. ;    in one emacs session?
  10. ; This is the code I use.  Then you can create new shells with C-u - M-x
  11. ; shell, and select existing shells with C-u (number) M-x shell.  M-x
  12. ; shell works as before, and creates shell number 1 if it doesn't
  13. ; already exist.
  14.  
  15. ;; LCD Archive Entry:
  16. ;; multishell|Dale Worley|worley@compass.com
  17. ;; |Multiple shell buffers
  18. ;; |91-07-22||~/functions/multishell.el.Z|
  19.  
  20. (defun shell (&optional shell-number)
  21.   "Run an inferior shell, with I/O through buffer *shell*.
  22. If buffer exists but shell process is not running, make new shell.
  23. Program used comes from variable explicit-shell-file-name,
  24.  or (if that is nil) from the ESHELL environment variable,
  25.  or else from SHELL if there is no ESHELL.
  26. If a file ~/.emacs_SHELLNAME exists, it is given as initial input.
  27. It is particularly useful to have this file set the environment
  28. variable 'PAGER' to the value 'cat'.
  29.  (Note that this may lose due to a timing error if the shell
  30.   discards input when it starts up.)
  31. The buffer is put in shell-mode, giving commands for sending input
  32. and controlling the subjobs of the shell.  See shell-mode.
  33. See also variable shell-prompt-pattern.
  34.  
  35. The shell file name (sans directories) is used to make a symbol name
  36. such as `explicit-csh-arguments'.  If that symbol is a variable,
  37. its value is used as a list of arguments when invoking the shell.
  38. Otherwise, one argument `-i' is passed to the shell.
  39.  
  40. Note that many people's .cshrc files unconditionally clear the prompt.
  41. If yours does, you will probably want to change it.
  42.  
  43. A negative argument causes further shell windows (*shell<2>*,
  44. *shell<3>*, etc.) to be created.  A positive argument causes the shell
  45. window with that number to be selected, instead of *shell* (which is
  46. number 1).  Shells with numbers > 1 must be created explicitly; shell
  47. 1 is created automatically if it is selected but does not already
  48. exist."
  49.   (interactive "p")
  50.   (let* ((prog (or explicit-shell-file-name
  51.            (getenv "ESHELL")
  52.            (getenv "SHELL")
  53.            "/bin/sh"))             
  54.      (name (file-name-nondirectory prog))
  55.      (dont nil)
  56.      shell-name)
  57.     ;; process the user argument
  58.     (cond
  59.      ;; a null argument is the same as 1
  60.      ((null shell-number)
  61.       (setq shell-number 1))
  62.      ;; a negative argument means find a new shell number
  63.      ((< shell-number 1)
  64.       (progn
  65.     (setq shell-number 2)
  66.     (while
  67.         (get-buffer (concat "*shell<" (int-to-string shell-number) ">*"))
  68.       (setq shell-number (1+ shell-number)))))
  69.      ;; if he's trying to select a shell > 1, check to see if it exists first
  70.      ((> shell-number 1)
  71.       (if (null (get-buffer (concat "*shell<" 
  72.                     (int-to-string shell-number)
  73.                     ">*")))
  74.       (progn
  75.         (ding)
  76.         (setq dont t)))))
  77.     ;; if we haven't found an error, do it
  78.     (if (not dont)
  79.     (progn
  80.       (setq shell-name (if (eq shell-number 1)
  81.                    "shell"
  82.                  (concat "shell<" 
  83.                      (int-to-string shell-number)
  84.                      ">")))
  85.       (switch-to-buffer
  86.        (apply 'make-shell shell-name prog
  87.           (if (file-exists-p (concat "~/.emacs_" name))
  88.               (concat "~/.emacs_" name))
  89.           (let ((symbol (intern-soft
  90.                  (concat "explicit-" name "-args"))))
  91.             (if (and symbol (boundp symbol))
  92.             (symbol-value symbol)
  93.               '("-i")))))))))
  94.