emacs-devel
[Top][All Lists]
Advanced

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

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


From: Robert Weiner
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

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 <address@hidden> 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)))


reply via email to

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