1

I want to realize a different color in my statusline if a file has no utf8 encoding.

This is what I use now:

set statusline+=%3*\ F:%{&fileencoding?&fileencoding:&fileencoding} 

hi User3 guifg=#292b00  guibg=#f4f597

This is what I want to realize:

set statusline+=%{Fenc()}*\ F:%{&fileencoding?&fileencoding:&fileencoding}
function! Fenc()
    if &fenc !~ "utf-8"
        return "4"
    else
        return "3"
    endif
endfunction

hi User3 guifg=#292b00  guibg=#f4f597
hi User4 guifg=#ff0000  guibg=#f4f597 

Why doesn't this work?

Reman
  • 7,931
  • 11
  • 55
  • 97

1 Answers1

1

first of all, in your code:

%{&fileencoding?&fileencoding:&fileencoding} 

makes no sense, it is like, if a is there, I write a, otherwise I write a anyway.

I guess you want to have &fenc?&fenc:&enc

I don't think you can evaluate the function and then put with % together with set stl, but you can build your function in this way:

hi User3 ....
hi User4 ....
function! MkStatusLine()
    if &fenc == "utf-8"     
        set statusline=%4*
    else
        set statusline=%3*
    endif
    set statusline+=Here you made your magic status line info text
endfunction

then call the function when you load buffer.

Edit

add how it worked in terminal:

enter image description here

Community
  • 1
  • 1
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thank you for replying. Maybe I didn't understand your answer. This didn't work. `set statusline+=%{CheckUtf8()}\ function! CheckUtf8() if &fenc == "utf-8" set statusline+=%4*\ %{&fenc}\ else set statusline+=%3*\ %{&fenc}\ endif " set statusline+=Here you made your magic status line info text endfunction` – Reman Aug 07 '14 at 10:57
  • don't use `%{CheckUtf8()}\` in `set stl`! you changed my codes. `=` -> `+=` make a small example, test with my codes, it should work. – Kent Aug 07 '14 at 10:59
  • I can't make it work. It gives an empty yellow colored statusline with every kind of fileencoding. – Reman Aug 07 '14 at 11:25
  • @Remonn add an animation to show how it worked in terminal. (I didn't set gui* colors) but you can see, the color changed. – Kent Aug 07 '14 at 12:44
  • Maybe it works in terminal but I can't make it work in gvim. It just returns an empty yellow colored statusline with every kind of fileencoding. :( – Reman Aug 07 '14 at 13:47
  • @Remonn they are the same. only the `hi ...` different. I don't want to make another gif to show it will work in gvim. – Kent Aug 07 '14 at 13:50
  • Kent, call the function when you load buffer: Do you mean with an autoload command? I just added this in vimrc: `:call MkStatusLine()`, together with the function as indicated above. – Reman Aug 07 '14 at 13:54
  • I showed that in the gif, maybe it was too fast? `au BufEnter * call XXX()` – Kent Aug 07 '14 at 13:55
  • Yes that did it :) Sorry Kent. I did not note the autocommand. – Reman Aug 07 '14 at 14:04
  • There is no problem to call other functions within MkStatusLine()/Fenc() isn't it? p.e. I call a function from the statusline which returns the current language, spellcheck info etc. Is it slower to use an autocommand or is it the same as `set stl`? I don't understand very well the difference between both. – Reman Aug 07 '14 at 14:19
  • 1
    @Remonn should be fine. do some test – Kent Aug 07 '14 at 14:20