Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Taylan Ulrich B.<taylanbayirli@gmail.com>

Hans de Groede
- fix overflow of supported_targets array

Nikola Hadzic <nikola.hadzic.000@protonmail.com>
- added -e / --empty flag
6 changes: 5 additions & 1 deletion xsel.1x
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH XSEL 1x "January 2008"
.TH XSEL 1x "March 2025"
.SH NAME
xsel \- manipulate the X selection.
.SH SYNOPSIS
Expand Down Expand Up @@ -78,6 +78,10 @@ options.
\fB\-x\fR, \fB\-\-exchange\fR
exchange the PRIMARY and SECONDARY selections. Ignores all \fIinput\fR
and \fIoutput\fR options.
.TP
\fB\-e\fR, \fB\-\-empty\fR
When used together with \fB-k\fR or \fB-x\fR it will not ignore the PRIMARY
and SECONDARY selections even if they (both or any one) are empty.

.PP
\fBX options\fR
Expand Down
28 changes: 24 additions & 4 deletions xsel.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static Bool do_zeroflush = False;
/* do_follow: Follow mode for output */
static Bool do_follow = False;

/* do_empty: Operate with empty strings when -k or -x are supplied */
static Bool do_empty = False;

/* nodaemon: Disable daemon mode if True. */
static Bool no_daemon = False;

Expand Down Expand Up @@ -135,7 +138,9 @@ usage (void)
printf (" -k, --keep Do not modify the selections, but make the PRIMARY\n");
printf (" and SECONDARY selections persist even after the\n");
printf (" programs they were selected in exit.\n");
printf (" -x, --exchange Exchange the PRIMARY and SECONDARY selections\n\n");
printf (" -x, --exchange Exchange the PRIMARY and SECONDARY selections\n");
printf (" -e, --empty Keep / exchange the PRIMARY and SECONDARY selections\n");
printf (" even when they, both or any one, are empty.\n\n");
printf ("X options\n");
printf (" --display displayname\n");
printf (" Specify the connection to the X server\n");
Expand Down Expand Up @@ -1877,13 +1882,26 @@ set_selection_pair (unsigned char * sel_p, unsigned char * sel_s)
* primary and secondary selections with texts 'sel_p' and 'sel_s'
* respectively.
*
* If both 'sel_p' and 'sel_s' are empty strings (NULL or "") then no
* daemon process is created, and both selections are cleared instead.
* If 'do_empty' is set, whichever one of 'sel_p' and 'sel_s' is NULL
* is assigned a newly allocated empty string (""); the function then
* proceeds normally i.e. as if 'sel_p' and 'sel_s' were passed to it
* as non-NULL strings. If 'do_empty' is not set and both 'sel_p' and
* 'sel_s' are empty strings (NULL or "") then no daemon process is
* created, and both selections are cleared instead.
*/
static void
set_selection_pair__daemon (unsigned char * sel_p, unsigned char * sel_s)
{
if (empty_string (sel_p) && empty_string (sel_s)) {
if (do_empty) {
if (sel_p == NULL) {
sel_p = xs_malloc (sizeof(char));
sel_p[0] = '\0';
}
if (sel_s == NULL) {
sel_s = xs_malloc (sizeof(char));
sel_s[0] = '\0';
}
} else if (empty_string (sel_p) && empty_string (sel_s)) {
clear_selection (XA_PRIMARY);
clear_selection (XA_SECONDARY);
return;
Expand Down Expand Up @@ -2117,6 +2135,8 @@ main(int argc, char *argv[])
do_keep = True;
} else if (OPT("--exchange") || OPT("-x")) {
do_exchange = True;
} else if (OPT("--empty") || OPT("-e")) {
do_empty = True;
} else if (OPT("--display")) {
i++; if (i >= argc) goto usage_err;
display_name = argv[i];
Expand Down