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
|
VERSION = '0.1.0'
local fmt = import 'fmt'
local micro = import 'micro'
local config = import 'micro/config'
function init()
micro.SetStatusInfoFn 'textcount.lines'
micro.SetStatusInfoFn 'textcount.paragraphs'
micro.SetStatusInfoFn 'textcount.sentences'
micro.SetStatusInfoFn 'textcount.words'
config.MakeCommand('wc.l', linesi, config.NoComplete)
config.MakeCommand('wc.p', paragraphsi, config.NoComplete)
config.MakeCommand('wc.s', sentencesi, config.NoComplete)
config.MakeCommand('wc.w', wordsi, config.NoComplete)
config.MakeCommand('wc', wc, config.NoComplete)
end
-- utils
local function pattern(b, pat)
local str = fmt.Sprintf('%s', b:Bytes())
local _, n = str:gsub(pat, '')
return n
end
local function genb(...)
local args = {...}
return function(b)
local total = 0
for _, v in ipairs(args) do
total = total + pattern(b, v)
end
return tostring(total)
end
end
local function geni(f, label)
return function(bp)
local data = f(bp.Buf)
micro.InfoBar():Message(data .. ' ' .. label)
end
end
-- functions
lines = genb '\n'
linesi = geni(lines, 'Lines')
paragraphs = genb('\n\n', '$') -- $ handles the final paragraph
paragraphsi = geni(paragraphs, 'Paragraphs')
sentences = genb '%.[^%.]'
sentencesi = geni(sentences, 'Sentences')
words = genb '%S+'
wordsi = geni(words, 'Words')
-- user-defined patterns
function wc(bp, args)
local total = 0
for _, v in args() do
total = total + pattern(bp.Buf, v)
end
micro.InfoBar():Message(tostring(total))
end
|