unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Emacs 26: Code that fixes mouse-drag-and-drop-region to work across frames
@ 2017-10-12 16:27 Robert Weiner
  2017-10-12 16:42 ` Robert Weiner
  2017-10-13  9:57 ` Tak Kunihiro
  0 siblings, 2 replies; 16+ messages in thread
From: Robert Weiner @ 2017-10-12 16:27 UTC (permalink / raw)
  To: emacs-devel

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

Presently the function mouse-drag-and-drop-region works only within a
single frame.  The code attached below fixes that and allows for drops in
windows of other frames.  Please try it out and send feedback with any
issues.

There is one remaining issue that I see in the last 3 lines of the
function.  I would like to have focus shift to the frame of the drop since
typically one might be doing more editing there (maybe this could be an
option), but under the macOS windowing system, focus reverts to the source
window and frame rather than the one the code selects.  Is there a way to
make that work.

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-pos
        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: 11986 bytes --]

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-11-02 20:16 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).