Thursday, December 22, 2011

Re: condition in statusline, how ?

On 23/12/11 03:52, stardiviner wrote:
> I want to make a condition in statusline setting.
> for example: when fileformat is unix, show "unix" with black color in statusline, if fileformat is
> "dos", then show in statusline with *red* color.
> here is my statusline setting in my vimrc file:
>
> <code>
> " item form: %-0{minwid}.{maxwid}{item} --> minwid: min-width,
> " %4 width, %-4 padding on the right instead of the left. %= switch to right side.
> " %04l pad with zeros instead of spaces. like 0012.
> " %.20F change the maximum width. The path will be truncated if necessary:<hapters/17.markdown.
> " Require: fugitive, current-func-info,
> set statusline=\%#StatusSeparator#\|\%#StatusBufferNumber#\b\:%n\%#StatusSeparator#\|
> \\%#StatusReadOnly#\ \%r\ \%#StatusModFlag#\%m\ \%#StatusList#\%q
> \\ %=\ %<%-15(\%#StatusNormal#\%{&ff}\%#StatusChar#\X\%#StatusNormal#\%{&fenc}%)
> \\ \%#StatusChar#\X\ \%#StatusLineNumber#\%-(L:%l/%L\ C:%c%V%)\ \%#StatusPercent#\%P
> "" FIXME write this function.
> "function StatusLineFileFormat(ff)
> "if&ff == 'unix'
> "return a:ff
> "elseif&ff == 'dos'
> "return a:ff " show with red color in statusline.
> "endif
> "endfunction
> </code>
>
> I do not know vimscript language, so I do not know how to write upper StatusLineFIleFormat()
> function. :P
>

The problem is not easy.

You can display any expression on the statusline by entering between %{
and } in the 'statusline' code. Or you can set any rainbow of colours by
using either %#HighlightName# or (for User1 to User9) %1* to %9* or (to
return to StatusLine or StatusLineNC) %*, but all these come at the same
level as %{} and are thus not subject to expression evaluation.

Maybe include a distinct field for each possible 'fileformat', which
would resolve to the empty string if the file has a different
'fileformat', like this (showing only the relevant part of the ":set
statusline=..." statement) (all quotes are single quotes) (untested)

%1*%{&ff=='unix'?'[u*x]':''}%2*%{&ff=='dos'?'[dos]':''}%3*%{&ff\ !=\
'unix'\ &&\ &ff\ !=\ 'dos'?'['\ .\ &ff\ .\ ']':''}%*

and somewhere else (possibly in a custom colorscheme if you have one)

hi User1 ctermbg=black ctermfg=white guibg=black guifg=white
hi User2 ctermbg=red ctermfg=white guibg=red guifg=white
hi User3 ctermbg=yellow ctermfg=red guibg=yellow guifg=red

which would display one of the following:

[u*x] (in white on black) if 'fileformat' is unix
[dos] (in white on red) if 'fileformat' is dos
or otherwise the 'fileformat' value between brackets (probably [mac]) in
red on yellow

without even the need for a function.

This of course assumes that you're not going to find yourself on a
monochrome terminal with only "black" "lightgrey" "darkgrey" and "white"
(but possibly in amber or green): for such a terminal you would need
term= highlight settings but it's been years (decades maybe) since I've
seen one in operation.


Best regards,
Tony.
--
... so long as the people do not care to exercise their freedom, those
who wish to tyrranize will do so; for tyrants are active and ardent,
and will devote themselves in the name of any number of gods, religious
and otherwise, to put shackles upon sleeping men.
-- Voltarine de Cleyre

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