]> code.delx.au - gnu-emacs-elpa/blobdiff - xsel.el
multishell - migrate few away from obsolete multishell-buffer-name-history
[gnu-emacs-elpa] / xsel.el
diff --git a/xsel.el b/xsel.el
index ad70110ab8050e9b69bb87afac4b87567aa1c27e..8e958d9040a9f00e9ae59b22e281cb1f12e3f35f 100644 (file)
--- a/xsel.el
+++ b/xsel.el
@@ -1,4 +1,6 @@
-;; 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
 
@@ -12,9 +14,9 @@
 ;;
 ;; 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)
@@ -27,33 +29,33 @@ Returns the resulting value for DISPLAY."
   (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)
       )))