1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "lyxparagraph.h"
21 #include "lyxparagraph.h"
22 #include "support/textutils.h"
25 #include "tex-strings.h"
27 #include "bufferparams.h"
28 #include "support/FileInfo.h"
29 #include "support/LAssert.h"
31 #include "LaTeXFeatures.h"
32 #include "insets/insetinclude.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
35 #include "support/filetools.h"
36 #include "lyx_gui_misc.h"
38 #include "support/lyxmanip.h"
39 #include "BufferView.h"
46 using std::lower_bound;
47 using std::upper_bound;
50 int tex_code_break_column = 72; // needs non-zero initialization. set later.
51 // this is a bad idea, but how can LyXParagraph find its buffer to get
52 // parameters? (JMarc)
54 extern string bibitemWidest(Buffer const *);
56 // this is a minibuffer
57 static char minibuffer_char;
58 static LyXFont minibuffer_font;
59 static Inset * minibuffer_inset;
62 extern BufferView * current_view;
64 // Initialization of the counter for the paragraph id's,
65 // declared in lyxparagraph.h
66 unsigned int LyXParagraph::paragraph_id = 0;
69 LyXParagraph::LyXParagraph()
72 text.reserve(500); // is this number too big?
74 for (int i = 0; i < 10; ++i) setCounter(i , 0);
81 footnoteflag = LyXParagraph::NO_FOOTNOTE;
82 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
84 align = LYX_ALIGN_BLOCK;
88 bibkey = 0; // ale970302
93 // This konstruktor inserts the new paragraph in a list.
94 LyXParagraph::LyXParagraph(LyXParagraph * par)
101 for (int i = 0; i < 10; ++i) setCounter(i, 0);
105 // double linked list begin
108 next->previous = this;
110 previous->next = this;
113 footnoteflag = LyXParagraph::NO_FOOTNOTE;
114 footnotekind = LyXParagraph::FOOTNOTE;
117 id_ = paragraph_id++;
119 bibkey = 0; // ale970302
125 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
126 BufferParams const & params,
127 char footflag, char dth) const
131 footnoteflag != LyXParagraph::NO_FOOTNOTE ||
133 || previous->footnoteflag == LyXParagraph::NO_FOOTNOTE
138 // The beginning or the end of a footnote environment?
139 if (footflag != footnoteflag) {
140 footflag = footnoteflag;
142 os << "\n\\begin_float "
143 << string_footnotekinds[footnotekind]
146 os << "\n\\end_float ";
150 // The beginning or end of a deeper (i.e. nested) area?
153 while (depth > dth) {
154 os << "\n\\begin_deeper ";
158 while (depth < dth) {
159 os << "\n\\end_deeper ";
165 // First write the layout
167 << textclasslist.NameOfLayout(params.textclass, layout)
170 // Maybe some vertical spaces.
171 if (added_space_top.kind() != VSpace::NONE)
172 os << "\\added_space_top "
173 << added_space_top.asLyXCommand() << " ";
174 if (added_space_bottom.kind() != VSpace::NONE)
175 os << "\\added_space_bottom "
176 << added_space_bottom.asLyXCommand() << " ";
178 // Maybe the paragraph has special spacing
179 spacing.writeFile(os, true);
181 // The labelwidth string used in lists.
182 if (!labelwidthstring.empty())
183 os << "\\labelwidthstring "
184 << labelwidthstring << '\n';
186 // Lines above or below?
190 os << "\\line_bottom ";
192 // Pagebreaks above or below?
194 os << "\\pagebreak_top ";
195 if (pagebreak_bottom)
196 os << "\\pagebreak_bottom ";
198 // Start of appendix?
199 if (start_of_appendix)
200 os << "\\start_of_appendix ";
207 if (align != LYX_ALIGN_LAYOUT) {
210 case LYX_ALIGN_LEFT: h = 1; break;
211 case LYX_ALIGN_RIGHT: h = 2; break;
212 case LYX_ALIGN_CENTER: h = 3; break;
213 default: h = 0; break;
215 os << "\\align " << string_align[h] << " ";
217 if (pextra_type != PEXTRA_NONE) {
218 os << "\\pextra_type " << pextra_type;
219 if (pextra_type == PEXTRA_MINIPAGE) {
220 os << " \\pextra_alignment "
223 os << " \\pextra_hfill "
225 if (pextra_start_minipage)
226 os << " \\pextra_start_minipage "
227 << pextra_start_minipage;
229 if (!pextra_width.empty()) {
230 os << " \\pextra_width "
231 << VSpace(pextra_width).asLyXCommand();
232 } else if (!pextra_widthp.empty()) {
233 os << " \\pextra_widthp "
240 // Dummy layout. This means that a footnote ended.
241 os << "\n\\end_float ";
242 footflag = LyXParagraph::NO_FOOTNOTE;
248 bibkey->Write(buf, os);
250 LyXFont font1(LyXFont::ALL_INHERIT, params.language);
253 for (size_type i = 0; i < size(); ++i) {
259 // Write font changes
260 LyXFont font2 = GetFontSettings(params, i);
261 if (font2 != font1) {
262 font2.lyxWriteChanges(font1, os);
267 value_type const c = GetChar(i);
271 Inset const * inset = GetInset(i);
273 if (inset->DirectWrite()) {
274 // international char, let it write
275 // code directly so it's shorter in
277 inset->Write(buf, os);
279 os << "\n\\begin_inset ";
280 inset->Write(buf, os);
281 os << "\n\\end_inset \n\n";
287 os << "\n\\newline \n";
291 os << "\n\\hfill \n";
295 os << "\n\\backslash \n";
299 if (i + 1 < size() && GetChar(i + 1) == ' ') {
306 if ((column > 70 && c == ' ')
311 // this check is to amend a bug. LyX sometimes
312 // inserts '\0' this could cause problems.
316 lyxerr << "ERROR (LyXParagraph::writeFile):"
317 " NULL char in structure." << endl;
323 // now write the next paragraph
325 next->writeFile(buf, os, params, footflag, dth);
329 void LyXParagraph::validate(LaTeXFeatures & features) const
331 BufferParams const & params = features.bufferParams();
334 // this will be useful later
335 LyXLayout const & layout =
336 textclasslist.Style(params.textclass,
341 if (line_top || line_bottom)
342 features.lyxline = true;
343 if (!spacing.isDefault())
344 features.setspace = true;
347 features.layout[GetLayout()] = true;
350 Language const * doc_language = params.language;
352 for (FontList::const_iterator cit = fontlist.begin();
353 cit != fontlist.end(); ++cit) {
354 if ((*cit).font.noun() == LyXFont::ON) {
355 lyxerr[Debug::LATEX] << "font.noun: "
356 << (*cit).font.noun()
358 features.noun = true;
359 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
360 << (*cit).font.stateText(0)
363 switch ((*cit).font.color()) {
365 case LColor::inherit:
369 features.color = true;
370 lyxerr[Debug::LATEX] << "Color enabled. Font: "
371 << (*cit).font.stateText(0)
375 Language const * language = (*cit).font.language();
376 if (language->babel() != doc_language->babel()) {
377 features.UsedLanguages.insert(language);
378 lyxerr[Debug::LATEX] << "Found language "
379 << language->babel() << endl;
384 for (InsetList::const_iterator cit = insetlist.begin();
385 cit != insetlist.end(); ++cit) {
387 (*cit).inset->Validate(features);
390 if (pextra_type == PEXTRA_INDENT)
391 features.LyXParagraphIndent = true;
392 if (pextra_type == PEXTRA_FLOATFLT)
393 features.floatflt = true;
395 if (layout.needprotect
396 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
397 features.NeedLyXFootnoteCode = true;
399 if (params.paragraph_separation == BufferParams::PARSEP_INDENT
400 && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
401 features.NeedLyXMinipageIndent = true;
403 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
404 features.algorithm = true;
409 // First few functions needed for cut and paste and paragraph breaking.
410 void LyXParagraph::CopyIntoMinibuffer(BufferParams const & bparams,
411 LyXParagraph::size_type pos) const
413 minibuffer_char = GetChar(pos);
414 minibuffer_font = GetFontSettings(bparams, pos);
415 minibuffer_inset = 0;
416 if (minibuffer_char == LyXParagraph::META_INSET) {
418 minibuffer_inset = GetInset(pos)->Clone(*current_view->buffer());
420 minibuffer_inset = 0;
421 minibuffer_char = ' ';
422 // This reflects what GetInset() does (ARRae)
428 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
429 LyXParagraph::size_type pos)
431 minibuffer_char = GetChar(pos);
432 minibuffer_font = GetFontSettings(bparams, pos);
433 minibuffer_inset = 0;
434 if (minibuffer_char == LyXParagraph::META_INSET) {
436 minibuffer_inset = GetInset(pos);
437 // This is a little hack since I want exactly
438 // the inset, not just a clone. Otherwise
439 // the inset would be deleted when calling Erase(pos)
441 InsetTable search_elem(pos, 0);
442 InsetList::iterator it =
443 lower_bound(insetlist.begin(),
445 search_elem, matchIT());
446 if (it != insetlist.end() && (*it).pos == pos)
449 minibuffer_inset = 0;
450 minibuffer_char = ' ';
451 // This reflects what GetInset() does (ARRae)
456 // Erase(pos); now the caller is responsible for that.
460 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
462 if ((minibuffer_char == LyXParagraph::META_INSET) &&
463 !InsertInsetAllowed(minibuffer_inset))
465 if (minibuffer_char == LyXParagraph::META_INSET)
466 InsertInset(pos, minibuffer_inset, minibuffer_font);
468 InsertChar(pos, minibuffer_char, minibuffer_font);
476 void LyXParagraph::Clear()
481 pagebreak_top = false;
482 pagebreak_bottom = false;
484 added_space_top = VSpace(VSpace::NONE);
485 added_space_bottom = VSpace(VSpace::NONE);
486 spacing.set(Spacing::Default);
488 align = LYX_ALIGN_LAYOUT;
492 pextra_type = PEXTRA_NONE;
493 pextra_width.erase();
494 pextra_widthp.erase();
495 pextra_alignment = MINIPAGE_ALIGN_TOP;
496 pextra_hfill = false;
497 pextra_start_minipage = false;
500 labelwidthstring.erase();
504 start_of_appendix = false;
508 // the destructor removes the new paragraph from the list
509 LyXParagraph::~LyXParagraph()
512 previous->next = next;
514 next->previous = previous;
516 for (InsetList::iterator it = insetlist.begin();
517 it != insetlist.end(); ++it) {
524 //lyxerr << "LyXParagraph::paragraph_id = "
525 // << LyXParagraph::paragraph_id << endl;
529 void LyXParagraph::Erase(LyXParagraph::size_type pos)
532 // > because last is the next unused position, and you can
533 // use it if you want
535 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
536 NextAfterFootnote()->Erase(pos - text.size() - 1);
538 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
539 "position does not exist." << endl;
543 Assert(pos < size());
546 if (pos < size()) { // last is free for insertation, but should be empty
548 // if it is an inset, delete the inset entry
549 if (text[pos] == LyXParagraph::META_INSET) {
551 InsetTable search_inset(pos, 0);
552 InsetList::iterator it =
553 lower_bound(insetlist.begin(),
555 search_inset, matchIT());
556 if (it != insetlist.end() && (*it).pos == pos) {
562 text.erase(text.begin() + pos);
564 text.erase(text.mutable_begin() + pos);
566 // Erase entries in the tables.
567 FontTable search_font(pos, LyXFont());
569 FontList::iterator it =
570 lower_bound(fontlist.begin(),
572 search_font, matchFT());
573 if (it != fontlist.end() && (*it).pos == pos &&
575 (it != fontlist.begin() && (*(it - 1)).pos == pos - 1))) {
576 // If it is a multi-character font
577 // entry, we just make it smaller
578 // (see update below), otherwise we
580 unsigned int const i = it - fontlist.begin();
581 fontlist.erase(fontlist.begin() + i);
582 it = fontlist.begin() + i;
583 if (i > 0 && i < fontlist.size() &&
584 fontlist[i - 1].font == fontlist[i].font) {
585 fontlist.erase(fontlist.begin() + i - 1);
586 it = fontlist.begin() + i - 1;
590 // Update all other entries.
591 FontList::iterator fend = fontlist.end();
592 for (; it != fend; ++it)
595 // Update the inset table.
596 InsetTable search_inset(pos, 0);
597 InsetList::iterator lend = insetlist.end();
598 for (InsetList::iterator it =
599 upper_bound(insetlist.begin(),
601 search_inset, matchIT());
606 lyxerr << "ERROR (LyXParagraph::Erase): "
607 "can't erase non-existant char." << endl;
613 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
614 LyXParagraph::value_type c)
616 LyXFont const f(LyXFont::ALL_INHERIT);
617 InsertChar(pos, c, f);
621 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
622 LyXParagraph::value_type c,
623 LyXFont const & font)
626 // > because last is the next unused position, and you can
627 // use it if you want
630 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
631 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
634 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
635 "position does not exist." << endl;
639 Assert(pos <= size());
642 text.insert(text.begin() + pos, c);
646 // Update the font table.
647 FontTable search_font(pos, LyXFont());
648 for (FontList::iterator it = lower_bound(fontlist.begin(),
650 search_font, matchFT());
651 it != fontlist.end(); ++it)
654 // Update the inset table.
655 InsetTable search_inset(pos, 0);
656 for (InsetList::iterator it = lower_bound(insetlist.begin(),
658 search_inset, matchIT());
659 it != insetlist.end(); ++it)
666 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
669 LyXFont const f(LyXFont::ALL_INHERIT);
670 InsertInset(pos, inset, f);
674 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
675 Inset * inset, LyXFont const & font)
680 // > because last is the next unused position, and you can
681 // use it if you want
684 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
686 ->InsertInset(pos - text.size() - 1,
689 lyxerr << "ERROR (LyXParagraph::InsertInset): "
690 "position does not exist: " << pos << endl;
694 Assert(pos <= size());
697 InsertChar(pos, META_INSET, font);
698 Assert(text[pos] == META_INSET);
700 // Add a new entry in the inset table.
701 InsetTable search_inset(pos, 0);
702 InsetList::iterator it = lower_bound(insetlist.begin(),
704 search_inset, matchIT());
705 if (it != insetlist.end() && (*it).pos == pos)
706 lyxerr << "ERROR (LyXParagraph::InsertInset): "
707 "there is an inset in position: " << pos << endl;
709 insetlist.insert(it, InsetTable(pos, inset));
711 inset->setOwner(inset_owner);
715 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
717 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
720 return inset_owner->InsertInsetAllowed(inset);
725 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
730 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
731 return NextAfterFootnote()
732 ->GetInset(pos - text.size() - 1);
734 lyxerr << "ERROR (LyXParagraph::GetInset): "
735 "position does not exist: "
741 Assert(pos < size());
744 InsetTable search_inset(pos, 0);
745 InsetList::iterator it = lower_bound(insetlist.begin(),
747 search_inset, matchIT());
748 if (it != insetlist.end() && (*it).pos == pos)
751 lyxerr << "ERROR (LyXParagraph::GetInset): "
752 "Inset does not exist: " << pos << endl;
755 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
756 // Did this commenting out introduce a bug? So far I have not
757 // see any, please enlighten me. (Lgb)
758 // My guess is that since the inset does not exist, we might
759 // as well replace it with a space to prevent craches. (Asger)
764 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
769 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
770 return NextAfterFootnote()
771 ->GetInset(pos - text.size() - 1);
773 lyxerr << "ERROR (LyXParagraph::GetInset): "
774 "position does not exist: "
780 Assert(pos < size());
783 InsetTable search_inset(pos, 0);
784 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
786 search_inset, matchIT());
787 if (cit != insetlist.end() && (*cit).pos == pos)
790 lyxerr << "ERROR (LyXParagraph::GetInset): "
791 "Inset does not exist: " << pos << endl;
793 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
794 // Did this commenting out introduce a bug? So far I have not
795 // see any, please enlighten me. (Lgb)
796 // My guess is that since the inset does not exist, we might
797 // as well replace it with a space to prevent craches. (Asger)
802 // Gets uninstantiated font setting at position.
803 // Optimized after profiling. (Asger)
804 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
805 LyXParagraph::size_type pos) const
808 Assert(pos <= size());
813 FontTable search_font(pos, LyXFont());
814 FontList::const_iterator cit = lower_bound(fontlist.begin(),
816 search_font, matchFT());
817 if (cit != fontlist.end())
823 // > because last is the next unused position, and you can
824 // use it if you want
825 else if (pos > size()) {
827 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
828 return NextAfterFootnote()
829 ->GetFontSettings(bparams,
830 pos - text.size() - 1);
832 // Why is it an error to ask for the font of a
833 // position that does not exist? Would it be
834 // enough for this to be enabled on debug?
835 // We want strict error checking, but it's ok to only
836 // have it when debugging. (Asger)
837 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
838 "position does not exist. "
839 << pos << " (" << static_cast<int>(pos)
843 return GetFontSettings(bparams, pos - 1);
846 if (pos == size() && size())
847 return GetFontSettings(bparams, pos - 1);
851 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
853 //return LyXFont(LyXFont::ALL_INHERIT);
856 // Gets uninstantiated font setting at position 0
857 LyXFont const LyXParagraph::GetFirstFontSettings() const
860 if (!fontlist.empty())
861 return fontlist[0].font;
865 else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
866 return NextAfterFootnote()->GetFirstFontSettings();
868 return LyXFont(LyXFont::ALL_INHERIT);
872 // Gets the fully instantiated font at a given position in a paragraph
873 // This is basically the same function as LyXText::GetFont() in text2.C.
874 // The difference is that this one is used for generating the LaTeX file,
875 // and thus cosmetic "improvements" are disallowed: This has to deliver
876 // the true picture of the buffer. (Asger)
877 // If position is -1, we get the layout font of the paragraph.
878 // If position is -2, we get the font of the manual label of the paragraph.
879 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
880 LyXParagraph::size_type pos) const
883 LyXLayout const & layout =
884 textclasslist.Style(bparams.textclass,
886 LyXParagraph::size_type main_body = 0;
887 if (layout.labeltype == LABEL_MANUAL)
888 main_body = BeginningOfMainBody();
893 layoutfont = layout.labelfont;
895 layoutfont = layout.font;
896 tmpfont = GetFontSettings(bparams, pos);
897 tmpfont.realize(layoutfont);
899 // process layoutfont for pos == -1 and labelfont for pos < -1
901 tmpfont = layout.font;
903 tmpfont = layout.labelfont;
904 tmpfont.setLanguage(getParLanguage(bparams));
907 // check for environment font information
908 char par_depth = GetDepth();
909 LyXParagraph const * par = this;
910 while (par && par_depth && !tmpfont.resolved()) {
911 par = par->DepthHook(par_depth - 1);
913 tmpfont.realize(textclasslist.
914 Style(bparams.textclass,
915 par->GetLayout()).font);
916 par_depth = par->GetDepth();
920 tmpfont.realize(textclasslist
921 .TextClass(bparams.textclass)
927 /// Returns the height of the highest font in range
929 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
930 LyXParagraph::size_type endpos) const
932 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
933 if (fontlist.empty())
936 FontTable end_search(endpos, LyXFont());
937 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
939 end_search, matchFT());
940 if (end_it != fontlist.end())
943 FontTable start_search(startpos, LyXFont());
944 for (FontList::const_iterator cit =
945 lower_bound(fontlist.begin(),
947 start_search, matchFT());
948 cit != end_it; ++cit) {
949 LyXFont::FONT_SIZE size = (*cit).font.size();
950 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
957 LyXParagraph::value_type
958 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
963 Assert(pos <= size());
964 if (!size() || pos == size()) return '\0';
975 // > because last is the next unused position, and you can
976 // use it if you want
977 else if (pos > size()) {
978 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
979 return NextAfterFootnote()
980 ->GetChar(pos - text.size() - 1);
983 lyxerr << "ERROR (LyXParagraph::GetChar const): "
984 "position does not exist."
985 << pos << " (" << static_cast<int>(pos)
991 // We should have a footnote environment.
992 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
993 // Notice that LyX does request the
994 // last char from time to time. (Asger)
995 //lyxerr << "ERROR (LyXParagraph::GetChar): "
996 // "expected footnote." << endl;
999 switch (next->footnotekind) {
1000 case LyXParagraph::FOOTNOTE:
1001 return LyXParagraph::META_FOOTNOTE;
1002 case LyXParagraph::MARGIN:
1003 return LyXParagraph::META_MARGIN;
1004 case LyXParagraph::FIG:
1005 case LyXParagraph::WIDE_FIG:
1006 return LyXParagraph::META_FIG;
1007 case LyXParagraph::TAB:
1008 case LyXParagraph::WIDE_TAB:
1009 return LyXParagraph::META_TAB;
1010 case LyXParagraph::ALGORITHM:
1011 return LyXParagraph::META_ALGORITHM;
1013 return '\0'; // to shut up gcc
1019 // return an string of the current word, and the end of the word in lastpos.
1020 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1022 Assert(lastpos >= 0);
1024 // the current word is defined as starting at the first character
1025 // from the immediate left of lastpospos which meets the definition
1026 // of IsLetter(), continuing to the last character to the right
1027 // of this meeting IsLetter.
1033 // move back until we have a letter
1035 //there's no real reason to have firstpos & lastpos as
1036 //separate variables as this is written, but maybe someon
1037 // will want to return firstpos in the future.
1039 //since someone might have typed a punctuation first
1040 int firstpos = lastpos;
1042 while ((firstpos >= 0) && !IsLetter(firstpos))
1045 // now find the beginning by looking for a nonletter
1047 while ((firstpos>= 0) && IsLetter(firstpos))
1050 // the above is now pointing to the preceeding non-letter
1054 // so copy characters into theword until we get a nonletter
1055 // note that this can easily exceed lastpos, wich means
1056 // that if used in the middle of a word, the whole word
1059 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1067 #warning Remember to get rid of this one. (Lgb)
1069 LyXParagraph::size_type LyXParagraph::Last() const
1072 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1073 return text.size() + NextAfterFootnote()->Last() + 1;
1074 // the 1 is the symbol
1083 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1085 // > because last is the next unused position, and you can
1086 // use it if you want
1089 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1090 return NextAfterFootnote()
1091 ->ParFromPos(pos - text.size() - 1);
1093 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1094 "position does not exist." << endl;
1103 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1105 // > because last is the next unused position, and you can
1106 // use it if you want
1109 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1110 return NextAfterFootnote()
1111 ->PositionInParFromPos(pos - text.size() - 1);
1114 "ERROR (LyXParagraph::PositionInParFromPos): "
1115 "position does not exist." << endl;
1124 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1125 LyXFont const & font)
1128 // > because last is the next unused position, and you can
1129 // use it if you want
1132 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1133 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1136 lyxerr << "ERROR (LyXParagraph::SetFont): "
1137 "position does not exist." << endl;
1142 Assert(pos <= size());
1145 // First, reduce font against layout/label font
1146 // Update: The SetCharFont() routine in text2.C already
1147 // reduces font, so we don't need to do that here. (Asger)
1148 // No need to simplify this because it will disappear
1149 // in a new kernel. (Asger)
1150 // Next search font table
1152 FontTable search_font(pos, LyXFont());
1153 FontList::iterator it = lower_bound(fontlist.begin(),
1155 search_font, matchFT());
1156 unsigned int i = it - fontlist.begin();
1157 bool notfound = it == fontlist.end();
1159 if (!notfound && fontlist[i].font == font)
1162 bool begin = pos == 0 || notfound ||
1163 (i > 0 && fontlist[i-1].pos == pos - 1);
1164 // Is position pos is a beginning of a font block?
1165 bool end = !notfound && fontlist[i].pos == pos;
1166 // Is position pos is the end of a font block?
1167 if (begin && end) { // A single char block
1168 if (i + 1 < fontlist.size() &&
1169 fontlist[i + 1].font == font) {
1170 // Merge the singleton block with the next block
1171 fontlist.erase(fontlist.begin() + i);
1172 if (i > 0 && fontlist[i - 1].font == font)
1173 fontlist.erase(fontlist.begin() + i-1);
1174 } else if (i > 0 && fontlist[i - 1].font == font) {
1175 // Merge the singleton block with the previous block
1176 fontlist[i - 1].pos = pos;
1177 fontlist.erase(fontlist.begin() + i);
1179 fontlist[i].font = font;
1181 if (i > 0 && fontlist[i - 1].font == font)
1182 fontlist[i - 1].pos = pos;
1184 fontlist.insert(fontlist.begin() + i,
1185 FontTable(pos, font));
1187 fontlist[i].pos = pos - 1;
1188 if (!(i + 1 < fontlist.size() &&
1189 fontlist[i + 1].font == font))
1190 fontlist.insert(fontlist.begin() + i + 1,
1191 FontTable(pos, font));
1192 } else { // The general case. The block is splitted into 3 blocks
1193 fontlist.insert(fontlist.begin() + i,
1194 FontTable(pos - 1, fontlist[i].font));
1195 fontlist.insert(fontlist.begin() + i + 1,
1196 FontTable(pos, font));
1201 // This function is able to hide closed footnotes.
1202 LyXParagraph * LyXParagraph::Next()
1205 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1206 LyXParagraph * tmp = next;
1208 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1210 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1211 return tmp->Next(); /* there can be more than one
1212 footnote in a logical
1215 return next; // This should never happen!
1223 LyXParagraph * LyXParagraph::NextAfterFootnote()
1225 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1226 LyXParagraph * tmp = next;
1227 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1229 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1230 return tmp; /* there can be more than one footnote
1231 in a logical paragraph */
1233 return next; // This should never happen!
1241 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1243 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1244 LyXParagraph * tmp = next;
1245 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1247 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1248 return tmp; /* there can be more than one footnote
1249 in a logical paragraph */
1251 return next; // This should never happen!
1259 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1262 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1264 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1265 tmp = tmp->previous;
1266 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1267 return tmp; /* there can be more than one footnote
1268 in a logical paragraph */
1270 return previous; // This should never happen!
1278 LyXParagraph * LyXParagraph::LastPhysicalPar()
1280 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1283 LyXParagraph * tmp = this;
1285 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1286 tmp = tmp->NextAfterFootnote();
1294 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1296 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1299 LyXParagraph const * tmp = this;
1301 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1302 tmp = tmp->NextAfterFootnote();
1310 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1314 LyXParagraph * tmppar = this;
1318 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1319 tmppar = tmppar->previous;
1330 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1334 LyXParagraph const * tmppar = this;
1338 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1339 tmppar = tmppar->previous;
1349 // This function is able to hide closed footnotes.
1350 LyXParagraph * LyXParagraph::Previous()
1353 LyXParagraph * tmp = previous;
1358 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1359 tmp = tmp->previous;
1361 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1362 tmp = tmp->previous;
1363 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1364 return tmp->next->Previous();
1374 // This function is able to hide closed footnotes.
1375 LyXParagraph const * LyXParagraph::Previous() const
1378 LyXParagraph * tmp = previous;
1382 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1383 tmp = tmp->previous;
1385 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1386 tmp = tmp->previous;
1387 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1388 return tmp->next->Previous();
1398 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1399 LyXParagraph::size_type pos,
1404 // create a new paragraph
1407 size_type pos_first;
1408 LyXParagraph * par = ParFromPos(pos);
1409 LyXParagraph * firstpar = FirstPhysicalPar();
1411 LyXParagraph * tmp = new LyXParagraph(par);
1413 //LyXParagraph * par = this;
1414 //LyXParagraph * firstpar = this;
1415 LyXParagraph * tmp = new LyXParagraph(this);
1419 tmp->footnoteflag = footnoteflag;
1420 tmp->footnotekind = footnotekind;
1422 // this is an idea for a more userfriendly layout handling, I will
1423 // see what the users say
1426 // layout stays the same with latex-environments
1428 tmp->SetOnlyLayout(bparams, firstpar->layout);
1429 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1432 // layout stays the same with latex-environments
1434 tmp->SetOnlyLayout(bparams, layout);
1435 tmp->SetLabelWidthString(labelwidthstring);
1439 if (Last() > pos || !Last() || flag == 2) {
1440 tmp->SetOnlyLayout(bparams, firstpar->layout);
1441 tmp->align = firstpar->align;
1442 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1444 tmp->line_bottom = firstpar->line_bottom;
1445 firstpar->line_bottom = false;
1446 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1447 firstpar->pagebreak_bottom = false;
1448 tmp->added_space_bottom = firstpar->added_space_bottom;
1449 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1451 tmp->depth = firstpar->depth;
1452 tmp->noindent = firstpar->noindent;
1454 if (Last() > pos || !Last() || flag == 2) {
1455 tmp->SetOnlyLayout(bparams, layout);
1457 tmp->SetLabelWidthString(labelwidthstring);
1459 tmp->line_bottom = line_bottom;
1460 line_bottom = false;
1461 tmp->pagebreak_bottom = pagebreak_bottom;
1462 pagebreak_bottom = false;
1463 tmp->added_space_bottom = added_space_bottom;
1464 added_space_bottom = VSpace(VSpace::NONE);
1467 tmp->noindent = noindent;
1469 // copy everything behind the break-position
1470 // to the new paragraph
1473 while (ParFromPos(pos_first) != par)
1475 pos_end = pos_first + par->text.size() - 1;
1478 for (i = j = pos; i <= pos_end; ++i) {
1479 par->CutIntoMinibuffer(bparams, i - pos_first);
1480 if (tmp->InsertFromMinibuffer(j - pos))
1486 for (i = pos_end; i >= pos; --i)
1487 par->Erase(i - pos_first);
1491 size_type pos_end = text.size() - 1;
1493 for (i = j = pos; i <= pos_end; ++i) {
1494 CutIntoMinibuffer(bparams, i);
1495 if (tmp->InsertFromMinibuffer(j - pos))
1499 for (i = pos_end; i >= pos; --i)
1507 // just an idea of me
1509 tmp->line_top = firstpar->line_top;
1510 tmp->pagebreak_top = firstpar->pagebreak_top;
1511 tmp->added_space_top = firstpar->added_space_top;
1512 tmp->bibkey = firstpar->bibkey;
1514 // layout stays the same with latex-environments
1516 firstpar->SetOnlyLayout(bparams, tmp->layout);
1517 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1518 firstpar->depth = tmp->depth;
1522 // just an idea of me
1524 tmp->line_top = line_top;
1525 tmp->pagebreak_top = pagebreak_top;
1526 tmp->added_space_top = added_space_top;
1527 tmp->bibkey = bibkey;
1529 // layout stays the same with latex-environments
1531 SetOnlyLayout(bparams, tmp->layout);
1532 SetLabelWidthString(tmp->labelwidthstring);
1540 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1543 par = par->FirstPhysicalPar();
1544 footnoteflag = par->footnoteflag;
1545 footnotekind = par->footnotekind;
1547 layout = par->layout;
1548 align = par-> align;
1549 SetLabelWidthString(par->labelwidthstring);
1551 line_bottom = par->line_bottom;
1552 pagebreak_bottom = par->pagebreak_bottom;
1553 added_space_bottom = par->added_space_bottom;
1555 line_top = par->line_top;
1556 pagebreak_top = par->pagebreak_top;
1557 added_space_top = par->added_space_top;
1559 spacing = par->spacing;
1561 pextra_type = par->pextra_type;
1562 pextra_width = par->pextra_width;
1563 pextra_widthp = par->pextra_widthp;
1564 pextra_alignment = par->pextra_alignment;
1565 pextra_hfill = par->pextra_hfill;
1566 pextra_start_minipage = par->pextra_start_minipage;
1568 noindent = par->noindent;
1574 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1576 LyXParagraph * tmppar = this;
1579 && tmppar->previous->footnoteflag ==
1580 LyXParagraph::CLOSED_FOOTNOTE)
1581 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1582 tmppar = tmppar->previous;
1585 return this; // This should never happen!
1592 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1594 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1602 && (IsNewline(0) || IsLineSeparator(0))){
1613 LyXParagraph * LyXParagraph::Clone() const
1615 // create a new paragraph
1616 LyXParagraph * result = new LyXParagraph;
1618 result->MakeSameLayout(this);
1620 // this is because of the dummy layout of the paragraphs that
1622 result->layout = layout;
1624 result->inset_owner = inset_owner;
1628 result->bibkey = static_cast<InsetBibKey *>
1629 (bibkey->Clone(*current_view->buffer()));
1633 // copy everything behind the break-position to the new paragraph
1635 result->text = text;
1636 result->fontlist = fontlist;
1637 result->insetlist = insetlist;
1638 for (InsetList::iterator it = result->insetlist.begin();
1639 it != result->insetlist.end(); ++it)
1640 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1645 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1648 par = par->FirstPhysicalPar();
1653 par->footnoteflag == footnoteflag &&
1654 par->footnotekind == footnotekind &&
1656 par->layout == layout &&
1658 par->align == align &&
1660 par->line_bottom == line_bottom &&
1661 par->pagebreak_bottom == pagebreak_bottom &&
1662 par->added_space_bottom == added_space_bottom &&
1664 par->line_top == line_top &&
1665 par->pagebreak_top == pagebreak_top &&
1666 par->added_space_top == added_space_top &&
1668 par->spacing == spacing &&
1670 par->pextra_type == pextra_type &&
1671 par->pextra_width == pextra_width &&
1672 par->pextra_widthp == pextra_widthp &&
1673 par->pextra_alignment == pextra_alignment &&
1674 par->pextra_hfill == pextra_hfill &&
1675 par->pextra_start_minipage == pextra_start_minipage &&
1676 par->noindent == noindent &&
1677 par->depth == depth);
1681 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1682 LyXParagraph::size_type pos)
1685 // create a new paragraph
1686 LyXParagraph * par = ParFromPos(pos);
1688 LyXParagraph * tmp = new LyXParagraph(par);
1690 tmp->MakeSameLayout(par);
1692 // When can pos < Last()?
1693 // I guess pos == Last() is possible.
1695 // copy everything behind the break-position to the new
1697 size_type pos_first = 0;
1698 while (ParFromPos(pos_first) != par)
1700 size_type pos_end = pos_first + par->text.size() - 1;
1703 for (i = j = pos; i <= pos_end; ++i) {
1704 par->CutIntoMinibuffer(bparams, i - pos_first);
1705 if (tmp->InsertFromMinibuffer(j - pos))
1711 for (size_type i = pos_end; i >= pos; --i)
1712 par->Erase(i - pos_first);
1717 // create a new paragraph
1718 LyXParagraph * tmp = new LyXParagraph(this);
1719 tmp->MakeSameLayout(this);
1721 // When can pos > Last()?
1722 // I guess pos == Last() is possible.
1724 // copy everything behind the break-position to the new
1726 size_type pos_end = text.size() - 1;
1729 for (i = j = pos; i <= pos_end; ++i) {
1730 CutIntoMinibuffer(bparams, i);
1731 if (tmp->InsertFromMinibuffer(j - pos))
1737 for (size_type i = pos_end; i >= pos; --i)
1746 // Be carefull, this does not make any check at all.
1747 // This method has wrong name, it combined this par with the next par.
1748 // In that sense it is the reverse of break paragraph. (Lgb)
1749 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1751 // copy the next paragraph to this one
1752 LyXParagraph * the_next = Next();
1754 LyXParagraph * firstpar = FirstPhysicalPar();
1757 // first the DTP-stuff
1759 firstpar->line_bottom = the_next->line_bottom;
1760 firstpar->added_space_bottom = the_next->added_space_bottom;
1761 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1763 line_bottom = the_next->line_bottom;
1764 added_space_bottom = the_next->added_space_bottom;
1765 pagebreak_bottom = the_next->pagebreak_bottom;
1768 size_type pos_end = the_next->text.size() - 1;
1769 size_type pos_insert = Last();
1771 // ok, now copy the paragraph
1773 for (i = j = 0; i <= pos_end; ++i) {
1774 the_next->CutIntoMinibuffer(bparams, i);
1775 if (InsertFromMinibuffer(pos_insert + j))
1779 // delete the next paragraph
1780 LyXParagraph * ppar = the_next->previous;
1781 LyXParagraph * npar = the_next->next;
1788 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1790 LyXParagraph * par = ParFromPos(pos);
1792 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1793 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1799 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1801 LyXParagraph * par = ParFromPos(pos);
1803 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1804 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1811 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1813 LyXParagraph const * par = this;
1814 int par_depth = GetDepth();
1816 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1817 int const endlabeltype =
1818 textclasslist.Style(bparams.textclass,
1819 layout).endlabeltype;
1820 if (endlabeltype != END_LABEL_NO_LABEL) {
1821 LyXParagraph const * last = this;
1823 if (footnoteflag == NO_FOOTNOTE)
1824 last = LastPhysicalPar();
1825 else if (next->footnoteflag == NO_FOOTNOTE)
1826 return endlabeltype;
1831 if (!last || !last->next)
1832 return endlabeltype;
1834 int next_depth = last->next->GetDepth();
1835 if (par_depth > next_depth ||
1836 (par_depth == next_depth && layout != last->next->GetLayout() ))
1837 return endlabeltype;
1842 par = par->DepthHook(par_depth - 1);
1844 par_depth = par->GetDepth();
1846 return END_LABEL_NO_LABEL;
1850 LyXTextClass::size_type LyXParagraph::GetLayout() const
1853 return FirstPhysicalPar()->layout;
1860 char LyXParagraph::GetDepth() const
1863 return FirstPhysicalPar()->depth;
1870 char LyXParagraph::GetAlign() const
1873 return FirstPhysicalPar()->align;
1880 string const & LyXParagraph::GetLabelstring() const
1883 return FirstPhysicalPar()->labelstring;
1890 int LyXParagraph::GetFirstCounter(int i) const
1893 return FirstPhysicalPar()->counter_[i];
1900 // the next two functions are for the manual labels
1901 string const LyXParagraph::GetLabelWidthString() const
1904 if (!FirstPhysicalPar()->labelwidthstring.empty())
1905 return FirstPhysicalPar()->labelwidthstring;
1907 if (!labelwidthstring.empty())
1908 return labelwidthstring;
1911 return _("Senseless with this layout!");
1915 void LyXParagraph::SetLabelWidthString(string const & s)
1918 LyXParagraph * par = FirstPhysicalPar();
1920 par->labelwidthstring = s;
1922 labelwidthstring = s;
1927 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1928 LyXTextClass::size_type new_layout)
1931 LyXParagraph * par = FirstPhysicalPar();
1933 LyXParagraph * par = this;
1935 LyXParagraph * ppar = 0;
1936 LyXParagraph * npar = 0;
1938 par->layout = new_layout;
1940 if (par->pextra_type == PEXTRA_NONE) {
1941 if (par->Previous()) {
1943 ppar = par->Previous()->FirstPhysicalPar();
1945 ppar = par->Previous();
1949 && (ppar->depth > par->depth))
1951 ppar = ppar->Previous()->FirstPhysicalPar();
1953 ppar = ppar->Previous();
1958 npar = par->Next()->NextAfterFootnote();
1964 && (npar->depth > par->depth))
1966 npar = npar->Next()->NextAfterFootnote();
1968 npar = npar->Next();
1971 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
1973 p1 = ppar->pextra_width,
1974 p2 = ppar->pextra_widthp;
1975 ppar->SetPExtraType(bparams, ppar->pextra_type,
1978 if ((par->pextra_type == PEXTRA_NONE) &&
1979 npar && (npar->pextra_type != PEXTRA_NONE)) {
1980 string const p1 = npar->pextra_width;
1981 string const p2 = npar->pextra_widthp;
1982 npar->SetPExtraType(bparams, npar->pextra_type,
1989 void LyXParagraph::SetLayout(BufferParams const & bparams,
1990 LyXTextClass::size_type new_layout)
1994 * par = FirstPhysicalPar(),
2001 par->layout = new_layout;
2002 par->labelwidthstring.erase();
2003 par->align = LYX_ALIGN_LAYOUT;
2004 par->added_space_top = VSpace(VSpace::NONE);
2005 par->added_space_bottom = VSpace(VSpace::NONE);
2006 par->spacing.set(Spacing::Default);
2008 if (par->pextra_type == PEXTRA_NONE) {
2009 if (par->Previous()) {
2011 ppar = par->Previous()->FirstPhysicalPar();
2013 ppar = par->Previous();
2017 && (ppar->depth > par->depth))
2019 ppar = ppar->Previous()->FirstPhysicalPar();
2021 ppar = ppar->Previous();
2026 npar = par->Next()->NextAfterFootnote();
2032 && (npar->depth > par->depth))
2034 npar = npar->Next()->NextAfterFootnote();
2036 npar = npar->Next();
2039 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2040 string const p1 = ppar->pextra_width;
2041 string const p2 = ppar->pextra_widthp;
2042 ppar->SetPExtraType(bparams, ppar->pextra_type,
2045 if ((par->pextra_type == PEXTRA_NONE) &&
2046 npar && (npar->pextra_type != PEXTRA_NONE)) {
2047 string const p1 = npar->pextra_width;
2048 string const p2 = npar->pextra_widthp;
2049 npar->SetPExtraType(bparams, npar->pextra_type,
2056 // if the layout of a paragraph contains a manual label, the beginning of the
2057 // main body is the beginning of the second word. This is what the par-
2058 // function returns. If the layout does not contain a label, the main
2059 // body always starts with position 0. This differentiation is necessary,
2060 // because there cannot be a newline or a blank <= the beginning of the
2061 // main body in TeX.
2063 int LyXParagraph::BeginningOfMainBody() const
2066 if (FirstPhysicalPar() != this)
2069 // Unroll the first two cycles of the loop
2070 // and remember the previous character to
2071 // remove unnecessary GetChar() calls
2074 && GetChar(i) != LyXParagraph::META_NEWLINE
2077 char previous_char = 0;
2080 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2081 // Yes, this ^ is supposed to be "= " not "=="
2084 && previous_char != ' '
2085 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2087 previous_char = temp;
2093 if (i == 0 && i == size() &&
2094 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2095 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2096 ++i; /* the cursor should not jump
2097 * to the main body if there
2104 LyXParagraph * LyXParagraph::DepthHook(int deth)
2106 LyXParagraph * newpar = this;
2112 newpar = newpar->FirstPhysicalPar()->Previous();
2114 newpar = newpar->Previous();
2116 } while (newpar && newpar->GetDepth() > deth
2118 && newpar->footnoteflag == footnoteflag
2123 if (Previous() || GetDepth())
2124 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2129 return newpar->FirstPhysicalPar();
2136 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2138 LyXParagraph const * newpar = this;
2144 newpar = newpar->FirstPhysicalPar()->Previous();
2146 newpar = newpar->Previous();
2148 } while (newpar && newpar->GetDepth() > deth
2150 && newpar->footnoteflag == footnoteflag
2155 if (Previous() || GetDepth())
2156 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2161 return newpar->FirstPhysicalPar();
2168 int LyXParagraph::AutoDeleteInsets()
2171 InsetList::size_type index = 0;
2172 while (index < insetlist.size()) {
2173 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2174 Erase(insetlist[index].pos);
2175 // Erase() calls to insetlist.erase(&insetlist[index])
2176 // so index shouldn't be increased.
2185 LyXParagraph::inset_iterator
2186 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2188 InsetTable search_inset(pos, 0);
2189 InsetList::iterator it = lower_bound(insetlist.begin(),
2191 search_inset, matchIT());
2192 return inset_iterator(it);
2196 // returns -1 if inset not found
2197 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2200 for (InsetList::const_iterator cit = insetlist.begin();
2201 cit != insetlist.end(); ++cit) {
2202 if ((*cit).inset == inset) {
2207 // Think about footnotes.
2208 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2209 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2211 NextAfterFootnote()->GetPositionOfInset(inset);
2213 return text.size() + 1 + further;
2220 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2221 BufferParams const & bparams,
2222 ostream & os, TexRow & texrow,
2227 TexRow & foot_texrow,
2232 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2233 LyXLayout const & style =
2234 textclasslist.Style(bparams.textclass,
2237 bool further_blank_line = false;
2240 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2243 if (start_of_appendix) {
2244 os << "\\appendix\n";
2248 if (!spacing.isDefault()
2249 && (!Previous() || !Previous()->HasSameLayout(this))) {
2250 os << spacing.writeEnvirBegin() << "\n";
2254 if (tex_code_break_column && style.isCommand()){
2259 if (pagebreak_top) {
2261 further_blank_line = true;
2263 if (added_space_top.kind() != VSpace::NONE) {
2264 os << added_space_top.asLatexCommand(bparams);
2265 further_blank_line = true;
2269 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2270 << "\\vspace{-1\\parskip}";
2271 further_blank_line = true;
2274 if (further_blank_line){
2279 Language const * language = getParLanguage(bparams);
2280 Language const * doc_language = bparams.language;
2281 Language const * previous_language = previous
2282 ? previous->getParLanguage(bparams) : doc_language;
2283 if (language->babel() != doc_language->babel() &&
2284 language->babel() != previous_language->babel()) {
2285 os << subst(lyxrc.language_command_begin, "$$lang",
2291 if (bparams.inputenc == "auto" &&
2292 language->encoding() != previous_language->encoding()) {
2293 os << "\\inputencoding{"
2294 << language->encoding()->LatexName()
2299 switch (style.latextype) {
2302 << style.latexname()
2303 << style.latexparam();
2305 case LATEX_ITEM_ENVIRONMENT:
2307 bibkey->Latex(buf, os, false, false);
2311 case LATEX_LIST_ENVIRONMENT:
2318 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2320 LyXParagraph * par = next;
2322 // Spit out footnotes
2323 if (lyxrc.rtl_support) {
2324 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2325 && next->footnoteflag != footnoteflag) {
2326 LyXParagraph * p = 0;
2327 bool is_rtl = (size() > 0)
2328 ? GetFontSettings(bparams,
2329 size()-1).isRightToLeft()
2330 : language->RightToLeft();
2331 if ((p = NextAfterFootnote()) != 0 &&
2333 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2334 is_rtl = getParLanguage(bparams)->RightToLeft();
2335 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2336 && par->footnoteflag != footnoteflag) {
2337 par = par->TeXFootnote(buf, bparams,
2339 foot_texrow, foot_count,
2341 par->SimpleTeXOnePar(buf, bparams,
2342 os, texrow, moving_arg);
2343 is_rtl = (par->size() > 0)
2344 ? par->GetFontSettings(bparams,
2345 par->size()-1).isRightToLeft()
2346 : language->RightToLeft();
2348 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2349 (p = par->NextAfterFootnote()) != 0 &&
2351 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2352 is_rtl = language->RightToLeft();
2357 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2358 && par->footnoteflag != footnoteflag) {
2359 par = par->TeXFootnote(buf, bparams,
2361 foot, foot_texrow, foot_count,
2363 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2369 // Make sure that \\par is done with the font of the last
2370 // character if this has another size as the default.
2371 // This is necessary because LaTeX (and LyX on the screen)
2372 // calculates the space between the baselines according
2373 // to this font. (Matthias)
2374 LyXFont font = getFont(bparams, Last() - 1);
2375 if (need_par && next) {
2376 if (style.resfont.size() != font.size()) {
2382 } else if (textclasslist.Style(bparams.textclass,
2383 GetLayout()).isCommand()) {
2384 if (style.resfont.size() != font.size()) {
2390 } else if ((style.resfont.size() != font.size()) && next){
2391 os << "{\\" << font.latexSize() << " \\par}";
2394 if (language->babel() != doc_language->babel() &&
2397 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2399 || par->getParLanguage(bparams)->babel() != language->babel())) {
2401 << subst(lyxrc.language_command_end, "$$lang",
2402 doc_language->babel());
2405 switch (style.latextype) {
2406 case LATEX_ITEM_ENVIRONMENT:
2407 case LATEX_LIST_ENVIRONMENT:
2408 if (par && (depth < par->depth)) {
2413 case LATEX_ENVIRONMENT:
2414 // if its the last paragraph of the current environment
2415 // skip it otherwise fall through
2417 && (par->layout != layout
2418 || par->depth != depth
2419 || par->pextra_type != pextra_type))
2422 // we don't need it for the last paragraph!!!
2429 further_blank_line = false;
2431 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2432 further_blank_line = true;
2435 if (added_space_bottom.kind() != VSpace::NONE) {
2436 os << added_space_bottom.asLatexCommand(bparams);
2437 further_blank_line = true;
2440 if (pagebreak_bottom) {
2442 further_blank_line = true;
2445 if (further_blank_line){
2450 if (!spacing.isDefault()
2451 && (!par || !par->HasSameLayout(this))) {
2452 os << spacing.writeEnvirEnd() << "\n";
2456 // we don't need it for the last paragraph!!!
2459 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2460 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2467 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2472 // This one spits out the text of the paragraph
2473 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2474 BufferParams const & bparams,
2475 ostream & os, TexRow & texrow,
2478 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2480 bool return_value = false;
2482 LyXLayout const & style =
2483 textclasslist.Style(bparams.textclass,
2485 LyXFont basefont, last_font;
2487 // Maybe we have to create a optional argument.
2488 size_type main_body;
2489 if (style.labeltype != LABEL_MANUAL)
2492 main_body = BeginningOfMainBody();
2494 if (main_body > 0) {
2496 basefont = getFont(bparams, -2); // Get label font
2498 basefont = getFont(bparams, -1); // Get layout font
2509 if (style.isCommand()) {
2512 } else if (align != LYX_ALIGN_LAYOUT) {
2515 return_value = true;
2519 moving_arg |= style.needprotect;
2521 // Which font is currently active?
2522 LyXFont running_font(basefont);
2523 // Do we have an open font change?
2524 bool open_font = false;
2526 texrow.start(this, 0);
2528 for (size_type i = 0; i < size(); ++i) {
2530 // First char in paragraph or after label?
2536 if (main_body > 0) {
2538 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2541 basefont = getFont(bparams, -1); // Now use the layout font
2542 running_font = basefont;
2546 if (style.isCommand()) {
2549 } else if (align != LYX_ALIGN_LAYOUT) {
2552 return_value = true;
2556 os << "\\noindent ";
2560 case LYX_ALIGN_NONE:
2561 case LYX_ALIGN_BLOCK:
2562 case LYX_ALIGN_LAYOUT:
2563 case LYX_ALIGN_SPECIAL:
2565 case LYX_ALIGN_LEFT:
2566 if (getParLanguage(bparams)->babel() != "hebrew") {
2567 os << "\\raggedright ";
2570 os << "\\raggedleft ";
2574 case LYX_ALIGN_RIGHT:
2575 if (getParLanguage(bparams)->babel() != "hebrew") {
2576 os << "\\raggedleft ";
2579 os << "\\raggedright ";
2583 case LYX_ALIGN_CENTER:
2584 os << "\\centering ";
2590 value_type c = GetChar(i);
2592 // Fully instantiated font
2593 LyXFont font = getFont(bparams, i);
2595 LyXParagraph * p = 0;
2598 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2599 (p = PreviousBeforeFootnote()) != 0)
2600 last_font = p->getFont(bparams, p->size() - 1);
2603 last_font = running_font;
2605 // Spaces at end of font change are simulated to be
2606 // outside font change, i.e. we write "\textXX{text} "
2607 // rather than "\textXX{text }". (Asger)
2608 if (open_font && c == ' ' && i <= size() - 2
2609 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2610 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2611 font = getFont(bparams, i + 1);
2613 // We end font definition before blanks
2614 if (!font.equalExceptLatex(running_font) && open_font) {
2615 column += running_font.latexWriteEndChanges(os,
2617 (i == main_body-1) ? basefont : font);
2618 running_font = basefont;
2622 // Blanks are printed before start of fontswitch
2624 // Do not print the separation of the optional argument
2625 if (i != main_body - 1) {
2626 SimpleTeXBlanks(os, texrow, i,
2627 column, font, style);
2631 // Do we need to change font?
2632 if (!font.equalExceptLatex(running_font)
2633 && i != main_body-1) {
2634 column += font.latexWriteStartChanges(os, basefont,
2636 running_font = font;
2640 if (c == LyXParagraph::META_NEWLINE) {
2641 // newlines are handled differently here than
2642 // the default in SimpleTeXSpecialChars().
2643 if (!style.newline_allowed
2644 || font.latex() == LyXFont::ON) {
2648 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2651 basefont = getFont(bparams, -1);
2652 running_font = basefont;
2653 if (font.family() ==
2654 LyXFont::TYPEWRITER_FAMILY) {
2662 texrow.start(this, i + 1);
2665 SimpleTeXSpecialChars(buf, bparams,
2666 os, texrow, moving_arg,
2667 font, running_font, basefont,
2668 open_font, style, i, column, c);
2672 // If we have an open font definition, we have to close it
2674 LyXParagraph * p = 0;
2677 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2678 && (p = NextAfterFootnote()) != 0
2683 running_font.latexWriteEndChanges(os, basefont,
2684 p->getFont(bparams, 0));
2686 running_font.latexWriteEndChanges(os, basefont, basefont);
2689 // Needed if there is an optional argument but no contents.
2690 if (main_body > 0 && main_body == size()) {
2692 return_value = false;
2695 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2696 return return_value;
2700 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2702 bool retval = false;
2704 case LyXParagraph::META_HFILL:
2705 sgml_string.erase();
2707 case LyXParagraph::META_NEWLINE:
2711 sgml_string = "&";
2714 sgml_string = "<";
2717 sgml_string = ">";
2720 sgml_string = "$";
2723 sgml_string = "#";
2726 sgml_string = "%";
2729 sgml_string = "[";
2732 sgml_string = "]";
2735 sgml_string = "{";
2738 sgml_string = "}";
2741 sgml_string = "˜";
2744 sgml_string = """;
2747 sgml_string = "\";
2753 case '\0': // Ignore :-)
2754 sgml_string.erase();
2764 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2765 LyXParagraph::size_type const i,
2766 int & column, LyXFont const & font,
2767 LyXLayout const & style)
2769 if (column > tex_code_break_column
2771 && GetChar(i - 1) != ' '
2773 // In LaTeX mode, we don't want to
2774 // break lines since some commands
2776 && ! (font.latex() == LyXFont::ON)
2777 // same in FreeSpacing mode
2778 && !style.free_spacing
2779 // In typewriter mode, we want to avoid
2780 // ! . ? : at the end of a line
2781 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2782 && (GetChar(i-1) == '.'
2783 || GetChar(i-1) == '?'
2784 || GetChar(i-1) == ':'
2785 || GetChar(i-1) == '!'))) {
2786 if (tex_code_break_column == 0) {
2787 // in batchmode we need LaTeX to still
2788 // see it as a space not as an extra '\n'
2794 texrow.start(this, i + 1);
2796 } else if (font.latex() == LyXFont::OFF) {
2797 if (style.free_spacing) {
2806 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2807 BufferParams const & bparams,
2808 ostream & os, TexRow & texrow,
2811 LyXFont & running_font,
2814 LyXLayout const & style,
2815 LyXParagraph::size_type & i,
2817 LyXParagraph::value_type const c)
2819 // Two major modes: LaTeX or plain
2820 // Handle here those cases common to both modes
2821 // and then split to handle the two modes separately.
2823 case LyXParagraph::META_INSET: {
2824 Inset * inset = GetInset(i);
2827 int const len = os.tellp();
2828 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2829 || inset->LyxCode() == Inset::MATH_CODE
2830 || inset->LyxCode() == Inset::URL_CODE)
2831 && running_font.isRightToLeft()) {
2836 int tmp = inset->Latex(buf, os, moving_arg,
2837 style.free_spacing);
2845 column += os.tellp() - len;
2854 case LyXParagraph::META_NEWLINE:
2856 column += running_font.latexWriteEndChanges(os,
2861 basefont = getFont(bparams, -1);
2862 running_font = basefont;
2865 case LyXParagraph::META_HFILL:
2871 // And now for the special cases within each mode
2872 // Are we in LaTeX mode?
2873 if (font.latex() == LyXFont::ON) {
2874 // at present we only have one option
2875 // but I'll leave it as a switch statement
2876 // so its simpler to extend. (ARRae)
2879 // make sure that we will not print
2880 // error generating chars to the tex
2881 // file. This test would not be needed
2882 // if it were done in the buffer
2890 // Plain mode (i.e. not LaTeX)
2893 os << "\\textbackslash{}";
2897 case '°': case '±': case '²': case '³':
2898 case '×': case '÷': case '¹': case 'ª':
2899 case 'º': case '¬': case 'µ':
2900 if (bparams.inputenc == "latin1" ||
2901 (bparams.inputenc == "auto" &&
2902 font.language()->encoding()->LatexName()
2904 os << "\\ensuremath{"
2913 case '|': case '<': case '>':
2914 // In T1 encoding, these characters exist
2915 if (lyxrc.fontenc == "T1") {
2917 //... but we should avoid ligatures
2918 if ((c == '>' || c == '<')
2920 && GetChar(i + 1) == c) {
2921 //os << "\\textcompwordmark{}";
2922 // Jean-Marc, have a look at
2923 // this. IÂ think this works
2931 // Typewriter font also has them
2932 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2936 // Otherwise, we use what LaTeX
2940 os << "\\textless{}";
2944 os << "\\textgreater{}";
2948 os << "\\textbar{}";
2954 case '-': // "--" in Typewriter mode -> "-{}-"
2956 && GetChar(i + 1) == '-'
2957 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
2966 os << "\\char`\\\"{}";
2971 if (bparams.inputenc == "default") {
2980 case '%': case '#': case '{':
2987 os << "\\textasciitilde{}";
2992 os << "\\textasciicircum{}";
2996 case '*': case '[': case ']':
2997 // avoid being mistaken for optional arguments
2998 os << '{' << c << '}';
3003 // Blanks are printed before font switching.
3004 // Sure? I am not! (try nice-latex)
3005 // I am sure it's correct. LyX might be smarter
3006 // in the future, but for now, nothing wrong is
3011 /* idea for labels --- begin*/
3015 && font.family() != LyXFont::TYPEWRITER_FAMILY
3016 && GetChar(i + 1) == 'y'
3017 && GetChar(i + 2) == 'X') {
3025 && font.family() != LyXFont::TYPEWRITER_FAMILY
3026 && GetChar(i + 1) == 'e'
3027 && GetChar(i + 2) == 'X') {
3032 // Check for "LaTeX2e"
3035 && font.family() != LyXFont::TYPEWRITER_FAMILY
3036 && GetChar(i + 1) == 'a'
3037 && GetChar(i + 2) == 'T'
3038 && GetChar(i + 3) == 'e'
3039 && GetChar(i + 4) == 'X'
3040 && GetChar(i + 5) == '2'
3041 && GetChar(i + 6) == 'e') {
3046 // Check for "LaTeX"
3049 && font.family() != LyXFont::TYPEWRITER_FAMILY
3050 && GetChar(i + 1) == 'a'
3051 && GetChar(i + 2) == 'T'
3052 && GetChar(i + 3) == 'e'
3053 && GetChar(i + 4) == 'X') {
3057 /* idea for labels --- end*/
3058 } else if (c != '\0') {
3068 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3069 BufferParams const & bparams,
3070 ostream & os, TexRow & texrow
3073 TexRow & foot_texrow,
3078 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3079 LyXParagraph * par = this;
3082 (par->depth == depth)
3084 && (par->footnoteflag == footnoteflag)
3089 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3091 if (textclasslist.Style(bparams.textclass,
3092 par->layout).isEnvironment()
3093 || par->pextra_type != PEXTRA_NONE) {
3094 par = par->TeXEnvironment(buf, bparams,
3102 par = par->TeXOnePar(buf, bparams,
3112 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3118 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3119 BufferParams const & bparams,
3120 ostream & os, TexRow & texrow
3123 TexRow & foot_texrow,
3128 bool eindent_open = false;
3130 bool foot_this_level = false;
3132 // flags when footnotetext should be appended to file.
3133 static bool minipage_open = false;
3134 static int minipage_open_depth = 0;
3135 char par_sep = bparams.paragraph_separation;
3137 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3140 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3143 LyXLayout const & style =
3144 textclasslist.Style(bparams.textclass,
3147 if (pextra_type == PEXTRA_INDENT) {
3148 if (!pextra_width.empty()) {
3149 os << "\\begin{LyXParagraphIndent}{"
3150 << pextra_width << "}\n";
3152 //float ib = atof(pextra_widthp.c_str())/100;
3153 // string can't handle floats at present (971109)
3154 // so I'll do a conversion by hand knowing that
3155 // the limits are 0.0 to 1.0. ARRae.
3156 os << "\\begin{LyXParagraphIndent}{";
3157 switch (pextra_widthp.length()) {
3169 os << "\\columnwidth}\n";
3172 eindent_open = true;
3174 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3175 if (pextra_hfill && Previous() &&
3176 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3177 os << "\\hfill{}\n";
3180 if (par_sep == BufferParams::PARSEP_INDENT) {
3181 os << "{\\setlength\\parindent{0pt}\n";
3184 os << "\\begin{minipage}";
3185 switch (pextra_alignment) {
3186 case MINIPAGE_ALIGN_TOP:
3189 case MINIPAGE_ALIGN_MIDDLE:
3192 case MINIPAGE_ALIGN_BOTTOM:
3196 if (!pextra_width.empty()) {
3197 os << '{' << pextra_width << "}\n";
3199 //float ib = atof(par->pextra_width.c_str())/100;
3200 // string can't handle floats at present
3201 // so I'll do a conversion by hand knowing that
3202 // the limits are 0.0 to 1.0. ARRae.
3204 switch (pextra_widthp.length()) {
3216 os << "\\columnwidth}\n";
3219 if (par_sep == BufferParams::PARSEP_INDENT) {
3220 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3223 minipage_open = true;
3224 minipage_open_depth = depth;
3227 #ifdef WITH_WARNINGS
3228 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3229 //I disabled it because it breaks when lists span on several
3232 if (style.isEnvironment()){
3233 if (style.latextype == LATEX_LIST_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 << labelwidthstring << "}\n";
3244 } else if (style.labeltype == LABEL_BIBLIO) {
3246 os << "\\begin{" << style.latexname() << "}{"
3247 << bibitemWidest(buf)
3249 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3250 #ifdef FANCY_FOOTNOTE_CODE
3251 if (foot_count < 0) {
3252 // flag that footnote[mark][text] should be
3253 // used for any footnotes from now on
3255 foot_this_level = true;
3258 os << "\\begin{" << style.latexname() << '}'
3259 << style.latexparam() << '\n';
3261 os << "\\begin{" << style.latexname() << '}'
3262 << style.latexparam() << '\n';
3265 LyXParagraph * par = this;
3267 par = par->TeXOnePar(buf, bparams,
3271 foot, foot_texrow, foot_count
3275 if (minipage_open && par && !style.isEnvironment() &&
3276 (par->pextra_type == PEXTRA_MINIPAGE) &&
3277 par->pextra_start_minipage) {
3278 os << "\\end{minipage}\n";
3280 if (par_sep == BufferParams::PARSEP_INDENT) {
3284 minipage_open = false;
3286 if (par && par->depth > depth) {
3287 if (textclasslist.Style(bparams.textclass,
3288 par->layout).isParagraph()
3290 // How to handle this? (Lgb)
3291 //&& !suffixIs(os, "\n\n")
3293 // There should be at least one '\n' already
3294 // but we need there to be two for Standard
3295 // paragraphs that are depth-increment'ed to be
3296 // output correctly. However, tables can
3297 // also be paragraphs so don't adjust them.
3300 // Will it ever harm to have one '\n' too
3301 // many? i.e. that we sometimes will have
3302 // three in a row. (Lgb)
3306 par = par->TeXDeeper(buf, bparams, os, texrow
3308 ,foot, foot_texrow, foot_count
3312 if (par && par->layout == layout && par->depth == depth &&
3313 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3314 if (par->pextra_hfill && par->Previous() &&
3315 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3316 os << "\\hfill{}\n";
3319 if (par_sep == BufferParams::PARSEP_INDENT) {
3320 os << "{\\setlength\\parindent{0pt}\n";
3323 os << "\\begin{minipage}";
3324 switch (par->pextra_alignment) {
3325 case MINIPAGE_ALIGN_TOP:
3328 case MINIPAGE_ALIGN_MIDDLE:
3331 case MINIPAGE_ALIGN_BOTTOM:
3335 if (!par->pextra_width.empty()) {
3336 os << '{' << par->pextra_width << "}\n";
3338 //float ib = atof(par->pextra_widthp.c_str())/100;
3339 // string can't handle floats at present
3340 // so I'll do a conversion by hand knowing that
3341 // the limits are 0.0 to 1.0. ARRae.
3343 switch (par->pextra_widthp.length()) {
3348 os << "0." << par->pextra_widthp;
3351 os << "0.0" << par->pextra_widthp;
3353 os << "\\columnwidth}\n";
3356 if (par_sep == BufferParams::PARSEP_INDENT) {
3357 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3360 minipage_open = true;
3361 minipage_open_depth = par->depth;
3364 && par->layout == layout
3365 && par->depth == depth
3366 && par->pextra_type == pextra_type
3368 && par->footnoteflag == footnoteflag
3372 if (style.isEnvironment()) {
3373 os << "\\end{" << style.latexname() << '}';
3375 // maybe this should go after the minipage closes?
3376 if (foot_this_level) {
3377 if (foot_count >= 1) {
3378 if (foot_count > 1) {
3379 os << "\\addtocounter{footnote}{-"
3384 texrow += foot_texrow;
3386 foot_texrow.reset();
3392 if (minipage_open && (minipage_open_depth == depth) &&
3393 (!par || par->pextra_start_minipage ||
3394 par->pextra_type != PEXTRA_MINIPAGE)) {
3395 os << "\\end{minipage}\n";
3397 if (par_sep == BufferParams::PARSEP_INDENT) {
3401 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3402 os << "\\medskip\n\n";
3406 minipage_open = false;
3409 os << "\\end{LyXParagraphIndent}\n";
3412 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3413 && par->pextra_hfill)) {
3417 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3418 return par; // ale970302
3423 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3424 BufferParams const & bparams,
3425 ostream & os, TexRow & texrow,
3426 ostream & foot, TexRow & foot_texrow,
3430 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3431 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3432 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3433 "No footnote!" << endl;
3435 LyXParagraph * par = this;
3436 LyXLayout const & style =
3437 textclasslist.Style(bparams.textclass,
3438 previous->GetLayout());
3440 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3441 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3442 "Float other than footnote in command"
3443 " with moving argument is illegal" << endl;
3446 if (footnotekind != LyXParagraph::FOOTNOTE
3447 && footnotekind != LyXParagraph::MARGIN
3450 // How to solve this?
3451 //&& !suffixIs(file, '\n')
3453 // we need to ensure that real floats like tables and figures
3454 // have their \begin{} on a new line otherwise we can get
3455 // incorrect results when using the endfloat.sty package
3456 // especially if two floats follow one another. ARRae 981022
3457 // NOTE: if the file is length 0 it must have just been
3458 // written out so we assume it ended with a '\n'
3460 // As far as I can see there is never any harm in writing
3461 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3466 bool moving_arg = false;
3467 bool need_closing = false;
3468 bool is_rtl = isRightToLeftPar(bparams);
3470 if (is_rtl != parent_is_rtl) {
3475 need_closing = true;
3478 bool footer_in_body = true;
3479 switch (footnotekind) {
3480 case LyXParagraph::FOOTNOTE:
3481 if (style.intitle) {
3482 os << "\\thanks{\n";
3483 footer_in_body = false;
3486 if (foot_count == -1) {
3487 // we're at depth 0 so we can use:
3488 os << "\\footnote{%\n";
3489 footer_in_body = false;
3491 os << "\\footnotemark{}%\n";
3493 // we only need this when there are
3494 // multiple footnotes
3495 os << "\\stepcounter{footnote}";
3497 os << "\\footnotetext{%\n";
3498 foot_texrow.start(this, 0);
3499 foot_texrow.newline();
3504 case LyXParagraph::MARGIN:
3505 os << "\\marginpar{\n";
3507 case LyXParagraph::FIG:
3508 if (pextra_type == PEXTRA_FLOATFLT
3509 && (!pextra_width.empty()
3510 || !pextra_widthp.empty())) {
3511 if (!pextra_width.empty())
3512 os << "\\begin{floatingfigure}{"
3513 << pextra_width << "}\n";
3515 os << "\\begin{floatingfigure}{"
3516 << lyx::atoi(pextra_widthp) / 100.0
3517 << "\\textwidth}\n";
3519 os << "\\begin{figure}";
3520 if (!bparams.float_placement.empty()) {
3521 os << '[' << bparams.float_placement << "]\n";
3527 case LyXParagraph::TAB:
3528 os << "\\begin{table}";
3529 if (!bparams.float_placement.empty()) {
3530 os << '[' << bparams.float_placement << "]\n";
3535 case LyXParagraph::WIDE_FIG:
3536 os << "\\begin{figure*}";
3537 if (!bparams.float_placement.empty()) {
3538 os << '[' << bparams.float_placement << "]\n";
3543 case LyXParagraph::WIDE_TAB:
3544 os << "\\begin{table*}";
3545 if (!bparams.float_placement.empty()) {
3546 os << '[' << bparams.float_placement << "]\n";
3551 case LyXParagraph::ALGORITHM:
3552 os << "\\begin{algorithm}\n";
3557 if (footnotekind != LyXParagraph::FOOTNOTE
3558 || !footer_in_body) {
3559 // Process text for all floats except footnotes in body
3561 LyXLayout const & style =
3563 .Style(bparams.textclass, par->layout);
3565 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3567 if (style.isEnvironment()
3568 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3569 // Allows the use of minipages within float
3570 // environments. Shouldn't be circular because
3571 // we don't support footnotes inside
3572 // floats (yet). ARRae
3573 par = par->TeXEnvironment(buf, bparams, os,
3578 par = par->TeXOnePar(buf, bparams,
3579 os, texrow, moving_arg,
3584 if (par && !par->IsDummy() && par->depth > depth) {
3585 par = par->TeXDeeper(buf, bparams, os, texrow,
3589 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3591 // process footnotes > depth 0 or in environments separately
3592 // NOTE: Currently don't support footnotes within footnotes
3593 // even though that is possible using the \footnotemark
3594 std::ostringstream dummy;
3595 TexRow dummy_texrow;
3596 int dummy_count = 0;
3598 LyXLayout const & style =
3600 .Style(bparams.textclass, par->layout);
3602 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3604 if (style.isEnvironment()
3605 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3606 // Allows the use of minipages within float
3607 // environments. Shouldn't be circular because
3608 // we don't support footnotes inside
3609 // floats (yet). ARRae
3610 par = par->TeXEnvironment(buf, bparams,
3612 dummy, dummy_texrow,
3615 par = par->TeXOnePar(buf, bparams,
3618 dummy, dummy_texrow,
3622 if (par && !par->IsDummy() && par->depth > depth) {
3623 par = par->TeXDeeper(buf, bparams,
3625 dummy, dummy_texrow,
3629 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3631 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3632 "Footnote in a Footnote -- not supported"
3637 switch (footnotekind) {
3638 case LyXParagraph::FOOTNOTE:
3639 if (footer_in_body) {
3640 // This helps tell which of the multiple
3641 // footnotetexts an error was in.
3643 foot_texrow.newline();
3648 case LyXParagraph::MARGIN:
3651 case LyXParagraph::FIG:
3652 if (pextra_type == PEXTRA_FLOATFLT
3653 && (!pextra_width.empty()
3654 || !pextra_widthp.empty()))
3655 os << "\\end{floatingfigure}";
3657 os << "\\end{figure}";
3659 case LyXParagraph::TAB:
3660 os << "\\end{table}";
3662 case LyXParagraph::WIDE_FIG:
3663 os << "\\end{figure*}";
3665 case LyXParagraph::WIDE_TAB:
3666 os << "\\end{table*}";
3668 case LyXParagraph::ALGORITHM:
3669 os << "\\end{algorithm}";
3676 if (footnotekind != LyXParagraph::FOOTNOTE
3677 && footnotekind != LyXParagraph::MARGIN) {
3678 // we need to ensure that real floats like tables and figures
3679 // have their \end{} on a line of their own otherwise we can
3680 // get incorrect results when using the endfloat.sty package.
3685 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3690 bool LyXParagraph::IsDummy() const
3692 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3693 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3697 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3698 int type, string const & width,
3699 string const & widthp)
3702 pextra_width = width;
3703 pextra_widthp = widthp;
3705 if (textclasslist.Style(bparams.textclass,
3706 layout).isEnvironment()) {
3707 LyXParagraph * par = this;
3708 LyXParagraph * ppar = par;
3710 while (par && (par->layout == layout)
3711 && (par->depth == depth)) {
3713 par = par->Previous();
3716 par = par->FirstPhysicalPar();
3718 while (par && par->depth > depth) {
3719 par = par->Previous();
3722 par = par->FirstPhysicalPar();
3727 while (par && (par->layout == layout)
3728 && (par->depth == depth)) {
3729 par->pextra_type = type;
3730 par->pextra_width = width;
3731 par->pextra_widthp = widthp;
3733 par = par->NextAfterFootnote();
3737 if (par && (par->depth > depth))
3738 par->SetPExtraType(bparams,
3739 type, width, widthp);
3741 while (par && ((par->depth > depth) || par->IsDummy()))
3742 par = par->NextAfterFootnote();
3744 while (par && ((par->depth > depth)))
3752 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3754 if (pextra_type == PEXTRA_NONE)
3757 pextra_type = PEXTRA_NONE;
3758 pextra_width.erase();
3759 pextra_widthp.erase();
3761 if (textclasslist.Style(bparams.textclass,
3762 layout).isEnvironment()) {
3763 LyXParagraph * par = this;
3764 LyXParagraph * ppar = par;
3766 while (par && (par->layout == layout)
3767 && (par->depth == depth)) {
3769 par = par->Previous();
3772 par = par->FirstPhysicalPar();
3774 while (par && par->depth > depth) {
3775 par = par->Previous();
3778 par = par->FirstPhysicalPar();
3783 while (par && (par->layout == layout)
3784 && (par->depth == depth)) {
3785 par->pextra_type = PEXTRA_NONE;
3786 par->pextra_width.erase();
3787 par->pextra_widthp.erase();
3789 par = par->NextAfterFootnote();
3793 if (par && (par->depth > depth))
3794 par->UnsetPExtraType(bparams);
3796 while (par && ((par->depth > depth) || par->IsDummy()))
3797 par = par->NextAfterFootnote();
3799 while (par && ((par->depth > depth)))
3807 bool LyXParagraph::IsHfill(size_type pos) const
3809 return IsHfillChar(GetChar(pos));
3813 bool LyXParagraph::IsInset(size_type pos) const
3815 return IsInsetChar(GetChar(pos));
3820 bool LyXParagraph::IsFloat(size_type pos) const
3822 return IsFloatChar(GetChar(pos));
3827 bool LyXParagraph::IsNewline(size_type pos) const
3829 return pos >= 0 && IsNewlineChar(GetChar(pos));
3833 bool LyXParagraph::IsSeparator(size_type pos) const
3835 return IsSeparatorChar(GetChar(pos));
3839 bool LyXParagraph::IsLineSeparator(size_type pos) const
3841 return IsLineSeparatorChar(GetChar(pos));
3845 bool LyXParagraph::IsKomma(size_type pos) const
3847 return IsKommaChar(GetChar(pos));
3851 /// Used by the spellchecker
3852 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3854 value_type const c = GetChar(pos);
3855 if (IsLetterChar(c))
3857 // '\0' is not a letter, allthough every string contains "" (below)
3860 // We want to pass the ' and escape chars to ispell
3861 string const extra = lyxrc.isp_esc_chars + '\'';
3862 char ch[2] = { c, 0 };
3863 return contains(extra, ch);
3867 bool LyXParagraph::IsWord(size_type pos ) const
3869 return IsWordChar(GetChar(pos)) ;
3874 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3878 return FirstPhysicalPar()->getParLanguage(bparams);
3882 #ifdef DO_USE_DEFAULT_LANGUAGE
3883 Language const * lang = GetFirstFontSettings().language();
3885 if (lang->lang() == "default")
3886 return bparams.language;
3889 return GetFirstFontSettings().language();
3891 } else if (previous)
3892 return previous->getParLanguage(bparams);
3894 return bparams.language;
3898 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3900 return lyxrc.rtl_support
3901 && getParLanguage(bparams)->RightToLeft();
3905 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3906 Language const * from, Language const * to)
3908 for (size_type i = 0; i < size(); ++i) {
3909 LyXFont font = GetFontSettings(bparams, i);
3910 if (font.language() == from) {
3911 font.setLanguage(to);
3918 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3920 Language const * doc_language = bparams.language;
3921 for (FontList::const_iterator cit = fontlist.begin();
3922 cit != fontlist.end(); ++cit)
3923 if ((*cit).font.language() != doc_language)
3929 // Convert the paragraph to a string.
3930 // Used for building the table of contents
3931 string const LyXParagraph::String(Buffer const * buffer, bool label)
3933 BufferParams const & bparams = buffer->params;
3936 if (label && !IsDummy() && !labelstring.empty())
3938 if (label && !labelstring.empty())
3940 s += labelstring + ' ';
3941 string::size_type const len = s.size();
3943 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3944 value_type c = GetChar(i);
3947 else if (c == META_INSET &&
3948 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3949 std::ostringstream ost;
3950 GetInset(i)->Ascii(buffer, ost);
3951 s += subst(ost.str().c_str(),'\n',' ');
3956 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
3957 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
3958 s += NextAfterFootnote()->String(buffer, false);
3962 if (isRightToLeftPar(bparams))
3963 reverse(s.begin() + len,s.end());
3971 string const LyXParagraph::String(Buffer const * buffer,
3972 LyXParagraph::size_type beg,
3973 LyXParagraph::size_type end)
3978 if (beg == 0 && !IsDummy() && !labelstring.empty())
3980 if (beg == 0 && !labelstring.empty())
3982 s += labelstring + ' ';
3984 for (LyXParagraph::size_type i = beg; i < end; ++i) {
3985 value_type c = GetChar(i);
3988 else if (c == META_INSET) {
3989 std::ostringstream ost;
3990 GetInset(i)->Ascii(buffer, ost);
3991 s += ost.str().c_str();
3999 void LyXParagraph::SetInsetOwner(Inset * i)
4002 for (InsetList::const_iterator cit = insetlist.begin();
4003 cit != insetlist.end(); ++cit) {
4005 (*cit).inset->setOwner(i);
4010 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4013 for (InsetList::const_iterator cit = insetlist.begin();
4014 cit != insetlist.end(); ++cit) {
4016 if ((*cit).inset->IsTextInset()) {
4017 static_cast<UpdatableInset *>
4018 ((*cit).inset)->deleteLyXText(bv);
4025 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4028 for (InsetList::const_iterator cit = insetlist.begin();
4029 cit != insetlist.end(); ++cit) {
4031 if ((*cit).inset->IsTextInset()) {
4032 static_cast<UpdatableInset *>
4033 ((*cit).inset)->resizeLyXText(bv);