]> git.lizzy.rs Git - micro.git/blob - internal/action/infopane.go
Remove autosave option
[micro.git] / internal / action / infopane.go
1 package action
2
3 import (
4         "bytes"
5         "strings"
6
7         "github.com/zyedidia/micro/internal/display"
8         "github.com/zyedidia/micro/internal/info"
9         "github.com/zyedidia/micro/internal/util"
10         "github.com/zyedidia/tcell"
11 )
12
13 type InfoKeyAction func(*InfoPane)
14
15 type InfoPane struct {
16         *BufPane
17         *info.InfoBuf
18 }
19
20 func NewInfoPane(ib *info.InfoBuf, w display.BWindow) *InfoPane {
21         ip := new(InfoPane)
22         ip.InfoBuf = ib
23         ip.BufPane = NewBufPane(ib.Buffer, w)
24
25         return ip
26 }
27
28 func NewInfoBar() *InfoPane {
29         ib := info.NewBuffer()
30         w := display.NewInfoWindow(ib)
31         return NewInfoPane(ib, w)
32 }
33
34 func (h *InfoPane) Close() {
35         h.InfoBuf.Close()
36         h.BufPane.Close()
37 }
38
39 func (h *InfoPane) HandleEvent(event tcell.Event) {
40         switch e := event.(type) {
41         case *tcell.EventKey:
42                 ke := KeyEvent{
43                         code: e.Key(),
44                         mod:  e.Modifiers(),
45                         r:    e.Rune(),
46                 }
47
48                 done := h.DoKeyEvent(ke)
49                 hasYN := h.HasYN
50                 if e.Key() == tcell.KeyRune && hasYN {
51                         if e.Rune() == 'y' && hasYN {
52                                 h.YNResp = true
53                                 h.DonePrompt(false)
54                         } else if e.Rune() == 'n' && hasYN {
55                                 h.YNResp = false
56                                 h.DonePrompt(false)
57                         }
58                 }
59                 if e.Key() == tcell.KeyRune && !done && !hasYN {
60                         h.DoRuneInsert(e.Rune())
61                         done = true
62                 }
63                 if done && h.HasPrompt && !hasYN {
64                         resp := string(h.LineBytes(0))
65                         hist := h.History[h.PromptType]
66                         hist[h.HistoryNum] = resp
67                         if h.EventCallback != nil {
68                                 h.EventCallback(resp)
69                         }
70                 }
71         case *tcell.EventMouse:
72                 h.BufPane.HandleEvent(event)
73         }
74 }
75
76 func (h *InfoPane) DoKeyEvent(e KeyEvent) bool {
77         done := false
78         if action, ok := BufKeyBindings[e]; ok {
79                 estr := BufKeyStrings[e]
80                 for _, s := range InfoNones {
81                         if s == estr {
82                                 return false
83                         }
84                 }
85                 for s, a := range InfoOverrides {
86                         // TODO this is a hack and really we should have support
87                         // for having binding overrides for different buffers
88                         if strings.Contains(estr, s) {
89                                 done = true
90                                 a(h)
91                                 break
92                         }
93                 }
94                 if !done {
95                         done = action(h.BufPane)
96                 }
97         }
98         return done
99 }
100
101 // InfoNones is a list of actions that should have no effect when executed
102 // by an infohandler
103 var InfoNones = []string{
104         "Save",
105         "SaveAll",
106         "SaveAs",
107         "Find",
108         "FindNext",
109         "FindPrevious",
110         "Center",
111         "DuplicateLine",
112         "MoveLinesUp",
113         "MoveLinesDown",
114         "OpenFile",
115         "Start",
116         "End",
117         "PageUp",
118         "PageDown",
119         "SelectPageUp",
120         "SelectPageDown",
121         "HalfPageUp",
122         "HalfPageDown",
123         "ToggleHelp",
124         "ToggleKeyMenu",
125         "ToggleRuler",
126         "JumpLine",
127         "ClearStatus",
128         "ShellMode",
129         "CommandMode",
130         "AddTab",
131         "PreviousTab",
132         "NextTab",
133         "NextSplit",
134         "PreviousSplit",
135         "Unsplit",
136         "VSplit",
137         "HSplit",
138         "ToggleMacro",
139         "PlayMacro",
140         "Suspend",
141         "ScrollUp",
142         "ScrollDown",
143         "SpawnMultiCursor",
144         "SpawnMultiCursorSelect",
145         "RemoveMultiCursor",
146         "RemoveAllMultiCursors",
147         "SkipMultiCursor",
148 }
149
150 // InfoOverrides is the list of actions which have been overriden
151 // by the infohandler
152 var InfoOverrides = map[string]InfoKeyAction{
153         "CursorUp":      (*InfoPane).CursorUp,
154         "CursorDown":    (*InfoPane).CursorDown,
155         "InsertNewline": (*InfoPane).InsertNewline,
156         "Autocomplete":  (*InfoPane).Autocomplete,
157         "OutdentLine":   (*InfoPane).CycleBack,
158         "Escape":        (*InfoPane).Escape,
159         "Quit":          (*InfoPane).Quit,
160         "QuitAll":       (*InfoPane).QuitAll,
161 }
162
163 // CursorUp cycles history up
164 func (h *InfoPane) CursorUp() {
165         h.UpHistory(h.History[h.PromptType])
166 }
167
168 // CursorDown cycles history down
169 func (h *InfoPane) CursorDown() {
170         h.DownHistory(h.History[h.PromptType])
171 }
172
173 // Autocomplete begins autocompletion
174 func (h *InfoPane) Autocomplete() {
175         b := h.Buf
176         if b.HasSuggestions {
177                 b.CycleAutocomplete(true)
178                 return
179         }
180
181         c := b.GetActiveCursor()
182         l := b.LineBytes(0)
183         l = util.SliceStart(l, c.X)
184
185         args := bytes.Split(l, []byte{' '})
186         cmd := string(args[0])
187
188         if len(args) == 1 {
189                 b.Autocomplete(CommandComplete)
190         } else {
191                 if action, ok := commands[cmd]; ok {
192                         if action.completer != nil {
193                                 b.Autocomplete(action.completer)
194                         }
195                 }
196         }
197 }
198
199 // CycleBack cycles back in the autocomplete suggestion list
200 func (h *InfoPane) CycleBack() {
201         if h.Buf.HasSuggestions {
202                 h.Buf.CycleAutocomplete(false)
203         }
204 }
205
206 // InsertNewline completes the prompt
207 func (h *InfoPane) InsertNewline() {
208         if !h.HasYN {
209                 h.DonePrompt(false)
210         }
211 }
212
213 // Quit cancels the prompt
214 func (h *InfoPane) Quit() {
215         h.DonePrompt(true)
216 }
217
218 // QuitAll cancels the prompt
219 func (h *InfoPane) QuitAll() {
220         h.DonePrompt(true)
221 }
222
223 // Escape cancels the prompt
224 func (h *InfoPane) Escape() {
225         h.DonePrompt(true)
226 }