The insertion template system is one of the hidden gems of KatePart, modelled after a similar feature in the eclipse IDE by Joseph Wenninger.
Insertion templates allows to insert a text snippet into the editor that contains variables like user name, date and similar, or fields that may be filled out by the user. Such fields can be repeated within the template text and can then be edited at once, they are highlighted in a special way (underlined) and are navigated between simply by pressing the TAB key.
Insertion templates aren’t widely used yet, as far as I know two functions use them:
KatePart with an insertion template inserted.
My name and email address as well as the year are expanded variables, and the underlined strings are due to be edited. The inserted text (by the Kate file templates plug-in) is
---
Copyright (C) %{year}, %{fullname} <%{email}>
*/
#ifndef _${ClassName}_h_
#define _${ClassName}_h_
class ${ClassName} : public ${Super} {
public:
${ClassName}( ${cursor} );
~${ClassName}();
You can see the template string ClassName repeated several times, the benefit of having to type the final class name should be obvious.
From a user point of view, insertion templates should hopefully make life a bit easier: the template strings can automatically insert trivial data like your name, email address, the date and time and others. And editable template fields provides a hint about what to put in a certain place.
The worst problem is that we do not have a help popup that informs you about what to do after a insertion template is inserted, so that must be a feature to come (something like the small help messages in kpdf).
As a developer, all you need to do is to insert a text containing template text using a special function in the editor API.
Since the feature is implemented as a KTextEditor interface KTextEditor::TemplateInterface, it is easy to use from plug-ins. The code in the patched version snippet plug-in responsible for inserting the template text looks like this:
Kate::View *kv = win->viewManager()->activeView();
...
KTextEditor::TemplateInterface *ti = KTextEditor::templateInterface(kv->document());
if (ti)
{
uint line; uint col;
kv->cursorPosition(&line,&col);
QMap<QString,QString> initVal;
if (!sSelection.isEmpty())
initVal.insert("selection",sSelection);
ti->insertTemplateText(line,col,sText,initVal);
}
else
kv->insertText(sText);
Templates in the inserted string have two formats, one in which variables are expanded, but that is not added to the list of editable strings:
%{variable name}
You should of course only use known variable for those. The other form is
${variable name}
and is added to the list of editable strings, after variables have been expanded.
Here is a list of the variables known by the template system and their values:
The variable retrieved from KAddressBook are taken from the personal entry of your KDE addressbook, and you will be prompted to fill it in the first time it’s required.
The cursor variable is special in that it is always the last in the order, and when tabbed to, it’s replaced with an empty string. The idea is to put it where you want the cursor after editing the other template fields.
The insertion template system provides a way of aiding insertion of text snippets in a very efficient an unobtrusive manner, with no extra prompts or other abstractions. Just activate a function that inserts a template and continue typing.
Developers can easily extend the set of variables to be expanded, all there is to do is to pass a prefilled QMap<QString,QString> to the insertTemplateText function.
Possible improvements includes providing a generic variable interface to KTextEditor allowing applications as well as users to define additional variables, and a context sensitive help system that could inform the user about how to deal with the templates would be nice. But the system is allready fully functional — go ahead and use it!
Comments
So, when I hit this, It
So, when I hit this, It gives me a template with a behaviour very similar to the one described in the anonymous comment about “textmate alike snippet”. sohbet - chat
Hi! Thanks for kate, it is
Hi! Thanks for kate, it is really great! Template are nice but what about code snippets, “à la” textmate (OS X)? Gedit has an implementation of it, scribes as well, even vim has a plugin that is essentially a port of textmate snippets. To my knowledge, there is no kde editor that has this feature… Do you plan on implementing it? Thank you again! Carl
Available since 2004 ?
Hi!
In this thread, I saw a post saying “try to hit ctrl+alt+shift+T”
So, when I hit this, It gives me a template with a behaviour very similar to the one described in the anonymous comment about “textmate alike snippet”.
The only thing is: I don’t know how to create my own template, or get other templates than the one I get hitting ctrl+alt+shift+T
No way to find any docs about this hidden but very useful features. Any Idea?
Sébastien.
A link to the kate plugin?
I searched the mailinglist but did not find the plugin mentioned above for “snippets”. Does anyone know where to find this?
Hi Looks like a helpful
Hi
Looks like a helpful tool for work. Sure it will become more sophisticated in the future. Good news!
Submited by : Caballos
Shortcut keys for insertion templates would be really helpful
It would make a world of difference if I could assign shortcut key combos to call up my insertion templates. Having to reach for the mouse each time I want to use one is almost counter productive… Great feature! Harel
how to use in kate?
Can kate use insertion templates? Or are they only custom features for enhancing kate-part?
what about code snippets?
Hi! Thanks for kate, it is really great! Template are nice but what about code snippets, “à la” textmate (OS X)? Gedit has an implementation of it, scribes as well, even vim has a plugin that is essentially a port of textmate snippets. To my knowledge, there is no kde editor that has this feature… Do you plan on implementing it? Thank you again! Carl
TextMate
I am sure you have all heard this before, but I really hope you are thinking about making Kate more similar to TextMate, in terms of functions… I never ever thought that a texteditor would STOP me from switching entirely to Linux but right now coding Ruby on Rails in textmate is pretty darn sweet… I just had a look at scite, which does a lot of the same things — I wonder if a lot of this would be doable through the ECMAscript thing, or maybe it will have support for other kinds of scripting in KDE4?
This snippet thing seems great, but after reading this article, I am still not entirely sure how to use it. Do I have to recompile Kate first? Also, there should be support for multiple tabstops, as in
def ($1) ($2) end
or similar.
It’s amazing the kind of community that has been built up around TextMate, despite the fact that is’s closed source. I’m not saying that we need to do everything the same, but making able the same kinds of things is really important… The Ruby, Ruby on Rails, and not least the subversion bundles are in use every two seconds when I write programs…
Thanks a lot for your efforts!
Stian
A textmate alike snippet
A textmate alike snippet system should not be too hard to build. From hwat I see, there are three main points that make textbundles such a great feature:
Hope this helps some developers get ideas :)
Post new comment