]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
That didn't really work. So revert to old CSS for gray notes.
[lyx.git] / src / Buffer.cpp
index b9a4f42958f21ff86a456d392c0ab6859b4a64e3..90512f0b63cbab0329fd588e93584ff600bbbd96 100644 (file)
@@ -127,7 +127,7 @@ namespace {
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 370; // uwestoehr: option to suppress default date
+int const LYX_FORMAT = 373; // jspitzm: merge g-brief class
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
@@ -253,7 +253,11 @@ public:
                return parent_buffer; 
        }
        ///
-       void setParent(Buffer const * pb) { parent_buffer = pb; }
+       void setParent(Buffer const * pb) {
+               if (parent_buffer && pb && parent_buffer != pb)
+                       LYXERR0("Warning: a buffer should not have two parents!");
+               parent_buffer = pb;
+       }
 private:
        /// So we can force access via the accessors.
        mutable Buffer const * parent_buffer;
@@ -298,7 +302,7 @@ Buffer::Buffer(string const & file, bool readonly)
 {
        LYXERR(Debug::INFO, "Buffer::Buffer()");
 
-       d->inset = new InsetText(*this);
+       d->inset = new InsetText(this);
        d->inset->setAutoBreakRows(true);
        d->inset->getText(0)->setMacrocontextPosition(par_iterator_begin());
 }
@@ -352,6 +356,17 @@ Buffer::~Buffer()
 }
 
 
