% matching double quotes (")

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

% matching double quotes (")

Dominic Evans
Is there an easy way to use matchit.vim or the built in % matching to
allow me to jump between an opening and closing double quote?

so if i have:

 'System.out.println("lala" +abc+ "dede");'

I can position on the " infront of lala and push percent and it jumps
to the closing " on lala?

Cheers,
Dom
Reply | Threaded
Open this post in threaded view
|

Re: % matching double quotes (")

Tim Chase-2
> Is there an easy way to use matchit.vim or the built in %
> matching to allow me to jump between an opening and closing
> double quote?

The simple answer is no.  In order to find the truely matching
quote, Vim would have to parse the entire file from the beginning
to find the parity of the quote in question.  This could also
easily be thrown off by the use of the single-quote/apostrophe as
an apostrophe rather than a single-quote.  Because the quote
marks are a single character that mark the beginning and the end,
Vim isn't cued with information regarding which direction to
search for it.  e.g.

        "That's crazy", she said, "but I ain't."

With the first clause or second clause omitted, you get peculiar
behavior trying to match.

On the Todo list, Bram has put in a text-object for quotes (at my
request...thanks, Bram!), however these assume you're inside the
quote in question, so vim can search back to the beginning quote
and forward to the ending quote.  I'm not sure how it would
evidence its smarts if you were actually on the quotation mark in
question.

If you presume quotes can't span lines, you might be able to
write up something in a function that would get called.  For some
such function you might be able to use something like


function! MatchThing()
     let l:col = col(".")
     let l:line = getline(".")
     let l:char = l:line[l:col - 1]
     if '["'."'" !~ l:char
         norm! %
         return
     endif
     let l:left = substitute(l:line, '\%'.l:col.'c.*', '','')
     let l:right = substitute(l:line, '.*\%'.(l:col+1).'c', '','')
     let l:lcount = strlen(substitute(l:left, '[^'.l:char.']',
'','g'))
     let l:rcount = strlen(substitute(l:right, '[^'.l:char.']',
'','g'))
     if l:lcount % 2
         let l:newcol = strlen(substitute(l:left,
'\(.*'.l:char.'\).*', '\1', ''))
     else
         let l:newcol = (1+l:col + strlen(substitute(l:right,
'\([^'.l:char.']*\).*', '\1', '')))
     endif
     exec 'norm! '.l:newcol."|"
endfunc


However, I had a few problems with this function when lines
wrapped on the screen.  My "ruler" showed things like quotation
marks being in "83-85" which caused the MatchThing function to
find the wrong column.  I tried with wincol() and virtcol() but
neither got me any closer.  I got similar behavior when I had
real tabs in the line.

It should also have the same behavior as the "%" on
non-quote-marks, so you should just be able to do

        :nnoremap % :call MatchThing()<cr>

to get this added behavior.

Lastly, there's always the ability to use

        f"
        F"
        f'
        F'

(and their "t" counterparts to the "f"s)


HTH,

-tim






Reply | Threaded
Open this post in threaded view
|

Re: % matching double quotes (")

John (Eljay) Love-Jensen
In reply to this post by Dominic Evans
Hi Dominic,

> I can position on the " infront of lala and push percent and it jumps
> to the closing " on lala?

Sort of, but not with %.  Use this:
f"

When on the end quote, and you want to move to the beginning, use this:
F"

HTH,
--Eljay


Reply | Threaded
Open this post in threaded view
|

Re: % matching double quotes (")

Yegappan Lakshmanan
In reply to this post by Tim Chase-2
Hi Tim,

On 9/13/05, Tim Chase <[hidden email]> wrote:
>
> On the Todo list, Bram has put in a text-object for quotes (at my
> request...thanks, Bram!), however these assume you're inside the
> quote in question, so vim can search back to the beginning quote
> and forward to the ending quote.  I'm not sure how it would
> evidence its smarts if you were actually on the quotation mark in
> question.
>

The quote (", ', `) text objects are already implemented in Vim 7.
Check-out the following help topics:

   :help a"
   :help i"

- Yegappan
Reply | Threaded
Open this post in threaded view
|

Re: % matching double quotes (")

Tim Chase-2
>> On the Todo list, Bram has put in a text-object for quotes
>> (at my request...thanks, Bram!), however these assume you're
>> inside the
[cut]
>
> The quote (", ', `) text objects are already implemented in
> Vim 7.

Well, wow!  Thanks, Bram (and according to the help file, Taro
Muraoka too)!

Browsing through the version7.txt file, it looks like a lot of
hard work has gone into it.  Thanks, all!

-tim