Changeset 320

Show
Ignore:
Timestamp:
10/01/07 04:55:35 (6 years ago)
Author:
kana
Message:

lang/vim/surround:
* Support user-defined objects as a surrounding target.

Location:
lang/vim/surround/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/vim/surround/trunk/doc/surround.txt

    • Property svn:keywords set to Id
    r318 r320  
    33Author:  Tim Pope <vimNOSPAM@tpope.info>        *surround-author* 
    44License: Same terms as Vim itself (see |license|) 
     5 
     6NOTE: This is a modified version of surround.vim.  See |surround-changelog| 
     7for the detail of the difference from the original version. 
    58 
    69This plugin is only available if 'compatible' is not set. 
     
    222225It would be nice if |.| would work to repeat an operation. 
    223226 
     227 
     228 
     229 
     230CHANGELOG                                       *surround-changelog* 
     231 
     2322007-10-01T00:25:20+09:00       kana    <http://nicht.s8.xrea.com/> 
     233 
     234        - Support user-defined surrounding objects (|surround-customizing|) as 
     235          |surround-targets|.  The original version (1.26) does not support 
     236          this feature. 
     237 
     238          BUGS: [count] is not supported yet. 
     239 
     240 
     241 
     242 
    224243 vim:tw=78:ts=8:ft=help:norl: 
  • lang/vim/surround/trunk/plugin/surround.vim

    • Property svn:keywords set to Id
    r318 r320  
    22" Author:       Tim Pope <vimNOSPAM@tpope.info> 
    33" GetLatestVimScripts: 1697 1 :AutoInstall: surround.vim 
    4 " $Id: surround.vim,v 1.26 2007-07-31 14:20:47 tpope Exp $ 
     4" $Id$ 
    55" 
    66" See surround.txt for help.  This can be accessed by doing 
     
    173173    endif 
    174174    let idx = stridx(pairs,newchar) 
     175    let user_defined_object = s:get_user_defined_object(newchar) 
    175176    if newchar == ' ' 
    176177        let before = '' 
    177178        let after  = '' 
    178     elseif exists("b:surround_".char2nr(newchar)) 
    179         let all    = s:process(b:surround_{char2nr(newchar)}) 
    180         let before = s:extractbefore(all) 
    181         let after  =  s:extractafter(all) 
    182     elseif exists("g:surround_".char2nr(newchar)) 
    183         let all    = s:process(g:surround_{char2nr(newchar)}) 
     179    elseif len(user_defined_object) 
     180        let all    = s:process(user_defined_object) 
    184181        let before = s:extractbefore(all) 
    185182        let after  =  s:extractafter(all) 
     
    389386endfunction " }}}1 
    390387 
    391 function! s:dosurround(...) " {{{1 
     388function! s:dosurround(...) "{{{1 
     389    " ([target-surrounding-object-char, [new-surrounding-object-char]]) 
     390    " adjust arguments  "{{{2 
    392391    let scount = v:count1 
    393392    let char = (a:0 ? a:1 : s:inputtarget()) 
     
    414413        endif 
    415414    endif 
     415 
     416    " save and initialize some values  "{{{2 
    416417    let cb_save = &clipboard 
    417418    set clipboard-=unnamed 
     
    420421    let otype = getregtype('"') 
    421422    call setreg('"',"") 
     423 
     424    " move the target text range into @@, then delete surroudings  "{{{2 
    422425    let strcount = (scount == 1 ? "" : scount) 
    423     if char == '/' 
     426    let user_defined_object = s:get_user_defined_object(char) 
     427    if len(user_defined_object)  " FIXME: [count] is not supported yet 
     428        let all = s:process(user_defined_object) 
     429        let before = s:extractbefore(all) 
     430        let after = s:extractafter(all) 
     431        call s:search_literally(before, 'bcW') 
     432        normal! v 
     433        call s:search_literally(after, 'ceW') 
     434        normal! d 
     435    elseif char == '/' 
    424436        exe 'norm '.strcount.'[/d'.strcount.']/' 
    425437    else 
     
    436448    let oldline = getline('.') 
    437449    let oldlnum = line('.') 
    438     if char ==# "p" 
     450    if len(user_defined_object) 
     451        call setreg('"', before.after, '') 
     452        let keeper = keeper[len(before):] 
     453        let keeper = keeper[:-(len(after)+1)] 
     454    elseif char ==# "p" 
    439455        "let append = matchstr(keeper,'\n*\%$') 
    440456        "let keeper = substitute(keeper,'\n*\%$','','') 
     
    482498        let pcmd = "p" 
    483499    endif 
     500 
     501    " surround @@ new objects  "{{{2 
    484502    call setreg('"',keeper,regtype) 
    485503    if newchar != "" 
    486504        call s:wrapreg('"',newchar) 
    487505    endif 
     506 
     507    " put the result into the original position, then reindent  "{{{2 
    488508    silent exe 'norm! ""'.pcmd.'`[' 
    489509    if removed =~ '\n' || okeeper =~ '\n' || getreg('"') =~ '\n' 
     
    494514        silent norm! cc 
    495515    endif 
     516 
     517    " restore the original value, set some values for later use  "{{{2 
    496518    call setreg('"',removed,regtype) 
    497519    let s:lastdel = removed 
     
    583605    endif 
    584606endfunction " }}}1 
     607  
     608" Misc. functions  "{{{1 
     609function! s:search_literally(pattern, flags) 
     610    return search(s:literalize_pattern(a:pattern), a:flags) 
     611endfunction 
     612 
     613function! s:literalize_pattern(pattern) 
     614    return '\V'.substitute(a:pattern, '\', '\\', 'g') 
     615endfunction 
     616 
     617function! s:get_user_defined_object(char) 
     618    if exists("b:surround_".char2nr(a:char)) 
     619        return b:surround_{char2nr(a:char)} 
     620    elseif exists("g:surround_".char2nr(a:char)) 
     621        return g:surround_{char2nr(a:char)} 
     622    else 
     623        return '' 
     624    endif 
     625endfunction 
     626"}}}1 
    585627 
    586628nnoremap <silent> <Plug>Dsurround  :<C-U>call <SID>dosurround(<SID>inputtarget())<CR>