Skip to content

Commit f34a939

Browse files
committed
32: Change customization mechanism to map
This implements surround mapping definition as maps. Related-to: tpope#32
1 parent 3ba4cac commit f34a939

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

import/surround.vim

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@ vim9script
22

33
const PAIRS = "b()B{}r[]a<>"
44

5+
g:surround_maps = g:->get('surround_maps', {})
6+
7+
def UpgradeSurroundMap(d: dict<any>, name: string)
8+
var match = name->matchlist('^surround_(\d+)$')
9+
10+
if match->empty()
11+
return
12+
endif
13+
14+
var [_, value] = name->matchlist('^surround_(\d+)$')
15+
16+
if value->empty()
17+
return
18+
endif
19+
20+
var maps = d->get('surround_maps', {})
21+
var key = value->str2nr()->nr2char()
22+
23+
maps[key] = d->get(name)
24+
25+
d['surround_maps'] = maps
26+
enddef
27+
28+
export def UpgradeSurroundMaps()
29+
for d in [g:, b:]
30+
for k in d->keys()
31+
UpgradeSurroundMap(d, k)
32+
endfor
33+
endfor
34+
enddef
35+
536
export def InputTarget(): string
637
var c = getcharstr()
738
while c =~ '^\d+$'
@@ -52,7 +83,13 @@ def ExtractAfter(str: string): string
5283
enddef
5384

5485
def CustomSurroundings(char: string, d: dict<any>, trim: bool): list<string>
55-
var all = Process(get(d, printf('surround_%d', char2nr(char))))
86+
var key = d->get('surround_maps', {})->get(char)
87+
88+
if key == null
89+
throw 'Invalid surround map ' .. char
90+
endif
91+
92+
var all = Process(key)
5693
var before = ExtractBefore(all)
5794
var after = ExtractAfter(all)
5895

@@ -150,14 +187,12 @@ def Wrap(str: string, char: string, wrapType: string, removed: string, linebreak
150187

151188
var idx = PAIRS->stridx(newchar)
152189

153-
var custom = printf('surround_%d', char2nr(newchar))
154-
155190
if newchar == ' '
156191
before = ''
157192
after = ''
158-
elseif !b:->get(custom)->empty()
193+
elseif !b:->get('surround_maps', {})->get(newchar)->empty()
159194
[before, after] = CustomSurroundings(newchar, b:, false)
160-
elseif !g:->get(custom)->empty()
195+
elseif !g:->get('surround_maps', {})->get(newchar)->empty()
161196
[before, after] = CustomSurroundings(newchar, g:, false)
162197
elseif newchar ==# "p"
163198
before = "\n"
@@ -483,9 +518,7 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
483518
spc = true
484519
endif
485520

486-
var custom = printf('surround_%d', char2nr(char))
487-
488-
if b:->get(custom, g:->get(custom))->empty()
521+
if b:->get('surround_maps', g:->get('surround_maps', {}))->get(char)->empty()
489522
if char == 'a'
490523
char = '>'
491524
elseif char == 'r'
@@ -522,9 +555,9 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
522555

523556
if char == '/'
524557
execute 'normal! ' .. strcount .. "[/\<Plug>(surround-d)" .. strcount .. ']/'
525-
elseif exists(printf('b:surround_%d', char2nr(char)))
558+
elseif !b:->get('surround_maps', {})->get(char)->empty()
526559
[before, after] = DeleteCustom(char, b:, scount)
527-
elseif exists(printf('g:surround_%d', char2nr(char)))
560+
elseif !g:->get('surround_maps', {})->get(char)->empty()
528561
[before, after] = DeleteCustom(char, g:, scount)
529562
elseif char =~# '[[:punct:][:space:]]' && char !~# '[][(){}<>"''`]'
530563
execute 'normal! T' .. char
@@ -553,9 +586,7 @@ export def DoSurround(value: string = null_string, new_value: string = null_stri
553586
var oldline = getline('.')
554587
var oldlnum = line('.')
555588

556-
custom = printf('surround_%d', char2nr(char))
557-
558-
if !b:->get(custom, g:->get(custom))->empty()
589+
if !b:->get('surround_maps', g:->get('surround_maps', {}))->get(char)->empty()
559590
setreg('"', before .. after, "c")
560591
keeper = keeper
561592
->substitute('\v\C^' .. Escape(before) .. '\s=', '', '')

plugin/surround.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ if exists("g:loaded_surround") || &cp || v:version < 700
1010
endif
1111
g:loaded_surround = 1
1212

13+
# g:surround_maps = g:->get('surround_maps', {})
14+
# g:surround_maps['-'] = "<% \r %>"
15+
# g:surround_maps['='] = "<%= \r %>"
16+
# g:surround_maps['l'] = "\\begin{\1environment: \1}\r\\end{\1\1}"
17+
# g:surround_maps['l'] = "\\begin{\1environment: \1}\r\\end{\1\r}.*\r\1}"
18+
# g:surround_maps['d'] = "<div\1id: \r..*\r id=\"&\"\1>\r</div>"
19+
1320
import 'surround.vim' as s9
1421

22+
s9.UpgradeSurroundMaps()
23+
1524
nnoremap <silent> <Plug>(surround-repeat) .
1625
nnoremap <silent> <Plug>(surround-d) d
1726
nnoremap <silent> <Plug>(surround-delete) <ScriptCmd>s9.DoSurround(s9.InputTarget())<CR>
@@ -47,4 +56,10 @@ if !exists("g:surround_no_mappings") || !g:surround_no_mappings
4756
endif
4857
endif
4958

59+
augroup vim9-surround-map-upgrade
60+
autocmd!
61+
62+
autocmd BufEnter * s9.UpgradeSurroundMaps()
63+
augroup END
64+
5065
# vim:set ft=vim sw=2 sts=2 et:

0 commit comments

Comments
 (0)