Skip to content

Commit

Permalink
Add add selection mode (#7)
Browse files Browse the repository at this point in the history
* Add `add selection` mode

* Update README.md on new API
  • Loading branch information
postsolar authored Dec 5, 2023
1 parent 1948458 commit c016033
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 39 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Dimmed text when entering jump mode (face used is configurable)
- Highlighted labels (face used is configurable)
- Jumps in both directions with single command
- You can make a jump which would discard your current selection, as well as a jump which would extend it
- You can make a jump which would discard your current selection, add the target word as a new selection, or extend current primary selection
- The label to be applied is calculated based on the distance from the cursor. That is, if you have
labels like `aa``bz`, then the ones closest to your cursor will be `aa`, `ab`, `ac`, and so on, whereas
the ones furthest from your cursor will be `bx`, `by`, `bz`, irrespective of whether the label comes before
Expand All @@ -36,13 +36,15 @@ If you're having troubles with installation or something else, feel free to ask
## Usage

Simply enter the command `:jumpJump`, and then enter the characters of the label you want to jump to.
If you wish to *extend* current selection up to and including the word under a given label, rather than discard it, then use `:jumpExtend`.
If you wish to *add* the target word as a new selection, use `:jumpJump -add`
If you wish to *extend* current selection up to and including the word under a given label, use `:jumpJump -extend`.

You can add the following mappings to your `kakrc`:

```kakscript
map global normal <ret> :jumpJump<ret>
map global normal <s-ret> :jumpExtend<ret>
map global normal <ret> :jumpJump <ret>
map global normal <a-ret> :jumpJump -add <ret>
map global normal <s-ret> :jumpJump -extend <ret>
```

## Configuration
Expand Down
54 changes: 19 additions & 35 deletions jump.kak
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ declare-option -hidden str jumpContents
# Selection description of the visible part of the buffer.
declare-option -hidden str jumpContentsRange

# A temporary storage for the selection description of the target word
# when executing a jump with `-add` switch.
declare-option -hidden str jumpAddSelectionDesc

# Grab window content and push it into respective options
define-command -hidden jumpSelectWindow %{
eval -draft %{
Expand Down Expand Up @@ -111,30 +115,12 @@ define-command -hidden jumpRemoveHighlighters %{
remove-highlighter window/jumpLabels
}

# Record keypresses and execute the jump as soon as there's a matching label
define-command -hidden jumpOnPromptChange %{
define-command -hidden jumpOnPromptChange -params 1..1 %{
evaluate-commands %sh{
node -e "
const jumpLabels = JSON.parse('$kak_opt_jumpLabelsPositions')
const targetLabel = jumpLabels.find(label => label.label === '$kak_text')
if (targetLabel === undefined) process.exit()
console.log('execute-keys <esc>')
console.log('select -display-column ' + targetLabel.selectionDescription)
console.log('execute-keys <semicolon>he')
"
}
}

define-command -hidden jumpExtendOnPromptChange %{
evaluate-commands %sh{
node -e "
const jumpLabels = JSON.parse('$kak_opt_jumpLabelsPositions')
const targetLabel = jumpLabels.find(label => label.label === '$kak_text')
if (targetLabel === undefined) process.exit()
console.log('execute-keys <esc>')
console.log('select -display-column ' + targetLabel.selectionDescription)
if (targetLabel.jumpOrientationForward) console.log('execute-keys HE')
"
# Environment variables to expose:
# $kak_text
# $kak_opt_jumpLabelsPositions
jumpMode="$1" node "$(dirname $kak_opt_jumpSourcePath)/performJump.js"
}
}

Expand All @@ -146,19 +132,17 @@ define-command -hidden jumpOnPromptSubmit %{
echo -markup '{Error}Jump label not found'
}

define-command jumpJump \
-params 0 \
-docstring "Perform a labels-based jump within the visible part of the buffer" \
%{
jumpPrepareJump
prompt 'Jump to: ' -on-change jumpOnPromptChange -on-abort jumpRemoveHighlighters jumpOnPromptSubmit
}
define-command -params 0..1 -docstring "
jumpJump [<switches>]: perform a labels-based jump within the visible part of the buffer

Switches describe the way the jump will modify current selection(s).
-discard Default behavior. Discard existing selection(s) and select target word.
-add Keep existing selection(s) and add target word as a new selection.
-extend Extend current primary selection up to and including target word. See option `jumpAutoFlipOnExtend` for more details.
" jumpJump %{

define-command jumpExtend \
-params 0 \
-docstring "Perform a labels-based jump within the visible part of the buffer, extending current selection" \
%{
jumpPrepareJump
prompt 'Extend to: ' -on-change jumpExtendOnPromptChange -on-abort jumpRemoveHighlighters jumpOnPromptSubmit
prompt 'Label: ' -on-change %{ jumpOnPromptChange %arg{1} } -on-abort jumpRemoveHighlighters jumpOnPromptSubmit

}

55 changes: 55 additions & 0 deletions performJump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as P from 'process'

const jumpLabels =
JSON.parse(P.env.kak_opt_jumpLabelsPositions)

const targetLabel =
jumpLabels.find(label => label.label === P.env.kak_text)

// This case is not a failure, it simply means that either not all of the characters of the label
// have yet been entered, or there was a typo and the user could correct themselves, so we
// just wait for a matching label.
if (targetLabel === undefined)
P.exit()

const onDiscard =
`
execute-keys <esc>
select -display-column ${ targetLabel.selectionDescription }
execute-keys <semicolon>he
`

const onAdd =
`
execute-keys <esc>
eval -draft %{
select -display-column ${ targetLabel.selectionDescription }
exec <a-i>w
set window jumpAddSelectionDesc %val{selection_desc}
}
select %opt{jumpAddSelectionDesc} %val{selections_desc}
unset window jumpAddSelectionDesc
`

const onExtend =
`
execute-keys <esc>
select -display-column ${ targetLabel.selectionDescription }
${ targetLabel.jumpOrientationForward ? 'execute-keys HE' : '' }
`

const onUnrecognized =
`
execute-keys <esc>
fail "jumpJump: unrecognized switch '${ P.env.jumpMode }'"
`

const jumpModeMap =
{ '': onDiscard
, '-discard': onDiscard
, '-add': onAdd
, '-extend': onExtend
}

console.log(jumpModeMap[P.env.jumpMode] || onUnrecognized)

0 comments on commit c016033

Please sign in to comment.