[Xfce4-commits] <mousepad:master> Move in all the changes from the old git branch.

Nick Schermer noreply at xfce.org
Sat May 5 21:31:20 CEST 2012


Updating branch refs/heads/master
         to 375a246bf9f38733f368bfa05163734119b54168 (commit)
       from d7fce4e8e9890bee3cde1d8afa549b320ca0bb4c (commit)

commit 375a246bf9f38733f368bfa05163734119b54168
Author: Nick Schermer <nick at xfce.org>
Date:   Wed Aug 12 18:01:02 2009 +0200

    Move in all the changes from the old git branch.
    
    Because individual patches did not apply, this is one big
    commit the moving everything at once.

 ChangeLog                                   |    9 +
 Makefile.am                                 |    2 -
 MousepadHelp.in                             |    2 -
 TODO                                        |    1 +
 autogen.sh                                  |    3 -
 configure.in.in                             |    4 +-
 docs/Makefile.am                            |    4 +-
 docs/manual/C/Makefile.am                   |    2 -
 docs/manual/C/Mousepad.xml.in               |  199 +++++--
 docs/manual/C/images/Makefile.am            |   12 +-
 docs/manual/C/images/find-and-replace.png   |  Bin 0 -> 20777 bytes
 docs/manual/C/images/go-to.png              |  Bin 0 -> 9507 bytes
 docs/manual/C/images/main-window.png        |  Bin 0 -> 13955 bytes
 docs/manual/C/images/other-tab-size.png     |  Bin 0 -> 8168 bytes
 docs/manual/C/images/paste-from-history.png |  Bin 0 -> 9495 bytes
 docs/manual/C/images/tab-size-menu.png      |  Bin 0 -> 9130 bytes
 docs/manual/C/images/typeahead-search.png   |  Bin 0 -> 6724 bytes
 docs/manual/Makefile.am                     |    2 -
 icons/16x16/Makefile.am                     |    2 -
 icons/24x24/Makefile.am                     |    2 -
 icons/Makefile.am                           |    2 -
 icons/scalable/Makefile.am                  |    2 -
 mousepad/Makefile.am                        |  182 +++---
 mousepad/main.c                             |    5 +-
 mousepad/mousepad-application.c             |   27 +-
 mousepad/mousepad-application.h             |    1 -
 mousepad/mousepad-dbus-infos.xml            |    2 -
 mousepad/mousepad-dbus.c                    |   38 +-
 mousepad/mousepad-dbus.h                    |    1 -
 mousepad/mousepad-dialogs.c                 |    9 +-
 mousepad/mousepad-dialogs.h                 |    3 +-
 mousepad/mousepad-document.c                |   51 +-
 mousepad/mousepad-document.h                |    1 -
 mousepad/mousepad-encoding-dialog.c         |  340 +++++------
 mousepad/mousepad-encoding-dialog.h         |   13 +-
 mousepad/mousepad-encoding.c                |  201 ++++++
 mousepad/mousepad-encoding.h                |  125 ++++
 mousepad/mousepad-file.c                    |  721 +++++++++++++---------
 mousepad/mousepad-file.h                    |   28 +-
 mousepad/mousepad-preferences.c             |    7 +-
 mousepad/mousepad-preferences.h             |    1 -
 mousepad/mousepad-print.c                   |   17 +-
 mousepad/mousepad-print.h                   |    1 -
 mousepad/mousepad-private.h                 |   35 +-
 mousepad/mousepad-replace-dialog.c          |    9 +-
 mousepad/mousepad-replace-dialog.h          |    1 -
 mousepad/mousepad-search-bar.c              |   25 +-
 mousepad/mousepad-search-bar.h              |    1 -
 mousepad/mousepad-statusbar.c               |    9 +-
 mousepad/mousepad-statusbar.h               |    1 -
 mousepad/mousepad-undo.c                    |   53 +-
 mousepad/mousepad-undo.h                    |    1 -
 mousepad/mousepad-util.c                    |   77 ++--
 mousepad/mousepad-util.h                    |    1 -
 mousepad/mousepad-view.c                    |  103 ++--
 mousepad/mousepad-view.h                    |    1 -
 mousepad/mousepad-window-ui.xml             |    4 +-
 mousepad/mousepad-window.c                  |  889 ++++++++++++++-------------
 mousepad/mousepad-window.h                  |    7 +-
 59 files changed, 1888 insertions(+), 1351 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b864971..244a1da 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-03-xx	Nick Schermer <nick at xfce.org
+
+	* mousepad/mousepad-file.c: Use GMappedFile (mmap) for opening a
+	  document. Also improve the line ending detection and avoid
+	  a lot of gtk_text_buffer_insert() calls when opening unix or
+	  mac line-ended files.
+	* Some internal renaming.
+
+
 2008-03-10	Nick Schermer <nick at xfce.org>
 
 	* mousepad/mousepad-document.c: Swap the colors of readonly and
diff --git a/Makefile.am b/Makefile.am
index aafee93..e9d4098 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 SUBDIRS =									\
 	icons									\
 	mousepad                                                        	\
diff --git a/MousepadHelp.in b/MousepadHelp.in
index 7da5051..bde651f 100644
--- a/MousepadHelp.in
+++ b/MousepadHelp.in
@@ -1,7 +1,5 @@
 #!/bin/sh
 #
-# $Id$
-#
 # Copyright (c) 2004-2006 Benedikt Meurer <benny at xfce.org>
 # Copyright (c) 2007      Nick Schermer <nick at xfce.org>
 #
diff --git a/TODO b/TODO
index 8c8887f..6976a23 100644
--- a/TODO
+++ b/TODO
@@ -40,3 +40,4 @@ Testing and polishing
 Feature
 =======
 - You'll never know. No promisses here...
+- GIO.
diff --git a/autogen.sh b/autogen.sh
index e8fcaae..852b2db 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -1,7 +1,4 @@
 #!/bin/sh
