[BACK]Return to undo.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Diff for /src/usr.bin/mg/undo.c between version 1.6 and 1.7

version 1.6, 2002/02/21 17:36:12 version 1.7, 2002/02/26 00:45:45
Line 29 
Line 29 
   
 #include <sys/queue.h>  #include <sys/queue.h>
   
 #define MAX_LIST_RECORDS        32  
 #define MAX_FREE_RECORDS        32  #define MAX_FREE_RECORDS        32
   
 /*  /*
  * Local variables   * Local variables
  */   */
 static struct undo_list undo_list;  static LIST_HEAD(, undo_rec)     undo_free;
 static int              undo_list_num;  static int                       undo_free_num;
 static struct undo_list undo_free;  
 static int              undo_free_num;  
   
 /*  /*
  * Global variables   * Global variables
Line 59 
Line 56 
 static int find_offset(LINE *, int);  static int find_offset(LINE *, int);
 static int find_linep(int, LINE **, int *);  static int find_linep(int, LINE **, int *);
 static struct undo_rec *new_undo_record(void);  static struct undo_rec *new_undo_record(void);
 static void free_undo_record(struct undo_rec *);  
 static int drop_oldest_undo_record(void);  static int drop_oldest_undo_record(void);
   
 static int  static int
Line 108 
Line 104 
 {  {
         struct undo_rec *rec;          struct undo_rec *rec;
   
         while (undo_list_num >= MAX_LIST_RECORDS) {  
                 drop_oldest_undo_record();  
                 undo_list_num--;  
         }  
         undo_list_num++;  
         rec = LIST_FIRST(&undo_free);          rec = LIST_FIRST(&undo_free);
         if (rec != NULL)          if (rec != NULL)
                 LIST_REMOVE(rec, next); /* Remove it from the free-list */                  LIST_REMOVE(rec, next); /* Remove it from the free-list */
Line 125 
Line 116 
         return rec;          return rec;
 }  }
   
 static void  void
 free_undo_record(struct undo_rec *rec)  free_undo_record(struct undo_rec *rec)
 {  {
         if (rec->content != NULL) {          if (rec->content != NULL) {
Line 165 
Line 156 
 undo_init(void)  undo_init(void)
 {  {
         LIST_INIT(&undo_free);          LIST_INIT(&undo_free);
         LIST_INIT(&undo_list);  
   
         return TRUE;          return TRUE;
 }  }
Line 195 
Line 185 
                 return TRUE;                  return TRUE;
         rec = new_undo_record();          rec = new_undo_record();
         rec->pos = find_offset(lp, offset);          rec->pos = find_offset(lp, offset);
         rec->buf = curbp;  
         rec->type = type;          rec->type = type;
         rec->content = content;          rec->content = content;
         rec->region.r_linep = lp;          rec->region.r_linep = lp;
         rec->region.r_offset = offset;          rec->region.r_offset = offset;
         rec->region.r_size = size;          rec->region.r_size = size;
   
         LIST_INSERT_HEAD(&undo_list, rec, next);          LIST_INSERT_HEAD(&curbp->b_undo, rec, next);
   
         return TRUE;          return TRUE;
 }  }
Line 216 
Line 205 
                 return TRUE;                  return TRUE;
   
         rec = new_undo_record();          rec = new_undo_record();
         rec->buf = curbp;  
         rec->type = BOUNDARY;          rec->type = BOUNDARY;
   
         LIST_INSERT_HEAD(&undo_list, rec, next);          LIST_INSERT_HEAD(&curbp->b_undo, rec, next);
   
         return TRUE;          return TRUE;
 }  }
Line 240 
Line 228 
         /*          /*
          * We try to reuse the last undo record to `compress' things.           * We try to reuse the last undo record to `compress' things.
          */           */
         rec = LIST_FIRST(&undo_list);          rec = LIST_FIRST(&curbp->b_undo);
         if ((rec != NULL) &&          if ((rec != NULL) &&
             (rec->type == INSERT) &&              (rec->type == INSERT) &&
             (rec->buf == curbp) &&  
             (rec->region.r_linep == lp)) {              (rec->region.r_linep == lp)) {
                 int dist;                  int dist;
   
Line 260 
Line 247 
          */           */
         rec = new_undo_record();          rec = new_undo_record();
         rec->pos = find_offset(lp, offset);          rec->pos = find_offset(lp, offset);
         rec->buf = curbp;  
         rec->type = INSERT;          rec->type = INSERT;
         memmove(&rec->region, &reg, sizeof(REGION));          memmove(&rec->region, &reg, sizeof(REGION));
         rec->content = NULL;          rec->content = NULL;
         LIST_INSERT_HEAD(&undo_list, rec, next);          LIST_INSERT_HEAD(&curbp->b_undo, rec, next);
   
         return TRUE;          return TRUE;
 }  }
Line 295 
Line 281 
         /*          /*
          * Again, try to reuse last undo record, if we can           * Again, try to reuse last undo record, if we can
          */           */
         rec = LIST_FIRST(&undo_list);          rec = LIST_FIRST(&curbp->b_undo);
         if (!skip &&          if (!skip &&
             (rec != NULL) &&              (rec != NULL) &&
             (rec->type == DELETE) &&              (rec->type == DELETE) &&
             (rec->buf == curbp) &&  
             (rec->region.r_linep == reg.r_linep)) {              (rec->region.r_linep == reg.r_linep)) {
                 char *newbuf;                  char *newbuf;
                 int newlen;                  int newlen;
Line 340 
Line 325 
         rec = new_undo_record();          rec = new_undo_record();
         rec->pos = pos;          rec->pos = pos;
   
         rec->buf = curbp;  
         rec->type = DELETE;          rec->type = DELETE;
         memmove(&rec->region, &reg, sizeof(REGION));          memmove(&rec->region, &reg, sizeof(REGION));
         do {          do {
Line 352 
Line 336 
   
         region_get_data(&reg, rec->content, reg.r_size);          region_get_data(&reg, rec->content, reg.r_size);
   
         LIST_INSERT_HEAD(&undo_list, rec, next);          LIST_INSERT_HEAD(&curbp->b_undo, rec, next);
   
         return TRUE;          return TRUE;
 }  }
Line 376 
Line 360 
   
         rec = new_undo_record();          rec = new_undo_record();
         rec->pos = find_offset(lp, offset);          rec->pos = find_offset(lp, offset);
         rec->buf = curbp;  
         rec->type = CHANGE;          rec->type = CHANGE;
         memmove(&rec->region, &reg, sizeof reg);          memmove(&rec->region, &reg, sizeof reg);
   
Line 389 
Line 372 
   
         region_get_data(&reg, rec->content, size);          region_get_data(&reg, rec->content, size);
   
         LIST_INSERT_HEAD(&undo_list, rec, next);          LIST_INSERT_HEAD(&curbp->b_undo, rec, next);
   
         return TRUE;          return TRUE;
 }  }
Line 409 
Line 392 
         undoaction++;          undoaction++;
   
         while (n > 0) {          while (n > 0) {
                 rec = LIST_FIRST(&undo_list);                  rec = LIST_FIRST(&curbp->b_undo);
                 if (rec == NULL) {                  if (rec == NULL) {
                         ewprintf("Nothing to undo!");                          ewprintf("Nothing to undo!");
                         return FALSE;                          return FALSE;
                 }                  }
                 if (rec->buf != curbp)  
                         popbuf(rec->buf);  
   
                 LIST_REMOVE(rec, next);                  LIST_REMOVE(rec, next);
                 if (rec->type == BOUNDARY) {                  if (rec->type == BOUNDARY) {
                         continue;                          continue;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7