[PATCH]Add an option for matchparen to disable it in insert mode

245 views
Skip to first unread message

Fanhe Fanhed

unread,
Apr 30, 2013, 12:54:46 AM4/30/13
to vim...@googlegroups.com
matchparen plugin in CursorMoveI autocmd is too slow while typing. So I wish disable it in insert mode.



diff -r c61da758a9a2 runtime/doc/pi_paren.txt
--- a/runtime/doc/pi_paren.txt Wed Apr 24 18:34:45 2013 +0200
+++ b/runtime/doc/pi_paren.txt Tue Apr 30 12:45:56 2013 +0800
@@ -15,6 +15,10 @@
 The plugin installs CursorMoved, CursorMovedI and WinEnter autocommands to
 redefine the match highlighting.
 
+You can disable this plugin in insert mode by setting the "matchparen_noinsert"
+variable: >
+ :let matchparen_noinsert = 1
+
  *:NoMatchParen* *:DoMatchParen*
 To disable the plugin after it was loaded use this command: >
 
diff -r c61da758a9a2 runtime/plugin/matchparen.vim
--- a/runtime/plugin/matchparen.vim Wed Apr 24 18:34:45 2013 +0200
+++ b/runtime/plugin/matchparen.vim Tue Apr 30 12:45:56 2013 +0800
@@ -1,6 +1,6 @@
 " Vim plugin for showing matching parens
 " Maintainer:  Bram Moolenaar <Br...@vim.org>
-" Last Change: 2013 Mar 19
+" Last Change: 2013 Apr 30
 
 " Exit quickly when:
 " - this plugin was already loaded (or disabled)
@@ -13,7 +13,13 @@
 
 augroup matchparen
   " Replace all matchparen autocommands
-  autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair()
+  autocmd! CursorMoved,WinEnter * call s:Highlight_Matching_Pair()
+  if exists("g:matchparen_noinsert") && g:matchparen_noinsert
+    autocmd! InsertEnter * call s:Unhighlight_Matching_Pair()
+    autocmd! InsertLeave * call s:Highlight_Matching_Pair()
+  else
+    autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
+  endif
   if exists('##TextChanged')
     autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair()
   endif
@@ -27,6 +33,14 @@
 let s:cpo_save = &cpo
 set cpo-=C
 
+function! s:Unhighlight_Matching_Pair()
+  " Remove any previous match.
+  if exists('w:paren_hl_on') && w:paren_hl_on
+    3match none
+    let w:paren_hl_on = 0
+  endif
+endfunction
+
 " The function that is invoked (very often) to define a ":match" highlighting
 " for any matching paren.
 function! s:Highlight_Matching_Pair()



matchparen.patch

Gary Johnson

unread,
Apr 30, 2013, 1:52:02 AM4/30/13
to vim...@googlegroups.com
On 2013-04-30, Fanhe Fanhed wrote:
> matchparen plugin in CursorMoveI autocmd is too slow while typing.
> So I wish disable it in insert mode.

I don't understand. I don't even notice a delay when I type the
closing parenthesis and then more characters. Typing after the
closing parenthesis seems to cancel the highlighting of the matching
open parenthesis. What sort of delay are you observing?

Regards,
Gary

Fanhe Fanhed

unread,
Apr 30, 2013, 10:16:13 AM4/30/13
to vim...@googlegroups.com
In a 1000+ lines file, typing in the clsoing parenthesis. eg.
func(|)
| means the cursor, go to type.
The cursor will go to blink or shake. So I prefer disable matchparen in insert mode.


2013/4/30 Gary Johnson <gary...@spocom.com>

--
--
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



glts

unread,
Apr 30, 2013, 1:13:41 PM4/30/13
to vim...@googlegroups.com
On Tue, Apr 30, 2013 at 4:16 PM, Fanhe Fanhed <fan...@gmail.com> wrote:
> In a 1000+ lines file, typing in the clsoing parenthesis. eg.
> func(|)
> | means the cursor, go to type.
> The cursor will go to blink or shake. So I prefer disable matchparen in
> insert mode.

