-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuz-bin.el
98 lines (75 loc) · 3.03 KB
/
fuz-bin.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
;;; fuz-bin.el --- Fast and precise fuzzy scoring/matching utils -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Zhu Zihao
;; Copyright (C) 2021 Shen, Jen-Chieh
;; Created date 2021-10-15 01:03:01
;; Author: Zhu Zihao <all_but_last@163.com>
;; Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/fuz-bin
;; Version: 1.5.0
;; Package-Requires: ((emacs "26.1"))
;; Keywords: lisp
;; 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 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:
;;
;; Fast and precise fuzzy scoring/matching utils.
;;
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(defconst fuz-bin--bin-dir
(concat (file-name-directory load-file-name) "bin/")
"Pre-built binaries directory path.")
(defconst fuz-bin--dyn-name "fuz_bin_dyn"
"Dynamic module name.")
;;
;; (@* "Externals" )
;;
(declare-function fuz-bin-dyn-score-clangd "fuz-bin-dyn")
(declare-function fuz-bin-dyn-score-skim "fuz-bin-dyn")
(declare-function fuz-bin-dyn-indices-clangd "fuz-bin-dyn")
(declare-function fuz-bin-dyn-indices-skim "fuz-bin-dyn")
;;
;; (@* "Utils" )
;;
(defsubst fuz-bin-score-skim (pattern str)
"Match STR against PATTERN, using skim's algorithm.
Sign: (-> Str Str (Option (Listof Long)))
Return (SCORE . (INDICES)) if matched, otherwise return nil."
(if-let* ((total-score (fuz-bin-dyn-score-skim pattern str)))
(cons total-score (fuz-bin-dyn-indices-skim pattern str))
nil))
(defsubst fuz-bin-score-clangd (pattern str)
"Match STR against PATTERN, using clangd's algorithm.
Sign: (-> Str Str (Option (Listof Long)))
Return (SCORE . (INDICES)) if matched, otherwise return nil."
(if-let* ((total-score (fuz-bin-dyn-score-clangd pattern str)))
(cons total-score (fuz-bin-dyn-indices-clangd pattern str))
nil))
;;
;; (@* "Bootstrap" )
;;
;;;###autoload
(defun fuz-bin-load-dyn (&optional quiet)
"Load dynamic module. Optional QUIET argument to disable load message."
(interactive)
(unless (featurep 'fuz-bin-dyn)
(let* ((dyn-name (cl-case system-type
((windows-nt ms-dos cygwin) (concat fuz-bin--dyn-name ".dll"))
(darwin (concat "lib" fuz-bin--dyn-name ".dylib"))
(t (concat "lib" fuz-bin--dyn-name ".so"))))
(dyn-path (concat fuz-bin--bin-dir dyn-name)))
(module-load dyn-path)
(unless quiet
(message "[INFO] Successfully load dynamic module, `%s`" dyn-name)))))
(provide 'fuz-bin)
;;; fuz-bin.el ends here