diff options
| author | 2021-07-07 17:39:55 -0400 | |
|---|---|---|
| committer | 2021-07-07 17:39:55 -0400 | |
| commit | 97bdcb51d04f302965c8e49a1d7173dddbf1cc31 (patch) | |
| tree | 4e492f70a75a735d4a47c6585ac61ad6974e9c73 /dot_config/micro/plug/textcount/textcount.lua | |
| parent | fish: exa: remove lr/lp, just set tree instead (diff) | |
micro: add textcount plugin, enable it for .md/.adoc
Diffstat (limited to 'dot_config/micro/plug/textcount/textcount.lua')
| -rw-r--r-- | dot_config/micro/plug/textcount/textcount.lua | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/dot_config/micro/plug/textcount/textcount.lua b/dot_config/micro/plug/textcount/textcount.lua new file mode 100644 index 0000000..2d123fa --- /dev/null +++ b/dot_config/micro/plug/textcount/textcount.lua @@ -0,0 +1,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 |
