7 // FromCharPos converts from a character position to an x, y position
8 func FromCharPos(loc int, buf *Buffer) (int, int) {
9 return FromCharPosStart(0, 0, 0, loc, buf)
12 // FromCharPosStart converts from a character position to an x, y position, starting at the specified character location
13 func FromCharPosStart(startLoc, startX, startY, loc int, buf *Buffer) (int, int) {
15 x, y := startX, startY
17 lineLen := Count(buf.Lines[y]) + 1
18 for charNum+lineLen <= loc {
21 if y >= buf.NumLines {
24 lineLen = Count(buf.Lines[y]) + 1
31 // ToCharPos converts from an x, y position to a character position
32 func ToCharPos(x, y int, buf *Buffer) int {
34 for i := 0; i < y; i++ {
35 // + 1 for the newline
36 loc += Count(buf.Lines[i]) + 1
42 // The Cursor struct stores the location of the cursor in the view
43 // The complicated part about the cursor is storing its location.
44 // The cursor must be displayed at an x, y location, but since the buffer
45 // uses a rope to store text, to insert text we must have an index. It
46 // is also simpler to use character indicies for other tasks such as
51 // The cursor display location
55 // Last cursor x position
58 // The current selection as a range of character numbers (inclusive)
60 // The original selection as a range of character numbers
61 // This is used for line and word selection where it is necessary
62 // to know what the original selection was
66 // SetLoc sets the location of the cursor in terms of character number
67 // and not x, y location
68 // It's just a simple wrapper of FromCharPos
69 func (c *Cursor) SetLoc(loc int) {
70 c.x, c.y = FromCharPos(loc, c.Buf)
71 c.lastVisualX = c.GetVisualX()
74 // Loc gets the cursor location in terms of character number instead
76 // It's just a simple wrapper of ToCharPos
77 func (c *Cursor) Loc() int {
78 return ToCharPos(c.x, c.y, c.Buf)
81 // ResetSelection resets the user's selection
82 func (c *Cursor) ResetSelection() {
87 // HasSelection returns whether or not the user has selected anything
88 func (c *Cursor) HasSelection() bool {
89 return c.curSelection[0] != c.curSelection[1]
92 // DeleteSelection deletes the currently selected text
93 func (c *Cursor) DeleteSelection() {
94 if c.curSelection[0] > c.curSelection[1] {
95 c.Buf.Remove(c.curSelection[1], c.curSelection[0])
96 c.SetLoc(c.curSelection[1])
97 } else if c.GetSelection() == "" {
100 c.Buf.Remove(c.curSelection[0], c.curSelection[1])
101 c.SetLoc(c.curSelection[0])
105 // GetSelection returns the cursor's selection
106 func (c *Cursor) GetSelection() string {
107 if c.curSelection[0] > c.curSelection[1] {
108 return c.Buf.Substr(c.curSelection[1], c.curSelection[0])
110 return c.Buf.Substr(c.curSelection[0], c.curSelection[1])
113 // SelectLine selects the current line
114 func (c *Cursor) SelectLine() {
116 c.curSelection[0] = c.Loc()
118 if c.Buf.NumLines-1 > c.y {
119 c.curSelection[1] = c.Loc() + 1
121 c.curSelection[1] = c.Loc()
124 c.origSelection = c.curSelection
127 // AddLineToSelection adds the current line to the selection
128 func (c *Cursor) AddLineToSelection() {
131 if loc < c.origSelection[0] {
133 c.curSelection[0] = c.Loc()
134 c.curSelection[1] = c.origSelection[1]
136 if loc > c.origSelection[1] {
138 c.curSelection[1] = c.Loc()
139 c.curSelection[0] = c.origSelection[0]
142 if loc < c.origSelection[1] && loc > c.origSelection[0] {
143 c.curSelection = c.origSelection
147 // SelectWord selects the word the cursor is currently on
148 func (c *Cursor) SelectWord() {
149 if len(c.Buf.Lines[c.y]) == 0 {
153 if !IsWordChar(string(c.RuneUnder(c.x))) {
155 c.curSelection[0] = loc
156 c.curSelection[1] = loc + 1
157 c.origSelection = c.curSelection
161 forward, backward := c.x, c.x
163 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
167 c.curSelection[0] = ToCharPos(backward, c.y, c.Buf)
168 c.origSelection[0] = c.curSelection[0]
170 for forward < Count(c.Buf.Lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
174 c.curSelection[1] = ToCharPos(forward, c.y, c.Buf) + 1
175 c.origSelection[1] = c.curSelection[1]
176 c.SetLoc(c.curSelection[1])
179 // AddWordToSelection adds the word the cursor is currently on to the selection
180 func (c *Cursor) AddWordToSelection() {
183 if loc > c.origSelection[0] && loc < c.origSelection[1] {
184 c.curSelection = c.origSelection
188 if loc < c.origSelection[0] {
191 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
195 c.curSelection[0] = ToCharPos(backward, c.y, c.Buf)
196 c.curSelection[1] = c.origSelection[1]
199 if loc > c.origSelection[1] {
202 for forward < Count(c.Buf.Lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
206 c.curSelection[1] = ToCharPos(forward, c.y, c.Buf) + 1
207 c.curSelection[0] = c.origSelection[0]
210 c.SetLoc(c.curSelection[1])
213 // SelectTo selects from the current cursor location to the given location
214 func (c *Cursor) SelectTo(loc int) {
215 if loc > c.origSelection[0] {
216 c.curSelection[0] = c.origSelection[0]
217 c.curSelection[1] = loc
219 c.curSelection[0] = loc
220 c.curSelection[1] = c.origSelection[0]
224 // WordRight moves the cursor one word to the right
225 func (c *Cursor) WordRight() {
227 for IsWhitespace(c.RuneUnder(c.x)) {
228 if c.x == Count(c.Buf.Lines[c.y]) {
233 for !IsWhitespace(c.RuneUnder(c.x)) {
234 if c.x == Count(c.Buf.Lines[c.y]) {
241 // WordLeft moves the cursor one word to the left
242 func (c *Cursor) WordLeft() {
244 for IsWhitespace(c.RuneUnder(c.x)) {
250 for !IsWhitespace(c.RuneUnder(c.x)) {
259 // RuneUnder returns the rune under the given x position
260 func (c *Cursor) RuneUnder(x int) rune {
261 line := []rune(c.Buf.Lines[c.y])
273 // Up moves the cursor up one line (if possible)
274 func (c *Cursor) Up() {
278 runes := []rune(c.Buf.Lines[c.y])
279 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
280 if c.x > len(runes) {
286 // Down moves the cursor down one line (if possible)
287 func (c *Cursor) Down() {
288 if c.y < c.Buf.NumLines-1 {
291 runes := []rune(c.Buf.Lines[c.y])
292 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
293 if c.x > len(runes) {
299 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
300 func (c *Cursor) Left() {
310 c.lastVisualX = c.GetVisualX()
313 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
314 func (c *Cursor) Right() {
315 if c.Loc() == c.Buf.Len() {
318 if c.x < Count(c.Buf.Lines[c.y]) {
324 c.lastVisualX = c.GetVisualX()
327 // End moves the cursor to the end of the line it is on
328 func (c *Cursor) End() {
329 c.x = Count(c.Buf.Lines[c.y])
330 c.lastVisualX = c.GetVisualX()
333 // Start moves the cursor to the start of the line it is on
334 func (c *Cursor) Start() {
336 c.lastVisualX = c.GetVisualX()
339 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
340 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
342 tabSize := int(settings["tabsize"].(float64))
343 // This is the visual line -- every \t replaced with the correct number of spaces
344 visualLine := strings.Replace(c.Buf.Lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
345 if visualPos > Count(visualLine) {
346 visualPos = Count(visualLine)
348 numTabs := NumOccurences(visualLine[:visualPos], '\t')
349 if visualPos >= (tabSize-1)*numTabs {
350 return visualPos - (tabSize-1)*numTabs
352 return visualPos / tabSize
355 // GetVisualX returns the x value of the cursor in visual spaces
356 func (c *Cursor) GetVisualX() int {
357 runes := []rune(c.Buf.Lines[c.y])
358 tabSize := int(settings["tabsize"].(float64))
359 return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
362 // Relocate makes sure that the cursor is inside the bounds of the buffer
363 // If it isn't, it moves it to be within the buffer's lines
364 func (c *Cursor) Relocate() {
367 } else if c.y >= c.Buf.NumLines {
368 c.y = c.Buf.NumLines - 1
373 } else if c.x > Count(c.Buf.Lines[c.y]) {
374 c.x = Count(c.Buf.Lines[c.y])