Monday, November 26, 2012

how to handle 'statusline' content with a variable ?

Hi,

In my .vimrc, I conditionally set the statusline, and store it into a variable
to use this variable later in autocommands, like this:

========== BEGIN =================================================================
[...]
set statusline&
if &filetype!="help" && &filetype!="netrw" && &filetype!="nerdtree" && &filetype!="conque_term" && &filetype!="qf"
" Display (truncated) full path of the file with 'User1' highlight settings:
setlocal statusline+=%1*%<%F
" Add 'modified' and 'readonly' optional flags with 'User2' highlight settings:
setlocal statusline+=%2*%m%r%1*
" Divider of the line (what follows it is right aligned):
setlocal statusline+=%=
" Display the number of the buffer with 'User3' highlight settings:
setlocal statusline+=%3*%a%1*
" Align and display the file type (or 'none') with 'User4' highlight settings:
setlocal statusline+=%16.(%4*[%{strlen(&filetype)?&filetype:'none'}]%)
" Display file format and file encoding (or default encoding) with 'User5'
" highlight settings:
setlocal statusline+=%5*[%{&fileformat},\ %{strlen(&fileencoding)?&fileencoding:&encoding}]%1*
" Align and display ASCII and Hex values of the character under the cursor
" with 'User6' highlight settings:
setlocal statusline+=%16.(%6*<%03.5b>\ <0x%02.4B>%)%1*
" Align and display the position of the cursor with 'User7' highlight settings:
setlocal statusline+=%16.(%7*%l/%L,%c%V%)\ (%P)
endif
" Store it into a variable to use it later in autocommands; don't forget
" to escape spaces and percent signs, and escape the backslashes themselves:
[The following is on one line:]
let local_stl="\\%1*\\%<\\%F\\%2*\\%m\\%r\\%1*\\%=\\%3*\\%a\\%1*\\%16.(\\%4*[\\%{strlen(&filetype)?&filetype:'none'}]\\%)\\%5*[\\%{&fileformat},\\ \\%{strlen(&fileencoding)?&fileencoding:&encoding}]\\%1*\\%16.(\\%6*<\\%03.5b>\\ <0x\\%02.4B>\\%)\\%1*\\%16.(\\%7*\\%l/\\%L,\\%c\\%V\\%)\\ (\\%P)"
[...]
autocmd BufLeave,TabLeave,WinLeave * setlocal statusline&
autocmd BufEnter,TabEnter,WinEnter * if &filetype=="help" || &filetype=="netrw" || &filetype=="nerdtree" || &filetype=="conque_term" || &filetype=="qf" | setlocal statusline& | else | exe "setlocal statusline=" . local_stl | endif
[...]
========== END ===================================================================

Then, the default statusline is displayed for 'help', 'nerdtree', 'quickfix', and so on, and the custom statusline is displayed for all other filetypes, but only if it is the active buffer/tab/window. All this works as I want. But if I want to modify something in the very clear (and commented) 'setlocal statusline+=' lines, I have to do the same changes in the obscure 'local_stl' variable. So, what I need is a way to define 'local_stl' by using the '&statusline' content instead of manually; more precisely: how to handle '&statusline' to add escape characters and put the result in my 'local_stl' variable ? I have tried:

let local_stl=substitute(&statusline, "\([% ]\)", "\\\\\1", "g")

but this leads to an error:
Error detected while processing BufEnter Auto commands for "*":
E518: Unknown option: %{strlen(&fileencoding)?&fileencoding:&encoding}]%1*%16.(%6*<%03.5b>
Error detected while processing TabEnter Auto commands for "*":
E518: Unknown option: %{strlen(&fileencoding)?&fileencoding:&encoding}]%1*%16.(%6*<%03.5b>
Error detected while processing WinEnter Auto commands for "*":
E518: Unknown option: %{strlen(&fileencoding)?&fileencoding:&encoding}]%1*%16.(%6*<%03.5b>

and the related part of the statusline (and all what follows) is not displayed.
It seems the problem is that the space characters behave as separators in the 'local_stl' when used as argument for the 'exe' command, even if they have been escaped before. I don't know how to deal with that.

Idea?
Thanks,
quidame

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

No comments:

Post a Comment