9 "github.com/zyedidia/tcell"
12 // The View struct stores information about a view into a buffer.
13 // It has a stores information about the cursor, and the viewport
14 // that the user sees the buffer from.
18 // The topmost line, used for vertical scrolling
20 // The leftmost column, used for horizontal scrolling
23 // Percentage of the terminal window that this view takes up (from 0 to 100)
27 // Actual with and height
31 // How much to offset because of line numbers
34 // The eventhandler for undo/redo
37 messages []GutterMessage
44 // Since tcell doesn't differentiate between a mouse release event
45 // and a mouse move event with no keys pressed, we need to keep
46 // track of whether or not the mouse was pressed (or not released) last event to determine
47 // mouse release events
50 // This stores when the last click was
51 // This is useful for detecting double and triple clicks
52 lastClickTime time.Time
54 // lastCutTime stores when the last ctrl+k was issued.
55 // It is used for clearing the clipboard to replace it with fresh cut lines.
58 // freshClip returns true if the clipboard has never been pasted.
61 // Was the last mouse event actually a double click?
62 // Useful for detecting triple clicks -- if a double click is detected
63 // but the last mouse event was actually a double click, it's a triple click
65 // Same here, just to keep track for mouse move events
68 // Syntax highlighting matches
70 // The matches from the last frame
71 lastMatches SyntaxMatches
74 // NewView returns a new fullscreen view
75 func NewView(buf *Buffer) *View {
76 return NewViewWidthHeight(buf, 100, 100)
79 // NewViewWidthHeight returns a new view with the specified width and height percentages
80 // Note that w and h are percentages not actual values
81 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
86 v.Resize(screen.Size())
90 v.eh = NewEventHandler(v)
99 // Resize recalculates the actual width and height of the view from the width and height
101 // This is usually called when the window is resized, or when a split has been added and
102 // the percentages have changed
103 func (v *View) Resize(w, h int) {
104 // Always include 1 line for the command line at the bottom
106 v.width = int(float32(w) * float32(v.widthPercent) / 100)
107 // We subtract 1 for the statusline
108 v.height = int(float32(h)*float32(v.heightPercent)/100) - 1
111 // ScrollUp scrolls the view up n lines (if possible)
112 func (v *View) ScrollUp(n int) {
113 // Try to scroll by n but if it would overflow, scroll by 1
114 if v.topline-n >= 0 {
116 } else if v.topline > 0 {
121 // ScrollDown scrolls the view down n lines (if possible)
122 func (v *View) ScrollDown(n int) {
123 // Try to scroll by n but if it would overflow, scroll by 1
124 if v.topline+n <= len(v.buf.lines)-v.height {
126 } else if v.topline < len(v.buf.lines)-v.height {
131 // CanClose returns whether or not the view can be closed
132 // If there are unsaved changes, the user will be asked if the view can be closed
133 // causing them to lose the unsaved changes
134 // The message is what to print after saying "You have unsaved changes. "
135 func (v *View) CanClose(msg string) bool {
137 quit, canceled := messenger.Prompt("You have unsaved changes. " + msg)
139 if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
141 } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
152 // OpenBuffer opens a new buffer in this view.
153 // This resets the topline, event handler and cursor.
154 func (v *View) OpenBuffer(buf *Buffer) {
158 // Put the cursor at the first spot
164 v.cursor.ResetSelection()
166 v.eh = NewEventHandler(v)
169 // Set mouseReleased to true because we assume the mouse is not being pressed when
170 // the editor is opened
171 v.mouseReleased = true
172 v.lastClickTime = time.Time{}
175 // Close and Re-open the current file.
176 func (v *View) reOpen() {
177 if v.CanClose("Continue? (yes, no, save) ") {
178 file, err := ioutil.ReadFile(v.buf.path)
179 filename := v.buf.name
182 messenger.Error(err.Error())
185 buf := NewBuffer(string(file), filename)
193 // Relocate moves the view window so that the cursor is in view
194 // This is useful if the user has scrolled far away, and then starts typing
195 func (v *View) Relocate() bool {
202 if cy > v.topline+v.height-1 {
203 v.topline = cy - v.height + 1
207 cx := v.cursor.GetVisualX()
212 if cx+v.lineNumOffset+1 > v.leftCol+v.width {
213 v.leftCol = cx - v.width + v.lineNumOffset + 1
219 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
221 func (v *View) MoveToMouseClick(x, y int) {
222 if y-v.topline > v.height-1 {
224 y = v.height + v.topline - 1
226 if y >= len(v.buf.lines) {
227 y = len(v.buf.lines) - 1
236 x = v.cursor.GetCharPosInLine(y, x)
237 if x > Count(v.buf.lines[y]) {
238 x = Count(v.buf.lines[y])
242 v.cursor.lastVisualX = v.cursor.GetVisualX()
245 // HandleEvent handles an event passed by the main loop
246 func (v *View) HandleEvent(event tcell.Event) {
247 // This bool determines whether the view is relocated at the end of the function
248 // By default it's true because most events should cause a relocate
251 switch e := event.(type) {
252 case *tcell.EventResize:
255 case *tcell.EventKey:
256 if e.Key() == tcell.KeyRune {
257 // Insert a character
258 if v.cursor.HasSelection() {
259 v.cursor.DeleteSelection()
260 v.cursor.ResetSelection()
262 v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
265 for key, action := range bindings {
271 case *tcell.EventMouse:
273 x -= v.lineNumOffset - v.leftCol
276 button := e.Buttons()
281 origX, origY := v.cursor.x, v.cursor.y
283 if v.mouseReleased && !e.HasMotion() {
284 v.MoveToMouseClick(x, y)
285 if (time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold) &&
286 (origX == v.cursor.x && origY == v.cursor.y) {
289 v.lastClickTime = time.Now()
292 v.doubleClick = false
294 v.cursor.SelectLine()
297 v.lastClickTime = time.Now()
300 v.tripleClick = false
302 v.cursor.SelectWord()
305 v.doubleClick = false
306 v.tripleClick = false
307 v.lastClickTime = time.Now()
309 loc := v.cursor.Loc()
310 v.cursor.curSelection[0] = loc
311 v.cursor.curSelection[1] = loc
313 v.mouseReleased = false
314 } else if !v.mouseReleased {
315 v.MoveToMouseClick(x, y)
317 v.cursor.AddLineToSelection()
318 } else if v.doubleClick {
319 v.cursor.AddWordToSelection()
321 v.cursor.curSelection[1] = v.cursor.Loc()
324 case tcell.ButtonNone:
325 // Mouse event with no click
326 if !v.mouseReleased {
327 // Mouse was just released
329 // Relocating here isn't really necessary because the cursor will
330 // be in the right place from the last mouse event
331 // However, if we are running in a terminal that doesn't support mouse motion
332 // events, this still allows the user to make selections, except only after they
335 if !v.doubleClick && !v.tripleClick {
336 v.MoveToMouseClick(x, y)
337 v.cursor.curSelection[1] = v.cursor.Loc()
339 v.mouseReleased = true
341 // We don't want to relocate because otherwise the view will be relocated
342 // every time the user moves the cursor
345 // Scroll up two lines
347 // We don't want to relocate if the user is scrolling
349 case tcell.WheelDown:
350 // Scroll down two lines
352 // We don't want to relocate if the user is scrolling
365 // GutterMessage creates a message in this view's gutter
366 func (v *View) GutterMessage(lineN int, msg string, kind int) {
367 gutterMsg := GutterMessage{
372 for _, gmsg := range v.messages {
373 if gmsg.lineNum == lineN {
377 v.messages = append(v.messages, gutterMsg)
380 // DisplayView renders the view to the screen
381 func (v *View) DisplayView() {
382 // The character number of the character in the top left of the screen
383 charNum := ToCharPos(0, v.topline, v.buf)
385 // Convert the length of buffer to a string, and get the length of the string
386 // We are going to have to offset by that amount
387 maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
388 // + 1 for the little space after the line number
389 if settings.Ruler == true {
390 v.lineNumOffset = maxLineLength + 1
394 var highlightStyle tcell.Style
396 if len(v.messages) > 0 {
400 for lineN := 0; lineN < v.height; lineN++ {
402 // If the buffer is smaller than the view height
403 // and we went too far, break
404 if lineN+v.topline >= len(v.buf.lines) {
407 line := v.buf.lines[lineN+v.topline]
409 if len(v.messages) > 0 {
411 for _, msg := range v.messages {
412 if msg.lineNum == lineN+v.topline {
414 gutterStyle := tcell.StyleDefault
417 if style, ok := colorscheme["gutter-info"]; ok {
421 if style, ok := colorscheme["gutter-warning"]; ok {
425 if style, ok := colorscheme["gutter-error"]; ok {
429 screen.SetContent(x, lineN, '>', nil, gutterStyle)
431 screen.SetContent(x, lineN, '>', nil, gutterStyle)
433 if v.cursor.y == lineN {
434 messenger.Message(msg.msg)
435 messenger.gutterMessage = true
440 screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
442 screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
444 if v.cursor.y == lineN && messenger.gutterMessage {
446 messenger.gutterMessage = false
451 // Write the line number
452 lineNumStyle := defStyle
453 if style, ok := colorscheme["line-number"]; ok {
456 // Write the spaces before the line number if necessary
458 if settings.Ruler == true {
459 lineNum = strconv.Itoa(lineN + v.topline + 1)
460 for i := 0; i < maxLineLength-len(lineNum); i++ {
461 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
464 // Write the actual line number
465 for _, ch := range lineNum {
466 screen.SetContent(x, lineN, ch, nil, lineNumStyle)
470 if settings.Ruler == true {
471 // Write the extra space
472 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
478 for colN, ch := range line {
479 var lineStyle tcell.Style
482 // Syntax highlighting is enabled
483 highlightStyle = v.matches[lineN][colN]
486 if v.cursor.HasSelection() &&
487 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
488 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
490 lineStyle = tcell.StyleDefault.Reverse(true)
492 if style, ok := colorscheme["selection"]; ok {
496 lineStyle = highlightStyle
500 screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
501 tabSize := settings.TabSize
502 for i := 0; i < tabSize-1; i++ {
504 if x-v.leftCol+tabchars >= v.lineNumOffset {
505 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, lineStyle)
509 if x-v.leftCol+tabchars >= v.lineNumOffset {
510 screen.SetContent(x-v.leftCol+tabchars, lineN, ch, nil, lineStyle)
516 // Here we are at a newline
518 // The newline may be selected, in which case we should draw the selection style
519 // with a space to represent it
520 if v.cursor.HasSelection() &&
521 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
522 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
524 selectStyle := defStyle.Reverse(true)
526 if style, ok := colorscheme["selection"]; ok {
529 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, selectStyle)
536 // Display renders the view, the cursor, and statusline
537 func (v *View) Display() {