Click find script [fix]

From: Jay <robert.jay.go....at.gmail.com>
Date: Tue, 27 Apr 2010 21:45:53 -0700 (PDT)

While I'm at it, the other day I began using the wiki's clickfind
script and found that it had a bug when the same word was contained
twice in the same line, with the first occurrence being at the line-
start.

Here is the fixed and optimized highlightFind function for the script:

function highlightFind(position, line)
        -- Grab the word that was clicked on
        buffer:word_left()
        buffer:word_right_end_extend()
        needle = buffer:get_sel_text()
        -- Trim any whitespace
        needle = needle:gsub('%s', '')
        -- Escape unwanted characters
        needle = needle:gsub('([().*+?^$%%[%]-])', '%%%1')
        -- Clear out the old marks
        buffer:marker_delete_all(BACK_MARKER_NUMBER)
        buffer.indicator_current = 0
        buffer:indicator_clear_range(0, buffer.length)
        -- Don't look for zero-length strings
        if #needle > 0 then
                for i = 0, buffer.line_count do
                        local text = buffer:get_line(i)
                        if #text>0 then
                                local first, last = 0, 0
                                while first do
                                        first, last = text:find("%f[%w]"..needle.."%f[%W]",last)
                                        if last then
                                                buffer:marker_add(i, BACK_MARKER_NUMBER)
                                                if (first ~= nil) and (first >0) then
                                                        first = first - 1
                                                end
                                                local head = last - first
                                                buffer:indicator_fill_range(buffer:position_from_line(i)+ first,
head )
                                                last = last+1
                                        end
                                end
                        end
                end
                --buffer:set_sel(position, position) -- restore caret
        end
end

-Jay

-- 
You received this message because you are subscribed to the Google Groups "textadept" group.
To post to this group, send email to textadept.at.googlegroups.com.
To unsubscribe from this group, send email to textadept+unsubscribe.at.googlegroups.com.
For more options, visit this group at http://groups.google.com/group/textadept?hl=en.
Received on Wed 28 Apr 2010 - 00:45:53 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:44:06 EST