]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
* In the process of fixing the math background color bug, this commit transfer backgr...
[lyx.git] / src / insets / insetinclude.C
index 859aba0cb3b5d64dacb993600a2562b342dbe7a6..213cb3ad63077f1715306de8678f06299e3db5c8 100644 (file)
@@ -217,7 +217,7 @@ string const parentFilename(Buffer const & buffer)
 }
 
 
-string const includedFilename(Buffer const & buffer,
+FileName const includedFilename(Buffer const & buffer,
                              InsetCommandParams const & params)
 {
        return makeAbsPath(to_utf8(params["filename"]),
@@ -271,7 +271,7 @@ void InsetInclude::read(Buffer const &, LyXLex & lex)
 void InsetInclude::read(LyXLex & lex)
 {
        if (lex.isOK()) {
-               lex.next();
+               lex.eatLine();
                string const command = lex.getString();
                params_.scanCommand(command);
        }
@@ -318,8 +318,7 @@ docstring const InsetInclude::getScreenLabel(Buffer const &) const
        if (params_["filename"].empty())
                temp += "???";
        else
-               // FIXME: We don't know the encoding of the filename
-               temp += from_ascii(onlyFilename(to_utf8(params_["filename"])));
+               temp += from_utf8(onlyFilename(to_utf8(params_["filename"])));
 
        return temp;
 }
@@ -333,7 +332,7 @@ Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params
        if (isVerbatim(params))
                return 0;
 
-       string const included_file = includedFilename(buffer, params);
+       string const included_file = includedFilename(buffer, params).absFilename();
        if (!isLyXFilename(included_file))
                return 0;
 
@@ -347,18 +346,17 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
        if (isVerbatim(params))
                return false;
 
-       string const included_file = includedFilename(buffer, params);
-       if (!isLyXFilename(included_file))
+       FileName const included_file = includedFilename(buffer, params);
+       if (!isLyXFilename(included_file.absFilename()))
                return false;
 
-       Buffer * buf = theBufferList().getBuffer(included_file);
+       Buffer * buf = theBufferList().getBuffer(included_file.absFilename());
        if (!buf) {
                // the readonly flag can/will be wrong, not anymore I think.
-               FileName const fullname(included_file);
-               if (!fs::exists(fullname.toFilesystemEncoding()))
+               if (!fs::exists(included_file.toFilesystemEncoding()))
                        return false;
-               buf = theBufferList().newBuffer(included_file);
-               if (!loadLyXFile(buf, fullname))
+               buf = theBufferList().newBuffer(included_file.absFilename());
+               if (!loadLyXFile(buf, included_file))
                        return false;
        }
        if (buf)
@@ -371,7 +369,7 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
 
 
 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
-                       OutputParams const & runparams) const
+                        OutputParams const & runparams) const
 {
        string incfile(to_utf8(params_["filename"]));
 
@@ -385,8 +383,9 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
        // if incfile is relative, make it relative to the master
        // buffer directory.
        if (!absolutePath(incfile)) {
-               incfile = makeRelPath(included_file.absFilename(),
-                                     m_buffer->filePath());
+               // FIXME UNICODE
+               incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
+                                             from_utf8(m_buffer->filePath())));
        }
 
        // write it to a file (so far the complete file)
@@ -397,9 +396,9 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
 
        if (!runparams.nice)
                incfile = mangled;
-       lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
-       lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
-       lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+       LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
+       LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
+       LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
 
        if (runparams.inComment || runparams.dryrun)
                // Don't try to load or copy the file
@@ -428,9 +427,13 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
 // argument. Should we set it to string(), or should makeLaTeXFile
 // make use of it somehow? (JMarc 20031002)
 #endif
+               // The included file might be written in a different encoding
+               Encoding const * const oldEnc = runparams.encoding;
+               runparams.encoding = &tmp->params().encoding();
                tmp->makeLaTeXFile(writefile,
                                   onlyPath(masterFilename(buffer)),
                                   runparams, false);
+               runparams.encoding = oldEnc;
        } else {
                // Copy the file to the temp dir, so that .aux files etc.
                // are not created in the original dir. Files included by
@@ -441,7 +444,7 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                if (checksum_in != checksum_out) {
                        if (!copy(included_file, writefile)) {
                                // FIXME UNICODE
-                               lyxerr[Debug::LATEX]
+                               LYXERR(Debug::LATEX)
                                        << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
                                                                  "into the temporary directory."),
                                                   from_utf8(included_file.absFilename())))
@@ -493,22 +496,26 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
 
 
 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
