]> git.lyx.org Git - lyx.git/blobdiff - src/buffer_funcs.cpp
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[lyx.git] / src / buffer_funcs.cpp
index ee2b9a6b6559271b78b2ec68b634ce4bfefe0c41..23ad4eb1284fa5a4d3127ffa6403ed57915c47a1 100644 (file)
@@ -49,6 +49,7 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/textutils.h"
 
 using namespace std;
 using namespace lyx::support;
@@ -81,6 +82,9 @@ Buffer * checkAndLoadLyXFile(FileName const & filename)
 
        if (filename.isReadableFile()) {
                Buffer * b = theBufferList().newBuffer(filename.absFilename());
+               if (!b)
+                       // Buffer creation is not possible.
+                       return 0;
                if (!b->loadLyXFile(filename)) {
                        theBufferList().release(b);
                        return 0;
@@ -105,7 +109,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.
@@ -177,6 +183,33 @@ 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()
+                   && dit.pos() != dit.lastpos()
+                   && !dit.paragraph().isDeleted(dit.pos())) {
+                       if (dit.paragraph().isInset(dit.pos())) {
+                               if (dit.paragraph().getInset(dit.pos())->isLetter())
+                                       ++chars;
+                               else if (dit.paragraph().getInset(dit.pos())->isSpace() && with_blanks)
+                                       ++blanks;
+                       } else {
+                               char_type const c = dit.paragraph().getChar(dit.pos());
+                               if (isPrintableNonspace(c))
+                                       ++chars;
+                               else if (isSpace(c) && with_blanks)
+                                       ++blanks;
+                       }
+               }
+       }
+
+       return chars + blanks;
+}
+
+
 namespace {
 
 depth_type getDepth(DocIterator const & it)
@@ -405,6 +438,12 @@ void updateLabels(Buffer const & buf, ParIterator & parit)
 {
        BOOST_ASSERT(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();
        for ( ; parit.pit() <= lastpit ; ++parit.pit()) {