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
;; http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
;; http://www.mail-archive.com/help-gnu-emacs@gnu.org/msg03577.html

(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))

;; http://bbs.archlinux.org/viewtopic.php?id=80226
;; and run the following
;; #autocutsel -fork &
;; #autocutsel -selection PRIMARY -fork &

;; also see http://stackoverflow.com/questions/994563/integrate-readlines-kill-ring-and-the-x11-clipboard
;; which I have configured my .bashrc file

;; http://snarfed.org/emacs_keybindings_in_gnu_screens_copy-scrollback_mode

(provide 'dss-clipboard-integration)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.