preservim / vim-indent-guides

A Vim plugin for visually displaying indent levels in code
MIT License
2.62k stars 162 forks source link

Replace :redir with execute() equivalent #134

Closed noscript closed 3 years ago

noscript commented 6 years ago

Using lambdas with execute() will produce an error if any of the subsequent code uses :redir. According to Vim docs (:h execute()):

It is not possible to use :redir anywhere in {command}.

In my case I have a mapping that causes indent guides to refresh. This PR replaces use of :redir with execute() equivalent.

How to reproduce Long version:

:IndentGuidesEnable
:edit dummy.vim
:split
:call timer_start(100, { -> execute('wincmd w') })

Short version:

:edit dummy.vim
:call timer_start(100, { -> execute('IndentGuidesEnable') })

Vim output:

Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    1:
E930: Cannot use :redir inside execute()
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    3:
E930: Cannot use :redir inside execute()
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    5:
E121: Undefined variable: l:output
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    5:
E116: Invalid arguments for function substitute(l:output, "\n", "", "")
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    5:
E15: Invalid expression: substitute(l:output, "\n", "", "")
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    6:
E121: Undefined variable: l:output
Error detected while processing function indent_guides#process_autocmds[2]..indent_guides#enable[8]..indent
_guides#init_script_vars[7]..indent_guides#capture_highlight:
line    6:
E15: Invalid expression: l:output
mattn commented 4 years ago

I got same. Since redir cannot be nested, the only solution to fix this problem is to merge this pull-request.

image

mattn commented 4 years ago

If you worry about conpatibility issue:

diff --git a/autoload/indent_guides.vim b/autoload/indent_guides.vim
index 482d0ad..001d4d1 100644
--- a/autoload/indent_guides.vim
+++ b/autoload/indent_guides.vim
@@ -246,9 +246,13 @@ endfunction
 " Returns: 'Normal xxx guifg=#323232 guibg=#ffffff'
 "
 function! indent_guides#capture_highlight(group_name)
-  redir => l:output
-  exe "silent hi " . a:group_name
-  redir END
+  if exists("*execute")
+    let l:output = execute("silent hi " . a:group_name)
+  else
+    redir => l:output
+    exe "silent hi " . a:group_name
+    redir END
+  endif
   let l:output = substitute(l:output, "\n", "", "")
   return l:output
tmm commented 4 years ago

I have this problem too. Would love for this PR to be considered!

gou4shi1 commented 1 year ago

@alerque FYI, seems that this PR is still useful.

alerque commented 1 year ago

Thanks for the tip @gou4shi1, I fired up a new PR based on this since the original poster here seems to have deleted their fork. I'd appreciate any review comments there.

alerque commented 1 year ago

I know it's been more than a month of Mondays since this fix was proposed, but it's now available on master if anybody still cares, @tmm @mattn — and thanks for the contribution @noscript.