Monday, August 8, 2016

Re: VimScript tips needed

2016-08-09 3:40 GMT+03:00 Arun <aemails@gmail.com>:
> On Mon, Aug 8, 2016 at 11:51 AM, Nikolay Aleksandrovich Pavlov
> <zyx.vim@gmail.com> wrote:
>>
>> 2016-08-08 20:44 GMT+03:00 Arun <aemails@gmail.com>:
>> > On Mon, Aug 8, 2016 at 12:11 AM, Marc Weber <marco-oweber@gmx.de> 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 vim_use+unsubscribe@googlegroups.com.
>> > 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 vim_use+unsubscribe@googlegroups.com.
>> 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 vim_use+unsubscribe@googlegroups.com.
> 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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment