虽然被他们说服了,但我对我的问题给出的答案有点失望。我试图找到一个解决方案,似乎我几乎成功了。不用说,这是一个笨拙的装置,但它确实有效。
首先,文件(出于测试目的稍作修改):
130825 Past ToDo test
130827 Today's ToDo test
130829 In two days ToDo test
130831 Another test
130902 Change of month ToDo test
131025 Another change of month test
二、http ://www.epochconverter.com 给出的数据:
1 day = 86400 seconds
1 month (30.44 days) = 2629743 seconds
1 year (365.24 days) = 31556926 seconds
第三,我修改的功能:
function! DaysLeft()
:normal! gg
let linenr = 1
while linenr <= line("$")
let linenr += 1
let line = getline(linenr)
:normal! 0"ayiw
:.s/\(\(\d\d\)\)\(\d\d\)\(\d\d\)\>/\1
:normal! 0"byiw
:execute "normal! diw0i\<C-R>a"
:normal! 0"ayiw
:.s/\(\d\d\)\(\(\d\d\)\)\(\d\d\)\>/\2
:normal! 0"cyiw
:execute "normal! diw0i\<C-R>a"
:normal! 0"ayiw
:.s/\(\d\d\)\(\d\d\)\(\(\d\d\)\)\>/\3
:normal! 0"dyiw
:execute "normal! diw0i\<C-R>a"
let @l = strftime("%s")
:execute "normal! 0wi\<C-R>=((\<C-R>b+30)*31556926+(\<C-R>c-1)*2629743+(\<C- R>d-1)*86400+1-\<C-R>l)/86400\<Enter>\<tab>"
exe linenr
endwhile
endfunction
四、结果:
130825 -2 Past ToDo test
130827 0 Today's ToDo test
130829 1 In two days ToDo test
130831 3 Another test
130902 5 Change of month ToDo test
131025 58 Another change of month test
如您所见,有一个小故障:130829 ToDo 显示为 1 天而不是 2 天(因为我没有进行浮点计算)。但事实上,我认为这是一个编程故障(除其他外……),但在心理上是一个合理的故障:事实上,我只有一整天的工作可用。
这可能是徒劳的练习,但这让我学习:捕获、循环、寄存器,当然还有以前的 StackOverflow 宝贵答案,以便给出纯 Vim 答案。
感谢您为我的回答带来的所有改进。