- /* Determine the file's encoding and update sw->encoding. (The ordering is
- important here because encoding_guess_whole_file() often returns its
- argument instead of a copy of it.) */
- encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
- len_locale));
- g_free (sw->encoding);
- sw->encoding = encoding;
+ if (!encoding || !encoding[0])
+ {
+ /* Determine the file's encoding and update sw->encoding. (The ordering
+ is important here because encoding_guess_whole_file() often returns
+ its argument instead of a copy of it.) */
+ char *guessed_encoding;
+
+ guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
+ text_locale,
+ len_locale));
+ g_free (sw->encoding);
+ sw->encoding = guessed_encoding;
+ }
+ else
+ {
+ g_free (sw->encoding);
+ sw->encoding = g_strdup (encoding);
+ }