preserve mark order in f[sx] variables (#59)

* preserve mark order in f[sx] variables

* Revert "preserve mark order in f[sx] variables"

This reverts commit e02c5e8bb3ec58f7cefafc92419f14c51e6730ef.

* preserve mark order in f[sx], but more efficiently
credit to @gokcehan for the idea
This commit is contained in:
Olivier Perret 2017-01-05 22:23:22 +01:00 committed by gokcehan
parent 79ca4cfe9d
commit a704248696
3 changed files with 51 additions and 27 deletions

View File

@ -292,7 +292,7 @@ func (e *callExpr) eval(app *app, args []string) {
log.Printf(msg) log.Printf(msg)
return return
} }
app.nav.marks = make(map[string]bool) app.nav.marks = make(map[string]int)
if err := sendRemote("send sync"); err != nil { if err := sendRemote("send sync"); err != nil {
msg := fmt.Sprintf("yank: %s", err) msg := fmt.Sprintf("yank: %s", err)
app.ui.message = msg app.ui.message = msg
@ -305,7 +305,7 @@ func (e *callExpr) eval(app *app, args []string) {
log.Printf(msg) log.Printf(msg)
return return
} }
app.nav.marks = make(map[string]bool) app.nav.marks = make(map[string]int)
if err := sendRemote("send sync"); err != nil { if err := sendRemote("send sync"); err != nil {
msg := fmt.Sprintf("delete: %s", err) msg := fmt.Sprintf("delete: %s", err)
app.ui.message = msg app.ui.message = msg

40
nav.go
View File

@ -183,8 +183,9 @@ type nav struct {
inds map[string]int inds map[string]int
poss map[string]int poss map[string]int
names map[string]string names map[string]string
marks map[string]bool marks map[string]int
saves map[string]bool saves map[string]bool
markInd int
height int height int
search string search string
} }
@ -225,8 +226,9 @@ func newNav(height int) *nav {
inds: make(map[string]int), inds: make(map[string]int),
poss: make(map[string]int), poss: make(map[string]int),
names: make(map[string]string), names: make(map[string]string),
marks: make(map[string]bool), marks: make(map[string]int),
saves: make(map[string]bool), saves: make(map[string]bool),
markInd: 0,
height: height, height: height,
} }
} }
@ -242,6 +244,9 @@ func (nav *nav) renew(height int) {
delete(nav.marks, m) delete(nav.marks, m)
} }
} }
if len(nav.marks) == 0 {
nav.markInd = 0
}
} }
func (nav *nav) up(dist int) { func (nav *nav) up(dist int) {
@ -380,10 +385,14 @@ func (nav *nav) searchPrev() {
} }
func (nav *nav) toggleMark(path string) { func (nav *nav) toggleMark(path string) {
if nav.marks[path] { if _, ok := nav.marks[path]; ok {
delete(nav.marks, path) delete(nav.marks, path)
if len(nav.marks) == 0 {
nav.markInd = 0
}
} else { } else {
nav.marks[path] = true nav.marks[path] = nav.markInd
nav.markInd = nav.markInd + 1
} }
} }
@ -502,10 +511,25 @@ func (nav *nav) currFile() (*file, error) {
return last.fi[last.ind], nil return last.fi[last.ind], nil
} }
type IndexedMarks struct {
paths []string
indices []int
}
func (m IndexedMarks) Len() int { return len(m.paths) }
func (m IndexedMarks) Swap(i, j int) {
m.paths[i], m.paths[j] = m.paths[j], m.paths[i]
m.indices[i], m.indices[j] = m.indices[j], m.indices[i]
}
func (m IndexedMarks) Less(i, j int) bool { return m.indices[i] < m.indices[j] }
func (nav *nav) currMarks() []string { func (nav *nav) currMarks() []string {
marks := make([]string, 0, len(nav.marks)) paths := make([]string, 0, len(nav.marks))
for m := range nav.marks { indices := make([]int, 0, len(nav.marks))
marks = append(marks, m) for path, index := range nav.marks {
paths = append(paths, path)
indices = append(indices, index)
} }
return marks sort.Sort(IndexedMarks{paths: paths, indices: indices})
return paths
} }

4
ui.go
View File

@ -209,7 +209,7 @@ func (win *win) printl(x, y int, fg, bg termbox.Attribute, s string) {
win.printf(x, y, fg, bg, "%s%*s", s, win.w-len(s), "") win.printf(x, y, fg, bg, "%s%*s", s, win.w-len(s), "")
} }
func (win *win) printd(dir *dir, marks, saves map[string]bool) { func (win *win) printd(dir *dir, marks map[string]int, saves map[string]bool) {
if win.w < 3 { if win.w < 3 {
return return
} }
@ -254,7 +254,7 @@ func (win *win) printd(dir *dir, marks, saves map[string]bool) {
path := filepath.Join(dir.path, f.Name()) path := filepath.Join(dir.path, f.Name())
if marks[path] { if _, ok := marks[path]; ok {
win.print(0, i, fg, termbox.ColorMagenta, " ") win.print(0, i, fg, termbox.ColorMagenta, " ")
} else if copy, ok := saves[path]; ok { } else if copy, ok := saves[path]; ok {
if copy { if copy {