Is there any way to update a buffer in the background?

191 views
Skip to first unread message

Kay Z.

unread,
Mar 12, 2017, 11:01:56 PM3/12/17
to vim_use
Hi Vim folks,

I'm writing a plugin, and I'm wondering whether it's possible to update a hidden buffer. The functions and commands I found in the docs, such as seem to work only with the current buffer.

The reason I want to do this is that my plugin used a buffer to show some interactive output, and I want the buffer content to always be up-to-date, whether it's hidden or not.

Currently I made things work by switching the buffers when needed:

function! vlime#ui#WithBuffer(buf, Func)
let old_buf = bufnr('%')
let cur_buf = bufnr(a:buf)
try
execute 'hide buffer ' . cur_buf
return a:Func()
finally
execute 'buffer ' . old_buf
endtry
endfunction

But this messed up the cursor position history, and that's very bad.

So, any suggestions?


Regards,

Kay Z.

Kay Z.

unread,
Mar 14, 2017, 1:18:34 PM3/14/17
to vim_use
Hmm, seems no one's interested, and the most recent discussion that I can find on this topic goes back to 2015:

https://github.com/neovim/neovim/issues/2750

There may have been little motivation to update buffers in the background, but with the async channel APIs in place, I think we need such functionality now. All the current solutions (that I'm aware of) are basically dirty hacks.

IIRC, the Python API can update any existing buffer, not just the current one. So why can't we have this nice ability in Vimscript? Are there any special reasons?

tooth pik

unread,
Mar 14, 2017, 3:40:37 PM3/14/17
to vim...@googlegroups.com
if the hidden buffer you want to update was started with the --servername argument you can send it commands using the --remote-send option


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bram Moolenaar

unread,
Mar 14, 2017, 5:39:39 PM3/14/17
to vim...@googlegroups.com
Kay Z wrote:
It's doable, depending on the conditions.  E.g., no Autocommands are
triggered.  Then setline() and getline() could take a buffer argument.
And it only works if the buffer is loaded.

I have been wondering about these "no autocommands" operations, since it
makes a cleaner interface to just manipulate the text without side
effects.  And we don't need to switch buffers/windows, which also has
lots of side effects.

Would you need more than setline() and getline()?

-- 
hundred-and-one symptoms of being an internet addict:
120. You ask a friend, "What's that big shiny thing?" He says, "It's the sun."

 /// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

Christian Brabandt

unread,
Mar 14, 2017, 5:41:38 PM3/14/17
to vim...@googlegroups.com
Hi Bram!

On Di, 14 Mär 2017, Bram Moolenaar wrote:

> Would you need more than setline() and getline()?

We already have getbufline() but are missing setbufline()

Best,
Christian
--
Arbeit bewahrt uns vor drei großen Übeln: der Langeweile, dem Laster
und der Not!
-- François Marie Voltaire

Kay Z.

unread,
Mar 14, 2017, 10:21:13 PM3/14/17
to vim_use
Hi Bram,

Thanks for your reply.

> I have been wondering about these "no autocommands" operations, since it
> makes a cleaner interface to just manipulate the text without side
> effects.  And we don't need to switch buffers/windows, which also has
> lots of side effects.

Can't agree more. I found my self fighting with side effects quite often.

> Would you need more than setline() and getline()?

Now that you asked, I examined my code again, and found myself doing these things to background buffers:

1) Read text
2) Append text
3) Delete text
4) Search text
5) Get the number of lines in a buffer
6) Define <buffer> key mappings

These operations, except 1), have no API or command for background buffers. I think 6) can be achieved by using custom filetype plugins, so it's 2), 3), 4) and 5) that truly matters.

It would be really nice if Vim can support all these operations for all buffers. But now I'm wondering, since the current buffer has quite a complete API, and Vim has closures and lambdas now, will it be easier and less intrusive to provide side-effect-free buffer/window switching operations? For example:

:call withbuffer(42, function('UpdateBuffer'))

The withbuffer() function temporarily switches to buffer 42, calls the passed function, then restores the old buffer before it returns, and doesn't do redraws or trigger autocmds or update the jump list etc.

I'm sorry if there are false optimism/assumptions. I'm not quite familiar with the Vim source code.

Regards,

Kay Z.

Kay Z.

unread,
Mar 14, 2017, 10:32:54 PM3/14/17
to vim_use
Hi toothpik,

> if the hidden buffer you want to update was started with the --servername argument you can send it commands using the --remote-send option
>

I don't think this is what I'm looking for...

Yegappan Lakshmanan

unread,
Mar 15, 2017, 1:15:08 AM3/15/17
to vim...@googlegroups.com
Hi,

On Tue, Mar 14, 2017 at 2:41 PM, Christian Brabandt <cbl...@256bit.org> wrote:
> Hi Bram!
>
> On Di, 14 Mär 2017, Bram Moolenaar wrote:
>
>> Would you need more than setline() and getline()?
>
> We already have getbufline() but are missing setbufline()
>

I submitted a patch back in 2003 to add the setbufline() function.
Refer to the following e-mail threads:

https://marc.info/?l=vim-dev&m=104308972718854&w=3
http://vim.1045645.n5.nabble.com/Re-missing-setbufline-td1200781.html

But there were problems in changing the lines in a buffer. So this was
not incorporated at that time.

Looking at the latest todo.txt, I see the following patches:

Patch to add setbufline(). (email from Yasuhiro Matsumoto, patch by Ozaki
Kiichi, 2016 Feb 28)
Update Mar 8: https://gist.github.com/mattn/23c1f50999084992ca98
Update Mar 13: https://gist.github.com/mattn/23c1f50999084992ca98

- Yegappan

Christian Brabandt

unread,
Jun 22, 2017, 4:57:52 AM6/22/17
to vim...@googlegroups.com, vim...@vim.org
It would be great, if this could be merged. Note, I haven't checked the
patch, however I notice that when using job_start(cmd, 'out_buf': bufnr)
it does already update a buffer even if the cursor is on a different
buffer. So it looks like it shouldn't be too hard to have the same
functionality in a new setbufline() vimscript function available.

Best,
Christian
Reply all
Reply to author
Forward
0 new messages