From ada4f2a6aa2cf03104acb46459e24b19b3901107 Mon Sep 17 00:00:00 2001 From: Bert Wesarg Date: Sun, 25 Mar 2012 21:07:30 +0200 Subject: [PATCH] git-gui: open file with GIT_EDITOR Signed-off-by: Bert Wesarg --- git-gui.sh | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/git-gui.sh b/git-gui.sh index 1b42c61..5876627 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -1340,6 +1340,9 @@ if {[lindex $_reponame end] eq {.git}} { set env(GIT_DIR) $_gitdir set env(GIT_WORK_TREE) $_gitworktree +catch { unset env(GIT_EDITOR_F_NBLOCK) } +catch { unset env(GIT_EDITOR_F_POSITION) } + ###################################################################### ## ## global init @@ -2523,6 +2526,100 @@ proc force_first_diff {after} { } } +# opens file in editor +proc open_in_git_editor {path {lno 0}} { + global env + + + catch { unset env(ARGS) } + catch { unset env(REVISION) } + + set env(GIT_EDITOR_F_NBLOCK) 1 + if {$lno != 0} { + set env(GIT_EDITOR_F_POSITION) $lno + } + + if {[catch {exec [shellpath] -c "[git var GIT_EDITOR] [sq $path]"} err]} { + tk_messageBox \ + -icon error \ + -type ok \ + -title {git-gui: Can't start GIT_EDITOR} \ + -message $err + } + + catch { unset env(GIT_EDITOR_F_NBLOCK) } + catch { unset env(GIT_EDITOR_F_POSITION) } +} + +proc get_best_diff_lno {w lno} { + set fwi [$w search -elide -regexp {^\d+$} $lno.0 end] + if {$fwi eq {}} { + set fwi end + } + set bwi [$w search -elide -backwards -regexp {^\d+$} $lno.0 1.0] + if {$bwi eq {}} { + set bwi 1.0 + } + set fwl [$w count -lines $lno.0 $fwi] + set bwl [$w count -lines $bwi $lno.0] + if {$fwl <= $bwl} { + set lno [$w get "$fwi linestart" "$fwi lineend"] + } else { + set lno [$w get "$bwi linestart" "$bwi lineend"] + } + + if {$lno eq {}} { + set lno 0 + } + + return $lno +} + +proc open_from_file_list {w x y} { + global ui_diff ui_diff_blnos + global file_lists current_diff_path + + set pos [split [$w index @$x,$y] .] + set lno [lindex $pos 0] + set col [lindex $pos 1] + set path [lindex $file_lists($w) [expr {$lno - 1}]] + if {$path eq {}} { + return + } + + set lno 0 + if {$path eq $current_diff_path} { + # calculate the line number which is visible in the middle + set height [$ui_diff count -ypixels 1.0 end] + set ypos [$ui_diff yview] + set yposm [expr {([lindex $ypos 1] + [lindex $ypos 0]) / 2}] + set mpixel [expr {$height * $yposm}] + set ytop [expr {[lindex $ypos 0] * $height}] + set rmpixel [expr {round($mpixel - $ytop)}] + set lno [$ui_diff index "@0,$rmpixel linestart"] + set lno [lindex [split $lno .] 0] + + set lno [get_best_diff_lno $ui_diff_blnos $lno] + } + + open_in_git_editor $path $lno +} + +proc open_from_diff_view {x y} { + global ui_diff ui_diff_blnos + global file_lists current_diff_path + + if {$current_diff_path eq {}} { + return + } + + set lno [$ui_diff index "@0,$y linestart"] + set lno [lindex [split $lno .] 0] + set lno [get_best_diff_lno $ui_diff_blnos $lno] + + open_in_git_editor $current_diff_path $lno +} + proc popup_files_ctxm {m w x y X Y} { global file_lists popup_path @@ -3275,10 +3372,11 @@ unset i set files_ctxm .vpane.files.ctxm menu $files_ctxm -tearoff 0 $files_ctxm add separator +$files_ctxm add command -label [mc "Open in Editor"] -command {open_from_file_list $current_diff_side $cursorX $cursorY} $files_ctxm add command -label [mc "Stage All Changed"] -command do_add_all $files_ctxm add command -label [mc "Stage Selected"] -command do_add_selection if {[array names repo_config guitool.*.cmd] ne {}} { - files_tools_populate_all 3 popup_path + files_tools_populate_all 4 popup_path } @@ -3764,6 +3862,10 @@ set ui_diff_revertline [$ctxmw index last] lappend diff_actions [list $ctxmw entryconf $ui_diff_applyline -state] $ctxmw add separator $ctxmw add command \ + -label [mc "Open in Editor"] \ + -command {open_from_diff_view $cursorX $cursorY} +lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state] +$ctxmw add command \ -label [mc "Show Less Context"] \ -command show_less_context lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state] @@ -3790,6 +3892,10 @@ $ctxmi add command \ lappend diff_actions [list $ctxmi entryconf $ui_diff_applyline -state] $ctxmi add separator $ctxmi add command \ + -label [mc "Open in Editor"] \ + -command {open_from_diff_view $cursorX $cursorY} +lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state] +$ctxmi add command \ -label [mc "Show Less Context"] \ -command show_less_context lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state] @@ -3825,6 +3931,10 @@ $ctxmmg add command \ lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state] $ctxmmg add separator $ctxmmg add command \ + -label [mc "Open in Editor"] \ + -command {open_from_diff_view $cursorX $cursorY} +lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state] +$ctxmmg add command \ -label [mc "Show Less Context"] \ -command show_less_context lappend diff_actions [list $ctxmmg entryconf [$ctxmmg index last] -state] @@ -3929,6 +4039,10 @@ proc popup_diff_menu {ctxmw ctxmi ctxmmg ctxmsm x y X Y} { } bind_button3 $ui_diff [list popup_diff_menu $ctxmw $ctxmi $ctxmmg $ctxmsm %x %y %X %Y] +foreach i $ui_diff_columns { + bind $i {open_from_diff_view %x %y} +} + # -- Status Bar # set main_status [::status_bar::new .status] @@ -4060,7 +4174,10 @@ foreach i [list $ui_index $ui_workdir] { bind $i "toggle_or_diff $i %x %y; break" bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break" bind $i "add_range_to_selection $i %x %y; break" + + bind $i "open_from_file_list $i %x %y; break" } + if {[$files_ctxm index end] == 1} { # remove the separator, when we don't have user specified file tools $files_ctxm delete 0 -- 2.11.4.GIT