home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / rvi / part3 / rv_undo.c < prev   
Encoding:
C/C++ Source or Header  |  1989-04-09  |  2.6 KB  |  138 lines

  1. #include "rv.h"
  2.  
  3. void
  4. rv_undo()
  5. /*
  6.  * undo last change
  7.  *
  8.  * undo is accomplished by deleting the last inserted text, and
  9.  * restoring the last deleted text, both of which are assumed
  10.  * to start at the same row,col position.
  11.  */
  12. {
  13.     struct ya_yank     *yank, *save_yank;
  14.     boolean deleted;
  15.     void rv_linecmd();
  16.     INT direction = -1;
  17.  
  18.     if (ed_undo) {
  19.         rv_linecmd("u");
  20.         ed_undo = TRUE;
  21.         return;
  22.     }
  23.         
  24.     yank = &yank_array[0];
  25.     save_yank = &yank_array[NUM_YANK_BUFS-1];
  26.  
  27.     /*
  28.      * See if there is something to undo
  29.      */
  30.     if (undo.un_deleted == FALSE && undo.un_inserted == FALSE) {
  31.         flash();
  32.         errflag = 1;
  33.         return;
  34.     }
  35.  
  36.     /*
  37.      * Save previous yanked (deleted) text
  38.      */
  39.     copy((char *)save_yank, (char *)yank, sizeof(struct ya_yank));
  40.     if (yank->ya_text) {
  41.         save_yank->ya_text = xalloc(strlen(yank->ya_text)+1);
  42.         strcpy(save_yank->ya_text, yank->ya_text);
  43.     }
  44.     deleted = undo.un_deleted;
  45.     undo.un_deleted = FALSE;
  46.  
  47.     if (undo.un_firstline > file.fi_numlines) {
  48.         direction = 1;
  49.         undo.un_firstline = file.fi_numlines;
  50.     }
  51.  
  52.     move_abs_cursor(undo.un_firstline, 0);
  53.     /*
  54.      * Delete last inserted text
  55.      */
  56.     if (undo.un_inserted) {
  57.         register struct sc_screen *sc;
  58.  
  59.         sc = &screen;
  60.         sc->sc_firstline = undo.un_firstline;
  61.         sc->sc_lastline = undo.un_lastline;
  62.         if (undo.un_validcol == FALSE)
  63.             sc->sc_validcol = FALSE;
  64.         else {
  65.             sc->sc_validcol = TRUE;
  66.             sc->sc_firstcol = undo.un_firstcol;
  67.             sc->sc_lastcol = undo.un_lastcol;
  68.         }
  69.         undo.un_inserted = FALSE;
  70.         delete();
  71.     }
  72.     /*
  73.      * Restore last deleted text
  74.      */
  75.     if (deleted && save_yank->ya_type != YANK_EMPTY) {
  76.         move_abs_cursor(undo.un_firstline, 0);
  77.         if (save_yank->ya_type == YANK_COLS)
  78.             screen.sc_column = undo.un_firstcol-1;
  79.         yank_cmd = '$';
  80.         put(direction);
  81.     }
  82.  
  83.     /*
  84.      * Free saved yank text
  85.      */
  86.     if (save_yank->ya_text) {
  87.         free(save_yank->ya_text);
  88.         save_yank->ya_text = NULL;
  89.     }
  90.     save_yank->ya_type = YANK_EMPTY;
  91. }
  92.  
  93.  
  94.  
  95. void
  96. save_Undo()
  97. /*
  98.  * Save current line into Undo buffer
  99.  */
  100. {
  101.     register struct li_line   *line;
  102.     register struct sc_screen *sc;
  103.  
  104.     sc = &screen;
  105.     line = sc->sc_curline;
  106.     file.fi_modified = TRUE;
  107.     if (sc->sc_origline.li_text == NULL) {
  108.         /*
  109.          * Copy line into Undo buffer
  110.          */
  111.         sc->sc_origline.li_text = xalloc(line->li_width+1);
  112.         sc->sc_origline.li_segments = line->li_segments;
  113.         sc->sc_origline.li_width = line->li_width;
  114.         strcpy(sc->sc_origline.li_text, line->li_text);
  115.     }
  116. }
  117.  
  118.  
  119. void
  120. toss_undo()
  121. /*
  122.  * Forget last undo operation
  123.  */
  124. {
  125.     struct ya_yank *yank;
  126.  
  127.     yank = &yank_array[0];
  128.     undo.un_inserted = FALSE;
  129.     undo.un_deleted = FALSE; 
  130.     undo.un_validcol = FALSE;
  131.     if (yank->ya_text) {
  132.         free(yank->ya_text);
  133.         yank->ya_text = NULL;
  134.     }
  135.     yank->ya_type = YANK_EMPTY;
  136.     ed_undo = FALSE;
  137. }
  138.