| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io/fs" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
"unicode" |
| 13 |
|
| 14 |
"github.com/micro-editor/json5" |
| 15 |
"github.com/micro-editor/tcell/v2" |
| 16 |
"github.com/zyedidia/micro/v2/internal/config" |
| 17 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 18 |
"github.com/zyedidia/micro/v2/internal/util" |
| 19 |
) |
| 20 |
|
| 21 |
var Binder = map[string]func(e Event, action string){ |
| 22 |
"command": InfoMapEvent, |
| 23 |
"buffer": BufMapEvent, |
| 24 |
"terminal": TermMapEvent, |
| 25 |
} |
| 26 |
|
| 27 |
func writeFile(name string, txt []byte) error { |
| 28 |
return util.SafeWrite(name, txt, false) |
| 29 |
} |
| 30 |
|
| 31 |
func createBindingsIfNotExist(fname string) { |
| 32 |
if _, e := os.Stat(fname); errors.Is(e, fs.ErrNotExist) { |
| 33 |
writeFile(fname, []byte("{}")) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// InitBindings intializes the bindings map by reading from bindings.json |
| 38 |
func InitBindings() { |
| 39 |
var parsed map[string]any |
| 40 |
|
| 41 |
filename := filepath.Join(config.ConfigDir, "bindings.json") |
| 42 |
createBindingsIfNotExist(filename) |
| 43 |
|
| 44 |
if _, e := os.Stat(filename); e == nil { |
| 45 |
input, err := os.ReadFile(filename) |
| 46 |
if err != nil { |
| 47 |
screen.TermMessage("Error reading bindings.json file: " + err.Error()) |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
err = json5.Unmarshal(input, &parsed) |
| 52 |
if err != nil { |
| 53 |
screen.TermMessage("Error reading bindings.json:", err.Error()) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
for p, bind := range Binder { |
| 58 |
defaults := DefaultBindings(p) |
| 59 |
|
| 60 |
for k, v := range defaults { |
| 61 |
BindKey(k, v, bind) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
for k, v := range parsed { |
| 66 |
switch val := v.(type) { |
| 67 |
case string: |
| 68 |
BindKey(k, val, Binder["buffer"]) |
| 69 |
case map[string]any: |
| 70 |
bind, ok := Binder[k] |
| 71 |
if !ok || bind == nil { |
| 72 |
screen.TermMessage(fmt.Sprintf("%s is not a valid pane type", k)) |
| 73 |
continue |
| 74 |
} |
| 75 |
for e, a := range val { |
| 76 |
s, ok := a.(string) |
| 77 |
if !ok { |
| 78 |
screen.TermMessage("Error reading bindings.json: non-string and non-map entry", k) |
| 79 |
} else { |
| 80 |
BindKey(e, s, bind) |
| 81 |
} |
| 82 |
} |
| 83 |
default: |
| 84 |
screen.TermMessage("Error reading bindings.json: non-string and non-map entry", k) |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func BindKey(k, v string, bind func(e Event, a string)) { |
| 90 |
event, err := findEvent(k) |
| 91 |
if err != nil { |
| 92 |
screen.TermMessage(err) |
| 93 |
return |
| 94 |
} |
| 95 |
|
| 96 |
if strings.HasPrefix(k, "\x1b") { |
| 97 |
screen.RegisterRawSeq(k) |
| 98 |
} |
| 99 |
|
| 100 |
bind(event, v) |
| 101 |
|
| 102 |
// switch e := event.(type) { |
| 103 |
// case KeyEvent: |
| 104 |
// InfoMapKey(e, v) |
| 105 |
// case KeySequenceEvent: |
| 106 |
// InfoMapKey(e, v) |
| 107 |
// case MouseEvent: |
| 108 |
// InfoMapMouse(e, v) |
| 109 |
// case RawEvent: |
| 110 |
// InfoMapKey(e, v) |
| 111 |
// } |
| 112 |
} |
| 113 |
|
| 114 |
var r = regexp.MustCompile("<(.+?)>") |
| 115 |
|
| 116 |
func findEvents(k string) (b KeySequenceEvent, ok bool, err error) { |
| 117 |
var events []Event = nil |
| 118 |
for len(k) > 0 { |
| 119 |
groups := r.FindStringSubmatchIndex(k) |
| 120 |
|
| 121 |
if len(groups) > 3 { |
| 122 |
if events == nil { |
| 123 |
events = make([]Event, 0, 3) |
| 124 |
} |
| 125 |
|
| 126 |
e, ok := findSingleEvent(k[groups[2]:groups[3]]) |
| 127 |
if !ok { |
| 128 |
return KeySequenceEvent{}, false, errors.New("Invalid event " + k[groups[2]:groups[3]]) |
| 129 |
} |
| 130 |
|
| 131 |
events = append(events, e) |
| 132 |
|
| 133 |
k = k[groups[3]+1:] |
| 134 |
} else { |
| 135 |
return KeySequenceEvent{}, false, nil |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return KeySequenceEvent{events}, true, nil |
| 140 |
} |
| 141 |
|
| 142 |
// findSingleEvent will find binding Key 'b' using string 'k' |
| 143 |
func findSingleEvent(k string) (b Event, ok bool) { |
| 144 |
modifiers := tcell.ModNone |
| 145 |
|
| 146 |
// First, we'll strip off all the modifiers in the name and add them to the |
| 147 |
// ModMask |
| 148 |
modSearch: |
| 149 |
for { |
| 150 |
switch { |
| 151 |
case strings.HasPrefix(k, "-") && k != "-": |
| 152 |
// We optionally support dashes between modifiers |
| 153 |
k = k[1:] |
| 154 |
case strings.HasPrefix(k, "Ctrl") && k != "CtrlH": |
| 155 |
// CtrlH technically does not have a 'Ctrl' modifier because it is really backspace |
| 156 |
k = k[4:] |
| 157 |
modifiers |= tcell.ModCtrl |
| 158 |
case strings.HasPrefix(k, "Alt"): |
| 159 |
k = k[3:] |
| 160 |
modifiers |= tcell.ModAlt |
| 161 |
case strings.HasPrefix(k, "Shift"): |
| 162 |
k = k[5:] |
| 163 |
modifiers |= tcell.ModShift |
| 164 |
case strings.HasPrefix(k, "\x1b"): |
| 165 |
return RawEvent{ |
| 166 |
esc: k, |
| 167 |
}, true |
| 168 |
default: |
| 169 |
break modSearch |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if k == "" { |
| 174 |
return KeyEvent{}, false |
| 175 |
} |
| 176 |
|
| 177 |
// Control is handled in a special way, since the terminal sends explicitly |
| 178 |
// marked escape sequences for control keys |
| 179 |
// We should check for Control keys first |
| 180 |
if modifiers&tcell.ModCtrl != 0 { |
| 181 |
// see if the key is in bindingKeys with the Ctrl prefix. |
| 182 |
k = string(unicode.ToUpper(rune(k[0]))) + k[1:] |
| 183 |
if code, ok := keyEvents["Ctrl"+k]; ok { |
| 184 |
return KeyEvent{ |
| 185 |
code: code, |
| 186 |
mod: modifiers, |
| 187 |
}, true |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// See if we can find the key in bindingKeys |
| 192 |
if code, ok := keyEvents[k]; ok { |
| 193 |
return KeyEvent{ |
| 194 |
code: code, |
| 195 |
mod: modifiers, |
| 196 |
}, true |
| 197 |
} |
| 198 |
|
| 199 |
var mstate MouseState = MousePress |
| 200 |
if strings.HasSuffix(k, "Drag") { |
| 201 |
k = k[:len(k)-4] |
| 202 |
mstate = MouseDrag |
| 203 |
} else if strings.HasSuffix(k, "Release") { |
| 204 |
k = k[:len(k)-7] |
| 205 |
mstate = MouseRelease |
| 206 |
} |
| 207 |
// See if we can find the key in bindingMouse |
| 208 |
if code, ok := mouseEvents[k]; ok { |
| 209 |
return MouseEvent{ |
| 210 |
btn: code, |
| 211 |
mod: modifiers, |
| 212 |
state: mstate, |
| 213 |
}, true |
| 214 |
} |
| 215 |
|
| 216 |
// If we were given one character, then we've got a rune. |
| 217 |
if len(k) == 1 { |
| 218 |
return KeyEvent{ |
| 219 |
code: tcell.KeyRune, |
| 220 |
mod: modifiers, |
| 221 |
r: rune(k[0]), |
| 222 |
}, true |
| 223 |
} |
| 224 |
|
| 225 |
// We don't know what happened. |
| 226 |
return KeyEvent{}, false |
| 227 |
} |
| 228 |
|
| 229 |
func findEvent(k string) (Event, error) { |
| 230 |
var event Event |
| 231 |
event, ok, err := findEvents(k) |
| 232 |
if err != nil { |
| 233 |
return nil, err |
| 234 |
} |
| 235 |
|
| 236 |
if !ok { |
| 237 |
event, ok = findSingleEvent(k) |
| 238 |
if !ok { |
| 239 |
return nil, errors.New(k + " is not a bindable event") |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return event, nil |
| 244 |
} |
| 245 |
|
| 246 |
func eventsEqual(e1 Event, e2 Event) bool { |
| 247 |
seq1, ok1 := e1.(KeySequenceEvent) |
| 248 |
seq2, ok2 := e2.(KeySequenceEvent) |
| 249 |
if ok1 && ok2 { |
| 250 |
if len(seq1.keys) != len(seq2.keys) { |
| 251 |
return false |
| 252 |
} |
| 253 |
for i := 0; i < len(seq1.keys); i++ { |
| 254 |
if seq1.keys[i] != seq2.keys[i] { |
| 255 |
return false |
| 256 |
} |
| 257 |
} |
| 258 |
return true |
| 259 |
} |
| 260 |
|
| 261 |
return e1 == e2 |
| 262 |
} |
| 263 |
|
| 264 |
// TryBindKeyPlug tries to bind a key for the plugin without writing to bindings.json. |
| 265 |
// This operation can be rejected by lockbindings to prevent unexpected actions by the user. |
| 266 |
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) { |
| 267 |
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) { |
| 268 |
return false, errors.New("bindings is locked by the user") |
| 269 |
} |
| 270 |
return TryBindKey(k, v, overwrite, false) |
| 271 |
} |
| 272 |
|
| 273 |
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json |
| 274 |
// Returns true if the keybinding already existed or is binded successfully and a possible error |
| 275 |
func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) { |
| 276 |
var e error |
| 277 |
var parsed map[string]any |
| 278 |
|
| 279 |
filename := filepath.Join(config.ConfigDir, "bindings.json") |
| 280 |
createBindingsIfNotExist(filename) |
| 281 |
if _, e = os.Stat(filename); e == nil { |
| 282 |
input, err := os.ReadFile(filename) |
| 283 |
if err != nil { |
| 284 |
return false, errors.New("Error reading bindings.json file: " + err.Error()) |
| 285 |
} |
| 286 |
|
| 287 |
err = json5.Unmarshal(input, &parsed) |
| 288 |
if err != nil { |
| 289 |
return false, errors.New("Error reading bindings.json: " + err.Error()) |
| 290 |
} |
| 291 |
|
| 292 |
key, err := findEvent(k) |
| 293 |
if err != nil { |
| 294 |
return false, err |
| 295 |
} |
| 296 |
|
| 297 |
found := false |
| 298 |
var ev string |
| 299 |
for ev = range parsed { |
| 300 |
if e, err := findEvent(ev); err == nil { |
| 301 |
if eventsEqual(e, key) { |
| 302 |
found = true |
| 303 |
break |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if found { |
| 309 |
if overwrite { |
| 310 |
parsed[ev] = v |
| 311 |
} else { |
| 312 |
return true, nil |
| 313 |
} |
| 314 |
} else { |
| 315 |
parsed[k] = v |
| 316 |
} |
| 317 |
|
| 318 |
BindKey(k, v, Binder["buffer"]) |
| 319 |
|
| 320 |
txt, _ := json.MarshalIndent(parsed, "", " ") |
| 321 |
txt = append(txt, '\n') |
| 322 |
|
| 323 |
if writeToFile { |
| 324 |
return true, writeFile(filename, txt) |
| 325 |
} else { |
| 326 |
return true, nil |
| 327 |
} |
| 328 |
} |
| 329 |
return false, e |
| 330 |
} |
| 331 |
|
| 332 |
// UnbindKey removes the binding for a key from the bindings.json file |
| 333 |
func UnbindKey(k string) error { |
| 334 |
var e error |
| 335 |
var parsed map[string]any |
| 336 |
|
| 337 |
filename := filepath.Join(config.ConfigDir, "bindings.json") |
| 338 |
createBindingsIfNotExist(filename) |
| 339 |
if _, e = os.Stat(filename); e == nil { |
| 340 |
input, err := os.ReadFile(filename) |
| 341 |
if err != nil { |
| 342 |
return errors.New("Error reading bindings.json file: " + err.Error()) |
| 343 |
} |
| 344 |
|
| 345 |
err = json5.Unmarshal(input, &parsed) |
| 346 |
if err != nil { |
| 347 |
return errors.New("Error reading bindings.json: " + err.Error()) |
| 348 |
} |
| 349 |
|
| 350 |
key, err := findEvent(k) |
| 351 |
if err != nil { |
| 352 |
return err |
| 353 |
} |
| 354 |
|
| 355 |
for ev := range parsed { |
| 356 |
if e, err := findEvent(ev); err == nil { |
| 357 |
if eventsEqual(e, key) { |
| 358 |
delete(parsed, ev) |
| 359 |
break |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
if strings.HasPrefix(k, "\x1b") { |
| 365 |
screen.UnregisterRawSeq(k) |
| 366 |
} |
| 367 |
|
| 368 |
defaults := DefaultBindings("buffer") |
| 369 |
if a, ok := defaults[k]; ok { |
| 370 |
BindKey(k, a, Binder["buffer"]) |
| 371 |
} else if _, ok := config.Bindings["buffer"][k]; ok { |
| 372 |
BufUnmap(key) |
| 373 |
delete(config.Bindings["buffer"], k) |
| 374 |
} |
| 375 |
|
| 376 |
txt, _ := json.MarshalIndent(parsed, "", " ") |
| 377 |
txt = append(txt, '\n') |
| 378 |
return writeFile(filename, txt) |
| 379 |
} |
| 380 |
return e |
| 381 |
} |
| 382 |
|
| 383 |
var mouseEvents = map[string]tcell.ButtonMask{ |
| 384 |
"MouseLeft": tcell.ButtonPrimary, |
| 385 |
"MouseMiddle": tcell.ButtonMiddle, |
| 386 |
"MouseRight": tcell.ButtonSecondary, |
| 387 |
"MouseWheelUp": tcell.WheelUp, |
| 388 |
"MouseWheelDown": tcell.WheelDown, |
| 389 |
"MouseWheelLeft": tcell.WheelLeft, |
| 390 |
"MouseWheelRight": tcell.WheelRight, |
| 391 |
} |
| 392 |
|
| 393 |
var keyEvents = map[string]tcell.Key{ |
| 394 |
"Up": tcell.KeyUp, |
| 395 |
"Down": tcell.KeyDown, |
| 396 |
"Right": tcell.KeyRight, |
| 397 |
"Left": tcell.KeyLeft, |
| 398 |
"UpLeft": tcell.KeyUpLeft, |
| 399 |
"UpRight": tcell.KeyUpRight, |
| 400 |
"DownLeft": tcell.KeyDownLeft, |
| 401 |
"DownRight": tcell.KeyDownRight, |
| 402 |
"Center": tcell.KeyCenter, |
| 403 |
"PageUp": tcell.KeyPgUp, |
| 404 |
"PageDown": tcell.KeyPgDn, |
| 405 |
"Home": tcell.KeyHome, |
| 406 |
"End": tcell.KeyEnd, |
| 407 |
"Insert": tcell.KeyInsert, |
| 408 |
"Delete": tcell.KeyDelete, |
| 409 |
"Help": tcell.KeyHelp, |
| 410 |
"Exit": tcell.KeyExit, |
| 411 |
"Clear": tcell.KeyClear, |
| 412 |
"Cancel": tcell.KeyCancel, |
| 413 |
"Print": tcell.KeyPrint, |
| 414 |
"Pause": tcell.KeyPause, |
| 415 |
"Backtab": tcell.KeyBacktab, |
| 416 |
"F1": tcell.KeyF1, |
| 417 |
"F2": tcell.KeyF2, |
| 418 |
"F3": tcell.KeyF3, |
| 419 |
"F4": tcell.KeyF4, |
| 420 |
"F5": tcell.KeyF5, |
| 421 |
"F6": tcell.KeyF6, |
| 422 |
"F7": tcell.KeyF7, |
| 423 |
"F8": tcell.KeyF8, |
| 424 |
"F9": tcell.KeyF9, |
| 425 |
"F10": tcell.KeyF10, |
| 426 |
"F11": tcell.KeyF11, |
| 427 |
"F12": tcell.KeyF12, |
| 428 |
"F13": tcell.KeyF13, |
| 429 |
"F14": tcell.KeyF14, |
| 430 |
"F15": tcell.KeyF15, |
| 431 |
"F16": tcell.KeyF16, |
| 432 |
"F17": tcell.KeyF17, |
| 433 |
"F18": tcell.KeyF18, |
| 434 |
"F19": tcell.KeyF19, |
| 435 |
"F20": tcell.KeyF20, |
| 436 |
"F21": tcell.KeyF21, |
| 437 |
"F22": tcell.KeyF22, |
| 438 |
"F23": tcell.KeyF23, |
| 439 |
"F24": tcell.KeyF24, |
| 440 |
"F25": tcell.KeyF25, |
| 441 |
"F26": tcell.KeyF26, |
| 442 |
"F27": tcell.KeyF27, |
| 443 |
"F28": tcell.KeyF28, |
| 444 |
"F29": tcell.KeyF29, |
| 445 |
"F30": tcell.KeyF30, |
| 446 |
"F31": tcell.KeyF31, |
| 447 |
"F32": tcell.KeyF32, |
| 448 |
"F33": tcell.KeyF33, |
| 449 |
"F34": tcell.KeyF34, |
| 450 |
"F35": tcell.KeyF35, |
| 451 |
"F36": tcell.KeyF36, |
| 452 |
"F37": tcell.KeyF37, |
| 453 |
"F38": tcell.KeyF38, |
| 454 |
"F39": tcell.KeyF39, |
| 455 |
"F40": tcell.KeyF40, |
| 456 |
"F41": tcell.KeyF41, |
| 457 |
"F42": tcell.KeyF42, |
| 458 |
"F43": tcell.KeyF43, |
| 459 |
"F44": tcell.KeyF44, |
| 460 |
"F45": tcell.KeyF45, |
| 461 |
"F46": tcell.KeyF46, |
| 462 |
"F47": tcell.KeyF47, |
| 463 |
"F48": tcell.KeyF48, |
| 464 |
"F49": tcell.KeyF49, |
| 465 |
"F50": tcell.KeyF50, |
| 466 |
"F51": tcell.KeyF51, |
| 467 |
"F52": tcell.KeyF52, |
| 468 |
"F53": tcell.KeyF53, |
| 469 |
"F54": tcell.KeyF54, |
| 470 |
"F55": tcell.KeyF55, |
| 471 |
"F56": tcell.KeyF56, |
| 472 |
"F57": tcell.KeyF57, |
| 473 |
"F58": tcell.KeyF58, |
| 474 |
"F59": tcell.KeyF59, |
| 475 |
"F60": tcell.KeyF60, |
| 476 |
"F61": tcell.KeyF61, |
| 477 |
"F62": tcell.KeyF62, |
| 478 |
"F63": tcell.KeyF63, |
| 479 |
"F64": tcell.KeyF64, |
| 480 |
"CtrlSpace": tcell.KeyCtrlSpace, |
| 481 |
"CtrlA": tcell.KeyCtrlA, |
| 482 |
"CtrlB": tcell.KeyCtrlB, |
| 483 |
"CtrlC": tcell.KeyCtrlC, |
| 484 |
"CtrlD": tcell.KeyCtrlD, |
| 485 |
"CtrlE": tcell.KeyCtrlE, |
| 486 |
"CtrlF": tcell.KeyCtrlF, |
| 487 |
"CtrlG": tcell.KeyCtrlG, |
| 488 |
"CtrlH": tcell.KeyCtrlH, |
| 489 |
"CtrlI": tcell.KeyCtrlI, |
| 490 |
"CtrlJ": tcell.KeyCtrlJ, |
| 491 |
"CtrlK": tcell.KeyCtrlK, |
| 492 |
"CtrlL": tcell.KeyCtrlL, |
| 493 |
"CtrlM": tcell.KeyCtrlM, |
| 494 |
"CtrlN": tcell.KeyCtrlN, |
| 495 |
"CtrlO": tcell.KeyCtrlO, |
| 496 |
"CtrlP": tcell.KeyCtrlP, |
| 497 |
"CtrlQ": tcell.KeyCtrlQ, |
| 498 |
"CtrlR": tcell.KeyCtrlR, |
| 499 |
"CtrlS": tcell.KeyCtrlS, |
| 500 |
"CtrlT": tcell.KeyCtrlT, |
| 501 |
"CtrlU": tcell.KeyCtrlU, |
| 502 |
"CtrlV": tcell.KeyCtrlV, |
| 503 |
"CtrlW": tcell.KeyCtrlW, |
| 504 |
"CtrlX": tcell.KeyCtrlX, |
| 505 |
"CtrlY": tcell.KeyCtrlY, |
| 506 |
"CtrlZ": tcell.KeyCtrlZ, |
| 507 |
"CtrlLeftSq": tcell.KeyCtrlLeftSq, |
| 508 |
"CtrlBackslash": tcell.KeyCtrlBackslash, |
| 509 |
"CtrlRightSq": tcell.KeyCtrlRightSq, |
| 510 |
"CtrlCarat": tcell.KeyCtrlCarat, |
| 511 |
"CtrlUnderscore": tcell.KeyCtrlUnderscore, |
| 512 |
"Tab": tcell.KeyTab, |
| 513 |
"Esc": tcell.KeyEsc, |
| 514 |
"Escape": tcell.KeyEscape, |
| 515 |
"Enter": tcell.KeyEnter, |
| 516 |
"Backspace": tcell.KeyBackspace2, |
| 517 |
"OldBackspace": tcell.KeyBackspace, |
| 518 |
|
| 519 |
// I renamed these keys to PageUp and PageDown but I don't want to break someone's keybindings |
| 520 |
"PgUp": tcell.KeyPgUp, |
| 521 |
"PgDown": tcell.KeyPgDn, |
| 522 |
} |