| 1 |
package buffer |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/zyedidia/micro/v2/internal/clipboard" |
| 5 |
"github.com/zyedidia/micro/v2/internal/util" |
| 6 |
) |
| 7 |
|
| 8 |
// InBounds returns whether the given location is a valid character position in the given buffer |
| 9 |
func InBounds(pos Loc, buf *Buffer) bool { |
| 10 |
if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > util.CharacterCount(buf.LineBytes(pos.Y)) { |
| 11 |
return false |
| 12 |
} |
| 13 |
|
| 14 |
return true |
| 15 |
} |
| 16 |
|
| 17 |
// The Cursor struct stores the location of the cursor in the buffer |
| 18 |
// as well as the selection |
| 19 |
type Cursor struct { |
| 20 |
buf *Buffer |
| 21 |
Loc |
| 22 |
|
| 23 |
// Last visual x position of the cursor. Used in cursor up/down movements |
| 24 |
// for remembering the original x position when moving to a line that is |
| 25 |
// shorter than current x position. |
| 26 |
LastVisualX int |
| 27 |
// Similar to LastVisualX but takes softwrapping into account, i.e. last |
| 28 |
// visual x position in a visual (wrapped) line on the screen, which may be |
| 29 |
// different from the line in the buffer. |
| 30 |
LastWrappedVisualX int |
| 31 |
|
| 32 |
// The current selection as a range of character numbers (inclusive) |
| 33 |
CurSelection [2]Loc |
| 34 |
// The original selection as a range of character numbers |
| 35 |
// This is used for line and word selection where it is necessary |
| 36 |
// to know what the original selection was |
| 37 |
OrigSelection [2]Loc |
| 38 |
|
| 39 |
// The line number where a new trailing whitespace has been added |
| 40 |
// or -1 if there is no new trailing whitespace at this cursor. |
| 41 |
// This is used for checking if a trailing whitespace should be highlighted |
| 42 |
NewTrailingWsY int |
| 43 |
|
| 44 |
// Which cursor index is this (for multiple cursors) |
| 45 |
Num int |
| 46 |
} |
| 47 |
|
| 48 |
func NewCursor(b *Buffer, l Loc) *Cursor { |
| 49 |
c := &Cursor{ |
| 50 |
buf: b, |
| 51 |
Loc: l, |
| 52 |
|
| 53 |
NewTrailingWsY: -1, |
| 54 |
} |
| 55 |
c.StoreVisualX() |
| 56 |
return c |
| 57 |
} |
| 58 |
|
| 59 |
func (c *Cursor) SetBuf(b *Buffer) { |
| 60 |
c.buf = b |
| 61 |
} |
| 62 |
|
| 63 |
func (c *Cursor) Buf() *Buffer { |
| 64 |
return c.buf |
| 65 |
} |
| 66 |
|
| 67 |
// Goto puts the cursor at the given cursor's location and gives |
| 68 |
// the current cursor its selection too |
| 69 |
func (c *Cursor) Goto(b Cursor) { |
| 70 |
c.X, c.Y = b.X, b.Y |
| 71 |
c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection |
| 72 |
c.StoreVisualX() |
| 73 |
} |
| 74 |
|
| 75 |
// GotoLoc puts the cursor at the given cursor's location and gives |
| 76 |
// the current cursor its selection too |
| 77 |
func (c *Cursor) GotoLoc(l Loc) { |
| 78 |
c.X, c.Y = l.X, l.Y |
| 79 |
c.StoreVisualX() |
| 80 |
} |
| 81 |
|
| 82 |
// GetVisualX returns the x value of the cursor in visual spaces |
| 83 |
func (c *Cursor) GetVisualX(wrap bool) int { |
| 84 |
if wrap && c.buf.GetVisualX != nil { |
| 85 |
return c.buf.GetVisualX(c.Loc) |
| 86 |
} |
| 87 |
|
| 88 |
if c.X <= 0 { |
| 89 |
c.X = 0 |
| 90 |
return 0 |
| 91 |
} |
| 92 |
|
| 93 |
bytes := c.buf.LineBytes(c.Y) |
| 94 |
tabsize := int(c.buf.Settings["tabsize"].(float64)) |
| 95 |
|
| 96 |
return util.StringWidth(bytes, c.X, tabsize) |
| 97 |
} |
| 98 |
|
| 99 |
// GetCharPosInLine gets the char position of a visual x y |
| 100 |
// coordinate (this is necessary because tabs are 1 char but |
| 101 |
// 4 visual spaces) |
| 102 |
func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int { |
| 103 |
tabsize := int(c.buf.Settings["tabsize"].(float64)) |
| 104 |
return util.GetCharPosInLine(b, visualPos, tabsize) |
| 105 |
} |
| 106 |
|
| 107 |
// Start moves the cursor to the start of the line it is on |
| 108 |
func (c *Cursor) Start() { |
| 109 |
c.X = 0 |
| 110 |
c.StoreVisualX() |
| 111 |
} |
| 112 |
|
| 113 |
// StartOfText moves the cursor to the first non-whitespace rune of |
| 114 |
// the line it is on |
| 115 |
func (c *Cursor) StartOfText() { |
| 116 |
c.Start() |
| 117 |
for util.IsWhitespace(c.RuneUnder(c.X)) { |
| 118 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 119 |
break |
| 120 |
} |
| 121 |
c.Right() |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// IsStartOfText returns whether the cursor is at the first |
| 126 |
// non-whitespace rune of the line it is on |
| 127 |
func (c *Cursor) IsStartOfText() bool { |
| 128 |
x := 0 |
| 129 |
for util.IsWhitespace(c.RuneUnder(x)) { |
| 130 |
if x == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 131 |
break |
| 132 |
} |
| 133 |
x++ |
| 134 |
} |
| 135 |
return c.X == x |
| 136 |
} |
| 137 |
|
| 138 |
// End moves the cursor to the end of the line it is on |
| 139 |
func (c *Cursor) End() { |
| 140 |
c.X = util.CharacterCount(c.buf.LineBytes(c.Y)) |
| 141 |
c.StoreVisualX() |
| 142 |
} |
| 143 |
|
| 144 |
// CopySelection copies the user's selection to either "primary" |
| 145 |
// or "clipboard" |
| 146 |
func (c *Cursor) CopySelection(target clipboard.Register) { |
| 147 |
if c.HasSelection() { |
| 148 |
if target != clipboard.PrimaryReg || c.buf.Settings["useprimary"].(bool) { |
| 149 |
clipboard.WriteMulti(string(c.GetSelection()), target, c.Num, c.buf.NumCursors()) |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// ResetSelection resets the user's selection |
| 155 |
func (c *Cursor) ResetSelection() { |
| 156 |
c.CurSelection[0] = c.buf.Start() |
| 157 |
c.CurSelection[1] = c.buf.Start() |
| 158 |
} |
| 159 |
|
| 160 |
// SetSelectionStart sets the start of the selection |
| 161 |
func (c *Cursor) SetSelectionStart(pos Loc) { |
| 162 |
c.CurSelection[0] = pos |
| 163 |
} |
| 164 |
|
| 165 |
// SetSelectionEnd sets the end of the selection |
| 166 |
func (c *Cursor) SetSelectionEnd(pos Loc) { |
| 167 |
c.CurSelection[1] = pos |
| 168 |
} |
| 169 |
|
| 170 |
// HasSelection returns whether or not the user has selected anything |
| 171 |
func (c *Cursor) HasSelection() bool { |
| 172 |
return c.CurSelection[0] != c.CurSelection[1] |
| 173 |
} |
| 174 |
|
| 175 |
// DeleteSelection deletes the currently selected text |
| 176 |
func (c *Cursor) DeleteSelection() { |
| 177 |
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) { |
| 178 |
c.buf.Remove(c.CurSelection[1], c.CurSelection[0]) |
| 179 |
c.Loc = c.CurSelection[1] |
| 180 |
} else if !c.HasSelection() { |
| 181 |
return |
| 182 |
} else { |
| 183 |
c.buf.Remove(c.CurSelection[0], c.CurSelection[1]) |
| 184 |
c.Loc = c.CurSelection[0] |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// Deselect closes the cursor's current selection |
| 189 |
// Start indicates whether the cursor should be placed |
| 190 |
// at the start or end of the selection |
| 191 |
func (c *Cursor) Deselect(start bool) { |
| 192 |
if c.HasSelection() { |
| 193 |
if start { |
| 194 |
c.Loc = c.CurSelection[0] |
| 195 |
} else { |
| 196 |
c.Loc = c.CurSelection[1] |
| 197 |
} |
| 198 |
c.ResetSelection() |
| 199 |
c.StoreVisualX() |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// GetSelection returns the cursor's selection |
| 204 |
func (c *Cursor) GetSelection() []byte { |
| 205 |
if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) { |
| 206 |
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) { |
| 207 |
return c.buf.Substr(c.CurSelection[1], c.CurSelection[0]) |
| 208 |
} |
| 209 |
return c.buf.Substr(c.CurSelection[0], c.CurSelection[1]) |
| 210 |
} |
| 211 |
return []byte{} |
| 212 |
} |
| 213 |
|
| 214 |
// SelectLine selects the current line |
| 215 |
func (c *Cursor) SelectLine() { |
| 216 |
c.Start() |
| 217 |
c.SetSelectionStart(c.Loc) |
| 218 |
c.End() |
| 219 |
if len(c.buf.lines)-1 > c.Y { |
| 220 |
c.SetSelectionEnd(c.Loc.Move(1, c.buf)) |
| 221 |
} else { |
| 222 |
c.SetSelectionEnd(c.Loc) |
| 223 |
} |
| 224 |
|
| 225 |
c.OrigSelection = c.CurSelection |
| 226 |
} |
| 227 |
|
| 228 |
// AddLineToSelection adds the current line to the selection |
| 229 |
func (c *Cursor) AddLineToSelection() { |
| 230 |
if c.Loc.LessThan(c.OrigSelection[0]) { |
| 231 |
c.Start() |
| 232 |
c.SetSelectionStart(c.Loc) |
| 233 |
c.SetSelectionEnd(c.OrigSelection[1]) |
| 234 |
} |
| 235 |
if c.Loc.GreaterThan(c.OrigSelection[1]) { |
| 236 |
c.End() |
| 237 |
c.SetSelectionEnd(c.Loc.Move(1, c.buf)) |
| 238 |
c.SetSelectionStart(c.OrigSelection[0]) |
| 239 |
} |
| 240 |
|
| 241 |
if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) { |
| 242 |
c.CurSelection = c.OrigSelection |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// UpN moves the cursor up N lines (if possible) |
| 247 |
func (c *Cursor) UpN(amount int) { |
| 248 |
proposedY := c.Y - amount |
| 249 |
if proposedY < 0 { |
| 250 |
proposedY = 0 |
| 251 |
} else if proposedY >= len(c.buf.lines) { |
| 252 |
proposedY = len(c.buf.lines) - 1 |
| 253 |
} |
| 254 |
|
| 255 |
bytes := c.buf.LineBytes(proposedY) |
| 256 |
c.X = c.GetCharPosInLine(bytes, c.LastVisualX) |
| 257 |
|
| 258 |
if c.X > util.CharacterCount(bytes) || (amount < 0 && proposedY == c.Y) { |
| 259 |
c.X = util.CharacterCount(bytes) |
| 260 |
c.StoreVisualX() |
| 261 |
} |
| 262 |
|
| 263 |
if c.X < 0 || (amount > 0 && proposedY == c.Y) { |
| 264 |
c.X = 0 |
| 265 |
c.StoreVisualX() |
| 266 |
} |
| 267 |
|
| 268 |
c.Y = proposedY |
| 269 |
} |
| 270 |
|
| 271 |
// DownN moves the cursor down N lines (if possible) |
| 272 |
func (c *Cursor) DownN(amount int) { |
| 273 |
c.UpN(-amount) |
| 274 |
} |
| 275 |
|
| 276 |
// Up moves the cursor up one line (if possible) |
| 277 |
func (c *Cursor) Up() { |
| 278 |
c.UpN(1) |
| 279 |
} |
| 280 |
|
| 281 |
// Down moves the cursor down one line (if possible) |
| 282 |
func (c *Cursor) Down() { |
| 283 |
c.DownN(1) |
| 284 |
} |
| 285 |
|
| 286 |
// Left moves the cursor left one cell (if possible) or to |
| 287 |
// the previous line if it is at the beginning |
| 288 |
func (c *Cursor) Left() { |
| 289 |
if c.Loc == c.buf.Start() { |
| 290 |
return |
| 291 |
} |
| 292 |
if c.X > 0 { |
| 293 |
c.X-- |
| 294 |
} else { |
| 295 |
c.Up() |
| 296 |
c.End() |
| 297 |
} |
| 298 |
c.StoreVisualX() |
| 299 |
} |
| 300 |
|
| 301 |
// Right moves the cursor right one cell (if possible) or |
| 302 |
// to the next line if it is at the end |
| 303 |
func (c *Cursor) Right() { |
| 304 |
if c.Loc == c.buf.End() { |
| 305 |
return |
| 306 |
} |
| 307 |
if c.X < util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 308 |
c.X++ |
| 309 |
} else { |
| 310 |
c.Down() |
| 311 |
c.Start() |
| 312 |
} |
| 313 |
c.StoreVisualX() |
| 314 |
} |
| 315 |
|
| 316 |
// Relocate makes sure that the cursor is inside the bounds |
| 317 |
// of the buffer If it isn't, it moves it to be within the |
| 318 |
// buffer's lines |
| 319 |
func (c *Cursor) Relocate() { |
| 320 |
if c.Y < 0 { |
| 321 |
c.Y = 0 |
| 322 |
} else if c.Y >= len(c.buf.lines) { |
| 323 |
c.Y = len(c.buf.lines) - 1 |
| 324 |
} |
| 325 |
|
| 326 |
if c.X < 0 { |
| 327 |
c.X = 0 |
| 328 |
} else if c.X > util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 329 |
c.X = util.CharacterCount(c.buf.LineBytes(c.Y)) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// SelectWord selects the word the cursor is currently on |
| 334 |
func (c *Cursor) SelectWord() { |
| 335 |
if len(c.buf.LineBytes(c.Y)) == 0 { |
| 336 |
return |
| 337 |
} |
| 338 |
|
| 339 |
if !util.IsWordChar(c.RuneUnder(c.X)) { |
| 340 |
c.SetSelectionStart(c.Loc) |
| 341 |
c.SetSelectionEnd(c.Loc.Move(1, c.buf)) |
| 342 |
c.OrigSelection = c.CurSelection |
| 343 |
return |
| 344 |
} |
| 345 |
|
| 346 |
forward, backward := c.X, c.X |
| 347 |
|
| 348 |
for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) { |
| 349 |
backward-- |
| 350 |
} |
| 351 |
|
| 352 |
c.SetSelectionStart(Loc{backward, c.Y}) |
| 353 |
c.OrigSelection[0] = c.CurSelection[0] |
| 354 |
|
| 355 |
lineLen := util.CharacterCount(c.buf.LineBytes(c.Y)) - 1 |
| 356 |
for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) { |
| 357 |
forward++ |
| 358 |
} |
| 359 |
|
| 360 |
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf)) |
| 361 |
c.OrigSelection[1] = c.CurSelection[1] |
| 362 |
c.Loc = c.CurSelection[1] |
| 363 |
} |
| 364 |
|
| 365 |
// AddWordToSelection adds the word the cursor is currently on |
| 366 |
// to the selection |
| 367 |
func (c *Cursor) AddWordToSelection() { |
| 368 |
if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) { |
| 369 |
c.CurSelection = c.OrigSelection |
| 370 |
return |
| 371 |
} |
| 372 |
|
| 373 |
if c.Loc.LessThan(c.OrigSelection[0]) { |
| 374 |
backward := c.X |
| 375 |
|
| 376 |
for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) { |
| 377 |
backward-- |
| 378 |
} |
| 379 |
|
| 380 |
c.SetSelectionStart(Loc{backward, c.Y}) |
| 381 |
c.SetSelectionEnd(c.OrigSelection[1]) |
| 382 |
} |
| 383 |
|
| 384 |
if c.Loc.GreaterThan(c.OrigSelection[1]) { |
| 385 |
forward := c.X |
| 386 |
|
| 387 |
lineLen := util.CharacterCount(c.buf.LineBytes(c.Y)) - 1 |
| 388 |
for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) { |
| 389 |
forward++ |
| 390 |
} |
| 391 |
|
| 392 |
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf)) |
| 393 |
c.SetSelectionStart(c.OrigSelection[0]) |
| 394 |
} |
| 395 |
|
| 396 |
c.Loc = c.CurSelection[1] |
| 397 |
} |
| 398 |
|
| 399 |
// SelectTo selects from the current cursor location to the given |
| 400 |
// location |
| 401 |
func (c *Cursor) SelectTo(loc Loc) { |
| 402 |
if loc.GreaterThan(c.OrigSelection[0]) { |
| 403 |
c.SetSelectionStart(c.OrigSelection[0]) |
| 404 |
c.SetSelectionEnd(loc) |
| 405 |
} else { |
| 406 |
c.SetSelectionStart(loc) |
| 407 |
c.SetSelectionEnd(c.OrigSelection[0]) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// WordRight moves the cursor one word to the right |
| 412 |
func (c *Cursor) WordRight() { |
| 413 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 414 |
c.Right() |
| 415 |
return |
| 416 |
} |
| 417 |
for util.IsWhitespace(c.RuneUnder(c.X)) { |
| 418 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 419 |
return |
| 420 |
} |
| 421 |
c.Right() |
| 422 |
} |
| 423 |
if util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) && |
| 424 |
util.IsNonWordChar(c.RuneUnder(c.X+1)) { |
| 425 |
for util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 426 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 427 |
return |
| 428 |
} |
| 429 |
c.Right() |
| 430 |
} |
| 431 |
return |
| 432 |
} |
| 433 |
c.Right() |
| 434 |
for util.IsWordChar(c.RuneUnder(c.X)) { |
| 435 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 436 |
return |
| 437 |
} |
| 438 |
c.Right() |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
// WordLeft moves the cursor one word to the left |
| 443 |
func (c *Cursor) WordLeft() { |
| 444 |
if c.X == 0 { |
| 445 |
c.Left() |
| 446 |
return |
| 447 |
} |
| 448 |
c.Left() |
| 449 |
for util.IsWhitespace(c.RuneUnder(c.X)) { |
| 450 |
if c.X == 0 { |
| 451 |
return |
| 452 |
} |
| 453 |
c.Left() |
| 454 |
} |
| 455 |
if util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) && |
| 456 |
util.IsNonWordChar(c.RuneUnder(c.X-1)) { |
| 457 |
for util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 458 |
if c.X == 0 { |
| 459 |
return |
| 460 |
} |
| 461 |
c.Left() |
| 462 |
} |
| 463 |
c.Right() |
| 464 |
return |
| 465 |
} |
| 466 |
c.Left() |
| 467 |
for util.IsWordChar(c.RuneUnder(c.X)) { |
| 468 |
if c.X == 0 { |
| 469 |
return |
| 470 |
} |
| 471 |
c.Left() |
| 472 |
} |
| 473 |
c.Right() |
| 474 |
} |
| 475 |
|
| 476 |
// SubWordRight moves the cursor one sub-word to the right |
| 477 |
func (c *Cursor) SubWordRight() { |
| 478 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 479 |
c.Right() |
| 480 |
return |
| 481 |
} |
| 482 |
if util.IsWhitespace(c.RuneUnder(c.X)) { |
| 483 |
for util.IsWhitespace(c.RuneUnder(c.X)) { |
| 484 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 485 |
return |
| 486 |
} |
| 487 |
c.Right() |
| 488 |
} |
| 489 |
return |
| 490 |
} |
| 491 |
if util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 492 |
for util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 493 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 494 |
return |
| 495 |
} |
| 496 |
c.Right() |
| 497 |
} |
| 498 |
return |
| 499 |
} |
| 500 |
if util.IsSubwordDelimiter(c.RuneUnder(c.X)) { |
| 501 |
for util.IsSubwordDelimiter(c.RuneUnder(c.X)) { |
| 502 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 503 |
return |
| 504 |
} |
| 505 |
c.Right() |
| 506 |
} |
| 507 |
if util.IsWhitespace(c.RuneUnder(c.X)) { |
| 508 |
return |
| 509 |
} |
| 510 |
} |
| 511 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 512 |
return |
| 513 |
} |
| 514 |
if util.IsUpperLetter(c.RuneUnder(c.X)) && |
| 515 |
util.IsUpperLetter(c.RuneUnder(c.X+1)) { |
| 516 |
for util.IsUpperAlphanumeric(c.RuneUnder(c.X)) { |
| 517 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 518 |
return |
| 519 |
} |
| 520 |
c.Right() |
| 521 |
} |
| 522 |
if util.IsLowerAlphanumeric(c.RuneUnder(c.X)) { |
| 523 |
c.Left() |
| 524 |
} |
| 525 |
} else { |
| 526 |
c.Right() |
| 527 |
for util.IsLowerAlphanumeric(c.RuneUnder(c.X)) { |
| 528 |
if c.X == util.CharacterCount(c.buf.LineBytes(c.Y)) { |
| 529 |
return |
| 530 |
} |
| 531 |
c.Right() |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// SubWordLeft moves the cursor one sub-word to the left |
| 537 |
func (c *Cursor) SubWordLeft() { |
| 538 |
if c.X == 0 { |
| 539 |
c.Left() |
| 540 |
return |
| 541 |
} |
| 542 |
c.Left() |
| 543 |
if util.IsWhitespace(c.RuneUnder(c.X)) { |
| 544 |
for util.IsWhitespace(c.RuneUnder(c.X)) { |
| 545 |
if c.X == 0 { |
| 546 |
return |
| 547 |
} |
| 548 |
c.Left() |
| 549 |
} |
| 550 |
c.Right() |
| 551 |
return |
| 552 |
} |
| 553 |
if util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 554 |
for util.IsNonWordChar(c.RuneUnder(c.X)) && !util.IsWhitespace(c.RuneUnder(c.X)) { |
| 555 |
if c.X == 0 { |
| 556 |
return |
| 557 |
} |
| 558 |
c.Left() |
| 559 |
} |
| 560 |
c.Right() |
| 561 |
return |
| 562 |
} |
| 563 |
if util.IsSubwordDelimiter(c.RuneUnder(c.X)) { |
| 564 |
for util.IsSubwordDelimiter(c.RuneUnder(c.X)) { |
| 565 |
if c.X == 0 { |
| 566 |
return |
| 567 |
} |
| 568 |
c.Left() |
| 569 |
} |
| 570 |
if util.IsWhitespace(c.RuneUnder(c.X)) { |
| 571 |
c.Right() |
| 572 |
return |
| 573 |
} |
| 574 |
} |
| 575 |
if c.X == 0 { |
| 576 |
return |
| 577 |
} |
| 578 |
if util.IsUpperLetter(c.RuneUnder(c.X)) && |
| 579 |
util.IsUpperLetter(c.RuneUnder(c.X-1)) { |
| 580 |
for util.IsUpperAlphanumeric(c.RuneUnder(c.X)) { |
| 581 |
if c.X == 0 { |
| 582 |
return |
| 583 |
} |
| 584 |
c.Left() |
| 585 |
} |
| 586 |
if !util.IsUpperAlphanumeric(c.RuneUnder(c.X)) { |
| 587 |
c.Right() |
| 588 |
} |
| 589 |
} else { |
| 590 |
for util.IsLowerAlphanumeric(c.RuneUnder(c.X)) { |
| 591 |
if c.X == 0 { |
| 592 |
return |
| 593 |
} |
| 594 |
c.Left() |
| 595 |
} |
| 596 |
if !util.IsAlphanumeric(c.RuneUnder(c.X)) { |
| 597 |
c.Right() |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
// RuneUnder returns the rune under the given x position |
| 603 |
func (c *Cursor) RuneUnder(x int) rune { |
| 604 |
line := c.buf.LineBytes(c.Y) |
| 605 |
if len(line) == 0 || x >= util.CharacterCount(line) { |
| 606 |
return '\n' |
| 607 |
} else if x < 0 { |
| 608 |
x = 0 |
| 609 |
} |
| 610 |
i := 0 |
| 611 |
for len(line) > 0 { |
| 612 |
r, _, size := util.DecodeCharacter(line) |
| 613 |
line = line[size:] |
| 614 |
|
| 615 |
if i == x { |
| 616 |
return r |
| 617 |
} |
| 618 |
|
| 619 |
i++ |
| 620 |
} |
| 621 |
return '\n' |
| 622 |
} |
| 623 |
|
| 624 |
func (c *Cursor) StoreVisualX() { |
| 625 |
c.LastVisualX = c.GetVisualX(false) |
| 626 |
c.LastWrappedVisualX = c.GetVisualX(true) |
| 627 |
} |