@matheusbsbs / matheus-bagliano / commits / 354af6bd69
Fork 0

gitk: Restore scrolling position of diff pane on back/forward in history

This arranges to save the scrolling position of the diff display pane when we move from displaying one thing to another, and then scroll the pane to the same position when we go back to the previous thing using the back or forward buttons. This works if we have clicked on a commit and are in patch display mode, or if we have clicked on a line or a tag, or have done a diff between two commits with the context menu. It doesn't currently restore the pane to where it was if is was displaying a commit in tree display mode. For future extensibility, addtohistory now takes an extra optional argument which is a script to invoke when moving from this thing to another. The script needs to return a list of pairs of variable name and value. If we go back to this thing, the godo procedure will set the named variables to the values given. At present that is just used to store the $ctext scrolling position, but in future we will use it to store the state of which directories are open in the file list pane. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Nov 23, 2008 at 13:14 UTC 354af6bd69b442d0ce9449961f37d95ef12ba988
1 file changed +75 -9
gitk
+75 -9
@@ -6618,7 +6618,7 @@ proc selectline {l isnew {desired_loc {}}} {
6618 make_secsel $id
6619
6620 if {$isnew} {
6621 - addtohistory [list selbyid $id]
6621 + addtohistory [list selbyid $id 0] savecmitpos
6622 }
6623
6624 $sha1entry delete 0 end
@@ -6770,10 +6770,12 @@ proc reselectline {} {
6770 }
6771 }
6772
6773 -proc addtohistory {cmd} {
6773 +proc addtohistory {cmd {saveproc {}}} {
6774 global history historyindex curview
6775
6776 - set elt [list $curview $cmd]
6776 + unset_posvars
6777 + save_position
6778 + set elt [list $curview $cmd $saveproc {}]
6779 if {$historyindex > 0
6780 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
6781 return
@@ -6793,14 +6795,45 @@ proc addtohistory {cmd} {
6795 .tf.bar.rightbut conf -state disabled
6796 }
6797
6798 +# save the scrolling position of the diff display pane
6799 +proc save_position {} {
6800 + global historyindex history
6801 +
6802 + if {$historyindex < 1} return
6803 + set hi [expr {$historyindex - 1}]
6804 + set fn [lindex $history $hi 2]
6805 + if {$fn ne {}} {
6806 + lset history $hi 3 [eval $fn]
6807 + }
6808 +}
6809 +
6810 +proc unset_posvars {} {
6811 + global last_posvars
6812 +
6813 + if {[info exists last_posvars]} {
6814 + foreach {var val} $last_posvars {
6815 + global $var
6816 + catch {unset $var}
6817 + }
6818 + unset last_posvars
6819 + }
6820 +}
6821 +
6822 proc godo {elt} {
6797 - global curview
6823 + global curview last_posvars
6824
6825 set view [lindex $elt 0]
6826 set cmd [lindex $elt 1]
6827 + set pv [lindex $elt 3]
6828 if {$curview != $view} {
6829 showview $view
6830 }
6831 + unset_posvars
6832 + foreach {var val} $pv {
6833 + global $var
6834 + set $var $val
6835 + }
6836 + set last_posvars $pv
6837 eval $cmd
6838 }
6839
@@ -6809,6 +6842,7 @@ proc goback {} {
6842 focus .
6843
6844 if {$historyindex > 1} {
6845 + save_position
6846 incr historyindex -1
6847 godo [lindex $history [expr {$historyindex - 1}]]
6848 .tf.bar.rightbut conf -state normal
@@ -6823,6 +6857,7 @@ proc goforw {} {
6857 focus .
6858
6859 if {$historyindex < [llength $history]} {
6860 + save_position
6861 set cmd [lindex $history $historyindex]
6862 incr historyindex
6863 godo $cmd
@@ -7189,6 +7224,34 @@ proc getblobdiffs {ids} {
7224 filerun $bdf [list getblobdiffline $bdf $diffids]
7225 }
7226
7227 +proc savecmitpos {} {
7228 + global ctext cmitmode
7229 +
7230 + if {$cmitmode eq "tree"} {
7231 + return {}
7232 + }
7233 + return [list target_scrollpos [$ctext index @0,0]]
7234 +}
7235 +
7236 +proc savectextpos {} {
7237 + global ctext
7238 +
7239 + return [list target_scrollpos [$ctext index @0,0]]
7240 +}
7241 +
7242 +proc maybe_scroll_ctext {ateof} {
7243 + global ctext target_scrollpos
7244 +
7245 + if {![info exists target_scrollpos]} return
7246 + if {!$ateof} {
7247 + set nlines [expr {[winfo height $ctext]
7248 + / [font metrics textfont -linespace]}]
7249 + if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7250 + }
7251 + $ctext yview $target_scrollpos
7252 + unset target_scrollpos
7253 +}
7254 +
7255 proc setinlist {var i val} {
7256 global $var
7257
@@ -7383,6 +7446,7 @@ proc getblobdiffline {bdf ids} {
7446 if {[info exists seehere]} {
7447 mark_ctext_line [lindex [split $seehere .] 0]
7448 }
7449 + maybe_scroll_ctext [eof $bdf]
7450 $ctext conf -state disabled
7451 if {[eof $bdf]} {
7452 close $bdf
@@ -7877,7 +7941,7 @@ proc lineclick {x y id isnew} {
7941 }
7942
7943 if {$isnew} {
7880 - addtohistory [list lineclick $x $y $id 0]
7944 + addtohistory [list lineclick $x $y $id 0] savectextpos
7945 }
7946 # fill the details pane with info about this line
7947 $ctext conf -state normal
@@ -7908,6 +7972,7 @@ proc lineclick {x y id isnew} {
7972 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
7973 }
7974 }
7975 + maybe_scroll_ctext 1
7976 $ctext conf -state disabled
7977 init_flist {}
7978 }
@@ -7921,10 +7986,10 @@ proc normalline {} {
7986 }
7987 }
7988
7924 -proc selbyid {id} {
7989 +proc selbyid {id {isnew 1}} {
7990 global curview
7991 if {[commitinview $id $curview]} {
7927 - selectline [rowofcommit $id] 1
7992 + selectline [rowofcommit $id] $isnew
7993 }
7994 }
7995
@@ -7974,7 +8039,7 @@ proc diffvssel {dirn} {
8039 set oldid $rowmenuid
8040 set newid [commitonrow $selectedline]
8041 }
7977 - addtohistory [list doseldiff $oldid $newid]
8042 + addtohistory [list doseldiff $oldid $newid] savectextpos
8043 doseldiff $oldid $newid
8044 }
8045
@@ -9886,7 +9951,7 @@ proc showtag {tag isnew} {
9951 global ctext tagcontents tagids linknum tagobjid
9952
9953 if {$isnew} {
9889 - addtohistory [list showtag $tag 0]
9954 + addtohistory [list showtag $tag 0] savectextpos
9955 }
9956 $ctext conf -state normal
9957 clear_ctext
@@ -9903,6 +9968,7 @@ proc showtag {tag isnew} {
9968 set text "[mc "Tag"]: $tag\n[mc "Id"]: $tagids($tag)"
9969 }
9970 appendwithlinks $text {}
9971 + maybe_scroll_ctext
9972 $ctext conf -state disabled
9973 init_flist {}
9974 }