About snippets

I think most people know that besides being a very light weight, and simple text editor for GNOME, gedit can also be “transformed” to a more powerful editor using the many plugins is supplies. One of those plugins is the Snippets plugin, and I would like to point out some of its more advanced features that even most of the gedit developers don’t know about.

Snippets is featured by virtually all source code editors in one way or another. It provides an easy way to insert/activate small templates in your document with some variables, or placeholders to quickly suit it to your specific use case. Placeholder can have default values, you can mirror placeholders in multiple locations, and you can specify where the cursor needs to end up when all the placeholders have been visited (you can read more on basic functionality of snippets in the gedit help). I would like to discuss four features that might be less well known.

1. Python placeholders

Python placeholders are placeholders that execute arbitrary python code to provide the text to be placed at the placeholder location. The placeholder syntax is as follows:

$<n:[k]: python code [return string] >

Here n is the optional placeholder tabstop as with other placeholders. If a python placeholder does not have any dependencies (implicit or explicit) on other placeholders, you will be able to tab to it by that tabstop. The tabstop can also be used to refer to this placeholders’ contents in other placeholders. The second optional modifier is [k], where k is one ore more comma separated tabstops referring to other placeholders. These indicate explicit dependencies (I’ll explain them later).  In the python code, you simply return the string you want at the placeholders location. In the code you can use gedit environment variables for snippets (like $GEDIT_CURRENT_DOCUMENT_PATH) and references to other placeholders (using $n). This will provide you with a kind of mirror placeholder doing a particular transformation on one ore more other placeholders. Python variables can be shared between multiple python placeholders  using global. This is where explicit dependencies come into play. Snippets does not automatically find out that another placeholder might depend on a change in a global variable, and thus should be updated. Defining the dependencies explicitly ensures the placeholder will be updated when any of its dependencies has changed.

For an example of this more advanced functionality, take a look at the gobject snippet provided for C headers and C sources. It makes extensive use of the python placeholders and is quite a nice way to bootstrap gobject classes.

2. Regular expression placeholders

Python placeholders are very powerful, and using the re module, you can do regular expression substitution quite easily. However, regular expression transformations are quite common, and a special placeholder is available which makes it easier and cleaner to use regular expressions. The syntax is the following:

${[n:]input/regular expression/substitution[/modifiers]}

Here n is the usual tabstop, input is the text on which you want to apply the regular expression. This can be either another placeholder (a number) or an environment variable such as GEDIT_SELECTED_TEXT (useful when you activate the snippet on selected text). The regular expression is a regular expression conform the python re module. This will be searched for in the input (note: this means it does not have to exactly match the input). The last part specifies how to substitute the match. Here you can use \n to backreference matched groups in the regular expression. Additionally, a special syntax allows you to apply a small number of transformations on the backreference using \<n,modifiers>. Modifiers can be one or more of the following:

  • U: Uppercase the matched group
  • L: Lowercase the matched group
  • u: Uppercase only the first character of the matched group
  • l: Lowercase only the first character of the matched group
  • t: Title case the matched group

Combining them can be useful if you want to want to capitalize only the first word, for example: \<0,Lu>. This will first lowercase the match, and then uppercase only the first character.

The last part of the regular expression placeholder specifies a set of optional modifiers for the regular expression matching equal to the modifiers in the python re module. One last feature is the use of conditions in the substitution part of the placeholder. Using the syntax (?n,truepart,falsepart), you can substitute a group depending on whether it matched or not (useful for optional groups). These can also be nested, so you can build more complex conditions.

3. Dynamic snippets

A new feature was recently developed and is currently available in the development releases (2.29.x): dynamic snippets. Snippets now exposes two functions on the gedit internal message bus (which is used to enable inter plugin communication). The message bus is similar to how you would use dbus, but just for internal use. It allows plugins to hook into each others functionality (for example, you can manipulate the file browser plugin in many useful ways using the bus). For snippets, the following two messages are available:

  1. /plugins/snippets.activate (trigger[, view, iter])
    When this message is send, the snippet corresponding to the tab trigger <trigger> is activated. You can optionally specify the view and iter where to activate the snippet, by default the current view and cursor will be used.
  2. /plugins/snippets.parse-and-activate (snippet[, view, iter])
    This message enables the dynamic snippets. Basicly, you provide the full snippet and optionally the iter and view where to activate it. This provides a very powerful mechanism to use all of the power of snippets from other plugins. I use this in one plugin (gedit-commander which I will talk about at some point) to dynamically build a snippet for a doxygen documentation of a function declaration.

