[Top][All Lists]

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

Re: OSC control sequences in comint

From: Augusto Stoffel
Subject: Re: OSC control sequences in comint
Date: Mon, 23 Aug 2021 18:46:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

On Mon, 23 Aug 2021 at 00:38, Lars Ingebrigtsen <larsi@gnus.org> wrote:

> I think it sounds like a good idea.  Could you format it up as a patch?

All right, I have attached a patch.  Since a comint buffer is editable,
I chose an unobtrusive but less convenient keymap to the hyperlink
buttons, based on bug-reference.el: 'C-c RET' opens the links.

Note that there's a call to

    (autoload 'browse-url-button-open "browse-url.el")

in the patch, which is probably not the best procedure.  Should that
function be autoloaded in browse-url.el?

Finally, in case anyone wants to test or play around, a handler for the
OSC 1337 escape (for inline images) could look like this:

    (defun comint-osc-1337-handler (_ text)
      (when (string-match ":" text)
         (create-image (base64-decode-string (substring text (match-end 0))) 
nil t))))

Then one can say

    (add-hook 'comint-output-filter-functions 'comint-osc-process-output nil t)
    (push '("1337" . comint-osc-1337-handler) comint-osc-handlers)

and use https://iterm2.com/utilities/imgcat to show (small...) images in
the shell.

However, I don't think it makes sense to provide this handler in Emacs
itself, since it is rather nonstandard.


>From 290107a10b638e41a73509d346336566791e6525 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Mon, 23 Aug 2021 18:03:39 +0200
Subject: [PATCH] Add support for OSC escape sequences in comint

* lisp/comint.el (comint-osc-handlers, comint-osc--marker,
comint-osc-process-output): New general infrastructure to handle OSC
escape sequences.
(comint-osc-hyperlink-map, comint-osc-hyperlink--state,
comint-osc-hyperlink-handler): New functions and variables to handle
OSC 8 (hyperlinks).
 etc/NEWS       | 10 ++++++
 lisp/comint.el | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index b008c46291..ac8bec244d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3019,6 +3019,16 @@ default are unaffected.)
 states to be maintained if 'so-long-mode' replaces the original major
 mode.  By default, these new options support 'view-mode'.
+** Comint
+*** Support for OSC escape sequences
+Adding the new 'comint-osc-process-output' to
+'comint-output-filter-functions' enables the interpretation of OSC
+escape sequences in comint buffers.  By default, only OSC 8, for
+hyperlinks, is acted upon.  Adding more entries to
+`comint-osc-handlers' allows a customized treatment of further escape
 * New Modes and Packages in Emacs 28.1
diff --git a/lisp/comint.el b/lisp/comint.el
index 7af8e8fd2a..b924029a3b 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -3887,6 +3887,93 @@ comint-redirect-results-list-from-process
           ;; don't advance, so ensure forward progress.
          (forward-line 1)))
       (nreverse results))))
+;;; OSC escape sequences (operating system commands)
+;; Adding `comint-osc-process-output' to `comint-output-filter-functions'
+;; enables the interpretation of OSC escape sequences.  By default, only
+;; 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))
+  "Alist of handlers for OSC escape sequences.
+See `comint-osc-process-output' for details.")
+(defvar-local comint-osc--marker nil)
+(defun comint-osc-process-output (_)
+  "Interpret OSC escape sequences in comint output.
+This function is intended to be added to
+`comint-output-filter-functions' in order to interpret escape
+sequences of the forms
+    ESC ] command ; text BEL
+    ESC ] command ; text ESC \\
+Specifically, every occurrence of such escape sequences is
+removed from the buffer.  Then, if `command' is a key of the
+`comint-osc-handlers' alist, the corresponding value, which
+should be a function, is called with `command' and `text' as
+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)))
+      (when (eq (char-before) ?\e) (backward-char))
+      (while (re-search-forward "\e]" bound t)
+        (let ((pos0 (match-beginning 0))
+              (code (and (re-search-forward "\\=\\([0-9A-Za-z]*\\);" bound t)
+                         (match-string 1)))
+              (pos1 (point)))
+          (if (re-search-forward "\a\\|\e\\\\" bound t)
+              (let ((text (buffer-substring-no-properties pos1 
(match-beginning 0))))
+                (setq comint-osc--marker nil)
+                (delete-region pos0 (point))
+                (when-let ((fun (cdr (assoc-string code comint-osc-handlers))))
+                  (funcall fun code text)))
+            (put-text-property pos0 bound 'invisible t)
+            (setq comint-osc--marker (copy-marker pos0))))))))
+;; Hyperlink handling (OSC 8)
+(autoload 'browse-url-button-open "browse-url.el")
+(defvar comint-osc-hyperlink-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "\C-c\r" 'browse-url-button-open)
+    (define-key map [mouse-2] 'browse-url-button-open)
+    (define-key map [follow-link] 'mouse-face)
+    map)
+  "Keymap used by OSC 8 hyperlink buttons.")
+(define-button-type 'comint-osc-hyperlink
+  'keymap comint-osc-hyperlink-map
+  'help-echo (lambda (_ buffer pos)
+               (when-let ((url (get-text-property pos 'browse-url-data 
+                 (format "mouse-2, C-c RET: Open %s" url))))
+(defvar-local comint-osc-hyperlink--state nil)
+(defun comint-osc-hyperlink-handler (_ text)
+  "Create a hyperlink from an OSC 8 escape sequence.
+This function is intended to be included as an entry of
+  (when comint-osc-hyperlink--state
+    (let ((start (car comint-osc-hyperlink--state))
+          (url (cdr comint-osc-hyperlink--state)))
+      (make-text-button start (point)
+                        'type 'comint-osc-hyperlink
+                        'browse-url-data url)))
+  (setq comint-osc-hyperlink--state
+        (and (string-match ";\\(.+\\)" text)
+             (cons (point-marker) (match-string-no-properties 1 text)))))
 ;;; Converting process modes to use comint mode

reply via email to

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