X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=st.c;h=11ea9fcfab662968200c63b0c18180ca20ed3cee;hb=9d5ea14b9dffac863b41432e4ce12b5624592000;hp=4d04416d1946b02bdb277585d030bb9c9c3f0991;hpb=54bc450dbf057e66bed5f10d13d90a469e5ca8ae;p=st.git diff --git a/st.c b/st.c index 4d04416..11ea9fc 100644 --- a/st.c +++ b/st.c @@ -16,10 +16,14 @@ #include #include #include -#include #include -#include +#include #include +#include +#include + +#include +#include #if defined(__linux) #include @@ -31,7 +35,7 @@ #define USAGE \ "st-" VERSION ", (c) 2010 st engineers\n" \ - "usage: st [-t title] [-c class] [-e cmd] [-v]\n" + "usage: st [-t title] [-c class] [-v] [-e cmd]\n" /* Arbitrary sizes */ #define ESC_TITLE_SIZ 256 @@ -44,11 +48,12 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) < (b) ? (b) : (a)) #define LEN(a) (sizeof(a) / sizeof(a[0])) -#define DEFAULT(a, b) (a) = (a) ? (a) : (b) +#define DEFAULT(a, b) (a) = (a) ? (a) : (b) #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b)) #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg) #define IS_SET(flag) (term.mode & (flag)) +#define TIMEDIFFERENCE(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000) /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */ enum { ATTR_NULL=0 , ATTR_REVERSE=1 , ATTR_UNDERLINE=2, ATTR_BOLD=4, ATTR_GFX=8 }; @@ -56,10 +61,9 @@ enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, CURSOR_SAVE, CURSOR_LOAD }; enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 }; enum { GLYPH_SET=1, GLYPH_DIRTY=2 }; -enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8, +enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8, MODE_CRLF=16 }; enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 }; -enum { SCREEN_UPDATE, SCREEN_REDRAW }; enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 }; #undef B0 @@ -86,21 +90,21 @@ typedef struct { /* ESC '[' [[ [] [;]] ] */ typedef struct { char buf[ESC_BUF_SIZ]; /* raw string */ - int len; /* raw string length */ + int len; /* raw string length */ char priv; int arg[ESC_ARG_SIZ]; - int narg; /* nb of args */ + int narg; /* nb of args */ char mode; } CSIEscape; /* Internal representation of the screen */ typedef struct { - int row; /* nb row */ + int row; /* nb row */ int col; /* nb col */ Line* line; /* screen */ Line* alt; /* alternate screen */ TCursor c; /* cursor */ - int top; /* top scroll limit */ + int top; /* top scroll limit */ int bot; /* bottom scroll limit */ int mode; /* terminal mode flags */ int esc; /* escape state flags */ @@ -117,17 +121,18 @@ typedef struct { XIM xim; XIC xic; int scr; - int w; /* window width */ + int w; /* window width */ int h; /* window height */ int bufw; /* pixmap width */ int bufh; /* pixmap height */ int ch; /* char height */ int cw; /* char width */ char state; /* focus, redraw, visible */ -} XWindow; +} XWindow; typedef struct { KeySym k; + unsigned int mask; char s[ESC_BUF_SIZ]; } Key; @@ -149,14 +154,18 @@ typedef struct { int mode; int bx, by; int ex, ey; - struct {int x, y;} b, e; + struct {int x, y;} b, e; char *clip; + Atom xtarget; + struct timeval tclick1; + struct timeval tclick2; } Selection; #include "config.h" static void die(const char *errstr, ...); -static void draw(int); +static void draw(); +static void drawregion(int, int, int, int); static void execsh(void); static void sigchld(int); static void run(void); @@ -204,7 +213,7 @@ static void xresize(int, int); static void expose(XEvent *); static void visibility(XEvent *); static void unmap(XEvent *); -static char* kmap(KeySym); +static char* kmap(KeySym, unsigned int state); static void kpress(XEvent *); static void resize(XEvent *); static void focus(XEvent *); @@ -217,7 +226,7 @@ static void selrequest(XEvent *); static void selinit(void); static inline int selected(int, int); static void selcopy(void); -static void selpaste(void); +static void selpaste(); static int utf8decode(char *, long *); static int utf8encode(long *, char *); @@ -247,7 +256,7 @@ static CSIEscape escseq; static int cmdfd; static pid_t pid; static Selection sel; -static char *opt_cmd = NULL; +static char **opt_cmd = NULL; static char *opt_title = NULL; static char *opt_class = NULL; @@ -338,7 +347,7 @@ isfullutf8(char *s, int b) { else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) return 0; else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) && - ((b == 1) || + ((b == 1) || ((b == 2) && (*c2&(B7|B6)) == B7))) return 0; else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) && @@ -360,25 +369,30 @@ utf8size(char *s) { return 2; else if ((c&(B7|B6|B5|B4)) == (B7|B6|B5)) return 3; - else + else return 4; } void selinit(void) { + sel.tclick1.tv_sec = 0; + sel.tclick1.tv_usec = 0; sel.mode = 0; sel.bx = -1; sel.clip = NULL; + sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0); + if(sel.xtarget == None) + sel.xtarget = XA_STRING; } -static inline int +static inline int selected(int x, int y) { if(sel.ey == y && sel.by == y) { int bx = MIN(sel.bx, sel.ex); int ex = MAX(sel.bx, sel.ex); return BETWEEN(x, bx, ex); } - return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x)) + return ((sel.b.y < y&&y < sel.e.y) || (y==sel.e.y && x<=sel.e.x)) || (y==sel.b.y && x>=sel.b.x && (x<=sel.e.x || sel.b.y!=sel.e.y)); } @@ -387,8 +401,8 @@ getbuttoninfo(XEvent *e, int *b, int *x, int *y) { if(b) *b = e->xbutton.button; - *x = e->xbutton.x/xw.cw; - *y = e->xbutton.y/xw.ch; + *x = (e->xbutton.x - BORDER)/xw.cw; + *y = (e->xbutton.y - BORDER)/xw.ch; sel.b.x = sel.by < sel.ey ? sel.bx : sel.ex; sel.b.y = MIN(sel.by, sel.ey); sel.e.x = sel.by < sel.ey ? sel.ex : sel.bx; @@ -398,8 +412,8 @@ getbuttoninfo(XEvent *e, int *b, int *x, int *y) { void bpress(XEvent *e) { sel.mode = 1; - sel.ex = sel.bx = e->xbutton.x/xw.cw; - sel.ey = sel.by = e->xbutton.y/xw.ch; + sel.ex = sel.bx = (e->xbutton.x - BORDER)/xw.cw; + sel.ey = sel.by = (e->xbutton.y - BORDER)/xw.ch; } void @@ -419,8 +433,8 @@ selcopy(void) { memcpy(ptr, term.line[y][x].c, sl); ptr += sl; } - if(ls) - *ptr = '\n', ptr++; + if(ls && y < sel.e.y) + *ptr++ = '\n'; } *ptr = 0; } @@ -452,7 +466,7 @@ selnotify(XEvent *e) { void selpaste() { - XConvertSelection(xw.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xw.win, CurrentTime); + XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY, xw.win, CurrentTime); } void @@ -473,12 +487,12 @@ selrequest(XEvent *e) { xa_targets = XInternAtom(xw.dpy, "TARGETS", 0); if(xsre->target == xa_targets) { /* respond with the supported type */ - Atom string = XA_STRING; + Atom string = sel.xtarget; XChangeProperty(xsre->display, xsre->requestor, xsre->property, XA_ATOM, 32, PropModeReplace, (unsigned char *) &string, 1); xev.property = xsre->property; - } else if(xsre->target == XA_STRING) { + } else if(xsre->target == sel.xtarget) { XChangeProperty(xsre->display, xsre->requestor, xsre->property, xsre->target, 8, PropModeReplace, (unsigned char *) sel.clip, strlen(sel.clip)); @@ -506,30 +520,63 @@ xsetsel(char *str) { XFlush(xw.dpy); } -/* TODO: doubleclick to select word */ void brelease(XEvent *e) { int b; sel.mode = 0; getbuttoninfo(e, &b, &sel.ex, &sel.ey); - if(sel.bx==sel.ex && sel.by==sel.ey) { + + if(sel.bx == sel.ex && sel.by == sel.ey) { sel.bx = -1; - if(b==2) + if(b == 2) selpaste(); + + else if(b == 1) { + /* double click to select word */ + struct timeval now; + gettimeofday(&now, NULL); + + if(TIMEDIFFERENCE(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) { + sel.bx = sel.ex; + while(term.line[sel.ey][sel.bx-1].state & GLYPH_SET && + term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--; + sel.b.x = sel.bx; + while(term.line[sel.ey][sel.ex+1].state & GLYPH_SET && + term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++; + sel.e.x = sel.ex; + sel.b.y = sel.e.y = sel.ey; + selcopy(); + } + + /* triple click on the line */ + if(TIMEDIFFERENCE(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) { + sel.b.x = sel.bx = 0; + sel.e.x = sel.ex = term.col; + sel.b.y = sel.e.y = sel.ey; + selcopy(); + } + } } else { - if(b==1) + if(b == 1) selcopy(); } - draw(1); + memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval)); + gettimeofday(&sel.tclick1, NULL); + draw(); } void bmotion(XEvent *e) { - if (sel.mode) { + if(sel.mode) { + int oldey = sel.ey, + oldex = sel.ex; getbuttoninfo(e, NULL, &sel.ex, &sel.ey); - /* XXX: draw() can't keep up, disabled for now. - selection is visible on button release. - draw(1); */ + + if(oldey != sel.ey || oldex != sel.ex) { + int starty = MIN(oldey, sel.ey); + int endy = MAX(oldey, sel.ey); + drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row)); + } } } @@ -545,13 +592,14 @@ die(const char *errstr, ...) { void execsh(void) { - char *args[] = {getenv("SHELL"), "-i", NULL}; - if(opt_cmd) - args[0] = opt_cmd, args[1] = NULL; - else - DEFAULT(args[0], SHELL); + char **args; + char *envshell = getenv("SHELL"); + + DEFAULT(envshell, "sh"); putenv("TERM="TNAME); + args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL}; execvp(args[0], args); + exit(EXIT_FAILURE); } void @@ -635,6 +683,10 @@ ttyread(void) { void ttywrite(const char *s, size_t n) { + {size_t nn; + for(nn = 0; nn < n; nn++) + dump(s[nn]); + } if(write(cmdfd, s, n) == -1) die("write error on tty: %s\n", SERRNO); } @@ -933,7 +985,7 @@ csihandle(void) { switch(escseq.mode) { default: unknown: - printf("erresc: unknown csi "); + fprintf(stderr, "erresc: unknown csi "); csidump(); /* die(""); */ break; @@ -1205,7 +1257,7 @@ tputc(char *c) { term.c.attr.mode &= ~ATTR_GFX; break; default: - printf("esc unhandled charset: ESC ( %c\n", ascii); + fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii); } term.esc = 0; } else { @@ -1502,6 +1554,7 @@ initfonts(char *fontstr, char *bfontstr) void xinit(void) { XSetWindowAttributes attrs; + Cursor cursor; if(!(xw.dpy = XOpenDisplay(NULL))) die("Can't open display\n"); @@ -1549,6 +1602,13 @@ xinit(void) { /* gc */ dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL); + /* white cursor, black outline */ + cursor = XCreateFontCursor(xw.dpy, XC_xterm); + XDefineCursor(xw.dpy, xw.win, cursor); + XRecolorCursor(xw.dpy, cursor, + &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff}, + &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000}); + XMapWindow(xw.dpy, xw.win); xhints(); XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st"); @@ -1626,8 +1686,13 @@ xdrawc(int x, int y, Glyph g) { dc.gc, r.x, r.y+dc.font.ascent, g.c, sl); } +void +drawregion(int x0, int x1, int y0, int y1) { + draw(); +} + void -draw(int dummy) { +draw() { int x, y; xclear(0, 0, term.col-1, term.row-1); @@ -1643,8 +1708,13 @@ draw(int dummy) { #else /* optimized drawing routine */ +void +draw() { + drawregion(0, 0, term.col, term.row); +} + void -draw(int redraw_all) { +drawregion(int x1, int y1, int x2, int y2) { int ic, ib, x, y, ox, sl; Glyph base, new; char buf[DRAW_BUF_SIZ]; @@ -1652,13 +1722,13 @@ draw(int redraw_all) { if(!(xw.state & WIN_VISIBLE)) return; - xclear(0, 0, term.col-1, term.row-1); - for(y = 0; y < term.row; y++) { + xclear(x1, y1, x2-1, y2-1); + for(y = y1; y < y2; y++) { base = term.line[y][0]; ic = ib = ox = 0; - for(x = 0; x < term.col; x++) { + for(x = x1; x < x2; x++) { new = term.line[y][x]; - if(sel.bx!=-1 && *(new.c) && selected(x, y)) + if(sel.bx != -1 && *(new.c) && selected(x, y)) new.mode ^= ATTR_REVERSE; if(ib > 0 && (!(new.state & GLYPH_SET) || ATTRCMP(base, new) || ib >= DRAW_BUF_SIZ-UTF_SIZ)) { @@ -1691,7 +1761,7 @@ expose(XEvent *ev) { if(xw.state & WIN_REDRAW) { if(!e->count) { xw.state &= ~WIN_REDRAW; - draw(SCREEN_REDRAW); + draw(); } } else XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, e->x-BORDER, e->y-BORDER, @@ -1728,14 +1798,14 @@ focus(XEvent *ev) { xseturgency(0); } else xw.state &= ~WIN_FOCUSED; - draw(SCREEN_UPDATE); + draw(); } char* -kmap(KeySym k) { +kmap(KeySym k, unsigned int state) { int i; for(i = 0; i < LEN(key); i++) - if(key[i].k == k) + if(key[i].k == k && (key[i].mask == 0 || key[i].mask & state)) return (char*)key[i].s; return NULL; } @@ -1756,7 +1826,7 @@ kpress(XEvent *ev) { len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status); /* 1. custom keys from config.h */ - if((customkey = kmap(ksym))) + if((customkey = kmap(ksym, e->state))) ttywrite(customkey, strlen(customkey)); /* 2. hardcoded (overrides X lookup) */ else @@ -1765,7 +1835,7 @@ kpress(XEvent *ev) { case XK_Down: case XK_Left: case XK_Right: - sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', "DACB"[ksym - XK_Left]); + sprintf(buf, "\033%c%c", IS_SET(MODE_APPKEYPAD) ? 'O' : '[', (shift ? "dacb":"DACB")[ksym - XK_Left]); ttywrite(buf, 3); break; case XK_Insert: @@ -1784,8 +1854,7 @@ kpress(XEvent *ev) { if(meta && len == 1) ttywrite("\033", 1); ttywrite(buf, len); - } else /* 4. nothing to send */ - fprintf(stderr, "errkey: %d\n", (int)ksym); + } break; } } @@ -1804,7 +1873,7 @@ resize(XEvent *e) { if(col == term.col && row == term.row) return; if(tresize(col, row)) - draw(SCREEN_REDRAW); + draw(); ttyresize(col, row); xresize(col, row); } @@ -1826,7 +1895,7 @@ run(void) { } if(FD_ISSET(cmdfd, &rfd)) { ttyread(); - draw(SCREEN_UPDATE); + draw(); } while(XPending(xw.dpy)) { XNextEvent(xw.dpy, &ev); @@ -1851,12 +1920,15 @@ main(int argc, char *argv[]) { if(++i < argc) opt_class = argv[i]; break; case 'e': - if(++i < argc) opt_cmd = argv[i]; + if(++i < argc) opt_cmd = &argv[i]; break; case 'v': default: die(USAGE); } + /* -e eats every remaining arguments */ + if(opt_cmd) + break; } setlocale(LC_CTYPE, ""); tnew(80, 24);