--- configure.orig	Fri Feb  7 20:52:50 2003
+++ configure	Fri Feb  7 20:54:38 2003
@@ -28,6 +28,10 @@
 prefix=/usr/local
 ssl_includedir=
 ssl_libdir=
+idn_includedir=
+idn_libdir=
+iconv_includedir=
+iconv_libdir=
 gc_includedir=
 gc_libdir=
 mimetypes=
@@ -115,6 +119,18 @@
   --ssl-libdir=*)
     ssl_libdir=`expr "X$1" : "X--ssl-libdir=\(.*\)"`
     ;;
+  --idn-includedir=*)
+    idn_includedir=`expr "X$1" : "X--idn-includedir=\(.*\)"`
+    ;;
+  --idn-libdir=*)
+    idn_libdir=`expr "X$1" : "X--idn-libdir=\(.*\)"`
+    ;;
+  --iconv-includedir=*)
+    iconv_includedir=`expr "X$1" : "X--iconv-includedir=\(.*\)"`
+    ;;
+  --iconv-libdir=*)
+    iconv_libdir=`expr "X$1" : "X--iconv-libdir=\(.*\)"`
+    ;;
   --gc-includedir=*)
     gc_includedir=`expr "X$1" : "X--gc-includedir=\(.*\)"`
     ;;
@@ -523,6 +539,57 @@
   return 0
 }
 
+find_idn() {
+  lite=$1
+  idninclude=""
+  for dirname in $idn_includedir /usr /usr/local $prefix
+  do
+    if [ -f $dirname/include/idn/api.h ]; then
+      echo "$dirname/include/idn/api.h found"
+      idninclude="-I${dirname}/include"
+      break
+    fi
+  done
+  idnlib=""
+  for dirname in $idn_libdir /usr /usr/local $prefix
+  do
+    if [ -f $dirname/lib/libidnkit$lite.a -o -f $dirname/lib/libidnkit$lite.$so_ext ]; then
+      idnlib="-L${dirname}/lib -lidnkit$lite"
+      break
+    fi
+  done
+  if [ "x$idninclude" = x -o "x$idnlib" = x ]; then
+    echo "You don't have idnkit. Install idnkit."
+    return 1
+  fi
+  return 0
+}
+
+find_iconv() {
+  iconvinclude=""
+  for dirname in $iconv_includedir /usr /usr/local $prefix
+  do
+    if [ -f $dirname/include/iconv.h ]; then
+      echo "$dirname/include/iconv.h found"
+      iconvinclude="-I${dirname}/include"
+      break
+    fi
+  done
+  iconvlib=""
+  for dirname in $iconv_libdir /usr /usr/local $prefix
+  do
+    if [ -f $dirname/lib/libiconv.a -o -f $dirname/lib/libiconv.$so_ext ]; then
+      iconvlib="-L${dirname}/lib -liconv"
+      break
+    fi
+  done
+  if [ "x$iconvinclude" = x -o "x$iconvlib" = x ]; then
+    echo "You don't have iconv. Install iconv."
+    return 1
+  fi
+  return 0
+}
+
 #--------------------------------------------------------------
 if [ -n "$USER" ]; then
 	user=$USER
@@ -955,6 +1022,7 @@
 else
   use_xface=n
 fi
+ask_param "Internationalized domain name (IDN) support (you need idnkit)" use_idn n
 
 ### only use config.param
 def_param use_dict y
@@ -2305,6 +2373,31 @@
   fi
 fi
 
+if [ "$use_idn" = y ]; then
+  if [ "$use_m17n" = y -a "$use_unicode" = y ]; then
+    if find_idn lite; then
+    else
+      echo "Disable IDN support"
+      def_use_idn="#undef USE_IDN"
+      idninclude=""
+      idnlib=""
+    fi
+  else
+    if find_idn "" && find_iconv; then
+      idninclude="$idninclude $iconvinclude"
+      idnlib="$idnlib $iconvlib"
+    else
+      echo "Disable IDN support"
+      def_use_idn="#undef USE_IDN"
+      idninclude=""
+      idnlib=""
+    fi
+  fi
+else
+  idninclude=""
+  idnlib=""
+fi
+
 rm -f _zmachdep$extension _zmachdep.c _zmachdep.o
 echo "------------ Configuration done ------------"
 
@@ -2429,10 +2522,10 @@
 ETC_DIR = $sysconfdir
 RC_DIR = ~/.w3m$suffix
 HELP_FILE = $helpfile
-SYS_LIBRARIES = $gpmlib $termlib $ssllib $v6lib
+SYS_LIBRARIES = $gpmlib $termlib $ssllib $v6lib $idnlib
 LOCAL_LIBRARIES = $extlib
 CC = $cc
