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 * ======================================================
26 #include <sys/types.h>
36 #pragma implementation
40 #include "bufferlist.h"
42 #include "lyx_gui_misc.h"
43 #include "LyXAction.h"
46 #include "tex-strings.h"
48 #include "bufferview_funcs.h"
51 #include "mathed/formulamacro.h"
52 #include "mathed/formula.h"
53 #include "insets/inset.h"
54 #include "insets/inseterror.h"
55 #include "insets/insetlabel.h"
56 #include "insets/insetref.h"
57 #include "insets/inseturl.h"
58 #include "insets/insetnote.h"
59 #include "insets/insetquotes.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetbib.h"
62 #include "insets/insetcite.h"
63 #include "insets/insetexternal.h"
64 #include "insets/insetindex.h"
65 #include "insets/insetinclude.h"
66 #include "insets/insettoc.h"
67 #include "insets/insetparent.h"
68 #include "insets/insetspecialchar.h"
69 #include "insets/figinset.h"
70 #include "insets/insettext.h"
71 #include "insets/insetert.h"
72 #include "insets/insetgraphics.h"
73 #include "insets/insetfoot.h"
74 #include "insets/insetmarginal.h"
75 #include "insets/insetminipage.h"
76 #include "insets/insetfloat.h"
77 #include "insets/insettabular.h"
79 #include "insets/insettheorem.h"
80 #include "insets/insetlist.h"
82 #include "insets/insetcaption.h"
83 #include "insets/insetfloatlist.h"
84 #include "support/textutils.h"
85 #include "support/filetools.h"
86 #include "support/path.h"
87 #include "support/os.h"
92 #include "LaTeXFeatures.h"
93 #include "support/syscall.h"
94 #include "support/lyxlib.h"
95 #include "support/FileInfo.h"
96 #include "support/lyxmanip.h"
100 #include "lyx_gui_misc.h" // WarnReadonly()
101 #include "frontends/Dialogs.h"
102 #include "encoding.h"
103 #include "exporter.h"
104 #include "Lsstream.h"
105 #include "converter.h"
106 #include "BufferView.h"
107 #include "ParagraphParameters.h"
117 using std::make_pair;
125 // all these externs should eventually be removed.
126 extern BufferList bufferlist;
128 extern LyXAction lyxaction;
132 const int LYX_FORMAT = 220;
136 extern int tex_code_break_column;
139 Buffer::Buffer(string const & file, bool ronly)
141 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
143 filepath = OnlyPath(file);
152 if (read_only || (lyxrc.use_tempdir)) {
153 tmppath = CreateBufferTmpDir();
154 } else tmppath.erase();
160 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
161 // here the buffer should take care that it is
162 // saved properly, before it goes into the void.
164 // make sure that views using this buffer
169 if (!tmppath.empty()) {
170 DestroyBufferTmpDir(tmppath);
173 Paragraph * par = paragraph;
176 tmppar = par->next();
184 string const Buffer::getLatexName(bool no_path) const
186 string name = ChangeExtension(MakeLatexName(filename), ".tex");
188 return OnlyFilename(name);
194 pair<Buffer::LogType, string> const Buffer::getLogName(void) const
196 string const filename = getLatexName(false);
198 if (filename.empty())
199 return make_pair(Buffer::latexlog, string());
201 string path = OnlyPath(filename);
203 if (lyxrc.use_tempdir || !IsDirWriteable(path))
206 string const fname = AddName(path,
207 OnlyFilename(ChangeExtension(filename,
210 AddName(path, OnlyFilename(
211 ChangeExtension(filename,
212 formats.extension("literate") + ".out")));
214 // If no Latex log or Build log is newer, show Build log
216 FileInfo const f_fi(fname);
217 FileInfo const b_fi(bname);
220 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
221 lyxerr[Debug::FILES] << "Log name calculated as : " << bname << endl;
222 return make_pair(Buffer::buildlog, bname);
224 lyxerr[Debug::FILES] << "Log name calculated as : " << fname << endl;
225 return make_pair(Buffer::latexlog, fname);
229 void Buffer::setReadonly(bool flag)
231 if (read_only != flag) {
234 users->owner()->getDialogs()->updateBufferDependent(false);
237 WarnReadonly(filename);
242 bool Buffer::saveParamsAsDefaults() // const
244 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
246 Buffer defaults = Buffer(fname);
248 // Use the current buffer's parameters as default
249 defaults.params = params;
251 // add an empty paragraph. Is this enough?
252 defaults.paragraph = new Paragraph;
254 return defaults.writeFile(defaults.filename, false);
258 /// Update window titles of all users
259 // Should work on a list
260 void Buffer::updateTitles() const
262 if (users) users->owner()->updateWindowTitle();
266 /// Reset autosave timer of all users
267 // Should work on a list
268 void Buffer::resetAutosaveTimers() const
270 if (users) users->owner()->resetAutosaveTimer();
274 void Buffer::setFileName(string const & newfile)
276 filename = MakeAbsPath(newfile);
277 filepath = OnlyPath(filename);
278 setReadonly(IsFileWriteable(filename) == 0);
283 // We'll remove this later. (Lgb)
286 string last_inset_read;
288 #ifndef NO_COMPABILITY
291 ErtComp() : active(false), in_tabular(false) {
298 std::stack<ErtComp> ert_stack;
305 #warning And _why_ is this here? (Lgb)
308 // candidate for move to BufferView
309 // (at least some parts in the beginning of the func)
312 // changed to be public and have one parameter
313 // if par = 0 normal behavior
314 // else insert behavior
315 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
316 bool Buffer::readLyXformat2(LyXLex & lex, Paragraph * par)
319 #ifndef NO_COMPABILITY
320 ert_comp.contents.erase();
321 ert_comp.active = false;
322 ert_comp.in_tabular = false;
325 Paragraph::depth_type depth = 0;
326 bool the_end_read = false;
328 Paragraph * first_par = 0;
329 LyXFont font(LyXFont::ALL_INHERIT, params.language);
330 if (file_format < 216 && params.language->lang() == "hebrew")
331 font.setLanguage(default_language);
336 // We are inserting into an existing document
337 users->text->breakParagraph(users);
338 first_par = users->text->firstParagraph();
341 // We don't want to adopt the parameters from the
342 // document we insert, so we skip until the text begins:
345 string const pretoken = lex.getString();
346 if (pretoken == "\\layout") {
347 lex.pushToken(pretoken);
355 string const token = lex.getString();
357 if (token.empty()) continue;
359 lyxerr[Debug::PARSER] << "Handling token: `"
360 << token << "'" << endl;
363 parseSingleLyXformat2Token(lex, par, first_par,
371 paragraph = first_par;
373 if (unknown_layouts > 0) {
374 string s = _("Couldn't set the layout for ");
375 if (unknown_layouts == 1) {
376 s += _("one paragraph");
378 s += tostr(unknown_layouts);
379 s += _(" paragraphs");
381 WriteAlert(_("Textclass Loading Error!"), s,
382 _("When reading " + fileName()));
389 #ifndef NO_COMPABILITY
390 void Buffer::insertErtContents(Paragraph * par, int & pos,
391 LyXFont const & font, bool set_inactive)
393 if (!ert_comp.contents.empty()) {
394 lyxerr[Debug::INSETS] << "ERT contents:\n"
395 << ert_comp.contents << endl;
396 Inset * inset = new InsetERT(ert_comp.contents, true);
397 par->insertInset(pos++, inset, font);
398 ert_comp.contents.erase();
401 ert_comp.active = false;
408 Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par,
409 Paragraph *& first_par,
410 string const & token, int & pos,
411 Paragraph::depth_type & depth,
415 bool the_end_read = false;
416 #ifndef NO_COMPABILITY
417 #ifndef NO_PEXTRA_REALLY
418 // This is super temporary but is needed to get the compability
419 // mode for minipages work correctly together with new tabulars.
420 static int call_depth;
422 bool checkminipage = false;
423 static Paragraph * minipar;
424 static Paragraph * parBeforeMinipage;
427 if (token[0] != '\\') {
428 #ifndef NO_COMPABILITY
429 if (ert_comp.active) {
430 ert_comp.contents += token;
433 for (string::const_iterator cit = token.begin();
434 cit != token.end(); ++cit) {
435 par->insertChar(pos, (*cit), font);
438 #ifndef NO_COMPABILITY
441 } else if (token == "\\i") {
442 Inset * inset = new InsetLatexAccent;
443 inset->read(this, lex);
444 par->insertInset(pos, inset, font);
446 } else if (token == "\\layout") {
447 #ifndef NO_COMPABILITY
448 ert_comp.in_tabular = false;
450 insertErtContents(par, pos, font);
453 string const layoutname = lex.getString();
454 pair<bool, LyXTextClass::LayoutList::size_type> pp
455 = textclasslist.NumberOfLayout(params.textclass,
458 #ifndef NO_COMPABILITY
459 if (compare_no_case(layoutname, "latex") == 0) {
460 ert_comp.active = true;
464 // The is the compability reading of layout caption.
465 // It can be removed in LyX version 1.3.0. (Lgb)
466 if (compare_no_case(layoutname, "caption") == 0) {
467 // We expect that the par we are now working on is
468 // really inside a InsetText inside a InsetFloat.
469 // We also know that captions can only be
470 // one paragraph. (Lgb)
472 // We should now read until the next "\layout"
474 // This is probably not good enough, what if the
475 // caption is the last par in the document (Lgb)
476 istream & ist = lex.getStream();
482 if (prefixIs(line, "\\layout")) {
486 if (prefixIs(line, "\\begin_inset"))
488 if (prefixIs(line, "\\end_inset")) {
499 // Now we should have the whole layout in ss
500 // we should now be able to give this to the
502 ss << "\\end_inset\n";
504 // This seems like a bug in stringstream.
505 // We really should be able to use ss
507 istringstream is(ss.str());
509 tmplex.setStream(is);
510 Inset * inset = new InsetCaption;
511 inset->Read(this, tmplex);
512 par->InsertInset(pos, inset, font);
519 par = new Paragraph(par);
523 par->layout = pp.second;
526 // use default layout "Standard" (0)
529 string const s = _("Layout had to be changed from\n")
530 + layoutname + _(" to ")
531 + textclasslist.NameOfLayout(params.textclass, par->layout);
532 InsetError * new_inset = new InsetError(s);
533 par->insertInset(0, new_inset);
535 // Test whether the layout is obsolete.
536 LyXLayout const & layout =
537 textclasslist.Style(params.textclass,
539 if (!layout.obsoleted_by().empty())
540 par->layout = textclasslist
541 .NumberOfLayout(params.textclass,
542 layout.obsoleted_by())
544 par->params().depth(depth);
545 font = LyXFont(LyXFont::ALL_INHERIT, params.language);
546 if (file_format < 216
547 && params.language->lang() == "hebrew")
548 font.setLanguage(default_language);
553 #ifndef NO_COMPABILITY
554 } else if (token == "\\begin_float") {
555 insertErtContents(par, pos, font);
556 //insertErtContents(par, pos, font, false);
557 //ert_stack.push(ert_comp);
558 //ert_comp = ErtComp();
560 // This is the compability reader. It can be removed in
561 // LyX version 1.3.0. (Lgb)
563 string const tmptok = lex.getString();
564 //lyxerr << "old float: " << tmptok << endl;
567 stringstream old_float;
569 if (tmptok == "footnote") {
570 inset = new InsetFoot;
571 } else if (tmptok == "margin") {
572 inset = new InsetMarginal;
573 } else if (tmptok == "fig") {
574 inset = new InsetFloat("figure");
575 old_float << "placement htbp\n"
577 } else if (tmptok == "tab") {
578 inset = new InsetFloat("table");
579 old_float << "placement htbp\n"
581 } else if (tmptok == "alg") {
582 inset = new InsetFloat("algorithm");
583 old_float << "placement htbp\n"
585 } else if (tmptok == "wide-fig") {
586 inset = new InsetFloat("figure");
587 //InsetFloat * tmp = new InsetFloat("figure");
590 old_float << "placement htbp\n"
592 } else if (tmptok == "wide-tab") {
593 inset = new InsetFloat("table");
594 //InsetFloat * tmp = new InsetFloat("table");
597 old_float << "placement htbp\n"
602 #ifndef NO_PEXTRA_REALLY
605 return false; // no end read yet
608 old_float << "collapsed true\n";
610 // Here we need to check for \end_deeper and handle that
611 // before we do the footnote parsing.
612 // This _is_ a hack! (Lgb)
615 string const tmp = lex.getString();
616 if (tmp == "\\end_deeper") {
617 //lyxerr << "\\end_deeper caught!" << endl;
619 lex.printError("\\end_deeper: "
620 "depth is already null");
625 old_float << tmp << ' ';
630 old_float << lex.getLongString("\\end_float")
631 << "\n\\end_inset\n";
632 //lyxerr << "Float Body:\n" << old_float.str() << endl;
633 // That this does not work seems like a bug
634 // in stringstream. (Lgb)
635 istringstream istr(old_float.str());
637 nylex.setStream(istr);
638 inset->read(this, nylex);
639 par->insertInset(pos, inset, font);
641 insertErtContents(par, pos, font);
643 } else if (token == "\\begin_deeper") {
645 } else if (token == "\\end_deeper") {
647 lex.printError("\\end_deeper: "
648 "depth is already null");
652 } else if (token == "\\begin_preamble") {
653 params.readPreamble(lex);
654 } else if (token == "\\textclass") {
656 pair<bool, LyXTextClassList::size_type> pp =
657 textclasslist.NumberOfClass(lex.getString());
659 params.textclass = pp.second;
661 WriteAlert(string(_("Textclass error")),
662 string(_("The document uses an unknown textclass \"")) +
663 lex.getString() + string("\"."),
664 string(_("LyX will not be able to produce output correctly.")));
665 params.textclass = 0;
667 if (!textclasslist.Load(params.textclass)) {
668 // if the textclass wasn't loaded properly
669 // we need to either substitute another
670 // or stop loading the file.
671 // I can substitute but I don't see how I can
672 // stop loading... ideas?? ARRae980418
673 WriteAlert(_("Textclass Loading Error!"),
674 string(_("Can't load textclass ")) +
675 textclasslist.NameOfClass(params.textclass),
676 _("-- substituting default"));
677 params.textclass = 0;
679 } else if (token == "\\options") {
681 params.options = lex.getString();
682 } else if (token == "\\language") {
683 params.readLanguage(lex);
684 } else if (token == "\\fontencoding") {
686 } else if (token == "\\inputencoding") {
688 params.inputenc = lex.getString();
689 } else if (token == "\\graphics") {
690 params.readGraphicsDriver(lex);
691 } else if (token == "\\fontscheme") {
693 params.fonts = lex.getString();
694 } else if (token == "\\noindent") {
695 par->params().noindent(true);
696 } else if (token == "\\fill_top") {
697 par->params().spaceTop(VSpace(VSpace::VFILL));
698 } else if (token == "\\fill_bottom") {
699 par->params().spaceBottom(VSpace(VSpace::VFILL));
700 } else if (token == "\\line_top") {
701 par->params().lineTop(true);
702 } else if (token == "\\line_bottom") {
703 par->params().lineBottom(true);
704 } else if (token == "\\pagebreak_top") {
705 par->params().pagebreakTop(true);
706 } else if (token == "\\pagebreak_bottom") {
707 par->params().pagebreakBottom(true);
708 } else if (token == "\\start_of_appendix") {
709 par->params().startOfAppendix(true);
710 } else if (token == "\\paragraph_separation") {
711 int tmpret = lex.findToken(string_paragraph_separation);
712 if (tmpret == -1) ++tmpret;
713 if (tmpret != LYX_LAYOUT_DEFAULT)
714 params.paragraph_separation =
715 static_cast<BufferParams::PARSEP>(tmpret);
716 } else if (token == "\\defskip") {
718 params.defskip = VSpace(lex.getString());
719 } else if (token == "\\epsfig") { // obsolete
720 // Indeed it is obsolete, but we HAVE to be backwards
721 // compatible until 0.14, because otherwise all figures
722 // in existing documents are irretrivably lost. (Asger)
723 params.readGraphicsDriver(lex);
724 } else if (token == "\\quotes_language") {
725 int tmpret = lex.findToken(string_quotes_language);
726 if (tmpret == -1) ++tmpret;
727 if (tmpret != LYX_LAYOUT_DEFAULT) {
728 InsetQuotes::quote_language tmpl =
729 InsetQuotes::EnglishQ;
732 tmpl = InsetQuotes::EnglishQ;
735 tmpl = InsetQuotes::SwedishQ;
738 tmpl = InsetQuotes::GermanQ;
741 tmpl = InsetQuotes::PolishQ;
744 tmpl = InsetQuotes::FrenchQ;
747 tmpl = InsetQuotes::DanishQ;
750 params.quotes_language = tmpl;
752 } else if (token == "\\quotes_times") {
754 switch (lex.getInteger()) {
756 params.quotes_times = InsetQuotes::SingleQ;
759 params.quotes_times = InsetQuotes::DoubleQ;
762 } else if (token == "\\papersize") {
763 int tmpret = lex.findToken(string_papersize);
767 params.papersize2 = tmpret;
768 } else if (token == "\\paperpackage") {
769 int tmpret = lex.findToken(string_paperpackages);
772 params.paperpackage = BufferParams::PACKAGE_NONE;
774 params.paperpackage = tmpret;
775 } else if (token == "\\use_geometry") {
777 params.use_geometry = lex.getInteger();
778 } else if (token == "\\use_amsmath") {
780 params.use_amsmath = lex.getInteger();
781 } else if (token == "\\use_natbib") {
783 params.use_natbib = lex.getInteger();
784 } else if (token == "\\use_numerical_citations") {
786 params.use_numerical_citations = lex.getInteger();
787 } else if (token == "\\paperorientation") {
788 int tmpret = lex.findToken(string_orientation);
789 if (tmpret == -1) ++tmpret;
790 if (tmpret != LYX_LAYOUT_DEFAULT)
791 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
792 } else if (token == "\\paperwidth") {
794 params.paperwidth = lex.getString();
795 } else if (token == "\\paperheight") {
797 params.paperheight = lex.getString();
798 } else if (token == "\\leftmargin") {
800 params.leftmargin = lex.getString();
801 } else if (token == "\\topmargin") {
803 params.topmargin = lex.getString();
804 } else if (token == "\\rightmargin") {
806 params.rightmargin = lex.getString();
807 } else if (token == "\\bottommargin") {
809 params.bottommargin = lex.getString();
810 } else if (token == "\\headheight") {
812 params.headheight = lex.getString();
813 } else if (token == "\\headsep") {
815 params.headsep = lex.getString();
816 } else if (token == "\\footskip") {
818 params.footskip = lex.getString();
819 } else if (token == "\\paperfontsize") {
821 params.fontsize = strip(lex.getString());
822 } else if (token == "\\papercolumns") {
824 params.columns = lex.getInteger();
825 } else if (token == "\\papersides") {
827 switch (lex.getInteger()) {
829 case 1: params.sides = LyXTextClass::OneSide; break;
830 case 2: params.sides = LyXTextClass::TwoSides; break;
832 } else if (token == "\\paperpagestyle") {
834 params.pagestyle = strip(lex.getString());
835 } else if (token == "\\bullet") {
837 int const index = lex.getInteger();
839 int temp_int = lex.getInteger();
840 params.user_defined_bullets[index].setFont(temp_int);
841 params.temp_bullets[index].setFont(temp_int);
843 temp_int = lex.getInteger();
844 params.user_defined_bullets[index].setCharacter(temp_int);
845 params.temp_bullets[index].setCharacter(temp_int);
847 temp_int = lex.getInteger();
848 params.user_defined_bullets[index].setSize(temp_int);
849 params.temp_bullets[index].setSize(temp_int);
851 string const temp_str = lex.getString();
852 if (temp_str != "\\end_bullet") {
853 // this element isn't really necessary for
854 // parsing but is easier for humans
855 // to understand bullets. Put it back and
856 // set a debug message?
857 lex.printError("\\end_bullet expected, got" + temp_str);
858 //how can I put it back?
860 } else if (token == "\\bulletLaTeX") {
862 int const index = lex.getInteger();
864 string temp_str = lex.getString();
866 while (temp_str != "\\end_bullet") {
867 // this loop structure is needed when user
868 // enters an empty string since the first
869 // thing returned will be the \\end_bullet
871 // if the LaTeX entry has spaces. Each element
872 // therefore needs to be read in turn
875 temp_str = lex.getString();
877 params.user_defined_bullets[index].setText(sum_str);
878 params.temp_bullets[index].setText(sum_str);
879 } else if (token == "\\secnumdepth") {
881 params.secnumdepth = lex.getInteger();
882 } else if (token == "\\tocdepth") {
884 params.tocdepth = lex.getInteger();
885 } else if (token == "\\spacing") {
887 string const tmp = strip(lex.getString());
888 Spacing::Space tmp_space = Spacing::Default;
890 if (tmp == "single") {
891 tmp_space = Spacing::Single;
892 } else if (tmp == "onehalf") {
893 tmp_space = Spacing::Onehalf;
894 } else if (tmp == "double") {
895 tmp_space = Spacing::Double;
896 } else if (tmp == "other") {
898 tmp_space = Spacing::Other;
899 tmp_val = lex.getFloat();
901 lex.printError("Unknown spacing token: '$$Token'");
903 // Small hack so that files written with klyx will be
906 par->params().spacing(Spacing(tmp_space, tmp_val));
908 params.spacing.set(tmp_space, tmp_val);
910 } else if (token == "\\paragraph_spacing") {
912 string const tmp = strip(lex.getString());
913 if (tmp == "single") {
914 par->params().spacing(Spacing(Spacing::Single));
915 } else if (tmp == "onehalf") {
916 par->params().spacing(Spacing(Spacing::Onehalf));
917 } else if (tmp == "double") {
918 par->params().spacing(Spacing(Spacing::Double));
919 } else if (tmp == "other") {
921 par->params().spacing(Spacing(Spacing::Other,
924 lex.printError("Unknown spacing token: '$$Token'");
926 } else if (token == "\\float_placement") {
928 params.float_placement = lex.getString();
929 } else if (token == "\\family") {
931 font.setLyXFamily(lex.getString());
932 } else if (token == "\\series") {
934 font.setLyXSeries(lex.getString());
935 } else if (token == "\\shape") {
937 font.setLyXShape(lex.getString());
938 } else if (token == "\\size") {
940 font.setLyXSize(lex.getString());
941 #ifndef NO_COMPABILITY
942 } else if (token == "\\latex") {
944 string const tok = lex.getString();
945 if (tok == "no_latex") {
947 insertErtContents(par, pos, font);
948 } else if (tok == "latex") {
949 ert_comp.active = true;
950 } else if (tok == "default") {
952 insertErtContents(par, pos, font);
954 lex.printError("Unknown LaTeX font flag "
958 } else if (token == "\\lang") {
960 string const tok = lex.getString();
961 Language const * lang = languages.getLanguage(tok);
963 font.setLanguage(lang);
965 font.setLanguage(params.language);
966 lex.printError("Unknown language `$$Token'");
968 } else if (token == "\\numeric") {
970 font.setNumber(font.setLyXMisc(lex.getString()));
971 } else if (token == "\\emph") {
973 font.setEmph(font.setLyXMisc(lex.getString()));
974 } else if (token == "\\bar") {
976 string const tok = lex.getString();
977 // This is dirty, but gone with LyX3. (Asger)
979 font.setUnderbar(LyXFont::ON);
980 else if (tok == "no")
981 font.setUnderbar(LyXFont::OFF);
982 else if (tok == "default")
983 font.setUnderbar(LyXFont::INHERIT);
985 lex.printError("Unknown bar font flag "
987 } else if (token == "\\noun") {
989 font.setNoun(font.setLyXMisc(lex.getString()));
990 } else if (token == "\\color") {
992 font.setLyXColor(lex.getString());
993 } else if (token == "\\align") {
994 int tmpret = lex.findToken(string_align);
995 if (tmpret == -1) ++tmpret;
996 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
997 int const tmpret2 = int(pow(2.0, tmpret));
998 //lyxerr << "Tmpret2 = " << tmpret2 << endl;
999 par->params().align(LyXAlignment(tmpret2));
1001 } else if (token == "\\added_space_top") {
1003 par->params().spaceTop(VSpace(lex.getString()));
1004 } else if (token == "\\added_space_bottom") {
1006 par->params().spaceBottom(VSpace(lex.getString()));
1007 #ifndef NO_COMPABILITY
1008 #ifndef NO_PEXTRA_REALLY
1009 } else if (token == "\\pextra_type") {
1011 par->params().pextraType(lex.getInteger());
1012 } else if (token == "\\pextra_width") {
1014 par->params().pextraWidth(lex.getString());
1015 } else if (token == "\\pextra_widthp") {
1017 par->params().pextraWidthp(lex.getString());
1018 } else if (token == "\\pextra_alignment") {
1020 par->params().pextraAlignment(lex.getInteger());
1021 } else if (token == "\\pextra_hfill") {
1023 par->params().pextraHfill(lex.getInteger());
1024 } else if (token == "\\pextra_start_minipage") {
1026 par->params().pextraStartMinipage(lex.getInteger());
1029 } else if (token == "\\labelwidthstring") {
1031 par->params().labelWidthString(lex.getString());
1032 // do not delete this token, it is still needed!
1033 } else if (token == "\\end_inset") {
1034 lyxerr << "Solitary \\end_inset. Missing \\begin_inset?.\n"
1035 << "Last inset read was: " << last_inset_read
1037 // Simply ignore this. The insets do not have
1039 // But insets should read it, it is a part of
1040 // the inset isn't it? Lgb.
1041 } else if (token == "\\begin_inset") {
1042 #ifndef NO_COMPABILITY
1043 insertErtContents(par, pos, font, false);
1044 ert_stack.push(ert_comp);
1045 ert_comp = ErtComp();
1047 readInset(lex, par, pos, font);
1048 #ifndef NO_COMPABILITY
1049 ert_comp = ert_stack.top();
1051 insertErtContents(par, pos, font);
1053 } else if (token == "\\SpecialChar") {
1054 LyXLayout const & layout =
1055 textclasslist.Style(params.textclass,
1058 // Insets don't make sense in a free-spacing context! ---Kayvan
1059 if (layout.free_spacing) {
1062 string next_token = lex.getString();
1063 if (next_token == "\\-") {
1064 par->insertChar(pos, '-', font);
1065 } else if (next_token == "\\protected_separator"
1066 || next_token == "~") {
1067 par->insertChar(pos, ' ', font);
1069 lex.printError("Token `$$Token' "
1071 "paragraph layout!");
1076 Inset * inset = new InsetSpecialChar;
1077 inset->read(this, lex);
1078 par->insertInset(pos, inset, font);
1081 } else if (token == "\\newline") {
1082 #ifndef NO_COMPABILITY
1083 if (!ert_comp.in_tabular && ert_comp.active) {
1084 ert_comp.contents += char(Paragraph::META_NEWLINE);
1086 // Since we cannot know it this is only a regular
1087 // newline or a tabular cell delimter we have to
1088 // handle the ERT here.
1089 insertErtContents(par, pos, font, false);
1091 par->insertChar(pos, Paragraph::META_NEWLINE, font);
1095 par->insertChar(pos, Paragraph::META_NEWLINE, font);
1098 } else if (token == "\\LyXTable") {
1099 #ifndef NO_COMPABILITY
1100 ert_comp.in_tabular = true;
1102 Inset * inset = new InsetTabular(*this);
1103 inset->read(this, lex);
1104 par->insertInset(pos, inset, font);
1106 } else if (token == "\\hfill") {
1107 par->insertChar(pos, Paragraph::META_HFILL, font);
1109 } else if (token == "\\protected_separator") { // obsolete
1110 // This is a backward compability thingie. (Lgb)
1111 // Remove it later some time...introduced with fileformat
1113 LyXLayout const & layout =
1114 textclasslist.Style(params.textclass,
1117 if (layout.free_spacing) {
1118 par->insertChar(pos, ' ', font);
1120 Inset * inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
1121 par->insertInset(pos, inset, font);
1124 } else if (token == "\\bibitem") { // ale970302
1126 InsetCommandParams p("bibitem", "dummy");
1127 par->bibkey = new InsetBibKey(p);
1129 par->bibkey->read(this, lex);
1130 } else if (token == "\\backslash") {
1131 #ifndef NO_COMPABILITY
1132 if (ert_comp.active) {
1133 ert_comp.contents += "\\";
1136 par->insertChar(pos, '\\', font);
1138 #ifndef NO_COMPABILITY
1141 } else if (token == "\\the_end") {
1142 #ifndef NO_COMPABILITY
1143 // If we still have some ert active here we have to insert
1144 // it so we don't loose it. (Lgb)
1145 insertErtContents(par, pos, font);
1147 the_end_read = true;
1148 #ifndef NO_COMPABILITY
1149 #ifndef NO_PEXTRA_REALLY
1150 minipar = parBeforeMinipage = 0;
1154 #ifndef NO_COMPABILITY
1155 if (ert_comp.active) {
1156 ert_comp.contents += token;
1159 // This should be insurance for the future: (Asger)
1160 lex.printError("Unknown token `$$Token'. "
1161 "Inserting as text.");
1162 string::const_iterator cit = token.begin();
1163 string::const_iterator end = token.end();
1164 for (; cit != end; ++cit) {
1165 par->insertChar(pos, (*cit), font);
1168 #ifndef NO_COMPABILITY
1173 #ifndef NO_COMPABILITY
1174 #ifndef NO_PEXTRA_REALLY
1175 // I wonder if we could use this blanket fix for all the
1176 // checkminipage cases...
1177 if (par && par->size()) {
1178 // It is possible that this will check to often,
1179 // but that should not be an correctness issue.
1180 // Only a speed issue.
1181 checkminipage = true;
1184 // now check if we have a minipage paragraph as at this
1185 // point we already read all the necessary data!
1186 // this cannot be done in layout because there we did
1187 // not read yet the paragraph PEXTRA-params (Jug)
1189 // BEGIN pextra_minipage compability
1190 // This should be removed in 1.3.x (Lgb)
1192 // This compability code is not perfect. In a couple
1193 // of rand cases it fails. When the minipage par is
1194 // the first par in the document, and when there are
1195 // none or only one regular paragraphs after the
1196 // minipage. Currently I am not investing any effort
1197 // in fixing those cases.
1199 //lyxerr << "Call depth: " << call_depth << endl;
1200 if (checkminipage && (call_depth == 1)) {
1201 checkminipage = false;
1202 if (minipar && (minipar != par) &&
1203 (par->params().pextraType()==Paragraph::PEXTRA_MINIPAGE))
1205 lyxerr << "minipages in a row" << endl;
1206 if (par->params().pextraStartMinipage()) {
1207 lyxerr << "start new minipage" << endl;
1208 // minipages in a row
1209 par->previous()->next(0);
1212 Paragraph * tmp = minipar;
1214 tmp->params().pextraType(0);
1215 tmp->params().pextraWidth(string());
1216 tmp->params().pextraWidthp(string());
1217 tmp->params().pextraAlignment(0);
1218 tmp->params().pextraHfill(false);
1219 tmp->params().pextraStartMinipage(false);
1222 // create a new paragraph to insert the
1223 // minipages in the following case
1224 if (par->params().pextraStartMinipage() &&
1225 !par->params().pextraHfill())
1227 Paragraph * p = new Paragraph;
1229 p->previous(parBeforeMinipage);
1230 parBeforeMinipage->next(p);
1232 p->params().depth(parBeforeMinipage->params().depth());
1233 parBeforeMinipage = p;
1235 InsetMinipage * mini = new InsetMinipage;
1236 mini->pos(static_cast<InsetMinipage::Position>(par->params().pextraAlignment()));
1237 mini->width(par->params().pextraWidth());
1238 if (!par->params().pextraWidthp().empty()) {
1239 lyxerr << "WP:" << mini->width() << endl;
1240 mini->width(tostr(par->params().pextraWidthp())+"%");
1242 mini->inset.paragraph(par);
1243 // Insert the minipage last in the
1244 // previous paragraph.
1245 if (par->params().pextraHfill()) {
1246 parBeforeMinipage->insertChar
1247 (parBeforeMinipage->size(), Paragraph::META_HFILL);
1249 parBeforeMinipage->insertInset
1250 (parBeforeMinipage->size(), mini);
1254 lyxerr << "new minipage par" << endl;
1255 //nothing to do just continue reading
1258 } else if (minipar && (minipar != par)) {
1259 lyxerr << "last minipage par read" << endl;
1260 // The last paragraph read was not part of a
1261 // minipage but the par linked list is...
1262 // So we need to remove the last par from the
1264 if (par->previous())
1265 par->previous()->next(0);
1266 par->previous(parBeforeMinipage);
1267 parBeforeMinipage->next(par);
1268 Paragraph * tmp = minipar;
1270 tmp->params().pextraType(0);
1271 tmp->params().pextraWidth(string());
1272 tmp->params().pextraWidthp(string());
1273 tmp->params().pextraAlignment(0);
1274 tmp->params().pextraHfill(false);
1275 tmp->params().pextraStartMinipage(false);
1278 depth = parBeforeMinipage->params().depth();
1279 minipar = parBeforeMinipage = 0;
1280 } else if (!minipar &&
1281 (par->params().pextraType() == Paragraph::PEXTRA_MINIPAGE))
1283 // par is the first paragraph in a minipage
1284 lyxerr << "begin minipage" << endl;
1285 // To minimize problems for
1286 // the users we will insert
1287 // the first minipage in
1288 // a sequence of minipages
1289 // in its own paragraph.
1290 Paragraph * p = new Paragraph;
1292 p->previous(par->previous());
1294 p->params().depth(depth);
1295 par->params().depth(0);
1297 if (par->previous())
1298 par->previous()->next(p);
1300 parBeforeMinipage = p;
1302 if (!first_par || (first_par == par))
1305 InsetMinipage * mini = new InsetMinipage;
1306 mini->pos(static_cast<InsetMinipage::Position>(minipar->params().pextraAlignment()));
1307 mini->width(minipar->params().pextraWidth());
1308 if (!par->params().pextraWidthp().empty()) {
1309 lyxerr << "WP:" << mini->width() << endl;
1310 mini->width(tostr(par->params().pextraWidthp())+"%");
1312 mini->inset.paragraph(minipar);
1314 // Insert the minipage last in the
1315 // previous paragraph.
1316 if (minipar->params().pextraHfill()) {
1317 parBeforeMinipage->insertChar
1318 (parBeforeMinipage->size(),Paragraph::META_HFILL);
1320 parBeforeMinipage->insertInset
1321 (parBeforeMinipage->size(), mini);
1324 // End of pextra_minipage compability
1328 return the_end_read;
1331 // needed to insert the selection
1332 void Buffer::insertStringAsLines(Paragraph *& par, Paragraph::size_type & pos,
1333 LyXFont const & font,
1334 string const & str) const
1336 LyXLayout const & layout = textclasslist.Style(params.textclass,
1338 // insert the string, don't insert doublespace
1339 bool space_inserted = true;
1340 for(string::const_iterator cit = str.begin();
1341 cit != str.end(); ++cit) {
1343 if (par->size() || layout.keepempty) {
1344 par->breakParagraph(params, pos,
1345 layout.isEnvironment());
1348 space_inserted = true;
1352 // do not insert consecutive spaces if !free_spacing
1353 } else if ((*cit == ' ' || *cit == '\t')
1354 && space_inserted && !layout.free_spacing) {
1356 } else if (*cit == '\t') {
1357 if (!layout.free_spacing) {
1358 // tabs are like spaces here
1359 par->insertChar(pos, ' ', font);
1361 space_inserted = true;
1363 const Paragraph::value_type nb = 8 - pos % 8;
1364 for (Paragraph::size_type a = 0;
1366 par->insertChar(pos, ' ', font);
1369 space_inserted = true;
1371 } else if (!IsPrintable(*cit)) {
1372 // Ignore unprintables
1375 // just insert the character
1376 par->insertChar(pos, *cit, font);
1378 space_inserted = (*cit == ' ');
1385 void Buffer::readInset(LyXLex & lex, Paragraph *& par,
1386 int & pos, LyXFont & font)
1388 // consistency check
1389 if (lex.getString() != "\\begin_inset") {
1390 lyxerr << "Buffer::readInset: Consistency check failed."
1397 string const tmptok = lex.getString();
1398 last_inset_read = tmptok;
1400 // test the different insets
1401 if (tmptok == "LatexCommand") {
1402 InsetCommandParams inscmd;
1405 string const cmdName = inscmd.getCmdName();
1407 // This strange command allows LyX to recognize "natbib" style
1408 // citations: citet, citep, Citet etc.
1409 if (compare_no_case(cmdName, "cite", 4) == 0) {
1410 inset = new InsetCitation(inscmd);
1411 } else if (cmdName == "bibitem") {
1412 lex.printError("Wrong place for bibitem");
1413 inset = new InsetBibKey(inscmd);
1414 } else if (cmdName == "BibTeX") {
1415 inset = new InsetBibtex(inscmd);
1416 } else if (cmdName == "index") {
1417 inset = new InsetIndex(inscmd);
1418 } else if (cmdName == "include") {
1419 inset = new InsetInclude(inscmd, *this);
1420 } else if (cmdName == "label") {
1421 inset = new InsetLabel(inscmd);
1422 } else if (cmdName == "url"
1423 || cmdName == "htmlurl") {
1424 inset = new InsetUrl(inscmd);
1425 } else if (cmdName == "ref"
1426 || cmdName == "pageref"
1427 || cmdName == "vref"
1428 || cmdName == "vpageref"
1429 || cmdName == "prettyref") {
1430 if (!inscmd.getOptions().empty()
1431 || !inscmd.getContents().empty()) {
1432 inset = new InsetRef(inscmd, *this);
1434 } else if (cmdName == "tableofcontents") {
1435 inset = new InsetTOC(inscmd);
1436 } else if (cmdName == "listofalgorithms") {
1437 inset = new InsetFloatList("algorithm");
1438 } else if (cmdName == "listoffigures") {
1439 inset = new InsetFloatList("figure");
1440 } else if (cmdName == "listoftables") {
1441 inset = new InsetFloatList("table");
1442 } else if (cmdName == "printindex") {
1443 inset = new InsetPrintIndex(inscmd);
1444 } else if (cmdName == "lyxparent") {
1445 inset = new InsetParent(inscmd, *this);
1448 bool alreadyread = false;
1449 if (tmptok == "Quotes") {
1450 inset = new InsetQuotes;
1451 } else if (tmptok == "External") {
1452 inset = new InsetExternal;
1453 } else if (tmptok == "FormulaMacro") {
1454 inset = new InsetFormulaMacro;
1455 } else if (tmptok == "Formula") {
1456 inset = new InsetFormula;
1457 } else if (tmptok == "Figure") { // Backward compatibility
1458 inset = new InsetFig(100, 100, *this);
1459 //inset = new InsetGraphics;
1460 } else if (tmptok == "Graphics") {
1461 inset = new InsetGraphics;
1462 } else if (tmptok == "Info") {// backwards compatibility
1463 inset = new InsetNote(this,
1464 lex.getLongString("\\end_inset"),
1467 } else if (tmptok == "Note") {
1468 inset = new InsetNote;
1469 } else if (tmptok == "Include") {
1470 InsetCommandParams p( "Include" );
1471 inset = new InsetInclude(p, *this);
1472 } else if (tmptok == "ERT") {
1473 inset = new InsetERT;
1474 } else if (tmptok == "Tabular") {
1475 inset = new InsetTabular(*this);
1476 } else if (tmptok == "Text") {
1477 inset = new InsetText;
1478 } else if (tmptok == "Foot") {
1479 inset = new InsetFoot;
1480 } else if (tmptok == "Marginal") {
1481 inset = new InsetMarginal;
1482 } else if (tmptok == "Minipage") {
1483 inset = new InsetMinipage;
1484 } else if (tmptok == "Float") {
1486 string tmptok = lex.getString();
1487 inset = new InsetFloat(tmptok);
1489 } else if (tmptok == "List") {
1490 inset = new InsetList;
1491 } else if (tmptok == "Theorem") {
1492 inset = new InsetList;
1494 } else if (tmptok == "Caption") {
1495 inset = new InsetCaption;
1496 } else if (tmptok == "FloatList") {
1497 inset = new InsetFloatList;
1500 if (inset && !alreadyread) inset->read(this, lex);
1504 par->insertInset(pos, inset, font);
1510 bool Buffer::readFile(LyXLex & lex, Paragraph * par)
1514 string const token(lex.getString());
1515 if (token == "\\lyxformat") { // the first token _must_ be...
1517 string tmp_format = lex.getString();
1518 //lyxerr << "LyX Format: `" << tmp_format << "'" << endl;
1519 // if present remove ".," from string.
1520 string::size_type dot = tmp_format.find_first_of(".,");
1521 //lyxerr << " dot found at " << dot << endl;
1522 if (dot != string::npos)
1523 tmp_format.erase(dot, 1);
1524 file_format = strToInt(tmp_format);
1525 if (file_format == LYX_FORMAT) {
1527 } else if (file_format > LYX_FORMAT) {
1529 WriteAlert(_("Warning!"),
1530 _("LyX file format is newer that what"),
1531 _("is supported in this LyX version. Expect some problems."));
1533 } else if (file_format < LYX_FORMAT) {
1535 if (file_format < 200) {
1536 WriteAlert(_("ERROR!"),
1537 _("Old LyX file format found. "
1538 "Use LyX 0.10.x to read this!"));
1542 bool the_end = readLyXformat2(lex, par);
1544 // the_end was added in 213
1545 if (file_format < 213)
1549 WriteAlert(_("Warning!"),
1550 _("Reading of document is not complete"),
1551 _("Maybe the document is truncated"));
1553 } else { // "\\lyxformat" not found
1554 WriteAlert(_("ERROR!"), _("Not a LyX file!"));
1557 WriteAlert(_("ERROR!"), _("Unable to read file!"));
1562 // Should probably be moved to somewhere else: BufferView? LyXView?
1563 bool Buffer::save() const
1565 // We don't need autosaves in the immediate future. (Asger)
1566 resetAutosaveTimers();
1570 if (lyxrc.make_backup) {
1571 s = fileName() + '~';
1572 if (!lyxrc.backupdir_path.empty())
1573 s = AddName(lyxrc.backupdir_path,
1574 subst(os::slashify_path(s),'/','!'));
1576 // Rename is the wrong way of making a backup,
1577 // this is the correct way.
1578 /* truss cp fil fil2:
1579 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
1580 stat("LyXVC.lyx", 0xEFFFF688) = 0
1581 open("LyXVC.lyx", O_RDONLY) = 3
1582 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1583 fstat(4, 0xEFFFF508) = 0
1584 fstat(3, 0xEFFFF508) = 0
1585 read(3, " # T h i s f i l e w".., 8192) = 5579
1586 write(4, " # T h i s f i l e w".., 5579) = 5579
1587 read(3, 0xEFFFD4A0, 8192) = 0
1590 chmod("LyXVC3.lyx", 0100644) = 0
1591 lseek(0, 0, SEEK_CUR) = 46440
1595 // Should probably have some more error checking here.
1596 // Should be cleaned up in 0.13, at least a bit.
1597 // Doing it this way, also makes the inodes stay the same.
1598 // This is still not a very good solution, in particular we
1599 // might loose the owner of the backup.
1600 FileInfo finfo(fileName());
1601 if (finfo.exist()) {
1602 mode_t fmode = finfo.getMode();
1603 struct utimbuf times = {
1604 finfo.getAccessTime(),
1605 finfo.getModificationTime() };
1607 ifstream ifs(fileName().c_str());
1608 ofstream ofs(s.c_str(), ios::out|ios::trunc);
1613 ::chmod(s.c_str(), fmode);
1615 if (::utime(s.c_str(), ×)) {
1616 lyxerr << "utime error." << endl;
1619 lyxerr << "LyX was not able to make "
1620 "backup copy. Beware." << endl;
1625 if (writeFile(fileName(), false)) {
1627 removeAutosaveFile(fileName());
1629 // Saving failed, so backup is not backup
1630 if (lyxrc.make_backup) {
1631 lyx::rename(s, fileName());
1639 // Returns false if unsuccesful
1640 bool Buffer::writeFile(string const & fname, bool flag) const
1642 // if flag is false writeFile will not create any GUI
1643 // warnings, only cerr.
1644 // Needed for autosave in background or panic save (Matthias 120496)
1646 if (read_only && (fname == filename)) {
1647 // Here we should come with a question if we should
1648 // perform the write anyway.
1650 lyxerr << _("Error! Document is read-only: ")
1653 WriteAlert(_("Error! Document is read-only: "),
1658 FileInfo finfo(fname);
1659 if (finfo.exist() && !finfo.writable()) {
1660 // Here we should come with a question if we should
1661 // try to do the save anyway. (i.e. do a chmod first)
1663 lyxerr << _("Error! Cannot write file: ")
1666 WriteFSAlert(_("Error! Cannot write file: "),
1671 ofstream ofs(fname.c_str());
1674 lyxerr << _("Error! Cannot open file: ")
1677 WriteFSAlert(_("Error! Cannot open file: "),
1683 // Use the standard "C" locale for file output.
1684 ofs.imbue(std::locale::classic());
1687 // The top of the file should not be written by params.
1689 // write out a comment in the top of the file
1690 ofs << '#' << LYX_DOCVERSION
1691 << " created this file. For more info see http://www.lyx.org/\n"
1692 << "\\lyxformat " << LYX_FORMAT << "\n";
1694 // now write out the buffer paramters.
1695 params.writeFile(ofs);
1697 Paragraph::depth_type depth = 0;
1699 // this will write out all the paragraphs
1700 // using recursive descent.
1701 paragraph->writeFile(this, ofs, params, depth);
1703 // Write marker that shows file is complete
1704 ofs << "\n\\the_end" << endl;
1708 // how to check if close went ok?
1709 // Following is an attempt... (BE 20001011)
1711 // good() returns false if any error occured, including some
1712 // formatting error.
1713 // bad() returns true if something bad happened in the buffer,
1714 // which should include file system full errors.
1721 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
1723 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
1733 string const Buffer::asciiParagraph(Paragraph const * par,
1734 unsigned int linelen) const
1736 ostringstream buffer;
1737 Paragraph::depth_type depth = 0;
1739 Paragraph::depth_type ltype_depth = 0;
1740 unsigned int currlinelen = 0;
1741 bool ref_printed = false;
1745 if (!par->previous()) {
1746 // begins or ends a deeper area ?
1747 if (depth != par->params().depth()) {
1748 if (par->params().depth() > depth) {
1749 while (par->params().depth() > depth) {
1753 while (par->params().depth() < depth) {
1759 // First write the layout
1760 string const tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1761 if (tmp == "Itemize") {
1763 ltype_depth = depth + 1;
1764 } else if (tmp == "Enumerate") {
1766 ltype_depth = depth + 1;
1767 } else if (contains(tmp, "ection")) {
1769 ltype_depth = depth + 1;
1770 } else if (contains(tmp, "aragraph")) {
1772 ltype_depth = depth + 1;
1773 } else if (tmp == "Description") {
1775 ltype_depth = depth + 1;
1776 } else if (tmp == "Abstract") {
1779 } else if (tmp == "Bibliography") {
1787 /* maybe some vertical spaces */
1789 /* the labelwidthstring used in lists */
1793 /* some pagebreaks? */
1797 /* what about the alignment */
1799 lyxerr << "Should this ever happen?" << endl;
1802 for (Paragraph::size_type i = 0; i < par->size(); ++i) {
1803 if (!i && !noparbreak) {
1806 for (Paragraph::depth_type j = 0; j < depth; ++j)
1808 currlinelen = depth * 2;
1811 case 4: // (Sub)Paragraph
1812 case 5: // Description
1816 buffer << "Abstract\n\n";
1818 buffer << "Abstract: ";
1820 case 7: // Bibliography
1823 buffer << "References\n\n";
1825 buffer << "References: ";
1830 buffer << par->params().labelString() << " ";
1833 if (ltype_depth > depth) {
1834 for (Paragraph::depth_type j = ltype_depth - 1;
1837 currlinelen += (ltype_depth-depth)*2;
1841 char c = par->getUChar(params, i);
1845 case Paragraph::META_INSET:
1847 Inset const * inset = par->getInset(i);
1849 if (!inset->ascii(this, buffer)) {
1852 rsplit(buffer.str().c_str(),
1854 currlinelen += s.length();
1856 // to be sure it breaks paragraph
1857 currlinelen += linelen;
1863 case Paragraph::META_NEWLINE:
1866 for (Paragraph::depth_type j = 0;
1870 currlinelen = depth * 2;
1871 if (ltype_depth > depth) {
1872 for (Paragraph::depth_type j = ltype_depth;
1875 currlinelen += (ltype_depth - depth) * 2;
1879 case Paragraph::META_HFILL:
1888 if ((linelen > 0) && (currlinelen > (linelen - 10)) &&
1889 (c == ' ') && ((i + 2) < par->size()))
1892 for (Paragraph::depth_type j = 0;
1895 currlinelen = depth * 2;
1896 if (ltype_depth > depth) {
1897 for (Paragraph::depth_type j = ltype_depth;
1900 currlinelen += (ltype_depth-depth)*2;
1902 } else if (c != '\0')
1905 lyxerr[Debug::INFO] << "writeAsciiFile: NULL char in structure." << endl;
1910 return buffer.str().c_str();
1914 void Buffer::writeFileAscii(string const & fname, int linelen)
1916 ofstream ofs(fname.c_str());
1918 WriteFSAlert(_("Error: Cannot write file:"), fname);
1921 writeFileAscii(ofs, linelen);
1925 void Buffer::writeFileAscii(ostream & ofs, int linelen)
1927 Paragraph * par = paragraph;
1929 ofs << asciiParagraph(par, linelen);
1937 void Buffer::makeLaTeXFile(string const & fname,
1938 string const & original_path,
1939 bool nice, bool only_body)
1941 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1943 niceFile = nice; // this will be used by Insetincludes.
1945 tex_code_break_column = lyxrc.ascii_linelen;
1947 LyXTextClass const & tclass =
1948 textclasslist.TextClass(params.textclass);
1950 ofstream ofs(fname.c_str());
1952 WriteFSAlert(_("Error: Cannot open file: "), fname);
1956 // validate the buffer.
1957 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1958 LaTeXFeatures features(params, tclass.numLayouts());
1960 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1963 // The starting paragraph of the coming rows is the
1964 // first paragraph of the document. (Asger)
1965 texrow.start(paragraph, 0);
1967 if (!only_body && nice) {
1968 ofs << "%% " LYX_DOCVERSION " created this file. "
1969 "For more info, see http://www.lyx.org/.\n"
1970 "%% Do not edit unless you really know what "
1975 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1976 // There are a few differences between nice LaTeX and usual files:
1977 // usual is \batchmode and has a
1978 // special input@path to allow the including of figures
1979 // with either \input or \includegraphics (what figinsets do).
1980 // batchmode is not set if there is a tex_code_break_column.
1981 // In this case somebody is interested in the generated LaTeX,
1982 // so this is OK. input@path is set when the actual parameter
1983 // original_path is set. This is done for usual tex-file, but not
1984 // for nice-latex-file. (Matthias 250696)
1987 // code for usual, NOT nice-latex-file
1988 ofs << "\\batchmode\n"; // changed
1989 // from \nonstopmode
1992 if (!original_path.empty()) {
1993 ofs << "\\makeatletter\n"
1994 << "\\def\\input@path{{"
1995 << os::external_path(original_path) << "/}}\n"
1996 << "\\makeatother\n";
2002 ofs << "\\documentclass";
2004 ostringstream options; // the document class options.
2006 if (tokenPos(tclass.opt_fontsize(),
2007 '|', params.fontsize) >= 0) {
2008 // only write if existing in list (and not default)
2009 options << params.fontsize << "pt,";
2013 if (!params.use_geometry &&
2014 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2015 switch (params.papersize) {
2016 case BufferParams::PAPER_A4PAPER:
2017 options << "a4paper,";
2019 case BufferParams::PAPER_USLETTER:
2020 options << "letterpaper,";
2022 case BufferParams::PAPER_A5PAPER:
2023 options << "a5paper,";
2025 case BufferParams::PAPER_B5PAPER:
2026 options << "b5paper,";
2028 case BufferParams::PAPER_EXECUTIVEPAPER:
2029 options << "executivepaper,";
2031 case BufferParams::PAPER_LEGALPAPER:
2032 options << "legalpaper,";
2038 if (params.sides != tclass.sides()) {
2039 switch (params.sides) {
2040 case LyXTextClass::OneSide:
2041 options << "oneside,";
2043 case LyXTextClass::TwoSides:
2044 options << "twoside,";
2050 if (params.columns != tclass.columns()) {
2051 if (params.columns == 2)
2052 options << "twocolumn,";
2054 options << "onecolumn,";
2057 if (!params.use_geometry
2058 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2059 options << "landscape,";
2061 // language should be a parameter to \documentclass
2063 ostringstream language_options;
2064 if (params.language->babel() == "hebrew"
2065 && default_language->babel() != "hebrew")
2066 // This seems necessary
2067 features.UsedLanguages.insert(default_language);
2069 if (lyxrc.language_use_babel ||
2070 params.language->lang() != lyxrc.default_language ||
2071 !features.UsedLanguages.empty()) {
2073 for (LaTeXFeatures::LanguageList::const_iterator cit =
2074 features.UsedLanguages.begin();
2075 cit != features.UsedLanguages.end(); ++cit)
2076 language_options << (*cit)->babel() << ',';
2077 language_options << params.language->babel();
2078 if (lyxrc.language_global_options)
2079 options << language_options.str() << ',';
2082 // the user-defined options
2083 if (!params.options.empty()) {
2084 options << params.options << ',';
2087 string strOptions(options.str().c_str());
2088 if (!strOptions.empty()){
2089 strOptions = strip(strOptions, ',');
2090 ofs << '[' << strOptions << ']';
2094 << textclasslist.LatexnameOfClass(params.textclass)
2097 // end of \documentclass defs
2099 // font selection must be done before loading fontenc.sty
2100 // The ae package is not needed when using OT1 font encoding.
2101 if (params.fonts != "default" &&
2102 (params.fonts != "ae" || lyxrc.fontenc != "default")) {
2103 ofs << "\\usepackage{" << params.fonts << "}\n";
2105 if (params.fonts == "ae") {
2106 ofs << "\\usepackage{aecompl}\n";
2110 // this one is not per buffer
2111 if (lyxrc.fontenc != "default") {
2112 ofs << "\\usepackage[" << lyxrc.fontenc
2117 if (params.inputenc == "auto") {
2118 string const doc_encoding =
2119 params.language->encoding()->LatexName();
2121 // Create a list with all the input encodings used
2123 set<string> encodings;
2124 for (LaTeXFeatures::LanguageList::const_iterator it =
2125 features.UsedLanguages.begin();
2126 it != features.UsedLanguages.end(); ++it)
2127 if ((*it)->encoding()->LatexName() != doc_encoding)
2128 encodings.insert((*it)->encoding()->LatexName());
2130 ofs << "\\usepackage[";
2131 std::copy(encodings.begin(), encodings.end(),
2132 std::ostream_iterator<string>(ofs, ","));
2133 ofs << doc_encoding << "]{inputenc}\n";
2135 } else if (params.inputenc != "default") {
2136 ofs << "\\usepackage[" << params.inputenc
2141 // At the very beginning the text parameters.
2142 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2143 switch (params.paperpackage) {
2144 case BufferParams::PACKAGE_A4:
2145 ofs << "\\usepackage{a4}\n";
2148 case BufferParams::PACKAGE_A4WIDE:
2149 ofs << "\\usepackage{a4wide}\n";
2152 case BufferParams::PACKAGE_WIDEMARGINSA4:
2153 ofs << "\\usepackage[widemargins]{a4}\n";
2158 if (params.use_geometry) {
2159 ofs << "\\usepackage{geometry}\n";
2161 ofs << "\\geometry{verbose";
2162 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2163 ofs << ",landscape";
2164 switch (params.papersize2) {
2165 case BufferParams::VM_PAPER_CUSTOM:
2166 if (!params.paperwidth.empty())
2167 ofs << ",paperwidth="
2168 << params.paperwidth;
2169 if (!params.paperheight.empty())
2170 ofs << ",paperheight="
2171 << params.paperheight;
2173 case BufferParams::VM_PAPER_USLETTER:
2174 ofs << ",letterpaper";
2176 case BufferParams::VM_PAPER_USLEGAL:
2177 ofs << ",legalpaper";
2179 case BufferParams::VM_PAPER_USEXECUTIVE:
2180 ofs << ",executivepaper";
2182 case BufferParams::VM_PAPER_A3:
2185 case BufferParams::VM_PAPER_A4:
2188 case BufferParams::VM_PAPER_A5:
2191 case BufferParams::VM_PAPER_B3:
2194 case BufferParams::VM_PAPER_B4:
2197 case BufferParams::VM_PAPER_B5:
2201 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2202 switch (lyxrc.default_papersize) {
2203 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2204 case BufferParams::PAPER_USLETTER:
2205 ofs << ",letterpaper";
2207 case BufferParams::PAPER_LEGALPAPER:
2208 ofs << ",legalpaper";
2210 case BufferParams::PAPER_EXECUTIVEPAPER:
2211 ofs << ",executivepaper";
2213 case BufferParams::PAPER_A3PAPER:
2216 case BufferParams::PAPER_A4PAPER:
2219 case BufferParams::PAPER_A5PAPER:
2222 case BufferParams::PAPER_B5PAPER:
2227 if (!params.topmargin.empty())
2228 ofs << ",tmargin=" << params.topmargin;
2229 if (!params.bottommargin.empty())
2230 ofs << ",bmargin=" << params.bottommargin;
2231 if (!params.leftmargin.empty())
2232 ofs << ",lmargin=" << params.leftmargin;
2233 if (!params.rightmargin.empty())
2234 ofs << ",rmargin=" << params.rightmargin;
2235 if (!params.headheight.empty())
2236 ofs << ",headheight=" << params.headheight;
2237 if (!params.headsep.empty())
2238 ofs << ",headsep=" << params.headsep;
2239 if (!params.footskip.empty())
2240 ofs << ",footskip=" << params.footskip;
2244 if (features.amsstyle
2245 && !tclass.provides(LyXTextClass::amsmath)) {
2246 ofs << "\\usepackage{amsmath}\n";
2250 if (tokenPos(tclass.opt_pagestyle(),
2251 '|', params.pagestyle) >= 0) {
2252 if (params.pagestyle == "fancy") {
2253 ofs << "\\usepackage{fancyhdr}\n";
2256 ofs << "\\pagestyle{" << params.pagestyle << "}\n";
2260 // We try to load babel late, in case it interferes
2261 // with other packages.
2263 string tmp = lyxrc.language_package;
2264 if (!lyxrc.language_global_options
2265 && tmp == "\\usepackage{babel}")
2266 tmp = string("\\usepackage[") +
2267 language_options.str().c_str() +
2273 if (params.secnumdepth != tclass.secnumdepth()) {
2274 ofs << "\\setcounter{secnumdepth}{"
2275 << params.secnumdepth
2279 if (params.tocdepth != tclass.tocdepth()) {
2280 ofs << "\\setcounter{tocdepth}{"
2286 if (params.paragraph_separation) {
2287 switch (params.defskip.kind()) {
2288 case VSpace::SMALLSKIP:
2289 ofs << "\\setlength\\parskip{\\smallskipamount}\n";
2291 case VSpace::MEDSKIP:
2292 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2294 case VSpace::BIGSKIP:
2295 ofs << "\\setlength\\parskip{\\bigskipamount}\n";
2297 case VSpace::LENGTH:
2298 ofs << "\\setlength\\parskip{"
2299 << params.defskip.length().asLatexString()
2302 default: // should never happen // Then delete it.
2303 ofs << "\\setlength\\parskip{\\medskipamount}\n";
2308 ofs << "\\setlength\\parindent{0pt}\n";
2312 // Now insert the LyX specific LaTeX commands...
2314 // The optional packages;
2315 string preamble(features.getPackages());
2317 // this might be useful...
2318 preamble += "\n\\makeatletter\n";
2320 // Some macros LyX will need
2321 string tmppreamble(features.getMacros());
2323 if (!tmppreamble.empty()) {
2324 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2325 "LyX specific LaTeX commands.\n"
2326 + tmppreamble + '\n';
2329 // the text class specific preamble
2330 tmppreamble = features.getTClassPreamble();
2331 if (!tmppreamble.empty()) {
2332 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2333 "Textclass specific LaTeX commands.\n"
2334 + tmppreamble + '\n';
2337 /* the user-defined preamble */
2338 if (!params.preamble.empty()) {
2339 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2340 "User specified LaTeX commands.\n"
2341 + params.preamble + '\n';
2344 preamble += "\\makeatother\n";
2346 // Itemize bullet settings need to be last in case the user
2347 // defines their own bullets that use a package included
2348 // in the user-defined preamble -- ARRae
2349 // Actually it has to be done much later than that
2350 // since some packages like frenchb make modifications
2351 // at \begin{document} time -- JMarc
2353 for (int i = 0; i < 4; ++i) {
2354 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2355 if (bullets_def.empty())
2356 bullets_def="\\AtBeginDocument{\n";
2357 bullets_def += " \\renewcommand{\\labelitemi";
2359 // `i' is one less than the item to modify
2366 bullets_def += "ii";
2372 bullets_def += "}{" +
2373 params.user_defined_bullets[i].getText()
2378 if (!bullets_def.empty())
2379 preamble += bullets_def + "}\n\n";
2381 for (int j = countChar(preamble, '\n'); j-- ;) {
2388 ofs << "\\begin{document}\n";
2391 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
2393 if (!lyxrc.language_auto_begin) {
2394 ofs << subst(lyxrc.language_command_begin, "$$lang",
2395 params.language->babel())
2400 latexParagraphs(ofs, paragraph, 0, texrow);
2402 // add this just in case after all the paragraphs
2406 if (!lyxrc.language_auto_end) {
2407 ofs << subst(lyxrc.language_command_end, "$$lang",
2408 params.language->babel())
2414 ofs << "\\end{document}\n";
2417 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2419 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2423 // Just to be sure. (Asger)
2426 // tex_code_break_column's value is used to decide
2427 // if we are in batchmode or not (within mathed_write()
2428 // in math_write.C) so we must set it to a non-zero
2429 // value when we leave otherwise we save incorrect .lyx files.
2430 tex_code_break_column = lyxrc.ascii_linelen;
2434 lyxerr << "File was not closed properly." << endl;
2437 lyxerr[Debug::INFO] << "Finished making latex file." << endl;
2442 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
2444 void Buffer::latexParagraphs(ostream & ofs, Paragraph * par,
2445 Paragraph * endpar, TexRow & texrow) const
2447 bool was_title = false;
2448 bool already_title = false;
2451 while (par != endpar) {
2452 LyXLayout const & layout =
2453 textclasslist.Style(params.textclass,
2456 if (layout.intitle) {
2457 if (already_title) {
2458 lyxerr <<"Error in latexParagraphs: You"
2459 " should not mix title layouts"
2460 " with normal ones." << endl;
2463 } else if (was_title && !already_title) {
2464 ofs << "\\maketitle\n";
2466 already_title = true;
2470 if (layout.isEnvironment()) {
2471 par = par->TeXEnvironment(this, params, ofs, texrow);
2473 par = par->TeXOnePar(this, params, ofs, texrow, false);
2476 // It might be that we only have a title in this document
2477 if (was_title && !already_title) {
2478 ofs << "\\maketitle\n";
2484 bool Buffer::isLatex() const
2486 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2490 bool Buffer::isLinuxDoc() const
2492 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2496 bool Buffer::isLiterate() const
2498 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2502 bool Buffer::isDocBook() const
2504 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2508 bool Buffer::isSGML() const
2510 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2511 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2515 void Buffer::sgmlOpenTag(ostream & os, Paragraph::depth_type depth,
2516 string const & latexname) const
2518 if (!latexname.empty() && latexname != "!-- --")
2519 os << "<!-- " << depth << " -->" << "<" << latexname << ">";
2520 //os << string(depth, ' ') << "<" << latexname << ">\n";
2524 void Buffer::sgmlCloseTag(ostream & os, Paragraph::depth_type depth,
2525 string const & latexname) const
2527 if (!latexname.empty() && latexname != "!-- --")
2528 os << "<!-- " << depth << " -->" << "</" << latexname << ">\n";
2529 //os << string(depth, ' ') << "</" << latexname << ">\n";
2533 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
2535 ofstream ofs(fname.c_str());
2538 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2542 niceFile = nice; // this will be used by included files.
2544 LyXTextClass const & tclass =
2545 textclasslist.TextClass(params.textclass);
2547 LaTeXFeatures features(params, tclass.numLayouts());
2552 string top_element = textclasslist.LatexnameOfClass(params.textclass);
2555 string sgml_includedfiles=features.getIncludedFiles(fname);
2557 if (params.preamble.empty() && sgml_includedfiles.empty()) {
2558 ofs << "<!doctype linuxdoc system>\n\n";
2560 ofs << "<!doctype linuxdoc system [ "
2561 << params.preamble << sgml_includedfiles << " \n]>\n\n";
2564 if (params.options.empty())
2565 sgmlOpenTag(ofs, 0, top_element);
2567 string top = top_element;
2569 top += params.options;
2570 sgmlOpenTag(ofs, 0, top);
2574 ofs << "<!-- " << LYX_DOCVERSION
2575 << " created this file. For more info see http://www.lyx.org/"
2578 Paragraph::depth_type depth = 0; // paragraph depth
2579 Paragraph * par = paragraph;
2581 vector<string> environment_stack(5);
2584 LyXLayout const & style =
2585 textclasslist.Style(params.textclass,
2588 // treat <toc> as a special case for compatibility with old code
2589 if (par->getChar(0) == Paragraph::META_INSET) {
2590 Inset * inset = par->getInset(0);
2591 Inset::Code lyx_code = inset->lyxCode();
2592 if (lyx_code == Inset::TOC_CODE){
2593 string const temp = "toc";
2594 sgmlOpenTag(ofs, depth, temp);
2601 // environment tag closing
2602 for (; depth > par->params().depth(); --depth) {
2603 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2604 environment_stack[depth].erase();
2607 // write opening SGML tags
2608 switch (style.latextype) {
2609 case LATEX_PARAGRAPH:
2610 if (depth == par->params().depth()
2611 && !environment_stack[depth].empty()) {
2612 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2613 environment_stack[depth].erase();
2619 sgmlOpenTag(ofs, depth, style.latexname());
2624 linuxDocError(par, 0,
2625 _("Error : Wrong depth for"
2626 " LatexType Command.\n"));
2628 if (!environment_stack[depth].empty()){
2629 sgmlCloseTag(ofs, depth,
2630 environment_stack[depth]);
2634 environment_stack[depth].erase();
2635 sgmlOpenTag(ofs, depth, style.latexname());
2638 case LATEX_ENVIRONMENT:
2639 case LATEX_ITEM_ENVIRONMENT:
2640 if (depth == par->params().depth()
2641 && environment_stack[depth] != style.latexname()) {
2642 sgmlCloseTag(ofs, depth,
2643 environment_stack[depth]);
2644 environment_stack[depth].erase();
2646 if (depth < par->params().depth()) {
2647 depth = par->params().depth();
2648 environment_stack[depth].erase();
2650 if (environment_stack[depth] != style.latexname()) {
2652 sgmlOpenTag(ofs, depth, "p");
2654 sgmlOpenTag(ofs, depth, style.latexname());
2656 if (environment_stack.size() == depth + 1)
2657 environment_stack.push_back("!-- --");
2658 environment_stack[depth] = style.latexname();
2661 if (style.latexparam() == "CDATA")
2664 if (style.latextype == LATEX_ENVIRONMENT) break;
2666 if (style.labeltype == LABEL_MANUAL)
2671 sgmlOpenTag(ofs, depth + 1, item_name);
2674 sgmlOpenTag(ofs, depth, style.latexname());
2678 simpleLinuxDocOnePar(ofs, par, depth);
2683 // write closing SGML tags
2684 switch (style.latextype) {
2687 case LATEX_ENVIRONMENT:
2688 case LATEX_ITEM_ENVIRONMENT:
2689 if (style.latexparam() == "CDATA")
2693 sgmlCloseTag(ofs, depth, style.latexname());
2699 for (int i=depth; i >= 0; --i)
2700 sgmlCloseTag(ofs, depth, environment_stack[i]);
2704 sgmlCloseTag(ofs, 0, top_element);
2708 // How to check for successful close
2712 void Buffer::docBookHandleCaption(ostream & os, string & inner_tag,
2713 Paragraph::depth_type depth, int desc_on,
2716 Paragraph * tpar = par;
2718 && (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2720 tpar = tpar->next();
2723 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2724 "Caption").second) {
2725 sgmlOpenTag(os, depth + 1, inner_tag);
2727 simpleDocBookOnePar(os, extra_par, tpar,
2728 desc_on, depth + 2);
2729 sgmlCloseTag(os, depth+1, inner_tag);
2730 if (!extra_par.empty())
2736 // checks, if newcol chars should be put into this line
2737 // writes newline, if necessary.
2740 void linux_doc_line_break(ostream & os, string::size_type & colcount,
2741 string::size_type newcol)
2744 if (colcount > lyxrc.ascii_linelen) {
2746 colcount = newcol; // assume write after this call
2761 string tag_name(PAR_TAG const & pt) {
2763 case NONE: return "!-- --";
2764 case TT: return "tt";
2765 case SF: return "sf";
2766 case BF: return "bf";
2767 case IT: return "it";
2768 case SL: return "sl";
2769 case EM: return "em";
2776 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
2778 p1 = static_cast<PAR_TAG>(p1 | p2);
2783 void reset(PAR_TAG & p1, PAR_TAG const & p2)
2785 p1 = static_cast<PAR_TAG>( p1 & ~p2);
2793 // Handle internal paragraph parsing -- layout already processed.
2794 void Buffer::simpleLinuxDocOnePar(ostream & os,
2796 Paragraph::depth_type /*depth*/)
2798 LyXLayout const & style = textclasslist.Style(params.textclass,
2800 string::size_type char_line_count = 5; // Heuristic choice ;-)
2802 // gets paragraph main font
2805 if (style.labeltype == LABEL_MANUAL) {
2806 font_old = style.labelfont;
2809 font_old = style.font;
2813 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
2814 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
2815 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
2818 stack<PAR_TAG> tag_state;
2819 // parsing main loop
2820 for (Paragraph::size_type i = 0; i < par->size(); ++i) {
2822 PAR_TAG tag_close = NONE;
2823 list < PAR_TAG > tag_open;
2825 LyXFont const font = par->getFont(params, i);
2827 if (font_old.family() != font.family()) {
2828 switch (family_type) {
2829 case LyXFont::SANS_FAMILY:
2832 case LyXFont::TYPEWRITER_FAMILY:
2839 family_type = font.family();
2841 switch (family_type) {
2842 case LyXFont::SANS_FAMILY:
2843 tag_open.push_back(SF);
2845 case LyXFont::TYPEWRITER_FAMILY:
2846 tag_open.push_back(TT);
2853 if (font_old.series() != font.series()) {
2854 switch (series_type) {
2855 case LyXFont::BOLD_SERIES:
2862 series_type = font.series();
2864 switch (series_type) {
2865 case LyXFont::BOLD_SERIES:
2866 tag_open.push_back(BF);
2874 if (font_old.shape() != font.shape()) {
2875 switch (shape_type) {
2876 case LyXFont::ITALIC_SHAPE:
2879 case LyXFont::SLANTED_SHAPE:
2886 shape_type = font.shape();
2888 switch (shape_type) {
2889 case LyXFont::ITALIC_SHAPE:
2890 tag_open.push_back(IT);
2892 case LyXFont::SLANTED_SHAPE:
2893 tag_open.push_back(SL);
2900 if (font_old.emph() != font.emph()) {
2901 if (font.emph() == LyXFont::ON) {
2902 tag_open.push_back(EM);
2911 list < PAR_TAG > temp;
2912 while(!tag_state.empty() && tag_close ) {
2913 PAR_TAG k = tag_state.top();
2915 os << "</" << tag_name(k) << ">";
2922 for(list< PAR_TAG >::const_iterator j = temp.begin();
2923 j != temp.end(); ++j) {
2925 os << "<" << tag_name(*j) << ">";
2928 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
2929 j != tag_open.end(); ++j) {
2931 os << "<" << tag_name(*j) << ">";
2934 char c = par->getChar(i);
2936 if (c == Paragraph::META_INSET) {
2937 Inset * inset = par->getInset(i);
2938 inset->linuxdoc(this, os);
2943 if (style.latexparam() == "CDATA") {
2944 // "TeX"-Mode on == > SGML-Mode on.
2950 if (par->linuxDocConvertChar(c, sgml_string)
2951 && !style.free_spacing) {
2952 // in freespacing mode, spaces are
2953 // non-breaking characters
2954 if (desc_on) {// if char is ' ' then...
2957 linux_doc_line_break(os, char_line_count, 6);
2961 linux_doc_line_break(os, char_line_count, 1);
2966 char_line_count += sgml_string.length();
2972 while (!tag_state.empty()) {
2973 os << "</" << tag_name(tag_state.top()) << ">";
2977 // resets description flag correctly
2979 // <tag> not closed...
2980 linux_doc_line_break(os, char_line_count, 6);
2986 // Print an error message.
2987 void Buffer::linuxDocError(Paragraph * par, int pos,
2988 string const & message)
2990 // insert an error marker in text
2991 InsetError * new_inset = new InsetError(message);
2992 par->insertInset(pos, new_inset);
2996 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
2998 ofstream ofs(fname.c_str());
3000 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3004 Paragraph * par = paragraph;
3006 niceFile = nice; // this will be used by Insetincludes.
3008 LyXTextClass const & tclass =
3009 textclasslist.TextClass(params.textclass);
3011 LaTeXFeatures features(params, tclass.numLayouts());
3016 string top_element = textclasslist.LatexnameOfClass(params.textclass);
3019 string sgml_includedfiles = features.getIncludedFiles(fname);
3021 ofs << "<!doctype " << top_element
3022 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
3024 if (params.preamble.empty() && sgml_includedfiles.empty())
3027 ofs << "\n [ " << params.preamble
3028 << sgml_includedfiles << " \n]>\n\n";
3031 string top = top_element;
3033 top += params.language->code();
3036 if (!params.options.empty()) {
3038 top += params.options;
3040 sgmlOpenTag(ofs, 0, top);
3042 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
3043 << "\n See http://www.lyx.org/ for more information -->\n";
3045 vector<string> environment_stack(10);
3046 vector<string> environment_inner(10);
3047 vector<string> command_stack(10);
3049 bool command_flag = false;
3050 Paragraph::depth_type command_depth = 0;
3051 Paragraph::depth_type command_base = 0;
3052 Paragraph::depth_type cmd_depth = 0;
3053 Paragraph::depth_type depth = 0; // paragraph depth
3056 string command_name;
3062 int desc_on = 0; // description mode
3064 LyXLayout const & style =
3065 textclasslist.Style(params.textclass,
3068 // environment tag closing
3069 for (; depth > par->params().depth(); --depth) {
3070 if (environment_inner[depth] != "!-- --") {
3071 item_name = "listitem";
3072 sgmlCloseTag(ofs, command_depth + depth,
3074 if (environment_inner[depth] == "varlistentry")
3075 sgmlCloseTag(ofs, depth+command_depth,
3076 environment_inner[depth]);
3078 sgmlCloseTag(ofs, depth + command_depth,
3079 environment_stack[depth]);
3080 environment_stack[depth].erase();
3081 environment_inner[depth].erase();
3084 if (depth == par->params().depth()
3085 && environment_stack[depth] != style.latexname()
3086 && !environment_stack[depth].empty()) {
3087 if (environment_inner[depth] != "!-- --") {
3088 item_name= "listitem";
3089 sgmlCloseTag(ofs, command_depth+depth,
3091 if (environment_inner[depth] == "varlistentry")
3093 depth + command_depth,
3094 environment_inner[depth]);
3097 sgmlCloseTag(ofs, depth + command_depth,
3098 environment_stack[depth]);
3100 environment_stack[depth].erase();
3101 environment_inner[depth].erase();
3104 // Write opening SGML tags.
3105 switch (style.latextype) {
3106 case LATEX_PARAGRAPH:
3107 sgmlOpenTag(ofs, depth + command_depth,
3113 linuxDocError(par, 0,
3114 _("Error : Wrong depth for "
3115 "LatexType Command.\n"));
3117 command_name = style.latexname();
3119 sgmlparam = style.latexparam();
3120 c_params = split(sgmlparam, c_depth,'|');
3122 cmd_depth = lyx::atoi(c_depth);
3125 if (cmd_depth < command_base) {
3126 for (Paragraph::depth_type j = command_depth; j >= command_base; --j)
3127 sgmlCloseTag(ofs, j, command_stack[j]);
3128 command_depth = command_base = cmd_depth;
3129 } else if (cmd_depth <= command_depth) {
3130 for (int j = command_depth; j >= int(cmd_depth); --j)
3131 sgmlCloseTag(ofs, j, command_stack[j]);
3132 command_depth = cmd_depth;
3134 command_depth = cmd_depth;
3136 command_depth = command_base = cmd_depth;
3137 command_flag = true;
3139 if (command_stack.size() == command_depth + 1)
3140 command_stack.push_back(string());
3141 command_stack[command_depth] = command_name;
3143 // treat label as a special case for
3144 // more WYSIWYM handling.
3145 if (par->getChar(0) == Paragraph::META_INSET) {
3146 Inset * inset = par->getInset(0);
3147 Inset::Code lyx_code = inset->lyxCode();
3148 if (lyx_code == Inset::LABEL_CODE){
3149 command_name += " id=\"";
3150 command_name += (static_cast<InsetCommand *>(inset))->getContents();
3151 command_name += "\"";
3156 sgmlOpenTag(ofs, depth + command_depth, command_name);
3157 if (c_params.empty())
3158 item_name = "title";
3160 item_name = c_params;
3161 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3164 case LATEX_ENVIRONMENT:
3165 case LATEX_ITEM_ENVIRONMENT:
3166 if (depth < par->params().depth()) {
3167 depth = par->params().depth();
3168 environment_stack[depth].erase();
3171 if (environment_stack[depth] != style.latexname()) {
3172 if(environment_stack.size() == depth + 1) {
3173 environment_stack.push_back("!-- --");
3174 environment_inner.push_back("!-- --");
3176 environment_stack[depth] = style.latexname();
3177 environment_inner[depth] = "!-- --";
3178 sgmlOpenTag(ofs, depth + command_depth,
3179 environment_stack[depth]);
3181 if (environment_inner[depth] != "!-- --") {
3182 item_name= "listitem";
3184 command_depth + depth,
3186 if (environment_inner[depth] == "varlistentry")
3188 depth + command_depth,
3189 environment_inner[depth]);
3193 if (style.latextype == LATEX_ENVIRONMENT) {
3194 if (!style.latexparam().empty()) {
3195 if(style.latexparam() == "CDATA")
3198 sgmlOpenTag(ofs, depth + command_depth,
3199 style.latexparam());
3204 desc_on = (style.labeltype == LABEL_MANUAL);
3207 environment_inner[depth]= "varlistentry";
3209 environment_inner[depth]= "listitem";
3211 sgmlOpenTag(ofs, depth + 1 + command_depth,
3212 environment_inner[depth]);
3216 sgmlOpenTag(ofs, depth + 1 + command_depth,
3220 sgmlOpenTag(ofs, depth + 1 + command_depth,
3225 sgmlOpenTag(ofs, depth + command_depth,
3231 simpleDocBookOnePar(ofs, extra_par, par, desc_on,
3232 depth + 1 + command_depth);
3236 // write closing SGML tags
3237 switch (style.latextype) {
3239 if (c_params.empty())
3243 sgmlCloseTag(ofs, depth + command_depth, end_tag);
3245 case LATEX_ENVIRONMENT:
3246 if (!style.latexparam().empty()) {
3247 if(style.latexparam() == "CDATA")
3250 sgmlCloseTag(ofs, depth + command_depth,
3251 style.latexparam());
3254 case LATEX_ITEM_ENVIRONMENT:
3255 if (desc_on == 1) break;
3257 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3259 case LATEX_PARAGRAPH:
3260 sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3263 sgmlCloseTag(ofs, depth + command_depth, style.latexname());
3269 for (int d = depth; d >= 0; --d) {
3270 if (!environment_stack[depth].empty()) {
3271 if (environment_inner[depth] != "!-- --") {
3272 item_name = "listitem";
3273 sgmlCloseTag(ofs, command_depth + depth,
3275 if (environment_inner[depth] == "varlistentry")
3276 sgmlCloseTag(ofs, depth + command_depth,
3277 environment_inner[depth]);
3280 sgmlCloseTag(ofs, depth + command_depth,
3281 environment_stack[depth]);
3285 for (int j = command_depth; j >= 0 ; --j)
3286 if (!command_stack[j].empty())
3287 sgmlCloseTag(ofs, j, command_stack[j]);
3290 sgmlCloseTag(ofs, 0, top_element);
3293 // How to check for successful close
3297 void Buffer::simpleDocBookOnePar(ostream & os, string & extra,
3298 Paragraph * par, int & desc_on,
3299 Paragraph::depth_type depth) const
3301 bool emph_flag = false;
3303 LyXLayout const & style = textclasslist.Style(params.textclass,
3306 LyXFont font_old = style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
3308 int char_line_count = depth;
3309 //if (!style.free_spacing)
3310 // os << string(depth,' ');
3312 // parsing main loop
3313 for (Paragraph::size_type i = 0;
3314 i < par->size(); ++i) {
3315 LyXFont font = par->getFont(params, i);
3317 // handle <emphasis> tag
3318 if (font_old.emph() != font.emph()) {
3319 if (font.emph() == LyXFont::ON) {
3323 os << "</emphasis>";
3328 char c = par->getChar(i);
3330 if (c == Paragraph::META_INSET) {
3331 Inset * inset = par->getInset(i);
3333 inset->docBook(this, ost);
3334 string tmp_out = ost.str().c_str();
3337 // This code needs some explanation:
3338 // Two insets are treated specially
3339 // label if it is the first element in a command paragraph
3341 // graphics inside tables or figure floats can't go on
3342 // title (the equivalente in latex for this case is caption
3343 // and title should come first
3346 if (desc_on!= 3 || i!= 0) {
3347 if (!tmp_out.empty() && tmp_out[0] == '@') {
3349 extra += frontStrip(tmp_out, '@');
3351 os << frontStrip(tmp_out, '@');
3358 if (par->linuxDocConvertChar(c, sgml_string)
3359 && !style.free_spacing) { // in freespacing
3361 // non-breaking characters
3365 os << "\n</term><listitem><para>";
3378 os << "</emphasis>";
3381 // resets description flag correctly
3383 // <term> not closed...
3386 if(style.free_spacing) os << '\n';
3390 // This should be enabled when the Chktex class is implemented. (Asger)
3391 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
3392 // Other flags: -wall -v0 -x
3393 int Buffer::runChktex()
3395 if (!users->text) return 0;
3397 users->owner()->prohibitInput();
3399 // get LaTeX-Filename
3400 string const name = getLatexName();
3401 string path = OnlyPath(filename);
3403 string const org_path = path;
3404 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
3408 Path p(path); // path to LaTeX file
3409 users->owner()->message(_("Running chktex..."));
3411 // Remove all error insets
3412 bool const removedErrorInsets = users->removeAutoInsets();
3414 // Generate the LaTeX file if neccessary
3415 makeLaTeXFile(name, org_path, false);
3418 Chktex chktex(lyxrc.chktex_command, name, filepath);
3419 int res = chktex.run(terr); // run chktex
3422 WriteAlert(_("chktex did not work!"),
3423 _("Could not run with file:"), name);
3424 } else if (res > 0) {
3425 // Insert all errors as errors boxes
3426 users->insertErrors(terr);
3429 // if we removed error insets before we ran chktex or if we inserted
3430 // error insets after we ran chktex, this must be run:
3431 if (removedErrorInsets || res){
3435 users->owner()->allowInput();
3441 void Buffer::validate(LaTeXFeatures & features) const
3443 Paragraph * par = paragraph;
3444 LyXTextClass const & tclass =
3445 textclasslist.TextClass(params.textclass);
3447 // AMS Style is at document level
3448 features.amsstyle = (params.use_amsmath ||
3449 tclass.provides(LyXTextClass::amsmath));
3452 // We don't use "lyxerr.debug" because of speed. (Asger)
3453 if (lyxerr.debugging(Debug::LATEX))
3454 lyxerr << "Paragraph: " << par << endl;
3456 // Now just follow the list of paragraphs and run
3457 // validate on each of them.
3458 par->validate(features);
3460 // and then the next paragraph
3464 // the bullet shapes are buffer level not paragraph level
3465 // so they are tested here
3466 for (int i = 0; i < 4; ++i) {
3467 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
3468 int const font = params.user_defined_bullets[i].getFont();
3470 int const c = params
3471 .user_defined_bullets[i]
3478 features.latexsym = true;
3480 } else if (font == 1) {
3481 features.amssymb = true;
3482 } else if ((font >= 2 && font <= 5)) {
3483 features.pifont = true;
3488 if (lyxerr.debugging(Debug::LATEX)) {
3489 features.showStruct();
3494 void Buffer::setPaperStuff()
3496 params.papersize = BufferParams::PAPER_DEFAULT;
3497 char const c1 = params.paperpackage;
3498 if (c1 == BufferParams::PACKAGE_NONE) {
3499 char const c2 = params.papersize2;
3500 if (c2 == BufferParams::VM_PAPER_USLETTER)
3501 params.papersize = BufferParams::PAPER_USLETTER;
3502 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
3503 params.papersize = BufferParams::PAPER_LEGALPAPER;
3504 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
3505 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
3506 else if (c2 == BufferParams::VM_PAPER_A3)
3507 params.papersize = BufferParams::PAPER_A3PAPER;
3508 else if (c2 == BufferParams::VM_PAPER_A4)
3509 params.papersize = BufferParams::PAPER_A4PAPER;
3510 else if (c2 == BufferParams::VM_PAPER_A5)
3511 params.papersize = BufferParams::PAPER_A5PAPER;
3512 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
3513 (c2 == BufferParams::VM_PAPER_B5))
3514 params.papersize = BufferParams::PAPER_B5PAPER;
3515 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
3516 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
3517 params.papersize = BufferParams::PAPER_A4PAPER;
3521 // This function should be in Buffer because it's a buffer's property (ale)
3522 string const Buffer::getIncludeonlyList(char delim)
3525 for (inset_iterator it = inset_iterator_begin();
3526 it != inset_iterator_end(); ++it) {
3527 if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3528 InsetInclude * insetinc =
3529 static_cast<InsetInclude *>(*it);
3530 if (insetinc->isIncludeOnly()) {
3533 lst += insetinc->getRelFileBaseName();
3537 lyxerr[Debug::INFO] << "Includeonly(" << lst << ')' << endl;
3542 vector<string> const Buffer::getLabelList()
3544 /// if this is a child document and the parent is already loaded
3545 /// Use the parent's list instead [ale990407]
3546 if (!params.parentname.empty()
3547 && bufferlist.exists(params.parentname)) {
3548 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3550 return tmp->getLabelList();
3553 vector<string> label_list;
3554 for (inset_iterator it = inset_iterator_begin();
3555 it != inset_iterator_end(); ++it) {
3556 vector<string> const l = (*it)->getLabelList();
3557 label_list.insert(label_list.end(), l.begin(), l.end());
3563 Buffer::Lists const Buffer::getLists() const
3566 Paragraph * par = paragraph;
3568 LyXTextClassList::size_type cap;
3569 boost::tie(found, cap) = textclasslist
3570 .NumberOfLayout(params.textclass, "Caption");
3573 char const labeltype =
3574 textclasslist.Style(params.textclass,
3575 par->getLayout()).labeltype;
3577 if (labeltype >= LABEL_COUNTER_CHAPTER
3578 && labeltype <= LABEL_COUNTER_CHAPTER + params.tocdepth) {
3579 // insert this into the table of contents
3580 SingleList & item = l["TOC"];
3583 textclasslist.TextClass(params.textclass).maxcounter());
3584 item.push_back(TocItem(par, depth, par->asString(this, true)));
3586 // For each paragrph, traverse its insets and look for
3590 Paragraph::inset_iterator it =
3591 par->inset_iterator_begin();
3592 Paragraph::inset_iterator end =
3593 par->inset_iterator_end();
3595 for (; it != end; ++it) {
3596 if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
3598 static_cast<InsetFloat*>(*it);
3600 string const type = il->type();
3602 // Now find the caption in the float...
3603 // We now tranverse the paragraphs of
3605 Paragraph * tmp = il->inset.paragraph();
3607 if (tmp->layout == cap) {
3608 SingleList & item = l[type];
3610 tostr(item.size()+1) + ". " + tmp->asString(this, false);
3611 item.push_back(TocItem(tmp, 0 , str));
3618 lyxerr << "caption not found" << endl;
3627 // This is also a buffer property (ale)
3628 vector<pair<string, string> > const Buffer::getBibkeyList()
3630 /// if this is a child document and the parent is already loaded
3631 /// Use the parent's list instead [ale990412]
3632 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
3633 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3635 return tmp->getBibkeyList();
3638 vector<pair<string, string> > keys;
3639 Paragraph * par = paragraph;
3642 keys.push_back(pair<string, string>(par->bibkey->getContents(),
3643 par->asString(this, false)));
3647 // Might be either using bibtex or a child has bibliography
3649 for (inset_iterator it = inset_iterator_begin();
3650 it != inset_iterator_end(); ++it) {
3651 // Search for Bibtex or Include inset
3652 if ((*it)->lyxCode() == Inset::BIBTEX_CODE) {
3653 vector<pair<string,string> > tmp =
3654 static_cast<InsetBibtex*>(*it)->getKeys(this);
3655 keys.insert(keys.end(), tmp.begin(), tmp.end());
3656 } else if ((*it)->lyxCode() == Inset::INCLUDE_CODE) {
3657 vector<pair<string,string> > const tmp =
3658 static_cast<InsetInclude*>(*it)->getKeys();
3659 keys.insert(keys.end(), tmp.begin(), tmp.end());
3668 bool Buffer::isDepClean(string const & name) const
3670 DEPCLEAN * item = dep_clean;
3671 while (item && item->master != name)
3673 if (!item) return true;
3678 void Buffer::markDepClean(string const & name)
3681 dep_clean = new DEPCLEAN;
3682 dep_clean->clean = true;
3683 dep_clean->master = name;
3684 dep_clean->next = 0;
3686 DEPCLEAN * item = dep_clean;
3687 while (item && item->master != name)
3692 item = new DEPCLEAN;
3694 item->master = name;
3701 bool Buffer::dispatch(string const & command)
3703 // Split command string into command and argument
3705 string line = frontStrip(command);
3706 string const arg = strip(frontStrip(split(line, cmd, ' ')));
3708 return dispatch(lyxaction.LookupFunc(cmd), arg);
3712 bool Buffer::dispatch(int action, string const & argument)
3714 bool dispatched = true;
3717 Exporter::Export(this, argument, false);
3727 void Buffer::resizeInsets(BufferView * bv)
3729 /// then remove all LyXText in text-insets
3730 Paragraph * par = paragraph;
3731 for (; par; par = par->next()) {
3732 par->resizeInsetsLyXText(bv);
3737 void Buffer::redraw()
3744 void Buffer::changeLanguage(Language const * from, Language const * to)
3747 Paragraph * par = paragraph;
3749 par->changeLanguage(params, from, to);
3755 bool Buffer::isMultiLingual()
3757 Paragraph * par = paragraph;
3759 if (par->isMultiLingual(params))
3767 Buffer::inset_iterator::inset_iterator(Paragraph * paragraph,
3768 Paragraph::size_type pos)
3771 it = par->InsetIterator(pos);
3772 if (it == par->inset_iterator_end()) {
3779 void Buffer::inset_iterator::setParagraph()
3782 it = par->inset_iterator_begin();
3783 if (it != par->inset_iterator_end())
3788 // We maintain an invariant that whenever par = 0 then it = 0
3792 Inset * Buffer::getInsetFromID(int id_arg) const
3794 for (inset_iterator it = inset_const_iterator_begin();
3795 it != inset_const_iterator_end(); ++it)
3797 if ((*it)->id() == id_arg)
3799 Inset * in = (*it)->getInsetFromID(id_arg);
3807 Paragraph * Buffer::getParFromID(int id) const
3809 if (id < 0) return 0;
3810 Paragraph * par = paragraph;
3812 if (par->id() == id) {
3815 Paragraph * tmp = par->getParFromID(id);