Created
April 7, 2022 21:35
-
-
Save sochotnicky/4d398ce876e5bc423f950f01dc38de4a to your computer and use it in GitHub Desktop.
wl-clipboard-mode.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode:elisp; -*- | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Module for switching to using wl-clipboard for pasting/cutting text | |
;; | |
;; Mostly useful when emacs is run under wayland and especially over waypipe or | |
;; similar. Once wl-clipboard is used it will use WAYLAND_DISPLAY of current | |
;; frame to copy and paste text so that the copying/pasting is not happing on | |
;; remote waypipe end | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Save original values | |
(defvar-local orig-interprogram-cut interprogram-cut-function) | |
(defvar-local orig-interprogram-paste interprogram-paste-function) | |
;;;###autoload | |
(define-minor-mode wl-clipboard-mode | |
"A minor mode to use wl-clipboard for copy/paste by overriding | |
`interprogram-cut-function' and `interprogram-paste-function' | |
with custom methods relying on wl-clipboard (wl-copy and | |
wl-paste)" | |
:init-value nil | |
:group 'wl-clipboard | |
:global t | |
:version "1.0" | |
(if wl-clipboard-mode | |
(setq orig-interprogram-cut interprogram-cut-function | |
orig-interprogram-paste interprogram-paste-function | |
interprogram-cut-function 'wl-copy | |
interprogram-paste-function 'wl-paste) | |
(setq interprogram-cut-function orig-interprogram-cut | |
interprogram-paste-function orig-interprogram-paste))) | |
;; Originally from | |
;; https://gist.github.com/yorickvP/6132f237fbc289a45c808d8d75e0e1fb | |
;; Added WAYLAND_DISPLAY override so that this works over waypipe | |
(setq-local wl-copy-process nil) | |
(defun wl-copy (text) | |
(setq wl-copy-process | |
(with-environment-variables (("WAYLAND_DISPLAY" (frame-parameter (selected-frame) 'display))) | |
(make-process :name "wl-copy" | |
:buffer nil | |
:command '("wl-copy" "-f" "-n") | |
:connection-type 'pipe))) | |
(process-send-string wl-copy-process text) | |
(process-send-eof wl-copy-process)) | |
(defun wl-paste () | |
(if (and wl-copy-process (process-live-p wl-copy-process)) | |
nil ; should return nil if we're the current paste owner | |
(with-environment-variables (("WAYLAND_DISPLAY" (frame-parameter (selected-frame) 'display))) | |
(shell-command-to-string "wl-paste -n | tr -d \r")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment