6 "github.com/zyedidia/clipboard"
7 "github.com/zyedidia/micro/internal/util"
10 // InBounds returns whether the given location is a valid character position in the given buffer
11 func InBounds(pos Loc, buf *Buffer) bool {
12 if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
19 // The Cursor struct stores the location of the cursor in the buffer
20 // as well as the selection
25 // Last cursor x position
28 // The current selection as a range of character numbers (inclusive)
30 // The original selection as a range of character numbers
31 // This is used for line and word selection where it is necessary
32 // to know what the original selection was
35 // Which cursor index is this (for multiple cursors)
39 func NewCursor(b *Buffer, l Loc) *Cursor {
48 func (c *Cursor) SetBuf(b *Buffer) {
52 func (c *Cursor) Buf() *Buffer {
56 // Goto puts the cursor at the given cursor's location and gives
57 // the current cursor its selection too
58 func (c *Cursor) Goto(b Cursor) {
59 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
60 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
63 // GotoLoc puts the cursor at the given cursor's location and gives
64 // the current cursor its selection too
65 func (c *Cursor) GotoLoc(l Loc) {
70 // GetVisualX returns the x value of the cursor in visual spaces
71 func (c *Cursor) GetVisualX() int {
77 bytes := c.buf.LineBytes(c.Y)
78 tabsize := int(c.buf.Settings["tabsize"].(float64))
79 if c.X > utf8.RuneCount(bytes) {
80 c.X = utf8.RuneCount(bytes) - 1
83 return util.StringWidth(bytes, c.X, tabsize)
86 // GetCharPosInLine gets the char position of a visual x y
87 // coordinate (this is necessary because tabs are 1 char but
89 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
90 tabsize := int(c.buf.Settings["tabsize"].(float64))
91 return util.GetCharPosInLine(b, visualPos, tabsize)
94 // Start moves the cursor to the start of the line it is on
95 func (c *Cursor) Start() {
97 c.LastVisualX = c.GetVisualX()
100 // StartOfText moves the cursor to the first non-whitespace rune of
102 func (c *Cursor) StartOfText() {
104 for util.IsWhitespace(c.RuneUnder(c.X)) {
105 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
112 // End moves the cursor to the end of the line it is on
113 func (c *Cursor) End() {
114 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
115 c.LastVisualX = c.GetVisualX()
118 // CopySelection copies the user's selection to either "primary"
120 func (c *Cursor) CopySelection(target string) {
121 if c.HasSelection() {
122 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
123 clipboard.WriteAll(string(c.GetSelection()), target)
128 // ResetSelection resets the user's selection
129 func (c *Cursor) ResetSelection() {
130 c.CurSelection[0] = c.buf.Start()
131 c.CurSelection[1] = c.buf.Start()
134 // SetSelectionStart sets the start of the selection
135 func (c *Cursor) SetSelectionStart(pos Loc) {
136 c.CurSelection[0] = pos
139 // SetSelectionEnd sets the end of the selection
140 func (c *Cursor) SetSelectionEnd(pos Loc) {
141 c.CurSelection[1] = pos
144 // HasSelection returns whether or not the user has selected anything
145 func (c *Cursor) HasSelection() bool {
146 return c.CurSelection[0] != c.CurSelection[1]
149 // DeleteSelection deletes the currently selected text
150 func (c *Cursor) DeleteSelection() {
151 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
152 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
153 c.Loc = c.CurSelection[1]
154 } else if !c.HasSelection() {
157 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
158 c.Loc = c.CurSelection[0]
162 // Deselect closes the cursor's current selection
163 // Start indicates whether the cursor should be placed
164 // at the start or end of the selection
165 func (c *Cursor) Deselect(start bool) {
166 if c.HasSelection() {
168 c.Loc = c.CurSelection[0]
170 c.Loc = c.CurSelection[1].Move(-1, c.buf)
177 // GetSelection returns the cursor's selection
178 func (c *Cursor) GetSelection() []byte {
179 if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
180 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
181 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
183 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
188 // SelectLine selects the current line
189 func (c *Cursor) SelectLine() {
191 c.SetSelectionStart(c.Loc)
193 if len(c.buf.lines)-1 > c.Y {
194 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
196 c.SetSelectionEnd(c.Loc)
199 c.OrigSelection = c.CurSelection
202 // AddLineToSelection adds the current line to the selection
203 func (c *Cursor) AddLineToSelection() {
204 if c.Loc.LessThan(c.OrigSelection[0]) {
206 c.SetSelectionStart(c.Loc)
207 c.SetSelectionEnd(c.OrigSelection[1])
209 if c.Loc.GreaterThan(c.OrigSelection[1]) {
211 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
212 c.SetSelectionStart(c.OrigSelection[0])
215 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
216 c.CurSelection = c.OrigSelection
220 // UpN moves the cursor up N lines (if possible)
221 func (c *Cursor) UpN(amount int) {
222 proposedY := c.Y - amount
225 } else if proposedY >= len(c.buf.lines) {
226 proposedY = len(c.buf.lines) - 1
229 bytes := c.buf.LineBytes(proposedY)
230 c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
232 if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
233 c.X = utf8.RuneCount(bytes)
239 // DownN moves the cursor down N lines (if possible)
240 func (c *Cursor) DownN(amount int) {
244 // Up moves the cursor up one line (if possible)
245 func (c *Cursor) Up() {
249 // Down moves the cursor down one line (if possible)
250 func (c *Cursor) Down() {
254 // Left moves the cursor left one cell (if possible) or to
255 // the previous line if it is at the beginning
256 func (c *Cursor) Left() {
257 if c.Loc == c.buf.Start() {
269 // Right moves the cursor right one cell (if possible) or
270 // to the next line if it is at the end
271 func (c *Cursor) Right() {
272 if c.Loc == c.buf.End() {
275 if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
284 // Relocate makes sure that the cursor is inside the bounds
285 // of the buffer If it isn't, it moves it to be within the
287 func (c *Cursor) Relocate() {
290 } else if c.Y >= len(c.buf.lines) {
291 c.Y = len(c.buf.lines) - 1
296 } else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
297 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
301 // SelectWord selects the word the cursor is currently on
302 func (c *Cursor) SelectWord() {
303 if len(c.buf.LineBytes(c.Y)) == 0 {
307 if !util.IsWordChar(c.RuneUnder(c.X)) {
308 c.SetSelectionStart(c.Loc)
309 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
310 c.OrigSelection = c.CurSelection
314 forward, backward := c.X, c.X
316 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
320 c.SetSelectionStart(Loc{backward, c.Y})
321 c.OrigSelection[0] = c.CurSelection[0]
323 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
324 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
328 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
329 c.OrigSelection[1] = c.CurSelection[1]
330 c.Loc = c.CurSelection[1]
333 // AddWordToSelection adds the word the cursor is currently on
335 func (c *Cursor) AddWordToSelection() {
336 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
337 c.CurSelection = c.OrigSelection
341 if c.Loc.LessThan(c.OrigSelection[0]) {
344 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
348 c.SetSelectionStart(Loc{backward, c.Y})
349 c.SetSelectionEnd(c.OrigSelection[1])
352 if c.Loc.GreaterThan(c.OrigSelection[1]) {
355 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
356 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
360 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
361 c.SetSelectionStart(c.OrigSelection[0])
364 c.Loc = c.CurSelection[1]
367 // SelectTo selects from the current cursor location to the given
369 func (c *Cursor) SelectTo(loc Loc) {
370 if loc.GreaterThan(c.OrigSelection[0]) {
371 c.SetSelectionStart(c.OrigSelection[0])
372 c.SetSelectionEnd(loc)
374 c.SetSelectionStart(loc)
375 c.SetSelectionEnd(c.OrigSelection[0])
379 // WordRight moves the cursor one word to the right
380 func (c *Cursor) WordRight() {
381 for util.IsWhitespace(c.RuneUnder(c.X)) {
382 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
389 for util.IsWordChar(c.RuneUnder(c.X)) {
390 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
397 // WordLeft moves the cursor one word to the left
398 func (c *Cursor) WordLeft() {
400 for util.IsWhitespace(c.RuneUnder(c.X)) {
407 for util.IsWordChar(c.RuneUnder(c.X)) {
416 // RuneUnder returns the rune under the given x position
417 func (c *Cursor) RuneUnder(x int) rune {
418 line := c.buf.LineBytes(c.Y)
419 if len(line) == 0 || x >= utf8.RuneCount(line) {
426 r, size := utf8.DecodeRune(line)
438 func (c *Cursor) StoreVisualX() {
439 c.LastVisualX = c.GetVisualX()