home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / comint / shell.el < prev    next >
Encoding:
Text File  |  1993-03-13  |  24.6 KB  |  592 lines

  1. ;;; -*-Emacs-Lisp-*- General command interpreter in a window stuff
  2. ;;; Copyright Olin Shivers (1988).
  3. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  4. ;;; notice appearing here to the effect that you may use this code any
  5. ;;; way you like, as long as you don't charge money for it, remove this
  6. ;;; notice, or hold me liable for its results.
  7.  
  8. ;;; The changelog is at the end of file.
  9.  
  10. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  11. ;;; merge them into the master source.
  12. ;;;     - Olin Shivers (shivers@cs.cmu.edu)
  13.  
  14. ;;; This file defines a shell-in-a-buffer package (shell mode) built on
  15. ;;; top of comint mode.
  16.  
  17. ;;; Since this mode is built on top of the general command-interpreter-in-
  18. ;;; a-buffer mode (comint mode), it shares a common base functionality, 
  19. ;;; and a common set of bindings, with all modes derived from comint mode.
  20. ;;; This makes these modes easier to use.
  21.  
  22. ;;; For documentation on the functionality provided by comint mode, and
  23. ;;; the hooks available for customising it, see the file comint.el.
  24. ;;; For further information on shell mode, see the comments below.
  25.  
  26. ;;; Needs fixin:
  27. ;;; When sending text from a source file to a subprocess, the process-mark can 
  28. ;;; move off the window, so you can lose sight of the process interactions.
  29. ;;; Maybe I should ensure the process mark is in the window when I send
  30. ;;; text to the process? Switch selectable?
  31.  
  32. (require 'comint)
  33. (provide 'shell)
  34.  
  35. ;; YOUR .EMACS FILE
  36. ;;=============================================================================
  37. ;; Some suggestions for your .emacs file.
  38. ;;
  39. ;; ; Define C-c C-t to run my favorite command in shell mode:
  40. ;; (setq shell-load-hook
  41. ;;       '((lambda () 
  42. ;;           (define-key shell-mode-map "\C-c\C-t" 'favorite-cmd))))
  43.  
  44.  
  45. ;;; Brief Command Documentation:
  46. ;;;============================================================================
  47. ;;; Comint Mode Commands: (common to shell and all comint-derived modes)
  48. ;;;
  49. ;;; m-p        comint-previous-input            Cycle backwards in input history
  50. ;;; m-n        comint-next-input                  Cycle forwards
  51. ;;; c-c r   comint-previous-input-matching  Search backwards in input history
  52. ;;; return  comint-send-input
  53. ;;; c-a     comint-bol                      Beginning of line; skip prompt.
  54. ;;; c-d        comint-delchar-or-maybe-eof        Delete char unless at end of buff.
  55. ;;; c-c c-u comint-kill-input                ^u
  56. ;;; c-c c-w backward-kill-word            ^w
  57. ;;; c-c c-c comint-interrupt-subjob         ^c
  58. ;;; c-c c-z comint-stop-subjob                ^z
  59. ;;; c-c c-\ comint-quit-subjob                ^\
  60. ;;; c-c c-o comint-kill-output            Delete last batch of process output
  61. ;;; c-c c-r comint-show-output            Show last batch of process output
  62. ;;;         send-invisible                  Read line w/o echo & send to proc
  63. ;;;         comint-continue-subjob        Useful if you accidentally suspend
  64. ;;;                            top-level job.
  65. ;;; comint-mode-hook is the comint mode hook.
  66.  
  67. ;;; Shell Mode Commands:
  68. ;;;         shell                Fires up the shell process.
  69. ;;; tab,m-tab comint-dynamic-complete        Complete a partial file name
  70. ;;; m-?     comint-dynamic-list-completions List completions in help buffer
  71. ;;;         shell-resync-dirs            Resync the buffer's dir stack.
  72. ;;;         shell-dirtrack-toggle           Turn dir tracking on/off.
  73. ;;;
  74. ;;; The shell mode hook is shell-mode-hook
  75. ;;; The shell-load-hook is run after this file is loaded.
  76. ;;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards
  77. ;;; compatibility.
  78.  
  79. ;;; Read the rest of this file for more information.
  80.  
  81. ;;; SHELL.EL COMPATIBILITY
  82. ;;;============================================================================
  83. ;;; In brief: this package should have no trouble coexisting with shell.el.
  84. ;;; 
  85. ;;; Most customising variables -- e.g., explicit-shell-file-name -- are the
  86. ;;; same, so the users shouldn't have much trouble. Hooks have different
  87. ;;; names, however, so you can customise shell mode differently from shell
  88. ;;; mode. You basically just have to remember to type M-x shell instead of
  89. ;;; M-x shell.
  90. ;;; 
  91. ;;; It would be nice if this file was completely plug-compatible with the old
  92. ;;; shell package -- if you could just name this file shell.el, and have it
  93. ;;; transparently replace the old one. But you can't.  Several other packages
  94. ;;; (tex-mode, background, dbx, gdb, kermit, monkey, prolog, telnet) are also
  95. ;;; clients of shell mode. These packages assume detailed knowledge of shell
  96. ;;; mode internals in ways that are incompatible with shell mode (mostly
  97. ;;; because of shell mode's greater functionality).  So, unless we are
  98. ;;; willing to port all of these packages, we can't have this file be a
  99. ;;; complete replacement for shell.el -- that is, we can't name this file
  100. ;;; shell.el, and its main entry point (shell), because dbx.el will break
  101. ;;; when it loads it in and tries to use it.
  102. ;;; 
  103. ;;; There are two ways to fix this. One: rewrite these other modes to use the
  104. ;;; new package. This is a win, but can't be assumed. The other, backwards
  105. ;;; compatible route, is to make this package non-conflict with shell.el, so
  106. ;;; both files can be loaded in at the same time. And *that* is why some
  107. ;;; functions and variables have different names: (shell),
  108. ;;; shell-mode-map, that sort of thing. All the names have been carefully
  109. ;;; chosen so that shell.el and shell.el won't tromp on each other.
  110.  
  111. ;;; Customisation and Buffer Variables
  112. ;;; ===========================================================================
  113. ;;; 
  114.  
  115. ;In loaddefs.el now.
  116. ;(defconst shell-prompt-pattern
  117. ;  "^[^#$%>]*[#$%>] *"
  118. ;  "*Regexp used by Newline command to match subshell prompts.
  119. ;;; Change the doc string for shell-prompt-pattern:
  120. (put 'shell-prompt-pattern 'variable-documentation
  121.   "Regexp to match prompts in the inferior shell.
  122. Defaults to \"^[^#$%>]*[#$%>] *\", which works pretty well.
  123. This variable is used to initialise comint-prompt-regexp in the 
  124. shell buffer.
  125.  
  126. This is a fine thing to set in your .emacs file.")
  127.  
  128. (defvar shell-popd-regexp "popd"
  129.   "*Regexp to match subshell commands equivalent to popd.")
  130.  
  131. (defvar shell-pushd-regexp "pushd"
  132.   "*Regexp to match subshell commands equivalent to pushd.")
  133.  
  134. (defvar shell-cd-regexp "cd"
  135.   "*Regexp to match subshell commands equivalent to cd.")
  136.  
  137. (defvar explicit-shell-file-name nil
  138.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  139.  
  140. (defvar explicit-csh-arguments
  141.   (if (eq system-type 'hpux)
  142.       ;; -T persuades HP's csh not to think it is smarter
  143.       ;; than us about what terminal modes to use.
  144.       '("-i" "-T")
  145.     '("-i"))
  146.   "*Args passed to inferior shell by M-x shell, if the shell is csh.
  147. Value is a list of strings, which may be nil.")
  148.  
  149. ;;; All the above vars aren't prefixed "shell-" to make them
  150. ;;; backwards compatible w/shell.el and old .emacs files.
  151.  
  152. (defvar shell-dirstack nil
  153.   "List of directories saved by pushd in this buffer's shell.")
  154.  
  155. (defvar shell-dirstack-query "dirs"
  156.   "Command used by shell-resync-dirlist to query shell.")
  157.  
  158. (defvar shell-mode-map '())
  159. (if (not shell-mode-map)
  160.     (let ((map (if (fboundp 'set-keymap-parent)
  161.                    (let ((map (make-keymap)))
  162.                      (set-keymap-parent map comint-mode-map)
  163.                      (set-keymap-name map 'shell-mode-map)
  164.                      map)
  165.          (full-copy-sparse-keymap comint-mode-map))))
  166.       (define-key map "\M-\t" 'comint-dynamic-complete)
  167.       (define-key map "\t" 'comint-dynamic-complete)
  168.       (define-key map "\M-?"  'comint-dynamic-list-completions)
  169.       (setq shell-mode-map map)))
  170.  
  171. (defvar shell-mode-hook '()
  172.   "*Hook for customising shell mode")
  173.  
  174.  
  175. ;;; Basic Procedures
  176. ;;; ===========================================================================
  177. ;;;
  178.  
  179. (defvar shell-dirtrackp)
  180.  
  181. (defun shell-mode ()
  182.   "Major mode for interacting with an inferior shell.
  183. Return after the end of the process' output sends the text from the 
  184.     end of process to the end of the current line.
  185. Return before end of process output copies rest of line to end (skipping
  186.     the prompt) and sends it.
  187. M-x send-invisible reads a line of text without echoing it, and sends it to
  188.     the shell.
  189.  
  190. If you accidentally suspend your process, use \\[comint-continue-subjob]
  191. to continue it.
  192.  
  193. cd, pushd and popd commands given to the shell are watched by Emacs to keep
  194. this buffer's default directory the same as the shell's working directory.
  195. M-x shell-resync-dirs queries the shell and resyncs Emacs' idea of what the 
  196.     current directory stack is.
  197. M-x shell-dirtrack-toggle turns directory tracking on and off.
  198.  
  199. \\{shell-mode-map}
  200. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  201. shell-mode-hook (in that order).
  202.  
  203. Variables shell-cd-regexp, shell-pushd-regexp and shell-popd-regexp are used
  204. to match their respective commands."
  205.   (interactive)
  206.   (comint-mode)
  207.   (setq comint-prompt-regexp shell-prompt-pattern)
  208.   (setq major-mode 'shell-mode)
  209.   (setq mode-name "shell")
  210.   (use-local-map shell-mode-map)
  211.   (make-local-variable 'shell-dirstack)
  212.   (setq shell-dirstack nil)
  213.   (make-local-variable 'shell-dirtrackp)
  214.   (setq shell-dirtrackp t)
  215.   (setq comint-input-sentinel 'shell-directory-tracker)
  216.   (run-hooks 'shell-mode-hook))
  217.  
  218.  
  219. (defun shell ()
  220.   "Run an inferior shell, with I/O through buffer *shell*.
  221. If buffer exists but shell process is not running, make new shell.
  222. If buffer exists and shell process is running, 
  223.  just switch to buffer *shell*.
  224. Program used comes from variable explicit-shell-file-name,
  225.  or (if that is nil) from the ESHELL environment variable,
  226.  or else from SHELL if there is no ESHELL.
  227. If a file ~/.emacs_SHELLNAME exists, it is given as initial input
  228.  (Note that this may lose due to a timing error if the shell
  229.   discards input when it starts up.)
  230. The buffer is put in shell-mode, giving commands for sending input
  231. and controlling the subjobs of the shell.  See shell-mode.
  232. See also variable shell-prompt-pattern.
  233.  
  234. The shell file name (sans directories) is used to make a symbol name
  235. such as `explicit-csh-arguments'.  If that symbol is a variable,
  236. its value is used as a list of arguments when invoking the shell.
  237. Otherwise, one argument `-i' is passed to the shell.
  238.  
  239. \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
  240.   (interactive)
  241.   (cond ((not (comint-check-proc "*shell*"))
  242.      (let* ((prog (or explicit-shell-file-name
  243.               (getenv "ESHELL")
  244.               (getenv "SHELL")
  245.               "/bin/sh"))             
  246.         (name (file-name-nondirectory prog))
  247.         (startfile (concat "~/.emacs_" name))
  248.         (xargs-name (intern-soft (concat "explicit-" name "-arguments"))))
  249.        (set-buffer (apply 'make-comint "shell" prog
  250.                   (if (file-exists-p startfile) startfile)
  251.                   (if (and xargs-name (boundp xargs-name))
  252.                   (symbol-value xargs-name)
  253.                   '("-i"))))
  254.        (shell-mode))))
  255.   (switch-to-buffer "*shell*"))
  256.  
  257.  
  258. ;;; Directory tracking
  259. ;;; ===========================================================================
  260. ;;; This code provides the shell mode input sentinel
  261. ;;;     SHELL-DIRECTORY-TRACKER
  262. ;;; that tracks cd, pushd, and popd commands issued to the shell, and
  263. ;;; changes the current directory of the shell buffer accordingly.
  264. ;;;
  265. ;;; This is basically a fragile hack, although it's more accurate than
  266. ;;; the released version in shell.el. It has the following failings:
  267. ;;; 1. It doesn't know about the cdpath shell variable.
  268. ;;; 2. It only spots the first command in a command sequence. E.g., it will
  269. ;;;    miss the cd in "ls; cd foo"
  270. ;;; 3. More generally, any complex command (like ";" sequencing) is going to
  271. ;;;    throw it. Otherwise, you'd have to build an entire shell interpreter in
  272. ;;;    emacs lisp.  Failing that, there's no way to catch shell commands where
  273. ;;;    cd's are buried inside conditional expressions, aliases, and so forth.
  274. ;;;
  275. ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
  276. ;;; messes it up. You run other processes under the shell; these each have
  277. ;;; separate working directories, and some have commands for manipulating
  278. ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
  279. ;;; commands that do *not* effect the current w.d. at all, but look like they
  280. ;;; do (e.g., the cd command in ftp).  In shells that allow you job
  281. ;;; control, you can switch between jobs, all having different w.d.'s. So
  282. ;;; simply saying %3 can shift your w.d..
  283. ;;;
  284. ;;; The solution is to relax, not stress out about it, and settle for
  285. ;;; a hack that works pretty well in typical circumstances. Remember
  286. ;;; that a half-assed solution is more in keeping with the spirit of Unix, 
  287. ;;; anyway. Blech.
  288. ;;;
  289. ;;; One good hack not implemented here for users of programmable shells
  290. ;;; is to program up the shell w.d. manipulation commands to output
  291. ;;; a coded command sequence to the tty. Something like
  292. ;;;     ESC | <cwd> |
  293. ;;; where <cwd> is the new current working directory. Then trash the
  294. ;;; directory tracking machinery currently used in this package, and
  295. ;;; replace it with a process filter that watches for and strips out
  296. ;;; these messages.
  297.  
  298. ;;; REGEXP is a regular expression. STR is a string. START is a fixnum.
  299. ;;; Returns T if REGEXP matches STR where the match is anchored to start
  300. ;;; at position START in STR. Sort of like LOOKING-AT for strings.
  301. (defun shell-front-match (regexp str start)
  302.   (eq start (string-match regexp str start)))
  303.  
  304. (defun shell-directory-tracker (str)
  305.   "Tracks cd, pushd and popd commands issued to the shell.
  306. This function is called on each input passed to the shell.
  307. It watches for cd, pushd and popd commands and sets the buffer's
  308. default directory to track these commands.
  309.  
  310. You may toggle this tracking on and off with M-x shell-dirtrack-toggle.
  311. If emacs gets confused, you can resync with the shell with M-x shell-resync-dirs.
  312.  
  313. See variables shell-cd-regexp, shell-pushd-regexp, and shell-popd-regexp."
  314.   (cond (shell-dirtrackp
  315.      (string-match "^\\s *" str) ; skip whitespace
  316.      (let ((bos (match-end 0))
  317.            (x nil))
  318.        (cond ((setq x (shell-match-cmd-w/optional-arg shell-popd-regexp
  319.                                  str bos))
  320.           (shell-process-popd x))
  321.          ((setq x (shell-match-cmd-w/optional-arg shell-pushd-regexp
  322.                                  str bos))
  323.           (shell-process-pushd x))
  324.          ((setq x (shell-match-cmd-w/optional-arg shell-cd-regexp
  325.                                  str bos))
  326.           (shell-process-cd x)))))))
  327.  
  328.  
  329. ;;; Try to match regexp CMD to string, anchored at position START.
  330. ;;; CMD may be followed by a single argument. If a match, then return
  331. ;;; the argument, if there is one, or the empty string if not. If
  332. ;;; no match, return nil.
  333.  
  334. (defun shell-match-cmd-w/optional-arg (cmd str start)
  335.   (and (shell-front-match cmd str start)
  336.        (let ((eoc (match-end 0))) ; end of command
  337.      (cond ((shell-front-match "\\s *\\(\;\\|$\\)" str eoc)
  338.         "")            ; no arg
  339.            ((shell-front-match "\\s +\\([^ \t\;]+\\)\\s *\\(\;\\|$\\)"
  340.                       str eoc)
  341.         (substring str (match-beginning 1) (match-end 1))) ; arg
  342.            (t nil))))) ; something else.
  343. ;;; The first regexp is [optional whitespace, (";" or the end of string)].
  344. ;;; The second regexp is [whitespace, (an arg), optional whitespace,
  345. ;;;     (";" or end of string)].
  346.  
  347.  
  348. (defun shell-snarf-bang-dollar ()
  349.   ;; If the most-recently-typed shell command-line ended in a cd or pushd
  350.   ;; command whose argument was !$, then this returns the last token on
  351.   ;; the line of the previous input (as recorded in the input history.)
  352.   ;; I wrote this because I tend to do "cd !$" fairly often, and this makes
  353.   ;; emacs understand that at least some of the time.
  354.   (condition-case nil
  355.       (let ((prev-command (ring-ref (get-input-ring) 1)))
  356.     (if (string-match "[ \t]+\\([^ \t\n;]+\\)[ \t]*\\'" prev-command)
  357.         (substring prev-command (match-beginning 1) (match-end 1))))
  358.     (error nil)))
  359.  
  360. ;;; popd [+n]
  361. (defun shell-process-popd (arg)
  362.   (let ((num (if (zerop (length arg)) 0 ; no arg means +0
  363.          (shell-extract-num arg))))
  364.     (if (and num (< num (length shell-dirstack)))
  365.     (if (= num 0) ; condition-case because the CD could lose.
  366.         (condition-case nil (progn (comint-cd (car shell-dirstack))
  367.                        (setq shell-dirstack
  368.                          (cdr shell-dirstack))
  369.                        (shell-dirstack-message))
  370.           (error (message "Couldn't cd.")))
  371.         (let* ((ds (cons nil shell-dirstack))
  372.            (cell (nthcdr (- num 1) ds)))
  373.           (rplacd cell (cdr (cdr cell)))
  374.           (setq shell-dirstack (cdr ds))
  375.           (shell-dirstack-message)))
  376.     (message "Bad popd."))))
  377.  
  378.  
  379. ;;; cd [dir]
  380. (defun shell-process-cd (arg)
  381.   (if (equal arg "!$") (setq arg (shell-snarf-bang-dollar)))
  382.   (condition-case nil (progn (comint-cd (if (zerop (length arg)) (getenv "HOME")
  383.                      arg))
  384.                  (shell-dirstack-message))
  385.        (error (message "Couldn't cd."))))
  386.  
  387.  
  388. ;;; pushd [+n | dir]
  389. (defun shell-process-pushd (arg)
  390.   (if (equal arg "!$") (setq arg (shell-snarf-bang-dollar)))
  391.   (if (zerop (length arg))
  392.       ;; no arg -- swap pwd and car of shell stack
  393.       (condition-case nil (if shell-dirstack
  394.                   (let ((old default-directory))
  395.                 (comint-cd (car shell-dirstack))
  396.                 (setq shell-dirstack
  397.                       (cons old (cdr shell-dirstack)))
  398.                 (shell-dirstack-message))
  399.                   (message "Directory stack empty."))
  400.     (error (message "Couldn't cd.")))
  401.  
  402.       (let ((num (shell-extract-num arg)))
  403.     (if num                ; pushd +n
  404.         (if (> num (length shell-dirstack))
  405.         (message "Directory stack not that deep.")
  406.         (let* ((ds (cons default-directory shell-dirstack))
  407.                (dslen (length ds))
  408.                (front (nthcdr num ds))
  409.                (back (reverse (nthcdr (- dslen num) (reverse ds))))
  410.                (new-ds (append front back)))
  411.           (condition-case nil
  412.               (progn (comint-cd (car new-ds))
  413.                  (setq shell-dirstack (cdr new-ds))
  414.                  (shell-dirstack-message))
  415.             (error (message "Couldn't cd.")))))
  416.            
  417.         ;; pushd <dir>
  418.         (let ((old-wd default-directory))
  419.           (condition-case nil
  420.           (progn (comint-cd arg)
  421.              (setq shell-dirstack
  422.                    (cons old-wd shell-dirstack))
  423.              (shell-dirstack-message))
  424.         (error (message "Couldn't cd."))))))))
  425.  
  426. ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
  427. (defun shell-extract-num (str)
  428.   (and (string-match "^\\+[1-9][0-9]*$" str)
  429.        (string-to-int str)))
  430.  
  431.  
  432. (defun shell-dirtrack-toggle ()
  433.   "Turn directory tracking on and off in a shell buffer."
  434.   (interactive)
  435.   (setq shell-dirtrackp (not shell-dirtrackp))
  436.   (message "directory tracking %s."
  437.        (if shell-dirtrackp "ON" "OFF")))
  438.  
  439. (defun shell-resync-dirs ()
  440.   "Resync the buffer's idea of the current directory stack.
  441. This command queries the shell with the command bound to 
  442. shell-dirstack-query (default \"dirs\"), reads the next
  443. line output and parses it to form the new directory stack.
  444. DON'T issue this command unless the buffer is at a shell prompt.
  445. Also, note that if some other subprocess decides to do output
  446. immediately after the query, its output will be taken as the
  447. new directory stack -- you lose. If this happens, just do the
  448. command again."
  449.   (interactive)
  450.   (let* ((proc (get-buffer-process (current-buffer)))
  451.      (pmark (process-mark proc)))
  452.     (goto-char pmark)
  453.     (insert shell-dirstack-query) (insert "\n")
  454.     (sit-for 0) ; force redisplay
  455.     (comint-send-string proc shell-dirstack-query) 
  456.     (comint-send-string proc "\n")
  457.     (set-marker pmark (point))
  458.     (let ((pt (point))) ; wait for 1 line
  459.       ;; This extra newline prevents the user's pending input from spoofing us.
  460.       (insert "\n") (backward-char 1)
  461.       (while (not (looking-at ".+\n"))
  462.     (accept-process-output proc)
  463.     (goto-char pt)))
  464.     (goto-char pmark) (delete-char 1) ; remove the extra newline
  465.     ;; That's the dirlist. grab it & parse it.
  466.     (let* ((dl (buffer-substring (match-beginning 0) (- (match-end 0) 1)))
  467.        (dl-len (length dl))
  468.        (ds '())            ; new dir stack
  469.        (i 0))
  470.       (while (< i dl-len)
  471.     ;; regexp = optional whitespace, (non-whitespace), optional whitespace
  472.     (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
  473.     (setq ds (cons (substring dl (match-beginning 1) (match-end 1))
  474.                ds))
  475.     (setq i (match-end 0)))
  476.       (let ((ds (reverse ds)))
  477.     (condition-case nil
  478.         (progn (comint-cd (car ds))
  479.            (setq shell-dirstack (cdr ds))
  480.            (shell-dirstack-message))
  481.       (error (message "Couldn't cd.")))))))
  482.  
  483. (defun comint-cd (d)
  484.   "Substitute environment variables before calling cd."
  485.   (cd (substitute-in-file-name d)))
  486.  
  487. (defvar shell-dirstack-message-hook nil
  488.   "Hook to run after a cd, pushd or popd event")
  489.  
  490. ;;; Show the current dirstack on the message line.
  491. ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
  492. ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
  493. ;;; All the commands that mung the buffer's dirstack finish by calling
  494. ;;; this guy.
  495. (defun shell-dirstack-message ()
  496.   (let ((msg "")
  497.     (ds (cons default-directory shell-dirstack)))
  498.     (while ds
  499.       (let ((dir (car ds)))
  500.     (if (string-match (format "^%s\\(/\\|$\\)" (getenv "HOME")) dir)
  501.         (setq dir (concat "~/" (substring dir (match-end 0)))))
  502.     (if (string-equal dir "~/") (setq dir "~"))
  503.     (setq msg (concat msg dir " "))
  504.     (setq ds (cdr ds))))
  505.     (run-hooks 'shell-dirstack-message-hook)
  506.     (message msg)))
  507.  
  508.  
  509.  
  510. ;;; Interfacing to client packages (and converting them)
  511. ;;;============================================================================
  512. ;;; Several gnu packages (tex-mode, background, dbx, gdb, kermit, prolog, 
  513. ;;; telnet are some) use the shell package as clients. Most of them would
  514. ;;; be better off using the comint package directly, but they predate it.
  515. ;;; The catch is that most of these packages (dbx, gdb, prolog, telnet)
  516. ;;; assume total knowledge of all the local variables that shell mode
  517. ;;; functions depend on. So they (kill-all-local-variables), then create
  518. ;;; the few local variables that shell.el functions depend on. Alas,
  519. ;;; shell.el functions depend on a different set of vars (for example,
  520. ;;; the input history ring is a local variable in shell.el's shell mode,
  521. ;;; whereas there is no input history ring in shell.el's shell mode).
  522. ;;; So we have a situation where the greater functionality of shell.el
  523. ;;; is biting us -- you can't just replace shell will shell.
  524. ;;;
  525. ;;; Altering these packages to use comint mode directly should *greatly*
  526. ;;; improve their functionality, and is actually pretty easy. It's
  527. ;;; mostly a matter of renaming a few variable names. See comint.el for more.
  528. ;;;     -Olin
  529.  
  530.  
  531.  
  532. ;;; Do the user's customisation...
  533. ;;;===============================
  534. (defvar shell-load-hook nil
  535.   "This hook is run when shell is loaded in.
  536. This is a good place to put keybindings.")
  537.     
  538. (run-hooks 'shell-load-hook)
  539.  
  540. ;;; Change Log
  541. ;;; ===========================================================================
  542. ;;; Olin 8/88
  543. ;;; Created.
  544. ;;;
  545. ;;; Olin 5/26/90
  546. ;;; - Split cmulisp and cmushell modes into separate files. 
  547. ;;;   Not only is this a good idea, it's apparently the way it'll be rel 19.
  548. ;;; - Souped up the directory tracking; it now can handle pushd, pushd +n, 
  549. ;;;   and popd +n.
  550. ;;; - Added cmushell-dirtrack-toggle command to toggle the directory
  551. ;;;   tracking that cmushell tries to do. This is useful, for example,
  552. ;;;   when you are running ftp -- it prevents the ftp "cd" command from
  553. ;;;   spoofing the tracking machinery.
  554. ;;; - Added cmushell-resync-dirs command. This queries the shell
  555. ;;;   for the current directory stack, and resets the buffer's stack
  556. ;;;   accordingly.
  557. ;;; - Bits of the new directory tracking code were adapted from source
  558. ;;;   contributed by Vince Broman, Jeff Peck, and Barry Warsaw.
  559. ;;; - See also the improvements made to comint.el at the same time.
  560. ;;; - Renamed several variables. Mostly this comprised changing "shell"
  561. ;;;   to "cmushell" in the names. The only variables that are not prefixed
  562. ;;;   with "cmushell-" are the ones that are common with shell.el:
  563. ;;;       explicit-shell-file-name shell-prompt-pattern explicit-csh-args 
  564. ;;;       and shell-cd/popd/pushd-regexp
  565. ;;;   The variables and functions that were changed to have "cmushell-" 
  566. ;;;   prefixes are:
  567. ;;;       shell-directory-stack (v), shell-directory-tracker (f)
  568. ;;;   This should not affect users, only elisp hackers. Hopefully
  569. ;;;   one day shell.el will just go away, and we can drop all this
  570. ;;;   "cmushell" bullshit.
  571. ;;; - Upgraded process sends to use comint-send-string instead of
  572. ;;;   process-send-string.
  573. ;;;
  574. ;;; Olin 6/14/90
  575. ;;; - If your shell is named <shellname>, and a variable named
  576. ;;;   explicit-<shellname>-arguments exists, cmushell is supposed
  577. ;;;   to use its value as the arglist to the shell invocation.
  578. ;;;   E.g., if you define explicit-csh-arguments to be 
  579. ;;;   ("-ifx"), then when cmushell cranks up a csh, it execs it
  580. ;;;   as "csh -ifx". This is what is documented. What has actually
  581. ;;;   been the case is that the variable checked is
  582. ;;;   explicit-<shellname>-args, not explicit-<shellname>-arguments.
  583. ;;;   The code has been changed to conform to the documentation.
  584. ;;;   This bug reported by Stephen Anderson.
  585. ;;;
  586. ;;; Eric Benson 7/10/91
  587. ;;;   Adapted for Emacs 19 by renaming cmushell to shell everywhere.
  588. ;;;   Removed (fset 'dirs 'shell-resync-dirs), as that causes M-X dir
  589. ;;;    to not call dired.
  590. ;;;   Instead of calling cd directly, use comint-cd which does
  591. ;;;    substitute-in-file-name.
  592.