X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=st.c;h=23a43c18169c1124b6568d1c45bb078daf261164;hb=5dc48af29e9972527767977be3bbc9c072cf1c13;hp=18935d4aac0f75133802b3681cfb3848e676b8e6;hpb=580c8bbd4691218849c9a809acaa4c95f65cb846;p=st.git diff --git a/st.c b/st.c index 18935d4..23a43c1 100644 --- a/st.c +++ b/st.c @@ -27,6 +27,10 @@ #include #include +#include "arg.h" + +char *argv0; + #define Glyph Glyph_ #define Font Font_ #define Draw XftDraw * @@ -41,10 +45,6 @@ #include #endif -#define USAGE \ - "st " VERSION " (c) 2010-2013 st engineers\n" \ - "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \ - " [-t title] [-w windowid] [-e command ...]\n" /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 @@ -758,7 +758,7 @@ bpress(XEvent *e) { void selcopy(void) { char *str, *ptr, *p; - int x, y, bufsize, is_selected = 0, size; + int x, y, bufsize, isselected = 0, size; Glyph *gp, *last; if(sel.bx == -1) { @@ -769,7 +769,7 @@ selcopy(void) { /* append every set & selected glyph to the selection */ for(y = sel.b.y; y < sel.e.y + 1; y++) { - is_selected = 0; + isselected = 0; gp = &term.line[y][0]; last = gp + term.col; @@ -780,7 +780,7 @@ selcopy(void) { if(!selected(x, y)) { continue; } else { - is_selected = 1; + isselected = 1; } p = (gp->state & GLYPH_SET) ? gp->c : " "; @@ -788,9 +788,18 @@ selcopy(void) { memcpy(ptr, p, size); ptr += size; } - /* \n at the end of every selected line except for the last one */ - if(is_selected && y < sel.e.y) - *ptr++ = '\r'; + + /* + * Copy and pasting of line endings is inconsistent + * in the inconsistent terminal and GUI world. + * The best solution seems like to produce '\n' when + * something is copied from st and convert '\n' to + * '\r', when something to be pasted is received by + * st. + * FIXME: Fix the computer world. + */ + if(isselected && y < sel.e.y) + *ptr++ = '\n'; } *ptr = 0; } @@ -801,7 +810,7 @@ void selnotify(XEvent *e) { ulong nitems, ofs, rem; int format; - uchar *data; + uchar *data, *last, *repl; Atom type; ofs = 0; @@ -812,7 +821,21 @@ selnotify(XEvent *e) { fprintf(stderr, "Clipboard allocation failed\n"); return; } - ttywrite((const char *) data, nitems * format / 8); + + /* + * As seen in selcopy: + * Line endings are inconsistent in the terminal and GUI world + * copy and pasting. When receiving some selection data, + * replace all '\n' with '\r'. + * FIXME: Fix the computer world. + */ + repl = data; + last = data + nitems * format / 8; + while((repl = memchr(repl, '\n', last - repl))) { + *repl++ = '\r'; + } + + ttywrite((const char *)data, nitems * format / 8); XFree(data); /* number of 32-bit chunks returned */ ofs += nitems * format / 32; @@ -3308,70 +3331,67 @@ run(void) { } } +void +usage(void) { + die("%s " VERSION " (c) 2010-2013 st engineers\n" \ + "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \ + " [-t title] [-w windowid] [-e command ...]\n", argv0); +} + int main(int argc, char *argv[]) { - int i, bitm, xr, yr; + int bitm, xr, yr; uint wr, hr; xw.fw = xw.fh = xw.fx = xw.fy = 0; xw.isfixed = False; - for(i = 1; i < argc; i++) { - switch(argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) { - case 'a': - allowaltscreen = false; - break; - case 'c': - if(++i < argc) - opt_class = argv[i]; - break; - case 'e': - /* eat all remaining arguments */ - if(++i < argc) - opt_cmd = &argv[i]; - goto run; - case 'f': - if(++i < argc) - opt_font = argv[i]; - break; - case 'g': - if(++i >= argc) - break; - - bitm = XParseGeometry(argv[i], &xr, &yr, &wr, &hr); - if(bitm & XValue) - xw.fx = xr; - if(bitm & YValue) - xw.fy = yr; - if(bitm & WidthValue) - xw.fw = (int)wr; - if(bitm & HeightValue) - xw.fh = (int)hr; - if(bitm & XNegative && xw.fx == 0) - xw.fx = -1; - if(bitm & XNegative && xw.fy == 0) - xw.fy = -1; - - if(xw.fh != 0 && xw.fw != 0) - xw.isfixed = True; - break; - case 'o': - if(++i < argc) - opt_io = argv[i]; - break; - case 't': - if(++i < argc) - opt_title = argv[i]; - break; - case 'v': - default: - die(USAGE); - case 'w': - if(++i < argc) - opt_embed = argv[i]; - break; - } - } + ARGBEGIN { + case 'a': + allowaltscreen = false; + break; + case 'c': + opt_class = EARGF(usage()); + break; + case 'e': + /* eat all remaining arguments */ + if(argc > 1) + opt_cmd = &argv[1]; + goto run; + case 'f': + opt_font = EARGF(usage()); + break; + case 'g': + bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr); + if(bitm & XValue) + xw.fx = xr; + if(bitm & YValue) + xw.fy = yr; + if(bitm & WidthValue) + xw.fw = (int)wr; + if(bitm & HeightValue) + xw.fh = (int)hr; + if(bitm & XNegative && xw.fx == 0) + xw.fx = -1; + if(bitm & XNegative && xw.fy == 0) + xw.fy = -1; + + if(xw.fh != 0 && xw.fw != 0) + xw.isfixed = True; + break; + case 'o': + opt_io = EARGF(usage()); + break; + case 't': + opt_title = EARGF(usage()); + break; + case 'w': + opt_embed = EARGF(usage()); + break; + case 'v': + default: + usage(); + } ARGEND; run: setlocale(LC_CTYPE, "");