Re: insert space after comma based on context

2006-07-14 Thread A.J.Mechelynck

Zhang Le wrote:

Hi,
   Most of time I want a space after a comma, so I use "imap , , "
   The problem is, sometime I do not want a comma inside square
brackets in some programming language such python and matlab:
a[10,:] or a(10,:)

Is there a way to not insert a space based on context around the
cursor so that if the text before cursor is [xxx, or (xxx, no space
will be inserted?
Any tips?
Zhang Le




I suppose there is: see :help pattern.txt

Then maybe you'll want not to replace it between digits, as in 
123,456,789.01 (decimal notation with thousands, Anglo-Saxon style) or 
123.456.789,01 (the same, Latin style). (BTW, I don't know whether the 
Chinese people use a comma or a period for the decimal point, and I'm 
not sure whether they separate thousands, millions, etc., or maybe 
tens-of-thousands [aka myriads], myriads of myriads, etc., in large 
numbers written with the characters 0123456789).



Best regards,
Tony.


Re: insert space after comma based on context

2006-07-03 Thread Vigil

You can also ctrl-v, which will prevent the , map.

On Thu, 29 Jun 2006, Zhang Le wrote:


Hi,
  Most of time I want a space after a comma, so I use "imap , , "
  The problem is, sometime I do not want a comma inside square
brackets in some programming language such python and matlab:
   a[10,:] or a(10,:)

   Is there a way to not insert a space based on context around the
cursor so that if the text before cursor is [xxx, or (xxx, no space
will be inserted?
Any tips?
Zhang Le



--

.


Re: insert space after comma based on context

2006-07-01 Thread Dr Bean
On Fri, 30 Jun 2006, Luc Hermitte wrote:

> Hello,

> > > He used the function InsertIfNotAfter() I define in my C++ ftplugin
> > > cpp_set.vim available in lh-cpp.tar.gz [1].
> > > In your case, I guess it will look like:

> > > inoremap  ,
> > > \ =InsertIfNotAfter(',', ', ', '[[(][^]]\+')

> > It's a mapping to add a space after the comma, but only when the comma
> > is not preceded by something the regex matches.

> I must admit I never remember which parameter is expanded when.
> If the pattern is matched, we expand the first parameter. The second
> otherwise.

The function, InsertIfNotAfter, is defined as

function! InsertIfNotAfter(key, what, pattern)
  let c = col('.') - 1
  let l = getline('.')
  let l = strpart(l, 0, c)
  if l =~ a:pattern.'\s*$'
return a:key
  else
return Def_AbbrC(a:key, a:what)
  endif
endfunction

I thought that if a:pattern matches, then a:key is not expanded.
And if it is not matched, it is expanded, to a:what.

Oh, wait, I see. This function is part of a mapping. As you said,
if the pattern matches, the comma is expanded to whatever the
first parameter of InsertIfNotAfter is. If it doesn't match, it
is expanded to the second parameter of InsertIfNotAfter.

-- 
Dr Bean  More Pricks than Kicks.
 --Samuel Beckett


Re: insert space after comma based on context

2006-06-30 Thread Luc Hermitte
Hello,

* On Fri, Jun 30, 2006 at 09:03:57AM +0800, Dr Bean <[EMAIL PROTECTED]> wrote:
> > > Is there a way to not insert a space based on context around the
> > > cursor so that if the text before cursor is [xxx, or (xxx, no space
> > > will be inserted?
> 
> > He used the function InsertIfNotAfter() I define in my C++ ftplugin
> > cpp_set.vim available in lh-cpp.tar.gz [1].
> > In your case, I guess it will look like:
> 
> > inoremap  ,
> > \ =InsertIfNotAfter(',', ', ', '[[(][^]]\+')
> 
> Just for my own benefit, and to refresh my memory about what this
> means:
> 
> It's a mapping to add a space after the comma, but only when the comma
> is not preceded by something the regex matches.

I must admit I never remember which parameter is expanded when.
If the pattern is matched, we expand the first parameter. The second
otherwise.

> What the regex matches is an opening bracket or parenthesis followed
> by a number of non-closing-bracket characters.
> 
> Shouldn't the closing parenthesis be included also in the regex?
> '[[(][^])]\+'

Of course. You're right.

-- 
Luc Hermitte
http://hermitte.free.fr/vim/


Re: insert space after comma based on context

2006-06-29 Thread Dr Bean
On Fri, 30 Jun 2006, Luc Hermitte wrote:

> * On Thu, Jun 29, 2006 at 02:45:16PM +0100, Zhang Le <[EMAIL PROTECTED]> 
> wrote:
> > Most of time I want a space after a comma, so I use "imap , , "
> > The problem is, sometime I do not want a comma inside square
> > brackets in some programming language such python and matlab:
> > a[10,:] or a(10,:)

> > Is there a way to not insert a space based on context around the
> > cursor so that if the text before cursor is [xxx, or (xxx, no space
> > will be inserted?

> I see two ways:

> 2- The second is to define context dependent mapping. You can search for
> the discussion I had with Dr Bean last week on this topic.
> You can search for the following Message-ID on gmane:
> <[EMAIL PROTECTED]>

> He used the function InsertIfNotAfter() I define in my C++ ftplugin
> cpp_set.vim available in lh-cpp.tar.gz [1].
> In your case, I guess it will look like:

> inoremap  ,
> \ =InsertIfNotAfter(',', ', ', '[[(][^]]\+')

Just for my own benefit, and to refresh my memory about what this means:

It's a mapping to add a space after the comma, but only when the
comma is not preceded by something the regex matches.

What the regex matches is an opening bracket or parenthesis followed by
a number of non-closing-bracket characters.

Shouldn't the closing parenthesis be included also in the regex?

'[[(][^])]\+'

-- 
Dr Bean People don't know what they say or 
what they think they ought to say.
--Leonard Bloomfield, quoted by Virginia McDavid


Re: insert space after comma based on context

2006-06-29 Thread Luc Hermitte
Hello,

* On Thu, Jun 29, 2006 at 02:45:16PM +0100, Zhang Le <[EMAIL PROTECTED]> wrote:
> Most of time I want a space after a comma, so I use "imap , , "
> The problem is, sometime I do not want a comma inside square
> brackets in some programming language such python and matlab:
> a[10,:] or a(10,:)
> 
> Is there a way to not insert a space based on context around the
> cursor so that if the text before cursor is [xxx, or (xxx, no space
> will be inserted?

I see two ways:
1- The first is to rely on ftplugins and :map-. This way, you
can specify mappings for specific filetypes. However, it won't be able
to distinguish normal commas, from commas used in arrays.

2- The second is to define context dependent mapping. You can search for
the discussion I had with Dr Bean last week on this topic.
You can search for the following Message-ID on gmane:
<[EMAIL PROTECTED]>

He used the function InsertIfNotAfter() I define in my C++ ftplugin
cpp_set.vim available in lh-cpp.tar.gz [1].
In your case, I guess it will look like:

inoremap  ,
\ =InsertIfNotAfter(',', ', ', '[[(][^]]\+')

HTH, 


[1] http://hermitte.free.fr/vim/ressources/lh-cpp.tar.gz
-- 
Luc Hermitte
http://hermitte.free.fr/vim/


insert space after comma based on context

2006-06-29 Thread Zhang Le

Hi,
   Most of time I want a space after a comma, so I use "imap , , "
   The problem is, sometime I do not want a comma inside square
brackets in some programming language such python and matlab:
a[10,:] or a(10,:)

Is there a way to not insert a space based on context around the
cursor so that if the text before cursor is [xxx, or (xxx, no space
will be inserted?
Any tips?
Zhang Le