| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"runtime" |
| 6 |
|
| 7 |
"github.com/micro-editor/tcell/v2" |
| 8 |
"github.com/micro-editor/terminal" |
| 9 |
"github.com/zyedidia/micro/v2/internal/clipboard" |
| 10 |
"github.com/zyedidia/micro/v2/internal/config" |
| 11 |
"github.com/zyedidia/micro/v2/internal/display" |
| 12 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 13 |
"github.com/zyedidia/micro/v2/internal/shell" |
| 14 |
) |
| 15 |
|
| 16 |
type TermKeyAction func(*TermPane) |
| 17 |
|
| 18 |
var TermBindings *KeyTree |
| 19 |
|
| 20 |
func init() { |
| 21 |
TermBindings = NewKeyTree() |
| 22 |
} |
| 23 |
|
| 24 |
func TermKeyActionGeneral(a TermKeyAction) PaneKeyAction { |
| 25 |
return func(p Pane) bool { |
| 26 |
a(p.(*TermPane)) |
| 27 |
return true |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TermMapEvent(k Event, action string) { |
| 32 |
config.Bindings["terminal"][k.Name()] = action |
| 33 |
|
| 34 |
switch e := k.(type) { |
| 35 |
case KeyEvent, KeySequenceEvent, RawEvent: |
| 36 |
termMapKey(e, action) |
| 37 |
case MouseEvent: |
| 38 |
termMapMouse(e, action) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func termMapKey(k Event, action string) { |
| 43 |
if f, ok := TermKeyActions[action]; ok { |
| 44 |
TermBindings.RegisterKeyBinding(k, TermKeyActionGeneral(f)) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func termMapMouse(k MouseEvent, action string) { |
| 49 |
// TODO: map mouse |
| 50 |
termMapKey(k, action) |
| 51 |
} |
| 52 |
|
| 53 |
type TermPane struct { |
| 54 |
*shell.Terminal |
| 55 |
display.Window |
| 56 |
|
| 57 |
mouseReleased bool |
| 58 |
id uint64 |
| 59 |
tab *Tab |
| 60 |
} |
| 61 |
|
| 62 |
func NewTermPane(x, y, w, h int, t *shell.Terminal, id uint64, tab *Tab) (*TermPane, error) { |
| 63 |
if !TermEmuSupported { |
| 64 |
return nil, errors.New("Terminal emulator is not supported on this system") |
| 65 |
} |
| 66 |
|
| 67 |
th := new(TermPane) |
| 68 |
th.Terminal = t |
| 69 |
th.id = id |
| 70 |
th.mouseReleased = true |
| 71 |
th.Window = display.NewTermWindow(x, y, w, h, t) |
| 72 |
th.tab = tab |
| 73 |
return th, nil |
| 74 |
} |
| 75 |
|
| 76 |
func (t *TermPane) ID() uint64 { |
| 77 |
return t.id |
| 78 |
} |
| 79 |
|
| 80 |
func (t *TermPane) SetID(i uint64) { |
| 81 |
t.id = i |
| 82 |
} |
| 83 |
|
| 84 |
func (t *TermPane) Name() string { |
| 85 |
return t.Terminal.Name() |
| 86 |
} |
| 87 |
|
| 88 |
func (t *TermPane) SetTab(tab *Tab) { |
| 89 |
t.tab = tab |
| 90 |
} |
| 91 |
|
| 92 |
func (t *TermPane) Tab() *Tab { |
| 93 |
return t.tab |
| 94 |
} |
| 95 |
|
| 96 |
func (t *TermPane) Close() {} |
| 97 |
|
| 98 |
// Quit closes this termpane |
| 99 |
func (t *TermPane) Quit() { |
| 100 |
t.Close() |
| 101 |
if len(MainTab().Panes) > 1 { |
| 102 |
t.Unsplit() |
| 103 |
} else if len(Tabs.List) > 1 { |
| 104 |
Tabs.RemoveTab(t.id) |
| 105 |
} else { |
| 106 |
screen.Screen.Fini() |
| 107 |
InfoBar.Close() |
| 108 |
runtime.Goexit() |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Unsplit removes this split |
| 113 |
func (t *TermPane) Unsplit() { |
| 114 |
n := MainTab().GetNode(t.id) |
| 115 |
n.Unsplit() |
| 116 |
|
| 117 |
MainTab().RemovePane(MainTab().GetPane(t.id)) |
| 118 |
MainTab().Resize() |
| 119 |
MainTab().SetActive(len(MainTab().Panes) - 1) |
| 120 |
} |
| 121 |
|
| 122 |
// HandleEvent handles a tcell event by forwarding it to the terminal emulator |
| 123 |
// If the event is a mouse event and the program running in the emulator |
| 124 |
// does not have mouse support, the emulator will support selections and |
| 125 |
// copy-paste |
| 126 |
func (t *TermPane) HandleEvent(event tcell.Event) { |
| 127 |
if e, ok := event.(*tcell.EventKey); ok { |
| 128 |
ke := keyEvent(e) |
| 129 |
action, more := TermBindings.NextEvent(ke, nil) |
| 130 |
|
| 131 |
if !more { |
| 132 |
if action != nil { |
| 133 |
action(t) |
| 134 |
TermBindings.ResetEvents() |
| 135 |
return |
| 136 |
} |
| 137 |
TermBindings.ResetEvents() |
| 138 |
} |
| 139 |
|
| 140 |
if more { |
| 141 |
return |
| 142 |
} |
| 143 |
|
| 144 |
if t.Status == shell.TTDone { |
| 145 |
switch e.Key() { |
| 146 |
case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter: |
| 147 |
t.Close() |
| 148 |
t.Quit() |
| 149 |
default: |
| 150 |
} |
| 151 |
} |
| 152 |
if e.Key() == tcell.KeyCtrlC && t.HasSelection() { |
| 153 |
clipboard.Write(t.GetSelection(t.GetView().Width), clipboard.ClipboardReg) |
| 154 |
InfoBar.Message("Copied selection to clipboard") |
| 155 |
} else if t.Status != shell.TTDone { |
| 156 |
t.WriteString(event.EscSeq()) |
| 157 |
} |
| 158 |
} else if _, ok := event.(*tcell.EventPaste); ok { |
| 159 |
if t.Status != shell.TTDone { |
| 160 |
t.WriteString(event.EscSeq()) |
| 161 |
} |
| 162 |
} else if e, ok := event.(*tcell.EventMouse); !ok || t.State.Mode(terminal.ModeMouseMask) { |
| 163 |
// t.WriteString(event.EscSeq()) |
| 164 |
} else { |
| 165 |
x, y := e.Position() |
| 166 |
v := t.GetView() |
| 167 |
x -= v.X |
| 168 |
y -= v.Y |
| 169 |
|
| 170 |
if e.Buttons() == tcell.Button1 { |
| 171 |
if !t.mouseReleased { |
| 172 |
// drag |
| 173 |
t.Selection[1].X = x |
| 174 |
t.Selection[1].Y = y |
| 175 |
} else { |
| 176 |
t.Selection[0].X = x |
| 177 |
t.Selection[0].Y = y |
| 178 |
t.Selection[1].X = x |
| 179 |
t.Selection[1].Y = y |
| 180 |
} |
| 181 |
|
| 182 |
t.mouseReleased = false |
| 183 |
} else if e.Buttons() == tcell.ButtonNone { |
| 184 |
if !t.mouseReleased { |
| 185 |
t.Selection[1].X = x |
| 186 |
t.Selection[1].Y = y |
| 187 |
} |
| 188 |
t.mouseReleased = true |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// HandleTermClose is called when a terminal has finished its job |
| 194 |
// and should be closed. If that terminal is this termpane's terminal, |
| 195 |
// HandleTermClose will close the terminal and the termpane itself. |
| 196 |
func (t *TermPane) HandleTermClose() { |
| 197 |
if t.Status == shell.TTClose { |
| 198 |
t.Quit() |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Exit closes the termpane |
| 203 |
func (t *TermPane) Exit() { |
| 204 |
t.Terminal.Close() |
| 205 |
t.Quit() |
| 206 |
} |
| 207 |
|
| 208 |
// CommandMode opens the termpane's command mode |
| 209 |
func (t *TermPane) CommandMode() { |
| 210 |
InfoBar.Prompt("> ", "", "TerminalCommand", nil, func(resp string, canceled bool) { |
| 211 |
if !canceled { |
| 212 |
t.HandleCommand(resp) |
| 213 |
} |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
// NextSplit moves to the next split |
| 218 |
func (t *TermPane) NextSplit() { |
| 219 |
a := t.tab.active |
| 220 |
if a < len(t.tab.Panes)-1 { |
| 221 |
a++ |
| 222 |
} else { |
| 223 |
a = 0 |
| 224 |
} |
| 225 |
|
| 226 |
t.tab.SetActive(a) |
| 227 |
} |
| 228 |
|
| 229 |
// HandleCommand handles a command for the term pane |
| 230 |
func (t *TermPane) HandleCommand(input string) { |
| 231 |
InfoBar.Error("Commands are unsupported in term for now") |
| 232 |
} |
| 233 |
|
| 234 |
// TermKeyActions contains the list of all possible key actions the termpane could execute |
| 235 |
var TermKeyActions = map[string]TermKeyAction{ |
| 236 |
"Exit": (*TermPane).Exit, |
| 237 |
"CommandMode": (*TermPane).CommandMode, |
| 238 |
"NextSplit": (*TermPane).NextSplit, |
| 239 |
} |