Allow splits to be created in either direction

This commit adds the `splitRight` and `splitBottom` options to allow
the user to pick which direction to split in.

This also means that a new split is no longer just appended to the list
of splits.
This commit is contained in:
Zachary Yedidia
2016-11-28 19:16:49 -05:00
parent 78b2a99f2e
commit 1b92700990
6 changed files with 106 additions and 26 deletions

File diff suppressed because one or more lines are too long

View File

@@ -190,6 +190,8 @@ func DefaultGlobalSettings() map[string]interface{} {
"scrollspeed": float64(2),
"scrollmargin": float64(3),
"softwrap": false,
"splitRight": true,
"splitBottom": true,
"statusline": true,
"syntax": true,
"tabsize": float64(4),
@@ -219,6 +221,8 @@ func DefaultLocalSettings() map[string]interface{} {
"scrollspeed": float64(2),
"scrollmargin": float64(3),
"softwrap": false,
"splitRight": true,
"splitBottom": true,
"statusline": true,
"syntax": true,
"tabsize": float64(4),

View File

@@ -12,8 +12,8 @@ const (
// A Node on the split tree
type Node interface {
VSplit(buf *Buffer)
HSplit(buf *Buffer)
VSplit(buf *Buffer, splitRight bool)
HSplit(buf *Buffer, splitBottom bool)
String() string
}
@@ -52,16 +52,26 @@ type SplitTree struct {
}
// VSplit creates a vertical split
func (l *LeafNode) VSplit(buf *Buffer) {
func (l *LeafNode) VSplit(buf *Buffer, splitRight bool) {
tab := tabs[l.parent.tabNum]
if l.parent.kind == VerticalSplit {
newView := NewView(buf)
newView.TabNum = l.parent.tabNum
newView.Num = len(tab.views)
l.parent.children = append(l.parent.children, NewLeafNode(newView, l.parent))
tab.curView++
tab.views = append(tab.views, newView)
i := 0
if splitRight {
i = 1
}
l.parent.children = append(l.parent.children, nil)
copy(l.parent.children[l.view.Num+i+1:], l.parent.children[l.view.Num+i:])
l.parent.children[l.view.Num+i] = NewLeafNode(newView, l.parent)
tab.views = append(tab.views, nil)
copy(tab.views[l.view.Num+i+1:], tab.views[l.view.Num+i:])
tab.views[l.view.Num+i] = newView
tab.curView = l.view.Num + i
} else {
s := new(SplitTree)
s.kind = VerticalSplit
@@ -69,27 +79,47 @@ func (l *LeafNode) VSplit(buf *Buffer) {
s.tabNum = l.parent.tabNum
newView := NewView(buf)
newView.TabNum = l.parent.tabNum
newView.Num = len(tab.views)
s.children = []Node{l, NewLeafNode(newView, s)}
i := 0
if splitRight {
i = 1
s.children = []Node{l, NewLeafNode(newView, s)}
} else {
tab.curView = l.view.Num
}
l.parent.children[search(l.parent.children, l)] = s
l.parent = s
tab.curView++
tab.views = append(tab.views, newView)
tab.views = append(tab.views, nil)
copy(tab.views[l.view.Num+i+1:], tab.views[l.view.Num+i:])
tab.views[l.view.Num+i] = newView
tab.curView = l.view.Num + i
}
tab.Resize()
}
// HSplit creates a horizontal split
func (l *LeafNode) HSplit(buf *Buffer) {
func (l *LeafNode) HSplit(buf *Buffer, splitBottom bool) {
tab := tabs[l.parent.tabNum]
if l.parent.kind == HorizontalSplit {
newView := NewView(buf)
newView.TabNum = l.parent.tabNum
newView.Num = len(tab.views)
l.parent.children = append(l.parent.children, NewLeafNode(newView, l.parent))
tab.curView++
tab.views = append(tab.views, newView)
i := 0
if splitBottom {
i = 1
}
l.parent.children = append(l.parent.children, nil)
copy(l.parent.children[l.view.Num+i+1:], l.parent.children[l.view.Num+i:])
l.parent.children[l.view.Num+i] = NewLeafNode(newView, l.parent)
tab.views = append(tab.views, nil)
copy(tab.views[l.view.Num+i+1:], tab.views[l.view.Num+i:])
tab.views[l.view.Num+i] = newView
tab.curView = l.view.Num + i
} else {
s := new(SplitTree)
s.kind = HorizontalSplit
@@ -98,13 +128,24 @@ func (l *LeafNode) HSplit(buf *Buffer) {
newView := NewView(buf)
newView.TabNum = l.parent.tabNum
newView.Num = len(tab.views)
s.children = []Node{l, NewLeafNode(newView, s)}
i := 0
if splitBottom {
i = 1
s.children = []Node{l, NewLeafNode(newView, s)}
} else {
s.children = []Node{NewLeafNode(newView, s), l}
}
l.parent.children[search(l.parent.children, l)] = s
l.parent = s
tab.curView++
tab.views = append(tab.views, newView)
tab.views = append(tab.views, nil)
copy(tab.views[l.view.Num+i+1:], tab.views[l.view.Num+i:])
tab.views[l.view.Num+i] = newView
tab.curView = l.view.Num + i
}
tab.Resize()
}
// Delete deletes a split
@@ -253,10 +294,10 @@ func findView(haystack []*View, needle *View) int {
}
// VSplit is here just to make SplitTree fit the Node interface
func (s *SplitTree) VSplit(buf *Buffer) {}
func (s *SplitTree) VSplit(buf *Buffer, splitRight bool) {}
// HSplit is here just to make SplitTree fit the Node interface
func (s *SplitTree) HSplit(buf *Buffer) {}
func (s *SplitTree) HSplit(buf *Buffer, splitBottom bool) {}
func (s *SplitTree) String() string {
str := "["

View File

@@ -64,6 +64,10 @@ func (t *Tab) Resize() {
}
t.tree.ResizeSplits()
for i, v := range t.views {
v.Num = i
}
}
// CurView returns the current view

View File

@@ -274,15 +274,13 @@ func (v *View) ReOpen() {
// HSplit opens a horizontal split with the given buffer
func (v *View) HSplit(buf *Buffer) bool {
v.splitNode.HSplit(buf)
tabs[v.TabNum].Resize()
v.splitNode.HSplit(buf, v.Buf.Settings["splitRight"].(bool))
return false
}
// VSplit opens a vertical split with the given buffer
func (v *View) VSplit(buf *Buffer) bool {
v.splitNode.VSplit(buf)
tabs[v.TabNum].Resize()
v.splitNode.VSplit(buf, v.Buf.Settings["splitBottom"].(bool))
return false
}

View File

@@ -101,6 +101,16 @@ Here are the options that you can set:
default value: `off`
* `splitRight`: when a vertical split is created, should it be created to the right of
the current split?
default value: `on`
* `splitRight`: when a horizontal split is created, should it be created below the
current split?
default value: `on`
* `autosave`: micro will save the buffer every 8 seconds automatically.
Micro also will automatically save and quit when you exit without asking.
Be careful when using this feature, because you might accidentally save a file,