From a3965ca1d94bc257ff0af37ef073175d581fa64e Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sat, 15 Oct 2016 21:46:31 +0300 Subject: [PATCH] handle keys of push command while scanning --- doc.go | 2 +- docstring.go | 2 +- etc/lfrc.example | 2 +- eval.go | 2 +- eval_test.go | 30 ++++++++++++++++++++++++++++++ parse.go | 2 +- scan.go | 16 +++++++++++++++- 7 files changed, 50 insertions(+), 6 deletions(-) diff --git a/doc.go b/doc.go index c3dde83e..d3d91d90 100644 --- a/doc.go +++ b/doc.go @@ -122,7 +122,7 @@ with a command count. Second, it can be used to avoid typing the name when a command takes arguments. - map r :push :rename + map r push :rename One thing to be careful is that since "push" command works with keys instead of commands it is possible to accidentally create recursive bindings. diff --git a/docstring.go b/docstring.go index e53f16a4..fa7c5561 100644 --- a/docstring.go +++ b/docstring.go @@ -132,7 +132,7 @@ command with a command count. Second, it can be used to avoid typing the name when a command takes arguments. - map r :push :rename + map r push :rename One thing to be careful is that since "push" command works with keys instead of commands it is possible to accidentally create recursive bindings. diff --git a/etc/lfrc.example b/etc/lfrc.example index 85f37cf1..3effc4be 100644 --- a/etc/lfrc.example +++ b/etc/lfrc.example @@ -74,7 +74,7 @@ cmd open-file ${{ # rename current file without overwrite cmd rename $[ -e "$1" ] || mv "$f" "$1" -map r :push :rename +map r push :rename # show disk usage cmd usage $du -h . | less diff --git a/eval.go b/eval.go index 9d735fad..27a473b4 100644 --- a/eval.go +++ b/eval.go @@ -337,7 +337,7 @@ func (e *CallExpr) eval(app *App, args []string) { app.ui.loadFile(app.nav) case "push": if len(e.args) > 0 { - app.ui.keysbuf = append(app.ui.keysbuf, splitKeys(strings.Join(e.args, ""))...) + app.ui.keysbuf = append(app.ui.keysbuf, splitKeys(e.args[0])...) } default: cmd, ok := gOpts.cmds[e.name] diff --git a/eval_test.go b/eval_test.go index 51554048..7c696c3a 100644 --- a/eval_test.go +++ b/eval_test.go @@ -113,6 +113,36 @@ var gTests = []struct { []Expr{&MapExpr{"u", &ListExpr{[]Expr{&CallExpr{"usage", nil}}}}}, }, + { + "map r push :rename", + []string{"map", "r", "push", ":rename", "\n"}, + []Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename"}}}}, + }, + + { + "map r push :rename;", + []string{"map", "r", "push", ":rename;", "\n"}, + []Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename;"}}}}, + }, + + { + "map r push :rename # trailing comments area allowed after a space", + []string{"map", "r", "push", ":rename", "\n"}, + []Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename"}}}}, + }, + + { + "map r :push :rename", + []string{"map", "r", ":", "push", ":rename", "\n", "\n"}, + []Expr{&MapExpr{"r", &ListExpr{[]Expr{&CallExpr{"push", []string{":rename"}}}}}}, + }, + + { + "map r :push :rename ; set hidden", + []string{"map", "r", ":", "push", ":rename", ";", "set", "hidden", "\n", "\n"}, + []Expr{&MapExpr{"r", &ListExpr{[]Expr{&CallExpr{"push", []string{":rename"}}, &SetExpr{"hidden", ""}}}}}, + }, + { "map u $du -h . | less", []string{"map", "u", "$", "du -h . | less", "\n"}, diff --git a/parse.go b/parse.go index 963e2c4c..409a9310 100644 --- a/parse.go +++ b/parse.go @@ -22,7 +22,7 @@ package main // // Prefix = '$' | '!' | '&' | '/' | '?' // -// ListExpr = ':' ListExpr '\n' +// ListExpr = ':' ListRest '\n' // | ':' '{{' ListRest '}}' ';' // // ListRest = Nil diff --git a/scan.go b/scan.go index 8c294ab0..d93d0c53 100644 --- a/scan.go +++ b/scan.go @@ -30,6 +30,7 @@ type Scanner struct { sem bool // insert semicolon nln bool // insert newline eof bool // buffer ended + key bool // scanning keys blk bool // scanning block cmd bool // scanning command typ TokenType // scanned token type @@ -93,7 +94,6 @@ func isPrefix(b byte) bool { } func (s *Scanner) scan() bool { - // log.Println("scanning:", s.tok) scan: switch { case s.eof: @@ -111,6 +111,14 @@ scan: return true } return false + case s.key: + beg := s.off + for !s.eof && !isSpace(s.chr) { + s.next() + } + s.typ = TokenIdent + s.tok = string(s.buf[beg:s.off]) + s.key = false case s.blk: // return here by setting s.cmd to false // after scanning the command in the loop below @@ -233,6 +241,12 @@ scan: } s.typ = TokenIdent s.tok = string(s.buf[beg:s.off]) + if s.tok == "push" { + s.key = true + for isSpace(s.chr) { + s.next() + } + } s.sem = true }