VimScript tips needed

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

VimScript tips needed

Mark Volkmann-2
I want to write a fairly simple VimScript function, but I'm having trouble getting started.
I'm hoping a kind soul can give me some tips or point me to a similar script to use as an example.
Here's what I want to do:
- move to the beginning of the current line (:normal '^' seems to work)
- find the first occurrence of => on the line
- determine whether that is followed by optional whitespace and {
There's more, but maybe if I can get this far, I can figure out the rest.

--
R. Mark Volkmann
Object Computing, Inc.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Marco-5
> - find the first occurrence of => on the line
> - determine whether that is followed by optional whitespace and {
function FindPattern( pattern )
    let line = getline('.')

    if ( line ) {
        if ( line =~ a:pattern ) {
        # do something
        }
        else {
        # do something else
        }
    }
}
endfun

FindPattern( '=>\\s?{' )


Not tested but I think should give you some directions

--
Marco Arthur @ (M)arco Creatives

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

MarcWeber
In reply to this post by Mark Volkmann-2
:h search() -> there is an option to not change cursor position or such
is an option as well

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Tumbler Terrall
In reply to this post by Marco-5

>     if ( line ) {
>         if ( line =~ a:pattern ) {
>         # do something
>         }
>         else {
>         # do something else
>         }
>     }
> }
> endfun
>
> FindPattern( '=>\\s?{' )

I feel like I'm breaking some rule by posting here without my name being some form of Mark. :)

No offence, but this code is riddled with syntax errors. You're using c like syntax instead of vimscript. Here's a cleaned up version:

function FindPattern( pattern )
   let line = getline('.')

   if ( line =~ a:pattern )
      " do something
   else
      " do something else
   endif
endfunction

"And the regex needs a little bit of work too:

call FindPattern( '=>\s\?{' )

I would probably use '=>\s*{' but either could work depending on the situation.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Nikolay Aleksandrovich Pavlov
2016-08-08 17:01 GMT+03:00 Tumbler Terrall <[hidden email]>:

>
>>     if ( line ) {
>>         if ( line =~ a:pattern ) {
>>         # do something
>>         }
>>         else {
>>         # do something else
>>         }
>>     }
>> }
>> endfun
>>
>> FindPattern( '=>\\s?{' )
>
>
> I feel like I'm breaking some rule by posting here without my name being some form of Mark. :)
>
> No offence, but this code is riddled with syntax errors. You're using c like syntax instead of vimscript. Here's a cleaned up version:
>
> function FindPattern( pattern )
>    let line = getline('.')
>
>    if ( line =~ a:pattern )

Never use `=~`/`!~`, `<`/`>`/`<=`/`>=` and `==`/`!=` for string
comparison, there are more predictable versions with `#` or `?`
appended (i.e. `=~#` in this case, versions with `?` are needed very
rarely). Also it is rather uncommon to use `if ( condition )` in VimL,
usually it is just `if condition`.

