Skip to content

(Bug with POC workaround) use pbcopy on MacOS for better clipboard manager support #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions gitk
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ if {[is_Windows]} {

# End of safe PATH lookup stuff

proc have_pbcopy {} {
if {$::tcl_platform(os) ne {Darwin}} {
return 0
}
if {[catch { exec -ignorestderr -- pbcopy -help 2>@1 } helptext] != 0 } {
return 0
}
if {![string match "*Usage: pbcopy*" $helptext]} {
return 0
}
return 1
}

if {[have_pbcopy]} {
proc set_clipboard {text} {
# Using `pbcopy` works around
# https://core.tcl-lang.org/tk/tktview/e94c8bc845b4d5a658cd5421ef23ef2484560fa8
# `clipboard append` trims the last newline, so we do too.
Copy link
Author

@ilyagr ilyagr May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this. When I try clipboard append "a\n", the \n goes to the clipboard. However, experimentally, string trimright below seems necessary for the result of gitk's copies to the clipboard with pbcopy to be the same as the results from before this commit. Perhaps the behavior of clipboard append is more confusing than I thought, intentionally or because of a bug.

exec pbcopy << [string trimright $text "\n"]
}
} else {
proc set_clipboard {text} {
clipboard clear
clipboard append -- $text
}
}

proc hasworktree {} {
return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
[exec git rev-parse --is-inside-git-dir] == "false"}]
Expand Down Expand Up @@ -2814,7 +2841,7 @@ proc makewindow {} {
{mc "Check out this branch" command cobranch}
{mc "Rename this branch" command mvbranch}
{mc "Remove this branch" command rmbranch}
{mc "Copy branch name" command {clipboard clear; clipboard append $headmenuhead}}
{mc "Copy branch name" command {set_clipboard $headmenuhead}}
}
$headctxmenu configure -tearoff 0

Expand All @@ -2825,7 +2852,7 @@ proc makewindow {} {
{mc "Highlight this only" command {flist_hl 1}}
{mc "External diff" command {external_diff}}
{mc "Blame parent commit" command {external_blame 1}}
{mc "Copy path" command {clipboard clear; clipboard append $flist_menu_file}}
{mc "Copy path" command {set_clipboard $flist_menu_file}}
}
$flist_menu configure -tearoff 0

Expand Down Expand Up @@ -7527,8 +7554,7 @@ proc selectline {l isnew {desired_loc {}} {switch_to_patch 0}} {
$sha1entry selection range 0 $autosellen
}
if {$autocopy} {
clipboard clear
clipboard append [string range $id 0 [expr $autosellen - 1]]
set_clipboard [string range $id 0 [expr $autosellen - 1]]
}
rhighlight_sel $id

Expand Down Expand Up @@ -9595,8 +9621,7 @@ proc copyreference {} {
}
set reference [eval exec $cmd $rowmenuid]

clipboard clear
clipboard append $reference
set_clipboard $reference
}

proc writecommit {} {
Expand Down