-
Notifications
You must be signed in to change notification settings - Fork 13
/
lentic-mode.el
368 lines (313 loc) · 10.5 KB
/
lentic-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
;;; lentic-mode.el --- minor mode for lentic buffers -*- lexical-binding: t -*-
;;; Header:
;; This file is not part of Emacs
;; Author: Phillip Lord <phillip.lord@russet.org.uk>
;; Maintainer: Phillip Lord <phillip.lord@russet.org.uk>
;; The contents of this file are subject to the GPL License, Version 3.0.
;; Copyright (C) 2014-2024 Free Software Foundation, Inc.
;; 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:
;; A minor mode for creating and manipulated lentic buffers.
;;; Code:
;; ** Preliminaries
;; #+begin_src emacs-lisp
(require 'lentic)
(require 'lentic-doc)
;; #+end_src
;; ** Utility
;; #+begin_src emacs-lisp
(defun lentic-mode-lentic-list (buffer)
"Return a list of all lentics for BUFFER.
Lentics are listed in an undefined order."
(lentic-mode--lentic-list-1 buffer nil))
(defun lentic-mode--lentic-list-1 (buffer _seen-buffer)
(let ((buffers))
(lentic-each
buffer
(lambda (b)
(push b buffers)))
buffers))
(defun lentic-mode-buffer-list (buffer &optional frame)
"Returns a list of all lentics for BUFFER.
Lentics are listed in the same order as in fundamental
`buffer-list'. or the frame local if FRAME is specified."
(let ((lentic-list
(lentic-mode-lentic-list buffer)))
(-filter
(lambda (b)
(-contains? lentic-list b))
(buffer-list frame))))
(defun lentic-mode-find-next-lentic-buffer (buffer &optional frame)
(car
(--drop-while
(eq buffer it)
(lentic-mode-buffer-list
buffer (or frame (selected-frame))))))
(defun lentic-mode-find-next-visible-lentic-buffer (buffer &optional frame)
(car
(--drop-while
(or (eq buffer it)
(not (get-buffer-window it frame)))
(lentic-mode-buffer-list
buffer (or frame (selected-frame))))))
(defun lentic-mode-find-next-non-visible-lentic-buffer (buffer &optional frame)
(car
(--drop-while
(or (eq buffer it)
(get-buffer-window it frame))
(lentic-mode-buffer-list
buffer (or frame (selected-frame))))))
;; #+end_src
;; ** Window and Buffer Functions
;; #+begin_src emacs-lisp
(defun lentic-mode-show-buffer-in-window (before-buffer new-buffer)
(let* ((buffer-window (get-buffer-window before-buffer))
(before-window-start
(window-start buffer-window))
(before-window-point
(m-buffer-at-point before-buffer)))
(set-window-buffer
buffer-window
new-buffer)
(set-window-start
buffer-window
before-window-start)
(goto-char before-window-point)
(bury-buffer before-buffer)))
;;;###autoload
(defun lentic-mode-create-from-init (&optional force)
(interactive "P")
(lentic-garbage-collect-config)
(if (and lentic-config (not force))
(message "Already initialized. C-u to force.")
(let ((before (length lentic-config))
(all (lentic-init-all-create)))
(message "Created %s buffers"
(- (length all)
before)))))
;;;###autoload
(defun lentic-mode-next-lentic-buffer ()
"Move the lentic buffer into the current window, creating if necessary."
(interactive)
(lentic-mode-show-buffer-in-window
(current-buffer)
(lentic-mode-find-next-lentic-buffer (current-buffer))))
;;;###autoload
(defun lentic-mode-split-window-below ()
"Move lentic buffer to the window below, creating if needed."
(interactive)
(-when-let
(next
(lentic-mode-find-next-non-visible-lentic-buffer
(current-buffer)))
(set-window-buffer
(split-window-below)
next)
next))
;;;###autoload
(defun lentic-mode-split-window-right ()
"Move lentic buffer to the window right, creating if needed."
(interactive)
(-when-let
(next
(lentic-mode-find-next-non-visible-lentic-buffer
(current-buffer)))
(set-window-buffer
(split-window-right)
next)
next))
;;;###autoload
(defun lentic-mode-show-all-lentic ()
(interactive)
(delete-other-windows)
(while
(lentic-mode-split-window-below))
(balance-windows))
(defun lentic-mode-swap-buffer-windows (a b)
"Swaps the window that two buffers are displayed in.
A and B are the buffers."
(let ((window-a (get-buffer-window a))
(window-b (get-buffer-window b)))
(when window-a
(set-window-buffer
window-a b))
(when window-b
(set-window-buffer
window-b a))))
;;;###autoload
(defun lentic-mode-move-lentic-window ()
"Move the next lentic buffer into the current window.
If the lentic is currently being displayed in another window,
then the current-buffer will be moved into that window. See also
`lentic-mode-swap-buffer-windows' and `lentic-mode-next-buffer'."
(interactive)
(let ((before-window-start
(window-start (get-buffer-window)))
(before-window-point
(point)))
(lentic-mode-swap-buffer-windows
(current-buffer)
(or
(lentic-mode-find-next-visible-lentic-buffer
(current-buffer))
(lentic-mode-find-next-lentic-buffer
(current-buffer))))
(set-window-start
(selected-window)
before-window-start)
(goto-char before-window-point)))
;;;###autoload
(defun lentic-mode-swap-lentic-window ()
"Swap the window of the buffer and lentic.
If both are current displayed, swap the windows they
are displayed in, which keeping current buffer.
See also `lentic-mode-move-lentic-window'."
(interactive)
(let ((next
(lentic-mode-find-next-visible-lentic-buffer
(current-buffer))))
(if (not next)
(message "Cannot swap windows when only one is visible")
(lentic-mode-swap-buffer-windows
(current-buffer)
next)
(when (window-live-p
(get-buffer-window
(current-buffer)))
(select-window
(get-buffer-window
(current-buffer)))))))
(defun lentic-mode-create-new-view ()
(let* ((conf (lentic-default-init))
(_ (oset conf
:sync-point nil))
(that (lentic-create conf)))
(setq lentic-config
(cons conf lentic-config))
that))
;;;###autoload
(defun lentic-mode-create-new-view-in-selected-window ()
(interactive)
(set-window-buffer
(selected-window)
(lentic-mode-create-new-view)))
(defun lentic-mode-force-clone-1 ()
(lentic-when-lentic
(let ((inhibit-modification-hooks t))
(lentic-after-change-function
(point-min) (point-max)
(- (point-max) (point-min))))))
(defun lentic-mode-force-clone ()
(interactive)
(when (yes-or-no-p "Force Clone of the current buffer? ")
(lentic-mode-force-clone-1)))
;; #+end_src
;; ** Minor Mode
;; #+begin_src emacs-lisp
;;;###autoload
(defun lentic-mode-toggle-auto-sync-point ()
(interactive)
(lentic-when-lentic
(oset lentic-config sync-point
(not (oref lentic-config sync-point)))))
(defvar lentic-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c ,c") #'lentic-mode-create-from-init)
(define-key map (kbd "C-c ,v")
#'lentic-mode-create-new-view-in-selected-window)
(define-key map (kbd "C-c ,n") #'lentic-mode-next-lentic-buffer)
(define-key map (kbd "C-c ,s") #'lentic-mode-swap-lentic-window)
(define-key map (kbd "C-c ,h") #'lentic-mode-move-lentic-window)
(define-key map (kbd "C-c ,b") #'lentic-mode-split-window-below)
(define-key map (kbd "C-c ,t") #'lentic-mode-split-window-right)
(define-key map (kbd "C-c ,f") #'lentic-mode-insert-file-local)
(define-key map (kbd "C-c ,a") #'lentic-mode-show-all-lentic)
map)
"Keymap for lentic-minor-mode")
(defcustom lentic-mode-line-lighter "Lentic"
"Default mode lighter for lentic"
:group 'lentic
:type 'string)
(defvar-local lentic-mode-line (format " %s[]" lentic-mode-line-lighter))
(defun lentic-mode-update-mode-line ()
(setq lentic-mode-line
(format " %s[%s]"
lentic-mode-line-lighter
(mapconcat #'lentic-mode-line-string
lentic-config
","))))
(defun lentic-mode-update-all-display ()
(if lentic-emergency
(setq lentic-mode-line
(format " %s[Emergency]" lentic-mode-line-lighter))
(dolist (b (buffer-list))
(lentic-when-with-current-buffer b
(lentic-mode-update-mode-line)))
(force-mode-line-update t)))
;; ** lentic self-doc
;; #+begin_src: emacs-lisp
;;;###autoload
(defun lentic-mode-doc-eww-view ()
(interactive)
(lentic-doc-eww-view 'lentic))
;;;###autoload
(defun lentic-mode-doc-external-view ()
(interactive)
(lentic-doc-external-view 'lentic))
;;;###autoload
(define-minor-mode lentic-mode
"Documentation"
:lighter lentic-mode-line)
;; FIXME: Cannot autoload this before `lentic.el' is loaded since otherwise
;; we get (void-variable lentic-config) errors in redisplay.
;; ;;;###autoload
(easy-menu-change
'("Edit")
"Lentic"
'(["Create All" lentic-mode-create-from-init
:active (not lentic-config)]
["Create View" lentic-mode-create-new-view-in-selected-window]
["Next" lentic-mode-next-lentic-buffer
:active lentic-config]
["Split Below" lentic-mode-split-window-below
:active lentic-config]
["Split Right" lentic-mode-split-window-right
:active lentic-config]
["Show All" lentic-mode-show-all-lentic
:active lentic-config]
["Swap" lentic-mode-swap-lentic-window
:active lentic-config]
["Force Clone" lentic-mode-force-clone
:active lentic-config]
["Insert File Local" lentic-mode-insert-file-local]
["Read Doc (eww)" lentic-mode-doc-eww-view]
["Read Doc (external)" lentic-mode-doc-external-view]
))
;;;###autoload
(defun lentic-mode-insert-file-local (init-function)
(interactive
(list (completing-read
"Lentic init function: "
(mapcar
#'symbol-name
lentic-init-functions)
nil 'confirm)))
(add-file-local-variable 'lentic-init (intern init-function)))
;;;###autoload
(define-globalized-minor-mode global-lentic-mode
lentic-mode
lentic-mode-on)
(defun lentic-mode-on ()
(lentic-mode 1))
;; #+end_src
(provide 'lentic-mode)
;;; lentic-mode.el ends here