On 16.01.2014, at 16:38, Allan Odgaard <mailinglist@textmate.org> wrote:

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.

I hope this will not cost you a sleepless night, because…


Result: [[18, 22], [4, 3], [11, 11]] (Lowest value: 3, one less on the minimum side)

But a very nice try anyway, thanks!