help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [QUESTION] buffer-substring-no-properties returned string not displa


From: Eli Zaretskii
Subject: Re: [QUESTION] buffer-substring-no-properties returned string not display Chinese correctly
Date: Fri, 03 Jun 2022 09:34:51 +0300

> From: "Christopher M. Miles" <numbchild@gmail.com>
> Date: Fri, 03 Jun 2022 12:38:03 +0800
> 
> When I use this code to read a file content which contains Chinese text.
> The returned string does not displaying Chinese correctly.
> 
> Here is the code snippet:
> 
> #+begin_src emacs-lisp
> (defun org-link-beautify--preview-text-file (file lines)
>   "Return first LINES of FILE."
>   (with-temp-buffer
>     (condition-case nil
>         (progn
>           (insert-file-contents-literally file)
>           (format "%s\n"
>                   (mapconcat
>                    'concat
>                    ;; extract lines of file contents
>                    (cl-loop repeat lines
>                             unless (eobp)
>                             collect (prog1 (buffer-substring-no-properties ; 
> FIXME: the encoding of Chinese text is wrong.
>                                             (line-beginning-position)
>                                             (line-end-position))
>                                       (forward-line 1)))
>                    "\n")))
>       (file-error
>        ;; XXX: reference to free variable ‘noerror’.
>        (funcall (if noerror #'message #'user-error)
>                       "Unable to read file %S"
>                       file)
>          nil))))
> 
> (org-link-beautify--preview-text-file
>  (expand-file-name "~/Code/Emacs/org-link-beautify/README.org")
>  5)
> #+end_src
> 
> Here is the file content:
> 
> #+begin_src org :file "README.org"
> ,* Motivation
> 
> 美化并预览 Org Mode 的链接。支持一些文件的缩略图,预览,等特性。

You use insert-file-contents-literally, so Emacs doesn't decode the
non-ASCII characters it reads from the file, i.e. it doesn't interpret
the byte sequences as Chinese characters.  Use insert-file-contents
instead.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]