Advertisement
Guest User

Untitled

a guest
Jan 29th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. ;; http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
  2. ;; I prefer using the "clipboard" selection (the one the
  3. ;; typically is used by c-c/c-v) before the primary selection
  4. ;; (that uses mouse-select/middle-button-click)
  5. (setq x-select-enable-clipboard t)
  6. ;; If emacs is run in a terminal, the clipboard- functions have no
  7. ;; effect. Instead, we use of xsel, see
  8. ;; http://www.vergenet.net/~conrad/software/xsel/ -- "a command-line
  9. ;; program for getting and setting the contents of the X selection"
  10.  
  11. ;; Dont do this if we are in a window-system, or if DISPLAY is unset
  12. ;; (in which case xsel will not work anyway). For some reason emacs
  13. ;; built-in getenv omits the DISPLAY variable (!), but it's there
  14. ;; when we do shell-command... Guess this will not work if emacs is
  15. ;; started with --display foo.
  16.  
  17. (unless window-system
  18. (let ((display (shell-command-to-string "echo $DISPLAY")))
  19. (unless (string= display "\n")
  20. ;; Callback for when user cuts
  21. (defun xsel-cut-function (text &optional push)
  22. ;; Insert text to temp-buffer, and "send" content to xsel stdin
  23. (with-temp-buffer
  24. (insert text)
  25. ;; I prefer using the "clipboard" selection (the one the
  26. ;; typically is used by c-c/c-v) before the primary selection
  27. ;; (that uses mouse-select/middle-button-click)
  28. (call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input")))
  29. ;; Call back for when user pastes
  30. (defun xsel-paste-function()
  31. ;; Find out what is current selection by xsel. If it is different
  32. ;; from the top of the kill-ring (car kill-ring), then return
  33. ;; it. Else, nil is returned, so whatever is in the top of the
  34. ;; kill-ring will be used.
  35. (let ((xsel-output (shell-command-to-string "xsel --clipboard --output")))
  36. (unless (string= (car kill-ring) xsel-output)
  37. xsel-output )))
  38. ;; Attach callbacks to hooks
  39. (setq interprogram-cut-function 'xsel-cut-function)
  40. (setq interprogram-paste-function 'xsel-paste-function)
  41. ;; Idea from
  42. ;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
  43. ;; http://www.mail-archive.com/help-gnu-emacs@gnu.org/msg03577.html
  44. )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement