]> git.lizzy.rs Git - micro.git/blob - cmd/micro/bufactionhandler.go
Cursor improvements
[micro.git] / cmd / micro / bufactionhandler.go
1 package main
2
3 import (
4         "time"
5
6         "github.com/zyedidia/micro/cmd/micro/buffer"
7         "github.com/zyedidia/tcell"
8 )
9
10 type BufKeyAction func(*BufActionHandler) bool
11 type BufMouseAction func(*BufActionHandler, *tcell.EventMouse) bool
12
13 var BufKeyBindings map[KeyEvent]BufKeyAction
14 var BufMouseBindings map[MouseEvent]BufMouseAction
15
16 func init() {
17         BufKeyBindings = make(map[KeyEvent]BufKeyAction)
18         BufMouseBindings = make(map[MouseEvent]BufMouseAction)
19 }
20
21 func BufMapKey(k KeyEvent, action string) {
22         BufKeyBindings[k] = BufKeyActions[action]
23 }
24 func BufMapMouse(k MouseEvent, action string) {
25         BufMouseBindings[k] = BufMouseActions[action]
26 }
27
28 // The BufActionHandler connects the buffer and the window
29 // It provides a cursor (or multiple) and defines a set of actions
30 // that can be taken on the buffer
31 // The ActionHandler can access the window for necessary info about
32 // visual positions for mouse clicks and scrolling
33 type BufActionHandler struct {
34         Buf *buffer.Buffer
35         Win *Window
36
37         cursors []*buffer.Cursor
38         Cursor  *buffer.Cursor // the active cursor
39
40         // Since tcell doesn't differentiate between a mouse release event
41         // and a mouse move event with no keys pressed, we need to keep
42         // track of whether or not the mouse was pressed (or not released) last event to determine
43         // mouse release events
44         mouseReleased bool
45
46         // We need to keep track of insert key press toggle
47         isOverwriteMode bool
48         // This stores when the last click was
49         // This is useful for detecting double and triple clicks
50         lastClickTime time.Time
51         lastLoc       buffer.Loc
52
53         // lastCutTime stores when the last ctrl+k was issued.
54         // It is used for clearing the clipboard to replace it with fresh cut lines.
55         lastCutTime time.Time
56
57         // freshClip returns true if the clipboard has never been pasted.
58         freshClip bool
59
60         // Was the last mouse event actually a double click?
61         // Useful for detecting triple clicks -- if a double click is detected
62         // but the last mouse event was actually a double click, it's a triple click
63         doubleClick bool
64         // Same here, just to keep track for mouse move events
65         tripleClick bool
66 }
67
68 func NewBufActionHandler(buf *buffer.Buffer, win *Window) *BufActionHandler {
69         a := new(BufActionHandler)
70         a.Buf = buf
71         a.Win = win
72
73         a.cursors = []*buffer.Cursor{&buffer.Cursor{
74                 Buf: buf,
75                 Loc: buf.StartCursor,
76         }}
77         a.Cursor = a.cursors[0]
78
79         buf.SetCursors(a.cursors)
80         return a
81 }
82
83 // HandleEvent executes the tcell event properly
84 // TODO: multiple actions bound to one key
85 func (a *BufActionHandler) HandleEvent(event tcell.Event) {
86         switch e := event.(type) {
87         case *tcell.EventKey:
88                 ke := KeyEvent{
89                         code: e.Key(),
90                         mod:  e.Modifiers(),
91                         r:    e.Rune(),
92                 }
93                 if action, ok := BufKeyBindings[ke]; ok {
94                         action(a)
95                 }
96         case *tcell.EventMouse:
97                 me := MouseEvent{
98                         btn: e.Buttons(),
99                         mod: e.Modifiers(),
100                 }
101                 if action, ok := BufMouseBindings[me]; ok {
102                         action(a, e)
103                 }
104         }
105 }
106
107 var BufKeyActions = map[string]BufKeyAction{
108         "CursorUp":               (*BufActionHandler).CursorUp,
109         "CursorDown":             (*BufActionHandler).CursorDown,
110         "CursorPageUp":           (*BufActionHandler).CursorPageUp,
111         "CursorPageDown":         (*BufActionHandler).CursorPageDown,
112         "CursorLeft":             (*BufActionHandler).CursorLeft,
113         "CursorRight":            (*BufActionHandler).CursorRight,
114         "CursorStart":            (*BufActionHandler).CursorStart,
115         "CursorEnd":              (*BufActionHandler).CursorEnd,
116         "SelectToStart":          (*BufActionHandler).SelectToStart,
117         "SelectToEnd":            (*BufActionHandler).SelectToEnd,
118         "SelectUp":               (*BufActionHandler).SelectUp,
119         "SelectDown":             (*BufActionHandler).SelectDown,
120         "SelectLeft":             (*BufActionHandler).SelectLeft,
121         "SelectRight":            (*BufActionHandler).SelectRight,
122         "WordRight":              (*BufActionHandler).WordRight,
123         "WordLeft":               (*BufActionHandler).WordLeft,
124         "SelectWordRight":        (*BufActionHandler).SelectWordRight,
125         "SelectWordLeft":         (*BufActionHandler).SelectWordLeft,
126         "DeleteWordRight":        (*BufActionHandler).DeleteWordRight,
127         "DeleteWordLeft":         (*BufActionHandler).DeleteWordLeft,
128         "SelectLine":             (*BufActionHandler).SelectLine,
129         "SelectToStartOfLine":    (*BufActionHandler).SelectToStartOfLine,
130         "SelectToEndOfLine":      (*BufActionHandler).SelectToEndOfLine,
131         "ParagraphPrevious":      (*BufActionHandler).ParagraphPrevious,
132         "ParagraphNext":          (*BufActionHandler).ParagraphNext,
133         "InsertNewline":          (*BufActionHandler).InsertNewline,
134         "InsertSpace":            (*BufActionHandler).InsertSpace,
135         "Backspace":              (*BufActionHandler).Backspace,
136         "Delete":                 (*BufActionHandler).Delete,
137         "InsertTab":              (*BufActionHandler).InsertTab,
138         "Save":                   (*BufActionHandler).Save,
139         "SaveAll":                (*BufActionHandler).SaveAll,
140         "SaveAs":                 (*BufActionHandler).SaveAs,
141         "Find":                   (*BufActionHandler).Find,
142         "FindNext":               (*BufActionHandler).FindNext,
143         "FindPrevious":           (*BufActionHandler).FindPrevious,
144         "Center":                 (*BufActionHandler).Center,
145         "Undo":                   (*BufActionHandler).Undo,
146         "Redo":                   (*BufActionHandler).Redo,
147         "Copy":                   (*BufActionHandler).Copy,
148         "Cut":                    (*BufActionHandler).Cut,
149         "CutLine":                (*BufActionHandler).CutLine,
150         "DuplicateLine":          (*BufActionHandler).DuplicateLine,
151         "DeleteLine":             (*BufActionHandler).DeleteLine,
152         "MoveLinesUp":            (*BufActionHandler).MoveLinesUp,
153         "MoveLinesDown":          (*BufActionHandler).MoveLinesDown,
154         "IndentSelection":        (*BufActionHandler).IndentSelection,
155         "OutdentSelection":       (*BufActionHandler).OutdentSelection,
156         "OutdentLine":            (*BufActionHandler).OutdentLine,
157         "Paste":                  (*BufActionHandler).Paste,
158         "PastePrimary":           (*BufActionHandler).PastePrimary,
159         "SelectAll":              (*BufActionHandler).SelectAll,
160         "OpenFile":               (*BufActionHandler).OpenFile,
161         "Start":                  (*BufActionHandler).Start,
162         "End":                    (*BufActionHandler).End,
163         "PageUp":                 (*BufActionHandler).PageUp,
164         "PageDown":               (*BufActionHandler).PageDown,
165         "SelectPageUp":           (*BufActionHandler).SelectPageUp,
166         "SelectPageDown":         (*BufActionHandler).SelectPageDown,
167         "HalfPageUp":             (*BufActionHandler).HalfPageUp,
168         "HalfPageDown":           (*BufActionHandler).HalfPageDown,
169         "StartOfLine":            (*BufActionHandler).StartOfLine,
170         "EndOfLine":              (*BufActionHandler).EndOfLine,
171         "ToggleHelp":             (*BufActionHandler).ToggleHelp,
172         "ToggleKeyMenu":          (*BufActionHandler).ToggleKeyMenu,
173         "ToggleRuler":            (*BufActionHandler).ToggleRuler,
174         "JumpLine":               (*BufActionHandler).JumpLine,
175         "ClearStatus":            (*BufActionHandler).ClearStatus,
176         "ShellMode":              (*BufActionHandler).ShellMode,
177         "CommandMode":            (*BufActionHandler).CommandMode,
178         "ToggleOverwriteMode":    (*BufActionHandler).ToggleOverwriteMode,
179         "Escape":                 (*BufActionHandler).Escape,
180         "Quit":                   (*BufActionHandler).Quit,
181         "QuitAll":                (*BufActionHandler).QuitAll,
182         "AddTab":                 (*BufActionHandler).AddTab,
183         "PreviousTab":            (*BufActionHandler).PreviousTab,
184         "NextTab":                (*BufActionHandler).NextTab,
185         "NextSplit":              (*BufActionHandler).NextSplit,
186         "PreviousSplit":          (*BufActionHandler).PreviousSplit,
187         "Unsplit":                (*BufActionHandler).Unsplit,
188         "VSplit":                 (*BufActionHandler).VSplitBinding,
189         "HSplit":                 (*BufActionHandler).HSplitBinding,
190         "ToggleMacro":            (*BufActionHandler).ToggleMacro,
191         "PlayMacro":              (*BufActionHandler).PlayMacro,
192         "Suspend":                (*BufActionHandler).Suspend,
193         "ScrollUp":               (*BufActionHandler).ScrollUpAction,
194         "ScrollDown":             (*BufActionHandler).ScrollDownAction,
195         "SpawnMultiCursor":       (*BufActionHandler).SpawnMultiCursor,
196         "SpawnMultiCursorSelect": (*BufActionHandler).SpawnMultiCursorSelect,
197         "RemoveMultiCursor":      (*BufActionHandler).RemoveMultiCursor,
198         "RemoveAllMultiCursors":  (*BufActionHandler).RemoveAllMultiCursors,
199         "SkipMultiCursor":        (*BufActionHandler).SkipMultiCursor,
200         "JumpToMatchingBrace":    (*BufActionHandler).JumpToMatchingBrace,
201
202         // This was changed to InsertNewline but I don't want to break backwards compatibility
203         "InsertEnter": (*BufActionHandler).InsertNewline,
204 }
205 var BufMouseActions = map[string]BufMouseAction{
206         "MousePress":       (*BufActionHandler).MousePress,
207         "MouseMultiCursor": (*BufActionHandler).MouseMultiCursor,
208 }