-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwordcount.lua
49 lines (44 loc) · 1.04 KB
/
wordcount.lua
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
local fn, vim = vim.fn, vim
local b, bo = vim.b, vim.bo
local common = require('hardline.common')
local enabled = false
local cache = ''
local options = {
filetypes = {
'asciidoc',
'help',
'mail',
'markdown',
'nroff',
'org',
'rst',
'plaintex',
'tex',
'text',
},
max_lines = 5000,
}
local function in_visual()
local mode = common.modes[fn.mode()] or common.modes['?']
return mode.state == 'visual'
end
local function get_wordcount()
local query = in_visual() and 'visual_words' or 'words'
local wordcount = fn.wordcount()[query]
return string.format('%d words', wordcount)
end
local function get_item()
if not enabled then
common.set_cache('hardline_wordcount')
enabled = true
end
if not vim.tbl_contains(options.filetypes, bo.filetype) then return '' end
if fn.line('$') > options.max_lines then return '' end
if b.hardline_wordcount and not in_visual() then return cache end
b.hardline_wordcount = true
cache = get_wordcount()
return cache
end
return {
get_item = get_item,
}