| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"time" |
| 6 |
|
| 7 |
luar "layeh.com/gopher-luar" |
| 8 |
|
| 9 |
"github.com/micro-editor/tcell/v2" |
| 10 |
lua "github.com/yuin/gopher-lua" |
| 11 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 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/micro/v2/internal/util" |
| 17 |
) |
| 18 |
|
| 19 |
type BufAction any |
| 20 |
|
| 21 |
// BufKeyAction represents an action bound to a key. |
| 22 |
type BufKeyAction func(*BufPane) bool |
| 23 |
|
| 24 |
// BufMouseAction is an action that must be bound to a mouse event. |
| 25 |
type BufMouseAction func(*BufPane, *tcell.EventMouse) bool |
| 26 |
|
| 27 |
// BufBindings stores the bindings for the buffer pane type. |
| 28 |
var BufBindings *KeyTree |
| 29 |
|
| 30 |
// BufKeyActionGeneral makes a general pane action from a BufKeyAction. |
| 31 |
func BufKeyActionGeneral(a BufKeyAction) PaneKeyAction { |
| 32 |
return func(p Pane) bool { |
| 33 |
return a(p.(*BufPane)) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// BufMouseActionGeneral makes a general pane mouse action from a BufKeyAction. |
| 38 |
func BufMouseActionGeneral(a BufMouseAction) PaneMouseAction { |
| 39 |
return func(p Pane, me *tcell.EventMouse) bool { |
| 40 |
return a(p.(*BufPane), me) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func init() { |
| 45 |
BufBindings = NewKeyTree() |
| 46 |
} |
| 47 |
|
| 48 |
// LuaAction makes an action from a lua function. It returns either a BufKeyAction |
| 49 |
// or a BufMouseAction depending on the event type. |
| 50 |
func LuaAction(fn string, k Event) BufAction { |
| 51 |
luaFn := strings.Split(fn, ".") |
| 52 |
if len(luaFn) <= 1 { |
| 53 |
return nil |
| 54 |
} |
| 55 |
plName, plFn := luaFn[0], luaFn[1] |
| 56 |
pl := config.FindPlugin(plName) |
| 57 |
if pl == nil { |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
var action BufAction |
| 62 |
switch k.(type) { |
| 63 |
case KeyEvent, KeySequenceEvent, RawEvent: |
| 64 |
action = BufKeyAction(func(h *BufPane) bool { |
| 65 |
val, err := pl.Call(plFn, luar.New(ulua.L, h)) |
| 66 |
if err != nil { |
| 67 |
screen.TermMessage(err) |
| 68 |
} |
| 69 |
if v, ok := val.(lua.LBool); !ok { |
| 70 |
return false |
| 71 |
} else { |
| 72 |
return bool(v) |
| 73 |
} |
| 74 |
}) |
| 75 |
case MouseEvent: |
| 76 |
action = BufMouseAction(func(h *BufPane, te *tcell.EventMouse) bool { |
| 77 |
val, err := pl.Call(plFn, luar.New(ulua.L, h), luar.New(ulua.L, te)) |
| 78 |
if err != nil { |
| 79 |
screen.TermMessage(err) |
| 80 |
} |
| 81 |
if v, ok := val.(lua.LBool); !ok { |
| 82 |
return false |
| 83 |
} else { |
| 84 |
return bool(v) |
| 85 |
} |
| 86 |
}) |
| 87 |
} |
| 88 |
return action |
| 89 |
} |
| 90 |
|
| 91 |
// BufMapEvent maps an event to an action |
| 92 |
func BufMapEvent(k Event, action string) { |
| 93 |
config.Bindings["buffer"][k.Name()] = action |
| 94 |
|
| 95 |
var actionfns []BufAction |
| 96 |
var names []string |
| 97 |
var types []byte |
| 98 |
for i := 0; ; i++ { |
| 99 |
if action == "" { |
| 100 |
break |
| 101 |
} |
| 102 |
|
| 103 |
idx := util.IndexAnyUnquoted(action, "&|,") |
| 104 |
a := action |
| 105 |
if idx >= 0 { |
| 106 |
a = action[:idx] |
| 107 |
types = append(types, action[idx]) |
| 108 |
action = action[idx+1:] |
| 109 |
} else { |
| 110 |
types = append(types, ' ') |
| 111 |
action = "" |
| 112 |
} |
| 113 |
|
| 114 |
var afn BufAction |
| 115 |
if strings.HasPrefix(a, "command:") { |
| 116 |
a = strings.SplitN(a, ":", 2)[1] |
| 117 |
afn = CommandAction(a) |
| 118 |
names = append(names, "") |
| 119 |
} else if strings.HasPrefix(a, "command-edit:") { |
| 120 |
a = strings.SplitN(a, ":", 2)[1] |
| 121 |
afn = CommandEditAction(a) |
| 122 |
names = append(names, "") |
| 123 |
} else if strings.HasPrefix(a, "lua:") { |
| 124 |
a = strings.SplitN(a, ":", 2)[1] |
| 125 |
afn = LuaAction(a, k) |
| 126 |
if afn == nil { |
| 127 |
screen.TermMessage("Lua Error:", a, "does not exist") |
| 128 |
continue |
| 129 |
} |
| 130 |
split := strings.SplitN(a, ".", 2) |
| 131 |
if len(split) > 1 { |
| 132 |
a = strings.Title(split[0]) + strings.Title(split[1]) |
| 133 |
} else { |
| 134 |
a = strings.Title(a) |
| 135 |
} |
| 136 |
|
| 137 |
names = append(names, a) |
| 138 |
} else if f, ok := BufKeyActions[a]; ok { |
| 139 |
afn = f |
| 140 |
names = append(names, a) |
| 141 |
} else if f, ok := BufMouseActions[a]; ok { |
| 142 |
afn = f |
| 143 |
names = append(names, a) |
| 144 |
} else { |
| 145 |
screen.TermMessage("Error in bindings: action", a, "does not exist") |
| 146 |
continue |
| 147 |
} |
| 148 |
actionfns = append(actionfns, afn) |
| 149 |
} |
| 150 |
bufAction := func(h *BufPane, te *tcell.EventMouse) bool { |
| 151 |
for i, a := range actionfns { |
| 152 |
var success bool |
| 153 |
if _, ok := MultiActions[names[i]]; ok { |
| 154 |
success = true |
| 155 |
for _, c := range h.Buf.GetCursors() { |
| 156 |
h.Buf.SetCurCursor(c.Num) |
| 157 |
h.Cursor = c |
| 158 |
success = success && h.execAction(a, names[i], te) |
| 159 |
} |
| 160 |
} else { |
| 161 |
h.Buf.SetCurCursor(0) |
| 162 |
h.Cursor = h.Buf.GetActiveCursor() |
| 163 |
success = h.execAction(a, names[i], te) |
| 164 |
} |
| 165 |
|
| 166 |
// if the action changed the current pane, update the reference |
| 167 |
h = MainTab().CurPane() |
| 168 |
if h == nil { |
| 169 |
// stop, in case the current pane is not a BufPane |
| 170 |
break |
| 171 |
} |
| 172 |
|
| 173 |
if (!success && types[i] == '&') || (success && types[i] == '|') { |
| 174 |
break |
| 175 |
} |
| 176 |
} |
| 177 |
return true |
| 178 |
} |
| 179 |
|
| 180 |
switch e := k.(type) { |
| 181 |
case KeyEvent, KeySequenceEvent, RawEvent: |
| 182 |
BufBindings.RegisterKeyBinding(e, BufKeyActionGeneral(func(h *BufPane) bool { |
| 183 |
return bufAction(h, nil) |
| 184 |
})) |
| 185 |
case MouseEvent: |
| 186 |
BufBindings.RegisterMouseBinding(e, BufMouseActionGeneral(bufAction)) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// BufUnmap unmaps a key or mouse event from any action |
| 191 |
func BufUnmap(k Event) { |
| 192 |
// TODO |
| 193 |
// delete(BufKeyBindings, k) |
| 194 |
// |
| 195 |
// switch e := k.(type) { |
| 196 |
// case MouseEvent: |
| 197 |
// delete(BufMouseBindings, e) |
| 198 |
// } |
| 199 |
} |
| 200 |
|
| 201 |
// The BufPane connects the buffer and the window |
| 202 |
// It provides a cursor (or multiple) and defines a set of actions |
| 203 |
// that can be taken on the buffer |
| 204 |
// The ActionHandler can access the window for necessary info about |
| 205 |
// visual positions for mouse clicks and scrolling |
| 206 |
type BufPane struct { |
| 207 |
display.BWindow |
| 208 |
|
| 209 |
// Buf is the buffer this BufPane views |
| 210 |
Buf *buffer.Buffer |
| 211 |
// Bindings stores the association of key events and actions |
| 212 |
bindings *KeyTree |
| 213 |
|
| 214 |
// Cursor is the currently active buffer cursor |
| 215 |
Cursor *buffer.Cursor |
| 216 |
|
| 217 |
// Since tcell doesn't differentiate between a mouse press event |
| 218 |
// and a mouse move event with button pressed (nor between a mouse |
| 219 |
// release event and a mouse move event with no buttons pressed), |
| 220 |
// we need to keep track of whether or not the mouse was previously |
| 221 |
// pressed, to determine mouse release and mouse drag events. |
| 222 |
// Moreover, since in case of a release event tcell doesn't tell us |
| 223 |
// which button was released, we need to keep track of which |
| 224 |
// (possibly multiple) buttons were pressed previously. |
| 225 |
mousePressed map[MouseEvent]bool |
| 226 |
|
| 227 |
// This stores when the last click was |
| 228 |
// This is useful for detecting double and triple clicks |
| 229 |
lastClickTime time.Time |
| 230 |
lastLoc buffer.Loc |
| 231 |
|
| 232 |
// freshClip returns true if one or more lines have been cut to the clipboard |
| 233 |
// and have never been pasted yet. |
| 234 |
freshClip bool |
| 235 |
|
| 236 |
// Was the last mouse event actually a double click? |
| 237 |
// Useful for detecting triple clicks -- if a double click is detected |
| 238 |
// but the last mouse event was actually a double click, it's a triple click |
| 239 |
DoubleClick bool |
| 240 |
// Same here, just to keep track for mouse move events |
| 241 |
TripleClick bool |
| 242 |
|
| 243 |
// Should the current multiple cursor selection search based on word or |
| 244 |
// based on selection (false for selection, true for word) |
| 245 |
multiWord bool |
| 246 |
|
| 247 |
splitID uint64 |
| 248 |
tab *Tab |
| 249 |
|
| 250 |
// remember original location of a search in case the search is canceled |
| 251 |
searchOrig buffer.Loc |
| 252 |
|
| 253 |
// The pane may not yet be fully initialized after its creation |
| 254 |
// since we may not know the window geometry yet. In such case we finish |
| 255 |
// its initialization a bit later, after the initial resize. |
| 256 |
initialized bool |
| 257 |
} |
| 258 |
|
| 259 |
func newBufPane(buf *buffer.Buffer, win display.BWindow, tab *Tab) *BufPane { |
| 260 |
h := new(BufPane) |
| 261 |
h.Buf = buf |
| 262 |
h.BWindow = win |
| 263 |
h.tab = tab |
| 264 |
|
| 265 |
h.Cursor = h.Buf.GetActiveCursor() |
| 266 |
h.mousePressed = make(map[MouseEvent]bool) |
| 267 |
|
| 268 |
return h |
| 269 |
} |
| 270 |
|
| 271 |
// NewBufPane creates a new buffer pane with the given window. |
| 272 |
func NewBufPane(buf *buffer.Buffer, win display.BWindow, tab *Tab) *BufPane { |
| 273 |
h := newBufPane(buf, win, tab) |
| 274 |
h.finishInitialize() |
| 275 |
return h |
| 276 |
} |
| 277 |
|
| 278 |
// NewBufPaneFromBuf constructs a new pane from the given buffer and automatically |
| 279 |
// creates a buf window. |
| 280 |
func NewBufPaneFromBuf(buf *buffer.Buffer, tab *Tab) *BufPane { |
| 281 |
w := display.NewBufWindow(0, 0, 0, 0, buf) |
| 282 |
h := newBufPane(buf, w, tab) |
| 283 |
// Postpone finishing initializing the pane until we know the actual geometry |
| 284 |
// of the buf window. |
| 285 |
return h |
| 286 |
} |
| 287 |
|
| 288 |
// TODO: make sure splitID and tab are set before finishInitialize is called |
| 289 |
func (h *BufPane) finishInitialize() { |
| 290 |
h.initialRelocate() |
| 291 |
h.initialized = true |
| 292 |
|
| 293 |
err := config.RunPluginFn("onBufPaneOpen", luar.New(ulua.L, h)) |
| 294 |
if err != nil { |
| 295 |
screen.TermMessage(err) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Resize resizes the pane |
| 300 |
func (h *BufPane) Resize(width, height int) { |
| 301 |
h.BWindow.Resize(width, height) |
| 302 |
if !h.initialized { |
| 303 |
h.finishInitialize() |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// SetTab sets this pane's tab. |
| 308 |
func (h *BufPane) SetTab(t *Tab) { |
| 309 |
h.tab = t |
| 310 |
} |
| 311 |
|
| 312 |
// Tab returns this pane's tab. |
| 313 |
func (h *BufPane) Tab() *Tab { |
| 314 |
return h.tab |
| 315 |
} |
| 316 |
|
| 317 |
func (h *BufPane) ResizePane(size int) { |
| 318 |
n := h.tab.GetNode(h.splitID) |
| 319 |
n.ResizeSplit(size) |
| 320 |
h.tab.Resize() |
| 321 |
} |
| 322 |
|
| 323 |
// PluginCB calls all plugin callbacks with a certain name and displays an |
| 324 |
// error if there is one and returns the aggregate boolean response. |
| 325 |
// The bufpane is passed as the first argument to the callbacks, |
| 326 |
// optional args are passed as the next arguments. |
| 327 |
func (h *BufPane) PluginCB(cb string, args ...any) bool { |
| 328 |
largs := []lua.LValue{luar.New(ulua.L, h)} |
| 329 |
for _, a := range args { |
| 330 |
largs = append(largs, luar.New(ulua.L, a)) |
| 331 |
} |
| 332 |
|
| 333 |
b, err := config.RunPluginFnBool(h.Buf.Settings, cb, largs...) |
| 334 |
if err != nil { |
| 335 |
screen.TermMessage(err) |
| 336 |
} |
| 337 |
return b |
| 338 |
} |
| 339 |
|
| 340 |
func (h *BufPane) resetMouse() { |
| 341 |
for me := range h.mousePressed { |
| 342 |
delete(h.mousePressed, me) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// OpenBuffer opens the given buffer in this pane. |
| 347 |
func (h *BufPane) OpenBuffer(b *buffer.Buffer) { |
| 348 |
h.Buf.Close() |
| 349 |
h.Buf = b |
| 350 |
h.BWindow.SetBuffer(b) |
| 351 |
h.Cursor = b.GetActiveCursor() |
| 352 |
h.Resize(h.GetView().Width, h.GetView().Height) |
| 353 |
h.initialRelocate() |
| 354 |
// Set mouseReleased to true because we assume the mouse is not being |
| 355 |
// pressed when the editor is opened |
| 356 |
h.resetMouse() |
| 357 |
h.lastClickTime = time.Time{} |
| 358 |
} |
| 359 |
|
| 360 |
// GotoLoc moves the cursor to a new location and adjusts the view accordingly. |
| 361 |
// Use GotoLoc when the new location may be far away from the current location. |
| 362 |
func (h *BufPane) GotoLoc(loc buffer.Loc) { |
| 363 |
sloc := h.SLocFromLoc(loc) |
| 364 |
d := h.Diff(h.SLocFromLoc(h.Cursor.Loc), sloc) |
| 365 |
|
| 366 |
h.Cursor.GotoLoc(loc) |
| 367 |
|
| 368 |
// If the new location is far away from the previous one, |
| 369 |
// ensure the cursor is at 25% of the window height |
| 370 |
height := h.BufView().Height |
| 371 |
if util.Abs(d) >= height { |
| 372 |
v := h.GetView() |
| 373 |
v.StartLine = h.Scroll(sloc, -height/4) |
| 374 |
h.ScrollAdjust() |
| 375 |
v.StartCol = 0 |
| 376 |
} |
| 377 |
h.Relocate() |
| 378 |
} |
| 379 |
|
| 380 |
func (h *BufPane) initialRelocate() { |
| 381 |
sloc := h.SLocFromLoc(h.Cursor.Loc) |
| 382 |
height := h.BufView().Height |
| 383 |
|
| 384 |
// If the initial cursor location is far away from the beginning |
| 385 |
// of the buffer, ensure the cursor is at 25% of the window height |
| 386 |
v := h.GetView() |
| 387 |
if h.Diff(display.SLoc{0, 0}, sloc) < height { |
| 388 |
v.StartLine = display.SLoc{0, 0} |
| 389 |
} else { |
| 390 |
v.StartLine = h.Scroll(sloc, -height/4) |
| 391 |
h.ScrollAdjust() |
| 392 |
} |
| 393 |
v.StartCol = 0 |
| 394 |
h.Relocate() |
| 395 |
} |
| 396 |
|
| 397 |
// ID returns this pane's split id. |
| 398 |
func (h *BufPane) ID() uint64 { |
| 399 |
return h.splitID |
| 400 |
} |
| 401 |
|
| 402 |
// SetID sets the split ID of this pane. |
| 403 |
func (h *BufPane) SetID(i uint64) { |
| 404 |
h.splitID = i |
| 405 |
} |
| 406 |
|
| 407 |
// Name returns the BufPane's name. |
| 408 |
func (h *BufPane) Name() string { |
| 409 |
n := h.Buf.GetName() |
| 410 |
if h.Buf.Modified() { |
| 411 |
n += " +" |
| 412 |
} |
| 413 |
return n |
| 414 |
} |
| 415 |
|
| 416 |
// ReOpen reloads the file opened in the bufpane from disk |
| 417 |
func (h *BufPane) ReOpen() { |
| 418 |
h.Buf.ReOpen() |
| 419 |
h.Relocate() |
| 420 |
} |
| 421 |
|
| 422 |
func (h *BufPane) getReloadSetting() string { |
| 423 |
reloadSetting := h.Buf.Settings["reload"] |
| 424 |
return reloadSetting.(string) |
| 425 |
} |
| 426 |
|
| 427 |
// HandleEvent executes the tcell event properly |
| 428 |
func (h *BufPane) HandleEvent(event tcell.Event) { |
| 429 |
if h.Buf.ExternallyModified() && !h.Buf.ReloadDisabled { |
| 430 |
reload := h.getReloadSetting() |
| 431 |
|
| 432 |
if reload == "prompt" { |
| 433 |
InfoBar.YNPrompt("The file on disk has changed. Reload file? (y,n,esc)", func(yes, canceled bool) { |
| 434 |
if canceled { |
| 435 |
h.Buf.DisableReload() |
| 436 |
} |
| 437 |
if !yes || canceled { |
| 438 |
h.Buf.UpdateModTime() |
| 439 |
} else { |
| 440 |
h.ReOpen() |
| 441 |
} |
| 442 |
}) |
| 443 |
} else if reload == "auto" { |
| 444 |
h.ReOpen() |
| 445 |
} else if reload == "disabled" { |
| 446 |
h.Buf.DisableReload() |
| 447 |
} else { |
| 448 |
InfoBar.Message("Invalid reload setting") |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
switch e := event.(type) { |
| 453 |
case *tcell.EventRaw: |
| 454 |
re := RawEvent{ |
| 455 |
esc: e.EscSeq(), |
| 456 |
} |
| 457 |
h.DoKeyEvent(re) |
| 458 |
case *tcell.EventPaste: |
| 459 |
h.paste(e.Text()) |
| 460 |
h.Relocate() |
| 461 |
case *tcell.EventKey: |
| 462 |
ke := keyEvent(e) |
| 463 |
|
| 464 |
done := h.DoKeyEvent(ke) |
| 465 |
if !done && e.Key() == tcell.KeyRune { |
| 466 |
h.DoRuneInsert(e.Rune()) |
| 467 |
} |
| 468 |
case *tcell.EventMouse: |
| 469 |
if e.Buttons() != tcell.ButtonNone { |
| 470 |
me := MouseEvent{ |
| 471 |
btn: e.Buttons(), |
| 472 |
mod: metaToAlt(e.Modifiers()), |
| 473 |
state: MousePress, |
| 474 |
} |
| 475 |
isDrag := len(h.mousePressed) > 0 |
| 476 |
|
| 477 |
if e.Buttons() & ^(tcell.WheelUp|tcell.WheelDown|tcell.WheelLeft|tcell.WheelRight) != tcell.ButtonNone { |
| 478 |
h.mousePressed[me] = true |
| 479 |
} |
| 480 |
|
| 481 |
if isDrag { |
| 482 |
me.state = MouseDrag |
| 483 |
} |
| 484 |
h.DoMouseEvent(me, e) |
| 485 |
} else { |
| 486 |
// Mouse event with no click - mouse was just released. |
| 487 |
// If there were multiple mouse buttons pressed, we don't know which one |
| 488 |
// was actually released, so we assume they all were released. |
| 489 |
pressed := len(h.mousePressed) > 0 |
| 490 |
for me := range h.mousePressed { |
| 491 |
delete(h.mousePressed, me) |
| 492 |
|
| 493 |
me.state = MouseRelease |
| 494 |
h.DoMouseEvent(me, e) |
| 495 |
} |
| 496 |
if !pressed { |
| 497 |
// Propagate the mouse release in case the press wasn't for this BufPane |
| 498 |
Tabs.ResetMouse() |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
h.Buf.MergeCursors() |
| 503 |
|
| 504 |
if h.IsActive() { |
| 505 |
// Display any gutter messages for this line |
| 506 |
c := h.Buf.GetActiveCursor() |
| 507 |
none := true |
| 508 |
for _, m := range h.Buf.Messages { |
| 509 |
if c.Y == m.Start.Y || c.Y == m.End.Y { |
| 510 |
InfoBar.GutterMessage(m.Msg) |
| 511 |
none = false |
| 512 |
break |
| 513 |
} |
| 514 |
} |
| 515 |
if none && InfoBar.HasGutter { |
| 516 |
InfoBar.ClearGutter() |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
cursors := h.Buf.GetCursors() |
| 521 |
for _, c := range cursors { |
| 522 |
if c.NewTrailingWsY != c.Y && (!c.HasSelection() || |
| 523 |
(c.NewTrailingWsY != c.CurSelection[0].Y && c.NewTrailingWsY != c.CurSelection[1].Y)) { |
| 524 |
c.NewTrailingWsY = -1 |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
// Bindings returns the current bindings tree for this buffer. |
| 530 |
func (h *BufPane) Bindings() *KeyTree { |
| 531 |
if h.bindings != nil { |
| 532 |
return h.bindings |
| 533 |
} |
| 534 |
return BufBindings |
| 535 |
} |
| 536 |
|
| 537 |
// DoKeyEvent executes a key event by finding the action it is bound |
| 538 |
// to and executing it (possibly multiple times for multiple cursors). |
| 539 |
// Returns true if the action was executed OR if there are more keys |
| 540 |
// remaining to process before executing an action (if this is a key |
| 541 |
// sequence event). Returns false if no action found. |
| 542 |
func (h *BufPane) DoKeyEvent(e Event) bool { |
| 543 |
binds := h.Bindings() |
| 544 |
action, more := binds.NextEvent(e, nil) |
| 545 |
if action != nil && !more { |
| 546 |
action(h) |
| 547 |
binds.ResetEvents() |
| 548 |
return true |
| 549 |
} else if action == nil && !more { |
| 550 |
binds.ResetEvents() |
| 551 |
} |
| 552 |
return more |
| 553 |
} |
| 554 |
|
| 555 |
func (h *BufPane) execAction(action BufAction, name string, te *tcell.EventMouse) bool { |
| 556 |
if name != "Autocomplete" && name != "CycleAutocompleteBack" { |
| 557 |
h.Buf.HasSuggestions = false |
| 558 |
} |
| 559 |
|
| 560 |
if !h.PluginCB("pre"+name, te) { |
| 561 |
return false |
| 562 |
} |
| 563 |
|
| 564 |
var success bool |
| 565 |
switch a := action.(type) { |
| 566 |
case BufKeyAction: |
| 567 |
success = a(h) |
| 568 |
case BufMouseAction: |
| 569 |
success = a(h, te) |
| 570 |
} |
| 571 |
success = success && h.PluginCB("on"+name, te) |
| 572 |
|
| 573 |
if _, ok := MultiActions[name]; ok { |
| 574 |
if recordingMacro { |
| 575 |
if name != "ToggleMacro" && name != "PlayMacro" { |
| 576 |
curmacro = append(curmacro, action) |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
return success |
| 582 |
} |
| 583 |
|
| 584 |
func (h *BufPane) completeAction(action string) { |
| 585 |
h.PluginCB("on" + action) |
| 586 |
} |
| 587 |
|
| 588 |
func (h *BufPane) HasKeyEvent(e Event) bool { |
| 589 |
// TODO |
| 590 |
return true |
| 591 |
// _, ok := BufKeyBindings[e] |
| 592 |
// return ok |
| 593 |
} |
| 594 |
|
| 595 |
// DoMouseEvent executes a mouse event by finding the action it is bound |
| 596 |
// to and executing it |
| 597 |
func (h *BufPane) DoMouseEvent(e MouseEvent, te *tcell.EventMouse) bool { |
| 598 |
binds := h.Bindings() |
| 599 |
action, _ := binds.NextEvent(e, te) |
| 600 |
if action != nil { |
| 601 |
action(h) |
| 602 |
binds.ResetEvents() |
| 603 |
return true |
| 604 |
} |
| 605 |
// TODO |
| 606 |
return false |
| 607 |
|
| 608 |
// if action, ok := BufMouseBindings[e]; ok { |
| 609 |
// if action(h, te) { |
| 610 |
// h.Relocate() |
| 611 |
// } |
| 612 |
// return true |
| 613 |
// } else if h.HasKeyEvent(e) { |
| 614 |
// return h.DoKeyEvent(e) |
| 615 |
// } |
| 616 |
// return false |
| 617 |
} |
| 618 |
|
| 619 |
// DoRuneInsert inserts a given rune into the current buffer |
| 620 |
// (possibly multiple times for multiple cursors) |
| 621 |
func (h *BufPane) DoRuneInsert(r rune) { |
| 622 |
cursors := h.Buf.GetCursors() |
| 623 |
for _, c := range cursors { |
| 624 |
// Insert a character |
| 625 |
h.Buf.SetCurCursor(c.Num) |
| 626 |
h.Cursor = c |
| 627 |
if !h.PluginCB("preRune", string(r)) { |
| 628 |
continue |
| 629 |
} |
| 630 |
if c.HasSelection() { |
| 631 |
c.DeleteSelection() |
| 632 |
c.ResetSelection() |
| 633 |
} |
| 634 |
|
| 635 |
if h.Buf.OverwriteMode { |
| 636 |
next := c.Loc |
| 637 |
next.X++ |
| 638 |
h.Buf.Replace(c.Loc, next, string(r)) |
| 639 |
} else { |
| 640 |
h.Buf.Insert(c.Loc, string(r)) |
| 641 |
} |
| 642 |
if recordingMacro { |
| 643 |
curmacro = append(curmacro, r) |
| 644 |
} |
| 645 |
h.Relocate() |
| 646 |
h.PluginCB("onRune", string(r)) |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
// VSplitIndex opens the given buffer in a vertical split on the given side. |
| 651 |
func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane { |
| 652 |
e := NewBufPaneFromBuf(buf, h.tab) |
| 653 |
e.splitID = h.tab.GetNode(h.splitID).VSplit(right) |
| 654 |
currentPaneIdx := h.tab.GetPane(h.splitID) |
| 655 |
if right { |
| 656 |
currentPaneIdx++ |
| 657 |
} |
| 658 |
h.tab.AddPane(e, currentPaneIdx) |
| 659 |
h.tab.Resize() |
| 660 |
h.tab.SetActive(currentPaneIdx) |
| 661 |
return e |
| 662 |
} |
| 663 |
|
| 664 |
// HSplitIndex opens the given buffer in a horizontal split on the given side. |
| 665 |
func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane { |
| 666 |
e := NewBufPaneFromBuf(buf, h.tab) |
| 667 |
e.splitID = h.tab.GetNode(h.splitID).HSplit(bottom) |
| 668 |
currentPaneIdx := h.tab.GetPane(h.splitID) |
| 669 |
if bottom { |
| 670 |
currentPaneIdx++ |
| 671 |
} |
| 672 |
h.tab.AddPane(e, currentPaneIdx) |
| 673 |
h.tab.Resize() |
| 674 |
h.tab.SetActive(currentPaneIdx) |
| 675 |
return e |
| 676 |
} |
| 677 |
|
| 678 |
// VSplitBuf opens the given buffer in a new vertical split. |
| 679 |
func (h *BufPane) VSplitBuf(buf *buffer.Buffer) *BufPane { |
| 680 |
return h.VSplitIndex(buf, h.Buf.Settings["splitright"].(bool)) |
| 681 |
} |
| 682 |
|
| 683 |
// HSplitBuf opens the given buffer in a new horizontal split. |
| 684 |
func (h *BufPane) HSplitBuf(buf *buffer.Buffer) *BufPane { |
| 685 |
return h.HSplitIndex(buf, h.Buf.Settings["splitbottom"].(bool)) |
| 686 |
} |
| 687 |
|
| 688 |
// Close this pane. |
| 689 |
func (h *BufPane) Close() { |
| 690 |
h.Buf.Close() |
| 691 |
} |
| 692 |
|
| 693 |
// SetActive marks this pane as active. |
| 694 |
func (h *BufPane) SetActive(b bool) { |
| 695 |
if h.IsActive() == b { |
| 696 |
return |
| 697 |
} |
| 698 |
|
| 699 |
h.BWindow.SetActive(b) |
| 700 |
if b { |
| 701 |
// Display any gutter messages for this line |
| 702 |
c := h.Buf.GetActiveCursor() |
| 703 |
none := true |
| 704 |
for _, m := range h.Buf.Messages { |
| 705 |
if c.Y == m.Start.Y || c.Y == m.End.Y { |
| 706 |
InfoBar.GutterMessage(m.Msg) |
| 707 |
none = false |
| 708 |
break |
| 709 |
} |
| 710 |
} |
| 711 |
if none && InfoBar.HasGutter { |
| 712 |
InfoBar.ClearGutter() |
| 713 |
} |
| 714 |
|
| 715 |
err := config.RunPluginFn("onSetActive", luar.New(ulua.L, h)) |
| 716 |
if err != nil { |
| 717 |
screen.TermMessage(err) |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
// BufKeyActions contains the list of all possible key actions the bufhandler could execute |
| 723 |
var BufKeyActions = map[string]BufKeyAction{ |
| 724 |
"CursorUp": (*BufPane).CursorUp, |
| 725 |
"CursorDown": (*BufPane).CursorDown, |
| 726 |
"CursorPageUp": (*BufPane).CursorPageUp, |
| 727 |
"CursorPageDown": (*BufPane).CursorPageDown, |
| 728 |
"CursorLeft": (*BufPane).CursorLeft, |
| 729 |
"CursorRight": (*BufPane).CursorRight, |
| 730 |
"CursorStart": (*BufPane).CursorStart, |
| 731 |
"CursorEnd": (*BufPane).CursorEnd, |
| 732 |
"CursorToViewTop": (*BufPane).CursorToViewTop, |
| 733 |
"CursorToViewCenter": (*BufPane).CursorToViewCenter, |
| 734 |
"CursorToViewBottom": (*BufPane).CursorToViewBottom, |
| 735 |
"SelectToStart": (*BufPane).SelectToStart, |
| 736 |
"SelectToEnd": (*BufPane).SelectToEnd, |
| 737 |
"SelectUp": (*BufPane).SelectUp, |
| 738 |
"SelectDown": (*BufPane).SelectDown, |
| 739 |
"SelectLeft": (*BufPane).SelectLeft, |
| 740 |
"SelectRight": (*BufPane).SelectRight, |
| 741 |
"WordRight": (*BufPane).WordRight, |
| 742 |
"WordLeft": (*BufPane).WordLeft, |
| 743 |
"SubWordRight": (*BufPane).SubWordRight, |
| 744 |
"SubWordLeft": (*BufPane).SubWordLeft, |
| 745 |
"SelectWordRight": (*BufPane).SelectWordRight, |
| 746 |
"SelectWordLeft": (*BufPane).SelectWordLeft, |
| 747 |
"SelectSubWordRight": (*BufPane).SelectSubWordRight, |
| 748 |
"SelectSubWordLeft": (*BufPane).SelectSubWordLeft, |
| 749 |
"DeleteWordRight": (*BufPane).DeleteWordRight, |
| 750 |
"DeleteWordLeft": (*BufPane).DeleteWordLeft, |
| 751 |
"DeleteSubWordRight": (*BufPane).DeleteSubWordRight, |
| 752 |
"DeleteSubWordLeft": (*BufPane).DeleteSubWordLeft, |
| 753 |
"SelectLine": (*BufPane).SelectLine, |
| 754 |
"SelectToStartOfLine": (*BufPane).SelectToStartOfLine, |
| 755 |
"SelectToStartOfText": (*BufPane).SelectToStartOfText, |
| 756 |
"SelectToStartOfTextToggle": (*BufPane).SelectToStartOfTextToggle, |
| 757 |
"SelectToEndOfLine": (*BufPane).SelectToEndOfLine, |
| 758 |
"ParagraphPrevious": (*BufPane).ParagraphPrevious, |
| 759 |
"ParagraphNext": (*BufPane).ParagraphNext, |
| 760 |
"SelectToParagraphPrevious": (*BufPane).SelectToParagraphPrevious, |
| 761 |
"SelectToParagraphNext": (*BufPane).SelectToParagraphNext, |
| 762 |
"InsertNewline": (*BufPane).InsertNewline, |
| 763 |
"Backspace": (*BufPane).Backspace, |
| 764 |
"Delete": (*BufPane).Delete, |
| 765 |
"InsertTab": (*BufPane).InsertTab, |
| 766 |
"Save": (*BufPane).Save, |
| 767 |
"SaveAll": (*BufPane).SaveAll, |
| 768 |
"SaveAs": (*BufPane).SaveAs, |
| 769 |
"Find": (*BufPane).Find, |
| 770 |
"FindLiteral": (*BufPane).FindLiteral, |
| 771 |
"FindNext": (*BufPane).FindNext, |
| 772 |
"FindPrevious": (*BufPane).FindPrevious, |
| 773 |
"DiffNext": (*BufPane).DiffNext, |
| 774 |
"DiffPrevious": (*BufPane).DiffPrevious, |
| 775 |
"Center": (*BufPane).Center, |
| 776 |
"Undo": (*BufPane).Undo, |
| 777 |
"Redo": (*BufPane).Redo, |
| 778 |
"Copy": (*BufPane).Copy, |
| 779 |
"CopyLine": (*BufPane).CopyLine, |
| 780 |
"Cut": (*BufPane).Cut, |
| 781 |
"CutLine": (*BufPane).CutLine, |
| 782 |
"Duplicate": (*BufPane).Duplicate, |
| 783 |
"DuplicateLine": (*BufPane).DuplicateLine, |
| 784 |
"DeleteLine": (*BufPane).DeleteLine, |
| 785 |
"MoveLinesUp": (*BufPane).MoveLinesUp, |
| 786 |
"MoveLinesDown": (*BufPane).MoveLinesDown, |
| 787 |
"IndentSelection": (*BufPane).IndentSelection, |
| 788 |
"OutdentSelection": (*BufPane).OutdentSelection, |
| 789 |
"Autocomplete": (*BufPane).Autocomplete, |
| 790 |
"CycleAutocompleteBack": (*BufPane).CycleAutocompleteBack, |
| 791 |
"OutdentLine": (*BufPane).OutdentLine, |
| 792 |
"IndentLine": (*BufPane).IndentLine, |
| 793 |
"Paste": (*BufPane).Paste, |
| 794 |
"PastePrimary": (*BufPane).PastePrimary, |
| 795 |
"SelectAll": (*BufPane).SelectAll, |
| 796 |
"OpenFile": (*BufPane).OpenFile, |
| 797 |
"Start": (*BufPane).Start, |
| 798 |
"End": (*BufPane).End, |
| 799 |
"PageUp": (*BufPane).PageUp, |
| 800 |
"PageDown": (*BufPane).PageDown, |
| 801 |
"SelectPageUp": (*BufPane).SelectPageUp, |
| 802 |
"SelectPageDown": (*BufPane).SelectPageDown, |
| 803 |
"HalfPageUp": (*BufPane).HalfPageUp, |
| 804 |
"HalfPageDown": (*BufPane).HalfPageDown, |
| 805 |
"StartOfText": (*BufPane).StartOfText, |
| 806 |
"StartOfTextToggle": (*BufPane).StartOfTextToggle, |
| 807 |
"StartOfLine": (*BufPane).StartOfLine, |
| 808 |
"EndOfLine": (*BufPane).EndOfLine, |
| 809 |
"ToggleHelp": (*BufPane).ToggleHelp, |
| 810 |
"ToggleKeyMenu": (*BufPane).ToggleKeyMenu, |
| 811 |
"ToggleDiffGutter": (*BufPane).ToggleDiffGutter, |
| 812 |
"ToggleRuler": (*BufPane).ToggleRuler, |
| 813 |
"ToggleHighlightSearch": (*BufPane).ToggleHighlightSearch, |
| 814 |
"UnhighlightSearch": (*BufPane).UnhighlightSearch, |
| 815 |
"ResetSearch": (*BufPane).ResetSearch, |
| 816 |
"ClearStatus": (*BufPane).ClearStatus, |
| 817 |
"ShellMode": (*BufPane).ShellMode, |
| 818 |
"CommandMode": (*BufPane).CommandMode, |
| 819 |
"ToggleOverwriteMode": (*BufPane).ToggleOverwriteMode, |
| 820 |
"Escape": (*BufPane).Escape, |
| 821 |
"Quit": (*BufPane).Quit, |
| 822 |
"QuitAll": (*BufPane).QuitAll, |
| 823 |
"ForceQuit": (*BufPane).ForceQuit, |
| 824 |
"AddTab": (*BufPane).AddTab, |
| 825 |
"PreviousTab": (*BufPane).PreviousTab, |
| 826 |
"NextTab": (*BufPane).NextTab, |
| 827 |
"FirstTab": (*BufPane).FirstTab, |
| 828 |
"LastTab": (*BufPane).LastTab, |
| 829 |
"NextSplit": (*BufPane).NextSplit, |
| 830 |
"PreviousSplit": (*BufPane).PreviousSplit, |
| 831 |
"FirstSplit": (*BufPane).FirstSplit, |
| 832 |
"LastSplit": (*BufPane).LastSplit, |
| 833 |
"Unsplit": (*BufPane).Unsplit, |
| 834 |
"VSplit": (*BufPane).VSplitAction, |
| 835 |
"HSplit": (*BufPane).HSplitAction, |
| 836 |
"ToggleMacro": (*BufPane).ToggleMacro, |
| 837 |
"PlayMacro": (*BufPane).PlayMacro, |
| 838 |
"Suspend": (*BufPane).Suspend, |
| 839 |
"ScrollUp": (*BufPane).ScrollUpAction, |
| 840 |
"ScrollDown": (*BufPane).ScrollDownAction, |
| 841 |
"SpawnMultiCursor": (*BufPane).SpawnMultiCursor, |
| 842 |
"SpawnMultiCursorUp": (*BufPane).SpawnMultiCursorUp, |
| 843 |
"SpawnMultiCursorDown": (*BufPane).SpawnMultiCursorDown, |
| 844 |
"SpawnMultiCursorSelect": (*BufPane).SpawnMultiCursorSelect, |
| 845 |
"RemoveMultiCursor": (*BufPane).RemoveMultiCursor, |
| 846 |
"RemoveAllMultiCursors": (*BufPane).RemoveAllMultiCursors, |
| 847 |
"SkipMultiCursor": (*BufPane).SkipMultiCursor, |
| 848 |
"SkipMultiCursorBack": (*BufPane).SkipMultiCursorBack, |
| 849 |
"JumpToMatchingBrace": (*BufPane).JumpToMatchingBrace, |
| 850 |
"JumpLine": (*BufPane).JumpLine, |
| 851 |
"Deselect": (*BufPane).Deselect, |
| 852 |
"ClearInfo": (*BufPane).ClearInfo, |
| 853 |
"None": (*BufPane).None, |
| 854 |
|
| 855 |
// This was changed to InsertNewline but I don't want to break backwards compatibility |
| 856 |
"InsertEnter": (*BufPane).InsertNewline, |
| 857 |
} |
| 858 |
|
| 859 |
// BufMouseActions contains the list of all possible mouse actions the bufhandler could execute |
| 860 |
var BufMouseActions = map[string]BufMouseAction{ |
| 861 |
"MousePress": (*BufPane).MousePress, |
| 862 |
"MouseDrag": (*BufPane).MouseDrag, |
| 863 |
"MouseRelease": (*BufPane).MouseRelease, |
| 864 |
"MouseMultiCursor": (*BufPane).MouseMultiCursor, |
| 865 |
} |
| 866 |
|
| 867 |
// MultiActions is a list of actions that should be executed multiple |
| 868 |
// times if there are multiple cursors (one per cursor) |
| 869 |
// Generally actions that modify global editor state like quitting or |
| 870 |
// saving should not be included in this list |
| 871 |
var MultiActions = map[string]bool{ |
| 872 |
"CursorUp": true, |
| 873 |
"CursorDown": true, |
| 874 |
"CursorPageUp": true, |
| 875 |
"CursorPageDown": true, |
| 876 |
"CursorLeft": true, |
| 877 |
"CursorRight": true, |
| 878 |
"CursorStart": true, |
| 879 |
"CursorEnd": true, |
| 880 |
"SelectToStart": true, |
| 881 |
"SelectToEnd": true, |
| 882 |
"SelectUp": true, |
| 883 |
"SelectDown": true, |
| 884 |
"SelectLeft": true, |
| 885 |
"SelectRight": true, |
| 886 |
"WordRight": true, |
| 887 |
"WordLeft": true, |
| 888 |
"SubWordRight": true, |
| 889 |
"SubWordLeft": true, |
| 890 |
"SelectWordRight": true, |
| 891 |
"SelectWordLeft": true, |
| 892 |
"SelectSubWordRight": true, |
| 893 |
"SelectSubWordLeft": true, |
| 894 |
"DeleteWordRight": true, |
| 895 |
"DeleteWordLeft": true, |
| 896 |
"DeleteSubWordRight": true, |
| 897 |
"DeleteSubWordLeft": true, |
| 898 |
"SelectLine": true, |
| 899 |
"SelectToStartOfLine": true, |
| 900 |
"SelectToStartOfText": true, |
| 901 |
"SelectToStartOfTextToggle": true, |
| 902 |
"SelectToEndOfLine": true, |
| 903 |
"ParagraphPrevious": true, |
| 904 |
"ParagraphNext": true, |
| 905 |
"InsertNewline": true, |
| 906 |
"Backspace": true, |
| 907 |
"Delete": true, |
| 908 |
"InsertTab": true, |
| 909 |
"FindNext": true, |
| 910 |
"FindPrevious": true, |
| 911 |
"CopyLine": true, |
| 912 |
"Copy": true, |
| 913 |
"Cut": true, |
| 914 |
"CutLine": true, |
| 915 |
"Duplicate": true, |
| 916 |
"DuplicateLine": true, |
| 917 |
"DeleteLine": true, |
| 918 |
"MoveLinesUp": true, |
| 919 |
"MoveLinesDown": true, |
| 920 |
"IndentSelection": true, |
| 921 |
"OutdentSelection": true, |
| 922 |
"OutdentLine": true, |
| 923 |
"IndentLine": true, |
| 924 |
"Paste": true, |
| 925 |
"PastePrimary": true, |
| 926 |
"SelectPageUp": true, |
| 927 |
"SelectPageDown": true, |
| 928 |
"StartOfLine": true, |
| 929 |
"StartOfText": true, |
| 930 |
"StartOfTextToggle": true, |
| 931 |
"EndOfLine": true, |
| 932 |
"JumpToMatchingBrace": true, |
| 933 |
} |