]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Make logic clearer, with help from Lars.
[lyx.git] / src / insets / InsetInclude.cpp
index 1b7ddf81a3dcd42d2a168855f3495a29071bf618..e8eb7adefecf4eabff86b3bfb50b49d911638060 100644 (file)
@@ -107,7 +107,7 @@ bool isListings(InsetCommandParams const & params)
 InsetInclude::InsetInclude(InsetCommandParams const & p)
        : params_(p), include_label(uniqueID()),
          preview_(new RenderMonitoredPreview(this)),
-         set_label_(false)
+         set_label_(false), counter_(0)
 {
        preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
@@ -118,7 +118,7 @@ InsetInclude::InsetInclude(InsetInclude const & other)
          params_(other.params_),
          include_label(other.include_label),
          preview_(new RenderMonitoredPreview(this)),
-         set_label_(false)
+         set_label_(false), counter_(0)
 {
        preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
@@ -230,6 +230,13 @@ bool isVerbatim(InsetCommandParams const & params)
 }
 
 
+bool isInputOrInclude(InsetCommandParams const & params) 
+{
+       Types const t = type(params);
+       return (t == INPUT) || (t == INCLUDE);
+}
+
+
 string const masterFilename(Buffer const & buffer)
 {
        return buffer.getMasterBuffer()->fileName();
@@ -336,9 +343,13 @@ docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
                case INCLUDE:
                        temp += buf.B_("Include");
                        break;
-               case LISTINGS:
-                       temp += buf.B_("Program Listing");
+               case LISTINGS: {
+                       if (counter_ > 0)
+                               temp += buf.B_("Program Listing ") + convert<docstring>(counter_);
+                       else
+                               temp += buf.B_("Program Listing");
                        break;
+               }
        }
 
        temp += ": ";
@@ -369,7 +380,8 @@ Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params
        //FIXME RECURSIVE INCLUDES
        if (childBuffer == & buffer)
                return 0;
-       else return childBuffer;
+       else
+               return childBuffer;
 }
 
 
@@ -389,12 +401,14 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
                if (!fs::exists(included_file.toFilesystemEncoding()))
                        return false;
                buf = theBufferList().newBuffer(included_file.absFilename());
-               if (!loadLyXFile(buf, included_file))
+               if (!loadLyXFile(buf, included_file)) {
+                       //close the buffer we just opened
+                       theBufferList().close(buf, false);
                        return false;
+               }
        }
-       if (buf)
-               buf->setParentName(parentFilename(buffer));
-       return buf != 0;
+       buf->setParentName(parentFilename(buffer));
+       return true;
 }
 
 
@@ -416,7 +430,9 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
        //FIXME RECURSIVE INCLUDE
        //This isn't sufficient, as the inclusion could be downstream.
        //But it'll have to do for now.
-       if (buffer.fileName() == included_file.toFilesystemEncoding()) {
+       if (isInputOrInclude(params_) &&
+               buffer.fileName() == included_file.toFilesystemEncoding()) 
+       {
                Alert::error(_("Recursive input"), 
                               bformat(_("Attempted to include file %1$s in itself! "
                               "Ignoring inclusion."), from_utf8(incfile)));
@@ -435,8 +451,9 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
 
        // write it to a file (so far the complete file)
        string const exportfile = changeExtension(incfile, ".tex");
-       string const mangled = DocFileName(changeExtension(included_file.absFilename(),
-                                                       ".tex")).mangledFilename();
+       string const mangled = 
+               DocFileName(changeExtension(included_file.absFilename(),".tex")).
+                       mangledFilename();
        FileName const writefile(makeAbsPath(mangled, m_buffer->temppath()));
 
        if (!runparams.nice)
@@ -445,10 +462,16 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
        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
-               ;
-       else if (loadIfNeeded(buffer, params_)) {
+       if (runparams.inComment || runparams.dryrun) {
+               //Don't try to load or copy the file if we're
+               //in a comment or doing a dryrun
+       } else if (isInputOrInclude(params_) && 
+                isLyXFilename(included_file.absFilename())) {
+               //if it's a LyX file and we're inputting or including,
+               //try to load it so we can write the associated latex
+               if (!loadIfNeeded(buffer, params_))
+                       return false;
+                       
                Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
 
                if (tmp->params().textclass != m_buffer->params().textclass) {
@@ -480,9 +503,10 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                                   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
-               // this file will be found via input@path, see ../Buffer.cpp.
+               // In this case, it's not a LyX file, so we copy the file
+               // to the temp dir, so that .aux files etc. are not created 
+               // in the original dir. Files included by this file will be 
+               // found via input@path, see ../Buffer.cpp.
                unsigned long const checksum_in  = sum(included_file);
                unsigned long const checksum_out = sum(writefile);
 
@@ -529,7 +553,7 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                // opt is set in QInclude dialog and should have passed validation.
                InsetListingsParams params(opt);
                if (!params.params().empty())
-                       os << "[" << from_utf8(params.encodedString()) << "]";
+                       os << "[" << from_utf8(params.params()) << "]";
                os << '{'  << from_utf8(incfile) << '}';
        } else {
                runparams.exportdata->addExternalFile(tex_format, writefile,
@@ -882,6 +906,21 @@ void InsetInclude::updateLabels(Buffer const & buffer) const
 }
 
 
+void InsetInclude::updateCounter(Counters & counters)
+{
+       if (!isListings(params_))
+               return;
+
+       InsetListingsParams const par = params_.getOptions();
+       if (par.getParamValue("caption").empty())
+               counter_ = 0;
+       else {
+               counters.step(from_ascii("listing"));
+               counter_ = counters.value(from_ascii("listing"));
+       }
+}
+
+
 string const InsetIncludeMailer::name_("include");
 
 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)