Search code examples
searchtcltk-toolkittext-widget

Concise way for supplying text search command its parameters? (Tcl/Tk)


I'm in the process of implementing a "Find" feature in a program I'm writing in Tcl/Tk, and I'm struggling to find a concise/efficient way to write this code. The search will have different options like "find all," "exact," and "Search up/down" etc.

Here is what I've tried and what I'm kinda going for here:

set idx [.text index insert];
set search_for $::search_entry;

set parameters "";
if {$::match_exact == 1} {append parameters "-exact "};
if {$::case_sensitive == 0} {append parameters "-nocase "};
if {$::find_all == 1} {append parameters "-all "};
if {$::direction == 1} {
    append parameters "-backwards";
} else {
    append parameters "-forewards";
}

.text search $parameters $search_for $idx;

With this I get the following error when I try searching:

bad switch "-nocase -forewards": must be --, -all, -backwards, -count, -elide, -exact, -forwards, -nocase, -nolinestop, -overlap, -regexp, or -strictlimits

Please tell me there is a similarly concise way to to write this? I worry that I'll have to create different searches for each and every combination of options set by the user...


Solution

  • You are defining parameters as a string. It needs to be a list that you expand at the time you call the search command. It would look something like this:

    set parameters [list]
    if {$::match_exact == 1} {lappend parameters "-exact"}
    ...
    .text search {*}$parameters $search_for $idx