I'm new to TextMate and thinking of using it for teaching purposes. I would like to fill out a form and have TextMate to automatically create the needed file. For instance I have a snippet called csc (short for CreateStandardClass) that looks like this
Class: ${1:ClassName} SubClassOf: ${2:Object} InstanceVariables: ${3:varName1},${4:varName2}
After I've entered the needed info (Rectangle, width and height) it looks like this
ClassName: Rectangle SubClassOf: Object InstanceVariables: width,height
After entering height and pressing tab I would like TextMate to generate the Objective-C code below. (It should be possible since all needed data is in the variables $1, $2, $3 and $4)
// Rectangle.h #import <objc/Object.h> @interface Rectangle: Object { int width; int height; } -(void) setWidth:(int) w; -(void) setHeight:(int) h; -(int) width; -(int) height; @end
// Rectangle.m #import "Rectangle.h"
@implementation Rectangle; -(void) setWidth:(int) w { width=w; } -(void) setHeight:(int) h { height=h; } -(int) width { return width; } -(int) height { return height; } @end
Is there a way to do this?
Thanks Bob
On Aug 9, 2007, at 6:47 AM, Bob Ueland wrote:
I'm new to TextMate and thinking of using it for teaching purposes. I would like to fill out a form and have TextMate to automatically create the needed file. For instance I have a snippet called csc (short for CreateStandardClass) that looks like this
Class: ${1:ClassName} SubClassOf: ${2:Object} InstanceVariables: ${3:varName1},${4:varName2}
After I've entered the needed info (Rectangle, width and height) it looks like this
ClassName: Rectangle SubClassOf: Object InstanceVariables: width,height
After entering height and pressing tab I would like TextMate to generate the Objective-C code below. (It should be possible since all needed data is in the variables $1, $2, $3 and $4)
...
Is there a way to do this?
Thanks Bob
Yes. There are a few ways you could do this. You could write some sort of ruby script to parse the text you just entered and generate the code. Then bind that command to some sort of keystroke or tab completion.
Or you could write a really complex snippet using multiple instances of the same variable. You can even have some of the instances of the variable use regex replacement to change the capitalization and let you insert an arbitrary number of objective-c instance variables with the corresponding code.
Check the help docs for the syntax. Check out the objective-c bundle for examples.
thomas Aylott — subtleGradient — CrazyEgg — bundleForge
Thomas Aylott writes:
Yes. There are a few ways you could do this. You could write some sort of ruby script to parse the text you just entered
and generate the code. ...
Or you could write a really complex snippet using multiple instances of the
same variable.
Thanks for your kind reply Thomas. The complex snippet (which I think I know how to do) alternative is not a real alternative, because of the teaching situation, I do not want to clutter the screen before I've entered the simple three line form. But if there are ways to hide the complex snippet and show it when I'm done with filling in the form, that would be useful!
The ruby script is an option, but unfortunately I do not know ruby, and would have to invest time to learn it. But I did some Python programming last year. Is it possible to use Python instead?
Thanks Bob
Thomas Aylott (subtleGradient <oblivious@...> writes:
You could write some sort of ruby script to parse the text you just entered
and generate the code. ...
Or you could write a really complex snippet using multiple instances of the
same variable.
Thanks for your kind reply Thomas, I do not know Ruby although I know some Python. Do you think I need to invest time to learn Ruby in order to manipulate TextMate or should I stick to Python?
Anyway, here is a snippet that I came up with.
// Class: ${1:ClassName} // Instance Variables: ${2:var1}, ${3:var2}
// $1.h #import <objc/Object.h>
@interface $1: Object { int $2; int $3; } -(void) set${2/(\w+)/\u$1/}: (int) $2Val; -(void) set${3/(\w+)/\u$1/}: (int) $3Val; -(int) $2; -(int) $3; @end
// $1.m #import "$1.h"
@implementation $1; -(void) set${2/(\w+)/\u$1/}: (int) $2Val { $2=$2Val; } -(void) set${3/(\w+)/\u$1/}: (int) $3Val { $3=$3Val; } -(int) $2 { return $2; } -(int) $3 { return $3; } @end
On Aug 10, 2007, at 6:52 AM, Cocoa Bob wrote:
Thanks for your kind reply Thomas, I do not know Ruby although I know some Python. Do you think I need to invest time to learn Ruby in order to manipulate TextMate or should I stick to Python?
You can write commands in any scripting language you want. A command is simply a script, exactly like a script you could run from the command line, which perhaps has some particular environment variables set, and doing things with input and output:
http://www.macromates.com/textmate/manual/commands
Haris Skiadas Department of Mathematics and Computer Science Hanover College