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 | 8bc7847 | 2007-05-06 12:26:25 +0000 | [diff] [blame] | 3 | " Last Change: 2006 Oct 12 |
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 |
| 8 | " - the "CursorMoved" autocmd event is not availble. |
| 9 | if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved") |
| 10 | finish |
| 11 | endif |
| 12 | let g:loaded_matchparen = 1 |
| 13 | |
| 14 | augroup matchparen |
| 15 | " Replace all matchparen autocommands |
| 16 | autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair() |
| 17 | augroup END |
| 18 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 19 | " Skip the rest if it was already done. |
| 20 | if exists("*s:Highlight_Matching_Pair") |
| 21 | finish |
| 22 | endif |
| 23 | |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 24 | let cpo_save = &cpo |
| 25 | set cpo-=C |
| 26 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 27 | " The function that is invoked (very often) to define a ":match" highlighting |
| 28 | " for any matching paren. |
| 29 | function! s:Highlight_Matching_Pair() |
| 30 | " Remove any previous match. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 31 | if exists('w:paren_hl_on') && w:paren_hl_on |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 32 | 3match none |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 33 | let w:paren_hl_on = 0 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 34 | endif |
| 35 | |
Bram Moolenaar | 36fc535 | 2006-03-04 21:49:37 +0000 | [diff] [blame] | 36 | " Avoid that we remove the popup menu. |
| 37 | if pumvisible() |
| 38 | return |
| 39 | endif |
| 40 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 41 | " Get the character under the cursor and check if it's in 'matchpairs'. |
| 42 | let c_lnum = line('.') |
| 43 | let c_col = col('.') |
| 44 | let before = 0 |
| 45 | |
| 46 | let c = getline(c_lnum)[c_col - 1] |
Bram Moolenaar | 41e6cd5 | 2006-09-09 11:37:51 +0000 | [diff] [blame] | 47 | let plist = split(&matchpairs, '.\zs[:,]') |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 48 | let i = index(plist, c) |
| 49 | if i < 0 |
| 50 | " not found, in Insert mode try character before the cursor |
| 51 | if c_col > 1 && (mode() == 'i' || mode() == 'R') |
| 52 | let before = 1 |
| 53 | let c = getline(c_lnum)[c_col - 2] |
| 54 | let i = index(plist, c) |
| 55 | endif |
| 56 | if i < 0 |
| 57 | " not found, nothing to do |
| 58 | return |
| 59 | endif |
| 60 | endif |
| 61 | |
| 62 | " Figure out the arguments for searchpairpos(). |
| 63 | " Restrict the search to visible lines with "stopline". |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 64 | " And avoid searching very far (e.g., for closed folds and long lines) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 65 | if i % 2 == 0 |
| 66 | let s_flags = 'nW' |
| 67 | let c2 = plist[i + 1] |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 68 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 69 | let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2]) |
| 70 | let stopline = min([line('w$'), byte2line(stopbyte)]) |
| 71 | else |
| 72 | let stopline = min([line('w$'), c_lnum + 100]) |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 73 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 74 | else |
| 75 | let s_flags = 'nbW' |
| 76 | let c2 = c |
| 77 | let c = plist[i - 1] |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 78 | if has("byte_offset") && has("syntax_items") && &smc > 0 |
| 79 | let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2]) |
| 80 | let stopline = max([line('w0'), byte2line(stopbyte)]) |
| 81 | else |
| 82 | let stopline = max([line('w0'), c_lnum - 100]) |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 83 | endif |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 84 | endif |
| 85 | if c == '[' |
| 86 | let c = '\[' |
| 87 | let c2 = '\]' |
| 88 | endif |
| 89 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 90 | " 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] | 91 | " moment. |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 92 | if before > 0 |
Bram Moolenaar | 2347f06 | 2006-05-13 12:48:30 +0000 | [diff] [blame] | 93 | let save_cursor = winsaveview() |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 94 | call cursor(c_lnum, c_col - before) |
| 95 | endif |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 96 | |
| 97 | " When not in a string or comment ignore matches inside them. |
| 98 | let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' . |
Bram Moolenaar | 8ff3800 | 2006-08-08 16:07:03 +0000 | [diff] [blame] | 99 | \ '=~? "string\\|character\\|singlequote\\|comment"' |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 100 | execute 'if' s_skip '| let s_skip = 0 | endif' |
| 101 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 102 | let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline) |
Bram Moolenaar | 25e2c9e | 2006-04-27 21:40:34 +0000 | [diff] [blame] | 103 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 104 | if before > 0 |
Bram Moolenaar | 2347f06 | 2006-05-13 12:48:30 +0000 | [diff] [blame] | 105 | call winrestview(save_cursor) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 106 | endif |
| 107 | |
| 108 | " If a match is found setup match highlighting. |
| 109 | if m_lnum > 0 && m_lnum >= line('w0') && m_lnum <= line('w$') |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 110 | exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) . |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 111 | \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/' |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 112 | let w:paren_hl_on = 1 |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 113 | endif |
| 114 | endfunction |
| 115 | |
| 116 | " Define commands that will disable and enable the plugin. |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 117 | command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 118 | command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved |
Bram Moolenaar | 3b1ddfe | 2006-03-14 22:55:34 +0000 | [diff] [blame] | 119 | |
| 120 | let &cpo = cpo_save |