--- complete.c~0	2011-01-16 22:32:57.000000000 +0200
+++ complete.c	2012-02-26 15:27:59.486660800 +0200
@@ -156,7 +156,7 @@ int _rl_complete_mark_symlink_dirs = 0;
 int _rl_print_completions_horizontally;
 
 /* Non-zero means that case is not significant in filename completion. */
-#if defined (__MSDOS__) && !defined (__DJGPP__)
+#if (defined (__MSDOS__) && !defined (__DJGPP__)) || defined(_WIN32)
 int _rl_completion_case_fold = 1;
 #else
 int _rl_completion_case_fold = 0;
@@ -537,6 +537,36 @@ path_isdir (filename)
 }
 
 #if defined (VISIBLE_STATS)
+
+#ifdef _WIN32
+/* Newer versions of Microsoft runtime barf when passed X_OK as access
+   mode, since only values of 0, 2, and 4 are supported.  */
+#ifdef access
+#undef access
+#endif
+int
+access (const char *fn, int mode)
+{
+  if (mode == X_OK)
+    {
+      char const * exeext[] = { ".exe", ".com", ".bat", ".cmd" };
+      size_t fnlen = strlen (fn);
+      int i;
+
+      if (fnlen > 4)
+	{
+	  for (i = 0; exeext[i]; i++)
+	    {
+	      if (stricmp (fn + fnlen - 4, exeext[i]) == 0)
+		return 0;
+	    }
+	}
+      return 1;
+    }
+  return _access (fn, mode);
+}
+#endif
+
 /* Return the character which best describes FILENAME.
      `@' for symbolic links
      `/' for directories
@@ -618,7 +648,7 @@ printable_part (pathname)
     return (pathname);
 
   temp = strrchr (pathname, '/');
-#if defined (__MSDOS__)
+#if defined (__MSDOS__) || defined(_WIN32)
   if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
     temp = pathname + 1;
 #endif
@@ -2179,7 +2209,7 @@ rl_filename_completion_function (text, s
 	  strcpy (filename, ++temp);
 	  *temp = '\0';
 	}
-#if defined (__MSDOS__)
+#if defined (__MSDOS__) || defined(_WIN32)
       /* searches from current directory on the drive */
       else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':')
         {
--- funmap.c~0	2010-05-31 01:32:37.000000000 +0300
+++ funmap.c	2012-02-26 15:46:27.284065800 +0200
@@ -113,7 +113,7 @@ static const FUNMAP default_funmap[] = {
   { "non-incremental-reverse-search-history-again", rl_noninc_reverse_search_again },
   { "old-menu-complete", rl_old_menu_complete },
   { "overwrite-mode", rl_overwrite_mode },
-#ifdef __CYGWIN__
+#if defined(__CYGWIN__) || defined(__MINGW32__)
   { "paste-from-clipboard", rl_paste_from_clipboard },
 #endif
   { "possible-completions", rl_possible_completions },
--- histfile.c~0	2010-07-26 00:39:31.000000000 +0300
+++ histfile.c	2012-02-26 15:33:45.628245600 +0200
@@ -123,6 +123,10 @@ history_filename (filename)
     return (return_val);
   
   home = sh_get_env_value ("HOME");
+#ifdef _WIN32
+  if (!home)
+    home = sh_get_env_value ("USERPROFILE");
+#endif
 
   if (home == 0)
     {
--- kill.c~0	2010-12-07 02:44:58.000000000 +0200
+++ kill.c	2012-02-26 15:46:47.768440800 +0200
@@ -657,7 +657,7 @@ rl_yank_last_arg (count, key)
 }
 
 /* A special paste command for users of Cygnus's cygwin32. */
-#if defined (__CYGWIN__)
+#if defined (__CYGWIN__) || defined (__MINGW32__)
 #include <windows.h>
 
 int
@@ -691,4 +691,4 @@ rl_paste_from_clipboard (count, key)
     }
   return (0);
 }
-#endif /* __CYGWIN__ */
+#endif /* __CYGWIN__ || __MINGW32__ */
--- shell.c~0	2010-07-26 00:40:06.000000000 +0300
+++ shell.c	2012-02-26 15:50:48.471565800 +0200
@@ -168,6 +168,9 @@ sh_get_home_dir ()
   if (entry)
     home_dir = entry->pw_dir;
 #endif
+#ifdef _WIN32
+  home_dir = sh_get_env_value ("USERPROFILE");
+#endif
   return (home_dir);
 }
 
