]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
more const correctness
[lyx.git] / src / Buffer.cpp
index 504bacdb9a4e1eee96d77f6463baafe2ab0e3578..12e3400f7c2ee402308bd17f9a81878e596c9a93 100644 (file)
 #include "Chktex.h"
 #include "Converter.h"
 #include "Counters.h"
+#include "Cursor.h"
+#include "CutAndPaste.h"
 #include "DispatchResult.h"
 #include "DocIterator.h"
-#include "Encoding.h"
+#include "BufferEncodings.h"
 #include "ErrorList.h"
 #include "Exporter.h"
 #include "Format.h"
@@ -56,7 +58,6 @@
 #include "SpellChecker.h"
 #include "sgml.h"
 #include "TexRow.h"
-#include "TexStream.h"
 #include "Text.h"
 #include "TextClass.h"
 #include "TocBackend.h"
 #include "support/gzstream.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
+#include "support/mutex.h"
 #include "support/os.h"
 #include "support/Package.h"
-#include "support/Path.h"
+#include "support/PathChanger.h"
 #include "support/Systemcall.h"
+#include "support/TempFile.h"
 #include "support/textutils.h"
 #include "support/types.h"
 
 #include "support/bind.h"
-#include "support/shared_ptr.h"
 
 #include <algorithm>
 #include <fstream>
 #include <iomanip>
 #include <map>
+#include <memory>
 #include <set>
 #include <sstream>
-#include <stack>
 #include <vector>
 
 using namespace std;
@@ -131,17 +133,13 @@ int const LYX_FORMAT = LYX_FORMAT_LYX;
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
 
-void showPrintError(string const & name)
-{
-       docstring str = bformat(_("Could not print the document %1$s.\n"
-                                           "Check that your printer is set up correctly."),
-                            makeDisplayPath(name, 50));
-       Alert::error(_("Print document failed"), str);
-}
-
 } // namespace anon
 
 
+// A storehouse for the cloned buffers.
+list<CloneList *> cloned_buffers;
+
+
 class Buffer::Impl
 {
 public:
@@ -156,7 +154,7 @@ public:
                }
                delete inset;
        }
-       
+
        /// search for macro in local (buffer) table or in children
        MacroData const * getBufferMacro(docstring const & name,
                DocIterator const & pos) const;
@@ -191,6 +189,9 @@ public:
        /// is this an unnamed file (New...)?
        bool unnamed;
 
+       /// is this an internal bufffer?
+       bool internal_buffer;
+
        /// buffer is r/o
        bool read_only;
 
@@ -203,11 +204,27 @@ public:
         */
        bool file_fully_loaded;
 
+       /// original format of loaded file
+       int file_format;
+
+       /// if the file was originally loaded from an older format, do
+       /// we need to back it up still?
+       bool need_format_backup;
+
+       /// Ignore the parent (e.g. when exporting a child standalone)?
+       bool ignore_parent;
+
        ///
        mutable TocBackend toc_backend;
 
        /// macro tables
-       typedef pair<DocIterator, MacroData> ScopeMacro;
+       struct ScopeMacro {
+               ScopeMacro() {}
+               ScopeMacro(DocIterator const & s, MacroData const & m)
+                       : scope(s), macro(m) {}
+               DocIterator scope;
+               MacroData macro;
+       };
        typedef map<DocIterator, ScopeMacro> PositionScopeMacroMap;
        typedef map<docstring, PositionScopeMacroMap> NamePositionScopeMacroMap;
        /// map from the macro name to the position map,
@@ -218,13 +235,29 @@ public:
 
        /// positions of child buffers in the buffer
        typedef map<Buffer const * const, DocIterator> BufferPositionMap;
-       typedef pair<DocIterator, Buffer const *> ScopeBuffer;
+       struct ScopeBuffer {
+               ScopeBuffer() : buffer(0) {}
+               ScopeBuffer(DocIterator const & s, Buffer const * b)
+                       : scope(s), buffer(b) {}
+               DocIterator scope;
+               Buffer const * buffer;
+       };
        typedef map<DocIterator, ScopeBuffer> PositionScopeBufferMap;
        /// position of children buffers in this buffer
        BufferPositionMap children_positions;
        /// map from children inclusion positions to their scope and their buffer
        PositionScopeBufferMap position_to_children;
 
+       /// Contains the old buffer filePath() while saving-as, or the
+       /// directory where the document was last saved while loading.
+       string old_position;
+
+       /** Keeps track of the path of local layout files.
+        *  If possible, it is always relative to the buffer path.
+        *  Empty for layouts in system or user directory.
+        */
+       string layout_position;
+
        /// Container for all sort of Buffer dependant errors.
        map<string, ErrorList> errorLists;
 
@@ -262,6 +295,15 @@ public:
        /// to be updated.
        mutable bool cite_labels_valid_;
 
+       /// These two hold the file name and format, written to by
+       /// Buffer::preview and read from by LFUN_BUFFER_VIEW_CACHE.
+       FileName preview_file_;
+       string preview_format_;
+       /// If there was an error when previewing, on the next preview we do
+       /// a fresh compile (e.g. in case the user installed a package that
+       /// was missing).
+       bool preview_error_;
+
        mutable RefCache ref_cache_;
 
        /// our Text that should be wrapped in an InsetText
@@ -272,7 +314,12 @@ public:
 
        /// This is here to force the test to be done whenever parent_buffer
        /// is accessed.
