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 // return an string of the current word, and the end of the word in lastpos.
1022 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1024 Assert(lastpos >= 0);
1026 // the current word is defined as starting at the first character
1027 // from the immediate left of lastpospos which meets the definition
1028 // of IsLetter(), continuing to the last character to the right
1029 // of this meeting IsLetter.
1035 // move back until we have a letter
1037 //there's no real reason to have firstpos & lastpos as
1038 //separate variables as this is written, but maybe someon
1039 // will want to return firstpos in the future.
1041 //since someone might have typed a punctuation first
1042 int firstpos = lastpos;
1044 while ((firstpos >= 0) && !IsLetter(firstpos))
1047 // now find the beginning by looking for a nonletter
1049 while ((firstpos>= 0) && IsLetter(firstpos))
1052 // the above is now pointing to the preceeding non-letter
1056 // so copy characters into theword until we get a nonletter
1057 // note that this can easily exceed lastpos, wich means
1058 // that if used in the middle of a word, the whole word
1061 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1069 #warning Remember to get rid of this one. (Lgb)
1071 LyXParagraph::size_type LyXParagraph::Last() const
1074 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1075 return text.size() + NextAfterFootnote()->Last() + 1;
1076 // the 1 is the symbol
1085 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1087 // > because last is the next unused position, and you can
1088 // use it if you want
1091 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1092 return NextAfterFootnote()
1093 ->ParFromPos(pos - text.size() - 1);
1095 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1096 "position does not exist." << endl;
1105 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1107 // > because last is the next unused position, and you can
1108 // use it if you want
1111 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1112 return NextAfterFootnote()
1113 ->PositionInParFromPos(pos - text.size() - 1);
1116 "ERROR (LyXParagraph::PositionInParFromPos): "
1117 "position does not exist." << endl;
1126 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1127 LyXFont const & font)
1130 // > because last is the next unused position, and you can
1131 // use it if you want
1134 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1135 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1138 lyxerr << "ERROR (LyXParagraph::SetFont): "
1139 "position does not exist." << endl;
1144 Assert(pos <= size());
1147 // First, reduce font against layout/label font
1148 // Update: The SetCharFont() routine in text2.C already
1149 // reduces font, so we don't need to do that here. (Asger)
1150 // No need to simplify this because it will disappear
1151 // in a new kernel. (Asger)
1152 // Next search font table
1154 FontTable search_font(pos, LyXFont());
1155 FontList::iterator it = lower_bound(fontlist.begin(),
1157 search_font, matchFT());
1158 unsigned int i = it - fontlist.begin();
1159 bool notfound = it == fontlist.end();
1161 if (!notfound && fontlist[i].font == font)
1164 bool begin = pos == 0 || notfound ||
1165 (i > 0 && fontlist[i-1].pos == pos - 1);
1166 // Is position pos is a beginning of a font block?
1167 bool end = !notfound && fontlist[i].pos == pos;
1168 // Is position pos is the end of a font block?
1169 if (begin && end) { // A single char block
1170 if (i + 1 < fontlist.size() &&
1171 fontlist[i + 1].font == font) {
1172 // Merge the singleton block with the next block
1173 fontlist.erase(fontlist.begin() + i);
1174 if (i > 0 && fontlist[i - 1].font == font)
1175 fontlist.erase(fontlist.begin() + i-1);
1176 } else if (i > 0 && fontlist[i - 1].font == font) {
1177 // Merge the singleton block with the previous block
1178 fontlist[i - 1].pos = pos;
1179 fontlist.erase(fontlist.begin() + i);
1181 fontlist[i].font = font;
1183 if (i > 0 && fontlist[i - 1].font == font)
1184 fontlist[i - 1].pos = pos;
1186 fontlist.insert(fontlist.begin() + i,
1187 FontTable(pos, font));
1189 fontlist[i].pos = pos - 1;
1190 if (!(i + 1 < fontlist.size() &&
1191 fontlist[i + 1].font == font))
1192 fontlist.insert(fontlist.begin() + i + 1,
1193 FontTable(pos, font));
1194 } else { // The general case. The block is splitted into 3 blocks
1195 fontlist.insert(fontlist.begin() + i,
1196 FontTable(pos - 1, fontlist[i].font));
1197 fontlist.insert(fontlist.begin() + i + 1,
1198 FontTable(pos, font));
1203 // This function is able to hide closed footnotes.
1204 LyXParagraph * LyXParagraph::Next()
1207 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1208 LyXParagraph * tmp = next;
1210 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1212 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1213 return tmp->Next(); /* there can be more than one
1214 footnote in a logical
1217 return next; // This should never happen!
1225 LyXParagraph * LyXParagraph::NextAfterFootnote()
1227 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1228 LyXParagraph * tmp = next;
1229 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1231 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1232 return tmp; /* there can be more than one footnote
1233 in a logical paragraph */
1235 return next; // This should never happen!
1243 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1245 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1246 LyXParagraph * tmp = next;
1247 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1249 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1250 return tmp; /* there can be more than one footnote
1251 in a logical paragraph */
1253 return next; // This should never happen!
1261 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1264 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1266 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1267 tmp = tmp->previous;
1268 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1269 return tmp; /* there can be more than one footnote
1270 in a logical paragraph */
1272 return previous; // This should never happen!
1280 LyXParagraph * LyXParagraph::LastPhysicalPar()
1282 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1285 LyXParagraph * tmp = this;
1287 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1288 tmp = tmp->NextAfterFootnote();
1296 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1298 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1301 LyXParagraph const * tmp = this;
1303 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1304 tmp = tmp->NextAfterFootnote();
1312 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1316 LyXParagraph * tmppar = this;
1320 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1321 tmppar = tmppar->previous;
1332 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1336 LyXParagraph const * tmppar = this;
1340 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1341 tmppar = tmppar->previous;
1351 // This function is able to hide closed footnotes.
1352 LyXParagraph * LyXParagraph::Previous()
1355 LyXParagraph * tmp = previous;
1360 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1361 tmp = tmp->previous;
1363 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1364 tmp = tmp->previous;
1365 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1366 return tmp->next->Previous();
1376 // This function is able to hide closed footnotes.
1377 LyXParagraph const * LyXParagraph::Previous() const
1380 LyXParagraph * tmp = previous;
1384 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1385 tmp = tmp->previous;
1387 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1388 tmp = tmp->previous;
1389 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1390 return tmp->next->Previous();
1400 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1401 LyXParagraph::size_type pos,
1406 // create a new paragraph
1409 size_type pos_first;
1410 LyXParagraph * par = ParFromPos(pos);
1411 LyXParagraph * firstpar = FirstPhysicalPar();
1413 LyXParagraph * tmp = new LyXParagraph(par);
1415 //LyXParagraph * par = this;
1416 //LyXParagraph * firstpar = this;
1417 LyXParagraph * tmp = new LyXParagraph(this);
1421 tmp->footnoteflag = footnoteflag;
1422 tmp->footnotekind = footnotekind;
1424 // this is an idea for a more userfriendly layout handling, I will
1425 // see what the users say
1428 // layout stays the same with latex-environments
1430 tmp->SetOnlyLayout(bparams, firstpar->layout);
1431 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1434 // layout stays the same with latex-environments
1436 tmp->SetOnlyLayout(bparams, layout);
1437 tmp->SetLabelWidthString(labelwidthstring);
1441 if (Last() > pos || !Last() || flag == 2) {
1442 tmp->SetOnlyLayout(bparams, firstpar->layout);
1443 tmp->align = firstpar->align;
1444 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1446 tmp->line_bottom = firstpar->line_bottom;
1447 firstpar->line_bottom = false;
1448 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1449 firstpar->pagebreak_bottom = false;
1450 tmp->added_space_bottom = firstpar->added_space_bottom;
1451 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1453 tmp->depth = firstpar->depth;
1454 tmp->noindent = firstpar->noindent;
1456 if (Last() > pos || !Last() || flag == 2) {
1457 tmp->SetOnlyLayout(bparams, layout);
1459 tmp->SetLabelWidthString(labelwidthstring);
1461 tmp->line_bottom = line_bottom;
1462 line_bottom = false;
1463 tmp->pagebreak_bottom = pagebreak_bottom;
1464 pagebreak_bottom = false;
1465 tmp->added_space_bottom = added_space_bottom;
1466 added_space_bottom = VSpace(VSpace::NONE);
1469 tmp->noindent = noindent;
1471 // copy everything behind the break-position
1472 // to the new paragraph
1475 while (ParFromPos(pos_first) != par)
1477 pos_end = pos_first + par->text.size() - 1;
1480 for (i = j = pos; i <= pos_end; ++i) {
1481 par->CutIntoMinibuffer(bparams, i - pos_first);
1482 if (tmp->InsertFromMinibuffer(j - pos))
1488 for (i = pos_end; i >= pos; --i)
1489 par->Erase(i - pos_first);
1493 size_type pos_end = text.size() - 1;
1495 for (i = j = pos; i <= pos_end; ++i) {
1496 CutIntoMinibuffer(bparams, i);
1497 if (tmp->InsertFromMinibuffer(j - pos))
1501 for (i = pos_end; i >= pos; --i)
1509 // just an idea of me
1511 tmp->line_top = firstpar->line_top;
1512 tmp->pagebreak_top = firstpar->pagebreak_top;
1513 tmp->added_space_top = firstpar->added_space_top;
1514 tmp->bibkey = firstpar->bibkey;
1516 // layout stays the same with latex-environments
1518 firstpar->SetOnlyLayout(bparams, tmp->layout);
1519 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1520 firstpar->depth = tmp->depth;
1524 // just an idea of me
1526 tmp->line_top = line_top;
1527 tmp->pagebreak_top = pagebreak_top;
1528 tmp->added_space_top = added_space_top;
1529 tmp->bibkey = bibkey;
1531 // layout stays the same with latex-environments
1533 SetOnlyLayout(bparams, tmp->layout);
1534 SetLabelWidthString(tmp->labelwidthstring);
1542 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1545 par = par->FirstPhysicalPar();
1546 footnoteflag = par->footnoteflag;
1547 footnotekind = par->footnotekind;
1549 layout = par->layout;
1550 align = par-> align;
1551 SetLabelWidthString(par->labelwidthstring);
1553 line_bottom = par->line_bottom;
1554 pagebreak_bottom = par->pagebreak_bottom;
1555 added_space_bottom = par->added_space_bottom;
1557 line_top = par->line_top;
1558 pagebreak_top = par->pagebreak_top;
1559 added_space_top = par->added_space_top;
1561 spacing = par->spacing;
1563 pextra_type = par->pextra_type;
1564 pextra_width = par->pextra_width;
1565 pextra_widthp = par->pextra_widthp;
1566 pextra_alignment = par->pextra_alignment;
1567 pextra_hfill = par->pextra_hfill;
1568 pextra_start_minipage = par->pextra_start_minipage;
1570 noindent = par->noindent;
1576 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1578 LyXParagraph * tmppar = this;
1581 && tmppar->previous->footnoteflag ==
1582 LyXParagraph::CLOSED_FOOTNOTE)
1583 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1584 tmppar = tmppar->previous;
1587 return this; // This should never happen!
1594 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1596 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1604 && (IsNewline(0) || IsLineSeparator(0))){
1615 LyXParagraph * LyXParagraph::Clone() const
1617 // create a new paragraph
1618 LyXParagraph * result = new LyXParagraph;
1620 result->MakeSameLayout(this);
1622 // this is because of the dummy layout of the paragraphs that
1624 result->layout = layout;
1626 result->inset_owner = inset_owner;
1630 result->bibkey = static_cast<InsetBibKey *>
1631 (bibkey->Clone(*current_view->buffer()));
1635 // copy everything behind the break-position to the new paragraph
1637 result->text = text;
1638 result->fontlist = fontlist;
1639 result->insetlist = insetlist;
1640 for (InsetList::iterator it = result->insetlist.begin();
1641 it != result->insetlist.end(); ++it)
1642 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1647 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1650 par = par->FirstPhysicalPar();
1655 par->footnoteflag == footnoteflag &&
1656 par->footnotekind == footnotekind &&
1658 par->layout == layout &&
1660 par->align == align &&
1662 par->line_bottom == line_bottom &&
1663 par->pagebreak_bottom == pagebreak_bottom &&
1664 par->added_space_bottom == added_space_bottom &&
1666 par->line_top == line_top &&
1667 par->pagebreak_top == pagebreak_top &&
1668 par->added_space_top == added_space_top &&
1670 par->spacing == spacing &&
1672 par->pextra_type == pextra_type &&
1673 par->pextra_width == pextra_width &&
1674 par->pextra_widthp == pextra_widthp &&
1675 par->pextra_alignment == pextra_alignment &&
1676 par->pextra_hfill == pextra_hfill &&
1677 par->pextra_start_minipage == pextra_start_minipage &&
1678 par->noindent == noindent &&
1679 par->depth == depth);
1683 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1684 LyXParagraph::size_type pos)
1687 // create a new paragraph
1688 LyXParagraph * par = ParFromPos(pos);
1690 LyXParagraph * tmp = new LyXParagraph(par);
1692 tmp->MakeSameLayout(par);
1694 // When can pos < Last()?
1695 // I guess pos == Last() is possible.
1697 // copy everything behind the break-position to the new
1699 size_type pos_first = 0;
1700 while (ParFromPos(pos_first) != par)
1702 size_type pos_end = pos_first + par->text.size() - 1;
1705 for (i = j = pos; i <= pos_end; ++i) {
1706 par->CutIntoMinibuffer(bparams, i - pos_first);
1707 if (tmp->InsertFromMinibuffer(j - pos))
1713 for (size_type i = pos_end; i >= pos; --i)
1714 par->Erase(i - pos_first);
1719 // create a new paragraph
1720 LyXParagraph * tmp = new LyXParagraph(this);
1721 tmp->MakeSameLayout(this);
1723 // When can pos > Last()?
1724 // I guess pos == Last() is possible.
1726 // copy everything behind the break-position to the new
1728 size_type pos_end = text.size() - 1;
1731 for (i = j = pos; i <= pos_end; ++i) {
1732 CutIntoMinibuffer(bparams, i);
1733 if (tmp->InsertFromMinibuffer(j - pos))
1739 for (size_type i = pos_end; i >= pos; --i)
1748 // Be carefull, this does not make any check at all.
1749 // This method has wrong name, it combined this par with the next par.
1750 // In that sense it is the reverse of break paragraph. (Lgb)
1751 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1753 // copy the next paragraph to this one
1754 LyXParagraph * the_next = Next();
1756 LyXParagraph * firstpar = FirstPhysicalPar();
1759 // first the DTP-stuff
1761 firstpar->line_bottom = the_next->line_bottom;
1762 firstpar->added_space_bottom = the_next->added_space_bottom;
1763 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1765 line_bottom = the_next->line_bottom;
1766 added_space_bottom = the_next->added_space_bottom;
1767 pagebreak_bottom = the_next->pagebreak_bottom;
1770 size_type pos_end = the_next->text.size() - 1;
1771 size_type pos_insert = Last();
1773 // ok, now copy the paragraph
1775 for (i = j = 0; i <= pos_end; ++i) {
1776 the_next->CutIntoMinibuffer(bparams, i);
1777 if (InsertFromMinibuffer(pos_insert + j))
1781 // delete the next paragraph
1782 LyXParagraph * ppar = the_next->previous;
1783 LyXParagraph * npar = the_next->next;
1790 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1792 LyXParagraph * par = ParFromPos(pos);
1794 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1795 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1801 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1803 LyXParagraph * par = ParFromPos(pos);
1805 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1806 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1813 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1815 LyXParagraph const * par = this;
1816 int par_depth = GetDepth();
1818 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1819 int const endlabeltype =
1820 textclasslist.Style(bparams.textclass,
1821 layout).endlabeltype;
1822 if (endlabeltype != END_LABEL_NO_LABEL) {
1823 LyXParagraph const * last = this;
1825 if (footnoteflag == NO_FOOTNOTE)
1826 last = LastPhysicalPar();
1827 else if (next->footnoteflag == NO_FOOTNOTE)
1828 return endlabeltype;
1833 if (!last || !last->next)
1834 return endlabeltype;
1836 int next_depth = last->next->GetDepth();
1837 if (par_depth > next_depth ||
1838 (par_depth == next_depth && layout != last->next->GetLayout() ))
1839 return endlabeltype;
1844 par = par->DepthHook(par_depth - 1);
1846 par_depth = par->GetDepth();
1848 return END_LABEL_NO_LABEL;
1852 LyXTextClass::size_type LyXParagraph::GetLayout() const
1855 return FirstPhysicalPar()->layout;
1862 char LyXParagraph::GetDepth() const
1865 return FirstPhysicalPar()->depth;
1872 char LyXParagraph::GetAlign() const
1875 return FirstPhysicalPar()->align;
1882 string const & LyXParagraph::GetLabelstring() const
1885 return FirstPhysicalPar()->labelstring;
1892 int LyXParagraph::GetFirstCounter(int i) const
1895 return FirstPhysicalPar()->counter_[i];
1902 // the next two functions are for the manual labels
1903 string const LyXParagraph::GetLabelWidthString() const
1906 if (!FirstPhysicalPar()->labelwidthstring.empty())
1907 return FirstPhysicalPar()->labelwidthstring;
1909 if (!labelwidthstring.empty())
1910 return labelwidthstring;
1913 return _("Senseless with this layout!");
1917 void LyXParagraph::SetLabelWidthString(string const & s)
1920 LyXParagraph * par = FirstPhysicalPar();
1922 par->labelwidthstring = s;
1924 labelwidthstring = s;
1929 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1930 LyXTextClass::size_type new_layout)
1933 LyXParagraph * par = FirstPhysicalPar();
1935 LyXParagraph * par = this;
1937 LyXParagraph * ppar = 0;
1938 LyXParagraph * npar = 0;
1940 par->layout = new_layout;
1942 if (par->pextra_type == PEXTRA_NONE) {
1943 if (par->Previous()) {
1945 ppar = par->Previous()->FirstPhysicalPar();
1947 ppar = par->Previous();
1951 && (ppar->depth > par->depth))
1953 ppar = ppar->Previous()->FirstPhysicalPar();
1955 ppar = ppar->Previous();
1960 npar = par->Next()->NextAfterFootnote();
1966 && (npar->depth > par->depth))
1968 npar = npar->Next()->NextAfterFootnote();
1970 npar = npar->Next();
1973 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1975 p1 = ppar->pextra_width,
1976 p2 = ppar->pextra_widthp;
1977 ppar->SetPExtraType(bparams, ppar->pextra_type,
1980 if ((par->pextra_type == PEXTRA_NONE) &&
1981 npar && (npar->pextra_type != PEXTRA_NONE)) {
1982 string const p1 = npar->pextra_width;
1983 string const p2 = npar->pextra_widthp;
1984 npar->SetPExtraType(bparams, npar->pextra_type,
1991 void LyXParagraph::SetLayout(BufferParams const & bparams,
1992 LyXTextClass::size_type new_layout)
1996 * par = FirstPhysicalPar(),
2003 par->layout = new_layout;
2004 par->labelwidthstring.erase();
2005 par->align = LYX_ALIGN_LAYOUT;
2006 par->added_space_top = VSpace(VSpace::NONE);
2007 par->added_space_bottom = VSpace(VSpace::NONE);
2008 par->spacing.set(Spacing::Default);
2010 if (par->pextra_type == PEXTRA_NONE) {
2011 if (par->Previous()) {
2013 ppar = par->Previous()->FirstPhysicalPar();
2015 ppar = par->Previous();
2019 && (ppar->depth > par->depth))
2021 ppar = ppar->Previous()->FirstPhysicalPar();
2023 ppar = ppar->Previous();
2028 npar = par->Next()->NextAfterFootnote();
2034 && (npar->depth > par->depth))
2036 npar = npar->Next()->NextAfterFootnote();
2038 npar = npar->Next();
2041 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2042 string const p1 = ppar->pextra_width;
2043 string const p2 = ppar->pextra_widthp;
2044 ppar->SetPExtraType(bparams, ppar->pextra_type,
2047 if ((par->pextra_type == PEXTRA_NONE) &&
2048 npar && (npar->pextra_type != PEXTRA_NONE)) {
2049 string const p1 = npar->pextra_width;
2050 string const p2 = npar->pextra_widthp;
2051 npar->SetPExtraType(bparams, npar->pextra_type,
2058 // if the layout of a paragraph contains a manual label, the beginning of the
2059 // main body is the beginning of the second word. This is what the par-
2060 // function returns. If the layout does not contain a label, the main
2061 // body always starts with position 0. This differentiation is necessary,
2062 // because there cannot be a newline or a blank <= the beginning of the
2063 // main body in TeX.
2065 int LyXParagraph::BeginningOfMainBody() const
2068 if (FirstPhysicalPar() != this)
2071 // Unroll the first two cycles of the loop
2072 // and remember the previous character to
2073 // remove unnecessary GetChar() calls
2076 && GetChar(i) != LyXParagraph::META_NEWLINE
2079 char previous_char = 0;
2082 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2083 // Yes, this ^ is supposed to be "= " not "=="
2086 && previous_char != ' '
2087 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2089 previous_char = temp;
2095 if (i == 0 && i == size() &&
2096 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2097 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2098 ++i; /* the cursor should not jump
2099 * to the main body if there
2106 LyXParagraph * LyXParagraph::DepthHook(int deth)
2108 LyXParagraph * newpar = this;
2114 newpar = newpar->FirstPhysicalPar()->Previous();
2116 newpar = newpar->Previous();
2118 } while (newpar && newpar->GetDepth() > deth
2120 && newpar->footnoteflag == footnoteflag
2125 if (Previous() || GetDepth())
2126 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2131 return newpar->FirstPhysicalPar();
2138 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2140 LyXParagraph const * newpar = this;
2146 newpar = newpar->FirstPhysicalPar()->Previous();
2148 newpar = newpar->Previous();
2150 } while (newpar && newpar->GetDepth() > deth
2152 && newpar->footnoteflag == footnoteflag
2157 if (Previous() || GetDepth())
2158 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2163 return newpar->FirstPhysicalPar();
2170 int LyXParagraph::AutoDeleteInsets()
2173 InsetList::size_type index = 0;
2174 while (index < insetlist.size()) {
2175 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2176 Erase(insetlist[index].pos);
2177 // Erase() calls to insetlist.erase(&insetlist[index])
2178 // so index shouldn't be increased.
2187 LyXParagraph::inset_iterator
2188 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2190 InsetTable search_inset(pos, 0);
2191 InsetList::iterator it = lower_bound(insetlist.begin(),
2193 search_inset, matchIT());
2194 return inset_iterator(it);
2198 // returns -1 if inset not found
2199 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2202 for (InsetList::const_iterator cit = insetlist.begin();
2203 cit != insetlist.end(); ++cit) {
2204 if ((*cit).inset == inset) {
2209 // Think about footnotes.
2210 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2211 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2213 NextAfterFootnote()->GetPositionOfInset(inset);
2215 return text.size() + 1 + further;
2222 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2223 BufferParams const & bparams,
2224 ostream & os, TexRow & texrow,
2229 TexRow & foot_texrow,
2234 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2235 LyXLayout const & style =
2236 textclasslist.Style(bparams.textclass,
2239 bool further_blank_line = false;
2242 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2245 if (start_of_appendix) {
2246 os << "\\appendix\n";
2250 if (!spacing.isDefault()
2251 && (!Previous() || !Previous()->HasSameLayout(this))) {
2252 os << spacing.writeEnvirBegin() << "\n";
2256 if (tex_code_break_column && style.isCommand()){
2261 if (pagebreak_top) {
2263 further_blank_line = true;
2265 if (added_space_top.kind() != VSpace::NONE) {
2266 os << added_space_top.asLatexCommand(bparams);
2267 further_blank_line = true;
2271 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2272 << "\\vspace{-1\\parskip}";
2273 further_blank_line = true;
2276 if (further_blank_line){
2281 Language const * language = getParLanguage(bparams);
2282 Language const * doc_language = bparams.language;
2283 Language const * previous_language = previous
2284 ? previous->getParLanguage(bparams) : doc_language;
2285 if (language->babel() != doc_language->babel() &&
2286 language->babel() != previous_language->babel()) {
2287 os << subst(lyxrc.language_command_begin, "$$lang",
2293 if (bparams.inputenc == "auto" &&
2294 language->encoding() != previous_language->encoding()) {
2295 os << "\\inputencoding{"
2296 << language->encoding()->LatexName()
2301 switch (style.latextype) {
2304 << style.latexname()
2305 << style.latexparam();
2307 case LATEX_ITEM_ENVIRONMENT:
2309 bibkey->Latex(buf, os, false, false);
2313 case LATEX_LIST_ENVIRONMENT:
2320 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2322 LyXParagraph * par = next;
2324 // Spit out footnotes
2325 if (lyxrc.rtl_support) {
2326 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2327 && next->footnoteflag != footnoteflag) {
2328 LyXParagraph * p = 0;
2329 bool is_rtl = (size() > 0)
2330 ? GetFontSettings(bparams,
2331 size()-1).isRightToLeft()
2332 : language->RightToLeft();
2333 if ((p = NextAfterFootnote()) != 0 &&
2335 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2336 is_rtl = getParLanguage(bparams)->RightToLeft();
2337 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2338 && par->footnoteflag != footnoteflag) {
2339 par = par->TeXFootnote(buf, bparams,
2341 foot_texrow, foot_count,
2343 par->SimpleTeXOnePar(buf, bparams,
2344 os, texrow, moving_arg);
2345 is_rtl = (par->size() > 0)
2346 ? par->GetFontSettings(bparams,
2347 par->size()-1).isRightToLeft()
2348 : language->RightToLeft();
2350 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2351 (p = par->NextAfterFootnote()) != 0 &&
2353 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2354 is_rtl = language->RightToLeft();
2359 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2360 && par->footnoteflag != footnoteflag) {
2361 par = par->TeXFootnote(buf, bparams,
2363 foot, foot_texrow, foot_count,
2365 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2371 // Make sure that \\par is done with the font of the last
2372 // character if this has another size as the default.
2373 // This is necessary because LaTeX (and LyX on the screen)
2374 // calculates the space between the baselines according
2375 // to this font. (Matthias)
2377 // Is this really needed ? (Dekel)
2378 // We do not need to use to change the font for the last paragraph
2379 // or for a command.
2380 LyXFont font = getFont(bparams, Last() - 1);
2381 bool is_command = textclasslist.Style(bparams.textclass,
2382 GetLayout()).isCommand();
2383 if (style.resfont.size() != font.size() && next && !is_command) {
2386 os << "\\" << font.latexSize() << " \\par}";
2387 } else if (need_par) {
2389 } else if (is_command)
2392 if (language->babel() != doc_language->babel() &&
2395 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2397 || par->getParLanguage(bparams)->babel() != language->babel())) {
2399 << subst(lyxrc.language_command_end, "$$lang",
2400 doc_language->babel());
2403 switch (style.latextype) {
2404 case LATEX_ITEM_ENVIRONMENT:
2405 case LATEX_LIST_ENVIRONMENT:
2406 if (par && (depth < par->depth)) {
2411 case LATEX_ENVIRONMENT:
2412 // if its the last paragraph of the current environment
2413 // skip it otherwise fall through
2415 && (par->layout != layout
2416 || par->depth != depth
2417 || par->pextra_type != pextra_type))
2420 // we don't need it for the last paragraph!!!
2427 further_blank_line = false;
2429 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2430 further_blank_line = true;
2433 if (added_space_bottom.kind() != VSpace::NONE) {
2434 os << added_space_bottom.asLatexCommand(bparams);
2435 further_blank_line = true;
2438 if (pagebreak_bottom) {
2440 further_blank_line = true;
2443 if (further_blank_line){
2448 if (!spacing.isDefault()
2449 && (!par || !par->HasSameLayout(this))) {
2450 os << spacing.writeEnvirEnd() << "\n";
2454 // we don't need it for the last paragraph!!!
2457 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2458 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2465 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2470 // This one spits out the text of the paragraph
2471 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2472 BufferParams const & bparams,
2473 ostream & os, TexRow & texrow,
2476 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2478 bool return_value = false;
2480 LyXLayout const & style =
2481 textclasslist.Style(bparams.textclass,
2483 LyXFont basefont, last_font;
2485 // Maybe we have to create a optional argument.
2486 size_type main_body;
2487 if (style.labeltype != LABEL_MANUAL)
2490 main_body = BeginningOfMainBody();
2492 if (main_body > 0) {
2494 basefont = getFont(bparams, -2); // Get label font
2496 basefont = getFont(bparams, -1); // Get layout font
2507 if (style.isCommand()) {
2510 } else if (align != LYX_ALIGN_LAYOUT) {
2513 return_value = true;
2517 moving_arg |= style.needprotect;
2519 // Which font is currently active?
2520 LyXFont running_font(basefont);
2521 // Do we have an open font change?
2522 bool open_font = false;
2524 texrow.start(this, 0);
2526 for (size_type i = 0; i < size(); ++i) {
2528 // First char in paragraph or after label?
2534 if (main_body > 0) {
2536 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2539 basefont = getFont(bparams, -1); // Now use the layout font
2540 running_font = basefont;
2544 if (style.isCommand()) {
2547 } else if (align != LYX_ALIGN_LAYOUT && next) {
2548 // We do not need \par here (Dekel)
2552 return_value = true;
2556 os << "\\noindent ";
2560 case LYX_ALIGN_NONE:
2561 case LYX_ALIGN_BLOCK:
2562 case LYX_ALIGN_LAYOUT:
2563 case LYX_ALIGN_SPECIAL:
2565 case LYX_ALIGN_LEFT:
2568 if (getParLanguage(bparams)->babel() != "hebrew") {
2569 os << "\\raggedright ";
2572 os << "\\raggedleft ";
2576 case LYX_ALIGN_RIGHT:
2579 if (getParLanguage(bparams)->babel() != "hebrew") {
2580 os << "\\raggedleft ";
2583 os << "\\raggedright ";
2587 case LYX_ALIGN_CENTER:
2590 os << "\\centering ";
2596 value_type c = GetChar(i);
2598 // Fully instantiated font
2599 LyXFont font = getFont(bparams, i);
2601 LyXParagraph * p = 0;
2604 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2605 (p = PreviousBeforeFootnote()) != 0)
2606 last_font = p->getFont(bparams, p->size() - 1);
2609 last_font = running_font;
2611 // Spaces at end of font change are simulated to be
2612 // outside font change, i.e. we write "\textXX{text} "
2613 // rather than "\textXX{text }". (Asger)
2614 if (open_font && c == ' ' && i <= size() - 2
2615 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2616 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2617 font = getFont(bparams, i + 1);
2619 // We end font definition before blanks
2620 if (!font.equalExceptLatex(running_font) && open_font) {
2621 column += running_font.latexWriteEndChanges(os,
2623 (i == main_body-1) ? basefont : font);
2624 running_font = basefont;
2628 // Blanks are printed before start of fontswitch
2630 // Do not print the separation of the optional argument
2631 if (i != main_body - 1) {
2632 SimpleTeXBlanks(os, texrow, i,
2633 column, font, style);
2637 // Do we need to change font?
2638 if (!font.equalExceptLatex(running_font)
2639 && i != main_body-1) {
2640 column += font.latexWriteStartChanges(os, basefont,
2642 running_font = font;
2646 if (c == LyXParagraph::META_NEWLINE) {
2647 // newlines are handled differently here than
2648 // the default in SimpleTeXSpecialChars().
2649 if (!style.newline_allowed
2650 || font.latex() == LyXFont::ON) {
2654 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2657 basefont = getFont(bparams, -1);
2658 running_font = basefont;
2659 if (font.family() ==
2660 LyXFont::TYPEWRITER_FAMILY) {
2668 texrow.start(this, i + 1);
2671 SimpleTeXSpecialChars(buf, bparams,
2672 os, texrow, moving_arg,
2673 font, running_font, basefont,
2674 open_font, style, i, column, c);
2678 // If we have an open font definition, we have to close it
2680 LyXParagraph * p = 0;
2683 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2684 && (p = NextAfterFootnote()) != 0
2689 running_font.latexWriteEndChanges(os, basefont,
2690 p->getFont(bparams, 0));
2692 running_font.latexWriteEndChanges(os, basefont, basefont);
2695 // Needed if there is an optional argument but no contents.
2696 if (main_body > 0 && main_body == size()) {
2698 return_value = false;
2701 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2702 return return_value;
2706 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2708 bool retval = false;
2710 case LyXParagraph::META_HFILL:
2711 sgml_string.erase();
2713 case LyXParagraph::META_NEWLINE:
2717 sgml_string = "&";
2720 sgml_string = "<";
2723 sgml_string = ">";
2726 sgml_string = "$";
2729 sgml_string = "#";
2732 sgml_string = "%";
2735 sgml_string = "[";
2738 sgml_string = "]";
2741 sgml_string = "{";
2744 sgml_string = "}";
2747 sgml_string = "˜";
2750 sgml_string = """;
2753 sgml_string = "\";
2759 case '\0': // Ignore :-)
2760 sgml_string.erase();
2770 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2771 LyXParagraph::size_type const i,
2772 int & column, LyXFont const & font,
2773 LyXLayout const & style)
2775 if (column > tex_code_break_column
2777 && GetChar(i - 1) != ' '
2779 // In LaTeX mode, we don't want to
2780 // break lines since some commands
2782 && ! (font.latex() == LyXFont::ON)
2783 // same in FreeSpacing mode
2784 && !style.free_spacing
2785 // In typewriter mode, we want to avoid
2786 // ! . ? : at the end of a line
2787 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2788 && (GetChar(i-1) == '.'
2789 || GetChar(i-1) == '?'
2790 || GetChar(i-1) == ':'
2791 || GetChar(i-1) == '!'))) {
2792 if (tex_code_break_column == 0) {
2793 // in batchmode we need LaTeX to still
2794 // see it as a space not as an extra '\n'
2800 texrow.start(this, i + 1);
2802 } else if (font.latex() == LyXFont::OFF) {
2803 if (style.free_spacing) {
2812 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2813 BufferParams const & bparams,
2814 ostream & os, TexRow & texrow,
2817 LyXFont & running_font,
2820 LyXLayout const & style,
2821 LyXParagraph::size_type & i,
2823 LyXParagraph::value_type const c)
2825 // Two major modes: LaTeX or plain
2826 // Handle here those cases common to both modes
2827 // and then split to handle the two modes separately.
2829 case LyXParagraph::META_INSET: {
2830 Inset * inset = GetInset(i);
2833 int const len = os.tellp();
2834 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2835 || inset->LyxCode() == Inset::MATH_CODE
2836 || inset->LyxCode() == Inset::URL_CODE)
2837 && running_font.isRightToLeft()) {
2842 int tmp = inset->Latex(buf, os, moving_arg,
2843 style.free_spacing);
2851 column += os.tellp() - len;
2860 case LyXParagraph::META_NEWLINE:
2862 column += running_font.latexWriteEndChanges(os,
2867 basefont = getFont(bparams, -1);
2868 running_font = basefont;
2871 case LyXParagraph::META_HFILL:
2877 // And now for the special cases within each mode
2878 // Are we in LaTeX mode?
2879 if (font.latex() == LyXFont::ON) {
2880 // at present we only have one option
2881 // but I'll leave it as a switch statement
2882 // so its simpler to extend. (ARRae)
2885 // make sure that we will not print
2886 // error generating chars to the tex
2887 // file. This test would not be needed
2888 // if it were done in the buffer
2896 // Plain mode (i.e. not LaTeX)
2899 os << "\\textbackslash{}";
2903 case '°': case '±': case '²': case '³':
2904 case '×': case '÷': case '¹': case 'ª':
2905 case 'º': case '¬': case 'µ':
2906 if (bparams.inputenc == "latin1" ||
2907 (bparams.inputenc == "auto" &&
2908 font.language()->encoding()->LatexName()
2910 os << "\\ensuremath{"
2919 case '|': case '<': case '>':
2920 // In T1 encoding, these characters exist
2921 if (lyxrc.fontenc == "T1") {
2923 //... but we should avoid ligatures
2924 if ((c == '>' || c == '<')
2926 && GetChar(i + 1) == c) {
2927 //os << "\\textcompwordmark{}";
2928 // Jean-Marc, have a look at
2929 // this. IÂ think this works
2937 // Typewriter font also has them
2938 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2942 // Otherwise, we use what LaTeX
2946 os << "\\textless{}";
2950 os << "\\textgreater{}";
2954 os << "\\textbar{}";
2960 case '-': // "--" in Typewriter mode -> "-{}-"
2962 && GetChar(i + 1) == '-'
2963 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
2972 os << "\\char`\\\"{}";
2977 if (bparams.inputenc == "default") {
2986 case '%': case '#': case '{':
2993 os << "\\textasciitilde{}";
2998 os << "\\textasciicircum{}";
3002 case '*': case '[': case ']':
3003 // avoid being mistaken for optional arguments
3004 os << '{' << c << '}';
3009 // Blanks are printed before font switching.
3010 // Sure? I am not! (try nice-latex)
3011 // I am sure it's correct. LyX might be smarter
3012 // in the future, but for now, nothing wrong is
3017 /* idea for labels --- begin*/
3021 && font.family() != LyXFont::TYPEWRITER_FAMILY
3022 && GetChar(i + 1) == 'y'
3023 && GetChar(i + 2) == 'X') {
3031 && font.family() != LyXFont::TYPEWRITER_FAMILY
3032 && GetChar(i + 1) == 'e'
3033 && GetChar(i + 2) == 'X') {
3038 // Check for "LaTeX2e"
3041 && font.family() != LyXFont::TYPEWRITER_FAMILY
3042 && GetChar(i + 1) == 'a'
3043 && GetChar(i + 2) == 'T'
3044 && GetChar(i + 3) == 'e'
3045 && GetChar(i + 4) == 'X'
3046 && GetChar(i + 5) == '2'
3047 && GetChar(i + 6) == 'e') {
3052 // Check for "LaTeX"
3055 && font.family() != LyXFont::TYPEWRITER_FAMILY
3056 && GetChar(i + 1) == 'a'
3057 && GetChar(i + 2) == 'T'
3058 && GetChar(i + 3) == 'e'
3059 && GetChar(i + 4) == 'X') {
3063 /* idea for labels --- end*/
3064 } else if (c != '\0') {
3074 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3075 BufferParams const & bparams,
3076 ostream & os, TexRow & texrow
3079 TexRow & foot_texrow,
3084 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3085 LyXParagraph * par = this;
3088 (par->depth == depth)
3090 && (par->footnoteflag == footnoteflag)
3095 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3097 if (textclasslist.Style(bparams.textclass,
3098 par->layout).isEnvironment()
3099 || par->pextra_type != PEXTRA_NONE) {
3100 par = par->TeXEnvironment(buf, bparams,
3108 par = par->TeXOnePar(buf, bparams,
3118 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3124 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3125 BufferParams const & bparams,
3126 ostream & os, TexRow & texrow
3129 TexRow & foot_texrow,
3134 bool eindent_open = false;
3136 bool foot_this_level = false;
3138 // flags when footnotetext should be appended to file.
3139 static bool minipage_open = false;
3140 static int minipage_open_depth = 0;
3141 char par_sep = bparams.paragraph_separation;
3143 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3146 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3149 LyXLayout const & style =
3150 textclasslist.Style(bparams.textclass,
3153 if (pextra_type == PEXTRA_INDENT) {
3154 if (!pextra_width.empty()) {
3155 os << "\\begin{LyXParagraphIndent}{"
3156 << pextra_width << "}\n";
3158 //float ib = atof(pextra_widthp.c_str())/100;
3159 // string can't handle floats at present (971109)
3160 // so I'll do a conversion by hand knowing that
3161 // the limits are 0.0 to 1.0. ARRae.
3162 os << "\\begin{LyXParagraphIndent}{";
3163 switch (pextra_widthp.length()) {
3175 os << "\\columnwidth}\n";
3178 eindent_open = true;
3180 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3181 if (pextra_hfill && Previous() &&
3182 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3183 os << "\\hfill{}\n";
3186 if (par_sep == BufferParams::PARSEP_INDENT) {
3187 os << "{\\setlength\\parindent{0pt}\n";
3190 os << "\\begin{minipage}";
3191 switch (pextra_alignment) {
3192 case MINIPAGE_ALIGN_TOP:
3195 case MINIPAGE_ALIGN_MIDDLE:
3198 case MINIPAGE_ALIGN_BOTTOM:
3202 if (!pextra_width.empty()) {
3203 os << '{' << pextra_width << "}\n";
3205 //float ib = atof(par->pextra_width.c_str())/100;
3206 // string can't handle floats at present
3207 // so I'll do a conversion by hand knowing that
3208 // the limits are 0.0 to 1.0. ARRae.
3210 switch (pextra_widthp.length()) {
3222 os << "\\columnwidth}\n";
3225 if (par_sep == BufferParams::PARSEP_INDENT) {
3226 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3229 minipage_open = true;
3230 minipage_open_depth = depth;
3233 #ifdef WITH_WARNINGS
3234 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3235 //I disabled it because it breaks when lists span on several
3238 if (style.isEnvironment()){
3239 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3240 #ifdef FANCY_FOOTNOTE_CODE
3241 if (foot_count < 0) {
3242 // flag that footnote[mark][text] should be
3243 // used for any footnotes from now on
3245 foot_this_level = true;
3248 os << "\\begin{" << style.latexname() << "}{"
3249 << labelwidthstring << "}\n";
3250 } else if (style.labeltype == LABEL_BIBLIO) {
3252 os << "\\begin{" << style.latexname() << "}{"
3253 << bibitemWidest(buf)
3255 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3256 #ifdef FANCY_FOOTNOTE_CODE
3257 if (foot_count < 0) {
3258 // flag that footnote[mark][text] should be
3259 // used for any footnotes from now on
3261 foot_this_level = true;
3264 os << "\\begin{" << style.latexname() << '}'
3265 << style.latexparam() << '\n';
3267 os << "\\begin{" << style.latexname() << '}'
3268 << style.latexparam() << '\n';
3271 LyXParagraph * par = this;
3273 par = par->TeXOnePar(buf, bparams,
3277 foot, foot_texrow, foot_count
3281 if (minipage_open && par && !style.isEnvironment() &&
3282 (par->pextra_type == PEXTRA_MINIPAGE) &&
3283 par->pextra_start_minipage) {
3284 os << "\\end{minipage}\n";
3286 if (par_sep == BufferParams::PARSEP_INDENT) {
3290 minipage_open = false;
3292 if (par && par->depth > depth) {
3293 if (textclasslist.Style(bparams.textclass,
3294 par->layout).isParagraph()
3296 // How to handle this? (Lgb)
3297 //&& !suffixIs(os, "\n\n")
3299 // There should be at least one '\n' already
3300 // but we need there to be two for Standard
3301 // paragraphs that are depth-increment'ed to be
3302 // output correctly. However, tables can
3303 // also be paragraphs so don't adjust them.
3306 // Will it ever harm to have one '\n' too
3307 // many? i.e. that we sometimes will have
3308 // three in a row. (Lgb)
3312 par = par->TeXDeeper(buf, bparams, os, texrow
3314 ,foot, foot_texrow, foot_count
3318 if (par && par->layout == layout && par->depth == depth &&
3319 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3320 if (par->pextra_hfill && par->Previous() &&
3321 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3322 os << "\\hfill{}\n";
3325 if (par_sep == BufferParams::PARSEP_INDENT) {
3326 os << "{\\setlength\\parindent{0pt}\n";
3329 os << "\\begin{minipage}";
3330 switch (par->pextra_alignment) {
3331 case MINIPAGE_ALIGN_TOP:
3334 case MINIPAGE_ALIGN_MIDDLE:
3337 case MINIPAGE_ALIGN_BOTTOM:
3341 if (!par->pextra_width.empty()) {
3342 os << '{' << par->pextra_width << "}\n";
3344 //float ib = atof(par->pextra_widthp.c_str())/100;
3345 // string can't handle floats at present
3346 // so I'll do a conversion by hand knowing that
3347 // the limits are 0.0 to 1.0. ARRae.
3349 switch (par->pextra_widthp.length()) {
3354 os << "0." << par->pextra_widthp;
3357 os << "0.0" << par->pextra_widthp;
3359 os << "\\columnwidth}\n";
3362 if (par_sep == BufferParams::PARSEP_INDENT) {
3363 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3366 minipage_open = true;
3367 minipage_open_depth = par->depth;
3370 && par->layout == layout
3371 && par->depth == depth
3372 && par->pextra_type == pextra_type
3374 && par->footnoteflag == footnoteflag
3378 if (style.isEnvironment()) {
3379 os << "\\end{" << style.latexname() << '}';
3381 // maybe this should go after the minipage closes?
3382 if (foot_this_level) {
3383 if (foot_count >= 1) {
3384 if (foot_count > 1) {
3385 os << "\\addtocounter{footnote}{-"
3390 texrow += foot_texrow;
3392 foot_texrow.reset();
3398 if (minipage_open && (minipage_open_depth == depth) &&
3399 (!par || par->pextra_start_minipage ||
3400 par->pextra_type != PEXTRA_MINIPAGE)) {
3401 os << "\\end{minipage}\n";
3403 if (par_sep == BufferParams::PARSEP_INDENT) {
3407 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3408 os << "\\medskip\n\n";
3412 minipage_open = false;
3415 os << "\\end{LyXParagraphIndent}\n";
3418 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3419 && par->pextra_hfill)) {
3423 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3424 return par; // ale970302
3429 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3430 BufferParams const & bparams,
3431 ostream & os, TexRow & texrow,
3432 ostream & foot, TexRow & foot_texrow,
3436 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3437 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3438 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3439 "No footnote!" << endl;
3441 LyXParagraph * par = this;
3442 LyXLayout const & style =
3443 textclasslist.Style(bparams.textclass,
3444 previous->GetLayout());
3446 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3447 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3448 "Float other than footnote in command"
3449 " with moving argument is illegal" << endl;
3452 if (footnotekind != LyXParagraph::FOOTNOTE
3453 && footnotekind != LyXParagraph::MARGIN
3456 // How to solve this?
3457 //&& !suffixIs(file, '\n')
3459 // we need to ensure that real floats like tables and figures
3460 // have their \begin{} on a new line otherwise we can get
3461 // incorrect results when using the endfloat.sty package
3462 // especially if two floats follow one another. ARRae 981022
3463 // NOTE: if the file is length 0 it must have just been
3464 // written out so we assume it ended with a '\n'
3466 // As far as I can see there is never any harm in writing
3467 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3472 bool moving_arg = false;
3473 bool need_closing = false;
3474 bool is_rtl = isRightToLeftPar(bparams);
3476 if (is_rtl != parent_is_rtl) {
3481 need_closing = true;
3484 bool footer_in_body = true;
3485 switch (footnotekind) {
3486 case LyXParagraph::FOOTNOTE:
3487 if (style.intitle) {
3488 os << "\\thanks{\n";
3489 footer_in_body = false;
3492 if (foot_count == -1) {
3493 // we're at depth 0 so we can use:
3494 os << "\\footnote{%\n";
3495 footer_in_body = false;
3497 os << "\\footnotemark{}%\n";
3499 // we only need this when there are
3500 // multiple footnotes
3501 os << "\\stepcounter{footnote}";
3503 os << "\\footnotetext{%\n";
3504 foot_texrow.start(this, 0);
3505 foot_texrow.newline();
3510 case LyXParagraph::MARGIN:
3511 os << "\\marginpar{\n";
3513 case LyXParagraph::FIG:
3514 if (pextra_type == PEXTRA_FLOATFLT
3515 && (!pextra_width.empty()
3516 || !pextra_widthp.empty())) {
3517 if (!pextra_width.empty())
3518 os << "\\begin{floatingfigure}{"
3519 << pextra_width << "}\n";
3521 os << "\\begin{floatingfigure}{"
3522 << lyx::atoi(pextra_widthp) / 100.0
3523 << "\\textwidth}\n";
3525 os << "\\begin{figure}";
3526 if (!bparams.float_placement.empty()) {
3527 os << '[' << bparams.float_placement << "]\n";
3533 case LyXParagraph::TAB:
3534 os << "\\begin{table}";
3535 if (!bparams.float_placement.empty()) {
3536 os << '[' << bparams.float_placement << "]\n";
3541 case LyXParagraph::WIDE_FIG:
3542 os << "\\begin{figure*}";
3543 if (!bparams.float_placement.empty()) {
3544 os << '[' << bparams.float_placement << "]\n";
3549 case LyXParagraph::WIDE_TAB:
3550 os << "\\begin{table*}";
3551 if (!bparams.float_placement.empty()) {
3552 os << '[' << bparams.float_placement << "]\n";
3557 case LyXParagraph::ALGORITHM:
3558 os << "\\begin{algorithm}\n";
3563 if (footnotekind != LyXParagraph::FOOTNOTE
3564 || !footer_in_body) {
3565 // Process text for all floats except footnotes in body
3567 LyXLayout const & style =
3569 .Style(bparams.textclass, par->layout);
3571 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3573 if (style.isEnvironment()
3574 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3575 // Allows the use of minipages within float
3576 // environments. Shouldn't be circular because
3577 // we don't support footnotes inside
3578 // floats (yet). ARRae
3579 par = par->TeXEnvironment(buf, bparams, os,
3584 par = par->TeXOnePar(buf, bparams,
3585 os, texrow, moving_arg,
3590 if (par && !par->IsDummy() && par->depth > depth) {
3591 par = par->TeXDeeper(buf, bparams, os, texrow,
3595 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3597 // process footnotes > depth 0 or in environments separately
3598 // NOTE: Currently don't support footnotes within footnotes
3599 // even though that is possible using the \footnotemark
3600 std::ostringstream dummy;
3601 TexRow dummy_texrow;
3602 int dummy_count = 0;
3604 LyXLayout const & style =
3606 .Style(bparams.textclass, par->layout);
3608 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3610 if (style.isEnvironment()
3611 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3612 // Allows the use of minipages within float
3613 // environments. Shouldn't be circular because
3614 // we don't support footnotes inside
3615 // floats (yet). ARRae
3616 par = par->TeXEnvironment(buf, bparams,
3618 dummy, dummy_texrow,
3621 par = par->TeXOnePar(buf, bparams,
3624 dummy, dummy_texrow,
3628 if (par && !par->IsDummy() && par->depth > depth) {
3629 par = par->TeXDeeper(buf, bparams,
3631 dummy, dummy_texrow,
3635 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3637 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3638 "Footnote in a Footnote -- not supported"
3643 switch (footnotekind) {
3644 case LyXParagraph::FOOTNOTE:
3645 if (footer_in_body) {
3646 // This helps tell which of the multiple
3647 // footnotetexts an error was in.
3649 foot_texrow.newline();
3654 case LyXParagraph::MARGIN:
3657 case LyXParagraph::FIG:
3658 if (pextra_type == PEXTRA_FLOATFLT
3659 && (!pextra_width.empty()
3660 || !pextra_widthp.empty()))
3661 os << "\\end{floatingfigure}";
3663 os << "\\end{figure}";
3665 case LyXParagraph::TAB:
3666 os << "\\end{table}";
3668 case LyXParagraph::WIDE_FIG:
3669 os << "\\end{figure*}";
3671 case LyXParagraph::WIDE_TAB:
3672 os << "\\end{table*}";
3674 case LyXParagraph::ALGORITHM:
3675 os << "\\end{algorithm}";
3682 if (footnotekind != LyXParagraph::FOOTNOTE
3683 && footnotekind != LyXParagraph::MARGIN) {
3684 // we need to ensure that real floats like tables and figures
3685 // have their \end{} on a line of their own otherwise we can
3686 // get incorrect results when using the endfloat.sty package.
3691 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3696 bool LyXParagraph::IsDummy() const
3698 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3699 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3703 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3704 int type, string const & width,
3705 string const & widthp)
3708 pextra_width = width;
3709 pextra_widthp = widthp;
3711 if (textclasslist.Style(bparams.textclass,
3712 layout).isEnvironment()) {
3713 LyXParagraph * par = this;
3714 LyXParagraph * ppar = par;
3716 while (par && (par->layout == layout)
3717 && (par->depth == depth)) {
3719 par = par->Previous();
3722 par = par->FirstPhysicalPar();
3724 while (par && par->depth > depth) {
3725 par = par->Previous();
3728 par = par->FirstPhysicalPar();
3733 while (par && (par->layout == layout)
3734 && (par->depth == depth)) {
3735 par->pextra_type = type;
3736 par->pextra_width = width;
3737 par->pextra_widthp = widthp;
3739 par = par->NextAfterFootnote();
3743 if (par && (par->depth > depth))
3744 par->SetPExtraType(bparams,
3745 type, width, widthp);
3747 while (par && ((par->depth > depth) || par->IsDummy()))
3748 par = par->NextAfterFootnote();
3750 while (par && ((par->depth > depth)))
3758 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3760 if (pextra_type == PEXTRA_NONE)
3763 pextra_type = PEXTRA_NONE;
3764 pextra_width.erase();
3765 pextra_widthp.erase();
3767 if (textclasslist.Style(bparams.textclass,
3768 layout).isEnvironment()) {
3769 LyXParagraph * par = this;
3770 LyXParagraph * ppar = par;
3772 while (par && (par->layout == layout)
3773 && (par->depth == depth)) {
3775 par = par->Previous();
3778 par = par->FirstPhysicalPar();
3780 while (par && par->depth > depth) {
3781 par = par->Previous();
3784 par = par->FirstPhysicalPar();
3789 while (par && (par->layout == layout)
3790 && (par->depth == depth)) {
3791 par->pextra_type = PEXTRA_NONE;
3792 par->pextra_width.erase();
3793 par->pextra_widthp.erase();
3795 par = par->NextAfterFootnote();
3799 if (par && (par->depth > depth))
3800 par->UnsetPExtraType(bparams);
3802 while (par && ((par->depth > depth) || par->IsDummy()))
3803 par = par->NextAfterFootnote();
3805 while (par && ((par->depth > depth)))
3813 bool LyXParagraph::IsHfill(size_type pos) const
3815 return IsHfillChar(GetChar(pos));
3819 bool LyXParagraph::IsInset(size_type pos) const
3821 return IsInsetChar(GetChar(pos));
3826 bool LyXParagraph::IsFloat(size_type pos) const
3828 return IsFloatChar(GetChar(pos));
3833 bool LyXParagraph::IsNewline(size_type pos) const
3835 return pos >= 0 && IsNewlineChar(GetChar(pos));
3839 bool LyXParagraph::IsSeparator(size_type pos) const
3841 return IsSeparatorChar(GetChar(pos));
3845 bool LyXParagraph::IsLineSeparator(size_type pos) const
3847 return IsLineSeparatorChar(GetChar(pos));
3851 bool LyXParagraph::IsKomma(size_type pos) const
3853 return IsKommaChar(GetChar(pos));
3857 /// Used by the spellchecker
3858 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3860 value_type const c = GetChar(pos);
3861 if (IsLetterChar(c))
3863 // '\0' is not a letter, allthough every string contains "" (below)
3866 // We want to pass the ' and escape chars to ispell
3867 string const extra = lyxrc.isp_esc_chars + '\'';
3868 char ch[2] = { c, 0 };
3869 return contains(extra, ch);
3873 bool LyXParagraph::IsWord(size_type pos ) const
3875 return IsWordChar(GetChar(pos)) ;
3880 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3884 return FirstPhysicalPar()->getParLanguage(bparams);
3888 #ifdef DO_USE_DEFAULT_LANGUAGE
3889 Language const * lang = GetFirstFontSettings().language();
3891 if (lang->lang() == "default")
3892 return bparams.language;
3895 return GetFirstFontSettings().language();
3897 } else if (previous)
3898 return previous->getParLanguage(bparams);
3900 return bparams.language;
3904 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3906 return lyxrc.rtl_support
3907 && getParLanguage(bparams)->RightToLeft();
3911 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3912 Language const * from, Language const * to)
3914 for (size_type i = 0; i < size(); ++i) {
3915 LyXFont font = GetFontSettings(bparams, i);
3916 if (font.language() == from) {
3917 font.setLanguage(to);
3924 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3926 Language const * doc_language = bparams.language;
3927 for (FontList::const_iterator cit = fontlist.begin();
3928 cit != fontlist.end(); ++cit)
3929 if ((*cit).font.language() != doc_language)
3935 // Convert the paragraph to a string.
3936 // Used for building the table of contents
3937 string const LyXParagraph::String(Buffer const * buffer, bool label)
3939 BufferParams const & bparams = buffer->params;
3942 if (label && !IsDummy() && !labelstring.empty())
3944 if (label && !labelstring.empty())
3946 s += labelstring + ' ';
3947 string::size_type const len = s.size();
3949 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3950 value_type c = GetChar(i);
3953 else if (c == META_INSET &&
3954 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3955 std::ostringstream ost;
3956 GetInset(i)->Ascii(buffer, ost);
3957 s += subst(ost.str().c_str(),'\n',' ');
3962 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
3963 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
3964 s += NextAfterFootnote()->String(buffer, false);
3968 if (isRightToLeftPar(bparams))
3969 reverse(s.begin() + len,s.end());
3977 string const LyXParagraph::String(Buffer const * buffer,
3978 LyXParagraph::size_type beg,
3979 LyXParagraph::size_type end)
3984 if (beg == 0 && !IsDummy() && !labelstring.empty())
3986 if (beg == 0 && !labelstring.empty())
3988 s += labelstring + ' ';
3990 for (LyXParagraph::size_type i = beg; i < end; ++i) {
3991 value_type c = GetChar(i);
3994 else if (c == META_INSET) {
3995 std::ostringstream ost;
3996 GetInset(i)->Ascii(buffer, ost);
3997 s += ost.str().c_str();
4005 void LyXParagraph::SetInsetOwner(Inset * i)
4008 for (InsetList::const_iterator cit = insetlist.begin();
4009 cit != insetlist.end(); ++cit) {
4011 (*cit).inset->setOwner(i);
4016 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4019 for (InsetList::const_iterator cit = insetlist.begin();
4020 cit != insetlist.end(); ++cit) {
4022 if ((*cit).inset->IsTextInset()) {
4023 static_cast<UpdatableInset *>
4024 ((*cit).inset)->deleteLyXText(bv);
4031 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4034 for (InsetList::const_iterator cit = insetlist.begin();
4035 cit != insetlist.end(); ++cit) {
4037 if ((*cit).inset->IsTextInset()) {
4038 static_cast<UpdatableInset *>
4039 ((*cit).inset)->resizeLyXText(bv);