Thursday, May 6, 2010

enclosing a word with a tag with dynamic parameters.

Hello everybody,

I think I have a problem that requires a function. Here it is. Let's
say I have the word "house". If I put the cursor under the letter "u",
I would like to get:

<span class="latin a3">house</span>.

It's 3 because I "u" is letter #3 in that word. On top of that, if I
again, put the cursor under letter "s" and press another combination,
I want to get:

<span class="latin a3 s4">house</span>.

If I again put the cursor under "e", I should get <span class="latin
a3 s4 s5">house</span>.

In other words those html classes should accumulate and they should be
either aX or sX depending on the key combination. The X is however is
always the letter number.

I'm already using the surround plugin but it's not enough. I've never
programmed in vi and he's what I attempted today:

" Marker is either "a" or "s"
function! my(marker)
let l:saveB = @b

" Mark the current column
let l:beginning_column=col('.')

" Put the character to the left into register b.
exe 'normal h'
exe 'normal v"by'
let l:myB = @b
exe 'normal l'

" If the character to the left was a "word" (i.e. character), then
move to
" the beginning of the word, otherwise we are already at the
beginning of the
" word
if l:myB =~ '\w'
exe 'normal b'
endif

" Get the difference between the positions. That would be the X I
talked about.
let l:diff = l:beginning_column - col('.') + 1

" Now look to the left of the beginning of the word
exe 'normal h'
exe 'normal v"by'
let l:myB = @b
exe 'normal l'

" If it's an angle bracket, we'll assume that we already have a span
tag. If not, surround the word with the tag and
" the attribute "latin" and move back to the end of the tag.
if l:myB != '>'
exe 'normal ves<span class="latin">'
normal f>
endif

" And here is where I have the problem. I need to do a replacement of
the form:
:s/<span class="(.\{-})">/<span class="\1 " + marker + l:diff/ and
only for the tag to the left of the word.
endfunction

Ideas? Would greatly appreciate any insight.

Sergei

P.S.: I can't figure out how to turn off Visual mode once I entered it
with: "exe 'normal v'".

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: