all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Chong Yidong <cyd@stupidchicken.com>
Cc: emacs-devel@gnu.org
Subject: Re: [david.hansen@gmx.net: Re: comint's directory tracking doesn't understand \( or \)]
Date: Sun, 04 Mar 2007 16:45:23 -0500	[thread overview]
Message-ID: <jwvmz2sd79g.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87irdgkdy1.fsf@stupidchicken.com> (Chong Yidong's message of "Sun\, 04 Mar 2007 14\:26\:30 -0500")

> 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.

  parent reply	other threads:[~2007-03-04 21:45 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-02 17:44 [david.hansen@gmx.net: Re: comint's directory tracking doesn't understand \( or \)] Richard Stallman
2007-03-04 13:13 ` David Hansen
2007-03-04 15:45   ` Chong Yidong
2007-03-04 15:51     ` David Kastrup
2007-03-04 19:26       ` Chong Yidong
2007-03-04 19:32         ` David Kastrup
2007-03-04 19:39           ` Lennart Borgman (gmail)
2007-03-04 20:16             ` David Kastrup
2007-03-04 20:25               ` Lennart Borgman (gmail)
2007-03-04 19:47           ` Miles Bader
2007-03-04 21:45         ` Stefan Monnier [this message]
2007-03-04 22:06           ` Andreas Seltenreich
2007-03-04 23:42             ` Stefan Monnier
2007-03-04 23:13         ` David Hansen
2007-03-04 23:30           ` David Kastrup
2007-03-05  2:09             ` David Hansen
2007-03-04 19:40       ` Robert J. Chassell
2007-03-04 20:17         ` David Kastrup
2007-03-04 23:22     ` Chris Moore
2007-03-04 23:23       ` Tom Tromey
2007-03-05  2:55   ` Richard Stallman
2007-03-05  6:23     ` David Hansen
2007-03-05 21:50       ` Richard Stallman
2007-03-06  2:23         ` Stefan Monnier
2007-03-06  3:10           ` David Hansen
2007-03-06 22:36           ` Richard Stallman
2007-03-07  0:48           ` Miles Bader
2007-03-07 14:49             ` David Hansen
2007-03-08  3:16               ` Richard Stallman
2007-03-09 19:55               ` Chong Yidong
2007-03-09 20:28                 ` Glenn Morris
2007-03-09 20:45                   ` David Hansen
2007-03-09 21:08                     ` David Kastrup
2007-03-10  0:04                 ` Chong Yidong
2007-03-10  8:06                   ` David Hansen
2007-03-10 20:18                   ` Chong Yidong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvmz2sd79g.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=cyd@stupidchicken.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.