From 3a09343eabaa751e7d40f0a21af5c63427d9a850 Mon Sep 17 00:00:00 2001 From: Mark Oteiza Date: Wed, 20 Sep 2017 11:39:37 -0400 Subject: [PATCH] Teach Emacs to set XTerm window titles * lisp/term/xterm.el (terminal-init-xterm): Add initialization. (xterm--init-frame-title, xterm-set-window-title-flag): (xterm-unset-window-title-flag, xterm-set-window-title): New functions. (xterm-window-title-flag): New variable. (xterm-set-window-title): New custom variable. * etc/NEWS: Mention it. --- etc/NEWS | 5 +++++ lisp/term/xterm.el | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 371cdf686ca..0e62a2bbb42 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -37,6 +37,11 @@ When you add a new item, use the appropriate mark if you are sure it applies, * Changes in Specialized Modes and Packages in Emacs 27.1 +** Enhanced xterm support + +*** New variable 'xterm-set-window-title' controls whether Emacs +sets the XTerm window title. The default is to set the window title. + * New Modes and Packages in Emacs 27.1 diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el index 4f79703833d..6a17d382b0a 100644 --- a/lisp/term/xterm.el +++ b/lisp/term/xterm.el @@ -68,6 +68,11 @@ string bytes that can be copied is 3/4 of this value." :version "25.1" :type 'integer) +(defcustom xterm-set-window-title t + "Whether Emacs should set window titles to an Emacs frame in an XTerm." + :version "27.1" + :type 'boolean) + (defconst xterm-paste-ending-sequence "\e[201~" "Characters send by the terminal to end a bracketed paste.") @@ -802,6 +807,8 @@ We run the first FUNCTION whose STRING matches the input events." (when (memq 'setSelection xterm-extra-capabilities) (xterm--init-activate-set-selection))) + (when xterm-set-window-title + (xterm--init-frame-title)) ;; Unconditionally enable bracketed paste mode: terminals that don't ;; support it just ignore the sequence. (xterm--init-bracketed-paste-mode) @@ -828,6 +835,34 @@ We run the first FUNCTION whose STRING matches the input events." "Terminal initialization for `gui-set-selection'." (set-terminal-parameter nil 'xterm--set-selection t)) +(defun xterm--init-frame-title () + "Terminal initialization for XTerm frame titles." + (xterm-set-window-title) + (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 'minibuffer-exit-hook 'xterm-set-window-title)) + +(defvar xterm-window-title-flag nil + "Whether a new frame has been created, calling for a title update.") + +(defun xterm-set-window-title-flag (_frame) + "Set `xterm-window-title-flag'. +See `xterm--init-frame-title'" + (setq xterm-window-title-flag t)) + +(defun xterm-unset-window-title-flag () + (when xterm-window-title-flag + (setq xterm-window-title-flag nil) + (xterm-set-window-title))) + +(defun xterm-set-window-title (&optional terminal) + "Set the window title of the Xterm TERMINAL. +The title is constructed from `frame-title-format'." + (send-string-to-terminal + (format "\e]2;%s\a" (format-mode-line frame-title-format)) + terminal)) + (defun xterm--selection-char (type) (pcase type ('PRIMARY "p") -- 2.39.2