]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
ws changes only
[lyx.git] / src / buffer.C
index 328f310bdd3be54782c35502ca00dde10a1bc895..d3c34619e572975e8c74b30c94222d6f20c34b50 100644 (file)
 
 #include "buffer.h"
 
+#include "author.h"
 #include "buffer_funcs.h"
 #include "bufferlist.h"
+#include "bufferparams.h"
+#include "Bullet.h"
 #include "Chktex.h"
 #include "debug.h"
 #include "errorlist.h"
 #include "exporter.h"
 #include "format.h"
+#include "funcrequest.h"
 #include "gettext.h"
 #include "iterators.h"
 #include "language.h"
 #include "LyXAction.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
+#include "lyxvc.h"
 #include "messages.h"
+#include "paragraph.h"
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "sgml.h"
+#include "texrow.h"
 #include "undo.h"
 #include "version.h"
 
@@ -46,7 +53,6 @@
 #include "support/FileInfo.h"
 #include "support/filetools.h"
 #include "support/gzstream.h"
-#include "support/LAssert.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
 #include "support/path.h"
 #ifdef HAVE_LOCALE
 #endif
 
-using namespace lyx::support;
+using lyx::pos_type;
+
+using lyx::support::AddName;
+using lyx::support::ascii_lowercase;
+using lyx::support::atoi;
+using lyx::support::bformat;
+using lyx::support::ChangeExtension;
+using lyx::support::cmd_ret;
+using lyx::support::compare_ascii_no_case;
+using lyx::support::compare_no_case;
+using lyx::support::contains;
+using lyx::support::CreateBufferTmpDir;
+using lyx::support::destroyDir;
+using lyx::support::FileInfo;
+using lyx::support::FileInfo;
+using lyx::support::getExtFromContents;
+using lyx::support::IsDirWriteable;
+using lyx::support::IsFileWriteable;
+using lyx::support::LibFileSearch;
+using lyx::support::ltrim;
+using lyx::support::MakeAbsPath;
+using lyx::support::MakeDisplayPath;
+using lyx::support::MakeLatexName;
+using lyx::support::OnlyFilename;
+using lyx::support::OnlyPath;
+using lyx::support::Path;
+using lyx::support::QuoteName;
+using lyx::support::removeAutosaveFile;
+using lyx::support::rename;
+using lyx::support::RunCommand;
+using lyx::support::split;
+using lyx::support::strToInt;
+using lyx::support::subst;
+using lyx::support::tempName;
+using lyx::support::trim;
+
+namespace os = lyx::support::os;
 
 using std::endl;
 using std::for_each;
@@ -81,8 +123,7 @@ using std::ofstream;
 using std::pair;
 using std::stack;
 using std::vector;
-
-using lyx::pos_type;
+using std::string;
 
 
 // all these externs should eventually be removed.
@@ -105,25 +146,64 @@ bool openFileWrite(ofstream & ofs, string const & fname)
        return true;
 }
 
-
 } // namespace anon
 
+
+typedef std::map<string, bool> DepClean;
+
+struct Buffer::Impl
+{
+       Impl(Buffer & parent, string const & file, bool readonly);
+
+       limited_stack<Undo> undostack;
+       limited_stack<Undo> redostack;
+       BufferParams params;
+       ParagraphList paragraphs;
+       LyXVC lyxvc;
+       string temppath;
+       bool nicefile;
+       TexRow texrow;
+
+       /// need to regenerate .tex ?
+       DepClean dep_clean;
+
+       /// is save needed
+       mutable bool lyx_clean;
+
+       /// is autosave needed
+       mutable bool bak_clean;
+
+       /// is this a unnamed file (New...)
+       bool unnamed;
+
+       /// buffer is r/o
+       bool read_only;
+
+       /// name of the file the buffer is associated with.
+       string filename;
+
+       /// The path to the document file.
+       string filepath;
+
+       boost::scoped_ptr<Messages> messages;
+};
+
+
+Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
+       : nicefile(true),
+         lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
+         filename(file), filepath(OnlyPath(file))
+{
+       lyxvc.buffer(&parent);
+       if (readonly_ || lyxrc.use_tempdir)
+               temppath = CreateBufferTmpDir();
+}
+
+
 Buffer::Buffer(string const & file, bool ronly)
