; FIXME: Do as a minor mode like mouse-sel / mouse-copy / mouse-drag ??? ; 0 (setq transient-mark-mode t) ; 1 (setq mouse-drag-copy-region nil) ;2 ;;; Like x-select-enable-clipboard (defcustom x-select-enable-primary t "Non-nil means cutting and pasting uses the primary selection." :type 'boolean :group 'killing) (setq x-select-enable-clipboard t) (setq x-select-enable-primary nil) ;;; modified from term/x-win.el ;;; cut and pasted, change is basically just an extra (when... (defun x-select-text* (text &optional push) "Make TEXT, a string, the primary or clipboard or both X selection, depending on value of x-select-enable-clipboard and x-select-enable-primary Also, set the value of X cut buffer 0, for backward compatibility with older X applications." ;; Don't send the cut buffer too much text. ;; It becomes slow, and if really big it causes errors. (cond ((>= (length text) x-cut-buffer-max) (x-set-cut-buffer "" push) (setq x-last-selected-text-cut "" x-last-selected-text-cut-encoded "")) (t (setq x-last-selected-text-cut text x-last-cut-buffer-coding 'iso-latin-1 x-last-selected-text-cut-encoded ;; ICCCM says cut buffer always contain ISO-Latin-1 (encode-coding-string text 'iso-latin-1)) (x-set-cut-buffer x-last-selected-text-cut-encoded push))) (when x-select-enable-primary (x-set-selection 'PRIMARY text) (setq x-last-selected-text-primary text)) (when x-select-enable-clipboard (x-set-selection 'CLIPBOARD text) (setq x-last-selected-text-clipboard text)) ) ;;; modified from term/x-win.el ;;; cut and pasted, change is basically just an extra (when ... ;; Return a value of one of the current X selections. ;; Consult the selections, and the cut buffer. Treat empty strings ;; as if they were unset. ;; If this function is called twice and finds the same text, ;; it returns nil the second time. This is so that a single ;; selection won't be added to the kill ring over and over. (defun x-cut-buffer-or-selection-value* () (let (clip-text primary-text cut-text) (when x-select-enable-clipboard (setq clip-text (x-selection-value 'CLIPBOARD)) (if (string= clip-text "") (setq clip-text nil)) ;; Check the CLIPBOARD selection for 'newness', is it different ;; from what we remebered them to be last time we did a ;; cut/paste operation. (setq clip-text (cond;; check clipboard ((or (not clip-text) (string= clip-text "")) (setq x-last-selected-text-clipboard nil)) ((eq clip-text x-last-selected-text-clipboard) nil) ((string= clip-text x-last-selected-text-clipboard) ;; Record the newer string, ;; so subsequent calls can use the `eq' test. (setq x-last-selected-text-clipboard clip-text) nil) (t (setq x-last-selected-text-clipboard clip-text)))) ) (when x-select-enable-primary (setq primary-text (x-selection-value 'PRIMARY)) ;; Check the PRIMARY selection for 'newness', is it different ;; from what we remebered them to be last time we did a ;; cut/paste operation. (setq primary-text (cond;; check primary selection ((or (not primary-text) (string= primary-text "")) (setq x-last-selected-text-primary nil)) ((eq primary-text x-last-selected-text-primary) nil) ((string= primary-text x-last-selected-text-primary) ;; Record the newer string, ;; so subsequent calls can use the `eq' test. (setq x-last-selected-text-primary primary-text) nil) (t (setq x-last-selected-text-primary primary-text)))) ) (setq cut-text (x-get-cut-buffer 0)) ;; Check the x cut buffer for 'newness', is it different ;; from what we remebered them to be last time we did a ;; cut/paste operation. (setq cut-text (let ((next-coding (or next-selection-coding-system 'iso-latin-1))) (cond;; check cut buffer ((or (not cut-text) (string= cut-text "")) (setq x-last-selected-text-cut nil)) ;; This short cut doesn't work because x-get-cut-buffer ;; always returns a newly created string. ;; ((eq cut-text x-last-selected-text-cut) nil) ((and (string= cut-text x-last-selected-text-cut-encoded) (eq x-last-cut-buffer-coding next-coding)) ;; See the comment above. No need of this recording. ;; Record the newer string, ;; so subsequent calls can use the `eq' test. ;; (setq x-last-selected-text-cut cut-text) nil) (t (setq x-last-selected-text-cut-encoded cut-text x-last-cut-buffer-coding next-coding x-last-selected-text-cut ;; ICCCM says cut buffer always contain ISO-Latin-1, but ;; use next-selection-coding-system if not nil. (decode-coding-string cut-text next-coding)))))) ;; As we have done one selection, clear this now. (setq next-selection-coding-system nil) ;; At this point we have recorded the current values for the ;; selection from clipboard (if we are supposed to) primary, ;; and cut buffer. So return the first one that has changed ;; (which is the first non-null one). ;; ;; NOTE: There will be cases where more than one of these has ;; changed and the new values differ. This indicates that ;; something like the following has happened since the last time ;; we looked at the selections: Application X set all the ;; selections, then Application Y set only one or two of them (say ;; just the cut-buffer). In this case since we don't have ;; timestamps there is no way to know what the 'correct' value to ;; return is. The nice thing to do would be to tell the user we ;; saw multiple possible selections and ask the user which was the ;; one they wanted. ;; This code is still a big improvement because now the user can ;; futz with the current selection and get emacs to pay attention ;; to the cut buffer again (previously as soon as clipboard or ;; primary had been set the cut buffer would essentially never be ;; checked again). (or clip-text primary-text cut-text) )) (setf interprogram-cut-function 'x-select-text*) (setf interprogram-paste-function 'x-cut-buffer-or-selection-value*) ; 3 ; x-set-selection nil == primary. (add-hook 'activate-mark-hook (function (lambda () (x-set-selection nil (buffer-substring (region-beginning) (region-end)))))) ; 4 ;;; based on mouse.el/mouse-yank-secondary (defun mouse-yank-primary (click) "Insert the primary selection at the position clicked on. Move point to the end of the inserted text. If `mouse-yank-at-point' is non-nil, insert at point regardless of where you click." (interactive "e") ;; Give temporary modes such as isearch a chance to turn off. (run-hooks 'mouse-leave-buffer-hook) (or mouse-yank-at-point (mouse-set-point click)) (let ((secondary (x-get-selection 'PRIMARY))) (if secondary (insert (x-get-selection 'PRIMARY)) (error "No primary selection")))) (global-set-key [mouse-2] 'mouse-yank-primary)