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

Conversation

ilyagr
Copy link

@ilyagr ilyagr commented May 24, 2025

This could be considered as a bug report with a proof of concept for a workaround.

TLDR: on Mac OS, gitk's "copy commit reference" and other clipboard operations are not detected properly by clipboard managers due to a Tk bug. The first copy from gitk is often detected, but any further ones are not.

See the links below for more details.

The POC fix is simply to shell out to Mac OS's pbcopy instead of using Tk's clipboard append on Mac OS. If the Tk bug could be fixed, that would probably be better.

I am not sure whether this is ready to merge. In particular, I am not familiar with Tcl/Tk conventions and I have only tested this on Tk 8.6.16.


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

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.

@j6t
Copy link
Owner

j6t commented May 25, 2025

Thanks. It would be great if you could resolve the confusing \n behavior, so that we don't have a big question mark in this code. It does not happen here on Linux and the documentation doesn't mention any modification of the data, either.

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
@ilyagr ilyagr force-pushed the use-pbcopy-on-mac branch from e84193d to f09a6db Compare May 25, 2025 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants