bug#81606: Mojibake with UTF-8 file names

Arash Esbati <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
Eli Zaretskii <[email protected]> writes:

> So this can only work if the encoding of the main file's name is
> easily determined from its byte sequence.  The patch below tries to
> do the best we can.  It is still not 100% reliable: if the default
> locale's encoding is not UTF-8, there are chances it will still not
> work.  But I don't think there's a solution in that case, except by
> providing the \inputencoding directive or the coding: cookie.

I think the above is a safe assumption.  This is an excerpt from LaTeX
news for release 2018-04-01:

  UTF-8: the new default input encoding
  [...]
  With this release, the default encoding for LaTeX files has been
  changed from the ``fall through raw'' encoding to UTF-8 if used with
  classic TeX or pdfTeX. The implementation is essentially the same as
  the existing UTF-8 support from \usepackage[utf8]{inputenc}.  The
  LuaTeX and XeTeX engines always supported the UTF-8 encoding as their
  native (and only) input encoding, so with these engines inputenc was
  always a no-op.

  This means that with new documents one can assume UTF-8 input and it
  is no longer required to always specify
  \usepackage[utf8]{inputenc}. But if this line is present it will not
  hurt either.  [...]

  Only documents that have been stored in a legacy encoding and used
  accented letters from the keyboard without loading inputenc (relying
  on the similarities between the input used and the T1 font encoding)
  are affected.

  These documents will now generate an error that they contain invalid
  UTF-8 sequences.  [...]

  Possible alternatives are reencoding the file to UTF-8 using a tool
  (such as recode or iconv or an editor) or adding the line
  \usepackage[<encoding>]{inputenc}
  to the preamble specifying the <encoding> that fits the file encoding.

IIUC, LaTeX users with old documents are now forced to touch their
files, and with your patch below, it should cover most cases, I think.

> Why not simply add a coding: cookie to the particular file?  It's
> simpler, and doesn't affect other TeX/LaTeX files.

It was only a suggestion, coding: cookie is of course a solution.

> Here's the patch I can suggest:
>
> diff --git a/lisp/international/latexenc.el b/lisp/international/latexenc.el
> index 6e0cde6..1e0f8e7 100644
> --- a/lisp/international/latexenc.el
> +++ b/lisp/international/latexenc.el
> @@ -160,6 +160,8 @@ latexenc-find-file-coding-system
>                                       (stringp TeX-master)
>                                       TeX-master)
>                                  (bound-and-true-p tex-main-file)))))
> +                ;; Decode the main file name as best as we can...
> +                (setq file (decode-coding-string file 'undecided))
>                  (dolist (ext `("" ,(if (boundp 'TeX-default-extension)
>                                         (concat "." TeX-default-extension)
>                                       "")

Thanks, I tested it and it works.

Best, Arash
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.