Action subpackage

This commit is contained in:
Zachary Yedidia
2018-08-27 19:53:08 -04:00
parent c3e2085e3c
commit d7b3f961b4
12 changed files with 539 additions and 520 deletions

500
cmd/micro/action/actions.go Normal file
View File

@@ -0,0 +1,500 @@
package action
import (
"os"
"github.com/zyedidia/micro/cmd/micro/screen"
"github.com/zyedidia/tcell"
)
// MousePress is the event that should happen when a normal click happens
// This is almost always bound to left click
func (a *BufHandler) MousePress(e *tcell.EventMouse) bool {
return false
}
// ScrollUpAction scrolls the view up
func (a *BufHandler) ScrollUpAction() bool {
return false
}
// ScrollDownAction scrolls the view up
func (a *BufHandler) ScrollDownAction() bool {
return false
}
// Center centers the view on the cursor
func (a *BufHandler) Center() bool {
return true
}
// CursorUp moves the cursor up
func (a *BufHandler) CursorUp() bool {
a.Cursor.Deselect(true)
a.Cursor.Up()
return true
}
// CursorDown moves the cursor down
func (a *BufHandler) CursorDown() bool {
a.Cursor.Deselect(true)
a.Cursor.Down()
return true
}
// CursorLeft moves the cursor left
func (a *BufHandler) CursorLeft() bool {
a.Cursor.Deselect(true)
a.Cursor.Left()
return true
}
// CursorRight moves the cursor right
func (a *BufHandler) CursorRight() bool {
a.Cursor.Deselect(true)
a.Cursor.Right()
return true
}
// WordRight moves the cursor one word to the right
func (a *BufHandler) WordRight() bool {
return true
}
// WordLeft moves the cursor one word to the left
func (a *BufHandler) WordLeft() bool {
return true
}
// SelectUp selects up one line
func (a *BufHandler) SelectUp() bool {
return true
}
// SelectDown selects down one line
func (a *BufHandler) SelectDown() bool {
return true
}
// SelectLeft selects the character to the left of the cursor
func (a *BufHandler) SelectLeft() bool {
return true
}
// SelectRight selects the character to the right of the cursor
func (a *BufHandler) SelectRight() bool {
return true
}
// SelectWordRight selects the word to the right of the cursor
func (a *BufHandler) SelectWordRight() bool {
return true
}
// SelectWordLeft selects the word to the left of the cursor
func (a *BufHandler) SelectWordLeft() bool {
return true
}
// StartOfLine moves the cursor to the start of the line
func (a *BufHandler) StartOfLine() bool {
return true
}
// EndOfLine moves the cursor to the end of the line
func (a *BufHandler) EndOfLine() bool {
return true
}
// SelectLine selects the entire current line
func (a *BufHandler) SelectLine() bool {
return true
}
// SelectToStartOfLine selects to the start of the current line
func (a *BufHandler) SelectToStartOfLine() bool {
return true
}
// SelectToEndOfLine selects to the end of the current line
func (a *BufHandler) SelectToEndOfLine() bool {
return true
}
// ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none
func (a *BufHandler) ParagraphPrevious() bool {
return true
}
// ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none
func (a *BufHandler) ParagraphNext() bool {
return true
}
// Retab changes all tabs to spaces or all spaces to tabs depending
// on the user's settings
func (a *BufHandler) Retab() bool {
return true
}
// CursorStart moves the cursor to the start of the buffer
func (a *BufHandler) CursorStart() bool {
return true
}
// CursorEnd moves the cursor to the end of the buffer
func (a *BufHandler) CursorEnd() bool {
return true
}
// SelectToStart selects the text from the cursor to the start of the buffer
func (a *BufHandler) SelectToStart() bool {
return true
}
// SelectToEnd selects the text from the cursor to the end of the buffer
func (a *BufHandler) SelectToEnd() bool {
return true
}
// InsertSpace inserts a space
func (a *BufHandler) InsertSpace() bool {
return true
}
// InsertNewline inserts a newline plus possible some whitespace if autoindent is on
func (a *BufHandler) InsertNewline() bool {
return true
}
// Backspace deletes the previous character
func (a *BufHandler) Backspace() bool {
return true
}
// DeleteWordRight deletes the word to the right of the cursor
func (a *BufHandler) DeleteWordRight() bool {
return true
}
// DeleteWordLeft deletes the word to the left of the cursor
func (a *BufHandler) DeleteWordLeft() bool {
return true
}
// Delete deletes the next character
func (a *BufHandler) Delete() bool {
return true
}
// IndentSelection indents the current selection
func (a *BufHandler) IndentSelection() bool {
return false
}
// OutdentLine moves the current line back one indentation
func (a *BufHandler) OutdentLine() bool {
return true
}
// OutdentSelection takes the current selection and moves it back one indent level
func (a *BufHandler) OutdentSelection() bool {
return false
}
// InsertTab inserts a tab or spaces
func (a *BufHandler) InsertTab() bool {
return true
}
// SaveAll saves all open buffers
func (a *BufHandler) SaveAll() bool {
return false
}
// Save the buffer to disk
func (a *BufHandler) Save() bool {
return false
}
// SaveAs saves the buffer to disk with the given name
func (a *BufHandler) SaveAs() bool {
return false
}
// Find opens a prompt and searches forward for the input
func (a *BufHandler) Find() bool {
return true
}
// FindNext searches forwards for the last used search term
func (a *BufHandler) FindNext() bool {
return true
}
// FindPrevious searches backwards for the last used search term
func (a *BufHandler) FindPrevious() bool {
return true
}
// Undo undoes the last action
func (a *BufHandler) Undo() bool {
return true
}
// Redo redoes the last action
func (a *BufHandler) Redo() bool {
return true
}
// Copy the selection to the system clipboard
func (a *BufHandler) Copy() bool {
return true
}
// CutLine cuts the current line to the clipboard
func (a *BufHandler) CutLine() bool {
return true
}
// Cut the selection to the system clipboard
func (a *BufHandler) Cut() bool {
return true
}
// DuplicateLine duplicates the current line or selection
func (a *BufHandler) DuplicateLine() bool {
return true
}
// DeleteLine deletes the current line
func (a *BufHandler) DeleteLine() bool {
return true
}
// MoveLinesUp moves up the current line or selected lines if any
func (a *BufHandler) MoveLinesUp() bool {
return true
}
// MoveLinesDown moves down the current line or selected lines if any
func (a *BufHandler) MoveLinesDown() bool {
return true
}
// Paste whatever is in the system clipboard into the buffer
// Delete and paste if the user has a selection
func (a *BufHandler) Paste() bool {
return true
}
// PastePrimary pastes from the primary clipboard (only use on linux)
func (a *BufHandler) PastePrimary() bool {
return true
}
// JumpToMatchingBrace moves the cursor to the matching brace if it is
// currently on a brace
func (a *BufHandler) JumpToMatchingBrace() bool {
return true
}
// SelectAll selects the entire buffer
func (a *BufHandler) SelectAll() bool {
return true
}
// OpenFile opens a new file in the buffer
func (a *BufHandler) OpenFile() bool {
return false
}
// Start moves the viewport to the start of the buffer
func (a *BufHandler) Start() bool {
return false
}
// End moves the viewport to the end of the buffer
func (a *BufHandler) End() bool {
return false
}
// PageUp scrolls the view up a page
func (a *BufHandler) PageUp() bool {
return false
}
// PageDown scrolls the view down a page
func (a *BufHandler) PageDown() bool {
return false
}
// SelectPageUp selects up one page
func (a *BufHandler) SelectPageUp() bool {
return true
}
// SelectPageDown selects down one page
func (a *BufHandler) SelectPageDown() bool {
return true
}
// CursorPageUp places the cursor a page up
func (a *BufHandler) CursorPageUp() bool {
return true
}
// CursorPageDown places the cursor a page up
func (a *BufHandler) CursorPageDown() bool {
return true
}
// HalfPageUp scrolls the view up half a page
func (a *BufHandler) HalfPageUp() bool {
return false
}
// HalfPageDown scrolls the view down half a page
func (a *BufHandler) HalfPageDown() bool {
return false
}
// ToggleRuler turns line numbers off and on
func (a *BufHandler) ToggleRuler() bool {
return false
}
// JumpLine jumps to a line and moves the view accordingly.
func (a *BufHandler) JumpLine() bool {
return false
}
// ClearStatus clears the messenger bar
func (a *BufHandler) ClearStatus() bool {
return false
}
// ToggleHelp toggles the help screen
func (a *BufHandler) ToggleHelp() bool {
return true
}
// ToggleKeyMenu toggles the keymenu option and resizes all tabs
func (a *BufHandler) ToggleKeyMenu() bool {
return true
}
// ShellMode opens a terminal to run a shell command
func (a *BufHandler) ShellMode() bool {
return false
}
// CommandMode lets the user enter a command
func (a *BufHandler) CommandMode() bool {
return false
}
// ToggleOverwriteMode lets the user toggle the text overwrite mode
func (a *BufHandler) ToggleOverwriteMode() bool {
return false
}
// Escape leaves current mode
func (a *BufHandler) Escape() bool {
return false
}
// Quit this will close the current tab or view that is open
func (a *BufHandler) Quit() bool {
screen.Screen.Fini()
os.Exit(0)
return false
}
// QuitAll quits the whole editor; all splits and tabs
func (a *BufHandler) QuitAll() bool {
return false
}
// AddTab adds a new tab with an empty buffer
func (a *BufHandler) AddTab() bool {
return true
}
// PreviousTab switches to the previous tab in the tab list
func (a *BufHandler) PreviousTab() bool {
return false
}
// NextTab switches to the next tab in the tab list
func (a *BufHandler) NextTab() bool {
return false
}
// VSplitBinding opens an empty vertical split
func (a *BufHandler) VSplitBinding() bool {
return false
}
// HSplitBinding opens an empty horizontal split
func (a *BufHandler) HSplitBinding() bool {
return false
}
// Unsplit closes all splits in the current tab except the active one
func (a *BufHandler) Unsplit() bool {
return false
}
// NextSplit changes the view to the next split
func (a *BufHandler) NextSplit() bool {
return false
}
// PreviousSplit changes the view to the previous split
func (a *BufHandler) PreviousSplit() bool {
return false
}
var curMacro []interface{}
var recordingMacro bool
// ToggleMacro toggles recording of a macro
func (a *BufHandler) ToggleMacro() bool {
return true
}
// PlayMacro plays back the most recently recorded macro
func (a *BufHandler) PlayMacro() bool {
return true
}
// SpawnMultiCursor creates a new multiple cursor at the next occurrence of the current selection or current word
func (a *BufHandler) SpawnMultiCursor() bool {
return false
}
// SpawnMultiCursorSelect adds a cursor at the beginning of each line of a selection
func (a *BufHandler) SpawnMultiCursorSelect() bool {
return false
}
// MouseMultiCursor is a mouse action which puts a new cursor at the mouse position
func (a *BufHandler) MouseMultiCursor(e *tcell.EventMouse) bool {
return false
}
// SkipMultiCursor moves the current multiple cursor to the next available position
func (a *BufHandler) SkipMultiCursor() bool {
return false
}
// RemoveMultiCursor removes the latest multiple cursor
func (a *BufHandler) RemoveMultiCursor() bool {
return false
}
// RemoveAllMultiCursors removes all cursors except the base cursor
func (a *BufHandler) RemoveAllMultiCursors() bool {
return false
}

