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) {
2208 if (inset == bibkey)
2212 // Think about footnotes.
2213 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2214 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2216 NextAfterFootnote()->GetPositionOfInset(inset);
2218 return text.size() + 1 + further;
2225 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2226 BufferParams const & bparams,
2227 ostream & os, TexRow & texrow,
2232 TexRow & foot_texrow,
2237 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2238 LyXLayout const & style =
2239 textclasslist.Style(bparams.textclass,
2242 bool further_blank_line = false;
2245 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2248 if (start_of_appendix) {
2249 os << "\\appendix\n";
2253 if (!spacing.isDefault()
2254 && (!Previous() || !Previous()->HasSameLayout(this))) {
2255 os << spacing.writeEnvirBegin() << "\n";
2259 if (tex_code_break_column && style.isCommand()){
2264 if (pagebreak_top) {
2266 further_blank_line = true;
2268 if (added_space_top.kind() != VSpace::NONE) {
2269 os << added_space_top.asLatexCommand(bparams);
2270 further_blank_line = true;
2274 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2275 << "\\vspace{-1\\parskip}";
2276 further_blank_line = true;
2279 if (further_blank_line){
2284 Language const * language = getParLanguage(bparams);
2285 Language const * doc_language = bparams.language;
2286 Language const * previous_language = previous
2287 ? previous->getParLanguage(bparams) : doc_language;
2288 if (language->babel() != doc_language->babel() &&
2289 language->babel() != previous_language->babel()) {
2290 os << subst(lyxrc.language_command_begin, "$$lang",
2296 if (bparams.inputenc == "auto" &&
2297 language->encoding() != previous_language->encoding()) {
2298 os << "\\inputencoding{"
2299 << language->encoding()->LatexName()
2304 switch (style.latextype) {
2307 << style.latexname()
2308 << style.latexparam();
2310 case LATEX_ITEM_ENVIRONMENT:
2312 bibkey->Latex(buf, os, false, false);
2316 case LATEX_LIST_ENVIRONMENT:
2323 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2325 LyXParagraph * par = next;
2327 // Spit out footnotes
2328 if (lyxrc.rtl_support) {
2329 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2330 && next->footnoteflag != footnoteflag) {
2331 LyXParagraph * p = 0;
2332 bool is_rtl = (size() > 0)
2333 ? GetFontSettings(bparams,
2334 size()-1).isRightToLeft()
2335 : language->RightToLeft();
2336 if ((p = NextAfterFootnote()) != 0 &&
2338 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2339 is_rtl = getParLanguage(bparams)->RightToLeft();
2340 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2341 && par->footnoteflag != footnoteflag) {
2342 par = par->TeXFootnote(buf, bparams,
2344 foot_texrow, foot_count,
2346 par->SimpleTeXOnePar(buf, bparams,
2347 os, texrow, moving_arg);
2348 is_rtl = (par->size() > 0)
2349 ? par->GetFontSettings(bparams,
2350 par->size()-1).isRightToLeft()
2351 : language->RightToLeft();
2353 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2354 (p = par->NextAfterFootnote()) != 0 &&
2356 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2357 is_rtl = language->RightToLeft();
2362 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2363 && par->footnoteflag != footnoteflag) {
2364 par = par->TeXFootnote(buf, bparams,
2366 foot, foot_texrow, foot_count,
2368 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2374 // Make sure that \\par is done with the font of the last
2375 // character if this has another size as the default.
2376 // This is necessary because LaTeX (and LyX on the screen)
2377 // calculates the space between the baselines according
2378 // to this font. (Matthias)
2380 // Is this really needed ? (Dekel)
2381 // We do not need to use to change the font for the last paragraph
2382 // or for a command.
2383 LyXFont font = getFont(bparams, Last() - 1);
2384 bool is_command = textclasslist.Style(bparams.textclass,
2385 GetLayout()).isCommand();
2386 if (style.resfont.size() != font.size() && next && !is_command) {
2389 os << "\\" << font.latexSize() << " \\par}";
2390 } else if (need_par) {
2392 } else if (is_command)
2395 if (language->babel() != doc_language->babel() &&
2398 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2400 || par->getParLanguage(bparams)->babel() != language->babel())) {
2402 << subst(lyxrc.language_command_end, "$$lang",
2403 doc_language->babel());
2406 switch (style.latextype) {
2407 case LATEX_ITEM_ENVIRONMENT:
2408 case LATEX_LIST_ENVIRONMENT:
2409 if (par && (depth < par->depth)) {
2414 case LATEX_ENVIRONMENT:
2415 // if its the last paragraph of the current environment
2416 // skip it otherwise fall through
2418 && (par->layout != layout
2419 || par->depth != depth
2420 || par->pextra_type != pextra_type))
2423 // we don't need it for the last paragraph!!!
2424 // or for tables in floats
2425 // -- effectively creates a \par where there isn't one which
2426 // breaks a \subfigure or \subtable.
2428 // && footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2434 further_blank_line = false;
2436 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2437 further_blank_line = true;
2440 if (added_space_bottom.kind() != VSpace::NONE) {
2441 os << added_space_bottom.asLatexCommand(bparams);
2442 further_blank_line = true;
2445 if (pagebreak_bottom) {
2447 further_blank_line = true;
2450 if (further_blank_line){
2455 if (!spacing.isDefault()
2456 && (!par || !par->HasSameLayout(this))) {
2457 os << spacing.writeEnvirEnd() << "\n";
2461 // we don't need it for the last paragraph!!!
2464 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2465 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2472 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2477 // This one spits out the text of the paragraph
2478 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2479 BufferParams const & bparams,
2480 ostream & os, TexRow & texrow,
2483 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2485 bool return_value = false;
2487 LyXLayout const & style =
2488 textclasslist.Style(bparams.textclass,
2490 LyXFont basefont, last_font;
2492 // Maybe we have to create a optional argument.
2493 size_type main_body;
2494 if (style.labeltype != LABEL_MANUAL)
2497 main_body = BeginningOfMainBody();
2499 if (main_body > 0) {
2501 basefont = getFont(bparams, -2); // Get label font
2503 basefont = getFont(bparams, -1); // Get layout font
2514 if (style.isCommand()) {
2517 } else if (align != LYX_ALIGN_LAYOUT) {
2520 return_value = true;
2524 moving_arg |= style.needprotect;
2526 // Which font is currently active?
2527 LyXFont running_font(basefont);
2528 // Do we have an open font change?
2529 bool open_font = false;
2531 texrow.start(this, 0);
2533 for (size_type i = 0; i < size(); ++i) {
2535 // First char in paragraph or after label?
2541 if (main_body > 0) {
2543 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2546 basefont = getFont(bparams, -1); // Now use the layout font
2547 running_font = basefont;
2551 if (style.isCommand()) {
2554 } else if (align != LYX_ALIGN_LAYOUT && next) {
2555 // We do not need \par here (Dekel)
2559 return_value = true;
2563 os << "\\noindent ";
2567 case LYX_ALIGN_NONE:
2568 case LYX_ALIGN_BLOCK:
2569 case LYX_ALIGN_LAYOUT:
2570 case LYX_ALIGN_SPECIAL:
2572 case LYX_ALIGN_LEFT:
2575 if (getParLanguage(bparams)->babel() != "hebrew") {
2576 os << "\\raggedright ";
2579 os << "\\raggedleft ";
2583 case LYX_ALIGN_RIGHT:
2586 if (getParLanguage(bparams)->babel() != "hebrew") {
2587 os << "\\raggedleft ";
2590 os << "\\raggedright ";
2594 case LYX_ALIGN_CENTER:
2597 os << "\\centering ";
2603 value_type c = GetChar(i);
2605 // Fully instantiated font
2606 LyXFont font = getFont(bparams, i);
2608 LyXParagraph * p = 0;
2611 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2612 (p = PreviousBeforeFootnote()) != 0)
2613 last_font = p->getFont(bparams, p->size() - 1);
2616 last_font = running_font;
2618 // Spaces at end of font change are simulated to be
2619 // outside font change, i.e. we write "\textXX{text} "
2620 // rather than "\textXX{text }". (Asger)
2621 if (open_font && c == ' ' && i <= size() - 2
2622 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2623 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2624 font = getFont(bparams, i + 1);
2626 // We end font definition before blanks
2627 if (!font.equalExceptLatex(running_font) && open_font) {
2628 column += running_font.latexWriteEndChanges(os,
2630 (i == main_body-1) ? basefont : font);
2631 running_font = basefont;
2635 // Blanks are printed before start of fontswitch
2637 // Do not print the separation of the optional argument
2638 if (i != main_body - 1) {
2639 SimpleTeXBlanks(os, texrow, i,
2640 column, font, style);
2644 // Do we need to change font?
2645 if (!font.equalExceptLatex(running_font)
2646 && i != main_body-1) {
2647 column += font.latexWriteStartChanges(os, basefont,
2649 running_font = font;
2653 if (c == LyXParagraph::META_NEWLINE) {
2654 // newlines are handled differently here than
2655 // the default in SimpleTeXSpecialChars().
2656 if (!style.newline_allowed
2657 || font.latex() == LyXFont::ON) {
2661 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2664 basefont = getFont(bparams, -1);
2665 running_font = basefont;
2666 if (font.family() ==
2667 LyXFont::TYPEWRITER_FAMILY) {
2675 texrow.start(this, i + 1);
2678 SimpleTeXSpecialChars(buf, bparams,
2679 os, texrow, moving_arg,
2680 font, running_font, basefont,
2681 open_font, style, i, column, c);
2685 // If we have an open font definition, we have to close it
2687 LyXParagraph * p = 0;
2690 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2691 && (p = NextAfterFootnote()) != 0
2696 running_font.latexWriteEndChanges(os, basefont,
2697 p->getFont(bparams, 0));
2699 running_font.latexWriteEndChanges(os, basefont, basefont);
2702 // Needed if there is an optional argument but no contents.
2703 if (main_body > 0 && main_body == size()) {
2705 return_value = false;
2708 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2709 return return_value;
2713 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2715 bool retval = false;
2717 case LyXParagraph::META_HFILL:
2718 sgml_string.erase();
2720 case LyXParagraph::META_NEWLINE:
2724 sgml_string = "&";
2727 sgml_string = "<";
2730 sgml_string = ">";
2733 sgml_string = "$";
2736 sgml_string = "#";
2739 sgml_string = "%";
2742 sgml_string = "[";
2745 sgml_string = "]";
2748 sgml_string = "{";
2751 sgml_string = "}";
2754 sgml_string = "˜";
2757 sgml_string = """;
2760 sgml_string = "\";
2766 case '\0': // Ignore :-)
2767 sgml_string.erase();
2777 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2778 LyXParagraph::size_type const i,
2779 int & column, LyXFont const & font,
2780 LyXLayout const & style)
2782 if (column > tex_code_break_column
2784 && GetChar(i - 1) != ' '
2786 // In LaTeX mode, we don't want to
2787 // break lines since some commands
2789 && ! (font.latex() == LyXFont::ON)
2790 // same in FreeSpacing mode
2791 && !style.free_spacing
2792 // In typewriter mode, we want to avoid
2793 // ! . ? : at the end of a line
2794 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2795 && (GetChar(i-1) == '.'
2796 || GetChar(i-1) == '?'
2797 || GetChar(i-1) == ':'
2798 || GetChar(i-1) == '!'))) {
2799 if (tex_code_break_column == 0) {
2800 // in batchmode we need LaTeX to still
2801 // see it as a space not as an extra '\n'
2807 texrow.start(this, i + 1);
2809 } else if (font.latex() == LyXFont::OFF) {
2810 if (style.free_spacing) {
2819 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2820 BufferParams const & bparams,
2821 ostream & os, TexRow & texrow,
2824 LyXFont & running_font,
2827 LyXLayout const & style,
2828 LyXParagraph::size_type & i,
2830 LyXParagraph::value_type const c)
2832 // Two major modes: LaTeX or plain
2833 // Handle here those cases common to both modes
2834 // and then split to handle the two modes separately.
2836 case LyXParagraph::META_INSET: {
2837 Inset * inset = GetInset(i);
2840 int const len = os.tellp();
2841 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2842 || inset->LyxCode() == Inset::MATH_CODE
2843 || inset->LyxCode() == Inset::URL_CODE)
2844 && running_font.isRightToLeft()) {
2849 int tmp = inset->Latex(buf, os, moving_arg,
2850 style.free_spacing);
2858 column += os.tellp() - len;
2867 case LyXParagraph::META_NEWLINE:
2869 column += running_font.latexWriteEndChanges(os,
2874 basefont = getFont(bparams, -1);
2875 running_font = basefont;
2878 case LyXParagraph::META_HFILL:
2884 // And now for the special cases within each mode
2885 // Are we in LaTeX mode?
2886 if (font.latex() == LyXFont::ON) {
2887 // at present we only have one option
2888 // but I'll leave it as a switch statement
2889 // so its simpler to extend. (ARRae)
2892 // make sure that we will not print
2893 // error generating chars to the tex
2894 // file. This test would not be needed
2895 // if it were done in the buffer
2903 // Plain mode (i.e. not LaTeX)
2906 os << "\\textbackslash{}";
2910 case '°': case '±': case '²': case '³':
2911 case '×': case '÷': case '¹': case 'ª':
2912 case 'º': case '¬': case 'µ':
2913 if (bparams.inputenc == "latin1" ||
2914 (bparams.inputenc == "auto" &&
2915 font.language()->encoding()->LatexName()
2917 os << "\\ensuremath{"
2926 case '|': case '<': case '>':
2927 // In T1 encoding, these characters exist
2928 if (lyxrc.fontenc == "T1") {
2930 //... but we should avoid ligatures
2931 if ((c == '>' || c == '<')
2933 && GetChar(i + 1) == c) {
2934 //os << "\\textcompwordmark{}";
2935 // Jean-Marc, have a look at
2936 // this. IÂ think this works
2944 // Typewriter font also has them
2945 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2949 // Otherwise, we use what LaTeX
2953 os << "\\textless{}";
2957 os << "\\textgreater{}";
2961 os << "\\textbar{}";
2967 case '-': // "--" in Typewriter mode -> "-{}-"
2969 && GetChar(i + 1) == '-'
2970 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
2979 os << "\\char`\\\"{}";
2984 if (bparams.inputenc == "default") {
2993 case '%': case '#': case '{':
3000 os << "\\textasciitilde{}";
3005 os << "\\textasciicircum{}";
3009 case '*': case '[': case ']':
3010 // avoid being mistaken for optional arguments
3011 os << '{' << c << '}';
3016 // Blanks are printed before font switching.
3017 // Sure? I am not! (try nice-latex)
3018 // I am sure it's correct. LyX might be smarter
3019 // in the future, but for now, nothing wrong is
3024 /* idea for labels --- begin*/
3028 && font.family() != LyXFont::TYPEWRITER_FAMILY
3029 && GetChar(i + 1) == 'y'
3030 && GetChar(i + 2) == 'X') {
3038 && font.family() != LyXFont::TYPEWRITER_FAMILY
3039 && GetChar(i + 1) == 'e'
3040 && GetChar(i + 2) == 'X') {
3045 // Check for "LaTeX2e"
3048 && font.family() != LyXFont::TYPEWRITER_FAMILY
3049 && GetChar(i + 1) == 'a'
3050 && GetChar(i + 2) == 'T'
3051 && GetChar(i + 3) == 'e'
3052 && GetChar(i + 4) == 'X'
3053 && GetChar(i + 5) == '2'
3054 && GetChar(i + 6) == 'e') {
3059 // Check for "LaTeX"
3062 && font.family() != LyXFont::TYPEWRITER_FAMILY
3063 && GetChar(i + 1) == 'a'
3064 && GetChar(i + 2) == 'T'
3065 && GetChar(i + 3) == 'e'
3066 && GetChar(i + 4) == 'X') {
3070 /* idea for labels --- end*/
3071 } else if (c != '\0') {
3081 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3082 BufferParams const & bparams,
3083 ostream & os, TexRow & texrow
3086 TexRow & foot_texrow,
3091 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3092 LyXParagraph * par = this;
3095 (par->depth == depth)
3097 && (par->footnoteflag == footnoteflag)
3102 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3104 if (textclasslist.Style(bparams.textclass,
3105 par->layout).isEnvironment()
3106 || par->pextra_type != PEXTRA_NONE) {
3107 par = par->TeXEnvironment(buf, bparams,
3115 par = par->TeXOnePar(buf, bparams,
3125 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3131 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3132 BufferParams const & bparams,
3133 ostream & os, TexRow & texrow
3136 TexRow & foot_texrow,
3141 bool eindent_open = false;
3143 bool foot_this_level = false;
3145 // flags when footnotetext should be appended to file.
3146 static bool minipage_open = false;
3147 static int minipage_open_depth = 0;
3148 char par_sep = bparams.paragraph_separation;
3150 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3153 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3156 LyXLayout const & style =
3157 textclasslist.Style(bparams.textclass,
3160 if (pextra_type == PEXTRA_INDENT) {
3161 if (!pextra_width.empty()) {
3162 os << "\\begin{LyXParagraphIndent}{"
3163 << pextra_width << "}\n";
3165 //float ib = atof(pextra_widthp.c_str())/100;
3166 // string can't handle floats at present (971109)
3167 // so I'll do a conversion by hand knowing that
3168 // the limits are 0.0 to 1.0. ARRae.
3169 os << "\\begin{LyXParagraphIndent}{";
3170 switch (pextra_widthp.length()) {
3182 os << "\\columnwidth}\n";
3185 eindent_open = true;
3187 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3188 if (pextra_hfill && Previous() &&
3189 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3190 os << "\\hfill{}\n";
3193 if (par_sep == BufferParams::PARSEP_INDENT) {
3194 os << "{\\setlength\\parindent{0pt}\n";
3197 os << "\\begin{minipage}";
3198 switch (pextra_alignment) {
3199 case MINIPAGE_ALIGN_TOP:
3202 case MINIPAGE_ALIGN_MIDDLE:
3205 case MINIPAGE_ALIGN_BOTTOM:
3209 if (!pextra_width.empty()) {
3210 os << '{' << pextra_width << "}\n";
3212 //float ib = atof(par->pextra_width.c_str())/100;
3213 // string can't handle floats at present
3214 // so I'll do a conversion by hand knowing that
3215 // the limits are 0.0 to 1.0. ARRae.
3217 switch (pextra_widthp.length()) {
3229 os << "\\columnwidth}\n";
3232 if (par_sep == BufferParams::PARSEP_INDENT) {
3233 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3236 minipage_open = true;
3237 minipage_open_depth = depth;
3240 #ifdef WITH_WARNINGS
3241 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3242 //I disabled it because it breaks when lists span on several
3245 if (style.isEnvironment()){
3246 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3247 #ifdef FANCY_FOOTNOTE_CODE
3248 if (foot_count < 0) {
3249 // flag that footnote[mark][text] should be
3250 // used for any footnotes from now on
3252 foot_this_level = true;
3255 os << "\\begin{" << style.latexname() << "}{"
3256 << labelwidthstring << "}\n";
3257 } else if (style.labeltype == LABEL_BIBLIO) {
3259 os << "\\begin{" << style.latexname() << "}{"
3260 << bibitemWidest(buf)
3262 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3263 #ifdef FANCY_FOOTNOTE_CODE
3264 if (foot_count < 0) {
3265 // flag that footnote[mark][text] should be
3266 // used for any footnotes from now on
3268 foot_this_level = true;
3271 os << "\\begin{" << style.latexname() << '}'
3272 << style.latexparam() << '\n';
3274 os << "\\begin{" << style.latexname() << '}'
3275 << style.latexparam() << '\n';
3278 LyXParagraph * par = this;
3280 par = par->TeXOnePar(buf, bparams,
3284 foot, foot_texrow, foot_count
3288 if (minipage_open && par && !style.isEnvironment() &&
3289 (par->pextra_type == PEXTRA_MINIPAGE) &&
3290 par->pextra_start_minipage) {
3291 os << "\\end{minipage}\n";
3293 if (par_sep == BufferParams::PARSEP_INDENT) {
3297 minipage_open = false;
3299 if (par && par->depth > depth) {
3300 if (textclasslist.Style(bparams.textclass,
3301 par->layout).isParagraph()
3303 // How to handle this? (Lgb)
3304 //&& !suffixIs(os, "\n\n")
3306 // There should be at least one '\n' already
3307 // but we need there to be two for Standard
3308 // paragraphs that are depth-increment'ed to be
3309 // output correctly. However, tables can
3310 // also be paragraphs so don't adjust them.
3313 // Will it ever harm to have one '\n' too
3314 // many? i.e. that we sometimes will have
3315 // three in a row. (Lgb)
3319 par = par->TeXDeeper(buf, bparams, os, texrow
3321 ,foot, foot_texrow, foot_count
3325 if (par && par->layout == layout && par->depth == depth &&
3326 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3327 if (par->pextra_hfill && par->Previous() &&
3328 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3329 os << "\\hfill{}\n";
3332 if (par_sep == BufferParams::PARSEP_INDENT) {
3333 os << "{\\setlength\\parindent{0pt}\n";
3336 os << "\\begin{minipage}";
3337 switch (par->pextra_alignment) {
3338 case MINIPAGE_ALIGN_TOP:
3341 case MINIPAGE_ALIGN_MIDDLE:
3344 case MINIPAGE_ALIGN_BOTTOM:
3348 if (!par->pextra_width.empty()) {
3349 os << '{' << par->pextra_width << "}\n";
3351 //float ib = atof(par->pextra_widthp.c_str())/100;
3352 // string can't handle floats at present
3353 // so I'll do a conversion by hand knowing that
3354 // the limits are 0.0 to 1.0. ARRae.
3356 switch (par->pextra_widthp.length()) {
3361 os << "0." << par->pextra_widthp;
3364 os << "0.0" << par->pextra_widthp;
3366 os << "\\columnwidth}\n";
3369 if (par_sep == BufferParams::PARSEP_INDENT) {
3370 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3373 minipage_open = true;
3374 minipage_open_depth = par->depth;
3377 && par->layout == layout
3378 && par->depth == depth
3379 && par->pextra_type == pextra_type
3381 && par->footnoteflag == footnoteflag
3385 if (style.isEnvironment()) {
3386 os << "\\end{" << style.latexname() << '}';
3388 // maybe this should go after the minipage closes?
3389 if (foot_this_level) {
3390 if (foot_count >= 1) {
3391 if (foot_count > 1) {
3392 os << "\\addtocounter{footnote}{-"
3397 texrow += foot_texrow;
3399 foot_texrow.reset();
3405 if (minipage_open && (minipage_open_depth == depth) &&
3406 (!par || par->pextra_start_minipage ||
3407 par->pextra_type != PEXTRA_MINIPAGE)) {
3408 os << "\\end{minipage}\n";
3410 if (par_sep == BufferParams::PARSEP_INDENT) {
3414 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3415 os << "\\medskip\n\n";
3419 minipage_open = false;
3422 os << "\\end{LyXParagraphIndent}\n";
3425 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3426 && par->pextra_hfill)) {
3430 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3431 return par; // ale970302
3436 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3437 BufferParams const & bparams,
3438 ostream & os, TexRow & texrow,
3439 ostream & foot, TexRow & foot_texrow,
3443 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3444 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3445 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3446 "No footnote!" << endl;
3448 LyXParagraph * par = this;
3449 LyXLayout const & style =
3450 textclasslist.Style(bparams.textclass,
3451 previous->GetLayout());
3453 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3454 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3455 "Float other than footnote in command"
3456 " with moving argument is illegal" << endl;
3459 if (footnotekind != LyXParagraph::FOOTNOTE
3460 && footnotekind != LyXParagraph::MARGIN
3463 // How to solve this?
3464 //&& !suffixIs(file, '\n')
3466 // we need to ensure that real floats like tables and figures
3467 // have their \begin{} on a new line otherwise we can get
3468 // incorrect results when using the endfloat.sty package
3469 // especially if two floats follow one another. ARRae 981022
3470 // NOTE: if the file is length 0 it must have just been
3471 // written out so we assume it ended with a '\n'
3473 // As far as I can see there is never any harm in writing
3474 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3479 bool moving_arg = false;
3480 bool need_closing = false;
3481 bool is_rtl = isRightToLeftPar(bparams);
3483 if (is_rtl != parent_is_rtl) {
3488 need_closing = true;
3491 bool footer_in_body = true;
3492 switch (footnotekind) {
3493 case LyXParagraph::FOOTNOTE:
3494 if (style.intitle) {
3495 os << "\\thanks{\n";
3496 footer_in_body = false;
3499 if (foot_count == -1) {
3500 // we're at depth 0 so we can use:
3501 os << "\\footnote{%\n";
3502 footer_in_body = false;
3504 os << "\\footnotemark{}%\n";
3506 // we only need this when there are
3507 // multiple footnotes
3508 os << "\\stepcounter{footnote}";
3510 os << "\\footnotetext{%\n";
3511 foot_texrow.start(this, 0);
3512 foot_texrow.newline();
3517 case LyXParagraph::MARGIN:
3518 os << "\\marginpar{\n";
3520 case LyXParagraph::FIG:
3521 if (pextra_type == PEXTRA_FLOATFLT
3522 && (!pextra_width.empty()
3523 || !pextra_widthp.empty())) {
3524 if (!pextra_width.empty())
3525 os << "\\begin{floatingfigure}{"
3526 << pextra_width << "}\n";
3528 os << "\\begin{floatingfigure}{"
3529 << lyx::atoi(pextra_widthp) / 100.0
3530 << "\\textwidth}\n";
3532 os << "\\begin{figure}";
3533 if (!bparams.float_placement.empty()) {
3534 os << '[' << bparams.float_placement << "]\n";
3540 case LyXParagraph::TAB:
3541 os << "\\begin{table}";
3542 if (!bparams.float_placement.empty()) {
3543 os << '[' << bparams.float_placement << "]\n";
3548 case LyXParagraph::WIDE_FIG:
3549 os << "\\begin{figure*}";
3550 if (!bparams.float_placement.empty()) {
3551 os << '[' << bparams.float_placement << "]\n";
3556 case LyXParagraph::WIDE_TAB:
3557 os << "\\begin{table*}";
3558 if (!bparams.float_placement.empty()) {
3559 os << '[' << bparams.float_placement << "]\n";
3564 case LyXParagraph::ALGORITHM:
3565 os << "\\begin{algorithm}\n";
3570 if (footnotekind != LyXParagraph::FOOTNOTE
3571 || !footer_in_body) {
3572 // Process text for all floats except footnotes in body
3574 LyXLayout const & style =
3576 .Style(bparams.textclass, par->layout);
3578 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3580 if (style.isEnvironment()
3581 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3582 // Allows the use of minipages within float
3583 // environments. Shouldn't be circular because
3584 // we don't support footnotes inside
3585 // floats (yet). ARRae
3586 par = par->TeXEnvironment(buf, bparams, os,
3591 par = par->TeXOnePar(buf, bparams,
3592 os, texrow, moving_arg,
3597 if (par && !par->IsDummy() && par->depth > depth) {
3598 par = par->TeXDeeper(buf, bparams, os, texrow,
3602 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3604 // process footnotes > depth 0 or in environments separately
3605 // NOTE: Currently don't support footnotes within footnotes
3606 // even though that is possible using the \footnotemark
3607 std::ostringstream dummy;
3608 TexRow dummy_texrow;
3609 int dummy_count = 0;
3611 LyXLayout const & style =
3613 .Style(bparams.textclass, par->layout);
3615 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3617 if (style.isEnvironment()
3618 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3619 // Allows the use of minipages within float
3620 // environments. Shouldn't be circular because
3621 // we don't support footnotes inside
3622 // floats (yet). ARRae
3623 par = par->TeXEnvironment(buf, bparams,
3625 dummy, dummy_texrow,
3628 par = par->TeXOnePar(buf, bparams,
3631 dummy, dummy_texrow,
3635 if (par && !par->IsDummy() && par->depth > depth) {
3636 par = par->TeXDeeper(buf, bparams,
3638 dummy, dummy_texrow,
3642 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3644 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3645 "Footnote in a Footnote -- not supported"
3650 switch (footnotekind) {
3651 case LyXParagraph::FOOTNOTE:
3652 if (footer_in_body) {
3653 // This helps tell which of the multiple
3654 // footnotetexts an error was in.
3656 foot_texrow.newline();
3661 case LyXParagraph::MARGIN:
3664 case LyXParagraph::FIG:
3665 if (pextra_type == PEXTRA_FLOATFLT
3666 && (!pextra_width.empty()
3667 || !pextra_widthp.empty()))
3668 os << "\\end{floatingfigure}";
3670 os << "\\end{figure}";
3672 case LyXParagraph::TAB:
3673 os << "\\end{table}";
3675 case LyXParagraph::WIDE_FIG:
3676 os << "\\end{figure*}";
3678 case LyXParagraph::WIDE_TAB:
3679 os << "\\end{table*}";
3681 case LyXParagraph::ALGORITHM:
3682 os << "\\end{algorithm}";
3689 if (footnotekind != LyXParagraph::FOOTNOTE
3690 && footnotekind != LyXParagraph::MARGIN) {
3691 // we need to ensure that real floats like tables and figures
3692 // have their \end{} on a line of their own otherwise we can
3693 // get incorrect results when using the endfloat.sty package.
3698 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3703 bool LyXParagraph::IsDummy() const
3705 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3706 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3710 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3711 int type, string const & width,
3712 string const & widthp)
3715 pextra_width = width;
3716 pextra_widthp = widthp;
3718 if (textclasslist.Style(bparams.textclass,
3719 layout).isEnvironment()) {
3720 LyXParagraph * par = this;
3721 LyXParagraph * ppar = par;
3723 while (par && (par->layout == layout)
3724 && (par->depth == depth)) {
3726 par = par->Previous();
3729 par = par->FirstPhysicalPar();
3731 while (par && par->depth > depth) {
3732 par = par->Previous();
3735 par = par->FirstPhysicalPar();
3740 while (par && (par->layout == layout)
3741 && (par->depth == depth)) {
3742 par->pextra_type = type;
3743 par->pextra_width = width;
3744 par->pextra_widthp = widthp;
3746 par = par->NextAfterFootnote();
3750 if (par && (par->depth > depth))
3751 par->SetPExtraType(bparams,
3752 type, width, widthp);
3754 while (par && ((par->depth > depth) || par->IsDummy()))
3755 par = par->NextAfterFootnote();
3757 while (par && ((par->depth > depth)))
3765 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3767 if (pextra_type == PEXTRA_NONE)
3770 pextra_type = PEXTRA_NONE;
3771 pextra_width.erase();
3772 pextra_widthp.erase();
3774 if (textclasslist.Style(bparams.textclass,
3775 layout).isEnvironment()) {
3776 LyXParagraph * par = this;
3777 LyXParagraph * ppar = par;
3779 while (par && (par->layout == layout)
3780 && (par->depth == depth)) {
3782 par = par->Previous();
3785 par = par->FirstPhysicalPar();
3787 while (par && par->depth > depth) {
3788 par = par->Previous();
3791 par = par->FirstPhysicalPar();
3796 while (par && (par->layout == layout)
3797 && (par->depth == depth)) {
3798 par->pextra_type = PEXTRA_NONE;
3799 par->pextra_width.erase();
3800 par->pextra_widthp.erase();
3802 par = par->NextAfterFootnote();
3806 if (par && (par->depth > depth))
3807 par->UnsetPExtraType(bparams);
3809 while (par && ((par->depth > depth) || par->IsDummy()))
3810 par = par->NextAfterFootnote();
3812 while (par && ((par->depth > depth)))
3820 bool LyXParagraph::IsHfill(size_type pos) const
3822 return IsHfillChar(GetChar(pos));
3826 bool LyXParagraph::IsInset(size_type pos) const
3828 return IsInsetChar(GetChar(pos));
3833 bool LyXParagraph::IsFloat(size_type pos) const
3835 return IsFloatChar(GetChar(pos));
3840 bool LyXParagraph::IsNewline(size_type pos) const
3842 return pos >= 0 && IsNewlineChar(GetChar(pos));
3846 bool LyXParagraph::IsSeparator(size_type pos) const
3848 return IsSeparatorChar(GetChar(pos));
3852 bool LyXParagraph::IsLineSeparator(size_type pos) const
3854 return IsLineSeparatorChar(GetChar(pos));
3858 bool LyXParagraph::IsKomma(size_type pos) const
3860 return IsKommaChar(GetChar(pos));
3864 /// Used by the spellchecker
3865 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3867 value_type const c = GetChar(pos);
3868 if (IsLetterChar(c))
3870 // '\0' is not a letter, allthough every string contains "" (below)
3873 // We want to pass the ' and escape chars to ispell
3874 string const extra = lyxrc.isp_esc_chars + '\'';
3875 char ch[2] = { c, 0 };
3876 return contains(extra, ch);
3880 bool LyXParagraph::IsWord(size_type pos ) const
3882 return IsWordChar(GetChar(pos)) ;
3887 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3891 return FirstPhysicalPar()->getParLanguage(bparams);
3895 #ifdef DO_USE_DEFAULT_LANGUAGE
3896 Language const * lang = GetFirstFontSettings().language();
3898 if (lang->lang() == "default")
3899 return bparams.language;
3902 return GetFirstFontSettings().language();
3904 } else if (previous)
3905 return previous->getParLanguage(bparams);
3907 return bparams.language;
3911 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3913 return lyxrc.rtl_support
3914 && getParLanguage(bparams)->RightToLeft();
3918 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3919 Language const * from, Language const * to)
3921 for (size_type i = 0; i < size(); ++i) {
3922 LyXFont font = GetFontSettings(bparams, i);
3923 if (font.language() == from) {
3924 font.setLanguage(to);
3931 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3933 Language const * doc_language = bparams.language;
3934 for (FontList::const_iterator cit = fontlist.begin();
3935 cit != fontlist.end(); ++cit)
3936 if ((*cit).font.language() != doc_language)
3942 // Convert the paragraph to a string.
3943 // Used for building the table of contents
3944 string const LyXParagraph::String(Buffer const * buffer, bool label)
3946 BufferParams const & bparams = buffer->params;
3949 if (label && !IsDummy() && !labelstring.empty())
3951 if (label && !labelstring.empty())
3953 s += labelstring + ' ';
3954 string::size_type const len = s.size();
3956 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3957 value_type c = GetChar(i);
3960 else if (c == META_INSET &&
3961 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3962 std::ostringstream ost;
3963 GetInset(i)->Ascii(buffer, ost);
3964 s += subst(ost.str().c_str(),'\n',' ');
3969 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
3970 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
3971 s += NextAfterFootnote()->String(buffer, false);
3975 if (isRightToLeftPar(bparams))
3976 reverse(s.begin() + len,s.end());
3984 string const LyXParagraph::String(Buffer const * buffer,
3985 LyXParagraph::size_type beg,
3986 LyXParagraph::size_type end)
3991 if (beg == 0 && !IsDummy() && !labelstring.empty())
3993 if (beg == 0 && !labelstring.empty())
3995 s += labelstring + ' ';
3997 for (LyXParagraph::size_type i = beg; i < end; ++i) {
3998 value_type c = GetChar(i);
4001 else if (c == META_INSET) {
4002 std::ostringstream ost;
4003 GetInset(i)->Ascii(buffer, ost);
4004 s += ost.str().c_str();
4012 void LyXParagraph::SetInsetOwner(Inset * i)
4015 for (InsetList::const_iterator cit = insetlist.begin();
4016 cit != insetlist.end(); ++cit) {
4018 (*cit).inset->setOwner(i);
4023 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4026 for (InsetList::const_iterator cit = insetlist.begin();
4027 cit != insetlist.end(); ++cit) {
4029 if ((*cit).inset->IsTextInset()) {
4030 static_cast<UpdatableInset *>
4031 ((*cit).inset)->deleteLyXText(bv);
4038 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4041 for (InsetList::const_iterator cit = insetlist.begin();
4042 cit != insetlist.end(); ++cit) {
4044 if ((*cit).inset->IsTextInset()) {
4045 static_cast<UpdatableInset *>
4046 ((*cit).inset)->resizeLyXText(bv);