emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: address@hidden: Re: comint's directory tracking doesn't understand \


From: Stefan Monnier
Subject: Re: address@hidden: Re: comint's directory tracking doesn't understand \( or \)]
Date: Sun, 04 Mar 2007 16:45:23 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.93 (gnu/linux)

> I don't think we should make the proposed change to comint.el.

Agreed.  It's simply too late for Emacs-22.

> AFAIK, the detailed rules for how backslash escape works is, in principle,
> different from shell to shell, and even if we choose to obey (e.g.)  bash
> semantics for backslash escapes, we might still be incompatible with other
> shells.  This might also introduce subtle bugs into non-shell uses of
> comint mode.

Agreed as well.

> It seems there will always be *some* way of confusing the directory
> tracker: that's what `M-x dirs' is for.

Yes.  Of course we can still improve the code that does it automatically.
E.g. in the example session, the directory is actually available directly in
the prompt, so we could tell shell.el to use it.  I have a proof-of-concept
local hack that does just that (it also works for prompts such as mine which
only include the last 2-3 levels of the full directory name).

Another improvement could be to check that the guessed new working directory
exists and if it doesn't, to try harder to find it.  E.g. by trying to
remove the backslashes ;-)

And if you want the dir-tracking to really work reliably, there's
always eshell.


        Stefan


PS: Repeat: this is just experimental code, not intended for installation.
The regexp in the code is the one that matches my particular prompt-shape.
I might very well be the only one on this planet to use such a prompt.


--- orig/lisp/shell.el
+++ mod/lisp/shell.el
@@ -472,6 +472,10 @@
       (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)
@@ -609,6 +608,31 @@
 ;; replace it with a process filter that watches for and strips out
 ;; these messages.
 
+(defcustom shell-dir-cookie-re "^[a-z]+-\\(.*/.*\\)-[0-9]+% "
+  "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'."
+  :type '(choice (const nil) regexp))
+
+(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.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]