]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
Some more changes for updating text-insets.
[lyx.git] / src / insets / insetinclude.C
index efd90e492d8f065e50a87dffe33cc76b75049245..440190bb5449d104c2d06d1b9b9d69c7a666eb76 100644 (file)
 #include "include_form.h"
 #include "support/FileInfo.h"
 #include "layout.h"
+#include "lyxfunc.h"
+
+using std::ostream;
+using std::endl;
+using std::vector;
+using std::pair;
 
 extern BufferView * current_view;
 
-extern LyXRC * lyxrc;
 extern BufferList bufferlist;
-extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
 
 
 FD_include * create_form_include(void)
@@ -108,7 +112,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                 else
                         ext = "*.lyx";
                // launches dialog
-               fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
+               fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
     
                // Use by default the master's path
                string filename = fileDlg.Select(_("Select Child Document"),
@@ -145,7 +149,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                        }
                        
                        fl_hide_form(form->include);
-                       UpdateInset(current_view, inset);
+                       current_view->updateInset(inset, true);
                        break;
                } // fall through
                
@@ -166,7 +170,7 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
                        }
                        
                        fl_hide_form(form->include);
-                       UpdateInset(current_view, inset);
+                       current_view->updateInset(inset, true);
                        current_view->owner()->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, inset->getContents().c_str());
                 }
                 break;
@@ -184,6 +188,22 @@ extern "C" void include_cb(FL_OBJECT *, long arg)
 }
 
 
+static string unique_id() {
+       static unsigned int seed=1000;
+
+#ifdef HAVE_SSTREAM
+       std::ostringstream ost;
+       ost << "file" << ++seed;
+#else
+       char ctmp[16];
+       ostrstream ost(ctmp,16);
+       ost << "file" << ++seed << '\0';
+#endif
+
+       return ost.str();
+}
+
+
 InsetInclude::InsetInclude(string const & fname, Buffer * bf)
        : InsetCommand("include") 
 {
@@ -191,6 +211,7 @@ InsetInclude::InsetInclude(string const & fname, Buffer * bf)
        setContents(fname);
        flag = InsetInclude::INCLUDE;
        noload = false;
+       include_label = unique_id();
 }
 
 
@@ -213,7 +234,7 @@ InsetInclude::~InsetInclude()
 
 Inset * InsetInclude::Clone() const
 { 
-       InsetInclude * ii = new InsetInclude (contents, master); 
+       InsetInclude * ii = new InsetInclude (getContents(), master); 
        ii->setNoLoad(isNoLoad());
        // By default, the newly created inset is of `include' type,
        // so we do not test this case.
@@ -227,7 +248,7 @@ Inset * InsetInclude::Clone() const
 }
 
 
-void InsetInclude::Edit(BufferView * bv, int, int)
+void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
 {
        if(bv->buffer()->isReadonly())
                WarnReadonly(bv->buffer()->fileName());
@@ -238,7 +259,7 @@ void InsetInclude::Edit(BufferView * bv, int, int)
        }
         form->include->u_vdata = this;
     
-        fl_set_input(form->input, contents.c_str());
+        fl_set_input(form->input, getContents().c_str());
        fl_set_button(form->flag1, int(isNoLoad()));
        fl_set_button(form->flag2, int(isInput()));
        fl_set_button(form->flag3, int(isInclude()));
@@ -260,15 +281,15 @@ void InsetInclude::Edit(BufferView * bv, int, int)
 }
 
 
-void InsetInclude::Write(ostream & os) const
+void InsetInclude::Write(Buffer const *, ostream & os) const
 {
        os << "Include " << getCommand() << "\n";
 }
 
 
-void InsetInclude::Read(LyXLex & lex)
+void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
 {
-       InsetCommand::Read(lex);
+       InsetCommand::Read(buf, lex);
     
        if (getCmdName() == "include")
                setInclude();
@@ -282,6 +303,12 @@ void InsetInclude::Read(LyXLex & lex)
 }
 
 
