[TxMt] Re: snippet that replaces the current word
Guido Governatori
guido at itee.uq.edu.au
Wed Sep 24 10:27:14 UTC 2008
On 23/09/2008, at 10:40 PM, Timothy Bates wrote:
> Dear all,
>
> Looking some more into this, the new environment variables for italic,
> bold, and underline markup are imported into commands in the ENV[]
> array, so that makes building those commands more generic. So far so
> good.
>
> My roadblock now is that to get the format commands to act as toggles
> (i.e. they remove or add markup as appropriate), the "word"
> environment variable should let the formatting though, but it doesn't,
> (so '*bold*' comes in as 'bold').
>
> Before I reinvent the wheel, does anyone have code to use the
> "TM_LINE_INDEX" to parse "TM_CURRENT_LINE", extracting the current
> word plus markers such as "TM_BOLD_MARKER" on either side if present?
>
> My stumbling block is writing a regex to find the "TM_CURRENT_WORD"
> but only when it is at the "TM_LINE_INDEX" location (to avoid making
> mistakes when a line contains multiple copies of the current word).
>
I had similar issue, and I have used the following ruby code (just for
what precede the cursor). The regexp were too complex. The idea is
instead of looking for a word (or in my case a string), you can look
for delimeters of the string (current word). The code split the
current line in 3 parts pre, input and post.
line = " " + ENV['TM_CURRENT_LINE']
cursor = ENV['TM_LINE_INDEX'].to_i
separator = Regexp.new(/[ \]\[({$})\\]/)
if line[0..cursor].reverse.split(separator).empty? or
line[0..cursor].reverse.split(separator)[0].empty?
input = e_sn(line[cursor..cursor])
else
input = line[0..cursor].reverse.split(separator)[0].reverse
end
post = e_sn(line[cursor+1..-1])
pre = e_sn(line[1..cursor-input.size])
All the best
Guido
--
Dr Guido Governatori
http://www.governatori.net http://www/governatori.net/TextMate
http://www.defeasible.org
More information about the textmate
mailing list