#![deny(unsafe_code)]
use super::keys::{self, Action};
use super::search::Dir;
use super::{Mode, Pager, HSTEP};
use crate::key::{Key, KeyEvent};
use crate::source::Anchor;
use crate::select::Yank;
impl Pager {
pub fn handle(&mut self, ev: KeyEvent) {
self.dirty = true;
if self.stop_seek() {
self.notify("scan stopped");
}
if keys::lookup(None, ev) == Some(Action::ForceQuit) {
return self.act(Action::ForceQuit);
}
if let Mode::Search(dir) = self.mode {
self.search_key(ev, dir);
return;
}
if self.mode == Mode::Index {
self.index_key(ev);
return;
}
if self.mode != Mode::Normal && self.overlay_key(ev) {
return;
}
let prefix = self.pending.take();
if prefix.is_none() && keys::is_prefix(ev) {
if let Key::Char(c) = ev.key {
self.pending = Some(c);
}
return;
}
match keys::lookup(prefix, ev) {
Some(a) => self.act(a),
None => {
if ev.key == Key::Esc {
match self.select.is_some() {
true => self.cancel_visual(),
false => self.clear_search(),
}
}
}
}
}
pub(super) fn act(&mut self, a: Action) {
match a {
Action::Visual => return self.toggle_visual(),
Action::Yank => return self.yank_selection(),
Action::YankSection => return self.yank_section(),
Action::YankCode => return self.yank_code(),
_ => {}
}
self.motion(a);
if let Some(s) = &mut self.select {
s.set_head(self.cursor);
}
}
fn motion(&mut self, a: Action) {
let h = self.content_rows().max(1);
match a {
Action::LineDown => self.move_cursor(1),
Action::LineUp => self.move_cursor(-1),
Action::HalfDown => self.move_cursor((h as isize + 1) / 2),
Action::HalfUp => self.move_cursor(-((h as isize + 1) / 2)),
Action::PageDown => self.move_cursor(h as isize),
Action::PageUp => self.move_cursor(-(h as isize)),
Action::Top => self.goto(0),
Action::Bottom => self.goto_end(),
Action::ScrollLeft => self.scroll_h(-(HSTEP as isize)),
Action::ScrollRight => self.scroll_h(HSTEP as isize),
Action::ArrowLeft => self.arrow(false),
Action::ArrowRight => self.arrow(true),
Action::Widen => self.widen(),
Action::ToggleHidden => match self.src.toggle_hidden() {
Some(msg) => {
self.notify(msg);
self.clamp();
}
None => self.notify("nothing hidden here"),
},
other => self.nav_action(other),
}
}
fn nav_action(&mut self, a: Action) {
match a {
Action::Quit => match self.can_pop() {
true => self.go_back(),
false => self.quit = true,
},
Action::ForceQuit => self.quit = true,
Action::Follow => self.follow(),
Action::Back => self.go_back(),
Action::Forward => self.go_forward(),
Action::OpenIndex => self.open_index(),
Action::NextDoc => self.step_doc(1),
Action::PrevDoc => self.step_doc(-1),
other => self.view_action(other),
}
}
fn view_action(&mut self, a: Action) {
match a {
Action::ToggleCollapse => self.fold(None),
Action::OpenTree => self.open_tree(),
Action::OpenSection => self.fold(Some(false)),
Action::CloseSection => self.fold(Some(true)),
Action::CollapseAll => self.fold_all(true),
Action::ExpandAll => self.fold_all(false),
Action::NextHeading => self.jump_heading(true),
Action::PrevHeading => self.jump_heading(false),
Action::Outline => self.open_outline(),
Action::Help => {
self.mode = Mode::Help;
self.help_top = 0;
}
Action::SearchForward => self.start_search(Dir::Forward),
Action::SearchBackward => self.start_search(Dir::Backward),
Action::NextMatch => match self.query.is_empty() {
true => self.step_link(true),
false => self.cycle(self.dir),
},
Action::PrevMatch => match self.query.is_empty() {
true => self.step_link(false),
false => self.cycle(self.dir.flip()),
},
_ => {}
}
}
pub(super) fn fold(&mut self, want: Option<bool>) {
if self.len() == 0 {
return;
}
if want.is_none() {
if let Some(closed) = self.src.fold_here(self.cursor) {
self.folds_changed();
let _ = closed;
return;
}
}
let entry = match self.src.section_at(self.cursor) {
Some(e) => e,
None => return self.notify("no heading here"),
};
let (anchor, is_closed) = match self.src.outline().get(entry) {
Some(e) => (e.anchor, e.folded),
None => return,
};
let close = want.unwrap_or(!is_closed);
if !self.src.set_fold(entry, close) {
return;
}
self.folds_changed();
self.jump_to(anchor);
}
fn fold_all(&mut self, close: bool) {
let here = self.cursor_anchor();
let section = self
.src
.section_at(self.cursor)
.and_then(|e| self.src.outline().get(e))
.map(|e| e.anchor);
self.src.fold_all(close);
self.folds_changed();
if let Some(a) = here {
let target = match close {
true => section.unwrap_or(a),
false => a,
};
self.jump_to(target);
}
self.notify(match close {
true => "all sections collapsed",
false => "all sections expanded",
});
}
fn open_outline(&mut self) {
if self.src.outline().is_empty() {
return self.notify("no headings");
}
self.mode = Mode::Outline;
let here = self.cursor_anchor().unwrap_or(Anchor(0));
self.outline_sel = self
.src
.outline()
.iter()
.rposition(|e| e.anchor <= here)
.unwrap_or(0);
}
fn overlay_key(&mut self, ev: KeyEvent) -> bool {
match (self.mode, ev.key) {
(_, Key::Esc) | (_, Key::Char('q')) => {
self.mode = Mode::Normal;
true
}
(Mode::Help, Key::Char('j')) | (Mode::Help, Key::Down) => {
self.help_top = (self.help_top + 1).min(keys::BINDINGS.len().saturating_sub(1));
true
}
(Mode::Help, Key::Char('k')) | (Mode::Help, Key::Up) => {
self.help_top = self.help_top.saturating_sub(1);
true
}
(Mode::Help, _) => {
self.mode = Mode::Normal;
true
}
(Mode::Outline, _) => self.outline_key(ev),
(Mode::Detail, _) => self.detail_key(ev),
_ => false,
}
}
fn detail_key(&mut self, ev: KeyEvent) -> bool {
let page = self.body_rows().max(1);
let last = match &self.detail {
Some(d) => d.fields.len().saturating_sub(1),
None => return false,
};
match ev.key {
Key::Char('j') | Key::Down => self.detail_sel = (self.detail_sel + 1).min(last),
Key::Char('k') | Key::Up => self.detail_sel = self.detail_sel.saturating_sub(1),
Key::Char('g') => self.detail_sel = 0,
Key::Char('G') => self.detail_sel = last,
Key::PageDown => self.detail_sel = (self.detail_sel + page).min(last),
Key::PageUp => self.detail_sel = self.detail_sel.saturating_sub(page),
Key::Char('y') => self.yank_field(),
_ => return false,
}
true
}
fn yank_field(&mut self) {
let Some(d) = &self.detail else { return };
let Some((label, value)) = d.fields.get(self.detail_sel) else {
return;
};
if value.is_empty() {
let empty = format!("{label} is empty");
return self.notify(empty);
}
let yank = Yank {
text: format!("{value}\n"),
what: format!("{label} \u{b7} {}", d.title.to_lowercase()),
};
self.queue_yank(yank);
}
fn outline_key(&mut self, ev: KeyEvent) -> bool {
let page = self.body_rows().max(1);
let last = self.src.outline().len().saturating_sub(1);
match ev.key {
Key::Char('j') | Key::Down => self.outline_sel = (self.outline_sel + 1).min(last),
Key::Char('k') | Key::Up => self.outline_sel = self.outline_sel.saturating_sub(1),
Key::Char('g') => self.outline_sel = 0,
Key::Char('G') => self.outline_sel = last,
Key::PageDown => self.outline_sel = (self.outline_sel + page).min(last),
Key::PageUp => self.outline_sel = self.outline_sel.saturating_sub(page),
Key::Enter => self.follow_outline(),
_ => return false,
}
true
}
fn follow_outline(&mut self) {
self.mode = Mode::Normal;
if let Some(target) = self.src.outline().get(self.outline_sel).map(|e| e.anchor) {
self.jump_to(target);
}
}
fn start_search(&mut self, dir: Dir) {
self.mode = Mode::Search(dir);
self.dir = dir;
self.query.clear();
self.src.set_query("");
self.search_origin = self.cursor_anchor();
}
fn search_key(&mut self, ev: KeyEvent, dir: Dir) {
match ev.key {
Key::Esc => {
self.mode = Mode::Normal;
self.clear_search();
self.jump_to_origin();
}
Key::Enter => {
self.mode = Mode::Normal;
if self.src.match_count() == 0 && !self.query.is_empty() {
self.notify(format!("pattern not found: {}", self.query));
}
}
Key::Backspace => {
if self.query.pop().is_none() {
self.mode = Mode::Normal;
}
self.rescan();
self.preview(dir);
}
Key::Char(c) => {
self.query.push(c);
self.rescan();
self.preview(dir);
}
_ => {}
}
}
pub(super) fn rescan(&mut self) {
self.src.set_query(&self.query);
}
fn jump_to_origin(&mut self) {
if let Some(a) = self.search_origin {
self.jump_to(a);
}
}
fn preview(&mut self, dir: Dir) {
if self.query.is_empty() {
self.jump_to_origin();
return;
}
let origin = match self.search_origin {
Some(a) => a,
None => return,
};
if let Some(hit) = self.src.preview_match(origin, dir) {
self.jump_to(hit.anchor);
}
}
fn cycle(&mut self, dir: Dir) {
if self.query.is_empty() {
return self.notify("no previous search");
}
if self.src.match_count() == 0 {
return self.notify(format!("pattern not found: {}", self.query));
}
let from = self.cursor_anchor().unwrap_or(Anchor(0));
if let Some(hit) = self.src.cycle_match(from, dir) {
self.jump_to(hit.anchor);
if hit.wrapped {
self.notify(match dir {
Dir::Forward => "search hit bottom, continuing at top",
Dir::Backward => "search hit top, continuing at bottom",
});
}
}
}
fn clear_search(&mut self) {
self.query.clear();
self.src.set_query("");
}
}