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 "support/filetools.h"
80 #include "support/path.h"
86 #include "LaTeXFeatures.h"
87 #include "support/syscall.h"
88 #include "support/lyxlib.h"
89 #include "support/FileInfo.h"
93 // Uncomment this line to enable a workaround for the weird behaviour
94 // of the cursor between a displayed inset and last character
95 // at the upper line. (Alejandro 20.9.96)
96 // #define BREAK_BEFORE_INSET
98 /* Lars, when changing this file sometime in the future to a list,
99 * please check out the figinsets-sideeffects described at the
100 * beginning of figinset.C Matthias (04.07.1996)
104 // all these externs should eventually be removed.
105 extern BufferList bufferlist;
107 extern void MenuExport(Buffer *, string const &);
108 extern LyXAction lyxaction;
111 static const float LYX_FORMAT = 2.15;
113 extern int tex_code_break_column;
115 extern void FreeUpdateTimer();
118 Buffer::Buffer(string const & file, LyXRC * lyxrc, bool ronly)
120 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
122 filepath = OnlyPath(file);
126 dvi_clean_orgd = false; // Heinrich Bauer, 23/03/98
127 dvi_clean_tmpd = false; // Heinrich Bauer, 23/03/98
132 if (read_only || (lyxrc && lyxrc->use_tempdir)) {
133 tmppath = CreateBufferTmpDir();
134 } else tmppath.clear();
140 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
141 // here the buffer should take care that it is
142 // saved properly, before it goes into the void.
144 // make sure that views using this buffer
149 if (!tmppath.empty()) {
150 DestroyBufferTmpDir(tmppath);
153 LyXParagraph * par = paragraph;
154 LyXParagraph * tmppar;
164 bool Buffer::saveParamsAsDefaults()
166 string fname = AddName(AddPath(user_lyxdir, "templates/"),
168 Buffer defaults = Buffer(fname);
170 // Use the current buffer's parameters as default
171 defaults.params = params;
173 // add an empty paragraph. Is this enough?
174 defaults.paragraph = new LyXParagraph;
176 return defaults.writeFile(defaults.filename, false);
180 /// Update window titles of all users
181 // Should work on a list
182 void Buffer::updateTitles() const
184 if (users) users->owner()->updateWindowTitle();
188 /// Reset autosave timer of all users
189 // Should work on a list
190 void Buffer::resetAutosaveTimers() const
192 if (users) users->owner()->resetAutosaveTimer();
196 void Buffer::fileName(string const & newfile)
198 filename = MakeAbsPath(newfile);
199 filepath = OnlyPath(filename);
200 setReadonly(IsFileWriteable(filename) == 0);
205 // candidate for move to BufferView
206 // (at least some parts in the beginning of the func)
209 // changed to be public and have one parameter
210 // if par = 0 normal behavior
211 // else insert behavior
212 // Returns false if "\the_end" is not read for formats >= 2.13. (Asger)
213 #define USE_PARSE_FUNCTION 1
214 bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
218 char depth = 0; // signed or unsigned?
219 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
220 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
221 bool the_end_read = false;
223 LyXParagraph * return_par = 0;
224 LyXFont font(LyXFont::ALL_INHERIT);
226 // If we are inserting, we cheat and get a token in advance
227 bool has_token = false;
231 par = new LyXParagraph;
233 users->text->BreakParagraph();
234 return_par = users->text->FirstParagraph();
237 // We don't want to adopt the parameters from the
238 // document we insert, so we skip until the text begins:
241 pretoken = lex.GetString();
242 if (pretoken == "\\layout") {
254 pretoken = lex.GetString();
257 // Profiling show this should give a lot: (Asger)
258 string const token = pretoken;
262 the_end_read = parseSingleLyXformat2Token(lex, par, return_par,
271 paragraph = return_par;
277 bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
278 LyXParagraph *& return_par,
279 const string & token, int & pos,
280 char & depth, LyXFont & font,
281 LyXParagraph::footnote_flag & footnoteflag,
282 LyXParagraph::footnote_kind & footnotekind)
287 bool the_end_read = false;
289 if (token[0] != '\\') {
290 int n = token.length();
291 for (int i = 0; i < n; ++i) {
292 par->InsertChar(pos, token[i]);
293 par->SetFont(pos, font);
296 } else if (token == "\\i") {
297 inset = new InsetLatexAccent;
299 par->InsertChar(pos, LyXParagraph::META_INSET);
301 par->InsertInset(pos, inset);
302 par->SetFont(pos, font);
304 } else if (token == "\\layout") {
308 par->text.resize(par->text.size());
309 par = new LyXParagraph(par);
313 string layoutname = lex.GetString();
314 pair<bool, LyXTextClass::LayoutList::size_type> pp
315 = textclasslist.NumberOfLayout(params.textclass,
318 par->layout = pp.second;
319 } else { // layout not found
320 // use default layout "Standard" (0)
323 // Test whether the layout is obsolete.
324 LyXLayout const & layout =
325 textclasslist.Style(params.textclass,
327 if (!layout.obsoleted_by().empty())
329 textclasslist.NumberOfLayout(params.textclass,
330 layout.obsoleted_by()).second;
331 par->footnoteflag = footnoteflag;
332 par->footnotekind = footnotekind;
334 font = LyXFont(LyXFont::ALL_INHERIT);
335 } else if (token == "\\end_float") {
339 par->text.resize(par->text.size());
340 par = new LyXParagraph(par);
342 footnotekind = LyXParagraph::FOOTNOTE;
343 footnoteflag = LyXParagraph::NO_FOOTNOTE;
346 par->layout = LYX_DUMMY_LAYOUT;
347 font = LyXFont(LyXFont::ALL_INHERIT);
348 } else if (token == "\\begin_float") {
349 tmpret = lex.FindToken(string_footnotekinds);
350 if (tmpret == -1) ++tmpret;
351 if (tmpret != LYX_LAYOUT_DEFAULT)
352 footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
353 if (footnotekind == LyXParagraph::FOOTNOTE
354 || footnotekind == LyXParagraph::MARGIN)
355 footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
357 footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
358 } else if (token == "\\begin_deeper") {
360 } else if (token == "\\end_deeper") {
362 lex.printError("\\end_deeper: "
363 "depth is already null");
367 } else if (token == "\\begin_preamble") {
368 params.readPreamble(lex);
369 } else if (token == "\\textclass") {
371 pair<bool, LyXTextClassList::size_type> pp =
372 textclasslist.NumberOfClass(lex.GetString());
374 params.textclass = pp.second;
376 lex.printError("Unknown textclass `$$Token'");
377 params.textclass = 0;
379 if (!textclasslist.Load(params.textclass)) {
380 // if the textclass wasn't loaded properly
381 // we need to either substitute another
382 // or stop loading the file.
383 // I can substitute but I don't see how I can
384 // stop loading... ideas?? ARRae980418
385 WriteAlert(_("Textclass Loading Error!"),
386 string(_("Can't load textclass ")) +
387 textclasslist.NameOfClass(params.textclass),
388 _("-- substituting default"));
389 params.textclass = 0;
391 } else if (token == "\\options") {
393 params.options = lex.GetString();
394 } else if (token == "\\language") {
395 params.readLanguage(lex);
396 } else if (token == "\\fontencoding") {
398 } else if (token == "\\inputencoding") {
400 params.inputenc = lex.GetString();
401 } else if (token == "\\graphics") {
402 params.readGraphicsDriver(lex);
403 } else if (token == "\\fontscheme") {
405 params.fonts = lex.GetString();
406 } else if (token == "\\noindent") {
407 par->noindent = true;
408 } else if (token == "\\fill_top") {
409 par->added_space_top = VSpace(VSpace::VFILL);
410 } else if (token == "\\fill_bottom") {
411 par->added_space_bottom = VSpace(VSpace::VFILL);
412 } else if (token == "\\line_top") {
413 par->line_top = true;
414 } else if (token == "\\line_bottom") {
415 par->line_bottom = true;
416 } else if (token == "\\pagebreak_top") {
417 par->pagebreak_top = true;
418 } else if (token == "\\pagebreak_bottom") {
419 par->pagebreak_bottom = true;
420 } else if (token == "\\start_of_appendix") {
421 par->start_of_appendix = true;
422 } else if (token == "\\paragraph_separation") {
423 tmpret = lex.FindToken(string_paragraph_separation);
424 if (tmpret == -1) ++tmpret;
425 if (tmpret != LYX_LAYOUT_DEFAULT)
426 params.paragraph_separation =
427 static_cast<BufferParams::PARSEP>(tmpret);
428 } else if (token == "\\defskip") {
430 params.defskip = VSpace(lex.GetString());
431 } else if (token == "\\no_isolatin1") { // obsolete
433 } else if (token == "\\no_babel") { // obsolete
435 } else if (token == "\\no_epsfig") { // obsolete
437 } else if (token == "\\epsfig") { // obsolete
438 // Indeed it is obsolete, but we HAVE to be backwards
439 // compatible until 0.14, because otherwise all figures
440 // in existing documents are irretrivably lost. (Asger)
441 params.readGraphicsDriver(lex);
442 } else if (token == "\\quotes_language") {
443 tmpret = lex.FindToken(string_quotes_language);
444 if (tmpret == -1) ++tmpret;
445 if (tmpret != LYX_LAYOUT_DEFAULT) {
446 InsetQuotes::quote_language tmpl =
447 InsetQuotes::EnglishQ;
450 tmpl = InsetQuotes::EnglishQ;
453 tmpl = InsetQuotes::SwedishQ;
456 tmpl = InsetQuotes::GermanQ;
459 tmpl = InsetQuotes::PolishQ;
462 tmpl = InsetQuotes::FrenchQ;
465 tmpl = InsetQuotes::DanishQ;
468 params.quotes_language = tmpl;
470 } else if (token == "\\quotes_times") {
472 switch(lex.GetInteger()) {
474 params.quotes_times = InsetQuotes::SingleQ;
477 params.quotes_times = InsetQuotes::DoubleQ;
480 } else if (token == "\\papersize") {
481 tmpret = lex.FindToken(string_papersize);
485 params.papersize2 = tmpret;
486 } else if (token == "\\paperpackage") {
487 tmpret = lex.FindToken(string_paperpackages);
490 params.paperpackage = BufferParams::PACKAGE_NONE;
492 params.paperpackage = tmpret;
493 } else if (token == "\\use_geometry") {
495 params.use_geometry = lex.GetInteger();
496 } else if (token == "\\use_amsmath") {
498 params.use_amsmath = lex.GetInteger();
499 } else if (token == "\\paperorientation") {
500 tmpret = lex.FindToken(string_orientation);
501 if (tmpret == -1) ++tmpret;
502 if (tmpret != LYX_LAYOUT_DEFAULT)
503 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
504 } else if (token == "\\paperwidth") {
506 params.paperwidth = lex.GetString();
507 } else if (token == "\\paperheight") {
509 params.paperheight = lex.GetString();
510 } else if (token == "\\leftmargin") {
512 params.leftmargin = lex.GetString();
513 } else if (token == "\\topmargin") {
515 params.topmargin = lex.GetString();
516 } else if (token == "\\rightmargin") {
518 params.rightmargin = lex.GetString();
519 } else if (token == "\\bottommargin") {
521 params.bottommargin = lex.GetString();
522 } else if (token == "\\headheight") {
524 params.headheight = lex.GetString();
525 } else if (token == "\\headsep") {
527 params.headsep = lex.GetString();
528 } else if (token == "\\footskip") {
530 params.footskip = lex.GetString();
531 } else if (token == "\\paperfontsize") {
533 params.fontsize = strip(lex.GetString());
534 } else if (token == "\\papercolumns") {
536 params.columns = lex.GetInteger();
537 } else if (token == "\\papersides") {
539 switch(lex.GetInteger()) {
541 case 1: params.sides = LyXTextClass::OneSide; break;
542 case 2: params.sides = LyXTextClass::TwoSides; break;
544 } else if (token == "\\paperpagestyle") {
546 params.pagestyle = strip(lex.GetString());
547 } else if (token == "\\bullet") {
549 int index = lex.GetInteger();
551 int temp_int = lex.GetInteger();
552 params.user_defined_bullets[index].setFont(temp_int);
553 params.temp_bullets[index].setFont(temp_int);
555 temp_int = lex.GetInteger();
556 params.user_defined_bullets[index].setCharacter(temp_int);
557 params.temp_bullets[index].setCharacter(temp_int);
559 temp_int = lex.GetInteger();
560 params.user_defined_bullets[index].setSize(temp_int);
561 params.temp_bullets[index].setSize(temp_int);
563 string temp_str = lex.GetString();
564 if (temp_str != "\\end_bullet") {
565 // this element isn't really necessary for
566 // parsing but is easier for humans
567 // to understand bullets. Put it back and
568 // set a debug message?
569 lex.printError("\\end_bullet expected, got" + temp_str);
570 //how can I put it back?
572 } else if (token == "\\bulletLaTeX") {
574 int index = lex.GetInteger();
576 string temp_str = lex.GetString(), sum_str;
577 while (temp_str != "\\end_bullet") {
578 // this loop structure is needed when user
579 // enters an empty string since the first
580 // thing returned will be the \\end_bullet
582 // if the LaTeX entry has spaces. Each element
583 // therefore needs to be read in turn
586 temp_str = lex.GetString();
588 params.user_defined_bullets[index].setText(sum_str);
589 params.temp_bullets[index].setText(sum_str);
590 } else if (token == "\\secnumdepth") {
592 params.secnumdepth = lex.GetInteger();
593 } else if (token == "\\tocdepth") {
595 params.tocdepth = lex.GetInteger();
596 } else if (token == "\\baselinestretch") { // obsolete
597 lex.nextToken(); // should not be used directly
599 // Will probably keep a kind of support just for
601 params.spacing.set(Spacing::Other, lex.GetFloat());
602 } else if (token == "\\spacing") {
604 string tmp = strip(lex.GetString());
605 if (tmp == "single") {
606 params.spacing.set(Spacing::Single);
607 } else if (tmp == "onehalf") {
608 params.spacing.set(Spacing::Onehalf);
609 } else if (tmp == "double") {
610 params.spacing.set(Spacing::Double);
611 } else if (tmp == "other") {
613 params.spacing.set(Spacing::Other,
616 lex.printError("Unknown spacing token: '$$Token'");
618 } else if (token == "\\float_placement") {
620 params.float_placement = lex.GetString();
621 } else if (token == "\\cursor") { // obsolete
622 // this is obsolete, so we just skip it.
624 } else if (token == "\\family") {
626 font.setLyXFamily(lex.GetString());
627 } else if (token == "\\series") {
629 font.setLyXSeries(lex.GetString());
630 } else if (token == "\\shape") {
632 font.setLyXShape(lex.GetString());
633 } else if (token == "\\size") {
635 font.setLyXSize(lex.GetString());
636 } else if (token == "\\latex") {
638 string tok = lex.GetString();
639 // This is dirty, but gone with LyX3. (Asger)
640 if (tok == "no_latex")
641 font.setLatex(LyXFont::OFF);
642 else if (tok == "latex")
643 font.setLatex(LyXFont::ON);
644 else if (tok == "default")
645 font.setLatex(LyXFont::INHERIT);
647 lex.printError("Unknown LaTeX font flag "
649 } else if (token == "\\direction") {
651 string tok = lex.GetString();
653 font.setDirection(LyXFont::LTR_DIR);
654 else if (tok == "rtl")
655 font.setDirection(LyXFont::RTL_DIR);
656 else if (tok == "default")
657 font.setDirection(LyXFont::INHERIT_DIR);
659 lex.printError("Unknown font flag "
661 } else if (token == "\\emph") {
663 font.setEmph(font.setLyXMisc(lex.GetString()));
664 } else if (token == "\\bar") {
666 string tok = lex.GetString();
667 // This is dirty, but gone with LyX3. (Asger)
669 font.setUnderbar(LyXFont::ON);
670 else if (tok == "no")
671 font.setUnderbar(LyXFont::OFF);
672 else if (tok == "default")
673 font.setUnderbar(LyXFont::INHERIT);
675 lex.printError("Unknown bar font flag "
677 } else if (token == "\\noun") {
679 font.setNoun(font.setLyXMisc(lex.GetString()));
680 } else if (token == "\\color") {
682 font.setLyXColor(lex.GetString());
683 } else if (token == "\\align") {
684 tmpret = lex.FindToken(string_align);
685 if (tmpret == -1) ++tmpret;
686 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
688 for (; tmpret > 0; --tmpret)
689 tmpret2 = tmpret2 * 2;
690 par->align = LyXAlignment(tmpret2);
692 } else if (token == "\\added_space_top") {
694 par->added_space_top = lex.GetString();
695 } else if (token == "\\added_space_bottom") {
697 par->added_space_bottom = lex.GetString();
698 } else if (token == "\\pextra_type") {
700 par->pextra_type = lex.GetInteger();
701 } else if (token == "\\pextra_width") {
703 par->pextra_width = lex.GetString();
704 } else if (token == "\\pextra_widthp") {
706 par->pextra_widthp = lex.GetString();
707 } else if (token == "\\pextra_alignment") {
709 par->pextra_alignment = lex.GetInteger();
710 } else if (token == "\\pextra_hfill") {
712 par->pextra_hfill = lex.GetInteger();
713 } else if (token == "\\pextra_start_minipage") {
715 par->pextra_start_minipage = lex.GetInteger();
716 } else if (token == "\\labelwidthstring") {
718 par->labelwidthstring = lex.GetString();
719 /* do not delete this token, it is still needed! */
720 } else if (token == "\\end_inset") {
721 /* simple ignore this. The insets do not have
723 // but insets should read it, it is a part of
724 //the inset isn't it? Lgb.
725 } else if (token == "\\begin_inset") {
727 tmptok = lex.GetString();
728 /* test the different insets */
729 if (tmptok == "Quotes") {
730 inset = new InsetQuotes;
732 par->InsertChar(pos, LyXParagraph::META_INSET);
733 par->InsertInset(pos, inset);
734 par->SetFont(pos, font);
736 } else if (tmptok == "\\i") {
737 inset = new InsetLatexAccent;
739 par->InsertChar(pos, LyXParagraph::META_INSET);
740 par->InsertInset(pos, inset);
741 par->SetFont(pos, font);
743 } else if (tmptok == "FormulaMacro") {
744 inset = new InsetFormulaMacro;
746 par->InsertChar(pos, LyXParagraph::META_INSET);
747 par->InsertInset(pos, inset);
748 par->SetFont(pos, font);
750 } else if (tmptok == "Formula") {
751 inset = new InsetFormula;
753 par->InsertChar(pos, LyXParagraph::META_INSET);
754 par->InsertInset(pos, inset);
755 par->SetFont(pos, font);
757 } else if (tmptok == "Figure") {
758 inset = new InsetFig(100, 100, this);
760 par->InsertChar(pos, LyXParagraph::META_INSET);
761 par->InsertInset(pos, inset);
762 par->SetFont(pos, font);
764 } else if (tmptok == "Info") {
765 inset = new InsetInfo;
767 par->InsertChar(pos, LyXParagraph::META_INSET);
768 par->InsertInset(pos, inset);
769 par->SetFont(pos, font);
771 } else if (tmptok == "Include") {
772 inset = new InsetInclude(string(), this);
774 par->InsertChar(pos, LyXParagraph::META_INSET);
775 par->InsertInset(pos, inset);
776 par->SetFont(pos, font);
778 } else if (tmptok == "ERT") {
779 inset = new InsetERT(this);
781 par->InsertChar(pos, LyXParagraph::META_INSET);
782 par->InsertInset(pos, inset);
783 par->SetFont(pos, font);
785 } else if (tmptok == "Text") {
786 inset = new InsetText(this);
788 par->InsertChar(pos, LyXParagraph::META_INSET);
789 par->InsertInset(pos, inset);
790 par->SetFont(pos, font);
792 } else if (tmptok == "GRAPHICS") {
793 inset = new InsetGraphics;
795 par->InsertChar(pos, LyXParagraph::META_INSET);
796 par->InsertInset(pos, inset);
797 par->SetFont(pos, font);
798 } else if (tmptok == "LatexCommand") {
801 if (inscmd.getCmdName() == "cite") {
802 inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
803 } else if (inscmd.getCmdName() == "bibitem") {
804 lex.printError("Wrong place for bibitem");
805 inset = inscmd.Clone();
806 } else if (inscmd.getCmdName() == "BibTeX") {
807 inset = new InsetBibtex(inscmd.getContents(), inscmd.getOptions(), this);
808 } else if (inscmd.getCmdName() == "index") {
809 inset = new InsetIndex(inscmd.getContents());
810 } else if (inscmd.getCmdName() == "include") {
811 inset = new InsetInclude(inscmd.getContents(), this);
812 } else if (inscmd.getCmdName() == "label") {
813 inset = new InsetLabel(inscmd.getCommand());
814 } else if (inscmd.getCmdName() == "url"
815 || inscmd.getCmdName() == "htmlurl") {
816 inset = new InsetUrl(inscmd.getCommand());
817 } else if (inscmd.getCmdName() == "ref"
818 || inscmd.getCmdName() == "pageref") {
819 if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
820 inset = new InsetRef(inscmd, this);
822 #warning Verify that this else clause is still needed. (Lgb)
824 // This condition comes from a
825 // temporary solution to the latexdel
826 // ref inset that was transformed to
827 // an empty ref inset plus the body
828 // surronded by latexdel insets
830 string cont, opt, tmptmptok, cmdname;
832 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
837 tmptmptok = lex.GetString();
838 if(tmptmptok[0] == '\\') {
839 if( tmptmptok == "\\backslash")
849 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
854 tmptmptok = lex.GetString();
855 if(tmptmptok[0] == '\\') {
856 if( tmptmptok == "\\backslash")
866 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
872 cmdname = "\\" + inscmd.getCmdName();
873 cmdname += "[" + cont + "]";
874 cmdname += "{" + opt + "}";
875 inset = new InsetRef(cmdname, this);
878 } else if (inscmd.getCmdName() == "tableofcontents") {
879 inset = new InsetTOC(this);
880 } else if (inscmd.getCmdName() == "listoffigures") {
881 inset = new InsetLOF(this);
882 } else if (inscmd.getCmdName() == "listofalgorithms") {
883 inset = new InsetLOA(this);
884 } else if (inscmd.getCmdName() == "listoftables") {
885 inset = new InsetLOT(this);
886 } else if (inscmd.getCmdName() == "printindex") {
887 inset = new InsetPrintIndex(this);
888 } else if (inscmd.getCmdName() == "lyxparent") {
889 inset = new InsetParent(inscmd.getContents(), this);
893 par->InsertChar(pos, LyXParagraph::META_INSET);
894 par->InsertInset(pos, inset);
895 par->SetFont(pos, font);
899 } else if (token == "\\InsetQuotes") {
900 inset = new InsetQuotes;
902 par->InsertChar(pos, LyXParagraph::META_INSET);
903 par->InsertInset(pos, inset);
904 par->SetFont(pos, font);
906 } else if (token == "\\InsetFormula") {
907 inset = new InsetFormula;
909 par->InsertChar(pos, LyXParagraph::META_INSET);
910 par->InsertInset(pos, inset);
911 par->SetFont(pos, font);
913 } else if (token == "\\SpecialChar") {
914 inset = new InsetSpecialChar;
916 par->InsertChar(pos, LyXParagraph::META_INSET);
917 par->InsertInset(pos, inset);
918 par->SetFont(pos, font);
920 } else if (token == "\\Figure") {
921 inset = new InsetFig(100, 100, this);
923 par->InsertChar(pos, LyXParagraph::META_INSET);
924 par->InsertInset(pos, inset);
925 par->SetFont(pos, font);
927 } else if (token == "\\newline") { // soon obsolete
929 par->InsertChar(pos, LyXParagraph::META_NEWLINE);
930 par->SetFont(pos, font);
932 inset = new InsetSpecialChar(InsetSpecialChar::NEWLINE);
933 par->InsertChar(pos, LyXParagraph::META_INSET);
934 par->InsertInset(pos, inset);
935 par->SetFont(pos, font);
938 } else if (token == "\\LyXTable") {
939 par->table = new LyXTable(lex);
940 } else if (token == "\\hfill") {
941 par->InsertChar(pos, LyXParagraph::META_HFILL);
942 par->SetFont(pos, font);
944 } else if (token == "\\protected_separator") { // obsolete
946 inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
947 par->InsertChar(pos, LyXParagraph::META_INSET);
948 par->InsertInset(pos, inset);
949 par->SetFont(pos, font);
951 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
952 par->SetFont(pos, font);
955 } else if (token == "\\bibitem") { // ale970302
957 par->bibkey = new InsetBibKey;
958 par->bibkey->Read(lex);
959 }else if (token == "\\backslash") {
960 par->InsertChar(pos, '\\');
961 par->SetFont(pos, font);
963 }else if (token == "\\the_end") {
966 // This should be insurance for the future: (Asger)
967 lex.printError("Unknown token `$$Token'. "
968 "Inserting as text.");
969 int n = token.length();
970 for (int i = 0; i < n; ++i) {
971 par->InsertChar(pos, token[i]);
972 par->SetFont(pos, font);
979 bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
985 token = lex.GetString();
986 if (token == "\\lyxformat") { // the first token _must_ be...
988 format = lex.GetFloat();
990 if (LYX_FORMAT - format > 0.05) {
991 printf(_("Warning: need lyxformat %.2f but found %.2f\n"),
994 if (format - LYX_FORMAT > 0.05) {
995 printf(_("ERROR: need lyxformat %.2f but found %.2f\n"),
998 bool the_end = readLyXformat2(lex, par);
999 // Formats >= 2.13 support "\the_end" marker
1006 WriteAlert(_("Warning!"),
1007 _("Reading of document is not complete"),
1008 _("Maybe the document is truncated"));
1009 // We simulate a safe reading anyways to allow
1010 // users to take the chance... (Asger)
1014 WriteAlert(_("ERROR!"),
1015 _("Old LyX file format found. "
1016 "Use LyX 0.10.x to read this!"));
1020 } else { // "\\lyxformat" not found
1021 WriteAlert(_("ERROR!"), _("Not a LyX file!"));
1024 WriteAlert(_("ERROR!"), _("Unable to read file!"));
1030 // Should probably be moved to somewhere else: BufferView? LyXView?
1031 bool Buffer::save(bool makeBackup) const
1033 // We don't need autosaves in the immediate future. (Asger)
1034 resetAutosaveTimers();
1038 string s = fileName() + '~';
1039 // Rename is the wrong way of making a backup,
1040 // this is the correct way.
1041 /* truss cp fil fil2:
1042 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
1043 stat("LyXVC.lyx", 0xEFFFF688) = 0
1044 open("LyXVC.lyx", O_RDONLY) = 3
1045 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1046 fstat(4, 0xEFFFF508) = 0
1047 fstat(3, 0xEFFFF508) = 0
1048 read(3, " # T h i s f i l e w".., 8192) = 5579
1049 write(4, " # T h i s f i l e w".., 5579) = 5579
1050 read(3, 0xEFFFD4A0, 8192) = 0
1053 chmod("LyXVC3.lyx", 0100644) = 0
1054 lseek(0, 0, SEEK_CUR) = 46440
1058 // Should proabaly have some more error checking here.
1059 // Should be cleaned up in 0.13, at least a bit.
1060 // Doing it this way, also makes the inodes stay the same.
1061 // This is still not a very good solution, in particular we
1062 // might loose the owner of the backup.
1063 FileInfo finfo(fileName());
1064 if (finfo.exist()) {
1065 mode_t fmode = finfo.getMode();
1066 struct utimbuf * times = new struct utimbuf;
1068 times->actime = finfo.getAccessTime();
1069 times->modtime = finfo.getModificationTime();
1070 ifstream ifs(fileName().c_str());
1071 ofstream ofs(s.c_str(), ios::out|ios::trunc);
1076 ::chmod(s.c_str(), fmode);
1078 if (::utime(s.c_str(), times)) {
1079 lyxerr << "utime error." << endl;
1082 lyxerr << "LyX was not able to make "
1083 "backupcopy. Beware." << endl;
1089 if (writeFile(fileName(), false)) {
1092 // now delete the autosavefile
1093 string a = OnlyPath(fileName());
1095 a += OnlyFilename(fileName());
1097 FileInfo fileinfo(a);
1098 if (fileinfo.exist()) {
1099 if (::remove(a.c_str()) != 0) {
1100 WriteFSAlert(_("Could not delete "
1101 "auto-save file!"), a);
1105 // Saving failed, so backup is not backup
1107 string s = fileName() + '~';
1108 ::rename(s.c_str(), fileName().c_str());
1116 // Returns false if unsuccesful
1117 bool Buffer::writeFile(string const & fname, bool flag) const
1119 // if flag is false writeFile will not create any GUI
1120 // warnings, only cerr.
1121 // Needed for autosave in background or panic save (Matthias 120496)
1123 if (read_only && (fname == filename)) {
1124 // Here we should come with a question if we should
1125 // perform the write anyway.
1127 lyxerr << _("Error! Document is read-only: ")
1130 WriteAlert(_("Error! Document is read-only: "),
1135 FileInfo finfo(fname);
1136 if (finfo.exist() && !finfo.writable()) {
1137 // Here we should come with a question if we should
1138 // try to do the save anyway. (i.e. do a chmod first)
1140 lyxerr << _("Error! Cannot write file: ")
1143 WriteFSAlert(_("Error! Cannot write file: "),
1148 ofstream ofs(fname.c_str());
1151 lyxerr << _("Error! Cannot open file: ")
1154 WriteFSAlert(_("Error! Cannot open file: "),
1158 // The top of the file should not be written by params.
1160 // write out a comment in the top of the file
1161 ofs << '#' << LYX_DOCVERSION
1162 << " created this file. For more info see http://www.lyx.org/\n";
1163 ofs.setf(ios::showpoint|ios::fixed);
1165 ofs << "\\lyxformat " << setw(4) << LYX_FORMAT << "\n";
1167 // now write out the buffer paramters.
1168 params.writeFile(ofs);
1170 char footnoteflag = 0;
1173 // this will write out all the paragraphs
1174 // using recursive descent.
1175 paragraph->writeFile(ofs, params, footnoteflag, depth);
1177 // Write marker that shows file is complete
1178 ofs << "\n\\the_end" << endl;
1180 // how to check if close went ok?
1185 void Buffer::writeFileAscii(string const & fname, int linelen)
1187 LyXFont font1, font2;
1189 char c, footnoteflag = 0, depth = 0;
1191 LyXParagraph::size_type i;
1192 int j, h, ltype = 0, ltype_depth = 0,
1193 * clen = 0, actcell = 0, actpos = 0, cell = 0, cells = 0,
1196 bool ref_printed = false;
1198 ofstream ofs(fname.c_str());
1200 WriteFSAlert(_("Error: Cannot write file:"), fname);
1204 string fname1 = TmpFileName();
1205 LyXParagraph * par = paragraph;
1209 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE ||
1211 par->previous->footnoteflag == LyXParagraph::NO_FOOTNOTE){
1213 /* begins a footnote environment ? */
1214 if (footnoteflag != par->footnoteflag) {
1215 footnoteflag = par->footnoteflag;
1217 j = strlen(string_footnotekinds[par->footnotekind])+4;
1218 if (currlinelen + j > linelen)
1221 << string_footnotekinds[par->footnotekind] << "] ";
1226 /* begins or ends a deeper area ?*/
1227 if (depth != par->depth) {
1228 if (par->depth > depth) {
1229 while (par->depth > depth) {
1234 while (par->depth < depth) {
1240 /* First write the layout */
1241 tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1242 if (tmp == "Itemize") {
1244 ltype_depth = depth+1;
1245 } else if (tmp == "Enumerate") {
1247 ltype_depth = depth+1;
1248 } else if (strstr(tmp.c_str(), "ection")) {
1250 ltype_depth = depth+1;
1251 } else if (strstr(tmp.c_str(), "aragraph")) {
1253 ltype_depth = depth+1;
1254 } else if (tmp == "Description") {
1256 ltype_depth = depth+1;
1257 } else if (tmp == "Abstract") {
1260 } else if (tmp == "Bibliography") {
1268 /* maybe some vertical spaces */
1270 /* the labelwidthstring used in lists */
1274 /* some pagebreaks? */
1278 /* what about the alignment */
1281 /* dummy layout, that means a footnote ended */
1282 footnoteflag = LyXParagraph::NO_FOOTNOTE;
1287 /* It might be a table */
1290 if (!lyxrc->ascii_roff_command.empty() &&
1291 lyxrc->ascii_roff_command != "none") {
1292 RoffAsciiTable(ofs, par);
1299 cells = par->table->columns;
1300 clen = new int [cells];
1301 memset(clen, 0, sizeof(int) * cells);
1302 for (i = 0, j = 0, h = 1; i < par->size(); ++i, ++h) {
1303 c = par->GetChar(i);
1304 if (c == LyXParagraph::META_INSET) {
1305 if ((inset = par->GetInset(i))) {
1309 inset->Latex(ost, -1);
1310 h += ost.str().length();
1313 inset->Latex(ost, -1);
1315 char * tmp = ost.str();
1321 ofstream fs(fname1.c_str());
1323 WriteFSAlert(_("Error: Cannot open temporary file:"), fname1);
1326 inset->Latex(fs, -1);
1327 h += fs.tellp() - 1;
1328 ::remove(fname1.c_str());
1331 } else if (c == LyXParagraph::META_NEWLINE) {
1335 j = (++j) % par->table->NumberOfCellsInRow(actcell);
1343 font1 = LyXFont(LyXFont::ALL_INHERIT);
1345 for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
1346 if (!i && !footnoteflag && !noparbreak){
1348 for(j = 0; j < depth; ++j)
1350 currlinelen = depth * 2;
1352 case 0: /* Standard */
1353 case 4: /* (Sub)Paragraph */
1354 case 5: /* Description */
1356 case 6: /* Abstract */
1357 ofs << "Abstract\n\n";
1359 case 7: /* Bibliography */
1361 ofs << "References\n\n";
1366 ofs << par->labelstring << " ";
1369 if (ltype_depth > depth) {
1370 for(j = ltype_depth - 1; j > depth; --j)
1372 currlinelen += (ltype_depth-depth)*2;
1375 for(j = 0; j < cells; ++j) {
1377 for(h = 0; h < (clen[j] + 1);
1382 for(j = 0; j < depth; ++j)
1384 currlinelen = depth * 2;
1385 if (ltype_depth > depth) {
1386 for(j = ltype_depth;
1389 currlinelen += (ltype_depth-depth)*2;
1394 font2 = par->GetFontSettings(i);
1395 if (font1.latex() != font2.latex()) {
1396 if (font2.latex() == LyXFont::OFF)
1404 c = par->GetChar(i);
1408 case LyXParagraph::META_INSET:
1409 if ((inset = par->GetInset(i))) {
1411 inset->Latex(ofs, -1);
1412 currlinelen += (ofs.tellp() - fpos);
1413 actpos += (ofs.tellp() - fpos) - 1;
1416 case LyXParagraph::META_NEWLINE:
1418 if (par->table->NumberOfCellsInRow(actcell) <= cell) {
1419 for(j = actpos; j < clen[cell - 1]; ++j)
1422 for(j = 0; j < depth; ++j)
1424 currlinelen = depth*2;
1425 if (ltype_depth > depth) {
1426 for(j = ltype_depth; j > depth; --j)
1428 currlinelen += (ltype_depth-depth) * 2;
1430 for(j = 0; j < cells; ++j) {
1432 for(h = 0; h < (clen[j] + 1); ++h)
1436 for(j = 0; j < depth; ++j)
1438 currlinelen = depth * 2;
1439 if (ltype_depth > depth) {
1440 for(j = ltype_depth;
1443 currlinelen += (ltype_depth-depth)*2;
1449 j < clen[cell - 1]; ++j)
1455 currlinelen = actpos = 0;
1458 for(j = 0; j < depth; ++j)
1460 currlinelen = depth * 2;
1461 if (ltype_depth > depth) {
1462 for(j = ltype_depth;
1465 currlinelen += (ltype_depth - depth) * 2;
1469 case LyXParagraph::META_HFILL:
1473 case LyXParagraph::META_PROTECTED_SEPARATOR:
1481 if (currlinelen > linelen - 10
1482 && c == ' ' && i + 2 < par->size()) {
1484 for(j = 0; j < depth; ++j)
1486 currlinelen = depth * 2;
1487 if (ltype_depth > depth) {
1488 for(j = ltype_depth;
1491 currlinelen += (ltype_depth-depth)*2;
1493 } else if (c != '\0')
1496 lyxerr.debug() << "writeAsciiFile: NULL char in structure." << endl;
1502 for(j = actpos; j < clen[cell - 1]; ++j)
1505 for(j = 0; j < depth; ++j)
1507 currlinelen = depth * 2;
1508 if (ltype_depth > depth) {
1509 for(j = ltype_depth; j > depth; --j)
1511 currlinelen += (ltype_depth - depth) * 2;
1513 for(j = 0; j < cells; ++j) {
1515 for(h = 0; h < (clen[j] + 1); ++h)
1528 #ifdef USE_OSTREAM_ONLY
1529 void Buffer::makeLaTeXFile(string const & fname,
1530 string const & original_path,
1531 bool nice, bool only_body)
1533 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1535 niceFile = nice; // this will be used by Insetincludes.
1537 tex_code_break_column = lyxrc->ascii_linelen;
1539 LyXTextClass const & tclass =
1540 textclasslist.TextClass(params.textclass);
1542 ofstream ofs(fname.c_str());
1544 WriteFSAlert(_("Error: Cannot open file: "), fname);
1548 // validate the buffer.
1549 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1550 LaTeXFeatures features(tclass.numLayouts());
1552 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1555 // The starting paragraph of the coming rows is the
1556 // first paragraph of the document. (Asger)
1557 texrow.start(paragraph, 0);
1559 if (!only_body && nice) {
1560 ofs << "%% " LYX_DOCVERSION " created this file. "
1561 "For more info, see http://www.lyx.org/.\n"
1562 "%% Do not edit unless you really know what "
1567 lyxerr.debug() << "lyx header finished" << endl;
1568 // There are a few differences between nice LaTeX and usual files:
1569 // usual is \batchmode and has a
1570 // special input@path to allow the including of figures
1571 // with either \input or \includegraphics (what figinsets do).
1572 // batchmode is not set if there is a tex_code_break_column.
1573 // In this case somebody is interested in the generated LaTeX,
1574 // so this is OK. input@path is set when the actual parameter
1575 // original_path is set. This is done for usual tex-file, but not
1576 // for nice-latex-file. (Matthias 250696)
1579 // code for usual, NOT nice-latex-file
1580 ofs << "\\batchmode\n"; // changed
1581 // from \nonstopmode
1584 if (!original_path.empty()) {
1585 ofs << "\\makeatletter\n"
1586 << "\\def\\input@path{{"
1587 << original_path << "/}}\n"
1588 << "\\makeatother\n";
1594 ofs << "\\documentclass";
1596 string options; // the document class options.
1598 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
1599 // only write if existing in list (and not default)
1600 options += params.fontsize;
1605 if (!params.use_geometry &&
1606 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
1607 switch (params.papersize) {
1608 case BufferParams::PAPER_A4PAPER:
1609 options += "a4paper,";
1611 case BufferParams::PAPER_USLETTER:
1612 options += "letterpaper,";
1614 case BufferParams::PAPER_A5PAPER:
1615 options += "a5paper,";
1617 case BufferParams::PAPER_B5PAPER:
1618 options += "b5paper,";
1620 case BufferParams::PAPER_EXECUTIVEPAPER:
1621 options += "executivepaper,";
1623 case BufferParams::PAPER_LEGALPAPER:
1624 options += "legalpaper,";
1630 if (params.sides != tclass.sides()) {
1631 switch (params.sides) {
1632 case LyXTextClass::OneSide:
1633 options += "oneside,";
1635 case LyXTextClass::TwoSides:
1636 options += "twoside,";
1643 if (params.columns != tclass.columns()) {
1644 if (params.columns == 2)
1645 options += "twocolumn,";
1647 options += "onecolumn,";
1650 if (!params.use_geometry
1651 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1652 options += "landscape,";
1654 // language should be a parameter to \documentclass
1655 if (params.language != "default") {
1656 if (params.language == "hebrew")
1657 options += "english,";
1658 else if (lyxrc->rtl_support)
1659 options += "hebrew,";
1660 options += params.language + ',';
1661 } else if (lyxrc->rtl_support)
1662 options += "hebrew,english,";
1664 // the user-defined options
1665 if (!params.options.empty()) {
1666 options += params.options + ',';
1669 if (!options.empty()){
1670 options = strip(options, ',');
1671 ofs << '[' << options << ']';
1675 << textclasslist.LatexnameOfClass(params.textclass)
1678 // end of \documentclass defs
1680 // font selection must be done before loading fontenc.sty
1681 if (params.fonts != "default") {
1682 ofs << "\\usepackage{" << params.fonts << "}\n";
1685 // this one is not per buffer
1686 if (lyxrc->fontenc != "default") {
1687 ofs << "\\usepackage[" << lyxrc->fontenc
1691 if (params.inputenc != "default") {
1692 ofs << "\\usepackage[" << params.inputenc
1697 /* at the very beginning the text parameters */
1698 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
1699 switch (params.paperpackage) {
1700 case BufferParams::PACKAGE_A4:
1701 ofs << "\\usepackage{a4}\n";
1704 case BufferParams::PACKAGE_A4WIDE:
1705 ofs << "\\usepackage{a4wide}\n";
1708 case BufferParams::PACKAGE_WIDEMARGINSA4:
1709 ofs << "\\usepackage[widemargins]{a4}\n";
1714 if (params.use_geometry) {
1715 ofs << "\\usepackage{geometry}\n";
1717 ofs << "\\geometry{verbose";
1718 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1719 ofs << ",landscape";
1720 switch (params.papersize2) {
1721 case BufferParams::VM_PAPER_CUSTOM:
1722 if (!params.paperwidth.empty())
1723 ofs << ",paperwidth="
1724 << params.paperwidth;
1725 if (!params.paperheight.empty())
1726 ofs << ",paperheight="
1727 << params.paperheight;
1729 case BufferParams::VM_PAPER_USLETTER:
1730 ofs << ",letterpaper";
1732 case BufferParams::VM_PAPER_USLEGAL:
1733 ofs << ",legalpaper";
1735 case BufferParams::VM_PAPER_USEXECUTIVE:
1736 ofs << ",executivepaper";
1738 case BufferParams::VM_PAPER_A3:
1741 case BufferParams::VM_PAPER_A4:
1744 case BufferParams::VM_PAPER_A5:
1747 case BufferParams::VM_PAPER_B3:
1750 case BufferParams::VM_PAPER_B4:
1753 case BufferParams::VM_PAPER_B5:
1757 // default papersize ie BufferParams::VM_PAPER_DEFAULT
1758 switch (lyxrc->default_papersize) {
1759 case BufferParams::PAPER_DEFAULT: // keep compiler happy
1760 case BufferParams::PAPER_USLETTER:
1761 ofs << ",letterpaper";
1763 case BufferParams::PAPER_LEGALPAPER:
1764 ofs << ",legalpaper";
1766 case BufferParams::PAPER_EXECUTIVEPAPER:
1767 ofs << ",executivepaper";
1769 case BufferParams::PAPER_A3PAPER:
1772 case BufferParams::PAPER_A4PAPER:
1775 case BufferParams::PAPER_A5PAPER:
1778 case BufferParams::PAPER_B5PAPER:
1783 if (!params.topmargin.empty())
1784 ofs << ",tmargin=" << params.topmargin;
1785 if (!params.bottommargin.empty())
1786 ofs << ",bmargin=" << params.bottommargin;
1787 if (!params.leftmargin.empty())
1788 ofs << ",lmargin=" << params.leftmargin;
1789 if (!params.rightmargin.empty())
1790 ofs << ",rmargin=" << params.rightmargin;
1791 if (!params.headheight.empty())
1792 ofs << ",headheight=" << params.headheight;
1793 if (!params.headsep.empty())
1794 ofs << ",headsep=" << params.headsep;
1795 if (!params.footskip.empty())
1796 ofs << ",footskip=" << params.footskip;
1800 if (params.use_amsmath
1801 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
1802 ofs << "\\usepackage{amsmath}\n";
1806 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
1807 if (params.pagestyle == "fancy") {
1808 ofs << "\\usepackage{fancyhdr}\n";
1811 ofs << "\\pagestyle{" << params.pagestyle << "}\n";
1815 // We try to load babel late, in case it interferes
1816 // with other packages.
1817 if (params.language != "default" || lyxrc->rtl_support ) {
1818 ofs << "\\usepackage{babel}\n";
1822 if (params.secnumdepth != tclass.secnumdepth()) {
1823 ofs << "\\setcounter{secnumdepth}{"
1824 << params.secnumdepth
1828 if (params.tocdepth != tclass.tocdepth()) {
1829 ofs << "\\setcounter{tocdepth}{"
1835 if (params.paragraph_separation) {
1836 switch (params.defskip.kind()) {
1837 case VSpace::SMALLSKIP:
1838 ofs << "\\setlength\\parskip{\\smallskipamount}\n";
1840 case VSpace::MEDSKIP:
1841 ofs << "\\setlength\\parskip{\\medskipamount}\n";
1843 case VSpace::BIGSKIP:
1844 ofs << "\\setlength\\parskip{\\bigskipamount}\n";
1846 case VSpace::LENGTH:
1847 ofs << "\\setlength\\parskip{"
1848 << params.defskip.length().asLatexString()
1851 default: // should never happen // Then delete it.
1852 ofs << "\\setlength\\parskip{\\medskipamount}\n";
1857 ofs << "\\setlength\\parindent{0pt}\n";
1861 // Now insert the LyX specific LaTeX commands...
1862 string preamble, tmppreamble;
1864 // The optional packages;
1865 preamble = features.getPackages(params);
1867 // this might be useful...
1868 preamble += "\n\\makeatletter\n\n";
1870 // Some macros LyX will need
1871 tmppreamble = features.getMacros(params);
1873 if (!tmppreamble.empty()) {
1874 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1875 "LyX specific LaTeX commands.\n"
1876 + tmppreamble + '\n';
1879 // the text class specific preamble
1880 tmppreamble = features.getTClassPreamble(params);
1881 if (!tmppreamble.empty()) {
1882 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1883 "Textclass specific LaTeX commands.\n"
1884 + tmppreamble + '\n';
1887 /* the user-defined preamble */
1888 if (!params.preamble.empty()) {
1889 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1890 "User specified LaTeX commands.\n"
1891 + params.preamble + '\n';
1894 preamble += "\\makeatother\n\n";
1896 // Itemize bullet settings need to be last in case the user
1897 // defines their own bullets that use a package included
1898 // in the user-defined preamble -- ARRae
1899 // Actually it has to be done much later than that
1900 // since some packages like frenchb make modifications
1901 // at \begin{document} time -- JMarc
1903 for (int i = 0; i < 4; ++i) {
1904 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
1905 if (bullets_def.empty())
1906 bullets_def="\\AtBeginDocument{\n";
1907 bullets_def += " \\renewcommand{\\labelitemi";
1909 // `i' is one less than the item to modify
1916 bullets_def += "ii";
1922 bullets_def += "}{" +
1923 params.user_defined_bullets[i].getText()
1928 if (!bullets_def.empty())
1929 preamble += bullets_def + "}\n\n";
1931 for (int j = countChar(preamble, '\n'); j-- ;) {
1938 ofs << "\\begin{document}\n\n";
1942 lyxerr.debug() << "preamble finished, now the body." << endl;
1944 bool was_title = false;
1945 bool already_title = false;
1947 ostringstream ftnote;
1949 char * tmpholder = 0;
1954 LyXParagraph * par = paragraph;
1958 #ifndef HAVE_SSTREAM
1961 ftnote << tmpholder;
1962 delete [] tmpholder;
1967 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
1969 LyXLayout const & layout =
1970 textclasslist.Style(params.textclass,
1973 if (layout.intitle) {
1974 if (already_title) {
1975 lyxerr <<"Error in MakeLatexFile: You"
1976 " should not mix title layouts"
1977 " with normal ones." << endl;
1980 } else if (was_title && !already_title) {
1981 ofs << "\\maketitle\n";
1983 already_title = true;
1986 // We are at depth 0 so we can just use
1987 // ordinary \footnote{} generation
1988 // flag this with ftcount
1990 if (layout.isEnvironment()
1991 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
1992 par = par->TeXEnvironment(ofs, texrow,
1993 ftnote, ft_texrow, ftcount);
1995 par = par->TeXOnePar(ofs, texrow,
1996 ftnote, ft_texrow, ftcount);
1999 // Write out what we've generated...
2002 ofs << "\\addtocounter{footnote}{-"
2006 ofs << ftnote.str();
2007 texrow += ft_texrow;
2009 // The extra .c_str() is needed when we use
2010 // lyxstring instead of the STL string class.
2011 ftnote.str(string().c_str());
2013 delete [] ftnote.str();
2018 #ifndef HAVE_SSTREAM
2020 // I hate strstreams
2021 tmpholder = ftnote.str();
2025 #ifndef HAVE_SSTREAM
2026 delete [] tmpholder;
2028 // It might be that we only have a title in this document
2029 if (was_title && !already_title) {
2030 ofs << "\\maketitle\n";
2035 ofs << "\\end{document}\n";
2038 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2040 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2044 // Just to be sure. (Asger)
2047 // tex_code_break_column's value is used to decide
2048 // if we are in batchmode or not (within mathed_write()
2049 // in math_write.C) so we must set it to a non-zero
2050 // value when we leave otherwise we save incorrect .lyx files.
2051 tex_code_break_column = lyxrc->ascii_linelen;
2055 lyxerr << "File was not closed properly." << endl;
2058 lyxerr.debug() << "Finished making latex file." << endl;
2061 void Buffer::makeLaTeXFile(string const & fname,
2062 string const & original_path,
2063 bool nice, bool only_body)
2065 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
2067 niceFile = nice; // this will be used by Insetincludes.
2069 tex_code_break_column = lyxrc->ascii_linelen;
2071 LyXTextClass const & tclass =
2072 textclasslist.TextClass(params.textclass);
2074 ofstream ofs(fname.c_str());
2076 WriteFSAlert(_("Error: Cannot open file: "), fname);
2080 // validate the buffer.
2081 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
2082 LaTeXFeatures features(tclass.numLayouts());
2084 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
2087 // The starting paragraph of the coming rows is the
2088 // first paragraph of the document. (Asger)
2089 texrow.start(paragraph, 0);
2093 if (!only_body && nice) {
2094 LFile += "%% " LYX_DOCVERSION " created this file. "
2095 "For more info, see http://www.lyx.org/.\n";
2096 LFile += "%% Do not edit unless you really know what you are doing.\n";
2100 lyxerr.debug() << "lyx header finished" << endl;
2101 // There are a few differences between nice LaTeX and usual files:
2102 // usual is \batchmode and has a
2103 // special input@path to allow the including of figures
2104 // with either \input or \includegraphics (what figinsets do).
2105 // batchmode is not set if there is a tex_code_break_column.
2106 // In this case somebody is interested in the generated LaTeX,
2107 // so this is OK. input@path is set when the actual parameter
2108 // original_path is set. This is done for usual tex-file, but not
2109 // for nice-latex-file. (Matthias 250696)
2112 // code for usual, NOT nice-latex-file
2113 LFile += "\\batchmode\n"; // changed
2114 // from \nonstopmode
2117 if (!original_path.empty()) {
2118 LFile += "\\makeatletter\n";
2120 LFile += "\\def\\input@path{{" + original_path
2123 LFile += "\\makeatother\n";
2127 LFile += "\\documentclass";
2129 string options; // the document class options.
2131 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
2132 // only write if existing in list (and not default)
2133 options += params.fontsize;
2138 if (!params.use_geometry &&
2139 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2140 switch (params.papersize) {
2141 case BufferParams::PAPER_A4PAPER:
2142 options += "a4paper,";
2144 case BufferParams::PAPER_USLETTER:
2145 options += "letterpaper,";
2147 case BufferParams::PAPER_A5PAPER:
2148 options += "a5paper,";
2150 case BufferParams::PAPER_B5PAPER:
2151 options += "b5paper,";
2153 case BufferParams::PAPER_EXECUTIVEPAPER:
2154 options += "executivepaper,";
2156 case BufferParams::PAPER_LEGALPAPER:
2157 options += "legalpaper,";
2163 if (params.sides != tclass.sides()) {
2164 switch (params.sides) {
2165 case LyXTextClass::OneSide:
2166 options += "oneside,";
2168 case LyXTextClass::TwoSides:
2169 options += "twoside,";
2176 if (params.columns != tclass.columns()) {
2177 if (params.columns == 2)
2178 options += "twocolumn,";
2180 options += "onecolumn,";
2183 if (!params.use_geometry
2184 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2185 options += "landscape,";
2187 // language should be a parameter to \documentclass
2188 if (params.language != "default") {
2189 if (params.language == "hebrew")
2190 options += "english,";
2191 else if (lyxrc->rtl_support)
2192 options += "hebrew,";
2193 options += params.language + ',';
2194 } else if (lyxrc->rtl_support)
2195 options += "hebrew,english,";
2197 // the user-defined options
2198 if (!params.options.empty()) {
2199 options += params.options + ',';
2202 if (!options.empty()){
2203 options = strip(options, ',');
2210 LFile += textclasslist.LatexnameOfClass(params.textclass);
2213 // end of \documentclass defs
2215 // font selection must be done before loading fontenc.sty
2216 if (params.fonts != "default") {
2217 LFile += "\\usepackage{" + params.fonts + "}\n";
2220 // this one is not per buffer
2221 if (lyxrc->fontenc != "default") {
2222 LFile += "\\usepackage[" + lyxrc->fontenc
2226 if (params.inputenc != "default") {
2227 LFile += "\\usepackage[" + params.inputenc
2232 /* at the very beginning the text parameters */
2233 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2234 switch (params.paperpackage) {
2235 case BufferParams::PACKAGE_A4:
2236 LFile += "\\usepackage{a4}\n";
2239 case BufferParams::PACKAGE_A4WIDE:
2240 LFile += "\\usepackage{a4wide}\n";
2243 case BufferParams::PACKAGE_WIDEMARGINSA4:
2244 LFile += "\\usepackage[widemargins]{a4}\n";
2249 if (params.use_geometry) {
2250 LFile += "\\usepackage{geometry}\n";
2252 LFile += "\\geometry{verbose";
2253 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2254 LFile += ",landscape";
2255 switch (params.papersize2) {
2256 case BufferParams::VM_PAPER_CUSTOM:
2257 if (!params.paperwidth.empty())
2258 LFile += ",paperwidth="
2259 + params.paperwidth;
2260 if (!params.paperheight.empty())
2261 LFile += ",paperheight="
2262 + params.paperheight;
2264 case BufferParams::VM_PAPER_USLETTER:
2265 LFile += ",letterpaper";
2267 case BufferParams::VM_PAPER_USLEGAL:
2268 LFile += ",legalpaper";
2270 case BufferParams::VM_PAPER_USEXECUTIVE:
2271 LFile += ",executivepaper";
2273 case BufferParams::VM_PAPER_A3:
2274 LFile += ",a3paper";
2276 case BufferParams::VM_PAPER_A4:
2277 LFile += ",a4paper";
2279 case BufferParams::VM_PAPER_A5:
2280 LFile += ",a5paper";
2282 case BufferParams::VM_PAPER_B3:
2283 LFile += ",b3paper";
2285 case BufferParams::VM_PAPER_B4:
2286 LFile += ",b4paper";
2288 case BufferParams::VM_PAPER_B5:
2289 LFile += ",b5paper";
2292 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2293 switch (lyxrc->default_papersize) {
2294 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2295 case BufferParams::PAPER_USLETTER:
2296 LFile += ",letterpaper";
2298 case BufferParams::PAPER_LEGALPAPER:
2299 LFile += ",legalpaper";
2301 case BufferParams::PAPER_EXECUTIVEPAPER:
2302 LFile += ",executivepaper";
2304 case BufferParams::PAPER_A3PAPER:
2305 LFile += ",a3paper";
2307 case BufferParams::PAPER_A4PAPER:
2308 LFile += ",a4paper";
2310 case BufferParams::PAPER_A5PAPER:
2311 LFile += ",a5paper";
2313 case BufferParams::PAPER_B5PAPER:
2314 LFile += ",b5paper";
2318 if (!params.topmargin.empty())
2319 LFile += ",tmargin=" + params.topmargin;
2320 if (!params.bottommargin.empty())
2321 LFile += ",bmargin=" + params.bottommargin;
2322 if (!params.leftmargin.empty())
2323 LFile += ",lmargin=" + params.leftmargin;
2324 if (!params.rightmargin.empty())
2325 LFile += ",rmargin=" + params.rightmargin;
2326 if (!params.headheight.empty())
2327 LFile += ",headheight=" + params.headheight;
2328 if (!params.headsep.empty())
2329 LFile += ",headsep=" + params.headsep;
2330 if (!params.footskip.empty())
2331 LFile += ",footskip=" + params.footskip;
2335 if (params.use_amsmath
2336 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
2337 LFile += "\\usepackage{amsmath}\n";
2340 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
2341 if (params.pagestyle == "fancy") {
2342 LFile += "\\usepackage{fancyhdr}\n";
2345 LFile += "\\pagestyle{" + params.pagestyle + "}\n";
2349 // We try to load babel late, in case it interferes
2350 // with other packages.
2351 if (params.language != "default" || lyxrc->rtl_support ) {
2352 LFile += "\\usepackage{babel}\n";
2356 if (params.secnumdepth != tclass.secnumdepth()) {
2357 LFile += "\\setcounter{secnumdepth}{";
2358 LFile += tostr(params.secnumdepth);
2362 if (params.tocdepth != tclass.tocdepth()) {
2363 LFile += "\\setcounter{tocdepth}{";
2364 LFile += tostr(params.tocdepth);
2369 if (params.paragraph_separation) {
2370 switch (params.defskip.kind()) {
2371 case VSpace::SMALLSKIP:
2372 LFile += "\\setlength\\parskip{\\smallskipamount}\n";
2374 case VSpace::MEDSKIP:
2375 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2377 case VSpace::BIGSKIP:
2378 LFile += "\\setlength\\parskip{\\bigskipamount}\n";
2380 case VSpace::LENGTH:
2381 LFile += "\\setlength\\parskip{"
2382 + params.defskip.length().asLatexString()
2385 default: // should never happen // Then delete it.
2386 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2391 LFile += "\\setlength\\parindent{0pt}\n";
2395 // Write out what we've generated so far...and reset LFile
2399 // Now insert the LyX specific LaTeX commands...
2400 string preamble, tmppreamble;
2402 // The optional packages;
2403 preamble = features.getPackages(params);
2405 // this might be useful...
2406 preamble += "\n\\makeatletter\n\n";
2408 // Some macros LyX will need
2409 tmppreamble = features.getMacros(params);
2411 if (!tmppreamble.empty()) {
2412 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2413 "LyX specific LaTeX commands.\n"
2414 + tmppreamble + '\n';
2417 // the text class specific preamble
2418 tmppreamble = features.getTClassPreamble(params);
2419 if (!tmppreamble.empty()) {
2420 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2421 "Textclass specific LaTeX commands.\n"
2422 + tmppreamble + '\n';
2425 /* the user-defined preamble */
2426 if (!params.preamble.empty()) {
2427 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2428 "User specified LaTeX commands.\n"
2429 + params.preamble + '\n';
2432 preamble += "\\makeatother\n\n";
2434 // Itemize bullet settings need to be last in case the user
2435 // defines their own bullets that use a package included
2436 // in the user-defined preamble -- ARRae
2437 // Actually it has to be done much later than that
2438 // since some packages like frenchb make modifications
2439 // at \begin{document} time -- JMarc
2441 for (int i = 0; i < 4; ++i) {
2442 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2443 if (bullets_def.empty())
2444 bullets_def="\\AtBeginDocument{\n";
2445 bullets_def += " \\renewcommand{\\labelitemi";
2447 // `i' is one less than the item to modify
2454 bullets_def += "ii";
2460 bullets_def += "}{" +
2461 params.user_defined_bullets[i].getText()
2466 if (!bullets_def.empty())
2467 preamble += bullets_def + "}\n\n";
2469 for (int j = countChar(preamble, '\n'); j-- ;) {
2476 LFile += "\\begin{document}\n\n";
2480 lyxerr.debug() << "preamble finished, now the body." << endl;
2482 bool was_title = false;
2483 bool already_title = false;
2489 LyXParagraph * par = paragraph;
2495 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
2497 LyXLayout const & layout =
2498 textclasslist.Style(params.textclass,
2501 if (layout.intitle) {
2502 if (already_title) {
2503 lyxerr <<"Error in MakeLatexFile: You"
2504 " should not mix title layouts"
2505 " with normal ones." << endl;
2508 } else if (was_title && !already_title) {
2509 LFile += "\\maketitle\n";
2511 already_title = true;
2514 // We are at depth 0 so we can just use
2515 // ordinary \footnote{} generation
2516 // flag this with ftcount
2518 if (layout.isEnvironment()
2519 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
2520 par = par->TeXEnvironment(LFile, texrow,
2521 ftnote, ft_texrow, ftcount);
2523 par = par->TeXOnePar(LFile, texrow,
2524 ftnote, ft_texrow, ftcount);
2527 // Write out what we've generated...and reset LFile
2530 LFile += "\\addtocounter{footnote}{-";
2531 LFile += tostr(ftcount - 1);
2535 texrow += ft_texrow;
2540 if (loop_count == 2) {
2541 // fwrite()ing every second time through the loop
2542 // gains a few extra % of speed; going higher than
2543 // 2 will slow things down again. I'll look at
2544 // LFile.length() in a future revision. ARRae
2551 // It might be that we only have a title in this document
2552 if (was_title && !already_title) {
2553 LFile += "\\maketitle\n";
2558 LFile += "\\end{document}\n";
2561 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2563 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2567 // Just to be sure. (Asger)
2570 // Write out what we've generated...and reset LFile
2574 // tex_code_break_column's value is used to decide
2575 // if we are in batchmode or not (within mathed_write()
2576 // in math_write.C) so we must set it to a non-zero
2577 // value when we leave otherwise we save incorrect .lyx files.
2578 tex_code_break_column = lyxrc->ascii_linelen;
2580 // How to check if the close went ok when using fstreams
2582 lyxerr.debug() << "Finished making latex file." << endl;
2587 bool Buffer::isLatex() const
2589 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2593 bool Buffer::isLinuxDoc() const
2595 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2599 bool Buffer::isLiterate() const
2601 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2605 bool Buffer::isDocBook() const
2607 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2611 bool Buffer::isSGML() const
2613 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2614 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2618 void Buffer::sgmlOpenTag(ostream & os, int depth,
2619 string const & latexname) const
2621 os << string(depth, ' ') << "<" << latexname << ">\n";
2625 void Buffer::sgmlCloseTag(ostream & os, int depth,
2626 string const & latexname) const
2628 os << string(depth, ' ') << "</" << latexname << ">\n";
2632 void Buffer::makeLinuxDocFile(string const & fname, int column)
2634 LyXParagraph * par = paragraph;
2636 string top_element = textclasslist.LatexnameOfClass(params.textclass);
2637 string environment_stack[10];
2640 int depth = 0; /* paragraph depth */
2642 ofstream ofs(fname.c_str());
2645 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2649 tex_code_break_column = column;
2652 if (params.preamble.empty()) {
2653 ofs << "<!doctype linuxdoc system>\n\n";
2656 ofs << "<!doctype linuxdoc system \n [ "
2657 << params.preamble << " \n]>\n\n";
2660 ofs << "<!-- " << LYX_DOCVERSION
2661 << " created this file. For more info see http://www.lyx.org/"
2664 if(params.options.empty())
2665 sgmlOpenTag(ofs, 0, top_element);
2667 string top = top_element;
2669 top += params.options;
2670 sgmlOpenTag(ofs, 0, top);
2674 int desc_on = 0; /* description mode*/
2675 LyXLayout const & style =
2676 textclasslist.Style(users->buffer()->params.textclass,
2678 par->AutoDeleteInsets();
2680 /* treat <toc> as a special case for compatibility with old code */
2681 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2682 Inset * inset = par->GetInset(0);
2683 char lyx_code = inset->LyxCode();
2684 if (lyx_code == Inset::TOC_CODE){
2685 string temp = "toc";
2686 sgmlOpenTag(ofs, depth, temp);
2689 linuxDocHandleFootnote(ofs, par, depth);
2694 // environment tag closing
2695 for( ; depth > par->depth; --depth) {
2696 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2697 environment_stack[depth].clear();
2700 // write opening SGML tags
2701 switch(style.latextype) {
2702 case LATEX_PARAGRAPH:
2703 if(depth == par->depth
2704 && !environment_stack[depth].empty()) {
2705 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2706 environment_stack[depth].clear();
2712 sgmlOpenTag(ofs, depth, style.latexname());
2717 LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
2719 if (!environment_stack[depth].empty()){
2720 sgmlCloseTag(ofs, depth,
2721 environment_stack[depth]);
2725 environment_stack[depth].clear();
2726 sgmlOpenTag(ofs, depth, style.latexname());
2729 case LATEX_ENVIRONMENT:
2730 case LATEX_ITEM_ENVIRONMENT:
2731 if(depth == par->depth
2732 && environment_stack[depth] != style.latexname()
2733 && !environment_stack[depth].empty()) {
2735 sgmlCloseTag(ofs, depth,
2736 environment_stack[depth]);
2737 environment_stack[depth].clear();
2739 if (depth < par->depth) {
2741 environment_stack[depth].clear();
2743 if (environment_stack[depth] != style.latexname()) {
2746 sgmlOpenTag(ofs, depth, temp);
2748 environment_stack[depth] = style.latexname();
2749 sgmlOpenTag(ofs, depth, environment_stack[depth]);
2751 if(style.latextype == LATEX_ENVIRONMENT) break;
2753 desc_on = (style.labeltype == LABEL_MANUAL);
2760 sgmlOpenTag(ofs, depth + 1, item_name);
2763 sgmlOpenTag(ofs, depth, style.latexname());
2768 SimpleLinuxDocOnePar(ofs, par, desc_on, depth);
2771 linuxDocHandleFootnote(ofs, par, depth);
2773 while(par && par->IsDummy());
2776 /* write closing SGML tags */
2777 switch(style.latextype) {
2779 case LATEX_ENVIRONMENT:
2780 case LATEX_ITEM_ENVIRONMENT:
2783 sgmlCloseTag(ofs, depth, style.latexname());
2788 /* Close open tags */
2789 for(; depth > 0; --depth)
2790 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2792 if(!environment_stack[depth].empty())
2793 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2796 sgmlCloseTag(ofs, 0, top_element);
2799 // How to check for successful close
2803 void Buffer::linuxDocHandleFootnote(ostream & os, LyXParagraph * & par,
2806 string tag = "footnote";
2808 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2809 sgmlOpenTag(os, depth + 1, tag);
2810 SimpleLinuxDocOnePar(os, par, 0, depth + 1);
2811 sgmlCloseTag(os, depth + 1, tag);
2817 void Buffer::DocBookHandleCaption(ostream & os, string & inner_tag,
2818 int const depth, int desc_on,
2819 LyXParagraph * & par)
2821 LyXParagraph * tpar = par;
2822 while (tpar && (tpar->footnoteflag != LyXParagraph::NO_FOOTNOTE) &&
2823 (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2827 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2828 "Caption").second) {
2829 sgmlOpenTag(os, depth + 1, inner_tag);
2830 #ifdef USE_OSTREAM_ONLY
2832 SimpleDocBookOnePar(os, extra_par, tpar,
2833 desc_on, depth + 2);
2835 string tmp_par, extra_par;
2836 SimpleDocBookOnePar(tmp_par, extra_par, tpar,
2837 desc_on, depth + 2);
2838 tmp_par = strip(tmp_par);
2839 tmp_par = frontStrip(tmp_par);
2842 sgmlCloseTag(os, depth+1, inner_tag);
2843 if(!extra_par.empty())
2849 void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
2852 string tag, inner_tag;
2853 string tmp_par, extra_par;
2854 bool inner_span = false;
2857 // This is not how I like to see enums. They should not be anonymous
2858 // and variables of its type should not be declared right after the
2859 // last brace. (Lgb)
2867 SOME_ENUM last = NO_ONE;
2868 SOME_ENUM present = FOOTNOTE_LIKE;
2870 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2871 if(last == present) {
2873 if(!tmp_par.empty()) {
2876 sgmlCloseTag(os, depth+1, inner_tag);
2877 sgmlOpenTag(os, depth+1, inner_tag);
2884 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1,
2886 if(!extra_par.empty()) os << extra_par;
2887 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2890 switch (par->footnotekind) {
2891 case LyXParagraph::FOOTNOTE:
2892 case LyXParagraph::ALGORITHM:
2895 present = FOOTNOTE_LIKE;
2898 case LyXParagraph::MARGIN:
2901 present = MARGIN_LIKE;
2904 case LyXParagraph::FIG:
2905 case LyXParagraph::WIDE_FIG:
2907 inner_tag = "title";
2911 case LyXParagraph::TAB:
2912 case LyXParagraph::WIDE_TAB:
2914 inner_tag = "title";
2919 sgmlOpenTag(os, depth, tag);
2920 if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
2921 DocBookHandleCaption(os, inner_tag, depth,
2925 sgmlOpenTag(os, depth + 1, inner_tag);
2928 // ignore all caption here, we processed them above!!!
2929 if (par->layout != textclasslist.NumberOfLayout(params.textclass,
2930 "Caption").second) {
2931 #ifdef USE_OSTREAM_ONLY
2937 SimpleDocBookOnePar(ost, extra_par, par,
2938 desc_on, depth + 2);
2940 tmp_par += ost.str().c_str();
2943 char * ctmp = ost.str();
2950 tmp_par = frontStrip(strip(tmp_par));
2956 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1, inner_tag);
2957 if(!extra_par.empty()) os << extra_par;
2958 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2962 /* push a tag in a style stack */
2963 void Buffer::push_tag(ostream & os, char const * tag,
2964 int & pos, char stack[5][3])
2966 /* pop all previous tags */
2967 for (int j = pos; j >= 0; --j)
2968 os << "</" << stack[j] << ">";
2971 sprintf(stack[++pos], "%s", tag);
2974 for (int i = 0; i <= pos; ++i)
2975 os << "<" << stack[i] << ">";
2978 void Buffer::pop_tag(ostream & os, char const * tag,
2979 int & pos, char stack[5][3])
2983 // pop all tags till specified one
2984 for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
2985 os << "</" << stack[j] << ">";
2988 os << "</" << tag << ">";
2990 // push all tags, but the specified one
2991 for (j = j + 1; j <= pos; ++j) {
2992 os << "<" << stack[j] << ">";
2993 strcpy(stack[j-1], stack[j]);
2999 /* handle internal paragraph parsing -- layout already processed */
3001 // checks, if newcol chars should be put into this line
3002 // writes newline, if necessary.
3004 void linux_doc_line_break(ostream & os, unsigned int & colcount,
3005 const unsigned int newcol)
3008 if (colcount > lyxrc->ascii_linelen) {
3010 colcount = newcol; // assume write after this call
3015 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
3016 int desc_on, int const /*depth*/)
3018 LyXFont font1, font2;
3021 LyXParagraph::size_type main_body;
3023 LyXLayout const & style = textclasslist.Style(params.textclass,
3026 char family_type = 0; // family font flag
3027 bool is_bold = false; // series font flag
3028 char shape_type = 0; // shape font flag
3029 bool is_em = false; // emphasis (italic) font flag
3031 int stack_num = -1; // style stack position
3032 char stack[5][3]; // style stack
3033 unsigned int char_line_count = 5; // Heuristic choice ;-)
3035 if (style.labeltype != LABEL_MANUAL)
3038 main_body = par->BeginningOfMainBody();
3040 /* gets paragraph main font */
3042 font1 = style.labelfont;
3047 /* parsing main loop */
3048 for (LyXParagraph::size_type i = 0;
3049 i < par->size(); ++i) {
3051 /* handle quote tag */
3052 if (i == main_body && !par->IsDummy()) {
3057 font2 = par->getFont(i);
3059 if (font1.family() != font2.family()) {
3060 switch(family_type) {
3062 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3063 push_tag(os, "tt", stack_num, stack);
3066 else if (font2.family() == LyXFont::SANS_FAMILY) {
3067 push_tag(os, "sf", stack_num, stack);
3072 pop_tag(os, "tt", stack_num, stack);
3073 if (font2.family() == LyXFont::SANS_FAMILY) {
3074 push_tag(os, "sf", stack_num, stack);
3082 pop_tag(os, "sf", stack_num, stack);
3083 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3084 push_tag(os, "tt", stack_num, stack);
3093 /* handle bold face */
3094 if (font1.series() != font2.series()) {
3095 if (font2.series() == LyXFont::BOLD_SERIES) {
3096 push_tag(os, "bf", stack_num, stack);
3100 pop_tag(os, "bf", stack_num, stack);
3105 /* handle italic and slanted fonts */
3106 if (font1.shape() != font2.shape()) {
3107 switch(shape_type) {
3109 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3110 push_tag(os, "it", stack_num, stack);
3113 else if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3114 push_tag(os, "sl", stack_num, stack);
3119 pop_tag(os, "it", stack_num, stack);
3120 if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3121 push_tag(os, "sl", stack_num, stack);
3129 pop_tag(os, "sl", stack_num, stack);
3130 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3131 push_tag(os, "it", stack_num, stack);
3139 /* handle <em> tag */
3140 if (font1.emph() != font2.emph()) {
3141 if (font2.emph() == LyXFont::ON) {
3142 push_tag(os, "em", stack_num, stack);
3145 pop_tag(os, "em", stack_num, stack);
3150 c = par->GetChar(i);
3152 if (font2.latex() == LyXFont::ON) {
3153 // "TeX"-Mode on == > SGML-Mode on.
3155 os << c; // see LaTeX-Generation...
3157 } else if (c == LyXParagraph::META_INSET) {
3158 inset = par->GetInset(i);
3159 #ifdef USE_OSTREAM_ONLY
3160 inset->Linuxdoc(os);
3163 inset->Linuxdoc(tmp_out);
3169 if (par->linuxDocConvertChar(c, sgml_string)
3170 && !style.free_spacing) { // in freespacing
3172 // non-breaking characters
3176 linux_doc_line_break(os, char_line_count, 6);
3181 linux_doc_line_break(os, char_line_count, 1);
3187 char_line_count += sgml_string.length();
3193 /* needed if there is an optional argument but no contents */
3194 if (main_body > 0 && main_body == par->size()) {
3198 /* pop all defined Styles */
3199 for (j = stack_num; j >= 0; --j) {
3200 linux_doc_line_break(os,
3202 3 + strlen(stack[j]));
3203 os << "</" << stack[j] << ">";
3206 /* resets description flag correctly */
3209 /* <tag> not closed... */
3210 linux_doc_line_break(os, char_line_count, 6);
3214 /* fprintf(file, "</p>");*/
3220 /* print an error message */
3221 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
3222 char const * message)
3224 InsetError * new_inset;
3226 /* insert an error marker in text */
3227 new_inset = new InsetError(message);
3228 par->InsertChar(pos, LyXParagraph::META_INSET);
3229 par->InsertInset(pos, new_inset);
3232 // This constant defines the maximum number of
3233 // environment layouts that can be nesteded.
3234 // The same applies for command layouts.
3235 // These values should be more than enough.
3236 // José Matos (1999/07/22)
3238 enum { MAX_NEST_LEVEL = 25};
3240 void Buffer::makeDocBookFile(string const & fname, int column)
3242 LyXParagraph * par = paragraph;
3244 string top_element= textclasslist.LatexnameOfClass(params.textclass);
3245 string environment_stack[MAX_NEST_LEVEL];
3246 string environment_inner[MAX_NEST_LEVEL];
3247 string command_stack[MAX_NEST_LEVEL];
3248 bool command_flag= false;
3249 int command_depth= 0, command_base= 0, cmd_depth= 0;
3251 string item_name, command_name;
3252 string c_depth, c_params, tmps;
3254 int depth= 0; /* paragraph depth */
3256 tex_code_break_column = column;
3258 ofstream ofs(fname.c_str());
3260 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3266 ofs << "<!doctype " << top_element
3267 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
3269 if (params.preamble.empty())
3272 ofs << "\n [ " << params.preamble << " \n]>\n\n";
3274 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
3275 << "\n See http://www.lyx.org/ for more information -->\n";
3277 if(params.options.empty())
3278 sgmlOpenTag(ofs, 0, top_element);
3280 string top = top_element;
3282 top += params.options;
3283 sgmlOpenTag(ofs, 0, top);
3287 int desc_on = 0; /* description mode*/
3288 LyXLayout const & style =
3289 textclasslist.Style(users->buffer()->params.textclass,
3291 par->AutoDeleteInsets();
3293 /* environment tag closing */
3294 for( ; depth > par->depth; --depth) {
3295 if(environment_inner[depth] != "!-- --") {
3296 item_name= "listitem";
3297 sgmlCloseTag(ofs, command_depth + depth,
3299 if( environment_inner[depth] == "varlistentry")
3300 sgmlCloseTag(ofs, depth+command_depth,
3301 environment_inner[depth]);
3303 sgmlCloseTag(ofs, depth + command_depth,
3304 environment_stack[depth]);
3305 environment_stack[depth].clear();
3306 environment_inner[depth].clear();
3309 if(depth == par->depth
3310 && environment_stack[depth] != style.latexname()
3311 && !environment_stack[depth].empty()) {
3312 if(environment_inner[depth] != "!-- --") {
3313 item_name= "listitem";
3314 sgmlCloseTag(ofs, command_depth+depth,
3316 if( environment_inner[depth] == "varlistentry")
3318 depth + command_depth,
3319 environment_inner[depth]);
3322 sgmlCloseTag(ofs, depth + command_depth,
3323 environment_stack[depth]);
3325 environment_stack[depth].clear();
3326 environment_inner[depth].clear();
3329 // Write opening SGML tags.
3330 switch(style.latextype) {
3331 case LATEX_PARAGRAPH:
3332 if(style.latexname() != "dummy")
3333 sgmlOpenTag(ofs, depth+command_depth,
3339 LinuxDocError(par, 0,
3340 _("Error : Wrong depth for "
3341 "LatexType Command.\n"));
3343 command_name = style.latexname();
3345 tmps = style.latexparam();
3346 c_params = split(tmps, c_depth,'|');
3348 cmd_depth= atoi(c_depth.c_str());
3351 if(cmd_depth<command_base) {
3352 for(int j = command_depth;
3353 j >= command_base; --j)
3354 if(!command_stack[j].empty())
3355 sgmlCloseTag(ofs, j, command_stack[j]);
3356 command_depth= command_base= cmd_depth;
3358 else if(cmd_depth <= command_depth) {
3359 for(int j = command_depth;
3360 j >= cmd_depth; --j)
3362 if(!command_stack[j].empty())
3363 sgmlCloseTag(ofs, j, command_stack[j]);
3364 command_depth= cmd_depth;
3367 command_depth= cmd_depth;
3370 command_depth = command_base = cmd_depth;
3371 command_flag = true;
3373 command_stack[command_depth]= command_name;
3375 // treat label as a special case for
3376 // more WYSIWYM handling.
3377 if (par->GetChar(0) == LyXParagraph::META_INSET) {
3378 Inset * inset = par->GetInset(0);
3379 char lyx_code = inset->LyxCode();
3380 if (lyx_code == Inset::LABEL_CODE){
3381 command_name += " id=\"";
3382 command_name += (static_cast<InsetCommand *>(inset))->getContents();
3383 command_name += "\"";
3388 sgmlOpenTag(ofs, depth+command_depth, command_name);
3389 item_name = "title";
3390 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3393 case LATEX_ENVIRONMENT:
3394 case LATEX_ITEM_ENVIRONMENT:
3395 if (depth < par->depth) {
3397 environment_stack[depth].clear();
3400 if (environment_stack[depth] != style.latexname()) {
3401 environment_stack[depth] = style.latexname();
3402 environment_inner[depth] = "!-- --";
3403 sgmlOpenTag(ofs, depth + command_depth,
3404 environment_stack[depth]);
3406 if(environment_inner[depth] != "!-- --") {
3407 item_name= "listitem";
3409 command_depth + depth,
3411 if (environment_inner[depth] == "varlistentry")
3413 depth + command_depth,
3414 environment_inner[depth]);
3418 if(style.latextype == LATEX_ENVIRONMENT) {
3419 if(!style.latexparam().empty())
3420 sgmlOpenTag(ofs, depth + command_depth,
3421 style.latexparam());
3425 desc_on = (style.labeltype == LABEL_MANUAL);
3428 environment_inner[depth]= "varlistentry";
3430 environment_inner[depth]= "listitem";
3432 sgmlOpenTag(ofs, depth + 1 + command_depth,
3433 environment_inner[depth]);
3437 sgmlOpenTag(ofs, depth + 1 + command_depth,
3442 sgmlOpenTag(ofs, depth + 1 + command_depth,
3447 sgmlOpenTag(ofs, depth + command_depth,
3453 #ifdef USE_OSTREAM_ONLY
3455 SimpleDocBookOnePar(ofs, extra_par, par, desc_on,
3456 depth + 1 + command_depth);
3458 DocBookHandleFootnote(ofs, par,
3459 depth + 1 + command_depth);
3461 string tmp_par, extra_par;
3463 SimpleDocBookOnePar(tmp_par, extra_par, par, desc_on,
3464 depth + 1 + command_depth);
3468 DocBookHandleFootnote(ofs, par,
3469 depth + 1 + command_depth);
3472 while(par && par->IsDummy());
3475 /* write closing SGML tags */
3476 switch(style.latextype) {
3479 sgmlCloseTag(ofs, depth + command_depth, end_tag);
3481 case LATEX_ENVIRONMENT:
3482 if(!style.latexparam().empty())
3483 sgmlCloseTag(ofs, depth + command_depth,
3484 style.latexparam());
3486 case LATEX_ITEM_ENVIRONMENT:
3487 if(desc_on == 1) break;
3489 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3491 case LATEX_PARAGRAPH:
3492 if(style.latexname() != "dummy")
3493 sgmlCloseTag(ofs, depth + command_depth,
3497 sgmlCloseTag(ofs, depth + command_depth,
3504 for(; depth >= 0; --depth) {
3505 if(!environment_stack[depth].empty()) {
3506 if(environment_inner[depth] != "!-- --") {
3507 item_name= "listitem";
3508 sgmlCloseTag(ofs, command_depth + depth,
3510 if( environment_inner[depth] == "varlistentry")
3511 sgmlCloseTag(ofs, depth + command_depth,
3512 environment_inner[depth]);
3515 sgmlCloseTag(ofs, depth + command_depth,
3516 environment_stack[depth]);
3520 for(int j = command_depth; j >= command_base; --j)
3521 if(!command_stack[j].empty())
3522 sgmlCloseTag(ofs, j, command_stack[j]);
3525 sgmlCloseTag(ofs, 0, top_element);
3528 // How to check for successful close
3532 #ifdef USE_OSTREAM_ONLY
3533 void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
3534 LyXParagraph * par, int & desc_on,
3538 par->SimpleDocBookOneTablePar(os, extra, desc_on, depth);
3541 LyXFont font1, font2;
3544 LyXParagraph::size_type main_body;
3546 //string emph = "emphasis";
3547 bool emph_flag = false;
3548 int char_line_count = 0;
3550 LyXLayout const & style = textclasslist.Style(params.textclass,
3553 if (style.labeltype != LABEL_MANUAL)
3556 main_body = par->BeginningOfMainBody();
3558 // gets paragraph main font
3560 font1 = style.labelfont;
3564 char_line_count = depth;
3565 if(!style.free_spacing)
3566 for (j = 0; j < depth; ++j)
3569 // parsing main loop
3570 for (LyXParagraph::size_type i = 0;
3571 i < par->size(); ++i) {
3572 font2 = par->getFont(i);
3574 // handle <emphasis> tag
3575 if (font1.emph() != font2.emph() && i) {
3576 if (font2.emph() == LyXFont::ON) {
3580 os << "</emphasis>";
3585 c = par->GetChar(i);
3587 if (c == LyXParagraph::META_INSET) {
3588 inset = par->GetInset(i);
3591 inset->DocBook(ost);
3592 string tmp_out = ost.str().c_str();
3595 inset->DocBook(ost);
3597 char * ctmp = ost.str();
3598 string tmp_out(ctmp);
3602 // This code needs some explanation:
3603 // Two insets are treated specially
3604 // label if it is the first element in a command paragraph
3606 // graphics inside tables or figure floats can't go on
3607 // title (the equivalente in latex for this case is caption
3608 // and title should come first
3611 if(desc_on!= 3 || i!= 0) {
3612 if(!tmp_out.empty() && tmp_out[0] == '@') {
3614 extra += frontStrip(tmp_out, '@');
3616 os << frontStrip(tmp_out, '@');
3621 } else if (font2.latex() == LyXFont::ON) {
3622 // "TeX"-Mode on ==> SGML-Mode on.
3629 if (par->linuxDocConvertChar(c, sgml_string)
3630 && !style.free_spacing) { // in freespacing
3632 // non-breaking characters
3636 os << "\n</term><listitem><para>";
3650 /* needed if there is an optional argument but no contents */
3651 if (main_body > 0 && main_body == par->size()) {
3655 os << "</emphasis>";
3658 /* resets description flag correctly */
3661 /* <term> not closed... */
3668 void Buffer::SimpleDocBookOnePar(string & file, string & extra,
3669 LyXParagraph * par, int & desc_on,
3673 par->SimpleDocBookOneTablePar(file, extra, desc_on, depth);
3676 LyXFont font1, font2;
3679 LyXParagraph::size_type main_body;
3681 string emph= "emphasis";
3682 bool emph_flag= false;
3683 int char_line_count= 0;
3685 LyXLayout const & style = textclasslist.Style(params.textclass,
3688 if (style.labeltype != LABEL_MANUAL)
3691 main_body = par->BeginningOfMainBody();
3693 /* gets paragraph main font */
3695 font1 = style.labelfont;
3699 char_line_count = depth;
3700 if(!style.free_spacing)
3701 for (j = 0; j < depth; ++j)
3704 /* parsing main loop */
3705 for (LyXParagraph::size_type i = 0;
3706 i < par->size(); ++i) {
3707 font2 = par->getFont(i);
3709 /* handle <emphasis> tag */
3710 if (font1.emph() != font2.emph() && i) {
3711 if (font2.emph() == LyXFont::ON) {
3712 file += "<emphasis>";
3715 file += "</emphasis>";
3720 c = par->GetChar(i);
3722 if (c == LyXParagraph::META_INSET) {
3723 inset = par->GetInset(i);
3724 #ifdef USE_OSTREAM_ONLY
3727 inset->DocBook(ost);
3728 string tmp_out = ost.str().c_str();
3731 inset->DocBook(ost);
3733 char * ctmp = ost.str();
3734 string tmp_out(ctmp);
3739 inset->DocBook(tmp_out);
3742 // This code needs some explanation:
3743 // Two insets are treated specially
3744 // label if it is the first element in a command paragraph
3746 // graphics inside tables or figure floats can't go on
3747 // title (the equivalente in latex for this case is caption
3748 // and title should come first
3751 if(desc_on!= 3 || i!= 0) {
3752 if(!tmp_out.empty() && tmp_out[0] == '@') {
3754 extra += frontStrip(tmp_out, '@');
3756 file += frontStrip(tmp_out, '@');
3761 } else if (font2.latex() == LyXFont::ON) {
3762 // "TeX"-Mode on ==> SGML-Mode on.
3769 if (par->linuxDocConvertChar(c, sgml_string)
3770 && !style.free_spacing) { // in freespacing
3772 // non-breaking characters
3777 file += "</term><listitem><para>";
3785 file += sgml_string;
3791 /* needed if there is an optional argument but no contents */
3792 if (main_body > 0 && main_body == par->size()) {
3796 file += "</emphasis>";
3799 /* resets description flag correctly */
3802 /* <term> not closed... */
3811 int Buffer::runLaTeX()
3813 if (!users->text) return 0;
3817 // get LaTeX-Filename
3818 string name = getLatexName();
3820 string path = OnlyPath(filename);
3822 string org_path = path;
3823 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3827 Path p(path); // path to LaTeX file
3828 users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
3830 // Remove all error insets
3831 bool a = users->removeAutoInsets();
3833 // Always generate the LaTeX file
3834 makeLaTeXFile(name, org_path, false);
3837 // do the LaTex run(s)
3839 string latex_command = lyxrc->pdf_mode ?
3840 lyxrc->pdflatex_command : lyxrc->latex_command;
3841 LaTeX latex(latex_command, name, filepath);
3842 int res = latex.run(terr,
3843 users->owner()->getMiniBuffer()); // running latex
3845 // check return value from latex.run().
3846 if ((res & LaTeX::NO_LOGFILE)) {
3847 WriteAlert(_("LaTeX did not work!"),
3848 _("Missing log file:"), name);
3849 } else if ((res & LaTeX::ERRORS)) {
3850 users->owner()->getMiniBuffer()->Set(_("Done"));
3851 // Insert all errors as errors boxes
3852 users->insertErrors(terr);
3854 // Dvi should also be kept dirty if the latex run
3855 // ends up with errors. However it should be possible
3856 // to view a dirty dvi too.
3858 //no errors or any other things to think about so:
3859 users->owner()->getMiniBuffer()->Set(_("Done"));
3863 // if we removed error insets before we ran LaTeX or if we inserted
3864 // error insets after we ran LaTeX this must be run:
3865 if (a || (res & LaTeX::ERRORS)){
3868 users->updateScrollbar();
3872 return latex.getNumErrors();
3876 int Buffer::runLiterate()
3878 if (!users->text) return 0;
3882 // get LaTeX-Filename
3883 string name = getLatexName();
3884 // get Literate-Filename
3885 string lit_name = ChangeExtension (getLatexName(),
3886 lyxrc->literate_extension, true);
3888 string path = OnlyPath(filename);
3890 string org_path = path;
3891 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3895 Path p(path); // path to Literate file
3896 users->owner()->getMiniBuffer()->Set(_("Running Literate..."));
3898 // Remove all error insets
3899 bool a = users->removeAutoInsets();
3901 // generate the Literate file if necessary
3902 if (!isDviClean() || a) {
3903 makeLaTeXFile(lit_name, org_path, false);
3907 string latex_command = lyxrc->pdf_mode ?
3908 lyxrc->pdflatex_command : lyxrc->latex_command;
3909 Literate literate(latex_command, name, filepath,
3911 lyxrc->literate_command, lyxrc->literate_error_filter,
3912 lyxrc->build_command, lyxrc->build_error_filter);
3914 int res = literate.weave(terr, users->owner()->getMiniBuffer());
3916 // check return value from literate.weave().
3917 if ((res & Literate::NO_LOGFILE)) {
3918 WriteAlert(_("Literate command did not work!"),
3919 _("Missing log file:"), name);
3920 } else if ((res & Literate::ERRORS)) {
3921 users->owner()->getMiniBuffer()->Set(_("Done"));
3922 // Insert all errors as errors boxes
3923 users->insertErrors(terr);
3925 // Dvi should also be kept dirty if the latex run
3926 // ends up with errors. However it should be possible
3927 // to view a dirty dvi too.
3929 //no errors or any other things to think about so:
3930 users->owner()->getMiniBuffer()->Set(_("Done"));
3934 // if we removed error insets before we ran LaTeX or if we inserted
3935 // error insets after we ran LaTeX this must be run:
3936 if (a || (res & Literate::ERRORS)){
3939 users->updateScrollbar();
3943 return literate.getNumErrors();
3947 int Buffer::buildProgram()
3949 if (!users->text) return 0;
3953 // get LaTeX-Filename
3954 string name = getLatexName();
3955 // get Literate-Filename
3956 string lit_name = ChangeExtension(getLatexName(),
3957 lyxrc->literate_extension, true);
3959 string path = OnlyPath(filename);
3961 string org_path = path;
3962 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3966 Path p(path); // path to Literate file
3967 users->owner()->getMiniBuffer()->Set(_("Building Program..."));
3969 // Remove all error insets
3970 bool a = users->removeAutoInsets();
3972 // generate the LaTeX file if necessary
3973 if (!isNwClean() || a) {
3974 makeLaTeXFile(lit_name, org_path, false);
3978 string latex_command = lyxrc->pdf_mode ?
3979 lyxrc->pdflatex_command : lyxrc->latex_command;
3980 Literate literate(latex_command, name, filepath,
3982 lyxrc->literate_command, lyxrc->literate_error_filter,
3983 lyxrc->build_command, lyxrc->build_error_filter);
3985 int res = literate.build(terr, users->owner()->getMiniBuffer());
3987 // check return value from literate.build().
3988 if ((res & Literate::NO_LOGFILE)) {
3989 WriteAlert(_("Build did not work!"),
3990 _("Missing log file:"), name);
3991 } else if ((res & Literate::ERRORS)) {
3992 users->owner()->getMiniBuffer()->Set(_("Done"));
3993 // Insert all errors as errors boxes
3994 users->insertErrors(terr);
3996 // Literate files should also be kept dirty if the literate
3997 // command run ends up with errors.
3999 //no errors or any other things to think about so:
4000 users->owner()->getMiniBuffer()->Set(_("Done"));
4004 // if we removed error insets before we ran Literate/Build or if we inserted
4005 // error insets after we ran Literate/Build this must be run:
4006 if (a || (res & Literate::ERRORS)){
4009 users->updateScrollbar();
4013 return literate.getNumErrors();
4017 // This should be enabled when the Chktex class is implemented. (Asger)
4018 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
4019 // Other flags: -wall -v0 -x
4020 int Buffer::runChktex()
4022 if (!users->text) return 0;
4026 // get LaTeX-Filename
4027 string name = getLatexName();
4028 string path = OnlyPath(filename);
4030 string org_path = path;
4031 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
4035 Path p(path); // path to LaTeX file
4036 users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
4038 // Remove all error insets
4039 bool a = users->removeAutoInsets();
4041 // Generate the LaTeX file if neccessary
4042 if (!isDviClean() || a) {
4043 makeLaTeXFile(name, org_path, false);
4048 Chktex chktex(lyxrc->chktex_command, name, filepath);
4049 int res = chktex.run(terr); // run chktex
4052 WriteAlert(_("chktex did not work!"),
4053 _("Could not run with file:"), name);
4054 } else if (res > 0) {
4055 // Insert all errors as errors boxes
4056 users->insertErrors(terr);
4059 // if we removed error insets before we ran chktex or if we inserted
4060 // error insets after we ran chktex, this must be run:
4064 users->updateScrollbar();
4073 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
4075 LyXFont font1(LyXFont::ALL_INHERIT);
4078 LyXParagraph::size_type i;
4082 string fname1 = TmpFileName(string(), "RAT1");
4083 string fname2 = TmpFileName(string(), "RAT2");
4085 ofstream ofs(fname1.c_str());
4087 WriteAlert(_("LYX_ERROR:"),
4088 _("Cannot open temporary file:"), fname1);
4091 par->table->RoffEndOfCell(ofs, -1);
4092 for (i = 0; i < par->size(); ++i) {
4093 c = par->GetChar(i);
4094 if (par->table->IsContRow(cell)) {
4095 if (c == LyXParagraph::META_NEWLINE)
4099 font2 = par->GetFontSettings(i);
4100 if (font1.latex() != font2.latex()) {
4101 if (font2.latex() != LyXFont::OFF)
4105 case LyXParagraph::META_INSET:
4106 if ((inset = par->GetInset(i))) {
4109 stringstresm ss(ios::in | ios::out);
4110 inset->Latex(ss, -1);
4122 inset->Latex(ss, -1);
4135 fstream fs(fname2.c_str(), ios::in|ios::out);
4137 WriteAlert(_("LYX_ERROR:"),
4138 _("Cannot open temporary file:"), fname2);
4140 remove(fname1.c_str());
4143 inset->Latex(fs, -1);
4157 case LyXParagraph::META_NEWLINE:
4158 if (par->table->CellHasContRow(cell)>= 0)
4159 par->RoffContTableRows(ofs, i+1, cell);
4160 par->table->RoffEndOfCell(ofs, cell);
4163 case LyXParagraph::META_HFILL:
4166 case LyXParagraph::META_PROTECTED_SEPARATOR:
4177 << "RoffAsciiTable:"
4178 " NULL char in structure." << endl;
4182 par->table->RoffEndOfCell(ofs, cell);
4184 string cmd = lyxrc->ascii_roff_command + " >" + fname2;
4185 cmd = subst(cmd, "$$FName", fname1);
4186 Systemcalls one(Systemcalls::System, cmd);
4187 if (!(lyxerr.debugging(Debug::ROFF))) {
4188 remove(fname1.c_str());
4190 ifstream ifs(fname2.c_str());
4192 WriteFSAlert(_("Error! Can't open temporary file:"), fname2);
4195 // now output the produced file
4199 WriteAlert(_("Error!"),
4200 _("Error executing *roff command on table"));
4201 // overread leading blank lines
4202 while(!ifs && (c == '\n'))
4205 for(j = 0; j < par->depth; ++j)
4207 while(!ifs && (c != '\n')) {
4212 // overread trailing blank lines
4213 while(!ifs && (c == '\n'))
4217 remove(fname2.c_str());
4222 /// changed Heinrich Bauer, 23/03/98
4223 bool Buffer::isDviClean() const
4225 if (lyxrc->use_tempdir)
4226 return dvi_clean_tmpd;
4228 return dvi_clean_orgd;
4232 /// changed Heinrich Bauer, 23/03/98
4233 void Buffer::markDviClean()
4235 if (lyxrc->use_tempdir)
4236 dvi_clean_tmpd = true;
4238 dvi_clean_orgd = true;
4242 /// changed Heinrich Bauer, 23/03/98
4243 void Buffer::markDviDirty()
4245 if (lyxrc->use_tempdir)
4246 dvi_clean_tmpd = false;
4248 dvi_clean_orgd = false;
4252 void Buffer::validate(LaTeXFeatures & features) const
4254 LyXParagraph * par = paragraph;
4255 LyXTextClass const & tclass =
4256 textclasslist.TextClass(params.textclass);
4258 // AMS Style is at document level
4260 features.amsstyle = (params.use_amsmath ||
4261 tclass.provides(LyXTextClass::amsmath));
4264 // We don't use "lyxerr.debug" because of speed. (Asger)
4265 if (lyxerr.debugging(Debug::LATEX))
4266 lyxerr << "Paragraph: " << par << endl;
4268 // Now just follow the list of paragraphs and run
4269 // validate on each of them.
4270 par->validate(features);
4272 // and then the next paragraph
4276 // the bullet shapes are buffer level not paragraph level
4277 // so they are tested here
4278 for (int i = 0; i < 4; ++i) {
4279 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
4280 int font = params.user_defined_bullets[i].getFont();
4283 .user_defined_bullets[i]
4290 features.latexsym = true;
4294 features.amssymb = true;
4296 else if ((font >= 2 && font <= 5)) {
4297 features.pifont = true;
4302 if (lyxerr.debugging(Debug::LATEX)) {
4303 features.showStruct(params);
4308 void Buffer::setPaperStuff()
4310 params.papersize = BufferParams::PAPER_DEFAULT;
4311 char c1 = params.paperpackage;
4312 if (c1 == BufferParams::PACKAGE_NONE) {
4313 char c2 = params.papersize2;
4314 if (c2 == BufferParams::VM_PAPER_USLETTER)
4315 params.papersize = BufferParams::PAPER_USLETTER;
4316 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
4317 params.papersize = BufferParams::PAPER_LEGALPAPER;
4318 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
4319 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
4320 else if (c2 == BufferParams::VM_PAPER_A3)
4321 params.papersize = BufferParams::PAPER_A3PAPER;
4322 else if (c2 == BufferParams::VM_PAPER_A4)
4323 params.papersize = BufferParams::PAPER_A4PAPER;
4324 else if (c2 == BufferParams::VM_PAPER_A5)
4325 params.papersize = BufferParams::PAPER_A5PAPER;
4326 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
4327 (c2 == BufferParams::VM_PAPER_B5))
4328 params.papersize = BufferParams::PAPER_B5PAPER;
4329 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
4330 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
4331 params.papersize = BufferParams::PAPER_A4PAPER;
4335 // This function should be in Buffer because it's a buffer's property (ale)
4336 string Buffer::getIncludeonlyList(char delim)
4339 LyXParagraph * par = paragraph;
4340 LyXParagraph::size_type pos;
4344 while ((inset = par->ReturnNextInsetPointer(pos))){
4345 if (inset->LyxCode() == Inset::INCLUDE_CODE) {
4346 InsetInclude * insetinc =
4347 static_cast<InsetInclude*>(inset);
4348 if (insetinc->isInclude()
4349 && insetinc->isNoLoad()) {
4352 lst += ChangeExtension(insetinc->getContents(), string(), true);
4359 lyxerr.debug() << "Includeonly(" << lst << ')' << endl;
4364 // This is also a buffer property (ale)
4365 string Buffer::getReferenceList(char delim)
4367 /// if this is a child document and the parent is already loaded
4368 /// Use the parent's list instead [ale990407]
4369 if (!params.parentname.empty()
4370 && bufferlist.exists(params.parentname)) {
4371 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4373 return tmp->getReferenceList(delim);
4376 LyXParagraph * par = paragraph;
4377 LyXParagraph::size_type pos;
4382 while ((inset = par->ReturnNextInsetPointer(pos))){
4383 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
4386 lst += inset->getLabel(i);
4392 lyxerr.debug() << "References(" << lst << ")" << endl;
4397 // This is also a buffer property (ale)
4398 string Buffer::getBibkeyList(char delim)
4400 /// if this is a child document and the parent is already loaded
4401 /// Use the parent's list instead [ale990412]
4402 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
4403 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4405 return tmp->getBibkeyList(delim);
4409 LyXParagraph * par = paragraph;
4412 if (!bibkeys.empty())
4414 bibkeys += par->bibkey->getContents();
4419 // Might be either using bibtex or a child has bibliography
4420 if (bibkeys.empty()) {
4424 LyXParagraph::size_type pos = -1;
4426 // Search for Bibtex or Include inset
4427 while ((inset = par->ReturnNextInsetPointer(pos))) {
4428 if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
4429 if (!bibkeys.empty())
4431 bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
4432 } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
4433 string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
4435 if (!bibkeys.empty())
4446 lyxerr.debug() << "Bibkeys(" << bibkeys << ")" << endl;
4451 bool Buffer::isDepClean(string const & name) const
4453 DEPCLEAN * item = dep_clean;
4454 while (item && item->master != name)
4456 if (!item) return true;
4461 void Buffer::markDepClean(string const & name)
4464 dep_clean = new DEPCLEAN;
4465 dep_clean->clean = true;
4466 dep_clean->master = name;
4467 dep_clean->next = 0;
4469 DEPCLEAN* item = dep_clean;
4470 while (item && item->master != name)
4475 item = new DEPCLEAN;
4477 item->master = name;
4484 void Buffer::Dispatch(string const & command)
4486 // Split command string into command and argument
4487 string cmd, line = frontStrip(command);
4488 string arg = strip(frontStrip(split(line, cmd, ' ')));
4490 Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
4494 void Buffer::Dispatch(int action, string const & argument)
4498 MenuExport(this, argument);
4502 lyxerr << "A truly unknown func!" << endl;