5

Suppose I copy some text from a source (e.g. a webpage, a text file opened in an editor such as gedit or emacs) to a buffer in emacs, and then save the buffer to a text file.

  1. When I copy the text from the source to the Emacs buffer,

    Do I actually copy "the code values of the text under the encoding method used by the source" to the Emacs buffer? Or do I copy something else of the text?

    Does emacs need to know the encoding method of the text in the source?

    Does conversion of encoding involve during this copy process?

  2. When Emacs writes the buffer to a text file,

    by default, without customized configurations of emacs, will emacs write the buffer to the text file using the same encoding method as the source does?

1 Answer 1

6

When I copy the text from the source to the emacs buffer ...

What you copy depends upon the application you are copying from. Some applications release UTF-8 encoded strings while others use different formats. Emacs uses the variable selection-coding-system to determine whether to decide the encoding from the data or presume a particular encoding. See the documentation of selection-coding-system for more details. Emacs has its own internal representation utf-8-emacs, so any text insertion has an implicit conversion into this representation.

When emacs write the buffer to a text file...

For an unconfigured emacs, I believe emacs uses select-safe-coding-system to determine a coding system or prompt the user if undecided. Here is an excerpt from the documentation of select-safe-coding-system that determines when emacs prompts for a coding system choice.

Source (Formatting mine)

"If default-coding-system is non-nil, that is the first coding system to try; if that can handle the text, select-safe-coding-system returns that coding system. It can also be a list of coding systems; then the function tries each of them one by one. After trying all of them, it next tries the current buffer's value of buffer-file-coding-system (if it is not undecided), then the default value of buffer-file-coding-system and finally the user's most preferred coding system, which the user can set using the command prefer-coding-system (see Recognizing Coding Systems).

If one of those coding systems can safely encode all the specified text, select-safe-coding-system chooses it and returns it. Otherwise, it asks the user to choose from a list of coding systems which can encode all the text, and returns the user's choice."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.