-#
-# $Id$
-#
 
 (type xdt-autogen) >/dev/null 2>&1 || {
   cat >&2 <<EOF
diff --git a/configure.in.in b/configure.in.in
index aba1fae..62c78ce 100644
--- a/configure.in.in
+++ b/configure.in.in
@@ -1,5 +1,3 @@
-dnl $Id$
-
 dnl ***************************
 dnl *** Version information ***
 dnl ***************************
@@ -24,7 +22,7 @@ AC_COPYRIGHT([Copyright (c) 2007-2008
 AC_INIT([Mousepad], [mousepad_version], [http://bugzilla.xfce.org/], [mousepad])
 AC_PREREQ([2.50])
 AC_CANONICAL_TARGET()
-AC_REVISION([$Id$])
+AC_REVISION([])
 
 dnl ***************************
 dnl *** Initialize automake ***
diff --git a/docs/Makefile.am b/docs/Makefile.am
index a936fe8..7511289 100644
--- a/docs/Makefile.am
+++ b/docs/Makefile.am
@@ -1,6 +1,4 @@
-# $Id$
-
-SUBDIRS =								\
+SUBDIRS = \
 	manual
 
 # vi:set ts=8 sw=8 noet ai nocindent syntax=automake:
diff --git a/docs/manual/C/Makefile.am b/docs/manual/C/Makefile.am
index c555c85..06db614 100644
--- a/docs/manual/C/Makefile.am
+++ b/docs/manual/C/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 SUBDIRS =								\
 	images
 
diff --git a/docs/manual/C/Mousepad.xml.in b/docs/manual/C/Mousepad.xml.in
index e31cef3..fcd6ce3 100644
--- a/docs/manual/C/Mousepad.xml.in
+++ b/docs/manual/C/Mousepad.xml.in
@@ -51,17 +51,18 @@
       <title>Introduction</title>
 
       <para>
-        &application; is the default text editor for the Xfce Desktop Environment. It has been design to be a
-        lightweight editor you would use for basic file editing. Therefore &application; starts quickly, but
-        also has features like editing multiple documents, vertical selection, type-ahead search, full tab
-        drag and drop and much more.
+        &application; is the default text editor for the Xfce Desktop Environment. It has
+        been design to be a lightweight editor you would use for basic file editing.
+        Therefore &application; starts quickly, but also has features like editing
+        multiple documents, vertical selection, type-ahead search, full tab drag and drop
+        and much more.
       </para>
 
       <para>
-        &application; started as a fork of Leafpad to provide printing support using Xfprint, but as of version
-        0.3 is has been completely rewritten to add support for tabs and DBus. Although the rewrite added
-        a lot of new features, it is still as fast as the Leafpad based fork and therefore fits perfectly
-        in the Xfce philosophy.
+        &application; started as a fork of Leafpad to provide printing support using
+        Xfprint, but as of version 0.3 is has been completely rewritten to add support for
+        tabs and DBus. Although the rewrite added a lot of new features, it is still as
+        fast as the Leafpad based fork and therefore fits perfectly in the Xfce philosophy.
       </para>
     </sect2>
 
@@ -69,8 +70,8 @@
       <title>Terminology</title>
 
       <para>
-        There might be a couple of words in this manual that could be the source of misunderstanding. The table
-        below will clarify those words.
+        There might be a couple of words in this manual that could be the source of
+        misunderstanding. The table below will clarify those words.
       </para>
 
       <table frame="all">
@@ -101,84 +102,182 @@
     </sect2>
   </sect1>
 
-  <sect1 id="mousepad">
-    <title>Working With &application;</title>
-
-    <sect2 id="window">
-      <title>Main Window</title>
-
-      <sect3 id="menu-bar">
-        <title>Menu Bar</title>
-      </sect3>
+  <sect1 id="working-with-text">
+    <title>Working With Text</title>
 
-      <sect3 id="statusbar">
-        <title>Statsbar</title>
-      </sect3>
+    <sect2 id="keyboard-shortcuts">
+      <title>Keyboard Shortcuts</title>
     </sect2>
 
-    <sect2 id="navigation">
-      <title>Navigation</title>
+    <sect2 id="undo-and-redo">
+      <title>Undo and Redo</title>
 
-      <sect3 id="go-to-line">
-        <title>Go To Line</title>
-      </sect3>
-    </sect2>
+      <para>
 
-    <sect2 id="ordering-documents">
-      <title>Ordering Documents</title>
+      </para>
     </sect2>
 
-    <sect2 id="documents">
-      <title>Documents</title>
+    <sect2 id="copy-and-paste">
+      <title>Copy and Paste</title>
 
-      <sect3 id="new-documents">
-        <title>Creating New Documents</title>
-      </sect3>
+      <para>
+        &application; supports both the selection and primary clipboard. To paste
+        something from the seletion clipboard you can click with the middle mouse
+        button in the &application; window.
+      </para>
 
-      <sect3 id="opening-documents">
-        <title>Opening Documents</title>
-      </sect3>
+      <para>
+        The primary clipboard can be accessed by the <guimenuitem>Cut</guimenuitem>,
+        <guimenuitem>Copy</guimenuitem> and <guimenuitem>Paste</guimenuitem> menu
+        items in the <guimenu>Edit</guimenu> menu. The keyboard shortcuts mentioned
+        above also work.
+      </para>
 
-      <sect3 id="recent-documents">
-        <title>Recent Documents</title>
+      <sect3 id="paste-from-history">
+        <title>Paste from History</title>
+
+        <screenshot>
+          <mediaobject>
+            <imageobject>
+              <imagedata fileref="images/paste-from-history.png" format="PNG"/>
+            </imageobject>
+
+            <textobject>
+              <phrase>Paste from History</phrase>
+            </textobject>
+
+            <caption>
+              <para>Screenshot of the history menu</para>
+            </caption>
+          </mediaobject>
+        </screenshot>
+
+        <para>
+          Each time you copy text in &application;, the text is pushed onto a stack.
+          When you choose <menuchoice><guimenu>Edit</guimenu><guimenuitem>Paste Special
+          </guimenuitem><guimenuitem>Paste from History</guimenuitem></menuchoice> a
+          menu will appear below the carret with the last 10 clipboard items.
+        </para>
+
+        <para>
+          You can choose an item from the list by clicking it, using the arrow keys
+          and press enter or by pressing the numeric keyboard accelerator.
+        </para>
       </sect3>
-    </sect2>
-  </sect1>
 
-  <sect1 id="text">
-    <title>Working With Text</title>
+      <sect3 id="paste-as-column">
+        <title>Paste as Column</title>
 
-    <sect2 id="keyboard-shortcuts">
-      <title>Keyboard Shortcuts</title>
+        <para>
+          When the content in the primary clipboard contains multiple lines, you can paste
+          the content in the same column. &application; will then try  to paste each line
+          in the clipboard at the same x-coordinate. You can access this option by
+          choosing <menuchoice><guimenu>Edit</guimenu><guimenuitem>Paste Special
+          </guimenuitem><guimenuitem>Paste as Column</guimenuitem></menuchoice>.
+        </para>
+      </sect3>
     </sect2>
 
     <sect2 id="auto-indent">
       <title>Auto Indent</title>
+
     </sect2>
 
     <sect2 id="selections">
       <title>Selecting Text</title>
 
       <sect3 id="multi-and-column-selections">
-        <title>Multi- And Column-Selections</title>
+        <title>Column-Selections</title>
       </sect3>
 
       <sect3 id="indentation">
         <title>Indentation</title>
       </sect3>
-    </sect2>
 
-    <sect2 id="clipboard">
-      <title>Copy, Cut and Paste From The Clipboard</title>
+      <sect3 id="moving-selections">
+        <title>Moving Selections</title>
+      </sect3>
     </sect2>
 
     <sect2 id="search-and-replace">
       <title>Search And Replace</title>
+
+      <sect3 id="typeahead-search">
+        <title>Typeahead Search</title>
+
+        <screenshot>
+          <mediaobject>
+            <imageobject>
+              <imagedata fileref="images/typeahead-search.png" format="PNG"/>
+            </imageobject>
+
+            <textobject>
+              <phrase>Typeahead search bar</phrase>
+            </textobject>
+
+            <caption>
+              <para>Screenshot of the typeahead search bar</para>
+            </caption>
+          </mediaobject>
+        </screenshot>
+      </sect3>
+
+      <sect3 id="replace-dialog">
+        <title>Search and Replace Dialog</title>
+
+        <screenshot>
+          <mediaobject>
+            <imageobject>
+              <imagedata fileref="images/find-and-replace.png" format="PNG"/>
+            </imageobject>
+
+            <textobject>
+              <phrase>Find and Replace Dialog</phrase>
+            </textobject>
+
+            <caption>
+              <para>Screenshot of the find and replace dialog</para>
+            </caption>
+          </mediaobject>
+        </screenshot>
+      </sect3>
     </sect2>
 
     <sect2 id="tabs">
       <title>Tabs</title>
 
+        <screenshot>
+          <mediaobject>
+            <imageobject>
+              <imagedata fileref="images/tab-size-menu.png" format="PNG"/>
+            </imageobject>
+
+            <textobject>
+              <phrase>Tab Size menu</phrase>
+            </textobject>
+
+            <caption>
+              <para>Screenshot of the tabs size menu</para>
+            </caption>
+          </mediaobject>
+        </screenshot>
+
+        <screenshot>
+          <mediaobject>
+            <imageobject>
+              <imagedata fileref="images/other-tab-size.png" format="PNG"/>
+            </imageobject>
+
+            <textobject>
+              <phrase>Other tab size</phrase>
+            </textobject>
+
+            <caption>
+              <para>Screenshot of the other tab size dialog</para>
+            </caption>
+          </mediaobject>
+        </screenshot>
+
       <sect3 id="insert-spaces">
         <title>Insert Spaces</title>
       </sect3>
diff --git a/docs/manual/C/images/Makefile.am b/docs/manual/C/images/Makefile.am
index 0a63e5f..c329f2b 100644
--- a/docs/manual/C/images/Makefile.am
+++ b/docs/manual/C/images/Makefile.am
@@ -1,7 +1,13 @@
-# $Id$
-
 imagesdir = $(datadir)/doc/Mousepad/html/C/images
-images_DATA =
+images_DATA =								\
+	find-and-replace.png						\
+	go-to.png							\
+	main-window.png							\
+	Makefile.am							\
+	other-tab-size.png						\
+	paste-from-history.png						\
+	tab-size-menu.png						\
+	typeahead-search.png
 
 EXTRA_DIST = 								\
 	$(images_DATA)
diff --git a/docs/manual/C/images/find-and-replace.png b/docs/manual/C/images/find-and-replace.png
new file mode 100644
index 0000000..9e8648a
Binary files /dev/null and b/docs/manual/C/images/find-and-replace.png differ
diff --git a/docs/manual/C/images/go-to.png b/docs/manual/C/images/go-to.png
new file mode 100644
index 0000000..4fe1d3c
Binary files /dev/null and b/docs/manual/C/images/go-to.png differ
diff --git a/docs/manual/C/images/main-window.png b/docs/manual/C/images/main-window.png
new file mode 100644
index 0000000..2ac0a27
Binary files /dev/null and b/docs/manual/C/images/main-window.png differ
diff --git a/docs/manual/C/images/other-tab-size.png b/docs/manual/C/images/other-tab-size.png
new file mode 100644
index 0000000..651d095
Binary files /dev/null and b/docs/manual/C/images/other-tab-size.png differ
diff --git a/docs/manual/C/images/paste-from-history.png b/docs/manual/C/images/paste-from-history.png
new file mode 100644
index 0000000..dd2d757
Binary files /dev/null and b/docs/manual/C/images/paste-from-history.png differ
diff --git a/docs/manual/C/images/tab-size-menu.png b/docs/manual/C/images/tab-size-menu.png
new file mode 100644
index 0000000..942063c
Binary files /dev/null and b/docs/manual/C/images/tab-size-menu.png differ
diff --git a/docs/manual/C/images/typeahead-search.png b/docs/manual/C/images/typeahead-search.png
new file mode 100644
index 0000000..2fc6664
Binary files /dev/null and b/docs/manual/C/images/typeahead-search.png differ
diff --git a/docs/manual/Makefile.am b/docs/manual/Makefile.am
index d0ae330..817eaa6 100644
--- a/docs/manual/Makefile.am
+++ b/docs/manual/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 SUBDIRS =								\
 	C
 
diff --git a/icons/16x16/Makefile.am b/icons/16x16/Makefile.am
index 6cedaf6..a8e5cb7 100644
--- a/icons/16x16/Makefile.am
+++ b/icons/16x16/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 iconsdir = $(datadir)/icons/hicolor/16x16/apps
 icons_DATA =								\
 	Mousepad.png
diff --git a/icons/24x24/Makefile.am b/icons/24x24/Makefile.am
index 0a73503..c22ae68 100644
--- a/icons/24x24/Makefile.am
+++ b/icons/24x24/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 iconsdir = $(datadir)/icons/hicolor/24x24/apps
 icons_DATA =								\
 	Mousepad.png
diff --git a/icons/Makefile.am b/icons/Makefile.am
index e6cdc99..21932ab 100644
--- a/icons/Makefile.am
+++ b/icons/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 SUBDIRS =								\
 	16x16								\
 	24x24								\
diff --git a/icons/scalable/Makefile.am b/icons/scalable/Makefile.am
index b2a0019..2beabc7 100644
--- a/icons/scalable/Makefile.am
+++ b/icons/scalable/Makefile.am
@@ -1,5 +1,3 @@
-# $Id$
-
 iconsdir = $(datadir)/icons/hicolor/scalable/apps
 icons_DATA =								\
 	Mousepad.svg
diff --git a/mousepad/Makefile.am b/mousepad/Makefile.am
index ebf6361..4c46a67 100644
--- a/mousepad/Makefile.am
+++ b/mousepad/Makefile.am
@@ -1,137 +1,125 @@
-# $Id$
-
-INCLUDES =									\
-	-I$(top_builddir)							\
-	-I$(top_srcdir)								\
-	-DBINDIR=\"$(bindir)\"							\
-	-DDATADIR=\"$(datadir)\"						\
-	-DLIBDIR=\"$(libdir)\"							\
-	-DLIBEXECDIR=\"$(libexecdir)\"						\
-	-DG_LOG_DOMAIN=\"Mousepad\"						\
-	-DLIBEXECDIR=\"$(libexecdir)\"						\
-	-DPACKAGE_LOCALE_DIR=\"$(localedir)\"					\
-	-DG_DISABLE_DEPRECATED							\
-	-DGTK_DISABLE_DEPRECATED						\
-	-DGDK_DISABLE_DEPRECATED						\
+INCLUDES = \
+	-I$(top_builddir) \
+	-I$(top_srcdir) \
+	-DBINDIR=\"$(bindir)\" \
+	-DDATADIR=\"$(datadir)\" \
+	-DLIBDIR=\"$(libdir)\" \
+	-DLIBEXECDIR=\"$(libexecdir)\" \
+	-DG_LOG_DOMAIN=\"Mousepad\" \
+	-DLIBEXECDIR=\"$(libexecdir)\" \
+	-DPACKAGE_LOCALE_DIR=\"$(localedir)\" \
+	-DG_DISABLE_DEPRECATED \
+	-DGTK_DISABLE_DEPRECATED \
+	-DGDK_DISABLE_DEPRECATED \
+	-DG_DISABLE_SINGLE_INCLUDES \
+	-DPANGO_DISABLE_SINGLE_INCLUDES \
+	-DGDK_PIXBUF_DISABLE_SINGLE_INCLUDES \
+	-DGTK_DISABLE_SINGLE_INCLUDES \
 	$(PLATFORM_CPPFLAGS)
 
-bin_PROGRAMS = 									\
+bin_PROGRAMS = \
 	mousepad
 
-mousepad_built_sources =							\
-	mousepad-marshal.c							\
+mousepad_built_sources = \
+	mousepad-marshal.c \
 	mousepad-marshal.h
 
-mousepad_SOURCES =								\
-	$(mousepad_built_sources)						\
-	$(mousepad_dbus_sources)						\
-	main.c									\
-	mousepad-application.c							\
-	mousepad-application.h							\
-	mousepad-dialogs.c							\
-	mousepad-dialogs.h							\
-	mousepad-document.c							\
-	mousepad-document.h							\
-	mousepad-encoding-dialog.c						\
-	mousepad-encoding-dialog.h						\
-	mousepad-file.c								\
-	mousepad-file.h								\
-	mousepad-preferences.c							\
-	mousepad-preferences.h							\
-	mousepad-print.c							\
-	mousepad-print.h							\
-	mousepad-private.h							\
-	mousepad-replace-dialog.c						\
-	mousepad-replace-dialog.h						\
-	mousepad-search-bar.c							\
-	mousepad-search-bar.h							\
-	mousepad-statusbar.c							\
-	mousepad-statusbar.h							\
-	mousepad-view.c								\
-	mousepad-view.h								\
-	mousepad-undo.c								\
-	mousepad-undo.h								\
-	mousepad-util.c								\
-	mousepad-util.h								\
-	mousepad-window.c							\
-	mousepad-window.h							\
+mousepad_SOURCES = \
+	$(mousepad_built_sources) \
+	$(mousepad_dbus_sources) \
+	main.c \
+	mousepad-application.c \
+	mousepad-application.h \
+	mousepad-dialogs.c \
+	mousepad-dialogs.h \
+	mousepad-document.c \
+	mousepad-document.h \
+	mousepad-encoding.c \
+	mousepad-encoding.h \
+	mousepad-encoding-dialog.c \
+	mousepad-encoding-dialog.h \
+	mousepad-file.c \
+	mousepad-file.h \
+	mousepad-preferences.c \
+	mousepad-preferences.h \
+	mousepad-print.c \
+	mousepad-print.h \
+	mousepad-private.h \
+	mousepad-replace-dialog.c \
+	mousepad-replace-dialog.h \
+	mousepad-search-bar.c \
+	mousepad-search-bar.h \
+	mousepad-statusbar.c \
+	mousepad-statusbar.h \
+	mousepad-view.c \
+	mousepad-view.h \
+	mousepad-undo.c \
+	mousepad-undo.h \
+	mousepad-util.c \
+	mousepad-util.h \
+	mousepad-window.c \
+	mousepad-window.h \
 	mousepad-window-ui.h
 
-mousepad_CFLAGS =								\
-	$(GLIB_CFLAGS)								\
-	$(GTK_CFLAGS)								\
-	$(GTHREAD_CFLAGS)							\
+mousepad_CFLAGS = \
+	$(GLIB_CFLAGS) \
+	$(GTK_CFLAGS) \
+	$(GTHREAD_CFLAGS) \
 	$(PLATFORM_CFLAGS)
 
-mousepad_LDFLAGS =								\
-	-no-undefined								\
+mousepad_LDFLAGS = \
+	-no-undefined \
 	$(PLATFORM_LDFLAGS)
 
-mousepad_LDADD =								\
-	$(GLIB_LIBS)								\
-	$(GTK_LIBS)								\
+mousepad_LDADD = \
+	$(GLIB_LIBS) \
+	$(GTK_LIBS)	\
 	$(GTHREAD_LIBS)
 
 if HAVE_DBUS
-mousepad_built_sources +=							\
+mousepad_built_sources +=	\
 	mousepad-dbus-infos.h
 
-mousepad_dbus_sources =								\
-	mousepad-dbus.c								\
+mousepad_dbus_sources = \
+	mousepad-dbus.c \
 	mousepad-dbus.h
 
-mousepad_CFLAGS +=								\
-	-DDBUS_API_SUBJECT_TO_CHANGE						\
+mousepad_CFLAGS += \
+	-DDBUS_API_SUBJECT_TO_CHANGE \
 	$(DBUS_CFLAGS)
 
-mousepad_LDADD +=								\
+mousepad_LDADD +=	\
 	$(DBUS_LIBS)
 endif
 
 if MAINTAINER_MODE
-CLEANFILES =									\
-	xgen-mmc								\
-	xgen-mmh
-
-DISTCLEANFILES =								\
-	$(mousepad_built_sources)                                       	\
-	stamp-mousepad-marshal.h						\
+DISTCLEANFILES = \
+	$(mousepad_built_sources) \
 	mousepad-window-ui.h
 
-BUILT_SOURCES =									\
-	$(mousepad_built_sources)                                       	\
+BUILT_SOURCES = \
+	$(mousepad_built_sources) \
 	mousepad-window-ui.h
 
 if HAVE_DBUS
-mousepad-dbus-infos.h: $(srcdir)/mousepad-dbus-infos.xml Makefile
-	dbus-binding-tool --prefix=mousepad_dbus_service --mode=glib-server $(srcdir)/mousepad-dbus-infos.xml > mousepad-dbus-infos.h
+mousepad-dbus-infos.h: mousepad-dbus-infos.xml Makefile
+	dbus-binding-tool --prefix=mousepad_dbus_service --mode=glib-server $< > $@
 endif
 
-mousepad-window-ui.h: Makefile $(srcdir)/mousepad-window-ui.xml
-	exo-csource --strip-comments --strip-content --static --name=mousepad_window_ui $(srcdir)/mousepad-window-ui.xml > mousepad-window-ui.h
-
-mousepad-marshal.h: stamp-mousepad-marshal.h
-	@true
+mousepad-window-ui.h: mousepad-window-ui.xml Makefile
+	exo-csource --strip-comments --strip-content --static --name=mousepad_window_ui $< > $@
 
-stamp-mousepad-marshal.h: mousepad-marshal.list Makefile
-	( cd $(srcdir) && glib-genmarshal \
-		--prefix=_mousepad_marshal \
-		--header mousepad-marshal.list ) >> xgen-mmh \
-	&& ( cmp -s xgen-mmh mousepad-marshal.h || cp xgen-mmh mousepad-marshal.h ) \
-	&& rm -f xgen-mmh \
-	&& echo timestamp > $(@F)
+mousepad-marshal.h: mousepad-marshal.list Makefile
+	glib-genmarshal --header --prefix=_mousepad_marshal $< > $@
 
 mousepad-marshal.c: mousepad-marshal.list Makefile
-	( cd $(srcdir) && glib-genmarshal \
-		--prefix=_mousepad_marshal \
-		--body mousepad-marshal.list ) >> xgen-mmc \
-	&& cp xgen-mmc mousepad-marshal.c \
-	&& rm -f xgen-mmc
+	echo "#include <mousepad/mousepad-marshal.h>" > $@ \
+	&& glib-genmarshal --body --prefix=_mousepad_marshal $< >> $@
 endif
 
-EXTRA_DIST =									\
-	mousepad-dbus-infos.xml							\
-	mousepad-marshal.list							\
+EXTRA_DIST = \
+	mousepad-dbus-infos.xml \
+	mousepad-marshal.list \
 	mousepad-window-ui.xml
 
 # vi:set ts=8 sw=8 noet ai nocindent syntax=automake:
diff --git a/mousepad/main.c b/mousepad/main.c
index 63d17a6..22e69eb 100644
--- a/mousepad/main.c
+++ b/mousepad/main.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -56,7 +55,7 @@ static const GOptionEntry option_entries[] =
 #endif
   { "version", 'v', 0, G_OPTION_ARG_NONE, &opt_version, N_("Print version information and exit"), NULL },
   { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
-  { NULL },
+  { NULL }
 };
 
 
@@ -90,7 +89,7 @@ main (gint argc, gchar **argv)
     g_thread_init (NULL);
 
   /* initialize gtk+ */
-  if (!gtk_init_with_args (&argc, &argv, _("[FILES...]"), (GOptionEntry *) option_entries, GETTEXT_PACKAGE, &error))
+  if (!gtk_init_with_args (&argc, &argv, _("[FILES...]"), (GOptionEntry *) option_entries, (gchar *) GETTEXT_PACKAGE, &error))
     {
       /* check if we have an error message */
       if (G_LIKELY (error == NULL))
diff --git a/mousepad/mousepad-application.c b/mousepad/mousepad-application.c
index 10e1250..cb7c778 100644
--- a/mousepad/mousepad-application.c
+++ b/mousepad/mousepad-application.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -27,8 +26,6 @@
 
 
 
-static void        mousepad_application_class_init                (MousepadApplicationClass  *klass);
-static void        mousepad_application_init                      (MousepadApplication       *application);
 static void        mousepad_application_finalize                  (GObject                   *object);
 static void        mousepad_application_window_destroyed          (GtkWidget                 *window,
                                                                    MousepadApplication        *application);
@@ -153,7 +150,7 @@ mousepad_application_get (void)
 gboolean
 mousepad_application_has_windows (MousepadApplication *application)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_APPLICATION (application), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_APPLICATION (application), FALSE);
 
   return (application->windows != NULL);
 }
@@ -164,9 +161,9 @@ static void
 mousepad_application_window_destroyed (GtkWidget           *window,
                                        MousepadApplication *application)
 {
-  _mousepad_return_if_fail (GTK_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
-  _mousepad_return_if_fail (g_slist_find (application->windows, window) != NULL);
+  mousepad_return_if_fail (GTK_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
+  mousepad_return_if_fail (g_slist_find (application->windows, window) != NULL);
 
   /* remove the window from the list */
   application->windows = g_slist_remove (application->windows, window);
@@ -182,9 +179,9 @@ void
 mousepad_application_take_window (MousepadApplication *application,
                                   GtkWindow           *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
-  _mousepad_return_if_fail (g_slist_find (application->windows, window) == NULL);
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
+  mousepad_return_if_fail (g_slist_find (application->windows, window) == NULL);
 
   /* connect to the "destroy" signal */
   g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (mousepad_application_window_destroyed), application);
@@ -225,9 +222,9 @@ mousepad_application_new_window_with_document (MousepadWindow      *existing,
   GtkWidget *window;
   GdkScreen *screen;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (existing));
-  _mousepad_return_if_fail (document == NULL || MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (existing));
+  mousepad_return_if_fail (document == NULL || MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
 
   /* create a new window (signals added and already hooked up) */
   window = mousepad_application_create_window (application);
@@ -274,8 +271,8 @@ mousepad_application_new_window_with_files (MousepadApplication  *application,
   gboolean          succeed = FALSE;
   MousepadDocument *document;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
-  _mousepad_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
+  mousepad_return_if_fail (MOUSEPAD_IS_APPLICATION (application));
+  mousepad_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
 
   /* create a new window (signals added and already hooked up) */
   window = mousepad_application_create_window (application);
diff --git a/mousepad/mousepad-application.h b/mousepad/mousepad-application.h
index 9af3941..28e382c 100644
--- a/mousepad/mousepad-application.h
+++ b/mousepad/mousepad-application.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-dbus-infos.xml b/mousepad/mousepad-dbus-infos.xml
index c356bb9..3f0fab1 100644
--- a/mousepad/mousepad-dbus-infos.xml
+++ b/mousepad/mousepad-dbus-infos.xml
@@ -1,8 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
 <!--
-  $Id$
-
   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 the Free
   Software Foundation; either version 2 of the License, or (at your option)
diff --git a/mousepad/mousepad-dbus.c b/mousepad/mousepad-dbus.c
index 9b6380c..8b7e769 100644
--- a/mousepad/mousepad-dbus.c
+++ b/mousepad/mousepad-dbus.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -41,13 +40,18 @@
 
 
 
-static void      mousepad_dbus_service_class_init    (MousepadDBusServiceClass  *klass);
-static void      mousepad_dbus_service_init          (MousepadDBusService       *dbus_service);
-static void      mousepad_dbus_service_finalize      (GObject                   *object);
-static gboolean  mousepad_dbus_service_launch_files  (MousepadDBusService       *dbus_service,
-                                                      const gchar               *working_directory,
-                                                      gchar                    **filenames,
-                                                      GError                   **error);
+static void      mousepad_dbus_service_finalize      (GObject              *object);
+static gboolean  mousepad_dbus_service_launch_files  (MousepadDBusService  *dbus_service,
+                                                      const gchar          *working_directory,
+                                                      gchar               **filenames,
+                                                      GError              **error);
+static gboolean  mousepad_dbus_service_terminate     (MousepadDBusService  *dbus_service,
+                                                      GError              **error);
+
+
+
+/* include the dbus glue generated by dbus-binding-tool */
+#include <mousepad/mousepad-dbus-infos.h>
 
 
 
@@ -72,8 +76,7 @@ G_DEFINE_TYPE (MousepadDBusService, mousepad_dbus_service, G_TYPE_OBJECT);
 static void
 mousepad_dbus_service_class_init (MousepadDBusServiceClass *klass)
 {
-  extern const DBusGObjectInfo  dbus_glib_mousepad_dbus_service_object_info;
-  GObjectClass                 *gobject_class;
+  GObjectClass *gobject_class;
 
   gobject_class = G_OBJECT_CLASS (klass);
   gobject_class->finalize = mousepad_dbus_service_finalize;
@@ -154,8 +157,8 @@ mousepad_dbus_service_launch_files (MousepadDBusService  *dbus_service,
 {
   MousepadApplication *application;
 
-  _mousepad_return_val_if_fail (g_path_is_absolute (working_directory), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (g_path_is_absolute (working_directory), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   /* open a mousepad window */
   application = mousepad_application_get ();
@@ -265,7 +268,7 @@ mousepad_dbus_client_terminate (GError **error)
 {
   DBusMessage *message;
 
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   /* generate the message */
   message = dbus_message_new_method_call (MOUSEPAD_DBUS_INTERFACE, MOUSEPAD_DBUS_PATH,
@@ -308,8 +311,8 @@ mousepad_dbus_client_launch_files (gchar       **filenames,
   guint        length = 0;
   gboolean     succeed;
 
-  _mousepad_return_val_if_fail (g_path_is_absolute (working_directory), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (g_path_is_absolute (working_directory), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   /* get the length of the filesname string */
   if (filenames)
@@ -332,8 +335,3 @@ mousepad_dbus_client_launch_files (gchar       **filenames,
 
   return succeed;
 }
-
-
-
-/* include the dbus glue generated by dbus-binding-tool */
-#include <mousepad/mousepad-dbus-infos.h>
diff --git a/mousepad/mousepad-dbus.h b/mousepad/mousepad-dbus.h
index 6bc12dd..e38ec44 100644
--- a/mousepad/mousepad-dbus.h
+++ b/mousepad/mousepad-dbus.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-dialogs.c b/mousepad/mousepad-dialogs.c
index faa0c00..65fc9b9 100644
--- a/mousepad/mousepad-dialogs.c
+++ b/mousepad/mousepad-dialogs.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -32,7 +31,7 @@ mousepad_dialogs_show_about (GtkWindow *parent)
   {
     "Nick Schermer <nick at xfce.org>",
     "Erik Harrison <erikharrison at xfce.org>",
-    NULL,
+    NULL
   };
 
   /* show the dialog */
@@ -176,14 +175,14 @@ mousepad_dialogs_go_to_line_changed (GtkSpinButton *line_spin,
   GtkTextBuffer *buffer;
   GtkTextIter    iter;
 
-  _mousepad_return_if_fail (GTK_IS_SPIN_BUTTON (line_spin));
-  _mousepad_return_if_fail (GTK_IS_SPIN_BUTTON (col_spin));
+  mousepad_return_if_fail (GTK_IS_SPIN_BUTTON (line_spin));
+  mousepad_return_if_fail (GTK_IS_SPIN_BUTTON (col_spin));
 
   /* get the text buffer */
   buffer = mousepad_object_get_data (G_OBJECT (col_spin), "buffer");
 
   /* debug check */
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
 
   /* get iter at line */
   gtk_text_buffer_get_iter_at_line (buffer, &iter, gtk_spin_button_get_value_as_int (line_spin) - 1);
diff --git a/mousepad/mousepad-dialogs.h b/mousepad/mousepad-dialogs.h
index c97820f..cb01fd0 100644
--- a/mousepad/mousepad-dialogs.h
+++ b/mousepad/mousepad-dialogs.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -54,7 +53,7 @@ gint       mousepad_dialogs_other_tab_size      (GtkWindow     *parent,
                                                  gint           active_size);
 
 gboolean   mousepad_dialogs_go_to               (GtkWindow     *parent,
-                                                 GtkTextBuffer *buffer;);
+                                                 GtkTextBuffer *buffer);
 
 gboolean   mousepad_dialogs_clear_recent        (GtkWindow     *parent);
 
diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index 770bf88..2e39044 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -44,8 +43,6 @@
 
 
 
-static void      mousepad_document_class_init              (MousepadDocumentClass  *klass);
-static void      mousepad_document_init                    (MousepadDocument       *document);
 static void      mousepad_document_finalize                (GObject                *object);
 static void      mousepad_document_notify_cursor_position  (GtkTextBuffer          *buffer,
                                                             GParamSpec             *pspec,
@@ -62,7 +59,7 @@ static void      mousepad_document_drag_data_received      (GtkWidget
                                                             gint                    y,
                                                             GtkSelectionData       *selection_data,
                                                             guint                   info,
-                                                            guint                   time,
+                                                            guint                   drag_time,
                                                             MousepadDocument       *document);
 static void      mousepad_document_filename_changed        (MousepadDocument       *document,
                                                             const gchar            *filename);
@@ -281,8 +278,8 @@ mousepad_document_notify_cursor_position (GtkTextBuffer    *buffer,
   gint        line, column, selection;
   gint        tab_size;
 
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* get the current iter position */
   gtk_text_buffer_get_iter_at_mark (buffer, &iter, gtk_text_buffer_get_insert (buffer));
@@ -313,8 +310,8 @@ mousepad_document_notify_has_selection (GtkTextBuffer    *buffer,
   gint     selection;
   gboolean is_column_selection;
 
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* get length of the selection */
   selection = mousepad_view_get_selection_length (document->textview, &is_column_selection);
@@ -340,8 +337,8 @@ mousepad_document_notify_overwrite (GtkTextView      *textview,
 {
   gboolean overwrite;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
 
   /* whether overwrite is enabled */
   overwrite = gtk_text_view_get_overwrite (textview);
@@ -359,14 +356,14 @@ mousepad_document_drag_data_received (GtkWidget        *widget,
                                       gint              y,
                                       GtkSelectionData *selection_data,
                                       guint             info,
-                                      guint             time,
+                                      guint             drag_time,
                                       MousepadDocument *document)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* emit the drag-data-received signal from the document when a tab or uri has been dropped */
   if (info == TARGET_TEXT_URI_LIST || info == TARGET_GTK_NOTEBOOK_TAB)
-    g_signal_emit_by_name (G_OBJECT (document), "drag-data-received", context, x, y, selection_data, info, time);
+    g_signal_emit_by_name (G_OBJECT (document), "drag-data-received", context, x, y, selection_data, info, drag_time);
 }
 
 
@@ -377,8 +374,8 @@ mousepad_document_filename_changed (MousepadDocument *document,
 {
   gchar *utf8_filename, *utf8_basename;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (filename != NULL);
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (filename != NULL);
 
   /* convert the title into a utf-8 valid version for display */
   utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
@@ -420,9 +417,9 @@ mousepad_document_label_color (MousepadDocument *document)
   GdkColor  red   = {0, 0xffff, 0x0000, 0x0000};
   GdkColor *color;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (document->buffer));
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (document->file));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (document->buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (document->file));
 
   if (document->priv->label)
     {
@@ -433,7 +430,7 @@ mousepad_document_label_color (MousepadDocument *document)
         color = &green;
       else
         color = NULL;
-       
+
       /* update colors */
       gtk_widget_modify_fg (document->priv->label, GTK_STATE_NORMAL, color);
       gtk_widget_modify_fg (document->priv->label, GTK_STATE_ACTIVE, color);
@@ -446,7 +443,7 @@ void
 mousepad_document_set_overwrite (MousepadDocument *document,
                                  gboolean          overwrite)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   gtk_text_view_set_overwrite (GTK_TEXT_VIEW (document->textview), overwrite);
 }
@@ -457,7 +454,7 @@ void
 mousepad_document_set_word_wrap (MousepadDocument *document,
                                  gboolean          word_wrap)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* store the setting */
   document->priv->word_wrap = word_wrap;
@@ -475,7 +472,7 @@ mousepad_document_set_font (MousepadDocument *document,
 {
   PangoFontDescription *font_desc;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   if (G_LIKELY (font_name))
     {
@@ -491,7 +488,7 @@ mousepad_document_set_font (MousepadDocument *document,
 void
 mousepad_document_focus_textview (MousepadDocument *document)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* focus the textview */
   gtk_widget_grab_focus (GTK_WIDGET (document->textview));
@@ -502,7 +499,7 @@ mousepad_document_focus_textview (MousepadDocument *document)
 void
 mousepad_document_send_signals (MousepadDocument *document)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* re-send the cursor changed signal */
   mousepad_document_notify_cursor_position (document->buffer, NULL, document);
@@ -583,7 +580,7 @@ mousepad_document_get_basename (MousepadDocument *document)
 {
   static gint untitled_counter = 0;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), NULL);
 
   /* check if there is a filename set */
   if (document->priv->utf8_basename == NULL)
@@ -600,7 +597,7 @@ mousepad_document_get_basename (MousepadDocument *document)
 const gchar *
 mousepad_document_get_filename (MousepadDocument *document)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), NULL);
 
   return document->priv->utf8_filename;
 }
@@ -610,7 +607,7 @@ mousepad_document_get_filename (MousepadDocument *document)
 gboolean
 mousepad_document_get_word_wrap (MousepadDocument *document)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
 
   return document->priv->word_wrap;
 }
diff --git a/mousepad/mousepad-document.h b/mousepad/mousepad-document.h
index a35c7d0..2daff7f 100644
--- a/mousepad/mousepad-document.h
+++ b/mousepad/mousepad-document.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-encoding-dialog.c b/mousepad/mousepad-encoding-dialog.c
index ae965a6..953bfbd 100644
--- a/mousepad/mousepad-encoding-dialog.c
+++ b/mousepad/mousepad-encoding-dialog.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -23,22 +22,28 @@
 #include <string.h>
 #endif
 
+#include <glib.h>
+#include <glib/gstdio.h>
+
 #include <mousepad/mousepad-private.h>
 #include <mousepad/mousepad-document.h>
+#include <mousepad/mousepad-encoding.h>
 #include <mousepad/mousepad-encoding-dialog.h>
 #include <mousepad/mousepad-preferences.h>
 #include <mousepad/mousepad-util.h>
 
 
 
-static void     mousepad_encoding_dialog_class_init             (MousepadEncodingDialogClass *klass);
-static void     mousepad_encoding_dialog_init                   (MousepadEncodingDialog      *dialog);
 static void     mousepad_encoding_dialog_finalize               (GObject                     *object);
-static gboolean mousepad_encoding_dialog_test_encodings_idle    (gpointer                     user_data);
-static void     mousepad_encoding_dialog_test_encodings_destroy (gpointer                     user_data);
+static void     mousepad_encoding_dialog_response               (GtkDialog                   *dialog,
+                                                                 gint                         response_id);
+//~ static gboolean mousepad_encoding_dialog_test_encodings_idle    (gpointer                     user_data);
+//~ static void     mousepad_encoding_dialog_test_encodings_destroy (gpointer                     user_data);
 static void     mousepad_encoding_dialog_test_encodings         (MousepadEncodingDialog      *dialog);
+static void     mousepad_encoding_dialog_cancel_test_encodings  (GtkWidget                   *button,
+                                                                 MousepadEncodingDialog      *dialog);
 static void     mousepad_encoding_dialog_read_file              (MousepadEncodingDialog      *dialog,
-                                                                 const gchar                 *encoding);
+                                                                 MousepadEncoding encoding);
 static void     mousepad_encoding_dialog_button_toggled         (GtkWidget                   *button,
                                                                  MousepadEncodingDialog      *dialog);
 static void     mousepad_encoding_dialog_combo_changed          (GtkComboBox                 *combo,
@@ -50,7 +55,6 @@ enum
 {
   COLUMN_LABEL,
   COLUMN_ID,
-  COLUMN_UNPRINTABLE,
   N_COLUMNS
 };
 
@@ -67,20 +71,17 @@ struct _MousepadEncodingDialog
   MousepadDocument *document;
 
   /* encoding test idle id */
-  guint          encoding_id;
+  guint          timer_id;
 
-  /* encoding test position */
-  gint           encoding_n;
+  /* boolean to cancel the testing loop */
+  guint          cancel_testing : 1;
 
-  /* ok button */
+  /* dialog widget */
   GtkWidget     *button_ok;
-
-  /* error label and box */
+  GtkWidget     *button_cancel;
   GtkWidget     *error_box;
   GtkWidget     *error_label;
-
-  /* progressbar */
-  GtkWidget     *progress;
+  GtkWidget     *progress_bar;
 
   /* the three radio button */
   GtkWidget     *radio_utf8;
@@ -94,85 +95,6 @@ struct _MousepadEncodingDialog
 
 
 
-static const gchar *encodings[] =
-{
-"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-6",
-"ISO-8859-7", "ISO-8859-8", "ISO-8859-9", "ISO-8859-10", "ISO-8859-11", "ISO-8859-13",
-"ISO-8859-14", "ISO-8859-15", "ISO-8859-16",
-
-"ARMSCII-8",
-"ASCII",
-"BIG5",
-"BIG5-HKSCS",
-"BIG5-HKSCS:1999",
-"BIG5-HKSCS:2001",
-"C99", "IBM850", "CP862", "CP866", "CP874", "CP932", "CP936", "CP949", "CP950",
-"CP1133", "CP1250", "CP1251", "CP1252", "CP1253", "CP1254", "CP1255", "CP1256", "CP1257", "CP1258",
-"EUC-CN",
-"EUC-JP",
-"EUC-KR",
-"EUC-TW",
-"GB18030",
-"GBK",
-"Georgian-Academy",
-"Georgian-PS",
-"HP-ROMAN8",
-"HZ",
-"ISO-2022-CN",
-"ISO-2022-CN-EXT",
-"ISO-2022-JP",
-"ISO-2022-JP-1",
-"ISO-2022-JP-2",
-"ISO-2022-KR",
-
-
-
-"JAVA",
-"JOHAB",
-"KOI8-R",
-"KOI8-RU",
-"KOI8-T",
-"KOI8-U",
-"MacArabic",
-"MacCentralEurope",
-"MacCroatian",
-"MacGreek",
-"MacHebrew",
-"MacIceland",
-"Macintosh",
-"MacMacRoman",
-"MacRomania",
-"MacThai",
-"MacTurkish",
-"MacUkraine",
-"MacCyrillic",
-"MuleLao-1",
-"NEXTSTEP",
-"PT154",
-"SHIFT_JIS",
-"TCVN",
-"TIS-620,",
-"UCS-2",
-"UCS-2-INTERNAL",
-"UCS-2BE",
-"UCS-2LE",
-"UCS-4",
-"UCS-4-INTERNAL",
-"UCS-4BE",
-"UCS-4LE",
-"UTF-16",
-"UTF-16BE",
-"UTF-16LE",
-"UTF-32",
-"UTF-32BE",
-"UTF-32LE",
-"UTF-7",
-"UTF-8",
-"VISCII"
-};
-
-
-
 G_DEFINE_TYPE (MousepadEncodingDialog, mousepad_encoding_dialog, GTK_TYPE_DIALOG);
 
 
@@ -180,10 +102,14 @@ G_DEFINE_TYPE (MousepadEncodingDialog, mousepad_encoding_dialog, GTK_TYPE_DIALOG
 static void
 mousepad_encoding_dialog_class_init (MousepadEncodingDialogClass *klass)
 {
-  GObjectClass *gobject_class;
+  GObjectClass   *gobject_class;
+  GtkDialogClass *gtkdialog_class;
 
   gobject_class = G_OBJECT_CLASS (klass);
   gobject_class->finalize = mousepad_encoding_dialog_finalize;
+
+  gtkdialog_class = GTK_DIALOG_CLASS (klass);
+  gtkdialog_class->response = mousepad_encoding_dialog_response;
 }
 
 
@@ -236,7 +162,7 @@ mousepad_encoding_dialog_init (MousepadEncodingDialog *dialog)
   gtk_box_pack_start (GTK_BOX (hbox), dialog->radio_other, FALSE, FALSE, 0);
 
   /* create store */
-  dialog->store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
+  dialog->store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
 
   /* combobox with other charsets */
   dialog->combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (dialog->store));
@@ -249,10 +175,20 @@ mousepad_encoding_dialog_init (MousepadEncodingDialog *dialog)
   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (dialog->combo), cell, "text", COLUMN_LABEL, NULL);
 
   /* progress bar */
-  dialog->progress = gtk_progress_bar_new ();
-  gtk_box_pack_start (GTK_BOX (hbox), dialog->progress, TRUE, TRUE, 0);
-  gtk_progress_bar_set_text (GTK_PROGRESS_BAR (dialog->progress), "Testing encodings...");
-  gtk_widget_show (dialog->progress);
+  dialog->progress_bar = gtk_progress_bar_new ();
+  gtk_box_pack_start (GTK_BOX (hbox), dialog->progress_bar, TRUE, TRUE, 0);
+  gtk_progress_bar_set_text (GTK_PROGRESS_BAR (dialog->progress_bar), _("Checking encodings..."));
+  gtk_widget_show (dialog->progress_bar);
+
+  /* cancel button */
+  dialog->button_cancel = gtk_button_new ();
+  gtk_box_pack_start (GTK_BOX (hbox), dialog->button_cancel, FALSE, FALSE, 0);
+  g_signal_connect (G_OBJECT (dialog->button_cancel), "clicked", G_CALLBACK (mousepad_encoding_dialog_cancel_test_encodings), dialog);
+  gtk_widget_show (dialog->button_cancel);
+
+  icon = gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_MENU);
+  gtk_container_add (GTK_CONTAINER (dialog->button_cancel), icon);
+  gtk_widget_show (icon);
 
   /* error box */
   dialog->error_box = gtk_hbox_new (FALSE, 6);
@@ -290,8 +226,8 @@ mousepad_encoding_dialog_finalize (GObject *object)
   MousepadEncodingDialog *dialog = MOUSEPAD_ENCODING_DIALOG (object);
 
   /* stop running timeout */
-  if (G_UNLIKELY (dialog->encoding_id))
-    g_source_remove (dialog->encoding_id);
+  if (G_UNLIKELY (dialog->timer_id))
+    g_source_remove (dialog->timer_id);
 
   /* clear and release store */
   gtk_list_store_clear (dialog->store);
@@ -302,89 +238,104 @@ mousepad_encoding_dialog_finalize (GObject *object)
 
 
 
+static void
+mousepad_encoding_dialog_response (GtkDialog *dialog,
+                                   gint       response_id)
+{
+  /* make sure we cancel encoding testing asap */
+  MOUSEPAD_ENCODING_DIALOG (dialog)->cancel_testing = TRUE;
+}
+
+
+
 static gboolean
 mousepad_encoding_dialog_test_encodings_idle (gpointer user_data)
 {
   MousepadEncodingDialog *dialog = MOUSEPAD_ENCODING_DIALOG (user_data);
-  gdouble                 fraction;
-  gint                    unprintable, value;
-  const gchar            *encoding;
-  GtkTreeIter             iter, needle;
+  const gchar            *filename;
+  GMappedFile            *mapped_file;
+  GError                 *error = NULL;
+  const gchar            *contents;
+  gsize                   length, written;
+  guint                   i, n;
+  gchar                  *encoded;
 
   GDK_THREADS_ENTER ();
 
-  /* calculate the status */
-  fraction = (dialog->encoding_n + 1.00) / G_N_ELEMENTS (encodings);
-
-  /* set progress bar */
-  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (dialog->progress), fraction);
-
-  /* get the encoding */
-  encoding = encodings[dialog->encoding_n];
+  /* get the filename */
+  filename = mousepad_file_get_filename (dialog->document->file);
 
-  /* set an encoding */
-  unprintable = mousepad_file_test_encoding (dialog->document->file, encoding, NULL);
-
-  /* add the encoding to the combo box is the test succeed */
-  if (unprintable != -1)
+  /* check if the file exists */
+  if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
     {
-      /* get the first model iter, if there is one */
-      if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (dialog->store), &needle))
+      /* try to open the file */
+      mapped_file = g_mapped_file_new (filename, FALSE, &error);
+
+      if (G_LIKELY (mapped_file))
         {
-          for (;;)
-            {
-              /* get the column value */
-              gtk_tree_model_get (GTK_TREE_MODEL (dialog->store), &needle, COLUMN_UNPRINTABLE, &value, -1);
+          /* get the mapped file contents and length */
+          contents = g_mapped_file_get_contents (mapped_file);
+          length = g_mapped_file_get_length (mapped_file);
 
-              /* insert before the item with a higher number of unprintable characters */
-              if (value > unprintable)
+          if (G_LIKELY (contents && length > 0))
+            {
+              /* test all the encodings */
+              for (i = 0, n = 0; i < n_encoding_infos && !dialog->cancel_testing; i++)
                 {
-                  gtk_list_store_insert_before (dialog->store, &iter, &needle);
-
-                  break;
+                  /* set progress bar fraction */
+                  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (dialog->progress_bar), (i + 1.00) / n_encoding_infos);
+
+                  /* try to convert the content */
+                  encoded = g_convert (contents, length, "UTF-8", encoding_infos[i].charset, NULL, &written, NULL);
+
+                  if (G_LIKELY (encoded))
+                    {
+                      /* glib uses a faster validator when the string is nul-terminated */
+                      if (G_LIKELY (written > 0 && encoded[written] == '\0'))
+                        written = -1;
+
+                      /* validate the encoded content */
+                      if (G_LIKELY (g_utf8_validate (encoded, written, NULL)))
+                        {
+                          /* insert in the store */
+                          gtk_list_store_insert_with_values (dialog->store, NULL, n++,
+                                                             COLUMN_LABEL, encoding_infos[i].charset,
+                                                             COLUMN_ID, encoding_infos[i].encoding, -1);
+                        }
+
+                      /* cleanup */
+                      g_free (encoded);
+                    }
+
+                  /* iterate the main loop to update the gui */
+                  while (gtk_events_pending ())
+                    gtk_main_iteration ();
                 }
-
-              /* leave when we reached the end of the tree */
-              if (gtk_tree_model_iter_next (GTK_TREE_MODEL (dialog->store), &needle) == FALSE)
-                goto append_to_list;
             }
-        }
-      else
-        {
-          append_to_list:
 
-          /* append to the list */
-          gtk_list_store_append (dialog->store, &iter);
+          /* close the mapped file */
+#if GLIB_CHECK_VERSION (2, 21, 0)
+          g_mapped_file_unref (mapped_file);
+#else
+          g_mapped_file_free (mapped_file);
+#endif
         }
-
-      /* set the column data */
-      gtk_list_store_set (dialog->store, &iter,
-                          COLUMN_LABEL, encoding,
-                          COLUMN_ID, dialog->encoding_n,
-                          COLUMN_UNPRINTABLE, unprintable,
-                          -1);
     }
 
-  /* advance offset */
-  dialog->encoding_n++;
-
-  /* show the widgets when we're done */
-  if (dialog->encoding_n == G_N_ELEMENTS (encodings))
-    {
-      /* select the first item */
-      gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
+  /* hide progress bar and cancel button */
+  gtk_widget_hide (dialog->progress_bar);
+  gtk_widget_hide (dialog->button_cancel);
 
-      /* hide progress bar */
-      gtk_widget_hide (dialog->progress);
+  /* show the radio button and combo box */
+  gtk_widget_show (dialog->radio_other);
+  gtk_widget_show (dialog->combo);
 
-      /* show the radio button and combo box */
-      gtk_widget_show (dialog->radio_other);
-      gtk_widget_show (dialog->combo);
-    }
+  /* select the first item */
+  gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
 
   GDK_THREADS_LEAVE ();
 
-  return (dialog->encoding_n < G_N_ELEMENTS (encodings));
+  return FALSE;
 }
 
 
@@ -392,7 +343,7 @@ mousepad_encoding_dialog_test_encodings_idle (gpointer user_data)
 static void
 mousepad_encoding_dialog_test_encodings_destroy (gpointer user_data)
 {
-  MOUSEPAD_ENCODING_DIALOG (user_data)->encoding_id = 0;
+  MOUSEPAD_ENCODING_DIALOG (user_data)->timer_id = 0;
 }
 
 
@@ -400,27 +351,37 @@ mousepad_encoding_dialog_test_encodings_destroy (gpointer user_data)
 static void
 mousepad_encoding_dialog_test_encodings (MousepadEncodingDialog *dialog)
 {
-  if (G_LIKELY (dialog->encoding_id == 0))
+  if (G_LIKELY (dialog->timer_id == 0))
     {
-      /* reset counter */
-      dialog->encoding_n = 0;
+      /* reset boolean */
+      dialog->cancel_testing = FALSE;
 
-      /* start new idle function */
-      dialog->encoding_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, mousepad_encoding_dialog_test_encodings_idle,
-                                             dialog, mousepad_encoding_dialog_test_encodings_destroy);
+      /* start a new idle function */
+      dialog->timer_id = g_idle_add_full (G_PRIORITY_LOW, mousepad_encoding_dialog_test_encodings_idle,
+                                          dialog, mousepad_encoding_dialog_test_encodings_destroy);
     }
 }
 
 
 
 static void
+mousepad_encoding_dialog_cancel_test_encodings (GtkWidget              *button,
+                                                MousepadEncodingDialog *dialog)
+{
+  /* cancel the testing loop */
+  dialog->cancel_testing = TRUE;
+}
+
+
+
+static void
 mousepad_encoding_dialog_read_file (MousepadEncodingDialog *dialog,
-                                    const gchar            *encoding)
+                                    MousepadEncoding        encoding)
 {
   GtkTextIter  start, end;
   GError      *error = NULL;
   gchar       *message;
-  gboolean     succeed;
+  gint         result;
 
   /* clear buffer */
   gtk_text_buffer_get_bounds (dialog->document->buffer, &start, &end);
@@ -430,12 +391,12 @@ mousepad_encoding_dialog_read_file (MousepadEncodingDialog *dialog,
   mousepad_file_set_encoding (dialog->document->file, encoding);
 
   /* try to open the file */
-  succeed = mousepad_file_open (dialog->document->file, &error);
+  result = mousepad_file_open (dialog->document->file, NULL, &error);
 
   /* set sensitivity of the ok button */
-  gtk_widget_set_sensitive (dialog->button_ok, succeed);
+  gtk_widget_set_sensitive (dialog->button_ok, result == 0);
 
-  if (succeed)
+  if (result == 0)
     {
       /* no error, hide the box */
       gtk_widget_hide (dialog->error_box);
@@ -465,8 +426,6 @@ static void
 mousepad_encoding_dialog_button_toggled (GtkWidget              *button,
                                          MousepadEncodingDialog *dialog)
 {
-  const gchar *system_charset;
-
   /* ignore inactive buttons */
   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
     {
@@ -476,15 +435,12 @@ mousepad_encoding_dialog_button_toggled (GtkWidget              *button,
       if (button == dialog->radio_utf8)
         {
           /* open the file */
-          mousepad_encoding_dialog_read_file (dialog, NULL);
+          mousepad_encoding_dialog_read_file (dialog, MOUSEPAD_ENCODING_UTF_8);
         }
       else if (button == dialog->radio_system)
         {
-          /* get the system charset */
-          g_get_charset (&system_charset);
-
           /* open the file */
-          mousepad_encoding_dialog_read_file (dialog, system_charset);
+          mousepad_encoding_dialog_read_file (dialog, mousepad_encoding_user ());
         }
       else
         {
@@ -510,7 +466,7 @@ mousepad_encoding_dialog_combo_changed (GtkComboBox            *combo,
       gtk_tree_model_get (GTK_TREE_MODEL (dialog->store), &iter, COLUMN_ID, &id, -1);
 
       /* open the file with other encoding */
-      mousepad_encoding_dialog_read_file (dialog, encodings[id]);
+      mousepad_encoding_dialog_read_file (dialog, id);
     }
 }
 
@@ -522,8 +478,8 @@ mousepad_encoding_dialog_new (GtkWindow    *parent,
 {
   MousepadEncodingDialog *dialog;
 
-  _mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
+  mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
 
   /* create the dialog */
   dialog = g_object_new (MOUSEPAD_TYPE_ENCODING_DIALOG, NULL);
@@ -545,10 +501,20 @@ mousepad_encoding_dialog_new (GtkWindow    *parent,
 
 
 
-const gchar *
+MousepadEncoding
 mousepad_encoding_dialog_get_encoding (MousepadEncodingDialog *dialog)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_ENCODING_DIALOG (dialog), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_ENCODING_DIALOG (dialog), MOUSEPAD_ENCODING_NONE);
 
   return mousepad_file_get_encoding (dialog->document->file);
 }
+
+
+
+const gchar *
+mousepad_encoding_dialog_get_encoding_custom (MousepadEncodingDialog *dialog)
+{
+  mousepad_return_val_if_fail (MOUSEPAD_IS_ENCODING_DIALOG (dialog), NULL);
+
+  return NULL;
+}
diff --git a/mousepad/mousepad-encoding-dialog.h b/mousepad/mousepad-encoding-dialog.h
index a992491..ca4ee7f 100644
--- a/mousepad/mousepad-encoding-dialog.h
+++ b/mousepad/mousepad-encoding-dialog.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -20,6 +19,8 @@
 
 G_BEGIN_DECLS
 
+#include <mousepad/mousepad-encoding.h>
+
 #define MOUSEPAD_TYPE_ENCODING_DIALOG            (mousepad_encoding_dialog_get_type ())
 #define MOUSEPAD_ENCODING_DIALOG(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOUSEPAD_TYPE_ENCODING_DIALOG, MousepadEncodingDialog))
 #define MOUSEPAD_ENCODING_DIALOG_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), MOUSEPAD_TYPE_ENCODING_DIALOG, MousepadEncodingDialogClass))
@@ -30,12 +31,14 @@ G_BEGIN_DECLS
 typedef struct _MousepadEncodingDialogClass MousepadEncodingDialogClass;
 typedef struct _MousepadEncodingDialog      MousepadEncodingDialog;
 
-GType        mousepad_encoding_dialog_get_type     (void) G_GNUC_CONST;
+GType             mousepad_encoding_dialog_get_type            (void) G_GNUC_CONST;
+
+GtkWidget        *mousepad_encoding_dialog_new                 (GtkWindow              *parent,
+                                                                MousepadFile           *file);
 
-GtkWidget   *mousepad_encoding_dialog_new          (GtkWindow              *parent,
-                                                    MousepadFile           *file);
+MousepadEncoding  mousepad_encoding_dialog_get_encoding        (MousepadEncodingDialog *dialog);
 
-const gchar *mousepad_encoding_dialog_get_encoding (MousepadEncodingDialog *dialog);
+const gchar      *mousepad_encoding_dialog_get_encoding_custom (MousepadEncodingDialog *dialog);
 
 G_END_DECLS
 
diff --git a/mousepad/mousepad-encoding.c b/mousepad/mousepad-encoding.c
new file mode 100644
index 0000000..ac2f19a
--- /dev/null
+++ b/mousepad/mousepad-encoding.c
@@ -0,0 +1,201 @@
+/*
+ * 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 the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include <glib.h>
+
+#include <mousepad/mousepad-private.h>
+#include <mousepad/mousepad-encoding.h>
+
+
+
+const MousepadEncodingInfo encoding_infos[] =
+{
+  /* west european */
+  { MOUSEPAD_ENCODING_ISO_8859_14,  "ISO-8859-14",      N_("Celtic") },
+  { MOUSEPAD_ENCODING_ISO_8859_7,   "ISO-8859-7",       N_("Greek") },
+  { MOUSEPAD_ENCODING_WINDOWS_1253, "WINDOWS-1253",     N_("Greek") },
+  { MOUSEPAD_ENCODING_ISO_8859_10,  "ISO-8859-10",      N_("Nordic") },
+  { MOUSEPAD_ENCODING_ISO_8859_3,   "ISO-8859-3",       N_("South European") },
+  { MOUSEPAD_ENCODING_IBM_850,      "IBM850",           N_("Western") },
+  { MOUSEPAD_ENCODING_ISO_8859_1,   "ISO-8859-1",       N_("Western") },
+  { MOUSEPAD_ENCODING_ISO_8859_15,  "ISO-8859-15",      N_("Western") },
+  { MOUSEPAD_ENCODING_WINDOWS_1252, "WINDOWS-1252",     N_("Western") },
+
+  /* east european */
+  { MOUSEPAD_ENCODING_ISO_8859_4,   "ISO-8859-4",       N_("Baltic") },
+  { MOUSEPAD_ENCODING_ISO_8859_13,  "ISO-8859-13",      N_("Baltic") },
+  { MOUSEPAD_ENCODING_WINDOWS_1257, "WINDOWS-1257",     N_("Baltic") },
+  { MOUSEPAD_ENCODING_IBM_852,      "IBM852",           N_("Central European") },
+  { MOUSEPAD_ENCODING_ISO_8859_2,   "ISO-8859-2",       N_("Central European") },
+  { MOUSEPAD_ENCODING_WINDOWS_1250, "WINDOWS-1250",     N_("Central European") },
+  { MOUSEPAD_ENCODING_IBM_855,      "IBM855",           N_("Cyrillic") },
+  { MOUSEPAD_ENCODING_ISO_8859_5,   "ISO-8859-5",       N_("Cyrillic") },
+  { MOUSEPAD_ENCODING_ISO_IR_111,   "ISO-IR-111",       N_("Cyrillic") },
+  { MOUSEPAD_ENCODING_KOI8_R,       "KOI8R",            N_("Cyrillic") },
+  { MOUSEPAD_ENCODING_WINDOWS_1251, "WINDOWS-1251",     N_("Cyrillic") },
+  { MOUSEPAD_ENCODING_CP_866,       "CP866",            N_("Cyrillic/Russian") },
+  { MOUSEPAD_ENCODING_KOI8_U,       "KOI8U",            N_("Cyrillic/Ukrainian") },
+  { MOUSEPAD_ENCODING_ISO_8859_16,  "ISO-8859-16",      N_("Romanian") },
+
+  /* middle eastern */
+  { MOUSEPAD_ENCODING_IBM_864,      "IBM864",           N_("Arabic") },
+  { MOUSEPAD_ENCODING_ISO_8859_6,   "ISO-8859-6",       N_("Arabic") },
+  { MOUSEPAD_ENCODING_WINDOWS_1256, "WINDOWS-1256",     N_("Arabic") },
+  { MOUSEPAD_ENCODING_IBM_862,      "IBM862",           N_("Hebrew") },
+  { MOUSEPAD_ENCODING_ISO_8859_8_I, "ISO-8859-8-I",     N_("Hebrew") },
+  { MOUSEPAD_ENCODING_WINDOWS_1255, "WINDOWS-1255",     N_("Hebrew") },
+  { MOUSEPAD_ENCODING_ISO_8859_8,   "ISO-8859-8",       N_("Hebrew Visual") },
+
+  /* asian */
+  { MOUSEPAD_ENCODING_ARMSCII_8,    "ARMSCII-8",        N_("Armenian") },
+  { MOUSEPAD_ENCODING_GEOSTD8,      "GEORGIAN-ACADEMY", N_("Georgian") },
+  { MOUSEPAD_ENCODING_TIS_620,      "TIS-620",          N_("Thai") },
+  { MOUSEPAD_ENCODING_IBM_857,      "IBM857",           N_("Turkish") },
+  { MOUSEPAD_ENCODING_WINDOWS_1254, "WINDOWS-1254",     N_("Turkish") },
+  { MOUSEPAD_ENCODING_ISO_8859_9,   "ISO-8859-9",       N_("Turkish") },
+  { MOUSEPAD_ENCODING_TCVN,         "TCVN",             N_("Vietnamese") },
+  { MOUSEPAD_ENCODING_VISCII,       "VISCII",           N_("Vietnamese") },
+  { MOUSEPAD_ENCODING_WINDOWS_1258, "WINDOWS-1258",     N_("Vietnamese") },
+
+  /* unicode */
+  { MOUSEPAD_ENCODING_UTF_7,        "UTF-7",            N_("Unicode") },
+  { MOUSEPAD_ENCODING_UTF_8,        "UTF-8",            N_("Unicode") },
+  { MOUSEPAD_ENCODING_UTF_16LE,     "UTF-16LE",         N_("Unicode") },
+  { MOUSEPAD_ENCODING_UTF_16BE,     "UTF-16BE",         N_("Unicode") },
+  { MOUSEPAD_ENCODING_UCS_2LE,      "UCS-2LE",          N_("Unicode") },
+  { MOUSEPAD_ENCODING_UCS_2BE,      "UCS-2BE",          N_("Unicode") },
+  { MOUSEPAD_ENCODING_UTF_32LE,     "UTF-32LE",         N_("Unicode") },
+  { MOUSEPAD_ENCODING_UTF_32BE,     "UTF-32BE",         N_("Unicode") },
+
+  /* east asian */
+  { MOUSEPAD_ENCODING_GB18030,      "GB18030",          N_("Chinese Simplified") },
+  { MOUSEPAD_ENCODING_GB2312,       "GB2312",           N_("Chinese Simplified") },
+  { MOUSEPAD_ENCODING_GBK,          "GBK",              N_("Chinese Simplified") },
+  { MOUSEPAD_ENCODING_HZ,           "HZ",               N_("Chinese Simplified") },
+  { MOUSEPAD_ENCODING_BIG5,         "BIG5",             N_("Chinese Traditional") },
+  { MOUSEPAD_ENCODING_BIG5_HKSCS,   "BIG5-HKSCS",       N_("Chinese Traditional") },
+  { MOUSEPAD_ENCODING_EUC_TW,       "EUC-TW",           N_("Chinese Traditional") },
+  { MOUSEPAD_ENCODING_EUC_JP,       "EUC-JP",           N_("Japanese") },
+  { MOUSEPAD_ENCODING_ISO_2022_JP,  "ISO-2022-JP",      N_("Japanese") },
+  { MOUSEPAD_ENCODING_SHIFT_JIS,    "SHIFT_JIS",        N_("Japanese") },
+  { MOUSEPAD_ENCODING_EUC_KR,       "EUC-KR",           N_("Korean") },
+  { MOUSEPAD_ENCODING_ISO_2022_KR,  "ISO-2022-KR",      N_("Korean") },
+  { MOUSEPAD_ENCODING_JOHAB,        "JOHAB",            N_("Korean") },
+  { MOUSEPAD_ENCODING_UHC,          "UHC",              N_("Korean") }
+};
+
+
+
+guint n_encoding_infos = G_N_ELEMENTS (encoding_infos);
+
+
+
+const gchar *
+mousepad_encoding_get_charset (MousepadEncoding encoding)
+{
+  guint i;
+
+  /* try to find and return the charset */
+  for (i = 0; i < G_N_ELEMENTS (encoding_infos); i++)
+    if (encoding_infos[i].encoding == encoding)
+      return encoding_infos[i].charset;
+
+  return NULL;
+}
+
+
+
+const gchar *
+mousepad_encoding_get_name (MousepadEncoding encoding)
+{
+  guint i;
+
+  /* try to find and return the translated name */
+  for (i = 0; i < G_N_ELEMENTS (encoding_infos); i++)
+    if (encoding_infos[i].encoding == encoding)
+      return _(encoding_infos[i].name);
+
+  return NULL;
+}
+
+
+
+MousepadEncoding
+mousepad_encoding_user (void)
+{
+  static MousepadEncoding  encoding = MOUSEPAD_ENCODING_NONE;
+  const gchar             *charset;
+
+  if (G_UNLIKELY (encoding == MOUSEPAD_ENCODING_NONE))
+    {
+      /* try to find the user charset */
+      if (g_get_charset (&charset) == FALSE)
+        encoding = mousepad_encoding_find (charset);
+
+      /* default to utf-8 */
+      if (G_LIKELY (encoding == MOUSEPAD_ENCODING_NONE))
+        encoding = MOUSEPAD_ENCODING_UTF_8;
+    }
+
+  return encoding;
+}
+
+
+
+MousepadEncoding
+mousepad_encoding_find (const gchar *charset)
+{
+  guint i;
+
+  /* check for invalid strings */
+  if (G_UNLIKELY (charset == NULL || *charset == '\0'))
+    return MOUSEPAD_ENCODING_NONE;
+
+  /* find the charset */
+  for (i = 0; i < G_N_ELEMENTS (encoding_infos); i++)
+    if (strcasecmp (encoding_infos[i].charset, charset) == 0)
+      return encoding_infos[i].encoding;
+
+  /* no encoding charset found */
+  return MOUSEPAD_ENCODING_NONE;
+}
+
+
+
+gboolean
+mousepad_encoding_is_unicode (MousepadEncoding encoding)
+{
+  const gchar *charset;
+
+  /* get the characterset name */
+  charset = mousepad_encoding_get_charset (encoding);
+
+  /* check for unicode charset */
+  if (charset != NULL && (strncmp (charset, "UTF", 3) == 0
+      || strncmp (charset, "UCS", 3) == 0))
+    return TRUE;
+
+  /* not an unicode charset */
+  return FALSE;
+}
diff --git a/mousepad/mousepad-encoding.h b/mousepad/mousepad-encoding.h
new file mode 100644
index 0000000..eee8528
--- /dev/null
+++ b/mousepad/mousepad-encoding.h
@@ -0,0 +1,125 @@
+/*
+ * 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 the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __MOUSEPAD_ENCODINGS_H__
+#define __MOUSEPAD_ENCODINGS_H__
+
+G_BEGIN_DECLS
+
+typedef enum   _MousepadEncoding     MousepadEncoding;
+typedef struct _MousepadEncodingInfo MousepadEncodingInfo;
+
+enum _MousepadEncoding
+{
+  MOUSEPAD_ENCODING_NONE,
+  MOUSEPAD_ENCODING_CUSTOM,
+
+  MOUSEPAD_ENCODING_ISO_8859_1,
+  MOUSEPAD_ENCODING_ISO_8859_2,
+  MOUSEPAD_ENCODING_ISO_8859_3,
+  MOUSEPAD_ENCODING_ISO_8859_4,
+  MOUSEPAD_ENCODING_ISO_8859_5,
+  MOUSEPAD_ENCODING_ISO_8859_6,
+  MOUSEPAD_ENCODING_ISO_8859_7,
+  MOUSEPAD_ENCODING_ISO_8859_8,
+  MOUSEPAD_ENCODING_ISO_8859_8_I,
+  MOUSEPAD_ENCODING_ISO_8859_9,
+  MOUSEPAD_ENCODING_ISO_8859_10,
+  MOUSEPAD_ENCODING_ISO_8859_13,
+  MOUSEPAD_ENCODING_ISO_8859_14,
+  MOUSEPAD_ENCODING_ISO_8859_15,
+  MOUSEPAD_ENCODING_ISO_8859_16,
+
+  MOUSEPAD_ENCODING_UTF_7,
+  MOUSEPAD_ENCODING_UTF_8,
+  MOUSEPAD_ENCODING_UTF_16LE,
+  MOUSEPAD_ENCODING_UTF_16BE,
+  MOUSEPAD_ENCODING_UCS_2LE,
+  MOUSEPAD_ENCODING_UCS_2BE,
+  MOUSEPAD_ENCODING_UTF_32LE,
+  MOUSEPAD_ENCODING_UTF_32BE,
+
+  MOUSEPAD_ENCODING_ARMSCII_8,
+  MOUSEPAD_ENCODING_BIG5,
+  MOUSEPAD_ENCODING_BIG5_HKSCS,
+  MOUSEPAD_ENCODING_CP_866,
+
+  MOUSEPAD_ENCODING_EUC_JP,
+  MOUSEPAD_ENCODING_EUC_KR,
+  MOUSEPAD_ENCODING_EUC_TW,
+
+  MOUSEPAD_ENCODING_GB18030,
+  MOUSEPAD_ENCODING_GB2312,
+  MOUSEPAD_ENCODING_GBK,
+  MOUSEPAD_ENCODING_GEOSTD8,
+  MOUSEPAD_ENCODING_HZ,
+
+  MOUSEPAD_ENCODING_IBM_850,
+  MOUSEPAD_ENCODING_IBM_852,
+  MOUSEPAD_ENCODING_IBM_855,
+  MOUSEPAD_ENCODING_IBM_857,
+  MOUSEPAD_ENCODING_IBM_862,
+  MOUSEPAD_ENCODING_IBM_864,
+
+  MOUSEPAD_ENCODING_ISO_2022_JP,
+  MOUSEPAD_ENCODING_ISO_2022_KR,
+  MOUSEPAD_ENCODING_ISO_IR_111,
+  MOUSEPAD_ENCODING_JOHAB,
+  MOUSEPAD_ENCODING_KOI8_R,
+  MOUSEPAD_ENCODING_KOI8_U,
+
+  MOUSEPAD_ENCODING_SHIFT_JIS,
+  MOUSEPAD_ENCODING_TCVN,
+  MOUSEPAD_ENCODING_TIS_620,
+  MOUSEPAD_ENCODING_UHC,
+  MOUSEPAD_ENCODING_VISCII,
+
+  MOUSEPAD_ENCODING_WINDOWS_1250,
+  MOUSEPAD_ENCODING_WINDOWS_1251,
+  MOUSEPAD_ENCODING_WINDOWS_1252,
+  MOUSEPAD_ENCODING_WINDOWS_1253,
+  MOUSEPAD_ENCODING_WINDOWS_1254,
+  MOUSEPAD_ENCODING_WINDOWS_1255,
+  MOUSEPAD_ENCODING_WINDOWS_1256,
+  MOUSEPAD_ENCODING_WINDOWS_1257,
+  MOUSEPAD_ENCODING_WINDOWS_1258
+};
+
+struct _MousepadEncodingInfo
+{
+  MousepadEncoding  encoding;
+  const gchar      *charset;
+  const gchar      *name;
+};
+
+
+extern const MousepadEncodingInfo encoding_infos[];
+
+extern guint                      n_encoding_infos;
+
+const gchar      *mousepad_encoding_get_charset (MousepadEncoding  encoding);
+
+const gchar      *mousepad_encoding_get_name    (MousepadEncoding  encoding);
+
+MousepadEncoding  mousepad_encoding_user        (void);
+
+MousepadEncoding  mousepad_encoding_find        (const gchar      *charset);
+
+gboolean          mousepad_encoding_is_unicode  (MousepadEncoding  encoding);
+
+G_END_DECLS
+
+#endif /* !__MOUSEPAD_ENCODINGS_H__ */
diff --git a/mousepad/mousepad-file.c b/mousepad/mousepad-file.c
index 81fec42..b45c980 100644
--- a/mousepad/mousepad-file.c
+++ b/mousepad/mousepad-file.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -19,9 +18,18 @@
 #include <config.h>
 #endif
 
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
@@ -36,9 +44,6 @@
 #include <mousepad/mousepad-file.h>
 
 
-#define MAX_ENCODING_CHECK_CHARS       (10000)
-#define MOUSEPAD_GET_LINE_END_CHARS(a) ((a) == MOUSEPAD_LINE_END_DOS ? "\r\n\0" : ((a) == MOUSEPAD_LINE_END_MAC ? "\r\0" : "\n\0"))
-
 
 enum
 {
@@ -64,7 +69,7 @@ struct _MousepadFile
   gchar              *filename;
 
   /* encoding of the file */
-  gchar              *encoding;
+  MousepadEncoding    encoding;
 
   /* line ending of the file */
   MousepadLineEnding  line_ending;
@@ -74,12 +79,13 @@ struct _MousepadFile
 
   /* if file is read-only */
   guint               readonly : 1;
+
+  /* whether we write the bom at the start of the file */
+  guint               write_bom : 1;
 };
 
 
 
-static void  mousepad_file_class_init       (MousepadFileClass  *klass);
-static void  mousepad_file_init             (MousepadFile       *file);
 static void  mousepad_file_finalize         (GObject            *object);
 static void  mousepad_file_set_readonly     (MousepadFile       *file,
                                              gboolean            readonly);
@@ -120,7 +126,7 @@ mousepad_file_class_init (MousepadFileClass *klass)
                   0, NULL, NULL,
                   g_cclosure_marshal_VOID__BOOLEAN,
                   G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
-  
+
   file_signals[FILENAME_CHANGED] =
     g_signal_new (I_("filename-changed"),
                   G_TYPE_FROM_CLASS (gobject_class),
@@ -137,10 +143,15 @@ mousepad_file_init (MousepadFile *file)
 {
   /* initialize */
   file->filename    = NULL;
-  file->encoding    = NULL;
-  file->line_ending = MOUSEPAD_LINE_END_NONE;
+  file->encoding    = MOUSEPAD_ENCODING_UTF_8;
+#ifdef G_OS_WIN32
+  file->line_ending = MOUSEPAD_EOL_DOS;
+#else
+  file->line_ending = MOUSEPAD_EOL_UNIX;
+#endif
   file->readonly    = TRUE;
   file->mtime       = 0;
+  file->write_bom   = FALSE;
 }
 
 
@@ -152,7 +163,6 @@ mousepad_file_finalize (GObject *object)
 
   /* cleanup */
   g_free (file->filename);
-  g_free (file->encoding);
 
   /* release the reference from the buffer */
   g_object_unref (G_OBJECT (file->buffer));
@@ -166,16 +176,85 @@ static void
 mousepad_file_set_readonly (MousepadFile *file,
                             gboolean      readonly)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
-  
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+
   if (G_LIKELY (file->readonly != readonly))
     {
       /* store new value */
       file->readonly = readonly;
-      
+
       /* emit signal */
       g_signal_emit (G_OBJECT (file), file_signals[READONLY_CHANGED], 0, readonly);
-    }  
+    }
+}
+
+
+
+static MousepadEncoding
+mousepad_file_encoding_read_bom (const gchar *contents,
+                                 gsize        length,
+                                 gsize       *bom_length)
+{
+  const guchar     *bom = (const guchar *) contents;
+  MousepadEncoding  encoding = MOUSEPAD_ENCODING_NONE;
+  gsize             bytes = 0;
+
+  mousepad_return_val_if_fail (contents != NULL && length > 0, MOUSEPAD_ENCODING_NONE);
+  mousepad_return_val_if_fail (contents != NULL && length > 0, MOUSEPAD_ENCODING_NONE);
+
+  switch (bom[0])
+    {
+      case 0xef:
+        if (length >= 3 && bom[1] == 0xbb && bom[2] == 0xbf)
+          {
+            bytes = 3;
+            encoding = MOUSEPAD_ENCODING_UTF_8;
+          }
+        break;
+
+      case 0x00:
+        if (length >= 4 && bom[1] == 0x00 && bom[2] == 0xfe && bom[3] == 0xff)
+          {
+            bytes = 4;
+            encoding = MOUSEPAD_ENCODING_UTF_32BE;
+          }
+        break;
+
+      case 0xff:
+        if (length >= 4 && bom[1] == 0xfe && bom[2] == 0x00 && bom[3] == 0x00)
+          {
+            bytes = 4;
+            encoding = MOUSEPAD_ENCODING_UTF_32LE;
+          }
+        else if (length >= 2 && bom[1] == 0xfe)
+          {
+            bytes = 2;
+            encoding = MOUSEPAD_ENCODING_UTF_16LE;
+          }
+        break;
+
+      case 0x2b:
+        if (length >= 4 && (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) &&
+            (bom[3] == 0x38 || bom[3] == 0x39 || bom[3] == 0x2b || bom[3] == 0x2f))
+          {
+            bytes = 4;
+            encoding = MOUSEPAD_ENCODING_UTF_7;
+          }
+        break;
+
+      case 0xfe:
+        if (length >= 2 && bom[1] == 0xff)
+          {
+            bytes = 2;
+            encoding = MOUSEPAD_ENCODING_UTF_16BE;
+          }
+        break;
+    }
+
+  if (bom_length)
+    *bom_length = bytes;
+
+  return encoding;
 }
 
 
@@ -185,7 +264,7 @@ mousepad_file_new (GtkTextBuffer *buffer)
 {
   MousepadFile *file;
 
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
 
   file = g_object_new (MOUSEPAD_TYPE_FILE, NULL);
 
@@ -201,7 +280,7 @@ void
 mousepad_file_set_filename (MousepadFile *file,
                             const gchar  *filename)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
 
   /* free the old filename */
   g_free (file->filename);
@@ -218,7 +297,7 @@ mousepad_file_set_filename (MousepadFile *file,
 const gchar *
 mousepad_file_get_filename (MousepadFile *file)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
 
   return file->filename;
 }
@@ -228,7 +307,7 @@ mousepad_file_get_filename (MousepadFile *file)
 gchar *
 mousepad_file_get_uri (MousepadFile *file)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
 
   return g_filename_to_uri (file->filename, NULL, NULL);
 }
@@ -236,26 +315,23 @@ mousepad_file_get_uri (MousepadFile *file)
 
 
 void
-mousepad_file_set_encoding (MousepadFile *file,
-                            const gchar  *encoding)
+mousepad_file_set_encoding (MousepadFile     *file,
+                            MousepadEncoding  encoding)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
-
-  /* cleanup */
-  g_free (file->encoding);
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
 
   /* set new encoding */
-  file->encoding = g_strdup (encoding);
+  file->encoding = encoding;
 }
 
 
 
-const gchar *
+MousepadEncoding
 mousepad_file_get_encoding (MousepadFile *file)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), MOUSEPAD_ENCODING_NONE);
 
-  return file->encoding ? file->encoding : "UTF-8";
+  return file->encoding;
 }
 
 
@@ -263,7 +339,7 @@ mousepad_file_get_encoding (MousepadFile *file)
 gboolean
 mousepad_file_get_read_only (MousepadFile *file)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
 
   return file->filename ? file->readonly : FALSE;
 }
@@ -271,10 +347,38 @@ mousepad_file_get_read_only (MousepadFile *file)
 
 
 void
+mousepad_file_set_write_bom (MousepadFile *file,
+                             gboolean      write_bom)
+{
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  mousepad_return_if_fail (mousepad_encoding_is_unicode (file->encoding));
+
+  /* set new value */
+  file->write_bom = write_bom;
+}
+
+
+
+gboolean
+mousepad_file_get_write_bom (MousepadFile *file,
+                             gboolean     *sensitive)
+{
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
+
+  /* return if we can write a bom */
+  if (G_LIKELY (sensitive))
+    *sensitive = mousepad_encoding_is_unicode (file->encoding);
+
+  return file->write_bom;
+}
+
+
+
+void
 mousepad_file_set_line_ending (MousepadFile       *file,
                                MousepadLineEnding  line_ending)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
 
   file->line_ending = line_ending;
 }
@@ -284,157 +388,227 @@ mousepad_file_set_line_ending (MousepadFile       *file,
 MousepadLineEnding
 mousepad_file_get_line_ending (MousepadFile *file)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), MOUSEPAD_LINE_END_NONE);
-
   return file->line_ending;
 }
 
 
 
-gboolean
+gint
 mousepad_file_open (MousepadFile  *file,
+                    const gchar   *template_filename,
                     GError       **error)
 {
-  GIOChannel  *channel;
-  GIOStatus    status;
-  gsize        length, terminator_pos;
-  gboolean     succeed = FALSE;
-  gchar       *string;
-  const gchar *line_term;
-  GtkTextIter  start, end;
-  struct stat  statb;
-
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-  _mousepad_return_val_if_fail (file->filename != NULL, FALSE);
+  GMappedFile      *mapped_file;
+  const gchar      *filename;
+  gint              retval = ERROR_READING_FAILED;
+  gsize             length, written;
+  gsize             bom_length;
+  const gchar      *contents;
+  gchar            *encoded = NULL;
+  const gchar      *charset;
+  GtkTextIter       start_iter, end_iter;
+  struct stat       statb;
+  const gchar      *end, *n, *m;
+  MousepadEncoding  bom_encoding;
+
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (file->filename != NULL || template_filename != NULL, FALSE);
+
+  /* get the filename */
+  if (G_UNLIKELY (template_filename != NULL))
+    filename = template_filename;
+  else
+    filename = file->filename;
 
   /* check if the file exists, if not, it's a filename from the command line */
-  if (g_file_test (file->filename, G_FILE_TEST_EXISTS) == FALSE)
+  if (g_file_test (filename, G_FILE_TEST_EXISTS) == FALSE)
     {
       /* update readonly status */
       mousepad_file_set_readonly (file, FALSE);
 
-      return TRUE;
+      return 0;
     }
 
-  /* open the channel */
-  channel = g_io_channel_new_file (file->filename, "r", error);
+  /* try to open the file */
+  mapped_file = g_mapped_file_new (filename, FALSE, error);
 
-  if (G_LIKELY (channel))
+  if (G_LIKELY (mapped_file))
     {
-      /* freeze notifications */
-      g_object_freeze_notify (G_OBJECT (file->buffer));
+      /* get the mapped file contents and length */
+      contents = g_mapped_file_get_contents (mapped_file);
+      length = g_mapped_file_get_length (mapped_file);
 
-      /* set the encoding of the channel */
-      if (file->encoding != NULL)
+      if (G_LIKELY (contents != NULL && length > 0))
         {
-          /* set the encoding */
-          status = g_io_channel_set_encoding (channel, file->encoding, error);
+          /* detect if there is a bom with the encoding type */
+          bom_encoding = mousepad_file_encoding_read_bom (contents, length, &bom_length);
+          if (G_UNLIKELY (bom_encoding != MOUSEPAD_ENCODING_NONE))
+            {
+              /* we've found a valid bom at the start of the contents */
+              file->write_bom = TRUE;
 
-          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
-            goto failed;
-        }
+              /* advance the contents offset and decrease length */
+              contents += bom_length;
+              length -= bom_length;
 
-      /* get the iter at the beginning of the document */
-      gtk_text_buffer_get_start_iter (file->buffer, &end);
+              /* set the detected encoding */
+              file->encoding = bom_encoding;
+            }
 
-      /* read the content of the file */
-      for (;;)
-        {
-          /* read the line */
-          status = g_io_channel_read_line (channel, &string, &length, &terminator_pos, error);
+          /* handle encoding and check for utf-8 valid text */
+          if (G_LIKELY (file->encoding == MOUSEPAD_ENCODING_UTF_8))
+            {
+              validate:
 
-          /* leave on problems */
-          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_EOF))
-            goto failed;
+              /* glib uses a faster validator when the string is nul-terminated */
+              if (G_LIKELY (length > 0 && contents[length] == '\0'))
+                length = -1;
 
-          if (G_LIKELY (string))
+              /* leave when the contents is not utf-8 valid */
+              if (g_utf8_validate (contents, length, &end) == FALSE)
+                {
+                  /* set return value */
+                  retval = ERROR_NOT_UTF8_VALID;
+
+                  /* set an error */
+                  g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+                               _("Invalid byte sequence in conversion input"));
+
+                  goto failed;
+                }
+            }
+          else
             {
-              /* detect the line ending of the file */
-              if (length != terminator_pos
-                  && file->line_ending == MOUSEPAD_LINE_END_NONE)
+              /* get the encoding charset */
+              charset = mousepad_encoding_get_charset (file->encoding);
+
+              /* convert the contents */
+              encoded = g_convert (contents, length, "UTF-8", charset, NULL, &written, error);
+
+              /* check if the string is utf-8 valid */
+              if (G_UNLIKELY (encoded == NULL))
                 {
-                  /* get the characters */
-                  line_term = string + terminator_pos;
-
-                  if (strcmp (line_term, "\n") == 0)
-                    file->line_ending = MOUSEPAD_LINE_END_UNIX;
-                  else if (strcmp (line_term, "\r") == 0)
-                    file->line_ending = MOUSEPAD_LINE_END_MAC;
-                  else if (strcmp (line_term, "\r\n") == 0)
-                    file->line_ending = MOUSEPAD_LINE_END_DOS;
-                  else
-                    g_warning (_("Unknown line ending detected (%s)"), line_term);
+                  /* set return value */
+                  retval = ERROR_CONVERTING_FAILED;
+
+                  goto failed;
                 }
 
-              /* make sure the string utf-8 valid */
-              if (G_UNLIKELY (g_utf8_validate (string, length, NULL) == FALSE))
+              /* set new values */
+              contents = encoded;
+              length = written;
+
+              /* validate the converted content */
+              goto validate;
+            }
+
+          /* detect the line ending, based on the first eol we match */
+          for (n = contents; n < end; n = g_utf8_next_char (n))
+            {
+              if (G_LIKELY (*n == '\n'))
                 {
-                  /* set an error */
-                  g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                               _("Converted string is not UTF-8 valid"));
+                  /* set unix line ending */
+                  file->line_ending = MOUSEPAD_EOL_UNIX;
 
-                  /* free string */
-                  g_free (string);
+                  break;
+                }
+              else if (*n == '\r')
+                {
+                  /* get next character */
+                  n = g_utf8_next_char (n);
 
-                  goto failed;
+                  /* set dos or mac line ending */
+                  file->line_ending = (*n == '\n') ? MOUSEPAD_EOL_DOS : MOUSEPAD_EOL_MAC;
+
+                  break;
                 }
+            }
 
-              /* insert the string in the buffer */
-              gtk_text_buffer_insert (file->buffer, &end, string, length);
+          /* get the iter at the beginning of the document */
+          gtk_text_buffer_get_start_iter (file->buffer, &start_iter);
 
-              /* cleanup */
-              g_free (string);
+          /* insert the file contents in the buffer (for documents with cr line ending) */
+          for (n = m = contents; n < end; n = g_utf8_next_char (n))
+            {
+              if (G_UNLIKELY (*n == '\r'))
+                {
+                  /* insert the text in the buffer */
+                  if (G_LIKELY (n - m > 0))
+                    gtk_text_buffer_insert (file->buffer, &start_iter, m, n - m);
+
+                  /* advance the offset */
+                  m = g_utf8_next_char (n);
+
+                  /* insert a new line when the document is not cr+lf */
+                  if (*m != '\n')
+                    gtk_text_buffer_insert (file->buffer, &start_iter, "\n", 1);
+                }
             }
 
-          /* break when we've reached the end of the file */
-          if (G_UNLIKELY (status == G_IO_STATUS_EOF))
-            break;
-        }
+          /* insert the remaining part, or everything for lf line ending */
+          if (G_LIKELY (n - m > 0))
+            gtk_text_buffer_insert (file->buffer, &start_iter, m, n - m);
 
-      /* done */
-      succeed = TRUE;
+          /* get the start iter */
+          gtk_text_buffer_get_start_iter (file->buffer, &start_iter);
 
-      /* get the start iter */
-      gtk_text_buffer_get_start_iter (file->buffer, &start);
+          /* set the cursor to the beginning of the document */
+          gtk_text_buffer_place_cursor (file->buffer, &start_iter);
+        }
 
-      /* set the cursor to the beginning of the document */
-      gtk_text_buffer_place_cursor (file->buffer, &start);
+      /* assume everything when file */
+      retval = 0;
 
-      /* get file status */
-      if (G_LIKELY (g_lstat (file->filename, &statb) == 0));
+      /* store the file status */
+      if (G_LIKELY (filename != template_filename))
         {
-          /* store the readonly mode */
-          mousepad_file_set_readonly (file, !((statb.st_mode & S_IWUSR) != 0));
+          if (G_LIKELY (g_stat (file->filename, &statb) == 0))
+            {
+              /* whether the file is readonly (ie. not writable by the user) */
+              mousepad_file_set_readonly (file, !((statb.st_mode & S_IWUSR) != 0));
 
-          /* store the file modification time */
-          file->mtime = statb.st_mtime;
+              /* store the file modification time */
+              file->mtime = statb.st_mtime;
+            }
+          else
+            {
+              /* set return value */
+              retval = ERROR_FILE_STATUS_FAILED;
+            }
+        }
+      else
+        {
+          /* this is a new document with content from a template */
+          file->mtime = 0;
+          mousepad_file_set_readonly (file, FALSE);
         }
 
       failed:
 
-      /* empty the buffer if we did not succeed */
-      if (G_UNLIKELY (succeed == FALSE))
+      /* make sure the buffer is empty if we did not succeed */
+      if (G_UNLIKELY (retval != 0))
         {
-          gtk_text_buffer_get_bounds (file->buffer, &start, &end);
-          gtk_text_buffer_delete (file->buffer, &start, &end);
+          gtk_text_buffer_get_bounds (file->buffer, &start_iter, &end_iter);
+          gtk_text_buffer_delete (file->buffer, &start_iter, &end_iter);
         }
 
-      /* this does not count as a modified buffer */
-      gtk_text_buffer_set_modified (file->buffer, FALSE);
-
-      /* thawn notifications */
-      g_object_thaw_notify (G_OBJECT (file->buffer));
+      /* cleanup */
+      g_free (encoded);
 
-      /* close the channel and flush the write buffer */
-      g_io_channel_shutdown (channel, TRUE, NULL);
+      /* close the mapped file */
+#if GLIB_CHECK_VERSION (2, 21, 0)
+      g_mapped_file_unref (mapped_file);
+#else
+      g_mapped_file_free (mapped_file);
+#endif
 
-      /* release the channel */
-      g_io_channel_unref (channel);
+      /* this does not count as a modified buffer */
+      gtk_text_buffer_set_modified (file->buffer, FALSE);
     }
 
-  return succeed;
+  return retval;
 }
 
 
@@ -443,99 +617,155 @@ gboolean
 mousepad_file_save (MousepadFile  *file,
                     GError       **error)
 {
-  GIOChannel  *channel;
-  GIOStatus    status;
-  gboolean     succeed = FALSE;
-  gchar       *slice;
-  const gchar *line_term;
-  GtkTextIter  start, end;
-  struct stat  statb;
-
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-  _mousepad_return_val_if_fail (file->filename != NULL, FALSE);
-
-  /* open the channel. the file is created or truncated */
-  channel = g_io_channel_new_file (file->filename, "w", error);
-
-  if (G_LIKELY (channel))
+  gint          fd;
+  gboolean      succeed = FALSE;
+  gchar        *contents, *p;
+  gchar        *encoded;
+  const gchar  *charset;
+  GtkTextIter   start_iter, end_iter;
+  gint          l, m, length;
+  gsize         written;
+  struct stat   statb;
+  gchar       **chunks;
+
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (file->filename != NULL, FALSE);
+
+  /* open the file */
+  fd = g_open (file->filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+  if (G_LIKELY (fd != -1))
     {
-      /* set the encoding of the channel */
-      if (file->encoding != NULL)
+      /* get the buffer bounds */
+      gtk_text_buffer_get_bounds (file->buffer, &start_iter, &end_iter);
+
+      /* get the buffer contents */
+      contents = gtk_text_buffer_get_slice (file->buffer, &start_iter, &end_iter, TRUE);
+
+      if (G_LIKELY (contents))
         {
-          /* set the encoding */
-          status = g_io_channel_set_encoding (channel, file->encoding, error);
+          /* get the content length */
+          length = strlen (contents);
 
-          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
-            goto failed;
-        }
+          /* handle line endings */
+          if (file->line_ending == MOUSEPAD_EOL_MAC)
+            {
+              /* replace the unix with a mac line ending */
+              for (p = contents; *p != '\0'; p++)
+                if (G_UNLIKELY (*p == '\n'))
+                  *p = '\r';
+            }
+          else if (file->line_ending == MOUSEPAD_EOL_DOS)
+            {
+              /* split the contents into chunks */
+              chunks = g_strsplit (contents, "\n", -1);
 
-      /* get the line ending characters we're going to use */
-      line_term = MOUSEPAD_GET_LINE_END_CHARS (file->line_ending);
+              /* cleanup */
+              g_free (contents);
 
-      /* get the start iter of the buffer */
-      gtk_text_buffer_get_start_iter (file->buffer, &start);
+              /* join the chunks with dos line endings in between */
+              contents = g_strjoinv ("\r\n", chunks);
 
-      /* walk the buffer */
-      do
-        {
-          /* set the end iter */
-          end = start;
+              /* cleanup */
+              g_strfreev (chunks);
 
-          /* insert the text if this line is not empty */
-          if (gtk_text_iter_ends_line (&start) == FALSE)
+              /* new contents length */
+              length = strlen (contents);
+            }
+
+          /* add and utf-8 bom at the start of the contents if needed */
+          if (file->write_bom && mousepad_encoding_is_unicode (file->encoding))
             {
-              /* move to the end of the line */
-              gtk_text_iter_forward_to_line_end (&end);
+              /* realloc the contents string */
+              contents = g_realloc (contents, length + 4);
 
-              /* get the test slice */
-              slice = gtk_text_buffer_get_slice (file->buffer, &start, &end, TRUE);
+              /* move the existing contents 3 bytes */
+              g_memmove (contents + 3, contents, length + 1);
 
-              /* write the file content */
-              status = g_io_channel_write_chars (channel, slice, -1, NULL, error);
+              /* write an utf-8 bom at the start of the contents */
+              contents[0] = (guchar) 0xef;
+              contents[1] = (guchar) 0xbb;
+              contents[2] = (guchar) 0xbf;
 
-              /* cleanup */
-              g_free (slice);
+              /* increase the length */
+              length += 3;
+            }
+
+          /* convert to the encoding if set */
+          if (G_UNLIKELY (file->encoding != MOUSEPAD_ENCODING_UTF_8))
+            {
+              /* get the charset */
+              charset = mousepad_encoding_get_charset (file->encoding);
+              if (G_UNLIKELY (charset == NULL))
+                goto failed;
+
+              /* convert the content to the user encoding */
+              encoded = g_convert (contents, length, charset, "UTF-8", NULL, &written, error);
 
-              /* leave when an error occured */
-              if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
+              /* return if nothing was encoded */
+              if (G_UNLIKELY (encoded == NULL))
                 goto failed;
+
+              /* cleanup */
+              g_free (contents);
+
+              /* set the new contents */
+              contents = encoded;
+              length = written;
             }
 
-          /* insert a new line if we haven't reached the end of the buffer */
-          if (gtk_text_iter_is_end (&end) == FALSE)
+          /* write the buffer to the file */
+          for (m = 0; m < length;)
             {
-              /* insert the new line */
-              status = g_io_channel_write_chars (channel, line_term, -1, NULL, error);
+              /* write */
+              l = write (fd, contents + m, length - m);
 
-              /* leave when an error occured */
-              if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
-                goto failed;
+              if (G_UNLIKELY (l < 0))
+                {
+                  /* just try again on EAGAIN/EINTR */
+                  if (G_LIKELY (errno != EAGAIN && errno != EINTR))
+                    {
+                      /* set an error */
+                      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), "%s", g_strerror (errno));
+
+                      /* bail out */
+                      goto failed;
+                    }
+                }
+              else
+                {
+                  /* advance the offset */
+                  m += l;
+                }
             }
-        }
-      while (gtk_text_iter_forward_line (&start));
 
-      /* everything seems to be ok */
-      succeed = TRUE;
+          /* set the new modification time */
+          if (G_LIKELY (fstat (fd, &statb) == 0))
+            file->mtime = statb.st_mtime;
 
-      /* set the new modification time */
-      if (G_LIKELY (g_lstat (file->filename, &statb) == 0))
-        file->mtime = statb.st_mtime;
+          /* everything has been saved */
+          gtk_text_buffer_set_modified (file->buffer, FALSE);
 
-      /* everything has been saved */
-      gtk_text_buffer_set_modified (file->buffer, FALSE);
+          /* we saved succesfully */
+          mousepad_file_set_readonly (file, FALSE);
 
-      /* we saved succesfully */
-      mousepad_file_set_readonly (file, FALSE);
+          /* everything went file */
+          succeed = TRUE;
 
-      failed:
+          failed:
 
-      /* close the channel and flush the write buffer */
-      g_io_channel_shutdown (channel, TRUE, error);
+          /* cleanup */
+          g_free (contents);
 
-      /* release the channel */
-      g_io_channel_unref (channel);
+          /* close the file */
+          close (fd);
+        }
+    }
+  else
+    {
+      /* set an error */
+      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), "%s", g_strerror (errno));
     }
 
   return succeed;
@@ -550,10 +780,10 @@ mousepad_file_reload (MousepadFile  *file,
   GtkTextIter start, end;
   gboolean    succeed = FALSE;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-  _mousepad_return_val_if_fail (file->filename != NULL, FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), FALSE);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (file->filename != NULL, FALSE);
 
   /* simple test if the file has not been removed */
   if (G_UNLIKELY (g_file_test (file->filename, G_FILE_TEST_EXISTS) == FALSE))
@@ -570,7 +800,7 @@ mousepad_file_reload (MousepadFile  *file,
   gtk_text_buffer_delete (file->buffer, &start, &end);
 
   /* reload the file */
-  succeed = mousepad_file_open (file, error);
+  succeed = mousepad_file_open (file, NULL, error);
 
   return succeed;
 }
@@ -584,17 +814,17 @@ mousepad_file_get_externally_modified (MousepadFile  *file,
   struct stat statb;
   GFileError  error_code;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), TRUE);
-  _mousepad_return_val_if_fail (file->filename != NULL, TRUE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), TRUE);
+  mousepad_return_val_if_fail (file->filename != NULL, TRUE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   /* check if our modification time differs from the current one */
-  if (G_LIKELY (g_lstat (file->filename, &statb) == 0))
+  if (G_LIKELY (g_stat (file->filename, &statb) == 0))
     return (file->mtime > 0 && statb.st_mtime != file->mtime);
-    
+
   /* get the error code */
   error_code = g_file_error_from_errno (errno);
-  
+
   /* file does not exists, nothing wrong with that */
   if (G_LIKELY (error_code == G_FILE_ERROR_NOENT))
     return FALSE;
@@ -605,80 +835,3 @@ mousepad_file_get_externally_modified (MousepadFile  *file,
 
   return TRUE;
 }
-
-
-
-gint
-mousepad_file_test_encoding (MousepadFile  *file,
-                             const gchar   *encoding,
-                             GError       **error)
-{
-  GIOChannel  *channel;
-  GIOStatus    status;
-  gunichar     c;
-  gint         unprintable = -1;
-  gint         counter = 0;
-
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_FILE (file), -1);
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (file->buffer), -1);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, -1);
-  _mousepad_return_val_if_fail (file->filename != NULL, -1);
-  _mousepad_return_val_if_fail (encoding != NULL, -1);
-
-  /* check if the file exists */
-  if (g_file_test (file->filename, G_FILE_TEST_EXISTS) == FALSE)
-    return -1;
-
-  /* open the channel */
-  channel = g_io_channel_new_file (file->filename, "r", error);
-
-  if (G_LIKELY (channel))
-    {
-      /* set the encoding of the channel */
-      status = g_io_channel_set_encoding (channel, encoding, error);
-
-      if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
-        goto failed;
-
-      /* set the counter */
-      unprintable = 0;
-
-      /* read the characters in the file and break on large files */
-      while (G_UNLIKELY (counter < MAX_ENCODING_CHECK_CHARS))
-        {
-          /* read a character */
-          status = g_io_channel_read_unichar (channel, &c, error);
-
-          /* leave on problems */
-          if (G_UNLIKELY (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_EOF))
-            {
-              /* reset counter */
-              unprintable = -1;
-
-              goto failed;
-            }
-
-          /* check if the character is unprintable, but not a soft hyphen */
-          if (G_UNLIKELY (g_unichar_isprint (c) == FALSE
-                          && g_unichar_break_type (c) != G_UNICODE_BREAK_AFTER))
-            unprintable++;
-
-          /* break when we've reached the end of the file */
-          if (G_UNLIKELY (status == G_IO_STATUS_EOF))
-            break;
-
-          /* increase counter */
-          counter++;
-        }
-
-      failed:
-
-      /* close the channel and flush the write buffer */
-      g_io_channel_shutdown (channel, TRUE, NULL);
-
-      /* release the channel */
-      g_io_channel_unref (channel);
-    }
-
-  return unprintable;
-}
diff --git a/mousepad/mousepad-file.h b/mousepad/mousepad-file.h
index 0f65a7f..18c5b6e 100644
--- a/mousepad/mousepad-file.h
+++ b/mousepad/mousepad-file.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -20,6 +19,8 @@
 
 G_BEGIN_DECLS
 
