1

I have a lengthy xml output from a rest endpoint, I copied if from chrome dev tools to clipboard and on pasting it into an org mode buffer inside an xml source block freezes emacs for a while.

I guess this is because of emacs trying to syntax highlight the xml data.

Pasting the same data with mode changed to fundamental mode works without any issue.

How can I advice emacs to not parse long lines while pasting from clipboard.

  • If you could provide an example of a lengthy xml file which produces this problem, probable more people can help. – Name Jan 20 '18 at 20:22
1

https://github.com/rolandwalker/simpleclip

It uses either Emacs internal API or third party CLI tool to insert the text. So syntax highlighting thing is skipped.

You can use simpleclip's default setup. But I recommend re-assign a more convenient key for pasting.

simpleclip doesn't work in certain scenario. See https://github.com/rolandwalker/simpleclip/issues/6 for details.

So my current solution combines simpleclip and my own code. It covers more scenarios than simpleclip (For example, access Windows 10 clipboard inside Linux Subsystem).

Insert below code into ~/.emacs and run M-x paste-from-x-clipboard to paste from clipboard:

(unless (featurep 'simpleclip) (require 'simpleclip))
(defun test-simpleclip ()
  (unwind-protect
      (let (retval)
        (condition-case ex
            (progn
              (simpleclip-set-contents "testsimpleclip!")
              (setq retval
                    (string= "testsimpleclip!"
                             (simpleclip-get-contents))))
          ('error
           (message (format "Please install %s to support clipboard from terminal."
                            (cond
                             (or (eq system-type 'gnu/linux)
                                 (eq system-type 'linux)
                                 (eq system-type 'usg-unix-v)
                                 (eq system-type 'berkeley-unix))
                              "xsel")
                             ((or (eq system-type 'cygwin) (eq system-type 'windows-nt))
                              "cygutils-extra from Cygwin")
                             (t
                              "CLI clipboard tools"))))
           (setq retval nil)))
        retval)))
(setq simpleclip-works (test-simpleclip))

(defun my-gclip ()
  (cond
   (simpleclip-works
    (simpleclip-get-contents))
   ((eq system-type 'darwin)
    (with-output-to-string
      (with-current-buffer standard-output
        (call-process "/usr/bin/pbpaste" nil t nil "-Prefer" "txt"))))
   ((eq system-type 'cygwin)
    (with-output-to-string
      (with-current-buffer standard-output
        (call-process "getclip" nil t nil))))
   ((memq system-type '(gnu gnu/linux gnu/kfreebsd))
    (let* ((powershell-program (executable-find "powershell.exe")))
           (cond
            (powershell-program
             ;; PowerLine adds extra white space character at the end of text
             (string-trim-right
              (with-output-to-string
                (with-current-buffer standard-output
                  (call-process powershell-program nil t nil "-command" "Get-Clipboard")))))
            (t
             (with-output-to-string
               (with-current-buffer standard-output
                 (call-process "xsel" nil t nil "--clipboard" "--output")))))))))

(defun paste-from-x-clipboard()
  "Paste from clipboard."
  (interactive "P")
  ;; paste after the cursor in evil normal state
  (when (and (functionp 'evil-normal-state-p)
             (functionp 'evil-move-cursor-back)
             (evil-normal-state-p)
             (not (eolp))
             (not (eobp)))
    (forward-char))
  (insert (my-gclip)))

The setup I post here is simplified version. See https://github.com/wangchen/redguardtoo-emacs.d/blob/master/lisp/init-clipboard.el for my real world setup.

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.