Skip to content

Commit

Permalink
Add % current file register
Browse files Browse the repository at this point in the history
Useful to open files relative to current file.
  • Loading branch information
pickfire committed Jan 20, 2023
1 parent 7868e5f commit 8c3921e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions helix-core/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Registers {
}
}

pub fn clear(&mut self, name: char) {
self.inner.remove(&name);
}

pub fn push(&mut self, name: char, value: String) {
if name != '_' {
if let Some(r) = self.inner.get_mut(&name) {
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4418,6 +4418,7 @@ fn wonly(cx: &mut Context) {
}

fn select_register(cx: &mut Context) {
cx.editor.update_registers();
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
cx.on_next_key(move |cx, event| {
if let Some(ch) = event.char() {
Expand All @@ -4428,6 +4429,7 @@ fn select_register(cx: &mut Context) {
}

fn insert_register(cx: &mut Context) {
cx.editor.update_registers();
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
cx.on_next_key(move |cx, event| {
if let Some(ch) = event.char() {
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ impl Component for Prompt {
}
ctrl!('q') => self.exit_selection(),
ctrl!('r') => {
cx.editor.update_registers();
self.completion = cx
.editor
.registers
Expand Down
11 changes: 11 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,17 @@ impl Editor {
doc.restore_cursor = false;
}
}

/// Update read-only registers.
pub fn update_registers(&mut self) {
let doc = doc!(self);
if let Some(path) = doc.relative_path() {
self.registers
.write('%', vec![path.to_string_lossy().to_string()]);
} else {
self.registers.clear('%');
}
}
}

fn try_restore_indent(doc: &mut Document, view: &mut View) {
Expand Down

0 comments on commit 8c3921e

Please sign in to comment.