0

I have the following script variable and a mapping defined as follows:

let s:tblgen_files = '-G \"\\.td$\"'
nnoremap <leader>st :call <SID>PrjAck(expand('<cword>'), s:tblgen_files, "-w -Q", proj_root)<cr>

When the mapping is execute I get a following error: enter image description here

What am I doing wrong?

  • Does this need to be a script variable? Would a global not work just as well? Maybe a buffer local would work if you need to have a value for only certain files – Peter Rincker Oct 13 '15 at 22:05
  • @PeterRincker Unfortunately it does need to be a script variable. – flashburn Oct 13 '15 at 22:06
  • You have stated it needs to be a script local variable but you have not given a reason. I would imagine this parameter should go way and be handled inside of s:PrjAck function. I see no advantage of passing it around as a parameter. Especially since, script variable is rather tricky for a user to modify with out some interface. – Peter Rincker Oct 13 '15 at 22:11
  • 1
    @OP Since when does a function argument need to have a specific scope? :) – VanLaser Oct 13 '15 at 22:17
  • 1
    ag / ack are external programs. CtrlSF is a Vim plugin than can use them, or can work without them. It doesn't solve your problem as you state it above; it does however do something that you seem to want to do with your plugin: search for the word under cursor with ag, or ack. You can find more about it reading the docs. Or you can ignore it, and my tangent. – Sato Katsura Oct 14 '15 at 18:14
2

Short answer

This is (probably) the wrong way to go about this.

Long answer

Without knowing exactly scenario it is hard to suggest better alternatives. In general script local variables are more of a way to protect and encapsulate variables. Having them in your mapping just seems wrong as your mappings do not change.

Below are some general solutions:

Hard coding

If your script local variable never changes and s:ProjectAck is always being called with s:tblgen_files then equivalent to a hard coded value so remove it from the function call.

Global variable

If you script local variable changes and s:ProjectAck is always being called with s:tblgen_files then this is equivalent to a global variable. Still probably best to put it in the function.

Default parameter

s:ProjectAck is being called from many places and not always using s:tblgen_files. This is a sign of a default value so use a different mechanism. See :h a:0

function! s:example(...)
  let param = a:0 ? a:1 : 'default value'
endfunction

Buffer local variable

This is the odd case out because this is not possible with a script local variable, however it is worth mentioning as buffer local variables are often used a way of mimicking a "project local variables".

You have made it this far: How to use script local variables in mappings

Simple, don't. Use another script local function.

function! s:files()
  return s:tblgen_files
endfunction

nnoremap <leader>st :call <SID>PrjAck(expand('<cword>'), <SID>files(), "-w -Q", proj_root)<cr>

Conclusion

I would suggest you figure out your needs and choose an alternative to using a script local variable inside your mapping.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.