1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "lyxparagraph.h"
21 #include "lyxparagraph.h"
22 #include "support/textutils.h"
25 #include "tex-strings.h"
27 #include "bufferparams.h"
28 #include "support/FileInfo.h"
29 #include "support/LAssert.h"
31 #include "LaTeXFeatures.h"
32 #include "insets/insetinclude.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
35 #include "support/filetools.h"
36 #include "lyx_gui_misc.h"
38 #include "support/lyxmanip.h"
39 #include "BufferView.h"
46 using std::lower_bound;
47 using std::upper_bound;
50 int tex_code_break_column = 72; // needs non-zero initialization. set later.
51 // this is a bad idea, but how can LyXParagraph find its buffer to get
52 // parameters? (JMarc)
54 extern string bibitemWidest(Buffer const *);
56 // this is a minibuffer
57 static char minibuffer_char;
58 static LyXFont minibuffer_font;
59 static Inset * minibuffer_inset;
62 extern BufferView * current_view;
64 // Initialization of the counter for the paragraph id's,
65 // declared in lyxparagraph.h
66 unsigned int LyXParagraph::paragraph_id = 0;
69 LyXParagraph::LyXParagraph()
72 text.reserve(500); // is this number too big?
74 for (int i = 0; i < 10; ++i) setCounter(i , 0);
81 footnoteflag = LyXParagraph::NO_FOOTNOTE;
82 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
84 align = LYX_ALIGN_BLOCK;
88 bibkey = 0; // ale970302
93 // This konstruktor inserts the new paragraph in a list.
94 LyXParagraph::LyXParagraph(LyXParagraph * par)
101 for (int i = 0; i < 10; ++i) setCounter(i, 0);
105 // double linked list begin
108 next->previous = this;
110 previous->next = this;
113 footnoteflag = LyXParagraph::NO_FOOTNOTE;
114 footnotekind = LyXParagraph::FOOTNOTE;
117 id_ = paragraph_id++;
119 bibkey = 0; // ale970302
125 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
126 BufferParams const & params,
127 char footflag, char dth) const
131 footnoteflag != LyXParagraph::NO_FOOTNOTE ||
133 || previous->footnoteflag == LyXParagraph::NO_FOOTNOTE
138 // The beginning or the end of a footnote environment?
139 if (footflag != footnoteflag) {
140 footflag = footnoteflag;
142 os << "\n\\begin_float "
143 << string_footnotekinds[footnotekind]
146 os << "\n\\end_float ";
150 // The beginning or end of a deeper (i.e. nested) area?
153 while (depth > dth) {
154 os << "\n\\begin_deeper ";
158 while (depth < dth) {
159 os << "\n\\end_deeper ";
165 // First write the layout
167 << textclasslist.NameOfLayout(params.textclass, layout)
170 // Maybe some vertical spaces.
171 if (added_space_top.kind() != VSpace::NONE)
172 os << "\\added_space_top "
173 << added_space_top.asLyXCommand() << " ";
174 if (added_space_bottom.kind() != VSpace::NONE)
175 os << "\\added_space_bottom "
176 << added_space_bottom.asLyXCommand() << " ";
178 // Maybe the paragraph has special spacing
179 spacing.writeFile(os, true);
181 // The labelwidth string used in lists.
182 if (!labelwidthstring.empty())
183 os << "\\labelwidthstring "
184 << labelwidthstring << '\n';
186 // Lines above or below?
190 os << "\\line_bottom ";
192 // Pagebreaks above or below?
194 os << "\\pagebreak_top ";
195 if (pagebreak_bottom)
196 os << "\\pagebreak_bottom ";
198 // Start of appendix?
199 if (start_of_appendix)
200 os << "\\start_of_appendix ";
207 if (align != LYX_ALIGN_LAYOUT) {
210 case LYX_ALIGN_LEFT: h = 1; break;
211 case LYX_ALIGN_RIGHT: h = 2; break;
212 case LYX_ALIGN_CENTER: h = 3; break;
213 default: h = 0; break;
215 os << "\\align " << string_align[h] << " ";
217 if (pextra_type != PEXTRA_NONE) {
218 os << "\\pextra_type " << pextra_type;
219 if (pextra_type == PEXTRA_MINIPAGE) {
220 os << " \\pextra_alignment "
223 os << " \\pextra_hfill "
225 if (pextra_start_minipage)
226 os << " \\pextra_start_minipage "
227 << pextra_start_minipage;
229 if (!pextra_width.empty()) {
230 os << " \\pextra_width "
231 << VSpace(pextra_width).asLyXCommand();
232 } else if (!pextra_widthp.empty()) {
233 os << " \\pextra_widthp "
240 // Dummy layout. This means that a footnote ended.
241 os << "\n\\end_float ";
242 footflag = LyXParagraph::NO_FOOTNOTE;
248 bibkey->Write(buf, os);
250 LyXFont font1(LyXFont::ALL_INHERIT, params.language);
253 for (size_type i = 0; i < size(); ++i) {
259 // Write font changes
260 LyXFont font2 = GetFontSettings(params, i);
261 if (font2 != font1) {
262 font2.lyxWriteChanges(font1, os);
267 value_type const c = GetChar(i);
271 Inset const * inset = GetInset(i);
273 if (inset->DirectWrite()) {
274 // international char, let it write
275 // code directly so it's shorter in
277 inset->Write(buf, os);
279 os << "\n\\begin_inset ";
280 inset->Write(buf, os);
281 os << "\n\\end_inset \n\n";
287 os << "\n\\newline \n";
291 os << "\n\\hfill \n";
295 os << "\n\\backslash \n";
299 if (i + 1 < size() && GetChar(i + 1) == ' ') {
306 if ((column > 70 && c == ' ')
311 // this check is to amend a bug. LyX sometimes
312 // inserts '\0' this could cause problems.
316 lyxerr << "ERROR (LyXParagraph::writeFile):"
317 " NULL char in structure." << endl;
323 // now write the next paragraph
325 next->writeFile(buf, os, params, footflag, dth);
329 void LyXParagraph::validate(LaTeXFeatures & features) const
331 BufferParams const & params = features.bufferParams();
334 // this will be useful later
335 LyXLayout const & layout =
336 textclasslist.Style(params.textclass,
341 if (line_top || line_bottom)
342 features.lyxline = true;
343 if (!spacing.isDefault())
344 features.setspace = true;
347 features.layout[GetLayout()] = true;
350 Language const * doc_language = params.language;
352 for (FontList::const_iterator cit = fontlist.begin();
353 cit != fontlist.end(); ++cit) {
354 if ((*cit).font.noun() == LyXFont::ON) {
355 lyxerr[Debug::LATEX] << "font.noun: "
356 << (*cit).font.noun()
358 features.noun = true;
359 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
360 << (*cit).font.stateText(0)
363 switch ((*cit).font.color()) {
365 case LColor::inherit:
369 features.color = true;
370 lyxerr[Debug::LATEX] << "Color enabled. Font: "
371 << (*cit).font.stateText(0)
375 Language const * language = (*cit).font.language();
376 if (language->babel() != doc_language->babel()) {
377 features.UsedLanguages.insert(language);
378 lyxerr[Debug::LATEX] << "Found language "
379 << language->babel() << endl;
384 for (InsetList::const_iterator cit = insetlist.begin();
385 cit != insetlist.end(); ++cit) {
387 (*cit).inset->Validate(features);
390 if (pextra_type == PEXTRA_INDENT)
391 features.LyXParagraphIndent = true;
392 if (pextra_type == PEXTRA_FLOATFLT)
393 features.floatflt = true;
395 if (layout.needprotect
396 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
397 features.NeedLyXFootnoteCode = true;
399 if (params.paragraph_separation == BufferParams::PARSEP_INDENT
400 && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
401 features.NeedLyXMinipageIndent = true;
403 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
404 features.algorithm = true;
409 // First few functions needed for cut and paste and paragraph breaking.
410 void LyXParagraph::CopyIntoMinibuffer(Buffer const & buffer,
411 LyXParagraph::size_type pos) const
413 BufferParams bparams = buffer.params;
415 minibuffer_char = GetChar(pos);
416 minibuffer_font = GetFontSettings(bparams, pos);
417 minibuffer_inset = 0;
418 if (minibuffer_char == LyXParagraph::META_INSET) {
420 minibuffer_inset = GetInset(pos)->Clone(buffer);
422 minibuffer_inset = 0;
423 minibuffer_char = ' ';
424 // This reflects what GetInset() does (ARRae)
430 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
431 LyXParagraph::size_type pos)
433 minibuffer_char = GetChar(pos);
434 minibuffer_font = GetFontSettings(bparams, pos);
435 minibuffer_inset = 0;
436 if (minibuffer_char == LyXParagraph::META_INSET) {
438 minibuffer_inset = GetInset(pos);
439 // This is a little hack since I want exactly
440 // the inset, not just a clone. Otherwise
441 // the inset would be deleted when calling Erase(pos)
443 InsetTable search_elem(pos, 0);
444 InsetList::iterator it =
445 lower_bound(insetlist.begin(),
447 search_elem, matchIT());
448 if (it != insetlist.end() && (*it).pos == pos)
451 minibuffer_inset = 0;
452 minibuffer_char = ' ';
453 // This reflects what GetInset() does (ARRae)
458 // Erase(pos); now the caller is responsible for that.
462 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
464 if ((minibuffer_char == LyXParagraph::META_INSET) &&
465 !InsertInsetAllowed(minibuffer_inset))
467 if (minibuffer_char == LyXParagraph::META_INSET)
468 InsertInset(pos, minibuffer_inset, minibuffer_font);
470 InsertChar(pos, minibuffer_char, minibuffer_font);
478 void LyXParagraph::Clear()
483 pagebreak_top = false;
484 pagebreak_bottom = false;
486 added_space_top = VSpace(VSpace::NONE);
487 added_space_bottom = VSpace(VSpace::NONE);
488 spacing.set(Spacing::Default);
490 align = LYX_ALIGN_LAYOUT;
494 pextra_type = PEXTRA_NONE;
495 pextra_width.erase();
496 pextra_widthp.erase();
497 pextra_alignment = MINIPAGE_ALIGN_TOP;
498 pextra_hfill = false;
499 pextra_start_minipage = false;
502 labelwidthstring.erase();
506 start_of_appendix = false;
510 // the destructor removes the new paragraph from the list
511 LyXParagraph::~LyXParagraph()
514 previous->next = next;
516 next->previous = previous;
518 for (InsetList::iterator it = insetlist.begin();
519 it != insetlist.end(); ++it) {
526 //lyxerr << "LyXParagraph::paragraph_id = "
527 // << LyXParagraph::paragraph_id << endl;
531 void LyXParagraph::Erase(LyXParagraph::size_type pos)
534 // > because last is the next unused position, and you can
535 // use it if you want
537 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
538 NextAfterFootnote()->Erase(pos - text.size() - 1);
540 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
541 "position does not exist." << endl;
545 Assert(pos < size());
548 if (pos < size()) { // last is free for insertation, but should be empty
550 // if it is an inset, delete the inset entry
551 if (text[pos] == LyXParagraph::META_INSET) {
553 InsetTable search_inset(pos, 0);
554 InsetList::iterator it =
555 lower_bound(insetlist.begin(),
557 search_inset, matchIT());
558 if (it != insetlist.end() && (*it).pos == pos) {
564 text.erase(text.begin() + pos);
566 text.erase(text.mutable_begin() + pos);
568 // Erase entries in the tables.
569 FontTable search_font(pos, LyXFont());
571 FontList::iterator it =
572 lower_bound(fontlist.begin(),
574 search_font, matchFT());
575 if (it != fontlist.end() && (*it).pos == pos &&
577 (it != fontlist.begin() && (*(it - 1)).pos == pos - 1))) {
578 // If it is a multi-character font
579 // entry, we just make it smaller
580 // (see update below), otherwise we
582 unsigned int const i = it - fontlist.begin();
583 fontlist.erase(fontlist.begin() + i);
584 it = fontlist.begin() + i;
585 if (i > 0 && i < fontlist.size() &&
586 fontlist[i - 1].font == fontlist[i].font) {
587 fontlist.erase(fontlist.begin() + i - 1);
588 it = fontlist.begin() + i - 1;
592 // Update all other entries.
593 FontList::iterator fend = fontlist.end();
594 for (; it != fend; ++it)
597 // Update the inset table.
598 InsetTable search_inset(pos, 0);
599 InsetList::iterator lend = insetlist.end();
600 for (InsetList::iterator it =
601 upper_bound(insetlist.begin(),
603 search_inset, matchIT());
608 lyxerr << "ERROR (LyXParagraph::Erase): "
609 "can't erase non-existant char." << endl;
615 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
616 LyXParagraph::value_type c)
618 LyXFont const f(LyXFont::ALL_INHERIT);
619 InsertChar(pos, c, f);
623 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
624 LyXParagraph::value_type c,
625 LyXFont const & font)
628 // > because last is the next unused position, and you can
629 // use it if you want
632 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
633 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
636 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
637 "position does not exist." << endl;
641 Assert(pos <= size());
644 text.insert(text.begin() + pos, c);
648 // Update the font table.
649 FontTable search_font(pos, LyXFont());
650 for (FontList::iterator it = lower_bound(fontlist.begin(),
652 search_font, matchFT());
653 it != fontlist.end(); ++it)
656 // Update the inset table.
657 InsetTable search_inset(pos, 0);
658 for (InsetList::iterator it = lower_bound(insetlist.begin(),
660 search_inset, matchIT());
661 it != insetlist.end(); ++it)
668 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
671 LyXFont const f(LyXFont::ALL_INHERIT);
672 InsertInset(pos, inset, f);
676 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
677 Inset * inset, LyXFont const & font)
682 // > because last is the next unused position, and you can
683 // use it if you want
686 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
688 ->InsertInset(pos - text.size() - 1,
691 lyxerr << "ERROR (LyXParagraph::InsertInset): "
692 "position does not exist: " << pos << endl;
696 Assert(pos <= size());
699 InsertChar(pos, META_INSET, font);
700 Assert(text[pos] == META_INSET);
702 // Add a new entry in the inset table.
703 InsetTable search_inset(pos, 0);
704 InsetList::iterator it = lower_bound(insetlist.begin(),
706 search_inset, matchIT());
707 if (it != insetlist.end() && (*it).pos == pos)
708 lyxerr << "ERROR (LyXParagraph::InsertInset): "
709 "there is an inset in position: " << pos << endl;
711 insetlist.insert(it, InsetTable(pos, inset));
713 inset->setOwner(inset_owner);
717 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
719 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
722 return inset_owner->InsertInsetAllowed(inset);
727 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
732 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
733 return NextAfterFootnote()
734 ->GetInset(pos - text.size() - 1);
736 lyxerr << "ERROR (LyXParagraph::GetInset): "
737 "position does not exist: "
743 Assert(pos < size());
746 InsetTable search_inset(pos, 0);
747 InsetList::iterator it = lower_bound(insetlist.begin(),
749 search_inset, matchIT());
750 if (it != insetlist.end() && (*it).pos == pos)
753 lyxerr << "ERROR (LyXParagraph::GetInset): "
754 "Inset does not exist: " << pos << endl;
757 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
758 // Did this commenting out introduce a bug? So far I have not
759 // see any, please enlighten me. (Lgb)
760 // My guess is that since the inset does not exist, we might
761 // as well replace it with a space to prevent craches. (Asger)
766 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
771 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
772 return NextAfterFootnote()
773 ->GetInset(pos - text.size() - 1);
775 lyxerr << "ERROR (LyXParagraph::GetInset): "
776 "position does not exist: "
782 Assert(pos < size());
785 InsetTable search_inset(pos, 0);
786 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
788 search_inset, matchIT());
789 if (cit != insetlist.end() && (*cit).pos == pos)
792 lyxerr << "ERROR (LyXParagraph::GetInset): "
793 "Inset does not exist: " << pos << endl;
795 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
796 // Did this commenting out introduce a bug? So far I have not
797 // see any, please enlighten me. (Lgb)
798 // My guess is that since the inset does not exist, we might
799 // as well replace it with a space to prevent craches. (Asger)
804 // Gets uninstantiated font setting at position.
805 // Optimized after profiling. (Asger)
806 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
807 LyXParagraph::size_type pos) const
810 Assert(pos <= size());
815 FontTable search_font(pos, LyXFont());
816 FontList::const_iterator cit = lower_bound(fontlist.begin(),
818 search_font, matchFT());
819 if (cit != fontlist.end())
825 // > because last is the next unused position, and you can
826 // use it if you want
827 else if (pos > size()) {
829 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
830 return NextAfterFootnote()
831 ->GetFontSettings(bparams,
832 pos - text.size() - 1);
834 // Why is it an error to ask for the font of a
835 // position that does not exist? Would it be
836 // enough for this to be enabled on debug?
837 // We want strict error checking, but it's ok to only
838 // have it when debugging. (Asger)
839 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
840 "position does not exist. "
841 << pos << " (" << static_cast<int>(pos)
843 } else if (pos > 0) {
844 return GetFontSettings(bparams, pos - 1);
847 if (pos == size() && size())
848 return GetFontSettings(bparams, pos - 1);
852 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
854 //return LyXFont(LyXFont::ALL_INHERIT);
858 // Gets uninstantiated font setting at position 0
859 LyXFont const LyXParagraph::GetFirstFontSettings() const
862 if (!fontlist.empty())
863 return fontlist[0].font;
867 else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
868 return NextAfterFootnote()->GetFirstFontSettings();
870 return LyXFont(LyXFont::ALL_INHERIT);
874 // Gets the fully instantiated font at a given position in a paragraph
875 // This is basically the same function as LyXText::GetFont() in text2.C.
876 // The difference is that this one is used for generating the LaTeX file,
877 // and thus cosmetic "improvements" are disallowed: This has to deliver
878 // the true picture of the buffer. (Asger)
879 // If position is -1, we get the layout font of the paragraph.
880 // If position is -2, we get the font of the manual label of the paragraph.
881 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
882 LyXParagraph::size_type pos) const
885 LyXLayout const & layout =
886 textclasslist.Style(bparams.textclass,
888 LyXParagraph::size_type main_body = 0;
889 if (layout.labeltype == LABEL_MANUAL)
890 main_body = BeginningOfMainBody();
895 layoutfont = layout.labelfont;
897 layoutfont = layout.font;
898 tmpfont = GetFontSettings(bparams, pos);
899 tmpfont.realize(layoutfont);
901 // process layoutfont for pos == -1 and labelfont for pos < -1
903 tmpfont = layout.font;
905 tmpfont = layout.labelfont;
906 tmpfont.setLanguage(getParLanguage(bparams));
909 // check for environment font information
910 char par_depth = GetDepth();
911 LyXParagraph const * par = this;
912 while (par && par_depth && !tmpfont.resolved()) {
913 par = par->DepthHook(par_depth - 1);
915 tmpfont.realize(textclasslist.
916 Style(bparams.textclass,
917 par->GetLayout()).font);
918 par_depth = par->GetDepth();
922 tmpfont.realize(textclasslist
923 .TextClass(bparams.textclass)
929 /// Returns the height of the highest font in range
931 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
932 LyXParagraph::size_type endpos) const
934 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
935 if (fontlist.empty())
938 FontTable end_search(endpos, LyXFont());
939 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
941 end_search, matchFT());
942 if (end_it != fontlist.end())
945 FontTable start_search(startpos, LyXFont());
946 for (FontList::const_iterator cit =
947 lower_bound(fontlist.begin(),
949 start_search, matchFT());
950 cit != end_it; ++cit) {
951 LyXFont::FONT_SIZE size = (*cit).font.size();
952 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
959 LyXParagraph::value_type
960 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
965 Assert(pos <= size());
966 if (!size() || pos == size()) return '\0';
977 // > because last is the next unused position, and you can
978 // use it if you want
979 else if (pos > size()) {
980 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
981 return NextAfterFootnote()
982 ->GetChar(pos - text.size() - 1);
985 lyxerr << "ERROR (LyXParagraph::GetChar const): "
986 "position does not exist."
987 << pos << " (" << static_cast<int>(pos)
993 // We should have a footnote environment.
994 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
995 // Notice that LyX does request the
996 // last char from time to time. (Asger)
997 //lyxerr << "ERROR (LyXParagraph::GetChar): "
998 // "expected footnote." << endl;
1001 switch (next->footnotekind) {
1002 case LyXParagraph::FOOTNOTE:
1003 return LyXParagraph::META_FOOTNOTE;
1004 case LyXParagraph::MARGIN:
1005 return LyXParagraph::META_MARGIN;
1006 case LyXParagraph::FIG:
1007 case LyXParagraph::WIDE_FIG:
1008 return LyXParagraph::META_FIG;
1009 case LyXParagraph::TAB:
1010 case LyXParagraph::WIDE_TAB:
1011 return LyXParagraph::META_TAB;
1012 case LyXParagraph::ALGORITHM:
1013 return LyXParagraph::META_ALGORITHM;
1015 return '\0'; // to shut up gcc
1021 LyXParagraph::value_type
1022 LyXParagraph::GetUChar(BufferParams const & bparams,
1023 LyXParagraph::size_type pos) const
1025 value_type c = GetChar(pos);
1026 if (!lyxrc.rtl_support)
1056 if (uc != c && GetFontSettings(bparams, pos).isRightToLeft())
1062 // return an string of the current word, and the end of the word in lastpos.
1063 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1065 Assert(lastpos >= 0);
1067 // the current word is defined as starting at the first character
1068 // from the immediate left of lastpospos which meets the definition
1069 // of IsLetter(), continuing to the last character to the right
1070 // of this meeting IsLetter.
1076 // move back until we have a letter
1078 //there's no real reason to have firstpos & lastpos as
1079 //separate variables as this is written, but maybe someon
1080 // will want to return firstpos in the future.
1082 //since someone might have typed a punctuation first
1083 int firstpos = lastpos;
1085 while ((firstpos >= 0) && !IsLetter(firstpos))
1088 // now find the beginning by looking for a nonletter
1090 while ((firstpos>= 0) && IsLetter(firstpos))
1093 // the above is now pointing to the preceeding non-letter
1097 // so copy characters into theword until we get a nonletter
1098 // note that this can easily exceed lastpos, wich means
1099 // that if used in the middle of a word, the whole word
1102 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1110 #warning Remember to get rid of this one. (Lgb)
1112 LyXParagraph::size_type LyXParagraph::Last() const
1115 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1116 return text.size() + NextAfterFootnote()->Last() + 1;
1117 // the 1 is the symbol
1126 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1128 // > because last is the next unused position, and you can
1129 // use it if you want
1132 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1133 return NextAfterFootnote()
1134 ->ParFromPos(pos - text.size() - 1);
1136 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1137 "position does not exist." << endl;
1146 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1148 // > because last is the next unused position, and you can
1149 // use it if you want
1152 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1153 return NextAfterFootnote()
1154 ->PositionInParFromPos(pos - text.size() - 1);
1157 "ERROR (LyXParagraph::PositionInParFromPos): "
1158 "position does not exist." << endl;
1167 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1168 LyXFont const & font)
1171 // > because last is the next unused position, and you can
1172 // use it if you want
1175 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1176 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1179 lyxerr << "ERROR (LyXParagraph::SetFont): "
1180 "position does not exist." << endl;
1185 Assert(pos <= size());
1188 // First, reduce font against layout/label font
1189 // Update: The SetCharFont() routine in text2.C already
1190 // reduces font, so we don't need to do that here. (Asger)
1191 // No need to simplify this because it will disappear
1192 // in a new kernel. (Asger)
1193 // Next search font table
1195 FontTable search_font(pos, LyXFont());
1196 FontList::iterator it = lower_bound(fontlist.begin(),
1198 search_font, matchFT());
1199 unsigned int i = it - fontlist.begin();
1200 bool notfound = it == fontlist.end();
1202 if (!notfound && fontlist[i].font == font)
1205 bool begin = pos == 0 || notfound ||
1206 (i > 0 && fontlist[i-1].pos == pos - 1);
1207 // Is position pos is a beginning of a font block?
1208 bool end = !notfound && fontlist[i].pos == pos;
1209 // Is position pos is the end of a font block?
1210 if (begin && end) { // A single char block
1211 if (i + 1 < fontlist.size() &&
1212 fontlist[i + 1].font == font) {
1213 // Merge the singleton block with the next block
1214 fontlist.erase(fontlist.begin() + i);
1215 if (i > 0 && fontlist[i - 1].font == font)
1216 fontlist.erase(fontlist.begin() + i-1);
1217 } else if (i > 0 && fontlist[i - 1].font == font) {
1218 // Merge the singleton block with the previous block
1219 fontlist[i - 1].pos = pos;
1220 fontlist.erase(fontlist.begin() + i);
1222 fontlist[i].font = font;
1224 if (i > 0 && fontlist[i - 1].font == font)
1225 fontlist[i - 1].pos = pos;
1227 fontlist.insert(fontlist.begin() + i,
1228 FontTable(pos, font));
1230 fontlist[i].pos = pos - 1;
1231 if (!(i + 1 < fontlist.size() &&
1232 fontlist[i + 1].font == font))
1233 fontlist.insert(fontlist.begin() + i + 1,
1234 FontTable(pos, font));
1235 } else { // The general case. The block is splitted into 3 blocks
1236 fontlist.insert(fontlist.begin() + i,
1237 FontTable(pos - 1, fontlist[i].font));
1238 fontlist.insert(fontlist.begin() + i + 1,
1239 FontTable(pos, font));
1244 // This function is able to hide closed footnotes.
1245 LyXParagraph * LyXParagraph::Next()
1248 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1249 LyXParagraph * tmp = next;
1251 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1253 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1254 return tmp->Next(); /* there can be more than one
1255 footnote in a logical
1258 return next; // This should never happen!
1266 LyXParagraph * LyXParagraph::NextAfterFootnote()
1268 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1269 LyXParagraph * tmp = next;
1270 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1272 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1273 return tmp; /* there can be more than one footnote
1274 in a logical paragraph */
1276 return next; // This should never happen!
1284 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1286 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1287 LyXParagraph * tmp = next;
1288 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1290 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1291 return tmp; /* there can be more than one footnote
1292 in a logical paragraph */
1294 return next; // This should never happen!
1302 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1305 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1307 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1308 tmp = tmp->previous;
1309 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1310 return tmp; /* there can be more than one footnote
1311 in a logical paragraph */
1313 return previous; // This should never happen!
1321 LyXParagraph * LyXParagraph::LastPhysicalPar()
1323 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1326 LyXParagraph * tmp = this;
1328 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1329 tmp = tmp->NextAfterFootnote();
1337 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1339 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1342 LyXParagraph const * tmp = this;
1344 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1345 tmp = tmp->NextAfterFootnote();
1353 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1357 LyXParagraph * tmppar = this;
1361 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1362 tmppar = tmppar->previous;
1373 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1377 LyXParagraph const * tmppar = this;
1381 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1382 tmppar = tmppar->previous;
1392 // This function is able to hide closed footnotes.
1393 LyXParagraph * LyXParagraph::Previous()
1396 LyXParagraph * tmp = previous;
1401 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1402 tmp = tmp->previous;
1404 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1405 tmp = tmp->previous;
1406 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1407 return tmp->next->Previous();
1417 // This function is able to hide closed footnotes.
1418 LyXParagraph const * LyXParagraph::Previous() const
1421 LyXParagraph * tmp = previous;
1425 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1426 tmp = tmp->previous;
1428 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1429 tmp = tmp->previous;
1430 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1431 return tmp->next->Previous();
1441 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1442 LyXParagraph::size_type pos,
1447 // create a new paragraph
1450 size_type pos_first;
1451 LyXParagraph * par = ParFromPos(pos);
1452 LyXParagraph * firstpar = FirstPhysicalPar();
1454 LyXParagraph * tmp = new LyXParagraph(par);
1456 //LyXParagraph * par = this;
1457 //LyXParagraph * firstpar = this;
1458 LyXParagraph * tmp = new LyXParagraph(this);
1462 tmp->footnoteflag = footnoteflag;
1463 tmp->footnotekind = footnotekind;
1465 // this is an idea for a more userfriendly layout handling, I will
1466 // see what the users say
1469 // layout stays the same with latex-environments
1471 tmp->SetOnlyLayout(bparams, firstpar->layout);
1472 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1475 // layout stays the same with latex-environments
1477 tmp->SetOnlyLayout(bparams, layout);
1478 tmp->SetLabelWidthString(labelwidthstring);
1482 if (Last() > pos || !Last() || flag == 2) {
1483 tmp->SetOnlyLayout(bparams, firstpar->layout);
1484 tmp->align = firstpar->align;
1485 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1487 tmp->line_bottom = firstpar->line_bottom;
1488 firstpar->line_bottom = false;
1489 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1490 firstpar->pagebreak_bottom = false;
1491 tmp->added_space_bottom = firstpar->added_space_bottom;
1492 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1494 tmp->depth = firstpar->depth;
1495 tmp->noindent = firstpar->noindent;
1497 if (Last() > pos || !Last() || flag == 2) {
1498 tmp->SetOnlyLayout(bparams, layout);
1500 tmp->SetLabelWidthString(labelwidthstring);
1502 tmp->line_bottom = line_bottom;
1503 line_bottom = false;
1504 tmp->pagebreak_bottom = pagebreak_bottom;
1505 pagebreak_bottom = false;
1506 tmp->added_space_bottom = added_space_bottom;
1507 added_space_bottom = VSpace(VSpace::NONE);
1510 tmp->noindent = noindent;
1512 // copy everything behind the break-position
1513 // to the new paragraph
1516 while (ParFromPos(pos_first) != par)
1518 pos_end = pos_first + par->text.size() - 1;
1521 for (i = j = pos; i <= pos_end; ++i) {
1522 par->CutIntoMinibuffer(bparams, i - pos_first);
1523 if (tmp->InsertFromMinibuffer(j - pos))
1529 for (i = pos_end; i >= pos; --i)
1530 par->Erase(i - pos_first);
1534 size_type pos_end = text.size() - 1;
1536 for (i = j = pos; i <= pos_end; ++i) {
1537 CutIntoMinibuffer(bparams, i);
1538 if (tmp->InsertFromMinibuffer(j - pos))
1542 for (i = pos_end; i >= pos; --i)
1550 // just an idea of me
1552 tmp->line_top = firstpar->line_top;
1553 tmp->pagebreak_top = firstpar->pagebreak_top;
1554 tmp->added_space_top = firstpar->added_space_top;
1555 tmp->bibkey = firstpar->bibkey;
1557 // layout stays the same with latex-environments
1559 firstpar->SetOnlyLayout(bparams, tmp->layout);
1560 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1561 firstpar->depth = tmp->depth;
1565 // just an idea of me
1567 tmp->line_top = line_top;
1568 tmp->pagebreak_top = pagebreak_top;
1569 tmp->added_space_top = added_space_top;
1570 tmp->bibkey = bibkey;
1572 // layout stays the same with latex-environments
1574 SetOnlyLayout(bparams, tmp->layout);
1575 SetLabelWidthString(tmp->labelwidthstring);
1583 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1586 par = par->FirstPhysicalPar();
1587 footnoteflag = par->footnoteflag;
1588 footnotekind = par->footnotekind;
1590 layout = par->layout;
1591 align = par-> align;
1592 SetLabelWidthString(par->labelwidthstring);
1594 line_bottom = par->line_bottom;
1595 pagebreak_bottom = par->pagebreak_bottom;
1596 added_space_bottom = par->added_space_bottom;
1598 line_top = par->line_top;
1599 pagebreak_top = par->pagebreak_top;
1600 added_space_top = par->added_space_top;
1602 spacing = par->spacing;
1604 pextra_type = par->pextra_type;
1605 pextra_width = par->pextra_width;
1606 pextra_widthp = par->pextra_widthp;
1607 pextra_alignment = par->pextra_alignment;
1608 pextra_hfill = par->pextra_hfill;
1609 pextra_start_minipage = par->pextra_start_minipage;
1611 noindent = par->noindent;
1617 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1619 LyXParagraph * tmppar = this;
1622 && tmppar->previous->footnoteflag ==
1623 LyXParagraph::CLOSED_FOOTNOTE)
1624 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1625 tmppar = tmppar->previous;
1628 return this; // This should never happen!
1635 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1637 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1645 && (IsNewline(0) || IsLineSeparator(0))){
1656 LyXParagraph * LyXParagraph::Clone() const
1658 // create a new paragraph
1659 LyXParagraph * result = new LyXParagraph;
1661 result->MakeSameLayout(this);
1663 // this is because of the dummy layout of the paragraphs that
1665 result->layout = layout;
1667 result->inset_owner = inset_owner;
1671 result->bibkey = static_cast<InsetBibKey *>
1672 (bibkey->Clone(*current_view->buffer()));
1676 // copy everything behind the break-position to the new paragraph
1678 result->text = text;
1679 result->fontlist = fontlist;
1680 result->insetlist = insetlist;
1681 for (InsetList::iterator it = result->insetlist.begin();
1682 it != result->insetlist.end(); ++it)
1683 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1688 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1691 par = par->FirstPhysicalPar();
1696 par->footnoteflag == footnoteflag &&
1697 par->footnotekind == footnotekind &&
1699 par->layout == layout &&
1701 par->align == align &&
1703 par->line_bottom == line_bottom &&
1704 par->pagebreak_bottom == pagebreak_bottom &&
1705 par->added_space_bottom == added_space_bottom &&
1707 par->line_top == line_top &&
1708 par->pagebreak_top == pagebreak_top &&
1709 par->added_space_top == added_space_top &&
1711 par->spacing == spacing &&
1713 par->pextra_type == pextra_type &&
1714 par->pextra_width == pextra_width &&
1715 par->pextra_widthp == pextra_widthp &&
1716 par->pextra_alignment == pextra_alignment &&
1717 par->pextra_hfill == pextra_hfill &&
1718 par->pextra_start_minipage == pextra_start_minipage &&
1719 par->noindent == noindent &&
1720 par->depth == depth);
1724 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1725 LyXParagraph::size_type pos)
1728 // create a new paragraph
1729 LyXParagraph * par = ParFromPos(pos);
1731 LyXParagraph * tmp = new LyXParagraph(par);
1733 tmp->MakeSameLayout(par);
1735 // When can pos < Last()?
1736 // I guess pos == Last() is possible.
1738 // copy everything behind the break-position to the new
1740 size_type pos_first = 0;
1741 while (ParFromPos(pos_first) != par)
1743 size_type pos_end = pos_first + par->text.size() - 1;
1746 for (i = j = pos; i <= pos_end; ++i) {
1747 par->CutIntoMinibuffer(bparams, i - pos_first);
1748 if (tmp->InsertFromMinibuffer(j - pos))
1754 for (size_type i = pos_end; i >= pos; --i)
1755 par->Erase(i - pos_first);
1760 // create a new paragraph
1761 LyXParagraph * tmp = new LyXParagraph(this);
1762 tmp->MakeSameLayout(this);
1764 // When can pos > Last()?
1765 // I guess pos == Last() is possible.
1767 // copy everything behind the break-position to the new
1769 size_type pos_end = text.size() - 1;
1772 for (i = j = pos; i <= pos_end; ++i) {
1773 CutIntoMinibuffer(bparams, i);
1774 if (tmp->InsertFromMinibuffer(j - pos))
1780 for (size_type i = pos_end; i >= pos; --i)
1789 // Be carefull, this does not make any check at all.
1790 // This method has wrong name, it combined this par with the next par.
1791 // In that sense it is the reverse of break paragraph. (Lgb)
1792 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1794 // copy the next paragraph to this one
1795 LyXParagraph * the_next = Next();
1797 LyXParagraph * firstpar = FirstPhysicalPar();
1800 // first the DTP-stuff
1802 firstpar->line_bottom = the_next->line_bottom;
1803 firstpar->added_space_bottom = the_next->added_space_bottom;
1804 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1806 line_bottom = the_next->line_bottom;
1807 added_space_bottom = the_next->added_space_bottom;
1808 pagebreak_bottom = the_next->pagebreak_bottom;
1811 size_type pos_end = the_next->text.size() - 1;
1812 size_type pos_insert = Last();
1814 // ok, now copy the paragraph
1816 for (i = j = 0; i <= pos_end; ++i) {
1817 the_next->CutIntoMinibuffer(bparams, i);
1818 if (InsertFromMinibuffer(pos_insert + j))
1822 // delete the next paragraph
1823 LyXParagraph * ppar = the_next->previous;
1824 LyXParagraph * npar = the_next->next;
1831 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1833 LyXParagraph * par = ParFromPos(pos);
1835 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1836 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1842 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1844 LyXParagraph * par = ParFromPos(pos);
1846 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1847 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1854 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1856 LyXParagraph const * par = this;
1857 int par_depth = GetDepth();
1859 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1860 int const endlabeltype =
1861 textclasslist.Style(bparams.textclass,
1862 layout).endlabeltype;
1863 if (endlabeltype != END_LABEL_NO_LABEL) {
1864 LyXParagraph const * last = this;
1866 if (footnoteflag == NO_FOOTNOTE)
1867 last = LastPhysicalPar();
1868 else if (next->footnoteflag == NO_FOOTNOTE)
1869 return endlabeltype;
1874 if (!last || !last->next)
1875 return endlabeltype;
1877 int next_depth = last->next->GetDepth();
1878 if (par_depth > next_depth ||
1879 (par_depth == next_depth && layout != last->next->GetLayout() ))
1880 return endlabeltype;
1885 par = par->DepthHook(par_depth - 1);
1887 par_depth = par->GetDepth();
1889 return END_LABEL_NO_LABEL;
1893 LyXTextClass::size_type LyXParagraph::GetLayout() const
1896 return FirstPhysicalPar()->layout;
1903 char LyXParagraph::GetDepth() const
1906 return FirstPhysicalPar()->depth;
1913 char LyXParagraph::GetAlign() const
1916 return FirstPhysicalPar()->align;
1923 string const & LyXParagraph::GetLabelstring() const
1926 return FirstPhysicalPar()->labelstring;
1933 int LyXParagraph::GetFirstCounter(int i) const
1936 return FirstPhysicalPar()->counter_[i];
1943 // the next two functions are for the manual labels
1944 string const LyXParagraph::GetLabelWidthString() const
1947 if (!FirstPhysicalPar()->labelwidthstring.empty())
1948 return FirstPhysicalPar()->labelwidthstring;
1950 if (!labelwidthstring.empty())
1951 return labelwidthstring;
1954 return _("Senseless with this layout!");
1958 void LyXParagraph::SetLabelWidthString(string const & s)
1961 LyXParagraph * par = FirstPhysicalPar();
1963 par->labelwidthstring = s;
1965 labelwidthstring = s;
1970 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1971 LyXTextClass::size_type new_layout)
1974 LyXParagraph * par = FirstPhysicalPar();
1976 LyXParagraph * par = this;
1978 LyXParagraph * ppar = 0;
1979 LyXParagraph * npar = 0;
1981 par->layout = new_layout;
1983 if (par->pextra_type == PEXTRA_NONE) {
1984 if (par->Previous()) {
1986 ppar = par->Previous()->FirstPhysicalPar();
1988 ppar = par->Previous();
1992 && (ppar->depth > par->depth))
1994 ppar = ppar->Previous()->FirstPhysicalPar();
1996 ppar = ppar->Previous();
2001 npar = par->Next()->NextAfterFootnote();
2007 && (npar->depth > par->depth))
2009 npar = npar->Next()->NextAfterFootnote();
2011 npar = npar->Next();
2014 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2016 p1 = ppar->pextra_width,
2017 p2 = ppar->pextra_widthp;
2018 ppar->SetPExtraType(bparams, ppar->pextra_type,
2021 if ((par->pextra_type == PEXTRA_NONE) &&
2022 npar && (npar->pextra_type != PEXTRA_NONE)) {
2023 string const p1 = npar->pextra_width;
2024 string const p2 = npar->pextra_widthp;
2025 npar->SetPExtraType(bparams, npar->pextra_type,
2032 void LyXParagraph::SetLayout(BufferParams const & bparams,
2033 LyXTextClass::size_type new_layout)
2037 * par = FirstPhysicalPar(),
2044 par->layout = new_layout;
2045 par->labelwidthstring.erase();
2046 par->align = LYX_ALIGN_LAYOUT;
2047 par->added_space_top = VSpace(VSpace::NONE);
2048 par->added_space_bottom = VSpace(VSpace::NONE);
2049 par->spacing.set(Spacing::Default);
2051 if (par->pextra_type == PEXTRA_NONE) {
2052 if (par->Previous()) {
2054 ppar = par->Previous()->FirstPhysicalPar();
2056 ppar = par->Previous();
2060 && (ppar->depth > par->depth))
2062 ppar = ppar->Previous()->FirstPhysicalPar();
2064 ppar = ppar->Previous();
2069 npar = par->Next()->NextAfterFootnote();
2075 && (npar->depth > par->depth))
2077 npar = npar->Next()->NextAfterFootnote();
2079 npar = npar->Next();
2082 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2083 string const p1 = ppar->pextra_width;
2084 string const p2 = ppar->pextra_widthp;
2085 ppar->SetPExtraType(bparams, ppar->pextra_type,
2088 if ((par->pextra_type == PEXTRA_NONE) &&
2089 npar && (npar->pextra_type != PEXTRA_NONE)) {
2090 string const p1 = npar->pextra_width;
2091 string const p2 = npar->pextra_widthp;
2092 npar->SetPExtraType(bparams, npar->pextra_type,
2099 // if the layout of a paragraph contains a manual label, the beginning of the
2100 // main body is the beginning of the second word. This is what the par-
2101 // function returns. If the layout does not contain a label, the main
2102 // body always starts with position 0. This differentiation is necessary,
2103 // because there cannot be a newline or a blank <= the beginning of the
2104 // main body in TeX.
2106 int LyXParagraph::BeginningOfMainBody() const
2109 if (FirstPhysicalPar() != this)
2112 // Unroll the first two cycles of the loop
2113 // and remember the previous character to
2114 // remove unnecessary GetChar() calls
2117 && GetChar(i) != LyXParagraph::META_NEWLINE
2120 char previous_char = 0;
2123 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2124 // Yes, this ^ is supposed to be "= " not "=="
2127 && previous_char != ' '
2128 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2130 previous_char = temp;
2136 if (i == 0 && i == size() &&
2137 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2138 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2139 ++i; /* the cursor should not jump
2140 * to the main body if there
2147 LyXParagraph * LyXParagraph::DepthHook(int deth)
2149 LyXParagraph * newpar = this;
2155 newpar = newpar->FirstPhysicalPar()->Previous();
2157 newpar = newpar->Previous();
2159 } while (newpar && newpar->GetDepth() > deth
2161 && newpar->footnoteflag == footnoteflag
2166 if (Previous() || GetDepth())
2167 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2172 return newpar->FirstPhysicalPar();
2179 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2181 LyXParagraph const * newpar = this;
2187 newpar = newpar->FirstPhysicalPar()->Previous();
2189 newpar = newpar->Previous();
2191 } while (newpar && newpar->GetDepth() > deth
2193 && newpar->footnoteflag == footnoteflag
2198 if (Previous() || GetDepth())
2199 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2204 return newpar->FirstPhysicalPar();
2211 int LyXParagraph::AutoDeleteInsets()
2214 InsetList::size_type index = 0;
2215 while (index < insetlist.size()) {
2216 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2217 Erase(insetlist[index].pos);
2218 // Erase() calls to insetlist.erase(&insetlist[index])
2219 // so index shouldn't be increased.
2228 LyXParagraph::inset_iterator
2229 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2231 InsetTable search_inset(pos, 0);
2232 InsetList::iterator it = lower_bound(insetlist.begin(),
2234 search_inset, matchIT());
2235 return inset_iterator(it);
2239 // returns -1 if inset not found
2240 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2243 for (InsetList::const_iterator cit = insetlist.begin();
2244 cit != insetlist.end(); ++cit) {
2245 if ((*cit).inset == inset) {
2249 if (inset == bibkey)
2253 // Think about footnotes.
2254 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2255 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2257 NextAfterFootnote()->GetPositionOfInset(inset);
2259 return text.size() + 1 + further;
2266 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2267 BufferParams const & bparams,
2268 ostream & os, TexRow & texrow,
2273 TexRow & foot_texrow,
2278 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2279 LyXLayout const & style =
2280 textclasslist.Style(bparams.textclass,
2283 bool further_blank_line = false;
2286 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2289 if (start_of_appendix) {
2290 os << "\\appendix\n";
2294 if (!spacing.isDefault()
2295 && (!Previous() || !Previous()->HasSameLayout(this))) {
2296 os << spacing.writeEnvirBegin() << "\n";
2300 if (tex_code_break_column && style.isCommand()){
2305 if (pagebreak_top) {
2307 further_blank_line = true;
2309 if (added_space_top.kind() != VSpace::NONE) {
2310 os << added_space_top.asLatexCommand(bparams);
2311 further_blank_line = true;
2315 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2316 << "\\vspace{-1\\parskip}";
2317 further_blank_line = true;
2320 if (further_blank_line){
2325 Language const * language = getParLanguage(bparams);
2326 Language const * doc_language = bparams.language;
2327 Language const * previous_language = previous
2328 ? previous->getParLanguage(bparams) : doc_language;
2329 if (language->babel() != doc_language->babel() &&
2330 language->babel() != previous_language->babel()) {
2331 os << subst(lyxrc.language_command_begin, "$$lang",
2337 if (bparams.inputenc == "auto" &&
2338 language->encoding() != previous_language->encoding()) {
2339 os << "\\inputencoding{"
2340 << language->encoding()->LatexName()
2345 switch (style.latextype) {
2348 << style.latexname()
2349 << style.latexparam();
2351 case LATEX_ITEM_ENVIRONMENT:
2353 bibkey->Latex(buf, os, false, false);
2357 case LATEX_LIST_ENVIRONMENT:
2364 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2366 LyXParagraph * par = next;
2368 // Spit out footnotes
2369 if (lyxrc.rtl_support) {
2370 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2371 && next->footnoteflag != footnoteflag) {
2372 LyXParagraph * p = 0;
2373 bool is_rtl = (size() > 0)
2374 ? GetFontSettings(bparams,
2375 size()-1).isRightToLeft()
2376 : language->RightToLeft();
2377 if ((p = NextAfterFootnote()) != 0 &&
2379 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2380 is_rtl = getParLanguage(bparams)->RightToLeft();
2381 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2382 && par->footnoteflag != footnoteflag) {
2383 par = par->TeXFootnote(buf, bparams,
2385 foot_texrow, foot_count,
2387 par->SimpleTeXOnePar(buf, bparams,
2388 os, texrow, moving_arg);
2389 is_rtl = (par->size() > 0)
2390 ? par->GetFontSettings(bparams,
2391 par->size()-1).isRightToLeft()
2392 : language->RightToLeft();
2394 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2395 (p = par->NextAfterFootnote()) != 0 &&
2397 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2398 is_rtl = language->RightToLeft();
2403 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2404 && par->footnoteflag != footnoteflag) {
2405 par = par->TeXFootnote(buf, bparams,
2407 foot, foot_texrow, foot_count,
2409 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2415 // Make sure that \\par is done with the font of the last
2416 // character if this has another size as the default.
2417 // This is necessary because LaTeX (and LyX on the screen)
2418 // calculates the space between the baselines according
2419 // to this font. (Matthias)
2421 // Is this really needed ? (Dekel)
2422 // We do not need to use to change the font for the last paragraph
2423 // or for a command.
2424 LyXFont font = getFont(bparams, Last() - 1);
2425 bool is_command = textclasslist.Style(bparams.textclass,
2426 GetLayout()).isCommand();
2427 if (style.resfont.size() != font.size() && next && !is_command) {
2430 os << "\\" << font.latexSize() << " \\par}";
2431 } else if (need_par) {
2433 } else if (is_command)
2436 if (language->babel() != doc_language->babel() &&
2439 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2441 || par->getParLanguage(bparams)->babel() != language->babel())) {
2443 << subst(lyxrc.language_command_end, "$$lang",
2444 doc_language->babel());
2447 switch (style.latextype) {
2448 case LATEX_ITEM_ENVIRONMENT:
2449 case LATEX_LIST_ENVIRONMENT:
2450 if (par && (depth < par->depth)) {
2455 case LATEX_ENVIRONMENT:
2456 // if its the last paragraph of the current environment
2457 // skip it otherwise fall through
2459 && (par->layout != layout
2460 || par->depth != depth
2461 || par->pextra_type != pextra_type))
2464 // we don't need it for the last paragraph!!!
2465 // or for tables in floats
2466 // -- effectively creates a \par where there isn't one which
2467 // breaks a \subfigure or \subtable.
2469 // && footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2475 further_blank_line = false;
2477 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2478 further_blank_line = true;
2481 if (added_space_bottom.kind() != VSpace::NONE) {
2482 os << added_space_bottom.asLatexCommand(bparams);
2483 further_blank_line = true;
2486 if (pagebreak_bottom) {
2488 further_blank_line = true;
2491 if (further_blank_line){
2496 if (!spacing.isDefault()
2497 && (!par || !par->HasSameLayout(this))) {
2498 os << spacing.writeEnvirEnd() << "\n";
2502 // we don't need it for the last paragraph!!!
2505 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2506 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2513 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2518 // This one spits out the text of the paragraph
2519 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2520 BufferParams const & bparams,
2521 ostream & os, TexRow & texrow,
2524 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2526 bool return_value = false;
2528 LyXLayout const & style =
2529 textclasslist.Style(bparams.textclass,
2531 LyXFont basefont, last_font;
2533 // Maybe we have to create a optional argument.
2534 size_type main_body;
2535 if (style.labeltype != LABEL_MANUAL)
2538 main_body = BeginningOfMainBody();
2540 if (main_body > 0) {
2542 basefont = getFont(bparams, -2); // Get label font
2544 basefont = getFont(bparams, -1); // Get layout font
2555 if (style.isCommand()) {
2558 } else if (align != LYX_ALIGN_LAYOUT) {
2561 return_value = true;
2565 moving_arg |= style.needprotect;
2567 // Which font is currently active?
2568 LyXFont running_font(basefont);
2569 // Do we have an open font change?
2570 bool open_font = false;
2572 texrow.start(this, 0);
2574 for (size_type i = 0; i < size(); ++i) {
2576 // First char in paragraph or after label?
2582 if (main_body > 0) {
2584 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2587 basefont = getFont(bparams, -1); // Now use the layout font
2588 running_font = basefont;
2592 if (style.isCommand()) {
2595 } else if (align != LYX_ALIGN_LAYOUT && next) {
2596 // We do not need \par here (Dekel)
2600 return_value = true;
2604 os << "\\noindent ";
2608 case LYX_ALIGN_NONE:
2609 case LYX_ALIGN_BLOCK:
2610 case LYX_ALIGN_LAYOUT:
2611 case LYX_ALIGN_SPECIAL:
2613 case LYX_ALIGN_LEFT:
2616 if (getParLanguage(bparams)->babel() != "hebrew") {
2617 os << "\\raggedright ";
2620 os << "\\raggedleft ";
2624 case LYX_ALIGN_RIGHT:
2627 if (getParLanguage(bparams)->babel() != "hebrew") {
2628 os << "\\raggedleft ";
2631 os << "\\raggedright ";
2635 case LYX_ALIGN_CENTER:
2638 os << "\\centering ";
2644 value_type c = GetChar(i);
2646 // Fully instantiated font
2647 LyXFont font = getFont(bparams, i);
2649 LyXParagraph * p = 0;
2652 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2653 (p = PreviousBeforeFootnote()) != 0)
2654 last_font = p->getFont(bparams, p->size() - 1);
2657 last_font = running_font;
2659 // Spaces at end of font change are simulated to be
2660 // outside font change, i.e. we write "\textXX{text} "
2661 // rather than "\textXX{text }". (Asger)
2662 if (open_font && c == ' ' && i <= size() - 2
2663 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2664 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2665 font = getFont(bparams, i + 1);
2667 // We end font definition before blanks
2668 if (!font.equalExceptLatex(running_font) && open_font) {
2669 column += running_font.latexWriteEndChanges(os,
2671 (i == main_body-1) ? basefont : font);
2672 running_font = basefont;
2676 // Blanks are printed before start of fontswitch
2678 // Do not print the separation of the optional argument
2679 if (i != main_body - 1) {
2680 SimpleTeXBlanks(os, texrow, i,
2681 column, font, style);
2685 // Do we need to change font?
2686 if (!font.equalExceptLatex(running_font)
2687 && i != main_body-1) {
2688 column += font.latexWriteStartChanges(os, basefont,
2690 running_font = font;
2694 if (c == LyXParagraph::META_NEWLINE) {
2695 // newlines are handled differently here than
2696 // the default in SimpleTeXSpecialChars().
2697 if (!style.newline_allowed
2698 || font.latex() == LyXFont::ON) {
2702 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2705 basefont = getFont(bparams, -1);
2706 running_font = basefont;
2707 if (font.family() ==
2708 LyXFont::TYPEWRITER_FAMILY) {
2716 texrow.start(this, i + 1);
2719 SimpleTeXSpecialChars(buf, bparams,
2720 os, texrow, moving_arg,
2721 font, running_font, basefont,
2722 open_font, style, i, column, c);
2726 // If we have an open font definition, we have to close it
2728 LyXParagraph * p = 0;
2731 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2732 && (p = NextAfterFootnote()) != 0
2737 running_font.latexWriteEndChanges(os, basefont,
2738 p->getFont(bparams, 0));
2740 running_font.latexWriteEndChanges(os, basefont, basefont);
2743 // Needed if there is an optional argument but no contents.
2744 if (main_body > 0 && main_body == size()) {
2746 return_value = false;
2749 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2750 return return_value;
2754 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2756 bool retval = false;
2758 case LyXParagraph::META_HFILL:
2759 sgml_string.erase();
2761 case LyXParagraph::META_NEWLINE:
2765 sgml_string = "&";
2768 sgml_string = "<";
2771 sgml_string = ">";
2774 sgml_string = "$";
2777 sgml_string = "#";
2780 sgml_string = "%";
2783 sgml_string = "[";
2786 sgml_string = "]";
2789 sgml_string = "{";
2792 sgml_string = "}";
2795 sgml_string = "˜";
2798 sgml_string = """;
2801 sgml_string = "\";
2807 case '\0': // Ignore :-)
2808 sgml_string.erase();
2818 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2819 LyXParagraph::size_type const i,
2820 int & column, LyXFont const & font,
2821 LyXLayout const & style)
2823 if (column > tex_code_break_column
2825 && GetChar(i - 1) != ' '
2827 // In LaTeX mode, we don't want to
2828 // break lines since some commands
2830 && ! (font.latex() == LyXFont::ON)
2831 // same in FreeSpacing mode
2832 && !style.free_spacing
2833 // In typewriter mode, we want to avoid
2834 // ! . ? : at the end of a line
2835 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2836 && (GetChar(i-1) == '.'
2837 || GetChar(i-1) == '?'
2838 || GetChar(i-1) == ':'
2839 || GetChar(i-1) == '!'))) {
2840 if (tex_code_break_column == 0) {
2841 // in batchmode we need LaTeX to still
2842 // see it as a space not as an extra '\n'
2848 texrow.start(this, i + 1);
2850 } else if (font.latex() == LyXFont::OFF) {
2851 if (style.free_spacing) {
2860 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2861 BufferParams const & bparams,
2862 ostream & os, TexRow & texrow,
2865 LyXFont & running_font,
2868 LyXLayout const & style,
2869 LyXParagraph::size_type & i,
2871 LyXParagraph::value_type const c)
2873 // Two major modes: LaTeX or plain
2874 // Handle here those cases common to both modes
2875 // and then split to handle the two modes separately.
2877 case LyXParagraph::META_INSET: {
2878 Inset * inset = GetInset(i);
2881 int const len = os.tellp();
2882 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2883 || inset->LyxCode() == Inset::MATH_CODE
2884 || inset->LyxCode() == Inset::URL_CODE)
2885 && running_font.isRightToLeft()) {
2890 int tmp = inset->Latex(buf, os, moving_arg,
2891 style.free_spacing);
2899 column += os.tellp() - len;
2908 case LyXParagraph::META_NEWLINE:
2910 column += running_font.latexWriteEndChanges(os,
2915 basefont = getFont(bparams, -1);
2916 running_font = basefont;
2919 case LyXParagraph::META_HFILL:
2925 // And now for the special cases within each mode
2926 // Are we in LaTeX mode?
2927 if (font.latex() == LyXFont::ON) {
2928 // at present we only have one option
2929 // but I'll leave it as a switch statement
2930 // so its simpler to extend. (ARRae)
2933 // make sure that we will not print
2934 // error generating chars to the tex
2935 // file. This test would not be needed
2936 // if it were done in the buffer
2944 // Plain mode (i.e. not LaTeX)
2947 os << "\\textbackslash{}";
2951 case '°': case '±': case '²': case '³':
2952 case '×': case '÷': case '¹': case 'ª':
2953 case 'º': case '¬': case 'µ':
2954 if (bparams.inputenc == "latin1" ||
2955 (bparams.inputenc == "auto" &&
2956 font.language()->encoding()->LatexName()
2958 os << "\\ensuremath{"
2967 case '|': case '<': case '>':
2968 // In T1 encoding, these characters exist
2969 if (lyxrc.fontenc == "T1") {
2971 //... but we should avoid ligatures
2972 if ((c == '>' || c == '<')
2974 && GetChar(i + 1) == c) {
2975 //os << "\\textcompwordmark{}";
2976 // Jean-Marc, have a look at
2977 // this. IÂ think this works
2985 // Typewriter font also has them
2986 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2990 // Otherwise, we use what LaTeX
2994 os << "\\textless{}";
2998 os << "\\textgreater{}";
3002 os << "\\textbar{}";
3008 case '-': // "--" in Typewriter mode -> "-{}-"
3010 && GetChar(i + 1) == '-'
3011 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3020 os << "\\char`\\\"{}";
3025 if (bparams.inputenc == "default") {
3034 case '%': case '#': case '{':
3041 os << "\\textasciitilde{}";
3046 os << "\\textasciicircum{}";
3050 case '*': case '[': case ']':
3051 // avoid being mistaken for optional arguments
3052 os << '{' << c << '}';
3057 // Blanks are printed before font switching.
3058 // Sure? I am not! (try nice-latex)
3059 // I am sure it's correct. LyX might be smarter
3060 // in the future, but for now, nothing wrong is
3065 /* idea for labels --- begin*/
3069 && font.family() != LyXFont::TYPEWRITER_FAMILY
3070 && GetChar(i + 1) == 'y'
3071 && GetChar(i + 2) == 'X') {
3079 && font.family() != LyXFont::TYPEWRITER_FAMILY
3080 && GetChar(i + 1) == 'e'
3081 && GetChar(i + 2) == 'X') {
3086 // Check for "LaTeX2e"
3089 && font.family() != LyXFont::TYPEWRITER_FAMILY
3090 && GetChar(i + 1) == 'a'
3091 && GetChar(i + 2) == 'T'
3092 && GetChar(i + 3) == 'e'
3093 && GetChar(i + 4) == 'X'
3094 && GetChar(i + 5) == '2'
3095 && GetChar(i + 6) == 'e') {
3100 // Check for "LaTeX"
3103 && font.family() != LyXFont::TYPEWRITER_FAMILY
3104 && GetChar(i + 1) == 'a'
3105 && GetChar(i + 2) == 'T'
3106 && GetChar(i + 3) == 'e'
3107 && GetChar(i + 4) == 'X') {
3111 /* idea for labels --- end*/
3112 } else if (c != '\0') {
3122 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3123 BufferParams const & bparams,
3124 ostream & os, TexRow & texrow
3127 TexRow & foot_texrow,
3132 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3133 LyXParagraph * par = this;
3136 (par->depth == depth)
3138 && (par->footnoteflag == footnoteflag)
3143 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3145 if (textclasslist.Style(bparams.textclass,
3146 par->layout).isEnvironment()
3147 || par->pextra_type != PEXTRA_NONE) {
3148 par = par->TeXEnvironment(buf, bparams,
3156 par = par->TeXOnePar(buf, bparams,
3166 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3172 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3173 BufferParams const & bparams,
3174 ostream & os, TexRow & texrow
3177 TexRow & foot_texrow,
3182 bool eindent_open = false;
3184 bool foot_this_level = false;
3186 // flags when footnotetext should be appended to file.
3187 static bool minipage_open = false;
3188 static int minipage_open_depth = 0;
3189 char par_sep = bparams.paragraph_separation;
3191 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3194 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3197 LyXLayout const & style =
3198 textclasslist.Style(bparams.textclass,
3201 if (pextra_type == PEXTRA_INDENT) {
3202 if (!pextra_width.empty()) {
3203 os << "\\begin{LyXParagraphIndent}{"
3204 << pextra_width << "}\n";
3206 //float ib = atof(pextra_widthp.c_str())/100;
3207 // string can't handle floats at present (971109)
3208 // so I'll do a conversion by hand knowing that
3209 // the limits are 0.0 to 1.0. ARRae.
3210 os << "\\begin{LyXParagraphIndent}{";
3211 switch (pextra_widthp.length()) {
3223 os << "\\columnwidth}\n";
3226 eindent_open = true;
3228 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3229 if (pextra_hfill && Previous() &&
3230 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3231 os << "\\hfill{}\n";
3234 if (par_sep == BufferParams::PARSEP_INDENT) {
3235 os << "{\\setlength\\parindent{0pt}\n";
3238 os << "\\begin{minipage}";
3239 switch (pextra_alignment) {
3240 case MINIPAGE_ALIGN_TOP:
3243 case MINIPAGE_ALIGN_MIDDLE:
3246 case MINIPAGE_ALIGN_BOTTOM:
3250 if (!pextra_width.empty()) {
3251 os << '{' << pextra_width << "}\n";
3253 //float ib = atof(par->pextra_width.c_str())/100;
3254 // string can't handle floats at present
3255 // so I'll do a conversion by hand knowing that
3256 // the limits are 0.0 to 1.0. ARRae.
3258 switch (pextra_widthp.length()) {
3270 os << "\\columnwidth}\n";
3273 if (par_sep == BufferParams::PARSEP_INDENT) {
3274 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3277 minipage_open = true;
3278 minipage_open_depth = depth;
3281 #ifdef WITH_WARNINGS
3282 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3283 //I disabled it because it breaks when lists span on several
3286 if (style.isEnvironment()){
3287 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3288 #ifdef FANCY_FOOTNOTE_CODE
3289 if (foot_count < 0) {
3290 // flag that footnote[mark][text] should be
3291 // used for any footnotes from now on
3293 foot_this_level = true;
3296 os << "\\begin{" << style.latexname() << "}{"
3297 << labelwidthstring << "}\n";
3298 } else if (style.labeltype == LABEL_BIBLIO) {
3300 os << "\\begin{" << style.latexname() << "}{"
3301 << bibitemWidest(buf)
3303 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3304 #ifdef FANCY_FOOTNOTE_CODE
3305 if (foot_count < 0) {
3306 // flag that footnote[mark][text] should be
3307 // used for any footnotes from now on
3309 foot_this_level = true;
3312 os << "\\begin{" << style.latexname() << '}'
3313 << style.latexparam() << '\n';
3315 os << "\\begin{" << style.latexname() << '}'
3316 << style.latexparam() << '\n';
3319 LyXParagraph * par = this;
3321 par = par->TeXOnePar(buf, bparams,
3325 foot, foot_texrow, foot_count
3329 if (minipage_open && par && !style.isEnvironment() &&
3330 (par->pextra_type == PEXTRA_MINIPAGE) &&
3331 par->pextra_start_minipage) {
3332 os << "\\end{minipage}\n";
3334 if (par_sep == BufferParams::PARSEP_INDENT) {
3338 minipage_open = false;
3340 if (par && par->depth > depth) {
3341 if (textclasslist.Style(bparams.textclass,
3342 par->layout).isParagraph()
3344 // How to handle this? (Lgb)
3345 //&& !suffixIs(os, "\n\n")
3347 // There should be at least one '\n' already
3348 // but we need there to be two for Standard
3349 // paragraphs that are depth-increment'ed to be
3350 // output correctly. However, tables can
3351 // also be paragraphs so don't adjust them.
3354 // Will it ever harm to have one '\n' too
3355 // many? i.e. that we sometimes will have
3356 // three in a row. (Lgb)
3360 par = par->TeXDeeper(buf, bparams, os, texrow
3362 ,foot, foot_texrow, foot_count
3366 if (par && par->layout == layout && par->depth == depth &&
3367 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3368 if (par->pextra_hfill && par->Previous() &&
3369 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3370 os << "\\hfill{}\n";
3373 if (par_sep == BufferParams::PARSEP_INDENT) {
3374 os << "{\\setlength\\parindent{0pt}\n";
3377 os << "\\begin{minipage}";
3378 switch (par->pextra_alignment) {
3379 case MINIPAGE_ALIGN_TOP:
3382 case MINIPAGE_ALIGN_MIDDLE:
3385 case MINIPAGE_ALIGN_BOTTOM:
3389 if (!par->pextra_width.empty()) {
3390 os << '{' << par->pextra_width << "}\n";
3392 //float ib = atof(par->pextra_widthp.c_str())/100;
3393 // string can't handle floats at present
3394 // so I'll do a conversion by hand knowing that
3395 // the limits are 0.0 to 1.0. ARRae.
3397 switch (par->pextra_widthp.length()) {
3402 os << "0." << par->pextra_widthp;
3405 os << "0.0" << par->pextra_widthp;
3407 os << "\\columnwidth}\n";
3410 if (par_sep == BufferParams::PARSEP_INDENT) {
3411 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3414 minipage_open = true;
3415 minipage_open_depth = par->depth;
3418 && par->layout == layout
3419 && par->depth == depth
3420 && par->pextra_type == pextra_type
3422 && par->footnoteflag == footnoteflag
3426 if (style.isEnvironment()) {
3427 os << "\\end{" << style.latexname() << '}';
3429 // maybe this should go after the minipage closes?
3430 if (foot_this_level) {
3431 if (foot_count >= 1) {
3432 if (foot_count > 1) {
3433 os << "\\addtocounter{footnote}{-"
3438 texrow += foot_texrow;
3440 foot_texrow.reset();
3446 if (minipage_open && (minipage_open_depth == depth) &&
3447 (!par || par->pextra_start_minipage ||
3448 par->pextra_type != PEXTRA_MINIPAGE)) {
3449 os << "\\end{minipage}\n";
3451 if (par_sep == BufferParams::PARSEP_INDENT) {
3455 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3456 os << "\\medskip\n\n";
3460 minipage_open = false;
3463 os << "\\end{LyXParagraphIndent}\n";
3466 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3467 && par->pextra_hfill)) {
3471 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3472 return par; // ale970302
3477 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3478 BufferParams const & bparams,
3479 ostream & os, TexRow & texrow,
3480 ostream & foot, TexRow & foot_texrow,
3484 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3485 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3486 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3487 "No footnote!" << endl;
3489 LyXParagraph * par = this;
3490 LyXLayout const & style =
3491 textclasslist.Style(bparams.textclass,
3492 previous->GetLayout());
3494 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3495 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3496 "Float other than footnote in command"
3497 " with moving argument is illegal" << endl;
3500 if (footnotekind != LyXParagraph::FOOTNOTE
3501 && footnotekind != LyXParagraph::MARGIN
3504 // How to solve this?
3505 //&& !suffixIs(file, '\n')
3507 // we need to ensure that real floats like tables and figures
3508 // have their \begin{} on a new line otherwise we can get
3509 // incorrect results when using the endfloat.sty package
3510 // especially if two floats follow one another. ARRae 981022
3511 // NOTE: if the file is length 0 it must have just been
3512 // written out so we assume it ended with a '\n'
3514 // As far as I can see there is never any harm in writing
3515 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3520 bool moving_arg = false;
3521 bool need_closing = false;
3522 bool is_rtl = isRightToLeftPar(bparams);
3524 if (is_rtl != parent_is_rtl) {
3529 need_closing = true;
3532 bool footer_in_body = true;
3533 switch (footnotekind) {
3534 case LyXParagraph::FOOTNOTE:
3535 if (style.intitle) {
3536 os << "\\thanks{\n";
3537 footer_in_body = false;
3540 if (foot_count == -1) {
3541 // we're at depth 0 so we can use:
3542 os << "\\footnote{%\n";
3543 footer_in_body = false;
3545 os << "\\footnotemark{}%\n";
3547 // we only need this when there are
3548 // multiple footnotes
3549 os << "\\stepcounter{footnote}";
3551 os << "\\footnotetext{%\n";
3552 foot_texrow.start(this, 0);
3553 foot_texrow.newline();
3558 case LyXParagraph::MARGIN:
3559 os << "\\marginpar{\n";
3561 case LyXParagraph::FIG:
3562 if (pextra_type == PEXTRA_FLOATFLT
3563 && (!pextra_width.empty()
3564 || !pextra_widthp.empty())) {
3565 if (!pextra_width.empty())
3566 os << "\\begin{floatingfigure}{"
3567 << pextra_width << "}\n";
3569 os << "\\begin{floatingfigure}{"
3570 << lyx::atoi(pextra_widthp) / 100.0
3571 << "\\textwidth}\n";
3573 os << "\\begin{figure}";
3574 if (!bparams.float_placement.empty()) {
3575 os << '[' << bparams.float_placement << "]\n";
3581 case LyXParagraph::TAB:
3582 os << "\\begin{table}";
3583 if (!bparams.float_placement.empty()) {
3584 os << '[' << bparams.float_placement << "]\n";
3589 case LyXParagraph::WIDE_FIG:
3590 os << "\\begin{figure*}";
3591 if (!bparams.float_placement.empty()) {
3592 os << '[' << bparams.float_placement << "]\n";
3597 case LyXParagraph::WIDE_TAB:
3598 os << "\\begin{table*}";
3599 if (!bparams.float_placement.empty()) {
3600 os << '[' << bparams.float_placement << "]\n";
3605 case LyXParagraph::ALGORITHM:
3606 os << "\\begin{algorithm}\n";
3611 if (footnotekind != LyXParagraph::FOOTNOTE
3612 || !footer_in_body) {
3613 // Process text for all floats except footnotes in body
3615 LyXLayout const & style =
3617 .Style(bparams.textclass, par->layout);
3619 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3621 if (style.isEnvironment()
3622 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3623 // Allows the use of minipages within float
3624 // environments. Shouldn't be circular because
3625 // we don't support footnotes inside
3626 // floats (yet). ARRae
3627 par = par->TeXEnvironment(buf, bparams, os,
3632 par = par->TeXOnePar(buf, bparams,
3633 os, texrow, moving_arg,
3638 if (par && !par->IsDummy() && par->depth > depth) {
3639 par = par->TeXDeeper(buf, bparams, os, texrow,
3643 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3645 // process footnotes > depth 0 or in environments separately
3646 // NOTE: Currently don't support footnotes within footnotes
3647 // even though that is possible using the \footnotemark
3648 std::ostringstream dummy;
3649 TexRow dummy_texrow;
3650 int dummy_count = 0;
3652 LyXLayout const & style =
3654 .Style(bparams.textclass, par->layout);
3656 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3658 if (style.isEnvironment()
3659 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3660 // Allows the use of minipages within float
3661 // environments. Shouldn't be circular because
3662 // we don't support footnotes inside
3663 // floats (yet). ARRae
3664 par = par->TeXEnvironment(buf, bparams,
3666 dummy, dummy_texrow,
3669 par = par->TeXOnePar(buf, bparams,
3672 dummy, dummy_texrow,
3676 if (par && !par->IsDummy() && par->depth > depth) {
3677 par = par->TeXDeeper(buf, bparams,
3679 dummy, dummy_texrow,
3683 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3685 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3686 "Footnote in a Footnote -- not supported"
3691 switch (footnotekind) {
3692 case LyXParagraph::FOOTNOTE:
3693 if (footer_in_body) {
3694 // This helps tell which of the multiple
3695 // footnotetexts an error was in.
3697 foot_texrow.newline();
3702 case LyXParagraph::MARGIN:
3705 case LyXParagraph::FIG:
3706 if (pextra_type == PEXTRA_FLOATFLT
3707 && (!pextra_width.empty()
3708 || !pextra_widthp.empty()))
3709 os << "\\end{floatingfigure}";
3711 os << "\\end{figure}";
3713 case LyXParagraph::TAB:
3714 os << "\\end{table}";
3716 case LyXParagraph::WIDE_FIG:
3717 os << "\\end{figure*}";
3719 case LyXParagraph::WIDE_TAB:
3720 os << "\\end{table*}";
3722 case LyXParagraph::ALGORITHM:
3723 os << "\\end{algorithm}";
3730 if (footnotekind != LyXParagraph::FOOTNOTE
3731 && footnotekind != LyXParagraph::MARGIN) {
3732 // we need to ensure that real floats like tables and figures
3733 // have their \end{} on a line of their own otherwise we can
3734 // get incorrect results when using the endfloat.sty package.
3739 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3744 bool LyXParagraph::IsDummy() const
3746 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3747 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3751 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3752 int type, string const & width,
3753 string const & widthp)
3756 pextra_width = width;
3757 pextra_widthp = widthp;
3759 if (textclasslist.Style(bparams.textclass,
3760 layout).isEnvironment()) {
3761 LyXParagraph * par = this;
3762 LyXParagraph * ppar = par;
3764 while (par && (par->layout == layout)
3765 && (par->depth == depth)) {
3767 par = par->Previous();
3770 par = par->FirstPhysicalPar();
3772 while (par && par->depth > depth) {
3773 par = par->Previous();
3776 par = par->FirstPhysicalPar();
3781 while (par && (par->layout == layout)
3782 && (par->depth == depth)) {
3783 par->pextra_type = type;
3784 par->pextra_width = width;
3785 par->pextra_widthp = widthp;
3787 par = par->NextAfterFootnote();
3791 if (par && (par->depth > depth))
3792 par->SetPExtraType(bparams,
3793 type, width, widthp);
3795 while (par && ((par->depth > depth) || par->IsDummy()))
3796 par = par->NextAfterFootnote();
3798 while (par && ((par->depth > depth)))
3806 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3808 if (pextra_type == PEXTRA_NONE)
3811 pextra_type = PEXTRA_NONE;
3812 pextra_width.erase();
3813 pextra_widthp.erase();
3815 if (textclasslist.Style(bparams.textclass,
3816 layout).isEnvironment()) {
3817 LyXParagraph * par = this;
3818 LyXParagraph * ppar = par;
3820 while (par && (par->layout == layout)
3821 && (par->depth == depth)) {
3823 par = par->Previous();
3826 par = par->FirstPhysicalPar();
3828 while (par && par->depth > depth) {
3829 par = par->Previous();
3832 par = par->FirstPhysicalPar();
3837 while (par && (par->layout == layout)
3838 && (par->depth == depth)) {
3839 par->pextra_type = PEXTRA_NONE;
3840 par->pextra_width.erase();
3841 par->pextra_widthp.erase();
3843 par = par->NextAfterFootnote();
3847 if (par && (par->depth > depth))
3848 par->UnsetPExtraType(bparams);
3850 while (par && ((par->depth > depth) || par->IsDummy()))
3851 par = par->NextAfterFootnote();
3853 while (par && ((par->depth > depth)))
3861 bool LyXParagraph::IsHfill(size_type pos) const
3863 return IsHfillChar(GetChar(pos));
3867 bool LyXParagraph::IsInset(size_type pos) const
3869 return IsInsetChar(GetChar(pos));
3874 bool LyXParagraph::IsFloat(size_type pos) const
3876 return IsFloatChar(GetChar(pos));
3881 bool LyXParagraph::IsNewline(size_type pos) const
3883 return pos >= 0 && IsNewlineChar(GetChar(pos));
3887 bool LyXParagraph::IsSeparator(size_type pos) const
3889 return IsSeparatorChar(GetChar(pos));
3893 bool LyXParagraph::IsLineSeparator(size_type pos) const
3895 return IsLineSeparatorChar(GetChar(pos));
3899 bool LyXParagraph::IsKomma(size_type pos) const
3901 return IsKommaChar(GetChar(pos));
3905 /// Used by the spellchecker
3906 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3908 value_type const c = GetChar(pos);
3909 if (IsLetterChar(c))
3911 // '\0' is not a letter, allthough every string contains "" (below)
3914 // We want to pass the ' and escape chars to ispell
3915 string const extra = lyxrc.isp_esc_chars + '\'';
3916 char ch[2] = { c, 0 };
3917 return contains(extra, ch);
3921 bool LyXParagraph::IsWord(size_type pos ) const
3923 return IsWordChar(GetChar(pos)) ;
3928 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3932 return FirstPhysicalPar()->getParLanguage(bparams);
3936 #ifdef DO_USE_DEFAULT_LANGUAGE
3937 Language const * lang = GetFirstFontSettings().language();
3939 if (lang->lang() == "default")
3940 return bparams.language;
3943 return GetFirstFontSettings().language();
3945 } else if (previous)
3946 return previous->getParLanguage(bparams);
3948 return bparams.language;
3952 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3954 return lyxrc.rtl_support
3955 && getParLanguage(bparams)->RightToLeft();
3959 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3960 Language const * from, Language const * to)
3962 for (size_type i = 0; i < size(); ++i) {
3963 LyXFont font = GetFontSettings(bparams, i);
3964 if (font.language() == from) {
3965 font.setLanguage(to);
3972 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3974 Language const * doc_language = bparams.language;
3975 for (FontList::const_iterator cit = fontlist.begin();
3976 cit != fontlist.end(); ++cit)
3977 if ((*cit).font.language() != doc_language)
3983 // Convert the paragraph to a string.
3984 // Used for building the table of contents
3985 string const LyXParagraph::String(Buffer const * buffer, bool label)
3987 BufferParams const & bparams = buffer->params;
3990 if (label && !IsDummy() && !labelstring.empty())
3992 if (label && !labelstring.empty())
3994 s += labelstring + ' ';
3995 string::size_type const len = s.size();
3997 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3998 value_type c = GetChar(i);
4001 else if (c == META_INSET &&
4002 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
4003 std::ostringstream ost;
4004 GetInset(i)->Ascii(buffer, ost);
4005 s += subst(ost.str().c_str(),'\n',' ');
4010 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
4011 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
4012 s += NextAfterFootnote()->String(buffer, false);
4016 if (isRightToLeftPar(bparams))
4017 reverse(s.begin() + len,s.end());
4025 string const LyXParagraph::String(Buffer const * buffer,
4026 LyXParagraph::size_type beg,
4027 LyXParagraph::size_type end)
4032 if (beg == 0 && !IsDummy() && !labelstring.empty())
4034 if (beg == 0 && !labelstring.empty())
4036 s += labelstring + ' ';
4038 for (LyXParagraph::size_type i = beg; i < end; ++i) {
4039 value_type c = GetUChar(buffer->params, i);
4042 else if (c == META_INSET) {
4043 std::ostringstream ost;
4044 GetInset(i)->Ascii(buffer, ost);
4045 s += ost.str().c_str();
4053 void LyXParagraph::SetInsetOwner(Inset * i)
4056 for (InsetList::const_iterator cit = insetlist.begin();
4057 cit != insetlist.end(); ++cit) {
4059 (*cit).inset->setOwner(i);
4064 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4067 for (InsetList::const_iterator cit = insetlist.begin();
4068 cit != insetlist.end(); ++cit) {
4070 if ((*cit).inset->IsTextInset()) {
4071 static_cast<UpdatableInset *>
4072 ((*cit).inset)->deleteLyXText(bv);
4079 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4082 for (InsetList::const_iterator cit = insetlist.begin();
4083 cit != insetlist.end(); ++cit) {
4085 if ((*cit).inset->IsTextInset()) {
4086 static_cast<UpdatableInset *>
4087 ((*cit).inset)->resizeLyXText(bv);