Skip to content

Commit d769b39

Browse files
jiangmiaovim-scripts
authored andcommitted
Version 1.2.9: Supports buffer level pairs setting.
1 parent 06ef259 commit d769b39

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ Options
154154

155155
Default: {'(':')', '[':']', '{':'}',"'":"'",'"':'"', '`':'`'}
156156

157+
* b:AutoPairs
158+
159+
Default: g:AutoPairs
160+
161+
Buffer level pairs set.
162+
157163
* g:AutoPairsShortcutToggle
158164

159165
Default: '<M-p>'
@@ -214,6 +220,16 @@ Options
214220

215221
Work with FlyMode, insert the key at the Fly Mode jumped postion
216222

223+
Buffer Level Pairs Setting
224+
--------------------------
225+
226+
Set b:AutoPairs before BufEnter
227+
228+
eg:
229+
230+
" When the filetype is FILETYPE then make AutoPairs only match for parenthesis
231+
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
232+
217233
TroubleShooting
218234
---------------
219235
The script will remap keys ([{'"}]) <BS>,

plugin/auto-pairs.vim

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
" Insert or delete brackets, parens, quotes in pairs.
22
" Maintainer: JiangMiao <jiangfriend@gmail.com>
33
" Contributor: camthompson
4-
" Last Change: 2012-12-18
5-
" Version: 1.2.8
4+
" Last Change: 2013-01-15
5+
" Version: 1.2.9
66
" Homepage: http://www.vim.org/scripts/script.php?script_id=3599
77
" Repository: https://github.com/jiangmiao/auto-pairs
88

@@ -19,9 +19,6 @@ if !exists('g:AutoPairsParens')
1919
let g:AutoPairsParens = {'(':')', '[':']', '{':'}'}
2020
end
2121

22-
let g:AutoExtraPairs = copy(g:AutoPairs)
23-
let g:AutoExtraPairs['<'] = '>'
24-
2522
if !exists('g:AutoPairsMapBS')
2623
let g:AutoPairsMapBS = 1
2724
end
@@ -90,7 +87,7 @@ function! AutoPairsInsert(key)
9087
end
9188

9289
" The key is difference open-pair, then it means only for ) ] } by default
93-
if !has_key(g:AutoPairs, a:key)
90+
if !has_key(b:AutoPairs, a:key)
9491
let b:autopairs_saved_pair = [a:key, getpos('.')]
9592

9693
" Skip the character if current character is the same as input
@@ -116,7 +113,7 @@ function! AutoPairsInsert(key)
116113
endif
117114

118115
" Fly Mode, and the key is closed-pairs, search closed-pair and jump
119-
if g:AutoPairsFlyMode && has_key(g:AutoPairsClosedPairs, a:key)
116+
if g:AutoPairsFlyMode && has_key(b:AutoPairsClosedPairs, a:key)
120117
if search(a:key, 'W')
121118
return "\<Right>"
122119
endif
@@ -127,7 +124,7 @@ function! AutoPairsInsert(key)
127124
end
128125

129126
let open = a:key
130-
let close = g:AutoPairs[open]
127+
let close = b:AutoPairs[open]
131128

132129
if current_char == close && open == close
133130
return "\<Right>"
@@ -169,12 +166,12 @@ function! AutoPairsDelete()
169166
end
170167

171168
" Delete last two spaces in parens, work with MapSpace
172-
if has_key(g:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
169+
if has_key(b:AutoPairs, pprev_char) && prev_char == ' ' && current_char == ' '
173170
return "\<BS>\<DEL>"
174171
endif
175172

176-
if has_key(g:AutoPairs, prev_char)
177-
let close = g:AutoPairs[prev_char]
173+
if has_key(b:AutoPairs, prev_char)
174+
let close = b:AutoPairs[prev_char]
178175
if match(line,'^\s*'.close, col('.')-1) != -1
179176
let space = matchstr(line, '^\s*', col('.')-1)
180177
return "\<BS>". repeat("\<DEL>", len(space)+1)
@@ -234,10 +231,10 @@ function! AutoPairsFastWrap()
234231
let next_char = line[col('.')-1]
235232
end
236233

237-
if has_key(g:AutoPairs, next_char)
234+
if has_key(b:AutoPairs, next_char)
238235
let followed_open_pair = next_char
239236
let inputed_close_pair = current_char
240-
let followed_close_pair = g:AutoPairs[next_char]
237+
let followed_close_pair = b:AutoPairs[next_char]
241238
if followed_close_pair != followed_open_pair
242239
" TODO replace system searchpair to skip string and nested pair.
243240
" eg: (|){"hello}world"} will transform to ({"hello})world"}
@@ -278,7 +275,7 @@ function! AutoPairsReturn()
278275
let prev_char = pline[strlen(pline)-1]
279276
let cmd = ''
280277
let cur_char = line[col('.')-1]
281-
if has_key(g:AutoPairs, prev_char) && g:AutoPairs[prev_char] == cur_char
278+
if has_key(b:AutoPairs, prev_char) && b:AutoPairs[prev_char] == cur_char
282279
if g:AutoPairsCenterLine && winline() * 3 >= winheight(0) * 2
283280
" Use \<BS> instead of \<ESC>cl will cause the placeholder deleted
284281
" incorrect. because <C-O>zz won't leave Normal mode.
@@ -328,14 +325,19 @@ endfunction
328325
function! AutoPairsInit()
329326
let b:autopairs_loaded = 1
330327
let b:autopairs_enabled = 1
328+
let b:AutoPairsClosedPairs = {}
329+
330+
if !exists('b:AutoPairs')
331+
let b:AutoPairs = g:AutoPairs
332+
end
331333

332334
" buffer level map pairs keys
333-
for [open, close] in items(g:AutoPairs)
335+
for [open, close] in items(b:AutoPairs)
334336
call AutoPairsMap(open)
335337
if open != close
336338
call AutoPairsMap(close)
337339
end
338-
let g:AutoPairsClosedPairs[close] = open
340+
let b:AutoPairsClosedPairs[close] = open
339341
endfor
340342

341343
" Still use <buffer> level mapping for <BS> <SPACE>
@@ -377,10 +379,11 @@ function! s:ExpandMap(map)
377379
return map
378380
endfunction
379381

380-
function! AutoPairsForceInit()
382+
function! AutoPairsTryInit()
381383
if exists('b:autopairs_loaded')
382384
return
383385
end
386+
384387
" for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise
385388
"
386389
" vim-endwise doesn't support <Plug>AutoPairsReturn
@@ -426,4 +429,4 @@ inoremap <silent> <SID>AutoPairsReturn <C-R>=AutoPairsReturn()<CR>
426429
imap <script> <Plug>AutoPairsReturn <SID>AutoPairsReturn
427430
428431

429-
au BufEnter * :call AutoPairsForceInit()
432+
au BufEnter * :call AutoPairsTryInit()

0 commit comments

Comments
 (0)