Skip to content

Commit e84193d

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 bfb0fa7 commit e84193d

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
proc hasworktree {} {
117144
return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
118145
[exec git rev-parse --is-inside-git-dir] == "false"}]
@@ -2814,7 +2841,7 @@ proc makewindow {} {
28142841
{mc "Check out this branch" command cobranch}
28152842
{mc "Rename this branch" command mvbranch}
28162843
{mc "Remove this branch" command rmbranch}
2817-
{mc "Copy branch name" command {clipboard clear; clipboard append $headmenuhead}}
2844+
{mc "Copy branch name" command {set_clipboard $headmenuhead}}
28182845
}
28192846
$headctxmenu configure -tearoff 0
28202847
@@ -2825,7 +2852,7 @@ proc makewindow {} {
28252852
{mc "Highlight this only" command {flist_hl 1}}
28262853
{mc "External diff" command {external_diff}}
28272854
{mc "Blame parent commit" command {external_blame 1}}
2828-
{mc "Copy path" command {clipboard clear; clipboard append $flist_menu_file}}
2855+
{mc "Copy path" command {set_clipboard $flist_menu_file}}
28292856
}
28302857
$flist_menu configure -tearoff 0
28312858
@@ -7527,8 +7554,7 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
75277554
$sha1entry selection range 0 $autosellen
75287555
}
75297556
if {$autocopy} {
7530-
clipboard clear
7531-
clipboard append [string range $id 0 [expr $autosellen - 1]]
7557+
set_clipboard [string range $id 0 [expr $autosellen - 1]]
75327558
}
75337559
rhighlight_sel $id
75347560
@@ -9595,8 +9621,7 @@ proc copyreference {} {
95959621
}
95969622
set reference [eval exec $cmd $rowmenuid]
95979623
9598-
clipboard clear
9599-
clipboard append $reference
9624+
set_clipboard $reference
96009625
}
96019626
96029627
proc writecommit {} {

0 commit comments

Comments
 (0)