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 if v.mouseReleased && !e.HasMotion() {
282 v.MoveToMouseClick(x, y)
283 if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
286 v.lastClickTime = time.Now()
289 v.doubleClick = false
291 v.cursor.SelectLine()
294 v.lastClickTime = time.Now()
297 v.tripleClick = false
299 v.cursor.SelectWord()
302 v.doubleClick = false
303 v.tripleClick = false
304 v.lastClickTime = time.Now()
306 loc := v.cursor.Loc()
307 v.cursor.origSelection[0] = loc
308 v.cursor.curSelection[0] = loc
309 v.cursor.curSelection[1] = loc
311 v.mouseReleased = false
312 } else if !v.mouseReleased {
313 v.MoveToMouseClick(x, y)
315 v.cursor.AddLineToSelection()
316 } else if v.doubleClick {
317 v.cursor.AddWordToSelection()
319 v.cursor.curSelection[1] = v.cursor.Loc()
322 case tcell.ButtonNone:
323 // Mouse event with no click
324 if !v.mouseReleased {
325 // Mouse was just released
327 // Relocating here isn't really necessary because the cursor will
328 // be in the right place from the last mouse event
329 // However, if we are running in a terminal that doesn't support mouse motion
330 // events, this still allows the user to make selections, except only after they
333 if !v.doubleClick && !v.tripleClick {
334 v.MoveToMouseClick(x, y)
335 v.cursor.curSelection[1] = v.cursor.Loc()
337 v.mouseReleased = true
339 // We don't want to relocate because otherwise the view will be relocated
340 // every time the user moves the cursor
343 // Scroll up two lines
345 // We don't want to relocate if the user is scrolling
347 case tcell.WheelDown:
348 // Scroll down two lines
350 // We don't want to relocate if the user is scrolling
358 if settings["syntax"].(bool) {
363 // GutterMessage creates a message in this view's gutter
364 func (v *View) GutterMessage(lineN int, msg string, kind int) {
365 gutterMsg := GutterMessage{
370 for _, gmsg := range v.messages {
371 if gmsg.lineNum == lineN {
375 v.messages = append(v.messages, gutterMsg)
378 // DisplayView renders the view to the screen
379 func (v *View) DisplayView() {
380 // The character number of the character in the top left of the screen
381 charNum := ToCharPos(0, v.topline, v.buf)
383 // Convert the length of buffer to a string, and get the length of the string
384 // We are going to have to offset by that amount
385 maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
386 // + 1 for the little space after the line number
387 if settings["ruler"] == true {
388 v.lineNumOffset = maxLineLength + 1
392 var highlightStyle tcell.Style
394 if len(v.messages) > 0 {
398 for lineN := 0; lineN < v.height; lineN++ {
400 // If the buffer is smaller than the view height
401 // and we went too far, break
402 if lineN+v.topline >= len(v.buf.lines) {
405 line := v.buf.lines[lineN+v.topline]
407 if len(v.messages) > 0 {
409 for _, msg := range v.messages {
410 if msg.lineNum == lineN+v.topline {
412 gutterStyle := tcell.StyleDefault
415 if style, ok := colorscheme["gutter-info"]; ok {
419 if style, ok := colorscheme["gutter-warning"]; ok {
423 if style, ok := colorscheme["gutter-error"]; ok {
427 screen.SetContent(x, lineN, '>', nil, gutterStyle)
429 screen.SetContent(x, lineN, '>', nil, gutterStyle)
431 if v.cursor.y == lineN {
432 messenger.Message(msg.msg)
433 messenger.gutterMessage = true
438 screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
440 screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
442 if v.cursor.y == lineN && messenger.gutterMessage {
444 messenger.gutterMessage = false
449 // Write the line number
450 lineNumStyle := defStyle
451 if style, ok := colorscheme["line-number"]; ok {
454 // Write the spaces before the line number if necessary
456 if settings["ruler"] == true {
457 lineNum = strconv.Itoa(lineN + v.topline + 1)
458 for i := 0; i < maxLineLength-len(lineNum); i++ {
459 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
462 // Write the actual line number
463 for _, ch := range lineNum {
464 screen.SetContent(x, lineN, ch, nil, lineNumStyle)
468 if settings["ruler"] == true {
469 // Write the extra space
470 screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
476 for colN, ch := range line {
477 var lineStyle tcell.Style
479 if settings["syntax"].(bool) {
480 // Syntax highlighting is enabled
481 highlightStyle = v.matches[lineN][colN]
484 if v.cursor.HasSelection() &&
485 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
486 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
488 lineStyle = tcell.StyleDefault.Reverse(true)
490 if style, ok := colorscheme["selection"]; ok {
494 lineStyle = highlightStyle
498 screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
499 tabSize := int(settings["tabsize"].(float64))
500 for i := 0; i < tabSize-1; i++ {
502 if x-v.leftCol+tabchars >= v.lineNumOffset {
503 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, lineStyle)
507 if x-v.leftCol+tabchars >= v.lineNumOffset {
508 screen.SetContent(x-v.leftCol+tabchars, lineN, ch, nil, lineStyle)
514 // Here we are at a newline
516 // The newline may be selected, in which case we should draw the selection style
517 // with a space to represent it
518 if v.cursor.HasSelection() &&
519 (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
520 charNum < v.cursor.curSelection[0] && charNum >= v.cursor.curSelection[1]) {
522 selectStyle := defStyle.Reverse(true)
524 if style, ok := colorscheme["selection"]; ok {
527 screen.SetContent(x-v.leftCol+tabchars, lineN, ' ', nil, selectStyle)
534 // Display renders the view, the cursor, and statusline
535 func (v *View) Display() {