Emacs 22 'feature' - Can Vim do this?

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

Emacs 22 'feature' - Can Vim do this?

Mark Woodward-4
Hi all,

came across this [Emacs] link the other day and wondered if Vim
can do this?
http://steve-yegge.blogspot.com/

in summary...

change these to Bob, Sue, Ralph etc (capitalised)

bob
sue
ralph
alice
jimmy
preston
billy joe jim bob

:s/\w\+/\u\0/g

capitalise last letter (eg boB)

:'<,'>s/\(\w\+\)\(\w\)/\1\u\2/g



change each of these to getFather(), getMother() etc

public Relative father() { return this.father; }
public Relative mother() { return this.mother; }
public Relative sister() { return this.sister; }
public Relative brother() { return this.brother; }
public Relative auntie() { return this.auntie; }
public Relative uncle() { return this.uncle; }


:'<,'>s/public Relative\zs\(\w\)/get\U\1/g



1. change these to number order starting at 1 (1, 2, 3, 4 etc)
2. change these to alpha list (a, b, c, etc)
[This one has me stumped although I'm sure I've seen something
along these lines before. ? something to do with sub-replace-special
and submatch?]


1987:Bogotá
5243:Fabergé
9772:Mallarmé
12044:Paraná
12499:Poincaré
16956:abbé
19923:appliqué
20932:attaché
23704:blasé
26223:café
26511:canapé
29314:cliché
31431:consommé
38981:décolleté
42995:fiancé
43623:flambé
44996:frappé
48317:habitué
58328:macramé
58898:manqué
62514:naiveté
65243:outré
66710:passé
71609:protégé
73675:recherché
76387:risqué
76847:roué
77811:sauté
82455:soufflé
89055:touché
96268:émigré
96274:études


any hints?,


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

Re: Emacs 22 'feature' - Can Vim do this?

Jürgen Krämer

Hi,

Mark Woodward wrote:
>
> came across this [Emacs] link the other day and wondered if Vim
> can do this?
> http://steve-yegge.blogspot.com/

[snip]

> 1. change these to number order starting at 1 (1, 2, 3, 4 etc)
> 2. change these to alpha list (a, b, c, etc)
> [This one has me stumped although I'm sure I've seen something
> along these lines before. ? something to do with sub-replace-special
> and submatch?]
>
>
> 1987:Bogotá
> 5243:Fabergé
> 9772:Mallarmé
> 12044:Paraná
> 12499:Poincaré
> 16956:abbé
> 19923:appliqué
> 20932:attaché
> 23704:blasé
> 26223:café
> 26511:canapé
> 29314:cliché
> 31431:consommé
> 38981:décolleté
> 42995:fiancé
> 43623:flambé
> 44996:frappé
> 48317:habitué
> 58328:macramé
> 58898:manqué
> 62514:naiveté
> 65243:outré
> 66710:passé
> 71609:protégé
> 73675:recherché
> 76387:risqué
> 76847:roué
> 77811:sauté
> 82455:soufflé
> 89055:touché
> 96268:émigré
> 96274:études
>
>
> any hints?,

1.) if this text starts at line 1

  %s/^\d\+:/\=line('.') . '.) '/

2.) for at most 26 lines

  %s/^\d\+:/\=nr2char(char2nr('a') + line('.') - 1) . ') '/

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

Reply | Threaded
Open this post in threaded view
|

Re: Emacs 22 'feature' - Can Vim do this?

Mark Woodward-4
Thanks Jürgen,

On Fri, 11 Aug 2006 11:54:50 +0200
Jürgen Krämer <[hidden email]> wrote:

snip...

> 1.) if this text starts at line 1
>
>   %s/^\d\+:/\=line('.') . '.) '/
>
> 2.) for at most 26 lines
>
>   %s/^\d\+:/\=nr2char(char2nr('a') + line('.') - 1) . ') '/
>
> Regards,
> Jürgen


Is is possible where text starts on a line other than 1?

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

Re: Emacs 22 'feature' - Can Vim do this?

Jürgen Krämer

Hi,

Mark Woodward wrote:

