Tuesday, March 29, 2011

Re: Function to write correctly French quotation marks

On Tue, 29 Mar 2011, Steve wrote:

> Le 29-03-2011, à 02:50:47 -0400, Benjamin R. Haskell a écrit :
>
>>>> Does those two mappings help you?
>>>>
>>>> :imap <expr> " getline('.')[col('.')-2]=~'\S' ? ' »' : '« '
>>>> :imap <expr> ' getline('.')[col('.')-2]=~'\S' ? ' »' : '« '
>>>
>>> [...]
>
>> [...]
>
> I gave it a try, and it works. But:
>
> - I need to press <"> before typing the work and <'> at the end of the
> word, not very convenient if I have to go through a file to correct it
> (in normal mode for instance).

Yes, this solution is not meant for post-processing.


> - I can not use those two keys anymore (I had to select from visual
> mode what you had written, yank it, and then paste it in order to
> write them in this message).

That's what I meant with "You might want a toggle".

E.g.:

==> ~/.vim/plugin/guillemets-toggle.vim <==
let s:mapped = 0
fun! ToggleGuillemets()
if s:mapped
iunmap "
iunmap '
else
imap <expr> " getline('.')[col('.')-2]=~'\S' ? ' »' : '« '
imap <expr> ' getline('.')[col('.')-2]=~'\S' ? ' »' : '« '
endif
let s:mapped = 1 - s:mapped
endfun
nmap <F10> :call ToggleGuillemets()<CR>
imap <F10> <C-\><C-O>:call ToggleGuillemets()<CR>
===========================================

Then <F10> toggles the mappings on or off.


> - it would be easier to place the cursor under a word (or visual
> select several words) and then hit a key to achieve my goal).

You might want something like:

nnoremap <Leader>gu "adi"hc2l«<Char-0xa0><Esc>"apa<Char-0xa0>»<Esc>

It'll work inside a quoted string. Basic idea is to yank out the text
from between two double quotes, using Vim's text objects. Then change
the quotes themselves into «(nbsp)text(nbsp)»

Fully broken down:
nnoremap <Leader>gu "adi"hc2l«<Char-0xa0><Esc>"apa<Char-0xa0>»<Esc>
nnoremap - normal mode mapping, but don't map things on the rhs
<Leader>gu - choose a key binding
"a - use register 'a'
d - delete
i" - inner quote
h - move left one char
c2l - change 2 letters
« - guillemet
<Char-0xa0> - alternate way to enter nbsp
<Esc> - exit insert mode
"a - use register 'a'
p - put text after cursor
a - append
<Char-0xa0> - another nbsp
» - closing guillemet
<Esc> - and finish

And, since email will probably ruin the vertical alignment:
nnoremap - normal mode mapping, but don't map things on the rhs
<Leader>gu - choose a key binding
"a - use register 'a'
d - delete
i" - inner quote
h - move left one char
c2l - change 2 letters
« - guillemet
<Char-0xa0> - alternate way to enter nbsp
<Esc> - exit insert mode
"a - use register 'a'
p - put text after cursor
a - append
<Char-0xa0> - another nbsp
» - closing guillemet
<Esc> - and finish

Most of those pieces should be :help-able. (e.g.:
:help h
:help d
)

> I see that Christian answered too, so I will (can not write single
> quote anymore ;-)) go through his message and come back later.

He had better :h[elp] references.


> Thanks for your reply Ben, very informative. The more I learn vim, the
> more I understand how far I am from mastering it (did not Einstein say
> something like that?)

Practice practice practice. One of the most important things is to
learn how to use the :help system. Vim's built-in :help is extremely
useful and comprehensive.

--
Best,
Ben

--
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: