github.com/grzegorz-zur/bm@v0.0.0-20240312214136-6fc133e3e2c0/move.go (about)

     1  package main
     2  
     3  // MoveLeft moves cursor to the left.
     4  func (file *File) MoveLeft() {
     5  	if file.AtFileStart() {
     6  		return
     7  	}
     8  	_, size := file.last()
     9  	file.location -= size
    10  }
    11  
    12  // MoveRight moves cursor to the right.
    13  func (file *File) MoveRight() {
    14  	if file.AtFileEnd() {
    15  		return
    16  	}
    17  	_, size := file.current()
    18  	file.location += size
    19  }
    20  
    21  // MoveUp moves cursor up.
    22  func (file *File) MoveUp() {
    23  	location := file.location
    24  	file.MoveLineStart()
    25  	columns := location - file.location
    26  	file.MoveLeft()
    27  	file.MoveLineStart()
    28  	for column := 0; column < columns && !file.AtLineEnd(); column++ {
    29  		file.MoveRight()
    30  	}
    31  }
    32  
    33  // MoveDown moves cursor down.
    34  func (file *File) MoveDown() {
    35  	location := file.location
    36  	file.MoveLineStart()
    37  	columns := location - file.location
    38  	file.MoveLineEnd()
    39  	file.MoveRight()
    40  	for column := 0; column < columns && !file.AtLineEnd(); column++ {
    41  		file.MoveRight()
    42  	}
    43  }
    44  
    45  // MoveLineStart moves cursor to the start of line.
    46  func (file *File) MoveLineStart() {
    47  	for !file.AtLineStart() {
    48  		file.MoveLeft()
    49  	}
    50  }
    51  
    52  // MoveLineEnd moves cursor to the end of line.
    53  func (file *File) MoveLineEnd() {
    54  	for !file.AtLineEnd() {
    55  		file.MoveRight()
    56  	}
    57  }
    58  
    59  // MoveFileStart moves cursor to the start of file.
    60  func (file *File) MoveFileStart() {
    61  	file.location = 0
    62  }
    63  
    64  // MoveFileEnd moves cursor to the end of file.
    65  func (file *File) MoveFileEnd() {
    66  	file.location = len(file.content)
    67  }
    68  
    69  // MoveWordPrevious moves cursor to previous word.
    70  func (file *File) MoveWordPrevious() {
    71  	file.MoveLeft()
    72  	for !file.AtWord() {
    73  		file.MoveLeft()
    74  	}
    75  }
    76  
    77  // MoveWordNext moves cursor to the next word.
    78  func (file *File) MoveWordNext() {
    79  	file.MoveRight()
    80  	for !file.AtWord() {
    81  		file.MoveRight()
    82  	}
    83  }
    84  
    85  // MoveParagraphPrevious moves cursor to the previous paragraph.
    86  func (file *File) MoveParagraphPrevious() {
    87  	file.MoveLeft()
    88  	for !file.AtParagraph() {
    89  		file.MoveLeft()
    90  	}
    91  }
    92  
    93  // MoveParagraphNext moves to cursor the next paragraph.
    94  func (file *File) MoveParagraphNext() {
    95  	file.MoveRight()
    96  	for !file.AtParagraph() {
    97  		file.MoveRight()
    98  	}
    99  }