--- src/options.c.orig	Mon Jan  1 15:31:39 2001
+++ src/options.c	Mon Jan  1 21:08:43 2001
@@ -41,6 +41,7 @@
 #endif
 	{"autopickup", &flags.pickup, TRUE},
 	{"autoquiver", &flags.autoquiver, FALSE},
+	{"bagmerge", &flags.bagmerge, FALSE},
 #if defined(MICRO) && !defined(AMIGA)
 	{"BIOS", &iflags.BIOS, FALSE},
 #else
--- src/pickup.c.orig	Sat Jul 22 14:41:29 2000
+++ src/pickup.c	Mon Jan  1 21:59:06 2001
@@ -1475,9 +1475,10 @@
 /* Returns: -1 to stop, 1 item was inserted, 0 item was not inserted. */
 STATIC_PTR int
 in_container(obj)
-register struct obj *obj;
+struct obj *obj;
 {
 	register struct obj *gold;
+	struct obj *omerge = 0;
 	boolean is_gold = (obj->oclass == GOLD_CLASS);
 	boolean floor_container = !carried(current_container);
 	char buf[BUFSZ];
@@ -1544,6 +1545,14 @@
 
 	freeinv(obj);
 
+	/*
+	 * Check for a potential merge candidate.  We can't actually do the
+	 * merge yet, because we need the object to exist after we (pretend
+	 * to) put it in the container.
+	 */
+	if (flags.bagmerge && !is_gold && !Icebox)
+	    omerge = merge_choice(current_container->cobj, obj);
+
 	if (is_gold) {	/* look for other money to merge within the container */
 		for (gold = current_container->cobj; gold; gold = gold->nobj)
 			if (gold->otyp == obj->otyp) break;
@@ -1552,7 +1561,7 @@
 
 	if (gold) {
 		gold->quan += obj->quan;
-	} else {
+	} else if (!omerge) {
 		add_to_container(current_container, obj);
 	}
 
@@ -1590,6 +1599,11 @@
 			(void)stolen_value(current_container, u.ux, u.uy,
 					   (boolean)shkp->mpeaceful, FALSE);
 		}
+		/*
+		 * Nothing that could cause a bag of holding to explode
+		 * stacks, so we can assume no merge candidate was found
+		 * and the offending object is indeed in the bag.
+		 */
 		delete_contents(current_container);
 		if (!floor_container)
 			useup(current_container);
@@ -1600,6 +1614,12 @@
 
 		losehp(d(6,6),"magical explosion", KILLED_BY_AN);
 		current_container = 0;	/* baggone = TRUE; */
+	}
+
+	if (omerge) {
+		/* Now that that's over with, do the actual merge. */
+		if (!merged(&omerge, &obj))
+		    add_to_container(current_container, obj);
 	}
 
 	if (is_gold) {
--- include/flag.h.orig	Mon Jan  1 15:33:24 2001
+++ include/flag.h	Mon Jan  1 21:07:16 2001
@@ -23,6 +23,7 @@
 	boolean  asksavedisk;
 #endif
 	boolean  autoquiver;	/* Automatically fill quiver */
+	boolean  bagmerge;      /* Merge objects in container */
 	boolean  beginner;
 #ifdef MAIL
 	boolean  biff;		/* enable checking for mail */
--- dat/opthelp.orig	Mon Jan  1 15:36:54 2001
+++ dat/opthelp	Mon Jan  1 21:10:57 2001
@@ -5,6 +5,7 @@
 autopickup     automatically pick up objects you move over        [TRUE]
 autoquiver     when firing with an empty quiver, select some
                suitable inventory weapon to fill the quiver       [FALSE]
+bagmerge       merge identical objects in containers              [FALSE]
 BIOS           allow the use of IBM ROM BIOS calls                [FALSE]
 confirm        ask before hitting tame or peaceful monsters       [TRUE]
 DECgraphics    use DEC/VT line-drawing characters for the dungeon [FALSE]
--- doc/Guidebook.mn.orig	Mon Jan  1 16:53:05 2001
+++ doc/Guidebook.mn	Mon Jan  1 21:15:24 2001
@@ -1585,6 +1585,10 @@
 quality of the weapon; you are free to manually fill your quiver with
 the `Q' command instead.  If no weapon is found or the option is
 false, the `t' (throw) command is executed instead.  (default false)
+.lp bagmerge
+Objects put in bags or other containers will merge into a single stack with
+identical objects already in the container, as they do in your inventory.
+Gold will always merge in a container, whether this option is set or not.
 .lp "BIOS    "
 Use BIOS calls to update the screen
 display quickly and to read the keyboard (allowing the use of arrow
--- doc/Guidebook.tex.orig	Mon Jan  1 16:53:35 2001
+++ doc/Guidebook.tex	Mon Jan  1 21:15:24 2001
@@ -1996,6 +1996,11 @@
 the `Q' command instead.  If no weapon is found or the option is
 false, the `t' (throw) command is executed instead.  (default false)
 %.lp
+\item[\ib{bagmerge}]
+Objects put in bags or other containers will merge into a single stack with
+identical objects already in the container, as they do in your inventory.
+Gold will always merge in a container, whether this option is set or not.
+%.lp
 \item[\ib{BIOS}]
 Use BIOS calls to update the screen display quickly and to read the keyboard
 (allowing the use of arrow keys to move) on machines with an IBM PC
