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

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

[SOLVED] Re: [QUESTION] buffer-substring-no-properties returned string n


From: Christopher M. Miles
Subject: [SOLVED] Re: [QUESTION] buffer-substring-no-properties returned string not display Chinese correctly
Date: Sat, 04 Jun 2022 14:08:18 +0800
User-agent: mu4e 1.6.10; emacs 28.1

Eli Zaretskii <eliz@gnu.org> writes:

>> 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.

Thanks for replying and awesome answer. This solved my problem.

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

Attachment: signature.asc
Description: PGP signature


reply via email to

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