unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH 1/4] Compilation cleanups
  2011-12-29 14:03 [PATCH 0/4] Support Win32 GUI in Cygwin Emacs Daniel Colascione
                   ` (2 preceding siblings ...)
  2011-12-29 14:03 ` [PATCH 2/4] Refactor window-system configuration Daniel Colascione
@ 2011-12-29 14:03 ` Daniel Colascione
  2011-12-29 17:54   ` Paul Eggert
  3 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 14:03 UTC (permalink / raw)
  To: emacs-devel

These fixes are necessary for compiling Emacs cleanly and without warnings.
---
 lib-src/update-game-score.c |    2 ++
 src/gmalloc.c               |    4 ++--
 src/unexcw.c                |    2 ++
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index e335617..5367f11 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -48,8 +48,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <sys/stat.h>
 
 /* Needed for SunOS4, for instance.  */
+#if !defined(CYGWIN)
 extern char *optarg;
 extern int optind, opterr;
+#endif // !defined(CYGWIN)
 
 static int usage (int err) NO_RETURN;
 
diff --git a/src/gmalloc.c b/src/gmalloc.c
index 7b5e6df..099a34b 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -1076,7 +1076,7 @@ _free_internal_nolock (ptr)
     return;
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char*)ptr < (char*)_heapbase)
     /* We're being asked to free something in the static heap. */
     return;
 #endif
@@ -1428,7 +1428,7 @@ _realloc_internal_nolock (ptr, size)
     return _malloc_internal_nolock (size);
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char*)ptr < (char*)_heapbase)
     /* ptr points into the static heap */
     return special_realloc (ptr, size);
 #endif
diff --git a/src/unexcw.c b/src/unexcw.c
index 62df82e..0629420 100644
--- a/src/unexcw.c
+++ b/src/unexcw.c
@@ -31,6 +31,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #define DOTEXE ".exe"
 
+extern void report_sheap_usage (int);
+
 extern int bss_sbrk_did_unexec;
 
 extern int __malloc_initialized;
-- 
1.7.5.1





^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [PATCH 0/4] Support Win32 GUI in Cygwin Emacs
@ 2011-12-29 14:03 Daniel Colascione
  2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
                   ` (3 more replies)
  0 siblings, 4 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 14:03 UTC (permalink / raw)
  To: emacs-devel

This set of patches allows a Cygwin Emacs to use the w32 window-system
instead of X11.  It also fixes a few bugs in the Windows code and
makes the window-system configuration mechanism more generic,
eliminating inclusion of window-system specific headers in most of the
code.

Daniel Colascione (4):
  Compilation cleanups
  Refactor window-system configuration
  Implement cygw32
  Fix emacsclient to work with cygw32

 configure.in                    |  133 ++++++-
 lib-src/emacsclient.c           |   71 ++--
 lib-src/update-game-score.c     |    2 +
 lisp/battery.el                 |    2 +-
 lisp/faces.el                   |    2 +-
 lisp/frame.el                   |    4 +-
 lisp/international/mule-cmds.el |    3 +-
 lisp/loadup.el                  |   13 +-
 lisp/mouse.el                   |    2 +-
 lisp/server.el                  |   10 +-
 lisp/simple.el                  |    2 +-
 lisp/term/common-win.el         |    2 +-
 lisp/term/w32-win.el            |   42 ++-
 lisp/w32-common-fns.el          |  130 +++++++
 lisp/w32-fns.el                 |  105 +------
 lisp/w32-vars.el                |   22 +-
 src/Makefile.in                 |   41 ++-
 src/ccl.h                       |    2 +
 src/cygw32.c                    |  168 +++++++++
 src/cygw32.h                    |   59 ++++
 src/dispextern.h                |    4 +-
 src/dispnew.c                   |   14 +-
 src/emacs.c                     |   42 ++-
 src/font.c                      |   18 +-
 src/font.h                      |    4 +-
 src/fontset.c                   |    2 +-
 src/frame.c                     |   23 +-
 src/frame.h                     |   33 ++-
 src/gmalloc.c                   |    4 +-
 src/gtkutil.h                   |    1 +
 src/image.c                     |   85 +++--
 src/keyboard.c                  |   31 +-
 src/keyboard.h                  |    4 +-
 src/menu.c                      |   21 +-
 src/nsterm.h                    |   26 +--
 src/process.c                   |    8 +-
 src/s/cygwin.h                  |   11 +
 src/termhooks.h                 |    6 +-
 src/unexcw.c                    |    2 +
 src/w32.c                       |   19 +-
 src/w32.h                       |   10 -
 src/w32console.c                |   48 ---
 src/w32fns.c                    |  728 ++++++++++++++++++++++++++++++++-------
 src/w32font.c                   |    4 +
 src/w32font.h                   |    4 +
 src/w32heap.c                   |   54 ---
 src/w32heap.h                   |   38 ++-
 src/w32inevt.c                  |  192 +----------
 src/w32menu.c                   |   21 +-
 src/w32proc.c                   |   14 -
 src/w32select.c                 |    5 +
 src/w32select.h                 |   30 ++
 src/w32term.c                   |   49 ++-
 src/w32term.h                   |   71 ++++-
 src/w32xfns.c                   |   34 ++-
 src/window.c                    |    2 +-
 src/xdisp.c                     |    6 +-
 src/xfaces.c                    |   36 +-
 src/xterm.h                     |   31 +--
 59 files changed, 1626 insertions(+), 924 deletions(-)
 create mode 100644 lisp/w32-common-fns.el
 create mode 100644 src/cygw32.c
 create mode 100644 src/cygw32.h
 create mode 100644 src/w32select.h

-- 
1.7.5.1




^ permalink raw reply	[flat|nested] 42+ messages in thread

* [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 14:03 [PATCH 0/4] Support Win32 GUI in Cygwin Emacs Daniel Colascione
@ 2011-12-29 14:03 ` Daniel Colascione
  2011-12-29 16:28   ` Juanma Barranquero
  2011-12-29 17:39   ` Eli Zaretskii
  2011-12-29 14:03 ` [PATCH 3/4] Implement cygw32 Daniel Colascione
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 14:03 UTC (permalink / raw)
  To: emacs-devel

---
 lib-src/emacsclient.c |   71 ++++++++++++++++++++++++++----------------------
 lisp/server.el        |   10 +++++--
 2 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 5e1c2d6..086bb86 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -43,6 +43,10 @@ char *w32_getenv (char *);
 
 #else /* !WINDOWSNT */
 
+# ifdef HAVE_NTGUI
+# include <windows.h>
+# endif /* HAVE_NTGUI */
+
 # include "syswait.h"
 
 # ifdef HAVE_INET_SOCKETS
@@ -184,7 +188,7 @@ struct option longopts[] =
   { "socket-name",	required_argument, NULL, 's' },
 #endif
   { "server-file",	required_argument, NULL, 'f' },
-#ifndef WINDOWSNT
+#ifndef HAVE_NTGUI
   { "display",	required_argument, NULL, 'd' },
 #endif
   { "parent-id", required_argument, NULL, 'p' },
@@ -402,32 +406,6 @@ w32_getenv (char *envvar)
   return NULL;
 }
 
-void
-w32_set_user_model_id (void)
-{
-  HMODULE shell;
-  HRESULT (WINAPI * set_user_model) (wchar_t * id);
-
-  /* On Windows 7 and later, we need to set the user model ID
-     to associate emacsclient launched files with Emacs frames
-     in the UI.  */
-  shell = LoadLibrary ("shell32.dll");
-  if (shell)
-    {
-      set_user_model
-	= (void *) GetProcAddress (shell,
-				   "SetCurrentProcessExplicitAppUserModelID");
-      /* If the function is defined, then we are running on Windows 7
-	 or newer, and the UI uses this to group related windows
-	 together.  Since emacs, runemacs, emacsclient are related, we
-	 want them grouped even though the executables are different,
-	 so we need to set a consistent ID between them.  */
-      if (set_user_model)
-	set_user_model (L"GNU.Emacs");
-
-      FreeLibrary (shell);
-    }
-}
 
 int
 w32_window_app (void)
@@ -1456,10 +1434,37 @@ set_socket (int no_exit_if_error)
   exit (EXIT_FAILURE);
 }
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 FARPROC set_fg;  /* Pointer to AllowSetForegroundWindow.  */
 FARPROC get_wc;  /* Pointer to RealGetWindowClassA.  */
 
+void
+w32_set_user_model_id (void)
+{
+  HMODULE shell;
+  HRESULT (WINAPI * set_user_model) (wchar_t * id);
+
+  /* On Windows 7 and later, we need to set the user model ID
+     to associate emacsclient launched files with Emacs frames
+     in the UI.  */
+  shell = LoadLibrary ("shell32.dll");
+  if (shell)
+    {
+      set_user_model
+	= (void *) GetProcAddress (shell,
+				   "SetCurrentProcessExplicitAppUserModelID");
+      /* If the function is defined, then we are running on Windows 7
+	 or newer, and the UI uses this to group related windows
+	 together.  Since emacs, runemacs, emacsclient are related, we
+	 want them grouped even though the executables are different,
+	 so we need to set a consistent ID between them.  */
+      if (set_user_model)
+	set_user_model (L"GNU.Emacs");
+
+      FreeLibrary (shell);
+    }
+}
+
 BOOL CALLBACK
 w32_find_emacs_process (HWND hWnd, LPARAM lParam)
 {
@@ -1509,7 +1514,7 @@ w32_give_focus (void)
       && (get_wc = GetProcAddress (user32, "RealGetWindowClassA")))
     EnumWindows (w32_find_emacs_process, (LPARAM) 0);
 }
-#endif
+#endif /* HAVE_NTGUI */
 
 /* Start the emacs daemon and try to connect to it.  */
 
@@ -1581,11 +1586,11 @@ main (int argc, char **argv)
   main_argv = argv;
   progname = argv[0];
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   /* On Windows 7 and later, we need to explicitly associate emacsclient
      with emacs so the UI behaves sensibly.  */
   w32_set_user_model_id ();
-#endif
+#endif /* HAVE_NTGUI */
 
   /* Process options.  */
   decode_options (argc, argv);
@@ -1636,9 +1641,9 @@ main (int argc, char **argv)
       fail ();
     }
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   w32_give_focus ();
-#endif
+#endif /* HAVE_NTGUI */
 
   /* Send over our environment and current directory. */
   if (!current_frame)
diff --git a/lisp/server.el b/lisp/server.el
index edd8f2a..ec93b18 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1038,9 +1038,13 @@ The following commands are accepted by the client:
                        tty-type (pop args-left)
                        dontkill (or dontkill
                                     (not use-current-frame)))
-                 ;; On Windows, emacsclient always asks for a tty frame.
-                 ;; If running a GUI server, force the frame type to GUI.
-                 (when (eq window-system 'w32)
+                 ;; On Windows, emacsclient always asks for a tty
+                 ;; frame.  If running a GUI server, force the frame
+                 ;; type to GUI.  (Cygwin is perfectly happy with
+                 ;; multi-tty support, so don't override the user's
+                 ;; choice there.)
+                 (when (and (eq system-type 'windows-nt)
+                            (eq window-system 'w32))
                    (push "-window-system" args-left)))
 
                 ;; -position LINE[:COLUMN]:  Set point to the given
-- 
1.7.5.1




^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [PATCH 2/4] Refactor window-system configuration
  2011-12-29 14:03 [PATCH 0/4] Support Win32 GUI in Cygwin Emacs Daniel Colascione
  2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
  2011-12-29 14:03 ` [PATCH 3/4] Implement cygw32 Daniel Colascione
@ 2011-12-29 14:03 ` Daniel Colascione
  2011-12-29 22:21   ` Dan Nicolaescu
  2011-12-29 14:03 ` [PATCH 1/4] Compilation cleanups Daniel Colascione
  3 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 14:03 UTC (permalink / raw)
  To: emacs-devel

This change streamlines the window system selection code in
configure.in and moves many common function declarations from
window-specific headers to frame.h.  It introduces a new TERM_HEADER
macro in config.h: we set this macro to the right header to use for
the window system for which we're compiling Emacs and have source
files include it indirectly.  This way, we don't have to teach every
file about every window system.
---
 configure.in    |   51 ++++++++++++++++++++++++++++++++++++++-------------
 src/Makefile.in |   17 +++++++++++------
 src/ccl.h       |    2 ++
 src/dispnew.c   |   14 +++-----------
 src/emacs.c     |   12 ++++--------
 src/font.c      |   14 +++-----------
 src/frame.c     |   19 +++++--------------
 src/frame.h     |   30 ++++++++++++++++++++++++++++++
 src/gtkutil.h   |    1 +
 src/image.c     |   30 +++++++++++-------------------
 src/keyboard.c  |   19 +++++--------------
 src/keyboard.h  |    2 +-
 src/menu.c      |   17 +++--------------
 src/nsterm.h    |   26 ++------------------------
 src/process.c   |    7 ++++---
 src/w32font.h   |    4 ++++
 src/w32term.h   |    2 +-
 src/xfaces.c    |   20 ++++++++------------
 src/xterm.h     |   30 ++++++++----------------------
 19 files changed, 144 insertions(+), 173 deletions(-)

diff --git a/configure.in b/configure.in
index 83acc82..ceabb68 100644
--- a/configure.in
+++ b/configure.in
@@ -1417,10 +1417,14 @@ AC_SYS_LONG_FILE_NAMES
 
 #### Choose a window system.
 
+## We leave window_system equal to none if
+## we end up building without one.  Any new window system should
+## set window_system to an appropriate value and add objects to
+## window-system-specific substs.
+
+window_system=none
 AC_PATH_X
-if test "$no_x" = yes; then
-  window_system=none
-else
+if test "$no_x" != yes; then
   window_system=x11
 fi
 
@@ -1569,7 +1573,6 @@ NS_OBJ=
 NS_OBJC_OBJ=
 if test "${HAVE_NS}" = yes; then
   window_system=nextstep
-  with_xft=no
   # set up packaging dirs
   if test "${EN_NS_SELF_CONTAINED}" = yes; then
      prefix=${ns_appresdir}
@@ -1577,7 +1580,6 @@ if test "${HAVE_NS}" = yes; then
      libexecdir=${ns_appbindir}/libexec
   fi
   ns_frag=$srcdir/src/ns.mk
-  NS_OBJ="fontset.o fringe.o image.o"
   NS_OBJC_OBJ="nsterm.o nsfns.o nsmenu.o nsselect.o nsimage.o nsfont.o"
 fi
 CFLAGS="$tmp_CFLAGS"
@@ -1587,18 +1589,29 @@ AC_SUBST(NS_OBJC_OBJ)
 AC_SUBST(LIB_STANDARD)
 AC_SUBST_FILE(ns_frag)
 
+## $window_system is now set to the window system we will
+## ultimately use.
+
+term_header=
+HAVE_X_WINDOWS=no
+HAVE_X11=no
+USE_X_TOOLKIT=none
+
 case "${window_system}" in
   x11 )
     HAVE_X_WINDOWS=yes
     HAVE_X11=yes
+    term_header=xterm.h
     case "${with_x_toolkit}" in
       athena | lucid ) USE_X_TOOLKIT=LUCID ;;
       motif ) USE_X_TOOLKIT=MOTIF ;;
       gtk ) with_gtk=yes
+            term_header=gtkutil.h
 dnl Don't set this for GTK.  A lot of tests below assumes Xt when
 dnl USE_X_TOOLKIT is set.
             USE_X_TOOLKIT=none ;;
       gtk3 ) with_gtk3=yes
+             term_header=gtkutil.h
              USE_X_TOOLKIT=none ;;
       no ) USE_X_TOOLKIT=none ;;
 dnl If user did not say whether to use a toolkit, make this decision later:
@@ -1606,13 +1619,16 @@ dnl use the toolkit if we have gtk, or X11R5 or newer.
       * ) USE_X_TOOLKIT=maybe ;;
     esac
   ;;
-  nextstep | none )
-    HAVE_X_WINDOWS=no
-    HAVE_X11=no
-    USE_X_TOOLKIT=none
+  nextstep )
+    term_header=nsterm.h
   ;;
 esac
 
+if test -n "${term_header}"; then
+    AC_DEFINE_UNQUOTED(TERM_HEADER, "${term_header}",
+        [Define to the header for the built-in window system.])
+fi
+
 if test "$window_system" = none && test "X$with_x" != "Xno"; then
    AC_CHECK_PROG(HAVE_XSERVER, X, true, false)
    if test "$HAVE_XSERVER" = true ||
@@ -1925,6 +1941,7 @@ if test "${with_gtk3}" = "yes"; then
   fi
   AC_DEFINE(HAVE_GTK3, 1, [Define to 1 if using GTK 3 or later.])
   GTK_OBJ=emacsgtkfixed.o
+  term_header=gtkutil.h
 fi
 
 if test "$pkg_check_gtk" != "yes"; then
@@ -2003,6 +2020,8 @@ if test "${HAVE_GTK}" = "yes"; then
                  gtk_widget_get_mapped gtk_adjustment_get_page_size \
                  gtk_orientable_set_orientation \
 		 gtk_window_set_has_resize_grip)
+
+ term_header=gtkutil.h
 fi
 
 dnl D-Bus has been tested under GNU/Linux only.  Must be adapted for
@@ -3211,6 +3230,11 @@ AC_SUBST(ns_appsrc)
 AC_SUBST(GNU_OBJC_CFLAGS)
 AC_SUBST(OTHER_FILES)
 
+if test -n "${term_header}"; then
+    AC_DEFINE_UNQUOTED(TERM_HEADER, "${term_header}",
+        [Define to the header for the built-in window system.])
+fi
+
 AC_DEFINE_UNQUOTED(EMACS_CONFIGURATION,  "${canonical}",
 		   [Define to the canonical Emacs configuration name.])
 AC_DEFINE_UNQUOTED(EMACS_CONFIG_OPTIONS, "${ac_configure_args}",
@@ -3229,7 +3253,7 @@ if test "${HAVE_X_WINDOWS}" = "yes" ; then
   AC_DEFINE(HAVE_X_WINDOWS, 1,
 	    [Define to 1 if you want to use the X window system.])
   XMENU_OBJ=xmenu.o
-  XOBJ="xterm.o xfns.o xselect.o xrdb.o fontset.o xsmfns.o fringe.o image.o xsettings.o xgselect.o"
+  XOBJ="xterm.o xfns.o xselect.o xrdb.o xsmfns.o xsettings.o xgselect.o"
   FONT_OBJ=xfont.o
   if test "$HAVE_XFT" = "yes"; then
     FONT_OBJ="$FONT_OBJ ftfont.o xftfont.o ftxfont.o"
@@ -3506,13 +3530,14 @@ if test "x$GCC" = "xyes" && test "x$ORDINARY_LINK" != "xyes"; then
 fi                              dnl if $GCC
 AC_SUBST(LIB_GCC)
 
-
-## If we're using X11/GNUstep, define some consequences.
-if test "$HAVE_X_WINDOWS" = "yes" || test "$HAVE_NS" = "yes"; then
+## Common for all window systems
+if test "$window_system" != "none"; then
   AC_DEFINE(HAVE_WINDOW_SYSTEM, 1, [Define if you have a window system.])
   AC_DEFINE(HAVE_MOUSE, 1, [Define if you have mouse support.])
+  WINDOW_SYSTEM_OBJ="fontset.o fringe.o image.o"
 fi
 
+AC_SUBST(WINDOW_SYSTEM_OBJ)
 
 AH_TOP([/* GNU Emacs site configuration template file.
 
diff --git a/src/Makefile.in b/src/Makefile.in
index 3be10c3..99517a2 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -205,8 +205,8 @@ LIBXMENU=@LIBXMENU@
 
 ## xmenu.o if HAVE_X_WINDOWS, else empty.
 XMENU_OBJ=@XMENU_OBJ@
-## xterm.o xfns.o xselect.o xrdb.o fontset.o xsmfns.o fringe.o image.o
-## xsettings.o xgselect.o if HAVE_X_WINDOWS, else empty.
+## xterm.o xfns.o xselect.o xrdb.o xsmfns.o xsettings.o xgselect.o if
+## HAVE_X_WINDOWS, else empty.
 XOBJ=@XOBJ@
 
 TOOLKIT_LIBW=@TOOLKIT_LIBW@
@@ -241,6 +241,9 @@ WIDGET_OBJ=@WIDGET_OBJ@
 ## sheap.o if CYGWIN, otherwise empty.
 CYGWIN_OBJ=@CYGWIN_OBJ@
 
+## fontset.o fringe.o image.o if we have any window system
+WINDOW_SYSTEM_OBJ=@WINDOW_SYSTEM_OBJ@
+
 ## dosfns.o msdos.o w16select.o if MSDOS.
 MSDOS_OBJ =
 ## w16select.o termcap.o if MSDOS && HAVE_X_WINDOWS.
@@ -249,7 +252,6 @@ MSDOS_X_OBJ =
 ns_appdir=@ns_appdir@
 ns_appbindir=@ns_appbindir@
 ns_appsrc=@ns_appsrc@
-## fontset.o fringe.o image.o if HAVE_NS, else empty.
 NS_OBJ=@NS_OBJ@
 ## nsterm.o nsfns.o nsmenu.o nsselect.o nsimage.o nsfont.o if HAVE_NS.
 NS_OBJC_OBJ=@NS_OBJC_OBJ@
@@ -340,7 +342,8 @@ base_obj = dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \
 	process.o gnutls.o callproc.o \
 	region-cache.o sound.o atimer.o \
 	doprnt.o intervals.o textprop.o composite.o xml.o \
-	$(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ)
+	$(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ) \
+	$(WINDOW_SYSTEM_OBJ)
 obj = $(base_obj) $(NS_OBJC_OBJ)
 
 ## Object files used on some machine or other.
@@ -536,10 +539,12 @@ extraclean: distclean
 ctagsfiles1 = [xyzXYZ]*.[hcm]
 ctagsfiles2 = [a-wA-W]*.[hcm]
 
-TAGS: $(srcdir)/$(ctagsfiles1) $(srcdir)/$(ctagsfiles2) $(M_FILE) $(S_FILE)
+TAGS: $(srcdir)/$(ctagsfiles1) $(srcdir)/$(ctagsfiles2) \
+	$(M_FILE) $(S_FILE)
 	../lib-src/etags --include=TAGS-LISP --include=$(lwlibdir)/TAGS \
 	  --regex='/[ 	]*DEFVAR_[A-Z_ 	(]+"\([^"]+\)"/' \
-	  $(srcdir)/$(ctagsfiles1) $(srcdir)/$(ctagsfiles2) $(M_FILE) $(S_FILE)
+	  $(srcdir)/$(ctagsfiles1) $(srcdir)/$(ctagsfiles2) \
+	  $(M_FILE) $(S_FILE)
 frc:
 TAGS-LISP: frc
 	$(MAKE) -f $(lispdir)/Makefile TAGS-LISP ETAGS=../lib-src/etags
diff --git a/src/ccl.h b/src/ccl.h
index e861543..68079c5 100644
--- a/src/ccl.h
+++ b/src/ccl.h
@@ -26,6 +26,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef EMACS_CCL_H
 #define EMACS_CCL_H
 
+#include "character.h" /* For MAX_MULTIBYTE_LENGTH */
+
 /* Macros for exit status of CCL program.  */
 #define CCL_STAT_SUCCESS	0 /* Terminated successfully.  */
 #define CCL_STAT_SUSPEND_BY_SRC	1 /* Terminated by empty input.  */
diff --git a/src/dispnew.c b/src/dispnew.c
index 2c0e74d..f13fc7a 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -44,17 +44,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "syssignal.h"
 
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif /* HAVE_X_WINDOWS */
-
-#ifdef HAVE_NTGUI
-#include "w32term.h"
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
 /* Include systime.h after xterm.h to avoid double inclusion of time.h.  */
 
diff --git a/src/emacs.c b/src/emacs.c
index f4fa0eb..92b1889 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -31,6 +31,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "lisp.h"
 
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
+
 #ifdef WINDOWSNT
 #include <fcntl.h>
 #include <windows.h> /* just for w32.h */
@@ -61,14 +65,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "gnutls.h"
 #endif
 
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
-
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-
 #ifdef HAVE_SETLOCALE
 #include <locale.h>
 #endif
diff --git a/src/font.c b/src/font.c
index 6ab6528..1081b9a 100644
--- a/src/font.c
+++ b/src/font.c
@@ -37,17 +37,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "fontset.h"
 #include "font.h"
 
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif /* HAVE_X_WINDOWS */
-
-#ifdef HAVE_NTGUI
-#include "w32term.h"
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif /* HAVE_NS */
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
 Lisp_Object Qopentype;
 
diff --git a/src/frame.c b/src/frame.c
index bd97c5f..4650b6b 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -26,15 +26,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <setjmp.h>
 #include "lisp.h"
 #include "character.h"
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-#ifdef WINDOWSNT
-#include "w32term.h"
-#endif
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
+
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
+
 #include "buffer.h"
 /* These help us bind and responding to switch-frame events.  */
 #include "commands.h"
@@ -54,11 +50,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "dosfns.h"
 #endif
 
-
-#ifdef HAVE_WINDOW_SYSTEM
-
-#endif
-
 #ifdef HAVE_NS
 Lisp_Object Qns_parse_geometry;
 #endif
diff --git a/src/frame.h b/src/frame.h
index a32d1c5..f068ff4 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -1131,10 +1131,40 @@ extern Lisp_Object display_x_get_resource (Display_Info *,
 					   Lisp_Object component,
 					   Lisp_Object subclass);
 
+extern void set_frame_menubar (struct frame *f, int first_time, int deep_p);
+extern void x_set_window_size (struct frame *f, int change_grav,
+                              int cols, int rows);
+extern void x_sync (struct frame *);
+extern Lisp_Object x_get_focus_frame (struct frame *);
+extern void x_set_mouse_position (struct frame *f, int h, int v);
+extern void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
+extern void x_make_frame_visible (struct frame *f);
+extern void x_make_frame_invisible (struct frame *f);
+extern void x_iconify_frame (struct frame *f);
+extern int x_char_width (struct frame *f);
+extern int x_char_height (struct frame *f);
+extern int x_pixel_width (struct frame *f);
+extern int x_pixel_height (struct frame *f);
+extern void x_set_frame_alpha (struct frame *f);
+extern void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
+extern void x_set_tool_bar_lines (struct frame *f,
+                                  Lisp_Object value,
+                                  Lisp_Object oldval);
+extern void x_activate_menubar (struct frame *);
+extern void x_real_positions (struct frame *, int *, int *);
+extern int x_bitmap_icon (struct frame *, Lisp_Object);
+extern void x_set_menu_bar_lines (struct frame *,
+                                  Lisp_Object,
+                                  Lisp_Object);
+extern void free_frame_menubar (struct frame *);
+extern void x_free_frame_resources (struct frame *);
+
 #if defined HAVE_X_WINDOWS && !defined USE_X_TOOLKIT
 extern char *x_get_resource_string (const char *, const char *);
 #endif
 
+extern void x_query_colors (struct frame *f, XColor *, int);
+
 /* In xmenu.c */
 extern void set_frame_menubar (FRAME_PTR, int, int);
 
diff --git a/src/gtkutil.h b/src/gtkutil.h
index 7cc2d21..ff41921 100644
--- a/src/gtkutil.h
+++ b/src/gtkutil.h
@@ -25,6 +25,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <gtk/gtk.h>
 #include "frame.h"
+#include "xterm.h"
 
 /* Minimum and maximum values used for GTK scroll bars  */
 
diff --git a/src/image.c b/src/image.c
index 3d189a5..20879f0 100644
--- a/src/image.c
+++ b/src/image.c
@@ -48,11 +48,19 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "termhooks.h"
 #include "font.h"
 
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#include <sys/types.h>
+#ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif /* HAVE_SYS_STAT_H */
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif /* HAVE_SYS_TYPES_H */
+
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
+#ifdef HAVE_X_WINDOWS
 #define COLOR_TABLE_SUPPORT 1
 
 typedef struct x_bitmap_record Bitmap_Record;
@@ -65,11 +73,7 @@ typedef struct x_bitmap_record Bitmap_Record;
 #define PIX_MASK_DRAW	1
 #endif /* HAVE_X_WINDOWS */
 
-
 #ifdef HAVE_NTGUI
-#include "w32.h"
-#include "w32term.h"
-
 /* W32_TODO : Color tables on W32.  */
 #undef COLOR_TABLE_SUPPORT
 
@@ -82,15 +86,9 @@ typedef struct w32_bitmap_record Bitmap_Record;
 #define PIX_MASK_RETAIN	0
 #define PIX_MASK_DRAW	1
 
-#define FRAME_X_VISUAL(f) FRAME_X_DISPLAY_INFO (f)->visual
 #define x_defined_color w32_defined_color
 #define DefaultDepthOfScreen(screen) (one_w32_display_info.n_cbits)
 
-/* Functions from w32term.c that depend on XColor (so can't go in w32term.h
-   without modifying lots of files).  */
-extern void x_query_colors (struct frame *f, XColor *colors, int ncolors);
-extern void x_query_color (struct frame *f, XColor *color);
-
 /* Version of libpng that we were compiled with, or -1 if no PNG
    support was compiled in.  This is tested by w32-win.el to correctly
    set up the alist used to search for PNG libraries.  */
@@ -98,10 +96,6 @@ Lisp_Object Qlibpng_version;
 #endif /* HAVE_NTGUI */
 
 #ifdef HAVE_NS
-#include "nsterm.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-
 #undef COLOR_TABLE_SUPPORT
 
 typedef struct ns_bitmap_record Bitmap_Record;
@@ -115,10 +109,8 @@ typedef struct ns_bitmap_record Bitmap_Record;
 #define PIX_MASK_RETAIN	0
 #define PIX_MASK_DRAW	1
 
-#define FRAME_X_VISUAL FRAME_NS_DISPLAY_INFO (f)->visual
 #define x_defined_color(f, name, color_def, alloc) \
   ns_defined_color (f, name, color_def, alloc, 0)
-#define FRAME_X_SCREEN(f) 0
 #define DefaultDepthOfScreen(screen) x_display_list->n_planes
 #endif /* HAVE_NS */
 
diff --git a/src/keyboard.c b/src/keyboard.c
index 2df1ba7..dc0fe48 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -60,20 +60,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <unistd.h>
 #include <fcntl.h>
 
-/* This is to get the definitions of the XK_ symbols.  */
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-
-#ifdef HAVE_NTGUI
-#include "w32term.h"
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
-/* Variables for blockinput.h:  */
+/* Variables for blockinput.h: */
 
 /* Non-zero if interrupt input is blocked right now.  */
 volatile int interrupt_input_blocked;
@@ -1273,7 +1264,7 @@ usage: (track-mouse BODY...)  */)
    If ignore_mouse_drag_p is non-zero, ignore (implicit) mouse movement
    after resizing the tool-bar window.  */
 
-#if !defined HAVE_WINDOW_SYSTEM || defined USE_GTK || defined HAVE_NS
+#if !defined HAVE_WINDOW_SYSTEM
 static
 #endif
 int ignore_mouse_drag_p;
diff --git a/src/keyboard.h b/src/keyboard.h
index d4339d0..7011bf0 100644
--- a/src/keyboard.h
+++ b/src/keyboard.h
@@ -428,7 +428,7 @@ extern int waiting_for_input;
    happens.  */
 extern EMACS_TIME *input_available_clear_time;
 
-#if defined HAVE_WINDOW_SYSTEM && !defined USE_GTK && !defined HAVE_NS
+#if defined HAVE_WINDOW_SYSTEM
 extern int ignore_mouse_drag_p;
 #endif
 
diff --git a/src/menu.c b/src/menu.c
index 587f55e..9d0e645 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -36,24 +36,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "../lwlib/lwlib.h"
 #endif
 
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
-
-#ifdef USE_GTK
-#include "gtkutil.h"
-#endif
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
 #ifdef HAVE_NTGUI
-#include "w32term.h"
-
 extern AppendMenuW_Proc unicode_append_menu;
 extern HMENU current_popup_menu;
-
 #endif /* HAVE_NTGUI  */
 
 #include "menu.h"
diff --git a/src/nsterm.h b/src/nsterm.h
index 14918cc..042066c 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -623,6 +623,8 @@ struct x_output
 /* This is the `Display *' which frame F is on.  */
 #define FRAME_NS_DISPLAY(f) (0)
 #define FRAME_X_DISPLAY(f) (0)
+#define FRAME_X_SCREEN(f) (0)
+#define FRAME_X_VISUAL(f) FRAME_NS_DISPLAY_INFO(f)->visual
 
 #define FRAME_FOREGROUND_COLOR(f) ((f)->output_data.ns->foreground_color)
 #define FRAME_BACKGROUND_COLOR(f) ((f)->output_data.ns->background_color)
