From 2e08e49d7fea8703f99d2e6328239a613997fbc7 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sat, 26 Jan 2019 12:32:03 +0100 Subject: [PATCH] Add ingo#plugin#cmd#withpattern#BufferCommandWithPattern() --- README.md | 1 + autoload/ingo/plugin/cmd/withpattern.vim | 62 ++++++++++++++++++++++++ doc/ingo-library.txt | 1 + ingo-library.manifest | 1 + 4 files changed, 65 insertions(+) create mode 100644 autoload/ingo/plugin/cmd/withpattern.vim diff --git a/README.md b/README.md index d856125..c196091 100644 --- a/README.md +++ b/README.md @@ -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(). diff --git a/autoload/ingo/plugin/cmd/withpattern.vim b/autoload/ingo/plugin/cmd/withpattern.vim new file mode 100644 index 0000000..025082d --- /dev/null +++ b/autoload/ingo/plugin/cmd/withpattern.vim @@ -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 + +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 : diff --git a/doc/ingo-library.txt b/doc/ingo-library.txt index 05d342a..aeeb1ae 100644 --- a/doc/ingo-library.txt +++ b/doc/ingo-library.txt @@ -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(). diff --git a/ingo-library.manifest b/ingo-library.manifest index 0b56913..ce67a90 100644 --- a/ingo-library.manifest +++ b/ingo-library.manifest @@ -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