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"
41 #include "ParameterStruct.h"
47 using std::lower_bound;
48 using std::upper_bound;
51 int tex_code_break_column = 72; // needs non-zero initialization. set later.
52 // this is a bad idea, but how can LyXParagraph find its buffer to get
53 // parameters? (JMarc)
55 extern string bibitemWidest(Buffer const *);
57 // this is a minibuffer
62 LyXFont minibuffer_font;
63 Inset * minibuffer_inset;
68 extern BufferView * current_view;
70 // Initialization of the counter for the paragraph id's,
71 // declared in lyxparagraph.h
72 unsigned int LyXParagraph::paragraph_id = 0;
74 // Initialize static member.
75 ShareContainer<LyXFont> LyXParagraph::FontTable::container;
78 LyXParagraph::LyXParagraph()
80 text.reserve(500); // is this number too big?
81 for (int i = 0; i < 10; ++i) setCounter(i , 0);
87 footnoteflag = LyXParagraph::NO_FOOTNOTE;
88 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
93 bibkey = 0; // ale970302
98 // This konstruktor inserts the new paragraph in a list.
99 LyXParagraph::LyXParagraph(LyXParagraph * par)
104 for (int i = 0; i < 10; ++i) setCounter(i, 0);
107 // double linked list begin
110 next_->previous_ = this;
112 previous_->next_ = this;
115 footnoteflag = LyXParagraph::NO_FOOTNOTE;
116 footnotekind = LyXParagraph::FOOTNOTE;
119 id_ = paragraph_id++;
121 bibkey = 0; // ale970302
127 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
128 BufferParams const & bparams,
129 char footflag, char dth) const
132 if (footnoteflag != LyXParagraph::NO_FOOTNOTE ||
134 || previous_->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
135 // The beginning or the end of a footnote environment?
136 if (footflag != footnoteflag) {
137 footflag = footnoteflag;
139 os << "\n\\begin_float "
140 << string_footnotekinds[footnotekind]
143 os << "\n\\end_float ";
147 // The beginning or end of a deeper (i.e. nested) area?
148 if (dth != params.depth()) {
149 if (params.depth() > dth) {
150 while (params.depth() > dth) {
151 os << "\n\\begin_deeper ";
155 while (params.depth() < dth) {
156 os << "\n\\end_deeper ";
162 // First write the layout
164 << textclasslist.NameOfLayout(bparams.textclass, layout)
167 // Maybe some vertical spaces.
168 if (params.spaceTop().kind() != VSpace::NONE)
169 os << "\\added_space_top "
170 << params.spaceTop().asLyXCommand() << " ";
171 if (params.spaceBottom().kind() != VSpace::NONE)
172 os << "\\added_space_bottom "
173 << params.spaceBottom().asLyXCommand() << " ";
175 // Maybe the paragraph has special spacing
176 params.spacing().writeFile(os, true);
178 // The labelwidth string used in lists.
179 if (!params.labelWidthString().empty())
180 os << "\\labelwidthstring "
181 << params.labelWidthString() << '\n';
183 // Lines above or below?
184 if (params.lineTop())
186 if (params.lineBottom())
187 os << "\\line_bottom ";
189 // Pagebreaks above or below?
190 if (params.pagebreakTop())
191 os << "\\pagebreak_top ";
192 if (params.pagebreakBottom())
193 os << "\\pagebreak_bottom ";
195 // Start of appendix?
196 if (params.startOfAppendix())
197 os << "\\start_of_appendix ";
200 if (params.noindent())
204 if (params.align() != LYX_ALIGN_LAYOUT) {
206 switch (params.align()) {
207 case LYX_ALIGN_LEFT: h = 1; break;
208 case LYX_ALIGN_RIGHT: h = 2; break;
209 case LYX_ALIGN_CENTER: h = 3; break;
210 default: h = 0; break;
212 os << "\\align " << string_align[h] << " ";
215 if (params.pextraType() != PEXTRA_NONE) {
216 os << "\\pextra_type " << params.pextraType();
217 if (params.pextraType() == PEXTRA_MINIPAGE) {
218 os << " \\pextra_alignment "
219 << params.pextraAlignment();
220 if (params.pextraHfill())
221 os << " \\pextra_hfill "
222 << params.pextraHfill();
223 if (params.pextraStartMinipage())
224 os << " \\pextra_start_minipage "
225 << params.pextraStartMinipage();
227 if (!params.pextraWidth().empty()) {
228 os << " \\pextra_width "
229 << VSpace(params.pextraWidth()).asLyXCommand();
230 } else if (!params.pextraWidthp().empty()) {
231 os << " \\pextra_widthp "
232 << params.pextraWidthp();
239 // Dummy layout. This means that a footnote ended.
240 os << "\n\\end_float ";
241 footflag = LyXParagraph::NO_FOOTNOTE;
247 bibkey->Write(buf, os);
249 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
252 for (size_type i = 0; i < size(); ++i) {
258 // Write font changes
259 LyXFont font2 = GetFontSettings(bparams, i);
260 if (font2 != font1) {
261 font2.lyxWriteChanges(font1, os);
266 value_type const c = GetChar(i);
270 Inset const * inset = GetInset(i);
272 if (inset->DirectWrite()) {
273 // international char, let it write
274 // code directly so it's shorter in
276 inset->Write(buf, os);
278 os << "\n\\begin_inset ";
279 inset->Write(buf, os);
280 os << "\n\\end_inset \n\n";
286 os << "\n\\newline \n";
290 os << "\n\\hfill \n";
294 os << "\n\\backslash \n";
298 if (i + 1 < size() && GetChar(i + 1) == ' ') {
305 if ((column > 70 && c == ' ')
310 // this check is to amend a bug. LyX sometimes
311 // inserts '\0' this could cause problems.
315 lyxerr << "ERROR (LyXParagraph::writeFile):"
316 " NULL char in structure." << endl;
322 // now write the next paragraph
324 next_->writeFile(buf, os, bparams, footflag, dth);
328 void LyXParagraph::validate(LaTeXFeatures & features) const
330 BufferParams const & bparams = features.bufferParams();
333 // this will be useful later
334 LyXLayout const & layout =
335 textclasslist.Style(bparams.textclass,
340 if (params.lineTop() || params.lineBottom())
341 features.lyxline = true;
342 if (!params.spacing().isDefault())
343 features.setspace = true;
346 features.layout[GetLayout()] = true;
349 Language const * doc_language = bparams.language;
351 for (FontList::const_iterator cit = fontlist.begin();
352 cit != fontlist.end(); ++cit) {
353 if ((*cit).font().noun() == LyXFont::ON) {
354 lyxerr[Debug::LATEX] << "font.noun: "
355 << (*cit).font().noun()
357 features.noun = true;
358 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
359 << (*cit).font().stateText(0)
362 switch ((*cit).font().color()) {
364 case LColor::inherit:
368 features.color = true;
369 lyxerr[Debug::LATEX] << "Color enabled. Font: "
370 << (*cit).font().stateText(0)
374 Language const * language = (*cit).font().language();
375 if (language->babel() != doc_language->babel()) {
376 features.UsedLanguages.insert(language);
377 lyxerr[Debug::LATEX] << "Found language "
378 << language->babel() << endl;
383 for (InsetList::const_iterator cit = insetlist.begin();
384 cit != insetlist.end(); ++cit) {
386 (*cit).inset->Validate(features);
390 if (params.pextraType() == PEXTRA_INDENT)
391 features.LyXParagraphIndent = true;
392 if (params.pextraType() == PEXTRA_FLOATFLT)
393 features.floatflt = true;
396 if (layout.needprotect
397 && next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
398 features.NeedLyXFootnoteCode = true;
401 if (bparams.paragraph_separation == BufferParams::PARSEP_INDENT
402 && params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE)
403 features.NeedLyXMinipageIndent = true;
406 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
407 features.algorithm = true;
412 // First few functions needed for cut and paste and paragraph breaking.
413 void LyXParagraph::CopyIntoMinibuffer(Buffer const & buffer,
414 LyXParagraph::size_type pos) const
416 BufferParams bparams = buffer.params;
418 minibuffer_char = GetChar(pos);
419 minibuffer_font = GetFontSettings(bparams, pos);
420 minibuffer_inset = 0;
421 if (minibuffer_char == LyXParagraph::META_INSET) {
423 minibuffer_inset = GetInset(pos)->Clone(buffer);
425 minibuffer_inset = 0;
426 minibuffer_char = ' ';
427 // This reflects what GetInset() does (ARRae)
433 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
434 LyXParagraph::size_type pos)
436 minibuffer_char = GetChar(pos);
437 minibuffer_font = GetFontSettings(bparams, pos);
438 minibuffer_inset = 0;
439 if (minibuffer_char == LyXParagraph::META_INSET) {
441 minibuffer_inset = GetInset(pos);
442 // This is a little hack since I want exactly
443 // the inset, not just a clone. Otherwise
444 // the inset would be deleted when calling Erase(pos)
446 InsetTable search_elem(pos, 0);
447 InsetList::iterator it =
448 lower_bound(insetlist.begin(),
450 search_elem, matchIT());
451 if (it != insetlist.end() && (*it).pos == pos)
454 minibuffer_inset = 0;
455 minibuffer_char = ' ';
456 // This reflects what GetInset() does (ARRae)
461 // Erase(pos); now the caller is responsible for that.
465 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
467 if ((minibuffer_char == LyXParagraph::META_INSET) &&
468 !InsertInsetAllowed(minibuffer_inset))
470 if (minibuffer_char == LyXParagraph::META_INSET)
471 InsertInset(pos, minibuffer_inset, minibuffer_font);
473 InsertChar(pos, minibuffer_char, minibuffer_font);
481 void LyXParagraph::Clear()
490 // the destructor removes the new paragraph from the list
491 LyXParagraph::~LyXParagraph()
494 previous_->next_ = next_;
496 next_->previous_ = previous_;
498 for (InsetList::iterator it = insetlist.begin();
499 it != insetlist.end(); ++it) {
506 //lyxerr << "LyXParagraph::paragraph_id = "
507 // << LyXParagraph::paragraph_id << endl;
511 void LyXParagraph::Erase(LyXParagraph::size_type pos)
514 // > because last is the next unused position, and you can
515 // use it if you want
517 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
518 NextAfterFootnote()->Erase(pos - text.size() - 1);
520 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
521 "position does not exist." << endl;
525 Assert(pos < size());
528 if (pos < size()) { // last is free for insertation, but should be empty
530 // if it is an inset, delete the inset entry
531 if (text[pos] == LyXParagraph::META_INSET) {
533 InsetTable search_inset(pos, 0);
534 InsetList::iterator it =
535 lower_bound(insetlist.begin(),
537 search_inset, matchIT());
538 if (it != insetlist.end() && (*it).pos == pos) {
544 text.erase(text.begin() + pos);
546 // Erase entries in the tables.
547 FontTable search_font(pos, LyXFont());
549 FontList::iterator it =
550 lower_bound(fontlist.begin(),
552 search_font, matchFT());
553 if (it != fontlist.end() && (*it).pos() == pos &&
555 (it != fontlist.begin() && (*(it - 1)).pos() == pos - 1))) {
556 // If it is a multi-character font
557 // entry, we just make it smaller
558 // (see update below), otherwise we
560 unsigned int const i = it - fontlist.begin();
561 fontlist.erase(fontlist.begin() + i);
562 it = fontlist.begin() + i;
563 if (i > 0 && i < fontlist.size() &&
564 fontlist[i - 1].font() == fontlist[i].font()) {
565 fontlist.erase(fontlist.begin() + i - 1);
566 it = fontlist.begin() + i - 1;
570 // Update all other entries.
571 FontList::iterator fend = fontlist.end();
572 for (; it != fend; ++it)
573 (*it).pos((*it).pos() - 1);
575 // Update the inset table.
576 InsetTable search_inset(pos, 0);
577 InsetList::iterator lend = insetlist.end();
578 for (InsetList::iterator it =
579 upper_bound(insetlist.begin(),
581 search_inset, matchIT());
586 lyxerr << "ERROR (LyXParagraph::Erase): "
587 "can't erase non-existant char." << endl;
593 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
594 LyXParagraph::value_type c)
596 LyXFont const f(LyXFont::ALL_INHERIT);
597 InsertChar(pos, c, f);
601 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
602 LyXParagraph::value_type c,
603 LyXFont const & font)
606 // > because last is the next unused position, and you can
607 // use it if you want
610 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
611 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
614 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
615 "position does not exist." << endl;
619 Assert(pos <= size());
621 text.insert(text.begin() + pos, c);
623 // Update the font table.
624 FontTable search_font(pos, LyXFont());
625 for (FontList::iterator it = lower_bound(fontlist.begin(),
627 search_font, matchFT());
628 it != fontlist.end(); ++it)
629 (*it).pos((*it).pos() + 1);
631 // Update the inset table.
632 InsetTable search_inset(pos, 0);
633 for (InsetList::iterator it = lower_bound(insetlist.begin(),
635 search_inset, matchIT());
636 it != insetlist.end(); ++it)
643 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
646 LyXFont const f(LyXFont::ALL_INHERIT);
647 InsertInset(pos, inset, f);
651 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
652 Inset * inset, LyXFont const & font)
657 // > because last is the next unused position, and you can
658 // use it if you want
661 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
663 ->InsertInset(pos - text.size() - 1,
666 lyxerr << "ERROR (LyXParagraph::InsertInset): "
667 "position does not exist: " << pos << endl;
671 Assert(pos <= size());
674 InsertChar(pos, META_INSET, font);
675 Assert(text[pos] == META_INSET);
677 // Add a new entry in the inset table.
678 InsetTable search_inset(pos, 0);
679 InsetList::iterator it = lower_bound(insetlist.begin(),
681 search_inset, matchIT());
682 if (it != insetlist.end() && (*it).pos == pos)
683 lyxerr << "ERROR (LyXParagraph::InsertInset): "
684 "there is an inset in position: " << pos << endl;
686 insetlist.insert(it, InsetTable(pos, inset));
688 inset->setOwner(inset_owner);
692 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
694 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
697 return inset_owner->InsertInsetAllowed(inset);
702 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
707 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
708 return NextAfterFootnote()
709 ->GetInset(pos - text.size() - 1);
711 lyxerr << "ERROR (LyXParagraph::GetInset): "
712 "position does not exist: "
718 Assert(pos < size());
721 InsetTable search_inset(pos, 0);
722 InsetList::iterator it = lower_bound(insetlist.begin(),
724 search_inset, matchIT());
725 if (it != insetlist.end() && (*it).pos == pos)
728 lyxerr << "ERROR (LyXParagraph::GetInset): "
729 "Inset does not exist: " << pos << endl;
732 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
733 // Did this commenting out introduce a bug? So far I have not
734 // see any, please enlighten me. (Lgb)
735 // My guess is that since the inset does not exist, we might
736 // as well replace it with a space to prevent craches. (Asger)
741 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
746 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
747 return NextAfterFootnote()
748 ->GetInset(pos - text.size() - 1);
750 lyxerr << "ERROR (LyXParagraph::GetInset): "
751 "position does not exist: "
757 Assert(pos < size());
760 InsetTable search_inset(pos, 0);
761 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
763 search_inset, matchIT());
764 if (cit != insetlist.end() && (*cit).pos == pos)
767 lyxerr << "ERROR (LyXParagraph::GetInset): "
768 "Inset does not exist: " << pos << endl;
770 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
771 // Did this commenting out introduce a bug? So far I have not
772 // see any, please enlighten me. (Lgb)
773 // My guess is that since the inset does not exist, we might
774 // as well replace it with a space to prevent craches. (Asger)
779 // Gets uninstantiated font setting at position.
780 // Optimized after profiling. (Asger)
782 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
783 LyXParagraph::size_type pos) const
786 FontTable search_font(pos, LyXFont());
787 FontList::const_iterator cit = lower_bound(fontlist.begin(),
789 search_font, matchFT());
790 if (cit != fontlist.end())
791 return (*cit).font();
793 // > because last is the next unused position, and you can
794 // use it if you want
795 else if (pos > size()) {
797 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
798 return NextAfterFootnote()
799 ->GetFontSettings(bparams,
800 pos - text.size() - 1);
802 // Why is it an error to ask for the font of a
803 // position that does not exist? Would it be
804 // enough for this to be enabled on debug?
805 // We want strict error checking, but it's ok to only
806 // have it when debugging. (Asger)
807 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
808 "position does not exist. "
809 << pos << " (" << static_cast<int>(pos)
811 } else if (pos > 0) {
812 return GetFontSettings(bparams, pos - 1);
815 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
818 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
819 LyXParagraph::size_type pos) const
821 Assert(pos <= size());
822 FontTable search_font(pos, LyXFont());
823 FontList::const_iterator cit = lower_bound(fontlist.begin(),
825 search_font, matchFT());
826 if (cit != fontlist.end())
827 return (*cit).font();
829 if (pos == size() && size())
830 return GetFontSettings(bparams, pos - 1);
832 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
837 // Gets uninstantiated font setting at position 0
838 LyXFont const LyXParagraph::GetFirstFontSettings() const
841 if (!fontlist.empty())
842 return fontlist[0].font();
846 else if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
847 return NextAfterFootnote()->GetFirstFontSettings();
849 return LyXFont(LyXFont::ALL_INHERIT);
853 // Gets the fully instantiated font at a given position in a paragraph
854 // This is basically the same function as LyXText::GetFont() in text2.C.
855 // The difference is that this one is used for generating the LaTeX file,
856 // and thus cosmetic "improvements" are disallowed: This has to deliver
857 // the true picture of the buffer. (Asger)
858 // If position is -1, we get the layout font of the paragraph.
859 // If position is -2, we get the font of the manual label of the paragraph.
860 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
861 LyXParagraph::size_type pos) const
864 LyXLayout const & layout =
865 textclasslist.Style(bparams.textclass,
867 LyXParagraph::size_type main_body = 0;
868 if (layout.labeltype == LABEL_MANUAL)
869 main_body = BeginningOfMainBody();
874 layoutfont = layout.labelfont;
876 layoutfont = layout.font;
877 tmpfont = GetFontSettings(bparams, pos);
878 tmpfont.realize(layoutfont);
880 // process layoutfont for pos == -1 and labelfont for pos < -1
882 tmpfont = layout.font;
884 tmpfont = layout.labelfont;
885 tmpfont.setLanguage(getParLanguage(bparams));
888 // check for environment font information
889 char par_depth = GetDepth();
890 LyXParagraph const * par = this;
891 while (par && par_depth && !tmpfont.resolved()) {
892 par = par->DepthHook(par_depth - 1);
894 tmpfont.realize(textclasslist.
895 Style(bparams.textclass,
896 par->GetLayout()).font);
897 par_depth = par->GetDepth();
901 tmpfont.realize(textclasslist
902 .TextClass(bparams.textclass)
908 /// Returns the height of the highest font in range
910 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
911 LyXParagraph::size_type endpos) const
913 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
914 if (fontlist.empty())
917 FontTable end_search(endpos, LyXFont());
918 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
920 end_search, matchFT());
921 if (end_it != fontlist.end())
924 FontTable start_search(startpos, LyXFont());
925 for (FontList::const_iterator cit =
926 lower_bound(fontlist.begin(),
928 start_search, matchFT());
929 cit != end_it; ++cit) {
930 LyXFont::FONT_SIZE size = (*cit).font().size();
931 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
939 LyXParagraph::value_type
940 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
948 // > because last is the next unused position, and you can
949 // use it if you want
950 else if (pos > size()) {
951 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
952 return NextAfterFootnote()
953 ->GetChar(pos - text.size() - 1);
955 lyxerr << "ERROR (LyXParagraph::GetChar const): "
956 "position does not exist."
957 << pos << " (" << static_cast<int>(pos)
963 // We should have a footnote environment.
964 if (!next_ || next_->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
965 // Notice that LyX does request the
966 // last char from time to time. (Asger)
967 //lyxerr << "ERROR (LyXParagraph::GetChar): "
968 // "expected footnote." << endl;
971 switch (next_->footnotekind) {
972 case LyXParagraph::FOOTNOTE:
973 return LyXParagraph::META_FOOTNOTE;
974 case LyXParagraph::MARGIN:
975 return LyXParagraph::META_MARGIN;
976 case LyXParagraph::FIG:
977 case LyXParagraph::WIDE_FIG:
978 return LyXParagraph::META_FIG;
979 case LyXParagraph::TAB:
980 case LyXParagraph::WIDE_TAB:
981 return LyXParagraph::META_TAB;
982 case LyXParagraph::ALGORITHM:
983 return LyXParagraph::META_ALGORITHM;
985 return '\0'; // to shut up gcc
989 LyXParagraph::value_type
990 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
992 Assert(pos <= size());
993 if (!size() || pos == size()) return '\0';
1000 LyXParagraph::value_type
1001 LyXParagraph::GetUChar(BufferParams const & bparams,
1002 LyXParagraph::size_type pos) const
1004 value_type c = GetChar(pos);
1005 if (!lyxrc.rtl_support)
1035 if (uc != c && GetFontSettings(bparams, pos).isRightToLeft())
1042 // return an string of the current word, and the end of the word in lastpos.
1043 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1045 Assert(lastpos >= 0);
1047 // the current word is defined as starting at the first character
1048 // from the immediate left of lastpospos which meets the definition
1049 // of IsLetter(), continuing to the last character to the right
1050 // of this meeting IsLetter.
1056 // move back until we have a letter
1058 //there's no real reason to have firstpos & lastpos as
1059 //separate variables as this is written, but maybe someon
1060 // will want to return firstpos in the future.
1062 //since someone might have typed a punctuation first
1063 int firstpos = lastpos;
1065 while ((firstpos >= 0) && !IsLetter(firstpos))
1068 // now find the beginning by looking for a nonletter
1070 while ((firstpos>= 0) && IsLetter(firstpos))
1073 // the above is now pointing to the preceeding non-letter
1077 // so copy characters into theword until we get a nonletter
1078 // note that this can easily exceed lastpos, wich means
1079 // that if used in the middle of a word, the whole word
1082 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1089 LyXParagraph::size_type LyXParagraph::Last() const
1091 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1092 return text.size() + NextAfterFootnote()->Last() + 1;
1093 // the 1 is the symbol
1102 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1104 // > because last is the next unused position, and you can
1105 // use it if you want
1108 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1109 return NextAfterFootnote()
1110 ->ParFromPos(pos - text.size() - 1);
1112 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1113 "position does not exist." << endl;
1122 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1124 // > because last is the next unused position, and you can
1125 // use it if you want
1128 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1129 return NextAfterFootnote()
1130 ->PositionInParFromPos(pos - text.size() - 1);
1133 "ERROR (LyXParagraph::PositionInParFromPos): "
1134 "position does not exist." << endl;
1143 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1144 LyXFont const & font)
1147 // > because last is the next unused position, and you can
1148 // use it if you want
1151 next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1152 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1155 lyxerr << "ERROR (LyXParagraph::SetFont): "
1156 "position does not exist." << endl;
1161 Assert(pos <= size());
1164 // First, reduce font against layout/label font
1165 // Update: The SetCharFont() routine in text2.C already
1166 // reduces font, so we don't need to do that here. (Asger)
1167 // No need to simplify this because it will disappear
1168 // in a new kernel. (Asger)
1169 // Next search font table
1171 FontTable search_font(pos, LyXFont());
1172 FontList::iterator it = lower_bound(fontlist.begin(),
1174 search_font, matchFT());
1175 unsigned int i = it - fontlist.begin();
1176 bool notfound = it == fontlist.end();
1178 if (!notfound && fontlist[i].font() == font)
1181 bool begin = pos == 0 || notfound ||
1182 (i > 0 && fontlist[i-1].pos() == pos - 1);
1183 // Is position pos is a beginning of a font block?
1184 bool end = !notfound && fontlist[i].pos() == pos;
1185 // Is position pos is the end of a font block?
1186 if (begin && end) { // A single char block
1187 if (i + 1 < fontlist.size() &&
1188 fontlist[i + 1].font() == font) {
1189 // Merge the singleton block with the next block
1190 fontlist.erase(fontlist.begin() + i);
1191 if (i > 0 && fontlist[i - 1].font() == font)
1192 fontlist.erase(fontlist.begin() + i-1);
1193 } else if (i > 0 && fontlist[i - 1].font() == font) {
1194 // Merge the singleton block with the previous block
1195 fontlist[i - 1].pos(pos);
1196 fontlist.erase(fontlist.begin() + i);
1198 fontlist[i].font(font);
1200 if (i > 0 && fontlist[i - 1].font() == font)
1201 fontlist[i - 1].pos(pos);
1203 fontlist.insert(fontlist.begin() + i,
1204 FontTable(pos, font));
1206 fontlist[i].pos(pos - 1);
1207 if (!(i + 1 < fontlist.size() &&
1208 fontlist[i + 1].font() == font))
1209 fontlist.insert(fontlist.begin() + i + 1,
1210 FontTable(pos, font));
1211 } else { // The general case. The block is splitted into 3 blocks
1212 fontlist.insert(fontlist.begin() + i,
1213 FontTable(pos - 1, fontlist[i].font()));
1214 fontlist.insert(fontlist.begin() + i + 1,
1215 FontTable(pos, font));
1221 void LyXParagraph::next(LyXParagraph * p)
1227 // This function is able to hide closed footnotes.
1228 LyXParagraph * LyXParagraph::next()
1231 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1232 LyXParagraph * tmp = next_;
1234 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1236 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1237 return tmp->next(); /* there can be more than one
1238 footnote in a logical
1241 return next_; // This should never happen!
1248 LyXParagraph const * LyXParagraph::next() const
1251 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1252 LyXParagraph * tmp = next_;
1254 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1256 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1257 return tmp->next(); /* there can be more than one
1258 footnote in a logical
1261 return next_; // This should never happen!
1269 LyXParagraph * LyXParagraph::NextAfterFootnote()
1271 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1272 LyXParagraph * tmp = next_;
1273 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1275 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1276 return tmp; /* there can be more than one footnote
1277 in a logical paragraph */
1279 return next_; // This should never happen!
1287 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1289 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1290 LyXParagraph * tmp = next_;
1291 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1293 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1294 return tmp; /* there can be more than one footnote
1295 in a logical paragraph */
1297 return next_; // This should never happen!
1305 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1308 if (previous_ && previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1310 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1311 tmp = tmp->previous_;
1312 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1313 return tmp; /* there can be more than one footnote
1314 in a logical paragraph */
1316 return previous_; // This should never happen!
1324 LyXParagraph * LyXParagraph::LastPhysicalPar()
1326 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1329 LyXParagraph * tmp = this;
1331 && tmp->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1332 tmp = tmp->NextAfterFootnote();
1340 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1342 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1345 LyXParagraph const * tmp = this;
1347 && tmp->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1348 tmp = tmp->NextAfterFootnote();
1356 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1360 LyXParagraph * tmppar = this;
1364 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1365 tmppar = tmppar->previous_;
1376 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1380 LyXParagraph const * tmppar = this;
1384 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1385 tmppar = tmppar->previous_;
1395 void LyXParagraph::previous(LyXParagraph * p)
1401 // This function is able to hide closed footnotes.
1402 LyXParagraph * LyXParagraph::previous()
1405 LyXParagraph * tmp = previous_;
1410 && tmp->previous_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1411 tmp = tmp->previous_;
1413 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1414 tmp = tmp->previous_;
1415 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1416 return tmp->next_->previous();
1426 // This function is able to hide closed footnotes.
1427 LyXParagraph const * LyXParagraph::previous() const
1430 LyXParagraph * tmp = previous_;
1434 && tmp->previous_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1435 tmp = tmp->previous_;
1437 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1438 tmp = tmp->previous_;
1439 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1440 return tmp->next_->previous();
1451 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1452 LyXParagraph::size_type pos,
1457 // create a new paragraph
1459 size_type pos_first;
1460 LyXParagraph * par = ParFromPos(pos);
1461 LyXParagraph * firstpar = FirstPhysicalPar();
1463 LyXParagraph * tmp = new LyXParagraph(par);
1465 tmp->footnoteflag = footnoteflag;
1466 tmp->footnotekind = footnotekind;
1468 // this is an idea for a more userfriendly layout handling, I will
1469 // see what the users say
1471 // layout stays the same with latex-environments
1473 tmp->SetOnlyLayout(bparams, firstpar->layout);
1474 tmp->SetLabelWidthString(firstpar->params.labelWidthString());
1477 if (Last() > pos || !Last() || flag == 2) {
1478 tmp->SetOnlyLayout(bparams, firstpar->layout);
1479 tmp->params.align(firstpar->params.align());
1480 tmp->SetLabelWidthString(firstpar->params.labelWidthString());
1482 tmp->params.lineBottom(firstpar->params.lineBottom());
1483 firstpar->params.lineBottom(false);
1484 tmp->params.pagebreakBottom(firstpar->params.pagebreakBottom());
1485 firstpar->params.pagebreakBottom(false);
1486 tmp->params.spaceBottom(firstpar->params.spaceBottom());
1487 firstpar->params.spaceBottom(VSpace(VSpace::NONE));
1489 tmp->params.depth(firstpar->params.depth());
1490 tmp->params.noindent(firstpar->params.noindent());
1492 // copy everything behind the break-position
1493 // to the new paragraph
1495 while (ParFromPos(pos_first) != par)
1497 pos_end = pos_first + par->text.size() - 1;
1500 for (i = j = pos; i <= pos_end; ++i) {
1501 par->CutIntoMinibuffer(bparams, i - pos_first);
1502 if (tmp->InsertFromMinibuffer(j - pos))
1508 for (i = pos_end; i >= pos; --i)
1509 par->Erase(i - pos_first);
1514 // just an idea of me
1516 tmp->params.lineTop(firstpar->params.lineTop());
1517 tmp->params.pagebreakTop(firstpar->params.pagebreakTop());
1518 tmp->params.spaceTop(firstpar->params.spaceTop());
1519 tmp->bibkey = firstpar->bibkey;
1521 // layout stays the same with latex-environments
1523 firstpar->SetOnlyLayout(bparams, tmp->layout);
1524 firstpar->SetLabelWidthString(tmp->params.labelWidthString());
1525 firstpar->params.depth(tmp->params.depth());
1530 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1531 LyXParagraph::size_type pos,
1534 // create a new paragraph
1535 LyXParagraph * tmp = new LyXParagraph(this);
1537 // this is an idea for a more userfriendly layout handling, I will
1538 // see what the users say
1540 // layout stays the same with latex-environments
1542 tmp->SetOnlyLayout(layout);
1543 tmp->SetLabelWidthString(params.labelWidthString());
1546 if (size() > pos || !size() || flag == 2) {
1547 tmp->SetOnlyLayout(layout);
1548 tmp->params.align(params.align());
1549 tmp->SetLabelWidthString(params.labelWidthString());
1551 tmp->params.lineBottom(params.lineBottom());
1552 params.lineBottom(false);
1553 tmp->params.pagebreakBottom(params.pagebreakBottom());
1554 params.pagebreakBottom(false);
1555 tmp->params.spaceBottom(params.spaceBottom());
1556 params.spaceBottom(VSpace(VSpace::NONE));
1558 tmp->params.depth(params.depth());
1559 tmp->params.noindent(params.noindent());
1561 // copy everything behind the break-position
1562 // to the new paragraph
1563 size_type pos_end = text.size() - 1;
1566 for (; i <= pos_end; ++i) {
1567 CutIntoMinibuffer(bparams, i);
1568 if (tmp->InsertFromMinibuffer(j - pos))
1572 for (i = pos_end; i >= pos; --i)
1578 // just an idea of me
1580 tmp->params.lineTop(params.lineTop());
1581 tmp->params.pagebreakTop(params.pagebreakTop());
1582 tmp->params.spaceTop(params.spaceTop());
1583 tmp->bibkey = bibkey;
1585 // layout stays the same with latex-environments
1587 SetOnlyLayout(tmp->layout);
1588 SetLabelWidthString(tmp->params.labelWidthString());
1589 params.depth(tmp->params.depth());
1596 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1599 par = par->FirstPhysicalPar();
1600 footnoteflag = par->footnoteflag;
1601 footnotekind = par->footnotekind;
1603 layout = par->layout;
1604 params.makeSame(par->params);
1606 // This can be changed after NEW_INSETS is in effect. (Lgb)
1607 SetLabelWidthString(par->params.labelWidthString());
1612 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1614 LyXParagraph * tmppar = this;
1617 && tmppar->previous_->footnoteflag ==
1618 LyXParagraph::CLOSED_FOOTNOTE)
1619 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1620 tmppar = tmppar->previous_;
1623 return this; // This should never happen!
1630 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1632 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1642 && (IsNewline(0) || IsLineSeparator(0))){
1653 LyXParagraph * LyXParagraph::Clone() const
1655 // create a new paragraph
1656 LyXParagraph * result = new LyXParagraph;
1658 result->MakeSameLayout(this);
1660 // this is because of the dummy layout of the paragraphs that
1662 result->layout = layout;
1664 result->inset_owner = inset_owner;
1668 result->bibkey = static_cast<InsetBibKey *>
1669 (bibkey->Clone(*current_view->buffer()));
1673 // copy everything behind the break-position to the new paragraph
1675 result->text = text;
1676 result->fontlist = fontlist;
1677 result->insetlist = insetlist;
1678 for (InsetList::iterator it = result->insetlist.begin();
1679 it != result->insetlist.end(); ++it)
1680 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1685 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1688 par = par->FirstPhysicalPar();
1693 par->footnoteflag == footnoteflag &&
1694 par->footnotekind == footnotekind &&
1696 par->layout == layout &&
1697 params.sameLayout(par->params);
1701 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1702 LyXParagraph::size_type pos)
1705 // create a new paragraph
1706 LyXParagraph * par = ParFromPos(pos);
1708 LyXParagraph * tmp = new LyXParagraph(par);
1710 tmp->MakeSameLayout(par);
1712 // When can pos < Last()?
1713 // I guess pos == Last() is possible.
1715 // copy everything behind the break-position to the new
1717 size_type pos_first = 0;
1718 while (ParFromPos(pos_first) != par)
1720 size_type pos_end = pos_first + par->text.size() - 1;
1723 for (i = j = pos; i <= pos_end; ++i) {
1724 par->CutIntoMinibuffer(bparams, i - pos_first);
1725 if (tmp->InsertFromMinibuffer(j - pos))
1731 for (size_type i = pos_end; i >= pos; --i)
1732 par->Erase(i - pos_first);
1737 // create a new paragraph
1738 LyXParagraph * tmp = new LyXParagraph(this);
1739 tmp->MakeSameLayout(this);
1741 // When can pos > Last()?
1742 // I guess pos == Last() is possible.
1744 // copy everything behind the break-position to the new
1746 size_type pos_end = text.size() - 1;
1749 for (i = j = pos; i <= pos_end; ++i) {
1750 CutIntoMinibuffer(bparams, i);
1751 if (tmp->InsertFromMinibuffer(j - pos))
1757 for (size_type i = pos_end; i >= pos; --i)
1766 // Be carefull, this does not make any check at all.
1767 // This method has wrong name, it combined this par with the next par.
1768 // In that sense it is the reverse of break paragraph. (Lgb)
1769 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1771 // copy the next paragraph to this one
1772 LyXParagraph * the_next = next();
1774 // first the DTP-stuff
1776 LyXParagraph * firstpar = FirstPhysicalPar();
1777 firstpar->params.lineBottom(the_next->params.lineBottom());
1778 firstpar->params.spaceBottom(the_next->params.spaceBottom());
1779 firstpar->params.pagebreakBottom(the_next->params.pagebreakBottom());
1781 size_type pos_end = the_next->text.size() - 1;
1782 size_type pos_insert = Last();
1784 params.lineBottom(the_next->params.lineBottom());
1785 params.spaceBottom(the_next->params.spaceBottom());
1786 params.pagebreakBottom(the_next->params.pagebreakBottom());
1788 size_type pos_end = the_next->text.size() - 1;
1789 size_type pos_insert = size();
1792 // ok, now copy the paragraph
1794 for (i = j = 0; i <= pos_end; ++i) {
1795 the_next->CutIntoMinibuffer(bparams, i);
1796 if (InsertFromMinibuffer(pos_insert + j))
1800 // delete the next paragraph
1801 LyXParagraph * ppar = the_next->previous_;
1802 LyXParagraph * npar = the_next->next_;
1809 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1811 LyXParagraph * par = ParFromPos(pos);
1813 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1814 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1820 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1822 LyXParagraph * par = ParFromPos(pos);
1824 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1825 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1832 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1834 LyXParagraph const * par = this;
1835 int par_depth = GetDepth();
1837 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1838 int const endlabeltype =
1839 textclasslist.Style(bparams.textclass,
1840 layout).endlabeltype;
1841 if (endlabeltype != END_LABEL_NO_LABEL) {
1842 LyXParagraph const * last = this;
1844 if (footnoteflag == NO_FOOTNOTE)
1845 last = LastPhysicalPar();
1846 else if (next_->footnoteflag == NO_FOOTNOTE)
1847 return endlabeltype;
1852 if (!last || !last->next_)
1853 return endlabeltype;
1855 int next_depth = last->next_->GetDepth();
1856 if (par_depth > next_depth ||
1857 (par_depth == next_depth && layout != last->next_->GetLayout() ))
1858 return endlabeltype;
1863 par = par->DepthHook(par_depth - 1);
1865 par_depth = par->GetDepth();
1867 return END_LABEL_NO_LABEL;
1871 LyXTextClass::size_type LyXParagraph::GetLayout() const
1874 return FirstPhysicalPar()->layout;
1881 char LyXParagraph::GetDepth() const
1884 return FirstPhysicalPar()->params.depth();
1886 return params.depth();
1891 char LyXParagraph::GetAlign() const
1894 return FirstPhysicalPar()->params.align();
1896 return params.align();
1901 string const & LyXParagraph::GetLabelstring() const
1904 return FirstPhysicalPar()->params.labelString();
1906 return params.labelString();
1911 int LyXParagraph::GetFirstCounter(int i) const
1914 return FirstPhysicalPar()->counter_[i];
1921 // the next two functions are for the manual labels
1922 string const LyXParagraph::GetLabelWidthString() const
1925 if (!FirstPhysicalPar()->params.labelWidthString().empty())
1926 return FirstPhysicalPar()->params.labelWidthString();
1928 if (!params.labelWidthString().empty())
1929 return params.labelWidthString();
1932 return _("Senseless with this layout!");
1936 void LyXParagraph::SetLabelWidthString(string const & s)
1939 LyXParagraph * par = FirstPhysicalPar();
1941 par->params.labelWidthString(s);
1943 params.labelWidthString(s);
1949 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1950 LyXTextClass::size_type new_layout)
1952 LyXParagraph * par = FirstPhysicalPar();
1953 LyXParagraph * ppar = 0;
1954 LyXParagraph * npar = 0;
1956 par->layout = new_layout;
1958 if (par->params.pextraType() == PEXTRA_NONE) {
1959 if (par->previous()) {
1960 ppar = par->previous()->FirstPhysicalPar();
1963 && (ppar->params.depth() > par->params.depth()))
1964 ppar = ppar->previous()->FirstPhysicalPar();
1967 npar = par->next()->NextAfterFootnote();
1970 && (npar->params.depth() > par->params.depth()))
1971 npar = npar->next()->NextAfterFootnote();
1973 if (ppar && (ppar->params.pextraType() != PEXTRA_NONE)) {
1974 string p1 = ppar->params.pextraWidth();
1975 string p2 = ppar->params.pextraWidthp();
1976 ppar->SetPExtraType(bparams,
1977 ppar->params.pextraType(),
1980 if ((par->params.pextraType() == PEXTRA_NONE) &&
1981 npar && (npar->params.pextraType() != PEXTRA_NONE)) {
1982 string const p1 = npar->params.pextraWidth();
1983 string const p2 = npar->params.pextraWidthp();
1984 npar->SetPExtraType(bparams, npar->params.pextraType(),
1990 void LyXParagraph::SetOnlyLayout(LyXTextClass::size_type new_layout)
1993 LyXParagraph * par = this;
1994 LyXParagraph * ppar = 0;
1995 LyXParagraph * npar = 0;
1997 par->layout = new_layout;
1999 if (par->previous()) {
2000 ppar = par->previous();
2003 && (ppar->params.depth() > par->params.depth()))
2004 ppar = ppar->previous();
2010 && (npar->params.depth() > par->params.depth()))
2011 npar = npar->next();
2014 layout = new_layout;
2021 void LyXParagraph::SetLayout(BufferParams const & bparams,
2022 LyXTextClass::size_type new_layout)
2025 * par = FirstPhysicalPar(),
2029 par->layout = new_layout;
2030 par->params.labelWidthString(string());
2031 par->params.align(LYX_ALIGN_LAYOUT);
2032 par->params.spaceTop(VSpace(VSpace::NONE));
2033 par->params.spaceBottom(VSpace(VSpace::NONE));
2034 par->params.spacing(Spacing(Spacing::Default));
2036 if (par->params.pextraType() == PEXTRA_NONE) {
2037 if (par->previous()) {
2038 ppar = par->previous()->FirstPhysicalPar();
2041 && (ppar->params.depth() > par->params.depth()))
2042 ppar = ppar->previous()->FirstPhysicalPar();
2045 npar = par->next()->NextAfterFootnote();
2048 && (npar->params.depth() > par->params.depth()))
2049 npar = npar->next()->NextAfterFootnote();
2051 if (ppar && (ppar->params.pextraType() != PEXTRA_NONE)) {
2052 string const p1 = ppar->params.pextraWidth();
2053 string const p2 = ppar->params.pextraWidthp();
2054 ppar->SetPExtraType(bparams, ppar->params.pextraType(),
2057 if ((par->params.pextraType() == PEXTRA_NONE) &&
2058 npar && (npar->params.pextraType() != PEXTRA_NONE)) {
2059 string const p1 = npar->params.pextraWidth();
2060 string const p2 = npar->params.pextraWidthp();
2061 npar->SetPExtraType(bparams, npar->params.pextraType(),
2067 void LyXParagraph::SetLayout(LyXTextClass::size_type new_layout)
2069 layout = new_layout;
2070 params.labelWidthString(string());
2071 params.align(LYX_ALIGN_LAYOUT);
2072 params.spaceTop(VSpace(VSpace::NONE));
2073 params.spaceBottom(VSpace(VSpace::NONE));
2074 params.spacing(Spacing(Spacing::Default));
2079 // if the layout of a paragraph contains a manual label, the beginning of the
2080 // main body is the beginning of the second word. This is what the par-
2081 // function returns. If the layout does not contain a label, the main
2082 // body always starts with position 0. This differentiation is necessary,
2083 // because there cannot be a newline or a blank <= the beginning of the
2084 // main body in TeX.
2086 int LyXParagraph::BeginningOfMainBody() const
2089 if (FirstPhysicalPar() != this)
2092 // Unroll the first two cycles of the loop
2093 // and remember the previous character to
2094 // remove unnecessary GetChar() calls
2097 && GetChar(i) != LyXParagraph::META_NEWLINE
2100 char previous_char = 0;
2103 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2104 // Yes, this ^ is supposed to be "= " not "=="
2107 && previous_char != ' '
2108 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2110 previous_char = temp;
2116 if (i == 0 && i == size() &&
2117 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2118 && next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2119 ++i; /* the cursor should not jump
2120 * to the main body if there
2127 LyXParagraph * LyXParagraph::DepthHook(int deth)
2129 LyXParagraph * newpar = this;
2135 newpar = newpar->FirstPhysicalPar()->previous();
2137 newpar = newpar->previous();
2139 } while (newpar && newpar->GetDepth() > deth
2141 && newpar->footnoteflag == footnoteflag
2146 if (previous() || GetDepth())
2147 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2152 return newpar->FirstPhysicalPar();
2159 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2161 LyXParagraph const * newpar = this;
2167 newpar = newpar->FirstPhysicalPar()->previous();
2169 newpar = newpar->previous();
2171 } while (newpar && newpar->GetDepth() > deth
2173 && newpar->footnoteflag == footnoteflag
2178 if (previous() || GetDepth())
2179 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2184 return newpar->FirstPhysicalPar();
2191 int LyXParagraph::AutoDeleteInsets()
2194 InsetList::size_type index = 0;
2195 while (index < insetlist.size()) {
2196 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2197 Erase(insetlist[index].pos);
2198 // Erase() calls to insetlist.erase(&insetlist[index])
2199 // so index shouldn't be increased.
2208 LyXParagraph::inset_iterator
2209 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2211 InsetTable search_inset(pos, 0);
2212 InsetList::iterator it = lower_bound(insetlist.begin(),
2214 search_inset, matchIT());
2215 return inset_iterator(it);
2219 // returns -1 if inset not found
2220 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2223 for (InsetList::const_iterator cit = insetlist.begin();
2224 cit != insetlist.end(); ++cit) {
2225 if ((*cit).inset == inset) {
2229 if (inset == bibkey)
2233 // Think about footnotes.
2234 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2235 && next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2237 NextAfterFootnote()->GetPositionOfInset(inset);
2239 return text.size() + 1 + further;
2246 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2247 BufferParams const & bparams,
2248 ostream & os, TexRow & texrow,
2253 TexRow & foot_texrow,
2258 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2259 LyXLayout const & style =
2260 textclasslist.Style(bparams.textclass,
2263 bool further_blank_line = false;
2266 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2269 if (params.startOfAppendix()) {
2270 os << "\\appendix\n";
2274 if (!params.spacing().isDefault()
2275 && (!previous() || !previous()->HasSameLayout(this))) {
2276 os << params.spacing().writeEnvirBegin() << "\n";
2280 if (tex_code_break_column && style.isCommand()){
2285 if (params.pagebreakTop()) {
2287 further_blank_line = true;
2289 if (params.spaceTop().kind() != VSpace::NONE) {
2290 os << params.spaceTop().asLatexCommand(bparams);
2291 further_blank_line = true;
2294 if (params.lineTop()) {
2295 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2296 << "\\vspace{-1\\parskip}";
2297 further_blank_line = true;
2300 if (further_blank_line){
2305 Language const * language = getParLanguage(bparams);
2306 Language const * doc_language = bparams.language;
2307 Language const * previous_language = previous_
2308 ? previous_->getParLanguage(bparams) : doc_language;
2309 if (language->babel() != doc_language->babel() &&
2310 language->babel() != previous_language->babel()) {
2311 os << subst(lyxrc.language_command_begin, "$$lang",
2317 if (bparams.inputenc == "auto" &&
2318 language->encoding() != previous_language->encoding()) {
2319 os << "\\inputencoding{"
2320 << language->encoding()->LatexName()
2325 switch (style.latextype) {
2328 << style.latexname()
2329 << style.latexparam();
2331 case LATEX_ITEM_ENVIRONMENT:
2333 bibkey->Latex(buf, os, false, false);
2337 case LATEX_LIST_ENVIRONMENT:
2344 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2346 LyXParagraph * par = next_;
2348 // Spit out footnotes
2349 if (lyxrc.rtl_support) {
2350 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
2351 && next_->footnoteflag != footnoteflag) {
2352 LyXParagraph * p = 0;
2353 bool is_rtl = (size() > 0)
2354 ? GetFontSettings(bparams,
2355 size()-1).isRightToLeft()
2356 : language->RightToLeft();
2357 if ((p = NextAfterFootnote()) != 0 &&
2359 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2360 is_rtl = getParLanguage(bparams)->RightToLeft();
2361 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2362 && par->footnoteflag != footnoteflag) {
2363 par = par->TeXFootnote(buf, bparams,
2365 foot_texrow, foot_count,
2367 par->SimpleTeXOnePar(buf, bparams,
2368 os, texrow, moving_arg);
2369 is_rtl = (par->size() > 0)
2370 ? par->GetFontSettings(bparams,
2371 par->size()-1).isRightToLeft()
2372 : language->RightToLeft();
2374 par->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2375 (p = par->NextAfterFootnote()) != 0 &&
2377 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2378 is_rtl = language->RightToLeft();
2383 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2384 && par->footnoteflag != footnoteflag) {
2385 par = par->TeXFootnote(buf, bparams,
2387 foot, foot_texrow, foot_count,
2389 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2395 // Make sure that \\par is done with the font of the last
2396 // character if this has another size as the default.
2397 // This is necessary because LaTeX (and LyX on the screen)
2398 // calculates the space between the baselines according
2399 // to this font. (Matthias)
2401 // Is this really needed ? (Dekel)
2402 // We do not need to use to change the font for the last paragraph
2403 // or for a command.
2405 LyXFont font = getFont(bparams, Last() - 1);
2407 LyXFont font = getFont(bparams, size() - 1);
2409 bool is_command = textclasslist.Style(bparams.textclass,
2410 GetLayout()).isCommand();
2411 if (style.resfont.size() != font.size() && next_ && !is_command) {
2414 os << "\\" << font.latexSize() << " \\par}";
2415 } else if (need_par) {
2417 } else if (is_command)
2420 if (language->babel() != doc_language->babel() &&
2423 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2425 || par->getParLanguage(bparams)->babel() != language->babel())) {
2427 << subst(lyxrc.language_command_end, "$$lang",
2428 doc_language->babel());
2431 switch (style.latextype) {
2432 case LATEX_ITEM_ENVIRONMENT:
2433 case LATEX_LIST_ENVIRONMENT:
2434 if (par && (params.depth() < par->params.depth())) {
2439 case LATEX_ENVIRONMENT:
2440 // if its the last paragraph of the current environment
2441 // skip it otherwise fall through
2443 && (par->layout != layout
2444 || par->params.depth() != params.depth()
2446 || par->params.pextraType() != params.pextraType()
2451 // we don't need it for the last paragraph!!!
2452 // or for tables in floats
2453 // -- effectively creates a \par where there isn't one which
2454 // breaks a \subfigure or \subtable.
2456 // && footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2462 further_blank_line = false;
2463 if (params.lineBottom()) {
2465 os << "\\lyxline{\\" << getFont(bparams,
2466 Last() - 1).latexSize() << '}';
2468 os << "\\lyxline{\\" << getFont(bparams,
2469 size() - 1).latexSize() << '}';
2471 further_blank_line = true;
2474 if (params.spaceBottom().kind() != VSpace::NONE) {
2475 os << params.spaceBottom().asLatexCommand(bparams);
2476 further_blank_line = true;
2479 if (params.pagebreakBottom()) {
2481 further_blank_line = true;
2484 if (further_blank_line){
2489 if (!params.spacing().isDefault()
2490 && (!par || !par->HasSameLayout(this))) {
2491 os << params.spacing().writeEnvirEnd() << "\n";
2495 // we don't need it for the last paragraph!!!
2498 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2499 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2506 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2511 // This one spits out the text of the paragraph
2512 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2513 BufferParams const & bparams,
2514 ostream & os, TexRow & texrow,
2517 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2519 bool return_value = false;
2521 LyXLayout const & style =
2522 textclasslist.Style(bparams.textclass,
2524 LyXFont basefont, last_font;
2526 // Maybe we have to create a optional argument.
2527 size_type main_body;
2528 if (style.labeltype != LABEL_MANUAL)
2531 main_body = BeginningOfMainBody();
2533 if (main_body > 0) {
2535 basefont = getFont(bparams, -2); // Get label font
2537 basefont = getFont(bparams, -1); // Get layout font
2548 if (style.isCommand()) {
2551 } else if (params.align() != LYX_ALIGN_LAYOUT) {
2554 return_value = true;
2558 moving_arg |= style.needprotect;
2560 // Which font is currently active?
2561 LyXFont running_font(basefont);
2562 // Do we have an open font change?
2563 bool open_font = false;
2565 texrow.start(this, 0);
2567 for (size_type i = 0; i < size(); ++i) {
2569 // First char in paragraph or after label?
2575 if (main_body > 0) {
2577 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2580 basefont = getFont(bparams, -1); // Now use the layout font
2581 running_font = basefont;
2585 if (style.isCommand()) {
2588 } else if (params.align() != LYX_ALIGN_LAYOUT && next_) {
2589 // We do not need \par here (Dekel)
2593 return_value = true;
2596 if (params.noindent()) {
2597 os << "\\noindent ";
2600 switch (params.align()) {
2601 case LYX_ALIGN_NONE:
2602 case LYX_ALIGN_BLOCK:
2603 case LYX_ALIGN_LAYOUT:
2604 case LYX_ALIGN_SPECIAL:
2606 case LYX_ALIGN_LEFT:
2609 if (getParLanguage(bparams)->babel() != "hebrew") {
2610 os << "\\raggedright ";
2613 os << "\\raggedleft ";
2617 case LYX_ALIGN_RIGHT:
2620 if (getParLanguage(bparams)->babel() != "hebrew") {
2621 os << "\\raggedleft ";
2624 os << "\\raggedright ";
2628 case LYX_ALIGN_CENTER:
2631 os << "\\centering ";
2637 value_type c = GetChar(i);
2639 // Fully instantiated font
2640 LyXFont font = getFont(bparams, i);
2642 LyXParagraph * p = 0;
2645 previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2646 (p = PreviousBeforeFootnote()) != 0)
2647 last_font = p->getFont(bparams, p->size() - 1);
2650 last_font = running_font;
2652 // Spaces at end of font change are simulated to be
2653 // outside font change, i.e. we write "\textXX{text} "
2654 // rather than "\textXX{text }". (Asger)
2655 if (open_font && c == ' ' && i <= size() - 2
2656 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2657 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2658 font = getFont(bparams, i + 1);
2660 // We end font definition before blanks
2661 if (!font.equalExceptLatex(running_font) && open_font) {
2662 column += running_font.latexWriteEndChanges(os,
2664 (i == main_body-1) ? basefont : font);
2665 running_font = basefont;
2669 // Blanks are printed before start of fontswitch
2671 // Do not print the separation of the optional argument
2672 if (i != main_body - 1) {
2673 SimpleTeXBlanks(os, texrow, i,
2674 column, font, style);
2678 // Do we need to change font?
2679 if (!font.equalExceptLatex(running_font)
2680 && i != main_body-1) {
2681 column += font.latexWriteStartChanges(os, basefont,
2683 running_font = font;
2687 if (c == LyXParagraph::META_NEWLINE) {
2688 // newlines are handled differently here than
2689 // the default in SimpleTeXSpecialChars().
2690 if (!style.newline_allowed
2691 || font.latex() == LyXFont::ON) {
2695 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2698 basefont = getFont(bparams, -1);
2699 running_font = basefont;
2700 if (font.family() ==
2701 LyXFont::TYPEWRITER_FAMILY) {
2709 texrow.start(this, i + 1);
2712 SimpleTeXSpecialChars(buf, bparams,
2713 os, texrow, moving_arg,
2714 font, running_font, basefont,
2715 open_font, style, i, column, c);
2719 // If we have an open font definition, we have to close it
2721 LyXParagraph * p = 0;
2724 && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
2725 && (p = NextAfterFootnote()) != 0
2730 running_font.latexWriteEndChanges(os, basefont,
2731 p->getFont(bparams, 0));
2733 running_font.latexWriteEndChanges(os, basefont, basefont);
2736 // Needed if there is an optional argument but no contents.
2737 if (main_body > 0 && main_body == size()) {
2739 return_value = false;
2742 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2743 return return_value;
2747 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2749 bool retval = false;
2751 case LyXParagraph::META_HFILL:
2752 sgml_string.erase();
2754 case LyXParagraph::META_NEWLINE:
2758 sgml_string = "&";
2761 sgml_string = "<";
2764 sgml_string = ">";
2767 sgml_string = "$";
2770 sgml_string = "#";
2773 sgml_string = "%";
2776 sgml_string = "[";
2779 sgml_string = "]";
2782 sgml_string = "{";
2785 sgml_string = "}";
2788 sgml_string = "˜";
2791 sgml_string = """;
2794 sgml_string = "\";
2800 case '\0': // Ignore :-)
2801 sgml_string.erase();
2811 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2812 LyXParagraph::size_type const i,
2813 int & column, LyXFont const & font,
2814 LyXLayout const & style)
2816 if (column > tex_code_break_column
2818 && GetChar(i - 1) != ' '
2820 // In LaTeX mode, we don't want to
2821 // break lines since some commands
2823 && ! (font.latex() == LyXFont::ON)
2824 // same in FreeSpacing mode
2825 && !style.free_spacing
2826 // In typewriter mode, we want to avoid
2827 // ! . ? : at the end of a line
2828 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2829 && (GetChar(i-1) == '.'
2830 || GetChar(i-1) == '?'
2831 || GetChar(i-1) == ':'
2832 || GetChar(i-1) == '!'))) {
2833 if (tex_code_break_column == 0) {
2834 // in batchmode we need LaTeX to still
2835 // see it as a space not as an extra '\n'
2841 texrow.start(this, i + 1);
2843 } else if (font.latex() == LyXFont::OFF) {
2844 if (style.free_spacing) {
2853 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2854 BufferParams const & bparams,
2855 ostream & os, TexRow & texrow,
2858 LyXFont & running_font,
2861 LyXLayout const & style,
2862 LyXParagraph::size_type & i,
2864 LyXParagraph::value_type const c)
2866 // Two major modes: LaTeX or plain
2867 // Handle here those cases common to both modes
2868 // and then split to handle the two modes separately.
2870 case LyXParagraph::META_INSET: {
2871 Inset * inset = GetInset(i);
2874 int const len = os.tellp();
2875 //ostream::pos_type const len = os.tellp();
2876 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2877 || inset->LyxCode() == Inset::MATH_CODE
2878 || inset->LyxCode() == Inset::URL_CODE)
2879 && running_font.isRightToLeft()) {
2884 int tmp = inset->Latex(buf, os, moving_arg,
2885 style.free_spacing);
2893 column += int(os.tellp()) - len;
2902 case LyXParagraph::META_NEWLINE:
2904 column += running_font.latexWriteEndChanges(os,
2909 basefont = getFont(bparams, -1);
2910 running_font = basefont;
2913 case LyXParagraph::META_HFILL:
2919 // And now for the special cases within each mode
2920 // Are we in LaTeX mode?
2921 if (font.latex() == LyXFont::ON) {
2922 // at present we only have one option
2923 // but I'll leave it as a switch statement
2924 // so its simpler to extend. (ARRae)
2927 // make sure that we will not print
2928 // error generating chars to the tex
2929 // file. This test would not be needed
2930 // if it were done in the buffer
2938 // Plain mode (i.e. not LaTeX)
2941 os << "\\textbackslash{}";
2945 case '°': case '±': case '²': case '³':
2946 case '×': case '÷': case '¹': case 'ª':
2947 case 'º': case '¬': case 'µ':
2948 if (bparams.inputenc == "latin1" ||
2949 (bparams.inputenc == "auto" &&
2950 font.language()->encoding()->LatexName()
2952 os << "\\ensuremath{"
2961 case '|': case '<': case '>':
2962 // In T1 encoding, these characters exist
2963 if (lyxrc.fontenc == "T1") {
2965 //... but we should avoid ligatures
2966 if ((c == '>' || c == '<')
2968 && GetChar(i + 1) == c) {
2969 //os << "\\textcompwordmark{}";
2970 // Jean-Marc, have a look at
2971 // this. IÂ think this works
2979 // Typewriter font also has them
2980 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2984 // Otherwise, we use what LaTeX
2988 os << "\\textless{}";
2992 os << "\\textgreater{}";
2996 os << "\\textbar{}";
3002 case '-': // "--" in Typewriter mode -> "-{}-"
3004 && GetChar(i + 1) == '-'
3005 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3014 os << "\\char`\\\"{}";
3019 if (bparams.inputenc == "default") {
3028 case '%': case '#': case '{':
3035 os << "\\textasciitilde{}";
3040 os << "\\textasciicircum{}";
3044 case '*': case '[': case ']':
3045 // avoid being mistaken for optional arguments
3046 os << '{' << c << '}';
3051 // Blanks are printed before font switching.
3052 // Sure? I am not! (try nice-latex)
3053 // I am sure it's correct. LyX might be smarter
3054 // in the future, but for now, nothing wrong is
3059 /* idea for labels --- begin*/
3063 && font.family() != LyXFont::TYPEWRITER_FAMILY
3064 && GetChar(i + 1) == 'y'
3065 && GetChar(i + 2) == 'X') {
3073 && font.family() != LyXFont::TYPEWRITER_FAMILY
3074 && GetChar(i + 1) == 'e'
3075 && GetChar(i + 2) == 'X') {
3080 // Check for "LaTeX2e"
3083 && font.family() != LyXFont::TYPEWRITER_FAMILY
3084 && GetChar(i + 1) == 'a'
3085 && GetChar(i + 2) == 'T'
3086 && GetChar(i + 3) == 'e'
3087 && GetChar(i + 4) == 'X'
3088 && GetChar(i + 5) == '2'
3089 && GetChar(i + 6) == 'e') {
3094 // Check for "LaTeX"
3097 && font.family() != LyXFont::TYPEWRITER_FAMILY
3098 && GetChar(i + 1) == 'a'
3099 && GetChar(i + 2) == 'T'
3100 && GetChar(i + 3) == 'e'
3101 && GetChar(i + 4) == 'X') {
3105 /* idea for labels --- end*/
3106 } else if (c != '\0') {
3116 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3117 BufferParams const & bparams,
3118 ostream & os, TexRow & texrow
3121 TexRow & foot_texrow,
3126 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3127 LyXParagraph * par = this;
3130 (par->params.depth() == params.depth())
3132 && (par->footnoteflag == footnoteflag)
3137 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3139 if (textclasslist.Style(bparams.textclass,
3140 par->layout).isEnvironment()
3142 || par->params.pextraType() != PEXTRA_NONE
3145 par = par->TeXEnvironment(buf, bparams,
3153 par = par->TeXOnePar(buf, bparams,
3163 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3169 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3170 BufferParams const & bparams,
3171 ostream & os, TexRow & texrow
3174 TexRow & foot_texrow,
3180 bool eindent_open = false;
3183 bool foot_this_level = false;
3186 // flags when footnotetext should be appended to file.
3187 static bool minipage_open = false;
3188 static int minipage_open_depth = 0;
3189 char par_sep = bparams.paragraph_separation;
3192 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3195 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3198 LyXLayout const & style =
3199 textclasslist.Style(bparams.textclass,
3203 if (params.pextraType() == PEXTRA_INDENT) {
3204 if (!params.pextraWidth().empty()) {
3205 os << "\\begin{LyXParagraphIndent}{"
3206 << params.pextraWidth() << "}\n";
3208 //float ib = atof(pextra_widthp.c_str())/100;
3209 // string can't handle floats at present (971109)
3210 // so I'll do a conversion by hand knowing that
3211 // the limits are 0.0 to 1.0. ARRae.
3212 os << "\\begin{LyXParagraphIndent}{";
3213 switch (params.pextraWidthp().length()) {
3219 << params.pextraWidthp();
3223 << params.pextraWidthp();
3225 os << "\\columnwidth}\n";
3228 eindent_open = true;
3230 if ((params.pextraType() == PEXTRA_MINIPAGE) && !minipage_open) {
3231 if (params.pextraHfill() && previous() &&
3232 (previous()->params.pextraType() == PEXTRA_MINIPAGE)) {
3233 os << "\\hfill{}\n";
3236 if (par_sep == BufferParams::PARSEP_INDENT) {
3237 os << "{\\setlength\\parindent{0pt}\n";
3240 os << "\\begin{minipage}";
3241 switch (params.pextraAlignment()) {
3242 case MINIPAGE_ALIGN_TOP:
3245 case MINIPAGE_ALIGN_MIDDLE:
3248 case MINIPAGE_ALIGN_BOTTOM:
3252 if (!params.pextraWidth().empty()) {
3253 os << '{' << params.pextraWidth() << "}\n";
3255 //float ib = atof(par->pextra_width.c_str())/100;
3256 // string can't handle floats at present
3257 // so I'll do a conversion by hand knowing that
3258 // the limits are 0.0 to 1.0. ARRae.
3260 switch (params.pextraWidthp().length()) {
3266 << params.pextraWidthp();
3270 << params.pextraWidthp();
3272 os << "\\columnwidth}\n";
3275 if (par_sep == BufferParams::PARSEP_INDENT) {
3276 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3279 minipage_open = true;
3280 minipage_open_depth = params.depth();
3283 #ifdef WITH_WARNINGS
3284 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3285 //I disabled it because it breaks when lists span on several
3288 if (style.isEnvironment()){
3289 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3290 #ifdef FANCY_FOOTNOTE_CODE
3291 if (foot_count < 0) {
3292 // flag that footnote[mark][text] should be
3293 // used for any footnotes from now on
3295 foot_this_level = true;
3298 os << "\\begin{" << style.latexname() << "}{"
3299 << params.labelWidthString() << "}\n";
3300 } else if (style.labeltype == LABEL_BIBLIO) {
3302 os << "\\begin{" << style.latexname() << "}{"
3303 << bibitemWidest(buf)
3305 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3306 #ifdef FANCY_FOOTNOTE_CODE
3307 if (foot_count < 0) {
3308 // flag that footnote[mark][text] should be
3309 // used for any footnotes from now on
3311 foot_this_level = true;
3314 os << "\\begin{" << style.latexname() << '}'
3315 << style.latexparam() << '\n';
3317 os << "\\begin{" << style.latexname() << '}'
3318 << style.latexparam() << '\n';
3321 LyXParagraph * par = this;
3323 par = par->TeXOnePar(buf, bparams,
3327 foot, foot_texrow, foot_count
3331 if (minipage_open && par && !style.isEnvironment() &&
3332 (par->params.pextraType() == PEXTRA_MINIPAGE) &&
3333 par->params.pextraStartMinipage()) {
3334 os << "\\end{minipage}\n";
3336 if (par_sep == BufferParams::PARSEP_INDENT) {
3340 minipage_open = false;
3343 if (par && par->params.depth() > params.depth()) {
3344 if (textclasslist.Style(bparams.textclass,
3345 par->layout).isParagraph()
3347 // How to handle this? (Lgb)
3348 //&& !suffixIs(os, "\n\n")
3350 // There should be at least one '\n' already
3351 // but we need there to be two for Standard
3352 // paragraphs that are depth-increment'ed to be
3353 // output correctly. However, tables can
3354 // also be paragraphs so don't adjust them.
3357 // Will it ever harm to have one '\n' too
3358 // many? i.e. that we sometimes will have
3359 // three in a row. (Lgb)
3363 par = par->TeXDeeper(buf, bparams, os, texrow
3365 ,foot, foot_texrow, foot_count
3370 if (par && par->layout == layout && par->params.depth() == params.depth() &&
3371 (par->params.pextraType() == PEXTRA_MINIPAGE) && !minipage_open) {
3372 if (par->params.pextraHfill() && par->previous() &&
3373 (par->previous()->params.pextraType() == PEXTRA_MINIPAGE)){
3374 os << "\\hfill{}\n";
3377 if (par_sep == BufferParams::PARSEP_INDENT) {
3378 os << "{\\setlength\\parindent{0pt}\n";
3381 os << "\\begin{minipage}";
3382 switch (par->params.pextraAlignment()) {
3383 case MINIPAGE_ALIGN_TOP:
3386 case MINIPAGE_ALIGN_MIDDLE:
3389 case MINIPAGE_ALIGN_BOTTOM:
3393 if (!par->params.pextraWidth().empty()) {
3394 os << '{' << par->params.pextraWidth() << "}\n";
3396 //float ib = atof(par->pextra_widthp.c_str())/100;
3397 // string can't handle floats at present
3398 // so I'll do a conversion by hand knowing that
3399 // the limits are 0.0 to 1.0. ARRae.
3401 switch (par->params.pextraWidthp().length()) {
3406 os << "0." << par->params.pextraWidthp();
3409 os << "0.0" << par->params.pextraWidthp();
3411 os << "\\columnwidth}\n";
3414 if (par_sep == BufferParams::PARSEP_INDENT) {
3415 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3418 minipage_open = true;
3419 minipage_open_depth = par->params.depth();
3423 && par->layout == layout
3424 && par->params.depth() == params.depth()
3426 && par->params.pextraType() == params.pextraType()
3429 && par->footnoteflag == footnoteflag
3433 if (style.isEnvironment()) {
3434 os << "\\end{" << style.latexname() << "}\n";
3437 // maybe this should go after the minipage closes?
3438 if (foot_this_level) {
3439 if (foot_count >= 1) {
3440 if (foot_count > 1) {
3441 os << "\\addtocounter{footnote}{-"
3446 texrow += foot_texrow;
3448 foot_texrow.reset();
3455 if (minipage_open && (minipage_open_depth == params.depth()) &&
3456 (!par || par->params.pextraStartMinipage() ||
3457 par->params.pextraType() != PEXTRA_MINIPAGE)) {
3458 os << "\\end{minipage}\n";
3460 if (par_sep == BufferParams::PARSEP_INDENT) {
3464 if (par && par->params.pextraType() != PEXTRA_MINIPAGE) {
3465 os << "\\medskip\n\n";
3469 minipage_open = false;
3472 os << "\\end{LyXParagraphIndent}\n";
3475 if (!(par && (par->params.pextraType() == PEXTRA_MINIPAGE)
3476 && par->params.pextraHfill())) {
3481 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3482 return par; // ale970302
3487 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3488 BufferParams const & bparams,
3489 ostream & os, TexRow & texrow,
3490 ostream & foot, TexRow & foot_texrow,
3494 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3495 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3496 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3497 "No footnote!" << endl;
3499 LyXParagraph * par = this;
3500 LyXLayout const & style =
3501 textclasslist.Style(bparams.textclass,
3502 previous_->GetLayout());
3504 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3505 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3506 "Float other than footnote in command"
3507 " with moving argument is illegal" << endl;
3510 if (footnotekind != LyXParagraph::FOOTNOTE
3511 && footnotekind != LyXParagraph::MARGIN
3514 // How to solve this?
3515 //&& !suffixIs(file, '\n')
3517 // we need to ensure that real floats like tables and figures
3518 // have their \begin{} on a new line otherwise we can get
3519 // incorrect results when using the endfloat.sty package
3520 // especially if two floats follow one another. ARRae 981022
3521 // NOTE: if the file is length 0 it must have just been
3522 // written out so we assume it ended with a '\n'
3524 // As far as I can see there is never any harm in writing
3525 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3530 bool moving_arg = false;
3531 bool need_closing = false;
3532 bool is_rtl = isRightToLeftPar(bparams);
3534 if (is_rtl != parent_is_rtl) {
3539 need_closing = true;
3542 bool footer_in_body = true;
3543 switch (footnotekind) {
3544 case LyXParagraph::FOOTNOTE:
3545 if (style.intitle) {
3546 os << "\\thanks{\n";
3547 footer_in_body = false;
3550 if (foot_count == -1) {
3551 // we're at depth 0 so we can use:
3552 os << "\\footnote{%\n";
3553 footer_in_body = false;
3555 os << "\\footnotemark{}%\n";
3557 // we only need this when there are
3558 // multiple footnotes
3559 os << "\\stepcounter{footnote}";
3561 os << "\\footnotetext{%\n";
3562 foot_texrow.start(this, 0);
3563 foot_texrow.newline();
3568 case LyXParagraph::MARGIN:
3569 os << "\\marginpar{\n";
3571 case LyXParagraph::FIG:
3572 if (params.pextraType() == PEXTRA_FLOATFLT
3573 && (!params.pextraWidth().empty()
3574 || !params.pextraWidthp().empty())) {
3575 if (!params.pextraWidth().empty())
3576 os << "\\begin{floatingfigure}{"
3577 << params.pextraWidth() << "}\n";
3579 os << "\\begin{floatingfigure}{"
3580 << lyx::atoi(params.pextraWidthp()) / 100.0
3581 << "\\textwidth}\n";
3583 os << "\\begin{figure}";
3584 if (!bparams.float_placement.empty()) {
3585 os << '[' << bparams.float_placement << "]\n";
3591 case LyXParagraph::TAB:
3592 os << "\\begin{table}";
3593 if (!bparams.float_placement.empty()) {
3594 os << '[' << bparams.float_placement << "]\n";
3599 case LyXParagraph::WIDE_FIG:
3600 os << "\\begin{figure*}";
3601 if (!bparams.float_placement.empty()) {
3602 os << '[' << bparams.float_placement << "]\n";
3607 case LyXParagraph::WIDE_TAB:
3608 os << "\\begin{table*}";
3609 if (!bparams.float_placement.empty()) {
3610 os << '[' << bparams.float_placement << "]\n";
3615 case LyXParagraph::ALGORITHM:
3616 os << "\\begin{algorithm}\n";
3621 if (footnotekind != LyXParagraph::FOOTNOTE
3622 || !footer_in_body) {
3623 // Process text for all floats except footnotes in body
3625 LyXLayout const & style =
3627 .Style(bparams.textclass, par->layout);
3629 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3631 if (style.isEnvironment()
3632 || par->params.pextraType() == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3633 // Allows the use of minipages within float
3634 // environments. Shouldn't be circular because
3635 // we don't support footnotes inside
3636 // floats (yet). ARRae
3637 par = par->TeXEnvironment(buf, bparams, os,
3642 par = par->TeXOnePar(buf, bparams,
3643 os, texrow, moving_arg,
3648 if (par && !par->IsDummy() && par->params.depth() > params.depth()) {
3649 par = par->TeXDeeper(buf, bparams, os, texrow,
3653 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3655 // process footnotes > depth 0 or in environments separately
3656 // NOTE: Currently don't support footnotes within footnotes
3657 // even though that is possible using the \footnotemark
3658 std::ostringstream dummy;
3659 TexRow dummy_texrow;
3660 int dummy_count = 0;
3662 LyXLayout const & style =
3664 .Style(bparams.textclass, par->layout);
3666 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3668 if (style.isEnvironment()
3669 || par->params.pextraType() == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3670 // Allows the use of minipages within float
3671 // environments. Shouldn't be circular because
3672 // we don't support footnotes inside
3673 // floats (yet). ARRae
3674 par = par->TeXEnvironment(buf, bparams,
3676 dummy, dummy_texrow,
3679 par = par->TeXOnePar(buf, bparams,
3682 dummy, dummy_texrow,
3686 if (par && !par->IsDummy() && par->params.depth() > params.depth()) {
3687 par = par->TeXDeeper(buf, bparams,
3689 dummy, dummy_texrow,
3693 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3695 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3696 "Footnote in a Footnote -- not supported"
3701 switch (footnotekind) {
3702 case LyXParagraph::FOOTNOTE:
3703 if (footer_in_body) {
3704 // This helps tell which of the multiple
3705 // footnotetexts an error was in.
3707 foot_texrow.newline();
3712 case LyXParagraph::MARGIN:
3715 case LyXParagraph::FIG:
3716 if (params.pextraType() == PEXTRA_FLOATFLT
3717 && (!params.pextraWidth().empty()
3718 || !params.pextraWidthp().empty()))
3719 os << "\\end{floatingfigure}";
3721 os << "\\end{figure}";
3723 case LyXParagraph::TAB:
3724 os << "\\end{table}";
3726 case LyXParagraph::WIDE_FIG:
3727 os << "\\end{figure*}";
3729 case LyXParagraph::WIDE_TAB:
3730 os << "\\end{table*}";
3732 case LyXParagraph::ALGORITHM:
3733 os << "\\end{algorithm}";
3740 if (footnotekind != LyXParagraph::FOOTNOTE
3741 && footnotekind != LyXParagraph::MARGIN) {
3742 // we need to ensure that real floats like tables and figures
3743 // have their \end{} on a line of their own otherwise we can
3744 // get incorrect results when using the endfloat.sty package.
3749 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next_ << endl;
3754 bool LyXParagraph::IsDummy() const
3756 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous_
3757 && previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3762 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3763 int type, string const & width,
3764 string const & widthp)
3766 params.pextraType(type);
3767 params.pextraWidth(width);
3768 params.pextraWidthp(widthp);
3770 if (textclasslist.Style(bparams.textclass,
3771 layout).isEnvironment()) {
3772 LyXParagraph * par = this;
3773 LyXParagraph * ppar = par;
3775 while (par && (par->layout == layout)
3776 && (par->params.depth() == params.depth())) {
3778 par = par->previous();
3781 par = par->FirstPhysicalPar();
3783 while (par && par->params.depth() > params.depth()) {
3784 par = par->previous();
3787 par = par->FirstPhysicalPar();
3792 while (par && (par->layout == layout)
3793 && (par->params.depth() == params.depth())) {
3794 par->params.pextraType(type);
3795 par->params.pextraWidth(width);
3796 par->params.pextraWidthp(widthp);
3798 par = par->NextAfterFootnote();
3802 if (par && (par->params.depth() > params.depth()))
3803 par->SetPExtraType(bparams,
3804 type, width, widthp);
3806 while (par && ((par->params.depth() > params.depth()) || par->IsDummy()))
3807 par = par->NextAfterFootnote();
3809 while (par && par->params.depth() > params.depth())
3817 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3819 if (params.pextraType() == PEXTRA_NONE)
3822 params.pextraType(PEXTRA_NONE);
3823 params.pextraWidth(string());
3824 params.pextraWidthp(string());
3826 if (textclasslist.Style(bparams.textclass,
3827 layout).isEnvironment()) {
3828 LyXParagraph * par = this;
3829 LyXParagraph * ppar = par;
3831 while (par && (par->layout == layout)
3832 && (par->params.depth() == params.depth())) {
3834 par = par->previous();
3837 par = par->FirstPhysicalPar();
3839 while (par && par->params.depth() > params.depth()) {
3840 par = par->previous();
3843 par = par->FirstPhysicalPar();
3848 while (par && (par->layout == layout)
3849 && (par->params.depth() == params.depth())) {
3850 par->params.pextraType(PEXTRA_NONE);
3851 par->params.pextraWidth(string());
3852 par->params.pextraWidthp(string());
3854 par = par->NextAfterFootnote();
3858 if (par && (par->params.depth() > params.depth()))
3859 par->UnsetPExtraType(bparams);
3861 while (par && ((par->params.depth() > params.depth()) || par->IsDummy()))
3862 par = par->NextAfterFootnote();
3864 while (par && par->params.depth() > params.depth())
3873 bool LyXParagraph::IsHfill(size_type pos) const
3875 return IsHfillChar(GetChar(pos));
3879 bool LyXParagraph::IsInset(size_type pos) const
3881 return IsInsetChar(GetChar(pos));
3886 bool LyXParagraph::IsFloat(size_type pos) const
3888 return IsFloatChar(GetChar(pos));
3893 bool LyXParagraph::IsNewline(size_type pos) const
3895 return pos >= 0 && IsNewlineChar(GetChar(pos));
3899 bool LyXParagraph::IsSeparator(size_type pos) const
3901 return IsSeparatorChar(GetChar(pos));
3905 bool LyXParagraph::IsLineSeparator(size_type pos) const
3907 return IsLineSeparatorChar(GetChar(pos));
3911 bool LyXParagraph::IsKomma(size_type pos) const
3913 return IsKommaChar(GetChar(pos));
3917 /// Used by the spellchecker
3918 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3920 value_type const c = GetChar(pos);
3921 if (IsLetterChar(c))
3923 // '\0' is not a letter, allthough every string contains "" (below)
3926 // We want to pass the ' and escape chars to ispell
3927 string const extra = lyxrc.isp_esc_chars + '\'';
3928 char ch[2] = { c, 0 };
3929 return contains(extra, ch);
3933 bool LyXParagraph::IsWord(size_type pos ) const
3935 return IsWordChar(GetChar(pos)) ;
3940 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3944 return FirstPhysicalPar()->getParLanguage(bparams);
3948 return GetFirstFontSettings().language();
3950 return previous_->getParLanguage(bparams);
3952 return bparams.language;
3956 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3958 return lyxrc.rtl_support
3959 && getParLanguage(bparams)->RightToLeft();
3963 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3964 Language const * from, Language const * to)
3966 for (size_type i = 0; i < size(); ++i) {
3967 LyXFont font = GetFontSettings(bparams, i);
3968 if (font.language() == from) {
3969 font.setLanguage(to);
3976 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3978 Language const * doc_language = bparams.language;
3979 for (FontList::const_iterator cit = fontlist.begin();
3980 cit != fontlist.end(); ++cit)
3981 if ((*cit).font().language() != doc_language)
3987 // Convert the paragraph to a string.
3988 // Used for building the table of contents
3989 string const LyXParagraph::String(Buffer const * buffer, bool label)
3991 BufferParams const & bparams = buffer->params;
3994 if (label && !IsDummy() && !params.labelString().empty())
3996 if (label && !params.labelString().empty())
3998 s += params.labelString() + ' ';
3999 string::size_type const len = s.size();
4001 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
4002 value_type c = GetChar(i);
4005 else if (c == META_INSET &&
4006 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
4007 std::ostringstream ost;
4008 GetInset(i)->Ascii(buffer, ost);
4009 s += subst(ost.str().c_str(),'\n',' ');
4014 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
4015 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
4016 s += NextAfterFootnote()->String(buffer, false);
4020 if (isRightToLeftPar(bparams))
4021 reverse(s.begin() + len,s.end());
4029 string const LyXParagraph::String(Buffer const * buffer,
4030 LyXParagraph::size_type beg,
4031 LyXParagraph::size_type end)
4036 if (beg == 0 && !IsDummy() && !params.labelString().empty())
4038 if (beg == 0 && !params.labelString().empty())
4040 s += params.labelString() + ' ';
4042 for (LyXParagraph::size_type i = beg; i < end; ++i) {
4043 value_type c = GetUChar(buffer->params, i);
4046 else if (c == META_INSET) {
4047 std::ostringstream ost;
4048 GetInset(i)->Ascii(buffer, ost);
4049 s += ost.str().c_str();
4057 void LyXParagraph::SetInsetOwner(Inset * i)
4060 for (InsetList::const_iterator cit = insetlist.begin();
4061 cit != insetlist.end(); ++cit) {
4063 (*cit).inset->setOwner(i);
4068 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4071 for (InsetList::const_iterator cit = insetlist.begin();
4072 cit != insetlist.end(); ++cit) {
4074 if ((*cit).inset->IsTextInset()) {
4075 static_cast<UpdatableInset *>
4076 ((*cit).inset)->deleteLyXText(bv);
4083 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4086 for (InsetList::const_iterator cit = insetlist.begin();
4087 cit != insetlist.end(); ++cit) {
4089 if ((*cit).inset->IsTextInset()) {
4090 static_cast<UpdatableInset *>
4091 ((*cit).inset)->resizeLyXText(bv);
4098 void LyXParagraph::fitToSize()
4100 TextContainer tmp(text.begin(), text.end());
4105 void LyXParagraph::setContentsFromPar(LyXParagraph * par)