Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vim-bufferline integration #36

Closed
timss opened this issue Sep 13, 2013 · 16 comments
Closed

vim-bufferline integration #36

timss opened this issue Sep 13, 2013 · 16 comments

Comments

@timss
Copy link

timss commented Sep 13, 2013

How would I go about integrating vim-bufferline to my statusline, somewhat similar to what is done in vim-airline?

I found a part about statusline integration, but not really sure how to do this for lightline so it updates when needed etc.

It doesn't have to change the background if selected and all that jazz, but being able to change the frontground and/or background color of the chosen buffer would definitely be a bonus.

I'd like to note that I like idea with lightline where usage of other plugins should be defined by the user instead letting the plugin do the "magic" or "hiding" it all in the plugin as is done in (vim-)powerline and airline. However adding things like the bufferline seems to be a bit tricky unless you're quite comfortable in vimscript.

@itchyny
Copy link
Owner

itchyny commented Sep 13, 2013

How about the following config?

let g:lightline = {
  \ 'active': {
  \   'left': [ [ 'mode', 'paste' ], [ 'filename' ], [ 'bufferline' ] ],
  \ },
  \ 'component': {
  \   'bufferline': '%{bufferline#refresh_status()}%{g:bufferline_status_info.before . g:bufferline_status_info.current . g:bufferline_status_info.after}'
  \ }
  \ }

@itchyny
Copy link
Owner

itchyny commented Sep 13, 2013

To change the color:

  \   'bufferline': '%{bufferline#refresh_status()}%{g:bufferline_status_info.before}%#TabLineSel#%{g:bufferline_status_info.current}%#LightLineLeft_active_3#%{g:bufferline_status_info.after}'

@timss
Copy link
Author

timss commented Sep 13, 2013

Cool, thanks for the quick reply! Didn't think about looking at other functions in in bufferline. It is also quite appearant that using your model makes it much easier to configure things like this yourself, both for you as the developer of lightline as you don't have to care about other plugins, and for the user since you have the full power to change it to whatever you like. The only "downside" is that my .vimrc is growing a lot.

Ultimately I would like it to replace the filename when applicable (i.e. not when using ctrlp/nerdtree or whatever), and to blend in with the background if not used, so not with []. Somewhat like the demo for vim-airline I guess, but with the colors respecting the background/colorscheme of lightline (which btw is so so easy to edit!).

img

I don't have time to take a look at it right now, so if you're fine with it I'd like to keep this issue open so I can resolve it when I get it to work as described, or post questions if I have. Thanks again for your great work!

@itchyny
Copy link
Owner

itchyny commented Sep 13, 2013

Yeah, "downside", exactly.
Supposing there's n plugins on the Earth, a kind developer will have to support all the plugins.
I am lazy and not so kind (ha ha...), so I've kept my plugin clean, independent from other plugins.
Then, what I had to do was to provide a single API.
A user using m plugins (definitely less than n) have to write some configurations for my unkind plugin.
But I want you all to try writing Vim script in .vimrc, make it longer and longer, and get used to the language.
It is, you know, one of the basic styles of the editor itself!
A kind developer will accept all the desires of the users (like disable a specific component in buffers of some plugin or you want to keep it in other plugin or ...), he will add exponentially lots of options, make his plugin messy.

You want to change the foreground color conditionally (when the selected buffer is modified)?
Respecting the background color of lightline?
Actually, such a color configuration is not supported in lightline.
Precisely speaking, there's no easy API.
Of course I'm wondering if there's a good way to provide such an API, but I haven't yet thought of.
The issue #22 is related to such a color configuration.

@timss
Copy link
Author

timss commented Sep 14, 2013

I don't have a big problem with having a lot in my .vimrc as long as it doesn't get messy or I don't actually need it. So yeah, writing your own functions etc. to work with other plugins isn't a bad idea, however it's important to be able to share with eachother, so maybe add a page to the wiki (or keep the README updated)?

The first example works pretty well, but it would be nice to arbitrarily change the background/foreground color as you like. If included in the group for filename I think it gets too much focus:

vim_lightline_bufferline

Adding it to the middle (left) it looks much better, and this is what I currently use and and I'm happy just using this. However what if you want current to have another foreground color instead of brackets? I tried adding bufferline as a component function instead with substitute(bufferline_status_info.current, '\[\(.*\)\]', '\1', '') etc, but I just get an error that bufferline_status_info.current doesn't exist.

I guess it's because this is run before bufferline is ready/run, but not sure how to solve that. Additionally not sure how one would add colors if using it as a function as such. How would I go about writing the bufferline component as a function? I can post more information if needed (don't want this to be too much of a tl;dr).

vim_lightline_bufferline_2
vim_lightline_bufferline_1

It also needs some function for automatically collapse/resize/hide when the window is too small. If I could get a component function for bufferline to work I could just check the window width (winwidth) as in the examples in the README, and maybe only print the current or something like that.

vim_lightline_bufferline_3

For reference; current lightline configuration: http://bpaste.net/show/HtlhenF5BgGEMTHT6gAb/

@itchyny
Copy link
Owner

itchyny commented Sep 15, 2013

You should look into the document of bufferline more carefully.
If you want to remove the brackets, use

let g:bufferline_active_buffer_left = ''
let g:bufferline_active_buffer_right = ''

