| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io/fs" |
| 7 |
"os" |
| 8 |
"regexp" |
| 9 |
"runtime" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
shellquote "github.com/kballard/go-shellquote" |
| 14 |
"github.com/micro-editor/tcell/v2" |
| 15 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 16 |
"github.com/zyedidia/micro/v2/internal/clipboard" |
| 17 |
"github.com/zyedidia/micro/v2/internal/config" |
| 18 |
"github.com/zyedidia/micro/v2/internal/display" |
| 19 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 20 |
"github.com/zyedidia/micro/v2/internal/shell" |
| 21 |
"github.com/zyedidia/micro/v2/internal/util" |
| 22 |
) |
| 23 |
|
| 24 |
// ScrollUp is not an action |
| 25 |
func (h *BufPane) ScrollUp(n int) { |
| 26 |
v := h.GetView() |
| 27 |
v.StartLine = h.Scroll(v.StartLine, -n) |
| 28 |
h.SetView(v) |
| 29 |
} |
| 30 |
|
| 31 |
// ScrollDown is not an action |
| 32 |
func (h *BufPane) ScrollDown(n int) { |
| 33 |
v := h.GetView() |
| 34 |
v.StartLine = h.Scroll(v.StartLine, n) |
| 35 |
h.SetView(v) |
| 36 |
} |
| 37 |
|
| 38 |
// ScrollAdjust can be used to shift the view so that the last line is at the |
| 39 |
// bottom if the user has scrolled past the last line. |
| 40 |
func (h *BufPane) ScrollAdjust() { |
| 41 |
v := h.GetView() |
| 42 |
end := h.SLocFromLoc(h.Buf.End()) |
| 43 |
if h.Diff(v.StartLine, end) < h.BufView().Height-1 { |
| 44 |
v.StartLine = h.Scroll(end, -h.BufView().Height+1) |
| 45 |
} |
| 46 |
h.SetView(v) |
| 47 |
} |
| 48 |
|
| 49 |
// ScrollReachedEnd returns true if the view is at the end of the buffer, |
| 50 |
// i.e. the last line of the buffer is in the view. |
| 51 |
func (h *BufPane) ScrollReachedEnd() bool { |
| 52 |
v := h.GetView() |
| 53 |
end := h.SLocFromLoc(h.Buf.End()) |
| 54 |
return h.Diff(v.StartLine, end) < h.BufView().Height |
| 55 |
} |
| 56 |
|
| 57 |
// MousePress is the event that should happen when a normal click happens |
| 58 |
// This is almost always bound to left click |
| 59 |
func (h *BufPane) MousePress(e *tcell.EventMouse) bool { |
| 60 |
b := h.Buf |
| 61 |
mx, my := e.Position() |
| 62 |
// ignore click on the status line |
| 63 |
if my >= h.BufView().Y+h.BufView().Height { |
| 64 |
return false |
| 65 |
} |
| 66 |
mouseLoc := h.LocFromVisual(buffer.Loc{mx, my}) |
| 67 |
h.Cursor.Loc = mouseLoc |
| 68 |
|
| 69 |
if b.NumCursors() > 1 { |
| 70 |
b.ClearCursors() |
| 71 |
h.Relocate() |
| 72 |
h.Cursor = h.Buf.GetActiveCursor() |
| 73 |
h.Cursor.Loc = mouseLoc |
| 74 |
} |
| 75 |
if time.Since(h.lastClickTime)/time.Millisecond < config.DoubleClickThreshold && (mouseLoc.X == h.lastLoc.X && mouseLoc.Y == h.lastLoc.Y) { |
| 76 |
if h.DoubleClick { |
| 77 |
// Triple click |
| 78 |
h.lastClickTime = time.Now() |
| 79 |
|
| 80 |
h.TripleClick = true |
| 81 |
h.DoubleClick = false |
| 82 |
|
| 83 |
h.Cursor.SelectLine() |
| 84 |
h.Cursor.CopySelection(clipboard.PrimaryReg) |
| 85 |
} else { |
| 86 |
// Double click |
| 87 |
h.lastClickTime = time.Now() |
| 88 |
|
| 89 |
h.DoubleClick = true |
| 90 |
h.TripleClick = false |
| 91 |
|
| 92 |
h.Cursor.SelectWord() |
| 93 |
h.Cursor.CopySelection(clipboard.PrimaryReg) |
| 94 |
} |
| 95 |
} else { |
| 96 |
h.DoubleClick = false |
| 97 |
h.TripleClick = false |
| 98 |
h.lastClickTime = time.Now() |
| 99 |
|
| 100 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 101 |
h.Cursor.CurSelection[0] = h.Cursor.Loc |
| 102 |
h.Cursor.CurSelection[1] = h.Cursor.Loc |
| 103 |
} |
| 104 |
|
| 105 |
h.Cursor.StoreVisualX() |
| 106 |
h.lastLoc = mouseLoc |
| 107 |
h.Relocate() |
| 108 |
return true |
| 109 |
} |
| 110 |
|
| 111 |
func (h *BufPane) MouseDrag(e *tcell.EventMouse) bool { |
| 112 |
mx, my := e.Position() |
| 113 |
// ignore drag on the status line |
| 114 |
if my >= h.BufView().Y+h.BufView().Height { |
| 115 |
return false |
| 116 |
} |
| 117 |
h.Cursor.Loc = h.LocFromVisual(buffer.Loc{mx, my}) |
| 118 |
|
| 119 |
if h.TripleClick { |
| 120 |
h.Cursor.AddLineToSelection() |
| 121 |
} else if h.DoubleClick { |
| 122 |
h.Cursor.AddWordToSelection() |
| 123 |
} else { |
| 124 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 125 |
} |
| 126 |
|
| 127 |
h.Cursor.StoreVisualX() |
| 128 |
h.Relocate() |
| 129 |
return true |
| 130 |
} |
| 131 |
|
| 132 |
func (h *BufPane) MouseRelease(e *tcell.EventMouse) bool { |
| 133 |
// We could finish the selection based on the release location as in the |
| 134 |
// commented out code below, to allow text selections even in a terminal |
| 135 |
// that doesn't support mouse motion events. But when the mouse click is |
| 136 |
// within the scroll margin, that would cause a scroll and selection |
| 137 |
// even for a simple mouse click, which is not good. |
| 138 |
// if !h.DoubleClick && !h.TripleClick { |
| 139 |
// mx, my := e.Position() |
| 140 |
// h.Cursor.Loc = h.LocFromVisual(buffer.Loc{mx, my}) |
| 141 |
// h.Cursor.SetSelectionEnd(h.Cursor.Loc) |
| 142 |
// } |
| 143 |
|
| 144 |
if h.Cursor.HasSelection() { |
| 145 |
h.Cursor.CopySelection(clipboard.PrimaryReg) |
| 146 |
} |
| 147 |
return true |
| 148 |
} |
| 149 |
|
| 150 |
// ScrollUpAction scrolls the view up |
| 151 |
func (h *BufPane) ScrollUpAction() bool { |
| 152 |
h.ScrollUp(util.IntOpt(h.Buf.Settings["scrollspeed"])) |
| 153 |
return true |
| 154 |
} |
| 155 |
|
| 156 |
// ScrollDownAction scrolls the view down |
| 157 |
func (h *BufPane) ScrollDownAction() bool { |
| 158 |
h.ScrollDown(util.IntOpt(h.Buf.Settings["scrollspeed"])) |
| 159 |
return true |
| 160 |
} |
| 161 |
|
| 162 |
// Center centers the view on the cursor |
| 163 |
func (h *BufPane) Center() bool { |
| 164 |
v := h.GetView() |
| 165 |
v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufView().Height/2) |
| 166 |
h.SetView(v) |
| 167 |
h.ScrollAdjust() |
| 168 |
return true |
| 169 |
} |
| 170 |
|
| 171 |
// CursorToViewTop moves the cursor to the top of the view, |
| 172 |
// offset by scrollmargin unless at the beginning or end of the file |
| 173 |
func (h *BufPane) CursorToViewTop() bool { |
| 174 |
v := h.GetView() |
| 175 |
h.Buf.ClearCursors() |
| 176 |
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64)) |
| 177 |
bStart := display.SLoc{0, 0} |
| 178 |
if v.StartLine == bStart { |
| 179 |
scrollmargin = 0 |
| 180 |
} |
| 181 |
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ |
| 182 |
SLoc: h.Scroll(v.StartLine, scrollmargin), |
| 183 |
VisualX: 0, |
| 184 |
})) |
| 185 |
return true |
| 186 |
} |
| 187 |
|
| 188 |
// CursorToViewCenter moves the cursor to the center of the view |
| 189 |
func (h *BufPane) CursorToViewCenter() bool { |
| 190 |
v := h.GetView() |
| 191 |
h.Buf.ClearCursors() |
| 192 |
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ |
| 193 |
SLoc: h.Scroll(v.StartLine, h.BufView().Height/2), |
| 194 |
VisualX: 0, |
| 195 |
})) |
| 196 |
return true |
| 197 |
} |
| 198 |
|
| 199 |
// CursorToViewBottom moves the cursor to the bottom of the view, |
| 200 |
// offset by scrollmargin unless at the beginning or end of the file |
| 201 |
func (h *BufPane) CursorToViewBottom() bool { |
| 202 |
v := h.GetView() |
| 203 |
h.Buf.ClearCursors() |
| 204 |
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64)) |
| 205 |
bEnd := h.SLocFromLoc(h.Buf.End()) |
| 206 |
lastLine := h.Scroll(v.StartLine, h.BufView().Height-1) |
| 207 |
if lastLine == bEnd { |
| 208 |
scrollmargin = 0 |
| 209 |
} |
| 210 |
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{ |
| 211 |
SLoc: h.Scroll(lastLine, -scrollmargin), |
| 212 |
VisualX: 0, |
| 213 |
})) |
| 214 |
return true |
| 215 |
} |
| 216 |
|
| 217 |
// MoveCursorUp is not an action |
| 218 |
func (h *BufPane) MoveCursorUp(n int) { |
| 219 |
if !h.Buf.Settings["softwrap"].(bool) { |
| 220 |
h.Cursor.UpN(n) |
| 221 |
} else { |
| 222 |
vloc := h.VLocFromLoc(h.Cursor.Loc) |
| 223 |
sloc := h.Scroll(vloc.SLoc, -n) |
| 224 |
if sloc == vloc.SLoc { |
| 225 |
// we are at the beginning of buffer |
| 226 |
h.Cursor.Loc = h.Buf.Start() |
| 227 |
h.Cursor.StoreVisualX() |
| 228 |
} else { |
| 229 |
vloc.SLoc = sloc |
| 230 |
vloc.VisualX = h.Cursor.LastWrappedVisualX |
| 231 |
h.Cursor.Loc = h.LocFromVLoc(vloc) |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// MoveCursorDown is not an action |
| 237 |
func (h *BufPane) MoveCursorDown(n int) { |
| 238 |
if !h.Buf.Settings["softwrap"].(bool) { |
| 239 |
h.Cursor.DownN(n) |
| 240 |
} else { |
| 241 |
vloc := h.VLocFromLoc(h.Cursor.Loc) |
| 242 |
sloc := h.Scroll(vloc.SLoc, n) |
| 243 |
if sloc == vloc.SLoc { |
| 244 |
// we are at the end of buffer |
| 245 |
h.Cursor.Loc = h.Buf.End() |
| 246 |
h.Cursor.StoreVisualX() |
| 247 |
} else { |
| 248 |
vloc.SLoc = sloc |
| 249 |
vloc.VisualX = h.Cursor.LastWrappedVisualX |
| 250 |
h.Cursor.Loc = h.LocFromVLoc(vloc) |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
// CursorUp moves the cursor up |
| 256 |
func (h *BufPane) CursorUp() bool { |
| 257 |
h.Cursor.Deselect(true) |
| 258 |
h.MoveCursorUp(1) |
| 259 |
h.Relocate() |
| 260 |
return true |
| 261 |
} |
| 262 |
|
| 263 |
// CursorDown moves the cursor down |
| 264 |
func (h *BufPane) CursorDown() bool { |
| 265 |
selectionEndNewline := h.Cursor.HasSelection() && h.Cursor.CurSelection[1].X == 0 |
| 266 |
h.Cursor.Deselect(false) |
| 267 |
if selectionEndNewline { |
| 268 |
h.Cursor.Start() |
| 269 |
} else { |
| 270 |
h.MoveCursorDown(1) |
| 271 |
} |
| 272 |
h.Relocate() |
| 273 |
return true |
| 274 |
} |
| 275 |
|
| 276 |
// CursorLeft moves the cursor left |
| 277 |
func (h *BufPane) CursorLeft() bool { |
| 278 |
if h.Cursor.HasSelection() { |
| 279 |
h.Cursor.Deselect(true) |
| 280 |
} else { |
| 281 |
tabstospaces := h.Buf.Settings["tabstospaces"].(bool) |
| 282 |
tabmovement := h.Buf.Settings["tabmovement"].(bool) |
| 283 |
if tabstospaces && tabmovement { |
| 284 |
tabsize := int(h.Buf.Settings["tabsize"].(float64)) |
| 285 |
line := h.Buf.LineBytes(h.Cursor.Y) |
| 286 |
if h.Cursor.X-tabsize >= 0 && util.IsSpaces(line[h.Cursor.X-tabsize:h.Cursor.X]) && util.IsBytesWhitespace(line[0:h.Cursor.X-tabsize]) { |
| 287 |
for i := 0; i < tabsize; i++ { |
| 288 |
h.Cursor.Left() |
| 289 |
} |
| 290 |
} else { |
| 291 |
h.Cursor.Left() |
| 292 |
} |
| 293 |
} else { |
| 294 |
h.Cursor.Left() |
| 295 |
} |
| 296 |
} |
| 297 |
h.Relocate() |
| 298 |
return true |
| 299 |
} |
| 300 |
|
| 301 |
// CursorRight moves the cursor right |
| 302 |
func (h *BufPane) CursorRight() bool { |
| 303 |
if h.Cursor.HasSelection() { |
| 304 |
h.Cursor.Deselect(false) |
| 305 |
} else { |
| 306 |
tabstospaces := h.Buf.Settings["tabstospaces"].(bool) |
| 307 |
tabmovement := h.Buf.Settings["tabmovement"].(bool) |
| 308 |
if tabstospaces && tabmovement { |
| 309 |
tabsize := int(h.Buf.Settings["tabsize"].(float64)) |
| 310 |
line := h.Buf.LineBytes(h.Cursor.Y) |
| 311 |
if h.Cursor.X+tabsize < util.CharacterCount(line) && util.IsSpaces(line[h.Cursor.X:h.Cursor.X+tabsize]) && util.IsBytesWhitespace(line[0:h.Cursor.X]) { |
| 312 |
for i := 0; i < tabsize; i++ { |
| 313 |
h.Cursor.Right() |
| 314 |
} |
| 315 |
} else { |
| 316 |
h.Cursor.Right() |
| 317 |
} |
| 318 |
} else { |
| 319 |
h.Cursor.Right() |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
h.Relocate() |
| 324 |
return true |
| 325 |
} |
| 326 |
|
| 327 |
// WordRight moves the cursor one word to the right |
| 328 |
func (h *BufPane) WordRight() bool { |
| 329 |
h.Cursor.Deselect(false) |
| 330 |
h.Cursor.WordRight() |
| 331 |
h.Relocate() |
| 332 |
return true |
| 333 |
} |
| 334 |
|
| 335 |
// WordLeft moves the cursor one word to the left |
| 336 |
func (h *BufPane) WordLeft() bool { |
| 337 |
h.Cursor.Deselect(true) |
| 338 |
h.Cursor.WordLeft() |
| 339 |
h.Relocate() |
| 340 |
return true |
| 341 |
} |
| 342 |
|
| 343 |
// SubWordRight moves the cursor one sub-word to the right |
| 344 |
func (h *BufPane) SubWordRight() bool { |
| 345 |
h.Cursor.Deselect(false) |
| 346 |
h.Cursor.SubWordRight() |
| 347 |
h.Relocate() |
| 348 |
return true |
| 349 |
} |
| 350 |
|
| 351 |
// SubWordLeft moves the cursor one sub-word to the left |
| 352 |
func (h *BufPane) SubWordLeft() bool { |
| 353 |
h.Cursor.Deselect(true) |
| 354 |
h.Cursor.SubWordLeft() |
| 355 |
h.Relocate() |
| 356 |
return true |
| 357 |
} |
| 358 |
|
| 359 |
// SelectUp selects up one line |
| 360 |
func (h *BufPane) SelectUp() bool { |
| 361 |
if !h.Cursor.HasSelection() { |
| 362 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 363 |
} |
| 364 |
h.MoveCursorUp(1) |
| 365 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 366 |
h.Relocate() |
| 367 |
return true |
| 368 |
} |
| 369 |
|
| 370 |
// SelectDown selects down one line |
| 371 |
func (h *BufPane) SelectDown() bool { |
| 372 |
if !h.Cursor.HasSelection() { |
| 373 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 374 |
} |
| 375 |
h.MoveCursorDown(1) |
| 376 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 377 |
h.Relocate() |
| 378 |
return true |
| 379 |
} |
| 380 |
|
| 381 |
// SelectLeft selects the character to the left of the cursor |
| 382 |
func (h *BufPane) SelectLeft() bool { |
| 383 |
loc := h.Cursor.Loc |
| 384 |
count := h.Buf.End() |
| 385 |
if loc.GreaterThan(count) { |
| 386 |
loc = count |
| 387 |
} |
| 388 |
if !h.Cursor.HasSelection() { |
| 389 |
h.Cursor.OrigSelection[0] = loc |
| 390 |
} |
| 391 |
h.Cursor.Left() |
| 392 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 393 |
h.Relocate() |
| 394 |
return true |
| 395 |
} |
| 396 |
|
| 397 |
// SelectRight selects the character to the right of the cursor |
| 398 |
func (h *BufPane) SelectRight() bool { |
| 399 |
loc := h.Cursor.Loc |
| 400 |
count := h.Buf.End() |
| 401 |
if loc.GreaterThan(count) { |
| 402 |
loc = count |
| 403 |
} |
| 404 |
if !h.Cursor.HasSelection() { |
| 405 |
h.Cursor.OrigSelection[0] = loc |
| 406 |
} |
| 407 |
h.Cursor.Right() |
| 408 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 409 |
h.Relocate() |
| 410 |
return true |
| 411 |
} |
| 412 |
|
| 413 |
// SelectWordRight selects the word to the right of the cursor |
| 414 |
func (h *BufPane) SelectWordRight() bool { |
| 415 |
if !h.Cursor.HasSelection() { |
| 416 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 417 |
} |
| 418 |
h.Cursor.WordRight() |
| 419 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 420 |
h.Relocate() |
| 421 |
return true |
| 422 |
} |
| 423 |
|
| 424 |
// SelectWordLeft selects the word to the left of the cursor |
| 425 |
func (h *BufPane) SelectWordLeft() bool { |
| 426 |
if !h.Cursor.HasSelection() { |
| 427 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 428 |
} |
| 429 |
h.Cursor.WordLeft() |
| 430 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 431 |
h.Relocate() |
| 432 |
return true |
| 433 |
} |
| 434 |
|
| 435 |
// SelectSubWordRight selects the sub-word to the right of the cursor |
| 436 |
func (h *BufPane) SelectSubWordRight() bool { |
| 437 |
if !h.Cursor.HasSelection() { |
| 438 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 439 |
} |
| 440 |
h.Cursor.SubWordRight() |
| 441 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 442 |
h.Relocate() |
| 443 |
return true |
| 444 |
} |
| 445 |
|
| 446 |
// SelectSubWordLeft selects the sub-word to the left of the cursor |
| 447 |
func (h *BufPane) SelectSubWordLeft() bool { |
| 448 |
if !h.Cursor.HasSelection() { |
| 449 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 450 |
} |
| 451 |
h.Cursor.SubWordLeft() |
| 452 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 453 |
h.Relocate() |
| 454 |
return true |
| 455 |
} |
| 456 |
|
| 457 |
// StartOfText moves the cursor to the start of the text of the line |
| 458 |
func (h *BufPane) StartOfText() bool { |
| 459 |
h.Cursor.Deselect(true) |
| 460 |
h.Cursor.StartOfText() |
| 461 |
h.Relocate() |
| 462 |
return true |
| 463 |
} |
| 464 |
|
| 465 |
// StartOfTextToggle toggles the cursor between the start of the text of the line |
| 466 |
// and the start of the line |
| 467 |
func (h *BufPane) StartOfTextToggle() bool { |
| 468 |
h.Cursor.Deselect(true) |
| 469 |
if h.Cursor.IsStartOfText() { |
| 470 |
h.Cursor.Start() |
| 471 |
} else { |
| 472 |
h.Cursor.StartOfText() |
| 473 |
} |
| 474 |
h.Relocate() |
| 475 |
return true |
| 476 |
} |
| 477 |
|
| 478 |
// StartOfLine moves the cursor to the start of the line |
| 479 |
func (h *BufPane) StartOfLine() bool { |
| 480 |
h.Cursor.Deselect(true) |
| 481 |
h.Cursor.Start() |
| 482 |
h.Relocate() |
| 483 |
return true |
| 484 |
} |
| 485 |
|
| 486 |
// EndOfLine moves the cursor to the end of the line |
| 487 |
func (h *BufPane) EndOfLine() bool { |
| 488 |
h.Cursor.Deselect(true) |
| 489 |
h.Cursor.End() |
| 490 |
h.Relocate() |
| 491 |
return true |
| 492 |
} |
| 493 |
|
| 494 |
// SelectLine selects the entire current line |
| 495 |
func (h *BufPane) SelectLine() bool { |
| 496 |
h.Cursor.SelectLine() |
| 497 |
h.Relocate() |
| 498 |
return true |
| 499 |
} |
| 500 |
|
| 501 |
// SelectToStartOfText selects to the start of the text on the current line |
| 502 |
func (h *BufPane) SelectToStartOfText() bool { |
| 503 |
if !h.Cursor.HasSelection() { |
| 504 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 505 |
} |
| 506 |
h.Cursor.StartOfText() |
| 507 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 508 |
h.Relocate() |
| 509 |
return true |
| 510 |
} |
| 511 |
|
| 512 |
// SelectToStartOfTextToggle toggles the selection between the start of the text |
| 513 |
// on the current line and the start of the line |
| 514 |
func (h *BufPane) SelectToStartOfTextToggle() bool { |
| 515 |
if !h.Cursor.HasSelection() { |
| 516 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 517 |
} |
| 518 |
if h.Cursor.IsStartOfText() { |
| 519 |
h.Cursor.Start() |
| 520 |
} else { |
| 521 |
h.Cursor.StartOfText() |
| 522 |
} |
| 523 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 524 |
h.Relocate() |
| 525 |
return true |
| 526 |
} |
| 527 |
|
| 528 |
// SelectToStartOfLine selects to the start of the current line |
| 529 |
func (h *BufPane) SelectToStartOfLine() bool { |
| 530 |
if !h.Cursor.HasSelection() { |
| 531 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 532 |
} |
| 533 |
h.Cursor.Start() |
| 534 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 535 |
h.Relocate() |
| 536 |
return true |
| 537 |
} |
| 538 |
|
| 539 |
// SelectToEndOfLine selects to the end of the current line |
| 540 |
func (h *BufPane) SelectToEndOfLine() bool { |
| 541 |
if !h.Cursor.HasSelection() { |
| 542 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 543 |
} |
| 544 |
h.Cursor.End() |
| 545 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 546 |
h.Relocate() |
| 547 |
return true |
| 548 |
} |
| 549 |
|
| 550 |
func (h *BufPane) paragraphPrevious() { |
| 551 |
var line int |
| 552 |
// Skip to the first non-empty line |
| 553 |
for line = h.Cursor.Y; line > 0; line-- { |
| 554 |
if len(h.Buf.LineBytes(line)) != 0 { |
| 555 |
break |
| 556 |
} |
| 557 |
} |
| 558 |
// Find the first empty line |
| 559 |
for ; line > 0; line-- { |
| 560 |
if len(h.Buf.LineBytes(line)) == 0 { |
| 561 |
h.Cursor.X = 0 |
| 562 |
h.Cursor.Y = line |
| 563 |
break |
| 564 |
} |
| 565 |
} |
| 566 |
// If no empty line was found, move the cursor to the start of the buffer |
| 567 |
if line == 0 { |
| 568 |
h.Cursor.Loc = h.Buf.Start() |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
func (h *BufPane) paragraphNext() { |
| 573 |
var line int |
| 574 |
// Skip to the first non-empty line |
| 575 |
for line = h.Cursor.Y; line < h.Buf.LinesNum(); line++ { |
| 576 |
if len(h.Buf.LineBytes(line)) != 0 { |
| 577 |
break |
| 578 |
} |
| 579 |
} |
| 580 |
// Find the first empty line |
| 581 |
for ; line < h.Buf.LinesNum(); line++ { |
| 582 |
if len(h.Buf.LineBytes(line)) == 0 { |
| 583 |
h.Cursor.X = 0 |
| 584 |
h.Cursor.Y = line |
| 585 |
break |
| 586 |
} |
| 587 |
} |
| 588 |
// If no empty line was found, move the cursor to the end of the buffer |
| 589 |
if line == h.Buf.LinesNum() { |
| 590 |
h.Cursor.Loc = h.Buf.End() |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
// ParagraphPrevious moves the cursor to the first empty line that comes before |
| 595 |
// the paragraph closest to the cursor, or beginning of the buffer if there |
| 596 |
// isn't a paragraph |
| 597 |
func (h *BufPane) ParagraphPrevious() bool { |
| 598 |
h.Cursor.Deselect(true) |
| 599 |
h.paragraphPrevious() |
| 600 |
h.Relocate() |
| 601 |
return true |
| 602 |
} |
| 603 |
|
| 604 |
// ParagraphNext moves the cursor to the first empty line that comes after the |
| 605 |
// paragraph closest to the cursor, or end of the buffer if there isn't a |
| 606 |
// paragraph |
| 607 |
func (h *BufPane) ParagraphNext() bool { |
| 608 |
h.Cursor.Deselect(true) |
| 609 |
h.paragraphNext() |
| 610 |
h.Relocate() |
| 611 |
return true |
| 612 |
} |
| 613 |
|
| 614 |
// SelectToParagraphPrevious selects to the first empty line that comes before |
| 615 |
// the paragraph closest to the cursor, or beginning of the buffer if there |
| 616 |
// isn't a paragraph |
| 617 |
func (h *BufPane) SelectToParagraphPrevious() bool { |
| 618 |
if !h.Cursor.HasSelection() { |
| 619 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 620 |
} |
| 621 |
h.paragraphPrevious() |
| 622 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 623 |
h.Relocate() |
| 624 |
return true |
| 625 |
} |
| 626 |
|
| 627 |
// SelectToParagraphNext selects to the first empty line that comes after the |
| 628 |
// paragraph closest to the cursor, or end of the buffer if there isn't a |
| 629 |
// paragraph |
| 630 |
func (h *BufPane) SelectToParagraphNext() bool { |
| 631 |
if !h.Cursor.HasSelection() { |
| 632 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 633 |
} |
| 634 |
h.paragraphNext() |
| 635 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 636 |
h.Relocate() |
| 637 |
return true |
| 638 |
} |
| 639 |
|
| 640 |
// Retab changes all tabs to spaces or all spaces to tabs depending |
| 641 |
// on the user's settings |
| 642 |
func (h *BufPane) Retab() bool { |
| 643 |
h.Buf.Retab() |
| 644 |
h.Relocate() |
| 645 |
return true |
| 646 |
} |
| 647 |
|
| 648 |
// CursorStart moves the cursor to the start of the buffer |
| 649 |
func (h *BufPane) CursorStart() bool { |
| 650 |
h.Cursor.Deselect(true) |
| 651 |
h.Cursor.X = 0 |
| 652 |
h.Cursor.Y = 0 |
| 653 |
h.Cursor.StoreVisualX() |
| 654 |
h.Relocate() |
| 655 |
return true |
| 656 |
} |
| 657 |
|
| 658 |
// CursorEnd moves the cursor to the end of the buffer |
| 659 |
func (h *BufPane) CursorEnd() bool { |
| 660 |
h.Cursor.Deselect(true) |
| 661 |
h.Cursor.Loc = h.Buf.End() |
| 662 |
h.Cursor.StoreVisualX() |
| 663 |
h.Relocate() |
| 664 |
return true |
| 665 |
} |
| 666 |
|
| 667 |
// SelectToStart selects the text from the cursor to the start of the buffer |
| 668 |
func (h *BufPane) SelectToStart() bool { |
| 669 |
if !h.Cursor.HasSelection() { |
| 670 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 671 |
} |
| 672 |
h.CursorStart() |
| 673 |
h.Cursor.SelectTo(h.Buf.Start()) |
| 674 |
h.Relocate() |
| 675 |
return true |
| 676 |
} |
| 677 |
|
| 678 |
// SelectToEnd selects the text from the cursor to the end of the buffer |
| 679 |
func (h *BufPane) SelectToEnd() bool { |
| 680 |
if !h.Cursor.HasSelection() { |
| 681 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 682 |
} |
| 683 |
h.CursorEnd() |
| 684 |
h.Cursor.SelectTo(h.Buf.End()) |
| 685 |
h.Relocate() |
| 686 |
return true |
| 687 |
} |
| 688 |
|
| 689 |
// InsertNewline inserts a newline plus possible some whitespace if autoindent is on |
| 690 |
func (h *BufPane) InsertNewline() bool { |
| 691 |
// Insert a newline |
| 692 |
if h.Cursor.HasSelection() { |
| 693 |
h.Cursor.DeleteSelection() |
| 694 |
h.Cursor.ResetSelection() |
| 695 |
} |
| 696 |
|
| 697 |
ws := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y)) |
| 698 |
cx := h.Cursor.X |
| 699 |
h.Buf.Insert(h.Cursor.Loc, "\n") |
| 700 |
// h.Cursor.Right() |
| 701 |
|
| 702 |
if h.Buf.Settings["autoindent"].(bool) { |
| 703 |
if cx < len(ws) { |
| 704 |
ws = ws[0:cx] |
| 705 |
} |
| 706 |
h.Buf.Insert(h.Cursor.Loc, string(ws)) |
| 707 |
// for i := 0; i < len(ws); i++ { |
| 708 |
// h.Cursor.Right() |
| 709 |
// } |
| 710 |
|
| 711 |
// Remove the whitespaces if keepautoindent setting is off |
| 712 |
if util.IsSpacesOrTabs(h.Buf.LineBytes(h.Cursor.Y-1)) && !h.Buf.Settings["keepautoindent"].(bool) { |
| 713 |
line := h.Buf.LineBytes(h.Cursor.Y - 1) |
| 714 |
h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y - 1}, buffer.Loc{X: util.CharacterCount(line), Y: h.Cursor.Y - 1}) |
| 715 |
} |
| 716 |
} |
| 717 |
h.Cursor.StoreVisualX() |
| 718 |
h.Relocate() |
| 719 |
return true |
| 720 |
} |
| 721 |
|
| 722 |
// Backspace deletes the previous character |
| 723 |
func (h *BufPane) Backspace() bool { |
| 724 |
if h.Cursor.HasSelection() { |
| 725 |
h.Cursor.DeleteSelection() |
| 726 |
h.Cursor.ResetSelection() |
| 727 |
} else if h.Cursor.Loc.GreaterThan(h.Buf.Start()) { |
| 728 |
// We have to do something a bit hacky here because we want to |
| 729 |
// delete the line by first moving left and then deleting backwards |
| 730 |
// but the undo redo would place the cursor in the wrong place |
| 731 |
// So instead we move left, save the position, move back, delete |
| 732 |
// and restore the position |
| 733 |
|
| 734 |
// If the user is using spaces instead of tabs and they are deleting |
| 735 |
// whitespace at the start of the line, we should delete as if it's a |
| 736 |
// tab (tabSize number of spaces) |
| 737 |
lineStart := util.SliceStart(h.Buf.LineBytes(h.Cursor.Y), h.Cursor.X) |
| 738 |
tabSize := int(h.Buf.Settings["tabsize"].(float64)) |
| 739 |
if h.Buf.Settings["tabstospaces"].(bool) && util.IsSpaces(lineStart) && len(lineStart) != 0 && util.CharacterCount(lineStart)%tabSize == 0 { |
| 740 |
loc := h.Cursor.Loc |
| 741 |
h.Buf.Remove(loc.Move(-tabSize, h.Buf), loc) |
| 742 |
} else { |
| 743 |
loc := h.Cursor.Loc |
| 744 |
h.Buf.Remove(loc.Move(-1, h.Buf), loc) |
| 745 |
} |
| 746 |
} |
| 747 |
h.Cursor.StoreVisualX() |
| 748 |
h.Relocate() |
| 749 |
return true |
| 750 |
} |
| 751 |
|
| 752 |
// DeleteWordRight deletes the word to the right of the cursor |
| 753 |
func (h *BufPane) DeleteWordRight() bool { |
| 754 |
h.SelectWordRight() |
| 755 |
if h.Cursor.HasSelection() { |
| 756 |
h.Cursor.DeleteSelection() |
| 757 |
h.Cursor.ResetSelection() |
| 758 |
} |
| 759 |
h.Relocate() |
| 760 |
return true |
| 761 |
} |
| 762 |
|
| 763 |
// DeleteWordLeft deletes the word to the left of the cursor |
| 764 |
func (h *BufPane) DeleteWordLeft() bool { |
| 765 |
h.SelectWordLeft() |
| 766 |
if h.Cursor.HasSelection() { |
| 767 |
h.Cursor.DeleteSelection() |
| 768 |
h.Cursor.ResetSelection() |
| 769 |
} |
| 770 |
h.Relocate() |
| 771 |
return true |
| 772 |
} |
| 773 |
|
| 774 |
// DeleteSubWordRight deletes the sub-word to the right of the cursor |
| 775 |
func (h *BufPane) DeleteSubWordRight() bool { |
| 776 |
h.SelectSubWordRight() |
| 777 |
if h.Cursor.HasSelection() { |
| 778 |
h.Cursor.DeleteSelection() |
| 779 |
h.Cursor.ResetSelection() |
| 780 |
} |
| 781 |
h.Relocate() |
| 782 |
return true |
| 783 |
} |
| 784 |
|
| 785 |
// DeleteSubWordLeft deletes the sub-word to the left of the cursor |
| 786 |
func (h *BufPane) DeleteSubWordLeft() bool { |
| 787 |
h.SelectSubWordLeft() |
| 788 |
if h.Cursor.HasSelection() { |
| 789 |
h.Cursor.DeleteSelection() |
| 790 |
h.Cursor.ResetSelection() |
| 791 |
} |
| 792 |
h.Relocate() |
| 793 |
return true |
| 794 |
} |
| 795 |
|
| 796 |
// Delete deletes the next character |
| 797 |
func (h *BufPane) Delete() bool { |
| 798 |
if h.Cursor.HasSelection() { |
| 799 |
h.Cursor.DeleteSelection() |
| 800 |
h.Cursor.ResetSelection() |
| 801 |
} else { |
| 802 |
loc := h.Cursor.Loc |
| 803 |
if loc.LessThan(h.Buf.End()) { |
| 804 |
h.Buf.Remove(loc, loc.Move(1, h.Buf)) |
| 805 |
} |
| 806 |
} |
| 807 |
h.Relocate() |
| 808 |
return true |
| 809 |
} |
| 810 |
|
| 811 |
// IndentSelection indents the current selection |
| 812 |
func (h *BufPane) IndentSelection() bool { |
| 813 |
if h.Cursor.HasSelection() { |
| 814 |
start := h.Cursor.CurSelection[0] |
| 815 |
end := h.Cursor.CurSelection[1] |
| 816 |
if end.Y < start.Y { |
| 817 |
start, end = end, start |
| 818 |
h.Cursor.SetSelectionStart(start) |
| 819 |
h.Cursor.SetSelectionEnd(end) |
| 820 |
} |
| 821 |
|
| 822 |
startY := start.Y |
| 823 |
endY := end.Move(-1, h.Buf).Y |
| 824 |
endX := end.Move(-1, h.Buf).X |
| 825 |
tabsize := int(h.Buf.Settings["tabsize"].(float64)) |
| 826 |
indentsize := len(h.Buf.IndentString(tabsize)) |
| 827 |
for y := startY; y <= endY; y++ { |
| 828 |
if len(h.Buf.LineBytes(y)) > 0 { |
| 829 |
h.Buf.Insert(buffer.Loc{X: 0, Y: y}, h.Buf.IndentString(tabsize)) |
| 830 |
if y == startY && start.X > 0 { |
| 831 |
h.Cursor.SetSelectionStart(start.Move(indentsize, h.Buf)) |
| 832 |
} |
| 833 |
if y == endY { |
| 834 |
h.Cursor.SetSelectionEnd(buffer.Loc{X: endX + indentsize + 1, Y: endY}) |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
h.Buf.RelocateCursors() |
| 839 |
|
| 840 |
h.Relocate() |
| 841 |
return true |
| 842 |
} |
| 843 |
return false |
| 844 |
} |
| 845 |
|
| 846 |
// IndentLine moves the current line forward one indentation |
| 847 |
func (h *BufPane) IndentLine() bool { |
| 848 |
if h.Cursor.HasSelection() { |
| 849 |
return false |
| 850 |
} |
| 851 |
|
| 852 |
tabsize := int(h.Buf.Settings["tabsize"].(float64)) |
| 853 |
indentstr := h.Buf.IndentString(tabsize) |
| 854 |
h.Buf.Insert(buffer.Loc{X: 0, Y: h.Cursor.Y}, indentstr) |
| 855 |
h.Buf.RelocateCursors() |
| 856 |
h.Relocate() |
| 857 |
return true |
| 858 |
} |
| 859 |
|
| 860 |
// OutdentLine moves the current line back one indentation |
| 861 |
func (h *BufPane) OutdentLine() bool { |
| 862 |
if h.Cursor.HasSelection() { |
| 863 |
return false |
| 864 |
} |
| 865 |
|
| 866 |
for x := 0; x < len(h.Buf.IndentString(util.IntOpt(h.Buf.Settings["tabsize"]))); x++ { |
| 867 |
if len(util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))) == 0 { |
| 868 |
break |
| 869 |
} |
| 870 |
h.Buf.Remove(buffer.Loc{X: 0, Y: h.Cursor.Y}, buffer.Loc{X: 1, Y: h.Cursor.Y}) |
| 871 |
} |
| 872 |
h.Buf.RelocateCursors() |
| 873 |
h.Relocate() |
| 874 |
return true |
| 875 |
} |
| 876 |
|
| 877 |
// OutdentSelection takes the current selection and moves it back one indent level |
| 878 |
func (h *BufPane) OutdentSelection() bool { |
| 879 |
if h.Cursor.HasSelection() { |
| 880 |
start := h.Cursor.CurSelection[0] |
| 881 |
end := h.Cursor.CurSelection[1] |
| 882 |
if end.Y < start.Y { |
| 883 |
start, end = end, start |
| 884 |
h.Cursor.SetSelectionStart(start) |
| 885 |
h.Cursor.SetSelectionEnd(end) |
| 886 |
} |
| 887 |
|
| 888 |
startY := start.Y |
| 889 |
endY := end.Move(-1, h.Buf).Y |
| 890 |
for y := startY; y <= endY; y++ { |
| 891 |
for x := 0; x < len(h.Buf.IndentString(util.IntOpt(h.Buf.Settings["tabsize"]))); x++ { |
| 892 |
if len(util.GetLeadingWhitespace(h.Buf.LineBytes(y))) == 0 { |
| 893 |
break |
| 894 |
} |
| 895 |
h.Buf.Remove(buffer.Loc{X: 0, Y: y}, buffer.Loc{X: 1, Y: y}) |
| 896 |
} |
| 897 |
} |
| 898 |
h.Buf.RelocateCursors() |
| 899 |
|
| 900 |
h.Relocate() |
| 901 |
return true |
| 902 |
} |
| 903 |
return false |
| 904 |
} |
| 905 |
|
| 906 |
// Autocomplete cycles the suggestions and performs autocompletion if there are suggestions |
| 907 |
func (h *BufPane) Autocomplete() bool { |
| 908 |
b := h.Buf |
| 909 |
|
| 910 |
if h.Cursor.HasSelection() { |
| 911 |
return false |
| 912 |
} |
| 913 |
|
| 914 |
if b.HasSuggestions { |
| 915 |
b.CycleAutocomplete(true) |
| 916 |
return true |
| 917 |
} |
| 918 |
|
| 919 |
if h.Cursor.X == 0 { |
| 920 |
return false |
| 921 |
} |
| 922 |
r := h.Cursor.RuneUnder(h.Cursor.X) |
| 923 |
prev := h.Cursor.RuneUnder(h.Cursor.X - 1) |
| 924 |
if !util.IsAutocomplete(prev) || util.IsWordChar(r) { |
| 925 |
// don't autocomplete if cursor is within a word |
| 926 |
return false |
| 927 |
} |
| 928 |
|
| 929 |
return b.Autocomplete(buffer.BufferComplete) |
| 930 |
} |
| 931 |
|
| 932 |
// CycleAutocompleteBack cycles back in the autocomplete suggestion list |
| 933 |
func (h *BufPane) CycleAutocompleteBack() bool { |
| 934 |
if h.Cursor.HasSelection() { |
| 935 |
return false |
| 936 |
} |
| 937 |
|
| 938 |
if h.Buf.HasSuggestions { |
| 939 |
h.Buf.CycleAutocomplete(false) |
| 940 |
return true |
| 941 |
} |
| 942 |
return false |
| 943 |
} |
| 944 |
|
| 945 |
// InsertTab inserts a tab or spaces |
| 946 |
func (h *BufPane) InsertTab() bool { |
| 947 |
b := h.Buf |
| 948 |
indent := b.IndentString(util.IntOpt(b.Settings["tabsize"])) |
| 949 |
tabBytes := len(indent) |
| 950 |
bytesUntilIndent := tabBytes - (h.Cursor.GetVisualX(false) % tabBytes) |
| 951 |
b.Insert(h.Cursor.Loc, indent[:bytesUntilIndent]) |
| 952 |
h.Relocate() |
| 953 |
return true |
| 954 |
} |
| 955 |
|
| 956 |
// SaveAll saves all open buffers |
| 957 |
func (h *BufPane) SaveAll() bool { |
| 958 |
for _, b := range buffer.OpenBuffers { |
| 959 |
b.Save() |
| 960 |
} |
| 961 |
return true |
| 962 |
} |
| 963 |
|
| 964 |
// SaveCB performs a save and does a callback at the very end (after all prompts have been resolved) |
| 965 |
func (h *BufPane) SaveCB(action string, callback func()) bool { |
| 966 |
// If this is an empty buffer, ask for a filename |
| 967 |
if h.Buf.Path == "" { |
| 968 |
h.SaveAsCB(action, callback) |
| 969 |
} else { |
| 970 |
noPrompt := h.saveBufToFile(h.Buf.Path, action, callback) |
| 971 |
if noPrompt { |
| 972 |
return true |
| 973 |
} |
| 974 |
} |
| 975 |
return false |
| 976 |
} |
| 977 |
|
| 978 |
// Save the buffer to disk |
| 979 |
func (h *BufPane) Save() bool { |
| 980 |
return h.SaveCB("Save", nil) |
| 981 |
} |
| 982 |
|
| 983 |
// SaveAsCB performs a save as and does a callback at the very end (after all prompts have been resolved) |
| 984 |
// The callback is only called if the save was successful |
| 985 |
func (h *BufPane) SaveAsCB(action string, callback func()) bool { |
| 986 |
InfoBar.Prompt("Filename: ", "", "Save", nil, func(resp string, canceled bool) { |
| 987 |
if !canceled { |
| 988 |
// the filename might or might not be quoted, so unquote first then join the strings. |
| 989 |
args, err := shellquote.Split(resp) |
| 990 |
if err != nil { |
| 991 |
InfoBar.Error("Error parsing arguments: ", err) |
| 992 |
return |
| 993 |
} |
| 994 |
if len(args) == 0 { |
| 995 |
InfoBar.Error("No filename given") |
| 996 |
return |
| 997 |
} |
| 998 |
filename := strings.Join(args, " ") |
| 999 |
fileinfo, err := os.Stat(filename) |
| 1000 |
if err != nil { |
| 1001 |
if errors.Is(err, fs.ErrNotExist) || errors.Is(err, fs.ErrPermission) { |
| 1002 |
noPrompt := h.saveBufToFile(filename, action, callback) |
| 1003 |
if noPrompt { |
| 1004 |
h.completeAction(action) |
| 1005 |
return |
| 1006 |
} |
| 1007 |
} else { |
| 1008 |
InfoBar.Error(err) |
| 1009 |
return |
| 1010 |
} |
| 1011 |
} else { |
| 1012 |
InfoBar.YNPrompt( |
| 1013 |
fmt.Sprintf("The file %s already exists in the directory, would you like to overwrite? Y/n", fileinfo.Name()), |
| 1014 |
func(yes, canceled bool) { |
| 1015 |
if yes && !canceled { |
| 1016 |
noPrompt := h.saveBufToFile(filename, action, callback) |
| 1017 |
if noPrompt { |
| 1018 |
h.completeAction(action) |
| 1019 |
} |
| 1020 |
} |
| 1021 |
}, |
| 1022 |
) |
| 1023 |
} |
| 1024 |
} |
| 1025 |
}) |
| 1026 |
return false |
| 1027 |
} |
| 1028 |
|
| 1029 |
// SaveAs saves the buffer to disk with the given name |
| 1030 |
func (h *BufPane) SaveAs() bool { |
| 1031 |
return h.SaveAsCB("SaveAs", nil) |
| 1032 |
} |
| 1033 |
|
| 1034 |
// This function saves the buffer to `filename` and changes the buffer's path and name |
| 1035 |
// to `filename` if the save is successful |
| 1036 |
// The callback is only called if the save was successful |
| 1037 |
func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool { |
| 1038 |
err := h.Buf.SaveAs(filename) |
| 1039 |
if err != nil { |
| 1040 |
if errors.Is(err, fs.ErrPermission) { |
| 1041 |
if runtime.GOOS == "windows" { |
| 1042 |
InfoBar.Error("Permission denied. Save with sudo not supported on Windows") |
| 1043 |
return true |
| 1044 |
} |
| 1045 |
|
| 1046 |
saveWithSudo := func() { |
| 1047 |
err = h.Buf.SaveAsWithSudo(filename) |
| 1048 |
if err != nil { |
| 1049 |
InfoBar.Error(err) |
| 1050 |
} else { |
| 1051 |
InfoBar.Message("Saved " + filename) |
| 1052 |
if callback != nil { |
| 1053 |
callback() |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} |
| 1057 |
if h.Buf.Settings["autosu"].(bool) { |
| 1058 |
saveWithSudo() |
| 1059 |
} else { |
| 1060 |
InfoBar.YNPrompt( |
| 1061 |
fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)), |
| 1062 |
func(yes, canceled bool) { |
| 1063 |
if yes && !canceled { |
| 1064 |
saveWithSudo() |
| 1065 |
h.completeAction(action) |
| 1066 |
} |
| 1067 |
}, |
| 1068 |
) |
| 1069 |
return false |
| 1070 |
} |
| 1071 |
} else { |
| 1072 |
InfoBar.Error(err) |
| 1073 |
} |
| 1074 |
} else { |
| 1075 |
InfoBar.Message("Saved " + filename) |
| 1076 |
if callback != nil { |
| 1077 |
callback() |
| 1078 |
} |
| 1079 |
} |
| 1080 |
return true |
| 1081 |
} |
| 1082 |
|
| 1083 |
// Find opens a prompt and searches forward for the input |
| 1084 |
func (h *BufPane) Find() bool { |
| 1085 |
return h.find(true) |
| 1086 |
} |
| 1087 |
|
| 1088 |
// FindLiteral is the same as Find() but does not support regular expressions |
| 1089 |
func (h *BufPane) FindLiteral() bool { |
| 1090 |
return h.find(false) |
| 1091 |
} |
| 1092 |
|
| 1093 |
// Search searches for a given string/regex in the buffer and selects the next |
| 1094 |
// match if a match is found |
| 1095 |
// This function behaves the same way as Find and FindLiteral actions: |
| 1096 |
// it affects the buffer's LastSearch and LastSearchRegex (saved searches) |
| 1097 |
// for use with FindNext and FindPrevious, and turns HighlightSearch on or off |
| 1098 |
// according to hlsearch setting |
| 1099 |
func (h *BufPane) Search(str string, useRegex bool, searchDown bool) error { |
| 1100 |
match, found, err := h.Buf.FindNext(str, h.Buf.Start(), h.Buf.End(), h.Cursor.Loc, searchDown, useRegex) |
| 1101 |
if err != nil { |
| 1102 |
return err |
| 1103 |
} |
| 1104 |
if found { |
| 1105 |
h.Cursor.SetSelectionStart(match[0]) |
| 1106 |
h.Cursor.SetSelectionEnd(match[1]) |
| 1107 |
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] |
| 1108 |
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] |
| 1109 |
h.GotoLoc(h.Cursor.CurSelection[1]) |
| 1110 |
h.Buf.LastSearch = str |
| 1111 |
h.Buf.LastSearchRegex = useRegex |
| 1112 |
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool) |
| 1113 |
} else { |
| 1114 |
h.Cursor.ResetSelection() |
| 1115 |
} |
| 1116 |
return nil |
| 1117 |
} |
| 1118 |
|
| 1119 |
func (h *BufPane) find(useRegex bool) bool { |
| 1120 |
h.searchOrig = h.Cursor.Loc |
| 1121 |
prompt := "Find: " |
| 1122 |
if useRegex { |
| 1123 |
prompt = "Find (regex): " |
| 1124 |
} |
| 1125 |
var eventCallback func(resp string) |
| 1126 |
if h.Buf.Settings["incsearch"].(bool) { |
| 1127 |
eventCallback = func(resp string) { |
| 1128 |
match, found, _ := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, useRegex) |
| 1129 |
if found { |
| 1130 |
h.Cursor.SetSelectionStart(match[0]) |
| 1131 |
h.Cursor.SetSelectionEnd(match[1]) |
| 1132 |
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] |
| 1133 |
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] |
| 1134 |
h.GotoLoc(match[1]) |
| 1135 |
} else { |
| 1136 |
h.GotoLoc(h.searchOrig) |
| 1137 |
h.Cursor.ResetSelection() |
| 1138 |
} |
| 1139 |
} |
| 1140 |
} |
| 1141 |
findCallback := func(resp string, canceled bool) { |
| 1142 |
// Finished callback |
| 1143 |
if !canceled { |
| 1144 |
match, found, err := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, useRegex) |
| 1145 |
if err != nil { |
| 1146 |
InfoBar.Error(err) |
| 1147 |
} else if found { |
| 1148 |
h.Cursor.SetSelectionStart(match[0]) |
| 1149 |
h.Cursor.SetSelectionEnd(match[1]) |
| 1150 |
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] |
| 1151 |
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] |
| 1152 |
h.GotoLoc(h.Cursor.CurSelection[1]) |
| 1153 |
h.Buf.LastSearch = resp |
| 1154 |
h.Buf.LastSearchRegex = useRegex |
| 1155 |
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool) |
| 1156 |
} else { |
| 1157 |
h.Cursor.ResetSelection() |
| 1158 |
InfoBar.Message("No matches found") |
| 1159 |
} |
| 1160 |
} else { |
| 1161 |
h.Cursor.ResetSelection() |
| 1162 |
} |
| 1163 |
} |
| 1164 |
pattern := string(h.Cursor.GetSelection()) |
| 1165 |
if useRegex && pattern != "" { |
| 1166 |
pattern = regexp.QuoteMeta(pattern) |
| 1167 |
} |
| 1168 |
if eventCallback != nil && pattern != "" { |
| 1169 |
eventCallback(pattern) |
| 1170 |
} |
| 1171 |
InfoBar.Prompt(prompt, pattern, "Find", eventCallback, findCallback) |
| 1172 |
if pattern != "" { |
| 1173 |
InfoBar.SelectAll() |
| 1174 |
} |
| 1175 |
return true |
| 1176 |
} |
| 1177 |
|
| 1178 |
// ToggleHighlightSearch toggles highlighting all instances of the last used search term |
| 1179 |
func (h *BufPane) ToggleHighlightSearch() bool { |
| 1180 |
h.Buf.HighlightSearch = !h.Buf.HighlightSearch |
| 1181 |
return true |
| 1182 |
} |
| 1183 |
|
| 1184 |
// UnhighlightSearch unhighlights all instances of the last used search term |
| 1185 |
func (h *BufPane) UnhighlightSearch() bool { |
| 1186 |
if !h.Buf.HighlightSearch { |
| 1187 |
return false |
| 1188 |
} |
| 1189 |
h.Buf.HighlightSearch = false |
| 1190 |
return true |
| 1191 |
} |
| 1192 |
|
| 1193 |
// ResetSearch resets the last used search term |
| 1194 |
func (h *BufPane) ResetSearch() bool { |
| 1195 |
if h.Buf.LastSearch != "" { |
| 1196 |
h.Buf.LastSearch = "" |
| 1197 |
return true |
| 1198 |
} |
| 1199 |
return false |
| 1200 |
} |
| 1201 |
|
| 1202 |
// FindNext searches forwards for the last used search term |
| 1203 |
func (h *BufPane) FindNext() bool { |
| 1204 |
if h.Buf.LastSearch == "" { |
| 1205 |
return false |
| 1206 |
} |
| 1207 |
// If the cursor is at the start of a selection and we search we want |
| 1208 |
// to search from the end of the selection in the case that |
| 1209 |
// the selection is a search result in which case we wouldn't move at |
| 1210 |
// at all which would be bad |
| 1211 |
searchLoc := h.Cursor.Loc |
| 1212 |
if h.Cursor.HasSelection() { |
| 1213 |
searchLoc = h.Cursor.CurSelection[1] |
| 1214 |
} |
| 1215 |
match, found, err := h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, true, h.Buf.LastSearchRegex) |
| 1216 |
if err != nil { |
| 1217 |
InfoBar.Error(err) |
| 1218 |
} else if found && searchLoc == match[0] && match[0] == match[1] { |
| 1219 |
// skip empty match at present cursor location |
| 1220 |
if searchLoc == h.Buf.End() { |
| 1221 |
searchLoc = h.Buf.Start() |
| 1222 |
} else { |
| 1223 |
searchLoc = searchLoc.Move(1, h.Buf) |
| 1224 |
} |
| 1225 |
match, found, _ = h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, true, h.Buf.LastSearchRegex) |
| 1226 |
} |
| 1227 |
if found { |
| 1228 |
h.Cursor.SetSelectionStart(match[0]) |
| 1229 |
h.Cursor.SetSelectionEnd(match[1]) |
| 1230 |
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] |
| 1231 |
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] |
| 1232 |
h.GotoLoc(h.Cursor.CurSelection[1]) |
| 1233 |
} else { |
| 1234 |
h.Cursor.ResetSelection() |
| 1235 |
} |
| 1236 |
return true |
| 1237 |
} |
| 1238 |
|
| 1239 |
// FindPrevious searches backwards for the last used search term |
| 1240 |
func (h *BufPane) FindPrevious() bool { |
| 1241 |
if h.Buf.LastSearch == "" { |
| 1242 |
return false |
| 1243 |
} |
| 1244 |
// If the cursor is at the end of a selection and we search we want |
| 1245 |
// to search from the beginning of the selection in the case that |
| 1246 |
// the selection is a search result in which case we wouldn't move at |
| 1247 |
// at all which would be bad |
| 1248 |
searchLoc := h.Cursor.Loc |
| 1249 |
if h.Cursor.HasSelection() { |
| 1250 |
searchLoc = h.Cursor.CurSelection[0] |
| 1251 |
} |
| 1252 |
match, found, err := h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, false, h.Buf.LastSearchRegex) |
| 1253 |
if err != nil { |
| 1254 |
InfoBar.Error(err) |
| 1255 |
} else if found && searchLoc == match[0] && match[0] == match[1] { |
| 1256 |
// skip empty match at present cursor location |
| 1257 |
if searchLoc == h.Buf.Start() { |
| 1258 |
searchLoc = h.Buf.End() |
| 1259 |
} else { |
| 1260 |
searchLoc = searchLoc.Move(-1, h.Buf) |
| 1261 |
} |
| 1262 |
match, found, _ = h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, false, h.Buf.LastSearchRegex) |
| 1263 |
} |
| 1264 |
if found { |
| 1265 |
h.Cursor.SetSelectionStart(match[0]) |
| 1266 |
h.Cursor.SetSelectionEnd(match[1]) |
| 1267 |
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] |
| 1268 |
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] |
| 1269 |
h.GotoLoc(h.Cursor.CurSelection[1]) |
| 1270 |
} else { |
| 1271 |
h.Cursor.ResetSelection() |
| 1272 |
} |
| 1273 |
return true |
| 1274 |
} |
| 1275 |
|
| 1276 |
// DiffNext searches forward until the beginning of the next block of diffs |
| 1277 |
func (h *BufPane) DiffNext() bool { |
| 1278 |
cur := h.Cursor.Loc.Y |
| 1279 |
dl, err := h.Buf.FindNextDiffLine(cur, true) |
| 1280 |
if err != nil { |
| 1281 |
return false |
| 1282 |
} |
| 1283 |
h.GotoLoc(buffer.Loc{0, dl}) |
| 1284 |
return true |
| 1285 |
} |
| 1286 |
|
| 1287 |
// DiffPrevious searches forward until the end of the previous block of diffs |
| 1288 |
func (h *BufPane) DiffPrevious() bool { |
| 1289 |
cur := h.Cursor.Loc.Y |
| 1290 |
dl, err := h.Buf.FindNextDiffLine(cur, false) |
| 1291 |
if err != nil { |
| 1292 |
return false |
| 1293 |
} |
| 1294 |
h.GotoLoc(buffer.Loc{0, dl}) |
| 1295 |
return true |
| 1296 |
} |
| 1297 |
|
| 1298 |
// Undo undoes the last action |
| 1299 |
func (h *BufPane) Undo() bool { |
| 1300 |
if !h.Buf.Undo() { |
| 1301 |
return false |
| 1302 |
} |
| 1303 |
InfoBar.Message("Undid action") |
| 1304 |
h.Relocate() |
| 1305 |
return true |
| 1306 |
} |
| 1307 |
|
| 1308 |
// Redo redoes the last action |
| 1309 |
func (h *BufPane) Redo() bool { |
| 1310 |
if !h.Buf.Redo() { |
| 1311 |
return false |
| 1312 |
} |
| 1313 |
InfoBar.Message("Redid action") |
| 1314 |
h.Relocate() |
| 1315 |
return true |
| 1316 |
} |
| 1317 |
|
| 1318 |
func (h *BufPane) selectLines() int { |
| 1319 |
if h.Cursor.HasSelection() { |
| 1320 |
start := h.Cursor.CurSelection[0] |
| 1321 |
end := h.Cursor.CurSelection[1] |
| 1322 |
if start.GreaterThan(end) { |
| 1323 |
start, end = end, start |
| 1324 |
} |
| 1325 |
if end.X == 0 { |
| 1326 |
end = end.Move(-1, h.Buf) |
| 1327 |
} |
| 1328 |
|
| 1329 |
h.Cursor.Deselect(true) |
| 1330 |
h.Cursor.SetSelectionStart(buffer.Loc{0, start.Y}) |
| 1331 |
h.Cursor.SetSelectionEnd(buffer.Loc{0, end.Y + 1}) |
| 1332 |
} else { |
| 1333 |
h.Cursor.SelectLine() |
| 1334 |
} |
| 1335 |
|
| 1336 |
nlines := h.Cursor.CurSelection[1].Y - h.Cursor.CurSelection[0].Y |
| 1337 |
if nlines == 0 && h.Cursor.HasSelection() { |
| 1338 |
// selected last line and it is not empty |
| 1339 |
nlines++ |
| 1340 |
} |
| 1341 |
return nlines |
| 1342 |
} |
| 1343 |
|
| 1344 |
// Copy the selection to the system clipboard |
| 1345 |
func (h *BufPane) Copy() bool { |
| 1346 |
if !h.Cursor.HasSelection() { |
| 1347 |
return false |
| 1348 |
} |
| 1349 |
h.Cursor.CopySelection(clipboard.ClipboardReg) |
| 1350 |
h.freshClip = false |
| 1351 |
InfoBar.Message("Copied selection") |
| 1352 |
h.Relocate() |
| 1353 |
return true |
| 1354 |
} |
| 1355 |
|
| 1356 |
// CopyLine copies the current line to the clipboard. If there is a selection, |
| 1357 |
// CopyLine copies all the lines that are (fully or partially) in the selection. |
| 1358 |
func (h *BufPane) CopyLine() bool { |
| 1359 |
origLoc := h.Cursor.Loc |
| 1360 |
origLastVisualX := h.Cursor.LastVisualX |
| 1361 |
origLastWrappedVisualX := h.Cursor.LastWrappedVisualX |
| 1362 |
origSelection := h.Cursor.CurSelection |
| 1363 |
|
| 1364 |
nlines := h.selectLines() |
| 1365 |
if nlines == 0 { |
| 1366 |
return false |
| 1367 |
} |
| 1368 |
h.Cursor.CopySelection(clipboard.ClipboardReg) |
| 1369 |
h.freshClip = false |
| 1370 |
if nlines > 1 { |
| 1371 |
InfoBar.Message(fmt.Sprintf("Copied %d lines", nlines)) |
| 1372 |
} else { |
| 1373 |
InfoBar.Message("Copied line") |
| 1374 |
} |
| 1375 |
|
| 1376 |
h.Cursor.Loc = origLoc |
| 1377 |
h.Cursor.LastVisualX = origLastVisualX |
| 1378 |
h.Cursor.LastWrappedVisualX = origLastWrappedVisualX |
| 1379 |
h.Cursor.CurSelection = origSelection |
| 1380 |
h.Relocate() |
| 1381 |
return true |
| 1382 |
} |
| 1383 |
|
| 1384 |
// Cut the selection to the system clipboard |
| 1385 |
func (h *BufPane) Cut() bool { |
| 1386 |
if !h.Cursor.HasSelection() { |
| 1387 |
return false |
| 1388 |
} |
| 1389 |
h.Cursor.CopySelection(clipboard.ClipboardReg) |
| 1390 |
h.Cursor.DeleteSelection() |
| 1391 |
h.Cursor.ResetSelection() |
| 1392 |
h.freshClip = false |
| 1393 |
InfoBar.Message("Cut selection") |
| 1394 |
|
| 1395 |
h.Relocate() |
| 1396 |
return true |
| 1397 |
} |
| 1398 |
|
| 1399 |
// CutLine cuts the current line to the clipboard. If there is a selection, |
| 1400 |
// CutLine cuts all the lines that are (fully or partially) in the selection. |
| 1401 |
func (h *BufPane) CutLine() bool { |
| 1402 |
nlines := h.selectLines() |
| 1403 |
if nlines == 0 { |
| 1404 |
return false |
| 1405 |
} |
| 1406 |
totalLines := nlines |
| 1407 |
if h.freshClip { |
| 1408 |
if clip, err := clipboard.Read(clipboard.ClipboardReg); err != nil { |
| 1409 |
InfoBar.Error(err) |
| 1410 |
return false |
| 1411 |
} else { |
| 1412 |
clipboard.WriteMulti(clip+string(h.Cursor.GetSelection()), clipboard.ClipboardReg, h.Cursor.Num, h.Buf.NumCursors()) |
| 1413 |
totalLines = strings.Count(clip, "\n") + nlines |
| 1414 |
} |
| 1415 |
} else { |
| 1416 |
h.Cursor.CopySelection(clipboard.ClipboardReg) |
| 1417 |
} |
| 1418 |
h.freshClip = true |
| 1419 |
h.Cursor.DeleteSelection() |
| 1420 |
h.Cursor.ResetSelection() |
| 1421 |
h.Cursor.StoreVisualX() |
| 1422 |
if totalLines > 1 { |
| 1423 |
InfoBar.Message(fmt.Sprintf("Cut %d lines", totalLines)) |
| 1424 |
} else { |
| 1425 |
InfoBar.Message("Cut line") |
| 1426 |
} |
| 1427 |
h.Relocate() |
| 1428 |
return true |
| 1429 |
} |
| 1430 |
|
| 1431 |
// Duplicate the selection |
| 1432 |
func (h *BufPane) Duplicate() bool { |
| 1433 |
if !h.Cursor.HasSelection() { |
| 1434 |
return false |
| 1435 |
} |
| 1436 |
h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection())) |
| 1437 |
InfoBar.Message("Duplicated selection") |
| 1438 |
h.Relocate() |
| 1439 |
return true |
| 1440 |
} |
| 1441 |
|
| 1442 |
// DuplicateLine duplicates the current line. If there is a selection, DuplicateLine |
| 1443 |
// duplicates all the lines that are (fully or partially) in the selection. |
| 1444 |
func (h *BufPane) DuplicateLine() bool { |
| 1445 |
if h.Cursor.HasSelection() { |
| 1446 |
origLoc := h.Cursor.Loc |
| 1447 |
origLastVisualX := h.Cursor.LastVisualX |
| 1448 |
origLastWrappedVisualX := h.Cursor.LastWrappedVisualX |
| 1449 |
origSelection := h.Cursor.CurSelection |
| 1450 |
|
| 1451 |
start := h.Cursor.CurSelection[0] |
| 1452 |
end := h.Cursor.CurSelection[1] |
| 1453 |
if start.GreaterThan(end) { |
| 1454 |
start, end = end, start |
| 1455 |
} |
| 1456 |
if end.X == 0 { |
| 1457 |
end = end.Move(-1, h.Buf) |
| 1458 |
} |
| 1459 |
|
| 1460 |
h.Cursor.Deselect(true) |
| 1461 |
h.Cursor.Loc = end |
| 1462 |
h.Cursor.End() |
| 1463 |
for y := start.Y; y <= end.Y; y++ { |
| 1464 |
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(y))) |
| 1465 |
} |
| 1466 |
|
| 1467 |
h.Cursor.Loc = origLoc |
| 1468 |
h.Cursor.LastVisualX = origLastVisualX |
| 1469 |
h.Cursor.LastWrappedVisualX = origLastWrappedVisualX |
| 1470 |
h.Cursor.CurSelection = origSelection |
| 1471 |
|
| 1472 |
if start.Y < end.Y { |
| 1473 |
InfoBar.Message(fmt.Sprintf("Duplicated %d lines", end.Y-start.Y+1)) |
| 1474 |
} else { |
| 1475 |
InfoBar.Message("Duplicated line") |
| 1476 |
} |
| 1477 |
} else { |
| 1478 |
h.Cursor.End() |
| 1479 |
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y))) |
| 1480 |
InfoBar.Message("Duplicated line") |
| 1481 |
} |
| 1482 |
h.Relocate() |
| 1483 |
return true |
| 1484 |
} |
| 1485 |
|
| 1486 |
// DeleteLine deletes the current line. If there is a selection, DeleteLine |
| 1487 |
// deletes all the lines that are (fully or partially) in the selection. |
| 1488 |
func (h *BufPane) DeleteLine() bool { |
| 1489 |
nlines := h.selectLines() |
| 1490 |
if nlines == 0 { |
| 1491 |
return false |
| 1492 |
} |
| 1493 |
h.Cursor.DeleteSelection() |
| 1494 |
h.Cursor.ResetSelection() |
| 1495 |
h.Cursor.StoreVisualX() |
| 1496 |
if nlines > 1 { |
| 1497 |
InfoBar.Message(fmt.Sprintf("Deleted %d lines", nlines)) |
| 1498 |
} else { |
| 1499 |
InfoBar.Message("Deleted line") |
| 1500 |
} |
| 1501 |
h.Relocate() |
| 1502 |
return true |
| 1503 |
} |
| 1504 |
|
| 1505 |
// MoveLinesUp moves up the current line or selected lines if any |
| 1506 |
func (h *BufPane) MoveLinesUp() bool { |
| 1507 |
if h.Cursor.HasSelection() { |
| 1508 |
if h.Cursor.CurSelection[0].Y == 0 { |
| 1509 |
InfoBar.Message("Cannot move further up") |
| 1510 |
return false |
| 1511 |
} |
| 1512 |
start := h.Cursor.CurSelection[0].Y |
| 1513 |
end := h.Cursor.CurSelection[1].Y |
| 1514 |
sel := 1 |
| 1515 |
if start > end { |
| 1516 |
end, start = start, end |
| 1517 |
sel = 0 |
| 1518 |
} |
| 1519 |
|
| 1520 |
compensate := false |
| 1521 |
if h.Cursor.CurSelection[sel].X != 0 { |
| 1522 |
end++ |
| 1523 |
} else { |
| 1524 |
compensate = true |
| 1525 |
} |
| 1526 |
|
| 1527 |
h.Buf.MoveLinesUp( |
| 1528 |
start, |
| 1529 |
end, |
| 1530 |
) |
| 1531 |
if compensate { |
| 1532 |
h.Cursor.CurSelection[sel].Y -= 1 |
| 1533 |
} |
| 1534 |
} else { |
| 1535 |
if h.Cursor.Loc.Y == 0 { |
| 1536 |
InfoBar.Message("Cannot move further up") |
| 1537 |
return false |
| 1538 |
} |
| 1539 |
h.Buf.MoveLinesUp( |
| 1540 |
h.Cursor.Loc.Y, |
| 1541 |
h.Cursor.Loc.Y+1, |
| 1542 |
) |
| 1543 |
} |
| 1544 |
|
| 1545 |
h.Relocate() |
| 1546 |
return true |
| 1547 |
} |
| 1548 |
|
| 1549 |
// MoveLinesDown moves down the current line or selected lines if any |
| 1550 |
func (h *BufPane) MoveLinesDown() bool { |
| 1551 |
if h.Cursor.HasSelection() { |
| 1552 |
if h.Cursor.CurSelection[1].Y >= h.Buf.LinesNum() { |
| 1553 |
InfoBar.Message("Cannot move further down") |
| 1554 |
return false |
| 1555 |
} |
| 1556 |
start := h.Cursor.CurSelection[0].Y |
| 1557 |
end := h.Cursor.CurSelection[1].Y |
| 1558 |
sel := 1 |
| 1559 |
if start > end { |
| 1560 |
end, start = start, end |
| 1561 |
sel = 0 |
| 1562 |
} |
| 1563 |
|
| 1564 |
if h.Cursor.CurSelection[sel].X != 0 { |
| 1565 |
end++ |
| 1566 |
} |
| 1567 |
|
| 1568 |
h.Buf.MoveLinesDown( |
| 1569 |
start, |
| 1570 |
end, |
| 1571 |
) |
| 1572 |
} else { |
| 1573 |
if h.Cursor.Loc.Y >= h.Buf.LinesNum()-1 { |
| 1574 |
InfoBar.Message("Cannot move further down") |
| 1575 |
return false |
| 1576 |
} |
| 1577 |
h.Buf.MoveLinesDown( |
| 1578 |
h.Cursor.Loc.Y, |
| 1579 |
h.Cursor.Loc.Y+1, |
| 1580 |
) |
| 1581 |
} |
| 1582 |
|
| 1583 |
h.Relocate() |
| 1584 |
return true |
| 1585 |
} |
| 1586 |
|
| 1587 |
// Paste whatever is in the system clipboard into the buffer |
| 1588 |
// Delete and paste if the user has a selection |
| 1589 |
func (h *BufPane) Paste() bool { |
| 1590 |
clip, err := clipboard.ReadMulti(clipboard.ClipboardReg, h.Cursor.Num, h.Buf.NumCursors()) |
| 1591 |
if err != nil { |
| 1592 |
InfoBar.Error(err) |
| 1593 |
} else { |
| 1594 |
h.paste(clip) |
| 1595 |
} |
| 1596 |
h.Relocate() |
| 1597 |
return true |
| 1598 |
} |
| 1599 |
|
| 1600 |
// PastePrimary pastes from the primary clipboard (only use on linux) |
| 1601 |
func (h *BufPane) PastePrimary() bool { |
| 1602 |
clip, err := clipboard.ReadMulti(clipboard.PrimaryReg, h.Cursor.Num, h.Buf.NumCursors()) |
| 1603 |
if err != nil { |
| 1604 |
InfoBar.Error(err) |
| 1605 |
} else { |
| 1606 |
h.paste(clip) |
| 1607 |
} |
| 1608 |
h.Relocate() |
| 1609 |
return true |
| 1610 |
} |
| 1611 |
|
| 1612 |
func (h *BufPane) paste(clip string) { |
| 1613 |
if h.Buf.Settings["smartpaste"].(bool) { |
| 1614 |
if h.Cursor.X > 0 { |
| 1615 |
leadingPasteWS := string(util.GetLeadingWhitespace([]byte(clip))) |
| 1616 |
if leadingPasteWS != " " && strings.Contains(clip, "\n"+leadingPasteWS) { |
| 1617 |
leadingWS := string(util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))) |
| 1618 |
clip = strings.TrimPrefix(clip, leadingPasteWS) |
| 1619 |
clip = strings.ReplaceAll(clip, "\n"+leadingPasteWS, "\n"+leadingWS) |
| 1620 |
} |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
if h.Cursor.HasSelection() { |
| 1625 |
h.Cursor.DeleteSelection() |
| 1626 |
h.Cursor.ResetSelection() |
| 1627 |
} |
| 1628 |
|
| 1629 |
h.Buf.Insert(h.Cursor.Loc, clip) |
| 1630 |
// h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf) |
| 1631 |
h.freshClip = false |
| 1632 |
InfoBar.Message("Pasted clipboard") |
| 1633 |
} |
| 1634 |
|
| 1635 |
// JumpToMatchingBrace moves the cursor to the matching brace if it is |
| 1636 |
// currently on a brace |
| 1637 |
func (h *BufPane) JumpToMatchingBrace() bool { |
| 1638 |
matchingBrace, left, found := h.Buf.FindMatchingBrace(h.Cursor.Loc) |
| 1639 |
if found { |
| 1640 |
if h.Buf.Settings["matchbraceleft"].(bool) { |
| 1641 |
if left { |
| 1642 |
h.Cursor.GotoLoc(matchingBrace) |
| 1643 |
} else { |
| 1644 |
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf)) |
| 1645 |
} |
| 1646 |
} else { |
| 1647 |
h.Cursor.GotoLoc(matchingBrace) |
| 1648 |
} |
| 1649 |
h.Relocate() |
| 1650 |
return true |
| 1651 |
} |
| 1652 |
return false |
| 1653 |
} |
| 1654 |
|
| 1655 |
// SelectAll selects the entire buffer |
| 1656 |
func (h *BufPane) SelectAll() bool { |
| 1657 |
h.Cursor.SetSelectionStart(h.Buf.Start()) |
| 1658 |
h.Cursor.SetSelectionEnd(h.Buf.End()) |
| 1659 |
// Put the cursor at the beginning |
| 1660 |
h.Cursor.X = 0 |
| 1661 |
h.Cursor.Y = 0 |
| 1662 |
h.Relocate() |
| 1663 |
return true |
| 1664 |
} |
| 1665 |
|
| 1666 |
// OpenFile opens a new file in the buffer |
| 1667 |
func (h *BufPane) OpenFile() bool { |
| 1668 |
InfoBar.Prompt("> ", "open ", "Open", nil, func(resp string, canceled bool) { |
| 1669 |
if !canceled { |
| 1670 |
h.HandleCommand(resp) |
| 1671 |
} |
| 1672 |
}) |
| 1673 |
return true |
| 1674 |
} |
| 1675 |
|
| 1676 |
// JumpLine asks the user to enter a line number to jump to |
| 1677 |
func (h *BufPane) JumpLine() bool { |
| 1678 |
InfoBar.Prompt("> ", "goto ", "Command", nil, func(resp string, canceled bool) { |
| 1679 |
if !canceled { |
| 1680 |
h.HandleCommand(resp) |
| 1681 |
} |
| 1682 |
}) |
| 1683 |
return true |
| 1684 |
} |
| 1685 |
|
| 1686 |
// Start moves the viewport to the start of the buffer |
| 1687 |
func (h *BufPane) Start() bool { |
| 1688 |
v := h.GetView() |
| 1689 |
v.StartLine = display.SLoc{0, 0} |
| 1690 |
h.SetView(v) |
| 1691 |
return true |
| 1692 |
} |
| 1693 |
|
| 1694 |
// End moves the viewport to the end of the buffer |
| 1695 |
func (h *BufPane) End() bool { |
| 1696 |
v := h.GetView() |
| 1697 |
v.StartLine = h.Scroll(h.SLocFromLoc(h.Buf.End()), -h.BufView().Height+1) |
| 1698 |
h.SetView(v) |
| 1699 |
return true |
| 1700 |
} |
| 1701 |
|
| 1702 |
// PageUp scrolls the view up a page |
| 1703 |
func (h *BufPane) PageUp() bool { |
| 1704 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1705 |
h.ScrollUp(h.BufView().Height - pageOverlap) |
| 1706 |
return true |
| 1707 |
} |
| 1708 |
|
| 1709 |
// PageDown scrolls the view down a page |
| 1710 |
func (h *BufPane) PageDown() bool { |
| 1711 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1712 |
h.ScrollDown(h.BufView().Height - pageOverlap) |
| 1713 |
h.ScrollAdjust() |
| 1714 |
return true |
| 1715 |
} |
| 1716 |
|
| 1717 |
// SelectPageUp selects up one page |
| 1718 |
func (h *BufPane) SelectPageUp() bool { |
| 1719 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1720 |
scrollAmount := h.BufView().Height - pageOverlap |
| 1721 |
if !h.Cursor.HasSelection() { |
| 1722 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 1723 |
} |
| 1724 |
h.MoveCursorUp(scrollAmount) |
| 1725 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 1726 |
if h.Cursor.Num == 0 { |
| 1727 |
h.ScrollUp(scrollAmount) |
| 1728 |
} |
| 1729 |
h.Relocate() |
| 1730 |
return true |
| 1731 |
} |
| 1732 |
|
| 1733 |
// SelectPageDown selects down one page |
| 1734 |
func (h *BufPane) SelectPageDown() bool { |
| 1735 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1736 |
scrollAmount := h.BufView().Height - pageOverlap |
| 1737 |
if !h.Cursor.HasSelection() { |
| 1738 |
h.Cursor.OrigSelection[0] = h.Cursor.Loc |
| 1739 |
} |
| 1740 |
h.MoveCursorDown(scrollAmount) |
| 1741 |
h.Cursor.SelectTo(h.Cursor.Loc) |
| 1742 |
if h.Cursor.Num == 0 && !h.ScrollReachedEnd() { |
| 1743 |
h.ScrollDown(scrollAmount) |
| 1744 |
h.ScrollAdjust() |
| 1745 |
} |
| 1746 |
h.Relocate() |
| 1747 |
return true |
| 1748 |
} |
| 1749 |
|
| 1750 |
// CursorPageUp places the cursor a page up, |
| 1751 |
// moving the view to keep cursor at the same relative position in the view |
| 1752 |
func (h *BufPane) CursorPageUp() bool { |
| 1753 |
h.Cursor.Deselect(true) |
| 1754 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1755 |
scrollAmount := h.BufView().Height - pageOverlap |
| 1756 |
h.MoveCursorUp(scrollAmount) |
| 1757 |
if h.Cursor.Num == 0 { |
| 1758 |
h.ScrollUp(scrollAmount) |
| 1759 |
} |
| 1760 |
h.Relocate() |
| 1761 |
return true |
| 1762 |
} |
| 1763 |
|
| 1764 |
// CursorPageDown places the cursor a page down, |
| 1765 |
// moving the view to keep cursor at the same relative position in the view |
| 1766 |
func (h *BufPane) CursorPageDown() bool { |
| 1767 |
selectionEndNewline := h.Cursor.HasSelection() && h.Cursor.CurSelection[1].X == 0 |
| 1768 |
h.Cursor.Deselect(false) |
| 1769 |
pageOverlap := int(h.Buf.Settings["pageoverlap"].(float64)) |
| 1770 |
scrollAmount := h.BufView().Height - pageOverlap |
| 1771 |
if selectionEndNewline { |
| 1772 |
scrollAmount-- |
| 1773 |
} |
| 1774 |
h.MoveCursorDown(scrollAmount) |
| 1775 |
if h.Cursor.Num == 0 && !h.ScrollReachedEnd() { |
| 1776 |
h.ScrollDown(scrollAmount) |
| 1777 |
h.ScrollAdjust() |
| 1778 |
} |
| 1779 |
if selectionEndNewline { |
| 1780 |
h.Cursor.Start() |
| 1781 |
} |
| 1782 |
h.Relocate() |
| 1783 |
return true |
| 1784 |
} |
| 1785 |
|
| 1786 |
// HalfPageUp scrolls the view up half a page |
| 1787 |
func (h *BufPane) HalfPageUp() bool { |
| 1788 |
h.ScrollUp(h.BufView().Height / 2) |
| 1789 |
return true |
| 1790 |
} |
| 1791 |
|
| 1792 |
// HalfPageDown scrolls the view down half a page |
| 1793 |
func (h *BufPane) HalfPageDown() bool { |
| 1794 |
h.ScrollDown(h.BufView().Height / 2) |
| 1795 |
h.ScrollAdjust() |
| 1796 |
return true |
| 1797 |
} |
| 1798 |
|
| 1799 |
// ToggleDiffGutter turns the diff gutter off and on |
| 1800 |
func (h *BufPane) ToggleDiffGutter() bool { |
| 1801 |
diffgutter := !h.Buf.Settings["diffgutter"].(bool) |
| 1802 |
h.Buf.SetOptionNative("diffgutter", diffgutter) |
| 1803 |
if diffgutter { |
| 1804 |
h.Buf.UpdateDiff() |
| 1805 |
InfoBar.Message("Enabled diff gutter") |
| 1806 |
} else { |
| 1807 |
InfoBar.Message("Disabled diff gutter") |
| 1808 |
} |
| 1809 |
return true |
| 1810 |
} |
| 1811 |
|
| 1812 |
// ToggleRuler turns line numbers off and on |
| 1813 |
func (h *BufPane) ToggleRuler() bool { |
| 1814 |
ruler := !h.Buf.Settings["ruler"].(bool) |
| 1815 |
h.Buf.SetOptionNative("ruler", ruler) |
| 1816 |
if ruler { |
| 1817 |
InfoBar.Message("Enabled ruler") |
| 1818 |
} else { |
| 1819 |
InfoBar.Message("Disabled ruler") |
| 1820 |
} |
| 1821 |
return true |
| 1822 |
} |
| 1823 |
|
| 1824 |
// ClearStatus clears the infobar. It is an alias for ClearInfo. |
| 1825 |
func (h *BufPane) ClearStatus() bool { |
| 1826 |
return h.ClearInfo() |
| 1827 |
} |
| 1828 |
|
| 1829 |
// ToggleHelp toggles the help screen |
| 1830 |
func (h *BufPane) ToggleHelp() bool { |
| 1831 |
if h.Buf.Type == buffer.BTHelp { |
| 1832 |
h.Quit() |
| 1833 |
} else { |
| 1834 |
hsplit := config.GlobalSettings["helpsplit"] == "hsplit" |
| 1835 |
h.openHelp("help", hsplit, false) |
| 1836 |
} |
| 1837 |
return true |
| 1838 |
} |
| 1839 |
|
| 1840 |
// ToggleKeyMenu toggles the keymenu option and resizes all tabs |
| 1841 |
func (h *BufPane) ToggleKeyMenu() bool { |
| 1842 |
config.GlobalSettings["keymenu"] = !config.GetGlobalOption("keymenu").(bool) |
| 1843 |
Tabs.Resize() |
| 1844 |
return true |
| 1845 |
} |
| 1846 |
|
| 1847 |
// ShellMode opens a terminal to run a shell command |
| 1848 |
func (h *BufPane) ShellMode() bool { |
| 1849 |
InfoBar.Prompt("$ ", "", "Shell", nil, func(resp string, canceled bool) { |
| 1850 |
if !canceled { |
| 1851 |
// The true here is for openTerm to make the command interactive |
| 1852 |
shell.RunInteractiveShell(resp, true, false) |
| 1853 |
} |
| 1854 |
}) |
| 1855 |
|
| 1856 |
return true |
| 1857 |
} |
| 1858 |
|
| 1859 |
// CommandMode lets the user enter a command |
| 1860 |
func (h *BufPane) CommandMode() bool { |
| 1861 |
InfoBar.Prompt("> ", "", "Command", nil, func(resp string, canceled bool) { |
| 1862 |
if !canceled { |
| 1863 |
h.HandleCommand(resp) |
| 1864 |
} |
| 1865 |
}) |
| 1866 |
return true |
| 1867 |
} |
| 1868 |
|
| 1869 |
// ToggleOverwriteMode lets the user toggle the text overwrite mode |
| 1870 |
func (h *BufPane) ToggleOverwriteMode() bool { |
| 1871 |
h.Buf.OverwriteMode = !h.Buf.OverwriteMode |
| 1872 |
return true |
| 1873 |
} |
| 1874 |
|
| 1875 |
// Escape leaves current mode |
| 1876 |
func (h *BufPane) Escape() bool { |
| 1877 |
return true |
| 1878 |
} |
| 1879 |
|
| 1880 |
// Deselect deselects on the current cursor |
| 1881 |
func (h *BufPane) Deselect() bool { |
| 1882 |
if !h.Cursor.HasSelection() { |
| 1883 |
return false |
| 1884 |
} |
| 1885 |
h.Cursor.Deselect(true) |
| 1886 |
return true |
| 1887 |
} |
| 1888 |
|
| 1889 |
// ClearInfo clears the infobar |
| 1890 |
func (h *BufPane) ClearInfo() bool { |
| 1891 |
if InfoBar.Msg == "" { |
| 1892 |
return false |
| 1893 |
} |
| 1894 |
InfoBar.Message("") |
| 1895 |
return true |
| 1896 |
} |
| 1897 |
|
| 1898 |
// ForceQuit closes the tab or view even if there are unsaved changes |
| 1899 |
// (no prompt) |
| 1900 |
func (h *BufPane) ForceQuit() bool { |
| 1901 |
h.Buf.Close() |
| 1902 |
if len(h.tab.Panes) > 1 { |
| 1903 |
h.Unsplit() |
| 1904 |
} else if len(Tabs.List) > 1 { |
| 1905 |
Tabs.RemoveTab(h.splitID) |
| 1906 |
} else { |
| 1907 |
screen.Screen.Fini() |
| 1908 |
InfoBar.Close() |
| 1909 |
runtime.Goexit() |
| 1910 |
} |
| 1911 |
return true |
| 1912 |
} |
| 1913 |
|
| 1914 |
// closePrompt displays a prompt to save the buffer before closing it to proceed |
| 1915 |
// with a different action or command |
| 1916 |
func (h *BufPane) closePrompt(action string, callback func()) { |
| 1917 |
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) { |
| 1918 |
if !canceled && !yes { |
| 1919 |
callback() |
| 1920 |
} else if !canceled && yes { |
| 1921 |
h.SaveCB(action, callback) |
| 1922 |
} |
| 1923 |
}) |
| 1924 |
} |
| 1925 |
|
| 1926 |
// Quit this will close the current tab or view that is open |
| 1927 |
func (h *BufPane) Quit() bool { |
| 1928 |
if h.Buf.Modified() && !h.Buf.Shared() { |
| 1929 |
if config.GlobalSettings["autosave"].(float64) > 0 && h.Buf.Path != "" { |
| 1930 |
// autosave on means we automatically save when quitting |
| 1931 |
h.SaveCB("Quit", func() { |
| 1932 |
h.ForceQuit() |
| 1933 |
}) |
| 1934 |
} else { |
| 1935 |
h.closePrompt("Quit", func() { |
| 1936 |
h.ForceQuit() |
| 1937 |
}) |
| 1938 |
} |
| 1939 |
} else { |
| 1940 |
h.ForceQuit() |
| 1941 |
} |
| 1942 |
return true |
| 1943 |
} |
| 1944 |
|
| 1945 |
// QuitAll quits the whole editor; all splits and tabs |
| 1946 |
func (h *BufPane) QuitAll() bool { |
| 1947 |
anyModified := false |
| 1948 |
for _, b := range buffer.OpenBuffers { |
| 1949 |
if b.Modified() { |
| 1950 |
anyModified = true |
| 1951 |
break |
| 1952 |
} |
| 1953 |
} |
| 1954 |
|
| 1955 |
quit := func() { |
| 1956 |
buffer.CloseOpenBuffers() |
| 1957 |
screen.Screen.Fini() |
| 1958 |
InfoBar.Close() |
| 1959 |
runtime.Goexit() |
| 1960 |
} |
| 1961 |
|
| 1962 |
if anyModified { |
| 1963 |
InfoBar.YNPrompt("Quit micro? (all open buffers will be closed without saving)", func(yes, canceled bool) { |
| 1964 |
if !canceled && yes { |
| 1965 |
quit() |
| 1966 |
} |
| 1967 |
}) |
| 1968 |
} else { |
| 1969 |
quit() |
| 1970 |
} |
| 1971 |
|
| 1972 |
return true |
| 1973 |
} |
| 1974 |
|
| 1975 |
// AddTab adds a new tab with an empty buffer |
| 1976 |
func (h *BufPane) AddTab() bool { |
| 1977 |
width, height := screen.Screen.Size() |
| 1978 |
iOffset := config.GetInfoBarOffset() |
| 1979 |
b := buffer.NewBufferFromString("", "", buffer.BTDefault) |
| 1980 |
tp := NewTabFromBuffer(0, 0, width, height-iOffset, b) |
| 1981 |
Tabs.AddTab(tp) |
| 1982 |
Tabs.SetActive(len(Tabs.List) - 1) |
| 1983 |
|
| 1984 |
return true |
| 1985 |
} |
| 1986 |
|
| 1987 |
// PreviousTab switches to the previous tab in the tab list |
| 1988 |
func (h *BufPane) PreviousTab() bool { |
| 1989 |
if Tabs.Active() == 0 { |
| 1990 |
return false |
| 1991 |
} |
| 1992 |
Tabs.SetActive(Tabs.Active() - 1) |
| 1993 |
return true |
| 1994 |
} |
| 1995 |
|
| 1996 |
// NextTab switches to the next tab in the tab list |
| 1997 |
func (h *BufPane) NextTab() bool { |
| 1998 |
if Tabs.Active() == len(Tabs.List)-1 { |
| 1999 |
return false |
| 2000 |
} |
| 2001 |
Tabs.SetActive(Tabs.Active() + 1) |
| 2002 |
return true |
| 2003 |
} |
| 2004 |
|
| 2005 |
// FirstTab switches to the first tab in the tab list |
| 2006 |
func (h *BufPane) FirstTab() bool { |
| 2007 |
if Tabs.Active() == 0 { |
| 2008 |
return false |
| 2009 |
} |
| 2010 |
Tabs.SetActive(0) |
| 2011 |
return true |
| 2012 |
} |
| 2013 |
|
| 2014 |
// LastTab switches to the last tab in the tab list |
| 2015 |
func (h *BufPane) LastTab() bool { |
| 2016 |
lastTabIndex := len(Tabs.List) - 1 |
| 2017 |
if Tabs.Active() == lastTabIndex { |
| 2018 |
return false |
| 2019 |
} |
| 2020 |
Tabs.SetActive(lastTabIndex) |
| 2021 |
return true |
| 2022 |
} |
| 2023 |
|
| 2024 |
// VSplitAction opens an empty vertical split |
| 2025 |
func (h *BufPane) VSplitAction() bool { |
| 2026 |
h.VSplitBuf(buffer.NewBufferFromString("", "", buffer.BTDefault)) |
| 2027 |
|
| 2028 |
return true |
| 2029 |
} |
| 2030 |
|
| 2031 |
// HSplitAction opens an empty horizontal split |
| 2032 |
func (h *BufPane) HSplitAction() bool { |
| 2033 |
h.HSplitBuf(buffer.NewBufferFromString("", "", buffer.BTDefault)) |
| 2034 |
|
| 2035 |
return true |
| 2036 |
} |
| 2037 |
|
| 2038 |
// Unsplit closes all splits in the current tab except the active one |
| 2039 |
func (h *BufPane) Unsplit() bool { |
| 2040 |
tab := h.tab |
| 2041 |
n := tab.GetNode(h.splitID) |
| 2042 |
ok := n.Unsplit() |
| 2043 |
if ok { |
| 2044 |
tab.RemovePane(tab.GetPane(h.splitID)) |
| 2045 |
tab.Resize() |
| 2046 |
tab.SetActive(len(tab.Panes) - 1) |
| 2047 |
|
| 2048 |
return true |
| 2049 |
} |
| 2050 |
return false |
| 2051 |
} |
| 2052 |
|
| 2053 |
// NextSplit changes the view to the next split |
| 2054 |
func (h *BufPane) NextSplit() bool { |
| 2055 |
if h.tab.active == len(h.tab.Panes)-1 { |
| 2056 |
return false |
| 2057 |
} |
| 2058 |
h.tab.SetActive(h.tab.active + 1) |
| 2059 |
return true |
| 2060 |
} |
| 2061 |
|
| 2062 |
// PreviousSplit changes the view to the previous split |
| 2063 |
func (h *BufPane) PreviousSplit() bool { |
| 2064 |
if h.tab.active == 0 { |
| 2065 |
return false |
| 2066 |
} |
| 2067 |
h.tab.SetActive(h.tab.active - 1) |
| 2068 |
return true |
| 2069 |
} |
| 2070 |
|
| 2071 |
// FirstSplit changes the view to the first split |
| 2072 |
func (h *BufPane) FirstSplit() bool { |
| 2073 |
if h.tab.active == 0 { |
| 2074 |
return false |
| 2075 |
} |
| 2076 |
h.tab.SetActive(0) |
| 2077 |
return true |
| 2078 |
} |
| 2079 |
|
| 2080 |
// LastSplit changes the view to the last split |
| 2081 |
func (h *BufPane) LastSplit() bool { |
| 2082 |
lastPaneIdx := len(h.tab.Panes) - 1 |
| 2083 |
if h.tab.active == lastPaneIdx { |
| 2084 |
return false |
| 2085 |
} |
| 2086 |
h.tab.SetActive(lastPaneIdx) |
| 2087 |
return true |
| 2088 |
} |
| 2089 |
|
| 2090 |
var curmacro []any |
| 2091 |
var recordingMacro bool |
| 2092 |
|
| 2093 |
// ToggleMacro toggles recording of a macro |
| 2094 |
func (h *BufPane) ToggleMacro() bool { |
| 2095 |
recordingMacro = !recordingMacro |
| 2096 |
if recordingMacro { |
| 2097 |
curmacro = []any{} |
| 2098 |
InfoBar.Message("Recording") |
| 2099 |
} else { |
| 2100 |
InfoBar.Message("Stopped recording") |
| 2101 |
} |
| 2102 |
h.Relocate() |
| 2103 |
return true |
| 2104 |
} |
| 2105 |
|
| 2106 |
// PlayMacro plays back the most recently recorded macro |
| 2107 |
func (h *BufPane) PlayMacro() bool { |
| 2108 |
if recordingMacro { |
| 2109 |
return false |
| 2110 |
} |
| 2111 |
for _, action := range curmacro { |
| 2112 |
switch t := action.(type) { |
| 2113 |
case rune: |
| 2114 |
h.DoRuneInsert(t) |
| 2115 |
case BufKeyAction: |
| 2116 |
t(h) |
| 2117 |
} |
| 2118 |
} |
| 2119 |
h.Relocate() |
| 2120 |
return true |
| 2121 |
} |
| 2122 |
|
| 2123 |
// SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word |
| 2124 |
func (h *BufPane) SpawnMultiCursor() bool { |
| 2125 |
spawner := h.Buf.GetCursor(h.Buf.NumCursors() - 1) |
| 2126 |
if !spawner.HasSelection() { |
| 2127 |
spawner.SelectWord() |
| 2128 |
h.multiWord = true |
| 2129 |
h.Relocate() |
| 2130 |
return true |
| 2131 |
} |
| 2132 |
|
| 2133 |
sel := spawner.GetSelection() |
| 2134 |
searchStart := spawner.CurSelection[1] |
| 2135 |
|
| 2136 |
search := string(sel) |
| 2137 |
search = regexp.QuoteMeta(search) |
| 2138 |
if h.multiWord { |
| 2139 |
search = "\\b" + search + "\\b" |
| 2140 |
} |
| 2141 |
match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true) |
| 2142 |
if err != nil { |
| 2143 |
InfoBar.Error(err) |
| 2144 |
} |
| 2145 |
if found { |
| 2146 |
c := buffer.NewCursor(h.Buf, buffer.Loc{}) |
| 2147 |
c.SetSelectionStart(match[0]) |
| 2148 |
c.SetSelectionEnd(match[1]) |
| 2149 |
c.OrigSelection[0] = c.CurSelection[0] |
| 2150 |
c.OrigSelection[1] = c.CurSelection[1] |
| 2151 |
c.Loc = c.CurSelection[1] |
| 2152 |
|
| 2153 |
h.Buf.AddCursor(c) |
| 2154 |
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) |
| 2155 |
h.Buf.MergeCursors() |
| 2156 |
} else { |
| 2157 |
InfoBar.Message("No matches found") |
| 2158 |
} |
| 2159 |
|
| 2160 |
h.Relocate() |
| 2161 |
return true |
| 2162 |
} |
| 2163 |
|
| 2164 |
// SpawnCursorAtLoc spawns a new cursor at a location and merges the cursors |
| 2165 |
func (h *BufPane) SpawnCursorAtLoc(loc buffer.Loc) *buffer.Cursor { |
| 2166 |
c := buffer.NewCursor(h.Buf, loc) |
| 2167 |
h.Buf.AddCursor(c) |
| 2168 |
h.Buf.MergeCursors() |
| 2169 |
return c |
| 2170 |
} |
| 2171 |
|
| 2172 |
// SpawnMultiCursorUpN is not an action |
| 2173 |
func (h *BufPane) SpawnMultiCursorUpN(n int) bool { |
| 2174 |
lastC := h.Buf.GetCursor(h.Buf.NumCursors() - 1) |
| 2175 |
if n > 0 && lastC.Y == 0 { |
| 2176 |
return false |
| 2177 |
} |
| 2178 |
if n < 0 && lastC.Y+1 == h.Buf.LinesNum() { |
| 2179 |
return false |
| 2180 |
} |
| 2181 |
|
| 2182 |
h.Buf.DeselectCursors() |
| 2183 |
|
| 2184 |
c := buffer.NewCursor(h.Buf, buffer.Loc{lastC.X, lastC.Y - n}) |
| 2185 |
c.LastVisualX = lastC.LastVisualX |
| 2186 |
c.LastWrappedVisualX = lastC.LastWrappedVisualX |
| 2187 |
c.X = c.GetCharPosInLine(h.Buf.LineBytes(c.Y), c.LastVisualX) |
| 2188 |
c.Relocate() |
| 2189 |
|
| 2190 |
h.Buf.AddCursor(c) |
| 2191 |
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) |
| 2192 |
h.Buf.MergeCursors() |
| 2193 |
|
| 2194 |
h.Relocate() |
| 2195 |
return true |
| 2196 |
} |
| 2197 |
|
| 2198 |
// SpawnMultiCursorUp creates additional cursor, at the same X (if possible), one Y less. |
| 2199 |
func (h *BufPane) SpawnMultiCursorUp() bool { |
| 2200 |
return h.SpawnMultiCursorUpN(1) |
| 2201 |
} |
| 2202 |
|
| 2203 |
// SpawnMultiCursorDown creates additional cursor, at the same X (if possible), one Y more. |
| 2204 |
func (h *BufPane) SpawnMultiCursorDown() bool { |
| 2205 |
return h.SpawnMultiCursorUpN(-1) |
| 2206 |
} |
| 2207 |
|
| 2208 |
// SpawnMultiCursorSelect adds a cursor at the beginning of each line of a selection |
| 2209 |
func (h *BufPane) SpawnMultiCursorSelect() bool { |
| 2210 |
// Avoid cases where multiple cursors already exist, that would create problems |
| 2211 |
if h.Buf.NumCursors() > 1 { |
| 2212 |
return false |
| 2213 |
} |
| 2214 |
|
| 2215 |
var startLine int |
| 2216 |
var endLine int |
| 2217 |
|
| 2218 |
a, b := h.Cursor.CurSelection[0].Y, h.Cursor.CurSelection[1].Y |
| 2219 |
if a > b { |
| 2220 |
startLine, endLine = b, a |
| 2221 |
} else { |
| 2222 |
startLine, endLine = a, b |
| 2223 |
} |
| 2224 |
|
| 2225 |
if h.Cursor.HasSelection() { |
| 2226 |
h.Cursor.ResetSelection() |
| 2227 |
h.Cursor.GotoLoc(buffer.Loc{0, startLine}) |
| 2228 |
|
| 2229 |
for i := startLine; i <= endLine; i++ { |
| 2230 |
c := buffer.NewCursor(h.Buf, buffer.Loc{0, i}) |
| 2231 |
c.StoreVisualX() |
| 2232 |
h.Buf.AddCursor(c) |
| 2233 |
} |
| 2234 |
h.Buf.MergeCursors() |
| 2235 |
} else { |
| 2236 |
return false |
| 2237 |
} |
| 2238 |
InfoBar.Message("Added cursors from selection") |
| 2239 |
return true |
| 2240 |
} |
| 2241 |
|
| 2242 |
// MouseMultiCursor is a mouse action which puts a new cursor at the mouse position, |
| 2243 |
// or removes a cursor if it is already there |
| 2244 |
func (h *BufPane) MouseMultiCursor(e *tcell.EventMouse) bool { |
| 2245 |
b := h.Buf |
| 2246 |
mx, my := e.Position() |
| 2247 |
// ignore click on the status line |
| 2248 |
if my >= h.BufView().Y+h.BufView().Height { |
| 2249 |
return false |
| 2250 |
} |
| 2251 |
mouseLoc := h.LocFromVisual(buffer.Loc{X: mx, Y: my}) |
| 2252 |
|
| 2253 |
if h.Buf.NumCursors() > 1 { |
| 2254 |
cursors := h.Buf.GetCursors() |
| 2255 |
for _, c := range cursors { |
| 2256 |
if c.Loc == mouseLoc { |
| 2257 |
h.Buf.RemoveCursor(c.Num) |
| 2258 |
return true |
| 2259 |
} |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
c := buffer.NewCursor(b, mouseLoc) |
| 2264 |
b.AddCursor(c) |
| 2265 |
b.MergeCursors() |
| 2266 |
|
| 2267 |
return true |
| 2268 |
} |
| 2269 |
|
| 2270 |
func (h *BufPane) skipMultiCursor(forward bool) bool { |
| 2271 |
lastC := h.Buf.GetCursor(h.Buf.NumCursors() - 1) |
| 2272 |
if !lastC.HasSelection() { |
| 2273 |
return false |
| 2274 |
} |
| 2275 |
sel := lastC.GetSelection() |
| 2276 |
searchStart := lastC.CurSelection[1] |
| 2277 |
if !forward { |
| 2278 |
searchStart = lastC.CurSelection[0] |
| 2279 |
} |
| 2280 |
|
| 2281 |
search := string(sel) |
| 2282 |
search = regexp.QuoteMeta(search) |
| 2283 |
if h.multiWord { |
| 2284 |
search = "\\b" + search + "\\b" |
| 2285 |
} |
| 2286 |
|
| 2287 |
match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, forward, true) |
| 2288 |
if err != nil { |
| 2289 |
InfoBar.Error(err) |
| 2290 |
} |
| 2291 |
if found { |
| 2292 |
lastC.SetSelectionStart(match[0]) |
| 2293 |
lastC.SetSelectionEnd(match[1]) |
| 2294 |
lastC.OrigSelection[0] = lastC.CurSelection[0] |
| 2295 |
lastC.OrigSelection[1] = lastC.CurSelection[1] |
| 2296 |
lastC.Loc = lastC.CurSelection[1] |
| 2297 |
|
| 2298 |
h.Buf.MergeCursors() |
| 2299 |
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) |
| 2300 |
} else { |
| 2301 |
InfoBar.Message("No matches found") |
| 2302 |
} |
| 2303 |
h.Relocate() |
| 2304 |
return true |
| 2305 |
} |
| 2306 |
|
| 2307 |
// SkipMultiCursor moves the current multiple cursor to the next available position |
| 2308 |
func (h *BufPane) SkipMultiCursor() bool { |
| 2309 |
return h.skipMultiCursor(true) |
| 2310 |
} |
| 2311 |
|
| 2312 |
// SkipMultiCursorBack moves the current multiple cursor to the previous available position |
| 2313 |
func (h *BufPane) SkipMultiCursorBack() bool { |
| 2314 |
return h.skipMultiCursor(false) |
| 2315 |
} |
| 2316 |
|
| 2317 |
// RemoveMultiCursor removes the latest multiple cursor |
| 2318 |
func (h *BufPane) RemoveMultiCursor() bool { |
| 2319 |
if h.Buf.NumCursors() > 1 { |
| 2320 |
h.Buf.RemoveCursor(h.Buf.NumCursors() - 1) |
| 2321 |
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) |
| 2322 |
h.Buf.UpdateCursors() |
| 2323 |
} else if h.multiWord { |
| 2324 |
h.multiWord = false |
| 2325 |
h.Cursor.Deselect(true) |
| 2326 |
} else { |
| 2327 |
return false |
| 2328 |
} |
| 2329 |
h.Relocate() |
| 2330 |
return true |
| 2331 |
} |
| 2332 |
|
| 2333 |
// RemoveAllMultiCursors removes all cursors except the base cursor |
| 2334 |
func (h *BufPane) RemoveAllMultiCursors() bool { |
| 2335 |
if h.Buf.NumCursors() > 1 || h.multiWord { |
| 2336 |
h.Buf.ClearCursors() |
| 2337 |
h.multiWord = false |
| 2338 |
} else { |
| 2339 |
return false |
| 2340 |
} |
| 2341 |
h.Relocate() |
| 2342 |
return true |
| 2343 |
} |
| 2344 |
|
| 2345 |
// None is an action that does nothing |
| 2346 |
func (h *BufPane) None() bool { |
| 2347 |
return true |
| 2348 |
} |