1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * This file is Copyright 1996-2001
12 * ======================================================
18 #pragma implementation
22 #include "bufferlist.h"
24 #include "LyXAction.h"
27 #include "tex-strings.h"
29 #include "bufferview_funcs.h"
36 #include "LaTeXFeatures.h"
43 #include "converter.h"
44 #include "BufferView.h"
45 #include "ParagraphParameters.h"
46 #include "iterators.h"
47 #include "lyxtextclasslist.h"
49 #include "mathed/formulamacro.h"
50 #include "mathed/formula.h"
52 #include "insets/inset.h"
53 #include "insets/inseterror.h"
54 #include "insets/insetlabel.h"
55 #include "insets/insetref.h"
56 #include "insets/inseturl.h"
57 #include "insets/insetnote.h"
58 #include "insets/insetquotes.h"
59 #include "insets/insetlatexaccent.h"
60 #include "insets/insetbib.h"
61 #include "insets/insetcite.h"
62 #include "insets/insetexternal.h"
63 #include "insets/insetindex.h"
64 #include "insets/insetinclude.h"
65 #include "insets/insettoc.h"
66 #include "insets/insetparent.h"
67 #include "insets/insetspecialchar.h"
68 #include "insets/insettext.h"
69 #include "insets/insetert.h"
70 #include "insets/insetgraphics.h"
71 #include "insets/insetfoot.h"
72 #include "insets/insetmarginal.h"
73 #include "insets/insetminipage.h"
74 #include "insets/insetfloat.h"
75 #include "insets/insettabular.h"
77 #include "insets/insettheorem.h"
78 #include "insets/insetlist.h"
80 #include "insets/insetcaption.h"
81 #include "insets/insetfloatlist.h"
83 #include "frontends/Dialogs.h"
84 #include "frontends/Alert.h"
86 #include "support/textutils.h"
87 #include "support/filetools.h"
88 #include "support/path.h"
89 #include "support/os.h"
90 #include "support/lyxlib.h"
91 #include "support/FileInfo.h"
92 #include "support/lyxmanip.h"
93 #include "support/lyxalgo.h" // for lyx::count
105 #include <sys/types.h>
122 using std::make_pair;
131 using lyx::textclass_type;
133 // all these externs should eventually be removed.
134 extern BufferList bufferlist;
136 extern LyXAction lyxaction;
140 const int LYX_FORMAT = 220;
144 extern int tex_code_break_column;
147 Buffer::Buffer(string const & file, bool ronly)
148 : paragraph(0), niceFile(true), lyx_clean(true), bak_clean(true),
149 unnamed(false), dep_clean(0), read_only(ronly),
150 filename_(file), users(0)
152 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
154 filepath_ = OnlyPath(file);
159 // read_only = ronly;
163 if (read_only || lyxrc.use_tempdir) {
164 tmppath = CreateBufferTmpDir();
173 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
174 // here the buffer should take care that it is
175 // saved properly, before it goes into the void.
177 // make sure that views using this buffer
182 if (!tmppath.empty()) {
183 DestroyBufferTmpDir(tmppath);
186 Paragraph * par = paragraph;
189 tmppar = par->next();
197 string const Buffer::getLatexName(bool no_path) const
199 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
201 return OnlyFilename(name);
207 pair<Buffer::LogType, string> const Buffer::getLogName(void) const
209 string const filename = getLatexName(false);
211 if (filename.empty())
212 return make_pair(Buffer::latexlog, string());
214 string path = OnlyPath(filename);
216 if (lyxrc.use_tempdir || !IsDirWriteable(path))
219 string const fname = AddName(path,
220 OnlyFilename(ChangeExtension(filename,
223 AddName(path, OnlyFilename(
224 ChangeExtension(filename,
225 formats.extension("literate") + ".out")));
227 // If no Latex log or Build log is newer, show Build log
229 FileInfo const f_fi(fname);
230 FileInfo const b_fi(bname);
233 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
234 lyxerr[Debug::FILES] << "Log name calculated as : " << bname << endl;
235 return make_pair(Buffer::buildlog, bname);
237 lyxerr[Debug::FILES] << "Log name calculated as : " << fname << endl;
238 return make_pair(Buffer::latexlog, fname);
242 void Buffer::setReadonly(bool flag)
244 if (read_only != flag) {
247 users->owner()->getDialogs()->updateBufferDependent(false);
252 /// Update window titles of all users
253 // Should work on a list
254 void Buffer::updateTitles() const
257 users->owner()->updateWindowTitle();
261 /// Reset autosave timer of all users
262 // Should work on a list
263 void Buffer::resetAutosaveTimers() const
266 users->owner()->resetAutosaveTimer();
270 void Buffer::setFileName(string const & newfile)
272 filename_ = MakeAbsPath(newfile);
273 filepath_ = OnlyPath(filename_);
274 setReadonly(IsFileWriteable(filename_) == 0);
279 // We'll remove this later. (Lgb)
282 string last_inset_read;
284 #ifndef NO_COMPABILITY
287 ErtComp() : active(false), fromlayout(false), in_tabular(false) {
296 std::stack<ErtComp> ert_stack;
301 #warning And _why_ is this here? (Lgb)
309 // candidate for move to BufferView
310 // (at least some parts in the beginning of the func)
313 // changed to be public and have one parameter
314 // if par = 0 normal behavior
315 // else insert behavior
316 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
317 bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
321 #ifndef NO_COMPABILITY
322 ert_comp.contents.erase();
323 ert_comp.active = false;
324 ert_comp.fromlayout = false;
325 ert_comp.in_tabular = false;
328 Paragraph::depth_type depth = 0;
329 bool the_end_read = false;
331 Paragraph * first_par = 0;
332 LyXFont font(LyXFont::ALL_INHERIT, params.language);
333 if (file_format < 216 && params.language->lang() == "hebrew")
334 font.setLanguage(default_language);
338 par->layout(textclasslist[params.textclass].defaultLayoutName());
340 // We are inserting into an existing document
341 users->text->breakParagraph(users);
342 first_par = users->text->ownerParagraph();
345 // We don't want to adopt the parameters from the
346 // document we insert, so we skip until the text begins:
349 string const pretoken = lex.getString();
350 if (pretoken == "\\layout") {
351 lex.pushToken(pretoken);
359 string const token = lex.getString();
361 if (token.empty()) continue;
363 lyxerr[Debug::PARSER] << "Handling token: `"
364 << token << "'" << endl;
367 parseSingleLyXformat2Token(lex, par, first_par,
375 paragraph = first_par;
377 if (unknown_layouts > 0) {
378 string s = _("Couldn't set the layout for ");
379 if (unknown_layouts == 1) {
380 s += _("one paragraph");
382 s += tostr(unknown_layouts);
383 s += _(" paragraphs");
385 Alert::alert(_("Textclass Loading Error!"), s,
386 _("When reading " + fileName()));
389 if (unknown_tokens > 0) {
390 string s = _("Encountered ");
391 if (unknown_tokens == 1) {
392 s += _("one unknown token");
394 s += tostr(unknown_tokens);
395 s += _(" unknown tokens");
397 Alert::alert(_("Textclass Loading Error!"), s,
398 _("When reading " + fileName()));
405 #ifndef NO_COMPABILITY
407 Inset * Buffer::isErtInset(Paragraph * par, int pos) const
410 if ((par->getChar(pos) == Paragraph::META_INSET) &&
411 (inset = par->getInset(pos)) &&
412 (inset->lyxCode() == Inset::ERT_CODE))
419 void Buffer::insertErtContents(Paragraph * par, int & pos, bool set_inactive)
421 string str = frontStrip(strip(ert_comp.contents, ' '), ' ');
423 lyxerr[Debug::INSETS] << "ERT contents:\n'"
424 << str << "'" << endl;
425 // check if we have already an ert inset a position earlier
426 // if this is the case then we should insert the contents
427 // inside the other ertinset as it is stupid to have various
430 if ((pos > 0) && (inset=isErtInset(par, pos-1))) {
431 // get the last paragraph from the inset before
432 Paragraph * last = inset->firstParagraph();
435 // create the new paragraph after the last one
436 Paragraph * par = new Paragraph(last);
437 par->layout(textclasslist[params.textclass].defaultLayoutName());
438 par->setInsetOwner(last->inInset());
440 LyXFont font(LyXFont::ALL_INHERIT, params.language);
441 string::const_iterator cit = str.begin();
442 string::const_iterator end = str.end();
444 for (; cit != end; ++cit) {
445 par->insertChar(pos++, *cit, font);
449 new InsetERT(params, params.language, str, true);
450 par->insertInset(pos++, inset, ert_comp.font);
453 ert_comp.contents.erase();
454 ert_comp.fromlayout = false;
456 ert_comp.active = false;
463 Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
464 Paragraph *& first_par,
465 string const & token, int & pos,
466 Paragraph::depth_type & depth,
470 bool the_end_read = false;
471 #ifndef NO_COMPABILITY
472 #ifndef NO_PEXTRA_REALLY
473 // This is super temporary but is needed to get the compability
474 // mode for minipages work correctly together with new tabulars.
475 static int call_depth;
477 bool checkminipage = false;
478 static Paragraph * minipar = 0;
479 static Paragraph * parBeforeMinipage;
482 if (token[0] != '\\') {
483 #ifndef NO_COMPABILITY
484 if (ert_comp.active) {
485 ert_comp.contents += token;
488 for (string::const_iterator cit = token.begin();
489 cit != token.end(); ++cit) {
490 par->insertChar(pos, (*cit), font);
493 #ifndef NO_COMPABILITY
496 } else if (token == "\\i") {
497 Inset * inset = new InsetLatexAccent;
498 inset->read(this, lex);
499 par->insertInset(pos, inset, font);
501 } else if (token == "\\layout") {
502 #ifndef NO_COMPABILITY
503 bool old_fromlayout = ert_comp.fromlayout;
504 bool create_new_par = true;
505 ert_comp.in_tabular = false;
507 if (!par->size() && par->previous() &&
508 (par->previous()->size() == 1) &&
509 isErtInset(par->previous(), par->previous()->size()-1))
511 int p = par->previous()->size();
512 insertErtContents(par->previous(), p);
513 create_new_par = false;
515 insertErtContents(par, pos);
518 // reset the font as we start a new layout and if the font is
519 // not ALL_INHERIT,document_language then it will be set to the
520 // right values after this tag (Jug 20020420)
521 font = LyXFont(LyXFont::ALL_INHERIT, params.language);
522 if (file_format < 216 && params.language->lang() == "hebrew")
523 font.setLanguage(default_language);
526 string layoutname = lex.getString();
528 LyXTextClass const & tclass = textclasslist[params.textclass];
530 if (layoutname.empty()) {
531 layoutname = tclass.defaultLayoutName();
533 #ifndef NO_COMPABILITY
534 if (compare_no_case(layoutname, "latex") == 0) {
535 ert_comp.active = true;
536 ert_comp.fromlayout = true;
537 ert_comp.font = font;
539 create_new_par = false;
542 bool hasLayout = tclass.hasLayout(layoutname);
544 lyxerr << "Layout '" << layoutname << "' does not"
545 << " exist in textclass '" << tclass.name()
547 lyxerr << "Trying to use default layout instead."
549 layoutname = tclass.defaultLayoutName();
553 // The is the compability reading of layout caption.
554 // It can be removed in LyX version 1.3.0. (Lgb)
555 if (compare_no_case(layoutname, "caption") == 0) {
556 // We expect that the par we are now working on is
557 // really inside a InsetText inside a InsetFloat.
558 // We also know that captions can only be
559 // one paragraph. (Lgb)
561 // We should now read until the next "\layout"
563 // This is probably not good enough, what if the
564 // caption is the last par in the document (Lgb)
565 istream & ist = lex.getStream();
571 if (prefixIs(line, "\\layout")) {
575 if (prefixIs(line, "\\begin_inset"))
577 if (prefixIs(line, "\\end_inset")) {
588 // Now we should have the whole layout in ss
589 // we should now be able to give this to the
591 ss << "\\end_inset\n";
593 // This seems like a bug in stringstream.
594 // We really should be able to use ss
596 istringstream is(ss.str());
598 tmplex.setStream(is);
599 Inset * inset = new InsetCaption;
600 inset->Read(this, tmplex);
601 par->InsertInset(pos, inset, font);
605 #ifndef NO_COMPABILITY
606 if (create_new_par) {
611 par = new Paragraph(par);
612 par->layout(textclasslist[params.textclass].defaultLayoutName());
615 par->layout(layoutname);
616 // Test whether the layout is obsolete.
617 LyXLayout const & layout =
618 textclasslist[params.textclass][par->layout()];
619 if (!layout.obsoleted_by().empty())
620 par->layout(layout.obsoleted_by());
621 par->params().depth(depth);
622 #ifndef NO_COMPABILITY
624 // we duplicate code here because it's easier to remove
625 // the code then of NO_COMPATIBILITY
626 par->layout(layoutname);
627 // Test whether the layout is obsolete.
628 LyXLayout const & layout =
629 textclasslist[params.textclass][par->layout()];
630 if (!layout.obsoleted_by().empty())
631 par->layout(layout.obsoleted_by());
632 par->params().depth(depth);
639 #ifndef NO_COMPABILITY
640 } else if (token == "\\begin_float") {
641 insertErtContents(par, pos);
642 //insertErtContents(par, pos, false);
643 //ert_stack.push(ert_comp);
644 //ert_comp = ErtComp();
646 // This is the compability reader. It can be removed in
647 // LyX version 1.3.0. (Lgb)
649 string const tmptok = lex.getString();
650 //lyxerr << "old float: " << tmptok << endl;
653 stringstream old_float;
655 if (tmptok == "footnote") {
656 inset = new InsetFoot(params);
657 old_float << "collapsed true\n";
658 } else if (tmptok == "margin") {
659 inset = new InsetMarginal(params);
660 old_float << "collapsed true\n";
661 } else if (tmptok == "fig") {
662 inset = new InsetFloat(params, "figure");
663 old_float << "placement htbp\n"
665 << "collapsed false\n";
666 } else if (tmptok == "tab") {
667 inset = new InsetFloat(params, "table");
668 old_float << "placement htbp\n"
670 << "collapsed false\n";
671 } else if (tmptok == "alg") {
672 inset = new InsetFloat(params, "algorithm");
673 old_float << "placement htbp\n"
675 << "collapsed false\n";
676 } else if (tmptok == "wide-fig") {
677 inset = new InsetFloat(params, "figure");
678 //InsetFloat * tmp = new InsetFloat("figure");
681 old_float << "placement htbp\n"
683 << "collapsed false\n";
684 } else if (tmptok == "wide-tab") {
685 inset = new InsetFloat(params, "table");
686 //InsetFloat * tmp = new InsetFloat("table");
689 old_float << "placement htbp\n"
691 << "collapsed false\n";
695 #ifndef NO_PEXTRA_REALLY
698 return false; // no end read yet
701 // Here we need to check for \end_deeper and handle that
702 // before we do the footnote parsing.
703 // This _is_ a hack! (Lgb)
706 string const tmp = lex.getString();
707 if (tmp == "\\end_deeper") {
708 //lyxerr << "\\end_deeper caught!" << endl;
710 lex.printError("\\end_deeper: "
711 "depth is already null");
716 old_float << tmp << ' ';
721 old_float << lex.getLongString("\\end_float")
722 << "\n\\end_inset\n";
723 //lyxerr << "Float Body:\n" << old_float.str() << endl;
724 // That this does not work seems like a bug
725 // in stringstream. (Lgb)
726 istringstream istr(old_float.str());
728 nylex.setStream(istr);
729 inset->read(this, nylex);
731 par->insertInset(pos, inset, font);
733 insertErtContents(par, pos);
735 // we have to reset the font as in the old format after a float
736 // the font was automatically reset!
737 font = LyXFont(LyXFont::ALL_INHERIT, params.language);
739 } else if (token == "\\begin_deeper") {
741 } else if (token == "\\end_deeper") {
743 lex.printError("\\end_deeper: "
744 "depth is already null");
748 } else if (token == "\\begin_preamble") {
749 params.readPreamble(lex);
750 } else if (token == "\\textclass") {
752 pair<bool, textclass_type> pp =
753 textclasslist.NumberOfClass(lex.getString());
755 params.textclass = pp.second;
757 Alert::alert(string(_("Textclass error")),
758 string(_("The document uses an unknown textclass \"")) +
759 lex.getString() + string("\"."),
760 string(_("LyX will not be able to produce output correctly.")));
761 params.textclass = 0;
763 if (!textclasslist[params.textclass].load()) {
764 // if the textclass wasn't loaded properly
765 // we need to either substitute another
766 // or stop loading the file.
767 // I can substitute but I don't see how I can
768 // stop loading... ideas?? ARRae980418
769 Alert::alert(_("Textclass Loading Error!"),
770 string(_("Can't load textclass ")) +
771 textclasslist[params.textclass].name(),
772 _("-- substituting default"));
773 params.textclass = 0;
775 } else if (token == "\\options") {
777 params.options = lex.getString();
778 } else if (token == "\\language") {
779 params.readLanguage(lex);
780 } else if (token == "\\fontencoding") {
782 } else if (token == "\\inputencoding") {
784 params.inputenc = lex.getString();
785 } else if (token == "\\graphics") {
786 params.readGraphicsDriver(lex);
787 } else if (token == "\\fontscheme") {
789 params.fonts = lex.getString();
790 } else if (token == "\\noindent") {
791 par->params().noindent(true);
792 } else if (token == "\\leftindent") {
794 LyXLength value(lex.getString());
795 par->params().leftIndent(value);
796 } else if (token == "\\fill_top") {
797 par->params().spaceTop(VSpace(VSpace::VFILL));
798 } else if (token == "\\fill_bottom") {
799 par->params().spaceBottom(VSpace(VSpace::VFILL));
800 } else if (token == "\\line_top") {
801 par->params().lineTop(true);
802 } else if (token == "\\line_bottom") {
803 par->params().lineBottom(true);
804 } else if (token == "\\pagebreak_top") {
805 par->params().pagebreakTop(true);
806 } else if (token == "\\pagebreak_bottom") {
807 par->params().pagebreakBottom(true);
808 } else if (token == "\\start_of_appendix") {
809 par->params().startOfAppendix(true);
810 } else if (token == "\\paragraph_separation") {
811 int tmpret = lex.findToken(string_paragraph_separation);
814 if (tmpret != LYX_LAYOUT_DEFAULT)
815 params.paragraph_separation =
816 static_cast<BufferParams::PARSEP>(tmpret);
817 } else if (token == "\\defskip") {
819 params.defskip = VSpace(lex.getString());
820 #ifndef NO_COMPABILITY
821 } else if (token == "\\epsfig") { // obsolete
822 // Indeed it is obsolete, but we HAVE to be backwards
823 // compatible until 0.14, because otherwise all figures
824 // in existing documents are irretrivably lost. (Asger)
825 params.readGraphicsDriver(lex);
827 } else if (token == "\\quotes_language") {
828 int tmpret = lex.findToken(string_quotes_language);
831 if (tmpret != LYX_LAYOUT_DEFAULT) {
832 InsetQuotes::quote_language tmpl =
833 InsetQuotes::EnglishQ;
836 tmpl = InsetQuotes::EnglishQ;
839 tmpl = InsetQuotes::SwedishQ;
842 tmpl = InsetQuotes::GermanQ;
845 tmpl = InsetQuotes::PolishQ;
848 tmpl = InsetQuotes::FrenchQ;
851 tmpl = InsetQuotes::DanishQ;
854 params.quotes_language = tmpl;
856 } else if (token == "\\quotes_times") {
858 switch (lex.getInteger()) {
860 params.quotes_times = InsetQuotes::SingleQ;
863 params.quotes_times = InsetQuotes::DoubleQ;
866 } else if (token == "\\papersize") {
867 int tmpret = lex.findToken(string_papersize);
871 params.papersize2 = tmpret;
872 } else if (token == "\\paperpackage") {
873 int tmpret = lex.findToken(string_paperpackages);
876 params.paperpackage = BufferParams::PACKAGE_NONE;
878 params.paperpackage = tmpret;
879 } else if (token == "\\use_geometry") {
881 params.use_geometry = lex.getInteger();
882 } else if (token == "\\use_amsmath") {
884 params.use_amsmath = lex.getInteger();
885 } else if (token == "\\use_natbib") {
887 params.use_natbib = lex.getInteger();
888 } else if (token == "\\use_numerical_citations") {
890 params.use_numerical_citations = lex.getInteger();
891 } else if (token == "\\paperorientation") {
892 int tmpret = lex.findToken(string_orientation);
895 if (tmpret != LYX_LAYOUT_DEFAULT)
896 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
897 } else if (token == "\\paperwidth") {
899 params.paperwidth = lex.getString();
900 } else if (token == "\\paperheight") {
902 params.paperheight = lex.getString();
903 } else if (token == "\\leftmargin") {
905 params.leftmargin = lex.getString();
906 } else if (token == "\\topmargin") {
908 params.topmargin = lex.getString();
909 } else if (token == "\\rightmargin") {
911 params.rightmargin = lex.getString();
912 } else if (token == "\\bottommargin") {
914 params.bottommargin = lex.getString();
915 } else if (token == "\\headheight") {
917 params.headheight = lex.getString();
918 } else if (token == "\\headsep") {
920 params.headsep = lex.getString();
921 } else if (token == "\\footskip") {
923 params.footskip = lex.getString();
924 } else if (token == "\\paperfontsize") {
926 params.fontsize = strip(lex.getString());
927 } else if (token == "\\papercolumns") {
929 params.columns = lex.getInteger();
930 } else if (token == "\\papersides") {
932 switch (lex.getInteger()) {
934 case 1: params.sides = LyXTextClass::OneSide; break;
935 case 2: params.sides = LyXTextClass::TwoSides; break;
937 } else if (token == "\\paperpagestyle") {
939 params.pagestyle = strip(lex.getString());
940 } else if (token == "\\bullet") {
942 int const index = lex.getInteger();
944 int temp_int = lex.getInteger();
945 params.user_defined_bullets[index].setFont(temp_int);
946 params.temp_bullets[index].setFont(temp_int);
948 temp_int = lex.getInteger();
949 params.user_defined_bullets[index].setCharacter(temp_int);
950 params.temp_bullets[index].setCharacter(temp_int);
952 temp_int = lex.getInteger();
953 params.user_defined_bullets[index].setSize(temp_int);
954 params.temp_bullets[index].setSize(temp_int);
956 string const temp_str = lex.getString();
957 if (temp_str != "\\end_bullet") {
958 // this element isn't really necessary for
959 // parsing but is easier for humans
960 // to understand bullets. Put it back and
961 // set a debug message?
962 lex.printError("\\end_bullet expected, got" + temp_str);
963 //how can I put it back?
965 } else if (token == "\\bulletLaTeX") {
966 // The bullet class should be able to read this.
968 int const index = lex.getInteger();
970 string temp_str = lex.getString();
972 while (temp_str != "\\end_bullet") {
973 // this loop structure is needed when user
974 // enters an empty string since the first
975 // thing returned will be the \\end_bullet
977 // if the LaTeX entry has spaces. Each element
978 // therefore needs to be read in turn
981 temp_str = lex.getString();
984 params.user_defined_bullets[index].setText(sum_str);
985 params.temp_bullets[index].setText(sum_str);
986 } else if (token == "\\secnumdepth") {
988 params.secnumdepth = lex.getInteger();
989 } else if (token == "\\tocdepth") {
991 params.tocdepth = lex.getInteger();
992 } else if (token == "\\spacing") {
994 string const tmp = strip(lex.getString());
995 Spacing::Space tmp_space = Spacing::Default;
997 if (tmp == "single") {
998 tmp_space = Spacing::Single;
999 } else if (tmp == "onehalf") {
1000 tmp_space = Spacing::Onehalf;
1001 } else if (tmp == "double") {
1002 tmp_space = Spacing::Double;
1003 } else if (tmp == "other") {
1005 tmp_space = Spacing::Other;
1006 tmp_val = lex.getFloat();
1008 lex.printError("Unknown spacing token: '$$Token'");
1010 // Small hack so that files written with klyx will be
1011 // parsed correctly.
1013 par->params().spacing(Spacing(tmp_space, tmp_val));
1015 params.spacing.set(tmp_space, tmp_val);
1017 } else if (token == "\\paragraph_spacing") {
1019 string const tmp = strip(lex.getString());
1020 if (tmp == "single") {
1021 par->params().spacing(Spacing(Spacing::Single));
1022 } else if (tmp == "onehalf") {
1023 par->params().spacing(Spacing(Spacing::Onehalf));
1024 } else if (tmp == "double") {
1025 par->params().spacing(Spacing(Spacing::Double));
1026 } else if (tmp == "other") {
1028 par->params().spacing(Spacing(Spacing::Other,
1031 lex.printError("Unknown spacing token: '$$Token'");
1033 } else if (token == "\\float_placement") {
1035 params.float_placement = lex.getString();
1036 } else if (token == "\\family") {
1038 font.setLyXFamily(lex.getString());
1039 } else if (token == "\\series") {
1041 font.setLyXSeries(lex.getString());
1042 } else if (token == "\\shape") {
1044 font.setLyXShape(lex.getString());
1045 } else if (token == "\\size") {
1047 font.setLyXSize(lex.getString());
1048 #ifndef NO_COMPABILITY
1049 } else if (token == "\\latex") {
1051 string const tok = lex.getString();
1052 if (tok == "no_latex") {
1054 insertErtContents(par, pos);
1055 } else if (tok == "latex") {
1056 ert_comp.active = true;
1057 ert_comp.font = font;
1058 } else if (tok == "default") {
1060 insertErtContents(par, pos);
1062 lex.printError("Unknown LaTeX font flag "
1066 } else if (token == "\\lang") {
1068 string const tok = lex.getString();
1069 Language const * lang = languages.getLanguage(tok);
1071 font.setLanguage(lang);
1073 font.setLanguage(params.language);
1074 lex.printError("Unknown language `$$Token'");
1076 #ifndef NO_COMPABILITY
1077 // if the contents is still empty we're just behind the
1078 // latex font change and so this inset should also be
1079 // inserted with that language tag!
1080 if (ert_comp.active && ert_comp.contents.empty()) {
1081 ert_comp.font.setLanguage(font.language());
1084 } else if (token == "\\numeric") {
1086 font.setNumber(font.setLyXMisc(lex.getString()));
1087 } else if (token == "\\emph") {
1089 font.setEmph(font.setLyXMisc(lex.getString()));
1090 } else if (token == "\\bar") {
1092 string const tok = lex.getString();
1093 // This is dirty, but gone with LyX3. (Asger)
1095 font.setUnderbar(LyXFont::ON);
1096 else if (tok == "no")
1097 font.setUnderbar(LyXFont::OFF);
1098 else if (tok == "default")
1099 font.setUnderbar(LyXFont::INHERIT);
1101 lex.printError("Unknown bar font flag "
1103 } else if (token == "\\noun") {
1105 font.setNoun(font.setLyXMisc(lex.getString()));
1106 } else if (token == "\\color") {
1108 font.setLyXColor(lex.getString());
1109 } else if (token == "\\align") {
1110 int tmpret = lex.findToken(string_align);
1111 if (tmpret == -1) ++tmpret;
1112 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
1113 int const tmpret2 = int(pow(2.0, tmpret));
1114 //lyxerr << "Tmpret2 = " << tmpret2 << endl;
1115 par->params().align(LyXAlignment(tmpret2));
1117 } else if (token == "\\added_space_top") {
1119 VSpace value = VSpace(lex.getString());
1120 if ((value.length().len().value() != 0) ||
1121 (value.kind() != VSpace::LENGTH))
1122 par->params().spaceTop(value);
1123 } else if (token == "\\added_space_bottom") {
1125 VSpace value = VSpace(lex.getString());
1126 if ((value.length().len().value() != 0) ||
1127 (value.kind() != VSpace::LENGTH))
1128 par->params().spaceBottom(value);
1129 #ifndef NO_COMPABILITY
1130 #ifndef NO_PEXTRA_REALLY
1131 } else if (token == "\\pextra_type") {
1133 par->params().pextraType(lex.getInteger());
1134 } else if (token == "\\pextra_width") {
1136 par->params().pextraWidth(lex.getString());
1137 } else if (token == "\\pextra_widthp") {
1139 par->params().pextraWidthp(lex.getString());
1140 } else if (token == "\\pextra_alignment") {
1142 par->params().pextraAlignment(lex.getInteger());
1143 } else if (token == "\\pextra_hfill") {
1145 par->params().pextraHfill(lex.getInteger());
1146 } else if (token == "\\pextra_start_minipage") {
1148 par->params().pextraStartMinipage(lex.getInteger());
1151 } else if (token == "\\labelwidthstring") {
1153 par->params().labelWidthString(lex.getString());
1154 // do not delete this token, it is still needed!
1155 } else if (token == "\\end_inset") {
1156 lyxerr << "Solitary \\end_inset. Missing \\begin_inset?.\n"
1157 << "Last inset read was: " << last_inset_read
1159 // Simply ignore this. The insets do not have
1161 // But insets should read it, it is a part of
1162 // the inset isn't it? Lgb.
1163 } else if (token == "\\begin_inset") {
1164 #ifndef NO_COMPABILITY
1165 insertErtContents(par, pos, false);
1166 ert_stack.push(ert_comp);
1167 ert_comp = ErtComp();
1169 readInset(lex, par, pos, font);
1170 #ifndef NO_COMPABILITY
1171 ert_comp = ert_stack.top();
1174 } else if (token == "\\SpecialChar") {
1175 LyXLayout const & layout =
1176 textclasslist[params.textclass][par->layout()];
1178 // Insets don't make sense in a free-spacing context! ---Kayvan
1179 if (layout.free_spacing || par->isFreeSpacing()) {
1182 string next_token = lex.getString();
1183 if (next_token == "\\-") {
1184 par->insertChar(pos, '-', font);
1185 } else if (next_token == "\\protected_separator"
1186 || next_token == "~") {
1187 par->insertChar(pos, ' ', font);
1189 lex.printError("Token `$$Token' "
1191 "paragraph layout!");
1196 Inset * inset = new InsetSpecialChar;
1197 inset->read(this, lex);
1198 par->insertInset(pos, inset, font);
1201 } else if (token == "\\newline") {
1202 #ifndef NO_COMPABILITY
1203 if (!ert_comp.in_tabular && ert_comp.active) {
1204 ert_comp.contents += char(Paragraph::META_NEWLINE);
1206 // Since we cannot know it this is only a regular
1207 // newline or a tabular cell delimter we have to
1208 // handle the ERT here.
1209 insertErtContents(par, pos, false);
1211 par->insertChar(pos, Paragraph::META_NEWLINE, font);
1215 par->insertChar(pos, Paragraph::META_NEWLINE, font);
1218 } else if (token == "\\LyXTable") {
1219 #ifndef NO_COMPABILITY
1220 ert_comp.in_tabular = true;
1222 Inset * inset = new InsetTabular(*this);
1223 inset->read(this, lex);
1224 par->insertInset(pos, inset, font);
1226 } else if (token == "\\hfill") {
1227 par->insertChar(pos, Paragraph::META_HFILL, font);
1229 } else if (token == "\\protected_separator") { // obsolete
1230 // This is a backward compability thingie. (Lgb)
1231 // Remove it later some time...introduced with fileformat
1233 LyXLayout const & layout =
1234 textclasslist[params.textclass][par->layout()];
1236 if (layout.free_spacing || par->isFreeSpacing()) {
1237 par->insertChar(pos, ' ', font);
1239 Inset * inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
1240 par->insertInset(pos, inset, font);
1243 } else if (token == "\\bibitem") { // ale970302
1245 InsetCommandParams p("bibitem", "dummy");
1246 par->bibkey = new InsetBibKey(p);
1248 par->bibkey->read(this, lex);
1249 } else if (token == "\\backslash") {
1250 #ifndef NO_COMPABILITY
1251 if (ert_comp.active) {
1252 ert_comp.contents += "\\";
1255 par->insertChar(pos, '\\', font);
1257 #ifndef NO_COMPABILITY
1260 } else if (token == "\\the_end") {
1261 #ifndef NO_COMPABILITY
1262 // If we still have some ert active here we have to insert
1263 // it so we don't loose it. (Lgb)
1264 insertErtContents(par, pos);
1266 the_end_read = true;
1267 #ifndef NO_COMPABILITY
1268 #ifndef NO_PEXTRA_REALLY
1271 minipar = parBeforeMinipage = 0;
1275 #ifndef NO_COMPABILITY
1276 if (ert_comp.active) {
1277 ert_comp.contents += token;
1280 // This should be insurance for the future: (Asger)
1283 string const s = _("Unknown token: ") + token
1284 + " " + lex.text() + "\n";
1285 // we can do this here this way because we're actually reading
1286 // the buffer and don't care about LyXText right now.
1287 InsetError * new_inset = new InsetError(s);
1288 par->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT,
1291 #ifndef NO_COMPABILITY
1296 #ifndef NO_COMPABILITY
1297 #ifndef NO_PEXTRA_REALLY
1298 // I wonder if we could use this blanket fix for all the
1299 // checkminipage cases...
1300 // don't forget about ert paragraphs and compatibility read for'em
1301 if (par && (par->size() || !ert_comp.contents.empty())) {
1302 // It is possible that this will check to often,
1303 // but that should not be an correctness issue.
1304 // Only a speed issue.
1305 checkminipage = true;
1308 // now check if we have a minipage paragraph as at this
1309 // point we already read all the necessary data!
1310 // this cannot be done in layout because there we did
1311 // not read yet the paragraph PEXTRA-params (Jug)
1313 // BEGIN pextra_minipage compability
1314 // This should be removed in 1.3.x (Lgb)
1315 // I don't think we should remove this so fast (Jug)
1317 // This compability code is not perfect. In a couple
1318 // of rand cases it fails. When the minipage par is
1319 // the first par in the document, and when there are
1320 // none or only one regular paragraphs after the
1321 // minipage. Currently I am not investing any effort
1322 // in fixing those cases.
1324 // lyxerr << "Call depth: " << call_depth << endl;
1325 // lyxerr << "Checkminipage: " << checkminipage << endl;
1327 if (checkminipage && (call_depth == 1)) {
1328 checkminipage = false;
1329 if (minipar && (minipar != par) &&
1330 (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE)) {
1331 lyxerr << "minipages in a row" << endl;
1332 if (par->params().pextraStartMinipage()) {
1333 lyxerr << "start new minipage" << endl;
1334 // minipages in a row
1335 par->previous()->next(0);
1338 Paragraph * tmp = minipar;
1340 tmp->params().pextraType(0);
1341 tmp->params().pextraWidth(string());
1342 tmp->params().pextraWidthp(string());
1343 tmp->params().pextraAlignment(0);
1344 tmp->params().pextraHfill(false);
1345 tmp->params().pextraStartMinipage(false);
1348 // create a new paragraph to insert the
1349 // minipages in the following case
1350 if (par->params().pextraStartMinipage() &&
1351 !par->params().pextraHfill()) {
1352 Paragraph * p = new Paragraph;
1353 p->layout(textclasslist[params.textclass].defaultLayoutName());
1355 p->previous(parBeforeMinipage);
1356 parBeforeMinipage->next(p);
1358 p->params().depth(parBeforeMinipage->params().depth());
1359 parBeforeMinipage = p;
1361 InsetMinipage * mini = new InsetMinipage(params);
1362 mini->pos(static_cast<InsetMinipage::Position>(par->params().pextraAlignment()));
1363 mini->pageWidth(LyXLength(par->params().pextraWidth()));
1364 if (!par->params().pextraWidthp().empty()) {
1365 lyxerr << "WP:" << mini->pageWidth().asString() << endl;
1366 mini->pageWidth(LyXLength((par->params().pextraWidthp())+"col%"));
1368 Paragraph * op = mini->firstParagraph();
1369 mini->inset.paragraph(par);
1371 // and free the old ones!
1374 Paragraph * pp = op->next();
1378 // Insert the minipage last in the
1379 // previous paragraph.
1380 if (par->params().pextraHfill()) {
1381 parBeforeMinipage->insertChar
1382 (parBeforeMinipage->size(),
1383 Paragraph::META_HFILL, font);
1385 parBeforeMinipage->insertInset
1386 (parBeforeMinipage->size(), mini, font);
1390 lyxerr << "new minipage par" << endl;
1391 //nothing to do just continue reading
1394 } else if (minipar && (minipar != par)) {
1395 lyxerr << "last minipage par read" << endl;
1396 // The last paragraph read was not part of a
1397 // minipage but the par linked list is...
1398 // So we need to remove the last par from the
1400 if (par->previous())
1401 par->previous()->next(0);
1402 par->previous(parBeforeMinipage);
1403 parBeforeMinipage->next(par);
1404 Paragraph * tmp = minipar;
1406 tmp->params().pextraType(0);
1407 tmp->params().pextraWidth(string());
1408 tmp->params().pextraWidthp(string());
1409 tmp->params().pextraAlignment(0);
1410 tmp->params().pextraHfill(false);
1411 tmp->params().pextraStartMinipage(false);
1414 depth = parBeforeMinipage->params().depth();
1415 // and set this depth on the par as it has not been set already
1416 par->params().depth(depth);
1417 minipar = parBeforeMinipage = 0;
1418 } else if (!minipar &&
1419 (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE)) {
1420 // par is the first paragraph in a minipage
1421 lyxerr << "begin minipage" << endl;
1422 // To minimize problems for
1423 // the users we will insert
1424 // the first minipage in
1425 // a sequence of minipages
1426 // in its own paragraph.
1427 Paragraph * p = new Paragraph;
1428 p->layout(textclasslist[params.textclass].defaultLayoutName());
1429 p->previous(par->previous());
1431 p->params().depth(depth);
1432 par->params().depth(0);
1434 if (par->previous())
1435 par->previous()->next(p);
1437 parBeforeMinipage = p;
1439 if (!first_par || (first_par == par))
1442 InsetMinipage * mini = new InsetMinipage(params);
1443 mini->pos(static_cast<InsetMinipage::Position>(minipar->params().pextraAlignment()));
1444 mini->pageWidth(LyXLength(minipar->params().pextraWidth()));
1445 if (!par->params().pextraWidthp().empty()) {
1446 lyxerr << "WP:" << mini->pageWidth().asString() << endl;
1447 mini->pageWidth(LyXLength((par->params().pextraWidthp())+"col%"));
1450 Paragraph * op = mini->firstParagraph();
1451 mini->inset.paragraph(minipar);
1453 // and free the old ones!
1456 Paragraph * pp = op->next();
1461 // Insert the minipage last in the
1462 // previous paragraph.
1463 if (minipar->params().pextraHfill()) {
1464 parBeforeMinipage->insertChar
1465 (parBeforeMinipage->size(),
1466 Paragraph::META_HFILL, font);
1468 parBeforeMinipage->insertInset
1469 (parBeforeMinipage->size(), mini, font);
1470 } else if (par->params().pextraType() == Paragraph::PEXTRA_INDENT) {
1471 par->params().leftIndent(LyXLength(par->params().pextraWidth()));
1472 if (!par->params().pextraWidthp().empty()) {
1473 par->params().leftIndent(LyXLength((par->params().pextraWidthp())+"col%"));
1477 // End of pextra_minipage compability
1481 return the_end_read;
1484 // needed to insert the selection
1485 void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
1486 LyXFont const & fn,string const & str) const
1488 LyXLayout const & layout =
1489 textclasslist[params.textclass][par->layout()];
1492 par->checkInsertChar(font);
1493 // insert the string, don't insert doublespace
1494 bool space_inserted = true;
1495 bool autobreakrows = !par->inInset() ||
1496 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
1497 for(string::const_iterator cit = str.begin();
1498 cit != str.end(); ++cit) {
1500 if (autobreakrows && (par->size() || layout.keepempty)) {
1501 par->breakParagraph(params, pos,
1502 layout.isEnvironment());
1505 space_inserted = true;
1509 // do not insert consecutive spaces if !free_spacing
1510 } else if ((*cit == ' ' || *cit == '\t') &&
1511 space_inserted && !layout.free_spacing &&
1512 !par->isFreeSpacing())
1515 } else if (*cit == '\t') {
1516 if (!layout.free_spacing && !par->isFreeSpacing()) {
1517 // tabs are like spaces here
1518 par->insertChar(pos, ' ', font);
1520 space_inserted = true;
1522 const pos_type nb = 8 - pos % 8;
1523 for (pos_type a = 0; a < nb ; ++a) {
1524 par->insertChar(pos, ' ', font);
1527 space_inserted = true;
1529 } else if (!IsPrintable(*cit)) {
1530 // Ignore unprintables
1533 // just insert the character
1534 par->insertChar(pos, *cit, font);
1536 space_inserted = (*cit == ' ');
1543 void Buffer::readInset(LyXLex & lex, Paragraph *& par,
1544 int & pos, LyXFont & font)
1546 // consistency check
1547 if (lex.getString() != "\\begin_inset") {
1548 lyxerr << "Buffer::readInset: Consistency check failed."
1555 string const tmptok = lex.getString();
1556 last_inset_read = tmptok;
1558 // test the different insets
1559 if (tmptok == "LatexCommand") {
1560 InsetCommandParams inscmd;
1563 string const cmdName = inscmd.getCmdName();
1565 // This strange command allows LyX to recognize "natbib" style
1566 // citations: citet, citep, Citet etc.
1567 if (compare_no_case(cmdName, "cite", 4) == 0) {
1568 inset = new InsetCitation(inscmd);
1569 } else if (cmdName == "bibitem") {
1570 lex.printError("Wrong place for bibitem");
1571 inset = new InsetBibKey(inscmd);
1572 } else if (cmdName == "BibTeX") {
1573 inset = new InsetBibtex(inscmd);
1574 } else if (cmdName == "index") {
1575 inset = new InsetIndex(inscmd);
1576 } else if (cmdName == "include") {
1577 inset = new InsetInclude(inscmd, *this);
1578 } else if (cmdName == "label") {
1579 inset = new InsetLabel(inscmd);
1580 } else if (cmdName == "url"
1581 || cmdName == "htmlurl") {
1582 inset = new InsetUrl(inscmd);
1583 } else if (cmdName == "ref"
1584 || cmdName == "pageref"
1585 || cmdName == "vref"
1586 || cmdName == "vpageref"
1587 || cmdName == "prettyref") {
1588 if (!inscmd.getOptions().empty()
1589 || !inscmd.getContents().empty()) {
1590 inset = new InsetRef(inscmd, *this);
1592 } else if (cmdName == "tableofcontents") {
1593 inset = new InsetTOC(inscmd);
1594 } else if (cmdName == "listofalgorithms") {
1595 inset = new InsetFloatList("algorithm");
1596 } else if (cmdName == "listoffigures") {
1597 inset = new InsetFloatList("figure");
1598 } else if (cmdName == "listoftables") {
1599 inset = new InsetFloatList("table");
1600 } else if (cmdName == "printindex") {
1601 inset = new InsetPrintIndex(inscmd);
1602 } else if (cmdName == "lyxparent") {
1603 inset = new InsetParent(inscmd, *this);
1606 bool alreadyread = false;
1607 if (tmptok == "Quotes") {
1608 inset = new InsetQuotes;
1609 } else if (tmptok == "External") {
1610 inset = new InsetExternal;
1611 } else if (tmptok == "FormulaMacro") {
1612 inset = new InsetFormulaMacro;
1613 } else if (tmptok == "Formula") {
1614 inset = new InsetFormula;
1615 } else if (tmptok == "Figure") { // Backward compatibility
1616 // inset = new InsetFig(100, 100, *this);
1617 inset = new InsetGraphics;
1618 } else if (tmptok == "Graphics") {
1619 inset = new InsetGraphics;
1620 } else if (tmptok == "Info") {// backwards compatibility
1621 inset = new InsetNote(this,
1622 lex.getLongString("\\end_inset"),
1625 } else if (tmptok == "Note") {
1626 inset = new InsetNote(params);
1627 } else if (tmptok == "Include") {
1628 InsetCommandParams p("Include");
1629 inset = new InsetInclude(p, *this);
1630 } else if (tmptok == "ERT") {
1631 inset = new InsetERT(params);
1632 } else if (tmptok == "Tabular") {
1633 inset = new InsetTabular(*this);
1634 } else if (tmptok == "Text") {
1635 inset = new InsetText(params);
1636 } else if (tmptok == "Foot") {
1637 inset = new InsetFoot(params);
1638 } else if (tmptok == "Marginal") {
1639 inset = new InsetMarginal(params);
1640 } else if (tmptok == "Minipage") {
1641 inset = new InsetMinipage(params);
1642 } else if (tmptok == "Float") {
1644 string tmptok = lex.getString();
1645 inset = new InsetFloat(params, tmptok);
1647 } else if (tmptok == "List") {
1648 inset = new InsetList;
1649 } else if (tmptok == "Theorem") {
1650 inset = new InsetList;
1652 } else if (tmptok == "Caption") {
1653 inset = new InsetCaption(params);
1654 } else if (tmptok == "FloatList") {
1655 inset = new InsetFloatList;
1658 if (inset && !alreadyread) inset->read(this, lex);
1662 par->insertInset(pos, inset, font);
1668 bool Buffer::readFile(LyXLex & lex, Paragraph * par)
1672 string const token(lex.getString());
1673 if (token == "\\lyxformat") { // the first token _must_ be...
1675 string tmp_format = lex.getString();
1676 //lyxerr << "LyX Format: `" << tmp_format << "'" << endl;
1677 // if present remove ".," from string.
1678 string::size_type dot = tmp_format.find_first_of(".,");
1679 //lyxerr << " dot found at " << dot << endl;
1680 if (dot != string::npos)
1681 tmp_format.erase(dot, 1);
1682 file_format = strToInt(tmp_format);
1683 if (file_format == LYX_FORMAT) {
1685 } else if (file_format > LYX_FORMAT) {
1687 Alert::alert(_("Warning!"),
1688 _("LyX file format is newer that what"),
1689 _("is supported in this LyX version. Expect some problems."));
1691 } else if (file_format < LYX_FORMAT) {
1693 if (file_format < 200) {
1694 Alert::alert(_("ERROR!"),
1695 _("Old LyX file format found. "
1696 "Use LyX 0.10.x to read this!"));
1700 bool the_end = readLyXformat2(lex, par);
1701 params.setPaperStuff();
1702 // the_end was added in 213
1703 if (file_format < 213)
1707 Alert::alert(_("Warning!"),
1708 _("Reading of document is not complete"),
1709 _("Maybe the document is truncated"));
1712 } else { // "\\lyxformat" not found
1713 Alert::alert(_("ERROR!"), _("Not a LyX file!"));
1716 Alert::alert(_("ERROR!"), _("Unable to read file!"));
1721 // Should probably be moved to somewhere else: BufferView? LyXView?
1722 bool Buffer::save() const
1724 // We don't need autosaves in the immediate future. (Asger)
1725 resetAutosaveTimers();
1729 if (lyxrc.make_backup) {
1730 s = fileName() + '~';
1731 if (!lyxrc.backupdir_path.empty())
1732 s = AddName(lyxrc.backupdir_path,
1733 subst(os::slashify_path(s),'/','!'));
1735 // Rename is the wrong way of making a backup,
1736 // this is the correct way.
1737 /* truss cp fil fil2:
1738 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
1739 stat("LyXVC.lyx", 0xEFFFF688) = 0
1740 open("LyXVC.lyx", O_RDONLY) = 3
1741 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1742 fstat(4, 0xEFFFF508) = 0
1743 fstat(3, 0xEFFFF508) = 0
1744 read(3, " # T h i s f i l e w".., 8192) = 5579
1745 write(4, " # T h i s f i l e w".., 5579) = 5579
1746 read(3, 0xEFFFD4A0, 8192) = 0
1749 chmod("LyXVC3.lyx", 0100644) = 0
1750 lseek(0, 0, SEEK_CUR) = 46440
1754 // Should probably have some more error checking here.
1755 // Should be cleaned up in 0.13, at least a bit.
1756 // Doing it this way, also makes the inodes stay the same.
1757 // This is still not a very good solution, in particular we
1758 // might loose the owner of the backup.
1759 FileInfo finfo(fileName());
1760 if (finfo.exist()) {
1761 mode_t fmode = finfo.getMode();
1762 struct utimbuf times = {
1763 finfo.getAccessTime(),
1764 finfo.getModificationTime() };
1766 ifstream ifs(fileName().c_str());
1767 ofstream ofs(s.c_str(), ios::out|ios::trunc);
1772 ::chmod(s.c_str(), fmode);
1774 if (::utime(s.c_str(), ×)) {
1775 lyxerr << "utime error." << endl;
1778 lyxerr << "LyX was not able to make "
1779 "backup copy. Beware." << endl;
1784 if (writeFile(fileName(), false)) {
1786 removeAutosaveFile(fileName());
1788 // Saving failed, so backup is not backup
1789 if (lyxrc.make_backup) {
1790 lyx::rename(s, fileName());
1798 // Returns false if unsuccesful
1799 bool Buffer::writeFile(string const & fname, bool flag) const
1801 // if flag is false writeFile will not create any GUI
1802 // warnings, only cerr.
1803 // Needed for autosave in background or panic save (Matthias 120496)
1805 if (read_only && (fname == fileName())) {
1806 // Here we should come with a question if we should
1807 // perform the write anyway.
1809 lyxerr << _("Error! Document is read-only: ")
1812 Alert::alert(_("Error! Document is read-only: "),
1817 FileInfo finfo(fname);
1818 if (finfo.exist() && !finfo.writable()) {
1819 // Here we should come with a question if we should
1820 // try to do the save anyway. (i.e. do a chmod first)
1822 lyxerr << _("Error! Cannot write file: ")
1825 Alert::err_alert(_("Error! Cannot write file: "),
1830 ofstream ofs(fname.c_str());
1833 lyxerr << _("Error! Cannot open file: ")
1836 Alert::err_alert(_("Error! Cannot open file: "),
1842 // Use the standard "C" locale for file output.
1843 ofs.imbue(std::locale::classic());
1846 // The top of the file should not be written by params.
1848 // write out a comment in the top of the file
1849 ofs << '#' << lyx_docversion
1850 << " created this file. For more info see http://www.lyx.org/\n"
1851 << "\\lyxformat " << LYX_FORMAT << "\n";
1853 // now write out the buffer paramters.
1854 params.writeFile(ofs);
1856 Paragraph::depth_type depth = 0;
1858 // this will write out all the paragraphs
1859 // using recursive descent.
1860 paragraph->writeFile(this, ofs, params, depth);
1862 // Write marker that shows file is complete
1863 ofs << "\n\\the_end" << endl;
1867 // how to check if close went ok?
1868 // Following is an attempt... (BE 20001011)
1870 // good() returns false if any error occured, including some
1871 // formatting error.
1872 // bad() returns true if something bad happened in the buffer,
1873 // which should include file system full errors.
1880 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
1882 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
1894 pair<int, string> const addDepth(int depth, int ldepth)
1898 d += (ldepth - depth) * 2;
1899 return make_pair(d, string(d, ' '));
1905 string const Buffer::asciiParagraph(Paragraph const * par,
1906 unsigned int linelen,
1907 bool noparbreak) const
1909 ostringstream buffer;
1910 Paragraph::depth_type depth = 0;
1912 Paragraph::depth_type ltype_depth = 0;
1913 bool ref_printed = false;
1914 // if (!par->previous()) {
1916 // begins or ends a deeper area ?
1917 if (depth != par->params().depth()) {
1918 if (par->params().depth() > depth) {
1919 while (par->params().depth() > depth) {
1923 while (par->params().depth() < depth) {
1929 depth = par->params().depth();
1932 // First write the layout
1933 string const & tmp = par->layout();
1934 if (compare_no_case(tmp, "itemize") == 0) {
1936 ltype_depth = depth + 1;
1937 } else if (compare_no_case(tmp, "enumerate") == 0) {
1939 ltype_depth = depth + 1;
1940 } else if (contains(lowercase(tmp), "ection")) {
1942 ltype_depth = depth + 1;
1943 } else if (contains(lowercase(tmp), "aragraph")) {
1945 ltype_depth = depth + 1;
1946 } else if (compare_no_case(tmp, "description") == 0) {
1948 ltype_depth = depth + 1;
1949 } else if (compare_no_case(tmp, "abstract") == 0) {
1952 } else if (compare_no_case(tmp, "bibliography") == 0) {
1960 /* maybe some vertical spaces */
1962 /* the labelwidthstring used in lists */
1966 /* some pagebreaks? */
1970 /* what about the alignment */
1972 // lyxerr << "Should this ever happen?" << endl;
1975 // linelen <= 0 is special and means we don't have pargraph breaks
1977 string::size_type currlinelen = 0;
1983 buffer << string(depth * 2, ' ');
1984 currlinelen += depth * 2;
1987 // we should probably change to the paragraph language in the
1988 // gettext here (if possible) so that strings are outputted in
1989 // the correct language! (20012712 Jug)
1993 case 4: // (Sub)Paragraph
1994 case 5: // Description
1998 buffer << _("Abstract") << "\n\n";
2001 string const abst = _("Abstract: ");
2003 currlinelen += abst.length();
2006 case 7: // Bibliography
2009 buffer << _("References") << "\n\n";
2012 string const refs = _("References: ");
2014 currlinelen += refs.length();
2022 string const parlab = par->params().labelString();
2023 buffer << parlab << " ";
2024 currlinelen += parlab.length() + 1;
2032 pair<int, string> p = addDepth(depth, ltype_depth);
2034 currlinelen += p.first;
2037 // this is to change the linebreak to do it by word a bit more
2038 // intelligent hopefully! (only in the case where we have a
2039 // max linelenght!) (Jug)
2043 for (pos_type i = 0; i < par->size(); ++i) {
2044 char c = par->getUChar(params, i);
2046 case Paragraph::META_INSET:
2048 Inset const * inset = par->getInset(i);
2052 currlinelen += word.length();
2055 if (inset->ascii(this, buffer, linelen)) {
2056 // to be sure it breaks paragraph
2057 currlinelen += linelen;
2063 case Paragraph::META_NEWLINE:
2065 buffer << word << "\n";
2068 pair<int, string> p = addDepth(depth,
2071 currlinelen = p.first;
2075 case Paragraph::META_HFILL:
2076 buffer << word << "\t";
2077 currlinelen += word.length() + 1;
2084 currlinelen + word.length() > linelen - 10) {
2086 pair<int, string> p =
2087 addDepth(depth, ltype_depth);
2089 currlinelen = p.first;
2092 buffer << word << ' ';
2093 currlinelen += word.length() + 1;
2100 lyxerr[Debug::INFO] <<
2101 "writeAsciiFile: NULL char in structure." << endl;
2103 if ((linelen > 0) &&
2104 (currlinelen + word.length()) > linelen)
2108 pair<int, string> p =
2109 addDepth(depth, ltype_depth);
2111 currlinelen = p.first;
2118 return buffer.str().c_str();
2122 void Buffer::writeFileAscii(string const & fname, int linelen)
2124 ofstream ofs(fname.c_str());
2126 Alert::err_alert(_("Error: Cannot write file:"), fname);
2129 writeFileAscii(ofs, linelen);
2133 void Buffer::writeFileAscii(ostream & ofs, int linelen)
2135 Paragraph * par = paragraph;
2137 ofs << asciiParagraph(par, linelen, par->previous() == 0);
2147 void Buffer::makeLaTeXFile(string const & fname,
2148 string const & original_path,
2149 bool nice, bool only_body)
2151 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
2153 ofstream ofs(fname.c_str());
2155 Alert::err_alert(_("Error: Cannot open file: "), fname);
2159 niceFile = nice; // this will be used by Insetincludes.
2161 tex_code_break_column = lyxrc.ascii_linelen;
2163 // validate the buffer.
2164 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
2165 LaTeXFeatures features(params);
2167 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
2170 // The starting paragraph of the coming rows is the
2171 // first paragraph of the document. (Asger)
2172 texrow.start(paragraph, 0);
2174 if (!only_body && nice) {
2175 ofs << "%% " << lyx_docversion << " created this file. "
2176 "For more info, see http://www.lyx.org/.\n"
2177 "%% Do not edit unless you really know what "
2182 lyxerr[Debug::INFO] << "lyx header finished" << endl;
2183 // There are a few differences between nice LaTeX and usual files:
2184 // usual is \batchmode and has a
2185 // special input@path to allow the including of figures
2186 // with either \input or \includegraphics (what figinsets do).
2187 // batchmode is not set if there is a tex_code_break_column.
2188 // In this case somebody is interested in the generated LaTeX,
2189 // so this is OK. input@path is set when the actual parameter
2190 // original_path is set. This is done for usual tex-file, but not
2191 // for nice-latex-file. (Matthias 250696)
2194 // code for usual, NOT nice-latex-file
2195 ofs << "\\batchmode\n"; // changed
2196 // from \nonstopmode
2199 if (!original_path.empty()) {
2200 string inputpath = os::external_path(original_path);
2201 subst(inputpath, "~", "\\string~");
2202 ofs << "\\makeatletter\n"
2203 << "\\def\\input@path{{"
2204 << inputpath << "/}}\n"
2205 << "\\makeatother\n";
2211 ofs << "\\documentclass";
2213 LyXTextClass const & tclass = textclasslist[params.textclass];
2215 ostringstream options; // the document class options.
2217 if (tokenPos(tclass.opt_fontsize(),
2218 '|', params.fontsize) >= 0) {
2219 // only write if existing in list (and not default)
2220 options << params.fontsize << "pt,";
2224 if (!params.use_geometry &&
2225 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2226 switch (params.papersize) {
2227 case BufferParams::PAPER_A4PAPER:
2228 options << "a4paper,";
2230 case BufferParams::PAPER_USLETTER:
2231 options << "letterpaper,";
2233 case BufferParams::PAPER_A5PAPER:
2234 options << "a5paper,";
2236 case BufferParams::PAPER_B5PAPER:
2237 options << "b5paper,";
2239 case BufferParams::PAPER_EXECUTIVEPAPER:
2240 options << "executivepaper,";
2242 case BufferParams::PAPER_LEGALPAPER:
2243 options << "legalpaper,";
2249 if (params.sides != tclass.sides()) {
2250 switch (params.sides) {
2251 case LyXTextClass::OneSide:
2252 options << "oneside,";
2254 case LyXTextClass::TwoSides:
2255 options << "twoside,";
2261 if (params.columns != tclass.columns()) {
2262 if (params.columns == 2)
2263 options << "twocolumn,";
2265 options << "onecolumn,";
2268 if (!params.use_geometry
2269 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2270 options << "landscape,";
2272 // language should be a parameter to \documentclass
2274 ostringstream language_options;
2275 if (params.language->babel() == "hebrew"
2276 && default_language->babel() != "hebrew")
2277 // This seems necessary
2278 features.useLanguage(default_language);
2280 if (lyxrc.language_use_babel ||
2281 params.language->lang() != lyxrc.default_language ||
2282 features.hasLanguages()) {
2284 language_options << features.getLanguages();
2285 language_options << params.language->babel();
2286 if (lyxrc.language_global_options)
2287 options << language_options.str() << ',';
2290 // the user-defined options
2291 if (!params.options.empty()) {
2292 options << params.options << ',';
2295 string strOptions(options.str().c_str());
2296 if (!strOptions.empty()) {
2297 strOptions = strip(strOptions, ',');
2298 ofs << '[' << strOptions << ']';
2301 ofs << '{' << tclass.latexname() << "}\n";
2303 // end of \documentclass defs
2305 // font selection must be done before loading fontenc.sty
2306 // The ae package is not needed when using OT1 font encoding.
2307 if (params.fonts != "default" &&
2308 (params.fonts != "ae" || lyxrc.fontenc != "default")) {
2309 ofs << "\\usepackage{" << params.fonts << "}\n";
2311 if (params.fonts == "ae") {
2312 ofs << "\\usepackage{aecompl}\n";
2316 // this one is not per buffer
2317 if (lyxrc.fontenc != "default") {
2318 ofs << "\\usepackage[" << lyxrc.fontenc
2323 if (params.inputenc == "auto") {
2324 string const doc_encoding =
2325 params.language->encoding()->LatexName();
2327 // Create a list with all the input encodings used
2329 set<string> encodings = features.getEncodingSet(doc_encoding);
2331 ofs << "\\usepackage[";
2332 std::copy(encodings.begin(), encodings.end(),
2333 std::ostream_iterator<string>(ofs, ","));
2334 ofs << doc_encoding << "]{inputenc}\n";
2336 } else if (params.inputenc != "default") {
2337 ofs << "\\usepackage[" << params.inputenc
2342 // At the very beginning the text parameters.
2343 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2344 switch (params.paperpackage) {
2345 case BufferParams::PACKAGE_A4:
2346 ofs << "\\usepackage{a4}\n";
2349 case BufferParams::PACKAGE_A4WIDE:
2350 ofs << "\\usepackage{a4wide}\n";
2353 case BufferParams::PACKAGE_WIDEMARGINSA4:
2354 ofs << "\\usepackage[widemargins]{a4}\n";
2359 if (params.use_geometry) {
2360 ofs << "\\usepackage{geometry}\n";
2362 ofs << "\\geometry{verbose";
2363 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2364 ofs << ",landscape";
2365 switch (params.papersize2) {
2366 case BufferParams::VM_PAPER_CUSTOM:
2367 if (!params.paperwidth.empty())
2368 ofs << ",paperwidth="
2369 << params.paperwidth;
2370 if (!params.paperheight.empty())
2371 ofs << ",paperheight="
2372 << params.paperheight;
2374 case BufferParams::VM_PAPER_USLETTER:
2375 ofs << ",letterpaper";
2377 case BufferParams::VM_PAPER_USLEGAL:
2378 ofs << ",legalpaper";
2380 case BufferParams::VM_PAPER_USEXECUTIVE:
2381 ofs << ",executivepaper";
2383 case BufferParams::VM_PAPER_A3:
2386 case BufferParams::VM_PAPER_A4:
2389 case BufferParams::VM_PAPER_A5:
2392 case BufferParams::VM_PAPER_B3:
2395 case BufferParams::VM_PAPER_B4:
2398 case BufferParams::VM_PAPER_B5:
2402 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2403 switch (lyxrc.default_papersize) {
2404 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2405 case BufferParams::PAPER_USLETTER:
2406 ofs << ",letterpaper";
2408 case BufferParams::PAPER_LEGALPAPER:
2409 ofs << ",legalpaper";
2411 case BufferParams::PAPER_EXECUTIVEPAPER:
2412 ofs << ",executivepaper";
2414 case BufferParams::PAPER_A3PAPER:
2417 case BufferParams::PAPER_A4PAPER:
2420 case BufferParams::PAPER_A5PAPER:
2423 case BufferParams::PAPER_B5PAPER:
2428 if (!params.topmargin.empty())
2429 ofs << ",tmargin=" << params.topmargin;
2430 if (!params.bottommargin.empty())
2431 ofs << ",bmargin=" << params.bottommargin;
2432 if (!params.leftmargin.empty())
2433 ofs << ",lmargin=" << params.leftmargin;
2434 if (!params.rightmargin.empty())
2435 ofs << ",rmargin=" << params.rightmargin;
2436 if (!params.headheight.empty())
2437 ofs << ",headheight=" << params.headheight;
2438 if (!params.headsep.empty())
2439 ofs << ",headsep=" << params.headsep;
2440 if (!params.footskip.empty())
2441 ofs << ",footskip=" << params.footskip;
2446 if (tokenPos(tclass.opt_pagestyle(),
2447 '|', params.pagestyle) >= 0) {
2448 if (params.pagestyle == "fancy") {
2449 ofs << "\\usepackage{fancyhdr}\n";
2452 ofs << "\\pagestyle{" << params.pagestyle << "}\n";
2456 if (params.secnumdepth != tclass.secnumdepth()) {
2457 ofs << "\\setcounter{secnumdepth}{"
2458 << params.secnumdepth
2462 if (params.tocdepth != tclass.tocdepth()) {
2463 ofs << "\\setcounter{tocdepth}{"
2469 if (params.paragraph_separation) {
2470 switch (params.defskip.kind()) {
2471 case VSpace::SMALLSKIP:
2472 ofs << "\\setlength\\parskip{\\smallskipamount}\n";
2474 case VSpace::MEDSKIP:
2475 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2477 case VSpace::BIGSKIP:
2478 ofs << "\\setlength\\parskip{\\bigskipamount}\n";
2480 case VSpace::LENGTH:
2481 ofs << "\\setlength\\parskip{"
2482 << params.defskip.length().asLatexString()
2485 default: // should never happen // Then delete it.
2486 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2491 ofs << "\\setlength\\parindent{0pt}\n";
2495 // Now insert the LyX specific LaTeX commands...
2497 // The optional packages;
2498 string preamble(features.getPackages());
2500 // this might be useful...
2501 preamble += "\n\\makeatletter\n";
2503 // Some macros LyX will need
2504 string tmppreamble(features.getMacros());
2506 if (!tmppreamble.empty()) {
2507 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2508 "LyX specific LaTeX commands.\n"
2509 + tmppreamble + '\n';
2512 // the text class specific preamble
2513 tmppreamble = features.getTClassPreamble();
2514 if (!tmppreamble.empty()) {
2515 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2516 "Textclass specific LaTeX commands.\n"
2517 + tmppreamble + '\n';
2520 /* the user-defined preamble */
2521 if (!params.preamble.empty()) {
2522 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2523 "User specified LaTeX commands.\n"
2524 + params.preamble + '\n';
2527 // Itemize bullet settings need to be last in case the user
2528 // defines their own bullets that use a package included
2529 // in the user-defined preamble -- ARRae
2530 // Actually it has to be done much later than that
2531 // since some packages like frenchb make modifications
2532 // at \begin{document} time -- JMarc
2534 for (int i = 0; i < 4; ++i) {
2535 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2536 if (bullets_def.empty())
2537 bullets_def="\\AtBeginDocument{\n";
2538 bullets_def += " \\renewcommand{\\labelitemi";
2540 // `i' is one less than the item to modify
2547 bullets_def += "ii";
2553 bullets_def += "}{" +
2554 params.user_defined_bullets[i].getText()
2559 if (!bullets_def.empty())
2560 preamble += bullets_def + "}\n\n";
2563 int(lyx::count(preamble.begin(), preamble.end(), '\n'));
2564 for (int j = 0; j != nlines; ++j) {
2568 // We try to load babel late, in case it interferes
2569 // with other packages.
2571 string tmp = lyxrc.language_package;
2572 if (!lyxrc.language_global_options
2573 && tmp == "\\usepackage{babel}")
2574 tmp = string("\\usepackage[") +
2575 language_options.str().c_str() +
2577 preamble += tmp + "\n";
2578 preamble += features.getBabelOptions();
2581 preamble += "\\makeatother\n";
2586 ofs << "\\begin{document}\n";
2589 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
2591 if (!lyxrc.language_auto_begin) {
2592 ofs << subst(lyxrc.language_command_begin, "$$lang",
2593 params.language->babel())
2598 latexParagraphs(ofs, paragraph, 0, texrow);
2600 // add this just in case after all the paragraphs
2604 if (!lyxrc.language_auto_end) {
2605 ofs << subst(lyxrc.language_command_end, "$$lang",
2606 params.language->babel())
2612 ofs << "\\end{document}\n";
2615 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2617 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2621 // Just to be sure. (Asger)
2624 // tex_code_break_column's value is used to decide
2625 // if we are in batchmode or not (within mathed_write()
2626 // in math_write.C) so we must set it to a non-zero
2627 // value when we leave otherwise we save incorrect .lyx files.
2628 tex_code_break_column = lyxrc.ascii_linelen;
2632 lyxerr << "File was not closed properly." << endl;
2635 lyxerr[Debug::INFO] << "Finished making latex file." << endl;
2636 lyxerr[Debug::INFO] << "Row count was " << texrow.rows()-1 << "." << endl;
2638 // we want this to be true outside previews (for insetexternal)
2644 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
2646 void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
2647 Paragraph * endpar, TexRow & texrow) const
2649 bool was_title = false;
2650 bool already_title = false;
2653 while (par != endpar) {
2654 Inset * in = par->inInset();
2655 // well we have to check if we are in an inset with unlimited
2656 // lenght (all in one row) if that is true then we don't allow
2657 // any special options in the paragraph and also we don't allow
2658 // any environment other then "Standard" to be valid!
2659 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
2660 LyXLayout const & layout =
2661 textclasslist[params.textclass][par->layout()];
2663 if (layout.intitle) {
2664 if (already_title) {
2665 lyxerr <<"Error in latexParagraphs: You"
2666 " should not mix title layouts"
2667 " with normal ones." << endl;
2670 } else if (was_title && !already_title) {
2671 ofs << "\\maketitle\n";
2673 already_title = true;
2677 if (layout.isEnvironment() ||
2678 !par->params().leftIndent().zero())
2680 par = par->TeXEnvironment(this, params, ofs, texrow);
2682 par = par->TeXOnePar(this, params, ofs, texrow, false);
2685 par = par->TeXOnePar(this, params, ofs, texrow, false);
2688 // It might be that we only have a title in this document
2689 if (was_title && !already_title) {
2690 ofs << "\\maketitle\n";
2696 bool Buffer::isLatex() const
2698 return textclasslist[params.textclass].outputType() == LATEX;
2702 bool Buffer::isLinuxDoc() const
2704 return textclasslist[params.textclass].outputType() == LINUXDOC;
2708 bool Buffer::isLiterate() const
2710 return textclasslist[params.textclass].outputType() == LITERATE;
2714 bool Buffer::isDocBook() const
2716 return textclasslist[params.textclass].outputType() == DOCBOOK;
2720 bool Buffer::isSGML() const
2722 LyXTextClass const & tclass = textclasslist[params.textclass];
2724 return tclass.outputType() == LINUXDOC ||
2725 tclass.outputType() == DOCBOOK;
2729 void Buffer::sgmlOpenTag(ostream & os, Paragraph::depth_type,
2730 string const & latexname) const
2732 if (!latexname.empty() && latexname != "!-- --")
2733 //os << "<!-- " << depth << " -->" << "<" << latexname << ">";
2734 os << "<" << latexname << ">";
2738 void Buffer::sgmlCloseTag(ostream & os, Paragraph::depth_type,
2739 string const & latexname) const
2741 if (!latexname.empty() && latexname != "!-- --")
2742 //os << "<!-- " << depth << " -->" << "</" << latexname << ">\n";
2743 os << "</" << latexname << ">";
2747 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
2749 ofstream ofs(fname.c_str());
2752 Alert::alert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2756 niceFile = nice; // this will be used by included files.
2758 LaTeXFeatures features(params);
2764 LyXTextClass const & tclass = textclasslist[params.textclass];
2766 string top_element = tclass.latexname();
2769 ofs << "<!doctype linuxdoc system";
2771 string preamble = params.preamble;
2772 const string name = nice ? ChangeExtension(filename_, ".sgml")
2774 preamble += features.getIncludedFiles(name);
2775 preamble += features.getLyXSGMLEntities();
2777 if (!preamble.empty()) {
2778 ofs << " [ " << preamble << " ]";
2782 if (params.options.empty())
2783 sgmlOpenTag(ofs, 0, top_element);
2785 string top = top_element;
2787 top += params.options;
2788 sgmlOpenTag(ofs, 0, top);
2792 ofs << "<!-- " << lyx_docversion
2793 << " created this file. For more info see http://www.lyx.org/"
2796 Paragraph::depth_type depth = 0; // paragraph depth
2797 Paragraph * par = paragraph;
2799 vector<string> environment_stack(5);
2802 LyXLayout const & style = tclass[par->layout()];
2804 // treat <toc> as a special case for compatibility with old code
2805 if (par->isInset(0)) {
2806 Inset * inset = par->getInset(0);
2807 Inset::Code lyx_code = inset->lyxCode();
2808 if (lyx_code == Inset::TOC_CODE) {
2809 string const temp = "toc";
2810 sgmlOpenTag(ofs, depth, temp);
2817 // environment tag closing
2818 for (; depth > par->params().depth(); --depth) {
2819 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2820 environment_stack[depth].erase();
2823 // write opening SGML tags
2824 switch (style.latextype) {
2825 case LATEX_PARAGRAPH:
2826 if (depth == par->params().depth()
2827 && !environment_stack[depth].empty()) {
2828 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2829 environment_stack[depth].erase();
2835 sgmlOpenTag(ofs, depth, style.latexname());
2841 _("Error : Wrong depth for"
2842 " LatexType Command.\n"));
2844 if (!environment_stack[depth].empty()) {
2845 sgmlCloseTag(ofs, depth,
2846 environment_stack[depth]);
2850 environment_stack[depth].erase();
2851 sgmlOpenTag(ofs, depth, style.latexname());
2854 case LATEX_ENVIRONMENT:
2855 case LATEX_ITEM_ENVIRONMENT:
2856 if (depth == par->params().depth()
2857 && environment_stack[depth] != style.latexname()) {
2858 sgmlCloseTag(ofs, depth,
2859 environment_stack[depth]);
2860 environment_stack[depth].erase();
2862 if (depth < par->params().depth()) {
2863 depth = par->params().depth();
2864 environment_stack[depth].erase();
2866 if (environment_stack[depth] != style.latexname()) {
2868 sgmlOpenTag(ofs, depth, "p");
2870 sgmlOpenTag(ofs, depth, style.latexname());
2872 if (environment_stack.size() == depth + 1)
2873 environment_stack.push_back("!-- --");
2874 environment_stack[depth] = style.latexname();
2877 if (style.latexparam() == "CDATA")
2880 if (style.latextype == LATEX_ENVIRONMENT) break;
2882 if (style.labeltype == LABEL_MANUAL)
2887 sgmlOpenTag(ofs, depth + 1, item_name);
2890 sgmlOpenTag(ofs, depth, style.latexname());
2894 simpleLinuxDocOnePar(ofs, par, depth);
2899 // write closing SGML tags
2900 switch (style.latextype) {
2903 case LATEX_ENVIRONMENT:
2904 case LATEX_ITEM_ENVIRONMENT:
2905 if (style.latexparam() == "CDATA")
2909 sgmlCloseTag(ofs, depth, style.latexname());
2915 for (int i=depth; i >= 0; --i)
2916 sgmlCloseTag(ofs, depth, environment_stack[i]);
2920 sgmlCloseTag(ofs, 0, top_element);
2924 // How to check for successful close
2926 // we want this to be true outside previews (for insetexternal)
2931 // checks, if newcol chars should be put into this line
2932 // writes newline, if necessary.
2935 void sgmlLineBreak(ostream & os, string::size_type & colcount,
2936 string::size_type newcol)
2939 if (colcount > lyxrc.ascii_linelen) {
2941 colcount = newcol; // assume write after this call
2956 string tag_name(PAR_TAG const & pt) {
2958 case NONE: return "!-- --";
2959 case TT: return "tt";
2960 case SF: return "sf";
2961 case BF: return "bf";
2962 case IT: return "it";
2963 case SL: return "sl";
2964 case EM: return "em";
2971 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2973 p1 = static_cast<PAR_TAG>(p1 | p2);
2978 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2980 p1 = static_cast<PAR_TAG>(p1 & ~p2);
2986 // Handle internal paragraph parsing -- layout already processed.
2987 void Buffer::simpleLinuxDocOnePar(ostream & os,
2989 Paragraph::depth_type /*depth*/)
2991 LyXLayout const & style =
2992 textclasslist[params.textclass][par->layout()];
2993 string::size_type char_line_count = 5; // Heuristic choice ;-)
2995 // gets paragraph main font
2998 if (style.labeltype == LABEL_MANUAL) {
2999 font_old = style.labelfont;
3002 font_old = style.font;
3006 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
3007 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
3008 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
3011 stack<PAR_TAG> tag_state;
3012 // parsing main loop
3013 for (pos_type i = 0; i < par->size(); ++i) {
3015 PAR_TAG tag_close = NONE;
3016 list < PAR_TAG > tag_open;
3018 LyXFont const font = par->getFont(params, i);
3020 if (font_old.family() != font.family()) {
3021 switch (family_type) {
3022 case LyXFont::SANS_FAMILY:
3025 case LyXFont::TYPEWRITER_FAMILY:
3032 family_type = font.family();
3034 switch (family_type) {
3035 case LyXFont::SANS_FAMILY:
3036 tag_open.push_back(SF);
3038 case LyXFont::TYPEWRITER_FAMILY:
3039 tag_open.push_back(TT);
3046 if (font_old.series() != font.series()) {
3047 switch (series_type) {
3048 case LyXFont::BOLD_SERIES:
3055 series_type = font.series();
3057 switch (series_type) {
3058 case LyXFont::BOLD_SERIES:
3059 tag_open.push_back(BF);
3067 if (font_old.shape() != font.shape()) {
3068 switch (shape_type) {
3069 case LyXFont::ITALIC_SHAPE:
3072 case LyXFont::SLANTED_SHAPE:
3079 shape_type = font.shape();
3081 switch (shape_type) {
3082 case LyXFont::ITALIC_SHAPE:
3083 tag_open.push_back(IT);
3085 case LyXFont::SLANTED_SHAPE:
3086 tag_open.push_back(SL);
3093 if (font_old.emph() != font.emph()) {
3094 if (font.emph() == LyXFont::ON) {
3095 tag_open.push_back(EM);
3104 list < PAR_TAG > temp;
3105 while (!tag_state.empty() && tag_close) {
3106 PAR_TAG k = tag_state.top();
3108 os << "</" << tag_name(k) << ">";
3115 for(list< PAR_TAG >::const_iterator j = temp.begin();
3116 j != temp.end(); ++j) {
3118 os << "<" << tag_name(*j) << ">";
3121 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
3122 j != tag_open.end(); ++j) {
3124 os << "<" << tag_name(*j) << ">";
3127 char c = par->getChar(i);
3129 if (c == Paragraph::META_INSET) {
3130 Inset * inset = par->getInset(i);
3131 inset->linuxdoc(this, os);
3136 if (style.latexparam() == "CDATA") {
3137 // "TeX"-Mode on == > SGML-Mode on.
3143 if (par->sgmlConvertChar(c, sgml_string)
3144 && !style.free_spacing && !par->isFreeSpacing())
3146 // in freespacing mode, spaces are
3147 // non-breaking characters
3148 if (desc_on) {// if char is ' ' then...
3151 sgmlLineBreak(os, char_line_count, 6);
3155 sgmlLineBreak(os, char_line_count, 1);
3160 char_line_count += sgml_string.length();
3166 while (!tag_state.empty()) {
3167 os << "</" << tag_name(tag_state.top()) << ">";
3171 // resets description flag correctly
3173 // <tag> not closed...
3174 sgmlLineBreak(os, char_line_count, 6);
3180 // Print an error message.
3181 void Buffer::sgmlError(Paragraph * /*par*/, int /*pos*/,
3182 string const & /*message*/) const
3184 #warning This is wrong we cannot insert an inset like this!!!
3185 // I guess this was Jose' so I explain you more or less why this
3186 // is wrong. This way you insert something in the paragraph and
3187 // don't tell it to LyXText (row rebreaking and undo handling!!!)
3188 // I deactivate this code, have a look at BufferView::insertErrors
3189 // how you should do this correctly! (Jug 20020315)
3191 // insert an error marker in text
3192 InsetError * new_inset = new InsetError(message);
3193 par->insertInset(pos, new_inset, LyXFont(LyXFont::ALL_INHERIT,
3199 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
3201 ofstream ofs(fname.c_str());
3203 Alert::alert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3207 Paragraph * par = paragraph;
3209 niceFile = nice; // this will be used by Insetincludes.
3211 LaTeXFeatures features(params);
3216 LyXTextClass const & tclass = textclasslist[params.textclass];
3217 string top_element = tclass.latexname();
3220 ofs << "<!DOCTYPE " << top_element
3221 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
3223 string preamble = params.preamble;
3224 const string name = nice ? ChangeExtension(filename_, ".sgml")
3226 preamble += features.getIncludedFiles(name);
3227 preamble += features.getLyXSGMLEntities();
3229 if (!preamble.empty()) {
3230 ofs << "\n [ " << preamble << " ]";
3235 string top = top_element;
3237 top += params.language->code();
3240 if (!params.options.empty()) {
3242 top += params.options;
3244 sgmlOpenTag(ofs, 0, top);
3246 ofs << "<!-- DocBook file was created by " << lyx_docversion
3247 << "\n See http://www.lyx.org/ for more information -->\n";
3249 vector<string> environment_stack(10);
3250 vector<string> environment_inner(10);
3251 vector<string> command_stack(10);
3253 bool command_flag = false;
3254 Paragraph::depth_type command_depth = 0;
3255 Paragraph::depth_type command_base = 0;
3256 Paragraph::depth_type cmd_depth = 0;
3257 Paragraph::depth_type depth = 0; // paragraph depth
3260 string command_name;
3266 int desc_on = 0; // description mode
3268 LyXLayout const & style = tclass[par->layout()];
3270 // environment tag closing
3271 for (; depth > par->params().depth(); --depth) {
3272 if (environment_inner[depth] != "!-- --") {
3273 item_name = "listitem";
3274 sgmlCloseTag(ofs, command_depth + depth,
3276 if (environment_inner[depth] == "varlistentry")
3277 sgmlCloseTag(ofs, depth+command_depth,
3278 environment_inner[depth]);
3280 sgmlCloseTag(ofs, depth + command_depth,
3281 environment_stack[depth]);
3282 environment_stack[depth].erase();
3283 environment_inner[depth].erase();
3286 if (depth == par->params().depth()
3287 && environment_stack[depth] != style.latexname()
3288 && !environment_stack[depth].empty()) {
3289 if (environment_inner[depth] != "!-- --") {
3290 item_name= "listitem";
3291 sgmlCloseTag(ofs, command_depth+depth,
3293 if (environment_inner[depth] == "varlistentry")
3295 depth + command_depth,
3296 environment_inner[depth]);
3299 sgmlCloseTag(ofs, depth + command_depth,
3300 environment_stack[depth]);
3302 environment_stack[depth].erase();
3303 environment_inner[depth].erase();
3306 // Write opening SGML tags.
3307 switch (style.latextype) {
3308 case LATEX_PARAGRAPH:
3309 sgmlOpenTag(ofs, depth + command_depth,
3316 _("Error : Wrong depth for "
3317 "LatexType Command.\n"));
3319 command_name = style.latexname();
3321 sgmlparam = style.latexparam();
3322 c_params = split(sgmlparam, c_depth,'|');
3324 cmd_depth = lyx::atoi(c_depth);
3327 if (cmd_depth < command_base) {
3328 for (Paragraph::depth_type j = command_depth;
3329 j >= command_base; --j) {
3330 sgmlCloseTag(ofs, j, command_stack[j]);
3333 command_depth = command_base = cmd_depth;
3334 } else if (cmd_depth <= command_depth) {
3335 for (int j = command_depth;
3336 j >= int(cmd_depth); --j) {
3337 sgmlCloseTag(ofs, j, command_stack[j]);
3340 command_depth = cmd_depth;
3342 command_depth = cmd_depth;
3344 command_depth = command_base = cmd_depth;
3345 command_flag = true;
3347 if (command_stack.size() == command_depth + 1)
3348 command_stack.push_back(string());
3349 command_stack[command_depth] = command_name;
3351 // treat label as a special case for
3352 // more WYSIWYM handling.
3353 // This is a hack while paragraphs can't have
3354 // attributes, like id in this case.
3355 if (par->isInset(0)) {
3356 Inset * inset = par->getInset(0);
3357 Inset::Code lyx_code = inset->lyxCode();
3358 if (lyx_code == Inset::LABEL_CODE) {
3359 command_name += " id=\"";
3360 command_name += (static_cast<InsetCommand *>(inset))->getContents();
3361 command_name += "\"";
3366 sgmlOpenTag(ofs, depth + command_depth, command_name);
3367 if (c_params.empty())
3368 item_name = "title";
3370 item_name = c_params;
3371 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3374 case LATEX_ENVIRONMENT:
3375 case LATEX_ITEM_ENVIRONMENT:
3376 if (depth < par->params().depth()) {
3377 depth = par->params().depth();
3378 environment_stack[depth].erase();
3381 if (environment_stack[depth] != style.latexname()) {
3382 if (environment_stack.size() == depth + 1) {
3383 environment_stack.push_back("!-- --");
3384 environment_inner.push_back("!-- --");
3386 environment_stack[depth] = style.latexname();
3387 environment_inner[depth] = "!-- --";
3388 sgmlOpenTag(ofs, depth + command_depth,
3389 environment_stack[depth]);
3391 if (environment_inner[depth] != "!-- --") {
3392 item_name= "listitem";
3394 command_depth + depth,
3396 if (environment_inner[depth] == "varlistentry")
3398 depth + command_depth,
3399 environment_inner[depth]);
3403 if (style.latextype == LATEX_ENVIRONMENT) {
3404 if (!style.latexparam().empty()) {
3405 if (style.latexparam() == "CDATA")
3408 sgmlOpenTag(ofs, depth + command_depth,
3409 style.latexparam());
3414 desc_on = (style.labeltype == LABEL_MANUAL);
3417 environment_inner[depth]= "varlistentry";
3419 environment_inner[depth]= "listitem";
3421 sgmlOpenTag(ofs, depth + 1 + command_depth,
3422 environment_inner[depth]);
3426 sgmlOpenTag(ofs, depth + 1 + command_depth,
3430 sgmlOpenTag(ofs, depth + 1 + command_depth,
3435 sgmlOpenTag(ofs, depth + command_depth,
3440 simpleDocBookOnePar(ofs, par, desc_on, depth+1+command_depth);
3444 // write closing SGML tags
3445 switch (style.latextype) {
3447 if (c_params.empty())
3451 sgmlCloseTag(ofs, depth + command_depth, end_tag);
3453 case LATEX_ENVIRONMENT:
3454 if (!style.latexparam().empty()) {
3455 if (style.latexparam() == "CDATA")
3458 sgmlCloseTag(ofs, depth + command_depth,
3459 style.latexparam());
3462 case LATEX_ITEM_ENVIRONMENT:
3463 if (desc_on == 1) break;
3465 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3467 case LATEX_PARAGRAPH:
3468 sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3471 sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3477 for (int d = depth; d >= 0; --d) {
3478 if (!environment_stack[depth].empty()) {
3479 if (environment_inner[depth] != "!-- --") {
3480 item_name = "listitem";
3481 sgmlCloseTag(ofs, command_depth + depth,
3483 if (environment_inner[depth] == "varlistentry")
3484 sgmlCloseTag(ofs, depth + command_depth,
3485 environment_inner[depth]);
3488 sgmlCloseTag(ofs, depth + command_depth,
3489 environment_stack[depth]);
3493 for (int j = command_depth; j >= 0 ; --j)
3494 if (!command_stack[j].empty()) {
3495 sgmlCloseTag(ofs, j, command_stack[j]);
3500 sgmlCloseTag(ofs, 0, top_element);
3503 // How to check for successful close
3505 // we want this to be true outside previews (for insetexternal)
3510 void Buffer::simpleDocBookOnePar(ostream & os,
3511 Paragraph * par, int & desc_on,
3512 Paragraph::depth_type depth) const
3514 bool emph_flag = false;
3516 LyXLayout const & style =
3517 textclasslist[params.textclass][par->layout()];
3519 LyXFont font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3521 int char_line_count = depth;
3522 //if (!style.free_spacing)
3523 // os << string(depth,' ');
3525 // parsing main loop
3526 for (pos_type i = 0; i < par->size(); ++i) {
3527 LyXFont font = par->getFont(params, i);
3529 // handle <emphasis> tag
3530 if (font_old.emph() != font.emph()) {
3531 if (font.emph() == LyXFont::ON) {
3532 if(style.latexparam() == "CDATA")
3535 if(style.latexparam() == "CDATA")
3539 if(style.latexparam() == "CDATA")
3541 os << "</emphasis>";
3542 if(style.latexparam() == "CDATA")
3549 if (par->isInset(i)) {
3550 Inset * inset = par->getInset(i);
3551 // don't print the inset in position 0 if desc_on == 3 (label)
3552 if ( i || desc_on != 3) {
3553 if(style.latexparam() == "CDATA")
3555 inset->docbook(this, os);
3556 if(style.latexparam() == "CDATA")
3560 char c = par->getChar(i);
3562 par->sgmlConvertChar(c, sgml_string);
3564 if (style.pass_thru) {
3566 } else if (style.free_spacing || par->isFreeSpacing() || c != ' ') {
3568 } else if (desc_on ==1) {
3570 os << "\n</term><listitem><para>";
3580 if(style.latexparam() == "CDATA")
3582 os << "</emphasis>";
3583 if(style.latexparam() == "CDATA")
3587 // resets description flag correctly
3589 // <term> not closed...
3590 os << "</term>\n<listitem><para> </para>";
3592 if (style.free_spacing) os << '\n';
3596 // This should be enabled when the Chktex class is implemented. (Asger)
3597 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
3598 // Other flags: -wall -v0 -x
3599 int Buffer::runChktex()
3601 if (!users->text) return 0;
3603 users->owner()->prohibitInput();
3605 // get LaTeX-Filename
3606 string const name = getLatexName();
3607 string path = filePath();
3609 string const org_path = path;
3610 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
3614 Path p(path); // path to LaTeX file
3615 users->owner()->message(_("Running chktex..."));
3617 // Remove all error insets
3618 bool const removedErrorInsets = users->removeAutoInsets();
3620 // Generate the LaTeX file if neccessary
3621 makeLaTeXFile(name, org_path, false);
3624 Chktex chktex(lyxrc.chktex_command, name, filePath());
3625 int res = chktex.run(terr); // run chktex
3628 Alert::alert(_("chktex did not work!"),
3629 _("Could not run with file:"), name);
3630 } else if (res > 0) {
3631 // Insert all errors as errors boxes
3632 users->insertErrors(terr);
3635 // if we removed error insets before we ran chktex or if we inserted
3636 // error insets after we ran chktex, this must be run:
3637 if (removedErrorInsets || res) {
3641 users->owner()->allowInput();
3647 void Buffer::validate(LaTeXFeatures & features) const
3649 Paragraph * par = paragraph;
3650 LyXTextClass const & tclass = textclasslist[params.textclass];
3652 // AMS Style is at document level
3653 if (params.use_amsmath || tclass.provides(LyXTextClass::amsmath))
3654 features.require("amsmath");
3657 // We don't use "lyxerr.debug" because of speed. (Asger)
3658 if (lyxerr.debugging(Debug::LATEX))
3659 lyxerr << "Paragraph: " << par << endl;
3661 // Now just follow the list of paragraphs and run
3662 // validate on each of them.
3663 par->validate(features);
3665 // and then the next paragraph
3669 // the bullet shapes are buffer level not paragraph level
3670 // so they are tested here
3671 for (int i = 0; i < 4; ++i) {
3672 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
3673 int const font = params.user_defined_bullets[i].getFont();
3675 int const c = params
3676 .user_defined_bullets[i]
3683 features.require("latexsym");
3685 } else if (font == 1) {
3686 features.require("amssymb");
3687 } else if ((font >= 2 && font <= 5)) {
3688 features.require("pifont");
3693 if (lyxerr.debugging(Debug::LATEX)) {
3694 features.showStruct();
3699 // This function should be in Buffer because it's a buffer's property (ale)
3700 string const Buffer::getIncludeonlyList(char delim)
3703 for (inset_iterator it = inset_iterator_begin();
3704 it != inset_iterator_end(); ++it) {
3705 if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3706 InsetInclude * insetinc =
3707 static_cast<InsetInclude *>(*it);
3708 if (insetinc->isIncludeOnly()) {
3711 lst += insetinc->getRelFileBaseName();
3715 lyxerr[Debug::INFO] << "Includeonly(" << lst << ')' << endl;
3720 vector<string> const Buffer::getLabelList()
3722 /// if this is a child document and the parent is already loaded
3723 /// Use the parent's list instead [ale990407]
3724 if (!params.parentname.empty()
3725 && bufferlist.exists(params.parentname)) {
3726 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3728 return tmp->getLabelList();
3731 vector<string> label_list;
3732 for (inset_iterator it = inset_iterator_begin();
3733 it != inset_iterator_end(); ++it) {
3734 vector<string> const l = (*it)->getLabelList();
3735 label_list.insert(label_list.end(), l.begin(), l.end());
3741 Buffer::Lists const Buffer::getLists() const
3744 Paragraph * par = paragraph;
3746 LyXTextClass const & textclass = textclasslist[params.textclass];
3747 bool found = textclass.hasLayout("Caption");
3748 string const layout("Caption");
3751 char const labeltype = textclass[par->layout()].labeltype;
3753 if (labeltype >= LABEL_COUNTER_CHAPTER
3754 && labeltype <= LABEL_COUNTER_CHAPTER + params.tocdepth) {
3755 // insert this into the table of contents
3756 SingleList & item = l["TOC"];
3759 textclass.maxcounter());
3760 item.push_back(TocItem(par, depth, par->asString(this, true)));
3762 // For each paragrph, traverse its insets and look for
3766 Paragraph::inset_iterator it =
3767 par->inset_iterator_begin();
3768 Paragraph::inset_iterator end =
3769 par->inset_iterator_end();
3771 for (; it != end; ++it) {
3772 if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
3774 static_cast<InsetFloat*>(*it);
3776 string const type = il->type();
3778 // Now find the caption in the float...
3779 // We now tranverse the paragraphs of
3781 Paragraph * tmp = il->inset.paragraph();
3783 if (tmp->layout() == layout) {
3784 SingleList & item = l[type];
3786 tostr(item.size()+1) + ". " + tmp->asString(this, false);
3787 item.push_back(TocItem(tmp, 0 , str));
3794 lyxerr << "Caption not found" << endl;
3803 // This is also a buffer property (ale)
3804 vector<pair<string, string> > const Buffer::getBibkeyList() const
3806 typedef pair<string, string> StringPair;
3807 /// if this is a child document and the parent is already loaded
3808 /// Use the parent's list instead [ale990412]
3809 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
3810 Buffer const * tmp = bufferlist.getBuffer(params.parentname);
3812 return tmp->getBibkeyList();
3815 vector<StringPair> keys;
3816 Paragraph * par = paragraph;
3819 string const key = par->bibkey->getContents();
3820 string const opt = par->bibkey->getOptions();
3821 string const ref = par->asString(this, false);
3822 string const info = opt + "TheBibliographyRef" + ref;
3824 keys.push_back(StringPair(key, info));
3829 // Might be either using bibtex or a child has bibliography
3831 for (inset_iterator it = inset_const_iterator_begin();
3832 it != inset_const_iterator_end(); ++it) {
3833 // Search for Bibtex or Include inset
3834 if ((*it)->lyxCode() == Inset::BIBTEX_CODE) {
3835 vector<StringPair> tmp =
3836 static_cast<InsetBibtex*>(*it)->getKeys(this);
3837 keys.insert(keys.end(), tmp.begin(), tmp.end());
3838 } else if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3839 vector<StringPair> const tmp =
3840 static_cast<InsetInclude*>(*it)->getKeys();
3841 keys.insert(keys.end(), tmp.begin(), tmp.end());
3850 bool Buffer::isDepClean(string const & name) const
3852 DEPCLEAN * item = dep_clean;
3853 while (item && item->master != name)
3855 if (!item) return true;
3860 void Buffer::markDepClean(string const & name)
3863 dep_clean = new DEPCLEAN;
3864 dep_clean->clean = true;
3865 dep_clean->master = name;
3866 dep_clean->next = 0;
3868 DEPCLEAN * item = dep_clean;
3869 while (item && item->master != name)
3874 item = new DEPCLEAN;
3876 item->master = name;
3883 bool Buffer::dispatch(string const & command)
3885 // Split command string into command and argument
3887 string line = frontStrip(command);
3888 string const arg = strip(frontStrip(split(line, cmd, ' ')));
3890 return dispatch(lyxaction.LookupFunc(cmd), arg);
3894 bool Buffer::dispatch(int action, string const & argument)
3896 bool dispatched = true;
3899 Exporter::Export(this, argument, false);
3909 void Buffer::resizeInsets(BufferView * bv)
3911 /// then remove all LyXText in text-insets
3912 Paragraph * par = paragraph;
3913 for (; par; par = par->next()) {
3914 par->resizeInsetsLyXText(bv);
3919 void Buffer::redraw()
3926 void Buffer::changeLanguage(Language const * from, Language const * to)
3929 ParIterator end = par_iterator_end();
3930 for (ParIterator it = par_iterator_begin(); it != end; ++it)
3931 (*it)->changeLanguage(params, from, to);
3935 bool Buffer::isMultiLingual()
3937 ParIterator end = par_iterator_end();
3938 for (ParIterator it = par_iterator_begin(); it != end; ++it)
3939 if ((*it)->isMultiLingual(params))
3946 Buffer::inset_iterator::inset_iterator(Paragraph * paragraph, pos_type pos)
3949 it = par->InsetIterator(pos);
3950 if (it == par->inset_iterator_end()) {
3957 void Buffer::inset_iterator::setParagraph()
3960 it = par->inset_iterator_begin();
3961 if (it != par->inset_iterator_end())
3966 // We maintain an invariant that whenever par = 0 then it = 0
3970 Inset * Buffer::getInsetFromID(int id_arg) const
3972 for (inset_iterator it = inset_const_iterator_begin();
3973 it != inset_const_iterator_end(); ++it)
3975 if ((*it)->id() == id_arg)
3977 Inset * in = (*it)->getInsetFromID(id_arg);
3985 Paragraph * Buffer::getParFromID(int id) const
3987 if (id < 0) return 0;
3988 Paragraph * par = paragraph;
3990 if (par->id() == id) {
3993 Paragraph * tmp = par->getParFromID(id);
4003 ParIterator Buffer::par_iterator_begin()
4005 return ParIterator(paragraph);
4009 ParIterator Buffer::par_iterator_end()
4011 return ParIterator();