I don't think this has anything to do with something being slow ... I've
never had any performance issues with matchparen, and 1000 lines still
isn't a big file.

Isn't the "blinking" or "shaking" coming from your 'showmatch' setting?
You can disable it with ":set noshowmatch".

Gary Johnson

unread,
Apr 30, 2013, 12:03:26 PM4/30/13
to vim...@googlegroups.com
On 2013-04-30, Fanhe Fanhed wrote:
> In a 1000+ lines file, typing in the clsoing parenthesis. eg.
> func(|)
> | means the cursor, go to type.
> The cursor will go to blink or shake. So I prefer disable matchparen in insert
> mode.

That's odd. I created a 2000+ line file, moved the cursor to the
50% point, opened a new line and entered "func()", then moved the
cursor to between the two parentheses as you showed, and started
typing. The opening parenthesis remained steadily highlighted--
there was no flickering or shaking.

I tried it with Vim 7.3.882, both vim and gvim, on a Fedora 14
system.

Regards,
Gary

Fanhe Fanhed

unread,
Apr 30, 2013, 10:25:24 PM4/30/13
to vim...@googlegroups.com
That's odd too. I just forget how to reproduce. I remember it occurs a year ago on my working pc.
I turn off this feature and try to reproduce in furturn.
Now just forget this...

Regards,
Fanhe


2013/5/1 Gary Johnson <gary...@spocom.com>

Regards,
Gary

Fanhe Fanhed

unread,
May 1, 2013, 3:09:57 AM5/1/13
to vim...@googlegroups.com
I just reproduce.
eg: edit tag.c of vim sorce code file, and go to 2000 line number, input the follow
func(((((((((())))))))) , snekjglenlnsekgnel|)

'|' means the cursor. Then go to type.

And type again after run :NoMatchParen.

In fact, the reason is this sentence in matchparen.vim
let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300)

300msec will delay screen refreshing.
For example run this
autocmd CursorMoveI * sleep 100m


After this, the cursor is flickering and shaking when typing.

So, I prefer disable all CursorMoveI autocmd include matchparen plugin, but is not a matter in CursorMove autocmd.

2013/5/1 Fanhe Fanhed <fan...@gmail.com>

Gary Johnson

unread,
May 1, 2013, 11:16:52 AM5/1/13
to vim...@googlegroups.com
On 2013-05-01, Fanhe Fanhed wrote:
> I just reproduce.
> eg: edit tag.c of vim sorce code file, and go to 2000 line number, input the
> follow
> func(((((((((())))))))) , snekjglenlnsekgnel|)
>
> '|' means the cursor. Then go to type.

Highlights the leftmost parenthesis and the cursor smoothly.

> And type again after run :NoMatchParen.

Cursor again highlighted smoothly, but of course no highlighting of
the parenthesis.

> In fact, the reason is this sentence in matchparen.vim
> let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 300)
>
> 300msec will delay screen refreshing.
> For example run this
> autocmd CursorMoveI * sleep 100m
>
>
> After this, the cursor is flickering and shaking when typing.

Now I see the flickering and shaking of the cursor, but only when
NoMatchParen has not been executed.

> So, I prefer disable all CursorMoveI autocmd include matchparen plugin, but is
> not a matter in CursorMove autocmd.

So you're saying that it takes on the order of 100 ms for the
matchparen.vim plugin's s:Highlight_Matching_Pair() function to
execute on your machine?

I can't reproduce the problem without the "sleep 100m", but I can
see that on slower hardware your patch could be useful.

Regards,
Gary

Bram Moolenaar

unread,
May 1, 2013, 4:29:28 PM5/1/13
to Fanhe Fanhed, vim...@googlegroups.com

Fanhe Fanhed wrote:

> matchparen plugin in CursorMoveI autocmd is too slow while typing. So I
> wish disable it in insert mode.

