Commit: 98c4ff9bb62ea4fd971ad5167738f11ad2ae8565
Parent: eedd8c39300dfa0700fae20ef75e607eb692e8a6
Author: ixtenu
Date: Sat, 11 Jul 2026 13:50:19 -0700
Save and restore cursor positions
Save window cursor positions during encrypt and restore them during
decrypt. This fixes an issue where saving a file causes the cursor
position to jump to the beginning of the file.
Diffstat:
1 file changed, 24 insertions(+), 0 deletions(-)
diff --git a/init.lua b/init.lua
@@ -1,5 +1,26 @@
local gpg = { key = 0 }
+-- replacing the whole buffer clamps all selections to the start of the
+-- file, so remember cursor positions across the encrypt/decrypt cycle
+local positions = setmetatable({}, { __mode = 'k' })
+
+local function save_positions(file)
+ for win in vis:windows() do
+ if win.file == file then
+ positions[win] = win.selection.pos
+ end
+ end
+end
+
+local function restore_positions(file)
+ for win in vis:windows() do
+ if win.file == file and positions[win] ~= nil then
+ win.selection.pos = positions[win]
+ positions[win] = nil
+ end
+ end
+end
+
local function splitext(file)
if file == nil then return nil, nil end
local i = file:reverse():find('%.')
@@ -34,6 +55,7 @@ local function decrypt(file)
file:delete(0, file.size)
file:insert(0, ostr)
file.modified = false
+ restore_positions(file)
return true
end
vis.events.subscribe(vis.events.FILE_OPEN, decrypt)
@@ -48,6 +70,8 @@ local function encrypt(file)
return false
end
+ save_positions(file)
+
local tfn = os.tmpname()
local cmd = "gpg --yes -o " .. tfn .. " -e -r " .. gpg.key
local err = errpipe(file, cmd, true)