]> git.lizzy.rs Git - micro.git/blob - cmd/micro/tab.go
Move cursor with search selection
[micro.git] / cmd / micro / tab.go
1 package main
2
3 import (
4         "sort"
5
6         "github.com/zyedidia/tcell"
7 )
8
9 type Tab struct {
10         // This contains all the views in this tab
11         // There is generally only one view per tab, but you can have
12         // multiple views with splits
13         views []*View
14         // This is the current view for this tab
15         curView int
16         // Generally this is the name of the current view's buffer
17         name string
18
19         tree *SplitTree
20 }
21
22 // NewTabFromView creates a new tab and puts the given view in the tab
23 func NewTabFromView(v *View) *Tab {
24         t := new(Tab)
25         t.views = append(t.views, v)
26         t.views[0].Num = 0
27
28         t.tree = new(SplitTree)
29         t.tree.kind = VerticalSplit
30         t.tree.children = []Node{NewLeafNode(t.views[0], t.tree)}
31
32         w, h := screen.Size()
33         t.tree.width = w
34         t.tree.height = h - 1
35         return t
36 }
37
38 // SetNum sets all this tab's views to have the correct tab number
39 func (t *Tab) SetNum(num int) {
40         for _, v := range t.views {
41                 v.TabNum = num
42         }
43 }
44
45 func (t *Tab) Cleanup() {
46         t.tree.Cleanup()
47 }
48
49 func (t *Tab) Resize() {
50         w, h := screen.Size()
51         t.tree.width = w
52         t.tree.height = h - 1
53         t.tree.ResizeSplits()
54 }
55
56 // CurView returns the current view
57 func CurView() *View {
58         curTab := tabs[curTab]
59         return curTab.views[curTab.curView]
60 }
61
62 // TabbarString returns the string that should be displayed in the tabbar
63 // It also returns a map containing which indicies correspond to which tab number
64 // This is useful when we know that the mouse click has occured at an x location
65 // but need to know which tab that corresponds to to accurately change the tab
66 func TabbarString() (string, map[int]int) {
67         str := ""
68         indicies := make(map[int]int)
69         for i, t := range tabs {
70                 if i == curTab {
71                         str += "["
72                 } else {
73                         str += " "
74                 }
75                 str += t.views[t.curView].Buf.Name
76                 if i == curTab {
77                         str += "]"
78                 } else {
79                         str += " "
80                 }
81                 indicies[len(str)-1] = i + 1
82                 str += " "
83         }
84         return str, indicies
85 }
86
87 // TabbarHandleMouseEvent checks the given mouse event if it is clicking on the tabbar
88 // If it is it changes the current tab accordingly
89 // This function returns true if the tab is changed
90 func TabbarHandleMouseEvent(event tcell.Event) bool {
91         // There is no tabbar displayed if there are less than 2 tabs
92         if len(tabs) <= 1 {
93                 return false
94         }
95
96         switch e := event.(type) {
97         case *tcell.EventMouse:
98                 button := e.Buttons()
99                 // Must be a left click
100                 if button == tcell.Button1 {
101                         x, y := e.Position()
102                         if y != 0 {
103                                 return false
104                         }
105                         str, indicies := TabbarString()
106                         if x >= len(str) {
107                                 return false
108                         }
109                         var tabnum int
110                         var keys []int
111                         for k := range indicies {
112                                 keys = append(keys, k)
113                         }
114                         sort.Ints(keys)
115                         for _, k := range keys {
116                                 if x <= k {
117                                         tabnum = indicies[k] - 1
118                                         break
119                                 }
120                         }
121                         curTab = tabnum
122                         return true
123                 }
124         }
125
126         return false
127 }
128
129 // DisplayTabs displays the tabbar at the top of the editor if there are multiple tabs
130 func DisplayTabs() {
131         if len(tabs) <= 1 {
132                 return
133         }
134
135         str, _ := TabbarString()
136
137         tabBarStyle := defStyle.Reverse(true)
138         if style, ok := colorscheme["tabbar"]; ok {
139                 tabBarStyle = style
140         }
141
142         // Maybe there is a unicode filename?
143         fileRunes := []rune(str)
144         w, _ := screen.Size()
145         for x := 0; x < w; x++ {
146                 if x < len(fileRunes) {
147                         screen.SetContent(x, 0, fileRunes[x], nil, tabBarStyle)
148                 } else {
149                         screen.SetContent(x, 0, ' ', nil, tabBarStyle)
150                 }
151         }
152 }