Fix encoding in the source editor

Fixes #591, #508, #427, #543