基于文件长度的动态 vim 字体大小

发布于 2024-11-09 20:06:53 字数 151 浏览 4 评论 0原文

我希望 vim (MacVim) 默认为新/短文件使用大字体,并随着行数的增加动态缩小到较小的字体(到设定的最小值)。这可以通过插件来实现吗?我需要了解哪些 vim 概念才能编写该插件?

我想要这个的原因是我喜欢用大字体编写代码,但是当文件变长后我宁愿眯着眼睛也不愿滚动。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-11-16 20:06:53

这是一个有趣的想法。不确定我是否会使用它:-)——但这确实是一个有趣的想法。

您不需要编写完整的插件,因为它所需要做的就是执行一些数学运算。更具体地说,一个粗略的公式是:

在此处输入图像描述

其中所需的大小 (S )取决于当前文档的行数 (n)、确定什么被视为大文件的常量(k,以行为单位)、所需的幅度 ( a)——意味着大小会变化多少——以及最小字体大小(m)。

现在我们知道了这一点,剩下的就是实施它了。快速说明:

  1. 要获得 n,我们可以调用 line() 函数,传递 "$" 作为参数。
  2. 要设置字体大小,我们有我们可以构建一个字符串并使用 exec 执行它的数字

考虑到这一点,一个具有描述性的快速函数可以写成:

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

我确信其他 Vim 高手可以对此进行很大改进。无论如何,快速解释一下:

  1. 设置我们所说的大文件。我选择了 200 行用于调试目的,您可能需要更大的数字。
  2. 获取当前文件中的行数。
  3. 执行前面公式中的括号。请注意,其中涉及一个条件(如果您注意到我在公式中遗漏了该条件,那么恭喜您!)。如果行数多于最大常量,则返回 0。否则,我们会得到一个负数——加上计算一些显而易见的东西。
  4. 在第四行中,我们构建在完成公式时要执行的字符串。我选择在这里对 am 的值进行硬编码,因为它们仅使用一次并且很容易修改它们。这里 a 是 5,m 是 11,这意味着字体将在 11 到 16 之间变化。我在这里用来设置字体的语法对 Mac 有效。如果其他读者使用其他系统,您可能需要相应地更改它。

将其放入您的 .vimrc 中或从其他文件中获取它,您就可以准备测试它了。在只有一行的文件上,字体设置为 16。如果有 39 行,则大小也为 16,但如果有 40 行,则大小为 15。当有 80 行时,大小变为 14,依此类推。

您可能想自动调用它,因此还要创建一个自动命令。

autocmd BufEnter * call DetermineFontSize()

正如名称所示,只有当您输入缓冲区时,这才会起作用。您可以将其更改为包含 InsertLeave 或类似内容,但请记住,这将生成对该函数的更多调用。但不应该是性能问题。

检查 :h autocommand-events 并根据需要构建 autocmd


更新

正如 ZyX 在评论中指出的那样,函数的最后一行可以写为:

let &guifont='Menlo:h'.float2nr(ceil((rate*5)+11))

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:

enter image description here

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))
染年凉城似染瑾 2024-11-16 20:06:53

vim 在终端中执行。字体大小取决于终端,因此您所要求的可能是不可能的,除非您的 vim 直接要求终端更改字体大小...这可能很难做到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文