1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Visual mode and the yank commands (`v`, `y`, `Y`, `c`).
//!
//! The pager only *builds* the text: `main` performs the clipboard write and
//! the cache-file fallback, then reports the outcome through
//! [`Pager::notify`]. Nothing here touches a file descriptor.
#![deny(unsafe_code)]
use super::Pager;
use crate::select::{self, Selection};
impl Pager {
/// `v`: start a selection anchored at the cursor, or drop the current one.
pub(super) fn toggle_visual(&mut self) {
match self.select {
Some(_) => self.cancel_visual(),
None if self.src.is_empty() => self.notify("nothing to select"),
None => self.select = Some(Selection::new(self.cursor)),
}
}
/// `Esc`: leave visual mode without yanking.
pub(super) fn cancel_visual(&mut self) {
self.select = None;
self.notify("selection cancelled");
}
/// `y`: yank the visual selection, or — outside visual mode, with a link
/// focused — that link's target.
///
/// SPEC.md §Navigation requires external links to be yankable, and `n`
/// focusing a link is the only way to name one, so `y` means "copy the
/// focused link" whenever there is nothing selected.
pub(super) fn yank_selection(&mut self) {
if self.select.is_none() {
return self.yank_link();
}
let rows = self.selected_rows();
self.select = None;
match self.src.yank_rows(rows) {
Some(y) => self.queue_yank(y),
None => self.notify("nothing to yank"),
}
}
/// `y` with no selection: the smallest thing the format offers at the
/// cursor — a CSV's cell — and otherwise the focused link.
fn yank_link(&mut self) {
if let Some(y) = self.cursor_row().and_then(|r| self.src.yank_point(r)) {
return self.queue_yank(y);
}
self.yank_focused_link()
}
/// External URLs are copied verbatim (they are never opened); internal ones
/// as the path relative to the index root.
fn yank_focused_link(&mut self) {
let target = match self.focused_link_yank() {
Some(t) => t,
None => {
return self
.notify("nothing selected \u{2014} press v to select, or n to focus a link")
}
};
match select::link_yank(&target) {
Some(y) => self.queue_yank(y),
None => self.notify("that link has no target to copy"),
}
}
/// `Y`: yank the whole section under the cursor, heading included.
pub(super) fn yank_section(&mut self) {
let row = match self.cursor_row() {
Some(r) => r,
None => return self.notify("nothing to yank"),
};
self.select = None;
match self.src.yank_section(row) {
Some(y) => self.queue_yank(y),
None => self.notify("no section here"),
}
}
/// `c`: yank the code block under (or nearest below) the cursor, verbatim.
pub(super) fn yank_code(&mut self) {
let row = self.cursor;
self.select = None;
match self.src.yank_block(row) {
Some(y) => self.queue_yank(y),
None => self.notify("no code block below the cursor"),
}
}
/// Test seam: the yank a command produced, without a terminal.
#[cfg(test)]
pub(crate) fn peek_yank(&mut self) -> Option<crate::select::Yank> {
self.take_yank()
}
}