Merge remote branch 'savannah/master' into sourceview
authorJohn Darrington <john@darrington.wattle.id.au>
Fri, 1 Jul 2011 12:54:36 +0000 (14:54 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Fri, 1 Jul 2011 12:54:36 +0000 (14:54 +0200)
Conflicts:
src/ui/gui/automake.mk
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-syntax-window.c
src/ui/gui/psppire-syntax-window.h

1  2 
INSTALL
configure.ac
src/ui/gui/automake.mk
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-syntax-window.c
src/ui/gui/psppire-syntax-window.h
src/ui/gui/syntax-editor.ui

diff --combined INSTALL
index 23bbf8a4b1ead5afaf8aab88c36afbb68f835808,1d63d4b5982add4a7fabfc1b4f1c0e53e4c8fd37..3e9456ef3507893e69018fda27b82b5851999f3d
+++ b/INSTALL
@@@ -55,10 -55,6 +55,10 @@@ use the GUI, you must run `configure' w
  
      * GTK+ (http://www.gtk.org/), version 2.12.0 or later.
  
 +    * GtkSourceView (http://projects.gnome.org/gtksourceview/) 
 +      version 2.2 or later.
 +
 +
  To cross-compile PSPP, you will likely need to set the
  PKG_CONFIG_LIBDIR environment variable to point to an
  appropriate pkg-config for the cross-compilation environment.
@@@ -89,8 -85,14 +89,14 @@@ The following packages are optional
        later.  Installing Texinfo will allow you to build PSPP
        documentation in PostScript or PDF format.
  
-     * libpq, from Postgresql (http://postgresql.org). This enables PSPP 
-       to read Postgresql databases.
+     * libpq, from Postgresql (http://postgresql.org).  This enables PSPP 
+       to read Postgresql databases.  The tests for the Postgresql
+       interface, but not the Postgresql interface itself, requires the
+       Postgresql server to be installed.
+     * The Text::Diff module for Perl (http://cpan.org).  This enables
+       PSPP to test the Perl module more thoroughly.  It is not needed
+       to build or use the Perl module.
  
  Basic Installation
  ==================
@@@ -163,6 -165,10 +169,10 @@@ release
       root permissions.  If you cannot get root permissions, see
       "Installation Names", below.
  
+      Please note:  The `make install' target does NOT install the perl
+      module (see below).  To install the perl module, you must change to
+      the `perl-module' directory and manually run `make install' there.
    5. You can remove the program binaries and object files from the
       source code directory by typing `make clean'.  To also remove the
       files that `configure' created (so you can compile the package for
diff --combined configure.ac
index abf7afadedb7f247295e88545270abbaad9dec53,eb10e560a2d2a4362a6f17ab269855b9ade0ecee..766d5a43859d43a26aa9c7771dcf959379861a98
@@@ -2,7 -2,7 +2,7 @@@ dnl Process this file with autoconf to 
  
  dnl Initialize.
  AC_PREREQ(2.63)
- AC_INIT([GNU PSPP], [0.7.6], [bug-gnu-pspp@gnu.org], [pspp])
+ AC_INIT([GNU PSPP], [0.7.8], [bug-gnu-pspp@gnu.org], [pspp])
  AC_CONFIG_AUX_DIR([build-aux])
  AC_CONFIG_HEADERS([config.h])
  AC_CONFIG_TESTDIR([tests])
@@@ -32,15 -32,14 +32,14 @@@ AM_CONDITIONAL(cc_is_gcc, test x"$GCC" 
  PSPP_CC_FOR_BUILD
  PSPP_PERL
  
- dnl Internationalization macros.
- AC_PROVIDE([AM_PO_SUBDIRS])   # PSPP provides its own po/ support.
+ dnl Disable automatic po/ support, because PSPP provides its own po/ support.
+ AC_PROVIDE([AM_PO_SUBDIRS])
  AM_GNU_GETTEXT([external], [need-ngettext])
- AM_GNU_GETTEXT_VERSION([0.17])
+ LIBS="$LIBINTL $LIBS"
  
  dnl Checks for libraries.
  AC_SYS_LARGEFILE
  AC_SEARCH_LIBS([sin], [m])
- AC_SEARCH_LIBS([dcgettext], [intl])
  PSPP_LC_PAPER
  
  
@@@ -75,15 -74,14 +74,17 @@@ AM_CONDITIONAL([HAVE_GUI]
  if test "$with_cairo" != no && test "$with_gui" != "no"; then
    PKG_CHECK_MODULES([GTK], [gtk+-2.0 >= 2.16], [],
      [PSPP_REQUIRED_PREREQ([gtk+ 2.0 version 2.16 or later (or use --without-gui)])])
 +
 +  PKG_CHECK_MODULES([GTKSOURCEVIEW], [gtksourceview-2.0 >= 2.2], [],
 +    [PSPP_REQUIRED_PREREQ([gtksourceview 2.0 version 2.2 or later (or use --without-gui)])])
  
    AC_ARG_VAR([GLIB_GENMARSHAL])
    AC_CHECK_PROGS([GLIB_GENMARSHAL], [glib-genmarshal])
    if test "x$GLIB_GENMARSHAL" = x; then
      PSPP_REQUIRED_PREREQ([glib-genmarshal (or use --without-gui)])
    fi
+   gl_NEXT_HEADERS([gtk/gtk.h])
  fi
  
  dnl Checks needed for psql reader
@@@ -291,12 -289,24 +292,24 @@@ PSPP_READLIN
  dnl Checks for header files.
  AC_CHECK_HEADERS([sys/wait.h fpu_control.h ieeefp.h fenv.h pwd.h])
  
+ dnl Some systems dont have SIGWINCH
+ AC_CHECK_DECLS([SIGWINCH], [], [],
+           [#include <signal.h>
+           /* NetBSD declares sys_siglist in unistd.h.  */
+           #ifdef HAVE_UNISTD_H
+           # include <unistd.h>
+           #endif
+           ])
  # For gnulib.
  gl_INIT
  
  AC_C_INLINE
  
- AC_CHECK_SIZEOF(double)
+ AC_CHECK_SIZEOF([size_t])
+ SIZEOF_SIZE_T=$ac_cv_sizeof_size_t
+ AC_SUBST([SIZEOF_SIZE_T])
  
  AC_C_BIGENDIAN
  
@@@ -313,9 -323,11 +326,11 @@@ if test x"$enable_debug" = x"yes"  ; th
  fi
  
  # iconv is required
+ AM_ICONV
  if test "$am_cv_func_iconv" != "yes"; then
     PSPP_REQUIRED_PREREQ([iconv (see http://www.gnu.org/software/libiconv/)])
  fi
+ LIBS="$LIBICONV $LIBS"
  
  dnl Required by the gnulib 'relocatable-prog' module.
  dnl See doc/relocatable-maint.texi in the gnulib tree for details.
diff --combined src/ui/gui/automake.mk
index fd78196df891d92fa48d2345bb61575db8b714b0,31253dbd99123c108a25a799c85bd3d82a350871..6097d16b82d3a2d9dcec1b46963d4cf06330a337
@@@ -10,11 -10,13 +10,13 @@@ UI_FILES = 
        src/ui/gui/crosstabs.ui \
        src/ui/gui/chi-square.ui \
        src/ui/gui/descriptives.ui \
+       src/ui/gui/entry-dialog.ui \
        src/ui/gui/examine.ui \
        src/ui/gui/goto-case.ui \
        src/ui/gui/factor.ui \
        src/ui/gui/find.ui \
        src/ui/gui/frequencies.ui \
+       src/ui/gui/k-related.ui \
        src/ui/gui/oneway.ui \
        src/ui/gui/psppire.ui \
        src/ui/gui/rank.ui \
@@@ -42,7 -44,7 +44,7 @@@ EXTRA_DIST += 
  if HAVE_GUI
  bin_PROGRAMS += src/ui/gui/psppire 
  
 -src_ui_gui_psppire_CFLAGS = $(GTK_CFLAGS) -Wall -DGDK_MULTIHEAD_SAFE=1
 +src_ui_gui_psppire_CFLAGS = $(GTK_CFLAGS) $(GTKSOURCEVIEW_CFLAGS) -Wall -DGDK_MULTIHEAD_SAFE=1
  
  
  src_ui_gui_psppire_LDFLAGS = \
@@@ -64,9 -66,7 +66,9 @@@ src_ui_gui_psppire_LDADD = 
        src/libpspp.la \
        src/libpspp-core.la \
        $(GTK_LIBS) \
 -      $(CAIRO_LIBS)
 +      $(GTKSOURCEVIEW_LIBS) \
 +      $(CAIRO_LIBS) \
 +      $(LIBINTL)
  
  src_ui_gui_psppiredir = $(pkgdatadir)
  
@@@ -75,9 -75,6 +77,9 @@@ themedir = $(DESTDIR)$(datadir)/icons/h
  context = apps
  
  
 +install-lang:
 +      $(INSTALL) $(top_srcdir)/src/ui/gui/pspp.lang $(pkgdatadir)
 +      
  install-icons:
        for size in 16x16 ; do \
          $(MKDIR_P) $(themedir)/$$size/$(context) ; \
@@@ -85,7 -82,7 +87,7 @@@
        done 
        gtk-update-icon-cache --ignore-theme-index $(themedir)
  
 -INSTALL_DATA_HOOKS += install-icons
 +INSTALL_DATA_HOOKS += install-icons install-lang
  
  uninstall-icons:
        for size in 16x16 ; do \
@@@ -97,7 -94,6 +99,7 @@@ UNINSTALL_DATA_HOOKS += uninstall-icon
  
  dist_src_ui_gui_psppire_DATA = \
        $(UI_FILES) \
 +      $(top_srcdir)/src/ui/gui/pspp.lang \
        $(top_srcdir)/src/ui/gui/pspplogo.png \
        $(top_srcdir)/src/ui/gui/icons/value-labels.png \
        $(top_srcdir)/src/ui/gui/icons/goto-variable.png\
@@@ -147,6 -143,8 +149,8 @@@ src_ui_gui_psppire_SOURCES = 
        src/ui/gui/dialog-common.h \
        src/ui/gui/dict-display.h \
        src/ui/gui/dict-display.c \
+       src/ui/gui/entry-dialog.c \
+       src/ui/gui/entry-dialog.h \
        src/ui/gui/examine-dialog.c \
        src/ui/gui/examine-dialog.h \
        src/ui/gui/executor.c \
        src/ui/gui/help-menu.c \
        src/ui/gui/help-menu.h \
        src/ui/gui/helper.h \
+       src/ui/gui/k-related-dialog.c \
+       src/ui/gui/k-related-dialog.h \
        src/ui/gui/main.c \
        src/ui/gui/missing-val-dialog.c \
        src/ui/gui/missing-val-dialog.h \
        src/ui/gui/psppire-dict.h \
        src/ui/gui/psppire-dictview.c \
        src/ui/gui/psppire-dictview.h \
+       src/ui/gui/psppire-encoding-selector.c \
+       src/ui/gui/psppire-encoding-selector.h \
        src/ui/gui/psppire-hbuttonbox.h \
        src/ui/gui/psppire-keypad.h \
        src/ui/gui/psppire-output-window.c \
        src/ui/gui/sort-cases-dialog.h \
        src/ui/gui/split-file-dialog.c \
        src/ui/gui/split-file-dialog.h \
-       src/ui/gui/syntax-editor-source.c \
-       src/ui/gui/syntax-editor-source.h \
        src/ui/gui/text-data-import-dialog.c \
        src/ui/gui/text-data-import-dialog.h \
        src/ui/gui/transpose-dialog.c \
@@@ -256,7 -256,6 +262,6 @@@ nodist_src_ui_gui_psppire_SOURCES = 
        src/ui/gui/psppire-marshal.c \
        src/ui/gui/psppire-marshal.h
  
  yelp-check:
        @if ! yelp --version > /dev/null 2>&1 ; then \
                echo    ; \
@@@ -289,3 -288,20 +294,20 @@@ endif HAVE_GU
  #ensure the installcheck passes even if there is no X server available
  installcheck-local:
        DISPLAY=/invalid/port $(MAKE) $(AM_MAKEFLAGS) installcheck-binPROGRAMS
+ # <gtk/gtk.h> wrapper
+ src_ui_gui_psppire_CPPFLAGS = $(AM_CPPFLAGS) -Isrc/ui/gui/include
+ BUILT_SOURCES += src/ui/gui/include/gtk/gtk.h
+ src/ui/gui/include/gtk/gtk.h: src/ui/gui/include/gtk/gtk.in.h
+       @$(MKDIR_P) src/ui/gui/include/gtk
+       $(AM_V_GEN)rm -f $@-t $@ && \
+       { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+         sed -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
+             -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
+             -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
+             -e 's|@''NEXT_GTK_GTK_H''@|$(NEXT_GTK_GTK_H)|g' \
+             < $(srcdir)/src/ui/gui/include/gtk/gtk.in.h; \
+       } > $@-t && \
+       mv $@-t $@
+ CLEANFILES += src/ui/gui/include/gtk/gtk.h
+ EXTRA_DIST += src/ui/gui/include/gtk/gtk.in.h
index aace66fa5a5947138087bd3656d6bc5c1741be77,10e3fed57b9a5cad701c99db38a1a5973def399f..be4702d34349ee65a1e4ca47726dc08c262a1c34
@@@ -1,5 -1,5 +1,5 @@@
  /* PSPPIRE - a graphical user interface for PSPP.
-    Copyright (C) 2008, 2009, 2010  Free Software Foundation
+    Copyright (C) 2008, 2009, 2010, 2011  Free Software Foundation
  
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
  
  #include <config.h>
  
- #include <gtk/gtksignal.h>
- #include <gtk/gtkbox.h>
+ #include <gtk/gtk.h>
  #include <stdlib.h>
  
- #include "data/any-reader.h"
- #include "data/procedure.h"
- #include "language/syntax-string-source.h"
+ #include "data/dataset.h"
+ #include "data/session.h"
+ #include "language/lexer/lexer.h"
  #include "libpspp/message.h"
- #include "ui/gui/help-menu.h"
+ #include "libpspp/str.h"
+ #include "ui/gui/aggregate-dialog.h"
  #include "ui/gui/binomial-dialog.h"
+ #include "ui/gui/chi-square-dialog.h"
  #include "ui/gui/comments-dialog.h"
  #include "ui/gui/compute-dialog.h"
  #include "ui/gui/correlation-dialog.h"
- #include "ui/gui/chi-square-dialog.h"
  #include "ui/gui/crosstabs-dialog.h"
  #include "ui/gui/descriptives-dialog.h"
+ #include "ui/gui/entry-dialog.h"
  #include "ui/gui/examine-dialog.h"
  #include "ui/gui/executor.h"
  #include "ui/gui/factor-dialog.h"
  #include "ui/gui/find-dialog.h"
  #include "ui/gui/frequencies-dialog.h"
  #include "ui/gui/goto-case-dialog.h"
+ #include "ui/gui/help-menu.h"
  #include "ui/gui/helper.h"
+ #include "ui/gui/k-related-dialog.h"
  #include "ui/gui/oneway-anova-dialog.h"
  #include "ui/gui/psppire-data-window.h"
  #include "ui/gui/psppire-syntax-window.h"
  #include "ui/gui/t-test-paired-samples.h"
  #include "ui/gui/text-data-import-dialog.h"
  #include "ui/gui/transpose-dialog.h"
- #include "ui/gui/aggregate-dialog.h"
  #include "ui/gui/variable-info-dialog.h"
  #include "ui/gui/weight-cases-dialog.h"
  #include "ui/syntax-gen.h"
  
+ #include "gl/c-strcase.h"
+ #include "gl/c-strcasestr.h"
+ #include "gl/xvasprintf.h"
  #include <gettext.h>
  #define _(msgid) gettext (msgid)
  #define N_(msgid) msgid
  
+ struct session *the_session;
+ struct ll_list all_data_windows = LL_INITIALIZER (all_data_windows);
  
- static void psppire_data_window_base_finalize (PsppireDataWindowClass *, gpointer);
- static void psppire_data_window_base_init     (PsppireDataWindowClass *class);
  static void psppire_data_window_class_init    (PsppireDataWindowClass *class);
  static void psppire_data_window_init          (PsppireDataWindow      *data_editor);
  
  
  static void psppire_data_window_iface_init (PsppireWindowIface *iface);
  
+ static void psppire_data_window_dispose (GObject *object);
+ static void psppire_data_window_set_property (GObject         *object,
+                                               guint            prop_id,
+                                               const GValue    *value,
+                                               GParamSpec      *pspec);
+ static void psppire_data_window_get_property (GObject         *object,
+                                               guint            prop_id,
+                                               GValue          *value,
+                                               GParamSpec      *pspec);
  
  GType
  psppire_data_window_get_type (void)
        static const GTypeInfo psppire_data_window_info =
        {
          sizeof (PsppireDataWindowClass),
-         (GBaseInitFunc) psppire_data_window_base_init,
-         (GBaseFinalizeFunc) psppire_data_window_base_finalize,
+         NULL,
+         NULL,
          (GClassInitFunc)psppire_data_window_class_init,
          (GClassFinalizeFunc) NULL,
          NULL,
  
  static GObjectClass *parent_class ;
  
- static void
- psppire_data_window_finalize (GObject *object)
- {
-   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (object);
-   g_object_unref (de->builder);
-   if (G_OBJECT_CLASS (parent_class)->finalize)
-     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
- }
+ enum {
+     PROP_DATASET = 1
+ };
  
  static void
  psppire_data_window_class_init (PsppireDataWindowClass *class)
- {
-   parent_class = g_type_class_peek_parent (class);
- }
- static void
- psppire_data_window_base_init (PsppireDataWindowClass *class)
  {
    GObjectClass *object_class = G_OBJECT_CLASS (class);
  
-   object_class->finalize = psppire_data_window_finalize;
- }
+   parent_class = g_type_class_peek_parent (class);
  
+   object_class->dispose = psppire_data_window_dispose;
+   object_class->set_property = psppire_data_window_set_property;
+   object_class->get_property = psppire_data_window_get_property;
  
- static void
- psppire_data_window_base_finalize (PsppireDataWindowClass *class,
-                                  gpointer class_data)
- {
+   g_object_class_install_property (
+     object_class, PROP_DATASET,
+     g_param_spec_pointer ("dataset", "Dataset",
+                           "'struct datset *' represented by the window",
+                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
  }
  \f
- extern PsppireVarStore *the_var_store;
- extern struct dataset *the_dataset;
- extern PsppireDataStore *the_data_store ;
- extern GtkRecentManager *the_recent_mgr;
  static void
  set_paste_menuitem_sensitivity (PsppireDataWindow *de, gboolean x)
  {
@@@ -184,11 -175,9 +175,9 @@@ set_cut_copy_menuitem_sensitivity (Pspp
  
  /* Run the EXECUTE command. */
  static void
- execute (void)
+ execute (PsppireDataWindow *dw)
  {
-   struct getl_interface *sss = create_syntax_string_source ("EXECUTE.");
-   execute_syntax (sss);
+   execute_const_syntax_string (dw, "EXECUTE.");
  }
  
  static void
@@@ -348,165 -337,71 +337,71 @@@ dump_rm (GtkRecentManager *rm
  }
  #endif
  
  static gboolean
load_file (PsppireWindow *de, const gchar *file_name)
name_has_por_suffix (const gchar *name)
  {
-   gchar *native_file_name;
-   struct getl_interface *sss;
-   struct string filename;
-   ds_init_empty (&filename);
-   native_file_name =
-     convert_glib_filename_to_system_filename (file_name, NULL);
-   syntax_gen_string (&filename, ss_cstr (native_file_name));
-   g_free (native_file_name);
-   sss = create_syntax_format_source ("GET FILE=%s.",
-                                    ds_cstr (&filename));
-   ds_destroy (&filename);
-   if (execute_syntax (sss) )
-     return TRUE;
-   return FALSE;
+   size_t length = strlen (name);
+   return length > 4 && !c_strcasecmp (&name[length - 4], ".por");
  }
  
- static GtkWidget *
sysfile_chooser_dialog (PsppireWindow *toplevel)
+ static gboolean
name_has_sav_suffix (const gchar *name)
  {
-   GtkWidget *dialog =
-     gtk_file_chooser_dialog_new (_("Open"),
-                                GTK_WINDOW (toplevel),
-                                GTK_FILE_CHOOSER_ACTION_OPEN,
-                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-                                GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-                                NULL);
-   GtkFileFilter *filter;
-   filter = gtk_file_filter_new ();
-   gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
-   gtk_file_filter_add_pattern (filter, "*.sav");
-   gtk_file_filter_add_pattern (filter, "*.SAV");
-   gtk_file_filter_add_pattern (filter, "*.por");
-   gtk_file_filter_add_pattern (filter, "*.POR");
-   gtk_file_filter_add_pattern (filter, "*.sps");
-   gtk_file_filter_add_pattern (filter, "*.SPS");
-   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-   filter = gtk_file_filter_new ();
-   gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
-   gtk_file_filter_add_pattern (filter, "*.sav");
-   gtk_file_filter_add_pattern (filter, "*.SAV");
-   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-   filter = gtk_file_filter_new ();
-   gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
-   gtk_file_filter_add_pattern (filter, "*.por");
-   gtk_file_filter_add_pattern (filter, "*.POR");
-   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-   filter = gtk_file_filter_new ();
-   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
-   gtk_file_filter_add_pattern (filter, "*.sps");
-   gtk_file_filter_add_pattern (filter, "*.SPS");
-   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-   filter = gtk_file_filter_new ();
-   gtk_file_filter_set_name (filter, _("All Files"));
-   gtk_file_filter_add_pattern (filter, "*");
-   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
-   {
-     gchar *dir_name;
-     gchar *filename = NULL;
-     g_object_get (toplevel, "filename", &filename, NULL);
-     if ( ! g_path_is_absolute (filename))
-       {
-       gchar *path =
-         g_build_filename (g_get_current_dir (), filename, NULL);
-       dir_name = g_path_get_dirname (path);
-       g_free (path);
-       }
-     else
-       {
-       dir_name = g_path_get_dirname (filename);
-       }
-     gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
-                                        dir_name);
-     free (dir_name);
-   }
+   size_t length = strlen (name);
+   return length > 4 && !c_strcasecmp (&name[length - 4], ".sav");
+ }
  
-   return dialog;
+ /* Returns true if NAME has a suffix which might denote a PSPP file */
+ static gboolean
+ name_has_suffix (const gchar *name)
+ {
+   return name_has_por_suffix (name) || name_has_sav_suffix (name);
  }
  
- /* Callback for the data_open action.
-    Prompts for a filename and opens it */
- static void
- open_window (PsppireWindow *de)
+ static gboolean
+ load_file (PsppireWindow *de, const gchar *file_name)
  {
-   GtkWidget *dialog = sysfile_chooser_dialog (de);
+   struct string filename;
+   gchar *utf8_file_name;
+   const char *mime_type;
+   gchar *syntax;
+   bool ok;
  
-   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
-     {
-     case GTK_RESPONSE_ACCEPT:
-       {
-       gchar *name =
-         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+   ds_init_empty (&filename);
  
-       gchar *sysname = convert_glib_filename_to_system_filename (name, NULL);
+   utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL);
  
-       if (any_reader_may_open (sysname))
-         psppire_window_load (de, name);
-       else
-         open_new_syntax_window (name);
+   syntax_gen_string (&filename, ss_cstr (utf8_file_name));
  
-       g_free (sysname);
-       g_free (name);
-       }
-       break;
-     default:
-       break;
-     }
+   g_free (utf8_file_name);
  
-   gtk_widget_destroy (dialog);
- }
+   syntax = g_strdup_printf ("GET FILE=%s.", ds_cstr (&filename));
+   ds_destroy (&filename);
  
- /* Returns true if NAME has a suffix which might denote a PSPP file */
- static gboolean
- name_has_suffix (const gchar *name)
- {
-   if ( g_str_has_suffix (name, ".sav"))
-     return TRUE;
-   if ( g_str_has_suffix (name, ".SAV"))
-     return TRUE;
-   if ( g_str_has_suffix (name, ".por"))
-     return TRUE;
-   if ( g_str_has_suffix (name, ".POR"))
-     return TRUE;
-   return FALSE;
- }
+   ok = execute_syntax (PSPPIRE_DATA_WINDOW (de),
+                        lex_reader_for_string (syntax));
+   g_free (syntax);
  
+   mime_type = (name_has_por_suffix (file_name)
+                ? "application/x-spss-por"
+                : "application/x-spss-sav");
+   add_most_recent (ds_cstr (&filename), mime_type);
+   return ok;
+ }
  
  /* Save DE to file */
  static void
  save_file (PsppireWindow *w)
  {
-   gchar *native_file_name = NULL;
-   gchar *file_name = NULL;
+   const gchar *file_name = NULL;
+   gchar *utf8_file_name = NULL;
    GString *fnx;
-   struct getl_interface *sss;
    struct string filename ;
    PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (w);
+   gchar *syntax;
  
-   g_object_get (w, "filename", &file_name, NULL);
+   file_name = psppire_window_get_filename (w);
  
    fnx = g_string_new (file_name);
  
  
    ds_init_empty (&filename);
  
-   native_file_name =
-     convert_glib_filename_to_system_filename (fnx->str, NULL);
+   utf8_file_name = g_filename_to_utf8 (fnx->str, -1, NULL, NULL, NULL);
  
    g_string_free (fnx, TRUE);
  
-   syntax_gen_string (&filename, ss_cstr (native_file_name));
-   g_free (native_file_name);
+   syntax_gen_string (&filename, ss_cstr (utf8_file_name));
+   g_free (utf8_file_name);
  
-   if ( de->save_as_portable )
-     {
-       sss = create_syntax_format_source ("EXPORT OUTFILE=%s.",
-                                        ds_cstr (&filename));
-     }
-   else
-     {
-       sss = create_syntax_format_source ("SAVE OUTFILE=%s.",
-                                        ds_cstr (&filename));
-     }
+   syntax = g_strdup_printf ("%s OUTFILE=%s.",
+                             de->save_as_portable ? "EXPORT" : "SAVE",
+                             ds_cstr (&filename));
  
    ds_destroy (&filename);
  
-   execute_syntax (sss);
+   g_free (execute_syntax_string (de, syntax));
  }
  
  
@@@ -561,47 -448,44 +448,44 @@@ on_insert_variable (PsppireDataWindow *
  static void
  display_dict (PsppireDataWindow *de)
  {
-   struct getl_interface *sss =
-     create_syntax_string_source ("DISPLAY DICTIONARY.");
-   execute_syntax (sss);
+   execute_const_syntax_string (de, "DISPLAY DICTIONARY.");
  }
  
  static void
  sysfile_info (PsppireDataWindow *de)
  {
-   GtkWidget *dialog = sysfile_chooser_dialog (PSPPIRE_WINDOW (de));
+   GtkWidget *dialog = psppire_window_file_chooser_dialog (PSPPIRE_WINDOW (de));
  
    if  ( GTK_RESPONSE_ACCEPT == gtk_dialog_run (GTK_DIALOG (dialog)))
      {
        struct string filename;
-       struct getl_interface *sss;
        gchar *file_name =
        gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  
-       gchar *native_file_name =
-       convert_glib_filename_to_system_filename (file_name, NULL);
+       gchar *utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL,
+                                                   NULL);
+       gchar *syntax;
  
        ds_init_empty (&filename);
  
-       syntax_gen_string (&filename, ss_cstr (native_file_name));
+       syntax_gen_string (&filename, ss_cstr (utf8_file_name));
  
-       g_free (native_file_name);
+       g_free (utf8_file_name);
  
-       sss = create_syntax_format_source ("SYSFILE INFO %s.",
-                                        ds_cstr (&filename));
-       execute_syntax (sss);
+       syntax = g_strdup_printf ("SYSFILE INFO %s.", ds_cstr (&filename));
+       g_free (execute_syntax_string (de, syntax));
      }
  
    gtk_widget_destroy (dialog);
  }
  
  
- /* Callback for data_save_as action. Prompt for a filename and save */
+ /* PsppireWindow 'pick_filename' callback: prompt for a filename to save as. */
  static void
- data_save_as_dialog (PsppireDataWindow *de)
+ data_pick_filename (PsppireWindow *window)
  {
+   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (window);
    GtkWidget *button_sys;
    GtkWidget *dialog =
      gtk_file_chooser_dialog_new (_("Save"),
      gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(dialog), vbox);
    }
  
+   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
+                                                   TRUE);
    switch (gtk_dialog_run (GTK_DIALOG (dialog)))
      {
      case GTK_RESPONSE_ACCEPT:
  
        psppire_window_set_filename (PSPPIRE_WINDOW (de), filename->str);
  
-       save_file (PSPPIRE_WINDOW (de));
        g_string_free (filename, TRUE);
        }
        break;
    gtk_widget_destroy (dialog);
  }
  
- /* Callback for data_save action.
-  */
- static void
data_save (PsppireWindow *de)
+ static bool
+ confirm_delete_dataset (PsppireDataWindow *de,
+                         const char *old_dataset,
+                         const char *new_dataset,
                        const char *existing_dataset)
  {
-   const gchar *fn = psppire_window_get_filename (de);
+   GtkWidget *dialog;
+   int result;
  
-   if ( NULL != fn)
-     psppire_window_save (de);
-   else
-     data_save_as_dialog (PSPPIRE_DATA_WINDOW (de));
- }
+   dialog = gtk_message_dialog_new (
+     GTK_WINDOW (de), 0, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s",
+     _("Delete Existing Dataset?"));
  
+   gtk_message_dialog_format_secondary_text (
+     GTK_MESSAGE_DIALOG (dialog),
+     _("Renaming \"%s\" to \"%s\" will delete destroy the existing "
+       "dataset named \"%s\".  Are you sure that you want to do this?"),
+     old_dataset, new_dataset, existing_dataset);
  
- /* Callback for data_new action.
-    Performs the NEW FILE command */
- static void
- new_file (PsppireDataWindow *de)
- {
-   struct getl_interface *sss =
-     create_syntax_string_source ("NEW FILE.");
+   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                           GTK_STOCK_DELETE, GTK_RESPONSE_OK,
+                           NULL);
  
-   execute_syntax (sss);
+   g_object_set (dialog, "icon-name", "psppicon", NULL);
  
-   psppire_window_set_filename (PSPPIRE_WINDOW (de), NULL);
- }
+   result = gtk_dialog_run (GTK_DIALOG (dialog));
  
+   gtk_widget_destroy (dialog);
  
+   return result == GTK_RESPONSE_OK;
+ }
+ static void
+ on_rename_dataset (PsppireDataWindow *de)
+ {
+   struct dataset *ds = de->dataset;
+   struct session *session = dataset_session (ds);
+   const char *old_name = dataset_name (ds);
+   struct dataset *existing_dataset;
+   char *new_name;
+   char *prompt;
+   prompt = xasprintf (_("Please enter a new name for dataset \"%s\":"),
+                       old_name);
+   new_name = entry_dialog_run (GTK_WINDOW (de), _("Rename Dataset"), prompt,
+                                old_name);
+   free (prompt);
+   if (new_name == NULL)
+     return;
+   existing_dataset = session_lookup_dataset (session, new_name);
+   if (existing_dataset == NULL || existing_dataset == ds
+       || confirm_delete_dataset (de, old_name, new_name,
+                                  dataset_name (existing_dataset)))
+     g_free (execute_syntax_string (de, g_strdup_printf ("DATASET NAME %s.",
+                                                         new_name)));
+   free (new_name);
+ }
  
  static void
  on_edit_paste (PsppireDataWindow  *de)
@@@ -819,13 -736,11 +736,11 @@@ toggle_split_window (PsppireDataWindo
  
  
  static void
- file_quit (void)
+ file_quit (PsppireDataWindow *de)
  {
    /* FIXME: Need to be more intelligent here.
       Give the user the opportunity to save any unsaved data.
    */
-   g_object_unref (the_data_store);
    psppire_quit ();
  }
  
@@@ -843,28 -758,76 +758,77 @@@ on_recent_data_select (GtkMenuShell *me
  
    g_free (uri);
  
-   psppire_window_load (window, file);
+   open_data_window (window, file);
  
    g_free (file);
  }
  
+ static char *
+ charset_from_mime_type (const char *mime_type)
+ {
+   const char *charset;
+   struct string s;
+   const char *p;
+   if (mime_type == NULL)
+     return NULL;
+   charset = c_strcasestr (mime_type, "charset=");
+   if (charset == NULL)
+     return NULL;
+   ds_init_empty (&s);
+   p = charset + 8;
+   if (*p == '"')
+     {
+       /* Parse a "quoted-string" as defined by RFC 822. */
+       for (p++; *p != '\0' && *p != '"'; p++)
+         {
+           if (*p != '\\')
+             ds_put_byte (&s, *p);
+           else if (*++p != '\0')
+             ds_put_byte (&s, *p);
+         }
+     }
+   else
+     {
+       /* Parse a "token" as defined by RFC 2045. */
+       while (*p > 32 && *p < 127 && strchr ("()<>@,;:\\\"/[]?=", *p) == NULL)
+         ds_put_byte (&s, *p++);
+     }
+   if (!ds_is_empty (&s))
+     return ds_steal_cstr (&s);
+   ds_destroy (&s);
+   return NULL;
+ }
  static void
  on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
  {
+   GtkRecentInfo *item;
+   char *encoding;
+   GtkWidget *se;
    gchar *file;
  
-   gchar *uri =
-     gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
+   /* Get the file name and its encoding. */
+   item = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (menushell));
+   file = g_filename_from_uri (gtk_recent_info_get_uri (item), NULL, NULL);
+   encoding = charset_from_mime_type (gtk_recent_info_get_mime_type (item));
+   gtk_recent_info_unref (item);
  
-   file = g_filename_from_uri (uri, NULL, NULL);
+   se = psppire_syntax_window_new (encoding);
  
-   g_free (uri);
+   free (encoding);
  
-   open_new_syntax_window (file);
+   if ( psppire_window_load (PSPPIRE_WINDOW (se), file) ) 
+     gtk_widget_show (se);
+   else
+     gtk_widget_destroy (se);
  
    g_free (file);
  }
 +
  
  
  static void
