@@ -13,6 +13,8 @@ use crate::view::View;
13/// state (usually by creating and applying a transaction).
13/// state (usually by creating and applying a transaction).
14pub type Command = fn(view: &mut View, count: usize);
14pub type Command = fn(view: &mut View, count: usize);
16+const PADDING: u16 = 10;
16pub fn move_char_left(view: &mut View, count: usize) {
18pub fn move_char_left(view: &mut View, count: usize) {
17 // TODO: use a transaction
19 // TODO: use a transaction
18 let selection = view
20 let selection = view
@@ -127,7 +129,7 @@ pub fn move_file_start(view: &mut View, _count: usize) {
127pub fn move_file_end(view: &mut View, _count: usize) {
129pub fn move_file_end(view: &mut View, _count: usize) {
128 // TODO: use a transaction
130 // TODO: use a transaction
129 let text = &view.state.doc;
131 let text = &view.state.doc;
130− let last_line = text.line_to_char(text.len_lines().checked_sub(2).unwrap());
132+ let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
131 view.state.selection = Selection::single(last_line, last_line);
133 view.state.selection = Selection::single(last_line, last_line);
133 view.state.mode = Mode::Normal;
135 view.state.mode = Mode::Normal;
@@ -136,7 +138,7 @@ pub fn move_file_end(view: &mut View, _count: usize) {
136pub fn check_cursor_in_view(view: &mut View) -> bool {
138pub fn check_cursor_in_view(view: &mut View) -> bool {
137 let cursor = view.state.selection().cursor();
139 let cursor = view.state.selection().cursor();
138 let line = view.state.doc().char_to_line(cursor) as u16;
140 let line = view.state.doc().char_to_line(cursor) as u16;
139− let document_end = view.first_line + view.size.1.saturating_sub(1) - 1;
141+ let document_end = view.first_line + view.size.1.saturating_sub(2);
140 let padding = 5u16;
142 let padding = 5u16;
142 if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
144 if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
@@ -148,44 +150,37 @@ pub fn check_cursor_in_view(view: &mut View) -> bool {
148pub fn page_up(view: &mut View, _count: usize) {
150pub fn page_up(view: &mut View, _count: usize) {
149 let text = &view.state.doc;
151 let text = &view.state.doc;
150 view.first_line = view.first_line.saturating_sub(view.size.1);
152 view.first_line = view.first_line.saturating_sub(view.size.1);
153+ let pos = text.line_to_char(view.first_line as usize);
152− view.state.selection = Selection::single(
155+ view.state.selection = Selection::single(pos, pos);
153− text.line_to_char(view.first_line as usize),
154− text.line_to_char(view.first_line as usize),
158pub fn page_down(view: &mut View, _count: usize) {
158pub fn page_down(view: &mut View, _count: usize) {
159 let text = &view.state.doc;
159 let text = &view.state.doc;
160− view.first_line += view.size.1;
160+ view.first_line += view.size.1 + PADDING;
161−
161+ if view.first_line < view.state.doc().len_lines() as u16 {
162+ let pos = text.line_to_char(view.first_line as usize);
162− view.state.selection = Selection::single(
163+ view.state.selection = Selection::single(pos, pos);
163− text.line_to_char(view.first_line as usize),
164+ }
164− text.line_to_char(view.first_line as usize),
168pub fn half_page_up(view: &mut View, _count: usize) {
167pub fn half_page_up(view: &mut View, _count: usize) {
169 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
168 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
171 if !check_cursor_in_view(view) {
169 if !check_cursor_in_view(view) {
172 let text = &view.state.doc;
170 let text = &view.state.doc;
171+ let pos = text.line_to_char(view.first_line as usize);
173− view.state.selection = Selection::single(
172+ view.state.selection = Selection::single(pos, pos);
174− text.line_to_char(view.first_line as usize),
175− text.line_to_char(view.first_line as usize),
180pub fn half_page_down(view: &mut View, _count: usize) {
176pub fn half_page_down(view: &mut View, _count: usize) {
177+ if view.first_line < view.state.doc().len_lines() as u16 - view.size.1 {
181− view.first_line += view.size.1 / 2;
178+ view.first_line += view.size.1 / 2;
183 if !check_cursor_in_view(view) {
180 if !check_cursor_in_view(view) {
184 let text = &view.state.doc;
181 let text = &view.state.doc;
182+ let pos = text.line_to_char(view.first_line as usize);
185− view.state.selection = Selection::single(
183+ view.state.selection = Selection::single(pos, pos);
186− text.line_to_char(view.first_line as usize),
187− text.line_to_char(view.first_line as usize),
191// avoid select by default by having a visual mode switch that makes movements into selects
186// avoid select by default by having a visual mode switch that makes movements into selects