system.el 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; system.el --- System integration functions -*- lexical-binding: t; -*-
  2. (defun my/system-paste-via-yank ()
  3. "Pure paste: Overwrites the kill ring with clipboard data and triggers local yank."
  4. (interactive)
  5. (let ((text (or (gui-get-selection 'CLIPBOARD)
  6. (gui-get-selection 'PRIMARY)
  7. (when (executable-find "wl-paste")
  8. (when (executable-find "wl-paste")
  9. (let ((val (shell-command-to-string "wl-paste -n -t text/plain")))
  10. (unless (string-empty-p val) val))))
  11. nil)))
  12. (if text
  13. (progn
  14. ;; Forcibly stage to the kill ring. With kill-ring-max=1, this replaces everything.
  15. (kill-new text)
  16. ;; Resolve the context-aware yank command (handles terminal wrappers automatically)
  17. (let ((local-yank (key-binding (kbd "C-y"))))
  18. (if (commandp local-yank)
  19. (call-interactively local-yank)
  20. (call-interactively 'yank))))
  21. (user-error "System clipboard is empty"))))
  22. (defun my/sync-clipboard-to-kill-ring ()
  23. "Explicitly pull the system clipboard into the Emacs kill ring without pasting."
  24. (interactive)
  25. (let ((text (or (gui-get-selection 'CLIPBOARD)
  26. (gui-get-selection 'PRIMARY)
  27. (when (executable-find "wl-paste")
  28. (when (executable-find "wl-paste")
  29. (let ((val (shell-command-to-string "wl-paste -n -t text/plain")))
  30. (unless (string-empty-p val) val))))
  31. nil)))
  32. (if text
  33. (progn
  34. (kill-new text)
  35. (message "System clipboard staged to kill ring."))
  36. (user-error "System clipboard is empty"))))
  37. (defun my/mc-system-copy ()
  38. "Copy all active MC regions to system clipboard as a multi-line string."
  39. (interactive)
  40. (if (and (bound-and-true-p multiple-cursors-mode) (use-region-p))
  41. (let ((strings (mapcar (lambda (cursor)
  42. (buffer-substring-no-properties (mc/cursor-beg cursor) (mc/cursor-end cursor)))
  43. (mc/all-fake-cursors))))
  44. (push (buffer-substring-no-properties (region-beginning) (region-end)) strings)
  45. ;; Sort by buffer position
  46. (setq strings (mapcar #'cdr
  47. (sort (cl-mapcar #'cons
  48. (cons (point) (mapcar #'mc/cursor-beg (mc/all-fake-cursors)))
  49. strings)
  50. (lambda (a b) (< (car a) (car b))))))
  51. (let ((text (string-join strings "\n")))
  52. (gui-backend-set-selection 'CLIPBOARD text)
  53. (message "Copied %d cursor regions to system clipboard." (length strings))))
  54. (user-error "Multiple cursors active region required")))
  55. (defun my/wl-copy (text)
  56. ;; Explicitly target ONLY the CLIPBOARD selection, leaving PRIMARY untouched
  57. (gui-set-selection 'CLIPBOARD text))
  58. (defun my/wl-paste ()
  59. ;; Explicitly fetch ONLY from CLIPBOARD
  60. (gui-get-selection 'CLIPBOARD 'text/plain))
  61. (defun my/system-copy (beg end)
  62. (interactive "r")
  63. (unless (use-region-p) (user-error "No region"))
  64. (let ((txt (buffer-substring-no-properties beg end)))
  65. (kill-new txt)
  66. (my/wl-copy txt)
  67. (deactivate-mark)
  68. (message "Copied %d chars to SYSTEM CLIPBOARD" (length txt))))
  69. (defun my/system-paste ()
  70. (interactive)
  71. (let ((txt (my/wl-paste)))
  72. (unless txt (user-error "Clipboard empty"))
  73. (kill-new txt)
  74. (insert-for-yank txt)))
  75. (defun my/system-cut (beg end)
  76. (interactive "r")
  77. (my/system-copy beg end)
  78. (delete-region beg end))
  79. (provide 'system)