highlight items in red that are past due

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

highlight items in red that are past due

robert h-2
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?

Thanks,

Bob

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: highlight items in red that are past due

Nazri Ramliy
On Tue, Jul 11, 2017 at 1:28 AM, Robert <[hidden email]> 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: highlight items in red that are past due

robert h-2
On Tuesday, July 11, 2017 at 1:35:06 AM UTC-4, Nazri wrote:

> On Tue, Jul 11, 2017 at 1:28 AM, Robert <[hidden email]> 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
Thanks Nazri, I'll give it shot.

Bob

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: highlight items in red that are past due

Christian Brabandt
In reply to this post by robert h-2

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.

Best,
Christian
--
Das Christentum braucht nur ein Jahrtausend in seiner Geschichte
zurückzublättern: im Anfang war es wohl die Güte, die diese Religion
hat zu gebären helfen - zur Macht gebracht hat sie die Gewalt.
                -- Kurt Tucholsky

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: highlight items in red that are past due

'Jürgen Krämer' via vim_use

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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...