[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode)
From: |
Augusto Stoffel |
Subject: |
Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode) |
Date: |
Sat, 28 Aug 2021 17:15:50 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
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.)
>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
- OSC control sequences in comint, Augusto Stoffel, 2021/08/22
- Re: OSC control sequences in comint, Lars Ingebrigtsen, 2021/08/22
- Re: OSC control sequences in comint, Augusto Stoffel, 2021/08/23
- Re: OSC control sequences in comint, Lars Ingebrigtsen, 2021/08/25
- Re: OSC control sequences in comint, Augusto Stoffel, 2021/08/26
- Re: OSC control sequences in comint, Lars Ingebrigtsen, 2021/08/26
- Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode),
Augusto Stoffel <=
- Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode), Michael Albinus, 2021/08/29
- Re: [PATCH] OSC 7 handler (alternative to shell-dirtrack-mode), Lars Ingebrigtsen, 2021/08/29