[TxMt] Re: Range of selection string / environment variable TM_SELECTION

Allan Odgaard mailinglist at textmate.org
Thu Jan 16 15:38:48 UTC 2014


On 16 Jan 2014, at 21:42, Kai Wood wrote:

> I need the first and last line number in a range of lines where either 
> a blinking cursor or a selection appears in the document. Only visible 
> selections count.

Here is a ruby function that parses the selection string syntax and maps 
it to a “first, last” line that is rendered selected:

     def parse_selection_string(str)
       str.split('&').map do |range|
         if range =~ 
/(\d+)(?::(\d+))?(?:\+\d+)?(?:([-x])(\d+)(?::(\d+))?(?:\+\d+)?)?/
           l1, l2, c1, c2 = $1.to_i, ($4 ? $4.to_i : nil), ($2 || 
1).to_i, ($5 || 1).to_i
           l1, l2, c1, c2 = l2, l1, c2, c1 if l2 && (l1 > l2 || l1 == l2 
&& c2 > c1)

           case $3
             when 'x'
               [ l1, l2 ]
             when '-'
               l2 -= 1 if c2 == 1
               [ l1, l2 ]
             else
               [ l1, l1 ]
           end
         else
           abort "unsupported selection string syntax: ‘#{range}’"
         end
       end
     end

You may want to post-process the result to merge overlapping ranges.


More information about the textmate mailing list