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)
53 extern BufferView * current_view;
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 // Initialization of the counter for the paragraph id's,
63 // declared in lyxparagraph.h
64 unsigned int LyXParagraph::paragraph_id = 0;
67 LyXParagraph::LyXParagraph()
69 text.reserve(500); // is this number too big?
71 for (int i = 0; i < 10; ++i) setCounter(i , 0);
78 footnoteflag = LyXParagraph::NO_FOOTNOTE;
79 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
81 align = LYX_ALIGN_BLOCK;
85 bibkey = 0; // ale970302
90 // This konstruktor inserts the new paragraph in a list.
91 LyXParagraph::LyXParagraph(LyXParagraph * par)
94 par->text.resize(par->text.size());
96 for (int i = 0; i < 10; ++i) setCounter(i, 0);
100 // double linked list begin
103 next->previous = this;
105 previous->next = this;
108 footnoteflag = LyXParagraph::NO_FOOTNOTE;
109 footnotekind = LyXParagraph::FOOTNOTE;
112 id_ = paragraph_id++;
114 bibkey = 0; // ale970302
120 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
121 BufferParams const & params,
122 char footflag, char dth) const
126 footnoteflag != LyXParagraph::NO_FOOTNOTE ||
128 || previous->footnoteflag == LyXParagraph::NO_FOOTNOTE
133 // The beginning or the end of a footnote environment?
134 if (footflag != footnoteflag) {
135 footflag = footnoteflag;
137 os << "\n\\begin_float "
138 << string_footnotekinds[footnotekind]
141 os << "\n\\end_float ";
145 // The beginning or end of a deeper (i.e. nested) area?
148 while (depth > dth) {
149 os << "\n\\begin_deeper ";
153 while (depth < dth) {
154 os << "\n\\end_deeper ";
160 // First write the layout
162 << textclasslist.NameOfLayout(params.textclass, layout)
165 // Maybe some vertical spaces.
166 if (added_space_top.kind() != VSpace::NONE)
167 os << "\\added_space_top "
168 << added_space_top.asLyXCommand() << " ";
169 if (added_space_bottom.kind() != VSpace::NONE)
170 os << "\\added_space_bottom "
171 << added_space_bottom.asLyXCommand() << " ";
173 // Maybe the paragraph has special spacing
174 spacing.writeFile(os, true);
176 // The labelwidth string used in lists.
177 if (!labelwidthstring.empty())
178 os << "\\labelwidthstring "
179 << labelwidthstring << '\n';
181 // Lines above or below?
185 os << "\\line_bottom ";
187 // Pagebreaks above or below?
189 os << "\\pagebreak_top ";
190 if (pagebreak_bottom)
191 os << "\\pagebreak_bottom ";
193 // Start of appendix?
194 if (start_of_appendix)
195 os << "\\start_of_appendix ";
202 if (align != LYX_ALIGN_LAYOUT) {
205 case LYX_ALIGN_LEFT: h = 1; break;
206 case LYX_ALIGN_RIGHT: h = 2; break;
207 case LYX_ALIGN_CENTER: h = 3; break;
208 default: h = 0; break;
210 os << "\\align " << string_align[h] << " ";
212 if (pextra_type != PEXTRA_NONE) {
213 os << "\\pextra_type " << pextra_type;
214 if (pextra_type == PEXTRA_MINIPAGE) {
215 os << " \\pextra_alignment "
218 os << " \\pextra_hfill "
220 if (pextra_start_minipage)
221 os << " \\pextra_start_minipage "
222 << pextra_start_minipage;
224 if (!pextra_width.empty()) {
225 os << " \\pextra_width "
226 << VSpace(pextra_width).asLyXCommand();
227 } else if (!pextra_widthp.empty()) {
228 os << " \\pextra_widthp "
235 // Dummy layout. This means that a footnote ended.
236 os << "\n\\end_float ";
237 footflag = LyXParagraph::NO_FOOTNOTE;
243 bibkey->Write(buf, os);
245 LyXFont font1(LyXFont::ALL_INHERIT, params.language_info);
248 for (size_type i = 0; i < size(); ++i) {
254 // Write font changes
255 LyXFont font2 = GetFontSettings(params, i);
256 if (font2 != font1) {
257 font2.lyxWriteChanges(font1, os);
262 value_type c = GetChar(i);
266 Inset const * inset = GetInset(i);
268 if (inset->DirectWrite()) {
269 // international char, let it write
270 // code directly so it's shorter in
272 inset->Write(buf, os);
274 os << "\n\\begin_inset ";
275 inset->Write(buf, os);
276 os << "\n\\end_inset \n\n";
282 os << "\n\\newline \n";
286 os << "\n\\hfill \n";
290 os << "\n\\backslash \n";
294 if (i + 1 < size() && GetChar(i + 1) == ' ') {
301 if ((column > 70 && c == ' ')
306 // this check is to amend a bug. LyX sometimes
307 // inserts '\0' this could cause problems.
311 lyxerr << "ERROR (LyXParagraph::writeFile):"
312 " NULL char in structure." << endl;
318 // now write the next paragraph
320 next->writeFile(buf, os, params, footflag, dth);
324 void LyXParagraph::validate(LaTeXFeatures & features) const
326 BufferParams const & params = features.bufferParams();
329 // this will be useful later
330 LyXLayout const & layout =
331 textclasslist.Style(params.textclass,
336 if (line_top || line_bottom)
337 features.lyxline = true;
338 if (!spacing.isDefault())
339 features.setspace = true;
342 features.layout[GetLayout()] = true;
345 Language const * doc_language = params.language_info;
347 for (FontList::const_iterator cit = fontlist.begin();
348 cit != fontlist.end(); ++cit) {
349 if ((*cit).font.noun() == LyXFont::ON) {
350 lyxerr[Debug::LATEX] << "font.noun: "
351 << (*cit).font.noun()
353 features.noun = true;
354 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
355 << (*cit).font.stateText(0)
358 switch ((*cit).font.color()) {
360 case LColor::inherit:
364 features.color = true;
365 lyxerr[Debug::LATEX] << "Color enabled. Font: "
366 << (*cit).font.stateText(0)
370 Language const * language = (*cit).font.language();
371 if (language != doc_language) {
372 features.UsedLanguages.insert(language);
373 lyxerr[Debug::LATEX] << "Found language "
374 << language->lang() << endl;
379 for (InsetList::const_iterator cit = insetlist.begin();
380 cit != insetlist.end(); ++cit) {
382 (*cit).inset->Validate(features);
385 if (pextra_type == PEXTRA_INDENT)
386 features.LyXParagraphIndent = true;
387 if (pextra_type == PEXTRA_FLOATFLT)
388 features.floatflt = true;
390 if (layout.needprotect
391 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
392 features.NeedLyXFootnoteCode = true;
394 if (params.paragraph_separation == BufferParams::PARSEP_INDENT
395 && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
396 features.NeedLyXMinipageIndent = true;
398 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
399 features.algorithm = true;
404 // First few functions needed for cut and paste and paragraph breaking.
405 void LyXParagraph::CopyIntoMinibuffer(BufferParams const & bparams,
406 LyXParagraph::size_type pos) const
408 minibuffer_char = GetChar(pos);
409 minibuffer_font = GetFontSettings(bparams, pos);
410 minibuffer_inset = 0;
411 if (minibuffer_char == LyXParagraph::META_INSET) {
413 minibuffer_inset = GetInset(pos)->Clone(*current_view->buffer());
415 minibuffer_inset = 0;
416 minibuffer_char = ' ';
417 // This reflects what GetInset() does (ARRae)
423 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
424 LyXParagraph::size_type pos)
426 minibuffer_char = GetChar(pos);
427 minibuffer_font = GetFontSettings(bparams, pos);
428 minibuffer_inset = 0;
429 if (minibuffer_char == LyXParagraph::META_INSET) {
431 minibuffer_inset = GetInset(pos);
432 // This is a little hack since I want exactly
433 // the inset, not just a clone. Otherwise
434 // the inset would be deleted when calling Erase(pos)
436 InsetTable search_elem(pos, 0);
437 InsetList::iterator it =
438 lower_bound(insetlist.begin(),
440 search_elem, matchIT());
441 if (it != insetlist.end() && (*it).pos == pos)
444 minibuffer_inset = 0;
445 minibuffer_char = ' ';
446 // This reflects what GetInset() does (ARRae)
451 // Erase(pos); now the caller is responsible for that.
455 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
457 if ((minibuffer_char == LyXParagraph::META_INSET) &&
458 !InsertInsetAllowed(minibuffer_inset))
460 if (minibuffer_char == LyXParagraph::META_INSET)
461 InsertInset(pos, minibuffer_inset, minibuffer_font);
463 InsertChar(pos, minibuffer_char, minibuffer_font);
471 void LyXParagraph::Clear()
476 pagebreak_top = false;
477 pagebreak_bottom = false;
479 added_space_top = VSpace(VSpace::NONE);
480 added_space_bottom = VSpace(VSpace::NONE);
481 spacing.set(Spacing::Default);
483 align = LYX_ALIGN_LAYOUT;
487 pextra_type = PEXTRA_NONE;
488 pextra_width.erase();
489 pextra_widthp.erase();
490 pextra_alignment = MINIPAGE_ALIGN_TOP;
491 pextra_hfill = false;
492 pextra_start_minipage = false;
495 labelwidthstring.erase();
499 start_of_appendix = false;
503 // the destructor removes the new paragraph from the list
504 LyXParagraph::~LyXParagraph()
507 previous->next = next;
509 next->previous = previous;
511 for (InsetList::iterator it = insetlist.begin();
512 it != insetlist.end(); ++it) {
519 //lyxerr << "LyXParagraph::paragraph_id = "
520 // << LyXParagraph::paragraph_id << endl;
524 void LyXParagraph::Erase(LyXParagraph::size_type pos)
527 // > because last is the next unused position, and you can
528 // use it if you want
530 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
531 NextAfterFootnote()->Erase(pos - text.size() - 1);
533 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
534 "position does not exist." << endl;
538 Assert(pos < size());
541 if (pos < size()) { // last is free for insertation, but should be empty
543 // if it is an inset, delete the inset entry
544 if (text[pos] == LyXParagraph::META_INSET) {
546 InsetTable search_inset(pos, 0);
547 InsetList::iterator it =
548 lower_bound(insetlist.begin(),
550 search_inset, matchIT());
551 if (it != insetlist.end() && (*it).pos == pos) {
556 text.erase(text.begin() + pos);
558 // Erase entries in the tables.
559 FontTable search_font(pos, LyXFont());
561 FontList::iterator it =
562 lower_bound(fontlist.begin(),
564 search_font, matchFT());
565 if (it != fontlist.end() && (*it).pos == pos &&
567 (it != fontlist.begin() && (*(it-1)).pos == pos - 1))) {
568 // If it is a multi-character font
569 // entry, we just make it smaller
570 // (see update below), otherwise we
572 unsigned int i = it - fontlist.begin();
573 fontlist.erase(fontlist.begin() + i);
574 it = fontlist.begin() + i;
575 if (i > 0 && i < fontlist.size() &&
576 fontlist[i-1].font == fontlist[i].font) {
577 fontlist.erase(fontlist.begin() + i-1);
578 it = fontlist.begin() + i-1;
582 // Update all other entries.
583 FontList::iterator fend = fontlist.end();
584 for (; it != fend; ++it)
587 // Update the inset table.
588 InsetTable search_inset(pos, 0);
589 InsetList::iterator lend = insetlist.end();
590 for (InsetList::iterator it =
591 upper_bound(insetlist.begin(),
593 search_inset, matchIT());
598 lyxerr << "ERROR (LyXParagraph::Erase): "
599 "can't erase non-existant char." << endl;
605 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
606 LyXParagraph::value_type c)
608 LyXFont f(LyXFont::ALL_INHERIT);
609 InsertChar(pos, c, f);
613 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
614 LyXParagraph::value_type c,
615 LyXFont const & font)
618 // > because last is the next unused position, and you can
619 // use it if you want
622 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
623 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
626 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
627 "position does not exist." << endl;
631 Assert(pos <= size());
633 text.insert(text.begin() + pos, c);
634 // Update the font table.
635 FontTable search_font(pos, LyXFont());
636 for (FontList::iterator it = lower_bound(fontlist.begin(),
638 search_font, matchFT());
639 it != fontlist.end(); ++it)
642 // Update the inset table.
643 InsetTable search_inset(pos, 0);
644 for (InsetList::iterator it = lower_bound(insetlist.begin(),
646 search_inset, matchIT());
647 it != insetlist.end(); ++it)
654 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
657 LyXFont f(LyXFont::ALL_INHERIT);
658 InsertInset(pos, inset, f);
662 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
663 Inset * inset, LyXFont const & font)
668 // > because last is the next unused position, and you can
669 // use it if you want
672 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
674 ->InsertInset(pos - text.size() - 1,
677 lyxerr << "ERROR (LyXParagraph::InsertInset): "
678 "position does not exist: " << pos << endl;
682 Assert(pos <= size());
685 InsertChar(pos, META_INSET, font);
686 Assert(text[pos] == META_INSET);
688 // Add a new entry in the inset table.
689 InsetTable search_inset(pos, 0);
690 InsetList::iterator it = lower_bound(insetlist.begin(),
692 search_inset, matchIT());
693 if (it != insetlist.end() && (*it).pos == pos)
694 lyxerr << "ERROR (LyXParagraph::InsertInset): "
695 "there is an inset in position: " << pos << endl;
697 insetlist.insert(it, InsetTable(pos, inset));
699 inset->setOwner(inset_owner);
703 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
705 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
708 return inset_owner->InsertInsetAllowed(inset);
713 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
718 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
719 return NextAfterFootnote()
720 ->GetInset(pos - text.size() - 1);
722 lyxerr << "ERROR (LyXParagraph::GetInset): "
723 "position does not exist: "
729 Assert(pos < size());
732 InsetTable search_inset(pos, 0);
733 InsetList::iterator it = lower_bound(insetlist.begin(),
735 search_inset, matchIT());
736 if (it != insetlist.end() && (*it).pos == pos)
739 lyxerr << "ERROR (LyXParagraph::GetInset): "
740 "Inset does not exist: " << pos << endl;
743 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
744 // Did this commenting out introduce a bug? So far I have not
745 // see any, please enlighten me. (Lgb)
746 // My guess is that since the inset does not exist, we might
747 // as well replace it with a space to prevent craches. (Asger)
752 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
757 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
758 return NextAfterFootnote()
759 ->GetInset(pos - text.size() - 1);
761 lyxerr << "ERROR (LyXParagraph::GetInset): "
762 "position does not exist: "
768 Assert(pos < size());
771 InsetTable search_inset(pos, 0);
772 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
774 search_inset, matchIT());
775 if (cit != insetlist.end() && (*cit).pos == pos)
778 lyxerr << "ERROR (LyXParagraph::GetInset): "
779 "Inset does not exist: " << pos << endl;
781 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
782 // Did this commenting out introduce a bug? So far I have not
783 // see any, please enlighten me. (Lgb)
784 // My guess is that since the inset does not exist, we might
785 // as well replace it with a space to prevent craches. (Asger)
790 // Gets uninstantiated font setting at position.
791 // Optimized after profiling. (Asger)
792 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
793 LyXParagraph::size_type pos) const
796 Assert(pos <= size());
801 FontTable search_font(pos, LyXFont());
802 FontList::const_iterator cit = lower_bound(fontlist.begin(),
804 search_font, matchFT());
805 if (cit != fontlist.end())
811 // > because last is the next unused position, and you can
812 // use it if you want
813 else if (pos > size()) {
815 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
816 return NextAfterFootnote()
817 ->GetFontSettings(bparams,
818 pos - text.size() - 1);
820 // Why is it an error to ask for the font of a
821 // position that does not exist? Would it be
822 // enough for this to be enabled on debug?
823 // We want strict error checking, but it's ok to only
824 // have it when debugging. (Asger)
825 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
826 "position does not exist. "
827 << pos << " (" << static_cast<int>(pos)
831 return GetFontSettings(bparams, pos - 1);
834 if (pos == size() && size())
835 return GetFontSettings(bparams, pos - 1);
839 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
841 //return LyXFont(LyXFont::ALL_INHERIT);
844 // Gets uninstantiated font setting at position 0
845 LyXFont const LyXParagraph::GetFirstFontSettings() const
848 if (!fontlist.empty())
849 return fontlist[0].font;
853 else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
854 return NextAfterFootnote()->GetFirstFontSettings();
856 return LyXFont(LyXFont::ALL_INHERIT);
860 // Gets the fully instantiated font at a given position in a paragraph
861 // This is basically the same function as LyXText::GetFont() in text2.C.
862 // The difference is that this one is used for generating the LaTeX file,
863 // and thus cosmetic "improvements" are disallowed: This has to deliver
864 // the true picture of the buffer. (Asger)
865 // If position is -1, we get the layout font of the paragraph.
866 // If position is -2, we get the font of the manual label of the paragraph.
867 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
868 LyXParagraph::size_type pos) const
871 LyXLayout const & layout =
872 textclasslist.Style(bparams.textclass,
874 LyXParagraph::size_type main_body = 0;
875 if (layout.labeltype == LABEL_MANUAL)
876 main_body = BeginningOfMainBody();
881 layoutfont = layout.labelfont;
883 layoutfont = layout.font;
884 tmpfont = GetFontSettings(bparams, pos);
885 tmpfont.realize(layoutfont);
887 // process layoutfont for pos == -1 and labelfont for pos < -1
889 tmpfont = layout.font;
891 tmpfont = layout.labelfont;
892 tmpfont.setLanguage(getParLanguage(bparams));
895 // check for environment font information
896 char par_depth = GetDepth();
897 LyXParagraph const * par = this;
898 while (par && par_depth && !tmpfont.resolved()) {
899 par = par->DepthHook(par_depth - 1);
901 tmpfont.realize(textclasslist.
902 Style(bparams.textclass,
903 par->GetLayout()).font);
904 par_depth = par->GetDepth();
908 tmpfont.realize(textclasslist
909 .TextClass(bparams.textclass)
915 /// Returns the height of the highest font in range
917 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
918 LyXParagraph::size_type endpos) const
920 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
921 if (fontlist.empty())
924 FontTable end_search(endpos, LyXFont());
925 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
927 end_search, matchFT());
928 if (end_it != fontlist.end())
931 FontTable start_search(startpos, LyXFont());
932 for (FontList::const_iterator cit =
933 lower_bound(fontlist.begin(),
935 start_search, matchFT());
936 cit != end_it; ++cit) {
937 LyXFont::FONT_SIZE size = (*cit).font.size();
938 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
945 LyXParagraph::value_type
946 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
951 Assert(pos <= size());
952 if (!size() || pos == size()) return '\0';
963 // > because last is the next unused position, and you can
964 // use it if you want
965 else if (pos > size()) {
966 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
967 return NextAfterFootnote()
968 ->GetChar(pos - text.size() - 1);
971 lyxerr << "ERROR (LyXParagraph::GetChar const): "
972 "position does not exist."
973 << pos << " (" << static_cast<int>(pos)
979 // We should have a footnote environment.
980 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
981 // Notice that LyX does request the
982 // last char from time to time. (Asger)
983 //lyxerr << "ERROR (LyXParagraph::GetChar): "
984 // "expected footnote." << endl;
987 switch (next->footnotekind) {
988 case LyXParagraph::FOOTNOTE:
989 return LyXParagraph::META_FOOTNOTE;
990 case LyXParagraph::MARGIN:
991 return LyXParagraph::META_MARGIN;
992 case LyXParagraph::FIG:
993 case LyXParagraph::WIDE_FIG:
994 return LyXParagraph::META_FIG;
995 case LyXParagraph::TAB:
996 case LyXParagraph::WIDE_TAB:
997 return LyXParagraph::META_TAB;
998 case LyXParagraph::ALGORITHM:
999 return LyXParagraph::META_ALGORITHM;
1001 return '\0'; // to shut up gcc
1007 // return an string of the current word, and the end of the word in lastpos.
1008 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1010 Assert(lastpos >= 0);
1012 // the current word is defined as starting at the first character
1013 // from the immediate left of lastpospos which meets the definition
1014 // of IsLetter(), continuing to the last character to the right
1015 // of this meeting IsLetter.
1021 // move back until we have a letter
1023 //there's no real reason to have firstpos & lastpos as
1024 //separate variables as this is written, but maybe someon
1025 // will want to return firstpos in the future.
1027 //since someone might have typed a punctuation first
1028 int firstpos = lastpos;
1030 while ((firstpos >= 0) && !IsLetter(firstpos))
1033 // now find the beginning by looking for a nonletter
1035 while ((firstpos>= 0) && IsLetter(firstpos))
1038 // the above is now pointing to the preceeding non-letter
1042 // so copy characters into theword until we get a nonletter
1043 // note that this can easily exceed lastpos, wich means
1044 // that if used in the middle of a word, the whole word
1047 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1055 #warning Remember to get rid of this one. (Lgb)
1057 LyXParagraph::size_type LyXParagraph::Last() const
1060 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1061 return text.size() + NextAfterFootnote()->Last() + 1;
1062 // the 1 is the symbol
1071 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1073 // > because last is the next unused position, and you can
1074 // use it if you want
1077 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1078 return NextAfterFootnote()
1079 ->ParFromPos(pos - text.size() - 1);
1081 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1082 "position does not exist." << endl;
1091 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1093 // > because last is the next unused position, and you can
1094 // use it if you want
1097 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1098 return NextAfterFootnote()
1099 ->PositionInParFromPos(pos - text.size() - 1);
1102 "ERROR (LyXParagraph::PositionInParFromPos): "
1103 "position does not exist." << endl;
1112 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1113 LyXFont const & font)
1116 // > because last is the next unused position, and you can
1117 // use it if you want
1120 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1121 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1124 lyxerr << "ERROR (LyXParagraph::SetFont): "
1125 "position does not exist." << endl;
1130 Assert(pos <= size());
1133 // First, reduce font against layout/label font
1134 // Update: The SetCharFont() routine in text2.C already
1135 // reduces font, so we don't need to do that here. (Asger)
1136 // No need to simplify this because it will disappear
1137 // in a new kernel. (Asger)
1138 // Next search font table
1140 FontTable search_font(pos, LyXFont());
1141 FontList::iterator it = lower_bound(fontlist.begin(),
1143 search_font, matchFT());
1144 unsigned int i = it - fontlist.begin();
1145 bool notfound = it == fontlist.end();
1147 if (!notfound && fontlist[i].font == font)
1150 bool begin = pos == 0 || notfound ||
1151 (i > 0 && fontlist[i-1].pos == pos - 1);
1152 // Is position pos is a beginning of a font block?
1153 bool end = !notfound && fontlist[i].pos == pos;
1154 // Is position pos is the end of a font block?
1155 if (begin && end) { // A single char block
1156 if (i+1 < fontlist.size() &&
1157 fontlist[i+1].font == font) {
1158 // Merge the singleton block with the next block
1159 fontlist.erase(fontlist.begin() + i);
1160 if (i > 0 && fontlist[i-1].font == font)
1161 fontlist.erase(fontlist.begin() + i-1);
1162 } else if (i > 0 && fontlist[i-1].font == font) {
1163 // Merge the singleton block with the previous block
1164 fontlist[i-1].pos = pos;
1165 fontlist.erase(fontlist.begin() + i);
1167 fontlist[i].font = font;
1169 if (i > 0 && fontlist[i-1].font == font)
1170 fontlist[i-1].pos = pos;
1172 fontlist.insert(fontlist.begin() + i,
1173 FontTable(pos, font));
1175 fontlist[i].pos = pos - 1;
1176 if (!(i+1 < fontlist.size() &&
1177 fontlist[i+1].font == font))
1178 fontlist.insert(fontlist.begin() + i+1,
1179 FontTable(pos, font));
1180 } else { // The general case. The block is splitted into 3 blocks
1181 fontlist.insert(fontlist.begin() + i,
1182 FontTable(pos - 1, fontlist[i].font));
1183 fontlist.insert(fontlist.begin() + i+1, FontTable(pos, font));
1188 // This function is able to hide closed footnotes.
1189 LyXParagraph * LyXParagraph::Next()
1192 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1193 LyXParagraph * tmp = next;
1195 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1197 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1198 return tmp->Next(); /* there can be more than one
1199 footnote in a logical
1202 return next; // This should never happen!
1210 LyXParagraph * LyXParagraph::NextAfterFootnote()
1212 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1213 LyXParagraph * tmp = next;
1214 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1216 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1217 return tmp; /* there can be more than one footnote
1218 in a logical paragraph */
1220 return next; // This should never happen!
1228 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1230 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1231 LyXParagraph * tmp = next;
1232 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1234 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1235 return tmp; /* there can be more than one footnote
1236 in a logical paragraph */
1238 return next; // This should never happen!
1246 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1249 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1251 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1252 tmp = tmp->previous;
1253 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1254 return tmp; /* there can be more than one footnote
1255 in a logical paragraph */
1257 return previous; // This should never happen!
1265 LyXParagraph * LyXParagraph::LastPhysicalPar()
1267 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1270 LyXParagraph * tmp = this;
1272 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1273 tmp = tmp->NextAfterFootnote();
1281 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1283 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1286 LyXParagraph const * tmp = this;
1288 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1289 tmp = tmp->NextAfterFootnote();
1297 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1301 LyXParagraph * tmppar = this;
1305 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1306 tmppar = tmppar->previous;
1317 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1321 LyXParagraph const * tmppar = this;
1325 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1326 tmppar = tmppar->previous;
1336 // This function is able to hide closed footnotes.
1337 LyXParagraph * LyXParagraph::Previous()
1340 LyXParagraph * tmp = previous;
1345 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1346 tmp = tmp->previous;
1348 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1349 tmp = tmp->previous;
1350 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1351 return tmp->next->Previous();
1361 // This function is able to hide closed footnotes.
1362 LyXParagraph const * LyXParagraph::Previous() const
1365 LyXParagraph * tmp = previous;
1369 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1370 tmp = tmp->previous;
1372 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1373 tmp = tmp->previous;
1374 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1375 return tmp->next->Previous();
1385 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1386 LyXParagraph::size_type pos,
1391 // create a new paragraph
1394 size_type pos_first;
1395 LyXParagraph * par = ParFromPos(pos);
1396 LyXParagraph * firstpar = FirstPhysicalPar();
1398 LyXParagraph * tmp = new LyXParagraph(par);
1400 //LyXParagraph * par = this;
1401 //LyXParagraph * firstpar = this;
1402 LyXParagraph * tmp = new LyXParagraph(this);
1406 tmp->footnoteflag = footnoteflag;
1407 tmp->footnotekind = footnotekind;
1409 // this is an idea for a more userfriendly layout handling, I will
1410 // see what the users say
1413 // layout stays the same with latex-environments
1415 tmp->SetOnlyLayout(bparams, firstpar->layout);
1416 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1419 // layout stays the same with latex-environments
1421 tmp->SetOnlyLayout(bparams, layout);
1422 tmp->SetLabelWidthString(labelwidthstring);
1426 if (Last() > pos || !Last() || flag == 2) {
1427 tmp->SetOnlyLayout(bparams, firstpar->layout);
1428 tmp->align = firstpar->align;
1429 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1431 tmp->line_bottom = firstpar->line_bottom;
1432 firstpar->line_bottom = false;
1433 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1434 firstpar->pagebreak_bottom = false;
1435 tmp->added_space_bottom = firstpar->added_space_bottom;
1436 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1438 tmp->depth = firstpar->depth;
1439 tmp->noindent = firstpar->noindent;
1441 if (Last() > pos || !Last() || flag == 2) {
1442 tmp->SetOnlyLayout(bparams, layout);
1444 tmp->SetLabelWidthString(labelwidthstring);
1446 tmp->line_bottom = line_bottom;
1447 line_bottom = false;
1448 tmp->pagebreak_bottom = pagebreak_bottom;
1449 pagebreak_bottom = false;
1450 tmp->added_space_bottom = added_space_bottom;
1451 added_space_bottom = VSpace(VSpace::NONE);
1454 tmp->noindent = noindent;
1456 // copy everything behind the break-position
1457 // to the new paragraph
1460 while (ParFromPos(pos_first) != par)
1462 pos_end = pos_first + par->text.size() - 1;
1465 for (i = j = pos; i <= pos_end; ++i) {
1466 par->CutIntoMinibuffer(bparams, i - pos_first);
1467 if (tmp->InsertFromMinibuffer(j - pos))
1470 tmp->text.resize(tmp->text.size());
1471 for (i = pos_end; i >= pos; --i)
1472 par->Erase(i - pos_first);
1474 par->text.resize(par->text.size());
1476 size_type pos_end = text.size() - 1;
1478 for (i = j = pos; i <= pos_end; ++i) {
1479 CutIntoMinibuffer(bparams, i);
1480 if (tmp->InsertFromMinibuffer(j - pos))
1484 for (i = pos_end; i >= pos; --i)
1492 // just an idea of me
1494 tmp->line_top = firstpar->line_top;
1495 tmp->pagebreak_top = firstpar->pagebreak_top;
1496 tmp->added_space_top = firstpar->added_space_top;
1497 tmp->bibkey = firstpar->bibkey;
1499 // layout stays the same with latex-environments
1501 firstpar->SetOnlyLayout(bparams, tmp->layout);
1502 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1503 firstpar->depth = tmp->depth;
1507 // just an idea of me
1509 tmp->line_top = line_top;
1510 tmp->pagebreak_top = pagebreak_top;
1511 tmp->added_space_top = added_space_top;
1512 tmp->bibkey = bibkey;
1514 // layout stays the same with latex-environments
1516 SetOnlyLayout(bparams, tmp->layout);
1517 SetLabelWidthString(tmp->labelwidthstring);
1525 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1528 par = par->FirstPhysicalPar();
1529 footnoteflag = par->footnoteflag;
1530 footnotekind = par->footnotekind;
1532 layout = par->layout;
1533 align = par-> align;
1534 SetLabelWidthString(par->labelwidthstring);
1536 line_bottom = par->line_bottom;
1537 pagebreak_bottom = par->pagebreak_bottom;
1538 added_space_bottom = par->added_space_bottom;
1540 line_top = par->line_top;
1541 pagebreak_top = par->pagebreak_top;
1542 added_space_top = par->added_space_top;
1544 spacing = par->spacing;
1546 pextra_type = par->pextra_type;
1547 pextra_width = par->pextra_width;
1548 pextra_widthp = par->pextra_widthp;
1549 pextra_alignment = par->pextra_alignment;
1550 pextra_hfill = par->pextra_hfill;
1551 pextra_start_minipage = par->pextra_start_minipage;
1553 noindent = par->noindent;
1559 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1561 LyXParagraph * tmppar = this;
1564 && tmppar->previous->footnoteflag ==
1565 LyXParagraph::CLOSED_FOOTNOTE)
1566 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1567 tmppar = tmppar->previous;
1570 return this; // This should never happen!
1577 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1579 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1587 && (IsNewline(0) || IsLineSeparator(0))){
1598 LyXParagraph * LyXParagraph::Clone() const
1600 // create a new paragraph
1601 LyXParagraph * result = new LyXParagraph;
1603 result->MakeSameLayout(this);
1605 // this is because of the dummy layout of the paragraphs that
1607 result->layout = layout;
1609 result->inset_owner = inset_owner;
1613 result->bibkey = static_cast<InsetBibKey *>
1614 (bibkey->Clone(*current_view->buffer()));
1618 // copy everything behind the break-position to the new paragraph
1620 result->text = text;
1621 result->fontlist = fontlist;
1622 result->insetlist = insetlist;
1623 for (InsetList::iterator it = result->insetlist.begin();
1624 it != result->insetlist.end(); ++it)
1625 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1630 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1633 par = par->FirstPhysicalPar();
1638 par->footnoteflag == footnoteflag &&
1639 par->footnotekind == footnotekind &&
1641 par->layout == layout &&
1643 par->align == align &&
1645 par->line_bottom == line_bottom &&
1646 par->pagebreak_bottom == pagebreak_bottom &&
1647 par->added_space_bottom == added_space_bottom &&
1649 par->line_top == line_top &&
1650 par->pagebreak_top == pagebreak_top &&
1651 par->added_space_top == added_space_top &&
1653 par->spacing == spacing &&
1655 par->pextra_type == pextra_type &&
1656 par->pextra_width == pextra_width &&
1657 par->pextra_widthp == pextra_widthp &&
1658 par->pextra_alignment == pextra_alignment &&
1659 par->pextra_hfill == pextra_hfill &&
1660 par->pextra_start_minipage == pextra_start_minipage &&
1661 par->noindent == noindent &&
1662 par->depth == depth);
1666 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1667 LyXParagraph::size_type pos)
1670 // create a new paragraph
1671 LyXParagraph * par = ParFromPos(pos);
1673 LyXParagraph * tmp = new LyXParagraph(par);
1675 tmp->MakeSameLayout(par);
1677 // When can pos < Last()?
1678 // I guess pos == Last() is possible.
1680 // copy everything behind the break-position to the new
1682 size_type pos_first = 0;
1683 while (ParFromPos(pos_first) != par)
1685 size_type pos_end = pos_first + par->text.size() - 1;
1688 for (i = j = pos; i <= pos_end; ++i) {
1689 par->CutIntoMinibuffer(bparams, i - pos_first);
1690 if (tmp->InsertFromMinibuffer(j - pos))
1693 tmp->text.resize(tmp->text.size());
1694 for (size_type i = pos_end; i >= pos; --i)
1695 par->Erase(i - pos_first);
1697 par->text.resize(par->text.size());
1700 // create a new paragraph
1701 LyXParagraph * tmp = new LyXParagraph(this);
1702 tmp->MakeSameLayout(this);
1704 // When can pos > Last()?
1705 // I guess pos == Last() is possible.
1707 // copy everything behind the break-position to the new
1709 size_type pos_end = text.size() - 1;
1712 for (i = j = pos; i <= pos_end; ++i) {
1713 CutIntoMinibuffer(bparams, i);
1714 if (tmp->InsertFromMinibuffer(j - pos))
1720 for (size_type i = pos_end; i >= pos; --i)
1729 // Be carefull, this does not make any check at all.
1730 // This method has wrong name, it combined this par with the next par.
1731 // In that sense it is the reverse of break paragraph. (Lgb)
1732 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1734 // copy the next paragraph to this one
1735 LyXParagraph * the_next = Next();
1737 LyXParagraph * firstpar = FirstPhysicalPar();
1740 // first the DTP-stuff
1742 firstpar->line_bottom = the_next->line_bottom;
1743 firstpar->added_space_bottom = the_next->added_space_bottom;
1744 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1746 line_bottom = the_next->line_bottom;
1747 added_space_bottom = the_next->added_space_bottom;
1748 pagebreak_bottom = the_next->pagebreak_bottom;
1751 size_type pos_end = the_next->text.size() - 1;
1752 size_type pos_insert = Last();
1754 // ok, now copy the paragraph
1756 for (i = j = 0; i <= pos_end; ++i) {
1757 the_next->CutIntoMinibuffer(bparams, i);
1758 if (InsertFromMinibuffer(pos_insert + j))
1762 // delete the next paragraph
1763 LyXParagraph * ppar = the_next->previous;
1764 LyXParagraph * npar = the_next->next;
1771 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1773 LyXParagraph * par = ParFromPos(pos);
1775 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1776 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1782 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1784 LyXParagraph * par = ParFromPos(pos);
1786 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1787 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1794 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1796 LyXParagraph const * par = this;
1797 int par_depth = GetDepth();
1799 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1800 int const endlabeltype =
1801 textclasslist.Style(bparams.textclass,
1802 layout).endlabeltype;
1803 if (endlabeltype != END_LABEL_NO_LABEL) {
1804 LyXParagraph const * last = this;
1806 if( footnoteflag == NO_FOOTNOTE)
1807 last = LastPhysicalPar();
1808 else if (next->footnoteflag == NO_FOOTNOTE)
1809 return endlabeltype;
1814 if (!last || !last->next)
1815 return endlabeltype;
1817 int next_depth = last->next->GetDepth();
1818 if (par_depth > next_depth ||
1819 (par_depth == next_depth && layout != last->next->GetLayout() ))
1820 return endlabeltype;
1825 par = par->DepthHook(par_depth - 1);
1827 par_depth = par->GetDepth();
1829 return END_LABEL_NO_LABEL;
1833 LyXTextClass::size_type LyXParagraph::GetLayout() const
1836 return FirstPhysicalPar()->layout;
1843 char LyXParagraph::GetDepth() const
1846 return FirstPhysicalPar()->depth;
1853 char LyXParagraph::GetAlign() const
1856 return FirstPhysicalPar()->align;
1863 string const & LyXParagraph::GetLabelstring() const
1866 return FirstPhysicalPar()->labelstring;
1873 int LyXParagraph::GetFirstCounter(int i) const
1876 return FirstPhysicalPar()->counter_[i];
1883 // the next two functions are for the manual labels
1884 string const LyXParagraph::GetLabelWidthString() const
1887 if (!FirstPhysicalPar()->labelwidthstring.empty())
1888 return FirstPhysicalPar()->labelwidthstring;
1890 if (!labelwidthstring.empty())
1891 return labelwidthstring;
1894 return _("Senseless with this layout!");
1898 void LyXParagraph::SetLabelWidthString(string const & s)
1901 LyXParagraph * par = FirstPhysicalPar();
1903 par->labelwidthstring = s;
1905 labelwidthstring = s;
1910 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1911 LyXTextClass::size_type new_layout)
1914 LyXParagraph * par = FirstPhysicalPar();
1916 LyXParagraph * par = this;
1918 LyXParagraph * ppar = 0;
1919 LyXParagraph * npar = 0;
1921 par->layout = new_layout;
1923 if (par->pextra_type == PEXTRA_NONE) {
1924 if (par->Previous()) {
1926 ppar = par->Previous()->FirstPhysicalPar();
1928 ppar = par->Previous();
1932 && (ppar->depth > par->depth))
1934 ppar = ppar->Previous()->FirstPhysicalPar();
1936 ppar = ppar->Previous();
1941 npar = par->Next()->NextAfterFootnote();
1947 && (npar->depth > par->depth))
1949 npar = npar->Next()->NextAfterFootnote();
1951 npar = npar->Next();
1954 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1956 p1 = ppar->pextra_width,
1957 p2 = ppar->pextra_widthp;
1958 ppar->SetPExtraType(bparams, ppar->pextra_type,
1961 if ((par->pextra_type == PEXTRA_NONE) &&
1962 npar && (npar->pextra_type != PEXTRA_NONE)) {
1964 p1 = npar->pextra_width,
1965 p2 = npar->pextra_widthp;
1966 npar->SetPExtraType(bparams, npar->pextra_type,
1973 void LyXParagraph::SetLayout(BufferParams const & bparams,
1974 LyXTextClass::size_type new_layout)
1978 * par = FirstPhysicalPar(),
1985 par->layout = new_layout;
1986 par->labelwidthstring.erase();
1987 par->align = LYX_ALIGN_LAYOUT;
1988 par->added_space_top = VSpace(VSpace::NONE);
1989 par->added_space_bottom = VSpace(VSpace::NONE);
1990 par->spacing.set(Spacing::Default);
1992 if (par->pextra_type == PEXTRA_NONE) {
1993 if (par->Previous()) {
1995 ppar = par->Previous()->FirstPhysicalPar();
1997 ppar = par->Previous();
2001 && (ppar->depth > par->depth))
2003 ppar = ppar->Previous()->FirstPhysicalPar();
2005 ppar = ppar->Previous();
2010 npar = par->Next()->NextAfterFootnote();
2016 && (npar->depth > par->depth))
2018 npar = npar->Next()->NextAfterFootnote();
2020 npar = npar->Next();
2023 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2025 p1 = ppar->pextra_width,
2026 p2 = ppar->pextra_widthp;
2027 ppar->SetPExtraType(bparams, ppar->pextra_type,
2030 if ((par->pextra_type == PEXTRA_NONE) &&
2031 npar && (npar->pextra_type != PEXTRA_NONE)) {
2033 p1 = npar->pextra_width,
2034 p2 = npar->pextra_widthp;
2035 npar->SetPExtraType(bparams, npar->pextra_type,
2042 // if the layout of a paragraph contains a manual label, the beginning of the
2043 // main body is the beginning of the second word. This is what the par-
2044 // function returns. If the layout does not contain a label, the main
2045 // body always starts with position 0. This differentiation is necessary,
2046 // because there cannot be a newline or a blank <= the beginning of the
2047 // main body in TeX.
2049 int LyXParagraph::BeginningOfMainBody() const
2052 if (FirstPhysicalPar() != this)
2055 // Unroll the first two cycles of the loop
2056 // and remember the previous character to
2057 // remove unnecessary GetChar() calls
2060 && GetChar(i) != LyXParagraph::META_NEWLINE
2063 char previous_char = 0, temp = 0;
2065 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2066 // Yes, this ^ is supposed to be "= " not "=="
2069 && previous_char != ' '
2070 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2072 previous_char = temp;
2078 if (i == 0 && i == size() &&
2079 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2080 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2081 ++i; /* the cursor should not jump
2082 * to the main body if there
2089 LyXParagraph * LyXParagraph::DepthHook(int deth)
2091 LyXParagraph * newpar = this;
2097 newpar = newpar->FirstPhysicalPar()->Previous();
2099 newpar = newpar->Previous();
2101 } while (newpar && newpar->GetDepth() > deth
2103 && newpar->footnoteflag == footnoteflag
2108 if (Previous() || GetDepth())
2109 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2114 return newpar->FirstPhysicalPar();
2121 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2123 LyXParagraph const * newpar = this;
2129 newpar = newpar->FirstPhysicalPar()->Previous();
2131 newpar = newpar->Previous();
2133 } while (newpar && newpar->GetDepth() > deth
2135 && newpar->footnoteflag == footnoteflag
2140 if (Previous() || GetDepth())
2141 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2146 return newpar->FirstPhysicalPar();
2153 int LyXParagraph::AutoDeleteInsets()
2156 InsetList::size_type index = 0;
2157 while (index < insetlist.size()) {
2158 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2159 Erase(insetlist[index].pos);
2160 // Erase() calls to insetlist.erase(&insetlist[index])
2161 // so index shouldn't be increased.
2170 LyXParagraph::inset_iterator
2171 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2173 InsetTable search_inset(pos, 0);
2174 InsetList::iterator it = lower_bound(insetlist.begin(),
2176 search_inset, matchIT());
2177 return inset_iterator(it);
2181 // returns -1 if inset not found
2182 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2185 for (InsetList::const_iterator cit = insetlist.begin();
2186 cit != insetlist.end(); ++cit) {
2187 if ((*cit).inset == inset) {
2192 // Think about footnotes.
2193 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2194 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2196 NextAfterFootnote()->GetPositionOfInset(inset);
2198 return text.size() + 1 + further;
2205 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2206 BufferParams const & bparams,
2207 ostream & os, TexRow & texrow,
2212 TexRow & foot_texrow,
2217 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2218 LyXLayout const & style =
2219 textclasslist.Style(bparams.textclass,
2222 bool further_blank_line = false;
2225 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2228 if (start_of_appendix) {
2229 os << "\\appendix\n";
2233 if (!spacing.isDefault()
2234 && (!Previous() || !Previous()->HasSameLayout(this))) {
2235 os << spacing.writeEnvirBegin() << "\n";
2239 if (tex_code_break_column && style.isCommand()){
2244 if (pagebreak_top) {
2246 further_blank_line = true;
2248 if (added_space_top.kind() != VSpace::NONE) {
2249 os << added_space_top.asLatexCommand(bparams);
2250 further_blank_line = true;
2254 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2255 << "\\vspace{-1\\parskip}";
2256 further_blank_line = true;
2259 if (further_blank_line){
2264 Language const * language = getParLanguage(bparams);
2265 Language const * doc_language = bparams.language_info;
2266 Language const * previous_language = previous
2267 ? previous->getParLanguage(bparams) : doc_language;
2268 if (language != doc_language && language != previous_language) {
2269 os << subst(lyxrc.language_command_begin, "$$lang",
2275 if (bparams.inputenc == "auto" &&
2276 language->encoding() != previous_language->encoding()) {
2277 os << "\\inputencoding{"
2278 << language->encoding()->LatexName()
2283 switch (style.latextype) {
2286 << style.latexname()
2287 << style.latexparam();
2289 case LATEX_ITEM_ENVIRONMENT:
2291 bibkey->Latex(buf, os, false, false);
2295 case LATEX_LIST_ENVIRONMENT:
2302 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2304 LyXParagraph * par = next;
2306 // Spit out footnotes
2307 if (lyxrc.rtl_support) {
2308 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2309 && next->footnoteflag != footnoteflag) {
2310 LyXParagraph * p = 0;
2311 bool is_rtl = (size() > 0)
2312 ? GetFontSettings(bparams,
2313 size()-1).isRightToLeft()
2314 : language->RightToLeft();
2315 if ( (p = NextAfterFootnote()) != 0 &&
2317 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2318 is_rtl = getParLanguage(bparams)->RightToLeft();
2319 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2320 && par->footnoteflag != footnoteflag) {
2321 par = par->TeXFootnote(buf, bparams,
2323 foot_texrow, foot_count,
2325 par->SimpleTeXOnePar(buf, bparams,
2326 os, texrow, moving_arg);
2327 is_rtl = (par->size() > 0)
2328 ? par->GetFontSettings(bparams,
2329 par->size()-1).isRightToLeft()
2330 : language->RightToLeft();
2332 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2333 (p = par->NextAfterFootnote()) != 0 &&
2335 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2336 is_rtl = language->RightToLeft();
2341 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2342 && par->footnoteflag != footnoteflag) {
2343 par = par->TeXFootnote(buf, bparams,
2345 foot, foot_texrow, foot_count,
2347 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2353 // Make sure that \\par is done with the font of the last
2354 // character if this has another size as the default.
2355 // This is necessary because LaTeX (and LyX on the screen)
2356 // calculates the space between the baselines according
2357 // to this font. (Matthias)
2358 LyXFont font = getFont(bparams, Last() - 1);
2359 if (need_par && next) {
2360 if (style.resfont.size() != font.size()) {
2366 } else if (textclasslist.Style(bparams.textclass,
2367 GetLayout()).isCommand()) {
2368 if (style.resfont.size() != font.size()) {
2374 } else if ((style.resfont.size() != font.size()) && next){
2375 os << "{\\" << font.latexSize() << " \\par}";
2378 if (language != doc_language &&
2381 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2383 || par->getParLanguage(bparams) != language)) {
2385 << subst(lyxrc.language_command_end, "$$lang",
2386 doc_language->lang());
2389 switch (style.latextype) {
2390 case LATEX_ITEM_ENVIRONMENT:
2391 case LATEX_LIST_ENVIRONMENT:
2392 if (par && (depth < par->depth)) {
2397 case LATEX_ENVIRONMENT:
2398 // if its the last paragraph of the current environment
2399 // skip it otherwise fall through
2401 && (par->layout != layout
2402 || par->depth != depth
2403 || par->pextra_type != pextra_type))
2406 // we don't need it for the last paragraph!!!
2413 further_blank_line = false;
2415 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2416 further_blank_line = true;
2419 if (added_space_bottom.kind() != VSpace::NONE) {
2420 os << added_space_bottom.asLatexCommand(bparams);
2421 further_blank_line = true;
2424 if (pagebreak_bottom) {
2426 further_blank_line = true;
2429 if (further_blank_line){
2434 if (!spacing.isDefault()
2435 && (!par || !par->HasSameLayout(this))) {
2436 os << spacing.writeEnvirEnd() << "\n";
2440 // we don't need it for the last paragraph!!!
2443 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2444 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2451 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2456 // This one spits out the text of the paragraph
2457 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2458 BufferParams const & bparams,
2459 ostream & os, TexRow & texrow,
2462 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2464 bool return_value = false;
2466 LyXLayout const & style =
2467 textclasslist.Style(bparams.textclass,
2469 LyXFont basefont, last_font;
2471 // Maybe we have to create a optional argument.
2472 size_type main_body;
2473 if (style.labeltype != LABEL_MANUAL)
2476 main_body = BeginningOfMainBody();
2478 if (main_body > 0) {
2480 basefont = getFont(bparams, -2); // Get label font
2482 basefont = getFont(bparams, -1); // Get layout font
2493 if (style.isCommand()) {
2496 } else if (align != LYX_ALIGN_LAYOUT) {
2499 return_value = true;
2503 moving_arg |= style.needprotect;
2505 // Which font is currently active?
2506 LyXFont running_font(basefont);
2507 // Do we have an open font change?
2508 bool open_font = false;
2510 texrow.start(this, 0);
2512 for (size_type i = 0; i < size(); ++i) {
2514 // First char in paragraph or after label?
2520 if (main_body > 0) {
2522 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2525 basefont = getFont(bparams, -1); // Now use the layout font
2526 running_font = basefont;
2530 if (style.isCommand()) {
2533 } else if (align != LYX_ALIGN_LAYOUT) {
2536 return_value = true;
2540 os << "\\noindent ";
2544 case LYX_ALIGN_NONE:
2545 case LYX_ALIGN_BLOCK:
2546 case LYX_ALIGN_LAYOUT:
2547 case LYX_ALIGN_SPECIAL:
2549 case LYX_ALIGN_LEFT:
2550 if (getParLanguage(bparams)->lang() != "hebrew") {
2551 os << "\\raggedright ";
2554 os << "\\raggedleft ";
2558 case LYX_ALIGN_RIGHT:
2559 if (getParLanguage(bparams)->lang() != "hebrew") {
2560 os << "\\raggedleft ";
2563 os << "\\raggedright ";
2567 case LYX_ALIGN_CENTER:
2568 os << "\\centering ";
2574 value_type c = GetChar(i);
2576 // Fully instantiated font
2577 LyXFont font = getFont(bparams, i);
2579 LyXParagraph * p = 0;
2582 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2583 (p = PreviousBeforeFootnote()) != 0)
2584 last_font = p->getFont(bparams, p->size() - 1);
2587 last_font = running_font;
2589 // Spaces at end of font change are simulated to be
2590 // outside font change, i.e. we write "\textXX{text} "
2591 // rather than "\textXX{text }". (Asger)
2592 if (open_font && c == ' ' && i <= size() - 2
2593 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2594 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2595 font = getFont(bparams, i + 1);
2597 // We end font definition before blanks
2598 if (!font.equalExceptLatex(running_font) && open_font) {
2599 column += running_font.latexWriteEndChanges(os,
2601 (i == main_body-1) ? basefont : font);
2602 running_font = basefont;
2606 // Blanks are printed before start of fontswitch
2608 // Do not print the separation of the optional argument
2609 if (i != main_body - 1) {
2610 SimpleTeXBlanks(os, texrow, i,
2611 column, font, style);
2615 // Do we need to change font?
2616 if (!font.equalExceptLatex(running_font)
2617 && i != main_body-1) {
2618 column += font.latexWriteStartChanges(os, basefont,
2620 running_font = font;
2624 if (c == LyXParagraph::META_NEWLINE) {
2625 // newlines are handled differently here than
2626 // the default in SimpleTeXSpecialChars().
2627 if (!style.newline_allowed
2628 || font.latex() == LyXFont::ON) {
2632 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2635 basefont = getFont(bparams, -1);
2636 running_font = basefont;
2637 if (font.family() ==
2638 LyXFont::TYPEWRITER_FAMILY) {
2646 texrow.start(this, i + 1);
2649 SimpleTeXSpecialChars(buf, bparams,
2650 os, texrow, moving_arg,
2651 font, running_font, basefont,
2652 open_font, style, i, column, c);
2656 // If we have an open font definition, we have to close it
2658 LyXParagraph * p = 0;
2661 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2662 && (p = NextAfterFootnote()) != 0
2667 running_font.latexWriteEndChanges(os, basefont,
2668 p->getFont(bparams, 0));
2670 running_font.latexWriteEndChanges(os, basefont, basefont);
2673 // Needed if there is an optional argument but no contents.
2674 if (main_body > 0 && main_body == size()) {
2676 return_value = false;
2679 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2680 return return_value;
2684 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2686 bool retval = false;
2688 case LyXParagraph::META_HFILL:
2689 sgml_string.erase();
2691 case LyXParagraph::META_NEWLINE:
2695 sgml_string = "&";
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 = "\";
2737 case '\0': // Ignore :-)
2738 sgml_string.erase();
2748 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2749 LyXParagraph::size_type const i,
2750 int & column, LyXFont const & font,
2751 LyXLayout const & style)
2753 if (column > tex_code_break_column
2755 && GetChar(i - 1) != ' '
2757 // In LaTeX mode, we don't want to
2758 // break lines since some commands
2760 && ! (font.latex() == LyXFont::ON)
2761 // same in FreeSpacing mode
2762 && !style.free_spacing
2763 // In typewriter mode, we want to avoid
2764 // ! . ? : at the end of a line
2765 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2766 && (GetChar(i-1) == '.'
2767 || GetChar(i-1) == '?'
2768 || GetChar(i-1) == ':'
2769 || GetChar(i-1) == '!'))) {
2770 if (tex_code_break_column == 0) {
2771 // in batchmode we need LaTeX to still
2772 // see it as a space not as an extra '\n'
2778 texrow.start(this, i + 1);
2780 } else if (font.latex() == LyXFont::OFF) {
2781 if (style.free_spacing) {
2790 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2791 BufferParams const & bparams,
2792 ostream & os, TexRow & texrow,
2795 LyXFont & running_font,
2798 LyXLayout const & style,
2799 LyXParagraph::size_type & i,
2801 LyXParagraph::value_type const c)
2803 // Two major modes: LaTeX or plain
2804 // Handle here those cases common to both modes
2805 // and then split to handle the two modes separately.
2807 case LyXParagraph::META_INSET: {
2808 Inset * inset = GetInset(i);
2811 int len = os.tellp();
2812 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2813 || inset->LyxCode() == Inset::MATH_CODE
2814 || inset->LyxCode() == Inset::URL_CODE)
2815 && running_font.isRightToLeft()) {
2820 int tmp = inset->Latex(buf, os, moving_arg,
2821 style.free_spacing);
2829 column += os.tellp() - len;
2838 case LyXParagraph::META_NEWLINE:
2840 column += running_font.latexWriteEndChanges(os,
2845 basefont = getFont(bparams, -1);
2846 running_font = basefont;
2849 case LyXParagraph::META_HFILL:
2855 // And now for the special cases within each mode
2856 // Are we in LaTeX mode?
2857 if (font.latex() == LyXFont::ON) {
2858 // at present we only have one option
2859 // but I'll leave it as a switch statement
2860 // so its simpler to extend. (ARRae)
2863 // make sure that we will not print
2864 // error generating chars to the tex
2865 // file. This test would not be needed
2866 // if it were done in the buffer
2874 // Plain mode (i.e. not LaTeX)
2877 os << "\\textbackslash{}";
2881 case '°': case '±': case '²': case '³':
2882 case '×': case '÷': case '¹': case 'ª':
2883 case 'º': case '¬': case 'µ':
2884 if (bparams.inputenc == "latin1" ||
2885 (bparams.inputenc == "auto" &&
2886 font.language()->encoding()->LatexName()
2888 os << "\\ensuremath{"
2897 case '|': case '<': case '>':
2898 // In T1 encoding, these characters exist
2899 if (lyxrc.fontenc == "T1") {
2901 //... but we should avoid ligatures
2902 if ((c == '>' || c == '<')
2904 && GetChar(i + 1) == c) {
2905 //os << "\\textcompwordmark{}";
2906 // Jean-Marc, have a look at
2907 // this. IÂ think this works
2915 // Typewriter font also has them
2916 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2920 // Otherwise, we use what LaTeX
2924 os << "\\textless{}";
2928 os << "\\textgreater{}";
2932 os << "\\textbar{}";
2938 case '-': // "--" in Typewriter mode -> "-{}-"
2940 && GetChar(i + 1) == '-'
2941 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
2950 os << "\\char`\\\"{}";
2955 if (bparams.inputenc == "default") {
2964 case '%': case '#': case '{':
2971 os << "\\textasciitilde{}";
2976 os << "\\textasciicircum{}";
2980 case '*': case '[': case ']':
2981 // avoid being mistaken for optional arguments
2982 os << '{' << c << '}';
2987 // Blanks are printed before font switching.
2988 // Sure? I am not! (try nice-latex)
2989 // I am sure it's correct. LyX might be smarter
2990 // in the future, but for now, nothing wrong is
2995 /* idea for labels --- begin*/
2999 && font.family() != LyXFont::TYPEWRITER_FAMILY
3000 && GetChar(i + 1) == 'y'
3001 && GetChar(i + 2) == 'X') {
3009 && font.family() != LyXFont::TYPEWRITER_FAMILY
3010 && GetChar(i + 1) == 'e'
3011 && GetChar(i + 2) == 'X') {
3016 // Check for "LaTeX2e"
3019 && font.family() != LyXFont::TYPEWRITER_FAMILY
3020 && GetChar(i + 1) == 'a'
3021 && GetChar(i + 2) == 'T'
3022 && GetChar(i + 3) == 'e'
3023 && GetChar(i + 4) == 'X'
3024 && GetChar(i + 5) == '2'
3025 && GetChar(i + 6) == 'e') {
3030 // Check for "LaTeX"
3033 && font.family() != LyXFont::TYPEWRITER_FAMILY
3034 && GetChar(i + 1) == 'a'
3035 && GetChar(i + 2) == 'T'
3036 && GetChar(i + 3) == 'e'
3037 && GetChar(i + 4) == 'X') {
3041 /* idea for labels --- end*/
3042 } else if (c != '\0') {
3052 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3053 BufferParams const & bparams,
3054 ostream & os, TexRow & texrow
3057 TexRow & foot_texrow,
3062 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3063 LyXParagraph * par = this;
3066 (par->depth == depth)
3068 && (par->footnoteflag == footnoteflag)
3073 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3075 if (textclasslist.Style(bparams.textclass,
3076 par->layout).isEnvironment()
3077 || par->pextra_type != PEXTRA_NONE) {
3078 par = par->TeXEnvironment(buf, bparams,
3086 par = par->TeXOnePar(buf, bparams,
3096 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3102 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3103 BufferParams const & bparams,
3104 ostream & os, TexRow & texrow
3107 TexRow & foot_texrow,
3112 bool eindent_open = false;
3114 bool foot_this_level = false;
3116 // flags when footnotetext should be appended to file.
3117 static bool minipage_open = false;
3118 static int minipage_open_depth = 0;
3119 char par_sep = bparams.paragraph_separation;
3121 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3124 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3127 LyXLayout const & style =
3128 textclasslist.Style(bparams.textclass,
3131 if (pextra_type == PEXTRA_INDENT) {
3132 if (!pextra_width.empty()) {
3133 os << "\\begin{LyXParagraphIndent}{"
3134 << pextra_width << "}\n";
3136 //float ib = atof(pextra_widthp.c_str())/100;
3137 // string can't handle floats at present (971109)
3138 // so I'll do a conversion by hand knowing that
3139 // the limits are 0.0 to 1.0. ARRae.
3140 os << "\\begin{LyXParagraphIndent}{";
3141 switch (pextra_widthp.length()) {
3153 os << "\\columnwidth}\n";
3156 eindent_open = true;
3158 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3159 if (pextra_hfill && Previous() &&
3160 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3161 os << "\\hfill{}\n";
3164 if (par_sep == BufferParams::PARSEP_INDENT) {
3165 os << "{\\setlength\\parindent{0pt}\n";
3168 os << "\\begin{minipage}";
3169 switch(pextra_alignment) {
3170 case MINIPAGE_ALIGN_TOP:
3173 case MINIPAGE_ALIGN_MIDDLE:
3176 case MINIPAGE_ALIGN_BOTTOM:
3180 if (!pextra_width.empty()) {
3181 os << '{' << pextra_width << "}\n";
3183 //float ib = atof(par->pextra_width.c_str())/100;
3184 // string can't handle floats at present
3185 // so I'll do a conversion by hand knowing that
3186 // the limits are 0.0 to 1.0. ARRae.
3188 switch (pextra_widthp.length()) {
3200 os << "\\columnwidth}\n";
3203 if (par_sep == BufferParams::PARSEP_INDENT) {
3204 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3207 minipage_open = true;
3208 minipage_open_depth = depth;
3211 #ifdef WITH_WARNINGS
3212 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3213 //I disabled it because it breaks when lists span on several
3216 if (style.isEnvironment()){
3217 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3218 #ifdef FANCY_FOOTNOTE_CODE
3219 if (foot_count < 0) {
3220 // flag that footnote[mark][text] should be
3221 // used for any footnotes from now on
3223 foot_this_level = true;
3226 os << "\\begin{" << style.latexname() << "}{"
3227 << labelwidthstring << "}\n";
3228 } else if (style.labeltype == LABEL_BIBLIO) {
3230 os << "\\begin{" << style.latexname() << "}{"
3231 << bibitemWidest(buf)
3233 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3234 #ifdef FANCY_FOOTNOTE_CODE
3235 if (foot_count < 0) {
3236 // flag that footnote[mark][text] should be
3237 // used for any footnotes from now on
3239 foot_this_level = true;
3242 os << "\\begin{" << style.latexname() << '}'
3243 << style.latexparam() << '\n';
3245 os << "\\begin{" << style.latexname() << '}'
3246 << style.latexparam() << '\n';
3249 LyXParagraph * par = this;
3251 par = par->TeXOnePar(buf, bparams,
3255 foot, foot_texrow, foot_count
3259 if (minipage_open && par && !style.isEnvironment() &&
3260 (par->pextra_type == PEXTRA_MINIPAGE) &&
3261 par->pextra_start_minipage) {
3262 os << "\\end{minipage}\n";
3264 if (par_sep == BufferParams::PARSEP_INDENT) {
3268 minipage_open = false;
3270 if (par && par->depth > depth) {
3271 if (textclasslist.Style(bparams.textclass,
3272 par->layout).isParagraph()
3274 // How to handle this? (Lgb)
3275 //&& !suffixIs(os, "\n\n")
3277 // There should be at least one '\n' already
3278 // but we need there to be two for Standard
3279 // paragraphs that are depth-increment'ed to be
3280 // output correctly. However, tables can
3281 // also be paragraphs so don't adjust them.
3284 // Will it ever harm to have one '\n' too
3285 // many? i.e. that we sometimes will have
3286 // three in a row. (Lgb)
3290 par = par->TeXDeeper(buf, bparams, os, texrow
3292 ,foot, foot_texrow, foot_count
3296 if (par && par->layout == layout && par->depth == depth &&
3297 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3298 if (par->pextra_hfill && par->Previous() &&
3299 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3300 os << "\\hfill{}\n";
3303 if (par_sep == BufferParams::PARSEP_INDENT) {
3304 os << "{\\setlength\\parindent{0pt}\n";
3307 os << "\\begin{minipage}";
3308 switch(par->pextra_alignment) {
3309 case MINIPAGE_ALIGN_TOP:
3312 case MINIPAGE_ALIGN_MIDDLE:
3315 case MINIPAGE_ALIGN_BOTTOM:
3319 if (!par->pextra_width.empty()) {
3320 os << '{' << par->pextra_width << "}\n";
3322 //float ib = atof(par->pextra_widthp.c_str())/100;
3323 // string can't handle floats at present
3324 // so I'll do a conversion by hand knowing that
3325 // the limits are 0.0 to 1.0. ARRae.
3327 switch (par->pextra_widthp.length()) {
3332 os << "0." << par->pextra_widthp;
3335 os << "0.0" << par->pextra_widthp;
3337 os << "\\columnwidth}\n";
3340 if (par_sep == BufferParams::PARSEP_INDENT) {
3341 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3344 minipage_open = true;
3345 minipage_open_depth = par->depth;
3348 && par->layout == layout
3349 && par->depth == depth
3350 && par->pextra_type == pextra_type
3352 && par->footnoteflag == footnoteflag
3356 if (style.isEnvironment()) {
3357 os << "\\end{" << style.latexname() << '}';
3359 // maybe this should go after the minipage closes?
3360 if (foot_this_level) {
3361 if (foot_count >= 1) {
3362 if (foot_count > 1) {
3363 os << "\\addtocounter{footnote}{-"
3368 texrow += foot_texrow;
3370 foot_texrow.reset();
3376 if (minipage_open && (minipage_open_depth == depth) &&
3377 (!par || par->pextra_start_minipage ||
3378 par->pextra_type != PEXTRA_MINIPAGE)) {
3379 os << "\\end{minipage}\n";
3381 if (par_sep == BufferParams::PARSEP_INDENT) {
3385 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3386 os << "\\medskip\n\n";
3390 minipage_open = false;
3393 os << "\\end{LyXParagraphIndent}\n";
3396 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3397 && par->pextra_hfill)) {
3401 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3402 return par; // ale970302
3407 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3408 BufferParams const & bparams,
3409 ostream & os, TexRow & texrow,
3410 ostream & foot, TexRow & foot_texrow,
3414 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3415 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3416 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3417 "No footnote!" << endl;
3419 LyXParagraph * par = this;
3420 LyXLayout const & style =
3421 textclasslist.Style(bparams.textclass,
3422 previous->GetLayout());
3424 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3425 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3426 "Float other than footnote in command"
3427 " with moving argument is illegal" << endl;
3430 if (footnotekind != LyXParagraph::FOOTNOTE
3431 && footnotekind != LyXParagraph::MARGIN
3434 // How to solve this?
3435 //&& !suffixIs(file, '\n')
3437 // we need to ensure that real floats like tables and figures
3438 // have their \begin{} on a new line otherwise we can get
3439 // incorrect results when using the endfloat.sty package
3440 // especially if two floats follow one another. ARRae 981022
3441 // NOTE: if the file is length 0 it must have just been
3442 // written out so we assume it ended with a '\n'
3444 // As far as I can see there is never any harm in writing
3445 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3450 bool moving_arg = false;
3451 bool need_closing = false;
3452 bool is_rtl = isRightToLeftPar(bparams);
3454 if (is_rtl != parent_is_rtl) {
3459 need_closing = true;
3462 bool footer_in_body = true;
3463 switch (footnotekind) {
3464 case LyXParagraph::FOOTNOTE:
3465 if (style.intitle) {
3466 os << "\\thanks{\n";
3467 footer_in_body = false;
3470 if (foot_count == -1) {
3471 // we're at depth 0 so we can use:
3472 os << "\\footnote{%\n";
3473 footer_in_body = false;
3475 os << "\\footnotemark{}%\n";
3477 // we only need this when there are
3478 // multiple footnotes
3479 os << "\\stepcounter{footnote}";
3481 os << "\\footnotetext{%\n";
3482 foot_texrow.start(this, 0);
3483 foot_texrow.newline();
3488 case LyXParagraph::MARGIN:
3489 os << "\\marginpar{\n";
3491 case LyXParagraph::FIG:
3492 if (pextra_type == PEXTRA_FLOATFLT
3493 && (!pextra_width.empty()
3494 || !pextra_widthp.empty())) {
3495 if (!pextra_width.empty())
3496 os << "\\begin{floatingfigure}{"
3497 << pextra_width << "}\n";
3499 os << "\\begin{floatingfigure}{"
3500 << lyx::atoi(pextra_widthp) / 100.0
3501 << "\\textwidth}\n";
3503 os << "\\begin{figure}";
3504 if (!bparams.float_placement.empty()) {
3505 os << '[' << bparams.float_placement << "]\n";
3511 case LyXParagraph::TAB:
3512 os << "\\begin{table}";
3513 if (!bparams.float_placement.empty()) {
3514 os << '[' << bparams.float_placement << "]\n";
3519 case LyXParagraph::WIDE_FIG:
3520 os << "\\begin{figure*}";
3521 if (!bparams.float_placement.empty()) {
3522 os << '[' << bparams.float_placement << "]\n";
3527 case LyXParagraph::WIDE_TAB:
3528 os << "\\begin{table*}";
3529 if (!bparams.float_placement.empty()) {
3530 os << '[' << bparams.float_placement << "]\n";
3535 case LyXParagraph::ALGORITHM:
3536 os << "\\begin{algorithm}\n";
3541 if (footnotekind != LyXParagraph::FOOTNOTE
3542 || !footer_in_body) {
3543 // Process text for all floats except footnotes in body
3545 LyXLayout const & style =
3547 .Style(bparams.textclass, par->layout);
3549 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3551 if (style.isEnvironment()
3552 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3553 // Allows the use of minipages within float
3554 // environments. Shouldn't be circular because
3555 // we don't support footnotes inside
3556 // floats (yet). ARRae
3557 par = par->TeXEnvironment(buf, bparams, os,
3562 par = par->TeXOnePar(buf, bparams,
3563 os, texrow, moving_arg,
3568 if (par && !par->IsDummy() && par->depth > depth) {
3569 par = par->TeXDeeper(buf, bparams, os, texrow,
3573 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3575 // process footnotes > depth 0 or in environments separately
3576 // NOTE: Currently don't support footnotes within footnotes
3577 // even though that is possible using the \footnotemark
3578 std::ostringstream dummy;
3579 TexRow dummy_texrow;
3580 int dummy_count = 0;
3582 LyXLayout const & style =
3584 .Style(bparams.textclass, par->layout);
3586 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3588 if (style.isEnvironment()
3589 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3590 // Allows the use of minipages within float
3591 // environments. Shouldn't be circular because
3592 // we don't support footnotes inside
3593 // floats (yet). ARRae
3594 par = par->TeXEnvironment(buf, bparams,
3596 dummy, dummy_texrow,
3599 par = par->TeXOnePar(buf, bparams,
3602 dummy, dummy_texrow,
3606 if (par && !par->IsDummy() && par->depth > depth) {
3607 par = par->TeXDeeper(buf, bparams,
3609 dummy, dummy_texrow,
3613 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3615 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3616 "Footnote in a Footnote -- not supported"
3619 //#ifndef HAVE_OSTREAM
3620 // delete [] dummy.str();
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 Language const * lang = GetFirstFontSettings().language();
3871 if (lang->lang() == default_language->lang())
3872 return bparams.language_info;
3874 } else if (previous)
3875 return previous->getParLanguage(bparams);
3877 return bparams.language_info;
3881 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3883 return lyxrc.rtl_support
3884 && getParLanguage(bparams)->RightToLeft();
3888 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3889 Language const * from, Language const * to)
3891 for(size_type i = 0; i < size(); ++i) {
3892 LyXFont font = GetFontSettings(bparams, i);
3893 if (font.language() == from) {
3894 font.setLanguage(to);
3901 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3903 Language const * doc_language = bparams.language_info;
3904 for (FontList::const_iterator cit = fontlist.begin();
3905 cit != fontlist.end(); ++cit)
3906 if ((*cit).font.language() != doc_language)
3912 // Convert the paragraph to a string.
3913 // Used for building the table of contents
3914 string const LyXParagraph::String(Buffer const * buffer, bool label)
3916 BufferParams const & bparams = buffer->params;
3919 if (label && !IsDummy() && !labelstring.empty())
3921 if (label && !labelstring.empty())
3923 s += labelstring + ' ';
3924 string::size_type len = s.size();
3926 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3927 value_type c = GetChar(i);
3930 else if (c == META_INSET &&
3931 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3932 std::ostringstream ost;
3933 GetInset(i)->Ascii(buffer, ost);
3934 s += subst(ost.str(),'\n',' ');
3939 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
3940 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
3941 s += NextAfterFootnote()->String(buffer, false);
3945 if (isRightToLeftPar(bparams))
3946 reverse(s.begin() + len,s.end());
3954 string const LyXParagraph::String(Buffer const * buffer,
3955 LyXParagraph::size_type beg,
3956 LyXParagraph::size_type end)
3961 if (beg == 0 && !IsDummy() && !labelstring.empty())
3963 if (beg == 0 && !labelstring.empty())
3965 s += labelstring + ' ';
3967 for (LyXParagraph::size_type i = beg; i < end; ++i) {
3968 value_type c = GetChar(i);
3971 else if (c == META_INSET) {
3972 std::ostringstream ost;
3973 GetInset(i)->Ascii(buffer, ost);
3982 void LyXParagraph::SetInsetOwner(Inset * i)
3985 for (InsetList::const_iterator cit = insetlist.begin();
3986 cit != insetlist.end(); ++cit) {
3988 (*cit).inset->setOwner(i);
3993 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
3996 for (InsetList::const_iterator cit = insetlist.begin();
3997 cit != insetlist.end(); ++cit) {
3999 if ((*cit).inset->IsTextInset()) {
4000 static_cast<UpdatableInset *>
4001 ((*cit).inset)->deleteLyXText(bv);
4008 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4011 for (InsetList::const_iterator cit = insetlist.begin();
4012 cit != insetlist.end(); ++cit) {
4014 if ((*cit).inset->IsTextInset()) {
4015 static_cast<UpdatableInset *>
4016 ((*cit).inset)->resizeLyXText(bv);