Friday, May 15, 2009

TextMate Snippets for GPL3 Copying Statements

I have, somewhat arbitrarily, chosen GPL3 as the licence for my (as yet unwritten) new code. Instead of writing that code I have been yak shaving in the form of learning how to write TextMate snippets to insert the GPL copying statements (available from the GPL Howto page). Turns out it is not too hard to get something working quickly.

New snippets can be created via the Bundle Editor. I created a new bundle (via the '+' button at the bottom left of the Bundle Editor) and named it MyAutomations (innovative, what?). I want two snippets: one to insert the program in a single file copying statement, and the other to insert the 'this file is part of program' statement.

A new snippet is created via the '+' button in the bottom left. I name my first snippet 'GPL' and replace the default helper text with the appropriate GPL copying statement. Select the Activation to be 'Tab Trigger' and set the text to be 'gpl'. Closing the Bundle Editor saves the changes and typing gpl and then hitting tab correctly inserts the copying statement.

The second snippet is slightly more complicated as it includes the name of a program, something that will change from file to file. This is easily accommodated using tab stops. I created another snippet, this one named 'GPL Program', and choose Tab Trigger activation with an activation string of 'Gpl'. For the text to be inserted I use:

This file is part of ${1:ProgramName}.

$1 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

$1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with $1. If not, see .
$0
The ${1:ProgramName} is the first, and only, tab stop. After typing Gpl and hitting tab, the default string 'ProgramName' will be highlighted with the cursor sitting, waiting for me to enter a proper program name. The other occurrences of $1 in the text mirror what is written here.

The final $0 is where the cursor ends up when I tab out after writing the program name.

Simple.

There are some enhancements I'd like to make:
  • If I type the tab trigger after a character or some text (like a comment character), i'd like all lines of the copying statement to begin with that text.
  • If I am editing source code, I'd like the copying statement to be prefixed with the appropriate comment character.
  • I'd like a snippet that just inserted a copyright statement and the copying statement in one go.
I'll work on these.

0 comments: