]> git.lyx.org Git - lyx.git/blobdiff - src/buffer_funcs.cpp
Update notes.
[lyx.git] / src / buffer_funcs.cpp
index f82535bae7a5b54af6d7253d6b6f54a15f5c01ff..8224241ee1db2a4a871648da302dc2cb653ca8bb 100644 (file)
@@ -28,7 +28,6 @@
 #include "LyX.h"
 #include "TextClass.h"
 #include "Paragraph.h"
-#include "paragraph_funcs.h"
 #include "ParagraphList.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
@@ -76,6 +75,7 @@ Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
                        return checkBuffer;
 
                // FIXME: should be LFUN_REVERT
+               checkBuffer->markClean();
                theBufferList().release(checkBuffer);
                // Load it again.
                return checkAndLoadLyXFile(filename);
@@ -153,56 +153,85 @@ Buffer * newFile(string const & filename, string const & templatename,
 }
 
 
-Buffer * newUnnamedFile(string const & templatename, FileName const & path)
+Buffer * newUnnamedFile(FileName const & path, string const & prefix,
+                                               string const & templatename)
 {
-       static int newfile_number;
-
-       FileName filename(path, 
-               "newfile" + convert<string>(++newfile_number) + ".lyx");
-       while (theBufferList().exists(filename)
-               || filename.isReadableFile()) {
-               ++newfile_number;
-               filename.set(path,
-                       "newfile" +     convert<string>(newfile_number) + ".lyx");
+       static map<string, int> file_number;
+
+       FileName filename;
+
+       do {
+               filename.set(path, 
+                       prefix + convert<string>(++file_number[prefix]) + ".lyx");
        }
+       while (theBufferList().exists(filename) || filename.isReadableFile());
+               
        return newFile(filename.absFilename(), templatename, false);
 }
 
 
+/* 
+ * FIXME : merge with countChars. The structures of the two functions
+ * are similar but, unfortunately, they seem to have a different
+ * notion of what to count. Since nobody ever complained about that,
+ * this proves (again) that any number beats no number ! (JMarc)
+ */
 int countWords(DocIterator const & from, DocIterator const & to)
 {
        int count = 0;
        bool inword = false;
-       for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
-               // Copied and adapted from isLetter() in ControlSpellChecker
-               if (dit.inTexted()
-                   && dit.pos() != dit.lastpos()
-                   && dit.paragraph().isLetter(dit.pos())
-                   && !dit.paragraph().isDeleted(dit.pos())) {
-                       if (!inword) {
+       for (DocIterator dit = from ; dit != to ; ) {
+               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() && !par.isDeleted(pos)) {
+                       Inset const * ins = par.getInset(pos);
+                       if (ins && !ins->producesOutput()) {
+                               //skip this inset
+                               ++dit.top().pos();
+                               continue;
+                       }
+                       if (par.isWordSeparator(pos)) 
+                               inword = false;
+                       else if (!inword) {
                                ++count;
                                inword = true;
                        }
-               } else if (inword)
-                       inword = false;
+               }
+               dit.forwardPos();
        }
 
        return count;
 }
 
 
-int countChars(DocIterator const & from, DocIterator const & to, bool with_blanks)
+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;
+       for (DocIterator dit = from ; dit != to ; ) {
+               if (!dit.inTexted()) {
+                       dit.forwardPos();
+                       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->producesOutput()) {
+                                       //skip this inset
+                                       ++dit.top().pos();
+                                       continue;
+                               }
                                if (ins->isLetter())
                                        ++chars;
                                else if (with_blanks && ins->isSpace())
@@ -215,9 +244,33 @@ int countChars(DocIterator const & from, DocIterator const & to, bool with_blank
                                        ++blanks;
                        }
                }
+               dit.forwardPos();
        }
 
        return chars + blanks;
 }
 
+
+Buffer * loadIfNeeded(FileName const & fname)
+{
+       Buffer * buffer = theBufferList().getBuffer(fname);
+       if (!buffer) {
+               if (!fname.exists())
+                       return 0;
+
+               buffer = theBufferList().newBuffer(fname.absFilename());
+               if (!buffer)
+                       // Buffer creation is not possible.
+                       return 0;
+
+               if (!buffer->loadLyXFile(fname)) {
+                       //close the buffer we just opened
+                       theBufferList().release(buffer);
+                       return 0;
+               }
+       }
+       return buffer;
+}
+
+
 } // namespace lyx