emacs -nw で X とクリップボード共有

emacs -nw で起動したとき、X と クリップボードを共有したい。

ネタもと
Paste number 52379: Use X clipboard in -nw mode
Automatic copy from X11.app to MacOS Clipboard
Sharing the Mac Clipboard with Emacs
http://d.hatena.ne.jp/pakepion/20081209/1228828521

xclip などの外部コマンドを介して、X のクリップボードと内容を共有する。

(defun my-cut-function (text &optional rest)
  (interactive)
  (let ((process-connection-type nil))
    (let ((proc (start-process "xclip" "*Messages*" "xclip")))
      (process-send-string proc text)
      (process-send-eof proc))))

(defun my-paste-function ()
  (interactive)
  (shell-command-to-string "xclip -o"))

(when (and (not window-system)
         (executable-find "xclip"))
  (setq interprogram-cut-function 'my-cut-function)
  (setq interprogram-paste-function 'my-paste-function))