View File

@@ -0,0 +1,8 @@
// +build plan9 nacl windows
package action
func (*BufHandler) Suspend() bool {
// TODO: error message
return false
}

View File

@@ -0,0 +1,35 @@
// +build linux darwin dragonfly solaris openbsd netbsd freebsd
package action
import (
"syscall"
"github.com/zyedidia/micro/cmd/micro/screen"
"github.com/zyedidia/micro/cmd/micro/util"
)
// Suspend sends micro to the background. This is the same as pressing CtrlZ in most unix programs.
// This only works on linux and has no default binding.
// This code was adapted from the suspend code in nsf/godit
func (*BufHandler) Suspend() bool {
screenWasNil := screen.Screen == nil
if !screenWasNil {
screen.Screen.Fini()
screen.Screen = nil
}
// suspend the process
pid := syscall.Getpid()
err := syscall.Kill(pid, syscall.SIGSTOP)
if err != nil {
util.TermMessage(err)
}
if !screenWasNil {
screen.Init()
}
return false
}

View File

@@ -0,0 +1,439 @@
package action
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"strings"
"unicode"
"github.com/flynn/json5"
"github.com/zyedidia/micro/cmd/micro/config"
"github.com/zyedidia/micro/cmd/micro/util"
"github.com/zyedidia/tcell"
)
var Bindings = DefaultBindings()
func InitBindings() {
var parsed map[string]string
defaults := DefaultBindings()
filename := config.ConfigDir + "/bindings.json"
if _, e := os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
if err != nil {
util.TermMessage("Error reading bindings.json file: " + err.Error())
return
}
err = json5.Unmarshal(input, &parsed)
if err != nil {
util.TermMessage("Error reading bindings.json:", err.Error())
}
}
for k, v := range defaults {
BindKey(k, v)
}
for k, v := range parsed {
BindKey(k, v)
}
}
func BindKey(k, v string) {
event, ok := findEvent(k)
if !ok {
util.TermMessage(k, "is not a bindable event")
}
switch e := event.(type) {
case KeyEvent:
BufMapKey(e, v)
case MouseEvent:
BufMapMouse(e, v)
case RawEvent:
util.TermMessage("Raw events not supported yet")
}
Bindings[k] = v
}
// findKeyEvent will find binding Key 'b' using string 'k'
func findEvent(k string) (b Event, ok bool) {
modifiers := tcell.ModNone
// First, we'll strip off all the modifiers in the name and add them to the
// ModMask
modSearch:
for {
switch {
case strings.HasPrefix(k, "-"):
// We optionally support dashes between modifiers
k = k[1:]
case strings.HasPrefix(k, "Ctrl") && k != "CtrlH":
// CtrlH technically does not have a 'Ctrl' modifier because it is really backspace
k = k[4:]
modifiers |= tcell.ModCtrl
case strings.HasPrefix(k, "Alt"):
k = k[3:]
modifiers |= tcell.ModAlt
case strings.HasPrefix(k, "Shift"):
k = k[5:]
modifiers |= tcell.ModShift
case strings.HasPrefix(k, "\x1b"):
return RawEvent{
esc: k,
}, true
default:
break modSearch
}
}
if len(k) == 0 {
return KeyEvent{}, false
}
// Control is handled in a special way, since the terminal sends explicitly
// marked escape sequences for control keys
// We should check for Control keys first
if modifiers&tcell.ModCtrl != 0 {
// see if the key is in bindingKeys with the Ctrl prefix.
k = string(unicode.ToUpper(rune(k[0]))) + k[1:]
if code, ok := keyEvents["Ctrl"+k]; ok {
// It is, we're done.
return KeyEvent{
code: code,
mod: modifiers,
r: rune(code),
}, true
}
}
// See if we can find the key in bindingKeys
if code, ok := keyEvents[k]; ok {
return KeyEvent{
code: code,
mod: modifiers,
r: 0,
}, true
}
// See if we can find the key in bindingMouse
if code, ok := mouseEvents[k]; ok {
return MouseEvent{
btn: code,
mod: modifiers,
}, true
}
// If we were given one character, then we've got a rune.
if len(k) == 1 {
return KeyEvent{
code: tcell.KeyRune,
mod: modifiers,
r: rune(k[0]),
}, true
}
// We don't know what happened.
return KeyEvent{}, false
}
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
// Returns true if the keybinding already existed and a possible error
func TryBindKey(k, v string, overwrite bool) (bool, error) {
var e error
var parsed map[string]string
filename := config.ConfigDir + "/bindings.json"
if _, e = os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
if err != nil {
return false, errors.New("Error reading bindings.json file: " + err.Error())
}
err = json5.Unmarshal(input, &parsed)
if err != nil {
return false, errors.New("Error reading bindings.json: " + err.Error())
}
key, ok := findEvent(k)
if !ok {
return false, errors.New("Invalid event " + k)
}
found := false
for ev := range parsed {
if e, ok := findEvent(ev); ok {
if e == key {
if overwrite {
parsed[ev] = v
}
found = true
break
}
}
}
if found && !overwrite {
return true, nil
} else if !found {
parsed[k] = v
}
BindKey(k, v)
txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
}
return false, e
}
var mouseEvents = map[string]tcell.ButtonMask{
"MouseLeft": tcell.Button1,
"MouseMiddle": tcell.Button2,
"MouseRight": tcell.Button3,
"MouseWheelUp": tcell.WheelUp,
"MouseWheelDown": tcell.WheelDown,
"MouseWheelLeft": tcell.WheelLeft,
"MouseWheelRight": tcell.WheelRight,
}
var keyEvents = map[string]tcell.Key{
"Up": tcell.KeyUp,
"Down": tcell.KeyDown,
"Right": tcell.KeyRight,
"Left": tcell.KeyLeft,
"UpLeft": tcell.KeyUpLeft,
"UpRight": tcell.KeyUpRight,
"DownLeft": tcell.KeyDownLeft,
"DownRight": tcell.KeyDownRight,
"Center": tcell.KeyCenter,
"PageUp": tcell.KeyPgUp,
"PageDown": tcell.KeyPgDn,
"Home": tcell.KeyHome,
"End": tcell.KeyEnd,
"Insert": tcell.KeyInsert,
"Delete": tcell.KeyDelete,
"Help": tcell.KeyHelp,
"Exit": tcell.KeyExit,
"Clear": tcell.KeyClear,
"Cancel": tcell.KeyCancel,
"Print": tcell.KeyPrint,
"Pause": tcell.KeyPause,
"Backtab": tcell.KeyBacktab,
"F1": tcell.KeyF1,
"F2": tcell.KeyF2,
"F3": tcell.KeyF3,
"F4": tcell.KeyF4,
"F5": tcell.KeyF5,
"F6": tcell.KeyF6,
"F7": tcell.KeyF7,
"F8": tcell.KeyF8,
"F9": tcell.KeyF9,
"F10": tcell.KeyF10,
"F11": tcell.KeyF11,
"F12": tcell.KeyF12,
"F13": tcell.KeyF13,
"F14": tcell.KeyF14,
"F15": tcell.KeyF15,
"F16": tcell.KeyF16,
"F17": tcell.KeyF17,
"F18": tcell.KeyF18,
"F19": tcell.KeyF19,
"F20": tcell.KeyF20,
"F21": tcell.KeyF21,
"F22": tcell.KeyF22,
"F23": tcell.KeyF23,
"F24": tcell.KeyF24,
"F25": tcell.KeyF25,
"F26": tcell.KeyF26,
"F27": tcell.KeyF27,
"F28": tcell.KeyF28,
"F29": tcell.KeyF29,
"F30": tcell.KeyF30,
"F31": tcell.KeyF31,
"F32": tcell.KeyF32,
"F33": tcell.KeyF33,
"F34": tcell.KeyF34,
"F35": tcell.KeyF35,
"F36": tcell.KeyF36,
"F37": tcell.KeyF37,
"F38": tcell.KeyF38,
"F39": tcell.KeyF39,
"F40": tcell.KeyF40,
"F41": tcell.KeyF41,
"F42": tcell.KeyF42,
"F43": tcell.KeyF43,
"F44": tcell.KeyF44,
"F45": tcell.KeyF45,
"F46": tcell.KeyF46,
"F47": tcell.KeyF47,
"F48": tcell.KeyF48,
"F49": tcell.KeyF49,
"F50": tcell.KeyF50,
"F51": tcell.KeyF51,
"F52": tcell.KeyF52,
"F53": tcell.KeyF53,
"F54": tcell.KeyF54,
"F55": tcell.KeyF55,
"F56": tcell.KeyF56,
"F57": tcell.KeyF57,
"F58": tcell.KeyF58,
"F59": tcell.KeyF59,
"F60": tcell.KeyF60,
"F61": tcell.KeyF61,
"F62": tcell.KeyF62,
"F63": tcell.KeyF63,
"F64": tcell.KeyF64,
"CtrlSpace": tcell.KeyCtrlSpace,
"CtrlA": tcell.KeyCtrlA,
"CtrlB": tcell.KeyCtrlB,
"CtrlC": tcell.KeyCtrlC,
"CtrlD": tcell.KeyCtrlD,
"CtrlE": tcell.KeyCtrlE,
"CtrlF": tcell.KeyCtrlF,
"CtrlG": tcell.KeyCtrlG,
"CtrlH": tcell.KeyCtrlH,
"CtrlI": tcell.KeyCtrlI,
"CtrlJ": tcell.KeyCtrlJ,
"CtrlK": tcell.KeyCtrlK,
"CtrlL": tcell.KeyCtrlL,
"CtrlM": tcell.KeyCtrlM,
"CtrlN": tcell.KeyCtrlN,
"CtrlO": tcell.KeyCtrlO,
"CtrlP": tcell.KeyCtrlP,
"CtrlQ": tcell.KeyCtrlQ,
"CtrlR": tcell.KeyCtrlR,
"CtrlS": tcell.KeyCtrlS,
"CtrlT": tcell.KeyCtrlT,
"CtrlU": tcell.KeyCtrlU,
"CtrlV": tcell.KeyCtrlV,
"CtrlW": tcell.KeyCtrlW,
"CtrlX": tcell.KeyCtrlX,
"CtrlY": tcell.KeyCtrlY,
"CtrlZ": tcell.KeyCtrlZ,
"CtrlLeftSq": tcell.KeyCtrlLeftSq,
"CtrlBackslash": tcell.KeyCtrlBackslash,
"CtrlRightSq": tcell.KeyCtrlRightSq,
"CtrlCarat": tcell.KeyCtrlCarat,
"CtrlUnderscore": tcell.KeyCtrlUnderscore,
"CtrlPageUp": tcell.KeyCtrlPgUp,
"CtrlPageDown": tcell.KeyCtrlPgDn,
"Tab": tcell.KeyTab,
"Esc": tcell.KeyEsc,
"Escape": tcell.KeyEscape,
"Enter": tcell.KeyEnter,
"Backspace": tcell.KeyBackspace2,
"OldBackspace": tcell.KeyBackspace,
// I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings
"PgUp": tcell.KeyPgUp,
"PgDown": tcell.KeyPgDn,
}
// DefaultBindings returns a map containing micro's default keybindings
func DefaultBindings() map[string]string {
return map[string]string{
"Up": "CursorUp",
"Down": "CursorDown",
"Right": "CursorRight",
"Left": "CursorLeft",
"ShiftUp": "SelectUp",
"ShiftDown": "SelectDown",
"ShiftLeft": "SelectLeft",
"ShiftRight": "SelectRight",
"AltLeft": "WordLeft",
"AltRight": "WordRight",
"AltUp": "MoveLinesUp",
"AltDown": "MoveLinesDown",
"AltShiftRight": "SelectWordRight",
"AltShiftLeft": "SelectWordLeft",
"CtrlLeft": "StartOfLine",
"CtrlRight": "EndOfLine",
"CtrlShiftLeft": "SelectToStartOfLine",
"ShiftHome": "SelectToStartOfLine",
"CtrlShiftRight": "SelectToEndOfLine",
"ShiftEnd": "SelectToEndOfLine",
"CtrlUp": "CursorStart",
"CtrlDown": "CursorEnd",
"CtrlShiftUp": "SelectToStart",
"CtrlShiftDown": "SelectToEnd",
"Alt-{": "ParagraphPrevious",
"Alt-}": "ParagraphNext",
"Enter": "InsertNewline",
"CtrlH": "Backspace",
"Backspace": "Backspace",
"Alt-CtrlH": "DeleteWordLeft",
"Alt-Backspace": "DeleteWordLeft",
"Tab": "IndentSelection,InsertTab",
"Backtab": "OutdentSelection,OutdentLine",
"CtrlO": "OpenFile",
"CtrlS": "Save",
"CtrlF": "Find",
"CtrlN": "FindNext",
"CtrlP": "FindPrevious",
"CtrlZ": "Undo",
"CtrlY": "Redo",
"CtrlC": "Copy",
"CtrlX": "Cut",
"CtrlK": "CutLine",
"CtrlD": "DuplicateLine",
"CtrlV": "Paste",
"CtrlA": "SelectAll",
"CtrlT": "AddTab",
"Alt,": "PreviousTab",
"Alt.": "NextTab",
"Home": "StartOfLine",
"End": "EndOfLine",
"CtrlHome": "CursorStart",
"CtrlEnd": "CursorEnd",
"PageUp": "CursorPageUp",
"PageDown": "CursorPageDown",
"CtrlPageUp": "PreviousTab",
"CtrlPageDown": "NextTab",
"CtrlG": "ToggleHelp",
"Alt-g": "ToggleKeyMenu",
"CtrlR": "ToggleRuler",
"CtrlL": "JumpLine",
"Delete": "Delete",
"CtrlB": "ShellMode",
"CtrlQ": "Quit",
"CtrlE": "CommandMode",
"CtrlW": "NextSplit",
"CtrlU": "ToggleMacro",
"CtrlJ": "PlayMacro",
"Insert": "ToggleOverwriteMode",
// Emacs-style keybindings
"Alt-f": "WordRight",
"Alt-b": "WordLeft",
"Alt-a": "StartOfLine",
"Alt-e": "EndOfLine",
// "Alt-p": "CursorUp",
// "Alt-n": "CursorDown",
// Integration with file managers
"F2": "Save",
"F3": "Find",
"F4": "Quit",
"F7": "Find",
"F10": "Quit",
"Esc": "Escape",
// Mouse bindings
"MouseWheelUp": "ScrollUp",
"MouseWheelDown": "ScrollDown",
"MouseLeft": "MousePress",
"MouseMiddle": "PastePrimary",
"Ctrl-MouseLeft": "MouseMultiCursor",
"Alt-n": "SpawnMultiCursor",
"Alt-m": "SpawnMultiCursorSelect",
"Alt-p": "RemoveMultiCursor",
"Alt-c": "RemoveAllMultiCursors",
"Alt-x": "SkipMultiCursor",
}
}

View File

@@ -0,0 +1,206 @@
package action
import (
"time"
"github.com/zyedidia/micro/cmd/micro/buffer"
"github.com/zyedidia/tcell"
)
type BufKeyAction func(*BufHandler) bool
type BufMouseAction func(*BufHandler, *tcell.EventMouse) bool
var BufKeyBindings map[KeyEvent]BufKeyAction
var BufMouseBindings map[MouseEvent]BufMouseAction
func init() {
BufKeyBindings = make(map[KeyEvent]BufKeyAction)
BufMouseBindings = make(map[MouseEvent]BufMouseAction)
}
func BufMapKey(k KeyEvent, action string) {
BufKeyBindings[k] = BufKeyActions[action]
}
func BufMapMouse(k MouseEvent, action string) {
BufMouseBindings[k] = BufMouseActions[action]
}
// The BufHandler connects the buffer and the window
// It provides a cursor (or multiple) and defines a set of actions
// that can be taken on the buffer
// The ActionHandler can access the window for necessary info about
// visual positions for mouse clicks and scrolling
type BufHandler struct {
Buf *buffer.Buffer
cursors []*buffer.Cursor
Cursor *buffer.Cursor // the active cursor
// Since tcell doesn't differentiate between a mouse release event
// and a mouse move event with no keys pressed, we need to keep
// track of whether or not the mouse was pressed (or not released) last event to determine
// mouse release events
mouseReleased bool
// We need to keep track of insert key press toggle
isOverwriteMode bool
// This stores when the last click was
// This is useful for detecting double and triple clicks
lastClickTime time.Time
lastLoc buffer.Loc
// lastCutTime stores when the last ctrl+k was issued.
// It is used for clearing the clipboard to replace it with fresh cut lines.
lastCutTime time.Time
// freshClip returns true if the clipboard has never been pasted.
freshClip bool
// Was the last mouse event actually a double click?
// Useful for detecting triple clicks -- if a double click is detected
// but the last mouse event was actually a double click, it's a triple click
doubleClick bool
// Same here, just to keep track for mouse move events
tripleClick bool
}
func NewBufHandler(buf *buffer.Buffer) *BufHandler {
a := new(BufHandler)
a.Buf = buf
a.cursors = []*buffer.Cursor{&buffer.Cursor{
Buf: buf,
Loc: buf.StartCursor,
}}
a.Cursor = a.cursors[0]
buf.SetCursors(a.cursors)
return a
}
// HandleEvent executes the tcell event properly
// TODO: multiple actions bound to one key
func (a *BufHandler) HandleEvent(event tcell.Event) {
switch e := event.(type) {
case *tcell.EventKey:
ke := KeyEvent{
code: e.Key(),
mod: e.Modifiers(),
r: e.Rune(),
}
if action, ok := BufKeyBindings[ke]; ok {
action(a)
}
case *tcell.EventMouse:
me := MouseEvent{
btn: e.Buttons(),
mod: e.Modifiers(),
}
if action, ok := BufMouseBindings[me]; ok {
action(a, e)
}
}
}
var BufKeyActions = map[string]BufKeyAction{
"CursorUp": (*BufHandler).CursorUp,
"CursorDown": (*BufHandler).CursorDown,
"CursorPageUp": (*BufHandler).CursorPageUp,
"CursorPageDown": (*BufHandler).CursorPageDown,
"CursorLeft": (*BufHandler).CursorLeft,
"CursorRight": (*BufHandler).CursorRight,
"CursorStart": (*BufHandler).CursorStart,
"CursorEnd": (*BufHandler).CursorEnd,
"SelectToStart": (*BufHandler).SelectToStart,
"SelectToEnd": (*BufHandler).SelectToEnd,
"SelectUp": (*BufHandler).SelectUp,
"SelectDown": (*BufHandler).SelectDown,
"SelectLeft": (*BufHandler).SelectLeft,
"SelectRight": (*BufHandler).SelectRight,
"WordRight": (*BufHandler).WordRight,
"WordLeft": (*BufHandler).WordLeft,
"SelectWordRight": (*BufHandler).SelectWordRight,
"SelectWordLeft": (*BufHandler).SelectWordLeft,
"DeleteWordRight": (*BufHandler).DeleteWordRight,
"DeleteWordLeft": (*BufHandler).DeleteWordLeft,
"SelectLine": (*BufHandler).SelectLine,
"SelectToStartOfLine": (*BufHandler).SelectToStartOfLine,
"SelectToEndOfLine": (*BufHandler).SelectToEndOfLine,
"ParagraphPrevious": (*BufHandler).ParagraphPrevious,
"ParagraphNext": (*BufHandler).ParagraphNext,
"InsertNewline": (*BufHandler).InsertNewline,
"InsertSpace": (*BufHandler).InsertSpace,
"Backspace": (*BufHandler).Backspace,
"Delete": (*BufHandler).Delete,
"InsertTab": (*BufHandler).InsertTab,
"Save": (*BufHandler).Save,
"SaveAll": (*BufHandler).SaveAll,
"SaveAs": (*BufHandler).SaveAs,
"Find": (*BufHandler).Find,
"FindNext": (*BufHandler).FindNext,
"FindPrevious": (*BufHandler).FindPrevious,
"Center": (*BufHandler).Center,
"Undo": (*BufHandler).Undo,
"Redo": (*BufHandler).Redo,
"Copy": (*BufHandler).Copy,
"Cut": (*BufHandler).Cut,
"CutLine": (*BufHandler).CutLine,
"DuplicateLine": (*BufHandler).DuplicateLine,
"DeleteLine": (*BufHandler).DeleteLine,
"MoveLinesUp": (*BufHandler).MoveLinesUp,
"MoveLinesDown": (*BufHandler).MoveLinesDown,
"IndentSelection": (*BufHandler).IndentSelection,
"OutdentSelection": (*BufHandler).OutdentSelection,
"OutdentLine": (*BufHandler).OutdentLine,
"Paste": (*BufHandler).Paste,
"PastePrimary": (*BufHandler).PastePrimary,
"SelectAll": (*BufHandler).SelectAll,
"OpenFile": (*BufHandler).OpenFile,
"Start": (*BufHandler).Start,
"End": (*BufHandler).End,
"PageUp": (*BufHandler).PageUp,
"PageDown": (*BufHandler).PageDown,
"SelectPageUp": (*BufHandler).SelectPageUp,
"SelectPageDown": (*BufHandler).SelectPageDown,
"HalfPageUp": (*BufHandler).HalfPageUp,
"HalfPageDown": (*BufHandler).HalfPageDown,
"StartOfLine": (*BufHandler).StartOfLine,
"EndOfLine": (*BufHandler).EndOfLine,
"ToggleHelp": (*BufHandler).ToggleHelp,
"ToggleKeyMenu": (*BufHandler).ToggleKeyMenu,
"ToggleRuler": (*BufHandler).ToggleRuler,
"JumpLine": (*BufHandler).JumpLine,
"ClearStatus": (*BufHandler).ClearStatus,
"ShellMode": (*BufHandler).ShellMode,
"CommandMode": (*BufHandler).CommandMode,
"ToggleOverwriteMode": (*BufHandler).ToggleOverwriteMode,
"Escape": (*BufHandler).Escape,
"Quit": (*BufHandler).Quit,
"QuitAll": (*BufHandler).QuitAll,
"AddTab": (*BufHandler).AddTab,
"PreviousTab": (*BufHandler).PreviousTab,
"NextTab": (*BufHandler).NextTab,
"NextSplit": (*BufHandler).NextSplit,
"PreviousSplit": (*BufHandler).PreviousSplit,
"Unsplit": (*BufHandler).Unsplit,
"VSplit": (*BufHandler).VSplitBinding,
"HSplit": (*BufHandler).HSplitBinding,
"ToggleMacro": (*BufHandler).ToggleMacro,
"PlayMacro": (*BufHandler).PlayMacro,
"Suspend": (*BufHandler).Suspend,
"ScrollUp": (*BufHandler).ScrollUpAction,
"ScrollDown": (*BufHandler).ScrollDownAction,
"SpawnMultiCursor": (*BufHandler).SpawnMultiCursor,
"SpawnMultiCursorSelect": (*BufHandler).SpawnMultiCursorSelect,
"RemoveMultiCursor": (*BufHandler).RemoveMultiCursor,
"RemoveAllMultiCursors": (*BufHandler).RemoveAllMultiCursors,
"SkipMultiCursor": (*BufHandler).SkipMultiCursor,
"JumpToMatchingBrace": (*BufHandler).JumpToMatchingBrace,
// This was changed to InsertNewline but I don't want to break backwards compatibility
"InsertEnter": (*BufHandler).InsertNewline,
}
var BufMouseActions = map[string]BufMouseAction{
"MousePress": (*BufHandler).MousePress,
"MouseMultiCursor": (*BufHandler).MouseMultiCursor,
}

View File

@@ -0,0 +1,38 @@
package action
import (
"github.com/zyedidia/tcell"
)
type Event interface{}
// RawEvent is simply an escape code
// We allow users to directly bind escape codes
// to get around some of a limitations of terminals
type RawEvent struct {
esc string
}
// KeyEvent is a key event containing a key code,
// some possible modifiers (alt, ctrl, etc...) and
// a rune if it was simply a character press
// Note: to be compatible with tcell events,
// for ctrl keys r=code
type KeyEvent struct {
code tcell.Key
mod tcell.ModMask
r rune
}
// MouseEvent is a mouse event with a mouse button and
// any possible key modifiers
type MouseEvent struct {
btn tcell.ButtonMask
mod tcell.ModMask
}
// A Handler will take a tcell event and execute it
// appropriately
type Handler interface {
HandleEvent(tcell.Event)
}