> Thanks Jürgen,
>
> On Fri, 11 Aug 2006 11:54:50 +0200
> Jürgen Krämer <[hidden email]> wrote:
>
> snip...
>
>> 1.) if this text starts at line 1
>>
>>   %s/^\d\+:/\=line('.') . '.) '/
>>
>> 2.) for at most 26 lines
>>
>>   %s/^\d\+:/\=nr2char(char2nr('a') + line('.') - 1) . ') '/
>>
>> Regards,
>> Jürgen
>
>
> Is is possible where text starts on a line other than 1?

subtract the line number and add 1, e.g.,

  50,100s/^\d\+:/\=(line('.') - 50 + 1)  . '.) '/

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

Reply | Threaded
Open this post in threaded view
|

Re: Emacs 22 'feature' - Can Vim do this?

A.J.Mechelynck
In reply to this post by Mark Woodward-4
Mark Woodward wrote:

> Hi all,
>
> came across this [Emacs] link the other day and wondered if Vim
> can do this?
> http://steve-yegge.blogspot.com/
>
> in summary...
>
> change these to Bob, Sue, Ralph etc (capitalised)
>
> bob
> sue
> ralph
> alice
> jimmy
> preston
> billy joe jim bob
>
> :s/\w\+/\u\0/g
>
> capitalise last letter (eg boB)
>
> :'<,'>s/\(\w\+\)\(\w\)/\1\u\2/g
>
>
>
> change each of these to getFather(), getMother() etc
>
> public Relative father() { return this.father; }
> public Relative mother() { return this.mother; }
> public Relative sister() { return this.sister; }
> public Relative brother() { return this.brother; }
> public Relative auntie() { return this.auntie; }
> public Relative uncle() { return this.uncle; }
>
>
> :'<,'>s/public Relative\zs\(\w\)/get\U\1/g
>
>
>
> 1. change these to number order starting at 1 (1, 2, 3, 4 etc)
> 2. change these to alpha list (a, b, c, etc)
> [This one has me stumped although I'm sure I've seen something
> along these lines before. ? something to do with sub-replace-special
> and submatch?]
>
>
> 1987:Bogotá
> 5243:Fabergé
> 9772:Mallarmé
> 12044:Paraná
> 12499:Poincaré
> 16956:abbé
> 19923:appliqué
> 20932:attaché
> 23704:blasé
> 26223:café
> 26511:canapé
> 29314:cliché
> 31431:consommé
> 38981:décolleté
> 42995:fiancé
> 43623:flambé
> 44996:frappé
> 48317:habitué
> 58328:macramé
> 58898:manqué
> 62514:naiveté
> 65243:outré
> 66710:passé
> 71609:protégé
> 73675:recherché
> 76387:risqué
> 76847:roué
> 77811:sauté
> 82455:soufflé
> 89055:touché
> 96268:émigré
> 96274:études
>
>
> any hints?,
>
>

Since only acute accents are involved, and only on a or e, we just need
to replace á by a and é by e. The easiest is to do it in two passes:

        :1,$s/á/a/g
        :1,$s/é/e/g

If you want to do it in a single pass, and maybe for any possible
accents, it might be possible with a "pure" regexp, but I believe a
function would be simpler, and maybe even faster (but I'm less sure
about the latter):

        :1,$s/[\x80-\xFF]/\=RemoveAccents(submatch(0))/g

where, prior to using the above substitute, we would have defined the
following:

        let s:accentsDict = {
        \ "á" : "a",
        \ "â" : "a",
        \ "à" : "a",
        \ "ä" : "ae",
        \ "ã" : "a",
        \ "æ" : "ae",
        \ "å" : "aa"
        \ "ç" : "c",
        \ "ð" : "dh"
        \ "é" : "e",
        \ "ê" : "e",
        \ "è" : "e",
        \ "ë" : "e",
        \ "í' : "i",
        \ "î" : "i",
        \ "ì" : "i",
        \ "ï" : "i",
        \ "ñ" : "ny"
        \ "ó" : "o",
        \ "ô" : "o",
        \ "ò" : "o",
        \ "ö" : "oe",
        \ "õ" : "o",
        \ "ø" : "oe",
        \ "ß" : "ss",
        \ "þ" : "th",
        \ "ú" : "u",
        \ "û" : "u",
        \ "ù" : "u",
        \ "ü" : "ue",
        \ "ÿ" : "y",

... similarly for uppercase ...

        \ }
        function RemoveAccents(byte)
                if has_key(s:accentsDict, a:byte)
                        return s:accentsDict[a:byte]
                else
                        return a:byte
                endif
        endfunction