You got an error that bufferline_status_info.current doesn't exist:
Use g:bufferline_status_info.current instead of bufferline_status_info.current.
The g: prefix means that the variable is a global one.

You can define the bufferline component as a function using the following config.

let g:lightline = {
        \ 'active': {
        \   'left': [ [ 'mode', 'paste' ], [ 'filename' ], ['bufferline'] ],
        \ },
        \ 'component_function': {
        \   'bufferline': 'MyBufferline'
        \ }
        \ }
function! MyBufferline()
  call bufferline#refresh_status()
  let b = g:bufferline_status_info.before
  let c = g:bufferline_status_info.current
  let a = g:bufferline_status_info.after
  return b . c . a
endfunction

But you cannot change the color of the current buffer from a component function.
If you just care of the width of your vimrc file, use

  \   'bufferline': '%{bufferline#refresh_status()}%{g:bufferline_status_info.before}'.
  \                 '%#TabLineSel#%{g:bufferline_status_info.current}'.
  \                 '%#LightLineLeft_active_3#%{g:bufferline_status_info.after}'

If you want to collapse the component based on the window size, use the following config.

function! MyBufferline()
  call bufferline#refresh_status()
  let b = g:bufferline_status_info.before
  let c = g:bufferline_status_info.current
  let a = g:bufferline_status_info.after
  let alen = strlen(a)
  let blen = strlen(b)
  let clen = strlen(c)
  let w = winwidth(0) * 4 / 9
  if w < alen+blen+clen
    let whalf = (w - strlen(c)) / 2
    let aa = alen > whalf && blen > whalf ? a[:whalf] : alen + blen < w - clen || alen < whalf ? a : a[:(w - clen - blen)]
    let bb = alen > whalf && blen > whalf ? b[-(whalf):] : alen + blen < w - clen || blen < whalf ? b : b[-(w - clen - alen):]
    return (strlen(bb) < strlen(b) ? '...' : '') . bb . c . aa . (strlen(aa) < strlen(a) ? '...' : '')
  else
    return b . c . a
  endif
endfunction

@itchyny
Copy link
Owner

itchyny commented Sep 15, 2013

Still, you want to fulfil the both desire, change return statements of the MyBufferline function to

    return [(strlen(bb) < strlen(b) ? '...' : '') . bb, c, aa . (strlen(aa) < strlen(a) ? '...' : '')]
  else
    return [b, c, a]
  endif

and define the bufferline component as

        \ 'component': {
        \   'bufferline': '%{bufferline#refresh_status()}%{MyBufferline()[0]}'.
        \                 '%#TabLineSel#%{g:bufferline_status_info.current}'.
        \                 '%#LightLineLeft_active_3#%{MyBufferline()[2]}'
        \ },

@timss
Copy link
Author

timss commented Sep 18, 2013

Really appriciate your quick and extensive responses, thanks!
I haven't got time to take a proper look at it yet because lots of school/work, but I'll comment properly on it later this week. Again I hope you're ok with having this issue laying around for a bit.

@itchyny
Copy link
Owner

itchyny commented Sep 18, 2013

OK. I'm a student, too and I understand lots of work makes us busy weekdays.
Close when you are satisfied with the appearance of the statusline.
Good luck with your homework.

@timss
Copy link
Author

timss commented Sep 18, 2013

Great, thanks!

@itchyny
Copy link
Owner

itchyny commented Sep 28, 2013

Hello?

@timss
Copy link
Author

timss commented Sep 30, 2013

Hi, really sorry for the delay. I had a hard time finding time to really sit down and take a proper look at your answer for the longest time. It wasn't my intention to ignore your help, because I really do appriciate it!

The missing g: (global) definition was a silly error, should've catched that. Also I'll get better at looking at the documentation of other plugins before trying to do it the hard way, for example with the brackets as discussed.

Now, the collapsing works really well, the use of "..." was a good fix. Pretty much a drop-in replacement to my own configuration. I did have to change the calculation of when to collapse it according to the window with because I also have the Git branch displayed for some files, e.g. let w = winwidth(0) * 4 / 10. But of course, this depends on my bufferline configuration, branch length name, paste on/off etc. so might be difficult to make a truly universal function. The current solution works just fine!

However the bufferline (tabline) highlighting seems to be somewhat borked. I think I'm going to settle with just using brackets anyway because it's cleaner. But not really sure why the use of tabline would not work so you might want to look into that. Here is the configuration I used when getting this bug (just your example configuration combined with mine). If you can't reproduce it I can see if I can help you further with it.

vim_lightline_bufferline_tabline

My current configuration can be found in this version. Thanks for the help!

@timss timss closed this as completed Sep 30, 2013
@itchyny
Copy link
Owner

itchyny commented Sep 30, 2013

Use LightLineLeft_active_2 instead of LightLineLeft_active_3.

@timss
Copy link
Author

timss commented Sep 30, 2013

Oh, it was that simple. Got it, might be handy if I change my mind.
Thanks again.

itchyny added a commit that referenced this issue Mar 13, 2016
itchyny added a commit that referenced this issue Mar 13, 2016
wilywampa pushed a commit to wilywampa/lightline.vim that referenced this issue Mar 22, 2016
@itchyny
Copy link
Owner

itchyny commented May 29, 2016

Ref: #155 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@itchyny @timss and others