>       " do something
>    else
>       " do something else
>    endif
> endfunction
>
> "And the regex needs a little bit of work too:
>
> call FindPattern( '=>\s\?{' )
>
> I would probably use '=>\s*{' but either could work depending on the situation.
>
> --
> --
> 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 [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Arun Easi-2
In reply to this post by MarcWeber
On Mon, Aug 8, 2016 at 12:11 AM, Marc Weber <[hidden email]> wrote:
:h search() -> there is an option to not change cursor position or such
is an option as well


Just to add to the above point, to check if the pattern is in current line, you either do:
let match_line = search('<patt>', 'cnW')
and.. 
if match_line == line('.')
  "Do something
endif

or can even form a complex search pattern, like

let match_line = search('\%'.line('.').'l<patt>', 'c')

..where the cursor is placed at the match if "match_line" is non-zero. "\%<line-num>l" is a special pattern to look for a match on a specific line. There are other variants too, see
:help \%l

Another tip, almost always, you want to use ":normal!" (with a !) inside a function, instead of ":normal". The former would not use mappings (:map) for the characters following it.

--Arun

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Nikolay Aleksandrovich Pavlov
2016-08-08 20:44 GMT+03:00 Arun <[hidden email]>:

> On Mon, Aug 8, 2016 at 12:11 AM, Marc Weber <[hidden email]> wrote:
>>
>> :h search() -> there is an option to not change cursor position or such
>> is an option as well
>>
>
> Just to add to the above point, to check if the pattern is in current line,
> you either do:
>
> let match_line = search('<patt>', 'cnW')

search() is affected by &ignorecase, &smartcase and (!)&magic. So it ought to be

   let match_line = search('\C\m<patt>', 'cnW')

>
> and..
>
> if match_line == line('.')

`==#`, not `==`. Do not use such operators for string comparison.

>   "Do something
> endif
>
>
> or can even form a complex search pattern, like
>
> let match_line = search('\%'.line('.').'l<patt>', 'c')
>
>
> ..where the cursor is placed at the match if "match_line" is non-zero.
> "\%<line-num>l" is a special pattern to look for a match on a specific line.
> There are other variants too, see
>
> :help \%l
>
>
> Another tip, almost always, you want to use ":normal!" (with a !) inside a
> function, instead of ":normal". The former would not use mappings (:map) for
> the characters following it.
>
> --Arun
>
> --
> --
> 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 [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Arun Easi-2
On Mon, Aug 8, 2016 at 11:51 AM, Nikolay Aleksandrovich Pavlov <[hidden email]> wrote:
2016-08-08 20:44 GMT+03:00 Arun <[hidden email]>:
> On Mon, Aug 8, 2016 at 12:11 AM, Marc Weber <[hidden email]> wrote:
>>
>> :h search() -> there is an option to not change cursor position or such
>> is an option as well
>>
>
> Just to add to the above point, to check if the pattern is in current line,
> you either do:
>
> let match_line = search('<patt>', 'cnW')

search() is affected by &ignorecase, &smartcase and (!)&magic. So it ought to be

   let match_line = search('\C\m<patt>', 'cnW')

Good info; should be used depending on the needs.
 

>
> and..
>
> if match_line == line('.')

`==#`, not `==`. Do not use such operators for string comparison.

Here it is an integer comparison, should not matter in this; but a point to be noted.

--Arun
 

>   "Do something
> endif
>
>
> or can even form a complex search pattern, like
>
> let match_line = search('\%'.line('.').'l<patt>', 'c')
>
>
> ..where the cursor is placed at the match if "match_line" is non-zero.
> "\%<line-num>l" is a special pattern to look for a match on a specific line.
> There are other variants too, see
>
> :help \%l
>
>
> Another tip, almost always, you want to use ":normal!" (with a !) inside a
> function, instead of ":normal". The former would not use mappings (:map) for
> the characters following it.
>
> --Arun
>
> --
> --
> 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 [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Nikolay Aleksandrovich Pavlov
2016-08-09 3:40 GMT+03:00 Arun <[hidden email]>:

> On Mon, Aug 8, 2016 at 11:51 AM, Nikolay Aleksandrovich Pavlov
> <[hidden email]> wrote:
>>
>> 2016-08-08 20:44 GMT+03:00 Arun <[hidden email]>:
>> > On Mon, Aug 8, 2016 at 12:11 AM, Marc Weber <[hidden email]> wrote:
>> >>
>> >> :h search() -> there is an option to not change cursor position or such
>> >> is an option as well
>> >>
>> >
>> > Just to add to the above point, to check if the pattern is in current
>> > line,
>> > you either do:
>> >
>> > let match_line = search('<patt>', 'cnW')
>>
>> search() is affected by &ignorecase, &smartcase and (!)&magic. So it ought
>> to be
>>
>>    let match_line = search('\C\m<patt>', 'cnW')
>
>
> Good info; should be used depending on the needs.

No, should be used always unless you know you want to depend on &*case
and &magic. As most such “best practices” it stands on the following
positions:

1. It is easier to write `\C\m` *always* then to waste brainpower on
determining whether `\C` or `\m` is needed.
2. One does not know how code will mutate in the future. So if
`<patt>` does not require `\C` or `\m` now it may appear that they
will be required in the future.
3. Not all contributors know all best practices. If contributor to
your projects sees that you write `search('\C\m-', 'cnW')` even though
searching for a hyphen-minus requires neither `\C` nor `\m` he may
guess that there is some convention involved and write
`search('\C\mfoo*', 'cnW')` which is good because `foo*` requires both
`\C` and `\m`.
4. I do not know whether there are VimL linters that may check these
“best practices”, but if you want to write one it is again easier to
check that pattern starts with `\\[Cc]\\[mMvV]` then to write code
that determines whether following pattern needs `\C` or `\m`.

// BTW, one ignored pattern problem: in `\v` (very-magic) mode spaces,
hyphen-minuses, colons, number signs (hashes, #), control characters
and many others need to be escaped according to the documentation. Not
that they do mean something special now, but help guarantees only
U+0080 and higher, ASCII alphanumeric characters and underscore to
have no special meaning without talking about other characters I
mentioned. So you may see me writing strangely escaped patterns in
`\v` mode.

>
>>
>>
>> >
>> > and..
>> >
>> > if match_line == line('.')
>>
>> `==#`, not `==`. Do not use such operators for string comparison.
>
>
> Here it is an integer comparison, should not matter in this; but a point to
> be noted.

Sorry, mistook with `getline('.')`.

>
> --Arun
>
>>
>>
>> >   "Do something
>> > endif
>> >
>> >
>> > or can even form a complex search pattern, like
>> >
>> > let match_line = search('\%'.line('.').'l<patt>', 'c')
>> >
>> >
>> > ..where the cursor is placed at the match if "match_line" is non-zero.
>> > "\%<line-num>l" is a special pattern to look for a match on a specific
>> > line.
>> > There are other variants too, see
>> >
>> > :help \%l
>> >
>> >
>> > Another tip, almost always, you want to use ":normal!" (with a !) inside
>> > a
>> > function, instead of ":normal". The former would not use mappings (:map)
>> > for
>> > the characters following it.
>> >
>> > --Arun
>> >
>> > --
>> > --
>> > 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 [hidden email].
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> --
>> 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 [hidden email].
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> --
> 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 [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: VimScript tips needed

Mark Volkmann-2
In reply to this post by Mark Volkmann-2
Thanks so much to everyone that provided tips on what I was trying to do! I have it working now and want to share it. This is a plugin for operating on JavaScript arrow functions. See https://github.com/mvolkmann/vim-js-arrow-function.

On Sun, Aug 7, 2016 at 6:34 PM, Mark Volkmann <[hidden email]> wrote:
I want to write a fairly simple VimScript function, but I'm having trouble getting started.
I'm hoping a kind soul can give me some tips or point me to a similar script to use as an example.
Here's what I want to do:
- move to the beginning of the current line (:normal '^' seems to work)
- find the first occurrence of => on the line
- determine whether that is followed by optional whitespace and {
There's more, but maybe if I can get this far, I can figure out the rest.

--
R. Mark Volkmann
Object Computing, Inc.



--
R. Mark Volkmann
Object Computing, Inc.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.