From 01fe12b2ce9f75790538624aff7acfd8f6bef287 Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Sun, 8 May 2022 12:58:56 +0200 Subject: [PATCH] Add VIMs hi/mid/lo-screen commands (#824) * add hi/mid/lo-screen commands and default keybindings * doc --- doc.go | 9 +++++++++ docstring.go | 9 +++++++++ eval.go | 24 ++++++++++++++++++++++++ lf.1 | 11 +++++++++++ nav.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ opts.go | 3 +++ 6 files changed, 103 insertions(+) diff --git a/doc.go b/doc.go index f0e044f..3d3f758 100644 --- a/doc.go +++ b/doc.go @@ -31,6 +31,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') @@ -295,6 +298,12 @@ Change the current working directory to the next/previous jumplist item. Move the current file selection to the top/bottom of the directory. + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') + +Move the current file selection to the top/middle/bottom of the screen. + toggle Toggle the selection of the current file or files given as arguments. diff --git a/docstring.go b/docstring.go index a8829ac..2627f0a 100644 --- a/docstring.go +++ b/docstring.go @@ -35,6 +35,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') @@ -307,6 +310,12 @@ Change the current working directory to the next/previous jumplist item. Move the current file selection to the top/bottom of the directory. + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') + +Move the current file selection to the top/middle/bottom of the screen. + toggle Toggle the selection of the current file or files given as arguments. diff --git a/eval.go b/eval.go index 54f2dcb..00ddb67 100644 --- a/eval.go +++ b/eval.go @@ -963,6 +963,30 @@ func (e *callExpr) eval(app *app, args []string) { app.ui.loadFile(app.nav, true) app.ui.loadFileInfo(app.nav) } + case "hi-screen": + if !app.nav.init { + return + } + if app.nav.hiScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } + case "mid-screen": + if !app.nav.init { + return + } + if app.nav.midScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } + case "lo-screen": + if !app.nav.init { + return + } + if app.nav.loScreen() { + app.ui.loadFile(app.nav, true) + app.ui.loadFileInfo(app.nav) + } case "toggle": if !app.nav.init { return diff --git a/lf.1 b/lf.1 index 16ae2d6..487b8fd 100644 --- a/lf.1 +++ b/lf.1 @@ -46,6 +46,9 @@ The following commands are provided by lf: jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') toggle invert (default 'v') unselect (default 'u') @@ -343,6 +346,14 @@ Change the current working directory to the next/previous jumplist item. .PP Move the current file selection to the top/bottom of the directory. .PP +.EX + hi-screen (default 'H') + mid-screen (default 'M') + lo-screen (default 'L') +.EE +.PP +Move the current file selection to the top/middle/bottom of the screen. +.PP .EX toggle .EE diff --git a/nav.go b/nav.go index 52a1e40..bb539d2 100644 --- a/nav.go +++ b/nav.go @@ -943,6 +943,53 @@ func (nav *nav) bottom() bool { return old != dir.ind } +func (nav *nav) hiScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + offs := gOpts.scrolloff + if beg == 0 { + offs = 0 + } + + dir.ind = beg + offs + dir.pos = offs + + return old != dir.ind +} + +func (nav *nav) midScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + end := min(beg+nav.height, len(dir.files)) + + half := (end - beg) / 2 + dir.ind = beg + half + dir.pos = half + + return old != dir.ind +} + +func (nav *nav) loScreen() bool { + dir := nav.currDir() + + old := dir.ind + beg := max(dir.ind-dir.pos, 0) + end := min(beg+nav.height, len(dir.files)) + offs := gOpts.scrolloff + if end == len(dir.files) { + offs = 0 + } + + dir.ind = end - 1 - offs + dir.pos = end - beg - 1 - offs + + return old != dir.ind +} + func (nav *nav) toggleSelection(path string) { if _, ok := nav.selections[path]; ok { delete(nav.selections, path) diff --git a/opts.go b/opts.go index dae3983..261a235 100644 --- a/opts.go +++ b/opts.go @@ -146,6 +146,9 @@ func init() { gOpts.keys[""] = &callExpr{"top", nil, 1} gOpts.keys["G"] = &callExpr{"bottom", nil, 1} gOpts.keys[""] = &callExpr{"bottom", nil, 1} + gOpts.keys["H"] = &callExpr{"hi-screen", nil, 1} + gOpts.keys["M"] = &callExpr{"mid-screen", nil, 1} + gOpts.keys["L"] = &callExpr{"lo-screen", nil, 1} gOpts.keys["["] = &callExpr{"jump-prev", nil, 1} gOpts.keys["]"] = &callExpr{"jump-next", nil, 1} gOpts.keys[""] = &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}