diff --git a/.gitignore b/.gitignore
index 89c53d0233872b8500a408a04586fc901f7bce86..40da99e55e23ca2a43781e2b89e8a03235ff8e45 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,6 +16,7 @@ core
 /ABOUT-NLS
 /INSTALL
 /aclocal.m4
+/ar-lib
 /autom4te.cache
 /compile
 /config.cache
@@ -68,6 +69,13 @@ core
 /m4/wint_t.m4
 /m4/xsize.m4
 
+# gnulib entries
+/gnulib/
+/lib/
+/m4/.gitignore
+/m4/gnulib-cache.m4
+/snippet/
+
 /nano.spec
 /src/nano
 /src/revision.h
diff --git a/Makefile.am b/Makefile.am
index 48d587e550f99db06bd635496e248057e0ab8bab..1971a117881239adab62fe686ee8f5e18b153a4a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
 AUTOMAKE_OPTIONS = gnu no-dependencies
 
-SUBDIRS = doc m4 po src
+SUBDIRS = doc lib m4 po src
 
 if USE_COLOR
 SUBDIRS += syntax
diff --git a/autogen.sh b/autogen.sh
index 540072239b4dccf71d287a89dcb151590ee983d6..cfe70582afb253aaf1ffbe91bdc63a688945a3b7 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -1,4 +1,30 @@
 #!/bin/sh
 # Generate configure & friends for GIT users.
 
+gnulib_url="git://git.sv.gnu.org/gnulib.git"
+gnulib_hash="4084b3a1094372b960ce4a97634e08f4538c8bdd"
+
+modules="
+	strcase
+	strcasestr-simple
+	strnlen
+"
+
+# Make sure the local gnulib git repo is up-to-date.
+if [ ! -d "gnulib" ]; then
+	git clone --depth=123 ${gnulib_url}
+fi
+cd gnulib >/dev/null || exit 1
+curr_hash=$(git log -1 --format=%H)
+if [ "${gnulib_hash}" != "${curr_hash}" ]; then
+	git pull
+	git checkout -f ${gnulib_hash}
+fi
+cd .. >/dev/null || exit 1
+
+rm -rf lib
+./gnulib/gnulib-tool \
+	--import \
+	${modules}
+
 autoreconf -f -i -s
diff --git a/configure.ac b/configure.ac
index d9db2faf4e08cb4ed44e8373075c7be38db6c01e..9379450da9abd5314ed75c6b61f681c34b724fb6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,10 +26,16 @@ dnl Make sure the ONCE macros are available.
 
 AC_PREREQ([2.69])
 
+dnl Lie about gnulib features we don't use to speed up & shrink down.
+
+gl_cv_func_printf_infinite_long_double=yes
+
 dnl Checks for programs.
 
 AC_GNU_SOURCE
 AC_PROG_CC
+gl_EARLY
+gl_INIT
 AC_PROG_LN_S
 AC_ISC_POSIX
 AC_SYS_LARGEFILE
@@ -747,6 +753,7 @@ AC_CONFIG_FILES([
 Makefile
 doc/Makefile
 doc/sample.nanorc
+lib/Makefile
 m4/Makefile
 po/Makefile.in
 src/Makefile
diff --git a/m4/Makefile.am b/m4/Makefile.am
index 1d565640003bbaad290b496578a61f07c242fc0f..b9a37af72d4805309e9c9551e84c2dba9eeef3b9 100644
--- a/m4/Makefile.am
+++ b/m4/Makefile.am
@@ -8,6 +8,7 @@ EXTRA_DIST = \
 	glib-2.0.m4 \
 	glibc21.m4 \
 	glibc2.m4 \
+	gnulib-cache.m4 \
 	iconv.m4 \
 	intdiv0.m4 \
 	intldir.m4 \
diff --git a/src/Makefile.am b/src/Makefile.am
index 22baa16f6b0c5fceacaa8c4d22a1209c67e041b6..f3f0609bc3f91183232e8b575dc5a3f9553e44a7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,5 @@
-AM_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" -DSYSCONFDIR=\"$(sysconfdir)\"
+AM_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" -DSYSCONFDIR=\"$(sysconfdir)\" \
+	-I$(top_builddir)/lib -I$(top_srcdir)/lib
 
 CLEANFILES = revision.h
 
@@ -37,7 +38,7 @@ nano_SOURCES =	browser.c \
 		utils.c \
 		winio.c 
 
-nano_LDADD = @GLIB_LIBS@ @LIBINTL@
+nano_LDADD = @GLIB_LIBS@ @LIBINTL@ $(top_builddir)/lib/libgnu.a
 
 install-exec-hook:
 	cd $(DESTDIR)$(bindir) && rm -f rnano && $(LN_S) nano rnano