X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=st.c;h=c043e3fc544171a52bfb6082ed90323010130d3c;hb=c4a9ccec19b14a6bdc980d149e2c27c30f250945;hp=4c6783bf35dea13bcedfcdda4815c26e45d2f0ca;hpb=86261187ab92a037a44af72847f6ed304ea58e64;p=st.git diff --git a/st.c b/st.c index 4c6783b..c043e3f 100644 --- a/st.c +++ b/st.c @@ -28,8 +28,12 @@ #include #include #include + #define Glyph Glyph_ #define Font Font_ +#define Draw XftDraw * +#define Colour XftColor +#define Colourmap Colormap #if defined(__linux) #include @@ -96,8 +100,8 @@ enum cursor_movement { enum cursor_state { CURSOR_DEFAULT = 0, - CURSOR_HIDE = 1, - CURSOR_WRAPNEXT = 2 + CURSOR_WRAPNEXT = 1, + CURSOR_ORIGIN = 2 }; enum glyph_state { @@ -115,7 +119,8 @@ enum term_mode { MODE_MOUSEMOTION = 64, MODE_MOUSE = 32|64, MODE_REVERSE = 128, - MODE_KBDLOCK = 256 + MODE_KBDLOCK = 256, + MODE_HIDE = 512 }; enum escape_state { @@ -197,14 +202,14 @@ typedef struct { /* Purely graphic info */ typedef struct { - Display* dpy; - Colormap cmap; + Display *dpy; + Colourmap cmap; Window win; XdbeBackBuffer buf; Atom xembed, wmdeletewin; XIM xim; XIC xic; - XftDraw *xft_draw; + Draw draw; Visual *vis; int scr; bool isfixed; /* is fixed geometry? */ @@ -266,12 +271,12 @@ typedef struct { int descent; short lbearing; short rbearing; - XftFont *xft_set; + XftFont *set; } Font; /* Drawing Context */ typedef struct { - XftColor xft_col[LEN(colorname) < 256 ? 256 : LEN(colorname)]; + Colour col[LEN(colorname) < 256 ? 256 : LEN(colorname)]; GC gc; Font font, bfont, ifont, ibfont; } DC; @@ -300,6 +305,7 @@ static void tdeleteline(int); static void tinsertblank(int); static void tinsertblankline(int); static void tmoveto(int, int); +static void tmoveato(int x, int y); static void tnew(int, int); static void tnewline(int); static void tputtab(bool); @@ -673,6 +679,10 @@ bpress(XEvent *e) { sel.mode = 1; sel.ex = sel.bx = x2col(e->xbutton.x); sel.ey = sel.by = y2row(e->xbutton.y); + } else if(e->xbutton.button == Button4) { + ttywrite("\031", 1); + } else if(e->xbutton.button == Button5) { + ttywrite("\005", 1); } } @@ -1072,6 +1082,8 @@ treset(void) { term.mode = MODE_WRAP; tclearregion(0, 0, term.col-1, term.row-1); + tmoveto(0, 0); + tcursor(CURSOR_SAVE); } void @@ -1205,10 +1217,25 @@ csiparse(void) { } } +/* for absolute user moves, when decom is set */ +void +tmoveato(int x, int y) { + tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0)); +} + void tmoveto(int x, int y) { + int miny, maxy; + + if(term.c.state & CURSOR_ORIGIN) { + miny = term.top; + maxy = term.bot; + } else { + miny = 0; + maxy = term.row - 1; + } LIMIT(x, 0, term.col-1); - LIMIT(y, 0, term.row-1); + LIMIT(y, miny, maxy); term.c.state &= ~CURSOR_WRAPNEXT; term.c.x = x; term.c.y = y; @@ -1436,6 +1463,7 @@ tsetscroll(int t, int b) { void tsetmode(bool priv, bool set, int *args, int narg) { int *lim, mode; + bool alt; for(lim = args + narg; args < lim; ++args) { if(priv) { @@ -1450,18 +1478,25 @@ tsetmode(bool priv, bool set, int *args, int narg) { if(mode != term.mode) redraw(); break; - case 6: /* XXX: DECOM -- Origin */ + case 6: /* DECOM -- Origin */ + MODBIT(term.c.state, set, CURSOR_ORIGIN); + tmoveato(0, 0); break; case 7: /* DECAWM -- Auto wrap */ MODBIT(term.mode, set, MODE_WRAP); break; - case 8: /* XXX: DECARM -- Auto repeat */ - break; case 0: /* Error (IGNORED) */ + case 2: /* DECANM -- ANSI/VT52 (IGNORED) */ + case 3: /* DECCOLM -- Column (IGNORED) */ + case 4: /* DECSCLM -- Scroll (IGNORED) */ + case 8: /* DECARM -- Auto repeat (IGNORED) */ + case 18: /* DECPFF -- Printer feed (IGNORED) */ + case 19: /* DECPEX -- Printer extent (IGNORED) */ + case 42: /* DECNRCM -- National characters (IGNORED) */ case 12: /* att610 -- Start blinking cursor (IGNORED) */ break; - case 25: - MODBIT(term.c.state, !set, CURSOR_HIDE); + case 25: /* DECTCEM -- Text Cursor Enable Mode */ + MODBIT(term.mode, !set, MODE_HIDE); break; case 1000: /* 1000,1002: enable xterm mouse report */ MODBIT(term.mode, set, MODE_MOUSEBTN); @@ -1471,26 +1506,20 @@ tsetmode(bool priv, bool set, int *args, int narg) { break; case 1049: /* = 1047 and 1048 */ case 47: - case 1047: - if(IS_SET(MODE_ALTSCREEN)) + case 1047: { + alt = IS_SET(MODE_ALTSCREEN) != 0; + if(alt) tclearregion(0, 0, term.col-1, term.row-1); - if((set && !IS_SET(MODE_ALTSCREEN)) || - (!set && IS_SET(MODE_ALTSCREEN))) { + if(set ^ alt) /* set is always 1 or 0 */ tswapscreen(); - } if(*args != 1049) break; + } /* pass through */ case 1048: tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD); break; default: - /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */ - /* case 3: DECCOLM -- Column (NOT SUPPORTED) */ - /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */ - /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */ - /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */ - /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */ fprintf(stderr, "erresc: unknown private set/reset mode %d\n", *args); @@ -1537,11 +1566,11 @@ csihandle(void) { tinsertblank(csiescseq.arg[0]); break; case 'A': /* CUU -- Cursor Up */ - case 'e': DEFAULT(csiescseq.arg[0], 1); tmoveto(term.c.x, term.c.y-csiescseq.arg[0]); break; case 'B': /* CUD -- Cursor Down */ + case 'e': /* VPR --Cursor Down */ DEFAULT(csiescseq.arg[0], 1); tmoveto(term.c.x, term.c.y+csiescseq.arg[0]); break; @@ -1550,7 +1579,7 @@ csihandle(void) { ttywrite(VT102ID, sizeof(VT102ID) - 1); break; case 'C': /* CUF -- Cursor Forward */ - case 'a': + case 'a': /* HPR -- Cursor Forward */ DEFAULT(csiescseq.arg[0], 1); tmoveto(term.c.x+csiescseq.arg[0], term.c.y); break; @@ -1587,7 +1616,7 @@ csihandle(void) { case 'f': /* HVP */ DEFAULT(csiescseq.arg[0], 1); DEFAULT(csiescseq.arg[1], 1); - tmoveto(csiescseq.arg[1]-1, csiescseq.arg[0]-1); + tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1); break; case 'I': /* CHT -- Cursor Forward Tabulation tab stops */ DEFAULT(csiescseq.arg[0], 1); @@ -1661,7 +1690,7 @@ csihandle(void) { break; case 'd': /* VPA -- Move to */ DEFAULT(csiescseq.arg[0], 1); - tmoveto(term.c.x, csiescseq.arg[0]-1); + tmoveato(term.c.x, csiescseq.arg[0]-1); break; case 'h': /* SM -- Set terminal mode */ tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg); @@ -1676,7 +1705,7 @@ csihandle(void) { DEFAULT(csiescseq.arg[0], 1); DEFAULT(csiescseq.arg[1], term.row); tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1); - tmoveto(0, 0); + tmoveato(0, 0); } break; case 's': /* DECSC -- Save cursor position (ANSI.SYS) */ @@ -1834,8 +1863,8 @@ tputc(char *c, int len) { } } /* - * STR sequences must be checked before of anything - * because it can use some control codes as part of the sequence + * STR sequences must be checked before anything else + * because it can use some control codes as part of the sequence. */ if(term.esc & ESC_STR) { switch(ascii) { @@ -1855,6 +1884,7 @@ tputc(char *c, int len) { } return; } + /* * Actions of control codes must be performed as soon they arrive * because they can be embedded inside a control sequence, and @@ -1895,11 +1925,11 @@ tputc(char *c, int len) { case '\030': /* CAN */ csireset(); return; - case '\005': /* ENQ (IGNORED) */ - case '\000': /* NUL (IGNORED) */ - case '\021': /* XON (IGNORED) */ - case '\023': /* XOFF (IGNORED) */ - case 0177: /* DEL (IGNORED) */ + case '\005': /* ENQ (IGNORED) */ + case '\000': /* NUL (IGNORED) */ + case '\021': /* XON (IGNORED) */ + case '\023': /* XOFF (IGNORED) */ + case 0177: /* DEL (IGNORED) */ return; } } else if(term.esc & ESC_START) { @@ -2112,10 +2142,10 @@ tresize(int col, int row) { /* update terminal size */ term.col = col; term.row = row; - /* make use of the LIMIT in tmoveto */ - tmoveto(term.c.x, term.c.y); /* reset scrolling region */ tsetscroll(0, row-1); + /* make use of the LIMIT in tmoveto */ + tmoveto(term.c.x, term.c.y); return (slide > 0); } @@ -2125,19 +2155,19 @@ xresize(int col, int row) { xw.tw = MAX(1, 2*borderpx + col * xw.cw); xw.th = MAX(1, 2*borderpx + row * xw.ch); - XftDrawChange(xw.xft_draw, xw.buf); + XftDrawChange(xw.draw, xw.buf); } void xloadcols(void) { int i, r, g, b; - XRenderColor xft_color = { .alpha = 0 }; + XRenderColor color = { .alpha = 0 }; /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */ for(i = 0; i < LEN(colorname); i++) { if(!colorname[i]) continue; - if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.xft_col[i])) { + if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) { die("Could not allocate color '%s'\n", colorname[i]); } } @@ -2146,10 +2176,10 @@ xloadcols(void) { for(i = 16, r = 0; r < 6; r++) { for(g = 0; g < 6; g++) { for(b = 0; b < 6; b++) { - xft_color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r; - xft_color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g; - xft_color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b; - if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, &dc.xft_col[i])) { + color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r; + color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g; + color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b; + if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) { die("Could not allocate color %d\n", i); } i++; @@ -2158,9 +2188,9 @@ xloadcols(void) { } for(r = 0; r < 24; r++, i++) { - xft_color.red = xft_color.green = xft_color.blue = 0x0808 + 0x0a0a * r; - if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &xft_color, - &dc.xft_col[i])) { + color.red = color.green = color.blue = 0x0808 + 0x0a0a * r; + if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, + &dc.col[i])) { die("Could not allocate color %d\n", i); } } @@ -2168,8 +2198,8 @@ xloadcols(void) { void xtermclear(int col1, int row1, int col2, int row2) { - XftDrawRect(xw.xft_draw, - &dc.xft_col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg], + XftDrawRect(xw.draw, + &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg], borderpx + col1 * xw.cw, borderpx + row1 * xw.ch, (col2-col1+1) * xw.cw, @@ -2181,8 +2211,8 @@ xtermclear(int col1, int row1, int col2, int row2) { */ void xclear(int x1, int y1, int x2, int y2) { - XftDrawRect(xw.xft_draw, - &dc.xft_col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg], + XftDrawRect(xw.draw, + &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg], x1, y1, x2-x1, y2-y1); } @@ -2219,17 +2249,17 @@ xloadfont(Font *f, FcPattern *pattern) { match = XftFontMatch(xw.dpy, xw.scr, pattern, &result); if(!match) return 1; - if(!(f->xft_set = XftFontOpenPattern(xw.dpy, match))) { + if(!(f->set = XftFontOpenPattern(xw.dpy, match))) { FcPatternDestroy(match); return 1; } - f->ascent = f->xft_set->ascent; - f->descent = f->xft_set->descent; + f->ascent = f->set->ascent; + f->descent = f->set->descent; f->lbearing = 0; - f->rbearing = f->xft_set->max_advance_width; + f->rbearing = f->set->max_advance_width; - f->height = f->xft_set->height; + f->height = f->set->height; f->width = f->lbearing + f->rbearing; return 0; @@ -2339,8 +2369,8 @@ xinit(void) { xw.fy = 0; } - attrs.background_pixel = dc.xft_col[defaultbg].pixel; - attrs.border_pixel = dc.xft_col[defaultbg].pixel; + attrs.background_pixel = dc.col[defaultbg].pixel; + attrs.border_pixel = dc.col[defaultbg].pixel; attrs.bit_gravity = NorthWestGravity; attrs.event_mask = FocusChangeMask | KeyPressMask | ExposureMask | VisibilityChangeMask | StructureNotifyMask @@ -2361,7 +2391,7 @@ xinit(void) { xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win, XdbeCopied); /* Xft rendering context */ - xw.xft_draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap); + xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap); /* input methods */ xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL); @@ -2394,20 +2424,20 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) { width = charlen * xw.cw; Font *font = &dc.font; XGlyphInfo extents; - XftColor *fg = &dc.xft_col[base.fg], *bg = &dc.xft_col[base.bg], + Colour *fg = &dc.col[base.fg], *bg = &dc.col[base.bg], *temp, revfg, revbg; XRenderColor colfg, colbg; if(base.mode & ATTR_BOLD) { if(BETWEEN(base.fg, 0, 7)) { /* basic system colors */ - fg = &dc.xft_col[base.fg + 8]; + fg = &dc.col[base.fg + 8]; } else if(BETWEEN(base.fg, 16, 195)) { /* 256 colors */ - fg = &dc.xft_col[base.fg + 36]; + fg = &dc.col[base.fg + 36]; } else if(BETWEEN(base.fg, 232, 251)) { /* greyscale */ - fg = &dc.xft_col[base.fg + 4]; + fg = &dc.col[base.fg + 4]; } /* * Those ranges will not be brightened: @@ -2424,8 +2454,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) { font = &dc.ibfont; if(IS_SET(MODE_REVERSE)) { - if(fg == &dc.xft_col[defaultfg]) { - fg = &dc.xft_col[defaultbg]; + if(fg == &dc.col[defaultfg]) { + fg = &dc.col[defaultbg]; } else { colfg.red = ~fg->color.red; colfg.green = ~fg->color.green; @@ -2435,8 +2465,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) { fg = &revfg; } - if(bg == &dc.xft_col[defaultbg]) { - bg = &dc.xft_col[defaultfg]; + if(bg == &dc.col[defaultbg]) { + bg = &dc.col[defaultfg]; } else { colbg.red = ~bg->color.red; colbg.green = ~bg->color.green; @@ -2450,7 +2480,7 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) { if(base.mode & ATTR_REVERSE) temp = fg, fg = bg, bg = temp; - XftTextExtentsUtf8(xw.dpy, font->xft_set, (FcChar8 *)s, bytelen, + XftTextExtentsUtf8(xw.dpy, font->set, (FcChar8 *)s, bytelen, &extents); width = extents.xOff; @@ -2468,12 +2498,12 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) { if(y == term.row-1) xclear(winx, winy + xw.ch, winx + width, xw.h); - XftDrawRect(xw.xft_draw, bg, winx, winy, width, xw.ch); - XftDrawStringUtf8(xw.xft_draw, fg, font->xft_set, winx, + XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch); + XftDrawStringUtf8(xw.draw, fg, font->set, winx, winy + font->ascent, (FcChar8 *)s, bytelen); if(base.mode & ATTR_UNDERLINE) { - XftDrawRect(xw.xft_draw, fg, winx, winy + font->ascent + 1, + XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1, width, 1); } } @@ -2500,7 +2530,7 @@ xdrawcursor(void) { } /* draw the new one */ - if(!(term.c.state & CURSOR_HIDE)) { + if(!(IS_SET(MODE_HIDE))) { if(!(xw.state & WIN_FOCUSED)) g.bg = defaultucs; @@ -2541,9 +2571,9 @@ drawregion(int x1, int y1, int x2, int y2) { int ic, ib, x, y, ox, sl; Glyph base, new; char buf[DRAW_BUF_SIZ]; - bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN); + bool ena_sel = sel.bx != -1, alt = IS_SET(MODE_ALTSCREEN) != 0; - if((sel.alt && !alt) || (!sel.alt && alt)) + if((sel.alt != 0) ^ alt) ena_sel = 0; if(!(xw.state & WIN_VISIBLE)) return; @@ -2694,6 +2724,9 @@ kpress(XEvent *ev) { selpaste(); break; case XK_Return: + if(meta) + ttywrite("\033", 1); + if(IS_SET(MODE_CRLF)) { ttywrite("\r\n", 2); } else {