diff --git a/README.md b/README.md index d88d039..6c0b465 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,8 @@ HISTORY - Add ingo#collections#SplitIntoMatches(). - Add ingo#regexp#collection#ToBranches(). - Add ingo/regexp/{deconstruct,length,multi} modules. +- Add ingo#range#Is{In,Out}side(). +- Add ingo/cursor/keep.vim module. ##### 1.034 13-Feb-2018 - Add ingo/regexp/split.vim module. diff --git a/autoload/ingo/cursor/keep.vim b/autoload/ingo/cursor/keep.vim new file mode 100644 index 0000000..7092c91 --- /dev/null +++ b/autoload/ingo/cursor/keep.vim @@ -0,0 +1,59 @@ +" ingo/cursor/keep.vim: Functions to keep the cursor at its current position. +" +" DEPENDENCIES: +" - ingo/actions.vim autoload script +" - ingo/range.vim autoload script +" +" Copyright: (C) 2018 Ingo Karkat +" The VIM LICENSE applies to this script; see ':help copyright'. +" +" Maintainer: Ingo Karkat + +function! ingo#cursor#keep#WhileExecuteOrFunc( startLnum, endLnum, Action, ... ) +"****************************************************************************** +"* PURPOSE: +" Commands in the executed a:Action do not change the current text position +" (within the range of a:startLnum,a:endLnum), relative to the current text. +" This works by temporarily inserting a sentinel character at the current +" cursor position, and searching for it after the action has executed. +"* ASSUMPTIONS / PRECONDITIONS: +" Current buffer is modifiable. +" Text within the a:startLnum, a:endLnum (adapted to any change in line +" numbers by a:Action) range does not contain the sentinel value (NUL = ^@). +"* EFFECTS / POSTCONDITIONS: +" None. +"* INPUTS: +" a:startLnum Range of lines that may be affected by a:Action. +" a:endLnum +" a:Action Either a Funcref or Ex commands to be :executed. +" a:arguments Value(s) to be passed to the a:Action Funcref (but not the +" Ex commands). +"* RETURN VALUES: +" Result of evaluating a:Action, for Ex commands you need to use :return. +"****************************************************************************** + let l:endLnum = a:endLnum + let l:lineNum = line('$') + let l:save_foldenable = &l:foldenable + setlocal nofoldenable + + noautocmd execute "normal! i\\\" + try + return call('ingo#actions#ExecuteOrFunc', [a:Action] + a:000) + finally + let l:addedLineNum = line('$') - l:lineNum + let l:endLnum += l:addedLineNum + + if ingo#range#IsOutside(line('.'), a:startLnum, l:endLnum) + call cursor(a:startLnum, 1) + endif + + if search("\", 'cW', l:endLnum) != 0 || search("\", 'bcW', a:startLnum) != 0 + " Found the sentinel, remove it. + noautocmd normal! x + endif + + let &l:foldenable = l:save_foldenable + endtry +endfunction + +" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : diff --git a/autoload/ingo/range.vim b/autoload/ingo/range.vim index a38b76a..8d48683 100644 --- a/autoload/ingo/range.vim +++ b/autoload/ingo/range.vim @@ -2,7 +2,7 @@ " " DEPENDENCIES: " -" Copyright: (C) 2012-2017 Ingo Karkat +" Copyright: (C) 2012-2018 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat @@ -49,4 +49,11 @@ function! ingo#range#IsEntireBuffer( startLnum, endLnum ) return (a:startLnum <= 1 && a:endLnum == line('$')) endfunction +function! ingo#range#IsOutside( lnum, startLnum, endLnum ) + return (a:lnum < a:startLnum || a:lnum > a:endLnum) +endfunction +function! ingo#range#IsInside( lnum, startLnum, endLnum ) + return ! ingo#range#IsOutside(a:lnum, a:startLnum, a:endLnum) +endfunction + " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax : diff --git a/doc/ingo-library.txt b/doc/ingo-library.txt index 7c8fbe7..f24d28e 100644 --- a/doc/ingo-library.txt +++ b/doc/ingo-library.txt @@ -146,6 +146,8 @@ HISTORY *ingo-library-history* - Add ingo#collections#SplitIntoMatches(). - Add ingo#regexp#collection#ToBranches(). - Add ingo/regexp/{deconstruct,length,multi} modules. +- Add ingo#range#Is{In,Out}side(). +- Add ingo/cursor/keep.vim module. 1.034 13-Feb-2018 - Add ingo/regexp/split.vim module. diff --git a/ingo-library.manifest b/ingo-library.manifest index 9b4f63a..8c01474 100644 --- a/ingo-library.manifest +++ b/ingo-library.manifest @@ -43,6 +43,7 @@ autoload/ingo/compat/regexp.vim autoload/ingo/compat/shellcommand.vim autoload/ingo/compat/window.vim autoload/ingo/cursor.vim +autoload/ingo/cursor/keep.vim autoload/ingo/cursor/move.vim autoload/ingo/date.vim autoload/ingo/date/epoch.vim