JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Merge branch 'master' of ssh://suckless.org/gitrepos/st
[st.git] / st.c
diff --git a/st.c b/st.c
index 9371b62..69b2491 100644 (file)
--- a/st.c
+++ b/st.c
@@ -134,6 +134,7 @@ enum term_mode {
        MODE_MOUSEX10    = 131072,
        MODE_MOUSEMANY   = 262144,
        MODE_BRCKTPASTE  = 524288,
+       MODE_PRINT       = 1048576,
        MODE_MOUSE       = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
                          |MODE_MOUSEMANY,
 };
@@ -313,6 +314,9 @@ static void clippaste(const Arg *);
 static void numlock(const Arg *);
 static void selpaste(const Arg *);
 static void xzoom(const Arg *);
+static void printsel(const Arg *);
+static void printscreen(const Arg *) ;
+static void toggleprinter(const Arg *);
 
 /* Config.h for applying patches and the configuration. */
 #include "config.h"
@@ -355,6 +359,10 @@ static void strparse(void);
 static void strreset(void);
 
 static int tattrset(int);
+static void tprinter(char *s, size_t len);
+static void tdumpsel(void);
+static void tdumpline(int);
+static void tdump(void);
 static void tclearregion(int, int, int, int);
 static void tcursor(int);
 static void tdeletechar(int);
@@ -429,6 +437,7 @@ static void selrequest(XEvent *);
 static void selinit(void);
 static void selsort(void);
 static inline bool selected(int, int);
+static char *getsel(void);
 static void selcopy(void);
 static void selscroll(int, int);
 static void selsnap(int, int *, int *, int);
@@ -469,7 +478,7 @@ static STREscape strescseq;
 static int cmdfd;
 static pid_t pid;
 static Selection sel;
-static int iofd = -1;
+static int iofd = STDOUT_FILENO;
 static char **opt_cmd = NULL;
 static char *opt_io = NULL;
 static char *opt_title = NULL;
@@ -949,8 +958,8 @@ bpress(XEvent *e) {
        }
 }
 
-void
-selcopy(void) {
+char *
+getsel(void) {
        char *str, *ptr;
        int x, y, bufsize, size, i, ex;
        Glyph *gp, *last;
@@ -1009,7 +1018,12 @@ selcopy(void) {
                }
                *ptr = 0;
        }
-       xsetsel(str);
+       return str;
+}
+
+void
+selcopy(void) {
+       xsetsel(getsel());
 }
 
 void
@@ -1256,6 +1270,7 @@ ttynew(void) {
                cmdfd = m;
                signal(SIGCHLD, sigchld);
                if(opt_io) {
+                       term.mode |= MODE_PRINT;
                        iofd = (!strcmp(opt_io, "-")) ?
                                  STDOUT_FILENO :
                                  open(opt_io, O_WRONLY | O_CREAT, 0666);
@@ -1979,6 +1994,25 @@ csihandle(void) {
                DEFAULT(csiescseq.arg[0], 1);
                tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
                break;
+       case 'i': /* MC -- Media Copy */
+               switch(csiescseq.arg[0]) {
+               case 0:
+                       tdump();
+                       break;
+               case 1:
+                       tdumpline(term.c.y);
+                       break;
+               case 2:
+                       tdumpsel();
+                       break;
+               case 4:
+                       term.mode &= ~MODE_PRINT;
+                       break;
+               case 5:
+                       term.mode |= MODE_PRINT;
+                       break;
+               }
+               break;
        case 'c': /* DA -- Device Attributes */
                if(csiescseq.arg[0] == 0)
                        ttywrite(VT102ID, sizeof(VT102ID) - 1);
@@ -2252,6 +2286,64 @@ strreset(void) {
 }
 
 void
+tprinter(char *s, size_t len) {
+       if(iofd != -1 && xwrite(iofd, s, len) < 0) {
+               fprintf(stderr, "Error writing in %s:%s\n",
+                       opt_io, strerror(errno));
+               close(iofd);
+               iofd = -1;
+       }
+}
+
+void
+toggleprinter(const Arg *arg) {
+       term.mode ^= MODE_PRINT;
+}
+
+void
+printscreen(const Arg *arg) {
+       tdump();
+}
+
+void
+printsel(const Arg *arg) {
+       tdumpsel();
+}
+
+void
+tdumpsel(void)
+{
+       char *ptr;
+
+       ptr = getsel();
+       tprinter(ptr, strlen(ptr));
+       free(ptr);
+}
+
+void
+tdumpline(int n) {
+       Glyph *bp, *end;
+
+       bp = &term.line[n][0];
+       end = &bp[term.col-1];
+       while(end > bp && !strcmp(" ", end->c))
+               --end;
+       if(bp != end || strcmp(bp->c, " ")) {
+               for( ;bp <= end; ++bp)
+                       tprinter(bp->c, strlen(bp->c));
+       }
+       tprinter("\n", 1);
+}
+
+void
+tdump(void) {
+       int i;
+
+       for(i = 0; i < term.row; ++i)
+               tdumpline(i);
+}
+
+void
 tputtab(bool forward) {
        uint x = term.c.x;
 
@@ -2332,14 +2424,8 @@ tputc(char *c, int len) {
                width = wcwidth(u8char);
        }
 
-       if(iofd != -1) {
-               if(xwrite(iofd, c, len) < 0) {
-                       fprintf(stderr, "Error writing in %s:%s\n",
-                               opt_io, strerror(errno));
-                       close(iofd);
-                       iofd = -1;
-               }
-       }
+       if(IS_SET(MODE_PRINT))
+               tprinter(c, len);
 
        /*
         * STR sequences must be checked before anything else
@@ -3091,6 +3177,7 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                if(base.fg == defaultfg)
                        base.fg = defaultunderline;
        }
+
        if(IS_TRUECOL(base.fg)) {
                colfg.alpha = 0xffff;
                colfg.red = TRUERED(base.fg);
@@ -3113,8 +3200,6 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                bg = &dc.col[base.bg];
        }
 
-
-
        if(base.mode & ATTR_BOLD) {
                if(BETWEEN(base.fg, 0, 7)) {
                        /* basic system colors */
@@ -3144,7 +3229,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                        colfg.green = ~fg->color.green;
                        colfg.blue = ~fg->color.blue;
                        colfg.alpha = fg->color.alpha;
-                       XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
+                       XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
+                                       &revfg);
                        fg = &revfg;
                }
 
@@ -3155,7 +3241,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                        colbg.green = ~bg->color.green;
                        colbg.blue = ~bg->color.blue;
                        colbg.alpha = bg->color.alpha;
-                       XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
+                       XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
+                                       &revbg);
                        bg = &revbg;
                }
        }
@@ -3235,7 +3322,7 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                        u8fblen += u8cblen;
                }
                if(doesexist) {
-                       if (oneatatime)
+                       if(oneatatime)
                                continue;
                        break;
                }
@@ -3258,6 +3345,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
                         * Nothing was found in the cache. Now use
                         * some dozen of Fontconfig calls to get the
                         * font for one single character.
+                        *
+                        * Xft and fontconfig are design failures.
                         */
                        fcpattern = FcPatternDuplicate(font->pattern);
                        fccharset = FcCharSetCreate();
@@ -3302,6 +3391,12 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
        }
 
        /*
+        * This is how the loop above actually should be. Why does the
+        * application have to care about font details?
+        *
+        * I have to repeat: Xft and Fontconfig are design failures.
+        */
+       /*
        XftDrawStringUtf8(xw.draw, fg, font->set, winx,
                        winy + font->ascent, (FcChar8 *)s, bytelen);
        */