Add nano-style key menu option

Use the `keymenu` option (default `off`) to enable. ToggleKeyMenu is
also bound to `Alt-g` and this info is now displayed in the status line.

Closes #829
This commit is contained in:
Zachary Yedidia 2017-10-02 23:44:11 -04:00
parent b3559df543
commit 8b8fffb98d
9 changed files with 81 additions and 7 deletions

View file

@ -1551,6 +1551,25 @@ func (v *View) ToggleHelp(usePlugin bool) bool {
return true return true
} }
// ToggleKeyMenu toggles the keymenu option and resizes all tabs
func (v *View) ToggleKeyMenu(usePlugin bool) bool {
if v.mainCursor() {
if usePlugin && !PreActionCall("ToggleBindings", v) {
return false
}
globalSettings["keymenu"] = !globalSettings["keymenu"].(bool)
for _, tab := range tabs {
tab.Resize()
}
if usePlugin {
return PostActionCall("ToggleBindings", v)
}
}
return true
}
// ShellMode opens a terminal to run a shell command // ShellMode opens a terminal to run a shell command
func (v *View) ShellMode(usePlugin bool) bool { func (v *View) ShellMode(usePlugin bool) bool {
if v.mainCursor() { if v.mainCursor() {

View file

@ -12,6 +12,7 @@ import (
var bindings map[Key][]func(*View, bool) bool var bindings map[Key][]func(*View, bool) bool
var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool var mouseBindings map[Key][]func(*View, bool, *tcell.EventMouse) bool
var helpBinding string var helpBinding string
var kmenuBinding string
var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{ var mouseBindingActions = map[string]func(*View, bool, *tcell.EventMouse) bool{
"MousePress": (*View).MousePress, "MousePress": (*View).MousePress,
@ -78,6 +79,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"StartOfLine": (*View).StartOfLine, "StartOfLine": (*View).StartOfLine,
"EndOfLine": (*View).EndOfLine, "EndOfLine": (*View).EndOfLine,
"ToggleHelp": (*View).ToggleHelp, "ToggleHelp": (*View).ToggleHelp,
"ToggleKeyMenu": (*View).ToggleKeyMenu,
"ToggleRuler": (*View).ToggleRuler, "ToggleRuler": (*View).ToggleRuler,
"JumpLine": (*View).JumpLine, "JumpLine": (*View).JumpLine,
"ClearStatus": (*View).ClearStatus, "ClearStatus": (*View).ClearStatus,
@ -397,9 +399,15 @@ func BindKey(k, v string) {
if v == "ToggleHelp" { if v == "ToggleHelp" {
helpBinding = k helpBinding = k
} }
if v == "ToggleKeyMenu" {
kmenuBinding = k
}
if helpBinding == k && v != "ToggleHelp" { if helpBinding == k && v != "ToggleHelp" {
helpBinding = "" helpBinding = ""
} }
if kmenuBinding == k && v != "ToggleKeyMenu" {
kmenuBinding = ""
}
actionNames := strings.Split(v, ",") actionNames := strings.Split(v, ",")
if actionNames[0] == "UnbindKey" { if actionNames[0] == "UnbindKey" {
@ -490,6 +498,7 @@ func DefaultBindings() map[string]string {
"CtrlPageUp": "PreviousTab", "CtrlPageUp": "PreviousTab",
"CtrlPageDown": "NextTab", "CtrlPageDown": "NextTab",
"CtrlG": "ToggleHelp", "CtrlG": "ToggleHelp",
"Alt-g": "ToggleKeyMenu",
"CtrlR": "ToggleRuler", "CtrlR": "ToggleRuler",
"CtrlL": "JumpLine", "CtrlL": "JumpLine",
"Delete": "Delete", "Delete": "Delete",
@ -509,7 +518,6 @@ func DefaultBindings() map[string]string {
// "Alt-n": "CursorDown", // "Alt-n": "CursorDown",
// Integration with file managers // Integration with file managers
"F1": "ToggleHelp",
"F2": "Save", "F2": "Save",
"F3": "Find", "F3": "Find",
"F4": "Quit", "F4": "Quit",

20
cmd/micro/keymenu.go Normal file
View file

@ -0,0 +1,20 @@
package main
// DisplayKeyMenu displays the nano-style key menu at the bottom of the screen
func DisplayKeyMenu() {
w, h := screen.Size()
bot := h - 3
display := []string{"^Q Quit, ^S Save, ^O Open, ^G Help, ^E Command Bar, ^K Cut Line", "^F Find, ^Z Undo, ^Y Redo, ^A Select All, ^D Duplicate Line, ^T New Tab"}
for y := 0; y < len(display); y++ {
for x := 0; x < w; x++ {
if x < len(display[y]) {
screen.SetContent(x, bot+y, rune(display[y][x]), nil, defStyle)
} else {
screen.SetContent(x, bot+y, ' ', nil, defStyle)
}
}
}
}

View file

@ -225,6 +225,9 @@ func RedrawAll() {
} }
DisplayTabs() DisplayTabs()
messenger.Display() messenger.Display()
if globalSettings["keymenu"].(bool) {
DisplayKeyMenu()
}
screen.Show() screen.Show()
} }

File diff suppressed because one or more lines are too long

View file

@ -204,6 +204,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"ignorecase": false, "ignorecase": false,
"indentchar": " ", "indentchar": " ",
"infobar": true, "infobar": true,
"keymenu": false,
"mouse": true, "mouse": true,
"rmtrailingws": false, "rmtrailingws": false,
"ruler": true, "ruler": true,
@ -312,7 +313,7 @@ func SetOption(option, value string) error {
} }
} }
if option == "infobar" { if option == "infobar" || option == "keymenu" {
for _, tab := range tabs { for _, tab := range tabs {
tab.Resize() tab.Resize()
} }

View file

@ -39,12 +39,24 @@ func (sline *Statusline) Display() {
file += " " + sline.view.Buf.Settings["fileformat"].(string) file += " " + sline.view.Buf.Settings["fileformat"].(string)
rightText := "" rightText := ""
if len(helpBinding) > 0 { if len(kmenuBinding) > 0 {
rightText = helpBinding + " for help " if globalSettings["keymenu"].(bool) {
if sline.view.Type == vtHelp { rightText += kmenuBinding + ": hide bindings"
rightText = helpBinding + " to close help " } else {
rightText += kmenuBinding + ": show bindings"
} }
} }
if len(helpBinding) > 0 {
if len(kmenuBinding) > 0 {
rightText += ", "
}
if sline.view.Type == vtHelp {
rightText += helpBinding + ": close help"
} else {
rightText += helpBinding + ": open help"
}
}
rightText += " "
statusLineStyle := defStyle.Reverse(true) statusLineStyle := defStyle.Reverse(true)
if style, ok := colorscheme["statusline"]; ok { if style, ok := colorscheme["statusline"]; ok {

View file

@ -36,6 +36,9 @@ func NewTabFromView(v *View) *Tab {
if globalSettings["infobar"].(bool) { if globalSettings["infobar"].(bool) {
t.tree.height-- t.tree.height--
} }
if globalSettings["keymenu"].(bool) {
t.tree.height -= 2
}
t.Resize() t.Resize()
@ -62,6 +65,9 @@ func (t *Tab) Resize() {
if globalSettings["infobar"].(bool) { if globalSettings["infobar"].(bool) {
t.tree.height-- t.tree.height--
} }
if globalSettings["keymenu"].(bool) {
t.tree.height -= 2
}
t.tree.ResizeSplits() t.tree.ResizeSplits()

View file

@ -182,6 +182,11 @@ Here are the options that you can set:
default value: `sudo` default value: `sudo`
* `keymenu`: display the nano-style key menu at the bottom of the screen. Note that ToggleKeyMenu is bound to
`Alt-g` by default and this is displayed in the statusline. To disable this, simply by `Alt-g` to `UnbindKey`.
default value: `off`
--- ---
Default plugin options: Default plugin options: