-
Notifications
You must be signed in to change notification settings - Fork 1
/
xterm-title.el
155 lines (129 loc) · 5.94 KB
/
xterm-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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;;; xterm-title.el --- Update xterm titles
;; Copyright (C) 2004 Noah S. Friedman
;; Author: Noah Friedman <[email protected]>
;; Maintainer: [email protected]
;; Created: 2004-06-21
;; $Id: xterm-title.el,v 1.1 2004/06/22 00:19:06 friedman Exp $
;; 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:
;; To use this, put something like the following in your .emacs:
;; (when (and (not window-system)
;; (string-match "^xterm" (getenv "TERM")))
;; (require 'xterm-title)
;; (xterm-title-mode 1))
;; This package requires the function `format-mode-line', which does not
;; appear in Emacs 21.3 or earlier versions. In fact, as of 2004-06-21
;; only the Emacs development sources in CVS have this function.
;; Updates of xterm-title.el may be retrieved via
;; http://www.splode.com/~friedman/software/emacs-lisp/
;;; Code:
(require 'xterm-frobs)
(defgroup xterm nil
"Emacs interaction with xterm windows"
:group 'xterm
:group 'extensions)
(defcustom xterm-title-frame-title-format nil
"Window title string to use for xterm 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 'xterm
:type '(choice
(string :tag "Literal text")
(sexp :tag "Dynamic title (see variable `mode-line-format')")))
(defcustom xterm-title-icon-title-format nil
"Window icon label to use for xterm windows.
This variable takes precedence over the usual manner of setting frame icon
titles in Emacs \(see variables `icon-title-format' and
`mode-line-format'\). If nil, it is ignored."
:group 'xterm
:type '(choice
(string :tag "Literal text")
(sexp :tag "Dynamic icon title (see variable `mode-line-format')")))
;; Not user variables; just used to maintain state
(defvar xterm-title-update-last-window nil)
(defvar xterm-title-orig-frame-title nil)
(defvar xterm-title-orig-icon-title nil)
(define-minor-mode xterm-title-mode
"Update xterm window and icon titles with the selected Emacs tty frame.
When this mode is enabled, the original state of the titles are saved.
If the mode is later disabled, or emacs is exited normally, these original
titles will be restored."
:global t
:group 'xterm
:lighter " XTitle"
:init-value nil
(cond (xterm-title-mode
(xterm-title-save-orig-titles)
(add-hook 'post-command-hook 'xterm-title-update)
(add-hook 'kill-emacs-hook 'xterm-title-restore-orig-titles))
(t
(remove-hook 'kill-emacs-hook 'xterm-title-restore-orig-titles)
(remove-hook 'post-command-hook 'xterm-title-update)
(xterm-title-restore-orig-titles))))
(defun xterm-title-update ()
"Update xterm window and icon titles with the selected Emacs tty frame."
(unless (and xterm-title-mode
(eq (selected-window) xterm-title-update-last-window))
(xterm-set-window-title
(format-mode-line (or xterm-title-frame-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)))
(xterm-set-icon-title
(format-mode-line (or xterm-title-icon-title-format
(frame-parameter nil 'icon-name)
(frame-parameter nil 'title)
;; See above.
;;
;;(frame-parameter nil 'name)
;; Iconified frames won't be running commands,
;; so they are never updated. Instead, just use
;; the icon-title-format unconditionally if we
;; get this far.
;;
;; Also, calling xterm-report-window-state
;; can interfere with xterm-mouse-mode.
;;
;;(if (eq (xterm-report-window-state) 'iconified)
;; icon-title-format
;; (or xterm-title-frame-title-format
;; frame-title-format)))))
icon-title-format)))
(setq xterm-update-last-window (selected-window))))
(defun xterm-title-save-orig-titles ()
(condition-case nil
(progn
(setq xterm-title-orig-frame-title (xterm-report-window-title)
xterm-title-orig-icon-title (xterm-report-icon-title)))
(error
(setq xterm-title-orig-frame-title nil
xterm-title-orig-icon-title nil))))
(defun xterm-title-restore-orig-titles ()
(unwind-protect
(progn
(when xterm-title-orig-frame-title
(xterm-set-window-title xterm-title-orig-frame-title))
(when xterm-title-orig-icon-title
(xterm-set-icon-title xterm-title-orig-icon-title)))
(setq xterm-title-orig-frame-title nil
xterm-title-orig-icon-title nil)))
(provide 'xterm-title)
;;; xterm-title.el ends here