@@@ -904,15 -867,14 +868,14 @@@ on_switch_sheet (GtkNotebook *notebook
    switch (page_num)
      {
      case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
-       gtk_widget_hide (view_variables);
-       gtk_widget_show (view_data);
+       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (view_variables),
+                                       TRUE);
        gtk_action_set_sensitive (de->insert_variable, TRUE);
        gtk_action_set_sensitive (de->insert_case, FALSE);
        gtk_action_set_sensitive (de->invoke_goto_dialog, FALSE);
        break;
      case PSPPIRE_DATA_EDITOR_DATA_VIEW:
-       gtk_widget_show (view_variables);
-       gtk_widget_hide (view_data);
+       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (view_data), TRUE);
        gtk_action_set_sensitive (de->invoke_goto_dialog, TRUE);
        gtk_action_set_sensitive (de->insert_case, TRUE);
        break;
@@@ -949,36 -911,68 +912,68 @@@ connect_action (PsppireDataWindow *dw, 
    return action;
  }
  
+ /* Initializes as much of a PsppireDataWindow as we can and must before the
+    dataset has been set.
+    In particular, the 'menu' member is required in case the "filename" property
+    is set before the "dataset" property: otherwise PsppireWindow will try to
+    modify the menu as part of the "filename" property_set() function and end up
+    with a Gtk-CRITICAL since 'menu' is NULL.  */
  static void
  psppire_data_window_init (PsppireDataWindow *de)
  {
-   PsppireVarStore *vs;
-   PsppireDict *dict = NULL;
+   GtkUIManager *uim;
+   de->builder = builder_new ("data-editor.ui");
+   uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
+   PSPPIRE_WINDOW (de)->menu =
+     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
+ }
+ static void
+ psppire_data_window_finish_init (PsppireDataWindow *de,
+                                  struct dataset *ds)
+ {
+   static const struct dataset_callbacks cbs =
+     {
+       set_unsaved,                    /* changed */
+       transformation_change_callback, /* transformations_changed */
+     };
+   PsppireDict *dict;
  
    GtkWidget *menubar;
    GtkWidget *hb ;
    GtkWidget *sb ;
  
    GtkWidget *box = gtk_vbox_new (FALSE, 0);
-   de->builder = builder_new ("data-editor.ui");
+   de->dataset = ds;
+   dict = psppire_dict_new_from_dict (dataset_dict (ds));
+   de->var_store = psppire_var_store_new (dict);
+   de->data_store = psppire_data_store_new (dict);
+   psppire_data_store_set_reader (de->data_store, NULL);
  
    menubar = get_widget_assert (de->builder, "menubar");
    hb = get_widget_assert (de->builder, "handlebox1");
    sb = get_widget_assert (de->builder, "status-bar");
  
    de->data_editor =
-     PSPPIRE_DATA_EDITOR (psppire_data_editor_new (the_var_store, the_data_store));
+     PSPPIRE_DATA_EDITOR (psppire_data_editor_new (de, de->var_store,
+                                                   de->data_store));
  
-   g_signal_connect_swapped (the_data_store, "case-changed",
+   g_signal_connect_swapped (de->data_store, "case-changed",
                            G_CALLBACK (set_unsaved), de);
  
-   g_signal_connect_swapped (the_data_store, "case-inserted",
+   g_signal_connect_swapped (de->data_store, "case-inserted",
                            G_CALLBACK (set_unsaved), de);
  
-   g_signal_connect_swapped (the_data_store, "cases-deleted",
+   g_signal_connect_swapped (de->data_store, "cases-deleted",
                            G_CALLBACK (set_unsaved), de);
  
-   dataset_set_callback (the_dataset, set_unsaved, de);
+   dataset_set_callbacks (de->dataset, &cbs, de);
  
    connect_help (de->builder);
  
    g_signal_connect_swapped (de->data_editor, "data-available-changed",
                            G_CALLBACK (set_paste_menuitem_sensitivity), de);
  
-   dataset_add_transform_change_callback (the_dataset,
-                                        transformation_change_callback,
-                                        de);
-   vs = the_var_store;
-   g_assert(vs); /* Traps a possible bug in w32 build */
-   g_object_get (vs, "dictionary", &dict, NULL);
    g_signal_connect (dict, "weight-changed",
                    G_CALLBACK (on_weight_change),
                    de);
  
    connect_action (de, "edit_cut", G_CALLBACK (on_edit_cut));
  
-   connect_action (de, "file_new_data", G_CALLBACK (new_file));
+   connect_action (de, "file_new_data", G_CALLBACK (create_data_window));
  
    connect_action (de, "file_import-text", G_CALLBACK (text_data_import_assistant));
  
-   connect_action (de, "file_save", G_CALLBACK (data_save));
+   connect_action (de, "file_save", G_CALLBACK (psppire_window_save));
   
-   connect_action (de, "file_open", G_CALLBACK (open_window));
+   connect_action (de, "file_open", G_CALLBACK (psppire_window_open));
+   connect_action (de, "file_save_as", G_CALLBACK (psppire_window_save_as));
  
-   connect_action (de, "file_save_as", G_CALLBACK (data_save_as_dialog));
+   connect_action (de, "rename_dataset", G_CALLBACK (on_rename_dataset));
  
    connect_action (de, "file_information_working-file", G_CALLBACK (display_dict));
  
    connect_action (de, "chi-square", G_CALLBACK (chisquare_dialog));
  
    connect_action (de, "binomial", G_CALLBACK (binomial_dialog));
+   connect_action (de, "k-related-samples", G_CALLBACK (k_related_dialog));
   
  
    {
        gtk_ui_manager_get_widget (uim,"/ui/menubar/file/file_recent-files");
  
  
-     GtkWidget *menu_data =
-       gtk_recent_chooser_menu_new_for_manager (the_recent_mgr);
+     GtkWidget *menu_data = gtk_recent_chooser_menu_new_for_manager (
+       gtk_recent_manager_get_default ());
  
-     GtkWidget *menu_files =
-       gtk_recent_chooser_menu_new_for_manager (the_recent_mgr);
+     GtkWidget *menu_files = gtk_recent_chooser_menu_new_for_manager (
+       gtk_recent_manager_get_default ());
  
      {
        GtkRecentFilter *filter = gtk_recent_filter_new ();
      GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
  
      merge_help_menu (uim);
-     
-     PSPPIRE_WINDOW (de)->menu =
-       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
    }
  
    {
  
    gtk_widget_show (GTK_WIDGET (de->data_editor));
    gtk_widget_show (box);
+   ll_push_head (&all_data_windows, &de->ll);
+ }
+ static void
+ psppire_data_window_dispose (GObject *object)
+ {
+   PsppireDataWindow *dw = PSPPIRE_DATA_WINDOW (object);
+   if (dw->builder != NULL)
+     {
+       g_object_unref (dw->builder);
+       dw->builder = NULL;
+     }
+   if (dw->var_store)
+     {
+       g_object_unref (dw->var_store);
+       dw->var_store = NULL;
+     }
+   if (dw->data_store)
+     {
+       g_object_unref (dw->data_store);
+       dw->data_store = NULL;
+     }
+   if (dw->ll.next != NULL)
+     {
+       ll_remove (&dw->ll);
+       dw->ll.next = NULL;
+     }
+   if (G_OBJECT_CLASS (parent_class)->dispose)
+     G_OBJECT_CLASS (parent_class)->dispose (object);
  }
  
+ static void
+ psppire_data_window_set_property (GObject         *object,
+                                   guint            prop_id,
+                                   const GValue    *value,
+                                   GParamSpec      *pspec)
+ {
+   PsppireDataWindow *window = PSPPIRE_DATA_WINDOW (object);
+   switch (prop_id)
+     {
+     case PROP_DATASET:
+       psppire_data_window_finish_init (window, g_value_get_pointer (value));
+       break;
+     default:
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       break;
+     };
+ }
+ static void
+ psppire_data_window_get_property (GObject         *object,
+                                   guint            prop_id,
+                                   GValue          *value,
+                                   GParamSpec      *pspec)
+ {
+   PsppireDataWindow *window = PSPPIRE_DATA_WINDOW (object);
+   switch (prop_id)
+     {
+     case PROP_DATASET:
+       g_value_set_pointer (value, window->dataset);
+       break;
+     default:
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       break;
+     };
+ }
  
  GtkWidget*
- psppire_data_window_new (void)
+ psppire_data_window_new (struct dataset *ds)
  {
-   return GTK_WIDGET (g_object_new (psppire_data_window_get_type (),
-                                  /* TRANSLATORS: This will form a filename.  Please avoid whitespace. */
-                                  "filename", _("PSPP-data"),
-                                  "description", _("Data Editor"),
-                                  NULL));
+   GtkWidget *dw;
+   if (the_session == NULL)
+     the_session = session_create ();
+   if (ds == NULL)
+     {
+       static int n_datasets;
+       char *dataset_name;
+       dataset_name = xasprintf ("DataSet%d", ++n_datasets);
+       ds = dataset_create (the_session, dataset_name);
+       free (dataset_name);
+     }
+   assert (dataset_session (ds) == the_session);
+   dw = GTK_WIDGET (
+     g_object_new (
+       psppire_data_window_get_type (),
+       /* TRANSLATORS: This will form a filename.  Please avoid whitespace. */
+       "description", _("Data Editor"),
+       "dataset", ds,
+       NULL));
+   if (dataset_name (ds) != NULL)
+     g_object_set (dw, "id", dataset_name (ds), (void *) NULL);
+   return dw;
  }
  
+ bool
+ psppire_data_window_is_empty (PsppireDataWindow *dw)
+ {
+   return psppire_var_store_get_var_cnt (dw->var_store) == 0;
+ }
  
  static void
  psppire_data_window_iface_init (PsppireWindowIface *iface)
  {
    iface->save = save_file;
+   iface->pick_filename = data_pick_filename;
    iface->load = load_file;
  }
\f
+ PsppireDataWindow *
+ psppire_default_data_window (void)
+ {
+   if (ll_is_empty (&all_data_windows))
+     create_data_window ();
+   return ll_data (ll_head (&all_data_windows), PsppireDataWindow, ll);
+ }
+ void
+ psppire_data_window_set_default (PsppireDataWindow *pdw)
+ {
+   ll_remove (&pdw->ll);
+   ll_push_head (&all_data_windows, &pdw->ll);
+ }
+ void
+ psppire_data_window_undefault (PsppireDataWindow *pdw)
+ {
+   ll_remove (&pdw->ll);
+   ll_push_tail (&all_data_windows, &pdw->ll);
+ }
+ PsppireDataWindow *
+ psppire_data_window_for_dataset (struct dataset *ds)
+ {
+   PsppireDataWindow *pdw;
+   ll_for_each (pdw, PsppireDataWindow, ll, &all_data_windows)
+     if (pdw->dataset == ds)
+       return pdw;
+   return NULL;
+ }
  
+ void
+ create_data_window (void)
+ {
+   gtk_widget_show (psppire_data_window_new (NULL));
+ }
+ void
+ open_data_window (PsppireWindow *victim, const char *file_name)
+ {
+   GtkWidget *window;
+   if (PSPPIRE_IS_DATA_WINDOW (victim)
+       && psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (victim)))
+     window = GTK_WIDGET (victim);
+   else
+     window = psppire_data_window_new (NULL);
+   psppire_window_load (PSPPIRE_WINDOW (window), file_name);
+   gtk_widget_show (window);
+ }
index c7251a1ab77206c9772df3f377b408708ef67ad3,b117825f752f8e7e074ac2f179651733533cd2c9..b5cf0d3345d27cc45474bcc24e3cbe53c6562c0c
@@@ -1,5 -1,5 +1,5 @@@
  /* PSPPIRE - a graphical user interface for PSPP.
-    Copyright (C) 2008, 2009, 2010  Free Software Foundation
+    Copyright (C) 2008, 2009, 2010, 2011  Free Software Foundation
  
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
  
  #include <config.h>
  
- #include "relocatable.h"
- #include <gtk/gtksignal.h>
- #include <gtk/gtkbox.h>
- #include "executor.h"
- #include "helper.h"
+ #include <gtk/gtk.h>
+ #include <stdlib.h>
  
- #include <libpspp/message.h>
- #include <stdlib.h>
- #include "psppire.h"
- #include "psppire-data-window.h"
- #include "psppire-window-register.h"
- #include "psppire.h"
- #include "help-menu.h"
- #include "psppire-syntax-window.h"
- #include "syntax-editor-source.h"
- #include <language/lexer/lexer.h>
 +#include <gtksourceview/gtksourcebuffer.h>
 +#include <gtksourceview/gtksourcelanguage.h>
 +#include <gtksourceview/gtksourcelanguagemanager.h>
 +#include <gtksourceview/gtksourceprintcompositor.h>
 +
- #include "xalloc.h"
 +
+ #include "language/lexer/lexer.h"
+ #include "libpspp/encoding-guesser.h"
+ #include "libpspp/i18n.h"
+ #include "libpspp/message.h"
+ #include "ui/gui/executor.h"
+ #include "ui/gui/help-menu.h"
+ #include "ui/gui/helper.h"
+ #include "ui/gui/psppire-data-window.h"
+ #include "ui/gui/psppire-encoding-selector.h"
+ #include "ui/gui/psppire-syntax-window.h"
+ #include "ui/gui/psppire-syntax-window.h"
+ #include "ui/gui/psppire-window-register.h"
+ #include "ui/gui/psppire.h"
+ #include "ui/gui/psppire.h"
+ #include "gl/localcharset.h"
+ #include "gl/xalloc.h"
+ #include "gl/xvasprintf.h"
  
  #include <gettext.h>
  #define _(msgid) gettext (msgid)
@@@ -56,6 -51,53 +57,53 @@@ static void psppire_syntax_window_ini
  static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
  
  
+ /* Properties */
+ enum
+ {
+   PROP_0,
+   PROP_ENCODING
+ };
+ static void
+ psppire_syntax_window_set_property (GObject         *object,
+                                     guint            prop_id,
+                                     const GValue    *value,
+                                     GParamSpec      *pspec)
+ {
+   PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
+   switch (prop_id)
+     {
+     case PROP_ENCODING:
+       g_free (window->encoding);
+       window->encoding = g_value_dup_string (value);
+       break;
+     default:
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       break;
+     };
+ }
+ static void
+ psppire_syntax_window_get_property (GObject         *object,
+                                     guint            prop_id,
+                                     GValue          *value,
+                                     GParamSpec      *pspec)
+ {
+   PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
+   switch (prop_id)
+     {
+     case PROP_ENCODING:
+       g_value_set_string (value, window->encoding);
+       break;
+     default:
+       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       break;
+     };
+ }
  GType
  psppire_syntax_window_get_type (void)
  {
@@@ -116,6 -158,9 +164,9 @@@ psppire_syntax_window_dispose (GObject 
    if (sw->dispose_has_run)
      return;
  
+   g_free (sw->encoding);
+   sw->encoding = NULL;
    clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
    clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
  
  static void
  psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
  {
++  GParamSpec *encoding_spec;
    GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  
 -  GParamSpec *encoding_spec =
 +  GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
 +
 +  const gchar * const *existing_paths =  gtk_source_language_manager_get_search_path (lm);
 +  gchar **new_paths = g_strdupv ((gchar **)existing_paths);
 +  int n = g_strv_length ((gchar **) existing_paths);
 +
 +  new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
 +  new_paths[n] = g_strdup (relocate (PKGDATADIR));
 +  new_paths[n+1] = NULL;
 +
 +  lm = gtk_source_language_manager_new ();
 +  gtk_source_language_manager_set_search_path (lm, new_paths);
 +
 +  class->lan = gtk_source_language_manager_get_language (lm, "pspp");
 +
 +  if (class->lan == NULL)
 +    g_warning ("pspp.lang file not found.  Syntax highlighting will not be available.");
 +
 +  parent_class = g_type_class_peek_parent (class);
 +
 +  g_strfreev (new_paths);
 +
++  encoding_spec =
+     null_if_empty_param ("encoding",
+                          "Character encoding",
+                          "IANA character encoding in this syntax file",
+                        NULL,
+                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
    parent_class = g_type_class_peek_parent (class);
  
+   gobject_class->set_property = psppire_syntax_window_set_property;
+   gobject_class->get_property = psppire_syntax_window_get_property;
    gobject_class->dispose = psppire_syntax_window_dispose;
+   g_object_class_install_property (gobject_class,
+                                    PROP_ENCODING,
+                                    encoding_spec);
  }
  
  
@@@ -169,6 -205,7 +234,6 @@@ static voi
  psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
  {
    GObjectClass *object_class = G_OBJECT_CLASS (class);
 -
    object_class->finalize = psppire_syntax_window_finalize;
  }
  
@@@ -186,25 -223,31 +251,28 @@@ editor_execute_syntax (const PsppireSyn
                       GtkTextIter stop)
  {
    PsppireWindow *win = PSPPIRE_WINDOW (sw);
-   const gchar *name = psppire_window_get_filename (win);
-   execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
- }
+   struct lex_reader *reader;
 -  gchar *text;
 -
 -  text = gtk_text_buffer_get_text (sw->buffer, &start, &stop, FALSE);
++  gchar *text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (sw->buffer), &start, &stop, FALSE);
+   reader = lex_reader_for_string (text);
+   g_free (text);
  
+   lex_reader_set_file_name (reader, psppire_window_get_filename (win));
  
+   execute_syntax (psppire_default_data_window (), reader);
+ }
  \f
  /* Delete the currently selected text */
  static void
  on_edit_delete (PsppireSyntaxWindow *sw)
  {
    GtkTextIter begin, end;
 +  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
    
 -  if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
 -    gtk_text_buffer_delete (sw->buffer, &begin, &end);
 +  if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
 +    gtk_text_buffer_delete (buffer, &begin, &end);
  }
  
  
 -
 -
  /* The syntax editor's clipboard deals only with text */
  enum {
    SELECT_FMT_NULL,
  static void
  selection_changed (PsppireSyntaxWindow *sw)
  {
 -  gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
 +  gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
  
    gtk_action_set_sensitive (sw->edit_copy, sel);
    gtk_action_set_sensitive (sw->edit_cut, sel);
@@@ -268,13 -311,12 +336,13 @@@ static gboolea
  set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
  {
    GtkClipboard *clipboard ;
 +  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
  
 -  if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
 +  if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
      return FALSE;
  
    g_free (sw->cliptext);
 -  sw->cliptext = gtk_text_buffer_get_text  (sw->buffer, begin, end, FALSE);
 +  sw->cliptext = gtk_text_buffer_get_text  (buffer, begin, end, FALSE);
  
    clipboard =
      gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
@@@ -294,7 -336,7 +362,7 @@@ on_edit_cut (PsppireSyntaxWindow *sw
    GtkTextIter begin, end;
    
    if ( set_clip (sw, &begin, &end))
 -    gtk_text_buffer_delete (sw->buffer, &begin, &end);
 +    gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
  }
  
  static void
@@@ -323,7 -365,7 +391,7 @@@ contents_received_callback (GtkClipboar
  
    c = (gchar *) sd->data;
  
 -  gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
 +  gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
                                    (gchar *) sd->data,
                                    sd->length);
  
@@@ -376,8 -418,8 +444,8 @@@ on_run_all (GtkMenuItem *menuitem, gpoi
    GtkTextIter begin, end;
    PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
  
 -  gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
 -  gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
 +  gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
 +  gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
  
    editor_execute_syntax (se, begin, end);
  }
@@@ -389,7 -431,7 +457,7 @@@ on_run_selection (GtkMenuItem *menuitem
    GtkTextIter begin, end;
    PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
  
 -  if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
 +  if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
      editor_execute_syntax (se, begin, end);
  }
  
@@@ -406,17 -448,17 +474,17 @@@ on_run_to_end (GtkMenuItem *menuitem, g
    PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
  
    /* Get the current line */
 -  gtk_text_buffer_get_iter_at_mark (se->buffer,
 +  gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
                                    &here,
 -                                  gtk_text_buffer_get_insert (se->buffer)
 +                                  gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
                                    );
  
    line = gtk_text_iter_get_line (&here) ;
  
    /* Now set begin and end to the start of this line, and end of buffer
       respectively */
 -  gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
 -  gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
 +  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
 +  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
  
    editor_execute_syntax (se, begin, end);
  }
@@@ -434,17 -476,17 +502,17 @@@ on_run_current_line (GtkMenuItem *menui
    PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
  
    /* Get the current line */
 -  gtk_text_buffer_get_iter_at_mark (se->buffer,
 +  gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
                                    &here,
 -                                  gtk_text_buffer_get_insert (se->buffer)
 +                                  gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
                                    );
  
    line = gtk_text_iter_get_line (&here) ;
  
    /* Now set begin and end to the start of this line, and start of
       following line respectively */
 -  gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
 -  gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
 +  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
 +  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
  
    editor_execute_syntax (se, begin, end);
  }
@@@ -476,7 -518,8 +544,8 @@@ save_editor_to_file (PsppireSyntaxWindo
                     const gchar *filename,
                     GError **err)
  {
 -  GtkTextBuffer *buffer = se->buffer;
 +  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
+   struct substring text_locale;
    gboolean result ;
    GtkTextIter start, stop;
    gchar *text;
  
    text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
  
-   result =  g_file_set_contents (suffixedname, text, -1, err);
+   text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
+                                        NULL);
  
+   result =  g_file_set_contents (suffixedname, ss_data (text_locale),
+                                  ss_length (text_locale), err);
+   ss_dealloc (&text_locale);
    g_free (suffixedname);
  
    if ( result )
  }
  
  
- /* Callback for the File->SaveAs menuitem */
+ /* PsppireWindow 'pick_Filename' callback. */
  static void
- syntax_save_as (PsppireWindow *se)
+ syntax_pick_filename (PsppireWindow *window)
  {
+   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
+   const char *default_encoding;
    GtkFileFilter *filter;
    gint response;
  
  
    gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
                                                  TRUE);
+   default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
+   gtk_file_chooser_set_extra_widget (
+     GTK_FILE_CHOOSER (dialog),
+     psppire_encoding_selector_new (default_encoding, false));
    response = gtk_dialog_run (GTK_DIALOG (dialog));
  
    if ( response == GTK_RESPONSE_ACCEPT )
      {
-       GError *err = NULL;
-       char *filename =
-       gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
-       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
-       {
-         msg ( ME, "%s", err->message );
-         g_error_free (err);
-       }
+       gchar *encoding;
+       char *filename;
  
+       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
+       psppire_window_set_filename (window, filename);
        free (filename);
+       encoding = psppire_encoding_selector_get_encoding (
+         gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
+       if (encoding != NULL)
+         {
+           g_free (se->encoding);
+           se->encoding = encoding;
+         }
      }
  
    gtk_widget_destroy (dialog);
  }
  
  
- /* Callback for the File->Save menuitem */
+ /* PsppireWindow 'save' callback. */
  static void
  syntax_save (PsppireWindow *se)
  {
    const gchar *filename = psppire_window_get_filename (se);
-   if ( filename == NULL )
-     syntax_save_as (se);
-   else
+   GError *err = NULL;
+   save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
+   if ( err )
      {
-       GError *err = NULL;
-       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
-       if ( err )
-       {
-         msg (ME, "%s", err->message);
-         g_error_free (err);
-       }
+       msg (ME, "%s", err->message);
+       g_error_free (err);
      }
  }
  
@@@ -589,40 -642,29 +668,40 @@@ on_quit (GtkMenuItem *menuitem, gpointe
  }
  
  
 +static void
 +load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
 +{
 +  gboolean ok;
 +
 +  gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
 +  ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
 +  gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
 +
 +  if (ok )
 +    gtk_widget_show (se);
 +  else
 +    gtk_widget_destroy (se);
 +}
 +
  void
  create_syntax_window (void)
  {
-   GtkWidget *w = psppire_syntax_window_new ();
+   GtkWidget *w = psppire_syntax_window_new (NULL);
    gtk_widget_show (w);
  }
  
  void
- open_new_syntax_window (const char *file_name)
+ open_syntax_window (const char *file_name, const gchar *encoding)
  {
-   GtkWidget *se = psppire_syntax_window_new ();
+   GtkWidget *se = psppire_syntax_window_new (encoding);
  
 -  if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
 -    gtk_widget_show (se);
 -  else
 -    gtk_widget_destroy (se);
 +  if ( file_name)
 +    load_and_show_syntax_window (se, file_name);
  }
  
 -static void
 -on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
 -{
 -  gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
 -}
 +
 +
 +static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
  
  static void
  on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
      psppire_window_set_unsaved (window);
  }
  
- extern struct source_stream *the_source_stream ;
 +static void undo_redo_update (PsppireSyntaxWindow *window);
 +static void undo_last_edit (PsppireSyntaxWindow *window);
 +static void redo_last_edit (PsppireSyntaxWindow *window);
 +
 +static void
 +on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
 +{
 +  gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
 +  undo_redo_update (window);
 +}
 +
  static void
  psppire_syntax_window_init (PsppireSyntaxWindow *window)
  {
    GtkWidget *menubar = get_widget_assert (xml, "menubar");
    GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
  
 -
    GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
  
 +  PsppireSyntaxWindowClass *class
 +    = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
 +
    GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
    GtkClipboard *clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
  
 +  window->print_settings = NULL;
 +  window->undo_menuitem = get_action_assert (xml, "edit_undo");
 +  window->redo_menuitem = get_action_assert (xml, "edit_redo");
 +
 +  if (class->lan)
 +    window->buffer = gtk_source_buffer_new_with_language (class->lan);
 +  else
 +    window->buffer = gtk_source_buffer_new (NULL);
 +
 +  gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
 +
 +  g_object_set (window->buffer,
 +              "highlight-matching-brackets", TRUE,
 +              NULL);
 +
 +  g_object_set (text_view,
 +              "show-line-numbers", TRUE,
 +              "show-line-marks", TRUE,
 +              "auto-indent", TRUE,
 +              "indent-width", 4,
 +              "highlight-current-line", TRUE,
 +              NULL);
 +
+   window->encoding = NULL;
    window->cliptext = NULL;
    window->dispose_has_run = FALSE;
  
    window->edit_cut = get_action_assert (xml, "edit_cut");
    window->edit_paste = get_action_assert (xml, "edit_paste");
  
-   window->lexer = lex_create (the_source_stream);
 -  window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
++  window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
  
    window->sb = get_widget_assert (xml, "statusbar2");
    window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
    g_signal_connect (window->buffer, "modified-changed", 
                    G_CALLBACK (on_modified_changed), window);
  
 +  g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
 +                            G_CALLBACK (psppire_syntax_window_print), window);
 +
 +
 +  g_signal_connect_swapped (window->undo_menuitem,
 +                          "activate",
 +                            G_CALLBACK (undo_last_edit),
 +                          window);
 +
 +  g_signal_connect_swapped (window->redo_menuitem,
 +                          "activate",
 +                            G_CALLBACK (redo_last_edit),
 +                          window);
 +
 +  undo_redo_update (window);
 +
    window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change", 
                                                   G_CALLBACK (selection_changed), window);
  
  
    g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
  
- #if 0
    g_signal_connect (get_action_assert (xml,"file_new_data"),
                    "activate",
                    G_CALLBACK (create_data_window),
                    window);
- #endif
+   g_signal_connect_swapped (get_action_assert (xml, "file_open"),
+                   "activate",
+                   G_CALLBACK (psppire_window_open),
+                   window);
  
    g_signal_connect_swapped (get_action_assert (xml, "file_save"),
                    "activate",
-                   G_CALLBACK (syntax_save),
+                   G_CALLBACK (psppire_window_save),
                    window);
  
    g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
                    "activate",
-                   G_CALLBACK (syntax_save_as),
+                   G_CALLBACK (psppire_window_save_as),
                    window);
  
    g_signal_connect (get_action_assert (xml,"file_quit"),
                    G_CALLBACK (psppire_window_minimise_all), NULL);
  
  
 +
 +
 +
    {
    GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
  
  
  
  GtkWidget*
- psppire_syntax_window_new (void)
+ psppire_syntax_window_new (const char *encoding)
  {
    return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
-                                  /* TRANSLATORS: This will form a filename.  Please avoid whitespace. */
-                                  "filename", _("Syntax"),
                                   "description", _("Syntax Editor"),
+                                    "encoding", encoding,
                                   NULL));
  }
  
@@@ -878,7 -866,9 +958,10 @@@ syntax_load (PsppireWindow *window, con
    gsize len_utf8 = -1;
    GtkTextIter iter;
    PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
 +  GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
+   gchar *encoding;
+   char *mime_type;
    /* FIXME: What if it's a very big file ? */
    if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
      {
        return FALSE;
      }
  
-   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
+   /* Determine the file's encoding and update sw->encoding.  (The ordering is
+      important here because encoding_guess_whole_file() often returns its
+      argument instead of a copy of it.) */
+   encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
+                                                   len_locale));
+   g_free (sw->encoding);
+   sw->encoding = encoding;
+   text_utf8 = recode_substring_pool ("UTF-8", encoding,
+                                      ss_buffer (text_locale, len_locale),
+                                      NULL).string;
    free (text_locale);
  
 -  gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
 +  if ( text_utf8 == NULL )
 +    {
 +      error_dialog (GTK_WINDOW (window), filename, err);
 +      g_clear_error (&err);
 +      return FALSE;
 +    }
  
 -  gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
 +  gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
  
 -  gtk_text_buffer_set_modified (sw->buffer, FALSE);
 +  gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
 +
 +  gtk_text_buffer_set_modified (buffer, FALSE);
  
    free (text_utf8);
  
+   mime_type = xasprintf ("text/x-spss-syntax; charset=%s", sw->encoding);
+   add_most_recent (filename, mime_type);
+   free (mime_type);
    return TRUE;
  }
  
@@@ -915,116 -911,7 +1011,117 @@@ static voi
  psppire_syntax_window_iface_init (PsppireWindowIface *iface)
  {
    iface->save = syntax_save;
+   iface->pick_filename = syntax_pick_filename;
    iface->load = syntax_load;
  }
  
 +
 +\f
 +
 +static void
 +undo_redo_update (PsppireSyntaxWindow *window)
 +{
 +  gtk_action_set_sensitive (window->undo_menuitem,
 +                          gtk_source_buffer_can_undo (window->buffer));
 +
 +  gtk_action_set_sensitive (window->redo_menuitem,
 +                          gtk_source_buffer_can_redo (window->buffer));
 +}
 +
 +static void
 +undo_last_edit (PsppireSyntaxWindow *window)
 +{
 +  gtk_source_buffer_undo (window->buffer);
 +  undo_redo_update (window);
 +}
 +
 +static void
 +redo_last_edit (PsppireSyntaxWindow *window)
 +{
 +  gtk_source_buffer_redo (window->buffer);
 +  undo_redo_update (window);
 +}
 +
 +
 +\f
 +/* Printing related stuff */
 +
 +
 +static void
 +begin_print (GtkPrintOperation *operation,
 +          GtkPrintContext   *context,
 +          PsppireSyntaxWindow *window)
 +{
 +  window->compositor =
 +    gtk_source_print_compositor_new (window->buffer);
 +}
 +
 +
 +static void
 +end_print (GtkPrintOperation *operation,
 +          GtkPrintContext   *context,
 +          PsppireSyntaxWindow *window)
 +{
 +  g_object_unref (window->compositor);
 +  window->compositor = NULL;
 +}
 +
 +
 +
 +static gboolean
 +paginate (GtkPrintOperation *operation,
 +          GtkPrintContext   *context,
 +          PsppireSyntaxWindow *window)
 +{
 +  if (gtk_source_print_compositor_paginate (window->compositor, context))
 +    {
 +      gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
 +      gtk_print_operation_set_n_pages (operation, n_pages);
 +        
 +      return TRUE;
 +    }
 +
 +  return FALSE;
 +}
 +
 +static void
 +draw_page (GtkPrintOperation *operation,
 +           GtkPrintContext   *context,
 +           gint               page_nr,
 +          PsppireSyntaxWindow *window)
 +{
 +  gtk_source_print_compositor_draw_page (window->compositor, 
 +                                       context,
 +                                       page_nr);
 +}
 +
 +
 +
 +static void
 +psppire_syntax_window_print (PsppireSyntaxWindow *window)
 +{
 +  GtkPrintOperationResult res;
 +
 +  GtkPrintOperation *print = gtk_print_operation_new ();
 +
 +  if (window->print_settings != NULL) 
 +    gtk_print_operation_set_print_settings (print, window->print_settings);
 +
 +
 +  g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
 +  g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
 +  g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
 +  g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
 +
 +  res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 +                                 GTK_WINDOW (window), NULL);
 +
 +  if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
 +    {
 +      if (window->print_settings != NULL)
 +        g_object_unref (window->print_settings);
 +      window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
 +    }
 +
 +  g_object_unref (print);
 +}