@@ -773,30 +775,6 @@ extern Lisp_Object find_and_return_menu_selection (FRAME_PTR f,
 extern Lisp_Object ns_popup_dialog (Lisp_Object position, Lisp_Object contents,
                                     Lisp_Object header);
 
-/* More prototypes that should be moved to a more general include file */
-extern void set_frame_menubar (struct frame *f, int first_time, int deep_p);
-extern void x_set_window_size (struct frame *f, int change_grav,
-                              int cols, int rows);
-extern void x_sync (struct frame *);
-extern Lisp_Object x_get_focus_frame (struct frame *);
-extern void x_set_mouse_position (struct frame *f, int h, int v);
-extern void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
-extern void x_make_frame_visible (struct frame *f);
-extern void x_make_frame_invisible (struct frame *f);
-extern void x_iconify_frame (struct frame *f);
-extern int x_char_width (struct frame *f);
-extern int x_char_height (struct frame *f);
-extern int x_pixel_width (struct frame *f);
-extern int x_pixel_height (struct frame *f);
-extern void x_set_frame_alpha (struct frame *f);
-extern void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
-extern void x_set_tool_bar_lines (struct frame *f,
-                                  Lisp_Object value,
-                                  Lisp_Object oldval);
-extern void x_activate_menubar (struct frame *);
-extern void free_frame_menubar (struct frame *);
-extern void x_free_frame_resources (struct frame *);
-
 #define NSAPP_DATA2_RUNASSCRIPT 10
 extern void ns_run_ascript (void);
 
diff --git a/src/process.c b/src/process.c
index 4b0f90b..2116027 100644
--- a/src/process.c
+++ b/src/process.c
@@ -113,12 +113,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "gnutls.h"
 #endif
 
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
+
 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
 #include "xgselect.h"
 #endif
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
 
 Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid;
 Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime;
diff --git a/src/w32font.h b/src/w32font.h
index f77866b..e13128e 100644
--- a/src/w32font.h
+++ b/src/w32font.h
@@ -19,6 +19,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifndef EMACS_W32FONT_H
 #define EMACS_W32FONT_H
 
+#include "font.h"
 
 /* Bit 17 of ntmFlags in NEWTEXTMETRIC is set for PostScript OpenType fonts,
    bit 18 for TrueType OpenType fonts, bit 20 for Type1 fonts.  */
@@ -83,4 +84,7 @@ int uniscribe_check_otf (LOGFONT *font, Lisp_Object otf_spec);
 
 Lisp_Object intern_font_name (char *);
 
+extern void syms_of_w32font (void);
+extern void globals_of_w32font (void);
+
 #endif
diff --git a/src/w32term.h b/src/w32term.h
index f587ee1..e3429fc 100644
--- a/src/w32term.h
+++ b/src/w32term.h
@@ -345,7 +345,7 @@ extern struct w32_output w32term_display;
 
 /* Return the window associated with the frame F.  */
 #define FRAME_W32_WINDOW(f) ((f)->output_data.w32->window_desc)
-#define FRAME_X_WINDOW(f) ((f)->output_data.w32->window_desc)
+#define FRAME_X_WINDOW(f) FRAME_W32_WINDOW (f)
 
 #define FRAME_FONT(f) ((f)->output_data.w32->font)
 #define FRAME_FONTSET(f) ((f)->output_data.w32->fontset)
diff --git a/src/xfaces.c b/src/xfaces.c
index 7e3ce41..1da31ae 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -225,11 +225,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "dosfns.h"
 #endif
 
-#ifdef WINDOWSNT
-#include "w32term.h"
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
 #include "fontset.h"
-/* Redefine X specifics to W32 equivalents to avoid cluttering the
-   code with #ifdef blocks. */
+#ifdef WINDOWSNT
 #undef FRAME_X_DISPLAY_INFO
 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
 #define x_display_info w32_display_info
@@ -239,7 +238,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #endif /* WINDOWSNT */
 
 #ifdef HAVE_NS
-#include "nsterm.h"
 #undef FRAME_X_DISPLAY_INFO
 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
 #define x_display_info ns_display_info
@@ -247,6 +245,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #define check_x check_ns
 #define GCGraphicsExposures 0
 #endif /* HAVE_NS */
+#endif /* HAVE_WINDOW_SYSTEM */
 
 #include "buffer.h"
 #include "dispextern.h"
@@ -256,9 +255,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "termchar.h"
 
 #include "font.h"
-#ifdef HAVE_WINDOW_SYSTEM
-#include "fontset.h"
-#endif /* HAVE_WINDOW_SYSTEM */
 
 #ifdef HAVE_X_WINDOWS
 
@@ -2579,13 +2575,13 @@ merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
 		}
 	      else if (EQ (keyword, QCstipple))
 		{
-#if defined (HAVE_X_WINDOWS) || defined (HAVE_NS)
+#if defined (HAVE_WINDOW_SYSTEM)
 		  Lisp_Object pixmap_p = Fbitmap_spec_p (value);
 		  if (!NILP (pixmap_p))
 		    to[LFACE_STIPPLE_INDEX] = value;
 		  else
 		    err = 1;
-#endif
+#endif /* HAVE_WINDOW_SYSTEM */
 		}
 	      else if (EQ (keyword, QCwidth))
 		{
@@ -3095,14 +3091,14 @@ FRAME 0 means change the face on all frames, and change the default
     }
   else if (EQ (attr, QCstipple))
     {
-#if defined (HAVE_X_WINDOWS) || defined (HAVE_NS)
+#if defined (HAVE_WINDOW_SYSTEM)
       if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
 	  && !NILP (value)
 	  && NILP (Fbitmap_spec_p (value)))
 	signal_error ("Invalid stipple attribute", value);
       old_value = LFACE_STIPPLE (lface);
       LFACE_STIPPLE (lface) = value;
-#endif /* HAVE_X_WINDOWS || HAVE_NS */
+#endif /* HAVE_WINDOW_SYSTEM */
     }
   else if (EQ (attr, QCwidth))
     {
diff --git a/src/xterm.h b/src/xterm.h
index 9e0e1ac..680d1d8 100644
--- a/src/xterm.h
+++ b/src/xterm.h
@@ -16,6 +16,9 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#ifndef XTERM_H
+#define XTERM_H
+
 #include <X11/Xlib.h>
 #include <X11/cursorfont.h>
 
@@ -367,13 +370,14 @@ extern int use_xim;
 extern void check_x (void);
 
 extern struct frame *x_window_to_frame (struct x_display_info *, int);
-
 extern struct frame *x_any_window_to_frame (struct x_display_info *, int);
 extern struct frame *x_menubar_window_to_frame (struct x_display_info *,
 						XEvent *);
-
 extern struct frame *x_top_window_to_frame (struct x_display_info *, int);
 
+extern struct frame *x_menubar_window_to_frame (struct x_display_info *,
+						XEvent *);
+
 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
 #define x_any_window_to_frame x_window_to_frame
 #define x_top_window_to_frame x_window_to_frame
@@ -389,7 +393,6 @@ extern struct x_display_info *x_display_list;
 extern Lisp_Object x_display_name_list;
 
 extern struct x_display_info *x_display_info_for_display (Display *);
-extern void x_set_frame_alpha (struct frame *);
 
 extern struct x_display_info *x_term_init (Lisp_Object, char *, char *);
 extern int x_display_ok  (const char *);
@@ -957,22 +960,13 @@ XrmDatabase x_load_resources (Display *, const char *, const char *,
 /* Defined in xterm.c */
 
 extern int x_text_icon (struct frame *, const char *);
-extern int x_bitmap_icon (struct frame *, Lisp_Object);
 extern void x_catch_errors (Display *);
 extern void x_check_errors (Display *, const char *)
   ATTRIBUTE_FORMAT_PRINTF (2, 0);
 extern int x_had_errors_p (Display *);
 extern void x_uncatch_errors (void);
 extern void x_clear_errors (Display *);
-extern void x_set_window_size (struct frame *, int, int, int);
-extern void x_set_mouse_position (struct frame *, int, int);
-extern void x_set_mouse_pixel_position (struct frame *, int, int);
 extern void x_ewmh_activate_frame (struct frame *);
-extern void x_make_frame_visible (struct frame *);
-extern void x_make_frame_invisible (struct frame *);
-extern void x_iconify_frame (struct frame *);
-extern void x_free_frame_resources (struct frame *);
-extern void x_wm_set_size_hint (struct frame *, long, int);
 extern void x_delete_terminal (struct terminal *terminal);
 extern unsigned long x_copy_color (struct frame *, unsigned long);
 #ifdef USE_X_TOOLKIT
@@ -985,7 +979,6 @@ extern int x_alloc_lighter_color_for_widget (Widget, Display *, Colormap,
                                              double, int);
 #endif
 extern int x_alloc_nearest_color (struct frame *, Colormap, XColor *);
-extern void x_query_colors (struct frame *f, XColor *, int);
 extern void x_query_color (struct frame *f, XColor *);
 extern void x_clear_area (Display *, Window, int, int, int, int, int);
 #if defined HAVE_MENUS && !defined USE_X_TOOLKIT && !defined USE_GTK
@@ -1036,15 +1029,12 @@ extern void x_clipboard_manager_save_all (void);
 /* Defined in xfns.c */
 
 extern struct x_display_info * check_x_display_info (Lisp_Object);
-extern Lisp_Object x_get_focus_frame (struct frame *);
 
 #ifdef USE_GTK
 extern int xg_set_icon (struct frame *, Lisp_Object);
 extern int xg_set_icon_from_xpm_data (struct frame *, const char**);
 #endif /* USE_GTK */
 
-extern void x_real_positions (struct frame *, int *, int *);
-extern void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
 extern void x_implicitly_set_name (struct frame *, Lisp_Object, Lisp_Object);
 extern void xic_free_xfontset (struct frame *);
 extern void create_frame_xic (struct frame *);
@@ -1054,9 +1044,6 @@ extern void xic_set_statusarea (struct frame *);
 extern void xic_set_xfontset (struct frame *, const char *);
 extern int x_pixel_width (struct frame *);
 extern int x_pixel_height (struct frame *);
-extern int x_char_width (struct frame *);
-extern int x_char_height (struct frame *);
-extern void x_sync (struct frame *);
 extern int x_defined_color (struct frame *, const char *, XColor *, int);
 #ifdef HAVE_X_I18N
 extern void free_frame_xic (struct frame *);
@@ -1064,7 +1051,6 @@ extern void free_frame_xic (struct frame *);
 extern char * xic_create_fontsetname (const char *base_fontname, int motif);
 # endif
 #endif
-extern void x_set_tool_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
 
 /* Defined in xfaces.c */
 
@@ -1081,10 +1067,8 @@ extern void x_menu_set_in_use (int);
 #ifdef USE_MOTIF
 extern void x_menu_wait_for_event (void *data);
 #endif
-extern void x_activate_menubar (struct frame *);
 extern int popup_activated (void);
 extern void initialize_frame_menubar (struct frame *);
-extern void free_frame_menubar (struct frame *);
 
 /* Defined in widget.c */
 
@@ -1126,3 +1110,5 @@ extern Lisp_Object Qx_gtk_map_stock;
    (nr).y = (ry),					\
    (nr).width = (rwidth),				\
    (nr).height = (rheight))
+
+#endif /* XTERM_H */
-- 
1.7.5.1





^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [PATCH 3/4] Implement cygw32
  2011-12-29 14:03 [PATCH 0/4] Support Win32 GUI in Cygwin Emacs Daniel Colascione
  2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
@ 2011-12-29 14:03 ` Daniel Colascione
  2011-12-29 17:29   ` Eli Zaretskii
  2011-12-30  9:49   ` Andreas Schwab
  2011-12-29 14:03 ` [PATCH 2/4] Refactor window-system configuration Daniel Colascione
  2011-12-29 14:03 ` [PATCH 1/4] Compilation cleanups Daniel Colascione
  3 siblings, 2 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 14:03 UTC (permalink / raw)
  To: emacs-devel

Here, we use the generic window-system configuration system we just
implemented to support the w32 window-system in the mainline build
under Cygwin.  (Previously, the w32 window system could only be
compiled as part of the NT-native Emacs build process.)

The changes in this patch need to be applied atomically in order to
avoid breaking Emacs.  The changes include:

  - Changes throughout the Lisp and C code to not assume that
      NT Emacs and the w32 window system are synonymous.

  - Wiring up the regular select(2) event loop to Windows messages

  - Cleaning up the w32 drag-and-drop receiving code.

  - Exposing Cygwin path conversion functions to elisp.

  - Unicode file dialog support when compiling for Cygwin.

  - Splitting the w32 term lisp initialization code into code
    applicable to any w32 window-system and code specific to
    system-type windows-nt.

  - Integrating the old and new w32 code into the build system.
---
 configure.in                    |   82 ++++-
 lisp/battery.el                 |    2 +-
 lisp/faces.el                   |    2 +-
 lisp/frame.el                   |    4 +-
 lisp/international/mule-cmds.el |    3 +-
 lisp/loadup.el                  |   13 +-
 lisp/mouse.el                   |    2 +-
 lisp/simple.el                  |    2 +-
 lisp/term/common-win.el         |    2 +-
 lisp/term/w32-win.el            |   42 ++-
 lisp/w32-common-fns.el          |  130 +++++++
 lisp/w32-fns.el                 |  105 +------
 lisp/w32-vars.el                |   22 +-
 src/Makefile.in                 |   26 +-
 src/cygw32.c                    |  168 +++++++++
 src/cygw32.h                    |   59 ++++
 src/dispextern.h                |    4 +-
 src/emacs.c                     |   30 ++-
 src/font.c                      |    4 +-
 src/font.h                      |    4 +-
 src/fontset.c                   |    2 +-
 src/frame.c                     |    4 +-
 src/frame.h                     |    3 +-
 src/image.c                     |   55 ++-
 src/keyboard.c                  |   12 +-
 src/keyboard.h                  |    2 +-
 src/menu.c                      |    4 +
 src/process.c                   |    1 +
 src/s/cygwin.h                  |   11 +
 src/termhooks.h                 |    6 +-
 src/w32.c                       |   19 +-
 src/w32.h                       |   10 -
 src/w32console.c                |   48 ---
 src/w32fns.c                    |  728 ++++++++++++++++++++++++++++++++-------
 src/w32font.c                   |    4 +
 src/w32heap.c                   |   54 ---
 src/w32heap.h                   |   38 ++-
 src/w32inevt.c                  |  192 +----------
 src/w32menu.c                   |   21 +-
 src/w32proc.c                   |   14 -
 src/w32select.c                 |    5 +
 src/w32select.h                 |   30 ++
 src/w32term.c                   |   49 ++-
 src/w32term.h                   |   69 ++++-
 src/w32xfns.c                   |   34 ++-
 src/window.c                    |    2 +-
 src/xdisp.c                     |    6 +-
 src/xfaces.c                    |   18 +-
 src/xterm.h                     |    1 -
 49 files changed, 1433 insertions(+), 715 deletions(-)
 create mode 100644 lisp/w32-common-fns.el
 create mode 100644 src/cygw32.c
 create mode 100644 src/cygw32.h
 create mode 100644 src/w32select.h

diff --git a/configure.in b/configure.in
index ceabb68..e7618f3 100644
--- a/configure.in
+++ b/configure.in
@@ -169,6 +169,7 @@ OPTION_DEFAULT_ON([toolkit-scroll-bars],[don't use Motif or Xaw3d scroll bars])
 OPTION_DEFAULT_ON([xaw3d],[don't use Xaw3d])
 OPTION_DEFAULT_ON([xim],[don't use X11 XIM])
 OPTION_DEFAULT_OFF([ns],[use NeXTstep (Cocoa or GNUstep) windowing system])
+OPTION_DEFAULT_OFF([w32], [use native Windows GUI])
 
 OPTION_DEFAULT_ON([gpm],[don't use -lgpm for mouse support on a GNU/Linux console])
 OPTION_DEFAULT_ON([dbus],[don't compile with D-Bus support])
@@ -1566,6 +1567,7 @@ fail;
     NS_HAVE_NSINTEGER=no
   fi
 fi
+
 AC_SUBST(TEMACS_LDFLAGS2)
 
 ns_frag=/dev/null
@@ -1589,6 +1591,30 @@ AC_SUBST(NS_OBJC_OBJ)
 AC_SUBST(LIB_STANDARD)
 AC_SUBST_FILE(ns_frag)
 
+HAVE_W32=no
+W32_OBJ=
+W32_LIBS=
+if test "${with_w32}" != no; then
+  if test "${opsys}" != "cygwin"; then
+    AC_MSG_ERROR([Using win32 with an autotools build is only supported for Cygwin.])
+  fi
+  AC_CHECK_HEADER([windows.h], [HAVE_W32=yes],
+                  [AC_MSG_ERROR([`--with-w32' was specified, but windows.h
+                  cannot be found.])])
+  AC_DEFINE(HAVE_NTGUI, 1, [Define to use native Windows GUI.])
+  W32_OBJ="w32fns.o w32menu.o w32reg.o w32font.o w32term.o"
+  W32_OBJ="$W32_OBJ w32xfns.o w32select.o w32uniscribe.o"
+  W32_LIBS="$W32_LIBS -lkernel32 -luser32 -lgdi32 -lole32 -lcomdlg32"
+  W32_LIBS="$W32_LIBS -lusp10 -lcomctl32 -lwinspool"
+fi
+AC_SUBST(W32_OBJ)
+AC_SUBST(W32_LIBS)
+
+if test "${HAVE_W32}" = "yes"; then
+  window_system=w32
+  with_xft=no
+fi
+
 ## $window_system is now set to the window system we will
 ## ultimately use.
 
@@ -1622,6 +1648,9 @@ dnl use the toolkit if we have gtk, or X11R5 or newer.
   nextstep )
     term_header=nsterm.h
   ;;
+  w32 )
+    term_header=w32term.h
+  ;;
 esac
 
 if test -n "${term_header}"; then
@@ -2278,6 +2307,9 @@ if test "${with_toolkit_scroll_bars}" != "no"; then
   elif test "${HAVE_NS}" = "yes"; then
     AC_DEFINE(USE_TOOLKIT_SCROLL_BARS)
     USE_TOOLKIT_SCROLL_BARS=yes
+  elif test "${HAVE_W32}" = "yes"; then
+    AC_DEFINE(USE_TOOLKIT_SCROLL_BARS)
+    USE_TOOLKIT_SCROLL_BARS=yes
   fi
 fi
 
@@ -2444,6 +2476,41 @@ AC_SUBST(M17N_FLT_LIBS)
 ### Use -lXpm if available, unless `--with-xpm=no'.
 HAVE_XPM=no
 LIBXPM=
+
+if test "${HAVE_W32}" = "yes"; then
+  if test "${with_xpm}" != "no"; then
+    SAVE_CPPFLAGS="$CPPFLAGS"
+    SAVE_LDFLAGS="$LDFLAGS"
+    CPPFLAGS="$CPPFLAGS -I/usr/include/noX"
+    LDFLAGS="$LDFLAGS -L/usr/lib/noX"
+    AC_CHECK_HEADER(X11/xpm.h,
+      [AC_CHECK_LIB(Xpm, XpmReadFileToImage, HAVE_XPM=yes)])
+    if test "${HAVE_XPM}" = "yes"; then
+      AC_MSG_CHECKING(for XpmReturnAllocPixels preprocessor define)
+      AC_EGREP_CPP(no_return_alloc_pixels,
+      [#include "X11/xpm.h"
+#ifndef XpmReturnAllocPixels
+no_return_alloc_pixels
+#endif
+      ], HAVE_XPM=no, HAVE_XPM=yes)
+
+      if test "${HAVE_XPM}" = "yes"; then
+        REAL_CPPFLAGS="$REAL_CPPFLAGS -I/usr/include/noX"
+	AC_MSG_RESULT(yes)
+      else
+	AC_MSG_RESULT(no)
+        CPPFLAGS="$SAVE_CPPFLAGS"
+        LDFLAGS="$SAVE_LDFLAGS"
+      fi
+    fi
+  fi
+
+  if test "${HAVE_XPM}" = "yes"; then
+    AC_DEFINE(HAVE_XPM, 1, [Define to 1 if you have the Xpm libary (-lXpm).])
+    LIBXPM=-lXpm
+  fi
+fi
+
 if test "${HAVE_X11}" = "yes"; then
   if test "${with_xpm}" != "no"; then
     AC_CHECK_HEADER(X11/xpm.h,
@@ -2470,12 +2537,13 @@ no_return_alloc_pixels
     LIBXPM=-lXpm
   fi
 fi
+
 AC_SUBST(LIBXPM)
 
 ### Use -ljpeg if available, unless `--with-jpeg=no'.
 HAVE_JPEG=no
 LIBJPEG=
-if test "${HAVE_X11}" = "yes"; then
+if test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
   if test "${with_jpeg}" != "no"; then
     dnl Checking for jpeglib.h can lose because of a redefinition of
     dnl  HAVE_STDLIB_H.
@@ -2503,7 +2571,7 @@ AC_SUBST(LIBJPEG)
 ### Use -lpng if available, unless `--with-png=no'.
 HAVE_PNG=no
 LIBPNG=
-if test "${HAVE_X11}" = "yes"; then
+if test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
   if test "${with_png}" != "no"; then
     # Debian unstable as of July 2003 has multiple libpngs, and puts png.h
     # in /usr/include/libpng.
@@ -2523,7 +2591,7 @@ AC_SUBST(LIBPNG)
 ### Use -ltiff if available, unless `--with-tiff=no'.
 HAVE_TIFF=no
 LIBTIFF=
-if test "${HAVE_X11}" = "yes"; then
+if test "${HAVE_X11}" = "yes" || test "${HAVE_W32}" = "yes"; then
   if test "${with_tiff}" != "no"; then
     AC_CHECK_HEADER(tiffio.h,
       [tifflibs="-lz -lm"
@@ -2543,7 +2611,8 @@ AC_SUBST(LIBTIFF)
 ### Use -lgif or -lungif if available, unless `--with-gif=no'.
 HAVE_GIF=no
 LIBGIF=
-if test "${HAVE_X11}" = "yes" && test "${with_gif}" != "no"; then
+if test "${HAVE_X11}" = "yes" && test "${with_gif}" != "no" \
+        || test "${HAVE_W32}" = "yes"; then
   AC_CHECK_HEADER(gif_lib.h,
 # EGifPutExtensionLast only exists from version libungif-4.1.0b1.
 # Earlier versions can crash Emacs.
@@ -2630,6 +2699,9 @@ if test "${HAVE_NS}" = "yes"; then
   OTHER_FILES=ns-app
 fi
 
+if test "${HAVE_W32}" = "yes"; then
+  HAVE_MENUS=yes
+fi
 
 ### Use session management (-lSM -lICE) if available
 HAVE_X_SM=no
@@ -3381,7 +3453,7 @@ fi
 AC_SUBST(RALLOC_OBJ)
 
 if test "$opsys" = "cygwin"; then
-  CYGWIN_OBJ="sheap.o"
+  CYGWIN_OBJ="sheap.o cygw32.o"
   ## Cygwin differs because of its unexec().
   PRE_ALLOC_OBJ=
   POST_ALLOC_OBJ=lastfile.o
diff --git a/lisp/battery.el b/lisp/battery.el
index bb899f2..0a86cd8 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -57,7 +57,7 @@
 			 (> (buffer-size) 0)))
 		(error nil)))
 	 'battery-pmset)
