-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod-fuzzy.js
143 lines (123 loc) · 3.68 KB
/
mod-fuzzy.js
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
const TEXT_COLOR = "green";
let BASEDIR = './';
let MAX_RESULTS = 50;
let workerActive = false;
let msgId = 0;
let worker = null;
const pendingPromises = {};
export function fuzzySearchAsync(pattern, dir = BASEDIR) {
if (!workerActive) {
workerActive = true;
worker = new Worker(new URL("mod-fuzzy-worker.js#1", import.meta.url).href,
{
type: "module",
deno: true,
});
worker.onmessage = function (result) {
const { responseMsgId, rank, error } = result.data;
const promise = pendingPromises[responseMsgId];
if (error) {
promise.reject(error);
return;
}
promise.resolve(rank);
pendingPromises[responseMsgId] = null;
}
}
return new Promise((resolve, reject) => {
msgId += 1;
pendingPromises[msgId] = {resolve, reject};
worker.postMessage({ msgId, pattern, dir });
});
}
export function setFuzzySearchDirectory(dir) {
BASEDIR = dir;
}
export function setFuzzySearchMaxResults(n) {
MAX_RESULTS = n;
}
lisp.defun({
name: "fuzzy-search-set-basedir",
interactive: true,
func: () => {
const base = lisp.read_directory_name("Fuzzy Base Dir: ");
setFuzzySearchDirectory(base);
}
});
lisp.defun({
name: "fuzzy-search-set-max-results",
interactive: true,
args: "nNew Value ",
func: (n) => { setFuzzySearchMaxResults(n) }
});
const setTextColor = (min, max, strSymbol, color) => {
return lisp.add_text_properties(min, max, lisp.list(lisp.q.font_lock_face,
lisp.list(lisp.keywords.foreground,
color)), strSymbol);
};
lisp.defun({
name: "fuzzy-find-file",
interactive: true,
func: () => {
let s = lisp.line_beginning_position();
let e = lisp.line_end_position();
lisp.find_file(lisp.buffer_substring(s, e));
}
});
lisp.defvar(lisp.symbols.fuzzy_mode_keymap,
lisp.make_keymap(),
"keymap");
lisp.define_key(lisp.symbols.fuzzy_mode_keymap,
lisp.kbd("RET"),
lisp.quote(lisp.symbols.fuzzy_find_file));
lisp.define_minor_mode(lisp.symbols.fuzzy_mode,
"Mode used for interacting with fuzzy search results",
lisp.symbols.nil,
"Fuzzy",
lisp.keywords.keymap,
lisp.symbols.fuzzy_mode_keymap);
lisp.defun({
name: "fuzzy-search",
interactive: true,
args: "MInput >> ",
func: (str) => {
fuzzySearchAsync(str)
.then((results) => {
// falling back due to older version of emacs-ng
const makeString = lisp.make.string || lisp_string;
const currentBuffer = lisp.current_buffer();
const resultBuffer = lisp.get_buffer_create('*Fuzzy Search*');
if (!lisp.eq(resultBuffer, currentBuffer)) {
lisp.switch_to_buffer_other_window(resultBuffer);
}
lisp.tabulated_list_mode();
lisp.fuzzy_mode();
const columns = lisp.make.list(
[`Results for "${str}"; Base Directory ${BASEDIR}`, 0, lisp.q.nil],
);
lisp.setq(lisp.symbols.tabulated_list_format, lisp.make.array([columns]));
const filtered = [];
let b = null;
for (let i = 0; i < Math.min(MAX_RESULTS, results.length); ++i) {
let name = results[i][0].path;
let idxs = results[i][2];
const str = makeString(name);
let currentRange = idxs[0];
for (let j = 0; j < idxs.length; ++j) {
if (j === idxs.length - 1) {
setTextColor(currentRange, idxs[j] + 1, str, TEXT_COLOR);
} else if (idxs[j + 1] - idxs[j] !== 1) {
setTextColor(currentRange, idxs[j] + 1, str, TEXT_COLOR);
currentRange = idxs[j + 1];
}
}
let namevec = lisp.make.array([str]);
filtered.push(lisp.make.list([lisp.q.nil, namevec]));
}
const data = lisp.list(...filtered);
lisp.setq(lisp.symbols.tabulated_list_entries, data);
lisp.tabulated_list_init_header();
lisp.tabulated_list_print();
});
}
});