Unable to filter and keep selection

I’ve been unable to use the <a-k> shortcut to filter a range of selected text.

For example, if we open kak to the scratch buffer:

*** this is a scratch buffer which won’t be automatically saved ***
*** use it for notes or open a file buffer with the :edit command ***

And use % to select the entire buffer, then <a-k>use it for notes<ret> to filter the selection. The entire buffer is still highlighted blue on screen, the contents of %val{selection} is still the entire buffer, and curiously the contents of %reg{/} is use it for notes.

This process works if I use s to match selections, but I see a lot of scripts using <a-h> or <a-x> to select a line then <a-k> to filter the selection and this method doesn’t seem to be working for me.

I’ve tried the master branch on commit ace499e and the v2019.1.20 tag with and without the -n flag.

I think you are confused about the semantics of the <a-k> command.

<a-k> is designed to reduce the amount of selections, so for example, If you would create a selection on both of the lines (edit: this means 1 per line) you mentioned and then execute <a-k>use it for notes<ret>, you should be left with only the selection of the second line.

The behaviour that I think you expected is indeed what the s command is for.

1 Like

Thanks for the reply. I was asking about <a-k> because I’m trying to get the previous typed word as a selection in a hook available via %val{selection} or $kak_selection. I’m looking at what some of the completion tools scripts do in an InsertIdle hook, but everyone of these execute-keys commands results in an 'execute-keys' no selections remaining error.

jedi.kak
execute-keys -draft <a-h><a-k>\..\z<ret>

racer.kak
execute-keys -draft <a-h><a-k>([\w\.]|::).\z<ret>

clang.kak
execute-keys -draft <a-h><a-k>(\.|->|::).\z<ret>

ctags.kak
execute-keys <space>hb<a-k>^\w+$<ret>

Here’s an example InsertIdle hook. Every version of execute-keys fails so nothing is ever echo’ed to the status bar prompt.

hook window InsertIdle .* %{
    try %{
        # choose one version of execute-keys below
        execute-keys -draft <a-h><a-k>\..\z<ret> # from jedi.kak
        #execute-keys -draft <a-h><a-k>([\w\.]|::).\z<ret> # from racer.kak
        #execute-keys -draft <a-h><a-k>(\.|->|::).\z<ret> # from clang.kak
        #execute-keys -draft <space>hb<a-k>^\w+$<ret> # from ctags.kak
        echo %val{selection}
    }
}

What would be the best way to capture the previously entered word as a user types in insert mode?

I think you are confused about the semantics of the <a-k> command.

add me to the equation

you should be left with only the selection of the second line.

interesting. Bu he already said that it doesn’t

The entire buffer is still highlighted blue on screen,

but what you’re saying, coincides with say kakoune/doc/autoedit.asciidoc at master · mawww/kakoune · GitHub

Fortunately, Kakoune provides us with a command for that: the <a-k> command, which keeps selections where a certain regex can be found.

All I know is that even starting kak without a config file I still don’t know what <a-k> it’s supposed to do.

Let’s say we have a document like this:

foo A bar
foo B bar
foo 3 bar
foo D bar

From normal mode, we can type %sfoo . bar<ret>:

  • % creates one selection covering the whole document
  • s selects regex matches within the current selection(s)
  • the regex foo . bar matches each line (not including the newline at the end)
  • <ret> executes the s command

Now we have four selections:

  • foo A bar
  • foo B bar
  • foo 3 bar
  • foo D bar

If we now type <a-k>[A-Z]<ret> we will keep the selections that match [A-Z] (i.e. which contain a capital letter) and discard the rest, resulting in three selections:

  • foo A bar
  • foo B bar
  • foo D bar

On the other hand, if we start from the initial document and instead type %<a-k>[A-Z]<ret>:

  • % creates one selection covering the whole document
  • <a-k> keeps selections that match the given regex and discard the rest
  • the regex [A-Z] matches somewhere within our document-wide selection (actually 3 times, but one is enough)
  • <ret> executes the <a-k> command

Now we have one selection, still covering the entire document.

All the examples you quote are looking for something more specific than just an arbitrary word. Jedi wants a word with a . in it, Racer wants a word followed by . or ::, Clang wants a word followed by ., -> or ::, and ctags wants a bare word… alone on a line? Or a word that contains only alphanumeric characters? I’m not sure if ^ and$ match the beginning and end of a selection as well as a line.

Anyway, you probably want something simpler than that, maybe something like:

execute-keys -draft <space>b_
  • <space> removes all secondary cursors, leaving only the primary (but it’s in -draft mode, so it doesn’t mess up the user’s cursors for real)
  • b goes back a word
  • _ trims any leading or trailing whitespace from the selection, which is important if bhad to go past multiple whitespace characters to get to the previous word
  • <a-k> keeps selections that match the given regex and discard the rest

interesting. Then again, coincides with I’ve read so far, assuming of course that the above is followed by the carriage return key.

I’ll have to check on another system

so to recapitulate if I have

 *** this is a *scratch* buffer which won't be automatically saved ***
 *** use it for notes or open a file buffer with the :edit command ***

and if I were to select the whole buffer with % then <a-k> and finally type notes and <ret> it’s supposed to discard the line everything that doesn’t match notes, correct?