-	((eq system-type 'windows-nt)
+	((fboundp 'w32-battery-status)
 	 'w32-battery-status))
   "Function for getting battery status information.
 The function has to return an alist of conversion definitions.
diff --git a/lisp/faces.el b/lisp/faces.el
index 2119358..649b127 100644
--- a/lisp/faces.el
+++ b/lisp/faces.el
@@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
 ;; This is defined originally in xfaces.c.
 (defcustom face-font-registry-alternatives
   (mapcar (lambda (arg) (mapcar 'purecopy arg))
-  (if (eq system-type 'windows-nt)
+  (if (featurep 'w32)
       '(("iso8859-1" "ms-oemlatin")
 	("gb2312.1980" "gb2312" "gbk" "gb18030")
 	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
diff --git a/lisp/frame.el b/lisp/frame.el
index af668f8..36ea821 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -522,7 +522,7 @@ The optional argument PARAMETERS specifies additional frame parameters."
 	   (ns-initialize-window-system))
 	 (make-frame `((window-system . ns)
 		       (display . ,display) . ,parameters)))
-	((eq system-type 'windows-nt)
+	((eq window-system 'w32)
 	 ;; On Windows, ignore DISPLAY.
 	 (make-frame parameters))
 	(t
@@ -1206,7 +1206,7 @@ frame's display)."
     (cond
      ((eq frame-type 'pc)
       (msdos-mouse-p))
-     ((eq system-type 'windows-nt)
+     ((eq frame-type 'w32)
       (with-no-warnings
        (> w32-num-mouse-buttons 0)))
      ((memq frame-type '(x ns))
diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index 0d3f079..3062b6c 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -2655,7 +2655,8 @@ See also `locale-charset-language-names', `locale-language-names',
     ;; On Windows, override locale-coding-system,
     ;; default-file-name-coding-system, keyboard-coding-system,
     ;; terminal-coding-system with system codepage.
-    (when (boundp 'w32-ansi-code-page)
+    (when (and (eq system-type 'windows-nt)
+               (boundp 'w32-ansi-code-page))
       (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
 	(when (coding-system-p code-page-coding)
 	  (unless frame (setq locale-coding-system code-page-coding))
diff --git a/lisp/loadup.el b/lisp/loadup.el
index 792827d..c995073 100644
--- a/lisp/loadup.el
+++ b/lisp/loadup.el
@@ -207,15 +207,18 @@
       (load "term/common-win")
       (load "term/x-win")))
 
-(if (eq system-type 'windows-nt)
+(if (or (eq system-type 'windows-nt)
+        (featurep 'w32))
     (progn
-      (load "w32-vars")
       (load "term/common-win")
+      (load "w32-vars")
       (load "term/w32-win")
-      (load "ls-lisp")
       (load "disp-table")
-      (load "dos-w32")
-      (load "w32-fns")))
+      (load "w32-common-fns")
+      (when (eq system-type 'windows-nt)
+        (load "w32-fns")
+        (load "ls-lisp")
+        (load "dos-w32"))))
 (if (eq system-type 'ms-dos)
     (progn
       (load "dos-w32")
diff --git a/lisp/mouse.el b/lisp/mouse.el
index 615062d..ad2ae29 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -1147,7 +1147,7 @@ regardless of where you click."
   (or mouse-yank-at-point (mouse-set-point click))
   (let ((primary
 	 (cond
-	  ((eq system-type 'windows-nt)
+	  ((eq (framep (selected-frame)) 'w32)
 	   ;; MS-Windows emulates PRIMARY in x-get-selection, but not
 	   ;; in x-get-selection-value (the latter only accesses the
 	   ;; clipboard).  So try PRIMARY first, in case they selected
diff --git a/lisp/simple.el b/lisp/simple.el
index b7165c9..e53a0d2 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6654,7 +6654,7 @@ call `normal-erase-is-backspace-mode' (which see) instead."
        (if (if (eq normal-erase-is-backspace 'maybe)
                (and (not noninteractive)
                     (or (memq system-type '(ms-dos windows-nt))
-			(memq window-system '(ns))
+			(memq window-system '(w32 ns))
                         (and (memq window-system '(x))
                              (fboundp 'x-backspace-delete-keys-p)
                              (x-backspace-delete-keys-p))
diff --git a/lisp/term/common-win.el b/lisp/term/common-win.el
index 63c8840..2d88cf9 100644
--- a/lisp/term/common-win.el
+++ b/lisp/term/common-win.el
@@ -57,7 +57,7 @@ clipboard as well.
 
 On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
 is not used)."
-  (cond ((eq system-type 'windows-nt)
+  (cond ((eq (framep (selected-frame)) 'w32)
 	 (if x-select-enable-clipboard
 	     (w32-set-clipboard-data text))
 	 (setq x-last-selected-text text))
diff --git a/lisp/term/w32-win.el b/lisp/term/w32-win.el
index b7f2a69..8a6a644 100644
--- a/lisp/term/w32-win.el
+++ b/lisp/term/w32-win.el
@@ -1,4 +1,4 @@
-;;; w32-win.el --- parse switches controlling interface with W32 window system
+;;; w32-win.el --- parse switches controlling interface with W32 window system -*- lexical-binding: t -*-
 
 ;; Copyright (C) 1993-1994, 2001-2011  Free Software Foundation, Inc.
 
@@ -102,7 +102,22 @@
 ;;   (interactive "e")
 ;;   (princ event))
 
-(defun w32-drag-n-drop (event)
+(defun w32-handle-dropped-file (window file-name)
+  (let ((f (if (eq system-type 'cygwin)
+               (cygwin-convert-path-from-windows file-name t)
+             (subst-char-in-string ?\\ ?/ file-name)))
+        (coding (or file-name-coding-system
+                    default-file-name-coding-system)))
+
+    (setq file-name
+          (mapconcat 'url-hexify-string
+                     (split-string (encode-coding-string f coding)
+                                   "/")
+                     "/")))
+		(dnd-handle-one-url window 'private
+				    (concat "file:" file-name)))
+
+(defun w32-drag-n-drop (event &optional new-frame)
   "Edit the files listed in the drag-n-drop EVENT.
 Switch to a buffer editing the last file dropped."
   (interactive "e")
@@ -116,26 +131,21 @@ Switch to a buffer editing the last file dropped."
 	   (y (cdr coords)))
       (if (and (> x 0) (> y 0))
 	  (set-frame-selected-window nil window))
-      (mapc (lambda (file-name)
-		(let ((f (subst-char-in-string ?\\ ?/ file-name))
-		      (coding (or file-name-coding-system
-				  default-file-name-coding-system)))
-		  (setq file-name
-			(mapconcat 'url-hexify-string
-				   (split-string (encode-coding-string f coding)
-						 "/")
-				   "/")))
-		(dnd-handle-one-url window 'private
-				    (concat "file:" file-name)))
-		(car (cdr (cdr event)))))
-  (raise-frame)))
+
+      (when new-frame
+        (select-frame (make-frame)))
+      (raise-frame)
+      (setq window (selected-window))
+
+      (mapc (apply-partially #'w32-handle-dropped-file window)
+            (car (cdr (cdr event)))))))
 
 (defun w32-drag-n-drop-other-frame (event)
   "Edit the files listed in the drag-n-drop EVENT, in other frames.
 May create new frames, or reuse existing ones.  The frame editing
 the last file dropped is selected."
   (interactive "e")
-  (mapcar 'find-file-other-frame (car (cdr (cdr event)))))
+  (w32-drag-n-drop event t))
 
 ;; Bind the drag-n-drop event.
 (global-set-key [drag-n-drop] 'w32-drag-n-drop)
diff --git a/lisp/w32-common-fns.el b/lisp/w32-common-fns.el
new file mode 100644
index 0000000..7ee0ec7
--- /dev/null
+++ b/lisp/w32-common-fns.el
@@ -0,0 +1,130 @@
+;;; w32-common-fns.el --- Lisp routines for Windows and Cygwin-w32
+
+;; Copyright (C) 1994, 2001-2011  Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;;
+;;; This file contains functions that are used by both native NT Emacs
+;;; and Cygwin Emacs compiled to use the native Windows widget
+;;; library.
+
+(defun w32-version ()
+  "Return the MS-Windows version numbers.
+The value is a list of three integers: the major and minor version
+numbers, and the build number."
+  (x-server-version))
+
+(defun w32-using-nt ()
+  "Return non-nil if running on a Windows NT descendant.
+That includes all Windows systems except for 9X/Me."
+  (getenv "SystemRoot"))
+
+(declare-function w32-get-clipboard-data "w32select.c")
+(declare-function w32-set-clipboard-data "w32select.c")
+(declare-function x-server-version "w32fns.c" (&optional display))
+
+;;; Fix interface to (X-specific) mouse.el
+(defun x-set-selection (type data)
+  "Make an X selection of type TYPE and value DATA.
+The argument TYPE (nil means `PRIMARY') says which selection, and
+DATA specifies the contents.  TYPE must be a symbol.  \(It can also
+be a string, which stands for the symbol with that name, but this
+is considered obsolete.)  DATA may be a string, a symbol, an
+integer (or a cons of two integers or list of two integers).
+
+The selection may also be a cons of two markers pointing to the same buffer,
+or an overlay.  In these cases, the selection is considered to be the text
+between the markers *at whatever time the selection is examined*.
+Thus, editing done in the buffer after you specify the selection
+can alter the effective value of the selection.
+
+The data may also be a vector of valid non-vector selection values.
+
+The return value is DATA.
+
+Interactively, this command sets the primary selection.  Without
+prefix argument, it reads the selection in the minibuffer.  With
+prefix argument, it uses the text of the region as the selection value.
+
+Note that on MS-Windows, primary and secondary selections set by Emacs
+are not available to other programs."
+  (put 'x-selections (or type 'PRIMARY) data))
+
+(defun x-get-selection (&optional type _data-type)
+  "Return the value of an X Windows selection.
+The argument TYPE (default `PRIMARY') says which selection,
+and the argument DATA-TYPE (default `STRING') says
+how to convert the data.
+
+TYPE may be any symbol \(but nil stands for `PRIMARY').  However,
+only a few symbols are commonly used.  They conventionally have
+all upper-case names.  The most often used ones, in addition to
+`PRIMARY', are `SECONDARY' and `CLIPBOARD'.
+
+DATA-TYPE is usually `STRING', but can also be one of the symbols
+in `selection-converter-alist', which see."
+  (get 'x-selections (or type 'PRIMARY)))
+
+;; x-selection-owner-p is used in simple.el
+(defun x-selection-owner-p (&optional type)
+  (and (memq type '(nil PRIMARY SECONDARY))
+       (get 'x-selections (or type 'PRIMARY))))
+
+;; The "Windows" keys on newer keyboards bring up the Start menu
+;; whether you want it or not - make Emacs ignore these keystrokes
+;; rather than beep.
+(global-set-key [lwindow] 'ignore)
+(global-set-key [rwindow] 'ignore)
+
+(defvar w32-charset-info-alist)		; w32font.c
+
+\f
+;;;; Selections
+
+;; We keep track of the last text selected here, so we can check the
+;; current selection against it, and avoid passing back our own text
+;; from x-selection-value.
+(defvar x-last-selected-text nil)
+
+(defun x-get-selection-value ()
+  "Return the value of the current selection.
+Consult the selection.  Treat empty strings as if they were unset."
+  (if x-select-enable-clipboard
+      (let (text)
+	;; Don't die if x-get-selection signals an error.
+	(condition-case c
+	    (setq text (w32-get-clipboard-data))
+	  (error (message "w32-get-clipboard-data:%s" c)))
+	(if (string= text "") (setq text nil))
+	(cond
+	 ((not text) nil)
+	 ((eq text x-last-selected-text) nil)
+	 ((string= text x-last-selected-text)
+	  ;; Record the newer string, so subsequent calls can use the 'eq' test.
+	  (setq x-last-selected-text text)
+	  nil)
+	 (t
+	  (setq x-last-selected-text text))))))
+\f
+(defalias 'x-selection-value 'x-get-selection-value)
+
+;; Arrange for the kill and yank functions to set and check the clipboard.
+(setq interprogram-cut-function 'x-select-text)
+(setq interprogram-paste-function 'x-get-selection-value)
+
+(provide 'w32-common-fns)
diff --git a/lisp/w32-fns.el b/lisp/w32-fns.el
index 1f54b85..beac839 100644
--- a/lisp/w32-fns.el
+++ b/lisp/w32-fns.el
@@ -26,34 +26,20 @@
 
 ;;; Code:
 (require 'w32-vars)
+(require 'w32-common-fns)
 
 (defvar explicit-shell-file-name)
 
 ;;;; Function keys
 
 (declare-function set-message-beep "w32console.c")
-(declare-function w32-get-clipboard-data "w32select.c")
 (declare-function w32-get-locale-info "w32proc.c")
 (declare-function w32-get-valid-locale-ids "w32proc.c")
-(declare-function w32-set-clipboard-data "w32select.c")
 
 ;; Map all versions of a filename (8.3, longname, mixed case) to the
 ;; same buffer.
 (setq find-file-visit-truename t)
 
-(declare-function x-server-version "w32fns.c" (&optional display))
-
-(defun w32-version ()
-  "Return the MS-Windows version numbers.
-The value is a list of three integers: the major and minor version
-numbers, and the build number."
-  (x-server-version))
-
-(defun w32-using-nt ()
-  "Return non-nil if running on a Windows NT descendant.
-That includes all Windows systems except for 9X/Me."
-  (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
-
 (defun w32-shell-name ()
   "Return the name of the shell being used."
   (or (bound-and-true-p shell-file-name)
@@ -258,53 +244,6 @@ requires it (see `w32-shell-dos-semantics')."
 	  (setq start (match-end 0))))
       name)))
 
-;;; Fix interface to (X-specific) mouse.el
-(defun x-set-selection (type data)
-  "Make an X selection of type TYPE and value DATA.
-The argument TYPE (nil means `PRIMARY') says which selection, and
-DATA specifies the contents.  TYPE must be a symbol.  \(It can also
-be a string, which stands for the symbol with that name, but this
-is considered obsolete.)  DATA may be a string, a symbol, an
-integer (or a cons of two integers or list of two integers).
-
-The selection may also be a cons of two markers pointing to the same buffer,
-or an overlay.  In these cases, the selection is considered to be the text
-between the markers *at whatever time the selection is examined*.
-Thus, editing done in the buffer after you specify the selection
-can alter the effective value of the selection.
-
-The data may also be a vector of valid non-vector selection values.
-
-The return value is DATA.
-
-Interactively, this command sets the primary selection.  Without
-prefix argument, it reads the selection in the minibuffer.  With
-prefix argument, it uses the text of the region as the selection value.
-
-Note that on MS-Windows, primary and secondary selections set by Emacs
-are not available to other programs."
-  (put 'x-selections (or type 'PRIMARY) data))
-
-(defun x-get-selection (&optional type _data-type)
-  "Return the value of an X Windows selection.
-The argument TYPE (default `PRIMARY') says which selection,
-and the argument DATA-TYPE (default `STRING') says
-how to convert the data.
-
-TYPE may be any symbol \(but nil stands for `PRIMARY').  However,
-only a few symbols are commonly used.  They conventionally have
-all upper-case names.  The most often used ones, in addition to
-`PRIMARY', are `SECONDARY' and `CLIPBOARD'.
-
-DATA-TYPE is usually `STRING', but can also be one of the symbols
-in `selection-converter-alist', which see."
-  (get 'x-selections (or type 'PRIMARY)))
-
-;; x-selection-owner-p is used in simple.el
-(defun x-selection-owner-p (&optional type)
-  (and (memq type '(nil PRIMARY SECONDARY))
-       (get 'x-selections (or type 'PRIMARY))))
-
 (defun set-w32-system-coding-system (coding-system)
   "Set the coding system used by the Windows system to CODING-SYSTEM.
 This is used for things like passing font names with non-ASCII
@@ -329,14 +268,6 @@ This function is provided for backward compatibility, since
 ;; Set to a system sound if you want a fancy bell.
 (set-message-beep nil)
 
-;; The "Windows" keys on newer keyboards bring up the Start menu
-;; whether you want it or not - make Emacs ignore these keystrokes
-;; rather than beep.
-(global-set-key [lwindow] 'ignore)
-(global-set-key [rwindow] 'ignore)
-
-(defvar w32-charset-info-alist)		; w32font.c
-
 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
   "Function to add character sets to display with Windows fonts.
 Creates entries in `w32-charset-info-alist'.
@@ -398,40 +329,6 @@ bit output with no translation."
                         'w32-charset-info-alist "21.1")
 
 \f
-;;;; Selections
-
-;; We keep track of the last text selected here, so we can check the
-;; current selection against it, and avoid passing back our own text
-;; from x-selection-value.
-(defvar x-last-selected-text nil)
-
-(defun x-get-selection-value ()
-  "Return the value of the current selection.
-Consult the selection.  Treat empty strings as if they were unset."
-  (if x-select-enable-clipboard
-      (let (text)
-	;; Don't die if x-get-selection signals an error.
-	(condition-case c
-	    (setq text (w32-get-clipboard-data))
-	  (error (message "w32-get-clipboard-data:%s" c)))
-	(if (string= text "") (setq text nil))
-	(cond
-	 ((not text) nil)
-	 ((eq text x-last-selected-text) nil)
-	 ((string= text x-last-selected-text)
-	  ;; Record the newer string, so subsequent calls can use the 'eq' test.
-	  (setq x-last-selected-text text)
-	  nil)
-	 (t
-	  (setq x-last-selected-text text))))))
-\f
-(defalias 'x-selection-value 'x-get-selection-value)
-
-;; Arrange for the kill and yank functions to set and check the clipboard.
-(setq interprogram-cut-function 'x-select-text)
-(setq interprogram-paste-function 'x-get-selection-value)
-
-\f
 ;;;; Support for build process
 
 ;; From autoload.el
diff --git a/lisp/w32-vars.el b/lisp/w32-vars.el
index d1e8edc..2a50bb1 100644
--- a/lisp/w32-vars.el
+++ b/lisp/w32-vars.el
@@ -44,17 +44,19 @@ X does.  See `w32-fixed-font-alist' for the font menu definition."
   "Include proportional fonts in the default font dialog.")
 (make-obsolete-variable 'w32-list-proportional-fonts "no longer used." "23.1")
 
-(defcustom w32-allow-system-shell nil
-  "Disable startup warning when using \"system\" shells."
-  :type 'boolean
-  :group 'w32)
-
-(defcustom w32-system-shells '("cmd" "cmd.exe" "command" "command.com"
-			       "4nt" "4nt.exe" "4dos" "4dos.exe"
-			       "tcc" "tcc.exe" "ndos" "ndos.exe")
-  "List of strings recognized as Windows system shells."
-  :type '(repeat string)
-  :group 'w32)
+(unless (eq system-type 'cygwin)
+  (defcustom w32-allow-system-shell nil
+    "Disable startup warning when using \"system\" shells."
+    :type 'boolean
+    :group 'w32))
+
+(unless (eq system-type 'cygwin)
+ (defcustom w32-system-shells '("cmd" "cmd.exe" "command" "command.com"
+                                "4nt" "4nt.exe" "4dos" "4dos.exe"
+                                "tcc" "tcc.exe" "ndos" "ndos.exe")
+   "List of strings recognized as Windows system shells."
+   :type '(repeat string)
+   :group 'w32))
 
 ;; Want "menu" custom type for this.
 (defcustom w32-fixed-font-alist
diff --git a/src/Makefile.in b/src/Makefile.in
index 99517a2..a74a2b2 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -133,13 +133,10 @@ LIB_MATH=@LIB_MATH@
 ## -lpthreads, or empty.
 LIB_PTHREAD=@LIB_PTHREAD@
 
-LIBTIFF=@LIBTIFF@
-LIBJPEG=@LIBJPEG@
-LIBPNG=@LIBPNG@
-LIBGIF=@LIBGIF@
-LIBXPM=@LIBXPM@
+LIBIMAGE=@LIBTIFF@ @LIBJPEG@ @LIBPNG@ @LIBGIF@ @LIBXPM@
+
 XFT_LIBS=@XFT_LIBS@
-LIBX_EXTRA=$(LIBTIFF) $(LIBJPEG) $(LIBPNG) $(LIBGIF) $(LIBXPM) -lX11 $(XFT_LIBS)
+LIBX_EXTRA=-lX11 $(XFT_LIBS)
 
 FONTCONFIG_CFLAGS = @FONTCONFIG_CFLAGS@
 FONTCONFIG_LIBS = @FONTCONFIG_LIBS@
@@ -257,6 +254,13 @@ NS_OBJ=@NS_OBJ@
 NS_OBJC_OBJ=@NS_OBJC_OBJ@
 ## Only set if NS_IMPL_GNUSTEP.
 GNU_OBJC_CFLAGS=@GNU_OBJC_CFLAGS@
+## w32fns.o w32menu.c w32reg.o fringe.o fontset.o w32font.o w32term.o
+## w32xfns.o w32select.o image.o w32uniscribe.o if HAVE_W32, else
+## empty.
+W32_OBJ=@W32_OBJ@
+## -lkernel32 -luser32 -lgdi32 -lole32 -lcomdlg32 lusp10 -lcomctl32
+## --lwinspool if HAVE_W32, else empty.
+W32_LIBS=@W32_LIBS@
 
 ## Empty if !HAVE_X_WINDOWS
 ## xfont.o ftfont.o xftfont.o ftxfont.o if HAVE_XFT
@@ -343,7 +347,7 @@ base_obj = dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \
 	region-cache.o sound.o atimer.o \
 	doprnt.o intervals.o textprop.o composite.o xml.o \
 	$(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ) \
-	$(WINDOW_SYSTEM_OBJ)
+	$(W32_OBJ) $(WINDOW_SYSTEM_OBJ)
 obj = $(base_obj) $(NS_OBJC_OBJ)
 
 ## Object files used on some machine or other.
@@ -352,9 +356,9 @@ obj = $(base_obj) $(NS_OBJC_OBJ)
 ## in the list, in case they ever add any such entries.
 SOME_MACHINE_OBJECTS = dosfns.o msdos.o \
   xterm.o xfns.o xmenu.o xselect.o xrdb.o xsmfns.o fringe.o image.o \
-  fontset.o dbusbind.o \
+  fontset.o dbusbind.o cygw32.o \
   nsterm.o nsfns.o nsmenu.o nsselect.o nsimage.o nsfont.o \
-  w32.o w32console.o w32fns.o w32heap.o w32inevt.o \
+  w32.o w32console.o w32fns.o w32heap.o \
   w32menu.o w32proc.o w32reg.o w32select.o w32term.o w32xfns.o \
   w16select.o widget.o xfont.o ftfont.o xftfont.o ftxfont.o gtkutil.o \
   xsettings.o xgselect.o termcap.o
@@ -386,8 +390,8 @@ otherobj= $(TERMCAP_OBJ) $(PRE_ALLOC_OBJ) $(GMALLOC_OBJ) $(RALLOC_OBJ) \
 ## Note that SunOS needs -lm to come before -lc; otherwise, you get
 ## duplicated symbols.  If the standard libraries were compiled
 ## with GCC, we might need LIB_GCC again after them.
-LIBES = $(LIBS) $(LIBX_BASE) $(LIBX_OTHER) $(LIBSOUND) \
-   $(RSVG_LIBS) $(IMAGEMAGICK_LIBS) $(DBUS_LIBS) \
+LIBES = $(LIBS) $(W32_LIBS) $(LIBX_BASE) $(LIBIMAGE) $(LIBX_OTHER) \
+   $(LIBSOUND) $(RSVG_LIBS) $(IMAGEMAGICK_LIBS) $(DBUS_LIBS) \
    $(LIBXML2_LIBS) $(LIBGPM) $(LIBRESOLV) $(LIBS_SYSTEM) \
    $(LIBS_TERMCAP) $(GETLOADAVG_LIBS) $(SETTINGS_LIBS) $(LIBSELINUX_LIBS) \
    $(FREETYPE_LIBS) $(FONTCONFIG_LIBS) $(LIBOTF_LIBS) $(M17N_FLT_LIBS) \
diff --git a/src/cygw32.c b/src/cygw32.c
new file mode 100644
index 0000000..b168a40
--- /dev/null
+++ b/src/cygw32.c
@@ -0,0 +1,168 @@
+/* Cygwin support routines.
+   Copyright (C) 2011  Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
+
+
+#include "cygw32.h"
+#include "buffer.h"
+#include <unistd.h>
+#include <fcntl.h>
+static Lisp_Object Qutf_16_le;
+
+static Lisp_Object
+fchdir_unwind (Lisp_Object dir_fd)
+{
+  (void) fchdir (XFASTINT (dir_fd));
+  (void) close (XFASTINT (dir_fd));
+  return Qnil;
+}
+
+static void
+chdir_to_default_directory ()
+{
+  Lisp_Object new_cwd;
+  int old_cwd_fd = open (".", O_RDONLY | O_DIRECTORY);
+
+  if (old_cwd_fd == -1)
+    error ("could not open current directory: %s", strerror (errno));
+
+  record_unwind_protect (fchdir_unwind, make_number (old_cwd_fd));
+
+  new_cwd = Funhandled_file_name_directory (
+    Fexpand_file_name (build_string ("."), Qnil));
+  if (!STRINGP (new_cwd))
+    new_cwd = build_string ("/");
+
+  if (chdir (SDATA (ENCODE_FILE (new_cwd))))
+    error ("could not chdir: %s", strerror (errno));
+}
+
+static Lisp_Object
+conv_filename_to_w32_unicode (Lisp_Object in, int absolute_p)
+{
+  ssize_t converted_len;
+  Lisp_Object converted;
+  unsigned flags;
+  int count = SPECPDL_INDEX ();
+
+  chdir_to_default_directory ();
+
+  flags = CCP_POSIX_TO_WIN_W;
+  if (!absolute_p) {
+    flags |= CCP_RELATIVE;
+  }
+
+  in = ENCODE_FILE (in);
+
+  converted_len = cygwin_conv_path (flags, SDATA (in), NULL, 0);
+  if (converted_len < 2)
+    error ("cygwin_conv_path: %s", strerror (errno));
+
+  converted = make_uninit_string (converted_len - 1);
+  if (cygwin_conv_path (flags, SDATA (in),
+                        SDATA (converted), converted_len))
+    error ("cygwin_conv_path: %s", strerror (errno));
+
+  return unbind_to (count, converted);
+}
+
+static Lisp_Object
+conv_filename_from_w32_unicode (const wchar_t* in, int absolute_p)
+{
+  ssize_t converted_len;
+  Lisp_Object converted;
+  unsigned flags;
+  int count = SPECPDL_INDEX ();
+
+  chdir_to_default_directory ();
+
+  flags = CCP_WIN_W_TO_POSIX;
+  if (!absolute_p) {
+    flags |= CCP_RELATIVE;
+  }
+
+  converted_len = cygwin_conv_path (flags, in, NULL, 0);
+  if (converted_len < 1)
+    error ("cygwin_conv_path: %s", strerror (errno));
+
+  converted = make_uninit_string (converted_len - 1 /*subtract terminator*/);
+  if (cygwin_conv_path (flags, in, SDATA (converted), converted_len))
+    error ("cygwin_conv_path: %s", strerror (errno));
+
+  return unbind_to (count, DECODE_FILE (converted));
+}
+
+Lisp_Object
+from_unicode (Lisp_Object str)
+{
+  CHECK_STRING (str);
+  if (!STRING_MULTIBYTE (str) &&
+      SBYTES (str) & 1)
+    {
+      str = Fsubstring (str, make_number (0), make_number (-1));
+    }
+
+  return code_convert_string_norecord (str, Qutf_16_le, 0);
+}
+
+wchar_t*
+to_unicode (Lisp_Object str, Lisp_Object* buf)
+{
+  *buf = code_convert_string_norecord (str, Qutf_16_le, 1);
+  /* We need to make a another copy (in addition to the one made by
+     code_convert_string_norecord) to ensure that the final string is
+     _doubly_ zero terminated --- that is, that the string is
+     terminated by two zero bytes and one utf-16le null character.
+     Because strings are already terminated with a single zero byte,
+     we just add one additional zero. */
+  str = make_uninit_string (SBYTES (*buf) + 1);
+  memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf));
+  SDATA (str) [SBYTES (*buf)] = '\0';
+  *buf = str;
+  return WCSDATA (*buf);
+}
+
+DEFUN ("cygwin-convert-path-to-windows",
+       Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
+       1, 2, 0,
+       doc: /* Convert PATH to a Windows path.  If ABSOLUTE-P if
+               non-nil, return an absolute path.*/)
+  (Lisp_Object path, Lisp_Object absolute_p)
+{
+  return from_unicode (
+    conv_filename_to_w32_unicode (path, absolute_p == Qnil ? 0 : 1));
+}
+
+DEFUN ("cygwin-convert-path-from-windows",
+       Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
+       1, 2, 0,
+       doc: /* Convert a Windows path to a Cygwin path.  If ABSOLUTE-P
+               if non-nil, return an absolute path.*/)
+  (Lisp_Object path, Lisp_Object absolute_p)
+{
+  return conv_filename_from_w32_unicode (to_unicode (path, &path),
+                                         absolute_p == Qnil ? 0 : 1);
+}
+
+void
+syms_of_cygw32 (void)
+{
+  /* No, not utf-16-le: that one has a BOM.  */
+  DEFSYM (Qutf_16_le, "utf-16le");
+  defsubr (&Scygwin_convert_path_from_windows);
+  defsubr (&Scygwin_convert_path_to_windows);
+}
diff --git a/src/cygw32.h b/src/cygw32.h
new file mode 100644
index 0000000..6fae131
--- /dev/null
+++ b/src/cygw32.h
@@ -0,0 +1,59 @@
+/* Header for Cygwin support routines.
+   Copyright (C) 2011  Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef CYGW32_H
+#define CYGW32_H
+#include <config.h>
+#include <windef.h>
+#include <sys/cygwin.h>
+#include <wchar.h>
+
+#include <signal.h>
+#include <stdio.h>
+#include <limits.h>
+#include <errno.h>
+#include <math.h>
+#include <setjmp.h>
+
+#include "lisp.h"
+#include "coding.h"
+
+/* *** Character conversion *** */
+
+/* Access the wide-character string stored in a Lisp string object.  */
+#define WCSDATA(x) ((wchar_t*) SDATA (x))
+
+/* Convert the Emacs string in STR to UTF-16LE and store a new string
+   containing the encoded version of STR into *BUF.  BUF may safely
+   point to STR on entry.  */
+extern wchar_t* to_unicode (Lisp_Object str, Lisp_Object* buf);
+
+/* Convert STR, a UTF-16LE encoded string embedded in an Emacs string
+   object, to a normal Emacs string and return it.  */
+extern Lisp_Object from_unicode (Lisp_Object str);
+
+/* *** Path conversion. *** */
+
+EXFUN (Fcygwin_convert_path_to_windows, 2);
+EXFUN (Fcygwin_convert_path_from_windows, 2);
+
+/* *** Misc *** */
+extern void syms_of_cygw32 (void);
+extern char * w32_strerror (int error_no);
+
+#endif /* CYGW32_H */
diff --git a/src/dispextern.h b/src/dispextern.h
index 5228a71..024905f 100644
--- a/src/dispextern.h
+++ b/src/dispextern.h
@@ -3122,7 +3122,7 @@ int draw_window_fringes (struct window *, int);
 int update_window_fringes (struct window *, int);
 void compute_fringe_widths (struct frame *, int);
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 void w32_init_fringe (struct redisplay_interface *);
 void w32_reset_fringes (void);
 #endif
@@ -3225,7 +3225,7 @@ extern char unspecified_fg[], unspecified_bg[];
 #ifdef HAVE_X_WINDOWS
 void gamma_correct (struct frame *, XColor *);
 #endif
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 void gamma_correct (struct frame *, COLORREF *);
 #endif
 
diff --git a/src/emacs.c b/src/emacs.c
index 92b1889..9709103 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -37,9 +37,17 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #ifdef WINDOWSNT
 #include <fcntl.h>
-#include <windows.h> /* just for w32.h */
 #include "w32.h"
-#include "w32heap.h" /* for prototype of sbrk */
+#endif
+
+#if defined (WINDOWSNT) || defined (HAVE_NTGUI)
+#include "w32heap.h"
+#include "w32select.h"
+#include "w32font.h"
+#endif
+
+#if defined (HAVE_NTGUI) && defined (CYGWIN)
+#include "cygw32.h"
 #endif
 
 #ifdef NS_IMPL_GNUSTEP
@@ -1419,6 +1427,11 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
     tzset ();
 #endif /* MSDOS */
 
+#if WINDOWSNT || HAVE_NTGUI
+  hinst = GetModuleHandle(NULL);
+  cache_system_info ();
+#endif
+
 #ifdef WINDOWSNT
   globals_of_w32 ();
   /* Initialize environment from registry settings.  */
@@ -1521,6 +1534,9 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
 #ifdef WINDOWSNT
       syms_of_ntproc ();
 #endif /* WINDOWSNT */
+#if defined (CYGWIN) && defined (HAVE_NTGUI)
+      syms_of_cygw32 ();
+#endif /* defined(CYGWIN) && defined (HAVE_NTGUI) */
       syms_of_window ();
       syms_of_xdisp ();
       syms_of_font ();
@@ -1551,11 +1567,14 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
 #ifdef HAVE_NTGUI
       syms_of_w32term ();
       syms_of_w32fns ();
-      syms_of_w32select ();
       syms_of_w32menu ();
       syms_of_fontset ();
 #endif /* HAVE_NTGUI */
 
+#ifdef HAVE_W32SELECT
+      syms_of_w32select ();
+#endif /* HAVE_W32SELECT */
+
 #ifdef MSDOS
       syms_of_xmenu ();
       syms_of_dosfns ();
@@ -1598,8 +1617,11 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
       globals_of_w32font ();
       globals_of_w32fns ();
       globals_of_w32menu ();
-      globals_of_w32select ();
 #endif  /* HAVE_NTGUI */
+
+#ifdef HAVE_W32SELECT
+      globals_of_w32select ();
+#endif /* HAVE_W32SELECT */
     }
 
   init_charset ();
diff --git a/src/font.c b/src/font.c
index 1081b9a..5432da1 100644
--- a/src/font.c
+++ b/src/font.c
@@ -5191,9 +5191,9 @@ EMACS_FONT_LOG is set.  Otherwise, it is set to t.  */);
 #ifdef HAVE_BDFFONT
   syms_of_bdffont ();
 #endif	/* HAVE_BDFFONT */
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   syms_of_w32font ();
-#endif	/* WINDOWSNT */
+#endif	/* HAVE_NTGUI */
 #ifdef HAVE_NS
   syms_of_nsfont ();
 #endif	/* HAVE_NS */
diff --git a/src/font.h b/src/font.h
index b6c1acf..1c20cbe 100644
--- a/src/font.h
+++ b/src/font.h
@@ -830,11 +830,11 @@ extern struct font_driver ftxfont_driver;
 extern void syms_of_bdffont (void);
 #endif	/* HAVE_BDFFONT */
 #endif	/* HAVE_X_WINDOWS */
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 extern struct font_driver w32font_driver;
 extern struct font_driver uniscribe_font_driver;
 extern void syms_of_w32font (void);
-#endif	/* WINDOWSNT */
+#endif	/* HAVE_NTGUI */
 #ifdef HAVE_NS
 extern Lisp_Object Qfontsize;
 extern struct font_driver nsfont_driver;
diff --git a/src/fontset.c b/src/fontset.c
index 281ac92..114432e 100644
--- a/src/fontset.c
+++ b/src/fontset.c
@@ -45,7 +45,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
 #endif
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 #include "w32term.h"
 #endif
 #ifdef HAVE_NS
diff --git a/src/frame.c b/src/frame.c
index 4650b6b..1fb2686 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -2719,7 +2719,7 @@ static const struct frame_parm_table frame_parms[] =
   {"tool-bar-position",		&Qtool_bar_position},
 };
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 
 /* Calculate fullscreen size.  Return in *TOP_POS and *LEFT_POS the
    wanted positions of the WM window (not Emacs window).
@@ -2763,7 +2763,7 @@ x_fullscreen_adjust (struct frame *f, int *width, int *height, int *top_pos, int
   *height = newheight;
 }
 
-#endif /* WINDOWSNT */
+#endif /* HAVE_NTGUI */
 
 #ifdef HAVE_WINDOW_SYSTEM
 
diff --git a/src/frame.h b/src/frame.h
index f068ff4..9e58435 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -1092,7 +1092,7 @@ extern Lisp_Object x_new_font (struct frame *, Lisp_Object, int);
 
 extern Lisp_Object Qface_set_after_frame_default;
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 extern void x_fullscreen_adjust (struct frame *f, int *, int *,
                                  int *, int *);
 #endif
@@ -1164,6 +1164,7 @@ extern char *x_get_resource_string (const char *, const char *);
 #endif
 
 extern void x_query_colors (struct frame *f, XColor *, int);
+extern void x_query_color (struct frame *f, XColor *);
 
 /* In xmenu.c */
 extern void set_frame_menubar (FRAME_PTR, int, int);
diff --git a/src/image.c b/src/image.c
index 20879f0..f492987 100644
--- a/src/image.c
+++ b/src/image.c
@@ -567,12 +567,14 @@ static void x_laplace (struct frame *, struct image *);
 static void x_emboss (struct frame *, struct image *);
 static int x_build_heuristic_mask (struct frame *, struct image *,
                                    Lisp_Object);
-#ifdef HAVE_NTGUI
+#if WINDOWSNT
+extern Lisp_Object Vlibrary_cache;
+
 #define CACHE_IMAGE_TYPE(type, status) \
   do { Vlibrary_cache = Fcons (Fcons (type, status), Vlibrary_cache); } while (0)
 #else
 #define CACHE_IMAGE_TYPE(type, status)
-#endif
+#endif /* WINDOWSNT */
 
 #define ADD_IMAGE_TYPE(type) \
   do { Vimage_types = Fcons (type, Vimage_types); } while (0)
@@ -1867,7 +1869,7 @@ mark_image_cache (struct image_cache *c)
 			  X / NS / W32 support code
  ***********************************************************************/
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* Macro for defining functions that will be loaded from image DLLs.  */
 #define DEF_IMGLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
@@ -1878,7 +1880,7 @@ mark_image_cache (struct image_cache *c)
     if (!fn_##func) return 0;						\
   }
 
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 static int x_create_x_image_and_pixmap (struct frame *, int, int, int,
                                         XImagePtr *, Pixmap *);
@@ -2941,7 +2943,7 @@ xbm_load (struct frame *f, struct image *img)
 	  else
 	    bits = (char *) XBOOL_VECTOR (data)->data;
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
           {
             char *invertedBits;
             int nbytes, i;
@@ -3250,7 +3252,7 @@ xpm_free_colors (Display *dpy, Colormap cmap, Pixel *pixels, int npixels, void *
 #endif /* ALLOC_XPM_COLORS */
 
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* XPM library details.  */
 
@@ -3276,8 +3278,15 @@ init_xpm_functions (Lisp_Object libraries)
   return 1;
 }
 
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
+#ifdef HAVE_NTGUI
+/* Glue for code below */
+#define fn_XpmReadFileToImage XpmReadFileToImage
+#define fn_XpmCreateImageFromBuffer XpmCreateImageFromBuffer
+#define fn_XImageFree XImageFree
+#define fn_XpmFreeAttributes XpmFreeAttributes
+#endif /* HAVE_NTGUI */
 
 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
    for XPM images.  Such a list must consist of conses whose car and
@@ -5427,7 +5436,7 @@ png_image_p (Lisp_Object object)
 
 #ifdef HAVE_PNG
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 /* PNG library details.  */
 
 DEF_IMGLIB_FN (png_voidp, png_get_io_ptr, (png_structp));
@@ -5527,7 +5536,7 @@ init_png_functions (Lisp_Object libraries)
 #define fn_png_set_longjmp_fn		png_set_longjmp_fn
 #endif /* libpng version >= 1.5 */
 
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 
 #if (PNG_LIBPNG_VER < 10500)
@@ -6059,14 +6068,20 @@ jpeg_image_p (Lisp_Object object)
 #define __WIN32__ 1
 #endif
 
+/* Work around conflict between jpeg boolean and rpcndr.h
+   under Windows. */
+#define boolean jpeg_boolean
 #include <jpeglib.h>
 #include <jerror.h>
 
+/* Don't undefine boolean --- use the JPEG boolean
+   through the rest of the file. */
+
 #ifdef HAVE_STLIB_H_1
 #define HAVE_STDLIB_H 1
 #endif
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* JPEG library details.  */
 DEF_IMGLIB_FN (void, jpeg_CreateDecompress, (j_decompress_ptr, int, size_t));
@@ -6116,7 +6131,7 @@ jpeg_resync_to_restart_wrapper (j_decompress_ptr cinfo, int desired)
 #define fn_jpeg_std_error		jpeg_std_error
 #define jpeg_resync_to_restart_wrapper	jpeg_resync_to_restart
 
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 struct my_jpeg_error_mgr
 {
@@ -6599,7 +6614,7 @@ tiff_image_p (Lisp_Object object)
 
 #include <tiffio.h>
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* TIFF library details.  */
 DEF_IMGLIB_FN (TIFFErrorHandler, TIFFSetErrorHandler, (TIFFErrorHandler));
@@ -6643,7 +6658,7 @@ init_tiff_functions (Lisp_Object libraries)
 #define fn_TIFFReadRGBAImage		TIFFReadRGBAImage
 #define fn_TIFFClose			TIFFClose
 #define fn_TIFFSetDirectory		TIFFSetDirectory
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 
 /* Reading from a memory buffer for TIFF images Based on the PNG
@@ -7072,7 +7087,7 @@ gif_image_p (Lisp_Object object)
 #endif /* HAVE_NTGUI */
 
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* GIF library details.  */
 DEF_IMGLIB_FN (int, DGifCloseFile, (GifFileType *));
@@ -7102,7 +7117,7 @@ init_gif_functions (Lisp_Object libraries)
 #define fn_DGifOpen		DGifOpen
 #define fn_DGifOpenFileName	DGifOpenFileName
 
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 /* Reading a GIF image from memory
    Based on the PNG memory stuff to a certain extent. */
@@ -8092,7 +8107,7 @@ svg_image_p (Lisp_Object object)
 
 #include <librsvg/rsvg.h>
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 
 /* SVG library functions.  */
 DEF_IMGLIB_FN (RsvgHandle *, rsvg_handle_new);
@@ -8170,7 +8185,7 @@ init_svg_functions (Lisp_Object libraries)
 #define fn_g_type_init                    g_type_init
 #define fn_g_object_unref                 g_object_unref
 #define fn_g_error_free                   g_error_free
-#endif /* !HAVE_NTGUI  */
+#endif /* !WINDOWSNT  */
 
 /* Load SVG image IMG for use on frame F.  Value is non-zero if
    successful. this function will go into the svg_type structure, and
@@ -8718,7 +8733,7 @@ DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
 			    Initialization
  ***********************************************************************/
 
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
 /* Image types that rely on external libraries are loaded dynamically
    if the library is available.  */
 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
@@ -8726,7 +8741,7 @@ DEFUN ("lookup-image", Flookup_image, Slookup_image, 1, 1, 0, "")
 #else
 #define CHECK_LIB_AVAILABLE(image_type, init_lib_fn, libraries) \
   define_image_type (image_type, 1)
-#endif /* HAVE_NTGUI */
+#endif /* WINDOWSNT */
 
 DEFUN ("init-image-library", Finit_image_library, Sinit_image_library, 2, 2, 0,
        doc: /* Initialize image library implementing image type TYPE.
@@ -8737,7 +8752,7 @@ Libraries to load are specified in alist LIBRARIES (usually, the value
 of `dynamic-library-alist', which see).  */)
   (Lisp_Object type, Lisp_Object libraries)
 {
-#ifdef HAVE_NTGUI
+#ifdef WINDOWSNT
   /* Don't try to reload the library.  */
   Lisp_Object tested = Fassq (type, Vlibrary_cache);
   if (CONSP (tested))
diff --git a/src/keyboard.c b/src/keyboard.c
index dc0fe48..8ad5ca3 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -313,7 +313,7 @@ static Lisp_Object Qmouse_fixup_help_message;
 /* Symbols to denote kinds of events.  */
 static Lisp_Object Qfunction_key;
 Lisp_Object Qmouse_click;
-#if defined (WINDOWSNT)
+#if defined (HAVE_NTGUI)
 Lisp_Object Qlanguage_change;
 #endif
 static Lisp_Object Qdrag_n_drop;
@@ -3768,8 +3768,8 @@ kbd_buffer_get_event (KBOARD **kbp,
 #ifdef subprocesses
   if (kbd_on_hold_p () && kbd_buffer_nr_stored () < KBD_BUFFER_SIZE/4)
     {
-      /* Start reading input again, we have processed enough so we can
-         accept new events again.  */
+      /* Start reading input again because we have processed enough to
+         be able to accept new events again.  */
       unhold_keyboard_input ();
 #ifdef SIGIO
       if (!noninteractive)
@@ -3953,7 +3953,7 @@ kbd_buffer_get_event (KBOARD **kbp,
 	    x_activate_menubar (XFRAME (event->frame_or_window));
 	}
 #endif
-#if defined (WINDOWSNT)
+#if defined (HAVE_NTGUI)
       else if (event->kind == LANGUAGE_CHANGE_EVENT)
 	{
 	  /* Make an event (language-change (FRAME CHARSET LCID)).  */
@@ -5412,7 +5412,7 @@ make_lispy_event (struct input_event *event)
 				  (sizeof (lispy_function_keys)
 				   / sizeof (lispy_function_keys[0])));
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
     case MULTIMEDIA_KEY_EVENT:
       if (event->code < (sizeof (lispy_multimedia_keys)
                          / sizeof (lispy_multimedia_keys[0]))
@@ -11562,7 +11562,7 @@ syms_of_keyboard (void)
   DEFSYM (Qconfig_changed_event, "config-changed-event");
   DEFSYM (Qmenu_enable, "menu-enable");
 
-#if defined (WINDOWSNT)
+#if defined (HAVE_NTGUI)
   DEFSYM (Qlanguage_change, "language-change");
 #endif
 
diff --git a/src/keyboard.h b/src/keyboard.h
index 7011bf0..de27805 100644
--- a/src/keyboard.h
+++ b/src/keyboard.h
@@ -511,6 +511,6 @@ extern int tty_read_avail_input (struct terminal *, int,
 extern EMACS_TIME timer_check (void);
 extern void mark_kboards (void);
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 extern const char *const lispy_function_keys[];
 #endif
diff --git a/src/menu.c b/src/menu.c
index 9d0e645..3e1fec9 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -41,7 +41,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #endif /* HAVE_WINDOW_SYSTEM */
 
 #ifdef HAVE_NTGUI
+# ifdef NTGUI_UNICODE
+# define unicode_append_menu AppendMenuW
+# else /* !NTGUI_UNICODE */
 extern AppendMenuW_Proc unicode_append_menu;
+# endif /* NTGUI_UNICODE */
 extern HMENU current_popup_menu;
 #endif /* HAVE_NTGUI  */
 
diff --git a/src/process.c b/src/process.c
index 2116027..5c8eef7 100644
--- a/src/process.c
+++ b/src/process.c
@@ -4605,6 +4605,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
 	      process_output_skip = 0;
 	    }
 #endif
+
 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
           nfds = xg_select
 #elif defined (HAVE_NS)
diff --git a/src/s/cygwin.h b/src/s/cygwin.h
index e025e46..35abbc3 100644
--- a/src/s/cygwin.h
+++ b/src/s/cygwin.h
@@ -95,3 +95,14 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Send signals to subprocesses by "typing" special chars at them.  */
 #define SIGNALS_VIA_CHARACTERS
+
+#ifdef EMACSDEBUG
+extern void _DebPrint (const char *fmt, ...);
+#define DebPrint(stuff) _DebPrint stuff
+#else
+#define DebPrint(stuff)
+#endif
+
+/* Cygwin runs only on Win2k or better, and never 9x.  */
+#define NTGUI_UNICODE 1
+#define _WIN32_WINNT 0x500 /* Win2k */
diff --git a/src/termhooks.h b/src/termhooks.h
index 5bd081d..1fa2bb8 100644
--- a/src/termhooks.h
+++ b/src/termhooks.h
@@ -102,9 +102,9 @@ enum event_kind
   HORIZ_WHEEL_EVENT,            /* A wheel event generated by a second
                                    horizontal wheel that is present on some
                                    mice. See WHEEL_EVENT.  */
-#if defined (WINDOWSNT)
+#if defined (HAVE_NTGUI)
   LANGUAGE_CHANGE_EVENT,	/* A LANGUAGE_CHANGE_EVENT is
-				   generated on WINDOWSNT or Mac OS
+				   generated on HAVE_NTGUI or Mac OS
 				   when the keyboard layout or input
 				   language is changed by the
 				   user.  */
@@ -183,7 +183,7 @@ enum event_kind
 
   , CONFIG_CHANGED_EVENT
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   /* Generated when an APPCOMMAND event is received, in response to
      Multimedia or Internet buttons on some keyboards.
      Such keys are available as normal function keys on X through the
diff --git a/src/w32.c b/src/w32.c
index b263274..6e52392 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -31,7 +31,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/utime.h>
-#include <mbstring.h>	/* for _mbspbrk */
 #include <math.h>
 #include <setjmp.h>
 #include <time.h>
@@ -39,6 +38,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 /* must include CRT headers *before* config.h */
 
 #include <config.h>
+#include <mbstring.h>	/* for _mbspbrk */
 
 #undef access
 #undef chdir
@@ -740,23 +740,6 @@ get_system_times (LPFILETIME lpIdleTime,
   return (s_pfn_Get_System_times (lpIdleTime, lpKernelTime, lpUserTime));
 }
 \f
-/* Equivalent of strerror for W32 error codes.  */
-char *
-w32_strerror (int error_no)
-{
-  static char buf[500];
-
-  if (error_no == 0)
-    error_no = GetLastError ();
-
-  buf[0] = '\0';
-  if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL,
-		      error_no,
-		      0, /* choose most suitable language */
-		      buf, sizeof (buf), NULL))
-    sprintf (buf, "w32 error %u", error_no);
-  return buf;
-}
 
 /* Return 1 if P is a valid pointer to an object of size SIZE.  Return
    0 if P is NOT a valid pointer.  Return -1 if we cannot validate P.
diff --git a/src/w32.h b/src/w32.h
index a3ca113..ace553a 100644
--- a/src/w32.h
+++ b/src/w32.h
@@ -130,16 +130,6 @@ extern LPBYTE w32_get_resource (char * key, LPDWORD type);
 extern void init_ntproc (void);
 extern void term_ntproc (void);
 extern void globals_of_w32 (void);
-extern void syms_of_w32term (void);
-extern void syms_of_w32fns (void);
-extern void globals_of_w32fns (void);
-extern void syms_of_w32select (void);
-extern void globals_of_w32select (void);
-extern void term_w32select (void);
-extern void syms_of_w32menu (void);
-extern void globals_of_w32menu (void);
-extern void syms_of_fontset (void);
-extern void syms_of_w32font (void);
 
 extern int _sys_read_ahead (int fd);
 extern int _sys_wait_accept (int fd);
diff --git a/src/w32console.c b/src/w32console.c
index 30c71f1..4cd79ec 100644
--- a/src/w32console.c
+++ b/src/w32console.c
@@ -350,53 +350,6 @@ w32con_delete_glyphs (struct frame *f, int n)
   scroll_line (f, n, LEFT);
 }
 
-static unsigned int sound_type = 0xFFFFFFFF;
-#define MB_EMACS_SILENT (0xFFFFFFFF - 1)
-
-void
-w32_sys_ring_bell (struct frame *f)
-{
-  if (sound_type == 0xFFFFFFFF)
-    {
-      Beep (666, 100);
-    }
-  else if (sound_type == MB_EMACS_SILENT)
-    {
-      /* Do nothing.  */
-    }
-  else
-    MessageBeep (sound_type);
-}
-
-DEFUN ("set-message-beep", Fset_message_beep, Sset_message_beep, 1, 1, 0,
-       doc: /* Set the sound generated when the bell is rung.
-SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
-to use the corresponding system sound for the bell.  The 'silent sound
-prevents Emacs from making any sound at all.
-SOUND is nil to use the normal beep.  */)
-  (Lisp_Object sound)
-{
-  CHECK_SYMBOL (sound);
-
-  if (NILP (sound))
-      sound_type = 0xFFFFFFFF;
-  else if (EQ (sound, intern ("asterisk")))
-      sound_type = MB_ICONASTERISK;
-  else if (EQ (sound, intern ("exclamation")))
-      sound_type = MB_ICONEXCLAMATION;
-  else if (EQ (sound, intern ("hand")))
-      sound_type = MB_ICONHAND;
-  else if (EQ (sound, intern ("question")))
-      sound_type = MB_ICONQUESTION;
-  else if (EQ (sound, intern ("ok")))
-      sound_type = MB_OK;
-  else if (EQ (sound, intern ("silent")))
-      sound_type = MB_EMACS_SILENT;
-  else
-      sound_type = 0xFFFFFFFF;
-
-  return sound;
-}
 
 static void
 w32con_reset_terminal_modes (struct terminal *t)
@@ -755,5 +708,4 @@ scroll-back buffer.  */);
   defsubr (&Sset_screen_color);
   defsubr (&Sget_screen_color);
   defsubr (&Sset_cursor_size);
-  defsubr (&Sset_message_beep);
 }
diff --git a/src/w32fns.c b/src/w32fns.c
index 4b94f16..45336ec 100644
--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -45,8 +45,14 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "fontset.h"
 #include "systime.h"
 #include "termhooks.h"
+
 #include "w32heap.h"
+
+#if CYGWIN
+#include "cygw32.h"
+#else
 #include "w32.h"
+#endif
 
 #include "bitmaps/gray.xbm"
 
@@ -59,9 +65,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <dlgs.h>
 #include <imm.h>
-#define FILE_NAME_TEXT_FIELD edt1
-#define FILE_NAME_COMBO_BOX cmb13
-#define FILE_NAME_LIST lst1
 
 #include "font.h"
 #include "w32font.h"
@@ -79,6 +82,7 @@ extern int w32_console_toggle_lock_key (int, Lisp_Object);
 extern void w32_menu_display_help (HWND, HMENU, UINT, UINT);
 extern void w32_free_menu_strings (HWND);
 extern const char *map_w32_filename (const char *, const char **);
+extern char * w32_strerror (int error_no);
 
 /* If non-zero, a w32 timer that, when it expires, displays an
    hourglass cursor on all frames.  */
@@ -165,7 +169,11 @@ ImmSetCompositionWindow_Proc set_ime_composition_window_fn = NULL;
 MonitorFromPoint_Proc monitor_from_point_fn = NULL;
 GetMonitorInfo_Proc get_monitor_info_fn = NULL;
 
+#ifdef NTGUI_UNICODE
+#define unicode_append_menu AppendMenuW
+#else /* !NTGUI_UNICODE */
 extern AppendMenuW_Proc unicode_append_menu;
+#endif /* NTGUI_UNICODE */
 
 /* Flag to selectively ignore WM_IME_CHAR messages.  */
 static int ignore_ime_char = 0;
@@ -201,6 +209,33 @@ extern int uniscribe_available;
 static void w32_show_hourglass (struct frame *);
 static void w32_hide_hourglass (void);
 
+#ifdef WINDOWSNT
+/* From w32inevet.c */
+extern int faked_key;
+#endif /* WINDOWSNT */
+
+/* This gives us the page size and the size of the allocation unit on NT.  */
+SYSTEM_INFO sysinfo_cache;
+
+/* This gives us version, build, and platform identification.  */
+OSVERSIONINFO osinfo_cache;
+
+unsigned long syspage_mask = 0;
+
+/* The major and minor versions of NT.  */
+int w32_major_version;
+int w32_minor_version;
+int w32_build_number;
+
+/* Distinguish between Windows NT and Windows 95.  */
+int os_subtype;
+
+#ifdef HAVE_NTGUI
+HINSTANCE hinst = NULL;
+#endif
+
+static unsigned int sound_type = 0xFFFFFFFF;
+#define MB_EMACS_SILENT (0xFFFFFFFF - 1)
 
 \f
 /* Error if we are not connected to MS-Windows.  */
@@ -1072,12 +1107,20 @@ gamma_correct (struct frame *f, COLORREF *color)
    If ALLOC is nonzero, allocate a new colormap cell.  */
 
 int
-w32_defined_color (FRAME_PTR f, char *color, XColor *color_def, int alloc)
+w32_defined_color (FRAME_PTR f, const char *color, XColor *color_def, int alloc)
 {
   register Lisp_Object tem;
   COLORREF w32_color_ref;
+  char* color_tem;
+  USE_SAFE_ALLOCA;
+
+  /* Allocate a temporary copy of COLOR because x_to_w32_color
+   * modifies it and we promised our caller we wouldn't. */
 
-  tem = x_to_w32_color (color);
+  SAFE_ALLOCA (color_tem, char*, strlen (color));
+  strcpy (color_tem, color);
+  tem = x_to_w32_color (color_tem);
+  SAFE_FREE ();
 
   if (!NILP (tem))
     {
@@ -1834,10 +1877,7 @@ w32_createwindow (struct frame *f)
 
   /* Do first time app init */
 
-  if (!hprevinst)
-    {
-      w32_init_class (hinst);
-    }
+  w32_init_class (hinst);
 
   if (f->size_hint_flags & USPosition || f->size_hint_flags & PPosition)
     {
@@ -2237,6 +2277,58 @@ unregister_hot_keys (HWND hwnd)
     }
 }
 
+#if EMACSDEBUG
+const char*
+w32_name_of_message (UINT msg)
+{
+  unsigned i;
+  static char buf[64];
+  static const struct {
+    UINT msg;
+    const char* name;
+  } msgnames[] = {
+#define M(msg) { msg, # msg }
+      M (WM_PAINT),
+      M (WM_TIMER),
+      M (WM_USER),
+      M (WM_MOUSEMOVE),
+      M (WM_LBUTTONUP),
+      M (WM_KEYDOWN),
+      M (WM_EMACS_KILL),
+      M (WM_EMACS_CREATEWINDOW),
+      M (WM_EMACS_DONE),
+      M (WM_EMACS_CREATESCROLLBAR),
+      M (WM_EMACS_SHOWWINDOW),
+      M (WM_EMACS_SETWINDOWPOS),
+      M (WM_EMACS_DESTROYWINDOW),
+      M (WM_EMACS_TRACKPOPUPMENU),
+      M (WM_EMACS_SETFOCUS),
+      M (WM_EMACS_SETFOREGROUND),
+      M (WM_EMACS_SETLOCALE),
+      M (WM_EMACS_SETKEYBOARDLAYOUT),
+      M (WM_EMACS_REGISTER_HOT_KEY),
+      M (WM_EMACS_UNREGISTER_HOT_KEY),
+      M (WM_EMACS_TOGGLE_LOCK_KEY),
+      M (WM_EMACS_TRACK_CARET),
+      M (WM_EMACS_DESTROY_CARET),
+      M (WM_EMACS_SHOW_CARET),
+      M (WM_EMACS_HIDE_CARET),
+      M (WM_EMACS_SETCURSOR),
+      M (WM_EMACS_PAINT),
+      M (WM_CHAR),
+#undef M
+      { 0, 0 }
+  };
+
+  for (i = 0; msgnames[i].name; ++i)
+    if (msgnames[i].msg == msg)
+      return msgnames[i].name;
+
+  sprintf (buf, "message 0x%04x", (unsigned)msg);
+  return buf;
+}
+#endif /* EMACSDEBUG */
+
 /* Main message dispatch loop. */
 
 static void
@@ -2250,6 +2342,10 @@ w32_msg_pump (deferred_msg * msg_buf)
 
   while (GetMessage (&msg, NULL, 0, 0))
     {
+
+      /* DebPrint (("w32_msg_pump: %s time:%u\n", */
+      /*            w32_name_of_message (msg.message), msg.time)); */
+
       if (msg.hwnd == NULL)
 	{
 	  switch (msg.message)
@@ -4663,6 +4759,37 @@ If omitted or nil, that stands for the selected frame's display.  */)
 {
   return Qnil;
 }
+
+DEFUN ("set-message-beep", Fset_message_beep, Sset_message_beep, 1, 1, 0,
+       doc: /* Set the sound generated when the bell is rung.
+SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
+to use the corresponding system sound for the bell.  The 'silent sound
+prevents Emacs from making any sound at all.
+SOUND is nil to use the normal beep.  */)
+  (Lisp_Object sound)
+{
+  CHECK_SYMBOL (sound);
+
+  if (NILP (sound))
+      sound_type = 0xFFFFFFFF;
+  else if (EQ (sound, intern ("asterisk")))
+      sound_type = MB_ICONASTERISK;
+  else if (EQ (sound, intern ("exclamation")))
+      sound_type = MB_ICONEXCLAMATION;
+  else if (EQ (sound, intern ("hand")))
+      sound_type = MB_ICONHAND;
+  else if (EQ (sound, intern ("question")))
+      sound_type = MB_ICONQUESTION;
+  else if (EQ (sound, intern ("ok")))
+      sound_type = MB_OK;
+  else if (EQ (sound, intern ("silent")))
+      sound_type = MB_EMACS_SILENT;
+  else
+      sound_type = 0xFFFFFFFF;
+
+  return sound;
+}
+
 \f
 int
 x_pixel_width (register struct frame *f)
@@ -5843,6 +5970,18 @@ Value is t if tooltip was open, nil otherwise.  */)
 			File selection dialog
  ***********************************************************************/
 
+#define FILE_NAME_TEXT_FIELD edt1
+#define FILE_NAME_COMBO_BOX cmb13
+#define FILE_NAME_LIST lst1
+
+#ifdef NTGUI_UNICODE
+#define GUISTR(x) (L ## x)
+typedef wchar_t guichar_t;
+#else /* !NTGUI_UNICODE */
+#define GUISTR(x) x
+typedef char guichar_t;
+#endif /* NTGUI_UNICODE */
+
 /* Callback for altering the behavior of the Open File dialog.
    Makes the Filename text field contain "Current Directory" and be
    read-only when "Directories" is selected in the filter.  This
@@ -5853,7 +5992,11 @@ file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
   if (msg == WM_NOTIFY)
     {
-      OFNOTIFY * notify = (OFNOTIFY *)lParam;
+#ifdef NTGUI_UNICODE
+      OFNOTIFYW * notify = (OFNOTIFYW *)lParam;
+#else /* !NTGUI_UNICODE */
+      OFNOTIFYA * notify = (OFNOTIFYA *)lParam;
+#endif /* NTGUI_UNICODE */
       /* Detect when the Filter dropdown is changed.  */
       if (notify->hdr.code == CDN_TYPECHANGE
 	  || notify->hdr.code == CDN_INITDONE)
@@ -5881,7 +6024,7 @@ file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 	  if (notify->lpOFN->nFilterIndex == 2)
 	    {
 	      CommDlg_OpenSave_SetControlText (dialog, FILE_NAME_TEXT_FIELD,
-					       "Current Directory");
+					       GUISTR ("Current Directory"));
 	      EnableWindow (edit_control, FALSE);
 	      /* Note that at least on Windows 7, the above call to EnableWindow
 		 disables the window that would ordinarily have focus.	If we
@@ -5897,7 +6040,8 @@ file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 	      /* Don't override default filename on init done.  */
 	      if (notify->hdr.code == CDN_TYPECHANGE)
 		CommDlg_OpenSave_SetControlText (dialog,
-						 FILE_NAME_TEXT_FIELD, "");
+						 FILE_NAME_TEXT_FIELD,
+                                                 GUISTR (""));
 	      EnableWindow (edit_control, TRUE);
 	    }
 	}
@@ -5905,19 +6049,6 @@ file_dialog_callback (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   return 0;
 }
 
-/* Since we compile with _WIN32_WINNT set to 0x0400 (for NT4 compatibility)
-   we end up with the old file dialogs. Define a big enough struct for the
-   new dialog to trick GetOpenFileName into giving us the new dialogs on
-   Windows 2000 and XP.  */
-typedef struct
-{
-  OPENFILENAME real_details;
-  void * pReserved;
-  DWORD dwReserved;
-  DWORD FlagsEx;
-} NEWOPENFILENAME;
-
-
 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
        doc: /* Read file name, prompting with PROMPT in directory DIR.
 Use a file selection dialog.  Select DEFAULT-FILENAME in the dialog's file
@@ -5929,134 +6060,203 @@ Motif or Gtk toolkits.  With the Motif toolkit, ONLY-DIR-P is ignored.
 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.  */)
   (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
 {
+  /* Filter index: 1: All Files, 2: Directories only  */
+  static const guichar_t filter[] =
+    GUISTR ("All Files (*.*)\0*.*\0Directories\0*|*\0");
+
+  Lisp_Object filename = default_filename;
   struct frame *f = SELECTED_FRAME ();
-  Lisp_Object file = Qnil;
-  int count = SPECPDL_INDEX ();
+  BOOL file_opened = FALSE;
+  Lisp_Object orig_dir = dir;
+  Lisp_Object orig_prompt = prompt;
+
+  /* If we compile with _WIN32_WINNT set to 0x0400 (for NT4
+     compatibility) we end up with the old file dialogs. Define a big
+     enough struct for the new dialog to trick GetOpenFileName into
+     giving us the new dialogs on newer versions of Windows.  */
+  struct {
+#ifdef NTGUI_UNICODE
+    OPENFILENAMEW details;
+#else /* !NTGUI_UNICODE */
+    OPENFILENAMEA details;
+#endif /* NTGUI_UNICODE */
+
+#if _WIN32_WINNT < 0x500 /* < win2k */
+      PVOID pvReserved;
+      DWORD dwReserved;
+      DWORD FlagsEx;
+#endif /* < win2k */
+  } new_file_details;
+
+#ifdef NTGUI_UNICODE
+  wchar_t filename_buf[MAX_PATH + 1];
+  OPENFILENAMEW * file_details = &new_file_details.details;
+#else /* not NTGUI_UNICODE */
+  char filename_buf[MAX_PATH + 1];
+  OPENFILENAMEA * file_details = &new_file_details.details;
+#endif /* NTGUI_UNICODE */
+
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
-  char filename[MAX_PATH + 1];
-  char init_dir[MAX_PATH + 1];
-  int default_filter_index = 1; /* 1: All Files, 2: Directories only  */
-
-  GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
-  CHECK_STRING (prompt);
-  CHECK_STRING (dir);
-
-  /* Create the dialog with PROMPT as title, using DIR as initial
-     directory and using "*" as pattern.  */
-  dir = Fexpand_file_name (dir, Qnil);
-  strncpy (init_dir, SDATA (ENCODE_FILE (dir)), MAX_PATH);
-  init_dir[MAX_PATH] = '\0';
-  unixtodos_filename (init_dir);
-
-  if (STRINGP (default_filename))
-    {
-      char *file_name_only;
-      char *full_path_name = SDATA (ENCODE_FILE (default_filename));
+  GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, filename);
 
-      unixtodos_filename (full_path_name);
+  {
+    struct gcpro gcpro1, gcpro2;
+    GCPRO2 (orig_dir, orig_prompt); /* There is no GCPRON, N>6.  */
 
-      file_name_only = strrchr (full_path_name, '\\');
-      if (!file_name_only)
-        file_name_only = full_path_name;
-      else
-	file_name_only++;
+    /* Note: under NTGUI_UNICODE, we do _NOT_ use ENCODE_FILE: the
+       system file encoding expected by the platform APIs (e.g. Cygwin's
+       POSIX implementation) may not the same as the encoding expected
+       by the Windows API!  */
 
-      strncpy (filename, file_name_only, MAX_PATH);
-      filename[MAX_PATH] = '\0';
-    }
-  else
-    filename[0] = '\0';
+    CHECK_STRING (prompt);
+    CHECK_STRING (dir);
 
-  /* The code in file_dialog_callback that attempts to set the text
-     of the file name edit window when handling the CDN_INITDONE
-     WM_NOTIFY message does not work.  Setting filename to "Current
-     Directory" in the only_dir_p case here does work however.  */
-  if (filename[0] == 0 && ! NILP (only_dir_p))
-    strcpy (filename, "Current Directory");
+    dir = Fexpand_file_name (dir, Qnil);
 
-  {
-    NEWOPENFILENAME new_file_details;
-    BOOL file_opened = FALSE;
-    OPENFILENAME * file_details = &new_file_details.real_details;
+    if (STRINGP (filename))
+      filename = Ffile_name_nondirectory (filename);
+    else
+      filename = empty_unibyte_string;
 
-    /* Prevent redisplay.  */
-    specbind (Qinhibit_redisplay, Qt);
-    BLOCK_INPUT;
+#ifdef CYGWIN
+    dir = Fcygwin_convert_path_to_windows (dir, Qt);
+    if (SCHARS (filename) > 0)
+      filename = Fcygwin_convert_path_to_windows (filename, Qnil);
+#endif
 
+    CHECK_STRING (dir);
+    CHECK_STRING (filename);
+
+    /* The code in file_dialog_callback that attempts to set the text
+       of the file name edit window when handling the CDN_INITDONE
+       WM_NOTIFY message does not work.  Setting filename to "Current
+       Directory" in the only_dir_p case here does work however.  */
+    if (SCHARS (filename) == 0 && ! NILP (only_dir_p))
+      filename = build_string ("Current Directory");
+
+    /* Convert the values we've computed so far to system form.  */
+#ifdef NTGUI_UNICODE
+    to_unicode (prompt, &prompt);
+    to_unicode (dir, &dir);
+    to_unicode (filename, &filename);
+#else /* !NTGUI_UNICODE */
+    prompt = ENCODE_FILE (prompt);
+    dir = ENCODE_FILE (dir);
+    filename = ENCODE_FILE (filename);
+
+    /* We modify these in-place, so make copies for safety.  */
+    dir = Fcopy_sequence (dir);
+    unixtodos_filename (SDATA (dir));
+    filename = Fcopy_sequence (filename);
+    unixtodos_filename (SDATA (filename));
+#endif /* NTGUI_UNICODE */
+
+    /* Fill in the structure for the call to GetOpenFileName below.  For
+       NTGUI_UNICODE builds (which run only on NT), we just use the
+       actual size of the structure.  For non-NTGUI_UNICODE builds, we
+       tell the OS we're using an old version of the structure if it's not
+       new enough to support the newer version.  */
     memset (&new_file_details, 0, sizeof (new_file_details));
-    /* Apparently NT4 crashes if you give it an unexpected size.
-       I'm not sure about Windows 9x, so play it safe.  */
+
     if (w32_major_version > 4 && w32_major_version < 95)
-      file_details->lStructSize = sizeof (NEWOPENFILENAME);
+      file_details->lStructSize = sizeof (new_file_details);
     else
-      file_details->lStructSize = sizeof (OPENFILENAME);
+      file_details->lStructSize = sizeof (*file_details);
+
+    /* Set up the inout parameter for the selected file name.  */
+    if (SBYTES (filename) + 1 > sizeof (filename_buf))
+      error ("filename too long");
+
+    memcpy (filename_buf, SDATA (filename), SBYTES (filename) + 1);
+    file_details->lpstrFile = filename_buf;
+    file_details->nMaxFile = sizeof (filename_buf) / sizeof (*filename_buf);
 
     file_details->hwndOwner = FRAME_W32_WINDOW (f);
     /* Undocumented Bug in Common File Dialog:
        If a filter is not specified, shell links are not resolved.  */
-    file_details->lpstrFilter = "All Files (*.*)\0*.*\0Directories\0*|*\0\0";
-    file_details->lpstrFile = filename;
-    file_details->nMaxFile = sizeof (filename);
-    file_details->lpstrInitialDir = init_dir;
-    file_details->lpstrTitle = SDATA (prompt);
-
-    if (! NILP (only_dir_p))
-      default_filter_index = 2;
-
-    file_details->nFilterIndex = default_filter_index;
-
+    file_details->lpstrFilter = filter;
+    file_details->lpstrInitialDir = (guichar_t*) SDATA (dir);
+    file_details->lpstrTitle = (guichar_t*) SDATA (prompt);
+    file_details->nFilterIndex = NILP (only_dir_p) ? 1 : 2;
     file_details->Flags = (OFN_HIDEREADONLY | OFN_NOCHANGEDIR
-			  | OFN_EXPLORER | OFN_ENABLEHOOK);
+                           | OFN_EXPLORER | OFN_ENABLEHOOK);
+
     if (!NILP (mustmatch))
       {
-	/* Require that the path to the parent directory exists.  */
-	file_details->Flags |= OFN_PATHMUSTEXIST;
-	/* If we are looking for a file, require that it exists.  */
-	if (NILP (only_dir_p))
-	  file_details->Flags |= OFN_FILEMUSTEXIST;
+        /* Require that the path to the parent directory exists.  */
+        file_details->Flags |= OFN_PATHMUSTEXIST;
+        /* If we are looking for a file, require that it exists.  */
+        if (NILP (only_dir_p))
+          file_details->Flags |= OFN_FILEMUSTEXIST;
       }
 
-    file_details->lpfnHook = (LPOFNHOOKPROC) file_dialog_callback;
-
-    file_opened = GetOpenFileName (file_details);
+    {
+      int count = SPECPDL_INDEX ();
+      specbind (Qinhibit_redisplay, Qt);
+      BLOCK_INPUT;
+      file_details->lpfnHook = file_dialog_callback;
 
-    UNBLOCK_INPUT;
+#ifdef NTGUI_UNICODE
+      file_opened = GetOpenFileNameW (file_details);
+#else /* !NTGUI_UNICODE */
+      file_opened = GetOpenFileNameA (file_details);
+#endif /* NTGUI_UNICODE */
+      UNBLOCK_INPUT;
+      unbind_to (count, Qnil);
+    }
 
     if (file_opened)
       {
-	dostounix_filename (filename);
-
-	if (file_details->nFilterIndex == 2)
-	  {
-	    /* "Directories" selected - strip dummy file name.  */
-	    char * last = strrchr (filename, '/');
-	    *last = '\0';
-	  }
-
-	file = DECODE_FILE (build_string (filename));
+        /* Get an Emacs string from the value Windows gave us.  */
+#ifdef NTGUI_UNICODE
+        filename = from_unicode (
+          make_unibyte_string (
+            (char*) filename_buf,
+            /* we get one of the two final 0 bytes for free. */
+            1 + sizeof (wchar_t) * wcslen (filename_buf)));
+#else /* !NTGUI_UNICODE */
+        dostounix_filename (filename_buf);
+        filename = DECODE_FILE (make_string (filename_buf));
+#endif /* NTGUI_UNICODE */
+
+#ifdef CYGWIN
+        filename = Fcygwin_convert_path_from_windows (filename, Qt);
+#endif /* CYGWIN */
+
+        /* Strip the dummy filename off the end of the string if we
+           added it to select a directory.  */
+        if (file_details->nFilterIndex == 2)
+          {
+            filename = Ffile_name_directory (filename);
+          }
       }
     /* User canceled the dialog without making a selection.  */
     else if (!CommDlgExtendedError ())
-      file = Qnil;
+      filename = Qnil;
     /* An error occurred, fallback on reading from the mini-buffer.  */
     else
-      file = Fcompleting_read (prompt, intern ("read-file-name-internal"),
-			       dir, mustmatch, dir, Qfile_name_history,
-			       default_filename, Qnil);
+      filename = Fcompleting_read (
+        orig_prompt,
+        intern ("read-file-name-internal"),
+        orig_dir,
+        mustmatch,
+        orig_dir,
+        Qfile_name_history,
+        default_filename,
+        Qnil);
 
-    file = unbind_to (count, file);
+    UNGCPRO;
   }
 
-  UNGCPRO;
-
   /* Make "Cancel" equivalent to C-g.  */
-  if (NILP (file))
+  if (NILP (filename))
     Fsignal (Qquit, Qnil);
 
-  return unbind_to (count, file);
+  RETURN_UNGCPRO (filename);
 }
 
-
+\f
+#ifdef WINDOWSNT
 /* Moving files to the system recycle bin.
    Used by `move-file-to-trash' instead of the default moving to ~/.Trash  */
 DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
@@ -6110,6 +6310,8 @@ DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
   return Qnil;
 }
 
+#endif /* WINDOWSNT */
+
 \f
 /***********************************************************************
                          w32 specialized functions
@@ -6544,7 +6746,7 @@ The following %-sequences are provided:
       else
 	{
 	  char buffer[16];
-	  _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
+	  snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
 	  load_percentage = build_string (buffer);
 	}
 
@@ -6555,18 +6757,18 @@ The following %-sequences are provided:
 	  long m;
 	  float h;
 	  char buffer[16];
-	  _snprintf (buffer, 16, "%ld", seconds_left);
+	  snprintf (buffer, 16, "%ld", seconds_left);
 	  seconds = build_string (buffer);
 
 	  m = seconds_left / 60;
-	  _snprintf (buffer, 16, "%ld", m);
+	  snprintf (buffer, 16, "%ld", m);
 	  minutes = build_string (buffer);
 
 	  h = seconds_left / 3600.0;
-	  _snprintf (buffer, 16, "%3.1f", h);
+	  snprintf (buffer, 16, "%3.1f", h);
 	  hours = build_string (buffer);
 
-	  _snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
+	  snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
 	  remain = build_string (buffer);
 	}
       sequences[0] = Fcons (make_number ('L'), line_status);
@@ -6728,10 +6930,10 @@ DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
         {
 	  /* a remote printer */
 	  if (*ppi2->pServerName == '\\')
-	    _snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
+	    snprintf (pname_buf, sizeof (pname_buf), "%s\\%s", ppi2->pServerName,
 		       ppi2->pShareName);
 	  else
-	    _snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
+	    snprintf (pname_buf, sizeof (pname_buf), "\\\\%s\\%s", ppi2->pServerName,
 		       ppi2->pShareName);
 	  pname_buf[sizeof (pname_buf) - 1] = '\0';
 	}
@@ -6750,6 +6952,278 @@ DEFUN ("default-printer-name", Fdefault_printer_name, Sdefault_printer_name,
   return build_string (pname_buf);
 }
 \f
+
+/* Equivalent of strerror for W32 error codes.  */
+char *
+w32_strerror (int error_no)
+{
+  static char buf[500];
+  DWORD ret;
+
+  if (error_no == 0)
+    error_no = GetLastError ();
+
+  ret = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
+                       FORMAT_MESSAGE_IGNORE_INSERTS,
+                       NULL,
+                       error_no,
+                       0, /* choose most suitable language */
+                       buf, sizeof (buf), NULL);
+
+  while (ret > 0 && (buf[ret - 1] == '\n' ||
+                     buf[ret - 1] == '\r' ))
+      --ret;
+  buf[ret] = '\0';
+  if (!ret)
+    sprintf (buf, "w32 error %u", error_no);
+
+  return buf;
+}
+
+/* For convenience when debugging.  */
+int
+w32_last_error (void)
+{
+  return GetLastError ();
+}
+
+/* Cache information describing the NT system for later use.  */
+void
+cache_system_info (void)
+{
+  union
+    {
+      struct info
+	{
+	  char  major;
+	  char  minor;
+	  short platform;
+	} info;
+      DWORD data;
+    } version;
+
+  /* Cache the version of the operating system.  */
+  version.data = GetVersion ();
+  w32_major_version = version.info.major;
+  w32_minor_version = version.info.minor;
+
+  if (version.info.platform & 0x8000)
+    os_subtype = OS_WIN95;
+  else
+    os_subtype = OS_NT;
+
+  /* Cache page size, allocation unit, processor type, etc.  */
+  GetSystemInfo (&sysinfo_cache);
+  syspage_mask = sysinfo_cache.dwPageSize - 1;
+
+  /* Cache os info.  */
+  osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+  GetVersionEx (&osinfo_cache);
+
+  w32_build_number = osinfo_cache.dwBuildNumber;
+  if (os_subtype == OS_WIN95)
+    w32_build_number &= 0xffff;
+
+  w32_num_mouse_buttons = GetSystemMetrics (SM_CMOUSEBUTTONS);
+}
+
+#ifdef EMACSDEBUG
+void
+_DebPrint (const char *fmt, ...)
+{
+  char buf[1024];
+  va_list args;
+
+  va_start (args, fmt);
+  vsprintf (buf, fmt, args);
+  va_end (args);
+#if CYGWIN
+  fprintf (stderr, "%s", buf);
+#endif
+  OutputDebugString (buf);
+}
+#endif
+
+int
+w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state)
+{
+  int cur_state = (GetKeyState (vk_code) & 1);
+
+  if (NILP (new_state)
+      || (NUMBERP (new_state)
+	  && ((XUINT (new_state)) & 1) != cur_state))
+    {
+#ifdef WINDOWSNT
+      faked_key = vk_code;
+#endif /* WINDOWSNT */
+
+      keybd_event ((BYTE) vk_code,
+		   (BYTE) MapVirtualKey (vk_code, 0),
+		   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
+      keybd_event ((BYTE) vk_code,
+		   (BYTE) MapVirtualKey (vk_code, 0),
+		   KEYEVENTF_EXTENDEDKEY | 0, 0);
+      keybd_event ((BYTE) vk_code,
+		   (BYTE) MapVirtualKey (vk_code, 0),
+		   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
+      cur_state = !cur_state;
+    }
+
+  return cur_state;
+}
+
+/* Translate console modifiers to emacs modifiers.
+   German keyboard support (Kai Morgan Zeise 2/18/95).  */
+int
+w32_kbd_mods_to_emacs (DWORD mods, WORD key)
+{
+  int retval = 0;
+
+  /* If we recognize right-alt and left-ctrl as AltGr, and it has been
+     pressed, first remove those modifiers.  */
+  if (!NILP (Vw32_recognize_altgr)
+      && (mods & (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
+      == (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
+    mods &= ~ (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED);
+
+  if (mods & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
+    retval = ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier);
+
+  if (mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
+    {
+      retval |= ctrl_modifier;
+      if ((mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
+	  == (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
+	retval |= meta_modifier;
+    }
+
+  if (mods & LEFT_WIN_PRESSED)
+    retval |= w32_key_to_modifier (VK_LWIN);
+  if (mods & RIGHT_WIN_PRESSED)
+    retval |= w32_key_to_modifier (VK_RWIN);
+  if (mods & APPS_PRESSED)
+    retval |= w32_key_to_modifier (VK_APPS);
+  if (mods & SCROLLLOCK_ON)
+    retval |= w32_key_to_modifier (VK_SCROLL);
+
+  /* Just in case someone wanted the original behavior, make it
+     optional by setting w32-capslock-is-shiftlock to t.  */
+  if (NILP (Vw32_capslock_is_shiftlock)
+      /* Keys that should _not_ be affected by CapsLock.  */
+      && (    (key == VK_BACK)
+	   || (key == VK_TAB)
+	   || (key == VK_CLEAR)
+	   || (key == VK_RETURN)
+	   || (key == VK_ESCAPE)
+	   || ((key >= VK_SPACE) && (key <= VK_HELP))
+	   || ((key >= VK_NUMPAD0) && (key <= VK_F24))
+	   || ((key >= VK_NUMPAD_CLEAR) && (key <= VK_NUMPAD_DELETE))
+	 ))
+    {
+      /* Only consider shift state.  */
+      if ((mods & SHIFT_PRESSED) != 0)
+	retval |= shift_modifier;
+    }
+  else
+    {
+      /* Ignore CapsLock state if not enabled.  */
+      if (NILP (Vw32_enable_caps_lock))
+	mods &= ~CAPSLOCK_ON;
+      if ((mods & (SHIFT_PRESSED | CAPSLOCK_ON)) != 0)
+	retval |= shift_modifier;
+    }
+
+  return retval;
+}
+
+/* The return code indicates key code size. */
+int
+w32_kbd_patch_key (KEY_EVENT_RECORD *event)
+{
+  unsigned int key_code = event->wVirtualKeyCode;
+  unsigned int mods = event->dwControlKeyState;
+  BYTE keystate[256];
+  static BYTE ansi_code[4];
+  static int isdead = 0;
+
+  if (isdead == 2)
+    {
+      event->uChar.AsciiChar = ansi_code[2];
+      isdead = 0;
+      return 1;
+    }
+  if (event->uChar.AsciiChar != 0)
+    return 1;
+
+  memset (keystate, 0, sizeof (keystate));
+  keystate[key_code] = 0x80;
+  if (mods & SHIFT_PRESSED)
+    keystate[VK_SHIFT] = 0x80;
+  if (mods & CAPSLOCK_ON)
+    keystate[VK_CAPITAL] = 1;
+  /* If we recognize right-alt and left-ctrl as AltGr, set the key
+     states accordingly before invoking ToAscii.  */
+  if (!NILP (Vw32_recognize_altgr)
+      && (mods & LEFT_CTRL_PRESSED) && (mods & RIGHT_ALT_PRESSED))
+    {
+      keystate[VK_CONTROL] = 0x80;
+      keystate[VK_LCONTROL] = 0x80;
+      keystate[VK_MENU] = 0x80;
+      keystate[VK_RMENU] = 0x80;
+    }
+
+  /* On NT, call ToUnicode instead and then convert to the current
+     locale's default codepage.  */
+  if (os_subtype == OS_NT)
+    {
+      WCHAR buf[128];
+
+      isdead = ToUnicode (event->wVirtualKeyCode, event->wVirtualScanCode,
+			  keystate, buf, 128, 0);
+      if (isdead > 0)
+	{
+	  char cp[20];
+	  int cpId;
+
+	  event->uChar.UnicodeChar = buf[isdead - 1];
+
+	  GetLocaleInfo (GetThreadLocale (),
+			 LOCALE_IDEFAULTANSICODEPAGE, cp, 20);
+	  cpId = atoi (cp);
+	  isdead = WideCharToMultiByte (cpId, 0, buf, isdead,
+					ansi_code, 4, NULL, NULL);
+	}
+      else
+	isdead = 0;
+    }
+  else
+    {
+      isdead = ToAscii (event->wVirtualKeyCode, event->wVirtualScanCode,
+                        keystate, (LPWORD) ansi_code, 0);
+    }
+
+  if (isdead == 0)
+    return 0;
+  event->uChar.AsciiChar = ansi_code[0];
+  return isdead;
+}
+
+void
+w32_sys_ring_bell (struct frame *f)
+{
+  if (sound_type == 0xFFFFFFFF)
+    {
+      Beep (666, 100);
+    }
+  else if (sound_type == MB_EMACS_SILENT)
+    {
+      /* Do nothing.  */
+    }
+  else
+    MessageBeep (sound_type);
+}
+
+\f
 /***********************************************************************
 			    Initialization
  ***********************************************************************/
@@ -7117,10 +7591,10 @@ only be necessary if the default setting causes problems.  */);
 
   defsubr (&Sfile_system_info);
   defsubr (&Sdefault_printer_name);
+  defsubr (&Sset_message_beep);
 
   check_window_system_func = check_w32;
 
-
   hourglass_timer = 0;
   hourglass_hwnd = NULL;
 
@@ -7135,7 +7609,9 @@ only be necessary if the default setting causes problems.  */);
   staticpro (&last_show_tip_args);
 
   defsubr (&Sx_file_dialog);
+#ifdef WINDOWSNT
   defsubr (&Ssystem_move_file_to_trash);
+#endif
 }
 
 
@@ -7214,9 +7690,3 @@ w32_abort (void)
     }
 }
 
-/* For convenience when debugging.  */
-int
-w32_last_error (void)
-{
-  return GetLastError ();
-}
diff --git a/src/w32font.c b/src/w32font.c
index e8a223d..24bf8db 100644
--- a/src/w32font.c
+++ b/src/w32font.c
@@ -18,6 +18,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <windows.h>
+#include <stdio.h>
 #include <math.h>
 #include <ctype.h>
 #include <commdlg.h>
@@ -1450,6 +1451,9 @@ w32font_coverage_ok (FONTSIGNATURE * coverage, BYTE charset)
   return 1;
 }
 
+#if !WINDOWSNT
+#define _strlwr strlwr
+#endif
 
 static int
 check_face_name (LOGFONT *font, char *full_name)
diff --git a/src/w32heap.c b/src/w32heap.c
index 477c11a..64c0085 100644
--- a/src/w32heap.c
+++ b/src/w32heap.c
@@ -29,60 +29,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
 
-/* This gives us the page size and the size of the allocation unit on NT.  */
-SYSTEM_INFO sysinfo_cache;
-
-/* This gives us version, build, and platform identification.  */
-OSVERSIONINFO osinfo_cache;
-
-unsigned long syspage_mask = 0;
-
-/* The major and minor versions of NT.  */
-int w32_major_version;
-int w32_minor_version;
-int w32_build_number;
-
-/* Distinguish between Windows NT and Windows 95.  */
-int os_subtype;
-
-/* Cache information describing the NT system for later use.  */
-void
-cache_system_info (void)
-{
-  union
-    {
-      struct info
-	{
-	  char  major;
-	  char  minor;
-	  short platform;
-	} info;
-      DWORD data;
-    } version;
-
-  /* Cache the version of the operating system.  */
-  version.data = GetVersion ();
-  w32_major_version = version.info.major;
-  w32_minor_version = version.info.minor;
-
-  if (version.info.platform & 0x8000)
-    os_subtype = OS_WIN95;
-  else
-    os_subtype = OS_NT;
-
-  /* Cache page size, allocation unit, processor type, etc.  */
-  GetSystemInfo (&sysinfo_cache);
-  syspage_mask = sysinfo_cache.dwPageSize - 1;
-
-  /* Cache os info.  */
-  osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
-  GetVersionEx (&osinfo_cache);
-
-  w32_build_number = osinfo_cache.dwBuildNumber;
-  if (os_subtype == OS_WIN95)
-    w32_build_number &= 0xffff;
-}
-
 /* Emulate getpagesize.  */
 int
 getpagesize (void)
diff --git a/src/w32heap.h b/src/w32heap.h
index e75bfcf..7f8214b 100644
--- a/src/w32heap.h
+++ b/src/w32heap.h
@@ -24,6 +24,26 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <windows.h>
 
+extern int    	      w32_major_version;
+extern int    	      w32_minor_version;
+extern int    	      w32_build_number;
+extern BOOL   	      using_dynamic_heap;
+
+enum {
+  OS_WIN95 = 1,
+  OS_NT
+};
+
+extern int os_subtype;
+
+#define get_w32_major_version()  	w32_major_version
+#define get_w32_minor_version()  	w32_minor_version
+
+/* Cache system info, e.g., the NT page size.  */
+extern void cache_system_info (void);
+
+#if WINDOWSNT
+
 #define ROUND_UP(p, align)   (((DWORD)(p) + (align)-1) & ~((align)-1))
 #define ROUND_DOWN(p, align) ((DWORD)(p) & ~((align)-1))
 
@@ -37,25 +57,12 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 #define get_page_size()			sysinfo_cache.dwPageSize
 #define get_allocation_unit()		sysinfo_cache.dwAllocationGranularity
 #define get_processor_type()		sysinfo_cache.dwProcessorType
-#define get_w32_major_version()  	w32_major_version
-#define get_w32_minor_version()  	w32_minor_version
 
 extern unsigned char *get_data_start (void);
 extern unsigned char *get_data_end (void);
 extern unsigned long  reserved_heap_size;
 extern SYSTEM_INFO    sysinfo_cache;
 extern OSVERSIONINFO  osinfo_cache;
-extern BOOL   	      using_dynamic_heap;
-extern int    	      w32_major_version;
-extern int    	      w32_minor_version;
-extern int    	      w32_build_number;
-
-enum {
-  OS_WIN95 = 1,
-  OS_NT
-};
-
-extern int os_subtype;
 
 /* Emulation of Unix sbrk().  */
 extern void *sbrk (unsigned long size);
@@ -66,9 +73,6 @@ extern void init_heap (void);
 /* Round the heap to this size.  */
 extern void round_heap (unsigned long size);
 
-/* Cache system info, e.g., the NT page size.  */
-extern void cache_system_info (void);
-
 /* ----------------------------------------------------------------- */
 /* Useful routines for manipulating memory-mapped files. */
 
@@ -91,5 +95,7 @@ IMAGE_SECTION_HEADER * find_section (char * name, IMAGE_NT_HEADERS * nt_header);
    relative virtual address. */
 IMAGE_SECTION_HEADER * rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header);
 
+#endif /* WINDOWSNT */
+
 #endif /* NTHEAP_H_ */
 
diff --git a/src/w32inevt.c b/src/w32inevt.c
index fddde61..03073b5 100644
--- a/src/w32inevt.c
+++ b/src/w32inevt.c
@@ -59,6 +59,9 @@ static INPUT_RECORD *queue_ptr = event_queue, *queue_end = event_queue;
 /* Temporarily store lead byte of DBCS input sequences.  */
 static char dbcs_lead = 0;
 
+/* Set by w32_console_toggle_lock_key.  */
+int faked_key;
+
 static int
 fill_queue (BOOL block)
 {
@@ -97,169 +100,6 @@ get_frame (void)
   return SELECTED_FRAME ();
 }
 
-/* Translate console modifiers to emacs modifiers.
-   German keyboard support (Kai Morgan Zeise 2/18/95).  */
-int
-w32_kbd_mods_to_emacs (DWORD mods, WORD key)
-{
-  int retval = 0;
-
-  /* If we recognize right-alt and left-ctrl as AltGr, and it has been
-     pressed, first remove those modifiers.  */
-  if (!NILP (Vw32_recognize_altgr)
-      && (mods & (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
-      == (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED))
-    mods &= ~ (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED);
-
-  if (mods & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
-    retval = ((NILP (Vw32_alt_is_meta)) ? alt_modifier : meta_modifier);
-
-  if (mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-    {
-      retval |= ctrl_modifier;
-      if ((mods & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-	  == (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
-	retval |= meta_modifier;
-    }
-
-  if (mods & LEFT_WIN_PRESSED)
-    retval |= w32_key_to_modifier (VK_LWIN);
-  if (mods & RIGHT_WIN_PRESSED)
-    retval |= w32_key_to_modifier (VK_RWIN);
-  if (mods & APPS_PRESSED)
-    retval |= w32_key_to_modifier (VK_APPS);
-  if (mods & SCROLLLOCK_ON)
-    retval |= w32_key_to_modifier (VK_SCROLL);
-
-  /* Just in case someone wanted the original behavior, make it
-     optional by setting w32-capslock-is-shiftlock to t.  */
-  if (NILP (Vw32_capslock_is_shiftlock)
-      /* Keys that should _not_ be affected by CapsLock.  */
-      && (    (key == VK_BACK)
-	   || (key == VK_TAB)
-	   || (key == VK_CLEAR)
-	   || (key == VK_RETURN)
-	   || (key == VK_ESCAPE)
-	   || ((key >= VK_SPACE) && (key <= VK_HELP))
-	   || ((key >= VK_NUMPAD0) && (key <= VK_F24))
-	   || ((key >= VK_NUMPAD_CLEAR) && (key <= VK_NUMPAD_DELETE))
-	 ))
-    {
-      /* Only consider shift state.  */
-      if ((mods & SHIFT_PRESSED) != 0)
-	retval |= shift_modifier;
-    }
-  else
-    {
-      /* Ignore CapsLock state if not enabled.  */
-      if (NILP (Vw32_enable_caps_lock))
-	mods &= ~CAPSLOCK_ON;
-      if ((mods & (SHIFT_PRESSED | CAPSLOCK_ON)) != 0)
-	retval |= shift_modifier;
-    }
-
-  return retval;
-}
-
-#if 0
-/* Return nonzero if the virtual key is a dead key.  */
-static int
-is_dead_key (int wparam)
-{
-  unsigned int code = MapVirtualKey (wparam, 2);
-
-  /* Windows 95 returns 0x8000, NT returns 0x80000000.  */
-  return (code & 0x80008000) ? 1 : 0;
-}
-#endif
-
-/* The return code indicates key code size. */
-int
-w32_kbd_patch_key (KEY_EVENT_RECORD *event)
-{
-  unsigned int key_code = event->wVirtualKeyCode;
-  unsigned int mods = event->dwControlKeyState;
-  BYTE keystate[256];
-  static BYTE ansi_code[4];
-  static int isdead = 0;
-
-  if (isdead == 2)
-    {
-      event->uChar.AsciiChar = ansi_code[2];
-      isdead = 0;
-      return 1;
-    }
-  if (event->uChar.AsciiChar != 0)
-    return 1;
-
-  memset (keystate, 0, sizeof (keystate));
-  keystate[key_code] = 0x80;
-  if (mods & SHIFT_PRESSED)
-    keystate[VK_SHIFT] = 0x80;
-  if (mods & CAPSLOCK_ON)
-    keystate[VK_CAPITAL] = 1;
-  /* If we recognize right-alt and left-ctrl as AltGr, set the key
-     states accordingly before invoking ToAscii.  */
-  if (!NILP (Vw32_recognize_altgr)
-      && (mods & LEFT_CTRL_PRESSED) && (mods & RIGHT_ALT_PRESSED))
-    {
-      keystate[VK_CONTROL] = 0x80;
-      keystate[VK_LCONTROL] = 0x80;
-      keystate[VK_MENU] = 0x80;
-      keystate[VK_RMENU] = 0x80;
-    }
-
-#if 0
-  /* Because of an OS bug, ToAscii corrupts the stack when called to
-     convert a dead key in console mode on NT4.  Unfortunately, trying
-     to check for dead keys using MapVirtualKey doesn't work either -
-     these functions apparently use internal information about keyboard
-     layout which doesn't get properly updated in console programs when
-     changing layout (though apparently it gets partly updated,
-     otherwise ToAscii wouldn't crash).  */
-  if (is_dead_key (event->wVirtualKeyCode))
-    return 0;
-#endif
-
-  /* On NT, call ToUnicode instead and then convert to the current
-     locale's default codepage.  */
-  if (os_subtype == OS_NT)
-    {
-      WCHAR buf[128];
-
-      isdead = ToUnicode (event->wVirtualKeyCode, event->wVirtualScanCode,
-			  keystate, buf, 128, 0);
-      if (isdead > 0)
-	{
-	  char cp[20];
-	  int cpId;
-
-	  event->uChar.UnicodeChar = buf[isdead - 1];
-
-	  GetLocaleInfo (GetThreadLocale (),
-			 LOCALE_IDEFAULTANSICODEPAGE, cp, 20);
-	  cpId = atoi (cp);
-	  isdead = WideCharToMultiByte (cpId, 0, buf, isdead,
-					ansi_code, 4, NULL, NULL);
-	}
-      else
-	isdead = 0;
-    }
-  else
-    {
-      isdead = ToAscii (event->wVirtualKeyCode, event->wVirtualScanCode,
-                        keystate, (LPWORD) ansi_code, 0);
-    }
-
-  if (isdead == 0)
-    return 0;
-  event->uChar.AsciiChar = ansi_code[0];
-  return isdead;
-}
-
-
-static int faked_key = 0;
-
 /* return code -1 means that event_queue_ptr won't be incremented.
    In other word, this event makes two key codes.   (by himi)       */
 static int
@@ -510,32 +350,6 @@ key_event (KEY_EVENT_RECORD *event, struct input_event *emacs_ev, int *isdead)
   return 1;
 }
 
-int
-w32_console_toggle_lock_key (int vk_code, Lisp_Object new_state)
-{
-  int cur_state = (GetKeyState (vk_code) & 1);
-
-  if (NILP (new_state)
-      || (NUMBERP (new_state)
-	  && ((XUINT (new_state)) & 1) != cur_state))
-    {
-      faked_key = vk_code;
-
-      keybd_event ((BYTE) vk_code,
-		   (BYTE) MapVirtualKey (vk_code, 0),
-		   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
-      keybd_event ((BYTE) vk_code,
-		   (BYTE) MapVirtualKey (vk_code, 0),
-		   KEYEVENTF_EXTENDEDKEY | 0, 0);
-      keybd_event ((BYTE) vk_code,
-		   (BYTE) MapVirtualKey (vk_code, 0),
-		   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
-      cur_state = !cur_state;
-    }
-
-  return cur_state;
-}
-
 /* Mouse position hook.  */
 void
 w32_console_mouse_position (FRAME_PTR *f,
diff --git a/src/w32menu.c b/src/w32menu.c
index 9b2d014..fb0b20f 100644
--- a/src/w32menu.c
+++ b/src/w32menu.c
@@ -21,7 +21,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <signal.h>
 #include <stdio.h>
-#include <mbstring.h>
 #include <setjmp.h>
 
 #include "lisp.h"
@@ -40,6 +39,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
    if this is not done before the other system files.  */
 #include "w32term.h"
 
+#ifndef NTGUI_UNICODE
+#include <mbstring.h>
+#endif /* !NTGUI_UNICODE */
+
 /* Load sys/types.h if not already loaded.
    In some systems loading it twice is suicidal.  */
 #ifndef makedev
@@ -76,10 +79,17 @@ typedef int (WINAPI * MessageBoxW_Proc) (
     IN WCHAR *caption,
     IN UINT type);
 
+#ifdef NTGUI_UNICODE
+#define get_menu_item_info GetMenuItemInfoA
+#define set_menu_item_info SetMenuItemInfoA
+#define unicode_append_menu AppendMenuW
+#define unicode_message_box MessageBoxW
+#else /* !NTGUI_UNICODE */
 GetMenuItemInfoA_Proc get_menu_item_info = NULL;
 SetMenuItemInfoA_Proc set_menu_item_info = NULL;
 AppendMenuW_Proc unicode_append_menu = NULL;
 MessageBoxW_Proc unicode_message_box = NULL;
+#endif /* NTGUI_UNICODE */
 
 Lisp_Object Qdebug_on_next_call;
 
@@ -96,6 +106,7 @@ static void utf8to16 (unsigned char *, int, WCHAR *);
 static int fill_in_menu (HMENU, widget_value *);
 
 void w32_free_menu_strings (HWND);
+
 \f
 
 /* This is set nonzero after the user activates the menu bar, and set
@@ -1393,6 +1404,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                 nlen++;
             }
         }
+#ifndef NTGUI_UNICODE
       else
         {
           /* If encoded with the system codepage, use multibyte string
@@ -1403,6 +1415,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                 nlen++;
             }
         }
+#endif /* !NTGUI_UNICODE */
 
       if (nlen > orig_len)
         {
@@ -1417,6 +1430,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                     *q++ = *p;
                   *q++ = *p++;
                 }
+#ifndef NTGUI_UNICODE
               else
                 {
                   if (_mbsnextc (p) == '&')
@@ -1428,6 +1442,7 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
                   p = _mbsinc (p);
                   q = _mbsinc (q);
                 }
+#endif /* !NTGUI_UNICODE */
             }
           *q = '\0';
         }
@@ -1486,8 +1501,10 @@ add_menu_item (HMENU menu, widget_value *wv, HMENU item)
 	    AppendMenu (menu, fuFlags,
 			item != NULL ? (UINT) item: (UINT) wv->call_data,
 			out_string);
+#ifndef NTGUI_UNICODE
 	  /* Don't use Unicode menus in future.  */
 	  unicode_append_menu = NULL;
+#endif /* !NTGUI_UNICODE */
 	}
 
       if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
@@ -1709,10 +1726,12 @@ syms_of_w32menu (void)
 void
 globals_of_w32menu (void)
 {
+#ifndef NTGUI_UNICODE
   /* See if Get/SetMenuItemInfo functions are available.  */
   HMODULE user32 = GetModuleHandle ("user32.dll");
   get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA");
   set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA");
   unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW");
   unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW");
+#endif /* !NTGUI_UNICODE */
 }
diff --git a/src/w32proc.c b/src/w32proc.c
index 254a325..e8c985b 100644
--- a/src/w32proc.c
+++ b/src/w32proc.c
@@ -68,20 +68,6 @@ extern BOOL WINAPI IsValidLocale (LCID, DWORD);
 
 Lisp_Object Qhigh, Qlow;
 
-#ifdef EMACSDEBUG
-void
-_DebPrint (const char *fmt, ...)
-{
-  char buf[1024];
-  va_list args;
-
-  va_start (args, fmt);
-  vsprintf (buf, fmt, args);
-  va_end (args);
-  OutputDebugString (buf);
-}
-#endif
-
 typedef void (_CALLBACK_ *signal_handler) (int);
 
 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly.  */
diff --git a/src/w32select.c b/src/w32select.c
index 392cb07..6d96b53 100644
--- a/src/w32select.c
+++ b/src/w32select.c
@@ -82,6 +82,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "coding.h"
 #include "composite.h"
 
+#ifdef CYGWIN
+#include <string.h>
+#include <stdio.h>
+#define _memccpy memccpy
+#endif
 
 static HGLOBAL convert_to_handle_as_ascii (void);
 static HGLOBAL convert_to_handle_as_coded (Lisp_Object coding_system);
diff --git a/src/w32select.h b/src/w32select.h
new file mode 100644
index 0000000..6924d4d
--- /dev/null
+++ b/src/w32select.h
@@ -0,0 +1,30 @@
+/* Selection processing for Emacs on the Microsoft W32 API.
+
+Copyright (C) 1993-1994, 2001-2011  Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef W32SELECT_H
+#define W32SELECT_H
+#include <windows.h>
+
+#define HAVE_W32SELECT 1
+
+extern void syms_of_w32select (void);
+extern void globals_of_w32select (void);
+extern void term_w32select (void);
+
+#endif
diff --git a/src/w32term.c b/src/w32term.c
index 29ac9d1..9478aac 100644
--- a/src/w32term.c
+++ b/src/w32term.c
@@ -52,7 +52,14 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "atimer.h"
 #include "keymap.h"
 
+#ifdef WINDOWSNT
 #include "w32heap.h"
+#endif
+
+#ifndef WINDOWSNT
+#include <io.h> /* for get_osfhandle */
+#endif
+
 #include <shellapi.h>
 
 #include "font.h"
@@ -103,7 +110,7 @@ struct w32_display_info *x_display_list;
 Lisp_Object w32_display_name_list;
 
 
-#ifndef GLYPHSET
+#if !defined (GLYPHSET) && _WIN32_WINNT < 0x500
 /* Pre Windows 2000, this was not available, but define it here so
    that Emacs compiled on such a platform will run on newer versions.  */
 
@@ -122,7 +129,7 @@ typedef struct tagGLYPHSET
   WCRANGE ranges[1];
 } GLYPHSET;
 
-#endif
+#endif /* compiling for pre-Win2k */
 
 /* Dynamic linking to SetLayeredWindowAttribute (only since 2000).  */
 BOOL (WINAPI *pfnSetLayeredWindowAttributes) (HWND, COLORREF, BYTE, DWORD);
@@ -188,6 +195,10 @@ static int volatile input_signal_count;
 static int input_signal_count;
 #endif
 
+#ifdef CYGWIN
+int w32_message_fd = -1;
+#endif /* CYGWIN */
+
 /* Keyboard code page - may be changed by language-change events.  */
 static int keyboard_codepage;
 
@@ -4042,6 +4053,7 @@ w32_read_socket (struct terminal *terminal, int expected,
   struct frame *f;
   struct w32_display_info *dpyinfo = &one_w32_display_info;
   Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
+  static char buf[1];
 
   if (interrupt_input_blocked)
     {
@@ -4055,12 +4067,19 @@ w32_read_socket (struct terminal *terminal, int expected,
   /* So people can tell when we have read the available input.  */
   input_signal_count++;
 
+  /* Process any incoming thread messages.  */
+  drain_message_queue ();
+
   /* TODO: ghostscript integration. */
   while (get_next_msg (&msg, FALSE))
     {
       struct input_event inev;
       int do_help = 0;
 
+      /* DebPrint (("w32_read_socket: %s time:%u\n", */
+      /*            w32_name_of_message (msg.msg.message), */
+      /*            msg.msg.time)); */
+
       EVENT_INIT (inev);
       inev.kind = NO_EVENT;
       inev.arg = Qnil;
@@ -6171,8 +6190,15 @@ w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
     w32_defined_color (0, "black", &color, 1);
   }
 
-  /* Add the default keyboard.  */
+#ifdef WINDOWSNT
+  /* Add the default keyboard.  When !WINDOWSNT, we're using the
+     standard Emacs console handling machinery and don't need an
+     explicit FD here.  */
   add_keyboard_wait_descriptor (0);
+#elif CYGWIN
+  /* /dev/windows wakes us up when we have a thread message pending.  */
+  add_keyboard_wait_descriptor (w32_message_fd);
+#endif
 
   /* Create Fringe Bitmaps and store them for later use.
 
@@ -6183,15 +6209,6 @@ w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
      the bitmaps.  */
   w32_init_fringe (terminal->rif);
 
-#ifdef F_SETOWN
-  fcntl (connection, F_SETOWN, getpid ());
-#endif /* ! defined (F_SETOWN) */
-
-#ifdef SIGIO
-  if (interrupt_input)
-    init_sigio (connection);
-#endif /* ! defined (SIGIO) */
-
   UNBLOCK_INPUT;
 
   return dpyinfo;
@@ -6241,6 +6258,7 @@ x_delete_display (struct w32_display_info *dpyinfo)
 
   w32_reset_fringes ();
 }
+
 \f
 /* Set up use of W32.  */
 
@@ -6278,6 +6296,11 @@ w32_initialize (void)
 	set_user_model (L"GNU.Emacs");
     }
 
+#ifdef CYGWIN
+  if ((w32_message_fd = open ("/dev/windows", O_RDWR | O_CLOEXEC)) == -1)
+    fatal ("opening /dev/windows: %s", strerror (errno));
+#endif /* CYGWIN */
+
   /* Initialize w32_use_visible_system_caret based on whether a screen
      reader is in use.  */
   if (!SystemParametersInfo (SPI_GETSCREENREADER, 0,
@@ -6437,4 +6460,6 @@ With MS Windows, the value is t.  */);
 
   staticpro (&last_mouse_motion_frame);
   last_mouse_motion_frame = Qnil;
+
+  Fprovide (intern_c_string ("w32"), Qnil);
 }
diff --git a/src/w32term.h b/src/w32term.h
index e3429fc..f2590d4 100644
--- a/src/w32term.h
+++ b/src/w32term.h
@@ -19,6 +19,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 /* Added by Kevin Gallo */
 
 #include "w32gui.h"
+#include "frame.h"
+#include "atimer.h"
 
 \f
 #define BLACK_PIX_DEFAULT(f) PALETTERGB(0,0,0)
@@ -195,11 +197,47 @@ Lisp_Object display_x_get_resource (struct w32_display_info *,
                                     Lisp_Object, Lisp_Object,
                                     Lisp_Object, Lisp_Object);
 
+extern void x_focus_on_frame (struct frame *f);
+
+/* also defined in xterm.h XXX: factor out to common header */
+
 extern struct w32_display_info *w32_term_init (Lisp_Object,
 					       char *, char *);
-
+extern void check_w32 (void);
+extern int w32_defined_color (FRAME_PTR f, const char *color,
+                              XColor *color_def, int alloc);
+extern void set_frame_menubar (struct frame *f, int first_time, int deep_p);
+extern void x_set_window_size (struct frame *f, int change_grav,
+                              int cols, int rows);
 extern int x_display_pixel_height (struct w32_display_info *);
 extern int x_display_pixel_width (struct w32_display_info *);
+extern void x_sync (struct frame *);
+extern Lisp_Object x_get_focus_frame (struct frame *);
+extern void x_set_mouse_position (struct frame *f, int h, int v);
+extern void x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y);
+extern void x_make_frame_visible (struct frame *f);
+extern void x_make_frame_invisible (struct frame *f);
+extern void x_iconify_frame (struct frame *f);
+extern int x_char_width (struct frame *f);
+extern int x_char_height (struct frame *f);
+extern int x_pixel_width (struct frame *f);
+extern int x_pixel_height (struct frame *f);
+extern void x_set_frame_alpha (struct frame *f);
+extern void x_set_menu_bar_lines (struct frame *, Lisp_Object, Lisp_Object);
+extern void x_set_tool_bar_lines (struct frame *f,
+                                  Lisp_Object value,
+                                  Lisp_Object oldval);
+extern void x_activate_menubar (struct frame *);
+extern int x_bitmap_icon (struct frame *, Lisp_Object);
+extern void initialize_frame_menubar (struct frame *);
+extern void x_free_frame_resources (struct frame *);
+extern void x_wm_set_size_hint (struct frame *, long, int);
+extern void x_real_positions (struct frame *, int *, int *);
+
+/* w32inevt.c */
+extern int w32_kbd_patch_key (KEY_EVENT_RECORD *event);
+extern int w32_kbd_mods_to_emacs (DWORD mods, WORD key);
+
 
 \f
 #define PIX_TYPE COLORREF
@@ -576,7 +614,8 @@ do { \
 #define WM_EMACS_HIDE_CARET            (WM_EMACS_START + 18)
 #define WM_EMACS_SETCURSOR             (WM_EMACS_START + 19)
 #define WM_EMACS_PAINT                 (WM_EMACS_START + 20)
-#define WM_EMACS_END                   (WM_EMACS_START + 21)
+#define WM_EMACS_INPUT_READY           (WM_EMACS_START + 21)
+#define WM_EMACS_END                   (WM_EMACS_START + 22)
 
 #define WND_FONTWIDTH_INDEX    (0)
 #define WND_LINEHEIGHT_INDEX   (4)
@@ -598,6 +637,8 @@ typedef struct W32Msg {
     RECT rect;
 } W32Msg;
 
+extern BOOL prepend_msg (W32Msg *lpmsg);
+
 /* Structure for recording message when input thread must return a
    result that depends on lisp thread to compute.  Lisp thread can
    complete deferred messages out of order.  */
@@ -701,3 +742,27 @@ extern HWND w32_system_caret_hwnd;
 extern int w32_system_caret_height;
 extern int w32_system_caret_x;
 extern int w32_system_caret_y;
+
+#if EMACSDEBUG
+extern const char*
+w32_name_of_message (UINT msg);
+#endif /* EMACSDEBUG */
+
+extern void syms_of_w32term (void);
+extern void syms_of_w32menu (void);
+extern void syms_of_w32fns (void);
+
+extern void globals_of_w32menu (void);
+extern void globals_of_w32fns (void);
+
+/* Ordinarily, we'd just send SIGIO to ourselves when we have input,
+ * terminating the main thread's normal select call with EINTR.
+ * However, Cygwin has a known bug [1] that causes us to crash when
+ * sending a signal to our main thread from our message pump thread.
+ * The workaround is to use an old-fashioned self-pipe.
+ *
+ * [1] <loom.20100428T060408-563@post.gmane.org>
+ */
+#ifdef CYGWIN
+extern int w32_message_fd;
+#endif /* CYGWIN */
diff --git a/src/w32xfns.c b/src/w32xfns.c
index fc2d590..d2db30e 100644
--- a/src/w32xfns.c
+++ b/src/w32xfns.c
@@ -20,6 +20,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <signal.h>
 #include <stdio.h>
 #include <setjmp.h>
+
 #include "lisp.h"
 #include "keyboard.h"
 #include "frame.h"
@@ -33,7 +34,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #define myfree(lp) GlobalFreePtr (lp)
 
 CRITICAL_SECTION critsect;
+
+#if WINDOWSNT
 extern HANDLE keyboard_handle;
+#endif
+
 HANDLE input_available = NULL;
 HANDLE interrupt_handle = NULL;
 
@@ -44,7 +49,11 @@ init_crit (void)
 
   /* For safety, input_available should only be reset by get_next_msg
      when the input queue is empty, so make it a manual reset event. */
-  keyboard_handle = input_available = CreateEvent (NULL, TRUE, FALSE, NULL);
+  input_available = CreateEvent (NULL, TRUE, FALSE, NULL);
+
+#if WINDOWSNT
+  keyboard_handle = input_available;
+#endif
 
   /* interrupt_handle is signaled when quit (C-g) is detected, so that
      blocking system calls can be interrupted.  We make it a manual
@@ -241,6 +250,22 @@ get_next_msg (W32Msg * lpmsg, BOOL bWait)
   return (bRet);
 }
 
+extern char * w32_strerror (int error_no);
+
+/* Tell the main thread that we have input available; if the main
+   thread is blocked in select(), we wake it up here.  */
+static void
+notify_msg_ready (void)
+{
+  SetEvent (input_available);
+
+#ifdef CYGWIN
+  /* Wakes up the main thread, which is blocked select()ing for /dev/windows,
+     among other files.  */
+  (void) PostThreadMessage (dwMainThreadId, WM_EMACS_INPUT_READY, 0, 0);
+#endif /* CYGWIN */
+}
+
 BOOL
 post_msg (W32Msg * lpmsg)
 {
@@ -264,8 +289,7 @@ post_msg (W32Msg * lpmsg)
     }
 
   lpTail = lpNew;
-  SetEvent (input_available);
-
+  notify_msg_ready ();
   leave_crit ();
 
   return (TRUE);
@@ -286,7 +310,7 @@ prepend_msg (W32Msg *lpmsg)
   nQueue++;
   lpNew->lpNext = lpHead;
   lpHead = lpNew;
-
+  notify_msg_ready ();
   leave_crit ();
 
   return (TRUE);
@@ -438,6 +462,6 @@ XParseGeometry (char *string,
 
 /* x_sync is a no-op on W32.  */
 void
-x_sync (void *f)
+x_sync (struct frame *f)
 {
 }
diff --git a/src/window.c b/src/window.c
index ef61d66..34d9de7 100644
--- a/src/window.c
+++ b/src/window.c
@@ -40,7 +40,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
 #endif	/* HAVE_X_WINDOWS */
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 #include "w32term.h"
 #endif
 #ifdef MSDOS
diff --git a/src/xdisp.c b/src/xdisp.c
index 90375ba..1ae99c6 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -302,7 +302,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
 #endif
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 #include "w32term.h"
 #endif
 #ifdef HAVE_NS
@@ -28672,7 +28672,7 @@ init_xdisp (void)
 
 /* Since w32 does not support atimers, it defines its own implementation of
    the following three functions in w32fns.c.  */
-#ifndef WINDOWSNT
+#ifndef HAVE_NTGUI
 
 /* Platform-independent portion of hourglass implementation. */
 
@@ -28731,4 +28731,4 @@ cancel_hourglass (void)
     hide_hourglass ();
 #endif
 }
-#endif /* ! WINDOWSNT  */
+#endif /* ! HAVE_NTGUI  */
diff --git a/src/xfaces.c b/src/xfaces.c
index 1da31ae..1b0d3aa 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -228,14 +228,14 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_WINDOW_SYSTEM
 #include TERM_HEADER
 #include "fontset.h"
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 #undef FRAME_X_DISPLAY_INFO
 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
 #define x_display_info w32_display_info
 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
 #define check_x check_w32
 #define GCGraphicsExposures 0
-#endif /* WINDOWSNT */
+#endif /* HAVE_NTGUI */
 
 #ifdef HAVE_NS
 #undef FRAME_X_DISPLAY_INFO
@@ -664,7 +664,7 @@ x_free_gc (struct frame *f, GC gc)
 
 #endif /* HAVE_X_WINDOWS */
 
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 /* W32 emulation of GCs */
 
 static inline GC
@@ -688,7 +688,7 @@ x_free_gc (struct frame *f, GC gc)
   xfree (gc);
 }
 
-#endif  /* WINDOWSNT */
+#endif  /* HAVE_NTGUI */
 
 #ifdef HAVE_NS
 /* NS emulation of GCs */
@@ -782,7 +782,7 @@ init_frame_faces (struct frame *f)
 #ifdef HAVE_X_WINDOWS
   if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
 #endif
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
 #endif
 #ifdef HAVE_NS
@@ -1161,7 +1161,7 @@ defined_color (struct frame *f, const char *color_name, XColor *color_def,
   else if (FRAME_X_P (f))
     return x_defined_color (f, color_name, color_def, alloc);
 #endif
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
   else if (FRAME_W32_P (f))
     return w32_defined_color (f, color_name, color_def, alloc);
 #endif
@@ -3253,7 +3253,7 @@ FRAME 0 means change the face on all frames, and change the default
 	    param = Qbackground_color;
 	}
 #ifdef HAVE_WINDOW_SYSTEM
-#ifndef WINDOWSNT
+#ifndef HAVE_NTGUI
       else if (EQ (face, Qscroll_bar))
 	{
 	  /* Changing the colors of `scroll-bar' sets frame parameters
@@ -3263,7 +3263,7 @@ FRAME 0 means change the face on all frames, and change the default
 	  else if (EQ (attr, QCbackground))
 	    param = Qscroll_bar_background;
 	}
-#endif /* not WINDOWSNT */
+#endif /* not HAVE_NTGUI */
       else if (EQ (face, Qborder))
 	{
 	  /* Changing background color of `border' sets frame parameter
@@ -6323,7 +6323,7 @@ where R,G,B are numbers between 0 and 255 and name is an arbitrary string.  */)
 	    if (num >= 0 && name[num] == '\n')
 	      name[num] = 0;
 	    cmap = Fcons (Fcons (build_string (name),
-#ifdef WINDOWSNT
+#ifdef HAVE_NTGUI
 				 make_number (RGB (red, green, blue))),
 #else
 				 make_number ((red << 16) | (green << 8) | blue)),
diff --git a/src/xterm.h b/src/xterm.h
index 680d1d8..8638472 100644
--- a/src/xterm.h
+++ b/src/xterm.h
@@ -979,7 +979,6 @@ extern int x_alloc_lighter_color_for_widget (Widget, Display *, Colormap,
                                              double, int);
 #endif
 extern int x_alloc_nearest_color (struct frame *, Colormap, XColor *);
-extern void x_query_color (struct frame *f, XColor *);
 extern void x_clear_area (Display *, Window, int, int, int, int, int);
 #if defined HAVE_MENUS && !defined USE_X_TOOLKIT && !defined USE_GTK
 extern void x_mouse_leave (struct x_display_info *);
-- 
1.7.5.1





^ permalink raw reply related	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
@ 2011-12-29 16:28   ` Juanma Barranquero
  2011-12-29 16:36     ` Daniel Colascione
  2011-12-29 17:39   ` Eli Zaretskii
  1 sibling, 1 reply; 42+ messages in thread
From: Juanma Barranquero @ 2011-12-29 16:28 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

On Thu, Dec 29, 2011 at 15:03, Daniel Colascione <dancol@dancol.org> wrote:

> -                 ;; On Windows, emacsclient always asks for a tty frame.
> -                 ;; If running a GUI server, force the frame type to GUI.
> -                 (when (eq window-system 'w32)
> +                 ;; On Windows, emacsclient always asks for a tty
> +                 ;; frame.  If running a GUI server, force the frame
> +                 ;; type to GUI.  (Cygwin is perfectly happy with
> +                 ;; multi-tty support, so don't override the user's
> +                 ;; choice there.)
> +                 (when (and (eq system-type 'windows-nt)
> +                            (eq window-system 'w32))

Don't you need to fix also this?

      ;; Delete the client's tty, except on Windows (both GUI and console),
      ;; where there's only one terminal and does not make sense to delete it.
      (unless (eq system-type 'windows-nt)
	(let ((terminal (process-get proc 'terminal)))
	  ;; Only delete the terminal if it is non-nil.
	  (when (and terminal (eq (terminal-live-p terminal) t))
	    (delete-terminal terminal))))

    Juanma



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 16:28   ` Juanma Barranquero
@ 2011-12-29 16:36     ` Daniel Colascione
  2011-12-29 16:43       ` Juanma Barranquero
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 16:36 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1515 bytes --]

On 12/29/11 8:28 AM, Juanma Barranquero wrote:
> On Thu, Dec 29, 2011 at 15:03, Daniel Colascione <dancol@dancol.org> wrote:
> 
>> -                 ;; On Windows, emacsclient always asks for a tty frame.
>> -                 ;; If running a GUI server, force the frame type to GUI.
>> -                 (when (eq window-system 'w32)
>> +                 ;; On Windows, emacsclient always asks for a tty
>> +                 ;; frame.  If running a GUI server, force the frame
>> +                 ;; type to GUI.  (Cygwin is perfectly happy with
>> +                 ;; multi-tty support, so don't override the user's
>> +                 ;; choice there.)
>> +                 (when (and (eq system-type 'windows-nt)
>> +                            (eq window-system 'w32))


This hunk narrows the scope of a windows-nt special case so that it
doesn't apply to Cygwin.

> Don't you need to fix also this?
> 
>       ;; Delete the client's tty, except on Windows (both GUI and console),
>       ;; where there's only one terminal and does not make sense to delete it.
>       (unless (eq system-type 'windows-nt)
> 	(let ((terminal (process-get proc 'terminal)))
> 	  ;; Only delete the terminal if it is non-nil.
> 	  (when (and terminal (eq (terminal-live-p terminal) t))
> 	    (delete-terminal terminal))))
> 
>     Juanma
> 

No --- a Cygwin tty acts like a tty on any other unix, so we don't want
to treat it specially.  The comment refers to native Windows consoles only.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 16:36     ` Daniel Colascione
@ 2011-12-29 16:43       ` Juanma Barranquero
  2011-12-29 16:47         ` Daniel Colascione
  0 siblings, 1 reply; 42+ messages in thread
From: Juanma Barranquero @ 2011-12-29 16:43 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

On Thu, Dec 29, 2011 at 17:36, Daniel Colascione <dancol@dancol.org> wrote:

> This hunk narrows the scope of a windows-nt special case so that it
> doesn't apply to Cygwin.

Yes, I understand that.

> No --- a Cygwin tty acts like a tty on any other unix, so we don't want
> to treat it specially.

What is the system-type then? cygw32?

>  The comment refers to native Windows consoles only.

I know, I wrote that comment ;-)

    Juanma



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 16:43       ` Juanma Barranquero
@ 2011-12-29 16:47         ` Daniel Colascione
  2011-12-29 16:49           ` Juanma Barranquero
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 16:47 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 515 bytes --]

On 12/29/11 8:43 AM, Juanma Barranquero wrote:
> On Thu, Dec 29, 2011 at 17:36, Daniel Colascione <dancol@dancol.org> wrote:
> 
>> This hunk narrows the scope of a windows-nt special case so that it
>> doesn't apply to Cygwin.
> 
> Yes, I understand that.

Ecks. Maybe I've been up too long.

>> No --- a Cygwin tty acts like a tty on any other unix, so we don't want
>> to treat it specially.
> 
> What is the system-type then? cygw32?

The system-type is cygwin, and the window-system is w32.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 16:47         ` Daniel Colascione
@ 2011-12-29 16:49           ` Juanma Barranquero
  0 siblings, 0 replies; 42+ messages in thread
From: Juanma Barranquero @ 2011-12-29 16:49 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

On Thu, Dec 29, 2011 at 17:47, Daniel Colascione <dancol@dancol.org> wrote:

> The system-type is cygwin, and the window-system is w32.

Silly me. Sorry for the noise.

    Juanma



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 14:03 ` [PATCH 3/4] Implement cygw32 Daniel Colascione
@ 2011-12-29 17:29   ` Eli Zaretskii
  2011-12-29 17:53     ` Daniel Colascione
  2011-12-30  9:49   ` Andreas Schwab
  1 sibling, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-29 17:29 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Thu, 29 Dec 2011 06:03:20 -0800
> From: Daniel Colascione <dancol@dancol.org>
> 
> --- a/lisp/faces.el
> +++ b/lisp/faces.el
> @@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
>  ;; This is defined originally in xfaces.c.
>  (defcustom face-font-registry-alternatives
>    (mapcar (lambda (arg) (mapcar 'purecopy arg))
> -  (if (eq system-type 'windows-nt)
> +  (if (featurep 'w32)
>        '(("iso8859-1" "ms-oemlatin")
>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")

Why not use window-system (the function) here?

> --- a/lisp/frame.el
> +++ b/lisp/frame.el
> @@ -522,7 +522,7 @@ The optional argument PARAMETERS specifies additional frame parameters."
>  	   (ns-initialize-window-system))
>  	 (make-frame `((window-system . ns)
>  		       (display . ,display) . ,parameters)))
> -	((eq system-type 'windows-nt)
> +	((eq window-system 'w32)
>  	 ;; On Windows, ignore DISPLAY.
>  	 (make-frame parameters))
>  	(t

Why not window-system the function?  You do want to continue
supporting multi-tty in this configuration, right?

> --- a/lisp/international/mule-cmds.el
> +++ b/lisp/international/mule-cmds.el
> @@ -2655,7 +2655,8 @@ See also `locale-charset-language-names', `locale-language-names',
>      ;; On Windows, override locale-coding-system,
>      ;; default-file-name-coding-system, keyboard-coding-system,
>      ;; terminal-coding-system with system codepage.
> -    (when (boundp 'w32-ansi-code-page)
> +    (when (and (eq system-type 'windows-nt)
> +               (boundp 'w32-ansi-code-page))
>        (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))

Can you explain the need for this change?

> --- a/lisp/loadup.el
> +++ b/lisp/loadup.el
> @@ -207,15 +207,18 @@
>        (load "term/common-win")
>        (load "term/x-win")))
>  
> -(if (eq system-type 'windows-nt)
> +(if (or (eq system-type 'windows-nt)
> +        (featurep 'w32))
>      (progn
> -      (load "w32-vars")
>        (load "term/common-win")
> +      (load "w32-vars")

Did you really need this order change?  If yes, why?  If not, I'd
prefer to leave the original order, as changing it could potentially
cause unintended consequences.

>        (load "term/w32-win")
> -      (load "ls-lisp")
>        (load "disp-table")
> -      (load "dos-w32")
> -      (load "w32-fns")))
> +      (load "w32-common-fns")
> +      (when (eq system-type 'windows-nt)
> +        (load "w32-fns")
> +        (load "ls-lisp")
> +        (load "dos-w32"))))

Likewise here: at the very least, keep the order in the group of
packages loaded for windows-nt.

> --- a/lisp/mouse.el
> +++ b/lisp/mouse.el
> @@ -1147,7 +1147,7 @@ regardless of where you click."
>    (or mouse-yank-at-point (mouse-set-point click))
>    (let ((primary
>  	 (cond
> -	  ((eq system-type 'windows-nt)
> +	  ((eq (framep (selected-frame)) 'w32)
>  	   ;; MS-Windows emulates PRIMARY in x-get-selection, but not
>  	   ;; in x-get-selection-value (the latter only accesses the
>  	   ;; clipboard).  So try PRIMARY first, in case they selected

You mean, the Cygwin build that uses w32 windowing will be unable to
support X selections?  That would be a pity.

> +(defun w32-handle-dropped-file (window file-name)
> +  (let ((f (if (eq system-type 'cygwin)
> +               (cygwin-convert-path-from-windows file-name t)
> +             (subst-char-in-string ?\\ ?/ file-name)))
> +        (coding (or file-name-coding-system
> +                    default-file-name-coding-system)))

What is the default value of default-file-name-coding-system in the
Cygwin build?  There could be a conflict here between e.g. UTF-8 in
Cygwin and the Windows codepage.

Also, it would be good to have a doc string for this function.

> +DEFUN ("cygwin-convert-path-to-windows",
> +       Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
> +       1, 2, 0,
> +       doc: /* Convert PATH to a Windows path.  If ABSOLUTE-P if
> +               non-nil, return an absolute path.*/)
> +  (Lisp_Object path, Lisp_Object absolute_p)
> +{
> +  return from_unicode (
> +    conv_filename_to_w32_unicode (path, absolute_p == Qnil ? 0 : 1));
> +}
> +
> +DEFUN ("cygwin-convert-path-from-windows",
> +       Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
> +       1, 2, 0,
> +       doc: /* Convert a Windows path to a Cygwin path.  If ABSOLUTE-P
> +               if non-nil, return an absolute path.*/)
> +  (Lisp_Object path, Lisp_Object absolute_p)
> +{
> +  return conv_filename_from_w32_unicode (to_unicode (path, &path),
> +                                         absolute_p == Qnil ? 0 : 1);
> +}

These two should probably be documented in the ELisp manual.

>  	  char buffer[16];
> -	  _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
> +	  snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
>  	  load_percentage = build_string (buffer);

If you want to use snprintf without the leading underscore, please add
a suitable #define somewhere for the benefit of the MSVC compiler,
which doesn't have snprintf, only _snprintf.

Btw, will the above code be used by the Cygwin build?  Does that
include default-printer-name etc.?



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
  2011-12-29 16:28   ` Juanma Barranquero
@ 2011-12-29 17:39   ` Eli Zaretskii
  2011-12-29 17:56     ` Daniel Colascione
  1 sibling, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-29 17:39 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Thu, 29 Dec 2011 06:03:20 -0800
> From: Daniel Colascione <dancol@dancol.org>
> 
> @@ -184,7 +188,7 @@ struct option longopts[] =
>    { "socket-name",	required_argument, NULL, 's' },
>  #endif
>    { "server-file",	required_argument, NULL, 'f' },
> -#ifndef WINDOWSNT
> +#ifndef HAVE_NTGUI
>    { "display",	required_argument, NULL, 'd' },
>  #endif

Maybe I'm confused: will HAVE_NTGUI be applicable to the Cygwin build
that can use the w32 windowing?  If so, it sounds like you are
creating here 2 kinds of emacsclient: one that can talk to a Cygwin
Emacs with X frames on another display, the other that cannot.  Why
cannot the same emacsclient work with both types of Emacs frames in a
Cygwin Emacs?

Apologies if I've misread the changes.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 17:29   ` Eli Zaretskii
@ 2011-12-29 17:53     ` Daniel Colascione
  2011-12-29 18:17       ` Eli Zaretskii
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 17:53 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 7997 bytes --]

On 12/29/11 9:29 AM, Eli Zaretskii wrote:
>> Date: Thu, 29 Dec 2011 06:03:20 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>>
>> --- a/lisp/faces.el
>> +++ b/lisp/faces.el
>> @@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
>>  ;; This is defined originally in xfaces.c.
>>  (defcustom face-font-registry-alternatives
>>    (mapcar (lambda (arg) (mapcar 'purecopy arg))
>> -  (if (eq system-type 'windows-nt)
>> +  (if (featurep 'w32)
>>        '(("iso8859-1" "ms-oemlatin")
>>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
>>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
> 
> Why not use window-system (the function) here?

Isn't it a little early for window-system at this point?  This
particular block doesn't have an immediate effect anyway: it just
registers a set of registry synonyms, which will be just as valid later.

>> --- a/lisp/frame.el
>> +++ b/lisp/frame.el
>> @@ -522,7 +522,7 @@ The optional argument PARAMETERS specifies additional frame parameters."
>>  	   (ns-initialize-window-system))
>>  	 (make-frame `((window-system . ns)
>>  		       (display . ,display) . ,parameters)))
>> -	((eq system-type 'windows-nt)
>> +	((eq window-system 'w32)
>>  	 ;; On Windows, ignore DISPLAY.
>>  	 (make-frame parameters))
>>  	(t
> 
> Why not window-system the function?  You do want to continue
> supporting multi-tty in this configuration, right?

I've tested multi-tty and it works fine. There's no support for multiple
displays, but that's a given with w32.

> 
>> --- a/lisp/international/mule-cmds.el
>> +++ b/lisp/international/mule-cmds.el
>> @@ -2655,7 +2655,8 @@ See also `locale-charset-language-names', `locale-language-names',
>>      ;; On Windows, override locale-coding-system,
>>      ;; default-file-name-coding-system, keyboard-coding-system,
>>      ;; terminal-coding-system with system codepage.
>> -    (when (boundp 'w32-ansi-code-page)
>> +    (when (and (eq system-type 'windows-nt)
>> +               (boundp 'w32-ansi-code-page))
>>        (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
> 
> Can you explain the need for this change?

Cygwin communicates locale information through environment variables as
most unix systems do.  The coding system derived from this locale
information is the one that should be used to configure Emacs' language
environment because it's the one Cygwin expects to be used with its
APIs.  When Emacs is configured with system-type cygwin and
window-system w32, it still provides w32-ansi-code-page.  The hunk above
forces Emacs to use the Windows code page only when it's really a native
Windows program.

> 
>> --- a/lisp/loadup.el
>> +++ b/lisp/loadup.el
>> @@ -207,15 +207,18 @@
>>        (load "term/common-win")
>>        (load "term/x-win")))
>>  
>> -(if (eq system-type 'windows-nt)
>> +(if (or (eq system-type 'windows-nt)
>> +        (featurep 'w32))
>>      (progn
>> -      (load "w32-vars")
>>        (load "term/common-win")
>> +      (load "w32-vars")
> 
> Did you really need this order change?  If yes, why?  If not, I'd
> prefer to leave the original order, as changing it could potentially
> cause unintended consequences.

No, moving w32-vars back on top shouldn't cause a problem.  It really
doesn't belong there though: shouldn't we load the more general code
first, then let the platform-specific code muck with it?

>>        (load "term/w32-win")
>> -      (load "ls-lisp")
>>        (load "disp-table")
>> -      (load "dos-w32")
>> -      (load "w32-fns")))
>> +      (load "w32-common-fns")
>> +      (when (eq system-type 'windows-nt)
>> +        (load "w32-fns")
>> +        (load "ls-lisp")
>> +        (load "dos-w32"))))
> 
> Likewise here: at the very least, keep the order in the group of
> packages loaded for windows-nt.

I can play with the order a bit, but to avoid code duplication, I'd
strongly prefer to maintain the separation of the code in w32-common-fns
and w32-fns.  This separation necessarily involves changing the order in
which some definitions appear, but the difference hasn't seemed to cause
any trouble so far.

>> --- a/lisp/mouse.el
>> +++ b/lisp/mouse.el
>> @@ -1147,7 +1147,7 @@ regardless of where you click."
>>    (or mouse-yank-at-point (mouse-set-point click))
>>    (let ((primary
>>  	 (cond
>> -	  ((eq system-type 'windows-nt)
>> +	  ((eq (framep (selected-frame)) 'w32)
>>  	   ;; MS-Windows emulates PRIMARY in x-get-selection, but not
>>  	   ;; in x-get-selection-value (the latter only accesses the
>>  	   ;; clipboard).  So try PRIMARY first, in case they selected
> 
> You mean, the Cygwin build that uses w32 windowing will be unable to
> support X selections?  That would be a pity.

Why would it? Emacs in this configuration is a native Windows
application and it doesn't know a thing about X selections.  Any
improvements to the X selection emulation would apply equally well to
both cygwin and windows-nt configurations of the w32 window-system.

>> +(defun w32-handle-dropped-file (window file-name)
>> +  (let ((f (if (eq system-type 'cygwin)
>> +               (cygwin-convert-path-from-windows file-name t)
>> +             (subst-char-in-string ?\\ ?/ file-name)))
>> +        (coding (or file-name-coding-system
>> +                    default-file-name-coding-system)))
> 
> What is the default value of default-file-name-coding-system in the
> Cygwin build?  There could be a conflict here between e.g. UTF-8 in
> Cygwin and the Windows codepage.

In Cygwin, default-file-name-coding-system will almost always be
utf-8-unix because the default system locale is UTF-8.

This particular function is a bit hairy, however: FILE-NAME is _already_
decoded.  The only reason we're mucking with the coding system here is
that dnd-handle-one-url unconditionally decodes its argument using (or
file-name-coding-system default-file-name-coding-system), so we have to
re-encode what we give it.  (I don't want to bind
file-name-coding-system because that would affect the entire file
loading process.)

> Also, it would be good to have a doc string for this function.

Sure thing.

> 
>> +DEFUN ("cygwin-convert-path-to-windows",
>> +       Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
>> +       1, 2, 0,
>> +       doc: /* Convert PATH to a Windows path.  If ABSOLUTE-P if
>> +               non-nil, return an absolute path.*/)
>> +  (Lisp_Object path, Lisp_Object absolute_p)
>> +{
>> +  return from_unicode (
>> +    conv_filename_to_w32_unicode (path, absolute_p == Qnil ? 0 : 1));
>> +}
>> +
>> +DEFUN ("cygwin-convert-path-from-windows",
>> +       Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
>> +       1, 2, 0,
>> +       doc: /* Convert a Windows path to a Cygwin path.  If ABSOLUTE-P
>> +               if non-nil, return an absolute path.*/)
>> +  (Lisp_Object path, Lisp_Object absolute_p)
>> +{
>> +  return conv_filename_from_w32_unicode (to_unicode (path, &path),
>> +                                         absolute_p == Qnil ? 0 : 1);
>> +}
> 
> These two should probably be documented in the ELisp manual.

Along with Changelog entries, NEWS entries, and so on.

>>  	  char buffer[16];
>> -	  _snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
>> +	  snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
>>  	  load_percentage = build_string (buffer);
> 
> If you want to use snprintf without the leading underscore, please add
> a suitable #define somewhere for the benefit of the MSVC compiler,
> which doesn't have snprintf, only _snprintf.

Good catch. I'll add a preprocessor macro.

> Btw, will the above code be used by the Cygwin build?  Does that
> include default-printer-name etc.?

The battery status functionality works fine under the Cygwin build, as
does default-printer-name. (I haven't tried to actually print anything.)


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 1/4] Compilation cleanups
  2011-12-29 14:03 ` [PATCH 1/4] Compilation cleanups Daniel Colascione
@ 2011-12-29 17:54   ` Paul Eggert
  2011-12-31 14:50     ` Ken Brown
  0 siblings, 1 reply; 42+ messages in thread
From: Paul Eggert @ 2011-12-29 17:54 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

On 12/29/11 06:03, Daniel Colascione wrote:

> --- a/lib-src/update-game-score.c
> +++ b/lib-src/update-game-score.c
> @@ -48,8 +48,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
>  #include <sys/stat.h>
>  
>  /* Needed for SunOS4, for instance.  */
> +#if !defined(CYGWIN)
>  extern char *optarg;
>  extern int optind, opterr;
> +#endif // !defined(CYGWIN)

I suggest replacing those lines with "#include <getopt.h>" instead;
that's what the other .c files do, and it's cleaner.


> --- a/src/gmalloc.c
> +++ b/src/gmalloc.c
...
> -  if (ptr < _heapbase)
> +  if ((char*)ptr < (char*)_heapbase)

No need to cast _heapbase as it's already a char *.
And please put a space after the ')', e.g.,
"(char*) ptr < _heapbase", as that's the usual
Emacs style.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 17:39   ` Eli Zaretskii
@ 2011-12-29 17:56     ` Daniel Colascione
  2011-12-29 18:18       ` Eli Zaretskii
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 17:56 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1032 bytes --]

On 12/29/11 9:39 AM, Eli Zaretskii wrote:
>> Date: Thu, 29 Dec 2011 06:03:20 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>>
>> @@ -184,7 +188,7 @@ struct option longopts[] =
>>    { "socket-name",	required_argument, NULL, 's' },
>>  #endif
>>    { "server-file",	required_argument, NULL, 'f' },
>> -#ifndef WINDOWSNT
>> +#ifndef HAVE_NTGUI
>>    { "display",	required_argument, NULL, 'd' },
>>  #endif
> 
> Maybe I'm confused: will HAVE_NTGUI be applicable to the Cygwin build
> that can use the w32 windowing?  If so, it sounds like you are
> creating here 2 kinds of emacsclient: one that can talk to a Cygwin
> Emacs with X frames on another display, the other that cannot.  Why
> cannot the same emacsclient work with both types of Emacs frames in a
> Cygwin Emacs?
> 
> Apologies if I've misread the changes.

No, you're right.  I was following the template of the existing code
here; IIUC, removing the conditional in the hunk above will let the same
emacsclient talk to any kind of Emacs.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 17:53     ` Daniel Colascione
@ 2011-12-29 18:17       ` Eli Zaretskii
  2011-12-29 21:50         ` Daniel Colascione
  0 siblings, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-29 18:17 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Thu, 29 Dec 2011 09:53:03 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: emacs-devel@gnu.org
> 
> >> --- a/lisp/faces.el
> >> +++ b/lisp/faces.el
> >> @@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
> >>  ;; This is defined originally in xfaces.c.
> >>  (defcustom face-font-registry-alternatives
> >>    (mapcar (lambda (arg) (mapcar 'purecopy arg))
> >> -  (if (eq system-type 'windows-nt)
> >> +  (if (featurep 'w32)
> >>        '(("iso8859-1" "ms-oemlatin")
> >>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
> >>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
> > 
> > Why not use window-system (the function) here?
> 
> Isn't it a little early for window-system at this point?

A defcustom is evaluated more than once.  So I'm a bit fuzzy about
"too early" ;-)

> >> --- a/lisp/frame.el
> >> +++ b/lisp/frame.el
> >> @@ -522,7 +522,7 @@ The optional argument PARAMETERS specifies additional frame parameters."
> >>  	   (ns-initialize-window-system))
> >>  	 (make-frame `((window-system . ns)
> >>  		       (display . ,display) . ,parameters)))
> >> -	((eq system-type 'windows-nt)
> >> +	((eq window-system 'w32)
> >>  	 ;; On Windows, ignore DISPLAY.
> >>  	 (make-frame parameters))
> >>  	(t
> > 
> > Why not window-system the function?  You do want to continue
> > supporting multi-tty in this configuration, right?
> 
> I've tested multi-tty and it works fine.

Including X and w32 frames (on the same display)?

> When Emacs is configured with system-type cygwin and window-system
> w32, it still provides w32-ansi-code-page.

What for?

> >> --- a/lisp/loadup.el
> >> +++ b/lisp/loadup.el
> >> @@ -207,15 +207,18 @@
> >>        (load "term/common-win")
> >>        (load "term/x-win")))
> >>  
> >> -(if (eq system-type 'windows-nt)
> >> +(if (or (eq system-type 'windows-nt)
> >> +        (featurep 'w32))
> >>      (progn
> >> -      (load "w32-vars")
> >>        (load "term/common-win")
> >> +      (load "w32-vars")
> > 
> > Did you really need this order change?  If yes, why?  If not, I'd
> > prefer to leave the original order, as changing it could potentially
> > cause unintended consequences.
> 
> No, moving w32-vars back on top shouldn't cause a problem.  It really
> doesn't belong there though: shouldn't we load the more general code
> first, then let the platform-specific code muck with it?

Maybe so, but I don't think it's worth time and energy to "fix" this.
The risk breaking something in subtle ways is very real, OTOH.

> >>        (load "term/w32-win")
> >> -      (load "ls-lisp")
> >>        (load "disp-table")
> >> -      (load "dos-w32")
> >> -      (load "w32-fns")))
> >> +      (load "w32-common-fns")
> >> +      (when (eq system-type 'windows-nt)
> >> +        (load "w32-fns")
> >> +        (load "ls-lisp")
> >> +        (load "dos-w32"))))
> > 
> > Likewise here: at the very least, keep the order in the group of
> > packages loaded for windows-nt.
> 
> I can play with the order a bit, but to avoid code duplication, I'd
> strongly prefer to maintain the separation of the code in w32-common-fns
> and w32-fns.

That's okay, and so is ls-lisp move (because it's largely independent
of the windowing stuff).  But please keep the order of dos-w32 and
w32-fns.

> > You mean, the Cygwin build that uses w32 windowing will be unable to
> > support X selections?  That would be a pity.
> 
> Why would it? Emacs in this configuration is a native Windows
> application and it doesn't know a thing about X selections.

But there could be X frames in the same session, couldn't there be?
And if so, the X selection code is present, right?

> >> +(defun w32-handle-dropped-file (window file-name)
> >> +  (let ((f (if (eq system-type 'cygwin)
> >> +               (cygwin-convert-path-from-windows file-name t)
> >> +             (subst-char-in-string ?\\ ?/ file-name)))
> >> +        (coding (or file-name-coding-system
> >> +                    default-file-name-coding-system)))
> > 
> > What is the default value of default-file-name-coding-system in the
> > Cygwin build?  There could be a conflict here between e.g. UTF-8 in
> > Cygwin and the Windows codepage.
> 
> In Cygwin, default-file-name-coding-system will almost always be
> utf-8-unix because the default system locale is UTF-8.
> 
> This particular function is a bit hairy, however: FILE-NAME is _already_
> decoded.  The only reason we're mucking with the coding system here is
> that dnd-handle-one-url unconditionally decodes its argument using (or
> file-name-coding-system default-file-name-coding-system), so we have to
> re-encode what we give it.

But you will be encoding it with UTF-8, which the w32 windows cannot
grok at all.  Or am I missing something?

> > Btw, will the above code be used by the Cygwin build?  Does that
> > include default-printer-name etc.?
> 
> The battery status functionality works fine under the Cygwin build, as
> does default-printer-name. (I haven't tried to actually print anything.)

Doesn't the Cygwin Emacs use lpr or some such to print?  If so, the
name of the system printer will be useless for a Cygwin user, no?



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 17:56     ` Daniel Colascione
@ 2011-12-29 18:18       ` Eli Zaretskii
  2011-12-29 18:53         ` Juanma Barranquero
  0 siblings, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-29 18:18 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Thu, 29 Dec 2011 09:56:43 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: emacs-devel@gnu.org
> 
> 
> [1:text/plain Hide]
> 
> On 12/29/11 9:39 AM, Eli Zaretskii wrote:
> >> Date: Thu, 29 Dec 2011 06:03:20 -0800
> >> From: Daniel Colascione <dancol@dancol.org>
> >>
> >> @@ -184,7 +188,7 @@ struct option longopts[] =
> >>    { "socket-name",	required_argument, NULL, 's' },
> >>  #endif
> >>    { "server-file",	required_argument, NULL, 'f' },
> >> -#ifndef WINDOWSNT
> >> +#ifndef HAVE_NTGUI
> >>    { "display",	required_argument, NULL, 'd' },
> >>  #endif
> > 
> > Maybe I'm confused: will HAVE_NTGUI be applicable to the Cygwin build
> > that can use the w32 windowing?  If so, it sounds like you are
> > creating here 2 kinds of emacsclient: one that can talk to a Cygwin
> > Emacs with X frames on another display, the other that cannot.  Why
> > cannot the same emacsclient work with both types of Emacs frames in a
> > Cygwin Emacs?
> > 
> > Apologies if I've misread the changes.
> 
> No, you're right.  I was following the template of the existing code
> here; IIUC, removing the conditional in the hunk above will let the same
> emacsclient talk to any kind of Emacs.

But the ifdef-out must stay in the native w32 build, I think, because
it causes trouble further down the line.  Juanma, can you comment,
please?




^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 4/4] Fix emacsclient to work with cygw32
  2011-12-29 18:18       ` Eli Zaretskii
@ 2011-12-29 18:53         ` Juanma Barranquero
  0 siblings, 0 replies; 42+ messages in thread
From: Juanma Barranquero @ 2011-12-29 18:53 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Daniel Colascione, emacs-devel

On Thu, Dec 29, 2011 at 19:18, Eli Zaretskii <eliz@gnu.org> wrote:

> But the ifdef-out must stay in the native w32 build, I think, because
> it causes trouble further down the line.  Juanma, can you comment,
> please?

From a cursory look and a full five-minute test, I'd say that, in a
GUI Emacs server it will cause no harm.

When the Emacs server instance is running in a console, "emacsclient
-d zzz myfile" will affect deletion of the frame after C-x #. I don't
think it'd be hard to fix, but you know how *much* I love fiddling
with emacsclient's #ifdefs and flags. Couldn't think of a better
Christmas gift for myself.

Also, offering the user an option which does nothing is ugly. So I'd
vote for #ifdef'ing it away as it is now and sparing us some trouble
and testing.

    Juanma



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 18:17       ` Eli Zaretskii
@ 2011-12-29 21:50         ` Daniel Colascione
  2011-12-30  0:56           ` Jason Rumney
  2011-12-30  9:23           ` Eli Zaretskii
  0 siblings, 2 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 21:50 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 6433 bytes --]

On 12/29/11 10:17 AM, Eli Zaretskii wrote:
>> Date: Thu, 29 Dec 2011 09:53:03 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>> CC: emacs-devel@gnu.org
>>
>>>> --- a/lisp/faces.el
>>>> +++ b/lisp/faces.el
>>>> @@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
>>>>  ;; This is defined originally in xfaces.c.
>>>>  (defcustom face-font-registry-alternatives
>>>>    (mapcar (lambda (arg) (mapcar 'purecopy arg))
>>>> -  (if (eq system-type 'windows-nt)
>>>> +  (if (featurep 'w32)
>>>>        '(("iso8859-1" "ms-oemlatin")
>>>>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
>>>>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
>>>
>>> Why not use window-system (the function) here?
>>
>> Isn't it a little early for window-system at this point?
> 
> A defcustom is evaluated more than once.  So I'm a bit fuzzy about
> "too early" ;-)

When we initialize for the first time, we won't have a GUI frame to
check, yes? (And then we dump the table into pure storage.) If there's
no facility to force reevaluation of the mapping table, we'll end up
using the wrong one when we *do* finally come up with a GUI frame.

>>>> --- a/lisp/frame.el
>>>> +++ b/lisp/frame.el
>>>> @@ -522,7 +522,7 @@ The optional argument PARAMETERS specifies additional frame parameters."
>>>>  	   (ns-initialize-window-system))
>>>>  	 (make-frame `((window-system . ns)
>>>>  		       (display . ,display) . ,parameters)))
>>>> -	((eq system-type 'windows-nt)
>>>> +	((eq window-system 'w32)
>>>>  	 ;; On Windows, ignore DISPLAY.
>>>>  	 (make-frame parameters))
>>>>  	(t
>>>
>>> Why not window-system the function?  You do want to continue
>>> supporting multi-tty in this configuration, right?
>>
>> I've tested multi-tty and it works fine.
> 
> Including X and w32 frames (on the same display)?

I haven't tested an X11 and a cygw32 emacs running different servers at
the same time, but I see no reason that this situation would be any
different from two X11 emacs instances both running servers.

> 
>> When Emacs is configured with system-type cygwin and window-system
>> w32, it still provides w32-ansi-code-page.
> 
> What for?

In cygw32 mode, we try to provide as much of the win32 infrastructure as
conveniently possible because doing so lets the user integrate her emacs
with Windows as tightly as possible. (That's the point of using cygw32
over X11.) For example, she might want to write out a text file encoded
in the system code page, then tell a native, non-Cygwin program about
it. Leaving w32-ansi-code-page in the build does no harm provided we fix
emacs so it isn't confused by the variable's presence.

>>>> -(if (eq system-type 'windows-nt)
>>>> +(if (or (eq system-type 'windows-nt)
>>>> +        (featurep 'w32))
>>>>      (progn
>>>> -      (load "w32-vars")
>>>>        (load "term/common-win")
>>>> +      (load "w32-vars")
>>>
>>> Did you really need this order change?  If yes, why?  If not, I'd
...
>> No, moving w32-vars back on top shouldn't cause a problem.  It really
>> doesn't belong there though: shouldn't we load the more general code
>> first, then let the platform-specific code muck with it?
...
> Maybe so, but I don't think it's worth time and energy to "fix" this.
> The risk breaking something in subtle ways is very real, OTOH.
...
>> I can play with the order a bit, but to avoid code duplication, I'd
>> strongly prefer to maintain the separation of the code in w32-common-fns
>> and w32-fns.
...
> That's okay, and so is ls-lisp move (because it's largely independent
> of the windowing stuff).  But please keep the order of dos-w32 and
> w32-fns.

All right. It's the same to me, though the idea of the code being so
delicate is troubling from a maintainability perspective.

>>> You mean, the Cygwin build that uses w32 windowing will be unable to
>>> support X selections?  That would be a pity.
>>
>> Why would it? Emacs in this configuration is a native Windows
>> application and it doesn't know a thing about X selections.
> 
> But there could be X frames in the same session, couldn't there be?
> And if so, the X selection code is present, right?

A cygw32 emacs could run in the same Windows session as an X server, to
which a different, appropriately-configured copy of emacs could connect.
 But a single emacs instance can't even be compiled with both w32 and
X11 windowing systems.  I don't think such a build would be
prohibitively difficult to implement (it's just a matter of adding a few
levels of indirection), but it's outside the scope of this patch series.

A cygw32 copy of emacs can no more talk X than a Mac or a DOS emacs can.

>>>> +(defun w32-handle-dropped-file (window file-name)
>>>> +  (let ((f (if (eq system-type 'cygwin)
>>>> +               (cygwin-convert-path-from-windows file-name t)
>>>> +             (subst-char-in-string ?\\ ?/ file-name)))
>>>> +        (coding (or file-name-coding-system
>>>> +                    default-file-name-coding-system)))
>>>
>>> What is the default value of default-file-name-coding-system in the
>>> Cygwin build?  There could be a conflict here between e.g. UTF-8 in
>>> Cygwin and the Windows codepage.
>>
>> In Cygwin, default-file-name-coding-system will almost always be
>> utf-8-unix because the default system locale is UTF-8.
>>
>> This particular function is a bit hairy, however: FILE-NAME is _already_
>> decoded.  The only reason we're mucking with the coding system here is
>> that dnd-handle-one-url unconditionally decodes its argument using (or
>> file-name-coding-system default-file-name-coding-system), so we have to
>> re-encode what we give it.
> 
> But you will be encoding it with UTF-8, which the w32 windows cannot
> grok at all.  Or am I missing something?

Windows never sees the encoded string here. At the end of it all, emacs
encoded, and emacs hath decoded away.

>>> Btw, will the above code be used by the Cygwin build?  Does that
>>> include default-printer-name etc.?
>>
>> The battery status functionality works fine under the Cygwin build, as
>> does default-printer-name. (I haven't tried to actually print anything.)
> 
> Doesn't the Cygwin Emacs use lpr or some such to print?  If so, the
> name of the system printer will be useless for a Cygwin user, no?

Cygwin can use Windows printer names (provided the slashes are converted
appropriately).


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 14:03 ` [PATCH 2/4] Refactor window-system configuration Daniel Colascione
@ 2011-12-29 22:21   ` Dan Nicolaescu
  2011-12-29 22:29     ` Daniel Colascione
  2011-12-30  8:19     ` Eli Zaretskii
  0 siblings, 2 replies; 42+ messages in thread
From: Dan Nicolaescu @ 2011-12-29 22:21 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

Daniel Colascione <dancol@dancol.org> writes:

> This change streamlines the window system selection code in
> configure.in and moves many common function declarations from
> window-specific headers to frame.h.  It introduces a new TERM_HEADER
> macro in config.h: we set this macro to the right header to use for
> the window system for which we're compiling Emacs and have source
> files include it indirectly.  This way, we don't have to teach every
> file about every window system.
> ---
[snip

> diff --git a/src/dispnew.c b/src/dispnew.c
> index 2c0e74d..f13fc7a 100644
> --- a/src/dispnew.c
> +++ b/src/dispnew.c
> @@ -44,17 +44,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
>  
>  #include "syssignal.h"
>  
> -#ifdef HAVE_X_WINDOWS
> -#include "xterm.h"
> -#endif /* HAVE_X_WINDOWS */
> -
> -#ifdef HAVE_NTGUI
> -#include "w32term.h"
> -#endif /* HAVE_NTGUI */
> -
> -#ifdef HAVE_NS
> -#include "nsterm.h"
> -#endif
> +#ifdef HAVE_WINDOW_SYSTEM
> +#include TERM_HEADER
> +#endif /* HAVE_WINDOW_SYSTEM */

We don't use something like "#include TERM_HEADER" in any other place,
it you really want to consolidate this stuff, creating a new header file
to include seems better.  Also "TERM" does not look like a good prefix
in this case, it's meaning might be confused with the TERM environment
variable (nsterm/w32term/xterm are not that great either, but better not
propagate the confusion).



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:21   ` Dan Nicolaescu
@ 2011-12-29 22:29     ` Daniel Colascione
  2011-12-29 22:43       ` Dan Nicolaescu
                         ` (2 more replies)
  2011-12-30  8:19     ` Eli Zaretskii
  1 sibling, 3 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 22:29 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1074 bytes --]

On 12/29/11 2:21 PM, Dan Nicolaescu wrote:
> We don't use something like "#include TERM_HEADER" in any other place,
> it you really want to consolidate this stuff, creating a new header
> file to include seems better.

There's a first time for everything, and besides: other projects have
successfully used this approach. My initial revisions actually did
exactly what you suggest, but I realized that the solution was more
complex and didn't actually have any benefit.

Besides, we have src/s/*.h and src/m/*.h. At least this approach is
explicit.

> Also "TERM" does not look like a good prefix
> in this case, it's meaning might be confused with the TERM environment
> variable (nsterm/w32term/xterm are not that great either, but better not
> propagate the confusion).

"Term", I think, it pretty clear in the context of Emacs. Using a
different name for the header constant wouldn't change the names of all
the datatypes in that header. It's better to at least be consistently
confusing.

There's XTERM_HEADER, but this name has other issues.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:29     ` Daniel Colascione
@ 2011-12-29 22:43       ` Dan Nicolaescu
  2011-12-29 22:48         ` Daniel Colascione
  2011-12-30  0:53       ` Stefan Monnier
  2011-12-30  8:21       ` Eli Zaretskii
  2 siblings, 1 reply; 42+ messages in thread
From: Dan Nicolaescu @ 2011-12-29 22:43 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

Daniel Colascione <dancol@dancol.org> writes:

> On 12/29/11 2:21 PM, Dan Nicolaescu wrote:
>> We don't use something like "#include TERM_HEADER" in any other place,
>> it you really want to consolidate this stuff, creating a new header
>> file to include seems better.
>
> There's a first time for everything, and besides: other projects have

Why add another wrinkle when we have perfectly working solutions already? 


> successfully used this approach. My initial revisions actually did
> exactly what you suggest, but I realized that the solution was more
> complex and didn't actually have any benefit.
>
> Besides, we have src/s/*.h and src/m/*.h. At least this approach is
> explicit.

More explicit than a header file? 


>> Also "TERM" does not look like a good prefix
>> in this case, it's meaning might be confused with the TERM environment
>> variable (nsterm/w32term/xterm are not that great either, but better not
>> propagate the confusion).
>
> "Term", I think, it pretty clear in the context of Emacs. Using a

Really? 
ls *term*
nsterm.h     term.c       termchar.h   terminal.c   termopts.h w32term.h
nsterm.m     termcap.c    termhooks.h  terminfo.c   w32term.c    xterm.c
xterm.h      




> different name for the header constant wouldn't change the names of all
> the datatypes in that header. It's better to at least be consistently
> confusing.
>
> There's XTERM_HEADER, but this name has other issues.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:43       ` Dan Nicolaescu
@ 2011-12-29 22:48         ` Daniel Colascione
  2011-12-29 23:05           ` Dan Nicolaescu
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 22:48 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1182 bytes --]

On 12/29/11 2:43 PM, Dan Nicolaescu wrote:
> Daniel Colascione <dancol@dancol.org> writes:
> 
>> On 12/29/11 2:21 PM, Dan Nicolaescu wrote:
>>> We don't use something like "#include TERM_HEADER" in any other place,
>>> it you really want to consolidate this stuff, creating a new header
>>> file to include seems better.
>>
>> There's a first time for everything, and besides: other projects have
> 
> Why add another wrinkle when we have perfectly working solutions already? 

We don't have a working solution. We have a mess, and we're trying to
clean it up. Sure, we don't have header constants right now, but we
don't have any pure-dispatch headers either.

I suppose we disagree on what counts as a wrinkle.  In my mind, it's
simpler for config.h to just tell code what header to include than to
create (and maintain) a header file that consists solely of ifdefs and
more includes.

Ideally, we'd not include window-system-specific headers at all, instead
getting at this functionality via function pointers in a generic struct
in frame.h. But for that, I'd have to change more code than I want to
right now, and this approach gets us 80% there.




[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:48         ` Daniel Colascione
@ 2011-12-29 23:05           ` Dan Nicolaescu
  2011-12-29 23:08             ` Daniel Colascione
  2011-12-30  8:54             ` Eli Zaretskii
  0 siblings, 2 replies; 42+ messages in thread
From: Dan Nicolaescu @ 2011-12-29 23:05 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

Daniel Colascione <dancol@dancol.org> writes:

> On 12/29/11 2:43 PM, Dan Nicolaescu wrote:
>> Daniel Colascione <dancol@dancol.org> writes:
>> 
>>> On 12/29/11 2:21 PM, Dan Nicolaescu wrote:
>>>> We don't use something like "#include TERM_HEADER" in any other place,
>>>> it you really want to consolidate this stuff, creating a new header
>>>> file to include seems better.
>>>
>>> There's a first time for everything, and besides: other projects have
>> 
>> Why add another wrinkle when we have perfectly working solutions already? 
>
> We don't have a working solution. We have a mess, and we're trying to
> clean it up. Sure, we don't have header constants right now, but we
> don't have any pure-dispatch headers either.
>
> I suppose we disagree on what counts as a wrinkle.  In my mind, it's
> simpler for config.h to just tell code what header to include than to
> create (and maintain) a header file that consists solely of ifdefs and
> more includes.

There's a lot more emacs contributors that are fluent in C than
contributors fluent with autoconf.  Why not cater to the majority?



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 23:05           ` Dan Nicolaescu
@ 2011-12-29 23:08             ` Daniel Colascione
  2011-12-30  8:54             ` Eli Zaretskii
  1 sibling, 0 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-29 23:08 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

On 12/29/11 3:05 PM, Dan Nicolaescu wrote:
>> We don't have a working solution. We have a mess, and we're trying to
>> clean it up. Sure, we don't have header constants right now, but we
>> don't have any pure-dispatch headers either.
>>
>> I suppose we disagree on what counts as a wrinkle.  In my mind, it's
>> simpler for config.h to just tell code what header to include than to
>> create (and maintain) a header file that consists solely of ifdefs and
>> more includes.
> 
> There's a lot more emacs contributors that are fluent in C than
> contributors fluent with autoconf.  Why not cater to the majority?

To add a new window system to Emacs, one has to be proficient in both
autoconf and C.  Does anyone else want to chime in here?



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:29     ` Daniel Colascione
  2011-12-29 22:43       ` Dan Nicolaescu
@ 2011-12-30  0:53       ` Stefan Monnier
  2011-12-30  8:21       ` Eli Zaretskii
  2 siblings, 0 replies; 42+ messages in thread
From: Stefan Monnier @ 2011-12-30  0:53 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: Dan Nicolaescu, emacs-devel

>> We don't use something like "#include TERM_HEADER" in any other place,
>> it you really want to consolidate this stuff, creating a new header
>> file to include seems better.
> There's a first time for everything, and besides: other projects have
> successfully used this approach. My initial revisions actually did
> exactly what you suggest, but I realized that the solution was more
> complex and didn't actually have any benefit.
> Besides, we have src/s/*.h and src/m/*.h. At least this approach is
> explicit.

I guess "#include TERM_HEADER" is OK for "now" (i.e. for when the trunk
gets unfrozen).

>> Also "TERM" does not look like a good prefix
>> in this case, it's meaning might be confused with the TERM environment
>> variable (nsterm/w32term/xterm are not that great either, but better not
>> propagate the confusion).
> "Term", I think, it pretty clear in the context of Emacs. Using a
> different name for the header constant wouldn't change the names of all
> the datatypes in that header. It's better to at least be consistently
> confusing.
> There's XTERM_HEADER, but this name has other issues.

We need to create a new file where we can put the common code shared
between the various *term.c files.  Not sure how we could/should call
this file, but it could be "guiterm.c", in which case GUITERM_HEADER
might make sense as well.


        Stefan



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 21:50         ` Daniel Colascione
@ 2011-12-30  0:56           ` Jason Rumney
  2011-12-30  9:23           ` Eli Zaretskii
  1 sibling, 0 replies; 42+ messages in thread
From: Jason Rumney @ 2011-12-30  0:56 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: Eli Zaretskii, emacs-devel

Daniel Colascione <dancol@dancol.org> writes:

> On 12/29/11 10:17 AM, Eli Zaretskii wrote:
>>> Date: Thu, 29 Dec 2011 09:53:03 -0800
>>> From: Daniel Colascione <dancol@dancol.org>
>>> CC: emacs-devel@gnu.org
>>>
>>>>> --- a/lisp/faces.el
>>>>> +++ b/lisp/faces.el
>>>>> @@ -89,7 +89,7 @@ ALTERNATIVE2 etc."
>>>>>  ;; This is defined originally in xfaces.c.
>>>>>  (defcustom face-font-registry-alternatives
>>>>>    (mapcar (lambda (arg) (mapcar 'purecopy arg))
>>>>> -  (if (eq system-type 'windows-nt)
>>>>> +  (if (featurep 'w32)
>>>>>        '(("iso8859-1" "ms-oemlatin")
>>>>>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
>>>>>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
>>>>
>>>> Why not use window-system (the function) here?
>>>
>>> Isn't it a little early for window-system at this point?
>> 
>> A defcustom is evaluated more than once.  So I'm a bit fuzzy about
>> "too early" ;-)
>
> When we initialize for the first time, we won't have a GUI frame to
> check, yes? (And then we dump the table into pure storage.) If there's
> no facility to force reevaluation of the mapping table, we'll end up
> using the wrong one when we *do* finally come up with a GUI frame.

In any case, these registry mappings are not important. They used to be,
because fonts were chosen based on particular registries, but now I
think all script to font mappings have an appropriate iso10646 character
range mapping which will work on Windows.  Some of them might still
serve some backwards compatibility purpose if users are using that
particular registry in their .emacs, but this probably only applies to
gb2312.




^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:21   ` Dan Nicolaescu
  2011-12-29 22:29     ` Daniel Colascione
@ 2011-12-30  8:19     ` Eli Zaretskii
  1 sibling, 0 replies; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  8:19 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: dancol, emacs-devel

> From: Dan Nicolaescu <dann@gnu.org>
> Date: Thu, 29 Dec 2011 17:21:09 -0500
> Cc: emacs-devel@gnu.org
> 
> We don't use something like "#include TERM_HEADER" in any other place

Well, technically we do.  We have this in config.h:

  #include config_opsysfile
  #ifdef config_machfile
  # include config_machfile
  #endif

But I agree with Dan that this technique should be avoided, as it adds
to complexity.

Daniel, can you describe the difficulties in using a string rather
than a macro for doing what you want?



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 22:29     ` Daniel Colascione
  2011-12-29 22:43       ` Dan Nicolaescu
  2011-12-30  0:53       ` Stefan Monnier
@ 2011-12-30  8:21       ` Eli Zaretskii
  2011-12-30  8:26         ` Daniel Colascione
  2 siblings, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  8:21 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: dann, emacs-devel

> Date: Thu, 29 Dec 2011 14:29:24 -0800
> From: Daniel Colascione <dancol@dancol.org>
> Cc: emacs-devel@gnu.org
> 
> My initial revisions actually did exactly what you suggest, but I
> realized that the solution was more complex and didn't actually have
> any benefit.

Can you describe the complexity in this solution?

> > Also "TERM" does not look like a good prefix
> > in this case, it's meaning might be confused with the TERM environment
> > variable (nsterm/w32term/xterm are not that great either, but better not
> > propagate the confusion).
> 
> "Term", I think, it pretty clear in the context of Emacs. Using a
> different name for the header constant wouldn't change the names of all
> the datatypes in that header. It's better to at least be consistently
> confusing.
> 
> There's XTERM_HEADER, but this name has other issues.

How about WINSYS_HEADER?



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  8:21       ` Eli Zaretskii
@ 2011-12-30  8:26         ` Daniel Colascione
  2011-12-30  9:38           ` Eli Zaretskii
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-30  8:26 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: dann, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1416 bytes --]

On 12/30/11 12:21 AM, Eli Zaretskii wrote:
>> Date: Thu, 29 Dec 2011 14:29:24 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>> Cc: emacs-devel@gnu.org
>>
>> My initial revisions actually did exactly what you suggest, but I
>> realized that the solution was more complex and didn't actually have
>> any benefit.
> 
> Can you describe the complexity in this solution?

configure already knows what window system we're using, so it can set a
macro to the name of the correct header without further logic.  Nobody
else has to be aware of the set of possible window systems.

On the other hand, using a fixed "dispatch header" --- i.e., one that
contains a bunch of preprocessor branches and include directives ---
forces us to create yet another place that has to know about all
possible window systems.

>>> Also "TERM" does not look like a good prefix
>>> in this case, it's meaning might be confused with the TERM environment
>>> variable (nsterm/w32term/xterm are not that great either, but better not
>>> propagate the confusion).
>>
>> "Term", I think, it pretty clear in the context of Emacs. Using a
>> different name for the header constant wouldn't change the names of all
>> the datatypes in that header. It's better to at least be consistently
>> confusing.
>>
>> There's XTERM_HEADER, but this name has other issues.
> 
> How about WINSYS_HEADER?

That's fine.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-29 23:05           ` Dan Nicolaescu
  2011-12-29 23:08             ` Daniel Colascione
@ 2011-12-30  8:54             ` Eli Zaretskii
  2011-12-30  9:07               ` Daniel Colascione
  1 sibling, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  8:54 UTC (permalink / raw)
  To: Dan Nicolaescu; +Cc: dancol, emacs-devel

> From: Dan Nicolaescu <dann@gnu.org>
> Date: Thu, 29 Dec 2011 18:05:44 -0500
> Cc: emacs-devel@gnu.org
> 
> There's a lot more emacs contributors that are fluent in C than
> contributors fluent with autoconf.  Why not cater to the majority?

I agree.

Daniel, please keep in mind that at least Windows users normally know
very little about autoconf, and will have difficulty finding their way
around the configure script and its products, and understanding how
and where the value of that constant is computed.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  8:54             ` Eli Zaretskii
@ 2011-12-30  9:07               ` Daniel Colascione
  2011-12-30  9:44                 ` Eli Zaretskii
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-30  9:07 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Dan Nicolaescu, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]

On 12/30/11 12:54 AM, Eli Zaretskii wrote:
>> From: Dan Nicolaescu <dann@gnu.org>
>> Date: Thu, 29 Dec 2011 18:05:44 -0500
>> Cc: emacs-devel@gnu.org
>>
>> There's a lot more emacs contributors that are fluent in C than
>> contributors fluent with autoconf.  Why not cater to the majority?
> 
> I agree.
> 
> Daniel, please keep in mind that at least Windows users normally know
> very little about autoconf, and will have difficulty finding their way
> around the configure script and its products, and understanding how
> and where the value of that constant is computed.
> 

Who exactly do you imagine being confused by a including macro and under
what circumstances do you expect this confusion to arise?  A quick
glance at config.h will reveal what header is being included; any
compiler errors will be attributed directly to the correct place.  The
only people who need to care about the configure machinery are those who
add window systems, and I hope these people would be familiar with autoconf.

I'd really prefer not to have another big list of window systems in the
source.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 21:50         ` Daniel Colascione
  2011-12-30  0:56           ` Jason Rumney
@ 2011-12-30  9:23           ` Eli Zaretskii
  2011-12-30  9:36             ` Daniel Colascione
  1 sibling, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  9:23 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Thu, 29 Dec 2011 13:50:32 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: emacs-devel@gnu.org
> 
> >>>> -  (if (eq system-type 'windows-nt)
> >>>> +  (if (featurep 'w32)
> >>>>        '(("iso8859-1" "ms-oemlatin")
> >>>>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
> >>>>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
> >>>
> >>> Why not use window-system (the function) here?
> >>
> >> Isn't it a little early for window-system at this point?
> > 
> > A defcustom is evaluated more than once.  So I'm a bit fuzzy about
> > "too early" ;-)
> 
> When we initialize for the first time, we won't have a GUI frame to
> check, yes? (And then we dump the table into pure storage.)

In a release tarball, this first time is normally on a GNU/Linux box,
and so the w32-specific setting will never be chosen.

The important initialization is when the dumped Emacs is started on a
Windows system.  At that time, we do have a GUI frame, I think.

> If there's
> no facility to force reevaluation of the mapping table, we'll end up
> using the wrong one when we *do* finally come up with a GUI frame.

But there _is_ such a facility: defcustoms are reevaluated upon
startup (and then again when you actually use "M-x customize", or have
custom-related forms in your .emacs).  This must be so, because
otherwise the values will inherit the platform where Emacs was dumped.

Anyway, given what Jason wrote about this particular variable, it is
not worth arguing about.

My point, however, is that it is a good idea to use window-system (the
function) where it could happen that different frames have different
types, in the current mult-tty Emacs.  If nothing else, using
window-system consistently contributes to the clarity of the code and
shows the overall design to newcomers, who would otherwise become
confused if they see inconsistent tests in each and every place.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-30  9:23           ` Eli Zaretskii
@ 2011-12-30  9:36             ` Daniel Colascione
  2011-12-30 11:16               ` Eli Zaretskii
  0 siblings, 1 reply; 42+ messages in thread
From: Daniel Colascione @ 2011-12-30  9:36 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2680 bytes --]

On 12/30/11 1:23 AM, Eli Zaretskii wrote:
>> Date: Thu, 29 Dec 2011 13:50:32 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>> CC: emacs-devel@gnu.org
>>
>>>>>> -  (if (eq system-type 'windows-nt)
>>>>>> +  (if (featurep 'w32)
>>>>>>        '(("iso8859-1" "ms-oemlatin")
>>>>>>  	("gb2312.1980" "gb2312" "gbk" "gb18030")
>>>>>>  	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
>>>>>
>>>>> Why not use window-system (the function) here?
>>>>
>>>> Isn't it a little early for window-system at this point?
>>>
>>> A defcustom is evaluated more than once.  So I'm a bit fuzzy about
>>> "too early" ;-)
>>
>> When we initialize for the first time, we won't have a GUI frame to
>> check, yes? (And then we dump the table into pure storage.)
> 
> In a release tarball, this first time is normally on a GNU/Linux box,
> and so the w32-specific setting will never be chosen.
> 
> The important initialization is when the dumped Emacs is started on a
> Windows system.  At that time, we do have a GUI frame, I think.
> 
>> If there's
>> no facility to force reevaluation of the mapping table, we'll end up
>> using the wrong one when we *do* finally come up with a GUI frame.
> 
> But there _is_ such a facility: defcustoms are reevaluated upon
> startup (and then again when you actually use "M-x customize", or have
> custom-related forms in your .emacs).  

~/software/emacs/cyg.w32/src
$ ./emacs --daemon
("./emacs")
...
Restarting server

~/software/emacs/cyg.w32/src
$ cd ../lib-src/

~/software/emacs/cyg.w32/lib-src
$ ./emacsclient --eval '(emacs-version)'
"GNU Emacs 24.0.92.71 (i686-pc-cygwin)\n of 2011-12-29 on dfaew"

~/software/emacs/cyg.w32/lib-src
$ ./emacsclient --eval '(window-system)'
nil

~/software/emacs/cyg.w32/lib-src
$ ./emacsclient --eval 'window-system'
nil

~/software/emacs/cyg.w32/lib-src
$ ./emacsclient --eval "(featurep 'w32)"
t

> My point, however, is that it is a good idea to use window-system (the
> function) where it could happen that different frames have different
> types, in the current mult-tty Emacs.  If nothing else, using
> window-system consistently contributes to the clarity of the code and
> shows the overall design to newcomers, who would otherwise become
> confused if they see inconsistent tests in each and every place.

I agree that using window-system where appropriate is best.  That said,
window-system isn't always appropriate.  It answers the question "what
window system am I using right now?".  The featurep query answers "what
window system is this copy of Emacs capable of using?".  Sometimes, the
latter question is of greater interest.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  8:26         ` Daniel Colascione
@ 2011-12-30  9:38           ` Eli Zaretskii
  2011-12-30  9:45             ` Daniel Colascione
  2011-12-30 11:15             ` Stefan Monnier
  0 siblings, 2 replies; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  9:38 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: dann, emacs-devel

> Date: Fri, 30 Dec 2011 00:26:45 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: dann@gnu.org, emacs-devel@gnu.org
> 
> On the other hand, using a fixed "dispatch header" --- i.e., one that
> contains a bunch of preprocessor branches and include directives ---
> forces us to create yet another place that has to know about all
> possible window systems.

I see nothing particularly complex about this, FWIW.  We have our
share of such ifdefs already; bringing most of them to a single place
is better, not worse, than what we have now.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  9:07               ` Daniel Colascione
@ 2011-12-30  9:44                 ` Eli Zaretskii
  0 siblings, 0 replies; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30  9:44 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: dann, emacs-devel

> Date: Fri, 30 Dec 2011 01:07:00 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: Dan Nicolaescu <dann@gnu.org>, emacs-devel@gnu.org
> 
> > Daniel, please keep in mind that at least Windows users normally know
> > very little about autoconf, and will have difficulty finding their way
> > around the configure script and its products, and understanding how
> > and where the value of that constant is computed.
> > 
> 
> Who exactly do you imagine being confused by a including macro and under
> what circumstances do you expect this confusion to arise?

The confusion will happen when J.R. Hacker wants to know where and how
the value of that macro is computed.

> A quick glance at config.h will reveal what header is being included

One must know to look in config.h in the first place.

And even then, the issue of _how_ that macro is computed will still
remain.

> The only people who need to care about the configure machinery are
> those who add window systems, and I hope these people would be
> familiar with autoconf.

There's another kind of people who might need to care: those who need
to figure out why their build process does the wrong thing with a
window system.  Suppose your patches, when applied, break the native
w32 build in some rare case, for example, because the wrong window
system is being computed.

> I'd really prefer not to have another big list of window systems in the
> source.

Why "another"?  I understand that you would be moving those other
places to a single header file.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  9:38           ` Eli Zaretskii
@ 2011-12-30  9:45             ` Daniel Colascione
  2011-12-30 11:15             ` Stefan Monnier
  1 sibling, 0 replies; 42+ messages in thread
From: Daniel Colascione @ 2011-12-30  9:45 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: dann, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1116 bytes --]

On 12/30/11 1:38 AM, Eli Zaretskii wrote:
>> Date: Fri, 30 Dec 2011 00:26:45 -0800
>> From: Daniel Colascione <dancol@dancol.org>
>> On the other hand, using a fixed "dispatch header" --- i.e., one that
>> contains a bunch of preprocessor branches and include directives ---
>> forces us to create yet another place that has to know about all
>> possible window systems.
> 
> I see nothing particularly complex about this, FWIW. 

The header decision tree would be more logic that doesn't have to be
there.  It would amount to yet another moving part in the configuration
machine.  Sure, putting this decision tree in one place is better than
pasting it into every C file that does something with a frame (the
current approach), but getting rid of it altogether would be even better.

I honestly don't understand why you're opposed to the include-a-macro
approach, or what you find unclear about it.

> We have our
> share of such ifdefs already

And we should try to centralize these things and make sure we have as
few as possible.  They make code hard to follow, especially when nested.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-29 14:03 ` [PATCH 3/4] Implement cygw32 Daniel Colascione
  2011-12-29 17:29   ` Eli Zaretskii
@ 2011-12-30  9:49   ` Andreas Schwab
  1 sibling, 0 replies; 42+ messages in thread
From: Andreas Schwab @ 2011-12-30  9:49 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

Daniel Colascione <dancol@dancol.org> writes:

> +DEFUN ("cygwin-convert-path-to-windows",
> +       Fcygwin_convert_path_to_windows, Scygwin_convert_path_to_windows,
> +       1, 2, 0,
> +       doc: /* Convert PATH to a Windows path.  If ABSOLUTE-P if
> +               non-nil, return an absolute path.*/)

> +DEFUN ("cygwin-convert-path-from-windows",
> +       Fcygwin_convert_path_from_windows, Scygwin_convert_path_from_windows,
> +       1, 2, 0,
> +       doc: /* Convert a Windows path to a Cygwin path.  If ABSOLUTE-P
> +               if non-nil, return an absolute path.*/)

The first line of the doc string should contain a single complete
sentence only.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 2/4] Refactor window-system configuration
  2011-12-30  9:38           ` Eli Zaretskii
  2011-12-30  9:45             ` Daniel Colascione
@ 2011-12-30 11:15             ` Stefan Monnier
  1 sibling, 0 replies; 42+ messages in thread
From: Stefan Monnier @ 2011-12-30 11:15 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: dann, Daniel Colascione, emacs-devel

>> On the other hand, using a fixed "dispatch header" --- i.e., one that
>> contains a bunch of preprocessor branches and include directives ---
>> forces us to create yet another place that has to know about all
>> possible window systems.
> I see nothing particularly complex about this, FWIW.  We have our
> share of such ifdefs already; bringing most of them to a single place
> is better, not worse, than what we have now.

Neither do I see anything particularly complex about his #include.
I think we're arguing about the color of the bikeshed.


        Stefan



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 3/4] Implement cygw32
  2011-12-30  9:36             ` Daniel Colascione
@ 2011-12-30 11:16               ` Eli Zaretskii
  0 siblings, 0 replies; 42+ messages in thread
From: Eli Zaretskii @ 2011-12-30 11:16 UTC (permalink / raw)
  To: Daniel Colascione; +Cc: emacs-devel

> Date: Fri, 30 Dec 2011 01:36:03 -0800
> From: Daniel Colascione <dancol@dancol.org>
> CC: emacs-devel@gnu.org
> 
> ~/software/emacs/cyg.w32/lib-src
> $ ./emacsclient --eval '(window-system)'
> nil
> 
> ~/software/emacs/cyg.w32/lib-src
> $ ./emacsclient --eval 'window-system'
> nil
> 
> ~/software/emacs/cyg.w32/lib-src
> $ ./emacsclient --eval "(featurep 'w32)"
> t

Sorry, I don't see the relevance: daemon needs special handling
anyway.  E.g., featurep will be irrelevant if emacsclient is invoked
to create a text-mode frame.

> I agree that using window-system where appropriate is best.  That said,
> window-system isn't always appropriate.  It answers the question "what
> window system am I using right now?".  The featurep query answers "what
> window system is this copy of Emacs capable of using?".  Sometimes, the
> latter question is of greater interest.

But in an Emacs that is capable of creating _both_ w32 and X frames,
the values of (featurep 'x) and (featurep 'w32) are not mutually
exclusive.

If we don't want to go through the pains we went through when
multi-tty was introduced, we should never again add code that makes
assumptions about which window systems can or cannot coexist.



^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 1/4] Compilation cleanups
  2011-12-29 17:54   ` Paul Eggert
@ 2011-12-31 14:50     ` Ken Brown
  2011-12-31 20:29       ` Paul Eggert
  0 siblings, 1 reply; 42+ messages in thread
From: Ken Brown @ 2011-12-31 14:50 UTC (permalink / raw)
  To: Paul Eggert; +Cc: Daniel Colascione, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1053 bytes --]



On 12/29/2011 12:54 PM, Paul Eggert wrote:
> On 12/29/11 06:03, Daniel Colascione wrote:
>
>> --- a/lib-src/update-game-score.c
>> +++ b/lib-src/update-game-score.c
>> @@ -48,8 +48,10 @@ along with GNU Emacs.  If not, see<http://www.gnu.org/licenses/>.  */
>>   #include<sys/stat.h>
>>
>>   /* Needed for SunOS4, for instance.  */
>> +#if !defined(CYGWIN)
>>   extern char *optarg;
>>   extern int optind, opterr;
>> +#endif // !defined(CYGWIN)
>
> I suggest replacing those lines with "#include<getopt.h>" instead;
> that's what the other .c files do, and it's cleaner.
>
>
>> --- a/src/gmalloc.c
>> +++ b/src/gmalloc.c
> ...
>> -  if (ptr<  _heapbase)
>> +  if ((char*)ptr<  (char*)_heapbase)
>
> No need to cast _heapbase as it's already a char *.
> And please put a space after the ')', e.g.,
> "(char*) ptr<  _heapbase", as that's the usual
> Emacs style.

And as long as we're cleaning up, why don't we get rid of all those 
warnings about old-style function definitions in gmalloc.c.  A revision 
of Daniel's patch that does this is attached.


[-- Attachment #2: cleanup.patch --]
[-- Type: text/plain, Size: 7921 bytes --]

=== modified file 'lib-src/update-game-score.c'
--- lib-src/update-game-score.c	2011-11-17 09:09:20 +0000
+++ lib-src/update-game-score.c	2011-12-31 14:09:29 +0000
@@ -46,10 +46,7 @@
 #include <fcntl.h>
 #endif
 #include <sys/stat.h>
-
-/* Needed for SunOS4, for instance.  */
-extern char *optarg;
-extern int optind, opterr;
+#include <getopt.h>
 
 static int usage (int err) NO_RETURN;
 

=== modified file 'src/gmalloc.c'
--- src/gmalloc.c	2011-09-09 01:06:52 +0000
+++ src/gmalloc.c	2011-12-31 14:36:48 +0000
@@ -455,8 +455,7 @@
 /* Aligned allocation.  */
 static __ptr_t align PP ((__malloc_size_t));
 static __ptr_t
-align (size)
-     __malloc_size_t size;
+align (__malloc_size_t size)
 {
   __ptr_t result;
   unsigned long int adj;
@@ -490,9 +489,7 @@
    If we cannot get space at END, fail and return 0.  */
 static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t));
 static __ptr_t
-get_contiguous_space (size, position)
-     __malloc_ptrdiff_t size;
-     __ptr_t position;
+get_contiguous_space (__malloc_ptrdiff_t size, __ptr_t position)
 {
   __ptr_t before;
   __ptr_t after;
@@ -548,21 +545,21 @@
 int _malloc_thread_enabled_p;
 
 static void
-malloc_atfork_handler_prepare ()
+malloc_atfork_handler_prepare (void)
 {
   LOCK ();
   LOCK_ALIGNED_BLOCKS ();
 }
 
 static void
-malloc_atfork_handler_parent ()
+malloc_atfork_handler_parent (void)
 {
   UNLOCK_ALIGNED_BLOCKS ();
   UNLOCK ();
 }
 
 static void
-malloc_atfork_handler_child ()
+malloc_atfork_handler_child (void)
 {
   UNLOCK_ALIGNED_BLOCKS ();
   UNLOCK ();
@@ -570,7 +567,7 @@
 
 /* Set up mutexes and make malloc etc. thread-safe.  */
 void
-malloc_enable_thread ()
+malloc_enable_thread (void)
 {
   if (_malloc_thread_enabled_p)
     return;
@@ -589,7 +586,7 @@
 #endif
 
 static void
-malloc_initialize_1 ()
+malloc_initialize_1 (void)
 {
 #ifdef GC_MCHECK
   mcheck (NULL);
@@ -630,7 +627,7 @@
    main will call malloc which calls this function.  That is before any threads
    or signal handlers has been set up, so we don't need thread protection.  */
 int
-__malloc_initialize ()
+__malloc_initialize (void)
 {
   if (__malloc_initialized)
     return 0;
@@ -646,8 +643,7 @@
    growing the heap info table as necessary. */
 static __ptr_t morecore_nolock PP ((__malloc_size_t));
 static __ptr_t
-morecore_nolock (size)
-     __malloc_size_t size;
+morecore_nolock (__malloc_size_t size)
 {
   __ptr_t result;
   malloc_info *newinfo, *oldinfo;
@@ -760,8 +756,7 @@
 
 /* Allocate memory from the heap.  */
 __ptr_t
-_malloc_internal_nolock (size)
-     __malloc_size_t size;
+_malloc_internal_nolock (__malloc_size_t size)
 {
   __ptr_t result;
   __malloc_size_t block, blocks, lastblocks, start;
@@ -960,8 +955,7 @@
 }
 
 __ptr_t
-_malloc_internal (size)
-     __malloc_size_t size;
+_malloc_internal (__malloc_size_t size)
 {
   __ptr_t result;
 
@@ -973,8 +967,7 @@
 }
 
 __ptr_t
-malloc (size)
-     __malloc_size_t size;
+malloc (__malloc_size_t size)
 {
   __ptr_t (*hook) (__malloc_size_t);
 
@@ -999,23 +992,19 @@
    and _realloc.  Make them use the GNU functions.  */
 
 __ptr_t
-_malloc (size)
-     __malloc_size_t size;
+_malloc (__malloc_size_t size)
 {
   return malloc (size);
 }
 
 void
-_free (ptr)
-     __ptr_t ptr;
+_free (__ptr_t ptr)
 {
   free (ptr);
 }
 
 __ptr_t
-_realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc (__ptr_t ptr, __malloc_size_t size)
 {
   return realloc (ptr, size);
 }
@@ -1058,8 +1047,7 @@
 /* Return memory to the heap.
    Like `_free_internal' but don't lock mutex.  */
 void
-_free_internal_nolock (ptr)
-     __ptr_t ptr;
+_free_internal_nolock (__ptr_t ptr)
 {
   int type;
   __malloc_size_t block, blocks;
@@ -1076,7 +1064,7 @@
     return;
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char*) ptr < _heapbase)
     /* We're being asked to free something in the static heap. */
     return;
 #endif
@@ -1313,8 +1301,7 @@
 /* Return memory to the heap.
    Like `free' but don't call a __free_hook if there is one.  */
 void
-_free_internal (ptr)
-     __ptr_t ptr;
+_free_internal (__ptr_t ptr)
 {
   LOCK ();
   _free_internal_nolock (ptr);
@@ -1324,8 +1311,7 @@
 /* Return memory to the heap.  */
 
 void
-free (ptr)
-     __ptr_t ptr;
+free (__ptr_t ptr)
 {
   void (*hook) (__ptr_t) = __free_hook;
 
@@ -1340,8 +1326,7 @@
 weak_alias (free, cfree)
 #else
 void
-cfree (ptr)
-     __ptr_t ptr;
+cfree (__ptr_t ptr)
 {
   free (ptr);
 }
@@ -1381,9 +1366,7 @@
    data.  */
 #ifdef CYGWIN
 __ptr_t
-special_realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+special_realloc (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
   int type;
@@ -1411,9 +1394,7 @@
    new region.  This module has incestuous knowledge of the
    internals of both free and malloc. */
 __ptr_t
-_realloc_internal_nolock (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc_internal_nolock (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
   int type;
@@ -1428,7 +1409,7 @@
     return _malloc_internal_nolock (size);
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char*) ptr < _heapbase)
     /* ptr points into the static heap */
     return special_realloc (ptr, size);
 #endif
@@ -1535,9 +1516,7 @@
 }
 
 __ptr_t
-_realloc_internal (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc_internal (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
 
@@ -1549,9 +1528,7 @@
 }
 
 __ptr_t
-realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+realloc (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t (*hook) (__ptr_t, __malloc_size_t);
 
@@ -1589,9 +1566,7 @@
 /* Allocate an array of NMEMB elements each SIZE bytes long.
    The entire array is initialized to zeros.  */
 __ptr_t
-calloc (nmemb, size)
-     register __malloc_size_t nmemb;
-     register __malloc_size_t size;
+calloc (register __malloc_size_t nmemb, register __malloc_size_t size)
 {
   register __ptr_t result = malloc (nmemb * size);
 
@@ -1643,8 +1618,7 @@
    and return the start of data space, or NULL on errors.
    If INCREMENT is negative, shrink data space.  */
 __ptr_t
-__default_morecore (increment)
-     __malloc_ptrdiff_t increment;
+__default_morecore (__malloc_ptrdiff_t increment)
 {
   __ptr_t result;
 #if defined (CYGWIN)
@@ -1684,9 +1658,7 @@
 				__malloc_size_t __alignment));
 
 __ptr_t
-memalign (alignment, size)
-     __malloc_size_t alignment;
-     __malloc_size_t size;
+memalign (__malloc_size_t alignment, __malloc_size_t size)
 {
   __ptr_t result;
   unsigned long int adj, lastadj;
@@ -1767,10 +1739,8 @@
 #endif
 
 int
-posix_memalign (memptr, alignment, size)
-     __ptr_t *memptr;
-     __malloc_size_t alignment;
-     __malloc_size_t size;
+posix_memalign (__ptr_t *memptr, __malloc_size_t alignment,
+		__malloc_size_t size)
 {
   __ptr_t mem;
 
@@ -1841,8 +1811,7 @@
 static __malloc_size_t pagesize;
 
 __ptr_t
-valloc (size)
-     __malloc_size_t size;
+valloc (__malloc_size_t size)
 {
   if (pagesize == 0)
     pagesize = __getpagesize ();
@@ -1935,8 +1904,7 @@
 
 static void freehook (__ptr_t);
 static void
-freehook (ptr)
-     __ptr_t ptr;
+freehook (__ptr_t ptr)
 {
   struct hdr *hdr;
 
@@ -1957,8 +1925,7 @@
 
 static __ptr_t mallochook (__malloc_size_t);
 static __ptr_t
-mallochook (size)
-     __malloc_size_t size;
+mallochook (__malloc_size_t size)
 {
   struct hdr *hdr;
 
@@ -1977,9 +1944,7 @@
 
 static __ptr_t reallochook (__ptr_t, __malloc_size_t);
 static __ptr_t
-reallochook (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+reallochook (__ptr_t ptr, __malloc_size_t size)
 {
   struct hdr *hdr = NULL;
   __malloc_size_t osize = 0;

=== modified file 'src/unexcw.c'
--- src/unexcw.c	2011-08-16 13:27:12 +0000
+++ src/unexcw.c	2011-12-31 13:52:57 +0000
@@ -31,6 +31,8 @@
 
 #define DOTEXE ".exe"
 
+extern void report_sheap_usage (int);
+
 extern int bss_sbrk_did_unexec;
 
 extern int __malloc_initialized;


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH 1/4] Compilation cleanups
  2011-12-31 14:50     ` Ken Brown
@ 2011-12-31 20:29       ` Paul Eggert
  0 siblings, 0 replies; 42+ messages in thread
From: Paul Eggert @ 2011-12-31 20:29 UTC (permalink / raw)
  To: Ken Brown; +Cc: Daniel Colascione, emacs-devel

On 12/31/11 06:50, Ken Brown wrote:
>  static __ptr_t align PP ((__malloc_size_t));
>  static __ptr_t
> -align (size)
> -     __malloc_size_t size;
> +align (__malloc_size_t size)

Good idea, and there's more where that came from --
for example, that first line (with the PP) can be removed
since it was there only because of the old-style definitions.

Here's an improved version of that patch, which includes
some hopefully-obvious extra cleanups.  (And there's more
where this came from -- that code is pretty crufty....)


=== modified file 'lib-src/update-game-score.c'
--- lib-src/update-game-score.c	2011-11-17 09:09:20 +0000
+++ lib-src/update-game-score.c	2011-12-31 18:37:05 +0000
@@ -46,10 +46,7 @@
 #include <fcntl.h>
 #endif
 #include <sys/stat.h>
-
-/* Needed for SunOS4, for instance.  */
-extern char *optarg;
-extern int optind, opterr;
+#include <getopt.h>
 
 static int usage (int err) NO_RETURN;
 

=== modified file 'src/buffer.c'
--- src/buffer.c	2011-11-27 04:43:11 +0000
+++ src/buffer.c	2011-12-31 19:24:12 +0000
@@ -2004,10 +2004,6 @@
   return byte_pos;
 }
 
-#ifdef REL_ALLOC
-extern void r_alloc_reset_variable (POINTER_TYPE *, POINTER_TYPE *);
-#endif /* REL_ALLOC */
-
 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
        1, 1, 0,
        doc: /* Swap the text between current buffer and BUFFER.  */)
@@ -4763,13 +4759,6 @@
 			    Buffer-text Allocation
  ***********************************************************************/
 
-#ifdef REL_ALLOC
-extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
-extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
-extern void r_alloc_free (POINTER_TYPE **ptr);
-#endif /* REL_ALLOC */
-
-
 /* Allocate NBYTES bytes for buffer B's text buffer.  */
 
 static void

=== modified file 'src/gmalloc.c'
--- src/gmalloc.c	2011-09-09 01:06:52 +0000
+++ src/gmalloc.c	2011-12-31 20:23:17 +0000
@@ -27,10 +27,6 @@
    The author may be reached (Email) at the address mike@ai.mit.edu,
    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
 
-#ifndef _MALLOC_H
-
-#define _MALLOC_H	1
-
 #ifdef _MALLOC_INTERNAL
 
 #ifdef	HAVE_CONFIG_H
@@ -78,17 +74,8 @@
 extern void free PP ((__ptr_t __ptr));
 
 /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
-#if !defined (_MALLOC_INTERNAL) || defined (MSDOS) /* Avoid conflict.  */
 extern __ptr_t memalign PP ((__malloc_size_t __alignment,
 			     __malloc_size_t __size));
-extern int posix_memalign PP ((__ptr_t *, __malloc_size_t,
-			       __malloc_size_t size));
-#endif
-
-/* Allocate SIZE bytes on a page boundary.  */
-#if ! (defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC))
-extern __ptr_t valloc PP ((__malloc_size_t __size));
-#endif
 
 #ifdef USE_PTHREAD
 /* Set up mutexes and make malloc etc. thread-safe.  */
@@ -173,7 +160,7 @@
 /* Free list headers for each fragment size.  */
 extern struct list _fraghead[];
 
-/* List of blocks allocated with `memalign' (or `valloc').  */
+/* List of blocks allocated with `memalign'.  */
 struct alignlist
   {
     struct alignlist *next;
@@ -305,24 +292,10 @@
 extern void memory_warnings PP ((__ptr_t __start,
 				 void (*__warnfun) PP ((const char *))));
 
-
-/* Relocating allocator.  */
-
-/* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
-extern __ptr_t r_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
-
-/* Free the storage allocated in HANDLEPTR.  */
-extern void r_alloc_free PP ((__ptr_t *__handleptr));
-
-/* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
-extern __ptr_t r_re_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
-
-
 #ifdef	__cplusplus
 }
 #endif
 
-#endif /* malloc.h  */
 /* Memory allocator `malloc'.
    Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
 		  Written May 1989 by Mike Haertel.
@@ -453,10 +426,8 @@
 
 
 /* Aligned allocation.  */
-static __ptr_t align PP ((__malloc_size_t));
 static __ptr_t
-align (size)
-     __malloc_size_t size;
+align (__malloc_size_t size)
 {
   __ptr_t result;
   unsigned long int adj;
@@ -473,9 +444,8 @@
 						  (char *) NULL)) % BLOCKSIZE;
   if (adj != 0)
     {
-      __ptr_t new;
       adj = BLOCKSIZE - adj;
-      new = (*__morecore) (adj);
+      (*__morecore) (adj);
       result = (char *) result + adj;
     }
 
@@ -488,11 +458,8 @@
 /* Get SIZE bytes, if we can get them starting at END.
    Return the address of the space we got.
    If we cannot get space at END, fail and return 0.  */
-static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t));
 static __ptr_t
-get_contiguous_space (size, position)
-     __malloc_ptrdiff_t size;
-     __ptr_t position;
+get_contiguous_space (__malloc_ptrdiff_t size, __ptr_t position)
 {
   __ptr_t before;
   __ptr_t after;
@@ -548,21 +515,21 @@
 int _malloc_thread_enabled_p;
 
 static void
-malloc_atfork_handler_prepare ()
+malloc_atfork_handler_prepare (void)
 {
   LOCK ();
   LOCK_ALIGNED_BLOCKS ();
 }
 
 static void
-malloc_atfork_handler_parent ()
+malloc_atfork_handler_parent (void)
 {
   UNLOCK_ALIGNED_BLOCKS ();
   UNLOCK ();
 }
 
 static void
-malloc_atfork_handler_child ()
+malloc_atfork_handler_child (void)
 {
   UNLOCK_ALIGNED_BLOCKS ();
   UNLOCK ();
@@ -570,7 +537,7 @@
 
 /* Set up mutexes and make malloc etc. thread-safe.  */
 void
-malloc_enable_thread ()
+malloc_enable_thread (void)
 {
   if (_malloc_thread_enabled_p)
     return;
@@ -589,7 +556,7 @@
 #endif
 
 static void
-malloc_initialize_1 ()
+malloc_initialize_1 (void)
 {
 #ifdef GC_MCHECK
   mcheck (NULL);
@@ -630,7 +597,7 @@
    main will call malloc which calls this function.  That is before any threads
    or signal handlers has been set up, so we don't need thread protection.  */
 int
-__malloc_initialize ()
+__malloc_initialize (void)
 {
   if (__malloc_initialized)
     return 0;
@@ -644,10 +611,8 @@
 
 /* Get neatly aligned memory, initializing or
    growing the heap info table as necessary. */
-static __ptr_t morecore_nolock PP ((__malloc_size_t));
 static __ptr_t
-morecore_nolock (size)
-     __malloc_size_t size;
+morecore_nolock (__malloc_size_t size)
 {
   __ptr_t result;
   malloc_info *newinfo, *oldinfo;
@@ -760,8 +725,7 @@
 
 /* Allocate memory from the heap.  */
 __ptr_t
-_malloc_internal_nolock (size)
-     __malloc_size_t size;
+_malloc_internal_nolock (__malloc_size_t size)
 {
   __ptr_t result;
   __malloc_size_t block, blocks, lastblocks, start;
@@ -960,8 +924,7 @@
 }
 
 __ptr_t
-_malloc_internal (size)
-     __malloc_size_t size;
+_malloc_internal (__malloc_size_t size)
 {
   __ptr_t result;
 
@@ -973,8 +936,7 @@
 }
 
 __ptr_t
-malloc (size)
-     __malloc_size_t size;
+malloc (__malloc_size_t size)
 {
   __ptr_t (*hook) (__malloc_size_t);
 
@@ -998,24 +960,24 @@
 /* On some ANSI C systems, some libc functions call _malloc, _free
    and _realloc.  Make them use the GNU functions.  */
 
+extern __ptr_t _malloc (__malloc_size_t);
+extern void _free (__ptr_t);
+extern __ptr_t _realloc (__ptr_t, __malloc_size_t);
+
 __ptr_t
-_malloc (size)
-     __malloc_size_t size;
+_malloc (__malloc_size_t size)
 {
   return malloc (size);
 }
 
 void
-_free (ptr)
-     __ptr_t ptr;
+_free (__ptr_t ptr)
 {
   free (ptr);
 }
 
 __ptr_t
-_realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc (__ptr_t ptr, __malloc_size_t size)
 {
   return realloc (ptr, size);
 }
@@ -1058,8 +1020,7 @@
 /* Return memory to the heap.
    Like `_free_internal' but don't lock mutex.  */
 void
-_free_internal_nolock (ptr)
-     __ptr_t ptr;
+_free_internal_nolock (__ptr_t ptr)
 {
   int type;
   __malloc_size_t block, blocks;
@@ -1076,7 +1037,7 @@
     return;
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char *) ptr < _heapbase)
     /* We're being asked to free something in the static heap. */
     return;
 #endif
@@ -1313,8 +1274,7 @@
 /* Return memory to the heap.
    Like `free' but don't call a __free_hook if there is one.  */
 void
-_free_internal (ptr)
-     __ptr_t ptr;
+_free_internal (__ptr_t ptr)
 {
   LOCK ();
   _free_internal_nolock (ptr);
@@ -1324,8 +1284,7 @@
 /* Return memory to the heap.  */
 
 void
-free (ptr)
-     __ptr_t ptr;
+free (__ptr_t ptr)
 {
   void (*hook) (__ptr_t) = __free_hook;
 
@@ -1340,8 +1299,7 @@
 weak_alias (free, cfree)
 #else
 void
-cfree (ptr)
-     __ptr_t ptr;
+cfree (__ptr_t ptr)
 {
   free (ptr);
 }
@@ -1381,9 +1339,7 @@
    data.  */
 #ifdef CYGWIN
 __ptr_t
-special_realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+special_realloc (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
   int type;
@@ -1411,9 +1367,7 @@
    new region.  This module has incestuous knowledge of the
    internals of both free and malloc. */
 __ptr_t
-_realloc_internal_nolock (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc_internal_nolock (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
   int type;
@@ -1428,7 +1382,7 @@
     return _malloc_internal_nolock (size);
 
 #ifdef CYGWIN
-  if (ptr < _heapbase)
+  if ((char *) ptr < _heapbase)
     /* ptr points into the static heap */
     return special_realloc (ptr, size);
 #endif
@@ -1535,9 +1489,7 @@
 }
 
 __ptr_t
-_realloc_internal (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+_realloc_internal (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t result;
 
@@ -1549,9 +1501,7 @@
 }
 
 __ptr_t
-realloc (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+realloc (__ptr_t ptr, __malloc_size_t size)
 {
   __ptr_t (*hook) (__ptr_t, __malloc_size_t);
 
@@ -1589,9 +1539,7 @@
 /* Allocate an array of NMEMB elements each SIZE bytes long.
    The entire array is initialized to zeros.  */
 __ptr_t
-calloc (nmemb, size)
-     register __malloc_size_t nmemb;
-     register __malloc_size_t size;
+calloc (register __malloc_size_t nmemb, register __malloc_size_t size)
 {
   register __ptr_t result = malloc (nmemb * size);
 
@@ -1643,8 +1591,7 @@
    and return the start of data space, or NULL on errors.
    If INCREMENT is negative, shrink data space.  */
 __ptr_t
-__default_morecore (increment)
-     __malloc_ptrdiff_t increment;
+__default_morecore (__malloc_ptrdiff_t increment)
 {
   __ptr_t result;
 #if defined (CYGWIN)
@@ -1658,6 +1605,9 @@
     return NULL;
   return result;
 }
+
+#ifdef USE_POSIX_MEMALIGN
+
 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
 
 This library is free software; you can redistribute it and/or
@@ -1684,9 +1634,7 @@
 				__malloc_size_t __alignment));
 
 __ptr_t
-memalign (alignment, size)
-     __malloc_size_t alignment;
-     __malloc_size_t size;
+memalign (__malloc_size_t alignment, __malloc_size_t size)
 {
   __ptr_t result;
   unsigned long int adj, lastadj;
@@ -1767,10 +1715,8 @@
 #endif
 
 int
-posix_memalign (memptr, alignment, size)
-     __ptr_t *memptr;
-     __malloc_size_t alignment;
-     __malloc_size_t size;
+posix_memalign (__ptr_t *memptr, __malloc_size_t alignment,
+		__malloc_size_t size)
 {
   __ptr_t mem;
 
@@ -1787,70 +1733,7 @@
 
   return 0;
 }
-
-/* Allocate memory on a page boundary.
-   Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public
-License along with this library; see the file COPYING.  If
-not, write to the Free Software Foundation, Inc., 51 Franklin Street,
-Fifth Floor, Boston, MA 02110-1301, USA.
-
-   The author may be reached (Email) at the address mike@ai.mit.edu,
-   or (US mail) as Mike Haertel c/o Free Software Foundation.  */
-
-#if defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC)
-
-/* Emacs defines GMALLOC_INHIBIT_VALLOC to avoid this definition
-   on MSDOS, where it conflicts with a system header file.  */
-
-#define ELIDE_VALLOC
-
-#endif
-
-#ifndef	ELIDE_VALLOC
-
-#if defined (__GNU_LIBRARY__) || defined (_LIBC)
-#include <stddef.h>
-#include <sys/cdefs.h>
-#if defined (__GLIBC__) && __GLIBC__ >= 2
-/* __getpagesize is already declared in <unistd.h> with return type int */
-#else
-extern size_t __getpagesize PP ((void));
-#endif
-#else
-#include "getpagesize.h"
-#define	 __getpagesize()	getpagesize ()
-#endif
-
-#ifndef	_MALLOC_INTERNAL
-#define	_MALLOC_INTERNAL
-#include <malloc.h>
-#endif
-
-static __malloc_size_t pagesize;
-
-__ptr_t
-valloc (size)
-     __malloc_size_t size;
-{
-  if (pagesize == 0)
-    pagesize = __getpagesize ();
-
-  return memalign (pagesize, size);
-}
-
-#endif	/* Not ELIDE_VALLOC.  */
+#endif /* USE_POSIX_MEMALIGN */
 
 #ifdef GC_MCHECK
 
@@ -1933,10 +1816,8 @@
   return status;
 }
 
-static void freehook (__ptr_t);
 static void
-freehook (ptr)
-     __ptr_t ptr;
+freehook (__ptr_t ptr)
 {
   struct hdr *hdr;
 
@@ -1955,10 +1836,8 @@
   __free_hook = freehook;
 }
 
-static __ptr_t mallochook (__malloc_size_t);
 static __ptr_t
-mallochook (size)
-     __malloc_size_t size;
+mallochook (__malloc_size_t size)
 {
   struct hdr *hdr;
 
@@ -1975,11 +1854,8 @@
   return (__ptr_t) (hdr + 1);
 }
 
-static __ptr_t reallochook (__ptr_t, __malloc_size_t);
 static __ptr_t
-reallochook (ptr, size)
-     __ptr_t ptr;
-     __malloc_size_t size;
+reallochook (__ptr_t ptr, __malloc_size_t size)
 {
   struct hdr *hdr = NULL;
   __malloc_size_t osize = 0;

=== modified file 'src/lisp.h'
--- src/lisp.h	2011-12-05 00:15:15 +0000
+++ src/lisp.h	2011-12-31 19:26:02 +0000
@@ -2835,6 +2835,14 @@
 extern struct buffer * allocate_buffer (void);
 extern int valid_lisp_object_p (Lisp_Object);
 
+#ifdef REL_ALLOC
+/* Defined in ralloc.c */
+extern POINTER_TYPE *r_alloc (POINTER_TYPE **, size_t);
+extern POINTER_TYPE r_alloc_free (POINTER_TYPE **);
+extern POINTER_TYPE *r_re_alloc (POINTER_TYPE **, size_t);
+void r_alloc_reset_variable (POINTER_TYPE **old, POINTER_TYPE **new);
+#endif
+
 /* Defined in chartab.c */
 EXFUN (Fmake_char_table, 2);
 EXFUN (Fset_char_table_parent, 2);

=== modified file 'src/m/ia64.h'
--- src/m/ia64.h	2011-04-19 00:34:42 +0000
+++ src/m/ia64.h	2011-12-31 19:23:14 +0000
@@ -30,13 +30,3 @@
 #define EMACS_INT		long
 #define pI			"l"
 #define EMACS_UINT		unsigned long
-
-#ifdef REL_ALLOC
-#ifndef _MALLOC_INTERNAL
-/* "char *" because ralloc.c defines it that way.  gmalloc.c thinks it
-   is allowed to prototype these as "void *" so we don't prototype in
-   that case.  You're right: it stinks!  */
-extern char *r_alloc (), *r_re_alloc ();
-extern void r_alloc_free ();
-#endif /* not _MALLOC_INTERNAL */
-#endif /* REL_ALLOC */

=== modified file 'src/ralloc.c'
--- src/ralloc.c	2011-11-15 00:54:19 +0000
+++ src/ralloc.c	2011-12-31 19:22:20 +0000
@@ -95,10 +95,8 @@
 /* Macros for rounding.  Note that rounding to any value is possible
    by changing the definition of PAGE.  */
 #define PAGE (getpagesize ())
-#define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
 #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
 		       & ~(page_size - 1))
-#define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
 
 #define MEM_ALIGN sizeof (double)
 #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
@@ -151,7 +149,6 @@
 } *heap_ptr;
 
 #define NIL_HEAP ((heap_ptr) 0)
-#define HEAP_PTR_SIZE (sizeof (struct heap))
 
 /* This is the first heap object.
    If we need additional heap objects, each one resides at the beginning of
@@ -366,15 +363,6 @@
 	}
     }
 }
-
-/* Return the total size in use by relocating allocator,
-   above where malloc gets space.  */
-
-long
-r_alloc_size_in_use (void)
-{
-  return (char *) break_value - (char *) virtual_break_value;
-}
 \f
 /* The meat - allocating, freeing, and relocating blocs.  */
 
@@ -745,7 +733,7 @@
    __morecore hook values - in particular, __default_morecore in the
    GNU malloc package.  */
 
-POINTER
+static POINTER
 r_alloc_sbrk (long int size)
 {
   register bloc_ptr b;
@@ -1009,52 +997,6 @@
   return *ptr;
 }
 
-/* Disable relocations, after making room for at least SIZE bytes
-   of non-relocatable heap if possible.  The relocatable blocs are
-   guaranteed to hold still until thawed, even if this means that
-   malloc must return a null pointer.  */
-
-void
-r_alloc_freeze (long int size)
-{
-  if (! r_alloc_initialized)
-    r_alloc_init ();
-
-  /* If already frozen, we can't make any more room, so don't try.  */
-  if (r_alloc_freeze_level > 0)
-    size = 0;
-  /* If we can't get the amount requested, half is better than nothing.  */
-  while (size > 0 && r_alloc_sbrk (size) == 0)
-    size /= 2;
-  ++r_alloc_freeze_level;
-  if (size > 0)
-    r_alloc_sbrk (-size);
-}
-
-void
-r_alloc_thaw (void)
-{
-
-  if (! r_alloc_initialized)
-    r_alloc_init ();
-
-  if (--r_alloc_freeze_level < 0)
-    abort ();
-
-  /* This frees all unused blocs.  It is not too inefficient, as the resize
-     and memcpy is done only once.  Afterwards, all unreferenced blocs are
-     already shrunk to zero size.  */
-  if (!r_alloc_freeze_level)
-    {
-      bloc_ptr *b = &first_bloc;
-      while (*b)
-	if (!(*b)->variable)
-	  free_bloc (*b);
-	else
-	  b = &(*b)->next;
-    }
-}
-
 
 #if defined (emacs) && defined (DOUG_LEA_MALLOC)
 

=== modified file 'src/s/msdos.h'
--- src/s/msdos.h	2011-11-14 20:23:26 +0000
+++ src/s/msdos.h	2011-12-31 19:57:51 +0000
@@ -48,10 +48,6 @@
    different things on your system and must be used only through an
    encapsulation (which you should place, by convention, in sysdep.c).  */
 \f
-/* Avoid incompatibilities between gmalloc.c and system header files
-   in how to declare valloc.  */
-#define GMALLOC_INHIBIT_VALLOC
-
 /* This overrides the default value on editfns.c, since DJGPP
    does not have pw->pw_gecos.  */
 #define USER_FULL_NAME (getenv ("NAME"))

=== modified file 'src/unexcw.c'
--- src/unexcw.c	2011-08-16 13:27:12 +0000
+++ src/unexcw.c	2011-12-31 18:37:05 +0000
@@ -31,6 +31,8 @@
 
 #define DOTEXE ".exe"
 
+extern void report_sheap_usage (int);
+
 extern int bss_sbrk_did_unexec;
 
 extern int __malloc_initialized;




^ permalink raw reply	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2011-12-31 20:29 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-29 14:03 [PATCH 0/4] Support Win32 GUI in Cygwin Emacs Daniel Colascione
2011-12-29 14:03 ` [PATCH 4/4] Fix emacsclient to work with cygw32 Daniel Colascione
2011-12-29 16:28   ` Juanma Barranquero
2011-12-29 16:36     ` Daniel Colascione
2011-12-29 16:43       ` Juanma Barranquero
2011-12-29 16:47         ` Daniel Colascione
2011-12-29 16:49           ` Juanma Barranquero
2011-12-29 17:39   ` Eli Zaretskii
2011-12-29 17:56     ` Daniel Colascione
2011-12-29 18:18       ` Eli Zaretskii
2011-12-29 18:53         ` Juanma Barranquero
2011-12-29 14:03 ` [PATCH 3/4] Implement cygw32 Daniel Colascione
2011-12-29 17:29   ` Eli Zaretskii
2011-12-29 17:53     ` Daniel Colascione
2011-12-29 18:17       ` Eli Zaretskii
2011-12-29 21:50         ` Daniel Colascione
2011-12-30  0:56           ` Jason Rumney
2011-12-30  9:23           ` Eli Zaretskii
2011-12-30  9:36             ` Daniel Colascione
2011-12-30 11:16               ` Eli Zaretskii
2011-12-30  9:49   ` Andreas Schwab
2011-12-29 14:03 ` [PATCH 2/4] Refactor window-system configuration Daniel Colascione
2011-12-29 22:21   ` Dan Nicolaescu
2011-12-29 22:29     ` Daniel Colascione
2011-12-29 22:43       ` Dan Nicolaescu
2011-12-29 22:48         ` Daniel Colascione
2011-12-29 23:05           ` Dan Nicolaescu
2011-12-29 23:08             ` Daniel Colascione
2011-12-30  8:54             ` Eli Zaretskii
2011-12-30  9:07               ` Daniel Colascione
2011-12-30  9:44                 ` Eli Zaretskii
2011-12-30  0:53       ` Stefan Monnier
2011-12-30  8:21       ` Eli Zaretskii
2011-12-30  8:26         ` Daniel Colascione
2011-12-30  9:38           ` Eli Zaretskii
2011-12-30  9:45             ` Daniel Colascione
2011-12-30 11:15             ` Stefan Monnier
2011-12-30  8:19     ` Eli Zaretskii
2011-12-29 14:03 ` [PATCH 1/4] Compilation cleanups Daniel Colascione
2011-12-29 17:54   ` Paul Eggert
2011-12-31 14:50     ` Ken Brown
2011-12-31 20:29       ` Paul Eggert

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).