]> git.lyx.org Git - lyx.git/blobdiff - src/buffer_funcs.cpp
infrastructure for 'graceful asserts'
[lyx.git] / src / buffer_funcs.cpp
index 544856a8e9fd0aad3c7aeee52c295a55f32d58cd..16a3ca1cb82aa6150564f7be6ae7659c9a4911bd 100644 (file)
 #include "Buffer.h"
 #include "BufferList.h"
 #include "BufferParams.h"
-#include "debug.h"
 #include "DocIterator.h"
 #include "Counters.h"
 #include "ErrorList.h"
 #include "Floating.h"
 #include "FloatList.h"
-#include "gettext.h"
 #include "InsetList.h"
-#include "InsetIterator.h"
 #include "Language.h"
 #include "LaTeX.h"
 #include "Layout.h"
-#include "LayoutPtr.h"
 #include "LyX.h"
 #include "TextClass.h"
-#include "TextClassList.h"
 #include "Paragraph.h"
 #include "paragraph_funcs.h"
 #include "ParagraphList.h"
 #include "insets/InsetBibitem.h"
 #include "insets/InsetInclude.h"
 
+#include "support/assert.h"
+#include "support/convert.h"
+#include "support/debug.h"
 #include "support/filetools.h"
-#include "support/lyxlib.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
-
-
-namespace lyx {
+#include "support/textutils.h"
 
 using namespace std;
+using namespace lyx::support;
 
-using support::bformat;
-using support::FileName;
-using support::libFileSearch;
-using support::makeAbsPath;
-using support::makeDisplayPath;
-using support::onlyFilename;
-using support::onlyPath;
-using support::unlink;
+namespace lyx {
 
 namespace Alert = frontend::Alert;
 
@@ -83,15 +73,23 @@ Buffer * checkAndLoadLyXFile(FileName const & filename)
                        return checkBuffer;
 
                // FIXME: should be LFUN_REVERT
-               if (theBufferList().close(checkBuffer, false))
-                       // Load it again.
-                       return checkAndLoadLyXFile(filename);
-               // The file could not be closed.
-               return 0;
+               theBufferList().release(checkBuffer);
+               // Load it again.
+               return checkAndLoadLyXFile(filename);
        }
 
-       if (filename.isFileReadable()) {
+       if (filename.exists()) {
+               if (!filename.isReadableFile()) {
+                       docstring text = bformat(_("The file %1$s exists but is not "
+                               "readable by the current user."),
+                               from_utf8(filename.absFilename()));
+                       Alert::error(_("File not readable!"), text);
+                       return 0;
+               }
                Buffer * b = theBufferList().newBuffer(filename.absFilename());
+               if (!b)
+                       // Buffer creation is not possible.
+                       return 0;
                if (!b->loadLyXFile(filename)) {
                        theBufferList().release(b);
                        return 0;
@@ -116,7 +114,9 @@ Buffer * newFile(string const & filename, string const & templatename,
 {
        // get a free buffer
        Buffer * b = theBufferList().newBuffer(filename);
-       BOOST_ASSERT(b);
+       if (!b)
+               // Buffer creation is not possible.
+               return 0;
 
        FileName tname;
        // use defaults.lyx as a default template if it exists.
@@ -149,6 +149,23 @@ Buffer * newFile(string const & filename, string const & templatename,
 }
 
 
+Buffer * newUnnamedFile(string const & templatename, FileName const & path)
+{
+       static int newfile_number;
+
+       string document_path = path.absFilename();
+       string filename = addName(document_path,
+               "newfile" + convert<string>(++newfile_number) + ".lyx");
+       while (theBufferList().exists(filename)
+               || FileName(filename).isReadableFile()) {
+               ++newfile_number;
+               filename = addName(document_path,
+                       "newfile" +     convert<string>(newfile_number) + ".lyx");
+       }
+       return newFile(filename, templatename, false);
+}
+
+
 int countWords(DocIterator const & from, DocIterator const & to)
 {
        int count = 0;
@@ -171,6 +188,36 @@ int countWords(DocIterator const & from, DocIterator const & to)
 }
 
 
+int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
+{
+       int chars = 0;
+       int blanks = 0;
+       for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
+
+               if (!dit.inTexted()) continue;
+               Paragraph const & par = dit.paragraph();
+               pos_type const pos = dit.pos();
+
+               if (pos != dit.lastpos() && !par.isDeleted(pos)) {
+                       if (Inset const * ins = par.getInset(pos)) {
+                               if (ins->isLetter())
+                                       ++chars;
+                               else if (with_blanks && ins->isSpace())
+                                       ++blanks;
+                       } else {
+                               char_type const c = par.getChar(pos);
+                               if (isPrintableNonspace(c))
+                                       ++chars;
+                               else if (isSpace(c) && with_blanks)
+                                       ++blanks;
+                       }
+               }
+       }
+
+       return chars + blanks;
+}
+
+
 namespace {
 
 depth_type getDepth(DocIterator const & it)
@@ -186,7 +233,7 @@ depth_type getDepth(DocIterator const & it)
 depth_type getItemDepth(ParIterator const & it)
 {
        Paragraph const & par = *it;
-       LabelType const labeltype = par.layout()->labeltype;
+       LabelType const labeltype = par.layout().labeltype;
 
        if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
                return 0;
@@ -210,13 +257,13 @@ depth_type getItemDepth(ParIterator const & it)
                // that is not more deeply nested.
                Paragraph & prev_par = *prev_it;
                depth_type const prev_depth = getDepth(prev_it);
-               if (labeltype == prev_par.layout()->labeltype) {
+               if (labeltype == prev_par.layout().labeltype) {
                        if (prev_depth < min_depth)
                                return prev_par.itemdepth + 1;
                        if (prev_depth == min_depth)
                                return prev_par.itemdepth;
                }
-               min_depth = std::min(min_depth, prev_depth);
+               min_depth = min(min_depth, prev_depth);
                // small optimization: if we are at depth 0, we won't
                // find anything else
                if (prev_depth == 0)
@@ -228,14 +275,14 @@ depth_type getItemDepth(ParIterator const & it)
 bool needEnumCounterReset(ParIterator const & it)
 {
        Paragraph const & par = *it;
-       BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
+       LASSERT(par.layout().labeltype == LABEL_ENUMERATE, /**/);
        depth_type const cur_depth = par.getDepth();
        ParIterator prev_it = it;
        while (prev_it.pit()) {
                --prev_it.top().pit();
                Paragraph const & prev_par = *prev_it;
                if (prev_par.getDepth() <= cur_depth)
-                       return  prev_par.layout()->labeltype != LABEL_ENUMERATE;
+                       return  prev_par.layout().labeltype != LABEL_ENUMERATE;
        }
        // start of nested inset: reset
        return true;
@@ -245,9 +292,9 @@ bool needEnumCounterReset(ParIterator const & it)
 // set the label of a paragraph. This includes the counters.
 void setLabel(Buffer const & buf, ParIterator & it)
 {
-       TextClass const & textclass = buf.params().getTextClass();
+       DocumentClass const & textclass = buf.params().documentClass();
        Paragraph & par = it.paragraph();
-       LayoutPtr const & layout = par.layout();
+       Layout const & layout = par.layout();
        Counters & counters = textclass.counters();
 
        if (par.params().startOfAppendix()) {
@@ -261,19 +308,19 @@ void setLabel(Buffer const & buf, ParIterator & it)
        // Compute the item depth of the paragraph
        par.itemdepth = getItemDepth(it);
 
-       if (layout->margintype == MARGIN_MANUAL) {
+       if (layout.margintype == MARGIN_MANUAL) {
                if (par.params().labelWidthString().empty())
-                       par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
+                       par.params().labelWidthString(par.translateIfPossible(layout.labelstring(), buf.params()));
        } else {
                par.params().labelWidthString(docstring());
        }
 
-       switch(layout->labeltype) {
+       switch(layout.labeltype) {
        case LABEL_COUNTER:
-               if (layout->toclevel <= buf.params().secnumdepth
-                   && (layout->latextype != LATEX_ENVIRONMENT
+               if (layout.toclevel <= buf.params().secnumdepth
+                   && (layout.latextype != LATEX_ENVIRONMENT
                        || isFirstInSequence(it.pit(), it.plist()))) {
-                       counters.step(layout->counter);
+                       counters.step(layout.counter);
                        par.params().labelString(
                                par.expandLabel(layout, buf.params()));
                } else
@@ -387,7 +434,7 @@ void setLabel(Buffer const & buf, ParIterator & it)
        case LABEL_STATIC:      
        case LABEL_BIBLIO:
                par.params().labelString(
-                       par.translateIfPossible(layout->labelstring(), 
+                       par.translateIfPossible(layout.labelstring(), 
                                                buf.params()));
                break;
        }
@@ -397,7 +444,13 @@ void setLabel(Buffer const & buf, ParIterator & it)
 
 void updateLabels(Buffer const & buf, ParIterator & parit)
 {
-       BOOST_ASSERT(parit.pit() == 0);
+       LASSERT(parit.pit() == 0, /**/);
+
+       // set the position of the text in the buffer to be able
+       // to resolve macros in it. This has nothing to do with
+       // labels, but by putting it here we avoid implementing
+       // a whole bunch of traversal routines just for this call.
+       parit.text()->setMacrocontextPosition(parit);
 
        depth_type maxdepth = 0;
        pit_type const lastpit = parit.lastpit();
@@ -414,10 +467,9 @@ void updateLabels(Buffer const & buf, ParIterator & parit)
                InsetList::const_iterator end = parit->insetList().end();
                for (; iit != end; ++iit) {
                        parit.pos() = iit->pos;
-                       iit->inset->updateLabels(buf, parit);
+                       iit->inset->updateLabels(parit);
                }
        }
-       
 }
 
 
@@ -427,7 +479,7 @@ void updateLabels(Buffer const & buf, bool childonly)
 {
        Buffer const * const master = buf.masterBuffer();
        // Use the master text class also for child documents
-       TextClass const & textclass = master->params().getTextClass();
+       DocumentClass const & textclass = master->params().documentClass();
 
        if (!childonly) {
                // If this is a child document start with the master
@@ -438,6 +490,8 @@ void updateLabels(Buffer const & buf, bool childonly)
 
                // start over the counters
                textclass.counters().reset();
+               buf.clearReferenceCache();
+               buf.updateMacros();
        }
 
        Buffer & cbuf = const_cast<Buffer &>(buf);
@@ -461,14 +515,4 @@ void updateLabels(Buffer const & buf, bool childonly)
 }
 
 
-void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
-{
-       if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
-               Buffer * master = buffer.masterBuffer();
-               master->tocBackend().updateItem(par_it);
-               master->structureChanged();
-       }
-}
-
-
 } // namespace lyx