+Buffer * Buffer::clone() const
+{
+       Buffer * clone = new Buffer(fileName().absFilename(), false);
+       clone->d->file_fully_loaded = true;
+       clone->d->params = d->params;
+       clone->d->inset = static_cast<InsetText *>(d->inset->clone());
+       clone->d->inset->setBuffer(*clone);
+       return clone;
+}
+
+
 void Buffer::changed() const
 {
        if (d->wa_)
@@ -701,6 +716,7 @@ bool Buffer::readDocument(Lexer & lex)
        // read main text
        bool const res = text().read(lex, errorList, d->inset);
 
+       usermacros.clear();
        updateMacros();
        updateMacroInstances();
        return res;
@@ -1392,7 +1408,7 @@ void Buffer::makeLyXHTMLFile(FileName const & fname,
                              OutputParams const & runparams,
                              bool const body_only) const
 {
-       LYXERR(Debug::LATEX, "makeLYXHTMLFile...");
+       LYXERR(Debug::LATEX, "makeLyXHTMLFile...");
 
        ofdocstream ofs;
        if (!openFileWrite(ofs, fname))
@@ -1412,14 +1428,15 @@ void Buffer::writeLyXHTMLSource(odocstream & os,
 {
        LaTeXFeatures features(*this, params(), runparams);
        validate(features);
+       updateLabels(UpdateMaster, true);
 
        d->texrow.reset();
 
        if (!only_body) {
-               os << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" <<
-                       " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
+               os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+               os << "<!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";
                // FIXME Language should be set properly.
-               os << "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
+               os << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
                // FIXME Header
                os << "<head>\n";
                // FIXME Presumably need to set this right
@@ -1427,21 +1444,24 @@ void Buffer::writeLyXHTMLSource(odocstream & os,
                // FIXME Get this during validation? What about other meta-data?
                os << "<title>TBA</title>\n";
 
-               os << features.getTClassHTMLPreamble();
-
-               os << '\n';
+               os << "\n<!-- Text Class Preamble -->\n"
+                       << features.getTClassHTMLPreamble()
+                       << "\n<!-- Premable 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";
-                       os << styleinfo;
-                       os << "</style>\n";
+                       os << "<style type='text/css'>\n"
+                               << styleinfo
+                               << "</style>\n";
                }
                os << "</head>\n<body>\n";
        }
 
+       XHTMLStream xs(os);
        params().documentClass().counters().reset();
-       xhtmlParagraphs(text(), *this, os, runparams);
+       xhtmlParagraphs(text(), *this, xs, runparams);
        if (!only_body)
                os << "</body>\n</html>\n";
 }
@@ -1742,6 +1762,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
        string const argument = to_utf8(func.argument());
        // We'll set this back to false if need be.
        bool dispatched = true;
+       undo().beginUndoGroup();
 
        switch (func.action) {
        case LFUN_BUFFER_TOGGLE_READ_ONLY:
@@ -1756,7 +1777,6 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                        lyx::dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
                        break;
                }
-               doExport(argument, false);
                bool success = doExport(argument, false);
                dr.setError(success);
                if (!success)
@@ -1856,21 +1876,25 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                break;
 
        case LFUN_BRANCH_ADD: {
-               BranchList & branchList = params().branchlist();
-               docstring const branchName = func.argument();
-               if (branchName.empty()) {
+               docstring const branch_name = func.argument();
+               if (branch_name.empty()) {
                        dispatched = false;
                        break;
                }
-               Branch * branch = branchList.find(branchName);
+               BranchList & branch_list = params().branchlist();
+               Branch * branch = branch_list.find(branch_name);
                if (branch) {
-                       LYXERR0("Branch " << branchName << " does already exist.");
+                       LYXERR0("Branch " << branch_name << " already exists.");
                        dr.setError(true);
                        docstring const msg = 
-                               bformat(_("Branch \"%1$s\" does already exist."), branchName);
+                               bformat(_("Branch \"%1$s\" already exists."), branch_name);
                        dr.setMessage(msg);
                } else {
-                       branchList.add(branchName);
+                       branch_list.add(branch_name);
+                       branch = branch_list.find(branch_name);
+                       string const x11hexname = X11hexname(branch->color());
+                       docstring const str = branch_name + ' ' + from_ascii(x11hexname);
+                       lyx::dispatch(FuncRequest(LFUN_SET_COLOR, str));        
                        dr.setError(false);
                        dr.update(Update::Force);
                }
@@ -2065,6 +2089,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                break;
        }
        dr.dispatched(dispatched);
+       undo().endUndoGroup();
 }
 
 
@@ -2331,7 +2356,7 @@ DocIterator Buffer::firstChildPosition(Buffer const * child)
 }
 
 
-std::vector<Buffer *> Buffer::getChildren() const
+std::vector<Buffer *> Buffer::getChildren(bool grand_children) const
 {
        std::vector<Buffer *> clist;
        // loop over children
@@ -2340,11 +2365,13 @@ std::vector<Buffer *> Buffer::getChildren() const
        for (; it != end; ++it) {
                Buffer * child = const_cast<Buffer *>(it->first);
                clist.push_back(child);
-               // there might be grandchildren
-               std::vector<Buffer *> glist = child->getChildren();
-               for (vector<Buffer *>::const_iterator git = glist.begin();
-                    git != glist.end(); ++git)
-                       clist.push_back(*git);
+               if (grand_children) {
+                       // there might be grandchildren
+                       std::vector<Buffer *> glist = child->getChildren();
+                       for (vector<Buffer *>::const_iterator git = glist.begin();
+                                git != glist.end(); ++git)
+                               clist.push_back(*git);
+               }
        }
        return clist;
 }
@@ -2557,7 +2584,7 @@ void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
                        // get macro data
                        MathMacroTemplate & macroTemplate =
                                static_cast<MathMacroTemplate &>(*iit->inset);
-                       MacroContext mc(*this, it);
+                       MacroContext mc(this, it);
                        macroTemplate.updateToContext(mc);
 
                        // valid?
@@ -2569,8 +2596,10 @@ void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
                                continue;
 
                        // register macro
+                       // FIXME (Abdel), I don't understandt why we pass 'it' here
+                       // instead of 'macroTemplate' defined above... is this correct?
                        d->macros[macroTemplate.name()][it] =
-                               Impl::ScopeMacro(scope, MacroData(*this, it));
+                               Impl::ScopeMacro(scope, MacroData(const_cast<Buffer *>(this), it));
                }
 
                // next paragraph
@@ -2637,20 +2666,17 @@ void Buffer::updateMacroInstances() const
        LYXERR(Debug::MACROS, "updateMacroInstances for "
                << d->filename.onlyFileName());
        DocIterator it = doc_iterator_begin(this);
-       DocIterator end = doc_iterator_end(this);
-       for (; it != end; it.forwardPos()) {
-               // look for MathData cells in InsetMathNest insets
-               Inset * inset = it.nextInset();
-               if (!inset)
-                       continue;
-
-               InsetMath * minset = inset->asInsetMath();
+       it.forwardInset();
+       DocIterator const end = doc_iterator_end(this);
+       for (; it != end; it.forwardInset()) {
+               // look for MathData cells in InsetMathNest insets
+               InsetMath * minset = it.nextInset()->asInsetMath();
                if (!minset)
                        continue;
 
                // update macro in all cells of the InsetMathNest
                DocIterator::idx_type n = minset->nargs();
-               MacroContext mc = MacroContext(*this, it);
+               MacroContext mc = MacroContext(this, it);
                for (DocIterator::idx_type i = 0; i < n; ++i) {
                        MathData & data = minset->cell(i);
                        data.updateMacros(0, mc);
@@ -2949,6 +2975,15 @@ private:
 
 int AutoSaveBuffer::generateChild()
 {
+#if defined(__APPLE__)
+       /* 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
+        * version, I just disable forking altogether for now (JMarc)
+        */
+       pid_t const pid = -1;
+#else
        // tmp_ret will be located (usually) in /tmp
        // will that be a problem?
        // Note that this calls ForkedCalls::fork(), so it's
@@ -2958,6 +2993,7 @@ int AutoSaveBuffer::generateChild()
        // you should set pid to -1, and comment out the fork.
        if (pid != 0 && pid != -1)
                return pid;
+#endif
 
        // pid = -1 signifies that lyx was unable
        // to fork. But we will do the save
@@ -3120,12 +3156,15 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
        updateMacroInstances();
 
        // Plain text backend
-       if (backend_format == "text")
+       if (backend_format == "text") {
+               runparams.flavor = OutputParams::TEXT;
                writePlaintextFile(*this, FileName(filename), runparams);
-       // no backend
-       else if (backend_format == "xhtml")
+       }
+       // HTML backend
+       else if (backend_format == "xhtml") {
+               runparams.flavor = OutputParams::HTML;
                makeLyXHTMLFile(FileName(filename), runparams);
-       else if (backend_format == "lyx")
+       }       else if (backend_format == "lyx")
                writeFile(FileName(filename));
        // Docbook backend
        else if (isDocBook()) {
@@ -3237,7 +3276,7 @@ bool Buffer::isExportable(string const & format) const
 
 vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
 {
-       vector<string> backs = backends();
+       vector<string> const backs = backends();
        vector<Format const *> result =
                theConverters().getReachable(backs[0], only_viewable, true);
        for (vector<string>::const_iterator it = backs.begin() + 1;
@@ -3253,14 +3292,12 @@ vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
 vector<string> Buffer::backends() const
 {
        vector<string> v;
-       if (params().baseClass()->isTeXClassAvailable()) {
-               v.push_back(bufferFormat());
-               // FIXME: Don't hardcode format names here, but use a flag
-               if (v.back() == "latex")
-                       v.push_back("pdflatex");
-       }
-       v.push_back("text");
+       v.push_back(bufferFormat());
+       // FIXME: Don't hardcode format names here, but use a flag
+       if (v.back() == "latex")
+               v.push_back("pdflatex");
        v.push_back("xhtml");
+       v.push_back("text");
        v.push_back("lyx");
        return v;
 }
@@ -3300,7 +3337,8 @@ bool Buffer::readFileHelper(FileName const & s)
                                str = _("Document was successfully recovered.");
                        else
                                str = _("Document was NOT successfully recovered.");
-                       str += "\n\n" + _("Remove emergency file now?");
+                       str += "\n\n" + bformat(_("Remove emergency file now?\n(%1$s)"),
+                                               from_utf8(e.absFilename()));
 
                        if (!Alert::prompt(_("Delete emergency file?"), str, 1, 1,
                                        _("&Remove"), _("&Keep it"))) {
@@ -3413,7 +3451,7 @@ void Buffer::setBuffersForInsets() const
 }
 
 
-void Buffer::updateLabels(UpdateScope scope) const
+void Buffer::updateLabels(UpdateScope scope, bool out) const
 {
        // Use the master text class also for child documents
        Buffer const * const master = masterBuffer();
@@ -3427,7 +3465,7 @@ void Buffer::updateLabels(UpdateScope scope) const
                // If this is a child document start with the master
                if (master != this) {
                        bufToUpdate.insert(this);
-                       master->updateLabels();
+                       master->updateLabels(UpdateMaster, out);
                        // 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->gui_)
@@ -3455,7 +3493,7 @@ void Buffer::updateLabels(UpdateScope scope) const
 
        // do the real work
        ParIterator parit = cbuf.par_iterator_begin();
-       updateLabels(parit);
+       updateLabels(parit, out);
 
        if (master != this)
                // TocBackend update will be done later.
@@ -3537,9 +3575,9 @@ static bool needEnumCounterReset(ParIterator const & it)
 
 
 // set the label of a paragraph. This includes the counters.
-static void setLabel(Buffer const & buf, ParIterator & it)
+void Buffer::setLabel(ParIterator & it) const
 {
-       BufferParams const & bp = buf.masterBuffer()->params();
+       BufferParams const & bp = this->masterBuffer()->params();
        DocumentClass const & textclass = bp.documentClass();
        Paragraph & par = it.paragraph();
        Layout const & layout = par.layout();
@@ -3547,7 +3585,7 @@ static void setLabel(Buffer const & buf, ParIterator & it)
 
        if (par.params().startOfAppendix()) {
                // FIXME: only the counter corresponding to toplevel
-               // sectionning should be reset
+               // sectioning should be reset
                counters.reset();
                counters.appendix(true);
        }
@@ -3635,9 +3673,9 @@ static void setLabel(Buffer const & buf, ParIterator & it)
                string const & type = counters.current_float();
                docstring full_label;
                if (type.empty())
-                       full_label = buf.B_("Senseless!!! ");
+                       full_label = this->B_("Senseless!!! ");
                else {
-                       docstring name = buf.B_(textclass.floats().getType(type).name());
+                       docstring name = this->B_(textclass.floats().getType(type).name());
                        if (counters.hasCounter(from_utf8(type))) {
                                string const & lang = par.getParLanguage(bp)->code();
                                counters.step(from_utf8(type));
@@ -3666,7 +3704,7 @@ static void setLabel(Buffer const & buf, ParIterator & it)
 }
 
 
-void Buffer::updateLabels(ParIterator & parit) const
+void Buffer::updateLabels(ParIterator & parit, bool out) const
 {
        LASSERT(parit.pit() == 0, /**/);
 
@@ -3684,14 +3722,14 @@ void Buffer::updateLabels(ParIterator & parit) const
                maxdepth = parit->getMaxDepthAfter();
 
                // set the counter for this paragraph
-               setLabel(*this, parit);
+               setLabel(parit);
 
                // Now the insets
                InsetList::const_iterator iit = parit->insetList().begin();
                InsetList::const_iterator end = parit->insetList().end();
                for (; iit != end; ++iit) {
                        parit.pos() = iit->pos;
-                       iit->inset->updateLabels(parit);
+                       iit->inset->updateLabels(parit, out);
                }
        }
 }
@@ -3708,15 +3746,26 @@ int Buffer::spellCheck(DocIterator & from, DocIterator & to,
        DocIterator const end = doc_iterator_end(this);
        for (; from != end; from.forwardPos()) {
                // We are only interested in text so remove the math CursorSlice.
-               while (from.inMathed())
-                       from.forwardInset();
+               while (from.inMathed()) {
+                       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.
+               if (from == end)
+                       break;
                to = from;
                if (from.paragraph().spellCheck(from.pos(), to.pos(), wl, suggestions)) {
                        word_lang = wl;
                        break;
                }
-               from = to;
-               ++progress;
+
+               // Do not increase progress when from == to, otherwise the word
+               // count will be wrong.
+               if (from != to) {
+                       from = to;
+                       ++progress;
+               }
        }
        return progress;
 }