Skip to content

kghose/lekha.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lekha.nvim

Lekha is a minimalist (in appearance and function) NeoVim plugin for novel writing:

  • It expects the work to be all in one file.
  • Top level headings (Lines that start with # ) are interpreted as chapters.
  • It periodically computes and displays word counts for chapters and the whole manuscript. Commented text (<!-- x y z -->) is not included in the word count.
  • Lines that contain a string like TODO:something appear in a list of TODOs for each chapter. The something has to be a single word. Intended use is to add a TODO in a comment block with details of what TODO.
  • It uses NeoVim's native command auto-complete feature to display chapters, chapter word counts and TODOs when the LekhaGoto command is called (Please see screenshot above).
  • There is no split pane. This reduces visual clutter while keeping the information accessible.

Installation

git clone the repository under .config/nvim/pack/bengal-bionics/start The bengal-bionics part is just a name I like, you can call it anything. This gets you the Lekha Lua plugin, the Lekha colorschemes) and the markdown ftplugin

If you just want the Lua plugin, copy the lua folder to somewhere in your runtime path. Then add something like this to your markdown.vim:

-- Register Lekha commands
lua require("lekha").enable()
-- Register a convenient shortcut key 
nmap <Tab> :LekhaGoto<Space>

Now by hitting Tab twice in normal mode you can access the LekhaGoto command and NeoVim's auto-complete will list chapters.

Try it out with the example document!

Use in statusline

Add %{%v:lua.require'lekha'.status_line()%} to the status line to print useful information, like chapter name, chapter wordcount and document word count.

A note about word counts

NeoVim has a built in wordcount() function, but it must be redoing the wordcount at each call: I found it made scrolling on NeoVim unusably slow and stuttery for documents with > 100,000 words.

lekha only recomputes things when we stop typing (CursorHold/CursorHoldI) so it doesn't interrupt our flow. lekha has a document word count property which is used in the status_line function.

Other commands

Lekha supplies two other commands LekhaGotoChapter and LekhaGotoTodo where the auto-completion shows only chapters or TODOs respectively.

Development

Code formatting using stylua. I just use the binary release at https://github.com/JohnnyMorganz/StyLua/releases

Test using:

lua lekha_test.lua -v

Notes

  1. I had little trouble writing this NeoVim plugin in Lua though I had never written a NeoVim plugin before and didn't know Lua. The NeoVim documentation is adequate though I had to supplement with web searches for a few things.
  2. Vim/NeoVim's editing efficiency impresses me. I have a 140,000 word manuscript and Vim/NeoVim handles it without any issue. I used VS Code for a long time but when I tried editing the manuscript with it, the simple task of word wrapping (which needs a plugin on VS Code) broke in a subtle way making it unusable.
  3. I used an O(N) algorithm (N = manuscript length). It takes ~20ms to process a 140,000 word (~800 kB)text, which is decently fast for an interpreted, studiously dynamic language.

Lua/NeoVim API Concepts

  1. Ordered arrays
  2. Calling NeoVim commands from Lua
  3. Creating autocommands and commands
  4. Unit testing with LuaUnit

References

  1. outline.nvim is both way more powerful and not powerful enough for my writing needs. It has a split-pane outline view, but not per-chapter word count and no TODO/bookmark feature.
  2. minimal-bookmarks.nvim
  3. section-wordcount.nvim

About

NeoVim plugin for writers using Markdown

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published