+#include <mousepad/mousepad-encoding.h>
+
 typedef struct _MousepadFileClass  MousepadFileClass;
 typedef struct _MousepadFile       MousepadFile;
 typedef enum   _MousepadLineEnding MousepadLineEnding;
@@ -33,10 +34,9 @@ typedef enum   _MousepadLineEnding MousepadLineEnding;
 
 enum _MousepadLineEnding
 {
-  MOUSEPAD_LINE_END_NONE,
-  MOUSEPAD_LINE_END_UNIX,
-  MOUSEPAD_LINE_END_MAC,
-  MOUSEPAD_LINE_END_DOS
+  MOUSEPAD_EOL_UNIX,
+  MOUSEPAD_EOL_MAC,
+  MOUSEPAD_EOL_DOS
 };
 
 GType               mousepad_file_get_type                 (void) G_GNUC_CONST;
@@ -51,18 +51,25 @@ const gchar        *mousepad_file_get_filename             (MousepadFile
 gchar              *mousepad_file_get_uri                  (MousepadFile        *file);
 
 void                mousepad_file_set_encoding             (MousepadFile        *file,
-                                                            const gchar         *encoding);
+                                                            MousepadEncoding     encoding);
 
-const gchar        *mousepad_file_get_encoding             (MousepadFile        *file);
+MousepadEncoding    mousepad_file_get_encoding             (MousepadFile        *file);
 
 gboolean            mousepad_file_get_read_only            (MousepadFile        *file);
 
