Support setting region from secondary selection and vice versa

* lisp/mouse.el (secondary-selection-exist-p): New function to
allow callers to tell existence of the secondary selection
in current buffer.
(secondary-selection-to-region): New function to set
beginning and end of the region from those of the secondary
selection.
(secondary-selection-from-region): New function to set
beginning and end of the secondary selection from those of
the region.  (Bug#27530)

* etc/NEWS: Mention the new functions.
This commit is contained in:
Tak Kunihiro 2017-09-21 11:26:00 +03:00 committed by Eli Zaretskii
parent 047f02f00f
commit 31e1d9ef2f
2 changed files with 35 additions and 0 deletions

View file

@ -1819,6 +1819,13 @@ can be replicated simply by setting 'comment-auto-fill-only-comments'.
** New pcase pattern 'rx' to match against a rx-style regular expression.
For details, see the doc string of 'rx--pcase-macroexpander'.
---
** New functions to set region from secondary selection and vice versa.
The new functions 'secondary-selection-to-region' and
'secondary-selection-from-region' let you set the beginning and the
end of the region from those of the secondary selection and vise
versa.
* Changes in Emacs 26.1 on Non-Free Operating Systems

View file

@ -1916,6 +1916,34 @@ CLICK position, kill the secondary selection."
(> (length str) 0)
(gui-set-selection 'SECONDARY str))))
(defun secondary-selection-exist-p ()
"Return non-nil if there is the secondary selection in current buffer."
(memq mouse-secondary-overlay (overlays-in (point-min) (point-max))))
(defun secondary-selection-to-region ()
"Set beginning and end of the region to those of the secondary selection.
This puts mark and point at the beginning and the end of the
secondary selection, respectively. This works when the secondary
selection exists and the region does not exist in current buffer;
the secondary selection will be deleted afterward.
If the region is active, or the secondary selection doesn't exist,
this function does nothing."
(when (and (not (region-active-p))
(secondary-selection-exist-p))
(let ((beg (overlay-start mouse-secondary-overlay))
(end (overlay-end mouse-secondary-overlay)))
(push-mark beg t t)
(goto-char end))
;; Delete the secondary selection on current buffer.
(delete-overlay mouse-secondary-overlay)))
(defun secondary-selection-from-region ()
"Set beginning and end of the secondary selection to those of the region.
When there is no region, this function does nothing."
(when (region-active-p) ; Create the secondary selection from the region.
(delete-overlay mouse-secondary-overlay) ; Delete the secondary selection even on a different buffer.
(move-overlay mouse-secondary-overlay (region-beginning) (region-end))))
(defcustom mouse-buffer-menu-maxlen 20
"Number of buffers in one pane (submenu) of the buffer menu.