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 lineLen = Count(buf.lines[y]) + 1
28 // ToCharPos converts from an x, y position to a character position
29 func ToCharPos(x, y int, buf *Buffer) int {
31 for i := 0; i < y; i++ {
32 // + 1 for the newline
33 loc += Count(buf.lines[i]) + 1
39 // The Cursor struct stores the location of the cursor in the view
40 // The complicated part about the cursor is storing its location.
41 // The cursor must be displayed at an x, y location, but since the buffer
42 // uses a rope to store text, to insert text we must have an index. It
43 // is also simpler to use character indicies for other tasks such as
48 // The cursor display location
52 // Last cursor x position
55 // The current selection as a range of character numbers (inclusive)
57 // The original selection as a range of character numbers
58 // This is used for line and word selection where it is necessary
59 // to know what the original selection was
63 // SetLoc sets the location of the cursor in terms of character number
64 // and not x, y location
65 // It's just a simple wrapper of FromCharPos
66 func (c *Cursor) SetLoc(loc int) {
67 c.x, c.y = FromCharPos(loc, c.v.buf)
68 c.lastVisualX = c.GetVisualX()
71 // Loc gets the cursor location in terms of character number instead
73 // It's just a simple wrapper of ToCharPos
74 func (c *Cursor) Loc() int {
75 return ToCharPos(c.x, c.y, c.v.buf)
78 // ResetSelection resets the user's selection
79 func (c *Cursor) ResetSelection() {
84 // HasSelection returns whether or not the user has selected anything
85 func (c *Cursor) HasSelection() bool {
86 return c.curSelection[0] != c.curSelection[1]
89 // DeleteSelection deletes the currently selected text
90 func (c *Cursor) DeleteSelection() {
91 if c.curSelection[0] > c.curSelection[1] {
92 c.v.eh.Remove(c.curSelection[1], c.curSelection[0])
93 c.SetLoc(c.curSelection[1])
95 c.v.eh.Remove(c.curSelection[0], c.curSelection[1])
96 c.SetLoc(c.curSelection[0])
100 // GetSelection returns the cursor's selection
101 func (c *Cursor) GetSelection() string {
102 if c.curSelection[0] > c.curSelection[1] {
103 return string([]rune(c.v.buf.text)[c.curSelection[1]:c.curSelection[0]])
105 return string([]rune(c.v.buf.text)[c.curSelection[0]:c.curSelection[1]])
108 // SelectLine selects the current line
109 func (c *Cursor) SelectLine() {
111 c.curSelection[0] = c.Loc()
113 c.curSelection[1] = c.Loc() + 1
115 c.origSelection = c.curSelection
118 // AddLineToSelection adds the current line to the selection
119 func (c *Cursor) AddLineToSelection() {
122 if loc < c.origSelection[0] {
124 c.curSelection[0] = c.Loc()
125 c.curSelection[1] = c.origSelection[1]
127 if loc > c.origSelection[1] {
129 c.curSelection[1] = c.Loc()
130 c.curSelection[0] = c.origSelection[0]
133 if loc < c.origSelection[1] && loc > c.origSelection[0] {
134 c.curSelection = c.origSelection
138 // SelectWord selects the word the cursor is currently on
139 func (c *Cursor) SelectWord() {
140 if len(c.v.buf.lines[c.y]) == 0 {
144 if !IsWordChar(string(c.RuneUnder(c.x))) {
146 c.curSelection[0] = loc
147 c.curSelection[1] = loc + 1
148 c.origSelection = c.curSelection
152 forward, backward := c.x, c.x
154 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
158 c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
159 c.origSelection[0] = c.curSelection[0]
161 for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
165 c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
166 c.origSelection[1] = c.curSelection[1]
169 // AddWordToSelection adds the word the cursor is currently on to the selection
170 func (c *Cursor) AddWordToSelection() {
173 if loc > c.origSelection[0] && loc < c.origSelection[1] {
174 c.curSelection = c.origSelection
178 if loc < c.origSelection[0] {
181 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
185 c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
186 c.curSelection[1] = c.origSelection[1]
189 if loc > c.origSelection[1] {
192 for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
196 c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
197 c.curSelection[0] = c.origSelection[0]
201 // RuneUnder returns the rune under the given x position
202 func (c *Cursor) RuneUnder(x int) rune {
203 line := []rune(c.v.buf.lines[c.y])
212 // Up moves the cursor up one line (if possible)
213 func (c *Cursor) Up() {
217 runes := []rune(c.v.buf.lines[c.y])
218 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
219 if c.x > len(runes) {
225 // Down moves the cursor down one line (if possible)
226 func (c *Cursor) Down() {
227 if c.y < len(c.v.buf.lines)-1 {
230 runes := []rune(c.v.buf.lines[c.y])
231 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
232 if c.x > len(runes) {
238 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
239 func (c *Cursor) Left() {
249 c.lastVisualX = c.GetVisualX()
252 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
253 func (c *Cursor) Right() {
254 if c.Loc() == c.v.buf.Len() {
257 if c.x < Count(c.v.buf.lines[c.y]) {
263 c.lastVisualX = c.GetVisualX()
266 // End moves the cursor to the end of the line it is on
267 func (c *Cursor) End() {
268 c.x = Count(c.v.buf.lines[c.y])
269 c.lastVisualX = c.GetVisualX()
272 // Start moves the cursor to the start of the line it is on
273 func (c *Cursor) Start() {
275 c.lastVisualX = c.GetVisualX()
278 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
279 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
281 tabSize := settings.TabSize
282 // This is the visual line -- every \t replaced with the correct number of spaces
283 visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
284 if visualPos > Count(visualLine) {
285 visualPos = Count(visualLine)
287 numTabs := NumOccurences(visualLine[:visualPos], '\t')
288 if visualPos >= (tabSize-1)*numTabs {
289 return visualPos - (tabSize-1)*numTabs
291 return visualPos / tabSize
294 // GetVisualX returns the x value of the cursor in visual spaces
295 func (c *Cursor) GetVisualX() int {
296 runes := []rune(c.v.buf.lines[c.y])
297 tabSize := settings.TabSize
298 return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
301 // Display draws the cursor to the screen at the correct position
302 func (c *Cursor) Display() {
303 // Don't draw the cursor if it is out of the viewport or if it has a selection
304 if (c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1) || c.HasSelection() {
307 screen.ShowCursor(c.GetVisualX()+c.v.lineNumOffset-c.v.leftCol, c.y-c.v.topline)