有人能解释一下 Vim 中“foldexpr"语法的特定用例吗? [英] Could someone explain a particular use case of ‘foldexpr’ syntax in Vim?

查看:43
本文介绍了有人能解释一下 Vim 中“foldexpr"语法的特定用例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以对以下示例中的语法进行解释,或者给我发一个链接,其中对该表达式中使用的各个符号有更一般的解释吗?我发现 Vim 帮助在这方面不完整.

Could anyone please provide an explanation of the syntax in the following example, or post me a link where there is a more general explanation of the individual symbols used in this expression? I found Vim help to be incomplete in this regard.

:set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1

我不清楚的是以下内容.

What is unclear to me is the following.

  1. 为什么字符串用单引号而不是双引号括起来?这是一个选择问题吗?
  2. =~'\\S'?'<1':1中的解释标记是什么意思?
  3. 表达式'string1'string2'string3是什么意思?
  4. :1 是什么意思?
  1. Why are strings enclosed in single quotes instead of double quotes? Is it a matter of choice?
  2. What does the explanation mark mean in =~'\\S'?'<1':1?
  3. What does the expression 'string1'string2'string3 mean?
  4. What does :1 mean?

推荐答案

foldexpr 选项应该包含一个计算表达式转换成指定折叠的整数或特定格式的字符串v:lnum 全局变量中存储数字的行的级别在评估的时候.

The foldexpr option supposed to contain an expression that evaluates into an integer or a string of particular format that specifies the folding level of the line which number is stored in the v:lnum global variable at the moment of evaluation.

让我们从上到下遵循这个 foldexpr 示例的逻辑.

Let us follow the logic of this foldexpr example from top to bottom.

getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1

在顶层,整个表达式是三元组的一个实例运算符 <代码>A ?乙:C.运算符的结果是B 表达式如果 A 的计算结果为非零,并且否则C 表达式(参见:help expr1).在这种情况下,B 是字符串文字 '<1'C 是数字 1(表示'<1'1 作为折叠级别说明符,请参阅 :help fold-expr).

At the top level, the whole expression is an instance of the ternary operator A ? B : C. The result of the operator is the value of the B expression if A evaluates to non-zero, and the value of the C expression otherwise (see :help expr1). In this case, B is the string literal '<1', and C is the number 1 (for meaning of '<1' and 1 as fold level specifiers see :help fold-expr).

A 表达式由两个条件组成&& 运算符:

The A expression consists of two conditions joined by the && operator:

getline(v:lnum) =~ '^\\s*$' && getline(v:lnum+1) =~ '\\S'

两个条件具有相同的形式:

Both conditions have the same form:

getline(N) =~ S

getline 函数返回行的内容(在当前缓冲区)由作为参数传递的行号引用(参见 :help getline).当对 foldexpr 求值时,v:lnum变量包含应折叠级别的行号计算.

The getline function returns contents of the line (in the current buffer) that is referenced by the line number passed as an argument (see :help getline). When the foldexpr is evaluated, the v:lnum variable contains number of the line for which folding level should be calculated.

=~ 运算符测试其左操作数是否与正则匹配表达式由其右字符串操作数给出,并返回布尔值(参见 :help expr4,特别是靠近 expr4 部分末尾的部分).因此,A 条件旨在检查 v:lnum-th 行匹配 '^\\s*$' 模式,以及 v:lnum-th 后面的行行匹配 '\\S' 模式.

The =~ operator tests whether its left operand matches a regular expression given by its right string operand, and returns boolean value (see :help expr4, in particular, near the end of the expr4 section). Thus, the A condition is intended to check that the v:lnum-th line matches the '^\\s*$' pattern, and the line following that v:lnum-th line matches the '\\S' pattern.

正则表达式模式在表达式中指定为字符串文字.字符串文字有两种语法形式,可以是使用双引号或单引号引用.这些之间的区别形式是双引号字符串可以包含各种控件以反斜杠开头的序列.该序列允许指定否则无法轻松输入的特殊字符(双引用,例如——它写成 \").单引号字符串,在另一方面,不允许这样的反斜杠序列.(为了完成单引号和双引号字符串的描述见:help expr-string:help literal-string.)

The regular expression patterns are specified in the expression as string literals. String literals have two syntactic forms and can be quoted using double or single quotes. The difference between these forms is that double quoted string could contain various control sequences which start with backslash. That sequences allow to specify special characters that cannot be easily typed otherwise (double quote, for example—it writes \"). Single quoted strings, at the other hand, do not allow such backslash-sequences. (For complete description of single and double quoted strings see :help expr-string and :help literal-string.)

双引号字符串语法的显着后果是反斜杠符号本身必须被转义 (\\).这就是为什么单身带引号的字符串通常用于指定正则表达式:有没有必要不断地要求转义反斜杠符号.一罐但是请注意,反斜杠在那些上面的图案.这是由于某些符号(包括反斜杠)在 Ex 命令(包括 :set, of课程).当您按 Enter 启动命令时

The notable consequence of the double quoted strings syntax is that backslash symbol itself must be escaped (\\). That is why single quoted strings are often used to specify regular expressions: there is no need to escape constantly demanded backslash symbol. One can notice, though, that backslashes are nevertheless escaped in those patterns above. This is due to that some symbols (including backslash) have special meaning when in Ex commands (including :set, of course). When you hit Enter to start the command

:set foldexpr=...

Vim 首先解释一些字符序列(见 :help cmdline-special).特别是,\\ 序列被视为单个反斜杠.

Vim interprets some character sequences first (see :help cmdline-special). In particular, the \\ sequence is treated as a single backslash.

综合起来,表达式测试行号是否v:lnum 只包含空白字符以及以下行是否(number v:lnum+1) 有任何非空白字符(参见 :help pattern掌握图案的含义).如果是,则表达式计算到字符串 '<1',否则计算结果为数字 1.

Putting it all together, the expression tests whether the line number v:lnum contains only blank characters and whether the following line (number v:lnum+1) has any non-blank character (see :help pattern to grasp the meaning of the patterns). If so, the expression evaluates to the string '<1', otherwise it evaluates to the number 1.

这篇关于有人能解释一下 Vim 中“foldexpr"语法的特定用例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