+void                mousepad_file_set_write_bom            (MousepadFile        *file,
+                                                            gboolean             write_bom);
+
+gboolean            mousepad_file_get_write_bom            (MousepadFile        *file,
+                                                            gboolean            *sensitive);
+
 void                mousepad_file_set_line_ending          (MousepadFile        *file,
                                                             MousepadLineEnding   line_ending);
 
 MousepadLineEnding  mousepad_file_get_line_ending          (MousepadFile        *file);
 
-gboolean            mousepad_file_open                     (MousepadFile        *file,
+gint                mousepad_file_open                     (MousepadFile        *file,
+                                                            const gchar         *template_filename,
                                                             GError             **error);
 
 gboolean            mousepad_file_save                     (MousepadFile        *file,
@@ -73,11 +80,6 @@ gboolean            mousepad_file_reload                   (MousepadFile
 
 gboolean            mousepad_file_get_externally_modified  (MousepadFile        *file,
                                                             GError             **error);
-
-gint                mousepad_file_test_encoding            (MousepadFile        *file,
-                                                            const gchar         *encoding,
-                                                            GError             **error);
-
 G_END_DECLS
 
 #endif /* !__MOUSEPAD_FILE_H__ */
diff --git a/mousepad/mousepad-preferences.c b/mousepad/mousepad-preferences.c
index a6917b1..cbf1ec1 100644
--- a/mousepad/mousepad-preferences.c
+++ b/mousepad/mousepad-preferences.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -75,13 +74,11 @@ enum
   PROP_MISC_PATH_IN_TITLE,
   PROP_MISC_RECENT_MENU_ITEMS,
   PROP_MISC_REMEMBER_GEOMETRY,
-  N_PROPERTIES,
+  N_PROPERTIES
 };
 
 
 
-static void     mousepad_preferences_class_init         (MousepadPreferencesClass *klass);
-static void     mousepad_preferences_init               (MousepadPreferences      *preferences);
 static void     mousepad_preferences_finalize           (GObject                  *object);
 static void     mousepad_preferences_get_property       (GObject                  *object,
                                                          guint                     prop_id,
@@ -286,7 +283,7 @@ mousepad_preferences_class_init (MousepadPreferencesClass *klass)
                                    g_param_spec_int ("misc-recent-menu-items",
                                                      "MiscRecentMenuItems",
                                                      NULL,
-                                                     1, G_MAXINT, 10,
+                                                     1, 100, 10,
                                                      MOUSEPAD_PARAM_READWRITE));
 
   g_object_class_install_property (gobject_class,
diff --git a/mousepad/mousepad-preferences.h b/mousepad/mousepad-preferences.h
index d3161a0..31302b7 100644
--- a/mousepad/mousepad-preferences.h
+++ b/mousepad/mousepad-preferences.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-print.c b/mousepad/mousepad-print.c
index a86d465..04aa284 100644
--- a/mousepad/mousepad-print.c
+++ b/mousepad/mousepad-print.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -32,8 +31,6 @@
 
 
 
-static void           mousepad_print_class_init            (MousepadPrintClass      *klass);
-static void           mousepad_print_init                  (MousepadPrint           *print);
 static void           mousepad_print_finalize              (GObject                 *object);
 static void           mousepad_print_settings_load         (GtkPrintOperation       *operation);
 static void           mousepad_print_settings_save_foreach (const gchar             *key,
@@ -171,8 +168,8 @@ mousepad_print_settings_load (GtkPrintOperation *operation)
   PangoContext          *context;
   PangoFontDescription  *font_desc;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (print->document));
-  _mousepad_return_if_fail (GTK_IS_WIDGET (print->document->textview));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (print->document));
+  mousepad_return_if_fail (GTK_IS_WIDGET (print->document->textview));
 
   /* get the config file filename */
   filename = mousepad_util_get_save_location (MOUSEPAD_RC_RELPATH, FALSE);
@@ -843,11 +840,11 @@ mousepad_print_document_interactive (MousepadPrint     *print,
 {
   GtkPrintOperationResult result;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_PRINT (print), FALSE);
-  _mousepad_return_val_if_fail (GTK_IS_PRINT_OPERATION (print), FALSE);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
-  _mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), FALSE);
-  _mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_PRINT (print), FALSE);
+  mousepad_return_val_if_fail (GTK_IS_PRINT_OPERATION (print), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+  mousepad_return_val_if_fail (GTK_IS_WINDOW (parent), FALSE);
+  mousepad_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   /* set the document */
   print->document = document;
diff --git a/mousepad/mousepad-print.h b/mousepad/mousepad-print.h
index 28011ae..3806d7b 100644
--- a/mousepad/mousepad-print.h
+++ b/mousepad/mousepad-print.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-private.h b/mousepad/mousepad-private.h
index f271186..4be50b1 100644
--- a/mousepad/mousepad-private.h
+++ b/mousepad/mousepad-private.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -25,6 +24,15 @@
 
 G_BEGIN_DECLS
 
+/* errors inside mousepad */
+enum
+{
+  ERROR_READING_FAILED     = -1,
+  ERROR_CONVERTING_FAILED  = -2,
+  ERROR_NOT_UTF8_VALID     = -3,
+  ERROR_FILE_STATUS_FAILED = -4
+};
+
 /* config file locations */
 #define MOUSEPAD_RC_RELPATH     ("Mousepad" G_DIR_SEPARATOR_S "mousepadrc")
 #define MOUSEPAD_ACCELS_RELPATH ("Mousepad" G_DIR_SEPARATOR_S "accels.scm")
@@ -45,24 +53,25 @@ G_BEGIN_DECLS
 
 /* support for canonical strings and quarks */
 #define I_(string)  (g_intern_static_string (string))
-#define QU_(string) (g_quark_from_static_string (string))
 
 /* convienient function for setting object data */
-#define mousepad_object_set_data(object,key,data)              (g_object_set_qdata ((object), QU_(key), (data)))
-#define mousepad_object_set_data_full(object,key,data,destroy) (g_object_set_qdata_full ((object), QU_(key), (data), (GDestroyNotify) (destroy)))
-#define mousepad_object_get_data(object,key)                   (g_object_get_qdata ((object), QU_(key)))
+#define mousepad_object_set_data(object,key,data)              (g_object_set_qdata ((object), \
+                                                                g_quark_from_static_string (key), (data)))
+#define mousepad_object_set_data_full(object,key,data,destroy) (g_object_set_qdata_full ((object), \
+                                                                g_quark_from_static_string (key), (data), (GDestroyNotify) (destroy)))
+#define mousepad_object_get_data(object,key)                   (g_object_get_qdata ((object), g_quark_try_string (key)))
 
 /* support macros for debugging */
 #ifndef NDEBUG
