"Fossies" - the Fresh Open Source Software Archive

Member "splitvt-1.6.6/cut-paste.c" (1 Apr 2007, 7051 Bytes) of package /linux/misc/old/splitvt-1.6.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cut-paste.c" see the Fossies "Dox" file reference documentation.

    1 
    2 /* The cut-paste module for splitvt */
    3 
    4 #include    <stdio.h>
    5 #include    <string.h>
    6 #include    "vt100.h"
    7 #include    "video.h"
    8 
    9 #define RIGHT 0x01
   10 #define LEFT  0x02
   11 #define Min(A,B)    ((A>B)?B:A)
   12 #define Max(A,B)    ((A>B)?A:B)
   13 
   14 /* Variables used by the cut-paste motion functions */
   15 static int marked, oldattr;
   16 static unsigned char on;
   17 
   18 extern struct physical physical;
   19 static int lastdirection=0;
   20 
   21 /* This function assumes that it alone will print selected text */
   22 static void put_sel_char(win, x, y, oldattr, on, direction)
   23 window *win;
   24 int x, y;
   25 int *oldattr;
   26 unsigned char *on;
   27 int direction;
   28 {
   29     int c, selected=0;
   30 
   31     c = get_video(win, x, y);
   32     /* Toggle selection highlighting */
   33     if ( direction ) {
   34 /*fprintf(stderr, "Turning %s...\n", direction == RIGHT ? "right" : "left");*/
   35         if ( lastdirection && lastdirection != direction )
   36             /* We turned... */ ;
   37         else {
   38             c ^= (SELECTED<<8);
   39             selected = ( ((c>>8)&SELECTED) ? 1 : 0 );
   40         }
   41         lastdirection=direction;
   42     } else
   43         c ^= (SELECTED<<8);
   44 
   45     (*oldattr)=check_attr(c, *oldattr, on);
   46     put_video(c, win, x, y);
   47 
   48     if ( c&0xFF )
   49         printf("%c", c&0xFF);
   50     else
   51         printf(" ");
   52 
   53     if ( direction ) {
   54         if ( !selected ) {
   55             if ( direction == RIGHT ) {
   56                 if ( y < win->cols )
   57                     ++y;
   58                 else
   59                     goto end;
   60             } else {
   61                 if ( y > 1 )
   62                     --y;
   63                 else
   64                     goto end;
   65                 vt_left(2);
   66             }
   67             c = get_video(win, x, y);
   68             if ( c&0xFF )
   69                 printf("%c", c&0xFF);
   70             else
   71                 printf(" ");
   72             vt_left(1);
   73         } else if ( direction == LEFT )
   74             vt_left(2);
   75     }
   76 end:
   77     fflush(stdout);
   78     return;
   79 }
   80 
   81 
   82 /* If this function returns 0, no selection was made */
   83 
   84 static char *extract_sel(win, buf, len, mark1, mark2)
   85 window *win;
   86 char *buf;
   87 int len;
   88 position *mark1, *mark2;
   89 {
   90     int selection=0;
   91     position startsel, endsel;
   92 
   93     if ( mark1->x == mark2->x )
   94     {
   95         if ( mark1->y == mark2->y ) {
   96             vt_info("Selection cancelled.");
   97             clrsel_video(win);
   98             reset_bar(1);
   99             return(NULL);
  100         }
  101         if ( mark1->y < mark2->y ) {
  102             startsel = (*mark1);
  103             endsel = (*mark2);
  104             ++endsel.y;
  105         } else {
  106             startsel = (*mark2);
  107             endsel = (*mark1);
  108             ++endsel.y;
  109         }
  110     } else if ( mark1->x < mark2->x ) {
  111         startsel = (*mark1);
  112         endsel = (*mark2);
  113     } else if ( mark1->x > mark2->x ) {
  114         startsel = (*mark2);
  115         endsel = (*mark1);
  116     }
  117     getsel_video(win,buf,len, startsel.x, endsel.x, startsel.y, endsel.y);
  118     clrsel_video(win);
  119     vt_info("Region selected.");
  120     reset_bar(1);
  121     return(buf);
  122 }
  123 
  124 static void line_down(win, cursor)
  125 window *win;
  126 position *cursor;
  127 {
  128     int j;
  129 
  130     if ( cursor->x < win->rows ) {
  131         if ( marked ) {
  132             for (j=cursor->y; j<win->cols; ++j) {
  133         put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
  134             }
  135             printf("\r");
  136             vt_down(1);
  137             ++cursor->x;
  138             for ( j=1; j<cursor->y; ++j ) {
  139         put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
  140             }
  141         } else {
  142             vt_down(1);
  143             ++cursor->x;
  144         }
  145     }
  146 }
  147 
  148 static void line_up(win, cursor)
  149 window *win;
  150 position *cursor;
  151 {
  152     int j;
  153 
  154     if ( cursor->x > 1 ) {
  155         --cursor->x;
  156         if ( marked ) {
  157             vt_up(1);
  158             for (j=cursor->y; j<win->cols; ++j) {
  159         put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
  160             }
  161             printf("\r");
  162             vt_down(1);
  163             ++cursor->x;
  164             for ( j=1; j<cursor->y; ++j ) {
  165         put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
  166             }
  167             vt_up(1);
  168             --cursor->x;
  169         } else 
  170             vt_up(1);
  171     }
  172 }
  173 
  174 static void move_left(win, cursor)
  175 window *win;
  176 position *cursor;
  177 {
  178     if ( cursor->y > 1 ) {
  179         if ( marked ) {
  180     put_sel_char(win, cursor->x, cursor->y, &oldattr, &on, LEFT);
  181         } else
  182             vt_left(1);
  183         --cursor->y;
  184     }
  185 }
  186 
  187 static void move_right(win, cursor)
  188 window *win;
  189 position *cursor;
  190 {
  191     if ( cursor->y < win->cols ) {
  192         if ( marked )
  193     put_sel_char(win, cursor->x, cursor->y, &oldattr, &on, RIGHT);
  194         else
  195             vt_right(1);
  196         ++cursor->y;
  197     }
  198 }
  199 
  200 static int use_xcb=0;   /* Do we use xcb to access X selection buffers? */
  201 void vt_initsel()
  202 {
  203     extern char *pathsearch();  /* From misc.c */
  204     char *display;
  205 
  206 #ifdef USE_XCB
  207     /* Use xcb if we have both an X display and the command. */
  208     /* xcb needs to support my hacked '-R' option. */
  209     if ( (display=(char *)getenv("DISPLAY")) && pathsearch("xcb", 1) )
  210         use_xcb=1;
  211     else
  212 #endif
  213         use_xcb=0;
  214 }
  215 
  216 static char selbuf[BUFSIZ];
  217 char *vt_getselbuf()
  218 {
  219     FILE *safe_popen();     /* From misc.c */
  220     FILE *xcb;
  221     char buffer[BUFSIZ];
  222     int len;
  223 
  224     if ( use_xcb ) {
  225         if ( (xcb=safe_popen("xcb -S 0 && xcb -p 0", "r")) == NULL )
  226             return(selbuf);
  227         len=fread(buffer, sizeof(char), BUFSIZ-1, xcb);
  228         (void) safe_pclose(xcb);
  229 
  230         if ( len > 0 ) {
  231             buffer[len]='\0';
  232             strcpy(selbuf, buffer);
  233         }
  234     }
  235     return(selbuf);
  236 }
  237 char *vt_setselbuf(buffer)
  238 char *buffer;
  239 {
  240     FILE *safe_popen();     /* From misc.c */
  241     FILE *xcb;
  242     
  243     strncpy(selbuf, buffer, BUFSIZ-1);
  244     selbuf[BUFSIZ-1]='\0';
  245     if (use_xcb && (xcb=safe_popen("xcb -s 0; xcb -RT 0", "w")) != NULL) {
  246         (void) fwrite(selbuf, sizeof(char), strlen(selbuf), xcb);
  247         (void) safe_pclose(xcb);
  248     }
  249     return(selbuf);
  250 }
  251 
  252 /* Get a window selection */
  253 char *vt_getsel(win, buf, len)
  254 int win;
  255 char *buf;
  256 int len;
  257 {
  258     int c, state=NORMAL;
  259     position here, cursor, mark1, mark2;
  260     window *thiswin;
  261     
  262     thiswin = physical.subwins[win];
  263     here=cursor=thiswin->cursor;
  264     marked=lastdirection=0;
  265     vt_info("Mark beginning of selection: ");
  266     while ( (c=getchar()) != EOF ) {
  267         switch (c) {
  268             case 'j':   /* Move down one line */
  269                     line_down(thiswin, &cursor);
  270                     break;
  271             case 'k':   /* Move up one line */
  272                     line_up(thiswin, &cursor);
  273                     break;
  274             case 'l':   /* Move right one char */
  275                     move_right(thiswin, &cursor);
  276                     break;
  277             case 'h':   /* Move left one char */
  278                     move_left(thiswin, &cursor);
  279                     break;
  280             case ' ':   /* Mark selection */
  281                     if ( marked ) {
  282                         mark2=cursor;
  283                         /* Copy and deselect area */
  284                         buf=extract_sel(thiswin, buf,
  285                             len, &mark1, &mark2); 
  286                         /* Repaint the screen */
  287                         paint_video(thiswin);
  288                         return(buf);
  289                     } else {
  290                         mark1=cursor;
  291                         oldattr = get_video(thiswin,
  292                             mark1.x, mark1.y);
  293                         on = (oldattr>>8);
  294                         marked=1;
  295                         vt_info(
  296                         "Mark end of selection: ");
  297                         /* Set reverse here */
  298                         vt_update();
  299                     }
  300                     break;
  301             case '\033':
  302             case 'q':   /* Cancel selection */
  303                     clrsel_video(thiswin);
  304                     vt_goto((here.x+thiswin->row_offset),
  305                                     here.y);
  306                     vt_info("Selection cancelled.");
  307                     sleep(1);
  308                     /* Repaint the screen */
  309                     paint_video(thiswin);
  310                     return(NULL);
  311                     break;
  312             default:    break;
  313         }
  314     }
  315     return; /* Hopefully, we never reach here */
  316 }
  317 
  318 
  319 /* Set a window selection */
  320 char *vt_setsel(buf, len, startx, endx, starty, endy)
  321 char *buf;
  322 int len;
  323 int startx, starty;
  324 int endx, endy;
  325 {
  326     window *thiswin;
  327 
  328     /* If nothing selected, return old selection */
  329     if ( (startx == endx) && (starty == endy) )
  330         return(buf);
  331 
  332     if ( startx == (physical.subwins[LOWER])->row_offset ) {
  333         /* Separator bar; return */
  334         return(buf);
  335     } else if ( startx < (physical.subwins[LOWER])->row_offset ) {
  336         thiswin = physical.subwins[UPPER];
  337     } else {
  338         thiswin = physical.subwins[LOWER];
  339         startx-=thiswin->row_offset;
  340         endx-=thiswin->row_offset;
  341     }
  342     getsel_video(thiswin, buf, len, startx, endx, starty, endy);
  343     return(buf);
  344 }