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, bool same_ids)
111 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
113 for (int i = 0; i < 10; ++i)
120 // this is because of the dummy layout of the paragraphs that
126 bibkey = static_cast<InsetBibKey *>
127 (lp.bibkey->clone(*current_view->buffer()));
132 // copy everything behind the break-position to the new paragraph
134 insetlist = lp.insetlist;
135 for (InsetList::iterator it = insetlist.begin();
136 it != insetlist.end(); ++it)
138 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
143 // the destructor removes the new paragraph from the list
144 Paragraph::~Paragraph()
147 previous_->next_ = next_;
149 next_->previous_ = previous_;
151 for (InsetList::iterator it = insetlist.begin();
152 it != insetlist.end(); ++it) {
161 //lyxerr << "Paragraph::paragraph_id = "
162 // << Paragraph::paragraph_id << endl;
166 void Paragraph::writeFile(Buffer const * buf, ostream & os,
167 BufferParams const & bparams,
168 depth_type dth) const
170 // The beginning or end of a deeper (i.e. nested) area?
171 if (dth != params().depth()) {
172 if (params().depth() > dth) {
173 while (params().depth() > dth) {
174 os << "\n\\begin_deeper ";
178 while (params().depth() < dth) {
179 os << "\n\\end_deeper ";
185 // First write the layout
187 << textclasslist.NameOfLayout(bparams.textclass, layout)
190 // Maybe some vertical spaces.
191 if (params().spaceTop().kind() != VSpace::NONE)
192 os << "\\added_space_top "
193 << params().spaceTop().asLyXCommand() << " ";
194 if (params().spaceBottom().kind() != VSpace::NONE)
195 os << "\\added_space_bottom "
196 << params().spaceBottom().asLyXCommand() << " ";
198 // Maybe the paragraph has special spacing
199 params().spacing().writeFile(os, true);
201 // The labelwidth string used in lists.
202 if (!params().labelWidthString().empty())
203 os << "\\labelwidthstring "
204 << params().labelWidthString() << '\n';
206 // Lines above or below?
207 if (params().lineTop())
209 if (params().lineBottom())
210 os << "\\line_bottom ";
212 // Pagebreaks above or below?
213 if (params().pagebreakTop())
214 os << "\\pagebreak_top ";
215 if (params().pagebreakBottom())
216 os << "\\pagebreak_bottom ";
218 // Start of appendix?
219 if (params().startOfAppendix())
220 os << "\\start_of_appendix ";
223 if (params().noindent())
227 if (params().align() != LYX_ALIGN_LAYOUT) {
229 switch (params().align()) {
230 case LYX_ALIGN_LEFT: h = 1; break;
231 case LYX_ALIGN_RIGHT: h = 2; break;
232 case LYX_ALIGN_CENTER: h = 3; break;
233 default: h = 0; break;
235 os << "\\align " << string_align[h] << " ";
240 bibkey->write(buf, os);
242 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
245 for (size_type i = 0; i < size(); ++i) {
251 // Write font changes
252 LyXFont font2 = getFontSettings(bparams, i);
253 if (font2 != font1) {
254 font2.lyxWriteChanges(font1, os);
259 value_type const c = getChar(i);
263 Inset const * inset = getInset(i);
265 if (inset->directWrite()) {
266 // international char, let it write
267 // code directly so it's shorter in
269 inset->write(buf, os);
271 os << "\n\\begin_inset ";
272 inset->write(buf, os);
273 os << "\n\\end_inset \n\n";
279 os << "\n\\newline \n";
283 os << "\n\\hfill \n";
287 os << "\n\\backslash \n";
291 if (i + 1 < size() && getChar(i + 1) == ' ') {
298 if ((column > 70 && c == ' ')
303 // this check is to amend a bug. LyX sometimes
304 // inserts '\0' this could cause problems.
308 lyxerr << "ERROR (Paragraph::writeFile):"
309 " NULL char in structure." << endl;
315 // now write the next paragraph
317 next_->writeFile(buf, os, bparams, dth);
321 void Paragraph::validate(LaTeXFeatures & features) const
323 BufferParams const & bparams = features.bufferParams();
326 if (params().lineTop() || params().lineBottom())
327 features.lyxline = true;
328 if (!params().spacing().isDefault())
329 features.setspace = true;
332 features.layout[getLayout()] = true;
335 Language const * doc_language = bparams.language;
337 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
338 cit != pimpl_->fontlist.end(); ++cit) {
339 if ((*cit).font().noun() == LyXFont::ON) {
340 lyxerr[Debug::LATEX] << "font.noun: "
341 << (*cit).font().noun()
343 features.noun = true;
344 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
345 << (*cit).font().stateText(0)
348 switch ((*cit).font().color()) {
350 case LColor::inherit:
354 features.color = true;
355 lyxerr[Debug::LATEX] << "Color enabled. Font: "
356 << (*cit).font().stateText(0)
360 Language const * language = (*cit).font().language();
361 if (language->babel() != doc_language->babel()) {
362 features.UsedLanguages.insert(language);
363 lyxerr[Debug::LATEX] << "Found language "
364 << language->babel() << endl;
369 for (InsetList::const_iterator cit = insetlist.begin();
370 cit != insetlist.end(); ++cit) {
372 (*cit).inset->validate(features);
377 // First few functions needed for cut and paste and paragraph breaking.
378 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
379 Paragraph::size_type pos) const
381 BufferParams bparams = buffer.params;
383 minibuffer_char = getChar(pos);
384 minibuffer_font = getFontSettings(bparams, pos);
385 minibuffer_inset = 0;
386 if (minibuffer_char == Paragraph::META_INSET) {
388 minibuffer_inset = getInset(pos)->clone(buffer);
390 minibuffer_inset = 0;
391 minibuffer_char = ' ';
392 // This reflects what GetInset() does (ARRae)
398 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
399 Paragraph::size_type pos)
401 minibuffer_char = getChar(pos);
402 minibuffer_font = getFontSettings(bparams, pos);
403 minibuffer_inset = 0;
404 if (minibuffer_char == Paragraph::META_INSET) {
406 minibuffer_inset = getInset(pos);
407 // This is a little hack since I want exactly
408 // the inset, not just a clone. Otherwise
409 // the inset would be deleted when calling Erase(pos)
411 InsetTable search_elem(pos, 0);
412 InsetList::iterator it =
413 lower_bound(insetlist.begin(),
415 search_elem, Pimpl::matchIT());
416 if (it != insetlist.end() && (*it).pos == pos)
419 minibuffer_inset = 0;
420 minibuffer_char = ' ';
421 // This reflects what GetInset() does (ARRae)
426 // Erase(pos); now the caller is responsible for that.
430 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
432 if ((minibuffer_char == Paragraph::META_INSET) &&
433 !insertInsetAllowed(minibuffer_inset))
435 if (minibuffer_char == Paragraph::META_INSET)
436 insertInset(pos, minibuffer_inset, minibuffer_font);
438 insertChar(pos, minibuffer_char, minibuffer_font);
446 void Paragraph::clear()
455 void Paragraph::erase(Paragraph::size_type pos)
461 void Paragraph::insertChar(Paragraph::size_type pos,
462 Paragraph::value_type c)
464 LyXFont const f(LyXFont::ALL_INHERIT);
465 insertChar(pos, c, f);
469 void Paragraph::insertChar(Paragraph::size_type pos,
470 Paragraph::value_type c,
471 LyXFont const & font)
473 pimpl_->insertChar(pos, c, font);
477 void Paragraph::insertInset(Paragraph::size_type pos,
480 LyXFont const f(LyXFont::ALL_INHERIT);
481 insertInset(pos, inset, f);
485 void Paragraph::insertInset(Paragraph::size_type pos,
486 Inset * inset, LyXFont const & font)
488 pimpl_->insertInset(pos, inset, font);
492 bool Paragraph::insertInsetAllowed(Inset * inset)
494 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
496 if (pimpl_->inset_owner)
497 return pimpl_->inset_owner->insertInsetAllowed(inset);
502 Inset * Paragraph::getInset(Paragraph::size_type pos)
504 lyx::Assert(pos < size());
507 InsetTable search_inset(pos, 0);
508 InsetList::iterator it = lower_bound(insetlist.begin(),
510 search_inset, Pimpl::matchIT());
511 if (it != insetlist.end() && (*it).pos == pos)
514 lyxerr << "ERROR (Paragraph::GetInset): "
515 "Inset does not exist: " << pos << endl;
518 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
519 // Did this commenting out introduce a bug? So far I have not
520 // see any, please enlighten me. (Lgb)
521 // My guess is that since the inset does not exist, we might
522 // as well replace it with a space to prevent craches. (Asger)
527 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
529 lyx::Assert(pos < size());
532 InsetTable search_inset(pos, 0);
533 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
535 search_inset, Pimpl::matchIT());
536 if (cit != insetlist.end() && (*cit).pos == pos)
539 lyxerr << "ERROR (Paragraph::GetInset): "
540 "Inset does not exist: " << pos << endl;
542 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
543 // Did this commenting out introduce a bug? So far I have not
544 // see any, please enlighten me. (Lgb)
545 // My guess is that since the inset does not exist, we might
546 // as well replace it with a space to prevent craches. (Asger)
551 // Gets uninstantiated font setting at position.
552 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
553 Paragraph::size_type pos) const
555 lyx::Assert(pos <= size());
557 Pimpl::FontTable search_font(pos, LyXFont());
558 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
559 pimpl_->fontlist.end(),
560 search_font, Pimpl::matchFT());
561 if (cit != pimpl_->fontlist.end())
562 return (*cit).font();
564 if (pos == size() && size())
565 return getFontSettings(bparams, pos - 1);
567 return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
571 // Gets uninstantiated font setting at position 0
572 LyXFont const Paragraph::getFirstFontSettings() const
575 if (!pimpl_->fontlist.empty())
576 return pimpl_->fontlist[0].font();
579 return LyXFont(LyXFont::ALL_INHERIT);
583 // Gets the fully instantiated font at a given position in a paragraph
584 // This is basically the same function as LyXText::GetFont() in text2.C.
585 // The difference is that this one is used for generating the LaTeX file,
586 // and thus cosmetic "improvements" are disallowed: This has to deliver
587 // the true picture of the buffer. (Asger)
588 // If position is -1, we get the layout font of the paragraph.
589 // If position is -2, we get the font of the manual label of the paragraph.
590 LyXFont const Paragraph::getFont(BufferParams const & bparams,
591 Paragraph::size_type pos) const
594 LyXLayout const & layout =
595 textclasslist.Style(bparams.textclass,
597 Paragraph::size_type main_body = 0;
598 if (layout.labeltype == LABEL_MANUAL)
599 main_body = beginningOfMainBody();
604 layoutfont = layout.labelfont;
606 layoutfont = layout.font;
607 tmpfont = getFontSettings(bparams, pos);
608 tmpfont.realize(layoutfont);
610 // process layoutfont for pos == -1 and labelfont for pos < -1
612 tmpfont = layout.font;
614 tmpfont = layout.labelfont;
615 tmpfont.setLanguage(getParLanguage(bparams));
618 // check for environment font information
619 char par_depth = getDepth();
620 Paragraph const * par = this;
621 while (par && par->getDepth() && !tmpfont.resolved()) {
622 par = par->outerHook();
624 tmpfont.realize(textclasslist.
625 Style(bparams.textclass,
626 par->getLayout()).font);
627 par_depth = par->getDepth();
631 tmpfont.realize(textclasslist
632 .TextClass(bparams.textclass)
638 /// Returns the height of the highest font in range
640 Paragraph::highestFontInRange(Paragraph::size_type startpos,
641 Paragraph::size_type endpos) const
643 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
644 if (pimpl_->fontlist.empty())
647 Pimpl::FontTable end_search(endpos, LyXFont());
648 Pimpl::FontList::const_iterator end_it = lower_bound(pimpl_->fontlist.begin(),
649 pimpl_->fontlist.end(),
650 end_search, Pimpl::matchFT());
651 if (end_it != pimpl_->fontlist.end())
654 Pimpl::FontTable start_search(startpos, LyXFont());
655 for (Pimpl::FontList::const_iterator cit =
656 lower_bound(pimpl_->fontlist.begin(),
657 pimpl_->fontlist.end(),
658 start_search, Pimpl::matchFT());
659 cit != end_it; ++cit) {
660 LyXFont::FONT_SIZE size = (*cit).font().size();
661 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
668 Paragraph::value_type
669 Paragraph::getUChar(BufferParams const & bparams,
670 Paragraph::size_type pos) const
672 value_type c = getChar(pos);
673 if (!lyxrc.rtl_support)
703 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
710 // return an string of the current word, and the end of the word in lastpos.
711 string const Paragraph::getWord(Paragraph::size_type & lastpos) const
713 lyx::Assert(lastpos >= 0);
715 // the current word is defined as starting at the first character
716 // from the immediate left of lastpospos which meets the definition
717 // of IsLetter(), continuing to the last character to the right
718 // of this meeting IsLetter.
724 // move back until we have a letter
726 //there's no real reason to have firstpos & lastpos as
727 //separate variables as this is written, but maybe someon
728 // will want to return firstpos in the future.
730 //since someone might have typed a punctuation first
731 int firstpos = lastpos;
733 while ((firstpos >= 0) && !isLetter(firstpos))
736 // now find the beginning by looking for a nonletter
738 while ((firstpos>= 0) && isLetter(firstpos))
741 // the above is now pointing to the preceeding non-letter
745 // so copy characters into theword until we get a nonletter
746 // note that this can easily exceed lastpos, wich means
747 // that if used in the middle of a word, the whole word
750 while (isLetter(lastpos)) theword += getChar(lastpos++);
756 void Paragraph::setFont(Paragraph::size_type pos,
757 LyXFont const & font)
759 lyx::Assert(pos <= size());
761 // First, reduce font against layout/label font
762 // Update: The SetCharFont() routine in text2.C already
763 // reduces font, so we don't need to do that here. (Asger)
764 // No need to simplify this because it will disappear
765 // in a new kernel. (Asger)
766 // Next search font table
768 Pimpl::FontTable search_font(pos, LyXFont());
769 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
770 pimpl_->fontlist.end(),
771 search_font, Pimpl::matchFT());
772 unsigned int i = it - pimpl_->fontlist.begin();
773 bool notfound = it == pimpl_->fontlist.end();
775 if (!notfound && pimpl_->fontlist[i].font() == font)
778 bool begin = pos == 0 || notfound ||
779 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
780 // Is position pos is a beginning of a font block?
781 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
782 // Is position pos is the end of a font block?
783 if (begin && end) { // A single char block
784 if (i + 1 < pimpl_->fontlist.size() &&
785 pimpl_->fontlist[i + 1].font() == font) {
786 // Merge the singleton block with the next block
787 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
788 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
789 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
790 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
791 // Merge the singleton block with the previous block
792 pimpl_->fontlist[i - 1].pos(pos);
793 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
795 pimpl_->fontlist[i].font(font);
797 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
798 pimpl_->fontlist[i - 1].pos(pos);
800 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
801 Pimpl::FontTable(pos, font));
803 pimpl_->fontlist[i].pos(pos - 1);
804 if (!(i + 1 < pimpl_->fontlist.size() &&
805 pimpl_->fontlist[i + 1].font() == font))
806 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
807 Pimpl::FontTable(pos, font));
808 } else { // The general case. The block is splitted into 3 blocks
809 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
810 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
811 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
812 Pimpl::FontTable(pos, font));
818 void Paragraph::next(Paragraph * p)
824 // This function is able to hide closed footnotes.
825 Paragraph * Paragraph::next()
831 Paragraph const * Paragraph::next() const
837 void Paragraph::previous(Paragraph * p)
843 // This function is able to hide closed footnotes.
844 Paragraph * Paragraph::previous()
850 // This function is able to hide closed footnotes.
851 Paragraph const * Paragraph::previous() const
857 void Paragraph::breakParagraph(BufferParams const & bparams,
858 Paragraph::size_type pos,
861 // create a new paragraph
862 Paragraph * tmp = new Paragraph(this);
863 // remember to set the inset_owner
864 tmp->setInsetOwner(InInset());
866 // this is an idea for a more userfriendly layout handling, I will
867 // see what the users say
869 // layout stays the same with latex-environments
871 tmp->setOnlyLayout(layout);
872 tmp->setLabelWidthString(params().labelWidthString());
875 if (size() > pos || !size() || flag == 2) {
876 tmp->setOnlyLayout(layout);
877 tmp->params().align(params().align());
878 tmp->setLabelWidthString(params().labelWidthString());
880 tmp->params().lineBottom(params().lineBottom());
881 params().lineBottom(false);
882 tmp->params().pagebreakBottom(params().pagebreakBottom());
883 params().pagebreakBottom(false);
884 tmp->params().spaceBottom(params().spaceBottom());
885 params().spaceBottom(VSpace(VSpace::NONE));
887 tmp->params().depth(params().depth());
888 tmp->params().noindent(params().noindent());
890 // copy everything behind the break-position
891 // to the new paragraph
892 size_type pos_end = pimpl_->size() - 1;
895 for (; i <= pos_end; ++i) {
896 cutIntoMinibuffer(bparams, i);
897 if (tmp->insertFromMinibuffer(j - pos))
900 for (i = pos_end; i >= pos; --i) {
905 // just an idea of me
907 tmp->params().lineTop(params().lineTop());
908 tmp->params().pagebreakTop(params().pagebreakTop());
909 tmp->params().spaceTop(params().spaceTop());
910 tmp->bibkey = bibkey;
912 // layout stays the same with latex-environments
914 setOnlyLayout(tmp->layout);
915 setLabelWidthString(tmp->params().labelWidthString());
916 params().depth(tmp->params().depth());
922 void Paragraph::makeSameLayout(Paragraph const * par)
924 layout = par->layout;
926 params() = par->params();
930 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
932 if (textclasslist.Style(tclass, getLayout()).free_spacing)
937 && (isNewline(0) || isLineSeparator(0))){
946 bool Paragraph::hasSameLayout(Paragraph const * par) const
949 par->layout == layout &&
950 params().sameLayout(par->params());
954 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
955 Paragraph::size_type pos)
957 // create a new paragraph
958 Paragraph * tmp = new Paragraph(this);
959 tmp->makeSameLayout(this);
961 // When can pos > Last()?
962 // I guess pos == Last() is possible.
964 // copy everything behind the break-position to the new
966 size_type pos_end = pimpl_->size() - 1;
970 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
971 cutIntoMinibuffer(bparams, i);
972 if (tmp->insertFromMinibuffer(j - pos))
976 for (size_type k = pos_end; k >= pos; --k) {
983 // Be carefull, this does not make any check at all.
984 // This method has wrong name, it combined this par with the next par.
985 // In that sense it is the reverse of break paragraph. (Lgb)
986 void Paragraph::pasteParagraph(BufferParams const & bparams)
988 // copy the next paragraph to this one
989 Paragraph * the_next = next();
991 // first the DTP-stuff
992 params().lineBottom(the_next->params().lineBottom());
993 params().spaceBottom(the_next->params().spaceBottom());
994 params().pagebreakBottom(the_next->params().pagebreakBottom());
996 size_type pos_end = the_next->pimpl_->size() - 1;
997 size_type pos_insert = size();
999 // ok, now copy the paragraph
1000 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
1001 the_next->cutIntoMinibuffer(bparams, i);
1002 if (insertFromMinibuffer(pos_insert + j))
1006 // delete the next paragraph
1007 Paragraph * ppar = the_next->previous_;
1008 Paragraph * npar = the_next->next_;
1014 int Paragraph::getEndLabel(BufferParams const & bparams) const
1016 Paragraph const * par = this;
1017 depth_type par_depth = getDepth();
1019 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1020 int const endlabeltype =
1021 textclasslist.Style(bparams.textclass,
1022 layout).endlabeltype;
1023 if (endlabeltype != END_LABEL_NO_LABEL) {
1025 return endlabeltype;
1027 depth_type const next_depth = next_->getDepth();
1028 if (par_depth > next_depth ||
1029 (par_depth == next_depth
1030 && layout != next_->getLayout()))
1031 return endlabeltype;
1036 par = par->outerHook();
1038 par_depth = par->getDepth();
1040 return END_LABEL_NO_LABEL;
1044 Paragraph::depth_type Paragraph::getDepth() const
1046 return params().depth();
1050 char Paragraph::getAlign() const
1052 return params().align();
1056 string const & Paragraph::getLabelstring() const
1058 return params().labelString();
1062 int Paragraph::getFirstCounter(int i) const
1064 return pimpl_->counter_[i];
1068 // the next two functions are for the manual labels
1069 string const Paragraph::getLabelWidthString() const
1071 if (!params().labelWidthString().empty())
1072 return params().labelWidthString();
1074 return _("Senseless with this layout!");
1078 void Paragraph::setLabelWidthString(string const & s)
1080 params().labelWidthString(s);
1084 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1086 layout = new_layout;
1090 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1092 layout = new_layout;
1093 params().labelWidthString(string());
1094 params().align(LYX_ALIGN_LAYOUT);
1095 params().spaceTop(VSpace(VSpace::NONE));
1096 params().spaceBottom(VSpace(VSpace::NONE));
1097 params().spacing(Spacing(Spacing::Default));
1101 // if the layout of a paragraph contains a manual label, the beginning of the
1102 // main body is the beginning of the second word. This is what the par-
1103 // function returns. If the layout does not contain a label, the main
1104 // body always starts with position 0. This differentiation is necessary,
1105 // because there cannot be a newline or a blank <= the beginning of the
1106 // main body in TeX.
1108 int Paragraph::beginningOfMainBody() const
1110 // Unroll the first two cycles of the loop
1111 // and remember the previous character to
1112 // remove unnecessary GetChar() calls
1115 && getChar(i) != Paragraph::META_NEWLINE) {
1117 char previous_char = 0;
1120 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1121 // Yes, this ^ is supposed to be "= " not "=="
1124 && previous_char != ' '
1125 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1127 previous_char = temp;
1136 Paragraph * Paragraph::depthHook(depth_type depth)
1138 Paragraph * newpar = this;
1141 newpar = newpar->previous();
1142 } while (newpar && newpar->getDepth() > depth);
1145 if (previous() || getDepth())
1146 lyxerr << "ERROR (Paragraph::DepthHook): "
1155 Paragraph const * Paragraph::depthHook(depth_type depth) const
1157 Paragraph const * newpar = this;
1160 newpar = newpar->previous();
1161 } while (newpar && newpar->getDepth() > depth);
1164 if (previous() || getDepth())
1165 lyxerr << "ERROR (Paragraph::DepthHook): "
1173 Paragraph * Paragraph::outerHook()
1177 return depthHook(depth_type(getDepth() - 1));
1180 Paragraph const * Paragraph::outerHook() const
1184 return depthHook(depth_type(getDepth() - 1));
1187 int Paragraph::autoDeleteInsets()
1190 InsetList::size_type index = 0;
1191 while (index < insetlist.size()) {
1192 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1193 erase(insetlist[index].pos);
1194 // Erase() calls to insetlist.erase(&insetlist[index])
1195 // so index shouldn't be increased.
1204 Paragraph::inset_iterator
1205 Paragraph::InsetIterator(Paragraph::size_type pos)
1207 InsetTable search_inset(pos, 0);
1208 InsetList::iterator it = lower_bound(insetlist.begin(),
1210 search_inset, Pimpl::matchIT());
1211 return inset_iterator(it);
1215 // returns -1 if inset not found
1216 int Paragraph::getPositionOfInset(Inset * inset) const
1219 for (InsetList::const_iterator cit = insetlist.begin();
1220 cit != insetlist.end(); ++cit) {
1221 if ((*cit).inset == inset) {
1225 if (inset == bibkey)
1232 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1233 BufferParams const & bparams,
1234 ostream & os, TexRow & texrow,
1237 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1238 LyXLayout const & style =
1239 textclasslist.Style(bparams.textclass,
1242 bool further_blank_line = false;
1244 if (params().startOfAppendix()) {
1245 os << "\\appendix\n";
1249 if (!params().spacing().isDefault()
1250 && (!previous() || !previous()->hasSameLayout(this))) {
1251 os << params().spacing().writeEnvirBegin() << "\n";
1255 if (tex_code_break_column && style.isCommand()){
1260 if (params().pagebreakTop()) {
1262 further_blank_line = true;
1264 if (params().spaceTop().kind() != VSpace::NONE) {
1265 os << params().spaceTop().asLatexCommand(bparams);
1266 further_blank_line = true;
1269 if (params().lineTop()) {
1270 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1271 << "\\vspace{-1\\parskip}";
1272 further_blank_line = true;
1275 if (further_blank_line){
1280 Language const * language = getParLanguage(bparams);
1281 Language const * doc_language = bparams.language;
1282 Language const * previous_language = previous_
1283 ? previous_->getParLanguage(bparams) : doc_language;
1284 if (language->babel() != doc_language->babel() &&
1285 language->babel() != previous_language->babel()) {
1286 os << subst(lyxrc.language_command_begin, "$$lang",
1292 if (bparams.inputenc == "auto" &&
1293 language->encoding() != previous_language->encoding()) {
1294 os << "\\inputencoding{"
1295 << language->encoding()->LatexName()
1300 switch (style.latextype) {
1303 << style.latexname()
1304 << style.latexparam();
1306 case LATEX_ITEM_ENVIRONMENT:
1308 bibkey->latex(buf, os, false, false);
1312 case LATEX_LIST_ENVIRONMENT:
1319 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1321 // Make sure that \\par is done with the font of the last
1322 // character if this has another size as the default.
1323 // This is necessary because LaTeX (and LyX on the screen)
1324 // calculates the space between the baselines according
1325 // to this font. (Matthias)
1327 // Is this really needed ? (Dekel)
1328 // We do not need to use to change the font for the last paragraph
1329 // or for a command.
1330 LyXFont font = getFont(bparams, size() - 1);
1332 bool is_command = textclasslist.Style(bparams.textclass,
1333 getLayout()).isCommand();
1334 if (style.resfont.size() != font.size() && next_ && !is_command) {
1337 os << "\\" << font.latexSize() << " \\par}";
1338 } else if (need_par) {
1340 } else if (is_command)
1343 if (language->babel() != doc_language->babel() &&
1345 || next_->getParLanguage(bparams)->babel() != language->babel())) {
1347 << subst(lyxrc.language_command_end, "$$lang",
1348 doc_language->babel());
1351 switch (style.latextype) {
1352 case LATEX_ITEM_ENVIRONMENT:
1353 case LATEX_LIST_ENVIRONMENT:
1354 if (next_ && (params().depth() < next_->params().depth())) {
1359 case LATEX_ENVIRONMENT:
1360 // if its the last paragraph of the current environment
1361 // skip it otherwise fall through
1363 && (next_->layout != layout
1364 || next_->params().depth() != params().depth()))
1367 // we don't need it for the last paragraph!!!
1368 // or for tables in floats
1369 // -- effectively creates a \par where there isn't one which
1370 // breaks a \subfigure or \subtable.
1377 further_blank_line = false;
1378 if (params().lineBottom()) {
1379 os << "\\lyxline{\\" << getFont(bparams,
1380 size() - 1).latexSize() << '}';
1381 further_blank_line = true;
1384 if (params().spaceBottom().kind() != VSpace::NONE) {
1385 os << params().spaceBottom().asLatexCommand(bparams);
1386 further_blank_line = true;
1389 if (params().pagebreakBottom()) {
1391 further_blank_line = true;
1394 if (further_blank_line){
1399 if (!params().spacing().isDefault()
1400 && (!next_ || !next_->hasSameLayout(this))) {
1401 os << params().spacing().writeEnvirEnd() << "\n";
1405 // we don't need it for the last paragraph!!!
1411 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1416 // This one spits out the text of the paragraph
1417 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1418 BufferParams const & bparams,
1419 ostream & os, TexRow & texrow,
1422 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1424 bool return_value = false;
1426 LyXLayout const & style =
1427 textclasslist.Style(bparams.textclass,
1431 // Maybe we have to create a optional argument.
1432 size_type main_body;
1433 if (style.labeltype != LABEL_MANUAL)
1436 main_body = beginningOfMainBody();
1440 if (main_body > 0) {
1443 basefont = getFont(bparams, -2); // Get label font
1445 basefont = getFont(bparams, -1); // Get layout font
1449 && !pimpl_->size()) {
1450 if (style.isCommand()) {
1456 moving_arg |= style.needprotect;
1458 // Which font is currently active?
1459 LyXFont running_font(basefont);
1460 // Do we have an open font change?
1461 bool open_font = false;
1463 texrow.start(this, 0);
1465 for (size_type i = 0; i < size(); ++i) {
1467 // First char in paragraph or after label?
1468 if (i == main_body) {
1469 if (main_body > 0) {
1471 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1474 basefont = getFont(bparams, -1); // Now use the layout font
1475 running_font = basefont;
1479 if (style.isCommand()) {
1484 if (params().noindent()) {
1485 os << "\\noindent ";
1488 switch (params().align()) {
1489 case LYX_ALIGN_NONE:
1490 case LYX_ALIGN_BLOCK:
1491 case LYX_ALIGN_LAYOUT:
1492 case LYX_ALIGN_SPECIAL:
1494 case LYX_ALIGN_LEFT:
1495 if (getParLanguage(bparams)->babel() != "hebrew") {
1496 os << "\\begin{flushleft}";
1499 os << "\\begin{flushright}";
1503 case LYX_ALIGN_RIGHT:
1504 if (getParLanguage(bparams)->babel() != "hebrew") {
1505 os << "\\begin{flushright}";
1508 os << "\\begin{flushleft}";
1512 case LYX_ALIGN_CENTER:
1513 os << "\\begin{center}";
1519 value_type c = getChar(i);
1521 // Fully instantiated font
1522 LyXFont font = getFont(bparams, i);
1524 LyXFont last_font = running_font;
1526 // Spaces at end of font change are simulated to be
1527 // outside font change, i.e. we write "\textXX{text} "
1528 // rather than "\textXX{text }". (Asger)
1529 if (open_font && c == ' ' && i <= size() - 2
1530 && !getFont(bparams, i + 1).equalExceptLatex(running_font)
1531 && !getFont(bparams, i + 1).equalExceptLatex(font)) {
1532 font = getFont(bparams, i + 1);
1534 // We end font definition before blanks
1535 if (!font.equalExceptLatex(running_font) && open_font) {
1536 column += running_font.latexWriteEndChanges(os,
1538 (i == main_body-1) ? basefont : font);
1539 running_font = basefont;
1543 // Blanks are printed before start of fontswitch
1545 // Do not print the separation of the optional argument
1546 if (i != main_body - 1) {
1547 pimpl_->simpleTeXBlanks(os, texrow, i,
1548 column, font, style);
1552 // Do we need to change font?
1553 if (!font.equalExceptLatex(running_font)
1554 && i != main_body-1) {
1555 column += font.latexWriteStartChanges(os, basefont,
1557 running_font = font;
1561 if (c == Paragraph::META_NEWLINE) {
1562 // newlines are handled differently here than
1563 // the default in SimpleTeXSpecialChars().
1564 if (!style.newline_allowed
1566 || font.latex() == LyXFont::ON
1572 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1575 basefont = getFont(bparams, -1);
1576 running_font = basefont;
1577 if (font.family() ==
1578 LyXFont::TYPEWRITER_FAMILY) {
1586 texrow.start(this, i + 1);
1589 pimpl_->simpleTeXSpecialChars(buf, bparams,
1590 os, texrow, moving_arg,
1592 basefont, open_font,
1593 style, i, column, c);
1597 // If we have an open font definition, we have to close it
1601 .latexWriteEndChanges(os, basefont,
1602 next_->getFont(bparams,
1605 running_font.latexWriteEndChanges(os, basefont,
1610 // Needed if there is an optional argument but no contents.
1611 if (main_body > 0 && main_body == size()) {
1613 return_value = false;
1616 switch (params().align()) {
1617 case LYX_ALIGN_NONE:
1618 case LYX_ALIGN_BLOCK:
1619 case LYX_ALIGN_LAYOUT:
1620 case LYX_ALIGN_SPECIAL:
1622 case LYX_ALIGN_LEFT:
1623 if (getParLanguage(bparams)->babel() != "hebrew") {
1624 os << "\\end{flushleft}";
1627 os << "\\end{flushright}";
1631 case LYX_ALIGN_RIGHT:
1632 if (getParLanguage(bparams)->babel() != "hebrew") {
1633 os << "\\end{flushright}";
1636 os << "\\end{flushleft}";
1640 case LYX_ALIGN_CENTER:
1641 os << "\\end{center}";
1646 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1647 return return_value;
1651 bool Paragraph::linuxDocConvertChar(char c, string & sgml_string)
1653 bool retval = false;
1655 case Paragraph::META_HFILL:
1656 sgml_string.erase();
1658 case Paragraph::META_NEWLINE:
1662 sgml_string = "&";
1665 sgml_string = "<";
1668 sgml_string = ">";
1671 sgml_string = "$";
1674 sgml_string = "#";
1677 sgml_string = "%";
1680 sgml_string = "[";
1683 sgml_string = "]";
1686 sgml_string = "{";
1689 sgml_string = "}";
1692 sgml_string = "˜";
1695 sgml_string = """;
1698 sgml_string = "\";
1704 case '\0': // Ignore :-)
1705 sgml_string.erase();
1715 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1716 BufferParams const & bparams,
1717 ostream & os, TexRow & texrow)
1719 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1721 LyXLayout const & style =
1722 textclasslist.Style(bparams.textclass,
1725 if (style.isEnvironment()){
1726 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1727 os << "\\begin{" << style.latexname() << "}{"
1728 << params().labelWidthString() << "}\n";
1729 } else if (style.labeltype == LABEL_BIBLIO) {
1731 os << "\\begin{" << style.latexname() << "}{"
1732 << bibitemWidest(buf)
1734 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1735 os << "\\begin{" << style.latexname() << '}'
1736 << style.latexparam() << '\n';
1738 os << "\\begin{" << style.latexname() << '}'
1739 << style.latexparam() << '\n';
1742 Paragraph * par = this;
1744 par = par->TeXOnePar(buf, bparams,
1747 if (par && par->params().depth() > params().depth()) {
1748 if (textclasslist.Style(bparams.textclass,
1749 par->layout).isParagraph()
1751 // How to handle this? (Lgb)
1752 //&& !suffixIs(os, "\n\n")
1754 // There should be at least one '\n' already
1755 // but we need there to be two for Standard
1756 // paragraphs that are depth-increment'ed to be
1757 // output correctly. However, tables can
1758 // also be paragraphs so don't adjust them.
1761 // Will it ever harm to have one '\n' too
1762 // many? i.e. that we sometimes will have
1763 // three in a row. (Lgb)
1767 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1770 && par->layout == layout
1771 && par->params().depth() == params().depth());
1773 if (style.isEnvironment()) {
1774 os << "\\end{" << style.latexname() << "}\n";
1777 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1778 return par; // ale970302
1782 bool Paragraph::isHfill(size_type pos) const
1784 return IsHfillChar(getChar(pos));
1788 bool Paragraph::isInset(size_type pos) const
1790 return IsInsetChar(getChar(pos));
1794 bool Paragraph::isNewline(size_type pos) const
1796 return pos >= 0 && IsNewlineChar(getChar(pos));
1800 bool Paragraph::isSeparator(size_type pos) const
1802 return IsSeparatorChar(getChar(pos));
1806 bool Paragraph::isLineSeparator(size_type pos) const
1808 return IsLineSeparatorChar(getChar(pos));
1812 bool Paragraph::isKomma(size_type pos) const
1814 return IsKommaChar(getChar(pos));
1818 /// Used by the spellchecker
1819 bool Paragraph::isLetter(Paragraph::size_type pos) const
1821 value_type const c = getChar(pos);
1822 if (IsLetterChar(c))
1824 // '\0' is not a letter, allthough every string contains "" (below)
1827 // We want to pass the ' and escape chars to ispell
1828 string const extra = lyxrc.isp_esc_chars + '\'';
1829 char ch[2] = { c, 0 };
1830 return contains(extra, ch);
1834 bool Paragraph::isWord(size_type pos ) const
1836 return IsWordChar(getChar(pos)) ;
1841 Paragraph::getParLanguage(BufferParams const & bparams) const
1844 return getFirstFontSettings().language();
1846 return previous_->getParLanguage(bparams);
1848 return bparams.language;
1852 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1854 return lyxrc.rtl_support
1855 && getParLanguage(bparams)->RightToLeft();
1859 void Paragraph::changeLanguage(BufferParams const & bparams,
1860 Language const * from, Language const * to)
1862 for (size_type i = 0; i < size(); ++i) {
1863 LyXFont font = getFontSettings(bparams, i);
1864 if (font.language() == from) {
1865 font.setLanguage(to);
1872 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1874 Language const * doc_language = bparams.language;
1875 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1876 cit != pimpl_->fontlist.end(); ++cit)
1877 if ((*cit).font().language() != doc_language)
1883 // Convert the paragraph to a string.
1884 // Used for building the table of contents
1885 string const Paragraph::asString(Buffer const * buffer, bool label)
1887 BufferParams const & bparams = buffer->params;
1889 if (label && !params().labelString().empty())
1890 s += params().labelString() + ' ';
1891 string::size_type const len = s.size();
1893 for (Paragraph::size_type i = 0; i < size(); ++i) {
1894 value_type c = getChar(i);
1897 else if (c == META_INSET &&
1898 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1899 std::ostringstream ost;
1900 getInset(i)->ascii(buffer, ost);
1901 s += subst(ost.str().c_str(),'\n',' ');
1905 if (isRightToLeftPar(bparams))
1906 reverse(s.begin() + len,s.end());
1912 string const Paragraph::asString(Buffer const * buffer,
1913 Paragraph::size_type beg,
1914 Paragraph::size_type end)
1916 std::ostringstream ost;
1918 if (beg == 0 && !params().labelString().empty())
1919 ost << params().labelString() << ' ';
1921 for (Paragraph::size_type i = beg; i < end; ++i) {
1922 value_type const c = getUChar(buffer->params, i);
1925 else if (c == META_INSET) {
1926 getInset(i)->ascii(buffer, ost);
1930 return ost.str().c_str();
1934 void Paragraph::setInsetOwner(Inset * i)
1936 pimpl_->inset_owner = i;
1937 for (InsetList::const_iterator cit = insetlist.begin();
1938 cit != insetlist.end(); ++cit) {
1940 (*cit).inset->setOwner(i);
1945 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1948 for (InsetList::const_iterator cit = insetlist.begin();
1949 cit != insetlist.end(); ++cit) {
1951 if ((*cit).inset->isTextInset()) {
1952 static_cast<UpdatableInset *>
1953 ((*cit).inset)->deleteLyXText(bv, true);
1960 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1963 for (InsetList::const_iterator cit = insetlist.begin();
1964 cit != insetlist.end(); ++cit) {
1966 if ((*cit).inset->isTextInset()) {
1967 static_cast<UpdatableInset *>
1968 ((*cit).inset)->resizeLyXText(bv, true);
1975 void Paragraph::setContentsFromPar(Paragraph * par)
1977 pimpl_->setContentsFromPar(par);
1981 Paragraph::size_type Paragraph::size() const
1983 return pimpl_->size();
1987 Paragraph::value_type
1988 Paragraph::getChar(Paragraph::size_type pos) const
1990 return pimpl_->getChar(pos);
1994 int Paragraph::id() const
2000 void Paragraph::id(int id_arg)
2002 pimpl_->id_ = id_arg;
2006 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2012 bool Paragraph::isFirstInSequence() const
2014 Paragraph const * dhook = depthHook(getDepth());
2015 return (dhook == this
2016 || dhook->getLayout() != getLayout()
2017 || dhook->getDepth() != getDepth());
2021 Inset * Paragraph::InInset() const
2023 return pimpl_->inset_owner;
2027 void Paragraph::clearContents()
2033 void Paragraph::setCounter(int i, int v)
2035 pimpl_->counter_[i] = v;
2039 int Paragraph::getCounter(int i) const
2041 return pimpl_->counter_[i];
2045 void Paragraph::incCounter(int i)
2047 pimpl_->counter_[i]++;
2051 void Paragraph::setChar(size_type pos, value_type c)
2053 pimpl_->setChar(pos, c);
2057 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2064 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2066 return inset_iterator(insetlist.begin());
2070 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2072 return inset_iterator(insetlist.end());
2076 ParagraphParameters & Paragraph::params()
2078 return pimpl_->params;
2081 ParagraphParameters const & Paragraph::params() const
2083 return pimpl_->params;
2086 Paragraph * Paragraph::getParFromID(int id) const
2088 return pimpl_->getParFromID(id);