1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
21 #include "paragraph.h"
22 #include "paragraph_pimpl.h"
23 #include "support/textutils.h"
27 #include "tex-strings.h"
29 #include "bufferparams.h"
30 #include "support/FileInfo.h"
31 #include "support/LAssert.h"
33 #include "LaTeXFeatures.h"
34 #include "insets/insetinclude.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
37 #include "support/filetools.h"
38 #include "lyx_gui_misc.h"
40 #include "support/lyxmanip.h"
41 #include "BufferView.h"
43 #include "ParameterStruct.h"
50 using std::lower_bound;
51 using std::upper_bound;
54 int tex_code_break_column = 72; // needs non-zero initialization. set later.
55 // this is a bad idea, but how can Paragraph find its buffer to get
56 // parameters? (JMarc)
58 extern string bibitemWidest(Buffer const *);
60 // this is a minibuffer
65 LyXFont minibuffer_font;
66 Inset * minibuffer_inset;
71 extern BufferView * current_view;
74 Paragraph::Paragraph()
75 : pimpl_(new Paragraph::Pimpl(this))
77 for (int i = 0; i < 10; ++i) setCounter(i , 0);
82 bibkey = 0; // ale970302
87 // This konstruktor inserts the new paragraph in a list.
88 Paragraph::Paragraph(Paragraph * par)
89 : pimpl_(new Paragraph::Pimpl(this))
91 for (int i = 0; i < 10; ++i)
96 // double linked list begin
99 next_->previous_ = this;
101 previous_->next_ = this;
104 bibkey = 0; // ale970302
110 Paragraph::Paragraph(Paragraph const & lp)
111 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
113 for (int i = 0; i < 10; ++i) setCounter(i , 0);
122 // this is because of the dummy layout of the paragraphs that
128 bibkey = static_cast<InsetBibKey *>
129 (lp.bibkey->Clone(*current_view->buffer()));
133 // copy everything behind the break-position to the new paragraph
135 insetlist = lp.insetlist;
136 for (InsetList::iterator it = insetlist.begin();
137 it != insetlist.end(); ++it)
138 it->inset = it->inset->Clone(*current_view->buffer());
142 // the destructor removes the new paragraph from the list
143 Paragraph::~Paragraph()
146 previous_->next_ = next_;
148 next_->previous_ = previous_;
150 for (InsetList::iterator it = insetlist.begin();
151 it != insetlist.end(); ++it) {
160 //lyxerr << "Paragraph::paragraph_id = "
161 // << Paragraph::paragraph_id << endl;
165 void Paragraph::writeFile(Buffer const * buf, ostream & os,
166 BufferParams const & bparams,
167 depth_type dth) const
169 // The beginning or end of a deeper (i.e. nested) area?
170 if (dth != params().depth()) {
171 if (params().depth() > dth) {
172 while (params().depth() > dth) {
173 os << "\n\\begin_deeper ";
177 while (params().depth() < dth) {
178 os << "\n\\end_deeper ";
184 // First write the layout
186 << textclasslist.NameOfLayout(bparams.textclass, layout)
189 // Maybe some vertical spaces.
190 if (params().spaceTop().kind() != VSpace::NONE)
191 os << "\\added_space_top "
192 << params().spaceTop().asLyXCommand() << " ";
193 if (params().spaceBottom().kind() != VSpace::NONE)
194 os << "\\added_space_bottom "
195 << params().spaceBottom().asLyXCommand() << " ";
197 // Maybe the paragraph has special spacing
198 params().spacing().writeFile(os, true);
200 // The labelwidth string used in lists.
201 if (!params().labelWidthString().empty())
202 os << "\\labelwidthstring "
203 << params().labelWidthString() << '\n';
205 // Lines above or below?
206 if (params().lineTop())
208 if (params().lineBottom())
209 os << "\\line_bottom ";
211 // Pagebreaks above or below?
212 if (params().pagebreakTop())
213 os << "\\pagebreak_top ";
214 if (params().pagebreakBottom())
215 os << "\\pagebreak_bottom ";
217 // Start of appendix?
218 if (params().startOfAppendix())
219 os << "\\start_of_appendix ";
222 if (params().noindent())
226 if (params().align() != LYX_ALIGN_LAYOUT) {
228 switch (params().align()) {
229 case LYX_ALIGN_LEFT: h = 1; break;
230 case LYX_ALIGN_RIGHT: h = 2; break;
231 case LYX_ALIGN_CENTER: h = 3; break;
232 default: h = 0; break;
234 os << "\\align " << string_align[h] << " ";
239 bibkey->Write(buf, os);
241 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
244 for (size_type i = 0; i < size(); ++i) {
250 // Write font changes
251 LyXFont font2 = getFontSettings(bparams, i);
252 if (font2 != font1) {
253 font2.lyxWriteChanges(font1, os);
258 value_type const c = getChar(i);
262 Inset const * inset = getInset(i);
264 if (inset->DirectWrite()) {
265 // international char, let it write
266 // code directly so it's shorter in
268 inset->Write(buf, os);
270 os << "\n\\begin_inset ";
271 inset->Write(buf, os);
272 os << "\n\\end_inset \n\n";
278 os << "\n\\newline \n";
282 os << "\n\\hfill \n";
286 os << "\n\\backslash \n";
290 if (i + 1 < size() && getChar(i + 1) == ' ') {
297 if ((column > 70 && c == ' ')
302 // this check is to amend a bug. LyX sometimes
303 // inserts '\0' this could cause problems.
307 lyxerr << "ERROR (Paragraph::writeFile):"
308 " NULL char in structure." << endl;
314 // now write the next paragraph
316 next_->writeFile(buf, os, bparams, dth);
320 void Paragraph::validate(LaTeXFeatures & features) const
322 BufferParams const & bparams = features.bufferParams();
325 if (params().lineTop() || params().lineBottom())
326 features.lyxline = true;
327 if (!params().spacing().isDefault())
328 features.setspace = true;
331 features.layout[getLayout()] = true;
334 Language const * doc_language = bparams.language;
336 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
337 cit != pimpl_->fontlist.end(); ++cit) {
338 if ((*cit).font().noun() == LyXFont::ON) {
339 lyxerr[Debug::LATEX] << "font.noun: "
340 << (*cit).font().noun()
342 features.noun = true;
343 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
344 << (*cit).font().stateText(0)
347 switch ((*cit).font().color()) {
349 case LColor::inherit:
353 features.color = true;
354 lyxerr[Debug::LATEX] << "Color enabled. Font: "
355 << (*cit).font().stateText(0)
359 Language const * language = (*cit).font().language();
360 if (language->babel() != doc_language->babel()) {
361 features.UsedLanguages.insert(language);
362 lyxerr[Debug::LATEX] << "Found language "
363 << language->babel() << endl;
368 for (InsetList::const_iterator cit = insetlist.begin();
369 cit != insetlist.end(); ++cit) {
371 (*cit).inset->Validate(features);
376 // First few functions needed for cut and paste and paragraph breaking.
377 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
378 Paragraph::size_type pos) const
380 BufferParams bparams = buffer.params;
382 minibuffer_char = getChar(pos);
383 minibuffer_font = getFontSettings(bparams, pos);
384 minibuffer_inset = 0;
385 if (minibuffer_char == Paragraph::META_INSET) {
387 minibuffer_inset = getInset(pos)->Clone(buffer);
389 minibuffer_inset = 0;
390 minibuffer_char = ' ';
391 // This reflects what GetInset() does (ARRae)
397 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
398 Paragraph::size_type pos)
400 minibuffer_char = getChar(pos);
401 minibuffer_font = getFontSettings(bparams, pos);
402 minibuffer_inset = 0;
403 if (minibuffer_char == Paragraph::META_INSET) {
405 minibuffer_inset = getInset(pos);
406 // This is a little hack since I want exactly
407 // the inset, not just a clone. Otherwise
408 // the inset would be deleted when calling Erase(pos)
410 InsetTable search_elem(pos, 0);
411 InsetList::iterator it =
412 lower_bound(insetlist.begin(),
414 search_elem, Pimpl::matchIT());
415 if (it != insetlist.end() && (*it).pos == pos)
418 minibuffer_inset = 0;
419 minibuffer_char = ' ';
420 // This reflects what GetInset() does (ARRae)
425 // Erase(pos); now the caller is responsible for that.
429 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
431 if ((minibuffer_char == Paragraph::META_INSET) &&
432 !insertInsetAllowed(minibuffer_inset))
434 if (minibuffer_char == Paragraph::META_INSET)
435 insertInset(pos, minibuffer_inset, minibuffer_font);
437 insertChar(pos, minibuffer_char, minibuffer_font);
445 void Paragraph::clear()
454 void Paragraph::erase(Paragraph::size_type pos)
460 void Paragraph::insertChar(Paragraph::size_type pos,
461 Paragraph::value_type c)
463 LyXFont const f(LyXFont::ALL_INHERIT);
464 insertChar(pos, c, f);
468 void Paragraph::insertChar(Paragraph::size_type pos,
469 Paragraph::value_type c,
470 LyXFont const & font)
472 pimpl_->insertChar(pos, c, font);
476 void Paragraph::insertInset(Paragraph::size_type pos,
479 LyXFont const f(LyXFont::ALL_INHERIT);
480 insertInset(pos, inset, f);
484 void Paragraph::insertInset(Paragraph::size_type pos,
485 Inset * inset, LyXFont const & font)
487 pimpl_->insertInset(pos, inset, font);
491 bool Paragraph::insertInsetAllowed(Inset * inset)
493 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
495 if (pimpl_->inset_owner)
496 return pimpl_->inset_owner->InsertInsetAllowed(inset);
501 Inset * Paragraph::getInset(Paragraph::size_type pos)
503 lyx::Assert(pos < size());
506 InsetTable search_inset(pos, 0);
507 InsetList::iterator it = lower_bound(insetlist.begin(),
509 search_inset, Pimpl::matchIT());
510 if (it != insetlist.end() && (*it).pos == pos)
513 lyxerr << "ERROR (Paragraph::GetInset): "
514 "Inset does not exist: " << pos << endl;
517 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
518 // Did this commenting out introduce a bug? So far I have not
519 // see any, please enlighten me. (Lgb)
520 // My guess is that since the inset does not exist, we might
521 // as well replace it with a space to prevent craches. (Asger)
526 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
528 lyx::Assert(pos < size());
531 InsetTable search_inset(pos, 0);
532 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
534 search_inset, Pimpl::matchIT());
535 if (cit != insetlist.end() && (*cit).pos == pos)
538 lyxerr << "ERROR (Paragraph::GetInset): "
539 "Inset does not exist: " << pos << endl;
541 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
542 // Did this commenting out introduce a bug? So far I have not
543 // see any, please enlighten me. (Lgb)
544 // My guess is that since the inset does not exist, we might
545 // as well replace it with a space to prevent craches. (Asger)
550 // Gets uninstantiated font setting at position.
551 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
552 Paragraph::size_type pos) const
554 lyx::Assert(pos <= size());
556 Pimpl::FontTable search_font(pos, LyXFont());
557 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
558 pimpl_->fontlist.end(),
559 search_font, Pimpl::matchFT());
560 if (cit != pimpl_->fontlist.end())
561 return (*cit).font();
563 if (pos == size() && size())
564 return getFontSettings(bparams, pos - 1);
566 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
570 // Gets uninstantiated font setting at position 0
571 LyXFont const Paragraph::getFirstFontSettings() const
574 if (!pimpl_->fontlist.empty())
575 return pimpl_->fontlist[0].font();
578 return LyXFont(LyXFont::ALL_INHERIT);
582 // Gets the fully instantiated font at a given position in a paragraph
583 // This is basically the same function as LyXText::GetFont() in text2.C.
584 // The difference is that this one is used for generating the LaTeX file,
585 // and thus cosmetic "improvements" are disallowed: This has to deliver
586 // the true picture of the buffer. (Asger)
587 // If position is -1, we get the layout font of the paragraph.
588 // If position is -2, we get the font of the manual label of the paragraph.
589 LyXFont const Paragraph::getFont(BufferParams const & bparams,
590 Paragraph::size_type pos) const
593 LyXLayout const & layout =
594 textclasslist.Style(bparams.textclass,
596 Paragraph::size_type main_body = 0;
597 if (layout.labeltype == LABEL_MANUAL)
598 main_body = beginningOfMainBody();
603 layoutfont = layout.labelfont;
605 layoutfont = layout.font;
606 tmpfont = getFontSettings(bparams, pos);
607 tmpfont.realize(layoutfont);
609 // process layoutfont for pos == -1 and labelfont for pos < -1
611 tmpfont = layout.font;
613 tmpfont = layout.labelfont;
614 tmpfont.setLanguage(getParLanguage(bparams));
617 // check for environment font information
618 char par_depth = getDepth();
619 Paragraph const * par = this;
620 while (par && par->getDepth() && !tmpfont.resolved()) {
621 par = par->outerHook();
623 tmpfont.realize(textclasslist.
624 Style(bparams.textclass,
625 par->getLayout()).font);
626 par_depth = par->getDepth();
630 tmpfont.realize(textclasslist
631 .TextClass(bparams.textclass)
637 /// Returns the height of the highest font in range
639 Paragraph::highestFontInRange(Paragraph::size_type startpos,
640 Paragraph::size_type endpos) const
642 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
643 if (pimpl_->fontlist.empty())
646 Pimpl::FontTable end_search(endpos, LyXFont());
647 Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
648 pimpl_->fontlist.end(),
649 end_search, Pimpl::matchFT());
650 if (end_it != pimpl_->fontlist.end())
653 Pimpl::FontTable start_search(startpos, LyXFont());
654 for (Pimpl::FontList::const_iterator cit =
655 lower_bound(pimpl_->fontlist.begin(),
656 pimpl_->fontlist.end(),
657 start_search, Pimpl::matchFT());
658 cit != end_it; ++cit) {
659 LyXFont::FONT_SIZE size = (*cit).font().size();
660 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
667 Paragraph::value_type
668 Paragraph::getUChar(BufferParams const & bparams,
669 Paragraph::size_type pos) const
671 value_type c = getChar(pos);
672 if (!lyxrc.rtl_support)
702 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
709 // return an string of the current word, and the end of the word in lastpos.
710 string const Paragraph::getWord(Paragraph::size_type & lastpos) const
712 lyx::Assert(lastpos >= 0);
714 // the current word is defined as starting at the first character
715 // from the immediate left of lastpospos which meets the definition
716 // of IsLetter(), continuing to the last character to the right
717 // of this meeting IsLetter.
723 // move back until we have a letter
725 //there's no real reason to have firstpos & lastpos as
726 //separate variables as this is written, but maybe someon
727 // will want to return firstpos in the future.
729 //since someone might have typed a punctuation first
730 int firstpos = lastpos;
732 while ((firstpos >= 0) && !isLetter(firstpos))
735 // now find the beginning by looking for a nonletter
737 while ((firstpos>= 0) && isLetter(firstpos))
740 // the above is now pointing to the preceeding non-letter
744 // so copy characters into theword until we get a nonletter
745 // note that this can easily exceed lastpos, wich means
746 // that if used in the middle of a word, the whole word
749 while (isLetter(lastpos)) theword += getChar(lastpos++);
755 void Paragraph::setFont(Paragraph::size_type pos,
756 LyXFont const & font)
758 lyx::Assert(pos <= size());
760 // First, reduce font against layout/label font
761 // Update: The SetCharFont() routine in text2.C already
762 // reduces font, so we don't need to do that here. (Asger)
763 // No need to simplify this because it will disappear
764 // in a new kernel. (Asger)
765 // Next search font table
767 Pimpl::FontTable search_font(pos, LyXFont());
768 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
769 pimpl_->fontlist.end(),
770 search_font, Pimpl::matchFT());
771 unsigned int i = it - pimpl_->fontlist.begin();
772 bool notfound = it == pimpl_->fontlist.end();
774 if (!notfound && pimpl_->fontlist[i].font() == font)
777 bool begin = pos == 0 || notfound ||
778 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
779 // Is position pos is a beginning of a font block?
780 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
781 // Is position pos is the end of a font block?
782 if (begin && end) { // A single char block
783 if (i + 1 < pimpl_->fontlist.size() &&
784 pimpl_->fontlist[i + 1].font() == font) {
785 // Merge the singleton block with the next block
786 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
787 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
788 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
789 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
790 // Merge the singleton block with the previous block
791 pimpl_->fontlist[i - 1].pos(pos);
792 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
794 pimpl_->fontlist[i].font(font);
796 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
797 pimpl_->fontlist[i - 1].pos(pos);
799 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
800 Pimpl::FontTable(pos, font));
802 pimpl_->fontlist[i].pos(pos - 1);
803 if (!(i + 1 < pimpl_->fontlist.size() &&
804 pimpl_->fontlist[i + 1].font() == font))
805 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
806 Pimpl::FontTable(pos, font));
807 } else { // The general case. The block is splitted into 3 blocks
808 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
809 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
810 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
811 Pimpl::FontTable(pos, font));
817 void Paragraph::next(Paragraph * p)
823 // This function is able to hide closed footnotes.
824 Paragraph * Paragraph::next()
830 Paragraph const * Paragraph::next() const
836 void Paragraph::previous(Paragraph * p)
842 // This function is able to hide closed footnotes.
843 Paragraph * Paragraph::previous()
849 // This function is able to hide closed footnotes.
850 Paragraph const * Paragraph::previous() const
856 void Paragraph::breakParagraph(BufferParams const & bparams,
857 Paragraph::size_type pos,
860 // create a new paragraph
861 Paragraph * tmp = new Paragraph(this);
862 // remember to set the inset_owner
863 tmp->setInsetOwner(InInset());
865 // this is an idea for a more userfriendly layout handling, I will
866 // see what the users say
868 // layout stays the same with latex-environments
870 tmp->setOnlyLayout(layout);
871 tmp->setLabelWidthString(params().labelWidthString());
874 if (size() > pos || !size() || flag == 2) {
875 tmp->setOnlyLayout(layout);
876 tmp->params().align(params().align());
877 tmp->setLabelWidthString(params().labelWidthString());
879 tmp->params().lineBottom(params().lineBottom());
880 params().lineBottom(false);
881 tmp->params().pagebreakBottom(params().pagebreakBottom());
882 params().pagebreakBottom(false);
883 tmp->params().spaceBottom(params().spaceBottom());
884 params().spaceBottom(VSpace(VSpace::NONE));
886 tmp->params().depth(params().depth());
887 tmp->params().noindent(params().noindent());
889 // copy everything behind the break-position
890 // to the new paragraph
891 size_type pos_end = pimpl_->size() - 1;
894 for (; i <= pos_end; ++i) {
895 cutIntoMinibuffer(bparams, i);
896 if (tmp->insertFromMinibuffer(j - pos))
899 for (i = pos_end; i >= pos; --i) {
904 // just an idea of me
906 tmp->params().lineTop(params().lineTop());
907 tmp->params().pagebreakTop(params().pagebreakTop());
908 tmp->params().spaceTop(params().spaceTop());
909 tmp->bibkey = bibkey;
911 // layout stays the same with latex-environments
913 setOnlyLayout(tmp->layout);
914 setLabelWidthString(tmp->params().labelWidthString());
915 params().depth(tmp->params().depth());
921 void Paragraph::makeSameLayout(Paragraph const * par)
923 layout = par->layout;
924 params().makeSame(par->params());
926 // This can be changed after NEW_INSETS is in effect. (Lgb)
927 setLabelWidthString(par->params().labelWidthString());
931 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
933 if (textclasslist.Style(tclass, getLayout()).free_spacing)
938 && (isNewline(0) || isLineSeparator(0))){
947 bool Paragraph::hasSameLayout(Paragraph const * par) const
950 par->layout == layout &&
951 params().sameLayout(par->params());
955 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
956 Paragraph::size_type pos)
958 // create a new paragraph
959 Paragraph * tmp = new Paragraph(this);
960 tmp->makeSameLayout(this);
962 // When can pos > Last()?
963 // I guess pos == Last() is possible.
965 // copy everything behind the break-position to the new
967 size_type pos_end = pimpl_->size() - 1;
971 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
972 cutIntoMinibuffer(bparams, i);
973 if (tmp->insertFromMinibuffer(j - pos))
977 for (size_type k = pos_end; k >= pos; --k) {
984 // Be carefull, this does not make any check at all.
985 // This method has wrong name, it combined this par with the next par.
986 // In that sense it is the reverse of break paragraph. (Lgb)
987 void Paragraph::pasteParagraph(BufferParams const & bparams)
989 // copy the next paragraph to this one
990 Paragraph * the_next = next();
992 // first the DTP-stuff
993 params().lineBottom(the_next->params().lineBottom());
994 params().spaceBottom(the_next->params().spaceBottom());
995 params().pagebreakBottom(the_next->params().pagebreakBottom());
997 size_type pos_end = the_next->pimpl_->size() - 1;
998 size_type pos_insert = size();
1000 // ok, now copy the paragraph
1001 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
1002 the_next->cutIntoMinibuffer(bparams, i);
1003 if (insertFromMinibuffer(pos_insert + j))
1007 // delete the next paragraph
1008 Paragraph * ppar = the_next->previous_;
1009 Paragraph * npar = the_next->next_;
1015 int Paragraph::getEndLabel(BufferParams const & bparams) const
1017 Paragraph const * par = this;
1018 depth_type par_depth = getDepth();
1020 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1021 int const endlabeltype =
1022 textclasslist.Style(bparams.textclass,
1023 layout).endlabeltype;
1024 if (endlabeltype != END_LABEL_NO_LABEL) {
1026 return endlabeltype;
1028 depth_type const next_depth = next_->getDepth();
1029 if (par_depth > next_depth ||
1030 (par_depth == next_depth
1031 && layout != next_->getLayout()))
1032 return endlabeltype;
1037 par = par->outerHook();
1039 par_depth = par->getDepth();
1041 return END_LABEL_NO_LABEL;
1045 Paragraph::depth_type Paragraph::getDepth() const
1047 return params().depth();
1051 char Paragraph::getAlign() const
1053 return params().align();
1057 string const & Paragraph::getLabelstring() const
1059 return params().labelString();
1063 int Paragraph::getFirstCounter(int i) const
1065 return pimpl_->counter_[i];
1069 // the next two functions are for the manual labels
1070 string const Paragraph::getLabelWidthString() const
1072 if (!params().labelWidthString().empty())
1073 return params().labelWidthString();
1075 return _("Senseless with this layout!");
1079 void Paragraph::setLabelWidthString(string const & s)
1081 params().labelWidthString(s);
1085 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1087 layout = new_layout;
1091 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1093 layout = new_layout;
1094 params().labelWidthString(string());
1095 params().align(LYX_ALIGN_LAYOUT);
1096 params().spaceTop(VSpace(VSpace::NONE));
1097 params().spaceBottom(VSpace(VSpace::NONE));
1098 params().spacing(Spacing(Spacing::Default));
1102 // if the layout of a paragraph contains a manual label, the beginning of the
1103 // main body is the beginning of the second word. This is what the par-
1104 // function returns. If the layout does not contain a label, the main
1105 // body always starts with position 0. This differentiation is necessary,
1106 // because there cannot be a newline or a blank <= the beginning of the
1107 // main body in TeX.
1109 int Paragraph::beginningOfMainBody() const
1111 // Unroll the first two cycles of the loop
1112 // and remember the previous character to
1113 // remove unnecessary GetChar() calls
1116 && getChar(i) != Paragraph::META_NEWLINE) {
1118 char previous_char = 0;
1121 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1122 // Yes, this ^ is supposed to be "= " not "=="
1125 && previous_char != ' '
1126 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1128 previous_char = temp;
1137 Paragraph * Paragraph::depthHook(depth_type depth)
1139 Paragraph * newpar = this;
1142 newpar = newpar->previous();
1143 } while (newpar && newpar->getDepth() > depth);
1146 if (previous() || getDepth())
1147 lyxerr << "ERROR (Paragraph::DepthHook): "
1156 Paragraph const * Paragraph::depthHook(depth_type depth) const
1158 Paragraph const * newpar = this;
1161 newpar = newpar->previous();
1162 } while (newpar && newpar->getDepth() > depth);
1165 if (previous() || getDepth())
1166 lyxerr << "ERROR (Paragraph::DepthHook): "
1174 Paragraph * Paragraph::outerHook()
1178 return depthHook(depth_type(getDepth() - 1));
1181 Paragraph const * Paragraph::outerHook() const
1185 return depthHook(depth_type(getDepth() - 1));
1188 int Paragraph::autoDeleteInsets()
1191 InsetList::size_type index = 0;
1192 while (index < insetlist.size()) {
1193 if (insetlist[index].inset && insetlist[index].inset->AutoDelete()) {
1194 erase(insetlist[index].pos);
1195 // Erase() calls to insetlist.erase(&insetlist[index])
1196 // so index shouldn't be increased.
1205 Paragraph::inset_iterator
1206 Paragraph::InsetIterator(Paragraph::size_type pos)
1208 InsetTable search_inset(pos, 0);
1209 InsetList::iterator it = lower_bound(insetlist.begin(),
1211 search_inset, Pimpl::matchIT());
1212 return inset_iterator(it);
1216 // returns -1 if inset not found
1217 int Paragraph::getPositionOfInset(Inset * inset) const
1220 for (InsetList::const_iterator cit = insetlist.begin();
1221 cit != insetlist.end(); ++cit) {
1222 if ((*cit).inset == inset) {
1226 if (inset == bibkey)
1233 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1234 BufferParams const & bparams,
1235 ostream & os, TexRow & texrow,
1238 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1239 LyXLayout const & style =
1240 textclasslist.Style(bparams.textclass,
1243 bool further_blank_line = false;
1245 if (params().startOfAppendix()) {
1246 os << "\\appendix\n";
1250 if (!params().spacing().isDefault()
1251 && (!previous() || !previous()->hasSameLayout(this))) {
1252 os << params().spacing().writeEnvirBegin() << "\n";
1256 if (tex_code_break_column && style.isCommand()){
1261 if (params().pagebreakTop()) {
1263 further_blank_line = true;
1265 if (params().spaceTop().kind() != VSpace::NONE) {
1266 os << params().spaceTop().asLatexCommand(bparams);
1267 further_blank_line = true;
1270 if (params().lineTop()) {
1271 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1272 << "\\vspace{-1\\parskip}";
1273 further_blank_line = true;
1276 if (further_blank_line){
1281 Language const * language = getParLanguage(bparams);
1282 Language const * doc_language = bparams.language;
1283 Language const * previous_language = previous_
1284 ? previous_->getParLanguage(bparams) : doc_language;
1285 if (language->babel() != doc_language->babel() &&
1286 language->babel() != previous_language->babel()) {
1287 os << subst(lyxrc.language_command_begin, "$$lang",
1293 if (bparams.inputenc == "auto" &&
1294 language->encoding() != previous_language->encoding()) {
1295 os << "\\inputencoding{"
1296 << language->encoding()->LatexName()
1301 switch (style.latextype) {
1304 << style.latexname()
1305 << style.latexparam();
1307 case LATEX_ITEM_ENVIRONMENT:
1309 bibkey->Latex(buf, os, false, false);
1313 case LATEX_LIST_ENVIRONMENT:
1320 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1322 // Make sure that \\par is done with the font of the last
1323 // character if this has another size as the default.
1324 // This is necessary because LaTeX (and LyX on the screen)
1325 // calculates the space between the baselines according
1326 // to this font. (Matthias)
1328 // Is this really needed ? (Dekel)
1329 // We do not need to use to change the font for the last paragraph
1330 // or for a command.
1331 LyXFont font = getFont(bparams, size() - 1);
1333 bool is_command = textclasslist.Style(bparams.textclass,
1334 getLayout()).isCommand();
1335 if (style.resfont.size() != font.size() && next_ && !is_command) {
1338 os << "\\" << font.latexSize() << " \\par}";
1339 } else if (need_par) {
1341 } else if (is_command)
1344 if (language->babel() != doc_language->babel() &&
1346 || next_->getParLanguage(bparams)->babel() != language->babel())) {
1348 << subst(lyxrc.language_command_end, "$$lang",
1349 doc_language->babel());
1352 switch (style.latextype) {
1353 case LATEX_ITEM_ENVIRONMENT:
1354 case LATEX_LIST_ENVIRONMENT:
1355 if (next_ && (params().depth() < next_->params().depth())) {
1360 case LATEX_ENVIRONMENT:
1361 // if its the last paragraph of the current environment
1362 // skip it otherwise fall through
1364 && (next_->layout != layout
1365 || next_->params().depth() != params().depth()))
1368 // we don't need it for the last paragraph!!!
1369 // or for tables in floats
1370 // -- effectively creates a \par where there isn't one which
1371 // breaks a \subfigure or \subtable.
1378 further_blank_line = false;
1379 if (params().lineBottom()) {
1380 os << "\\lyxline{\\" << getFont(bparams,
1381 size() - 1).latexSize() << '}';
1382 further_blank_line = true;
1385 if (params().spaceBottom().kind() != VSpace::NONE) {
1386 os << params().spaceBottom().asLatexCommand(bparams);
1387 further_blank_line = true;
1390 if (params().pagebreakBottom()) {
1392 further_blank_line = true;
1395 if (further_blank_line){
1400 if (!params().spacing().isDefault()
1401 && (!next_ || !next_->hasSameLayout(this))) {
1402 os << params().spacing().writeEnvirEnd() << "\n";
1406 // we don't need it for the last paragraph!!!
1412 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1417 // This one spits out the text of the paragraph
1418 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1419 BufferParams const & bparams,
1420 ostream & os, TexRow & texrow,
1423 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1425 bool return_value = false;
1427 LyXLayout const & style =
1428 textclasslist.Style(bparams.textclass,
1432 // Maybe we have to create a optional argument.
1433 size_type main_body;
1434 if (style.labeltype != LABEL_MANUAL)
1437 main_body = beginningOfMainBody();
1439 if (main_body > 0) {
1441 basefont = getFont(bparams, -2); // Get label font
1443 basefont = getFont(bparams, -1); // Get layout font
1449 && !pimpl_->size()) {
1450 if (style.isCommand()) {
1453 } else if (params().align() != LYX_ALIGN_LAYOUT) {
1456 return_value = true;
1460 moving_arg |= style.needprotect;
1462 // Which font is currently active?
1463 LyXFont running_font(basefont);
1464 // Do we have an open font change?
1465 bool open_font = false;
1467 texrow.start(this, 0);
1469 for (size_type i = 0; i < size(); ++i) {
1471 // First char in paragraph or after label?
1472 if (i == main_body) {
1473 if (main_body > 0) {
1475 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1478 basefont = getFont(bparams, -1); // Now use the layout font
1479 running_font = basefont;
1483 if (style.isCommand()) {
1486 } else if (params().align() != LYX_ALIGN_LAYOUT && next_) {
1487 // We do not need \par here (Dekel)
1491 return_value = true;
1494 if (params().noindent()) {
1495 os << "\\noindent ";
1498 switch (params().align()) {
1499 case LYX_ALIGN_NONE:
1500 case LYX_ALIGN_BLOCK:
1501 case LYX_ALIGN_LAYOUT:
1502 case LYX_ALIGN_SPECIAL:
1504 case LYX_ALIGN_LEFT:
1507 if (getParLanguage(bparams)->babel() != "hebrew") {
1508 os << "\\raggedright ";
1511 os << "\\raggedleft ";
1515 case LYX_ALIGN_RIGHT:
1518 if (getParLanguage(bparams)->babel() != "hebrew") {
1519 os << "\\raggedleft ";
1522 os << "\\raggedright ";
1526 case LYX_ALIGN_CENTER:
1529 os << "\\centering ";
1535 value_type c = getChar(i);
1537 // Fully instantiated font
1538 LyXFont font = getFont(bparams, i);
1540 LyXFont last_font = running_font;
1542 // Spaces at end of font change are simulated to be
1543 // outside font change, i.e. we write "\textXX{text} "
1544 // rather than "\textXX{text }". (Asger)
1545 if (open_font && c == ' ' && i <= size() - 2
1546 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1547 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1548 font = getFont(bparams, i + 1);
1550 // We end font definition before blanks
1551 if (!font.equalExceptLatex(running_font) && open_font) {
1552 column += running_font.latexWriteEndChanges(os,
1554 (i == main_body-1) ? basefont : font);
1555 running_font = basefont;
1559 // Blanks are printed before start of fontswitch
1561 // Do not print the separation of the optional argument
1562 if (i != main_body - 1) {
1563 pimpl_->simpleTeXBlanks(os, texrow, i,
1564 column, font, style);
1568 // Do we need to change font?
1569 if (!font.equalExceptLatex(running_font)
1570 && i != main_body-1) {
1571 column += font.latexWriteStartChanges(os, basefont,
1573 running_font = font;
1577 if (c == Paragraph::META_NEWLINE) {
1578 // newlines are handled differently here than
1579 // the default in SimpleTeXSpecialChars().
1580 if (!style.newline_allowed
1581 || font.latex() == LyXFont::ON) {
1585 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1588 basefont = getFont(bparams, -1);
1589 running_font = basefont;
1590 if (font.family() ==
1591 LyXFont::TYPEWRITER_FAMILY) {
1599 texrow.start(this, i + 1);
1602 pimpl_->simpleTeXSpecialChars(buf, bparams,
1603 os, texrow, moving_arg,
1605 basefont, open_font,
1606 style, i, column, c);
1610 // If we have an open font definition, we have to close it
1614 .latexWriteEndChanges(os, basefont,
1615 next_->getFont(bparams,
1618 running_font.latexWriteEndChanges(os, basefont,
1623 // Needed if there is an optional argument but no contents.
1624 if (main_body > 0 && main_body == size()) {
1626 return_value = false;
1629 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1630 return return_value;
1634 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1636 bool retval = false;
1638 case Paragraph::META_HFILL:
1639 sgml_string.erase();
1641 case Paragraph::META_NEWLINE:
1645 sgml_string = "&";
1648 sgml_string = "<";
1651 sgml_string = ">";
1654 sgml_string = "$";
1657 sgml_string = "#";
1660 sgml_string = "%";
1663 sgml_string = "[";
1666 sgml_string = "]";
1669 sgml_string = "{";
1672 sgml_string = "}";
1675 sgml_string = "˜";
1678 sgml_string = """;
1681 sgml_string = "\";
1687 case '\0': // Ignore :-)
1688 sgml_string.erase();
1698 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1699 BufferParams const & bparams,
1700 ostream & os, TexRow & texrow)
1702 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1704 LyXLayout const & style =
1705 textclasslist.Style(bparams.textclass,
1708 if (style.isEnvironment()){
1709 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1710 os << "\\begin{" << style.latexname() << "}{"
1711 << params().labelWidthString() << "}\n";
1712 } else if (style.labeltype == LABEL_BIBLIO) {
1714 os << "\\begin{" << style.latexname() << "}{"
1715 << bibitemWidest(buf)
1717 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1718 os << "\\begin{" << style.latexname() << '}'
1719 << style.latexparam() << '\n';
1721 os << "\\begin{" << style.latexname() << '}'
1722 << style.latexparam() << '\n';
1725 Paragraph * par = this;
1727 par = par->TeXOnePar(buf, bparams,
1730 if (par && par->params().depth() > params().depth()) {
1731 if (textclasslist.Style(bparams.textclass,
1732 par->layout).isParagraph()
1734 // How to handle this? (Lgb)
1735 //&& !suffixIs(os, "\n\n")
1737 // There should be at least one '\n' already
1738 // but we need there to be two for Standard
1739 // paragraphs that are depth-increment'ed to be
1740 // output correctly. However, tables can
1741 // also be paragraphs so don't adjust them.
1744 // Will it ever harm to have one '\n' too
1745 // many? i.e. that we sometimes will have
1746 // three in a row. (Lgb)
1750 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1753 && par->layout == layout
1754 && par->params().depth() == params().depth());
1756 if (style.isEnvironment()) {
1757 os << "\\end{" << style.latexname() << "}\n";
1760 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1761 return par; // ale970302
1765 bool Paragraph::isHfill(size_type pos) const
1767 return IsHfillChar(getChar(pos));
1771 bool Paragraph::isInset(size_type pos) const
1773 return IsInsetChar(getChar(pos));
1777 bool Paragraph::isNewline(size_type pos) const
1779 return pos >= 0 && IsNewlineChar(getChar(pos));
1783 bool Paragraph::isSeparator(size_type pos) const
1785 return IsSeparatorChar(getChar(pos));
1789 bool Paragraph::isLineSeparator(size_type pos) const
1791 return IsLineSeparatorChar(getChar(pos));
1795 bool Paragraph::isKomma(size_type pos) const
1797 return IsKommaChar(getChar(pos));
1801 /// Used by the spellchecker
1802 bool Paragraph::isLetter(Paragraph::size_type pos) const
1804 value_type const c = getChar(pos);
1805 if (IsLetterChar(c))
1807 // '\0' is not a letter, allthough every string contains "" (below)
1810 // We want to pass the ' and escape chars to ispell
1811 string const extra = lyxrc.isp_esc_chars + '\'';
1812 char ch[2] = { c, 0 };
1813 return contains(extra, ch);
1817 bool Paragraph::isWord(size_type pos ) const
1819 return IsWordChar(getChar(pos)) ;
1824 Paragraph::getParLanguage(BufferParams const & bparams) const
1827 return getFirstFontSettings().language();
1829 return previous_->getParLanguage(bparams);
1831 return bparams.language;
1835 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1837 return lyxrc.rtl_support
1838 && getParLanguage(bparams)->RightToLeft();
1842 void Paragraph::changeLanguage(BufferParams const & bparams,
1843 Language const * from, Language const * to)
1845 for (size_type i = 0; i < size(); ++i) {
1846 LyXFont font = getFontSettings(bparams, i);
1847 if (font.language() == from) {
1848 font.setLanguage(to);
1855 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1857 Language const * doc_language = bparams.language;
1858 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1859 cit != pimpl_->fontlist.end(); ++cit)
1860 if ((*cit).font().language() != doc_language)
1866 // Convert the paragraph to a string.
1867 // Used for building the table of contents
1868 string const Paragraph::asString(Buffer const * buffer, bool label)
1870 BufferParams const & bparams = buffer->params;
1872 if (label && !params().labelString().empty())
1873 s += params().labelString() + ' ';
1874 string::size_type const len = s.size();
1876 for (Paragraph::size_type i = 0; i < size(); ++i) {
1877 value_type c = getChar(i);
1880 else if (c == META_INSET &&
1881 getInset(i)->LyxCode() == Inset::MATH_CODE) {
1882 std::ostringstream ost;
1883 getInset(i)->Ascii(buffer, ost);
1884 s += subst(ost.str().c_str(),'\n',' ');
1888 if (isRightToLeftPar(bparams))
1889 reverse(s.begin() + len,s.end());
1895 string const Paragraph::asString(Buffer const * buffer,
1896 Paragraph::size_type beg,
1897 Paragraph::size_type end)
1899 std::ostringstream ost;
1901 if (beg == 0 && !params().labelString().empty())
1902 ost << params().labelString() << ' ';
1904 for (Paragraph::size_type i = beg; i < end; ++i) {
1905 value_type const c = getUChar(buffer->params, i);
1908 else if (c == META_INSET) {
1909 getInset(i)->Ascii(buffer, ost);
1913 return ost.str().c_str();
1917 void Paragraph::setInsetOwner(Inset * i)
1919 pimpl_->inset_owner = i;
1920 for (InsetList::const_iterator cit = insetlist.begin();
1921 cit != insetlist.end(); ++cit) {
1923 (*cit).inset->setOwner(i);
1928 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1931 for (InsetList::const_iterator cit = insetlist.begin();
1932 cit != insetlist.end(); ++cit) {
1934 if ((*cit).inset->IsTextInset()) {
1935 static_cast<UpdatableInset *>
1936 ((*cit).inset)->deleteLyXText(bv, true);
1943 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1946 for (InsetList::const_iterator cit = insetlist.begin();
1947 cit != insetlist.end(); ++cit) {
1949 if ((*cit).inset->IsTextInset()) {
1950 static_cast<UpdatableInset *>
1951 ((*cit).inset)->resizeLyXText(bv, true);
1958 void Paragraph::setContentsFromPar(Paragraph * par)
1960 pimpl_->setContentsFromPar(par);
1964 Paragraph::size_type Paragraph::size() const
1966 return pimpl_->size();
1970 Paragraph::value_type
1971 Paragraph::getChar(Paragraph::size_type pos) const
1973 return pimpl_->getChar(pos);
1977 int Paragraph::id() const
1983 void Paragraph::id(int id_arg)
1985 pimpl_->id_ = id_arg;
1989 LyXTextClass::size_type Paragraph::getLayout() const
1995 bool Paragraph::isFirstInSequence() const
1997 Paragraph const * dhook = depthHook(getDepth());
1998 return (dhook == this
1999 || dhook->getLayout() != getLayout()
2000 || dhook->getDepth() != getDepth());
2004 Inset * Paragraph::InInset()
2006 return pimpl_->inset_owner;
2010 void Paragraph::clearContents()
2016 void Paragraph::setCounter(int i, int v)
2018 pimpl_->counter_[i] = v;
2022 int Paragraph::getCounter(int i) const
2024 return pimpl_->counter_[i];
2028 void Paragraph::incCounter(int i)
2030 pimpl_->counter_[i]++;
2034 void Paragraph::setChar(size_type pos, value_type c)
2036 pimpl_->setChar(pos, c);
2040 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2047 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2049 return inset_iterator(insetlist.begin());
2053 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2055 return inset_iterator(insetlist.end());
2059 ParagraphParameters & Paragraph::params()
2061 return pimpl_->params;
2064 ParagraphParameters const & Paragraph::params() const
2066 return pimpl_->params;