Vim python buffer.append (string) window switch focus

I am trying to fill a Vim buffer from a separate thread using this python code.

python << PYTHON_CODE

import vim
import time

buffer_number = -1
class AppendLineTest( Thread ):
   def run(self):
      buffer = vim.buffers[buffer_number - 1]

      for i in range(10):
         buffer.append('Line number %s' % i)
         time.sleep(1)

PYTHON_CODE

function! s:test()
   split TestBuffer
   exec 'python buffer_number = '.bufnr('%')

   setlocal noswapfile
   setlocal bufhidden=delete
   setlocal buftype=nofile
   setlocal nobuflisted
   setlocal nonumber
   setlocal nowrap
   setlocal nocursorline

   python AppendLineTest().start()
endfunction

command! -nargs=0 PythonAppendTest call s:test()

      

I'm not sure if accessing Vim buffers from separate threads is allowed and wondering if there is some safe way to send. But if allowed, I would like to get rid of the cursor jumping when doing the add line.

So, to reproduce the behavior, save the code in some name.vim file and open it in Vim. Then

:source %

      

And then

PythonAppendTest

      

The lines will be added 10 times to the temporary buffer. Just move your cursor back to the window where you were and you will know what I am talking about.

If you're wondering why someone needs to add lines to buffers from a separate stream, here's an example:

  • unix top
  • unix tail
  • news
  • Email

While you work, they appear in some kind of predefined buffer.

Thanks in advance.

+2


source to share


1 answer


I don't think Vim is very multi-thread tolerant without patches. There's more detail in the discussion at this link , but I suspect what you want is far from trivial.



+2


source







All Articles