From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tak Kunihiro Newsgroups: gmane.emacs.bugs Subject: bug#26725: patch for mouse.el Date: Fri, 19 May 2017 11:07:43 +0900 (JST) Message-ID: <20170519.110743.171510165.tkk@misasa.okayama-u.ac.jp> References: <20170501.144308.327409523.tkk@misasa.okayama-u.ac.jp> <83d1bc8xnm.fsf@gnu.org> <20170515.130131.472087335.tkk@misasa.okayama-u.ac.jp> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Fri_May_19_11_07_43_2017_897)--" Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1495159758 11244 195.159.176.226 (19 May 2017 02:09:18 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 19 May 2017 02:09:18 +0000 (UTC) Cc: tkk@misasa.okayama-u.ac.jp, 26725@debbugs.gnu.org To: eliz@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri May 19 04:09:11 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1dBXLq-0002fY-JL for geb-bug-gnu-emacs@m.gmane.org; Fri, 19 May 2017 04:09:10 +0200 Original-Received: from localhost ([::1]:56165 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBXLs-0007YH-Ta for geb-bug-gnu-emacs@m.gmane.org; Thu, 18 May 2017 22:09:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39906) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBXLm-0007XY-Ag for bug-gnu-emacs@gnu.org; Thu, 18 May 2017 22:09:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dBXLj-0001mH-17 for bug-gnu-emacs@gnu.org; Thu, 18 May 2017 22:09:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:51457) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dBXLi-0001m9-Sx for bug-gnu-emacs@gnu.org; Thu, 18 May 2017 22:09:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dBXLi-0007K0-Dv for bug-gnu-emacs@gnu.org; Thu, 18 May 2017 22:09:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tak Kunihiro Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 19 May 2017 02:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26725 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 26725-submit@debbugs.gnu.org id=B26725.149515968228064 (code B ref 26725); Fri, 19 May 2017 02:09:02 +0000 Original-Received: (at 26725) by debbugs.gnu.org; 19 May 2017 02:08:02 +0000 Original-Received: from localhost ([127.0.0.1]:54134 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dBXKj-0007IX-HN for submit@debbugs.gnu.org; Thu, 18 May 2017 22:08:01 -0400 Original-Received: from mxl065v67.mxlogic.net ([208.81.65.67]:50658 helo=s14p02o144.mxlogic.net) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dBXKh-0007IL-15 for 26725@debbugs.gnu.org; Thu, 18 May 2017 22:08:00 -0400 Original-Received: from unknown [42.127.236.175] (EHLO mlsec.cc.okayama-u.ac.jp) by s14p02o144.mxlogic.net(mxl_mta-8.5.0-1675) over TLS secured channel with ESMTP id 4735e195.0.1214433.00-384.2563317.s14p02o144.mxlogic.net (envelope-from ); Thu, 18 May 2017 20:07:48 -0600 (MDT) X-MXL-Hash: 591e53747f02e15e-89100b3aa96e8ca285104aa95f4c3beca6a64285 Original-Received: from alml002.ouadm.okayama-u.ac.jp (unknown [42.127.236.168]) by mlsec.cc.okayama-u.ac.jp with smtp id 19c7_1672_53f5f347_78fd_41db_a12a_0d59e61cd466; Fri, 19 May 2017 11:07:43 +0900 Original-Received: from localhost (unknown [172.24.1.79]) by alml002.ouadm.okayama-u.ac.jp (Postfix) with ESMTPSA id E99D24C0BFE; Fri, 19 May 2017 11:07:43 +0900 (JST) In-Reply-To: <20170515.130131.472087335.tkk@misasa.okayama-u.ac.jp> X-Mailer: Mew version 6.7 on Emacs 25.2 / Mule 6.0 (HANACHIRUSATO) X-NAI-Spam-Flag: NO X-NAI-Spam-Threshold: 11 X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 4 Rules triggered EDT_SA_AU_PASS=0, EDT_SA_DN_PASS=0, EDT_SA_TS_PASS=0, RV6028=0 X-NAI-Spam-Version: 2.3.0.9418 : core <6028> : inlines <5879> : streams <1746110> : uri <2429009> X-AnalysisOut: [v=2.2 cv=MPYQoIRl c=1 sm=1 tr=0 a=8LLPK8U+aGQ6qN8QlyMYtQ==] X-AnalysisOut: [:117 a=8LLPK8U+aGQ6qN8QlyMYtQ==:17 a=tJ8p9aeEuA8A:10 a=j9g] X-AnalysisOut: [ihSJ1NfYLMCF46uAA:9 a=7Zwj6sZBwVKJAoWSPKxL6X1jA+E=:19 a=Cj] X-AnalysisOut: [uIK1q_8ugA:10 a=hjlGXw3i1xpCsdrjrhUA:9] X-Spam: [F=0.5299760192; CM=0.500; MH=0.529(2017051808); S=0.200(2015072901)] X-MAIL-FROM: X-SOURCE-IP: [42.127.236.175] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:132619 Archived-At: ----Next_Part(Fri_May_19_11_07_43_2017_897)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Thank you for the comments. * English I revised English as suggested. * Algorithm in detection of clicking region >> +(defun mouse-on-region-p (position &optional start end) >> + "Return if POSITION is in between START and END in the current buffer. >> +When START and END are nil but there is active region, those of >> +active region is fed." >> + (when (region-active-p) >> + (setq start (or start (region-beginning))) >> + (setq end (or end (region-end)))) >> + (let ((point (posn-point position))) >> + (and >> + (numberp start) >> + (numberp end) >> + (numberp point) >> + (<= start point) >> + (<= point end)))) > > This algorithm will fail with bidirectional text, where buffer > positions don't increase monotonically with screen coordinates. How > about testing the face of the character under mouse instead? I revised the code using following statements. Accordingly, `mouse-on-region-p' is deleted. (equal (mouse-posn-property (event-end event) 'face) 'region) (member 'secondary-selection(mapcar (lambda (xxx) (overlay-get xxx 'face)) (overlays-at (posn-point (event-end event))))) * Name of function >> +(defun mouse-drag-region-pasting (event) > > The function's name is confusing. Why not name it like the defcustom? I changed the name from `mouse-drag-region-pasting' to `mouse-drag-and-drop-region'. As a consequence, variable and function have the same name. I suppose it is OK. * NEWS, ChangeLog, and Info > Please add a NEWS entry and some minimal documentation in the user > manual. I add ChangeLog, NEWS, and Info as shown below. * Copy on Drag and Drop with meta key pressed I found that sometimes I want to copy on drop (instead of cut) even when source buffer and destination buffer are the same. I added a logic to let me do so. When modifier key is pressed on drop, the function does not delete the original region. The modifier key is specified by the variable `mouse-drag-and-drop-region' that is used as flag. I did not create a new variable. # ChangeLog 2017-05-20 Tak Kunihiro Support drag and drop region by the mouse (Bug#26725) * doc/emacs/frames.texi (Drag and Drop): Document support of drag and drop region by the mouse. * lisp/mouse.el (mouse-drag-region): Call mouse-drag-and-drop-region when start-event is against region. (mouse-drag-and-drop-region): Move the region by dragging the mouse. # NEWS ** Support drag and drop text by the mouse. You can start drag-and-drop text by customizing 'mouse-drag-and-drop-region'. # Info diff --git a/site-lisp/frames.252.texi b/site-lisp/frames.texi index 1611bd1..da140a6 100755 --- a/site-lisp/frames.252.texi +++ b/site-lisp/frames.texi @@ -1046,12 +1046,17 @@ Window Dividers, elisp, The Emacs Lisp Reference Manual}. @cindex drag and drop In most graphical desktop environments, Emacs has basic support for -@dfn{drag and drop} operations. For instance, dropping text onto an -Emacs frame inserts the text where it is dropped. Dropping a file -onto an Emacs frame visits that file. As a special case, dropping the -file on a Dired buffer moves or copies the file (according to the -conventions of the application it came from) into the directory -displayed in that buffer. +@dfn{drag and drop} operations. For instance, dragging region can move +the entire region of text to point where mouse is dragged over to. +Dropping text onto an Emacs frame inserts the text where it is dropped. +Dropping a file onto an Emacs frame visits that file. As a special +case, dropping the file on a Dired buffer moves or copies the file +(according to the conventions of the application it came from) into the +directory displayed in that buffer. + +@vindex mouse-drag-and-drop-region + If you prefer to move the entire region of text by mouse, customize +the variable @code{mouse-drag-and-drop-region}. @vindex dnd-open-file-other-window Dropping a file normally visits it in the window you drop it on. If ----Next_Part(Fri_May_19_11_07_43_2017_897)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="mouse.el.patch" diff --git a/mouse.252.el b/mouse.el index 3336e2b..66142f4 100755 --- a/mouse.252.el +++ b/mouse.el @@ -688,12 +688,19 @@ Upon exit, point is at the far edge of the newly visible text." Highlight the drag area as you move the mouse. This must be bound to a button-down mouse event. In Transient Mark mode, the highlighting remains as long as the mark -remains active. Otherwise, it remains until the next input event." - (interactive "e") - ;; Give temporary modes such as isearch a chance to turn off. - (run-hooks 'mouse-leave-buffer-hook) - (mouse-drag-track start-event)) +remains active. Otherwise, it remains until the next input event. +When the region already exists and the variable +`mouse-drag-and-drop-region' is non-nil, this moves the entire +region of text to point where mouse is dragged over to." + (interactive "e") + (if (and mouse-drag-and-drop-region + (not (member 'triple (event-modifiers start-event))) + (equal (mouse-posn-property (event-start start-event) 'face) 'region)) + (mouse-drag-and-drop-region start-event) + ;; Give temporary modes such as isearch a chance to turn off. + (run-hooks 'mouse-leave-buffer-hook) + (mouse-drag-track start-event))) (defun mouse-posn-property (pos property) "Look for a property at click position. @@ -1911,6 +1918,81 @@ choose a font." t (called-interactively-p 'interactive))))))))) +;; Drag and drop support. +(defcustom mouse-drag-and-drop-region nil + "If non-nil, dragging the mouse drags the region, when that exists. +When the modifier key is specified, dropping with the modifier +key pressed copies the text instead of cuts." + :type 'symbol + :version "26.1" + :group 'mouse) + +(defun mouse-drag-and-drop-region (event) + "Move text in a region to point where mouse is dragged over to. +The transportation of text is also referred as `drag and drop'. +When text is dragged over to a different buffer, the text is +copied instead of being cut." + (interactive "e") + (require 'tooltip) + (let ((start (region-beginning)) + (end (region-end)) + (point (point)) + (buffer (current-buffer)) + (window (selected-window)) + value-selection) + (track-mouse + ;; When event was click instead of drag, skip loop + (while (progn + (setq event (read-event)) + (mouse-movement-p event)) + (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) ; care 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" + (member 'secondary-selection ; "drag negligible" + (mapcar (lambda (xxx) (overlay-get xxx 'face)) + (overlays-at (posn-point (event-end event))))) + buffer-read-only) + ;; Do not modify buffer under mouse + (cond + ;; "drag negligible" or "drag to read-only", restore region + (value-selection + (select-window window) ; 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 success insert + (setq deactivate-mark nil) + (activate-mark)) ; have region on destination + ;; Take care 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)))) + (let ((window1 (selected-window))) ; when beyond buffer + (select-window window) + (goto-char point) ; restore point on source window + (activate-mark) ; restore region + (select-window window1)))) + (delete-overlay mouse-secondary-overlay))) + + ;;; Bindings for mouse commands. (global-set-key [down-mouse-1] 'mouse-drag-region) ----Next_Part(Fri_May_19_11_07_43_2017_897)----