Re: [code] [textadept] highlighting dates and hours

From: Mitchell <m.att.foicica.com>
Date: Sun, 2 Apr 2017 11:35:50 -0400 (EDT)

Hi,

On Sat, 1 Apr 2017, Pascal wrote:

> if I try to reduce the number of loops to one like this :
>
> -- read only and highlight
>
> local date_indicator = _SCINTILLA.next_indic_number()
> local time_indicator = _SCINTILLA.next_indic_number()
>
> events.connect(events.FILE_OPENED,
> function()
> buffer.indic_style[date_indicator] = buffer.INDIC_ROUNDBOX
> buffer.indic_fore[date_indicator] = 0x0000FF
> buffer.indic_style[time_indicator] = buffer.INDIC_ROUNDBOX
> buffer.indic_fore[time_indicator] = 0x00FF00
> buffer.search_flags = buffer.FIND_REGEXP
>
> buffer.target_start = 0
> while true do
> buffer.target_end = buffer.length
> if
> buffer:search_in_target('20[1-9][0-9][-/.](0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])')
> ~= -1 then buffer.indicator_current = date_indicator
> elseif
> buffer:search_in_target('(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?')
> ~= -1 then buffer.indicator_current = time_indicator
> else break end
> buffer:indicator_fill_range(buffer.target_start,
> buffer.target_end - buffer.target_start)
> buffer.target_start = buffer.target_end
> end
>
> buffer.read_only = true
> end
> )
>
> I get this :
>
> 2017/04/01 11:59:59 2017/04/01 12:00:00 2017/04/01 12:00:00
> 2017.04-01 11:59:59
> 11:59:59
> 11:59:59
>
> whereas with the two loops, I've the expected result :
>
> 2017/04/01 11:59:59 2017/04/01 12:00:00 2017/04/01 12:00:00
> 2017.04-01 11:59:59
> 11:59:59
> 11:59:59

When you use one loop, you are always looking for dates first. This will
skip over any times (due to `buffer.target_start = buffer.target_end`
after a date match) until no date can be found. Only then will you find
any subsequent times.

If you want to do one loop, you need to check for both a date and a time
(resetting `buffer.target_start` between checks), and see which returns a
lesser position. Go with the lesser position, and continue the loop.

So using your example input, the loop would look for both a date and a
time. The first date found is "2017/04/01" and the first time found is
"11:59:59". Since the date has a lesser position, you use that, and then
increment `buffer.target_start` before starting the loop again. In the
next iteration, the first date found is the next occurrence of
"2017/04/01" and the first time found is "11:59:59" (remember to reset
`buffer.target_start` in order to find that time; otherwise you'll get
"12:00:00"). Since the time has the lesser position, you use that, and
then increment `buffer.target_end`. And so on.

Cheers,
Mitchell

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Sun 02 Apr 2017 - 11:35:50 EDT

This archive was generated by hypermail 2.2.0 : Mon 03 Apr 2017 - 06:26:08 EDT