Wednesday, July 12, 2017

Re: highlight items in red that are past due

Hi,

Christian Brabandt schrieb am 12.07.2017 um 09:18:
>
> On Mo, 10 Jul 2017, Robert wrote:
>
>> I have a specific tag "due:YYYY-MM-DD" in my files.
>>
>> Is it possible for a function to compare all the matches it finds to
>> today's date and if the "due:" is is past, to highlight it?
>
> You can do something like this.
>
> func! HighlightMatches(substr)$
> " Should also be done here:
> " 1) Make sure, substr is actually a date
> " 2) clear previous highlighting using matchdelete()
> try
> let cur=localtime()$
> let match=system('date --date="'.a:substr.'" +%s')$
> if cur > match$
> call matchadd('Todo', a:substr)$
> endif$
> catch
> " handle errors
> finally
> return a:substr$
> endtry
> endfunc$
>
> Then you can use e.g.
>
> :%s/due:\zs\d\{4}-\d\d-\d\d/\=HighlightMatch(submatch(0))/g
>
> There doesn't seem to be a way to have Vim create a date for a given
> string, so I had to use the shell's date command. Unfortunately, because
> of using system() one cannot use the 'n' flag, because this is not
> allowed in the sandbox.
>
> All in all, this would make a nice plugin.

as long as the matched date is in ISO 8601 format -- or at least can be
brought into year-month-day order -- you can compare the string
representation of two dates directly:

func! HighlightMatches(substr)
" Should also be done here:
" 1) Make sure, substr is actually a date in year-month-day order
" 2) clear previous highlighting using matchdelete()
try
let cur = strftime("%Y-%m-%d')
if cur > a:substr$
call matchadd('Todo', a:substr)$
endif$
catch
" handle errors
finally
return a:substr$
endtry
endfunc$

Of course, this only works if strftime() is actually available. The
format string for strftime() might also need adjustment to the actual
system.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment