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

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • 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 03: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
  • BTW, for display of control chars (which is only about display by Emacs and not conversion in a string), see variable `ctl-arrow`. It won't help you here, but it will let you know how/why/when Emacs shows control chars using `^`. – Drew May 16 '17 at 14:19
  • @Drew close, but not 100%. My specific use-case is related to clipboard, but if it was possible to convert the string with special characters into one where special characters are replaced by their visual representation. – wvxvw May 16 '17 at 15:17

1 Answers1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225