diff --git a/README.md b/README.md index 2a91ef5..faa2af1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 :jumpJump -map global normal :jumpExtend +map global normal :jumpJump +map global normal :jumpJump -add +map global normal :jumpJump -extend ``` ## Configuration diff --git a/jump.kak b/jump.kak index 0749dfe..02a25d0 100644 --- a/jump.kak +++ b/jump.kak @@ -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 %{ @@ -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 ') - console.log('select -display-column ' + targetLabel.selectionDescription) - console.log('execute-keys 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 ') - 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" } } @@ -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 []: 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 + } diff --git a/performJump.js b/performJump.js new file mode 100644 index 0000000..e307d3b --- /dev/null +++ b/performJump.js @@ -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 + select -display-column ${ targetLabel.selectionDescription } + execute-keys he + ` + +const onAdd = + ` + execute-keys + eval -draft %{ + select -display-column ${ targetLabel.selectionDescription } + exec w + set window jumpAddSelectionDesc %val{selection_desc} + } + select %opt{jumpAddSelectionDesc} %val{selections_desc} + unset window jumpAddSelectionDesc + ` + +const onExtend = + ` + execute-keys + select -display-column ${ targetLabel.selectionDescription } + ${ targetLabel.jumpOrientationForward ? 'execute-keys HE' : '' } + ` + +const onUnrecognized = + ` + execute-keys + fail "jumpJump: unrecognized switch '${ P.env.jumpMode }'" + ` + +const jumpModeMap = + { '': onDiscard + , '-discard': onDiscard + , '-add': onAdd + , '-extend': onExtend + } + +console.log(jumpModeMap[P.env.jumpMode] || onUnrecognized) +