home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / rvi / part2 / rv_delete.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.6 KB  |  144 lines

  1. #include "rv.h"
  2.  
  3. void delete()
  4. /*
  5.  *  Delete - delete text
  6.  */
  7. {
  8.     register struct li_line      *line;
  9.     register struct sc_screen *sc;
  10.     register struct wi_window *wi;
  11.     void    delete_columns();
  12.     INT seg;
  13.  
  14.     sc = &screen;
  15.     wi = &window;
  16.  
  17.     file.fi_modified = TRUE;
  18.  
  19.     if (sc->sc_firstline != sc->sc_lastline || !sc->sc_validcol)
  20.         xmit_curline();
  21.     /*
  22.      * Save text in unnamed buffer for 'p' command.
  23.      */
  24.     if (yank_cmd != ' ') { /* If user also specified buf */
  25.         yank();    /* Yank into it too */
  26.         yank_cmd = ' ';
  27.     }
  28.     yank(); /* Yank to the unnamed buf */
  29.     undo.un_deleted = TRUE;
  30.     undo.un_validcol = FALSE;
  31.     undo.un_firstline = sc->sc_firstline;
  32.  
  33.     /*
  34.      * Three cases:  lines, columns, or both
  35.      */
  36.     if (sc->sc_validcol) { /* If columns */
  37.         if (sc->sc_firstline != sc->sc_lastline) { /* If both */
  38.             botprint(TRUE,
  39.                 "Cant delete columns within multiple lines yet.\n");
  40.             return;
  41.         }
  42.         undo.un_validcol = TRUE;
  43.         undo.un_firstcol = sc->sc_firstcol;
  44.         delete_columns(sc->sc_firstcol, sc->sc_lastcol);
  45.     }
  46.     else { /* If lines */
  47.         /*
  48.          * Store deleted lines in numbered (1-9) yank buffers
  49.          */
  50.         ++yank_shift;  /* Shift numbered bufs */
  51.         yank_cmd = '1';
  52.         yank(); /* Yank to new buf 1 */
  53.  
  54.         if (sc->sc_firstline == sc->sc_lastline) {
  55.             /*
  56.              * Case 1 - delete single line
  57.              */
  58.             xmit_ed("%dd\n", sc->sc_firstline);
  59.             move_abs_cursor(sc->sc_lineno, COL_SAME);
  60.             seg = sc->sc_curline->li_segments;
  61.             if (sc->sc_curline->li_text) {
  62.                 free(sc->sc_curline->li_text);
  63.                 sc->sc_curline->li_text = NULL;
  64.             }
  65.             for (line=sc->sc_curline+1; line <= wi->wi_botline;
  66.                ++line) {
  67.                 /*
  68.                  * Compact window
  69.                  */
  70.                 (line-1)->li_text = line->li_text;
  71.                 (line-1)->li_width = line->li_width;
  72.                 (line-1)->li_segments = line->li_segments;
  73.             }
  74.             wi->wi_botline->li_text = NULL;
  75.             wi->wi_botline--;
  76.             sc->sc_botline--;
  77.             file.fi_numlines--;
  78.             if (sc->sc_curline > wi->wi_botline) {
  79.                 sc->sc_curline--;
  80.                 sc->sc_lineno--;
  81.             }
  82.             if (sc->sc_curline < sc->sc_topline) {
  83.                 sc->sc_abovetop = 0;
  84.                 sc->sc_topline = sc->sc_curline;
  85.             } else {
  86.                 rv_fdeleteln(seg);
  87.                 move_cursor(sc->sc_lineno, COL_SAME);
  88.                 redraw_screen(sc->sc_botline+1);
  89.                 return;
  90.             }
  91.             move_cursor(sc->sc_lineno, COL_SAME);
  92.             line = sc->sc_curline;
  93.         } else {
  94.             /*
  95.              * Delete multiple lines
  96.              */
  97.             xmit_ed("%d,%dd\n",sc->sc_firstline,sc->sc_lastline);
  98.             file.fi_numlines -= sc->sc_lastline-sc->sc_firstline+1;
  99.             if (file.fi_numlines > 0)
  100.                 fetch_window(sc->sc_lineno - NUM_WINDOW_LINES/4
  101.                     - LINES/2 + 1, FALSE);
  102.             else /* deleted all lines, wipe window clean */
  103.                 for (line = wi->wi_topline;
  104.                         line <= wi->wi_botline; ++line) {
  105.                     free(line->li_text);
  106.                     line->li_text = NULL;
  107.                 }
  108.             line = NULL;
  109.         }
  110.         if (file.fi_numlines <= 0) { 
  111.             /*
  112.              * Deleted whole file.  Set up initial window
  113.              */
  114.             wi->wi_topline = &line_array[NUM_WINDOW_LINES/4];
  115.             wi->wi_botline = wi->wi_topline;
  116.             wi->wi_topline->li_text = xalloc(1);
  117.             wi->wi_topline->li_text[0] = '\0';
  118.             wi->wi_topline->li_segments = 1;
  119.             wi->wi_topline->li_width = 0;
  120.             sc->sc_curline = wi->wi_topline;
  121.             sc->sc_lineno = 1;
  122.             sc->sc_column = 0;
  123.             sc->sc_abovetop = 0;
  124.             sc->sc_topline = sc->sc_botline = sc->sc_curline;
  125.             file.fi_numlines = 1;
  126.             line = NULL;
  127.             xmit_ed("0a\n\n.\n");
  128.             redraw_screen(line);
  129.             move_cursor(sc->sc_lineno, COL_SAME);
  130.             botprint(FALSE, "Empty file");
  131.             hitcr_continue();
  132.             return;
  133.         }
  134.  
  135.         redraw_screen(line);
  136.         move_cursor(sc->sc_lineno, COL_SAME);
  137.         if (sc->sc_lastline - sc->sc_firstline > 1) {
  138.             botprint(FALSE, "%d lines deleted",
  139.                 sc->sc_lastline - sc->sc_firstline + 1);
  140.             hitcr_continue();
  141.         }
  142.     }
  143. }
  144.