on master help sign in
fixed page up/down
88f9339Jan Hrastnik
helix-view/src/commands.rs+40−2
@@ -133,22 +133,60 @@ pub fn move_file_end(view: &mut View, _count: usize) {
133 view.state.mode = Mode::Normal;
133 view.state.mode = Mode::Normal;
134}
134}
135
135
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+
136pub fn page_up(view: &mut View, _count: usize) {
148pub fn page_up(view: &mut View, _count: usize) {
149+ let text = &view.state.doc;
137 view.first_line = view.first_line.saturating_sub(view.size.1);
150 view.first_line = view.first_line.saturating_sub(view.size.1);
151+
138− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
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+ );
139}
156}
140
157
141pub fn page_down(view: &mut View, _count: usize) {
158pub fn page_down(view: &mut View, _count: usize) {
159+ let text = &view.state.doc;
142 view.first_line += view.size.1;
160 view.first_line += view.size.1;
161+
143− view.state.selection = Selection::single(view.first_line as usize, view.first_line as usize);
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+ );
144}
166}
145
167
146pub fn half_page_up(view: &mut View, _count: usize) {
168pub fn half_page_up(view: &mut View, _count: usize) {
147 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
169 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+ }
148}
178}
149
179
150pub fn half_page_down(view: &mut View, _count: usize) {
180pub fn half_page_down(view: &mut View, _count: usize) {
151 view.first_line += view.size.1 / 2;
181 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+ }
152}
190}
153// avoid select by default by having a visual mode switch that makes movements into selects
191// avoid select by default by having a visual mode switch that makes movements into selects
154
192
helix-view/src/keymap.rs+11−2
@@ -108,6 +108,15 @@ macro_rules! shift {
108 };
108 };
109}
109}
110
110
111+macro_rules! ctrl {
112+ ($ch:expr) => {
113+ Key {
114+ code: KeyCode::Char($ch),
115+ modifiers: Modifiers::CONTROL,
116+ }
117+ };
118+}
119+
111pub fn default() -> Keymaps {
120pub fn default() -> Keymaps {
112 hashmap!(
121 hashmap!(
113 state::Mode::Normal =>
122 state::Mode::Normal =>
@@ -148,8 +157,8 @@ pub fn default() -> Keymaps {
148 code: KeyCode::PageDown,
157 code: KeyCode::PageDown,
149 modifiers: Modifiers::NONE
158 modifiers: Modifiers::NONE
150 }] => commands::page_down,
159 }] => commands::page_down,
151− vec![shift!('u')] => commands::half_page_up,
160+ vec![ctrl!('u')] => commands::half_page_up,
152− vec![shift!('d')] => commands::half_page_down,
161+ vec![ctrl!('d')] => commands::half_page_down,
153 ),
162 ),
154 state::Mode::Insert => hashmap!(
163 state::Mode::Insert => hashmap!(
155 vec![Key {
164 vec![Key {