dotfiles

Alpine Linux dotfiles

git clone git://git.lin.moe/dotfiles.git

 1;;; term-title.el --- Synchronize Emacs frame title with terminal title
 2
 3;; Copyright (C) 2021, 2022 Vladimir Panteleev
 4;; Copyright (C) 2004 Noah S. Friedman
 5
 6;; This program is free software; you can redistribute it and/or modify
 7;; it under the terms of the GNU General Public License as published by
 8;; 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 of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;; GNU General Public License for more details.
15;;
16;; You should have received a copy of the GNU General Public License
17;; along with this program; if not, you can either send email to this
18;; program's maintainer or write to: The Free Software Foundation,
19;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
20
21;;; Commentary:
22
23;; Loosely based on xterm-title by Noah S. Friedman.
24
25;; Please see the README.md file accompanying this file for
26;; documentation.
27
28;;; Code:
29
30(defcustom term-title-format nil
31  "Window title string to use for terminal windows.
32
33This variable takes precedence over the usual manner of setting
34frame titles in Emacs (see variables `frame-title-format' and
35`mode-line-format').  If nil, it is ignored."
36  :group 'terminals
37  :type '(choice
38          (string :tag "Literal text")
39          (sexp   :tag "Dynamic title (see variable `mode-line-format')")))
40
41
42;;;###autoload
43(define-minor-mode term-title-mode
44  "Synchronize terminal window titles with the selected Emacs tty frame.
45
46Note that if the mode is later disabled, or emacs is exited
47normally, the original title is not restored."
48  :global t
49  :group 'terminals
50  :init-value nil
51  (if term-title-mode
52      (progn
53        (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)))
59
60(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-format
66			    (frame-parameter nil 'title)
67			    ;; This would mimic the semantics in X but since
68			    ;; frame ttys always have a name (even if just
69			    ;; 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 e
77          (setq title (format-mode-line title-format))
78        (wrong-type-argument
79         (message "format-mode-line error in term-title: %S" e)))
80      (when title
81        (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))))))
84
85(defun term-title--set (title)
86  "Unconditionally set the current TTY terminal's title."
87
88  (send-string-to-terminal (format "\e]0;%s\a" title)))
89
90(provide 'term-title)
91
92;;; term-title.el ends here