1;;; term-title.el --- Synchronize Emacs frame title with terminal title23;; Copyright (C) 2021, 2022 Vladimir Panteleev4;; Copyright (C) 2004 Noah S. Friedman56;; This program is free software; you can redistribute it and/or modify7;; it under the terms of the GNU General Public License as published by8;; the Free Software Foundation; either version 2, or (at your option)9;; any later version.10;;11;; This program is distributed in the hope that it will be useful,12;; but WITHOUT ANY WARRANTY; without even the implied warranty of13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14;; GNU General Public License for more details.15;;16;; You should have received a copy of the GNU General Public License17;; along with this program; if not, you can either send email to this18;; program's maintainer or write to: The Free Software Foundation,19;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.2021;;; Commentary:2223;; Loosely based on xterm-title by Noah S. Friedman.2425;; Please see the README.md file accompanying this file for26;; documentation.2728;;; Code:2930(defcustom term-title-format nil31 "Window title string to use for terminal windows.3233This variable takes precedence over the usual manner of setting34frame titles in Emacs (see variables `frame-title-format' and35`mode-line-format'). If nil, it is ignored."36 :group 'terminals37 :type '(choice38 (string :tag "Literal text")39 (sexp :tag "Dynamic title (see variable `mode-line-format')")))404142;;;###autoload43(define-minor-mode term-title-mode44 "Synchronize terminal window titles with the selected Emacs tty frame.4546Note that if the mode is later disabled, or emacs is exited47normally, the original title is not restored."48 :global t49 :group 'terminals50 :init-value nil51 (if term-title-mode52 (progn53 (add-hook 'post-command-hook 'term-title--update)54 (add-hook 'window-state-change-hook 'term-title--update)55 (add-hook 'tty-setup-hook 'term-title--update))56 (remove-hook 'post-command-hook 'term-title--update)57 (remove-hook 'window-state-change-hook 'term-title--update)58 (remove-hook 'tty-setup-hook 'term-title--update)))5960(defun term-title--update ()61 "Synchronize terminal window title with the selected Emacs frame, if it is a tty."62 (when (and (frame-live-p (selected-frame))63 (eq (framep-on-display) t))64 (let ((frame (selected-frame))65 (title-format (or term-title-format66 (frame-parameter nil 'title)67 ;; This would mimic the semantics in X but since68 ;; frame ttys always have a name (even if just69 ;; the default "F<n>"), don't bother.70 ;;71 ;;(frame-parameter nil 'name)72 frame-title-format))73 title)74 ;; format-mode-line sometimes, for whatever reason, fails with e.g.75 ;; (wrong-type-argument frame-live-p #<dead frame F23 0x55cbd62f93c0>)76 (condition-case e77 (setq title (format-mode-line title-format))78 (wrong-type-argument79 (message "format-mode-line error in term-title: %S" e)))80 (when title81 (unless (string-equal title (frame-parameter frame 'term-title-last-title))82 (term-title--set title)83 (set-frame-parameter frame 'term-title-last-title title))))))8485(defun term-title--set (title)86 "Unconditionally set the current TTY terminal's title."8788 (send-string-to-terminal (format "\e]0;%s\a" title)))8990(provide 'term-title)9192;;; term-title.el ends here