Subject: [PATCH] Extend comint-osc-handlers to support OSC 2

From: Matthias Meulien <[email-masked]>
Date: Fri, 16 Sep 2022 19:59:26 +0200

Tags: patch

The attached patch adds support for OSC 2 escape sequences in comint buffers.

To test one can update `frame-title-format' with something like:

(setq frame-title-format
      '("%b"
	(:eval
	 (when (and
	      comint-osc-window-title
	      (not (string-empty-p comint-osc-window-title)))
	     (concat " - " comint-osc-window-title)))
	(:eval
	 (if multiple-frames
	     ""
	   (concat " - GNU Emacs at " system-name)))))

After M-x shell call a command (like flatpak-builder) that emits OSC 2
commands, the frame title is updated while the command runs.

Note that one is expected to extend Bash prompt command to print an OSC
2 command to clear the title:

set -x

osc_clear() {
  printf "\e]2;\e\\"
}

PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }osc_clear

set +x

Regards,

In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, cairo version
 1.16.0) of 2022-09-14 built on carbon
Repository revision: f0798ac13dcb4c01a883f165e03c3cd7f208667c
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure --with-native-compilation --with-x-toolkit=no'

>From 9ae3c83873169765721395966205f8cd10dcc333 Mon Sep 17 00:00:00 2001
From: Matthias Meulien <[email-masked]>
Date: Thu, 15 Sep 2022 14:30:07 +0200
Subject: [PATCH] Extend comint-osc-handlers to support OSC 2

* lisp/comint.el (comint-osc-handlers): Add new
comint-osc-window-title-handler.
(comint-osc-window-title): Local variable to store window title.
(comint-osc-window-title-handler): Set comint-osc-window-title
according to OSC 2 command.
---
 lisp/comint.el | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lisp/comint.el b/lisp/comint.el
index 696dac3d12..9445482f1b 100644
--- a/lisp/comint.el
+++ b/lisp/comint.el
@@ -3914,7 +3914,8 @@ comint-redirect-results-list-from-process
 ;; to `comint-osc-handlers' allows a customized treatment of further
 ;; sequences.
 
-(defvar-local comint-osc-handlers '(("7" . comint-osc-directory-tracker)
+(defvar-local comint-osc-handlers '(("2" . comint-osc-window-title-handler)
+                                    ("7" . comint-osc-directory-tracker)
                                     ("8" . comint-osc-hyperlink-handler))
   "Alist of handlers for OSC escape sequences.
 See `comint-osc-process-output' for details.")
@@ -3955,6 +3956,18 @@ comint-osc-process-output
             (put-text-property pos0 bound 'invisible t)
             (setq comint-osc--marker (copy-marker pos0))))))))
 
+;; Window title handling (OSC 2)
+
+(defvar-local comint-osc-window-title nil)
+(defun comint-osc-window-title-handler (_ text)
+  "Set value of `comint-osc-window-title' from an OSC 2 escape sequence.
+The variable `comint-osc-window-title' can be reffered to in
+`frame-title-format' to dynamically set the frame title.
+
+This function is intended to be included as an entry of
+`comint-osc-handlers'."
+  (setq comint-osc-window-title text))
+
 ;; Current directory tracking (OSC 7)
 
 (declare-function url-host "url/url-parse.el")
-- 
2.30.2