+bool InsetInclude::display() const 
+{
+       return !isInput();
+}
+
+
 string InsetInclude::getScreenLabel() const
 {
        string temp;
@@ -293,15 +320,23 @@ string InsetInclude::getScreenLabel() const
        } else temp += _("Include");
        temp += ": ";
        
-       if (contents.empty()) {
+       if (getContents().empty()) {
                temp+= "???";
        } else {
-               temp+= contents;
+               temp+= getContents();
        }
        return temp;
 }
 
 
+void InsetInclude::setContents(string const & c)
+{
+       InsetCommand::setContents(c);
+       filename = MakeAbsPath(getContents(), 
+                              OnlyPath(getMasterFilename())); 
+}
+
+
 bool InsetInclude::loadIfNeeded() const
 {
        if (isNoLoad() || isVerb()) return false;
@@ -316,26 +351,16 @@ bool InsetInclude::loadIfNeeded() const
 }
 
 
-int InsetInclude::Latex(ostream & os, signed char /*fragile*/) const
+int InsetInclude::Latex(Buffer const *, ostream & os,
+                       bool /*fragile*/, bool /*fs*/) const
 {
-       string include_file;
-       signed char dummy = 0;
-       Latex(include_file, dummy);
-       os << include_file;
-       return 0;
-}
-
-
-int InsetInclude::Latex(string & file, signed char /*fragile*/) const
-{
-       string writefile, incfile;
-
        // Do nothing if no file name has been specified
-       if (contents.empty())
+       if (getContents().empty())
                return 0;
     
        // Use += to force a copy of contents (JMarc)
-       incfile += contents;
+       // How does that force anything? (Lgb)
+       string incfile(getContents());
 
        if (loadIfNeeded()) {
                Buffer * tmp = bufferlist.getBuffer(getFileName());
@@ -352,7 +377,7 @@ int InsetInclude::Latex(string & file, signed char /*fragile*/) const
                }
                
                // write it to a file (so far the complete file)
-               writefile = ChangeExtension(getFileName(), ".tex", false);
+               string writefile = ChangeExtension(getFileName(), ".tex");
                if (!master->tmppath.empty()
                    && !master->niceFile) {
                        incfile = subst(incfile, '/','@');
@@ -362,7 +387,7 @@ int InsetInclude::Latex(string & file, signed char /*fragile*/) const
                        writefile = AddName(master->tmppath, incfile);
                } else
                        writefile = getFileName();
-               writefile = ChangeExtension(writefile, ".tex", false);
+               writefile = ChangeExtension(writefile, ".tex");
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
                
@@ -374,37 +399,119 @@ int InsetInclude::Latex(string & file, signed char /*fragile*/) const
        } 
 
        if (isVerb()) {
-               file += '\\';
-               file += command + '{';
-               file += incfile + '}';
-       } 
-       else if (isInput()) {
+               os << '\\' << getCmdName() << '{' << incfile << '}';
+       } else if (isInput()) {
                // \input wants file with extension (default is .tex)
                if (!IsLyXFilename(getFileName())) {
-                       file += '\\';
-                       file += command + '{';
-                       file += incfile + '}';
+                       os << '\\' << getCmdName() << '{' << incfile << '}';
                } else {
-                       file += '\\';
-                       file += command + '{';
-                       file += ChangeExtension(incfile, ".tex", false)
-                               + '}';
+                       os << '\\' << getCmdName() << '{'
+                          << ChangeExtension(incfile, ".tex")
+                          <<  '}';
                }
        } else {
                // \include don't want extension and demands that the
                // file really have .tex
-               file += '\\';
-               file += command + '{';
-               file +=         ChangeExtension(incfile, string(), false)
-                       + '}';
+               os << '\\' << getCmdName() << '{'
+                  << ChangeExtension(incfile, string())
+                  << '}';
        }
 
        return 0;
 }
 
 
