highlighting all occurrences of the same piece of text
Tony Balinski
ajbj at free.fr
Tue Aug 15 12:48:33 CEST 2006
Quoting Mike MacGillis <Mike.MacGillis at arc.com>:
> Hi Eric,
> if you could send that macro to me when you have a chance that would be
> great.
> Cheers
> Mike
>
Here's a macro that'll do it:
------------------------------------------------------------------------
# pick up search string
sel = get_selection("any")
if (sel != "")
{
pat = string_dialog("Enter search pattern\n" \
"(default is '" sel "')", \
"OK", "Cancel")
if (pat == "")
pat = sel
}
else
{
pat = string_dialog("Enter search pattern", "OK", "Cancel")
}
if (sel == "" || $string_dialog_button != 1)
return
# choose search type
ty[1] = "literal"
ty[2] = "case"
ty[3] = "word"
ty[4] = "caseWord"
ty[5] = "regex"
ty[6] = "regexNoCase"
ind = dialog("Search for\n\n '" pat "'\n\n" \
"as the following pattern type", \
ty[1], ty[2], ty[3], ty[4], ty[5], ty[6], "Cancel")
if (!(ind in ty))
return
srchtype = ty[ind]
# delete old result rangeset(s) for re-creation
sets = rangeset_get_by_name("Find all")
if (sets[] != 0)
rangeset_destroy(sets)
# (re)create a rangeset for results
# - this will be the "top-most" rangeset
rs = rangeset_create()
if (rs == 0)
return dialog("Could not create a rangeset for Find All search", \
"Dismiss")
rangeset_set_name(rs, "Find all")
rangeset_set_color(rs, "#80FFC0") # greenish
# now do the search and assemble results
res = ""
num = 0
end = 0
for (pos = search(pat, 0, srchtype); \
pos >= 0; \
pos = search(pat, end, srchtype))
{
++num
end = $search_end
rangeset_add(rs, pos, end)
bol = max(search("\n", pos, "case", "backward") + 1, 0)
eol = min(search("\n", pos, "case", "forward"), $text_length)
# add to res a representatoin of the (first) line of the found pattern
if (eol > end)
res = res num ": " get_range(bol, pos) \
"[[" get_range(pos, end) \
"]]" get_range(end, eol) "\n"
else
res = res num ": " get_range(bol, pos) \
"[[" get_range(pos, eol) "...\n"
}
if (num == 0)
return dialog("No occurrences of " srchtype " pattern\n\n" \
" '" pat "'\n\nfound", "Dismiss")
# found all, have results - now go there
line = list_dialog("Found "num" occurrences of " srchtype " pattern\n\n" \
" '" pat "'\n\nGo to which one?", \
res, "OK", "Select", "Cancel")
btn = $list_dialog_button
if (line == "" || !(btn == 1 || btn == 2))
return
num = replace_in_string(line, ":.*", "", "regex") + 0
range = rangeset_range(rs, num)
if (range[])
{
set_cursor_pos(range["start"])
if (btn == 2)
select(range["start"], range["end"])
}
------------------------------------------------------------------------
Paste that code into the macro dialog (Preferences> Default Settings>
Customize Menus> Macro Menu...), give it a name (eg Find All), an
Accelerator if you want it attached to some control key, and that's it!
(This doesn't do the continuous highlighting that TK's patch does, but
it should be good in most circumstances - adapt it as you see fit.)
Tony
More information about the Discuss
mailing list