7 luar "layeh.com/gopher-luar"
9 lua "github.com/yuin/gopher-lua"
10 "github.com/zyedidia/micro/v2/internal/buffer"
11 "github.com/zyedidia/micro/v2/internal/config"
12 "github.com/zyedidia/micro/v2/internal/display"
13 ulua "github.com/zyedidia/micro/v2/internal/lua"
14 "github.com/zyedidia/micro/v2/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, ".")
36 plName, plFn := luaFn[0], luaFn[1]
37 pl := config.FindPlugin(plName)
41 return func(h *BufPane) bool {
42 val, err := pl.Call(plFn, luar.New(ulua.L, h))
44 screen.TermMessage(err)
46 if v, ok := val.(lua.LBool); !ok {
54 // BufMapKey maps a key event to an action
55 func BufMapKey(k Event, action string) {
56 BufKeyStrings[k] = action
57 var actionfns []func(*BufPane) bool
65 // TODO: fix problem when complex bindings have these
66 // characters (escape them?)
67 idx := strings.IndexAny(action, "&|,")
71 types = append(types, action[idx])
72 action = action[idx+1:]
74 types = append(types, ' ')
78 var afn func(*BufPane) bool
79 if strings.HasPrefix(a, "command:") {
80 a = strings.SplitN(a, ":", 2)[1]
81 afn = CommandAction(a)
82 names = append(names, "")
83 } else if strings.HasPrefix(a, "command-edit:") {
84 a = strings.SplitN(a, ":", 2)[1]
85 afn = CommandEditAction(a)
86 names = append(names, "")
87 } else if strings.HasPrefix(a, "lua:") {
88 a = strings.SplitN(a, ":", 2)[1]
91 screen.TermMessage("Lua Error:", a, "does not exist")
94 split := strings.SplitN(a, ".", 2)
96 a = strings.Title(split[0]) + strings.Title(split[1])
101 names = append(names, a)
102 } else if f, ok := BufKeyActions[a]; ok {
104 names = append(names, a)
106 screen.TermMessage("Error in bindings: action", a, "does not exist")
109 actionfns = append(actionfns, afn)
111 BufKeyBindings[k] = func(h *BufPane) bool {
112 cursors := h.Buf.GetCursors()
114 for i, a := range actionfns {
115 for j, c := range cursors {
119 h.Buf.SetCurCursor(c.Num)
121 if i == 0 || (success && types[i-1] == '&') || (!success && types[i-1] == '|') || (types[i-1] == ',') {
122 success = h.execAction(a, names[i], j)
132 // BufMapMouse maps a mouse event to an action
133 func BufMapMouse(k MouseEvent, action string) {
134 if f, ok := BufMouseActions[action]; ok {
135 BufMouseBindings[k] = f
137 delete(BufMouseBindings, k)
142 // BufUnmap unmaps a key or mouse event from any action
143 func BufUnmap(k Event) {
144 delete(BufKeyBindings, k)
145 delete(BufKeyStrings, k)
147 switch e := k.(type) {
149 delete(BufMouseBindings, e)
153 // The BufPane connects the buffer and the window
154 // It provides a cursor (or multiple) and defines a set of actions
155 // that can be taken on the buffer
156 // The ActionHandler can access the window for necessary info about
157 // visual positions for mouse clicks and scrolling
158 type BufPane struct {
163 Cursor *buffer.Cursor // the active cursor
165 // Since tcell doesn't differentiate between a mouse release event
166 // and a mouse move event with no keys pressed, we need to keep
167 // track of whether or not the mouse was pressed (or not released) last event to determine
168 // mouse release events
171 // We need to keep track of insert key press toggle
173 // This stores when the last click was
174 // This is useful for detecting double and triple clicks
175 lastClickTime time.Time
178 // lastCutTime stores when the last ctrl+k was issued.
179 // It is used for clearing the clipboard to replace it with fresh cut lines.
180 lastCutTime time.Time
182 // freshClip returns true if the clipboard has never been pasted.
185 // Was the last mouse event actually a double click?
186 // Useful for detecting triple clicks -- if a double click is detected
187 // but the last mouse event was actually a double click, it's a triple click
189 // Same here, just to keep track for mouse move events
192 // Last search stores the last successful search for FindNext and FindPrev
195 // Should the current multiple cursor selection search based on word or
196 // based on selection (false for selection, true for word)
202 // remember original location of a search in case the search is canceled
203 searchOrig buffer.Loc
206 func NewBufPane(buf *buffer.Buffer, win display.BWindow, tab *Tab) *BufPane {
212 h.Cursor = h.Buf.GetActiveCursor()
213 h.mouseReleased = true
215 config.RunPluginFn("onBufPaneOpen", luar.New(ulua.L, h))
220 func NewBufPaneFromBuf(buf *buffer.Buffer, tab *Tab) *BufPane {
221 w := display.NewBufWindow(0, 0, 0, 0, buf)
222 return NewBufPane(buf, w, tab)
225 func (h *BufPane) SetTab(t *Tab) {
229 func (h *BufPane) Tab() *Tab {
233 func (h *BufPane) ResizePane(size int) {
234 n := h.tab.GetNode(h.splitID)
239 // PluginCB calls all plugin callbacks with a certain name and
240 // displays an error if there is one and returns the aggregrate
242 func (h *BufPane) PluginCB(cb string) bool {
243 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h))
245 screen.TermMessage(err)
250 // PluginCBRune is the same as PluginCB but also passes a rune to
252 func (h *BufPane) PluginCBRune(cb string, r rune) bool {
253 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h), luar.New(ulua.L, string(r)))
255 screen.TermMessage(err)
260 func (h *BufPane) OpenBuffer(b *buffer.Buffer) {
263 h.BWindow.SetBuffer(b)
264 h.Cursor = b.GetActiveCursor()
265 h.Resize(h.GetView().Width, h.GetView().Height)
267 // Set mouseReleased to true because we assume the mouse is not being pressed when
268 // the editor is opened
269 h.mouseReleased = true
270 // Set isOverwriteMode to false, because we assume we are in the default mode when editor
272 h.isOverwriteMode = false
273 h.lastClickTime = time.Time{}
276 func (h *BufPane) ID() uint64 {
280 func (h *BufPane) SetID(i uint64) {
284 func (h *BufPane) Name() string {
286 if h.Buf.Modified() {
292 // HandleEvent executes the tcell event properly
293 func (h *BufPane) HandleEvent(event tcell.Event) {
294 if h.Buf.ExternallyModified() && !h.Buf.ReloadDisabled {
295 InfoBar.YNPrompt("The file on disk has changed. Reload file? (y,n,esc)", func(yes, canceled bool) {
297 h.Buf.DisableReload()
299 if !yes || canceled {
300 h.Buf.UpdateModTime()
308 switch e := event.(type) {
309 case *tcell.EventRaw:
314 case *tcell.EventPaste:
317 case *tcell.EventKey:
324 done := h.DoKeyEvent(ke)
325 if !done && e.Key() == tcell.KeyRune {
326 h.DoRuneInsert(e.Rune())
328 case *tcell.EventMouse:
332 _, my := e.Position()
333 if h.Buf.Settings["statusline"].(bool) && my >= h.GetView().Y+h.GetView().Height-1 {
336 case tcell.ButtonNone:
337 // Mouse event with no click
338 if !h.mouseReleased {
339 // Mouse was just released
341 // mx, my := e.Position()
342 // mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my})
344 // we could finish the selection based on the release location as described
345 // below but when the mouse click is within the scroll margin this will
346 // cause a scroll and selection even for a simple mouse click which is
348 // for terminals that don't support mouse motion events, selection via
349 // the mouse won't work but this is ok
351 // Relocating here isn't really necessary because the cursor will
352 // be in the right place from the last mouse event
353 // However, if we are running in a terminal that doesn't support mouse motion
354 // events, this still allows the user to make selections, except only after they
357 // if !h.doubleClick && !h.tripleClick {
358 // h.Cursor.SetSelectionEnd(h.Cursor.Loc)
360 if h.Cursor.HasSelection() {
361 h.Cursor.CopySelection("primary")
363 h.mouseReleased = true
372 h.DoMouseEvent(me, e)
378 // Display any gutter messages for this line
379 c := h.Buf.GetActiveCursor()
381 for _, m := range h.Buf.Messages {
382 if c.Y == m.Start.Y || c.Y == m.End.Y {
383 InfoBar.GutterMessage(m.Msg)
388 if none && InfoBar.HasGutter {
389 InfoBar.ClearGutter()
394 // DoKeyEvent executes a key event by finding the action it is bound
395 // to and executing it (possibly multiple times for multiple cursors)
396 func (h *BufPane) DoKeyEvent(e Event) bool {
397 if action, ok := BufKeyBindings[e]; ok {
403 func (h *BufPane) execAction(action func(*BufPane) bool, name string, cursor int) bool {
404 if name != "Autocomplete" && name != "CycleAutocompleteBack" {
405 h.Buf.HasSuggestions = false
408 _, isMulti := MultiActions[name]
409 if (!isMulti && cursor == 0) || isMulti {
410 if h.PluginCB("pre" + name) {
412 success = success && h.PluginCB("on"+name)
416 if name != "ToggleMacro" && name != "PlayMacro" {
417 curmacro = append(curmacro, action)
429 func (h *BufPane) completeAction(action string) {
430 h.PluginCB("on" + action)
433 func (h *BufPane) HasKeyEvent(e Event) bool {
434 _, ok := BufKeyBindings[e]
438 // DoMouseEvent executes a mouse event by finding the action it is bound
439 // to and executing it
440 func (h *BufPane) DoMouseEvent(e MouseEvent, te *tcell.EventMouse) bool {
441 if action, ok := BufMouseBindings[e]; ok {
446 } else if h.HasKeyEvent(e) {
447 return h.DoKeyEvent(e)
452 // DoRuneInsert inserts a given rune into the current buffer
453 // (possibly multiple times for multiple cursors)
454 func (h *BufPane) DoRuneInsert(r rune) {
455 cursors := h.Buf.GetCursors()
456 for _, c := range cursors {
457 // Insert a character
458 h.Buf.SetCurCursor(c.Num)
460 if !h.PluginCBRune("preRune", r) {
463 if c.HasSelection() {
468 if h.isOverwriteMode {
471 h.Buf.Replace(c.Loc, next, string(r))
473 h.Buf.Insert(c.Loc, string(r))
476 curmacro = append(curmacro, r)
479 h.PluginCBRune("onRune", r)
483 func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane {
484 e := NewBufPaneFromBuf(buf, h.tab)
485 e.splitID = MainTab().GetNode(h.splitID).VSplit(right)
486 MainTab().Panes = append(MainTab().Panes, e)
488 MainTab().SetActive(len(MainTab().Panes) - 1)
491 func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane {
492 e := NewBufPaneFromBuf(buf, h.tab)
493 e.splitID = MainTab().GetNode(h.splitID).HSplit(bottom)
494 MainTab().Panes = append(MainTab().Panes, e)
496 MainTab().SetActive(len(MainTab().Panes) - 1)
500 func (h *BufPane) VSplitBuf(buf *buffer.Buffer) *BufPane {
501 return h.VSplitIndex(buf, h.Buf.Settings["splitright"].(bool))
503 func (h *BufPane) HSplitBuf(buf *buffer.Buffer) *BufPane {
504 return h.HSplitIndex(buf, h.Buf.Settings["splitbottom"].(bool))
506 func (h *BufPane) Close() {
510 func (h *BufPane) SetActive(b bool) {
511 h.BWindow.SetActive(b)
513 // Display any gutter messages for this line
514 c := h.Buf.GetActiveCursor()
516 for _, m := range h.Buf.Messages {
517 if c.Y == m.Start.Y || c.Y == m.End.Y {
518 InfoBar.GutterMessage(m.Msg)
523 if none && InfoBar.HasGutter {
524 InfoBar.ClearGutter()
530 // BufKeyActions contains the list of all possible key actions the bufhandler could execute
531 var BufKeyActions = map[string]BufKeyAction{
532 "CursorUp": (*BufPane).CursorUp,
533 "CursorDown": (*BufPane).CursorDown,
534 "CursorPageUp": (*BufPane).CursorPageUp,
535 "CursorPageDown": (*BufPane).CursorPageDown,
536 "CursorLeft": (*BufPane).CursorLeft,
537 "CursorRight": (*BufPane).CursorRight,
538 "CursorStart": (*BufPane).CursorStart,
539 "CursorEnd": (*BufPane).CursorEnd,
540 "SelectToStart": (*BufPane).SelectToStart,
541 "SelectToEnd": (*BufPane).SelectToEnd,
542 "SelectUp": (*BufPane).SelectUp,
543 "SelectDown": (*BufPane).SelectDown,
544 "SelectLeft": (*BufPane).SelectLeft,
545 "SelectRight": (*BufPane).SelectRight,
546 "WordRight": (*BufPane).WordRight,
547 "WordLeft": (*BufPane).WordLeft,
548 "SelectWordRight": (*BufPane).SelectWordRight,
549 "SelectWordLeft": (*BufPane).SelectWordLeft,
550 "DeleteWordRight": (*BufPane).DeleteWordRight,
551 "DeleteWordLeft": (*BufPane).DeleteWordLeft,
552 "SelectLine": (*BufPane).SelectLine,
553 "SelectToStartOfLine": (*BufPane).SelectToStartOfLine,
554 "SelectToStartOfText": (*BufPane).SelectToStartOfText,
555 "SelectToStartOfTextToggle": (*BufPane).SelectToStartOfTextToggle,
556 "SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
557 "ParagraphPrevious": (*BufPane).ParagraphPrevious,
558 "ParagraphNext": (*BufPane).ParagraphNext,
559 "InsertNewline": (*BufPane).InsertNewline,
560 "Backspace": (*BufPane).Backspace,
561 "Delete": (*BufPane).Delete,
562 "InsertTab": (*BufPane).InsertTab,
563 "Save": (*BufPane).Save,
564 "SaveAll": (*BufPane).SaveAll,
565 "SaveAs": (*BufPane).SaveAs,
566 "Find": (*BufPane).Find,
567 "FindLiteral": (*BufPane).FindLiteral,
568 "FindNext": (*BufPane).FindNext,
569 "FindPrevious": (*BufPane).FindPrevious,
570 "Center": (*BufPane).Center,
571 "Undo": (*BufPane).Undo,
572 "Redo": (*BufPane).Redo,
573 "Copy": (*BufPane).Copy,
574 "CopyLine": (*BufPane).CopyLine,
575 "Cut": (*BufPane).Cut,
576 "CutLine": (*BufPane).CutLine,
577 "DuplicateLine": (*BufPane).DuplicateLine,
578 "DeleteLine": (*BufPane).DeleteLine,
579 "MoveLinesUp": (*BufPane).MoveLinesUp,
580 "MoveLinesDown": (*BufPane).MoveLinesDown,
581 "IndentSelection": (*BufPane).IndentSelection,
582 "OutdentSelection": (*BufPane).OutdentSelection,
583 "Autocomplete": (*BufPane).Autocomplete,
584 "CycleAutocompleteBack": (*BufPane).CycleAutocompleteBack,
585 "OutdentLine": (*BufPane).OutdentLine,
586 "IndentLine": (*BufPane).IndentLine,
587 "Paste": (*BufPane).Paste,
588 "PastePrimary": (*BufPane).PastePrimary,
589 "SelectAll": (*BufPane).SelectAll,
590 "OpenFile": (*BufPane).OpenFile,
591 "Start": (*BufPane).Start,
592 "End": (*BufPane).End,
593 "PageUp": (*BufPane).PageUp,
594 "PageDown": (*BufPane).PageDown,
595 "SelectPageUp": (*BufPane).SelectPageUp,
596 "SelectPageDown": (*BufPane).SelectPageDown,
597 "HalfPageUp": (*BufPane).HalfPageUp,
598 "HalfPageDown": (*BufPane).HalfPageDown,
599 "StartOfText": (*BufPane).StartOfText,
600 "StartOfTextToggle": (*BufPane).StartOfTextToggle,
601 "StartOfLine": (*BufPane).StartOfLine,
602 "EndOfLine": (*BufPane).EndOfLine,
603 "ToggleHelp": (*BufPane).ToggleHelp,
604 "ToggleKeyMenu": (*BufPane).ToggleKeyMenu,
605 "ToggleDiffGutter": (*BufPane).ToggleDiffGutter,
606 "ToggleRuler": (*BufPane).ToggleRuler,
607 "ClearStatus": (*BufPane).ClearStatus,
608 "ShellMode": (*BufPane).ShellMode,
609 "CommandMode": (*BufPane).CommandMode,
610 "ToggleOverwriteMode": (*BufPane).ToggleOverwriteMode,
611 "Escape": (*BufPane).Escape,
612 "Quit": (*BufPane).Quit,
613 "QuitAll": (*BufPane).QuitAll,
614 "AddTab": (*BufPane).AddTab,
615 "PreviousTab": (*BufPane).PreviousTab,
616 "NextTab": (*BufPane).NextTab,
617 "NextSplit": (*BufPane).NextSplit,
618 "PreviousSplit": (*BufPane).PreviousSplit,
619 "Unsplit": (*BufPane).Unsplit,
620 "VSplit": (*BufPane).VSplitAction,
621 "HSplit": (*BufPane).HSplitAction,
622 "ToggleMacro": (*BufPane).ToggleMacro,
623 "PlayMacro": (*BufPane).PlayMacro,
624 "Suspend": (*BufPane).Suspend,
625 "ScrollUp": (*BufPane).ScrollUpAction,
626 "ScrollDown": (*BufPane).ScrollDownAction,
627 "SpawnMultiCursor": (*BufPane).SpawnMultiCursor,
628 "SpawnMultiCursorUp": (*BufPane).SpawnMultiCursorUp,
629 "SpawnMultiCursorDown": (*BufPane).SpawnMultiCursorDown,
630 "SpawnMultiCursorSelect": (*BufPane).SpawnMultiCursorSelect,
631 "RemoveMultiCursor": (*BufPane).RemoveMultiCursor,
632 "RemoveAllMultiCursors": (*BufPane).RemoveAllMultiCursors,
633 "SkipMultiCursor": (*BufPane).SkipMultiCursor,
634 "JumpToMatchingBrace": (*BufPane).JumpToMatchingBrace,
635 "JumpLine": (*BufPane).JumpLine,
636 "None": (*BufPane).None,
638 // This was changed to InsertNewline but I don't want to break backwards compatibility
639 "InsertEnter": (*BufPane).InsertNewline,
642 // BufMouseActions contains the list of all possible mouse actions the bufhandler could execute
643 var BufMouseActions = map[string]BufMouseAction{
644 "MousePress": (*BufPane).MousePress,
645 "MouseMultiCursor": (*BufPane).MouseMultiCursor,
648 // MultiActions is a list of actions that should be executed multiple
649 // times if there are multiple cursors (one per cursor)
650 // Generally actions that modify global editor state like quitting or
651 // saving should not be included in this list
652 var MultiActions = map[string]bool{
655 "CursorPageUp": true,
656 "CursorPageDown": true,
661 "SelectToStart": true,
669 "SelectWordRight": true,
670 "SelectWordLeft": true,
671 "DeleteWordRight": true,
672 "DeleteWordLeft": true,
674 "SelectToStartOfLine": true,
675 "SelectToStartOfText": true,
676 "SelectToStartOfTextToggle": true,
677 "SelectToEndOfLine": true,
678 "ParagraphPrevious": true,
679 "ParagraphNext": true,
680 "InsertNewline": true,
685 "FindPrevious": true,
689 "DuplicateLine": true,
692 "MoveLinesDown": true,
693 "IndentSelection": true,
694 "OutdentSelection": true,
698 "PastePrimary": true,
699 "SelectPageUp": true,
700 "SelectPageDown": true,
703 "StartOfTextToggle": true,
705 "JumpToMatchingBrace": true,