on master help sign in
Add infobox for view
commit3e4cd8f8e6456fdea4f0e82908d395eb516e1be6 authorIvan Tham <pickfire@riseup.net> dateThu 8 Jul 2021, 09:58 (5 years ago) parentbb121a3
1 file changed+49−40
helix-term/src/commands.rs+49−40
helix-term/src/commands.rs+49−40
@@ -40,6 +40,7 @@ use crate::{
4040use crate::job::{self, Job, Jobs};
4141use futures_util::{FutureExt, TryFutureExt};
4242use std::collections::HashMap;
43+use std::num::NonZeroUsize;
4344use std::{fmt, future::Future};
4445
4546use std::{
@@ -52,7 +53,7 @@ use serde::de::{self, Deserialize, Deserializer};
5253
5354pub struct Context<'a> {
5455 pub selected_register: helix_view::RegisterSelection,
55− pub count: Option<std::num::NonZeroUsize>,
56+ pub count: Option<NonZeroUsize>,
5657 pub editor: &'a mut Editor,
5758
5859 pub callback: Option<crate::compositor::Callback>,
@@ -3384,47 +3385,38 @@ fn select_register(cx: &mut Context) {
33843385 })
33853386}
33863387
3387−fn view_mode(cx: &mut Context) {
3388− cx.on_next_key(move |cx, event| {
3389− if let KeyEvent {
3390− code: KeyCode::Char(ch),
3391− ..
3392− } = event
3393− {
3394− // if lock, call cx again
3395− // TODO: temporarily show VIE in the mode list
3396− match ch {
3397− // center
3398− 'z' | 'c'
3399− // top
3400− | 't'
3401− // bottom
3402− | 'b' => {
3403− let (view, doc) = current!(cx.editor);
3388+fn align_view_top(cx: &mut Context) {
3389+ let (view, doc) = current!(cx.editor);
3390+ align_view(doc, view, Align::Top);
3391+}
34043392
3405− align_view(doc, view, match ch {
3406− 'z' | 'c' => Align::Center,
3407− 't' => Align::Top,
3408− 'b' => Align::Bottom,
3409− _ => unreachable!()
3410− });
3411− }
3412− 'm' => {
3413− let (view, doc) = current!(cx.editor);
3414− let pos = doc.selection(view.id).cursor();
3415− let pos = coords_at_pos(doc.text().slice(..), pos);
3393+fn align_view_center(cx: &mut Context) {
3394+ let (view, doc) = current!(cx.editor);
3395+ align_view(doc, view, Align::Center);
3396+}
34163397
3417− const OFFSET: usize = 7; // gutters
3418− view.first_col = pos.col.saturating_sub(((view.area.width as usize).saturating_sub(OFFSET)) / 2);
3419− },
3420− 'h' => (),
3421− 'j' => scroll(cx, 1, Direction::Forward),
3422− 'k' => scroll(cx, 1, Direction::Backward),
3423− 'l' => (),
3424− _ => (),
3425− }
3426− }
3427− })
3398+fn align_view_bottom(cx: &mut Context) {
3399+ let (view, doc) = current!(cx.editor);
3400+ align_view(doc, view, Align::Bottom);
3401+}
3402+
3403+fn align_view_middle(cx: &mut Context) {
3404+ let (view, doc) = current!(cx.editor);
3405+ let pos = doc.selection(view.id).cursor();
3406+ let pos = coords_at_pos(doc.text().slice(..), pos);
3407+
3408+ const OFFSET: usize = 7; // gutters
3409+ view.first_col = pos
3410+ .col
3411+ .saturating_sub(((view.area.width as usize).saturating_sub(OFFSET)) / 2);
3412+}
3413+
3414+fn scroll_up(cx: &mut Context) {
3415+ scroll(cx, cx.count(), Direction::Backward);
3416+}
3417+
3418+fn scroll_down(cx: &mut Context) {
3419+ scroll(cx, cx.count(), Direction::Forward);
34283420}
34293421
34303422fn select_textobject_around(cx: &mut Context) {
@@ -3732,3 +3724,20 @@ mode_info! {
37323724 /// diagnostic (last)
37333725 "D" => goto_last_diag,
37343726}
3727+
3728+mode_info! {
3729+ /// view
3730+ view_mode, VIEW_MODE,
3731+ /// align view top
3732+ "t" => align_view_top,
3733+ /// align view center
3734+ "z" | "c" => align_view_center,
3735+ /// align view bottom
3736+ "b" => align_view_bottom,
3737+ /// align view middle
3738+ "m" => align_view_middle,
3739+ /// scroll up
3740+ "k" => scroll_up,
3741+ /// scroll down
3742+ "j" => scroll_down,
3743+}