| 1 |
|
| 2 |
|
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
use std::borrow::Cow; |
| 5 |
use thiserror::Error; |
| 6 |
|
| 7 |
#[derive(Clone, Copy)] |
| 8 |
pub enum ClipboardType { |
| 9 |
Clipboard, |
| 10 |
Selection, |
| 11 |
} |
| 12 |
|
| 13 |
#[derive(Debug, Error)] |
| 14 |
pub enum ClipboardError { |
| 15 |
#[error(transparent)] |
| 16 |
IoError(#[from] std::io::Error), |
| 17 |
#[error("could not convert terminal output to UTF-8: {0}")] |
| 18 |
FromUtf8Error(#[from] std::string::FromUtf8Error), |
| 19 |
#[cfg(windows)] |
| 20 |
#[error("Windows API error: {0}")] |
| 21 |
WinAPI(#[from] clipboard_win::ErrorCode), |
| 22 |
#[error("clipboard provider command failed")] |
| 23 |
CommandFailed, |
| 24 |
#[error("failed to write to clipboard provider's stdin")] |
| 25 |
StdinWriteFailed, |
| 26 |
#[error("clipboard provider did not return any contents")] |
| 27 |
MissingStdout, |
| 28 |
#[error("This clipboard provider does not support reading")] |
| 29 |
ReadingNotSupported, |
| 30 |
} |
| 31 |
|
| 32 |
type Result<T> = std::result::Result<T, ClipboardError>; |
| 33 |
|
| 34 |
#[cfg(not(target_arch = "wasm32"))] |
| 35 |
pub use external::ClipboardProvider; |
| 36 |
#[cfg(target_arch = "wasm32")] |
| 37 |
pub use noop::ClipboardProvider; |
| 38 |
|
| 39 |
|
| 40 |
#[cfg(target_arch = "wasm32")] |
| 41 |
mod noop { |
| 42 |
use super::*; |
| 43 |
|
| 44 |
#[derive(Debug, Clone)] |
| 45 |
pub enum ClipboardProvider {} |
| 46 |
|
| 47 |
impl ClipboardProvider { |
| 48 |
pub fn detect() -> Self { |
| 49 |
Self |
| 50 |
} |
| 51 |
|
| 52 |
pub fn name(&self) -> Cow<str> { |
| 53 |
"none".into() |
| 54 |
} |
| 55 |
|
| 56 |
pub fn get_contents(&self, _clipboard_type: ClipboardType) -> Result<String> { |
| 57 |
Err(ClipboardError::ReadingNotSupported) |
| 58 |
} |
| 59 |
|
| 60 |
pub fn set_contents(&self, _content: &str, _clipboard_type: ClipboardType) -> Result<()> { |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg(not(target_arch = "wasm32"))] |
| 67 |
mod external { |
| 68 |
use super::*; |
| 69 |
|
| 70 |
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 71 |
pub struct Command { |
| 72 |
command: Cow<'static, str>, |
| 73 |
#[serde(default)] |
| 74 |
args: Cow<'static, [Cow<'static, str>]>, |
| 75 |
} |
| 76 |
|
| 77 |
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 78 |
#[serde(rename_all = "kebab-case")] |
| 79 |
pub struct CommandProvider { |
| 80 |
yank: Command, |
| 81 |
paste: Command, |
| 82 |
yank_primary: Option<Command>, |
| 83 |
paste_primary: Option<Command>, |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 87 |
#[serde(rename_all = "kebab-case")] |
| 88 |
#[allow(clippy::large_enum_variant)] |
| 89 |
pub enum ClipboardProvider { |
| 90 |
Pasteboard, |
| 91 |
Wayland, |
| 92 |
XClip, |
| 93 |
XSel, |
| 94 |
Win32Yank, |
| 95 |
Tmux, |
| 96 |
#[cfg(windows)] |
| 97 |
Windows, |
| 98 |
Termux, |
| 99 |
#[cfg(feature = "term")] |
| 100 |
Termcode, |
| 101 |
Custom(CommandProvider), |
| 102 |
None, |
| 103 |
} |
| 104 |
|
| 105 |
impl Default for ClipboardProvider { |
| 106 |
#[cfg(windows)] |
| 107 |
fn default() -> Self { |
| 108 |
use helix_stdx::env::binary_exists; |
| 109 |
|
| 110 |
if binary_exists("win32yank.exe") { |
| 111 |
Self::Win32Yank |
| 112 |
} else { |
| 113 |
Self::Windows |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(target_os = "macos")] |
| 118 |
fn default() -> Self { |
| 119 |
use helix_stdx::env::{binary_exists, env_var_is_set}; |
| 120 |
|
| 121 |
if env_var_is_set("TMUX") && binary_exists("tmux") { |
| 122 |
Self::Tmux |
| 123 |
} else if binary_exists("pbcopy") && binary_exists("pbpaste") { |
| 124 |
Self::Pasteboard |
| 125 |
} else { |
| 126 |
#[cfg(feature = "term")] |
| 127 |
return Self::Termcode; |
| 128 |
#[cfg(not(feature = "term"))] |
| 129 |
return Self::None; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[cfg(not(any(windows, target_os = "macos")))] |
| 134 |
fn default() -> Self { |
| 135 |
use helix_stdx::env::{binary_exists, env_var_is_set}; |
| 136 |
|
| 137 |
fn is_exit_success(program: &str, args: &[&str]) -> bool { |
| 138 |
std::process::Command::new(program) |
| 139 |
.args(args) |
| 140 |
.output() |
| 141 |
.ok() |
| 142 |
.and_then(|out| out.status.success().then_some(())) |
| 143 |
.is_some() |
| 144 |
} |
| 145 |
|
| 146 |
if binary_exists("termux-clipboard-set") && binary_exists("termux-clipboard-get") { |
| 147 |
Self::Termux |
| 148 |
} else if env_var_is_set("TMUX") && binary_exists("tmux") { |
| 149 |
Self::Tmux |
| 150 |
} else if cfg!(feature = "term") |
| 151 |
&& env_var_is_set("WEZTERM_UNIX_SOCKET") |
| 152 |
&& binary_exists("wezterm") |
| 153 |
{ |
| 154 |
#[cfg(feature = "term")] |
| 155 |
return Self::Termcode; |
| 156 |
#[cfg(not(feature = "term"))] |
| 157 |
return Self::None; |
| 158 |
} else if env_var_is_set("WAYLAND_DISPLAY") |
| 159 |
&& binary_exists("wl-copy") |
| 160 |
&& binary_exists("wl-paste") |
| 161 |
{ |
| 162 |
Self::Wayland |
| 163 |
} else if env_var_is_set("DISPLAY") && binary_exists("xclip") { |
| 164 |
Self::XClip |
| 165 |
} else if env_var_is_set("DISPLAY") |
| 166 |
&& binary_exists("xsel") |
| 167 |
|
| 168 |
&& is_exit_success("xsel", &["-o", "-b"]) |
| 169 |
{ |
| 170 |
Self::XSel |
| 171 |
} else if binary_exists("win32yank.exe") { |
| 172 |
Self::Win32Yank |
| 173 |
} else { |
| 174 |
#[cfg(feature = "term")] |
| 175 |
return Self::Termcode; |
| 176 |
#[cfg(not(feature = "term"))] |
| 177 |
return Self::None; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl ClipboardProvider { |
| 183 |
pub fn name(&self) -> Cow<'_, str> { |
| 184 |
fn builtin_name<'a>( |
| 185 |
name: &'static str, |
| 186 |
provider: &'static CommandProvider, |
| 187 |
) -> Cow<'a, str> { |
| 188 |
if provider.yank.command != provider.paste.command { |
| 189 |
Cow::Owned(format!( |
| 190 |
"{} ({}+{})", |
| 191 |
name, provider.yank.command, provider.paste.command |
| 192 |
)) |
| 193 |
} else { |
| 194 |
Cow::Owned(format!("{} ({})", name, provider.yank.command)) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
match self { |
| 199 |
|
| 200 |
Self::Pasteboard => builtin_name("pasteboard", &PASTEBOARD), |
| 201 |
Self::Wayland => builtin_name("wayland", &WL_CLIPBOARD), |
| 202 |
Self::XClip => builtin_name("x-clip", &XCLIP), |
| 203 |
Self::XSel => builtin_name("x-sel", &XSEL), |
| 204 |
Self::Win32Yank => builtin_name("win32-yank", &WIN32), |
| 205 |
Self::Tmux => builtin_name("tmux", &TMUX), |
| 206 |
Self::Termux => builtin_name("termux", &TERMUX), |
| 207 |
#[cfg(windows)] |
| 208 |
Self::Windows => "windows".into(), |
| 209 |
#[cfg(feature = "term")] |
| 210 |
Self::Termcode => "termcode".into(), |
| 211 |
Self::Custom(command_provider) => Cow::Owned(format!( |
| 212 |
"custom ({}+{})", |
| 213 |
command_provider.yank.command, command_provider.paste.command |
| 214 |
)), |
| 215 |
Self::None => "none".into(), |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
pub fn get_contents(&self, clipboard_type: &ClipboardType) -> Result<String> { |
| 220 |
fn yank_from_builtin( |
| 221 |
provider: CommandProvider, |
| 222 |
clipboard_type: &ClipboardType, |
| 223 |
) -> Result<String> { |
| 224 |
match clipboard_type { |
| 225 |
ClipboardType::Clipboard => execute_command(&provider.yank, None, true)? |
| 226 |
.ok_or(ClipboardError::MissingStdout), |
| 227 |
ClipboardType::Selection => { |
| 228 |
if let Some(cmd) = provider.yank_primary.as_ref() { |
| 229 |
return execute_command(cmd, None, true)? |
| 230 |
.ok_or(ClipboardError::MissingStdout); |
| 231 |
} |
| 232 |
|
| 233 |
Ok(String::new()) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
match self { |
| 239 |
Self::Pasteboard => yank_from_builtin(PASTEBOARD, clipboard_type), |
| 240 |
Self::Wayland => yank_from_builtin(WL_CLIPBOARD, clipboard_type), |
| 241 |
Self::XClip => yank_from_builtin(XCLIP, clipboard_type), |
| 242 |
Self::XSel => yank_from_builtin(XSEL, clipboard_type), |
| 243 |
Self::Win32Yank => yank_from_builtin(WIN32, clipboard_type), |
| 244 |
Self::Tmux => yank_from_builtin(TMUX, clipboard_type), |
| 245 |
Self::Termux => yank_from_builtin(TERMUX, clipboard_type), |
| 246 |
#[cfg(target_os = "windows")] |
| 247 |
Self::Windows => match clipboard_type { |
| 248 |
ClipboardType::Clipboard => { |
| 249 |
let contents = |
| 250 |
clipboard_win::get_clipboard(clipboard_win::formats::Unicode)?; |
| 251 |
Ok(contents) |
| 252 |
} |
| 253 |
ClipboardType::Selection => Ok(String::new()), |
| 254 |
}, |
| 255 |
#[cfg(feature = "term")] |
| 256 |
Self::Termcode => Err(ClipboardError::ReadingNotSupported), |
| 257 |
Self::Custom(command_provider) => { |
| 258 |
execute_command(&command_provider.yank, None, true)? |
| 259 |
.ok_or(ClipboardError::MissingStdout) |
| 260 |
} |
| 261 |
Self::None => Err(ClipboardError::ReadingNotSupported), |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
pub fn set_contents(&self, content: &str, clipboard_type: ClipboardType) -> Result<()> { |
| 266 |
fn paste_to_builtin( |
| 267 |
provider: CommandProvider, |
| 268 |
content: &str, |
| 269 |
clipboard_type: ClipboardType, |
| 270 |
) -> Result<()> { |
| 271 |
let cmd = match clipboard_type { |
| 272 |
ClipboardType::Clipboard => &provider.paste, |
| 273 |
ClipboardType::Selection => { |
| 274 |
if let Some(cmd) = provider.paste_primary.as_ref() { |
| 275 |
cmd |
| 276 |
} else { |
| 277 |
return Ok(()); |
| 278 |
} |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
execute_command(cmd, Some(content), false).map(|_| ()) |
| 283 |
} |
| 284 |
|
| 285 |
match self { |
| 286 |
Self::Pasteboard => paste_to_builtin(PASTEBOARD, content, clipboard_type), |
| 287 |
Self::Wayland => paste_to_builtin(WL_CLIPBOARD, content, clipboard_type), |
| 288 |
Self::XClip => paste_to_builtin(XCLIP, content, clipboard_type), |
| 289 |
Self::XSel => paste_to_builtin(XSEL, content, clipboard_type), |
| 290 |
Self::Win32Yank => paste_to_builtin(WIN32, content, clipboard_type), |
| 291 |
Self::Tmux => paste_to_builtin(TMUX, content, clipboard_type), |
| 292 |
Self::Termux => paste_to_builtin(TERMUX, content, clipboard_type), |
| 293 |
#[cfg(target_os = "windows")] |
| 294 |
Self::Windows => match clipboard_type { |
| 295 |
ClipboardType::Clipboard => { |
| 296 |
clipboard_win::set_clipboard(clipboard_win::formats::Unicode, content)?; |
| 297 |
Ok(()) |
| 298 |
} |
| 299 |
ClipboardType::Selection => Ok(()), |
| 300 |
}, |
| 301 |
#[cfg(feature = "term")] |
| 302 |
Self::Termcode => { |
| 303 |
use std::io::Write; |
| 304 |
use termina::escape::osc::{self, Osc}; |
| 305 |
let selection = match clipboard_type { |
| 306 |
ClipboardType::Clipboard => osc::Selection::CLIPBOARD, |
| 307 |
ClipboardType::Selection => osc::Selection::PRIMARY, |
| 308 |
}; |
| 309 |
|
| 310 |
|
| 311 |
let mut stdout = std::io::stdout().lock(); |
| 312 |
write!(stdout, "{}", Osc::SetSelection(selection, content))?; |
| 313 |
stdout.flush()?; |
| 314 |
Ok(()) |
| 315 |
} |
| 316 |
Self::Custom(command_provider) => match clipboard_type { |
| 317 |
ClipboardType::Clipboard => { |
| 318 |
execute_command(&command_provider.paste, Some(content), false).map(|_| ()) |
| 319 |
} |
| 320 |
ClipboardType::Selection => { |
| 321 |
if let Some(cmd) = &command_provider.paste_primary { |
| 322 |
execute_command(cmd, Some(content), false).map(|_| ()) |
| 323 |
} else { |
| 324 |
Ok(()) |
| 325 |
} |
| 326 |
} |
| 327 |
}, |
| 328 |
Self::None => Ok(()), |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
macro_rules! command_provider { |
| 334 |
($name:ident, |
| 335 |
yank => $yank_cmd:literal $( , $yank_arg:literal )* ; |
| 336 |
paste => $paste_cmd:literal $( , $paste_arg:literal )* ; ) => { |
| 337 |
const $name: CommandProvider = CommandProvider { |
| 338 |
yank: Command { |
| 339 |
command: Cow::Borrowed($yank_cmd), |
| 340 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($yank_arg) ),* ]) |
| 341 |
}, |
| 342 |
paste: Command { |
| 343 |
command: Cow::Borrowed($paste_cmd), |
| 344 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($paste_arg) ),* ]) |
| 345 |
}, |
| 346 |
yank_primary: None, |
| 347 |
paste_primary: None, |
| 348 |
}; |
| 349 |
}; |
| 350 |
($name:ident, |
| 351 |
yank => $yank_cmd:literal $( , $yank_arg:literal )* ; |
| 352 |
paste => $paste_cmd:literal $( , $paste_arg:literal )* ; |
| 353 |
yank_primary => $yank_primary_cmd:literal $( , $yank_primary_arg:literal )* ; |
| 354 |
paste_primary => $paste_primary_cmd:literal $( , $paste_primary_arg:literal )* ; ) => { |
| 355 |
const $name: CommandProvider = CommandProvider { |
| 356 |
yank: Command { |
| 357 |
command: Cow::Borrowed($yank_cmd), |
| 358 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($yank_arg) ),* ]) |
| 359 |
}, |
| 360 |
paste: Command { |
| 361 |
command: Cow::Borrowed($paste_cmd), |
| 362 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($paste_arg) ),* ]) |
| 363 |
}, |
| 364 |
yank_primary: Some(Command { |
| 365 |
command: Cow::Borrowed($yank_primary_cmd), |
| 366 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($yank_primary_arg) ),* ]) |
| 367 |
}), |
| 368 |
paste_primary: Some(Command { |
| 369 |
command: Cow::Borrowed($paste_primary_cmd), |
| 370 |
args: Cow::Borrowed(&[ $( Cow::Borrowed($paste_primary_arg) ),* ]) |
| 371 |
}), |
| 372 |
}; |
| 373 |
}; |
| 374 |
} |
| 375 |
|
| 376 |
command_provider! { |
| 377 |
TMUX, |
| 378 |
yank => "tmux", "save-buffer", "-"; |
| 379 |
paste => "tmux", "load-buffer", "-w", "-"; |
| 380 |
} |
| 381 |
command_provider! { |
| 382 |
PASTEBOARD, |
| 383 |
yank => "pbpaste"; |
| 384 |
paste => "pbcopy"; |
| 385 |
} |
| 386 |
command_provider! { |
| 387 |
WL_CLIPBOARD, |
| 388 |
yank => "wl-paste", "--no-newline"; |
| 389 |
paste => "wl-copy", "--type", "text/plain"; |
| 390 |
yank_primary => "wl-paste", "-p", "--no-newline"; |
| 391 |
paste_primary => "wl-copy", "-p", "--type", "text/plain"; |
| 392 |
} |
| 393 |
command_provider! { |
| 394 |
XCLIP, |
| 395 |
yank => "xclip", "-o", "-selection", "clipboard"; |
| 396 |
paste => "xclip", "-i", "-selection", "clipboard"; |
| 397 |
yank_primary => "xclip", "-o"; |
| 398 |
paste_primary => "xclip", "-i"; |
| 399 |
} |
| 400 |
command_provider! { |
| 401 |
XSEL, |
| 402 |
yank => "xsel", "-o", "-b"; |
| 403 |
paste => "xsel", "-i", "-b"; |
| 404 |
yank_primary => "xsel", "-o"; |
| 405 |
paste_primary => "xsel", "-i"; |
| 406 |
} |
| 407 |
command_provider! { |
| 408 |
WIN32, |
| 409 |
yank => "win32yank.exe", "-o", "--lf"; |
| 410 |
paste => "win32yank.exe", "-i", "--crlf"; |
| 411 |
} |
| 412 |
command_provider! { |
| 413 |
TERMUX, |
| 414 |
yank => "termux-clipboard-get"; |
| 415 |
paste => "termux-clipboard-set"; |
| 416 |
} |
| 417 |
|
| 418 |
fn execute_command( |
| 419 |
cmd: &Command, |
| 420 |
input: Option<&str>, |
| 421 |
pipe_output: bool, |
| 422 |
) -> Result<Option<String>> { |
| 423 |
use std::io::Write; |
| 424 |
use std::process::{Command, Stdio}; |
| 425 |
|
| 426 |
let stdin = input.map(|_| Stdio::piped()).unwrap_or_else(Stdio::null); |
| 427 |
let stdout = pipe_output.then(Stdio::piped).unwrap_or_else(Stdio::null); |
| 428 |
|
| 429 |
let mut command: Command = Command::new(cmd.command.as_ref()); |
| 430 |
|
| 431 |
#[allow(unused_mut)] |
| 432 |
let mut command_mut: &mut Command = command |
| 433 |
.args(cmd.args.iter().map(AsRef::as_ref)) |
| 434 |
.stdin(stdin) |
| 435 |
.stdout(stdout) |
| 436 |
.stderr(Stdio::null()); |
| 437 |
|
| 438 |
|
| 439 |
#[cfg(unix)] |
| 440 |
{ |
| 441 |
use std::os::unix::process::CommandExt; |
| 442 |
|
| 443 |
unsafe { |
| 444 |
command_mut = command_mut.pre_exec(|| match libc::setsid() { |
| 445 |
-1 => Err(std::io::Error::last_os_error()), |
| 446 |
_ => Ok(()), |
| 447 |
}); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
let mut child = command_mut.spawn()?; |
| 452 |
|
| 453 |
if let Some(input) = input { |
| 454 |
let mut stdin = child.stdin.take().ok_or(ClipboardError::StdinWriteFailed)?; |
| 455 |
stdin |
| 456 |
.write_all(input.as_bytes()) |
| 457 |
.map_err(|_| ClipboardError::StdinWriteFailed)?; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
let output = child.wait_with_output()?; |
| 462 |
|
| 463 |
if !output.status.success() { |
| 464 |
log::error!( |
| 465 |
"clipboard provider {} failed with stderr: \"{}\"", |
| 466 |
cmd.command, |
| 467 |
String::from_utf8_lossy(&output.stderr) |
| 468 |
); |
| 469 |
return Err(ClipboardError::CommandFailed); |
| 470 |
} |
| 471 |
|
| 472 |
if pipe_output { |
| 473 |
Ok(Some(String::from_utf8(output.stdout)?)) |
| 474 |
} else { |
| 475 |
Ok(None) |
| 476 |
} |
| 477 |
} |
| 478 |
} |