Vim:在光标上测试模式的最有效方法?

时间:2023-01-01 22:23:45

I have a problem with matching text in Vim buffers. I have a specific form of dates in my text and often need to test whether the text at cursor matches the date pattern.

我在Vim缓冲区中匹配文本时遇到问题。我的文本中有一种特定的日期形式,通常需要测试光标处的文本是否与日期模式匹配。

Here are some examples of the formatted dates, which may be preceded or followed by other text on the same line:

以下是格式化日期的一些示例,可以在同一行上的其他文本之前或之后:

<2011-10-13 Wed>
[2011-10-13 Wed]
<2011-10-13 Wed +1>
[2011-10-13 Wed +1]
<2011-10-13 Wed 10:30>
[2011-10-13 Wed 10:30]
<2011-10-13 Wed 10:30-11:30>
[2011-10-13 Wed 10:30-11:30]
<2011-10-13 Wed 10:30 +1w>
[2011-10-13 Wed 10:30 +1w]

I have some code that tests at cursor position to see whether cursor is on a date, and if so what the date text is, but what I'm doing seems kind of clumsy.

我有一些代码在光标位置测试以查看光标是否在某个日期,如果是,那么日期文本是什么,但我正在做的事情似乎有点笨拙。

Any comments on what most efficient function for returning the date under cursor (or empty string if not on a date)? (I would post my code but will refrain for now both (1) out of embarrassment and (2) to avoid polluting your minds by suggesting a particular approach to the problem.)

有关在光标下返回日期的最有效函数的任何注释(如果不是在日期,则为空字符串)? (我会发布我的代码,但现在不会因为尴尬而(1)避免尴尬,并且(2)通过建议对问题的特定方法来避免污染你的思想。)

Thanks for any tips.

谢谢你的任何提示。

1 个解决方案

#1


5  

function! CheckDate(expr)
    let date_pattern = '[\[<]'      " The first < or [
                \.'\d\{4\}-\d\{2\}-\d\{2\}' " YYYY-MM-DD
                \.' \(Mon\|Tue\|Wed\|Thu\|Fri\|Sat\|Sun\)' " Week day
                \.'\( \d\{2\}:\d\{2\}\(-\d\{2\}:\d\{2\}\)\?\)\?'
                                    " Optional time or range
                \.'\( +\dw\?\)\?'   " Optional + digit with optional 'w'
                \.'\([\]>]\)'     " The closing > or ]
    return match(a:expr, date_pattern) != -1
endfunction

function! IsOverDate()
    call setreg('a','')
    call setreg('b','')
    normal! "aya>
    let expr1 = getreg('a')
    normal! "bya]
    let expr2 = getreg('b')
    if CheckDate(expr1)
        return expr1
    elseif CheckDate(expr2)
        return expr2
    endif
    return ''
endfunction

The function IsOverDate() clears the registers a and b and stores in the respective registers the text under the cursor which is inside < and > and < and >, including the brackets. Then, it gets the value from the registers a and b and sends it to the function CheckDate() which checks if the expression matches the date pattern (I have based myself in your samples and made some assumptions to build the pattern).

函数IsOverDate()清除寄存器a和b,并在相应的寄存器中存储光标下的文本,该文本位于 <和> 和 <和> 内,包括括号。然后,它从寄存器a和b中获取值并将其发送到函数CheckDate(),该函数检查表达式是否与日期模式匹配(我已将自己基于您的样本并做出一些假设来构建模式)。

The CheckDate() function returns true only if the expression matches the date pattern. The function IsOverDate() returns the date under the cursor (with the brackets) or an empty string if the cursor is not over a date.

仅当表达式与日期模式匹配时,CheckDate()函数才返回true。函数IsOverDate()返回光标下的日期(带括号),如果光标不在日期上,则返回空字符串。

I hope it suits.

我希望它适合。

#1


5  

function! CheckDate(expr)
    let date_pattern = '[\[<]'      " The first < or [
                \.'\d\{4\}-\d\{2\}-\d\{2\}' " YYYY-MM-DD
                \.' \(Mon\|Tue\|Wed\|Thu\|Fri\|Sat\|Sun\)' " Week day
                \.'\( \d\{2\}:\d\{2\}\(-\d\{2\}:\d\{2\}\)\?\)\?'
                                    " Optional time or range
                \.'\( +\dw\?\)\?'   " Optional + digit with optional 'w'
                \.'\([\]>]\)'     " The closing > or ]
    return match(a:expr, date_pattern) != -1
endfunction

function! IsOverDate()
    call setreg('a','')
    call setreg('b','')
    normal! "aya>
    let expr1 = getreg('a')
    normal! "bya]
    let expr2 = getreg('b')
    if CheckDate(expr1)
        return expr1
    elseif CheckDate(expr2)
        return expr2
    endif
    return ''
endfunction

The function IsOverDate() clears the registers a and b and stores in the respective registers the text under the cursor which is inside < and > and < and >, including the brackets. Then, it gets the value from the registers a and b and sends it to the function CheckDate() which checks if the expression matches the date pattern (I have based myself in your samples and made some assumptions to build the pattern).

函数IsOverDate()清除寄存器a和b,并在相应的寄存器中存储光标下的文本,该文本位于 <和> 和 <和> 内,包括括号。然后,它从寄存器a和b中获取值并将其发送到函数CheckDate(),该函数检查表达式是否与日期模式匹配(我已将自己基于您的样本并做出一些假设来构建模式)。

The CheckDate() function returns true only if the expression matches the date pattern. The function IsOverDate() returns the date under the cursor (with the brackets) or an empty string if the cursor is not over a date.

仅当表达式与日期模式匹配时,CheckDate()函数才返回true。函数IsOverDate()返回光标下的日期(带括号),如果光标不在日期上,则返回空字符串。

I hope it suits.

我希望它适合。