From 25fa3abe999ea0e9d3fb10b79da343bec9ecb8fd Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Fri, 18 May 2018 21:41:35 +0200 Subject: [PATCH] Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc() This removes a bit of duplication in the existing two functions in this module, and the functionality is useful on its own to some clients. --- README.md | 1 + autoload/ingo/plugin/cmdcomplete.vim | 73 ++++++++++++++++++---------- doc/ingo-library.txt | 1 + 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 42107d6..5716d8f 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ HISTORY - Add ingo/lists/find.vim module. - Add ingo/folds/containment.vim module. - Add ingo/ftplugin/setting.vim module. +- Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc(). ##### 1.034 13-Feb-2018 - Add ingo/regexp/split.vim module. diff --git a/autoload/ingo/plugin/cmdcomplete.vim b/autoload/ingo/plugin/cmdcomplete.vim index fa6d26e..d9a16d6 100644 --- a/autoload/ingo/plugin/cmdcomplete.vim +++ b/autoload/ingo/plugin/cmdcomplete.vim @@ -2,7 +2,7 @@ " " DEPENDENCIES: " -" Copyright: (C) 2013-2017 Ingo Karkat +" Copyright: (C) 2013-2018 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat @@ -10,25 +10,27 @@ let s:save_cpo = &cpo set cpo&vim let s:completeFuncCnt = 0 -function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... ) +function! ingo#plugin#cmdcomplete#MakeCompleteFunc( implementation, ... ) "****************************************************************************** "* PURPOSE: -" Define a complete function for :command -complete=customlist that completes -" from a static list of possible arguments. +" Generically define a complete function for :command -complete=customlist +" with a:implementation as the function body. "* USAGE: -" call ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( -" \ ['foo', 'fox', 'bar'], 'FooCompleteFunc') +" call ingo#plugin#cmdcomplete#MakeCompleteFunc( +" \ 'return a:ArgLead ==? "f" ? "Foo" : "Bar"', 'FooCompleteFunc') " command! -complete=customlist,FooCompleteFunc Foo ... " or alternatively " execute 'command! -complete=customlist,' . -" ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( -" \ ['foo', 'fox', 'bar']) . 'Foo ...' +" ingo#plugin#cmdcomplete#MakeCompleteFunc( +" \ 'return a:ArgLead ==? "f" ? "Foo" : "Bar"') 'Foo ...' "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: " Defines function. "* INPUTS: -" a:argumentList List of possible arguments. +" a:implementation String representing the function body of the completion +" function. It can refer to the completion arguments +" a:ArgLead, a:CmdLine, a:CursorPos. " a:funcName Optional name for the complete function; when not specified, " a unique name is generated. "* RETURN VALUES: @@ -43,12 +45,42 @@ function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... ) execute \ printf('function! %s( ArgLead, CmdLine, CursorPos )', l:funcName) . "\n" . - \ printf(' return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', string(a:argumentList)) . "\n" . - \ 'endfunction' + \ a:implementation . "\n" . + \ 'endfunction' return l:funcName endfunction +function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... ) +"****************************************************************************** +"* PURPOSE: +" Define a complete function for :command -complete=customlist that completes +" from a static list of possible arguments. +"* USAGE: +" call ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( +" \ ['foo', 'fox', 'bar'], 'FooCompleteFunc') +" command! -complete=customlist,FooCompleteFunc Foo ... +" or alternatively +" execute 'command! -complete=customlist,' . +" ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( +" \ ['foo', 'fox', 'bar']) 'Foo ...' +"* ASSUMPTIONS / PRECONDITIONS: +" None. +"* EFFECTS / POSTCONDITIONS: +" Defines function. +"* INPUTS: +" a:argumentList List of possible arguments. +" a:funcName Optional name for the complete function; when not specified, +" a unique name is generated. +"* RETURN VALUES: +" Name of the defined complete function. +"****************************************************************************** + return call('ingo#plugin#cmdcomplete#MakeCompleteFunc', + \ [printf('return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', string(a:argumentList))] + + \ a:000 + \) +endfunction + function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... ) "****************************************************************************** "* PURPOSE: @@ -61,7 +93,7 @@ function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... ) " or alternatively " execute 'command! -complete=customlist,' . " ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( -" \ 'map(copy(g:values), "v:val[0:3]")') ... +" \ 'map(copy(g:values), "v:val[0:3]")') 'Foo ...' "* ASSUMPTIONS / PRECONDITIONS: " None. "* EFFECTS / POSTCONDITIONS: @@ -74,19 +106,10 @@ function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... ) "* RETURN VALUES: " Name of the defined complete function. "****************************************************************************** - if a:0 - let l:funcName = a:1 - else - let s:completeFuncCnt += 1 - let l:funcName = printf('CompleteFunc%d', s:completeFuncCnt) - endif - - execute - \ printf('function! %s( ArgLead, CmdLine, CursorPos )', l:funcName) . "\n" . - \ printf(' return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', a:argumentExpr) . "\n" . - \ 'endfunction' - - return l:funcName + return call('ingo#plugin#cmdcomplete#MakeCompleteFunc', + \ [printf('return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', a:argumentExpr)] + + \ a:000 + \) endfunction let &cpo = s:save_cpo diff --git a/doc/ingo-library.txt b/doc/ingo-library.txt index cd4c253..72ecd73 100644 --- a/doc/ingo-library.txt +++ b/doc/ingo-library.txt @@ -107,6 +107,7 @@ HISTORY *ingo-library-history* - Add ingo/lists/find.vim module. - Add ingo/folds/containment.vim module. - Add ingo/ftplugin/setting.vim module. +- Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc(). 1.034 13-Feb-2018 - Add ingo/regexp/split.vim module.