@@ -20,12 +20,12 @@ local function replace_visual_selection (buffer, callback)
2020 for i , line in ipairs (lines ) do
2121 if # lines == 1 then
2222 local recased = callback (line :sub (lpos [3 ], rpos [3 ]))
23- lines [i ] = line :sub (buffer , lpos [3 ] - 1 ) .. recased .. line :sub (rpos [3 ] + 1 )
23+ lines [i ] = line :sub (0 , lpos [3 ] - 1 ) .. recased .. line :sub (rpos [3 ] + 1 )
2424 elseif i == 1 then
2525 local recased = callback (line :sub (lpos [3 ]))
26- lines [i ] = line :sub (buffer , lpos [3 ] - 1 ) .. recased
26+ lines [i ] = line :sub (0 , lpos [3 ] - 1 ) .. recased
2727 elseif i == # lines then
28- local recased = callback (line :sub (buffer , rpos [3 ]))
28+ local recased = callback (line :sub (0 , rpos [3 ]))
2929 lines [i ] = recased .. line :sub (rpos [3 ] + 1 )
3030 else
3131 lines [i ] = callback (line )
@@ -43,28 +43,47 @@ local function replace_line_range (buffer, args, callback)
4343 vim .api .nvim_buf_set_lines (buffer , first - 1 , last , true , lines )
4444end
4545
46- local function nvim_decasify (args , preview_ns , preview_buffer )
46+ local function replace_preview_buffer (buffer , args , preview_buf , callback )
47+ local first , last = args .line1 , args .line2
48+ local src_lines = vim .api .nvim_buf_get_lines (buffer , first - 1 , last , false )
49+ local preview_lines = {}
50+ for _ , line in ipairs (src_lines ) do
51+ table.insert (preview_lines , callback (line ))
52+ end
53+ vim .api .nvim_buf_set_lines (preview_buf , 0 , - 1 , false , preview_lines )
54+ end
55+
56+ local function nvim_decasify (args , _ , preview_buf )
4757 local case = args .fargs [1 ] or vim .b .decasify_case or vim .g .decasify_case or nil
4858 local locale = args .fargs [2 ] or vim .b .decasify_locale or vim .g .decasify_locale or nil
4959 local style = args .fargs [3 ] or vim .b .decasify_style or vim .g .decasify_style or nil
5060 local overrides = vim .b .decasify_overrides or vim .g .decasify_overrides or {}
5161 local opts = {
5262 overrides = args .fargs [4 ] and vim .split (args .fargs [4 ], " ," ) or overrides ,
5363 }
54- local buffer = preview_ns and preview_buffer or vim .api .nvim_get_current_buf ()
55- local decase = function (input )
64+ local function decase (input )
5665 return decasify .case (input , case , locale , style , opts )
5766 end
58- -- https://www.petergundel.de/neovim/lua/hack/2023/12/17/get-neovim-mode-when-executing-a-command.html
59- local smark = vim .api .nvim_buf_get_mark (buffer , " <" )[2 ]
60- local emark = vim .api .nvim_buf_get_mark (buffer , " >" )[2 ]
61- if args .count == - 1 or smark > 1000000 or emark > 1000000 then
62- replace_line_range (buffer , args , decase )
67+ local buffer = vim .api .nvim_get_current_buf ()
68+ if not preview_buf then
69+ -- https://www.petergundel.de/neovim/lua/hack/2023/12/17/get-neovim-mode-when-executing-a-command.html
70+ local smark = vim .api .nvim_buf_get_mark (buffer , " <" )[2 ]
71+ local emark = vim .api .nvim_buf_get_mark (buffer , " >" )[2 ]
72+ if args .count == - 1 or smark > 1000000 or emark > 1000000 then
73+ replace_line_range (buffer , args , decase )
74+ else
75+ replace_visual_selection (buffer , decase )
76+ end
6377 else
64- replace_visual_selection (buffer , decase )
78+ replace_preview_buffer (buffer , args , preview_buf , decase )
6579 end
6680end
6781
82+ local function nvim_decasify_preview (args , _ , preview_buf )
83+ nvim_decasify (args , _ , preview_buf )
84+ return 1
85+ end
86+
6887local function nvim_decasify_complete (arg_lead , cmd_line , _ )
6988 local parts = vim .split (cmd_line , " %s+" , { trimempty = true })
7089 table.remove (parts , 1 )
@@ -90,7 +109,7 @@ vim.api.nvim_create_user_command("Decasify", nvim_decasify, {
90109 desc = " Pass lines to decasify for recasing prose" ,
91110 nargs = " *" ,
92111 range = true ,
93- preview = nvim_decasify ,
112+ preview = nvim_decasify_preview ,
94113 complete = nvim_decasify_complete ,
95114})
96115
0 commit comments