This repository was archived by the owner on Mar 14, 2021. It is now read-only.
forked from nickgammon/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource_scanner.xml
375 lines (284 loc) · 9.66 KB
/
Source_scanner.xml
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="Source_scanner"
author="Nick Gammon"
id="efc3c27a95ba6d8b534e46d5"
language="Lua"
purpose="Scans source files for keywords"
date_written="2012-11-04"
requires="4.41"
version="1.1"
>
<description>
find <phrase>
eg.
cat AND dog --> both words
cat dog --> both words, the "AND" is implied
fish OR bicycle --> one or the other
cat NOT food --> one word but not the other
bite NEAR me --> one near the other (within 10 words)
disk NEAR/3 drive --> one within 3 words of the other
"trouble brewing" --> exact phrase
chip* --> prefix query, matches chip, chips, chipping etc.
fish NOT (bacon OR eggs) --> brackets can be used to clarify groupings
The words AND / OR / NEAR / NOT must be in upper case or they just match those words literally.
find name <wildcard> --> filter on file name, not contents
You can use "f" instead of "filter".
index --> scans new directory (choose one) to re-make the database
help --> this text
</description>
</plugin>
<aliases>
<alias
script="filter_files"
match="^(?:f|find) (.+)$"
enabled="y"
regexp="y"
sequence="100"
>
</alias>
<alias
script="load_text_files"
match="index"
enabled="y"
sequence="100"
>
</alias>
<alias
script="OnHelp"
match="help"
enabled="y"
sequence="100"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
-- ----------- CONFIGURATION -----------
-- file types, separate by spaces, commas, semicolons, whatever.
-- We assume suffixes are alphanumeric
SUFFIXES = "cpp,c,h,xml,lua"
-- default drive for directory picker
DRIVE_TO_SCAN = "C:\\\\"
-- file name for the database (MUSHclient directory)
SOURCE_FILES = GetInfo (66) .. "\\source_files.db"
-- colour to show file names in
HYPERLINK_COLOUR = "dodgerblue"
-- viewer program
TEXT_VIEWER = "C:\\Program Files\\Crimson Editor\\cedt.exe"
-- or maybe:
-- TEXT_VIEWER = "C:\\Windows\\notepad.exe"
-- view program title (for bringing to the front)
EDITOR_WINDOW_NAME = "Crimson Editor"
-- or maybe:
-- EDITOR_WINDOW_NAME = "Notepad"
-- show this many before suggesting they narrow the search
MAX_RESULTS = 500
-- number of tokens to display around the snippet
SNIPPET_SIZE = -7
-- strip from displayed file names to avoid clutter, like always having "Desktop" or
-- "My Documents" there, which you might not care about.
PREFIX_TO_OMIT = "^%a:\\Documents and Settings\\.-\\"
-- for opening editor and bringing it to the front
-- available here: http://www.gammon.com.au/files/mushclient/lua5.1_extras/windows_utils.zip
assert (package.loadlib ("windows_utils.dll", "luaopen_windows_utils")) ()
require "alphanum"
os.setlocale ("", "time")
-- build suffixes into a table (keyed by suffix) for quick lookup later
local suffixes = {}
for w in string.gmatch (SUFFIXES, "[%a%d]+") do
suffixes [w:lower ()] = true
end -- for
-- helper function to check for database errors
function dbcheck (code)
if code ~= sqlite3.OK and -- no error
code ~= sqlite3.ROW and -- completed OK with another row of data
code ~= sqlite3.DONE then -- completed OK, no more rows
local err = db:errmsg() -- the rollback will change the error message
db:exec ("ROLLBACK") -- rollback any transaction to unlock the database
error (err, 2) -- show error in caller's context
end -- if error
end -- dbcheck
-- find where to start scanning from
function get_text_path ()
local path = utils.directorypicker ("Directory for source", DRIVE_TO_SCAN)
if not path then
return false
end -- if
text_drive, text_path = string.match (path, "^(%a:)(.+)$")
return true
end -- get_text_path
-- process one file
function read_file (text_drive, name, stats)
SetStatus ("Processing: " .. name)
-- ignore empty files
if stats.size == 0 then
return
end -- if
-- look for wanted suffix
if not suffixes [string.match (name:lower (), "%.(%a+)$")] then
return
end -- if
count = count + 1
-- read contents into memory
local f = assert (io.open (text_drive .. name, "rb"))
local s = f:read ("*a")
f:close () -- close it
-- quotes have to be doubled
local fixed_name = string.gsub (text_drive .. name, "'", "''")
s = string.gsub (s, "%z", "") -- get rid of 0x00 bytes
local fixed_contents = string.gsub (s, "'", "''")
-- save contents
dbcheck (db:exec ( string.format ([[
INSERT INTO source (name, contents, size, date_written)
VALUES ('%s', '%s', %i, %i);
]],
fixed_name,
fixed_contents,
stats.size,
stats.write_time)))
end -- read_file
-- process one directory (recurses if necessary)
function scan_text_dir (text_drive, text_path, f)
-- find all files in that directory
local t = assert (utils.readdir (text_drive .. text_path .. "\\*"),
"Could not find " .. text_drive .. text_path .. "\\*")
for k, v in pairs (t) do
if not v.hidden and
not v.system and
k:sub (1, 1) ~= "." then
-- recurse to process file or subdirectory
if v.directory then
scan_text_dir (text_drive, text_path .. "\\" .. k, f)
else
f (text_drive, text_path .. "\\" .. k, v)
end -- if directory or not
end -- if not hidden or system
end -- for
end -- scan_text_dir
-- scan entire tree
function load_text_files (name, line, wildcards)
if not get_text_path () then
return
end -- no directory
db, err, errmsg = sqlite3.open(SOURCE_FILES)
if not db then
ColourNote ("white", "red", errmsg .. ": " .. SOURCE_FILES)
return
end
-- omit the "tokenize" line to get normal tokenization
-- that line omits underscores as tokens (for C source)
-- the "X" is there because of SQLITE3 bug:
-- http://sqlite.1065341.n5.nabble.com/FTS-simple-tokenizer-with-custom-delimeters-td43926.html
dbcheck (db:exec [[
DROP TABLE IF EXISTS source;
CREATE VIRTUAL TABLE source USING FTS4(name, contents, size, date_written,
tokenize=simple X ' !"#$%&''()*+,-./:;<=>?@[\]^`{|}~'
);
]])
count = 0 -- actual
start_time = utils.timer ()
-- write database inside a transaction
db:exec "BEGIN TRANSACTION;"
scan_text_dir (text_drive, text_path, read_file)
db:exec "COMMIT;"
db:close() -- close
end_time = utils.timer ()
SetStatus ("Ready")
print (string.format ("Loaded %i files in %.1f seconds.", count, end_time - start_time))
end -- load_text_files
local snippets = {}
local reset = ANSI (0)
local highlight = ANSI (1) .. ANSI (36) -- bold and cyan
local unhighlight = ANSI (22) .. ANSI (37) -- unbold and white
-- here when hyperlink clicked to open a particular file
function display_text_file (name)
assert (windows_utils.shell_execute (TEXT_VIEWER, '"' .. name .. '"'))
windows_utils.send_to_front (EDITOR_WINDOW_NAME)
end -- display_text_file
function show_filtered (show_snippet)
local names = {}
for k in pairs (filtered) do
table.insert (names, k)
end -- for
table.sort(names, alphanum (names))
ColourNote ("yellow", "", "Search term: " .. match)
print ""
-- now pull out all entries
for _, name in ipairs (names) do
Hyperlink ("!!" .. GetPluginID () .. ":display_text_file(" .. name .. ")",
(string.gsub (name, PREFIX_TO_OMIT, "")),
"Click to open", HYPERLINK_COLOUR, "", 0)
if name == current then
print (" <-- this file")
else
print ""
end -- if
if show_snippet then
AnsiNote (reset .. filtered [name])
print ""
end -- if
end -- for each file
ColourNote ("yellow", "", "Matched " .. #names .. " file(s) (Search term: " .. match .. ")")
end -- show_filtered
-- search function ... searches on name or contents
function filter_on (what, field)
db, err, errmsg = sqlite3.open(SOURCE_FILES)
if not db then
ColourNote ("white", "red", errmsg .. ": " .. SOURCE_FILES)
return
end
local matches = 0
filtered = {}
local fixed_match = string.gsub (what, "'", "''")
for row in db:nrows(string.format ([[
SELECT name,
snippet(source, '%s', '%s', ' ... ', -1, %i) AS snippet
FROM source WHERE %s MATCH '%s']],
highlight, -- for snippet
unhighlight, -- also for snippet
SNIPPET_SIZE,
field, -- name or contents
fixed_match)) do
filtered [row.name] = row.snippet
matches = matches + 1
end -- for
print (string.rep ("+", 79))
-- show matches
if matches == 0 then
ColourNote ("red", "", "No matches on '" .. match .. "'.")
elseif matches <= MAX_RESULTS then
show_filtered (field == "contents")
else
ColourNote ("red", "", "Too many to list - try using 'this AND that' to narrow it down")
ColourNote ("yellow", "", "Matched " .. matches .. " files (Search term: " .. match .. ")")
end -- if
local source_files = 0
-- show how many files were considered
for row in db:nrows "SELECT COUNT(*) AS row_count FROM source;" do
source_files = row.row_count
end -- for
print (source_files, "files searched.")
print (string.rep ("-", 79))
db:close ()
end -- filter_on
-- here when they want to filter on name or contents
function filter_files (name, line, wildcards)
match = wildcards [1]
local a, b = string.match (Trim (match), "(name)%s+(.+)")
if a and a:lower () == "name" then
filter_on (b, "name")
else
filter_on (match, "contents")
end
end -- filter_files
]]>
-- show help
function OnHelp ()
ColourNote ("cyan", "", GetPluginInfo (GetPluginID (), 3))
end
</script>
</muclient>