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/clipboard"
12 "github.com/zyedidia/micro/v2/internal/config"
13 "github.com/zyedidia/micro/v2/internal/display"
14 ulua "github.com/zyedidia/micro/v2/internal/lua"
15 "github.com/zyedidia/micro/v2/internal/screen"
16 "github.com/zyedidia/tcell/v2"
19 type BufKeyAction func(*BufPane) bool
20 type BufMouseAction func(*BufPane, *tcell.EventMouse) bool
22 var BufBindings *KeyTree
24 func BufKeyActionGeneral(a BufKeyAction) PaneKeyAction {
25 return func(p Pane) bool {
26 return a(p.(*BufPane))
30 func BufMouseActionGeneral(a BufMouseAction) PaneMouseAction {
31 return func(p Pane, me *tcell.EventMouse) bool {
32 return a(p.(*BufPane), me)
37 BufBindings = NewKeyTree()
40 func LuaAction(fn string) func(*BufPane) bool {
41 luaFn := strings.Split(fn, ".")
45 plName, plFn := luaFn[0], luaFn[1]
46 pl := config.FindPlugin(plName)
50 return func(h *BufPane) bool {
51 val, err := pl.Call(plFn, luar.New(ulua.L, h))
53 screen.TermMessage(err)
55 if v, ok := val.(lua.LBool); !ok {
63 // BufMapKey maps an event to an action
64 func BufMapEvent(k Event, action string) {
65 config.Bindings["buffer"][k.Name()] = action
67 switch e := k.(type) {
68 case KeyEvent, KeySequenceEvent, RawEvent:
71 bufMapMouse(e, action)
75 func bufMapKey(k Event, action string) {
76 var actionfns []func(*BufPane) bool
84 // TODO: fix problem when complex bindings have these
85 // characters (escape them?)
86 idx := strings.IndexAny(action, "&|,")
90 types = append(types, action[idx])
91 action = action[idx+1:]
93 types = append(types, ' ')
97 var afn func(*BufPane) bool
98 if strings.HasPrefix(a, "command:") {
99 a = strings.SplitN(a, ":", 2)[1]
100 afn = CommandAction(a)
101 names = append(names, "")
102 } else if strings.HasPrefix(a, "command-edit:") {
103 a = strings.SplitN(a, ":", 2)[1]
104 afn = CommandEditAction(a)
105 names = append(names, "")
106 } else if strings.HasPrefix(a, "lua:") {
107 a = strings.SplitN(a, ":", 2)[1]
110 screen.TermMessage("Lua Error:", a, "does not exist")
113 split := strings.SplitN(a, ".", 2)
115 a = strings.Title(split[0]) + strings.Title(split[1])
120 names = append(names, a)
121 } else if f, ok := BufKeyActions[a]; ok {
123 names = append(names, a)
125 screen.TermMessage("Error in bindings: action", a, "does not exist")
128 actionfns = append(actionfns, afn)
130 bufAction := func(h *BufPane) bool {
131 cursors := h.Buf.GetCursors()
133 for i, a := range actionfns {
135 for j, c := range cursors {
139 h.Buf.SetCurCursor(c.Num)
141 if i == 0 || (success && types[i-1] == '&') || (!success && types[i-1] == '|') || (types[i-1] == ',') {
142 innerSuccess = innerSuccess && h.execAction(a, names[i], j)
147 // if the action changed the current pane, update the reference
148 h = MainTab().CurPane()
149 success = innerSuccess
154 BufBindings.RegisterKeyBinding(k, BufKeyActionGeneral(bufAction))
157 // BufMapMouse maps a mouse event to an action
158 func bufMapMouse(k MouseEvent, action string) {
159 if f, ok := BufMouseActions[action]; ok {
160 BufBindings.RegisterMouseBinding(k, BufMouseActionGeneral(f))
163 // delete(BufMouseBindings, k)
168 // BufUnmap unmaps a key or mouse event from any action
169 func BufUnmap(k Event) {
171 // delete(BufKeyBindings, k)
173 // switch e := k.(type) {
175 // delete(BufMouseBindings, e)
179 // The BufPane connects the buffer and the window
180 // It provides a cursor (or multiple) and defines a set of actions
181 // that can be taken on the buffer
182 // The ActionHandler can access the window for necessary info about
183 // visual positions for mouse clicks and scrolling
184 type BufPane struct {
187 // Buf is the buffer this BufPane views
189 // Bindings stores the association of key events and actions
192 // Cursor is the currently active buffer cursor
193 Cursor *buffer.Cursor
195 // Since tcell doesn't differentiate between a mouse release event
196 // and a mouse move event with no keys pressed, we need to keep
197 // track of whether or not the mouse was pressed (or not released) last event to determine
198 // mouse release events
201 // We need to keep track of insert key press toggle
203 // This stores when the last click was
204 // This is useful for detecting double and triple clicks
205 lastClickTime time.Time
208 // lastCutTime stores when the last ctrl+k was issued.
209 // It is used for clearing the clipboard to replace it with fresh cut lines.
210 lastCutTime time.Time
212 // freshClip returns true if the clipboard has never been pasted.
215 // Was the last mouse event actually a double click?
216 // Useful for detecting triple clicks -- if a double click is detected
217 // but the last mouse event was actually a double click, it's a triple click
219 // Same here, just to keep track for mouse move events
222 // Last search stores the last successful search for FindNext and FindPrev
225 // Should the current multiple cursor selection search based on word or
226 // based on selection (false for selection, true for word)
232 // remember original location of a search in case the search is canceled
233 searchOrig buffer.Loc
236 func NewBufPane(buf *buffer.Buffer, win display.BWindow, tab *Tab) *BufPane {
242 h.Cursor = h.Buf.GetActiveCursor()
243 h.mouseReleased = true
245 config.RunPluginFn("onBufPaneOpen", luar.New(ulua.L, h))
250 func NewBufPaneFromBuf(buf *buffer.Buffer, tab *Tab) *BufPane {
251 w := display.NewBufWindow(0, 0, 0, 0, buf)
252 return NewBufPane(buf, w, tab)
255 func (h *BufPane) SetTab(t *Tab) {
259 func (h *BufPane) Tab() *Tab {
263 func (h *BufPane) ResizePane(size int) {
264 n := h.tab.GetNode(h.splitID)
269 // PluginCB calls all plugin callbacks with a certain name and
270 // displays an error if there is one and returns the aggregrate
272 func (h *BufPane) PluginCB(cb string) bool {
273 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h))
275 screen.TermMessage(err)
280 // PluginCBRune is the same as PluginCB but also passes a rune to
282 func (h *BufPane) PluginCBRune(cb string, r rune) bool {
283 b, err := config.RunPluginFnBool(cb, luar.New(ulua.L, h), luar.New(ulua.L, string(r)))
285 screen.TermMessage(err)
290 func (h *BufPane) OpenBuffer(b *buffer.Buffer) {
293 h.BWindow.SetBuffer(b)
294 h.Cursor = b.GetActiveCursor()
295 h.Resize(h.GetView().Width, h.GetView().Height)
297 // Set mouseReleased to true because we assume the mouse is not being pressed when
298 // the editor is opened
299 h.mouseReleased = true
300 // Set isOverwriteMode to false, because we assume we are in the default mode when editor
302 h.isOverwriteMode = false
303 h.lastClickTime = time.Time{}
306 func (h *BufPane) ID() uint64 {
310 func (h *BufPane) SetID(i uint64) {
314 func (h *BufPane) Name() string {
316 if h.Buf.Modified() {
322 // HandleEvent executes the tcell event properly
323 func (h *BufPane) HandleEvent(event tcell.Event) {
324 if h.Buf.ExternallyModified() && !h.Buf.ReloadDisabled {
325 InfoBar.YNPrompt("The file on disk has changed. Reload file? (y,n,esc)", func(yes, canceled bool) {
327 h.Buf.DisableReload()
329 if !yes || canceled {
330 h.Buf.UpdateModTime()
338 switch e := event.(type) {
339 case *tcell.EventRaw:
344 case *tcell.EventPaste:
347 case *tcell.EventKey:
350 mod: metaToAlt(e.Modifiers()),
354 done := h.DoKeyEvent(ke)
355 if !done && e.Key() == tcell.KeyRune {
356 h.DoRuneInsert(e.Rune())
358 case *tcell.EventMouse:
362 _, my := e.Position()
363 if h.Buf.Type.Kind != buffer.BTInfo.Kind && h.Buf.Settings["statusline"].(bool) && my >= h.GetView().Y+h.GetView().Height-1 {
366 case tcell.ButtonNone:
367 // Mouse event with no click
368 if !h.mouseReleased {
369 // Mouse was just released
371 // mx, my := e.Position()
372 // mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my})
374 // we could finish the selection based on the release location as described
375 // below but when the mouse click is within the scroll margin this will
376 // cause a scroll and selection even for a simple mouse click which is
378 // for terminals that don't support mouse motion events, selection via
379 // the mouse won't work but this is ok
381 // Relocating here isn't really necessary because the cursor will
382 // be in the right place from the last mouse event
383 // However, if we are running in a terminal that doesn't support mouse motion
384 // events, this still allows the user to make selections, except only after they
387 // if !h.doubleClick && !h.tripleClick {
388 // h.Cursor.SetSelectionEnd(h.Cursor.Loc)
390 if h.Cursor.HasSelection() {
391 h.Cursor.CopySelection(clipboard.PrimaryReg)
393 h.mouseReleased = true
400 mod: metaToAlt(e.Modifiers()),
402 h.DoMouseEvent(me, e)
408 // Display any gutter messages for this line
409 c := h.Buf.GetActiveCursor()
411 for _, m := range h.Buf.Messages {
412 if c.Y == m.Start.Y || c.Y == m.End.Y {
413 InfoBar.GutterMessage(m.Msg)
418 if none && InfoBar.HasGutter {
419 InfoBar.ClearGutter()
424 func (h *BufPane) Bindings() *KeyTree {
425 if h.bindings != nil {
431 // DoKeyEvent executes a key event by finding the action it is bound
432 // to and executing it (possibly multiple times for multiple cursors)
433 func (h *BufPane) DoKeyEvent(e Event) bool {
434 binds := h.Bindings()
435 action, more := binds.NextEvent(e, nil)
436 if action != nil && !more {
440 } else if action == nil && !more {
446 func (h *BufPane) execAction(action func(*BufPane) bool, name string, cursor int) bool {
447 if name != "Autocomplete" && name != "CycleAutocompleteBack" {
448 h.Buf.HasSuggestions = false
451 _, isMulti := MultiActions[name]
452 if (!isMulti && cursor == 0) || isMulti {
453 if h.PluginCB("pre" + name) {
455 success = success && h.PluginCB("on"+name)
459 if name != "ToggleMacro" && name != "PlayMacro" {
460 curmacro = append(curmacro, action)
472 func (h *BufPane) completeAction(action string) {
473 h.PluginCB("on" + action)
476 func (h *BufPane) HasKeyEvent(e Event) bool {
479 // _, ok := BufKeyBindings[e]
483 // DoMouseEvent executes a mouse event by finding the action it is bound
484 // to and executing it
485 func (h *BufPane) DoMouseEvent(e MouseEvent, te *tcell.EventMouse) bool {
486 binds := h.Bindings()
487 action, _ := binds.NextEvent(e, te)
496 // if action, ok := BufMouseBindings[e]; ok {
497 // if action(h, te) {
501 // } else if h.HasKeyEvent(e) {
502 // return h.DoKeyEvent(e)
507 // DoRuneInsert inserts a given rune into the current buffer
508 // (possibly multiple times for multiple cursors)
509 func (h *BufPane) DoRuneInsert(r rune) {
510 cursors := h.Buf.GetCursors()
511 for _, c := range cursors {
512 // Insert a character
513 h.Buf.SetCurCursor(c.Num)
515 if !h.PluginCBRune("preRune", r) {
518 if c.HasSelection() {
523 if h.isOverwriteMode {
526 h.Buf.Replace(c.Loc, next, string(r))
528 h.Buf.Insert(c.Loc, string(r))
531 curmacro = append(curmacro, r)
534 h.PluginCBRune("onRune", r)
538 func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane {
539 e := NewBufPaneFromBuf(buf, h.tab)
540 e.splitID = MainTab().GetNode(h.splitID).VSplit(right)
541 MainTab().Panes = append(MainTab().Panes, e)
543 MainTab().SetActive(len(MainTab().Panes) - 1)
546 func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane {
547 e := NewBufPaneFromBuf(buf, h.tab)
548 e.splitID = MainTab().GetNode(h.splitID).HSplit(bottom)
549 MainTab().Panes = append(MainTab().Panes, e)
551 MainTab().SetActive(len(MainTab().Panes) - 1)
555 func (h *BufPane) VSplitBuf(buf *buffer.Buffer) *BufPane {
556 return h.VSplitIndex(buf, h.Buf.Settings["splitright"].(bool))
558 func (h *BufPane) HSplitBuf(buf *buffer.Buffer) *BufPane {
559 return h.HSplitIndex(buf, h.Buf.Settings["splitbottom"].(bool))
561 func (h *BufPane) Close() {
565 func (h *BufPane) SetActive(b bool) {
566 h.BWindow.SetActive(b)
568 // Display any gutter messages for this line
569 c := h.Buf.GetActiveCursor()
571 for _, m := range h.Buf.Messages {
572 if c.Y == m.Start.Y || c.Y == m.End.Y {
573 InfoBar.GutterMessage(m.Msg)
578 if none && InfoBar.HasGutter {
579 InfoBar.ClearGutter()
585 // BufKeyActions contains the list of all possible key actions the bufhandler could execute
586 var BufKeyActions = map[string]BufKeyAction{
587 "CursorUp": (*BufPane).CursorUp,
588 "CursorDown": (*BufPane).CursorDown,
589 "CursorPageUp": (*BufPane).CursorPageUp,
590 "CursorPageDown": (*BufPane).CursorPageDown,
591 "CursorLeft": (*BufPane).CursorLeft,
592 "CursorRight": (*BufPane).CursorRight,
593 "CursorStart": (*BufPane).CursorStart,
594 "CursorEnd": (*BufPane).CursorEnd,
595 "SelectToStart": (*BufPane).SelectToStart,
596 "SelectToEnd": (*BufPane).SelectToEnd,
597 "SelectUp": (*BufPane).SelectUp,
598 "SelectDown": (*BufPane).SelectDown,
599 "SelectLeft": (*BufPane).SelectLeft,
600 "SelectRight": (*BufPane).SelectRight,
601 "WordRight": (*BufPane).WordRight,
602 "WordLeft": (*BufPane).WordLeft,
603 "SelectWordRight": (*BufPane).SelectWordRight,
604 "SelectWordLeft": (*BufPane).SelectWordLeft,
605 "DeleteWordRight": (*BufPane).DeleteWordRight,
606 "DeleteWordLeft": (*BufPane).DeleteWordLeft,
607 "SelectLine": (*BufPane).SelectLine,
608 "SelectToStartOfLine": (*BufPane).SelectToStartOfLine,
609 "SelectToStartOfText": (*BufPane).SelectToStartOfText,
610 "SelectToStartOfTextToggle": (*BufPane).SelectToStartOfTextToggle,
611 "SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
612 "ParagraphPrevious": (*BufPane).ParagraphPrevious,
613 "ParagraphNext": (*BufPane).ParagraphNext,
614 "InsertNewline": (*BufPane).InsertNewline,
615 "Backspace": (*BufPane).Backspace,
616 "Delete": (*BufPane).Delete,
617 "InsertTab": (*BufPane).InsertTab,
618 "Save": (*BufPane).Save,
619 "SaveAll": (*BufPane).SaveAll,
620 "SaveAs": (*BufPane).SaveAs,
621 "Find": (*BufPane).Find,
622 "FindLiteral": (*BufPane).FindLiteral,
623 "FindNext": (*BufPane).FindNext,
624 "FindPrevious": (*BufPane).FindPrevious,
625 "Center": (*BufPane).Center,
626 "Undo": (*BufPane).Undo,
627 "Redo": (*BufPane).Redo,
628 "Copy": (*BufPane).Copy,
629 "CopyLine": (*BufPane).CopyLine,
630 "Cut": (*BufPane).Cut,
631 "CutLine": (*BufPane).CutLine,
632 "DuplicateLine": (*BufPane).DuplicateLine,
633 "DeleteLine": (*BufPane).DeleteLine,
634 "MoveLinesUp": (*BufPane).MoveLinesUp,
635 "MoveLinesDown": (*BufPane).MoveLinesDown,
636 "IndentSelection": (*BufPane).IndentSelection,
637 "OutdentSelection": (*BufPane).OutdentSelection,
638 "Autocomplete": (*BufPane).Autocomplete,
639 "CycleAutocompleteBack": (*BufPane).CycleAutocompleteBack,
640 "OutdentLine": (*BufPane).OutdentLine,
641 "IndentLine": (*BufPane).IndentLine,
642 "Paste": (*BufPane).Paste,
643 "PastePrimary": (*BufPane).PastePrimary,
644 "SelectAll": (*BufPane).SelectAll,
645 "OpenFile": (*BufPane).OpenFile,
646 "Start": (*BufPane).Start,
647 "End": (*BufPane).End,
648 "PageUp": (*BufPane).PageUp,
649 "PageDown": (*BufPane).PageDown,
650 "SelectPageUp": (*BufPane).SelectPageUp,
651 "SelectPageDown": (*BufPane).SelectPageDown,
652 "HalfPageUp": (*BufPane).HalfPageUp,
653 "HalfPageDown": (*BufPane).HalfPageDown,
654 "StartOfText": (*BufPane).StartOfText,
655 "StartOfTextToggle": (*BufPane).StartOfTextToggle,
656 "StartOfLine": (*BufPane).StartOfLine,
657 "EndOfLine": (*BufPane).EndOfLine,
658 "ToggleHelp": (*BufPane).ToggleHelp,
659 "ToggleKeyMenu": (*BufPane).ToggleKeyMenu,
660 "ToggleDiffGutter": (*BufPane).ToggleDiffGutter,
661 "ToggleRuler": (*BufPane).ToggleRuler,
662 "ClearStatus": (*BufPane).ClearStatus,
663 "ShellMode": (*BufPane).ShellMode,
664 "CommandMode": (*BufPane).CommandMode,
665 "ToggleOverwriteMode": (*BufPane).ToggleOverwriteMode,
666 "Escape": (*BufPane).Escape,
667 "Quit": (*BufPane).Quit,
668 "QuitAll": (*BufPane).QuitAll,
669 "ForceQuit": (*BufPane).ForceQuit,
670 "AddTab": (*BufPane).AddTab,
671 "PreviousTab": (*BufPane).PreviousTab,
672 "NextTab": (*BufPane).NextTab,
673 "NextSplit": (*BufPane).NextSplit,
674 "PreviousSplit": (*BufPane).PreviousSplit,
675 "Unsplit": (*BufPane).Unsplit,
676 "VSplit": (*BufPane).VSplitAction,
677 "HSplit": (*BufPane).HSplitAction,
678 "ToggleMacro": (*BufPane).ToggleMacro,
679 "PlayMacro": (*BufPane).PlayMacro,
680 "Suspend": (*BufPane).Suspend,
681 "ScrollUp": (*BufPane).ScrollUpAction,
682 "ScrollDown": (*BufPane).ScrollDownAction,
683 "SpawnMultiCursor": (*BufPane).SpawnMultiCursor,
684 "SpawnMultiCursorUp": (*BufPane).SpawnMultiCursorUp,
685 "SpawnMultiCursorDown": (*BufPane).SpawnMultiCursorDown,
686 "SpawnMultiCursorSelect": (*BufPane).SpawnMultiCursorSelect,
687 "RemoveMultiCursor": (*BufPane).RemoveMultiCursor,
688 "RemoveAllMultiCursors": (*BufPane).RemoveAllMultiCursors,
689 "SkipMultiCursor": (*BufPane).SkipMultiCursor,
690 "JumpToMatchingBrace": (*BufPane).JumpToMatchingBrace,
691 "JumpLine": (*BufPane).JumpLine,
692 "Deselect": (*BufPane).Deselect,
693 "ClearInfo": (*BufPane).ClearInfo,
694 "None": (*BufPane).None,
696 // This was changed to InsertNewline but I don't want to break backwards compatibility
697 "InsertEnter": (*BufPane).InsertNewline,
700 // BufMouseActions contains the list of all possible mouse actions the bufhandler could execute
701 var BufMouseActions = map[string]BufMouseAction{
702 "MousePress": (*BufPane).MousePress,
703 "MouseMultiCursor": (*BufPane).MouseMultiCursor,
706 // MultiActions is a list of actions that should be executed multiple
707 // times if there are multiple cursors (one per cursor)
708 // Generally actions that modify global editor state like quitting or
709 // saving should not be included in this list
710 var MultiActions = map[string]bool{
713 "CursorPageUp": true,
714 "CursorPageDown": true,
719 "SelectToStart": true,
727 "SelectWordRight": true,
728 "SelectWordLeft": true,
729 "DeleteWordRight": true,
730 "DeleteWordLeft": true,
732 "SelectToStartOfLine": true,
733 "SelectToStartOfText": true,
734 "SelectToStartOfTextToggle": true,
735 "SelectToEndOfLine": true,
736 "ParagraphPrevious": true,
737 "ParagraphNext": true,
738 "InsertNewline": true,
743 "FindPrevious": true,
748 "DuplicateLine": true,
751 "MoveLinesDown": true,
752 "IndentSelection": true,
753 "OutdentSelection": true,
757 "PastePrimary": true,
758 "SelectPageUp": true,
759 "SelectPageDown": true,
762 "StartOfTextToggle": true,
764 "JumpToMatchingBrace": true,