-;; xsel.el -- copy and paste from emacs tty sessions, using xsel
+;; xsel.el -- X copy and paste emacs region from emacs tty sessions, using xsel
+
+;; TODO: Check alternative: http://emacs.stackexchange.com/a/819/9668
;; Copyright (C) 2015 Free Software Foundation, Inc. and Ken Manheimer
;;
;; If xsel is installed and DISPLAY is working, use `klm:xsel-copy' to copy
;; the region to the X clipboard and `klm:xsel-paste' to paste the contents
-;; of the clipboard at point. (The advantage of the latter over just usign
-;; X paste into the terminal is `klm:xsel-paste' looks unitary, to emacs,
-;; rather than being continuous input.)
+;; of the clipboard at point. (The advantage of the latter over regular X
+;; mouse paste is `klm:xsel-paste' looks unitary, to emacs, rather than
+;; the mouse paste's continuous, parsed/indented/auto-parenned/etc input.)
(defun klm:xsel-check-get-DISPLAY (&optional arg)
(when (or arg (not (getenv "DISPLAY")))
(setenv "DISPLAY"
(read-from-minibuffer "DISPLAY: "
- (or (getenv "DISPLAY") "localhost:10.0"))))
+ (or (getenv "DISPLAY") ":10.0"))))
(getenv "DISPLAY")
)
(defun klm:xsel-copy (from to)
- "Place contents of region in X copy/paste buffer, using `xsel'."
+ "Place contents of region in X copy/paste buffer, using shell command.
+
+With universal argument, prompt to set DISPLAY."
+
(interactive "r")
- (when (klm:xsel-check-get-DISPLAY)
- (let ((temp-file-name (make-temp-file "klm:xsel-copy_"))
- (content (buffer-substring from to)))
- (with-temp-file temp-file-name (insert-string content))
- ;; Didn't get call-process to work, and would have to do error handling.
- ;; (call-process "/usr/bin/xsel" temp-file-name nil nil
- ;; "--input" "--clipboard")
- (shell-command (format "/usr/bin/xsel --input --clipboard < %s"
- temp-file-name))
- (delete-file temp-file-name nil)
+ (when (klm:xsel-check-get-DISPLAY current-prefix-arg)
+ (let ((command (cond ((eq system-type 'darwin) "pbcopy")
+ ((eq system-type 'cygwin) "putclip")
+ ;; Linux &c:
+ (t "xsel --input --clipboard"))))
+ (shell-command-on-region from to command)
+ (deactivate-mark)
)))
(defun klm:xsel-paste ()
- "Place contents of region in X copy/paste buffer, using `xsel'."
+ "Place contents of region in X copy/paste buffer, using shell command."
(interactive "")
- (when (klm:xsel-check-get-DISPLAY)
- (let (contents)
- (shell-command "/usr/bin/xsel --output --clipboard")
- (save-current-buffer
- (set-buffer "*Shell Command Output*")
- (setq contents (buffer-substring (point-min)(point-max))))
- (insert-string contents)
+ (when (klm:xsel-check-get-DISPLAY current-prefix-arg)
+ (let ((command (cond ((eq system-type 'darwin) "pbpaste")
+ ((eq system-type 'cygwin) "getclip")
+ ;; Linux &c:
+ (t "xsel --output --clipboard"))))
+ (shell-command command 1)
+ (exchange-point-and-mark)
)))