From 5292cf4d78fdd39743518ad83e264e7e3cc0686d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 6 Oct 2021 19:20:25 +0200 Subject: projects/aodocs.vim: Make `s:TodoCopyLast()` work for second entry When only a single TODO day entry exists in a file, this function would fail with a "pattern not found" error because the copy pattern expects at least two entries in a file. Alter the function so that it also works when only one entry is present in the file. --- projects/aodocs.vim | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'projects') diff --git a/projects/aodocs.vim b/projects/aodocs.vim index b965ce4..971d3c8 100644 --- a/projects/aodocs.vim +++ b/projects/aodocs.vim @@ -90,13 +90,24 @@ endfunction function! s:TodoCopyLast() - " Copy the last entry to the bottom - ?\n\n\d?,$t$ - - " Move to date line - ?\n\n\zs\d? - - " Increment day, then put the current entry at the top of the window - call setline('.', strftime('%Y.%m.%d:')) - execute "normal! zt2\" + try + " Copy the last entry to the bottom + ?\n\n\d?,$t$ + + " We only have a single entry (pattern not found error). + catch /^Vim\%((\a\+)\)\?:E486: .*\\n\\n\\d/ + " Add two lines to the end of the file. + call append('$', ['', '']) + + " Copy the only entry to the bottom. + 0,$-2t$ + + finally + " Move to date line + ?\n\n\zs\d? + + " Increment day, then put the current entry at the top of the window + call setline('.', strftime('%Y.%m.%d:')) + execute "normal! zt2\" + endtry endfunction -- cgit v1.2.3