home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / x / xvisrc.zoo / buffers.c < prev    next >
C/C++ Source or Header  |  1992-07-28  |  3KB  |  150 lines

  1. /* Copyright (c) 1990,1991,1992 Chris and John Downey */
  2. #ifndef lint
  3. static char *sccsid = "@(#)buffers.c    2.1 (Chris & John Downey) 7/29/92";
  4. #endif
  5.  
  6. /***
  7.  
  8. * program name:
  9.     xvi
  10. * function:
  11.     PD version of UNIX "vi" editor, with extensions.
  12. * module name:
  13.     buffers.c
  14. * module function:
  15.     Handle buffer allocation, deallocation, etc.
  16. * history:
  17.     STEVIE - ST Editor for VI Enthusiasts, Version 3.10
  18.     Originally by Tim Thompson (twitch!tjt)
  19.     Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
  20.     Heavily modified by Chris & John Downey
  21.  
  22. ***/
  23.  
  24. #include "xvi.h"
  25.  
  26. static    bool_t    setup_buffer P((Buffer *));
  27.  
  28. /*
  29.  * Create a new buffer.
  30.  */
  31. Buffer *
  32. new_buffer()
  33. {
  34.     Buffer    *new;
  35.  
  36.     new = (Buffer *) alloc(sizeof(Buffer));
  37.     if (new == NULL) {
  38.     return(NULL);
  39.     }
  40.  
  41.     /*
  42.      * Allocate memory for lines etc.
  43.      */
  44.     if (setup_buffer(new) == FALSE) {
  45.     free((char *) new);
  46.     return(NULL);
  47.     }
  48.  
  49.     /*
  50.      * Since setup_buffer() does not set up
  51.      * the filenames, we must do it ourselves.
  52.      */
  53.     new->b_filename = NULL;
  54.     new->b_tempfname = NULL;
  55.  
  56.     new->b_nwindows = 0;
  57.  
  58.     return(new);
  59. }
  60.  
  61. /*
  62.  * Delete the given buffer.
  63.  */
  64. void
  65. free_buffer(buffer)
  66. Buffer    *buffer;
  67. {
  68.     if (buffer == NULL)
  69.     return;
  70.  
  71.     /*
  72.      * Free all the lines in the buffer.
  73.      */
  74.     throw(buffer->b_line0);
  75.  
  76.     free((char *) buffer);
  77. }
  78.  
  79. /*
  80.  * Free up all the memory used indirectly by the buffer,
  81.  * and then get some new stuff. This only has an effect
  82.  * on the allocated fields within the buffer, i.e. it
  83.  * does not change any variables such as filenames.
  84.  *
  85.  * Returns TRUE for success, FALSE for failure to get memory.
  86.  */
  87. bool_t
  88. clear_buffer(buffer)
  89. Buffer    *buffer;
  90. {
  91.     /*
  92.      * Free all the lines in the buffer.
  93.      */
  94.     throw(buffer->b_line0);
  95.     return(setup_buffer(buffer));
  96. }
  97.  
  98. /*
  99.  * Allocate and initialise a buffer structure.
  100.  *
  101.  * Don't touch filenames.
  102.  *
  103.  * Returns TRUE for success, FALSE if we couldn't get memory.
  104.  */
  105. static bool_t
  106. setup_buffer(b)
  107. Buffer    *b;
  108. {
  109.     /*
  110.      * Allocate the single "dummy" line, and the two
  111.      * out-of-bounds lines for lines 0 and (n+1).
  112.      * This is a little strange ...
  113.      */
  114.     b->b_line0 = newline(0);
  115.     b->b_file = newline(1);
  116.     b->b_lastline = newline(0);
  117.     if (b->b_line0 == NULL || b->b_file == NULL || b->b_lastline == NULL) {
  118.     return(FALSE);
  119.     }
  120.  
  121.     /*
  122.      * Connect everything togther to form a minimal list.
  123.      */
  124.     b->b_line0->l_next = b->b_file;
  125.     b->b_file->l_prev = b->b_line0;
  126.     b->b_file->l_next = b->b_lastline;
  127.     b->b_lastline->l_prev = b->b_file;
  128.  
  129.     /*
  130.      * Clear all marks.
  131.      */
  132.     init_marks(b);
  133.  
  134.     /*
  135.      * Clear the undo status of the buffer.
  136.      */
  137.     init_undo(b);
  138.  
  139.     /*
  140.      * Clear all flags, and then pay special attention to the
  141.      * "noedit" flag which must be set if the parameter is FALSE.
  142.      */
  143.     b->b_flags = 0;
  144.     if (!Pb(P_edit)) {
  145.     b->b_flags |= FL_NOEDIT;
  146.     }
  147.  
  148.     return(TRUE);
  149. }
  150.