;;; system.el --- System integration functions -*- lexical-binding: t; -*- (defun my/system-paste-via-yank () "Pure paste: Overwrites the kill ring with clipboard data and triggers local yank." (interactive) (let ((text (or (gui-get-selection 'CLIPBOARD) (gui-get-selection 'PRIMARY) (when (executable-find "wl-paste") (when (executable-find "wl-paste") (let ((val (shell-command-to-string "wl-paste -n -t text/plain"))) (unless (string-empty-p val) val)))) nil))) (if text (progn ;; Forcibly stage to the kill ring. With kill-ring-max=1, this replaces everything. (kill-new text) ;; Resolve the context-aware yank command (handles terminal wrappers automatically) (let ((local-yank (key-binding (kbd "C-y")))) (if (commandp local-yank) (call-interactively local-yank) (call-interactively 'yank)))) (user-error "System clipboard is empty")))) (defun my/sync-clipboard-to-kill-ring () "Explicitly pull the system clipboard into the Emacs kill ring without pasting." (interactive) (let ((text (or (gui-get-selection 'CLIPBOARD) (gui-get-selection 'PRIMARY) (when (executable-find "wl-paste") (when (executable-find "wl-paste") (let ((val (shell-command-to-string "wl-paste -n -t text/plain"))) (unless (string-empty-p val) val)))) nil))) (if text (progn (kill-new text) (message "System clipboard staged to kill ring.")) (user-error "System clipboard is empty")))) (defun my/mc-system-copy () "Copy all active MC regions to system clipboard as a multi-line string." (interactive) (if (and (bound-and-true-p multiple-cursors-mode) (use-region-p)) (let ((strings (mapcar (lambda (cursor) (buffer-substring-no-properties (mc/cursor-beg cursor) (mc/cursor-end cursor))) (mc/all-fake-cursors)))) (push (buffer-substring-no-properties (region-beginning) (region-end)) strings) ;; Sort by buffer position (setq strings (mapcar #'cdr (sort (cl-mapcar #'cons (cons (point) (mapcar #'mc/cursor-beg (mc/all-fake-cursors))) strings) (lambda (a b) (< (car a) (car b)))))) (let ((text (string-join strings "\n"))) (gui-backend-set-selection 'CLIPBOARD text) (message "Copied %d cursor regions to system clipboard." (length strings)))) (user-error "Multiple cursors active region required"))) (defun my/wl-copy (text) ;; Explicitly target ONLY the CLIPBOARD selection, leaving PRIMARY untouched (gui-set-selection 'CLIPBOARD text)) (defun my/wl-paste () ;; Explicitly fetch ONLY from CLIPBOARD (gui-get-selection 'CLIPBOARD 'text/plain)) (defun my/system-copy (beg end) (interactive "r") (unless (use-region-p) (user-error "No region")) (let ((txt (buffer-substring-no-properties beg end))) (kill-new txt) (my/wl-copy txt) (deactivate-mark) (message "Copied %d chars to SYSTEM CLIPBOARD" (length txt)))) (defun my/system-paste () (interactive) (let ((txt (my/wl-paste))) (unless txt (user-error "Clipboard empty")) (kill-new txt) (insert-for-yank txt))) (defun my/system-cut (beg end) (interactive "r") (my/system-copy beg end) (delete-region beg end)) (provide 'system)