]> git.lyx.org Git - lyx.git/blobdiff - src/EmbeddedFiles.cpp
Allow using \binom without amsmath and add support for \brace and \brack
[lyx.git] / src / EmbeddedFiles.cpp
index e5d7ab35c1620f6d1771e6f2420172bc647f13dd..666cb7acf26bcec6b40947f06d26538e388e7996 100644 (file)
@@ -71,7 +71,7 @@ void EmbeddedFile::setInzipName(std::string const & name)
                return;
 
        // an enabled EmbeededFile should have this problem handled
-       BOOST_ASSERT(!enabled());
+       BOOST_ASSERT(!isEnabled());
        // file will be synced when it is enabled
        inzip_name_ = name;
 }
@@ -79,14 +79,14 @@ void EmbeddedFile::setInzipName(std::string const & name)
 
 string EmbeddedFile::embeddedFile() const
 {
-       BOOST_ASSERT(enabled());
+       BOOST_ASSERT(isEnabled());
        return temp_path_ + inzip_name_;
 }
 
 
 FileName EmbeddedFile::availableFile() const
 {
-       if (enabled() && embedded())
+       if (isEnabled() && embedded())
                return FileName(embeddedFile());
        else
                return *this;
@@ -95,7 +95,7 @@ FileName EmbeddedFile::availableFile() const
 
 string EmbeddedFile::latexFilename(std::string const & buffer_path) const
 {
-       return (enabled() && embedded()) ? inzip_name_ : relFilename(buffer_path);
+       return (isEnabled() && embedded()) ? inzip_name_ : relFilename(buffer_path);
 }
 
 
@@ -112,7 +112,7 @@ void EmbeddedFile::setEmbed(bool embed)
 }
 
 