index df16ed3ba62fd650552c8897c01f1b3d48f925f5,31ceebb224185b9c4224bafcad19bd3414af2c7f..c4a6d2c07a5d56c8816460107efc668ce65a6bb4
@@@ -1,5 -1,5 +1,5 @@@
  /* PSPPIRE - a graphical user interface for PSPP.
-    Copyright (C) 2008, 2010  Free Software Foundation
+    Copyright (C) 2008, 2010, 2011  Free Software Foundation
  
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
  
  #include <glib.h>
  #include <glib-object.h>
- #include <gtk/gtkaction.h>
- #include <gtk/gtktextbuffer.h>
- #include "psppire-window.h"
  #include <gtk/gtk.h>
+ #include "psppire-window.h"
  
 +#include <gtksourceview/gtksourcelanguage.h>
 +#include <gtksourceview/gtksourcelanguagemanager.h>
 +#include <gtksourceview/gtksourcebuffer.h>
 +#include <gtksourceview/gtksourceprintcompositor.h>
 +
  G_BEGIN_DECLS
  
  #define PSPPIRE_SYNTAX_WINDOW_TYPE            (psppire_syntax_window_get_type ())
  #define PSPPIRE_SYNTAX_WINDOW(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), PSPPIRE_SYNTAX_WINDOW_TYPE, PsppireSyntaxWindow))
  #define PSPPIRE_SYNTAX_WINDOW_CLASS(class)    (G_TYPE_CHECK_CLASS_CAST ((class), \
 -    PSPPIRE_SYNTAX_WINDOW_TYPE, PsppireSyntax_WindowClass))
 +    PSPPIRE_SYNTAX_WINDOW_TYPE, PsppireSyntaxWindowClass))
  #define PSPPIRE_IS_SYNTAX_WINDOW(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
      PSPPIRE_SYNTAX_WINDOW_TYPE))
  #define PSPPIRE_IS_SYNTAX_WINDOW_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), \
@@@ -52,16 -45,12 +50,17 @@@ struct _PsppireSyntaxWindo
  
    /* <private> */
  
 -
 -  GtkTextBuffer *buffer;  /* The buffer which contains the text */
 +  GtkSourceBuffer *buffer;  /* The buffer which contains the text */
 +  struct lexer *lexer;    /* Lexer to parse syntax */
