Sunday, March 28, 2010

Re: user function for multiple substitution

On 29/01/10 03:43, bill lam wrote:
> Now I use bash script sed to convert esperanto x-system to unicode
>
> #!/bin/sh
> sed -e "s/cx/ĉ/g" -e "s/gx/ĝ/g" -e "s/hx/ĥ/g" ... ktp
>
> so that :%!xeo to convert whole file or visual hilite region then :!seo
>
> How to define a user function Xeo to do the same thing?
>

Matt gave you a solution in several steps, but here's a single-step one,
taking advantage of both case-matching and case-insensitive operators,
of |sub-replace-expression| and of the ternary operator ?: as in
(condition ? result_if_true : result_if_false) |expr1| :

command -nargs=0 -range=% -bar Xeo
\ <line1>,<line2>s/\c[scujgh]x/\=(
\ submatch(0) ==# 'sx' ? 'ŝ' :
\ submatch(0) ==# 'cx' ? 'ĉ' :
\ submatch(0) ==# 'ux' ? 'ŭ' :
\ submatch(0) ==# 'jx' ? 'ĵ' :
\ submatch(0) ==# 'gx' ? 'ĝ' :
\ submatch(0) ==# 'hx' ? 'ĥ' :
\ submatch(0) ==? 'SX' ? 'Ŝ' :
\ submatch(0) ==? 'CX' ? 'Ĉ' :
\ submatch(0) ==? 'UX' ? 'Ŭ' :
\ submatch(0) ==? 'JX' ? 'Ĵ' :
\ submatch(0) ==? 'GX' ? 'Ĝ' : 'Ĥ' )/g

For maximum efficiency, the most frequent cases should be tested first,
but the use of ==# and ==? to enable (for instance) both Cx and CX for Ĉ
but only cx for ĉ requires lowercase to come first. (This will identify
cX as Ĉ but I think it can be tolerated.)

You may want to add a c flag at the end if you also have, let's say,
proper names with -ux in them.


Best regards,
Tony.
--
"I love Saturday morning cartoons, what classic humour! This is what
entertainment is all about ... Idiots, explosives and falling anvils."
-- Calvin and Hobbes, Bill Watterson

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

No comments: