6 runewidth "github.com/mattn/go-runewidth"
7 "github.com/zyedidia/clipboard"
8 "github.com/zyedidia/micro/cmd/micro/util"
11 // InBounds returns whether the given location is a valid character position in the given buffer
12 func InBounds(pos Loc, buf *Buffer) bool {
13 if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
20 // The Cursor struct stores the location of the cursor in the buffer
21 // as well as the selection
26 // Last cursor x position
29 // The current selection as a range of character numbers (inclusive)
31 // The original selection as a range of character numbers
32 // This is used for line and word selection where it is necessary
33 // to know what the original selection was
36 // Which cursor index is this (for multiple cursors)
40 // Goto puts the cursor at the given cursor's location and gives
41 // the current cursor its selection too
42 func (c *Cursor) Goto(b Cursor) {
43 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
44 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
47 // GotoLoc puts the cursor at the given cursor's location and gives
48 // the current cursor its selection too
49 func (c *Cursor) GotoLoc(l Loc) {
54 // GetVisualX returns the x value of the cursor in visual spaces
55 func (c *Cursor) GetVisualX() int {
61 bytes := c.Buf.LineBytes(c.Y)
62 tabsize := int(c.Buf.Settings["tabsize"].(float64))
63 if c.X > utf8.RuneCount(bytes) {
64 c.X = utf8.RuneCount(bytes) - 1
67 return util.StringWidth(bytes, c.X, tabsize)
70 // GetCharPosInLine gets the char position of a visual x y
71 // coordinate (this is necessary because tabs are 1 char but
73 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
74 tabsize := int(c.Buf.Settings["tabsize"].(float64))
76 // Scan rune by rune until we exceed the visual width that we are
77 // looking for. Then we can return the character position we have found
79 width := 0 // string visual width
81 r, size := utf8.DecodeRune(b)
86 ts := tabsize - (width % tabsize)
89 width += runewidth.RuneWidth(r)
92 if width >= visualPos {
93 if width == visualPos {
104 // Start moves the cursor to the start of the line it is on
105 func (c *Cursor) Start() {
107 c.LastVisualX = c.GetVisualX()
110 // StartOfText moves the cursor to the first non-whitespace rune of
112 func (c *Cursor) StartOfText() {
114 for util.IsWhitespace(c.RuneUnder(c.X)) {
115 if c.X == utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
122 // End moves the cursor to the end of the line it is on
123 func (c *Cursor) End() {
124 c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
125 c.LastVisualX = c.GetVisualX()
128 // CopySelection copies the user's selection to either "primary"
130 func (c *Cursor) CopySelection(target string) {
131 if c.HasSelection() {
132 if target != "primary" || c.Buf.Settings["useprimary"].(bool) {
133 clipboard.WriteAll(string(c.GetSelection()), target)
138 // ResetSelection resets the user's selection
139 func (c *Cursor) ResetSelection() {
140 c.CurSelection[0] = c.Buf.Start()
141 c.CurSelection[1] = c.Buf.Start()
144 // SetSelectionStart sets the start of the selection
145 func (c *Cursor) SetSelectionStart(pos Loc) {
146 c.CurSelection[0] = pos
149 // SetSelectionEnd sets the end of the selection
150 func (c *Cursor) SetSelectionEnd(pos Loc) {
151 c.CurSelection[1] = pos
154 // HasSelection returns whether or not the user has selected anything
155 func (c *Cursor) HasSelection() bool {
156 return c.CurSelection[0] != c.CurSelection[1]
159 // DeleteSelection deletes the currently selected text
160 func (c *Cursor) DeleteSelection() {
161 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
162 c.Buf.Remove(c.CurSelection[1], c.CurSelection[0])
163 c.Loc = c.CurSelection[1]
164 } else if !c.HasSelection() {
167 c.Buf.Remove(c.CurSelection[0], c.CurSelection[1])
168 c.Loc = c.CurSelection[0]
172 // Deselect closes the cursor's current selection
173 // Start indicates whether the cursor should be placed
174 // at the start or end of the selection
175 func (c *Cursor) Deselect(start bool) {
176 if c.HasSelection() {
178 c.Loc = c.CurSelection[0]
180 c.Loc = c.CurSelection[1]
187 // GetSelection returns the cursor's selection
188 func (c *Cursor) GetSelection() []byte {
189 if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
190 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
191 return c.Buf.Substr(c.CurSelection[1], c.CurSelection[0])
193 return c.Buf.Substr(c.CurSelection[0], c.CurSelection[1])
198 // SelectLine selects the current line
199 func (c *Cursor) SelectLine() {
201 c.SetSelectionStart(c.Loc)
203 if len(c.Buf.lines)-1 > c.Y {
204 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
206 c.SetSelectionEnd(c.Loc)
209 c.OrigSelection = c.CurSelection
212 // AddLineToSelection adds the current line to the selection
213 func (c *Cursor) AddLineToSelection() {
214 if c.Loc.LessThan(c.OrigSelection[0]) {
216 c.SetSelectionStart(c.Loc)
217 c.SetSelectionEnd(c.OrigSelection[1])
219 if c.Loc.GreaterThan(c.OrigSelection[1]) {
221 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
222 c.SetSelectionStart(c.OrigSelection[0])
225 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
226 c.CurSelection = c.OrigSelection
230 // UpN moves the cursor up N lines (if possible)
231 func (c *Cursor) UpN(amount int) {
232 proposedY := c.Y - amount
235 } else if proposedY >= len(c.Buf.lines) {
236 proposedY = len(c.Buf.lines) - 1
239 bytes := c.Buf.LineBytes(proposedY)
240 c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
242 if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
243 c.X = utf8.RuneCount(bytes)
249 // DownN moves the cursor down N lines (if possible)
250 func (c *Cursor) DownN(amount int) {
254 // Up moves the cursor up one line (if possible)
255 func (c *Cursor) Up() {
259 // Down moves the cursor down one line (if possible)
260 func (c *Cursor) Down() {
264 // Left moves the cursor left one cell (if possible) or to
265 // the previous line if it is at the beginning
266 func (c *Cursor) Left() {
267 if c.Loc == c.Buf.Start() {
279 // Right moves the cursor right one cell (if possible) or
280 // to the next line if it is at the end
281 func (c *Cursor) Right() {
282 if c.Loc == c.Buf.End() {
285 if c.X < utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
294 // Relocate makes sure that the cursor is inside the bounds
295 // of the buffer If it isn't, it moves it to be within the
297 func (c *Cursor) Relocate() {
300 } else if c.Y >= len(c.Buf.lines) {
301 c.Y = len(c.Buf.lines) - 1
306 } else if c.X > utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
307 c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
311 // SelectWord selects the word the cursor is currently on
312 func (c *Cursor) SelectWord() {
313 if len(c.Buf.LineBytes(c.Y)) == 0 {
317 if !util.IsWordChar(c.RuneUnder(c.X)) {
318 c.SetSelectionStart(c.Loc)
319 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
320 c.OrigSelection = c.CurSelection
324 forward, backward := c.X, c.X
326 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
330 c.SetSelectionStart(Loc{backward, c.Y})
331 c.OrigSelection[0] = c.CurSelection[0]
333 lineLen := utf8.RuneCount(c.Buf.LineBytes(c.Y)) - 1
334 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
338 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.Buf))
339 c.OrigSelection[1] = c.CurSelection[1]
340 c.Loc = c.CurSelection[1]
343 // AddWordToSelection adds the word the cursor is currently on
345 func (c *Cursor) AddWordToSelection() {
346 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
347 c.CurSelection = c.OrigSelection
351 if c.Loc.LessThan(c.OrigSelection[0]) {
354 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
358 c.SetSelectionStart(Loc{backward, c.Y})
359 c.SetSelectionEnd(c.OrigSelection[1])
362 if c.Loc.GreaterThan(c.OrigSelection[1]) {
365 lineLen := utf8.RuneCount(c.Buf.LineBytes(c.Y)) - 1
366 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
370 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.Buf))
371 c.SetSelectionStart(c.OrigSelection[0])
374 c.Loc = c.CurSelection[1]
377 // SelectTo selects from the current cursor location to the given
379 func (c *Cursor) SelectTo(loc Loc) {
380 if loc.GreaterThan(c.OrigSelection[0]) {
381 c.SetSelectionStart(c.OrigSelection[0])
382 c.SetSelectionEnd(loc)
384 c.SetSelectionStart(loc)
385 c.SetSelectionEnd(c.OrigSelection[0])
389 // WordRight moves the cursor one word to the right
390 func (c *Cursor) WordRight() {
391 for util.IsWhitespace(c.RuneUnder(c.X)) {
392 if c.X == utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
399 for util.IsWordChar(c.RuneUnder(c.X)) {
400 if c.X == utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
407 // WordLeft moves the cursor one word to the left
408 func (c *Cursor) WordLeft() {
410 for util.IsWhitespace(c.RuneUnder(c.X)) {
417 for util.IsWordChar(c.RuneUnder(c.X)) {
426 // RuneUnder returns the rune under the given x position
427 func (c *Cursor) RuneUnder(x int) rune {
428 line := c.Buf.LineBytes(c.Y)
429 if len(line) == 0 || x >= utf8.RuneCount(line) {
436 r, size := utf8.DecodeRune(line)
448 func (c *Cursor) StoreVisualX() {
449 c.LastVisualX = c.GetVisualX()