occivink / kakoune-snippets

Snippet support for kakoune
The Unlicense
48 stars 6 forks source link

Replace first placeholder with selection #26

Closed aidam38 closed 1 year ago

aidam38 commented 5 years ago

A command like snippets-insert-replace-first-placeholder which would trigger a prompt, to which you would write the name of your snippets, which preferably only has one placeholder. Then, your current main selection would be the default value for the first placeholder.

A nice usecase would be \textbf{} and many other stuff in LaTeX.

occivink commented 5 years ago

You can already do this with this simple proof-of-concept command

define-command snippets-insert-replace-first-placeholder %{
    prompt 'Snippet name: ' %{
        try %{
            eval -save-regs '"' %{
                exec -save-regs '' d
                snippets %val{text}
                exec R
            }
        }
    }
}

The prompt could be replaced by a menu or a command with completions without too much effort

andreyorst commented 5 years ago

In UltiSnips Vim plugin this is called ${VISUAL} placeholder (because of visual mode). The desired selection is stored inside special variable, and gets pasted into next snippet which contains such placeholder. For example: We have such text ([] represent selection):

lorem ipsum [dolor sit amet]

We have such snippet tbf:

\textbf{${VISUAL}}`

When we select a text, and execute store-visual command (via some mapping), text disappears, and we can insert trigger somewhere:

lorem ipsum tbf[]

now we expand tbf snippet:

lorem ipsum \textbf{dolor sit amet}

If we expand it one more time:

lorem ipsum \textbf{dolor sit amet}
\textbf{}

${VISUAL} placeholder is empty, since we used it once, and didn't refilled it with new selection, so it acts as a tabstop.

This is useful in C++ code when we have munch of classes and want to put them to the namespace, we can select those, hit a key to store them, and expand a namespace snippet with desired classes inside. I've asked for these placeholders in this issue: https://github.com/occivink/kakoune-snippets/issues/1#issuecomment-448874940

occivink commented 5 years ago

I don't really see the advantage over simply deleting, inserting the snippet and then either pasting/replacing (depending on whether there is some default text)

andreyorst commented 5 years ago

In case of Vim it was automation. In case of Kakoune, we have R which will allow us to replace any selcted placeholder with stored text

occivink commented 5 years ago

So is this something we should have? I don't see much benefit to it in kakoune

occivink commented 1 year ago

The concept of a 'first placeholder' is no longer relevant.