Monday, July 10, 2017

Re: highlight items in red that are past due

On Tue, Jul 11, 2017 at 1:28 AM, Robert <sigzero@gmail.com> 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?

Your question can be simplified to "How do I highlight dates that are
not today?"

Vim regex supports what is known as "look behind" pattern - it is a pattern
that tells the regex engine to register as positive match if the previous
sequence of characters matches or does not match a given pattern.

The following search pattern will match all the text "due:YYYY-MM-DD" in your
file (not quite what you want, yet):

due:[0-9]\{4}-[0-9][0-9]-[0-9][0-9]

To tell the regex engine to exclude today's date use the negative look
behind pattern \@!:

due:\(2017-07-11\)\@![0-9]\{4}-[0-9][0-9]-[0-9][0-9]

If you do a search using the above regex it will find all "due:" lines
with dates that doesn't match 2017-07-11.

To highlight the pattern you can put this in your .vimrc:

syn match nottoday 'due:\(2017-07-11\)\@![0-9]\{4}-[0-9][0-9]-[0-9][0-9]'
hi nottoday ctermfg=yellow guifg=yellow

The next step is to automate the "today's date" part so that if you open the
file again tomorrow it would exclude tomorrow's date, something like this should
do it:

function HighlightNotToday()
exe "syn match nottoday 'due:\\(" . strftime("%Y-%m-%d") .
"\\)\\@![0-9]\\{4}-[0-9][0-9]-[0-9][0-9]'"
hi nottoday ctermfg=yellow guifg=yellow
endfun
nmap <f4> :call HighlightNotToday()<cr>

nazri

--
--
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: