-
Notifications
You must be signed in to change notification settings - Fork 8
/
sesman-browser.el
482 lines (431 loc) · 18.6 KB
/
sesman-browser.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
;;; sesman-browser.el --- Interactive Browser for Sesman -*- lexical-binding: t -*-
;;
;; Copyright (C) 2018, Vitalie Spinu
;; Author: Vitalie Spinu
;; URL: https://github.com/vspinu/sesman
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This file is *NOT* part of GNU Emacs.
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Interactive session browser.
;;
;;; Code:
(require 'seq)
(require 'sesman)
(defgroup sesman-browser nil
"Browser for Sesman."
:prefix "sesman-browser-"
:group 'sesman
:link '(url-link :tag "GitHub" "https://github.com/vspinu/sesman"))
(defface sesman-browser-highligh-face
'((default (:inherit highlight :weight bold)))
"Face used to highlight currently selected button."
:group 'sesman-browser)
(defface sesman-browser-button-face
'((default (:inherit button :slant italic)))
"Face used to highlight currently selected object."
:group 'sesman-browser)
(defvar-local sesman-browser--sort-types '(name relevance))
(defcustom sesman-browser-sort-type 'name
"Default sorting type in sesman browser buffers.
Currently can be either 'name or 'relevance."
:type '(choice (const name) (const relevance))
:group 'sesman-browser)
(defvar sesman-browser-map
(let (sesman-browser-map)
(define-prefix-command 'sesman-browser-map)
(define-key sesman-browser-map (kbd "r") #'sesman-browser-restart-session)
(define-key sesman-browser-map (kbd "q") #'sesman-browser-quit-session)
(define-key sesman-browser-map (kbd "b") #'sesman-browser-link-with-buffer)
(define-key sesman-browser-map (kbd "d") #'sesman-browser-link-with-directory)
(define-key sesman-browser-map (kbd "p") #'sesman-browser-link-with-project)
(define-key sesman-browser-map (kbd "u") #'sesman-browser-unlink)
sesman-browser-map)
"Prefix keymap for sesman commands from sesman browser.")
(defvar sesman-browser-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "n") #'sesman-browser-vertical-next)
(define-key map (kbd "p") #'sesman-browser-vertical-prev)
(define-key map (kbd "f") #'sesman-browser-forward)
(define-key map (kbd "b") #'sesman-browser-backward)
(define-key map [remap forward-paragraph] #'sesman-browser-session-next)
(define-key map [remap backward-paragraph] #'sesman-browser-session-prev)
(define-key map (kbd "C-M-n") #'sesman-browser-session-next)
(define-key map (kbd "C-M-p") #'sesman-browser-session-prev)
(define-key map (kbd "<tab>") #'sesman-browser-forward)
(define-key map (kbd "<backtab>") #'sesman-browser-backward)
(define-key map (kbd "<RET>") #'sesman-goto)
(define-key map (kbd "o") #'sesman-show)
(define-key map (kbd "t") #'sesman-browser-toggle-sort)
(define-key map (kbd "S") #'sesman-browser-toggle-sort)
(define-key map (kbd "l b") #'sesman-browser-link-with-buffer)
(define-key map (kbd "l d") #'sesman-browser-link-with-directory)
(define-key map (kbd "l p") #'sesman-browser-link-with-project)
(define-key map (kbd "u") #'sesman-browser-unlink)
(define-key map (kbd "s") 'sesman-browser-map)
(define-key map (kbd "C-c C-s") 'sesman-browser-map)
(easy-menu-define sesman-browser-mode-map map
"Sesman Browser"
'("SesmanBrowser"
["Next row" sesman-browser-vertical-next]
["Previous row" sesman-browser-vertical-prev]
["Next button" sesman-browser-forward]
["Previous button" sesman-browser-backward]
["Next session" sesman-browser-session-next]
["Previous session" sesman-browser-session-prev]
"--"
["Goto buffer" sesman-goto]
["Show buffer" sesman-show]
"--"
["Link with Buffer" sesman-browser-link-with-buffer]
["Link with Directory" sesman-browser-link-with-directory]
["Link with Project" sesman-browser-link-with-project]
["Unlink" sesman-browser-unlink]
"--"
["Toggle sort" sesman-browser-toggle-sort]
["Refresh View" revert-buffer]))
map)
"Local keymap in `sesman-browser-mode'.")
;;; Utilities
(defun sesman-browser--closeby-pos (prop lax)
(or (when (get-text-property (point) prop)
(point))
(when (and (not (bobp))
(get-text-property (1- (point)) prop))
(1- (point)))
(when lax
(let ((next (save-excursion
(and
(goto-char (next-single-char-property-change (point) prop))
(get-text-property (point) prop)
(point))))
(prev (save-excursion
(and
(goto-char (previous-single-char-property-change (point) prop))
(not (bobp))
(get-text-property (1- (point)) prop)
(1- (point))))))
(if next
(if prev
(if (< (- (point) prev) (- next (point)))
prev
next)
next)
prev)))))
(defun sesman-browser--closeby-value (prop lax)
(when-let ((pos (sesman-browser--closeby-pos prop lax)))
(get-text-property pos prop)))
(defun sesman-browser-get (what &optional no-error lax)
"Get value of the property WHAT at point.
If NO-ERROR is non-nil, don't throw an error if no value has been found and
return nil. If LAX is non-nil, search nearby and return the closest value."
(when (derived-mode-p 'sesman-browser-mode)
(or (let ((prop (pcase what
('session :sesman-session)
('link :sesman-link)
('object :sesman-object)
(_ what))))
(sesman-browser--closeby-value prop 'lax))
(unless no-error
(user-error "No %s %s" what (if lax "nearby" "at point"))))))
;;; Navigation
(defvar-local sesman-browser--section-overlay nil)
(defvar-local sesman-browser--stop-overlay nil)
(when (fboundp 'define-fringe-bitmap)
(define-fringe-bitmap 'sesman-left-bar
[#b00001100] nil nil '(top t)))
(defun sesman-browser--next (prop)
(let ((pos (point)))
(goto-char (previous-single-char-property-change (point) prop))
(unless (get-text-property (point) prop)
(goto-char (previous-single-char-property-change (point) prop)))
(when (bobp)
(goto-char pos))))
(defun sesman-browser--prev (prop)
(let ((pos (point)))
(goto-char (next-single-char-property-change (point) prop))
(unless (get-text-property (point) prop)
(goto-char (next-single-char-property-change (point) prop)))
(when (eobp)
(goto-char pos))))
(defun sesman-browser-forward ()
"Go to next button."
(interactive)
(sesman-browser--prev :sesman-stop))
(defun sesman-browser-backward ()
"Go to previous button."
(interactive)
(sesman-browser--next :sesman-stop))
(defun sesman-browser-vertical-next ()
"Go to next button section or row."
(interactive)
(sesman-browser--prev :sesman-vertical-stop))
(defun sesman-browser-vertical-prev ()
"Go to previous button section or row."
(interactive)
(sesman-browser--next :sesman-vertical-stop))
(defun sesman-browser-session-next ()
"Go to next session."
(interactive)
(sesman-browser--prev :sesman-session-stop))
(defun sesman-browser-session-prev ()
"Go to previous session."
(interactive)
(sesman-browser--next :sesman-session-stop))
;;; Display
(defun sesman-goto (&optional no-switch)
"Go to most relevant buffer for session at point.
If NO-SWITCH is non-nil, only display the buffer."
(interactive "P")
(let ((object (get-text-property (point) :sesman-object)))
(if (and object (bufferp object))
(if no-switch
(display-buffer object)
(pop-to-buffer object))
(let* ((session (sesman-browser-get 'session))
(info (sesman-session-info (sesman--system) session))
(buffers (or (plist-get info :buffers)
(let ((objects (plist-get info :objects)))
(seq-filter #'bufferp objects)))))
(if buffers
(let ((most-recent-buf (seq-find (lambda (b)
(member b buffers))
(buffer-list))))
(if no-switch
(display-buffer most-recent-buf)
(pop-to-buffer most-recent-buf)))
(user-error "Cannot jump to session %s; it doesn't contain any buffers" (car session)))))))
(defun sesman-show ()
"Show the most relevant buffer for the session at point."
(interactive)
(sesman-goto 'no-switch))
(defun sesman-browser--sensor-function (&rest _ignore)
(let ((beg (or (when (get-text-property (point) :sesman-stop)
(if (get-text-property (1- (point)) :sesman-stop)
(previous-single-char-property-change (point) :sesman-stop)
(point)))
(next-single-char-property-change (point) :sesman-stop)))
(end (next-single-char-property-change (point) :sesman-stop)))
(move-overlay sesman-browser--stop-overlay beg end)
(when window-system
(let ((beg (get-text-property (point) :sesman-fragment-beg))
(end (get-text-property (point) :sesman-fragment-end)))
(when (and beg end)
(move-overlay sesman-browser--section-overlay beg end))))))
;;; Sesman UI
(defun sesman-browser-quit-session ()
"Quite session at point."
(interactive)
(sesman-quit (sesman-browser-get 'session)))
(defun sesman-browser-restart-session ()
"Restart session at point."
(interactive)
(sesman-restart (sesman-browser-get 'session)))
(defun sesman-browser-link-with-buffer ()
"Ask for buffer to link session at point to."
(interactive)
(let ((session (sesman-browser-get 'session)))
(sesman-link-with-buffer 'ask session)))
(defun sesman-browser-link-with-directory ()
"Ask for directory to link session at point to."
(interactive)
(let ((session (sesman-browser-get 'session)))
(sesman-link-with-directory 'ask session)))
(defun sesman-browser-link-with-project ()
"Ask for project to link session at point to."
(interactive)
(let ((session (sesman-browser-get 'session)))
(sesman-link-with-project 'ask session)))
(defun sesman-browser-unlink ()
"Unlink the link at point or ask for link to unlink."
(interactive)
(if-let ((link (sesman-browser-get 'link 'no-error)))
(sesman--unlink link)
(if-let ((links (sesman-links (sesman--system)
(sesman-browser-get 'session))))
(mapc #'sesman--unlink
(sesman--ask-for-link "Unlink: " links 'ask-all))
(user-error "No links for session %s" (car (sesman-browser-get 'session)))))
(run-hooks 'sesman-post-command-hook))
;;; Major Mode
(defun sesman-browser-revert (&rest _ignore)
"Refresh current browser buffer."
(let ((pos (point)))
(sesman-browser)
;; simple but not particularly reliable or useful
(goto-char (min pos (point-max)))))
(defun sesman-browser-revert-all (system)
"Refresh all Sesman SYSTEM browsers."
(mapc (lambda (b)
(with-current-buffer b
(when (and (derived-mode-p 'sesman-browser-mode)
(eq system (sesman--system)))
(sesman-browser-revert))))
(buffer-list)))
(defun sesman-browser--goto-stop (stop-value)
(let ((search t))
(goto-char (point-min))
(while search
(goto-char (next-single-char-property-change (point) :sesman-stop))
(if (eobp)
(progn (setq search nil)
(goto-char (next-single-char-property-change (point-min) :sesman-stop)))
(when (equal (get-text-property (point) :sesman-stop) stop-value)
(setq search nil))))))
(defun sesman-browser-toggle-sort ()
"Toggle sorting of sessions.
See `sesman-browser-sort-type' for the default sorting type."
(interactive)
(when (eq sesman-browser-sort-type
(car sesman-browser--sort-types))
(pop sesman-browser--sort-types))
(unless sesman-browser--sort-types
(setq-local sesman-browser--sort-types (default-value 'sesman-browser--sort-types)))
(setq sesman-browser-sort-type (pop sesman-browser--sort-types))
(let ((stop (sesman-browser-get :sesman-stop nil 'lax)))
(sesman-browser)
(sesman-browser--goto-stop stop)
(sesman-browser--sensor-function))
(message "Sorted by %s"
(propertize (symbol-name sesman-browser-sort-type) 'face 'bold)))
(define-derived-mode sesman-browser-mode special-mode "SesmanBrowser"
"Interactive view of Sesman sessions.
When applicable, system specific commands are locally bound to j when point is
on a session object."
;; ensure there is a sesman-system here
(sesman--system)
(delete-all-overlays)
(setq-local sesman-browser--stop-overlay (make-overlay (point) (point)))
(overlay-put sesman-browser--stop-overlay 'face 'sesman-browser-highligh-face)
(setq-local sesman-browser--section-overlay (make-overlay (point) (point)))
(when window-system
(let* ((fringe-spec '(left-fringe sesman-left-bar sesman-browser-highligh-face))
(dummy-string (propertize "|" 'display fringe-spec)))
(overlay-put sesman-browser--section-overlay 'line-prefix dummy-string)))
(add-hook 'sesman-post-command-hook 'sesman-browser-revert nil t)
(setq-local display-buffer-base-action '(nil . ((inhibit-same-window . t))))
(setq-local sesman-browser--sort-types (default-value 'sesman-browser--sort-types))
(setq-local revert-buffer-function #'sesman-browser-revert))
(defun sesman-browser--insert-session (system ses i)
(let ((ses-name (car ses))
(head-template "%17s")
beg end)
(setq beg (point))
;; session header
(insert (format "%3d: " i))
(insert (propertize (car ses)
:sesman-stop ses-name
:sesman-vertical-stop t
:sesman-session-stop t
'face 'bold
'cursor-sensor-functions (list #'sesman-browser--sensor-function)
'mouse-face 'highlight)
"\n")
;; links
(insert (format head-template "linked-to: "))
(let ((link-groups (sesman-grouped-links system ses))
(vert-stop))
(dolist (grp link-groups)
(let* ((type (car grp)))
(dolist (link (cdr grp))
(when (> (current-column) fill-column)
(insert "\n" (format head-template " "))
(setq vert-stop nil))
(let ((val (sesman--abbrev-path-maybe (sesman--lnk-value link))))
(insert (propertize (sesman--format-context type val 'sesman-browser-button-face)
:sesman-stop (car link)
:sesman-vertical-stop (unless vert-stop (setq vert-stop t))
:sesman-link link
'cursor-sensor-functions (list #'sesman-browser--sensor-function)
'mouse-face 'highlight)))
(insert " ")))))
(insert "\n")
;; objects
(insert (format head-template "objects: "))
(let* ((info (sesman-session-info system ses))
(map (plist-get info :map))
(objects (plist-get info :objects))
(strings (or (plist-get info :strings)
(mapcar (lambda (x) (format "%s" x)) objects)))
(kvals (seq-mapn #'cons objects strings))
(kvals (seq-sort (lambda (a b) (string-lessp (cdr a) (cdr b)))
kvals))
(vert-stop))
(dolist (kv kvals)
(when (> (current-column) fill-column)
(insert "\n" (format head-template " "))
(setq vert-stop nil))
(let ((str (replace-regexp-in-string ses-name "%s" (cdr kv) nil t)))
(insert (propertize str
:sesman-stop str
:sesman-vertical-stop (unless vert-stop (setq vert-stop t))
:sesman-object (car kv)
'cursor-sensor-functions (list #'sesman-browser--sensor-function)
'face 'sesman-browser-button-face
'mouse-face 'highlight
'help-echo "mouse-2: visit in other window"
'keymap map)
" "))))
;; session properties
(setq end (point))
(put-text-property beg end :sesman-session ses)
(put-text-property beg end :sesman-session-name ses-name)
(put-text-property beg end :sesman-fragment-beg beg)
(put-text-property beg end :sesman-fragment-end end)
(insert "\n\n")))
;;;###autoload
(defun sesman-browser ()
"Display an interactive session browser.
See `sesman-browser-mode' for more details."
(interactive)
(let* ((system (sesman--system))
(pop-to (called-interactively-p 'any))
(sessions (sesman-sessions system))
(cur-session (when pop-to
(sesman-current-session 'CIDER)))
(buff (get-buffer-create (format "*sesman %s browser*" system))))
(with-current-buffer buff
(setq-local sesman-system system)
(sesman-browser-mode)
(cursor-sensor-mode 1)
(let ((inhibit-read-only t)
(sessions (pcase sesman-browser-sort-type
('name (seq-sort (lambda (a b) (string-greaterp (car b) (car a)))
sessions))
('relevance (sesman--sort-sessions system sessions))
(_ (error "Invalid `sesman-browser-sort-type'"))))
(i 0))
(erase-buffer)
(insert "\n ")
(insert (propertize (format "%s Sessions:" system)
'face '(bold font-lock-keyword-face)))
(insert "\n\n")
(dolist (ses sessions)
(setq i (1+ i))
(sesman-browser--insert-session system ses i))
(when pop-to
(pop-to-buffer buff)
(sesman-browser--goto-stop (car cur-session)))
(sesman-browser--sensor-function)))))
(provide 'sesman-browser)
;;; sesman-browser.el ends here