# Embracing and debracing Greek passages

I need two commands: one to find all occurences of \gr{...} and remove the \gr{ and }, leaving the contents ..., which should work accross lines; and another to find all strings consisting of Greek characters (unicode ranges 0x0370--0x03ff and 0x1f00--0x1fff), AND non-Latin punctuation characters, AND TeX commands (words precedes by \), (the last is not terribly importaint, as there will be few places to correct manually without it), and embrace them with \gr{ and }.

This is what I have tried:

 " Greek and Coptic or Greek Extended
let g = '[\u370-\u3ff\u1f00-\u1fff]'
let G = '[^\u370-\u3ff\u1f00-\u1fff]'
let GL = '[^\u370-\u3ff\u1f00-\u1fffA-Za-z]'
let ACn = '%([^A-Za-z%]|\n)'
" replacement string
let r1 = '\1\\gr{\2}\3'
let r = r1

let p3 = '\v%(^|(\a'.GL.'*))('.g.ACn.'*'.g.')(\A*\w)'
let p = p3

let tgr3 = 'v/\\in\=de\=x/s/'.p.'/'.r.'/g'
let tgr = tgr3

command! GrClear %s/\v\\gr\{(\_[^}]*)\}/\1/g
command! GrInsert norm ,<c-r>=tgr<cr><cr>


It works to an extent, but not great. Any pointers in the right direction will be greatly appreciated.---And I do apologize that this attempt is poorly explained, and that I haven't explained exactly what the fail cases are: it's been a while since I did this, and I just wanted to see if this might be familiar and simple to someone; otherwise, ignore it and I will post a better question when I have more time. Thanks.

PS. I am now using

#!/usr/bin/perl
binmode STDIN,  ":utf8";
binmode STDOUT, ":utf8";
my $g=qr/[\x{0370}-\x{03FF}\x{1F00}-\x{1FFF}]/; my$L=qr/[^a-zA-Z]/;
while (<>)
{
s/( (?: $g$L* $g) )/\\gr{$1}/xg;
print
}


which must be called with the cmd-line option -0. this works fine except a minor aesthetic problem: the pattern doesn't know whether a bracket at the end of it belongs inside or not.

:set lazyredraw