Skip to content

Commit c016033

Browse files
authored
Add add selection mode (#7)
* Add `add selection` mode * Update README.md on new API
1 parent 1948458 commit c016033

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Dimmed text when entering jump mode (face used is configurable)
1010
- Highlighted labels (face used is configurable)
1111
- Jumps in both directions with single command
12-
- You can make a jump which would discard your current selection, as well as a jump which would extend it
12+
- You can make a jump which would discard your current selection, add the target word as a new selection, or extend current primary selection
1313
- The label to be applied is calculated based on the distance from the cursor. That is, if you have
1414
labels like `aa``bz`, then the ones closest to your cursor will be `aa`, `ab`, `ac`, and so on, whereas
1515
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
3636
## Usage
3737

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

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

4344
```kakscript
44-
map global normal <ret> :jumpJump<ret>
45-
map global normal <s-ret> :jumpExtend<ret>
45+
map global normal <ret> :jumpJump <ret>
46+
map global normal <a-ret> :jumpJump -add <ret>
47+
map global normal <s-ret> :jumpJump -extend <ret>
4648
```
4749

4850
## Configuration

jump.kak

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ declare-option -hidden str jumpContents
4545
# Selection description of the visible part of the buffer.
4646
declare-option -hidden str jumpContentsRange
4747

48+
# A temporary storage for the selection description of the target word
49+
# when executing a jump with `-add` switch.
50+
declare-option -hidden str jumpAddSelectionDesc
51+
4852
# Grab window content and push it into respective options
4953
define-command -hidden jumpSelectWindow %{
5054
eval -draft %{
@@ -111,30 +115,12 @@ define-command -hidden jumpRemoveHighlighters %{
111115
remove-highlighter window/jumpLabels
112116
}
113117

114-
# Record keypresses and execute the jump as soon as there's a matching label
115-
define-command -hidden jumpOnPromptChange %{
118+
define-command -hidden jumpOnPromptChange -params 1..1 %{
116119
evaluate-commands %sh{
117-
node -e "
118-
const jumpLabels = JSON.parse('$kak_opt_jumpLabelsPositions')
119-
const targetLabel = jumpLabels.find(label => label.label === '$kak_text')
120-
if (targetLabel === undefined) process.exit()
121-
console.log('execute-keys <esc>')
122-
console.log('select -display-column ' + targetLabel.selectionDescription)
123-
console.log('execute-keys <semicolon>he')
124-
"
125-
}
126-
}
127-
128-
define-command -hidden jumpExtendOnPromptChange %{
129-
evaluate-commands %sh{
130-
node -e "
131-
const jumpLabels = JSON.parse('$kak_opt_jumpLabelsPositions')
132-
const targetLabel = jumpLabels.find(label => label.label === '$kak_text')
133-
if (targetLabel === undefined) process.exit()
134-
console.log('execute-keys <esc>')
135-
console.log('select -display-column ' + targetLabel.selectionDescription)
136-
if (targetLabel.jumpOrientationForward) console.log('execute-keys HE')
137-
"
120+
# Environment variables to expose:
121+
# $kak_text
122+
# $kak_opt_jumpLabelsPositions
123+
jumpMode="$1" node "$(dirname $kak_opt_jumpSourcePath)/performJump.js"
138124
}
139125
}
140126

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

149-
define-command jumpJump \
150-
-params 0 \
151-
-docstring "Perform a labels-based jump within the visible part of the buffer" \
152-
%{
153-
jumpPrepareJump
154-
prompt 'Jump to: ' -on-change jumpOnPromptChange -on-abort jumpRemoveHighlighters jumpOnPromptSubmit
155-
}
135+
define-command -params 0..1 -docstring "
136+
jumpJump [<switches>]: perform a labels-based jump within the visible part of the buffer
137+
138+
Switches describe the way the jump will modify current selection(s).
139+
-discard Default behavior. Discard existing selection(s) and select target word.
140+
-add Keep existing selection(s) and add target word as a new selection.
141+
-extend Extend current primary selection up to and including target word. See option `jumpAutoFlipOnExtend` for more details.
142+
" jumpJump %{
156143

157-
define-command jumpExtend \
158-
-params 0 \
159-
-docstring "Perform a labels-based jump within the visible part of the buffer, extending current selection" \
160-
%{
161144
jumpPrepareJump
162-
prompt 'Extend to: ' -on-change jumpExtendOnPromptChange -on-abort jumpRemoveHighlighters jumpOnPromptSubmit
145+
prompt 'Label: ' -on-change %{ jumpOnPromptChange %arg{1} } -on-abort jumpRemoveHighlighters jumpOnPromptSubmit
146+
163147
}
164148

performJump.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as P from 'process'
2+
3+
const jumpLabels =
4+
JSON.parse(P.env.kak_opt_jumpLabelsPositions)
5+
6+
const targetLabel =
7+
jumpLabels.find(label => label.label === P.env.kak_text)
8+
9+
// This case is not a failure, it simply means that either not all of the characters of the label
10+
// have yet been entered, or there was a typo and the user could correct themselves, so we
11+
// just wait for a matching label.
12+
if (targetLabel === undefined)
13+
P.exit()
14+
15+
const onDiscard =
16+
`
17+
execute-keys <esc>
18+
select -display-column ${ targetLabel.selectionDescription }
19+
execute-keys <semicolon>he
20+
`
21+
22+
const onAdd =
23+
`
24+
execute-keys <esc>
25+
eval -draft %{
26+
select -display-column ${ targetLabel.selectionDescription }
27+
exec <a-i>w
28+
set window jumpAddSelectionDesc %val{selection_desc}
29+
}
30+
select %opt{jumpAddSelectionDesc} %val{selections_desc}
31+
unset window jumpAddSelectionDesc
32+
`
33+
34+
const onExtend =
35+
`
36+
execute-keys <esc>
37+
select -display-column ${ targetLabel.selectionDescription }
38+
${ targetLabel.jumpOrientationForward ? 'execute-keys HE' : '' }
39+
`
40+
41+
const onUnrecognized =
42+
`
43+
execute-keys <esc>
44+
fail "jumpJump: unrecognized switch '${ P.env.jumpMode }'"
45+
`
46+
47+
const jumpModeMap =
48+
{ '': onDiscard
49+
, '-discard': onDiscard
50+
, '-add': onAdd
51+
, '-extend': onExtend
52+
}
53+
54+
console.log(jumpModeMap[P.env.jumpMode] || onUnrecognized)
55+

0 commit comments

Comments
 (0)