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();
1441 if (main_body > 0) {
1444 basefont = getFont(bparams, -2); // Get label font
1446 basefont = getFont(bparams, -1); // Get layout font
1450 && !pimpl_->size()) {
1451 if (style.isCommand()) {
1457 moving_arg |= style.needprotect;
1459 // Which font is currently active?
1460 LyXFont running_font(basefont);
1461 // Do we have an open font change?
1462 bool open_font = false;
1464 texrow.start(this, 0);
1466 for (size_type i = 0; i < size(); ++i) {
1468 // First char in paragraph or after label?
1469 if (i == main_body) {
1470 if (main_body > 0) {
1472 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1475 basefont = getFont(bparams, -1); // Now use the layout font
1476 running_font = basefont;
1480 if (style.isCommand()) {
1485 if (params().noindent()) {
1486 os << "\\noindent ";
1489 switch (params().align()) {
1490 case LYX_ALIGN_NONE:
1491 case LYX_ALIGN_BLOCK:
1492 case LYX_ALIGN_LAYOUT:
1493 case LYX_ALIGN_SPECIAL:
1495 case LYX_ALIGN_LEFT:
1496 if (getParLanguage(bparams)->babel() != "hebrew") {
1497 os << "\\begin{flushleft}";
1500 os << "\\begin{flushright}";
1504 case LYX_ALIGN_RIGHT:
1505 if (getParLanguage(bparams)->babel() != "hebrew") {
1506 os << "\\begin{flushright}";
1509 os << "\\begin{flushleft}";
1513 case LYX_ALIGN_CENTER:
1514 os << "\\begin{center}";
1520 value_type c = getChar(i);
1522 // Fully instantiated font
1523 LyXFont font = getFont(bparams, i);
1525 LyXFont last_font = running_font;
1527 // Spaces at end of font change are simulated to be
1528 // outside font change, i.e. we write "\textXX{text} "
1529 // rather than "\textXX{text }". (Asger)
1530 if (open_font && c == ' ' && i <= size() - 2
1531 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1532 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1533 font = getFont(bparams, i + 1);
1535 // We end font definition before blanks
1536 if (!font.equalExceptLatex(running_font) && open_font) {
1537 column += running_font.latexWriteEndChanges(os,
1539 (i == main_body-1) ? basefont : font);
1540 running_font = basefont;
1544 // Blanks are printed before start of fontswitch
1546 // Do not print the separation of the optional argument
1547 if (i != main_body - 1) {
1548 pimpl_->simpleTeXBlanks(os, texrow, i,
1549 column, font, style);
1553 // Do we need to change font?
1554 if (!font.equalExceptLatex(running_font)
1555 && i != main_body-1) {
1556 column += font.latexWriteStartChanges(os, basefont,
1558 running_font = font;
1562 if (c == Paragraph::META_NEWLINE) {
1563 // newlines are handled differently here than
1564 // the default in SimpleTeXSpecialChars().
1565 if (!style.newline_allowed
1567 || font.latex() == LyXFont::ON
1573 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1576 basefont = getFont(bparams, -1);
1577 running_font = basefont;
1578 if (font.family() ==
1579 LyXFont::TYPEWRITER_FAMILY) {
1587 texrow.start(this, i + 1);
1590 pimpl_->simpleTeXSpecialChars(buf, bparams,
1591 os, texrow, moving_arg,
1593 basefont, open_font,
1594 style, i, column, c);
1598 // If we have an open font definition, we have to close it
1602 .latexWriteEndChanges(os, basefont,
1603 next_->getFont(bparams,
1606 running_font.latexWriteEndChanges(os, basefont,
1611 // Needed if there is an optional argument but no contents.
1612 if (main_body > 0 && main_body == size()) {
1614 return_value = false;
1617 switch (params().align()) {
1618 case LYX_ALIGN_NONE:
1619 case LYX_ALIGN_BLOCK:
1620 case LYX_ALIGN_LAYOUT:
1621 case LYX_ALIGN_SPECIAL:
1623 case LYX_ALIGN_LEFT:
1624 if (getParLanguage(bparams)->babel() != "hebrew") {
1625 os << "\\end{flushleft}";
1628 os << "\\end{flushright}";
1632 case LYX_ALIGN_RIGHT:
1633 if (getParLanguage(bparams)->babel() != "hebrew") {
1634 os << "\\end{flushright}";
1637 os << "\\end{flushleft}";
1641 case LYX_ALIGN_CENTER:
1642 os << "\\end{center}";
1647 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1648 return return_value;
1652 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1654 bool retval = false;
1656 case Paragraph::META_HFILL:
1657 sgml_string.erase();
1659 case Paragraph::META_NEWLINE:
1663 sgml_string = "&";
1666 sgml_string = "<";
1669 sgml_string = ">";
1672 sgml_string = "$";
1675 sgml_string = "#";
1678 sgml_string = "%";
1681 sgml_string = "[";
1684 sgml_string = "]";
1687 sgml_string = "{";
1690 sgml_string = "}";
1693 sgml_string = "˜";
1696 sgml_string = """;
1699 sgml_string = "\";
1705 case '\0': // Ignore :-)
1706 sgml_string.erase();
1716 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1717 BufferParams const & bparams,
1718 ostream & os, TexRow & texrow)
1720 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1722 LyXLayout const & style =
1723 textclasslist.Style(bparams.textclass,
1726 if (style.isEnvironment()){
1727 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1728 os << "\\begin{" << style.latexname() << "}{"
1729 << params().labelWidthString() << "}\n";
1730 } else if (style.labeltype == LABEL_BIBLIO) {
1732 os << "\\begin{" << style.latexname() << "}{"
1733 << bibitemWidest(buf)
1735 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1736 os << "\\begin{" << style.latexname() << '}'
1737 << style.latexparam() << '\n';
1739 os << "\\begin{" << style.latexname() << '}'
1740 << style.latexparam() << '\n';
1743 Paragraph * par = this;
1745 par = par->TeXOnePar(buf, bparams,
1748 if (par && par->params().depth() > params().depth()) {
1749 if (textclasslist.Style(bparams.textclass,
1750 par->layout).isParagraph()
1752 // How to handle this? (Lgb)
1753 //&& !suffixIs(os, "\n\n")
1755 // There should be at least one '\n' already
1756 // but we need there to be two for Standard
1757 // paragraphs that are depth-increment'ed to be
1758 // output correctly. However, tables can
1759 // also be paragraphs so don't adjust them.
1762 // Will it ever harm to have one '\n' too
1763 // many? i.e. that we sometimes will have
1764 // three in a row. (Lgb)
1768 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1771 && par->layout == layout
1772 && par->params().depth() == params().depth());
1774 if (style.isEnvironment()) {
1775 os << "\\end{" << style.latexname() << "}\n";
1778 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1779 return par; // ale970302
1783 bool Paragraph::isHfill(size_type pos) const
1785 return IsHfillChar(getChar(pos));
1789 bool Paragraph::isInset(size_type pos) const
1791 return IsInsetChar(getChar(pos));
1795 bool Paragraph::isNewline(size_type pos) const
1797 return pos >= 0 && IsNewlineChar(getChar(pos));
1801 bool Paragraph::isSeparator(size_type pos) const
1803 return IsSeparatorChar(getChar(pos));
1807 bool Paragraph::isLineSeparator(size_type pos) const
1809 return IsLineSeparatorChar(getChar(pos));
1813 bool Paragraph::isKomma(size_type pos) const
1815 return IsKommaChar(getChar(pos));
1819 /// Used by the spellchecker
1820 bool Paragraph::isLetter(Paragraph::size_type pos) const
1822 value_type const c = getChar(pos);
1823 if (IsLetterChar(c))
1825 // '\0' is not a letter, allthough every string contains "" (below)
1828 // We want to pass the ' and escape chars to ispell
1829 string const extra = lyxrc.isp_esc_chars + '\'';
1830 char ch[2] = { c, 0 };
1831 return contains(extra, ch);
1835 bool Paragraph::isWord(size_type pos ) const
1837 return IsWordChar(getChar(pos)) ;
1842 Paragraph::getParLanguage(BufferParams const & bparams) const
1845 return getFirstFontSettings().language();
1847 return previous_->getParLanguage(bparams);
1849 return bparams.language;
1853 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1855 return lyxrc.rtl_support
1856 && getParLanguage(bparams)->RightToLeft();
1860 void Paragraph::changeLanguage(BufferParams const & bparams,
1861 Language const * from, Language const * to)
1863 for (size_type i = 0; i < size(); ++i) {
1864 LyXFont font = getFontSettings(bparams, i);
1865 if (font.language() == from) {
1866 font.setLanguage(to);
1873 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1875 Language const * doc_language = bparams.language;
1876 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1877 cit != pimpl_->fontlist.end(); ++cit)
1878 if ((*cit).font().language() != doc_language)
1884 // Convert the paragraph to a string.
1885 // Used for building the table of contents
1886 string const Paragraph::asString(Buffer const * buffer, bool label)
1888 BufferParams const & bparams = buffer->params;
1890 if (label && !params().labelString().empty())
1891 s += params().labelString() + ' ';
1892 string::size_type const len = s.size();
1894 for (Paragraph::size_type i = 0; i < size(); ++i) {
1895 value_type c = getChar(i);
1898 else if (c == META_INSET &&
1899 getInset(i)->LyxCode() == Inset::MATH_CODE) {
1900 std::ostringstream ost;
1901 getInset(i)->Ascii(buffer, ost);
1902 s += subst(ost.str().c_str(),'\n',' ');
1906 if (isRightToLeftPar(bparams))
1907 reverse(s.begin() + len,s.end());
1913 string const Paragraph::asString(Buffer const * buffer,
1914 Paragraph::size_type beg,
1915 Paragraph::size_type end)
1917 std::ostringstream ost;
1919 if (beg == 0 && !params().labelString().empty())
1920 ost << params().labelString() << ' ';
1922 for (Paragraph::size_type i = beg; i < end; ++i) {
1923 value_type const c = getUChar(buffer->params, i);
1926 else if (c == META_INSET) {
1927 getInset(i)->Ascii(buffer, ost);
1931 return ost.str().c_str();
1935 void Paragraph::setInsetOwner(Inset * i)
1937 pimpl_->inset_owner = i;
1938 for (InsetList::const_iterator cit = insetlist.begin();
1939 cit != insetlist.end(); ++cit) {
1941 (*cit).inset->setOwner(i);
1946 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1949 for (InsetList::const_iterator cit = insetlist.begin();
1950 cit != insetlist.end(); ++cit) {
1952 if ((*cit).inset->IsTextInset()) {
1953 static_cast<UpdatableInset *>
1954 ((*cit).inset)->deleteLyXText(bv, true);
1961 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1964 for (InsetList::const_iterator cit = insetlist.begin();
1965 cit != insetlist.end(); ++cit) {
1967 if ((*cit).inset->IsTextInset()) {
1968 static_cast<UpdatableInset *>
1969 ((*cit).inset)->resizeLyXText(bv, true);
1976 void Paragraph::setContentsFromPar(Paragraph * par)
1978 pimpl_->setContentsFromPar(par);
1982 Paragraph::size_type Paragraph::size() const
1984 return pimpl_->size();
1988 Paragraph::value_type
1989 Paragraph::getChar(Paragraph::size_type pos) const
1991 return pimpl_->getChar(pos);
1995 int Paragraph::id() const
2001 void Paragraph::id(int id_arg)
2003 pimpl_->id_ = id_arg;
2007 LyXTextClass::size_type Paragraph::getLayout() const
2013 bool Paragraph::isFirstInSequence() const
2015 Paragraph const * dhook = depthHook(getDepth());
2016 return (dhook == this
2017 || dhook->getLayout() != getLayout()
2018 || dhook->getDepth() != getDepth());
2022 Inset * Paragraph::InInset()
2024 return pimpl_->inset_owner;
2028 void Paragraph::clearContents()
2034 void Paragraph::setCounter(int i, int v)
2036 pimpl_->counter_[i] = v;
2040 int Paragraph::getCounter(int i) const
2042 return pimpl_->counter_[i];
2046 void Paragraph::incCounter(int i)
2048 pimpl_->counter_[i]++;
2052 void Paragraph::setChar(size_type pos, value_type c)
2054 pimpl_->setChar(pos, c);
2058 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2065 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2067 return inset_iterator(insetlist.begin());
2071 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2073 return inset_iterator(insetlist.end());
2077 ParagraphParameters & Paragraph::params()
2079 return pimpl_->params;
2082 ParagraphParameters const & Paragraph::params() const
2084 return pimpl_->params;