Code

git-gui: enable the smart case sensitive search only if gui.search.smartcase is true
[git.git] / lib / search.tcl
index d292f20f66a6319c34c3b38e28ebc120e7dd8400..04a316bbb27897ebd2247934db03c978988d0487 100644 (file)
@@ -7,25 +7,46 @@ field w
 field ctext
 
 field searchstring   {}
-field casesensitive  1
+field regexpsearch
+field default_regexpsearch
+field casesensitive
+field default_casesensitive
+field smartcase
 field searchdirn     -forwards
 
+field history
+field history_index
+
 field smarktop
 field smarkbot
 
 constructor new {i_w i_text args} {
+       global use_ttk NS
        set w      $i_w
        set ctext  $i_text
 
-       frame  $w
-       label  $w.l       -text [mc Find:]
-       button $w.bn      -text [mc Next] -command [cb find_next]
-       button $w.bp      -text [mc Prev] -command [cb find_prev]
-       checkbutton $w.cs -text [mc Case-Sensitive] \
+       set default_regexpsearch [is_config_true gui.search.regexp]
+       set smartcase [is_config_true gui.search.smartcase]
+       if {$smartcase} {
+               set default_casesensitive 0
+       } else {
+               set default_casesensitive 1
+       }
+
+       set history [list]
+
+       ${NS}::frame  $w
+       ${NS}::label  $w.l       -text [mc Find:]
+       tentry  $w.ent -textvariable ${__this}::searchstring -background lightgreen
+       ${NS}::button $w.bn      -text [mc Next] -command [cb find_next]
+       ${NS}::button $w.bp      -text [mc Prev] -command [cb find_prev]
+       ${NS}::checkbutton $w.re -text [mc RegExp] \
+               -variable ${__this}::regexpsearch -command [cb _incrsearch]
+       ${NS}::checkbutton $w.cs -text [mc Case] \
                -variable ${__this}::casesensitive -command [cb _incrsearch]
-       entry  $w.ent -textvariable ${__this}::searchstring -background lightgreen
        pack   $w.l   -side left
        pack   $w.cs  -side right
+       pack   $w.re  -side right
        pack   $w.bp  -side right
        pack   $w.bn  -side right
        pack   $w.ent -side left -expand 1 -fill x
@@ -34,25 +55,42 @@ constructor new {i_w i_text args} {
        grid remove $w
 
        trace add variable searchstring write [cb _incrsearch_cb]
+       bind $w.ent <Return> [cb find_next]
+       bind $w.ent <Shift-Return> [cb find_prev]
+       bind $w.ent <Key-Up>   [cb _prev_search]
+       bind $w.ent <Key-Down> [cb _next_search]
        
-       bind $w <Destroy> [cb delete_this]
+       bind $w <Destroy> [list delete_this $this]
        return $this
 }
 
 method show {} {
-       if {![winfo ismapped $w]} {
+       if {![visible $this]} {
                grid $w
+               $w.ent delete 0 end
+               set regexpsearch  $default_regexpsearch
+               set casesensitive $default_casesensitive
+               set history_index [llength $history]
        }
        focus -force $w.ent
 }
 
 method hide {} {
-       if {[winfo ismapped $w]} {
+       if {[visible $this]} {
                focus $ctext
                grid remove $w
+               _save_search $this
        }
 }
 
+method visible {} {
+       return [winfo ismapped $w]
+}
+
+method editor {} {
+       return $w.ent
+}
+
 method _get_new_anchor {} {
        # use start of selection if it is visible,
        # or the bounds of the visible area
@@ -87,6 +125,9 @@ method _do_search {start {mlenvar {}} {dir {}} {endbound {}}} {
                upvar $mlenvar mlen
                lappend cmd -count mlen
        }
+       if {$regexpsearch} {
+               lappend cmd -regexp
+       }
        if {!$casesensitive} {
                lappend cmd -nocase
        }
@@ -94,14 +135,16 @@ method _do_search {start {mlenvar {}} {dir {}} {endbound {}}} {
                set dir $searchdirn
        }
        lappend cmd $dir -- $searchstring
-       if {$endbound ne {}} {
-               set here [eval $cmd [list $start] [list $endbound]]
-       } else {
-               set here [eval $cmd [list $start]]
-               if {$here eq {}} {
-                       set here [eval $cmd [_get_wrap_anchor $this $dir]]
+       if {[catch {
+               if {$endbound ne {}} {
+                       set here [eval $cmd [list $start] [list $endbound]]
+               } else {
+                       set here [eval $cmd [list $start]]
+                       if {$here eq {}} {
+                               set here [eval $cmd [_get_wrap_anchor $this $dir]]
+                       }
                }
-       }
+       } err]} { set here {} }
        return $here
 }
 
@@ -114,20 +157,76 @@ method _incrsearch {} {
        if {[catch {$ctext index anchor}]} {
                $ctext mark set anchor [_get_new_anchor $this]
        }
+       if {$smartcase} {
+               if {[regexp {[[:upper:]]} $searchstring]} {
+                       set casesensitive 1
+               }
+       }
        if {$searchstring ne {}} {
                set here [_do_search $this anchor mlen]
                if {$here ne {}} {
                        $ctext see $here
                        $ctext tag remove sel 1.0 end
                        $ctext tag add sel $here "$here + $mlen c"
-                       $w.ent configure -background lightgreen
+                       #$w.ent configure -background lightgreen
+                       $w.ent state !pressed
                        _set_marks $this 1
                } else {
-                       $w.ent configure -background lightpink
+                       #$w.ent configure -background lightpink
+                       $w.ent state pressed
                }
        }
 }
 
+method _save_search {} {
+       if {$searchstring eq {}} {
+               return
+       }
+       if {[llength $history] > 0} {
+               foreach {s_regexp s_case s_expr} [lindex $history end] break
+       } else {
+               set s_regexp $regexpsearch
+               set s_case   $casesensitive
+               set s_expr   ""
+       }
+       if {$searchstring eq $s_expr} {
+               # update modes
+               set history [lreplace $history end end \
+                               [list $regexpsearch $casesensitive $searchstring]]
+       } else {
+               lappend history [list $regexpsearch $casesensitive $searchstring]
+       }
+       set history_index [llength $history]
+}
+
+method _prev_search {} {
+       if {$history_index > 0} {
+               incr history_index -1
+               foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
+               $w.ent delete 0 end
+               $w.ent insert 0 $s_expr
+               set regexpsearch $s_regexp
+               set casesensitive $s_case
+       }
+}
+
+method _next_search {} {
+       if {$history_index < [llength $history]} {
+               incr history_index
+       }
+       if {$history_index < [llength $history]} {
+               foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
+       } else {
+               set s_regexp $default_regexpsearch
+               set s_case   $default_casesensitive
+               set s_expr   ""
+       }
+       $w.ent delete 0 end
+       $w.ent insert 0 $s_expr
+       set regexpsearch $s_regexp
+       set casesensitive $s_case
+}
+
 method find_prev {} {
        find_next $this -backwards
 }
@@ -138,6 +237,7 @@ method find_next {{dir -forwards}} {
        set searchdirn $dir
        $ctext mark unset anchor
        if {$searchstring ne {}} {
+               _save_search $this
                set start [_get_new_anchor $this]
                if {$dir eq "-forwards"} {
                        set start "$start + 1c"
@@ -187,4 +287,4 @@ method scrolled {} {
        }
 }
 
-}
\ No newline at end of file
+}