diff options
Diffstat (limited to 'buff.c')
-rw-r--r-- | buff.c | 71 |
1 files changed, 29 insertions, 42 deletions
@@ -95,8 +95,26 @@ void __append_lines(int addr, int n, line_t *first, line_t *last){ } -void __copy_lines(line_t **first, line_t **last){ - +int __copy_lines(line_t **first, line_t **last){ + + line_t *new, *l, *p; + int n = addr2; + + move_to_line(addr2,0); + l = malloc(sizeof(line_t)); + l-> c = malloc((strlen(cur->c) + 1) * sizeof(char)); + strcpy(l->c, cur->c); + p = *first = l; + while(++n <= addr1){ + cur = cur -> next, pos++; + l = malloc(sizeof(line_t)); + l->c = malloc((strlen(cur->c) + 1) * sizeof(char)); + strcpy(l->c, cur->c); + p->next = l; + p = l; + } + *last = p; + return n - addr2; } @@ -178,32 +196,6 @@ void print_lineno(){ printf("%d\n", addr1); } -int move_forward(int n){ - - if (pos + n > num){ - E; - return -1; - } - while(n-- > 0){ - cur = cur->next; - } - pos += n; - return 0; -} - -int move_backward(int n){ - - if (pos - n < 1){ - E; - return -1; - } - while(n-- >0){ - cur = cur->prev; - } - pos -= n; - return 0; -} - /* add lines after pos */ @@ -248,8 +240,7 @@ void delete_lines(){ if (addr2 > 1){ handle = cur -> prev; } - while(pos <= addr1){ - pos ++; + while(pos++ <= addr1){ next = cur -> next; prev = cur -> prev; free(cur -> c); @@ -259,7 +250,6 @@ void delete_lines(){ else break; } - if (!handle){ /* first line deleted */ b_start = cur; @@ -274,7 +264,6 @@ void delete_lines(){ pos = addr2 - 1; cur = prev; } - num -= addr1 - addr2 + 1; } @@ -372,26 +361,24 @@ int mark(char *c){ } -int transfer_lines(int addr, char move){ +void transfer_lines(int addr, char move){ line_t *first, *last; - + int n; + if (addr2 < addr && addr < addr1){ E; - return -1; + return; } first = last = NULL; - - __copy_lines(&first, &last); - + n = __copy_lines(&first, &last); + fprintf(stderr, " >>>> transfer_lines -- addr: %d n: %d\n", addr, n); if (move){ if (addr1 < addr) addr -= (addr1 - addr2 + 1); delete_lines(); } - /* make a generic function to append lines after an addr */ - - return 0; - + __append_lines(addr, n, first, last); + move_to_line(addr+n,0); } |