-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrigpa-window-mode.el
373 lines (324 loc) · 13.1 KB
/
rigpa-window-mode.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
;;; rigpa-window-mode.el --- Self-reflective editing modes -*- lexical-binding: t -*-
;; URL: https://github.com/countvajhula/rigpa
;; This program is "part of the world," in the sense described at
;; http://drym.org. From your perspective, this is no different than
;; MIT or BSD or other such "liberal" licenses that you may be
;; familiar with, that is to say, you are free to do whatever you like
;; with this program. It is much more than BSD or MIT, however, in
;; that it isn't a license at all but an idea about the world and how
;; economic systems could be set up so that everyone wins. Learn more
;; at drym.org.
;;
;; This work transcends traditional legal and economic systems, but
;; for the purposes of any such systems within which you may need to
;; operate:
;;
;; This is free and unencumbered software released into the public domain.
;; The authors relinquish any copyright claims on this work.
;;
;;; Commentary:
;;
;; A mode to refer to windows
;;
;;; Code:
(require 'evil)
(require 'hydra)
(require 'ace-window)
(require 'transpose-frame)
(require 'winner)
(require 'windmove)
(require 'chimera)
(require 'chimera-hydra)
(require 'dash)
(evil-define-state window
"Window state."
:tag " <W> "
:message "-- WINDOW --"
:enable (normal))
;; configure home-row hotkeys to index windows in ace-window,
;; used as "search" feature in window mode
(setq aw-keys '(?h ?j ?k ?l ?g ?f ?d ?s ?a))
;; enable winner mode, used to provide "undo/redo" functionality
;; in window mode
(winner-mode t)
;; enable fitting window to buffer width
(setq fit-window-to-buffer-horizontally t)
(defun rigpa-window-mru ()
"Jump to most recent window, or other window if there is only one other.
TODO: This doesn't work with more than 2 windows that are all the same buffer."
(interactive)
(let ((num-windows (length (window-list))))
(if (= num-windows 2)
(other-window 1)
(evil-window-mru))))
(defun rigpa-window-quit-other ()
"Quit other window without changing focus."
(interactive)
(let ((original-window (selected-window)))
(other-window 1)
(quit-window)
(select-window original-window)))
(defun rigpa-window--opposite-direction (direction)
"The opposite direction."
(cond ((eq 'left direction) 'right)
((eq 'right direction) 'left)
((eq 'up direction) 'down)
((eq 'down direction) 'up)))
(defun rigpa-window--find (direction)
"Find window in DIRECTION."
(let ((original-window (selected-window))
(next-window (windmove-find-other-window direction)))
(cond ((save-window-excursion
(rigpa-window-mru)
(let ((window (windmove-find-other-window
(rigpa-window--opposite-direction direction))))
(and window
(eq original-window window))))
(save-window-excursion (rigpa-window-mru)
(selected-window)))
((and next-window
(not (window-minibuffer-p next-window)))
next-window)
(t nil))))
(defun rigpa-window--go (direction)
"Select window in DIRECTION."
(let ((window (rigpa-window--find direction)))
(when window
(select-window window))))
(defun rigpa-window-left ()
"Select window on the left."
(interactive)
(rigpa-window--go 'left))
(defun rigpa-window-right ()
"Select window on the right."
(interactive)
(rigpa-window--go 'right))
(defun rigpa-window-up ()
"Select window above."
(interactive)
(rigpa-window--go 'up))
(defun rigpa-window-down ()
"Select window below."
(interactive)
(rigpa-window--go 'down))
(defun rigpa-window--move-buffer (direction)
"Move buffer in current window in DIRECTION.
If both buffers are the same, then just preserve the position in the
buffer from the source context."
(let ((buffer (current-buffer))
(original-position (point))
(next-window (rigpa-window--find direction)))
(when next-window
(switch-to-buffer (other-buffer))
(select-window next-window)
(unless (eq buffer (current-buffer))
(switch-to-buffer buffer))
(goto-char original-position)
(recenter))))
(defun rigpa-window-move-buffer-left ()
"Move buffer in current window to the window on the left."
(interactive)
(rigpa-window--move-buffer 'left))
(defun rigpa-window-move-buffer-right ()
"Move buffer in current window to the window on the right."
(interactive)
(rigpa-window--move-buffer 'right))
(defun rigpa-window-move-buffer-up ()
"Move buffer in current window to the window above."
(interactive)
(rigpa-window--move-buffer 'up))
(defun rigpa-window-move-buffer-down ()
"Move buffer in current window to the window below."
(interactive)
(rigpa-window--move-buffer 'down))
(defun rigpa-window--exchange-buffer (direction)
"Exchange buffer with the window on the DIRECTION side.
If both windows contain the same buffer, simply exchange cursor
positions."
(interactive)
(let ((buffer (current-buffer))
(original-position (point))
(original-window (selected-window))
(next-window (rigpa-window--find direction)))
(when next-window
(select-window next-window)
(let ((other-position (point))
(other-buffer (current-buffer)))
(unless (eq buffer other-buffer)
(switch-to-buffer buffer))
(goto-char original-position)
(recenter)
(select-window original-window)
(unless (eq buffer other-buffer)
(switch-to-buffer other-buffer))
(goto-char other-position)
(recenter)
(select-window next-window)))))
(defun rigpa-window-exchange-buffer-left ()
"Exchange buffer with the one on the left."
(interactive)
(rigpa-window--exchange-buffer 'left))
(defun rigpa-window-exchange-buffer-right ()
"Exchange buffer with the one on the right."
(interactive)
(rigpa-window--exchange-buffer 'right))
(defun rigpa-window-exchange-buffer-up ()
"Exchange buffer with the one on the up."
(interactive)
(rigpa-window--exchange-buffer 'up))
(defun rigpa-window-exchange-buffer-down ()
"Exchange buffer with the one on the down."
(interactive)
(rigpa-window--exchange-buffer 'down))
(defun rigpa-window-setup-marks-table ()
"Initialize the buffer marks hashtable and add an entry for the
current ('original') buffer."
(interactive)
(defvar rigpa-window-marks-hash
(make-hash-table :test 'equal))
(rigpa-window-save-original))
(defun rigpa-window-save-original ()
"Save current buffer as original buffer."
(interactive)
(rigpa-window-set-mark ?0))
(defun rigpa-window-original-configuration ()
"Get original window configuration."
(interactive)
(cdr (rigpa-window-get-mark ?0)))
(defun rigpa-window-original-window ()
"Get original selected window."
(interactive)
(car (rigpa-window-get-mark ?0)))
(defun rigpa-window-set-mark (mark-name)
"Set a mark"
(interactive "cMark name?")
(puthash mark-name
(cons (selected-window)
(winner-configuration))
rigpa-window-marks-hash)
(message "Mark '%c' set." mark-name))
(defun rigpa-window-get-mark (mark-name)
"Retrieve a mark"
(gethash mark-name rigpa-window-marks-hash))
(defun rigpa-window-return-to-mark (mark-name)
"Return to mark"
(interactive "cMark name?")
(winner-set (cdr (rigpa-window-get-mark mark-name))))
(defun rigpa-window-return-to-original-configuration ()
"Return to the window configuration we were in at the time of entering
window mode."
(interactive)
(winner-set (rigpa-window-original-configuration)))
(defun rigpa-window-return-to-original-window ()
"Return to the window we were in at the time of entering
window mode."
(interactive)
(let ((window (rigpa-window-original-window)))
;; TODO: it seems that when a window (e.g. REPL) is closed
;; it shows Normal mode in the other (only remaining) window
;; but it doesn't allow you to move, suggesting the hydra is
;; still invisibly active.
(when (window-live-p window)
(select-window window))))
(defun rigpa-window-flash-to-original ()
"Go momentarily to original window and return.
This 'flash' allows the original window, rather than the previous one
encountered while navigating to the present one, to be treated as the
last window for 'flashback' ('Alt-tab') purposes. The flash should
happen quickly enough not to be noticeable."
(interactive)
;; TODO: in some cases when a REPL window (maybe large > 2500 lines) is open
;; this doesn't seem to restore the target window after the "excursion"
(unless (eq (selected-window) (rigpa-window-original-window))
(let ((inhibit-redisplay t)) ;; not sure if this is doing anything but FWIW
(save-window-excursion
(rigpa-window-return-to-original-window)))))
(defhydra hydra-window (:columns 4
:body-pre (progn (rigpa-window-setup-marks-table)
(chimera-hydra-signal-entry chimera-window-mode)
;; TODO: this should happen via proper entry hook.
;; Also, if it is already active, then make a note
;; of it and don't disable it upon exit
(auto-dim-other-buffers-mode 1))
:post (progn (chimera-hydra-portend-exit chimera-window-mode t))
:after-exit (chimera-hydra-signal-exit chimera-window-mode
#'chimera-handle-hydra-exit))
"Window mode"
("h" rigpa-window-left "left")
("j" rigpa-window-down "down")
("k" rigpa-window-up "up")
("l" rigpa-window-right "right")
("H" rigpa-window-move-buffer-left "move buffer left")
("J" rigpa-window-move-buffer-down "move buffer down")
("K" rigpa-window-move-buffer-up "move buffer up")
("L" rigpa-window-move-buffer-right "move buffer right")
("C-S-h" rigpa-window-exchange-buffer-left "exchange buffer left")
("C-S-j" rigpa-window-exchange-buffer-down "exchange buffer down")
("C-S-k" rigpa-window-exchange-buffer-up "exchange buffer up")
("C-S-l" rigpa-window-exchange-buffer-right "exchange buffer left")
("M-H" evil-window-move-far-left "move to far left")
("M-J" evil-window-move-very-bottom "move to bottom")
("M-K" evil-window-move-very-top "move to top")
("M-L" evil-window-move-far-right "move to far right")
("x" evil-window-delete "delete")
("d" evil-window-delete :exit t)
("X" transpose-frame "transpose") ; there are more in transpose-frame that may be useful
("Q" rigpa-window-quit-other "quit other window" :exit t)
("o" rigpa-window-mru "Jump to most recent (like Alt-Tab)" :exit t)
("s-w" rigpa-window-mru "Jump to most recent (like Alt-Tab)" :exit t)
("n" other-window "next")
("w" delete-other-windows "maximize" :exit t)
("s" evil-window-split "split horizontally")
("_" evil-window-split "")
("v" evil-window-vsplit "split vertically")
("|" evil-window-vsplit "")
("u" winner-undo "undo")
("C-r" winner-redo "redo")
("m" rigpa-window-set-mark "set mark")
("'" rigpa-window-return-to-mark "return to mark" :exit t)
("`" rigpa-window-return-to-mark "return to mark" :exit t)
("q" rigpa-window-return-to-original-configuration "return to original" :exit t)
("/" ace-window "search")
("+" evil-window-increase-height "expand vertically")
("-" evil-window-decrease-height "shrink vertically")
("C-+" (lambda ()
(interactive)
(evil-window-increase-height 3)) "expand vertically more")
("C--" (lambda ()
(interactive)
(evil-window-decrease-height 3)) "shrink vertically more")
(">" evil-window-increase-width "expand horizontally")
("<" evil-window-decrease-width "shrink horizontally")
("C->" (lambda ()
(interactive)
(evil-window-increase-width 5)) "expand horizontally more")
("C-<" (lambda ()
(interactive)
(evil-window-decrease-width 5)) "shrink horizontally more")
("<backspace>" balance-windows "balance")
("<tab>" fit-window-to-buffer "fit to width")
("=" fit-window-to-buffer "fit to width")
("r" evil-window-rotate-downwards "rotate downwards")
("R" evil-window-rotate-upwards "rotate upwards")
("f" ffap-other-window "go to file in other window" :exit t)
("i" nil "exit" :exit t)
("H-m" rigpa-toggle-menu "show/hide this menu")
("<return>" rigpa-enter-lower-level "enter lower level" :exit t)
("<escape>" rigpa-enter-higher-level "escape to higher level" :exit t))
(defun rigpa--on-window-mode-post-exit ()
"Actions to take upon exit from window mode."
(rigpa-window-flash-to-original)
(auto-dim-other-buffers-mode -1))
(defvar chimera-window-mode-entry-hook nil
"Entry hook for rigpa window mode.")
(defvar chimera-window-mode-exit-hook nil
"Exit hook for rigpa window mode.")
(defvar chimera-window-mode
(make-chimera-mode :name "window"
:enter #'hydra-window/body
:pre-entry-hook 'chimera-window-mode-entry-hook
:post-exit-hook 'chimera-window-mode-exit-hook
:entry-hook 'evil-window-state-entry-hook
:exit-hook 'evil-window-state-exit-hook))
(provide 'rigpa-window-mode)
;;; rigpa-window-mode.el ends here