Explaining foldexpr syntax

女生的网名这么多〃 提交于 2019-11-30 11:20:12

The foldexpr option supposed to contain an expression that evaluates into an integer number or 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.

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

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

At the top level, the whole expression is a 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).

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

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.

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 next to v:lnum-th one matches the '\\S' pattern.

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

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 interprets some character sequences first (see :help cmdline-special). In particular, the \\ sequence is treated as a single backslash.

Putting it all together, the expression tests whether the line number v:lnum contains only blank characters while the line next to it (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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!