JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix segfault when selecting big buffers
[st.git] / st.c
diff --git a/st.c b/st.c
index 4a89876..c2f294a 100644 (file)
--- a/st.c
+++ b/st.c
@@ -185,9 +185,9 @@ static char* kmap(KeySym);
 static void kpress(XEvent *);
 static void resize(XEvent *);
 static void focus(XEvent *);
-static void brelease(XEvent *e);
-static void bpress(XEvent *e);
-static void bmotion(XEvent *e);
+static void brelease(XEvent *);
+static void bpress(XEvent *);
+static void bmotion(XEvent *);
 
 
 static void (*handler[LASTEvent])(XEvent *) = {
@@ -210,7 +210,12 @@ static int cmdfd;
 static pid_t pid;
 static Selection sel;
 
-/* TODO: use X11 clipboard */
+void
+selinit(void) {
+       sel.mode = 0;
+       sel.bx = -1;
+       sel.clip = NULL;
+}
 
 static inline int selected(int x, int y) {
        if ((sel.ey==y && sel.by==y)) {
@@ -244,52 +249,43 @@ static char *getseltext() {
        int ls, x, y, sz;
        if(sel.bx==-1)
                return NULL;
-       sz = ((xw.w/xw.ch) * (sel.e[1]-sel.b[1]+2));
+       sz = ((term.col+1) * (sel.e[1]-sel.b[1]+1));
        ptr = str = malloc (sz);
        for(y = 0; y < term.row; y++) {
                for(x = 0; x < term.col; x++) {
-                       if(term.line[y][x].state & GLYPH_SET && (ls=selected(x, y))) {
-                               *ptr = term.line[y][x].c;
-                               ptr++;
-                       }
-               }
-               if (ls) {
-                       *ptr = '\n';
-                       ptr++;
+                       if(term.line[y][x].state & GLYPH_SET && (ls=selected(x, y)))
+                               *ptr = term.line[y][x].c, ptr++;
                }
+               if (ls)
+                       *ptr = '\n', ptr++;
        }
        *ptr = 0;
        return str;
 }
 
-static void clipboard_copy(char *str) {
+/* TODO: use X11 clipboard */
+static void selcopy(char *str) {
        free(sel.clip);
        sel.clip = str;
 }
 
-static void clipboard_paste() {
+static void selpaste() {
        if(sel.clip)
                ttywrite(sel.clip, strlen(sel.clip));
 }
 
-// TODO: doubleclick to select word
+/* TODO: doubleclick to select word */
 static void brelease(XEvent *e) {
        int b;
        sel.mode = 0;
        getbuttoninfo(e, &b, &sel.ex, &sel.ey);
-       if(b==4)
-               tscrollup(1);
-       else
-       if(b==5)
-               tscrolldown(1);
-       else
        if(sel.bx==sel.ex && sel.by==sel.ey) {
                sel.bx = -1;
                if(b==2)
-                       clipboard_paste();
+                       selpaste();
        } else {
                if(b==1)
-                       clipboard_copy(getseltext());
+                       selcopy(getseltext());
        }
        draw(1);
 }
@@ -1214,9 +1210,14 @@ xinit(void) {
        xw.bufw = xw.w - 2*BORDER;
        xw.bufh = xw.h - 2*BORDER;
        xw.buf = XCreatePixmap(xw.dis, xw.win, xw.bufw, xw.bufh, XDefaultDepth(xw.dis, xw.scr));
-       xw.hasfocus = 1;
        /* gc */
        dc.gc = XCreateGC(xw.dis, xw.win, 0, NULL);
+       
+       /* event mask */
+       XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask
+               | StructureNotifyMask | FocusChangeMask | PointerMotionMask
+               | ButtonPressMask | ButtonReleaseMask);
+       
        XMapWindow(xw.dis, xw.win);
        xhints();
        XStoreName(xw.dis, xw.win, "st");
@@ -1400,7 +1401,7 @@ kpress(XEvent *ev) {
                        break;
                case XK_Insert:
                        if(shift)
-                               draw(1), puts("draw!")/* XXX: paste X clipboard */;
+                               selpaste(), draw(1);
                        break;
                default:
                        fprintf(stderr, "errkey: %d\n", (int)ksym);
@@ -1435,12 +1436,6 @@ run(void) {
        XEvent ev;
        fd_set rfd;
        int xfd = XConnectionNumber(xw.dis);
-       long mask = ExposureMask | KeyPressMask | StructureNotifyMask
-               | FocusChangeMask | PointerMotionMask | ButtonPressMask 
-               | ButtonReleaseMask;
-
-       XSelectInput(xw.dis, xw.win, mask);
-       XResizeWindow(xw.dis, xw.win, xw.w, xw.h); /* XXX: fix resize bug in wmii (?) */
 
        for(;;) {
                FD_ZERO(&rfd);
@@ -1473,6 +1468,7 @@ main(int argc, char *argv[]) {
        tnew(80, 24);
        ttynew();
        xinit();
+       selinit();
        run();
        return 0;
 }