emacs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package prompt
  2. import "github.com/c-bata/go-prompt/internal/debug"
  3. /*
  4. ========
  5. PROGRESS
  6. ========
  7. Moving the cursor
  8. -----------------
  9. * [x] Ctrl + a Go to the beginning of the line (Home)
  10. * [x] Ctrl + e Go to the End of the line (End)
  11. * [x] Ctrl + p Previous command (Up arrow)
  12. * [x] Ctrl + n Next command (Down arrow)
  13. * [x] Ctrl + f Forward one character
  14. * [x] Ctrl + b Backward one character
  15. * [x] Ctrl + xx Toggle between the start of line and current cursor position
  16. Editing
  17. -------
  18. * [x] Ctrl + L Clear the Screen, similar to the clear command
  19. * [x] Ctrl + d Delete character under the cursor
  20. * [x] Ctrl + h Delete character before the cursor (Backspace)
  21. * [x] Ctrl + w Cut the Word before the cursor to the clipboard.
  22. * [x] Ctrl + k Cut the Line after the cursor to the clipboard.
  23. * [x] Ctrl + u Cut/delete the Line before the cursor to the clipboard.
  24. * [ ] Ctrl + t Swap the last two characters before the cursor (typo).
  25. * [ ] Esc + t Swap the last two words before the cursor.
  26. * [ ] ctrl + y Paste the last thing to be cut (yank)
  27. * [ ] ctrl + _ Undo
  28. */
  29. var emacsKeyBindings = []KeyBind{
  30. // Go to the End of the line
  31. {
  32. Key: ControlE,
  33. Fn: func(buf *Buffer) {
  34. x := []rune(buf.Document().TextAfterCursor())
  35. buf.CursorRight(len(x))
  36. },
  37. },
  38. // Go to the beginning of the line
  39. {
  40. Key: ControlA,
  41. Fn: func(buf *Buffer) {
  42. x := []rune(buf.Document().TextBeforeCursor())
  43. buf.CursorLeft(len(x))
  44. },
  45. },
  46. // Cut the Line after the cursor
  47. {
  48. Key: ControlK,
  49. Fn: func(buf *Buffer) {
  50. x := []rune(buf.Document().TextAfterCursor())
  51. buf.Delete(len(x))
  52. },
  53. },
  54. // Cut/delete the Line before the cursor
  55. {
  56. Key: ControlU,
  57. Fn: func(buf *Buffer) {
  58. x := []rune(buf.Document().TextBeforeCursor())
  59. buf.DeleteBeforeCursor(len(x))
  60. },
  61. },
  62. // Delete character under the cursor
  63. {
  64. Key: ControlD,
  65. Fn: func(buf *Buffer) {
  66. if buf.Text() != "" {
  67. buf.Delete(1)
  68. }
  69. },
  70. },
  71. // Backspace
  72. {
  73. Key: ControlH,
  74. Fn: func(buf *Buffer) {
  75. buf.DeleteBeforeCursor(1)
  76. },
  77. },
  78. // Right allow: Forward one character
  79. {
  80. Key: ControlF,
  81. Fn: func(buf *Buffer) {
  82. buf.CursorRight(1)
  83. },
  84. },
  85. // Left allow: Backward one character
  86. {
  87. Key: ControlB,
  88. Fn: func(buf *Buffer) {
  89. buf.CursorLeft(1)
  90. },
  91. },
  92. // Cut the Word before the cursor.
  93. {
  94. Key: ControlW,
  95. Fn: func(buf *Buffer) {
  96. buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursorWithSpace())))
  97. },
  98. },
  99. // Clear the Screen, similar to the clear command
  100. {
  101. Key: ControlL,
  102. Fn: func(buf *Buffer) {
  103. consoleWriter.EraseScreen()
  104. consoleWriter.CursorGoTo(0, 0)
  105. debug.AssertNoError(consoleWriter.Flush())
  106. },
  107. },
  108. }