4. GtkSourceCompletion integration

The last thing I want to show is the recent integration with gtksourcecompletion nacho recently blogged about. Before, snippets implemented its own completion popup for when there are multiple snippets with the same trigger. This popup was really quite horrible and had a lot of usability problems. This has now been resolved in the development version (2.29.x) using gtksourcecompletion. One additional feature is the use of multiple default values for placeholders. Using the syntax ${n:[value1,value2,value3]}, you can provide multiple choices for the default value. When you tab into this placeholder, a completion popup will show you the available choices so that you can quickly pick one. This is particularly useful for example for CSS snippets, where some CSS properties have a specific set of valid options.

Defining a placeholder with multiple defaults

Defining a placeholder with multiple defaults

Activating the snippet

Activating the snippet

I’ll try to occasionally highlight other hidden gems in gedit, until then…

Posted in gedit | 9 Comments

gedit 2.28.2 released for OS X

Just a quick post to inform that gedit 2.28.2 has been release for OS X. This release includes the work to make it possible to open files with gedit from other parts of OS X (such as with finder, or using the command line ‘open’ utility). There has also been a fix in the automatic updates plugin so that it now actually works on OS X. Also, gtk+ has been updated to 2.18 in this release. Enjoy!

Download: http://ftp.acc.umu.se/pub/GNOME/binaries/mac/gedit/2.28/

Posted in gedit | 19 Comments

New gedit plugin: multi edit

We recently landed a new gedit plugin in the gedit-plugins module named ‘multi-edit’. This plugin allows you to create multiple edit points in the document by which you can simultaneously edit your document at multiple places. I myself found the plugin very helpful in many, otherwise tedious, editing tasks. Although the plugin was written from scratch, credits should go to Jon Walsh who wrote a similar plugin and on which the ideas for this plugin were based.

The new plugin introduces a new ‘mode’ in which you can do multi editing. You enter this mode by Ctrl+Shift+C, and once enabled you can start inserting edit points manually by pressing Ctrl+E at any point in the document. Additionally, you can use Ctrl+Home and Ctrl+End to respectively insert edit points at the beginning or end of the line automatically (it will also skip to the previous/next line so you can quickly insert edit points at the beginning or end of some lines). When you start typing the inserts/deletes will be replicated in all the edit points. You can then press Escape to remove the additional edit points, and press Escape again to finally quit multi edit mode.

In addition to inserting multi edit points, it also features column editing when you are in multi edit mode. This means you can select a part of the text, and press Enter to make the selection into a column selection. When you then start editing, it will edit the whole column (just as if you would have inserted multiple edit points manually and removed the text in the column).

You can watch a short (somewhat lame :)) movie showing this new plugin:

http://people.gnome.org/~jessevdk/multiedit.ogv

Posted in gedit | 4 Comments

gedit OS X opening files

Finally sat down and took the time to solve the issue of opening files on OS X. Since OS X uses IPC for opening files on existing processes, some special care had to be taken to make this work. Anyway, gedit now properly opens files when opened from for instance the finder, and also supports dropping files in the dock on the gedit icon. It also now registers itself as a recommended application for opening most known text files. Hurray!

Posted in gedit | 1 Comment

gedit 2.28.0, OS X

Lagging a bit behind, but it is there, gedit 2.28.0 for OS X. There wasn’t much time during this cycle for fixing some of the reported bugs on OS X, I especially wanted to have the issue resolved with gedit not being able to open files when opening them from for instance finder. Implementing this turned out to not be as trivial as a few hours (at least not for me, I don’t really know any objective-c).

So, not so much OS X goodies in this release, but at least you get the latest and the greatest in terms of gedit. The most notable change in this release is probably the ‘Quick Open’ plugin which you can use to quickly open documents (similar, but not quite, to textmate’s snap open).

