Convert Case

#1

Does anyone know of a way to convert selections between different case styles (e.g. snake_case, camelCase, Title Case, etc)?

Like the “Coercion” section of vim-abolish

#2

As you may know, backtick and ~ convert the selection to all-lower and all-upper case. It shouldn’t be too hard to write a script that decodes the various case styles to a standard style then offers a menu of various styles to encode to.

I don’t know of any existing plugins that do that, but it seems like it would be a good First Plugin training exercise.

#3

it seems like it would be a good First Plugin training exercise

Ya, it might be.
But unfortunately I don’t have time for that right now; maybe later.

#4

#5

I don’t know anymore where I found this (I believe on the wiki), but here is a plugin that does exactly that.

#6

Someone asked me that in the past but I don’t remember well.

#7

I made a plugin for this:

There are a few things I’d like your input on:

  • What do you think of the mappings?
  • Undo/redo doesn’t restore selections properly, do you know how to fix that?
  • Makes use of mark (^) register, should this be avoided?

And as it’s my first plugin, feel free to give any other comments on convention and such.

P.S.
Do you know if there’s a way to extend a selection until the anchor of the next selection?

#8

I’m completely baffled by this:

evaluate-commands -itersel %{
        try %{
            # select part begninnings
            execute-keys 's(?<![a-zA-Z0-9])\w<ret>'
            # extend until separator
            execute-keys ';?\w(?=[\-_.\s])<ret>'
        }
}

This snippet does nothing when in a command, but if I do the actions manually it works as expected.

running this command line:

eval %{ exec "s(?<![a-z])\w<ret>" }

gives me the following error:

1:1: 'eval' 1:2: 'exec' unable to parse modifier in '<![a-z])\w<ret>'

on Kakoune v2019.01.20-249-g92972bed

#9

You want the regex (?<![a-z]), but exec wants keys, not a regex string, and < is used in the key-name syntax for things like <a-x> and <c-f>.

Make it exec "s(?<lt>![a-z])\w<ret>" instead.

The reason nothing happens when it’s in a command is because your command wraps the offending exec in try, which eats the error. You might consider adding a catch clause that checks %val{error}, eats the error if it’s one you expect, and re-raises it with fail otherwise.

#10

Thanks! That was it.
There is a catch following it in my actual script, but it’s to try a different regex, so the error message still gets eaten.

Could you elaborate on how to test for an expected error?
In python you can do ... except TypeError as e: ...
Is there something similar in kakscript?

#11

I’ve never actually tried it myself, but the ingredients are as mentioned in my previous message. I imagine it might look something like:

try %{

    # ...whatever...

} catch {
    eval %sh{
        case "$kak_error" in
            no selections remaining)
                # we expected this, it's fine
                printf "nop\n"
                ;;
            *)
                # something unexpected occurred,
                # re-raise the same error
                printf "fail %s\n" "$kak_error"
                ;;
        esac
    }
}

Not quite as elegant as Python, but better than losing all error reporting entirely.

EDIT: There’s got to be a nicer way to catch particular errors.