bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#27530: patch to cut and copy secondary


From: Tak Kunihiro
Subject: bug#27530: patch to cut and copy secondary
Date: Thu, 29 Jun 2017 21:43:04 +0900 (JST)

I found secondary can be used as another region.  Here I send a patch
to cut and copy secondary as if region (when region does not exist).

# Change log

2017-07-01  Tak Kunihiro  <address@hidden>

        Cut and copy secondary when region does not exist.

        * doc/emacs/killing.texi (Secondary Selection): Document support of cut 
and copy secondary.
        * lisp/simple.el (kill-secondary-flag): Use also secondary not only 
region to cut and copy.
        (kill-region): Set target to secondary not only region if 
kill-secondary-flag is non-nil.
        (kill-ring-save): Set target to secondary not only region if 
kill-secondary-flag is non-nil.
        * lisp/mouse.el (mouse-secondary-exists-p): Return location of 
secondary when it exists in current buffer.
        (mouse-set-mark-and-point-from-secondary): Set mark and point from 
secondary.
        (mouse-set-secondary-from-region): Set secondary to the text in region.

# NEWS

** Use can cut and copy secondary when region does not exist.
To cut and copy secondary using C-w and M-w, set 'kill-secondary-flag' to t.

# Code 1/2

diff --git a/mouse.260.el b/mouse.el
old mode 100644
new mode 100755
index 9b6b169..6ee368e
--- a/mouse.260.el
+++ b/mouse.el
@@ -1545,6 +1545,45 @@ CLICK position, kill the secondary selection."
         (> (length str) 0)
         (gui-set-selection 'SECONDARY str))))
 
+(defun mouse-secondary-exists-p ()
+  "Return location of mouse-secondary-overlay when exists in current buffer."
+  (let ((buf (overlay-buffer mouse-secondary-overlay))
+        (beg (overlay-start mouse-secondary-overlay))
+        (end (overlay-end mouse-secondary-overlay)))
+    (when (and buf beg end
+               (equal buf (current-buffer))
+               (/= beg end))
+      (list beg end))))
+
+(defun mouse-set-mark-and-point-from-secondary ()
+  "Set mark and point from mouse-secondary-overlay.
+This works when mouse-secondary-overlay exists and region does
+not exist.  The mouse-secondary-overlay will be deactivated."
+  (let ((secondary-to-kill (and (not (region-active-p))
+                                (mouse-secondary-exists-p))))
+    ;; delete overlay even on different buffer.
+    ;; this should be later than obtaining location.
+    (delete-overlay mouse-secondary-overlay)
+    (when secondary-to-kill
+      (push-mark (car secondary-to-kill) t t)
+      (goto-char (cadr secondary-to-kill)))))
+
+(defun mouse-set-secondary-from-region ()
+  "Set the secondary selection to the text in region.
+When region does not exists, set mouse-secondary-start to the point.
+When point is on mouse-secondary-overlay, do nothing."
+  (cond
+   ((region-active-p) ; create mouse-secondary-overlay from region
+    (delete-overlay mouse-secondary-overlay)
+    (move-overlay mouse-secondary-overlay (region-beginning) (region-end)))
+   ((member 'secondary-selection ; do nothing
+            (mapcar (lambda (xxx) (overlay-get xxx 'face))
+                    (overlays-at (point)))))
+   (t (delete-overlay mouse-secondary-overlay) ; create mouse-secondary-start 
from point
+      (push-mark (point))
+      (setq mouse-secondary-start (make-marker))
+      (move-marker mouse-secondary-start (point)))))
+
 
 (defcustom mouse-buffer-menu-maxlen 20
   "Number of buffers in one pane (submenu) of the buffer menu.

# Code 2/2

diff --git a/simple.252.el b/simple.el
index 5f70ade..e75743a 100644
--- a/simple.252.el
+++ b/simple.el
@@ -4287,11 +4287,19 @@ move the yanking point; just return the Nth kill 
forward."
   :type 'boolean
   :group 'killing)
 
+(defcustom kill-secondary-flag nil
+  "Kill mouse-secondary-overlay when it exists but region does not exists."
+  :type 'boolean
+  :group 'mouse
+  :version "26.1")
+
 (defun kill-region (beg end &optional region)
   "Kill (\"cut\") text between point and mark.
 This deletes the text from the buffer and saves it in the kill ring.
 The command \\[yank] can retrieve it from there.
 \(If you want to save the region without killing it, use \\[kill-ring-save].)
+If `kill-secondary-flag' is non-nil, kill
+mouse-secondary-overlay instead of the region.
 
 If you want to append the killed region to the last killed text,
 use \\[append-next-kill] before \\[kill-region].
@@ -4317,7 +4325,10 @@ Supply two arguments, character positions BEG and END 
indicating the
  region instead."
   ;; Pass mark first, then point, because the order matters when
   ;; calling `kill-append'.
-  (interactive (list (mark) (point) 'region))
+  (interactive
+   (progn (when kill-secondary-flag
+            (mouse-set-mark-and-point-from-secondary)) ; no region but 
secondary
+          (list (mark) (point) 'region)))
   (unless (and beg end)
     (user-error "The mark is not set now, so there is no region"))
   (condition-case nil
@@ -4387,6 +4398,8 @@ This command's old key binding has been given to 
`kill-ring-save'."
 In Transient Mark mode, deactivate the mark.
 If `interprogram-cut-function' is non-nil, also save the text for a window
 system cut and paste.
+If `kill-secondary-flag' is non-nil, save
+mouse-secondary-overlay instead of the region.
 
 If you want to append the killed line to the last killed text,
 use \\[append-next-kill] before \\[kill-ring-save].
@@ -4404,8 +4417,11 @@ This command is similar to `copy-region-as-kill', except 
that it gives
 visual feedback indicating the extent of the region being copied."
   ;; Pass mark first, then point, because the order matters when
   ;; calling `kill-append'.
-  (interactive (list (mark) (point)
-                    (prefix-numeric-value current-prefix-arg)))
+  (interactive
+   (progn (when kill-secondary-flag
+            (mouse-set-mark-and-point-from-secondary)) ; no region but 
secondary
+          (list (mark) (point)
+                (prefix-numeric-value current-prefix-arg))))
   (copy-region-as-kill beg end region)
   ;; This use of called-interactively-p is correct because the code it
   ;; controls just gives the user visual feedback.

# Info

diff --git a/killing.252.texi b/killing.texi
index 47de053..721b68e 100755
--- a/killing.252.texi
+++ b/killing.texi
@@ -624,6 +624,11 @@ end of the yanked text (@code{mouse-yank-secondary}).
 Double or triple clicking of @kbd{M-mouse-1} operates on words and
 lines, much like @kbd{mouse-1}.
 
+If @code{kill-secondary-flag} is address@hidden, a command
address@hidden (@code{kill-region}), and a command @kbd{M-w}
+(@code{kill-ring-save}) will target secondary selection when region
+does not exist.
+
 If @code{mouse-yank-at-point} is address@hidden, @kbd{M-mouse-2} yanks
 at point.  Then it does not matter precisely where you click, or even
 which of the frame's windows you click on.  @xref{Mouse Commands}.





reply via email to

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