all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: emacs-devel@gnu.org
Subject: Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode)
Date: Sat, 28 Aug 2021 17:15:50 +0200	[thread overview]
Message-ID: <87ilzpzjkp.fsf_-_@gmail.com> (raw)
In-Reply-To: <87lf4o1es2.fsf@gnus.org> (Lars Ingebrigtsen's message of "Thu, 26 Aug 2021 16:06:37 +0200")

[-- Attachment #1: Type: text/plain, Size: 1309 bytes --]

On Thu, 26 Aug 2021 at 16:06, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> Augusto Stoffel <arstoffel@gmail.com> writes:
>
>> Great!  Another useful addition might be OSC 7 to report the working
>> directory.  This would be much simpler and more reliable than
>> 'shell-dirtrack-mode', but needs a small configuration in the shell as
>> well (append some stuff to $PS1 or $PROMPT_COMMAND)
>
> Yes, that sounds quite useful.
>

I've attached a patch to handle OSC 7, which is a protocol for the
inferior process to tell the terminal about directory changes.  This
replaces the functionality of shell-dirtrack-mode and dirtrack-mode,
perhaps in a less hacky way.

Unfortunately, it needs a bit of configuration on the shell side too (no
system will be preconfigured to emit OSCs to a dumb terminal).  A
slightly naive approach is run

  PROMPT_COMMAND='printf "\e]7;file://%s%s\e\\" "$HOSTNAME" "$PWD"'

or add this to the bashrc file.

On my system bash comes with a `__vte_osc7' function which is similar
but slightly more robust (it urlencodes the PWD).

(I've also removed some borderline paranoid checks on the variable
`comint-last-output-start' which I had copied from ansi-color.  On
closer inspection, those checks are working around a bug in gdb-mi.
Good opportunity to check if that bug was fixed.)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-support-for-OSC-7-in-comint-current-directory-tr.patch --]
[-- Type: text/x-patch, Size: 3007 bytes --]

From 2e6127373f9fbd4126beeb136e86f2f8e7d1e3d6 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 28 Aug 2021 15:47:49 +0200
Subject: [PATCH] Add support for OSC 7 in comint (current directory tracking)

* lisp/comint.el (comint-osc-directory-tracker, comint-osc-handlers):
Define and register a handler for OSC 7.
(comint-osc-process-output): Do fewer checks on
'comint-last-output-start'.
---
 lisp/comint.el | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index b924029a3b..be1c0fbf24 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -3896,7 +3896,8 @@ comint-redirect-results-list-from-process
 ;; OSC 8, for hyperlinks, is acted upon.  Adding more entries to
 ;; `comint-osc-handlers' allows a customized treatment of further sequences.
 
-(defvar-local comint-osc-handlers '(("8" . comint-osc-hyperlink-handler))
+(defvar-local comint-osc-handlers '(("7" . comint-osc-directory-tracker)
+                                    ("8" . comint-osc-hyperlink-handler))
   "Alist of handlers for OSC escape sequences.
 See `comint-osc-process-output' for details.")
 
@@ -3919,12 +3920,8 @@ comint-osc-process-output
 arguments, with point where the escape sequence was located."
   (let ((bound (process-mark (get-buffer-process (current-buffer)))))
     (save-excursion
-      (goto-char (or comint-osc--marker
-                     (and (markerp comint-last-output-start)
-			  (eq (marker-buffer comint-last-output-start)
-			      (current-buffer))
-			  comint-last-output-start)
-                     (point-min)))
+      ;; Start one char before last output to catch a possibly stray ESC
+      (goto-char (or comint-osc--marker (1- comint-last-output-start)))
       (when (eq (char-before) ?\e) (backward-char))
       (while (re-search-forward "\e]" bound t)
         (let ((pos0 (match-beginning 0))
@@ -3940,6 +3937,27 @@ comint-osc-process-output
             (put-text-property pos0 bound 'invisible t)
             (setq comint-osc--marker (copy-marker pos0))))))))
 
+;; Current directory tracking (OSC 7)
+
+(defun comint-osc-directory-tracker (_ text)
+  "Update `default-directory' from OSC 7 escape sequences.
+
+This function is intended to be included as an entry of
+`comint-osc-handlers'.  You should moreover arrange for your
+shell to print the appropriate escape sequence at each prompt,
+say with the following command:
+
+    printf \"\\e]7;file://%s%s\\e\\\\\" \"$HOSTNAME\" \"$PWD\"
+
+This functionality serves as an alternative to `dirtrack-mode'
+and `shell-dirtrack-mode'."
+  (let ((url (url-generic-parse-url text)))
+    (when (and (string= (url-type url) "file")
+               (or (null (url-host url))
+                   (string= (url-host url) (system-name))))
+      (ignore-errors
+        (cd-absolute (url-unhex-string (url-filename url)))))))
+
 ;; Hyperlink handling (OSC 8)
 
 (autoload 'browse-url-button-open "browse-url.el")
-- 
2.31.1


  reply	other threads:[~2021-08-28 15:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-22 17:31 OSC control sequences in comint Augusto Stoffel
2021-08-22 22:38 ` Lars Ingebrigtsen
2021-08-23 16:46   ` Augusto Stoffel
2021-08-25 10:32     ` Lars Ingebrigtsen
2021-08-26  7:44       ` Augusto Stoffel
2021-08-26 14:06         ` Lars Ingebrigtsen
2021-08-28 15:15           ` Augusto Stoffel [this message]
2021-08-29  8:06             ` [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode) Michael Albinus
2021-08-29 18:56             ` Lars Ingebrigtsen

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=87ilzpzjkp.fsf_-_@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.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.