Wordcount.kak - Count characters, words, lines, and paragraphs

Here is a first plugin for Kakoune:

https://github.com/ftonneau/wordcount.kak

Basically this is a wrapper around wc, except for paragraphs (which are counted in pure Kakoune). The plugin is very simple, but prose writers may find it useful.

NB: the other repository in my account is the repeat-char.kak recipe that was discussed on the list some time ago.

2 Likes

Perhaps additional functionality could include word wrapping. The below has a reference for implementation examples at rosettacode. If you’re looking to dig deeper head over to Par docs and Par repo for inspiration.

Example code:

#------------------------------------------------------------------------------------#
/**
 * wordwrap.kt
 *
 * Word Wrap Regex is adapted from source:
 * @author http://rosettacode.org/wiki/Word_wrap#Regex_Implementation
 */
fun String.wordwrap(n: Int): String {
    return Regex("(.{1,$n})(?:\\s[^\\S\r\n]*|\\Z)")
           .findAll(this)
           .joinToString("\n")
           { it.value }
}

fun main() {
    val stdin = generateSequence(::readLine)
    val string = stdin.joinToString()
    val result = string.wordwrap(102)
    println(result)
}
#------------------------------------------------------------------------------------#
# Kakoune commands
#------------------------------------------------------------------------------------#
map global user 'W' '| wordwrap <ret>' -docstring "wrap selected text to column 102"
#------------------------------------------------------------------------------------#
hook global BufSetOption filetype=(latex|tex|markdown|asciidoc) %{
  # set-option buffer formatcmd "par 102f"
  set-option buffer formatcmd wordwrap
  alias buffer fs format-selections
}
#------------------------------------------------------------------------------------#

Bye :wave:.