dotfiles

Personal dotfiles
git clone https://git.bracken.jp/dotfiles.git
Log | Files | Refs | LICENSE

commit 058c0b6aae2c9039a0ffa39f1fbc793dec616e29
parent 0d3e6f055ad9304dcfd306324c95d5fc7c537613
Author: Chris Bracken <chris@bracken.jp>
Date:   Mon, 25 May 2026 23:02:54 +0900

nvim: improve beancount format-on-save

* Change to BufWritePost to BufWritePre to format in-memory before the
  save.
* Scopes the autocommand to just the active buffer via `buffer = 0`.
* Read from stdin
* Revert formatting via silent undo if the fomatter fails

Diffstat:
M.config/nvim/after/ftplugin/beancount.lua | 25+++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/.config/nvim/after/ftplugin/beancount.lua b/.config/nvim/after/ftplugin/beancount.lua @@ -3,19 +3,24 @@ vim.opt_local.textwidth = 0 vim.opt_local.wrap = false -- Format on save. -vim.api.nvim_create_autocmd("BufWritePost", { +vim.api.nvim_create_autocmd("BufWritePre", { + buffer = 0, desc = "Run bean-format on save", - callback = function(args) - local filepath = vim.api.nvim_buf_get_name(args.buf) - local filename = vim.fn.fnamemodify(filepath, ":t") -- just the tail name - local cmd + callback = function() + local filepath = vim.api.nvim_buf_get_name(0) + local view = vim.fn.winsaveview() + if filepath:match("[\\/]prices[\\/]") then - -- prices_* are formatted without a hardcoded currency column. - cmd = string.format("silent !bean-format %s -o %s", vim.fn.shellescape(filepath), vim.fn.shellescape(filepath)) + vim.cmd("silent %!bean-format -") else - -- other beancount files are formatted with currency on 80th column. - cmd = string.format("silent !bean-format -c 80 %s -o %s", vim.fn.shellescape(filepath), vim.fn.shellescape(filepath)) + vim.cmd("silent %!bean-format -c 80 -") + end + + if vim.v.shell_error ~= 0 then + vim.cmd("silent undo") + vim.notify("bean-format failed (syntax error)", vim.log.levels.WARN) + else + vim.fn.winrestview(view) end - vim.cmd(cmd) end, })