...

Source file src/github.com/c-bata/go-prompt/emacs.go

Documentation: github.com/c-bata/go-prompt

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

View as plain text