and if I were to select the whole buffer with % then <a-k> and finally type notes and <ret> it’s supposed to discard the line everything that doesn’t match notes , correct?

<a-k> keeps selections. If you select whole buffer, you have single selection. If you press <a-k> and type anything that is inside this selection it will be kept. If you have several selections, you discard selections that doesn’t have the regex, and keep those who match.

For example:

string buffer_name;
int buffer_index;
string window_name;
int window_index;

You want to make all strings near each other, and all ints near other ints.
% - select whole buffer:

[string buffer_name;
int buffer_index;
string window_name;
int window_index;]

Alt+s -split selections by lines:

[string buffer_name;]
[int buffer_index;]
[string window_name;]
[int window_index;]

Alt+k string - keep selections matching pattern:

[string buffer_name;]
int buffer_index;
[string window_name;]
int window_index;

d - cut selections

int buffer_index;
int window_index;

Now you can move to another place, and paste them with P, or Alt+P in case you merged cursors:

int buffer_index;
int window_index;
string buffer_name;
string window_name;

@andreyorst loved how you broke it down.
@Screwtapello needless to say your input is always appreciated.

Alt+s works but Alt+k doesn’t in my end.

this is of course, without a configuration file

Weird. Just started freshly compiled from HEAD Kakoune with these arguments:

kak -n -e "execute-keys %{%<a-s><a-k>notes<ret>}"

and got this:

image

Seems working fine without configuration file (-n)

Weird. Just started freshly compiled from HEAD Kakoune

give me a few minutes to compile

I’m not even using grp: lalt_lshifton setxkbmap anymore but ctrls keys

hehe that’s funny

and just to clarify % selects the buffer

and x follows by j or k respectively moves the selection up and down, correct?

No. x selects line, or next line if current is empty. j or k simply moves the cursor.

right, x doesn’t work and neither by having j or k

for that use case with the notes from the scratch buffer what seems to be working is

Alt + C

of course, moving forward with w afterwards

I’ll try it again, but that’s what seemed to pick it up (in my end at least)

and yes, % is useless

I lost track of your thoughts.

1 Like

the only combination that seemed - so far - to be working is Alt + C that selects whatever regex is to be used by Alt + k later on.

Or else, Alt +k doesn’t work with % or even x

edit

the problem here (in my end again) is with selection and nothing else

Again, if you need to select something within selection you need s key. Alt+k keeps selections that contain matches, not matches itself.

Can you write a simple text example like I did of what you trying to achieve?

@andreyorst if you were to have the same sample than the op

[*** this is a *scratch* buffer which won't be automatically saved ***
*** use it for notes or open a file buffer with the :edit command ***]

Assuming that everything is selected with % or by selecting the first line with x then J I would assume that Alt + k should work, But it doesn’t.

It’s a selection regardless .

the only combination that would ‘select’ it for Alt + k to work, is either C or Alt+ C

I think I understand your problem

If you select everything with % or with xJ you then have 1 selection containing the whole buffer. Then if you do <a-k> with a regex, you obtain either:

  1. No selection in case the regex does not match within the selection
  2. The full selection in case the regex does match

However, if you select the first line and do C or <a-C> you obtain 2 selections, one on line 1 and one on line 2. Then if you execute <a-k> with a regex you can obtain:

  1. No selections in case neither of the selections contain a match with the regex
  2. 1 selection in case one of the selections match
  3. 2 selections, in case both match.

As pointed out by andreyorst:

Again, if you need to select something within selection you need s key. Alt+k keeps selections that contain matches, not matches itself.

Sometimes people use “selections” to mean “things that have been selected”. For example, if I select five apples from the fruit market, I have five selected apples or five selections, and if I select the text “hello” I have five selected characters or five selections.

That is not how Kakoune counts selections. In Kakoune, all the text from one end (the cursor) to the other (the anchor) is a single selection. If you have multiple cursors (by ctrl-clicking with the mouse, by pressing C, by using s, S, <a-s>, or <a-S>) then you have multiple selections, but only as many as you have cursors.

So when we say <a-k> keeps selections matching a regex, we don’t mean “it keeps the individual characters that match the regex selected”, we mean if there are any matches within a single, large selection, then the entire single, large selection is kept.

For example, if I select five apples from the fruit market, I have five selected apples or five selections, and if I select the text “hello” I have five selected characters or five selections.

:slight_smile: still, there would be only other items sans a group by the time you select them. And god forbid if you select them all at once.

but you can still delete the whole scratch buffer by % and hitting the d key, right? I don’t see any documentation that I have to select those two lines separately on the scratch buffer with C or Alt-Ceither, and then delete the lines. d doesn’t know any better. Or c for that matter.

What I’d like to see is the same behavior that you would normally get by having those d or c keys.

That is not how Kakoune counts selections. In Kakoune, all the text from one end (the cursor) to the other (the anchor) is a single selection.

but you’re referring to Kakoune now as a third-person object. Kakoune doesn’t know any better.

You do. Not Kakoune.

as a matter of fact, even C or a-C don’t do anything when there are newlines or whitespaces either. There goes the functionality of the purpose of what Alt-k is supposed to do.

So now I’m fighting with and trying to figure out where should I begin with a-C and where to finish.