--- a/configure.ac
+++ b/configure.ac
@@ -1058,27 +1058,25 @@
       ;;
       yes|*)
         use_uim_fep="yes"
-	AC_CHECK_LIB(curses, setupterm, FEP_LIBADD="-lcurses $FEP_LIBADD",
-	  [AC_CHECK_LIB(ncurses, setupterm, FEP_LIBADD="-lncurses $FEP_LIBADD",
-	    AC_MSG_WARN([fep needs setupterm in libcurses or libncurses. disabled...]);use_uim_fep="no")])
-	AC_CHECK_FUNCS(forkpty)
-	if test $ac_cv_func_forkpty = no; then
-	  AC_CHECK_LIB(util, forkpty, [AC_DEFINE(HAVE_FORKPTY) FEP_LIBADD="-lutil $FEP_LIBADD"])
-	fi
-	AC_SUBST(FEP_LIBADD)
       ;;
     esac
   ],
-  [ use_uim_fep="yes"
-    AC_CHECK_LIB(curses, setupterm, FEP_LIBADD="-lcurses $FEP_LIBADD",
-      [AC_CHECK_LIB(ncurses, setupterm, FEP_LIBADD="-lncurses $FEP_LIBADD",
-        AC_MSG_WARN([fep needs setupterm in libcurses or libncurses. disabled...]);use_uim_fep="no")])
-    AC_CHECK_FUNCS(forkpty)
-    if test $ac_cv_func_forkpty = no; then
-      AC_CHECK_LIB(util, forkpty, [AC_DEFINE(HAVE_FORKPTY) FEP_LIBADD="-lutil $FEP_LIBADD"])
-    fi
-    AC_SUBST(FEP_LIBADD)
-  ])
+  [use_uim_fep="yes"])
+
+if test "x$use_uim_fep" != "xno"; then
+  PKG_CHECK_MODULES(CURSES, ncurses,
+    [FEP_LIBADD="$CURSES_LIBS $FEP_LIBADD"],
+    [
+      AC_CHECK_LIB(curses, setupterm, FEP_LIBADD="-lcurses $FEP_LIBADD",
+        [AC_CHECK_LIB(ncurses, setupterm, FEP_LIBADD="-lncurses $FEP_LIBADD",
+          AC_MSG_WARN([fep needs setupterm in libcurses or libncurses. disabled...]);use_uim_fep="no")])
+    ])
+  AC_CHECK_FUNCS(forkpty)
+  if test $ac_cv_func_forkpty = no; then
+    AC_CHECK_LIB(util, forkpty, [AC_DEFINE(HAVE_FORKPTY) FEP_LIBADD="-lutil $FEP_LIBADD"])
+  fi
+  AC_SUBST(FEP_LIBADD)
+fi
 
 AC_ARG_ENABLE(emacs,
   AC_HELP_STRING([--disable-emacs],
@@ -1210,10 +1208,14 @@
     saved_LDFLAGS=$LDFLAGS
     CPPFLAGS="${CPPFLAGS} -I$libedit_path/include"
     LDFLAGS="${LDFLAGS} -L$libedit_path/lib"
-    AC_CHECK_LIB(curses, tgetent, LIBEDIT_LIBS="-lcurses",
-        [AC_CHECK_LIB(ncurses, tgetent, LIBEDIT_LIBS="-lncurses",
-            AC_MSG_WARN("libedit needs libcurses or libncurses. disabled...")
-            use_libedit="no")])
+    PKG_CHECK_MODULES(CURSES, ncurses,
+        [LIBEDIT_LIBS="$CURSES_LIBS"],
+        [
+            AC_CHECK_LIB(curses, tgetent, LIBEDIT_LIBS="-lcurses",
+                [AC_CHECK_LIB(ncurses, tgetent, LIBEDIT_LIBS="-lncurses",
+                    AC_MSG_WARN("libedit needs libcurses or libncurses. disabled...")
+                    use_libedit="no")])
+        ])
     if test "x$use_libedit" != "xno"; then
         AC_CHECK_LIB(edit, el_init,
             [LIBEDIT_LIBS="-ledit $LIBEDIT_LIBS -L$libedit_path/lib"
