From 5b2d5ad99ddfbaf305704b4fe9585507eab769b9 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Sun, 20 Jul 2025 19:54:30 +1000 Subject: [PATCH] Support wrapping while in Visual mode (#2056) --- app.go | 1 + eval.go | 2 ++ nav.go | 27 +++++++++++++++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index 6d510c1b..fe8f7a9f 100644 --- a/app.go +++ b/app.go @@ -392,6 +392,7 @@ func (app *app) loop() { d.ind = prev.ind d.pos = prev.pos d.visualAnchor = min(prev.visualAnchor, len(d.files)-1) + d.visualWrap = prev.visualWrap d.filter = prev.filter d.sort() d.sel(prev.name(), app.nav.height) diff --git a/eval.go b/eval.go index d30d3a4f..cefbcbe1 100644 --- a/eval.go +++ b/eval.go @@ -837,6 +837,7 @@ func normal(app *app) { func visual(app *app) { dir := app.nav.currDir() dir.visualAnchor = dir.ind + dir.visualWrap = 0 app.ui.loadFileInfo(app.nav) } @@ -2378,6 +2379,7 @@ func (e *callExpr) eval(app *app, args []string) { beg := max(dir.ind-dir.pos, 0) dir.ind, dir.visualAnchor = dir.visualAnchor, dir.ind dir.pos = dir.ind - beg + dir.visualWrap = -dir.visualWrap dir.boundPos(app.nav.height) default: cmd, ok := gOpts.cmds[e.name] diff --git a/nav.go b/nav.go index 49c70cc2..7a15535c 100644 --- a/nav.go +++ b/nav.go @@ -182,7 +182,8 @@ type dir struct { dironly bool // dironly value from last sort hidden bool // hidden value from last sort reverse bool // reverse value from last sort - visualAnchor int // index where visual mode was initiated + visualAnchor int // index where Visual mode was initiated + visualWrap int // wrap direction in Visual mode hiddenfiles []string // hiddenfiles value from last sort filter []string // last filter for this directory ignorecase bool // ignorecase value from last sort @@ -459,13 +460,21 @@ func (dir *dir) visualSelections() []string { return paths } - beg, end := dir.ind, dir.visualAnchor - if beg > end { - beg, end = end, beg + var beg, end int + switch { + case dir.visualWrap == 0: + beg = min(dir.ind, dir.visualAnchor) + end = max(dir.ind, dir.visualAnchor) + case dir.visualWrap < 0: + beg = dir.ind + end = dir.visualAnchor - dir.visualWrap*len(dir.files) + case dir.visualWrap > 0: + beg = dir.visualAnchor + end = dir.ind + dir.visualWrap*len(dir.files) } - for _, f := range dir.files[beg : end+1] { - paths = append(paths, f.path) + for i := beg; i < min(end+1, beg+len(dir.files)); i++ { + paths = append(paths, dir.files[i%len(dir.files)].path) } return paths @@ -1017,6 +1026,7 @@ func (nav *nav) up(dist int) bool { if dir.ind == 0 { if gOpts.wrapscroll { nav.bottom() + dir.visualWrap -= 1 } return old != dir.ind } @@ -1040,6 +1050,7 @@ func (nav *nav) down(dist int) bool { if dir.ind >= maxind { if gOpts.wrapscroll { nav.top() + dir.visualWrap += 1 } return old != dir.ind } @@ -1751,6 +1762,7 @@ func (nav *nav) findNext() (bool, bool) { if gOpts.wrapscan { for i := range dir.ind { if findMatch(dir.files[i].Name(), nav.find) { + dir.visualWrap += 1 return nav.up(dir.ind - i), true } } @@ -1768,6 +1780,7 @@ func (nav *nav) findPrev() (bool, bool) { if gOpts.wrapscan { for i := len(dir.files) - 1; i > dir.ind; i-- { if findMatch(dir.files[i].Name(), nav.find) { + dir.visualWrap -= 1 return nav.down(i - dir.ind), true } } @@ -1810,6 +1823,7 @@ func (nav *nav) searchNext() (bool, error) { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { return false, err } else if matched { + dir.visualWrap += 1 return nav.up(dir.ind - i), nil } } @@ -1831,6 +1845,7 @@ func (nav *nav) searchPrev() (bool, error) { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { return false, err } else if matched { + dir.visualWrap -= 1 return nav.down(i - dir.ind), nil } }