Index: uspace/app/edit/sheet.c
===================================================================
--- uspace/app/edit/sheet.c	(revision 99e5526942b631362a42f7ed64600868918dd9b0)
+++ uspace/app/edit/sheet.c	(revision c29f20ba90f19e7fd960e63503c18c7fb0f714b0)
@@ -54,9 +54,13 @@
 #include <adt/list.h>
 #include <align.h>
+#include <macros.h>
 
 #include "sheet.h"
 
 enum {
-	TAB_WIDTH = 8
+	TAB_WIDTH	= 8,
+
+	/** Initial  of dat buffer in bytes */
+	INITIAL_SIZE	= 32
 };
 
@@ -64,5 +68,5 @@
 int sheet_init(sheet_t *sh)
 {
-	sh->dbuf_size = 65536;
+	sh->dbuf_size = INITIAL_SIZE;
 	sh->text_size = 0;
 
@@ -95,16 +99,25 @@
 	link_t *link;
 	tag_t *tag;
+	char *newp;
 
 	sz = str_size(str);
-	if (sh->text_size + sz > sh->dbuf_size)
-		return ELIMIT;
+	if (sh->text_size + sz > sh->dbuf_size) {
+		/* Enlarge data buffer. */
+		newp = realloc(sh->data, sh->dbuf_size * 2);
+		if (newp == NULL)
+			return ELIMIT;
+
+		sh->data = newp;
+		sh->dbuf_size = sh->dbuf_size * 2;
+	}
 
 	ipp = sh->data + pos->b_off;
 
+	/* Copy data. */
 	memmove(ipp + sz, ipp, sh->text_size - pos->b_off);
 	memcpy(ipp, str, sz);
 	sh->text_size += sz;
 
-	/* Adjust tags */
+	/* Adjust tags. */
 
 	link = sh->tags_head.next;
@@ -139,4 +152,6 @@
 	link_t *link;
 	tag_t *tag;
+	char *newp;
+	size_t shrink_size;
 
 	spp = sh->data + spos->b_off;
@@ -146,5 +161,5 @@
 	sh->text_size -= sz;
 
-	/* Adjust tags */
+	/* Adjust tags. */
 	link = sh->tags_head.next;
 	while (link != &sh->tags_head) {
@@ -158,5 +173,19 @@
 		link = link->next;
 	}
-	
+
+	/* See if we should free up some memory. */
+	shrink_size = max(sh->dbuf_size / 4, INITIAL_SIZE);
+	if (sh->text_size <= shrink_size && sh->dbuf_size > INITIAL_SIZE) {
+		/* Shrink data buffer. */
+		newp = realloc(sh->data, shrink_size);
+		if (newp == NULL) {
+			/* Failed to shrink buffer... no matter. */
+			return EOK;
+		}
+
+		sh->data = newp;
+		sh->dbuf_size = shrink_size;
+	}
+
 	return EOK;
 }