For speed, we don't change bytes lower than 0x80 (which don't have
accents) and we use a Dictionary (which is set up once, when
initialising the script) rather than a set of if... elseif... etc.
However, Dictionaries are only possible with Vim 7 or later. Note also
that the "keys" of the Dictionary must be single characters but the
"values" don't have to. I have tried to mention all lowercase accented
letters present in Latin1, plus ae, o-bar, edh, thorn and eszet, which
are also part of Latin1 and present in some national alphabets, but not
in 7-bit US-ASCII. If I forgot any, they can easily be added.


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Emacs 22 'feature' - Can Vim do this?

A.J.Mechelynck
A.J.Mechelynck wrote:
> Mark Woodward wrote:
[...]
>

[...]

Oops, I misread your post. I thought you wanted to remove the accents;
apparently you wanted to renumber the items. But maybe my approach can
help someone else in another circumstance.


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Emacs 22 'feature' - Can Vim do this?

Charles E Campbell Jr
In reply to this post by A.J.Mechelynck
 > 1. change these to number order starting at 1 (1, 2, 3, 4 etc)
 > 2. change these to alpha list (a, b, c, etc)
 > [This one has me stumped although I'm sure I've seen something
 > along these lines before. ? something to do with sub-replace-special
 > and submatch?]
 >
 >
 > 1987:Bogotá
 > 5243:Fabergé
 > 9772:Mallarmé
 > 12044:Paraná
 > 12499:Poincaré
 > 16956:abbé
 > 19923:appliqué
 > 20932:attaché
 > 23704:blasé
 > 26223:café
 > 26511:canapé
 > 29314:cliché
 > 31431:consommé
 > 38981:décolleté
 > 42995:fiancé
 > 43623:flambé
 > 44996:frappé
 > 48317:habitué
 > 58328:macramé
 > 58898:manqué
 > 62514:naiveté
 > 65243:outré
 > 66710:passé
 > 71609:protégé
 > 73675:recherché
 > 76387:risqué
 > 76847:roué
 > 77811:sauté
 > 82455:soufflé
 > 89055:touché
 > 96268:émigré
 > 96274:études
 >
 > any hints?,

The following shows how I'd do it...

  1. Line up the colons:
      (use V to visually select the region) \t:
  2. Manually change  (the \t: will have inserted the spaces)
       1987 : Bogotá
     to
          1 : Bogotá
  3. Visually select the numbers in the column (using ctrl-v and motion),
     then press
        :I

To do this I would have used:

   Align and AlignMaps:
http://vim.sourceforge.net/scripts/script.php?script_id=294
   VisIncr            :
http://vim.sourceforge.net/scripts/script.php?script_id=670

To do the alphabetic "numbering", VisIncr supports the ":IA" command
(use it instead of the ":I" above)

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: Emacs 22 'feature' - Can Vim do this?

William Pursell
In reply to this post by Jürgen Krämer
Jürgen Krämer wrote:

> Mark Woodward wrote:
>
>>came across this [Emacs] link the other day and wondered if Vim
>>can do this?
>>http://steve-yegge.blogspot.com/
> [snip]
>>1. change these to number order starting at 1 (1, 2, 3, 4 etc)
>>2. change these to alpha list (a, b, c, etc)
>>[This one has me stumped although I'm sure I've seen something
>>along these lines before. ? something to do with sub-replace-special
>>and submatch?]
>>
>>
>>1987:Bogotá
>>5243:Fabergé
>>9772:Mallarmé
>>12044:Paraná
>>12499:Poincaré
<snip>

>>any hints?,
>
>
> 1.) if this text starts at line 1
>
>   %s/^\d\+:/\=line('.') . '.) '/
>
> 2.) for at most 26 lines
>
>   %s/^\d\+:/\=nr2char(char2nr('a') + line('.') - 1) . ') '/

I've never spent much time learning the syntax for this
sort of thing, and I'm very curious about the motivation
for it.  For example, for these 2 cases, I would simply
filter the text through sort  (ie, to get the
list sorted alphabetically, assuming the entire
file was the list and the cursor was on the
first line, I'd do:  !Gsort -t ':' -k 2)

I suppose if I were working in an environment
that didn't have good external filters, I'd need to
learn to do this sort of text manipulation directly
from the editor, but I'm not sure I really grok the
point.  Is it primarily to cover the situation where
external filters aren't available?