" Define a function Input(function) implementing input() with completion. " Other actions are possible, too. " " The completion must be handled by the function given as argument. It will be " invoked in three different ways: " " 1) function("init") " " The function can set up command line mode mappings that set the global " variable 'g:InputHandlerString'. If set to the empty string, Input() will " terminate. Mappings for will be overriden and the special string " "CtrlC" is reserved. " " For all this to work, the mappings should be of the form of eg. " " cnoremap =KeyHandler("Tab") " fun KeyHandler(key) " let g:InputHandlerString = a:key " return '' " endfun " " Note that an empty string is returned. If not, vim will return 0, which is " inserted at the current cursor position in the input() string. " " 2) function("handle", str) " " The function is handed the current string used by the input() cycle. " Depending on the value of the 'g:InputHandlerString' variable, different " actions can be taken. Whatever is returned by the function is used in the " next input() cycle. " " 3) function("cleanup") " " The function should reset mappings defined during initialisation. " " ---------------------------------------------------------------------------- " " A fairly silly example of all this, which does not behave well and just " overrides a mapping for : " " fun! KeyHandler(key) " let g:InputHandlerString = a:key " return '' " endfun " " fun! InputFoo(action, ...) " if a:action == "init" " cnoremap =KeyHandler("Tab") " return '' " elseif a:action == "cleanup" " silent! cunmap " return '' " endif " " Handle completion. " if a:1 == 'foo' " return a:1 . 'bar' " elseif a:1 == 'foobar' " return 'foo' " endif " return a:1 " endfun " " :call Input("InputFoo") " What: foo " " TODO: recursion of Input() calls. " ---------------------------------------------------------------------------- " Internal function to detect CTRL-C. " The "CtrlC" key is reserved for this script to handle abortion. fun! s:InputCtrlC() let g:InputHandlerString = "CtrlC" return '' endfun " Function to safely map . " TODO: Information on :cnoremap'ed mappings are lost. " TODO: We cannot fully rely on maparg() (eg. if a key is mapped to ). " TODO: If a and regular map are identical, only the regular one is " restored later on. " TODO: If more than and standard maps are stacked, this approach " is not the best. Not sure if they can be. " A bit of testing indicates that