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

Kai Wood lists at kwood.eu
Thu Jan 16 16:07:26 UTC 2014


On 16.01.2014, at 16:38, Allan Odgaard <mailinglist at 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!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macromates.com/textmate/attachments/20140116/59f136ff/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2014-01-16 at 16.58.53.png
Type: image/png
Size: 62391 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20140116/59f136ff/attachment-0001.png>


More information about the textmate mailing list