-
Notifications
You must be signed in to change notification settings - Fork 0
/
term-title.el
92 lines (76 loc) · 3.37 KB
/
term-title.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;; term-title.el --- Synchronize Emacs frame title with terminal title
;; Copyright (C) 2021, 2022 Vladimir Panteleev
;; Copyright (C) 2004 Noah S. Friedman
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Loosely based on xterm-title by Noah S. Friedman.
;; Please see the README.md file accompanying this file for
;; documentation.
;;; Code:
(defcustom term-title-format nil
"Window title string to use for terminal windows.
This variable takes precedence over the usual manner of setting
frame titles in Emacs (see variables `frame-title-format' and
`mode-line-format'). If nil, it is ignored."
:group 'terminals
:type '(choice
(string :tag "Literal text")
(sexp :tag "Dynamic title (see variable `mode-line-format')")))
;;;###autoload
(define-minor-mode term-title-mode
"Synchronize terminal window titles with the selected Emacs tty frame.
Note that if the mode is later disabled, or emacs is exited
normally, the original title is not restored."
:global t
:group 'terminals
:init-value nil
(if term-title-mode
(progn
(add-hook 'post-command-hook 'term-title--update)
(add-hook 'window-state-change-hook 'term-title--update)
(add-hook 'tty-setup-hook 'term-title--update))
(remove-hook 'post-command-hook 'term-title--update)
(remove-hook 'window-state-change-hook 'term-title--update)
(remove-hook 'tty-setup-hook 'term-title--update)))
(defun term-title--update ()
"Synchronize terminal window title with the selected Emacs frame, if it is a tty."
(when (and (frame-live-p (selected-frame))
(eq (framep-on-display) t))
(let ((frame (selected-frame))
(title-format (or term-title-format
(frame-parameter nil 'title)
;; This would mimic the semantics in X but since
;; frame ttys always have a name (even if just
;; the default "F<n>"), don't bother.
;;
;;(frame-parameter nil 'name)
frame-title-format))
title)
;; format-mode-line sometimes, for whatever reason, fails with e.g.
;; (wrong-type-argument frame-live-p #<dead frame F23 0x55cbd62f93c0>)
(condition-case e
(setq title (format-mode-line title-format))
(wrong-type-argument
(message "format-mode-line error in term-title: %S" e)))
(when title
(unless (string-equal title (frame-parameter frame 'term-title-last-title))
(term-title--set title)
(set-frame-parameter frame 'term-title-last-title title))))))
(defun term-title--set (title)
"Unconditionally set the current TTY terminal's title."
(send-string-to-terminal (format "\e]0;%s\a" title)))
(provide 'term-title)
;;; term-title.el ends here