1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation "lyxparagraph.h"
21 #include "lyxparagraph.h"
22 #include "support/textutils.h"
25 #include "tex-strings.h"
27 #include "bufferparams.h"
28 #include "support/FileInfo.h"
29 #include "support/LAssert.h"
31 #include "LaTeXFeatures.h"
32 #include "insets/insetinclude.h"
33 #include "insets/insetbib.h"
34 #include "insets/insettext.h"
35 #include "support/filetools.h"
36 #include "lyx_gui_misc.h"
38 #include "support/lyxmanip.h"
39 #include "BufferView.h"
46 using std::lower_bound;
47 using std::upper_bound;
50 int tex_code_break_column = 72; // needs non-zero initialization. set later.
51 // this is a bad idea, but how can LyXParagraph find its buffer to get
52 // parameters? (JMarc)
54 extern string bibitemWidest(Buffer const *);
56 // this is a minibuffer
57 static char minibuffer_char;
58 static LyXFont minibuffer_font;
59 static Inset * minibuffer_inset;
62 extern BufferView * current_view;
64 // Initialization of the counter for the paragraph id's,
65 // declared in lyxparagraph.h
66 unsigned int LyXParagraph::paragraph_id = 0;
69 LyXParagraph::LyXParagraph()
71 text.reserve(500); // is this number too big?
72 for (int i = 0; i < 10; ++i) setCounter(i , 0);
79 footnoteflag = LyXParagraph::NO_FOOTNOTE;
80 footnotekind = LyXParagraph::FOOTNOTE; // should not be needed
82 align = LYX_ALIGN_BLOCK;
86 bibkey = 0; // ale970302
91 // This konstruktor inserts the new paragraph in a list.
92 LyXParagraph::LyXParagraph(LyXParagraph * par)
97 for (int i = 0; i < 10; ++i) setCounter(i, 0);
101 // double linked list begin
104 next->previous = this;
106 previous->next = this;
109 footnoteflag = LyXParagraph::NO_FOOTNOTE;
110 footnotekind = LyXParagraph::FOOTNOTE;
113 id_ = paragraph_id++;
115 bibkey = 0; // ale970302
121 void LyXParagraph::writeFile(Buffer const * buf, ostream & os,
122 BufferParams const & params,
123 char footflag, char dth) const
127 footnoteflag != LyXParagraph::NO_FOOTNOTE ||
129 || previous->footnoteflag == LyXParagraph::NO_FOOTNOTE
134 // The beginning or the end of a footnote environment?
135 if (footflag != footnoteflag) {
136 footflag = footnoteflag;
138 os << "\n\\begin_float "
139 << string_footnotekinds[footnotekind]
142 os << "\n\\end_float ";
146 // The beginning or end of a deeper (i.e. nested) area?
149 while (depth > dth) {
150 os << "\n\\begin_deeper ";
154 while (depth < dth) {
155 os << "\n\\end_deeper ";
161 // First write the layout
163 << textclasslist.NameOfLayout(params.textclass, layout)
166 // Maybe some vertical spaces.
167 if (added_space_top.kind() != VSpace::NONE)
168 os << "\\added_space_top "
169 << added_space_top.asLyXCommand() << " ";
170 if (added_space_bottom.kind() != VSpace::NONE)
171 os << "\\added_space_bottom "
172 << added_space_bottom.asLyXCommand() << " ";
174 // Maybe the paragraph has special spacing
175 spacing.writeFile(os, true);
177 // The labelwidth string used in lists.
178 if (!labelwidthstring.empty())
179 os << "\\labelwidthstring "
180 << labelwidthstring << '\n';
182 // Lines above or below?
186 os << "\\line_bottom ";
188 // Pagebreaks above or below?
190 os << "\\pagebreak_top ";
191 if (pagebreak_bottom)
192 os << "\\pagebreak_bottom ";
194 // Start of appendix?
195 if (start_of_appendix)
196 os << "\\start_of_appendix ";
203 if (align != LYX_ALIGN_LAYOUT) {
206 case LYX_ALIGN_LEFT: h = 1; break;
207 case LYX_ALIGN_RIGHT: h = 2; break;
208 case LYX_ALIGN_CENTER: h = 3; break;
209 default: h = 0; break;
211 os << "\\align " << string_align[h] << " ";
213 if (pextra_type != PEXTRA_NONE) {
214 os << "\\pextra_type " << pextra_type;
215 if (pextra_type == PEXTRA_MINIPAGE) {
216 os << " \\pextra_alignment "
219 os << " \\pextra_hfill "
221 if (pextra_start_minipage)
222 os << " \\pextra_start_minipage "
223 << pextra_start_minipage;
225 if (!pextra_width.empty()) {
226 os << " \\pextra_width "
227 << VSpace(pextra_width).asLyXCommand();
228 } else if (!pextra_widthp.empty()) {
229 os << " \\pextra_widthp "
236 // Dummy layout. This means that a footnote ended.
237 os << "\n\\end_float ";
238 footflag = LyXParagraph::NO_FOOTNOTE;
244 bibkey->Write(buf, os);
246 LyXFont font1(LyXFont::ALL_INHERIT, params.language);
249 for (size_type i = 0; i < size(); ++i) {
255 // Write font changes
256 LyXFont font2 = GetFontSettings(params, i);
257 if (font2 != font1) {
258 font2.lyxWriteChanges(font1, os);
263 value_type const c = GetChar(i);
267 Inset const * inset = GetInset(i);
269 if (inset->DirectWrite()) {
270 // international char, let it write
271 // code directly so it's shorter in
273 inset->Write(buf, os);
275 os << "\n\\begin_inset ";
276 inset->Write(buf, os);
277 os << "\n\\end_inset \n\n";
283 os << "\n\\newline \n";
287 os << "\n\\hfill \n";
291 os << "\n\\backslash \n";
295 if (i + 1 < size() && GetChar(i + 1) == ' ') {
302 if ((column > 70 && c == ' ')
307 // this check is to amend a bug. LyX sometimes
308 // inserts '\0' this could cause problems.
312 lyxerr << "ERROR (LyXParagraph::writeFile):"
313 " NULL char in structure." << endl;
319 // now write the next paragraph
321 next->writeFile(buf, os, params, footflag, dth);
325 void LyXParagraph::validate(LaTeXFeatures & features) const
327 BufferParams const & params = features.bufferParams();
330 // this will be useful later
331 LyXLayout const & layout =
332 textclasslist.Style(params.textclass,
337 if (line_top || line_bottom)
338 features.lyxline = true;
339 if (!spacing.isDefault())
340 features.setspace = true;
343 features.layout[GetLayout()] = true;
346 Language const * doc_language = params.language;
348 for (FontList::const_iterator cit = fontlist.begin();
349 cit != fontlist.end(); ++cit) {
350 if ((*cit).font.noun() == LyXFont::ON) {
351 lyxerr[Debug::LATEX] << "font.noun: "
352 << (*cit).font.noun()
354 features.noun = true;
355 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
356 << (*cit).font.stateText(0)
359 switch ((*cit).font.color()) {
361 case LColor::inherit:
365 features.color = true;
366 lyxerr[Debug::LATEX] << "Color enabled. Font: "
367 << (*cit).font.stateText(0)
371 Language const * language = (*cit).font.language();
372 if (language->babel() != doc_language->babel()) {
373 features.UsedLanguages.insert(language);
374 lyxerr[Debug::LATEX] << "Found language "
375 << language->babel() << endl;
380 for (InsetList::const_iterator cit = insetlist.begin();
381 cit != insetlist.end(); ++cit) {
383 (*cit).inset->Validate(features);
386 if (pextra_type == PEXTRA_INDENT)
387 features.LyXParagraphIndent = true;
388 if (pextra_type == PEXTRA_FLOATFLT)
389 features.floatflt = true;
391 if (layout.needprotect
392 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
393 features.NeedLyXFootnoteCode = true;
395 if (params.paragraph_separation == BufferParams::PARSEP_INDENT
396 && pextra_type == LyXParagraph::PEXTRA_MINIPAGE)
397 features.NeedLyXMinipageIndent = true;
399 if (footnoteflag != NO_FOOTNOTE && footnotekind == ALGORITHM)
400 features.algorithm = true;
405 // First few functions needed for cut and paste and paragraph breaking.
406 void LyXParagraph::CopyIntoMinibuffer(Buffer const & buffer,
407 LyXParagraph::size_type pos) const
409 BufferParams bparams = buffer.params;
411 minibuffer_char = GetChar(pos);
412 minibuffer_font = GetFontSettings(bparams, pos);
413 minibuffer_inset = 0;
414 if (minibuffer_char == LyXParagraph::META_INSET) {
416 minibuffer_inset = GetInset(pos)->Clone(buffer);
418 minibuffer_inset = 0;
419 minibuffer_char = ' ';
420 // This reflects what GetInset() does (ARRae)
426 void LyXParagraph::CutIntoMinibuffer(BufferParams const & bparams,
427 LyXParagraph::size_type pos)
429 minibuffer_char = GetChar(pos);
430 minibuffer_font = GetFontSettings(bparams, pos);
431 minibuffer_inset = 0;
432 if (minibuffer_char == LyXParagraph::META_INSET) {
434 minibuffer_inset = GetInset(pos);
435 // This is a little hack since I want exactly
436 // the inset, not just a clone. Otherwise
437 // the inset would be deleted when calling Erase(pos)
439 InsetTable search_elem(pos, 0);
440 InsetList::iterator it =
441 lower_bound(insetlist.begin(),
443 search_elem, matchIT());
444 if (it != insetlist.end() && (*it).pos == pos)
447 minibuffer_inset = 0;
448 minibuffer_char = ' ';
449 // This reflects what GetInset() does (ARRae)
454 // Erase(pos); now the caller is responsible for that.
458 bool LyXParagraph::InsertFromMinibuffer(LyXParagraph::size_type pos)
460 if ((minibuffer_char == LyXParagraph::META_INSET) &&
461 !InsertInsetAllowed(minibuffer_inset))
463 if (minibuffer_char == LyXParagraph::META_INSET)
464 InsertInset(pos, minibuffer_inset, minibuffer_font);
466 InsertChar(pos, minibuffer_char, minibuffer_font);
474 void LyXParagraph::Clear()
479 pagebreak_top = false;
480 pagebreak_bottom = false;
482 added_space_top = VSpace(VSpace::NONE);
483 added_space_bottom = VSpace(VSpace::NONE);
484 spacing.set(Spacing::Default);
486 align = LYX_ALIGN_LAYOUT;
490 pextra_type = PEXTRA_NONE;
491 pextra_width.erase();
492 pextra_widthp.erase();
493 pextra_alignment = MINIPAGE_ALIGN_TOP;
494 pextra_hfill = false;
495 pextra_start_minipage = false;
498 labelwidthstring.erase();
502 start_of_appendix = false;
506 // the destructor removes the new paragraph from the list
507 LyXParagraph::~LyXParagraph()
510 previous->next = next;
512 next->previous = previous;
514 for (InsetList::iterator it = insetlist.begin();
515 it != insetlist.end(); ++it) {
522 //lyxerr << "LyXParagraph::paragraph_id = "
523 // << LyXParagraph::paragraph_id << endl;
527 void LyXParagraph::Erase(LyXParagraph::size_type pos)
530 // > because last is the next unused position, and you can
531 // use it if you want
533 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
534 NextAfterFootnote()->Erase(pos - text.size() - 1);
536 lyxerr.debug() << "ERROR (LyXParagraph::Erase): "
537 "position does not exist." << endl;
541 Assert(pos < size());
544 if (pos < size()) { // last is free for insertation, but should be empty
546 // if it is an inset, delete the inset entry
547 if (text[pos] == LyXParagraph::META_INSET) {
549 InsetTable search_inset(pos, 0);
550 InsetList::iterator it =
551 lower_bound(insetlist.begin(),
553 search_inset, matchIT());
554 if (it != insetlist.end() && (*it).pos == pos) {
560 text.erase(text.begin() + pos);
562 // Erase entries in the tables.
563 FontTable search_font(pos, LyXFont());
565 FontList::iterator it =
566 lower_bound(fontlist.begin(),
568 search_font, matchFT());
569 if (it != fontlist.end() && (*it).pos == pos &&
571 (it != fontlist.begin() && (*(it - 1)).pos == pos - 1))) {
572 // If it is a multi-character font
573 // entry, we just make it smaller
574 // (see update below), otherwise we
576 unsigned int const i = it - fontlist.begin();
577 fontlist.erase(fontlist.begin() + i);
578 it = fontlist.begin() + i;
579 if (i > 0 && i < fontlist.size() &&
580 fontlist[i - 1].font == fontlist[i].font) {
581 fontlist.erase(fontlist.begin() + i - 1);
582 it = fontlist.begin() + i - 1;
586 // Update all other entries.
587 FontList::iterator fend = fontlist.end();
588 for (; it != fend; ++it)
591 // Update the inset table.
592 InsetTable search_inset(pos, 0);
593 InsetList::iterator lend = insetlist.end();
594 for (InsetList::iterator it =
595 upper_bound(insetlist.begin(),
597 search_inset, matchIT());
602 lyxerr << "ERROR (LyXParagraph::Erase): "
603 "can't erase non-existant char." << endl;
609 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
610 LyXParagraph::value_type c)
612 LyXFont const f(LyXFont::ALL_INHERIT);
613 InsertChar(pos, c, f);
617 void LyXParagraph::InsertChar(LyXParagraph::size_type pos,
618 LyXParagraph::value_type c,
619 LyXFont const & font)
622 // > because last is the next unused position, and you can
623 // use it if you want
626 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
627 NextAfterFootnote()->InsertChar(pos - text.size() - 1,
630 lyxerr.debug() << "ERROR (LyXParagraph::InsertChar): "
631 "position does not exist." << endl;
635 Assert(pos <= size());
637 text.insert(text.begin() + pos, c);
639 // Update the font table.
640 FontTable search_font(pos, LyXFont());
641 for (FontList::iterator it = lower_bound(fontlist.begin(),
643 search_font, matchFT());
644 it != fontlist.end(); ++it)
647 // Update the inset table.
648 InsetTable search_inset(pos, 0);
649 for (InsetList::iterator it = lower_bound(insetlist.begin(),
651 search_inset, matchIT());
652 it != insetlist.end(); ++it)
659 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
662 LyXFont const f(LyXFont::ALL_INHERIT);
663 InsertInset(pos, inset, f);
667 void LyXParagraph::InsertInset(LyXParagraph::size_type pos,
668 Inset * inset, LyXFont const & font)
673 // > because last is the next unused position, and you can
674 // use it if you want
677 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
679 ->InsertInset(pos - text.size() - 1,
682 lyxerr << "ERROR (LyXParagraph::InsertInset): "
683 "position does not exist: " << pos << endl;
687 Assert(pos <= size());
690 InsertChar(pos, META_INSET, font);
691 Assert(text[pos] == META_INSET);
693 // Add a new entry in the inset table.
694 InsetTable search_inset(pos, 0);
695 InsetList::iterator it = lower_bound(insetlist.begin(),
697 search_inset, matchIT());
698 if (it != insetlist.end() && (*it).pos == pos)
699 lyxerr << "ERROR (LyXParagraph::InsertInset): "
700 "there is an inset in position: " << pos << endl;
702 insetlist.insert(it, InsetTable(pos, inset));
704 inset->setOwner(inset_owner);
708 bool LyXParagraph::InsertInsetAllowed(Inset * inset)
710 //lyxerr << "LyXParagraph::InsertInsetAllowed" << endl;
713 return inset_owner->InsertInsetAllowed(inset);
718 Inset * LyXParagraph::GetInset(LyXParagraph::size_type pos)
723 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
724 return NextAfterFootnote()
725 ->GetInset(pos - text.size() - 1);
727 lyxerr << "ERROR (LyXParagraph::GetInset): "
728 "position does not exist: "
734 Assert(pos < size());
737 InsetTable search_inset(pos, 0);
738 InsetList::iterator it = lower_bound(insetlist.begin(),
740 search_inset, matchIT());
741 if (it != insetlist.end() && (*it).pos == pos)
744 lyxerr << "ERROR (LyXParagraph::GetInset): "
745 "Inset does not exist: " << pos << endl;
748 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
749 // Did this commenting out introduce a bug? So far I have not
750 // see any, please enlighten me. (Lgb)
751 // My guess is that since the inset does not exist, we might
752 // as well replace it with a space to prevent craches. (Asger)
757 Inset const * LyXParagraph::GetInset(LyXParagraph::size_type pos) const
762 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
763 return NextAfterFootnote()
764 ->GetInset(pos - text.size() - 1);
766 lyxerr << "ERROR (LyXParagraph::GetInset): "
767 "position does not exist: "
773 Assert(pos < size());
776 InsetTable search_inset(pos, 0);
777 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
779 search_inset, matchIT());
780 if (cit != insetlist.end() && (*cit).pos == pos)
783 lyxerr << "ERROR (LyXParagraph::GetInset): "
784 "Inset does not exist: " << pos << endl;
786 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
787 // Did this commenting out introduce a bug? So far I have not
788 // see any, please enlighten me. (Lgb)
789 // My guess is that since the inset does not exist, we might
790 // as well replace it with a space to prevent craches. (Asger)
795 // Gets uninstantiated font setting at position.
796 // Optimized after profiling. (Asger)
797 LyXFont const LyXParagraph::GetFontSettings(BufferParams const & bparams,
798 LyXParagraph::size_type pos) const
801 Assert(pos <= size());
806 FontTable search_font(pos, LyXFont());
807 FontList::const_iterator cit = lower_bound(fontlist.begin(),
809 search_font, matchFT());
810 if (cit != fontlist.end())
816 // > because last is the next unused position, and you can
817 // use it if you want
818 else if (pos > size()) {
820 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
821 return NextAfterFootnote()
822 ->GetFontSettings(bparams,
823 pos - text.size() - 1);
825 // Why is it an error to ask for the font of a
826 // position that does not exist? Would it be
827 // enough for this to be enabled on debug?
828 // We want strict error checking, but it's ok to only
829 // have it when debugging. (Asger)
830 lyxerr << "ERROR (LyXParagraph::GetFontSettings): "
831 "position does not exist. "
832 << pos << " (" << static_cast<int>(pos)
834 } else if (pos > 0) {
835 return GetFontSettings(bparams, pos - 1);
838 if (pos == size() && size())
839 return GetFontSettings(bparams, pos - 1);
843 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
845 //return LyXFont(LyXFont::ALL_INHERIT);
849 // Gets uninstantiated font setting at position 0
850 LyXFont const LyXParagraph::GetFirstFontSettings() const
853 if (!fontlist.empty())
854 return fontlist[0].font;
858 else if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
859 return NextAfterFootnote()->GetFirstFontSettings();
861 return LyXFont(LyXFont::ALL_INHERIT);
865 // Gets the fully instantiated font at a given position in a paragraph
866 // This is basically the same function as LyXText::GetFont() in text2.C.
867 // The difference is that this one is used for generating the LaTeX file,
868 // and thus cosmetic "improvements" are disallowed: This has to deliver
869 // the true picture of the buffer. (Asger)
870 // If position is -1, we get the layout font of the paragraph.
871 // If position is -2, we get the font of the manual label of the paragraph.
872 LyXFont const LyXParagraph::getFont(BufferParams const & bparams,
873 LyXParagraph::size_type pos) const
876 LyXLayout const & layout =
877 textclasslist.Style(bparams.textclass,
879 LyXParagraph::size_type main_body = 0;
880 if (layout.labeltype == LABEL_MANUAL)
881 main_body = BeginningOfMainBody();
886 layoutfont = layout.labelfont;
888 layoutfont = layout.font;
889 tmpfont = GetFontSettings(bparams, pos);
890 tmpfont.realize(layoutfont);
892 // process layoutfont for pos == -1 and labelfont for pos < -1
894 tmpfont = layout.font;
896 tmpfont = layout.labelfont;
897 tmpfont.setLanguage(getParLanguage(bparams));
900 // check for environment font information
901 char par_depth = GetDepth();
902 LyXParagraph const * par = this;
903 while (par && par_depth && !tmpfont.resolved()) {
904 par = par->DepthHook(par_depth - 1);
906 tmpfont.realize(textclasslist.
907 Style(bparams.textclass,
908 par->GetLayout()).font);
909 par_depth = par->GetDepth();
913 tmpfont.realize(textclasslist
914 .TextClass(bparams.textclass)
920 /// Returns the height of the highest font in range
922 LyXParagraph::HighestFontInRange(LyXParagraph::size_type startpos,
923 LyXParagraph::size_type endpos) const
925 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
926 if (fontlist.empty())
929 FontTable end_search(endpos, LyXFont());
930 FontList::const_iterator end_it = lower_bound(fontlist.begin(),
932 end_search, matchFT());
933 if (end_it != fontlist.end())
936 FontTable start_search(startpos, LyXFont());
937 for (FontList::const_iterator cit =
938 lower_bound(fontlist.begin(),
940 start_search, matchFT());
941 cit != end_it; ++cit) {
942 LyXFont::FONT_SIZE size = (*cit).font.size();
943 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
950 LyXParagraph::value_type
951 LyXParagraph::GetChar(LyXParagraph::size_type pos) const
956 Assert(pos <= size());
957 if (!size() || pos == size()) return '\0';
968 // > because last is the next unused position, and you can
969 // use it if you want
970 else if (pos > size()) {
971 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
972 return NextAfterFootnote()
973 ->GetChar(pos - text.size() - 1);
976 lyxerr << "ERROR (LyXParagraph::GetChar const): "
977 "position does not exist."
978 << pos << " (" << static_cast<int>(pos)
984 // We should have a footnote environment.
985 if (!next || next->footnoteflag == LyXParagraph::NO_FOOTNOTE) {
986 // Notice that LyX does request the
987 // last char from time to time. (Asger)
988 //lyxerr << "ERROR (LyXParagraph::GetChar): "
989 // "expected footnote." << endl;
992 switch (next->footnotekind) {
993 case LyXParagraph::FOOTNOTE:
994 return LyXParagraph::META_FOOTNOTE;
995 case LyXParagraph::MARGIN:
996 return LyXParagraph::META_MARGIN;
997 case LyXParagraph::FIG:
998 case LyXParagraph::WIDE_FIG:
999 return LyXParagraph::META_FIG;
1000 case LyXParagraph::TAB:
1001 case LyXParagraph::WIDE_TAB:
1002 return LyXParagraph::META_TAB;
1003 case LyXParagraph::ALGORITHM:
1004 return LyXParagraph::META_ALGORITHM;
1006 return '\0'; // to shut up gcc
1012 LyXParagraph::value_type
1013 LyXParagraph::GetUChar(BufferParams const & bparams,
1014 LyXParagraph::size_type pos) const
1016 value_type c = GetChar(pos);
1017 if (!lyxrc.rtl_support)
1047 if (uc != c && GetFontSettings(bparams, pos).isRightToLeft())
1053 // return an string of the current word, and the end of the word in lastpos.
1054 string const LyXParagraph::GetWord(LyXParagraph::size_type & lastpos) const
1056 Assert(lastpos >= 0);
1058 // the current word is defined as starting at the first character
1059 // from the immediate left of lastpospos which meets the definition
1060 // of IsLetter(), continuing to the last character to the right
1061 // of this meeting IsLetter.
1067 // move back until we have a letter
1069 //there's no real reason to have firstpos & lastpos as
1070 //separate variables as this is written, but maybe someon
1071 // will want to return firstpos in the future.
1073 //since someone might have typed a punctuation first
1074 int firstpos = lastpos;
1076 while ((firstpos >= 0) && !IsLetter(firstpos))
1079 // now find the beginning by looking for a nonletter
1081 while ((firstpos>= 0) && IsLetter(firstpos))
1084 // the above is now pointing to the preceeding non-letter
1088 // so copy characters into theword until we get a nonletter
1089 // note that this can easily exceed lastpos, wich means
1090 // that if used in the middle of a word, the whole word
1093 while (IsLetter(lastpos)) theword += GetChar(lastpos++);
1101 #warning Remember to get rid of this one. (Lgb)
1103 LyXParagraph::size_type LyXParagraph::Last() const
1106 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1107 return text.size() + NextAfterFootnote()->Last() + 1;
1108 // the 1 is the symbol
1117 LyXParagraph * LyXParagraph::ParFromPos(LyXParagraph::size_type pos)
1119 // > because last is the next unused position, and you can
1120 // use it if you want
1123 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1124 return NextAfterFootnote()
1125 ->ParFromPos(pos - text.size() - 1);
1127 lyxerr << "ERROR (LyXParagraph::ParFromPos): "
1128 "position does not exist." << endl;
1137 int LyXParagraph::PositionInParFromPos(LyXParagraph::size_type pos) const
1139 // > because last is the next unused position, and you can
1140 // use it if you want
1143 && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1144 return NextAfterFootnote()
1145 ->PositionInParFromPos(pos - text.size() - 1);
1148 "ERROR (LyXParagraph::PositionInParFromPos): "
1149 "position does not exist." << endl;
1158 void LyXParagraph::SetFont(LyXParagraph::size_type pos,
1159 LyXFont const & font)
1162 // > because last is the next unused position, and you can
1163 // use it if you want
1166 next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1167 NextAfterFootnote()->SetFont(pos - text.size() - 1,
1170 lyxerr << "ERROR (LyXParagraph::SetFont): "
1171 "position does not exist." << endl;
1176 Assert(pos <= size());
1179 // First, reduce font against layout/label font
1180 // Update: The SetCharFont() routine in text2.C already
1181 // reduces font, so we don't need to do that here. (Asger)
1182 // No need to simplify this because it will disappear
1183 // in a new kernel. (Asger)
1184 // Next search font table
1186 FontTable search_font(pos, LyXFont());
1187 FontList::iterator it = lower_bound(fontlist.begin(),
1189 search_font, matchFT());
1190 unsigned int i = it - fontlist.begin();
1191 bool notfound = it == fontlist.end();
1193 if (!notfound && fontlist[i].font == font)
1196 bool begin = pos == 0 || notfound ||
1197 (i > 0 && fontlist[i-1].pos == pos - 1);
1198 // Is position pos is a beginning of a font block?
1199 bool end = !notfound && fontlist[i].pos == pos;
1200 // Is position pos is the end of a font block?
1201 if (begin && end) { // A single char block
1202 if (i + 1 < fontlist.size() &&
1203 fontlist[i + 1].font == font) {
1204 // Merge the singleton block with the next block
1205 fontlist.erase(fontlist.begin() + i);
1206 if (i > 0 && fontlist[i - 1].font == font)
1207 fontlist.erase(fontlist.begin() + i-1);
1208 } else if (i > 0 && fontlist[i - 1].font == font) {
1209 // Merge the singleton block with the previous block
1210 fontlist[i - 1].pos = pos;
1211 fontlist.erase(fontlist.begin() + i);
1213 fontlist[i].font = font;
1215 if (i > 0 && fontlist[i - 1].font == font)
1216 fontlist[i - 1].pos = pos;
1218 fontlist.insert(fontlist.begin() + i,
1219 FontTable(pos, font));
1221 fontlist[i].pos = pos - 1;
1222 if (!(i + 1 < fontlist.size() &&
1223 fontlist[i + 1].font == font))
1224 fontlist.insert(fontlist.begin() + i + 1,
1225 FontTable(pos, font));
1226 } else { // The general case. The block is splitted into 3 blocks
1227 fontlist.insert(fontlist.begin() + i,
1228 FontTable(pos - 1, fontlist[i].font));
1229 fontlist.insert(fontlist.begin() + i + 1,
1230 FontTable(pos, font));
1235 // This function is able to hide closed footnotes.
1236 LyXParagraph * LyXParagraph::Next()
1239 if (next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1240 LyXParagraph * tmp = next;
1242 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1244 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1245 return tmp->Next(); /* there can be more than one
1246 footnote in a logical
1249 return next; // This should never happen!
1257 LyXParagraph * LyXParagraph::NextAfterFootnote()
1259 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1260 LyXParagraph * tmp = next;
1261 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1263 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1264 return tmp; /* there can be more than one footnote
1265 in a logical paragraph */
1267 return next; // This should never happen!
1275 LyXParagraph const * LyXParagraph::NextAfterFootnote() const
1277 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1278 LyXParagraph * tmp = next;
1279 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1281 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1282 return tmp; /* there can be more than one footnote
1283 in a logical paragraph */
1285 return next; // This should never happen!
1293 LyXParagraph * LyXParagraph::PreviousBeforeFootnote()
1296 if (previous && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
1298 while (tmp && tmp->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1299 tmp = tmp->previous;
1300 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1301 return tmp; /* there can be more than one footnote
1302 in a logical paragraph */
1304 return previous; // This should never happen!
1312 LyXParagraph * LyXParagraph::LastPhysicalPar()
1314 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1317 LyXParagraph * tmp = this;
1319 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1320 tmp = tmp->NextAfterFootnote();
1328 LyXParagraph const * LyXParagraph::LastPhysicalPar() const
1330 if (footnoteflag != LyXParagraph::NO_FOOTNOTE)
1333 LyXParagraph const * tmp = this;
1335 && tmp->next->footnoteflag != LyXParagraph::NO_FOOTNOTE)
1336 tmp = tmp->NextAfterFootnote();
1344 LyXParagraph * LyXParagraph::FirstPhysicalPar()
1348 LyXParagraph * tmppar = this;
1352 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1353 tmppar = tmppar->previous;
1364 LyXParagraph const * LyXParagraph::FirstPhysicalPar() const
1368 LyXParagraph const * tmppar = this;
1372 || tmppar->footnoteflag != LyXParagraph::NO_FOOTNOTE))
1373 tmppar = tmppar->previous;
1383 // This function is able to hide closed footnotes.
1384 LyXParagraph * LyXParagraph::Previous()
1387 LyXParagraph * tmp = previous;
1392 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1393 tmp = tmp->previous;
1395 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1396 tmp = tmp->previous;
1397 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1398 return tmp->next->Previous();
1408 // This function is able to hide closed footnotes.
1409 LyXParagraph const * LyXParagraph::Previous() const
1412 LyXParagraph * tmp = previous;
1416 && tmp->previous->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1417 tmp = tmp->previous;
1419 && tmp->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE)
1420 tmp = tmp->previous;
1421 if (tmp && tmp->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
1422 return tmp->next->Previous();
1432 void LyXParagraph::BreakParagraph(BufferParams const & bparams,
1433 LyXParagraph::size_type pos,
1438 // create a new paragraph
1441 size_type pos_first;
1442 LyXParagraph * par = ParFromPos(pos);
1443 LyXParagraph * firstpar = FirstPhysicalPar();
1445 LyXParagraph * tmp = new LyXParagraph(par);
1447 //LyXParagraph * par = this;
1448 //LyXParagraph * firstpar = this;
1449 LyXParagraph * tmp = new LyXParagraph(this);
1453 tmp->footnoteflag = footnoteflag;
1454 tmp->footnotekind = footnotekind;
1456 // this is an idea for a more userfriendly layout handling, I will
1457 // see what the users say
1460 // layout stays the same with latex-environments
1462 tmp->SetOnlyLayout(bparams, firstpar->layout);
1463 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1466 // layout stays the same with latex-environments
1468 tmp->SetOnlyLayout(bparams, layout);
1469 tmp->SetLabelWidthString(labelwidthstring);
1473 if (Last() > pos || !Last() || flag == 2) {
1474 tmp->SetOnlyLayout(bparams, firstpar->layout);
1475 tmp->align = firstpar->align;
1476 tmp->SetLabelWidthString(firstpar->labelwidthstring);
1478 tmp->line_bottom = firstpar->line_bottom;
1479 firstpar->line_bottom = false;
1480 tmp->pagebreak_bottom = firstpar->pagebreak_bottom;
1481 firstpar->pagebreak_bottom = false;
1482 tmp->added_space_bottom = firstpar->added_space_bottom;
1483 firstpar->added_space_bottom = VSpace(VSpace::NONE);
1485 tmp->depth = firstpar->depth;
1486 tmp->noindent = firstpar->noindent;
1488 if (Last() > pos || !Last() || flag == 2) {
1489 tmp->SetOnlyLayout(bparams, layout);
1491 tmp->SetLabelWidthString(labelwidthstring);
1493 tmp->line_bottom = line_bottom;
1494 line_bottom = false;
1495 tmp->pagebreak_bottom = pagebreak_bottom;
1496 pagebreak_bottom = false;
1497 tmp->added_space_bottom = added_space_bottom;
1498 added_space_bottom = VSpace(VSpace::NONE);
1501 tmp->noindent = noindent;
1503 // copy everything behind the break-position
1504 // to the new paragraph
1507 while (ParFromPos(pos_first) != par)
1509 pos_end = pos_first + par->text.size() - 1;
1512 for (i = j = pos; i <= pos_end; ++i) {
1513 par->CutIntoMinibuffer(bparams, i - pos_first);
1514 if (tmp->InsertFromMinibuffer(j - pos))
1520 for (i = pos_end; i >= pos; --i)
1521 par->Erase(i - pos_first);
1525 size_type pos_end = text.size() - 1;
1527 for (i = j = pos; i <= pos_end; ++i) {
1528 CutIntoMinibuffer(bparams, i);
1529 if (tmp->InsertFromMinibuffer(j - pos))
1533 for (i = pos_end; i >= pos; --i)
1541 // just an idea of me
1543 tmp->line_top = firstpar->line_top;
1544 tmp->pagebreak_top = firstpar->pagebreak_top;
1545 tmp->added_space_top = firstpar->added_space_top;
1546 tmp->bibkey = firstpar->bibkey;
1548 // layout stays the same with latex-environments
1550 firstpar->SetOnlyLayout(bparams, tmp->layout);
1551 firstpar->SetLabelWidthString(tmp->labelwidthstring);
1552 firstpar->depth = tmp->depth;
1556 // just an idea of me
1558 tmp->line_top = line_top;
1559 tmp->pagebreak_top = pagebreak_top;
1560 tmp->added_space_top = added_space_top;
1561 tmp->bibkey = bibkey;
1563 // layout stays the same with latex-environments
1565 SetOnlyLayout(bparams, tmp->layout);
1566 SetLabelWidthString(tmp->labelwidthstring);
1574 void LyXParagraph::MakeSameLayout(LyXParagraph const * par)
1577 par = par->FirstPhysicalPar();
1578 footnoteflag = par->footnoteflag;
1579 footnotekind = par->footnotekind;
1581 layout = par->layout;
1582 align = par-> align;
1583 SetLabelWidthString(par->labelwidthstring);
1585 line_bottom = par->line_bottom;
1586 pagebreak_bottom = par->pagebreak_bottom;
1587 added_space_bottom = par->added_space_bottom;
1589 line_top = par->line_top;
1590 pagebreak_top = par->pagebreak_top;
1591 added_space_top = par->added_space_top;
1593 spacing = par->spacing;
1595 pextra_type = par->pextra_type;
1596 pextra_width = par->pextra_width;
1597 pextra_widthp = par->pextra_widthp;
1598 pextra_alignment = par->pextra_alignment;
1599 pextra_hfill = par->pextra_hfill;
1600 pextra_start_minipage = par->pextra_start_minipage;
1602 noindent = par->noindent;
1608 LyXParagraph * LyXParagraph::FirstSelfrowPar()
1610 LyXParagraph * tmppar = this;
1613 && tmppar->previous->footnoteflag ==
1614 LyXParagraph::CLOSED_FOOTNOTE)
1615 || tmppar->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE))
1616 tmppar = tmppar->previous;
1619 return this; // This should never happen!
1626 int LyXParagraph::StripLeadingSpaces(LyXTextClassList::size_type tclass)
1628 if (textclasslist.Style(tclass, GetLayout()).free_spacing)
1636 && (IsNewline(0) || IsLineSeparator(0))){
1647 LyXParagraph * LyXParagraph::Clone() const
1649 // create a new paragraph
1650 LyXParagraph * result = new LyXParagraph;
1652 result->MakeSameLayout(this);
1654 // this is because of the dummy layout of the paragraphs that
1656 result->layout = layout;
1658 result->inset_owner = inset_owner;
1662 result->bibkey = static_cast<InsetBibKey *>
1663 (bibkey->Clone(*current_view->buffer()));
1667 // copy everything behind the break-position to the new paragraph
1669 result->text = text;
1670 result->fontlist = fontlist;
1671 result->insetlist = insetlist;
1672 for (InsetList::iterator it = result->insetlist.begin();
1673 it != result->insetlist.end(); ++it)
1674 (*it).inset = (*it).inset->Clone(*current_view->buffer());
1679 bool LyXParagraph::HasSameLayout(LyXParagraph const * par) const
1682 par = par->FirstPhysicalPar();
1687 par->footnoteflag == footnoteflag &&
1688 par->footnotekind == footnotekind &&
1690 par->layout == layout &&
1692 par->align == align &&
1694 par->line_bottom == line_bottom &&
1695 par->pagebreak_bottom == pagebreak_bottom &&
1696 par->added_space_bottom == added_space_bottom &&
1698 par->line_top == line_top &&
1699 par->pagebreak_top == pagebreak_top &&
1700 par->added_space_top == added_space_top &&
1702 par->spacing == spacing &&
1704 par->pextra_type == pextra_type &&
1705 par->pextra_width == pextra_width &&
1706 par->pextra_widthp == pextra_widthp &&
1707 par->pextra_alignment == pextra_alignment &&
1708 par->pextra_hfill == pextra_hfill &&
1709 par->pextra_start_minipage == pextra_start_minipage &&
1710 par->noindent == noindent &&
1711 par->depth == depth);
1715 void LyXParagraph::BreakParagraphConservative(BufferParams const & bparams,
1716 LyXParagraph::size_type pos)
1719 // create a new paragraph
1720 LyXParagraph * par = ParFromPos(pos);
1722 LyXParagraph * tmp = new LyXParagraph(par);
1724 tmp->MakeSameLayout(par);
1726 // When can pos < Last()?
1727 // I guess pos == Last() is possible.
1729 // copy everything behind the break-position to the new
1731 size_type pos_first = 0;
1732 while (ParFromPos(pos_first) != par)
1734 size_type pos_end = pos_first + par->text.size() - 1;
1737 for (i = j = pos; i <= pos_end; ++i) {
1738 par->CutIntoMinibuffer(bparams, i - pos_first);
1739 if (tmp->InsertFromMinibuffer(j - pos))
1745 for (size_type i = pos_end; i >= pos; --i)
1746 par->Erase(i - pos_first);
1751 // create a new paragraph
1752 LyXParagraph * tmp = new LyXParagraph(this);
1753 tmp->MakeSameLayout(this);
1755 // When can pos > Last()?
1756 // I guess pos == Last() is possible.
1758 // copy everything behind the break-position to the new
1760 size_type pos_end = text.size() - 1;
1763 for (i = j = pos; i <= pos_end; ++i) {
1764 CutIntoMinibuffer(bparams, i);
1765 if (tmp->InsertFromMinibuffer(j - pos))
1771 for (size_type i = pos_end; i >= pos; --i)
1780 // Be carefull, this does not make any check at all.
1781 // This method has wrong name, it combined this par with the next par.
1782 // In that sense it is the reverse of break paragraph. (Lgb)
1783 void LyXParagraph::PasteParagraph(BufferParams const & bparams)
1785 // copy the next paragraph to this one
1786 LyXParagraph * the_next = Next();
1788 LyXParagraph * firstpar = FirstPhysicalPar();
1791 // first the DTP-stuff
1793 firstpar->line_bottom = the_next->line_bottom;
1794 firstpar->added_space_bottom = the_next->added_space_bottom;
1795 firstpar->pagebreak_bottom = the_next->pagebreak_bottom;
1797 line_bottom = the_next->line_bottom;
1798 added_space_bottom = the_next->added_space_bottom;
1799 pagebreak_bottom = the_next->pagebreak_bottom;
1802 size_type pos_end = the_next->text.size() - 1;
1803 size_type pos_insert = Last();
1805 // ok, now copy the paragraph
1807 for (i = j = 0; i <= pos_end; ++i) {
1808 the_next->CutIntoMinibuffer(bparams, i);
1809 if (InsertFromMinibuffer(pos_insert + j))
1813 // delete the next paragraph
1814 LyXParagraph * ppar = the_next->previous;
1815 LyXParagraph * npar = the_next->next;
1822 void LyXParagraph::OpenFootnote(LyXParagraph::size_type pos)
1824 LyXParagraph * par = ParFromPos(pos);
1826 while (par && par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
1827 par->footnoteflag = LyXParagraph::OPEN_FOOTNOTE;
1833 void LyXParagraph::CloseFootnote(LyXParagraph::size_type pos)
1835 LyXParagraph * par = ParFromPos(pos);
1837 while (par && par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE) {
1838 par->footnoteflag = LyXParagraph::CLOSED_FOOTNOTE;
1845 int LyXParagraph::GetEndLabel(BufferParams const & bparams) const
1847 LyXParagraph const * par = this;
1848 int par_depth = GetDepth();
1850 LyXTextClass::LayoutList::size_type layout = par->GetLayout();
1851 int const endlabeltype =
1852 textclasslist.Style(bparams.textclass,
1853 layout).endlabeltype;
1854 if (endlabeltype != END_LABEL_NO_LABEL) {
1855 LyXParagraph const * last = this;
1857 if (footnoteflag == NO_FOOTNOTE)
1858 last = LastPhysicalPar();
1859 else if (next->footnoteflag == NO_FOOTNOTE)
1860 return endlabeltype;
1865 if (!last || !last->next)
1866 return endlabeltype;
1868 int next_depth = last->next->GetDepth();
1869 if (par_depth > next_depth ||
1870 (par_depth == next_depth && layout != last->next->GetLayout() ))
1871 return endlabeltype;
1876 par = par->DepthHook(par_depth - 1);
1878 par_depth = par->GetDepth();
1880 return END_LABEL_NO_LABEL;
1884 LyXTextClass::size_type LyXParagraph::GetLayout() const
1887 return FirstPhysicalPar()->layout;
1894 char LyXParagraph::GetDepth() const
1897 return FirstPhysicalPar()->depth;
1904 char LyXParagraph::GetAlign() const
1907 return FirstPhysicalPar()->align;
1914 string const & LyXParagraph::GetLabelstring() const
1917 return FirstPhysicalPar()->labelstring;
1924 int LyXParagraph::GetFirstCounter(int i) const
1927 return FirstPhysicalPar()->counter_[i];
1934 // the next two functions are for the manual labels
1935 string const LyXParagraph::GetLabelWidthString() const
1938 if (!FirstPhysicalPar()->labelwidthstring.empty())
1939 return FirstPhysicalPar()->labelwidthstring;
1941 if (!labelwidthstring.empty())
1942 return labelwidthstring;
1945 return _("Senseless with this layout!");
1949 void LyXParagraph::SetLabelWidthString(string const & s)
1952 LyXParagraph * par = FirstPhysicalPar();
1954 par->labelwidthstring = s;
1956 labelwidthstring = s;
1961 void LyXParagraph::SetOnlyLayout(BufferParams const & bparams,
1962 LyXTextClass::size_type new_layout)
1965 LyXParagraph * par = FirstPhysicalPar();
1967 LyXParagraph * par = this;
1969 LyXParagraph * ppar = 0;
1970 LyXParagraph * npar = 0;
1972 par->layout = new_layout;
1974 if (par->pextra_type == PEXTRA_NONE) {
1975 if (par->Previous()) {
1977 ppar = par->Previous()->FirstPhysicalPar();
1979 ppar = par->Previous();
1983 && (ppar->depth > par->depth))
1985 ppar = ppar->Previous()->FirstPhysicalPar();
1987 ppar = ppar->Previous();
1992 npar = par->Next()->NextAfterFootnote();
1998 && (npar->depth > par->depth))
2000 npar = npar->Next()->NextAfterFootnote();
2002 npar = npar->Next();
2005 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2007 p1 = ppar->pextra_width,
2008 p2 = ppar->pextra_widthp;
2009 ppar->SetPExtraType(bparams, ppar->pextra_type,
2012 if ((par->pextra_type == PEXTRA_NONE) &&
2013 npar && (npar->pextra_type != PEXTRA_NONE)) {
2014 string const p1 = npar->pextra_width;
2015 string const p2 = npar->pextra_widthp;
2016 npar->SetPExtraType(bparams, npar->pextra_type,
2023 void LyXParagraph::SetLayout(BufferParams const & bparams,
2024 LyXTextClass::size_type new_layout)
2028 * par = FirstPhysicalPar(),
2035 par->layout = new_layout;
2036 par->labelwidthstring.erase();
2037 par->align = LYX_ALIGN_LAYOUT;
2038 par->added_space_top = VSpace(VSpace::NONE);
2039 par->added_space_bottom = VSpace(VSpace::NONE);
2040 par->spacing.set(Spacing::Default);
2042 if (par->pextra_type == PEXTRA_NONE) {
2043 if (par->Previous()) {
2045 ppar = par->Previous()->FirstPhysicalPar();
2047 ppar = par->Previous();
2051 && (ppar->depth > par->depth))
2053 ppar = ppar->Previous()->FirstPhysicalPar();
2055 ppar = ppar->Previous();
2060 npar = par->Next()->NextAfterFootnote();
2066 && (npar->depth > par->depth))
2068 npar = npar->Next()->NextAfterFootnote();
2070 npar = npar->Next();
2073 if (ppar && (ppar->pextra_type != PEXTRA_NONE)) {
2074 string const p1 = ppar->pextra_width;
2075 string const p2 = ppar->pextra_widthp;
2076 ppar->SetPExtraType(bparams, ppar->pextra_type,
2079 if ((par->pextra_type == PEXTRA_NONE) &&
2080 npar && (npar->pextra_type != PEXTRA_NONE)) {
2081 string const p1 = npar->pextra_width;
2082 string const p2 = npar->pextra_widthp;
2083 npar->SetPExtraType(bparams, npar->pextra_type,
2090 // if the layout of a paragraph contains a manual label, the beginning of the
2091 // main body is the beginning of the second word. This is what the par-
2092 // function returns. If the layout does not contain a label, the main
2093 // body always starts with position 0. This differentiation is necessary,
2094 // because there cannot be a newline or a blank <= the beginning of the
2095 // main body in TeX.
2097 int LyXParagraph::BeginningOfMainBody() const
2100 if (FirstPhysicalPar() != this)
2103 // Unroll the first two cycles of the loop
2104 // and remember the previous character to
2105 // remove unnecessary GetChar() calls
2108 && GetChar(i) != LyXParagraph::META_NEWLINE
2111 char previous_char = 0;
2114 && (previous_char = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2115 // Yes, this ^ is supposed to be "= " not "=="
2118 && previous_char != ' '
2119 && (temp = GetChar(i)) != LyXParagraph::META_NEWLINE) {
2121 previous_char = temp;
2127 if (i == 0 && i == size() &&
2128 !(footnoteflag == LyXParagraph::NO_FOOTNOTE
2129 && next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE))
2130 ++i; /* the cursor should not jump
2131 * to the main body if there
2138 LyXParagraph * LyXParagraph::DepthHook(int deth)
2140 LyXParagraph * newpar = this;
2146 newpar = newpar->FirstPhysicalPar()->Previous();
2148 newpar = newpar->Previous();
2150 } while (newpar && newpar->GetDepth() > deth
2152 && newpar->footnoteflag == footnoteflag
2157 if (Previous() || GetDepth())
2158 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2163 return newpar->FirstPhysicalPar();
2170 LyXParagraph const * LyXParagraph::DepthHook(int deth) const
2172 LyXParagraph const * newpar = this;
2178 newpar = newpar->FirstPhysicalPar()->Previous();
2180 newpar = newpar->Previous();
2182 } while (newpar && newpar->GetDepth() > deth
2184 && newpar->footnoteflag == footnoteflag
2189 if (Previous() || GetDepth())
2190 lyxerr << "ERROR (LyXParagraph::DepthHook): "
2195 return newpar->FirstPhysicalPar();
2202 int LyXParagraph::AutoDeleteInsets()
2205 InsetList::size_type index = 0;
2206 while (index < insetlist.size()) {
2207 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
2208 Erase(insetlist[index].pos);
2209 // Erase() calls to insetlist.erase(&insetlist[index])
2210 // so index shouldn't be increased.
2219 LyXParagraph::inset_iterator
2220 LyXParagraph::InsetIterator(LyXParagraph::size_type pos)
2222 InsetTable search_inset(pos, 0);
2223 InsetList::iterator it = lower_bound(insetlist.begin(),
2225 search_inset, matchIT());
2226 return inset_iterator(it);
2230 // returns -1 if inset not found
2231 int LyXParagraph::GetPositionOfInset(Inset * inset) const
2234 for (InsetList::const_iterator cit = insetlist.begin();
2235 cit != insetlist.end(); ++cit) {
2236 if ((*cit).inset == inset) {
2240 if (inset == bibkey)
2244 // Think about footnotes.
2245 if (footnoteflag == LyXParagraph::NO_FOOTNOTE
2246 && next && next->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE) {
2248 NextAfterFootnote()->GetPositionOfInset(inset);
2250 return text.size() + 1 + further;
2257 LyXParagraph * LyXParagraph::TeXOnePar(Buffer const * buf,
2258 BufferParams const & bparams,
2259 ostream & os, TexRow & texrow,
2264 TexRow & foot_texrow,
2269 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
2270 LyXLayout const & style =
2271 textclasslist.Style(bparams.textclass,
2274 bool further_blank_line = false;
2277 lyxerr << "ERROR (LyXParagraph::TeXOnePar) is dummy." << endl;
2280 if (start_of_appendix) {
2281 os << "\\appendix\n";
2285 if (!spacing.isDefault()
2286 && (!Previous() || !Previous()->HasSameLayout(this))) {
2287 os << spacing.writeEnvirBegin() << "\n";
2291 if (tex_code_break_column && style.isCommand()){
2296 if (pagebreak_top) {
2298 further_blank_line = true;
2300 if (added_space_top.kind() != VSpace::NONE) {
2301 os << added_space_top.asLatexCommand(bparams);
2302 further_blank_line = true;
2306 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
2307 << "\\vspace{-1\\parskip}";
2308 further_blank_line = true;
2311 if (further_blank_line){
2316 Language const * language = getParLanguage(bparams);
2317 Language const * doc_language = bparams.language;
2318 Language const * previous_language = previous
2319 ? previous->getParLanguage(bparams) : doc_language;
2320 if (language->babel() != doc_language->babel() &&
2321 language->babel() != previous_language->babel()) {
2322 os << subst(lyxrc.language_command_begin, "$$lang",
2328 if (bparams.inputenc == "auto" &&
2329 language->encoding() != previous_language->encoding()) {
2330 os << "\\inputencoding{"
2331 << language->encoding()->LatexName()
2336 switch (style.latextype) {
2339 << style.latexname()
2340 << style.latexparam();
2342 case LATEX_ITEM_ENVIRONMENT:
2344 bibkey->Latex(buf, os, false, false);
2348 case LATEX_LIST_ENVIRONMENT:
2355 bool need_par = SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2357 LyXParagraph * par = next;
2359 // Spit out footnotes
2360 if (lyxrc.rtl_support) {
2361 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2362 && next->footnoteflag != footnoteflag) {
2363 LyXParagraph * p = 0;
2364 bool is_rtl = (size() > 0)
2365 ? GetFontSettings(bparams,
2366 size()-1).isRightToLeft()
2367 : language->RightToLeft();
2368 if ((p = NextAfterFootnote()) != 0 &&
2370 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2371 is_rtl = getParLanguage(bparams)->RightToLeft();
2372 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2373 && par->footnoteflag != footnoteflag) {
2374 par = par->TeXFootnote(buf, bparams,
2376 foot_texrow, foot_count,
2378 par->SimpleTeXOnePar(buf, bparams,
2379 os, texrow, moving_arg);
2380 is_rtl = (par->size() > 0)
2381 ? par->GetFontSettings(bparams,
2382 par->size()-1).isRightToLeft()
2383 : language->RightToLeft();
2385 par->next->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2386 (p = par->NextAfterFootnote()) != 0 &&
2388 p->GetFontSettings(bparams, 0).isRightToLeft() != is_rtl)
2389 is_rtl = language->RightToLeft();
2394 while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE
2395 && par->footnoteflag != footnoteflag) {
2396 par = par->TeXFootnote(buf, bparams,
2398 foot, foot_texrow, foot_count,
2400 par->SimpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
2406 // Make sure that \\par is done with the font of the last
2407 // character if this has another size as the default.
2408 // This is necessary because LaTeX (and LyX on the screen)
2409 // calculates the space between the baselines according
2410 // to this font. (Matthias)
2412 // Is this really needed ? (Dekel)
2413 // We do not need to use to change the font for the last paragraph
2414 // or for a command.
2415 LyXFont font = getFont(bparams, Last() - 1);
2416 bool is_command = textclasslist.Style(bparams.textclass,
2417 GetLayout()).isCommand();
2418 if (style.resfont.size() != font.size() && next && !is_command) {
2421 os << "\\" << font.latexSize() << " \\par}";
2422 } else if (need_par) {
2424 } else if (is_command)
2427 if (language->babel() != doc_language->babel() &&
2430 || (footnoteflag != NO_FOOTNOTE && par->footnoteflag != footnoteflag)
2432 || par->getParLanguage(bparams)->babel() != language->babel())) {
2434 << subst(lyxrc.language_command_end, "$$lang",
2435 doc_language->babel());
2438 switch (style.latextype) {
2439 case LATEX_ITEM_ENVIRONMENT:
2440 case LATEX_LIST_ENVIRONMENT:
2441 if (par && (depth < par->depth)) {
2446 case LATEX_ENVIRONMENT:
2447 // if its the last paragraph of the current environment
2448 // skip it otherwise fall through
2450 && (par->layout != layout
2451 || par->depth != depth
2452 || par->pextra_type != pextra_type))
2455 // we don't need it for the last paragraph!!!
2456 // or for tables in floats
2457 // -- effectively creates a \par where there isn't one which
2458 // breaks a \subfigure or \subtable.
2460 // && footnoteflag == LyXParagraph::NO_FOOTNOTE) {
2466 further_blank_line = false;
2468 os << "\\lyxline{\\" << getFont(bparams, Last() - 1).latexSize() << '}';
2469 further_blank_line = true;
2472 if (added_space_bottom.kind() != VSpace::NONE) {
2473 os << added_space_bottom.asLatexCommand(bparams);
2474 further_blank_line = true;
2477 if (pagebreak_bottom) {
2479 further_blank_line = true;
2482 if (further_blank_line){
2487 if (!spacing.isDefault()
2488 && (!par || !par->HasSameLayout(this))) {
2489 os << spacing.writeEnvirEnd() << "\n";
2493 // we don't need it for the last paragraph!!!
2496 && !(footnoteflag != LyXParagraph::NO_FOOTNOTE && par &&
2497 par->footnoteflag == LyXParagraph::NO_FOOTNOTE)
2504 lyxerr[Debug::LATEX] << "TeXOnePar...done " << par << endl;
2509 // This one spits out the text of the paragraph
2510 bool LyXParagraph::SimpleTeXOnePar(Buffer const * buf,
2511 BufferParams const & bparams,
2512 ostream & os, TexRow & texrow,
2515 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
2517 bool return_value = false;
2519 LyXLayout const & style =
2520 textclasslist.Style(bparams.textclass,
2522 LyXFont basefont, last_font;
2524 // Maybe we have to create a optional argument.
2525 size_type main_body;
2526 if (style.labeltype != LABEL_MANUAL)
2529 main_body = BeginningOfMainBody();
2531 if (main_body > 0) {
2533 basefont = getFont(bparams, -2); // Get label font
2535 basefont = getFont(bparams, -1); // Get layout font
2546 if (style.isCommand()) {
2549 } else if (align != LYX_ALIGN_LAYOUT) {
2552 return_value = true;
2556 moving_arg |= style.needprotect;
2558 // Which font is currently active?
2559 LyXFont running_font(basefont);
2560 // Do we have an open font change?
2561 bool open_font = false;
2563 texrow.start(this, 0);
2565 for (size_type i = 0; i < size(); ++i) {
2567 // First char in paragraph or after label?
2573 if (main_body > 0) {
2575 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2578 basefont = getFont(bparams, -1); // Now use the layout font
2579 running_font = basefont;
2583 if (style.isCommand()) {
2586 } else if (align != LYX_ALIGN_LAYOUT && next) {
2587 // We do not need \par here (Dekel)
2591 return_value = true;
2595 os << "\\noindent ";
2599 case LYX_ALIGN_NONE:
2600 case LYX_ALIGN_BLOCK:
2601 case LYX_ALIGN_LAYOUT:
2602 case LYX_ALIGN_SPECIAL:
2604 case LYX_ALIGN_LEFT:
2607 if (getParLanguage(bparams)->babel() != "hebrew") {
2608 os << "\\raggedright ";
2611 os << "\\raggedleft ";
2615 case LYX_ALIGN_RIGHT:
2618 if (getParLanguage(bparams)->babel() != "hebrew") {
2619 os << "\\raggedleft ";
2622 os << "\\raggedright ";
2626 case LYX_ALIGN_CENTER:
2629 os << "\\centering ";
2635 value_type c = GetChar(i);
2637 // Fully instantiated font
2638 LyXFont font = getFont(bparams, i);
2640 LyXParagraph * p = 0;
2643 previous->footnoteflag != LyXParagraph::NO_FOOTNOTE &&
2644 (p = PreviousBeforeFootnote()) != 0)
2645 last_font = p->getFont(bparams, p->size() - 1);
2648 last_font = running_font;
2650 // Spaces at end of font change are simulated to be
2651 // outside font change, i.e. we write "\textXX{text} "
2652 // rather than "\textXX{text }". (Asger)
2653 if (open_font && c == ' ' && i <= size() - 2
2654 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
2655 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
2656 font = getFont(bparams, i + 1);
2658 // We end font definition before blanks
2659 if (!font.equalExceptLatex(running_font) && open_font) {
2660 column += running_font.latexWriteEndChanges(os,
2662 (i == main_body-1) ? basefont : font);
2663 running_font = basefont;
2667 // Blanks are printed before start of fontswitch
2669 // Do not print the separation of the optional argument
2670 if (i != main_body - 1) {
2671 SimpleTeXBlanks(os, texrow, i,
2672 column, font, style);
2676 // Do we need to change font?
2677 if (!font.equalExceptLatex(running_font)
2678 && i != main_body-1) {
2679 column += font.latexWriteStartChanges(os, basefont,
2681 running_font = font;
2685 if (c == LyXParagraph::META_NEWLINE) {
2686 // newlines are handled differently here than
2687 // the default in SimpleTeXSpecialChars().
2688 if (!style.newline_allowed
2689 || font.latex() == LyXFont::ON) {
2693 column += running_font.latexWriteEndChanges(os, basefont, basefont);
2696 basefont = getFont(bparams, -1);
2697 running_font = basefont;
2698 if (font.family() ==
2699 LyXFont::TYPEWRITER_FAMILY) {
2707 texrow.start(this, i + 1);
2710 SimpleTeXSpecialChars(buf, bparams,
2711 os, texrow, moving_arg,
2712 font, running_font, basefont,
2713 open_font, style, i, column, c);
2717 // If we have an open font definition, we have to close it
2719 LyXParagraph * p = 0;
2722 && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
2723 && (p = NextAfterFootnote()) != 0
2728 running_font.latexWriteEndChanges(os, basefont,
2729 p->getFont(bparams, 0));
2731 running_font.latexWriteEndChanges(os, basefont, basefont);
2734 // Needed if there is an optional argument but no contents.
2735 if (main_body > 0 && main_body == size()) {
2737 return_value = false;
2740 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
2741 return return_value;
2745 bool LyXParagraph::linuxDocConvertChar(char c, string & sgml_string)
2747 bool retval = false;
2749 case LyXParagraph::META_HFILL:
2750 sgml_string.erase();
2752 case LyXParagraph::META_NEWLINE:
2756 sgml_string = "&";
2759 sgml_string = "<";
2762 sgml_string = ">";
2765 sgml_string = "$";
2768 sgml_string = "#";
2771 sgml_string = "%";
2774 sgml_string = "[";
2777 sgml_string = "]";
2780 sgml_string = "{";
2783 sgml_string = "}";
2786 sgml_string = "˜";
2789 sgml_string = """;
2792 sgml_string = "\";
2798 case '\0': // Ignore :-)
2799 sgml_string.erase();
2809 void LyXParagraph::SimpleTeXBlanks(ostream & os, TexRow & texrow,
2810 LyXParagraph::size_type const i,
2811 int & column, LyXFont const & font,
2812 LyXLayout const & style)
2814 if (column > tex_code_break_column
2816 && GetChar(i - 1) != ' '
2818 // In LaTeX mode, we don't want to
2819 // break lines since some commands
2821 && ! (font.latex() == LyXFont::ON)
2822 // same in FreeSpacing mode
2823 && !style.free_spacing
2824 // In typewriter mode, we want to avoid
2825 // ! . ? : at the end of a line
2826 && !(font.family() == LyXFont::TYPEWRITER_FAMILY
2827 && (GetChar(i-1) == '.'
2828 || GetChar(i-1) == '?'
2829 || GetChar(i-1) == ':'
2830 || GetChar(i-1) == '!'))) {
2831 if (tex_code_break_column == 0) {
2832 // in batchmode we need LaTeX to still
2833 // see it as a space not as an extra '\n'
2839 texrow.start(this, i + 1);
2841 } else if (font.latex() == LyXFont::OFF) {
2842 if (style.free_spacing) {
2851 void LyXParagraph::SimpleTeXSpecialChars(Buffer const * buf,
2852 BufferParams const & bparams,
2853 ostream & os, TexRow & texrow,
2856 LyXFont & running_font,
2859 LyXLayout const & style,
2860 LyXParagraph::size_type & i,
2862 LyXParagraph::value_type const c)
2864 // Two major modes: LaTeX or plain
2865 // Handle here those cases common to both modes
2866 // and then split to handle the two modes separately.
2868 case LyXParagraph::META_INSET: {
2869 Inset * inset = GetInset(i);
2872 int const len = os.tellp();
2873 if ((inset->LyxCode() == Inset::GRAPHICS_CODE
2874 || inset->LyxCode() == Inset::MATH_CODE
2875 || inset->LyxCode() == Inset::URL_CODE)
2876 && running_font.isRightToLeft()) {
2881 int tmp = inset->Latex(buf, os, moving_arg,
2882 style.free_spacing);
2890 column += os.tellp() - len;
2899 case LyXParagraph::META_NEWLINE:
2901 column += running_font.latexWriteEndChanges(os,
2906 basefont = getFont(bparams, -1);
2907 running_font = basefont;
2910 case LyXParagraph::META_HFILL:
2916 // And now for the special cases within each mode
2917 // Are we in LaTeX mode?
2918 if (font.latex() == LyXFont::ON) {
2919 // at present we only have one option
2920 // but I'll leave it as a switch statement
2921 // so its simpler to extend. (ARRae)
2924 // make sure that we will not print
2925 // error generating chars to the tex
2926 // file. This test would not be needed
2927 // if it were done in the buffer
2935 // Plain mode (i.e. not LaTeX)
2938 os << "\\textbackslash{}";
2942 case '°': case '±': case '²': case '³':
2943 case '×': case '÷': case '¹': case 'ª':
2944 case 'º': case '¬': case 'µ':
2945 if (bparams.inputenc == "latin1" ||
2946 (bparams.inputenc == "auto" &&
2947 font.language()->encoding()->LatexName()
2949 os << "\\ensuremath{"
2958 case '|': case '<': case '>':
2959 // In T1 encoding, these characters exist
2960 if (lyxrc.fontenc == "T1") {
2962 //... but we should avoid ligatures
2963 if ((c == '>' || c == '<')
2965 && GetChar(i + 1) == c) {
2966 //os << "\\textcompwordmark{}";
2967 // Jean-Marc, have a look at
2968 // this. IÂ think this works
2976 // Typewriter font also has them
2977 if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
2981 // Otherwise, we use what LaTeX
2985 os << "\\textless{}";
2989 os << "\\textgreater{}";
2993 os << "\\textbar{}";
2999 case '-': // "--" in Typewriter mode -> "-{}-"
3001 && GetChar(i + 1) == '-'
3002 && font.family() == LyXFont::TYPEWRITER_FAMILY) {
3011 os << "\\char`\\\"{}";
3016 if (bparams.inputenc == "default") {
3025 case '%': case '#': case '{':
3032 os << "\\textasciitilde{}";
3037 os << "\\textasciicircum{}";
3041 case '*': case '[': case ']':
3042 // avoid being mistaken for optional arguments
3043 os << '{' << c << '}';
3048 // Blanks are printed before font switching.
3049 // Sure? I am not! (try nice-latex)
3050 // I am sure it's correct. LyX might be smarter
3051 // in the future, but for now, nothing wrong is
3056 /* idea for labels --- begin*/
3060 && font.family() != LyXFont::TYPEWRITER_FAMILY
3061 && GetChar(i + 1) == 'y'
3062 && GetChar(i + 2) == 'X') {
3070 && font.family() != LyXFont::TYPEWRITER_FAMILY
3071 && GetChar(i + 1) == 'e'
3072 && GetChar(i + 2) == 'X') {
3077 // Check for "LaTeX2e"
3080 && font.family() != LyXFont::TYPEWRITER_FAMILY
3081 && GetChar(i + 1) == 'a'
3082 && GetChar(i + 2) == 'T'
3083 && GetChar(i + 3) == 'e'
3084 && GetChar(i + 4) == 'X'
3085 && GetChar(i + 5) == '2'
3086 && GetChar(i + 6) == 'e') {
3091 // Check for "LaTeX"
3094 && font.family() != LyXFont::TYPEWRITER_FAMILY
3095 && GetChar(i + 1) == 'a'
3096 && GetChar(i + 2) == 'T'
3097 && GetChar(i + 3) == 'e'
3098 && GetChar(i + 4) == 'X') {
3102 /* idea for labels --- end*/
3103 } else if (c != '\0') {
3113 LyXParagraph * LyXParagraph::TeXDeeper(Buffer const * buf,
3114 BufferParams const & bparams,
3115 ostream & os, TexRow & texrow
3118 TexRow & foot_texrow,
3123 lyxerr[Debug::LATEX] << "TeXDeeper... " << this << endl;
3124 LyXParagraph * par = this;
3127 (par->depth == depth)
3129 && (par->footnoteflag == footnoteflag)
3134 lyxerr << "ERROR (LyXParagraph::TeXDeeper)" << endl;
3136 if (textclasslist.Style(bparams.textclass,
3137 par->layout).isEnvironment()
3138 || par->pextra_type != PEXTRA_NONE) {
3139 par = par->TeXEnvironment(buf, bparams,
3147 par = par->TeXOnePar(buf, bparams,
3157 lyxerr[Debug::LATEX] << "TeXDeeper...done " << par << endl;
3163 LyXParagraph * LyXParagraph::TeXEnvironment(Buffer const * buf,
3164 BufferParams const & bparams,
3165 ostream & os, TexRow & texrow
3168 TexRow & foot_texrow,
3173 bool eindent_open = false;
3175 bool foot_this_level = false;
3177 // flags when footnotetext should be appended to file.
3178 static bool minipage_open = false;
3179 static int minipage_open_depth = 0;
3180 char par_sep = bparams.paragraph_separation;
3182 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
3185 lyxerr << "ERROR (LyXParagraph::TeXEnvironment)" << endl;
3188 LyXLayout const & style =
3189 textclasslist.Style(bparams.textclass,
3192 if (pextra_type == PEXTRA_INDENT) {
3193 if (!pextra_width.empty()) {
3194 os << "\\begin{LyXParagraphIndent}{"
3195 << pextra_width << "}\n";
3197 //float ib = atof(pextra_widthp.c_str())/100;
3198 // string can't handle floats at present (971109)
3199 // so I'll do a conversion by hand knowing that
3200 // the limits are 0.0 to 1.0. ARRae.
3201 os << "\\begin{LyXParagraphIndent}{";
3202 switch (pextra_widthp.length()) {
3214 os << "\\columnwidth}\n";
3217 eindent_open = true;
3219 if ((pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3220 if (pextra_hfill && Previous() &&
3221 (Previous()->pextra_type == PEXTRA_MINIPAGE)) {
3222 os << "\\hfill{}\n";
3225 if (par_sep == BufferParams::PARSEP_INDENT) {
3226 os << "{\\setlength\\parindent{0pt}\n";
3229 os << "\\begin{minipage}";
3230 switch (pextra_alignment) {
3231 case MINIPAGE_ALIGN_TOP:
3234 case MINIPAGE_ALIGN_MIDDLE:
3237 case MINIPAGE_ALIGN_BOTTOM:
3241 if (!pextra_width.empty()) {
3242 os << '{' << pextra_width << "}\n";
3244 //float ib = atof(par->pextra_width.c_str())/100;
3245 // string can't handle floats at present
3246 // so I'll do a conversion by hand knowing that
3247 // the limits are 0.0 to 1.0. ARRae.
3249 switch (pextra_widthp.length()) {
3261 os << "\\columnwidth}\n";
3264 if (par_sep == BufferParams::PARSEP_INDENT) {
3265 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3268 minipage_open = true;
3269 minipage_open_depth = depth;
3272 #ifdef WITH_WARNINGS
3273 #warning Define FANCY_FOOTNOTE_CODE to re-enable Allan footnote code
3274 //I disabled it because it breaks when lists span on several
3277 if (style.isEnvironment()){
3278 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
3279 #ifdef FANCY_FOOTNOTE_CODE
3280 if (foot_count < 0) {
3281 // flag that footnote[mark][text] should be
3282 // used for any footnotes from now on
3284 foot_this_level = true;
3287 os << "\\begin{" << style.latexname() << "}{"
3288 << labelwidthstring << "}\n";
3289 } else if (style.labeltype == LABEL_BIBLIO) {
3291 os << "\\begin{" << style.latexname() << "}{"
3292 << bibitemWidest(buf)
3294 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
3295 #ifdef FANCY_FOOTNOTE_CODE
3296 if (foot_count < 0) {
3297 // flag that footnote[mark][text] should be
3298 // used for any footnotes from now on
3300 foot_this_level = true;
3303 os << "\\begin{" << style.latexname() << '}'
3304 << style.latexparam() << '\n';
3306 os << "\\begin{" << style.latexname() << '}'
3307 << style.latexparam() << '\n';
3310 LyXParagraph * par = this;
3312 par = par->TeXOnePar(buf, bparams,
3316 foot, foot_texrow, foot_count
3320 if (minipage_open && par && !style.isEnvironment() &&
3321 (par->pextra_type == PEXTRA_MINIPAGE) &&
3322 par->pextra_start_minipage) {
3323 os << "\\end{minipage}\n";
3325 if (par_sep == BufferParams::PARSEP_INDENT) {
3329 minipage_open = false;
3331 if (par && par->depth > depth) {
3332 if (textclasslist.Style(bparams.textclass,
3333 par->layout).isParagraph()
3335 // How to handle this? (Lgb)
3336 //&& !suffixIs(os, "\n\n")
3338 // There should be at least one '\n' already
3339 // but we need there to be two for Standard
3340 // paragraphs that are depth-increment'ed to be
3341 // output correctly. However, tables can
3342 // also be paragraphs so don't adjust them.
3345 // Will it ever harm to have one '\n' too
3346 // many? i.e. that we sometimes will have
3347 // three in a row. (Lgb)
3351 par = par->TeXDeeper(buf, bparams, os, texrow
3353 ,foot, foot_texrow, foot_count
3357 if (par && par->layout == layout && par->depth == depth &&
3358 (par->pextra_type == PEXTRA_MINIPAGE) && !minipage_open) {
3359 if (par->pextra_hfill && par->Previous() &&
3360 (par->Previous()->pextra_type == PEXTRA_MINIPAGE)){
3361 os << "\\hfill{}\n";
3364 if (par_sep == BufferParams::PARSEP_INDENT) {
3365 os << "{\\setlength\\parindent{0pt}\n";
3368 os << "\\begin{minipage}";
3369 switch (par->pextra_alignment) {
3370 case MINIPAGE_ALIGN_TOP:
3373 case MINIPAGE_ALIGN_MIDDLE:
3376 case MINIPAGE_ALIGN_BOTTOM:
3380 if (!par->pextra_width.empty()) {
3381 os << '{' << par->pextra_width << "}\n";
3383 //float ib = atof(par->pextra_widthp.c_str())/100;
3384 // string can't handle floats at present
3385 // so I'll do a conversion by hand knowing that
3386 // the limits are 0.0 to 1.0. ARRae.
3388 switch (par->pextra_widthp.length()) {
3393 os << "0." << par->pextra_widthp;
3396 os << "0.0" << par->pextra_widthp;
3398 os << "\\columnwidth}\n";
3401 if (par_sep == BufferParams::PARSEP_INDENT) {
3402 os << "\\setlength\\parindent{\\LyXMinipageIndent}\n";
3405 minipage_open = true;
3406 minipage_open_depth = par->depth;
3409 && par->layout == layout
3410 && par->depth == depth
3411 && par->pextra_type == pextra_type
3413 && par->footnoteflag == footnoteflag
3417 if (style.isEnvironment()) {
3418 os << "\\end{" << style.latexname() << '}';
3420 // maybe this should go after the minipage closes?
3421 if (foot_this_level) {
3422 if (foot_count >= 1) {
3423 if (foot_count > 1) {
3424 os << "\\addtocounter{footnote}{-"
3429 texrow += foot_texrow;
3431 foot_texrow.reset();
3437 if (minipage_open && (minipage_open_depth == depth) &&
3438 (!par || par->pextra_start_minipage ||
3439 par->pextra_type != PEXTRA_MINIPAGE)) {
3440 os << "\\end{minipage}\n";
3442 if (par_sep == BufferParams::PARSEP_INDENT) {
3446 if (par && par->pextra_type != PEXTRA_MINIPAGE) {
3447 os << "\\medskip\n\n";
3451 minipage_open = false;
3454 os << "\\end{LyXParagraphIndent}\n";
3457 if (!(par && (par->pextra_type == PEXTRA_MINIPAGE)
3458 && par->pextra_hfill)) {
3462 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
3463 return par; // ale970302
3468 LyXParagraph * LyXParagraph::TeXFootnote(Buffer const * buf,
3469 BufferParams const & bparams,
3470 ostream & os, TexRow & texrow,
3471 ostream & foot, TexRow & foot_texrow,
3475 lyxerr[Debug::LATEX] << "TeXFootnote... " << this << endl;
3476 if (footnoteflag == LyXParagraph::NO_FOOTNOTE)
3477 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3478 "No footnote!" << endl;
3480 LyXParagraph * par = this;
3481 LyXLayout const & style =
3482 textclasslist.Style(bparams.textclass,
3483 previous->GetLayout());
3485 if (style.needprotect && footnotekind != LyXParagraph::FOOTNOTE){
3486 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3487 "Float other than footnote in command"
3488 " with moving argument is illegal" << endl;
3491 if (footnotekind != LyXParagraph::FOOTNOTE
3492 && footnotekind != LyXParagraph::MARGIN
3495 // How to solve this?
3496 //&& !suffixIs(file, '\n')
3498 // we need to ensure that real floats like tables and figures
3499 // have their \begin{} on a new line otherwise we can get
3500 // incorrect results when using the endfloat.sty package
3501 // especially if two floats follow one another. ARRae 981022
3502 // NOTE: if the file is length 0 it must have just been
3503 // written out so we assume it ended with a '\n'
3505 // As far as I can see there is never any harm in writing
3506 // a '\n' too much. Please tell me if I am wrong. (Lgb)
3511 bool moving_arg = false;
3512 bool need_closing = false;
3513 bool is_rtl = isRightToLeftPar(bparams);
3515 if (is_rtl != parent_is_rtl) {
3520 need_closing = true;
3523 bool footer_in_body = true;
3524 switch (footnotekind) {
3525 case LyXParagraph::FOOTNOTE:
3526 if (style.intitle) {
3527 os << "\\thanks{\n";
3528 footer_in_body = false;
3531 if (foot_count == -1) {
3532 // we're at depth 0 so we can use:
3533 os << "\\footnote{%\n";
3534 footer_in_body = false;
3536 os << "\\footnotemark{}%\n";
3538 // we only need this when there are
3539 // multiple footnotes
3540 os << "\\stepcounter{footnote}";
3542 os << "\\footnotetext{%\n";
3543 foot_texrow.start(this, 0);
3544 foot_texrow.newline();
3549 case LyXParagraph::MARGIN:
3550 os << "\\marginpar{\n";
3552 case LyXParagraph::FIG:
3553 if (pextra_type == PEXTRA_FLOATFLT
3554 && (!pextra_width.empty()
3555 || !pextra_widthp.empty())) {
3556 if (!pextra_width.empty())
3557 os << "\\begin{floatingfigure}{"
3558 << pextra_width << "}\n";
3560 os << "\\begin{floatingfigure}{"
3561 << lyx::atoi(pextra_widthp) / 100.0
3562 << "\\textwidth}\n";
3564 os << "\\begin{figure}";
3565 if (!bparams.float_placement.empty()) {
3566 os << '[' << bparams.float_placement << "]\n";
3572 case LyXParagraph::TAB:
3573 os << "\\begin{table}";
3574 if (!bparams.float_placement.empty()) {
3575 os << '[' << bparams.float_placement << "]\n";
3580 case LyXParagraph::WIDE_FIG:
3581 os << "\\begin{figure*}";
3582 if (!bparams.float_placement.empty()) {
3583 os << '[' << bparams.float_placement << "]\n";
3588 case LyXParagraph::WIDE_TAB:
3589 os << "\\begin{table*}";
3590 if (!bparams.float_placement.empty()) {
3591 os << '[' << bparams.float_placement << "]\n";
3596 case LyXParagraph::ALGORITHM:
3597 os << "\\begin{algorithm}\n";
3602 if (footnotekind != LyXParagraph::FOOTNOTE
3603 || !footer_in_body) {
3604 // Process text for all floats except footnotes in body
3606 LyXLayout const & style =
3608 .Style(bparams.textclass, par->layout);
3610 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3612 if (style.isEnvironment()
3613 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3614 // Allows the use of minipages within float
3615 // environments. Shouldn't be circular because
3616 // we don't support footnotes inside
3617 // floats (yet). ARRae
3618 par = par->TeXEnvironment(buf, bparams, os,
3623 par = par->TeXOnePar(buf, bparams,
3624 os, texrow, moving_arg,
3629 if (par && !par->IsDummy() && par->depth > depth) {
3630 par = par->TeXDeeper(buf, bparams, os, texrow,
3634 } while (par && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3636 // process footnotes > depth 0 or in environments separately
3637 // NOTE: Currently don't support footnotes within footnotes
3638 // even though that is possible using the \footnotemark
3639 std::ostringstream dummy;
3640 TexRow dummy_texrow;
3641 int dummy_count = 0;
3643 LyXLayout const & style =
3645 .Style(bparams.textclass, par->layout);
3647 lyxerr << "ERROR (LyXParagraph::TeXFootnote)"
3649 if (style.isEnvironment()
3650 || par->pextra_type == PEXTRA_MINIPAGE) { /* && !minipage_open ?? */
3651 // Allows the use of minipages within float
3652 // environments. Shouldn't be circular because
3653 // we don't support footnotes inside
3654 // floats (yet). ARRae
3655 par = par->TeXEnvironment(buf, bparams,
3657 dummy, dummy_texrow,
3660 par = par->TeXOnePar(buf, bparams,
3663 dummy, dummy_texrow,
3667 if (par && !par->IsDummy() && par->depth > depth) {
3668 par = par->TeXDeeper(buf, bparams,
3670 dummy, dummy_texrow,
3674 && par->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3676 lyxerr << "ERROR (LyXParagraph::TeXFootnote): "
3677 "Footnote in a Footnote -- not supported"
3682 switch (footnotekind) {
3683 case LyXParagraph::FOOTNOTE:
3684 if (footer_in_body) {
3685 // This helps tell which of the multiple
3686 // footnotetexts an error was in.
3688 foot_texrow.newline();
3693 case LyXParagraph::MARGIN:
3696 case LyXParagraph::FIG:
3697 if (pextra_type == PEXTRA_FLOATFLT
3698 && (!pextra_width.empty()
3699 || !pextra_widthp.empty()))
3700 os << "\\end{floatingfigure}";
3702 os << "\\end{figure}";
3704 case LyXParagraph::TAB:
3705 os << "\\end{table}";
3707 case LyXParagraph::WIDE_FIG:
3708 os << "\\end{figure*}";
3710 case LyXParagraph::WIDE_TAB:
3711 os << "\\end{table*}";
3713 case LyXParagraph::ALGORITHM:
3714 os << "\\end{algorithm}";
3721 if (footnotekind != LyXParagraph::FOOTNOTE
3722 && footnotekind != LyXParagraph::MARGIN) {
3723 // we need to ensure that real floats like tables and figures
3724 // have their \end{} on a line of their own otherwise we can
3725 // get incorrect results when using the endfloat.sty package.
3730 lyxerr[Debug::LATEX] << "TeXFootnote...done " << par->next << endl;
3735 bool LyXParagraph::IsDummy() const
3737 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
3738 && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
3742 void LyXParagraph::SetPExtraType(BufferParams const & bparams,
3743 int type, string const & width,
3744 string const & widthp)
3747 pextra_width = width;
3748 pextra_widthp = widthp;
3750 if (textclasslist.Style(bparams.textclass,
3751 layout).isEnvironment()) {
3752 LyXParagraph * par = this;
3753 LyXParagraph * ppar = par;
3755 while (par && (par->layout == layout)
3756 && (par->depth == depth)) {
3758 par = par->Previous();
3761 par = par->FirstPhysicalPar();
3763 while (par && par->depth > depth) {
3764 par = par->Previous();
3767 par = par->FirstPhysicalPar();
3772 while (par && (par->layout == layout)
3773 && (par->depth == depth)) {
3774 par->pextra_type = type;
3775 par->pextra_width = width;
3776 par->pextra_widthp = widthp;
3778 par = par->NextAfterFootnote();
3782 if (par && (par->depth > depth))
3783 par->SetPExtraType(bparams,
3784 type, width, widthp);
3786 while (par && ((par->depth > depth) || par->IsDummy()))
3787 par = par->NextAfterFootnote();
3789 while (par && ((par->depth > depth)))
3797 void LyXParagraph::UnsetPExtraType(BufferParams const & bparams)
3799 if (pextra_type == PEXTRA_NONE)
3802 pextra_type = PEXTRA_NONE;
3803 pextra_width.erase();
3804 pextra_widthp.erase();
3806 if (textclasslist.Style(bparams.textclass,
3807 layout).isEnvironment()) {
3808 LyXParagraph * par = this;
3809 LyXParagraph * ppar = par;
3811 while (par && (par->layout == layout)
3812 && (par->depth == depth)) {
3814 par = par->Previous();
3817 par = par->FirstPhysicalPar();
3819 while (par && par->depth > depth) {
3820 par = par->Previous();
3823 par = par->FirstPhysicalPar();
3828 while (par && (par->layout == layout)
3829 && (par->depth == depth)) {
3830 par->pextra_type = PEXTRA_NONE;
3831 par->pextra_width.erase();
3832 par->pextra_widthp.erase();
3834 par = par->NextAfterFootnote();
3838 if (par && (par->depth > depth))
3839 par->UnsetPExtraType(bparams);
3841 while (par && ((par->depth > depth) || par->IsDummy()))
3842 par = par->NextAfterFootnote();
3844 while (par && ((par->depth > depth)))
3852 bool LyXParagraph::IsHfill(size_type pos) const
3854 return IsHfillChar(GetChar(pos));
3858 bool LyXParagraph::IsInset(size_type pos) const
3860 return IsInsetChar(GetChar(pos));
3865 bool LyXParagraph::IsFloat(size_type pos) const
3867 return IsFloatChar(GetChar(pos));
3872 bool LyXParagraph::IsNewline(size_type pos) const
3874 return pos >= 0 && IsNewlineChar(GetChar(pos));
3878 bool LyXParagraph::IsSeparator(size_type pos) const
3880 return IsSeparatorChar(GetChar(pos));
3884 bool LyXParagraph::IsLineSeparator(size_type pos) const
3886 return IsLineSeparatorChar(GetChar(pos));
3890 bool LyXParagraph::IsKomma(size_type pos) const
3892 return IsKommaChar(GetChar(pos));
3896 /// Used by the spellchecker
3897 bool LyXParagraph::IsLetter(LyXParagraph::size_type pos) const
3899 value_type const c = GetChar(pos);
3900 if (IsLetterChar(c))
3902 // '\0' is not a letter, allthough every string contains "" (below)
3905 // We want to pass the ' and escape chars to ispell
3906 string const extra = lyxrc.isp_esc_chars + '\'';
3907 char ch[2] = { c, 0 };
3908 return contains(extra, ch);
3912 bool LyXParagraph::IsWord(size_type pos ) const
3914 return IsWordChar(GetChar(pos)) ;
3919 LyXParagraph::getParLanguage(BufferParams const & bparams) const
3923 return FirstPhysicalPar()->getParLanguage(bparams);
3927 #ifdef DO_USE_DEFAULT_LANGUAGE
3928 Language const * lang = GetFirstFontSettings().language();
3930 if (lang->lang() == "default")
3931 return bparams.language;
3934 return GetFirstFontSettings().language();
3936 } else if (previous)
3937 return previous->getParLanguage(bparams);
3939 return bparams.language;
3943 bool LyXParagraph::isRightToLeftPar(BufferParams const & bparams) const
3945 return lyxrc.rtl_support
3946 && getParLanguage(bparams)->RightToLeft();
3950 void LyXParagraph::ChangeLanguage(BufferParams const & bparams,
3951 Language const * from, Language const * to)
3953 for (size_type i = 0; i < size(); ++i) {
3954 LyXFont font = GetFontSettings(bparams, i);
3955 if (font.language() == from) {
3956 font.setLanguage(to);
3963 bool LyXParagraph::isMultiLingual(BufferParams const & bparams)
3965 Language const * doc_language = bparams.language;
3966 for (FontList::const_iterator cit = fontlist.begin();
3967 cit != fontlist.end(); ++cit)
3968 if ((*cit).font.language() != doc_language)
3974 // Convert the paragraph to a string.
3975 // Used for building the table of contents
3976 string const LyXParagraph::String(Buffer const * buffer, bool label)
3978 BufferParams const & bparams = buffer->params;
3981 if (label && !IsDummy() && !labelstring.empty())
3983 if (label && !labelstring.empty())
3985 s += labelstring + ' ';
3986 string::size_type const len = s.size();
3988 for (LyXParagraph::size_type i = 0; i < size(); ++i) {
3989 value_type c = GetChar(i);
3992 else if (c == META_INSET &&
3993 GetInset(i)->LyxCode() == Inset::MATH_CODE) {
3994 std::ostringstream ost;
3995 GetInset(i)->Ascii(buffer, ost);
3996 s += subst(ost.str().c_str(),'\n',' ');
4001 if (next && next->footnoteflag != LyXParagraph::NO_FOOTNOTE
4002 && footnoteflag == LyXParagraph::NO_FOOTNOTE)
4003 s += NextAfterFootnote()->String(buffer, false);
4007 if (isRightToLeftPar(bparams))
4008 reverse(s.begin() + len,s.end());
4016 string const LyXParagraph::String(Buffer const * buffer,
4017 LyXParagraph::size_type beg,
4018 LyXParagraph::size_type end)
4023 if (beg == 0 && !IsDummy() && !labelstring.empty())
4025 if (beg == 0 && !labelstring.empty())
4027 s += labelstring + ' ';
4029 for (LyXParagraph::size_type i = beg; i < end; ++i) {
4030 value_type c = GetUChar(buffer->params, i);
4033 else if (c == META_INSET) {
4034 std::ostringstream ost;
4035 GetInset(i)->Ascii(buffer, ost);
4036 s += ost.str().c_str();
4044 void LyXParagraph::SetInsetOwner(Inset * i)
4047 for (InsetList::const_iterator cit = insetlist.begin();
4048 cit != insetlist.end(); ++cit) {
4050 (*cit).inset->setOwner(i);
4055 void LyXParagraph::deleteInsetsLyXText(BufferView * bv)
4058 for (InsetList::const_iterator cit = insetlist.begin();
4059 cit != insetlist.end(); ++cit) {
4061 if ((*cit).inset->IsTextInset()) {
4062 static_cast<UpdatableInset *>
4063 ((*cit).inset)->deleteLyXText(bv);
4070 void LyXParagraph::resizeInsetsLyXText(BufferView * bv)
4073 for (InsetList::const_iterator cit = insetlist.begin();
4074 cit != insetlist.end(); ++cit) {
4076 if ((*cit).inset->IsTextInset()) {
4077 static_cast<UpdatableInset *>
4078 ((*cit).inset)->resizeLyXText(bv);
4085 void LyXParagraph::fitToSize()
4087 TextContainer tmp(text.begin(), text.end());
4092 void LyXParagraph::setContentsFromPar(LyXParagraph * par)