-       Buffer const * parent() const { 
+       Buffer const * parent() const
+       {
+               // ignore_parent temporarily "orphans" a buffer
+               // (e.g. if a child is compiled standalone)
+               if (ignore_parent)
+                       return 0;
                // if parent_buffer is not loaded, then it has been unloaded,
                // which means that parent_buffer is an invalid pointer. So we
                // set it to null in that case.
@@ -281,11 +328,12 @@ public:
                // for the best.
                if (!cloned_buffer_ && !theBufferList().isLoaded(parent_buffer))
                        parent_buffer = 0;
-               return parent_buffer; 
+               return parent_buffer;
        }
-       
+
        ///
-       void setParent(Buffer const * pb) {
+       void setParent(Buffer const * pb)
+       {
                if (parent_buffer == pb)
                        // nothing to do
                        return;
@@ -301,25 +349,59 @@ public:
        /// If non zero, this buffer is a clone of existing buffer \p cloned_buffer_
        /// This one is useful for preview detached in a thread.
        Buffer const * cloned_buffer_;
+       ///
+       CloneList * clone_list_;
        /// are we in the process of exporting this buffer?
        mutable bool doing_export;
-       
+
+       /// compute statistics
+       /// \p from initial position
+       /// \p to points to the end position
+       void updateStatistics(DocIterator & from, DocIterator & to,
+                             bool skipNoOutput = true);
+       /// statistics accessor functions
+       int wordCount() const
+       {
+               return word_count_;
+       }
+       int charCount(bool with_blanks) const
+       {
+               return char_count_
+               + (with_blanks ? blank_count_ : 0);
+       }
+
+       // does the buffer contain tracked changes? (if so, we automatically
+       // display the review toolbar, for instance)
+       mutable bool tracked_changes_present_;
+
 private:
        /// So we can force access via the accessors.
        mutable Buffer const * parent_buffer;
 
+       int word_count_;
+       int char_count_;
+       int blank_count_;
+
 };
 
 
 /// Creates the per buffer temporary directory
 static FileName createBufferTmpDir()
 {
-       static int count;
+       // FIXME This would be the ideal application for a TempDir class (like
+       //       TempFile but for directories)
+       string counter;
+       {
+               static int count;
+               static Mutex mutex;
+               Mutex::Locker locker(&mutex);
+               counter = convert<string>(count++);
+       }
        // We are in our own directory.  Why bother to mangle name?
        // In fact I wrote this code to circumvent a problematic behaviour
        // (bug?) of EMX mkstemp().
        FileName tmpfl(package().temp_dir().absFileName() + "/lyx_tmpbuf" +
-               convert<string>(count++));
+               counter);
 
        if (!tmpfl.createDirectory(0777)) {
                throw ExceptionMessage(WarningException, _("Disk Error: "), bformat(
@@ -333,12 +415,14 @@ static FileName createBufferTmpDir()
 Buffer::Impl::Impl(Buffer * owner, FileName const & file, bool readonly_,
        Buffer const * cloned_buffer)
        : owner_(owner), lyx_clean(true), bak_clean(true), unnamed(false),
-         read_only(readonly_), filename(file), file_fully_loaded(false),
-         toc_backend(owner), macro_lock(false), timestamp_(0), checksum_(0),
-         wa_(0), gui_(0), undo_(*owner), bibinfo_cache_valid_(false), 
-         bibfile_cache_valid_(false), cite_labels_valid_(false), 
-         preview_loader_(0), cloned_buffer_(cloned_buffer),
-         doing_export(false), parent_buffer(0)
+         internal_buffer(false), read_only(readonly_), filename(file),
+         file_fully_loaded(false), file_format(LYX_FORMAT), need_format_backup(false),
+         ignore_parent(false),  toc_backend(owner), macro_lock(false), timestamp_(0),
+         checksum_(0), wa_(0),  gui_(0), undo_(*owner), bibinfo_cache_valid_(false),
+         bibfile_cache_valid_(false), cite_labels_valid_(false), inset(0),
+         preview_loader_(0), cloned_buffer_(cloned_buffer), clone_list_(0),
+         doing_export(false), parent_buffer(0),
+         word_count_(0), char_count_(0), blank_count_(0)
 {
        if (!cloned_buffer_) {
                temppath = createBufferTmpDir();
@@ -357,6 +441,12 @@ Buffer::Impl::Impl(Buffer * owner, FileName const & file, bool readonly_,
        bibfile_status_ = cloned_buffer_->d->bibfile_status_;
        cite_labels_valid_ = cloned_buffer_->d->cite_labels_valid_;
        unnamed = cloned_buffer_->d->unnamed;
+       internal_buffer = cloned_buffer_->d->internal_buffer;
+       layout_position = cloned_buffer_->d->layout_position;
+       preview_file_ = cloned_buffer_->d->preview_file_;
+       preview_format_ = cloned_buffer_->d->preview_format_;
+       preview_error_ = cloned_buffer_->d->preview_error_;
+       tracked_changes_present_ = cloned_buffer_->d->tracked_changes_present_;
 }
 
 
@@ -375,7 +465,6 @@ Buffer::Buffer(string const & file, bool readonly, Buffer const * cloned_buffer)
                        it.paragraph().setId(cloned_it.paragraph().id());
        } else
                d->inset = new InsetText(this);
-       d->inset->setAutoBreakRows(true);
        d->inset->getText(0)->setMacrocontextPosition(par_iterator_begin());
 }
 
@@ -395,61 +484,113 @@ Buffer::~Buffer()
                return;
        }
 
-       // loop over children
-       Impl::BufferPositionMap::iterator it = d->children_positions.begin();
-       Impl::BufferPositionMap::iterator end = d->children_positions.end();
-       for (; it != end; ++it) {
-               Buffer * child = const_cast<Buffer *>(it->first);
-               if (d->cloned_buffer_)
-                       delete child;
-               // The child buffer might have been closed already.
-               else if (theBufferList().isLoaded(child))
-                       theBufferList().releaseChild(this, child);
-       }
+       if (isClone()) {
+               // this is in case of recursive includes: we won't try to delete
+               // ourselves as a child.
+               d->clone_list_->erase(this);
+               // loop over children
+               Impl::BufferPositionMap::iterator it = d->children_positions.begin();
+               Impl::BufferPositionMap::iterator end = d->children_positions.end();
+               for (; it != end; ++it) {
+                       Buffer * child = const_cast<Buffer *>(it->first);
+                               if (d->clone_list_->erase(child))
+                                       delete child;
+               }
+               // if we're the master buffer, then we should get rid of the list
+               // of clones
+               if (!parent()) {
+                       // If this is not empty, we have leaked something. Worse, one of the
+                       // children still has a reference to this list. But we will try to
+                       // continue, rather than shut down.
+                       LATTEST(d->clone_list_->empty());
+                       list<CloneList *>::iterator it =
+                               find(cloned_buffers.begin(), cloned_buffers.end(), d->clone_list_);
+                       if (it == cloned_buffers.end()) {
+                               // We will leak in this case, but it is safe to continue.
+                               LATTEST(false);
+                       } else
+                               cloned_buffers.erase(it);
+                       delete d->clone_list_;
+               }
+               // FIXME Do we really need to do this right before we delete d?
+               // clear references to children in macro tables
+               d->children_positions.clear();
+               d->position_to_children.clear();
+       } else {
+               // loop over children
+               Impl::BufferPositionMap::iterator it = d->children_positions.begin();
+               Impl::BufferPositionMap::iterator end = d->children_positions.end();
+               for (; it != end; ++it) {
+                       Buffer * child = const_cast<Buffer *>(it->first);
+                       if (theBufferList().isLoaded(child)) { 
+                        if (theBufferList().isOthersChild(this, child))
+                                child->setParent(0);
+                        else
+                               theBufferList().release(child);
+                       }
+               }
 
-       if (!isClean()) {
-               docstring msg = _("LyX attempted to close a document that had unsaved changes!\n");
-               msg += emergencyWrite();
-               Alert::warning(_("Attempting to close changed document!"), msg);
-       }
-               
-       // clear references to children in macro tables
-       d->children_positions.clear();
-       d->position_to_children.clear();
+               if (!isClean()) {
+                       docstring msg = _("LyX attempted to close a document that had unsaved changes!\n");
+                       msg += emergencyWrite();
+                       Alert::warning(_("Attempting to close changed document!"), msg);
+               }
 
-       if (!d->cloned_buffer_ && !d->temppath.destroyDirectory()) {
-               Alert::warning(_("Could not remove temporary directory"),
-                       bformat(_("Could not remove the temporary directory %1$s"),
-                       from_utf8(d->temppath.absFileName())));
-       }
+               // FIXME Do we really need to do this right before we delete d?
+               // clear references to children in macro tables
+               d->children_positions.clear();
+               d->position_to_children.clear();
 
-       if (!isClone())
+               if (!d->temppath.destroyDirectory()) {
+                       LYXERR0(bformat(_("Could not remove the temporary directory %1$s"),
+                               from_utf8(d->temppath.absFileName())));
+               }
                removePreviews();
+       }
 
        delete d;
 }
 
 
-Buffer * Buffer::clone() const
+Buffer * Buffer::cloneFromMaster() const
 {
        BufferMap bufmap;
-       masterBuffer()->clone(bufmap);
-       BufferMap::iterator it = bufmap.find(this);
-       LASSERT(it != bufmap.end(), return 0);
-       return it->second;
+       cloned_buffers.push_back(new CloneList);
+       CloneList * clones = cloned_buffers.back();
+
+       masterBuffer()->cloneWithChildren(bufmap, clones);
+
+       // make sure we got cloned
+       BufferMap::const_iterator bit = bufmap.find(this);
+       LASSERT(bit != bufmap.end(), return 0);
+       Buffer * cloned_buffer = bit->second;
+
+       return cloned_buffer;
 }
 
 
-void Buffer::clone(BufferMap & bufmap) const
+void Buffer::cloneWithChildren(BufferMap & bufmap, CloneList * clones) const
 {
        // have we already been cloned?
        if (bufmap.find(this) != bufmap.end())
                return;
 
        Buffer * buffer_clone = new Buffer(fileName().absFileName(), false, this);
+
+       // The clone needs its own DocumentClass, since running updateBuffer() will
+       // modify it, and we would otherwise be sharing it with the original Buffer.
+       buffer_clone->params().makeDocumentClass(true);
+       ErrorList el;
+       cap::switchBetweenClasses(
+                       params().documentClassPtr(), buffer_clone->params().documentClassPtr(),
+                       static_cast<InsetText &>(buffer_clone->inset()), el);
+
        bufmap[this] = buffer_clone;
+       clones->insert(buffer_clone);
+       buffer_clone->d->clone_list_ = clones;
        buffer_clone->d->macro_lock = true;
        buffer_clone->d->children_positions.clear();
+
        // FIXME (Abdel 09/01/2010): this is too complicated. The whole children_positions and
        // math macro caches need to be rethought and simplified.
        // I am not sure wether we should handle Buffer cloning here or in BufferList.
@@ -459,9 +600,9 @@ void Buffer::clone(BufferMap & bufmap) const
        for (; it != end; ++it) {
                DocIterator dit = it->first.clone(buffer_clone);
                dit.setBuffer(buffer_clone);
-               Buffer * child = const_cast<Buffer *>(it->second.second);
+               Buffer * child = const_cast<Buffer *>(it->second.buffer);
 
-               child->clone(bufmap);
+               child->cloneWithChildren(bufmap, clones);
                BufferMap::iterator const bit = bufmap.find(child);
                LASSERT(bit != bufmap.end(), continue);
                Buffer * child_clone = bit->second;
@@ -479,6 +620,28 @@ void Buffer::clone(BufferMap & bufmap) const
 }
 
 
+Buffer * Buffer::cloneBufferOnly() const {
+       cloned_buffers.push_back(new CloneList);
+       CloneList * clones = cloned_buffers.back();
+       Buffer * buffer_clone = new Buffer(fileName().absFileName(), false, this);
+
+       // The clone needs its own DocumentClass, since running updateBuffer() will
+       // modify it, and we would otherwise be sharing it with the original Buffer.
+       buffer_clone->params().makeDocumentClass(true);
+       ErrorList el;
+       cap::switchBetweenClasses(
+                       params().documentClassPtr(), buffer_clone->params().documentClassPtr(),
+                       static_cast<InsetText &>(buffer_clone->inset()), el);
+
+       clones->insert(buffer_clone);
+       buffer_clone->d->clone_list_ = clones;
+
+       // we won't be cloning the children
+       buffer_clone->d->children_positions.clear();
+       return buffer_clone;
+}
+
+
 bool Buffer::isClone() const
 {
        return d->cloned_buffer_;
@@ -494,7 +657,7 @@ void Buffer::changed(bool update_metrics) const
 
 frontend::WorkAreaManager & Buffer::workAreaManager() const
 {
-       LASSERT(d->wa_, /**/);
+       LBUFERR(d->wa_);
        return *d->wa_;
 }
 
@@ -523,6 +686,28 @@ BufferParams const & Buffer::params() const
 }
 
 
+BufferParams const & Buffer::masterParams() const
+{
+       if (masterBuffer() == this)
+               return params();
+
+       BufferParams & mparams = const_cast<Buffer *>(masterBuffer())->params();
+       // Copy child authors to the params. We need those pointers.
+       AuthorList const & child_authors = params().authors();
+       AuthorList::Authors::const_iterator it = child_authors.begin();
+       for (; it != child_authors.end(); ++it)
+               mparams.authors().record(*it);
+       return mparams;
+}
+
+
+double Buffer::fontScalingFactor() const
+{
+       return isExporting() ? 75.0 * params().html_math_img_scale
+               : 0.01 * lyxrc.dpi * lyxrc.zoom * lyxrc.preview_scale_factor * params().display_pixel_ratio;
+}
+
+
 ParagraphList & Buffer::paragraphs()
 {
        return text().paragraphs();
@@ -632,7 +817,7 @@ string Buffer::logName(LogType * type) const
 
        // Also consider the master buffer log file
        FileName masterfname = fname;
-       LogType mtype;
+       LogType mtype = latexlog;
        if (masterBuffer() != this) {
                string const mlogfile = masterBuffer()->logName(&mtype);
                masterfname = FileName(mlogfile);
@@ -674,7 +859,10 @@ void Buffer::setReadonly(bool const flag)
 
 void Buffer::setFileName(FileName const & fname)
 {
+       bool const changed = fname != d->filename;
        d->filename = fname;
+       if (changed)
+               lyxvc().file_found_hook(fname);
        setReadonly(d->filename.isReadOnly());
        saveCheckSum();
        updateTitles();
@@ -720,6 +908,8 @@ int Buffer::readHeader(Lexer & lex)
        params().html_latex_end.clear();
        params().html_math_img_scale = 1.0;
        params().output_sync_macro.erase();
+       params().setLocalLayout(string(), false);
+       params().setLocalLayout(string(), true);
 
        for (int i = 0; i < 4; ++i) {
                params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
@@ -747,12 +937,11 @@ int Buffer::readHeader(Lexer & lex)
                LYXERR(Debug::PARSER, "Handling document header token: `"
                                      << token << '\'');
 
-               string unknown = params().readToken(lex, token, d->filename.onlyPath());
-               if (!unknown.empty()) {
-                       if (unknown[0] != '\\' && token == "\\textclass") {
-                               Alert::warning(_("Unknown document class"),
-                      bformat(_("Using the default document class, because the "
-                                             "class %1$s is unknown."), from_utf8(unknown)));
+               string const result =
+                       params().readToken(lex, token, d->filename.onlyPath());
+               if (!result.empty()) {
+                       if (token == "\\textclass") {
+                               d->layout_position = result;
                        } else {
                                ++unknown_tokens;
                                docstring const s = bformat(_("Unknown token: "
@@ -795,7 +984,7 @@ bool Buffer::readDocument(Lexer & lex)
 
        readHeader(lex);
 
-       if (params().outputChanges) {
+       if (params().output_changes) {
                bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
                bool xcolorulem = LaTeXFeatures::isAvailable("ulem") &&
                                  LaTeXFeatures::isAvailable("xcolor");
@@ -815,11 +1004,11 @@ bool Buffer::readDocument(Lexer & lex)
                }
        }
 
-       if (!params().master.empty()) {
+       if (!parent() && !params().master.empty()) {
                FileName const master_file = makeAbsPath(params().master,
                           onlyPath(absFileName()));
                if (isLyXFileName(master_file.absFileName())) {
-                       Buffer * master = 
+                       Buffer * master =
                                checkAndLoadLyXFile(master_file, true);
                        if (master) {
                                // necessary e.g. after a reload
@@ -844,12 +1033,17 @@ bool Buffer::readDocument(Lexer & lex)
                        }
                }
        }
-       
+
        // assure we have a default index
        params().indiceslist().addDefault(B_("Index"));
 
        // read main text
+       if (FileName::isAbsolute(params().origin))
+               d->old_position = params().origin;
+       else
+               d->old_position = filePath();
        bool const res = text().read(lex, errorList, d->inset);
+       d->old_position.clear();
 
        // inform parent buffer about local macros
        if (parent()) {
@@ -866,6 +1060,50 @@ bool Buffer::readDocument(Lexer & lex)
 }
 
 
+bool Buffer::importString(string const & format, docstring const & contents, ErrorList & errorList)
+{
+       Format const * fmt = formats.getFormat(format);
+       if (!fmt)
+               return false;
+       // It is important to use the correct extension here, since some
+       // converters create a wrong output file otherwise (e.g. html2latex)
+       TempFile const tempfile("Buffer_importStringXXXXXX." + fmt->extension());
+       FileName const name(tempfile.name());
+       ofdocstream os(name.toFilesystemEncoding().c_str());
+       // Do not convert os implicitly to bool, since that is forbidden in C++11.
+       bool const success = !(os << contents).fail();
+       os.close();
+
+       bool converted = false;
+       if (success) {
+               params().compressed = false;
+
+               // remove dummy empty par
+               paragraphs().clear();
+
+               converted = importFile(format, name, errorList);
+       }
+
+       if (name.exists())
+               name.removeFile();
+       return converted;
+}
+
+
+bool Buffer::importFile(string const & format, FileName const & name, ErrorList & errorList)
+{
+       if (!theConverters().isReachable(format, "lyx"))
+               return false;
+
+       TempFile const tempfile("Buffer_importFileXXXXXX.lyx");
+       FileName const lyx(tempfile.name());
+       if (theConverters().convert(0, name, lyx, name, format, "lyx", errorList))
+               return readFile(lyx) == ReadSuccess;
+
+       return false;
+}
+
+
 bool Buffer::readString(string const & s)
 {
        params().compressed = false;
@@ -873,25 +1111,25 @@ bool Buffer::readString(string const & s)
        Lexer lex;
        istringstream is(s);
        lex.setStream(is);
-       FileName const fn = FileName::tempName("Buffer_readString");
+       TempFile tempfile("Buffer_readStringXXXXXX.lyx");
+       FileName const fn = tempfile.name();
 
        int file_format;
-       ReadStatus const ret_plf = parseLyXFormat(lex, fn, file_format);
-       if (ret_plf != ReadSuccess)
-               return ret_plf;
+       bool success = parseLyXFormat(lex, fn, file_format) == ReadSuccess;
 
-       if (file_format != LYX_FORMAT) {
+       if (success && file_format != LYX_FORMAT) {
                // We need to call lyx2lyx, so write the input to a file
                ofstream os(fn.toFilesystemEncoding().c_str());
                os << s;
                os.close();
                // lyxvc in readFile
-               return readFile(fn) == ReadSuccess;
+               if (readFile(fn) != ReadSuccess)
+                       success = false;
        }
-
-       if (readDocument(lex))
-               return false;
-       return true;
+       else if (success)
+               if (readDocument(lex))
+                       success = false;
+       return success;
 }
 
 
@@ -901,7 +1139,7 @@ Buffer::ReadStatus Buffer::readFile(FileName const & fn)
        Lexer lex;
        if (!lex.setFile(fname)) {
                Alert::error(_("File Not Found"),
-                       bformat(_("Unable to open file `%1$s'."), 
+                       bformat(_("Unable to open file `%1$s'."),
                                from_utf8(fn.absFileName())));
                return ReadFileNotFound;
        }
@@ -913,13 +1151,18 @@ Buffer::ReadStatus Buffer::readFile(FileName const & fn)
 
        if (file_format != LYX_FORMAT) {
                FileName tmpFile;
-               ReadStatus const ret_clf = convertLyXFormat(fn, tmpFile, file_format);
+               ReadStatus ret_clf = convertLyXFormat(fn, tmpFile, file_format);
                if (ret_clf != ReadSuccess)
                        return ret_clf;
-               return readFile(tmpFile);
+               ret_clf = readFile(tmpFile);
+               if (ret_clf == ReadSuccess) {
+                       d->file_format = file_format;
+                       d->need_format_backup = true;
+               }
+               return ret_clf;
        }
 
-       // FIXME: InsetInfo needs to know whether the file is under VCS 
+       // FIXME: InsetInfo needs to know whether the file is under VCS
        // during the parse process, so this has to be done before.
        lyxvc().file_found_hook(d->filename);
 
@@ -933,7 +1176,7 @@ Buffer::ReadStatus Buffer::readFile(FileName const & fn)
 
        d->file_fully_loaded = true;
        d->read_only = !d->filename.isWritable();
-       params().compressed = d->filename.isZippedFile();
+       params().compressed = formats.isZippedFile(d->filename);
        saveCheckSum();
        return ReadSuccess;
 }
@@ -951,6 +1194,12 @@ void Buffer::setFullyLoaded(bool value)
 }
 
 
+bool Buffer::lastPreviewError() const
+{
+       return d->preview_error_;
+}
+
+
 PreviewLoader * Buffer::loader() const
 {
        if (!isExporting() && lyxrc.preview == LyXRC::PREVIEW_OFF)
@@ -991,7 +1240,7 @@ Buffer::ReadStatus Buffer::parseLyXFormat(Lexer & lex,
                        bformat(_("%1$s is not a readable LyX document."),
                                from_utf8(fn.absFileName())));
                return ReadNoLyXFormat;
-       }       
+       }
 
        string tmp_format;
        lex >> tmp_format;
@@ -1008,10 +1257,12 @@ Buffer::ReadStatus Buffer::parseLyXFormat(Lexer & lex,
 }
 
 
-Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn, 
+Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn,
        FileName & tmpfile, int from_format)
 {
-       tmpfile = FileName::tempName("Buffer_convertLyXFormat");
+       TempFile tempfile("Buffer_convertLyXFormatXXXXXX.lyx");
+       tempfile.setAutoRemove(false);
+       tmpfile = tempfile.name();
        if(tmpfile.empty()) {
                Alert::error(_("Conversion failed"),
                        bformat(_("%1$s is from a different"
@@ -1067,6 +1318,41 @@ Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn,
 }
 
 
+FileName Buffer::getBackupName() const {
+       FileName const & fn = fileName();
+       string const fname = fn.onlyFileNameWithoutExt();
+       string const fext  = fn.extension();
+       string const fpath = lyxrc.backupdir_path.empty() ?
+               fn.onlyPath().absFileName() :
+               lyxrc.backupdir_path;
+       string const fform = convert<string>(d->file_format);
+       string const backname = fname + "-lyxformat-" + fform;
+       FileName backup(addName(fpath, addExtension(backname, fext)));
+
+       // limit recursion, just in case
+       int v = 1;
+       unsigned long orig_checksum = 0;
+       while (backup.exists() && v < 100) {
+               if (orig_checksum == 0)
+                       orig_checksum = fn.checksum();
+               unsigned long new_checksum = backup.checksum();
+               if (orig_checksum == new_checksum) {
+                       LYXERR(Debug::FILES, "Not backing up " << fn <<
+                              "since " << backup << "has the same checksum.");
+                       // a bit of a hack, but we have to check this anyway
+                       // below, and setting this is simpler than introducing
+                       // a special boolean for this purpose.
+                       v = 1000;
+                       break;
+               }
+               string const newbackname = backname + "-" + convert<string>(v);
+               backup.set(addName(fpath, addExtension(newbackname, fext)));
+               v++;
+       }
+       return v < 100 ? backup : FileName();
+}
+
+
 // Should probably be moved to somewhere else: BufferView? GuiView?
 bool Buffer::save() const
 {
@@ -1086,7 +1372,7 @@ bool Buffer::save() const
 
        // ask if the disk file has been externally modified (use checksum method)
        if (fileName().exists() && isExternallyModified(checksum_method)) {
-               docstring text = 
+               docstring text =
                        bformat(_("Document %1$s has been externally modified. "
                                "Are you sure you want to overwrite this file?"), file);
                int const ret = Alert::prompt(_("Overwrite modified file?"),
@@ -1098,54 +1384,120 @@ bool Buffer::save() const
        // We don't need autosaves in the immediate future. (Asger)
        resetAutosaveTimers();
 
+       // if the file does not yet exist, none of the backup activity
+       // that follows is necessary
+  if (!fileName().exists()) {
+               if (!writeFile(fileName()))
+      return false;
+    markClean();
+    return true;
+  }
+
+       // we first write the file to a new name, then move it to its
+       // proper location once that has been done successfully. that
+       // way we preserve the original file if something goes wrong.
+       string const justname = fileName().onlyFileNameWithoutExt();
+       auto tempfile = make_unique<TempFile>(fileName().onlyPath(),
+                                             justname + "-XXXXXX.lyx");
+       bool const symlink = fileName().isSymLink();
+       if (!symlink)
+               tempfile->setAutoRemove(false);
+
+       FileName savefile(tempfile->name());
+       LYXERR(Debug::FILES, "Saving to " << savefile.absFileName());
+       if (!writeFile(savefile))
+               return false;
+
+       // we will set this to false if we fail
+       bool made_backup = true;
+
        FileName backupName;
-       bool madeBackup = false;
-
-       // make a backup if the file already exists
-       if (lyxrc.make_backup && fileName().exists()) {
-               backupName = FileName(absFileName() + '~');
-               if (!lyxrc.backupdir_path.empty()) {
-                       string const mangledName =
-                               subst(subst(backupName.absFileName(), '/', '!'), ':', '!');
-                       backupName = FileName(addName(lyxrc.backupdir_path,
-                                                     mangledName));
+       bool const needBackup = lyxrc.make_backup || d->need_format_backup;
+       if (needBackup) {
+               if (d->need_format_backup)
+                       backupName = getBackupName();
+
+               // If we for some reason failed to find a backup name in case of
+               // a format change, this will still set one. It's the best we can
+               // do in this case.
+               if (backupName.empty()) {
+                       backupName.set(fileName().absFileName() + "~");
+                       if (!lyxrc.backupdir_path.empty()) {
+                               string const mangledName =
+                                       subst(subst(backupName.absFileName(), '/', '!'), ':', '!');
+                               backupName.set(addName(lyxrc.backupdir_path, mangledName));
+                       }
                }
 
+               LYXERR(Debug::FILES, "Backing up original file to " <<
+                               backupName.absFileName());
                // Except file is symlink do not copy because of #6587.
                // Hard links have bad luck.
-               if (fileName().isSymLink())
-                       madeBackup = fileName().copyTo(backupName);
-               else
-                       madeBackup = fileName().moveTo(backupName);
+               made_backup = symlink ?
+                       fileName().copyTo(backupName):
+                       fileName().moveTo(backupName);
 
-               if (!madeBackup) {
+               if (!made_backup) {
                        Alert::error(_("Backup failure"),
                                     bformat(_("Cannot create backup file %1$s.\n"
                                               "Please check whether the directory exists and is writable."),
                                             from_utf8(backupName.absFileName())));
                        //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
+               } else if (d->need_format_backup) {
+                       // the original file has been backed up successfully, so we
+                       // will not need to do that again
+                       d->need_format_backup = false;
                }
        }
 
-       if (writeFile(d->filename)) {
+       // Destroy tempfile since it keeps the file locked on windows (bug 9234)
+       // Only do this if tempfile is not in autoremove mode
+       if (!symlink)
+               tempfile.reset();
+       // If we have no symlink, we can simply rename the temp file.
+       // Otherwise, we need to copy it so the symlink stays intact.
+       if (made_backup && symlink ? savefile.copyTo(fileName(), true) :
+                                          savefile.moveTo(fileName()))
+       {
+               // saveCheckSum() was already called by writeFile(), but the
+               // time stamp is invalidated by copying/moving
+               saveCheckSum();
                markClean();
+               if (d->file_format != LYX_FORMAT)
+                       // the file associated with this buffer is now in the current format
+                       d->file_format = LYX_FORMAT;
                return true;
+       }
+       // else we saved the file, but failed to move it to the right location.
+
+       if (needBackup && made_backup && !symlink) {
+               // the original file was moved to some new location, so it will look
+               // to the user as if it was deleted. (see bug #9234.) we could try
+               // to restore it, but that would basically mean trying to do again
+               // what we just failed to do. better to leave things as they are.
+               Alert::error(_("Write failure"),
+                            bformat(_("The file has successfully been saved as:\n  %1$s.\n"
+                                      "But LyX could not move it to:\n  %2$s.\n"
+                                      "Your original file has been backed up to:\n  %3$s"),
+                                    from_utf8(savefile.absFileName()),
+                                    from_utf8(fileName().absFileName()),
+                                    from_utf8(backupName.absFileName())));
        } else {
-               // Saving failed, so backup is not backup
-               if (madeBackup)
-                       backupName.moveTo(d->filename);
-               return false;
+               // either we did not try to make a backup, or else we tried and failed,
+               // or else the original file was a symlink, in which case it was copied,
+               // not moved. so the original file is intact.
+               Alert::error(_("Write failure"),
+                            bformat(_("Cannot move saved file to:\n  %1$s.\n"
+                                      "But the file has successfully been saved as:\n  %2$s."),
+                                    from_utf8(fileName().absFileName()),
+                        from_utf8(savefile.absFileName())));
        }
+       return false;
 }
 
 
 bool Buffer::writeFile(FileName const & fname) const
 {
-       // FIXME Do we need to do these here? I don't think writing
-       // the LyX file depends upon it. (RGH)
-       // updateBuffer();
-       // updateMacroInstances();
-
        if (d->read_only && fname == d->filename)
                return false;
 
@@ -1247,6 +1599,8 @@ bool Buffer::write(ostream & ofs) const
        // The top of the file should not be written by params().
 
        // write out a comment in the top of the file
+       // Important: Keep the version formatting in sync with lyx2lyx and
+       //            tex2lyx (bug 7951)
        ofs << "#LyX " << lyx_version_major << "." << lyx_version_minor
            << " created this file. For more info see http://www.lyx.org/\n"
            << "\\lyxformat " << LYX_FORMAT << "\n"
@@ -1266,7 +1620,7 @@ bool Buffer::write(ostream & ofs) const
 
        // now write out the buffer parameters.
        ofs << "\\begin_header\n";
-       params().writeFile(ofs);
+       params().writeFile(ofs, this);
        ofs << "\\end_header\n";
 
        // write the text
@@ -1283,7 +1637,7 @@ bool Buffer::write(ostream & ofs) const
        // how to check if close went ok?
        // Following is an attempt... (BE 20001011)
 
-       // good() returns false if any error occured, including some
+       // good() returns false if any error occurred, including some
        //        formatting error.
        // bad()  returns true if something bad happened in the buffer,
        //        which should include file system full errors.
@@ -1305,17 +1659,19 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
 {
        OutputParams runparams = runparams_in;
 
-       // This is necessary for LuaTeX/XeTeX with tex fonts.
-       // See FIXME in BufferParams::encoding()
-       if (runparams.isFullUnicode())
-               runparams.encoding = encodings.fromLyXName("utf8-plain");
+       // XeTeX with TeX fonts is only safe with ASCII encoding (see also #9740),
+       // Check here, because the "flavor" is not known in BufferParams::encoding()
+       // (power users can override this safety measure selecting "utf8-plain").
+       if (!params().useNonTeXFonts && (runparams.flavor == OutputParams::XETEX)
+           && (runparams.encoding->name() != "utf8-plain"))
+               runparams.encoding = encodings.fromLyXName("ascii");
 
        string const encoding = runparams.encoding->iconvName();
        LYXERR(Debug::LATEX, "makeLaTeXFile encoding: " << encoding << ", fname=" << fname.realPath());
 
        ofdocstream ofs;
        try { ofs.reset(encoding); }
-       catch (iconv_codecvt_facet_exception & e) {
+       catch (iconv_codecvt_facet_exception const & e) {
                lyxerr << "Caught iconv exception: " << e.what() << endl;
                Alert::error(_("Iconv software exception Detected"), bformat(_("Please "
                        "verify that the support software for your encoding (%1$s) is "
@@ -1325,7 +1681,6 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
        if (!openFileWrite(ofs, fname))
                return false;
 
-       //TexStream ts(ofs.rdbuf(), &texrow());
        ErrorList & errorList = d->errorLists["Export"];
        errorList.clear();
        bool failed_export = false;
@@ -1342,21 +1697,20 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
                os.texrow().reset();
                writeLaTeXSource(os, original_path, runparams, output);
        }
-       catch (EncodingException & e) {
-               odocstringstream ods;
-               ods.put(e.failed_char);
+       catch (EncodingException const & e) {
+               docstring const failed(1, e.failed_char);
                ostringstream oss;
                oss << "0x" << hex << e.failed_char << dec;
                docstring msg = bformat(_("Could not find LaTeX command for character '%1$s'"
                                          " (code point %2$s)"),
-                                         ods.str(), from_utf8(oss.str()));
+                                         failed, from_utf8(oss.str()));
                errorList.push_back(ErrorItem(msg, _("Some characters of your document are probably not "
                                "representable in the chosen encoding.\n"
                                "Changing the document encoding to utf8 could help."),
                                e.par_id, e.pos, e.pos + 1));
                failed_export = true;
        }
-       catch (iconv_codecvt_facet_exception & e) {
+       catch (iconv_codecvt_facet_exception const & e) {
                errorList.push_back(ErrorItem(_("iconv conversion failed"),
                        _(e.what()), -1, 0, 0));
                failed_export = true;
@@ -1377,7 +1731,10 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
                lyxerr << "File '" << fname << "' was not closed properly." << endl;
        }
 
-       errors("Export");
+       if (runparams_in.silent)
+               errorList.clear();
+       else
+               errors("Export");
        return !failed_export;
 }
 
@@ -1385,25 +1742,45 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
 void Buffer::writeLaTeXSource(otexstream & os,
                           string const & original_path,
                           OutputParams const & runparams_in,
-         OutputWhat output) const
+                          OutputWhat output) const
 {
        // The child documents, if any, shall be already loaded at this point.
 
        OutputParams runparams = runparams_in;
 
+       // XeTeX with TeX fonts is only safe with ASCII encoding,
+       // Check here, because the "flavor" is not known in BufferParams::encoding()
+       // (power users can override this safety measure selecting "utf8-plain").
+       if (!params().useNonTeXFonts && (runparams.flavor == OutputParams::XETEX)
+           && (runparams.encoding->name() != "utf8-plain"))
+               runparams.encoding = encodings.fromLyXName("ascii");
+       // FIXME: when only the current paragraph is shown, this is ignored
+       //        (or not reached) and characters encodable in the current
+       //        encoding are not converted to ASCII-representation.
+
+       // If we are compiling a file standalone, even if this is the
+       // child of some other buffer, let's cut the link here, so the
+       // file is really independent and no concurring settings from
+       // the master (e.g. branch state) interfere (see #8100).
+       if (!runparams.is_child)
+               d->ignore_parent = true;
+
        // Classify the unicode characters appearing in math insets
-       Encodings::initUnicodeMath(*this);
+       BufferEncodings::initUnicodeMath(*this);
 
        // validate the buffer.
        LYXERR(Debug::LATEX, "  Validating buffer...");
        LaTeXFeatures features(*this, params(), runparams);
        validate(features);
+       // This is only set once per document (in master)
+       if (!runparams.is_child)
+               runparams.use_polyglossia = features.usePolyglossia();
        LYXERR(Debug::LATEX, "  Buffer validation done.");
 
        bool const output_preamble =
                output == FullSource || output == OnlyPreamble;
        bool const output_body =
-         output == FullSource || output == OnlyBody;
+               output == FullSource || output == OnlyBody;
 
        // The starting paragraph of the coming rows is the
        // first paragraph of the document. (Asger)
@@ -1439,9 +1816,8 @@ void Buffer::writeLaTeXSource(otexstream & os,
                        Encoding const * const enc = runparams.encoding;
                        if (enc) {
                                for (size_t n = 0; n < inputpath.size(); ++n) {
-                                       docstring const glyph =
-                                               docstring(1, inputpath[n]);
-                                       if (enc->latexChar(inputpath[n], true) != glyph) {
+                                       if (!enc->encodable(inputpath[n])) {
+                                               docstring const glyph(1, inputpath[n]);
                                                LYXERR0("Uncodable character '"
                                                        << glyph
                                                        << "' in input path!");
@@ -1486,7 +1862,7 @@ void Buffer::writeLaTeXSource(otexstream & os,
                                }
                                os << "\\makeatletter\n"
                                   << "\\def\\input@path{{"
-                                  << docdir << "/}}\n"
+                                  << docdir << "}}\n"
                                   << "\\makeatother\n";
                        }
                }
@@ -1496,15 +1872,20 @@ void Buffer::writeLaTeXSource(otexstream & os,
                MacroSet parentMacros;
                listParentMacros(parentMacros, features);
 
-               runparams.use_polyglossia = features.usePolyglossia();
                // Write the preamble
                runparams.use_babel = params().writeLaTeX(os, features,
                                                          d->filename.onlyPath());
 
-               runparams.use_japanese = features.isRequired("japanese");
+               // Japanese might be required only in some children of a document,
+               // but once required, we must keep use_japanese true.
+               runparams.use_japanese |= features.isRequired("japanese");
 
-               if (!output_body)
+               if (!output_body) {
+                       // Restore the parenthood if needed
+                       if (!runparams.is_child)
+                               d->ignore_parent = false;
                        return;
+               }
 
                // make the body.
                os << "\\begin{document}\n";
@@ -1516,28 +1897,19 @@ void Buffer::writeLaTeXSource(otexstream & os,
                        int num_lines = (*it)->write(os.os(), true);
                        os.texrow().newlines(num_lines);
                }
-               
+
        } // output_preamble
 
        os.texrow().start(paragraphs().begin()->id(), 0);
 
        LYXERR(Debug::INFO, "preamble finished, now the body.");
 
-       // if we are doing a real file with body, even if this is the
-       // child of some other buffer, let's cut the link here.
-       // This happens for example if only a child document is printed.
-       Buffer const * save_parent = 0;
-       if (output_preamble) {
-               save_parent = d->parent();
-               d->setParent(0);
-       }
-
        // the real stuff
        latexParagraphs(*this, text(), os, runparams);
 
        // Restore the parenthood if needed
-       if (output_preamble)
-               d->setParent(save_parent);
+       if (!runparams.is_child)
+               d->ignore_parent = false;
 
        // add this just in case after all the paragraphs
        os << endl;
@@ -1550,14 +1922,7 @@ void Buffer::writeLaTeXSource(otexstream & os,
        }
        runparams_in.encoding = runparams.encoding;
 
-       // Just to be sure. (Asger)
-       os.texrow().newline();
-
-       //for (int i = 0; i<d->texrow.rows(); i++) {
-       // int id,pos;
-       // if (d->texrow.getIdFromRow(i+1,id,pos) && id>0)
-       //      lyxerr << i+1 << ":" << id << ":" << getParFromID(id).paragraph().asString()<<"\n";
-       //}
+       os.texrow().finalize();
 
        LYXERR(Debug::INFO, "Finished making LaTeX file.");
        LYXERR(Debug::INFO, "Row count was " << os.texrow().rows() - 1 << '.');
@@ -1594,10 +1959,10 @@ void Buffer::writeDocBookSource(odocstream & os, string const & fname,
        LaTeXFeatures features(*this, params(), runparams);
        validate(features);
 
-       d->texrow.reset();
+       d->texrow.reset(false);
 
        DocumentClass const & tclass = params().documentClass();
-       string const top_element = tclass.latexname();
+       string const top_element = tclass.latexname();
 
        bool const output_preamble =
                output == FullSource || output == OnlyPreamble;
@@ -1647,18 +2012,18 @@ void Buffer::writeDocBookSource(odocstream & os, string const & fname,
                else
                        top += params().language->code().substr(0, 2);
                top += '"';
-       
+
                if (!params().options.empty()) {
                        top += ' ';
                        top += params().options;
                }
-       
+
                os << "<!-- " << ((runparams.flavor == OutputParams::XML)? "XML" : "SGML")
                                << " file was created by LyX " << lyx_version
                                << "\n  See http://www.lyx.org/ for more information -->\n";
-       
+
                params().documentClass().counters().reset();
-       
+
                sgml::openTag(os, top);
                os << '\n';
                docbookParagraphs(text(), *this, os, runparams);
@@ -1700,11 +2065,11 @@ void Buffer::writeLyXHTMLSource(odocstream & os,
        bool const output_preamble =
                output == FullSource || output == OnlyPreamble;
        bool const output_body =
-         output == FullSource || output == OnlyBody;
+         output == FullSource || output == OnlyBody || output == IncludedFile;
 
        if (output_preamble) {
                os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-                  << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd\">\n"
+                  << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\">\n"
                   // FIXME Language should be set properly.
                   << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                   << "<head>\n"
@@ -1714,45 +2079,85 @@ void Buffer::writeLyXHTMLSource(odocstream & os,
 
                docstring const & doctitle = features.htmlTitle();
                os << "<title>"
-                  << (doctitle.empty() ? from_ascii("LyX Document") : doctitle)
+                  << (doctitle.empty() ?
+                        from_ascii("LyX Document") :
+                        html::htmlize(doctitle, XHTMLStream::ESCAPE_ALL))
                   << "</title>\n";
 
-               os << "\n<!-- Text Class Preamble -->\n"
-                  << features.getTClassHTMLPreamble()
-                  << "\n<!-- Preamble Snippets -->\n"
-                  << from_utf8(features.getPreambleSnippets());
-
-               os << "\n<!-- Layout-provided Styles -->\n";
-               docstring const styleinfo = features.getTClassHTMLStyles();
-               if (!styleinfo.empty()) {
-                       os << "<style type='text/css'>\n"
-                               << styleinfo
-                               << "</style>\n";
-               }
+               docstring styles = features.getTClassHTMLPreamble();
+               if (!styles.empty())
+                       os << "\n<!-- Text Class Preamble -->\n" << styles << '\n';
+
+               styles = from_utf8(features.getPreambleSnippets());
+               if (!styles.empty())
+                       os << "\n<!-- Preamble Snippets -->\n" << styles << '\n';
+
+               // we will collect CSS information in a stream, and then output it
+               // either here, as part of the header, or else in a separate file.
+               odocstringstream css;
+               styles = from_utf8(features.getCSSSnippets());
+               if (!styles.empty())
+                       css << "/* LyX Provided Styles */\n" << styles << '\n';
+
+               styles = features.getTClassHTMLStyles();
+               if (!styles.empty())
+                       css << "/* Layout-provided Styles */\n" << styles << '\n';
 
                bool const needfg = params().fontcolor != RGBColor(0, 0, 0);
                bool const needbg = params().backgroundcolor != RGBColor(0xFF, 0xFF, 0xFF);
                if (needfg || needbg) {
-                               os << "<style type='text/css'>\nbody {\n";
+                               css << "\nbody {\n";
                                if (needfg)
-                                  os << "  color: "
+                                  css << "  color: "
                                            << from_ascii(X11hexname(params().fontcolor))
                                            << ";\n";
                                if (needbg)
-                                  os << "  background-color: "
+                                  css << "  background-color: "
                                            << from_ascii(X11hexname(params().backgroundcolor))
                                            << ";\n";
-                               os << "}\n</style>\n";
+                               css << "}\n";
+               }
+
+               docstring const dstyles = css.str();
+               if (!dstyles.empty()) {
+                       bool written = false;
+                       if (params().html_css_as_file) {
+                               // open a file for CSS info
+                               ofdocstream ocss;
+                               string const fcssname = addName(temppath(), "docstyle.css");
+                               FileName const fcssfile = FileName(fcssname);
+                               if (openFileWrite(ocss, fcssfile)) {
+                                       ocss << dstyles;
+                                       ocss.close();
+                                       written = true;
+                                       // write link to header
+                                       os << "<link rel='stylesheet' href='docstyle.css' type='text/css' />\n";
+                                       // register file
+                                       runparams.exportdata->addExternalFile("xhtml", fcssfile);
+                               }
+                       }
+                       // we are here if the CSS is supposed to be written to the header
+                       // or if we failed to write it to an external file.
+                       if (!written) {
+                               os << "<style type='text/css'>\n"
+                                        << dstyles
+                                        << "\n</style>\n";
+                       }
                }
                os << "</head>\n";
        }
 
        if (output_body) {
-               os << "<body>\n";
+               bool const output_body_tag = (output != IncludedFile);
+               if (output_body_tag)
+                       os << "<body>\n";
                XHTMLStream xs(os);
-               params().documentClass().counters().reset();
+               if (output != IncludedFile)
+                       // if we're an included file, the counters are in the master.
+                       params().documentClass().counters().reset();
                xhtmlParagraphs(text(), *this, xs, runparams);
-               os << "</body>\n";
+               if (output_body_tag)
+                       os << "</body>\n";
        }
 
        if (output_preamble)
@@ -1788,7 +2193,7 @@ int Buffer::runChktex()
        if (res == -1) {
                Alert::error(_("chktex failure"),
                             _("Could not run chktex successfully."));
-       } else if (res > 0) {
+       } else {
                ErrorList & errlist = d->errorLists["ChkTeX"];
                errlist.clear();
                bufferErrors(terr, errlist);
@@ -1796,7 +2201,10 @@ int Buffer::runChktex()
 
        setBusy(false);
 
-       errors("ChkTeX");
+       if (runparams.silent)
+               d->errorLists["ChkTeX"].clear();
+       else
+               errors("ChkTeX");
 
        return res;
 }
@@ -1804,7 +2212,11 @@ int Buffer::runChktex()
 
 void Buffer::validate(LaTeXFeatures & features) const
 {
-       params().validate(features);
+       // Validate the buffer params, but not for included
+       // files, since they also use the parent buffer's
+       // params (# 5941)
+       if (!features.runparams().is_child)
+               params().validate(features);
 
        for_each(paragraphs().begin(), paragraphs().end(),
                 bind(&Paragraph::validate, _1, ref(features)));
@@ -1824,9 +2236,9 @@ void Buffer::getLabelList(vector<docstring> & list) const
        }
 
        list.clear();
-       Toc & toc = d->toc_backend.toc("label");
-       TocIterator toc_it = toc.begin();
-       TocIterator end = toc.end();
+       shared_ptr<Toc> toc = d->toc_backend.toc("label");
+       Toc::const_iterator toc_it = toc->begin();
+       Toc::const_iterator end = toc->end();
        for (; toc_it != end; ++toc_it) {
                if (toc_it->depth() == 0)
                        list.push_back(toc_it->str());
@@ -1987,12 +2399,37 @@ void Buffer::addBibTeXInfo(docstring const & key, BibTeXInfo const & bi) const
 }
 
 
+void Buffer::makeCitationLabels() const
+{
+       Buffer const * const master = masterBuffer();
+       return d->bibinfo_.makeCitationLabels(*master);
+}
+
+
 bool Buffer::citeLabelsValid() const
 {
        return masterBuffer()->d->cite_labels_valid_;
 }
 
 
+void Buffer::removeBiblioTempFiles() const
+{
+       // We remove files that contain LaTeX commands specific to the
+       // particular bibliographic style being used, in order to avoid
+       // LaTeX errors when we switch style.
+       FileName const aux_file(addName(temppath(), changeExtension(latexName(),".aux")));
+       FileName const bbl_file(addName(temppath(), changeExtension(latexName(),".bbl")));
+       LYXERR(Debug::FILES, "Removing the .aux file " << aux_file);
+       aux_file.removeFile();
+       LYXERR(Debug::FILES, "Removing the .bbl file " << bbl_file);
+       bbl_file.removeFile();
+       // Also for the parent buffer
+       Buffer const * const pbuf = parent();
+       if (pbuf)
+               pbuf->removeBiblioTempFiles();
+}
+
+
 bool Buffer::isDepClean(string const & name) const
 {
        DepClean::const_iterator const it = d->dep_clean.find(name);
@@ -2020,65 +2457,94 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 
        switch (cmd.action()) {
 
-               case LFUN_BUFFER_TOGGLE_READ_ONLY:
-                       flag.setOnOff(isReadonly());
-                       break;
+       case LFUN_BUFFER_TOGGLE_READ_ONLY:
+               flag.setOnOff(isReadonly());
+               break;
 
                // FIXME: There is need for a command-line import.
                //case LFUN_BUFFER_IMPORT:
 
-               case LFUN_BUFFER_AUTO_SAVE:
-                       break;
+       case LFUN_BUFFER_AUTO_SAVE:
+               break;
 
-               case LFUN_BUFFER_EXPORT_CUSTOM:
-                       // FIXME: Nothing to check here?
-                       break;
+       case LFUN_BUFFER_EXPORT_CUSTOM:
+               // FIXME: Nothing to check here?
+               break;
 
-               case LFUN_BUFFER_EXPORT: {
-                       docstring const arg = cmd.argument();
-                       if (arg == "custom") {
-                               enable = true;
-                               break;
-                       }
-                       string format = to_utf8(arg);
-                       size_t pos = format.find(' ');
-                       if (pos != string::npos)
-                               format = format.substr(0, pos);
-                       enable = params().isExportable(format);
-                       if (!enable)
-                               flag.message(bformat(
-                                       _("Don't know how to export to format: %1$s"), arg));
+       case LFUN_BUFFER_EXPORT: {
+               docstring const arg = cmd.argument();
+               if (arg == "custom") {
+                       enable = true;
                        break;
                }
+               string format = to_utf8(arg);
+               size_t pos = format.find(' ');
+               if (pos != string::npos)
+                       format = format.substr(0, pos);
+               enable = params().isExportable(format);
+               if (!enable)
+                       flag.message(bformat(
+                                            _("Don't know how to export to format: %1$s"), arg));
+               break;
+       }
 
-               case LFUN_BUFFER_CHKTEX:
-                       enable = params().isLatex() && !lyxrc.chktex_command.empty();
-                       break;
+       case LFUN_BUFFER_CHKTEX:
+               enable = params().isLatex() && !lyxrc.chktex_command.empty();
+               break;
 
-               case LFUN_BUILD_PROGRAM:
-                       enable = params().isExportable("program");
-                       break;
+       case LFUN_BUILD_PROGRAM:
+               enable = params().isExportable("program");
+               break;
 
-               case LFUN_BRANCH_ACTIVATE: 
-               case LFUN_BRANCH_DEACTIVATE: {
-                       BranchList const & branchList = params().branchlist();
-                       docstring const branchName = cmd.argument();
-                       enable = !branchName.empty() && branchList.find(branchName);
-                       break;
-               }
+       case LFUN_BRANCH_ACTIVATE:
+       case LFUN_BRANCH_DEACTIVATE:
+       case LFUN_BRANCH_MASTER_ACTIVATE:
+       case LFUN_BRANCH_MASTER_DEACTIVATE: {
+               bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
+                                    || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
+               BranchList const & branchList = master ? masterBuffer()->params().branchlist()
+                       : params().branchlist();
+               docstring const branchName = cmd.argument();
+               flag.setEnabled(!branchName.empty() && branchList.find(branchName));
+               break;
+       }
 
-               case LFUN_BRANCH_ADD:
-               case LFUN_BRANCHES_RENAME:
-               case LFUN_BUFFER_PRINT:
-                       // if no Buffer is present, then of course we won't be called!
-                       break;
+       case LFUN_BRANCH_ADD:
+       case LFUN_BRANCHES_RENAME:
+               // if no Buffer is present, then of course we won't be called!
+               break;
 
-               case LFUN_BUFFER_LANGUAGE:
-                       enable = !isReadonly();
-                       break;
+       case LFUN_BUFFER_LANGUAGE:
+               enable = !isReadonly();
+               break;
 
-               default:
-                       return false;
+       case LFUN_BUFFER_VIEW_CACHE:
+               (d->preview_file_).refresh();
+               enable = (d->preview_file_).exists() && !(d->preview_file_).isFileEmpty();
+               break;
+
+       case LFUN_CHANGES_TRACK:
+               flag.setEnabled(true);
+               flag.setOnOff(params().track_changes);
+               break;
+
+       case LFUN_CHANGES_OUTPUT:
+               flag.setEnabled(true);
+               flag.setOnOff(params().output_changes);
+               break;
+
+       case LFUN_BUFFER_TOGGLE_COMPRESSION: {
+               flag.setOnOff(params().compressed);
+               break;
+       }
+
+       case LFUN_BUFFER_TOGGLE_OUTPUT_SYNC: {
+               flag.setOnOff(params().output_sync);
+               break;
+       }
+
+       default:
+               return false;
        }
        flag.setEnabled(enable);
        return true;
@@ -2092,7 +2558,7 @@ void Buffer::dispatch(string const & command, DispatchResult & result)
 
 
 // NOTE We can end up here even if we have no GUI, because we are called
-// by LyX::exec to handled command-line requests. So we may need to check 
+// by LyX::exec to handled command-line requests. So we may need to check
 // whether we have a GUI or not. The boolean use_gui holds this information.
 void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
 {
@@ -2109,8 +2575,11 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
 
        switch (func.action()) {
        case LFUN_BUFFER_TOGGLE_READ_ONLY:
-               if (lyxvc().inUse())
-                       lyxvc().toggleReadOnly();
+               if (lyxvc().inUse()) {
+                       string log = lyxvc().toggleReadOnly();
+                       if (!log.empty())
+                               dr.setMessage(log);
+               }
                else
                        setReadonly(!isReadonly());
                break;
@@ -2119,14 +2588,18 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                ExportStatus const status = doExport(argument, false);
                dr.setError(status != ExportSuccess);
                if (status != ExportSuccess)
-                       dr.setMessage(bformat(_("Error exporting to format: %1$s."), 
+                       dr.setMessage(bformat(_("Error exporting to format: %1$s."),
                                              func.argument()));
                break;
        }
 
-       case LFUN_BUILD_PROGRAM:
-               doExport("program", true);
+       case LFUN_BUILD_PROGRAM: {
+               ExportStatus const status = doExport("program", true);
+               dr.setError(status != ExportSuccess);
+               if (status != ExportSuccess)
+                       dr.setMessage(_("Error generating literate programming code."));
                break;
+       }
 
        case LFUN_BUFFER_CHKTEX:
                runChktex();
@@ -2167,7 +2640,8 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
 
                // Execute the command in the background
                Systemcall call;
-               call.startscript(Systemcall::DontWait, command, filePath());
+               call.startscript(Systemcall::DontWait, command,
+                                filePath(), layoutPos());
                break;
        }
 
@@ -2183,6 +2657,42 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                resetAutosaveTimers();
                break;
 
+       case LFUN_BRANCH_ACTIVATE:
+       case LFUN_BRANCH_DEACTIVATE:
+       case LFUN_BRANCH_MASTER_ACTIVATE:
+       case LFUN_BRANCH_MASTER_DEACTIVATE: {
+               bool const master = (func.action() == LFUN_BRANCH_MASTER_ACTIVATE
+                                    || func.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
+               Buffer * buf = master ? const_cast<Buffer *>(masterBuffer())
+                                     : this;
+
+               docstring const branch_name = func.argument();
+               // the case without a branch name is handled elsewhere
+               if (branch_name.empty()) {
+                       dispatched = false;
+                       break;
+               }
+               Branch * branch = buf->params().branchlist().find(branch_name);
+               if (!branch) {
+                       LYXERR0("Branch " << branch_name << " does not exist.");
+                       dr.setError(true);
+                       docstring const msg =
+                               bformat(_("Branch \"%1$s\" does not exist."), branch_name);
+                       dr.setMessage(msg);
+                       break;
+               }
+               bool const activate = (func.action() == LFUN_BRANCH_ACTIVATE
+                                      || func.action() == LFUN_BRANCH_MASTER_ACTIVATE);
+               if (branch->isSelected() != activate) {
+                       buf->undo().recordUndoBufferParams(CursorData());
+                       branch->setSelected(activate);
+                       dr.setError(false);
+                       dr.screenUpdate(Update::Force);
+                       dr.forceBufferUpdate();
+               }
+               break;
+       }
+
        case LFUN_BRANCH_ADD: {
                docstring branch_name = func.argument();
                if (branch_name.empty()) {
@@ -2204,6 +2714,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                                        msg += ("\n");
                                msg += bformat(_("Branch \"%1$s\" already exists."), branch_name);
                        } else {
+                               undo().recordUndoBufferParams(CursorData());
                                branch_list.add(branch_name);
                                branch = branch_list.find(branch_name);
                                string const x11hexname = X11hexname(branch->color());
@@ -2218,31 +2729,6 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                break;
        }
 
-       case LFUN_BRANCH_ACTIVATE:
-       case LFUN_BRANCH_DEACTIVATE: {
-               BranchList & branchList = params().branchlist();
-               docstring const branchName = func.argument();
-               // the case without a branch name is handled elsewhere
-               if (branchName.empty()) {
-                       dispatched = false;
-                       break;
-               }
-               Branch * branch = branchList.find(branchName);
-               if (!branch) {
-                       LYXERR0("Branch " << branchName << " does not exist.");
-                       dr.setError(true);
-                       docstring const msg = 
-                               bformat(_("Branch \"%1$s\" does not exist."), branchName);
-                       dr.setMessage(msg);
-               } else {
-                       branch->setSelected(func.action() == LFUN_BRANCH_ACTIVATE);
-                       dr.setError(false);
-                       dr.screenUpdate(Update::Force);
-                       dr.forceBufferUpdate();
-               }
-               break;
-       }
-
        case LFUN_BRANCHES_RENAME: {
                if (func.argument().empty())
                        break;
@@ -2256,7 +2742,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                        if (it->lyxCode() == BRANCH_CODE) {
                                InsetBranch & ins = static_cast<InsetBranch &>(*it);
                                if (ins.branch() == oldname) {
-                                       undo().recordUndo(it);
+                                       undo().recordUndo(CursorData(it));
                                        ins.rename(newname);
                                        success = true;
                                        continue;
@@ -2280,122 +2766,55 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                break;
        }
 
-       case LFUN_BUFFER_PRINT: {
-               // we'll assume there's a problem until we succeed
-               dr.setError(true); 
-               string target = func.getArg(0);
-               string target_name = func.getArg(1);
-               string command = func.getArg(2);
-
-               if (target.empty()
-                   || target_name.empty()
-                   || command.empty()) {
-                       LYXERR0("Unable to parse " << func.argument());
-                       docstring const msg = 
-                               bformat(_("Unable to parse \"%1$s\""), func.argument());
-                       dr.setMessage(msg);
-                       break;
-               }
-               if (target != "printer" && target != "file") {
-                       LYXERR0("Unrecognized target \"" << target << '"');
-                       docstring const msg = 
-                               bformat(_("Unrecognized target \"%1$s\""), from_utf8(target));
-                       dr.setMessage(msg);
-                       break;
-               }
-
-               if (!doExport("dvi", true)) {
-                       showPrintError(absFileName());
-                       dr.setMessage(_("Error exporting to DVI."));
-                       break;
-               }
+       case LFUN_BUFFER_VIEW_CACHE:
+               if (!formats.view(*this, d->preview_file_,
+                                 d->preview_format_))
+                       dr.setMessage(_("Error viewing the output file."));
+               break;
 
-               // Push directory path.
-               string const path = temppath();
-               // Prevent the compiler from optimizing away p
-               FileName pp(path);
-               PathChanger p(pp);
-
-               // there are three cases here:
-               // 1. we print to a file
-               // 2. we print directly to a printer
-               // 3. we print using a spool command (print to file first)
-               Systemcall one;
-               int res = 0;
-               string const dviname = changeExtension(latexName(true), "dvi");
-
-               if (target == "printer") {
-                       if (!lyxrc.print_spool_command.empty()) {
-                               // case 3: print using a spool
-                               string const psname = changeExtension(dviname,".ps");
-                               command += ' ' + lyxrc.print_to_file
-                                       + quoteName(psname)
-                                       + ' '
-                                       + quoteName(dviname);
-
-                               string command2 = lyxrc.print_spool_command + ' ';
-                               if (target_name != "default") {
-                                       command2 += lyxrc.print_spool_printerprefix
-                                               + target_name
-                                               + ' ';
-                               }
-                               command2 += quoteName(psname);
-                               // First run dvips.
-                               // If successful, then spool command
-                               res = one.startscript(Systemcall::Wait, command,
-                                                     filePath());
-
-                               if (res == 0) {
-                                       // If there's no GUI, we have to wait on this command. Otherwise,
-                                       // LyX deletes the temporary directory, and with it the spooled
-                                       // file, before it can be printed!!
-                                       Systemcall::Starttype stype = use_gui ?
-                                               Systemcall::DontWait : Systemcall::Wait;
-                                       res = one.startscript(stype, command2,
-                                                             filePath());
-                               }
-                       } else {
-                               // case 2: print directly to a printer
-                               if (target_name != "default")
-                                       command += ' ' + lyxrc.print_to_printer + target_name + ' ';
-                               // as above....
-                               Systemcall::Starttype stype = use_gui ?
-                                       Systemcall::DontWait : Systemcall::Wait;
-                               res = one.startscript(stype, command +
-                                               quoteName(dviname), filePath());
-                       }
+       case LFUN_CHANGES_TRACK:
+               if (params().save_transient_properties)
+                       undo().recordUndoBufferParams(CursorData());
+               params().track_changes = !params().track_changes;
+               if (!params().track_changes)
+                       dr.forceChangesUpdate();
+               break;
 
-               } else {
-                       // case 1: print to a file
-                       FileName const filename(makeAbsPath(target_name, filePath()));
-                       FileName const dvifile(makeAbsPath(dviname, path));
-                       if (filename.exists()) {
-                               docstring text = bformat(
-                                       _("The file %1$s already exists.\n\n"
-                                         "Do you want to overwrite that file?"),
-                                       makeDisplayPath(filename.absFileName()));
-                               if (Alert::prompt(_("Overwrite file?"),
-                                                 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
-                                       break;
+       case LFUN_CHANGES_OUTPUT:
+               if (params().save_transient_properties)
+                       undo().recordUndoBufferParams(CursorData());
+               params().output_changes = !params().output_changes;
+               if (params().output_changes) {
+                       bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
+                       bool xcolorulem = LaTeXFeatures::isAvailable("ulem") &&
+                                         LaTeXFeatures::isAvailable("xcolor");
+
+                       if (!dvipost && !xcolorulem) {
+                               Alert::warning(_("Changes not shown in LaTeX output"),
+                                              _("Changes will not be highlighted in LaTeX output, "
+                                                "because neither dvipost nor xcolor/ulem are installed.\n"
+                                                "Please install these packages or redefine "
+                                                "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
+                       } else if (!xcolorulem) {
+                               Alert::warning(_("Changes not shown in LaTeX output"),
+                                              _("Changes will not be highlighted in LaTeX output "
+                                                "when using pdflatex, because xcolor and ulem are not installed.\n"
+                                                "Please install both packages or redefine "
+                                                "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
                        }
-                       command += ' ' + lyxrc.print_to_file
-                               + quoteName(filename.toFilesystemEncoding())
-                               + ' '
-                               + quoteName(dvifile.toFilesystemEncoding());
-                       // as above....
-                       Systemcall::Starttype stype = use_gui ?
-                               Systemcall::DontWait : Systemcall::Wait;
-                       res = one.startscript(stype, command, filePath());
                }
+               break;
 
-               if (res == 0) 
-                       dr.setError(false);
-               else {
-                       dr.setMessage(_("Error running external commands."));
-                       showPrintError(absFileName());
-               }
+       case LFUN_BUFFER_TOGGLE_COMPRESSION:
+               // turn compression on/off
+               undo().recordUndoBufferParams(CursorData());
+               params().compressed = !params().compressed;
+               break;
+
+       case LFUN_BUFFER_TOGGLE_OUTPUT_SYNC:
+               undo().recordUndoBufferParams(CursorData());
+               params().output_sync = !params().output_sync;
                break;
-       }
 
        default:
                dispatched = false;
@@ -2408,8 +2827,8 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
 
 void Buffer::changeLanguage(Language const * from, Language const * to)
 {
-       LASSERT(from, /**/);
-       LASSERT(to, /**/);
+       LASSERT(from, return);
+       LASSERT(to, return);
 
        for_each(par_iterator_begin(),
                 par_iterator_end(),
@@ -2520,7 +2939,7 @@ bool Buffer::isClean() const
 
 bool Buffer::isExternallyModified(CheckMethod method) const
 {
-       LASSERT(d->filename.exists(), /**/);
+       LASSERT(d->filename.exists(), return false);
        // if method == timestamp, check timestamp before checksum
        return (method == checksum_method
                || d->timestamp_ != d->filename.lastModified())
@@ -2576,7 +2995,13 @@ bool Buffer::isUnnamed() const
 /// retrieving fileName() nor for checking if it is unnamed or not.
 bool Buffer::isInternal() const
 {
-       return fileName().extension() == "internal";
+       return d->internal_buffer;
+}
+
+
+void Buffer::setInternal(bool flag)
+{
+       d->internal_buffer = flag;
 }
 
 
@@ -2610,7 +3035,53 @@ string Buffer::absFileName() const
 
 string Buffer::filePath() const
 {
-       return d->filename.onlyPath().absFileName() + "/";
+       string const abs = d->filename.onlyPath().absFileName();
+       if (abs.empty())
+               return abs;
+       int last = abs.length() - 1;
+
+       return abs[last] == '/' ? abs : abs + '/';
+}
+
+
+DocFileName Buffer::getReferencedFileName(string const & fn) const
+{
+       DocFileName result;
+       if (FileName::isAbsolute(fn) || !FileName::isAbsolute(params().origin))
+               result.set(fn, filePath());
+       else {
+               // filePath() ends with a path separator
+               FileName const test(filePath() + fn);
+               if (test.exists())
+                       result.set(fn, filePath());
+               else
+                       result.set(fn, params().origin);
+       }
+
+       return result;
+}
+
+
+string Buffer::layoutPos() const
+{
+       return d->layout_position;
+}
+
+
+void Buffer::setLayoutPos(string const & path)
+{
+       if (path.empty()) {
+               d->layout_position.clear();
+               return;
+       }
+
+       LATTEST(FileName::isAbsolute(path));
+
+       d->layout_position =
+               to_utf8(makeRelPath(from_utf8(path), from_utf8(filePath())));
+
+       if (d->layout_position.empty())
+               d->layout_position = ".";
 }
 
 
@@ -2672,7 +3143,7 @@ DocIterator Buffer::firstChildPosition(Buffer const * child)
 
 bool Buffer::hasChildren() const
 {
-       return !d->children_positions.empty();  
+       return !d->children_positions.empty();
 }
 
 
@@ -2688,7 +3159,7 @@ void Buffer::collectChildren(ListOfBuffers & clist, bool grand_children) const
                if (bit != clist.end())
                        continue;
                clist.push_back(child);
-               if (grand_children) 
+               if (grand_children)
                        // there might be grandchildren
                        child->collectChildren(clist, true);
        }
@@ -2760,19 +3231,19 @@ MacroData const * Buffer::Impl::getBufferMacro(docstring const & name,
                if (it != nameIt->second.end()) {
                        while (true) {
                                // scope ends behind pos?
-                               if (pos < it->second.first) {
+                               if (pos < it->second.scope) {
                                        // Looks good, remember this. If there
                                        // is no external macro behind this,
                                        // we found the right one already.
                                        bestPos = it->first;
-                                       bestData = &it->second.second;
+                                       bestData = &it->second.macro;
                                        break;
                                }
 
                                // try previous macro if there is one
                                if (it == nameIt->second.begin())
                                        break;
-                               it--;
+                               --it;
                        }
                }
        }
@@ -2790,13 +3261,13 @@ MacroData const * Buffer::Impl::getBufferMacro(docstring const & name,
                        break;
 
                // scope ends behind pos?
-               if (pos < it->second.first
+               if (pos < it->second.scope
                        && (cloned_buffer_ ||
-                           theBufferList().isLoaded(it->second.second))) {
+                           theBufferList().isLoaded(it->second.buffer))) {
                        // look for macro in external file
                        macro_lock = true;
                        MacroData const * data
-                               = it->second.second->getMacro(name, false);
+                               = it->second.buffer->getMacro(name, false);
                        macro_lock = false;
                        if (data) {
                                bestPos = it->first;
@@ -2902,7 +3373,7 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
                                it.pop_back();
                                continue;
                        }
-                       
+
                        if (iit->inset->asInsetTabular()) {
                                CursorSlice slice(*iit->inset);
                                size_t const numcells = slice.nargs();
@@ -2961,7 +3432,7 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
                                continue;
 
                        // register macro
-                       // FIXME (Abdel), I don't understandt why we pass 'it' here
+                       // FIXME (Abdel), I don't understand why we pass 'it' here
                        // instead of 'macroTemplate' defined above... is this correct?
                        macros[macroTemplate.name()][it] =
                                Impl::ScopeMacro(scope, MacroData(const_cast<Buffer *>(owner_), it));
@@ -3107,17 +3578,17 @@ void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
 }
 
 
-Buffer::References & Buffer::references(docstring const & label)
+Buffer::References & Buffer::getReferenceCache(docstring const & label)
 {
        if (d->parent())
-               return const_cast<Buffer *>(masterBuffer())->references(label);
+               return const_cast<Buffer *>(masterBuffer())->getReferenceCache(label);
 
        RefCache::iterator it = d->ref_cache_.find(label);
        if (it != d->ref_cache_.end())
                return it->second.second;
 
        static InsetLabel const * dummy_il = 0;
-       static References const dummy_refs;
+       static References const dummy_refs = References();
        it = d->ref_cache_.insert(
                make_pair(label, make_pair(dummy_il, dummy_refs))).first;
        return it->second.second;
@@ -3126,7 +3597,14 @@ Buffer::References & Buffer::references(docstring const & label)
 
 Buffer::References const & Buffer::references(docstring const & label) const
 {
-       return const_cast<Buffer *>(this)->references(label);
+       return const_cast<Buffer *>(this)->getReferenceCache(label);
+}
+
+
+void Buffer::addReference(docstring const & label, Inset * inset, ParIterator it)
+{
+       References & refs = getReferenceCache(label);
+       refs.push_back(make_pair(inset, it));
 }
 
 
@@ -3149,12 +3627,9 @@ void Buffer::clearReferenceCache() const
 }
 
 
-void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
-       InsetCode code)
+void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to)
 {
        //FIXME: This does not work for child documents yet.
-       LASSERT(code == CITE_CODE, /**/);
-
        reloadBibInfoCache();
 
        // Check if the label 'from' appears more than once
@@ -3172,22 +3647,21 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
 
        string const paramName = "key";
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
-               if (it->lyxCode() == code) {
-                       InsetCommand * inset = it->asInsetCommand();
-                       if (!inset)
-                               continue;
-                       docstring const oldValue = inset->getParam(paramName);
-                       if (oldValue == from)
-                               inset->setParam(paramName, to);
-               }
+               if (it->lyxCode() != CITE_CODE)
+                       continue;
+               InsetCommand * inset = it->asInsetCommand();
+               docstring const oldValue = inset->getParam(paramName);
+               if (oldValue == from)
+                       inset->setParam(paramName, to);
        }
 }
 
-
-void Buffer::getSourceCode(odocstream & os, string const format,
-                          pit_type par_begin, pit_type par_end,
-                          OutputWhat output) const
+// returns NULL if id-to-row conversion is unsupported
+unique_ptr<TexRow> Buffer::getSourceCode(odocstream & os, string const & format,
+                                         pit_type par_begin, pit_type par_end,
+                                         OutputWhat output, bool master) const
 {
+       unique_ptr<TexRow> texrow;
        OutputParams runparams(&params().encoding());
        runparams.nice = true;
        runparams.flavor = params().getOutputFlavor(format);
@@ -3209,43 +3683,92 @@ void Buffer::getSourceCode(odocstream & os, string const format,
                                        convert<docstring>(par_end - 1))
                           << "\n\n";
                }
-               TexRow texrow;
-               texrow.reset();
-               texrow.newline();
-               texrow.newline();
                // output paragraphs
-               if (params().isDocBook())
-                       docbookParagraphs(text(), *this, os, runparams);
-               else if (runparams.flavor == OutputParams::HTML) {
+               if (runparams.flavor == OutputParams::LYX) {
+                       Paragraph const & par = text().paragraphs()[par_begin];
+                       ostringstream ods;
+                       depth_type dt = par.getDepth();
+                       par.write(ods, params(), dt);
+                       os << from_utf8(ods.str());
+               } else if (runparams.flavor == OutputParams::HTML) {
                        XHTMLStream xs(os);
+                       setMathFlavor(runparams);
                        xhtmlParagraphs(text(), *this, xs, runparams);
+               } else if (runparams.flavor == OutputParams::TEXT) {
+                       bool dummy = false;
+                       // FIXME Handles only one paragraph, unlike the others.
+                       // Probably should have some routine with a signature like them.
+                       writePlaintextParagraph(*this,
+                               text().paragraphs()[par_begin], os, runparams, dummy);
+               } else if (params().isDocBook()) {
+                       docbookParagraphs(text(), *this, os, runparams);
                } else {
+                       // If we are previewing a paragraph, even if this is the
+                       // child of some other buffer, let's cut the link here,
+                       // so that no concurring settings from the master
+                       // (e.g. branch state) interfere (see #8101).
+                       if (!master)
+                               d->ignore_parent = true;
+                       // We need to validate the Buffer params' features here
+                       // in order to know if we should output polyglossia
+                       // macros (instead of babel macros)
+                       LaTeXFeatures features(*this, params(), runparams);
+                       params().validate(features);
+                       runparams.use_polyglossia = features.usePolyglossia();
+                       texrow = make_unique<TexRow>();
+                       texrow->newline();
+                       texrow->newline();
                        // latex or literate
-                       otexstream ots(os, texrow);
+                       otexstream ots(os, *texrow);
+
+                       // the real stuff
                        latexParagraphs(*this, text(), ots, runparams);
+                       texrow->finalize();
+
+                       // Restore the parenthood
+                       if (!master)
+                               d->ignore_parent = false;
                }
        } else {
                os << "% ";
-               if (output == FullSource) 
+               if (output == FullSource)
                        os << _("Preview source code");
                else if (output == OnlyPreamble)
                        os << _("Preview preamble");
                else if (output == OnlyBody)
                        os << _("Preview body");
                os << "\n\n";
-               d->texrow.reset();
-               d->texrow.newline();
-               d->texrow.newline();
-               if (params().isDocBook())
-                       writeDocBookSource(os, absFileName(), runparams, output);
-               else if (runparams.flavor == OutputParams::HTML)
+               if (runparams.flavor == OutputParams::LYX) {
+                       ostringstream ods;
+                       if (output == FullSource)
+                               write(ods);
+                       else if (output == OnlyPreamble)
+                               params().writeFile(ods, this);
+                       else if (output == OnlyBody)
+                               text().write(ods);
+                       os << from_utf8(ods.str());
+               } else if (runparams.flavor == OutputParams::HTML) {
                        writeLyXHTMLSource(os, runparams, output);
-               else {
+               } else if (runparams.flavor == OutputParams::TEXT) {
+                       if (output == OnlyPreamble) {
+                               os << "% "<< _("Plain text does not have a preamble.");
+                       } else
+                               writePlaintextFile(*this, os, runparams);
+               } else if (params().isDocBook()) {
+                               writeDocBookSource(os, absFileName(), runparams, output);
+               } else {
                        // latex or literate
-                       otexstream ots(os, d->texrow);
+                       texrow = make_unique<TexRow>();
+                       texrow->newline();
+                       texrow->newline();
+                       otexstream ots(os, *texrow);
+                       if (master)
+                               runparams.is_child = true;
                        writeLaTeXSource(ots, string(), runparams, output);
+                       texrow->finalize();
                }
        }
+       return texrow;
 }
 
 
@@ -3328,7 +3851,7 @@ public:
        ///
        virtual shared_ptr<ForkedProcess> clone() const
        {
-               return shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
+               return make_shared<AutoSaveBuffer>(*this);
        }
        ///
        int start()
@@ -3349,7 +3872,7 @@ private:
 int AutoSaveBuffer::generateChild()
 {
 #if defined(__APPLE__)
-       /* FIXME fork() is not usable for autosave on Mac OS X 10.6 (snow leopard) 
+       /* FIXME fork() is not usable for autosave on Mac OS X 10.6 (snow leopard)
         *   We should use something else like threads.
         *
         * Since I do not know how to determine at run time what is the OS X
@@ -3372,11 +3895,13 @@ int AutoSaveBuffer::generateChild()
        // to fork. But we will do the save
        // anyway.
        bool failed = false;
-       FileName const tmp_ret = FileName::tempName("lyxauto");
+       TempFile tempfile("lyxautoXXXXXX.lyx");
+       tempfile.setAutoRemove(false);
+       FileName const tmp_ret = tempfile.name();
        if (!tmp_ret.empty()) {
-               buffer_.writeFile(tmp_ret);
-               // assume successful write of tmp_ret
-               if (!tmp_ret.moveTo(fname_))
+               if (!buffer_.writeFile(tmp_ret))
+                       failed = true;
+               else if (!tmp_ret.moveTo(fname_))
                        failed = true;
        } else
                failed = true;
@@ -3409,7 +3934,7 @@ FileName Buffer::getEmergencyFileName() const
 FileName Buffer::getAutosaveFileName() const
 {
        // if the document is unnamed try to save in the backup dir, else
-       // in the default document path, and as a last try in the filePath, 
+       // in the default document path, and as a last try in the filePath,
        // which will most often be the temporary directory
        string fpath;
        if (isUnnamed())
@@ -3442,7 +3967,7 @@ void Buffer::moveAutosaveFile(support::FileName const & oldauto) const
 }
 
 
-bool Buffer::autoSave() const 
+bool Buffer::autoSave() const
 {
        Buffer const * buf = d->cloned_buffer_ ? d->cloned_buffer_ : this;
        if (buf->d->bak_clean || isReadonly())
@@ -3450,52 +3975,29 @@ bool Buffer::autoSave() const
 
        message(_("Autosaving current document..."));
        buf->d->bak_clean = true;
-       
-       FileName const fname = getAutosaveFileName();
-       if (d->cloned_buffer_) {
-               // If this buffer is cloned, we assume that
-               // we are running in a separate thread already.
-               FileName const tmp_ret = FileName::tempName("lyxauto");
-               if (!tmp_ret.empty()) {
-                       writeFile(tmp_ret);
-                       // assume successful write of tmp_ret
-                       if (tmp_ret.moveTo(fname))
-                               return true;
-               }
-               // failed to write/rename tmp_ret so try writing direct
-               return writeFile(fname);
-       } else {        
-               /// This function is deprecated as the frontend needs to take care
-               /// of cloning the buffer and autosaving it in another thread. It
-               /// is still here to allow (QT_VERSION < 0x040400).
-               AutoSaveBuffer autosave(*this, fname);
-               autosave.start();
-               return true;
-       }
-}
 
+       FileName const fname = getAutosaveFileName();
+       LASSERT(d->cloned_buffer_, return false);
 
-// helper class, to guarantee this gets reset properly
-class Buffer::MarkAsExporting {
-public:
-       MarkAsExporting(Buffer const * buf) : buf_(buf)
-       {
-               LASSERT(buf_, /* */);
-               buf_->setExportStatus(true);
-       }
-       ~MarkAsExporting()
-       {
-               buf_->setExportStatus(false);
+       // If this buffer is cloned, we assume that
+       // we are running in a separate thread already.
+       TempFile tempfile("lyxautoXXXXXX.lyx");
+       tempfile.setAutoRemove(false);
+       FileName const tmp_ret = tempfile.name();
+       if (!tmp_ret.empty()) {
+               writeFile(tmp_ret);
+               // assume successful write of tmp_ret
+               if (tmp_ret.moveTo(fname))
+                       return true;
        }
-private:
-       Buffer const * const buf_;
-};
-
+       // failed to write/rename tmp_ret so try writing direct
+       return writeFile(fname);
+}
 
 
 void Buffer::setExportStatus(bool e) const
 {
-       d->doing_export = e;    
+       d->doing_export = e;
        ListOfBuffers clist = getDescendents();
        ListOfBuffers::const_iterator cit = clist.begin();
        ListOfBuffers::const_iterator const cen = clist.end();
@@ -3525,8 +4027,8 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                        && !params().getIncludedChildren().empty();
 
        // (1) export with all included children (omit \includeonly)
-       if (update_unincluded) { 
-               ExportStatus const status = 
+       if (update_unincluded) {
+               ExportStatus const status =
                        doExport(target, put_in_tempdir, true, result_file);
                if (status != ExportSuccess)
                        return status;
@@ -3536,6 +4038,25 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
 }
 
 
+void Buffer::setMathFlavor(OutputParams & op) const
+{
+       switch (params().html_math_output) {
+       case BufferParams::MathML:
+               op.math_flavor = OutputParams::MathAsMathML;
+               break;
+       case BufferParams::HTML:
+               op.math_flavor = OutputParams::MathAsHTML;
+               break;
+       case BufferParams::Images:
+               op.math_flavor = OutputParams::MathAsImages;
+               break;
+       case BufferParams::LaTeX:
+               op.math_flavor = OutputParams::MathAsLaTeX;
+               break;
+       }
+}
+
+
 Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir,
        bool includeall, string & result_file) const
 {
@@ -3558,6 +4079,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
        runparams.includeall = includeall;
        vector<string> backs = params().backends();
        Converters converters = theConverters();
+       bool need_nice_file = false;
        if (find(backs.begin(), backs.end(), format) == backs.end()) {
                // Get shortest path to format
                converters.buildGraph();
@@ -3580,7 +4102,14 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                        }
                        return ExportNoPathToFormat;
                }
-               runparams.flavor = converters.getFlavor(path);
+               runparams.flavor = converters.getFlavor(path, this);
+               Graph::EdgePath::const_iterator it = path.begin();
+               Graph::EdgePath::const_iterator en = path.end();
+               for (; it != en; ++it)
+                       if (theConverters().get(*it).nice()) {
+                               need_nice_file = true;
+                               break;
+                       }
 
        } else {
                backend_format = format;
@@ -3610,20 +4139,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
        // HTML backend
        else if (backend_format == "xhtml") {
                runparams.flavor = OutputParams::HTML;
-               switch (params().html_math_output) {
-               case BufferParams::MathML: 
-                       runparams.math_flavor = OutputParams::MathAsMathML; 
-                       break;
-               case BufferParams::HTML: 
-                       runparams.math_flavor = OutputParams::MathAsHTML; 
-                       break;
-               case BufferParams::Images:
-                       runparams.math_flavor = OutputParams::MathAsImages; 
-                       break;
-               case BufferParams::LaTeX:
-                       runparams.math_flavor = OutputParams::MathAsLaTeX; 
-                       break;
-               }
+               setMathFlavor(runparams);
                makeLyXHTMLFile(FileName(filename), runparams);
        } else if (backend_format == "lyx")
                writeFile(FileName(filename));
@@ -3633,15 +4149,13 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                makeDocBookFile(FileName(filename), runparams);
        }
        // LaTeX backend
-       else if (backend_format == format) {
+       else if (backend_format == format || need_nice_file) {
                runparams.nice = true;
-               if (!makeLaTeXFile(FileName(filename), string(), runparams)) {
-                       if (d->cloned_buffer_) {
-                               d->cloned_buffer_->d->errorLists["Export"] =
-                                       d->errorLists["Export"];
-                       }
+               bool const success = makeLaTeXFile(FileName(filename), string(), runparams);
+               if (d->cloned_buffer_)
+                       d->cloned_buffer_->d->errorLists["Export"] = d->errorLists["Export"];
+               if (!success)
                        return ExportError;
-               }
        } else if (!lyxrc.tex_allows_spaces
                   && contains(filePath(), ' ')) {
                Alert::error(_("File name error"),
@@ -3649,13 +4163,12 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                return ExportTexPathHasSpaces;
        } else {
                runparams.nice = false;
-               if (!makeLaTeXFile(FileName(filename), filePath(), runparams)) {
-                       if (d->cloned_buffer_) {
-                               d->cloned_buffer_->d->errorLists["Export"] =
-                                       d->errorLists["Export"];
-                       }
+               bool const success = makeLaTeXFile(
+                       FileName(filename), filePath(), runparams);
+               if (d->cloned_buffer_)
+                       d->cloned_buffer_->d->errorLists["Export"] = d->errorLists["Export"];
+               if (!success)
                        return ExportError;
-               }
        }
 
        string const error_type = (format == "program")
@@ -3670,22 +4183,26 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
        // Emit the signal to show the error list or copy it back to the
        // cloned Buffer so that it can be emitted afterwards.
        if (format != backend_format) {
-               if (d->cloned_buffer_) {
-                       d->cloned_buffer_->d->errorLists[error_type] = 
+               if (runparams.silent)
+                       error_list.clear();
+               else if (d->cloned_buffer_)
+                       d->cloned_buffer_->d->errorLists[error_type] =
                                d->errorLists[error_type];
-               } else 
+               else
                        errors(error_type);
                // also to the children, in case of master-buffer-view
                ListOfBuffers clist = getDescendents();
                ListOfBuffers::const_iterator cit = clist.begin();
                ListOfBuffers::const_iterator const cen = clist.end();
                for (; cit != cen; ++cit) {
-                       if (d->cloned_buffer_) {
+                       if (runparams.silent)
+                               (*cit)->d->errorLists[error_type].clear();
+                       else if (d->cloned_buffer_) {
                                // Enable reverse search by copying back the
                                // texrow object to the cloned buffer.
                                // FIXME: this is not thread safe.
                                (*cit)->d->cloned_buffer_->d->texrow = (*cit)->d->texrow;
-                               (*cit)->d->cloned_buffer_->d->errorLists[error_type] = 
+                               (*cit)->d->cloned_buffer_->d->errorLists[error_type] =
                                        (*cit)->d->errorLists[error_type];
                        } else
                                (*cit)->errors(error_type, true);
@@ -3702,12 +4219,10 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                d->cloned_buffer_->d->errorLists[error_type] = d->errorLists[error_type];
        }
 
-       if (!success)
-               return ExportConverterError;
 
        if (put_in_tempdir) {
                result_file = tmp_result_file.absFileName();
-               return ExportSuccess;
+               return success ? ExportSuccess : ExportConverterError;
        }
 
        if (dest_filename.empty())
@@ -3723,7 +4238,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
        bool use_force = use_gui ? lyxrc.export_overwrite == ALL_FILES
                                 : force_overwrite == ALL_FILES;
        CopyStatus status = use_force ? FORCE : SUCCESS;
-       
+
        vector<ExportedFile>::const_iterator it = files.begin();
        vector<ExportedFile>::const_iterator const en = files.end();
        for (; it != en && status != CANCEL; ++it) {
@@ -3746,8 +4261,8 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
        if (status == CANCEL) {
                message(_("Document export cancelled."));
                return ExportCancel;
-       } 
-       
+       }
+
        if (tmp_result_file.exists()) {
                // Finally copy the main file
                use_force = use_gui ? lyxrc.export_overwrite != NO_FILES
@@ -3772,7 +4287,7 @@ Buffer::ExportStatus Buffer::doExport(string const & target, bool put_in_tempdir
                        formats.prettyName(format)));
        }
 
-       return ExportSuccess;
+       return success ? ExportSuccess : ExportConverterError;
 }
 
 
@@ -3784,23 +4299,41 @@ Buffer::ExportStatus Buffer::preview(string const & format) const
        return preview(format, update_unincluded);
 }
 
+
 Buffer::ExportStatus Buffer::preview(string const & format, bool includeall) const
 {
        MarkAsExporting exporting(this);
        string result_file;
        // (1) export with all included children (omit \includeonly)
-       if (includeall) { 
+       if (includeall) {
                ExportStatus const status = doExport(format, true, true, result_file);
                if (status != ExportSuccess)
                        return status;
        }
        // (2) export with included children only
        ExportStatus const status = doExport(format, true, false, result_file);
+       FileName const previewFile(result_file);
+
+       LATTEST (isClone());
+       d->cloned_buffer_->d->preview_file_ = previewFile;
+       d->cloned_buffer_->d->preview_format_ = format;
+       d->cloned_buffer_->d->preview_error_ = (status != ExportSuccess);
+
        if (status != ExportSuccess)
                return status;
-       if (!formats.view(*this, FileName(result_file), format))
-               return PreviewError;
-       return PreviewSuccess;
+       if (previewFile.exists()) {
+               if (!formats.view(*this, previewFile, format))
+                       return PreviewError;
+               else
+                       return PreviewSuccess;
+       }
+       else {
+               // Successful export but no output file?
+               // Probably a bug in error detection.
+               LATTEST (status != ExportSuccess);
+
+               return status;
+       }
 }
 
 
@@ -3818,14 +4351,14 @@ Buffer::ReadStatus Buffer::extractFromVC()
 Buffer::ReadStatus Buffer::loadEmergency()
 {
        FileName const emergencyFile = getEmergencyFileName();
-       if (!emergencyFile.exists() 
+       if (!emergencyFile.exists()
                  || emergencyFile.lastModified() <= d->filename.lastModified())
                return ReadFileNotFound;
 
        docstring const file = makeDisplayPath(d->filename.absFileName(), 20);
        docstring const text = bformat(_("An emergency save of the document "
                "%1$s exists.\n\nRecover emergency save?"), file);
-       
+
        int const load_emerg = Alert::prompt(_("Load emergency save?"), text,
                0, 2, _("&Recover"), _("&Load Original"), _("&Cancel"));
 
@@ -3844,13 +4377,14 @@ Buffer::ReadStatus Buffer::loadEmergency()
                                        "file."), from_utf8(d->filename.absFileName())));
                        }
                        markDirty();
+                       lyxvc().file_found_hook(d->filename);
                        str = _("Document was successfully recovered.");
                } else
                        str = _("Document was NOT successfully recovered.");
                str += "\n\n" + bformat(_("Remove emergency file now?\n(%1$s)"),
                        makeDisplayPath(emergencyFile.absFileName()));
 
-               int const del_emerg = 
+               int const del_emerg =
                        Alert::prompt(_("Delete emergency file?"), str, 1, 1,
                                _("&Remove"), _("&Keep"));
                if (del_emerg == 0) {
@@ -3882,16 +4416,16 @@ Buffer::ReadStatus Buffer::loadAutosave()
 {
        // Now check if autosave file is newer.
        FileName const autosaveFile = getAutosaveFileName();
-       if (!autosaveFile.exists() 
-                 || autosaveFile.lastModified() <= d->filename.lastModified()) 
+       if (!autosaveFile.exists()
+                 || autosaveFile.lastModified() <= d->filename.lastModified())
                return ReadFileNotFound;
 
        docstring const file = makeDisplayPath(d->filename.absFileName(), 20);
-       docstring const text = bformat(_("The backup of the document %1$s " 
+       docstring const text = bformat(_("The backup of the document %1$s "
                "is newer.\n\nLoad the backup instead?"), file);
        int const ret = Alert::prompt(_("Load backup?"), text, 0, 2,
                _("&Load backup"), _("Load &original"), _("&Cancel"));
-       
+
        switch (ret)
        {
        case 0: {
@@ -3903,10 +4437,11 @@ Buffer::ReadStatus Buffer::loadAutosave()
                                        bformat(_("A backup file is successfully loaded, "
                                        "but the original file %1$s is marked read-only. "
                                        "Please make sure to save the document as a "
-                                       "different file."), 
+                                       "different file."),
                                        from_utf8(d->filename.absFileName())));
                        }
                        markDirty();
+                       lyxvc().file_found_hook(d->filename);
                        return ReadSuccess;
                }
                return ReadAutosaveFailure;
@@ -3933,7 +4468,7 @@ Buffer::ReadStatus Buffer::loadLyXFile()
        ReadStatus const ret_re = loadEmergency();
        if (ret_re == ReadSuccess || ret_re == ReadCancel)
                return ret_re;
-       
+
        ReadStatus const ret_ra = loadAutosave();
        if (ret_ra == ReadSuccess || ret_ra == ReadCancel)
                return ret_ra;
@@ -4006,19 +4541,23 @@ void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const
 
 void Buffer::setBuffersForInsets() const
 {
-       inset().setBuffer(const_cast<Buffer &>(*this)); 
+       inset().setBuffer(const_cast<Buffer &>(*this));
 }
 
 
 void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
 {
+       LBUFERR(!text().paragraphs().empty());
+
        // Use the master text class also for child documents
        Buffer const * const master = masterBuffer();
        DocumentClass const & textclass = master->params().documentClass();
-       
+
        // do this only if we are the top-level Buffer
-       if (master == this)
+       if (master == this) {
+               textclass.counters().reset(from_ascii("bibitem"));
                reloadBibInfoCache();
+       }
 
        // keep the buffers to be children in this set. If the call from the
        // master comes back we can see which of them were actually seen (i.e.
@@ -4029,9 +4568,15 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
                if (master != this) {
                        bufToUpdate.insert(this);
                        master->updateBuffer(UpdateMaster, utype);
-                       // Do this here in case the master has no gui associated with it. Then, 
-                       // the TocModel is not updated and TocModel::toc_ is invalid (bug 5699).
-                       if (!master->d->gui_)
+                       // If the master buffer has no gui associated with it, then the TocModel is
+                       // not updated during the updateBuffer call and TocModel::toc_ is invalid
+                       // (bug 5699). The same happens if the master buffer is open in a different
+                       // window. This test catches both possibilities.
+                       // See: http://marc.info/?l=lyx-devel&m=138590578911716&w=2
+                       // There remains a problem here: If there is another child open in yet a third
+                       // window, that TOC is not updated. So some more general solution is needed at
+                       // some point.
+                       if (master->d->gui_ != d->gui_)
                                structureChanged();
 
                        // was buf referenced from the master (i.e. not in bufToUpdate anymore)?
@@ -4049,11 +4594,10 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
        // update all caches
        clearReferenceCache();
        updateMacros();
+       setChangesPresent(false);
 
        Buffer & cbuf = const_cast<Buffer &>(*this);
 
-       LASSERT(!text().paragraphs().empty(), /**/);
-
        // do the real work
        ParIterator parit = cbuf.par_iterator_begin();
        updateBuffer(parit, utype);
@@ -4064,7 +4608,8 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
 
        d->bibinfo_cache_valid_ = true;
        d->cite_labels_valid_ = true;
-       cbuf.tocBackend().update();
+       /// FIXME: Perf
+       cbuf.tocBackend().update(true, utype);
        if (scope == UpdateMaster)
                cbuf.structureChanged();
 }
@@ -4077,6 +4622,11 @@ static depth_type getDepth(DocIterator const & it)
                if (!it[i].inset().inMathed())
                        depth += it[i].paragraph().getDepth() + 1;
        // remove 1 since the outer inset does not count
+       // we should have at least one non-math inset, so
+       // depth should nevery be 0. but maybe it is worth
+       // marking this, just in case.
+       LATTEST(depth > 0);
+       // coverity[INTEGER_OVERFLOW]
        return depth - 1;
 }
 
@@ -4125,7 +4675,7 @@ static depth_type getItemDepth(ParIterator const & it)
 static bool needEnumCounterReset(ParIterator const & it)
 {
        Paragraph const & par = *it;
-       LASSERT(par.layout().labeltype == LABEL_ENUMERATE, /**/);
+       LASSERT(par.layout().labeltype == LABEL_ENUMERATE, return false);
        depth_type const cur_depth = par.getDepth();
        ParIterator prev_it = it;
        while (prev_it.pit()) {
@@ -4149,9 +4699,11 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const
        Counters & counters = textclass.counters();
 
        if (par.params().startOfAppendix()) {
-               // FIXME: only the counter corresponding to toplevel
-               // sectioning should be reset
-               counters.reset();
+               // We want to reset the counter corresponding to toplevel sectioning
+               Layout const & lay = textclass.getTOCLayout();
+               docstring const cnt = lay.counter;
+               if (!cnt.empty())
+                       counters.reset(cnt);
                counters.appendix(true);
        }
        par.params().appendix(counters.appendix());
@@ -4170,17 +4722,6 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const
        }
 
        switch(layout.labeltype) {
-       case LABEL_COUNTER:
-               if (layout.toclevel <= bp.secnumdepth
-                     && (layout.latextype != LATEX_ENVIRONMENT
-                         || it.text()->isFirstInSequence(it.pit()))) {
-                       if (counters.hasCounter(layout.counter))
-                               counters.step(layout.counter, utype);
-                       par.params().labelString(par.expandLabel(layout, bp));
-               } else
-                       par.params().labelString(docstring());
-               break;
-
        case LABEL_ITEMIZE: {
                // At some point of time we should do something more
                // clever here, like:
@@ -4246,13 +4787,13 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const
                        if (counters.hasCounter(from_utf8(type))) {
                                string const & lang = par.getParLanguage(bp)->code();
                                counters.step(from_utf8(type), utype);
-                               full_label = bformat(from_ascii("%1$s %2$s:"), 
-                                                    name, 
+                               full_label = bformat(from_ascii("%1$s %2$s:"),
+                                                    name,
                                                     counters.theCounter(from_utf8(type), lang));
                        } else
-                               full_label = bformat(from_ascii("%1$s #:"), name);      
+                               full_label = bformat(from_ascii("%1$s #:"), name);
                }
-               par.params().labelString(full_label);   
+               par.params().labelString(full_label);
                break;
        }
 
@@ -4260,19 +4801,34 @@ void Buffer::Impl::setLabel(ParIterator & it, UpdateType utype) const
                par.params().labelString(docstring());
                break;
 
+       case LABEL_ABOVE:
+       case LABEL_CENTERED:
+       case LABEL_STATIC: {
+               docstring const & lcounter = layout.counter;
+               if (!lcounter.empty()) {
+                       if (layout.toclevel <= bp.secnumdepth
+                                               && (layout.latextype != LATEX_ENVIRONMENT
+                                       || it.text()->isFirstInSequence(it.pit()))) {
+                               if (counters.hasCounter(lcounter))
+                                       counters.step(lcounter, utype);
+                               par.params().labelString(par.expandLabel(layout, bp));
+                       } else
+                               par.params().labelString(docstring());
+               } else
+                       par.params().labelString(par.expandLabel(layout, bp));
+               break;
+       }
+
        case LABEL_MANUAL:
-       case LABEL_TOP_ENVIRONMENT:
-       case LABEL_CENTERED_TOP_ENVIRONMENT:
-       case LABEL_STATIC:      
        case LABEL_BIBLIO:
                par.params().labelString(par.expandLabel(layout, bp));
-               break;
        }
 }
 
 
 void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const
 {
+       // LASSERT: Is it safe to continue here, or should we just return?
        LASSERT(parit.pit() == 0, /**/);
 
        // Set the position of the text in the buffer to be able
@@ -4283,7 +4839,17 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const
        pit_type const lastpit = parit.lastpit();
        for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
                // reduce depth if necessary
-               parit->params().depth(min(parit->params().depth(), maxdepth));
+               if (parit->params().depth() > maxdepth) {
+                       /** FIXME: this function is const, but
+                        * nevertheless it modifies the buffer. To be
+                        * cleaner, one should modify the buffer in
+                        * another function, which is actually
+                        * non-const. This would however be costly in
+                        * terms of code duplication.
+                        */
+                       const_cast<Buffer *>(this)->undo().recordUndo(CursorData(parit));
+                       parit->params().depth(maxdepth);
+               }
                maxdepth = parit->getMaxDepthAfter();
 
                if (utype == OutputUpdate) {
@@ -4293,10 +4859,13 @@ void Buffer::updateBuffer(ParIterator & parit, UpdateType utype) const
                        masterBuffer()->params().documentClass().counters().
                                        setActiveLayout(parit->layout());
                }
-               
+
                // set the counter for this paragraph
                d->setLabel(parit, utype);
 
+               // update change-tracking flag 
+               parit->addChangesToBuffer(*this);
+
                // now the insets
                InsetList::const_iterator iit = parit->insetList().begin();
                InsetList::const_iterator end = parit->insetList().end();
@@ -4319,13 +4888,13 @@ int Buffer::spellCheck(DocIterator & from, DocIterator & to,
        DocIterator const end = to_end ? doc_iterator_end(this) : to;
        // OK, we start from here.
        for (; from != end; from.forwardPos()) {
-               // We are only interested in text so remove the math CursorSlice.
-               while (from.inMathed()) {
+               // This skips all insets with spell check disabled.
+               while (!from.allowSpellCheck()) {
                        from.pop_back();
                        from.pos()++;
                }
                // If from is at the end of the document (which is possible
-               // when leaving the mathed) LyX will crash later otherwise.
+               // when "from" was changed above) LyX will crash later otherwise.
                if (from.atEnd() || (!to_end && from >= end))
                        break;
                to = from;
@@ -4335,7 +4904,6 @@ int Buffer::spellCheck(DocIterator & from, DocIterator & to,
                        word_lang = wl;
                        break;
                }
-
                // Do not increase progress when from == to, otherwise the word
                // count will be wrong.
                if (from != to) {
@@ -4347,6 +4915,77 @@ int Buffer::spellCheck(DocIterator & from, DocIterator & to,
 }
 
 
+void Buffer::Impl::updateStatistics(DocIterator & from, DocIterator & to, bool skipNoOutput)
+{
+       bool inword = false;
+       word_count_ = 0;
+       char_count_ = 0;
+       blank_count_ = 0;
+
+       for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
+               if (!dit.inTexted()) {
+                       dit.forwardPos();
+                       continue;
+               }
+
+               Paragraph const & par = dit.paragraph();
+               pos_type const pos = dit.pos();
+
+               // Copied and adapted from isWordSeparator() in Paragraph
+               if (pos == dit.lastpos()) {
+                       inword = false;
+               } else {
+                       Inset const * ins = par.getInset(pos);
+                       if (ins && skipNoOutput && !ins->producesOutput()) {
+                               // skip this inset
+                               ++dit.top().pos();
+                               // stop if end of range was skipped
+                               if (!to.atEnd() && dit >= to)
+                                       break;
+                               continue;
+                       } else if (!par.isDeleted(pos)) {
+                               if (par.isWordSeparator(pos))
+                                       inword = false;
+                               else if (!inword) {
+                                       ++word_count_;
+                                       inword = true;
+                               }
+                               if (ins && ins->isLetter())
+                                       ++char_count_;
+                               else if (ins && ins->isSpace())
+                                       ++blank_count_;
+                               else {
+                                       char_type const c = par.getChar(pos);
+                                       if (isPrintableNonspace(c))
+                                               ++char_count_;
+                                       else if (isSpace(c))
+                                               ++blank_count_;
+                               }
+                       }
+               }
+               dit.forwardPos();
+       }
+}
+
+
+void Buffer::updateStatistics(DocIterator & from, DocIterator & to, bool skipNoOutput) const
+{
+       d->updateStatistics(from, to, skipNoOutput);
+}
+
+
+int Buffer::wordCount() const
+{
+       return d->wordCount();
+}
+
+
+int Buffer::charCount(bool with_blanks) const
+{
+       return d->charCount(with_blanks);
+}
+
+
 Buffer::ReadStatus Buffer::reload()
 {
        setBusy(true);
@@ -4356,6 +4995,8 @@ Buffer::ReadStatus Buffer::reload()
        d->filename.refresh();
        docstring const disp_fn = makeDisplayPath(d->filename.absFileName());
 
+       // clear parent. this will get reset if need be.
+       d->setParent(0);
        ReadStatus const status = loadLyXFile();
        if (status == ReadSuccess) {
                updateBuffer();
@@ -4366,7 +5007,7 @@ Buffer::ReadStatus Buffer::reload()
                d->undo_.clear();
        } else {
                message(bformat(_("Could not reload document %1$s."), disp_fn));
-       }       
+       }
        setBusy(false);
        removePreviews();
        updatePreviews();
@@ -4380,6 +5021,8 @@ bool Buffer::saveAs(FileName const & fn)
        FileName const old_name = fileName();
        FileName const old_auto = getAutosaveFileName();
        bool const old_unnamed = isUnnamed();
+       bool success = true;
+       d->old_position = filePath();
 
        setFileName(fn);
        markDirty();
@@ -4396,22 +5039,20 @@ bool Buffer::saveAs(FileName const & fn)
                // we need to check that the locations of child buffers
                // are still valid.
                checkChildBuffers();
-               return true;
+               checkMasterBuffer();
        } else {
                // save failed
                // reset the old filename and unnamed state
                setFileName(old_name);
                setUnnamed(old_unnamed);
-               return false;
+               success = false;
        }
+
+       d->old_position.clear();
+       return success;
 }
 
 
-// FIXME We could do better here, but it is complicated. What would be
-// nice is to offer either (a) to save the child buffer to an appropriate
-// location, so that it would "move with the master", or else (b) to update
-// the InsetInclude so that it pointed to the same file. But (a) is a bit
-// complicated, because the code for this lives in GuiView.
 void Buffer::checkChildBuffers()
 {
        Impl::BufferPositionMap::iterator it = d->children_positions.begin();
@@ -4431,11 +5072,6 @@ void Buffer::checkChildBuffers()
                if (oldloc == newloc)
                        continue;
                // the location of the child file is incorrect.
-               Alert::warning(_("Included File Invalid"),
-                               bformat(_("Saving this document to a new location has made the file:\n"
-                               "  %1$s\n"
-                               "inaccessible. You will need to update the included filename."),
-                               from_utf8(oldloc)));
                cbuf->setParent(0);
                inset_inc->setChildBuffer(0);
        }
@@ -4444,4 +5080,79 @@ void Buffer::checkChildBuffers()
        d->position_to_children.clear();
 }
 
+
+// If a child has been saved under a different name/path, it might have been
+// orphaned. Therefore the master needs to be reset (bug 8161).
+void Buffer::checkMasterBuffer()
+{
+       Buffer const * const master = masterBuffer();
+       if (master == this)
+               return;
+
+       // necessary to re-register the child (bug 5873)
+       // FIXME: clean up updateMacros (here, only
+       // child registering is needed).
+       master->updateMacros();
+       // (re)set master as master buffer, but only
+       // if we are a real child
+       if (master->isChild(this))
+               setParent(master);
+       else
+               setParent(0);
+}
+
+
+string Buffer::includedFilePath(string const & name, string const & ext) const
+{
+       if (d->old_position.empty() ||
+           equivalent(FileName(d->old_position), FileName(filePath())))
+               return name;
+
+       bool isabsolute = FileName::isAbsolute(name);
+       // both old_position and filePath() end with a path separator
+       string absname = isabsolute ? name : d->old_position + name;
+
+       // if old_position is set to origin, we need to do the equivalent of
+       // getReferencedFileName() (see readDocument())
+       if (!isabsolute && d->old_position == params().origin) {
+               FileName const test(addExtension(filePath() + name, ext));
+               if (test.exists())
+                       absname = filePath() + name;
+       }
+
+       if (!FileName(addExtension(absname, ext)).exists())
+               return name;
+
+       if (isabsolute)
+               return to_utf8(makeRelPath(from_utf8(name), from_utf8(filePath())));
+
+       return to_utf8(makeRelPath(from_utf8(FileName(absname).realPath()),
+                                  from_utf8(filePath())));
+}
+
+
+void Buffer::setChangesPresent(bool b) const
+{
+       d->tracked_changes_present_ = b;
+}
+
+
+bool Buffer::areChangesPresent() const
+{
+       return d->tracked_changes_present_;
+}
+
+
+void Buffer::updateChangesPresent() const
+{
+       LYXERR(Debug::CHANGES, "Buffer::updateChangesPresent");
+       setChangesPresent(false);
+       ParConstIterator it = par_iterator_begin();
+       ParConstIterator const end = par_iterator_end();
+       for (; !areChangesPresent() && it != end; ++it)
+               it->addChangesToBuffer(*this);
+}
+
+
+
 } // namespace lyx