Skip to content

Commit

Permalink
Merge pull request #23 from hwangcc23/remove_last_newline
Browse files Browse the repository at this point in the history
Add the remove-last-newline option
  • Loading branch information
astrand committed Mar 14, 2016
2 parents 1175740 + aae47c9 commit 0a1bbcc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions xclip.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ print the selection to standard out (generally for piping to a file or program)
\fB\-f\fR, \fB\-filter\fR
when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified
.TP
\fB\-r\fR, \fB\-rmlastnl\fR
when the last character of the selection is a newline character, remove it. Newline characters that are not the last character in the selection are not affected. If the selection does not end with a newline character, this option has no effect. This option is useful for copying one-line output of programs like \fBpwd\fR to the clipboard to paste it again into the command prompt without executing the line immideately due to the newline character \fBpwd\fR appends.
.TP
\fB\-l\fR, \fB\-loops\fR
number of X selection requests (pastes into X applications) to wait for before exiting, with a value of 0 (default) causing xclip to wait for an unlimited number of requests until another application (possibly another invocation of xclip) takes ownership of the selection
.TP
Expand Down
27 changes: 25 additions & 2 deletions xclip.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "xclib.h"

/* command line option table for XrmParseCommand() */
XrmOptionDescRec opt_tab[13];
XrmOptionDescRec opt_tab[14];

/* Options that get set on the command line */
int sloop = 0; /* number of loops */
Expand All @@ -47,6 +47,7 @@ Atom target = XA_STRING;
static int fverb = OSILENT; /* output level */
static int fdiri = T; /* direction is in */
static int ffilt = F; /* filter mode */
static int frmnl = F; /* remove (single) newline character at the very end if present */

Display *dpy; /* connection to X11 display */
XrmDatabase opt_db = NULL; /* database for options */
Expand Down Expand Up @@ -100,6 +101,12 @@ doOptMain(int argc, char *argv[])
ffilt = T;
}

/* set "remove last newline character if present" mode */
if (XrmGetResource(opt_db, "xclip.rmlastnl", "Xclip.RmLastNl", &rec_typ, &rec_val)
) {
frmnl = T;
}

/* check for -help and -version */
if (XrmGetResource(opt_db, "xclip.print", "Xclip.Print", &rec_typ, &rec_val)
) {
Expand Down Expand Up @@ -253,6 +260,11 @@ doIn(Window win, const char *progname)
}
} while (fil_current < fil_number);

/* remove the last newline character if necessary */
if (frmnl && sel_len && sel_buf[sel_len - 1] == '\n') {
sel_len--;
}

/* if there are no files being read from (i.e., input
* is from stdin not files, and we are in filter mode,
* spit all the input back out to stdout
Expand Down Expand Up @@ -465,6 +477,11 @@ doOut(Window win)
}
}

/* remove the last newline character if necessary */
if (frmnl && sel_len && sel_buf[sel_len - 1] == '\n') {
sel_len--;
}

if (sel_len) {
/* only print the buffer out, and free it, if it's not
* empty
Expand All @@ -490,7 +507,7 @@ main(int argc, char *argv[])
* do it while sticking to pure ANSI C. The option and specifier
* members have a type of volatile char *, so they need to be allocated
* by strdup or malloc, you can't set them to a string constant at
* declare time, this is note pure ANSI C apparently, although it does
* declare time, this is not pure ANSI C apparently, although it does
* work with gcc
*/

Expand Down Expand Up @@ -572,6 +589,12 @@ main(int argc, char *argv[])
opt_tab[12].argKind = XrmoptionSepArg;
opt_tab[12].value = (XPointer) NULL;

/* "remove newline if it is the last character" entry */
opt_tab[13].option = xcstrdup("-rmlastnl");
opt_tab[13].specifier = xcstrdup(".rmlastnl");
opt_tab[13].argKind = XrmoptionNoArg;
opt_tab[13].value = (XPointer) xcstrdup(ST);

/* parse command line options */
doOptMain(argc, argv);

Expand Down
1 change: 1 addition & 0 deletions xcprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ prhelp(char *name)
"\"secondary\", \"clipboard\" or \"buffer-cut\")\n"
" -noutf8 don't treat text as utf-8, use old unicode\n"
" -target use the given target atom\n"
" -rmlastnl remove the last newline charater if present\n"
" -version version information\n"
" -silent errors only, run in background (default)\n"
" -quiet run in foreground, show what's happening\n"
Expand Down

0 comments on commit 0a1bbcc

Please sign in to comment.