Skip to content

Commit a8f5053

Browse files
Merge pull request #45 from joshuadanpeterson/codex/fix-issue-#44
Fix issue #44
2 parents ef567af + 2208198 commit a8f5053

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [v0.7.4](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.7.4) (2025-07-05)
4+
- fix(autocommands): restore column preservation after search (issue #44)
5+
- feat(autocommands): enable preservation for Zen Mode and True Zen events
6+
- docs: clarify column preservation behavior
7+
- test: add coverage for search state handling
8+
39
## [v0.7.3](https://github.com/joshuadanpeterson/typewriter.nvim/tree/v0.7.3) (2025-06-19)
410
- Merge pull request #43 from joshuadanpeterson/codex/fix-issue-#42-based-on-suggestion
511
- test: 🧪 restore filetype in commands test

doc/typewriter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ User commands created:
7070
- TWTop: Move the top of the current code block to the top of the screen
7171
- TWBottom: Move the bottom of the current code block to the bottom of the screen
7272
- TWPreserveColumn: Toggle column preservation mode
73+
Column preservation automatically resumes after search when Typewriter mode remains active.
7374

7475
@usage require("typewriter.autocommands").autocmd_setup()
7576

lua/typewriter/autocommands.lua

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ end
6666

6767
-- Function to handle search completion
6868
local function handle_search_completion()
69-
if vim.fn.mode() == "n" then
70-
set_state(State.NORMAL)
71-
if vim.v.hlsearch == 1 then
72-
local search_pattern = vim.fn.getreg("/")
73-
move_cursor_to_combined_match(search_pattern)
74-
end
75-
end
69+
if vim.fn.mode() == "n" then
70+
if utils.is_typewriter_active() then
71+
set_state(State.PRESERVE_COLUMN)
72+
else
73+
set_state(State.NORMAL)
74+
end
75+
if vim.v.hlsearch == 1 then
76+
local search_pattern = vim.fn.getreg("/")
77+
move_cursor_to_combined_match(search_pattern)
78+
end
79+
end
7680
end
7781

7882
--- Move cursor to the best match found using Treesitter and LSP
@@ -321,13 +325,14 @@ function M.autocmd_setup()
321325
-- ZenMode and True Zen integration (same as before)
322326
-- Autocommands for ZenMode integration
323327
if config.config.enable_with_zen_mode then
324-
vim.api.nvim_create_autocmd("User", {
325-
pattern = "ZenModePre",
326-
callback = function()
327-
commands.enable_typewriter_mode()
328-
end,
329-
desc = "Enable Typewriter mode when entering Zen Mode",
330-
})
328+
vim.api.nvim_create_autocmd("User", {
329+
pattern = "ZenModePre",
330+
callback = function()
331+
commands.enable_typewriter_mode()
332+
set_state(State.PRESERVE_COLUMN)
333+
end,
334+
desc = "Enable Typewriter mode when entering Zen Mode",
335+
})
331336
vim.api.nvim_create_autocmd("User", {
332337
pattern = "ZenModeLeave",
333338
callback = function()
@@ -343,6 +348,7 @@ function M.autocmd_setup()
343348
pattern = "TZWoon",
344349
callback = function()
345350
commands.enable_typewriter_mode()
351+
set_state(State.PRESERVE_COLUMN)
346352
end,
347353
desc = "Enable Typewriter mode when entering True Zen",
348354
})
@@ -363,4 +369,20 @@ function M.autocmd_setup()
363369
})
364370
end
365371

372+
--- Enable column preservation state
373+
function M.enable_column_preservation()
374+
set_state(State.PRESERVE_COLUMN)
375+
end
376+
377+
--- Disable column preservation state
378+
function M.disable_column_preservation()
379+
set_state(State.NORMAL)
380+
end
381+
382+
--- Get the current internal state (for testing)
383+
-- @return number current state
384+
function M.get_state()
385+
return current_state
386+
end
387+
366388
return M

lua/typewriter/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ M.setup = function(user_config)
4343
autocommands.autocmd_setup()
4444
if config.config.start_enabled then
4545
commands.enable_typewriter_mode()
46+
autocommands.enable_column_preservation()
4647
end
4748
require("typewriter.utils").notify("Typewriter.nvim started")
4849
logger.info("Typewriter.nvim started")

tests/autocommands_spec.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,45 @@ describe('typewriter.autocommands scope', function()
1212
assert.is_nil(_G.move_cursor_to_regex_match)
1313
end)
1414
end)
15+
16+
describe('typewriter.autocommands search state', function()
17+
before_each(function()
18+
_G.created_autocmds = {}
19+
vim.api.nvim_create_autocmd = function(event, opts)
20+
table.insert(_G.created_autocmds, { event = event, callback = opts.callback })
21+
end
22+
vim.fn.mode = function() return 'n' end
23+
vim.v = { hlsearch = 1 }
24+
vim.fn.getreg = function() return 'foo' end
25+
vim.api.nvim_get_current_buf = function() return 1 end
26+
vim.api.nvim_buf_get_lines = function() return { 'foo' } end
27+
vim.api.nvim_win_set_cursor = function() end
28+
package.loaded['typewriter.autocommands'] = nil
29+
end)
30+
31+
after_each(function()
32+
_G.created_autocmds = nil
33+
end)
34+
35+
it('restores column preservation after search', function()
36+
local autocmds = require('typewriter.autocommands')
37+
local utils = require('typewriter.utils')
38+
39+
autocmds.autocmd_setup()
40+
41+
local enter_cb, leave_cb
42+
for _, ac in ipairs(_G.created_autocmds) do
43+
if ac.event == 'CmdlineEnter' then enter_cb = ac.callback end
44+
if ac.event == 'CmdlineLeave' then leave_cb = ac.callback end
45+
end
46+
47+
utils.set_typewriter_active(true)
48+
autocmds.enable_column_preservation()
49+
local preserve_state = autocmds.get_state()
50+
51+
enter_cb()
52+
leave_cb()
53+
54+
assert.are.equal(preserve_state, autocmds.get_state())
55+
end)
56+
end)

0 commit comments

Comments
 (0)