简体   繁体   中英

Emacs showing ^M in a process buffer

At the moment, I have a process-buffer which is utf-8-auto (emacs modeline reports the buffer as utf-8-auto-dos ) with CRLF style newlines. When I write multi-line text into the buffer via a process-send-region or process-send-string each line is suffixed with ^M .

What makes this problem odd is that text written to the process-buffer directly from the process, does not contain ^M 's.

It doesn't seem to make any difference where the source text comes from, in fact, even a multi-line region marked and sent that already appears in the process buffer (that doesn't contain ^M ) will have them when sent.

( Note the source text for the process-send-region will always come from a Emacs buffer, process-send-string, when multi-line will be from the Windows clipboard interface to the killring, or again from an Emacs buffer to killring.)

I should also add that the incoming text to the buffer is parsed by a after-change-functions hook (to do some colorisation based on input) so a last resort I'd do an additional regexp-replace-in-string on this incoming text as part of that hook function, I'd like to avoid that because it seems wrong , but I'll add it as a hacky solution if nothing else works.

Addendum

I updated the encoding settings for the buffer and the process to use utf-8-dos instead of utf-8-auto and the ^M 's vanished.

So in the buffer setup part of my app, I did...

(switch-to-buffer "sock-buffer")
(set-process-coding-system (get-process sock-process) 'utf-8-dos 'utf-8-dos)
(set-buffer-file-coding-system 'utf-8-dos nil)
(set-buffer-process-coding-system 'utf-8-dos 'utf-8-dos)

Then reduced this to just...

(switch-to-buffer "sock-buffer")
(set-buffer-process-coding-system 'utf-8-dos 'utf-8-dos)

And everything worked fine.

This is because those files are in DOS/Windows line endings. You can use Cx [Enter] f unix [Enter] to convert them to the Unix encoding.

^L is a page break. I've seen them some times to separate different parts of source code (for old-fashioned listings in a text printer), or in text documentation to insert an actual "new page" command.

As of the update, here you can see that you have to select set-process-coding-system to the correct coding system.

Alternately to the dos2unix approach, you could use one of the MULE commands in Emacs, or (my favorite), since these characters are mistakenly treated as part of the text, you can replace them using the command to replace a string in the text: M-% Cq CM RETURN

M-% is the query-replace command.

Cq means "let me type the next character without interpreting it as the RETURN key".

我相信您会因为换行不一致(例如Windows换行与* nux的不一致)而看到这些,您应该尝试使用dos2unix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM