1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-03 04:34:21 -05:00

Allow copying selection to primary clipboard

A new configuration option `save_to_clipboard` has been added
to the `selection` section of the configuration. This allows writing
every selection to the primary system clipboard when it is set
to `true`.

On linux the selection is still written to the selection clipboard,
independent of the value of the `save_to_clipboard` setting.
This commit is contained in:
Gris Ge 2018-09-24 20:12:45 +08:00 committed by Christian Duerr
parent cd79680ba2
commit a752066bfa
6 changed files with 30 additions and 8 deletions

View file

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
viewport to extend to the top of the window.
- `buttonless` - Similar to transparent but also removed the buttons.
- Add support for changing the colors from 16 to 256 in the `indexed_colors` config section
- Add `save_to_clipboard` configuration option for copying selected text to the system clipboard
### Changed

View file

@ -262,6 +262,11 @@ mouse:
selection:
semantic_escape_chars: ",│`|:\"' ()[]{}<>"
# When set to `true`, selected text will be copied to both the primary and
# the selection clipboard. Otherwise, it will only be copied to the selection
# clipboard.
save_to_clipboard: false
dynamic_title: true
hide_cursor_when_typing: false

View file

@ -260,6 +260,9 @@ mouse:
selection:
semantic_escape_chars: ",│`|:\"' ()[]{}<>"
# When set to `true`, selected text will be copied to the primary clipboard.
save_to_clipboard: false
dynamic_title: true
hide_cursor_when_typing: false

View file

@ -40,12 +40,15 @@ fn true_bool() -> bool {
#[derive(Clone, Debug, Deserialize)]
pub struct Selection {
pub semantic_escape_chars: String,
#[serde(default, deserialize_with = "failure_default")]
pub save_to_clipboard: bool,
}
impl Default for Selection {
fn default() -> Selection {
Selection {
semantic_escape_chars: String::new()
semantic_escape_chars: String::new(),
save_to_clipboard: false
}
}
}

View file

@ -8,7 +8,7 @@ use std::time::{Instant};
use serde_json as json;
use parking_lot::MutexGuard;
use glutin::{self, ModifiersState, Event, ElementState};
use copypasta::{Clipboard, Load, Store};
use copypasta::{Clipboard, Load, Store, Buffer as ClipboardBuffer};
use ansi::{Handler, ClearMode};
use grid::Scroll;
@ -64,7 +64,7 @@ impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> {
self.terminal.clear_screen(ClearMode::Saved);
}
fn copy_selection(&self, buffer: ::copypasta::Buffer) {
fn copy_selection(&self, buffer: ClipboardBuffer) {
if let Some(selected) = self.terminal.selection_to_string() {
if !selected.is_empty() {
Clipboard::new()
@ -238,6 +238,7 @@ pub struct Processor<N> {
last_modifiers: ModifiersState,
pending_events: Vec<Event>,
window_changes: WindowChanges,
save_to_clipboard: bool,
}
/// Notify that the terminal was resized
@ -281,6 +282,7 @@ impl<N: Notify> Processor<N> {
last_modifiers: Default::default(),
pending_events: Vec::with_capacity(4),
window_changes: Default::default(),
save_to_clipboard: config.selection().save_to_clipboard,
}
}
@ -442,6 +444,7 @@ impl<N: Notify> Processor<N> {
mouse_config: &self.mouse_config,
key_bindings: &self.key_bindings[..],
mouse_bindings: &self.mouse_bindings[..],
save_to_clipboard: self.save_to_clipboard,
};
let mut window_is_focused = window.is_focused;
@ -496,5 +499,6 @@ impl<N: Notify> Processor<N> {
self.key_bindings = config.key_bindings().to_vec();
self.mouse_bindings = config.mouse_bindings().to_vec();
self.mouse_config = config.mouse().to_owned();
self.save_to_clipboard = config.selection().save_to_clipboard;
}
}

View file

@ -24,7 +24,7 @@ use std::process::Command;
use std::time::Instant;
use std::os::unix::process::CommandExt;
use copypasta::{Clipboard, Load, Buffer};
use copypasta::{Clipboard, Load, Buffer as ClipboardBuffer};
use glutin::{ElementState, VirtualKeyCode, MouseButton, TouchPhase, MouseScrollDelta, ModifiersState};
use config;
@ -49,13 +49,14 @@ pub struct Processor<'a, A: 'a> {
pub mouse_config: &'a config::Mouse,
pub scrolling_config: &'a config::Scrolling,
pub ctx: A,
pub save_to_clipboard: bool,
}
pub trait ActionContext {
fn write_to_pty<B: Into<Cow<'static, [u8]>>>(&mut self, B);
fn terminal_mode(&self) -> TermMode;
fn size_info(&self) -> SizeInfo;
fn copy_selection(&self, Buffer);
fn copy_selection(&self, ClipboardBuffer);
fn clear_selection(&mut self);
fn update_selection(&mut self, point: Point, side: Side);
fn simple_selection(&mut self, point: Point, side: Side);
@ -206,7 +207,7 @@ impl Action {
ctx.write_to_pty(s.clone().into_bytes())
},
Action::Copy => {
ctx.copy_selection(Buffer::Primary);
ctx.copy_selection(ClipboardBuffer::Primary);
},
Action::Paste => {
Clipboard::new()
@ -469,7 +470,10 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> {
return;
}
self.ctx.copy_selection(Buffer::Selection);
if self.save_to_clipboard {
self.ctx.copy_selection(ClipboardBuffer::Primary);
}
self.ctx.copy_selection(ClipboardBuffer::Selection);
}
pub fn on_mouse_wheel(&mut self, delta: MouseScrollDelta, phase: TouchPhase, modifiers: ModifiersState) {
@ -688,6 +692,7 @@ mod tests {
use grid::Scroll;
use super::{Action, Binding, Processor};
use copypasta::Buffer as ClipboardBuffer;
const KEY: VirtualKeyCode = VirtualKeyCode::Key0;
@ -723,7 +728,7 @@ mod tests {
*self.size_info
}
fn copy_selection(&self, _buffer: ::copypasta::Buffer) {
fn copy_selection(&self, _buffer: ClipboardBuffer) {
// STUBBED
}
@ -824,6 +829,7 @@ mod tests {
scrolling_config: &config::Scrolling::default(),
key_bindings: &config.key_bindings()[..],
mouse_bindings: &config.mouse_bindings()[..],
save_to_clipboard: config.selection().save_to_clipboard
};
if let Event::WindowEvent { event: WindowEvent::MouseInput { state, button, modifiers, .. }, .. } = $input {