Skip to content

Commit f8e532b

Browse files
authored
Adding selection for ParagraphPrevious and ParagraphNext. (zyedidia#3353)
Also tweaked the behavior of Paragraph/{Previous/Next} so that it skips all empty lines immediately next to cursor position, until it finds the start/end of the paragraph closest to it. Once it finds the paragraph closest to it, the same behavior as before applies. With the previous behavior if the cursor was surrounded by empty lines, then Paragraph/{Previous/Next} would only jump to the next empty line, instead of jumping to the start/end of a paragraph.
1 parent 762e31f commit f8e532b

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

internal/action/actions.go

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -490,38 +490,92 @@ func (h *BufPane) SelectToEndOfLine() bool {
490490
return true
491491
}
492492

493-
// ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none
494-
func (h *BufPane) ParagraphPrevious() bool {
493+
func (h *BufPane) paragraphPrevious() {
495494
var line int
495+
// Skip to the first non-empty line
496496
for line = h.Cursor.Y; line > 0; line-- {
497-
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
497+
if len(h.Buf.LineBytes(line)) != 0 {
498+
break
499+
}
500+
}
501+
// Find the first empty line
502+
for ; line > 0; line-- {
503+
if len(h.Buf.LineBytes(line)) == 0 {
498504
h.Cursor.X = 0
499505
h.Cursor.Y = line
500506
break
501507
}
502508
}
503-
// If no empty line found. move cursor to end of buffer
509+
// If no empty line was found, move the cursor to the start of the buffer
504510
if line == 0 {
505511
h.Cursor.Loc = h.Buf.Start()
506512
}
507-
h.Relocate()
508-
return true
509513
}
510514

511-
// ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none
512-
func (h *BufPane) ParagraphNext() bool {
515+
func (h *BufPane) paragraphNext() {
513516
var line int
517+
// Skip to the first non-empty line
514518
for line = h.Cursor.Y; line < h.Buf.LinesNum(); line++ {
515-
if len(h.Buf.LineBytes(line)) == 0 && line != h.Cursor.Y {
519+
if len(h.Buf.LineBytes(line)) != 0 {
520+
break
521+
}
522+
}
523+
// Find the first empty line
524+
for ; line < h.Buf.LinesNum(); line++ {
525+
if len(h.Buf.LineBytes(line)) == 0 {
516526
h.Cursor.X = 0
517527
h.Cursor.Y = line
518528
break
519529
}
520530
}
521-
// If no empty line found. move cursor to end of buffer
531+
// If no empty line was found, move the cursor to the end of the buffer
522532
if line == h.Buf.LinesNum() {
523533
h.Cursor.Loc = h.Buf.End()
524534
}
535+
}
536+
537+
// ParagraphPrevious moves the cursor to the first empty line that comes before
538+
// the paragraph closest to the cursor, or beginning of the buffer if there
539+
// isn't a paragraph
540+
func (h *BufPane) ParagraphPrevious() bool {
541+
h.Cursor.Deselect(true)
542+
h.paragraphPrevious()
543+
h.Relocate()
544+
return true
545+
}
546+
547+
// ParagraphNext moves the cursor to the first empty line that comes after the
548+
// paragraph closest to the cursor, or end of the buffer if there isn't a
549+
// paragraph
550+
func (h *BufPane) ParagraphNext() bool {
551+
h.Cursor.Deselect(true)
552+
h.paragraphNext()
553+
h.Relocate()
554+
return true
555+
}
556+
557+
// SelectToParagraphPrevious selects to the first empty line that comes before
558+
// the paragraph closest to the cursor, or beginning of the buffer if there
559+
// isn't a paragraph
560+
func (h *BufPane) SelectToParagraphPrevious() bool {
561+
if !h.Cursor.HasSelection() {
562+
h.Cursor.OrigSelection[0] = h.Cursor.Loc
563+
}
564+
h.paragraphPrevious()
565+
h.Cursor.SelectTo(h.Cursor.Loc)
566+
h.Relocate()
567+
return true
568+
}
569+
570+
// SelectToParagraphNext selects to the first empty line that comes after the
571+
// paragraph closest to the cursor, or end of the buffer if there isn't a
572+
// paragraph
573+
func (h *BufPane) SelectToParagraphNext() bool {
574+
if !h.Cursor.HasSelection() {
575+
h.Cursor.OrigSelection[0] = h.Cursor.Loc
576+
}
577+
h.paragraphNext()
578+
h.Cursor.SelectTo(h.Cursor.Loc)
525579
h.Relocate()
526580
return true
527581
}

internal/action/bufpane.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ var BufKeyActions = map[string]BufKeyAction{
759759
"SelectToEndOfLine": (*BufPane).SelectToEndOfLine,
760760
"ParagraphPrevious": (*BufPane).ParagraphPrevious,
761761
"ParagraphNext": (*BufPane).ParagraphNext,
762+
"SelectToParagraphPrevious": (*BufPane).SelectToParagraphPrevious,
763+
"SelectToParagraphNext": (*BufPane).SelectToParagraphNext,
762764
"InsertNewline": (*BufPane).InsertNewline,
763765
"Backspace": (*BufPane).Backspace,
764766
"Delete": (*BufPane).Delete,

runtime/help/keybindings.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ StartOfText
237237
StartOfTextToggle
238238
ParagraphPrevious
239239
ParagraphNext
240+
SelectToParagraphPrevious
241+
SelectToParagraphNext
240242
ToggleHelp
241243
ToggleDiffGutter
242244
ToggleRuler

0 commit comments

Comments
 (0)