/[qemacs]/qemacs/buffer.c
ViewVC logotype

Diff of /qemacs/buffer.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by chqrlie, Mon May 9 03:19:36 2005 UTC revision 1.8 by chqrlie, Wed May 11 14:35:54 2005 UTC
# Line 59  static Page *find_page(EditBuffer *b, in Line 59  static Page *find_page(EditBuffer *b, in
59  /* prepare a page to be written */  /* prepare a page to be written */
60  static void update_page(Page *p)  static void update_page(Page *p)
61  {  {
62      u8 * buf;      u8 *buf;
63    
64      /* if the page is read only, copy it */      /* if the page is read only, copy it */
65      if (p->flags & PG_READ_ONLY) {      if (p->flags & PG_READ_ONLY) {
66          buf = malloc(p->size);          buf = malloc(p->size);
67          /* XXX: should return an error */          /* XXX: should return an error */
68          if (!buf)          if (!buf)
69              return;              return;
70          memcpy(buf, p->data,p->size);          memcpy(buf, p->data, p->size);
71          p->data = buf;          p->data = buf;
72          p->flags &= ~PG_READ_ONLY;          p->flags &= ~PG_READ_ONLY;
73      }      }
# Line 87  static int eb_rw(EditBuffer *b, int offs Line 88  static int eb_rw(EditBuffer *b, int offs
88    
89      size = size1;      size = size1;
90      if (do_write)      if (do_write)
91          eb_addlog(b, LOGOP_WRITE, offset, size);                  eb_addlog(b, LOGOP_WRITE, offset, size);        
92            
93      p = find_page(b, &offset);      p = find_page(b, &offset);
94      while (size > 0) {      while (size > 0) {
# Line 146  static void eb_insert1(EditBuffer *b, in Line 147  static void eb_insert1(EditBuffer *b, in
147          if (len > 0) {          if (len > 0) {
148              update_page(p);              update_page(p);
149              p->data = realloc(p->data, p->size + len);              p->data = realloc(p->data, p->size + len);
150              memmove(p->data + len,              memmove(p->data + len, p->data, p->size);
                     p->data, p->size);  
151              memcpy(p->data, buf + size - len, len);              memcpy(p->data, buf + size - len, len);
152              size -= len;              size -= len;
153              p->size += len;              p->size += len;
# Line 160  static void eb_insert1(EditBuffer *b, in Line 160  static void eb_insert1(EditBuffer *b, in
160          b->nb_pages += n;          b->nb_pages += n;
161          b->page_table = realloc(b->page_table, b->nb_pages * sizeof(Page));          b->page_table = realloc(b->page_table, b->nb_pages * sizeof(Page));
162          p = b->page_table + page_index;          p = b->page_table + page_index;
163          memmove(p + n, p,          memmove(p + n, p,
164                  sizeof(Page) * (b->nb_pages - n - page_index));                  sizeof(Page) * (b->nb_pages - n - page_index));
165          while (size > 0) {          while (size > 0) {
166              len = size;              len = size;
# Line 179  static void eb_insert1(EditBuffer *b, in Line 179  static void eb_insert1(EditBuffer *b, in
179    
180  /* We must have : 0 <= offset <= b->total_size */  /* We must have : 0 <= offset <= b->total_size */
181  static void eb_insert_lowlevel(EditBuffer *b, int offset,  static void eb_insert_lowlevel(EditBuffer *b, int offset,
182                                 const u8 *buf, int size)                                 const u8 *buf, int size)
183  {  {
184      int len, len_out, page_index;      int len, len_out, page_index;
185      Page *p;      Page *p;
# Line 293  void eb_insert_buffer(EditBuffer *dest, Line 293  void eb_insert_buffer(EditBuffer *dest,
293      if (n > 0) {      if (n > 0) {
294          /* add the pages */          /* add the pages */
295          dest->nb_pages += n;          dest->nb_pages += n;
296          dest->page_table = realloc(dest->page_table, dest->nb_pages * sizeof(Page));          dest->page_table = realloc(dest->page_table,
297                                       dest->nb_pages * sizeof(Page));
298          q = dest->page_table + page_index;          q = dest->page_table + page_index;
299          memmove(q + n, q,          memmove(q + n, q,
300                  sizeof(Page) * (dest->nb_pages - n - page_index));                  sizeof(Page) * (dest->nb_pages - n - page_index));
# Line 456  EditBuffer *eb_new(const char *name, int Line 457  EditBuffer *eb_new(const char *name, int
457      eb_add_callback(b, eb_offset_callback, &b->mark);      eb_add_callback(b, eb_offset_callback, &b->mark);
458    
459      if (!strcmp(name, "*trace*"))      if (!strcmp(name, "*trace*"))
460          trace_buffer = b;          trace_buffer = b;
461    
462      return b;      return b;
463  }  }
# Line 472  void eb_free(EditBuffer *b) Line 473  void eb_free(EditBuffer *b)
473          b->close(b);          b->close(b);
474    
475      /* free each callback */      /* free each callback */
476      for(l = b->first_callback; l != NULL;) {      for (l = b->first_callback; l != NULL;) {
477          l1 = l->next;          l1 = l->next;
478          free(l);          free(l);
479          l = l1;          l = l1;
# Line 552  void eb_free_callback(EditBuffer *b, Edi Line 553  void eb_free_callback(EditBuffer *b, Edi
553  {  {
554      EditBufferCallbackList **pl, *l;      EditBufferCallbackList **pl, *l;
555            
556      for(pl = &b->first_callback; (*pl) != NULL; pl = &(*pl)->next) {      for (pl = &b->first_callback; (*pl) != NULL; pl = &(*pl)->next) {
557          l = *pl;          l = *pl;
558          if (l->callback == cb && l->opaque == opaque) {          if (l->callback == cb && l->opaque == opaque) {
559              *pl = l->next;              *pl = l->next;
# Line 571  void eb_offset_callback(EditBuffer *b, Line 572  void eb_offset_callback(EditBuffer *b,
572  {  {
573      int *offset_ptr = opaque;      int *offset_ptr = opaque;
574    
575      switch(op) {      switch (op) {
576      case LOGOP_INSERT:      case LOGOP_INSERT:
577          if (*offset_ptr > offset)          if (*offset_ptr > offset)
578              *offset_ptr += size;              *offset_ptr += size;
# Line 601  static void eb_addlog(EditBuffer *b, enu Line 602  static void eb_addlog(EditBuffer *b, enu
602      EditBufferCallbackList *l;      EditBufferCallbackList *l;
603    
604      /* call each callback */      /* call each callback */
605      for(l = b->first_callback; l != NULL; l = l->next) {      for (l = b->first_callback; l != NULL; l = l->next) {
606          l->callback(b, l->opaque, op, offset, size);          l->callback(b, l->opaque, op, offset, size);
607      }      }
608    
# Line 641  static void eb_addlog(EditBuffer *b, enu Line 642  static void eb_addlog(EditBuffer *b, enu
642      b->log_new_index += sizeof(LogBuffer);      b->log_new_index += sizeof(LogBuffer);
643    
644      /* data */      /* data */
645      switch(op) {      switch (op) {
646      case LOGOP_DELETE:      case LOGOP_DELETE:
647      case LOGOP_WRITE:      case LOGOP_WRITE:
648          eb_insert_buffer(b->log_buffer, b->log_new_index, b, offset, size);          eb_insert_buffer(b->log_buffer, b->log_new_index, b, offset, size);
# Line 695  void do_undo(EditState *s) Line 696  void do_undo(EditState *s)
696      eb_read(b->log_buffer, log_index, (unsigned char *)&lb, sizeof(LogBuffer));      eb_read(b->log_buffer, log_index, (unsigned char *)&lb, sizeof(LogBuffer));
697      log_index += sizeof(LogBuffer);      log_index += sizeof(LogBuffer);
698    
699      switch(lb.op) {      switch (lb.op) {
700      case LOGOP_WRITE:      case LOGOP_WRITE:
701          /* we must disable the log because we want to record a single          /* we must disable the log because we want to record a single
702             write (we should have the single operation: eb_write_buffer) */             write (we should have the single operation: eb_write_buffer) */
# Line 821  static void get_pos(u8 *buf, int size, i Line 822  static void get_pos(u8 *buf, int size, i
822      p = buf;      p = buf;
823      lp = p;      lp = p;
824      p1 = p + size;      p1 = p + size;
825      for(;;) {      for (;;) {
826          p = memchr(p, '\n', p1 - p);          p = memchr(p, '\n', p1 - p);
827          if (!p)          if (!p)
828              break;              break;
# Line 906  int eb_get_pos(EditBuffer *b, int *line_ Line 907  int eb_get_pos(EditBuffer *b, int *line_
907      col = 0;      col = 0;
908      p = b->page_table;      p = b->page_table;
909      p_end = p + b->nb_pages;      p_end = p + b->nb_pages;
910      for(;;) {      for (;;) {
911          if (p >= p_end)          if (p >= p_end)
912              goto the_end;              goto the_end;
913          if (offset < p->size)          if (offset < p->size)
# Line 923  int eb_get_pos(EditBuffer *b, int *line_ Line 924  int eb_get_pos(EditBuffer *b, int *line_
924          offset -= p->size;          offset -= p->size;
925          p++;          p++;
926      }      }
927      get_pos(p->data, offset, &line1, &col1,      get_pos(p->data, offset, &line1, &col1, &b->charset_state);
             &b->charset_state);  
928      line += line1;      line += line1;
929      if (line1)      if (line1)
930          col = 0;          col = 0;
# Line 967  static int goto_char(u8 *buf, int pos, Q Line 967  static int goto_char(u8 *buf, int pos, Q
967    
968      nb_chars = 0;      nb_chars = 0;
969      buf_ptr = buf;      buf_ptr = buf;
970      for(;;) {      for (;;) {
971          c = *buf_ptr;          c = *buf_ptr;
972          if (c < 0x80 || c >= 0xc0) {          if (c < 0x80 || c >= 0xc0) {
973              if (nb_chars >= pos)              if (nb_chars >= pos)
# Line 1028  int eb_get_char_offset(EditBuffer *b, in Line 1028  int eb_get_char_offset(EditBuffer *b, in
1028          p = b->page_table;          p = b->page_table;
1029          p_end = p + b->nb_pages;          p_end = p + b->nb_pages;
1030          pos = 0;          pos = 0;
1031          for(;;) {          for (;;) {
1032              if (p >= p_end)              if (p >= p_end)
1033                  goto the_end;                  goto the_end;
1034              if (offset < p->size)              if (offset < p->size)
# Line 1165  int raw_load_buffer1(EditBuffer *b, FILE Line 1165  int raw_load_buffer1(EditBuffer *b, FILE
1165      int len;      int len;
1166      unsigned char buf[IOBUF_SIZE];      unsigned char buf[IOBUF_SIZE];
1167    
1168      for(;;) {      for (;;) {
1169          len = fread(buf, 1, IOBUF_SIZE, f);          len = fread(buf, 1, IOBUF_SIZE, f);
1170          if (len < 0)          if (len < 0)
1171              return -1;              return -1;
# Line 1304  void eb_line_pad(EditBuffer *b, int n) Line 1304  void eb_line_pad(EditBuffer *b, int n)
1304      int offset, i;      int offset, i;
1305      i = 0;      i = 0;
1306      offset = b->total_size;      offset = b->total_size;
1307      for(;;) {      for (;;) {
1308          if (eb_prevc(b, offset, &offset) == '\n')          if (eb_prevc(b, offset, &offset) == '\n')
1309              break;              break;
1310          i++;          i++;
# Line 1340  int eb_get_line(EditBuffer *b, unsigned Line 1340  int eb_get_line(EditBuffer *b, unsigned
1340      /* record line */      /* record line */
1341      buf_ptr = buf;      buf_ptr = buf;
1342      buf_end = buf + buf_size;      buf_end = buf + buf_size;
1343      for(;;) {      for (;;) {
1344          c = eb_nextc(b, offset, &offset);          c = eb_nextc(b, offset, &offset);
1345          if (c == '\n')          if (c == '\n')
1346              break;              break;
# Line 1365  int eb_get_strline(EditBuffer *b, char * Line 1365  int eb_get_strline(EditBuffer *b, char *
1365      /* record line */      /* record line */
1366      buf_ptr = buf;      buf_ptr = buf;
1367      buf_end = buf + buf_size - 1;      buf_end = buf + buf_size - 1;
1368      for(;;) {      for (;;) {
1369          c = eb_nextc(b, offset, &offset);          c = eb_nextc(b, offset, &offset);
1370          if (c == '\n')          if (c == '\n')
1371              break;              break;
# Line 1381  int eb_goto_bol(EditBuffer *b, int offse Line 1381  int eb_goto_bol(EditBuffer *b, int offse
1381  {  {
1382      int c, offset1;      int c, offset1;
1383    
1384      for(;;) {      for (;;) {
1385          c = eb_prevc(b, offset, &offset1);          c = eb_prevc(b, offset, &offset1);
1386          if (c == '\n')          if (c == '\n')
1387              break;              break;
# Line 1394  int eb_is_empty_line(EditBuffer *b, int Line 1394  int eb_is_empty_line(EditBuffer *b, int
1394  {  {
1395      int c;      int c;
1396    
1397      for(;;) {      for (;;) {
1398          c = eb_nextc(b, offset, &offset);          c = eb_nextc(b, offset, &offset);
1399          if (c == '\n')          if (c == '\n')
1400              return 1;              return 1;
# Line 1407  int eb_is_empty_line(EditBuffer *b, int Line 1407  int eb_is_empty_line(EditBuffer *b, int
1407  int eb_next_line(EditBuffer *b, int offset)  int eb_next_line(EditBuffer *b, int offset)
1408  {  {
1409      int c;      int c;
1410      for(;;) {      for (;;) {
1411          c = eb_nextc(b, offset, &offset);          c = eb_nextc(b, offset, &offset);
1412          if (c == '\n')          if (c == '\n')
1413              break;              break;

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

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26