@@ -13,6 +13,8 @@ use crate::view::View;
1313/// state (usually by creating and applying a transaction).
1414pub type Command = fn(view: &mut View, count: usize);
1515
16+const PADDING: u16 = 10;
17+
1618pub fn move_char_left(view: &mut View, count: usize) {
1719 // TODO: use a transaction
1820 let selection = view
@@ -127,7 +129,7 @@ pub fn move_file_start(view: &mut View, _count: usize) {
127129pub fn move_file_end(view: &mut View, _count: usize) {
128130 // TODO: use a transaction
129131 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));
131133 view.state.selection = Selection::single(last_line, last_line);
132134
133135 view.state.mode = Mode::Normal;
@@ -136,7 +138,7 @@ pub fn move_file_end(view: &mut View, _count: usize) {
136138pub fn check_cursor_in_view(view: &mut View) -> bool {
137139 let cursor = view.state.selection().cursor();
138140 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);
140142 let padding = 5u16;
141143
142144 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 {
148150pub fn page_up(view: &mut View, _count: usize) {
149151 let text = &view.state.doc;
150152 view.first_line = view.first_line.saturating_sub(view.size.1);
153+ let pos = text.line_to_char(view.first_line as usize);
151154
152− view.state.selection = Selection::single(
153− text.line_to_char(view.first_line as usize),
154− text.line_to_char(view.first_line as usize),
155− );
155+ view.state.selection = Selection::single(pos, pos);
156156}
157157
158158pub fn page_down(view: &mut View, _count: usize) {
159159 let text = &view.state.doc;
160− view.first_line += view.size.1;
161−
162− view.state.selection = Selection::single(
163− text.line_to_char(view.first_line as usize),
164− text.line_to_char(view.first_line as usize),
165− );
160+ view.first_line += view.size.1 + PADDING;
161+ if view.first_line < view.state.doc().len_lines() as u16 {
162+ let pos = text.line_to_char(view.first_line as usize);
163+ view.state.selection = Selection::single(pos, pos);
164+ }
166165}
167166
168167pub fn half_page_up(view: &mut View, _count: usize) {
169168 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
170−
171169 if !check_cursor_in_view(view) {
172170 let text = &view.state.doc;
173− view.state.selection = Selection::single(
174− text.line_to_char(view.first_line as usize),
175− text.line_to_char(view.first_line as usize),
176− );
171+ let pos = text.line_to_char(view.first_line as usize);
172+ view.state.selection = Selection::single(pos, pos);
177173 }
178174}
179175
180176pub fn half_page_down(view: &mut View, _count: usize) {
181− view.first_line += view.size.1 / 2;
182−
177+ if view.first_line < view.state.doc().len_lines() as u16 - view.size.1 {
178+ view.first_line += view.size.1 / 2;
179+ }
183180 if !check_cursor_in_view(view) {
184181 let text = &view.state.doc;
185− view.state.selection = Selection::single(
186− text.line_to_char(view.first_line as usize),
187− text.line_to_char(view.first_line as usize),
188− );
182+ let pos = text.line_to_char(view.first_line as usize);
183+ view.state.selection = Selection::single(pos, pos);
189184 }
190185}
191186// avoid select by default by having a visual mode switch that makes movements into selects