Skip to content

Commit

Permalink
Merge pull request #3403 from masmu/refactor/tab-actions
Browse files Browse the repository at this point in the history
Implemented new actions `FirstTab`, `LastTab`, `FirstSplit` and `LastSplit`
  • Loading branch information
dmaluka committed Sep 16, 2024
2 parents ca60120 + 5f83661 commit 9eaeb19
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 48 deletions.
69 changes: 41 additions & 28 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1871,27 +1871,38 @@ func (h *BufPane) AddTab() bool {

// PreviousTab switches to the previous tab in the tab list
func (h *BufPane) PreviousTab() bool {
tabsLen := len(Tabs.List)
if tabsLen == 1 {
if Tabs.Active() == 0 {
return false
}

a := Tabs.Active() + tabsLen
Tabs.SetActive((a - 1) % tabsLen)

Tabs.SetActive(Tabs.Active() - 1)
return true
}

// NextTab switches to the next tab in the tab list
func (h *BufPane) NextTab() bool {
tabsLen := len(Tabs.List)
if tabsLen == 1 {
if Tabs.Active() == len(Tabs.List)-1 {
return false
}
Tabs.SetActive(Tabs.Active() + 1)
return true
}

a := Tabs.Active()
Tabs.SetActive((a + 1) % tabsLen)
// FirstTab switches to the first tab in the tab list
func (h *BufPane) FirstTab() bool {
if Tabs.Active() == 0 {
return false
}
Tabs.SetActive(0)
return true
}

// LastTab switches to the last tab in the tab list
func (h *BufPane) LastTab() bool {
lastTabIndex := len(Tabs.List) - 1
if Tabs.Active() == lastTabIndex {
return false
}
Tabs.SetActive(lastTabIndex)
return true
}

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

// NextSplit changes the view to the next split
func (h *BufPane) NextSplit() bool {
if len(h.tab.Panes) == 1 {
if h.tab.active == len(h.tab.Panes)-1 {
return false
}

a := h.tab.active
if a < len(h.tab.Panes)-1 {
a++
} else {
a = 0
}

h.tab.SetActive(a)

h.tab.SetActive(h.tab.active + 1)
return true
}

// PreviousSplit changes the view to the previous split
func (h *BufPane) PreviousSplit() bool {
if len(h.tab.Panes) == 1 {
if h.tab.active == 0 {
return false
}
h.tab.SetActive(h.tab.active - 1)
return true
}

a := h.tab.active
if a > 0 {
a--
} else {
a = len(h.tab.Panes) - 1
// FirstSplit changes the view to the first split
func (h *BufPane) FirstSplit() bool {
if h.tab.active == 0 {
return false
}
h.tab.SetActive(a)
h.tab.SetActive(0)
return true
}

// LastSplit changes the view to the last split
func (h *BufPane) LastSplit() bool {
lastPaneIdx := len(h.tab.Panes) - 1
if h.tab.active == lastPaneIdx {
return false
}
h.tab.SetActive(lastPaneIdx)
return true
}

Expand Down
20 changes: 16 additions & 4 deletions internal/action/bufpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,27 @@ func (h *BufPane) DoRuneInsert(r rune) {
func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane {
e := NewBufPaneFromBuf(buf, h.tab)
e.splitID = MainTab().GetNode(h.splitID).VSplit(right)
MainTab().Panes = append(MainTab().Panes, e)
currentPaneIdx := MainTab().GetPane(h.splitID)
if right {
currentPaneIdx++
}
MainTab().AddPane(e, currentPaneIdx)
MainTab().Resize()
MainTab().SetActive(len(MainTab().Panes) - 1)
MainTab().SetActive(currentPaneIdx)
return e
}

// HSplitIndex opens the given buffer in a horizontal split on the given side.
func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane {
e := NewBufPaneFromBuf(buf, h.tab)
e.splitID = MainTab().GetNode(h.splitID).HSplit(bottom)
MainTab().Panes = append(MainTab().Panes, e)
currentPaneIdx := MainTab().GetPane(h.splitID)
if bottom {
currentPaneIdx++
}
MainTab().AddPane(e, currentPaneIdx)
MainTab().Resize()
MainTab().SetActive(len(MainTab().Panes) - 1)
MainTab().SetActive(currentPaneIdx)
return e
}

Expand Down Expand Up @@ -822,8 +830,12 @@ var BufKeyActions = map[string]BufKeyAction{
"AddTab": (*BufPane).AddTab,
"PreviousTab": (*BufPane).PreviousTab,
"NextTab": (*BufPane).NextTab,
"FirstTab": (*BufPane).FirstTab,
"LastTab": (*BufPane).LastTab,
"NextSplit": (*BufPane).NextSplit,
"PreviousSplit": (*BufPane).PreviousSplit,
"FirstSplit": (*BufPane).FirstSplit,
"LastSplit": (*BufPane).LastSplit,
"Unsplit": (*BufPane).Unsplit,
"VSplit": (*BufPane).VSplitAction,
"HSplit": (*BufPane).HSplitAction,
Expand Down
2 changes: 1 addition & 1 deletion internal/action/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package action
var termdefaults = map[string]string{
"<Ctrl-q><Ctrl-q>": "Exit",
"<Ctrl-e><Ctrl-e>": "CommandMode",
"<Ctrl-w><Ctrl-w>": "NextSplit",
"<Ctrl-w><Ctrl-w>": "NextSplit|FirstSplit",
}

// DefaultBindings returns a map containing micro's default keybindings
Expand Down
10 changes: 5 additions & 5 deletions internal/action/defaults_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ var bufdefaults = map[string]string{
"Ctrl-v": "Paste",
"Ctrl-a": "SelectAll",
"Ctrl-t": "AddTab",
"Alt-,": "PreviousTab",
"Alt-.": "NextTab",
"Alt-,": "PreviousTab|LastTab",
"Alt-.": "NextTab|FirstTab",
"Home": "StartOfTextToggle",
"End": "EndOfLine",
"CtrlHome": "CursorStart",
"CtrlEnd": "CursorEnd",
"PageUp": "CursorPageUp",
"PageDown": "CursorPageDown",
"CtrlPageUp": "PreviousTab",
"CtrlPageDown": "NextTab",
"CtrlPageUp": "PreviousTab|LastTab",
"CtrlPageDown": "NextTab|FirstTab",
"ShiftPageUp": "SelectPageUp",
"ShiftPageDown": "SelectPageDown",
"Ctrl-g": "ToggleHelp",
Expand All @@ -72,7 +72,7 @@ var bufdefaults = map[string]string{
"Ctrl-b": "ShellMode",
"Ctrl-q": "Quit",
"Ctrl-e": "CommandMode",
"Ctrl-w": "NextSplit",
"Ctrl-w": "NextSplit|FirstSplit",
"Ctrl-u": "ToggleMacro",
"Ctrl-j": "PlayMacro",
"Insert": "ToggleOverwriteMode",
Expand Down
10 changes: 5 additions & 5 deletions internal/action/defaults_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ var bufdefaults = map[string]string{
"Ctrl-v": "Paste",
"Ctrl-a": "SelectAll",
"Ctrl-t": "AddTab",
"Alt-,": "PreviousTab",
"Alt-.": "NextTab",
"Alt-,": "PreviousTab|LastTab",
"Alt-.": "NextTab|FirstTab",
"Home": "StartOfTextToggle",
"End": "EndOfLine",
"CtrlHome": "CursorStart",
"CtrlEnd": "CursorEnd",
"PageUp": "CursorPageUp",
"PageDown": "CursorPageDown",
"CtrlPageUp": "PreviousTab",
"CtrlPageDown": "NextTab",
"CtrlPageUp": "PreviousTab|LastTab",
"CtrlPageDown": "NextTab|FirstTab",
"ShiftPageUp": "SelectPageUp",
"ShiftPageDown": "SelectPageDown",
"Ctrl-g": "ToggleHelp",
Expand All @@ -75,7 +75,7 @@ var bufdefaults = map[string]string{
"Ctrl-b": "ShellMode",
"Ctrl-q": "Quit",
"Ctrl-e": "CommandMode",
"Ctrl-w": "NextSplit",
"Ctrl-w": "NextSplit|FirstSplit",
"Ctrl-u": "ToggleMacro",
"Ctrl-j": "PlayMacro",
"Insert": "ToggleOverwriteMode",
Expand Down
10 changes: 10 additions & 0 deletions internal/action/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ func (t *Tab) SetActive(i int) {
}
}

// AddPane adds a pane at a given index
func (t *Tab) AddPane(pane Pane, i int) {
if len(t.Panes) == i {
t.Panes = append(t.Panes, pane)
return
}
t.Panes = append(t.Panes[:i+1], t.Panes[i:]...)
t.Panes[i] = pane
}

// GetPane returns the pane with the given split index
func (t *Tab) GetPane(splitid uint64) int {
for i, p := range t.Panes {
Expand Down
14 changes: 9 additions & 5 deletions runtime/help/keybindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,15 @@ QuitAll
AddTab
PreviousTab
NextTab
FirstTab
LastTab
NextSplit
Unsplit
VSplit
HSplit
PreviousSplit
FirstSplit
LastSplit
ToggleMacro
PlayMacro
Suspend (Unix only)
Expand Down Expand Up @@ -510,16 +514,16 @@ conventions for text editing defaults.
"Ctrl-v": "Paste",
"Ctrl-a": "SelectAll",
"Ctrl-t": "AddTab",
"Alt-,": "PreviousTab",
"Alt-.": "NextTab",
"Alt-,": "PreviousTab|LastTab",
"Alt-.": "NextTab|FirstTab",
"Home": "StartOfText",
"End": "EndOfLine",
"CtrlHome": "CursorStart",
"CtrlEnd": "CursorEnd",
"PageUp": "CursorPageUp",
"PageDown": "CursorPageDown",
"CtrlPageUp": "PreviousTab",
"CtrlPageDown": "NextTab",
"CtrlPageUp": "PreviousTab|LastTab",
"CtrlPageDown": "NextTab|FirstTab",
"ShiftPageUp": "SelectPageUp",
"ShiftPageDown": "SelectPageDown",
"Ctrl-g": "ToggleHelp",
Expand All @@ -530,7 +534,7 @@ conventions for text editing defaults.
"Ctrl-b": "ShellMode",
"Ctrl-q": "Quit",
"Ctrl-e": "CommandMode",
"Ctrl-w": "NextSplit",
"Ctrl-w": "NextSplit|FirstSplit",
"Ctrl-u": "ToggleMacro",
"Ctrl-j": "PlayMacro",
"Insert": "ToggleOverwriteMode",
Expand Down

0 comments on commit 9eaeb19

Please sign in to comment.