From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [david.hansen@gmx.net: Re: comint's directory tracking doesn't understand \( or \)] Date: Sun, 04 Mar 2007 16:45:23 -0500 Message-ID: References: <871wk56tjh.fsf@localhorst.mine.nu> <87wt1xhv0u.fsf@stupidchicken.com> <85ejo5j9bj.fsf@lola.goethe.zz> <87irdgkdy1.fsf@stupidchicken.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1173044754 3973 80.91.229.12 (4 Mar 2007 21:45:54 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 4 Mar 2007 21:45:54 +0000 (UTC) Cc: emacs-devel@gnu.org To: Chong Yidong Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Mar 04 22:45:43 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HNyWT-0005y2-Dl for ged-emacs-devel@m.gmane.org; Sun, 04 Mar 2007 22:45:41 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HNyWS-00076b-UC for ged-emacs-devel@m.gmane.org; Sun, 04 Mar 2007 16:45:40 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1HNyWH-00076W-AK for emacs-devel@gnu.org; Sun, 04 Mar 2007 16:45:29 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1HNyWE-00076J-QB for emacs-devel@gnu.org; Sun, 04 Mar 2007 16:45:28 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HNyWE-00076G-LI for emacs-devel@gnu.org; Sun, 04 Mar 2007 16:45:26 -0500 Original-Received: from tomts36.bellnexxia.net ([209.226.175.93] helo=tomts36-srv.bellnexxia.net) by monty-python.gnu.org with esmtp (Exim 4.52) id 1HNyWD-0005HW-BB; Sun, 04 Mar 2007 16:45:25 -0500 Original-Received: from pastel.home ([74.12.206.221]) by tomts36-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20070304214523.BQIT1612.tomts36-srv.bellnexxia.net@pastel.home>; Sun, 4 Mar 2007 16:45:23 -0500 Original-Received: by pastel.home (Postfix, from userid 20848) id 6929C7F42; Sun, 4 Mar 2007 16:45:23 -0500 (EST) In-Reply-To: <87irdgkdy1.fsf@stupidchicken.com> (Chong Yidong's message of "Sun\, 04 Mar 2007 14\:26\:30 -0500") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.93 (gnu/linux) X-detected-kernel: Solaris 8 (1) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:67318 Archived-At: > 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.