Skip to content

Commit

Permalink
Add option to choose base direction when calling :BidiEnable
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Cook committed Mar 31, 2023
1 parent 30dd619 commit 5b16429
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ in the meantime.**

### Toggle `Bidi-Mode`

Use `:BidiEnable<cr>` to enable `Bidi-Mode`
and `:BidiDisable<cr>` to disable `Bidi-Mode`.
**Note: I have yet to add the ability to choose a base direction.
Currently, these commands default to `ML` base direction.**
Use `:BidiEnable <base direction>` to enable `Bidi-Mode`.
The base direction is case insensitive.

```vim
" Example: Enable RTL Bidi-Mode
:BidiEnable RL
" Or
:BidiEnable rl
```

If no base direction is supplied (`:BidiEnable`),
Bidi-Mode will activate using the default base direction.

Use `:BidiDisable` to disable `Bidi-Mode`.

### Statusline Indicator

Expand Down Expand Up @@ -67,8 +78,8 @@ more or less in the order I plan to add functionality.
- [x] GNU FriBidi piping ([0c5861a](https://github.com/mcookly/bidi.nvim/commit/0c5861ace3e6e807c5ce8300f63572d50318c154))
- [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
- [ ] Manually choose base direction
- [ ] Switch to `revins` automatically
- [ ] Paste in properly in `Bidi-Mode`
- [ ] Dynamic padding for RTL paragraphs
Expand Down
30 changes: 19 additions & 11 deletions lua/bidi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ local default_opts = {

-- >>> Helper Functions >>>
local function notify(level, msg)
vim.notify(string.format([[BiDi (%s): %s]], level, msg), vim.log.levels[level])
vim.notify(
string.format([[BiDi (%s): %s]], level, msg),
vim.log.levels[level]
)
end
-- <<< Helper Functions <<<

Expand All @@ -23,7 +26,9 @@ end
-- @treturn table Lines run through FriBidi
function M.fribidi(lines, base_dir, args)
-- Sanitize incoming lines
lines = vim.tbl_map(function(line) return line:gsub([[']], [['\'']]) end, lines)
lines = vim.tbl_map(function(line)
return line:gsub([[']], [['\'']])
end, lines)

-- Append `\n` to the end of lines
lines = table.concat(lines, [[\n]])
Expand All @@ -33,13 +38,13 @@ function M.fribidi(lines, base_dir, args)

-- Format base_dir
local fmt_base_dir = ''
if base_dir:match('ML') then
if base_dir:upper():match('ML') then
fmt_base_dir = 'wltr'
elseif base_dir:match('MR') then
elseif base_dir:upper():match('MR') then
fmt_base_dir = 'wrtl'
elseif base_dir:match('LR') then
elseif base_dir:upper():match('LR') then
fmt_base_dir = 'ltr'
elseif base_dir:match('RL') then
elseif base_dir:upper():match('RL') then
fmt_base_dir = 'rtl'
else
notify('ERROR', base_dir)
Expand All @@ -65,7 +70,7 @@ function M.buf_enable_bidi(base_dir)
local buf_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
buf_lines = M.fribidi(buf_lines, base_dir, {})
vim.api.nvim_buf_set_lines(0, 0, -1, false, buf_lines)
M.active_bufs[tostring(bufnr)] = base_dir
M.active_bufs[tostring(bufnr)] = base_dir:upper()
else
notify('ERROR', 'Bidi-Mode already enabled.')
return
Expand All @@ -88,7 +93,7 @@ function M.buf_disable_bidi()
end

-- Get Bidi-Mode status for buffer via <bufnr>
-- @param int The buffer number
-- @param int The buffer number
-- NOTE: This is currently a function
-- in case I implement more procedures down the road.
function M.buf_get_bidi_mode(bufnr)
Expand All @@ -102,12 +107,15 @@ function M.setup(opts)
-- Generate user commands
if M.options.create_user_commands then
-- Enable Bidi-Mode
vim.api.nvim_create_user_command('BidiEnable', function()
M.buf_enable_bidi('ML') end, {})
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(base_dir)
end, { nargs = '?' })

-- Disable Bidi-Mode
vim.api.nvim_create_user_command('BidiDisable', function()
M.buf_disable_bidi() end, {})
M.buf_disable_bidi()
end, {})
end
end

Expand Down

0 comments on commit 5b16429

Please sign in to comment.