@@ -43,13 +43,17 @@ function M.err(msg, schedule)
4343 M ._echo_multiline (" [WinShift.nvim] " .. msg , " ErrorMsg" , schedule )
4444end
4545
46- function M .no_win_event_call (cb )
47- local last = vim .opt .eventignore ._value
48- vim .opt .eventignore = (
49- " WinEnter,WinLeave,WinNew,WinClosed,BufEnter,BufLeave"
50- .. (last ~= " " and " ," .. last or " " )
51- )
52- local ok , err = pcall (cb )
46+ --- Call the function `f`, ignoring most of the window and buffer related
47+ --- events. The function is called in protected mode.
48+ --- @param f function
49+ --- @return boolean success
50+ --- @return any result Return value
51+ function M .no_win_event_call (f )
52+ local last = vim .o .eventignore
53+ vim .opt .eventignore :prepend (
54+ " WinEnter,WinLeave,WinNew,WinClosed,BufWinEnter,BufWinLeave,BufEnter,BufLeave"
55+ )
56+ local ok , err = pcall (f )
5357 vim .opt .eventignore = last
5458 return ok , err
5559end
@@ -259,59 +263,47 @@ function M.pause(msg)
259263 )
260264end
261265
266+ --- @class SetLocalSpec
267+ --- @field method ' "set"' | ' "remove"' | ' "append"' | ' "prepend"' Assignment method. (default : " set" )
268+
269+ --- @class SetLocalListSpec : string []
270+ --- @field opt SetLocalSpec
271+
262272--- HACK: workaround for inconsistent behavior from `vim.opt_local`.
263273--- @see [Neovim issue ](https ://github.com /neovim /neovim /issues /14670)
264274--- @param winids number[] | number Either a list of winids , or a single winid (0 for current window ).
265- --- `opt` fields:
266- --- @tfield method '"set"'|'"remove"'|'"append"'|'"prepend"' Assignment method. (default: "set")
267- --- @overload fun ( winids : number[] | number , option : string , value : string[] | string | boolean , opt ?: any )
268- --- @overload fun ( winids : number[] | number , map : table<string , string[] | string | boolean> , opt ?: table )
269- function M .set_local (winids , x , y , z )
275+ --- @param option_map table<string , SetLocalListSpec | string | boolean>
276+ --- @param opt ? SetLocalSpec
277+ function M .set_local (winids , option_map , opt )
270278 if type (winids ) ~= " table" then
271279 winids = { winids }
272280 end
273281
274- local map , opt
275- if y == nil or type (y ) == " table" then
276- map = x
277- opt = y
278- else
279- map = { [x ] = y }
280- opt = z
281- end
282-
283282 opt = vim .tbl_extend (" keep" , opt or {}, { method = " set" })
284283
285284 local cmd
286- local ok , err = M .no_win_event_call (function ()
287- for _ , id in ipairs (winids ) do
288- api .nvim_win_call (id , function ()
289- for option , value in pairs (map ) do
285+ for _ , id in ipairs (winids ) do
286+ api .nvim_win_call (id , function ()
287+ for option , value in pairs (option_map ) do
288+ if type (value ) == " boolean" then
289+ cmd = string.format (" setl %s%s" , value and " " or " no" , option )
290+ else
291+ --- @type SetLocalSpec
290292 local o = opt
291-
292- if type (value ) == " boolean" then
293- cmd = string.format (" setl %s%s" , value and " " or " no" , option )
294- else
295- if type (value ) == " table" then
296- --- @diagnostic disable-next-line : undefined-field
297- o = vim .tbl_extend (" force" , opt , value .opt or {})
298- value = table.concat (value , " ," )
299- end
300-
301- cmd = M .str_template (
302- setlocal_opr_templates [o .method ],
303- { option = option , value = tostring (value ):gsub (" '" , " ''" ) }
304- )
293+ if type (value ) == " table" then
294+ o = vim .tbl_extend (" force" , opt , value .opt or {})
295+ value = table.concat (value , " ," )
305296 end
306297
307- vim .cmd (cmd )
298+ cmd = M .str_template (
299+ setlocal_opr_templates [o .method ],
300+ { option = option , value = tostring (value ):gsub (" '" , " ''" ) }
301+ )
308302 end
309- end )
310- end
311- end )
312303
313- if not ok then
314- error (err )
304+ vim .cmd (cmd )
305+ end
306+ end )
315307 end
316308end
317309
@@ -322,13 +314,11 @@ function M.unset_local(winids, option)
322314 winids = { winids }
323315 end
324316
325- M .no_win_event_call (function ()
326- for _ , id in ipairs (winids ) do
327- api .nvim_win_call (id , function ()
328- vim .cmd (string.format (" set %s<" , option ))
329- end )
330- end
331- end )
317+ for _ , id in ipairs (winids ) do
318+ api .nvim_win_call (id , function ()
319+ vim .cmd (string.format (" set %s<" , option ))
320+ end )
321+ end
332322end
333323
334324return M
0 commit comments