From 16db95707eda1735714505bbd9f7f0da5a06405e Mon Sep 17 00:00:00 2001 From: Juris Arturs Majors <32071671+JurisMajors@users.noreply.github.com> Date: Tue, 25 Jun 2019 20:38:34 +0200 Subject: [PATCH] Mark clear (#190) * markclear * Revert "markclear" This reverts commit 5e3a188618cb02f7aa284e9f2c9a00ed8ce3dc14. * Revert "Revert "markclear"" This reverts commit a7a3bfebbf99c10b4a09f197fa997eca461369c4. * markclear with arguments * docs * reverted some whitepsace * some inline comments * additional error handling * wrong argument case * consider case when argument specified clears all marks * code cleanup and removed readMarks from writeMarks * indentation :/ * discussed changes implemnted * return readMarks error in sync() * remove reset marks --- app.go | 4 ---- complete.go | 1 + doc.go | 1 + docstring.go | 1 + eval.go | 23 +++++++++++++++++++++++ lf.1 | 1 + nav.go | 20 +++++++++++--------- opts.go | 1 + 8 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app.go b/app.go index c3d1ded..2eb095a 100644 --- a/app.go +++ b/app.go @@ -181,10 +181,6 @@ func (app *app) loop() { log.Print("bye!") - if err := app.nav.writeMarks(); err != nil { - log.Printf("writing marks file: %s", err) - } - if err := app.writeHistory(); err != nil { log.Printf("writing history file: %s", err) } diff --git a/complete.go b/complete.go index 2a282b8..82d4f88 100644 --- a/complete.go +++ b/complete.go @@ -51,6 +51,7 @@ var ( "search-next", "search-prev", "mark-save", + "mark-remove", "mark-load", "sync", "echo", diff --git a/doc.go b/doc.go index 2a4730c..937053c 100644 --- a/doc.go +++ b/doc.go @@ -51,6 +51,7 @@ The following commands are provided by lf with default keybindings: search-prev (default 'N') mark-save (default 'm') mark-load (default "'") + mark-remove (default `"`) The following commands are provided by lf without default keybindings: diff --git a/docstring.go b/docstring.go index 522c813..31d1401 100644 --- a/docstring.go +++ b/docstring.go @@ -54,6 +54,7 @@ The following commands are provided by lf with default keybindings: search-prev (default 'N') mark-save (default 'm') mark-load (default "'") + mark-remove (default '"') The following commands are provided by lf without default keybindings: diff --git a/eval.go b/eval.go index bdf781a..7cdd5e2 100644 --- a/eval.go +++ b/eval.go @@ -470,6 +470,12 @@ func insert(app *app, arg string) { return } app.nav.marks[arg] = wd + if err := app.nav.writeMarks(); err != nil { + app.ui.echoerrf("mark-save: %s", err) + } + if err := remote("send sync"); err != nil { + app.ui.echoerrf("mark-save: %s", err) + } case app.ui.cmdPrefix == "mark-load: ": normal(app) @@ -493,6 +499,20 @@ func insert(app *app, arg string) { if wd != path { app.nav.marks["'"] = wd } + case app.ui.cmdPrefix == "mark-remove: ": + normal(app) + if err := app.nav.removeMark(arg); err != nil { + app.ui.echoerrf("mark-remove: %s", err) + return + } + if err := app.nav.writeMarks(); err != nil { + app.ui.echoerrf("mark-remove: %s", err) + return + } + if err := remote("send sync"); err != nil { + app.ui.echoerrf("mark-remove: %s", err) + } + default: app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...) } @@ -766,6 +786,9 @@ func (e *callExpr) eval(app *app, args []string) { case "mark-load": app.ui.menuBuf = listMarks(app.nav.marks) app.ui.cmdPrefix = "mark-load: " + case "mark-remove": + app.ui.menuBuf = listMarks(app.nav.marks) + app.ui.cmdPrefix = "mark-remove: " case "sync": if err := app.nav.sync(); err != nil { app.ui.echoerrf("sync: %s", err) diff --git a/lf.1 b/lf.1 index 4690fe6..81531fd 100644 --- a/lf.1 +++ b/lf.1 @@ -61,6 +61,7 @@ The following commands are provided by lf with default keybindings: search-prev (default 'N') mark-save (default 'm') mark-load (default "'") + mark-remove (default `"`) .EE .PP The following commands are provided by lf without default keybindings: diff --git a/nav.go b/nav.go index ed1e833..b4e07c8 100644 --- a/nav.go +++ b/nav.go @@ -747,7 +747,7 @@ func (nav *nav) sync() error { nav.saves[f] = cp } - return nil + return nav.readMarks() } func (nav *nav) cd(wd string) error { @@ -975,7 +975,17 @@ func (nav *nav) searchPrev() error { return nil } +func (nav *nav) removeMark(mark string) error { + if _, ok := nav.marks[mark]; ok { + delete(nav.marks, mark) + return nil + } else { + return fmt.Errorf("no such mark") + } +} + func (nav *nav) readMarks() error { + nav.marks = make(map[string]string) f, err := os.Open(gMarksPath) if os.IsNotExist(err) { return nil @@ -1001,14 +1011,6 @@ func (nav *nav) readMarks() error { } func (nav *nav) writeMarks() error { - if len(nav.marks) == 0 { - return nil - } - - if err := nav.readMarks(); err != nil { - return fmt.Errorf("reading marks file: %s", err) - } - if err := os.MkdirAll(filepath.Dir(gMarksPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %s", err) } diff --git a/opts.go b/opts.go index 2d8a7e2..8283ae1 100644 --- a/opts.go +++ b/opts.go @@ -137,6 +137,7 @@ func init() { gOpts.keys["N"] = &callExpr{"search-prev", nil, 1} gOpts.keys["m"] = &callExpr{"mark-save", nil, 1} gOpts.keys["'"] = &callExpr{"mark-load", nil, 1} + gOpts.keys[`"`] = &callExpr{"mark-remove", nil, 1} gOpts.keys[""] = &callExpr{"cmd-history-next", nil, 1} gOpts.keys[""] = &callExpr{"cmd-history-prev", nil, 1}