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"
26 #include "tex-strings.h"
28 #include "bufferparams.h"
29 #include "support/FileInfo.h"
30 #include "support/LAssert.h"
32 #include "LaTeXFeatures.h"
33 #include "insets/insetinclude.h"
34 #include "insets/insetbib.h"
35 #include "insets/insettext.h"
36 #include "support/filetools.h"
37 #include "lyx_gui_misc.h"
39 #include "support/lyxmanip.h"
40 #include "BufferView.h"
42 #include "ParameterStruct.h"
49 using std::lower_bound;
50 using std::upper_bound;
53 int tex_code_break_column = 72; // needs non-zero initialization. set later.
54 // this is a bad idea, but how can LyXParagraph find its buffer to get
55 // parameters? (JMarc)
57 extern string bibitemWidest(Buffer const *);
59 // this is a minibuffer
64 LyXFont minibuffer_font;
65 Inset * minibuffer_inset;
70 extern BufferView * current_view;
72 // Initialization of the counter for the paragraph id's,
73 // declared in lyxparagraph.h
74 unsigned int LyXParagraph::paragraph_id = 0;
76 // Initialize static member.
77 ShareContainer<LyXFont> LyXParagraph::FontTable::container;
80 LyXParagraph::LyXParagraph()
82 text.reserve(500); // is this number too big?
83 for (int i = 0; i < 10; ++i) setCounter(i , 0);
89 footnoteflag = LyXParagraph::NO_FOOTNOTE;
90 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
95 bibkey = 0; // ale970302
100 // This konstruktor inserts the new paragraph in a list.
101 LyXParagraph::LyXParagraph(LyXParagraph * par)
106 for (int i = 0; i < 10; ++i) setCounter(i, 0);
109 // double linked list begin
112 next_->previous_ = this;
114 previous_->next_ = this;
117 footnoteflag = LyXParagraph::NO_FOOTNOTE;
118 footnotekind = LyXParagraph::FOOTNOTE;
121 id_ = paragraph_id++;
123 bibkey = 0; // ale970302
129 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
130 BufferParams const & bparams,
131 char footflag, char dth) const
134 if (footnoteflag != LyXParagraph::NO_FOOTNOTE ||
136 || previous_->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
137 // The beginning or the end of a footnote environment?
138 if (footflag != footnoteflag) {
139 footflag = footnoteflag;
141 os << "\n\\begin_float "
142 << string_footnotekinds[footnotekind]
145 os << "\n\\end_float ";
149 // The beginning or end of a deeper (i.e. nested) area?
150 if (dth != params.depth()) {
151 if (params.depth() > dth) {
152 while (params.depth() > dth) {
153 os << "\n\\begin_deeper ";
157 while (params.depth() < dth) {
158 os << "\n\\end_deeper ";
164 // First write the layout
166 << textclasslist.NameOfLayout(bparams.textclass, layout)
169 // Maybe some vertical spaces.
170 if (params.spaceTop().kind() != VSpace::NONE)
171 os << "\\added_space_top "
172 << params.spaceTop().asLyXCommand() << " ";
173 if (params.spaceBottom().kind() != VSpace::NONE)
174 os << "\\added_space_bottom "
175 << params.spaceBottom().asLyXCommand() << " ";
177 // Maybe the paragraph has special spacing
178 params.spacing().writeFile(os, true);
180 // The labelwidth string used in lists.
181 if (!params.labelWidthString().empty())
182 os << "\\labelwidthstring "
183 << params.labelWidthString() << '\n';
185 // Lines above or below?
186 if (params.lineTop())
188 if (params.lineBottom())
189 os << "\\line_bottom ";
191 // Pagebreaks above or below?
192 if (params.pagebreakTop())
193 os << "\\pagebreak_top ";
194 if (params.pagebreakBottom())
195 os << "\\pagebreak_bottom ";
197 // Start of appendix?
198 if (params.startOfAppendix())
199 os << "\\start_of_appendix ";
202 if (params.noindent())
206 if (params.align() != LYX_ALIGN_LAYOUT) {
208 switch (params.align()) {
209 case LYX_ALIGN_LEFT: h = 1; break;
210 case LYX_ALIGN_RIGHT: h = 2; break;
211 case LYX_ALIGN_CENTER: h = 3; break;
212 default: h = 0; break;
214 os << "\\align " << string_align[h] << " ";
217 if (params.pextraType() != PEXTRA_NONE) {
218 os << "\\pextra_type " << params.pextraType();
219 if (params.pextraType() == PEXTRA_MINIPAGE) {
220 os << " \\pextra_alignment "
221 << params.pextraAlignment();
222 if (params.pextraHfill())
223 os << " \\pextra_hfill "
224 << params.pextraHfill();
225 if (params.pextraStartMinipage())
226 os << " \\pextra_start_minipage "
227 << params.pextraStartMinipage();
229 if (!params.pextraWidth().empty()) {
230 os << " \\pextra_width "
231 << VSpace(params.pextraWidth()).asLyXCommand();
232 } else if (!params.pextraWidthp().empty()) {
233 os << " \\pextra_widthp "
234 << params.pextraWidthp();
241 // Dummy layout. This means that a footnote ended.
242 os << "\n\\end_float ";
243 footflag = LyXParagraph::NO_FOOTNOTE;
249 bibkey->Write(buf, os);
251 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
254 for (size_type i = 0; i < size(); ++i) {
260 // Write font changes
261 LyXFont font2 = GetFontSettings(bparams, i);
262 if (font2 != font1) {
263 font2.lyxWriteChanges(font1, os);
268 value_type const c = GetChar(i);
272 Inset const * inset = GetInset(i);
274 if (inset->DirectWrite()) {
275 // international char, let it write
276 // code directly so it's shorter in
278 inset->Write(buf, os);
280 os << "\n\\begin_inset ";
281 inset->Write(buf, os);
282 os << "\n\\end_inset \n\n";
288 os << "\n\\newline \n";
292 os << "\n\\hfill \n";
296 os << "\n\\backslash \n";
300 if (i + 1 < size() && GetChar(i + 1) == ' ') {
307 if ((column > 70 && c == ' ')
312 // this check is to amend a bug. LyX sometimes
313 // inserts '\0' this could cause problems.
317 lyxerr << "ERROR (LyXParagraph::writeFile):"
318 " NULL char in structure." << endl;
324 // now write the next paragraph
326 next_->writeFile(buf, os, bparams, footflag, dth);
330 void LyXParagraph::validate(LaTeXFeatures & features) const
332 BufferParams const & bparams = features.bufferParams();
335 // this will be useful later
336 LyXLayout const & layout =
337 textclasslist.Style(bparams.textclass,
342 if (params.lineTop() || params.lineBottom())
343 features.lyxline = true;
344 if (!params.spacing().isDefault())
345 features.setspace = true;
348 features.layout[GetLayout()] = true;
351 Language const * doc_language = bparams.language;
353 for (FontList::const_iterator cit = fontlist.begin();
354 cit != fontlist.end(); ++cit) {
355 if ((*cit).font().noun() == LyXFont::ON) {
356 lyxerr[Debug::LATEX] << "font.noun: "
357 << (*cit).font().noun()
359 features.noun = true;
360 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
361 << (*cit).font().stateText(0)
364 switch ((*cit).font().color()) {
366 case LColor::inherit:
370 features.color = true;
371 lyxerr[Debug::LATEX] << "Color enabled. Font: "
372 << (*cit).font().stateText(0)
376 Language const * language = (*cit).font().language();
377 if (language->babel() != doc_language->babel()) {
378 features.UsedLanguages.insert(language);
379 lyxerr[Debug::LATEX] << "Found language "
380 << language->babel() << endl;
385 for (InsetList::const_iterator cit = insetlist.begin();
386 cit != insetlist.end(); ++cit) {
388 (*cit).inset->Validate(features);
392 if (params.pextraType() == PEXTRA_INDENT)
393 features.LyXParagraphIndent = true;
394 if (params.pextraType() == PEXTRA_FLOATFLT)
395 features.floatflt = true;
398 if (layout.needprotect
399 && next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
400 features.NeedLyXFootnoteCode = true;
403 if (bparams.paragraph_separation == BufferParams::PARSEP_INDENT
404 && params.pextraType() == LyXParagraph::PEXTRA_MINIPAGE)
405 features.NeedLyXMinipageIndent = true;
408 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
409 features.algorithm = true;
414 // First few functions needed for cut and paste and paragraph breaking.
415 void LyXParagraph::CopyIntoMinibuffer(Buffer const & buffer,
416 LyXParagraph::size_type pos) const
418 BufferParams bparams = buffer.params;
420 minibuffer_char = GetChar(pos);
421 minibuffer_font = GetFontSettings(bparams, pos);
422 minibuffer_inset = 0;
423 if (minibuffer_char == LyXParagraph::META_INSET) {
425 minibuffer_inset = GetInset(pos)->Clone(buffer);
427 minibuffer_inset = 0;
428 minibuffer_char = ' ';
429 // This reflects what GetInset() does (ARRae)
435 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
436 LyXParagraph::size_type pos)
438 minibuffer_char = GetChar(pos);
439 minibuffer_font = GetFontSettings(bparams, pos);
440 minibuffer_inset = 0;
441 if (minibuffer_char == LyXParagraph::META_INSET) {
443 minibuffer_inset = GetInset(pos);
444 // This is a little hack since I want exactly
445 // the inset, not just a clone. Otherwise
446 // the inset would be deleted when calling Erase(pos)
448 InsetTable search_elem(pos, 0);
449 InsetList::iterator it =
450 lower_bound(insetlist.begin(),
452 search_elem, matchIT());
453 if (it != insetlist.end() && (*it).pos == pos)
456 minibuffer_inset = 0;
457 minibuffer_char = ' ';
458 // This reflects what GetInset() does (ARRae)
463 // Erase(pos); now the caller is responsible for that.
467 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
469 if ((minibuffer_char == LyXParagraph::META_INSET) &&
470 !InsertInsetAllowed(minibuffer_inset))
472 if (minibuffer_char == LyXParagraph::META_INSET)
473 InsertInset(pos, minibuffer_inset, minibuffer_font);
475 InsertChar(pos, minibuffer_char, minibuffer_font);
483 void LyXParagraph::Clear()
492 // the destructor removes the new paragraph from the list
493 LyXParagraph::~LyXParagraph()
496 previous_->next_ = next_;
498 next_->previous_ = previous_;
500 for (InsetList::iterator it = insetlist.begin();
501 it != insetlist.end(); ++it) {
508 //lyxerr << "LyXParagraph::paragraph_id = "
509 // << LyXParagraph::paragraph_id << endl;
513 void LyXParagraph::Erase(LyXParagraph::size_type pos)
516 // > because last is the next unused position, and you can
517 // use it if you want
519 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
520 NextAfterFootnote()->Erase(pos - text.size() - 1);
522 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
523 "position does not exist." << endl;
527 Assert(pos < size());
530 if (pos < size()) { // last is free for insertation, but should be empty
532 // if it is an inset, delete the inset entry
533 if (text[pos] == LyXParagraph::META_INSET) {
535 InsetTable search_inset(pos, 0);
536 InsetList::iterator it =
537 lower_bound(insetlist.begin(),
539 search_inset, matchIT());
540 if (it != insetlist.end() && (*it).pos == pos) {
546 text.erase(text.begin() + pos);
548 // Erase entries in the tables.
549 FontTable search_font(pos, LyXFont());
551 FontList::iterator it =
552 lower_bound(fontlist.begin(),
554 search_font, matchFT());
555 if (it != fontlist.end() && (*it).pos() == pos &&
557 (it != fontlist.begin() && (*(it - 1)).pos() == pos - 1))) {
558 // If it is a multi-character font
559 // entry, we just make it smaller
560 // (see update below), otherwise we
562 unsigned int const i = it - fontlist.begin();
563 fontlist.erase(fontlist.begin() + i);
564 it = fontlist.begin() + i;
565 if (i > 0 && i < fontlist.size() &&
566 fontlist[i - 1].font() == fontlist[i].font()) {
567 fontlist.erase(fontlist.begin() + i - 1);
568 it = fontlist.begin() + i - 1;
572 // Update all other entries.
573 FontList::iterator fend = fontlist.end();
574 for (; it != fend; ++it)
575 (*it).pos((*it).pos() - 1);
577 // Update the inset table.
578 InsetTable search_inset(pos, 0);
579 InsetList::iterator lend = insetlist.end();
580 for (InsetList::iterator it =
581 upper_bound(insetlist.begin(),
583 search_inset, matchIT());
588 lyxerr << "ERROR (LyXParagraph::Erase): "
589 "can't erase non-existant char." << endl;
595 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
596 LyXParagraph::value_type c)
598 LyXFont const f(LyXFont::ALL_INHERIT);
599 InsertChar(pos, c, f);
603 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
604 LyXParagraph::value_type c,
605 LyXFont const & font)
608 // > because last is the next unused position, and you can
609 // use it if you want
612 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
613 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
616 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
617 "position does not exist." << endl;
621 Assert(pos <= size());
623 text.insert(text.begin() + pos, c);
625 // Update the font table.
626 FontTable search_font(pos, LyXFont());
627 for (FontList::iterator it = lower_bound(fontlist.begin(),
629 search_font, matchFT());
630 it != fontlist.end(); ++it)
631 (*it).pos((*it).pos() + 1);
633 // Update the inset table.
634 InsetTable search_inset(pos, 0);
635 for (InsetList::iterator it = lower_bound(insetlist.begin(),
637 search_inset, matchIT());
638 it != insetlist.end(); ++it)
645 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
648 LyXFont const f(LyXFont::ALL_INHERIT);
649 InsertInset(pos, inset, f);
653 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
654 Inset * inset, LyXFont const & font)
659 // > because last is the next unused position, and you can
660 // use it if you want
663 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
665 ->InsertInset(pos - text.size() - 1,
668 lyxerr << "ERROR (LyXParagraph::InsertInset): "
669 "position does not exist: " << pos << endl;
673 Assert(pos <= size());
676 InsertChar(pos, META_INSET, font);
677 Assert(text[pos] == META_INSET);
679 // Add a new entry in the inset table.
680 InsetTable search_inset(pos, 0);
681 InsetList::iterator it = lower_bound(insetlist.begin(),
683 search_inset, matchIT());
684 if (it != insetlist.end() && (*it).pos == pos)
685 lyxerr << "ERROR (LyXParagraph::InsertInset): "
686 "there is an inset in position: " << pos << endl;
688 insetlist.insert(it, InsetTable(pos, inset));
690 inset->setOwner(inset_owner);
694 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
696 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
699 return inset_owner->InsertInsetAllowed(inset);
704 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
709 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
710 return NextAfterFootnote()
711 ->GetInset(pos - text.size() - 1);
713 lyxerr << "ERROR (LyXParagraph::GetInset): "
714 "position does not exist: "
720 Assert(pos < size());
723 InsetTable search_inset(pos, 0);
724 InsetList::iterator it = lower_bound(insetlist.begin(),
726 search_inset, matchIT());
727 if (it != insetlist.end() && (*it).pos == pos)
730 lyxerr << "ERROR (LyXParagraph::GetInset): "
731 "Inset does not exist: " << pos << endl;
734 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
735 // Did this commenting out introduce a bug? So far I have not
736 // see any, please enlighten me. (Lgb)
737 // My guess is that since the inset does not exist, we might
738 // as well replace it with a space to prevent craches. (Asger)
743 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
748 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
749 return NextAfterFootnote()
750 ->GetInset(pos - text.size() - 1);
752 lyxerr << "ERROR (LyXParagraph::GetInset): "
753 "position does not exist: "
759 Assert(pos < size());
762 InsetTable search_inset(pos, 0);
763 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
765 search_inset, matchIT());
766 if (cit != insetlist.end() && (*cit).pos == pos)
769 lyxerr << "ERROR (LyXParagraph::GetInset): "
770 "Inset does not exist: " << pos << endl;
772 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
773 // Did this commenting out introduce a bug? So far I have not
774 // see any, please enlighten me. (Lgb)
775 // My guess is that since the inset does not exist, we might
776 // as well replace it with a space to prevent craches. (Asger)
781 // Gets uninstantiated font setting at position.
782 // Optimized after profiling. (Asger)
784 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
785 LyXParagraph::size_type pos) const
788 FontTable search_font(pos, LyXFont());
789 FontList::const_iterator cit = lower_bound(fontlist.begin(),
791 search_font, matchFT());
792 if (cit != fontlist.end())
793 return (*cit).font();
795 // > because last is the next unused position, and you can
796 // use it if you want
797 else if (pos > size()) {
799 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
800 return NextAfterFootnote()
801 ->GetFontSettings(bparams,
802 pos - text.size() - 1);
804 // Why is it an error to ask for the font of a
805 // position that does not exist? Would it be
806 // enough for this to be enabled on debug?
807 // We want strict error checking, but it's ok to only
808 // have it when debugging. (Asger)
809 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
810 "position does not exist. "
811 << pos << " (" << static_cast<int>(pos)
813 } else if (pos > 0) {
814 return GetFontSettings(bparams, pos - 1);
817 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
820 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
821 LyXParagraph::size_type pos) const
823 Assert(pos <= size());
824 FontTable search_font(pos, LyXFont());
825 FontList::const_iterator cit = lower_bound(fontlist.begin(),
827 search_font, matchFT());
828 if (cit != fontlist.end())
829 return (*cit).font();
831 if (pos == size() && size())
832 return GetFontSettings(bparams, pos - 1);
834 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
839 // Gets uninstantiated font setting at position 0
840 LyXFont const LyXParagraph::GetFirstFontSettings() const
843 if (!fontlist.empty())
844 return fontlist[0].font();
848 else if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
849 return NextAfterFootnote()->GetFirstFontSettings();
851 return LyXFont(LyXFont::ALL_INHERIT);
855 // Gets the fully instantiated font at a given position in a paragraph
856 // This is basically the same function as LyXText::GetFont() in text2.C.
857 // The difference is that this one is used for generating the LaTeX file,
858 // and thus cosmetic "improvements" are disallowed: This has to deliver
859 // the true picture of the buffer. (Asger)
860 // If position is -1, we get the layout font of the paragraph.
861 // If position is -2, we get the font of the manual label of the paragraph.
862 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
863 LyXParagraph::size_type pos) const
866 LyXLayout const & layout =
867 textclasslist.Style(bparams.textclass,
869 LyXParagraph::size_type main_body = 0;
870 if (layout.labeltype == LABEL_MANUAL)
871 main_body = BeginningOfMainBody();
876 layoutfont = layout.labelfont;
878 layoutfont = layout.font;
879 tmpfont = GetFontSettings(bparams, pos);
880 tmpfont.realize(layoutfont);
882 // process layoutfont for pos == -1 and labelfont for pos < -1
884 tmpfont = layout.font;
886 tmpfont = layout.labelfont;
887 tmpfont.setLanguage(getParLanguage(bparams));
890 // check for environment font information
891 char par_depth = GetDepth();
892 LyXParagraph const * par = this;
893 while (par && par_depth && !tmpfont.resolved()) {
894 par = par->DepthHook(par_depth - 1);
896 tmpfont.realize(textclasslist.
897 Style(bparams.textclass,
898 par->GetLayout()).font);
899 par_depth = par->GetDepth();
903 tmpfont.realize(textclasslist
904 .TextClass(bparams.textclass)
910 /// Returns the height of the highest font in range
912 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
913 LyXParagraph::size_type endpos) const
915 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
916 if (fontlist.empty())
919 FontTable end_search(endpos, LyXFont());
920 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
922 end_search, matchFT());
923 if (end_it != fontlist.end())
926 FontTable start_search(startpos, LyXFont());
927 for (FontList::const_iterator cit =
928 lower_bound(fontlist.begin(),
930 start_search, matchFT());
931 cit != end_it; ++cit) {
932 LyXFont::FONT_SIZE size = (*cit).font().size();
933 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
941 LyXParagraph::value_type
942 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
950 // > because last is the next unused position, and you can
951 // use it if you want
952 else if (pos > size()) {
953 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
954 return NextAfterFootnote()
955 ->GetChar(pos - text.size() - 1);
957 lyxerr << "ERROR (LyXParagraph::GetChar const): "
958 "position does not exist."
959 << pos << " (" << static_cast<int>(pos)
965 // We should have a footnote environment.
966 if (!next_ || next_->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
967 // Notice that LyX does request the
968 // last char from time to time. (Asger)
969 //lyxerr << "ERROR (LyXParagraph::GetChar): "
970 // "expected footnote." << endl;
973 switch (next_->footnotekind) {
974 case LyXParagraph::FOOTNOTE:
975 return LyXParagraph::META_FOOTNOTE;
976 case LyXParagraph::MARGIN:
977 return LyXParagraph::META_MARGIN;
978 case LyXParagraph::FIG:
979 case LyXParagraph::WIDE_FIG:
980 return LyXParagraph::META_FIG;
981 case LyXParagraph::TAB:
982 case LyXParagraph::WIDE_TAB:
983 return LyXParagraph::META_TAB;
984 case LyXParagraph::ALGORITHM:
985 return LyXParagraph::META_ALGORITHM;
987 return '\0'; // to shut up gcc
991 LyXParagraph::value_type
992 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
994 Assert(pos <= size());
995 if (!size() || pos == size()) return '\0';
1002 LyXParagraph::value_type
1003 LyXParagraph::GetUChar(BufferParams const & bparams,
1004 LyXParagraph::size_type pos) const
1006 value_type c = GetChar(pos);
1007 if (!lyxrc.rtl_support)
1037 if (uc != c && GetFontSettings(bparams, pos).isRightToLeft())
1044 // return an string of the current word, and the end of the word in lastpos.
1045 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1047 Assert(lastpos >= 0);
1049 // the current word is defined as starting at the first character
1050 // from the immediate left of lastpospos which meets the definition
1051 // of IsLetter(), continuing to the last character to the right
1052 // of this meeting IsLetter.
1058 // move back until we have a letter
1060 //there's no real reason to have firstpos & lastpos as
1061 //separate variables as this is written, but maybe someon
1062 // will want to return firstpos in the future.
1064 //since someone might have typed a punctuation first
1065 int firstpos = lastpos;
1067 while ((firstpos >= 0) && !IsLetter(firstpos))
1070 // now find the beginning by looking for a nonletter
1072 while ((firstpos>= 0) && IsLetter(firstpos))
1075 // the above is now pointing to the preceeding non-letter
1079 // so copy characters into theword until we get a nonletter
1080 // note that this can easily exceed lastpos, wich means
1081 // that if used in the middle of a word, the whole word
1084 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1091 LyXParagraph::size_type LyXParagraph::Last() const
1093 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1094 return text.size() + NextAfterFootnote()->Last() + 1;
1095 // the 1 is the symbol
1104 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1106 // > because last is the next unused position, and you can
1107 // use it if you want
1110 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1111 return NextAfterFootnote()
1112 ->ParFromPos(pos - text.size() - 1);
1114 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1115 "position does not exist." << endl;
1124 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1126 // > because last is the next unused position, and you can
1127 // use it if you want
1130 && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1131 return NextAfterFootnote()
1132 ->PositionInParFromPos(pos - text.size() - 1);
1135 "ERROR (LyXParagraph::PositionInParFromPos): "
1136 "position does not exist." << endl;
1145 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1146 LyXFont const & font)
1149 // > because last is the next unused position, and you can
1150 // use it if you want
1153 next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1154 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1157 lyxerr << "ERROR (LyXParagraph::SetFont): "
1158 "position does not exist." << endl;
1163 Assert(pos <= size());
1166 // First, reduce font against layout/label font
1167 // Update: The SetCharFont() routine in text2.C already
1168 // reduces font, so we don't need to do that here. (Asger)
1169 // No need to simplify this because it will disappear
1170 // in a new kernel. (Asger)
1171 // Next search font table
1173 FontTable search_font(pos, LyXFont());
1174 FontList::iterator it = lower_bound(fontlist.begin(),
1176 search_font, matchFT());
1177 unsigned int i = it - fontlist.begin();
1178 bool notfound = it == fontlist.end();
1180 if (!notfound && fontlist[i].font() == font)
1183 bool begin = pos == 0 || notfound ||
1184 (i > 0 && fontlist[i-1].pos() == pos - 1);
1185 // Is position pos is a beginning of a font block?
1186 bool end = !notfound && fontlist[i].pos() == pos;
1187 // Is position pos is the end of a font block?
1188 if (begin && end) { // A single char block
1189 if (i + 1 < fontlist.size() &&
1190 fontlist[i + 1].font() == font) {
1191 // Merge the singleton block with the next block
1192 fontlist.erase(fontlist.begin() + i);
1193 if (i > 0 && fontlist[i - 1].font() == font)
1194 fontlist.erase(fontlist.begin() + i-1);
1195 } else if (i > 0 && fontlist[i - 1].font() == font) {
1196 // Merge the singleton block with the previous block
1197 fontlist[i - 1].pos(pos);
1198 fontlist.erase(fontlist.begin() + i);
1200 fontlist[i].font(font);
1202 if (i > 0 && fontlist[i - 1].font() == font)
1203 fontlist[i - 1].pos(pos);
1205 fontlist.insert(fontlist.begin() + i,
1206 FontTable(pos, font));
1208 fontlist[i].pos(pos - 1);
1209 if (!(i + 1 < fontlist.size() &&
1210 fontlist[i + 1].font() == font))
1211 fontlist.insert(fontlist.begin() + i + 1,
1212 FontTable(pos, font));
1213 } else { // The general case. The block is splitted into 3 blocks
1214 fontlist.insert(fontlist.begin() + i,
1215 FontTable(pos - 1, fontlist[i].font()));
1216 fontlist.insert(fontlist.begin() + i + 1,
1217 FontTable(pos, font));
1223 void LyXParagraph::next(LyXParagraph * p)
1229 // This function is able to hide closed footnotes.
1230 LyXParagraph * LyXParagraph::next()
1233 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1234 LyXParagraph * tmp = next_;
1236 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1238 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1239 return tmp->next(); /* there can be more than one
1240 footnote in a logical
1243 return next_; // This should never happen!
1250 LyXParagraph const * LyXParagraph::next() const
1253 if (next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1254 LyXParagraph * tmp = next_;
1256 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1258 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1259 return tmp->next(); /* there can be more than one
1260 footnote in a logical
1263 return next_; // This should never happen!
1271 LyXParagraph * LyXParagraph::NextAfterFootnote()
1273 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1274 LyXParagraph * tmp = next_;
1275 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1277 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1278 return tmp; /* there can be more than one footnote
1279 in a logical paragraph */
1281 return next_; // This should never happen!
1289 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1291 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1292 LyXParagraph * tmp = next_;
1293 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1295 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1296 return tmp; /* there can be more than one footnote
1297 in a logical paragraph */
1299 return next_; // This should never happen!
1307 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1310 if (previous_ && previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1312 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1313 tmp = tmp->previous_;
1314 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1315 return tmp; /* there can be more than one footnote
1316 in a logical paragraph */
1318 return previous_; // This should never happen!
1326 LyXParagraph * LyXParagraph::LastPhysicalPar()
1328 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1331 LyXParagraph * tmp = this;
1333 && tmp->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1334 tmp = tmp->NextAfterFootnote();
1342 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1344 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1347 LyXParagraph const * tmp = this;
1349 && tmp->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1350 tmp = tmp->NextAfterFootnote();
1358 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1362 LyXParagraph * tmppar = this;
1366 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1367 tmppar = tmppar->previous_;
1378 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1382 LyXParagraph const * tmppar = this;
1386 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1387 tmppar = tmppar->previous_;
1397 void LyXParagraph::previous(LyXParagraph * p)
1403 // This function is able to hide closed footnotes.
1404 LyXParagraph * LyXParagraph::previous()
1407 LyXParagraph * tmp = previous_;
1412 && tmp->previous_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1413 tmp = tmp->previous_;
1415 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1416 tmp = tmp->previous_;
1417 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1418 return tmp->next_->previous();
1428 // This function is able to hide closed footnotes.
1429 LyXParagraph const * LyXParagraph::previous() const
1432 LyXParagraph * tmp = previous_;
1436 && tmp->previous_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1437 tmp = tmp->previous_;
1439 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1440 tmp = tmp->previous_;
1441 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1442 return tmp->next_->previous();
1453 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1454 LyXParagraph::size_type pos,
1459 // create a new paragraph
1461 size_type pos_first;
1462 LyXParagraph * par = ParFromPos(pos);
1463 LyXParagraph * firstpar = FirstPhysicalPar();
1465 LyXParagraph * tmp = new LyXParagraph(par);
1467 tmp->footnoteflag = footnoteflag;
1468 tmp->footnotekind = footnotekind;
1470 // this is an idea for a more userfriendly layout handling, I will
1471 // see what the users say
1473 // layout stays the same with latex-environments
1475 tmp->SetOnlyLayout(bparams, firstpar->layout);
1476 tmp->SetLabelWidthString(firstpar->params.labelWidthString());
1479 if (Last() > pos || !Last() || flag == 2) {
1480 tmp->SetOnlyLayout(bparams, firstpar->layout);
1481 tmp->params.align(firstpar->params.align());
1482 tmp->SetLabelWidthString(firstpar->params.labelWidthString());
1484 tmp->params.lineBottom(firstpar->params.lineBottom());
1485 firstpar->params.lineBottom(false);
1486 tmp->params.pagebreakBottom(firstpar->params.pagebreakBottom());
1487 firstpar->params.pagebreakBottom(false);
1488 tmp->params.spaceBottom(firstpar->params.spaceBottom());
1489 firstpar->params.spaceBottom(VSpace(VSpace::NONE));
1491 tmp->params.depth(firstpar->params.depth());
1492 tmp->params.noindent(firstpar->params.noindent());
1494 // copy everything behind the break-position
1495 // to the new paragraph
1497 while (ParFromPos(pos_first) != par)
1499 pos_end = pos_first + par->text.size() - 1;
1502 for (i = j = pos; i <= pos_end; ++i) {
1503 par->CutIntoMinibuffer(bparams, i - pos_first);
1504 if (tmp->InsertFromMinibuffer(j - pos))
1510 for (i = pos_end; i >= pos; --i)
1511 par->Erase(i - pos_first);
1516 // just an idea of me
1518 tmp->params.lineTop(firstpar->params.lineTop());
1519 tmp->params.pagebreakTop(firstpar->params.pagebreakTop());
1520 tmp->params.spaceTop(firstpar->params.spaceTop());
1521 tmp->bibkey = firstpar->bibkey;
1523 // layout stays the same with latex-environments
1525 firstpar->SetOnlyLayout(bparams, tmp->layout);
1526 firstpar->SetLabelWidthString(tmp->params.labelWidthString());
1527 firstpar->params.depth(tmp->params.depth());
1532 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1533 LyXParagraph::size_type pos,
1536 // create a new paragraph
1537 LyXParagraph * tmp = new LyXParagraph(this);
1539 // this is an idea for a more userfriendly layout handling, I will
1540 // see what the users say
1542 // layout stays the same with latex-environments
1544 tmp->SetOnlyLayout(layout);
1545 tmp->SetLabelWidthString(params.labelWidthString());
1548 if (size() > pos || !size() || flag == 2) {
1549 tmp->SetOnlyLayout(layout);
1550 tmp->params.align(params.align());
1551 tmp->SetLabelWidthString(params.labelWidthString());
1553 tmp->params.lineBottom(params.lineBottom());
1554 params.lineBottom(false);
1555 tmp->params.pagebreakBottom(params.pagebreakBottom());
1556 params.pagebreakBottom(false);
1557 tmp->params.spaceBottom(params.spaceBottom());
1558 params.spaceBottom(VSpace(VSpace::NONE));
1560 tmp->params.depth(params.depth());
1561 tmp->params.noindent(params.noindent());
1563 // copy everything behind the break-position
1564 // to the new paragraph
1565 size_type pos_end = text.size() - 1;
1568 for (; i <= pos_end; ++i) {
1569 CutIntoMinibuffer(bparams, i);
1570 if (tmp->InsertFromMinibuffer(j - pos))
1574 for (i = pos_end; i >= pos; --i)
1580 // just an idea of me
1582 tmp->params.lineTop(params.lineTop());
1583 tmp->params.pagebreakTop(params.pagebreakTop());
1584 tmp->params.spaceTop(params.spaceTop());
1585 tmp->bibkey = bibkey;
1587 // layout stays the same with latex-environments
1589 SetOnlyLayout(tmp->layout);
1590 SetLabelWidthString(tmp->params.labelWidthString());
1591 params.depth(tmp->params.depth());
1598 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1601 par = par->FirstPhysicalPar();
1602 footnoteflag = par->footnoteflag;
1603 footnotekind = par->footnotekind;
1605 layout = par->layout;
1606 params.makeSame(par->params);
1608 // This can be changed after NEW_INSETS is in effect. (Lgb)
1609 SetLabelWidthString(par->params.labelWidthString());
1614 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1616 LyXParagraph * tmppar = this;
1619 && tmppar->previous_->footnoteflag ==
1620 LyXParagraph::CLOSED_FOOTNOTE)
1621 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1622 tmppar = tmppar->previous_;
1625 return this; // This should never happen!
1632 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1634 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1644 && (IsNewline(0) || IsLineSeparator(0))){
1655 LyXParagraph * LyXParagraph::Clone() const
1657 // create a new paragraph
1658 LyXParagraph * result = new LyXParagraph;
1660 result->MakeSameLayout(this);
1662 // this is because of the dummy layout of the paragraphs that
1664 result->layout = layout;
1666 result->inset_owner = inset_owner;
1670 result->bibkey = static_cast<InsetBibKey *>
1671 (bibkey->Clone(*current_view->buffer()));
1675 // copy everything behind the break-position to the new paragraph
1677 result->text = text;
1678 result->fontlist = fontlist;
1679 result->insetlist = insetlist;
1680 for (InsetList::iterator it = result->insetlist.begin();
1681 it != result->insetlist.end(); ++it)
1682 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1687 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1690 par = par->FirstPhysicalPar();
1695 par->footnoteflag == footnoteflag &&
1696 par->footnotekind == footnotekind &&
1698 par->layout == layout &&
1699 params.sameLayout(par->params);
1703 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1704 LyXParagraph::size_type pos)
1707 // create a new paragraph
1708 LyXParagraph * par = ParFromPos(pos);
1710 LyXParagraph * tmp = new LyXParagraph(par);
1712 tmp->MakeSameLayout(par);
1714 // When can pos < Last()?
1715 // I guess pos == Last() is possible.
1717 // copy everything behind the break-position to the new
1719 size_type pos_first = 0;
1720 while (ParFromPos(pos_first) != par)
1722 size_type pos_end = pos_first + par->text.size() - 1;
1725 for (i = j = pos; i <= pos_end; ++i) {
1726 par->CutIntoMinibuffer(bparams, i - pos_first);
1727 if (tmp->InsertFromMinibuffer(j - pos))
1733 for (size_type i = pos_end; i >= pos; --i)
1734 par->Erase(i - pos_first);
1739 // create a new paragraph
1740 LyXParagraph * tmp = new LyXParagraph(this);
1741 tmp->MakeSameLayout(this);
1743 // When can pos > Last()?
1744 // I guess pos == Last() is possible.
1746 // copy everything behind the break-position to the new
1748 size_type pos_end = text.size() - 1;
1751 for (i = j = pos; i <= pos_end; ++i) {
1752 CutIntoMinibuffer(bparams, i);
1753 if (tmp->InsertFromMinibuffer(j - pos))
1759 for (size_type i = pos_end; i >= pos; --i)
1768 // Be carefull, this does not make any check at all.
1769 // This method has wrong name, it combined this par with the next par.
1770 // In that sense it is the reverse of break paragraph. (Lgb)
1771 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1773 // copy the next paragraph to this one
1774 LyXParagraph * the_next = next();
1776 // first the DTP-stuff
1778 LyXParagraph * firstpar = FirstPhysicalPar();
1779 firstpar->params.lineBottom(the_next->params.lineBottom());
1780 firstpar->params.spaceBottom(the_next->params.spaceBottom());
1781 firstpar->params.pagebreakBottom(the_next->params.pagebreakBottom());
1783 size_type pos_end = the_next->text.size() - 1;
1784 size_type pos_insert = Last();
1786 params.lineBottom(the_next->params.lineBottom());
1787 params.spaceBottom(the_next->params.spaceBottom());
1788 params.pagebreakBottom(the_next->params.pagebreakBottom());
1790 size_type pos_end = the_next->text.size() - 1;
1791 size_type pos_insert = size();
1794 // ok, now copy the paragraph
1796 for (i = j = 0; i <= pos_end; ++i) {
1797 the_next->CutIntoMinibuffer(bparams, i);
1798 if (InsertFromMinibuffer(pos_insert + j))
1802 // delete the next paragraph
1803 LyXParagraph * ppar = the_next->previous_;
1804 LyXParagraph * npar = the_next->next_;
1811 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1813 LyXParagraph * par = ParFromPos(pos);
1815 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1816 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1822 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1824 LyXParagraph * par = ParFromPos(pos);
1826 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1827 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1834 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1836 LyXParagraph const * par = this;
1837 int par_depth = GetDepth();
1839 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1840 int const endlabeltype =
1841 textclasslist.Style(bparams.textclass,
1842 layout).endlabeltype;
1843 if (endlabeltype != END_LABEL_NO_LABEL) {
1844 LyXParagraph const * last = this;
1846 if (footnoteflag == NO_FOOTNOTE)
1847 last = LastPhysicalPar();
1848 else if (next_->footnoteflag == NO_FOOTNOTE)
1849 return endlabeltype;
1854 if (!last || !last->next_)
1855 return endlabeltype;
1857 int next_depth = last->next_->GetDepth();
1858 if (par_depth > next_depth ||
1859 (par_depth == next_depth && layout != last->next_->GetLayout() ))
1860 return endlabeltype;
1865 par = par->DepthHook(par_depth - 1);
1867 par_depth = par->GetDepth();
1869 return END_LABEL_NO_LABEL;
1873 LyXTextClass::size_type LyXParagraph::GetLayout() const
1876 return FirstPhysicalPar()->layout;
1883 char LyXParagraph::GetDepth() const
1886 return FirstPhysicalPar()->params.depth();
1888 return params.depth();
1893 char LyXParagraph::GetAlign() const
1896 return FirstPhysicalPar()->params.align();
1898 return params.align();
1903 string const & LyXParagraph::GetLabelstring() const
1906 return FirstPhysicalPar()->params.labelString();
1908 return params.labelString();
1913 int LyXParagraph::GetFirstCounter(int i) const
1916 return FirstPhysicalPar()->counter_[i];
1923 // the next two functions are for the manual labels
1924 string const LyXParagraph::GetLabelWidthString() const
1927 if (!FirstPhysicalPar()->params.labelWidthString().empty())
1928 return FirstPhysicalPar()->params.labelWidthString();
1930 if (!params.labelWidthString().empty())
1931 return params.labelWidthString();
1934 return _("Senseless with this layout!");
1938 void LyXParagraph::SetLabelWidthString(string const & s)
1941 LyXParagraph * par = FirstPhysicalPar();
1943 par->params.labelWidthString(s);
1945 params.labelWidthString(s);
1951 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1952 LyXTextClass::size_type new_layout)
1954 LyXParagraph * par = FirstPhysicalPar();
1955 LyXParagraph * ppar = 0;
1956 LyXParagraph * npar = 0;
1958 par->layout = new_layout;
1960 if (par->params.pextraType() == PEXTRA_NONE) {
1961 if (par->previous()) {
1962 ppar = par->previous()->FirstPhysicalPar();
1965 && (ppar->params.depth() > par->params.depth()))
1966 ppar = ppar->previous()->FirstPhysicalPar();
1969 npar = par->next()->NextAfterFootnote();
1972 && (npar->params.depth() > par->params.depth()))
1973 npar = npar->next()->NextAfterFootnote();
1975 if (ppar && (ppar->params.pextraType() != PEXTRA_NONE)) {
1976 string p1 = ppar->params.pextraWidth();
1977 string p2 = ppar->params.pextraWidthp();
1978 ppar->SetPExtraType(bparams,
1979 ppar->params.pextraType(),
1982 if ((par->params.pextraType() == PEXTRA_NONE) &&
1983 npar && (npar->params.pextraType() != PEXTRA_NONE)) {
1984 string const p1 = npar->params.pextraWidth();
1985 string const p2 = npar->params.pextraWidthp();
1986 npar->SetPExtraType(bparams, npar->params.pextraType(),
1992 void LyXParagraph::SetOnlyLayout(LyXTextClass::size_type new_layout)
1995 LyXParagraph * par = this;
1996 LyXParagraph * ppar = 0;
1997 LyXParagraph * npar = 0;
1999 par->layout = new_layout;
2001 if (par->previous()) {
2002 ppar = par->previous();
2005 && (ppar->params.depth() > par->params.depth()))
2006 ppar = ppar->previous();
2012 && (npar->params.depth() > par->params.depth()))
2013 npar = npar->next();
2016 layout = new_layout;
2023 void LyXParagraph::SetLayout(BufferParams const & bparams,
2024 LyXTextClass::size_type new_layout)
2027 * par = FirstPhysicalPar(),
2031 par->layout = new_layout;
2032 par->params.labelWidthString(string());
2033 par->params.align(LYX_ALIGN_LAYOUT);
2034 par->params.spaceTop(VSpace(VSpace::NONE));
2035 par->params.spaceBottom(VSpace(VSpace::NONE));
2036 par->params.spacing(Spacing(Spacing::Default));
2038 if (par->params.pextraType() == PEXTRA_NONE) {
2039 if (par->previous()) {
2040 ppar = par->previous()->FirstPhysicalPar();
2043 && (ppar->params.depth() > par->params.depth()))
2044 ppar = ppar->previous()->FirstPhysicalPar();
2047 npar = par->next()->NextAfterFootnote();
2050 && (npar->params.depth() > par->params.depth()))
2051 npar = npar->next()->NextAfterFootnote();
2053 if (ppar && (ppar->params.pextraType() != PEXTRA_NONE)) {
2054 string const p1 = ppar->params.pextraWidth();
2055 string const p2 = ppar->params.pextraWidthp();
2056 ppar->SetPExtraType(bparams, ppar->params.pextraType(),
2059 if ((par->params.pextraType() == PEXTRA_NONE) &&
2060 npar && (npar->params.pextraType() != PEXTRA_NONE)) {
2061 string const p1 = npar->params.pextraWidth();
2062 string const p2 = npar->params.pextraWidthp();
2063 npar->SetPExtraType(bparams, npar->params.pextraType(),
2069 void LyXParagraph::SetLayout(LyXTextClass::size_type new_layout)
2071 layout = new_layout;
2072 params.labelWidthString(string());
2073 params.align(LYX_ALIGN_LAYOUT);
2074 params.spaceTop(VSpace(VSpace::NONE));
2075 params.spaceBottom(VSpace(VSpace::NONE));
2076 params.spacing(Spacing(Spacing::Default));
2081 // if the layout of a paragraph contains a manual label, the beginning of the
2082 // main body is the beginning of the second word. This is what the par-
2083 // function returns. If the layout does not contain a label, the main
2084 // body always starts with position 0. This differentiation is necessary,
2085 // because there cannot be a newline or a blank <= the beginning of the
2086 // main body in TeX.
2088 int LyXParagraph::BeginningOfMainBody() const
2091 if (FirstPhysicalPar() != this)
2094 // Unroll the first two cycles of the loop
2095 // and remember the previous character to
2096 // remove unnecessary GetChar() calls
2099 && GetChar(i) != LyXParagraph::META_NEWLINE
2102 char previous_char = 0;
2105 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2106 // Yes, this ^ is supposed to be "= " not "=="
2109 && previous_char != ' '
2110 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2112 previous_char = temp;
2118 if (i == 0 && i == size() &&
2119 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2120 && next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2121 ++i; /* the cursor should not jump
2122 * to the main body if there
2129 LyXParagraph * LyXParagraph::DepthHook(int deth)
2131 LyXParagraph * newpar = this;
2137 newpar = newpar->FirstPhysicalPar()->previous();
2139 newpar = newpar->previous();
2141 } while (newpar && newpar->GetDepth() > deth
2143 && newpar->footnoteflag == footnoteflag
2148 if (previous() || GetDepth())
2149 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2154 return newpar->FirstPhysicalPar();
2161 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2163 LyXParagraph const * newpar = this;
2169 newpar = newpar->FirstPhysicalPar()->previous();
2171 newpar = newpar->previous();
2173 } while (newpar && newpar->GetDepth() > deth
2175 && newpar->footnoteflag == footnoteflag
2180 if (previous() || GetDepth())
2181 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2186 return newpar->FirstPhysicalPar();
2193 int LyXParagraph::AutoDeleteInsets()
2196 InsetList::size_type index = 0;
2197 while (index < insetlist.size()) {
2198 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2199 Erase(insetlist[index].pos);
2200 // Erase() calls to insetlist.erase(&insetlist[index])
2201 // so index shouldn't be increased.
2210 LyXParagraph::inset_iterator
2211 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2213 InsetTable search_inset(pos, 0);
2214 InsetList::iterator it = lower_bound(insetlist.begin(),
2216 search_inset, matchIT());
2217 return inset_iterator(it);
2221 // returns -1 if inset not found
2222 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2225 for (InsetList::const_iterator cit = insetlist.begin();
2226 cit != insetlist.end(); ++cit) {
2227 if ((*cit).inset == inset) {
2231 if (inset == bibkey)
2235 // Think about footnotes.
2236 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2237 && next_ && next_->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2239 NextAfterFootnote()->GetPositionOfInset(inset);
2241 return text.size() + 1 + further;
2248 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2249 BufferParams const & bparams,
2250 ostream & os, TexRow & texrow,
2255 TexRow & foot_texrow,
2260 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2261 LyXLayout const & style =
2262 textclasslist.Style(bparams.textclass,
2265 bool further_blank_line = false;
2268 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2271 if (params.startOfAppendix()) {
2272 os << "\\appendix\n";
2276 if (!params.spacing().isDefault()
2277 && (!previous() || !previous()->HasSameLayout(this))) {
2278 os << params.spacing().writeEnvirBegin() << "\n";
2282 if (tex_code_break_column && style.isCommand()){
2287 if (params.pagebreakTop()) {
2289 further_blank_line = true;
2291 if (params.spaceTop().kind() != VSpace::NONE) {
2292 os << params.spaceTop().asLatexCommand(bparams);
2293 further_blank_line = true;
2296 if (params.lineTop()) {
2297 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2298 << "\\vspace{-1\\parskip}";
2299 further_blank_line = true;
2302 if (further_blank_line){
2307 Language const * language = getParLanguage(bparams);
2308 Language const * doc_language = bparams.language;
2309 Language const * previous_language = previous_
2310 ? previous_->getParLanguage(bparams) : doc_language;
2311 if (language->babel() != doc_language->babel() &&
2312 language->babel() != previous_language->babel()) {
2313 os << subst(lyxrc.language_command_begin, "$$lang",
2319 if (bparams.inputenc == "auto" &&
2320 language->encoding() != previous_language->encoding()) {
2321 os << "\\inputencoding{"
2322 << language->encoding()->LatexName()
2327 switch (style.latextype) {
2330 << style.latexname()
2331 << style.latexparam();
2333 case LATEX_ITEM_ENVIRONMENT:
2335 bibkey->Latex(buf, os, false, false);
2339 case LATEX_LIST_ENVIRONMENT:
2346 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2348 LyXParagraph * par = next_;
2350 // Spit out footnotes
2351 if (lyxrc.rtl_support) {
2352 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
2353 && next_->footnoteflag != footnoteflag) {
2354 LyXParagraph * p = 0;
2355 bool is_rtl = (size() > 0)
2356 ? GetFontSettings(bparams,
2357 size()-1).isRightToLeft()
2358 : language->RightToLeft();
2359 if ((p = NextAfterFootnote()) != 0 &&
2361 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2362 is_rtl = getParLanguage(bparams)->RightToLeft();
2363 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2364 && par->footnoteflag != footnoteflag) {
2365 par = par->TeXFootnote(buf, bparams,
2367 foot_texrow, foot_count,
2369 par->SimpleTeXOnePar(buf, bparams,
2370 os, texrow, moving_arg);
2371 is_rtl = (par->size() > 0)
2372 ? par->GetFontSettings(bparams,
2373 par->size()-1).isRightToLeft()
2374 : language->RightToLeft();
2376 par->next_->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2377 (p = par->NextAfterFootnote()) != 0 &&
2379 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2380 is_rtl = language->RightToLeft();
2385 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2386 && par->footnoteflag != footnoteflag) {
2387 par = par->TeXFootnote(buf, bparams,
2389 foot, foot_texrow, foot_count,
2391 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2397 // Make sure that \\par is done with the font of the last
2398 // character if this has another size as the default.
2399 // This is necessary because LaTeX (and LyX on the screen)
2400 // calculates the space between the baselines according
2401 // to this font. (Matthias)
2403 // Is this really needed ? (Dekel)
2404 // We do not need to use to change the font for the last paragraph
2405 // or for a command.
2407 LyXFont font = getFont(bparams, Last() - 1);
2409 LyXFont font = getFont(bparams, size() - 1);
2411 bool is_command = textclasslist.Style(bparams.textclass,
2412 GetLayout()).isCommand();
2413 if (style.resfont.size() != font.size() && next_ && !is_command) {
2416 os << "\\" << font.latexSize() << " \\par}";
2417 } else if (need_par) {
2419 } else if (is_command)
2422 if (language->babel() != doc_language->babel() &&
2425 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2427 || par->getParLanguage(bparams)->babel() != language->babel())) {
2429 << subst(lyxrc.language_command_end, "$$lang",
2430 doc_language->babel());
2433 switch (style.latextype) {
2434 case LATEX_ITEM_ENVIRONMENT:
2435 case LATEX_LIST_ENVIRONMENT:
2436 if (par && (params.depth() < par->params.depth())) {
2441 case LATEX_ENVIRONMENT:
2442 // if its the last paragraph of the current environment
2443 // skip it otherwise fall through
2445 && (par->layout != layout
2446 || par->params.depth() != params.depth()
2448 || par->params.pextraType() != params.pextraType()
2453 // we don't need it for the last paragraph!!!
2454 // or for tables in floats
2455 // -- effectively creates a \par where there isn't one which
2456 // breaks a \subfigure or \subtable.
2458 // && footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2464 further_blank_line = false;
2465 if (params.lineBottom()) {
2467 os << "\\lyxline{\\" << getFont(bparams,
2468 Last() - 1).latexSize() << '}';
2470 os << "\\lyxline{\\" << getFont(bparams,
2471 size() - 1).latexSize() << '}';
2473 further_blank_line = true;
2476 if (params.spaceBottom().kind() != VSpace::NONE) {
2477 os << params.spaceBottom().asLatexCommand(bparams);
2478 further_blank_line = true;
2481 if (params.pagebreakBottom()) {
2483 further_blank_line = true;
2486 if (further_blank_line){
2491 if (!params.spacing().isDefault()
2492 && (!par || !par->HasSameLayout(this))) {
2493 os << params.spacing().writeEnvirEnd() << "\n";
2497 // we don't need it for the last paragraph!!!
2500 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2501 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2508 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2513 // This one spits out the text of the paragraph
2514 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2515 BufferParams const & bparams,
2516 ostream & os, TexRow & texrow,
2519 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2521 bool return_value = false;
2523 LyXLayout const & style =
2524 textclasslist.Style(bparams.textclass,
2526 LyXFont basefont, last_font;
2528 // Maybe we have to create a optional argument.
2529 size_type main_body;
2530 if (style.labeltype != LABEL_MANUAL)
2533 main_body = BeginningOfMainBody();
2535 if (main_body > 0) {
2537 basefont = getFont(bparams, -2); // Get label font
2539 basefont = getFont(bparams, -1); // Get layout font
2550 if (style.isCommand()) {
2553 } else if (params.align() != LYX_ALIGN_LAYOUT) {
2556 return_value = true;
2560 moving_arg |= style.needprotect;
2562 // Which font is currently active?
2563 LyXFont running_font(basefont);
2564 // Do we have an open font change?
2565 bool open_font = false;
2567 texrow.start(this, 0);
2569 for (size_type i = 0; i < size(); ++i) {
2571 // First char in paragraph or after label?
2577 if (main_body > 0) {
2579 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2582 basefont = getFont(bparams, -1); // Now use the layout font
2583 running_font = basefont;
2587 if (style.isCommand()) {
2590 } else if (params.align() != LYX_ALIGN_LAYOUT && next_) {
2591 // We do not need \par here (Dekel)
2595 return_value = true;
2598 if (params.noindent()) {
2599 os << "\\noindent ";
2602 switch (params.align()) {
2603 case LYX_ALIGN_NONE:
2604 case LYX_ALIGN_BLOCK:
2605 case LYX_ALIGN_LAYOUT:
2606 case LYX_ALIGN_SPECIAL:
2608 case LYX_ALIGN_LEFT:
2611 if (getParLanguage(bparams)->babel() != "hebrew") {
2612 os << "\\raggedright ";
2615 os << "\\raggedleft ";
2619 case LYX_ALIGN_RIGHT:
2622 if (getParLanguage(bparams)->babel() != "hebrew") {
2623 os << "\\raggedleft ";
2626 os << "\\raggedright ";
2630 case LYX_ALIGN_CENTER:
2633 os << "\\centering ";
2639 value_type c = GetChar(i);
2641 // Fully instantiated font
2642 LyXFont font = getFont(bparams, i);
2644 LyXParagraph * p = 0;
2647 previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2648 (p = PreviousBeforeFootnote()) != 0)
2649 last_font = p->getFont(bparams, p->size() - 1);
2652 last_font = running_font;
2654 // Spaces at end of font change are simulated to be
2655 // outside font change, i.e. we write "\textXX{text} "
2656 // rather than "\textXX{text }". (Asger)
2657 if (open_font && c == ' ' && i <= size() - 2
2658 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2659 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2660 font = getFont(bparams, i + 1);
2662 // We end font definition before blanks
2663 if (!font.equalExceptLatex(running_font) && open_font) {
2664 column += running_font.latexWriteEndChanges(os,
2666 (i == main_body-1) ? basefont : font);
2667 running_font = basefont;
2671 // Blanks are printed before start of fontswitch
2673 // Do not print the separation of the optional argument
2674 if (i != main_body - 1) {
2675 SimpleTeXBlanks(os, texrow, i,
2676 column, font, style);
2680 // Do we need to change font?
2681 if (!font.equalExceptLatex(running_font)
2682 && i != main_body-1) {
2683 column += font.latexWriteStartChanges(os, basefont,
2685 running_font = font;
2689 if (c == LyXParagraph::META_NEWLINE) {
2690 // newlines are handled differently here than
2691 // the default in SimpleTeXSpecialChars().
2692 if (!style.newline_allowed
2693 || font.latex() == LyXFont::ON) {
2697 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2700 basefont = getFont(bparams, -1);
2701 running_font = basefont;
2702 if (font.family() ==
2703 LyXFont::TYPEWRITER_FAMILY) {
2711 texrow.start(this, i + 1);
2714 SimpleTeXSpecialChars(buf, bparams,
2715 os, texrow, moving_arg,
2716 font, running_font, basefont,
2717 open_font, style, i, column, c);
2721 // If we have an open font definition, we have to close it
2723 LyXParagraph * p = 0;
2726 && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
2727 && (p = NextAfterFootnote()) != 0
2732 running_font.latexWriteEndChanges(os, basefont,
2733 p->getFont(bparams, 0));
2735 running_font.latexWriteEndChanges(os, basefont, basefont);
2738 // Needed if there is an optional argument but no contents.
2739 if (main_body > 0 && main_body == size()) {
2741 return_value = false;
2744 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2745 return return_value;
2749 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2751 bool retval = false;
2753 case LyXParagraph::META_HFILL:
2754 sgml_string.erase();
2756 case LyXParagraph::META_NEWLINE:
2760 sgml_string = "&";
2763 sgml_string = "<";
2766 sgml_string = ">";
2769 sgml_string = "$";
2772 sgml_string = "#";
2775 sgml_string = "%";
2778 sgml_string = "[";
2781 sgml_string = "]";
2784 sgml_string = "{";
2787 sgml_string = "}";
2790 sgml_string = "˜";
2793 sgml_string = """;
2796 sgml_string = "\";
2802 case '\0': // Ignore :-)
2803 sgml_string.erase();
2813 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2814 LyXParagraph::size_type const i,
2815 int & column, LyXFont const & font,
2816 LyXLayout const & style)
2818 if (column > tex_code_break_column
2820 && GetChar(i - 1) != ' '
2822 // In LaTeX mode, we don't want to
2823 // break lines since some commands
2825 && ! (font.latex() == LyXFont::ON)
2826 // same in FreeSpacing mode
2827 && !style.free_spacing
2828 // In typewriter mode, we want to avoid
2829 // ! . ? : at the end of a line
2830 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2831 && (GetChar(i-1) == '.'
2832 || GetChar(i-1) == '?'
2833 || GetChar(i-1) == ':'
2834 || GetChar(i-1) == '!'))) {
2835 if (tex_code_break_column == 0) {
2836 // in batchmode we need LaTeX to still
2837 // see it as a space not as an extra '\n'
2843 texrow.start(this, i + 1);
2845 } else if (font.latex() == LyXFont::OFF) {
2846 if (style.free_spacing) {
2855 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2856 BufferParams const & bparams,
2857 ostream & os, TexRow & texrow,
2860 LyXFont & running_font,
2863 LyXLayout const & style,
2864 LyXParagraph::size_type & i,
2866 LyXParagraph::value_type const c)
2868 // Two major modes: LaTeX or plain
2869 // Handle here those cases common to both modes
2870 // and then split to handle the two modes separately.
2872 case LyXParagraph::META_INSET: {
2873 Inset * inset = GetInset(i);
2876 int const len = os.tellp();
2877 //ostream::pos_type const len = os.tellp();
2878 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2879 || inset->LyxCode() == Inset::MATH_CODE
2880 || inset->LyxCode() == Inset::URL_CODE)
2881 && running_font.isRightToLeft()) {
2886 int tmp = inset->Latex(buf, os, moving_arg,
2887 style.free_spacing);
2895 column += int(os.tellp()) - len;
2904 case LyXParagraph::META_NEWLINE:
2906 column += running_font.latexWriteEndChanges(os,
2911 basefont = getFont(bparams, -1);
2912 running_font = basefont;
2915 case LyXParagraph::META_HFILL:
2921 // And now for the special cases within each mode
2922 // Are we in LaTeX mode?
2923 if (font.latex() == LyXFont::ON) {
2924 // at present we only have one option
2925 // but I'll leave it as a switch statement
2926 // so its simpler to extend. (ARRae)
2929 // make sure that we will not print
2930 // error generating chars to the tex
2931 // file. This test would not be needed
2932 // if it were done in the buffer
2940 // Plain mode (i.e. not LaTeX)
2943 os << "\\textbackslash{}";
2947 case '°': case '±': case '²': case '³':
2948 case '×': case '÷': case '¹': case 'ª':
2949 case 'º': case '¬': case 'µ':
2950 if (bparams.inputenc == "latin1" ||
2951 (bparams.inputenc == "auto" &&
2952 font.language()->encoding()->LatexName()
2954 os << "\\ensuremath{"
2963 case '|': case '<': case '>':
2964 // In T1 encoding, these characters exist
2965 if (lyxrc.fontenc == "T1") {
2967 //... but we should avoid ligatures
2968 if ((c == '>' || c == '<')
2970 && GetChar(i + 1) == c) {
2971 //os << "\\textcompwordmark{}";
2972 // Jean-Marc, have a look at
2973 // this. IÂ think this works
2981 // Typewriter font also has them
2982 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2986 // Otherwise, we use what LaTeX
2990 os << "\\textless{}";
2994 os << "\\textgreater{}";
2998 os << "\\textbar{}";
3004 case '-': // "--" in Typewriter mode -> "-{}-"
3006 && GetChar(i + 1) == '-'
3007 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3016 os << "\\char`\\\"{}";
3021 if (bparams.inputenc == "default") {
3030 case '%': case '#': case '{':
3037 os << "\\textasciitilde{}";
3042 os << "\\textasciicircum{}";
3046 case '*': case '[': case ']':
3047 // avoid being mistaken for optional arguments
3048 os << '{' << c << '}';
3053 // Blanks are printed before font switching.
3054 // Sure? I am not! (try nice-latex)
3055 // I am sure it's correct. LyX might be smarter
3056 // in the future, but for now, nothing wrong is
3061 /* idea for labels --- begin*/
3065 && font.family() != LyXFont::TYPEWRITER_FAMILY
3066 && GetChar(i + 1) == 'y'
3067 && GetChar(i + 2) == 'X') {
3075 && font.family() != LyXFont::TYPEWRITER_FAMILY
3076 && GetChar(i + 1) == 'e'
3077 && GetChar(i + 2) == 'X') {
3082 // Check for "LaTeX2e"
3085 && font.family() != LyXFont::TYPEWRITER_FAMILY
3086 && GetChar(i + 1) == 'a'
3087 && GetChar(i + 2) == 'T'
3088 && GetChar(i + 3) == 'e'
3089 && GetChar(i + 4) == 'X'
3090 && GetChar(i + 5) == '2'
3091 && GetChar(i + 6) == 'e') {
3096 // Check for "LaTeX"
3099 && font.family() != LyXFont::TYPEWRITER_FAMILY
3100 && GetChar(i + 1) == 'a'
3101 && GetChar(i + 2) == 'T'
3102 && GetChar(i + 3) == 'e'
3103 && GetChar(i + 4) == 'X') {
3107 /* idea for labels --- end*/
3108 } else if (c != '\0') {
3118 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3119 BufferParams const & bparams,
3120 ostream & os, TexRow & texrow
3123 TexRow & foot_texrow,
3128 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3129 LyXParagraph * par = this;
3132 (par->params.depth() == params.depth())
3134 && (par->footnoteflag == footnoteflag)
3139 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3141 if (textclasslist.Style(bparams.textclass,
3142 par->layout).isEnvironment()
3144 || par->params.pextraType() != PEXTRA_NONE
3147 par = par->TeXEnvironment(buf, bparams,
3155 par = par->TeXOnePar(buf, bparams,
3165 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3171 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3172 BufferParams const & bparams,
3173 ostream & os, TexRow & texrow
3176 TexRow & foot_texrow,
3182 bool eindent_open = false;
3185 bool foot_this_level = false;
3188 // flags when footnotetext should be appended to file.
3189 static bool minipage_open = false;
3190 static int minipage_open_depth = 0;
3191 char par_sep = bparams.paragraph_separation;
3194 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3197 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3200 LyXLayout const & style =
3201 textclasslist.Style(bparams.textclass,
3205 if (params.pextraType() == PEXTRA_INDENT) {
3206 if (!params.pextraWidth().empty()) {
3207 os << "\\begin{LyXParagraphIndent}{"
3208 << params.pextraWidth() << "}\n";
3210 //float ib = atof(pextra_widthp.c_str())/100;
3211 // string can't handle floats at present (971109)
3212 // so I'll do a conversion by hand knowing that
3213 // the limits are 0.0 to 1.0. ARRae.
3214 os << "\\begin{LyXParagraphIndent}{";
3215 switch (params.pextraWidthp().length()) {
3221 << params.pextraWidthp();
3225 << params.pextraWidthp();
3227 os << "\\columnwidth}\n";
3230 eindent_open = true;
3232 if ((params.pextraType() == PEXTRA_MINIPAGE) && !minipage_open) {
3233 if (params.pextraHfill() && previous() &&
3234 (previous()->params.pextraType() == PEXTRA_MINIPAGE)) {
3235 os << "\\hfill{}\n";
3238 if (par_sep == BufferParams::PARSEP_INDENT) {
3239 os << "{\\setlength\\parindent{0pt}\n";
3242 os << "\\begin{minipage}";
3243 switch (params.pextraAlignment()) {
3244 case MINIPAGE_ALIGN_TOP:
3247 case MINIPAGE_ALIGN_MIDDLE:
3250 case MINIPAGE_ALIGN_BOTTOM:
3254 if (!params.pextraWidth().empty()) {
3255 os << '{' << params.pextraWidth() << "}\n";
3257 //float ib = atof(par->pextra_width.c_str())/100;
3258 // string can't handle floats at present
3259 // so I'll do a conversion by hand knowing that
3260 // the limits are 0.0 to 1.0. ARRae.
3262 switch (params.pextraWidthp().length()) {
3268 << params.pextraWidthp();
3272 << params.pextraWidthp();
3274 os << "\\columnwidth}\n";
3277 if (par_sep == BufferParams::PARSEP_INDENT) {
3278 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3281 minipage_open = true;
3282 minipage_open_depth = params.depth();
3285 #ifdef WITH_WARNINGS
3286 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3287 //I disabled it because it breaks when lists span on several
3290 if (style.isEnvironment()){
3291 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3292 #ifdef FANCY_FOOTNOTE_CODE
3293 if (foot_count < 0) {
3294 // flag that footnote[mark][text] should be
3295 // used for any footnotes from now on
3297 foot_this_level = true;
3300 os << "\\begin{" << style.latexname() << "}{"
3301 << params.labelWidthString() << "}\n";
3302 } else if (style.labeltype == LABEL_BIBLIO) {
3304 os << "\\begin{" << style.latexname() << "}{"
3305 << bibitemWidest(buf)
3307 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3308 #ifdef FANCY_FOOTNOTE_CODE
3309 if (foot_count < 0) {
3310 // flag that footnote[mark][text] should be
3311 // used for any footnotes from now on
3313 foot_this_level = true;
3316 os << "\\begin{" << style.latexname() << '}'
3317 << style.latexparam() << '\n';
3319 os << "\\begin{" << style.latexname() << '}'
3320 << style.latexparam() << '\n';
3323 LyXParagraph * par = this;
3325 par = par->TeXOnePar(buf, bparams,
3329 foot, foot_texrow, foot_count
3333 if (minipage_open && par && !style.isEnvironment() &&
3334 (par->params.pextraType() == PEXTRA_MINIPAGE) &&
3335 par->params.pextraStartMinipage()) {
3336 os << "\\end{minipage}\n";
3338 if (par_sep == BufferParams::PARSEP_INDENT) {
3342 minipage_open = false;
3345 if (par && par->params.depth() > params.depth()) {
3346 if (textclasslist.Style(bparams.textclass,
3347 par->layout).isParagraph()
3349 // How to handle this? (Lgb)
3350 //&& !suffixIs(os, "\n\n")
3352 // There should be at least one '\n' already
3353 // but we need there to be two for Standard
3354 // paragraphs that are depth-increment'ed to be
3355 // output correctly. However, tables can
3356 // also be paragraphs so don't adjust them.
3359 // Will it ever harm to have one '\n' too
3360 // many? i.e. that we sometimes will have
3361 // three in a row. (Lgb)
3365 par = par->TeXDeeper(buf, bparams, os, texrow
3367 ,foot, foot_texrow, foot_count
3372 if (par && par->layout == layout && par->params.depth() == params.depth() &&
3373 (par->params.pextraType() == PEXTRA_MINIPAGE) && !minipage_open) {
3374 if (par->params.pextraHfill() && par->previous() &&
3375 (par->previous()->params.pextraType() == PEXTRA_MINIPAGE)){
3376 os << "\\hfill{}\n";
3379 if (par_sep == BufferParams::PARSEP_INDENT) {
3380 os << "{\\setlength\\parindent{0pt}\n";
3383 os << "\\begin{minipage}";
3384 switch (par->params.pextraAlignment()) {
3385 case MINIPAGE_ALIGN_TOP:
3388 case MINIPAGE_ALIGN_MIDDLE:
3391 case MINIPAGE_ALIGN_BOTTOM:
3395 if (!par->params.pextraWidth().empty()) {
3396 os << '{' << par->params.pextraWidth() << "}\n";
3398 //float ib = atof(par->pextra_widthp.c_str())/100;
3399 // string can't handle floats at present
3400 // so I'll do a conversion by hand knowing that
3401 // the limits are 0.0 to 1.0. ARRae.
3403 switch (par->params.pextraWidthp().length()) {
3408 os << "0." << par->params.pextraWidthp();
3411 os << "0.0" << par->params.pextraWidthp();
3413 os << "\\columnwidth}\n";
3416 if (par_sep == BufferParams::PARSEP_INDENT) {
3417 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3420 minipage_open = true;
3421 minipage_open_depth = par->params.depth();
3425 && par->layout == layout
3426 && par->params.depth() == params.depth()
3428 && par->params.pextraType() == params.pextraType()
3431 && par->footnoteflag == footnoteflag
3435 if (style.isEnvironment()) {
3436 os << "\\end{" << style.latexname() << "}\n";
3439 // maybe this should go after the minipage closes?
3440 if (foot_this_level) {
3441 if (foot_count >= 1) {
3442 if (foot_count > 1) {
3443 os << "\\addtocounter{footnote}{-"
3448 texrow += foot_texrow;
3450 foot_texrow.reset();
3457 if (minipage_open && (minipage_open_depth == params.depth()) &&
3458 (!par || par->params.pextraStartMinipage() ||
3459 par->params.pextraType() != PEXTRA_MINIPAGE)) {
3460 os << "\\end{minipage}\n";
3462 if (par_sep == BufferParams::PARSEP_INDENT) {
3466 if (par && par->params.pextraType() != PEXTRA_MINIPAGE) {
3467 os << "\\medskip\n\n";
3471 minipage_open = false;
3474 os << "\\end{LyXParagraphIndent}\n";
3477 if (!(par && (par->params.pextraType() == PEXTRA_MINIPAGE)
3478 && par->params.pextraHfill())) {
3483 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3484 return par; // ale970302
3489 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3490 BufferParams const & bparams,
3491 ostream & os, TexRow & texrow,
3492 ostream & foot, TexRow & foot_texrow,
3496 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3497 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3498 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3499 "No footnote!" << endl;
3501 LyXParagraph * par = this;
3502 LyXLayout const & style =
3503 textclasslist.Style(bparams.textclass,
3504 previous_->GetLayout());
3506 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3507 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3508 "Float other than footnote in command"
3509 " with moving argument is illegal" << endl;
3512 if (footnotekind != LyXParagraph::FOOTNOTE
3513 && footnotekind != LyXParagraph::MARGIN
3516 // How to solve this?
3517 //&& !suffixIs(file, '\n')
3519 // we need to ensure that real floats like tables and figures
3520 // have their \begin{} on a new line otherwise we can get
3521 // incorrect results when using the endfloat.sty package
3522 // especially if two floats follow one another. ARRae 981022
3523 // NOTE: if the file is length 0 it must have just been
3524 // written out so we assume it ended with a '\n'
3526 // As far as I can see there is never any harm in writing
3527 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3532 bool moving_arg = false;
3533 bool need_closing = false;
3534 bool is_rtl = isRightToLeftPar(bparams);
3536 if (is_rtl != parent_is_rtl) {
3541 need_closing = true;
3544 bool footer_in_body = true;
3545 switch (footnotekind) {
3546 case LyXParagraph::FOOTNOTE:
3547 if (style.intitle) {
3548 os << "\\thanks{\n";
3549 footer_in_body = false;
3552 if (foot_count == -1) {
3553 // we're at depth 0 so we can use:
3554 os << "\\footnote{%\n";
3555 footer_in_body = false;
3557 os << "\\footnotemark{}%\n";
3559 // we only need this when there are
3560 // multiple footnotes
3561 os << "\\stepcounter{footnote}";
3563 os << "\\footnotetext{%\n";
3564 foot_texrow.start(this, 0);
3565 foot_texrow.newline();
3570 case LyXParagraph::MARGIN:
3571 os << "\\marginpar{\n";
3573 case LyXParagraph::FIG:
3574 if (params.pextraType() == PEXTRA_FLOATFLT
3575 && (!params.pextraWidth().empty()
3576 || !params.pextraWidthp().empty())) {
3577 if (!params.pextraWidth().empty())
3578 os << "\\begin{floatingfigure}{"
3579 << params.pextraWidth() << "}\n";
3581 os << "\\begin{floatingfigure}{"
3582 << lyx::atoi(params.pextraWidthp()) / 100.0
3583 << "\\textwidth}\n";
3585 os << "\\begin{figure}";
3586 if (!bparams.float_placement.empty()) {
3587 os << '[' << bparams.float_placement << "]\n";
3593 case LyXParagraph::TAB:
3594 os << "\\begin{table}";
3595 if (!bparams.float_placement.empty()) {
3596 os << '[' << bparams.float_placement << "]\n";
3601 case LyXParagraph::WIDE_FIG:
3602 os << "\\begin{figure*}";
3603 if (!bparams.float_placement.empty()) {
3604 os << '[' << bparams.float_placement << "]\n";
3609 case LyXParagraph::WIDE_TAB:
3610 os << "\\begin{table*}";
3611 if (!bparams.float_placement.empty()) {
3612 os << '[' << bparams.float_placement << "]\n";
3617 case LyXParagraph::ALGORITHM:
3618 os << "\\begin{algorithm}\n";
3623 if (footnotekind != LyXParagraph::FOOTNOTE
3624 || !footer_in_body) {
3625 // Process text for all floats except footnotes in body
3627 LyXLayout const & style =
3629 .Style(bparams.textclass, par->layout);
3631 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3633 if (style.isEnvironment()
3634 || par->params.pextraType() == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3635 // Allows the use of minipages within float
3636 // environments. Shouldn't be circular because
3637 // we don't support footnotes inside
3638 // floats (yet). ARRae
3639 par = par->TeXEnvironment(buf, bparams, os,
3644 par = par->TeXOnePar(buf, bparams,
3645 os, texrow, moving_arg,
3650 if (par && !par->IsDummy() && par->params.depth() > params.depth()) {
3651 par = par->TeXDeeper(buf, bparams, os, texrow,
3655 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3657 // process footnotes > depth 0 or in environments separately
3658 // NOTE: Currently don't support footnotes within footnotes
3659 // even though that is possible using the \footnotemark
3660 std::ostringstream dummy;
3661 TexRow dummy_texrow;
3662 int dummy_count = 0;
3664 LyXLayout const & style =
3666 .Style(bparams.textclass, par->layout);
3668 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3670 if (style.isEnvironment()
3671 || par->params.pextraType() == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3672 // Allows the use of minipages within float
3673 // environments. Shouldn't be circular because
3674 // we don't support footnotes inside
3675 // floats (yet). ARRae
3676 par = par->TeXEnvironment(buf, bparams,
3678 dummy, dummy_texrow,
3681 par = par->TeXOnePar(buf, bparams,
3684 dummy, dummy_texrow,
3688 if (par && !par->IsDummy() && par->params.depth() > params.depth()) {
3689 par = par->TeXDeeper(buf, bparams,
3691 dummy, dummy_texrow,
3695 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3697 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3698 "Footnote in a Footnote -- not supported"
3703 switch (footnotekind) {
3704 case LyXParagraph::FOOTNOTE:
3705 if (footer_in_body) {
3706 // This helps tell which of the multiple
3707 // footnotetexts an error was in.
3709 foot_texrow.newline();
3714 case LyXParagraph::MARGIN:
3717 case LyXParagraph::FIG:
3718 if (params.pextraType() == PEXTRA_FLOATFLT
3719 && (!params.pextraWidth().empty()
3720 || !params.pextraWidthp().empty()))
3721 os << "\\end{floatingfigure}";
3723 os << "\\end{figure}";
3725 case LyXParagraph::TAB:
3726 os << "\\end{table}";
3728 case LyXParagraph::WIDE_FIG:
3729 os << "\\end{figure*}";
3731 case LyXParagraph::WIDE_TAB:
3732 os << "\\end{table*}";
3734 case LyXParagraph::ALGORITHM:
3735 os << "\\end{algorithm}";
3742 if (footnotekind != LyXParagraph::FOOTNOTE
3743 && footnotekind != LyXParagraph::MARGIN) {
3744 // we need to ensure that real floats like tables and figures
3745 // have their \end{} on a line of their own otherwise we can
3746 // get incorrect results when using the endfloat.sty package.
3751 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next_ << endl;
3756 bool LyXParagraph::IsDummy() const
3758 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous_
3759 && previous_->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3764 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3765 int type, string const & width,
3766 string const & widthp)
3768 params.pextraType(type);
3769 params.pextraWidth(width);
3770 params.pextraWidthp(widthp);
3772 if (textclasslist.Style(bparams.textclass,
3773 layout).isEnvironment()) {
3774 LyXParagraph * par = this;
3775 LyXParagraph * ppar = par;
3777 while (par && (par->layout == layout)
3778 && (par->params.depth() == params.depth())) {
3780 par = par->previous();
3783 par = par->FirstPhysicalPar();
3785 while (par && par->params.depth() > params.depth()) {
3786 par = par->previous();
3789 par = par->FirstPhysicalPar();
3794 while (par && (par->layout == layout)
3795 && (par->params.depth() == params.depth())) {
3796 par->params.pextraType(type);
3797 par->params.pextraWidth(width);
3798 par->params.pextraWidthp(widthp);
3800 par = par->NextAfterFootnote();
3804 if (par && (par->params.depth() > params.depth()))
3805 par->SetPExtraType(bparams,
3806 type, width, widthp);
3808 while (par && ((par->params.depth() > params.depth()) || par->IsDummy()))
3809 par = par->NextAfterFootnote();
3811 while (par && par->params.depth() > params.depth())
3819 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3821 if (params.pextraType() == PEXTRA_NONE)
3824 params.pextraType(PEXTRA_NONE);
3825 params.pextraWidth(string());
3826 params.pextraWidthp(string());
3828 if (textclasslist.Style(bparams.textclass,
3829 layout).isEnvironment()) {
3830 LyXParagraph * par = this;
3831 LyXParagraph * ppar = par;
3833 while (par && (par->layout == layout)
3834 && (par->params.depth() == params.depth())) {
3836 par = par->previous();
3839 par = par->FirstPhysicalPar();
3841 while (par && par->params.depth() > params.depth()) {
3842 par = par->previous();
3845 par = par->FirstPhysicalPar();
3850 while (par && (par->layout == layout)
3851 && (par->params.depth() == params.depth())) {
3852 par->params.pextraType(PEXTRA_NONE);
3853 par->params.pextraWidth(string());
3854 par->params.pextraWidthp(string());
3856 par = par->NextAfterFootnote();
3860 if (par && (par->params.depth() > params.depth()))
3861 par->UnsetPExtraType(bparams);
3863 while (par && ((par->params.depth() > params.depth()) || par->IsDummy()))
3864 par = par->NextAfterFootnote();
3866 while (par && par->params.depth() > params.depth())
3875 bool LyXParagraph::IsHfill(size_type pos) const
3877 return IsHfillChar(GetChar(pos));
3881 bool LyXParagraph::IsInset(size_type pos) const
3883 return IsInsetChar(GetChar(pos));
3888 bool LyXParagraph::IsFloat(size_type pos) const
3890 return IsFloatChar(GetChar(pos));
3895 bool LyXParagraph::IsNewline(size_type pos) const
3897 return pos >= 0 && IsNewlineChar(GetChar(pos));
3901 bool LyXParagraph::IsSeparator(size_type pos) const
3903 return IsSeparatorChar(GetChar(pos));
3907 bool LyXParagraph::IsLineSeparator(size_type pos) const
3909 return IsLineSeparatorChar(GetChar(pos));
3913 bool LyXParagraph::IsKomma(size_type pos) const
3915 return IsKommaChar(GetChar(pos));
3919 /// Used by the spellchecker
3920 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3922 value_type const c = GetChar(pos);
3923 if (IsLetterChar(c))
3925 // '\0' is not a letter, allthough every string contains "" (below)
3928 // We want to pass the ' and escape chars to ispell
3929 string const extra = lyxrc.isp_esc_chars + '\'';
3930 char ch[2] = { c, 0 };
3931 return contains(extra, ch);
3935 bool LyXParagraph::IsWord(size_type pos ) const
3937 return IsWordChar(GetChar(pos)) ;
3942 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3946 return FirstPhysicalPar()->getParLanguage(bparams);
3950 return GetFirstFontSettings().language();
3952 return previous_->getParLanguage(bparams);
3954 return bparams.language;
3958 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3960 return lyxrc.rtl_support
3961 && getParLanguage(bparams)->RightToLeft();
3965 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3966 Language const * from, Language const * to)
3968 for (size_type i = 0; i < size(); ++i) {
3969 LyXFont font = GetFontSettings(bparams, i);
3970 if (font.language() == from) {
3971 font.setLanguage(to);
3978 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3980 Language const * doc_language = bparams.language;
3981 for (FontList::const_iterator cit = fontlist.begin();
3982 cit != fontlist.end(); ++cit)
3983 if ((*cit).font().language() != doc_language)
3989 // Convert the paragraph to a string.
3990 // Used for building the table of contents
3991 string const LyXParagraph::String(Buffer const * buffer, bool label)
3993 BufferParams const & bparams = buffer->params;
3996 if (label && !IsDummy() && !params.labelString().empty())
3998 if (label && !params.labelString().empty())
4000 s += params.labelString() + ' ';
4001 string::size_type const len = s.size();
4003 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
4004 value_type c = GetChar(i);
4007 else if (c == META_INSET &&
4008 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
4009 std::ostringstream ost;
4010 GetInset(i)->Ascii(buffer, ost);
4011 s += subst(ost.str().c_str(),'\n',' ');
4016 if (next_ && next_->footnoteflag != LyXParagraph::NO_FOOTNOTE
4017 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
4018 s += NextAfterFootnote()->String(buffer, false);
4022 if (isRightToLeftPar(bparams))
4023 reverse(s.begin() + len,s.end());
4031 string const LyXParagraph::String(Buffer const * buffer,
4032 LyXParagraph::size_type beg,
4033 LyXParagraph::size_type end)
4038 if (beg == 0 && !IsDummy() && !params.labelString().empty())
4040 if (beg == 0 && !params.labelString().empty())
4042 s += params.labelString() + ' ';
4044 for (LyXParagraph::size_type i = beg; i < end; ++i) {
4045 value_type c = GetUChar(buffer->params, i);
4048 else if (c == META_INSET) {
4049 std::ostringstream ost;
4050 GetInset(i)->Ascii(buffer, ost);
4051 s += ost.str().c_str();
4059 void LyXParagraph::SetInsetOwner(Inset * i)
4062 for (InsetList::const_iterator cit = insetlist.begin();
4063 cit != insetlist.end(); ++cit) {
4065 (*cit).inset->setOwner(i);
4070 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4073 for (InsetList::const_iterator cit = insetlist.begin();
4074 cit != insetlist.end(); ++cit) {
4076 if ((*cit).inset->IsTextInset()) {
4077 static_cast<UpdatableInset *>
4078 ((*cit).inset)->deleteLyXText(bv);
4085 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4088 for (InsetList::const_iterator cit = insetlist.begin();
4089 cit != insetlist.end(); ++cit) {
4091 if ((*cit).inset->IsTextInset()) {
4092 static_cast<UpdatableInset *>
4093 ((*cit).inset)->resizeLyXText(bv);
4100 void LyXParagraph::fitToSize()
4102 TextContainer tmp(text.begin(), text.end());
4107 void LyXParagraph::setContentsFromPar(LyXParagraph * par)