6 "github.com/zyedidia/micro/v2/internal/buffer"
7 "github.com/zyedidia/micro/v2/internal/config"
8 "github.com/zyedidia/micro/v2/internal/display"
9 "github.com/zyedidia/micro/v2/internal/info"
10 "github.com/zyedidia/micro/v2/internal/util"
11 "github.com/zyedidia/tcell/v2"
14 type InfoKeyAction func(*InfoPane)
16 var InfoBindings *KeyTree
17 var InfoBufBindings *KeyTree
20 InfoBindings = NewKeyTree()
21 InfoBufBindings = NewKeyTree()
24 func InfoMapEvent(k Event, action string) {
25 config.Bindings["command"][k.Name()] = action
27 switch e := k.(type) {
28 case KeyEvent, KeySequenceEvent, RawEvent:
31 infoMapMouse(e, action)
35 func infoMapKey(k Event, action string) {
36 if f, ok := InfoKeyActions[action]; ok {
37 InfoBindings.RegisterKeyBinding(k, InfoKeyActionGeneral(f))
38 } else if f, ok := BufKeyActions[action]; ok {
39 InfoBufBindings.RegisterKeyBinding(k, BufKeyActionGeneral(f))
43 func infoMapMouse(k MouseEvent, action string) {
45 if f, ok := BufMouseActions[action]; ok {
46 InfoBufBindings.RegisterMouseBinding(k, BufMouseActionGeneral(f))
52 func InfoKeyActionGeneral(a InfoKeyAction) PaneKeyAction {
53 return func(p Pane) bool {
59 type InfoPane struct {
64 func NewInfoPane(ib *info.InfoBuf, w display.BWindow, tab *Tab) *InfoPane {
67 ip.BufPane = NewBufPane(ib.Buffer, w, tab)
68 ip.BufPane.bindings = InfoBufBindings
73 func NewInfoBar() *InfoPane {
74 ib := info.NewBuffer()
75 w := display.NewInfoWindow(ib)
76 return NewInfoPane(ib, w, nil)
79 func (h *InfoPane) Close() {
84 func (h *InfoPane) HandleEvent(event tcell.Event) {
85 switch e := event.(type) {
89 mod: metaToAlt(e.Modifiers()),
93 done := h.DoKeyEvent(ke)
95 if e.Key() == tcell.KeyRune && hasYN {
96 if (e.Rune() == 'y' || e.Rune() == 'Y') && hasYN {
99 } else if (e.Rune() == 'n' || e.Rune() == 'N') && hasYN {
104 if e.Key() == tcell.KeyRune && !done && !hasYN {
105 h.DoRuneInsert(e.Rune())
108 if done && h.HasPrompt && !hasYN {
109 resp := string(h.LineBytes(0))
110 hist := h.History[h.PromptType]
111 hist[h.HistoryNum] = resp
112 if h.EventCallback != nil {
113 h.EventCallback(resp)
117 h.BufPane.HandleEvent(event)
121 // DoKeyEvent executes a key event for the command bar, doing any overridden actions
122 func (h *InfoPane) DoKeyEvent(e KeyEvent) bool {
123 action, more := InfoBindings.NextEvent(e, nil)
124 if action != nil && !more {
126 InfoBindings.ResetEvents()
129 } else if action == nil && !more {
130 InfoBindings.ResetEvents()
131 // return false //TODO:?
135 action, more = InfoBufBindings.NextEvent(e, nil)
136 if action != nil && !more {
137 done := action(h.BufPane)
138 InfoBufBindings.ResetEvents()
140 } else if action == nil && !more {
141 InfoBufBindings.ResetEvents()
148 // HistoryUp cycles history up
149 func (h *InfoPane) HistoryUp() {
150 h.UpHistory(h.History[h.PromptType])
153 // HistoryDown cycles history down
154 func (h *InfoPane) HistoryDown() {
155 h.DownHistory(h.History[h.PromptType])
158 // Autocomplete begins autocompletion
159 func (h *InfoPane) CommandComplete() {
161 if b.HasSuggestions {
162 b.CycleAutocomplete(true)
166 c := b.GetActiveCursor()
168 l = util.SliceStart(l, c.X)
170 args := bytes.Split(l, []byte{' '})
171 cmd := string(args[0])
173 if h.PromptType == "Command" {
175 b.Autocomplete(CommandComplete)
176 } else if action, ok := commands[cmd]; ok {
177 if action.completer != nil {
178 b.Autocomplete(action.completer)
182 // by default use filename autocompletion
183 b.Autocomplete(buffer.FileComplete)
187 // ExecuteCommand completes the prompt
188 func (h *InfoPane) ExecuteCommand() {
194 // AbortCommand cancels the prompt
195 func (h *InfoPane) AbortCommand() {
199 // InfoKeyActions contains the list of all possible key actions the infopane could execute
200 var InfoKeyActions = map[string]InfoKeyAction{
201 "HistoryUp": (*InfoPane).HistoryUp,
202 "HistoryDown": (*InfoPane).HistoryDown,
203 "CommandComplete": (*InfoPane).CommandComplete,
204 "ExecuteCommand": (*InfoPane).ExecuteCommand,
205 "AbortCommand": (*InfoPane).AbortCommand,