/
side-hustle.el
383 lines (308 loc) · 13 KB
/
side-hustle.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
;;; side-hustle.el --- Hustle through Imenu in a side window -*- lexical-binding: t; -*-
;; Copyright (c) 2021-2024 Paul W. Rankin
;; Author: Paul W. Rankin <hello@paulwrankin.com>
;; Keywords: convenience
;; Version: 0.3.0
;; Package-Requires: ((emacs "24.4") (seq "2.20"))
;; URL: https://github.com/rnkn/side-hustle
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Side Hustle
;; ===========
;; Hustle through a buffer's Imenu in a side window in GNU Emacs.
;; Side Hustle spawns a side window linked to the current buffer, which allows
;; working with multiple buffers simultaneously.
;; Installation
;; ------------
;; The latest stable release of Side Hustle is available via [MELPA-stable][1].
;; First, add MELPA-stable to your package archives:
;; M-x customize-option RET package-archives RET
;; Insert an entry named melpa-stable with URL:
;; https://stable.melpa.org/packages/
;; You can then find the latest stable version of side-hustle in the list
;; returned by:
;; M-x list-packages RET
;; If you prefer the latest but perhaps unstable version, do the above using
;; [MELPA][2].
;; Then add a key binding to your init file:
;; (define-key (current-global-map) (kbd "M-s l") #'side-hustle-toggle)
;; Bugs and Feature Requests
;; -------------------------
;; Send me an email (address in the package header). For bugs, please
;; ensure you can reproduce with:
;; $ emacs -Q -l side-hustle.el
;; Known issues are tracked with FIXME comments in the source.
;; Alternatives
;; ------------
;; Side Hustle takes inspiration primarily from
;; [imenu-list](https://github.com/bmag/imenu-list).
;; [1]: https://stable.melpa.org/#/side-hustle
;; [2]: https://melpa.org/#/side-hustle
;;; Code:
(require 'imenu)
(defgroup side-hustle nil
"Navigate Imenu in a side window."
:group 'convenience
:group 'matching
:link '(info-link "(emacs) Imenu"))
;;; Internal Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar-local side-hustle--source-buffer nil)
(defvar-local side-hustle--hidden nil)
;;; User Options ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defcustom side-hustle-display-alist
'((side . left)
(slot . 0)
(window-width . 40))
"Alist used to display side-hustle buffer."
:type 'alist
:group 'side-hustle
:link '(info-link "(elisp) Buffer Display Action Alists"))
(defcustom side-hustle-select-window t
"When non-nil, select the menu window after creating it."
:type 'boolean
:safe 'booleanp
:group 'side-hustle)
(defcustom side-hustle-persistent-window nil
"When non-nil, make the side-window persistent.
This requires either calling `quit-window' or
`side-hustle-toggle' to quit the side-window."
:type 'boolean
:safe 'booleanp
:group 'side-hustle)
(defcustom side-hustle-evaporate-window nil
"When non-nil, quit the side window when following link."
:type 'boolean
:safe 'booleanp
:group 'side-hustle)
(defcustom side-hustle-item-char ?\*
"Character to use to itemize `imenu' items."
:type '(choice (const nil) character)
:group 'side-hustle)
(defcustom side-hustle-indent-width 4
"Indent width in columns for sublevels of `imenu' items."
:type 'integer
:safe 'integerp
:group 'side-hustle)
;;; Faces ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defface side-hustle
'((t (:underline nil :inherit button)))
"Default face for side-window items."
:group 'side-hustle)
;; (defface side-hustle-highlight
;; '((t (:extend t :inherit (secondary-selection))))
;; "Default face for highlighted items."
;; :group 'side-hustle)
;;; Internal Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun side-hustle-button-ensure ()
"Ensure point is at button."
(or (button-at (point))
(and (eolp) (forward-button -1 nil nil t))
(forward-button 1 nil nil t)))
(defun side-hustle-imenu-item (button save-window)
"Pop up buffer containing `imenu' item for BUTTON.
Handle buffer according to SAVE-WINDOW or value of
`side-hustle-evaporate-window'."
(let ((buffer (current-buffer))
(imenu-item (button-get button 'hustle-item)))
(when imenu-item
(pop-to-buffer side-hustle--source-buffer)
(imenu imenu-item)
(recenter-top-bottom)
(cond (save-window
(select-window (get-buffer-window buffer (selected-frame))))
(side-hustle-evaporate-window
(quit-window nil (get-buffer-window buffer (selected-frame))))))))
(defun side-hustle-switch-hide-state (label)
"Toggle inclusion of LABEL in `side-hustle--hidden'."
(setq side-hustle--hidden
(if (member label side-hustle--hidden)
(remove label side-hustle--hidden)
(cons label side-hustle--hidden))))
(defun side-hustle-show-hide (start end)
"Toggle invisibility of items between START and END."
(with-silent-modifications
(if (eq (get-text-property (button-end (button-at (point))) 'invisible)
'hustle-invisible)
(put-text-property start end 'invisible nil)
(put-text-property start end 'invisible 'hustle-invisible))))
(defun side-hustle-button-action (button &optional hide save-window)
"Call appropriate button action for BUTTON.
When HIDE is non-nil, always hide child items. Pass SAVE-WINDOW
to `side-hustle-imenu-item'."
(let ((level (button-get button 'hustle-level))
(label (button-label button))
(start (button-end button))
(end (button-end button)))
(save-excursion
(while (and (forward-button 1 nil nil t)
(< level (button-get (button-at (point)) 'hustle-level)))
(setq end (button-end (button-at (point))))))
(cond ((= start end)
(side-hustle-imenu-item button save-window))
(hide
(side-hustle-show-hide start end))
(t
(side-hustle-show-hide start end)
(side-hustle-switch-hide-state label)))))
(defun side-hustle-insert (item level)
"Insert ITEM at indentation level LEVEL.
And `imenu' marker as button property."
(insert (make-string level ?\t))
(when (characterp side-hustle-item-char)
(insert side-hustle-item-char "\s"))
(insert-text-button (car item)
'hustle-item item
'hustle-level level
'face 'side-hustle
'action #'side-hustle-button-action
'help-echo "mouse-1, RET: go to this item; SPC: show this item"
'follow-link t)
(insert ?\n))
(defun side-hustle-insert-items (imenu-items level)
"For each item in IMENU-ITEMS, insert appropriately.
Either call `side-hustle-insert' at LEVEL, or if item is an
alist, insert alist string and increment LEVEL before calling
recursively with `cdr'."
(mapc
(lambda (item)
(if (imenu--subalist-p item)
(progn
(side-hustle-insert item level)
(side-hustle-insert-items (cdr item) (1+ level)))
(side-hustle-insert item level)))
imenu-items))
(defun side-hustle-refresh ()
"Rebuild and insert `imenu' entries for source buffer."
(interactive)
(with-silent-modifications
(let ((x (point))
imenu-items)
(when (buffer-live-p side-hustle--source-buffer)
(setq imenu-items
(with-current-buffer side-hustle--source-buffer
(setq imenu--index-alist nil)
(imenu--make-index-alist t)
imenu--index-alist)))
(erase-buffer)
(setq header-line-format (buffer-name side-hustle--source-buffer))
(setq tab-width side-hustle-indent-width)
(when imenu-items (side-hustle-insert-items imenu-items 0))
(goto-char (point-min))
(let (button)
(while (setq button (forward-button 1 nil nil t))
(when (member (button-label button) side-hustle--hidden)
(side-hustle-button-action button t))))
(goto-char x))))
(defun side-hustle-find-existing (sourcebuf)
"Return existing `side-hustle' buffer for SOURCEBUF or nil if none."
(seq-find
(lambda (buf)
(with-current-buffer buf
(eq side-hustle--source-buffer sourcebuf)))
(buffer-list)))
(defun side-hustle-get-buffer-create (sourcebuf)
"Get or create `side-hustle' buffer for SOURCEBUF."
(or (side-hustle-find-existing sourcebuf)
(let ((new-buf (get-buffer-create
(concat "Side-Hustle: " (buffer-name sourcebuf)))))
(with-current-buffer new-buf
(side-hustle-mode)
(setq side-hustle--source-buffer sourcebuf)
(setq side-hustle--hidden nil))
new-buf)))
;; (defun side-hustle-highlight-current ()
;; "Highlight the current `imenu' item in `side-hustle'.
;; Added to `window-configuration-change-hook'."
;; (unless (or (minibuffer-window-active-p (selected-window))
;; (eq major-mode 'side-hustle-mode))
;; (let ((x (point))
;; (buf (side-hustle-find-existing (current-buffer)))
;; candidate diff)
;; (when (and buf (window-live-p (get-buffer-window buf)))
;; (with-current-buffer buf
;; (with-silent-modifications
;; (remove-text-properties (point-min) (point-max) '(face))
;; (goto-char (point-min))
;; (while (< (point) (point-max))
;; (let ((marker (get-text-property (point) 'side-hustle-imenu-marker)))
;; (when (and (markerp marker)
;; (<= (marker-position marker) x)
;; (or (null diff) (< (- x (marker-position marker)) diff)))
;; (setq candidate (point)
;; diff (if diff
;; (min diff (- x (marker-position marker)))
;; (- x (marker-position marker))))))
;; (forward-line 1))
;; (when candidate
;; (goto-char candidate)
;; (put-text-property (line-beginning-position 1)
;; (line-beginning-position 2)
;; 'face 'side-hustle-highlight))))))))
;;; Commands ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun side-hustle-goto-item ()
"Go to the `imenu' item at point in other window."
(interactive)
(when (side-hustle-button-ensure)
(side-hustle-button-action (button-at (point)))))
(defun side-hustle-show-item ()
"Display the `imenu' item at point in other window."
(interactive)
(when (side-hustle-button-ensure)
(side-hustle-button-action (button-at (point)) nil t)))
;;;###autoload
(defun side-hustle-toggle ()
"Pop up a side window containing `side-hustle'."
(interactive)
(if (eq major-mode 'side-hustle-mode)
(quit-window)
(let ((display-buffer-mark-dedicated t)
(buf (side-hustle-get-buffer-create (current-buffer))))
(if (get-buffer-window buf (selected-frame))
(delete-windows-on buf (selected-frame))
(display-buffer-in-side-window
buf (append side-hustle-display-alist
(when side-hustle-persistent-window
(list '(window-parameters
(no-delete-other-windows . t))))))
(with-current-buffer buf (side-hustle-refresh))
;; (side-hustle-highlight-current)
(when side-hustle-select-window
(select-window (get-buffer-window buf (selected-frame))))))))
;; (defalias 'toggle-side-hustle #'side-hustle-toggle)
;;; Mode Definition ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar side-hustle-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "p") #'previous-line)
(define-key map (kbd "n") #'next-line)
(define-key map (kbd "q") #'quit-window)
(define-key map (kbd "g") #'side-hustle-refresh)
(define-key map (kbd "RET") #'side-hustle-goto-item)
(define-key map (kbd "SPC") #'side-hustle-show-item)
(define-key map (kbd "TAB") #'forward-button)
(define-key map (kbd "S-TAB") #'backward-button)
(define-key map (kbd "<backtab>") #'backward-button)
map))
(define-derived-mode side-hustle-mode
special-mode "Side-Hustle"
"Major mode to navigate `imenu' via a side window.
You should not activate this mode directly, rather, call
`side-hustle-toggle' \\[side-hustle-toggle] in a source buffer."
(add-to-invisibility-spec '(hustle-invisible . t)))
(provide 'side-hustle)
;;; side-hustle.el ends here
;; Local Variables:
;; coding: utf-8-unix
;; fill-column: 80
;; require-final-newline: t
;; sentence-end-double-space: nil
;; indent-tabs-mode: nil
;; End: