Subject: bug#28591: 27.0.50; xterm-set-window-title

From: Mark Oteiza <[email-masked]>
Date: Sat, 30 Sep 2017 15:26:27 -0400

On 29/09/17 at 08:34pm, Eli Zaretskii wrote:
> > Date: Fri, 29 Sep 2017 09:05:42 -0400
> > From: Mark Oteiza <[email-masked]>
> > Cc: [email-masked], [email-masked]
> > 
> > Correction: deleting a frame does not restore the window title as
> > Katsumi pointed out.
> 
> So you are saying the title is only restored when Emacs exits?
> Otherwise, I think I'm missing something: in what case(s) your
> restoration code does work?

There is no restoration code at the moment--I totally overlooked it
because of my shell configuration.

The following works for me on a VTE based terminal.
The commented bits I would expect to do _something_ on XTerm, but
I have been unable to get titles to restore on XTerm, whether or not
I set the allowWindowOps and disallowedWindowOps resources.

diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index 6a17d382b0..337c75e0ea 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -708,6 +708,16 @@ xterm--version-handler
           ;;(xterm--init-activate-get-selection)
           (xterm--init-activate-set-selection))))))
 
+(defun xterm--title-handler ()
+  (let ((str "")
+        chr)
+    (while (and (setq chr (read-event nil nil 2))
+                (not (eq chr ?\\)))
+      (setq str (concat str (string chr))))
+    (push (replace-regexp-in-string "\e$" "" str)
+          (terminal-parameter (frame-terminal (selected-frame))
+                              'xterm-saved-title))))
+
 (defvar xterm-query-timeout 2
   "Seconds to wait for an answer from the terminal.
 Can be nil to mean \"no timeout\".")
@@ -837,12 +847,25 @@ xterm--init-activate-set-selection
 
 (defun xterm--init-frame-title ()
   "Terminal initialization for XTerm frame titles."
+  ;; (xterm-push-title-stack)
+  (xterm-push-window-title (frame-terminal (selected-frame)))
   (xterm-set-window-title)
+  (add-hook 'suspend-tty-functions 'xterm-pop-window-title)
+  (add-hook 'resume-tty-functions 'xterm-push-window-title)
+  ;; (add-hook 'delete-frame-functions 'xterm-pop-title-stack)
   (add-hook 'after-make-frame-functions 'xterm-set-window-title-flag)
   (add-hook 'window-configuration-change-hook 'xterm-unset-window-title-flag)
-  (add-hook 'post-command-hook 'xterm-set-window-title)
+  (add-hook 'delete-terminal-functions 'xterm-pop-window-title)
+  ;; (add-hook 'delete-terminal-functions 'xterm-pop-title-stack)
+  (add-hook 'buffer-list-update-hook 'xterm-set-window-title)
   (add-hook 'minibuffer-exit-hook 'xterm-set-window-title))
 
+;; (defun xterm-push-title-stack ()
+;;   (send-string-to-terminal "\e[22;0t"))
+
+;; (defun xterm-pop-title-stack (&optional terminal)
+;;   (send-string-to-terminal "\e[23;0t" terminal))
+
 (defvar xterm-window-title-flag nil
   "Whether a new frame has been created, calling for a title update.")
 
@@ -863,6 +886,15 @@ xterm-set-window-title
    (format "\e]2;%s\a" (format-mode-line frame-title-format))
    terminal))
 
+(defun xterm-pop-window-title (terminal)
+  (send-string-to-terminal
+   (format "\e]2;%s\a" (pop (terminal-parameter terminal 'xterm-saved-title)))
+   terminal))
+
+(defun xterm-push-window-title (terminal)
+  (xterm--query "\e[21;0t" '(("\e]l" . xterm--title-handler)))
+  (xterm-set-window-title terminal))
+
 (defun xterm--selection-char (type)
   (pcase type
     ('PRIMARY "p")