Debian Codegraph @

internal/action/infopane.go

raw
1 package action
2
3 import (
4 "bytes"
5
6 "github.com/micro-editor/tcell/v2"
7 "github.com/zyedidia/micro/v2/internal/buffer"
8 "github.com/zyedidia/micro/v2/internal/config"
9 "github.com/zyedidia/micro/v2/internal/display"
10 "github.com/zyedidia/micro/v2/internal/info"
11 "github.com/zyedidia/micro/v2/internal/util"
12 )
13
14 type InfoKeyAction func(*InfoPane)
15
16 var InfoBindings *KeyTree
17 var InfoBufBindings *KeyTree
18
19 func init() {
20 InfoBindings = NewKeyTree()
21 InfoBufBindings = NewKeyTree()
22 }
23
24 func InfoMapEvent(k Event, action string) {
25 config.Bindings["command"][k.Name()] = action
26
27 switch e := k.(type) {
28 case KeyEvent, KeySequenceEvent, RawEvent:
29 infoMapKey(e, action)
30 case MouseEvent:
31 infoMapMouse(e, action)
32 }
33 }
34
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))
40 }
41 }
42
43 func infoMapMouse(k MouseEvent, action string) {
44 // TODO: map mouse
45 if f, ok := BufMouseActions[action]; ok {
46 InfoBufBindings.RegisterMouseBinding(k, BufMouseActionGeneral(f))
47 } else {
48 infoMapKey(k, action)
49 }
50 }
51
52 func InfoKeyActionGeneral(a InfoKeyAction) PaneKeyAction {
53 return func(p Pane) bool {
54 a(p.(*InfoPane))
55 return true
56 }
57 }
58
59 type InfoPane struct {
60 *BufPane
61 *info.InfoBuf
62 }
63
64 func NewInfoPane(ib *info.InfoBuf, w display.BWindow, tab *Tab) *InfoPane {
65 ip := new(InfoPane)
66 ip.InfoBuf = ib
67 ip.BufPane = NewBufPane(ib.Buffer, w, tab)
68 ip.BufPane.bindings = InfoBufBindings
69
70 return ip
71 }
72
73 func NewInfoBar() *InfoPane {
74 ib := info.NewBuffer()
75 w := display.NewInfoWindow(ib)
76 return NewInfoPane(ib, w, nil)
77 }
78
79 func (h *InfoPane) Close() {
80 h.InfoBuf.Close()
81 h.BufPane.Close()
82 }
83
84 func (h *InfoPane) HandleEvent(event tcell.Event) {
85 switch e := event.(type) {
86 case *tcell.EventResize:
87 // TODO
88 case *tcell.EventKey:
89 ke := keyEvent(e)
90
91 done := h.DoKeyEvent(ke)
92 hasYN := h.HasYN
93 if e.Key() == tcell.KeyRune && hasYN {
94 y := e.Rune() == 'y' || e.Rune() == 'Y'
95 n := e.Rune() == 'n' || e.Rune() == 'N'
96 if y || n {
97 h.YNResp = y
98 h.DonePrompt(false)
99
100 InfoBindings.ResetEvents()
101 InfoBufBindings.ResetEvents()
102 }
103 }
104 if e.Key() == tcell.KeyRune && !done && !hasYN {
105 h.DoRuneInsert(e.Rune())
106 done = true
107 }
108 if done && h.HasPrompt && !hasYN {
109 resp := string(h.LineBytes(0))
110 hist := h.History[h.PromptType]
111 if resp != hist[h.HistoryNum] {
112 h.HistoryNum = len(hist) - 1
113 hist[h.HistoryNum] = resp
114 h.HistorySearch = false
115 }
116 if h.EventCallback != nil {
117 h.EventCallback(resp)
118 }
119 }
120 default:
121 h.BufPane.HandleEvent(event)
122 }
123 }
124
125 // DoKeyEvent executes a key event for the command bar, doing any overridden actions.
126 // Returns true if the action was executed OR if there are more keys remaining
127 // to process before executing an action (if this is a key sequence event).
128 // Returns false if no action found.
129 func (h *InfoPane) DoKeyEvent(e KeyEvent) bool {
130 action, more := InfoBindings.NextEvent(e, nil)
131 if action != nil && !more {
132 action(h)
133 InfoBindings.ResetEvents()
134
135 return true
136 } else if action == nil && !more {
137 InfoBindings.ResetEvents()
138 // return false //TODO:?
139 }
140
141 if !more {
142 // If no infopane action found, try to find a bufpane action.
143 //
144 // TODO: this is buggy. For example, if the command bar has the following
145 // two bindings:
146 //
147 // "<Ctrl-x><Ctrl-p>": "HistoryUp",
148 // "<Ctrl-x><Ctrl-v>": "Paste",
149 //
150 // the 2nd binding (with a bufpane action) doesn't work, since <Ctrl-x>
151 // has been already consumed by the 1st binding (with an infopane action).
152 //
153 // We should either iterate both InfoBindings and InfoBufBindings keytrees
154 // together, or just use the same keytree for both infopane and bufpane
155 // bindings.
156 action, more = InfoBufBindings.NextEvent(e, nil)
157 if action != nil && !more {
158 action(h.BufPane)
159 InfoBufBindings.ResetEvents()
160 return true
161 } else if action == nil && !more {
162 InfoBufBindings.ResetEvents()
163 }
164 }
165
166 return more
167 }
168
169 // HistoryUp cycles history up
170 func (h *InfoPane) HistoryUp() {
171 h.UpHistory(h.History[h.PromptType])
172 }
173
174 // HistoryDown cycles history down
175 func (h *InfoPane) HistoryDown() {
176 h.DownHistory(h.History[h.PromptType])
177 }
178
179 // HistorySearchUp fetches the previous history item beginning with the text
180 // in the infobuffer before cursor
181 func (h *InfoPane) HistorySearchUp() {
182 h.SearchUpHistory(h.History[h.PromptType])
183 }
184
185 // HistorySearchDown fetches the next history item beginning with the text
186 // in the infobuffer before cursor
187 func (h *InfoPane) HistorySearchDown() {
188 h.SearchDownHistory(h.History[h.PromptType])
189 }
190
191 // Autocomplete begins autocompletion
192 func (h *InfoPane) CommandComplete() {
193 b := h.Buf
194 if b.HasSuggestions {
195 b.CycleAutocomplete(true)
196 return
197 }
198
199 c := b.GetActiveCursor()
200 l := b.LineBytes(0)
201 l = util.SliceStart(l, c.X)
202
203 args := bytes.Split(l, []byte{' '})
204 cmd := string(args[0])
205
206 if h.PromptType == "Command" {
207 if len(args) == 1 {
208 b.Autocomplete(CommandComplete)
209 } else if action, ok := commands[cmd]; ok {
210 if action.completer != nil {
211 b.Autocomplete(action.completer)
212 }
213 }
214 } else {
215 // by default use filename autocompletion
216 b.Autocomplete(buffer.FileComplete)
217 }
218 }
219
220 // ExecuteCommand completes the prompt
221 func (h *InfoPane) ExecuteCommand() {
222 if !h.HasYN {
223 h.DonePrompt(false)
224 }
225 }
226
227 // AbortCommand cancels the prompt
228 func (h *InfoPane) AbortCommand() {
229 h.DonePrompt(true)
230 }
231
232 // InfoKeyActions contains the list of all possible key actions the infopane could execute
233 var InfoKeyActions = map[string]InfoKeyAction{
234 "HistoryUp": (*InfoPane).HistoryUp,
235 "HistoryDown": (*InfoPane).HistoryDown,
236 "HistorySearchUp": (*InfoPane).HistorySearchUp,
237 "HistorySearchDown": (*InfoPane).HistorySearchDown,
238 "CommandComplete": (*InfoPane).CommandComplete,
239 "ExecuteCommand": (*InfoPane).ExecuteCommand,
240 "AbortCommand": (*InfoPane).AbortCommand,
241 }