Skip to content

Commit

Permalink
Enable emacs-style editing in prompt window
Browse files Browse the repository at this point in the history
M  README.md
M  autoload/scope/popup.vim
  • Loading branch information
girishji committed Aug 6, 2024
1 parent a4f2dd4 commit 07845ea
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,41 @@ Mapping | Action
`<Right>` | Cursor one character right
`<C-e>/<End>` | Move cursor to the end of line
`<C-b>/<Home>` | Move cursor to the beginning of line
`<C-u>` | Delete characters between cursor and beginning of line
`<C-w>` | Delete word before the cursor
`<S-Left>/<C-Left>` | Cursor one WORD left
`<S-Right>/<C-Right>` | Cursor one WORD right
`<C-u>` | Delete characters between cursor and beginning of line
`<C-w>` | Delete word before the cursor
`<C-Up>/<S-Up>` | Recall history previous
`<C-Down>/<S-Down>` | Recall history next
`<C-r><C-w>` | Insert word under cursor (`<cword>`) into prompt
`<C-r><C-a>` | Insert WORD under cursor (`<cWORD>`) into prompt
`<C-r><C-l>` | Insert line under cursor into prompt
`<C-r>` {register} | Insert the contents of a numbered or named register. Between typing CTRL-R and the second character '"' will be displayed to indicate that you are expected to enter the name of a register.

To enable emacs-style editing in the prompt window, set the option `emacsKeys` to `true` as follows:

```vim
scope#popup#OptionsSet({emacsKeys: true})
```

or,

```vim
import autoload 'scope/popup.vim' as sp
sp.OptionsSet({emacsKeys: true})
```

When emacs-style editing is enabled, following keybinding take effect:

Mapping | Action
--------|-------
`<C-b>/<Left>` | Cursor one character left
`<C-f>/<Right>` | Cursor one character right
`<C-e>/<End>` | Move cursor to the end of line
`<C-a>/<Home>` | Move cursor to the beginning of line
`<A-b>/<S-Left>/<C-Left>` | Cursor one WORD left
`<A-f>/<S-Right>/<C-Right>` | Cursor one WORD right

# Requirements

- Vim version 9.1 or higher
Expand Down
13 changes: 7 additions & 6 deletions autoload/scope/popup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export var options = {
maxwidth: -1,
promptchar: '>',
# cursorchar: '',
emacsKeys: false,
}

export def OptionsSet(opt: dict<any>)
Expand Down Expand Up @@ -159,7 +160,7 @@ export class FilterMenu
# close the popup window for <cr> when popup window is empty
id->popup_close(-1)
endif
elseif key == "\<Right>" || key == "\<PageDown>"
elseif key == "\<Right>" || key == "\<PageDown>" || (options.emacsKeys && key == "\<C-f>")
if this.idp->getmatches()->indexof((_, v) => v.group == 'ScopeMenuVirtualText') != -1
# virtual text present. grep using virtual text.
this.prompt = this.idp->getwininfo()[0].bufnr->getbufline(1)[0]->slice(2)
Expand All @@ -168,7 +169,7 @@ export class FilterMenu
[this.items_dict, this.filtered_items] = GetFilteredItemsFn(this.items_dict, this.prompt)
this._SetPopupContent()
else
if key == "\<Right>"
if key == "\<Right>" || (options.emacsKeys && key == "\<C-f>")
if this.cursorpos < (3 + this.prompt->strcharlen())
this.cursorpos = this.cursorpos + 1
this._CursorSet()
Expand All @@ -177,12 +178,12 @@ export class FilterMenu
win_execute(id, 'normal! ' .. "\<C-d>")
endif
endif
elseif key == "\<Left>"
elseif key == "\<Left>" || (options.emacsKeys && key == "\<C-b>")
if this.cursorpos > 3
this.cursorpos = this.cursorpos - 1
this._CursorSet()
endif
elseif key == "\<C-Right>" || key == "\<S-Right>"
elseif key == "\<C-Right>" || key == "\<S-Right>" || (options.emacsKeys && key == "\<A-f>")
if this.cursorpos < (3 + this.prompt->strcharlen())
var pos = this.cursorpos - 3
var byteidx = this.prompt->stridx(' ', this.prompt->byteidx(pos) + 1)
Expand All @@ -193,7 +194,7 @@ export class FilterMenu
endif
this._CursorSet()
endif
elseif key == "\<C-Left>" || key == "\<S-Left>"
elseif key == "\<C-Left>" || key == "\<S-Left>" || (options.emacsKeys && key == "\<A-b>")
if this.cursorpos > 3
var pos = this.cursorpos - 3
if pos > 1 && this.prompt[pos - 1] == ' ' && this.prompt[pos - 2] == ' '
Expand All @@ -206,7 +207,7 @@ export class FilterMenu
endif
elseif key == "\<PageUp>"
win_execute(id, 'normal! ' .. "\<C-u>")
elseif key == "\<Home>" || key == "\<C-b>"
elseif key == "\<Home>" || (!options.emacsKeys && key == "\<C-b>") || (options.emacsKeys && key == "\<C-a>")
if this.cursorpos > 3
this.cursorpos = 3
this._CursorSet()
Expand Down

0 comments on commit 07845ea

Please sign in to comment.