+   gchar *encoding;              /* File's encoding. */
    GtkWidget *sb;
    guint text_context;
  
 +  GtkPrintSettings *print_settings;
 +  GtkSourcePrintCompositor *compositor;
 +  GtkAction *undo_menuitem;
 +  GtkAction *redo_menuitem;
 +
    gchar *cliptext;
  
    GtkAction *edit_cut;
@@@ -79,16 -68,13 +78,15 @@@ struct _PsppireSyntaxWindowClas
  {
    PsppireWindowClass parent_class;
  
 +
 +  GtkSourceLanguage *lan ;
  };
  
  GType      psppire_syntax_window_get_type        (void);
- GtkWidget* psppire_syntax_window_new             (void);
+ GtkWidget* psppire_syntax_window_new             (const char *encoding);
  
  void create_syntax_window (void);
- void open_new_syntax_window (const char *file_name);
+ void open_syntax_window (const char *file_name, const char *encoding);
  
  G_END_DECLS
  
index 4f134c2e1cb51b966674a5c0dc764941b7a5b93e,91fdec4946b7049b398b4a35bfe4e8a49c0591c0..51e589ec730c048cbd2a72799fc8c4800dff0f1c
            </object>
          </child>
          <child>
-           <object class="GtkAction" id="open2">
+           <object class="GtkAction" id="file_open">
              <property name="stock-id">gtk-open</property>
