unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#27530: patch to cut and copy secondary
@ 2017-06-29 12:43 Tak Kunihiro
  2017-06-29 13:41 ` Drew Adams
  2017-09-05  3:11 ` Tak Kunihiro
  0 siblings, 2 replies; 26+ messages in thread
From: Tak Kunihiro @ 2017-06-29 12:43 UTC (permalink / raw)
  To: 27530; +Cc: tkk

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  <tkk@misasa.okayama-u.ac.jp>

        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)))))
+
 \f
 (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 non-@code{nil}, a command
+@kbd{C-w} (@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 non-@code{nil}, @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}.





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

end of thread, other threads:[~2017-09-21  8:32 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-29 12:43 bug#27530: patch to cut and copy secondary Tak Kunihiro
2017-06-29 13:41 ` Drew Adams
2017-07-01  0:45   ` Tak Kunihiro
2017-08-01  1:35     ` Tak Kunihiro
2017-09-05  3:11 ` Tak Kunihiro
2017-09-05  7:51   ` Robert Pluim
2017-09-05  9:25     ` Tak Kunihiro
2017-09-05  9:32       ` Robert Pluim
2017-09-05 12:22         ` Tak Kunihiro
2017-09-05 13:53       ` Drew Adams
2017-09-05 23:17         ` Tak Kunihiro
2017-09-06  0:29           ` Drew Adams
2017-09-05 15:08   ` Eli Zaretskii
2017-09-05 23:06     ` Tak Kunihiro
2017-09-06  2:33       ` Eli Zaretskii
2017-09-06  3:42         ` Tak Kunihiro
2017-09-06 16:04           ` Eli Zaretskii
2017-09-07 12:35             ` Tak Kunihiro
2017-09-07 19:11               ` Eli Zaretskii
2017-09-07 23:02                 ` Tak Kunihiro
2017-09-08  8:27                   ` Eli Zaretskii
2017-09-10  3:01                     ` Tak Kunihiro
2017-09-11  2:03                       ` Tak Kunihiro
2017-09-20  7:55                         ` Eli Zaretskii
2017-09-20 12:39                           ` Tak Kunihiro
2017-09-21  8:32                             ` Eli Zaretskii

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