--- tilde.c~0	2010-07-26 00:42:13.000000000 +0300
+++ tilde.c	2012-02-26 16:05:00.284065800 +0200
@@ -171,7 +171,7 @@ tilde_find_suffix (string)
 
   for (i = 0; i < string_len; i++)
     {
-#if defined (__MSDOS__)
+#if defined (__MSDOS__) || defined (_WIN32)
       if (string[i] == '/' || string[i] == '\\' /* || !string[i] */)
 #else
       if (string[i] == '/' /* || !string[i] */)
@@ -271,7 +271,7 @@ isolate_tilde_prefix (fname, lenp)
   int i;
 
   ret = (char *)xmalloc (strlen (fname));
-#if defined (__MSDOS__)
+#if defined (__MSDOS__) || defined (_WIN32)
   for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++)
 #else
   for (i = 1; fname[i] && fname[i] != '/'; i++)
--- util.c~0	2010-05-31 01:36:02.000000000 +0300
+++ util.c	2012-02-26 16:01:40.987190800 +0200
@@ -507,7 +507,14 @@ _rl_tropen ()
 
   if (_rl_tracefp)
     fclose (_rl_tracefp);
+#ifdef _WIN32
+  /* Windows doesn't have /var/tmp, so open the trace file in
+     temporary directory.  */
+  sprintf (fnbuf, "%s/rltrace.%ld",
+	   (getenv ("TEMP") ? getenv ("TEMP") : "."), getpid());
+#else
   sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid());
+#endif
   unlink(fnbuf);
   _rl_tracefp = fopen (fnbuf, "w+");
   return _rl_tracefp != 0;
--- readline.h~0	2011-01-16 22:33:09.000000000 +0200
+++ readline.h	2012-02-26 16:08:38.268440800 +0200
@@ -173,7 +173,7 @@
 extern int rl_yank_nth_arg PARAMS((int, int));
 extern int rl_yank_last_arg PARAMS((int, int));
 /* Not available unless __CYGWIN__ is defined. */
-#ifdef __CYGWIN__
+#if defined (__CYGWIN__) || defined (__MINGW32__)
 extern int rl_paste_from_clipboard PARAMS((int, int));
 #endif
 
--- input.c~0	2010-05-31 01:33:01.000000000 +0300
+++ input.c	2012-02-26 17:00:01.986330800 +0200
@@ -86,6 +86,21 @@ static int ibuffer_space PARAMS((void));
 static int rl_get_char PARAMS((int *));
 static int rl_gather_tyi PARAMS((void));
 
+#ifdef __MINGW32__
+
+#define WIN32_LEAN_AND_MEAN 1
+#include <windows.h>
+
+int w32_isatty ( int fd )
+{
+  if (_isatty(fd))
+    return (((long) (HANDLE) _get_osfhandle(fd)) & 3) == 3;
+  return 0;
+}
+
+#define isatty(x)  w32_isatty(x)
+#endif
+
 /* **************************************************************** */
 /*								    */
 /*			Character Input Buffering       	    */
@@ -466,7 +481,10 @@ rl_getc (stream)
 
 #if defined (__MINGW32__)
       if (isatty (fileno (stream)))
-	return (getch ());
+	{
+	  /* Use _getch() from MS runtime, not getch() from ncurses!  */
+	  return (_getch ());
+	}
 #endif
       result = read (fileno (stream), &c, sizeof (unsigned char));
 
--- examples/fileman.c~	2009-01-04 21:32:33.000000000 +0200
+++ examples/fileman.c	2012-02-27 09:31:21.368132100 +0200
@@ -323,7 +323,7 @@ com_list (arg)
   if (!arg)
     arg = "";
 
-  sprintf (syscom, "ls -FClg %s", arg);
+  sprintf (syscom, "ls -FClg \"%s\"", arg);
   return (system (syscom));
 }
 
@@ -337,7 +337,7 @@ com_view (arg)
   /* more.com doesn't grok slashes in pathnames */
   sprintf (syscom, "less %s", arg);
 #else
-  sprintf (syscom, "more %s", arg);
+  sprintf (syscom, "more \"%s\"", arg);
 #endif
   return (system (syscom));
 }
--- examples/histexamp.c~	2009-01-04 21:32:33.000000000 +0200
+++ examples/histexamp.c	2012-02-27 09:36:30.005042100 +0200
@@ -96,7 +96,7 @@ main (argc, argv)
 	      {
 	      	tt = history_get_time (the_list[i]);
 		if (tt)
-		  strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt));
+		  strftime (timestr, sizeof (timestr), "%a %H:%M", localtime(&tt));
 		else
 		  strcpy (timestr, "??");
 	        printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line);
--- examples/rlevent.c~	2009-01-06 19:04:26.000000000 +0200
+++ examples/rlevent.c	2012-02-27 09:24:21.517329300 +0200
@@ -47,6 +47,12 @@ extern void exit();
 #  include <readline/history.h>
 #endif
 
+#ifdef __MINGW32__
+#include <windows.h>
+
+#define sleep(n)  Sleep(n*1000)
+#endif
+
 extern int optind;
 extern char *optarg;
 
