7 luar "layeh.com/gopher-luar"
9 lua "github.com/yuin/gopher-lua"
10 "github.com/zyedidia/micro/internal/buffer"
11 "github.com/zyedidia/micro/internal/config"
12 "github.com/zyedidia/micro/internal/display"
13 ulua "github.com/zyedidia/micro/internal/lua"
14 "github.com/zyedidia/micro/internal/screen"
15 "github.com/zyedidia/tcell"
18 type BufKeyAction func(*BufPane) bool
19 type BufMouseAction func(*BufPane, *tcell.EventMouse) bool
21 var BufKeyBindings map[Event]BufKeyAction
22 var BufKeyStrings map[Event]string
23 var BufMouseBindings map[MouseEvent]BufMouseAction
26 BufKeyBindings = make(map[Event]BufKeyAction)
27 BufKeyStrings = make(map[Event]string)
28 BufMouseBindings = make(map[MouseEvent]BufMouseAction)
31 func LuaAction(fn string) func(*BufPane) bool {
32 luaFn := strings.Split(fn, ".")
33 plName, plFn := luaFn[0], luaFn[1]
34 pl := config.FindPlugin(plName)
35 return func(h *BufPane) bool {
36 val, err := pl.Call(plFn, luar.New(ulua.L, h))
38 screen.TermMessage(err)
40 if v, ok := val.(lua.LBool); !ok {
48 // BufMapKey maps a key event to an action
49 func BufMapKey(k Event, action string) {
50 if strings.HasPrefix(action, "command:") {
51 action = strings.SplitN(action, ":", 2)[1]
52 BufKeyStrings[k] = action
53 BufKeyBindings[k] = CommandAction(action)
54 } else if strings.HasPrefix(action, "command-edit:") {
55 action = strings.SplitN(action, ":", 2)[1]
56 BufKeyStrings[k] = action
57 BufKeyBindings[k] = CommandEditAction(action)
58 } else if strings.HasPrefix(action, "lua:") {
59 action = strings.SplitN(action, ":", 2)[1]
60 BufKeyStrings[k] = action
61 BufKeyBindings[k] = LuaAction(action)
62 } else if f, ok := BufKeyActions[action]; ok {
63 BufKeyStrings[k] = action
66 screen.TermMessage("Error:", action, "does not exist")
70 // BufMapMouse maps a mouse event to an action
71 func BufMapMouse(k MouseEvent, action string) {
72 if f, ok := BufMouseActions[action]; ok {
73 BufMouseBindings[k] = f
75 delete(BufMouseBindings, k)
80 // The BufPane connects the buffer and the window
81 // It provides a cursor (or multiple) and defines a set of actions
82 // that can be taken on the buffer
83 // The ActionHandler can access the window for necessary info about
84 // visual positions for mouse clicks and scrolling
90 Cursor *buffer.Cursor // the active cursor
92 // Since tcell doesn't differentiate between a mouse release event
93 // and a mouse move event with no keys pressed, we need to keep
94 // track of whether or not the mouse was pressed (or not released) last event to determine
95 // mouse release events
98 // We need to keep track of insert key press toggle
100 // This stores when the last click was
101 // This is useful for detecting double and triple clicks
102 lastClickTime time.Time
105 // lastCutTime stores when the last ctrl+k was issued.
106 // It is used for clearing the clipboard to replace it with fresh cut lines.
107 lastCutTime time.Time
109 // freshClip returns true if the clipboard has never been pasted.
112 // Was the last mouse event actually a double click?
113 // Useful for detecting triple clicks -- if a double click is detected
114 // but the last mouse event was actually a double click, it's a triple click
116 // Same here, just to keep track for mouse move events
119 // Last search stores the last successful search for FindNext and FindPrev
121 // Should the current multiple cursor selection search based on word or
122 // based on selection (false for selection, true for word)
127 // remember original location of a search in case the search is canceled
128 searchOrig buffer.Loc
131 func NewBufPane(buf *buffer.Buffer, win display.BWindow) *BufPane {
136 h.Cursor = h.Buf.GetActiveCursor()
137 h.mouseReleased = true
139 config.RunPluginFn("onBufPaneOpen", luar.New(ulua.L, h))
144 func NewBufPaneFromBuf(buf *buffer.Buffer) *BufPane {
145 w := display.NewBufWindow(0, 0, 0, 0, buf)
146 return NewBufPane(buf, w)
149 // PluginCB calls all plugin callbacks with a certain name and
150 // displays an error if there is one and returns the aggregrate
152 func (h *BufPane) PluginCB(cb string) bool {
153 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h))
155 screen.TermMessage(err)
160 // PluginCBRune is the same as PluginCB but also passes a rune to
162 func (h *BufPane) PluginCBRune(cb string, r rune) bool {
163 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h), luar.New(ulua.L, string(r)))
165 screen.TermMessage(err)
170 func (h *BufPane) OpenBuffer(b *buffer.Buffer) {
173 h.BWindow.SetBuffer(b)
174 h.Cursor = b.GetActiveCursor()
175 h.Resize(h.GetView().Width, h.GetView().Height)
176 v := new(display.View)
179 // Set mouseReleased to true because we assume the mouse is not being pressed when
180 // the editor is opened
181 h.mouseReleased = true
182 // Set isOverwriteMode to false, because we assume we are in the default mode when editor
184 h.isOverwriteMode = false
185 h.lastClickTime = time.Time{}
188 func (h *BufPane) ID() uint64 {
192 func (h *BufPane) SetID(i uint64) {
196 func (h *BufPane) Name() string {
197 return h.Buf.GetName()
200 // HandleEvent executes the tcell event properly
201 // TODO: multiple actions bound to one key
202 func (h *BufPane) HandleEvent(event tcell.Event) {
203 switch e := event.(type) {
204 case *tcell.EventRaw:
209 case *tcell.EventKey:
216 done := h.DoKeyEvent(ke)
217 if !done && e.Key() == tcell.KeyRune {
218 h.DoRuneInsert(e.Rune())
220 case *tcell.EventMouse:
222 case tcell.ButtonNone:
223 // Mouse event with no click
224 if !h.mouseReleased {
225 // Mouse was just released
227 mx, my := e.Position()
228 mouseLoc := h.GetMouseLoc(buffer.Loc{X: mx, Y: my})
230 // Relocating here isn't really necessary because the cursor will
231 // be in the right place from the last mouse event
232 // However, if we are running in a terminal that doesn't support mouse motion
233 // events, this still allows the user to make selections, except only after they
236 if !h.doubleClick && !h.tripleClick {
237 h.Cursor.Loc = mouseLoc
238 h.Cursor.SetSelectionEnd(h.Cursor.Loc)
239 h.Cursor.CopySelection("primary")
241 h.mouseReleased = true
249 h.DoMouseEvent(me, e)
254 // Display any gutter messages for this line
255 c := h.Buf.GetActiveCursor()
257 for _, m := range h.Buf.Messages {
258 if c.Y == m.Start.Y || c.Y == m.End.Y {
259 InfoBar.GutterMessage(m.Msg)
264 if none && InfoBar.HasGutter {
265 InfoBar.ClearGutter()
270 // DoKeyEvent executes a key event by finding the action it is bound
271 // to and executing it (possibly multiple times for multiple cursors)
272 func (h *BufPane) DoKeyEvent(e Event) bool {
273 if action, ok := BufKeyBindings[e]; ok {
274 estr := BufKeyStrings[e]
275 if estr != "InsertTab" {
276 h.Buf.HasSuggestions = false
278 for _, s := range MultiActions {
280 cursors := h.Buf.GetCursors()
281 for _, c := range cursors {
282 h.Buf.SetCurCursor(c.Num)
284 if !h.PluginCB("pre" + estr) {
285 // canceled by plugin
289 if h.PluginCB("on"+estr) && rel {
294 if estr != "ToggleMacro" && estr != "PlayMacro" {
295 curmacro = append(curmacro, e)
302 if !h.PluginCB("pre" + estr) {
306 if h.PluginCB("on"+estr) && rel {
314 func (h *BufPane) HasKeyEvent(e Event) bool {
315 _, ok := BufKeyBindings[e]
319 // DoMouseEvent executes a mouse event by finding the action it is bound
320 // to and executing it
321 func (h *BufPane) DoMouseEvent(e MouseEvent, te *tcell.EventMouse) bool {
322 if action, ok := BufMouseBindings[e]; ok {
327 } else if h.HasKeyEvent(e) {
328 return h.DoKeyEvent(e)
333 // DoRuneInsert inserts a given rune into the current buffer
334 // (possibly multiple times for multiple cursors)
335 func (h *BufPane) DoRuneInsert(r rune) {
336 cursors := h.Buf.GetCursors()
337 for _, c := range cursors {
338 // Insert a character
339 h.Buf.SetCurCursor(c.Num)
341 if !h.PluginCBRune("preRune", r) {
344 if c.HasSelection() {
349 if h.isOverwriteMode {
352 h.Buf.Replace(c.Loc, next, string(r))
354 h.Buf.Insert(c.Loc, string(r))
357 curmacro = append(curmacro, r)
359 h.PluginCBRune("onRune", r)
363 func (h *BufPane) VSplitBuf(buf *buffer.Buffer) *BufPane {
364 e := NewBufPaneFromBuf(buf)
365 e.splitID = MainTab().GetNode(h.splitID).VSplit(h.Buf.Settings["splitright"].(bool))
366 MainTab().Panes = append(MainTab().Panes, e)
368 MainTab().SetActive(len(MainTab().Panes) - 1)
371 func (h *BufPane) HSplitBuf(buf *buffer.Buffer) *BufPane {
372 e := NewBufPaneFromBuf(buf)
373 e.splitID = MainTab().GetNode(h.splitID).HSplit(h.Buf.Settings["splitbottom"].(bool))
374 MainTab().Panes = append(MainTab().Panes, e)
376 MainTab().SetActive(len(MainTab().Panes) - 1)
379 func (h *BufPane) Close() {
383 func (h *BufPane) SetActive(b bool) {
384 h.BWindow.SetActive(b)
386 // Display any gutter messages for this line
387 c := h.Buf.GetActiveCursor()
389 for _, m := range h.Buf.Messages {
390 if c.Y == m.Start.Y || c.Y == m.End.Y {
391 InfoBar.GutterMessage(m.Msg)
396 if none && InfoBar.HasGutter {
397 InfoBar.ClearGutter()
403 // BufKeyActions contains the list of all possible key actions the bufhandler could execute
404 var BufKeyActions = map[string]BufKeyAction{
405 "CursorUp": (*BufPane).CursorUp,
406 "CursorDown": (*BufPane).CursorDown,
407 "CursorPageUp": (*BufPane).CursorPageUp,
408 "CursorPageDown": (*BufPane).CursorPageDown,
409 "CursorLeft": (*BufPane).CursorLeft,
410 "CursorRight": (*BufPane).CursorRight,
411 "CursorStart": (*BufPane).CursorStart,
412 "CursorEnd": (*BufPane).CursorEnd,
413 "SelectToStart": (*BufPane).SelectToStart,
414 "SelectToEnd": (*BufPane).SelectToEnd,
415 "SelectUp": (*BufPane).SelectUp,
416 "SelectDown": (*BufPane).SelectDown,
417 "SelectLeft": (*BufPane).SelectLeft,
418 "SelectRight": (*BufPane).SelectRight,
419 "WordRight": (*BufPane).WordRight,
420 "WordLeft": (*BufPane).WordLeft,
421 "SelectWordRight": (*BufPane).SelectWordRight,
422 "SelectWordLeft": (*BufPane).SelectWordLeft,
423 "DeleteWordRight": (*BufPane).DeleteWordRight,
424 "DeleteWordLeft": (*BufPane).DeleteWordLeft,
425 "SelectLine": (*BufPane).SelectLine,
426 "SelectToStartOfLine": (*BufPane).SelectToStartOfLine,
427 "SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
428 "ParagraphPrevious": (*BufPane).ParagraphPrevious,
429 "ParagraphNext": (*BufPane).ParagraphNext,
430 "InsertNewline": (*BufPane).InsertNewline,
431 "Backspace": (*BufPane).Backspace,
432 "Delete": (*BufPane).Delete,
433 "InsertTab": (*BufPane).InsertTab,
434 "Save": (*BufPane).Save,
435 "SaveAll": (*BufPane).SaveAll,
436 "SaveAs": (*BufPane).SaveAs,
437 "Find": (*BufPane).Find,
438 "FindNext": (*BufPane).FindNext,
439 "FindPrevious": (*BufPane).FindPrevious,
440 "Center": (*BufPane).Center,
441 "Undo": (*BufPane).Undo,
442 "Redo": (*BufPane).Redo,
443 "Copy": (*BufPane).Copy,
444 "Cut": (*BufPane).Cut,
445 "CutLine": (*BufPane).CutLine,
446 "DuplicateLine": (*BufPane).DuplicateLine,
447 "DeleteLine": (*BufPane).DeleteLine,
448 "MoveLinesUp": (*BufPane).MoveLinesUp,
449 "MoveLinesDown": (*BufPane).MoveLinesDown,
450 "IndentSelection": (*BufPane).IndentSelection,
451 "OutdentSelection": (*BufPane).OutdentSelection,
452 "OutdentLine": (*BufPane).OutdentLine,
453 "Paste": (*BufPane).Paste,
454 "PastePrimary": (*BufPane).PastePrimary,
455 "SelectAll": (*BufPane).SelectAll,
456 "OpenFile": (*BufPane).OpenFile,
457 "Start": (*BufPane).Start,
458 "End": (*BufPane).End,
459 "PageUp": (*BufPane).PageUp,
460 "PageDown": (*BufPane).PageDown,
461 "SelectPageUp": (*BufPane).SelectPageUp,
462 "SelectPageDown": (*BufPane).SelectPageDown,
463 "HalfPageUp": (*BufPane).HalfPageUp,
464 "HalfPageDown": (*BufPane).HalfPageDown,
465 "StartOfLine": (*BufPane).StartOfLine,
466 "EndOfLine": (*BufPane).EndOfLine,
467 "ToggleHelp": (*BufPane).ToggleHelp,
468 "ToggleKeyMenu": (*BufPane).ToggleKeyMenu,
469 "ToggleRuler": (*BufPane).ToggleRuler,
470 "ClearStatus": (*BufPane).ClearStatus,
471 "ShellMode": (*BufPane).ShellMode,
472 "CommandMode": (*BufPane).CommandMode,
473 "ToggleOverwriteMode": (*BufPane).ToggleOverwriteMode,
474 "Escape": (*BufPane).Escape,
475 "Quit": (*BufPane).Quit,
476 "QuitAll": (*BufPane).QuitAll,
477 "AddTab": (*BufPane).AddTab,
478 "PreviousTab": (*BufPane).PreviousTab,
479 "NextTab": (*BufPane).NextTab,
480 "NextSplit": (*BufPane).NextSplit,
481 "PreviousSplit": (*BufPane).PreviousSplit,
482 "Unsplit": (*BufPane).Unsplit,
483 "VSplit": (*BufPane).VSplitAction,
484 "HSplit": (*BufPane).HSplitAction,
485 "ToggleMacro": (*BufPane).ToggleMacro,
486 "PlayMacro": (*BufPane).PlayMacro,
487 "Suspend": (*BufPane).Suspend,
488 "ScrollUp": (*BufPane).ScrollUpAction,
489 "ScrollDown": (*BufPane).ScrollDownAction,
490 "SpawnMultiCursor": (*BufPane).SpawnMultiCursor,
491 "SpawnMultiCursorSelect": (*BufPane).SpawnMultiCursorSelect,
492 "RemoveMultiCursor": (*BufPane).RemoveMultiCursor,
493 "RemoveAllMultiCursors": (*BufPane).RemoveAllMultiCursors,
494 "SkipMultiCursor": (*BufPane).SkipMultiCursor,
495 "JumpToMatchingBrace": (*BufPane).JumpToMatchingBrace,
497 // This was changed to InsertNewline but I don't want to break backwards compatibility
498 "InsertEnter": (*BufPane).InsertNewline,
501 // BufMouseActions contains the list of all possible mouse actions the bufhandler could execute
502 var BufMouseActions = map[string]BufMouseAction{
503 "MousePress": (*BufPane).MousePress,
504 "MouseMultiCursor": (*BufPane).MouseMultiCursor,
507 // MultiActions is a list of actions that should be executed multiple
508 // times if there are multiple cursors (one per cursor)
509 // Generally actions that modify global editor state like quitting or
510 // saving should not be included in this list
511 var MultiActions = []string{
533 "SelectToStartOfLine",
558 "JumpToMatchingBrace",