Skip to content
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

rc clipboard: Support system clipboard ineractions #4016

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lenormf
Copy link
Contributor

@lenormf lenormf commented Jan 8, 2021

This commit implements an API to interact with the system clipboard.

Similarly to how terminals are picked by x11.kak, the
clipboardcmd_copy and clipboardcmd_paste options detect which tool
should be used to interact with the system clipboard at startup. Only
xsel has been tested, but pbcopy/pbpaste and wl-copy/wl-paste
will be used if they're installed. These options can also be overridden
by the user to use xclip or any other tool they want.

Commands allow copying, pasting before/after, replacing
selections. Data to copy to the clipboard can also be passed directly
to clipboard-copy.

A convenience alias forwards clipboard-paste to
clipboard-paste-after to save users, who expect pasting to always
append, some brain cycles.

The clipboard-mode user mode is also declared to accommodate user
mappings, and assigned predictable key mappings.

Savvier users can enable register integration with
clipboard-enable-register, which currently only works one way: all
contents assigned to the c register is automatically forwarded to
the system clipboard. Vim uses the * and + registers, but
punctuation signs are generally associated with “core” primitives
in Kakoune, so using a letter register for this opt-in layer of
abstraction seemed more fitting.

Implementing the register integration in the opposite direction
in a portable way is not self-evident, and thus not implemented by
this patch.

Fixes #3935.

@lenormf lenormf marked this pull request as draft January 8, 2021 12:49
@lenormf
Copy link
Contributor Author

lenormf commented Jan 8, 2021

I've been using this for some time (albeit occasionally), if everyone is happy with the API I would write some documentation for the plugin.

It would also be great if some people could test the code, to make sure it's not broken and is not too awkward to use!

rc/tools/clipboard.kak Show resolved Hide resolved
rc/tools/clipboard.kak Show resolved Hide resolved
rc/tools/clipboard.kak Outdated Show resolved Hide resolved
rc/tools/clipboard.kak Outdated Show resolved Hide resolved
rc/tools/clipboard.kak Outdated Show resolved Hide resolved
rc/tools/clipboard.kak Outdated Show resolved Hide resolved
@lenormf lenormf force-pushed the fix-3935 branch 4 times, most recently from 9c61630 to 188bb55 Compare January 9, 2021 05:35
rc/tools/clipboard.kak Outdated Show resolved Hide resolved
@lenormf lenormf marked this pull request as ready for review January 10, 2021 18:37
@lenormf
Copy link
Contributor Author

lenormf commented Mar 3, 2021

PING

This commit implements an API to interact with the system clipboard.

Similarly to how terminals are picked by `x11.kak`, the
`clipboardcmd_copy` and `clipboardcmd_paste` options detect which tool
should be used to interact with the system clipboard at startup. Only
`xsel` has been tested, but `pbcopy`/`pbpaste` and `wl-copy`/`wl-paste`
will be used if they're installed. These options can also be overridden
by the user to use `xclip` or any other tool they want.

Commands allow copying, pasting before/after, replacing
selections. Data to copy to the clipboard can also be passed directly
to `clipboard-copy`.

A convenience alias forwards `clipboard-paste` to
`clipboard-paste-after` to save users, who expect pasting to always
append, some brain cycles.

The `clipboard-mode` user mode is also declared to accommodate user
mappings, and assigned predictable key mappings.

Savvier users can enable register integration with
`clipboard-enable-register`, which currently only works one way: all
contents assigned to the given register is automatically forwarded to
the system clipboard. Vim uses the `*` and `+` registers, but
punctuation signs are generally associated with “core” primitives
in Kakoune, so using a letter register for this opt-in layer of
abstraction seemed more fitting.

Implementing the register integration in the opposite direction
in a portable way is not self-evident, and thus not implemented by
this patch.

Fixes mawww#3935.
@lenormf
Copy link
Contributor Author

lenormf commented Nov 7, 2021

Drop the -mode suffix on the user mode.

Copy link
Contributor

@Screwtapello Screwtapello left a comment

Choose a reason for hiding this comment

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

As much as I'd love to have real native clipboard integration in Kakoune, or the hooks required to implement such a thing (a RegisterReadPre hook, for example, or some kind of "paste" and "paste request" events in the JSON UI protocol), that would take a bunch of thought and design and implementation work.

On the other hand, this PR solves 80% of the problem and is here now.

Given how often new users get confused or frustrated with clipboard integration, I think this is worth merging.

dquote) reg='"';;
esac

# NOTE: The following will break with the closing-brace register
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to worry, there is no closing-brace register

define-command -docstring %{
Paste the contents of the system clipboard before the selection
} clipboard-paste-before %{
execute-keys ! <space> %opt{clipboardcmd_paste} <ret>
Copy link
Contributor

Choose a reason for hiding this comment

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

the space is not necessary, everything that's run inside a command or mapping does not add to history.
Same for the mappings at the bottom of the file.

declare-option -docstring %{
The shell command which will paste the system clipboard into the current buffer
} str clipboardcmd_paste %sh{
for cmd in "xsel -o 2>/dev/null" "pbpaste" "wl-paste -p"; do
Copy link
Contributor

Choose a reason for hiding this comment

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

wl-paste needs the --no-newline switch to print the clipboard as-is

@Kratacoa
Copy link

bump?

@stacyharper
Copy link
Contributor

stacyharper commented Mar 28, 2023

I think Kakoune should, as Tmux does, support the set-clipboard escape sentence first. And then, eventually fallback to an auto-detected program. It make clipboard over ssh to works, and make things easier for Kakoune, as this rely to the terminal to do the connection with the wm clipboard.

ref: https://github.com/tmux/tmux/wiki/Clipboard

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.

Built-in clipboard integration
7 participants