-#define _mousepad_assert(expr)                  g_assert (expr)
-#define _mousepad_assert_not_reached()          g_assert_not_reached ()
-#define _mousepad_return_if_fail(expr)          g_return_if_fail (expr)
-#define _mousepad_return_val_if_fail(expr, val) g_return_val_if_fail (expr, (val))
+#define mousepad_assert(expr)                  g_assert (expr)
+#define mousepad_assert_not_reached()          g_assert_not_reached ()
+#define mousepad_return_if_fail(expr)          g_return_if_fail (expr)
+#define mousepad_return_val_if_fail(expr, val) g_return_val_if_fail (expr, (val))
 #else
-#define _mousepad_assert(expr)                  G_STMT_START{ (void)0; }G_STMT_END
-#define _mousepad_assert_not_reached()          G_STMT_START{ (void)0; }G_STMT_END
-#define _mousepad_return_if_fail(expr)          G_STMT_START{ (void)0; }G_STMT_END
-#define _mousepad_return_val_if_fail(expr, val) G_STMT_START{ (void)0; }G_STMT_END
+#define mousepad_assert(expr)                  G_STMT_START{ (void)0; }G_STMT_END
+#define mousepad_assert_not_reached()          G_STMT_START{ (void)0; }G_STMT_END
+#define mousepad_return_if_fail(expr)          G_STMT_START{ (void)0; }G_STMT_END
+#define mousepad_return_val_if_fail(expr, val) G_STMT_START{ (void)0; }G_STMT_END
 #endif
 
 /* avoid trivial g_value_get_*() function calls */
diff --git a/mousepad/mousepad-replace-dialog.c b/mousepad/mousepad-replace-dialog.c
index aa6bb41..a3a171e 100644
--- a/mousepad/mousepad-replace-dialog.c
+++ b/mousepad/mousepad-replace-dialog.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -33,8 +32,6 @@
 
 
 