I suppose this is because the syntax highlighting for the language is
slow. Disabling it for all file types is a bit of a blunt instrument
then.

How about allowing the user to specify the filetypes for which
matchparen works in insert mode? While at it, might as well defined the
file types for which it works at all.


--
hundred-and-one symptoms of being an internet addict:
258. When you want to see your girlfriend, you surf to her homepage.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ingo Karkat

unread,
May 2, 2013, 2:56:23 AM5/2/13
to vim...@googlegroups.com
On 01-May-2013 22:29 +0200, Bram Moolenaar wrote:

> Fanhe Fanhed wrote:
>
>> matchparen plugin in CursorMoveI autocmd is too slow while typing. So I
>> wish disable it in insert mode.
>
> I suppose this is because the syntax highlighting for the language is
> slow. Disabling it for all file types is a bit of a blunt instrument
> then.
>
> How about allowing the user to specify the filetypes for which
> matchparen works in insert mode? While at it, might as well defined the
> file types for which it works at all.

Such a configuration is not trivial to implement (think of compound
filetypes like "c.doxygen") and still not the most general. If all this
configurability really has to be there, I'd rather have the script
observe both buffer-local or global variables, like:

#v+
autocmd CursorMovedI *
\ if exists('b:matchparen_no_insertmode') |
\ if b:matchparen_no_insertmode |
\ return |
\ endif
\ elseif exists('g:matchparen_no_insertmode') &&
g:matchparen_no_insertmode |
\ return |
\ endif |
\ ...
#v-

This gets you the filetype-specific disabling (in a little more
roundabout way, by setting b:matchparen_no_insertmode in
~/.vim/after/ftplugin/{filetype}.vim), and you can also disable manually
for the current buffer, for all buffers in a particular subdirectory /
of a certain file size / matching a certain pattern / etc.

I use this idiom in a couple of my plugins, and I've found it more
flexible than a list of configurable filetypes.

-- regards, ingo

Christian Brabandt

unread,
May 2, 2013, 3:11:17 AM5/2/13
to vim...@googlegroups.com
Hi Ingo!

On Do, 02 Mai 2013, Ingo Karkat wrote:

> #v+
> autocmd CursorMovedI *
> \ if exists('b:matchparen_no_insertmode') |
> \ if b:matchparen_no_insertmode |
> \ return |
> \ endif
> \ elseif exists('g:matchparen_no_insertmode') &&
> g:matchparen_no_insertmode |
> \ return |
> \ endif |
> \ ...
> #v-

Excuse me for chiming in, but I find this construct rather ugly and
unreadable. How about this instead:

#v+
autocmd CursorMovedI *
\ if get(b:, 'matchparen_no_insertmode', 0) ||
\ get(g:, 'matchparen_no_insertmode', 0)
\ return |
\ endif |
\ ...
#v-

regards,
Christian
--
Es ist lange her, da� sich die menschliche Phantasie die H�lle
ausgemalt hat, aber erst durch ihre j�ngst erworbenen Fertigkeiten ist
sie in die Lage versetzt worden, ihre einstigen Vorstellungen zu
verwirklichen.
-- Bertrand A. W. Russell

Ingo Karkat

unread,
May 2, 2013, 3:32:09 AM5/2/13
to vim...@googlegroups.com
Hello Christian,

I appreciate your critique. I would move the conditionals into the
s:Highlight_Matching_Pair() function, too; I just wanted to show the
algorithm with the minimal amount of additional context.

Your rewrite doesn't correctly capture the fact that the buffer-local
value takes precedence over the global one; when b:... is 0 and g:... is
1, insert-mode matching should be ON, whereas your code would :return
early. Though it may not make that much sense here, I think the
precedence should be the same as with the built-in global-local options.

-- regards, ingo

Bram Moolenaar

unread,
May 2, 2013, 9:33:23 PM5/2/13
to Ingo Karkat, vim...@googlegroups.com
Agreed, using a buffer-local variable is better than trying to use a
pattern to match filetypes.

