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

From: Pascal <patatetom.att.gmail.com>
Date: Sat, 1 Apr 2017 10:33:30 +0200

hi Mitchell,

first thanks for answere and sorry for the poor quality of my post.

I use TA 9.3 and my goal is to use TA more like a viewer than an editor.
when I view a file, I'd like some strings highlighted (dates, times and
some words) :

blabla 2017/04/01 10:30:40 blabla some words blabla blabla
some words blabla 10:30:40 blabla blabla blabla 2017/04/01

in the meantime, I revisited my file and now it works as I expect but I
think my code can/must be reversed :

io.encodings[#io.encodings + 1] = 'ISO-8859-15'
io.encodings[#io.encodings + 1] = 'CP1252'
io.encodings[#io.encodings + 1] = 'CP850'
ui.set_theme(not CURSES and 'dark' or 'term', {font = 'Monospace', fontsize
= 13})
ui.maximized = true
events.connect(events.INITIALIZED,function() textadept.menu.menubar = nil
end)

-- 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.search_flags = buffer.FIND_REGEXP

        -- highlight date
        buffer.target_start = 0
        buffer.indicator_current = date_indicator
        buffer.indic_style[date_indicator] = buffer.INDIC_ROUNDBOX
        buffer.indic_fore[date_indicator] = 0x0000FF
        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 break end
            buffer:indicator_fill_range(buffer.target_start,
buffer.target_end - buffer.target_start)
            buffer.target_start = buffer.target_end
        end

        -- highlight date
        buffer.target_start = 0
        buffer.indicator_current = time_indicator
        buffer.indic_style[time_indicator] = buffer.INDIC_ROUNDBOX
        buffer.indic_fore[time_indicator] = 0x00FF00
        while true do
            buffer.target_end = buffer.length
            if
buffer:search_in_target('(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?')
== -1 then 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
)

-- highlight selection

local has_markers
local last_selected_text
local selected_indicator = _SCINTILLA.next_indic_number()

buffer.indic_style[selected_indicator] = buffer.INDIC_ROUNDBOX
buffer.indic_fore[selected_indicator] = "0x00FFFF"
buffer.indic_alpha[selected_indicator] = "96"

local function remove_all_markers()
    buffer.indicator_current = selected_indicator
    buffer:indicator_clear_range(0, buffer.length)
    has_markers = nil
end

local function update_marker_selection()
    local selected_text, index = buffer:get_sel_text()
    if selected_text ~= last_selected_text then
        if has_markers then remove_all_markers() end
        last_selected_text = selected_text
        if selected_text and selected_text ~= "" and
string.len(selected_text) >= 3 then
            buffer.indicator_current = selected_indicator
            local buffer_text = buffer:get_text()
            local index = 0
            local found_start, found_end = string.find(buffer_text,
selected_text, index, true)
            while(found_start and found_end) do
                index = found_end
                buffer:indicator_fill_range(found_start-1, found_end -
found_start+1)
                found_start, found_end = string.find(buffer_text,
selected_text, index, true)
            end
            has_markers = true
        end
    end
end

events.connect(events.UPDATE_UI, update_marker_selection)

I placed the highlight in the event events.FILE_OPENED but I saw that you
used events.VIEW_NEW, so I'll move code to this event.
now I use two loops for both searches but I think there must be more
simple/efficient (for the date, I have three regexp : a loop for each?
:-()...

regards, lacsaP.

(ps
the result of my previous init.lua was :

2017/04/01 12:00:00
2017/04/01 12:00:00

only the last time was highlighted
)

2017-04-01 0:00 GMT+02:00 Mitchell <m.att.foicica.com>:

> Hi,
>
> On Fri, 31 Mar 2017, Pascal wrote:
>
> hi,
>>
>> inspired by "Highlight Trailing Whitespace
>> <https://foicica.com/wiki/highlight-trailing-whitespace>" from wiki, I've
>>
>> added this code to my init.lua to do the job :
>>
>> io.encodings[#io.encodings + 1] = 'ISO-8859-15'
>> io.encodings[#io.encodings + 1] = 'CP1252'
>> io.encodings[#io.encodings + 1] = 'CP850'
>>
>> ui.set_theme('dark')
>>
>> -- highlight
>> local date_indicator = _SCINTILLA.next_indic_number()
>> buffer.indic_style[date_indicator] = buffer.INDIC_ROUNDBOX
>> buffer.indic_fore[date_indicator] = 0xFFFF00
>> buffer.indic_alpha[date_indicator] = "80"
>>
>> local hour_indicator = _SCINTILLA.next_indic_number()
>> buffer.indic_style[hour_indicator] = buffer.INDIC_ROUNDBOX
>> buffer.indic_fore[hour_indicator] = 0x0000FF
>> buffer.indic_alpha[hour_indicator] = "80"
>>
>> events.connect(events.UPDATE_UI,
>> function()
>> buffer.target_start = 0
>> buffer.search_flags = buffer.FIND_REGEXP
>> 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[1-9]|1[012])[-/.](0[1-9]|[12][0-
>> 9]|3[01])[-/.]20[1-9][0-9]')
>> ~= -1 then buffer.indicator_current = date_indicator
>> elseif
>> buffer.search_in_target('(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9
>> ]|1[012])[-/.]20[1-9][0-9]')
>> ~= -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 = hour_indicator
>> else break
>> end
>> buffer.indicator_fill_range(buffer.target_start,
>> buffer.target_end - buffer.target_start)
>> buffer.target_start = buffer.target_end
>> end
>> end
>> )
>>
>> --view only
>> events.connect(events.FILE_OPENED,
>> function()
>> buffer.read_only = true
>> end
>> )
>>
>> but when I open a file, only the last hour string is highlighted :-(
>> but when I write in a new buffer (not read-only), the code seems
>> working...
>>
>
> You should wrap your indicator settings `buffer.indic_fore = ...` in
>
> events.connect(events.VIEW_NEW, function()
> buffer.indic_fore = ...
> buffer.indic_alpha = ...
> end)
>
> (Leave the `_SCINTILLA.next_indic_number()` outside.) That way your
> indicator settings apply to all buffers, not just the first one.
>
> Without any input text it's next to impossible to deduce the issue from
> your source code alone. You may want to try adding
> `ui.dialogs.msgbox{...}`[1] statements in your code to see which
> conditionals are hit as a debugging technique.
>
> Cheers,
> Mitchell
>
> [1]: http://foicica.com/textadept/api.html#ui.dialogs.msgbox
> --
> 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.
>
>

-- 
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 Sat 01 Apr 2017 - 04:33:30 EDT

This archive was generated by hypermail 2.2.0 : Sat 01 Apr 2017 - 06:41:47 EDT