Refill some copyright headers.
[bpt/emacs.git] / lisp / shell.el
index 4d8f3b9..5d27efa 100644 (file)
@@ -1,7 +1,8 @@
 ;;; shell.el --- specialized comint.el for running the shell
 
-;; Copyright (C) 1988, 1993, 1994, 1995, 1996, 1997, 2000, 2001,
-;;   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1988, 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002,
+;;   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
+;;   Free Software Foundation, Inc.
 
 ;; Author: Olin Shivers <shivers@cs.cmu.edu>
 ;;     Simon Marshall <simon@gnu.org>
@@ -70,7 +71,7 @@
 ;; c-c c-c comint-interrupt-subjob        ^c
 ;; c-c c-z comint-stop-subjob             ^z
 ;; c-c c-\ comint-quit-subjob             ^\
-;; c-c c-o comint-kill-output             Delete last batch of process output
+;; c-c c-o comint-delete-output                   Delete last batch of process output
 ;; c-c c-r comint-show-output             Show last batch of process output
 ;; c-c c-l comint-dynamic-list-input-ring  List input history
 ;;         send-invisible                  Read line w/o echo & send to proc
   :group 'shell)
 
 ;;;###autoload
-(defcustom shell-dumb-shell-regexp "cmd\\(proxy\\)?\\.exe"
+(defcustom shell-dumb-shell-regexp (purecopy "cmd\\(proxy\\)?\\.exe")
   "Regexp to match shells that don't save their command history, and
 don't handle the backslash as a quote character.  For shells that
 match this regexp, Emacs will write out the command history when the
@@ -172,7 +173,7 @@ This is a fine thing to set in your `.emacs' file.")
 (defvar shell-file-name-quote-list
   (if (memq system-type '(ms-dos windows-nt))
       nil
-    (append shell-delimiter-argument-list '(?\s ?\* ?\! ?\" ?\' ?\` ?\# ?\\)))
+    (append shell-delimiter-argument-list '(?\s ?$ ?\* ?\! ?\" ?\' ?\` ?\# ?\\)))
   "List of characters to quote when in a file name.
 This variable is used to initialize `comint-file-name-quote-list' in the
 shell buffer.  The value may depend on the operating system or shell.
@@ -184,7 +185,8 @@ This is a fine thing to set in your `.emacs' file.")
     shell-dynamic-complete-environment-variable
     shell-dynamic-complete-command
     shell-replace-by-expanded-directory
-    shell-dynamic-complete-filename)
+    shell-dynamic-complete-filename
+    comint-dynamic-complete-filename)
   "List of functions called to perform completion.
 This variable is used to initialize `comint-dynamic-complete-functions' in the
 shell buffer.
@@ -333,28 +335,28 @@ Thus, this does not include the shell's current directory.")
 (defvar shell-dirstack-query nil
   "Command used by `shell-resync-dirs' to query the shell.")
 
-(defvar shell-mode-map nil)
-(cond ((not shell-mode-map)
-       (setq shell-mode-map (nconc (make-sparse-keymap) comint-mode-map))
-       (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
-       (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
-       (define-key shell-mode-map "\t" 'comint-dynamic-complete)
-       (define-key shell-mode-map "\M-?"
-        'comint-dynamic-list-filename-completions)
-       (define-key shell-mode-map [menu-bar completion]
-        (cons "Complete"
-              (copy-keymap (lookup-key comint-mode-map [menu-bar completion]))))
-       (define-key-after (lookup-key shell-mode-map [menu-bar completion])
-        [complete-env-variable] '("Complete Env. Variable Name" .
-                                  shell-dynamic-complete-environment-variable)
-        'complete-file)
-       (define-key-after (lookup-key shell-mode-map [menu-bar completion])
-        [expand-directory] '("Expand Directory Reference" .
-                             shell-replace-by-expanded-directory)
-        'complete-expand)))
+(defvar shell-mode-map
+  (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
+    (define-key map "\C-c\C-f" 'shell-forward-command)
+    (define-key map "\C-c\C-b" 'shell-backward-command)
+    (define-key map "\t" 'comint-dynamic-complete)
+    (define-key map (kbd "M-RET") 'shell-resync-dirs)
+    (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
+    (define-key map [menu-bar completion]
+      (cons "Complete"
+           (copy-keymap (lookup-key comint-mode-map [menu-bar completion]))))
+    (define-key-after (lookup-key map [menu-bar completion])
+      [complete-env-variable] '("Complete Env. Variable Name" .
+                               shell-dynamic-complete-environment-variable)
+      'complete-file)
+    (define-key-after (lookup-key map [menu-bar completion])
+      [expand-directory] '("Expand Directory Reference" .
+                          shell-replace-by-expanded-directory)
+      'complete-expand)
+    map))
 
 (defcustom shell-mode-hook '()
-  "*Hook for customising Shell mode."
+  "Hook for customizing Shell mode."
   :type 'hook
   :group 'shell)
 
@@ -366,6 +368,17 @@ Thus, this does not include the shell's current directory.")
 
 ;;; Basic Procedures
 
+(defcustom shell-dir-cookie-re nil
+  "Regexp matching your prompt, including some part of the current directory.
+If your prompt includes the current directory or the last few elements of it,
+set this to a pattern that matches your prompt and whose subgroup 1 matches
+the directory part of it.
+This is used by `shell-dir-cookie-watcher' to try and use this info
+to track your current directory.  It can be used instead of or in addition
+to `dirtrack-mode'."
+  :group 'shell
+  :type '(choice (const nil) regexp))
+
 (put 'shell-mode 'mode-class 'special)
 
 (define-derived-mode shell-mode comint-mode "Shell"
@@ -439,10 +452,9 @@ buffer."
   (make-local-variable 'shell-last-dir)
   (setq shell-last-dir nil)
   (setq comint-input-autoexpand shell-input-autoexpand)
+  (shell-dirtrack-mode 1)
   ;; This is not really correct, since the shell buffer does not really
   ;; edit this directory.  But it is useful in the buffer list and menus.
-  (make-local-variable 'list-buffers-directory)
-  (shell-dirtrack-mode 1)
   (setq list-buffers-directory (expand-file-name default-directory))
   ;; shell-dependent assignments.
   (when (ring-empty-p comint-input-ring)
@@ -471,6 +483,10 @@ buffer."
       (when (string-equal shell "bash")
         (add-hook 'comint-output-filter-functions
                   'shell-filter-ctrl-a-ctrl-b nil t)))
+    (when shell-dir-cookie-re
+      ;; Watch for magic cookies in the output to track the current dir.
+      (add-hook 'comint-output-filter-functions
+               'shell-dir-cookie-watcher nil t))
     (comint-read-input-ring t)))
 
 (defun shell-filter-ctrl-a-ctrl-b (string)
@@ -549,12 +565,19 @@ Otherwise, one argument `-i' is passed to the shell.
                          (generate-new-buffer-name "*shell*"))
           (if (file-remote-p default-directory)
               ;; It must be possible to declare a local default-directory.
+               ;; FIXME: This can't be right: it changes the default-directory
+               ;; of the current-buffer rather than of the *shell* buffer.
               (setq default-directory
                     (expand-file-name
                      (read-file-name
                       "Default directory: " default-directory default-directory
                       t nil 'file-directory-p))))))))
-  (setq buffer (get-buffer-create (or buffer "*shell*")))
+  (require 'ansi-color)
+  (setq buffer (if (or buffer (not (derived-mode-p 'shell-mode))
+                       (comint-check-proc (current-buffer)))
+                   (get-buffer-create (or buffer "*shell*"))
+                 ;; If the current buffer is a dead shell buffer, use it.
+                 (current-buffer)))
   ;; Pop to buffer, so that the buffer's window will be correctly set
   ;; when we call comint (so that comint sets the COLUMNS env var properly).
   (pop-to-buffer buffer)
@@ -575,7 +598,7 @@ Otherwise, one argument `-i' is passed to the shell.
   buffer)
 
 ;; Don't do this when shell.el is loaded, only while dumping.
-;;;###autoload (add-hook 'same-window-buffer-names "*shell*")
+;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*shell*"))
 
 ;;; Directory tracking
 ;;
@@ -617,6 +640,20 @@ Otherwise, one argument `-i' is passed to the shell.
 ;; replace it with a process filter that watches for and strips out
 ;; these messages.
 
+(defun shell-dir-cookie-watcher (text)
+  ;; This is fragile: the TEXT could be split into several chunks and we'd
+  ;; miss it.  Oh well.  It's a best effort anyway.  I'd expect that it's
+  ;; rather unusual to have the prompt split into several packets, but
+  ;; I'm sure Murphy will prove me wrong.
+  (when (and shell-dir-cookie-re (string-match shell-dir-cookie-re text))
+    (let ((dir (match-string 1 text)))
+      (cond
+       ((file-name-absolute-p dir) (shell-cd dir))
+       ;; Let's try and see if it seems to be up or down from where we were.
+       ((string-match "\\`\\(.*\\)\\(?:/.*\\)?\n\\(.*/\\)\\1\\(?:/.*\\)?\\'"
+                     (setq text (concat dir "\n" default-directory)))
+       (shell-cd (concat (match-string 2 text) dir)))))))
+
 (defun shell-directory-tracker (str)
   "Tracks cd, pushd and popd commands issued to the shell.
 This function is called on each input passed to the shell.
@@ -699,7 +736,7 @@ Environment variables are expanded, see function `substitute-in-file-name'."
 (defun shell-process-popd (arg)
   (let ((num (or (shell-extract-num arg) 0)))
     (cond ((and num (= num 0) shell-dirstack)
-          (shell-cd (car shell-dirstack))
+          (shell-cd (shell-prefixed-directory-name (car shell-dirstack)))
           (setq shell-dirstack (cdr shell-dirstack))
           (shell-dirstack-message))
          ((and num (> num 0) (<= num (length shell-dirstack)))
@@ -927,7 +964,7 @@ Copy Shell environment variable to Emacs: ")))
   "Move forward across ARG shell command(s).  Does not cross lines.
 See `shell-command-regexp'."
   (interactive "p")
-  (let ((limit (save-excursion (end-of-line nil) (point))))
+  (let ((limit (line-end-position)))
     (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+")
                           limit 'move arg)
        (skip-syntax-backward " "))))
@@ -1110,5 +1147,4 @@ Returns t if successful."
 
 (provide 'shell)
 
-;; arch-tag: bcb5f12a-c1f4-4aea-a809-2504bd5bd797
 ;;; shell.el ends here