-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
zoom-window.el
375 lines (320 loc) · 14.8 KB
/
zoom-window.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
;;; zoom-window.el --- Zoom window like tmux -*- lexical-binding: t; -*-
;; Copyright (C) 2017 by Syohei YOSHIDA
;; Copyright (C) 2020-2024 Shen, Jen-Chieh
;; Author: Syohei YOSHIDA <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/syohex/emacs-zoom-window
;; Version: 0.06
;; Package-Requires: ((emacs "24.3"))
;; 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; zoom-window.el provides functions which zooms specific window in frame and
;; restore original window configuration. This is like tmux's zoom/unzoom
;; features.
;;; Code:
(require 'cl-lib)
;; for byte-compile warnings
(declare-function elscreen-get-screen-property "elscreen")
(declare-function elscreen-get-current-screen "elscreen")
(declare-function elscreen-set-screen-property "elscreen")
(declare-function elscreen-get-conf-list "elscreen")
(declare-function safe-persp-name "persp-mode")
(declare-function get-frame-persp "persp-mode")
(defgroup zoom-window nil
"zoom window like tmux"
:group 'windows)
(defcustom zoom-window-use-elscreen nil
"Non-nil means using elscreen."
:type 'boolean
:group 'zoom-window)
(defcustom zoom-window-use-persp nil
"Non-nil means using persp-mode."
:type 'boolean
:group 'zoom-window)
(defvar zoom-window-persp-alist nil
"Association list for working with persp-mode.")
(defcustom zoom-window-mode-line-color "green"
"Color of mode-line when zoom-window is enabled."
:type 'string
:group 'zoom-window)
(defvar zoom-window--window-configuration (make-hash-table :test #'equal))
(defvar zoom-window--orig-color nil)
(defun zoom-window--put-alist (key value alist)
"Not documented."
(let ((elm (assoc key alist)))
(if elm
(progn
(setcdr elm value)
alist)
(cons (cons key value) alist))))
(defun zoom-window--persp-rename-hook (_perspecitve oldName newName)
"Meant to be used in the `persp-renamed-functions' hook. Updates
the state of perspectives in `zoom-window' from OLDNAME to NEWNAME."
;; Update `zoom-window-persp-alist'
(let ((oldPersp (assoc oldName zoom-window-persp-alist)))
(when oldPersp (setcar oldPersp newName)))
;; Update `zoom-window--window-configuration'
(let* ((oldKey (zoom-window--configuration-key oldName))
(configuration (gethash oldKey zoom-window--window-configuration)))
(when configuration
(progn (puthash (zoom-window--configuration-key newName)
configuration zoom-window--window-configuration)
(remhash oldKey zoom-window--window-configuration)))))
(defsubst zoom-window--elscreen-current-property ()
"Not documented."
(elscreen-get-screen-property (elscreen-get-current-screen)))
(defsubst zoom-window--elscreen-current-tab-property (prop)
"Not documented."
(let ((property (zoom-window--elscreen-current-property)))
(assoc-default prop property)))
(defun zoom-window--elscreen-update ()
"Not documented."
(let* ((property (zoom-window--elscreen-current-property))
(orig-background (assoc-default 'zoom-window-saved-color property))
(is-zoomed (assoc-default 'zoom-window-is-zoomed property))
(curframe (window-frame nil)))
(if is-zoomed
(set-face-background 'mode-line zoom-window-mode-line-color curframe)
(when orig-background
(set-face-background 'mode-line orig-background curframe)))
(force-mode-line-update)))
(defun zoom-window--elscreen-set-zoomed ()
"Not documented."
(let* ((current-screen (elscreen-get-current-screen))
(prop (elscreen-get-screen-property current-screen))
(orig-mode-line (face-background 'mode-line)))
(setq prop (zoom-window--put-alist 'zoom-window-saved-color orig-mode-line prop))
(elscreen-set-screen-property current-screen prop)))
(defun zoom-window--elscreen-set-default ()
"Not documented."
(let* ((history (elscreen-get-conf-list 'screen-history))
(current-screen (car (last history)))
(prop (elscreen-get-screen-property current-screen)))
(setq prop (zoom-window--put-alist 'zoom-window-is-zoomed nil prop))
(setq prop (zoom-window--put-alist 'zoom-window-saved-color zoom-window--orig-color prop))
(elscreen-set-screen-property current-screen prop)))
(defun zoom-window--persp-before-switch-hook (persp-name _frame-or-window)
"Hook to run when do persp switching.
PERSP-NAME: name of a perspective to switch.
FRAME-OR-WINDOW: a frame or a window for which the switching takes place."
(let* ((property (assoc-default persp-name zoom-window-persp-alist))
(orig-background (assoc-default 'zoom-window-saved-color property))
(is-zoomed (assoc-default 'zoom-window-is-zoomed property))
(curframe (window-frame nil)))
(if is-zoomed
(set-face-background 'mode-line zoom-window-mode-line-color curframe)
(if orig-background
(set-face-background 'mode-line orig-background curframe)
(set-face-background 'mode-line zoom-window--orig-color curframe)))
(if (and is-zoomed
orig-background)
(force-mode-line-update))))
(defun zoom-window--persp-before-kill-hook (persp)
"Hook to run when do persp killing.
PERSP: the perspective to be killed."
(let ((persp-name (safe-persp-name persp)))
(setq zoom-window-persp-alist
(delq (assoc persp-name zoom-window-persp-alist)
zoom-window-persp-alist))))
;;;###autoload
(defun zoom-window-setup ()
"To work with elscreen or persp-mode."
(cond
;; to work with elscreen
(zoom-window-use-elscreen
(setq zoom-window--orig-color (face-background 'mode-line))
(add-hook 'elscreen-create-hook #'zoom-window--elscreen-set-default)
(add-hook 'elscreen-screen-update-hook #'zoom-window--elscreen-update)
;; for first tab
(zoom-window--elscreen-set-default))
;; to work with persp
(zoom-window-use-persp
(setq zoom-window--orig-color (face-background 'mode-line))
(add-hook 'persp-before-switch-functions
#'zoom-window--persp-before-switch-hook)
(add-hook 'persp-renamed-functions #'zoom-window--persp-rename-hook)
(add-hook 'persp-before-kill-functions #'zoom-window--persp-before-kill-hook))
;; do nothing else
(t nil)))
(defun zoom-window--init-persp-property (persp-name)
"Initialize property of PERSP-NAME in `zoom-window-persp-alist'."
(let ((property '(('zoom-window-is-zoomed nil)
('zoom-window-saved-color zoom-window--orig-color))))
(setq zoom-window-persp-alist
(zoom-window--put-alist persp-name property zoom-window-persp-alist))
property))
(defun zoom-window--save-mode-line-color ()
"Not documented."
(cond (zoom-window-use-elscreen
(zoom-window--elscreen-set-zoomed))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (or (assoc-default
persp-name zoom-window-persp-alist)
(zoom-window--init-persp-property persp-name))))
(setq property (zoom-window--put-alist
'zoom-window-saved-color
(face-background 'mode-line)
property))
(setq zoom-window-persp-alist (zoom-window--put-alist
persp-name
property
zoom-window-persp-alist))))
(t (setq zoom-window--orig-color (face-background 'mode-line)))))
(defun zoom-window--save-buffers ()
"Not documented."
(let ((buffers (cl-loop for window in (window-list)
collect (window-buffer window))))
(cond (zoom-window-use-elscreen
(let* ((curprops (zoom-window--elscreen-current-property))
(props (zoom-window--put-alist 'zoom-window-buffers buffers curprops)))
(elscreen-set-screen-property (elscreen-get-current-screen) props)))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (or (assoc-default persp-name zoom-window-persp-alist)
(zoom-window--init-persp-property persp-name))))
(setq property (zoom-window--put-alist
'zoom-window-buffers buffers property))
(setq zoom-window-persp-alist (zoom-window--put-alist
persp-name property zoom-window-persp-alist))))
(t
(set-frame-parameter
(window-frame nil) 'zoom-window-buffers buffers)))))
(defun zoom-window--get-buffers ()
(cond (zoom-window-use-elscreen
(let ((props (zoom-window--elscreen-current-property)))
(assoc-default 'zoom-window-buffers props)))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (assoc-default persp-name zoom-window-persp-alist)))
(assoc-default 'zoom-window-buffers property)))
(t
(frame-parameter (window-frame nil) 'zoom-window-buffers))))
(defun zoom-window--restore-mode-line-face ()
"Not documented."
(let ((color
(cond (zoom-window-use-elscreen
(zoom-window--elscreen-current-tab-property
'zoom-window-saved-color))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (assoc-default persp-name
zoom-window-persp-alist)))
(assoc-default 'zoom-window-saved-color property)))
(t zoom-window--orig-color))))
(set-face-background 'mode-line color (window-frame nil))))
(defun zoom-window--configuration-key (&optional name)
"Not documented."
(cond (zoom-window-use-elscreen
(format "zoom-window-%d" (or name (elscreen-get-current-screen))))
(zoom-window-use-persp
(let ((persp-name (or name (safe-persp-name (get-frame-persp)))))
(format "perspective-%s" persp-name)))
(t (let ((parent-id (or name (frame-parameter (window-frame nil) 'parent-id))))
(if (not parent-id)
:zoom-window ;; not support multiple frame
(format ":zoom-window-%d" parent-id))))))
(defun zoom-window--save-window-configuration ()
"Not documented."
(let ((key (zoom-window--configuration-key))
(window-conf (list (current-window-configuration) (point-marker))))
(puthash key window-conf zoom-window--window-configuration)))
(defun zoom-window--restore-window-configuration ()
"Not documented."
(let* ((key (zoom-window--configuration-key))
(window-context (gethash key zoom-window--window-configuration 'not-found)))
(when (eq window-context 'not-found)
(error "window configuration is not found"))
(let ((window-conf (cl-first window-context))
(marker (cl-second window-context)))
(set-window-configuration window-conf)
(when (marker-buffer marker)
(goto-char marker))
(remhash key zoom-window--window-configuration))))
(defun zoom-window--toggle-enabled ()
"Not documented."
(cond
(zoom-window-use-elscreen
(let* ((current-screen (elscreen-get-current-screen))
(prop (elscreen-get-screen-property current-screen))
(val (assoc-default 'zoom-window-is-zoomed prop)))
(setq prop (zoom-window--put-alist 'zoom-window-is-zoomed (not val) prop))
(elscreen-set-screen-property current-screen prop)))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (or (assoc-default persp-name
zoom-window-persp-alist)
(zoom-window--init-persp-property persp-name)))
(value (assoc-default 'zoom-window-is-zoomed property)))
(setq property (zoom-window--put-alist
'zoom-window-is-zoomed (not value) property))
(setq zoom-window-persp-alist
(zoom-window--put-alist persp-name
property
zoom-window-persp-alist))))
(t (let* ((curframe (window-frame nil))
(status (frame-parameter curframe 'zoom-window-enabled)))
(set-frame-parameter curframe 'zoom-window-enabled (not status))))))
(defun zoom-window--enable-p ()
"Not documented."
(cond
(zoom-window-use-elscreen
(zoom-window--elscreen-current-tab-property 'zoom-window-is-zoomed))
(zoom-window-use-persp
(let* ((persp-name (safe-persp-name (get-frame-persp)))
(property (assoc-default persp-name zoom-window-persp-alist)))
(and property (assoc-default 'zoom-window-is-zoomed property))))
(t (frame-parameter (window-frame nil) 'zoom-window-enabled))))
(defsubst zoom-window--goto-line (line)
"Not documented."
(goto-char (point-min))
(forward-line (1- line)))
(defun zoom-window--do-unzoom ()
"Not documented."
(let ((current-line (line-number-at-pos))
(current-column (current-column))
(current-buf (current-buffer)))
(zoom-window--restore-mode-line-face)
(zoom-window--restore-window-configuration)
(pop-to-buffer current-buf)
(zoom-window--goto-line current-line)
(move-to-column current-column)))
;;;###autoload
(defun zoom-window-zoom ()
"Zoom the current window."
(interactive)
(let ((enabled (zoom-window--enable-p))
(curframe (window-frame nil)))
(if (and (one-window-p) (not enabled))
(message "There is only one window!!")
(if enabled
(with-demoted-errors "Warning: %S"
(zoom-window--do-unzoom))
(zoom-window--save-mode-line-color)
(zoom-window--save-buffers)
(zoom-window--save-window-configuration)
(delete-other-windows)
(set-face-background 'mode-line zoom-window-mode-line-color curframe))
(force-mode-line-update)
(zoom-window--toggle-enabled))))
(defun zoom-window-next ()
"Switch to next buffer which is in zoomed frame/screen/perspective."
(interactive)
(let* ((buffers (zoom-window--get-buffers))
(targets (member (current-buffer) buffers)))
(if targets
(if (cdr targets)
(switch-to-buffer (cadr targets))
(switch-to-buffer (car buffers)))
(switch-to-buffer (car buffers)))))
(provide 'zoom-window)
;;; zoom-window.el ends here