]> code.delx.au - gnu-emacs-elpa/blob - xsel.el
multishell - migrate few away from obsolete multishell-buffer-name-history
[gnu-emacs-elpa] / xsel.el
1 ;; xsel.el -- X copy and paste emacs region from emacs tty sessions, using xsel
2
3 ;; TODO: Check alternative: http://emacs.stackexchange.com/a/819/9668
4
5 ;; Copyright (C) 2015 Free Software Foundation, Inc. and Ken Manheimer
6
7 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
8 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
9 ;; Created: 1999 -- first public release
10 ;; Keywords: copy, paste, X11
11 ;; Website: https://github.com/kenmanheimer/EmacsUtils
12
13 ;;; Commentary:
14 ;;
15 ;; If xsel is installed and DISPLAY is working, use `klm:xsel-copy' to copy
16 ;; the region to the X clipboard and `klm:xsel-paste' to paste the contents
17 ;; of the clipboard at point. (The advantage of the latter over regular X
18 ;; mouse paste is `klm:xsel-paste' looks unitary, to emacs, rather than
19 ;; the mouse paste's continuous, parsed/indented/auto-parenned/etc input.)
20
21
22 (defun klm:xsel-check-get-DISPLAY (&optional arg)
23 "Ensure X DISPLAY is set, and prompt for it if not.
24
25 With universal argument, always prompt to set it, regardless.
26
27 Returns the resulting value for DISPLAY."
28 (interactive "P")
29 (when (or arg (not (getenv "DISPLAY")))
30 (setenv "DISPLAY"
31 (read-from-minibuffer "DISPLAY: "
32 (or (getenv "DISPLAY") ":10.0"))))
33 (getenv "DISPLAY")
34 )
35
36 (defun klm:xsel-copy (from to)
37 "Place contents of region in X copy/paste buffer, using shell command.
38
39 With universal argument, prompt to set DISPLAY."
40
41 (interactive "r")
42 (when (klm:xsel-check-get-DISPLAY current-prefix-arg)
43 (let ((command (cond ((eq system-type 'darwin) "pbcopy")
44 ((eq system-type 'cygwin) "putclip")
45 ;; Linux &c:
46 (t "xsel --input --clipboard"))))
47 (shell-command-on-region from to command)
48 (deactivate-mark)
49 )))
50
51 (defun klm:xsel-paste ()
52 "Place contents of region in X copy/paste buffer, using shell command."
53 (interactive "")
54 (when (klm:xsel-check-get-DISPLAY current-prefix-arg)
55 (let ((command (cond ((eq system-type 'darwin) "pbpaste")
56 ((eq system-type 'cygwin) "getclip")
57 ;; Linux &c:
58 (t "xsel --output --clipboard"))))
59 (shell-command command 1)
60 (exchange-point-and-mark)
61 )))