0

I'm trying to write a simple plugin that let user input something, and while inputing, the plugin echos the input text in real time.

So far the script shows result after Enter pressed:

function! Test()
  let curline = getline('.')
  call inputsave()
  let name = input('Enter name: ')
  call inputrestore()
  call setline('.', curline . ' ' . name)
endfunction

How to improve it to handle real time input? I searched for :h input but I don't see any typing callback.

Update

The getchar works, but the text input is not displayed, I need to see the text input and modify it, like the input(message) feature, is it possible?

function! s:getchar()
  let c = getchar()
  if c =~ '^\d\+$'
    let c = nr2char(c)
  endif
  return c
endfunction

" Interactively change the window title
function! InteractiveTest()
    let char = "s"
    while char =~ '^\w$'
        echo "input to change title"
        let char = s:getchar()
        let &titlestring=char
        redraw
    endwhile
endfunction

1

I guess you'll have to introduce a fake mode, loop on getchar(), and redraw as you type.

In lh-vim-lib, check the code that is using lh#ui#ask(). In my modes I update a kind of status line that is used for displaying check boxes or comboboxes.

You'll likely have to update something equivalent and your buffer if I understand correctly your question.


Regarding your edit, you have to build the titlestring as you type whilst you were only using the last character typed.

It's more like

" autoload/youplugin.vim

function! s:getchar() abort
  let c = getchar()
  if c =~ '^\d\+$'
    let c = nr2char(c)
  endif
  return c
endfunction

" Interactively change the window title
function! yourplugin#InteractiveTest() abort
  let orig = &titlestring
  let title = ''
  while 1
    echo "input to change title: ".title
    let char = s:getchar()
    if char == "\<bs>" && !empty(title)
      let title = title[:-2]
    elseif  strtrans(char) == char
      let title .= char
    elseif char == "\<cr>"
      return title
    elseif char == "\<esc>"
      " restore and return
      let &titlestring = orig
      return ''
    endif
    let &titlestring = title
    redraw
  endwhile
endfunction
  • The getchar() works, but there is a new problem, please see the updated post. – aj3423 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.