-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelm-flx.el
267 lines (230 loc) · 10.5 KB
/
helm-flx.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
;;; helm-flx.el --- Sort helm candidates by flx score -*- lexical-binding: t -*-
;; Copyright (C) 2014-2022 Jonathan Hayase
;; Author: Jonathan Hayase <[email protected]>
;; Keywords: convenience, helm, fuzzy, flx
;; Version: 20220401
;; URL: https://github.com/PythonNut/helm-flx
;; Package-Requires: ((emacs "24.4") (helm "1.7.9") (flx "0.5"))
;;; License:
;; 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:
;; This package implements intelligent helm fuzzy sorting, provided by flx.
;; You can install the package by either cloning it yourself, or by doing M-x package-install RET helm-flx RET.
;; After that, you can enable it by putting the following in your init file:
;; (helm-flx-mode +1)
;; See the README for more info.
;;; Code:
(eval-when-compile
(with-demoted-errors "Byte-compile: %s"
(require 'helm)
(require 'flx)))
(defgroup helm-flx nil
"Sort helm candidates by flx score"
:group 'convenience
:prefix "helm-flx-")
(defcustom helm-flx-limit 5000
"The maximum number of helm candidates (N) to sort. If the number of
candidates is greater than this number, only sort the first N (presorted by length). Set to nil to sort all candidates."
:type 'number
:group 'helm-flx)
(defcustom helm-flx-for-helm-find-files t
"Master toggle for helm-find-files support"
:type 'boolean
:group 'helm-flx)
(defcustom helm-flx-for-helm-locate nil
"Master toggle for helm-locate support"
:type 'boolean
:group 'helm-flx)
(defvar helm-flx-cache nil
"The current flx cache for helm-flx.")
(defvar helm-flx-old-helm-fuzzy-sort-fn nil
"The old value of helm-fuzzy-sort-fn")
(defvar helm-flx-old-helm-fuzzy-matching-highlight-fn nil
"The old value of helm-fuzzy-matching-highlight-fn")
(defvar helm-flx-old-helm-locate-fuzzy-sort-fn nil
"The old value of helm-locate-fuzzy-sort-fn")
(with-eval-after-load 'flx
(setq helm-flx-cache (flx-make-filename-cache)))
(defun helm-flx-sort (candidates pattern display-string-fn &optional score-fn)
(require 'flx)
(let* ((candidates (mapcar #'helm-flx-candidate-string candidates))
(num-cands (length candidates)))
(mapcar #'car
(sort (mapcar
(or (and score-fn
(funcall score-fn pattern display-string-fn))
(lambda (cand)
(cons cand
(or (car (flx-score (funcall display-string-fn
cand)
pattern
helm-flx-cache))
most-negative-fixnum))))
(if (or (not helm-flx-limit)
(< num-cands helm-flx-limit))
candidates
(let* ((seq (sort candidates
(lambda (c1 c2)
(< (length (funcall display-string-fn
c1))
(length (funcall display-string-fn
c2))))))
(end (min helm-flx-limit
num-cands
(length seq)))
(result nil))
(dotimes (_ end result)
(push (pop seq) result)))))
(lambda (c1 c2)
;; break ties by length
(if (/= (cdr c1) (cdr c2))
(> (cdr c1)
(cdr c2))
(< (length (funcall display-string-fn
(car c1)))
(length (funcall display-string-fn
(car c2))))))))))
(defun helm-flx-helm-ff-sort-candidates (old-fun candidates source)
"Sort function for `helm-source-find-files'.
Return candidates prefixed with basename of `helm-input' first."
(require 'flx)
(if (string= (file-name-nondirectory helm-input) "")
candidates
(if (string-match-p " " helm-pattern)
(funcall old-fun candidates source)
(helm-flx-sort candidates helm-input
(lambda (cand)
(substring-no-properties
(if (consp cand)
(cdr cand)
cand)))
(lambda (pattern display-string-fn)
(lambda (cand)
(cons cand
(if (string-match-p
"^\\[\\?\\]"
(funcall display-string-fn cand))
most-positive-fixnum
(or (car (flx-score
(funcall display-string-fn cand)
pattern
helm-flx-cache))
most-negative-fixnum)))))))))
(defun helm-flx-fuzzy-matching-sort (candidates source &optional use-real)
(let ((candidates (mapcar #'helm-flx-candidate-string candidates)))
(if (string= helm-pattern "")
candidates
(if (string-match-p " " helm-pattern)
(helm-fuzzy-matching-default-sort-fn candidates source)
(helm-flx-sort candidates
helm-pattern
(if use-real
(lambda (cand)
(if (consp cand)
(cdr cand)
cand))
(lambda (cand)
(if (consp cand)
(car cand)
cand))))))))
(defun helm-flx-candidate-string (candidate)
(cond
((symbolp candidate) (symbol-name candidate))
(t candidate)))
(defun helm-flx-fuzzy-highligher (display pattern)
(with-temp-buffer
(insert (propertize display 'read-only nil))
(goto-char (point-min))
(dolist (index (cdr (flx-score
(substring-no-properties display)
pattern helm-flx-cache)))
(with-demoted-errors "helm-fx error: %s"
(add-text-properties
(1+ index) (+ 2 index) '(face helm-match))))
(buffer-string)))
(defun helm-flx-fuzzy-highlight-match (candidate &optional pattern diacritics file-comp-p)
(require 'flx)
(let ((pattern (or pattern helm-pattern)))
(if (string-match-p " " pattern)
(helm-fuzzy-default-highlight-match candidate pattern diacritics file-comp-p)
(let* ((candidate (helm-flx-candidate-string candidate))
(pair (and (consp candidate) candidate))
(display (if pair (car pair) candidate))
(real (cdr pair)))
(setq display (helm-flx-fuzzy-highligher display pattern))
(if real (cons display real) display)))))
(defun helm-flx-helm-ff-filter-candidate-one-by-one (old-fun &rest args)
(let ((candidate (apply old-fun args)))
(when (and (consp candidate)
(not (string-match-p "/$" helm-input))
;; Don't highlight the [?] <whatever you typed> cand
(not (string-match-p "^ " (car candidate))))
;; Horrible hack to split into directory and basename
;; while preserving text-properties
(let* ((display (car candidate))
(directory-length (length (file-name-directory display)))
(directory (substring display 0 directory-length))
(basename (substring display directory-length)))
;; Candidate must be modified in-place
(setcar candidate
(concat directory
(helm-flx-fuzzy-highligher
basename
(helm-basename helm-input))))))
candidate))
(defun helm-flx-helm-locate-fuzzy-sort-fn (candidates)
(helm-flx-sort candidates
(or (bound-and-true-p helm-input)
"")
#'identity))
;;;###autoload
(define-minor-mode helm-flx-mode
"helm-flx minor mode"
:init-value nil
:group 'helm-flx
:global t
(if helm-flx-mode
(progn (setq helm-flx-old-helm-fuzzy-sort-fn
(bound-and-true-p helm-fuzzy-sort-fn))
(setq helm-flx-old-helm-fuzzy-matching-highlight-fn
(bound-and-true-p helm-fuzzy-matching-highlight-fn))
(setq helm-fuzzy-sort-fn
#'helm-flx-fuzzy-matching-sort)
(setq helm-fuzzy-matching-highlight-fn
#'helm-flx-fuzzy-highlight-match)
(when helm-flx-for-helm-find-files
(advice-add 'helm-ff-sort-candidates
:around
#'helm-flx-helm-ff-sort-candidates)
(advice-add 'helm-ff-filter-candidate-one-by-one
:around
#'helm-flx-helm-ff-filter-candidate-one-by-one))
(when helm-flx-for-helm-locate
(setq helm-flx-old-helm-locate-fuzzy-sort-fn
(bound-and-true-p helm-locate-fuzzy-sort-fn))
(setq helm-locate-fuzzy-sort-fn
#'helm-flx-helm-locate-fuzzy-sort-fn)))
(setq helm-fuzzy-sort-fn
(or helm-flx-old-helm-fuzzy-sort-fn
#'helm-fuzzy-matching-default-sort-fn))
(setq helm-fuzzy-matching-highlight-fn
(or helm-flx-old-helm-fuzzy-matching-highlight-fn
#'helm-fuzzy-default-highlight-match))
(advice-remove 'helm-ff-sort-candidates
#'helm-flx-helm-ff-sort-candidates)
(advice-remove 'helm-ff-filter-candidate-one-by-one
#'helm-flx-helm-ff-filter-candidate-one-by-one)
(when helm-flx-old-helm-locate-fuzzy-sort-fn
(setq helm-locate-fuzzy-sort-fn
helm-flx-old-helm-locate-fuzzy-sort-fn))))
(provide 'helm-flx)
;;; helm-flx.el ends here