Created
October 16, 2022 03:56
-
-
Save arp242/ffeb8d3ba05a57ec08ffa78067485f69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def <SID>wordcount(): string | |
var cursor = getcurpos() | |
var current_word = 0 | |
var word_count = 0 | |
var lnum = 1 | |
for line in getline(1, '$') | |
# We're at the line the cursor is on, get the current word. | |
if lnum == cursor[1] | |
# Start with the number of words already counted. | |
current_word = word_count | |
var col = 0 | |
# Split words by start-of-word; return value includes any non-word | |
# characters too. | |
for word in line->split('\<') | |
# Contains 'isk'? This is a word! Skip if not. | |
if word =~ '\k' | |
current_word += 1 | |
endif | |
# Count word until we're beyond the cursor position. | |
col += len(word) | |
if col > cursor[2] | |
break | |
endif | |
endfor | |
endif | |
for word in line->split('') | |
if word =~ '\k' | |
word_count += 1 | |
endif | |
endfor | |
lnum += 1 | |
endfor | |
return execute("normal! g\<C-g>") | |
->trim() | |
->substitute('Word \d\+ of \d\+', $'Word {current_word} of {word_count}', '') | |
enddef | |
augroup markdown-wordcount | |
au! | |
au filetype markdown nnoremap <buffer> <silent> g<C-g> :echo <SID>wordcount()<CR> | |
augroup end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment