[TxMt] Using non-saved file with TM command

Charilaos Skiadas cskiadas at uchicago.edu
Sun Jun 18 03:45:04 UTC 2006

On Jun 17, 2006, at 10:15 PM, Mike Mellor wrote:
> Here is the code (well, the method in question).  It is a command  
> in my bundle.  What I want is to sort the whole file using a  
> specific sequence, I don't want to have to select the text.  The  
> sorting works OK; I have a version that properly sorts the file.   
> The problem is that it only sorts the saved version of the file.  I  
> would prefer to sort the existing version that is open in  
> TextMate.  What I want tthe following code to do is take the file  
> from TextMate and convert it to an array for sorting.

I don't even understand how it even sorts the whole file, it seems to  
me it would only work on the selected text, if any. But you haven't  
yet told us what the input and output for the command is. I would  
1) Set the input of the command to be the entire document
2) Change $fileName = ENV['TM_SELECTED_TEXT'] to $fileName = STDIN.read

Do I understand correctly that up to the "puts line" part, your  
method just separates the string into an array of each of its lines?  
In that case, you could simply do:

def mySort(fileName)
   #opens lists and sends them to be sorted
   list = fileName.to_s.split("\n")
   list.each do |line|
     puts line #this will actually call a sorting method

Btw, any particular reason you create these global variables,  
$fileName etc? You could probably just work with local variables.

> Thanks.
> Mike

> #!/usr/bin/ruby -w
> # This is where the GTD files are kept
> $myPath = ENV['TM_DIRECTORY']
> $fileName = ENV['TM_SELECTED_TEXT']
> def mySort fileName
>   #opens lists and sends them to be sorted
>   a = fileName.to_s + "\n"
>   re = /\n/
>   list = []
>   while a.length > 0
>     if a =~ re
>       task = "#{$`}"
>       list.push task
>       a = "#{$'}"
>     end
>   end
>   list.each do |line|
>     puts line #this will actually call a sorting method
>   end
> end
> mySort $fileName

More information about the textmate mailing list