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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Kay Zheng
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.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Kay Zheng
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?

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

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

On Tue, Mar 14, 2017 at 12:18 PM, Kay Z. <[hidden email]> wrote:
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?

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Bram Moolenaar-2
In reply to this post by Kay Zheng
Kay Z wrote:

On 14 March 2017 at 18:18, Kay Z. <[hidden email]> wrote:
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?

 
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 -- [hidden email] -- 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    ///

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Christian Brabandt
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

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Kay Zheng
In reply to this post by Bram Moolenaar-2
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.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Kay Zheng
In reply to this post by toothpik
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...

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Yegappan Lakshmanan
In reply to this post by Christian Brabandt
Hi,

On Tue, Mar 14, 2017 at 2:41 PM, Christian Brabandt <[hidden email]> 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

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Christian Brabandt

On Di, 14 Mär 2017, Yegappan Lakshmanan wrote:

> On Tue, Mar 14, 2017 at 2:41 PM, Christian Brabandt <[hidden email]> wrote:
> > 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

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

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.