Skip to content

Commit

Permalink
Temporarily disable Bidi-Mode when writing buffer contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Cook committed Mar 31, 2023
1 parent 8cae70b commit 85b77d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on a per-buffer basis.
I recommend using [`rosetta.nvim`](https://github.com/mcookly/rosetta.nvim)
in the meantime.**

## Depedencies
## Dependencies

### Required

Expand Down Expand Up @@ -79,7 +79,7 @@ more or less in the order I plan to add functionality.
- [x] `Bidi-Mode` toggleability ([331de66](https://github.com/mcookly/bidi.nvim/commit/331de66c19937c85c7f704b5f7e836a4d356d0ca))
- [x] `Bidi-Mode` statusline option ([dcba4df](https://github.com/mcookly/bidi.nvim/commit/dcba4dfb430d04da0140cef4ccd391eab1e8c057))
- [x] Manually choose base direction
- [ ] Save files only in logical mode
- [x] Save files only in logical mode
- [ ] Switch to `revins` automatically
- [ ] Paste in properly in `Bidi-Mode`
- [ ] Dynamic padding for RTL paragraphs
Expand Down
26 changes: 20 additions & 6 deletions lua/bidi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,36 @@ end
function M.setup(opts)
M.options = vim.tbl_deep_extend('force', default_opts, opts or {})

-- Create autocommands
M.augroup = vim.api.nvim_create_augroup('Bidi', { clear = true })

-- Temporarily disable Bidi-Mode when writing buffer contents
vim.api.nvim_create_autocmd({ 'BufWritePre', 'BufWritePost' }, {
callback = function(opts)
local buf_base_dir = M.active_bufs[tostring(opts.buf)]
if buf_base_dir ~= nil then
M.buf_disable_bidi(opts.buf)
M.active_bufs[tostring(opts.buf)] = 'w-' .. buf_base_dir
else
M.buf_enable_bidi(opts.buf, M.active_bufs[tostring(opts.buf)]:sub(3))
end
end,
group = M.augroup,
desc = 'Temporarily disable Bidi-Mode when writing buffer contents',
})

-- Generate user commands
if M.options.create_user_commands then
-- Enable Bidi-Mode in current buffer
vim.api.nvim_create_user_command('BidiEnable', function(opts)
local base_dir = opts.fargs[1] or M.options.default_base_direction
M.buf_enable_bidi(0, base_dir)
end,
{ nargs = '?', desc = 'Enable Bidi-Mode in the current buffer' }
)
end, { nargs = '?', desc = 'Enable Bidi-Mode in the current buffer' })

-- Disable Bidi-Mode in current buffer
vim.api.nvim_create_user_command('BidiDisable', function()
M.buf_disable_bidi(0)
end,
{ desc = 'Disable Bidi-Mode in the current buffer' }
)
end, { desc = 'Disable Bidi-Mode in the current buffer' })
end
end

Expand Down

0 comments on commit 85b77d2

Please sign in to comment.