JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Move Shift + Insert to shortcut
[st.git] / st.c
diff --git a/st.c b/st.c
index 5e7e4ac..02a3502 100644 (file)
--- a/st.c
+++ b/st.c
 #include <X11/extensions/Xdbe.h>
 #include <X11/Xft/Xft.h>
 #include <fontconfig/fontconfig.h>
+
 #define Glyph Glyph_
 #define Font Font_
+#define Draw XftDraw *
+#define Colour XftColor
+#define Colourmap Colormap
 
 #if   defined(__linux)
  #include <pty.h>
@@ -106,17 +110,19 @@ enum glyph_state {
 };
 
 enum term_mode {
-       MODE_WRAP       = 1,
+       MODE_WRAP        = 1,
        MODE_INSERT      = 2,
        MODE_APPKEYPAD   = 4,
        MODE_ALTSCREEN   = 8,
-       MODE_CRLF       = 16,
+       MODE_CRLF        = 16,
        MODE_MOUSEBTN    = 32,
        MODE_MOUSEMOTION = 64,
        MODE_MOUSE       = 32|64,
        MODE_REVERSE     = 128,
        MODE_KBDLOCK     = 256,
-       MODE_HIDE      = 512
+       MODE_HIDE        = 512,
+       MODE_ECHO        = 1024,
+       MODE_APPCURSOR   = 2048
 };
 
 enum escape_state {
@@ -198,14 +204,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? */
@@ -255,6 +261,7 @@ typedef struct {
 
 /* function definitions used in config.h */
 static void xzoom(const Arg *);
+static void selpaste(const Arg *);
 
 /* Config.h for applying patches and the configuration. */
 #include "config.h"
@@ -267,13 +274,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)];
-       GC gc;
+       Colour col[LEN(colorname) < 256 ? 256 : LEN(colorname)];
        Font font, bfont, ifont, ibfont;
 } DC;
 
@@ -317,6 +323,7 @@ static void tswapscreen(void);
 static void tsetdirt(int, int);
 static void tsetmode(bool, bool, int *, int);
 static void tfulldirt(void);
+static void techo(char *, int);
 
 static void ttynew(void);
 static void ttyread(void);
@@ -354,7 +361,6 @@ static void selrequest(XEvent *);
 static void selinit(void);
 static inline bool selected(int, int);
 static void selcopy(void);
-static void selpaste(void);
 static void selscroll(int, int);
 
 static int utf8decode(char *, long *);
@@ -744,7 +750,7 @@ selnotify(XEvent *e) {
 }
 
 void
-selpaste(void) {
+selpaste(const Arg *dummy) {
        XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
                        xw.win, CurrentTime);
 }
@@ -815,7 +821,7 @@ brelease(XEvent *e) {
        }
 
        if(e->xbutton.button == Button2) {
-               selpaste();
+               selpaste(NULL);
        } else if(e->xbutton.button == Button1) {
                sel.mode = 0;
                getbuttoninfo(e, NULL, &sel.ex, &sel.ey);
@@ -1459,13 +1465,14 @@ 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) {
                        switch(*args) {
                                break;
                        case 1: /* DECCKM -- Cursor key */
-                               MODBIT(term.mode, set, MODE_APPKEYPAD);
+                               MODBIT(term.mode, set, MODE_APPCURSOR);
                                break;
                        case 5: /* DECSCNM -- Reverse video */
                                mode = term.mode;
@@ -1480,8 +1487,14 @@ tsetmode(bool priv, bool set, int *args, int narg) {
                        case 7: /* DECAWM -- Auto wrap */
                                MODBIT(term.mode, set, MODE_WRAP);
                                break;
-                       case 8:  /* DECARM -- Auto repeat (IGNORED) */
                        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: /* DECTCEM -- Text Cursor Enable Mode */
@@ -1496,7 +1509,7 @@ tsetmode(bool priv, bool set, int *args, int narg) {
                        case 1049: /* = 1047 and 1048 */
                        case 47:
                        case 1047: {
-                               bool alt = IS_SET(MODE_ALTSCREEN) != 0;
+                               alt = IS_SET(MODE_ALTSCREEN) != 0;
                                if(alt)
                                        tclearregion(0, 0, term.col-1, term.row-1);
                                if(set ^ alt)           /* set is always 1 or 0 */
@@ -1509,12 +1522,6 @@ tsetmode(bool priv, bool set, int *args, int narg) {
                                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);
@@ -1530,7 +1537,8 @@ tsetmode(bool priv, bool set, int *args, int narg) {
                        case 4:  /* IRM -- Insertion-replacement */
                                MODBIT(term.mode, set, MODE_INSERT);
                                break;
-                       case 12: /* XXX: SRM -- Send/Receive */
+                       case 12: /* SRM -- Send/Receive */
+                               MODBIT(term.mode, !set, MODE_ECHO);
                                break;
                        case 20: /* LNM -- Linefeed/new line */
                                MODBIT(term.mode, set, MODE_CRLF);
@@ -1845,6 +1853,28 @@ tputtab(bool forward) {
 }
 
 void
+techo(char *buf, int len) {
+       for(; len > 0; buf++, len--) {
+               char c = *buf;
+
+               if(c == '\033') {               /* escape */
+                       tputc("^", 1);
+                       tputc("[", 1);
+               } else if (c < '\x20') {        /* control code */
+                       if(c != '\n' && c != '\r' && c != '\t') {
+                               c |= '\x40';
+                               tputc("^", 1);
+                       }
+                       tputc(&c, 1);
+               } else {
+                       break;
+               }
+       }
+       if (len)
+               tputc(buf, len);
+}
+
+void
 tputc(char *c, int len) {
        uchar ascii = *c;
        bool control = ascii < '\x20' || ascii == 0177;
@@ -2150,19 +2180,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]);
                }
        }
@@ -2171,10 +2201,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++;
@@ -2183,9 +2213,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);
                }
        }
