diff --git a/helix-loader/src/lib.rs b/helix-loader/src/lib.rs index 4ff9df88d..60a9d9b9c 100644 --- a/helix-loader/src/lib.rs +++ b/helix-loader/src/lib.rs @@ -22,6 +22,8 @@ static SEARCH_HISTFILE: once_cell::sync::OnceCell = once_cell::sync::On static FILE_HISTFILE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); +static CLIPBOARD_FILE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); + pub fn initialize_config_file(specified_file: Option) { let config_file = specified_file.unwrap_or_else(default_config_file); ensure_parent_dir(&config_file); @@ -52,6 +54,12 @@ pub fn initialize_file_histfile(specified_file: Option) { FILE_HISTFILE.set(file_histfile).ok(); } +pub fn initialize_clipboard_file(specified_file: Option) { + let clipboard_file = specified_file.unwrap_or_else(default_clipboard_file); + ensure_parent_dir(&clipboard_file); + CLIPBOARD_FILE.set(clipboard_file).ok(); +} + /// A list of runtime directories from highest to lowest priority /// /// The priority is: @@ -191,6 +199,10 @@ pub fn file_histfile() -> PathBuf { FILE_HISTFILE.get().map(|path| path.to_path_buf()).unwrap() } +pub fn clipboard_file() -> PathBuf { + CLIPBOARD_FILE.get().map(|path| path.to_path_buf()).unwrap() +} + pub fn workspace_config_file() -> PathBuf { find_workspace().0.join(".helix").join("config.toml") } @@ -215,6 +227,10 @@ pub fn default_file_histfile() -> PathBuf { state_dir().join("file_history") } +pub fn default_clipboard_file() -> PathBuf { + state_dir().join("clipboard") +} + /// Merge two TOML documents, merging values from `right` onto `left` /// /// When an array exists in both `left` and `right`, `right`'s array is diff --git a/helix-loader/src/persistence.rs b/helix-loader/src/persistence.rs index 73e9d05bb..9cc5a516b 100644 --- a/helix-loader/src/persistence.rs +++ b/helix-loader/src/persistence.rs @@ -6,6 +6,20 @@ use std::{ path::PathBuf, }; +pub fn write_history(filepath: PathBuf, entries: &Vec) { + let file = OpenOptions::new() + .write(true) + .create(true) + .open(filepath) + // TODO: do something about this unwrap + .unwrap(); + + for entry in entries { + // TODO: do something about this unwrap + serialize_into(&file, &entry).unwrap(); + } +} + pub fn push_history(filepath: PathBuf, entry: &T) { let file = OpenOptions::new() .append(true) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d5c20a1e1..e2ce91603 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -169,6 +169,11 @@ impl Application { .write('/', persistence::read_search_history()) // TODO: do something about this unwrap .unwrap(); + editor + .registers + .write('"', persistence::read_clipboard_file()) + // TODO: do something about this unwrap + .unwrap(); let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| { &config.keys diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6e037a471..f555e9833 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -44,6 +44,7 @@ use helix_view::{ info::Info, input::KeyEvent, keyboard::KeyCode, + persistence, theme::Style, tree, view::View, @@ -4205,6 +4206,8 @@ fn yank_impl(editor: &mut Editor, register: char) { .collect(); let selections = values.len(); + persistence::write_clipboard_file(&values); + match editor.registers.write(register, values) { Ok(_) => editor.set_status(format!( "yanked {selections} selection{} to register {register}", diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 02df55c7a..ff0586b41 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -83,6 +83,7 @@ FLAGS: helix_loader::initialize_command_histfile(None); helix_loader::initialize_search_histfile(None); helix_loader::initialize_file_histfile(None); + helix_loader::initialize_clipboard_file(None); // Help has a higher priority and should be handled separately. if args.display_help { diff --git a/helix-term/tests/test/helpers.rs b/helix-term/tests/test/helpers.rs index 3e4c952c2..623f5df3e 100644 --- a/helix-term/tests/test/helpers.rs +++ b/helix-term/tests/test/helpers.rs @@ -230,7 +230,7 @@ pub fn test_syntax_loader(overrides: Option) -> helix_core::syntax::Load helix_core::syntax::Loader::new(lang.try_into().unwrap()).unwrap() } -fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath)> { +fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath, TempPath)> { let command_file = NamedTempFile::new()?; let command_path = command_file.into_temp_path(); helix_loader::initialize_command_histfile(Some(command_path.to_path_buf())); @@ -243,7 +243,11 @@ fn init_persistence_files() -> anyhow::Result<(TempPath, TempPath, TempPath)> { let file_path = file_file.into_temp_path(); helix_loader::initialize_file_histfile(Some(file_path.to_path_buf())); - Ok((command_path, search_path, file_path)) + let clipboard_file = NamedTempFile::new()?; + let clipboard_path = clipboard_file.into_temp_path(); + helix_loader::initialize_clipboard_file(Some(clipboard_path.to_path_buf())); + + Ok((command_path, search_path, file_path, clipboard_path)) } /// Use this for very simple test cases where there is one input @@ -253,7 +257,7 @@ pub async fn test_with_config>( app_builder: AppBuilder, test_case: T, ) -> anyhow::Result<()> { - let (_, _, _) = init_persistence_files()?; + let (_, _, _, _) = init_persistence_files()?; let test_case = test_case.into(); let app = app_builder.build()?; diff --git a/helix-view/src/persistence.rs b/helix-view/src/persistence.rs index bb02d7714..9f0235769 100644 --- a/helix-view/src/persistence.rs +++ b/helix-view/src/persistence.rs @@ -1,7 +1,7 @@ use helix_core::Selection; use helix_loader::{ - command_histfile, file_histfile, - persistence::{push_history, read_history}, + clipboard_file, command_histfile, file_histfile, + persistence::{push_history, read_history, write_history}, search_histfile, }; use serde::{Deserialize, Serialize}; @@ -59,3 +59,11 @@ pub fn read_search_history() -> Vec { hist.reverse(); hist } + +pub fn write_clipboard_file(values: &Vec) { + write_history(clipboard_file(), values) +} + +pub fn read_clipboard_file() -> Vec { + read_history(clipboard_file()) +}