开发者

Dynamic vim font size based on file length

开发者 https://www.devze.com 2023-03-08 13:33 出处:网络
I want vim (MacVim) to defa开发者_如何学编程ult 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

I want vim (MacVim) to defa开发者_如何学编程ult 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:

Dynamic vim font size based on file length

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:

  1. To get n we can call the line() function passing "$" as argument
  2. To set the font size, after we have the number we can build a string and execute it with exec

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:

  1. Set up what we call big file. I've chossen 200 lines for debugging purposes, you probably want a bigger number.
  2. Get the number of lines in the current file.
  3. Do the parenthesis in the previous formula. Note that there is a conditional involved (if you noticed I missed that in the formula, congratulations!). If we have more lines than the maximum constant, 0 is returned. Otherwise, we'd have a negative number — plus calculating something that's obvious.
  4. In the fourth line we build the string to be executed while completing the formula. I choose to hardcode the values for a and m here, since they are used only once and it's easy to modify them. Here a is 5 and m is 11, meaning the font will vary between 11 and 16. The syntax I used here to set the font is valid for Mac. If another reader uses another system you may want to change it accordingly.

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.


Update

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


vim executes in a terminal. Font size is terminal dependant, so what you ask may be impossible unless your vim ask directly the terminal to change font size... which may be difficult to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消