Mon, 13 Mar 2023 23:43:25 -0500
Port encrypted file edit to lua
.config/nvim/init.lua | file | annotate | diff | comparison | revisions | |
.config/nvim/lua/config/util.lua | file | annotate | diff | comparison | revisions | |
.config/nvim/lua/local/encrypted.lua | file | annotate | diff | comparison | revisions | |
.config/nvim/lua/local/tig.lua | file | annotate | diff | comparison | revisions | |
.config/nvim/plugin/vimrc/autocmds.vim | file | annotate | diff | comparison | revisions |
--- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -7,7 +7,8 @@ require("config.lsp") require("config.autocmds") require("config.commands") -require("local.tig").setup() +require("local.tig") +require("local.encrypted") -- >> Builtin g.netrw_altfile = 1
--- a/.config/nvim/lua/config/util.lua +++ b/.config/nvim/lua/config/util.lua @@ -1,5 +1,8 @@ local M = {} +local api = vim.api +local fn = vim.fn + function M.calias(abbrev, expand) vim.cmd.cnoreabbrev( string.format( @@ -15,12 +18,14 @@ end function M.autocmd(group, event, pattern, opts) if type(opts) == "function" then local func = opts - opts = { callback = function (_) - func() - end } + opts = { + callback = function(_) + func() + end, + } end - vim.api.nvim_create_autocmd( + api.nvim_create_autocmd( event, vim.tbl_extend("keep", opts, { group = group, @@ -29,4 +34,14 @@ function M.autocmd(group, event, pattern ) end +function M.safe_filter_file(cmd) + local errorfile = fn.tempname() + vim.cmd([[silent %!]] .. cmd .. [[ 2>]] .. fn.shellescape(errorfile)) + if vim.v.shell_error ~= 0 then + vim.cmd("silent undo") + api.nvim_err_write(io.open(errorfile):read("*a")) + end + fn.delete(errorfile) +end + return M
new file mode 100644 --- /dev/null +++ b/.config/nvim/lua/local/encrypted.lua @@ -0,0 +1,24 @@ +local util = require("config.util") + +local safe_filter_file = util.safe_filter_file + +local g = vim.api.nvim_create_augroup("encrypted", { clear = true }) +local filepattern = { "*.gpg", "*.gpg.*" } + +local function autocmd(event, opts) + util.autocmd(g, event, filepattern, opts) +end + +autocmd({ "BufReadPre", "FileReadPre" }, { + command = [[ setl noswapfile noundofile nobackup viminfo= ]] +}) + +autocmd("BufReadPost", function(_) + safe_filter_file("gpg -d") +end) + +autocmd("BufWritePre", function(_) + safe_filter_file("gpg -se -a --default-recipient-self") +end) + +autocmd("BufWritePost", { command = [[ silent undo ]] })
--- a/.config/nvim/lua/local/tig.lua +++ b/.config/nvim/lua/local/tig.lua @@ -20,18 +20,12 @@ local function tig(ctx) }) end -local function setup(config) - api.nvim_create_user_command("Tig", tig, { nargs = "*", complete = "file" }) - - api.nvim_create_user_command("TigBlame", function() - tig({ fargs = { "blame", "+" .. fn.line("."), "--", fn.expand("%") } }) - end, {}) +api.nvim_create_user_command("Tig", tig, { nargs = "*", complete = "file" }) - api.nvim_create_user_command("TigLog", function() - tig({ fargs = { "log", "-p", "--", fn.expand("%") } }) - end, {}) -end +api.nvim_create_user_command("TigBlame", function() + tig({ fargs = { "blame", "+" .. fn.line("."), "--", fn.expand("%") } }) +end, {}) -return { - setup = setup, -} +api.nvim_create_user_command("TigLog", function() + tig({ fargs = { "log", "-p", "--", fn.expand("%") } }) +end, {})
deleted file mode 100644 --- a/.config/nvim/plugin/vimrc/autocmds.vim +++ /dev/null @@ -1,10 +0,0 @@ -" https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/ -" hacked to work with vimwiki -augroup encrypted - autocmd! - autocmd BufReadPre,FileReadPre *.gpg,*.gpg.* setl noswapfile noundofile nobackup viminfo= - autocmd BufReadPost *.gpg,*.gpg.* call vimrc#SafeFilterFile('gpg2 -d') - autocmd BufWritePre *.gpg,*.gpg.* call vimrc#SafeFilterFile('gpg2 -se -a --default-recipient-self') - autocmd BufWritePost *.gpg,*.gpg.* :sil undo -augroup END -