[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Emacs 26: Code that fixes mouse-drag-and-drop-region to work across fram

From: Robert Weiner
Subject: Emacs 26: Code that fixes mouse-drag-and-drop-region to work across frames
Date: Thu, 12 Oct 2017 12:27:44 -0400

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.



(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))
      ;; 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-"
(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)))
    ;; 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))))
        ;; Do not modify buffer under mouse.
         ;; "drag negligible" or "drag to read-only", restore region.
          (select-window source-window) ; In case miss drag to other window
          (goto-char point)
          (setq deactivate-mark nil)
         ;; "event was click"
          (mouse-set-point event)))
      ;; Modify buffer under mouse by inserting text.
      (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)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]