9 "github.com/mitchellh/go-homedir"
10 "github.com/zyedidia/tcell"
13 type ViewType struct {
15 readonly bool // The file cannot be edited
16 scratch bool // The file cannot be saved
20 vtDefault = ViewType{0, false, false}
21 vtHelp = ViewType{1, true, true}
22 vtLog = ViewType{2, true, true}
23 vtScratch = ViewType{3, false, true}
26 // The View struct stores information about a view into a buffer.
27 // It stores information about the cursor, and the viewport
28 // that the user sees the buffer from.
30 // A pointer to the buffer's cursor for ease of access
33 // The topmost line, used for vertical scrolling
35 // The leftmost column, used for horizontal scrolling
38 // Specifies whether or not this view holds a help buffer
41 // Actual width and height
48 // Where this view is located
51 // How much to offset because of line numbers
54 // Holds the list of gutter messages
55 messages map[string][]GutterMessage
57 // This is the index of this view in the views array
59 // What tab is this view stored in
67 // Since tcell doesn't differentiate between a mouse release event
68 // and a mouse move event with no keys pressed, we need to keep
69 // track of whether or not the mouse was pressed (or not released) last event to determine
70 // mouse release events
73 // This stores when the last click was
74 // This is useful for detecting double and triple clicks
75 lastClickTime time.Time
78 // lastCutTime stores when the last ctrl+k was issued.
79 // It is used for clearing the clipboard to replace it with fresh cut lines.
82 // freshClip returns true if the clipboard has never been pasted.
85 // Was the last mouse event actually a double click?
86 // Useful for detecting triple clicks -- if a double click is detected
87 // but the last mouse event was actually a double click, it's a triple click
89 // Same here, just to keep track for mouse move events
97 // NewView returns a new fullscreen view
98 func NewView(buf *Buffer) *View {
99 screenW, screenH := screen.Size()
100 return NewViewWidthHeight(buf, screenW, screenH)
103 // NewViewWidthHeight returns a new view with the specified width and height
104 // Note that w and h are raw column and row values
105 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
112 v.cellview = new(CellView)
118 v.messages = make(map[string][]GutterMessage)
120 v.sline = Statusline{
124 if v.Buf.Settings["statusline"].(bool) {
128 for pl := range loadedPlugins {
129 _, err := Call(pl+".onViewOpen", v)
130 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
139 // ToggleStatusLine creates an extra row for the statusline if necessary
140 func (v *View) ToggleStatusLine() {
141 if v.Buf.Settings["statusline"].(bool) {
148 // ToggleTabbar creates an extra row for the tabbar if necessary
149 func (v *View) ToggleTabbar() {
152 // Include one line for the tab bar at the top
164 func (v *View) paste(clip string) {
165 leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
167 if v.Cursor.HasSelection() {
168 v.Cursor.DeleteSelection()
169 v.Cursor.ResetSelection()
171 clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
172 v.Buf.Insert(v.Cursor.Loc, clip)
173 // v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
175 messenger.Message("Pasted clipboard")
178 // ScrollUp scrolls the view up n lines (if possible)
179 func (v *View) ScrollUp(n int) {
180 // Try to scroll by n but if it would overflow, scroll by 1
181 if v.Topline-n >= 0 {
183 } else if v.Topline > 0 {
188 // ScrollDown scrolls the view down n lines (if possible)
189 func (v *View) ScrollDown(n int) {
190 // Try to scroll by n but if it would overflow, scroll by 1
191 if v.Topline+n <= v.Buf.NumLines {
193 } else if v.Topline < v.Buf.NumLines-1 {
198 // CanClose returns whether or not the view can be closed
199 // If there are unsaved changes, the user will be asked if the view can be closed
200 // causing them to lose the unsaved changes
201 func (v *View) CanClose() bool {
202 if v.Type == vtDefault && v.Buf.IsModified {
205 if v.Buf.Settings["autosave"].(bool) {
208 choice, canceled = messenger.YesNoPrompt("Save changes to " + v.Buf.GetName() + " before closing? (y,n,esc) ")
225 // OpenBuffer opens a new buffer in this view.
226 // This resets the topline, event handler and cursor.
227 func (v *View) OpenBuffer(buf *Buffer) {
231 v.Cursor = &buf.Cursor
234 v.Cursor.ResetSelection()
237 v.messages = make(map[string][]GutterMessage)
239 // Set mouseReleased to true because we assume the mouse is not being pressed when
240 // the editor is opened
241 v.mouseReleased = true
242 v.lastClickTime = time.Time{}
245 // Open opens the given file in the view
246 func (v *View) Open(filename string) {
247 home, _ := homedir.Dir()
248 filename = strings.Replace(filename, "~", home, 1)
249 file, err := os.Open(filename)
250 fileInfo, _ := os.Stat(filename)
252 if err == nil && fileInfo.IsDir() {
253 messenger.Error(filename, " is a directory")
261 messenger.Message(err.Error())
262 // File does not exist -- create an empty buffer with that name
263 buf = NewBufferFromString("", filename)
265 buf = NewBuffer(file, FSize(file), filename)
270 // CloseBuffer performs any closing functions on the buffer
271 func (v *View) CloseBuffer() {
277 // ReOpen reloads the current buffer
278 func (v *View) ReOpen() {
286 // HSplit opens a horizontal split with the given buffer
287 func (v *View) HSplit(buf *Buffer) {
289 if v.Buf.Settings["splitBottom"].(bool) {
292 v.splitNode.HSplit(buf, v.Num+i)
295 // VSplit opens a vertical split with the given buffer
296 func (v *View) VSplit(buf *Buffer) {
298 if v.Buf.Settings["splitRight"].(bool) {
301 v.splitNode.VSplit(buf, v.Num+i)
304 // HSplitIndex opens a horizontal split with the given buffer at the given index
305 func (v *View) HSplitIndex(buf *Buffer, splitIndex int) {
306 v.splitNode.HSplit(buf, splitIndex)
309 // VSplitIndex opens a vertical split with the given buffer at the given index
310 func (v *View) VSplitIndex(buf *Buffer, splitIndex int) {
311 v.splitNode.VSplit(buf, splitIndex)
314 // GetSoftWrapLocation gets the location of a visual click on the screen and converts it to col,line
315 func (v *View) GetSoftWrapLocation(vx, vy int) (int, int) {
316 if !v.Buf.Settings["softwrap"].(bool) {
317 if vy >= v.Buf.NumLines {
318 vy = v.Buf.NumLines - 1
320 vx = v.Cursor.GetCharPosInLine(vy, vx)
324 screenX, screenY := 0, v.Topline
325 for lineN := v.Topline; lineN < v.Bottomline(); lineN++ {
326 line := v.Buf.Line(lineN)
327 if lineN >= v.Buf.NumLines {
328 return 0, v.Buf.NumLines - 1
332 for _, ch := range line {
333 if screenX >= v.Width-v.lineNumOffset {
338 if screenX == vx && screenY == vy {
343 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
359 func (v *View) Bottomline() int {
360 if !v.Buf.Settings["softwrap"].(bool) {
361 return v.Topline + v.Height
364 screenX, screenY := 0, 0
366 for lineN := v.Topline; lineN < v.Topline+v.Height; lineN++ {
367 line := v.Buf.Line(lineN)
370 for _, ch := range line {
371 if screenX >= v.Width-v.lineNumOffset {
377 screenX += int(v.Buf.Settings["tabsize"].(float64)) - 1
387 if screenY >= v.Height {
391 return numLines + v.Topline
394 // Relocate moves the view window so that the cursor is in view
395 // This is useful if the user has scrolled far away, and then starts typing
396 func (v *View) Relocate() bool {
397 height := v.Bottomline() - v.Topline
400 scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
401 if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
402 v.Topline = cy - scrollmargin
404 } else if cy < v.Topline {
408 if cy > v.Topline+height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
409 v.Topline = cy - height + 1 + scrollmargin
411 } else if cy >= v.Buf.NumLines-scrollmargin && cy > height {
412 v.Topline = v.Buf.NumLines - height
416 if !v.Buf.Settings["softwrap"].(bool) {
417 cx := v.Cursor.GetVisualX()
422 if cx+v.lineNumOffset+1 > v.leftCol+v.Width {
423 v.leftCol = cx - v.Width + v.lineNumOffset + 1
430 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
432 func (v *View) MoveToMouseClick(x, y int) {
433 if y-v.Topline > v.Height-1 {
435 y = v.Height + v.Topline - 1
444 x, y = v.GetSoftWrapLocation(x, y)
445 // x = v.Cursor.GetCharPosInLine(y, x)
446 if x > Count(v.Buf.Line(y)) {
447 x = Count(v.Buf.Line(y))
451 v.Cursor.LastVisualX = v.Cursor.GetVisualX()
454 func (v *View) ExecuteActions(actions []func(*View, bool) bool) bool {
456 readonlyBindingsList := []string{"Delete", "Insert", "Backspace", "Cut", "Play", "Paste", "Move", "Add", "DuplicateLine", "Macro"}
457 for _, action := range actions {
458 readonlyBindingsResult := false
459 funcName := ShortFuncName(action)
460 if v.Type.readonly == true {
461 // check for readonly and if true only let key bindings get called if they do not change the contents.
462 for _, readonlyBindings := range readonlyBindingsList {
463 if strings.Contains(funcName, readonlyBindings) {
464 readonlyBindingsResult = true
468 if !readonlyBindingsResult {
469 // call the key binding
470 relocate = action(v, true) || relocate
472 if funcName != "ToggleMacro" && funcName != "PlayMacro" {
474 curMacro = append(curMacro, action)
483 func (v *View) SetCursor(c *Cursor) bool {
488 v.Buf.curCursor = c.Num
493 // HandleEvent handles an event passed by the main loop
494 func (v *View) HandleEvent(event tcell.Event) {
495 // This bool determines whether the view is relocated at the end of the function
496 // By default it's true because most events should cause a relocate
501 switch e := event.(type) {
502 case *tcell.EventKey:
503 // Check first if input is a key binding, if it is we 'eat' the input and don't insert a rune
505 for key, actions := range bindings {
506 if e.Key() == key.keyCode {
507 if e.Key() == tcell.KeyRune {
508 if e.Rune() != key.r {
512 if e.Modifiers() == key.modifiers {
513 for _, c := range v.Buf.cursors {
520 relocate = v.ExecuteActions(actions) || relocate
522 v.SetCursor(&v.Buf.Cursor)
528 if !isBinding && e.Key() == tcell.KeyRune {
529 // Check viewtype if readonly don't insert a rune (readonly help and log view etc.)
530 if v.Type.readonly == false {
531 for _, c := range v.Buf.cursors {
534 // Insert a character
535 if v.Cursor.HasSelection() {
536 v.Cursor.DeleteSelection()
537 v.Cursor.ResetSelection()
539 v.Buf.Insert(v.Cursor.Loc, string(e.Rune()))
541 for pl := range loadedPlugins {
542 _, err := Call(pl+".onRune", string(e.Rune()), v)
543 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
549 curMacro = append(curMacro, e.Rune())
552 v.SetCursor(&v.Buf.Cursor)
555 case *tcell.EventPaste:
556 // Check viewtype if readonly don't paste (readonly help and log view etc.)
557 if v.Type.readonly == false {
558 if !PreActionCall("Paste", v) {
562 for _, c := range v.Buf.cursors {
566 v.SetCursor(&v.Buf.Cursor)
568 PostActionCall("Paste", v)
570 case *tcell.EventMouse:
571 // Don't relocate for mouse events
574 button := e.Buttons()
576 for key, actions := range bindings {
577 if button == key.buttons && e.Modifiers() == key.modifiers {
578 for _, c := range v.Buf.cursors {
583 relocate = v.ExecuteActions(actions) || relocate
585 v.SetCursor(&v.Buf.Cursor)
590 for key, actions := range mouseBindings {
591 if button == key.buttons && e.Modifiers() == key.modifiers {
592 for _, action := range actions {
599 case tcell.ButtonNone:
600 // Mouse event with no click
601 if !v.mouseReleased {
602 // Mouse was just released
605 x -= v.lineNumOffset - v.leftCol + v.x
608 // Relocating here isn't really necessary because the cursor will
609 // be in the right place from the last mouse event
610 // However, if we are running in a terminal that doesn't support mouse motion
611 // events, this still allows the user to make selections, except only after they
614 if !v.doubleClick && !v.tripleClick {
615 v.MoveToMouseClick(x, y)
616 v.Cursor.SetSelectionEnd(v.Cursor.Loc)
617 v.Cursor.CopySelection("primary")
619 v.mouseReleased = true
626 // We run relocate again because there's a bug with relocating with softwrap
627 // when for example you jump to the bottom of the buffer and it tries to
628 // calculate where to put the topline so that the bottom line is at the bottom
629 // of the terminal and it runs into problems with visual lines vs real lines.
630 // This is (hopefully) a temporary solution
635 func (v *View) mainCursor() bool {
636 return v.Buf.curCursor == len(v.Buf.cursors)-1
639 // GutterMessage creates a message in this view's gutter
640 func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
642 gutterMsg := GutterMessage{
647 for _, v := range v.messages {
648 for _, gmsg := range v {
649 if gmsg.lineNum == lineN {
654 messages := v.messages[section]
655 v.messages[section] = append(messages, gutterMsg)
658 // ClearGutterMessages clears all gutter messages from a given section
659 func (v *View) ClearGutterMessages(section string) {
660 v.messages[section] = []GutterMessage{}
663 // ClearAllGutterMessages clears all the gutter messages
664 func (v *View) ClearAllGutterMessages() {
665 for k := range v.messages {
666 v.messages[k] = []GutterMessage{}
670 // Opens the given help page in a new horizontal split
671 func (v *View) openHelp(helpPage string) {
672 if data, err := FindRuntimeFile(RTHelp, helpPage).Data(); err != nil {
673 TermMessage("Unable to load help text", helpPage, "\n", err)
675 helpBuffer := NewBufferFromString(string(data), helpPage+".md")
676 helpBuffer.name = "Help"
678 if v.Type == vtHelp {
679 v.OpenBuffer(helpBuffer)
682 CurView().Type = vtHelp
687 func (v *View) DisplayView() {
688 if v.Buf.Settings["softwrap"].(bool) && v.leftCol != 0 {
693 // Log views should always follow the cursor...
697 // We need to know the string length of the largest line number
698 // so we can pad appropriately when displaying line numbers
699 maxLineNumLength := len(strconv.Itoa(v.Buf.NumLines))
701 if v.Buf.Settings["ruler"] == true {
702 // + 1 for the little space after the line number
703 v.lineNumOffset = maxLineNumLength + 1
708 // We need to add to the line offset if there are gutter messages
709 var hasGutterMessages bool
710 for _, v := range v.messages {
712 hasGutterMessages = true
715 if hasGutterMessages {
721 // One space for the extra split divider
726 xOffset := v.x + v.lineNumOffset
734 v.cellview.Draw(v.Buf, top, height, left, width-v.lineNumOffset)
740 for visualLineN, line = range v.cellview.lines {
747 if firstChar != nil {
748 if firstChar.realLoc.Y == realLineN {
751 realLineN = firstChar.realLoc.Y
756 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
757 if colorcolumn != 0 {
758 style := GetColor("color-column")
759 fg, _, _ := style.Decompose()
760 st := defStyle.Background(fg)
761 screen.SetContent(xOffset+colorcolumn, yOffset+visualLineN, ' ', nil, st)
766 // If there are gutter messages we need to display the '>>' symbol here
767 if hasGutterMessages {
768 // msgOnLine stores whether or not there is a gutter message on this line in particular
770 for k := range v.messages {
771 for _, msg := range v.messages[k] {
772 if msg.lineNum == realLineN {
774 gutterStyle := defStyle
777 if style, ok := colorscheme["gutter-info"]; ok {
781 if style, ok := colorscheme["gutter-warning"]; ok {
785 if style, ok := colorscheme["gutter-error"]; ok {
789 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
791 screen.SetContent(screenX, yOffset+visualLineN, '>', nil, gutterStyle)
793 if v.Cursor.Y == realLineN && !messenger.hasPrompt {
794 messenger.Message(msg.msg)
795 messenger.gutterMessage = true
800 // If there is no message on this line we just display an empty offset
802 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
804 screen.SetContent(screenX, yOffset+visualLineN, ' ', nil, defStyle)
806 if v.Cursor.Y == realLineN && messenger.gutterMessage {
808 messenger.gutterMessage = false
813 lineNumStyle := defStyle
814 if v.Buf.Settings["ruler"] == true {
815 // Write the line number
816 if style, ok := colorscheme["line-number"]; ok {
819 if style, ok := colorscheme["current-line-number"]; ok {
820 if realLineN == v.Cursor.Y && tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() {
825 lineNum := strconv.Itoa(realLineN + 1)
827 // Write the spaces before the line number if necessary
828 for i := 0; i < maxLineNumLength-len(lineNum); i++ {
829 screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
832 if softwrapped && visualLineN != 0 {
833 // Pad without the line number because it was written on the visual line before
835 screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
839 // Write the actual line number
840 for _, ch := range lineNum {
841 screen.SetContent(screenX+divider, yOffset+visualLineN, ch, nil, lineNumStyle)
846 // Write the extra space
847 screen.SetContent(screenX+divider, yOffset+visualLineN, ' ', nil, lineNumStyle)
853 for _, char := range line {
855 lineStyle := char.style
857 colorcolumn := int(v.Buf.Settings["colorcolumn"].(float64))
858 if colorcolumn != 0 && char.visualLoc.X == colorcolumn {
859 style := GetColor("color-column")
860 fg, _, _ := style.Decompose()
861 lineStyle = lineStyle.Background(fg)
864 charLoc := char.realLoc
865 for _, c := range v.Buf.cursors {
867 if v.Cursor.HasSelection() &&
868 (charLoc.GreaterEqual(v.Cursor.CurSelection[0]) && charLoc.LessThan(v.Cursor.CurSelection[1]) ||
869 charLoc.LessThan(v.Cursor.CurSelection[0]) && charLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
870 // The current character is selected
871 lineStyle = defStyle.Reverse(true)
873 if style, ok := colorscheme["selection"]; ok {
878 v.SetCursor(&v.Buf.Cursor)
880 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
881 !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
882 style := GetColor("cursor-line")
883 fg, _, _ := style.Decompose()
884 lineStyle = lineStyle.Background(fg)
887 screen.SetContent(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, char.drawChar, nil, lineStyle)
889 for i, c := range v.Buf.cursors {
891 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
892 v.Cursor.Y == char.realLoc.Y && v.Cursor.X == char.realLoc.X && (!cursorSet || i != 0) {
893 ShowMultiCursor(xOffset+char.visualLoc.X, yOffset+char.visualLoc.Y, i)
897 v.SetCursor(&v.Buf.Cursor)
908 lastX = xOffset + lastChar.visualLoc.X + lastChar.width
909 for i, c := range v.Buf.cursors {
911 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
912 v.Cursor.Y == lastChar.realLoc.Y && v.Cursor.X == lastChar.realLoc.X+1 {
913 ShowMultiCursor(lastX, yOffset+lastChar.visualLoc.Y, i)
914 cx, cy = lastX, yOffset+lastChar.visualLoc.Y
917 v.SetCursor(&v.Buf.Cursor)
918 realLoc = Loc{lastChar.realLoc.X + 1, realLineN}
919 visualLoc = Loc{lastX - xOffset, lastChar.visualLoc.Y}
920 } else if len(line) == 0 {
921 for i, c := range v.Buf.cursors {
923 if tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() &&
924 v.Cursor.Y == realLineN {
925 ShowMultiCursor(xOffset, yOffset+visualLineN, i)
926 cx, cy = xOffset, yOffset+visualLineN
929 v.SetCursor(&v.Buf.Cursor)
931 realLoc = Loc{0, realLineN}
932 visualLoc = Loc{0, visualLineN}
935 if v.Cursor.HasSelection() &&
936 (realLoc.GreaterEqual(v.Cursor.CurSelection[0]) && realLoc.LessThan(v.Cursor.CurSelection[1]) ||
937 realLoc.LessThan(v.Cursor.CurSelection[0]) && realLoc.GreaterEqual(v.Cursor.CurSelection[1])) {
938 // The current character is selected
939 selectStyle := defStyle.Reverse(true)
941 if style, ok := colorscheme["selection"]; ok {
944 screen.SetContent(xOffset+visualLoc.X, yOffset+visualLoc.Y, ' ', nil, selectStyle)
947 if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].CurView == v.Num &&
948 !v.Cursor.HasSelection() && v.Cursor.Y == realLineN {
949 for i := lastX; i < xOffset+v.Width-v.lineNumOffset; i++ {
950 style := GetColor("cursor-line")
951 fg, _, _ := style.Decompose()
952 style = style.Background(fg)
953 if !(tabs[curTab].CurView == v.Num && !v.Cursor.HasSelection() && i == cx && yOffset+visualLineN == cy) {
954 screen.SetContent(i, yOffset+visualLineN, ' ', nil, style)
961 dividerStyle := defStyle
962 if style, ok := colorscheme["divider"]; ok {
965 for i := 0; i < v.Height; i++ {
966 screen.SetContent(v.x, yOffset+i, '|', nil, dividerStyle.Reverse(true))
971 // ShowMultiCursor will display a cursor at a location
972 // If i == 0 then the terminal cursor will be used
973 // Otherwise a fake cursor will be drawn at the position
974 func ShowMultiCursor(x, y, i int) {
976 screen.ShowCursor(x, y)
978 r, _, _, _ := screen.GetContent(x, y)
979 screen.SetContent(x, y, r, nil, defStyle.Reverse(true))
983 // Display renders the view, the cursor, and statusline
984 func (v *View) Display() {
985 if globalSettings["termtitle"].(bool) {
986 screen.SetTitle("micro: " + v.Buf.GetName())
989 // Don't draw the cursor if it is out of the viewport or if it has a selection
990 if v.Num == tabs[curTab].CurView && (v.Cursor.Y-v.Topline < 0 || v.Cursor.Y-v.Topline > v.Height-1 || v.Cursor.HasSelection()) {
993 _, screenH := screen.Size()
994 if v.Buf.Settings["statusline"].(bool) {
996 } else if (v.y + v.Height) != screenH-1 {
997 for x := 0; x < v.Width; x++ {
998 screen.SetContent(v.x+x, v.y+v.Height, '-', nil, defStyle.Reverse(true))