Permalink
Browse files

Version 1.24

- ENH: Allow optional [!] for made commands, and suppress the "Nothing transformed" error in that case. This can help when using the resulting command as an optional step, as prefixing with :silent! can be difficult to combine with ranges, and would suppress _all_ transformation errors.
- ENH: Add g:TextTransformContext.isBang (for custom transformation commands).
  • Loading branch information...
1 parent 881b68a commit c3b33110f42c2713c47238164178a51fd31cbfc4 Ingo Karkat committed with Jun 19, 2014
Showing with 42 additions and 19 deletions.
  1. +10 −3 autoload/TextTransform.vim
  2. +11 −8 autoload/TextTransform/Arbitrary.vim
  3. +10 −7 autoload/TextTransform/Lines.vim
  4. +11 −1 doc/TextTransform.txt
@@ -5,14 +5,21 @@
" - TextTransform#Lines.vim autoload script
" - ingo/err.vim autoload script
"
-" Copyright: (C) 2011-2013 Ingo Karkat
+" Copyright: (C) 2011-2014 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.24.016 13-Jun-2014 ENH: Allow optional [!] for made commands, and
+" suppress the "Nothing transformed" error in that
+" case. This can help when using the resulting
+" command as an optional step, as prefixing with
+" :silent! can be difficult to combine with
+" ranges, and would suppress _all_ transformation
+" errors.
" 1.20.015 25-Sep-2013 Allow to pass command arguments, which are then
" accessible to the algorithm through
" g:TextTransformContext.arguments.
@@ -169,7 +176,7 @@ endfunction
function! TextTransform#MakeCommand( commandOptions, commandName, algorithm, ... )
let l:options = (a:0 ? a:1 : {})
- execute printf('command! -bar %s %s %s call <SID>Before() | call setline(<line1>, getline(<line1>)) | call <SID>After() | if ! TextTransform#Lines#TransformCommand(<line1>, <line2>, %s, %s, <f-args>) | echoerr ingo#err#Get() | endif',
+ execute printf('command! -bar -bang %s %s %s call <SID>Before() | call setline(<line1>, getline(<line1>)) | call <SID>After() | if ! TextTransform#Lines#TransformCommand(<line1>, <line2>, <bang>0, %s, %s, <f-args>) | if <bang>1 || ingo#err#Get() !=# "Nothing transformed" | echoerr ingo#err#Get() | endif | endif',
\ a:commandOptions,
\ (a:commandOptions =~# '\%(^\|\s\)-range\%(=\|\s\)' ? '' : '-range'),
\ a:commandName,
@@ -180,7 +187,7 @@ endfunction
function! TextTransform#MakeSelectionCommand( commandOptions, commandName, algorithm, selectionModes )
- execute printf('command -bar -count %s %s call <SID>Before() | call setline(<line1>, getline(<line1>)) | call <SID>After() | if ! TextTransform#Arbitrary#Command(<line1>, <line2>, <count>, %s, %s, <f-args>) | echoerr ingo#err#Get() | endif',
+ execute printf('command -bar -bang -count %s %s call <SID>Before() | call setline(<line1>, getline(<line1>)) | call <SID>After() | if ! TextTransform#Arbitrary#Command(<line1>, <line2>, <bang>0, <count>, %s, %s, <f-args>) | if <bang>1 || ingo#err#Get() !=# "Nothing transformed" | echoerr ingo#err#Get() | endif | endif',
\ a:commandOptions,
\ a:commandName,
\ string(a:algorithm),
@@ -19,6 +19,8 @@
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
+" 1.24.021 13-Jun-2014 ENH: Add g:TextTransformContext.isBang (for
+" custom transformation commands).
" 1.23.020 08-May-2014 ENH: Support selection mode "/{pattern}/", which
" selects the text region under / after the cursor
" that matches {pattern}.
@@ -116,7 +118,7 @@ function! s:Error( onError, errorText )
call ingo#err#Set(a:errorText)
endif
endfunction
-function! s:ApplyAlgorithm( algorithm, text, mapMode, changedtick, arguments )
+function! s:ApplyAlgorithm( algorithm, text, mapMode, changedtick, arguments, isBang )
let l:isAlgorithmRepeat = (s:previousTransform.changedtick == a:changedtick &&
\ type(s:previousTransform.algorithm) == type(a:algorithm) && s:previousTransform.algorithm ==# a:algorithm
\)
@@ -126,6 +128,7 @@ function! s:ApplyAlgorithm( algorithm, text, mapMode, changedtick, arguments )
\ 'startPos': getpos("'<"),
\ 'endPos': getpos("'>"),
\ 'arguments': a:arguments,
+ \ 'isBang': a:isBang,
\ 'isAlgorithmRepeat': l:isAlgorithmRepeat,
\ 'isRepeat': (l:isAlgorithmRepeat && s:repeatTick == a:changedtick)
\}
@@ -141,7 +144,7 @@ function! s:ApplyAlgorithm( algorithm, text, mapMode, changedtick, arguments )
unlet g:TextTransformContext
endtry
endfunction
-function! s:Transform( count, algorithm, selectionModes, onError, mapMode, changedtick, arguments )
+function! s:Transform( count, algorithm, selectionModes, onError, mapMode, changedtick, arguments, isBang )
let l:save_view = winsaveview()
let l:save_cursor = getpos('.')
let l:save_clipboard = &clipboard
@@ -204,7 +207,7 @@ function! s:Transform( count, algorithm, selectionModes, onError, mapMode, chang
call s:Error(a:onError, 'Not applicable here')
else
let l:yankMode = getregtype('"')
- let [l:isSuccess, l:transformedText] = s:ApplyAlgorithm(a:algorithm, @", a:mapMode, a:changedtick, a:arguments)
+ let [l:isSuccess, l:transformedText] = s:ApplyAlgorithm(a:algorithm, @", a:mapMode, a:changedtick, a:arguments, a:isBang)
if ! l:isSuccess
call winrestview(l:save_view)
if a:onError ==# 'beep'
@@ -289,7 +292,7 @@ endfunction
function! TextTransform#Arbitrary#Opfunc( selectionMode )
let l:count = v:count1
- if ! s:Transform(v:count, s:algorithm, a:selectionMode, 'beep', 'o', b:changedtick - (&l:readonly ? 1 : 0), []) " Need to subtract 1 from b:changedtick because of the no-op modification check (which here is conditional on 'readonly').
+ if ! s:Transform(v:count, s:algorithm, a:selectionMode, 'beep', 'o', b:changedtick - (&l:readonly ? 1 : 0), [], 0) " Need to subtract 1 from b:changedtick because of the no-op modification check (which here is conditional on 'readonly').
if ingo#err#IsSet()
call ingo#msg#ErrorMsg(ingo#err#Get())
endif
@@ -308,7 +311,7 @@ endfunction
function! TextTransform#Arbitrary#Line( algorithm, selectionModes, repeatMapping, isRepeat )
let l:count = v:count1
if ! a:isRepeat | let s:repeatTick = -1 | endif
- if ! s:Transform(v:count, a:algorithm, a:selectionModes, 'beep', 'n', b:changedtick - 1, []) " Need to subtract 1 from b:changedtick because of the no-op modification check.
+ if ! s:Transform(v:count, a:algorithm, a:selectionModes, 'beep', 'n', b:changedtick - 1, [], 0) " Need to subtract 1 from b:changedtick because of the no-op modification check.
if ingo#err#IsSet()
call ingo#msg#ErrorMsg(ingo#err#Get())
endif
@@ -329,7 +332,7 @@ endfunction
function! TextTransform#Arbitrary#Visual( algorithm, repeatMapping, isRepeat )
let l:count = v:count1
if ! a:isRepeat | let s:repeatTick = -1 | endif
- if ! s:Transform(v:count, a:algorithm, visualmode(), 'beep', 'v', b:changedtick - 1, []) " Need to subtract 1 from b:changedtick because of the no-op modification check.
+ if ! s:Transform(v:count, a:algorithm, visualmode(), 'beep', 'v', b:changedtick - 1, [], 0) " Need to subtract 1 from b:changedtick because of the no-op modification check.
if ingo#err#IsSet()
call ingo#msg#ErrorMsg(ingo#err#Get())
endif
@@ -349,13 +352,13 @@ function! TextTransform#Arbitrary#Visual( algorithm, repeatMapping, isRepeat )
let s:repeatTick = b:changedtick
endfunction
-function! TextTransform#Arbitrary#Command( firstLine, lastLine, count, algorithm, selectionModes, ... )
+function! TextTransform#Arbitrary#Command( firstLine, lastLine, isBang, count, algorithm, selectionModes, ... )
let l:selectionMode = a:selectionModes
if a:firstLine == line("'<") && a:lastLine == line("'>")
let l:selectionMode = visualmode()
endif
- let l:status = s:Transform(a:count, a:algorithm, l:selectionMode, 'errmsg', 'c', b:changedtick - 1, a:000) " Need to subtract 1 from b:changedtick because of the no-op modification check.
+ let l:status = s:Transform(a:count, a:algorithm, l:selectionMode, 'errmsg', 'c', b:changedtick - 1, a:000, a:isBang) " Need to subtract 1 from b:changedtick because of the no-op modification check.
" Store the change number and algorithm so that we can detect a repeat of
" the same substitution.
@@ -13,6 +13,8 @@
" Maintainer: Ingo Karkat <ingo@karkat.de>
"
" REVISION DATE REMARKS
+" 1.24.011 13-Jun-2014 ENH: Add g:TextTransformContext.isBang (for
+" custom transformation commands).
" 1.23.010 19-May-2014 Refactoring: Use ingo#lines#Replace().
" 1.23.009 25-Mar-2014 Minor: Also handle :echoerr in the algorithm.
" 1.20.008 25-Sep-2013 Add g:TextTransformContext.arguments.
@@ -35,13 +37,14 @@ let s:save_cpo = &cpo
set cpo&vim
let s:previousAlgorithm = ''
-function! s:Transform( firstLine, lastLine, text, algorithm, isRepeat, arguments )
+function! s:Transform( firstLine, lastLine, text, algorithm, isRepeat, arguments, isBang )
let g:TextTransformContext = {
\ 'mapMode': 'c',
\ 'mode': 'V',
\ 'startPos': [0, a:firstLine, 1, 0],
\ 'endPos': [0, a:lastLine, 0x7FFFFFFF, 0],
\ 'arguments': a:arguments,
+ \ 'isBang': a:isBang,
\ 'isAlgorithmRepeat': (type(s:previousAlgorithm) == type(a:algorithm) && s:previousAlgorithm ==# a:algorithm),
\ 'isRepeat': a:isRepeat
\}
@@ -51,12 +54,12 @@ function! s:Transform( firstLine, lastLine, text, algorithm, isRepeat, arguments
unlet g:TextTransformContext
endtry
endfunction
-function! TextTransform#Lines#TransformLinewise( firstLine, lastLine, algorithm, ... )
+function! TextTransform#Lines#TransformLinewise( firstLine, lastLine, isBang, 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 = s:Transform(l:i, l:i, l:text, a:algorithm, (l:i != a:firstLine), a:000)
+ let l:transformedText = s:Transform(l:i, l:i, l:text, a:algorithm, (l:i != a:firstLine), a:000, a:isBang)
if l:text !=# l:transformedText
let l:lastModifiedLine = l:i
let l:modifiedLineCnt += 1
@@ -66,12 +69,12 @@ function! TextTransform#Lines#TransformLinewise( firstLine, lastLine, algorithm,
return [l:lastModifiedLine, l:modifiedLineCnt]
endfunction
-function! TextTransform#Lines#TransformWholeText( firstLine, lastLine, algorithm, ... )
+function! TextTransform#Lines#TransformWholeText( firstLine, lastLine, isBang, algorithm, ... )
let [l:lastModifiedLine, l:modifiedLineCnt] = [0, 0]
let l:lines = getline(a:firstLine, a:lastLine)
let l:text = join(l:lines, "\n")
- let l:transformedText = s:Transform(a:firstLine, a:lastLine, l:text, a:algorithm, 0, a:000)
+ let l:transformedText = s:Transform(a:firstLine, a:lastLine, l:text, a:algorithm, 0, a:000, a:isBang)
if l:text !=# l:transformedText
call ingo#lines#Replace(a:firstLine, a:lastLine, l:transformedText)
@@ -89,9 +92,9 @@ function! TextTransform#Lines#TransformWholeText( firstLine, lastLine, algorithm
echomsg string( [l:lastModifiedLine, l:modifiedLineCnt])
return [l:lastModifiedLine, l:modifiedLineCnt]
endfunction
-function! TextTransform#Lines#TransformCommand( firstLine, lastLine, algorithm, ProcessFunction, ... )
+function! TextTransform#Lines#TransformCommand( firstLine, lastLine, isBang, algorithm, ProcessFunction, ... )
try
- let [l:lastModifiedLine, l:modifiedLineCnt] = call(a:ProcessFunction, [a:firstLine, a:lastLine, a:algorithm] + a:000)
+ let [l:lastModifiedLine, l:modifiedLineCnt] = call(a:ProcessFunction, [a:firstLine, a:lastLine, a:isBang, a:algorithm] + a:000)
unlet! s:previousAlgorithm | let s:previousAlgorithm = a:algorithm
View
@@ -100,6 +100,8 @@ TextTransform#MakeMappings( {mapArgs}, {key}, {algorithm} [, {selectionModes}] )
arguments (when arguments have been
configured for a command; always empty
for mappings).
+ "isBang": Flag whether a [!] was passed to the
+ command; always 0 for mappings.
"isAlgorithmRepeat":
Flag whether the same
|TextTransform-algorithm| is used
@@ -157,7 +159,8 @@ TextTransform#MakeCommand( {commandOptions}, {commandName}, {algorithm} [, {opti
Create a custom command {commandName} that takes a
range (defaulting to the current line), and applies
- {algorithm} to the line(s).
+ {algorithm} to the line(s). With an optional [!], the
+ usual "Nothing transformed" error is suppressed.
|:command| attributes can be passed in
{commandOptions}. For example, pass "-range=%" to make
@@ -257,6 +260,13 @@ IDEAS *TextTransform-ideas*
==============================================================================
HISTORY *TextTransform-history*
+1.24 19-Jun-2014
+- ENH: Allow optional [!] for made commands, and suppress the "Nothing
+ transformed" error in that case. This can help when using the resulting
+ command as an optional step, as prefixing with :silent! can be difficult to
+ combine with ranges, and would suppress _all_ transformation errors.
+- ENH: Add g:TextTransformContext.isBang (for custom transformation commands).
+
1.23 31-May-2014
- ENH: Support selection mode "/{pattern}/", which selects the text region
under / after the cursor that matches {pattern}.

0 comments on commit c3b3311

Please sign in to comment.