@@ -133,22 +133,60 @@ pub fn move_file_end(view: &mut View, _count: usize) {
133133 view.state.mode = Mode::Normal;
134134}
135135
136+pub fn check_cursor_in_view(view: &mut View) -> bool {
137+ let cursor = view.state.selection().cursor();
138+ 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;
140+ let padding = 5u16;
141+
142+ if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
143+ return false;
144+ }
145+ true
146+}
147+
136148pub fn page_up(view: &mut View, _count: usize) {
149+ let text = &view.state.doc;
137150 view.first_line = view.first_line.saturating_sub(view.size.1);
138− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
151+
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+ );
139156}
140157
141158pub fn page_down(view: &mut View, _count: usize) {
159+ let text = &view.state.doc;
142160 view.first_line += view.size.1;
143− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
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+ );
144166}
145167
146168pub fn half_page_up(view: &mut View, _count: usize) {
147169 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
170+
171+ if !check_cursor_in_view(view) {
172+ 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+ );
177+ }
148178}
149179
150180pub fn half_page_down(view: &mut View, _count: usize) {
151181 view.first_line += view.size.1 / 2;
182+
183+ if !check_cursor_in_view(view) {
184+ 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+ );
189+ }
152190}
153191// avoid select by default by having a visual mode switch that makes movements into selects
154192