Permalink
Browse files

Version 1.10

- FIX: In a blockwise visual selection with $ to the end of the lines, only the square block from '< to '> is transformed. Need to yank the selection with gvy instead of defining a new selection with the marks, a mistake inherited from the original unimpaired.vim implementation.
- Save and restore the original visual area to avoid clobbering the '< and '> marks and gv by line- and motion mappings.
- Temporarily set g:TextTransformContext to the begin and end of the currently transformed area to offer an extended interface to algorithms.
  • Loading branch information...
1 parent 6f818e6 commit 6ee1e40b97033f16196e13db2fc15202ce9a2759 Ingo Karkat committed with Jan 21, 2013
Showing with 69 additions and 14 deletions.
  1. +5 −3 autoload/TextTransform.vim
  2. +26 −4 autoload/TextTransform/Arbitrary.vim
  3. +18 −5 autoload/TextTransform/Lines.vim
  4. +20 −2 doc/TextTransform.txt
@@ -1,8 +1,8 @@
" TextTransform.vim: Create text transformation mappings and commands.
"
" DEPENDENCIES:
-" - TextTransform#Arbitrary.vim autoload script.
-" - TextTransform#Lines.vim autoload script.
+" - TextTransform#Arbitrary.vim autoload script
+" - TextTransform#Lines.vim autoload script
"
" Copyright: (C) 2011-2012 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
@@ -12,6 +12,8 @@
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
+" 1.04.011 28-Dec-2012 Minor: Correct lnum for no-modifiable buffer
+" check.
" 1.00.010 05-Apr-2012 Initial release.
" 010 19-Oct-2011 BUG: Variable rename from LineTypes to
" l:selectionModes broke Funcref arguments; my
@@ -92,7 +94,7 @@ function! TextTransform#MakeMappings( mapArgs, key, algorithm, ... )
\ string(l:mappingName)
\)
- let l:noopModificationCheck = 'call <SID>Before()<Bar>call setline(1, getline(1))<Bar>call <SID>After()<Bar>'
+ let l:noopModificationCheck = 'call <SID>Before()<Bar>call setline(".", getline("."))<Bar>call <SID>After()<Bar>'
" Repeat not defined in visual mode.
execute printf('vnoremap <silent> %s <SID>%sVisual :<C-u>%scall TextTransform#Arbitrary#Visual(%s, %s)<CR>',
@@ -3,17 +3,29 @@
" This module is responsible for the transformation triggered by mappings.
"
" DEPENDENCIES:
-" - vimscript #2136 repeat.vim autoload script (optional).
-" - visualrepeat.vim (vimscript #3848) autoload script (optional).
+" - vimscript #2136 repeat.vim autoload script (optional)
+" - visualrepeat.vim (vimscript #3848) autoload script (optional)
"
-" Copyright: (C) 2011-2012 Ingo Karkat
+" Copyright: (C) 2011-2013 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
" Idea, design and implementation based on unimpaired.vim (vimscript #1590)
" by Tim Pope.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
+" 1.10.009 18-Jan-2013 FIX: In a blockwise visual selection with $ to
+" the end of the lines, only the square block from
+" '< to '> is transformed. Need to yank the
+" selection with gvy instead of defining a new
+" selection with the marks, a mistake inherited
+" from the original unimpaired.vim implementation.
+" Save and restore the original visual area to
+" avoid clobbering the '< and '> marks and gv by
+" line- and motion mappings.
+" Temporarily set g:TextTransformContext to the
+" begin and end of the currently transformed area
+" to offer an extended interface to algorithms.
" 1.03.008 28-Aug-2012 For the custom operators, handle readonly and
" nomodifiable buffers by printing just the
" warning / error, without the multi-line function
@@ -66,6 +78,7 @@ function! s:Error( onError, errorText )
endif
endfunction
function! s:ApplyAlgorithm( algorithm, text )
+ let g:TextTransformContext = {'mode': visualmode(), 'startPos': getpos("'<"), 'endPos': getpos("'>")}
try
return call(a:algorithm, [a:text])
catch /^Vim\%((\a\+)\)\=:E/
@@ -80,6 +93,8 @@ function! s:ApplyAlgorithm( algorithm, text )
echohl ErrorMsg
echomsg v:errmsg
echohl None
+ finally
+ unlet g:TextTransformContext
endtry
endfunction
function! s:Transform( count, algorithm, selectionModes, onError )
@@ -90,6 +105,7 @@ function! s:Transform( count, algorithm, selectionModes, onError )
let l:save_reg = getreg('"')
let l:save_regmode = getregtype('"')
let @" = ''
+ let l:save_visualarea = [visualmode(), getpos("'<"), getpos("'>")]
let l:isSuccess = 0
let l:count = (a:count ? a:count : '')
@@ -104,7 +120,7 @@ function! s:Transform( count, algorithm, selectionModes, onError )
elseif l:SelectionMode ==# 'lines'
silent! execute 'normal! 0v' . l:count . '$' . (&selection ==# 'exclusive' ? '' : 'h') . 'y'
elseif l:SelectionMode =~# "^[vV\<C-v>]$"
- silent! execute 'normal! `<' . l:SelectionMode . '`>y'
+ silent! normal! gvy
elseif l:SelectionMode ==# 'char'
silent! execute 'normal! `[v`]'. (&selection ==# 'exclusive' ? 'l' : '') . 'y'
elseif l:SelectionMode ==# 'line'
@@ -165,6 +181,12 @@ function! s:Transform( count, algorithm, selectionModes, onError )
endif
endif
+ if visualmode() !=# l:save_visualarea[0]
+ execute 'normal!' l:save_visualarea[0] . "\<Esc>"
+ endif
+ call setpos("'<", l:save_visualarea[1])
+ call setpos("'>", l:save_visualarea[2])
+
call setreg('"', l:save_reg, l:save_regmode)
let &clipboard = l:save_clipboard
@@ -3,28 +3,41 @@
" This module is responsible for the transformation triggered by commands.
"
" DEPENDENCIES:
-" - ingolines.vim autoload script (for
-" TextTransform#Lines#TransformWholeText())
+" - ingolines.vim autoload script (for TextTransform#Lines#TransformWholeText())
"
-" Copyright: (C) 2011-2012 Ingo Karkat
+" Copyright: (C) 2011-2013 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
" Idea, design and implementation based on unimpaired.vim (vimscript #1590)
" by Tim Pope.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
+" 1.10.003 18-Jan-2013 Temporarily set g:TextTransformContext to the
+" begin and end of the currently transformed lines
+" to offer the same extended interface for
+" algorithms that TextTransform/Arbitrary.vim
+" had automatically with the '<, '> marks and
+" visualmode().
" 1.03.002 02-Sep-2012 Avoid clobbering the expression register (for
" commands that use options.isProcessEntireText).
" 1.00.001 05-Apr-2012 Initial release.
" 001 05-Apr-2011 file creation from autoload/TextTransform.vim.
+function! s:Transform( firstLine, lastLine, text, algorithm )
+ let g:TextTransformContext = {'mode': 'V', 'startPos': [0, a:firstLine, 1, 0], 'endPos': [0, a:lastLine, 0x7FFFFFFF, 0]}
+ try
+ return call(a:algorithm, [a:text])
+ finally
+ unlet g:TextTransformContext
+ endtry
+endfunction
function! TextTransform#Lines#TransformLinewise( firstLine, lastLine, algorithm )
let [l:lastModifiedLine, l:modifiedLineCnt] = [0, 0]
for l:i in range(a:firstLine, a:lastLine)
let l:text = getline(l:i)
- let l:transformedText = call(a:algorithm, [l:text])
+ let l:transformedText = s:Transform(l:i, l:i, l:text, a:algorithm)
if l:text !=# l:transformedText
let l:lastModifiedLine = l:i
let l:modifiedLineCnt += 1
@@ -39,7 +52,7 @@ function! TextTransform#Lines#TransformWholeText( firstLine, lastLine, algorithm
let l:lines = getline(a:firstLine, a:lastLine)
let l:text = join(l:lines, "\n")
- let l:transformedText = call(a:algorithm, [l:text])
+ let l:transformedText = s:Transform(a:firstLine, a:lastLine, l:text, a:algorithm)
if l:text !=# l:transformedText
silent execute a:firstLine . ',' . a:lastLine . 'delete _'
call ingolines#PutWrapper((a:firstLine - 1), 'put', l:transformedText)
View
@@ -78,7 +78,15 @@ TextTransform#MakeMappings( {mapArgs}, {key}, {algorithm} [, {selectionModes}] )
than one line, {algorithm} is invoked only once with
the entire, newline-delimited text. You need to
consider that when using regular expression atoms like
- |^| and |$|.
+ |^| and |$|. *g:TextTransformContext*
+ If the algorithm needs to inspect the place in the
+ buffer from where the text was retrieved, this
+ information is provided in g:TextTransformContext, a
+ dictionary with the following keys:
+ "mode": indicates the selection mode, one of "v",
+ "V", or "^V" (like |visualmode()|)
+ "startPos": the start position of the text
+ "endPos" : the end position of the text
The function can |:throw| an exception to signal an
error. When the original text is returned,
TextTransform will print an error that nothing was
@@ -212,6 +220,16 @@ IDEAS *TextTransform-ideas*
==============================================================================
HISTORY *TextTransform-history*
+1.10 19-Jan-2013
+- FIX: In a blockwise visual selection with $ to the end of the lines, only
+ the square block from '< to '> is transformed. Need to yank the selection
+ with gvy instead of defining a new selection with the marks, a mistake
+ inherited from the original unimpaired.vim implementation.
+- Save and restore the original visual area to avoid clobbering the '< and '>
+ marks and gv by line- and motion mappings.
+- Temporarily set g:TextTransformContext to the begin and end of the currently
+ transformed area to offer an extended interface to algorithms.
+
1.03 05-Sep-2012
- For the custom operators, handle readonly and nomodifiable buffers by
printing just the warning / error, without the multi-line function error.
@@ -234,7 +252,7 @@ First published version.
Started development.
==============================================================================
-Copyright: (C) 2011-2012 Ingo Karkat
+Copyright: (C) 2011-2013 Ingo Karkat
The VIM LICENSE applies to this script; see |copyright|.
Maintainer: Ingo Karkat <ingo@karkat.de>

0 comments on commit 6ee1e40

Please sign in to comment.