Add ingo#plugin#cmd#withpattern#BufferCommandWithPattern()

This commit is contained in:
Ingo Karkat
2019-01-26 12:32:03 +01:00
parent c44878dd37
commit 2e08e49d7f
4 changed files with 65 additions and 0 deletions
+1
View File
@@ -118,6 +118,7 @@ HISTORY
ingo#comments#SplitIndentAndText() that just returns the pattern.
- Extract ingo#cmdrange#FromCount() from
ingo#cmdrangeconverter#LineToBufferRange().
- Add ingo/plugin/cmd/withpattern.vim module.
##### 1.035 29-Sep-2018
- Add ingo#compat#commands#NormalWithCount().
+62
View File
@@ -0,0 +1,62 @@
" ingo/plugin/cmd/withpattern.vim: Functions to make plugin commands that operate on a pattern.
"
" DEPENDENCIES:
"
" Copyright: (C) 2019 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
let s:lastCommandPatternForId = {}
function! ingo#plugin#cmd#withpattern#BufferCommandWithPattern( id, isQuery, isSelection, commandTemplate )
"******************************************************************************
"* PURPOSE:
" Build an Ex command from a:commandTemplate that is passed a queried /
" recalled pattern (stored under a:id) and apply this to the visual selection
" or the command-line range created from a:count, defaulting to the whole
" buffer if no count is given (even though the command defaults to the current
" line).
"* ASSUMPTIONS / PRECONDITIONS:
" None.
"* EFFECTS / POSTCONDITIONS:
" - queries for input with a:isQuery
" - executes a:commandTemplate
"* INPUTS:
" a:id Identifier under which the queried pattern is stored and recalled.
" a:isQuery Flag whether the pattern is queried from the user.
" a:isSelection Flag whether the command should be applied to the last
" selected range.
" a:commandTemplate Ex command that contains a %s for the queried / recalled
" range to be inserted.
"* RETURN VALUES:
" 1 if success, 0 if the execution failed. An error message is then available
" from ingo#err#Get().
"******************************************************************************
if a:isQuery
let l:pattern = input('/')
if empty(l:pattern) | return 1 | endif
let s:lastCommandPatternForId[a:id] = l:pattern
endif
if ! has_key(s:lastCommandPatternForId, a:id)
call ingo#err#Set('No pattern defined yet')
return 0
endif
let l:command = printf(a:commandTemplate, escape(s:lastCommandPatternForId[a:id], '/'))
if a:isSelection
try
execute "'<,'>" . l:command
return 1
catch /^Vim\%((\a\+)\)\=:/
call ingo#err#SetVimException()
return 0
endtry
else
" The function handles errors itself.
return ingo#cmdrangeconverter#LineToBufferRange(l:command)
endif
endfunction
" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :
+1
View File
@@ -132,6 +132,7 @@ HISTORY *ingo-library-history*
ingo#comments#SplitIndentAndText() that just returns the pattern.
- Extract ingo#cmdrange#FromCount() from
ingo#cmdrangeconverter#LineToBufferRange().
- Add ingo/plugin/cmd/withpattern.vim module.
1.035 29-Sep-2018
- Add ingo#compat#commands#NormalWithCount().
+1
View File
@@ -107,6 +107,7 @@ autoload/ingo/nary.vim
autoload/ingo/number.vim
autoload/ingo/option.vim
autoload/ingo/os.vim
autoload/ingo/plugin/cmd/withpattern.vim
autoload/ingo/plugin/cmdcomplete.vim
autoload/ingo/plugin/compiler.vim
autoload/ingo/plugin/marks.vim