Thursday, January 7, 2016

Re: vim function problem w/numeric arguments

On Wednesday, January 6, 2016 at 7:55:59 AM UTC-5, Tony Mechelynck wrote:
> On Tue, Jan 5, 2016 at 7:28 PM, JomarBueyes <jomarbueyes at very warm mail dot com> wrote:
> > Hi,
> >
> > I'm having this problem: I define a function as
> >
> > function SetTabs(w)
> > set sw=a:w
> > set tabstop=a:w
> > ( some other settings )
> > endfunction
> >
> > When I try to use the function as:
> >
> > call SetTabs(4)
> >
> > I get an error on the first line:
> > Number required after =: sw=a:w
> >
> > I tried coercing a:w into a number 'width' that I then try to use to
> > set sw=width
> > These are some of the ways I tried that did not work:
> > let width = str2nr(a:w)
> > let width = str2nr(a:w)*1
> > let width = str2nr(a:w)*1.0
> > also tried dividing instead of multiplying and adding or subtracting 0 or 0.0.
> >
> > I tried to convert in two steps such as:
> > let width = str2nr(a:w)+1
> > let width -= 1
> > I tried changing 1 to 1.0, I tried first subtracting, then adding.
> >
> > Neither attempt has worked.
> >
> > Any pointer to what I'm doing wrong will be greatly appreciated.
> > (I'm using vim 4.3 on either bash or tcsh under Mac OS 10.7.5).
> >
> > Thank you in advance,
> >
> > Jomar
> >
> > PS: My idea for this function is to quickly change sw and tabstop when I'm editing files from colleagues, some of which use sw=2, some sw=3, some sw=4.
>
>
> What you're doing wrong is that you are using the _name_ of a variable
> as argument to the :set command; it expects the _value_ to be given
> the option.
>
> You should use either
> let &sw = a:w
> or
> exe "set sw=" . a:w
>
> See
> :help :let-&
> :help :execute
>
> Best regards,
> Tony.

Hi Tony,

Thank you for your response. Your suggestion did work.

Jomar

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: