Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 1 | " Vim plugin for showing matching parens |
| 2 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 3 | " Last Change: 2014 Jun 17 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 4 | |
| 5 | " Exit quickly when: |
| 6 | " - this plugin was already loaded (or disabled) |
| 7 | " - when 'compatible' is set |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 8 | " - the "CursorMoved" autocmd event is not available. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 9 | if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved") |
| 10 | finish |
| 11 | endif |
| 12 | let g:loaded_matchparen = 1 |
| 13 | |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 14 | if !exists("g:matchparen_timeout") |
| 15 | let g:matchparen_timeout = 300 |
| 16 | endif |
| 17 | if !exists("g:matchparen_insert_timeout") |
| 18 | let g:matchparen_insert_timeout = 60 |
| 19 | endif |
| 20 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 21 | augroup matchparen |
| 22 | " Replace all matchparen autocommands |
Bram Moolenaar | fa2e044 | 2007-08-18 16:21:50 +0000 | [diff] [blame] | 23 | autocmd! CursorMoved,CursorMovedI,WinEnter * call s:Highlight_Matching_Pair() |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 24 | if exists('##TextChanged') |
| 25 | autocmd! TextChanged,TextChangedI * call s:Highlight_Matching_Pair() |
| 26 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 27 | augroup END |
| 28 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 29 | " Skip the rest if it was already done. |
| 30 | if exists("*s:Highlight_Matching_Pair") |
| 31 | finish |
| 32 | endif |
| 33 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 34 | let s:cpo_save = &cpo |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 35 | set cpo-=C |
| 36 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 37 | " The function that is invoked (very often) to define a ":match" highlighting |
| 38 | " for any matching paren. |
| 39 | function! s:Highlight_Matching_Pair() |
| 40 | " Remove any previous match. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 41 | if exists('w:paren_hl_on') && w:paren_hl_on |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 42 | silent! call matchdelete(3) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 43 | let w:paren_hl_on = 0 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 44 | endif |
| 45 | |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 46 | " Avoid that we remove the popup menu. |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 47 | " Return when there are no colors (looks like the cursor jumps). |
| 48 | if pumvisible() || (&t_Co < 8 && !has("gui_running")) |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 49 | return |
| 50 | endif |
| 51 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 52 | " Get the character under the cursor and check if it's in 'matchpairs'. |
| 53 | let c_lnum = line('.') |
| 54 | let c_col = col('.') |
| 55 | let before = 0 |
| 56 | |
| 57 | let c = getline(c_lnum)[c_col - 1] |
Bram Moolenaar | 41e6cd5 | 2006-09-09 11:37:51 +0000 | [diff] [blame] | 58 | let plist = split(&matchpairs, '.\zs[:,]') |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 59 | let i = index(plist, c) |
| 60 | if i < 0 |
| 61 | " not found, in Insert mode try character before the cursor |
| 62 | if c_col > 1 && (mode() == 'i' || mode() == 'R') |
| 63 | let before = 1 |
| 64 | let c = getline(c_lnum)[c_col - 2] |
| 65 | let i = index(plist, c) |
| 66 | endif |
| 67 | if i < 0 |
| 68 | " not found, nothing to do |
| 69 | return |
| 70 | endif |
| 71 | endif |
| 72 | |
| 73 | " Figure out the arguments for searchpairpos(). |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 74 | if i % 2 == 0 |
| 75 | let s_flags = 'nW' |
| 76 | let c2 = plist[i + 1] |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 77 | else |
| 78 | let s_flags = 'nbW' |
| 79 | let c2 = c |
| 80 | let c = plist[i - 1] |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 81 | endif |
| 82 | if c == '[' |
| 83 | let c = '\[' |
| 84 | let c2 = '\]' |
| 85 | endif |
| 86 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 87 | " Find the match. When it was just before the cursor move it there for a |
Bram Moolenaar | c06ac34 | 2006-03-02 22:43:39 +0000 | [diff] [blame] | 88 | " moment. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 89 | if before > 0 |
Bram Moolenaar | 2347f06 | 2006-05-13 12:48:30 +0000 | [diff] [blame] | 90 | let save_cursor = winsaveview() |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 91 | call cursor(c_lnum, c_col - before) |
| 92 | endif |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 93 | |
| 94 | " When not in a string or comment ignore matches inside them. |
Bram Moolenaar | 5302d9e | 2011-09-14 17:55:08 +0200 | [diff] [blame] | 95 | " We match "escape" for special items, such as lispEscapeSpecial. |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 96 | let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' . |
Bram Moolenaar | 81af925 | 2010-12-10 20:35:50 +0100 | [diff] [blame] | 97 | \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"' |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 98 | execute 'if' s_skip '| let s_skip = 0 | endif' |
| 99 | |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 100 | " Limit the search to lines visible in the window. |
| 101 | let stoplinebottom = line('w$') |
| 102 | let stoplinetop = line('w0') |
| 103 | if i % 2 == 0 |
| 104 | let stopline = stoplinebottom |
| 105 | else |
| 106 | let stopline = stoplinetop |
| 107 | endif |
| 108 | |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 109 | " Limit the search time to 300 msec to avoid a hang on very long lines. |
| 110 | " This fails when a timeout is not supported. |
| 111 | if mode() == 'i' || mode() == 'R' |
| 112 | let timeout = exists("b:matchparen_insert_timeout") ? b:matchparen_insert_timeout : g:matchparen_insert_timeout |
| 113 | else |
| 114 | let timeout = exists("b:matchparen_timeout") ? b:matchparen_timeout : g:matchparen_timeout |
| 115 | endif |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 116 | try |
Bram Moolenaar | ad3b366 | 2013-05-17 18:14:19 +0200 | [diff] [blame] | 117 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, timeout) |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 118 | catch /E118/ |
Bram Moolenaar | f2b2e70 | 2008-03-09 15:45:53 +0000 | [diff] [blame] | 119 | " Can't use the timeout, restrict the stopline a bit more to avoid taking |
| 120 | " a long time on closed folds and long lines. |
| 121 | " The "viewable" variables give a range in which we can scroll while |
| 122 | " keeping the cursor at the same position. |
| 123 | " adjustedScrolloff accounts for very large numbers of scrolloff. |
| 124 | let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2]) |
| 125 | let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2]) |
| 126 | let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2]) |
| 127 | " one of these stoplines will be adjusted below, but the current values are |
| 128 | " minimal boundaries within the current window |
| 129 | if i % 2 == 0 |
| 130 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 131 | let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) |
| 132 | let stopline = min([bottom_viewable, byte2line(stopbyte)]) |
| 133 | else |
| 134 | let stopline = min([bottom_viewable, c_lnum + 100]) |
| 135 | endif |
| 136 | let stoplinebottom = stopline |
| 137 | else |
| 138 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 139 | let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) |
| 140 | let stopline = max([top_viewable, byte2line(stopbyte)]) |
| 141 | else |
| 142 | let stopline = max([top_viewable, c_lnum - 100]) |
| 143 | endif |
| 144 | let stoplinetop = stopline |
| 145 | endif |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 146 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
| 147 | endtry |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 148 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 149 | if before > 0 |
Bram Moolenaar | 2347f06 | 2006-05-13 12:48:30 +0000 | [diff] [blame] | 150 | call winrestview(save_cursor) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 151 | endif |
| 152 | |
| 153 | " If a match is found setup match highlighting. |
Bram Moolenaar | ef04586 | 2007-08-02 21:00:50 +0000 | [diff] [blame] | 154 | if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 155 | if exists('*matchaddpos') |
| 156 | call matchaddpos('MatchParen', [[c_lnum, c_col - before], [m_lnum, m_col]], 10, 3) |
| 157 | else |
| 158 | exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . |
| 159 | \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' |
| 160 | endif |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 161 | let w:paren_hl_on = 1 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 162 | endif |
| 163 | endfunction |
| 164 | |
| 165 | " Define commands that will disable and enable the plugin. |
Bram Moolenaar | b341459 | 2014-06-17 17:48:32 +0200 | [diff] [blame] | 166 | command! NoMatchParen windo silent! call matchdelete(3) | unlet! g:loaded_matchparen | |
Bram Moolenaar | fa2e044 | 2007-08-18 16:21:50 +0000 | [diff] [blame] | 167 | \ au! matchparen |
| 168 | command! DoMatchParen runtime plugin/matchparen.vim | windo doau CursorMoved |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 169 | |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 170 | let &cpo = s:cpo_save |
| 171 | unlet s:cpo_save |