Skip to content

Commit

Permalink
Implement black hole register
Browse files Browse the repository at this point in the history
  • Loading branch information
ath3 committed Nov 25, 2021
1 parent 67bf425 commit 5972f66
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
2 changes: 2 additions & 0 deletions book/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ If there is a selected register before invoking a change or delete command, the
| `/` | Last search |
| `:` | Last executed command |
| `"` | Last yanked text |
| `_` | Black hole |

> There is no special register for copying to system clipboard, instead special commands and keybindings are provided. See the [keymap](keymap.md#space-mode) for the specifics.
> The black hole register works as a no-op register, meaning no data will be written to / read from it.
## Surround

Expand Down
40 changes: 28 additions & 12 deletions helix-core/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ pub struct Register {
}

impl Register {
pub const fn new(name: char) -> Self {
Self {
name,
values: Vec::new(),
pub const fn new(name: char) -> Option<Self> {
if name != '_' {
Some(Self {
name,
values: Vec::new(),
})
} else {
None
}
}

pub fn new_with_values(name: char, values: Vec<String>) -> Self {
Self { name, values }
pub fn new_with_values(name: char, values: Vec<String>) -> Option<Self> {
if name != '_' {
Some(Self { name, values })
} else {
None
}
}

pub const fn name(&self) -> char {
Expand Down Expand Up @@ -46,15 +54,23 @@ impl Registers {
self.inner.get(&name)
}

pub fn get_mut(&mut self, name: char) -> &mut Register {
self.inner
.entry(name)
.or_insert_with(|| Register::new(name))
pub fn get_mut(&mut self, name: char) -> Option<&mut Register> {
if name != '_' {
Some(
self.inner
.entry(name)
.or_insert_with(|| Register::new(name).unwrap()),
)
} else {
None
}
}

pub fn write(&mut self, name: char, values: Vec<String>) {
self.inner
.insert(name, Register::new_with_values(name, values));
if name != '_' {
self.inner
.insert(name, Register::new_with_values(name, values).unwrap());
}
}

pub fn read(&self, name: char) -> Option<&[String]> {
Expand Down
7 changes: 4 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ fn search_selection(cx: &mut Context) {
let contents = doc.text().slice(..);
let query = doc.selection(view.id).primary().fragment(contents);
let regex = regex::escape(&query);
cx.editor.registers.get_mut('/').push(regex);
cx.editor.registers.get_mut('/').unwrap().push(regex);
let msg = format!("register '{}' set to '{}'", '\\', query);
cx.editor.set_status(msg);
}
Expand Down Expand Up @@ -1710,8 +1710,9 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
let registers = &mut cx.editor.registers;
let reg = registers.get_mut(reg_name);
reg.write(values);
if let Some(reg) = registers.get_mut(reg_name) {
reg.write(values);
}
};

// then delete
Expand Down
19 changes: 11 additions & 8 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,24 +481,27 @@ impl Component for Prompt {

if let Some(register) = self.history_register {
// store in history
let register = cx.editor.registers.get_mut(register);
register.push(self.line.clone());
if let Some(register) = cx.editor.registers.get_mut(register) {
register.push(self.line.clone());
}
}
return close_fn;
}
}
ctrl!('p') | key!(Up) => {
if let Some(register) = self.history_register {
let register = cx.editor.registers.get_mut(register);
self.change_history(register.read(), CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
if let Some(register) = cx.editor.registers.get_mut(register) {
self.change_history(register.read(), CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
}
}
}
ctrl!('n') | key!(Down) => {
if let Some(register) = self.history_register {
let register = cx.editor.registers.get_mut(register);
self.change_history(register.read(), CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
if let Some(register) = cx.editor.registers.get_mut(register) {
self.change_history(register.read(), CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
}
}
}
key!(Tab) => {
Expand Down

0 comments on commit 5972f66

Please sign in to comment.