Skip to content

Commit 83ca8a8

Browse files
author
Max Cook
committed
revins automatically toggled for RTL keymaps
1 parent 80f4a34 commit 83ca8a8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

lua/bidi.lua

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
local M = {}
22

3+
local rtl_keymaps = {
4+
'arabic_utf-8',
5+
'arabic',
6+
'hebrew_cp1255',
7+
'hebrew_iso-8859-7',
8+
'hebrew_utf-8',
9+
'hebrew',
10+
'persian-iranian_utf-8',
11+
'persian',
12+
'thaana',
13+
}
14+
315
-- Handler for buffers with bidi mode enabled
416
M.active_bufs = {}
517

@@ -106,6 +118,10 @@ end
106118

107119
-- Initialize plugin
108120
function M.setup(opts)
121+
-- Set options
122+
vim.o.allowrevins = true
123+
124+
-- Set user options
109125
M.options = vim.tbl_deep_extend('force', default_opts, opts or {})
110126

111127
-- Create autocommands
@@ -126,6 +142,25 @@ function M.setup(opts)
126142
desc = 'Temporarily disable Bidi-Mode when writing buffer contents',
127143
})
128144

145+
-- Automatically enter `revins` depending on language and `rightleft`
146+
-- For `rightleft` buffers, LTR languages are `revins`.
147+
-- For `norightleft` buffers, RTL languages are `revins`.
148+
vim.api.nvim_create_autocmd('OptionSet', {
149+
callback = function(opts)
150+
local buf_base_dir = M.active_bufs[tostring(opts.buf)]
151+
if vim.tbl_contains(rtl_keymaps, vim.v.option_new) then
152+
-- NOTE: `revins` is a global option,
153+
-- so if a local option is wanted,
154+
-- might need to use `InsertCharPre` and check the buffer.
155+
vim.o.revins = true
156+
else
157+
vim.o.revins = false
158+
end
159+
end,
160+
group = M.augroup,
161+
desc = 'Automatically enter `revins` depending on language and `rightleft`',
162+
})
163+
129164
-- Generate user commands
130165
if M.options.create_user_commands then
131166
-- Enable Bidi-Mode in current buffer

0 commit comments

Comments
 (0)