-             <property name="name">open2</property>
-           </object>
-         </child>
-         <child>
-           <object class="GtkAction" id="file_open_syntax">
-             <property name="name">file_open_syntax</property>
-             <property name="label" translatable="yes">_Syntax</property>
-           </object>
-         </child>
-         <child>
-           <object class="GtkAction" id="file_open_data">
-             <property name="name">file_open_data</property>
-             <property name="label" translatable="yes">_Data</property>
+             <property name="name">file_open</property>
+             <property name="label" translatable="yes">_Open...</property>
            </object>
          </child>
          <child>
            <object class="GtkAction" id="file_save_as">
              <property name="stock-id">gtk-save-as</property>
              <property name="name">file_save_as</property>
+             <property name="label" translatable="yes">Save _As...</property>
            </object>
          </child>
 +        <child>
 +          <object class="GtkAction" id="file_print">
 +            <property name="name">file_print</property>
 +            <property name="stock-id">gtk-print</property>
 +          </object>
 +        </child>
          <child>
            <object class="GtkAction" id="file_quit">
              <property name="stock-id">gtk-quit</property>
              <property name="sensitive">false</property>
            </object>
          </child>
 +        <child>
 +          <object class="GtkAction" id="edit_undo">
 +            <property name="stock-id">gtk-undo</property>
 +            <property name="name">edit_undo</property>
 +          </object>
 +        </child>
 +        <child>
 +          <object class="GtkAction" id="edit_redo">
 +            <property name="stock-id">gtk-redo</property>
 +            <property name="name">edit_redo</property>
 +          </object>
 +        </child>
          <child>
            <object class="GtkAction" id="run1">
              <property name="name">run1</property>
              <menuitem action="file_new_syntax"/>
              <menuitem action="file_new_data"/>
            </menu>
-           <menu action="open2">
-             <menuitem action="file_open_syntax"/>
-             <menuitem action="file_open_data"/>
-           </menu>
+           <menuitem action="file_open"/>
            <menuitem action="file_save"/>
            <menuitem action="file_save_as"/>
            <separator/>
 +          <menuitem action="file_print"/>
 +          <separator/>
            <menuitem action="file_quit"/>
          </menu>
          <menu action="menuitem7">
            <menuitem action="edit_copy"/>
            <menuitem action="edit_paste"/>
            <menuitem action="edit_delete"/>
 +          <separator/>
 +          <menuitem action="edit_undo"/>
 +          <menuitem action="edit_redo"/>
          </menu>
          <menu action="run1">
            <menuitem action="run_all"/>
      <property name="shadow_type">GTK_SHADOW_IN</property>
      <property name="window_placement">GTK_CORNER_TOP_LEFT</property>
      <child>
 -      <object class="GtkTextView" id="syntax_text_view">
 +      <object class="GtkSourceView" id="syntax_text_view">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="editable">True</property>