1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "lyxparagraph.h"
21 #include "lyxparagraph.h"
22 #include "support/textutils.h"
25 #include "tex-strings.h"
27 #include "bufferparams.h"
28 #include "support/FileInfo.h"
29 #include "support/LAssert.h"
31 #include "LaTeXFeatures.h"
32 #include "insets/insetinclude.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
35 #include "support/filetools.h"
36 #include "lyx_gui_misc.h"
38 #include "support/lyxmanip.h"
39 #include "BufferView.h"
46 using std::lower_bound;
47 using std::upper_bound;
50 int tex_code_break_column = 72; // needs non-zero initialization. set later.
51 // this is a bad idea, but how can LyXParagraph find its buffer to get
52 // parameters? (JMarc)
54 extern string bibitemWidest(Buffer const *);
56 // this is a minibuffer
57 static char minibuffer_char;
58 static LyXFont minibuffer_font;
59 static Inset * minibuffer_inset;
62 extern BufferView * current_view;
64 // Initialization of the counter for the paragraph id's,
65 // declared in lyxparagraph.h
66 unsigned int LyXParagraph::paragraph_id = 0;
69 LyXParagraph::LyXParagraph()
71 text.reserve(500); // is this number too big?
73 for (int i = 0; i < 10; ++i) setCounter(i , 0);
80 footnoteflag = LyXParagraph::NO_FOOTNOTE;
81 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
83 align = LYX_ALIGN_BLOCK;
87 bibkey = 0; // ale970302
92 // This konstruktor inserts the new paragraph in a list.
93 LyXParagraph::LyXParagraph(LyXParagraph * par)
96 par->text.resize(par->text.size());
98 for (int i = 0; i < 10; ++i) setCounter(i, 0);
102 // double linked list begin
105 next->previous = this;
107 previous->next = this;
110 footnoteflag = LyXParagraph::NO_FOOTNOTE;
111 footnotekind = LyXParagraph::FOOTNOTE;
114 id_ = paragraph_id++;
116 bibkey = 0; // ale970302
122 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
123 BufferParams const & params,
124 char footflag, char dth) const
128 footnoteflag != LyXParagraph::NO_FOOTNOTE ||
130 || 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?
150 while (depth > dth) {
151 os << "\n\\begin_deeper ";
155 while (depth < dth) {
156 os << "\n\\end_deeper ";
162 // First write the layout
164 << textclasslist.NameOfLayout(params.textclass, layout)
167 // Maybe some vertical spaces.
168 if (added_space_top.kind() != VSpace::NONE)
169 os << "\\added_space_top "
170 << added_space_top.asLyXCommand() << " ";
171 if (added_space_bottom.kind() != VSpace::NONE)
172 os << "\\added_space_bottom "
173 << added_space_bottom.asLyXCommand() << " ";
175 // Maybe the paragraph has special spacing
176 spacing.writeFile(os, true);
178 // The labelwidth string used in lists.
179 if (!labelwidthstring.empty())
180 os << "\\labelwidthstring "
181 << labelwidthstring << '\n';
183 // Lines above or below?
187 os << "\\line_bottom ";
189 // Pagebreaks above or below?
191 os << "\\pagebreak_top ";
192 if (pagebreak_bottom)
193 os << "\\pagebreak_bottom ";
195 // Start of appendix?
196 if (start_of_appendix)
197 os << "\\start_of_appendix ";
204 if (align != LYX_ALIGN_LAYOUT) {
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] << " ";
214 if (pextra_type != PEXTRA_NONE) {
215 os << "\\pextra_type " << pextra_type;
216 if (pextra_type == PEXTRA_MINIPAGE) {
217 os << " \\pextra_alignment "
220 os << " \\pextra_hfill "
222 if (pextra_start_minipage)
223 os << " \\pextra_start_minipage "
224 << pextra_start_minipage;
226 if (!pextra_width.empty()) {
227 os << " \\pextra_width "
228 << VSpace(pextra_width).asLyXCommand();
229 } else if (!pextra_widthp.empty()) {
230 os << " \\pextra_widthp "
237 // Dummy layout. This means that a footnote ended.
238 os << "\n\\end_float ";
239 footflag = LyXParagraph::NO_FOOTNOTE;
245 bibkey->Write(buf, os);
247 LyXFont font1(LyXFont::ALL_INHERIT, params.language);
250 for (size_type i = 0; i < size(); ++i) {
256 // Write font changes
257 LyXFont font2 = GetFontSettings(params, i);
258 if (font2 != font1) {
259 font2.lyxWriteChanges(font1, os);
264 value_type c = GetChar(i);
268 Inset const * inset = GetInset(i);
270 if (inset->DirectWrite()) {
271 // international char, let it write
272 // code directly so it's shorter in
274 inset->Write(buf, os);
276 os << "\n\\begin_inset ";
277 inset->Write(buf, os);
278 os << "\n\\end_inset \n\n";
284 os << "\n\\newline \n";
288 os << "\n\\hfill \n";
292 os << "\n\\backslash \n";
296 if (i + 1 < size() && GetChar(i + 1) == ' ') {
303 if ((column > 70 && c == ' ')
308 // this check is to amend a bug. LyX sometimes
309 // inserts '\0' this could cause problems.
313 lyxerr << "ERROR (LyXParagraph::writeFile):"
314 " NULL char in structure." << endl;
320 // now write the next paragraph
322 next->writeFile(buf, os, params, footflag, dth);
326 void LyXParagraph::validate(LaTeXFeatures & features) const
328 BufferParams const & params = features.bufferParams();
331 // this will be useful later
332 LyXLayout const & layout =
333 textclasslist.Style(params.textclass,
338 if (line_top || line_bottom)
339 features.lyxline = true;
340 if (!spacing.isDefault())
341 features.setspace = true;
344 features.layout[GetLayout()] = true;
347 Language const * doc_language = params.language;
349 for (FontList::const_iterator cit = fontlist.begin();
350 cit != fontlist.end(); ++cit) {
351 if ((*cit).font.noun() == LyXFont::ON) {
352 lyxerr[Debug::LATEX] << "font.noun: "
353 << (*cit).font.noun()
355 features.noun = true;
356 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
357 << (*cit).font.stateText(0)
360 switch ((*cit).font.color()) {
362 case LColor::inherit:
366 features.color = true;
367 lyxerr[Debug::LATEX] << "Color enabled. Font: "
368 << (*cit).font.stateText(0)
372 Language const * language = (*cit).font.language();
373 if (language->babel() != doc_language->babel()) {
374 features.UsedLanguages.insert(language);
375 lyxerr[Debug::LATEX] << "Found language "
376 << language->babel() << endl;
381 for (InsetList::const_iterator cit = insetlist.begin();
382 cit != insetlist.end(); ++cit) {
384 (*cit).inset->Validate(features);
387 if (pextra_type == PEXTRA_INDENT)
388 features.LyXParagraphIndent = true;
389 if (pextra_type == PEXTRA_FLOATFLT)
390 features.floatflt = true;
392 if (layout.needprotect
393 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
394 features.NeedLyXFootnoteCode = true;
396 if (params.paragraph_separation == BufferParams::PARSEP_INDENT
397 && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
398 features.NeedLyXMinipageIndent = true;
400 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
401 features.algorithm = true;
406 // First few functions needed for cut and paste and paragraph breaking.
407 void LyXParagraph::CopyIntoMinibuffer(BufferParams const & bparams,
408 LyXParagraph::size_type pos) const
410 minibuffer_char = GetChar(pos);
411 minibuffer_font = GetFontSettings(bparams, pos);
412 minibuffer_inset = 0;
413 if (minibuffer_char == LyXParagraph::META_INSET) {
415 minibuffer_inset = GetInset(pos)->Clone(*current_view->buffer());
417 minibuffer_inset = 0;
418 minibuffer_char = ' ';
419 // This reflects what GetInset() does (ARRae)
425 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
426 LyXParagraph::size_type pos)
428 minibuffer_char = GetChar(pos);
429 minibuffer_font = GetFontSettings(bparams, pos);
430 minibuffer_inset = 0;
431 if (minibuffer_char == LyXParagraph::META_INSET) {
433 minibuffer_inset = GetInset(pos);
434 // This is a little hack since I want exactly
435 // the inset, not just a clone. Otherwise
436 // the inset would be deleted when calling Erase(pos)
438 InsetTable search_elem(pos, 0);
439 InsetList::iterator it =
440 lower_bound(insetlist.begin(),
442 search_elem, matchIT());
443 if (it != insetlist.end() && (*it).pos == pos)
446 minibuffer_inset = 0;
447 minibuffer_char = ' ';
448 // This reflects what GetInset() does (ARRae)
453 // Erase(pos); now the caller is responsible for that.
457 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
459 if ((minibuffer_char == LyXParagraph::META_INSET) &&
460 !InsertInsetAllowed(minibuffer_inset))
462 if (minibuffer_char == LyXParagraph::META_INSET)
463 InsertInset(pos, minibuffer_inset, minibuffer_font);
465 InsertChar(pos, minibuffer_char, minibuffer_font);
473 void LyXParagraph::Clear()
478 pagebreak_top = false;
479 pagebreak_bottom = false;
481 added_space_top = VSpace(VSpace::NONE);
482 added_space_bottom = VSpace(VSpace::NONE);
483 spacing.set(Spacing::Default);
485 align = LYX_ALIGN_LAYOUT;
489 pextra_type = PEXTRA_NONE;
490 pextra_width.erase();
491 pextra_widthp.erase();
492 pextra_alignment = MINIPAGE_ALIGN_TOP;
493 pextra_hfill = false;
494 pextra_start_minipage = false;
497 labelwidthstring.erase();
501 start_of_appendix = false;
505 // the destructor removes the new paragraph from the list
506 LyXParagraph::~LyXParagraph()
509 previous->next = next;
511 next->previous = previous;
513 for (InsetList::iterator it = insetlist.begin();
514 it != insetlist.end(); ++it) {
521 //lyxerr << "LyXParagraph::paragraph_id = "
522 // << LyXParagraph::paragraph_id << endl;
526 void LyXParagraph::Erase(LyXParagraph::size_type pos)
529 // > because last is the next unused position, and you can
530 // use it if you want
532 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
533 NextAfterFootnote()->Erase(pos - text.size() - 1);
535 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
536 "position does not exist." << endl;
540 Assert(pos < size());
543 if (pos < size()) { // last is free for insertation, but should be empty
545 // if it is an inset, delete the inset entry
546 if (text[pos] == LyXParagraph::META_INSET) {
548 InsetTable search_inset(pos, 0);
549 InsetList::iterator it =
550 lower_bound(insetlist.begin(),
552 search_inset, matchIT());
553 if (it != insetlist.end() && (*it).pos == pos) {
558 text.erase(text.begin() + pos);
560 // Erase entries in the tables.
561 FontTable search_font(pos, LyXFont());
563 FontList::iterator it =
564 lower_bound(fontlist.begin(),
566 search_font, matchFT());
567 if (it != fontlist.end() && (*it).pos == pos &&
569 (it != fontlist.begin() && (*(it-1)).pos == pos - 1))) {
570 // If it is a multi-character font
571 // entry, we just make it smaller
572 // (see update below), otherwise we
574 unsigned int i = it - fontlist.begin();
575 fontlist.erase(fontlist.begin() + i);
576 it = fontlist.begin() + i;
577 if (i > 0 && i < fontlist.size() &&
578 fontlist[i-1].font == fontlist[i].font) {
579 fontlist.erase(fontlist.begin() + i-1);
580 it = fontlist.begin() + i-1;
584 // Update all other entries.
585 FontList::iterator fend = fontlist.end();
586 for (; it != fend; ++it)
589 // Update the inset table.
590 InsetTable search_inset(pos, 0);
591 InsetList::iterator lend = insetlist.end();
592 for (InsetList::iterator it =
593 upper_bound(insetlist.begin(),
595 search_inset, matchIT());
600 lyxerr << "ERROR (LyXParagraph::Erase): "
601 "can't erase non-existant char." << endl;
607 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
608 LyXParagraph::value_type c)
610 LyXFont f(LyXFont::ALL_INHERIT);
611 InsertChar(pos, c, f);
615 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
616 LyXParagraph::value_type c,
617 LyXFont const & font)
620 // > because last is the next unused position, and you can
621 // use it if you want
624 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
625 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
628 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
629 "position does not exist." << endl;
633 Assert(pos <= size());
635 text.insert(text.begin() + pos, c);
636 // Update the font table.
637 FontTable search_font(pos, LyXFont());
638 for (FontList::iterator it = lower_bound(fontlist.begin(),
640 search_font, matchFT());
641 it != fontlist.end(); ++it)
644 // Update the inset table.
645 InsetTable search_inset(pos, 0);
646 for (InsetList::iterator it = lower_bound(insetlist.begin(),
648 search_inset, matchIT());
649 it != insetlist.end(); ++it)
656 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
659 LyXFont f(LyXFont::ALL_INHERIT);
660 InsertInset(pos, inset, f);
664 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
665 Inset * inset, LyXFont const & font)
670 // > because last is the next unused position, and you can
671 // use it if you want
674 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
676 ->InsertInset(pos - text.size() - 1,
679 lyxerr << "ERROR (LyXParagraph::InsertInset): "
680 "position does not exist: " << pos << endl;
684 Assert(pos <= size());
687 InsertChar(pos, META_INSET, font);
688 Assert(text[pos] == META_INSET);
690 // Add a new entry in the inset table.
691 InsetTable search_inset(pos, 0);
692 InsetList::iterator it = lower_bound(insetlist.begin(),
694 search_inset, matchIT());
695 if (it != insetlist.end() && (*it).pos == pos)
696 lyxerr << "ERROR (LyXParagraph::InsertInset): "
697 "there is an inset in position: " << pos << endl;
699 insetlist.insert(it, InsetTable(pos, inset));
701 inset->setOwner(inset_owner);
705 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
707 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
710 return inset_owner->InsertInsetAllowed(inset);
715 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
720 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
721 return NextAfterFootnote()
722 ->GetInset(pos - text.size() - 1);
724 lyxerr << "ERROR (LyXParagraph::GetInset): "
725 "position does not exist: "
731 Assert(pos < size());
734 InsetTable search_inset(pos, 0);
735 InsetList::iterator it = lower_bound(insetlist.begin(),
737 search_inset, matchIT());
738 if (it != insetlist.end() && (*it).pos == pos)
741 lyxerr << "ERROR (LyXParagraph::GetInset): "
742 "Inset does not exist: " << pos << endl;
745 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
746 // Did this commenting out introduce a bug? So far I have not
747 // see any, please enlighten me. (Lgb)
748 // My guess is that since the inset does not exist, we might
749 // as well replace it with a space to prevent craches. (Asger)
754 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
759 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
760 return NextAfterFootnote()
761 ->GetInset(pos - text.size() - 1);
763 lyxerr << "ERROR (LyXParagraph::GetInset): "
764 "position does not exist: "
770 Assert(pos < size());
773 InsetTable search_inset(pos, 0);
774 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
776 search_inset, matchIT());
777 if (cit != insetlist.end() && (*cit).pos == pos)
780 lyxerr << "ERROR (LyXParagraph::GetInset): "
781 "Inset does not exist: " << pos << endl;
783 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
784 // Did this commenting out introduce a bug? So far I have not
785 // see any, please enlighten me. (Lgb)
786 // My guess is that since the inset does not exist, we might
787 // as well replace it with a space to prevent craches. (Asger)
792 // Gets uninstantiated font setting at position.
793 // Optimized after profiling. (Asger)
794 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
795 LyXParagraph::size_type pos) const
798 Assert(pos <= size());
803 FontTable search_font(pos, LyXFont());
804 FontList::const_iterator cit = lower_bound(fontlist.begin(),
806 search_font, matchFT());
807 if (cit != fontlist.end())
813 // > because last is the next unused position, and you can
814 // use it if you want
815 else if (pos > size()) {
817 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
818 return NextAfterFootnote()
819 ->GetFontSettings(bparams,
820 pos - text.size() - 1);
822 // Why is it an error to ask for the font of a
823 // position that does not exist? Would it be
824 // enough for this to be enabled on debug?
825 // We want strict error checking, but it's ok to only
826 // have it when debugging. (Asger)
827 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
828 "position does not exist. "
829 << pos << " (" << static_cast<int>(pos)
833 return GetFontSettings(bparams, pos - 1);
836 if (pos == size() && size())
837 return GetFontSettings(bparams, pos - 1);
841 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
843 //return LyXFont(LyXFont::ALL_INHERIT);
846 // Gets uninstantiated font setting at position 0
847 LyXFont const LyXParagraph::GetFirstFontSettings() const
850 if (!fontlist.empty())
851 return fontlist[0].font;
855 else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
856 return NextAfterFootnote()->GetFirstFontSettings();
858 return LyXFont(LyXFont::ALL_INHERIT);
862 // Gets the fully instantiated font at a given position in a paragraph
863 // This is basically the same function as LyXText::GetFont() in text2.C.
864 // The difference is that this one is used for generating the LaTeX file,
865 // and thus cosmetic "improvements" are disallowed: This has to deliver
866 // the true picture of the buffer. (Asger)
867 // If position is -1, we get the layout font of the paragraph.
868 // If position is -2, we get the font of the manual label of the paragraph.
869 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
870 LyXParagraph::size_type pos) const
873 LyXLayout const & layout =
874 textclasslist.Style(bparams.textclass,
876 LyXParagraph::size_type main_body = 0;
877 if (layout.labeltype == LABEL_MANUAL)
878 main_body = BeginningOfMainBody();
883 layoutfont = layout.labelfont;
885 layoutfont = layout.font;
886 tmpfont = GetFontSettings(bparams, pos);
887 tmpfont.realize(layoutfont);
889 // process layoutfont for pos == -1 and labelfont for pos < -1
891 tmpfont = layout.font;
893 tmpfont = layout.labelfont;
894 tmpfont.setLanguage(getParLanguage(bparams));
897 // check for environment font information
898 char par_depth = GetDepth();
899 LyXParagraph const * par = this;
900 while (par && par_depth && !tmpfont.resolved()) {
901 par = par->DepthHook(par_depth - 1);
903 tmpfont.realize(textclasslist.
904 Style(bparams.textclass,
905 par->GetLayout()).font);
906 par_depth = par->GetDepth();
910 tmpfont.realize(textclasslist
911 .TextClass(bparams.textclass)
917 /// Returns the height of the highest font in range
919 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
920 LyXParagraph::size_type endpos) const
922 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
923 if (fontlist.empty())
926 FontTable end_search(endpos, LyXFont());
927 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
929 end_search, matchFT());
930 if (end_it != fontlist.end())
933 FontTable start_search(startpos, LyXFont());
934 for (FontList::const_iterator cit =
935 lower_bound(fontlist.begin(),
937 start_search, matchFT());
938 cit != end_it; ++cit) {
939 LyXFont::FONT_SIZE size = (*cit).font.size();
940 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
947 LyXParagraph::value_type
948 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
953 Assert(pos <= size());
954 if (!size() || pos == size()) return '\0';
965 // > because last is the next unused position, and you can
966 // use it if you want
967 else if (pos > size()) {
968 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
969 return NextAfterFootnote()
970 ->GetChar(pos - text.size() - 1);
973 lyxerr << "ERROR (LyXParagraph::GetChar const): "
974 "position does not exist."
975 << pos << " (" << static_cast<int>(pos)
981 // We should have a footnote environment.
982 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
983 // Notice that LyX does request the
984 // last char from time to time. (Asger)
985 //lyxerr << "ERROR (LyXParagraph::GetChar): "
986 // "expected footnote." << endl;
989 switch (next->footnotekind) {
990 case LyXParagraph::FOOTNOTE:
991 return LyXParagraph::META_FOOTNOTE;
992 case LyXParagraph::MARGIN:
993 return LyXParagraph::META_MARGIN;
994 case LyXParagraph::FIG:
995 case LyXParagraph::WIDE_FIG:
996 return LyXParagraph::META_FIG;
997 case LyXParagraph::TAB:
998 case LyXParagraph::WIDE_TAB:
999 return LyXParagraph::META_TAB;
1000 case LyXParagraph::ALGORITHM:
1001 return LyXParagraph::META_ALGORITHM;
1003 return '\0'; // to shut up gcc
1009 // return an string of the current word, and the end of the word in lastpos.
1010 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1012 Assert(lastpos >= 0);
1014 // the current word is defined as starting at the first character
1015 // from the immediate left of lastpospos which meets the definition
1016 // of IsLetter(), continuing to the last character to the right
1017 // of this meeting IsLetter.
1023 // move back until we have a letter
1025 //there's no real reason to have firstpos & lastpos as
1026 //separate variables as this is written, but maybe someon
1027 // will want to return firstpos in the future.
1029 //since someone might have typed a punctuation first
1030 int firstpos = lastpos;
1032 while ((firstpos >= 0) && !IsLetter(firstpos))
1035 // now find the beginning by looking for a nonletter
1037 while ((firstpos>= 0) && IsLetter(firstpos))
1040 // the above is now pointing to the preceeding non-letter
1044 // so copy characters into theword until we get a nonletter
1045 // note that this can easily exceed lastpos, wich means
1046 // that if used in the middle of a word, the whole word
1049 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1057 #warning Remember to get rid of this one. (Lgb)
1059 LyXParagraph::size_type LyXParagraph::Last() const
1062 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1063 return text.size() + NextAfterFootnote()->Last() + 1;
1064 // the 1 is the symbol
1073 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1075 // > because last is the next unused position, and you can
1076 // use it if you want
1079 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1080 return NextAfterFootnote()
1081 ->ParFromPos(pos - text.size() - 1);
1083 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1084 "position does not exist." << endl;
1093 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1095 // > because last is the next unused position, and you can
1096 // use it if you want
1099 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1100 return NextAfterFootnote()
1101 ->PositionInParFromPos(pos - text.size() - 1);
1104 "ERROR (LyXParagraph::PositionInParFromPos): "
1105 "position does not exist." << endl;
1114 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1115 LyXFont const & font)
1118 // > because last is the next unused position, and you can
1119 // use it if you want
1122 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1123 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1126 lyxerr << "ERROR (LyXParagraph::SetFont): "
1127 "position does not exist." << endl;
1132 Assert(pos <= size());
1135 // First, reduce font against layout/label font
1136 // Update: The SetCharFont() routine in text2.C already
1137 // reduces font, so we don't need to do that here. (Asger)
1138 // No need to simplify this because it will disappear
1139 // in a new kernel. (Asger)
1140 // Next search font table
1142 FontTable search_font(pos, LyXFont());
1143 FontList::iterator it = lower_bound(fontlist.begin(),
1145 search_font, matchFT());
1146 unsigned int i = it - fontlist.begin();
1147 bool notfound = it == fontlist.end();
1149 if (!notfound && fontlist[i].font == font)
1152 bool begin = pos == 0 || notfound ||
1153 (i > 0 && fontlist[i-1].pos == pos - 1);
1154 // Is position pos is a beginning of a font block?
1155 bool end = !notfound && fontlist[i].pos == pos;
1156 // Is position pos is the end of a font block?
1157 if (begin && end) { // A single char block
1158 if (i+1 < fontlist.size() &&
1159 fontlist[i+1].font == font) {
1160 // Merge the singleton block with the next block
1161 fontlist.erase(fontlist.begin() + i);
1162 if (i > 0 && fontlist[i-1].font == font)
1163 fontlist.erase(fontlist.begin() + i-1);
1164 } else if (i > 0 && fontlist[i-1].font == font) {
1165 // Merge the singleton block with the previous block
1166 fontlist[i-1].pos = pos;
1167 fontlist.erase(fontlist.begin() + i);
1169 fontlist[i].font = font;
1171 if (i > 0 && fontlist[i-1].font == font)
1172 fontlist[i-1].pos = pos;
1174 fontlist.insert(fontlist.begin() + i,
1175 FontTable(pos, font));
1177 fontlist[i].pos = pos - 1;
1178 if (!(i+1 < fontlist.size() &&
1179 fontlist[i+1].font == font))
1180 fontlist.insert(fontlist.begin() + i+1,
1181 FontTable(pos, font));
1182 } else { // The general case. The block is splitted into 3 blocks
1183 fontlist.insert(fontlist.begin() + i,
1184 FontTable(pos - 1, fontlist[i].font));
1185 fontlist.insert(fontlist.begin() + i+1, FontTable(pos, font));
1190 // This function is able to hide closed footnotes.
1191 LyXParagraph * LyXParagraph::Next()
1194 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1195 LyXParagraph * tmp = next;
1197 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1199 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1200 return tmp->Next(); /* there can be more than one
1201 footnote in a logical
1204 return next; // This should never happen!
1212 LyXParagraph * LyXParagraph::NextAfterFootnote()
1214 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1215 LyXParagraph * tmp = next;
1216 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1218 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1219 return tmp; /* there can be more than one footnote
1220 in a logical paragraph */
1222 return next; // This should never happen!
1230 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1232 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1233 LyXParagraph * tmp = next;
1234 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1236 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1237 return tmp; /* there can be more than one footnote
1238 in a logical paragraph */
1240 return next; // This should never happen!
1248 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1251 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1253 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1254 tmp = tmp->previous;
1255 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1256 return tmp; /* there can be more than one footnote
1257 in a logical paragraph */
1259 return previous; // This should never happen!
1267 LyXParagraph * LyXParagraph::LastPhysicalPar()
1269 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1272 LyXParagraph * tmp = this;
1274 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1275 tmp = tmp->NextAfterFootnote();
1283 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1285 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1288 LyXParagraph const * tmp = this;
1290 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1291 tmp = tmp->NextAfterFootnote();
1299 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1303 LyXParagraph * tmppar = this;
1307 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1308 tmppar = tmppar->previous;
1319 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1323 LyXParagraph const * tmppar = this;
1327 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1328 tmppar = tmppar->previous;
1338 // This function is able to hide closed footnotes.
1339 LyXParagraph * LyXParagraph::Previous()
1342 LyXParagraph * tmp = previous;
1347 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1348 tmp = tmp->previous;
1350 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1351 tmp = tmp->previous;
1352 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1353 return tmp->next->Previous();
1363 // This function is able to hide closed footnotes.
1364 LyXParagraph const * LyXParagraph::Previous() const
1367 LyXParagraph * tmp = previous;
1371 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1372 tmp = tmp->previous;
1374 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1375 tmp = tmp->previous;
1376 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1377 return tmp->next->Previous();
1387 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1388 LyXParagraph::size_type pos,
1393 // create a new paragraph
1396 size_type pos_first;
1397 LyXParagraph * par = ParFromPos(pos);
1398 LyXParagraph * firstpar = FirstPhysicalPar();
1400 LyXParagraph * tmp = new LyXParagraph(par);
1402 //LyXParagraph * par = this;
1403 //LyXParagraph * firstpar = this;
1404 LyXParagraph * tmp = new LyXParagraph(this);
1408 tmp->footnoteflag = footnoteflag;
1409 tmp->footnotekind = footnotekind;
1411 // this is an idea for a more userfriendly layout handling, I will
1412 // see what the users say
1415 // layout stays the same with latex-environments
1417 tmp->SetOnlyLayout(bparams, firstpar->layout);
1418 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1421 // layout stays the same with latex-environments
1423 tmp->SetOnlyLayout(bparams, layout);
1424 tmp->SetLabelWidthString(labelwidthstring);
1428 if (Last() > pos || !Last() || flag == 2) {
1429 tmp->SetOnlyLayout(bparams, firstpar->layout);
1430 tmp->align = firstpar->align;
1431 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1433 tmp->line_bottom = firstpar->line_bottom;
1434 firstpar->line_bottom = false;
1435 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1436 firstpar->pagebreak_bottom = false;
1437 tmp->added_space_bottom = firstpar->added_space_bottom;
1438 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1440 tmp->depth = firstpar->depth;
1441 tmp->noindent = firstpar->noindent;
1443 if (Last() > pos || !Last() || flag == 2) {
1444 tmp->SetOnlyLayout(bparams, layout);
1446 tmp->SetLabelWidthString(labelwidthstring);
1448 tmp->line_bottom = line_bottom;
1449 line_bottom = false;
1450 tmp->pagebreak_bottom = pagebreak_bottom;
1451 pagebreak_bottom = false;
1452 tmp->added_space_bottom = added_space_bottom;
1453 added_space_bottom = VSpace(VSpace::NONE);
1456 tmp->noindent = noindent;
1458 // copy everything behind the break-position
1459 // to the new paragraph
1462 while (ParFromPos(pos_first) != par)
1464 pos_end = pos_first + par->text.size() - 1;
1467 for (i = j = pos; i <= pos_end; ++i) {
1468 par->CutIntoMinibuffer(bparams, i - pos_first);
1469 if (tmp->InsertFromMinibuffer(j - pos))
1472 tmp->text.resize(tmp->text.size());
1473 for (i = pos_end; i >= pos; --i)
1474 par->Erase(i - pos_first);
1476 par->text.resize(par->text.size());
1478 size_type pos_end = text.size() - 1;
1480 for (i = j = pos; i <= pos_end; ++i) {
1481 CutIntoMinibuffer(bparams, i);
1482 if (tmp->InsertFromMinibuffer(j - pos))
1486 for (i = pos_end; i >= pos; --i)
1494 // just an idea of me
1496 tmp->line_top = firstpar->line_top;
1497 tmp->pagebreak_top = firstpar->pagebreak_top;
1498 tmp->added_space_top = firstpar->added_space_top;
1499 tmp->bibkey = firstpar->bibkey;
1501 // layout stays the same with latex-environments
1503 firstpar->SetOnlyLayout(bparams, tmp->layout);
1504 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1505 firstpar->depth = tmp->depth;
1509 // just an idea of me
1511 tmp->line_top = line_top;
1512 tmp->pagebreak_top = pagebreak_top;
1513 tmp->added_space_top = added_space_top;
1514 tmp->bibkey = bibkey;
1516 // layout stays the same with latex-environments
1518 SetOnlyLayout(bparams, tmp->layout);
1519 SetLabelWidthString(tmp->labelwidthstring);
1527 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1530 par = par->FirstPhysicalPar();
1531 footnoteflag = par->footnoteflag;
1532 footnotekind = par->footnotekind;
1534 layout = par->layout;
1535 align = par-> align;
1536 SetLabelWidthString(par->labelwidthstring);
1538 line_bottom = par->line_bottom;
1539 pagebreak_bottom = par->pagebreak_bottom;
1540 added_space_bottom = par->added_space_bottom;
1542 line_top = par->line_top;
1543 pagebreak_top = par->pagebreak_top;
1544 added_space_top = par->added_space_top;
1546 spacing = par->spacing;
1548 pextra_type = par->pextra_type;
1549 pextra_width = par->pextra_width;
1550 pextra_widthp = par->pextra_widthp;
1551 pextra_alignment = par->pextra_alignment;
1552 pextra_hfill = par->pextra_hfill;
1553 pextra_start_minipage = par->pextra_start_minipage;
1555 noindent = par->noindent;
1561 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1563 LyXParagraph * tmppar = this;
1566 && tmppar->previous->footnoteflag ==
1567 LyXParagraph::CLOSED_FOOTNOTE)
1568 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1569 tmppar = tmppar->previous;
1572 return this; // This should never happen!
1579 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1581 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1589 && (IsNewline(0) || IsLineSeparator(0))){
1600 LyXParagraph * LyXParagraph::Clone() const
1602 // create a new paragraph
1603 LyXParagraph * result = new LyXParagraph;
1605 result->MakeSameLayout(this);
1607 // this is because of the dummy layout of the paragraphs that
1609 result->layout = layout;
1611 result->inset_owner = inset_owner;
1615 result->bibkey = static_cast<InsetBibKey *>
1616 (bibkey->Clone(*current_view->buffer()));
1620 // copy everything behind the break-position to the new paragraph
1622 result->text = text;
1623 result->fontlist = fontlist;
1624 result->insetlist = insetlist;
1625 for (InsetList::iterator it = result->insetlist.begin();
1626 it != result->insetlist.end(); ++it)
1627 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1632 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1635 par = par->FirstPhysicalPar();
1640 par->footnoteflag == footnoteflag &&
1641 par->footnotekind == footnotekind &&
1643 par->layout == layout &&
1645 par->align == align &&
1647 par->line_bottom == line_bottom &&
1648 par->pagebreak_bottom == pagebreak_bottom &&
1649 par->added_space_bottom == added_space_bottom &&
1651 par->line_top == line_top &&
1652 par->pagebreak_top == pagebreak_top &&
1653 par->added_space_top == added_space_top &&
1655 par->spacing == spacing &&
1657 par->pextra_type == pextra_type &&
1658 par->pextra_width == pextra_width &&
1659 par->pextra_widthp == pextra_widthp &&
1660 par->pextra_alignment == pextra_alignment &&
1661 par->pextra_hfill == pextra_hfill &&
1662 par->pextra_start_minipage == pextra_start_minipage &&
1663 par->noindent == noindent &&
1664 par->depth == depth);
1668 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1669 LyXParagraph::size_type pos)
1672 // create a new paragraph
1673 LyXParagraph * par = ParFromPos(pos);
1675 LyXParagraph * tmp = new LyXParagraph(par);
1677 tmp->MakeSameLayout(par);
1679 // When can pos < Last()?
1680 // I guess pos == Last() is possible.
1682 // copy everything behind the break-position to the new
1684 size_type pos_first = 0;
1685 while (ParFromPos(pos_first) != par)
1687 size_type pos_end = pos_first + par->text.size() - 1;
1690 for (i = j = pos; i <= pos_end; ++i) {
1691 par->CutIntoMinibuffer(bparams, i - pos_first);
1692 if (tmp->InsertFromMinibuffer(j - pos))
1695 tmp->text.resize(tmp->text.size());
1696 for (size_type i = pos_end; i >= pos; --i)
1697 par->Erase(i - pos_first);
1699 par->text.resize(par->text.size());
1702 // create a new paragraph
1703 LyXParagraph * tmp = new LyXParagraph(this);
1704 tmp->MakeSameLayout(this);
1706 // When can pos > Last()?
1707 // I guess pos == Last() is possible.
1709 // copy everything behind the break-position to the new
1711 size_type pos_end = text.size() - 1;
1714 for (i = j = pos; i <= pos_end; ++i) {
1715 CutIntoMinibuffer(bparams, i);
1716 if (tmp->InsertFromMinibuffer(j - pos))
1722 for (size_type i = pos_end; i >= pos; --i)
1731 // Be carefull, this does not make any check at all.
1732 // This method has wrong name, it combined this par with the next par.
1733 // In that sense it is the reverse of break paragraph. (Lgb)
1734 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1736 // copy the next paragraph to this one
1737 LyXParagraph * the_next = Next();
1739 LyXParagraph * firstpar = FirstPhysicalPar();
1742 // first the DTP-stuff
1744 firstpar->line_bottom = the_next->line_bottom;
1745 firstpar->added_space_bottom = the_next->added_space_bottom;
1746 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1748 line_bottom = the_next->line_bottom;
1749 added_space_bottom = the_next->added_space_bottom;
1750 pagebreak_bottom = the_next->pagebreak_bottom;
1753 size_type pos_end = the_next->text.size() - 1;
1754 size_type pos_insert = Last();
1756 // ok, now copy the paragraph
1758 for (i = j = 0; i <= pos_end; ++i) {
1759 the_next->CutIntoMinibuffer(bparams, i);
1760 if (InsertFromMinibuffer(pos_insert + j))
1764 // delete the next paragraph
1765 LyXParagraph * ppar = the_next->previous;
1766 LyXParagraph * npar = the_next->next;
1773 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1775 LyXParagraph * par = ParFromPos(pos);
1777 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1778 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1784 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1786 LyXParagraph * par = ParFromPos(pos);
1788 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1789 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1796 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1798 LyXParagraph const * par = this;
1799 int par_depth = GetDepth();
1801 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1802 int const endlabeltype =
1803 textclasslist.Style(bparams.textclass,
1804 layout).endlabeltype;
1805 if (endlabeltype != END_LABEL_NO_LABEL) {
1806 LyXParagraph const * last = this;
1808 if (footnoteflag == NO_FOOTNOTE)
1809 last = LastPhysicalPar();
1810 else if (next->footnoteflag == NO_FOOTNOTE)
1811 return endlabeltype;
1816 if (!last || !last->next)
1817 return endlabeltype;
1819 int next_depth = last->next->GetDepth();
1820 if (par_depth > next_depth ||
1821 (par_depth == next_depth && layout != last->next->GetLayout() ))
1822 return endlabeltype;
1827 par = par->DepthHook(par_depth - 1);
1829 par_depth = par->GetDepth();
1831 return END_LABEL_NO_LABEL;
1835 LyXTextClass::size_type LyXParagraph::GetLayout() const
1838 return FirstPhysicalPar()->layout;
1845 char LyXParagraph::GetDepth() const
1848 return FirstPhysicalPar()->depth;
1855 char LyXParagraph::GetAlign() const
1858 return FirstPhysicalPar()->align;
1865 string const & LyXParagraph::GetLabelstring() const
1868 return FirstPhysicalPar()->labelstring;
1875 int LyXParagraph::GetFirstCounter(int i) const
1878 return FirstPhysicalPar()->counter_[i];
1885 // the next two functions are for the manual labels
1886 string const LyXParagraph::GetLabelWidthString() const
1889 if (!FirstPhysicalPar()->labelwidthstring.empty())
1890 return FirstPhysicalPar()->labelwidthstring;
1892 if (!labelwidthstring.empty())
1893 return labelwidthstring;
1896 return _("Senseless with this layout!");
1900 void LyXParagraph::SetLabelWidthString(string const & s)
1903 LyXParagraph * par = FirstPhysicalPar();
1905 par->labelwidthstring = s;
1907 labelwidthstring = s;
1912 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1913 LyXTextClass::size_type new_layout)
1916 LyXParagraph * par = FirstPhysicalPar();
1918 LyXParagraph * par = this;
1920 LyXParagraph * ppar = 0;
1921 LyXParagraph * npar = 0;
1923 par->layout = new_layout;
1925 if (par->pextra_type == PEXTRA_NONE) {
1926 if (par->Previous()) {
1928 ppar = par->Previous()->FirstPhysicalPar();
1930 ppar = par->Previous();
1934 && (ppar->depth > par->depth))
1936 ppar = ppar->Previous()->FirstPhysicalPar();
1938 ppar = ppar->Previous();
1943 npar = par->Next()->NextAfterFootnote();
1949 && (npar->depth > par->depth))
1951 npar = npar->Next()->NextAfterFootnote();
1953 npar = npar->Next();
1956 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1958 p1 = ppar->pextra_width,
1959 p2 = ppar->pextra_widthp;
1960 ppar->SetPExtraType(bparams, ppar->pextra_type,
1963 if ((par->pextra_type == PEXTRA_NONE) &&
1964 npar && (npar->pextra_type != PEXTRA_NONE)) {
1966 p1 = npar->pextra_width,
1967 p2 = npar->pextra_widthp;
1968 npar->SetPExtraType(bparams, npar->pextra_type,
1975 void LyXParagraph::SetLayout(BufferParams const & bparams,
1976 LyXTextClass::size_type new_layout)
1980 * par = FirstPhysicalPar(),
1987 par->layout = new_layout;
1988 par->labelwidthstring.erase();
1989 par->align = LYX_ALIGN_LAYOUT;
1990 par->added_space_top = VSpace(VSpace::NONE);
1991 par->added_space_bottom = VSpace(VSpace::NONE);
1992 par->spacing.set(Spacing::Default);
1994 if (par->pextra_type == PEXTRA_NONE) {
1995 if (par->Previous()) {
1997 ppar = par->Previous()->FirstPhysicalPar();
1999 ppar = par->Previous();
2003 && (ppar->depth > par->depth))
2005 ppar = ppar->Previous()->FirstPhysicalPar();
2007 ppar = ppar->Previous();
2012 npar = par->Next()->NextAfterFootnote();
2018 && (npar->depth > par->depth))
2020 npar = npar->Next()->NextAfterFootnote();
2022 npar = npar->Next();
2025 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2027 p1 = ppar->pextra_width,
2028 p2 = ppar->pextra_widthp;
2029 ppar->SetPExtraType(bparams, ppar->pextra_type,
2032 if ((par->pextra_type == PEXTRA_NONE) &&
2033 npar && (npar->pextra_type != PEXTRA_NONE)) {
2035 p1 = npar->pextra_width,
2036 p2 = npar->pextra_widthp;
2037 npar->SetPExtraType(bparams, npar->pextra_type,
2044 // if the layout of a paragraph contains a manual label, the beginning of the
2045 // main body is the beginning of the second word. This is what the par-
2046 // function returns. If the layout does not contain a label, the main
2047 // body always starts with position 0. This differentiation is necessary,
2048 // because there cannot be a newline or a blank <= the beginning of the
2049 // main body in TeX.
2051 int LyXParagraph::BeginningOfMainBody() const
2054 if (FirstPhysicalPar() != this)
2057 // Unroll the first two cycles of the loop
2058 // and remember the previous character to
2059 // remove unnecessary GetChar() calls
2062 && GetChar(i) != LyXParagraph::META_NEWLINE
2065 char previous_char = 0, temp = 0;
2067 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2068 // Yes, this ^ is supposed to be "= " not "=="
2071 && previous_char != ' '
2072 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2074 previous_char = temp;
2080 if (i == 0 && i == size() &&
2081 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2082 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2083 ++i; /* the cursor should not jump
2084 * to the main body if there
2091 LyXParagraph * LyXParagraph::DepthHook(int deth)
2093 LyXParagraph * newpar = this;
2099 newpar = newpar->FirstPhysicalPar()->Previous();
2101 newpar = newpar->Previous();
2103 } while (newpar && newpar->GetDepth() > deth
2105 && newpar->footnoteflag == footnoteflag
2110 if (Previous() || GetDepth())
2111 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2116 return newpar->FirstPhysicalPar();
2123 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2125 LyXParagraph const * newpar = this;
2131 newpar = newpar->FirstPhysicalPar()->Previous();
2133 newpar = newpar->Previous();
2135 } while (newpar && newpar->GetDepth() > deth
2137 && newpar->footnoteflag == footnoteflag
2142 if (Previous() || GetDepth())
2143 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2148 return newpar->FirstPhysicalPar();
2155 int LyXParagraph::AutoDeleteInsets()
2158 InsetList::size_type index = 0;
2159 while (index < insetlist.size()) {
2160 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2161 Erase(insetlist[index].pos);
2162 // Erase() calls to insetlist.erase(&insetlist[index])
2163 // so index shouldn't be increased.
2172 LyXParagraph::inset_iterator
2173 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2175 InsetTable search_inset(pos, 0);
2176 InsetList::iterator it = lower_bound(insetlist.begin(),
2178 search_inset, matchIT());
2179 return inset_iterator(it);
2183 // returns -1 if inset not found
2184 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2187 for (InsetList::const_iterator cit = insetlist.begin();
2188 cit != insetlist.end(); ++cit) {
2189 if ((*cit).inset == inset) {
2194 // Think about footnotes.
2195 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2196 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2198 NextAfterFootnote()->GetPositionOfInset(inset);
2200 return text.size() + 1 + further;
2207 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2208 BufferParams const & bparams,
2209 ostream & os, TexRow & texrow,
2214 TexRow & foot_texrow,
2219 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2220 LyXLayout const & style =
2221 textclasslist.Style(bparams.textclass,
2224 bool further_blank_line = false;
2227 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2230 if (start_of_appendix) {
2231 os << "\\appendix\n";
2235 if (!spacing.isDefault()
2236 && (!Previous() || !Previous()->HasSameLayout(this))) {
2237 os << spacing.writeEnvirBegin() << "\n";
2241 if (tex_code_break_column && style.isCommand()){
2246 if (pagebreak_top) {
2248 further_blank_line = true;
2250 if (added_space_top.kind() != VSpace::NONE) {
2251 os << added_space_top.asLatexCommand(bparams);
2252 further_blank_line = true;
2256 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2257 << "\\vspace{-1\\parskip}";
2258 further_blank_line = true;
2261 if (further_blank_line){
2266 Language const * language = getParLanguage(bparams);
2267 Language const * doc_language = bparams.language;
2268 Language const * previous_language = previous
2269 ? previous->getParLanguage(bparams) : doc_language;
2270 if (language->babel() != doc_language->babel() &&
2271 language->babel() != previous_language->babel()) {
2272 os << subst(lyxrc.language_command_begin, "$$lang",
2278 if (bparams.inputenc == "auto" &&
2279 language->encoding() != previous_language->encoding()) {
2280 os << "\\inputencoding{"
2281 << language->encoding()->LatexName()
2286 switch (style.latextype) {
2289 << style.latexname()
2290 << style.latexparam();
2292 case LATEX_ITEM_ENVIRONMENT:
2294 bibkey->Latex(buf, os, false, false);
2298 case LATEX_LIST_ENVIRONMENT:
2305 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2307 LyXParagraph * par = next;
2309 // Spit out footnotes
2310 if (lyxrc.rtl_support) {
2311 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2312 && next->footnoteflag != footnoteflag) {
2313 LyXParagraph * p = 0;
2314 bool is_rtl = (size() > 0)
2315 ? GetFontSettings(bparams,
2316 size()-1).isRightToLeft()
2317 : language->RightToLeft();
2318 if ((p = NextAfterFootnote()) != 0 &&
2320 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2321 is_rtl = getParLanguage(bparams)->RightToLeft();
2322 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2323 && par->footnoteflag != footnoteflag) {
2324 par = par->TeXFootnote(buf, bparams,
2326 foot_texrow, foot_count,
2328 par->SimpleTeXOnePar(buf, bparams,
2329 os, texrow, moving_arg);
2330 is_rtl = (par->size() > 0)
2331 ? par->GetFontSettings(bparams,
2332 par->size()-1).isRightToLeft()
2333 : language->RightToLeft();
2335 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2336 (p = par->NextAfterFootnote()) != 0 &&
2338 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2339 is_rtl = language->RightToLeft();
2344 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2345 && par->footnoteflag != footnoteflag) {
2346 par = par->TeXFootnote(buf, bparams,
2348 foot, foot_texrow, foot_count,
2350 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2356 // Make sure that \\par is done with the font of the last
2357 // character if this has another size as the default.
2358 // This is necessary because LaTeX (and LyX on the screen)
2359 // calculates the space between the baselines according
2360 // to this font. (Matthias)
2361 LyXFont font = getFont(bparams, Last() - 1);
2362 if (need_par && next) {
2363 if (style.resfont.size() != font.size()) {
2369 } else if (textclasslist.Style(bparams.textclass,
2370 GetLayout()).isCommand()) {
2371 if (style.resfont.size() != font.size()) {
2377 } else if ((style.resfont.size() != font.size()) && next){
2378 os << "{\\" << font.latexSize() << " \\par}";
2381 if (language->babel() != doc_language->babel() &&
2384 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2386 || par->getParLanguage(bparams)->babel() != language->babel())) {
2388 << subst(lyxrc.language_command_end, "$$lang",
2389 doc_language->babel());
2392 switch (style.latextype) {
2393 case LATEX_ITEM_ENVIRONMENT:
2394 case LATEX_LIST_ENVIRONMENT:
2395 if (par && (depth < par->depth)) {
2400 case LATEX_ENVIRONMENT:
2401 // if its the last paragraph of the current environment
2402 // skip it otherwise fall through
2404 && (par->layout != layout
2405 || par->depth != depth
2406 || par->pextra_type != pextra_type))
2409 // we don't need it for the last paragraph!!!
2416 further_blank_line = false;
2418 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2419 further_blank_line = true;
2422 if (added_space_bottom.kind() != VSpace::NONE) {
2423 os << added_space_bottom.asLatexCommand(bparams);
2424 further_blank_line = true;
2427 if (pagebreak_bottom) {
2429 further_blank_line = true;
2432 if (further_blank_line){
2437 if (!spacing.isDefault()
2438 && (!par || !par->HasSameLayout(this))) {
2439 os << spacing.writeEnvirEnd() << "\n";
2443 // we don't need it for the last paragraph!!!
2446 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2447 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2454 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2459 // This one spits out the text of the paragraph
2460 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2461 BufferParams const & bparams,
2462 ostream & os, TexRow & texrow,
2465 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2467 bool return_value = false;
2469 LyXLayout const & style =
2470 textclasslist.Style(bparams.textclass,
2472 LyXFont basefont, last_font;
2474 // Maybe we have to create a optional argument.
2475 size_type main_body;
2476 if (style.labeltype != LABEL_MANUAL)
2479 main_body = BeginningOfMainBody();
2481 if (main_body > 0) {
2483 basefont = getFont(bparams, -2); // Get label font
2485 basefont = getFont(bparams, -1); // Get layout font
2496 if (style.isCommand()) {
2499 } else if (align != LYX_ALIGN_LAYOUT) {
2502 return_value = true;
2506 moving_arg |= style.needprotect;
2508 // Which font is currently active?
2509 LyXFont running_font(basefont);
2510 // Do we have an open font change?
2511 bool open_font = false;
2513 texrow.start(this, 0);
2515 for (size_type i = 0; i < size(); ++i) {
2517 // First char in paragraph or after label?
2523 if (main_body > 0) {
2525 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2528 basefont = getFont(bparams, -1); // Now use the layout font
2529 running_font = basefont;
2533 if (style.isCommand()) {
2536 } else if (align != LYX_ALIGN_LAYOUT) {
2539 return_value = true;
2543 os << "\\noindent ";
2547 case LYX_ALIGN_NONE:
2548 case LYX_ALIGN_BLOCK:
2549 case LYX_ALIGN_LAYOUT:
2550 case LYX_ALIGN_SPECIAL:
2552 case LYX_ALIGN_LEFT:
2553 if (getParLanguage(bparams)->babel() != "hebrew") {
2554 os << "\\raggedright ";
2557 os << "\\raggedleft ";
2561 case LYX_ALIGN_RIGHT:
2562 if (getParLanguage(bparams)->babel() != "hebrew") {
2563 os << "\\raggedleft ";
2566 os << "\\raggedright ";
2570 case LYX_ALIGN_CENTER:
2571 os << "\\centering ";
2577 value_type c = GetChar(i);
2579 // Fully instantiated font
2580 LyXFont font = getFont(bparams, i);
2582 LyXParagraph * p = 0;
2585 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2586 (p = PreviousBeforeFootnote()) != 0)
2587 last_font = p->getFont(bparams, p->size() - 1);
2590 last_font = running_font;
2592 // Spaces at end of font change are simulated to be
2593 // outside font change, i.e. we write "\textXX{text} "
2594 // rather than "\textXX{text }". (Asger)
2595 if (open_font && c == ' ' && i <= size() - 2
2596 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2597 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2598 font = getFont(bparams, i + 1);
2600 // We end font definition before blanks
2601 if (!font.equalExceptLatex(running_font) && open_font) {
2602 column += running_font.latexWriteEndChanges(os,
2604 (i == main_body-1) ? basefont : font);
2605 running_font = basefont;
2609 // Blanks are printed before start of fontswitch
2611 // Do not print the separation of the optional argument
2612 if (i != main_body - 1) {
2613 SimpleTeXBlanks(os, texrow, i,
2614 column, font, style);
2618 // Do we need to change font?
2619 if (!font.equalExceptLatex(running_font)
2620 && i != main_body-1) {
2621 column += font.latexWriteStartChanges(os, basefont,
2623 running_font = font;
2627 if (c == LyXParagraph::META_NEWLINE) {
2628 // newlines are handled differently here than
2629 // the default in SimpleTeXSpecialChars().
2630 if (!style.newline_allowed
2631 || font.latex() == LyXFont::ON) {
2635 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2638 basefont = getFont(bparams, -1);
2639 running_font = basefont;
2640 if (font.family() ==
2641 LyXFont::TYPEWRITER_FAMILY) {
2649 texrow.start(this, i + 1);
2652 SimpleTeXSpecialChars(buf, bparams,
2653 os, texrow, moving_arg,
2654 font, running_font, basefont,
2655 open_font, style, i, column, c);
2659 // If we have an open font definition, we have to close it
2661 LyXParagraph * p = 0;
2664 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2665 && (p = NextAfterFootnote()) != 0
2670 running_font.latexWriteEndChanges(os, basefont,
2671 p->getFont(bparams, 0));
2673 running_font.latexWriteEndChanges(os, basefont, basefont);
2676 // Needed if there is an optional argument but no contents.
2677 if (main_body > 0 && main_body == size()) {
2679 return_value = false;
2682 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2683 return return_value;
2687 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2689 bool retval = false;
2691 case LyXParagraph::META_HFILL:
2692 sgml_string.erase();
2694 case LyXParagraph::META_NEWLINE:
2698 sgml_string = "&";
2701 sgml_string = "<";
2704 sgml_string = ">";
2707 sgml_string = "$";
2710 sgml_string = "#";
2713 sgml_string = "%";
2716 sgml_string = "[";
2719 sgml_string = "]";
2722 sgml_string = "{";
2725 sgml_string = "}";
2728 sgml_string = "˜";
2731 sgml_string = """;
2734 sgml_string = "\";
2740 case '\0': // Ignore :-)
2741 sgml_string.erase();
2751 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2752 LyXParagraph::size_type const i,
2753 int & column, LyXFont const & font,
2754 LyXLayout const & style)
2756 if (column > tex_code_break_column
2758 && GetChar(i - 1) != ' '
2760 // In LaTeX mode, we don't want to
2761 // break lines since some commands
2763 && ! (font.latex() == LyXFont::ON)
2764 // same in FreeSpacing mode
2765 && !style.free_spacing
2766 // In typewriter mode, we want to avoid
2767 // ! . ? : at the end of a line
2768 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2769 && (GetChar(i-1) == '.'
2770 || GetChar(i-1) == '?'
2771 || GetChar(i-1) == ':'
2772 || GetChar(i-1) == '!'))) {
2773 if (tex_code_break_column == 0) {
2774 // in batchmode we need LaTeX to still
2775 // see it as a space not as an extra '\n'
2781 texrow.start(this, i + 1);
2783 } else if (font.latex() == LyXFont::OFF) {
2784 if (style.free_spacing) {
2793 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2794 BufferParams const & bparams,
2795 ostream & os, TexRow & texrow,
2798 LyXFont & running_font,
2801 LyXLayout const & style,
2802 LyXParagraph::size_type & i,
2804 LyXParagraph::value_type const c)
2806 // Two major modes: LaTeX or plain
2807 // Handle here those cases common to both modes
2808 // and then split to handle the two modes separately.
2810 case LyXParagraph::META_INSET: {
2811 Inset * inset = GetInset(i);
2814 int len = os.tellp();
2815 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2816 || inset->LyxCode() == Inset::MATH_CODE
2817 || inset->LyxCode() == Inset::URL_CODE)
2818 && running_font.isRightToLeft()) {
2823 int tmp = inset->Latex(buf, os, moving_arg,
2824 style.free_spacing);
2832 column += os.tellp() - len;
2841 case LyXParagraph::META_NEWLINE:
2843 column += running_font.latexWriteEndChanges(os,
2848 basefont = getFont(bparams, -1);
2849 running_font = basefont;
2852 case LyXParagraph::META_HFILL:
2858 // And now for the special cases within each mode
2859 // Are we in LaTeX mode?
2860 if (font.latex() == LyXFont::ON) {
2861 // at present we only have one option
2862 // but I'll leave it as a switch statement
2863 // so its simpler to extend. (ARRae)
2866 // make sure that we will not print
2867 // error generating chars to the tex
2868 // file. This test would not be needed
2869 // if it were done in the buffer
2877 // Plain mode (i.e. not LaTeX)
2880 os << "\\textbackslash{}";
2884 case '°': case '±': case '²': case '³':
2885 case '×': case '÷': case '¹': case 'ª':
2886 case 'º': case '¬': case 'µ':
2887 if (bparams.inputenc == "latin1" ||
2888 (bparams.inputenc == "auto" &&
2889 font.language()->encoding()->LatexName()
2891 os << "\\ensuremath{"
2900 case '|': case '<': case '>':
2901 // In T1 encoding, these characters exist
2902 if (lyxrc.fontenc == "T1") {
2904 //... but we should avoid ligatures
2905 if ((c == '>' || c == '<')
2907 && GetChar(i + 1) == c) {
2908 //os << "\\textcompwordmark{}";
2909 // Jean-Marc, have a look at
2910 // this. IÂ think this works
2918 // Typewriter font also has them
2919 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2923 // Otherwise, we use what LaTeX
2927 os << "\\textless{}";
2931 os << "\\textgreater{}";
2935 os << "\\textbar{}";
2941 case '-': // "--" in Typewriter mode -> "-{}-"
2943 && GetChar(i + 1) == '-'
2944 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
2953 os << "\\char`\\\"{}";
2958 if (bparams.inputenc == "default") {
2967 case '%': case '#': case '{':
2974 os << "\\textasciitilde{}";
2979 os << "\\textasciicircum{}";
2983 case '*': case '[': case ']':
2984 // avoid being mistaken for optional arguments
2985 os << '{' << c << '}';
2990 // Blanks are printed before font switching.
2991 // Sure? I am not! (try nice-latex)
2992 // I am sure it's correct. LyX might be smarter
2993 // in the future, but for now, nothing wrong is
2998 /* idea for labels --- begin*/
3002 && font.family() != LyXFont::TYPEWRITER_FAMILY
3003 && GetChar(i + 1) == 'y'
3004 && GetChar(i + 2) == 'X') {
3012 && font.family() != LyXFont::TYPEWRITER_FAMILY
3013 && GetChar(i + 1) == 'e'
3014 && GetChar(i + 2) == 'X') {
3019 // Check for "LaTeX2e"
3022 && font.family() != LyXFont::TYPEWRITER_FAMILY
3023 && GetChar(i + 1) == 'a'
3024 && GetChar(i + 2) == 'T'
3025 && GetChar(i + 3) == 'e'
3026 && GetChar(i + 4) == 'X'
3027 && GetChar(i + 5) == '2'
3028 && GetChar(i + 6) == 'e') {
3033 // Check for "LaTeX"
3036 && font.family() != LyXFont::TYPEWRITER_FAMILY
3037 && GetChar(i + 1) == 'a'
3038 && GetChar(i + 2) == 'T'
3039 && GetChar(i + 3) == 'e'
3040 && GetChar(i + 4) == 'X') {
3044 /* idea for labels --- end*/
3045 } else if (c != '\0') {
3055 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3056 BufferParams const & bparams,
3057 ostream & os, TexRow & texrow
3060 TexRow & foot_texrow,
3065 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3066 LyXParagraph * par = this;
3069 (par->depth == depth)
3071 && (par->footnoteflag == footnoteflag)
3076 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3078 if (textclasslist.Style(bparams.textclass,
3079 par->layout).isEnvironment()
3080 || par->pextra_type != PEXTRA_NONE) {
3081 par = par->TeXEnvironment(buf, bparams,
3089 par = par->TeXOnePar(buf, bparams,
3099 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3105 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3106 BufferParams const & bparams,
3107 ostream & os, TexRow & texrow
3110 TexRow & foot_texrow,
3115 bool eindent_open = false;
3117 bool foot_this_level = false;
3119 // flags when footnotetext should be appended to file.
3120 static bool minipage_open = false;
3121 static int minipage_open_depth = 0;
3122 char par_sep = bparams.paragraph_separation;
3124 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3127 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3130 LyXLayout const & style =
3131 textclasslist.Style(bparams.textclass,
3134 if (pextra_type == PEXTRA_INDENT) {
3135 if (!pextra_width.empty()) {
3136 os << "\\begin{LyXParagraphIndent}{"
3137 << pextra_width << "}\n";
3139 //float ib = atof(pextra_widthp.c_str())/100;
3140 // string can't handle floats at present (971109)
3141 // so I'll do a conversion by hand knowing that
3142 // the limits are 0.0 to 1.0. ARRae.
3143 os << "\\begin{LyXParagraphIndent}{";
3144 switch (pextra_widthp.length()) {
3156 os << "\\columnwidth}\n";
3159 eindent_open = true;
3161 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3162 if (pextra_hfill && Previous() &&
3163 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3164 os << "\\hfill{}\n";
3167 if (par_sep == BufferParams::PARSEP_INDENT) {
3168 os << "{\\setlength\\parindent{0pt}\n";
3171 os << "\\begin{minipage}";
3172 switch (pextra_alignment) {
3173 case MINIPAGE_ALIGN_TOP:
3176 case MINIPAGE_ALIGN_MIDDLE:
3179 case MINIPAGE_ALIGN_BOTTOM:
3183 if (!pextra_width.empty()) {
3184 os << '{' << pextra_width << "}\n";
3186 //float ib = atof(par->pextra_width.c_str())/100;
3187 // string can't handle floats at present
3188 // so I'll do a conversion by hand knowing that
3189 // the limits are 0.0 to 1.0. ARRae.
3191 switch (pextra_widthp.length()) {
3203 os << "\\columnwidth}\n";
3206 if (par_sep == BufferParams::PARSEP_INDENT) {
3207 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3210 minipage_open = true;
3211 minipage_open_depth = depth;
3214 #ifdef WITH_WARNINGS
3215 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3216 //I disabled it because it breaks when lists span on several
3219 if (style.isEnvironment()){
3220 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3221 #ifdef FANCY_FOOTNOTE_CODE
3222 if (foot_count < 0) {
3223 // flag that footnote[mark][text] should be
3224 // used for any footnotes from now on
3226 foot_this_level = true;
3229 os << "\\begin{" << style.latexname() << "}{"
3230 << labelwidthstring << "}\n";
3231 } else if (style.labeltype == LABEL_BIBLIO) {
3233 os << "\\begin{" << style.latexname() << "}{"
3234 << bibitemWidest(buf)
3236 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3237 #ifdef FANCY_FOOTNOTE_CODE
3238 if (foot_count < 0) {
3239 // flag that footnote[mark][text] should be
3240 // used for any footnotes from now on
3242 foot_this_level = true;
3245 os << "\\begin{" << style.latexname() << '}'
3246 << style.latexparam() << '\n';
3248 os << "\\begin{" << style.latexname() << '}'
3249 << style.latexparam() << '\n';
3252 LyXParagraph * par = this;
3254 par = par->TeXOnePar(buf, bparams,
3258 foot, foot_texrow, foot_count
3262 if (minipage_open && par && !style.isEnvironment() &&
3263 (par->pextra_type == PEXTRA_MINIPAGE) &&
3264 par->pextra_start_minipage) {
3265 os << "\\end{minipage}\n";
3267 if (par_sep == BufferParams::PARSEP_INDENT) {
3271 minipage_open = false;
3273 if (par && par->depth > depth) {
3274 if (textclasslist.Style(bparams.textclass,
3275 par->layout).isParagraph()
3277 // How to handle this? (Lgb)
3278 //&& !suffixIs(os, "\n\n")
3280 // There should be at least one '\n' already
3281 // but we need there to be two for Standard
3282 // paragraphs that are depth-increment'ed to be
3283 // output correctly. However, tables can
3284 // also be paragraphs so don't adjust them.
3287 // Will it ever harm to have one '\n' too
3288 // many? i.e. that we sometimes will have
3289 // three in a row. (Lgb)
3293 par = par->TeXDeeper(buf, bparams, os, texrow
3295 ,foot, foot_texrow, foot_count
3299 if (par && par->layout == layout && par->depth == depth &&
3300 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3301 if (par->pextra_hfill && par->Previous() &&
3302 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3303 os << "\\hfill{}\n";
3306 if (par_sep == BufferParams::PARSEP_INDENT) {
3307 os << "{\\setlength\\parindent{0pt}\n";
3310 os << "\\begin{minipage}";
3311 switch (par->pextra_alignment) {
3312 case MINIPAGE_ALIGN_TOP:
3315 case MINIPAGE_ALIGN_MIDDLE:
3318 case MINIPAGE_ALIGN_BOTTOM:
3322 if (!par->pextra_width.empty()) {
3323 os << '{' << par->pextra_width << "}\n";
3325 //float ib = atof(par->pextra_widthp.c_str())/100;
3326 // string can't handle floats at present
3327 // so I'll do a conversion by hand knowing that
3328 // the limits are 0.0 to 1.0. ARRae.
3330 switch (par->pextra_widthp.length()) {
3335 os << "0." << par->pextra_widthp;
3338 os << "0.0" << par->pextra_widthp;
3340 os << "\\columnwidth}\n";
3343 if (par_sep == BufferParams::PARSEP_INDENT) {
3344 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3347 minipage_open = true;
3348 minipage_open_depth = par->depth;
3351 && par->layout == layout
3352 && par->depth == depth
3353 && par->pextra_type == pextra_type
3355 && par->footnoteflag == footnoteflag
3359 if (style.isEnvironment()) {
3360 os << "\\end{" << style.latexname() << '}';
3362 // maybe this should go after the minipage closes?
3363 if (foot_this_level) {
3364 if (foot_count >= 1) {
3365 if (foot_count > 1) {
3366 os << "\\addtocounter{footnote}{-"
3371 texrow += foot_texrow;
3373 foot_texrow.reset();
3379 if (minipage_open && (minipage_open_depth == depth) &&
3380 (!par || par->pextra_start_minipage ||
3381 par->pextra_type != PEXTRA_MINIPAGE)) {
3382 os << "\\end{minipage}\n";
3384 if (par_sep == BufferParams::PARSEP_INDENT) {
3388 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3389 os << "\\medskip\n\n";
3393 minipage_open = false;
3396 os << "\\end{LyXParagraphIndent}\n";
3399 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3400 && par->pextra_hfill)) {
3404 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3405 return par; // ale970302
3410 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3411 BufferParams const & bparams,
3412 ostream & os, TexRow & texrow,
3413 ostream & foot, TexRow & foot_texrow,
3417 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3418 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3419 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3420 "No footnote!" << endl;
3422 LyXParagraph * par = this;
3423 LyXLayout const & style =
3424 textclasslist.Style(bparams.textclass,
3425 previous->GetLayout());
3427 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3428 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3429 "Float other than footnote in command"
3430 " with moving argument is illegal" << endl;
3433 if (footnotekind != LyXParagraph::FOOTNOTE
3434 && footnotekind != LyXParagraph::MARGIN
3437 // How to solve this?
3438 //&& !suffixIs(file, '\n')
3440 // we need to ensure that real floats like tables and figures
3441 // have their \begin{} on a new line otherwise we can get
3442 // incorrect results when using the endfloat.sty package
3443 // especially if two floats follow one another. ARRae 981022
3444 // NOTE: if the file is length 0 it must have just been
3445 // written out so we assume it ended with a '\n'
3447 // As far as I can see there is never any harm in writing
3448 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3453 bool moving_arg = false;
3454 bool need_closing = false;
3455 bool is_rtl = isRightToLeftPar(bparams);
3457 if (is_rtl != parent_is_rtl) {
3462 need_closing = true;
3465 bool footer_in_body = true;
3466 switch (footnotekind) {
3467 case LyXParagraph::FOOTNOTE:
3468 if (style.intitle) {
3469 os << "\\thanks{\n";
3470 footer_in_body = false;
3473 if (foot_count == -1) {
3474 // we're at depth 0 so we can use:
3475 os << "\\footnote{%\n";
3476 footer_in_body = false;
3478 os << "\\footnotemark{}%\n";
3480 // we only need this when there are
3481 // multiple footnotes
3482 os << "\\stepcounter{footnote}";
3484 os << "\\footnotetext{%\n";
3485 foot_texrow.start(this, 0);
3486 foot_texrow.newline();
3491 case LyXParagraph::MARGIN:
3492 os << "\\marginpar{\n";
3494 case LyXParagraph::FIG:
3495 if (pextra_type == PEXTRA_FLOATFLT
3496 && (!pextra_width.empty()
3497 || !pextra_widthp.empty())) {
3498 if (!pextra_width.empty())
3499 os << "\\begin{floatingfigure}{"
3500 << pextra_width << "}\n";
3502 os << "\\begin{floatingfigure}{"
3503 << lyx::atoi(pextra_widthp) / 100.0
3504 << "\\textwidth}\n";
3506 os << "\\begin{figure}";
3507 if (!bparams.float_placement.empty()) {
3508 os << '[' << bparams.float_placement << "]\n";
3514 case LyXParagraph::TAB:
3515 os << "\\begin{table}";
3516 if (!bparams.float_placement.empty()) {
3517 os << '[' << bparams.float_placement << "]\n";
3522 case LyXParagraph::WIDE_FIG:
3523 os << "\\begin{figure*}";
3524 if (!bparams.float_placement.empty()) {
3525 os << '[' << bparams.float_placement << "]\n";
3530 case LyXParagraph::WIDE_TAB:
3531 os << "\\begin{table*}";
3532 if (!bparams.float_placement.empty()) {
3533 os << '[' << bparams.float_placement << "]\n";
3538 case LyXParagraph::ALGORITHM:
3539 os << "\\begin{algorithm}\n";
3544 if (footnotekind != LyXParagraph::FOOTNOTE
3545 || !footer_in_body) {
3546 // Process text for all floats except footnotes in body
3548 LyXLayout const & style =
3550 .Style(bparams.textclass, par->layout);
3552 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3554 if (style.isEnvironment()
3555 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3556 // Allows the use of minipages within float
3557 // environments. Shouldn't be circular because
3558 // we don't support footnotes inside
3559 // floats (yet). ARRae
3560 par = par->TeXEnvironment(buf, bparams, os,
3565 par = par->TeXOnePar(buf, bparams,
3566 os, texrow, moving_arg,
3571 if (par && !par->IsDummy() && par->depth > depth) {
3572 par = par->TeXDeeper(buf, bparams, os, texrow,
3576 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3578 // process footnotes > depth 0 or in environments separately
3579 // NOTE: Currently don't support footnotes within footnotes
3580 // even though that is possible using the \footnotemark
3581 std::ostringstream dummy;
3582 TexRow dummy_texrow;
3583 int dummy_count = 0;
3585 LyXLayout const & style =
3587 .Style(bparams.textclass, par->layout);
3589 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3591 if (style.isEnvironment()
3592 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3593 // Allows the use of minipages within float
3594 // environments. Shouldn't be circular because
3595 // we don't support footnotes inside
3596 // floats (yet). ARRae
3597 par = par->TeXEnvironment(buf, bparams,
3599 dummy, dummy_texrow,
3602 par = par->TeXOnePar(buf, bparams,
3605 dummy, dummy_texrow,
3609 if (par && !par->IsDummy() && par->depth > depth) {
3610 par = par->TeXDeeper(buf, bparams,
3612 dummy, dummy_texrow,
3616 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3618 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3619 "Footnote in a Footnote -- not supported"
3624 switch (footnotekind) {
3625 case LyXParagraph::FOOTNOTE:
3626 if (footer_in_body) {
3627 // This helps tell which of the multiple
3628 // footnotetexts an error was in.
3630 foot_texrow.newline();
3635 case LyXParagraph::MARGIN:
3638 case LyXParagraph::FIG:
3639 if (pextra_type == PEXTRA_FLOATFLT
3640 && (!pextra_width.empty()
3641 || !pextra_widthp.empty()))
3642 os << "\\end{floatingfigure}";
3644 os << "\\end{figure}";
3646 case LyXParagraph::TAB:
3647 os << "\\end{table}";
3649 case LyXParagraph::WIDE_FIG:
3650 os << "\\end{figure*}";
3652 case LyXParagraph::WIDE_TAB:
3653 os << "\\end{table*}";
3655 case LyXParagraph::ALGORITHM:
3656 os << "\\end{algorithm}";
3663 if (footnotekind != LyXParagraph::FOOTNOTE
3664 && footnotekind != LyXParagraph::MARGIN) {
3665 // we need to ensure that real floats like tables and figures
3666 // have their \end{} on a line of their own otherwise we can
3667 // get incorrect results when using the endfloat.sty package.
3672 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3677 bool LyXParagraph::IsDummy() const
3679 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3680 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3684 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3685 int type, string const & width,
3686 string const & widthp)
3689 pextra_width = width;
3690 pextra_widthp = widthp;
3692 if (textclasslist.Style(bparams.textclass,
3693 layout).isEnvironment()) {
3694 LyXParagraph * par = this;
3695 LyXParagraph * ppar = par;
3697 while (par && (par->layout == layout)
3698 && (par->depth == depth)) {
3700 par = par->Previous();
3703 par = par->FirstPhysicalPar();
3705 while (par && par->depth > depth) {
3706 par = par->Previous();
3709 par = par->FirstPhysicalPar();
3714 while (par && (par->layout == layout)
3715 && (par->depth == depth)) {
3716 par->pextra_type = type;
3717 par->pextra_width = width;
3718 par->pextra_widthp = widthp;
3720 par = par->NextAfterFootnote();
3724 if (par && (par->depth > depth))
3725 par->SetPExtraType(bparams,
3726 type, width, widthp);
3728 while (par && ((par->depth > depth) || par->IsDummy()))
3729 par = par->NextAfterFootnote();
3731 while (par && ((par->depth > depth)))
3739 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3741 if (pextra_type == PEXTRA_NONE)
3744 pextra_type = PEXTRA_NONE;
3745 pextra_width.erase();
3746 pextra_widthp.erase();
3748 if (textclasslist.Style(bparams.textclass,
3749 layout).isEnvironment()) {
3750 LyXParagraph * par = this;
3751 LyXParagraph * ppar = par;
3753 while (par && (par->layout == layout)
3754 && (par->depth == depth)) {
3756 par = par->Previous();
3759 par = par->FirstPhysicalPar();
3761 while (par && par->depth > depth) {
3762 par = par->Previous();
3765 par = par->FirstPhysicalPar();
3770 while (par && (par->layout == layout)
3771 && (par->depth == depth)) {
3772 par->pextra_type = PEXTRA_NONE;
3773 par->pextra_width.erase();
3774 par->pextra_widthp.erase();
3776 par = par->NextAfterFootnote();
3780 if (par && (par->depth > depth))
3781 par->UnsetPExtraType(bparams);
3783 while (par && ((par->depth > depth) || par->IsDummy()))
3784 par = par->NextAfterFootnote();
3786 while (par && ((par->depth > depth)))
3794 bool LyXParagraph::IsHfill(size_type pos) const
3796 return IsHfillChar(GetChar(pos));
3800 bool LyXParagraph::IsInset(size_type pos) const
3802 return IsInsetChar(GetChar(pos));
3807 bool LyXParagraph::IsFloat(size_type pos) const
3809 return IsFloatChar(GetChar(pos));
3814 bool LyXParagraph::IsNewline(size_type pos) const
3816 return pos >= 0 && IsNewlineChar(GetChar(pos));
3820 bool LyXParagraph::IsSeparator(size_type pos) const
3822 return IsSeparatorChar(GetChar(pos));
3826 bool LyXParagraph::IsLineSeparator(size_type pos) const
3828 return IsLineSeparatorChar(GetChar(pos));
3832 bool LyXParagraph::IsKomma(size_type pos) const
3834 return IsKommaChar(GetChar(pos));
3838 /// Used by the spellchecker
3839 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3841 value_type c = GetChar(pos);
3842 if (IsLetterChar(c))
3844 // '\0' is not a letter, allthough every string contains "" (below)
3847 // We want to pass the ' and escape chars to ispell
3848 string extra = lyxrc.isp_esc_chars + '\'';
3849 char ch[2] = { c, 0 };
3850 return contains(extra, ch);
3854 bool LyXParagraph::IsWord(size_type pos ) const
3856 return IsWordChar(GetChar(pos)) ;
3861 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3865 return FirstPhysicalPar()->getParLanguage(bparams);
3869 #ifdef DO_USE_DEFAULT_LANGUAGE
3870 Language const * lang = GetFirstFontSettings().language();
3872 if (lang->lang() == "default")
3873 return bparams.language;
3876 return GetFirstFontSettings().language();
3878 } else if (previous)
3879 return previous->getParLanguage(bparams);
3881 return bparams.language;
3885 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3887 return lyxrc.rtl_support
3888 && getParLanguage(bparams)->RightToLeft();
3892 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3893 Language const * from, Language const * to)
3895 for (size_type i = 0; i < size(); ++i) {
3896 LyXFont font = GetFontSettings(bparams, i);
3897 if (font.language() == from) {
3898 font.setLanguage(to);
3905 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3907 Language const * doc_language = bparams.language;
3908 for (FontList::const_iterator cit = fontlist.begin();
3909 cit != fontlist.end(); ++cit)
3910 if ((*cit).font.language() != doc_language)
3916 // Convert the paragraph to a string.
3917 // Used for building the table of contents
3918 string const LyXParagraph::String(Buffer const * buffer, bool label)
3920 BufferParams const & bparams = buffer->params;
3923 if (label && !IsDummy() && !labelstring.empty())
3925 if (label && !labelstring.empty())
3927 s += labelstring + ' ';
3928 string::size_type len = s.size();
3930 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3931 value_type c = GetChar(i);
3934 else if (c == META_INSET &&
3935 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3936 std::ostringstream ost;
3937 GetInset(i)->Ascii(buffer, ost);
3938 s += subst(ost.str().c_str(),'\n',' ');
3943 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
3944 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
3945 s += NextAfterFootnote()->String(buffer, false);
3949 if (isRightToLeftPar(bparams))
3950 reverse(s.begin() + len,s.end());
3958 string const LyXParagraph::String(Buffer const * buffer,
3959 LyXParagraph::size_type beg,
3960 LyXParagraph::size_type end)
3965 if (beg == 0 && !IsDummy() && !labelstring.empty())
3967 if (beg == 0 && !labelstring.empty())
3969 s += labelstring + ' ';
3971 for (LyXParagraph::size_type i = beg; i < end; ++i) {
3972 value_type c = GetChar(i);
3975 else if (c == META_INSET) {
3976 std::ostringstream ost;
3977 GetInset(i)->Ascii(buffer, ost);
3978 s += ost.str().c_str();
3986 void LyXParagraph::SetInsetOwner(Inset * i)
3989 for (InsetList::const_iterator cit = insetlist.begin();
3990 cit != insetlist.end(); ++cit) {
3992 (*cit).inset->setOwner(i);
3997 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4000 for (InsetList::const_iterator cit = insetlist.begin();
4001 cit != insetlist.end(); ++cit) {
4003 if ((*cit).inset->IsTextInset()) {
4004 static_cast<UpdatableInset *>
4005 ((*cit).inset)->deleteLyXText(bv);
4012 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4015 for (InsetList::const_iterator cit = insetlist.begin();
4016 cit != insetlist.end(); ++cit) {
4018 if ((*cit).inset->IsTextInset()) {
4019 static_cast<UpdatableInset *>
4020 ((*cit).inset)->resizeLyXText(bv);