+int InsetInclude::Linuxdoc(Buffer const *, ostream & os) const
+{
+       // Do nothing if no file name has been specified
+       if (getContents().empty())
+               return 0;
+    
+       string incfile(getContents());
+
+       if (loadIfNeeded()) {
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
+
+               // write it to a file (so far the complete file)
+               string writefile = ChangeExtension(getFileName(), ".sgml");
+               if (!master->tmppath.empty() && !master->niceFile) {
+                       incfile = subst(incfile, '/','@');
+                       writefile = AddName(master->tmppath, incfile);
+               } else
+                       writefile = getFileName();
+
+               if(IsLyXFilename(getFileName()))
+                       writefile = ChangeExtension(writefile, ".sgml");
+
+               lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+               
+               tmp->makeLinuxDocFile(writefile, master->niceFile, true);
+       } 
+
+       if (isVerb()) {
+               os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
+       } else 
+               os << '&' << include_label << ';';
+       
+       return 0;
+}
+
+
+int InsetInclude::DocBook(Buffer const *, ostream & os) const
+{
+       // Do nothing if no file name has been specified
+       if (getContents().empty())
+               return 0;
+    
+       string incfile(getContents());
+
+       if (loadIfNeeded()) {
+               Buffer * tmp = bufferlist.getBuffer(getFileName());
+
+               // write it to a file (so far the complete file)
+               string writefile = ChangeExtension(getFileName(), ".sgml");
+               if (!master->tmppath.empty() && !master->niceFile) {
+                       incfile = subst(incfile, '/','@');
+                       writefile = AddName(master->tmppath, incfile);
+               } else
+                       writefile = getFileName();
+               if(IsLyXFilename(getFileName()))
+                       writefile = ChangeExtension(writefile, ".sgml");
+
+               lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
+               
+               tmp->makeDocBookFile(writefile, master->niceFile, true);
+       } 
+
+       if (isVerb()) {
+               os << "<!-- includefile verbatim=\"" << incfile << "\" -->";
+       } else 
+               os << '&' << include_label << ';';
+       
+       return 0;
+}
+
+
 void InsetInclude::Validate(LaTeXFeatures & features) const
 {
+
+       string incfile(getContents());
+       string writefile = ChangeExtension(getFileName(), ".sgml");
+       if (!master->tmppath.empty() && !master->niceFile) {
+               incfile = subst(incfile, '/','@');
+               writefile = AddName(master->tmppath, incfile);
+       } else
+               // writefile = getFileName();
+               // Use the relative path.
+               writefile = incfile;
+
+       if(IsLyXFilename(getFileName()))
+               writefile = ChangeExtension(writefile, ".sgml");
+
+       features.IncludedFiles[include_label] = writefile;
+
        if (isVerb())
                features.verbatim = true;
 
@@ -419,49 +526,32 @@ void InsetInclude::Validate(LaTeXFeatures & features) const
 }
 
 
-string InsetInclude::getLabel(int) const
+vector<string> InsetInclude::getLabelList() const
 {
-    string label;
+    vector<string> l;
     string parentname;
-       
-       
+
     if (loadIfNeeded()) {
        Buffer * tmp = bufferlist.getBuffer(getFileName());
        tmp->setParentName(""); 
-       label =  tmp->getReferenceList('\n');
+       l = tmp->getLabelList();
        tmp->setParentName(getMasterFilename());
     }
 
-    return label;
-}
-
-
-int InsetInclude::GetNumberOfLabels() const
-{
-    string label;
-
-    if (loadIfNeeded()) {
-       Buffer * tmp = bufferlist.getBuffer(getFileName());
-       tmp->setParentName("");    
-       label = tmp->getReferenceList('\n');
-       tmp->setParentName(getMasterFilename());
-    }
-    int nl = (label.empty())? 0: 1;
-       
-    return nl;
+    return l;
 }
 
 
-string InsetInclude::getKeys(char delim) const
+vector<pair<string,string> > InsetInclude::getKeys() const
 {
-       string lst;
+       vector<pair<string,string> > keys;
        
        if (loadIfNeeded()) {
                Buffer *tmp = bufferlist.getBuffer(getFileName());
                tmp->setParentName(""); 
-               lst =  tmp->getBibkeyList(delim);
+               keys =  tmp->getBibkeyList();
                tmp->setParentName(getMasterFilename());
        }
        
-       return lst;
+       return keys;
 }