all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Robert Weiner <rsw@gnu.org>
To: emacs-devel <emacs-devel@gnu.org>
Subject: Re: Emacs 26: Code that fixes mouse-drag-and-drop-region to work across frames
Date: Thu, 12 Oct 2017 13:11:52 -0400	[thread overview]
Message-ID: <CA+OMD9hHsLRPgc4cqv0GfK3HhQNz4L91Z5GeWS-32SnGUdRRMA@mail.gmail.com> (raw)
In-Reply-To: <CA+OMD9ig=05ZjAjTu3FQQN3dCgjYBMCH5S6av0dOrm1TG3fuXw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4956 bytes --]

I rewrote the doc strings for clarity.  -- Bob

(defcustom mouse-drag-and-drop-region nil
  "If non-nil, then text regions can be dragged and dropped to new
locations.
Normally, drops within the same buffer move the text of the region; drops
to a different buffer copy the region.  However, if this value is a modifier
symbol, such as `control' or `shift' or `meta', and the matching modifier
key is pressed when dropping the region, then the region text is always
copied.  The drop location must be within the text area of an Emacs window."
  :type 'symbol
  :version "26.1"
  :group 'mouse)

(defun mouse-drag-and-drop-region (event)
  "Drag and drop the active region text to the point of mouse button
release.
Normally, drops within the same buffer move the region text and drops
to a different buffer copy it.  However, if a modifier key is pressed
when dropping, and the value of the variable `mouse-drag-and-drop-region'
is the symbol for that modifier, then the text is always copied.  The drop
location must be within the text area of an Emacs window."


On Thu, Oct 12, 2017 at 12:42 PM, Robert Weiner <rsw@gnu.org> wrote:

> 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)))
>

[-- Attachment #2: Type: text/html, Size: 14495 bytes --]

  reply	other threads:[~2017-10-12 17:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-12 16:27 Emacs 26: Code that fixes mouse-drag-and-drop-region to work across frames Robert Weiner
2017-10-12 16:42 ` Robert Weiner
2017-10-12 17:11   ` Robert Weiner [this message]
2017-10-13  9:57 ` Tak Kunihiro
2017-10-16 13:45   ` Robert Weiner
2017-10-16 14:13     ` Robert Weiner
2017-10-19  0:43       ` Tak Kunihiro
2017-10-26 22:07         ` Robert Weiner
2017-11-01  2:04           ` Tak Kunihiro
2017-11-01 15:24             ` Robert Weiner
2017-11-01 17:16               ` Alan Third
2017-11-01 20:18                 ` Robert Weiner
2017-11-01 20:41                   ` Robert Weiner
2017-11-02  9:51                     ` martin rudalics
2017-11-02 19:00                       ` Robert Weiner
2017-11-02 20:16                         ` martin rudalics

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+OMD9hHsLRPgc4cqv0GfK3HhQNz4L91Z5GeWS-32SnGUdRRMA@mail.gmail.com \
    --to=rsw@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=rswgnu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.