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 // Clamp makes sure that the cursor is in the bounds of the buffer
67 // It cannot be less than 0 or greater than the buffer length
68 func (c *Cursor) Clamp() {
72 } else if loc > c.buf.Len() {
77 // SetLoc sets the location of the cursor in terms of character number
78 // and not x, y location
79 // It's just a simple wrapper of FromCharPos
80 func (c *Cursor) SetLoc(loc int) {
81 c.X, c.Y = FromCharPos(loc, c.buf)
82 c.LastVisualX = c.GetVisualX()
85 // Loc gets the cursor location in terms of character number instead
87 // It's just a simple wrapper of ToCharPos
88 func (c *Cursor) Loc() int {
89 return ToCharPos(c.X, c.Y, c.buf)
92 // ResetSelection resets the user's selection
93 func (c *Cursor) ResetSelection() {
98 // HasSelection returns whether or not the user has selected anything
99 func (c *Cursor) HasSelection() bool {
100 return c.CurSelection[0] != c.CurSelection[1]
103 // DeleteSelection deletes the currently selected text
104 func (c *Cursor) DeleteSelection() {
105 if c.CurSelection[0] > c.CurSelection[1] {
106 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
107 c.SetLoc(c.CurSelection[1])
108 } else if c.GetSelection() == "" {
111 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
112 c.SetLoc(c.CurSelection[0])
116 // GetSelection returns the cursor's selection
117 func (c *Cursor) GetSelection() string {
118 if c.CurSelection[0] > c.CurSelection[1] {
119 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
121 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
124 // SelectLine selects the current line
125 func (c *Cursor) SelectLine() {
127 c.CurSelection[0] = c.Loc()
129 if c.buf.NumLines-1 > c.Y {
130 c.CurSelection[1] = c.Loc() + 1
132 c.CurSelection[1] = c.Loc()
135 c.OrigSelection = c.CurSelection
138 // AddLineToSelection adds the current line to the selection
139 func (c *Cursor) AddLineToSelection() {
142 if loc < c.OrigSelection[0] {
144 c.CurSelection[0] = c.Loc()
145 c.CurSelection[1] = c.OrigSelection[1]
147 if loc > c.OrigSelection[1] {
149 c.CurSelection[1] = c.Loc()
150 c.CurSelection[0] = c.OrigSelection[0]
153 if loc < c.OrigSelection[1] && loc > c.OrigSelection[0] {
154 c.CurSelection = c.OrigSelection
158 // SelectWord selects the word the cursor is currently on
159 func (c *Cursor) SelectWord() {
160 if len(c.buf.Lines[c.Y]) == 0 {
164 if !IsWordChar(string(c.RuneUnder(c.X))) {
166 c.CurSelection[0] = loc
167 c.CurSelection[1] = loc + 1
168 c.OrigSelection = c.CurSelection
172 forward, backward := c.X, c.X
174 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
178 c.CurSelection[0] = ToCharPos(backward, c.Y, c.buf)
179 c.OrigSelection[0] = c.CurSelection[0]
181 for forward < Count(c.buf.Lines[c.Y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
185 c.CurSelection[1] = ToCharPos(forward, c.Y, c.buf) + 1
186 c.OrigSelection[1] = c.CurSelection[1]
187 c.SetLoc(c.CurSelection[1])
190 // AddWordToSelection adds the word the cursor is currently on to the selection
191 func (c *Cursor) AddWordToSelection() {
194 if loc > c.OrigSelection[0] && loc < c.OrigSelection[1] {
195 c.CurSelection = c.OrigSelection
199 if loc < c.OrigSelection[0] {
202 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
206 c.CurSelection[0] = ToCharPos(backward, c.Y, c.buf)
207 c.CurSelection[1] = c.OrigSelection[1]
210 if loc > c.OrigSelection[1] {
213 for forward < Count(c.buf.Lines[c.Y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
217 c.CurSelection[1] = ToCharPos(forward, c.Y, c.buf) + 1
218 c.CurSelection[0] = c.OrigSelection[0]
221 c.SetLoc(c.CurSelection[1])
224 // SelectTo selects from the current cursor location to the given location
225 func (c *Cursor) SelectTo(loc int) {
226 if loc > c.OrigSelection[0] {
227 c.CurSelection[0] = c.OrigSelection[0]
228 c.CurSelection[1] = loc
230 c.CurSelection[0] = loc
231 c.CurSelection[1] = c.OrigSelection[0]
235 // WordRight moves the cursor one word to the right
236 func (c *Cursor) WordRight() {
238 for IsWhitespace(c.RuneUnder(c.X)) {
239 if c.X == Count(c.buf.Lines[c.Y]) {
244 for !IsWhitespace(c.RuneUnder(c.X)) {
245 if c.X == Count(c.buf.Lines[c.Y]) {
252 // WordLeft moves the cursor one word to the left
253 func (c *Cursor) WordLeft() {
255 for IsWhitespace(c.RuneUnder(c.X)) {
261 for !IsWhitespace(c.RuneUnder(c.X)) {
270 // RuneUnder returns the rune under the given x position
271 func (c *Cursor) RuneUnder(x int) rune {
272 line := []rune(c.buf.Lines[c.Y])
284 // Up moves the cursor up one line (if possible)
285 func (c *Cursor) Up() {
289 runes := []rune(c.buf.Lines[c.Y])
290 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
291 if c.X > len(runes) {
297 // Down moves the cursor down one line (if possible)
298 func (c *Cursor) Down() {
299 if c.Y < c.buf.NumLines-1 {
302 runes := []rune(c.buf.Lines[c.Y])
303 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
304 if c.X > len(runes) {
310 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
311 func (c *Cursor) Left() {
321 c.LastVisualX = c.GetVisualX()
324 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
325 func (c *Cursor) Right() {
326 if c.Loc() == c.buf.Len() {
329 if c.X < Count(c.buf.Lines[c.Y]) {
335 c.LastVisualX = c.GetVisualX()
338 // End moves the cursor to the end of the line it is on
339 func (c *Cursor) End() {
340 c.X = Count(c.buf.Lines[c.Y])
341 c.LastVisualX = c.GetVisualX()
344 // Start moves the cursor to the start of the line it is on
345 func (c *Cursor) Start() {
347 c.LastVisualX = c.GetVisualX()
350 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
351 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
353 tabSize := int(settings["tabsize"].(float64))
354 // This is the visual line -- every \t replaced with the correct number of spaces
355 visualLine := strings.Replace(c.buf.Lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
356 if visualPos > Count(visualLine) {
357 visualPos = Count(visualLine)
359 numTabs := NumOccurences(visualLine[:visualPos], '\t')
360 if visualPos >= (tabSize-1)*numTabs {
361 return visualPos - (tabSize-1)*numTabs
363 return visualPos / tabSize
366 // GetVisualX returns the x value of the cursor in visual spaces
367 func (c *Cursor) GetVisualX() int {
368 runes := []rune(c.buf.Lines[c.Y])
369 tabSize := int(settings["tabsize"].(float64))
370 return c.X + NumOccurences(string(runes[:c.X]), '\t')*(tabSize-1)
373 // Relocate makes sure that the cursor is inside the bounds of the buffer
374 // If it isn't, it moves it to be within the buffer's lines
375 func (c *Cursor) Relocate() {
378 } else if c.Y >= c.buf.NumLines {
379 c.Y = c.buf.NumLines - 1
384 } else if c.X > Count(c.buf.Lines[c.Y]) {
385 c.X = Count(c.buf.Lines[c.Y])