-                       OutputParams const &) const
+                            OutputParams const &) const
 {
        if (isVerbatim(params_)) {
+               os << '[' << getScreenLabel(buffer) << '\n';
                // FIXME: We don't know the encoding of the file
-               docstring const str = from_utf8(
-                       getFileContents(FileName(includedFilename(buffer, params_))));
+               docstring const str =
+                    from_utf8(getFileContents(includedFilename(buffer, params_)));
                os << str;
-               // Return how many newlines we issued.
-               return int(lyx::count(str.begin(), str.end(), '\n'));
+               os << "\n]";
+               return PLAINTEXT_NEWLINE + 1; // one char on a separate line
+       } else {
+               docstring const str = '[' + getScreenLabel(buffer) + ']';
+               os << str;
+               return str.size();
        }
-       return 0;
 }
 
 
 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
-                         OutputParams const & runparams) const
+                          OutputParams const & runparams) const
 {
        string incfile = to_utf8(params_["filename"]);
 
@@ -516,7 +523,7 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
        if (incfile.empty())
                return 0;
 
-       string const included_file = includedFilename(buffer, params_);
+       string const included_file = includedFilename(buffer, params_).absFilename();
 
        // write it to a file (so far the complete file)
        string const exportfile = changeExtension(incfile, ".sgml");
@@ -531,9 +538,9 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
                if (!runparams.nice)
                        incfile = mangled;
 
-               lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
-               lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
-               lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+               LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
+               LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
+               LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
 
                tmp->makeDocBookFile(writefile, runparams, true);
        }
@@ -561,7 +568,7 @@ void InsetInclude::validate(LaTeXFeatures & features) const
 
        Buffer const & buffer = features.buffer();
 
-       string const included_file = includedFilename(buffer, params_);
+       string const included_file = includedFilename(buffer, params_).absFilename();
 
        if (isLyXFilename(included_file))
                writefile = changeExtension(included_file, ".sgml");
@@ -571,7 +578,7 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        if (!features.runparams().nice && !isVerbatim(params_)) {
                incfile = DocFileName(writefile).mangledFilename();
                writefile = makeAbsPath(incfile,
-                                       buffer.getMasterBuffer()->temppath());
+                                       buffer.getMasterBuffer()->temppath()).absFilename();
        }
 
        features.includeFile(include_label, writefile);
@@ -601,7 +608,7 @@ void InsetInclude::getLabelList(Buffer const & buffer,
                                std::vector<docstring> & list) const
 {
        if (loadIfNeeded(buffer, params_)) {
-               string const included_file = includedFilename(buffer, params_);
+               string const included_file = includedFilename(buffer, params_).absFilename();
                Buffer * tmp = theBufferList().getBuffer(included_file);
                tmp->setParentName("");
                tmp->getLabelList(list);
@@ -614,7 +621,7 @@ void InsetInclude::fillWithBibKeys(Buffer const & buffer,
                std::vector<std::pair<string, docstring> > & keys) const
 {
        if (loadIfNeeded(buffer, params_)) {
-               string const included_file = includedFilename(buffer, params_);
+               string const included_file = includedFilename(buffer, params_).absFilename();
                Buffer * tmp = theBufferList().getBuffer(included_file);
                tmp->setParentName("");
                tmp->fillWithBibKeys(keys);
@@ -727,17 +734,19 @@ namespace {
 
 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
 {
-       string const included_file = includedFilename(buffer, params);
+       FileName const included_file = includedFilename(buffer, params);
 
        return type(params) == INPUT && params.preview() &&
-               isFileReadable(FileName(included_file));
+               isFileReadable(included_file);
 }
 
 
 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
 {
        odocstringstream os;
-       OutputParams runparams;
+       // We don't need to set runparams.encoding since this will be done
+       // by latex() anyway.
+       OutputParams runparams(0);
        runparams.flavor = OutputParams::LATEX;
        inset.latex(buffer, os, runparams);
 
@@ -751,7 +760,7 @@ void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
        InsetCommandParams const & params = inset.params();
        if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
            preview_wanted(params, buffer)) {
-               renderer.setAbsFile(FileName(includedFilename(buffer, params)));
+               renderer.setAbsFile(includedFilename(buffer, params));
                docstring const snippet = latex_string(inset, buffer);
                renderer.addPreview(snippet, buffer);
        }
@@ -764,7 +773,7 @@ void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
 {
        Buffer const & buffer = ploader.buffer();
        if (preview_wanted(params(), buffer)) {
-               preview_->setAbsFile(FileName(includedFilename(buffer, params())));
+               preview_->setAbsFile(includedFilename(buffer, params()));
                docstring const snippet = latex_string(*this, buffer);
                preview_->addPreview(snippet, ploader);
        }