From 3fd1222e4f1597321630bfe5e5f6b91be4251c4b Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 10 Mar 2017 18:53:21 +0300 Subject: [PATCH] implement cmap for command line mapping --- doc.go | 93 +++++++++++++++++++++++++++++++++------------------- docstring.go | 91 ++++++++++++++++++++++++++++++++------------------ eval.go | 8 +++++ eval_test.go | 6 ++++ opts.go | 27 +++++++++++++++ parse.go | 28 ++++++++++++++++ ui.go | 35 ++------------------ 7 files changed, 191 insertions(+), 97 deletions(-) diff --git a/doc.go b/doc.go index 672fd6bc..cacc38f2 100644 --- a/doc.go +++ b/doc.go @@ -39,6 +39,25 @@ The following commands are provided by lf with default keybindings: put (default "p") renew (default "") +The following command line commands are provided by lf with default +keybindings: + + cmd-escape (default "") + cmd-insert (default "") + cmd-comp (default "") + cmd-enter (default "" and "") + cmd-delete-back (default "" and "") + cmd-delete (default "" and "") + cmd-left (default "" and "") + cmd-right (default "" and "") + cmd-beg (default "" and "") + cmd-end (default "" and "") + cmd-delete-end (default "") + cmd-delete-beg (default "") + cmd-delete-word (default "") + cmd-put (default "") + cmd-transpose (default "") + The following commands are provided by lf without default keybindings: sync synchronizes yanked/deleted files with server @@ -48,20 +67,20 @@ The following commands are provided by lf without default keybindings: The following options can be used to customize the behavior of lf: - dirfirst boolean (default on) - hidden boolean (default off) - preview boolean (default on) - reverse boolean (default off) - scrolloff integer (default 0) - tabstop integer (default 8) - filesep string (default ":") - ifs string (default "") (not exported if empty) - previewer string (default "") (not filtered if empty) - shell string (default "/bin/sh") - sortby string (default "natural") - timefmt string (default "Mon Jan _2 15:04:05 2006") - ratios string (default "1:2:3") - info string (default "") + dirfirst boolean (default on) + hidden boolean (default off) + preview boolean (default on) + reverse boolean (default off) + scrolloff integer (default 0) + tabstop integer (default 8) + filesep string (default ":") + ifs string (default "") (not exported if empty) + previewer string (default "") (not filtered if empty) + shell string (default "/bin/sh") + sortby string (default "natural") + timefmt string (default "Mon Jan _2 15:04:05 2006") + ratios string (default "1:2:3") + info string (default "") The following variables are exported for shell commands: @@ -106,28 +125,36 @@ Characters from "#" to "\n" are comments and ignored: # comments start with '#' -There are three special commands for configuration. +There are three special commands ("set", "map", and "cmd") and their variants +for configuration. -"set" is used to set an option which could be boolean, integer, or string: +"set" is used to set an option which can be boolean, integer, or string: - set hidden # boolean on - set nohidden # boolean off - set hidden! # boolean toggle - set scrolloff 10 # integer value - set sortby time # string value w/o quotes - set sortby 'time' # string value with quotes + set hidden # boolean on + set nohidden # boolean off + set hidden! # boolean toggle + set scrolloff 10 # integer value + set sortby time # string value w/o quotes + set sortby 'time' # string value with single quotes (whitespaces) + set sortby "time" # string value with double quotes (backslash escapes) -"map" is used to bind a key to a command which could be built-in command, -custom command, or shell command: +"map" is used to bind a key to a command which can be built-in command, custom +command, or shell command: - map gh cd ~ # built-in command - map D trash # custom command - map i $less "$f" # shell command - map u !du -h . # waiting shell command + map gh cd ~ # built-in command + map D trash # custom command + map i $less "$f" # shell command + map u !du -h . # waiting shell command + +"cmap" is used to bind a key to a command line command which can only be one of +the built-in commands: + + cmap cmd-escape You can delete an existing binding by leaving the expression empty: - map gh # deletes 'gh' mapping + map gh # deletes 'gh' mapping + cmap # deletes '' mapping "cmd" is used to define a custom command @@ -135,13 +162,13 @@ You can delete an existing binding by leaving the expression empty: You can delete an existing command by leaving the expression empty: - cmd trash # deletes trash command + cmd trash # deletes trash command If there is no prefix then ":" is assumed: map zt set info time -An explicit ":" could be provided to group statements until a "\n" occurs which +An explicit ":" can be provided to group statements until a "\n" occurs which is especially useful for "map" and "cmd" commands: map st :set sortby time; set info time @@ -219,7 +246,7 @@ Since these are one liners, we can drop "{{" and "}}": Finally note that we set "IFS" variable accordingly in the command. Instead we could use the "ifs" option to set it for all commands (e.g. "set ifs ':'"). -This could be especially useful for interactive use (e.g. "rm $fs" would simply +This can be especially useful for interactive use (e.g. "rm $fs" would simply work). This option is not set by default as things may behave unexpectedly at other places. @@ -281,7 +308,7 @@ on startup based on terminal width: elif [ $w -le 160 ]; then lf -remote "send $id set ratios 1:2:3" else - lf -remote "send $id set ratios 1:2:3:4" + lf -remote "send $id set ratios 1:2:3:5" fi }} diff --git a/docstring.go b/docstring.go index e5678b81..b62ade42 100644 --- a/docstring.go +++ b/docstring.go @@ -43,6 +43,25 @@ The following commands are provided by lf with default keybindings: put (default "p") renew (default "") +The following command line commands are provided by lf with default +keybindings: + + cmd-escape (default "") + cmd-insert (default "") + cmd-comp (default "") + cmd-enter (default "" and "") + cmd-delete-back (default "" and "") + cmd-delete (default "" and "") + cmd-left (default "" and "") + cmd-right (default "" and "") + cmd-beg (default "" and "") + cmd-end (default "" and "") + cmd-delete-end (default "") + cmd-delete-beg (default "") + cmd-delete-word (default "") + cmd-put (default "") + cmd-transpose (default "") + The following commands are provided by lf without default keybindings: sync synchronizes yanked/deleted files with server @@ -52,20 +71,20 @@ The following commands are provided by lf without default keybindings: The following options can be used to customize the behavior of lf: - dirfirst boolean (default on) - hidden boolean (default off) - preview boolean (default on) - reverse boolean (default off) - scrolloff integer (default 0) - tabstop integer (default 8) - filesep string (default ":") - ifs string (default "") (not exported if empty) - previewer string (default "") (not filtered if empty) - shell string (default "/bin/sh") - sortby string (default "natural") - timefmt string (default "Mon Jan _2 15:04:05 2006") - ratios string (default "1:2:3") - info string (default "") + dirfirst boolean (default on) + hidden boolean (default off) + preview boolean (default on) + reverse boolean (default off) + scrolloff integer (default 0) + tabstop integer (default 8) + filesep string (default ":") + ifs string (default "") (not exported if empty) + previewer string (default "") (not filtered if empty) + shell string (default "/bin/sh") + sortby string (default "natural") + timefmt string (default "Mon Jan _2 15:04:05 2006") + ratios string (default "1:2:3") + info string (default "") The following variables are exported for shell commands: @@ -113,28 +132,36 @@ Characters from "#" to "\n" are comments and ignored: # comments start with '#' -There are three special commands for configuration. +There are three special commands ("set", "map", and "cmd") and their +variants for configuration. -"set" is used to set an option which could be boolean, integer, or string: +"set" is used to set an option which can be boolean, integer, or string: - set hidden # boolean on - set nohidden # boolean off - set hidden! # boolean toggle - set scrolloff 10 # integer value - set sortby time # string value w/o quotes - set sortby 'time' # string value with quotes + set hidden # boolean on + set nohidden # boolean off + set hidden! # boolean toggle + set scrolloff 10 # integer value + set sortby time # string value w/o quotes + set sortby 'time' # string value with single quotes (whitespaces) + set sortby "time" # string value with double quotes (backslash escapes) -"map" is used to bind a key to a command which could be built-in command, +"map" is used to bind a key to a command which can be built-in command, custom command, or shell command: - map gh cd ~ # built-in command - map D trash # custom command - map i $less "$f" # shell command - map u !du -h . # waiting shell command + map gh cd ~ # built-in command + map D trash # custom command + map i $less "$f" # shell command + map u !du -h . # waiting shell command + +"cmap" is used to bind a key to a command line command which can only be one +of the built-in commands: + + cmap cmd-escape You can delete an existing binding by leaving the expression empty: - map gh # deletes 'gh' mapping + map gh # deletes 'gh' mapping + cmap # deletes '' mapping "cmd" is used to define a custom command @@ -142,13 +169,13 @@ You can delete an existing binding by leaving the expression empty: You can delete an existing command by leaving the expression empty: - cmd trash # deletes trash command + cmd trash # deletes trash command If there is no prefix then ":" is assumed: map zt set info time -An explicit ":" could be provided to group statements until a "\n" occurs +An explicit ":" can be provided to group statements until a "\n" occurs which is especially useful for "map" and "cmd" commands: map st :set sortby time; set info time @@ -230,7 +257,7 @@ Since these are one liners, we can drop "{{" and "}}": Finally note that we set "IFS" variable accordingly in the command. Instead we could use the "ifs" option to set it for all commands (e.g. "set ifs -':'"). This could be especially useful for interactive use (e.g. "rm $fs" +':'"). This can be especially useful for interactive use (e.g. "rm $fs" would simply work). This option is not set by default as things may behave unexpectedly at other places. @@ -294,7 +321,7 @@ columns on startup based on terminal width: elif [ $w -le 160 ]; then lf -remote "send $id set ratios 1:2:3" else - lf -remote "send $id set ratios 1:2:3:4" + lf -remote "send $id set ratios 1:2:3:5" fi }} diff --git a/eval.go b/eval.go index 6a9614e9..f702a527 100644 --- a/eval.go +++ b/eval.go @@ -139,6 +139,14 @@ func (e *mapExpr) eval(app *app, args []string) { gOpts.keys[e.keys] = e.expr } +func (e *cmapExpr) eval(app *app, args []string) { + if e.cmd == "" { + delete(gOpts.cmdkeys, e.key) + return + } + gOpts.cmdkeys[e.key] = &callExpr{e.cmd, nil} +} + func (e *cmdExpr) eval(app *app, args []string) { if e.expr == nil { delete(gOpts.cmds, e.name) diff --git a/eval_test.go b/eval_test.go index 49ede3a5..23400d32 100644 --- a/eval_test.go +++ b/eval_test.go @@ -173,6 +173,12 @@ var gEvalTests = []struct { []expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}}}}}}, }, + { + "cmap cmd-escape", + []string{"cmap", "", "cmd-escape", "\n"}, + []expr{&cmapExpr{"", "cmd-escape"}}, + }, + { "cmd usage $du -h . | less", []string{"cmd", "usage", "$", "du -h . | less", "\n"}, diff --git a/opts.go b/opts.go index 6de6274b..da6113d8 100644 --- a/opts.go +++ b/opts.go @@ -18,6 +18,7 @@ var gOpts struct { ratios []int info []string keys map[string]expr + cmdkeys map[string]expr cmds map[string]expr } @@ -68,5 +69,31 @@ func init() { gOpts.keys["p"] = &callExpr{"put", nil} gOpts.keys[""] = &callExpr{"renew", nil} + gOpts.cmdkeys = make(map[string]expr) + + // TODO: rest of the keys + gOpts.cmdkeys[""] = &callExpr{"cmd-escape", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-insert", []string{" "}} + gOpts.cmdkeys[""] = &callExpr{"cmd-comp", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-enter", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-enter", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete-back", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete-back", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-left", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-left", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-right", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-right", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-beg", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-beg", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-end", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-end", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete-end", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete-beg", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-delete-word", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-put", nil} + gOpts.cmdkeys[""] = &callExpr{"cmd-transpose", nil} + gOpts.cmds = make(map[string]expr) } diff --git a/parse.go b/parse.go index e4d61b3c..43ddd75b 100644 --- a/parse.go +++ b/parse.go @@ -4,6 +4,7 @@ package main // // Expr = SetExpr // | MapExpr +// | CMapExpr // | CmdExpr // | CallExpr // | ExecExpr @@ -13,6 +14,8 @@ package main // // MapExpr = 'map' Expr ';' // +// CMapExpr = 'cmap' ';' +// // CmdExpr = 'cmd' Expr ';' // // CallExpr = ';' @@ -54,6 +57,13 @@ type mapExpr struct { func (e *mapExpr) String() string { return fmt.Sprintf("map %s %s", e.keys, e.expr) } +type cmapExpr struct { + key string + cmd string +} + +func (e *cmapExpr) String() string { return fmt.Sprintf("cmap %s %s", e.key, e.cmd) } + type cmdExpr struct { name string expr expr @@ -147,6 +157,24 @@ func (p *parser) parseExpr() expr { } result = &mapExpr{keys, expr} + case "cmap": + var cmd string + + s.scan() + key := s.tok + + s.scan() + if s.typ != tokenSemicolon { + if s.typ != tokenIdent { + p.err = fmt.Errorf("expected command: %s", s.tok) + } + cmd = s.tok + s.scan() + } + + s.scan() + + result = &cmapExpr{key, cmd} case "cmd": var expr expr diff --git a/ui.go b/ui.go index 8ed295a2..d00d3a61 100644 --- a/ui.go +++ b/ui.go @@ -647,38 +647,9 @@ func readCmdEvent(ch chan<- multiExpr, ev termbox.Event) { if ev.Ch != 0 { ch <- multiExpr{&callExpr{"cmd-insert", []string{string(ev.Ch)}}, 1} } else { - // TODO: rest of the keys - switch ev.Key { - case termbox.KeyEsc: - ch <- multiExpr{&callExpr{"cmd-escape", nil}, 1} - case termbox.KeySpace: - ch <- multiExpr{&callExpr{"cmd-insert", []string{" "}}, 1} - case termbox.KeyTab: - ch <- multiExpr{&callExpr{"cmd-comp", nil}, 1} - case termbox.KeyEnter, termbox.KeyCtrlJ: - ch <- multiExpr{&callExpr{"cmd-enter", nil}, 1} - case termbox.KeyBackspace, termbox.KeyBackspace2: - ch <- multiExpr{&callExpr{"cmd-delete-back", nil}, 1} - case termbox.KeyDelete, termbox.KeyCtrlD: - ch <- multiExpr{&callExpr{"cmd-delete", nil}, 1} - case termbox.KeyArrowLeft, termbox.KeyCtrlB: - ch <- multiExpr{&callExpr{"cmd-left", nil}, 1} - case termbox.KeyArrowRight, termbox.KeyCtrlF: - ch <- multiExpr{&callExpr{"cmd-right", nil}, 1} - case termbox.KeyHome, termbox.KeyCtrlA: - ch <- multiExpr{&callExpr{"cmd-beg", nil}, 1} - case termbox.KeyEnd, termbox.KeyCtrlE: - ch <- multiExpr{&callExpr{"cmd-end", nil}, 1} - case termbox.KeyCtrlK: - ch <- multiExpr{&callExpr{"cmd-delete-end", nil}, 1} - case termbox.KeyCtrlU: - ch <- multiExpr{&callExpr{"cmd-delete-beg", nil}, 1} - case termbox.KeyCtrlW: - ch <- multiExpr{&callExpr{"cmd-delete-word", nil}, 1} - case termbox.KeyCtrlY: - ch <- multiExpr{&callExpr{"cmd-put", nil}, 1} - case termbox.KeyCtrlT: - ch <- multiExpr{&callExpr{"cmd-transpose", nil}, 1} + val := gKeyVal[ev.Key] + if expr, ok := gOpts.cmdkeys[string(val)]; ok { + ch <- multiExpr{expr, 1} } } }