--
hundred-and-one symptoms of being an internet addict:
267. You get an extra phone line so you can get phone calls.

Fanhe Fanhed

unread,
May 4, 2013, 8:59:16 AM5/4/13
to vim...@googlegroups.com
Agreed too.
So, Ingo, can you offer a full patch?
And please offer a global way to disable matchparen in insert mode. Because I found I really not need it in insert mode. :)


2013/5/3 Bram Moolenaar <Br...@moolenaar.net>

Ingo Karkat

unread,
May 6, 2013, 8:54:19 AM5/6/13
to vim...@googlegroups.com, Bram Moolenaar
On 04-May-2013 14:59 +0200, Fanhe Fanhed wrote:

> Agreed too.
> So, Ingo, can you offer a full patch?
> And please offer a global way to disable matchparen in insert mode.
> Because I found I really not need it in insert mode. :)
>
>
> 2013/5/3 Bram Moolenaar <Br...@moolenaar.net <mailto:Br...@moolenaar.net>>
After thinking some more about it, I've come to the conclusion that all
attempts so far step around the central issue: The maximum matchparen
delay in insert mode is too large, so that it noticeably affects typing.
We shouldn't work around this, neither by globally disabling in insert
mode, nor through any of the proposed filetype- or buffer-local
configurations.

Instead, let's just reduce the maximum allowable delay in insert mode,
as this also is much simpler to do. The plugin contains a hard-coded
maximum of 300 ms; attached patch reduces that somewhat arbitrarily to a
fifth, hopefully unnoticeable 60 ms. This way, there's no need to
configure anything, and you still reap the benefits of matchparen in
most cases, even for problematic files / file types. This only leaves
out old Vim versions that don't have the {timeout} argument on
searchpairpos(), but as you're the first to raise this issue, it may not
be that widespread to bother.

-- regards, ingo

PS: Do not top-post! Type your reply below the text you are replying to.
matchparen delay.vim.patch

Bram Moolenaar

unread,
May 6, 2013, 11:11:44 PM5/6/13
to Ingo Karkat, vim...@googlegroups.com, Bram Moolenaar
Thanks, makes sense.

So, how do we decide on the timeout? 300 msec is too much, 60 msec is
OK? Perhaps we should make this time configurable.


--
They now pass three KNIGHTS impaled to a tree. With their feet off the
ground, with one lance through the lot of them, they are skewered up
like a barbecue.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

Ingo Karkat

unread,
May 7, 2013, 4:27:03 AM5/7/13
to vim...@googlegroups.com, Bram Moolenaar
If Fanhe agrees with the proposed change, I'd rather not make it more
complex so far. Although I'm usually a big proponent of configurability,
the values seem to be fine for 99,99% of users, and a maximum, corner
case delay of 1/3 s while moving around seems as reasonable as a barely
noticeable 60 ms while typing.

-- regards, ingo

Bram Moolenaar

unread,
May 7, 2013, 11:22:17 PM5/7/13
to Ingo Karkat, vim...@googlegroups.com, Bram Moolenaar
It's not so complicated:

if !exists("g:matchparen_timeout")
let g:matchparen_timeout = 300
endif
if !exists("g:matchparen_insert_timeout")
let g:matchparen_insert_timeout = 60
endif

...


if mode() == 'i' || mode() == 'R'
let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout
else
let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout
endif

--
ZOOT: I'm afraid our life must seem very dull and quiet compared to yours.
We are but eightscore young blondes, all between sixteen and
nineteen-and-a-half, cut off in this castle, with no one to protect us.
Oooh. It is a lonely life ... bathing ... dressing ... undressing ...
making exciting underwear....

Fanhe Fanhed

unread,
May 13, 2013, 10:03:38 AM5/13/13
to vim...@googlegroups.com
Agree with Bram.
Thanks all.


2013/5/8 Bram Moolenaar <Br...@moolenaar.net>
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
Reply all
Reply to author
Forward
0 new messages