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

Sync tmux clipboard and system clipboard #1343

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You may also specify a file to use for configuration with the `-c` or
| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
| `auto-info` | Whether to display infoboxes | `true` |
| `tmux-system-clipboard` | Sync with the system clipboard when running in tmux. | `true` |
| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` |
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
Expand Down
5 changes: 3 additions & 2 deletions helix-term/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crossterm::{
use helix_core::config::{default_syntax_loader, user_syntax_loader};
use helix_loader::grammar::load_runtime_file;
use helix_view::clipboard::get_clipboard_provider;
use helix_view::editor::Config;
use std::io::Write;

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -53,7 +54,7 @@ pub fn general() -> std::io::Result<()> {
let lang_file = helix_loader::lang_config_file();
let log_file = helix_loader::log_file();
let rt_dir = helix_loader::runtime_dir();
let clipboard_provider = get_clipboard_provider();
let clipboard_provider = get_clipboard_provider(&Config::default());

if config_file.exists() {
writeln!(stdout, "Config file: {}", config_file.display())?;
Expand Down Expand Up @@ -87,7 +88,7 @@ pub fn clipboard() -> std::io::Result<()> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();

let board = get_clipboard_provider();
let board = get_clipboard_provider(&Config::default());
match board.name().as_ref() {
"none" => {
writeln!(
Expand Down
24 changes: 17 additions & 7 deletions helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use anyhow::Result;
use std::borrow::Cow;

use crate::editor::Config;

pub enum ClipboardType {
Clipboard,
Selection,
Expand Down Expand Up @@ -66,12 +68,12 @@ macro_rules! command_provider {
}

#[cfg(windows)]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
pub fn get_clipboard_provider(_: &Config) -> Box<dyn ClipboardProvider> {
Box::new(provider::WindowsProvider::default())
}

#[cfg(target_os = "macos")]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
pub fn get_clipboard_provider(_: &Config) -> Box<dyn ClipboardProvider> {
use provider::command::exists;

if exists("pbcopy") && exists("pbpaste") {
Expand All @@ -85,13 +87,13 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
}

#[cfg(target_os = "wasm32")]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
pub fn get_clipboard_provider(_: &Config) -> Box<dyn ClipboardProvider> {
// TODO:
Box::new(provider::NopProvider::new())
}

#[cfg(not(any(windows, target_os = "wasm32", target_os = "macos")))]
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
pub fn get_clipboard_provider(config: &Config) -> Box<dyn ClipboardProvider> {
use provider::command::{env_var_is_set, exists, is_exit_success};
// TODO: support for user-defined provider, probably when we have plugin support by setting a
// variable?
Expand Down Expand Up @@ -130,9 +132,17 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
copy => "termux-clipboard-set";
}
} else if env_var_is_set("TMUX") && exists("tmux") {
command_provider! {
paste => "tmux", "save-buffer", "-";
copy => "tmux", "load-buffer", "-";
if config.tmux_system_clipboard {
command_provider! {
// Refresh tmux clipboard, wait a bit for it to be updated and paste it
paste => "sh", "-c", "tmux refresh-client -l; sleep 0.1; tmux save-buffer -";
copy => "tmux", "load-buffer", "-w", "-";
}
Comment on lines +135 to +140
Copy link
Member

Choose a reason for hiding this comment

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

How about adding a editor.clipboard.paste/editor.clipboard.copy (now that I think about it, copy -> yank) section to the configuration? That way you're able to set this in config.toml

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, ideally I’d like the clipboard to just work out of the box. (Apart from this PR, there’s also OSC 52 support missing (which are terminal escape sequences), they are needed to support copy and paste e.g. in alacritty through ssh.)

How do you propose should the config look like if these commands should be used inside tmux, but the other built-in commands should be used outside tmux, e.g. when DISPLAY is set.

Copy link
Contributor

Choose a reason for hiding this comment

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

If that config is enabled but tmux is not enabled won't it have an issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean with the tmux-system-clipboard from this PR? That shouldn’t be a problem. It’s only used when helix is running inside tmux.

With editor.clipboard.paste commands set in the config that are executed regardless of the environment that helix runs in, that would be a problem.

} else {
command_provider! {
paste => "tmux", "save-buffer", "-";
copy => "tmux", "load-buffer", "-";
}
}
} else {
Box::new(provider::NopProvider::new())
Expand Down
5 changes: 4 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub struct Config {
pub statusline: StatusLineConfig,
/// Shape for cursor in each mode
pub cursor_shape: CursorShapeConfig,
/// Sync with the system clipboard when running in tmux. Defaults to `true`.
pub tmux_system_clipboard: bool,
/// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`.
pub true_color: bool,
/// Search configuration.
Expand Down Expand Up @@ -548,6 +550,7 @@ impl Default for Config {
file_picker: FilePickerConfig::default(),
statusline: StatusLineConfig::default(),
cursor_shape: CursorShapeConfig::default(),
tmux_system_clipboard: true,
true_color: false,
search: SearchConfig::default(),
lsp: LspConfig::default(),
Expand Down Expand Up @@ -697,7 +700,7 @@ impl Editor {
theme_loader,
last_theme: None,
registers: Registers::default(),
clipboard_provider: get_clipboard_provider(),
clipboard_provider: get_clipboard_provider(&*conf),
status_msg: None,
autoinfo: None,
idle_timer: Box::pin(sleep(conf.idle_timeout)),
Expand Down