Feedback on function converting latin1 to utf-8

0 views
Skip to first unread message

Markus Fischer

unread,
Jan 12, 2008, 9:57:35 AM1/12/08
to vim...@googlegroups.com
Hi,

I've written a simple function to convert latin1 to utf-8 and vice versa and
hooked it up to a menu. It works well and there's nothing to complain about
from my side, but I thought I give it a shot here if people spot something to
improve.

function! Mfn_Buffer_Recode(from, to)
if &encoding != a:from
echoerr "Current file is encoded in " . &encoding .
\ ", can't convert from " . a:from . " to " . a:to . " here"
return
endif
let i = 1
for line in getline(1, '$')
call setline(i, iconv(line, a:from, a:to))
let i = i + 1
endfor
exec 'set encoding=' . a:to
endfunction
menu &Mfn.&latin1\ to\ utf-8 : call Mfn_Buffer_Recode('latin1', 'utf-8')<CR>
menu &Mfn.&utf-8\ to\ latin1 : call Mfn_Buffer_Recode('utf-8', 'latin1')<CR>

It's vim7 only and I've this wrapped in a version check block.

thanks,
- Markus

Reply all
Reply to author
Forward
0 new messages