@cindex primary selection
@cindex selection, primary
- Under the X Window System, there exists a @dfn{primary selection}
-containing the last stretch of text selected in an X application
-(usually by dragging the mouse). Typically, this text can be inserted
-into other X applications by @kbd{mouse-2} clicks. The primary
-selection is separate from the clipboard. Its contents are more
-fragile; they are overwritten each time you select text with the
-mouse, whereas the clipboard is only overwritten by explicit cut
-or copy commands.
+ Under the X Window System, PGTK and Haiku, there exists a
+@dfn{primary selection} containing the last stretch of text selected
+in an X application (usually by dragging the mouse). Typically, this
+text can be inserted into other X applications by @kbd{mouse-2}
+clicks. The primary selection is separate from the clipboard. Its
+contents are more fragile; they are overwritten each time you select
+text with the mouse, whereas the clipboard is only overwritten by
+explicit cut or copy commands.
Under X, whenever the region is active (@pxref{Mark}), the text in
the region is saved in the primary selection. This applies regardless
(@kbd{C-y}) to insert this text if @code{select-enable-primary} is set
(@pxref{Clipboard}).
+@cindex lost-selection-mode
+ By default, Emacs keeps the region active even after text is
+selected in another program; this is contrary to typical X behavior.
+To make Emacs deactivate the region after another program places data
+in the primary selection, enable the global minor mode
+@code{lost-selection-mode}.
+
@cindex MS-Windows, and primary selection
MS-Windows provides no primary selection, but Emacs emulates it
within a single Emacs session by storing the selected text internally.
be kept aligned with the buffer contents when the user switches
'display-line-numbers-mode' on or off.
++++
+** New minor mode 'lost-selection-mode'.
+This minor mode makes Emacs deactivate the mark in all buffers when
+the primary selection is obtained by another program.
+
+++
** New predicate 'char-uppercase-p'.
This returns non-nil if its argument its an uppercase character.
(symbolp data)
(integerp data)))
\f
+
+;; Minor mode to make losing ownership of PRIMARY behave more like
+;; other X programs.
+
+(defun lost-selection-function (selection)
+ "Handle losing of ownership of SELECTION.
+If SELECTION is `PRIMARY', deactivate the mark in every
+non-temporary buffer."
+ (let ((select-active-regions nil))
+ (when (eq selection 'PRIMARY)
+ (dolist (buffer (buffer-list))
+ (unless (string-match-p "^ "
+ (buffer-name buffer))
+ (with-current-buffer buffer
+ (deactivate-mark t)))))))
+
+(define-minor-mode lost-selection-mode
+ "Toggle `lost-selection-mode'.
+
+When this is enabled, selecting some text in another program will
+cause the mark to be deactivated in all buffers, mimicking the
+behavior of most X Windows programs."
+ :global t
+ :group 'x
+ (if lost-selection-mode
+ (cond ((featurep 'x) (add-hook 'x-lost-selection-functions
+ #'lost-selection-function))
+ ((featurep 'pgtk) (add-hook 'pgtk-lost-selection-functions
+ #'lost-selection-function))
+ ((featurep 'haiku) (add-hook 'haiku-lost-selection-functions
+ #'lost-selection-function)))
+ (cond ((featurep 'x) (remove-hook 'x-lost-selection-functions
+ #'lost-selection-function))
+ ((featurep 'pgtk) (remove-hook 'pgtk-lost-selection-functions
+ #'lost-selection-function))
+ ((featurep 'haiku) (remove-hook 'haiku-lost-selection-functions
+ #'lost-selection-function)))))
+
+\f
;; Functions to convert the selection into various other selection types.
;; Every selection type that Emacs handles is implemented this way, except
;; for TIMESTAMP, which is a special case.