Update: you can download the dmg at the usual location: ftp://ftp.gnome.org/pub/GNOME/binaries/mac/gedit/2.28/

Posted in gedit | 3 Comments

gitg 0.0.4

I thought it might be a good time to blog about gitg a little. Basicly, gitg is a clone of GitX (a git gui client for OS X written by Pieter de Pie) for GNOME. I initially started to develop it just because it was fun and I needed to learn about git (and writing a gui application for it seemed like a good way to learn it). After having finished the basics like loading and displaying the history, I lost interest a bit. But, with the migration of GNOME from svn to git, I picked it up again and I think by now it has become a really useful application (I use it daily).

gitg was always meant to be fast, and I think it’s pretty decent in loading the history and calculating all the tracks, and it’s pretty too :). It features two distinct areas, the first being the history view where you browse the history, look at the diff of a specific commit, etc.

gitg history showing git history

gitg history showing git history

The other view is the commit view where you can easily stage changes in files, or hunks by simply clicking in the diff view.

gitg commit view

gitg commit view

So, just released 0.0.4, which includes really cool stuff that enables you to merge, rebase, apply items from the stash by drag and drop, or by context menu. You can manage your remotes, fecth the latest objects. Create a new local branch from a remote branch, or push a local branch to a remote branch. You can now also create and remove tags. gitg is maintained on git.gnome.org and GNOME bugzilla. So please give it a try and let me know what you think :)

For some more screenshots: http://www.icecrew.nl/files/gitg/
Git: http://git.gnome.org/cgit/gitg
Downloads: ftp://ftp.gnome.org/pub/gnome/sources/gitg/0.0/

Posted in gitg | Leave a comment

gedit 2.26.3

Just released gedit 2.26.3. A small number of bugfixes, but worth a release nevertheless. Needless to say, there is also a dmg for OS X. No windows installer yet, but that’s sure to follow. From the NEWS:

New Features and Fixes
======================
– Avoid sync stat for icon of remote bookmarks
– Hide fullscreen control when minimizing gedit
– Only apply modelines when strictly necessary

New and updated translations
============================
– cs (Marek Cernocky)

Downloads: http://ftp.gnome.org/pub/GNOME/sources/gedit/2.26/
Mac OS X: http://ftp.gnome.org/pub/GNOME/binaries/mac/gedit/2.26/

Posted in gedit | Tagged | 4 Comments

gedit OS X for realz

My first ever blog post on gnome blogs, and about time. Today I ‘finished’ my earlier porting of gedit to OS X. Although we did the initial port some time ago, it was more a proof-of-concept than actually something usable. That is, I was the only one who could use it. Not any more though! I finally sat down to make it into an actual OS X App Bundle (using the excellent ige-mac-bundler). We include everything that we need in there, so it should be entirely standalone. That also means that the full thing is about 100 MB :(

I was pretty impressed by how well OS X is supported by gtk+ and friends already, but I did hit some snags on the way. One of them was gconf. We decided that it would be simpler for now not to involve dbus, so we went with gconf 2.22. Unfortunately, gconf has hardcoded install paths, and since we want to put everything in a single bundle, this was causing some problems. In the end I patched it to replace the compile time prefix with a prefix set as an environment variable. Other than that I had some issues with python modules installing in the wrong place, and I had to patch ige-mac-integration in some places to get a bit nicer menu integration (especially accelerators). All in all pretty smooth, but still about a days work to get it all done.

So, what do you get? If you’re the proud owner of a Mac (running OS X 10.4 intel), feel free to download the installer for gedit 2.26.2. After downloading, you should be prompted with the following:

After launching gedit, you should be prompted with something like this:

 

So there it is, gedit nicely running on OS X natively. It features:

  • Menu integration (including appropriate shortcuts)
  • Full support for plugins (both C and Python)
  • Everything else you expect from gedit

Of course there are still some things to be done:

  • There is no help yet (somehow, building the help eats all of my 4G of mem, crashing python for xmlpo)
  • More and more integration (proxy icon, dock menu)
  • Translations do not seem to be working at the moment

Update:
There is a new dmg 
including help, translations, better integration (proxy icon, closing/quitting) and most of the plugins in gedit-plugins.

    Posted in gedit | 26 Comments