BY_LINE
};
+void range_add(Sel* p_range, size_t off);
+void range_del(Sel* p_range, size_t beg, size_t end);
+
void gapbuf_init(GapBuf* buf);
size_t gapbuf_end(GapBuf* buf);
long gapbuf_save(GapBuf* buf, char* path);
static void putb(Buf* buf, char b, Sel* p_sel)
{
- if (p_sel->end < buf->point.beg)
- {
- buf->point.beg++;
- }
- if (p_sel->end <= buf->point.end)
- {
- buf->point.end++;
- }
+ range_add(&(buf->point), p_sel->end);
gapbuf_putb(&buf->contents, b, p_sel);
}
size_t nbytes = sel.end - sel.beg;
if (nbytes > 0)
{
- /* adjust the point according to the characters deleted */
- size_t bpoint = 0;
- if (sel.beg <= buf->point.beg)
- {
- bpoint = min(nbytes, buf->point.beg - sel.beg);
- }
- size_t inpoint = 0;
- if (sel.end >= buf->point.beg)
- {
- inpoint = min(sel.end, buf->point.end) - max(sel.beg, buf->point.beg);
- }
- buf->point.beg -= bpoint;
- buf->point.end -= (bpoint + inpoint);
-
/* perform the delete */
buf->status = MODIFIED;
char* str = buf_gets(buf);
+ range_del(&(buf->point), sel.beg, sel.end);
gapbuf_del(&buf->contents, sel.beg, nbytes);
sel.end = sel.beg;
buf->selection = sel;
/* reinsert deleted bytes */
for (char* s = item->data; s && *s; s++, item->end++)
{
+ range_add(&(buf->point), buf->selection.end);
gapbuf_putb(&buf->contents, *s, &(buf->selection));
buf->status = MODIFIED;
}
/* delete the added bytes */
Sel sel = buf->selection;
item->data = buf_gets(buf);
+ range_del(&(buf->point), item->beg, item->end);
gapbuf_del(&buf->contents, sel.beg, (item->end - item->beg));
sel.end = sel.beg;
buf->selection = sel;
--- /dev/null
+#include <stdc.h>
+#include <utf.h>
+#include <edit.h>
+
+void range_add(Sel* p_range, size_t off)
+{
+ if (off < p_range->beg)
+ {
+ p_range->beg++;
+ }
+ if (off <= p_range->end)
+ {
+ p_range->end++;
+ }
+}
+
+void range_del(Sel* p_range, size_t beg, size_t end)
+{
+ /* adjust the point according to the characters deleted */
+ size_t nbytes = end - beg;
+ size_t bpoint = 0;
+ if (beg <= p_range->beg)
+ {
+ bpoint = min(nbytes, p_range->beg - beg);
+ }
+ size_t inpoint = 0;
+ if (end >= p_range->beg)
+ {
+ inpoint = min(end, p_range->end) - max(beg, p_range->beg);
+ }
+ p_range->beg -= bpoint;
+ p_range->end -= (bpoint + inpoint);
+}