]> code.delx.au - gnu-emacs-elpa/blob - xsel.el
Copy and paste from emacs tty sessions, using xsel.
[gnu-emacs-elpa] / xsel.el
1 ;; xsel.el -- copy and paste from emacs tty sessions, using xsel
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc. and Ken Manheimer
4
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
6 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
7 ;; Created: 1999 -- first public release
8 ;; Keywords: copy, paste, X11
9 ;; Website: https://github.com/kenmanheimer/EmacsUtils
10
11 ;;; Commentary:
12 ;;
13 ;; If xsel is installed and DISPLAY is working, use `klm:xsel-copy' to copy
14 ;; the region to the X clipboard and `klm:xsel-paste' to paste the contents
15 ;; of the clipboard at point. (The advantage of the latter over just usign
16 ;; X paste into the terminal is `klm:xsel-paste' looks unitary, to emacs,
17 ;; rather than being continuous input.)
18
19
20 (defun klm:xsel-check-get-DISPLAY (&optional arg)
21 "Ensure X DISPLAY is set, and prompt for it if not.
22
23 With universal argument, always prompt to set it, regardless.
24
25 Returns the resulting value for DISPLAY."
26 (interactive "P")
27 (when (or arg (not (getenv "DISPLAY")))
28 (setenv "DISPLAY"
29 (read-from-minibuffer "DISPLAY: "
30 (or (getenv "DISPLAY") "localhost:10.0"))))
31 (getenv "DISPLAY")
32 )
33
34 (defun klm:xsel-copy (from to)
35 "Place contents of region in X copy/paste buffer, using `xsel'."
36 (interactive "r")
37 (when (klm:xsel-check-get-DISPLAY)
38 (let ((temp-file-name (make-temp-file "klm:xsel-copy_"))
39 (content (buffer-substring from to)))
40 (with-temp-file temp-file-name (insert-string content))
41 ;; Didn't get call-process to work, and would have to do error handling.
42 ;; (call-process "/usr/bin/xsel" temp-file-name nil nil
43 ;; "--input" "--clipboard")
44 (shell-command (format "/usr/bin/xsel --input --clipboard < %s"
45 temp-file-name))
46 (delete-file temp-file-name nil)
47 )))
48
49 (defun klm:xsel-paste ()
50 "Place contents of region in X copy/paste buffer, using `xsel'."
51 (interactive "")
52 (when (klm:xsel-check-get-DISPLAY)
53 (let (contents)
54 (shell-command "/usr/bin/xsel --output --clipboard")
55 (save-current-buffer
56 (set-buffer "*Shell Command Output*")
57 (setq contents (buffer-substring (point-min)(point-max))))
58 (insert-string contents)
59 )))