blob: d709c982c2accf995db9c40ad7adf38f8fb89970 [file] [log] [blame]
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001" Vim plugin for showing matching parens
2" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaaref045862007-08-02 21:00:50 +00003" Last Change: 2007 Jul 30
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004
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.
9if exists("g:loaded_matchparen") || &cp || !exists("##CursorMoved")
10 finish
11endif
12let g:loaded_matchparen = 1
13
14augroup matchparen
15 " Replace all matchparen autocommands
16 autocmd! CursorMoved,CursorMovedI * call s:Highlight_Matching_Pair()
17augroup END
18
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019" Skip the rest if it was already done.
20if exists("*s:Highlight_Matching_Pair")
21 finish
22endif
23
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +000024let cpo_save = &cpo
25set cpo-=C
26
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000027" The function that is invoked (very often) to define a ":match" highlighting
28" for any matching paren.
29function! s:Highlight_Matching_Pair()
30 " Remove any previous match.
Bram Moolenaar910f66f2006-04-05 20:41:53 +000031 if exists('w:paren_hl_on') && w:paren_hl_on
Bram Moolenaare1438bb2006-03-01 22:01:55 +000032 3match none
Bram Moolenaar910f66f2006-04-05 20:41:53 +000033 let w:paren_hl_on = 0
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000034 endif
35
Bram Moolenaar36fc5352006-03-04 21:49:37 +000036 " Avoid that we remove the popup menu.
37 if pumvisible()
38 return
39 endif
40
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000041 " 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 Moolenaar41e6cd52006-09-09 11:37:51 +000047 let plist = split(&matchpairs, '.\zs[:,]')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000048 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 Moolenaar910f66f2006-04-05 20:41:53 +000064 " And avoid searching very far (e.g., for closed folds and long lines)
Bram Moolenaaref045862007-08-02 21:00:50 +000065 " The "viewable" variables give a range in which we can scroll while keeping
66 " the cursor at the same position
67 " adjustedScrolloff accounts for very large numbers of scrolloff
68 let adjustedScrolloff = min([&scrolloff, (line('w$') - line('w0')) / 2])
69 let bottom_viewable = min([line('$'), c_lnum + &lines - adjustedScrolloff - 2])
70 let top_viewable = max([1, c_lnum-&lines+adjustedScrolloff + 2])
71 " one of these stoplines will be adjusted below, but the current values are
72 " minimal boundaries within the current window
73 let stoplinebottom = line('w$')
74 let stoplinetop = line('w0')
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000075 if i % 2 == 0
76 let s_flags = 'nW'
77 let c2 = plist[i + 1]
Bram Moolenaar910f66f2006-04-05 20:41:53 +000078 if has("byte_offset") && has("syntax_items") && &smc > 0
79 let stopbyte = min([line2byte("$"), line2byte(".") + col(".") + &smc * 2])
Bram Moolenaaref045862007-08-02 21:00:50 +000080 let stopline = min([bottom_viewable, byte2line(stopbyte)])
Bram Moolenaar910f66f2006-04-05 20:41:53 +000081 else
Bram Moolenaaref045862007-08-02 21:00:50 +000082 let stopline = min([bottom_viewable, c_lnum + 100])
Bram Moolenaare2f98b92006-03-29 21:18:24 +000083 endif
Bram Moolenaaref045862007-08-02 21:00:50 +000084 let stoplinebottom = stopline
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000085 else
86 let s_flags = 'nbW'
87 let c2 = c
88 let c = plist[i - 1]
Bram Moolenaar910f66f2006-04-05 20:41:53 +000089 if has("byte_offset") && has("syntax_items") && &smc > 0
90 let stopbyte = max([1, line2byte(".") + col(".") - &smc * 2])
Bram Moolenaaref045862007-08-02 21:00:50 +000091 let stopline = max([top_viewable, byte2line(stopbyte)])
Bram Moolenaar910f66f2006-04-05 20:41:53 +000092 else
Bram Moolenaaref045862007-08-02 21:00:50 +000093 let stopline = max([top_viewable, c_lnum - 100])
Bram Moolenaare2f98b92006-03-29 21:18:24 +000094 endif
Bram Moolenaaref045862007-08-02 21:00:50 +000095 let stoplinetop = stopline
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000096 endif
97 if c == '['
98 let c = '\['
99 let c2 = '\]'
100 endif
101
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000102 " Find the match. When it was just before the cursor move it there for a
Bram Moolenaarc06ac342006-03-02 22:43:39 +0000103 " moment.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000104 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +0000105 let save_cursor = winsaveview()
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000106 call cursor(c_lnum, c_col - before)
107 endif
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000108
109 " When not in a string or comment ignore matches inside them.
110 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
Bram Moolenaar8ff38002006-08-08 16:07:03 +0000111 \ '=~? "string\\|character\\|singlequote\\|comment"'
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000112 execute 'if' s_skip '| let s_skip = 0 | endif'
113
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000114 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline)
Bram Moolenaar25e2c9e2006-04-27 21:40:34 +0000115
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000116 if before > 0
Bram Moolenaar2347f062006-05-13 12:48:30 +0000117 call winrestview(save_cursor)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000118 endif
119
120 " If a match is found setup match highlighting.
Bram Moolenaaref045862007-08-02 21:00:50 +0000121 if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000122 exe '3match MatchParen /\(\%' . c_lnum . 'l\%' . (c_col - before) .
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000123 \ 'c\)\|\(\%' . m_lnum . 'l\%' . m_col . 'c\)/'
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000124 let w:paren_hl_on = 1
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000125 endif
126endfunction
127
128" Define commands that will disable and enable the plugin.
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000129command! NoMatchParen 3match none | unlet! g:loaded_matchparen | au! matchparen
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000130command! DoMatchParen runtime plugin/matchparen.vim | doau CursorMoved
Bram Moolenaar3b1ddfe2006-03-14 22:55:34 +0000131
132let &cpo = cpo_save