-       : nicefile_(true), lyx_clean(true), bak_clean(true),
-         unnamed(false), read_only(ronly),
-         filename_(file)
+       : pimpl_(new Impl(*this, file, ronly))
 {
        lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
-       filepath_ = OnlyPath(file);
-       lyxvc().buffer(this);
-       if (read_only || lyxrc.use_tempdir) {
-               temppath_ = CreateBufferTmpDir();
-       } else {
-               temppath_.erase();
-       }
-
-       // set initial author
-       authors().record(Author(lyxrc.user_name, lyxrc.user_email));
 }
 
 
@@ -149,91 +229,91 @@ Buffer::~Buffer()
 
 limited_stack<Undo> & Buffer::undostack()
 {
-       return undostack_;
+       return pimpl_->undostack;
 }
 
 
 limited_stack<Undo> const & Buffer::undostack() const
 {
-       return undostack_;
+       return pimpl_->undostack;
 }
 
 
 limited_stack<Undo> & Buffer::redostack()
 {
-       return redostack_;
+       return pimpl_->redostack;
 }
 
 
 limited_stack<Undo> const & Buffer::redostack() const
 {
-       return redostack_;
+       return pimpl_->redostack;
 }
 
 
 BufferParams & Buffer::params()
 {
-       return params_;
+       return pimpl_->params;
 }
 
 
 BufferParams const & Buffer::params() const
 {
-       return params_;
+       return pimpl_->params;
 }
 
 
 ParagraphList & Buffer::paragraphs()
 {
-       return paragraphs_;
+       return pimpl_->paragraphs;
 }
 
 
 ParagraphList const & Buffer::paragraphs() const
 {
-       return paragraphs_;
+       return pimpl_->paragraphs;
 }
 
 
 LyXVC & Buffer::lyxvc()
 {
-       return lyxvc_;
+       return pimpl_->lyxvc;
 }
 
 
 LyXVC const & Buffer::lyxvc() const
 {
-       return lyxvc_;
+       return pimpl_->lyxvc;
 }
 
 
 string const & Buffer::temppath() const
 {
-       return temppath_;
+       return pimpl_->temppath;
 }
 
 
 bool & Buffer::niceFile()
 {
-       return nicefile_;
+       return pimpl_->nicefile;
 }
 
 
 bool Buffer::niceFile() const
 {
-       return nicefile_;
+       return pimpl_->nicefile;
 }
 
 
 TexRow & Buffer::texrow()
 {
-       return texrow_;
+       return pimpl_->texrow;
 }
 
 
 TexRow const & Buffer::texrow() const
 {
-       return texrow_;
+       return pimpl_->texrow;
 }
 
 
