From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Robert Weiner Newsgroups: gmane.emacs.devel Subject: Re: Emacs 26: Code that fixes mouse-drag-and-drop-region to work across frames Date: Thu, 12 Oct 2017 12:42:59 -0400 Message-ID: References: Reply-To: rswgnu@gmail.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="94eb2c0749be72ed94055b5c3de0" X-Trace: blaine.gmane.org 1507826663 24881 195.159.176.226 (12 Oct 2017 16:44:23 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 12 Oct 2017 16:44:23 +0000 (UTC) To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 12 18:44:20 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2gaf-0004oY-3W for ged-emacs-devel@m.gmane.org; Thu, 12 Oct 2017 18:44:09 +0200 Original-Received: from localhost ([::1]:46515 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2gam-0005Wp-J1 for ged-emacs-devel@m.gmane.org; Thu, 12 Oct 2017 12:44:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2ga6-0005Ut-Pg for emacs-devel@gnu.org; Thu, 12 Oct 2017 12:43:36 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e2ga2-0000As-Po for emacs-devel@gnu.org; Thu, 12 Oct 2017 12:43:34 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:46866) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2ga2-0000Ac-KL for emacs-devel@gnu.org; Thu, 12 Oct 2017 12:43:30 -0400 Original-Received: from mail-qk0-f174.google.com ([209.85.220.174]:43532) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.82) (envelope-from ) id 1e2ga2-0008Su-A8 for emacs-devel@gnu.org; Thu, 12 Oct 2017 12:43:30 -0400 Original-Received: by mail-qk0-f174.google.com with SMTP id w134so1924095qkb.0 for ; Thu, 12 Oct 2017 09:43:30 -0700 (PDT) X-Gm-Message-State: AMCzsaXWirudPArz0EzLPVFifKL2V67k0WJcaefHbqwGJwLn19jo37BJ f+SPGXT3q40WxaQXRHFG/jeGnZ6Ifs4+iYt6t40= X-Google-Smtp-Source: AOwi7QCNzxcxbxUM+hG8PSFzI/x+eWjjbLNct4HFSxHBid67VmAehZO+R5X28rMeJChwd10lXqa9sul6OPIerz8QdaM= X-Received: by 10.55.156.141 with SMTP id f135mr1358970qke.309.1507826609611; Thu, 12 Oct 2017 09:43:29 -0700 (PDT) Original-Received: by 10.237.34.225 with HTTP; Thu, 12 Oct 2017 09:42:59 -0700 (PDT) In-Reply-To: X-Gmail-Original-Message-ID: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:219423 Archived-At: --94eb2c0749be72ed94055b5c3de0 Content-Type: text/plain; charset="UTF-8" 2 small fixes; release-point was misnamed in the let and indentation was wrong in the latter part of the code. Use this version. -- Bob (setq mouse-drag-and-drop-region 'shift) (defun mouse-drag-and-drop-region (event) "Move text in the region to point where mouse is dragged to. The transportation of text is also referred as `drag and drop'. When text is dragged over to a different buffer, or if a modifier key was pressed when dropping, and the value of the variable `mouse-drag-and-drop-region' is that modifier, the text is copied instead of being cut." (interactive "e") (require 'tooltip) (let ((start (region-beginning)) (end (region-end)) (point (point)) (buffer (current-buffer)) (source-window (selected-window)) event-handler release-point value-selection) (track-mouse ;; When event was click instead of drag, skip loop (while (or (mouse-movement-p (setq event (read-event))) (setq event-handler (and (consp event) (intern-soft (concat "handle-" (symbol-name (event-basic-type event))))))) (if (fboundp event-handler) (progn (funcall event-handler event) (setq event-handler nil)) (setq event-handler nil)) (unless value-selection ; initialization (delete-overlay mouse-secondary-overlay) (setq value-selection (buffer-substring start end)) (move-overlay mouse-secondary-overlay start end)) ; (deactivate-mark) (ignore-errors (deactivate-mark) ; deactivate any existing region in other window (mouse-set-point event) (tooltip-show value-selection))) (tooltip-hide)) ;; Do not modify buffer under mouse when "event was click", ;; "drag negligible", or ;; "drag to read-only". (if (or (equal (mouse-posn-property (event-end event) 'face) 'region) ; "event was click" (and (setq release-point (posn-point (event-end event))) (member 'secondary-selection ; "drag negligible" (mapcar (lambda (xxx) (overlay-get xxx 'face)) (overlays-at release-point)))) buffer-read-only) ;; Do not modify buffer under mouse. (cond ;; "drag negligible" or "drag to read-only", restore region. (value-selection (select-window source-window) ; In case miss drag to other window (goto-char point) (setq deactivate-mark nil) (activate-mark)) ;; "event was click" (t (deactivate-mark) (mouse-set-point event))) ;; Modify buffer under mouse by inserting text. (push-mark) (insert value-selection) (when (not (equal (mark) (point))) ; on successful insert (setq deactivate-mark nil) (activate-mark)) ; have region on destination (let ((dest-window (selected-window))) ; when beyond buffer ;; Take care of initial region on source. (if (equal (current-buffer) buffer) ; when same buffer (let (deactivate-mark) ; remove text (unless (member mouse-drag-and-drop-region (event-modifiers event)) (kill-region (overlay-start mouse-secondary-overlay) (overlay-end mouse-secondary-overlay)))) (select-window source-window) (goto-char point) ; restore point in source-window (activate-mark)) ; restore region ;; Ensure change focus to any new frame; typically edits ;; would continue in its selected window where the text was ;; dropped. (select-frame (window-frame dest-window)) (select-window dest-window))) (delete-overlay mouse-secondary-overlay))) --94eb2c0749be72ed94055b5c3de0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
2 small fixes; release-point was misnamed in the let and inden= tation was wrong in the latter part of the code.
Use this version.=C2=A0 -= - Bob

= (setq mouse-drag-and-drop-region 's= hift)=C2=A0

(defun mouse-drag-and-drop-region (event)
=
=C2=A0 &qu= ot;Move text in the region to point where mouse is dragged to.
=
The transp= ortation of text is also referred as `drag and drop'.
When text is dr= agged over to a different buffer, or if a
modifier key was pressed when d= ropping, and the value of the
variable `mouse-drag-and-drop-region' i= s that modifier, the text
is copied instead of being cut."
=C2=A0 = (interactive "e")
=C2=A0 (require 'tooltip)
=C2=A0 (let (= (start (region-beginning))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (end (region-end))=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (point (point))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 (buffer (current-buffer))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (source-window = (selected-window))
event-handler<= /font>
release-point
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 value-selection)
=C2=A0 =C2=A0 (track-mouse
=C2=A0 =C2=A0 = =C2=A0 ;; When event was click instead of drag, skip loop
=C2=A0 =C2=A0 = =C2=A0 (while (or (mouse-movement-p (setq event (read-event)))
=
(setq event-handler (and (consp event)
=C2=A0 (intern-soft (concat "= ;handle-"
=C2=A0 =C2= =A0 =C2=A0 =C2=A0(symbol-name
(event-basic-type event)))))))
(if (fboundp event-handler)
=C2= =A0 =C2=A0 (progn (funcall event-handler event) (setq event-handler nil))
=C2=A0 (setq event-handler nil))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (unless value-selection ; initialization
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (de= lete-overlay mouse-secondary-overlay)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = (setq value-selection (buffer-substring start end))
=C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 (move-overlay mouse-secondary-overlay start end)) ; (deactiv= ate-mark)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (ignore-errors (deactivate-mark) ; = deactivate any existing region in other window
=C2=A0 =C2=A0 =C2=A0 =C2=A0(mouse-set-point event)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(tooltip-show = value-selection)))
=C2=A0 =C2=A0 =C2=A0 (tooltip-hide))
=C2=A0 =C2=A0 ;= ; Do not modify buffer under mouse when "event was click",=
=C2= =A0 =C2=A0 ;;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0"drag negligible", or
=C2=A0 =C2=A0 ;;=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"drag to read-only".=
=C2=A0 =C2=A0 (if (or (equal (mouse-posn-property (event-end event) '= ;face) 'region) ; "event was click"
=C2=A0 =C2=A0 (and (setq release-point (posn-point (event-= end event)))
(member 'secon= dary-selection ; "drag negligible"
(mapcar (lambda (xxx) (overlay-get xxx 'face))
(overlays-at release-point))))<= /div>
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 buffer-read-only)
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 ;; Do not modify buffer under mouse.
=C2=A0 =C2=A0 =C2=A0= =C2=A0 (cond
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; "drag negligible&= quot; or "drag to read-only", restore region.
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0(value-selection
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (sel= ect-window source-window) ; In case miss drag to other window
<= div class=3D"gmail_default">=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (goto-char point)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (setq deactivate-mark nil)
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (act= ivate-mark))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0;; "event was click&q= uot;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(t
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (deactivate-mark)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (mouse-set-po= int event)))
=C2=A0 =C2=A0 =C2=A0 ;; Modify buffer under mouse by inserti= ng text.
=C2=A0 =C2=A0 =C2=A0 (push-mark)
=C2=A0 =C2=A0 =C2=A0 (insert = value-selection)
=C2=A0 =C2=A0 =C2=A0 (when (not (equal (mark) (point))) = ; on successful insert
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq deactivate-mark= nil)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (activate-mark)) ; have region on destination
=C2=A0 =C2=A0 =C2=A0 (let = ((dest-window (selected-window))) ; when beyond buffer
;; Take care of initial region on source.
(if (equal (current-buffer) buffer) ; when s= ame buffer
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let (deactivate-mar= k) =C2=A0 =C2=A0 ; remove text
=C2=A0 =C2=A0 =C2=A0 (unless (member m= ouse-drag-and-drop-region (event-modifiers event))
(kill-region (overlay-start mouse-secondary-overlay)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(overlay-end mouse-secondary-overlay))))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (select-window source-window)
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (goto-char point) =C2=A0 =C2=A0 =C2=A0 ; restore point in source-window
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (activate-mark)) =C2=A0 =C2=A0 =C2=A0 ; restore region
;; Ensure change focus to any new frame; typically edits
;; would continue in its selected win= dow where the text was
;; droppe= d.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (select-frame (window-frame dest-window))<= /font>
(select-window dest-window)))
= =C2=A0 =C2=A0 (delete-overlay mouse-secondary-overlay)))
=
--94eb2c0749be72ed94055b5c3de0--