views:

230

answers:

1

I'm modding a TextMate bundle even though I'm a complete beginner at Ruby. The problem I'm trying to solve is the issue of moving the caret to a certain positition after the command has made its output.

Basically what happens is this: I hit a key combo which triggers a command to filter through the document and inserts text at the relevant places, then exits with replacing the document with the new filtered text.

What I want to happen next is for the caret to move back to where it originally was. I was pretty happy when I found the TextMate.go_to function, but I can only get it partly to work. The function:

positionY = ENV['TM_LINE_NUMBER']
positionX = ENV['TM_LINE_INDEX']
...
TextMate.go_to :line => positionY, :column => positionX; #column no worky

I can get the caret to the right line, but the column parameter isn't working. I've tried shifting them about and even doing the function with just the column param, but no luck. I've also tried with a hard coded integer, but the positionX param prints the correct line index, so I doubt there's anything there.

This is the only documentation I've found on this method, but I took a look in the textmate.rb and to my untrained eyes it seems I'm using it properly.

I know this can be achieved by macros, but I want to avoid that if possible. I also know that you can use markers if you choose "Insert as snippet" but then I'd have to clear the document first, and I haven't really figured out how to do this either without using the "Replace document" option. Anyone?

+4  A: 

Let's look at the source code of the bindings:

def go_to(options = {})
  default_line = options.has_key?(:file) ? 1 : ENV['TM_LINE_NUMBER']
  options = {:file => ENV['TM_FILEPATH'], :line => default_line, :column => 1}.merge(options)
  if options[:file]
    `open "txmt://open?url=file://#{e_url options[:file]}&line=#{options[:line]}&column=#{options[:column]}"`
  else
    `open "txmt://open?line=#{options[:line]}&column=#{options[:column]}"`
  end
end

Rather hackishly, the binding sets up a txmt:// URL and calls open on it in the shell.

So the first thing to do would be constructing an open URL and typing it into Terminal/your browser to see if TextMate is respecting the column parameter. If that works then perhaps there is a bug in your version's implementation of Textmate.go_to.

rjh
That seems to be it, yes. It opens fine from the terminal, and from another TM command. It seems there's something in the script that prevents the column to be set properly. I shall have to investigate.
Martin