implement cmap for command line mapping
This commit is contained in:
1 parent
0969514f10
commit
3fd1222e4f
7 files changed
+191
-97
No files matched your search
@@ -39,6 +39,25 @@ The following commands are provided by lf with default keybindings:
|
||||
put (default "p")
|
||||
renew (default "<c-l>")
|
||||
|
||||
The following command line commands are provided by lf with default
|
||||
keybindings:
|
||||
|
||||
cmd-escape (default "<esc>")
|
||||
cmd-insert (default "<space>")
|
||||
cmd-comp (default "<tab>")
|
||||
cmd-enter (default "<c-j>" and "<enter>")
|
||||
cmd-delete-back (default "<bs>" and "<bs2>")
|
||||
cmd-delete (default "<c-d>" and "<delete>")
|
||||
cmd-left (default "<c-b>" and "<left>")
|
||||
cmd-right (default "<c-f>" and "<right>")
|
||||
cmd-beg (default "<c-a>" and "<home>")
|
||||
cmd-end (default "<c-e>" and "<end>")
|
||||
cmd-delete-end (default "<c-k>")
|
||||
cmd-delete-beg (default "<c-u>")
|
||||
cmd-delete-word (default "<c-w>")
|
||||
cmd-put (default "<c-y>")
|
||||
cmd-transpose (default "<c-t>")
|
||||
|
||||
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 <c-g> cmd-escape
|
||||
|
||||
You can delete an existing binding by leaving the expression empty:
|
||||
|
||||
map gh # deletes 'gh' mapping
|
||||
map gh # deletes 'gh' mapping
|
||||
cmap <c-g> # deletes '<c-g>' 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
|
||||
}}
|
||||
|
||||
|
||||
+59
-32
@@ -43,6 +43,25 @@ The following commands are provided by lf with default keybindings:
|
||||
put (default "p")
|
||||
renew (default "<c-l>")
|
||||
|
||||
The following command line commands are provided by lf with default
|
||||
keybindings:
|
||||
|
||||
cmd-escape (default "<esc>")
|
||||
cmd-insert (default "<space>")
|
||||
cmd-comp (default "<tab>")
|
||||
cmd-enter (default "<c-j>" and "<enter>")
|
||||
cmd-delete-back (default "<bs>" and "<bs2>")
|
||||
cmd-delete (default "<c-d>" and "<delete>")
|
||||
cmd-left (default "<c-b>" and "<left>")
|
||||
cmd-right (default "<c-f>" and "<right>")
|
||||
cmd-beg (default "<c-a>" and "<home>")
|
||||
cmd-end (default "<c-e>" and "<end>")
|
||||
cmd-delete-end (default "<c-k>")
|
||||
cmd-delete-beg (default "<c-u>")
|
||||
cmd-delete-word (default "<c-w>")
|
||||
cmd-put (default "<c-y>")
|
||||
cmd-transpose (default "<c-t>")
|
||||
|
||||
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 <c-g> cmd-escape
|
||||
|
||||
You can delete an existing binding by leaving the expression empty:
|
||||
|
||||
map gh # deletes 'gh' mapping
|
||||
map gh # deletes 'gh' mapping
|
||||
cmap <c-g> # deletes '<c-g>' 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
|
||||
}}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -173,6 +173,12 @@ var gEvalTests = []struct {
|
||||
[]expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}}}}}},
|
||||
},
|
||||
|
||||
{
|
||||
"cmap <c-g> cmd-escape",
|
||||
[]string{"cmap", "<c-g>", "cmd-escape", "\n"},
|
||||
[]expr{&cmapExpr{"<c-g>", "cmd-escape"}},
|
||||
},
|
||||
|
||||
{
|
||||
"cmd usage $du -h . | less",
|
||||
[]string{"cmd", "usage", "$", "du -h . | less", "\n"},
|
||||
|
||||
@@ -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["<c-l>"] = &callExpr{"renew", nil}
|
||||
|
||||
gOpts.cmdkeys = make(map[string]expr)
|
||||
|
||||
// TODO: rest of the keys
|
||||
gOpts.cmdkeys["<esc>"] = &callExpr{"cmd-escape", nil}
|
||||
gOpts.cmdkeys["<space>"] = &callExpr{"cmd-insert", []string{" "}}
|
||||
gOpts.cmdkeys["<tab>"] = &callExpr{"cmd-comp", nil}
|
||||
gOpts.cmdkeys["<enter>"] = &callExpr{"cmd-enter", nil}
|
||||
gOpts.cmdkeys["<c-j>"] = &callExpr{"cmd-enter", nil}
|
||||
gOpts.cmdkeys["<bs>"] = &callExpr{"cmd-delete-back", nil}
|
||||
gOpts.cmdkeys["<bs2>"] = &callExpr{"cmd-delete-back", nil}
|
||||
gOpts.cmdkeys["<delete>"] = &callExpr{"cmd-delete", nil}
|
||||
gOpts.cmdkeys["<c-d>"] = &callExpr{"cmd-delete", nil}
|
||||
gOpts.cmdkeys["<left>"] = &callExpr{"cmd-left", nil}
|
||||
gOpts.cmdkeys["<c-b>"] = &callExpr{"cmd-left", nil}
|
||||
gOpts.cmdkeys["<right>"] = &callExpr{"cmd-right", nil}
|
||||
gOpts.cmdkeys["<c-f>"] = &callExpr{"cmd-right", nil}
|
||||
gOpts.cmdkeys["<home>"] = &callExpr{"cmd-beg", nil}
|
||||
gOpts.cmdkeys["<c-a>"] = &callExpr{"cmd-beg", nil}
|
||||
gOpts.cmdkeys["<end>"] = &callExpr{"cmd-end", nil}
|
||||
gOpts.cmdkeys["<c-e>"] = &callExpr{"cmd-end", nil}
|
||||
gOpts.cmdkeys["<c-k>"] = &callExpr{"cmd-delete-end", nil}
|
||||
gOpts.cmdkeys["<c-u>"] = &callExpr{"cmd-delete-beg", nil}
|
||||
gOpts.cmdkeys["<c-w>"] = &callExpr{"cmd-delete-word", nil}
|
||||
gOpts.cmdkeys["<c-y>"] = &callExpr{"cmd-put", nil}
|
||||
gOpts.cmdkeys["<c-t>"] = &callExpr{"cmd-transpose", nil}
|
||||
|
||||
gOpts.cmds = make(map[string]expr)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ package main
|
||||
//
|
||||
// Expr = SetExpr
|
||||
// | MapExpr
|
||||
// | CMapExpr
|
||||
// | CmdExpr
|
||||
// | CallExpr
|
||||
// | ExecExpr
|
||||
@@ -13,6 +14,8 @@ package main
|
||||
//
|
||||
// MapExpr = 'map' <keys> Expr ';'
|
||||
//
|
||||
// CMapExpr = 'cmap' <key> <cmd> ';'
|
||||
//
|
||||
// CmdExpr = 'cmd' <name> Expr ';'
|
||||
//
|
||||
// CallExpr = <name> <args> ';'
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user