@@ -2193,8 +2223,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,
@@ -2206,8 +2236,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);
 }
 
@@ -2244,17 +2274,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;
@@ -2364,8 +2394,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
@@ -2386,15 +2416,13 @@ 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);
        xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
                                           | XIMStatusNothing, XNClientWindow, xw.win,
                                           XNFocusWindow, xw.win, NULL);
-       /* gc */
-       dc.gc = XCreateGC(xw.dpy, xw.win, 0, NULL);
 
        /* white cursor, black outline */
        cursor = XCreateFontCursor(xw.dpy, XC_xterm);
@@ -2419,20 +2447,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:
@@ -2449,8 +2477,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;
@@ -2460,8 +2488,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;
@@ -2475,7 +2503,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;
 
@@ -2493,12 +2521,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);
        }
 }
@@ -2677,7 +2705,7 @@ void
 kpress(XEvent *ev) {
        XKeyEvent *e = &ev->xkey;
        KeySym ksym;
-       char buf[32], *customkey;
+       char xstr[31], buf[32], *customkey, *cp = buf;
        int len, meta, shift, i;
        Status status;
 
@@ -2686,7 +2714,7 @@ kpress(XEvent *ev) {
 
        meta = e->state & Mod1Mask;
        shift = e->state & ShiftMask;
-       len = XmbLookupString(xw.xic, e, buf, sizeof(buf), &ksym, &status);
+       len = XmbLookupString(xw.xic, e, xstr, sizeof(xstr), &ksym, &status);
 
        /* 1. shortcuts */
        for(i = 0; i < LEN(shortcuts); i++) {
@@ -2700,7 +2728,8 @@ kpress(XEvent *ev) {
 
        /* 2. custom keys from config.h */
        if((customkey = kmap(ksym, e->state))) {
-               ttywrite(customkey, strlen(customkey));
+               len = strlen(customkey);
+               memcpy(buf, customkey, len);
        /* 2. hardcoded (overrides X lookup) */
        } else {
                switch(ksym) {
@@ -2712,34 +2741,37 @@ kpress(XEvent *ev) {
                        sprintf(buf, "\033%c%c",
                                IS_SET(MODE_APPKEYPAD) ? 'O' : '[',
                                (shift ? "dacb":"DACB")[ksym - XK_Left]);
-                       ttywrite(buf, 3);
-                       break;
-               case XK_Insert:
-                       if(shift)
-                               selpaste();
+                       len = 3;
                        break;
                case XK_Return:
+                       len = 0;
                        if(meta)
-                               ttywrite("\033", 1);
+                               *cp++ = '\033', len++;
 
-                       if(IS_SET(MODE_CRLF)) {
-                               ttywrite("\r\n", 2);
-                       } else {
-                               ttywrite("\r", 1);
-                       }
+                       *cp++ = '\r', len++;
+
+                       if(IS_SET(MODE_CRLF))
+                               *cp = '\n', len++;
                        break;
                        /* 3. X lookup  */
                default:
-                       if(len > 0) {
-                               if(meta && len == 1)
-                                       ttywrite("\033", 1);
-                               ttywrite(buf, len);
-                       }
+                       if(len == 0)
+                               return;
+
+                       if (len == 1 && meta)
+                               *cp++ = '\033';
+
+                       memcpy(cp, xstr, len);
+                       len = cp - buf + len;
                        break;
                }
        }
+       ttywrite(buf, len);
+       if(IS_SET(MODE_ECHO))
+               techo(buf, len);
 }
 
+
 void
 cmessage(XEvent *e) {
        /* See xembed specs