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
|
VERSION = '0.1.0'
local micro = import 'micro'
local config = import 'micro/config'
local util = import 'micro/util'
function init()
micro.SetStatusInfoFn 'textcount.characters'
micro.SetStatusInfoFn 'textcount.lines'
micro.SetStatusInfoFn 'textcount.paragraphs'
micro.SetStatusInfoFn 'textcount.sentences'
micro.SetStatusInfoFn 'textcount.words'
config.MakeCommand('wc.c', charactersi, config.NoCompete)
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
local function selections(b)
local str = ''
for _, c in b:GetCursors()() do
if c:HasSelection() then
str = str .. ' ' .. util.String(c:GetSelection())
end
end
return str
end
-- utils
local function pattern(b, pat)
-- if there is a selection, we use it, else we use b:Bytes()
local str = selections(b)
if str == '' then str = util.String(b:Bytes()) end
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
characters = genb '.'
charactersi = geni(characters, 'Characters')
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
|