I want vim (MacVim) to default to a large font for new/short files and dynamically scale down to a smaller font (to a set minimum) as the number of lines grows. Is this possible to do with a plugin? What vim concepts will I need to know to write that plugin?
My reason for wanting this is that I love writing code in a large font, but after files get longer I'd rather squint a little than scroll.
That's an interesting idea. Not sure if I'd use it :-) — but it's certainly an interesting idea.
You don't need to write a complete plugin, as all it needs to do is to perform some math. More specifically, a rough formula would be:
Where the desired size (S) depends on the current document number of lines (n), a constant determining what is considered a big file (k, in lines), the desired amplitude (a) — meaning how much will the size vary — and a minimum font size (m).
Now that we know that, it's just a matter of implementing it. Quick notes:
line()
function passing "$"
as argumentexec
With that in mind, a quick function quite descriptive could be written as:
function! DetermineFontSize()
let bigFile = 200
let nLines = line("$")
let rate = (nLines > bigFile) ? 0 : (1-nLines/(bigFile*1.0))
exec "set guifont=Menlo:h".float2nr(ceil((rate*5)+11))
endfunction
I'm sure that other Vim masters can improve this a lot. Anyway, a quick explanation:
Put that in your .vimrc or source it from other file and you'll be ready to test it. On a file with one line, the font is set to 16. If there are 39 lines, also size 16 but size 15 if there are 40. Size goes to 14 when there are 80 lines and so on.
You probably want to call this automatically, so create an auto command as well.
autocmd BufEnter * call DetermineFontSize()
This will work only when you enter a buffer, as the name says. You could change that to include InsertLeave
or something like, but keep in mind this will generate more calls to the function. Should not be a performance problem though.
Check :h autocommand-events
and build the autocmd
as you like.
As ZyX pointed out in the comments, the last line from the function could be written as:
let &guifont='Menlo:h'.float2nr(ceil((rate*5)+11))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With