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
37 #include <sys/types.h>
41 #pragma implementation "buffer.h"
45 #include "bufferlist.h"
47 #include "lyx_gui_misc.h"
48 #include "LyXAction.h"
51 #include "tex-strings.h"
54 #include "minibuffer.h"
57 #include "mathed/formulamacro.h"
58 #include "insets/lyxinset.h"
59 #include "insets/inseterror.h"
60 #include "insets/insetlabel.h"
61 #include "insets/insetref.h"
62 #include "insets/inseturl.h"
63 #include "insets/insetinfo.h"
64 #include "insets/insetquotes.h"
65 #include "insets/insetlatexaccent.h"
66 #include "insets/insetbib.h"
67 #include "insets/insetindex.h"
68 #include "insets/insetinclude.h"
69 #include "insets/insettoc.h"
70 #include "insets/insetlof.h"
71 #include "insets/insetlot.h"
72 #include "insets/insetloa.h"
73 #include "insets/insetparent.h"
74 #include "insets/insetspecialchar.h"
75 #include "insets/figinset.h"
76 #include "insets/insettext.h"
77 #include "insets/insetert.h"
78 #include "insets/insetgraphics.h"
79 #include "insets/insetfoot.h"
80 #include "support/filetools.h"
81 #include "support/path.h"
87 #include "LaTeXFeatures.h"
88 #include "support/syscall.h"
89 #include "support/lyxlib.h"
90 #include "support/FileInfo.h"
94 // Uncomment this line to enable a workaround for the weird behaviour
95 // of the cursor between a displayed inset and last character
96 // at the upper line. (Alejandro 20.9.96)
97 // #define BREAK_BEFORE_INSET
99 /* Lars, when changing this file sometime in the future to a list,
100 * please check out the figinsets-sideeffects described at the
101 * beginning of figinset.C Matthias (04.07.1996)
105 // all these externs should eventually be removed.
106 extern BufferList bufferlist;
108 extern void MenuExport(Buffer *, string const &);
109 extern LyXAction lyxaction;
112 static const float LYX_FORMAT = 2.15;
114 extern int tex_code_break_column;
116 extern void FreeUpdateTimer();
119 Buffer::Buffer(string const & file, LyXRC * lyxrc, bool ronly)
121 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
123 filepath = OnlyPath(file);
127 dvi_clean_orgd = false; // Heinrich Bauer, 23/03/98
128 dvi_clean_tmpd = false; // Heinrich Bauer, 23/03/98
133 if (read_only || (lyxrc && lyxrc->use_tempdir)) {
134 tmppath = CreateBufferTmpDir();
135 } else tmppath.clear();
141 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
142 // here the buffer should take care that it is
143 // saved properly, before it goes into the void.
145 // make sure that views using this buffer
150 if (!tmppath.empty()) {
151 DestroyBufferTmpDir(tmppath);
154 LyXParagraph * par = paragraph;
155 LyXParagraph * tmppar;
165 bool Buffer::saveParamsAsDefaults()
167 string fname = AddName(AddPath(user_lyxdir, "templates/"),
169 Buffer defaults = Buffer(fname);
171 // Use the current buffer's parameters as default
172 defaults.params = params;
174 // add an empty paragraph. Is this enough?
175 defaults.paragraph = new LyXParagraph;
177 return defaults.writeFile(defaults.filename, false);
181 /// Update window titles of all users
182 // Should work on a list
183 void Buffer::updateTitles() const
185 if (users) users->owner()->updateWindowTitle();
189 /// Reset autosave timer of all users
190 // Should work on a list
191 void Buffer::resetAutosaveTimers() const
193 if (users) users->owner()->resetAutosaveTimer();
197 void Buffer::fileName(string const & newfile)
199 filename = MakeAbsPath(newfile);
200 filepath = OnlyPath(filename);
201 setReadonly(IsFileWriteable(filename) == 0);
206 // candidate for move to BufferView
207 // (at least some parts in the beginning of the func)
210 // changed to be public and have one parameter
211 // if par = 0 normal behavior
212 // else insert behavior
213 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
214 #define USE_PARSE_FUNCTION 1
215 bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
219 char depth = 0; // signed or unsigned?
220 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
221 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
222 bool the_end_read = false;
224 LyXParagraph * return_par = 0;
225 LyXFont font(LyXFont::ALL_INHERIT);
227 // If we are inserting, we cheat and get a token in advance
228 bool has_token = false;
232 par = new LyXParagraph;
234 users->text->BreakParagraph();
235 return_par = users->text->FirstParagraph();
238 // We don't want to adopt the parameters from the
239 // document we insert, so we skip until the text begins:
242 pretoken = lex.GetString();
243 if (pretoken == "\\layout") {
255 pretoken = lex.GetString();
258 // Profiling show this should give a lot: (Asger)
259 string const token = pretoken;
263 the_end_read = parseSingleLyXformat2Token(lex, par, return_par,
272 paragraph = return_par;
278 bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
279 LyXParagraph *& return_par,
280 const string & token, int & pos,
281 char & depth, LyXFont & font,
282 LyXParagraph::footnote_flag & footnoteflag,
283 LyXParagraph::footnote_kind & footnotekind)
288 bool the_end_read = false;
290 if (token[0] != '\\') {
291 int n = token.length();
292 for (int i = 0; i < n; ++i) {
293 par->InsertChar(pos, token[i]);
294 par->SetFont(pos, font);
297 } else if (token == "\\i") {
298 inset = new InsetLatexAccent;
300 par->InsertChar(pos, LyXParagraph::META_INSET);
302 par->InsertInset(pos, inset);
303 par->SetFont(pos, font);
305 } else if (token == "\\layout") {
309 par->text.resize(par->text.size());
310 par = new LyXParagraph(par);
314 string layoutname = lex.GetString();
315 pair<bool, LyXTextClass::LayoutList::size_type> pp
316 = textclasslist.NumberOfLayout(params.textclass,
319 par->layout = pp.second;
320 } else { // layout not found
321 // use default layout "Standard" (0)
324 // Test whether the layout is obsolete.
325 LyXLayout const & layout =
326 textclasslist.Style(params.textclass,
328 if (!layout.obsoleted_by().empty())
330 textclasslist.NumberOfLayout(params.textclass,
331 layout.obsoleted_by()).second;
332 par->footnoteflag = footnoteflag;
333 par->footnotekind = footnotekind;
335 font = LyXFont(LyXFont::ALL_INHERIT);
336 } else if (token == "\\end_float") {
340 par->text.resize(par->text.size());
341 par = new LyXParagraph(par);
343 footnotekind = LyXParagraph::FOOTNOTE;
344 footnoteflag = LyXParagraph::NO_FOOTNOTE;
347 par->layout = LYX_DUMMY_LAYOUT;
348 font = LyXFont(LyXFont::ALL_INHERIT);
349 } else if (token == "\\begin_float") {
350 tmpret = lex.FindToken(string_footnotekinds);
351 if (tmpret == -1) ++tmpret;
352 if (tmpret != LYX_LAYOUT_DEFAULT)
353 footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
354 if (footnotekind == LyXParagraph::FOOTNOTE
355 || footnotekind == LyXParagraph::MARGIN)
356 footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
358 footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
359 } else if (token == "\\begin_deeper") {
361 } else if (token == "\\end_deeper") {
363 lex.printError("\\end_deeper: "
364 "depth is already null");
368 } else if (token == "\\begin_preamble") {
369 params.readPreamble(lex);
370 } else if (token == "\\textclass") {
372 pair<bool, LyXTextClassList::size_type> pp =
373 textclasslist.NumberOfClass(lex.GetString());
375 params.textclass = pp.second;
377 lex.printError("Unknown textclass `$$Token'");
378 params.textclass = 0;
380 if (!textclasslist.Load(params.textclass)) {
381 // if the textclass wasn't loaded properly
382 // we need to either substitute another
383 // or stop loading the file.
384 // I can substitute but I don't see how I can
385 // stop loading... ideas?? ARRae980418
386 WriteAlert(_("Textclass Loading Error!"),
387 string(_("Can't load textclass ")) +
388 textclasslist.NameOfClass(params.textclass),
389 _("-- substituting default"));
390 params.textclass = 0;
392 } else if (token == "\\options") {
394 params.options = lex.GetString();
395 } else if (token == "\\language") {
396 params.readLanguage(lex);
397 } else if (token == "\\fontencoding") {
399 } else if (token == "\\inputencoding") {
401 params.inputenc = lex.GetString();
402 } else if (token == "\\graphics") {
403 params.readGraphicsDriver(lex);
404 } else if (token == "\\fontscheme") {
406 params.fonts = lex.GetString();
407 } else if (token == "\\noindent") {
408 par->noindent = true;
409 } else if (token == "\\fill_top") {
410 par->added_space_top = VSpace(VSpace::VFILL);
411 } else if (token == "\\fill_bottom") {
412 par->added_space_bottom = VSpace(VSpace::VFILL);
413 } else if (token == "\\line_top") {
414 par->line_top = true;
415 } else if (token == "\\line_bottom") {
416 par->line_bottom = true;
417 } else if (token == "\\pagebreak_top") {
418 par->pagebreak_top = true;
419 } else if (token == "\\pagebreak_bottom") {
420 par->pagebreak_bottom = true;
421 } else if (token == "\\start_of_appendix") {
422 par->start_of_appendix = true;
423 } else if (token == "\\paragraph_separation") {
424 tmpret = lex.FindToken(string_paragraph_separation);
425 if (tmpret == -1) ++tmpret;
426 if (tmpret != LYX_LAYOUT_DEFAULT)
427 params.paragraph_separation =
428 static_cast<BufferParams::PARSEP>(tmpret);
429 } else if (token == "\\defskip") {
431 params.defskip = VSpace(lex.GetString());
432 } else if (token == "\\no_isolatin1") { // obsolete
434 } else if (token == "\\no_babel") { // obsolete
436 } else if (token == "\\no_epsfig") { // obsolete
438 } else if (token == "\\epsfig") { // obsolete
439 // Indeed it is obsolete, but we HAVE to be backwards
440 // compatible until 0.14, because otherwise all figures
441 // in existing documents are irretrivably lost. (Asger)
442 params.readGraphicsDriver(lex);
443 } else if (token == "\\quotes_language") {
444 tmpret = lex.FindToken(string_quotes_language);
445 if (tmpret == -1) ++tmpret;
446 if (tmpret != LYX_LAYOUT_DEFAULT) {
447 InsetQuotes::quote_language tmpl =
448 InsetQuotes::EnglishQ;
451 tmpl = InsetQuotes::EnglishQ;
454 tmpl = InsetQuotes::SwedishQ;
457 tmpl = InsetQuotes::GermanQ;
460 tmpl = InsetQuotes::PolishQ;
463 tmpl = InsetQuotes::FrenchQ;
466 tmpl = InsetQuotes::DanishQ;
469 params.quotes_language = tmpl;
471 } else if (token == "\\quotes_times") {
473 switch(lex.GetInteger()) {
475 params.quotes_times = InsetQuotes::SingleQ;
478 params.quotes_times = InsetQuotes::DoubleQ;
481 } else if (token == "\\papersize") {
482 tmpret = lex.FindToken(string_papersize);
486 params.papersize2 = tmpret;
487 } else if (token == "\\paperpackage") {
488 tmpret = lex.FindToken(string_paperpackages);
491 params.paperpackage = BufferParams::PACKAGE_NONE;
493 params.paperpackage = tmpret;
494 } else if (token == "\\use_geometry") {
496 params.use_geometry = lex.GetInteger();
497 } else if (token == "\\use_amsmath") {
499 params.use_amsmath = lex.GetInteger();
500 } else if (token == "\\paperorientation") {
501 tmpret = lex.FindToken(string_orientation);
502 if (tmpret == -1) ++tmpret;
503 if (tmpret != LYX_LAYOUT_DEFAULT)
504 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
505 } else if (token == "\\paperwidth") {
507 params.paperwidth = lex.GetString();
508 } else if (token == "\\paperheight") {
510 params.paperheight = lex.GetString();
511 } else if (token == "\\leftmargin") {
513 params.leftmargin = lex.GetString();
514 } else if (token == "\\topmargin") {
516 params.topmargin = lex.GetString();
517 } else if (token == "\\rightmargin") {
519 params.rightmargin = lex.GetString();
520 } else if (token == "\\bottommargin") {
522 params.bottommargin = lex.GetString();
523 } else if (token == "\\headheight") {
525 params.headheight = lex.GetString();
526 } else if (token == "\\headsep") {
528 params.headsep = lex.GetString();
529 } else if (token == "\\footskip") {
531 params.footskip = lex.GetString();
532 } else if (token == "\\paperfontsize") {
534 params.fontsize = strip(lex.GetString());
535 } else if (token == "\\papercolumns") {
537 params.columns = lex.GetInteger();
538 } else if (token == "\\papersides") {
540 switch(lex.GetInteger()) {
542 case 1: params.sides = LyXTextClass::OneSide; break;
543 case 2: params.sides = LyXTextClass::TwoSides; break;
545 } else if (token == "\\paperpagestyle") {
547 params.pagestyle = strip(lex.GetString());
548 } else if (token == "\\bullet") {
550 int index = lex.GetInteger();
552 int temp_int = lex.GetInteger();
553 params.user_defined_bullets[index].setFont(temp_int);
554 params.temp_bullets[index].setFont(temp_int);
556 temp_int = lex.GetInteger();
557 params.user_defined_bullets[index].setCharacter(temp_int);
558 params.temp_bullets[index].setCharacter(temp_int);
560 temp_int = lex.GetInteger();
561 params.user_defined_bullets[index].setSize(temp_int);
562 params.temp_bullets[index].setSize(temp_int);
564 string temp_str = lex.GetString();
565 if (temp_str != "\\end_bullet") {
566 // this element isn't really necessary for
567 // parsing but is easier for humans
568 // to understand bullets. Put it back and
569 // set a debug message?
570 lex.printError("\\end_bullet expected, got" + temp_str);
571 //how can I put it back?
573 } else if (token == "\\bulletLaTeX") {
575 int index = lex.GetInteger();
577 string temp_str = lex.GetString(), sum_str;
578 while (temp_str != "\\end_bullet") {
579 // this loop structure is needed when user
580 // enters an empty string since the first
581 // thing returned will be the \\end_bullet
583 // if the LaTeX entry has spaces. Each element
584 // therefore needs to be read in turn
587 temp_str = lex.GetString();
589 params.user_defined_bullets[index].setText(sum_str);
590 params.temp_bullets[index].setText(sum_str);
591 } else if (token == "\\secnumdepth") {
593 params.secnumdepth = lex.GetInteger();
594 } else if (token == "\\tocdepth") {
596 params.tocdepth = lex.GetInteger();
597 } else if (token == "\\baselinestretch") { // obsolete
598 lex.nextToken(); // should not be used directly
600 // Will probably keep a kind of support just for
602 params.spacing.set(Spacing::Other, lex.GetFloat());
603 } else if (token == "\\spacing") {
605 string tmp = strip(lex.GetString());
606 if (tmp == "single") {
607 params.spacing.set(Spacing::Single);
608 } else if (tmp == "onehalf") {
609 params.spacing.set(Spacing::Onehalf);
610 } else if (tmp == "double") {
611 params.spacing.set(Spacing::Double);
612 } else if (tmp == "other") {
614 params.spacing.set(Spacing::Other,
617 lex.printError("Unknown spacing token: '$$Token'");
619 } else if (token == "\\float_placement") {
621 params.float_placement = lex.GetString();
622 } else if (token == "\\cursor") { // obsolete
623 // this is obsolete, so we just skip it.
625 } else if (token == "\\family") {
627 font.setLyXFamily(lex.GetString());
628 } else if (token == "\\series") {
630 font.setLyXSeries(lex.GetString());
631 } else if (token == "\\shape") {
633 font.setLyXShape(lex.GetString());
634 } else if (token == "\\size") {
636 font.setLyXSize(lex.GetString());
637 } else if (token == "\\latex") {
639 string tok = lex.GetString();
640 // This is dirty, but gone with LyX3. (Asger)
641 if (tok == "no_latex")
642 font.setLatex(LyXFont::OFF);
643 else if (tok == "latex")
644 font.setLatex(LyXFont::ON);
645 else if (tok == "default")
646 font.setLatex(LyXFont::INHERIT);
648 lex.printError("Unknown LaTeX font flag "
650 } else if (token == "\\direction") {
652 string tok = lex.GetString();
654 font.setDirection(LyXFont::LTR_DIR);
655 else if (tok == "rtl")
656 font.setDirection(LyXFont::RTL_DIR);
657 else if (tok == "default")
658 font.setDirection(LyXFont::INHERIT_DIR);
660 lex.printError("Unknown font flag "
662 } else if (token == "\\emph") {
664 font.setEmph(font.setLyXMisc(lex.GetString()));
665 } else if (token == "\\bar") {
667 string tok = lex.GetString();
668 // This is dirty, but gone with LyX3. (Asger)
670 font.setUnderbar(LyXFont::ON);
671 else if (tok == "no")
672 font.setUnderbar(LyXFont::OFF);
673 else if (tok == "default")
674 font.setUnderbar(LyXFont::INHERIT);
676 lex.printError("Unknown bar font flag "
678 } else if (token == "\\noun") {
680 font.setNoun(font.setLyXMisc(lex.GetString()));
681 } else if (token == "\\color") {
683 font.setLyXColor(lex.GetString());
684 } else if (token == "\\align") {
685 tmpret = lex.FindToken(string_align);
686 if (tmpret == -1) ++tmpret;
687 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
689 for (; tmpret > 0; --tmpret)
690 tmpret2 = tmpret2 * 2;
691 par->align = LyXAlignment(tmpret2);
693 } else if (token == "\\added_space_top") {
695 par->added_space_top = lex.GetString();
696 } else if (token == "\\added_space_bottom") {
698 par->added_space_bottom = lex.GetString();
699 } else if (token == "\\pextra_type") {
701 par->pextra_type = lex.GetInteger();
702 } else if (token == "\\pextra_width") {
704 par->pextra_width = lex.GetString();
705 } else if (token == "\\pextra_widthp") {
707 par->pextra_widthp = lex.GetString();
708 } else if (token == "\\pextra_alignment") {
710 par->pextra_alignment = lex.GetInteger();
711 } else if (token == "\\pextra_hfill") {
713 par->pextra_hfill = lex.GetInteger();
714 } else if (token == "\\pextra_start_minipage") {
716 par->pextra_start_minipage = lex.GetInteger();
717 } else if (token == "\\labelwidthstring") {
719 par->labelwidthstring = lex.GetString();
720 /* do not delete this token, it is still needed! */
721 } else if (token == "\\end_inset") {
722 /* simple ignore this. The insets do not have
724 // but insets should read it, it is a part of
725 //the inset isn't it? Lgb.
726 } else if (token == "\\begin_inset") {
728 tmptok = lex.GetString();
729 /* test the different insets */
730 if (tmptok == "Quotes") {
731 inset = new InsetQuotes;
733 par->InsertChar(pos, LyXParagraph::META_INSET);
734 par->InsertInset(pos, inset);
735 par->SetFont(pos, font);
737 } else if (tmptok == "\\i") {
738 inset = new InsetLatexAccent;
740 par->InsertChar(pos, LyXParagraph::META_INSET);
741 par->InsertInset(pos, inset);
742 par->SetFont(pos, font);
744 } else if (tmptok == "FormulaMacro") {
745 inset = new InsetFormulaMacro;
747 par->InsertChar(pos, LyXParagraph::META_INSET);
748 par->InsertInset(pos, inset);
749 par->SetFont(pos, font);
751 } else if (tmptok == "Formula") {
752 inset = new InsetFormula;
754 par->InsertChar(pos, LyXParagraph::META_INSET);
755 par->InsertInset(pos, inset);
756 par->SetFont(pos, font);
758 } else if (tmptok == "Figure") {
759 inset = new InsetFig(100, 100, this);
761 par->InsertChar(pos, LyXParagraph::META_INSET);
762 par->InsertInset(pos, inset);
763 par->SetFont(pos, font);
765 } else if (tmptok == "Info") {
766 inset = new InsetInfo;
768 par->InsertChar(pos, LyXParagraph::META_INSET);
769 par->InsertInset(pos, inset);
770 par->SetFont(pos, font);
772 } else if (tmptok == "Include") {
773 inset = new InsetInclude(string(), this);
775 par->InsertChar(pos, LyXParagraph::META_INSET);
776 par->InsertInset(pos, inset);
777 par->SetFont(pos, font);
779 } else if (tmptok == "ERT") {
780 inset = new InsetERT(this);
782 par->InsertChar(pos, LyXParagraph::META_INSET);
783 par->InsertInset(pos, inset);
784 par->SetFont(pos, font);
786 } else if (tmptok == "Text") {
787 inset = new InsetText(this);
789 par->InsertChar(pos, LyXParagraph::META_INSET);
790 par->InsertInset(pos, inset);
791 par->SetFont(pos, font);
793 } else if (tmptok == "Foot") {
794 inset = new InsetFoot(this);
796 par->InsertChar(pos, LyXParagraph::META_INSET);
797 par->InsertInset(pos, inset);
798 par->SetFont(pos, font);
800 } else if (tmptok == "GRAPHICS") {
801 inset = new InsetGraphics;
803 par->InsertChar(pos, LyXParagraph::META_INSET);
804 par->InsertInset(pos, inset);
805 par->SetFont(pos, font);
806 } else if (tmptok == "LatexCommand") {
809 if (inscmd.getCmdName() == "cite") {
810 inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
811 } else if (inscmd.getCmdName() == "bibitem") {
812 lex.printError("Wrong place for bibitem");
813 inset = inscmd.Clone();
814 } else if (inscmd.getCmdName() == "BibTeX") {
815 inset = new InsetBibtex(inscmd.getContents(), inscmd.getOptions(), this);
816 } else if (inscmd.getCmdName() == "index") {
817 inset = new InsetIndex(inscmd.getContents());
818 } else if (inscmd.getCmdName() == "include") {
819 inset = new InsetInclude(inscmd.getContents(), this);
820 } else if (inscmd.getCmdName() == "label") {
821 inset = new InsetLabel(inscmd.getCommand());
822 } else if (inscmd.getCmdName() == "url"
823 || inscmd.getCmdName() == "htmlurl") {
824 inset = new InsetUrl(inscmd.getCommand());
825 } else if (inscmd.getCmdName() == "ref"
826 || inscmd.getCmdName() == "pageref") {
827 if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
828 inset = new InsetRef(inscmd, this);
830 #warning Verify that this else clause is still needed. (Lgb)
832 // This condition comes from a
833 // temporary solution to the latexdel
834 // ref inset that was transformed to
835 // an empty ref inset plus the body
836 // surronded by latexdel insets
838 string cont, opt, tmptmptok, cmdname;
840 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
845 tmptmptok = lex.GetString();
846 if(tmptmptok[0] == '\\') {
847 if( tmptmptok == "\\backslash")
857 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
862 tmptmptok = lex.GetString();
863 if(tmptmptok[0] == '\\') {
864 if( tmptmptok == "\\backslash")
874 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
880 cmdname = "\\" + inscmd.getCmdName();
881 cmdname += "[" + cont + "]";
882 cmdname += "{" + opt + "}";
883 inset = new InsetRef(cmdname, this);
886 } else if (inscmd.getCmdName() == "tableofcontents") {
887 inset = new InsetTOC(this);
888 } else if (inscmd.getCmdName() == "listoffigures") {
889 inset = new InsetLOF(this);
890 } else if (inscmd.getCmdName() == "listofalgorithms") {
891 inset = new InsetLOA(this);
892 } else if (inscmd.getCmdName() == "listoftables") {
893 inset = new InsetLOT(this);
894 } else if (inscmd.getCmdName() == "printindex") {
895 inset = new InsetPrintIndex(this);
896 } else if (inscmd.getCmdName() == "lyxparent") {
897 inset = new InsetParent(inscmd.getContents(), this);
901 par->InsertChar(pos, LyXParagraph::META_INSET);
902 par->InsertInset(pos, inset);
903 par->SetFont(pos, font);
907 } else if (token == "\\InsetQuotes") {
908 inset = new InsetQuotes;
910 par->InsertChar(pos, LyXParagraph::META_INSET);
911 par->InsertInset(pos, inset);
912 par->SetFont(pos, font);
914 } else if (token == "\\InsetFormula") {
915 inset = new InsetFormula;
917 par->InsertChar(pos, LyXParagraph::META_INSET);
918 par->InsertInset(pos, inset);
919 par->SetFont(pos, font);
921 } else if (token == "\\SpecialChar") {
922 inset = new InsetSpecialChar;
924 par->InsertChar(pos, LyXParagraph::META_INSET);
925 par->InsertInset(pos, inset);
926 par->SetFont(pos, font);
928 } else if (token == "\\Figure") {
929 inset = new InsetFig(100, 100, this);
931 par->InsertChar(pos, LyXParagraph::META_INSET);
932 par->InsertInset(pos, inset);
933 par->SetFont(pos, font);
935 } else if (token == "\\newline") { // soon obsolete
937 par->InsertChar(pos, LyXParagraph::META_NEWLINE);
938 par->SetFont(pos, font);
940 inset = new InsetSpecialChar(InsetSpecialChar::NEWLINE);
941 par->InsertChar(pos, LyXParagraph::META_INSET);
942 par->InsertInset(pos, inset);
943 par->SetFont(pos, font);
946 } else if (token == "\\LyXTable") {
947 par->table = new LyXTable(lex);
948 } else if (token == "\\hfill") {
949 par->InsertChar(pos, LyXParagraph::META_HFILL);
950 par->SetFont(pos, font);
952 } else if (token == "\\protected_separator") { // obsolete
954 inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
955 par->InsertChar(pos, LyXParagraph::META_INSET);
956 par->InsertInset(pos, inset);
957 par->SetFont(pos, font);
959 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
960 par->SetFont(pos, font);
963 } else if (token == "\\bibitem") { // ale970302
965 par->bibkey = new InsetBibKey;
966 par->bibkey->Read(lex);
967 }else if (token == "\\backslash") {
968 par->InsertChar(pos, '\\');
969 par->SetFont(pos, font);
971 }else if (token == "\\the_end") {
974 // This should be insurance for the future: (Asger)
975 lex.printError("Unknown token `$$Token'. "
976 "Inserting as text.");
977 int n = token.length();
978 for (int i = 0; i < n; ++i) {
979 par->InsertChar(pos, token[i]);
980 par->SetFont(pos, font);
987 bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
993 token = lex.GetString();
994 if (token == "\\lyxformat") { // the first token _must_ be...
996 format = lex.GetFloat();
998 if (LYX_FORMAT - format > 0.05) {
999 printf(_("Warning: need lyxformat %.2f but found %.2f\n"),
1000 LYX_FORMAT, format);
1002 if (format - LYX_FORMAT > 0.05) {
1003 printf(_("ERROR: need lyxformat %.2f but found %.2f\n"),
1004 LYX_FORMAT, format);
1006 bool the_end = readLyXformat2(lex, par);
1007 // Formats >= 2.13 support "\the_end" marker
1014 WriteAlert(_("Warning!"),
1015 _("Reading of document is not complete"),
1016 _("Maybe the document is truncated"));
1017 // We simulate a safe reading anyways to allow
1018 // users to take the chance... (Asger)
1022 WriteAlert(_("ERROR!"),
1023 _("Old LyX file format found. "
1024 "Use LyX 0.10.x to read this!"));
1028 } else { // "\\lyxformat" not found
1029 WriteAlert(_("ERROR!"), _("Not a LyX file!"));
1032 WriteAlert(_("ERROR!"), _("Unable to read file!"));
1038 // Should probably be moved to somewhere else: BufferView? LyXView?
1039 bool Buffer::save(bool makeBackup) const
1041 // We don't need autosaves in the immediate future. (Asger)
1042 resetAutosaveTimers();
1046 string s = fileName() + '~';
1047 // Rename is the wrong way of making a backup,
1048 // this is the correct way.
1049 /* truss cp fil fil2:
1050 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
1051 stat("LyXVC.lyx", 0xEFFFF688) = 0
1052 open("LyXVC.lyx", O_RDONLY) = 3
1053 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1054 fstat(4, 0xEFFFF508) = 0
1055 fstat(3, 0xEFFFF508) = 0
1056 read(3, " # T h i s f i l e w".., 8192) = 5579
1057 write(4, " # T h i s f i l e w".., 5579) = 5579
1058 read(3, 0xEFFFD4A0, 8192) = 0
1061 chmod("LyXVC3.lyx", 0100644) = 0
1062 lseek(0, 0, SEEK_CUR) = 46440
1066 // Should proabaly have some more error checking here.
1067 // Should be cleaned up in 0.13, at least a bit.
1068 // Doing it this way, also makes the inodes stay the same.
1069 // This is still not a very good solution, in particular we
1070 // might loose the owner of the backup.
1071 FileInfo finfo(fileName());
1072 if (finfo.exist()) {
1073 mode_t fmode = finfo.getMode();
1074 struct utimbuf * times = new struct utimbuf;
1076 times->actime = finfo.getAccessTime();
1077 times->modtime = finfo.getModificationTime();
1078 ifstream ifs(fileName().c_str());
1079 ofstream ofs(s.c_str(), ios::out|ios::trunc);
1084 ::chmod(s.c_str(), fmode);
1086 if (::utime(s.c_str(), times)) {
1087 lyxerr << "utime error." << endl;
1090 lyxerr << "LyX was not able to make "
1091 "backupcopy. Beware." << endl;
1097 if (writeFile(fileName(), false)) {
1100 // now delete the autosavefile
1101 string a = OnlyPath(fileName());
1103 a += OnlyFilename(fileName());
1105 FileInfo fileinfo(a);
1106 if (fileinfo.exist()) {
1107 if (::remove(a.c_str()) != 0) {
1108 WriteFSAlert(_("Could not delete "
1109 "auto-save file!"), a);
1113 // Saving failed, so backup is not backup
1115 string s = fileName() + '~';
1116 ::rename(s.c_str(), fileName().c_str());
1124 // Returns false if unsuccesful
1125 bool Buffer::writeFile(string const & fname, bool flag) const
1127 // if flag is false writeFile will not create any GUI
1128 // warnings, only cerr.
1129 // Needed for autosave in background or panic save (Matthias 120496)
1131 if (read_only && (fname == filename)) {
1132 // Here we should come with a question if we should
1133 // perform the write anyway.
1135 lyxerr << _("Error! Document is read-only: ")
1138 WriteAlert(_("Error! Document is read-only: "),
1143 FileInfo finfo(fname);
1144 if (finfo.exist() && !finfo.writable()) {
1145 // Here we should come with a question if we should
1146 // try to do the save anyway. (i.e. do a chmod first)
1148 lyxerr << _("Error! Cannot write file: ")
1151 WriteFSAlert(_("Error! Cannot write file: "),
1156 ofstream ofs(fname.c_str());
1159 lyxerr << _("Error! Cannot open file: ")
1162 WriteFSAlert(_("Error! Cannot open file: "),
1166 // The top of the file should not be written by params.
1168 // write out a comment in the top of the file
1169 ofs << '#' << LYX_DOCVERSION
1170 << " created this file. For more info see http://www.lyx.org/\n";
1171 ofs.setf(ios::showpoint|ios::fixed);
1173 ofs << "\\lyxformat " << setw(4) << LYX_FORMAT << "\n";
1175 // now write out the buffer paramters.
1176 params.writeFile(ofs);
1178 char footnoteflag = 0;
1181 // this will write out all the paragraphs
1182 // using recursive descent.
1183 paragraph->writeFile(ofs, params, footnoteflag, depth);
1185 // Write marker that shows file is complete
1186 ofs << "\n\\the_end" << endl;
1188 // how to check if close went ok?
1193 void Buffer::writeFileAscii(string const & fname, int linelen)
1195 LyXFont font1, font2;
1197 char c, footnoteflag = 0, depth = 0;
1199 LyXParagraph::size_type i;
1200 int j, h, ltype = 0, ltype_depth = 0,
1201 * clen = 0, actcell = 0, actpos = 0, cell = 0, cells = 0,
1204 bool ref_printed = false;
1206 ofstream ofs(fname.c_str());
1208 WriteFSAlert(_("Error: Cannot write file:"), fname);
1212 string fname1 = TmpFileName();
1213 LyXParagraph * par = paragraph;
1217 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE ||
1219 par->previous->footnoteflag == LyXParagraph::NO_FOOTNOTE){
1221 /* begins a footnote environment ? */
1222 if (footnoteflag != par->footnoteflag) {
1223 footnoteflag = par->footnoteflag;
1225 j = strlen(string_footnotekinds[par->footnotekind])+4;
1226 if (currlinelen + j > linelen)
1229 << string_footnotekinds[par->footnotekind] << "] ";
1234 /* begins or ends a deeper area ?*/
1235 if (depth != par->depth) {
1236 if (par->depth > depth) {
1237 while (par->depth > depth) {
1242 while (par->depth < depth) {
1248 /* First write the layout */
1249 tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1250 if (tmp == "Itemize") {
1252 ltype_depth = depth+1;
1253 } else if (tmp == "Enumerate") {
1255 ltype_depth = depth+1;
1256 } else if (strstr(tmp.c_str(), "ection")) {
1258 ltype_depth = depth+1;
1259 } else if (strstr(tmp.c_str(), "aragraph")) {
1261 ltype_depth = depth+1;
1262 } else if (tmp == "Description") {
1264 ltype_depth = depth+1;
1265 } else if (tmp == "Abstract") {
1268 } else if (tmp == "Bibliography") {
1276 /* maybe some vertical spaces */
1278 /* the labelwidthstring used in lists */
1282 /* some pagebreaks? */
1286 /* what about the alignment */
1289 /* dummy layout, that means a footnote ended */
1290 footnoteflag = LyXParagraph::NO_FOOTNOTE;
1295 /* It might be a table */
1298 if (!lyxrc->ascii_roff_command.empty() &&
1299 lyxrc->ascii_roff_command != "none") {
1300 RoffAsciiTable(ofs, par);
1307 cells = par->table->columns;
1308 clen = new int [cells];
1309 memset(clen, 0, sizeof(int) * cells);
1310 for (i = 0, j = 0, h = 1; i < par->size(); ++i, ++h) {
1311 c = par->GetChar(i);
1312 if (c == LyXParagraph::META_INSET) {
1313 if ((inset = par->GetInset(i))) {
1317 inset->Latex(ost, -1);
1318 h += ost.str().length();
1321 inset->Latex(ost, -1);
1323 char * tmp = ost.str();
1329 ofstream fs(fname1.c_str());
1331 WriteFSAlert(_("Error: Cannot open temporary file:"), fname1);
1334 inset->Latex(fs, -1);
1335 h += fs.tellp() - 1;
1336 ::remove(fname1.c_str());
1339 } else if (c == LyXParagraph::META_NEWLINE) {
1343 j = (++j) % par->table->NumberOfCellsInRow(actcell);
1351 font1 = LyXFont(LyXFont::ALL_INHERIT);
1353 for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
1354 if (!i && !footnoteflag && !noparbreak){
1356 for(j = 0; j < depth; ++j)
1358 currlinelen = depth * 2;
1360 case 0: /* Standard */
1361 case 4: /* (Sub)Paragraph */
1362 case 5: /* Description */
1364 case 6: /* Abstract */
1365 ofs << "Abstract\n\n";
1367 case 7: /* Bibliography */
1369 ofs << "References\n\n";
1374 ofs << par->labelstring << " ";
1377 if (ltype_depth > depth) {
1378 for(j = ltype_depth - 1; j > depth; --j)
1380 currlinelen += (ltype_depth-depth)*2;
1383 for(j = 0; j < cells; ++j) {
1385 for(h = 0; h < (clen[j] + 1);
1390 for(j = 0; j < depth; ++j)
1392 currlinelen = depth * 2;
1393 if (ltype_depth > depth) {
1394 for(j = ltype_depth;
1397 currlinelen += (ltype_depth-depth)*2;
1402 font2 = par->GetFontSettings(i);
1403 if (font1.latex() != font2.latex()) {
1404 if (font2.latex() == LyXFont::OFF)
1412 c = par->GetChar(i);
1416 case LyXParagraph::META_INSET:
1417 if ((inset = par->GetInset(i))) {
1419 inset->Latex(ofs, -1);
1420 currlinelen += (ofs.tellp() - fpos);
1421 actpos += (ofs.tellp() - fpos) - 1;
1424 case LyXParagraph::META_NEWLINE:
1426 if (par->table->NumberOfCellsInRow(actcell) <= cell) {
1427 for(j = actpos; j < clen[cell - 1]; ++j)
1430 for(j = 0; j < depth; ++j)
1432 currlinelen = depth*2;
1433 if (ltype_depth > depth) {
1434 for(j = ltype_depth; j > depth; --j)
1436 currlinelen += (ltype_depth-depth) * 2;
1438 for(j = 0; j < cells; ++j) {
1440 for(h = 0; h < (clen[j] + 1); ++h)
1444 for(j = 0; j < depth; ++j)
1446 currlinelen = depth * 2;
1447 if (ltype_depth > depth) {
1448 for(j = ltype_depth;
1451 currlinelen += (ltype_depth-depth)*2;
1457 j < clen[cell - 1]; ++j)
1463 currlinelen = actpos = 0;
1466 for(j = 0; j < depth; ++j)
1468 currlinelen = depth * 2;
1469 if (ltype_depth > depth) {
1470 for(j = ltype_depth;
1473 currlinelen += (ltype_depth - depth) * 2;
1477 case LyXParagraph::META_HFILL:
1481 case LyXParagraph::META_PROTECTED_SEPARATOR:
1489 if (currlinelen > linelen - 10
1490 && c == ' ' && i + 2 < par->size()) {
1492 for(j = 0; j < depth; ++j)
1494 currlinelen = depth * 2;
1495 if (ltype_depth > depth) {
1496 for(j = ltype_depth;
1499 currlinelen += (ltype_depth-depth)*2;
1501 } else if (c != '\0')
1504 lyxerr.debug() << "writeAsciiFile: NULL char in structure." << endl;
1510 for(j = actpos; j < clen[cell - 1]; ++j)
1513 for(j = 0; j < depth; ++j)
1515 currlinelen = depth * 2;
1516 if (ltype_depth > depth) {
1517 for(j = ltype_depth; j > depth; --j)
1519 currlinelen += (ltype_depth - depth) * 2;
1521 for(j = 0; j < cells; ++j) {
1523 for(h = 0; h < (clen[j] + 1); ++h)
1536 #ifdef USE_OSTREAM_ONLY
1537 void Buffer::makeLaTeXFile(string const & fname,
1538 string const & original_path,
1539 bool nice, bool only_body)
1541 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1543 niceFile = nice; // this will be used by Insetincludes.
1545 tex_code_break_column = lyxrc->ascii_linelen;
1547 LyXTextClass const & tclass =
1548 textclasslist.TextClass(params.textclass);
1550 ofstream ofs(fname.c_str());
1552 WriteFSAlert(_("Error: Cannot open file: "), fname);
1556 // validate the buffer.
1557 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1558 LaTeXFeatures features(tclass.numLayouts());
1560 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1563 // The starting paragraph of the coming rows is the
1564 // first paragraph of the document. (Asger)
1565 texrow.start(paragraph, 0);
1567 if (!only_body && nice) {
1568 ofs << "%% " LYX_DOCVERSION " created this file. "
1569 "For more info, see http://www.lyx.org/.\n"
1570 "%% Do not edit unless you really know what "
1575 lyxerr.debug() << "lyx header finished" << endl;
1576 // There are a few differences between nice LaTeX and usual files:
1577 // usual is \batchmode and has a
1578 // special input@path to allow the including of figures
1579 // with either \input or \includegraphics (what figinsets do).
1580 // batchmode is not set if there is a tex_code_break_column.
1581 // In this case somebody is interested in the generated LaTeX,
1582 // so this is OK. input@path is set when the actual parameter
1583 // original_path is set. This is done for usual tex-file, but not
1584 // for nice-latex-file. (Matthias 250696)
1587 // code for usual, NOT nice-latex-file
1588 ofs << "\\batchmode\n"; // changed
1589 // from \nonstopmode
1592 if (!original_path.empty()) {
1593 ofs << "\\makeatletter\n"
1594 << "\\def\\input@path{{"
1595 << original_path << "/}}\n"
1596 << "\\makeatother\n";
1602 ofs << "\\documentclass";
1604 string options; // the document class options.
1606 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
1607 // only write if existing in list (and not default)
1608 options += params.fontsize;
1613 if (!params.use_geometry &&
1614 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
1615 switch (params.papersize) {
1616 case BufferParams::PAPER_A4PAPER:
1617 options += "a4paper,";
1619 case BufferParams::PAPER_USLETTER:
1620 options += "letterpaper,";
1622 case BufferParams::PAPER_A5PAPER:
1623 options += "a5paper,";
1625 case BufferParams::PAPER_B5PAPER:
1626 options += "b5paper,";
1628 case BufferParams::PAPER_EXECUTIVEPAPER:
1629 options += "executivepaper,";
1631 case BufferParams::PAPER_LEGALPAPER:
1632 options += "legalpaper,";
1638 if (params.sides != tclass.sides()) {
1639 switch (params.sides) {
1640 case LyXTextClass::OneSide:
1641 options += "oneside,";
1643 case LyXTextClass::TwoSides:
1644 options += "twoside,";
1651 if (params.columns != tclass.columns()) {
1652 if (params.columns == 2)
1653 options += "twocolumn,";
1655 options += "onecolumn,";
1658 if (!params.use_geometry
1659 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1660 options += "landscape,";
1662 // language should be a parameter to \documentclass
1663 if (params.language != "default") {
1664 if (params.language == "hebrew")
1665 options += "english,";
1666 else if (lyxrc->rtl_support)
1667 options += "hebrew,";
1668 options += params.language + ',';
1669 } else if (lyxrc->rtl_support)
1670 options += "hebrew,english,";
1672 // the user-defined options
1673 if (!params.options.empty()) {
1674 options += params.options + ',';
1677 if (!options.empty()){
1678 options = strip(options, ',');
1679 ofs << '[' << options << ']';
1683 << textclasslist.LatexnameOfClass(params.textclass)
1686 // end of \documentclass defs
1688 // font selection must be done before loading fontenc.sty
1689 if (params.fonts != "default") {
1690 ofs << "\\usepackage{" << params.fonts << "}\n";
1693 // this one is not per buffer
1694 if (lyxrc->fontenc != "default") {
1695 ofs << "\\usepackage[" << lyxrc->fontenc
1699 if (params.inputenc != "default") {
1700 ofs << "\\usepackage[" << params.inputenc
1705 /* at the very beginning the text parameters */
1706 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
1707 switch (params.paperpackage) {
1708 case BufferParams::PACKAGE_A4:
1709 ofs << "\\usepackage{a4}\n";
1712 case BufferParams::PACKAGE_A4WIDE:
1713 ofs << "\\usepackage{a4wide}\n";
1716 case BufferParams::PACKAGE_WIDEMARGINSA4:
1717 ofs << "\\usepackage[widemargins]{a4}\n";
1722 if (params.use_geometry) {
1723 ofs << "\\usepackage{geometry}\n";
1725 ofs << "\\geometry{verbose";
1726 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1727 ofs << ",landscape";
1728 switch (params.papersize2) {
1729 case BufferParams::VM_PAPER_CUSTOM:
1730 if (!params.paperwidth.empty())
1731 ofs << ",paperwidth="
1732 << params.paperwidth;
1733 if (!params.paperheight.empty())
1734 ofs << ",paperheight="
1735 << params.paperheight;
1737 case BufferParams::VM_PAPER_USLETTER:
1738 ofs << ",letterpaper";
1740 case BufferParams::VM_PAPER_USLEGAL:
1741 ofs << ",legalpaper";
1743 case BufferParams::VM_PAPER_USEXECUTIVE:
1744 ofs << ",executivepaper";
1746 case BufferParams::VM_PAPER_A3:
1749 case BufferParams::VM_PAPER_A4:
1752 case BufferParams::VM_PAPER_A5:
1755 case BufferParams::VM_PAPER_B3:
1758 case BufferParams::VM_PAPER_B4:
1761 case BufferParams::VM_PAPER_B5:
1765 // default papersize ie BufferParams::VM_PAPER_DEFAULT
1766 switch (lyxrc->default_papersize) {
1767 case BufferParams::PAPER_DEFAULT: // keep compiler happy
1768 case BufferParams::PAPER_USLETTER:
1769 ofs << ",letterpaper";
1771 case BufferParams::PAPER_LEGALPAPER:
1772 ofs << ",legalpaper";
1774 case BufferParams::PAPER_EXECUTIVEPAPER:
1775 ofs << ",executivepaper";
1777 case BufferParams::PAPER_A3PAPER:
1780 case BufferParams::PAPER_A4PAPER:
1783 case BufferParams::PAPER_A5PAPER:
1786 case BufferParams::PAPER_B5PAPER:
1791 if (!params.topmargin.empty())
1792 ofs << ",tmargin=" << params.topmargin;
1793 if (!params.bottommargin.empty())
1794 ofs << ",bmargin=" << params.bottommargin;
1795 if (!params.leftmargin.empty())
1796 ofs << ",lmargin=" << params.leftmargin;
1797 if (!params.rightmargin.empty())
1798 ofs << ",rmargin=" << params.rightmargin;
1799 if (!params.headheight.empty())
1800 ofs << ",headheight=" << params.headheight;
1801 if (!params.headsep.empty())
1802 ofs << ",headsep=" << params.headsep;
1803 if (!params.footskip.empty())
1804 ofs << ",footskip=" << params.footskip;
1808 if (params.use_amsmath
1809 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
1810 ofs << "\\usepackage{amsmath}\n";
1814 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
1815 if (params.pagestyle == "fancy") {
1816 ofs << "\\usepackage{fancyhdr}\n";
1819 ofs << "\\pagestyle{" << params.pagestyle << "}\n";
1823 // We try to load babel late, in case it interferes
1824 // with other packages.
1825 if (params.language != "default" || lyxrc->rtl_support ) {
1826 ofs << "\\usepackage{babel}\n";
1830 if (params.secnumdepth != tclass.secnumdepth()) {
1831 ofs << "\\setcounter{secnumdepth}{"
1832 << params.secnumdepth
1836 if (params.tocdepth != tclass.tocdepth()) {
1837 ofs << "\\setcounter{tocdepth}{"
1843 if (params.paragraph_separation) {
1844 switch (params.defskip.kind()) {
1845 case VSpace::SMALLSKIP:
1846 ofs << "\\setlength\\parskip{\\smallskipamount}\n";
1848 case VSpace::MEDSKIP:
1849 ofs << "\\setlength\\parskip{\\medskipamount}\n";
1851 case VSpace::BIGSKIP:
1852 ofs << "\\setlength\\parskip{\\bigskipamount}\n";
1854 case VSpace::LENGTH:
1855 ofs << "\\setlength\\parskip{"
1856 << params.defskip.length().asLatexString()
1859 default: // should never happen // Then delete it.
1860 ofs << "\\setlength\\parskip{\\medskipamount}\n";
1865 ofs << "\\setlength\\parindent{0pt}\n";
1869 // Now insert the LyX specific LaTeX commands...
1870 string preamble, tmppreamble;
1872 // The optional packages;
1873 preamble = features.getPackages(params);
1875 // this might be useful...
1876 preamble += "\n\\makeatletter\n\n";
1878 // Some macros LyX will need
1879 tmppreamble = features.getMacros(params);
1881 if (!tmppreamble.empty()) {
1882 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1883 "LyX specific LaTeX commands.\n"
1884 + tmppreamble + '\n';
1887 // the text class specific preamble
1888 tmppreamble = features.getTClassPreamble(params);
1889 if (!tmppreamble.empty()) {
1890 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1891 "Textclass specific LaTeX commands.\n"
1892 + tmppreamble + '\n';
1895 /* the user-defined preamble */
1896 if (!params.preamble.empty()) {
1897 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1898 "User specified LaTeX commands.\n"
1899 + params.preamble + '\n';
1902 preamble += "\\makeatother\n\n";
1904 // Itemize bullet settings need to be last in case the user
1905 // defines their own bullets that use a package included
1906 // in the user-defined preamble -- ARRae
1907 // Actually it has to be done much later than that
1908 // since some packages like frenchb make modifications
1909 // at \begin{document} time -- JMarc
1911 for (int i = 0; i < 4; ++i) {
1912 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
1913 if (bullets_def.empty())
1914 bullets_def="\\AtBeginDocument{\n";
1915 bullets_def += " \\renewcommand{\\labelitemi";
1917 // `i' is one less than the item to modify
1924 bullets_def += "ii";
1930 bullets_def += "}{" +
1931 params.user_defined_bullets[i].getText()
1936 if (!bullets_def.empty())
1937 preamble += bullets_def + "}\n\n";
1939 for (int j = countChar(preamble, '\n'); j-- ;) {
1946 ofs << "\\begin{document}\n\n";
1950 lyxerr.debug() << "preamble finished, now the body." << endl;
1952 bool was_title = false;
1953 bool already_title = false;
1955 ostringstream ftnote;
1957 char * tmpholder = 0;
1962 LyXParagraph * par = paragraph;
1966 #ifndef HAVE_SSTREAM
1969 ftnote << tmpholder;
1970 delete [] tmpholder;
1975 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
1977 LyXLayout const & layout =
1978 textclasslist.Style(params.textclass,
1981 if (layout.intitle) {
1982 if (already_title) {
1983 lyxerr <<"Error in MakeLatexFile: You"
1984 " should not mix title layouts"
1985 " with normal ones." << endl;
1988 } else if (was_title && !already_title) {
1989 ofs << "\\maketitle\n";
1991 already_title = true;
1994 // We are at depth 0 so we can just use
1995 // ordinary \footnote{} generation
1996 // flag this with ftcount
1998 if (layout.isEnvironment()
1999 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
2000 par = par->TeXEnvironment(ofs, texrow,
2001 ftnote, ft_texrow, ftcount);
2003 par = par->TeXOnePar(ofs, texrow,
2004 ftnote, ft_texrow, ftcount);
2007 // Write out what we've generated...
2010 ofs << "\\addtocounter{footnote}{-"
2014 ofs << ftnote.str();
2015 texrow += ft_texrow;
2017 // The extra .c_str() is needed when we use
2018 // lyxstring instead of the STL string class.
2019 ftnote.str(string().c_str());
2021 delete [] ftnote.str();
2026 #ifndef HAVE_SSTREAM
2028 // I hate strstreams
2029 tmpholder = ftnote.str();
2033 #ifndef HAVE_SSTREAM
2034 delete [] tmpholder;
2036 // It might be that we only have a title in this document
2037 if (was_title && !already_title) {
2038 ofs << "\\maketitle\n";
2043 ofs << "\\end{document}\n";
2046 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2048 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2052 // Just to be sure. (Asger)
2055 // tex_code_break_column's value is used to decide
2056 // if we are in batchmode or not (within mathed_write()
2057 // in math_write.C) so we must set it to a non-zero
2058 // value when we leave otherwise we save incorrect .lyx files.
2059 tex_code_break_column = lyxrc->ascii_linelen;
2063 lyxerr << "File was not closed properly." << endl;
2066 lyxerr.debug() << "Finished making latex file." << endl;
2069 void Buffer::makeLaTeXFile(string const & fname,
2070 string const & original_path,
2071 bool nice, bool only_body)
2073 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
2075 niceFile = nice; // this will be used by Insetincludes.
2077 tex_code_break_column = lyxrc->ascii_linelen;
2079 LyXTextClass const & tclass =
2080 textclasslist.TextClass(params.textclass);
2082 ofstream ofs(fname.c_str());
2084 WriteFSAlert(_("Error: Cannot open file: "), fname);
2088 // validate the buffer.
2089 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
2090 LaTeXFeatures features(tclass.numLayouts());
2092 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
2095 // The starting paragraph of the coming rows is the
2096 // first paragraph of the document. (Asger)
2097 texrow.start(paragraph, 0);
2101 if (!only_body && nice) {
2102 LFile += "%% " LYX_DOCVERSION " created this file. "
2103 "For more info, see http://www.lyx.org/.\n";
2104 LFile += "%% Do not edit unless you really know what you are doing.\n";
2108 lyxerr.debug() << "lyx header finished" << endl;
2109 // There are a few differences between nice LaTeX and usual files:
2110 // usual is \batchmode and has a
2111 // special input@path to allow the including of figures
2112 // with either \input or \includegraphics (what figinsets do).
2113 // batchmode is not set if there is a tex_code_break_column.
2114 // In this case somebody is interested in the generated LaTeX,
2115 // so this is OK. input@path is set when the actual parameter
2116 // original_path is set. This is done for usual tex-file, but not
2117 // for nice-latex-file. (Matthias 250696)
2120 // code for usual, NOT nice-latex-file
2121 LFile += "\\batchmode\n"; // changed
2122 // from \nonstopmode
2125 if (!original_path.empty()) {
2126 LFile += "\\makeatletter\n";
2128 LFile += "\\def\\input@path{{" + original_path
2131 LFile += "\\makeatother\n";
2135 LFile += "\\documentclass";
2137 string options; // the document class options.
2139 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
2140 // only write if existing in list (and not default)
2141 options += params.fontsize;
2146 if (!params.use_geometry &&
2147 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2148 switch (params.papersize) {
2149 case BufferParams::PAPER_A4PAPER:
2150 options += "a4paper,";
2152 case BufferParams::PAPER_USLETTER:
2153 options += "letterpaper,";
2155 case BufferParams::PAPER_A5PAPER:
2156 options += "a5paper,";
2158 case BufferParams::PAPER_B5PAPER:
2159 options += "b5paper,";
2161 case BufferParams::PAPER_EXECUTIVEPAPER:
2162 options += "executivepaper,";
2164 case BufferParams::PAPER_LEGALPAPER:
2165 options += "legalpaper,";
2171 if (params.sides != tclass.sides()) {
2172 switch (params.sides) {
2173 case LyXTextClass::OneSide:
2174 options += "oneside,";
2176 case LyXTextClass::TwoSides:
2177 options += "twoside,";
2184 if (params.columns != tclass.columns()) {
2185 if (params.columns == 2)
2186 options += "twocolumn,";
2188 options += "onecolumn,";
2191 if (!params.use_geometry
2192 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2193 options += "landscape,";
2195 // language should be a parameter to \documentclass
2196 if (params.language != "default") {
2197 if (params.language == "hebrew")
2198 options += "english,";
2199 else if (lyxrc->rtl_support)
2200 options += "hebrew,";
2201 options += params.language + ',';
2202 } else if (lyxrc->rtl_support)
2203 options += "hebrew,english,";
2205 // the user-defined options
2206 if (!params.options.empty()) {
2207 options += params.options + ',';
2210 if (!options.empty()){
2211 options = strip(options, ',');
2218 LFile += textclasslist.LatexnameOfClass(params.textclass);
2221 // end of \documentclass defs
2223 // font selection must be done before loading fontenc.sty
2224 if (params.fonts != "default") {
2225 LFile += "\\usepackage{" + params.fonts + "}\n";
2228 // this one is not per buffer
2229 if (lyxrc->fontenc != "default") {
2230 LFile += "\\usepackage[" + lyxrc->fontenc
2234 if (params.inputenc != "default") {
2235 LFile += "\\usepackage[" + params.inputenc
2240 /* at the very beginning the text parameters */
2241 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2242 switch (params.paperpackage) {
2243 case BufferParams::PACKAGE_A4:
2244 LFile += "\\usepackage{a4}\n";
2247 case BufferParams::PACKAGE_A4WIDE:
2248 LFile += "\\usepackage{a4wide}\n";
2251 case BufferParams::PACKAGE_WIDEMARGINSA4:
2252 LFile += "\\usepackage[widemargins]{a4}\n";
2257 if (params.use_geometry) {
2258 LFile += "\\usepackage{geometry}\n";
2260 LFile += "\\geometry{verbose";
2261 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2262 LFile += ",landscape";
2263 switch (params.papersize2) {
2264 case BufferParams::VM_PAPER_CUSTOM:
2265 if (!params.paperwidth.empty())
2266 LFile += ",paperwidth="
2267 + params.paperwidth;
2268 if (!params.paperheight.empty())
2269 LFile += ",paperheight="
2270 + params.paperheight;
2272 case BufferParams::VM_PAPER_USLETTER:
2273 LFile += ",letterpaper";
2275 case BufferParams::VM_PAPER_USLEGAL:
2276 LFile += ",legalpaper";
2278 case BufferParams::VM_PAPER_USEXECUTIVE:
2279 LFile += ",executivepaper";
2281 case BufferParams::VM_PAPER_A3:
2282 LFile += ",a3paper";
2284 case BufferParams::VM_PAPER_A4:
2285 LFile += ",a4paper";
2287 case BufferParams::VM_PAPER_A5:
2288 LFile += ",a5paper";
2290 case BufferParams::VM_PAPER_B3:
2291 LFile += ",b3paper";
2293 case BufferParams::VM_PAPER_B4:
2294 LFile += ",b4paper";
2296 case BufferParams::VM_PAPER_B5:
2297 LFile += ",b5paper";
2300 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2301 switch (lyxrc->default_papersize) {
2302 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2303 case BufferParams::PAPER_USLETTER:
2304 LFile += ",letterpaper";
2306 case BufferParams::PAPER_LEGALPAPER:
2307 LFile += ",legalpaper";
2309 case BufferParams::PAPER_EXECUTIVEPAPER:
2310 LFile += ",executivepaper";
2312 case BufferParams::PAPER_A3PAPER:
2313 LFile += ",a3paper";
2315 case BufferParams::PAPER_A4PAPER:
2316 LFile += ",a4paper";
2318 case BufferParams::PAPER_A5PAPER:
2319 LFile += ",a5paper";
2321 case BufferParams::PAPER_B5PAPER:
2322 LFile += ",b5paper";
2326 if (!params.topmargin.empty())
2327 LFile += ",tmargin=" + params.topmargin;
2328 if (!params.bottommargin.empty())
2329 LFile += ",bmargin=" + params.bottommargin;
2330 if (!params.leftmargin.empty())
2331 LFile += ",lmargin=" + params.leftmargin;
2332 if (!params.rightmargin.empty())
2333 LFile += ",rmargin=" + params.rightmargin;
2334 if (!params.headheight.empty())
2335 LFile += ",headheight=" + params.headheight;
2336 if (!params.headsep.empty())
2337 LFile += ",headsep=" + params.headsep;
2338 if (!params.footskip.empty())
2339 LFile += ",footskip=" + params.footskip;
2343 if (params.use_amsmath
2344 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
2345 LFile += "\\usepackage{amsmath}\n";
2348 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
2349 if (params.pagestyle == "fancy") {
2350 LFile += "\\usepackage{fancyhdr}\n";
2353 LFile += "\\pagestyle{" + params.pagestyle + "}\n";
2357 // We try to load babel late, in case it interferes
2358 // with other packages.
2359 if (params.language != "default" || lyxrc->rtl_support ) {
2360 LFile += "\\usepackage{babel}\n";
2364 if (params.secnumdepth != tclass.secnumdepth()) {
2365 LFile += "\\setcounter{secnumdepth}{";
2366 LFile += tostr(params.secnumdepth);
2370 if (params.tocdepth != tclass.tocdepth()) {
2371 LFile += "\\setcounter{tocdepth}{";
2372 LFile += tostr(params.tocdepth);
2377 if (params.paragraph_separation) {
2378 switch (params.defskip.kind()) {
2379 case VSpace::SMALLSKIP:
2380 LFile += "\\setlength\\parskip{\\smallskipamount}\n";
2382 case VSpace::MEDSKIP:
2383 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2385 case VSpace::BIGSKIP:
2386 LFile += "\\setlength\\parskip{\\bigskipamount}\n";
2388 case VSpace::LENGTH:
2389 LFile += "\\setlength\\parskip{"
2390 + params.defskip.length().asLatexString()
2393 default: // should never happen // Then delete it.
2394 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2399 LFile += "\\setlength\\parindent{0pt}\n";
2403 // Write out what we've generated so far...and reset LFile
2407 // Now insert the LyX specific LaTeX commands...
2408 string preamble, tmppreamble;
2410 // The optional packages;
2411 preamble = features.getPackages(params);
2413 // this might be useful...
2414 preamble += "\n\\makeatletter\n\n";
2416 // Some macros LyX will need
2417 tmppreamble = features.getMacros(params);
2419 if (!tmppreamble.empty()) {
2420 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2421 "LyX specific LaTeX commands.\n"
2422 + tmppreamble + '\n';
2425 // the text class specific preamble
2426 tmppreamble = features.getTClassPreamble(params);
2427 if (!tmppreamble.empty()) {
2428 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2429 "Textclass specific LaTeX commands.\n"
2430 + tmppreamble + '\n';
2433 /* the user-defined preamble */
2434 if (!params.preamble.empty()) {
2435 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2436 "User specified LaTeX commands.\n"
2437 + params.preamble + '\n';
2440 preamble += "\\makeatother\n\n";
2442 // Itemize bullet settings need to be last in case the user
2443 // defines their own bullets that use a package included
2444 // in the user-defined preamble -- ARRae
2445 // Actually it has to be done much later than that
2446 // since some packages like frenchb make modifications
2447 // at \begin{document} time -- JMarc
2449 for (int i = 0; i < 4; ++i) {
2450 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2451 if (bullets_def.empty())
2452 bullets_def="\\AtBeginDocument{\n";
2453 bullets_def += " \\renewcommand{\\labelitemi";
2455 // `i' is one less than the item to modify
2462 bullets_def += "ii";
2468 bullets_def += "}{" +
2469 params.user_defined_bullets[i].getText()
2474 if (!bullets_def.empty())
2475 preamble += bullets_def + "}\n\n";
2477 for (int j = countChar(preamble, '\n'); j-- ;) {
2484 LFile += "\\begin{document}\n\n";
2488 lyxerr.debug() << "preamble finished, now the body." << endl;
2490 bool was_title = false;
2491 bool already_title = false;
2497 LyXParagraph * par = paragraph;
2503 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
2505 LyXLayout const & layout =
2506 textclasslist.Style(params.textclass,
2509 if (layout.intitle) {
2510 if (already_title) {
2511 lyxerr <<"Error in MakeLatexFile: You"
2512 " should not mix title layouts"
2513 " with normal ones." << endl;
2516 } else if (was_title && !already_title) {
2517 LFile += "\\maketitle\n";
2519 already_title = true;
2522 // We are at depth 0 so we can just use
2523 // ordinary \footnote{} generation
2524 // flag this with ftcount
2526 if (layout.isEnvironment()
2527 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
2528 par = par->TeXEnvironment(LFile, texrow,
2529 ftnote, ft_texrow, ftcount);
2531 par = par->TeXOnePar(LFile, texrow,
2532 ftnote, ft_texrow, ftcount);
2535 // Write out what we've generated...and reset LFile
2538 LFile += "\\addtocounter{footnote}{-";
2539 LFile += tostr(ftcount - 1);
2543 texrow += ft_texrow;
2548 if (loop_count == 2) {
2549 // fwrite()ing every second time through the loop
2550 // gains a few extra % of speed; going higher than
2551 // 2 will slow things down again. I'll look at
2552 // LFile.length() in a future revision. ARRae
2559 // It might be that we only have a title in this document
2560 if (was_title && !already_title) {
2561 LFile += "\\maketitle\n";
2566 LFile += "\\end{document}\n";
2569 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2571 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2575 // Just to be sure. (Asger)
2578 // Write out what we've generated...and reset LFile
2582 // tex_code_break_column's value is used to decide
2583 // if we are in batchmode or not (within mathed_write()
2584 // in math_write.C) so we must set it to a non-zero
2585 // value when we leave otherwise we save incorrect .lyx files.
2586 tex_code_break_column = lyxrc->ascii_linelen;
2588 // How to check if the close went ok when using fstreams
2590 lyxerr.debug() << "Finished making latex file." << endl;
2595 bool Buffer::isLatex() const
2597 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2601 bool Buffer::isLinuxDoc() const
2603 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2607 bool Buffer::isLiterate() const
2609 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2613 bool Buffer::isDocBook() const
2615 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2619 bool Buffer::isSGML() const
2621 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2622 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2626 void Buffer::sgmlOpenTag(ostream & os, int depth,
2627 string const & latexname) const
2629 os << string(depth, ' ') << "<" << latexname << ">\n";
2633 void Buffer::sgmlCloseTag(ostream & os, int depth,
2634 string const & latexname) const
2636 os << string(depth, ' ') << "</" << latexname << ">\n";
2640 void Buffer::makeLinuxDocFile(string const & fname, int column)
2642 LyXParagraph * par = paragraph;
2644 string top_element = textclasslist.LatexnameOfClass(params.textclass);
2645 string environment_stack[10];
2648 int depth = 0; /* paragraph depth */
2650 ofstream ofs(fname.c_str());
2653 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2657 tex_code_break_column = column;
2660 if (params.preamble.empty()) {
2661 ofs << "<!doctype linuxdoc system>\n\n";
2664 ofs << "<!doctype linuxdoc system \n [ "
2665 << params.preamble << " \n]>\n\n";
2668 ofs << "<!-- " << LYX_DOCVERSION
2669 << " created this file. For more info see http://www.lyx.org/"
2672 if(params.options.empty())
2673 sgmlOpenTag(ofs, 0, top_element);
2675 string top = top_element;
2677 top += params.options;
2678 sgmlOpenTag(ofs, 0, top);
2682 int desc_on = 0; /* description mode*/
2683 LyXLayout const & style =
2684 textclasslist.Style(users->buffer()->params.textclass,
2686 par->AutoDeleteInsets();
2688 /* treat <toc> as a special case for compatibility with old code */
2689 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2690 Inset * inset = par->GetInset(0);
2691 char lyx_code = inset->LyxCode();
2692 if (lyx_code == Inset::TOC_CODE){
2693 string temp = "toc";
2694 sgmlOpenTag(ofs, depth, temp);
2697 linuxDocHandleFootnote(ofs, par, depth);
2702 // environment tag closing
2703 for( ; depth > par->depth; --depth) {
2704 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2705 environment_stack[depth].clear();
2708 // write opening SGML tags
2709 switch(style.latextype) {
2710 case LATEX_PARAGRAPH:
2711 if(depth == par->depth
2712 && !environment_stack[depth].empty()) {
2713 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2714 environment_stack[depth].clear();
2720 sgmlOpenTag(ofs, depth, style.latexname());
2725 LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
2727 if (!environment_stack[depth].empty()){
2728 sgmlCloseTag(ofs, depth,
2729 environment_stack[depth]);
2733 environment_stack[depth].clear();
2734 sgmlOpenTag(ofs, depth, style.latexname());
2737 case LATEX_ENVIRONMENT:
2738 case LATEX_ITEM_ENVIRONMENT:
2739 if(depth == par->depth
2740 && environment_stack[depth] != style.latexname()
2741 && !environment_stack[depth].empty()) {
2743 sgmlCloseTag(ofs, depth,
2744 environment_stack[depth]);
2745 environment_stack[depth].clear();
2747 if (depth < par->depth) {
2749 environment_stack[depth].clear();
2751 if (environment_stack[depth] != style.latexname()) {
2754 sgmlOpenTag(ofs, depth, temp);
2756 environment_stack[depth] = style.latexname();
2757 sgmlOpenTag(ofs, depth, environment_stack[depth]);
2759 if(style.latextype == LATEX_ENVIRONMENT) break;
2761 desc_on = (style.labeltype == LABEL_MANUAL);
2768 sgmlOpenTag(ofs, depth + 1, item_name);
2771 sgmlOpenTag(ofs, depth, style.latexname());
2776 SimpleLinuxDocOnePar(ofs, par, desc_on, depth);
2779 linuxDocHandleFootnote(ofs, par, depth);
2781 while(par && par->IsDummy());
2784 /* write closing SGML tags */
2785 switch(style.latextype) {
2787 case LATEX_ENVIRONMENT:
2788 case LATEX_ITEM_ENVIRONMENT:
2791 sgmlCloseTag(ofs, depth, style.latexname());
2796 /* Close open tags */
2797 for(; depth > 0; --depth)
2798 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2800 if(!environment_stack[depth].empty())
2801 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2804 sgmlCloseTag(ofs, 0, top_element);
2807 // How to check for successful close
2811 void Buffer::linuxDocHandleFootnote(ostream & os, LyXParagraph * & par,
2814 string tag = "footnote";
2816 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2817 sgmlOpenTag(os, depth + 1, tag);
2818 SimpleLinuxDocOnePar(os, par, 0, depth + 1);
2819 sgmlCloseTag(os, depth + 1, tag);
2825 void Buffer::DocBookHandleCaption(ostream & os, string & inner_tag,
2826 int const depth, int desc_on,
2827 LyXParagraph * & par)
2829 LyXParagraph * tpar = par;
2830 while (tpar && (tpar->footnoteflag != LyXParagraph::NO_FOOTNOTE) &&
2831 (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2835 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2836 "Caption").second) {
2837 sgmlOpenTag(os, depth + 1, inner_tag);
2838 #ifdef USE_OSTREAM_ONLY
2840 SimpleDocBookOnePar(os, extra_par, tpar,
2841 desc_on, depth + 2);
2843 string tmp_par, extra_par;
2844 SimpleDocBookOnePar(tmp_par, extra_par, tpar,
2845 desc_on, depth + 2);
2846 tmp_par = strip(tmp_par);
2847 tmp_par = frontStrip(tmp_par);
2850 sgmlCloseTag(os, depth+1, inner_tag);
2851 if(!extra_par.empty())
2857 void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
2860 string tag, inner_tag;
2861 string tmp_par, extra_par;
2862 bool inner_span = false;
2865 // This is not how I like to see enums. They should not be anonymous
2866 // and variables of its type should not be declared right after the
2867 // last brace. (Lgb)
2875 SOME_ENUM last = NO_ONE;
2876 SOME_ENUM present = FOOTNOTE_LIKE;
2878 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2879 if(last == present) {
2881 if(!tmp_par.empty()) {
2884 sgmlCloseTag(os, depth+1, inner_tag);
2885 sgmlOpenTag(os, depth+1, inner_tag);
2892 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1,
2894 if(!extra_par.empty()) os << extra_par;
2895 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2898 switch (par->footnotekind) {
2899 case LyXParagraph::FOOTNOTE:
2900 case LyXParagraph::ALGORITHM:
2903 present = FOOTNOTE_LIKE;
2906 case LyXParagraph::MARGIN:
2909 present = MARGIN_LIKE;
2912 case LyXParagraph::FIG:
2913 case LyXParagraph::WIDE_FIG:
2915 inner_tag = "title";
2919 case LyXParagraph::TAB:
2920 case LyXParagraph::WIDE_TAB:
2922 inner_tag = "title";
2927 sgmlOpenTag(os, depth, tag);
2928 if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
2929 DocBookHandleCaption(os, inner_tag, depth,
2933 sgmlOpenTag(os, depth + 1, inner_tag);
2936 // ignore all caption here, we processed them above!!!
2937 if (par->layout != textclasslist.NumberOfLayout(params.textclass,
2938 "Caption").second) {
2939 #ifdef USE_OSTREAM_ONLY
2945 SimpleDocBookOnePar(ost, extra_par, par,
2946 desc_on, depth + 2);
2948 tmp_par += ost.str().c_str();
2951 char * ctmp = ost.str();
2958 tmp_par = frontStrip(strip(tmp_par));
2964 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1, inner_tag);
2965 if(!extra_par.empty()) os << extra_par;
2966 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2970 /* push a tag in a style stack */
2971 void Buffer::push_tag(ostream & os, char const * tag,
2972 int & pos, char stack[5][3])
2974 /* pop all previous tags */
2975 for (int j = pos; j >= 0; --j)
2976 os << "</" << stack[j] << ">";
2979 sprintf(stack[++pos], "%s", tag);
2982 for (int i = 0; i <= pos; ++i)
2983 os << "<" << stack[i] << ">";
2986 void Buffer::pop_tag(ostream & os, char const * tag,
2987 int & pos, char stack[5][3])
2991 // pop all tags till specified one
2992 for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
2993 os << "</" << stack[j] << ">";
2996 os << "</" << tag << ">";
2998 // push all tags, but the specified one
2999 for (j = j + 1; j <= pos; ++j) {
3000 os << "<" << stack[j] << ">";
3001 strcpy(stack[j-1], stack[j]);
3007 /* handle internal paragraph parsing -- layout already processed */
3009 // checks, if newcol chars should be put into this line
3010 // writes newline, if necessary.
3012 void linux_doc_line_break(ostream & os, unsigned int & colcount,
3013 const unsigned int newcol)
3016 if (colcount > lyxrc->ascii_linelen) {
3018 colcount = newcol; // assume write after this call
3023 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
3024 int desc_on, int const /*depth*/)
3026 LyXFont font1, font2;
3029 LyXParagraph::size_type main_body;
3031 LyXLayout const & style = textclasslist.Style(params.textclass,
3034 char family_type = 0; // family font flag
3035 bool is_bold = false; // series font flag
3036 char shape_type = 0; // shape font flag
3037 bool is_em = false; // emphasis (italic) font flag
3039 int stack_num = -1; // style stack position
3040 char stack[5][3]; // style stack
3041 unsigned int char_line_count = 5; // Heuristic choice ;-)
3043 if (style.labeltype != LABEL_MANUAL)
3046 main_body = par->BeginningOfMainBody();
3048 /* gets paragraph main font */
3050 font1 = style.labelfont;
3055 /* parsing main loop */
3056 for (LyXParagraph::size_type i = 0;
3057 i < par->size(); ++i) {
3059 /* handle quote tag */
3060 if (i == main_body && !par->IsDummy()) {
3065 font2 = par->getFont(i);
3067 if (font1.family() != font2.family()) {
3068 switch(family_type) {
3070 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3071 push_tag(os, "tt", stack_num, stack);
3074 else if (font2.family() == LyXFont::SANS_FAMILY) {
3075 push_tag(os, "sf", stack_num, stack);
3080 pop_tag(os, "tt", stack_num, stack);
3081 if (font2.family() == LyXFont::SANS_FAMILY) {
3082 push_tag(os, "sf", stack_num, stack);
3090 pop_tag(os, "sf", stack_num, stack);
3091 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3092 push_tag(os, "tt", stack_num, stack);
3101 /* handle bold face */
3102 if (font1.series() != font2.series()) {
3103 if (font2.series() == LyXFont::BOLD_SERIES) {
3104 push_tag(os, "bf", stack_num, stack);
3108 pop_tag(os, "bf", stack_num, stack);
3113 /* handle italic and slanted fonts */
3114 if (font1.shape() != font2.shape()) {
3115 switch(shape_type) {
3117 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3118 push_tag(os, "it", stack_num, stack);
3121 else if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3122 push_tag(os, "sl", stack_num, stack);
3127 pop_tag(os, "it", stack_num, stack);
3128 if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3129 push_tag(os, "sl", stack_num, stack);
3137 pop_tag(os, "sl", stack_num, stack);
3138 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3139 push_tag(os, "it", stack_num, stack);
3147 /* handle <em> tag */
3148 if (font1.emph() != font2.emph()) {
3149 if (font2.emph() == LyXFont::ON) {
3150 push_tag(os, "em", stack_num, stack);
3153 pop_tag(os, "em", stack_num, stack);
3158 c = par->GetChar(i);
3160 if (font2.latex() == LyXFont::ON) {
3161 // "TeX"-Mode on == > SGML-Mode on.
3163 os << c; // see LaTeX-Generation...
3165 } else if (c == LyXParagraph::META_INSET) {
3166 inset = par->GetInset(i);
3167 #ifdef USE_OSTREAM_ONLY
3168 inset->Linuxdoc(os);
3171 inset->Linuxdoc(tmp_out);
3177 if (par->linuxDocConvertChar(c, sgml_string)
3178 && !style.free_spacing) { // in freespacing
3180 // non-breaking characters
3184 linux_doc_line_break(os, char_line_count, 6);
3189 linux_doc_line_break(os, char_line_count, 1);
3195 char_line_count += sgml_string.length();
3201 /* needed if there is an optional argument but no contents */
3202 if (main_body > 0 && main_body == par->size()) {
3206 /* pop all defined Styles */
3207 for (j = stack_num; j >= 0; --j) {
3208 linux_doc_line_break(os,
3210 3 + strlen(stack[j]));
3211 os << "</" << stack[j] << ">";
3214 /* resets description flag correctly */
3217 /* <tag> not closed... */
3218 linux_doc_line_break(os, char_line_count, 6);
3222 /* fprintf(file, "</p>");*/
3228 /* print an error message */
3229 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
3230 char const * message)
3232 InsetError * new_inset;
3234 /* insert an error marker in text */
3235 new_inset = new InsetError(message);
3236 par->InsertChar(pos, LyXParagraph::META_INSET);
3237 par->InsertInset(pos, new_inset);
3240 // This constant defines the maximum number of
3241 // environment layouts that can be nesteded.
3242 // The same applies for command layouts.
3243 // These values should be more than enough.
3244 // José Matos (1999/07/22)
3246 enum { MAX_NEST_LEVEL = 25};
3248 void Buffer::makeDocBookFile(string const & fname, int column)
3250 LyXParagraph * par = paragraph;
3252 string top_element= textclasslist.LatexnameOfClass(params.textclass);
3253 string environment_stack[MAX_NEST_LEVEL];
3254 string environment_inner[MAX_NEST_LEVEL];
3255 string command_stack[MAX_NEST_LEVEL];
3256 bool command_flag= false;
3257 int command_depth= 0, command_base= 0, cmd_depth= 0;
3259 string item_name, command_name;
3260 string c_depth, c_params, tmps;
3262 int depth= 0; /* paragraph depth */
3264 tex_code_break_column = column;
3266 ofstream ofs(fname.c_str());
3268 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3274 ofs << "<!doctype " << top_element
3275 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
3277 if (params.preamble.empty())
3280 ofs << "\n [ " << params.preamble << " \n]>\n\n";
3282 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
3283 << "\n See http://www.lyx.org/ for more information -->\n";
3285 if(params.options.empty())
3286 sgmlOpenTag(ofs, 0, top_element);
3288 string top = top_element;
3290 top += params.options;
3291 sgmlOpenTag(ofs, 0, top);
3295 int desc_on = 0; /* description mode*/
3296 LyXLayout const & style =
3297 textclasslist.Style(users->buffer()->params.textclass,
3299 par->AutoDeleteInsets();
3301 /* environment tag closing */
3302 for( ; depth > par->depth; --depth) {
3303 if(environment_inner[depth] != "!-- --") {
3304 item_name= "listitem";
3305 sgmlCloseTag(ofs, command_depth + depth,
3307 if( environment_inner[depth] == "varlistentry")
3308 sgmlCloseTag(ofs, depth+command_depth,
3309 environment_inner[depth]);
3311 sgmlCloseTag(ofs, depth + command_depth,
3312 environment_stack[depth]);
3313 environment_stack[depth].clear();
3314 environment_inner[depth].clear();
3317 if(depth == par->depth
3318 && environment_stack[depth] != style.latexname()
3319 && !environment_stack[depth].empty()) {
3320 if(environment_inner[depth] != "!-- --") {
3321 item_name= "listitem";
3322 sgmlCloseTag(ofs, command_depth+depth,
3324 if( environment_inner[depth] == "varlistentry")
3326 depth + command_depth,
3327 environment_inner[depth]);
3330 sgmlCloseTag(ofs, depth + command_depth,
3331 environment_stack[depth]);
3333 environment_stack[depth].clear();
3334 environment_inner[depth].clear();
3337 // Write opening SGML tags.
3338 switch(style.latextype) {
3339 case LATEX_PARAGRAPH:
3340 if(style.latexname() != "dummy")
3341 sgmlOpenTag(ofs, depth+command_depth,
3347 LinuxDocError(par, 0,
3348 _("Error : Wrong depth for "
3349 "LatexType Command.\n"));
3351 command_name = style.latexname();
3353 tmps = style.latexparam();
3354 c_params = split(tmps, c_depth,'|');
3356 cmd_depth= atoi(c_depth.c_str());
3359 if(cmd_depth<command_base) {
3360 for(int j = command_depth;
3361 j >= command_base; --j)
3362 if(!command_stack[j].empty())
3363 sgmlCloseTag(ofs, j, command_stack[j]);
3364 command_depth= command_base= cmd_depth;
3366 else if(cmd_depth <= command_depth) {
3367 for(int j = command_depth;
3368 j >= cmd_depth; --j)
3370 if(!command_stack[j].empty())
3371 sgmlCloseTag(ofs, j, command_stack[j]);
3372 command_depth= cmd_depth;
3375 command_depth= cmd_depth;
3378 command_depth = command_base = cmd_depth;
3379 command_flag = true;
3381 command_stack[command_depth]= command_name;
3383 // treat label as a special case for
3384 // more WYSIWYM handling.
3385 if (par->GetChar(0) == LyXParagraph::META_INSET) {
3386 Inset * inset = par->GetInset(0);
3387 char lyx_code = inset->LyxCode();
3388 if (lyx_code == Inset::LABEL_CODE){
3389 command_name += " id=\"";
3390 command_name += (static_cast<InsetCommand *>(inset))->getContents();
3391 command_name += "\"";
3396 sgmlOpenTag(ofs, depth+command_depth, command_name);
3397 item_name = "title";
3398 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3401 case LATEX_ENVIRONMENT:
3402 case LATEX_ITEM_ENVIRONMENT:
3403 if (depth < par->depth) {
3405 environment_stack[depth].clear();
3408 if (environment_stack[depth] != style.latexname()) {
3409 environment_stack[depth] = style.latexname();
3410 environment_inner[depth] = "!-- --";
3411 sgmlOpenTag(ofs, depth + command_depth,
3412 environment_stack[depth]);
3414 if(environment_inner[depth] != "!-- --") {
3415 item_name= "listitem";
3417 command_depth + depth,
3419 if (environment_inner[depth] == "varlistentry")
3421 depth + command_depth,
3422 environment_inner[depth]);
3426 if(style.latextype == LATEX_ENVIRONMENT) {
3427 if(!style.latexparam().empty())
3428 sgmlOpenTag(ofs, depth + command_depth,
3429 style.latexparam());
3433 desc_on = (style.labeltype == LABEL_MANUAL);
3436 environment_inner[depth]= "varlistentry";
3438 environment_inner[depth]= "listitem";
3440 sgmlOpenTag(ofs, depth + 1 + command_depth,
3441 environment_inner[depth]);
3445 sgmlOpenTag(ofs, depth + 1 + command_depth,
3450 sgmlOpenTag(ofs, depth + 1 + command_depth,
3455 sgmlOpenTag(ofs, depth + command_depth,
3461 #ifdef USE_OSTREAM_ONLY
3463 SimpleDocBookOnePar(ofs, extra_par, par, desc_on,
3464 depth + 1 + command_depth);
3466 DocBookHandleFootnote(ofs, par,
3467 depth + 1 + command_depth);
3469 string tmp_par, extra_par;
3471 SimpleDocBookOnePar(tmp_par, extra_par, par, desc_on,
3472 depth + 1 + command_depth);
3476 DocBookHandleFootnote(ofs, par,
3477 depth + 1 + command_depth);
3480 while(par && par->IsDummy());
3483 /* write closing SGML tags */
3484 switch(style.latextype) {
3487 sgmlCloseTag(ofs, depth + command_depth, end_tag);
3489 case LATEX_ENVIRONMENT:
3490 if(!style.latexparam().empty())
3491 sgmlCloseTag(ofs, depth + command_depth,
3492 style.latexparam());
3494 case LATEX_ITEM_ENVIRONMENT:
3495 if(desc_on == 1) break;
3497 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3499 case LATEX_PARAGRAPH:
3500 if(style.latexname() != "dummy")
3501 sgmlCloseTag(ofs, depth + command_depth,
3505 sgmlCloseTag(ofs, depth + command_depth,
3512 for(; depth >= 0; --depth) {
3513 if(!environment_stack[depth].empty()) {
3514 if(environment_inner[depth] != "!-- --") {
3515 item_name= "listitem";
3516 sgmlCloseTag(ofs, command_depth + depth,
3518 if( environment_inner[depth] == "varlistentry")
3519 sgmlCloseTag(ofs, depth + command_depth,
3520 environment_inner[depth]);
3523 sgmlCloseTag(ofs, depth + command_depth,
3524 environment_stack[depth]);
3528 for(int j = command_depth; j >= command_base; --j)
3529 if(!command_stack[j].empty())
3530 sgmlCloseTag(ofs, j, command_stack[j]);
3533 sgmlCloseTag(ofs, 0, top_element);
3536 // How to check for successful close
3540 #ifdef USE_OSTREAM_ONLY
3541 void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
3542 LyXParagraph * par, int & desc_on,
3546 par->SimpleDocBookOneTablePar(os, extra, desc_on, depth);
3549 LyXFont font1, font2;
3552 LyXParagraph::size_type main_body;
3554 //string emph = "emphasis";
3555 bool emph_flag = false;
3556 int char_line_count = 0;
3558 LyXLayout const & style = textclasslist.Style(params.textclass,
3561 if (style.labeltype != LABEL_MANUAL)
3564 main_body = par->BeginningOfMainBody();
3566 // gets paragraph main font
3568 font1 = style.labelfont;
3572 char_line_count = depth;
3573 if(!style.free_spacing)
3574 for (j = 0; j < depth; ++j)
3577 // parsing main loop
3578 for (LyXParagraph::size_type i = 0;
3579 i < par->size(); ++i) {
3580 font2 = par->getFont(i);
3582 // handle <emphasis> tag
3583 if (font1.emph() != font2.emph() && i) {
3584 if (font2.emph() == LyXFont::ON) {
3588 os << "</emphasis>";
3593 c = par->GetChar(i);
3595 if (c == LyXParagraph::META_INSET) {
3596 inset = par->GetInset(i);
3599 inset->DocBook(ost);
3600 string tmp_out = ost.str().c_str();
3603 inset->DocBook(ost);
3605 char * ctmp = ost.str();
3606 string tmp_out(ctmp);
3610 // This code needs some explanation:
3611 // Two insets are treated specially
3612 // label if it is the first element in a command paragraph
3614 // graphics inside tables or figure floats can't go on
3615 // title (the equivalente in latex for this case is caption
3616 // and title should come first
3619 if(desc_on!= 3 || i!= 0) {
3620 if(!tmp_out.empty() && tmp_out[0] == '@') {
3622 extra += frontStrip(tmp_out, '@');
3624 os << frontStrip(tmp_out, '@');
3629 } else if (font2.latex() == LyXFont::ON) {
3630 // "TeX"-Mode on ==> SGML-Mode on.
3637 if (par->linuxDocConvertChar(c, sgml_string)
3638 && !style.free_spacing) { // in freespacing
3640 // non-breaking characters
3644 os << "\n</term><listitem><para>";
3658 /* needed if there is an optional argument but no contents */
3659 if (main_body > 0 && main_body == par->size()) {
3663 os << "</emphasis>";
3666 /* resets description flag correctly */
3669 /* <term> not closed... */
3676 void Buffer::SimpleDocBookOnePar(string & file, string & extra,
3677 LyXParagraph * par, int & desc_on,
3681 par->SimpleDocBookOneTablePar(file, extra, desc_on, depth);
3684 LyXFont font1, font2;
3687 LyXParagraph::size_type main_body;
3689 string emph= "emphasis";
3690 bool emph_flag= false;
3691 int char_line_count= 0;
3693 LyXLayout const & style = textclasslist.Style(params.textclass,
3696 if (style.labeltype != LABEL_MANUAL)
3699 main_body = par->BeginningOfMainBody();
3701 /* gets paragraph main font */
3703 font1 = style.labelfont;
3707 char_line_count = depth;
3708 if(!style.free_spacing)
3709 for (j = 0; j < depth; ++j)
3712 /* parsing main loop */
3713 for (LyXParagraph::size_type i = 0;
3714 i < par->size(); ++i) {
3715 font2 = par->getFont(i);
3717 /* handle <emphasis> tag */
3718 if (font1.emph() != font2.emph() && i) {
3719 if (font2.emph() == LyXFont::ON) {
3720 file += "<emphasis>";
3723 file += "</emphasis>";
3728 c = par->GetChar(i);
3730 if (c == LyXParagraph::META_INSET) {
3731 inset = par->GetInset(i);
3732 #ifdef USE_OSTREAM_ONLY
3735 inset->DocBook(ost);
3736 string tmp_out = ost.str().c_str();
3739 inset->DocBook(ost);
3741 char * ctmp = ost.str();
3742 string tmp_out(ctmp);
3747 inset->DocBook(tmp_out);
3750 // This code needs some explanation:
3751 // Two insets are treated specially
3752 // label if it is the first element in a command paragraph
3754 // graphics inside tables or figure floats can't go on
3755 // title (the equivalente in latex for this case is caption
3756 // and title should come first
3759 if(desc_on!= 3 || i!= 0) {
3760 if(!tmp_out.empty() && tmp_out[0] == '@') {
3762 extra += frontStrip(tmp_out, '@');
3764 file += frontStrip(tmp_out, '@');
3769 } else if (font2.latex() == LyXFont::ON) {
3770 // "TeX"-Mode on ==> SGML-Mode on.
3777 if (par->linuxDocConvertChar(c, sgml_string)
3778 && !style.free_spacing) { // in freespacing
3780 // non-breaking characters
3785 file += "</term><listitem><para>";
3793 file += sgml_string;
3799 /* needed if there is an optional argument but no contents */
3800 if (main_body > 0 && main_body == par->size()) {
3804 file += "</emphasis>";
3807 /* resets description flag correctly */
3810 /* <term> not closed... */
3819 int Buffer::runLaTeX()
3821 if (!users->text) return 0;
3825 // get LaTeX-Filename
3826 string name = getLatexName();
3828 string path = OnlyPath(filename);
3830 string org_path = path;
3831 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3835 Path p(path); // path to LaTeX file
3836 users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
3838 // Remove all error insets
3839 bool a = users->removeAutoInsets();
3841 // Always generate the LaTeX file
3842 makeLaTeXFile(name, org_path, false);
3845 // do the LaTex run(s)
3847 string latex_command = lyxrc->pdf_mode ?
3848 lyxrc->pdflatex_command : lyxrc->latex_command;
3849 LaTeX latex(latex_command, name, filepath);
3850 int res = latex.run(terr,
3851 users->owner()->getMiniBuffer()); // running latex
3853 // check return value from latex.run().
3854 if ((res & LaTeX::NO_LOGFILE)) {
3855 WriteAlert(_("LaTeX did not work!"),
3856 _("Missing log file:"), name);
3857 } else if ((res & LaTeX::ERRORS)) {
3858 users->owner()->getMiniBuffer()->Set(_("Done"));
3859 // Insert all errors as errors boxes
3860 users->insertErrors(terr);
3862 // Dvi should also be kept dirty if the latex run
3863 // ends up with errors. However it should be possible
3864 // to view a dirty dvi too.
3866 //no errors or any other things to think about so:
3867 users->owner()->getMiniBuffer()->Set(_("Done"));
3871 // if we removed error insets before we ran LaTeX or if we inserted
3872 // error insets after we ran LaTeX this must be run:
3873 if (a || (res & LaTeX::ERRORS)){
3876 users->updateScrollbar();
3880 return latex.getNumErrors();
3884 int Buffer::runLiterate()
3886 if (!users->text) return 0;
3890 // get LaTeX-Filename
3891 string name = getLatexName();
3892 // get Literate-Filename
3893 string lit_name = ChangeExtension (getLatexName(),
3894 lyxrc->literate_extension, true);
3896 string path = OnlyPath(filename);
3898 string org_path = path;
3899 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3903 Path p(path); // path to Literate file
3904 users->owner()->getMiniBuffer()->Set(_("Running Literate..."));
3906 // Remove all error insets
3907 bool a = users->removeAutoInsets();
3909 // generate the Literate file if necessary
3910 if (!isDviClean() || a) {
3911 makeLaTeXFile(lit_name, org_path, false);
3915 string latex_command = lyxrc->pdf_mode ?
3916 lyxrc->pdflatex_command : lyxrc->latex_command;
3917 Literate literate(latex_command, name, filepath,
3919 lyxrc->literate_command, lyxrc->literate_error_filter,
3920 lyxrc->build_command, lyxrc->build_error_filter);
3922 int res = literate.weave(terr, users->owner()->getMiniBuffer());
3924 // check return value from literate.weave().
3925 if ((res & Literate::NO_LOGFILE)) {
3926 WriteAlert(_("Literate command did not work!"),
3927 _("Missing log file:"), name);
3928 } else if ((res & Literate::ERRORS)) {
3929 users->owner()->getMiniBuffer()->Set(_("Done"));
3930 // Insert all errors as errors boxes
3931 users->insertErrors(terr);
3933 // Dvi should also be kept dirty if the latex run
3934 // ends up with errors. However it should be possible
3935 // to view a dirty dvi too.
3937 //no errors or any other things to think about so:
3938 users->owner()->getMiniBuffer()->Set(_("Done"));
3942 // if we removed error insets before we ran LaTeX or if we inserted
3943 // error insets after we ran LaTeX this must be run:
3944 if (a || (res & Literate::ERRORS)){
3947 users->updateScrollbar();
3951 return literate.getNumErrors();
3955 int Buffer::buildProgram()
3957 if (!users->text) return 0;
3961 // get LaTeX-Filename
3962 string name = getLatexName();
3963 // get Literate-Filename
3964 string lit_name = ChangeExtension(getLatexName(),
3965 lyxrc->literate_extension, true);
3967 string path = OnlyPath(filename);
3969 string org_path = path;
3970 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3974 Path p(path); // path to Literate file
3975 users->owner()->getMiniBuffer()->Set(_("Building Program..."));
3977 // Remove all error insets
3978 bool a = users->removeAutoInsets();
3980 // generate the LaTeX file if necessary
3981 if (!isNwClean() || a) {
3982 makeLaTeXFile(lit_name, org_path, false);
3986 string latex_command = lyxrc->pdf_mode ?
3987 lyxrc->pdflatex_command : lyxrc->latex_command;
3988 Literate literate(latex_command, name, filepath,
3990 lyxrc->literate_command, lyxrc->literate_error_filter,
3991 lyxrc->build_command, lyxrc->build_error_filter);
3993 int res = literate.build(terr, users->owner()->getMiniBuffer());
3995 // check return value from literate.build().
3996 if ((res & Literate::NO_LOGFILE)) {
3997 WriteAlert(_("Build did not work!"),
3998 _("Missing log file:"), name);
3999 } else if ((res & Literate::ERRORS)) {
4000 users->owner()->getMiniBuffer()->Set(_("Done"));
4001 // Insert all errors as errors boxes
4002 users->insertErrors(terr);
4004 // Literate files should also be kept dirty if the literate
4005 // command run ends up with errors.
4007 //no errors or any other things to think about so:
4008 users->owner()->getMiniBuffer()->Set(_("Done"));
4012 // if we removed error insets before we ran Literate/Build or if we inserted
4013 // error insets after we ran Literate/Build this must be run:
4014 if (a || (res & Literate::ERRORS)){
4017 users->updateScrollbar();
4021 return literate.getNumErrors();
4025 // This should be enabled when the Chktex class is implemented. (Asger)
4026 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
4027 // Other flags: -wall -v0 -x
4028 int Buffer::runChktex()
4030 if (!users->text) return 0;
4034 // get LaTeX-Filename
4035 string name = getLatexName();
4036 string path = OnlyPath(filename);
4038 string org_path = path;
4039 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
4043 Path p(path); // path to LaTeX file
4044 users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
4046 // Remove all error insets
4047 bool a = users->removeAutoInsets();
4049 // Generate the LaTeX file if neccessary
4050 if (!isDviClean() || a) {
4051 makeLaTeXFile(name, org_path, false);
4056 Chktex chktex(lyxrc->chktex_command, name, filepath);
4057 int res = chktex.run(terr); // run chktex
4060 WriteAlert(_("chktex did not work!"),
4061 _("Could not run with file:"), name);
4062 } else if (res > 0) {
4063 // Insert all errors as errors boxes
4064 users->insertErrors(terr);
4067 // if we removed error insets before we ran chktex or if we inserted
4068 // error insets after we ran chktex, this must be run:
4072 users->updateScrollbar();
4081 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
4083 LyXFont font1(LyXFont::ALL_INHERIT);
4086 LyXParagraph::size_type i;
4090 string fname1 = TmpFileName(string(), "RAT1");
4091 string fname2 = TmpFileName(string(), "RAT2");
4093 ofstream ofs(fname1.c_str());
4095 WriteAlert(_("LYX_ERROR:"),
4096 _("Cannot open temporary file:"), fname1);
4099 par->table->RoffEndOfCell(ofs, -1);
4100 for (i = 0; i < par->size(); ++i) {
4101 c = par->GetChar(i);
4102 if (par->table->IsContRow(cell)) {
4103 if (c == LyXParagraph::META_NEWLINE)
4107 font2 = par->GetFontSettings(i);
4108 if (font1.latex() != font2.latex()) {
4109 if (font2.latex() != LyXFont::OFF)
4113 case LyXParagraph::META_INSET:
4114 if ((inset = par->GetInset(i))) {
4117 stringstresm ss(ios::in | ios::out);
4118 inset->Latex(ss, -1);
4130 inset->Latex(ss, -1);
4143 fstream fs(fname2.c_str(), ios::in|ios::out);
4145 WriteAlert(_("LYX_ERROR:"),
4146 _("Cannot open temporary file:"), fname2);
4148 remove(fname1.c_str());
4151 inset->Latex(fs, -1);
4165 case LyXParagraph::META_NEWLINE:
4166 if (par->table->CellHasContRow(cell)>= 0)
4167 par->RoffContTableRows(ofs, i+1, cell);
4168 par->table->RoffEndOfCell(ofs, cell);
4171 case LyXParagraph::META_HFILL:
4174 case LyXParagraph::META_PROTECTED_SEPARATOR:
4185 << "RoffAsciiTable:"
4186 " NULL char in structure." << endl;
4190 par->table->RoffEndOfCell(ofs, cell);
4192 string cmd = lyxrc->ascii_roff_command + " >" + fname2;
4193 cmd = subst(cmd, "$$FName", fname1);
4194 Systemcalls one(Systemcalls::System, cmd);
4195 if (!(lyxerr.debugging(Debug::ROFF))) {
4196 remove(fname1.c_str());
4198 ifstream ifs(fname2.c_str());
4200 WriteFSAlert(_("Error! Can't open temporary file:"), fname2);
4203 // now output the produced file
4207 WriteAlert(_("Error!"),
4208 _("Error executing *roff command on table"));
4209 // overread leading blank lines
4210 while(!ifs && (c == '\n'))
4213 for(j = 0; j < par->depth; ++j)
4215 while(!ifs && (c != '\n')) {
4220 // overread trailing blank lines
4221 while(!ifs && (c == '\n'))
4225 remove(fname2.c_str());
4230 /// changed Heinrich Bauer, 23/03/98
4231 bool Buffer::isDviClean() const
4233 if (lyxrc->use_tempdir)
4234 return dvi_clean_tmpd;
4236 return dvi_clean_orgd;
4240 /// changed Heinrich Bauer, 23/03/98
4241 void Buffer::markDviClean()
4243 if (lyxrc->use_tempdir)
4244 dvi_clean_tmpd = true;
4246 dvi_clean_orgd = true;
4250 /// changed Heinrich Bauer, 23/03/98
4251 void Buffer::markDviDirty()
4253 if (lyxrc->use_tempdir)
4254 dvi_clean_tmpd = false;
4256 dvi_clean_orgd = false;
4260 void Buffer::validate(LaTeXFeatures & features) const
4262 LyXParagraph * par = paragraph;
4263 LyXTextClass const & tclass =
4264 textclasslist.TextClass(params.textclass);
4266 // AMS Style is at document level
4268 features.amsstyle = (params.use_amsmath ||
4269 tclass.provides(LyXTextClass::amsmath));
4272 // We don't use "lyxerr.debug" because of speed. (Asger)
4273 if (lyxerr.debugging(Debug::LATEX))
4274 lyxerr << "Paragraph: " << par << endl;
4276 // Now just follow the list of paragraphs and run
4277 // validate on each of them.
4278 par->validate(features);
4280 // and then the next paragraph
4284 // the bullet shapes are buffer level not paragraph level
4285 // so they are tested here
4286 for (int i = 0; i < 4; ++i) {
4287 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
4288 int font = params.user_defined_bullets[i].getFont();
4291 .user_defined_bullets[i]
4298 features.latexsym = true;
4302 features.amssymb = true;
4304 else if ((font >= 2 && font <= 5)) {
4305 features.pifont = true;
4310 if (lyxerr.debugging(Debug::LATEX)) {
4311 features.showStruct(params);
4316 void Buffer::setPaperStuff()
4318 params.papersize = BufferParams::PAPER_DEFAULT;
4319 char c1 = params.paperpackage;
4320 if (c1 == BufferParams::PACKAGE_NONE) {
4321 char c2 = params.papersize2;
4322 if (c2 == BufferParams::VM_PAPER_USLETTER)
4323 params.papersize = BufferParams::PAPER_USLETTER;
4324 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
4325 params.papersize = BufferParams::PAPER_LEGALPAPER;
4326 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
4327 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
4328 else if (c2 == BufferParams::VM_PAPER_A3)
4329 params.papersize = BufferParams::PAPER_A3PAPER;
4330 else if (c2 == BufferParams::VM_PAPER_A4)
4331 params.papersize = BufferParams::PAPER_A4PAPER;
4332 else if (c2 == BufferParams::VM_PAPER_A5)
4333 params.papersize = BufferParams::PAPER_A5PAPER;
4334 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
4335 (c2 == BufferParams::VM_PAPER_B5))
4336 params.papersize = BufferParams::PAPER_B5PAPER;
4337 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
4338 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
4339 params.papersize = BufferParams::PAPER_A4PAPER;
4343 // This function should be in Buffer because it's a buffer's property (ale)
4344 string Buffer::getIncludeonlyList(char delim)
4347 LyXParagraph * par = paragraph;
4348 LyXParagraph::size_type pos;
4352 while ((inset = par->ReturnNextInsetPointer(pos))){
4353 if (inset->LyxCode() == Inset::INCLUDE_CODE) {
4354 InsetInclude * insetinc =
4355 static_cast<InsetInclude*>(inset);
4356 if (insetinc->isInclude()
4357 && insetinc->isNoLoad()) {
4360 lst += ChangeExtension(insetinc->getContents(), string(), true);
4367 lyxerr.debug() << "Includeonly(" << lst << ')' << endl;
4372 // This is also a buffer property (ale)
4373 string Buffer::getReferenceList(char delim)
4375 /// if this is a child document and the parent is already loaded
4376 /// Use the parent's list instead [ale990407]
4377 if (!params.parentname.empty()
4378 && bufferlist.exists(params.parentname)) {
4379 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4381 return tmp->getReferenceList(delim);
4384 LyXParagraph * par = paragraph;
4385 LyXParagraph::size_type pos;
4390 while ((inset = par->ReturnNextInsetPointer(pos))){
4391 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
4394 lst += inset->getLabel(i);
4400 lyxerr.debug() << "References(" << lst << ")" << endl;
4405 // This is also a buffer property (ale)
4406 string Buffer::getBibkeyList(char delim)
4408 /// if this is a child document and the parent is already loaded
4409 /// Use the parent's list instead [ale990412]
4410 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
4411 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4413 return tmp->getBibkeyList(delim);
4417 LyXParagraph * par = paragraph;
4420 if (!bibkeys.empty())
4422 bibkeys += par->bibkey->getContents();
4427 // Might be either using bibtex or a child has bibliography
4428 if (bibkeys.empty()) {
4432 LyXParagraph::size_type pos = -1;
4434 // Search for Bibtex or Include inset
4435 while ((inset = par->ReturnNextInsetPointer(pos))) {
4436 if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
4437 if (!bibkeys.empty())
4439 bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
4440 } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
4441 string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
4443 if (!bibkeys.empty())
4454 lyxerr.debug() << "Bibkeys(" << bibkeys << ")" << endl;
4459 bool Buffer::isDepClean(string const & name) const
4461 DEPCLEAN * item = dep_clean;
4462 while (item && item->master != name)
4464 if (!item) return true;
4469 void Buffer::markDepClean(string const & name)
4472 dep_clean = new DEPCLEAN;
4473 dep_clean->clean = true;
4474 dep_clean->master = name;
4475 dep_clean->next = 0;
4477 DEPCLEAN* item = dep_clean;
4478 while (item && item->master != name)
4483 item = new DEPCLEAN;
4485 item->master = name;
4492 void Buffer::Dispatch(string const & command)
4494 // Split command string into command and argument
4495 string cmd, line = frontStrip(command);
4496 string arg = strip(frontStrip(split(line, cmd, ' ')));
4498 Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
4502 void Buffer::Dispatch(int action, string const & argument)
4506 MenuExport(this, argument);
4510 lyxerr << "A truly unknown func!" << endl;