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 bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
219 char depth = 0; // signed or unsigned?
220 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
221 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
222 bool the_end_read = false;
224 LyXParagraph * return_par = 0;
225 LyXFont font(LyXFont::ALL_INHERIT);
227 // If we are inserting, we cheat and get a token in advance
228 bool has_token = false;
232 par = new LyXParagraph;
234 users->text->BreakParagraph();
235 return_par = users->text->FirstParagraph();
238 // We don't want to adopt the parameters from the
239 // document we insert, so we skip until the text begins:
242 pretoken = lex.GetString();
243 if (pretoken == "\\layout") {
255 pretoken = lex.GetString();
258 // Profiling show this should give a lot: (Asger)
259 string const token = pretoken;
263 else if (token[0] != '\\') {
264 int n = token.length();
265 for (int i = 0; i < n; ++i) {
266 par->InsertChar(pos, token[i]);
267 par->SetFont(pos, font);
270 } else if (token == "\\i") {
271 inset = new InsetLatexAccent;
273 par->InsertChar(pos, LyXParagraph::META_INSET);
275 par->InsertInset(pos, inset);
276 par->SetFont(pos, font);
278 } else if (token == "\\layout") {
282 par->text.resize(par->text.size());
283 par = new LyXParagraph(par);
287 string layoutname = lex.GetString();
288 pair<bool, LyXTextClass::LayoutList::size_type> pp
289 = textclasslist.NumberOfLayout(params.textclass,
292 par->layout = pp.second;
293 } else { // layout not found
294 // use default layout "Standard" (0)
297 // Test whether the layout is obsolete.
298 LyXLayout const & layout =
299 textclasslist.Style(params.textclass,
301 if (!layout.obsoleted_by().empty())
303 textclasslist.NumberOfLayout(params.textclass,
304 layout.obsoleted_by()).second;
305 par->footnoteflag = footnoteflag;
306 par->footnotekind = footnotekind;
308 font = LyXFont(LyXFont::ALL_INHERIT);
309 } else if (token == "\\end_float") {
313 par->text.resize(par->text.size());
314 par = new LyXParagraph(par);
316 footnotekind = LyXParagraph::FOOTNOTE;
317 footnoteflag = LyXParagraph::NO_FOOTNOTE;
320 par->layout = LYX_DUMMY_LAYOUT;
321 font = LyXFont(LyXFont::ALL_INHERIT);
322 } else if (token == "\\begin_float") {
323 tmpret = lex.FindToken(string_footnotekinds);
324 if (tmpret == -1) ++tmpret;
325 if (tmpret != LYX_LAYOUT_DEFAULT)
326 footnotekind = static_cast<LyXParagraph::footnote_kind>(tmpret); // bad
327 if (footnotekind == LyXParagraph::FOOTNOTE
328 || footnotekind == LyXParagraph::MARGIN)
329 footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
331 footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
332 } else if (token == "\\begin_deeper") {
334 } else if (token == "\\end_deeper") {
336 lex.printError("\\end_deeper: "
337 "depth is already null");
341 } else if (token == "\\begin_preamble") {
342 params.readPreamble(lex);
343 } else if (token == "\\textclass") {
345 pair<bool, LyXTextClassList::size_type> pp =
346 textclasslist.NumberOfClass(lex.GetString());
348 params.textclass = pp.second;
350 lex.printError("Unknown textclass `$$Token'");
351 params.textclass = 0;
353 if (!textclasslist.Load(params.textclass)) {
354 // if the textclass wasn't loaded properly
355 // we need to either substitute another
356 // or stop loading the file.
357 // I can substitute but I don't see how I can
358 // stop loading... ideas?? ARRae980418
359 WriteAlert(_("Textclass Loading Error!"),
360 string(_("Can't load textclass ")) +
361 textclasslist.NameOfClass(params.textclass),
362 _("-- substituting default"));
363 params.textclass = 0;
365 } else if (token == "\\options") {
367 params.options = lex.GetString();
368 } else if (token == "\\language") {
369 params.readLanguage(lex);
370 } else if (token == "\\fontencoding") {
372 } else if (token == "\\inputencoding") {
374 params.inputenc = lex.GetString();
375 } else if (token == "\\graphics") {
376 params.readGraphicsDriver(lex);
377 } else if (token == "\\fontscheme") {
379 params.fonts = lex.GetString();
380 } else if (token == "\\noindent") {
381 par->noindent = true;
382 } else if (token == "\\fill_top") {
383 par->added_space_top = VSpace(VSpace::VFILL);
384 } else if (token == "\\fill_bottom") {
385 par->added_space_bottom = VSpace(VSpace::VFILL);
386 } else if (token == "\\line_top") {
387 par->line_top = true;
388 } else if (token == "\\line_bottom") {
389 par->line_bottom = true;
390 } else if (token == "\\pagebreak_top") {
391 par->pagebreak_top = true;
392 } else if (token == "\\pagebreak_bottom") {
393 par->pagebreak_bottom = true;
394 } else if (token == "\\start_of_appendix") {
395 par->start_of_appendix = true;
396 } else if (token == "\\paragraph_separation") {
397 tmpret = lex.FindToken(string_paragraph_separation);
398 if (tmpret == -1) ++tmpret;
399 if (tmpret != LYX_LAYOUT_DEFAULT)
400 params.paragraph_separation =
401 static_cast<BufferParams::PARSEP>(tmpret);
402 } else if (token == "\\defskip") {
404 params.defskip = VSpace(lex.GetString());
405 } else if (token == "\\no_isolatin1") { // obsolete
407 } else if (token == "\\no_babel") { // obsolete
409 } else if (token == "\\no_epsfig") { // obsolete
411 } else if (token == "\\epsfig") { // obsolete
412 // Indeed it is obsolete, but we HAVE to be backwards
413 // compatible until 0.14, because otherwise all figures
414 // in existing documents are irretrivably lost. (Asger)
415 params.readGraphicsDriver(lex);
416 } else if (token == "\\quotes_language") {
417 tmpret = lex.FindToken(string_quotes_language);
418 if (tmpret == -1) ++tmpret;
419 if (tmpret != LYX_LAYOUT_DEFAULT) {
420 InsetQuotes::quote_language tmpl =
421 InsetQuotes::EnglishQ;
424 tmpl = InsetQuotes::EnglishQ;
427 tmpl = InsetQuotes::SwedishQ;
430 tmpl = InsetQuotes::GermanQ;
433 tmpl = InsetQuotes::PolishQ;
436 tmpl = InsetQuotes::FrenchQ;
439 tmpl = InsetQuotes::DanishQ;
442 params.quotes_language = tmpl;
444 } else if (token == "\\quotes_times") {
446 switch(lex.GetInteger()) {
448 params.quotes_times = InsetQuotes::SingleQ;
451 params.quotes_times = InsetQuotes::DoubleQ;
454 } else if (token == "\\papersize") {
455 tmpret = lex.FindToken(string_papersize);
459 params.papersize2 = tmpret;
460 } else if (token == "\\paperpackage") {
461 tmpret = lex.FindToken(string_paperpackages);
464 params.paperpackage = BufferParams::PACKAGE_NONE;
466 params.paperpackage = tmpret;
467 } else if (token == "\\use_geometry") {
469 params.use_geometry = lex.GetInteger();
470 } else if (token == "\\use_amsmath") {
472 params.use_amsmath = lex.GetInteger();
473 } else if (token == "\\paperorientation") {
474 tmpret = lex.FindToken(string_orientation);
475 if (tmpret == -1) ++tmpret;
476 if (tmpret != LYX_LAYOUT_DEFAULT)
477 params.orientation = static_cast<BufferParams::PAPER_ORIENTATION>(tmpret);
478 } else if (token == "\\paperwidth") {
480 params.paperwidth = lex.GetString();
481 } else if (token == "\\paperheight") {
483 params.paperheight = lex.GetString();
484 } else if (token == "\\leftmargin") {
486 params.leftmargin = lex.GetString();
487 } else if (token == "\\topmargin") {
489 params.topmargin = lex.GetString();
490 } else if (token == "\\rightmargin") {
492 params.rightmargin = lex.GetString();
493 } else if (token == "\\bottommargin") {
495 params.bottommargin = lex.GetString();
496 } else if (token == "\\headheight") {
498 params.headheight = lex.GetString();
499 } else if (token == "\\headsep") {
501 params.headsep = lex.GetString();
502 } else if (token == "\\footskip") {
504 params.footskip = lex.GetString();
505 } else if (token == "\\paperfontsize") {
507 params.fontsize = strip(lex.GetString());
508 } else if (token == "\\papercolumns") {
510 params.columns = lex.GetInteger();
511 } else if (token == "\\papersides") {
513 switch(lex.GetInteger()) {
515 case 1: params.sides = LyXTextClass::OneSide; break;
516 case 2: params.sides = LyXTextClass::TwoSides; break;
518 } else if (token == "\\paperpagestyle") {
520 params.pagestyle = strip(lex.GetString());
521 } else if (token == "\\bullet") {
523 int index = lex.GetInteger();
525 int temp_int = lex.GetInteger();
526 params.user_defined_bullets[index].setFont(temp_int);
527 params.temp_bullets[index].setFont(temp_int);
529 temp_int = lex.GetInteger();
530 params.user_defined_bullets[index].setCharacter(temp_int);
531 params.temp_bullets[index].setCharacter(temp_int);
533 temp_int = lex.GetInteger();
534 params.user_defined_bullets[index].setSize(temp_int);
535 params.temp_bullets[index].setSize(temp_int);
537 string temp_str = lex.GetString();
538 if (temp_str != "\\end_bullet") {
539 // this element isn't really necessary for
540 // parsing but is easier for humans
541 // to understand bullets. Put it back and
542 // set a debug message?
543 lex.printError("\\end_bullet expected, got" + temp_str);
544 //how can I put it back?
546 } else if (token == "\\bulletLaTeX") {
548 int index = lex.GetInteger();
550 string temp_str = lex.GetString(), sum_str;
551 while (temp_str != "\\end_bullet") {
552 // this loop structure is needed when user
553 // enters an empty string since the first
554 // thing returned will be the \\end_bullet
556 // if the LaTeX entry has spaces. Each element
557 // therefore needs to be read in turn
560 temp_str = lex.GetString();
562 params.user_defined_bullets[index].setText(sum_str);
563 params.temp_bullets[index].setText(sum_str);
564 } else if (token == "\\secnumdepth") {
566 params.secnumdepth = lex.GetInteger();
567 } else if (token == "\\tocdepth") {
569 params.tocdepth = lex.GetInteger();
570 } else if (token == "\\baselinestretch") { // obsolete
571 lex.nextToken(); // should not be used directly
573 // Will probably keep a kind of support just for
575 params.spacing.set(Spacing::Other, lex.GetFloat());
576 } else if (token == "\\spacing") {
578 string tmp = strip(lex.GetString());
579 if (tmp == "single") {
580 params.spacing.set(Spacing::Single);
581 } else if (tmp == "onehalf") {
582 params.spacing.set(Spacing::Onehalf);
583 } else if (tmp == "double") {
584 params.spacing.set(Spacing::Double);
585 } else if (tmp == "other") {
587 params.spacing.set(Spacing::Other,
590 lex.printError("Unknown spacing token: '$$Token'");
592 } else if (token == "\\float_placement") {
594 params.float_placement = lex.GetString();
595 } else if (token == "\\cursor") { // obsolete
596 // this is obsolete, so we just skip it.
598 } else if (token == "\\family") {
600 font.setLyXFamily(lex.GetString());
601 } else if (token == "\\series") {
603 font.setLyXSeries(lex.GetString());
604 } else if (token == "\\shape") {
606 font.setLyXShape(lex.GetString());
607 } else if (token == "\\size") {
609 font.setLyXSize(lex.GetString());
610 } else if (token == "\\latex") {
612 string tok = lex.GetString();
613 // This is dirty, but gone with LyX3. (Asger)
614 if (tok == "no_latex")
615 font.setLatex(LyXFont::OFF);
616 else if (tok == "latex")
617 font.setLatex(LyXFont::ON);
618 else if (tok == "default")
619 font.setLatex(LyXFont::INHERIT);
621 lex.printError("Unknown LaTeX font flag "
623 } else if (token == "\\direction") {
625 string tok = lex.GetString();
627 font.setDirection(LyXFont::LTR_DIR);
628 else if (tok == "rtl")
629 font.setDirection(LyXFont::RTL_DIR);
630 else if (tok == "default")
631 font.setDirection(LyXFont::INHERIT_DIR);
633 lex.printError("Unknown font flag "
635 } else if (token == "\\emph") {
637 font.setEmph(font.setLyXMisc(lex.GetString()));
638 } else if (token == "\\bar") {
640 string tok = lex.GetString();
641 // This is dirty, but gone with LyX3. (Asger)
643 font.setUnderbar(LyXFont::ON);
644 else if (tok == "no")
645 font.setUnderbar(LyXFont::OFF);
646 else if (tok == "default")
647 font.setUnderbar(LyXFont::INHERIT);
649 lex.printError("Unknown bar font flag "
651 } else if (token == "\\noun") {
653 font.setNoun(font.setLyXMisc(lex.GetString()));
654 } else if (token == "\\color") {
656 font.setLyXColor(lex.GetString());
657 } else if (token == "\\align") {
658 tmpret = lex.FindToken(string_align);
659 if (tmpret == -1) ++tmpret;
660 if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
662 for (; tmpret > 0; --tmpret)
663 tmpret2 = tmpret2 * 2;
664 par->align = LyXAlignment(tmpret2);
666 } else if (token == "\\added_space_top") {
668 par->added_space_top = lex.GetString();
669 } else if (token == "\\added_space_bottom") {
671 par->added_space_bottom = lex.GetString();
672 } else if (token == "\\pextra_type") {
674 par->pextra_type = lex.GetInteger();
675 } else if (token == "\\pextra_width") {
677 par->pextra_width = lex.GetString();
678 } else if (token == "\\pextra_widthp") {
680 par->pextra_widthp = lex.GetString();
681 } else if (token == "\\pextra_alignment") {
683 par->pextra_alignment = lex.GetInteger();
684 } else if (token == "\\pextra_hfill") {
686 par->pextra_hfill = lex.GetInteger();
687 } else if (token == "\\pextra_start_minipage") {
689 par->pextra_start_minipage = lex.GetInteger();
690 } else if (token == "\\labelwidthstring") {
692 par->labelwidthstring = lex.GetString();
693 /* do not delete this token, it is still needed! */
694 } else if (token == "\\end_inset") {
695 /* simple ignore this. The insets do not have
697 // but insets should read it, it is a part of
698 //the inset isn't it? Lgb.
699 } else if (token == "\\begin_inset") {
701 tmptok = lex.GetString();
702 /* test the different insets */
703 if (tmptok == "Quotes") {
704 inset = new InsetQuotes;
706 par->InsertChar(pos, LyXParagraph::META_INSET);
707 par->InsertInset(pos, inset);
708 par->SetFont(pos, font);
710 } else if (tmptok == "\\i") {
711 inset = new InsetLatexAccent;
713 par->InsertChar(pos, LyXParagraph::META_INSET);
714 par->InsertInset(pos, inset);
715 par->SetFont(pos, font);
717 } else if (tmptok == "FormulaMacro") {
718 inset = new InsetFormulaMacro;
720 par->InsertChar(pos, LyXParagraph::META_INSET);
721 par->InsertInset(pos, inset);
722 par->SetFont(pos, font);
724 } else if (tmptok == "Formula") {
725 inset = new InsetFormula;
727 par->InsertChar(pos, LyXParagraph::META_INSET);
728 par->InsertInset(pos, inset);
729 par->SetFont(pos, font);
731 } else if (tmptok == "Figure") {
732 inset = new InsetFig(100, 100, this);
734 par->InsertChar(pos, LyXParagraph::META_INSET);
735 par->InsertInset(pos, inset);
736 par->SetFont(pos, font);
738 } else if (tmptok == "Info") {
739 inset = new InsetInfo;
741 par->InsertChar(pos, LyXParagraph::META_INSET);
742 par->InsertInset(pos, inset);
743 par->SetFont(pos, font);
745 } else if (tmptok == "Include") {
746 inset = new InsetInclude(string(), this);
748 par->InsertChar(pos, LyXParagraph::META_INSET);
749 par->InsertInset(pos, inset);
750 par->SetFont(pos, font);
752 } else if (tmptok == "ERT") {
753 inset = new InsetERT(this);
755 par->InsertChar(pos, LyXParagraph::META_INSET);
756 par->InsertInset(pos, inset);
757 par->SetFont(pos, font);
759 } else if (tmptok == "Text") {
760 inset = new InsetText(this);
762 par->InsertChar(pos, LyXParagraph::META_INSET);
763 par->InsertInset(pos, inset);
764 par->SetFont(pos, font);
766 } else if (tmptok == "GRAPHICS") {
767 inset = new InsetGraphics;
769 par->InsertChar(pos, LyXParagraph::META_INSET);
770 par->InsertInset(pos, inset);
771 par->SetFont(pos, font);
772 } else if (tmptok == "LatexCommand") {
775 if (inscmd.getCmdName() == "cite") {
776 inset = new InsetCitation(inscmd.getContents(), inscmd.getOptions());
777 } else if (inscmd.getCmdName() == "bibitem") {
778 lex.printError("Wrong place for bibitem");
779 inset = inscmd.Clone();
780 } else if (inscmd.getCmdName() == "BibTeX") {
781 inset = new InsetBibtex(inscmd.getContents(), inscmd.getOptions(), this);
782 } else if (inscmd.getCmdName() == "index") {
783 inset = new InsetIndex(inscmd.getContents());
784 } else if (inscmd.getCmdName() == "include") {
785 inset = new InsetInclude(inscmd.getContents(), this);
786 } else if (inscmd.getCmdName() == "label") {
787 inset = new InsetLabel(inscmd.getCommand());
788 } else if (inscmd.getCmdName() == "url"
789 || inscmd.getCmdName() == "htmlurl") {
790 inset = new InsetUrl(inscmd.getCommand());
791 } else if (inscmd.getCmdName() == "ref"
792 || inscmd.getCmdName() == "pageref") {
793 if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
794 inset = new InsetRef(inscmd, this);
796 #warning Verify that this else clause is still needed. (Lgb)
798 // This condition comes from a
799 // temporary solution to the latexdel
800 // ref inset that was transformed to
801 // an empty ref inset plus the body
802 // surronded by latexdel insets
804 string cont, opt, tmptmptok, cmdname;
806 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
811 tmptmptok = lex.GetString();
812 if(tmptmptok[0] == '\\') {
813 if( tmptmptok == "\\backslash")
823 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
828 tmptmptok = lex.GetString();
829 if(tmptmptok[0] == '\\') {
830 if( tmptmptok == "\\backslash")
840 while(lex.IsOK() && lex.GetString() != "\\end_inset" ) {
846 cmdname = "\\" + inscmd.getCmdName();
847 cmdname += "[" + cont + "]";
848 cmdname += "{" + opt + "}";
849 inset = new InsetRef(cmdname, this);
852 } else if (inscmd.getCmdName() == "tableofcontents") {
853 inset = new InsetTOC(this);
854 } else if (inscmd.getCmdName() == "listoffigures") {
855 inset = new InsetLOF(this);
856 } else if (inscmd.getCmdName() == "listofalgorithms") {
857 inset = new InsetLOA(this);
858 } else if (inscmd.getCmdName() == "listoftables") {
859 inset = new InsetLOT(this);
860 } else if (inscmd.getCmdName() == "printindex") {
861 inset = new InsetPrintIndex(this);
862 } else if (inscmd.getCmdName() == "lyxparent") {
863 inset = new InsetParent(inscmd.getContents(), this);
867 par->InsertChar(pos, LyXParagraph::META_INSET);
868 par->InsertInset(pos, inset);
869 par->SetFont(pos, font);
873 } else if (token == "\\InsetQuotes") {
874 inset = new InsetQuotes;
876 par->InsertChar(pos, LyXParagraph::META_INSET);
877 par->InsertInset(pos, inset);
878 par->SetFont(pos, font);
880 } else if (token == "\\InsetFormula") {
881 inset = new InsetFormula;
883 par->InsertChar(pos, LyXParagraph::META_INSET);
884 par->InsertInset(pos, inset);
885 par->SetFont(pos, font);
887 } else if (token == "\\SpecialChar") {
888 inset = new InsetSpecialChar;
890 par->InsertChar(pos, LyXParagraph::META_INSET);
891 par->InsertInset(pos, inset);
892 par->SetFont(pos, font);
894 } else if (token == "\\Figure") {
895 inset = new InsetFig(100, 100, this);
897 par->InsertChar(pos, LyXParagraph::META_INSET);
898 par->InsertInset(pos, inset);
899 par->SetFont(pos, font);
901 } else if (token == "\\newline") { // soon obsolete
903 par->InsertChar(pos, LyXParagraph::META_NEWLINE);
904 par->SetFont(pos, font);
906 inset = new InsetSpecialChar(InsetSpecialChar::NEWLINE);
907 par->InsertChar(pos, LyXParagraph::META_INSET);
908 par->InsertInset(pos, inset);
909 par->SetFont(pos, font);
912 } else if (token == "\\LyXTable") {
913 par->table = new LyXTable(lex);
914 } else if (token == "\\hfill") {
915 par->InsertChar(pos, LyXParagraph::META_HFILL);
916 par->SetFont(pos, font);
918 } else if (token == "\\protected_separator") { // obsolete
920 inset = new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
921 par->InsertChar(pos, LyXParagraph::META_INSET);
922 par->InsertInset(pos, inset);
923 par->SetFont(pos, font);
925 par->InsertChar(pos, LyXParagraph::META_PROTECTED_SEPARATOR);
926 par->SetFont(pos, font);
929 } else if (token == "\\bibitem") { // ale970302
931 par->bibkey = new InsetBibKey;
932 par->bibkey->Read(lex);
933 }else if (token == "\\backslash") {
934 par->InsertChar(pos, '\\');
935 par->SetFont(pos, font);
937 }else if (token == "\\the_end") {
940 // This should be insurance for the future: (Asger)
941 lex.printError("Unknown token `$$Token'. "
942 "Inserting as text.");
943 int n = token.length();
944 for (int i = 0; i < n; ++i) {
945 par->InsertChar(pos, token[i]);
946 par->SetFont(pos, font);
955 paragraph = return_par;
961 bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
967 token = lex.GetString();
968 if (token == "\\lyxformat") { // the first token _must_ be...
970 format = lex.GetFloat();
972 if (LYX_FORMAT - format > 0.05) {
973 printf(_("Warning: need lyxformat %.2f but found %.2f\n"),
976 if (format - LYX_FORMAT > 0.05) {
977 printf(_("ERROR: need lyxformat %.2f but found %.2f\n"),
980 bool the_end = readLyXformat2(lex, par);
981 // Formats >= 2.13 support "\the_end" marker
988 WriteAlert(_("Warning!"),
989 _("Reading of document is not complete"),
990 _("Maybe the document is truncated"));
991 // We simulate a safe reading anyways to allow
992 // users to take the chance... (Asger)
996 WriteAlert(_("ERROR!"),
997 _("Old LyX file format found. "
998 "Use LyX 0.10.x to read this!"));
1002 } else { // "\\lyxformat" not found
1003 WriteAlert(_("ERROR!"), _("Not a LyX file!"));
1006 WriteAlert(_("ERROR!"), _("Unable to read file!"));
1012 // Should probably be moved to somewhere else: BufferView? LyXView?
1013 bool Buffer::save(bool makeBackup) const
1015 // We don't need autosaves in the immediate future. (Asger)
1016 resetAutosaveTimers();
1020 string s = fileName() + '~';
1021 // Rename is the wrong way of making a backup,
1022 // this is the correct way.
1023 /* truss cp fil fil2:
1024 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
1025 stat("LyXVC.lyx", 0xEFFFF688) = 0
1026 open("LyXVC.lyx", O_RDONLY) = 3
1027 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
1028 fstat(4, 0xEFFFF508) = 0
1029 fstat(3, 0xEFFFF508) = 0
1030 read(3, " # T h i s f i l e w".., 8192) = 5579
1031 write(4, " # T h i s f i l e w".., 5579) = 5579
1032 read(3, 0xEFFFD4A0, 8192) = 0
1035 chmod("LyXVC3.lyx", 0100644) = 0
1036 lseek(0, 0, SEEK_CUR) = 46440
1040 // Should proabaly have some more error checking here.
1041 // Should be cleaned up in 0.13, at least a bit.
1042 // Doing it this way, also makes the inodes stay the same.
1043 // This is still not a very good solution, in particular we
1044 // might loose the owner of the backup.
1045 FileInfo finfo(fileName());
1046 if (finfo.exist()) {
1047 mode_t fmode = finfo.getMode();
1048 struct utimbuf * times = new struct utimbuf;
1050 times->actime = finfo.getAccessTime();
1051 times->modtime = finfo.getModificationTime();
1052 ifstream ifs(fileName().c_str());
1053 ofstream ofs(s.c_str(), ios::out|ios::trunc);
1058 ::chmod(s.c_str(), fmode);
1060 if (::utime(s.c_str(), times)) {
1061 lyxerr << "utime error." << endl;
1064 lyxerr << "LyX was not able to make "
1065 "backupcopy. Beware." << endl;
1071 if (writeFile(fileName(), false)) {
1074 // now delete the autosavefile
1075 string a = OnlyPath(fileName());
1077 a += OnlyFilename(fileName());
1079 FileInfo fileinfo(a);
1080 if (fileinfo.exist()) {
1081 if (::remove(a.c_str()) != 0) {
1082 WriteFSAlert(_("Could not delete "
1083 "auto-save file!"), a);
1087 // Saving failed, so backup is not backup
1089 string s = fileName() + '~';
1090 ::rename(s.c_str(), fileName().c_str());
1098 // Returns false if unsuccesful
1099 bool Buffer::writeFile(string const & fname, bool flag) const
1101 // if flag is false writeFile will not create any GUI
1102 // warnings, only cerr.
1103 // Needed for autosave in background or panic save (Matthias 120496)
1105 if (read_only && (fname == filename)) {
1106 // Here we should come with a question if we should
1107 // perform the write anyway.
1109 lyxerr << _("Error! Document is read-only: ")
1112 WriteAlert(_("Error! Document is read-only: "),
1117 FileInfo finfo(fname);
1118 if (finfo.exist() && !finfo.writable()) {
1119 // Here we should come with a question if we should
1120 // try to do the save anyway. (i.e. do a chmod first)
1122 lyxerr << _("Error! Cannot write file: ")
1125 WriteFSAlert(_("Error! Cannot write file: "),
1130 ofstream ofs(fname.c_str());
1133 lyxerr << _("Error! Cannot open file: ")
1136 WriteFSAlert(_("Error! Cannot open file: "),
1140 // The top of the file should not be written by params.
1142 // write out a comment in the top of the file
1143 ofs << '#' << LYX_DOCVERSION
1144 << " created this file. For more info see http://www.lyx.org/\n";
1145 ofs.setf(ios::showpoint|ios::fixed);
1147 ofs << "\\lyxformat " << setw(4) << LYX_FORMAT << "\n";
1149 // now write out the buffer paramters.
1150 params.writeFile(ofs);
1152 char footnoteflag = 0;
1155 // this will write out all the paragraphs
1156 // using recursive descent.
1157 paragraph->writeFile(ofs, params, footnoteflag, depth);
1159 // Write marker that shows file is complete
1160 ofs << "\n\\the_end" << endl;
1162 // how to check if close went ok?
1167 void Buffer::writeFileAscii(string const & fname, int linelen)
1169 LyXFont font1, font2;
1171 char c, footnoteflag = 0, depth = 0;
1173 LyXParagraph::size_type i;
1174 int j, h, ltype = 0, ltype_depth = 0,
1175 * clen = 0, actcell = 0, actpos = 0, cell = 0, cells = 0,
1178 bool ref_printed = false;
1180 ofstream ofs(fname.c_str());
1182 WriteFSAlert(_("Error: Cannot write file:"), fname);
1186 string fname1 = TmpFileName();
1187 LyXParagraph * par = paragraph;
1191 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE ||
1193 par->previous->footnoteflag == LyXParagraph::NO_FOOTNOTE){
1195 /* begins a footnote environment ? */
1196 if (footnoteflag != par->footnoteflag) {
1197 footnoteflag = par->footnoteflag;
1199 j = strlen(string_footnotekinds[par->footnotekind])+4;
1200 if (currlinelen + j > linelen)
1203 << string_footnotekinds[par->footnotekind] << "] ";
1208 /* begins or ends a deeper area ?*/
1209 if (depth != par->depth) {
1210 if (par->depth > depth) {
1211 while (par->depth > depth) {
1216 while (par->depth < depth) {
1222 /* First write the layout */
1223 tmp = textclasslist.NameOfLayout(params.textclass, par->layout);
1224 if (tmp == "Itemize") {
1226 ltype_depth = depth+1;
1227 } else if (tmp == "Enumerate") {
1229 ltype_depth = depth+1;
1230 } else if (strstr(tmp.c_str(), "ection")) {
1232 ltype_depth = depth+1;
1233 } else if (strstr(tmp.c_str(), "aragraph")) {
1235 ltype_depth = depth+1;
1236 } else if (tmp == "Description") {
1238 ltype_depth = depth+1;
1239 } else if (tmp == "Abstract") {
1242 } else if (tmp == "Bibliography") {
1250 /* maybe some vertical spaces */
1252 /* the labelwidthstring used in lists */
1256 /* some pagebreaks? */
1260 /* what about the alignment */
1263 /* dummy layout, that means a footnote ended */
1264 footnoteflag = LyXParagraph::NO_FOOTNOTE;
1269 /* It might be a table */
1272 if (!lyxrc->ascii_roff_command.empty() &&
1273 lyxrc->ascii_roff_command != "none") {
1274 RoffAsciiTable(ofs, par);
1281 cells = par->table->columns;
1282 clen = new int [cells];
1283 memset(clen, 0, sizeof(int) * cells);
1284 for (i = 0, j = 0, h = 1; i < par->size(); ++i, ++h) {
1285 c = par->GetChar(i);
1286 if (c == LyXParagraph::META_INSET) {
1287 if ((inset = par->GetInset(i))) {
1291 inset->Latex(ost, -1);
1292 h += ost.str().length();
1295 inset->Latex(ost, -1);
1297 char * tmp = ost.str();
1303 ofstream fs(fname1.c_str());
1305 WriteFSAlert(_("Error: Cannot open temporary file:"), fname1);
1308 inset->Latex(fs, -1);
1309 h += fs.tellp() - 1;
1310 ::remove(fname1.c_str());
1313 } else if (c == LyXParagraph::META_NEWLINE) {
1317 j = (++j) % par->table->NumberOfCellsInRow(actcell);
1325 font1 = LyXFont(LyXFont::ALL_INHERIT);
1327 for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
1328 if (!i && !footnoteflag && !noparbreak){
1330 for(j = 0; j < depth; ++j)
1332 currlinelen = depth * 2;
1334 case 0: /* Standard */
1335 case 4: /* (Sub)Paragraph */
1336 case 5: /* Description */
1338 case 6: /* Abstract */
1339 ofs << "Abstract\n\n";
1341 case 7: /* Bibliography */
1343 ofs << "References\n\n";
1348 ofs << par->labelstring << " ";
1351 if (ltype_depth > depth) {
1352 for(j = ltype_depth - 1; j > depth; --j)
1354 currlinelen += (ltype_depth-depth)*2;
1357 for(j = 0; j < cells; ++j) {
1359 for(h = 0; h < (clen[j] + 1);
1364 for(j = 0; j < depth; ++j)
1366 currlinelen = depth * 2;
1367 if (ltype_depth > depth) {
1368 for(j = ltype_depth;
1371 currlinelen += (ltype_depth-depth)*2;
1376 font2 = par->GetFontSettings(i);
1377 if (font1.latex() != font2.latex()) {
1378 if (font2.latex() == LyXFont::OFF)
1386 c = par->GetChar(i);
1390 case LyXParagraph::META_INSET:
1391 if ((inset = par->GetInset(i))) {
1393 inset->Latex(ofs, -1);
1394 currlinelen += (ofs.tellp() - fpos);
1395 actpos += (ofs.tellp() - fpos) - 1;
1398 case LyXParagraph::META_NEWLINE:
1400 if (par->table->NumberOfCellsInRow(actcell) <= cell) {
1401 for(j = actpos; j < clen[cell - 1]; ++j)
1404 for(j = 0; j < depth; ++j)
1406 currlinelen = depth*2;
1407 if (ltype_depth > depth) {
1408 for(j = ltype_depth; j > depth; --j)
1410 currlinelen += (ltype_depth-depth) * 2;
1412 for(j = 0; j < cells; ++j) {
1414 for(h = 0; h < (clen[j] + 1); ++h)
1418 for(j = 0; j < depth; ++j)
1420 currlinelen = depth * 2;
1421 if (ltype_depth > depth) {
1422 for(j = ltype_depth;
1425 currlinelen += (ltype_depth-depth)*2;
1431 j < clen[cell - 1]; ++j)
1437 currlinelen = actpos = 0;
1440 for(j = 0; j < depth; ++j)
1442 currlinelen = depth * 2;
1443 if (ltype_depth > depth) {
1444 for(j = ltype_depth;
1447 currlinelen += (ltype_depth - depth) * 2;
1451 case LyXParagraph::META_HFILL:
1455 case LyXParagraph::META_PROTECTED_SEPARATOR:
1463 if (currlinelen > linelen - 10
1464 && c == ' ' && i + 2 < par->size()) {
1466 for(j = 0; j < depth; ++j)
1468 currlinelen = depth * 2;
1469 if (ltype_depth > depth) {
1470 for(j = ltype_depth;
1473 currlinelen += (ltype_depth-depth)*2;
1475 } else if (c != '\0')
1478 lyxerr.debug() << "writeAsciiFile: NULL char in structure." << endl;
1484 for(j = actpos; j < clen[cell - 1]; ++j)
1487 for(j = 0; j < depth; ++j)
1489 currlinelen = depth * 2;
1490 if (ltype_depth > depth) {
1491 for(j = ltype_depth; j > depth; --j)
1493 currlinelen += (ltype_depth - depth) * 2;
1495 for(j = 0; j < cells; ++j) {
1497 for(h = 0; h < (clen[j] + 1); ++h)
1510 void Buffer::makeLaTeXFile(string const & fname,
1511 string const & original_path,
1512 bool nice, bool only_body)
1514 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1516 niceFile = nice; // this will be used by Insetincludes.
1518 tex_code_break_column = lyxrc->ascii_linelen;
1520 LyXTextClass const & tclass =
1521 textclasslist.TextClass(params.textclass);
1523 ofstream ofs(fname.c_str());
1525 WriteFSAlert(_("Error: Cannot open file: "), fname);
1529 // validate the buffer.
1530 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1531 LaTeXFeatures features(tclass.numLayouts());
1533 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1536 // The starting paragraph of the coming rows is the
1537 // first paragraph of the document. (Asger)
1538 texrow.start(paragraph, 0);
1542 if (!only_body && nice) {
1543 LFile += "%% " LYX_DOCVERSION " created this file. "
1544 "For more info, see http://www.lyx.org/.\n";
1545 LFile += "%% Do not edit unless you really know what you are doing.\n";
1549 lyxerr.debug() << "lyx header finished" << endl;
1550 // There are a few differences between nice LaTeX and usual files:
1551 // usual is \batchmode and has a
1552 // special input@path to allow the including of figures
1553 // with either \input or \includegraphics (what figinsets do).
1554 // batchmode is not set if there is a tex_code_break_column.
1555 // In this case somebody is interested in the generated LaTeX,
1556 // so this is OK. input@path is set when the actual parameter
1557 // original_path is set. This is done for usual tex-file, but not
1558 // for nice-latex-file. (Matthias 250696)
1561 // code for usual, NOT nice-latex-file
1562 LFile += "\\batchmode\n"; // changed
1563 // from \nonstopmode
1566 if (!original_path.empty()) {
1567 LFile += "\\makeatletter\n";
1569 LFile += "\\def\\input@path{{" + original_path
1572 LFile += "\\makeatother\n";
1576 LFile += "\\documentclass";
1578 string options; // the document class options.
1580 if (tokenPos(tclass.opt_fontsize(), '|', params.fontsize) >= 0) {
1581 // only write if existing in list (and not default)
1582 options += params.fontsize;
1587 if (!params.use_geometry &&
1588 (params.paperpackage == BufferParams::PACKAGE_NONE)) {
1589 switch (params.papersize) {
1590 case BufferParams::PAPER_A4PAPER:
1591 options += "a4paper,";
1593 case BufferParams::PAPER_USLETTER:
1594 options += "letterpaper,";
1596 case BufferParams::PAPER_A5PAPER:
1597 options += "a5paper,";
1599 case BufferParams::PAPER_B5PAPER:
1600 options += "b5paper,";
1602 case BufferParams::PAPER_EXECUTIVEPAPER:
1603 options += "executivepaper,";
1605 case BufferParams::PAPER_LEGALPAPER:
1606 options += "legalpaper,";
1612 if (params.sides != tclass.sides()) {
1613 switch (params.sides) {
1614 case LyXTextClass::OneSide:
1615 options += "oneside,";
1617 case LyXTextClass::TwoSides:
1618 options += "twoside,";
1625 if (params.columns != tclass.columns()) {
1626 if (params.columns == 2)
1627 options += "twocolumn,";
1629 options += "onecolumn,";
1632 if (!params.use_geometry
1633 && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1634 options += "landscape,";
1636 // language should be a parameter to \documentclass
1637 if (params.language != "default") {
1638 if (params.language == "hebrew")
1639 options += "english,";
1640 else if (lyxrc->rtl_support)
1641 options += "hebrew,";
1642 options += params.language + ',';
1643 } else if (lyxrc->rtl_support)
1644 options += "hebrew,english,";
1646 // the user-defined options
1647 if (!params.options.empty()) {
1648 options += params.options + ',';
1651 if (!options.empty()){
1652 options = strip(options, ',');
1659 LFile += textclasslist.LatexnameOfClass(params.textclass);
1662 // end of \documentclass defs
1664 // font selection must be done before loading fontenc.sty
1665 if (params.fonts != "default") {
1666 LFile += "\\usepackage{" + params.fonts + "}\n";
1669 // this one is not per buffer
1670 if (lyxrc->fontenc != "default") {
1671 LFile += "\\usepackage[" + lyxrc->fontenc
1675 if (params.inputenc != "default") {
1676 LFile += "\\usepackage[" + params.inputenc
1681 /* at the very beginning the text parameters */
1682 if (params.paperpackage != BufferParams::PACKAGE_NONE) {
1683 switch (params.paperpackage) {
1684 case BufferParams::PACKAGE_A4:
1685 LFile += "\\usepackage{a4}\n";
1688 case BufferParams::PACKAGE_A4WIDE:
1689 LFile += "\\usepackage{a4wide}\n";
1692 case BufferParams::PACKAGE_WIDEMARGINSA4:
1693 LFile += "\\usepackage[widemargins]{a4}\n";
1698 if (params.use_geometry) {
1699 LFile += "\\usepackage{geometry}\n";
1701 LFile += "\\geometry{verbose";
1702 if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1703 LFile += ",landscape";
1704 switch (params.papersize2) {
1705 case BufferParams::VM_PAPER_CUSTOM:
1706 if (!params.paperwidth.empty())
1707 LFile += ",paperwidth="
1708 + params.paperwidth;
1709 if (!params.paperheight.empty())
1710 LFile += ",paperheight="
1711 + params.paperheight;
1713 case BufferParams::VM_PAPER_USLETTER:
1714 LFile += ",letterpaper";
1716 case BufferParams::VM_PAPER_USLEGAL:
1717 LFile += ",legalpaper";
1719 case BufferParams::VM_PAPER_USEXECUTIVE:
1720 LFile += ",executivepaper";
1722 case BufferParams::VM_PAPER_A3:
1723 LFile += ",a3paper";
1725 case BufferParams::VM_PAPER_A4:
1726 LFile += ",a4paper";
1728 case BufferParams::VM_PAPER_A5:
1729 LFile += ",a5paper";
1731 case BufferParams::VM_PAPER_B3:
1732 LFile += ",b3paper";
1734 case BufferParams::VM_PAPER_B4:
1735 LFile += ",b4paper";
1737 case BufferParams::VM_PAPER_B5:
1738 LFile += ",b5paper";
1741 // default papersize ie BufferParams::VM_PAPER_DEFAULT
1742 switch (lyxrc->default_papersize) {
1743 case BufferParams::PAPER_DEFAULT: // keep compiler happy
1744 case BufferParams::PAPER_USLETTER:
1745 LFile += ",letterpaper";
1747 case BufferParams::PAPER_LEGALPAPER:
1748 LFile += ",legalpaper";
1750 case BufferParams::PAPER_EXECUTIVEPAPER:
1751 LFile += ",executivepaper";
1753 case BufferParams::PAPER_A3PAPER:
1754 LFile += ",a3paper";
1756 case BufferParams::PAPER_A4PAPER:
1757 LFile += ",a4paper";
1759 case BufferParams::PAPER_A5PAPER:
1760 LFile += ",a5paper";
1762 case BufferParams::PAPER_B5PAPER:
1763 LFile += ",b5paper";
1767 if (!params.topmargin.empty())
1768 LFile += ",tmargin=" + params.topmargin;
1769 if (!params.bottommargin.empty())
1770 LFile += ",bmargin=" + params.bottommargin;
1771 if (!params.leftmargin.empty())
1772 LFile += ",lmargin=" + params.leftmargin;
1773 if (!params.rightmargin.empty())
1774 LFile += ",rmargin=" + params.rightmargin;
1775 if (!params.headheight.empty())
1776 LFile += ",headheight=" + params.headheight;
1777 if (!params.headsep.empty())
1778 LFile += ",headsep=" + params.headsep;
1779 if (!params.footskip.empty())
1780 LFile += ",footskip=" + params.footskip;
1784 if (params.use_amsmath
1785 && !prefixIs(textclasslist.LatexnameOfClass(params.textclass), "ams")) {
1786 LFile += "\\usepackage{amsmath}\n";
1789 if (tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle) >= 0) {
1790 if (params.pagestyle == "fancy") {
1791 LFile += "\\usepackage{fancyhdr}\n";
1794 LFile += "\\pagestyle{" + params.pagestyle + "}\n";
1798 // We try to load babel late, in case it interferes
1799 // with other packages.
1800 if (params.language != "default" || lyxrc->rtl_support ) {
1801 LFile += "\\usepackage{babel}\n";
1805 if (params.secnumdepth != tclass.secnumdepth()) {
1806 LFile += "\\setcounter{secnumdepth}{";
1807 LFile += tostr(params.secnumdepth);
1811 if (params.tocdepth != tclass.tocdepth()) {
1812 LFile += "\\setcounter{tocdepth}{";
1813 LFile += tostr(params.tocdepth);
1818 if (params.paragraph_separation) {
1819 switch (params.defskip.kind()) {
1820 case VSpace::SMALLSKIP:
1821 LFile += "\\setlength\\parskip{\\smallskipamount}\n";
1823 case VSpace::MEDSKIP:
1824 LFile += "\\setlength\\parskip{\\medskipamount}\n";
1826 case VSpace::BIGSKIP:
1827 LFile += "\\setlength\\parskip{\\bigskipamount}\n";
1829 case VSpace::LENGTH:
1830 LFile += "\\setlength\\parskip{"
1831 + params.defskip.length().asLatexString()
1834 default: // should never happen // Then delete it.
1835 LFile += "\\setlength\\parskip{\\medskipamount}\n";
1840 LFile += "\\setlength\\parindent{0pt}\n";
1844 // Write out what we've generated so far...and reset LFile
1848 // Now insert the LyX specific LaTeX commands...
1849 string preamble, tmppreamble;
1851 // The optional packages;
1852 preamble = features.getPackages(params);
1854 // this might be useful...
1855 preamble += "\n\\makeatletter\n\n";
1857 // Some macros LyX will need
1858 tmppreamble = features.getMacros(params);
1860 if (!tmppreamble.empty()) {
1861 preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1862 "LyX specific LaTeX commands.\n"
1863 + tmppreamble + '\n';
1866 // the text class specific preamble
1867 tmppreamble = features.getTClassPreamble(params);
1868 if (!tmppreamble.empty()) {
1869 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1870 "Textclass specific LaTeX commands.\n"
1871 + tmppreamble + '\n';
1874 /* the user-defined preamble */
1875 if (!params.preamble.empty()) {
1876 preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1877 "User specified LaTeX commands.\n"
1878 + params.preamble + '\n';
1881 preamble += "\\makeatother\n\n";
1883 // Itemize bullet settings need to be last in case the user
1884 // defines their own bullets that use a package included
1885 // in the user-defined preamble -- ARRae
1886 // Actually it has to be done much later than that
1887 // since some packages like frenchb make modifications
1888 // at \begin{document} time -- JMarc
1890 for (int i = 0; i < 4; ++i) {
1891 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
1892 if (bullets_def.empty())
1893 bullets_def="\\AtBeginDocument{\n";
1894 bullets_def += " \\renewcommand{\\labelitemi";
1896 // `i' is one less than the item to modify
1903 bullets_def += "ii";
1909 bullets_def += "}{" +
1910 params.user_defined_bullets[i].getText()
1915 if (!bullets_def.empty())
1916 preamble += bullets_def + "}\n\n";
1918 for (int j = countChar(preamble, '\n'); j-- ;) {
1925 LFile += "\\begin{document}\n\n";
1929 lyxerr.debug() << "preamble finished, now the body." << endl;
1931 bool was_title = false;
1932 bool already_title = false;
1938 LyXParagraph * par = paragraph;
1944 lyxerr[Debug::LATEX] << "Error in MakeLateXFile."
1946 LyXLayout const & layout =
1947 textclasslist.Style(params.textclass,
1950 if (layout.intitle) {
1951 if (already_title) {
1952 lyxerr <<"Error in MakeLatexFile: You"
1953 " should not mix title layouts"
1954 " with normal ones." << endl;
1957 } else if (was_title && !already_title) {
1958 LFile += "\\maketitle\n";
1960 already_title = true;
1963 // We are at depth 0 so we can just use
1964 // ordinary \footnote{} generation
1965 // flag this with ftcount
1967 if (layout.isEnvironment()
1968 || par->pextra_type != LyXParagraph::PEXTRA_NONE) {
1969 par = par->TeXEnvironment(LFile, texrow,
1970 ftnote, ft_texrow, ftcount);
1972 par = par->TeXOnePar(LFile, texrow,
1973 ftnote, ft_texrow, ftcount);
1976 // Write out what we've generated...and reset LFile
1979 LFile += "\\addtocounter{footnote}{-";
1980 LFile += tostr(ftcount - 1);
1984 texrow += ft_texrow;
1989 if (loop_count == 2) {
1990 // fwrite()ing every second time through the loop
1991 // gains a few extra % of speed; going higher than
1992 // 2 will slow things down again. I'll look at
1993 // LFile.length() in a future revision. ARRae
2000 // It might be that we only have a title in this document
2001 if (was_title && !already_title) {
2002 LFile += "\\maketitle\n";
2007 LFile += "\\end{document}\n";
2010 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
2012 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
2016 // Just to be sure. (Asger)
2019 // Write out what we've generated...and reset LFile
2023 // tex_code_break_column's value is used to decide
2024 // if we are in batchmode or not (within mathed_write()
2025 // in math_write.C) so we must set it to a non-zero
2026 // value when we leave otherwise we save incorrect .lyx files.
2027 tex_code_break_column = lyxrc->ascii_linelen;
2029 // How to check if the close went ok when using fstreams
2031 lyxerr.debug() << "Finished making latex file." << endl;
2035 bool Buffer::isLatex() const
2037 return textclasslist.TextClass(params.textclass).outputType() == LATEX;
2041 bool Buffer::isLinuxDoc() const
2043 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC;
2047 bool Buffer::isLiterate() const
2049 return textclasslist.TextClass(params.textclass).outputType() == LITERATE;
2053 bool Buffer::isDocBook() const
2055 return textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2059 bool Buffer::isSGML() const
2061 return textclasslist.TextClass(params.textclass).outputType() == LINUXDOC ||
2062 textclasslist.TextClass(params.textclass).outputType() == DOCBOOK;
2066 void Buffer::sgmlOpenTag(ostream & os, int depth,
2067 string const & latexname) const
2069 os << string(depth, ' ') << "<" << latexname << ">\n";
2073 void Buffer::sgmlCloseTag(ostream & os, int depth,
2074 string const & latexname) const
2076 os << string(depth, ' ') << "</" << latexname << ">\n";
2080 void Buffer::makeLinuxDocFile(string const & fname, int column)
2082 LyXParagraph * par = paragraph;
2084 string top_element = textclasslist.LatexnameOfClass(params.textclass);
2085 string environment_stack[10];
2088 int depth = 0; /* paragraph depth */
2090 ofstream ofs(fname.c_str());
2093 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2097 tex_code_break_column = column;
2100 if (params.preamble.empty()) {
2101 ofs << "<!doctype linuxdoc system>\n\n";
2104 ofs << "<!doctype linuxdoc system \n [ "
2105 << params.preamble << " \n]>\n\n";
2108 ofs << "<!-- " << LYX_DOCVERSION
2109 << " created this file. For more info see http://www.lyx.org/"
2112 if(params.options.empty())
2113 sgmlOpenTag(ofs, 0, top_element);
2115 string top = top_element;
2117 top += params.options;
2118 sgmlOpenTag(ofs, 0, top);
2122 int desc_on = 0; /* description mode*/
2123 LyXLayout const & style =
2124 textclasslist.Style(users->buffer()->params.textclass,
2126 par->AutoDeleteInsets();
2128 /* treat <toc> as a special case for compatibility with old code */
2129 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2130 Inset * inset = par->GetInset(0);
2131 char lyx_code = inset->LyxCode();
2132 if (lyx_code == Inset::TOC_CODE){
2133 string temp = "toc";
2134 sgmlOpenTag(ofs, depth, temp);
2137 linuxDocHandleFootnote(ofs, par, depth);
2142 // environment tag closing
2143 for( ; depth > par->depth; --depth) {
2144 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2145 environment_stack[depth].clear();
2148 // write opening SGML tags
2149 switch(style.latextype) {
2150 case LATEX_PARAGRAPH:
2151 if(depth == par->depth
2152 && !environment_stack[depth].empty()) {
2153 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2154 environment_stack[depth].clear();
2160 sgmlOpenTag(ofs, depth, style.latexname());
2165 LinuxDocError(par, 0, _("Error : Wrong depth for LatexType Command.\n"));
2167 if (!environment_stack[depth].empty()){
2168 sgmlCloseTag(ofs, depth,
2169 environment_stack[depth]);
2173 environment_stack[depth].clear();
2174 sgmlOpenTag(ofs, depth, style.latexname());
2177 case LATEX_ENVIRONMENT:
2178 case LATEX_ITEM_ENVIRONMENT:
2179 if(depth == par->depth
2180 && environment_stack[depth] != style.latexname()
2181 && !environment_stack[depth].empty()) {
2183 sgmlCloseTag(ofs, depth,
2184 environment_stack[depth]);
2185 environment_stack[depth].clear();
2187 if (depth < par->depth) {
2189 environment_stack[depth].clear();
2191 if (environment_stack[depth] != style.latexname()) {
2194 sgmlOpenTag(ofs, depth, temp);
2196 environment_stack[depth] = style.latexname();
2197 sgmlOpenTag(ofs, depth, environment_stack[depth]);
2199 if(style.latextype == LATEX_ENVIRONMENT) break;
2201 desc_on = (style.labeltype == LABEL_MANUAL);
2208 sgmlOpenTag(ofs, depth + 1, item_name);
2211 sgmlOpenTag(ofs, depth, style.latexname());
2216 SimpleLinuxDocOnePar(ofs, par, desc_on, depth);
2219 linuxDocHandleFootnote(ofs, par, depth);
2221 while(par && par->IsDummy());
2224 /* write closing SGML tags */
2225 switch(style.latextype) {
2227 case LATEX_ENVIRONMENT:
2228 case LATEX_ITEM_ENVIRONMENT:
2231 sgmlCloseTag(ofs, depth, style.latexname());
2236 /* Close open tags */
2237 for(; depth > 0; --depth)
2238 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2240 if(!environment_stack[depth].empty())
2241 sgmlCloseTag(ofs, depth, environment_stack[depth]);
2244 sgmlCloseTag(ofs, 0, top_element);
2247 // How to check for successful close
2251 void Buffer::linuxDocHandleFootnote(ostream & os, LyXParagraph * & par,
2254 string tag = "footnote";
2256 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2257 sgmlOpenTag(os, depth + 1, tag);
2258 SimpleLinuxDocOnePar(os, par, 0, depth + 1);
2259 sgmlCloseTag(os, depth + 1, tag);
2265 void Buffer::DocBookHandleCaption(ostream & os, string & inner_tag,
2266 int const depth, int desc_on,
2267 LyXParagraph * & par)
2269 LyXParagraph * tpar = par;
2270 string tmp_par, extra_par;
2271 while (tpar && (tpar->footnoteflag != LyXParagraph::NO_FOOTNOTE) &&
2272 (tpar->layout != textclasslist.NumberOfLayout(params.textclass,
2276 tpar->layout == textclasslist.NumberOfLayout(params.textclass,
2277 "Caption").second) {
2278 sgmlOpenTag(os, depth + 1, inner_tag);
2279 SimpleDocBookOnePar(tmp_par, extra_par, tpar,
2280 desc_on, depth + 2);
2281 tmp_par = strip(tmp_par);
2282 tmp_par = frontStrip(tmp_par);
2284 sgmlCloseTag(os, depth+1, inner_tag);
2285 if(!extra_par.empty())
2291 void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
2294 string tag, inner_tag;
2295 string tmp_par, extra_par;
2296 bool inner_span = false;
2299 // This is not how I like to see enums. They should not be anonymous
2300 // and variables of its type should not be declared right after the
2301 // last brace. (Lgb)
2308 } last = NO_ONE, present = FOOTNOTE_LIKE;
2310 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
2311 if(last == present) {
2313 if(!tmp_par.empty()) {
2316 sgmlCloseTag(os, depth+1, inner_tag);
2317 sgmlOpenTag(os, depth+1, inner_tag);
2324 if(!inner_tag.empty()) sgmlCloseTag(os, depth+1, inner_tag);
2325 if(!extra_par.empty()) os << extra_par;
2326 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2329 switch (par->footnotekind) {
2330 case LyXParagraph::FOOTNOTE:
2331 case LyXParagraph::ALGORITHM:
2334 present = FOOTNOTE_LIKE;
2337 case LyXParagraph::MARGIN:
2340 present = MARGIN_LIKE;
2343 case LyXParagraph::FIG:
2344 case LyXParagraph::WIDE_FIG:
2346 inner_tag = "title";
2350 case LyXParagraph::TAB:
2351 case LyXParagraph::WIDE_TAB:
2353 inner_tag = "title";
2358 sgmlOpenTag(os, depth, tag);
2359 if ((present == TAB_LIKE) || (present == FIG_LIKE)) {
2360 DocBookHandleCaption(os, inner_tag, depth,
2364 sgmlOpenTag(os, depth + 1, inner_tag);
2367 // ignore all caption here, we processed them above!!!
2368 if (par->layout != textclasslist.NumberOfLayout(params.textclass,
2369 "Caption").second) {
2370 SimpleDocBookOnePar(tmp_par, extra_par, par,
2371 desc_on, depth + 2);
2373 tmp_par = frontStrip(strip(tmp_par));
2379 if(!inner_tag.empty()) sgmlCloseTag(os, depth+1, inner_tag);
2380 if(!extra_par.empty()) os << extra_par;
2381 if(!tag.empty()) sgmlCloseTag(os, depth, tag);
2385 /* push a tag in a style stack */
2386 void Buffer::push_tag(ostream & os, char const * tag,
2387 int & pos, char stack[5][3])
2389 /* pop all previous tags */
2390 for (int j = pos; j >= 0; --j)
2391 os << "</" << stack[j] << ">";
2394 sprintf(stack[++pos], "%s", tag);
2397 for (int i = 0; i <= pos; ++i)
2398 os << "<" << stack[i] << ">";
2401 void Buffer::pop_tag(ostream & os, char const * tag,
2402 int & pos, char stack[5][3])
2406 // pop all tags till specified one
2407 for (j = pos; (j >= 0) && (strcmp(stack[j], tag)); --j)
2408 os << "</" << stack[j] << ">";
2411 os << "</" << tag << ">";
2413 // push all tags, but the specified one
2414 for (j = j + 1; j <= pos; ++j) {
2415 os << "<" << stack[j] << ">";
2416 strcpy(stack[j-1], stack[j]);
2422 /* handle internal paragraph parsing -- layout already processed */
2424 // checks, if newcol chars should be put into this line
2425 // writes newline, if necessary.
2427 void linux_doc_line_break(ostream & os, unsigned int & colcount,
2428 const unsigned int newcol)
2431 if (colcount > lyxrc->ascii_linelen) {
2433 colcount = newcol; // assume write after this call
2438 void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
2439 int desc_on, int const /*depth*/)
2441 LyXFont font1, font2;
2444 LyXParagraph::size_type main_body;
2446 LyXLayout const & style = textclasslist.Style(params.textclass,
2449 char family_type = 0; // family font flag
2450 bool is_bold = false; // series font flag
2451 char shape_type = 0; // shape font flag
2452 bool is_em = false; // emphasis (italic) font flag
2454 int stack_num = -1; // style stack position
2455 char stack[5][3]; // style stack
2456 unsigned int char_line_count = 5; // Heuristic choice ;-)
2458 if (style.labeltype != LABEL_MANUAL)
2461 main_body = par->BeginningOfMainBody();
2463 /* gets paragraph main font */
2465 font1 = style.labelfont;
2470 /* parsing main loop */
2471 for (LyXParagraph::size_type i = 0;
2472 i < par->size(); ++i) {
2474 /* handle quote tag */
2475 if (i == main_body && !par->IsDummy()) {
2480 font2 = par->getFont(i);
2482 if (font1.family() != font2.family()) {
2483 switch(family_type) {
2485 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
2486 push_tag(os, "tt", stack_num, stack);
2489 else if (font2.family() == LyXFont::SANS_FAMILY) {
2490 push_tag(os, "sf", stack_num, stack);
2495 pop_tag(os, "tt", stack_num, stack);
2496 if (font2.family() == LyXFont::SANS_FAMILY) {
2497 push_tag(os, "sf", stack_num, stack);
2505 pop_tag(os, "sf", stack_num, stack);
2506 if (font2.family() == LyXFont::TYPEWRITER_FAMILY) {
2507 push_tag(os, "tt", stack_num, stack);
2516 /* handle bold face */
2517 if (font1.series() != font2.series()) {
2518 if (font2.series() == LyXFont::BOLD_SERIES) {
2519 push_tag(os, "bf", stack_num, stack);
2523 pop_tag(os, "bf", stack_num, stack);
2528 /* handle italic and slanted fonts */
2529 if (font1.shape() != font2.shape()) {
2530 switch(shape_type) {
2532 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
2533 push_tag(os, "it", stack_num, stack);
2536 else if (font2.shape() == LyXFont::SLANTED_SHAPE) {
2537 push_tag(os, "sl", stack_num, stack);
2542 pop_tag(os, "it", stack_num, stack);
2543 if (font2.shape() == LyXFont::SLANTED_SHAPE) {
2544 push_tag(os, "sl", stack_num, stack);
2552 pop_tag(os, "sl", stack_num, stack);
2553 if (font2.shape() == LyXFont::ITALIC_SHAPE) {
2554 push_tag(os, "it", stack_num, stack);
2562 /* handle <em> tag */
2563 if (font1.emph() != font2.emph()) {
2564 if (font2.emph() == LyXFont::ON) {
2565 push_tag(os, "em", stack_num, stack);
2568 pop_tag(os, "em", stack_num, stack);
2573 c = par->GetChar(i);
2575 if (font2.latex() == LyXFont::ON) {
2576 // "TeX"-Mode on == > SGML-Mode on.
2578 os << c; // see LaTeX-Generation...
2580 } else if (c == LyXParagraph::META_INSET) {
2581 inset = par->GetInset(i);
2583 inset->Linuxdoc(tmp_out);
2588 if (par->linuxDocConvertChar(c, sgml_string)
2589 && !style.free_spacing) { // in freespacing
2591 // non-breaking characters
2595 linux_doc_line_break(os, char_line_count, 6);
2600 linux_doc_line_break(os, char_line_count, 1);
2606 char_line_count += sgml_string.length();
2612 /* needed if there is an optional argument but no contents */
2613 if (main_body > 0 && main_body == par->size()) {
2617 /* pop all defined Styles */
2618 for (j = stack_num; j >= 0; --j) {
2619 linux_doc_line_break(os,
2621 3 + strlen(stack[j]));
2622 os << "</" << stack[j] << ">";
2625 /* resets description flag correctly */
2628 /* <tag> not closed... */
2629 linux_doc_line_break(os, char_line_count, 6);
2633 /* fprintf(file, "</p>");*/
2639 /* print an error message */
2640 void Buffer::LinuxDocError(LyXParagraph * par, int pos,
2641 char const * message)
2643 InsetError * new_inset;
2645 /* insert an error marker in text */
2646 new_inset = new InsetError(message);
2647 par->InsertChar(pos, LyXParagraph::META_INSET);
2648 par->InsertInset(pos, new_inset);
2651 // This constant defines the maximum number of
2652 // environment layouts that can be nesteded.
2653 // The same applies for command layouts.
2654 // These values should be more than enough.
2655 // José Matos (1999/07/22)
2657 enum { MAX_NEST_LEVEL = 25};
2659 void Buffer::makeDocBookFile(string const & fname, int column)
2661 LyXParagraph * par = paragraph;
2663 string top_element= textclasslist.LatexnameOfClass(params.textclass);
2664 string environment_stack[MAX_NEST_LEVEL];
2665 string environment_inner[MAX_NEST_LEVEL];
2666 string command_stack[MAX_NEST_LEVEL];
2667 bool command_flag= false;
2668 int command_depth= 0, command_base= 0, cmd_depth= 0;
2670 string item_name, command_name;
2671 string c_depth, c_params, tmps;
2673 int depth= 0; /* paragraph depth */
2675 tex_code_break_column = column;
2677 ofstream ofs(fname.c_str());
2679 WriteAlert(_("LYX_ERROR:"), _("Cannot write file"), fname);
2685 ofs << "<!doctype " << top_element
2686 << " public \"-//OASIS//DTD DocBook V3.1//EN\"";
2688 if (params.preamble.empty())
2691 ofs << "\n [ " << params.preamble << " \n]>\n\n";
2693 ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION
2694 << "\n See http://www.lyx.org/ for more information -->\n";
2696 if(params.options.empty())
2697 sgmlOpenTag(ofs, 0, top_element);
2699 string top = top_element;
2701 top += params.options;
2702 sgmlOpenTag(ofs, 0, top);
2706 int desc_on = 0; /* description mode*/
2707 LyXLayout const & style =
2708 textclasslist.Style(users->buffer()->params.textclass,
2710 par->AutoDeleteInsets();
2712 /* environment tag closing */
2713 for( ; depth > par->depth; --depth) {
2714 if(environment_inner[depth] != "!-- --") {
2715 item_name= "listitem";
2716 sgmlCloseTag(ofs, command_depth + depth,
2718 if( environment_inner[depth] == "varlistentry")
2719 sgmlCloseTag(ofs, depth+command_depth,
2720 environment_inner[depth]);
2722 sgmlCloseTag(ofs, depth + command_depth,
2723 environment_stack[depth]);
2724 environment_stack[depth].clear();
2725 environment_inner[depth].clear();
2728 if(depth == par->depth
2729 && environment_stack[depth] != style.latexname()
2730 && !environment_stack[depth].empty()) {
2731 if(environment_inner[depth] != "!-- --") {
2732 item_name= "listitem";
2733 sgmlCloseTag(ofs, command_depth+depth,
2735 if( environment_inner[depth] == "varlistentry")
2737 depth + command_depth,
2738 environment_inner[depth]);
2741 sgmlCloseTag(ofs, depth + command_depth,
2742 environment_stack[depth]);
2744 environment_stack[depth].clear();
2745 environment_inner[depth].clear();
2748 // Write opening SGML tags.
2749 switch(style.latextype) {
2750 case LATEX_PARAGRAPH:
2751 if(style.latexname() != "dummy")
2752 sgmlOpenTag(ofs, depth+command_depth,
2758 LinuxDocError(par, 0,
2759 _("Error : Wrong depth for "
2760 "LatexType Command.\n"));
2762 command_name = style.latexname();
2764 tmps = style.latexparam();
2765 c_params = split(tmps, c_depth,'|');
2767 cmd_depth= atoi(c_depth.c_str());
2770 if(cmd_depth<command_base) {
2771 for(int j = command_depth;
2772 j >= command_base; --j)
2773 if(!command_stack[j].empty())
2774 sgmlCloseTag(ofs, j, command_stack[j]);
2775 command_depth= command_base= cmd_depth;
2777 else if(cmd_depth <= command_depth) {
2778 for(int j = command_depth;
2779 j >= cmd_depth; --j)
2781 if(!command_stack[j].empty())
2782 sgmlCloseTag(ofs, j, command_stack[j]);
2783 command_depth= cmd_depth;
2786 command_depth= cmd_depth;
2789 command_depth = command_base = cmd_depth;
2790 command_flag = true;
2792 command_stack[command_depth]= command_name;
2794 // treat label as a special case for
2795 // more WYSIWYM handling.
2796 if (par->GetChar(0) == LyXParagraph::META_INSET) {
2797 Inset * inset = par->GetInset(0);
2798 char lyx_code = inset->LyxCode();
2799 if (lyx_code == Inset::LABEL_CODE){
2800 command_name += " id=\"";
2801 command_name += (static_cast<InsetCommand *>(inset))->getContents();
2802 command_name += "\"";
2807 sgmlOpenTag(ofs, depth+command_depth, command_name);
2808 item_name = "title";
2809 sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
2812 case LATEX_ENVIRONMENT:
2813 case LATEX_ITEM_ENVIRONMENT:
2814 if (depth < par->depth) {
2816 environment_stack[depth].clear();
2819 if (environment_stack[depth] != style.latexname()) {
2820 environment_stack[depth] = style.latexname();
2821 environment_inner[depth] = "!-- --";
2822 sgmlOpenTag(ofs, depth + command_depth,
2823 environment_stack[depth]);
2825 if(environment_inner[depth] != "!-- --") {
2826 item_name= "listitem";
2828 command_depth + depth,
2830 if (environment_inner[depth] == "varlistentry")
2832 depth + command_depth,
2833 environment_inner[depth]);
2837 if(style.latextype == LATEX_ENVIRONMENT) {
2838 if(!style.latexparam().empty())
2839 sgmlOpenTag(ofs, depth + command_depth,
2840 style.latexparam());
2844 desc_on = (style.labeltype == LABEL_MANUAL);
2847 environment_inner[depth]= "varlistentry";
2849 environment_inner[depth]= "listitem";
2851 sgmlOpenTag(ofs, depth + 1 + command_depth,
2852 environment_inner[depth]);
2856 sgmlOpenTag(ofs, depth + 1 + command_depth,
2861 sgmlOpenTag(ofs, depth + 1 + command_depth,
2866 sgmlOpenTag(ofs, depth + command_depth,
2872 string tmp_par, extra_par;
2874 SimpleDocBookOnePar(tmp_par, extra_par, par, desc_on,
2875 depth + 1 + command_depth);
2879 DocBookHandleFootnote(ofs, par,
2880 depth + 1 + command_depth);
2882 while(par && par->IsDummy());
2885 /* write closing SGML tags */
2886 switch(style.latextype) {
2889 sgmlCloseTag(ofs, depth + command_depth, end_tag);
2891 case LATEX_ENVIRONMENT:
2892 if(!style.latexparam().empty())
2893 sgmlCloseTag(ofs, depth + command_depth,
2894 style.latexparam());
2896 case LATEX_ITEM_ENVIRONMENT:
2897 if(desc_on == 1) break;
2899 sgmlCloseTag(ofs, depth + 1 + command_depth, end_tag);
2901 case LATEX_PARAGRAPH:
2902 if(style.latexname() != "dummy")
2903 sgmlCloseTag(ofs, depth + command_depth,
2907 sgmlCloseTag(ofs, depth + command_depth,
2914 for(; depth >= 0; --depth) {
2915 if(!environment_stack[depth].empty()) {
2916 if(environment_inner[depth] != "!-- --") {
2917 item_name= "listitem";
2918 sgmlCloseTag(ofs, command_depth + depth,
2920 if( environment_inner[depth] == "varlistentry")
2921 sgmlCloseTag(ofs, depth + command_depth,
2922 environment_inner[depth]);
2925 sgmlCloseTag(ofs, depth + command_depth,
2926 environment_stack[depth]);
2930 for(int j = command_depth; j >= command_base; --j)
2931 if(!command_stack[j].empty())
2932 sgmlCloseTag(ofs, j, command_stack[j]);
2935 sgmlCloseTag(ofs, 0, top_element);
2938 // How to check for successful close
2942 void Buffer::SimpleDocBookOnePar(string & file, string & extra,
2943 LyXParagraph * par, int & desc_on,
2947 par->SimpleDocBookOneTablePar(file, extra, desc_on, depth);
2950 LyXFont font1, font2;
2953 LyXParagraph::size_type main_body;
2955 string emph= "emphasis";
2956 bool emph_flag= false;
2957 int char_line_count= 0;
2959 LyXLayout const & style = textclasslist.Style(params.textclass,
2962 if (style.labeltype != LABEL_MANUAL)
2965 main_body = par->BeginningOfMainBody();
2967 /* gets paragraph main font */
2969 font1 = style.labelfont;
2973 char_line_count = depth;
2974 if(!style.free_spacing)
2975 for (j = 0; j < depth; ++j)
2978 /* parsing main loop */
2979 for (LyXParagraph::size_type i = 0;
2980 i < par->size(); ++i) {
2981 font2 = par->getFont(i);
2983 /* handle <emphasis> tag */
2984 if (font1.emph() != font2.emph() && i) {
2985 if (font2.emph() == LyXFont::ON) {
2986 file += "<emphasis>";
2989 file += "</emphasis>";
2994 c = par->GetChar(i);
2996 if (c == LyXParagraph::META_INSET) {
2997 inset = par->GetInset(i);
2999 inset->DocBook(tmp_out);
3001 // This code needs some explanation:
3002 // Two insets are treated specially
3003 // label if it is the first element in a command paragraph
3005 // graphics inside tables or figure floats can't go on
3006 // title (the equivalente in latex for this case is caption
3007 // and title should come first
3010 if(desc_on!= 3 || i!= 0) {
3011 if(!tmp_out.empty() && tmp_out[0] == '@') {
3013 extra += frontStrip(tmp_out, '@');
3015 file += frontStrip(tmp_out, '@');
3020 } else if (font2.latex() == LyXFont::ON) {
3021 // "TeX"-Mode on ==> SGML-Mode on.
3028 if (par->linuxDocConvertChar(c, sgml_string)
3029 && !style.free_spacing) { // in freespacing
3031 // non-breaking characters
3036 file += "</term><listitem><para>";
3044 file += sgml_string;
3050 /* needed if there is an optional argument but no contents */
3051 if (main_body > 0 && main_body == par->size()) {
3055 file += "</emphasis>";
3058 /* resets description flag correctly */
3061 /* <term> not closed... */
3069 int Buffer::runLaTeX()
3071 if (!users->text) return 0;
3075 // get LaTeX-Filename
3076 string name = getLatexName();
3078 string path = OnlyPath(filename);
3080 string org_path = path;
3081 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3085 Path p(path); // path to LaTeX file
3086 users->owner()->getMiniBuffer()->Set(_("Running LaTeX..."));
3088 // Remove all error insets
3089 bool a = users->removeAutoInsets();
3091 // Always generate the LaTeX file
3092 makeLaTeXFile(name, org_path, false);
3095 // do the LaTex run(s)
3097 string latex_command = lyxrc->pdf_mode ?
3098 lyxrc->pdflatex_command : lyxrc->latex_command;
3099 LaTeX latex(latex_command, name, filepath);
3100 int res = latex.run(terr,
3101 users->owner()->getMiniBuffer()); // running latex
3103 // check return value from latex.run().
3104 if ((res & LaTeX::NO_LOGFILE)) {
3105 WriteAlert(_("LaTeX did not work!"),
3106 _("Missing log file:"), name);
3107 } else if ((res & LaTeX::ERRORS)) {
3108 users->owner()->getMiniBuffer()->Set(_("Done"));
3109 // Insert all errors as errors boxes
3110 users->insertErrors(terr);
3112 // Dvi should also be kept dirty if the latex run
3113 // ends up with errors. However it should be possible
3114 // to view a dirty dvi too.
3116 //no errors or any other things to think about so:
3117 users->owner()->getMiniBuffer()->Set(_("Done"));
3121 // if we removed error insets before we ran LaTeX or if we inserted
3122 // error insets after we ran LaTeX this must be run:
3123 if (a || (res & LaTeX::ERRORS)){
3126 users->updateScrollbar();
3130 return latex.getNumErrors();
3134 int Buffer::runLiterate()
3136 if (!users->text) return 0;
3140 // get LaTeX-Filename
3141 string name = getLatexName();
3142 // get Literate-Filename
3143 string lit_name = ChangeExtension (getLatexName(),
3144 lyxrc->literate_extension, true);
3146 string path = OnlyPath(filename);
3148 string org_path = path;
3149 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3153 Path p(path); // path to Literate file
3154 users->owner()->getMiniBuffer()->Set(_("Running Literate..."));
3156 // Remove all error insets
3157 bool a = users->removeAutoInsets();
3159 // generate the Literate file if necessary
3160 if (!isDviClean() || a) {
3161 makeLaTeXFile(lit_name, org_path, false);
3165 string latex_command = lyxrc->pdf_mode ?
3166 lyxrc->pdflatex_command : lyxrc->latex_command;
3167 Literate literate(latex_command, name, filepath,
3169 lyxrc->literate_command, lyxrc->literate_error_filter,
3170 lyxrc->build_command, lyxrc->build_error_filter);
3172 int res = literate.weave(terr, users->owner()->getMiniBuffer());
3174 // check return value from literate.weave().
3175 if ((res & Literate::NO_LOGFILE)) {
3176 WriteAlert(_("Literate command did not work!"),
3177 _("Missing log file:"), name);
3178 } else if ((res & Literate::ERRORS)) {
3179 users->owner()->getMiniBuffer()->Set(_("Done"));
3180 // Insert all errors as errors boxes
3181 users->insertErrors(terr);
3183 // Dvi should also be kept dirty if the latex run
3184 // ends up with errors. However it should be possible
3185 // to view a dirty dvi too.
3187 //no errors or any other things to think about so:
3188 users->owner()->getMiniBuffer()->Set(_("Done"));
3192 // if we removed error insets before we ran LaTeX or if we inserted
3193 // error insets after we ran LaTeX this must be run:
3194 if (a || (res & Literate::ERRORS)){
3197 users->updateScrollbar();
3201 return literate.getNumErrors();
3205 int Buffer::buildProgram()
3207 if (!users->text) return 0;
3211 // get LaTeX-Filename
3212 string name = getLatexName();
3213 // get Literate-Filename
3214 string lit_name = ChangeExtension(getLatexName(),
3215 lyxrc->literate_extension, true);
3217 string path = OnlyPath(filename);
3219 string org_path = path;
3220 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3224 Path p(path); // path to Literate file
3225 users->owner()->getMiniBuffer()->Set(_("Building Program..."));
3227 // Remove all error insets
3228 bool a = users->removeAutoInsets();
3230 // generate the LaTeX file if necessary
3231 if (!isNwClean() || a) {
3232 makeLaTeXFile(lit_name, org_path, false);
3236 string latex_command = lyxrc->pdf_mode ?
3237 lyxrc->pdflatex_command : lyxrc->latex_command;
3238 Literate literate(latex_command, name, filepath,
3240 lyxrc->literate_command, lyxrc->literate_error_filter,
3241 lyxrc->build_command, lyxrc->build_error_filter);
3243 int res = literate.build(terr, users->owner()->getMiniBuffer());
3245 // check return value from literate.build().
3246 if ((res & Literate::NO_LOGFILE)) {
3247 WriteAlert(_("Build did not work!"),
3248 _("Missing log file:"), name);
3249 } else if ((res & Literate::ERRORS)) {
3250 users->owner()->getMiniBuffer()->Set(_("Done"));
3251 // Insert all errors as errors boxes
3252 users->insertErrors(terr);
3254 // Literate files should also be kept dirty if the literate
3255 // command run ends up with errors.
3257 //no errors or any other things to think about so:
3258 users->owner()->getMiniBuffer()->Set(_("Done"));
3262 // if we removed error insets before we ran Literate/Build or if we inserted
3263 // error insets after we ran Literate/Build this must be run:
3264 if (a || (res & Literate::ERRORS)){
3267 users->updateScrollbar();
3271 return literate.getNumErrors();
3275 // This should be enabled when the Chktex class is implemented. (Asger)
3276 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
3277 // Other flags: -wall -v0 -x
3278 int Buffer::runChktex()
3280 if (!users->text) return 0;
3284 // get LaTeX-Filename
3285 string name = getLatexName();
3286 string path = OnlyPath(filename);
3288 string org_path = path;
3289 if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
3293 Path p(path); // path to LaTeX file
3294 users->owner()->getMiniBuffer()->Set(_("Running chktex..."));
3296 // Remove all error insets
3297 bool a = users->removeAutoInsets();
3299 // Generate the LaTeX file if neccessary
3300 if (!isDviClean() || a) {
3301 makeLaTeXFile(name, org_path, false);
3306 Chktex chktex(lyxrc->chktex_command, name, filepath);
3307 int res = chktex.run(terr); // run chktex
3310 WriteAlert(_("chktex did not work!"),
3311 _("Could not run with file:"), name);
3312 } else if (res > 0) {
3313 // Insert all errors as errors boxes
3314 users->insertErrors(terr);
3317 // if we removed error insets before we ran chktex or if we inserted
3318 // error insets after we ran chktex, this must be run:
3322 users->updateScrollbar();
3331 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
3333 LyXFont font1(LyXFont::ALL_INHERIT);
3336 LyXParagraph::size_type i;
3340 string fname1 = TmpFileName(string(), "RAT1");
3341 string fname2 = TmpFileName(string(), "RAT2");
3343 ofstream ofs(fname1.c_str());
3345 WriteAlert(_("LYX_ERROR:"),
3346 _("Cannot open temporary file:"), fname1);
3349 par->table->RoffEndOfCell(ofs, -1);
3350 for (i = 0; i < par->size(); ++i) {
3351 c = par->GetChar(i);
3352 if (par->table->IsContRow(cell)) {
3353 if (c == LyXParagraph::META_NEWLINE)
3357 font2 = par->GetFontSettings(i);
3358 if (font1.latex() != font2.latex()) {
3359 if (font2.latex() != LyXFont::OFF)
3363 case LyXParagraph::META_INSET:
3364 if ((inset = par->GetInset(i))) {
3367 stringstresm ss(ios::in | ios::out);
3368 inset->Latex(ss, -1);
3380 inset->Latex(ss, -1);
3393 fstream fs(fname2.c_str(), ios::in|ios::out);
3395 WriteAlert(_("LYX_ERROR:"),
3396 _("Cannot open temporary file:"), fname2);
3398 remove(fname1.c_str());
3401 inset->Latex(fs, -1);
3415 case LyXParagraph::META_NEWLINE:
3416 if (par->table->CellHasContRow(cell)>= 0)
3417 par->RoffContTableRows(ofs, i+1, cell);
3418 par->table->RoffEndOfCell(ofs, cell);
3421 case LyXParagraph::META_HFILL:
3424 case LyXParagraph::META_PROTECTED_SEPARATOR:
3435 << "RoffAsciiTable:"
3436 " NULL char in structure." << endl;
3440 par->table->RoffEndOfCell(ofs, cell);
3442 string cmd = lyxrc->ascii_roff_command + " >" + fname2;
3443 cmd = subst(cmd, "$$FName", fname1);
3444 Systemcalls one(Systemcalls::System, cmd);
3445 if (!(lyxerr.debugging(Debug::ROFF))) {
3446 remove(fname1.c_str());
3448 ifstream ifs(fname2.c_str());
3450 WriteFSAlert(_("Error! Can't open temporary file:"), fname2);
3453 // now output the produced file
3457 WriteAlert(_("Error!"),
3458 _("Error executing *roff command on table"));
3459 // overread leading blank lines
3460 while(!ifs && (c == '\n'))
3463 for(j = 0; j < par->depth; ++j)
3465 while(!ifs && (c != '\n')) {
3470 // overread trailing blank lines
3471 while(!ifs && (c == '\n'))
3475 remove(fname2.c_str());
3480 /// changed Heinrich Bauer, 23/03/98
3481 bool Buffer::isDviClean() const
3483 if (lyxrc->use_tempdir)
3484 return dvi_clean_tmpd;
3486 return dvi_clean_orgd;
3490 /// changed Heinrich Bauer, 23/03/98
3491 void Buffer::markDviClean()
3493 if (lyxrc->use_tempdir)
3494 dvi_clean_tmpd = true;
3496 dvi_clean_orgd = true;
3500 /// changed Heinrich Bauer, 23/03/98
3501 void Buffer::markDviDirty()
3503 if (lyxrc->use_tempdir)
3504 dvi_clean_tmpd = false;
3506 dvi_clean_orgd = false;
3510 void Buffer::validate(LaTeXFeatures & features) const
3512 LyXParagraph * par = paragraph;
3513 LyXTextClass const & tclass =
3514 textclasslist.TextClass(params.textclass);
3516 // AMS Style is at document level
3518 features.amsstyle = (params.use_amsmath ||
3519 tclass.provides(LyXTextClass::amsmath));
3522 // We don't use "lyxerr.debug" because of speed. (Asger)
3523 if (lyxerr.debugging(Debug::LATEX))
3524 lyxerr << "Paragraph: " << par << endl;
3526 // Now just follow the list of paragraphs and run
3527 // validate on each of them.
3528 par->validate(features);
3530 // and then the next paragraph
3534 // the bullet shapes are buffer level not paragraph level
3535 // so they are tested here
3536 for (int i = 0; i < 4; ++i) {
3537 if (params.user_defined_bullets[i] != ITEMIZE_DEFAULTS[i]) {
3538 int font = params.user_defined_bullets[i].getFont();
3541 .user_defined_bullets[i]
3548 features.latexsym = true;
3552 features.amssymb = true;
3554 else if ((font >= 2 && font <= 5)) {
3555 features.pifont = true;
3560 if (lyxerr.debugging(Debug::LATEX)) {
3561 features.showStruct(params);
3566 void Buffer::setPaperStuff()
3568 params.papersize = BufferParams::PAPER_DEFAULT;
3569 char c1 = params.paperpackage;
3570 if (c1 == BufferParams::PACKAGE_NONE) {
3571 char c2 = params.papersize2;
3572 if (c2 == BufferParams::VM_PAPER_USLETTER)
3573 params.papersize = BufferParams::PAPER_USLETTER;
3574 else if (c2 == BufferParams::VM_PAPER_USLEGAL)
3575 params.papersize = BufferParams::PAPER_LEGALPAPER;
3576 else if (c2 == BufferParams::VM_PAPER_USEXECUTIVE)
3577 params.papersize = BufferParams::PAPER_EXECUTIVEPAPER;
3578 else if (c2 == BufferParams::VM_PAPER_A3)
3579 params.papersize = BufferParams::PAPER_A3PAPER;
3580 else if (c2 == BufferParams::VM_PAPER_A4)
3581 params.papersize = BufferParams::PAPER_A4PAPER;
3582 else if (c2 == BufferParams::VM_PAPER_A5)
3583 params.papersize = BufferParams::PAPER_A5PAPER;
3584 else if ((c2 == BufferParams::VM_PAPER_B3) || (c2 == BufferParams::VM_PAPER_B4) ||
3585 (c2 == BufferParams::VM_PAPER_B5))
3586 params.papersize = BufferParams::PAPER_B5PAPER;
3587 } else if ((c1 == BufferParams::PACKAGE_A4) || (c1 == BufferParams::PACKAGE_A4WIDE) ||
3588 (c1 == BufferParams::PACKAGE_WIDEMARGINSA4))
3589 params.papersize = BufferParams::PAPER_A4PAPER;
3593 // This function should be in Buffer because it's a buffer's property (ale)
3594 string Buffer::getIncludeonlyList(char delim)
3597 LyXParagraph * par = paragraph;
3598 LyXParagraph::size_type pos;
3602 while ((inset = par->ReturnNextInsetPointer(pos))){
3603 if (inset->LyxCode() == Inset::INCLUDE_CODE) {
3604 InsetInclude * insetinc =
3605 static_cast<InsetInclude*>(inset);
3606 if (insetinc->isInclude()
3607 && insetinc->isNoLoad()) {
3610 lst += ChangeExtension(insetinc->getContents(), string(), true);
3617 lyxerr.debug() << "Includeonly(" << lst << ')' << endl;
3622 // This is also a buffer property (ale)
3623 string Buffer::getReferenceList(char delim)
3625 /// if this is a child document and the parent is already loaded
3626 /// Use the parent's list instead [ale990407]
3627 if (!params.parentname.empty()
3628 && bufferlist.exists(params.parentname)) {
3629 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3631 return tmp->getReferenceList(delim);
3634 LyXParagraph * par = paragraph;
3635 LyXParagraph::size_type pos;
3640 while ((inset = par->ReturnNextInsetPointer(pos))){
3641 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
3644 lst += inset->getLabel(i);
3650 lyxerr.debug() << "References(" << lst << ")" << endl;
3655 // This is also a buffer property (ale)
3656 string Buffer::getBibkeyList(char delim)
3658 /// if this is a child document and the parent is already loaded
3659 /// Use the parent's list instead [ale990412]
3660 if (!params.parentname.empty() && bufferlist.exists(params.parentname)) {
3661 Buffer * tmp = bufferlist.getBuffer(params.parentname);
3663 return tmp->getBibkeyList(delim);
3667 LyXParagraph * par = paragraph;
3670 if (!bibkeys.empty())
3672 bibkeys += par->bibkey->getContents();
3677 // Might be either using bibtex or a child has bibliography
3678 if (bibkeys.empty()) {
3682 LyXParagraph::size_type pos = -1;
3684 // Search for Bibtex or Include inset
3685 while ((inset = par->ReturnNextInsetPointer(pos))) {
3686 if (inset-> LyxCode() == Inset::BIBTEX_CODE) {
3687 if (!bibkeys.empty())
3689 bibkeys += static_cast<InsetBibtex*>(inset)->getKeys(delim);
3690 } else if (inset-> LyxCode() == Inset::INCLUDE_CODE) {
3691 string bk = static_cast<InsetInclude*>(inset)->getKeys(delim);
3693 if (!bibkeys.empty())
3704 lyxerr.debug() << "Bibkeys(" << bibkeys << ")" << endl;
3709 bool Buffer::isDepClean(string const & name) const
3711 DEPCLEAN * item = dep_clean;
3712 while (item && item->master != name)
3714 if (!item) return true;
3719 void Buffer::markDepClean(string const & name)
3722 dep_clean = new DEPCLEAN;
3723 dep_clean->clean = true;
3724 dep_clean->master = name;
3725 dep_clean->next = 0;
3727 DEPCLEAN* item = dep_clean;
3728 while (item && item->master != name)
3733 item = new DEPCLEAN;
3735 item->master = name;
3742 void Buffer::Dispatch(string const & command)
3744 // Split command string into command and argument
3745 string cmd, line = frontStrip(command);
3746 string arg = strip(frontStrip(split(line, cmd, ' ')));
3748 Dispatch(lyxaction.LookupFunc(cmd.c_str()), arg.c_str());
3752 void Buffer::Dispatch(int action, string const & argument)
3756 MenuExport(this, argument);
3760 lyxerr << "A truly unknown func!" << endl;