4

I'm using some VBA scripts which copy Text in MS Outlook from an e-mail message and send it to the clipboard.

If I then paste the clipboard content with Ctrl-Y in Emacs 24.5.1 in Windows 7, the linebreaks are missing and I see a red ^M instead in my emacs buffer.

What can I do to directly insert linebreaks or convert the text in emacs to the "normal" encoding?

My current settings in my .emacs file are

;; file encoding utf-8 preferred
(prefer-coding-system 'utf-8)
(setq coding-system-for-read 'utf-8)
(setq coding-system-for-write 'utf-8)

However: If I copy a text from an e-Mail in MS Outlook to the Clipboard with Ctrl-C and paste it in an emacs-buffer with Ctrl-Y, the linebreaks are ok.

2 Answers 2

5

The linebreaks are missing because there is a conflict between your coding system on the desktop and your emacs setup. Emacs can be configured to have different coding systems for the terminal, keyboard and clipboard.

Try to set your clipboard coding system and selection coding system to utf-16-le, i.e.,:

(if (eq system-type 'windows-nt)
    (progn
      (set-clipboard-coding-system 'utf-16-le)
      (set-selection-coding-system 'utf-16-le))
  (set-selection-coding-system 'utf-8))
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
3
  • 1
    unfortunately, this does not help but makes things even worse, as Umlauts are now also pasted as \xxx code (like \344 instead of ä).
    – p.m
    Commented Jun 3, 2016 at 11:11
  • 1
    Try setting (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
    – aagaard
    Commented Jun 3, 2016 at 11:27
  • unfortunately, this does not help either. :( Any other suggestion?
    – p.m
    Commented Sep 30, 2016 at 12:13
2

Combined with @aagaard 's solution. This worked for me:

(if (eq system-type 'windows-nt)
    (progn
      (set-clipboard-coding-system 'utf-16-le)
      (set-selection-coding-system 'utf-16-le))
  (set-selection-coding-system 'utf-8))
(prefer-coding-system 'utf-8-unix)
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8-unix)
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8-unix)
(setq locale-coding-system 'utf-8-unix)
;; Treat clipboard input as UTF-8 string first; compound text next, etc.
(when (display-graphic-p)
  (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING)))

Full can be found here

1
  • This worked for me in that when I copied the ^M occurrences showed upon first pasting in to a file but if I saved file and then reopened they disappeared. Any thoughts?
    – swihart
    Commented Nov 14, 2023 at 15:43

Your Answer

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

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