Skip to content

Commit 189fca2

Browse files
committed
use pbcopy on MacOS for better clipboard manager support
On Mac OS, current versions of Tk 9 or Tk 8 `clipboard append` do not interact properly with clipboard managers. This makes `gitk` interact badly with them as well. See [1] for an example of how this affects `gitk` and [2] for a ticket describing the problem with Tk. This commit works around the problem by having `gitk` use the `pbcopy` CLI to copy to the clipboard instead of Tk's `clipboard append`, as long as we are on Mac OS and `pbcopy` is available. AFAIK, `pbcopy` is always installed with Mac OS. [1]: p0deje/Maccy#1100 [2]: https://core.tcl-lang.org/tk/tktview/e94c8bc845b4d5a658cd5421ef23ef2484560fa8
1 parent b7ef407 commit 189fca2

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

gitk

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ if {[is_Windows]} {
113113
114114
# End of safe PATH lookup stuff
115115
116+
proc have_pbcopy {} {
117+
if {$::tcl_platform(os) ne {Darwin}} {
118+
return 0
119+
}
120+
if {[catch { exec -ignorestderr -- pbcopy -help 2>@1 } helptext] != 0 } {
121+
return 0
122+
}
123+
if {![string match "*Usage: pbcopy*" $helptext]} {
124+
return 0
125+
}
126+
return 1
127+
}
128+
129+
if {[have_pbcopy]} {
130+
proc set_clipboard {text} {
131+
# Using `pbcopy` works around
132+
# https://core.tcl-lang.org/tk/tktview/e94c8bc845b4d5a658cd5421ef23ef2484560fa8
133+
# `clipboard append` trims the last newline, so we do too.
134+
exec pbcopy << [string trimright $text "\n"]
135+
}
136+
} else {
137+
proc set_clipboard {text} {
138+
clipboard clear
139+
clipboard append -- $text
140+
}
141+
}
142+
116143
# Wrap exec/open to sanitize arguments
117144
118145
# unsafe arguments begin with redirections or the pipe or background operators
@@ -2886,7 +2913,7 @@ proc makewindow {} {
28862913
{mc "Check out this branch" command cobranch}
28872914
{mc "Rename this branch" command mvbranch}
28882915
{mc "Remove this branch" command rmbranch}
2889-
{mc "Copy branch name" command {clipboard clear; clipboard append $headmenuhead}}
2916+
{mc "Copy branch name" command {set_clipboard $headmenuhead}}
28902917
}
28912918
$headctxmenu configure -tearoff 0
28922919
@@ -2897,7 +2924,7 @@ proc makewindow {} {
28972924
{mc "Highlight this only" command {flist_hl 1}}
28982925
{mc "External diff" command {external_diff}}
28992926
{mc "Blame parent commit" command {external_blame 1}}
2900-
{mc "Copy path" command {clipboard clear; clipboard append $flist_menu_file}}
2927+
{mc "Copy path" command {set_clipboard $flist_menu_file}}
29012928
}
29022929
$flist_menu configure -tearoff 0
29032930
@@ -7597,8 +7624,7 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
75977624
$sha1entry selection range 0 $autosellen
75987625
}
75997626
if {$autocopy} {
7600-
clipboard clear
7601-
clipboard append [string range $id 0 [expr $autosellen - 1]]
7627+
set_clipboard [string range $id 0 [expr $autosellen - 1]]
76027628
}
76037629
rhighlight_sel $id
76047630
@@ -9660,8 +9686,7 @@ proc copyreference {} {
96609686
}
96619687
set reference [safe_exec [concat $cmd $rowmenuid]]
96629688
9663-
clipboard clear
9664-
clipboard append $reference
9689+
set_clipboard $reference
96659690
}
96669691
96679692
proc writecommit {} {

0 commit comments

Comments
 (0)