-void EmbeddedFile::enable(bool flag, Buffer const * buf, bool updateFile)
+void EmbeddedFile::enable(bool enabled, Buffer const & buf, bool updateFile)
 {
        // This function will be called when
        // 1. through EmbeddedFiles::enable() when a file is read. Files
@@ -121,12 +121,12 @@ void EmbeddedFile::enable(bool flag, Buffer const * buf, bool updateFile)
        // 2. through menu item enable/disable. updateFile should be true.
        // 3. A single embedded file is added or modified. updateFile
        //    can be true or false.
-       LYXERR(Debug::FILES, (flag ? "Enable" : "Disable") 
+       LYXERR(Debug::FILES, (enabled ? "Enable" : "Disable") 
                << " " << absFilename() 
                << (updateFile ? " (update file)." : " (no update)."));
 
-       if (flag) {
-               temp_path_ = buf->temppath();
+       if (enabled) {
+               temp_path_ = buf.temppath();
                if (!suffixIs(temp_path_, '/'))
                        temp_path_ += '/';
                if (embedded() && updateFile)
@@ -134,7 +134,7 @@ void EmbeddedFile::enable(bool flag, Buffer const * buf, bool updateFile)
        } else {
                // when a new embeddeed file is created, it is not enabled, and 
                // there is no need to extract.
-               if (enabled() && embedded() && updateFile)
+               if (isEnabled() && embedded() && updateFile)
                        extract();
                temp_path_ = "";
        }
@@ -143,7 +143,7 @@ void EmbeddedFile::enable(bool flag, Buffer const * buf, bool updateFile)
 
 bool EmbeddedFile::extract() const
 {
-       BOOST_ASSERT(enabled());
+       BOOST_ASSERT(isEnabled());
 
        string ext_file = absFilename();
        string emb_file = embeddedFile();
@@ -168,7 +168,7 @@ bool EmbeddedFile::extract() const
                // otherwise, ask if overwrite
                int ret = Alert::prompt(
                        _("Overwrite external file?"),
-                       bformat(_("External file %1$s already exists, do you want to overwrite it"),
+                       bformat(_("External file %1$s already exists, do you want to overwrite it?"),
                                from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
                if (ret != 0)
                        // if the user does not want to overwrite, we still consider it
@@ -202,7 +202,7 @@ bool EmbeddedFile::extract() const
 
 bool EmbeddedFile::updateFromExternalFile() const
 {
-       BOOST_ASSERT(enabled());
+       BOOST_ASSERT(isEnabled());
 
        string ext_file = absFilename();
        string emb_file = embeddedFile();
@@ -254,11 +254,11 @@ bool EmbeddedFile::updateFromExternalFile() const
 }
 
 
-EmbeddedFile EmbeddedFile::copyTo(Buffer const * buf)
+EmbeddedFile EmbeddedFile::copyTo(Buffer const & buf)
 {
-       EmbeddedFile file = EmbeddedFile(absFilename(), buf->filePath());
+       EmbeddedFile file = EmbeddedFile(absFilename(), buf.filePath());
        file.setEmbed(embedded());
-       file.enable(buf->embedded(), buf, false);
+       file.enable(buf.embedded(), buf, false);
        
        // use external file.
        if (!embedded())
@@ -392,7 +392,7 @@ std::string EmbeddedFile::calcInzipName(std::string const & buffer_path)
 
 void EmbeddedFile::syncInzipFile(std::string const & buffer_path)
 {
-       BOOST_ASSERT(enabled());
+       BOOST_ASSERT(isEnabled());
        string old_emb_file = temp_path_ + '/' + inzip_name_;
        FileName old_emb(old_emb_file);
 
@@ -448,7 +448,7 @@ bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
 }
 
 
-void EmbeddedFileList::enable(bool flag, Buffer & buffer, bool updateFile)
+void EmbeddedFileList::enable(bool enabled, Buffer & buffer, bool updateFile)
 {
        // update embedded file list
        update(buffer);
@@ -459,14 +459,14 @@ void EmbeddedFileList::enable(bool flag, Buffer & buffer, bool updateFile)
        iterator it_end = end();
        // an exception may be thrown
        for (; it != it_end; ++it) {
-               it->enable(flag, &buffer, updateFile);
+               it->enable(enabled, buffer, updateFile);
                if (it->embedded())
                        ++count_embedded;
                else
                        ++count_external;
        }
        // if operation is successful (no exception is thrown)
-       buffer.params().embedded = flag;
+       buffer.params().embedded = enabled;
 
        // if the operation is successful, update insets
        for (it = begin(); it != it_end; ++it)
@@ -476,7 +476,7 @@ void EmbeddedFileList::enable(bool flag, Buffer & buffer, bool updateFile)
                return;
 
        // show result
-       if (flag) {
+       if (enabled) {
                docstring const msg = bformat(_("%1$d external files are ignored.\n"
                        "%2$d embeddable files are embedded.\n"), count_external, count_embedded);
                Alert::information(_("Packing all files"), msg);
@@ -491,29 +491,52 @@ void EmbeddedFileList::enable(bool flag, Buffer & buffer, bool updateFile)
 void EmbeddedFileList::registerFile(EmbeddedFile const & file,
        Inset const * inset, Buffer const & buffer)
 {
-       BOOST_ASSERT(!buffer.embedded() || file.enabled());
+       BOOST_ASSERT(!buffer.embedded() || file.isEnabled());
+
+       string newfile = file.absFilename();
+       iterator efp = findFile(newfile);
+       if (efp != end()) {
+               if (efp->embedded() != file.embedded()) {
+                       Alert::error(_("Wrong embedding status."),
+                               bformat(_("File %1$s is included in more than one insets, "
+                                       "but with different embedding status. Assuming embedding status."),
+                                       from_utf8(efp->outputFilename())));
+                       efp->setEmbed(true);
+                       // update the inset with this embedding status.
+                       const_cast<Inset*>(inset)->updateEmbeddedFile(*efp);
+               }
+               efp->addInset(inset);
+               return;
+       }
+       file.clearInsets();
+       push_back(file);
+       back().addInset(inset);
+}
+
+
+EmbeddedFileList::const_iterator 
+       EmbeddedFileList::findFile(std::string const & filename) const
+{
+       // try to find this file from the list
+       std::vector<EmbeddedFile>::const_iterator it = begin();
+       std::vector<EmbeddedFile>::const_iterator it_end = end();
+       for (; it != it_end; ++it)
+               if (it->absFilename() == filename)
+                       return it;
+       return end();
+}
 
+
+EmbeddedFileList::iterator 
+       EmbeddedFileList::findFile(std::string const & filename)
+{
        // try to find this file from the list
        std::vector<EmbeddedFile>::iterator it = begin();
        std::vector<EmbeddedFile>::iterator it_end = end();
        for (; it != it_end; ++it)
-               if (it->absFilename() == file.absFilename()) {
-                       if (it->embedded() != file.embedded()) {
-                               Alert::error(_("Wrong embedding status."),
-                                       bformat(_("File %1$s is included in more than one insets, "
-                                               "but with different embedding status. Assuming embedding status."),
-                                               from_utf8(it->outputFilename())));
-                               it->setEmbed(true);
-                               // update the inset with this embedding status.
-                               const_cast<Inset*>(inset)->updateEmbeddedFile(*it);
-                       }
-                       it->addInset(inset);
-                       return;
-               }
-       //
-       file.clearInsets();
-       push_back(file);
-       back().addInset(inset);
+               if (it->absFilename() == filename)
+                       return it;
+       return end();
 }
 
 
@@ -547,7 +570,7 @@ void EmbeddedFileList::validate(Buffer const & buffer)
        for (; e_it != e_end; ++e_it) {
                EmbeddedFile file = EmbeddedFile(*e_it, buffer.filePath());
                // do not update from external file
-               file.enable(true, &buffer, false);
+               file.enable(true, buffer, false);
                // but we do need to check file existence.
                if (!FileName(file.embeddedFile()).exists())
                        throw ExceptionMessage(ErrorException, _("Failed to open file"),
@@ -571,7 +594,7 @@ void EmbeddedFileList::update(Buffer const & buffer)
        for (; it != it_end; ++it) {
                EmbeddedFile file = EmbeddedFile(*it, buffer.filePath());
                file.setEmbed(true);
-               file.enable(buffer.embedded(), &buffer, false);
+               file.enable(buffer.embedded(), buffer, false);
                insert(end(), file);
        }
 }