xsel

Program for manipulating the X clipboard
git clone https://git.sinitax.com/kfish/xsel
Log | Files | Refs | README | LICENSE | sfeed.txt

commit b655668a48ab07a87173f7f6810b6d0ab480a7db
parent f2dc36b62b23fdc74eb5526e1fd0d236750d1c91
Author: Conrad Parker <conrad@metadecks.org>
Date:   Thu, 10 Mar 2022 14:52:06 +0800

Merge branch 'master' into master
Diffstat:
MREADME | 2+-
Mxsel.c | 50++++++++++++++++++++++++++++++++++++++++----------
2 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/README b/README @@ -143,7 +143,7 @@ transfers. My thoughts on ICCCM are available at: - http://lists.slug.org.au/archives/slug-chat/2001/July/msg00054.html + https://web.archive.org/web/20110720132303/https://lists.slug.org.au/archives/slug-chat/2001/July/msg00054.html (Warning: explicit language). diff --git a/xsel.c b/xsel.c @@ -139,11 +139,13 @@ usage (void) printf ("X options\n"); printf (" --display displayname\n"); printf (" Specify the connection to the X server\n"); + printf (" -m wm, --name wm Name with the process will be identified\n"); printf (" -t ms, --selectionTimeout ms\n"); printf (" Specify the timeout in milliseconds within which the\n"); printf (" selection must be retrieved. A value of 0 (zero)\n"); printf (" specifies no timeout (default)\n\n"); printf ("Miscellaneous options\n"); + printf (" --trim Remove newline ('\\n') char from end of input / output\n"); printf (" -l, --logfile Specify file to log errors to when detached.\n"); printf (" -n, --nodetach Do not detach from the controlling terminal. Without\n"); printf (" this option, xsel will fork to become a background\n"); @@ -1583,13 +1585,14 @@ handle_multiple (Display * display, Window requestor, Atom property, { MultTrack * mt; int format; + Atom type; unsigned long bytesafter; HandleResult retval = HANDLE_OK; mt = xs_malloc (sizeof (MultTrack)); XGetWindowProperty (display, requestor, property, 0L, 1000000, - False, (Atom)AnyPropertyType, &mt->property, + False, (Atom)AnyPropertyType, &type, &format, &mt->length, &bytesafter, (unsigned char **)&mt->atoms); @@ -1601,6 +1604,7 @@ handle_multiple (Display * display, Window requestor, Atom property, mt->display = display; mt->requestor = requestor; mt->sel = sel; + mt->property = property; mt->selection = selection; mt->time = time; mt->index = 0; @@ -1668,6 +1672,7 @@ handle_selection_request (XEvent event, unsigned char * sel) ev.property = None; } else { /* Handle MULTIPLE request */ + ev.property = xsr->property; hr = handle_multiple (ev.display, ev.requestor, ev.property, sel, ev.selection, ev.time, NULL); } @@ -2027,6 +2032,7 @@ main(int argc, char *argv[]) Bool do_input = False, do_output = False; Bool force_input = False, force_output = False; Bool want_clipboard = False, do_delete = False; + Bool trim_trailing_newline = False; Window root; Atom selection = XA_PRIMARY, test_atom; XClassHint * class_hints; @@ -2093,6 +2099,7 @@ main(int argc, char *argv[]) force_input = True; do_output = False; do_follow = True; + trim_trailing_newline = False; } else if (OPT("--zeroflush") || OPT("-z")) { force_input = True; do_output = False; @@ -2104,6 +2111,8 @@ main(int argc, char *argv[]) selection = XA_SECONDARY; } else if (OPT("--clipboard") || OPT("-b")) { want_clipboard = True; + } else if (OPT("--trim")) { + trim_trailing_newline = True; } else if (OPT("--keep") || OPT("-k")) { do_keep = True; } else if (OPT("--exchange") || OPT("-x")) { @@ -2118,6 +2127,9 @@ main(int argc, char *argv[]) i++; if (i >= argc) goto usage_err; timeout_ms = strtol(argv[i], (char **)NULL, 10); if (timeout_ms < 0) timeout_ms = 0; + } else if (OPT("--name") || OPT("-m")) { + i++; if (i >= argc) goto usage_err; + window_name = argv[i]; } else if (OPT("--nodetach") || OPT("-n")) { no_daemon = True; } else if (OPT("--delete") || OPT("-d")) { @@ -2175,6 +2187,11 @@ main(int argc, char *argv[]) window = XCreateSimpleWindow (display, root, 0, 0, 1, 1, 0, black, black); print_debug (D_INFO, "Window id: 0x%x (unmapped)", window); + + if (window_name != NULL) { + XStoreName (display, window, window_name); + print_debug (D_INFO, "The name %s is assigned to the window", window_name); + } /* Set window name and class */ XStoreName(display, window, window_name); @@ -2292,16 +2309,21 @@ main(int argc, char *argv[]) if (do_output || force_output) { /* Get the current selection */ old_sel = get_selection_text (selection); - if (old_sel) - { - printf ("%s", old_sel); - if (!do_append && *old_sel != '\0' && isatty(1) && - old_sel[xs_strlen (old_sel) - 1] != '\n') - { - fflush (stdout); - fprintf (stderr, "\n\\ No newline at end of selection\n"); - } + if (old_sel) { + + if (trim_trailing_newline) { + unsigned int old_sel_len = xs_strlen(old_sel); + if (old_sel[old_sel_len - 1 ] == '\n') { + old_sel[old_sel_len - 1] = '\0'; + } } + + printf ("%s", old_sel); + if (!do_append && *old_sel != '\0' && isatty(1) && + old_sel[xs_strlen (old_sel) - 1] != '\n') { + fflush (stdout); + } + } } /* handle input and clear modes */ @@ -2319,6 +2341,14 @@ main(int argc, char *argv[]) new_sel = initialise_read (new_sel); if(!do_follow) new_sel = read_input (new_sel, False); + + if(trim_trailing_newline) { + unsigned int sel_len = xs_strlen(new_sel); + if (new_sel[sel_len - 1 ] == '\n') { + new_sel[sel_len - 1] = '\0'; + } + } + set_selection__daemon (selection, new_sel); }