3

Very similar to this question: Copy text as it is rendered by Emacs but what I need is to coy the ^M, ^E and so on characters as they are displayed. (Otherwise the text in clipboard gets truncated).

  • Not sure what you mean by copy as displayed. What happens when you just copy the chars normally, using, say, M-w? How does what happens differ from what you want/need? – Drew May 15 '17 at 16:00
  • @Drew In Emacs clipboard--the contents stays the same. Other applications will get the content, and will treat it as they know how to treat strings, so, say, for example, if your Emacs puts in clipboards a string that starts with \000 character, then applications reading from clipboard will think that it is empty. – wvxvw May 15 '17 at 17:31
  • I still don't understand what you would like Emacs to do. It sounds like the problem is with the other application and how it renders the chars. And each such other application might render this or that char differently, no? Can you specify the behavior that you would like from Emacs? If not, can you at least give an example - e.g., what does it mean to copy ^M "as displayed"? What char(s) would you like Emacs to put in the clipboard, in place of a ^M char? – Drew May 15 '17 at 18:23
  • @Drew no, it's not a problem with other applications. Well, not in my question that is. I'll try explaining this differently: when Emacs encounters a string in a file that reads ^M it will display it as ^M, when Emacs encounters a string in a file which is a carriage return, it will display it as ^M. What I want is to get Emacs to take the string displayed in the second case and save it as it appears in the file from the first case. – wvxvw May 16 '17 at 3:39
  • I'm still not very clear about what you want. Do you want the Control-M character to be replaced in the clipboard string by the two chars ^ and M? Is that it? (If my questions help at all, maybe clarify a bit in your question, and we can delete the comments?) – Drew May 16 '17 at 14:18
2

It sounds like you are looking for function text-char-description.

(text-char-desription ?^M)

returns the string "^M".

(length (text-char-desription ?^M))

returns 2.

Here, ^M is the single control character except in "^M", where it is the two chars ^ and M.

(This is different from what single-key-description does. For ?^M that returns the string "RET".

C-h f text-char-description:

text-char-description is a built-in function in C source code.

(text-char-description CHARACTER)

Return a pretty description of file-character CHARACTER.

Control characters turn into "^char", etc. This differs from `single-key-description' which turns them into "C-char".

Also, this function recognizes the 2**7 bit as the Meta character, whereas `single-key-description' uses the 2**27 bit for Meta.

See Info node (elisp) Describing Characters for examples.

  • OK, this covers enough for me to write a function to replace all of them. – wvxvw May 17 '17 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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