-static void                 mousepad_replace_dialog_class_init              (MousepadReplaceDialogClass *klass);
-static void                 mousepad_replace_dialog_init                    (MousepadReplaceDialog      *dialog);
 static void                 mousepad_replace_dialog_unrealize               (GtkWidget                  *widget);
 static void                 mousepad_replace_dialog_finalize                (GObject                    *object);
 static void                 mousepad_replace_dialog_response                (GtkWidget                  *widget,
@@ -299,8 +296,8 @@ mousepad_replace_dialog_unrealize (GtkWidget *widget)
   MousepadReplaceDialog *dialog = MOUSEPAD_REPLACE_DIALOG (widget);
   const gchar           *text;
 
-  _mousepad_return_if_fail (GTK_IS_ENTRY (dialog->replace_entry));
-  _mousepad_return_if_fail (GTK_IS_ENTRY (dialog->search_entry));
+  mousepad_return_if_fail (GTK_IS_ENTRY (dialog->replace_entry));
+  mousepad_return_if_fail (GTK_IS_ENTRY (dialog->search_entry));
 
   text = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
   mousepad_replace_dialog_history_insert_text (text);
@@ -588,7 +585,7 @@ mousepad_replace_dialog_history_combo_box (GtkComboBox *combo_box)
 {
   GSList *li;
 
-  _mousepad_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
+  mousepad_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
 
   /* append the items from the history to the combobox */
   for (li = history_list; li != NULL; li = li->next)
diff --git a/mousepad/mousepad-replace-dialog.h b/mousepad/mousepad-replace-dialog.h
index b5cb629..b3e7d2f 100644
--- a/mousepad/mousepad-replace-dialog.h
+++ b/mousepad/mousepad-replace-dialog.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-search-bar.c b/mousepad/mousepad-search-bar.c
index 1b8ee15..4158d02 100644
--- a/mousepad/mousepad-search-bar.c
+++ b/mousepad/mousepad-search-bar.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -37,8 +36,6 @@
 
 
 
-static void      mousepad_search_bar_class_init                 (MousepadSearchBarClass  *klass);
-static void      mousepad_search_bar_init                       (MousepadSearchBar       *bar);
 static void      mousepad_search_bar_finalize                   (GObject                 *object);
 static void      mousepad_search_bar_find_string                (MousepadSearchBar       *bar,
                                                                  MousepadSearchFlags   flags);
@@ -61,7 +58,7 @@ enum
 {
   HIDE_BAR,
   SEARCH,
-  LAST_SIGNAL,
+  LAST_SIGNAL
 };
 
 struct _MousepadSearchBarClass
@@ -319,7 +316,7 @@ mousepad_search_bar_find_string (MousepadSearchBar   *bar,
 static void
 mousepad_search_bar_hide_clicked (MousepadSearchBar *bar)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* emit the signal */
   g_signal_emit (G_OBJECT (bar), search_bar_signals[HIDE_BAR], 0);
@@ -352,7 +349,7 @@ mousepad_search_bar_highlight_toggled (GtkWidget         *button,
 {
   MousepadSearchFlags flags;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* set the new state */
   bar->highlight_all = gtk_toggle_tool_button_get_active (GTK_TOGGLE_TOOL_BUTTON (button));
@@ -385,7 +382,7 @@ mousepad_search_bar_match_case_toggled (GtkWidget         *button,
 {
   gboolean active;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* get the state of the toggle button */
   active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
@@ -414,8 +411,8 @@ mousepad_search_bar_menuitem_toggled (GtkCheckMenuItem *item,
 {
   gboolean active;
 
-  _mousepad_return_if_fail (GTK_IS_CHECK_MENU_ITEM (item));
-  _mousepad_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
+  mousepad_return_if_fail (GTK_IS_CHECK_MENU_ITEM (item));
+  mousepad_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
 
   /* toggle the menubar item, he/she will send the signal */
   active = gtk_check_menu_item_get_active (item);
@@ -427,7 +424,7 @@ mousepad_search_bar_menuitem_toggled (GtkCheckMenuItem *item,
 static void
 mousepad_search_bar_highlight_schedule (MousepadSearchBar *bar)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* stop a pending timeout */
   if (bar->highlight_id != 0)
@@ -489,7 +486,7 @@ mousepad_search_bar_entry (MousepadSearchBar *bar)
 void
 mousepad_search_bar_focus (MousepadSearchBar *bar)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* focus the entry field */
   gtk_widget_grab_focus (bar->entry);
@@ -499,7 +496,7 @@ mousepad_search_bar_focus (MousepadSearchBar *bar)
 
   /* update the highlight */
   mousepad_search_bar_highlight_schedule (bar);
-  
+
   /* select the entire entry */
   gtk_editable_select_region (GTK_EDITABLE (bar->entry), 0, -1);
 }
@@ -511,7 +508,7 @@ mousepad_search_bar_find_next (MousepadSearchBar *bar)
 {
   MousepadSearchFlags flags;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* set search flags */
   flags = MOUSEPAD_SEARCH_FLAGS_ITER_SEL_END
@@ -528,7 +525,7 @@ mousepad_search_bar_find_previous (MousepadSearchBar *bar)
 {
   MousepadSearchFlags flags;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (bar));
 
   /* set search flags */
   flags = MOUSEPAD_SEARCH_FLAGS_ITER_SEL_START
diff --git a/mousepad/mousepad-search-bar.h b/mousepad/mousepad-search-bar.h
index 8d80096..4dbfe11 100644
--- a/mousepad/mousepad-search-bar.h
+++ b/mousepad/mousepad-search-bar.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-statusbar.c b/mousepad/mousepad-statusbar.c
index 712bdd1..0321c3e 100644
--- a/mousepad/mousepad-statusbar.c
+++ b/mousepad/mousepad-statusbar.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -26,8 +25,6 @@
 
 
 
-static void              mousepad_statusbar_class_init                (MousepadStatusbarClass *klass);
-static void              mousepad_statusbar_init                      (MousepadStatusbar      *statusbar);
 static gboolean          mousepad_statusbar_overwrite_clicked         (GtkWidget              *widget,
                                                                        GdkEventButton         *event,
                                                                        MousepadStatusbar      *statusbar);
@@ -149,7 +146,7 @@ mousepad_statusbar_overwrite_clicked (GtkWidget         *widget,
                                       GdkEventButton    *event,
                                       MousepadStatusbar *statusbar)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar), FALSE);
 
   /* only respond on the left button click */
   if (event->type != GDK_BUTTON_PRESS || event->button != 1)
@@ -174,7 +171,7 @@ mousepad_statusbar_set_cursor_position (MousepadStatusbar *statusbar,
 {
   gchar string[64];
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar));
+  mousepad_return_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar));
 
   /* create printable string */
   if (G_UNLIKELY (selection > 0))
@@ -192,7 +189,7 @@ void
 mousepad_statusbar_set_overwrite (MousepadStatusbar *statusbar,
                                   gboolean           overwrite)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar));
+  mousepad_return_if_fail (MOUSEPAD_IS_STATUSBAR (statusbar));
 
   gtk_widget_set_sensitive (statusbar->overwrite, overwrite);
 
diff --git a/mousepad/mousepad-statusbar.h b/mousepad/mousepad-statusbar.h
index 28c7c59..c44b54e 100644
--- a/mousepad/mousepad-statusbar.h
+++ b/mousepad/mousepad-statusbar.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-undo.c b/mousepad/mousepad-undo.c
index 5b28e3b..ecea5cd 100644
--- a/mousepad/mousepad-undo.c
+++ b/mousepad/mousepad-undo.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -41,8 +40,6 @@ typedef enum   _MousepadUndoAction MousepadUndoAction;
 
 
 
-static void mousepad_undo_class_init               (MousepadUndoClass  *klass);
-static void mousepad_undo_init                     (MousepadUndo       *undo);
 static void mousepad_undo_finalize                 (GObject            *object);
 static void mousepad_undo_emit_signals             (MousepadUndo       *undo);
 static void mousepad_undo_step_free                (MousepadUndoStep   *step);
@@ -312,7 +309,7 @@ mousepad_undo_step (MousepadUndo *undo,
         {
           case INSERT:
             /* debug check */
-            _mousepad_return_if_fail (step->data == NULL);
+            mousepad_return_if_fail (step->data == NULL);
 
             /* get the end iter */
             gtk_text_buffer_get_iter_at_offset (undo->buffer, &end_iter, step->end);
@@ -326,7 +323,7 @@ mousepad_undo_step (MousepadUndo *undo,
 
           case DELETE:
             /* debug check */
-            _mousepad_return_if_fail (step->data != NULL);
+            mousepad_return_if_fail (step->data != NULL);
 
             /* insert the deleted text */
             gtk_text_buffer_insert (undo->buffer, &start_iter, step->data, -1);
@@ -337,7 +334,7 @@ mousepad_undo_step (MousepadUndo *undo,
             break;
 
           default:
-            _mousepad_assert_not_reached ();
+            mousepad_assert_not_reached ();
             break;
         }
 
@@ -383,7 +380,7 @@ mousepad_undo_clear_oldest_step (MousepadUndo *undo)
   MousepadUndoStep *step;
   gint              to_remove;
 
-  _mousepad_return_if_fail (undo->n_steps > MOUSEPAD_UNDO_MAX_STEPS);
+  mousepad_return_if_fail (undo->n_steps > MOUSEPAD_UNDO_MAX_STEPS);
 
   /* number of steps to remove */
   to_remove = undo->n_steps - MOUSEPAD_UNDO_MAX_STEPS;
@@ -420,7 +417,7 @@ mousepad_undo_clear_oldest_step (MousepadUndo *undo)
 static void
 mousepad_undo_cache_reset (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (undo->cache == NULL);
+  mousepad_return_if_fail (undo->cache == NULL);
 
   /* reset variables */
   undo->cache_start = undo->cache_end = -1;
@@ -440,7 +437,7 @@ mousepad_undo_cache_to_step (MousepadUndo *undo)
   if (G_LIKELY (undo->cache_start != undo->cache_end))
     {
       /* make sure the needle has been reset */
-      _mousepad_return_if_fail (undo->needle == undo->steps);
+      mousepad_return_if_fail (undo->needle == undo->steps);
 
       /* allocate slice */
       step = g_slice_new0 (MousepadUndoStep);
@@ -502,7 +499,7 @@ mousepad_undo_needle_reset (MousepadUndo *undo)
     }
 
   /* debug check */
-  _mousepad_return_if_fail (undo->needle == undo->steps);
+  mousepad_return_if_fail (undo->needle == undo->steps);
 }
 
 
@@ -630,8 +627,8 @@ mousepad_undo_buffer_insert (GtkTextBuffer *buffer,
 {
   gint start_pos, end_pos;
 
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  _mousepad_return_if_fail (buffer == undo->buffer);
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (buffer == undo->buffer);
 
   /* leave when locked */
   if (G_LIKELY (undo->locked == 0))
@@ -657,8 +654,8 @@ mousepad_undo_buffer_delete (GtkTextBuffer *buffer,
   gchar *text;
   gint   start_pos, end_pos;
 
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  _mousepad_return_if_fail (buffer == undo->buffer);
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (buffer == undo->buffer);
 
   /* no nothing when locked */
   if (G_LIKELY (undo->locked == 0))
@@ -686,8 +683,8 @@ static void
 mousepad_undo_buffer_begin_user_action (GtkTextBuffer *buffer,
                                         MousepadUndo  *undo)
 {
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  _mousepad_return_if_fail (buffer == undo->buffer);
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+  mousepad_return_if_fail (buffer == undo->buffer);
 
   /* only reset the group counter when not locked */
   if (G_LIKELY (undo->locked == 0))
@@ -704,7 +701,7 @@ mousepad_undo_new (GtkTextBuffer *buffer)
 {
   MousepadUndo *undo;
 
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
 
   /* create the undo object */
   undo = g_object_new (MOUSEPAD_TYPE_UNDO, NULL);
@@ -727,7 +724,7 @@ mousepad_undo_clear (MousepadUndo *undo)
 {
   GList *li;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
 
   /* lock to avoid updates */
   mousepad_undo_lock (undo);
@@ -758,7 +755,7 @@ mousepad_undo_clear (MousepadUndo *undo)
 void
 mousepad_undo_lock (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
 
   /* increase the lock count */
   undo->locked++;
@@ -769,8 +766,8 @@ mousepad_undo_lock (MousepadUndo *undo)
 void
 mousepad_undo_unlock (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  _mousepad_return_if_fail (undo->locked > 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (undo->locked > 0);
 
   /* decrease the lock count */
   undo->locked--;
@@ -781,7 +778,7 @@ mousepad_undo_unlock (MousepadUndo *undo)
 void
 mousepad_undo_save_point (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
 
   /* reset the needle */
   mousepad_undo_needle_reset (undo);
@@ -798,7 +795,7 @@ mousepad_undo_save_point (MousepadUndo *undo)
 gboolean
 mousepad_undo_can_undo (MousepadUndo *undo)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
 
   return undo->can_undo;
 }
@@ -808,7 +805,7 @@ mousepad_undo_can_undo (MousepadUndo *undo)
 gboolean
 mousepad_undo_can_redo (MousepadUndo *undo)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
 
   return undo->can_redo;
 }
@@ -818,8 +815,8 @@ mousepad_undo_can_redo (MousepadUndo *undo)
 void
 mousepad_undo_do_undo (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  _mousepad_return_if_fail (mousepad_undo_can_undo (undo));
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (mousepad_undo_can_undo (undo));
 
   /* undo the last step */
   mousepad_undo_step (undo, FALSE);
@@ -830,8 +827,8 @@ mousepad_undo_do_undo (MousepadUndo *undo)
 void
 mousepad_undo_do_redo (MousepadUndo *undo)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  _mousepad_return_if_fail (mousepad_undo_can_redo (undo));
+  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
+  mousepad_return_if_fail (mousepad_undo_can_redo (undo));
 
   /* redo the last undo-ed step */
   mousepad_undo_step (undo, TRUE);
diff --git a/mousepad/mousepad-undo.h b/mousepad/mousepad-undo.h
index 9a92d90..b096ef5 100644
--- a/mousepad/mousepad-undo.h
+++ b/mousepad/mousepad-undo.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-util.c b/mousepad/mousepad-util.c
index 16ff0dd..f9abdf6 100644
--- a/mousepad/mousepad-util.c
+++ b/mousepad/mousepad-util.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -28,7 +27,7 @@
 
 
 
-static inline gboolean
+static gboolean
 mousepad_util_iter_word_characters (const GtkTextIter *iter)
 {
   gunichar c;
@@ -150,7 +149,7 @@ mousepad_util_iter_backward_word_start (GtkTextIter *iter)
 gboolean
 mousepad_util_iter_forward_text_start (GtkTextIter *iter)
 {
-  _mousepad_return_val_if_fail (!mousepad_util_iter_inside_word (iter), FALSE);
+  mousepad_return_val_if_fail (!mousepad_util_iter_inside_word (iter), FALSE);
 
   /* keep until we hit text or a line end */
   while (g_unichar_isspace (gtk_text_iter_get_char (iter)))
@@ -167,7 +166,7 @@ mousepad_util_iter_backward_text_start (GtkTextIter *iter)
 {
   GtkTextIter prev = *iter;
 
-  _mousepad_return_val_if_fail (!mousepad_util_iter_inside_word (iter), FALSE);
+  mousepad_return_val_if_fail (!mousepad_util_iter_inside_word (iter), FALSE);
 
   while (!gtk_text_iter_starts_line (&prev) && gtk_text_iter_backward_char (&prev))
     {
@@ -253,7 +252,7 @@ mousepad_util_utf8_strcapital (const gchar *str)
   GString     *result;
   gboolean     upper = TRUE;
 
-  _mousepad_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);
+  mousepad_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);
 
   /* create a new string */
   result = g_string_sized_new (strlen (str));
@@ -308,7 +307,7 @@ mousepad_util_utf8_stropposite (const gchar *str)
   gchar       *buf;
   GString     *result;
 
-  _mousepad_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);
+  mousepad_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);
 
   /* create a new string */
   result = g_string_sized_new (strlen (str));
@@ -394,7 +393,7 @@ mousepad_util_entry_error (GtkWidget *widget,
   const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
   gpointer       pointer;
 
-  _mousepad_return_if_fail (GTK_IS_WIDGET (widget));
+  mousepad_return_if_fail (GTK_IS_WIDGET (widget));
 
   /* get the current error state */
   pointer = mousepad_object_get_data (G_OBJECT (widget), "error-state");
@@ -480,8 +479,8 @@ mousepad_util_set_tooltip (GtkWidget   *widget,
 {
   static GtkTooltips *tooltips = NULL;
 
-  _mousepad_return_if_fail (GTK_IS_WIDGET (widget));
-  _mousepad_return_if_fail (string ? g_utf8_validate (string, -1, NULL) : TRUE);
+  mousepad_return_if_fail (GTK_IS_WIDGET (widget));
+  mousepad_return_if_fail (string ? g_utf8_validate (string, -1, NULL) : TRUE);
 
   /* allocate the shared tooltips on-demand */
   if (G_UNLIKELY (tooltips == NULL))
@@ -553,13 +552,13 @@ mousepad_util_get_save_location (const gchar *relpath,
 {
   gchar *filename, *dirname;
 
-  _mousepad_return_val_if_fail (g_get_user_config_dir () != NULL, NULL);
+  mousepad_return_val_if_fail (g_get_user_config_dir () != NULL, NULL);
 
   /* create the full filename */
   filename = g_build_filename (g_get_user_config_dir (), relpath, NULL);
 
   /* test if the file exists */
-  if (G_UNLIKELY (g_file_test (filename, G_FILE_TEST_IS_REGULAR) == FALSE))
+  if (G_UNLIKELY (g_file_test (filename, G_FILE_TEST_EXISTS) == FALSE))
     {
       if (create_parents)
         {
@@ -631,18 +630,18 @@ mousepad_util_save_key_file (GKeyFile    *keyfile,
 GType
 mousepad_util_search_flags_get_type (void)
 {
-	static GType type = G_TYPE_NONE;
+  static GType type = G_TYPE_NONE;
 
-	if (G_UNLIKELY (type == G_TYPE_NONE))
-	  {
-	    /* use empty values table */
-	    static const GFlagsValue values[] =
-	    {
+  if (G_UNLIKELY (type == G_TYPE_NONE))
+    {
+      /* use empty values table */
+      static const GFlagsValue values[] =
+      {
           { 0, NULL, NULL }
-	    };
+      };
 
-	    /* register the type */
-	    type = g_flags_register_static (I_("MousepadSearchFlags"), values);
+      /* register the type */
+      type = g_flags_register_static (I_("MousepadSearchFlags"), values);
       }
 
   return type;
@@ -665,9 +664,9 @@ mousepad_util_search_iter (const GtkTextIter   *start,
   gboolean     match_case, search_backwards, whole_word;
   guint        needle_offset = 0;
 
-  _mousepad_return_val_if_fail (start != NULL, FALSE);
-  _mousepad_return_val_if_fail (string != NULL, FALSE);
-  _mousepad_return_val_if_fail (limit != NULL, FALSE);
+  mousepad_return_val_if_fail (start != NULL, FALSE);
+  mousepad_return_val_if_fail (string != NULL, FALSE);
+  mousepad_return_val_if_fail (limit != NULL, FALSE);
 
   /* search properties */
   match_case       = (flags & MOUSEPAD_SEARCH_FLAGS_MATCH_CASE) != 0;
@@ -809,12 +808,12 @@ mousepad_util_search_get_iters (GtkTextBuffer       *buffer,
       else if (flags & (MOUSEPAD_SEARCH_FLAGS_ITER_AREA_END | MOUSEPAD_SEARCH_FLAGS_ITER_SEL_END))
         *iter = sel_end;
       else
-        _mousepad_assert_not_reached ();
+        mousepad_assert_not_reached ();
     }
   else
     {
       /* this should never happen */
-      _mousepad_assert_not_reached ();
+      mousepad_assert_not_reached ();
     }
 
   /* invert the start and end iter on backwards searching */
@@ -840,10 +839,10 @@ mousepad_util_highlight (GtkTextBuffer       *buffer,
   gboolean    found, cached = FALSE;
   gint        counter = 0;
 
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), -1);
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_TAG (tag), -1);
-  _mousepad_return_val_if_fail (string == NULL || g_utf8_validate (string, -1, NULL), -1);
-  _mousepad_return_val_if_fail ((flags & MOUSEPAD_SEARCH_FLAGS_DIR_BACKWARD) == 0, -1);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), -1);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_TAG (tag), -1);
+  mousepad_return_val_if_fail (string == NULL || g_utf8_validate (string, -1, NULL), -1);
+  mousepad_return_val_if_fail ((flags & MOUSEPAD_SEARCH_FLAGS_DIR_BACKWARD) == 0, -1);
 
   /* get the buffer bounds */
   gtk_text_buffer_get_bounds (buffer, &start, &end);
@@ -926,10 +925,10 @@ mousepad_util_search (GtkTextBuffer       *buffer,
   GtkTextIter  match_start, match_end;
   GtkTextMark *mark_start, *mark_iter, *mark_end, *mark_replace;
 
-  _mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), -1);
-  _mousepad_return_val_if_fail (string && g_utf8_validate (string, -1, NULL), -1);
-  _mousepad_return_val_if_fail (replace == NULL || g_utf8_validate (replace, -1, NULL), -1);
-  _mousepad_return_val_if_fail ((flags & MOUSEPAD_SEARCH_FLAGS_ACTION_HIGHTLIGHT) == 0, -1);
+  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), -1);
+  mousepad_return_val_if_fail (string && g_utf8_validate (string, -1, NULL), -1);
+  mousepad_return_val_if_fail (replace == NULL || g_utf8_validate (replace, -1, NULL), -1);
+  mousepad_return_val_if_fail ((flags & MOUSEPAD_SEARCH_FLAGS_ACTION_HIGHTLIGHT) == 0, -1);
 
   /* freeze buffer notifications */
   g_object_freeze_notify (G_OBJECT (buffer));
@@ -976,7 +975,7 @@ mousepad_util_search (GtkTextBuffer       *buffer,
               gtk_text_buffer_select_range (buffer, &match_start, &match_end);
             }
           else if (flags & MOUSEPAD_SEARCH_FLAGS_ACTION_REPLACE)
-	        {
+          {
               /* create text mark */
               mark_replace = gtk_text_buffer_create_mark (buffer, NULL, &match_start, search_backwards);
 
@@ -1003,8 +1002,8 @@ mousepad_util_search (GtkTextBuffer       *buffer,
 
               /* search again */
               search_again = TRUE;
-	        }
-	      else if (flags & MOUSEPAD_SEARCH_FLAGS_ACTION_NONE)
+          }
+        else if (flags & MOUSEPAD_SEARCH_FLAGS_ACTION_NONE)
             {
               /* keep searching when requested */
               if (flags & MOUSEPAD_SEARCH_FLAGS_ENTIRE_AREA)
@@ -1013,10 +1012,10 @@ mousepad_util_search (GtkTextBuffer       *buffer,
               /* move iter */
               iter = match_end;
             }
-	      else
-	        {
+        else
+          {
               /* no valid action was defined */
-              _mousepad_assert_not_reached ();
+              mousepad_assert_not_reached ();
             }
 
           /* increase the counter */
diff --git a/mousepad/mousepad-util.h b/mousepad/mousepad-util.h
index 0fb9032..0aa6513 100644
--- a/mousepad/mousepad-util.h
+++ b/mousepad/mousepad-util.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-view.c b/mousepad/mousepad-view.c
index 99e3386..4e12fa6 100644
--- a/mousepad/mousepad-view.c
+++ b/mousepad/mousepad-view.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -37,8 +36,6 @@
 
 
 
-static void      mousepad_view_class_init                    (MousepadViewClass  *klass);
-static void      mousepad_view_init                          (MousepadView       *view);
 static void      mousepad_view_finalize                      (GObject            *object);
 static void      mousepad_view_style_set                     (GtkWidget          *widget,
                                                               GtkStyle           *previous_style);
@@ -568,14 +565,14 @@ mousepad_view_commit_handler (GtkIMContext *context,
                               const gchar  *str,
                               MousepadView *view)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (GTK_IS_IM_CONTEXT (context));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (GTK_IS_IM_CONTEXT (context));
 
   /* if there is a selection, insert this string there too */
   if (G_UNLIKELY (view->selection_marks != NULL))
     {
       /* debug check */
-      _mousepad_return_if_fail (gtk_text_view_get_editable (GTK_TEXT_VIEW (view)) == FALSE);
+      mousepad_return_if_fail (gtk_text_view_get_editable (GTK_TEXT_VIEW (view)) == FALSE);
 
       /* handle the text input for the multi selection */
       mousepad_view_selection_key_press_event (view, str, 0, 0);
@@ -720,11 +717,11 @@ mousepad_view_selection_key_press_event (MousepadView *view,
   GSList        *li;
   GtkTextBuffer *buffer;
 
-  _mousepad_return_if_fail (view->selection_marks != NULL);
-  _mousepad_return_if_fail (view->selection_start_x == -1);
-  _mousepad_return_if_fail (view->selection_end_x == -1);
-  _mousepad_return_if_fail ((keyval == 0 && text != NULL) || keyval != 0);
-  _mousepad_return_if_fail (g_slist_length (view->selection_marks) % 2 == 0);
+  mousepad_return_if_fail (view->selection_marks != NULL);
+  mousepad_return_if_fail (view->selection_start_x == -1);
+  mousepad_return_if_fail (view->selection_end_x == -1);
+  mousepad_return_if_fail ((keyval == 0 && text != NULL) || keyval != 0);
+  mousepad_return_if_fail (g_slist_length (view->selection_marks) % 2 == 0);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -785,9 +782,9 @@ mousepad_view_selection_delete_content (MousepadView *view)
   GtkTextIter    start_iter, end_iter;
   GSList        *li;
 
-  _mousepad_return_if_fail (view->selection_marks != NULL);
-  _mousepad_return_if_fail (view->selection_length > 0);
-  _mousepad_return_if_fail (g_slist_length (view->selection_marks) % 2 == 0);
+  mousepad_return_if_fail (view->selection_marks != NULL);
+  mousepad_return_if_fail (view->selection_length > 0);
+  mousepad_return_if_fail (g_slist_length (view->selection_marks) % 2 == 0);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -827,7 +824,7 @@ mousepad_view_selection_destroy (MousepadView *view)
   GSList        *li;
   GtkTextIter    start_iter, end_iter;
 
-  _mousepad_return_if_fail (view->selection_marks != NULL);
+  mousepad_return_if_fail (view->selection_marks != NULL);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -887,8 +884,8 @@ mousepad_view_selection_draw (MousepadView *view,
   GtkTextMark   *start_mark, *end_mark;
   GSList        *li;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (view->selection_marks == NULL || g_slist_length (view->selection_marks) % 2 == 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (view->selection_marks == NULL || g_slist_length (view->selection_marks) % 2 == 0);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -1124,7 +1121,7 @@ mousepad_view_selection_string (MousepadView *view)
   GtkTextIter    start_iter, end_iter;
   GSList        *li;
 
-  _mousepad_return_val_if_fail (view->selection_marks == NULL || g_slist_length (view->selection_marks) % 2 == 0, NULL);
+  mousepad_return_val_if_fail (view->selection_marks == NULL || g_slist_length (view->selection_marks) % 2 == 0, NULL);
 
   /* create new string */
   string = g_string_new (NULL);
@@ -1279,7 +1276,7 @@ mousepad_view_indent_selection (MousepadView *view,
           || ((gtk_text_iter_starts_line (&start_iter) && gtk_text_iter_ends_line (&end_iter)) || force))
         {
           /* change indentation of each line */
-          for (i = start_line; i <= end_line; i++)
+          for (i = start_line; i <= end_line && i < G_MAXINT; i++)
             {
               /* get the iter of the line we're going to indent */
               gtk_text_buffer_get_iter_at_line (buffer, &start_iter, i);
@@ -1341,8 +1338,8 @@ mousepad_view_calculate_layout_width (GtkWidget *widget,
   gchar       *string;
   gint         width = -1;
 
-  _mousepad_return_val_if_fail (GTK_IS_WIDGET (widget), -1);
-  _mousepad_return_val_if_fail (length > 0, -1);
+  mousepad_return_val_if_fail (GTK_IS_WIDGET (widget), -1);
+  mousepad_return_val_if_fail (length > 0, -1);
 
   /* create character string */
   string = g_strnfill (length, fill_char);
@@ -1372,7 +1369,7 @@ mousepad_view_scroll_to_cursor (MousepadView *view)
 {
   GtkTextBuffer *buffer;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -1393,7 +1390,7 @@ mousepad_view_transpose_multi_selection (GtkTextBuffer *buffer,
   GSList      *strings = NULL;
   GtkTextIter  start_iter, end_iter;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the strings and delete the existing strings */
   for (li = view->selection_marks; li != NULL; li = li->next)
@@ -1498,7 +1495,7 @@ mousepad_view_transpose_lines (GtkTextBuffer *buffer,
   string = g_string_new (NULL);
 
   /* add the lines in reversed order to the string */
-  for (i = start_line; i <= end_line; i++)
+  for (i = start_line; i <= end_line && i < G_MAXINT; i++)
     {
       /* get start iter */
       gtk_text_buffer_get_iter_at_line (buffer, start_iter, i);
@@ -1621,7 +1618,7 @@ mousepad_view_transpose (MousepadView *view)
   GtkTextBuffer *buffer;
   GtkTextIter    sel_start, sel_end;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -1697,8 +1694,8 @@ mousepad_view_clipboard_cut (MousepadView *view)
   GtkTextBuffer *buffer;
   gchar         *string;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
 
   /* get the clipboard */
   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (view), GDK_SELECTION_CLIPBOARD);
@@ -1742,8 +1739,8 @@ mousepad_view_clipboard_copy (MousepadView *view)
   GtkTextBuffer *buffer;
   gchar         *string;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
 
   /* get the clipboard */
   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (view), GDK_SELECTION_CLIPBOARD);
@@ -1886,8 +1883,8 @@ mousepad_view_delete_selection (MousepadView *view)
 {
   GtkTextBuffer *buffer;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
 
   if (view->selection_marks != NULL)
     {
@@ -1918,7 +1915,7 @@ mousepad_view_select_all (MousepadView *view)
   GtkTextIter    start, end;
   GtkTextBuffer *buffer;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* cleanup our selection */
   if (view->selection_marks != NULL)
@@ -1943,8 +1940,8 @@ mousepad_view_change_selection (MousepadView *view)
   GtkTextIter    start_iter, end_iter;
   GdkRectangle   rect;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) != 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) != 0);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2008,8 +2005,8 @@ mousepad_view_convert_selection_case (MousepadView *view,
   gint           offset = -1;
   GSList        *li = NULL;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (mousepad_view_get_selection_length (view, NULL) > 0);
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2063,7 +2060,7 @@ mousepad_view_convert_selection_case (MousepadView *view,
                 break;
 
               default:
-                _mousepad_assert_not_reached ();
+                mousepad_assert_not_reached ();
                 break;
             }
 
@@ -2071,7 +2068,7 @@ mousepad_view_convert_selection_case (MousepadView *view,
           if (G_LIKELY (converted && strcmp (text, converted) != 0))
             {
               /* debug check */
-              _mousepad_return_if_fail (g_utf8_validate (converted, -1, NULL));
+              mousepad_return_if_fail (g_utf8_validate (converted, -1, NULL));
 
               /* delete old string */
               gtk_text_buffer_delete (buffer, &start_iter, &end_iter);
@@ -2128,7 +2125,7 @@ mousepad_view_convert_spaces_and_tabs (MousepadView *view,
   gint           start_offset = -1;
   gchar         *string;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2293,7 +2290,7 @@ mousepad_view_strip_trailing_spaces (MousepadView *view)
   gint           start, end, i;
   gunichar       c;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2365,7 +2362,7 @@ mousepad_view_move_selection (MousepadView *view,
   gchar         *text;
   gboolean       insert_eol;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2488,7 +2485,7 @@ mousepad_view_duplicate (MousepadView *view)
   gboolean       has_selection;
   gboolean       insert_eol = FALSE;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* get the buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2527,7 +2524,7 @@ void
 mousepad_view_indent (MousepadView *view,
                       gint          type)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* run a forced indent of the line(s) */
   mousepad_view_indent_selection (view, type == INCREASE_INDENT, TRUE);
@@ -2539,7 +2536,7 @@ void
 mousepad_view_set_line_numbers (MousepadView *view,
                                 gboolean      line_numbers)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   if (view->line_numbers != line_numbers)
     {
@@ -2559,7 +2556,7 @@ void
 mousepad_view_set_auto_indent (MousepadView *view,
                                gboolean      auto_indent)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* set the boolean */
   view->auto_indent = auto_indent;
@@ -2574,8 +2571,8 @@ mousepad_view_set_tab_size (MousepadView *view,
   PangoTabArray *tab_array;
   gint           layout_width;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
-  _mousepad_return_if_fail (GTK_IS_TEXT_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (GTK_IS_TEXT_VIEW (view));
 
   /* set the value */
   view->tab_size = tab_size;
@@ -2603,7 +2600,7 @@ void
 mousepad_view_set_insert_spaces (MousepadView *view,
                                  gboolean      insert_spaces)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (view));
 
   /* set boolean */
   view->insert_spaces = insert_spaces;
@@ -2620,7 +2617,7 @@ mousepad_view_get_selection_length (MousepadView *view,
   gint           sel_length = 0;
   gboolean       column_selection;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
 
   /* get the text buffer */
   buffer = mousepad_view_get_buffer (view);
@@ -2653,7 +2650,7 @@ mousepad_view_get_selection_length (MousepadView *view,
 gboolean
 mousepad_view_get_line_numbers (MousepadView *view)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
 
   return view->line_numbers;
 }
@@ -2663,7 +2660,7 @@ mousepad_view_get_line_numbers (MousepadView *view)
 gboolean
 mousepad_view_get_auto_indent (MousepadView *view)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
 
   return view->auto_indent;
 }
@@ -2673,7 +2670,7 @@ mousepad_view_get_auto_indent (MousepadView *view)
 gint
 mousepad_view_get_tab_size (MousepadView *view)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), -1);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), -1);
 
   return view->tab_size;
 }
@@ -2683,7 +2680,7 @@ mousepad_view_get_tab_size (MousepadView *view)
 gboolean
 mousepad_view_get_insert_spaces (MousepadView *view)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_VIEW (view), FALSE);
 
   return view->insert_spaces;
 }
diff --git a/mousepad/mousepad-view.h b/mousepad/mousepad-view.h
index 3cb6b97..109f7f0 100644
--- a/mousepad/mousepad-view.h
+++ b/mousepad/mousepad-view.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
diff --git a/mousepad/mousepad-window-ui.xml b/mousepad/mousepad-window-ui.xml
index e271752..b6b6575 100644
--- a/mousepad/mousepad-window-ui.xml
+++ b/mousepad/mousepad-window-ui.xml
@@ -1,7 +1,5 @@
 <ui>
   <!--
-    $Id$
-
     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 the Free
     Software Foundation; either version 2 of the License, or (at your option)
@@ -103,6 +101,8 @@
       <separator />
       <menuitem action="auto-indent" />
       <separator />
+      <menuitem action="write-bom" />
+      <separator />
       <menu action="tab-size-menu">
         <placeholder name="placeholder-tab-items" />
         <separator />
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index 66ee859..1c284a1 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -54,7 +53,7 @@
 #define PASTE_HISTORY_MENU_LENGTH (30)
 
 #if GTK_CHECK_VERSION (2,12,0)
-static gpointer NOTEBOOK_GROUP = "Mousepad";
+static gconstpointer NOTEBOOK_GROUP = "Mousepad";
 #endif
 
 
@@ -63,14 +62,11 @@ enum
 {
   NEW_WINDOW,
   NEW_WINDOW_WITH_DOCUMENT,
-  LAST_SIGNAL,
+  LAST_SIGNAL
 };
 
 
 
-/* class functions */
-static void              mousepad_window_class_init                   (MousepadWindowClass    *klass);
-static void              mousepad_window_init                         (MousepadWindow         *window);
 static void              mousepad_window_dispose                      (GObject                *object);
 static void              mousepad_window_finalize                     (GObject                *object);
 static gboolean          mousepad_window_configure_event              (GtkWidget              *widget,
@@ -97,7 +93,7 @@ static void              mousepad_window_save_geometry_timer_destroy  (gpointer
 /* window functions */
 static gboolean          mousepad_window_open_file                    (MousepadWindow         *window,
                                                                        const gchar            *filename,
-                                                                       const gchar            *encoding);
+                                                                       MousepadEncoding        encoding);
 static gboolean          mousepad_window_close_document               (MousepadWindow         *window,
                                                                        MousepadDocument       *document);
 static void              mousepad_window_set_title                    (MousepadWindow         *window);
@@ -183,7 +179,7 @@ static void              mousepad_window_recent_manager_init          (MousepadW
 static gboolean          mousepad_window_recent_menu_idle             (gpointer                user_data);
 static void              mousepad_window_recent_menu_idle_destroy     (gpointer                user_data);
 static void              mousepad_window_recent_menu                  (MousepadWindow         *window);
-static const gchar      *mousepad_window_recent_get_encoding          (GtkRecentInfo          *info);
+static const gchar      *mousepad_window_recent_get_charset           (GtkRecentInfo          *info);
 static void              mousepad_window_recent_clear                 (MousepadWindow         *window);
 
 /* dnd */
@@ -193,7 +189,7 @@ static void              mousepad_window_drag_data_received           (GtkWidget
                                                                        gint                    y,
                                                                        GtkSelectionData       *selection_data,
                                                                        guint                   info,
-                                                                       guint                   time,
+                                                                       guint                   drag_time,
                                                                        MousepadWindow         *window);
 
 /* search bar */
@@ -312,6 +308,8 @@ static void              mousepad_window_action_line_numbers          (GtkToggle
                                                                        MousepadWindow         *window);
 static void              mousepad_window_action_word_wrap             (GtkToggleAction        *action,
                                                                        MousepadWindow         *window);
+static void              mousepad_window_action_write_bom             (GtkToggleAction        *action,
+                                                                       MousepadWindow         *window);
 static void              mousepad_window_action_auto_indent           (GtkToggleAction        *action,
                                                                        MousepadWindow         *window);
 static void              mousepad_window_action_tab_size              (GtkToggleAction        *action,
@@ -450,7 +448,7 @@ static const GtkActionEntry action_entries[] =
 
   { "help-menu", NULL, N_("_Help"), NULL, },
     { "contents", GTK_STOCK_HELP, N_ ("_Contents"), "F1", N_("Display the Mousepad user manual"), G_CALLBACK (mousepad_window_action_contents), },
-    { "about", GTK_STOCK_ABOUT, NULL, NULL, N_("About this application"), G_CALLBACK (mousepad_window_action_about), },
+    { "about", GTK_STOCK_ABOUT, NULL, NULL, N_("About this application"), G_CALLBACK (mousepad_window_action_about), }
 };
 
 static const GtkToggleActionEntry toggle_action_entries[] =
@@ -459,14 +457,15 @@ static const GtkToggleActionEntry toggle_action_entries[] =
   { "line-numbers", NULL, N_("Line N_umbers"), NULL, N_("Show line numbers"), G_CALLBACK (mousepad_window_action_line_numbers), FALSE, },
   { "auto-indent", NULL, N_("_Auto Indent"), NULL, N_("Auto indent a new line"), G_CALLBACK (mousepad_window_action_auto_indent), FALSE, },
   { "word-wrap", NULL, N_("_Word Wrap"), NULL, N_("Toggle breaking lines in between words"), G_CALLBACK (mousepad_window_action_word_wrap), FALSE, },
-  { "insert-spaces", NULL, N_("Insert _Spaces"), NULL, N_("Insert spaces when the tab button is pressed"), G_CALLBACK (mousepad_window_action_insert_spaces), FALSE, },
+  { "write-bom", NULL, N_("Write Unicode _BOM"), NULL, N_("Store the byte-order mark in the file"), G_CALLBACK (mousepad_window_action_write_bom), FALSE, },
+  { "insert-spaces", NULL, N_("Insert _Spaces"), NULL, N_("Insert spaces when the tab button is pressed"), G_CALLBACK (mousepad_window_action_insert_spaces), FALSE, }
 };
 
 static const GtkRadioActionEntry radio_action_entries[] =
 {
-  { "unix", NULL, N_("Unix (_LF)"), NULL, N_("Set the line ending of the document to Unix (LF)"), MOUSEPAD_LINE_END_UNIX, },
-  { "mac", NULL, N_("Mac (_CR)"), NULL, N_("Set the line ending of the document to Mac (CR)"), MOUSEPAD_LINE_END_MAC, },
-  { "dos", NULL, N_("DOS / Windows (C_R LF)"), NULL, N_("Set the line ending of the document to DOS / Windows (CR LF)"), MOUSEPAD_LINE_END_DOS, },
+  { "unix", NULL, N_("Unix (_LF)"), NULL, N_("Set the line ending of the document to Unix (LF)"), MOUSEPAD_EOL_UNIX, },
+  { "mac", NULL, N_("Mac (_CR)"), NULL, N_("Set the line ending of the document to Mac (CR)"), MOUSEPAD_EOL_MAC, },
+  { "dos", NULL, N_("DOS / Windows (C_R LF)"), NULL, N_("Set the line ending of the document to DOS / Windows (CR LF)"), MOUSEPAD_EOL_DOS, }
 };
 
 
@@ -658,7 +657,7 @@ mousepad_window_init (MousepadWindow *window)
 
   /* set the group id */
 #if GTK_CHECK_VERSION (2,12,0)
-  gtk_notebook_set_group (GTK_NOTEBOOK (window->notebook), NOTEBOOK_GROUP);
+  gtk_notebook_set_group (GTK_NOTEBOOK (window->notebook), (gpointer) NOTEBOOK_GROUP);
 #else
   gtk_notebook_set_group_id (GTK_NOTEBOOK (window->notebook), 1337);
 #endif
@@ -787,9 +786,9 @@ mousepad_window_connect_proxy (GtkUIManager   *manager,
                                GtkWidget      *proxy,
                                MousepadWindow *window)
 {
-  _mousepad_return_if_fail (GTK_IS_ACTION (action));
-  _mousepad_return_if_fail (GTK_IS_MENU_ITEM (proxy));
-  _mousepad_return_if_fail (GTK_IS_UI_MANAGER (manager));
+  mousepad_return_if_fail (GTK_IS_ACTION (action));
+  mousepad_return_if_fail (GTK_IS_MENU_ITEM (proxy));
+  mousepad_return_if_fail (GTK_IS_UI_MANAGER (manager));
 
   /* we want to get informed when the user hovers a menu item */
   g_signal_connect_closure (G_OBJECT (proxy), "select", window->menu_item_selected_closure, FALSE);
@@ -804,9 +803,9 @@ mousepad_window_disconnect_proxy (GtkUIManager   *manager,
                                   GtkWidget      *proxy,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (GTK_IS_ACTION (action));
-  _mousepad_return_if_fail (GTK_IS_MENU_ITEM (proxy));
-  _mousepad_return_if_fail (GTK_IS_UI_MANAGER (manager));
+  mousepad_return_if_fail (GTK_IS_ACTION (action));
+  mousepad_return_if_fail (GTK_IS_MENU_ITEM (proxy));
+  mousepad_return_if_fail (GTK_IS_UI_MANAGER (manager));
 
   /* disconnect the signal from mousepad_window_connect_proxy() */
   g_signal_handlers_disconnect_matched (G_OBJECT (proxy), G_SIGNAL_MATCH_CLOSURE, 0, 0, window->menu_item_selected_closure, NULL, NULL);
@@ -827,7 +826,11 @@ mousepad_window_menu_item_selected (GtkWidget      *menu_item,
   if (G_LIKELY (window->statusbar != NULL))
     {
       /* get the action from the menu item */
+#if GTK_CHECK_VERSION (2, 16, 0)
+      action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
+#else
       action = gtk_widget_get_action (menu_item);
+#endif
       if (G_LIKELY (action))
         {
           /* read the tooltip from the action, if there is one */
@@ -922,24 +925,24 @@ mousepad_window_save_geometry_timer_destroy (gpointer user_data)
  * Mousepad Window Functions
  **/
 static gboolean
-mousepad_window_open_file (MousepadWindow *window,
-                           const gchar    *filename,
-                           const gchar    *encoding)
+mousepad_window_open_file (MousepadWindow   *window,
+                           const gchar      *filename,
+                           MousepadEncoding  encoding)
 {
   MousepadDocument *document;
   GError           *error = NULL;
-  gboolean          succeed = FALSE;
+  gint              result;
   gint              npages = 0, i;
   gint              response;
+  const gchar      *charset;
   const gchar      *opened_filename;
-  const gchar      *new_encoding;
   GtkWidget        *dialog;
   gboolean          encoding_from_recent = FALSE;
   gchar            *uri;
   GtkRecentInfo    *info;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (filename != NULL && *filename != '\0', FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (filename != NULL && *filename != '\0', FALSE);
 
   /* check if the file is already openend */
   npages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook));
@@ -948,7 +951,7 @@ mousepad_window_open_file (MousepadWindow *window,
       document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i));
 
       /* debug check */
-      _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+      mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
 
       if (G_LIKELY (document))
         {
@@ -979,107 +982,107 @@ mousepad_window_open_file (MousepadWindow *window,
   /* set the passed encoding */
   mousepad_file_set_encoding (document->file, encoding);
 
-  try_open_again:
+  retry:
 
   /* lock the undo manager */
   mousepad_undo_lock (document->undo);
 
   /* read the content into the buffer */
-  succeed = mousepad_file_open (document->file, &error);
+  result = mousepad_file_open (document->file, NULL, &error);
 
   /* release the lock */
   mousepad_undo_unlock (document->undo);
 
-  if (G_LIKELY (succeed))
+  switch (result)
     {
-      /* add the document to the notebook and connect some signals */
-      mousepad_window_add (window, document);
+      case 0:
+        /* add the document to the window */
+        mousepad_window_add (window, document);
 
-      /* add to the recent history */
-      mousepad_window_recent_add (window, document->file);
-    }
-  else if (error->domain == G_CONVERT_ERROR)
-    {
-      /* clear the error */
-      g_clear_error (&error);
+        /* insert in the recent history */
+        mousepad_window_recent_add (window, document->file);
+        break;
 
-      /* try to lookup the encoding from the recent history */
-      if (encoding_from_recent == FALSE)
-        {
-          /* don't try this again */
-          encoding_from_recent = TRUE;
+      case ERROR_CONVERTING_FAILED:
+      case ERROR_NOT_UTF8_VALID:
+        /* clear the error */
+        g_clear_error (&error);
 
-          /* build uri */
-          uri = g_filename_to_uri (filename, NULL, NULL);
-          if (G_LIKELY (uri != NULL))
-            {
-              /* try to lookup the recent item */
-              info = gtk_recent_manager_lookup_item (window->recent_manager, uri, NULL);
+        /* try to lookup the encoding from the recent history */
+        if (encoding_from_recent == FALSE)
+          {
+            /* we only try this once */
+            encoding_from_recent = TRUE;
 
-              /* cleanup */
-              g_free (uri);
+            /* build uri */
+            uri = g_filename_to_uri (filename, NULL, NULL);
+            if (G_LIKELY (uri))
+              {
+                /* try to lookup the recent item */
+                info = gtk_recent_manager_lookup_item (window->recent_manager, uri, NULL);
 
-              if (info)
-                {
-                  /* try to find the encoding */
-                  new_encoding = mousepad_window_recent_get_encoding (info);
+                /* cleanup */
+                g_free (uri);
 
-                  /* release */
-                  gtk_recent_info_unref (info);
+                if (info)
+                  {
+                    /* try to find the encoding */
+                    charset = mousepad_window_recent_get_charset (info);
 
-                  /* try again if the encoding looks usefull, else
-                   * fall-trough and open encoding dialog */
-                  if (G_LIKELY (new_encoding != NULL))
-                    {
-                      /* set encoding */
-                      mousepad_file_set_encoding (document->file, new_encoding);
+                    encoding = mousepad_encoding_find (charset);
 
-                      goto try_open_again;
-                    }
-                }
-            }
-        }
+                    /* set the new encoding */
+                    mousepad_file_set_encoding (document->file, encoding);
 
-      /* run the encoding dialog */
-      dialog = mousepad_encoding_dialog_new (GTK_WINDOW (window), document->file);
+                    /* release */
+                    gtk_recent_info_unref (info);
 
-      /* run the dialog */
-      response = gtk_dialog_run (GTK_DIALOG (dialog));
+                    /* try to open again with the last used encoding */
+                    if (G_LIKELY (encoding))
+                      goto retry;
+                  }
+              }
+          }
 
-      if (response == GTK_RESPONSE_OK)
-        {
-          /* set the new encoding */
-          new_encoding = mousepad_encoding_dialog_get_encoding (MOUSEPAD_ENCODING_DIALOG (dialog));
+        /* run the encoding dialog */
+        dialog = mousepad_encoding_dialog_new (GTK_WINDOW (window), document->file);
 
-          /* set encoding */
-          mousepad_file_set_encoding (document->file, new_encoding);
-        }
+        /* run the dialog */
+        response = gtk_dialog_run (GTK_DIALOG (dialog));
 
-      /* destroy the dialog */
-      gtk_widget_destroy (dialog);
+        if (response == GTK_RESPONSE_OK)
+          {
+            /* set the new encoding */
+            encoding = mousepad_encoding_dialog_get_encoding (MOUSEPAD_ENCODING_DIALOG (dialog));
 
-      /* handle */
-      if (response == GTK_RESPONSE_OK)
-        goto try_open_again;
-      else
-        goto opening_failed;
-    }
-  else
-    {
-      opening_failed:
+            /* set encoding */
+            mousepad_file_set_encoding (document->file, encoding);
+          }
 
-      /* something went wrong, release the document */
-      g_object_unref (G_OBJECT (document));
+        /* destroy the dialog */
+        gtk_widget_destroy (dialog);
 
-      if (G_LIKELY (error))
-        {
-          /* show the warning */
-          mousepad_dialogs_show_error (GTK_WINDOW (window), error, _("Failed to open file"));
-          g_error_free (error);
-        }
+        /* handle */
+        if (response == GTK_RESPONSE_OK)
+          goto retry;
+
+      default:
+        /* something went wrong, release the document */
+        g_object_unref (G_OBJECT (document));
+
+        if (G_LIKELY (error))
+          {
+            /* show the warning */
+            mousepad_dialogs_show_error (GTK_WINDOW (window), error, _("Failed to open the document"));
+
+            /* cleanup */
+            g_error_free (error);
+          }
+
+        break;
     }
 
-  return succeed;
+  return (result == 0);
 }
 
 
@@ -1092,10 +1095,10 @@ mousepad_window_open_files (MousepadWindow  *window,
   guint  n;
   gchar *filename;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (working_directory != NULL, FALSE);
-  _mousepad_return_val_if_fail (filenames != NULL, FALSE);
-  _mousepad_return_val_if_fail (*filenames != NULL, FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (working_directory != NULL, FALSE);
+  mousepad_return_val_if_fail (filenames != NULL, FALSE);
+  mousepad_return_val_if_fail (*filenames != NULL, FALSE);
 
   /* block menu updates */
   lock_menu_updates++;
@@ -1121,7 +1124,7 @@ mousepad_window_open_files (MousepadWindow  *window,
         }
 
       /* open a new tab with the file */
-      mousepad_window_open_file (window, filename ? filename : filenames[n], NULL);
+      mousepad_window_open_file (window, filename ? filename : filenames[n], MOUSEPAD_ENCODING_UTF_8);
 
       /* cleanup */
       g_free (filename);
@@ -1149,14 +1152,11 @@ mousepad_window_add (MousepadWindow   *window,
 {
   GtkWidget        *label;
   gint              page;
-  MousepadDocument *prev_active;
-
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (GTK_IS_NOTEBOOK (window->notebook));
+  MousepadDocument *prev_active = window->active;
 
-  /* get the active tab before we switch to the new one */
-  prev_active = window->active;
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_NOTEBOOK (window->notebook));
 
   /* create the tab label */
   label = mousepad_document_get_tab_label (document);
@@ -1201,8 +1201,8 @@ mousepad_window_close_document (MousepadWindow   *window,
   gint     response;
   gboolean readonly;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), FALSE);
 
   /* check if the document has been modified */
   if (gtk_text_buffer_get_modified (document->buffer))
@@ -1256,8 +1256,8 @@ mousepad_window_set_title (MousepadWindow *window)
   gboolean          show_full_path;
   MousepadDocument *document = window->active;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* whether to show the full path */
   g_object_get (G_OBJECT (window->preferences), "misc-path-in-title", &show_full_path, NULL);
@@ -1294,8 +1294,8 @@ mousepad_window_notebook_switch_page (GtkNotebook     *notebook,
 {
   MousepadDocument *document;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
 
   /* get the new active document */
   document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (notebook, page_num));
@@ -1325,8 +1325,8 @@ mousepad_window_notebook_reordered (GtkNotebook     *notebook,
                                     guint            page_num,
                                     MousepadWindow  *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (page));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (page));
 
   /* update the go menu */
   mousepad_window_update_gomenu (window);
@@ -1344,8 +1344,8 @@ mousepad_window_notebook_added (GtkNotebook     *notebook,
   gboolean          always_show_tabs;
   gint              npages;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* connect signals to the document for this window */
   g_signal_connect (G_OBJECT (page), "close-tab", G_CALLBACK (mousepad_window_button_close_tab), window);
@@ -1383,9 +1383,9 @@ mousepad_window_notebook_removed (GtkNotebook     *notebook,
   gint              npages;
   MousepadDocument *document = MOUSEPAD_DOCUMENT (page);
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
 
   /* disconnect the old document signals */
   g_signal_handlers_disconnect_by_func (G_OBJECT (page), mousepad_window_button_close_tab, window);
@@ -1457,7 +1457,7 @@ mousepad_window_notebook_button_press_event (GtkNotebook    *notebook,
   guint      page_num = 0;
   gint       x_root;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
 
   if (event->type == GDK_BUTTON_PRESS && (event->button == 3 || event->button == 2))
     {
@@ -1524,8 +1524,8 @@ mousepad_window_notebook_button_release_event (GtkNotebook    *notebook,
                                                GdkEventButton *event,
                                                MousepadWindow *window)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
 
   /* focus the active textview */
   mousepad_document_focus_textview (window->active);
@@ -1545,8 +1545,8 @@ mousepad_window_notebook_create_window (GtkNotebook    *notebook,
 {
   MousepadDocument *document;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), NULL);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (page), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (page), NULL);
 
   /* only create new window when there are more then 2 tabs */
   if (gtk_notebook_get_n_pages (notebook) >= 2)
@@ -1579,7 +1579,7 @@ mousepad_window_notebook_create_window (GtkNotebook    *notebook,
 static void
 mousepad_window_modified_changed (MousepadWindow   *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   mousepad_window_set_title (window);
 }
@@ -1595,8 +1595,8 @@ mousepad_window_cursor_changed (MousepadDocument *document,
 {
 
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   if (window->statusbar)
     {
@@ -1612,15 +1612,17 @@ mousepad_window_selection_changed (MousepadDocument *document,
                                    gint              selection,
                                    MousepadWindow   *window)
 {
-  GtkAction *action;
-  gint       i;
+  GtkAction   *action;
+  guint        i;
+  const gchar *action_names1[] = { "tabs-to-spaces", "spaces-to-tabs", "duplicate", "strip-trailing" };
+  const gchar *action_names2[] = { "line-up", "line-down" };
+  const gchar *action_names3[] = { "cut", "copy", "delete", "lowercase", "uppercase", "titlecase", "opposite-case" };
 
   /* sensitivity of the change selection action */
   action = gtk_action_group_get_action (window->action_group, "change-selection");
   gtk_action_set_sensitive (action, selection != 0);
 
   /* actions that are unsensitive during a column selection */
-  const gchar *action_names1[] = { "tabs-to-spaces", "spaces-to-tabs", "duplicate", "strip-trailing" };
   for (i = 0; i < G_N_ELEMENTS (action_names1); i++)
     {
       action = gtk_action_group_get_action (window->action_group, action_names1[i]);
@@ -1628,7 +1630,6 @@ mousepad_window_selection_changed (MousepadDocument *document,
     }
 
   /* action that are only sensitive for normal selections */
-  const gchar *action_names2[] = { "line-up", "line-down" };
   for (i = 0; i < G_N_ELEMENTS (action_names2); i++)
     {
       action = gtk_action_group_get_action (window->action_group, action_names2[i]);
@@ -1636,7 +1637,6 @@ mousepad_window_selection_changed (MousepadDocument *document,
     }
 
   /* actions that are sensitive for all selections with content */
-  const gchar *action_names3[] = { "cut", "copy", "delete", "lowercase", "uppercase", "titlecase", "opposite-case" };
   for (i = 0; i < G_N_ELEMENTS (action_names3); i++)
     {
       action = gtk_action_group_get_action (window->action_group, action_names3[i]);
@@ -1651,8 +1651,8 @@ mousepad_window_overwrite_changed (MousepadDocument *document,
                                    gboolean          overwrite,
                                    MousepadWindow   *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
   /* set the new overwrite mode in the statusbar */
   if (window->statusbar)
@@ -1817,9 +1817,9 @@ mousepad_window_menu_templates (GtkWidget      *item,
   const gchar *homedir;
   gchar       *templates_path;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
-  _mousepad_return_if_fail (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL);
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
+  mousepad_return_if_fail (gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)) == NULL);
 
   /* schedule the idle build of the recent menu */
   mousepad_window_recent_menu (window);
@@ -1943,8 +1943,8 @@ mousepad_window_menu_tab_sizes_update (MousepadWindow *window)
   gchar     *name, *label = NULL;
   GtkAction *action;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* avoid menu actions */
   lock_menu_updates++;
@@ -1991,8 +1991,8 @@ static void
 mousepad_window_menu_textview_deactivate (GtkWidget   *menu,
                                           GtkTextView *textview)
 {
-  _mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
-  _mousepad_return_if_fail (textview->popup_menu == menu);
+  mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
+  mousepad_return_if_fail (textview->popup_menu == menu);
 
   /* disconnect this signal */
   g_signal_handlers_disconnect_by_func (G_OBJECT (menu), mousepad_window_menu_textview_deactivate, textview);
@@ -2010,11 +2010,11 @@ mousepad_window_menu_textview_popup (GtkTextView    *textview,
 {
   GtkWidget *menu;
 
-  _mousepad_return_if_fail (GTK_WIDGET (old_menu) == textview->popup_menu);
-  _mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
-  _mousepad_return_if_fail (GTK_IS_MENU (textview->popup_menu));
+  mousepad_return_if_fail (GTK_WIDGET (old_menu) == textview->popup_menu);
+  mousepad_return_if_fail (GTK_IS_TEXT_VIEW (textview));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (GTK_IS_MENU (textview->popup_menu));
 
   /* destroy origional menu */
   gtk_widget_destroy (textview->popup_menu);
@@ -2039,18 +2039,15 @@ mousepad_window_update_actions (MousepadWindow *window)
 {
   GtkAction          *action;
   GtkNotebook        *notebook = GTK_NOTEBOOK (window->notebook);
-  MousepadDocument   *document;
+  MousepadDocument   *document = window->active;
   gboolean            cycle_tabs;
   gint                n_pages;
   gint                page_num;
-  gboolean            active;
+  gboolean            active, sensitive;
   MousepadLineEnding  line_ending;
   const gchar        *action_name;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-
-  /* active document */
-  document = window->active;
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* update the actions for the active document */
   if (G_LIKELY (document))
@@ -2084,9 +2081,9 @@ mousepad_window_update_actions (MousepadWindow *window)
 
       /* line ending type */
       line_ending = mousepad_file_get_line_ending (document->file);
-      if (G_UNLIKELY (line_ending == MOUSEPAD_LINE_END_MAC))
+      if (G_UNLIKELY (line_ending == MOUSEPAD_EOL_MAC))
         action_name = "mac";
-      else if (G_UNLIKELY (line_ending == MOUSEPAD_LINE_END_DOS))
+      else if (G_UNLIKELY (line_ending == MOUSEPAD_EOL_DOS))
         action_name = "dos";
       else
         action_name = "unix";
@@ -2095,6 +2092,11 @@ mousepad_window_update_actions (MousepadWindow *window)
       action = gtk_action_group_get_action (window->action_group, action_name);
       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
 
+      /* write bom */
+      action = gtk_action_group_get_action (window->action_group, "write-bom");
+      gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), mousepad_file_get_write_bom (document->file, &sensitive));
+      gtk_action_set_sensitive (action, sensitive);
+
       /* toggle the document settings */
       active = mousepad_document_get_word_wrap (document);
       action = gtk_action_group_get_action (window->action_group, "word-wrap");
@@ -2138,15 +2140,15 @@ mousepad_window_update_gomenu_idle (gpointer user_data)
   MousepadWindow   *window;
   gint              npages;
   gint              n;
-  gchar            *name;
+  gchar             name[32];
   const gchar      *title;
   const gchar      *tooltip;
   gchar             accelerator[7];
-  GtkRadioAction   *action;
+  GtkAction        *action;
+  GtkRadioAction   *radio_action;
   GSList           *group = NULL;
-  GList            *actions, *li;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (user_data), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (user_data), FALSE);
 
   GDK_THREADS_ENTER ();
 
@@ -2161,12 +2163,15 @@ mousepad_window_update_gomenu_idle (gpointer user_data)
     {
       gtk_ui_manager_remove_ui (window->ui_manager, window->gomenu_merge_id);
 
-      /* drop all the previous actions from the action group */
-      actions = gtk_action_group_list_actions (window->action_group);
-      for (li = actions; li != NULL; li = li->next)
-        if (strncmp (gtk_action_get_name (li->data), "mousepad-tab-", 13) == 0)
-          gtk_action_group_remove_action (window->action_group, GTK_ACTION (li->data));
-      g_list_free (actions);
+      /* drop all the old recent items from the menu */
+      for (n = 0; n < 100 /* arbitrary */; n++)
+        {
+          g_snprintf (name, sizeof (name), "mousepad-tab-%d", n);
+          action = gtk_action_group_get_action (window->action_group, name);
+          if (G_UNLIKELY (action == NULL))
+            break;
+          gtk_action_group_remove_action (window->action_group, action);
+        }
     }
 
   /* create a new merge id */
@@ -2180,50 +2185,44 @@ mousepad_window_update_gomenu_idle (gpointer user_data)
       document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), n));
 
       /* create a new action name */
-      name = g_strdup_printf ("mousepad-tab-%d", n);
+      g_snprintf (name, sizeof (name), "mousepad-tab-%d", n);
 
       /* get the name and file name */
       title = mousepad_document_get_basename (document);
       tooltip = mousepad_document_get_filename (document);
 
       /* create the radio action */
-      action = gtk_radio_action_new (name, title, tooltip, NULL, n);
-      gtk_radio_action_set_group (action, group);
-      group = gtk_radio_action_get_group (action);
-      g_signal_connect (G_OBJECT (action), "activate", G_CALLBACK (mousepad_window_action_go_to_tab), window->notebook);
+      radio_action = gtk_radio_action_new (name, title, tooltip, NULL, n);
+      gtk_radio_action_set_group (radio_action, group);
+      group = gtk_radio_action_get_group (radio_action);
+      g_signal_connect (G_OBJECT (radio_action), "activate", G_CALLBACK (mousepad_window_action_go_to_tab), window->notebook);
 
       /* connect the action to the document to we can easily active it when the user switched from tab */
-      mousepad_object_set_data (G_OBJECT (document), "navigation-menu-action", action);
+      mousepad_object_set_data (G_OBJECT (document), "navigation-menu-action", radio_action);
 
       if (G_LIKELY (n < 9))
         {
           /* create an accelerator and add it to the menu */
           g_snprintf (accelerator, sizeof (accelerator), "<Alt>%d", n + 1);
-          gtk_action_group_add_action_with_accel (window->action_group, GTK_ACTION (action), accelerator);
+          gtk_action_group_add_action_with_accel (window->action_group, GTK_ACTION (radio_action), accelerator);
         }
       else
         /* add a menu item without accelerator */
-        gtk_action_group_add_action (window->action_group, GTK_ACTION (action));
+        gtk_action_group_add_action (window->action_group, GTK_ACTION (radio_action));
 
       /* select the active entry */
       if (gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook)) == n)
-        gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
+        gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action), TRUE);
 
       /* release the action */
-      g_object_unref (G_OBJECT (action));
+      g_object_unref (G_OBJECT (radio_action));
 
       /* add the action to the go menu */
       gtk_ui_manager_add_ui (window->ui_manager, window->gomenu_merge_id,
                              "/main-menu/navigation-menu/placeholder-file-items",
                              name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
-
-      /* cleanup */
-      g_free (name);
     }
 
-  /* make sure the ui is up2date to avoid flickering */
-  gtk_ui_manager_ensure_update (window->ui_manager);
-
   /* release our lock */
   lock_menu_updates--;
 
@@ -2237,7 +2236,7 @@ mousepad_window_update_gomenu_idle (gpointer user_data)
 static void
 mousepad_window_update_gomenu_idle_destroy (gpointer user_data)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (user_data));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (user_data));
 
   MOUSEPAD_WINDOW (user_data)->update_go_menu_id = 0;
 }
@@ -2247,7 +2246,7 @@ mousepad_window_update_gomenu_idle_destroy (gpointer user_data)
 static void
 mousepad_window_update_gomenu (MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* leave when we're updating multiple files or there is this an idle function pending */
   if (lock_menu_updates && window->update_go_menu_id != 0)
@@ -2269,21 +2268,25 @@ mousepad_window_recent_add (MousepadWindow *window,
 {
   GtkRecentData  info;
   gchar         *uri;
-  static gchar  *groups[] = { PACKAGE_NAME, NULL };
   gchar         *description;
+  const gchar   *charset;
+  static gchar  *groups[] = { (gchar *) PACKAGE_NAME, NULL };
+
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (file));
+  /* get the charset */
+  charset = mousepad_encoding_get_charset (mousepad_file_get_encoding (file));
 
   /* build description */
-  description = g_strdup_printf ("%s: %s", _("Encoding"), mousepad_file_get_encoding (file));
+  description = g_strdup_printf ("%s: %s", _("Charset"), charset);
 
   /* create the recent data */
   info.display_name = NULL;
-  info.description  = description;
-  info.mime_type    = "text/plain";
-  info.app_name     = PACKAGE_NAME;
-  info.app_exec     = PACKAGE " %u";
+  info.description  = (gchar *) description;
+  info.mime_type    = (gchar *) "text/plain";
+  info.app_name     = (gchar *) PACKAGE_NAME;
+  info.app_exec     = (gchar *) PACKAGE " %u";
   info.groups       = groups;
   info.is_private   = FALSE;
 
@@ -2337,15 +2340,16 @@ static gboolean
 mousepad_window_recent_menu_idle (gpointer user_data)
 {
   MousepadWindow *window = MOUSEPAD_WINDOW (user_data);
-  GList          *items, *li, *actions;
+  GList          *items, *li;
   GList          *filtered = NULL;
   GtkRecentInfo  *info;
   const gchar    *uri;
   const gchar    *display_name;
-  gchar          *tooltip, *name, *label;
+  gchar          *tooltip, *label;
   gchar          *filename, *filename_utf8;
   GtkAction      *action;
-  gint            n;
+  gchar           name[32];
+  gint            n, i;
 
   GDK_THREADS_ENTER ();
 
@@ -2354,12 +2358,15 @@ mousepad_window_recent_menu_idle (gpointer user_data)
       /* unmerge the ui controls from the previous update */
       gtk_ui_manager_remove_ui (window->ui_manager, window->recent_merge_id);
 
-      /* drop all the previous actions from the action group */
-      actions = gtk_action_group_list_actions (window->action_group);
-      for (li = actions; li != NULL; li = li->next)
-        if (strncmp (gtk_action_get_name (li->data), "recent-info-", 12) == 0)
-          gtk_action_group_remove_action (window->action_group, li->data);
-      g_list_free (actions);
+      /* drop all the old recent items from the menu */
+      for (i = 1; i < 100 /* arbitrary */; i++)
+        {
+          g_snprintf (name, sizeof (name), "recent-info-%d", i);
+          action = gtk_action_group_get_action (window->action_group, name);
+          if (G_UNLIKELY (action == NULL))
+            break;
+          gtk_action_group_remove_action (window->action_group, action);
+        }
     }
 
   /* create a new merge id */
@@ -2398,7 +2405,7 @@ mousepad_window_recent_menu_idle (gpointer user_data)
       if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
         {
           /* create the action name */
-          name = g_strdup_printf ("recent-info-%d", n);
+          g_snprintf (name, sizeof (name), "recent-info-%d", n);
 
           /* get the name of the item and escape the underscores */
           display_name = gtk_recent_info_get_display_name (info);
@@ -2431,9 +2438,6 @@ mousepad_window_recent_menu_idle (gpointer user_data)
                                  "/main-menu/file-menu/recent-menu/placeholder-recent-items",
                                  name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
 
-          /* cleanup */
-          g_free (name);
-
           /* decrease counter */
           n--;
         }
@@ -2461,9 +2465,6 @@ mousepad_window_recent_menu_idle (gpointer user_data)
   g_list_free (items);
   g_list_free (filtered);
 
-  /* make sure the ui is up2date to avoid flickering */
-  gtk_ui_manager_ensure_update (window->ui_manager);
-
   GDK_THREADS_LEAVE ();
 
   /* stop the idle function */
@@ -2483,7 +2484,7 @@ mousepad_window_recent_menu_idle_destroy (gpointer user_data)
 static void
 mousepad_window_recent_menu (MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* leave when we're updating multiple files or there is this an idle function pending */
   if (lock_menu_updates > 0 || window->update_recent_menu_id != 0)
@@ -2497,25 +2498,25 @@ mousepad_window_recent_menu (MousepadWindow *window)
 
 
 static const gchar *
-mousepad_window_recent_get_encoding (GtkRecentInfo *info)
+mousepad_window_recent_get_charset (GtkRecentInfo *info)
 {
   const gchar *description;
-  const gchar *encoding = NULL;
-  gint         offset;
+  const gchar *charset = NULL;
+  guint        offset;
 
   /* get the description */
   description = gtk_recent_info_get_description (info);
   if (G_LIKELY (description))
     {
       /* get the offset length: 'Encoding: ' */
-      offset = strlen (_("Encoding")) + 2;
+      offset = strlen (_("Charset")) + 2;
 
       /* check if the encoding string looks valid, if so, set it */
       if (G_LIKELY (strlen (description) > offset))
-        encoding = description + offset;
+        charset = description + offset;
     }
 
-  return encoding;
+  return charset;
 }
 
 
@@ -2575,7 +2576,7 @@ mousepad_window_drag_data_received (GtkWidget        *widget,
                                     gint              y,
                                     GtkSelectionData *selection_data,
                                     guint             info,
-                                    guint             time,
+                                    guint             drag_time,
                                     MousepadWindow   *window)
 {
   gchar     **uris;
@@ -2584,8 +2585,8 @@ mousepad_window_drag_data_received (GtkWidget        *widget,
   GtkWidget  *child, *label;
   gint        i, n_pages;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
 
   /* we only accept text/uri-list drops with format 8 and atleast one byte of data */
   if (info == TARGET_TEXT_URI_LIST && selection_data->format == 8 && selection_data->length > 0)
@@ -2604,7 +2605,7 @@ mousepad_window_drag_data_received (GtkWidget        *widget,
       g_strfreev (uris);
 
       /* finish the drag (copy) */
-      gtk_drag_finish (context, TRUE, FALSE, time);
+      gtk_drag_finish (context, TRUE, FALSE, drag_time);
     }
   else if (info == TARGET_GTK_NOTEBOOK_TAB)
     {
@@ -2615,7 +2616,7 @@ mousepad_window_drag_data_received (GtkWidget        *widget,
       document = (GtkWidget **) selection_data->data;
 
       /* check */
-      _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (*document));
+      mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (*document));
 
       /* take a reference on the document before we remove it */
       g_object_ref (G_OBJECT (*document));
@@ -2648,7 +2649,7 @@ mousepad_window_drag_data_received (GtkWidget        *widget,
       g_object_unref (G_OBJECT (*document));
 
       /* finish the drag (move) */
-      gtk_drag_finish (context, TRUE, TRUE, time);
+      gtk_drag_finish (context, TRUE, TRUE, drag_time);
     }
 }
 
@@ -2667,7 +2668,7 @@ mousepad_window_search (MousepadWindow      *window,
   gint       npages, i;
   GtkWidget *document;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), -1);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), -1);
 
   if (flags & MOUSEPAD_SEARCH_FLAGS_ACTION_HIGHTLIGHT)
     {
@@ -2701,7 +2702,7 @@ mousepad_window_search (MousepadWindow      *window,
   else
     {
       /* should never be reaches */
-      _mousepad_assert_not_reached ();
+      mousepad_assert_not_reached ();
     }
 
   return nmatches;
@@ -2717,9 +2718,9 @@ mousepad_window_hide_search_bar (MousepadWindow *window)
 {
   MousepadSearchFlags flags;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
-  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (window->search_bar));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (window->search_bar));
 
   /* setup flags */
   flags = MOUSEPAD_SEARCH_FLAGS_ACTION_HIGHTLIGHT
@@ -2747,7 +2748,7 @@ mousepad_window_paste_history_add (MousepadWindow *window)
   gchar        *text;
   GSList       *li;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* get the current clipboard text */
   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
@@ -2768,8 +2769,8 @@ mousepad_window_paste_history_add (MousepadWindow *window)
       /* add to the list */
       clipboard_history = g_slist_prepend (clipboard_history, text);
 
-      /* get the 9th item from the list and remove it if it exists */
-      li = g_slist_nth (clipboard_history, 9);
+      /* get the 10th item from the list and remove it if it exists */
+      li = g_slist_nth (clipboard_history, 10);
       if (li != NULL)
         {
           /* cleanup */
@@ -2800,10 +2801,10 @@ mousepad_window_paste_history_menu_position (GtkMenu  *menu,
   GdkRectangle      location;
   gint              iter_x, iter_y;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (GTK_IS_TEXT_VIEW (document->textview));
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (document->buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (GTK_IS_TEXT_VIEW (document->textview));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (document->buffer));
 
   /* get the root coordinates of the texview widget */
   gdk_window_get_origin (gtk_text_view_get_window (GTK_TEXT_VIEW (document->textview), GTK_TEXT_WINDOW_TEXT), x, y);
@@ -2832,10 +2833,10 @@ mousepad_window_paste_history_activate (GtkMenuItem    *item,
 {
   const gchar *text;
 
-  _mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
-  _mousepad_return_if_fail (MOUSEPAD_IS_VIEW (window->active->textview));
+  mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_VIEW (window->active->textview));
 
   /* get the menu item text */
   text = mousepad_object_get_data (G_OBJECT (item), "history-pointer");
@@ -2924,7 +2925,7 @@ mousepad_window_paste_history_menu (MousepadWindow *window)
   gchar         mnemonic[4];
   gint          n;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), NULL);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), NULL);
 
   /* create new menu and set the screen */
   menu = gtk_menu_new ();
@@ -3002,8 +3003,8 @@ mousepad_window_button_close_tab (MousepadDocument *document,
 {
   gint page_num;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* switch to the tab we're going to close */
   page_num = gtk_notebook_page_num (GTK_NOTEBOOK (window->notebook), GTK_WIDGET (document));
@@ -3019,7 +3020,7 @@ static gboolean
 mousepad_window_delete_event (MousepadWindow *window,
                               GdkEvent       *event)
 {
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
 
   /* try to close the window */
   mousepad_window_action_close_window (NULL, window);
@@ -3043,7 +3044,7 @@ mousepad_window_action_new (GtkAction      *action,
 {
   MousepadDocument *document;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* create new document */
   document = mousepad_document_new ();
@@ -3058,7 +3059,7 @@ static void
 mousepad_window_action_new_window (GtkAction      *action,
                                    MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* emit the new window signal */
   g_signal_emit (G_OBJECT (window), window_signals[NEW_WINDOW], 0);
@@ -3071,75 +3072,74 @@ mousepad_window_action_new_from_template (GtkMenuItem    *item,
                                           MousepadWindow *window)
 {
   const gchar      *filename;
-  gchar            *contents;
   GError           *error = NULL;
-  gboolean          succeed;
-  gsize             length;
+  gint              result;
   MousepadDocument *document;
-  GtkTextIter       iter;
+  const gchar      *message;
+
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (GTK_IS_MENU_ITEM (item));
 
   /* get the filename from the menu item */
   filename = mousepad_object_get_data (G_OBJECT (item), "filename");
 
   /* test if the file exists */
-  if (G_LIKELY (filename && g_file_test (filename, G_FILE_TEST_IS_REGULAR)))
+  if (G_LIKELY (filename))
     {
-      /* get the content of the template */
-      succeed = g_file_get_contents (filename, &contents, &length, &error);
-      if (G_LIKELY (succeed))
-        {
-          /* check if the template is utf-8 valid */
-          succeed = g_utf8_validate (contents, length, NULL);
-          if (G_LIKELY (succeed))
-            {
-              /* create new document */
-              document = mousepad_document_new ();
+      /* create new document */
+      document = mousepad_document_new ();
 
-              /* lock the undo manager */
-              mousepad_undo_lock (document->undo);
+      /* sink floating object */
+      g_object_ref_sink (G_OBJECT (document));
 
-              /* set the template content */
-              gtk_text_buffer_set_text (document->buffer, contents, length);
+      /* lock the undo manager */
+      mousepad_undo_lock (document->undo);
 
-              /* move iter to the start of the document */
-              gtk_text_buffer_get_start_iter (document->buffer, &iter);
-              gtk_text_buffer_place_cursor (document->buffer, &iter);
+      /* try to load the template into the buffer */
+      result = mousepad_file_open (document->file, filename, &error);
 
-              /* buffer is not modified */
-              gtk_text_buffer_set_modified (document->buffer, FALSE);
+      /* release the lock */
+      mousepad_undo_unlock (document->undo);
 
-              /* release the lock */
-              mousepad_undo_unlock (document->undo);
+      /* handle the result */
+      if (G_LIKELY (result == 0))
+        {
+          /* no errors, insert the document */
+          mousepad_window_add (window, document);
+        }
+      else
+        {
+          /* release the document */
+          g_object_unref (G_OBJECT (document));
 
-              /* add the document to the window */
-              mousepad_window_add (window, document);
-            }
-          else
+          /* handle the error */
+          switch (result)
             {
-              /* set and error */
-              g_set_error (&error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                           _("The template is not UTF-8 valid"));
+              case ERROR_NOT_UTF8_VALID:
+              case ERROR_CONVERTING_FAILED:
+                /* set error message */
+                message = _("Templates should be UTF-8 valid");
+                break;
+
+              case ERROR_READING_FAILED:
+                /* destroy the menu item */
+                gtk_widget_destroy (GTK_WIDGET (item));
+
+                /* set error message */
+                message = _("Reading the template failed, the menu item has been removed");
+                break;
+
+              default:
+                /* set error message */
+                message = _("Loading the template failed");
+                break;
             }
 
-          /* cleanup */
-          g_free (contents);
-        }
-
-      if (G_UNLIKELY (succeed == FALSE))
-        {
-          /* show an error */
-          mousepad_dialogs_show_error (GTK_WINDOW (window), error, _("Failed to open new file from a template"));
-          g_error_free (error);
+          /* show the error */
+          mousepad_dialogs_show_error (GTK_WINDOW (window), error, message);
         }
     }
-  else
-    {
-      /* destroy the menu item */
-      gtk_widget_destroy (GTK_WIDGET (item));
-    }
 }
 
 
@@ -3149,11 +3149,12 @@ mousepad_window_action_open (GtkAction      *action,
                              MousepadWindow *window)
 {
   GtkWidget   *chooser;
+  GtkWidget    *hbox, *label, *combobox;
   const gchar *filename;
   GSList      *filenames, *li;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* create new file chooser dialog */
   chooser = gtk_file_chooser_dialog_new (_("Open File"),
@@ -3166,6 +3167,22 @@ mousepad_window_action_open (GtkAction      *action,
   gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), TRUE);
   gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
 
+  /* encoding selector */
+  hbox = gtk_hbox_new (FALSE, 6);
+  gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
+  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), hbox);
+  gtk_widget_show (hbox);
+
+  label = gtk_label_new_with_mnemonic ("_Encoding:");
+  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+  gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
+  gtk_widget_show (label);
+
+  combobox = gtk_combo_box_new ();
+  gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
+  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combobox);
+  gtk_widget_show (combobox);
+
   /* select the active document in the file chooser */
   filename = mousepad_file_get_filename (window->active->file);
   if (filename && g_file_test (filename, G_FILE_TEST_EXISTS))
@@ -3187,7 +3204,7 @@ mousepad_window_action_open (GtkAction      *action,
       for (li = filenames; li != NULL; li = li->next)
         {
           /* open the file */
-          mousepad_window_open_file (window, li->data, NULL);
+          mousepad_window_open_file (window, li->data, MOUSEPAD_ENCODING_UTF_8);
 
           /* cleanup */
           g_free (li->data);
@@ -3214,15 +3231,15 @@ static void
 mousepad_window_action_open_recent (GtkAction      *action,
                                     MousepadWindow *window)
 {
-  const gchar   *uri;
-  const gchar   *encoding;
-  GError        *error = NULL;
-  gchar         *filename;
-  gboolean       succeed = FALSE;
-  GtkRecentInfo *info;
+  const gchar      *uri, *charset;
+  MousepadEncoding  encoding;
+  GError           *error = NULL;
+  gchar            *filename;
+  gboolean          succeed = FALSE;
+  GtkRecentInfo    *info;
 
-  _mousepad_return_if_fail (GTK_IS_ACTION (action));
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (GTK_IS_ACTION (action));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* get the info */
   info = mousepad_object_get_data (G_OBJECT (action), "gtk-recent-info");
@@ -3240,8 +3257,11 @@ mousepad_window_action_open_recent (GtkAction      *action,
           /* open the file in a new tab if it exists */
           if (g_file_test (filename, G_FILE_TEST_EXISTS))
             {
-              /* try to get the encoding from the recent description */
-              encoding = mousepad_window_recent_get_encoding (info);
+              /* try to get the charset from the description */
+              charset = mousepad_window_recent_get_charset (info);
+
+              /* lookup the encoding */
+              encoding = mousepad_encoding_find (charset);
 
               /* try to open the file */
               succeed = mousepad_window_open_file (window, filename, encoding);
@@ -3276,7 +3296,7 @@ static void
 mousepad_window_action_clear_recent (GtkAction      *action,
                                      MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* ask the user if he or she really want to clear the history */
   if (mousepad_dialogs_clear_recent (GTK_WINDOW (window)))
@@ -3307,8 +3327,8 @@ mousepad_window_action_save (GtkAction      *action,
   gboolean          modified;
   gint              response;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
 
   if (mousepad_file_get_filename (document->file) == NULL)
     {
@@ -3383,8 +3403,8 @@ mousepad_window_action_save_as (GtkAction      *action,
   GtkWidget        *dialog;
   gboolean          succeed = FALSE;
 
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
-  _mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_WINDOW (window), FALSE);
+  mousepad_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (window->active), FALSE);
 
   /* create the dialog */
   dialog = gtk_file_chooser_dialog_new (_("Save As"),
@@ -3435,15 +3455,15 @@ static void
 mousepad_window_action_save_all (GtkAction      *action,
                                  MousepadWindow *window)
 {
-  guint             i, current;
+  gint              i, current;
   gint              page_num;
   MousepadDocument *document;
   GSList           *li, *documents = NULL;
   gboolean          succeed = TRUE;
   GError           *error = NULL;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get the current active tab */
   current = gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook));
@@ -3455,7 +3475,7 @@ mousepad_window_action_save_all (GtkAction      *action,
       document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i));
 
       /* debug check */
-      _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+      mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
       /* continue if the document is not modified */
       if (!gtk_text_buffer_get_modified (document->buffer))
@@ -3543,8 +3563,8 @@ mousepad_window_action_revert (GtkAction      *action,
   gint              response;
   gboolean          succeed;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* ask the user if he really wants to do this when the file is modified */
   if (gtk_text_buffer_get_modified (document->buffer))
@@ -3565,7 +3585,7 @@ mousepad_window_action_revert (GtkAction      *action,
         }
 
       /* small check for debug builds */
-      _mousepad_return_if_fail (response == MOUSEPAD_RESPONSE_REVERT);
+      mousepad_return_if_fail (response == MOUSEPAD_RESPONSE_REVERT);
     }
 
   /* lock the undo manager */
@@ -3598,8 +3618,8 @@ mousepad_window_action_print (GtkAction      *action,
   GError           *error = NULL;
   gboolean          succeed;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* create new print operation */
   print = mousepad_print_new ();
@@ -3624,8 +3644,8 @@ static void
 mousepad_window_action_detach (GtkAction      *action,
                                MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
 #if GTK_CHECK_VERSION (2,12,0)
   /* invoke function without cooridinates */
@@ -3657,8 +3677,8 @@ static void
 mousepad_window_action_close (GtkAction      *action,
                               MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* close active document */
   mousepad_window_close_document (window, window->active);
@@ -3673,8 +3693,8 @@ mousepad_window_action_close_window (GtkAction      *action,
   gint       npages, i;
   GtkWidget *document;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get the number of page in the notebook */
   npages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook)) - 1;
@@ -3689,7 +3709,7 @@ mousepad_window_action_close_window (GtkAction      *action,
       document = gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i);
 
       /* check for debug builds */
-      _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+      mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
       /* focus the tab we're going to close */
       gtk_notebook_set_current_page (GTK_NOTEBOOK (window->notebook), i);
@@ -3718,8 +3738,8 @@ static void
 mousepad_window_action_undo (GtkAction      *action,
                              MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* undo */
   mousepad_undo_do_undo (window->active->undo);
@@ -3734,8 +3754,8 @@ static void
 mousepad_window_action_redo (GtkAction      *action,
                              MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* redo */
   mousepad_undo_do_redo (window->active->undo);
@@ -3752,8 +3772,8 @@ mousepad_window_action_cut (GtkAction      *action,
 {
   GtkEditable *entry;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get searchbar entry */
   entry = mousepad_search_bar_entry (MOUSEPAD_SEARCH_BAR (window->search_bar));
@@ -3776,8 +3796,8 @@ mousepad_window_action_copy (GtkAction      *action,
 {
   GtkEditable *entry;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get searchbar entry */
   entry = mousepad_search_bar_entry (MOUSEPAD_SEARCH_BAR (window->search_bar));
@@ -3800,8 +3820,8 @@ mousepad_window_action_paste (GtkAction      *action,
 {
   GtkEditable *entry;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get searchbar entry */
   entry = mousepad_search_bar_entry (MOUSEPAD_SEARCH_BAR (window->search_bar));
@@ -3821,8 +3841,8 @@ mousepad_window_action_paste_history (GtkAction      *action,
 {
   GtkWidget *menu;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get the history menu */
   menu = mousepad_window_paste_history_menu (window);
@@ -3842,8 +3862,8 @@ static void
 mousepad_window_action_paste_column (GtkAction      *action,
                                      MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* paste the clipboard into a column */
   mousepad_view_clipboard_paste (window->active->textview, NULL, TRUE);
@@ -3857,8 +3877,8 @@ mousepad_window_action_delete (GtkAction      *action,
 {
   GtkEditable *entry;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* get searchbar entry */
   entry = mousepad_search_bar_entry (MOUSEPAD_SEARCH_BAR (window->search_bar));
@@ -3876,8 +3896,8 @@ static void
 mousepad_window_action_select_all (GtkAction      *action,
                                    MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* select everything in the document */
   mousepad_view_select_all (window->active->textview);
@@ -3889,8 +3909,8 @@ static void
 mousepad_window_action_change_selection (GtkAction      *action,
                                          MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* change the selection */
   mousepad_view_change_selection (window->active->textview);
@@ -3902,8 +3922,8 @@ static void
 mousepad_window_action_find (GtkAction      *action,
                              MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* create a new search bar is needed */
   if (window->search_bar == NULL)
@@ -3930,8 +3950,8 @@ static void
 mousepad_window_action_find_next (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* find the next occurence */
   if (G_LIKELY (window->search_bar != NULL))
@@ -3944,8 +3964,8 @@ static void
 mousepad_window_action_find_previous (GtkAction      *action,
                                       MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* find the previous occurence */
   if (G_LIKELY (window->search_bar != NULL))
@@ -3956,9 +3976,9 @@ mousepad_window_action_find_previous (GtkAction      *action,
 static void
 mousepad_window_action_replace_switch_page (MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_REPLACE_DIALOG (window->replace_dialog));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_REPLACE_DIALOG (window->replace_dialog));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* page switched */
   mousepad_replace_dialog_page_switched (MOUSEPAD_REPLACE_DIALOG (window->replace_dialog));
@@ -3968,7 +3988,7 @@ mousepad_window_action_replace_switch_page (MousepadWindow *window)
 static void
 mousepad_window_action_replace_destroy (MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* disconnect tab switch signal */
   g_signal_handlers_disconnect_by_func (G_OBJECT (window->notebook), mousepad_window_action_replace_switch_page, window);
@@ -3982,8 +4002,8 @@ static void
 mousepad_window_action_replace (GtkAction      *action,
                                 MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   if (window->replace_dialog == NULL)
     {
@@ -4016,10 +4036,10 @@ mousepad_window_action_select_font (GtkAction      *action,
   GtkWidget        *dialog;
   MousepadDocument *document;
   gchar            *font_name;
-  guint             i;
+  gint              i;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   dialog = gtk_font_selection_dialog_new (_("Choose Mousepad Font"));
   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
@@ -4048,7 +4068,7 @@ mousepad_window_action_select_font (GtkAction      *action,
           document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i));
 
           /* debug check */
-          _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
+          mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
 
           /* set the font */
           mousepad_document_set_font (document, font_name);
@@ -4071,8 +4091,8 @@ static void
 mousepad_window_action_statusbar_overwrite (MousepadWindow *window,
                                             gboolean        overwrite)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* set the new overwrite mode */
   mousepad_document_set_overwrite (window->active, overwrite);
@@ -4086,7 +4106,7 @@ mousepad_window_action_statusbar (GtkToggleAction *action,
 {
   gboolean show_statusbar;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* whether we show the statusbar */
   show_statusbar = gtk_toggle_action_get_active (action);
@@ -4113,7 +4133,7 @@ mousepad_window_action_statusbar (GtkToggleAction *action,
       if (window->active)
         {
           /* debug check */
-          _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+          mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
           /* ask document to resend the cursor status signals */
           mousepad_document_send_signals (window->active);
@@ -4130,8 +4150,8 @@ static void
 mousepad_window_action_lowercase (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert selection to lowercase */
   mousepad_view_convert_selection_case (window->active->textview, LOWERCASE);
@@ -4143,8 +4163,8 @@ static void
 mousepad_window_action_uppercase (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert selection to uppercase */
   mousepad_view_convert_selection_case (window->active->textview, UPPERCASE);
@@ -4156,8 +4176,8 @@ static void
 mousepad_window_action_titlecase (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert selection to titlecase */
   mousepad_view_convert_selection_case (window->active->textview, TITLECASE);
@@ -4169,8 +4189,8 @@ static void
 mousepad_window_action_opposite_case (GtkAction      *action,
                                       MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert selection to opposite case */
   mousepad_view_convert_selection_case (window->active->textview, OPPOSITE_CASE);
@@ -4182,8 +4202,8 @@ static void
 mousepad_window_action_tabs_to_spaces (GtkAction      *action,
                                        MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert tabs to spaces */
   mousepad_view_convert_spaces_and_tabs (window->active->textview, TABS_TO_SPACES);
@@ -4195,8 +4215,8 @@ static void
 mousepad_window_action_spaces_to_tabs (GtkAction      *action,
                                        MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert spaces to tabs */
   mousepad_view_convert_spaces_and_tabs (window->active->textview, SPACES_TO_TABS);
@@ -4208,8 +4228,8 @@ static void
 mousepad_window_action_strip_trailing_spaces (GtkAction      *action,
                                               MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* convert spaces to tabs */
   mousepad_view_strip_trailing_spaces (window->active->textview);
@@ -4221,8 +4241,8 @@ static void
 mousepad_window_action_transpose (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* transpose */
   mousepad_view_transpose (window->active->textview);
@@ -4234,8 +4254,8 @@ static void
 mousepad_window_action_move_line_up (GtkAction      *action,
                                      MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* move the selection on line up */
   mousepad_view_move_selection (window->active->textview, MOVE_LINE_UP);
@@ -4247,8 +4267,8 @@ static void
 mousepad_window_action_move_line_down (GtkAction      *action,
                                        MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* move the selection on line down */
   mousepad_view_move_selection (window->active->textview, MOVE_LINE_DOWN);
@@ -4260,8 +4280,8 @@ static void
 mousepad_window_action_duplicate (GtkAction      *action,
                                   MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* dupplicate */
   mousepad_view_duplicate (window->active->textview);
@@ -4273,8 +4293,8 @@ static void
 mousepad_window_action_increase_indent (GtkAction      *action,
                                         MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* increase the indent */
   mousepad_view_indent (window->active->textview, INCREASE_INDENT);
@@ -4286,8 +4306,8 @@ static void
 mousepad_window_action_decrease_indent (GtkAction      *action,
                                         MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* decrease the indent */
   mousepad_view_indent (window->active->textview, DECREASE_INDENT);
@@ -4301,8 +4321,8 @@ mousepad_window_action_line_numbers (GtkToggleAction *action,
 {
   gboolean active;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0)
@@ -4326,8 +4346,8 @@ mousepad_window_action_word_wrap (GtkToggleAction *action,
 {
   gboolean active;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0)
@@ -4346,13 +4366,38 @@ mousepad_window_action_word_wrap (GtkToggleAction *action,
 
 
 static void
+mousepad_window_action_write_bom (GtkToggleAction *action,
+                                  MousepadWindow  *window)
+{
+  gboolean active;
+
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+
+  /* leave when menu updates are locked */
+  if (lock_menu_updates == 0)
+    {
+      /* get the current state */
+      active = gtk_toggle_action_get_active (action);
+
+      /* set new value */
+      mousepad_file_set_write_bom (window->active->file, active);
+
+      /* make buffer as modified to show the user the change is not saved */
+      gtk_text_buffer_set_modified (window->active->buffer, TRUE);
+    }
+}
+
+
+
+static void
 mousepad_window_action_auto_indent (GtkToggleAction *action,
                                     MousepadWindow  *window)
 {
   gboolean active;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0)
@@ -4376,12 +4421,12 @@ mousepad_window_action_tab_size (GtkToggleAction *action,
 {
   gboolean tab_size;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0 && gtk_toggle_action_get_active (action))
     {
-      _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+      mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
       /* get the tab size */
       tab_size = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
@@ -4415,8 +4460,8 @@ mousepad_window_action_insert_spaces (GtkToggleAction *action,
 {
   gboolean insert_spaces;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0)
@@ -4441,10 +4486,10 @@ mousepad_window_action_line_ending (GtkRadioAction *action,
 {
   MousepadLineEnding eol;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
-  _mousepad_return_if_fail (MOUSEPAD_IS_FILE (window->active->file));
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (window->active->buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (MOUSEPAD_IS_FILE (window->active->file));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (window->active->buffer));
 
   /* leave when menu updates are locked */
   if (lock_menu_updates == 0)
@@ -4470,7 +4515,7 @@ mousepad_window_action_prev_tab (GtkAction      *action,
   gint page_num;
   gint n_pages;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* get notebook info */
   page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook));
@@ -4489,7 +4534,7 @@ mousepad_window_action_next_tab (GtkAction      *action,
   gint page_num;
   gint n_pages;
 
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* get notebook info */
   page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook));
@@ -4507,9 +4552,9 @@ mousepad_window_action_go_to_tab (GtkRadioAction *action,
 {
   gint page;
 
-  _mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
-  _mousepad_return_if_fail (GTK_IS_RADIO_ACTION (action));
-  _mousepad_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
+  mousepad_return_if_fail (GTK_IS_NOTEBOOK (notebook));
+  mousepad_return_if_fail (GTK_IS_RADIO_ACTION (action));
+  mousepad_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
 
   /* leave when the menu is locked or this is not the active radio button */
   if (lock_menu_updates == 0
@@ -4529,9 +4574,9 @@ static void
 mousepad_window_action_go_to_position (GtkAction      *action,
                                        MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
-  _mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
-  _mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (window->active->buffer));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
+  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (window->active->buffer));
 
   /* run jump dialog */
   if (mousepad_dialogs_go_to (GTK_WINDOW (window), window->active->buffer))
@@ -4547,7 +4592,7 @@ static void
 mousepad_window_action_contents (GtkAction      *action,
                                  MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* show help */
   mousepad_dialogs_show_help (GTK_WINDOW (window), NULL, NULL);
@@ -4559,7 +4604,7 @@ static void
 mousepad_window_action_about (GtkAction      *action,
                               MousepadWindow *window)
 {
-  _mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
+  mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
 
   /* show about dialog */
   mousepad_dialogs_show_about (GTK_WINDOW (window));
diff --git a/mousepad/mousepad-window.h b/mousepad/mousepad-window.h
index 9a7204d..cf68bc4 100644
--- a/mousepad/mousepad-window.h
+++ b/mousepad/mousepad-window.h
@@ -1,4 +1,3 @@
-/* $Id$ */
 /*
  * 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 the Free
@@ -32,13 +31,13 @@ G_BEGIN_DECLS
 enum
 {
   TARGET_TEXT_URI_LIST,
-  TARGET_GTK_NOTEBOOK_TAB,
+  TARGET_GTK_NOTEBOOK_TAB
 };
 
 static const GtkTargetEntry drop_targets[] =
 {
-  { "text/uri-list", 0, TARGET_TEXT_URI_LIST },
-  { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, TARGET_GTK_NOTEBOOK_TAB },
+  { (gchar *) "text/uri-list", 0, TARGET_TEXT_URI_LIST },
+  { (gchar *) "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, TARGET_GTK_NOTEBOOK_TAB }
 };
 
 typedef struct _MousepadWindowClass MousepadWindowClass;


More information about the Xfce4-commits mailing list