Proof of concept substitutions in user replacements

This commit is contained in:
Tim Pope 2007-02-05 17:47:43 +00:00
parent d49993acfa
commit 90295205ad
2 changed files with 83 additions and 7 deletions

View File

@ -138,9 +138,11 @@ specify attributes here and they will be stripped from the closing tag.
End your input by pressing <CR> or >. As an experimental feature, if <C-T>
is used, the tags will appear on lines by themselves.
An experimental replacement of a LaTeX environment is provided on \ and l.
A deprecated replacement of a LaTeX environment is provided on \ and l.
The name of the environment and any arguments will be input from a prompt.
The following shows the resulting environment from csp\tabular}{lc<CR>
This will be removed once a more fully functional customization system is
implemented. The following shows the resulting environment from
csp\tabular}{lc<CR>
>
\begin{tabular}{lc}
\end{tabular}
@ -164,6 +166,29 @@ replacements.
let g:surround_45 = "<% \r %>"
let g:surround_61 = "<%= \r %>"
<
Advanced, experimental, and subject to change: One can also prompt for
replacement text. The syntax for this is to surround the replacement in pairs
of low numbered controller characters. If this sounds confusing, that's
because it is (but it makes the parsing easy). Consider the following example
for a LaTeX environment on the "l" replacement.
>
let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\1}"
<
When this replacement is used, the user is prompted with an "environment: "
prompt for input. This input is inserted between each set of \1's.
Additional inputs up to \7 can be used.
Furthermore, one can specify a regular expression substitution to apply.
>
let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\r}.*\r\1}"
<
This will remove anything after the first } in the input when the text is
placed within the \end{} slot. The first \r marks where the pattern begins,
and the second where the replacement text begins.
Inputting text replacements is a proof of concept at this point. The ugly,
unintuitive interface and the brevity of the documentation reflect this.
ISSUES *surround-issues*
Vim could potentially get confused when deleting/changing occurs at the very
@ -172,7 +197,8 @@ end of the line. Please report any repeatable instances of this.
Do we need to use |inputsave()|/|inputrestore()| with the tag replacement?
Customization isn't very flexible. Need a system that allows for prompting,
like with HTML tags and LaTeX environments.
like with HTML tags and LaTeX environments. (There is an experimental
interface for this now).
Indenting is handled haphazardly. Need to decide the most appropriate
behavior and implement it. Right now one can do :let b:surround_indent = 1
@ -180,4 +206,5 @@ behavior and implement it. Right now one can do :let b:surround_indent = 1
should this be the default?
It would be nice if |.| would work to repeat an operation.
vim:tw=78:ts=8:ft=help:norl:

View File

@ -111,6 +111,47 @@ function! s:fixindent(str,spc)
return str
endfunction
function! s:process(string)
let i = 0
while i < 7
let i = i + 1
let repl_{i} = ''
let m = matchstr(a:string,nr2char(i).'.\{-\}\ze'.nr2char(i))
if m != ''
let m = substitute(strpart(m,1),'\r.*','','')
let repl_{i} = input(substitute(m,':\s*$','','').': ')
endif
endwhile
let s = ""
let i = 0
while i < strlen(a:string)
let char = strpart(a:string,i,1)
if char2nr(char) < 8
let next = stridx(a:string,char,i+1)
if next == -1
let s = s . char
else
let insertion = repl_{char2nr(char)}
let subs = strpart(a:string,i+1,next-i-1)
let subs = matchstr(subs,'\r.*')
echo substitute(subs,'\r','R','g')
while subs =~ '^\r.*\r'
let sub = matchstr(subs,"^\r\\zs[^\r]*\r[^\r]*")
let subs = strpart(subs,strlen(sub)+1)
let r = stridx(sub,"\r")
let insertion = substitute(insertion,strpart(sub,0,r),strpart(sub,r+1),'')
endwhile
let s = s . insertion
let i = next
endif
else
let s = s . char
endif
let i = i + 1
endwhile
return s
endfunction
function! s:wrap(string,char,type,...)
let keeper = a:string
let newchar = a:char
@ -133,11 +174,13 @@ function! s:wrap(string,char,type,...)
endif
let idx = stridx(pairs,newchar)
if exists("b:surround_".char2nr(newchar))
let before = s:extractbefore(b:surround_{char2nr(newchar)})
let after = s:extractafter(b:surround_{char2nr(newchar)})
let all = s:process(b:surround_{char2nr(newchar)})
let before = s:extractbefore(all)
let after = s:extractafter(all)
elseif exists("g:surround_".char2nr(newchar))
let before = s:extractbefore(g:surround_{char2nr(newchar)})
let after = s:extractafter(g:surround_{char2nr(newchar)})
let all = s:process(g:surround_{char2nr(newchar)})
let before = s:extractbefore(all)
let after = s:extractafter(all)
elseif newchar ==# "p"
let before = "\n"
let after = "\n\n"
@ -427,6 +470,7 @@ function! s:opfunc(type,...) " {{{1
let &selection = "inclusive"
let reg_save = getreg(reg)
let reg_type = getregtype(reg)
"call setreg(reg,"\n","c")
let type = a:type
if a:type == "char"
silent exe 'norm! v`[o`]"'.reg."y"
@ -437,7 +481,12 @@ function! s:opfunc(type,...) " {{{1
elseif a:type ==# "v" || a:type ==# "V" || a:type ==# "\<C-V>"
silent exe 'norm! gv"'.reg."y"
elseif a:type =~ '^\d\+$'
let type = 'v'
silent exe 'norm! ^v'.a:type.'$h"'.reg.'y'
if mode() == 'v'
norm! v
return s:beep()
endif
else
let &selection = sel_save
return s:beep()