-MYCFLAGS = $cflags $bsdinclude $sslinclude $wcinclude
+MYCFLAGS = $cflags $bsdinclude $sslinclude $idninclude $wcinclude
 LDFLAGS = $ldflags
 GCCFLAGS = $cflags -I./\$(srcdir)/include -DATOMIC_UNCOLLECTABLE -DNO_SIGNALS -DNO_EXECUTE_PERMISSION -DSILENT -DALL_INTERIOR_POINTERS
 KEYBIND_SRC = $keymap_file.c
@@ -2504,6 +2597,7 @@
 $def_use_imlib2
 $def_use_help_cgi
 $def_use_xface
+$def_use_idn
 
 #define DEF_EDITOR "$editor"
 #define DEF_MAILER "$mailer"
--- file.c.orig	Fri Feb  7 20:52:50 2003
+++ file.c	Fri Feb  7 20:54:05 2003
@@ -1580,7 +1580,8 @@
     url_option.referer = referer;
     url_option.flag = flag;
     f = openURL(tpath, &pu, current, &url_option, request, extra_header, of,
-		&hr, &status);
+		&hr, &status, (current && CurrentTab && Currentbuf) ?
+			      Currentbuf->document_charset : SystemCharset);
     of = NULL;
 #ifdef USE_M17N
     content_charset = 0;
--- proto.h.orig	Fri Feb  7 20:52:51 2003
+++ proto.h	Fri Feb  7 20:54:05 2003
@@ -505,10 +505,13 @@
 extern void init_stream(URLFile *uf, int scheme, InputStream stream);
 Str HTTPrequestMethod(HRequest *hr);
 Str HTTPrequestURI(ParsedURL *pu, HRequest *hr);
+#if !(defined(USE_IDN) && defined(USE_M17N))
+#define openURL(a,b,c,d,e,f,g,h,i,j) _openURL(a,b,c,d,e,f,g,h,i)
+#endif
 extern URLFile openURL(char *url, ParsedURL *pu, ParsedURL *current,
 		       URLOption *option, FormList *request,
 		       TextList *extra_header, URLFile *ouf,
-		       HRequest *hr, unsigned char *status);
+		       HRequest *hr, unsigned char *status, wc_ces charset);
 extern int mailcapMatch(struct mailcap *mcap, char *type);
 extern struct mailcap *searchMailcap(struct mailcap *table, char *type);
 extern void initMailcap();
--- url.c.orig	Fri Feb  7 20:52:51 2003
+++ url.c	Fri Feb  7 20:54:06 2003
@@ -27,6 +27,14 @@
 #include <err.h>
 #endif
 
+#ifdef USE_IDN
+#include <idn/api.h>
+#ifndef USE_M17N
+#define host_idn_encode(a,b) _host_idn_encode(a)
+#endif
+static char *host_idn_encode(char *host, wc_ces charset);
+#endif
+
 #ifdef	__WATT32__
 #define	write(a,b,c)	write_s(a,b,c)
 #endif				/* __WATT32__ */
@@ -1450,7 +1458,7 @@
 URLFile
 openURL(char *url, ParsedURL *pu, ParsedURL *current,
 	URLOption *option, FormList *request, TextList *extra_header,
-	URLFile *ouf, HRequest *hr, unsigned char *status)
+	URLFile *ouf, HRequest *hr, unsigned char *status, wc_ces charset)
 {
     Str tmp;
     int sock, scheme;
@@ -1507,6 +1515,10 @@
     hr->referer = option->referer;
     hr->request = request;
 
+#ifdef USE_IDN
+    if (pu->host)
+	pu->host = host_idn_encode(pu->host, charset);
+#endif
     switch (pu->scheme) {
     case SCM_LOCAL:
     case SCM_LOCAL_CGI:
@@ -2207,3 +2219,39 @@
     }
     return pu;
 }
+
+#ifdef USE_IDN
+static char*
+host_idn_encode(char *host, wc_ces charset)
+{
+    char *p;
+    char buf[64];
+    int actions = IDN_ENCODE_APP;
+
+    if (!host)
+	return host;
+    for (p = host; *p; p++) {
+	if (!IS_ASCII(*p) || *p == '%')
+	    break;
+    }
+    if (!*p)
+	return host;
+
+    p = file_unquote(host);
+#ifdef USE_M17N
+    if (!charset || charset == WC_CES_US_ASCII)
+	charset = SystemCharset;
+    charset = wc_auto_detect(p, strlen(p), charset);
+#ifdef USE_UNICODE
+    p = wc_conv_strict(p, charset, WC_CES_UTF_8)->ptr;
+    actions &= ~IDN_LOCALCONV;
+#else
+    set_environ("IDN_LOCAL_CODESET", wc_ces_to_charset(charset));
+#endif
+#endif
+    idn_nameinit(1);
+    if (idn_encodename(actions, p, buf, sizeof(buf)) != idn_success)
+	return host;
+    return allocStr(buf, -1);
+}
+#endif
