Skip to content

Commit 9eaeb19

Browse files
authored
Merge pull request zyedidia#3403 from masmu/refactor/tab-actions
Implemented new actions `FirstTab`, `LastTab`, `FirstSplit` and `LastSplit`
2 parents ca60120 + 5f83661 commit 9eaeb19

File tree

7 files changed

+87
-48
lines changed

7 files changed

+87
-48
lines changed

internal/action/actions.go

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,27 +1871,38 @@ func (h *BufPane) AddTab() bool {
18711871

18721872
// PreviousTab switches to the previous tab in the tab list
18731873
func (h *BufPane) PreviousTab() bool {
1874-
tabsLen := len(Tabs.List)
1875-
if tabsLen == 1 {
1874+
if Tabs.Active() == 0 {
18761875
return false
18771876
}
1878-
1879-
a := Tabs.Active() + tabsLen
1880-
Tabs.SetActive((a - 1) % tabsLen)
1881-
1877+
Tabs.SetActive(Tabs.Active() - 1)
18821878
return true
18831879
}
18841880

18851881
// NextTab switches to the next tab in the tab list
18861882
func (h *BufPane) NextTab() bool {
1887-
tabsLen := len(Tabs.List)
1888-
if tabsLen == 1 {
1883+
if Tabs.Active() == len(Tabs.List)-1 {
18891884
return false
18901885
}
1886+
Tabs.SetActive(Tabs.Active() + 1)
1887+
return true
1888+
}
18911889

1892-
a := Tabs.Active()
1893-
Tabs.SetActive((a + 1) % tabsLen)
1890+
// FirstTab switches to the first tab in the tab list
1891+
func (h *BufPane) FirstTab() bool {
1892+
if Tabs.Active() == 0 {
1893+
return false
1894+
}
1895+
Tabs.SetActive(0)
1896+
return true
1897+
}
18941898

1899+
// LastTab switches to the last tab in the tab list
1900+
func (h *BufPane) LastTab() bool {
1901+
lastTabIndex := len(Tabs.List) - 1
1902+
if Tabs.Active() == lastTabIndex {
1903+
return false
1904+
}
1905+
Tabs.SetActive(lastTabIndex)
18951906
return true
18961907
}
18971908

@@ -1926,36 +1937,38 @@ func (h *BufPane) Unsplit() bool {
19261937

19271938
// NextSplit changes the view to the next split
19281939
func (h *BufPane) NextSplit() bool {
1929-
if len(h.tab.Panes) == 1 {
1940+
if h.tab.active == len(h.tab.Panes)-1 {
19301941
return false
19311942
}
1932-
1933-
a := h.tab.active
1934-
if a < len(h.tab.Panes)-1 {
1935-
a++
1936-
} else {
1937-
a = 0
1938-
}
1939-
1940-
h.tab.SetActive(a)
1941-
1943+
h.tab.SetActive(h.tab.active + 1)
19421944
return true
19431945
}
19441946

19451947
// PreviousSplit changes the view to the previous split
19461948
func (h *BufPane) PreviousSplit() bool {
1947-
if len(h.tab.Panes) == 1 {
1949+
if h.tab.active == 0 {
19481950
return false
19491951
}
1952+
h.tab.SetActive(h.tab.active - 1)
1953+
return true
1954+
}
19501955

1951-
a := h.tab.active
1952-
if a > 0 {
1953-
a--
1954-
} else {
1955-
a = len(h.tab.Panes) - 1
1956+
// FirstSplit changes the view to the first split
1957+
func (h *BufPane) FirstSplit() bool {
1958+
if h.tab.active == 0 {
1959+
return false
19561960
}
1957-
h.tab.SetActive(a)
1961+
h.tab.SetActive(0)
1962+
return true
1963+
}
19581964

1965+
// LastSplit changes the view to the last split
1966+
func (h *BufPane) LastSplit() bool {
1967+
lastPaneIdx := len(h.tab.Panes) - 1
1968+
if h.tab.active == lastPaneIdx {
1969+
return false
1970+
}
1971+
h.tab.SetActive(lastPaneIdx)
19591972
return true
19601973
}
19611974

internal/action/bufpane.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,19 +660,27 @@ func (h *BufPane) DoRuneInsert(r rune) {
660660
func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane {
661661
e := NewBufPaneFromBuf(buf, h.tab)
662662
e.splitID = MainTab().GetNode(h.splitID).VSplit(right)
663-
MainTab().Panes = append(MainTab().Panes, e)
663+
currentPaneIdx := MainTab().GetPane(h.splitID)
664+
if right {
665+
currentPaneIdx++
666+
}
667+
MainTab().AddPane(e, currentPaneIdx)
664668
MainTab().Resize()
665-
MainTab().SetActive(len(MainTab().Panes) - 1)
669+
MainTab().SetActive(currentPaneIdx)
666670
return e
667671
}
668672

669673
// HSplitIndex opens the given buffer in a horizontal split on the given side.
670674
func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane {
671675
e := NewBufPaneFromBuf(buf, h.tab)
672676
e.splitID = MainTab().GetNode(h.splitID).HSplit(bottom)
673-
MainTab().Panes = append(MainTab().Panes, e)
677+
currentPaneIdx := MainTab().GetPane(h.splitID)
678+
if bottom {
679+
currentPaneIdx++
680+
}
681+
MainTab().AddPane(e, currentPaneIdx)
674682
MainTab().Resize()
675-
MainTab().SetActive(len(MainTab().Panes) - 1)
683+
MainTab().SetActive(currentPaneIdx)
676684
return e
677685
}
678686

@@ -822,8 +830,12 @@ var BufKeyActions = map[string]BufKeyAction{
822830
"AddTab": (*BufPane).AddTab,
823831
"PreviousTab": (*BufPane).PreviousTab,
824832
"NextTab": (*BufPane).NextTab,
833+
"FirstTab": (*BufPane).FirstTab,
834+
"LastTab": (*BufPane).LastTab,
825835
"NextSplit": (*BufPane).NextSplit,
826836
"PreviousSplit": (*BufPane).PreviousSplit,
837+
"FirstSplit": (*BufPane).FirstSplit,
838+
"LastSplit": (*BufPane).LastSplit,
827839
"Unsplit": (*BufPane).Unsplit,
828840
"VSplit": (*BufPane).VSplitAction,
829841
"HSplit": (*BufPane).HSplitAction,

internal/action/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package action
33
var termdefaults = map[string]string{
44
"<Ctrl-q><Ctrl-q>": "Exit",
55
"<Ctrl-e><Ctrl-e>": "CommandMode",
6-
"<Ctrl-w><Ctrl-w>": "NextSplit",
6+
"<Ctrl-w><Ctrl-w>": "NextSplit|FirstSplit",
77
}
88

99
// DefaultBindings returns a map containing micro's default keybindings

internal/action/defaults_darwin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ var bufdefaults = map[string]string{
5252
"Ctrl-v": "Paste",
5353
"Ctrl-a": "SelectAll",
5454
"Ctrl-t": "AddTab",
55-
"Alt-,": "PreviousTab",
56-
"Alt-.": "NextTab",
55+
"Alt-,": "PreviousTab|LastTab",
56+
"Alt-.": "NextTab|FirstTab",
5757
"Home": "StartOfTextToggle",
5858
"End": "EndOfLine",
5959
"CtrlHome": "CursorStart",
6060
"CtrlEnd": "CursorEnd",
6161
"PageUp": "CursorPageUp",
6262
"PageDown": "CursorPageDown",
63-
"CtrlPageUp": "PreviousTab",
64-
"CtrlPageDown": "NextTab",
63+
"CtrlPageUp": "PreviousTab|LastTab",
64+
"CtrlPageDown": "NextTab|FirstTab",
6565
"ShiftPageUp": "SelectPageUp",
6666
"ShiftPageDown": "SelectPageDown",
6767
"Ctrl-g": "ToggleHelp",
@@ -72,7 +72,7 @@ var bufdefaults = map[string]string{
7272
"Ctrl-b": "ShellMode",
7373
"Ctrl-q": "Quit",
7474
"Ctrl-e": "CommandMode",
75-
"Ctrl-w": "NextSplit",
75+
"Ctrl-w": "NextSplit|FirstSplit",
7676
"Ctrl-u": "ToggleMacro",
7777
"Ctrl-j": "PlayMacro",
7878
"Insert": "ToggleOverwriteMode",

internal/action/defaults_other.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ var bufdefaults = map[string]string{
5555
"Ctrl-v": "Paste",
5656
"Ctrl-a": "SelectAll",
5757
"Ctrl-t": "AddTab",
58-
"Alt-,": "PreviousTab",
59-
"Alt-.": "NextTab",
58+
"Alt-,": "PreviousTab|LastTab",
59+
"Alt-.": "NextTab|FirstTab",
6060
"Home": "StartOfTextToggle",
6161
"End": "EndOfLine",
6262
"CtrlHome": "CursorStart",
6363
"CtrlEnd": "CursorEnd",
6464
"PageUp": "CursorPageUp",
6565
"PageDown": "CursorPageDown",
66-
"CtrlPageUp": "PreviousTab",
67-
"CtrlPageDown": "NextTab",
66+
"CtrlPageUp": "PreviousTab|LastTab",
67+
"CtrlPageDown": "NextTab|FirstTab",
6868
"ShiftPageUp": "SelectPageUp",
6969
"ShiftPageDown": "SelectPageDown",
7070
"Ctrl-g": "ToggleHelp",
@@ -75,7 +75,7 @@ var bufdefaults = map[string]string{
7575
"Ctrl-b": "ShellMode",
7676
"Ctrl-q": "Quit",
7777
"Ctrl-e": "CommandMode",
78-
"Ctrl-w": "NextSplit",
78+
"Ctrl-w": "NextSplit|FirstSplit",
7979
"Ctrl-u": "ToggleMacro",
8080
"Ctrl-j": "PlayMacro",
8181
"Insert": "ToggleOverwriteMode",

internal/action/tab.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ func (t *Tab) SetActive(i int) {
349349
}
350350
}
351351

352+
// AddPane adds a pane at a given index
353+
func (t *Tab) AddPane(pane Pane, i int) {
354+
if len(t.Panes) == i {
355+
t.Panes = append(t.Panes, pane)
356+
return
357+
}
358+
t.Panes = append(t.Panes[:i+1], t.Panes[i:]...)
359+
t.Panes[i] = pane
360+
}
361+
352362
// GetPane returns the pane with the given split index
353363
func (t *Tab) GetPane(splitid uint64) int {
354364
for i, p := range t.Panes {

runtime/help/keybindings.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,15 @@ QuitAll
253253
AddTab
254254
PreviousTab
255255
NextTab
256+
FirstTab
257+
LastTab
256258
NextSplit
257259
Unsplit
258260
VSplit
259261
HSplit
260262
PreviousSplit
263+
FirstSplit
264+
LastSplit
261265
ToggleMacro
262266
PlayMacro
263267
Suspend (Unix only)
@@ -510,16 +514,16 @@ conventions for text editing defaults.
510514
"Ctrl-v": "Paste",
511515
"Ctrl-a": "SelectAll",
512516
"Ctrl-t": "AddTab",
513-
"Alt-,": "PreviousTab",
514-
"Alt-.": "NextTab",
517+
"Alt-,": "PreviousTab|LastTab",
518+
"Alt-.": "NextTab|FirstTab",
515519
"Home": "StartOfText",
516520
"End": "EndOfLine",
517521
"CtrlHome": "CursorStart",
518522
"CtrlEnd": "CursorEnd",
519523
"PageUp": "CursorPageUp",
520524
"PageDown": "CursorPageDown",
521-
"CtrlPageUp": "PreviousTab",
522-
"CtrlPageDown": "NextTab",
525+
"CtrlPageUp": "PreviousTab|LastTab",
526+
"CtrlPageDown": "NextTab|FirstTab",
523527
"ShiftPageUp": "SelectPageUp",
524528
"ShiftPageDown": "SelectPageDown",
525529
"Ctrl-g": "ToggleHelp",
@@ -530,7 +534,7 @@ conventions for text editing defaults.
530534
"Ctrl-b": "ShellMode",
531535
"Ctrl-q": "Quit",
532536
"Ctrl-e": "CommandMode",
533-
"Ctrl-w": "NextSplit",
537+
"Ctrl-w": "NextSplit|FirstSplit",
534538
"Ctrl-u": "ToggleMacro",
535539
"Ctrl-j": "PlayMacro",
536540
"Insert": "ToggleOverwriteMode",

0 commit comments

Comments
 (0)