emacs.d / dss-clipboard-integration.el

(require 'browse-kill-ring)
(require 'dss-elisp-funcs) ; dss/file-to-string

(setq x-select-enable-clipboard t)

;; Ideas and code stolen from

(defun dss/xsel-cut-function (text &optional push)
  ;; Insert text to temp-buffer, and "send" content to xsel stdin
  (with-current-buffer "*scratch*"
      (insert text)
      (call-process-region (point-min) (point-max) "xsel" nil 0 nil "--input" "-p" "--clipboard"))))

(defun dss/xsel-paste-function()
  ;; Find out what is current selection by xsel. If it is different
  ;; from the top of the kill-ring (car kill-ring), then return
  ;; it. Else, nil is returned, so whatever is in the top of the
  ;; kill-ring will be used.
  (let ((xsel-output (dss/local-shell-command-to-string "xsel --clipboard --output")))
    (unless (or (string-match-p "^xsel: Can't open display" xsel-output)
                (string= (car kill-ring) xsel-output))

(defun dss/x-display-sync ()
  (setenv "DISPLAY"
          (or (dss/file-to-string "~/.last-display") ":0")))

(defun dss/osx-cut-function (text &optional push)
    (insert text)
    (call-process-region (point-min) (point-max) "mac_copy" nil 0 nil)))

(defun dss/osx-paste-function()
  (let ((pb (dss/local-shell-command-to-string "mac_paste")))
    (unless (string= (car kill-ring) pb)

(defun dss/tty-x-clipboard-init ()
  (unless window-system
    (unless (getenv "DISPLAY")
    (setq interprogram-cut-function 'dss/xsel-cut-function)
    (setq interprogram-paste-function 'dss/xsel-paste-function)))

(defun dss/tty-x-clipboard-disable ()
  (setq interprogram-cut-function nil)
  (setq interprogram-paste-function nil))

(defun dss/osx-clipboard-enable ()
  (setq interprogram-cut-function #'dss/osx-cut-function)
  (setq interprogram-paste-function #'dss/osx-paste-function))

;; and run the following
;; #autocutsel -fork &
;; #autocutsel -selection PRIMARY -fork &

;; also see
;; which I have configured my .bashrc file


(provide 'dss-clipboard-integration)