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 ftnote.str(string());
2011 delete [] ftnote.str();
2016 #ifndef HAVE_SSTREAM
2018 // I hate strstreams
2019 tmpholder = ftnote.str();
2023 #ifndef HAVE_SSTREAM
2024 delete [] tmpholder;
2026 // It might be that we only have a title in this document
2027 if (was_title && !already_title) {
2028 ofs << "\\maketitle\n";
2033 ofs << "\\end{document}\n";
2036 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2038 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2042 // Just to be sure. (Asger)
2045 // tex_code_break_column's value is used to decide
2046 // if we are in batchmode or not (within mathed_write()
2047 // in math_write.C) so we must set it to a non-zero
2048 // value when we leave otherwise we save incorrect .lyx files.
2049 tex_code_break_column = lyxrc->ascii_linelen;
2053 lyxerr << "File was not closed properly." << endl;
2056 lyxerr.debug() << "Finished making latex file." << endl;
2059 void Buffer::makeLaTeXFile(string const & fname,
2060 string const & original_path,
2061 bool nice, bool only_body)
2063 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
2065 niceFile = nice; // this will be used by Insetincludes.
2067 tex_code_break_column = lyxrc->ascii_linelen;
2069 LyXTextClass const & tclass =
2070 textclasslist.TextClass(params.textclass);
2072 ofstream ofs(fname.c_str());
2074 WriteFSAlert(_("Error: Cannot open file: "), fname);
2078 // validate the buffer.
2079 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
2080 LaTeXFeatures features(tclass.numLayouts());
2082 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
2085 // The starting paragraph of the coming rows is the
2086 // first paragraph of the document. (Asger)
2087 texrow.start(paragraph, 0);
2091 if (!only_body && nice) {
2092 LFile += "%% " LYX_DOCVERSION " created this file. "
2093 "For more info, see http://www.lyx.org/.\n";
2094 LFile += "%% Do not edit unless you really know what you are doing.\n";
2098 lyxerr.debug() << "lyx header finished" << endl;
2099 // There are a few differences between nice LaTeX and usual files:
2100 // usual is \batchmode and has a
2101 // special input@path to allow the including of figures
2102 // with either \input or \includegraphics (what figinsets do).
2103 // batchmode is not set if there is a tex_code_break_column.
2104 // In this case somebody is interested in the generated LaTeX,
2105 // so this is OK. input@path is set when the actual parameter
2106 // original_path is set. This is done for usual tex-file, but not
2107 // for nice-latex-file. (Matthias 250696)
2110 // code for usual, NOT nice-latex-file
2111 LFile += "\\batchmode\n"; // changed
2112 // from \nonstopmode
2115 if (!original_path.empty()) {
2116 LFile += "\\makeatletter\n";
2118 LFile += "\\def\\input@path{{" + original_path
2121 LFile += "\\makeatother\n";
2125 LFile += "\\documentclass";
2127 string options; // the document class options.
2129 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
2130 // only write if existing in list (and not default)
2131 options += params.fontsize;
2136 if (!params.use_geometry &&
2137 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
2138 switch (params.papersize) {
2139 case BufferParams::PAPER_A4PAPER:
2140 options += "a4paper,";
2142 case BufferParams::PAPER_USLETTER:
2143 options += "letterpaper,";
2145 case BufferParams::PAPER_A5PAPER:
2146 options += "a5paper,";
2148 case BufferParams::PAPER_B5PAPER:
2149 options += "b5paper,";
2151 case BufferParams::PAPER_EXECUTIVEPAPER:
2152 options += "executivepaper,";
2154 case BufferParams::PAPER_LEGALPAPER:
2155 options += "legalpaper,";
2161 if (params.sides != tclass.sides()) {
2162 switch (params.sides) {
2163 case LyXTextClass::OneSide:
2164 options += "oneside,";
2166 case LyXTextClass::TwoSides:
2167 options += "twoside,";
2174 if (params.columns != tclass.columns()) {
2175 if (params.columns == 2)
2176 options += "twocolumn,";
2178 options += "onecolumn,";
2181 if (!params.use_geometry
2182 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2183 options += "landscape,";
2185 // language should be a parameter to \documentclass
2186 if (params.language != "default") {
2187 if (params.language == "hebrew")
2188 options += "english,";
2189 else if (lyxrc->rtl_support)
2190 options += "hebrew,";
2191 options += params.language + ',';
2192 } else if (lyxrc->rtl_support)
2193 options += "hebrew,english,";
2195 // the user-defined options
2196 if (!params.options.empty()) {
2197 options += params.options + ',';
2200 if (!options.empty()){
2201 options = strip(options, ',');
2208 LFile += textclasslist.LatexnameOfClass(params.textclass);
2211 // end of \documentclass defs
2213 // font selection must be done before loading fontenc.sty
2214 if (params.fonts != "default") {
2215 LFile += "\\usepackage{" + params.fonts + "}\n";
2218 // this one is not per buffer
2219 if (lyxrc->fontenc != "default") {
2220 LFile += "\\usepackage[" + lyxrc->fontenc
2224 if (params.inputenc != "default") {
2225 LFile += "\\usepackage[" + params.inputenc
2230 /* at the very beginning the text parameters */
2231 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
2232 switch (params.paperpackage) {
2233 case BufferParams::PACKAGE_A4:
2234 LFile += "\\usepackage{a4}\n";
2237 case BufferParams::PACKAGE_A4WIDE:
2238 LFile += "\\usepackage{a4wide}\n";
2241 case BufferParams::PACKAGE_WIDEMARGINSA4:
2242 LFile += "\\usepackage[widemargins]{a4}\n";
2247 if (params.use_geometry) {
2248 LFile += "\\usepackage{geometry}\n";
2250 LFile += "\\geometry{verbose";
2251 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
2252 LFile += ",landscape";
2253 switch (params.papersize2) {
2254 case BufferParams::VM_PAPER_CUSTOM:
2255 if (!params.paperwidth.empty())
2256 LFile += ",paperwidth="
2257 + params.paperwidth;
2258 if (!params.paperheight.empty())
2259 LFile += ",paperheight="
2260 + params.paperheight;
2262 case BufferParams::VM_PAPER_USLETTER:
2263 LFile += ",letterpaper";
2265 case BufferParams::VM_PAPER_USLEGAL:
2266 LFile += ",legalpaper";
2268 case BufferParams::VM_PAPER_USEXECUTIVE:
2269 LFile += ",executivepaper";
2271 case BufferParams::VM_PAPER_A3:
2272 LFile += ",a3paper";
2274 case BufferParams::VM_PAPER_A4:
2275 LFile += ",a4paper";
2277 case BufferParams::VM_PAPER_A5:
2278 LFile += ",a5paper";
2280 case BufferParams::VM_PAPER_B3:
2281 LFile += ",b3paper";
2283 case BufferParams::VM_PAPER_B4:
2284 LFile += ",b4paper";
2286 case BufferParams::VM_PAPER_B5:
2287 LFile += ",b5paper";
2290 // default papersize ie BufferParams::VM_PAPER_DEFAULT
2291 switch (lyxrc->default_papersize) {
2292 case BufferParams::PAPER_DEFAULT: // keep compiler happy
2293 case BufferParams::PAPER_USLETTER:
2294 LFile += ",letterpaper";
2296 case BufferParams::PAPER_LEGALPAPER:
2297 LFile += ",legalpaper";
2299 case BufferParams::PAPER_EXECUTIVEPAPER:
2300 LFile += ",executivepaper";
2302 case BufferParams::PAPER_A3PAPER:
2303 LFile += ",a3paper";
2305 case BufferParams::PAPER_A4PAPER:
2306 LFile += ",a4paper";
2308 case BufferParams::PAPER_A5PAPER:
2309 LFile += ",a5paper";
2311 case BufferParams::PAPER_B5PAPER:
2312 LFile += ",b5paper";
2316 if (!params.topmargin.empty())
2317 LFile += ",tmargin=" + params.topmargin;
2318 if (!params.bottommargin.empty())
2319 LFile += ",bmargin=" + params.bottommargin;
2320 if (!params.leftmargin.empty())
2321 LFile += ",lmargin=" + params.leftmargin;
2322 if (!params.rightmargin.empty())
2323 LFile += ",rmargin=" + params.rightmargin;
2324 if (!params.headheight.empty())
2325 LFile += ",headheight=" + params.headheight;
2326 if (!params.headsep.empty())
2327 LFile += ",headsep=" + params.headsep;
2328 if (!params.footskip.empty())
2329 LFile += ",footskip=" + params.footskip;
2333 if (params.use_amsmath
2334 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
2335 LFile += "\\usepackage{amsmath}\n";
2338 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
2339 if (params.pagestyle == "fancy") {
2340 LFile += "\\usepackage{fancyhdr}\n";
2343 LFile += "\\pagestyle{" + params.pagestyle + "}\n";
2347 // We try to load babel late, in case it interferes
2348 // with other packages.
2349 if (params.language != "default" || lyxrc->rtl_support ) {
2350 LFile += "\\usepackage{babel}\n";
2354 if (params.secnumdepth != tclass.secnumdepth()) {
2355 LFile += "\\setcounter{secnumdepth}{";
2356 LFile += tostr(params.secnumdepth);
2360 if (params.tocdepth != tclass.tocdepth()) {
2361 LFile += "\\setcounter{tocdepth}{";
2362 LFile += tostr(params.tocdepth);
2367 if (params.paragraph_separation) {
2368 switch (params.defskip.kind()) {
2369 case VSpace::SMALLSKIP:
2370 LFile += "\\setlength\\parskip{\\smallskipamount}\n";
2372 case VSpace::MEDSKIP:
2373 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2375 case VSpace::BIGSKIP:
2376 LFile += "\\setlength\\parskip{\\bigskipamount}\n";
2378 case VSpace::LENGTH:
2379 LFile += "\\setlength\\parskip{"
2380 + params.defskip.length().asLatexString()
2383 default: // should never happen // Then delete it.
2384 LFile += "\\setlength\\parskip{\\medskipamount}\n";
2389 LFile += "\\setlength\\parindent{0pt}\n";
2393 // Write out what we've generated so far...and reset LFile
2397 // Now insert the LyX specific LaTeX commands...
2398 string preamble, tmppreamble;
2400 // The optional packages;
2401 preamble = features.getPackages(params);
2403 // this might be useful...
2404 preamble += "\n\\makeatletter\n\n";
2406 // Some macros LyX will need
2407 tmppreamble = features.getMacros(params);
2409 if (!tmppreamble.empty()) {
2410 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2411 "LyX specific LaTeX commands.\n"
2412 + tmppreamble + '\n';
2415 // the text class specific preamble
2416 tmppreamble = features.getTClassPreamble(params);
2417 if (!tmppreamble.empty()) {
2418 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2419 "Textclass specific LaTeX commands.\n"
2420 + tmppreamble + '\n';
2423 /* the user-defined preamble */
2424 if (!params.preamble.empty()) {
2425 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
2426 "User specified LaTeX commands.\n"
2427 + params.preamble + '\n';
2430 preamble += "\\makeatother\n\n";
2432 // Itemize bullet settings need to be last in case the user
2433 // defines their own bullets that use a package included
2434 // in the user-defined preamble -- ARRae
2435 // Actually it has to be done much later than that
2436 // since some packages like frenchb make modifications
2437 // at \begin{document} time -- JMarc
2439 for (int i = 0; i < 4; ++i) {
2440 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
2441 if (bullets_def.empty())
2442 bullets_def="\\AtBeginDocument{\n";
2443 bullets_def += " \\renewcommand{\\labelitemi";
2445 // `i' is one less than the item to modify
2452 bullets_def += "ii";
2458 bullets_def += "}{" +
2459 params.user_defined_bullets[i].getText()
2464 if (!bullets_def.empty())
2465 preamble += bullets_def + "}\n\n";
2467 for (int j = countChar(preamble, '\n'); j-- ;) {
2474 LFile += "\\begin{document}\n\n";
2478 lyxerr.debug() << "preamble finished, now the body." << endl;
2480 bool was_title = false;
2481 bool already_title = false;
2487 LyXParagraph * par = paragraph;
2493 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
2495 LyXLayout const & layout =
2496 textclasslist.Style(params.textclass,
2499 if (layout.intitle) {
2500 if (already_title) {
2501 lyxerr <<"Error in MakeLatexFile: You"
2502 " should not mix title layouts"
2503 " with normal ones." << endl;
2506 } else if (was_title && !already_title) {
2507 LFile += "\\maketitle\n";
2509 already_title = true;
2512 // We are at depth 0 so we can just use
2513 // ordinary \footnote{} generation
2514 // flag this with ftcount
2516 if (layout.isEnvironment()
2517 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
2518 par = par->TeXEnvironment(LFile, texrow,
2519 ftnote, ft_texrow, ftcount);
2521 par = par->TeXOnePar(LFile, texrow,
2522 ftnote, ft_texrow, ftcount);
2525 // Write out what we've generated...and reset LFile
2528 LFile += "\\addtocounter{footnote}{-";
2529 LFile += tostr(ftcount - 1);
2533 texrow += ft_texrow;
2538 if (loop_count == 2) {
2539 // fwrite()ing every second time through the loop
2540 // gains a few extra % of speed; going higher than
2541 // 2 will slow things down again. I'll look at
2542 // LFile.length() in a future revision. ARRae
2549 // It might be that we only have a title in this document
2550 if (was_title && !already_title) {
2551 LFile += "\\maketitle\n";
2556 LFile += "\\end{document}\n";
2559 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2561 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2565 // Just to be sure. (Asger)
2568 // Write out what we've generated...and reset LFile
2572 // tex_code_break_column's value is used to decide
2573 // if we are in batchmode or not (within mathed_write()
2574 // in math_write.C) so we must set it to a non-zero
2575 // value when we leave otherwise we save incorrect .lyx files.
2576 tex_code_break_column = lyxrc->ascii_linelen;
2578 // How to check if the close went ok when using fstreams
2580 lyxerr.debug() << "Finished making latex file." << endl;
2585 bool Buffer::isLatex() const
2587 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2591 bool Buffer::isLinuxDoc() const
2593 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2597 bool Buffer::isLiterate() const
2599 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2603 bool Buffer::isDocBook() const
2605 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2609 bool Buffer::isSGML() const
2611 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2612 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2616 void Buffer::sgmlOpenTag(ostream & os, int depth,
2617 string const & latexname) const
2619 os << string(depth, ' ') << "<" << latexname << ">\n";
2623 void Buffer::sgmlCloseTag(ostream & os, int depth,
2624 string const & latexname) const
2626 os << string(depth, ' ') << "</" << latexname << ">\n";
2630 void Buffer::makeLinuxDocFile(string const & fname, int column)
2632 LyXParagraph * par = paragraph;
2634 string top_element = textclasslist.LatexnameOfClass(params.textclass);
2635 string environment_stack[10];
2638 int depth = 0; /* paragraph depth */
2640 ofstream ofs(fname.c_str());
2643 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2647 tex_code_break_column = column;
2650 if (params.preamble.empty()) {
2651 ofs << "<!doctype linuxdoc system>\n\n";
2654 ofs << "<!doctype linuxdoc system \n [ "
2655 << params.preamble << " \n]>\n\n";
2658 ofs << "<!-- " << LYX_DOCVERSION
2659 << " created this file. For more info see http://www.lyx.org/"
2662 if(params.options.empty())
2663 sgmlOpenTag(ofs, 0, top_element);
2665 string top = top_element;
2667 top += params.options;
2668 sgmlOpenTag(ofs, 0, top);
2672 int desc_on = 0; /* description mode*/
2673 LyXLayout const & style =
2674 textclasslist.Style(users->buffer()->params.textclass,
2676 par->AutoDeleteInsets();
2678 /* treat <toc> as a special case for compatibility with old code */
2679 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2680 Inset * inset = par->GetInset(0);
2681 char lyx_code = inset->LyxCode();
2682 if (lyx_code == Inset::TOC_CODE){
2683 string temp = "toc";
2684 sgmlOpenTag(ofs, depth, temp);
2687 linuxDocHandleFootnote(ofs, par, depth);
2692 // environment tag closing
2693 for( ; depth > par->depth; --depth) {
2694 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2695 environment_stack[depth].clear();
2698 // write opening SGML tags
2699 switch(style.latextype) {
2700 case LATEX_PARAGRAPH:
2701 if(depth == par->depth
2702 && !environment_stack[depth].empty()) {
2703 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2704 environment_stack[depth].clear();
2710 sgmlOpenTag(ofs, depth, style.latexname());
2715 LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
2717 if (!environment_stack[depth].empty()){
2718 sgmlCloseTag(ofs, depth,
2719 environment_stack[depth]);
2723 environment_stack[depth].clear();
2724 sgmlOpenTag(ofs, depth, style.latexname());
2727 case LATEX_ENVIRONMENT:
2728 case LATEX_ITEM_ENVIRONMENT:
2729 if(depth == par->depth
2730 && environment_stack[depth] != style.latexname()
2731 && !environment_stack[depth].empty()) {
2733 sgmlCloseTag(ofs, depth,
2734 environment_stack[depth]);
2735 environment_stack[depth].clear();
2737 if (depth < par->depth) {
2739 environment_stack[depth].clear();
2741 if (environment_stack[depth] != style.latexname()) {
2744 sgmlOpenTag(ofs, depth, temp);
2746 environment_stack[depth] = style.latexname();
2747 sgmlOpenTag(ofs, depth, environment_stack[depth]);
2749 if(style.latextype == LATEX_ENVIRONMENT) break;
2751 desc_on = (style.labeltype == LABEL_MANUAL);
2758 sgmlOpenTag(ofs, depth + 1, item_name);
2761 sgmlOpenTag(ofs, depth, style.latexname());
2766 SimpleLinuxDocOnePar(ofs, par, desc_on, depth);
2769 linuxDocHandleFootnote(ofs, par, depth);
2771 while(par && par->IsDummy());
2774 /* write closing SGML tags */
2775 switch(style.latextype) {
2777 case LATEX_ENVIRONMENT:
2778 case LATEX_ITEM_ENVIRONMENT:
2781 sgmlCloseTag(ofs, depth, style.latexname());
2786 /* Close open tags */
2787 for(; depth > 0; --depth)
2788 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2790 if(!environment_stack[depth].empty())
2791 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2794 sgmlCloseTag(ofs, 0, top_element);
2797 // How to check for successful close
2801 void Buffer::linuxDocHandleFootnote(ostream & os, LyXParagraph * & par,
2804 string tag = "footnote";
2806 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2807 sgmlOpenTag(os, depth + 1, tag);
2808 SimpleLinuxDocOnePar(os, par, 0, depth + 1);
2809 sgmlCloseTag(os, depth + 1, tag);
2815 void Buffer::DocBookHandleCaption(ostream & os, string & inner_tag,
2816 int const depth, int desc_on,
2817 LyXParagraph * & par)
2819 LyXParagraph * tpar = par;
2820 while (tpar && (tpar->footnoteflag != LyXParagraph::NO_FOOTNOTE) &&
2821 (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2825 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2826 "Caption").second) {
2827 sgmlOpenTag(os, depth + 1, inner_tag);
2828 #ifdef USE_OSTREAM_ONLY
2830 SimpleDocBookOnePar(os, extra_par, tpar,
2831 desc_on, depth + 2);
2833 string tmp_par, extra_par;
2834 SimpleDocBookOnePar(tmp_par, extra_par, tpar,
2835 desc_on, depth + 2);
2836 tmp_par = strip(tmp_par);
2837 tmp_par = frontStrip(tmp_par);
2840 sgmlCloseTag(os, depth+1, inner_tag);
2841 if(!extra_par.empty())
2847 void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
2850 string tag, inner_tag;
2851 string tmp_par, extra_par;
2852 bool inner_span = false;
2855 // This is not how I like to see enums. They should not be anonymous
2856 // and variables of its type should not be declared right after the
2857 // last brace. (Lgb)
2865 SOME_ENUM last = NO_ONE;
2866 SOME_ENUM present = FOOTNOTE_LIKE;
2868 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2869 if(last == present) {
2871 if(!tmp_par.empty()) {
2874 sgmlCloseTag(os, depth+1, inner_tag);
2875 sgmlOpenTag(os, depth+1, inner_tag);
2882 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1,
2884 if(!extra_par.empty()) os << extra_par;
2885 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2888 switch (par->footnotekind) {
2889 case LyXParagraph::FOOTNOTE:
2890 case LyXParagraph::ALGORITHM:
2893 present = FOOTNOTE_LIKE;
2896 case LyXParagraph::MARGIN:
2899 present = MARGIN_LIKE;
2902 case LyXParagraph::FIG:
2903 case LyXParagraph::WIDE_FIG:
2905 inner_tag = "title";
2909 case LyXParagraph::TAB:
2910 case LyXParagraph::WIDE_TAB:
2912 inner_tag = "title";
2917 sgmlOpenTag(os, depth, tag);
2918 if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
2919 DocBookHandleCaption(os, inner_tag, depth,
2923 sgmlOpenTag(os, depth + 1, inner_tag);
2926 // ignore all caption here, we processed them above!!!
2927 if (par->layout != textclasslist.NumberOfLayout(params.textclass,
2928 "Caption").second) {
2929 #ifdef USE_OSTREAM_ONLY
2935 SimpleDocBookOnePar(ost, extra_par, par,
2936 desc_on, depth + 2);
2938 tmp_par += ost.str().c_str();
2941 char * ctmp = ost.str();
2948 tmp_par = frontStrip(strip(tmp_par));
2954 if(!inner_tag.empty()) sgmlCloseTag(os, depth + 1, inner_tag);
2955 if(!extra_par.empty()) os << extra_par;
2956 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2960 /* push a tag in a style stack */
2961 void Buffer::push_tag(ostream & os, char const * tag,
2962 int & pos, char stack[5][3])
2964 /* pop all previous tags */
2965 for (int j = pos; j >= 0; --j)
2966 os << "</" << stack[j] << ">";
2969 sprintf(stack[++pos], "%s", tag);
2972 for (int i = 0; i <= pos; ++i)
2973 os << "<" << stack[i] << ">";
2976 void Buffer::pop_tag(ostream & os, char const * tag,
2977 int & pos, char stack[5][3])
2981 // pop all tags till specified one
2982 for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
2983 os << "</" << stack[j] << ">";
2986 os << "</" << tag << ">";
2988 // push all tags, but the specified one
2989 for (j = j + 1; j <= pos; ++j) {
2990 os << "<" << stack[j] << ">";
2991 strcpy(stack[j-1], stack[j]);
2997 /* handle internal paragraph parsing -- layout already processed */
2999 // checks, if newcol chars should be put into this line
3000 // writes newline, if necessary.
3002 void linux_doc_line_break(ostream & os, unsigned int & colcount,
3003 const unsigned int newcol)
3006 if (colcount > lyxrc->ascii_linelen) {
3008 colcount = newcol; // assume write after this call
3013 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
3014 int desc_on, int const /*depth*/)
3016 LyXFont font1, font2;
3019 LyXParagraph::size_type main_body;
3021 LyXLayout const & style = textclasslist.Style(params.textclass,
3024 char family_type = 0; // family font flag
3025 bool is_bold = false; // series font flag
3026 char shape_type = 0; // shape font flag
3027 bool is_em = false; // emphasis (italic) font flag
3029 int stack_num = -1; // style stack position
3030 char stack[5][3]; // style stack
3031 unsigned int char_line_count = 5; // Heuristic choice ;-)
3033 if (style.labeltype != LABEL_MANUAL)
3036 main_body = par->BeginningOfMainBody();
3038 /* gets paragraph main font */
3040 font1 = style.labelfont;
3045 /* parsing main loop */
3046 for (LyXParagraph::size_type i = 0;
3047 i < par->size(); ++i) {
3049 /* handle quote tag */
3050 if (i == main_body && !par->IsDummy()) {
3055 font2 = par->getFont(i);
3057 if (font1.family() != font2.family()) {
3058 switch(family_type) {
3060 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3061 push_tag(os, "tt", stack_num, stack);
3064 else if (font2.family() == LyXFont::SANS_FAMILY) {
3065 push_tag(os, "sf", stack_num, stack);
3070 pop_tag(os, "tt", stack_num, stack);
3071 if (font2.family() == LyXFont::SANS_FAMILY) {
3072 push_tag(os, "sf", stack_num, stack);
3080 pop_tag(os, "sf", stack_num, stack);
3081 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
3082 push_tag(os, "tt", stack_num, stack);
3091 /* handle bold face */
3092 if (font1.series() != font2.series()) {
3093 if (font2.series() == LyXFont::BOLD_SERIES) {
3094 push_tag(os, "bf", stack_num, stack);
3098 pop_tag(os, "bf", stack_num, stack);
3103 /* handle italic and slanted fonts */
3104 if (font1.shape() != font2.shape()) {
3105 switch(shape_type) {
3107 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3108 push_tag(os, "it", stack_num, stack);
3111 else if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3112 push_tag(os, "sl", stack_num, stack);
3117 pop_tag(os, "it", stack_num, stack);
3118 if (font2.shape() == LyXFont::SLANTED_SHAPE) {
3119 push_tag(os, "sl", stack_num, stack);
3127 pop_tag(os, "sl", stack_num, stack);
3128 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
3129 push_tag(os, "it", stack_num, stack);
3137 /* handle <em> tag */
3138 if (font1.emph() != font2.emph()) {
3139 if (font2.emph() == LyXFont::ON) {
3140 push_tag(os, "em", stack_num, stack);
3143 pop_tag(os, "em", stack_num, stack);
3148 c = par->GetChar(i);
3150 if (font2.latex() == LyXFont::ON) {
3151 // "TeX"-Mode on == > SGML-Mode on.
3153 os << c; // see LaTeX-Generation...
3155 } else if (c == LyXParagraph::META_INSET) {
3156 inset = par->GetInset(i);
3157 #ifdef USE_OSTREAM_ONLY
3158 inset->Linuxdoc(os);
3161 inset->Linuxdoc(tmp_out);
3167 if (par->linuxDocConvertChar(c, sgml_string)
3168 && !style.free_spacing) { // in freespacing
3170 // non-breaking characters
3174 linux_doc_line_break(os, char_line_count, 6);
3179 linux_doc_line_break(os, char_line_count, 1);
3185 char_line_count += sgml_string.length();
3191 /* needed if there is an optional argument but no contents */
3192 if (main_body > 0 && main_body == par->size()) {
3196 /* pop all defined Styles */
3197 for (j = stack_num; j >= 0; --j) {
3198 linux_doc_line_break(os,
3200 3 + strlen(stack[j]));
3201 os << "</" << stack[j] << ">";
3204 /* resets description flag correctly */
3207 /* <tag> not closed... */
3208 linux_doc_line_break(os, char_line_count, 6);
3212 /* fprintf(file, "</p>");*/
3218 /* print an error message */
3219 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
3220 char const * message)
3222 InsetError * new_inset;
3224 /* insert an error marker in text */
3225 new_inset = new InsetError(message);
3226 par->InsertChar(pos, LyXParagraph::META_INSET);
3227 par->InsertInset(pos, new_inset);
3230 // This constant defines the maximum number of
3231 // environment layouts that can be nesteded.
3232 // The same applies for command layouts.
3233 // These values should be more than enough.
3234 // José Matos (1999/07/22)
3236 enum { MAX_NEST_LEVEL = 25};
3238 void Buffer::makeDocBookFile(string const & fname, int column)
3240 LyXParagraph * par = paragraph;
3242 string top_element= textclasslist.LatexnameOfClass(params.textclass);
3243 string environment_stack[MAX_NEST_LEVEL];
3244 string environment_inner[MAX_NEST_LEVEL];
3245 string command_stack[MAX_NEST_LEVEL];
3246 bool command_flag= false;
3247 int command_depth= 0, command_base= 0, cmd_depth= 0;
3249 string item_name, command_name;
3250 string c_depth, c_params, tmps;
3252 int depth= 0; /* paragraph depth */
3254 tex_code_break_column = column;
3256 ofstream ofs(fname.c_str());
3258 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
3264 ofs << "<!doctype " << top_element
3265 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
3267 if (params.preamble.empty())
3270 ofs << "\n [ " << params.preamble << " \n]>\n\n";
3272 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
3273 << "\n See http://www.lyx.org/ for more information -->\n";
3275 if(params.options.empty())
3276 sgmlOpenTag(ofs, 0, top_element);
3278 string top = top_element;
3280 top += params.options;
3281 sgmlOpenTag(ofs, 0, top);
3285 int desc_on = 0; /* description mode*/
3286 LyXLayout const & style =
3287 textclasslist.Style(users->buffer()->params.textclass,
3289 par->AutoDeleteInsets();
3291 /* environment tag closing */
3292 for( ; depth > par->depth; --depth) {
3293 if(environment_inner[depth] != "!-- --") {
3294 item_name= "listitem";
3295 sgmlCloseTag(ofs, command_depth + depth,
3297 if( environment_inner[depth] == "varlistentry")
3298 sgmlCloseTag(ofs, depth+command_depth,
3299 environment_inner[depth]);
3301 sgmlCloseTag(ofs, depth + command_depth,
3302 environment_stack[depth]);
3303 environment_stack[depth].clear();
3304 environment_inner[depth].clear();
3307 if(depth == par->depth
3308 && environment_stack[depth] != style.latexname()
3309 && !environment_stack[depth].empty()) {
3310 if(environment_inner[depth] != "!-- --") {
3311 item_name= "listitem";
3312 sgmlCloseTag(ofs, command_depth+depth,
3314 if( environment_inner[depth] == "varlistentry")
3316 depth + command_depth,
3317 environment_inner[depth]);
3320 sgmlCloseTag(ofs, depth + command_depth,
3321 environment_stack[depth]);
3323 environment_stack[depth].clear();
3324 environment_inner[depth].clear();
3327 // Write opening SGML tags.
3328 switch(style.latextype) {
3329 case LATEX_PARAGRAPH:
3330 if(style.latexname() != "dummy")
3331 sgmlOpenTag(ofs, depth+command_depth,
3337 LinuxDocError(par, 0,
3338 _("Error : Wrong depth for "
3339 "LatexType Command.\n"));
3341 command_name = style.latexname();
3343 tmps = style.latexparam();
3344 c_params = split(tmps, c_depth,'|');
3346 cmd_depth= atoi(c_depth.c_str());
3349 if(cmd_depth<command_base) {
3350 for(int j = command_depth;
3351 j >= command_base; --j)
3352 if(!command_stack[j].empty())
3353 sgmlCloseTag(ofs, j, command_stack[j]);
3354 command_depth= command_base= cmd_depth;
3356 else if(cmd_depth <= command_depth) {
3357 for(int j = command_depth;
3358 j >= cmd_depth; --j)
3360 if(!command_stack[j].empty())
3361 sgmlCloseTag(ofs, j, command_stack[j]);
3362 command_depth= cmd_depth;
3365 command_depth= cmd_depth;
3368 command_depth = command_base = cmd_depth;
3369 command_flag = true;
3371 command_stack[command_depth]= command_name;
3373 // treat label as a special case for
3374 // more WYSIWYM handling.
3375 if (par->GetChar(0) == LyXParagraph::META_INSET) {
3376 Inset * inset = par->GetInset(0);
3377 char lyx_code = inset->LyxCode();
3378 if (lyx_code == Inset::LABEL_CODE){
3379 command_name += " id=\"";
3380 command_name += (static_cast<InsetCommand *>(inset))->getContents();
3381 command_name += "\"";
3386 sgmlOpenTag(ofs, depth+command_depth, command_name);
3387 item_name = "title";
3388 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
3391 case LATEX_ENVIRONMENT:
3392 case LATEX_ITEM_ENVIRONMENT:
3393 if (depth < par->depth) {
3395 environment_stack[depth].clear();
3398 if (environment_stack[depth] != style.latexname()) {
3399 environment_stack[depth] = style.latexname();
3400 environment_inner[depth] = "!-- --";
3401 sgmlOpenTag(ofs, depth + command_depth,
3402 environment_stack[depth]);
3404 if(environment_inner[depth] != "!-- --") {
3405 item_name= "listitem";
3407 command_depth + depth,
3409 if (environment_inner[depth] == "varlistentry")
3411 depth + command_depth,
3412 environment_inner[depth]);
3416 if(style.latextype == LATEX_ENVIRONMENT) {
3417 if(!style.latexparam().empty())
3418 sgmlOpenTag(ofs, depth + command_depth,
3419 style.latexparam());
3423 desc_on = (style.labeltype == LABEL_MANUAL);
3426 environment_inner[depth]= "varlistentry";
3428 environment_inner[depth]= "listitem";
3430 sgmlOpenTag(ofs, depth + 1 + command_depth,
3431 environment_inner[depth]);
3435 sgmlOpenTag(ofs, depth + 1 + command_depth,
3440 sgmlOpenTag(ofs, depth + 1 + command_depth,
3445 sgmlOpenTag(ofs, depth + command_depth,
3451 #ifdef USE_OSTREAM_ONLY
3453 SimpleDocBookOnePar(ofs, extra_par, par, desc_on,
3454 depth + 1 + command_depth);
3456 DocBookHandleFootnote(ofs, par,
3457 depth + 1 + command_depth);
3459 string tmp_par, extra_par;
3461 SimpleDocBookOnePar(tmp_par, extra_par, par, desc_on,
3462 depth + 1 + command_depth);
3466 DocBookHandleFootnote(ofs, par,
3467 depth + 1 + command_depth);
3470 while(par && par->IsDummy());
3473 /* write closing SGML tags */
3474 switch(style.latextype) {
3477 sgmlCloseTag(ofs, depth + command_depth, end_tag);
3479 case LATEX_ENVIRONMENT:
3480 if(!style.latexparam().empty())
3481 sgmlCloseTag(ofs, depth + command_depth,
3482 style.latexparam());
3484 case LATEX_ITEM_ENVIRONMENT:
3485 if(desc_on == 1) break;
3487 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
3489 case LATEX_PARAGRAPH:
3490 if(style.latexname() != "dummy")
3491 sgmlCloseTag(ofs, depth + command_depth,
3495 sgmlCloseTag(ofs, depth + command_depth,
3502 for(; depth >= 0; --depth) {
3503 if(!environment_stack[depth].empty()) {
3504 if(environment_inner[depth] != "!-- --") {
3505 item_name= "listitem";
3506 sgmlCloseTag(ofs, command_depth + depth,
3508 if( environment_inner[depth] == "varlistentry")
3509 sgmlCloseTag(ofs, depth + command_depth,
3510 environment_inner[depth]);
3513 sgmlCloseTag(ofs, depth + command_depth,
3514 environment_stack[depth]);
3518 for(int j = command_depth; j >= command_base; --j)
3519 if(!command_stack[j].empty())
3520 sgmlCloseTag(ofs, j, command_stack[j]);
3523 sgmlCloseTag(ofs, 0, top_element);
3526 // How to check for successful close
3530 #ifdef USE_OSTREAM_ONLY
3531 void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
3532 LyXParagraph * par, int & desc_on,
3536 par->SimpleDocBookOneTablePar(os, extra, desc_on, depth);
3539 LyXFont font1, font2;
3542 LyXParagraph::size_type main_body;
3544 //string emph = "emphasis";
3545 bool emph_flag = false;
3546 int char_line_count = 0;
3548 LyXLayout const & style = textclasslist.Style(params.textclass,
3551 if (style.labeltype != LABEL_MANUAL)
3554 main_body = par->BeginningOfMainBody();
3556 // gets paragraph main font
3558 font1 = style.labelfont;
3562 char_line_count = depth;
3563 if(!style.free_spacing)
3564 for (j = 0; j < depth; ++j)
3567 // parsing main loop
3568 for (LyXParagraph::size_type i = 0;
3569 i < par->size(); ++i) {
3570 font2 = par->getFont(i);
3572 // handle <emphasis> tag
3573 if (font1.emph() != font2.emph() && i) {
3574 if (font2.emph() == LyXFont::ON) {
3578 os << "</emphasis>";
3583 c = par->GetChar(i);
3585 if (c == LyXParagraph::META_INSET) {
3586 inset = par->GetInset(i);
3589 inset->DocBook(ost);
3590 string tmp_out = ost.str().c_str();
3593 inset->DocBook(ost);
3595 char * ctmp = ost.str();
3596 string tmp_out(ctmp);
3600 // This code needs some explanation:
3601 // Two insets are treated specially
3602 // label if it is the first element in a command paragraph
3604 // graphics inside tables or figure floats can't go on
3605 // title (the equivalente in latex for this case is caption
3606 // and title should come first
3609 if(desc_on!= 3 || i!= 0) {
3610 if(!tmp_out.empty() && tmp_out[0] == '@') {
3612 extra += frontStrip(tmp_out, '@');
3614 os << frontStrip(tmp_out, '@');
3619 } else if (font2.latex() == LyXFont::ON) {
3620 // "TeX"-Mode on ==> SGML-Mode on.
3627 if (par->linuxDocConvertChar(c, sgml_string)
3628 && !style.free_spacing) { // in freespacing
3630 // non-breaking characters
3634 os << "\n</term><listitem><para>";
3648 /* needed if there is an optional argument but no contents */
3649 if (main_body > 0 && main_body == par->size()) {
3653 os << "</emphasis>";
3656 /* resets description flag correctly */
3659 /* <term> not closed... */
3666 void Buffer::SimpleDocBookOnePar(string & file, string & extra,
3667 LyXParagraph * par, int & desc_on,
3671 par->SimpleDocBookOneTablePar(file, extra, desc_on, depth);
3674 LyXFont font1, font2;
3677 LyXParagraph::size_type main_body;
3679 string emph= "emphasis";
3680 bool emph_flag= false;
3681 int char_line_count= 0;
3683 LyXLayout const & style = textclasslist.Style(params.textclass,
3686 if (style.labeltype != LABEL_MANUAL)
3689 main_body = par->BeginningOfMainBody();
3691 /* gets paragraph main font */
3693 font1 = style.labelfont;
3697 char_line_count = depth;
3698 if(!style.free_spacing)
3699 for (j = 0; j < depth; ++j)
3702 /* parsing main loop */
3703 for (LyXParagraph::size_type i = 0;
3704 i < par->size(); ++i) {
3705 font2 = par->getFont(i);
3707 /* handle <emphasis> tag */
3708 if (font1.emph() != font2.emph() && i) {
3709 if (font2.emph() == LyXFont::ON) {
3710 file += "<emphasis>";
3713 file += "</emphasis>";
3718 c = par->GetChar(i);
3720 if (c == LyXParagraph::META_INSET) {
3721 inset = par->GetInset(i);
3722 #ifdef USE_OSTREAM_ONLY
3725 inset->DocBook(ost);
3726 string tmp_out = ost.str().c_str();
3729 inset->DocBook(ost);
3731 char * ctmp = ost.str();
3732 string tmp_out(ctmp);
3737 inset->DocBook(tmp_out);
3740 // This code needs some explanation:
3741 // Two insets are treated specially
3742 // label if it is the first element in a command paragraph
3744 // graphics inside tables or figure floats can't go on
3745 // title (the equivalente in latex for this case is caption
3746 // and title should come first
3749 if(desc_on!= 3 || i!= 0) {
3750 if(!tmp_out.empty() && tmp_out[0] == '@') {
3752 extra += frontStrip(tmp_out, '@');
3754 file += frontStrip(tmp_out, '@');
3759 } else if (font2.latex() == LyXFont::ON) {
3760 // "TeX"-Mode on ==> SGML-Mode on.
3767 if (par->linuxDocConvertChar(c, sgml_string)
3768 && !style.free_spacing) { // in freespacing
3770 // non-breaking characters
3775 file += "</term><listitem><para>";
3783 file += sgml_string;
3789 /* needed if there is an optional argument but no contents */
3790 if (main_body > 0 && main_body == par->size()) {
3794 file += "</emphasis>";
3797 /* resets description flag correctly */
3800 /* <term> not closed... */
3809 int Buffer::runLaTeX()
3811 if (!users->text) return 0;
3815 // get LaTeX-Filename
3816 string name = getLatexName();
3818 string path = OnlyPath(filename);
3820 string org_path = path;
3821 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3825 Path p(path); // path to LaTeX file
3826 users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
3828 // Remove all error insets
3829 bool a = users->removeAutoInsets();
3831 // Always generate the LaTeX file
3832 makeLaTeXFile(name, org_path, false);
3835 // do the LaTex run(s)
3837 string latex_command = lyxrc->pdf_mode ?
3838 lyxrc->pdflatex_command : lyxrc->latex_command;
3839 LaTeX latex(latex_command, name, filepath);
3840 int res = latex.run(terr,
3841 users->owner()->getMiniBuffer()); // running latex
3843 // check return value from latex.run().
3844 if ((res & LaTeX::NO_LOGFILE)) {
3845 WriteAlert(_("LaTeX did not work!"),
3846 _("Missing log file:"), name);
3847 } else if ((res & LaTeX::ERRORS)) {
3848 users->owner()->getMiniBuffer()->Set(_("Done"));
3849 // Insert all errors as errors boxes
3850 users->insertErrors(terr);
3852 // Dvi should also be kept dirty if the latex run
3853 // ends up with errors. However it should be possible
3854 // to view a dirty dvi too.
3856 //no errors or any other things to think about so:
3857 users->owner()->getMiniBuffer()->Set(_("Done"));
3861 // if we removed error insets before we ran LaTeX or if we inserted
3862 // error insets after we ran LaTeX this must be run:
3863 if (a || (res & LaTeX::ERRORS)){
3866 users->updateScrollbar();
3870 return latex.getNumErrors();
3874 int Buffer::runLiterate()
3876 if (!users->text) return 0;
3880 // get LaTeX-Filename
3881 string name = getLatexName();
3882 // get Literate-Filename
3883 string lit_name = ChangeExtension (getLatexName(),
3884 lyxrc->literate_extension, true);
3886 string path = OnlyPath(filename);
3888 string org_path = path;
3889 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3893 Path p(path); // path to Literate file
3894 users->owner()->getMiniBuffer()->Set(_("Running Literate..."));
3896 // Remove all error insets
3897 bool a = users->removeAutoInsets();
3899 // generate the Literate file if necessary
3900 if (!isDviClean() || a) {
3901 makeLaTeXFile(lit_name, org_path, false);
3905 string latex_command = lyxrc->pdf_mode ?
3906 lyxrc->pdflatex_command : lyxrc->latex_command;
3907 Literate literate(latex_command, name, filepath,
3909 lyxrc->literate_command, lyxrc->literate_error_filter,
3910 lyxrc->build_command, lyxrc->build_error_filter);
3912 int res = literate.weave(terr, users->owner()->getMiniBuffer());
3914 // check return value from literate.weave().
3915 if ((res & Literate::NO_LOGFILE)) {
3916 WriteAlert(_("Literate command did not work!"),
3917 _("Missing log file:"), name);
3918 } else if ((res & Literate::ERRORS)) {
3919 users->owner()->getMiniBuffer()->Set(_("Done"));
3920 // Insert all errors as errors boxes
3921 users->insertErrors(terr);
3923 // Dvi should also be kept dirty if the latex run
3924 // ends up with errors. However it should be possible
3925 // to view a dirty dvi too.
3927 //no errors or any other things to think about so:
3928 users->owner()->getMiniBuffer()->Set(_("Done"));
3932 // if we removed error insets before we ran LaTeX or if we inserted
3933 // error insets after we ran LaTeX this must be run:
3934 if (a || (res & Literate::ERRORS)){
3937 users->updateScrollbar();
3941 return literate.getNumErrors();
3945 int Buffer::buildProgram()
3947 if (!users->text) return 0;
3951 // get LaTeX-Filename
3952 string name = getLatexName();
3953 // get Literate-Filename
3954 string lit_name = ChangeExtension(getLatexName(),
3955 lyxrc->literate_extension, true);
3957 string path = OnlyPath(filename);
3959 string org_path = path;
3960 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3964 Path p(path); // path to Literate file
3965 users->owner()->getMiniBuffer()->Set(_("Building Program..."));
3967 // Remove all error insets
3968 bool a = users->removeAutoInsets();
3970 // generate the LaTeX file if necessary
3971 if (!isNwClean() || a) {
3972 makeLaTeXFile(lit_name, org_path, false);
3976 string latex_command = lyxrc->pdf_mode ?
3977 lyxrc->pdflatex_command : lyxrc->latex_command;
3978 Literate literate(latex_command, name, filepath,
3980 lyxrc->literate_command, lyxrc->literate_error_filter,
3981 lyxrc->build_command, lyxrc->build_error_filter);
3983 int res = literate.build(terr, users->owner()->getMiniBuffer());
3985 // check return value from literate.build().
3986 if ((res & Literate::NO_LOGFILE)) {
3987 WriteAlert(_("Build did not work!"),
3988 _("Missing log file:"), name);
3989 } else if ((res & Literate::ERRORS)) {
3990 users->owner()->getMiniBuffer()->Set(_("Done"));
3991 // Insert all errors as errors boxes
3992 users->insertErrors(terr);
3994 // Literate files should also be kept dirty if the literate
3995 // command run ends up with errors.
3997 //no errors or any other things to think about so:
3998 users->owner()->getMiniBuffer()->Set(_("Done"));
4002 // if we removed error insets before we ran Literate/Build or if we inserted
4003 // error insets after we ran Literate/Build this must be run:
4004 if (a || (res & Literate::ERRORS)){
4007 users->updateScrollbar();
4011 return literate.getNumErrors();
4015 // This should be enabled when the Chktex class is implemented. (Asger)
4016 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
4017 // Other flags: -wall -v0 -x
4018 int Buffer::runChktex()
4020 if (!users->text) return 0;
4024 // get LaTeX-Filename
4025 string name = getLatexName();
4026 string path = OnlyPath(filename);
4028 string org_path = path;
4029 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
4033 Path p(path); // path to LaTeX file
4034 users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
4036 // Remove all error insets
4037 bool a = users->removeAutoInsets();
4039 // Generate the LaTeX file if neccessary
4040 if (!isDviClean() || a) {
4041 makeLaTeXFile(name, org_path, false);
4046 Chktex chktex(lyxrc->chktex_command, name, filepath);
4047 int res = chktex.run(terr); // run chktex
4050 WriteAlert(_("chktex did not work!"),
4051 _("Could not run with file:"), name);
4052 } else if (res > 0) {
4053 // Insert all errors as errors boxes
4054 users->insertErrors(terr);
4057 // if we removed error insets before we ran chktex or if we inserted
4058 // error insets after we ran chktex, this must be run:
4062 users->updateScrollbar();
4071 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
4073 LyXFont font1(LyXFont::ALL_INHERIT);
4076 LyXParagraph::size_type i;
4080 string fname1 = TmpFileName(string(), "RAT1");
4081 string fname2 = TmpFileName(string(), "RAT2");
4083 ofstream ofs(fname1.c_str());
4085 WriteAlert(_("LYX_ERROR:"),
4086 _("Cannot open temporary file:"), fname1);
4089 par->table->RoffEndOfCell(ofs, -1);
4090 for (i = 0; i < par->size(); ++i) {
4091 c = par->GetChar(i);
4092 if (par->table->IsContRow(cell)) {
4093 if (c == LyXParagraph::META_NEWLINE)
4097 font2 = par->GetFontSettings(i);
4098 if (font1.latex() != font2.latex()) {
4099 if (font2.latex() != LyXFont::OFF)
4103 case LyXParagraph::META_INSET:
4104 if ((inset = par->GetInset(i))) {
4107 stringstresm ss(ios::in | ios::out);
4108 inset->Latex(ss, -1);
4120 inset->Latex(ss, -1);
4133 fstream fs(fname2.c_str(), ios::in|ios::out);
4135 WriteAlert(_("LYX_ERROR:"),
4136 _("Cannot open temporary file:"), fname2);
4138 remove(fname1.c_str());
4141 inset->Latex(fs, -1);
4155 case LyXParagraph::META_NEWLINE:
4156 if (par->table->CellHasContRow(cell)>= 0)
4157 par->RoffContTableRows(ofs, i+1, cell);
4158 par->table->RoffEndOfCell(ofs, cell);
4161 case LyXParagraph::META_HFILL:
4164 case LyXParagraph::META_PROTECTED_SEPARATOR:
4175 << "RoffAsciiTable:"
4176 " NULL char in structure." << endl;
4180 par->table->RoffEndOfCell(ofs, cell);
4182 string cmd = lyxrc->ascii_roff_command + " >" + fname2;
4183 cmd = subst(cmd, "$$FName", fname1);
4184 Systemcalls one(Systemcalls::System, cmd);
4185 if (!(lyxerr.debugging(Debug::ROFF))) {
4186 remove(fname1.c_str());
4188 ifstream ifs(fname2.c_str());
4190 WriteFSAlert(_("Error! Can't open temporary file:"), fname2);
4193 // now output the produced file
4197 WriteAlert(_("Error!"),
4198 _("Error executing *roff command on table"));
4199 // overread leading blank lines
4200 while(!ifs && (c == '\n'))
4203 for(j = 0; j < par->depth; ++j)
4205 while(!ifs && (c != '\n')) {
4210 // overread trailing blank lines
4211 while(!ifs && (c == '\n'))
4215 remove(fname2.c_str());
4220 /// changed Heinrich Bauer, 23/03/98
4221 bool Buffer::isDviClean() const
4223 if (lyxrc->use_tempdir)
4224 return dvi_clean_tmpd;
4226 return dvi_clean_orgd;
4230 /// changed Heinrich Bauer, 23/03/98
4231 void Buffer::markDviClean()
4233 if (lyxrc->use_tempdir)
4234 dvi_clean_tmpd = true;
4236 dvi_clean_orgd = true;
4240 /// changed Heinrich Bauer, 23/03/98
4241 void Buffer::markDviDirty()
4243 if (lyxrc->use_tempdir)
4244 dvi_clean_tmpd = false;
4246 dvi_clean_orgd = false;
4250 void Buffer::validate(LaTeXFeatures & features) const
4252 LyXParagraph * par = paragraph;
4253 LyXTextClass const & tclass =
4254 textclasslist.TextClass(params.textclass);
4256 // AMS Style is at document level
4258 features.amsstyle = (params.use_amsmath ||
4259 tclass.provides(LyXTextClass::amsmath));
4262 // We don't use "lyxerr.debug" because of speed. (Asger)
4263 if (lyxerr.debugging(Debug::LATEX))
4264 lyxerr << "Paragraph: " << par << endl;
4266 // Now just follow the list of paragraphs and run
4267 // validate on each of them.
4268 par->validate(features);
4270 // and then the next paragraph
4274 // the bullet shapes are buffer level not paragraph level
4275 // so they are tested here
4276 for (int i = 0; i < 4; ++i) {
4277 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
4278 int font = params.user_defined_bullets[i].getFont();
4281 .user_defined_bullets[i]
4288 features.latexsym = true;
4292 features.amssymb = true;
4294 else if ((font >= 2 && font <= 5)) {
4295 features.pifont = true;
4300 if (lyxerr.debugging(Debug::LATEX)) {
4301 features.showStruct(params);
4306 void Buffer::setPaperStuff()
4308 params.papersize = BufferParams::PAPER_DEFAULT;
4309 char c1 = params.paperpackage;
4310 if (c1 == BufferParams::PACKAGE_NONE) {
4311 char c2 = params.papersize2;
4312 if (c2 == BufferParams::VM_PAPER_USLETTER)
4313 params.papersize = BufferParams::PAPER_USLETTER;
4314 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
4315 params.papersize = BufferParams::PAPER_LEGALPAPER;
4316 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
4317 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
4318 else if (c2 == BufferParams::VM_PAPER_A3)
4319 params.papersize = BufferParams::PAPER_A3PAPER;
4320 else if (c2 == BufferParams::VM_PAPER_A4)
4321 params.papersize = BufferParams::PAPER_A4PAPER;
4322 else if (c2 == BufferParams::VM_PAPER_A5)
4323 params.papersize = BufferParams::PAPER_A5PAPER;
4324 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
4325 (c2 == BufferParams::VM_PAPER_B5))
4326 params.papersize = BufferParams::PAPER_B5PAPER;
4327 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
4328 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
4329 params.papersize = BufferParams::PAPER_A4PAPER;
4333 // This function should be in Buffer because it's a buffer's property (ale)
4334 string Buffer::getIncludeonlyList(char delim)
4337 LyXParagraph * par = paragraph;
4338 LyXParagraph::size_type pos;
4342 while ((inset = par->ReturnNextInsetPointer(pos))){
4343 if (inset->LyxCode() == Inset::INCLUDE_CODE) {
4344 InsetInclude * insetinc =
4345 static_cast<InsetInclude*>(inset);
4346 if (insetinc->isInclude()
4347 && insetinc->isNoLoad()) {
4350 lst += ChangeExtension(insetinc->getContents(), string(), true);
4357 lyxerr.debug() << "Includeonly(" << lst << ')' << endl;
4362 // This is also a buffer property (ale)
4363 string Buffer::getReferenceList(char delim)
4365 /// if this is a child document and the parent is already loaded
4366 /// Use the parent's list instead [ale990407]
4367 if (!params.parentname.empty()
4368 && bufferlist.exists(params.parentname)) {
4369 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4371 return tmp->getReferenceList(delim);
4374 LyXParagraph * par = paragraph;
4375 LyXParagraph::size_type pos;
4380 while ((inset = par->ReturnNextInsetPointer(pos))){
4381 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
4384 lst += inset->getLabel(i);
4390 lyxerr.debug() << "References(" << lst << ")" << endl;
4395 // This is also a buffer property (ale)
4396 string Buffer::getBibkeyList(char delim)
4398 /// if this is a child document and the parent is already loaded
4399 /// Use the parent's list instead [ale990412]
4400 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
4401 Buffer * tmp = bufferlist.getBuffer(params.parentname);
4403 return tmp->getBibkeyList(delim);
4407 LyXParagraph * par = paragraph;
4410 if (!bibkeys.empty())
4412 bibkeys += par->bibkey->getContents();
4417 // Might be either using bibtex or a child has bibliography
4418 if (bibkeys.empty()) {
4422 LyXParagraph::size_type pos = -1;
4424 // Search for Bibtex or Include inset
4425 while ((inset = par->ReturnNextInsetPointer(pos))) {
4426 if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
4427 if (!bibkeys.empty())
4429 bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
4430 } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
4431 string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
4433 if (!bibkeys.empty())
4444 lyxerr.debug() << "Bibkeys(" << bibkeys << ")" << endl;
4449 bool Buffer::isDepClean(string const & name) const
4451 DEPCLEAN * item = dep_clean;
4452 while (item && item->master != name)
4454 if (!item) return true;
4459 void Buffer::markDepClean(string const & name)
4462 dep_clean = new DEPCLEAN;
4463 dep_clean->clean = true;
4464 dep_clean->master = name;
4465 dep_clean->next = 0;
4467 DEPCLEAN* item = dep_clean;
4468 while (item && item->master != name)
4473 item = new DEPCLEAN;
4475 item->master = name;
4482 void Buffer::Dispatch(string const & command)
4484 // Split command string into command and argument
4485 string cmd, line = frontStrip(command);
4486 string arg = strip(frontStrip(split(line, cmd, ' ')));
4488 Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
4492 void Buffer::Dispatch(int action, string const & argument)
4496 MenuExport(this, argument);
4500 lyxerr << "A truly unknown func!" << endl;