[TxMt] How to select something in a document by using a macro/command?

Hans-Joerg Bibiko bibiko at eva.mpg.de
Thu Nov 22 11:02:05 UTC 2007



I found a way to select something - calculated on run-time - by using  
a normal macro (plus command) without TMTOOLS!!

The problem was if I have a script which outputs a text chunk or a  
regexp how can we select that text/regexp in a TM window?
The approach is actually very simple. I copy the text/regexp into the  
shared find pasteboard; place the caret to a proper location; and  
execute 'findNext'. Thus I wrote a tiny C program which copies a  
string into the shared find pasteboard. OK, then I wanted to write a  
man page for that command on basis of the normal pbcopy man page, and  
I figured out that pbcopy is already able to do this ;)

Fine. The only problem was that if I write a tmcommand à la:

echo -en "FINDTEXT" | pbcopy -pboard find

it doesn't work because "pbcopy -pboard find" is called from inside  
of TM. If I execute that in a Terminal, switch back to TM it works.  
The solution: I have to execute this in a new bash environment.

The basic tmcommand (example name "SELECTTEXT"):

export RESULT
/bin/bash -c 'echo -en "$RESULT" | pbcopy -pboard find'
#place the caret to a proper location to be able to execute 'findNext'!
open "txmt://open/?line=$LINE&column=$COLUMN"

After executing that command the string $RESULT is in the shared find  
pasteboard, and the caret is set.

Next step > the macro:
Before we can execute 'findNext' (= APPLE+G) we have to set the  
parameters of the find panel, meaning whether we want to do a regexp  
search or not; ignore case or not.

Thus record a macro à la:

1) open find panel, set the desired parameters, and do a dummy search  
for something which is NOT in the document -e.g. look for \xFFF3; and  
close it

2) execute the command "SELECTTEXT"


That's it. The nice side-effect is that the macro changes nothing  
within TM's find panel ;)
And the macro does not affect the undo buffer, it does not change the  
text etc., and the selection is done instantly.

But attention:
The only tricky point : Be aware of correct escaping!!

On that basis I wrote the "Select XML/HTML balanced tags" script  
which will come as soon as possible. I only have to fix some tiny  




More information about the textmate mailing list