1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * This file is Copyright 1996-1999
12 * ======================================================
17 // 23/03/98 Heinrich Bauer (heinrich.bauer@t-mobil.de)
18 // Spots marked "changed Heinrich Bauer, 23/03/98" modified due to the
19 // following bug: dvi file export did not work after printing (or previewing)
20 // and vice versa as long as the same file was concerned. This happened
21 // every time the LyX-file was left unchanged between the two actions mentioned
39 #pragma implementation "buffer.h"
43 #include "bufferlist.h"
45 #include "lyx_gui_misc.h"
46 #include "LyXAction.h"
49 #include "tex-strings.h"
52 #include "minibuffer.h"
55 #include "mathed/formulamacro.h"
56 #include "insets/lyxinset.h"
57 #include "insets/inseterror.h"
58 #include "insets/insetlabel.h"
59 #include "insets/insetref.h"
60 #include "insets/inseturl.h"
61 #include "insets/insetinfo.h"
62 #include "insets/insetquotes.h"
63 #include "insets/insetlatexaccent.h"
64 #include "insets/insetbib.h"
65 #include "insets/insetindex.h"
66 #include "insets/insetinclude.h"
67 #include "insets/insettoc.h"
68 #include "insets/insetlof.h"
69 #include "insets/insetlot.h"
70 #include "insets/insetloa.h"
71 #include "insets/insetparent.h"
72 #include "insets/insetspecialchar.h"
73 #include "insets/figinset.h"
74 #include "support/filetools.h"
75 #include "support/path.h"
81 #include "LaTeXFeatures.h"
82 #include "support/syscall.h"
83 #include "support/lyxlib.h"
84 #include "support/FileInfo.h"
88 // Uncomment this line to enable a workaround for the weird behaviour
89 // of the cursor between a displayed inset and last character
90 // at the upper line. (Alejandro 20.9.96)
91 // #define BREAK_BEFORE_INSET
93 /* Lars, when changing this file sometime in the future to a list,
94 * please check out the figinsets-sideeffects described at the
95 * beginning of figinset.C Matthias (04.07.1996)
99 // all these externs should eventually be removed.
100 extern BufferList bufferlist;
102 extern void MenuExport(Buffer *, string const &);
103 extern LyXAction lyxaction;
106 static const float LYX_FORMAT = 2.15;
108 extern int tex_code_break_column;
110 extern void FreeUpdateTimer();
113 Buffer::Buffer(string const & file, LyXRC * lyxrc, bool ronly)
115 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
117 filepath = OnlyPath(file);
121 dvi_clean_orgd = false; // Heinrich Bauer, 23/03/98
122 dvi_clean_tmpd = false; // Heinrich Bauer, 23/03/98
127 if (read_only || (lyxrc && lyxrc->use_tempdir)) {
128 tmppath = CreateBufferTmpDir();
129 } else tmppath.clear();
135 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
136 // here the buffer should take care that it is
137 // saved properly, before it goes into the void.
139 // make sure that views using this buffer
144 if (!tmppath.empty()) {
145 DestroyBufferTmpDir(tmppath);
148 LyXParagraph * par = paragraph;
149 LyXParagraph * tmppar;
159 bool Buffer::saveParamsAsDefaults()
161 string fname = AddName(AddPath(user_lyxdir, "templates/"),
163 Buffer defaults = Buffer(fname);
165 // Use the current buffer's parameters as default
166 defaults.params = params;
168 // add an empty paragraph. Is this enough?
169 defaults.paragraph = new LyXParagraph;
171 return defaults.writeFile(defaults.filename, false);
175 /// Update window titles of all users
176 // Should work on a list
177 void Buffer::updateTitles()
179 if (users) users->owner()->updateWindowTitle();
183 /// Reset autosave timer of all users
184 // Should work on a list
185 void Buffer::resetAutosaveTimers()
187 if (users) users->owner()->resetAutosaveTimer();
191 void Buffer::fileName(string const & newfile)
193 filename = MakeAbsPath(newfile);
194 filepath = OnlyPath(filename);
195 setReadonly(IsFileWriteable(filename) == 0);
200 // candidate for move to BufferView
201 // (at least some parts in the beginning of the func)
204 // changed to be public and have one parameter
205 // if par = 0 normal behavior
206 // else insert behavior
207 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
208 bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
214 char depth = 0; // signed or unsigned?
215 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
216 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
217 bool the_end_read = false;
219 LyXParagraph * return_par = 0;
220 LyXFont font = LyXFont(LyXFont::ALL_INHERIT);
222 // If we are inserting, we cheat and get a token in advance
223 bool has_token = false;
227 par = new LyXParagraph;
229 users->text->BreakParagraph();
230 return_par = users->text->FirstParagraph();
233 // We don't want to adopt the parameters from the
234 // document we insert, so we skip until the text begins:
237 pretoken = lex.GetString();
238 if (pretoken == "\\layout") {
250 pretoken = lex.GetString();
253 // Profiling show this should give a lot: (Asger)
254 string const token = pretoken;
258 else if (token[0] != '\\') {
259 int n = token.length();
260 for (int i = 0; i < n; ++i) {
261 par->InsertChar(pos, token[i]);
262 par->SetFont(pos, font);
265 } else if (token == "\\i") {
266 inset = new InsetLatexAccent;
268 par->InsertChar(pos, LyXParagraph::META_INSET);
270 par->InsertInset(pos, inset);
271 par->SetFont(pos, font);
273 } else if (token == "\\layout") {
277 par->text.resize(par->text.size());
278 par = new LyXParagraph(par);
282 string layoutname = lex.GetString();
283 pair<bool, LyXTextClass::LayoutList::size_type> pp
284 = textclasslist.NumberOfLayout(params.textclass,
287 par->layout = pp.second;
288 } else { // layout not found
289 // use default layout "Standard" (0)
292 // Test whether the layout is obsolete.
293 LyXLayout const & layout = textclasslist.Style(params.textclass,
295 if (!layout.obsoleted_by().empty())
297 textclasslist.NumberOfLayout(params.textclass,
298 layout.obsoleted_by()).second;
299 par->footnoteflag = footnoteflag;
300 par->footnotekind = footnotekind;
302 font = LyXFont(LyXFont::ALL_INHERIT);
303 } else if (token == "\\end_float") {
307 par->text.resize(par->text.size());
308 par = new LyXParagraph(par);
310 footnotekind = LyXParagraph::FOOTNOTE;
311 footnoteflag = LyXParagraph::NO_FOOTNOTE;
314 par->layout = LYX_DUMMY_LAYOUT;
315 font = LyXFont(LyXFont::ALL_INHERIT);
316 } else if (token == "\\begin_float") {
317 tmpret = lex.FindToken(string_footnotekinds);
318 if (tmpret == -1) ++tmpret;
319 if (tmpret != LYX_LAYOUT_DEFAULT)
320 footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
321 if (footnotekind == LyXParagraph::FOOTNOTE
322 || footnotekind == LyXParagraph::MARGIN)
323 footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
325 footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
326 } else if (token == "\\begin_deeper") {
328 } else if (token == "\\end_deeper") {
330 lex.printError("\\end_deeper: "
331 "depth is already null");
335 } else if (token == "\\begin_preamble") {
336 params.readPreamble(lex);
337 } else if (token == "\\textclass") {
339 pair<bool, LyXTextClassList::size_type> pp =
340 textclasslist.NumberOfClass(lex.GetString());
342 params.textclass = pp.second;
344 lex.printError("Unknown textclass `$$Token'");
345 params.textclass = 0;
347 if (!textclasslist.Load(params.textclass)) {
348 // if the textclass wasn't loaded properly
349 // we need to either substitute another
350 // or stop loading the file.
351 // I can substitute but I don't see how I can
352 // stop loading... ideas?? ARRae980418
353 WriteAlert(_("Textclass Loading Error!"),
354 string(_("Can't load textclass ")) +
355 textclasslist.NameOfClass(params.textclass),
356 _("-- substituting default"));
357 params.textclass = 0;
359 } else if (token == "\\options") {
361 params.options = lex.GetString();
362 } else if (token == "\\language") {
363 params.readLanguage(lex);
364 } else if (token == "\\fontencoding") {
366 } else if (token == "\\inputencoding") {
368 params.inputenc = lex.GetString();
369 } else if (token == "\\graphics") {
370 params.readGraphicsDriver(lex);
371 } else if (token == "\\fontscheme") {
373 params.fonts = lex.GetString();
374 } else if (token == "\\noindent") {
375 par->noindent = true;
376 } else if (token == "\\fill_top") {
377 par->added_space_top = VSpace(VSpace::VFILL);
378 } else if (token == "\\fill_bottom") {
379 par->added_space_bottom = VSpace(VSpace::VFILL);
380 } else if (token == "\\line_top") {
381 par->line_top = true;
382 } else if (token == "\\line_bottom") {
383 par->line_bottom = true;
384 } else if (token == "\\pagebreak_top") {
385 par->pagebreak_top = true;
386 } else if (token == "\\pagebreak_bottom") {
387 par->pagebreak_bottom = true;
388 } else if (token == "\\start_of_appendix") {
389 par->start_of_appendix = true;
390 } else if (token == "\\paragraph_separation") {
391 tmpret = lex.FindToken(string_paragraph_separation);
392 if (tmpret == -1) ++tmpret;
393 if (tmpret != LYX_LAYOUT_DEFAULT)
394 params.paragraph_separation =
395 static_cast<BufferParams::PARSEP>(tmpret);
396 } else if (token == "\\defskip") {
398 params.defskip = VSpace(lex.GetString());
399 } else if (token == "\\no_isolatin1") { // obsolete
401 } else if (token == "\\no_babel") { // obsolete
403 } else if (token == "\\no_epsfig") { // obsolete
405 } else if (token == "\\epsfig") { // obsolete
406 // Indeed it is obsolete, but we HAVE to be backwards
407 // compatible until 0.14, because otherwise all figures
408 // in existing documents are irretrivably lost. (Asger)
409 params.readGraphicsDriver(lex);
410 } else if (token == "\\quotes_language") {
411 tmpret = lex.FindToken(string_quotes_language);
412 if (tmpret == -1) ++tmpret;
413 if (tmpret != LYX_LAYOUT_DEFAULT) {
414 InsetQuotes::quote_language tmpl =
415 InsetQuotes::EnglishQ;
418 tmpl = InsetQuotes::EnglishQ;
421 tmpl = InsetQuotes::SwedishQ;
424 tmpl = InsetQuotes::GermanQ;
427 tmpl = InsetQuotes::PolishQ;
430 tmpl = InsetQuotes::FrenchQ;
433 tmpl = InsetQuotes::DanishQ;
436 params.quotes_language = tmpl;
438 } else if (token == "\\quotes_times") {
440 switch(lex.GetInteger()) {
442 params.quotes_times = InsetQuotes::SingleQ;
445 params.quotes_times = InsetQuotes::DoubleQ;
448 } else if (token == "\\papersize") {
449 tmpret = lex.FindToken(string_papersize);
453 params.papersize2 = tmpret;
454 } else if (token == "\\paperpackage") {
455 tmpret = lex.FindToken(string_paperpackages);
458 params.paperpackage = BufferParams::PACKAGE_NONE;
460 params.paperpackage = tmpret;
461 } else if (token == "\\use_geometry") {
463 params.use_geometry = lex.GetInteger();
464 } else if (token == "\\use_amsmath") {
466 params.use_amsmath = lex.GetInteger();
467 } else if (token == "\\paperorientation") {
468 tmpret = lex.FindToken(string_orientation);
469 if (tmpret == -1) ++tmpret;
470 if (tmpret != LYX_LAYOUT_DEFAULT)
471 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
472 } else if (token == "\\paperwidth") {
474 params.paperwidth = lex.GetString();
475 } else if (token == "\\paperheight") {
477 params.paperheight = lex.GetString();
478 } else if (token == "\\leftmargin") {
480 params.leftmargin = lex.GetString();
481 } else if (token == "\\topmargin") {
483 params.topmargin = lex.GetString();
484 } else if (token == "\\rightmargin") {
486 params.rightmargin = lex.GetString();
487 } else if (token == "\\bottommargin") {
489 params.bottommargin = lex.GetString();
490 } else if (token == "\\headheight") {
492 params.headheight = lex.GetString();
493 } else if (token == "\\headsep") {
495 params.headsep = lex.GetString();
496 } else if (token == "\\footskip") {
498 params.footskip = lex.GetString();
499 } else if (token == "\\paperfontsize") {
501 params.fontsize = strip(lex.GetString());
502 } else if (token == "\\papercolumns") {
504 params.columns = lex.GetInteger();
505 } else if (token == "\\papersides") {
507 switch(lex.GetInteger()) {
509 case 1: params.sides = LyXTextClass::OneSide; break;
510 case 2: params.sides = LyXTextClass::TwoSides; break;
512 } else if (token == "\\paperpagestyle") {
514 params.pagestyle = strip(lex.GetString());
515 } else if (token == "\\bullet") {
517 int index = lex.GetInteger();
519 int temp_int = lex.GetInteger();
520 params.user_defined_bullets[index].setFont(temp_int);
521 params.temp_bullets[index].setFont(temp_int);
523 temp_int = lex.GetInteger();
524 params.user_defined_bullets[index].setCharacter(temp_int);
525 params.temp_bullets[index].setCharacter(temp_int);
527 temp_int = lex.GetInteger();
528 params.user_defined_bullets[index].setSize(temp_int);
529 params.temp_bullets[index].setSize(temp_int);
531 string temp_str = lex.GetString();
532 if (temp_str != "\\end_bullet") {
533 // this element isn't really necessary for
534 // parsing but is easier for humans
535 // to understand bullets. Put it back and
536 // set a debug message?
537 lex.printError("\\end_bullet expected, got" + temp_str);
538 //how can I put it back?
540 } else if (token == "\\bulletLaTeX") {
542 int index = lex.GetInteger();
544 string temp_str = lex.GetString(), sum_str;
545 while (temp_str != "\\end_bullet") {
546 // this loop structure is needed when user
547 // enters an empty string since the first
548 // thing returned will be the \\end_bullet
550 // if the LaTeX entry has spaces. Each element
551 // therefore needs to be read in turn
554 temp_str = lex.GetString();
556 params.user_defined_bullets[index].setText(sum_str);
557 params.temp_bullets[index].setText(sum_str);
558 } else if (token == "\\secnumdepth") {
560 params.secnumdepth = lex.GetInteger();
561 } else if (token == "\\tocdepth") {
563 params.tocdepth = lex.GetInteger();
564 } else if (token == "\\baselinestretch") { // obsolete
565 lex.nextToken(); // should not be used directly
567 // Will probably keep a kind of support just for
569 params.spacing.set(Spacing::Other, lex.GetFloat());
570 } else if (token == "\\spacing") {
572 string tmp = strip(lex.GetString());
573 if (tmp == "single") {
574 params.spacing.set(Spacing::Single);
575 } else if (tmp == "onehalf") {
576 params.spacing.set(Spacing::Onehalf);
577 } else if (tmp == "double") {
578 params.spacing.set(Spacing::Double);
579 } else if (tmp == "other") {
581 params.spacing.set(Spacing::Other,
584 lex.printError("Unknown spacing token: '$$Token'");
586 } else if (token == "\\float_placement") {
588 params.float_placement = lex.GetString();
589 } else if (token == "\\cursor") { // obsolete
590 // this is obsolete, so we just skip it.
592 } else if (token == "\\family") {
594 font.setLyXFamily(lex.GetString());
595 } else if (token == "\\series") {
597 font.setLyXSeries(lex.GetString());
598 } else if (token == "\\shape") {
600 font.setLyXShape(lex.GetString());
601 } else if (token == "\\size") {
603 font.setLyXSize(lex.GetString());
604 } else if (token == "\\latex") {
606 string tok = lex.GetString();
607 // This is dirty, but gone with LyX3. (Asger)
608 if (tok == "no_latex")
609 font.setLatex(LyXFont::OFF);
610 else if (tok == "latex")
611 font.setLatex(LyXFont::ON);
612 else if (tok == "default")
613 font.setLatex(LyXFont::INHERIT);
615 lex.printError("Unknown LaTeX font flag "
617 } else if (token == "\\direction") {
619 string tok = lex.GetString();
621 font.setDirection(LyXFont::LTR_DIR);
622 else if (tok == "rtl")
623 font.setDirection(LyXFont::RTL_DIR);
624 else if (tok == "default")
625 font.setDirection(LyXFont::INHERIT_DIR);
627 lex.printError("Unknown font flag "
629 } else if (token == "\\emph") {
631 font.setEmph(font.setLyXMisc(lex.GetString()));
632 } else if (token == "\\bar") {
634 string tok = lex.GetString();
635 // This is dirty, but gone with LyX3. (Asger)
637 font.setUnderbar(LyXFont::ON);
638 else if (tok == "no")
639 font.setUnderbar(LyXFont::OFF);
640 else if (tok == "default")
641 font.setUnderbar(LyXFont::INHERIT);
643 lex.printError("Unknown bar font flag "
645 } else if (token == "\\noun") {
647 font.setNoun(font.setLyXMisc(lex.GetString()));
648 } else if (token == "\\color") {
650 font.setLyXColor(lex.GetString());
651 } else if (token == "\\align") {
652 tmpret = lex.FindToken(string_align);
653 if (tmpret == -1) ++tmpret;
654 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
656 for (; tmpret > 0; --tmpret)
657 tmpret2 = tmpret2 * 2;
658 par->align = LyXAlignment(tmpret2);
660 } else if (token == "\\added_space_top") {
662 par->added_space_top = lex.GetString();
663 } else if (token == "\\added_space_bottom") {
665 par->added_space_bottom = lex.GetString();
666 } else if (token == "\\pextra_type") {
668 par->pextra_type = lex.GetInteger();
669 } else if (token == "\\pextra_width") {
671 par->pextra_width = lex.GetString();
672 } else if (token == "\\pextra_widthp") {
674 par->pextra_widthp = lex.GetString();
675 } else if (token == "\\pextra_alignment") {
677 par->pextra_alignment = lex.GetInteger();
678 } else if (token == "\\pextra_hfill") {
680 par->pextra_hfill = lex.GetInteger();
681 } else if (token == "\\pextra_start_minipage") {
683 par->pextra_start_minipage = lex.GetInteger();
684 } else if (token == "\\labelwidthstring") {
686 par->labelwidthstring = lex.GetString();
687 /* do not delete this token, it is still needed! */
688 } else if (token == "\\end_inset") {
689 /* simple ignore this. The insets do not have
691 // but insets should read it, it is a part of
692 //the inset isn't it? Lgb.
693 } else if (token == "\\begin_inset") {
695 tmptok = lex.GetString();
696 /* test the different insets */
697 if (tmptok == "Quotes") {
698 inset = new InsetQuotes;
700 par->InsertChar(pos, LyXParagraph::META_INSET);
701 par->InsertInset(pos, inset);
702 par->SetFont(pos, font);
704 } else if (tmptok == "\\i") {
705 inset = new InsetLatexAccent;
707 par->InsertChar(pos, LyXParagraph::META_INSET);
708 par->InsertInset(pos, inset);
709 par->SetFont(pos, font);
711 } else if (tmptok == "FormulaMacro") {
712 inset = new InsetFormulaMacro;
714 par->InsertChar(pos, LyXParagraph::META_INSET);
715 par->InsertInset(pos, inset);
716 par->SetFont(pos, font);
718 } else if (tmptok == "Formula") {
719 inset = new InsetFormula;
721 par->InsertChar(pos, LyXParagraph::META_INSET);
722 par->InsertInset(pos, inset);
723 par->SetFont(pos, font);
725 } else if (tmptok == "Figure") {
726 inset = new InsetFig(100, 100, this);
728 par->InsertChar(pos, LyXParagraph::META_INSET);
729 par->InsertInset(pos, inset);
730 par->SetFont(pos, font);
732 } else if (tmptok == "Info") {
733 inset = new InsetInfo;
735 par->InsertChar(pos, LyXParagraph::META_INSET);
736 par->InsertInset(pos, inset);
737 par->SetFont(pos, font);
739 } else if (tmptok == "Include") {
740 inset = new InsetInclude(string(), this);
742 par->InsertChar(pos, LyXParagraph::META_INSET);
743 par->InsertInset(pos, inset);
744 par->SetFont(pos, font);
746 } else if (tmptok == "LatexCommand") {
749 if (inscmd.getCmdName() == "cite") {
750 inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
751 } else if (inscmd.getCmdName() == "bibitem") {
752 lex.printError("Wrong place for bibitem");
753 inset = inscmd.Clone();
754 } else if (inscmd.getCmdName() == "BibTeX") {
755 inset = new InsetBibtex(inscmd.getContents(), inscmd.getOptions(), this);
756 } else if (inscmd.getCmdName() == "index") {
757 inset = new InsetIndex(inscmd.getContents());
758 } else if (inscmd.getCmdName() == "include") {
759 inset = new InsetInclude(inscmd.getContents(), this);
760 } else if (inscmd.getCmdName() == "label") {
761 inset = new InsetLabel(inscmd.getCommand());
762 } else if (inscmd.getCmdName() == "url"
763 || inscmd.getCmdName() == "htmlurl") {
764 inset = new InsetUrl(inscmd.getCommand());
765 } else if (inscmd.getCmdName() == "ref"
766 || inscmd.getCmdName() == "pageref") {
767 if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
768 inset = new InsetRef(inscmd, this);
770 #warning Verify that this else clause is still needed. (Lgb)
771 // This condition comes from a
772 // temporary solution to the latexdel
773 // ref inset that was transformed to
774 // an empty ref inset plus the body
775 // surronded by latexdel insets
777 string cont, opt, tmptmptok, cmdname;
779 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
784 tmptmptok = lex.GetString();
785 if(tmptmptok[0] == '\\') {
786 if( tmptmptok == "\\backslash")
796 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
801 tmptmptok = lex.GetString();
802 if(tmptmptok[0] == '\\') {
803 if( tmptmptok == "\\backslash")
813 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
819 cmdname = "\\" + inscmd.getCmdName();
820 cmdname += "[" + cont + "]";
821 cmdname += "{" + opt + "}";
822 inset = new InsetRef(cmdname, this);
824 } else if (inscmd.getCmdName() == "tableofcontents") {
825 inset = new InsetTOC(this);
826 } else if (inscmd.getCmdName() == "listoffigures") {
827 inset = new InsetLOF(this);
828 } else if (inscmd.getCmdName() == "listofalgorithms") {
829 inset = new InsetLOA(this);
830 } else if (inscmd.getCmdName() == "listoftables") {
831 inset = new InsetLOT(this);
832 } else if (inscmd.getCmdName() == "printindex") {
833 inset = new InsetPrintIndex(this);
834 } else if (inscmd.getCmdName() == "lyxparent") {
835 inset = new InsetParent(inscmd.getContents(), this);
839 par->InsertChar(pos, LyXParagraph::META_INSET);
840 par->InsertInset(pos, inset);
841 par->SetFont(pos, font);
845 } else if (token == "\\InsetQuotes") {
846 inset = new InsetQuotes;
848 par->InsertChar(pos, LyXParagraph::META_INSET);
849 par->InsertInset(pos, inset);
850 par->SetFont(pos, font);
852 } else if (token == "\\InsetFormula") {
853 inset = new InsetFormula;
855 par->InsertChar(pos, LyXParagraph::META_INSET);
856 par->InsertInset(pos, inset);
857 par->SetFont(pos, font);
859 } else if (token == "\\SpecialChar") {
860 inset = new InsetSpecialChar;
862 par->InsertChar(pos, LyXParagraph::META_INSET);
863 par->InsertInset(pos, inset);
864 par->SetFont(pos, font);
866 } else if (token == "\\Figure") {
867 inset = new InsetFig(100, 100, this);
869 par->InsertChar(pos, LyXParagraph::META_INSET);
870 par->InsertInset(pos, inset);
871 par->SetFont(pos, font);
873 } else if (token == "\\newline") {
874 par->InsertChar(pos, LyXParagraph::META_NEWLINE);
875 par->SetFont(pos, font);
877 } else if (token == "\\LyXTable") {
878 par->table = new LyXTable(lex);
879 } else if (token == "\\hfill") {
880 par->InsertChar(pos, LyXParagraph::META_HFILL);
881 par->SetFont(pos, font);
883 } else if (token == "\\protected_separator") {
884 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
885 par->SetFont(pos, font);
887 } else if (token == "\\bibitem") { // ale970302
889 par->bibkey = new InsetBibKey;
890 par->bibkey->Read(lex);
891 }else if (token == "\\backslash") {
892 par->InsertChar(pos, '\\');
893 par->SetFont(pos, font);
895 }else if (token == "\\the_end") {
898 // This should be insurance for the future: (Asger)
899 lex.printError("Unknown token `$$Token'. "
900 "Inserting as text.");
901 int n = token.length();
902 for (int i = 0; i < n; ++i) {
903 par->InsertChar(pos, token[i]);
904 par->SetFont(pos, font);
913 paragraph = return_par;
919 bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
925 token = lex.GetString();
926 if (token == "\\lyxformat") { // the first token _must_ be...
928 format = lex.GetFloat();
930 if (LYX_FORMAT - format > 0.05) {
931 printf(_("Warning: need lyxformat %.2f but found %.2f\n"),
934 if (format - LYX_FORMAT > 0.05) {
935 printf(_("ERROR: need lyxformat %.2f but found %.2f\n"),
938 bool the_end = readLyXformat2(lex, par);
939 // Formats >= 2.13 support "\the_end" marker
946 WriteAlert(_("Warning!"),
947 _("Reading of document is not complete"),
948 _("Maybe the document is truncated"));
949 // We simulate a safe reading anyways to allow
950 // users to take the chance... (Asger)
954 WriteAlert(_("ERROR!"),
955 _("Old LyX file format found. "
956 "Use LyX 0.10.x to read this!"));
960 } else { // "\\lyxformat" not found
961 WriteAlert(_("ERROR!"), _("Not a LyX file!"));
964 WriteAlert(_("ERROR!"), _("Unable to read file!"));
969 // Returns false if unsuccesful
970 bool Buffer::writeFile(string const & fname, bool flag)
972 // if flag is false writeFile will not create any GUI
973 // warnings, only cerr.
974 // Needed for autosave in background or panic save (Matthias 120496)
976 if (read_only && (fname == filename)) {
977 // Here we should come with a question if we should
978 // perform the write anyway.
980 lyxerr << _("Error! Document is read-only: ")
983 WriteAlert(_("Error! Document is read-only: "),
988 FileInfo finfo(fname);
989 if (finfo.exist() && !finfo.writable()) {
990 // Here we should come with a question if we should
991 // try to do the save anyway. (i.e. do a chmod first)
993 lyxerr << _("Error! Cannot write file: ")
996 WriteFSAlert(_("Error! Cannot write file: "),
1001 ofstream ofs(fname.c_str());
1004 lyxerr << _("Error! Cannot open file: ")
1007 WriteFSAlert(_("Error! Cannot open file: "),
1011 // The top of the file should not be written by params.
1013 // write out a comment in the top of the file
1014 ofs << '#' << LYX_DOCVERSION
1015 << " created this file. For more info see http://www.lyx.org/\n";
1016 ofs.setf(ios::showpoint|ios::fixed);
1018 ofs << "\\lyxformat " << setw(4) << LYX_FORMAT << "\n";
1020 // now write out the buffer paramters.
1021 params.writeFile(ofs);
1023 char footnoteflag = 0;
1026 // this will write out all the paragraphs
1027 // using recursive descent.
1028 paragraph->writeFile(ofs, params, footnoteflag, depth);
1030 // Write marker that shows file is complete
1031 ofs << "\n\\the_end" << endl;
1033 // how to check if close went ok?
1038 void Buffer::writeFileAscii(string const & fname, int linelen)
1040 LyXFont font1, font2;
1042 char c, footnoteflag = 0, depth = 0;
1044 LyXParagraph::size_type i;
1045 int j, h, ltype = 0, ltype_depth = 0,
1046 * clen = 0, actcell = 0, actpos = 0, cell = 0, cells = 0,
1049 bool ref_printed = false;
1051 ofstream ofs(fname.c_str());
1053 WriteFSAlert(_("Error: Cannot write file:"), fname);
1057 string fname1 = TmpFileName();
1058 LyXParagraph * par = paragraph;
1062 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE ||
1064 par->previous->footnoteflag == LyXParagraph::NO_FOOTNOTE){
1066 /* begins a footnote environment ? */
1067 if (footnoteflag != par->footnoteflag) {
1068 footnoteflag = par->footnoteflag;
1070 j = strlen(string_footnotekinds[par->footnotekind])+4;
1071 if (currlinelen + j > linelen)
1074 << string_footnotekinds[par->footnotekind] << "] ";
1079 /* begins or ends a deeper area ?*/
1080 if (depth != par->depth) {
1081 if (par->depth > depth) {
1082 while (par->depth > depth) {
1087 while (par->depth < depth) {
1093 /* First write the layout */
1094 tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1095 if (tmp == "Itemize") {
1097 ltype_depth = depth+1;
1098 } else if (tmp == "Enumerate") {
1100 ltype_depth = depth+1;
1101 } else if (strstr(tmp.c_str(), "ection")) {
1103 ltype_depth = depth+1;
1104 } else if (strstr(tmp.c_str(), "aragraph")) {
1106 ltype_depth = depth+1;
1107 } else if (tmp == "Description") {
1109 ltype_depth = depth+1;
1110 } else if (tmp == "Abstract") {
1113 } else if (tmp == "Bibliography") {
1121 /* maybe some vertical spaces */
1123 /* the labelwidthstring used in lists */
1127 /* some pagebreaks? */
1131 /* what about the alignment */
1134 /* dummy layout, that means a footnote ended */
1135 footnoteflag = LyXParagraph::NO_FOOTNOTE;
1140 /* It might be a table */
1143 if (!lyxrc->ascii_roff_command.empty() &&
1144 lyxrc->ascii_roff_command != "none") {
1145 RoffAsciiTable(ofs, par);
1152 cells = par->table->columns;
1153 clen = new int [cells];
1154 memset(clen, 0, sizeof(int) * cells);
1155 for (i = 0, j = 0, h = 1; i < par->size(); ++i, ++h) {
1156 c = par->GetChar(i);
1157 if (c == LyXParagraph::META_INSET) {
1158 if ((inset = par->GetInset(i))) {
1159 ofstream fs(fname1.c_str());
1161 WriteFSAlert(_("Error: Cannot open temporary file:"), fname1);
1164 inset->Latex(fs, -1);
1165 h += fs.tellp() - 1;
1166 remove(fname1.c_str());
1168 } else if (c == LyXParagraph::META_NEWLINE) {
1172 j = (++j) % par->table->NumberOfCellsInRow(actcell);
1180 font1 = LyXFont(LyXFont::ALL_INHERIT);
1182 for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
1183 if (!i && !footnoteflag && !noparbreak){
1185 for(j = 0; j < depth; ++j)
1187 currlinelen = depth * 2;
1189 case 0: /* Standard */
1190 case 4: /* (Sub)Paragraph */
1191 case 5: /* Description */
1193 case 6: /* Abstract */
1194 ofs << "Abstract\n\n";
1196 case 7: /* Bibliography */
1198 ofs << "References\n\n";
1203 ofs << par->labelstring << " ";
1206 if (ltype_depth > depth) {
1207 for(j = ltype_depth - 1; j > depth; --j)
1209 currlinelen += (ltype_depth-depth)*2;
1212 for(j = 0; j < cells; ++j) {
1214 for(h = 0; h < (clen[j] + 1);
1219 for(j = 0; j < depth; ++j)
1221 currlinelen = depth * 2;
1222 if (ltype_depth > depth) {
1223 for(j = ltype_depth;
1226 currlinelen += (ltype_depth-depth)*2;
1231 font2 = par->GetFontSettings(i);
1232 if (font1.latex() != font2.latex()) {
1233 if (font2.latex() == LyXFont::OFF)
1241 c = par->GetChar(i);
1245 case LyXParagraph::META_INSET:
1246 if ((inset = par->GetInset(i))) {
1248 inset->Latex(ofs, -1);
1249 currlinelen += (ofs.tellp() - fpos);
1250 actpos += (ofs.tellp() - fpos) - 1;
1253 case LyXParagraph::META_NEWLINE:
1255 if (par->table->NumberOfCellsInRow(actcell) <= cell) {
1256 for(j = actpos; j < clen[cell - 1]; ++j)
1259 for(j = 0; j < depth; ++j)
1261 currlinelen = depth*2;
1262 if (ltype_depth > depth) {
1263 for(j = ltype_depth; j > depth; --j)
1265 currlinelen += (ltype_depth-depth) * 2;
1267 for(j = 0; j < cells; ++j) {
1269 for(h = 0; h < (clen[j] + 1); ++h)
1273 for(j = 0; j < depth; ++j)
1275 currlinelen = depth * 2;
1276 if (ltype_depth > depth) {
1277 for(j = ltype_depth;
1280 currlinelen += (ltype_depth-depth)*2;
1286 j < clen[cell - 1]; ++j)
1292 currlinelen = actpos = 0;
1295 for(j = 0; j < depth; ++j)
1297 currlinelen = depth * 2;
1298 if (ltype_depth > depth) {
1299 for(j = ltype_depth;
1302 currlinelen += (ltype_depth - depth) * 2;
1306 case LyXParagraph::META_HFILL:
1309 case LyXParagraph::META_PROTECTED_SEPARATOR:
1316 if (currlinelen > linelen - 10
1317 && c == ' ' && i + 2 < par->size()) {
1319 for(j = 0; j < depth; ++j)
1321 currlinelen = depth * 2;
1322 if (ltype_depth > depth) {
1323 for(j = ltype_depth;
1326 currlinelen += (ltype_depth-depth)*2;
1328 } else if (c != '\0')
1331 lyxerr.debug() << "writeAsciiFile: NULL char in structure." << endl;
1337 for(j = actpos; j < clen[cell - 1]; ++j)
1340 for(j = 0; j < depth; ++j)
1342 currlinelen = depth * 2;
1343 if (ltype_depth > depth) {
1344 for(j = ltype_depth; j > depth; --j)
1346 currlinelen += (ltype_depth - depth) * 2;
1348 for(j = 0; j < cells; ++j) {
1350 for(h = 0; h < (clen[j] + 1); ++h)
1363 void Buffer::makeLaTeXFile(string const & fname,
1364 string const & original_path,
1365 bool nice, bool only_body)
1367 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1369 // How the **** can this be needed?
1370 //params.textclass = current_view->buffer()->params.textclass;
1372 niceFile = nice; // this will be used by Insetincludes.
1374 tex_code_break_column = lyxrc->ascii_linelen;
1376 LyXTextClass const & tclass =
1377 textclasslist.TextClass(params.textclass);
1379 ofstream ofs(fname.c_str());
1381 WriteFSAlert(_("Error: Cannot open file: "), fname);
1385 // validate the buffer.
1386 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1387 LaTeXFeatures features(tclass.numLayouts());
1389 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1392 // The starting paragraph of the coming rows is the
1393 // first paragraph of the document. (Asger)
1394 texrow.start(paragraph, 0);
1398 if (!only_body && nice) {
1399 LFile += "%% " LYX_DOCVERSION " created this file. "
1400 "For more info, see http://www.lyx.org/.\n";
1401 LFile += "%% Do not edit unless you really know what you are doing.\n";
1405 lyxerr.debug() << "lyx header finished" << endl;
1406 // There are a few differences between nice LaTeX and usual files:
1407 // usual is \batchmode and has a
1408 // special input@path to allow the including of figures
1409 // with either \input or \includegraphics (what figinsets do).
1410 // batchmode is not set if there is a tex_code_break_column.
1411 // In this case somebody is interested in the generated LaTeX,
1412 // so this is OK. input@path is set when the actual parameter
1413 // original_path is set. This is done for usual tex-file, but not
1414 // for nice-latex-file. (Matthias 250696)
1417 // code for usual, NOT nice-latex-file
1418 LFile += "\\batchmode\n"; // changed
1419 // from \nonstopmode
1422 if (!original_path.empty()) {
1423 LFile += "\\makeatletter\n";
1425 LFile += "\\def\\input@path{{" + original_path
1428 LFile += "\\makeatother\n";
1432 LFile += "\\documentclass";
1434 string options; // the document class options.
1436 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
1437 // only write if existing in list (and not default)
1438 options += params.fontsize;
1443 if (!params.use_geometry &&
1444 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
1445 switch (params.papersize) {
1446 case BufferParams::PAPER_A4PAPER:
1447 options += "a4paper,";
1449 case BufferParams::PAPER_USLETTER:
1450 options += "letterpaper,";
1452 case BufferParams::PAPER_A5PAPER:
1453 options += "a5paper,";
1455 case BufferParams::PAPER_B5PAPER:
1456 options += "b5paper,";
1458 case BufferParams::PAPER_EXECUTIVEPAPER:
1459 options += "executivepaper,";
1461 case BufferParams::PAPER_LEGALPAPER:
1462 options += "legalpaper,";
1468 if (params.sides != tclass.sides()) {
1469 switch (params.sides) {
1470 case LyXTextClass::OneSide:
1471 options += "oneside,";
1473 case LyXTextClass::TwoSides:
1474 options += "twoside,";
1481 if (params.columns != tclass.columns()) {
1482 if (params.columns == 2)
1483 options += "twocolumn,";
1485 options += "onecolumn,";
1488 if (!params.use_geometry
1489 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1490 options += "landscape,";
1492 // language should be a parameter to \documentclass
1493 if (params.language != "default") {
1494 if (params.language == "hebrew")
1495 options += "english,";
1496 else if (lyxrc->rtl_support)
1497 options += "hebrew,";
1498 options += params.language + ',';
1499 } else if (lyxrc->rtl_support)
1500 options += "hebrew,english,";
1502 // the user-defined options
1503 if (!params.options.empty()) {
1504 options += params.options + ',';
1507 if (!options.empty()){
1508 options = strip(options, ',');
1515 LFile += textclasslist.LatexnameOfClass(params.textclass);
1518 // end of \documentclass defs
1520 // font selection must be done before loading fontenc.sty
1521 if (params.fonts != "default") {
1522 LFile += "\\usepackage{" + params.fonts + "}\n";
1525 // this one is not per buffer
1526 if (lyxrc->fontenc != "default") {
1527 LFile += "\\usepackage[" + lyxrc->fontenc
1531 if (params.inputenc != "default") {
1532 LFile += "\\usepackage[" + params.inputenc
1537 /* at the very beginning the text parameters */
1538 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
1539 switch (params.paperpackage) {
1540 case BufferParams::PACKAGE_A4:
1541 LFile += "\\usepackage{a4}\n";
1544 case BufferParams::PACKAGE_A4WIDE:
1545 LFile += "\\usepackage{a4wide}\n";
1548 case BufferParams::PACKAGE_WIDEMARGINSA4:
1549 LFile += "\\usepackage[widemargins]{a4}\n";
1554 if (params.use_geometry) {
1555 LFile += "\\usepackage{geometry}\n";
1557 LFile += "\\geometry{verbose";
1558 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1559 LFile += ",landscape";
1560 switch (params.papersize2) {
1561 case BufferParams::VM_PAPER_CUSTOM:
1562 if (!params.paperwidth.empty())
1563 LFile += ",paperwidth="
1564 + params.paperwidth;
1565 if (!params.paperheight.empty())
1566 LFile += ",paperheight="
1567 + params.paperheight;
1569 case BufferParams::VM_PAPER_USLETTER:
1570 LFile += ",letterpaper";
1572 case BufferParams::VM_PAPER_USLEGAL:
1573 LFile += ",legalpaper";
1575 case BufferParams::VM_PAPER_USEXECUTIVE:
1576 LFile += ",executivepaper";
1578 case BufferParams::VM_PAPER_A3:
1579 LFile += ",a3paper";
1581 case BufferParams::VM_PAPER_A4:
1582 LFile += ",a4paper";
1584 case BufferParams::VM_PAPER_A5:
1585 LFile += ",a5paper";
1587 case BufferParams::VM_PAPER_B3:
1588 LFile += ",b3paper";
1590 case BufferParams::VM_PAPER_B4:
1591 LFile += ",b4paper";
1593 case BufferParams::VM_PAPER_B5:
1594 LFile += ",b5paper";
1597 // default papersize ie BufferParams::VM_PAPER_DEFAULT
1598 switch (lyxrc->default_papersize) {
1599 case BufferParams::PAPER_DEFAULT: // keep compiler happy
1600 case BufferParams::PAPER_USLETTER:
1601 LFile += ",letterpaper";
1603 case BufferParams::PAPER_LEGALPAPER:
1604 LFile += ",legalpaper";
1606 case BufferParams::PAPER_EXECUTIVEPAPER:
1607 LFile += ",executivepaper";
1609 case BufferParams::PAPER_A3PAPER:
1610 LFile += ",a3paper";
1612 case BufferParams::PAPER_A4PAPER:
1613 LFile += ",a4paper";
1615 case BufferParams::PAPER_A5PAPER:
1616 LFile += ",a5paper";
1618 case BufferParams::PAPER_B5PAPER:
1619 LFile += ",b5paper";
1623 if (!params.topmargin.empty())
1624 LFile += ",tmargin=" + params.topmargin;
1625 if (!params.bottommargin.empty())
1626 LFile += ",bmargin=" + params.bottommargin;
1627 if (!params.leftmargin.empty())
1628 LFile += ",lmargin=" + params.leftmargin;
1629 if (!params.rightmargin.empty())
1630 LFile += ",rmargin=" + params.rightmargin;
1631 if (!params.headheight.empty())
1632 LFile += ",headheight=" + params.headheight;
1633 if (!params.headsep.empty())
1634 LFile += ",headsep=" + params.headsep;
1635 if (!params.footskip.empty())
1636 LFile += ",footskip=" + params.footskip;
1640 if (params.use_amsmath
1641 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
1642 LFile += "\\usepackage{amsmath}\n";
1645 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
1646 if (params.pagestyle == "fancy") {
1647 LFile += "\\usepackage{fancyhdr}\n";
1650 LFile += "\\pagestyle{" + params.pagestyle + "}\n";
1654 // We try to load babel late, in case it interferes
1655 // with other packages.
1656 if (params.language != "default" || lyxrc->rtl_support ) {
1657 LFile += "\\usepackage{babel}\n";
1661 if (params.secnumdepth != tclass.secnumdepth()) {
1662 LFile += "\\setcounter{secnumdepth}{";
1663 LFile += tostr(params.secnumdepth);
1667 if (params.tocdepth != tclass.tocdepth()) {
1668 LFile += "\\setcounter{tocdepth}{";
1669 LFile += tostr(params.tocdepth);
1674 if (params.paragraph_separation) {
1675 switch (params.defskip.kind()) {
1676 case VSpace::SMALLSKIP:
1677 LFile += "\\setlength\\parskip{\\smallskipamount}\n";
1679 case VSpace::MEDSKIP:
1680 LFile += "\\setlength\\parskip{\\medskipamount}\n";
1682 case VSpace::BIGSKIP:
1683 LFile += "\\setlength\\parskip{\\bigskipamount}\n";
1685 case VSpace::LENGTH:
1686 LFile += "\\setlength\\parskip{"
1687 + params.defskip.length().asLatexString()
1690 default: // should never happen // Then delete it.
1691 LFile += "\\setlength\\parskip{\\medskipamount}\n";
1696 LFile += "\\setlength\\parindent{0pt}\n";
1700 // Write out what we've generated so far...and reset LFile
1704 // Now insert the LyX specific LaTeX commands...
1705 string preamble, tmppreamble;
1707 // The optional packages;
1708 preamble = features.getPackages(params);
1710 // this might be useful...
1711 preamble += "\n\\makeatletter\n\n";
1713 // Some macros LyX will need
1714 tmppreamble = features.getMacros(params);
1716 if (!tmppreamble.empty()) {
1717 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1718 "LyX specific LaTeX commands.\n"
1719 + tmppreamble + '\n';
1722 // the text class specific preamble
1723 tmppreamble = features.getTClassPreamble(params);
1724 if (!tmppreamble.empty()) {
1725 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1726 "Textclass specific LaTeX commands.\n"
1727 + tmppreamble + '\n';
1730 /* the user-defined preamble */
1731 if (!params.preamble.empty()) {
1732 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1733 "User specified LaTeX commands.\n"
1734 + params.preamble + '\n';
1737 preamble += "\\makeatother\n\n";
1739 // Itemize bullet settings need to be last in case the user
1740 // defines their own bullets that use a package included
1741 // in the user-defined preamble -- ARRae
1742 // Actually it has to be done much later than that
1743 // since some packages like frenchb make modifications
1744 // at \begin{document} time -- JMarc
1746 for (int i = 0; i < 4; ++i) {
1747 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
1748 if (bullets_def.empty())
1749 bullets_def="\\AtBeginDocument{\n";
1750 bullets_def += " \\renewcommand{\\labelitemi";
1752 // `i' is one less than the item to modify
1759 bullets_def += "ii";
1765 bullets_def += "}{" +
1766 params.user_defined_bullets[i].getText()
1771 if (!bullets_def.empty())
1772 preamble += bullets_def + "}\n\n";
1774 for (int j = countChar(preamble, '\n'); j-- ;) {
1781 LFile += "\\begin{document}\n\n";
1785 lyxerr.debug() << "preamble finished, now the body." << endl;
1787 bool was_title = false;
1788 bool already_title = false;
1794 LyXParagraph * par = paragraph;
1800 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
1802 LyXLayout const & layout = textclasslist.Style(params.textclass,
1805 if (layout.intitle) {
1806 if (already_title) {
1807 lyxerr <<"Error in MakeLatexFile: You"
1808 " should not mix title layouts"
1809 " with normal ones." << endl;
1812 } else if (was_title && !already_title) {
1813 LFile += "\\maketitle\n";
1815 already_title = true;
1818 // We are at depth 0 so we can just use
1819 // ordinary \footnote{} generation
1820 // flag this with ftcount
1822 if (layout.isEnvironment()
1823 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
1824 par = par->TeXEnvironment(LFile, texrow,
1825 ftnote, ft_texrow, ftcount);
1827 par = par->TeXOnePar(LFile, texrow,
1828 ftnote, ft_texrow, ftcount);
1831 // Write out what we've generated...and reset LFile
1834 LFile += "\\addtocounter{footnote}{-";
1835 LFile += tostr(ftcount - 1);
1839 texrow += ft_texrow;
1844 if (loop_count == 2) {
1845 // fwrite()ing every second time through the loop
1846 // gains a few extra % of speed; going higher than
1847 // 2 will slow things down again. I'll look at
1848 // LFile.length() in a future revision. ARRae
1855 // It might be that we only have a title in this document
1856 if (was_title && !already_title) {
1857 LFile += "\\maketitle\n";
1862 LFile += "\\end{document}\n";
1865 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1867 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1871 // Just to be sure. (Asger)
1874 // Write out what we've generated...and reset LFile
1878 // tex_code_break_column's value is used to decide
1879 // if we are in batchmode or not (within mathed_write()
1880 // in math_write.C) so we must set it to a non-zero
1881 // value when we leave otherwise we save incorrect .lyx files.
1882 tex_code_break_column = lyxrc->ascii_linelen;
1884 // How to check if the close went ok when using fstreams
1886 lyxerr.debug() << "Finished making latex file." << endl;
1890 bool Buffer::isLatex() const
1892 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
1896 bool Buffer::isLinuxDoc() const
1898 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
1902 bool Buffer::isLiterate() const
1904 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
1908 bool Buffer::isDocBook() const
1910 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
1914 bool Buffer::isSGML() const
1916 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
1917 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
1921 void Buffer::sgmlOpenTag(ostream & os, int depth,
1922 string const & latexname) const
1924 os << string(depth, ' ') << "<" << latexname << ">\n";
1928 void Buffer::sgmlCloseTag(ostream & os, int depth,
1929 string const & latexname) const
1931 os << string(depth, ' ') << "</" << latexname << ">\n";
1935 void Buffer::makeLinuxDocFile(string const & fname, int column)
1937 LyXParagraph * par = paragraph;
1939 string top_element = textclasslist.LatexnameOfClass(params.textclass);
1940 string environment_stack[10];
1943 int depth = 0; /* paragraph depth */
1945 ofstream ofs(fname.c_str());
1948 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
1952 tex_code_break_column = column;
1955 if (params.preamble.empty()) {
1956 ofs << "<!doctype linuxdoc system>\n\n";
1959 ofs << "<!doctype linuxdoc system \n [ "
1960 << params.preamble << " \n]>\n\n";
1963 ofs << "<!-- " << LYX_DOCVERSION
1964 << " created this file. For more info see http://www.lyx.org/"
1967 if(params.options.empty())
1968 sgmlOpenTag(ofs, 0, top_element);
1970 string top = top_element;
1972 top += params.options;
1973 sgmlOpenTag(ofs, 0, top);
1977 int desc_on = 0; /* description mode*/
1978 LyXLayout const & style =
1979 textclasslist.Style(users->buffer()->params.textclass,
1981 par->AutoDeleteInsets();
1983 /* treat <toc> as a special case for compatibility with old code */
1984 if (par->GetChar(0) == LyXParagraph::META_INSET) {
1985 Inset * inset = par->GetInset(0);
1986 char lyx_code = inset->LyxCode();
1987 if (lyx_code == Inset::TOC_CODE){
1988 string temp = "toc";
1989 sgmlOpenTag(ofs, depth, temp);
1992 linuxDocHandleFootnote(ofs, par, depth);
1997 // environment tag closing
1998 for( ; depth > par->depth; --depth) {
1999 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2000 environment_stack[depth].clear();
2003 // write opening SGML tags
2004 switch(style.latextype) {
2005 case LATEX_PARAGRAPH:
2006 if(depth == par->depth
2007 && !environment_stack[depth].empty()) {
2008 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2009 environment_stack[depth].clear();
2015 sgmlOpenTag(ofs, depth, style.latexname());
2020 LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
2022 if (!environment_stack[depth].empty()){
2023 sgmlCloseTag(ofs, depth,
2024 environment_stack[depth]);
2028 environment_stack[depth].clear();
2029 sgmlOpenTag(ofs, depth, style.latexname());
2032 case LATEX_ENVIRONMENT:
2033 case LATEX_ITEM_ENVIRONMENT:
2034 if(depth == par->depth
2035 && environment_stack[depth] != style.latexname()
2036 && !environment_stack[depth].empty()) {
2038 sgmlCloseTag(ofs, depth,
2039 environment_stack[depth]);
2040 environment_stack[depth].clear();
2042 if (depth < par->depth) {
2044 environment_stack[depth].clear();
2046 if (environment_stack[depth] != style.latexname()) {
2049 sgmlOpenTag(ofs, depth, temp);
2051 environment_stack[depth] = style.latexname();
2052 sgmlOpenTag(ofs, depth, environment_stack[depth]);
2054 if(style.latextype == LATEX_ENVIRONMENT) break;
2056 desc_on = (style.labeltype == LABEL_MANUAL);
2063 sgmlOpenTag(ofs, depth + 1, item_name);
2066 sgmlOpenTag(ofs, depth, style.latexname());
2071 SimpleLinuxDocOnePar(ofs, par, desc_on, depth);
2074 linuxDocHandleFootnote(ofs, par, depth);
2076 while(par && par->IsDummy());
2079 /* write closing SGML tags */
2080 switch(style.latextype) {
2082 case LATEX_ENVIRONMENT:
2083 case LATEX_ITEM_ENVIRONMENT:
2086 sgmlCloseTag(ofs, depth, style.latexname());
2091 /* Close open tags */
2092 for(; depth > 0; --depth)
2093 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2095 if(!environment_stack[depth].empty())
2096 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2099 sgmlCloseTag(ofs, 0, top_element);
2102 // How to check for successful close
2106 void Buffer::linuxDocHandleFootnote(ostream & os, LyXParagraph * & par,
2109 string tag = "footnote";
2111 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2112 sgmlOpenTag(os, depth + 1, tag);
2113 SimpleLinuxDocOnePar(os, par, 0, depth + 1);
2114 sgmlCloseTag(os, depth + 1, tag);
2120 void Buffer::DocBookHandleCaption(ostream & os, string & inner_tag,
2121 int const depth, int desc_on,
2122 LyXParagraph * & par)
2124 LyXParagraph * tpar = par;
2125 string tmp_par, extra_par;
2126 while (tpar && (tpar->footnoteflag != LyXParagraph::NO_FOOTNOTE) &&
2127 (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2131 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2132 "Caption").second) {
2133 sgmlOpenTag(os, depth + 1, inner_tag);
2134 SimpleDocBookOnePar(tmp_par, extra_par, tpar,
2135 desc_on, depth + 2);
2136 tmp_par = strip(tmp_par);
2137 tmp_par = frontStrip(tmp_par);
2139 sgmlCloseTag(os, depth+1, inner_tag);
2140 if(!extra_par.empty())
2146 void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
2149 string tag, inner_tag;
2150 string tmp_par, extra_par;
2151 bool inner_span = false;
2154 // This is not how I like to see enums. They should not be anonymous
2155 // and variables of its type should not be declared right after the
2156 // last brace. (Lgb)
2163 } last = NO_ONE, present = FOOTNOTE_LIKE;
2165 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2166 if(last == present) {
2168 if(!tmp_par.empty()) {
2171 sgmlCloseTag(os, depth+1, inner_tag);
2172 sgmlOpenTag(os, depth+1, inner_tag);
2179 if(!inner_tag.empty()) sgmlCloseTag(os, depth+1, inner_tag);
2180 if(!extra_par.empty()) os << extra_par;
2181 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2184 switch (par->footnotekind) {
2185 case LyXParagraph::FOOTNOTE:
2186 case LyXParagraph::ALGORITHM:
2189 present = FOOTNOTE_LIKE;
2192 case LyXParagraph::MARGIN:
2195 present = MARGIN_LIKE;
2198 case LyXParagraph::FIG:
2199 case LyXParagraph::WIDE_FIG:
2201 inner_tag = "title";
2205 case LyXParagraph::TAB:
2206 case LyXParagraph::WIDE_TAB:
2208 inner_tag = "title";
2213 sgmlOpenTag(os, depth, tag);
2214 if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
2215 DocBookHandleCaption(os, inner_tag, depth,
2219 sgmlOpenTag(os, depth + 1, inner_tag);
2222 // ignore all caption here, we processed them above!!!
2223 if (par->layout != textclasslist.NumberOfLayout(params.textclass,
2224 "Caption").second) {
2225 SimpleDocBookOnePar(tmp_par, extra_par, par,
2226 desc_on, depth + 2);
2228 tmp_par = frontStrip(strip(tmp_par));
2234 if(!inner_tag.empty()) sgmlCloseTag(os, depth+1, inner_tag);
2235 if(!extra_par.empty()) os << extra_par;
2236 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2240 /* push a tag in a style stack */
2241 void Buffer::push_tag(ostream & os, char const * tag,
2242 int & pos, char stack[5][3])
2244 /* pop all previous tags */
2245 for (int j = pos; j >= 0; --j)
2246 os << "</" << stack[j] << ">";
2249 sprintf(stack[++pos], "%s", tag);
2252 for (int i = 0; i <= pos; ++i)
2253 os << "<" << stack[i] << ">";
2256 void Buffer::pop_tag(ostream & os, char const * tag,
2257 int & pos, char stack[5][3])
2261 // pop all tags till specified one
2262 for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
2263 os << "</" << stack[j] << ">";
2266 os << "</" << tag << ">";
2268 // push all tags, but the specified one
2269 for (j = j + 1; j <= pos; ++j) {
2270 os << "<" << stack[j] << ">";
2271 strcpy(stack[j-1], stack[j]);
2277 /* handle internal paragraph parsing -- layout already processed */
2279 // checks, if newcol chars should be put into this line
2280 // writes newline, if necessary.
2282 void linux_doc_line_break(ostream & os, unsigned int & colcount,
2283 const unsigned int newcol)
2286 if (colcount > lyxrc->ascii_linelen) {
2288 colcount = newcol; // assume write after this call
2293 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
2294 int desc_on, int const /*depth*/)
2296 LyXFont font1, font2;
2299 LyXParagraph::size_type main_body;
2301 LyXLayout const & style = textclasslist.Style(params.textclass,
2304 char family_type = 0; // family font flag
2305 bool is_bold = false; // series font flag
2306 char shape_type = 0; // shape font flag
2307 bool is_em = false; // emphasis (italic) font flag
2309 int stack_num = -1; // style stack position
2310 char stack[5][3]; // style stack
2311 unsigned int char_line_count = 5; // Heuristic choice ;-)
2313 if (style.labeltype != LABEL_MANUAL)
2316 main_body = par->BeginningOfMainBody();
2318 /* gets paragraph main font */
2320 font1 = style.labelfont;
2325 /* parsing main loop */
2326 for (LyXParagraph::size_type i = 0;
2327 i < par->size(); ++i) {
2329 /* handle quote tag */
2330 if (i == main_body && !par->IsDummy()) {
2335 font2 = par->getFont(i);
2337 if (font1.family() != font2.family()) {
2338 switch(family_type) {
2340 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
2341 push_tag(os, "tt", stack_num, stack);
2344 else if (font2.family() == LyXFont::SANS_FAMILY) {
2345 push_tag(os, "sf", stack_num, stack);
2350 pop_tag(os, "tt", stack_num, stack);
2351 if (font2.family() == LyXFont::SANS_FAMILY) {
2352 push_tag(os, "sf", stack_num, stack);
2360 pop_tag(os, "sf", stack_num, stack);
2361 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
2362 push_tag(os, "tt", stack_num, stack);
2371 /* handle bold face */
2372 if (font1.series() != font2.series()) {
2373 if (font2.series() == LyXFont::BOLD_SERIES) {
2374 push_tag(os, "bf", stack_num, stack);
2378 pop_tag(os, "bf", stack_num, stack);
2383 /* handle italic and slanted fonts */
2384 if (font1.shape() != font2.shape()) {
2385 switch(shape_type) {
2387 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
2388 push_tag(os, "it", stack_num, stack);
2391 else if (font2.shape() == LyXFont::SLANTED_SHAPE) {
2392 push_tag(os, "sl", stack_num, stack);
2397 pop_tag(os, "it", stack_num, stack);
2398 if (font2.shape() == LyXFont::SLANTED_SHAPE) {
2399 push_tag(os, "sl", stack_num, stack);
2407 pop_tag(os, "sl", stack_num, stack);
2408 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
2409 push_tag(os, "it", stack_num, stack);
2417 /* handle <em> tag */
2418 if (font1.emph() != font2.emph()) {
2419 if (font2.emph() == LyXFont::ON) {
2420 push_tag(os, "em", stack_num, stack);
2423 pop_tag(os, "em", stack_num, stack);
2428 c = par->GetChar(i);
2430 if (font2.latex() == LyXFont::ON) {
2431 // "TeX"-Mode on == > SGML-Mode on.
2433 os << c; // see LaTeX-Generation...
2435 } else if (c == LyXParagraph::META_INSET) {
2436 inset = par->GetInset(i);
2438 inset->Linuxdoc(tmp_out);
2443 if (par->linuxDocConvertChar(c, sgml_string)
2444 && !style.free_spacing) { // in freespacing
2446 // non-breaking characters
2450 linux_doc_line_break(os, char_line_count, 6);
2455 linux_doc_line_break(os, char_line_count, 1);
2461 char_line_count += sgml_string.length();
2467 /* needed if there is an optional argument but no contents */
2468 if (main_body > 0 && main_body == par->size()) {
2472 /* pop all defined Styles */
2473 for (j = stack_num; j >= 0; --j) {
2474 linux_doc_line_break(os,
2476 3 + strlen(stack[j]));
2477 os << "</" << stack[j] << ">";
2480 /* resets description flag correctly */
2483 /* <tag> not closed... */
2484 linux_doc_line_break(os, char_line_count, 6);
2488 /* fprintf(file, "</p>");*/
2494 /* print an error message */
2495 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
2496 char const * message)
2498 InsetError * new_inset;
2500 /* insert an error marker in text */
2501 new_inset = new InsetError(message);
2502 par->InsertChar(pos, LyXParagraph::META_INSET);
2503 par->InsertInset(pos, new_inset);
2506 // This constant defines the maximum number of
2507 // environment layouts that can be nesteded.
2508 // The same applies for command layouts.
2509 // These values should be more than enough.
2510 // José Matos (1999/07/22)
2512 enum { MAX_NEST_LEVEL = 25};
2514 void Buffer::makeDocBookFile(string const & fname, int column)
2516 LyXParagraph * par = paragraph;
2518 string top_element= textclasslist.LatexnameOfClass(params.textclass);
2519 string environment_stack[MAX_NEST_LEVEL];
2520 string environment_inner[MAX_NEST_LEVEL];
2521 string command_stack[MAX_NEST_LEVEL];
2522 bool command_flag= false;
2523 int command_depth= 0, command_base= 0, cmd_depth= 0;
2525 string item_name, command_name;
2526 string c_depth, c_params, tmps;
2528 int depth= 0; /* paragraph depth */
2530 tex_code_break_column = column;
2532 ofstream ofs(fname.c_str());
2534 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2540 ofs << "<!doctype " << top_element
2541 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
2543 if (params.preamble.empty())
2546 ofs << "\n [ " << params.preamble << " \n]>\n\n";
2548 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
2549 << "\n See http://www.lyx.org/ for more information -->\n";
2551 if(params.options.empty())
2552 sgmlOpenTag(ofs, 0, top_element);
2554 string top = top_element;
2556 top += params.options;
2557 sgmlOpenTag(ofs, 0, top);
2561 int desc_on= 0; /* description mode*/
2562 LyXLayout const & style = textclasslist.Style(users->buffer()->params.textclass,
2564 par->AutoDeleteInsets();
2566 /* environment tag closing */
2567 for( ; depth > par->depth; --depth) {
2568 if(environment_inner[depth] != "!-- --") {
2569 item_name= "listitem";
2570 sgmlCloseTag(ofs, command_depth + depth,
2572 if( environment_inner[depth] == "varlistentry")
2573 sgmlCloseTag(ofs, depth+command_depth,
2574 environment_inner[depth]);
2576 sgmlCloseTag(ofs, depth + command_depth,
2577 environment_stack[depth]);
2578 environment_stack[depth].clear();
2579 environment_inner[depth].clear();
2582 if(depth == par->depth
2583 && environment_stack[depth] != style.latexname()
2584 && !environment_stack[depth].empty()) {
2585 if(environment_inner[depth] != "!-- --") {
2586 item_name= "listitem";
2587 sgmlCloseTag(ofs, command_depth+depth,
2589 if( environment_inner[depth] == "varlistentry")
2591 depth + command_depth,
2592 environment_inner[depth]);
2595 sgmlCloseTag(ofs, depth + command_depth,
2596 environment_stack[depth]);
2598 environment_stack[depth].clear();
2599 environment_inner[depth].clear();
2602 // Write opening SGML tags.
2603 switch(style.latextype) {
2604 case LATEX_PARAGRAPH:
2605 if(style.latexname() != "dummy")
2606 sgmlOpenTag(ofs, depth+command_depth,
2612 LinuxDocError(par, 0,
2613 _("Error : Wrong depth for "
2614 "LatexType Command.\n"));
2616 command_name = style.latexname();
2618 tmps = style.latexparam();
2619 c_params = split(tmps, c_depth,'|');
2621 cmd_depth= atoi(c_depth.c_str());
2624 if(cmd_depth<command_base) {
2625 for(int j = command_depth;
2626 j >= command_base; --j)
2627 if(!command_stack[j].empty())
2628 sgmlCloseTag(ofs, j, command_stack[j]);
2629 command_depth= command_base= cmd_depth;
2631 else if(cmd_depth <= command_depth) {
2632 for(int j = command_depth;
2633 j >= cmd_depth; --j)
2635 if(!command_stack[j].empty())
2636 sgmlCloseTag(ofs, j, command_stack[j]);
2637 command_depth= cmd_depth;
2640 command_depth= cmd_depth;
2643 command_depth = command_base = cmd_depth;
2644 command_flag = true;
2646 command_stack[command_depth]= command_name;
2648 // treat label as a special case for
2649 // more WYSIWYM handling.
2650 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2651 Inset * inset = par->GetInset(0);
2652 char lyx_code = inset->LyxCode();
2653 if (lyx_code == Inset::LABEL_CODE){
2654 command_name += " id=\"";
2655 command_name += (static_cast<InsetCommand *>(inset))->getContents();
2656 command_name += "\"";
2661 sgmlOpenTag(ofs, depth+command_depth, command_name);
2662 item_name = "title";
2663 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
2666 case LATEX_ENVIRONMENT:
2667 case LATEX_ITEM_ENVIRONMENT:
2668 if (depth < par->depth) {
2670 environment_stack[depth].clear();
2673 if (environment_stack[depth] != style.latexname()) {
2674 environment_stack[depth] = style.latexname();
2675 environment_inner[depth] = "!-- --";
2676 sgmlOpenTag(ofs, depth + command_depth,
2677 environment_stack[depth]);
2679 if(environment_inner[depth] != "!-- --") {
2680 item_name= "listitem";
2682 command_depth + depth,
2684 if (environment_inner[depth] == "varlistentry")
2686 depth + command_depth,
2687 environment_inner[depth]);
2691 if(style.latextype == LATEX_ENVIRONMENT) {
2692 if(!style.latexparam().empty())
2693 sgmlOpenTag(ofs, depth + command_depth,
2694 style.latexparam());
2698 desc_on = (style.labeltype == LABEL_MANUAL);
2701 environment_inner[depth]= "varlistentry";
2703 environment_inner[depth]= "listitem";
2705 sgmlOpenTag(ofs, depth + 1 + command_depth,
2706 environment_inner[depth]);
2710 sgmlOpenTag(ofs, depth + 1 + command_depth,
2715 sgmlOpenTag(ofs, depth + 1 + command_depth,
2720 sgmlOpenTag(ofs, depth + command_depth,
2726 string tmp_par, extra_par;
2728 SimpleDocBookOnePar(tmp_par, extra_par, par, desc_on,
2729 depth + 1 + command_depth);
2733 DocBookHandleFootnote(ofs, par,
2734 depth + 1 + command_depth);
2736 while(par && par->IsDummy());
2739 /* write closing SGML tags */
2740 switch(style.latextype) {
2743 sgmlCloseTag(ofs, depth + command_depth, end_tag);
2745 case LATEX_ENVIRONMENT:
2746 if(!style.latexparam().empty())
2747 sgmlCloseTag(ofs, depth + command_depth,
2748 style.latexparam());
2750 case LATEX_ITEM_ENVIRONMENT:
2751 if(desc_on == 1) break;
2753 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
2755 case LATEX_PARAGRAPH:
2756 if(style.latexname() != "dummy")
2757 sgmlCloseTag(ofs, depth + command_depth,
2761 sgmlCloseTag(ofs, depth + command_depth,
2768 for(; depth >= 0; --depth) {
2769 if(!environment_stack[depth].empty()) {
2770 if(environment_inner[depth] != "!-- --") {
2771 item_name= "listitem";
2772 sgmlCloseTag(ofs, command_depth + depth,
2774 if( environment_inner[depth] == "varlistentry")
2775 sgmlCloseTag(ofs, depth + command_depth,
2776 environment_inner[depth]);
2779 sgmlCloseTag(ofs, depth + command_depth,
2780 environment_stack[depth]);
2784 for(int j = command_depth; j >= command_base; --j)
2785 if(!command_stack[j].empty())
2786 sgmlCloseTag(ofs, j, command_stack[j]);
2789 sgmlCloseTag(ofs, 0, top_element);
2792 // How to check for successful close
2796 void Buffer::SimpleDocBookOnePar(string & file, string & extra,
2797 LyXParagraph * par, int & desc_on,
2801 par->SimpleDocBookOneTablePar(file, extra, desc_on, depth);
2804 LyXFont font1, font2;
2807 LyXParagraph::size_type main_body;
2809 string emph= "emphasis";
2810 bool emph_flag= false;
2811 int char_line_count= 0;
2813 LyXLayout const & style = textclasslist.Style(params.textclass,
2816 if (style.labeltype != LABEL_MANUAL)
2819 main_body = par->BeginningOfMainBody();
2821 /* gets paragraph main font */
2823 font1 = style.labelfont;
2827 char_line_count = depth;
2828 if(!style.free_spacing)
2829 for (j = 0; j < depth; ++j)
2832 /* parsing main loop */
2833 for (LyXParagraph::size_type i = 0;
2834 i < par->size(); ++i) {
2835 font2 = par->getFont(i);
2837 /* handle <emphasis> tag */
2838 if (font1.emph() != font2.emph() && i) {
2839 if (font2.emph() == LyXFont::ON) {
2840 file += "<emphasis>";
2843 file += "</emphasis>";
2848 c = par->GetChar(i);
2850 if (c == LyXParagraph::META_INSET) {
2851 inset = par->GetInset(i);
2853 inset->DocBook(tmp_out);
2855 // This code needs some explanation:
2856 // Two insets are treated specially
2857 // label if it is the first element in a command paragraph
2859 // graphics inside tables or figure floats can't go on
2860 // title (the equivalente in latex for this case is caption
2861 // and title should come first
2864 if(desc_on!= 3 || i!= 0) {
2865 if(!tmp_out.empty() && tmp_out[0] == '@') {
2867 extra += frontStrip(tmp_out, '@');
2869 file += frontStrip(tmp_out, '@');
2874 } else if (font2.latex() == LyXFont::ON) {
2875 // "TeX"-Mode on ==> SGML-Mode on.
2882 if (par->linuxDocConvertChar(c, sgml_string)
2883 && !style.free_spacing) { // in freespacing
2885 // non-breaking characters
2890 file += "</term><listitem><para>";
2898 file += sgml_string;
2904 /* needed if there is an optional argument but no contents */
2905 if (main_body > 0 && main_body == par->size()) {
2909 file += "</emphasis>";
2912 /* resets description flag correctly */
2915 /* <term> not closed... */
2923 int Buffer::runLaTeX()
2925 if (!users->text) return 0;
2929 // get LaTeX-Filename
2930 string name = getLatexName();
2932 string path = OnlyPath(filename);
2934 string org_path = path;
2935 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
2939 Path p(path); // path to LaTeX file
2940 users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
2942 // Remove all error insets
2943 bool a = users->removeAutoInsets();
2945 // Always generate the LaTeX file
2946 makeLaTeXFile(name, org_path, false);
2949 // do the LaTex run(s)
2951 string latex_command = lyxrc->pdf_mode ?
2952 lyxrc->pdflatex_command : lyxrc->latex_command;
2953 LaTeX latex(latex_command, name, filepath);
2954 int res = latex.run(terr,
2955 users->owner()->getMiniBuffer()); // running latex
2957 // check return value from latex.run().
2958 if ((res & LaTeX::NO_LOGFILE)) {
2959 WriteAlert(_("LaTeX did not work!"),
2960 _("Missing log file:"), name);
2961 } else if ((res & LaTeX::ERRORS)) {
2962 users->owner()->getMiniBuffer()->Set(_("Done"));
2963 // Insert all errors as errors boxes
2964 users->insertErrors(terr);
2966 // Dvi should also be kept dirty if the latex run
2967 // ends up with errors. However it should be possible
2968 // to view a dirty dvi too.
2970 //no errors or any other things to think about so:
2971 users->owner()->getMiniBuffer()->Set(_("Done"));
2975 // if we removed error insets before we ran LaTeX or if we inserted
2976 // error insets after we ran LaTeX this must be run:
2977 if (a || (res & LaTeX::ERRORS)){
2980 users->updateScrollbar();
2984 return latex.getNumErrors();
2988 int Buffer::runLiterate()
2990 if (!users->text) return 0;
2994 // get LaTeX-Filename
2995 string name = getLatexName();
2996 // get Literate-Filename
2997 string lit_name = ChangeExtension (getLatexName(),
2998 lyxrc->literate_extension, true);
3000 string path = OnlyPath(filename);
3002 string org_path = path;
3003 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3007 Path p(path); // path to Literate file
3008 users->owner()->getMiniBuffer()->Set(_("Running Literate..."));
3010 // Remove all error insets
3011 bool a = users->removeAutoInsets();
3013 // generate the Literate file if necessary
3014 if (!isDviClean() || a) {
3015 makeLaTeXFile(lit_name, org_path, false);
3019 string latex_command = lyxrc->pdf_mode ?
3020 lyxrc->pdflatex_command : lyxrc->latex_command;
3021 Literate literate(latex_command, name, filepath,
3023 lyxrc->literate_command, lyxrc->literate_error_filter,
3024 lyxrc->build_command, lyxrc->build_error_filter);
3026 int res = literate.weave(terr, users->owner()->getMiniBuffer());
3028 // check return value from literate.weave().
3029 if ((res & Literate::NO_LOGFILE)) {
3030 WriteAlert(_("Literate command did not work!"),
3031 _("Missing log file:"), name);
3032 } else if ((res & Literate::ERRORS)) {
3033 users->owner()->getMiniBuffer()->Set(_("Done"));
3034 // Insert all errors as errors boxes
3035 users->insertErrors(terr);
3037 // Dvi should also be kept dirty if the latex run
3038 // ends up with errors. However it should be possible
3039 // to view a dirty dvi too.
3041 //no errors or any other things to think about so:
3042 users->owner()->getMiniBuffer()->Set(_("Done"));
3046 // if we removed error insets before we ran LaTeX or if we inserted
3047 // error insets after we ran LaTeX this must be run:
3048 if (a || (res & Literate::ERRORS)){
3051 users->updateScrollbar();
3055 return literate.getNumErrors();
3059 int Buffer::buildProgram()
3061 if (!users->text) return 0;
3065 // get LaTeX-Filename
3066 string name = getLatexName();
3067 // get Literate-Filename
3068 string lit_name = ChangeExtension(getLatexName(),
3069 lyxrc->literate_extension, true);
3071 string path = OnlyPath(filename);
3073 string org_path = path;
3074 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3078 Path p(path); // path to Literate file
3079 users->owner()->getMiniBuffer()->Set(_("Building Program..."));
3081 // Remove all error insets
3082 bool a = users->removeAutoInsets();
3084 // generate the LaTeX file if necessary
3085 if (!isNwClean() || a) {
3086 makeLaTeXFile(lit_name, org_path, false);
3090 string latex_command = lyxrc->pdf_mode ?
3091 lyxrc->pdflatex_command : lyxrc->latex_command;
3092 Literate literate(latex_command, name, filepath,
3094 lyxrc->literate_command, lyxrc->literate_error_filter,
3095 lyxrc->build_command, lyxrc->build_error_filter);
3097 int res = literate.build(terr, users->owner()->getMiniBuffer());
3099 // check return value from literate.build().
3100 if ((res & Literate::NO_LOGFILE)) {
3101 WriteAlert(_("Build did not work!"),
3102 _("Missing log file:"), name);
3103 } else if ((res & Literate::ERRORS)) {
3104 users->owner()->getMiniBuffer()->Set(_("Done"));
3105 // Insert all errors as errors boxes
3106 users->insertErrors(terr);
3108 // Literate files should also be kept dirty if the literate
3109 // command run ends up with errors.
3111 //no errors or any other things to think about so:
3112 users->owner()->getMiniBuffer()->Set(_("Done"));
3116 // if we removed error insets before we ran Literate/Build or if we inserted
3117 // error insets after we ran Literate/Build this must be run:
3118 if (a || (res & Literate::ERRORS)){
3121 users->updateScrollbar();
3125 return literate.getNumErrors();
3129 // This should be enabled when the Chktex class is implemented. (Asger)
3130 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
3131 // Other flags: -wall -v0 -x
3132 int Buffer::runChktex()
3134 if (!users->text) return 0;
3138 // get LaTeX-Filename
3139 string name = getLatexName();
3140 string path = OnlyPath(filename);
3142 string org_path = path;
3143 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3147 Path p(path); // path to LaTeX file
3148 users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
3150 // Remove all error insets
3151 bool a = users->removeAutoInsets();
3153 // Generate the LaTeX file if neccessary
3154 if (!isDviClean() || a) {
3155 makeLaTeXFile(name, org_path, false);
3160 Chktex chktex(lyxrc->chktex_command, name, filepath);
3161 int res = chktex.run(terr); // run chktex
3164 WriteAlert(_("chktex did not work!"),
3165 _("Could not run with file:"), name);
3166 } else if (res > 0) {
3167 // Insert all errors as errors boxes
3168 users->insertErrors(terr);
3171 // if we removed error insets before we ran chktex or if we inserted
3172 // error insets after we ran chktex, this must be run:
3176 users->updateScrollbar();
3184 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
3186 LyXFont font1 = LyXFont(LyXFont::ALL_INHERIT);
3189 LyXParagraph::size_type i;
3193 string fname1 = TmpFileName(string(), "RAT1");
3194 string fname2 = TmpFileName(string(), "RAT2");
3196 ofstream ofs(fname1.c_str());
3198 WriteAlert(_("LYX_ERROR:"),
3199 _("Cannot open temporary file:"), fname1);
3202 par->table->RoffEndOfCell(ofs, -1);
3203 for (i = 0; i < par->size(); ++i) {
3204 c = par->GetChar(i);
3205 if (par->table->IsContRow(cell)) {
3206 if (c == LyXParagraph::META_NEWLINE)
3210 font2 = par->GetFontSettings(i);
3211 if (font1.latex() != font2.latex()) {
3212 if (font2.latex() != LyXFont::OFF)
3216 case LyXParagraph::META_INSET:
3217 if ((inset = par->GetInset(i))) {
3218 fstream fs(fname2.c_str(), ios::in|ios::out);
3220 WriteAlert(_("LYX_ERROR:"),
3221 _("Cannot open temporary file:"), fname2);
3223 remove(fname1.c_str());
3226 inset->Latex(fs, -1);
3239 case LyXParagraph::META_NEWLINE:
3240 if (par->table->CellHasContRow(cell)>= 0)
3241 par->RoffContTableRows(ofs, i+1, cell);
3242 par->table->RoffEndOfCell(ofs, cell);
3245 case LyXParagraph::META_HFILL:
3247 case LyXParagraph::META_PROTECTED_SEPARATOR:
3257 << "RoffAsciiTable:"
3258 " NULL char in structure." << endl;
3262 par->table->RoffEndOfCell(ofs, cell);
3264 string cmd = lyxrc->ascii_roff_command + " >" + fname2;
3265 cmd = subst(cmd, "$$FName", fname1);
3266 Systemcalls one(Systemcalls::System, cmd);
3267 if (!(lyxerr.debugging(Debug::ROFF))) {
3268 remove(fname1.c_str());
3270 ifstream ifs(fname2.c_str());
3272 WriteFSAlert(_("Error! Can't open temporary file:"), fname2);
3275 // now output the produced file
3279 WriteAlert(_("Error!"),
3280 _("Error executing *roff command on table"));
3281 // overread leading blank lines
3282 while(!ifs && (c == '\n'))
3285 for(j = 0; j < par->depth; ++j)
3287 while(!ifs && (c != '\n')) {
3292 // overread trailing blank lines
3293 while(!ifs && (c == '\n'))
3297 remove(fname2.c_str());
3301 /// changed Heinrich Bauer, 23/03/98
3302 bool Buffer::isDviClean() const
3304 if (lyxrc->use_tempdir)
3305 return dvi_clean_tmpd;
3307 return dvi_clean_orgd;
3311 /// changed Heinrich Bauer, 23/03/98
3312 void Buffer::markDviClean()
3314 if (lyxrc->use_tempdir)
3315 dvi_clean_tmpd = true;
3317 dvi_clean_orgd = true;
3321 /// changed Heinrich Bauer, 23/03/98
3322 void Buffer::markDviDirty()
3324 if (lyxrc->use_tempdir)
3325 dvi_clean_tmpd = false;
3327 dvi_clean_orgd = false;
3331 void Buffer::validate(LaTeXFeatures & features) const
3333 LyXParagraph * par = paragraph;
3334 LyXTextClass const & tclass =
3335 textclasslist.TextClass(params.textclass);
3337 // AMS Style is at document level
3339 features.amsstyle = (params.use_amsmath ||
3340 tclass.provides(LyXTextClass::amsmath));
3343 // We don't use "lyxerr.debug" because of speed. (Asger)
3344 if (lyxerr.debugging(Debug::LATEX))
3345 lyxerr << "Paragraph: " << par << endl;
3347 // Now just follow the list of paragraphs and run
3348 // validate on each of them.
3349 par->validate(features);
3351 // and then the next paragraph
3355 // the bullet shapes are buffer level not paragraph level
3356 // so they are tested here
3357 for (int i = 0; i < 4; ++i) {
3358 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
3359 int font = params.user_defined_bullets[i].getFont();
3362 .user_defined_bullets[i]
3369 features.latexsym = true;
3373 features.amssymb = true;
3375 else if ((font >= 2 && font <= 5)) {
3376 features.pifont = true;
3381 if (lyxerr.debugging(Debug::LATEX)) {
3382 features.showStruct(params);
3387 void Buffer::setPaperStuff()
3389 params.papersize = BufferParams::PAPER_DEFAULT;
3390 char c1 = params.paperpackage;
3391 if (c1 == BufferParams::PACKAGE_NONE) {
3392 char c2 = params.papersize2;
3393 if (c2 == BufferParams::VM_PAPER_USLETTER)
3394 params.papersize = BufferParams::PAPER_USLETTER;
3395 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
3396 params.papersize = BufferParams::PAPER_LEGALPAPER;
3397 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
3398 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
3399 else if (c2 == BufferParams::VM_PAPER_A3)
3400 params.papersize = BufferParams::PAPER_A3PAPER;
3401 else if (c2 == BufferParams::VM_PAPER_A4)
3402 params.papersize = BufferParams::PAPER_A4PAPER;
3403 else if (c2 == BufferParams::VM_PAPER_A5)
3404 params.papersize = BufferParams::PAPER_A5PAPER;
3405 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
3406 (c2 == BufferParams::VM_PAPER_B5))
3407 params.papersize = BufferParams::PAPER_B5PAPER;
3408 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
3409 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
3410 params.papersize = BufferParams::PAPER_A4PAPER;
3414 // This function should be in Buffer because it's a buffer's property (ale)
3415 string Buffer::getIncludeonlyList(char delim)
3418 LyXParagraph * par = paragraph;
3419 LyXParagraph::size_type pos;
3423 while ((inset = par->ReturnNextInsetPointer(pos))){
3424 if (inset->LyxCode() == Inset::INCLUDE_CODE) {
3425 InsetInclude * insetinc =
3426 static_cast<InsetInclude*>(inset);
3427 if (insetinc->isInclude()
3428 && insetinc->isNoLoad()) {
3431 lst += ChangeExtension(insetinc->getContents(), string(), true);
3438 lyxerr.debug() << "Includeonly(" << lst << ')' << endl;
3443 // This is also a buffer property (ale)
3444 string Buffer::getReferenceList(char delim)
3446 /// if this is a child document and the parent is already loaded
3447 /// Use the parent's list instead [ale990407]
3448 if (!params.parentname.empty()
3449 && bufferlist.exists(params.parentname)) {
3450 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3452 return tmp->getReferenceList(delim);
3455 LyXParagraph * par = paragraph;
3456 LyXParagraph::size_type pos;
3461 while ((inset = par->ReturnNextInsetPointer(pos))){
3462 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
3465 lst += inset->getLabel(i);
3471 lyxerr.debug() << "References(" << lst << ")" << endl;
3476 // This is also a buffer property (ale)
3477 string Buffer::getBibkeyList(char delim)
3479 /// if this is a child document and the parent is already loaded
3480 /// Use the parent's list instead [ale990412]
3481 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
3482 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3484 return tmp->getBibkeyList(delim);
3488 LyXParagraph * par = paragraph;
3491 if (!bibkeys.empty())
3493 bibkeys += par->bibkey->getContents();
3498 // Might be either using bibtex or a child has bibliography
3499 if (bibkeys.empty()) {
3503 LyXParagraph::size_type pos = -1;
3505 // Search for Bibtex or Include inset
3506 while ((inset = par->ReturnNextInsetPointer(pos))) {
3507 if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
3508 if (!bibkeys.empty())
3510 bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
3511 } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
3512 string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
3514 if (!bibkeys.empty())
3525 lyxerr.debug() << "Bibkeys(" << bibkeys << ")" << endl;
3530 bool Buffer::isDepClean(string const & name) const
3532 DEPCLEAN * item = dep_clean;
3533 while (item && item->master != name)
3535 if (!item) return true;
3540 void Buffer::markDepClean(string const & name)
3543 dep_clean = new DEPCLEAN;
3544 dep_clean->clean = true;
3545 dep_clean->master = name;
3546 dep_clean->next = 0;
3548 DEPCLEAN* item = dep_clean;
3549 while (item && item->master != name)
3554 item = new DEPCLEAN;
3556 item->master = name;
3563 void Buffer::Dispatch(string const & command)
3565 // Split command string into command and argument
3566 string cmd, line = frontStrip(command);
3567 string arg = strip(frontStrip(split(line, cmd, ' ')));
3569 Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
3573 void Buffer::Dispatch(int action, string const & argument)
3577 MenuExport(this, argument);
3581 lyxerr << "A truly unknown func!" << endl;