X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=st.c;h=46c0b6ef554b7dea153824426ff01f148c202ba2;hb=abfad4c4fc69ebb22febfe32677aadd112ce375a;hp=55a5c56eefc1feab52f7806bf1666564e19c1e45;hpb=980991fa6e7b737d54f8e5c60c26275890eb28ff;p=st.git diff --git a/st.c b/st.c index 55a5c56..46c0b6e 100644 --- a/st.c +++ b/st.c @@ -71,7 +71,7 @@ char *argv0; #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177') #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f)) #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c)) -#define ISDELIM(u) (BETWEEN(u, 0, 127) && strchr(worddelimiters, u) != NULL) +#define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL) #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)) != 0) @@ -191,8 +191,8 @@ typedef XftColor Color; typedef struct { Rune u; /* character code */ ushort mode; /* attribute flags */ - ushort fg; /* foreground */ - ushort bg; /* background */ + uint32_t fg; /* foreground */ + uint32_t bg; /* background */ } Glyph; typedef Glyph *Line; @@ -473,6 +473,7 @@ static size_t utf8decode(char *, Rune *, size_t); static Rune utf8decodebyte(char, size_t *); static size_t utf8encode(Rune, char *); static char utf8encodebyte(Rune, size_t); +static char *utf8strchr(char *s, Rune u); static size_t utf8validate(Rune *, size_t); static ssize_t xwrite(int, const char *, size_t); @@ -512,7 +513,7 @@ static STREscape strescseq; static int cmdfd; static pid_t pid; static Selection sel; -static int iofd = STDOUT_FILENO; +static int iofd = 1; static char **opt_cmd = NULL; static char *opt_io = NULL; static char *opt_title = NULL; @@ -640,6 +641,21 @@ utf8encodebyte(Rune u, size_t i) { return utfbyte[i] | (u & ~utfmask[i]); } +char * +utf8strchr(char *s, Rune u) { + Rune r; + size_t i, j, len; + + len = strlen(s); + for(i = 0, j = 0; i < len; i += j) { + if(!(j = utf8decode(&s[i], &r, len - i))) + break; + if(r == u) + return &(s[i]); + } + return NULL; +} + size_t utf8validate(Rune *u, size_t i) { if(!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF)) @@ -1069,6 +1085,7 @@ void selclear(XEvent *e) { if(sel.ob.x == -1) return; + sel.mode = SEL_IDLE; sel.ob.x = -1; tsetdirt(sel.nb.y, sel.ne.y); } @@ -1190,7 +1207,7 @@ die(const char *errstr, ...) { va_start(ap, errstr); vfprintf(stderr, errstr, ap); va_end(ap); - exit(EXIT_FAILURE); + exit(1); } void @@ -1239,12 +1256,12 @@ execsh(void) { signal(SIGALRM, SIG_DFL); execvp(prog, args); - _exit(EXIT_FAILURE); + _exit(1); } void sigchld(int a) { - int stat, ret; + int stat; pid_t p; if((p = waitpid(pid, &stat, WNOHANG)) < 0) @@ -1253,10 +1270,9 @@ sigchld(int a) { if(pid != p) return; - ret = WIFEXITED(stat) ? WEXITSTATUS(stat) : EXIT_FAILURE; - if (ret != EXIT_SUCCESS) + if (!WIFEXITED(stat) || WEXITSTATUS(stat)) die("child finished with error '%d'\n", stat); - exit(EXIT_SUCCESS); + exit(0); } @@ -1292,8 +1308,7 @@ ttynew(void) { if(opt_io) { term.mode |= MODE_PRINT; iofd = (!strcmp(opt_io, "-")) ? - STDOUT_FILENO : - open(opt_io, O_WRONLY | O_CREAT, 0666); + 1 : open(opt_io, O_WRONLY | O_CREAT, 0666); if(iofd < 0) { fprintf(stderr, "Error opening %s:%s\n", opt_io, strerror(errno)); @@ -1303,7 +1318,7 @@ ttynew(void) { if (opt_line) { if((cmdfd = open(opt_line, O_RDWR)) < 0) die("open line failed: %s\n", strerror(errno)); - close(STDIN_FILENO); + close(0); dup(cmdfd); stty(); return; @@ -1320,9 +1335,9 @@ ttynew(void) { case 0: close(iofd); setsid(); /* create a new process group */ - dup2(s, STDIN_FILENO); - dup2(s, STDOUT_FILENO); - dup2(s, STDERR_FILENO); + dup2(s, 0); + dup2(s, 1); + dup2(s, 2); if(ioctl(s, TIOCSCTTY, NULL) < 0) die("ioctl TIOCSCTTY failed: %s\n", strerror(errno)); close(s); @@ -3044,7 +3059,6 @@ xloadfont(Font *f, FcPattern *pattern) { void xloadfonts(char *fontstr, double fontsize) { FcPattern *pattern; - FcResult r_sz, r_psz; double fontval; float ceilf(float); @@ -3063,11 +3077,11 @@ xloadfonts(char *fontstr, double fontsize) { FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize); usedfontsize = fontsize; } else { - r_psz = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval); - r_sz = FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval); - if(r_psz == FcResultMatch) { + if(FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) == + FcResultMatch) { usedfontsize = fontval; - } else if(r_sz == FcResultMatch) { + } else if(FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) == + FcResultMatch) { usedfontsize = -1; } else { /* @@ -3140,14 +3154,14 @@ void xzoom(const Arg *arg) { Arg larg; - larg.i = usedfontsize + arg->i; + larg.f = usedfontsize + arg->f; xzoomabs(&larg); } void xzoomabs(const Arg *arg) { xunloadfonts(); - xloadfonts(usedfont, arg->i); + xloadfonts(usedfont, arg->f); cresize(0, 0); redraw(); xhints(); @@ -3158,7 +3172,7 @@ xzoomreset(const Arg *arg) { Arg larg; if(defaultfontsize > 0) { - larg.i = defaultfontsize; + larg.f = defaultfontsize; xzoomabs(&larg); } } @@ -3663,7 +3677,7 @@ drawregion(int x1, int y1, int x2, int y2) { term.dirty[y] = 0; specs = term.specbuf; - numspecs = xmakeglyphfontspecs(specs, &term.line[y][0], x2 - x1, x1, y); + numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y); i = ox = 0; for(x = x1; x < x2 && i < numspecs; x++) { @@ -3855,7 +3869,7 @@ cmessage(XEvent *e) { } else if(e->xclient.data.l[0] == xw.wmdeletewin) { /* Send SIGHUP to shell */ kill(pid, SIGHUP); - exit(EXIT_SUCCESS); + exit(0); } } @@ -3896,6 +3910,13 @@ run(void) { /* Waiting for window mapping */ do { XNextEvent(xw.dpy, &ev); + /* + * XFilterEvent is required to be called after you using XOpenIM, + * this is not unnecessary.It does not only filter the key event, + * but some clientmessage for input method as well. + */ + if(XFilterEvent(&ev, None)) + continue; if(ev.type == ConfigureNotify) { w = ev.xconfigure.width; h = ev.xconfigure.height;