@@ -281,24 +361,18 @@ pair<Buffer::LogType, string> const Buffer::getLogName() const
 
 void Buffer::setReadonly(bool flag)
 {
-       if (read_only != flag) {
-               read_only = flag;
+       if (pimpl_->read_only != flag) {
+               pimpl_->read_only = flag;
                readonly(flag);
        }
 }
 
 
-AuthorList & Buffer::authors()
-{
-       return params().authorlist;
-}
-
-
 void Buffer::setFileName(string const & newfile)
 {
-       filename_ = MakeAbsPath(newfile);
-       filepath_ = OnlyPath(filename_);
-       setReadonly(IsFileWriteable(filename_) == 0);
+       pimpl_->filename = MakeAbsPath(newfile);
+       pimpl_->filepath = OnlyPath(pimpl_->filename);
+       setReadonly(IsFileWriteable(pimpl_->filename) == 0);
        updateTitles();
 }
 
@@ -407,7 +481,7 @@ bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
 
 int Buffer::readParagraph(LyXLex & lex, string const & token,
                          ParagraphList & pars, ParagraphList::iterator & pit,
-                         Paragraph::depth_type & depth)
+                         lyx::depth_type & depth)
 {
        static Change current_change;
        int unknown = 0;
@@ -535,7 +609,7 @@ bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
 bool Buffer::readFile(LyXLex & lex, string const & filename,
                      ParagraphList::iterator pit)
 {
-       Assert(!filename.empty());
+       BOOST_ASSERT(!filename.empty());
 
        if (!lex.isOK()) {
                Alert::error(_("Document could not be read"),
@@ -705,7 +779,7 @@ bool Buffer::save() const
 
 bool Buffer::writeFile(string const & fname) const
 {
-       if (read_only && (fname == fileName())) {
+       if (pimpl_->read_only && (fname == fileName())) {
                return false;
        }
 
@@ -983,7 +1057,7 @@ string const Buffer::asciiParagraph(Paragraph const & par,
                }
        }
        buffer << word;
-       return STRCONV(buffer.str());
+       return buffer.str();
 }
 
 
@@ -1039,7 +1113,7 @@ void Buffer::makeLaTeXFile(ostream & os,
 
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
-       LaTeXFeatures features(params());
+       LaTeXFeatures features(*this, params());
        validate(features);
        lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
 
@@ -1178,7 +1252,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
 
        niceFile() = nice; // this will be used by included files.
 
-       LaTeXFeatures features(params());
+       LaTeXFeatures features(*this, params());
 
        validate(features);
 
@@ -1192,7 +1266,7 @@ void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
                ofs << "<!doctype linuxdoc system";
 
                string preamble = params().preamble;
-               string const name = nice ? ChangeExtension(filename_, ".sgml")
+               string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
                         : fname;
                preamble += features.getIncludedFiles(name);
                preamble += features.getLyXSGMLEntities();
@@ -1409,7 +1483,7 @@ void reset(PAR_TAG & p1, PAR_TAG const & p2)
 // Handle internal paragraph parsing -- layout already processed.
 void Buffer::simpleLinuxDocOnePar(ostream & os,
        ParagraphList::iterator par,
-       Paragraph::depth_type /*depth*/) const
+       lyx::depth_type /*depth*/) const
 {
        LyXLayout_ptr const & style = par->layout();
 
@@ -1608,7 +1682,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
        niceFile() = nice; // this will be used by Insetincludes.
 
-       LaTeXFeatures features(params());
+       LaTeXFeatures features(*this, params());
        validate(features);
 
        texrow().reset();
@@ -1621,7 +1695,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                    << "  PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
 
                string preamble = params().preamble;
-               string const name = nice ? ChangeExtension(filename_, ".sgml")
+               string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
                         : fname;
                preamble += features.getIncludedFiles(name);
                preamble += features.getLyXSGMLEntities();
@@ -1879,7 +1953,7 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
 
 void Buffer::simpleDocBookOnePar(ostream & os,
                                 ParagraphList::iterator par, int & desc_on,
-                                Paragraph::depth_type depth) const
+                                lyx::depth_type depth) const
 {
        bool emph_flag = false;
 
@@ -2026,11 +2100,11 @@ void Buffer::validate(LaTeXFeatures & features) const
        // the bullet shapes are buffer level not paragraph level
        // so they are tested here
        for (int i = 0; i < 4; ++i) {
-               if (params().user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
-                       int const font = params().user_defined_bullets[i].getFont();
+               if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
+                       int const font = params().user_defined_bullet(i).getFont();
                        if (font == 0) {
                                int const c = params()
-                                       .user_defined_bullets[i]
+                                       .user_defined_bullet(i)
                                        .getCharacter();
                                if (c == 16
                                   || c == 17
@@ -2068,7 +2142,7 @@ void Buffer::getLabelList(std::vector<string> & list) const
 
        for (inset_iterator it = inset_const_iterator_begin();
             it != inset_const_iterator_end(); ++it) {
-               it->getLabelList(list);
+               it->getLabelList(*this, list);
        }
 }
 
@@ -2089,14 +2163,19 @@ void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) con
 
        for (inset_iterator it = inset_const_iterator_begin();
                it != inset_const_iterator_end(); ++it) {
-               if (it->lyxCode() == InsetOld::BIBTEX_CODE)
-                       static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
-               else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
-                       static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
-               else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
-                       InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
-                       string const key = bib.getContents();
-                       string const opt = bib.getOptions();
+               if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
+                       InsetBibtex const & inset =
+                               dynamic_cast<InsetBibtex const &>(*it);
+                       inset.fillWithBibKeys(*this, keys);
+               } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
+                       InsetInclude const & inset =
+                               dynamic_cast<InsetInclude const &>(*it);
+                       inset.fillWithBibKeys(*this, keys);
+               } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
+                       InsetBibitem const & inset =
+                               dynamic_cast<InsetBibitem const &>(*it);
+                       string const key = inset.getContents();
+                       string const opt = inset.getOptions();
                        string const ref; // = pit->asString(this, false);
                        string const info = opt + "TheBibliographyRef" + ref;
                        keys.push_back(pair<string, string>(key, info));
@@ -2107,8 +2186,8 @@ void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) con
 
 bool Buffer::isDepClean(string const & name) const
 {
-       DepClean::const_iterator it = dep_clean_.find(name);
-       if (it == dep_clean_.end())
+       DepClean::const_iterator it = pimpl_->dep_clean.find(name);
+       if (it == pimpl_->dep_clean.end())
                return true;
        return it->second;
 }
@@ -2116,28 +2195,23 @@ bool Buffer::isDepClean(string const & name) const
 
 void Buffer::markDepClean(string const & name)
 {
-       dep_clean_[name] = true;
+       pimpl_->dep_clean[name] = true;
 }
 
 
 bool Buffer::dispatch(string const & command, bool * result)
 {
-       // Split command string into command and argument
-       string cmd;
-       string line = ltrim(command);
-       string const arg = trim(split(line, cmd, ' '));
-
-       return dispatch(lyxaction.LookupFunc(cmd), arg, result);
+       return dispatch(lyxaction.lookupFunc(command), result);
 }
 
 
-bool Buffer::dispatch(int action, string const & argument, bool * result)
+bool Buffer::dispatch(FuncRequest const & func, bool * result)
 {
        bool dispatched = true;
 
-       switch (action) {
+       switch (func.action) {
                case LFUN_EXPORT: {
-                       bool const tmp = Exporter::Export(this, argument, false);
+                       bool const tmp = Exporter::Export(this, func.argument, false);
                        if (result)
                                *result = tmp;
                        break;
@@ -2165,7 +2239,7 @@ void Buffer::changeLanguage(Language const * from, Language const * to)
 
 void Buffer::updateDocLang(Language const * nlang)
 {
-       messages_.reset(new Messages(nlang->code()));
+       pimpl_->messages.reset(new Messages(nlang->code()));
 }
 
 
@@ -2280,8 +2354,8 @@ Language const * Buffer::getLanguage() const
 
 string const Buffer::B_(string const & l10n) const
 {
-       if (messages_.get()) {
-               return messages_->get(l10n);
+       if (pimpl_->messages.get()) {
+               return pimpl_->messages->get(l10n);
        }
 
        return _(l10n);
@@ -2290,56 +2364,56 @@ string const Buffer::B_(string const & l10n) const
 
 bool Buffer::isClean() const
 {
-       return lyx_clean;
+       return pimpl_->lyx_clean;
 }
 
 
 bool Buffer::isBakClean() const
 {
-       return bak_clean;
+       return pimpl_->bak_clean;
 }
 
 
 void Buffer::markClean() const
 {
-       if (!lyx_clean) {
-               lyx_clean = true;
+       if (!pimpl_->lyx_clean) {
+               pimpl_->lyx_clean = true;
                updateTitles();
        }
        // if the .lyx file has been saved, we don't need an
        // autosave
-       bak_clean = true;
+       pimpl_->bak_clean = true;
 }
 
 
 void Buffer::markBakClean()
 {
-       bak_clean = true;
+       pimpl_->bak_clean = true;
 }
 
 
 void Buffer::setUnnamed(bool flag)
 {
-       unnamed = flag;
+       pimpl_->unnamed = flag;
 }
 
 
 bool Buffer::isUnnamed()
 {
-       return unnamed;
+       return pimpl_->unnamed;
 }
 
 
 void Buffer::markDirty()
 {
-       if (lyx_clean) {
-               lyx_clean = false;
+       if (pimpl_->lyx_clean) {
+               pimpl_->lyx_clean = false;
                updateTitles();
        }
-       bak_clean = false;
+       pimpl_->bak_clean = false;
 
-       DepClean::iterator it = dep_clean_.begin();
-       DepClean::const_iterator const end = dep_clean_.end();
+       DepClean::iterator it = pimpl_->dep_clean.begin();
+       DepClean::const_iterator const end = pimpl_->dep_clean.end();
 
        for (; it != end; ++it) {
                it->second = false;
@@ -2349,19 +2423,19 @@ void Buffer::markDirty()
 
 string const & Buffer::fileName() const
 {
-       return filename_;
+       return pimpl_->filename;
 }
 
 
 string const & Buffer::filePath() const
 {
-       return filepath_;
+       return pimpl_->filepath;
 }
 
 
 bool Buffer::isReadonly() const
 {
-       return read_only;
+       return pimpl_->read_only;
 }