-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoicemacs-extend-helm.el
163 lines (121 loc) · 5.27 KB
/
voicemacs-extend-helm.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
(require 'helm)
(require 'voicemacs-base)
;; TODO: Maybe check the nature of the current buffer? This seems janky.
;;
;; (eq (current-buffer)
;; (with-helm-buffer (ignore-errors
;; (current-buffer))))
(voicemacs-define-sync-change-buffer in-helm-prompt
:update (bound-and-true-p helm-alive-p)
:defer nil)
;; The minibuffer prompt for the current helm session
;; (voicemacs-define-sync-change-buffer helm-prompt-text
;; :update (and (bound-and-true-p helm-alive-p) helm--prompt)
;; :defer nil)
;; The name of the current helm buffer
(voicemacs-define-sync-change-buffer helm-buffer-name
:update (and (bound-and-true-p helm-alive-p)
helm-last-buffer)
:defer nil)
;; Hacky. Might not work.
(voicemacs-define-sync-change-buffer helm-title
:update (when (bound-and-true-p helm-alive-p)
(with-helm-buffer
(buffer-substring-no-properties (line-beginning-position 0)
(line-end-position 0))))
:defer nil)
(defun voicemacs--helm-line-number ()
"Line number of the current candidate."
(with-helm-buffer (line-number-at-pos)))
(defun voicemacs-helm-goto-line (line &optional do-not-restore)
"Go to the candidate on `line'.
`do-not-restore' is used for internal purposes. It suppresses
infinite loops on a recursive call."
;; TODO: This structure is gross. Rewrite it.
(let ((start-line (voicemacs--helm-line-number)))
(helm-beginning-of-buffer)
(let ((last-line (voicemacs--helm-line-number)))
(while (< (voicemacs--helm-line-number) line)
(helm-next-line)
(when (= (voicemacs--helm-line-number) last-line)
;; No more candidates - restore original selection.
(unless do-not-restore
(voicemacs-helm-goto-line start-line t))
(error "Candidate does not exist"))
(setq last-line (voicemacs--helm-line-number)))))
;; Redisplay & pause so the user can see we moved the selection.
(redisplay t)
(sit-for 0.1))
;; TODO: Only expose this while minor mode active?
(voicemacs-expose-function 'voicemacs-helm-goto-line)
(defun voicemacs--local-override-face (face &rest overrides)
"Override a face in the current buffer only."
(setq-local face-remapping-alist
(cons `(,face ,@overrides)
;; Remove any overrides for this face that already exist.
(-filter (lambda (mapping)
(not (eq (car mapping) face)))
face-remapping-alist))))
(defun voicemacs--enable-selection-line-numbers (&rest _)
"Make the current buffer show bold, absolute line numbers.
These numbers can be used for selection."
;; Default line numbers are subtle. These are selection numbers, make them
;; stand out.
;;
;; TODO: Extract line numbers face?
;;
;; FIXME: `bold' will override the color in some themes, e.g. `leuven'
;; (voicemacs--local-override-face 'line-number 'bold 'font-lock-keyword-face)
(voicemacs--local-override-face 'line-number 'helm-bookmark-directory)
;; (voicemacs--local-override-face 'line-number-current-line
;; 'bold
;; 'helm-selection
;; 'font-lock-keyword-face)
(voicemacs--local-override-face 'line-number-current-line
'helm-selection
'helm-bookmark-directory)
(setq-local display-line-numbers 'absolute))
(defun voicemacs--show-helm-numbers ()
(when helm-alive-p
(with-helm-buffer
(voicemacs--enable-selection-line-numbers))))
(defun voicemacs--helm-numbers-mode-setup ()
(cl-assert (boundp 'display-line-numbers) nil
"`display-line-numbers' not available")
(add-hook 'helm-update-hook 'voicemacs--show-helm-numbers))
(defun voicemacs--helm-numbers-mode-teardown ()
(remove-hook 'helm-update-hook 'voicemacs--show-helm-numbers)
;; Also disable any active numbers
(when helm-alive-p
(with-helm-buffer
(setq-local display-line-numbers nil))))
(define-minor-mode voicemacs-helm-numbers-mode
"When active, helm will number candidates with absolute line numbers.
Incompatible with `helm-display-line-numbers-mode'."
:group 'voicemacs
:global t
:lighter nil
:after-hook (if voicemacs-helm-numbers-mode
(voicemacs--helm-numbers-mode-setup)
(voicemacs--helm-numbers-mode-teardown)))
;; TODO: System for enabling by default?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: Maybe make a generalised function to invoke nonblocking RPC calls
(defun voicemacs-helm-swoop (query)
"Run `helm-swoop' with a specific `query' on a timer.
Allows helm-swoop to be invoked via RPC without blocking."
;; Type check because it's delayed.
(assert (stringp query))
(run-with-timer 0 nil 'helm-swoop :query query))
(voicemacs-expose-function 'voicemacs-helm-swoop)
(defun voicemacs-helm-submit-if-single-match ()
"Submit the match if only one exists."
;; Ensure matches are updated
(when (helm--updating-p)
(helm-refresh))
;; TODO: How to get currently matching candidates?
(helm-candidate-number-limit)
(helm-confirm-and-exit-minibuffer)
)
(provide 'voicemacs-extend-helm)
;;; voicemacs-extend-helm.el ends here.