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 "support/lstrings.h"
39 #include "lyx_gui_misc.h"
41 #include "support/lyxmanip.h"
42 #include "BufferView.h"
44 #include "ParameterStruct.h"
51 using std::lower_bound;
52 using std::upper_bound;
55 int tex_code_break_column = 72; // needs non-zero initialization. set later.
56 // this is a bad idea, but how can Paragraph find its buffer to get
57 // parameters? (JMarc)
59 extern string bibitemWidest(Buffer const *);
61 // this is a minibuffer
66 LyXFont minibuffer_font;
67 Inset * minibuffer_inset;
72 extern BufferView * current_view;
75 Paragraph::Paragraph()
76 : pimpl_(new Paragraph::Pimpl(this))
78 for (int i = 0; i < 10; ++i) setCounter(i , 0);
83 bibkey = 0; // ale970302
88 // This konstruktor inserts the new paragraph in a list.
89 Paragraph::Paragraph(Paragraph * par)
90 : pimpl_(new Paragraph::Pimpl(this))
92 for (int i = 0; i < 10; ++i)
97 // double linked list begin
100 next_->previous_ = this;
102 previous_->next_ = this;
105 bibkey = 0; // ale970302
111 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
112 : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
114 for (int i = 0; i < 10; ++i)
121 // this is because of the dummy layout of the paragraphs that
127 bibkey = static_cast<InsetBibKey *>
128 (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)
139 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
144 // the destructor removes the new paragraph from the list
145 Paragraph::~Paragraph()
148 previous_->next_ = next_;
150 next_->previous_ = previous_;
152 for (InsetList::iterator it = insetlist.begin();
153 it != insetlist.end(); ++it) {
162 //lyxerr << "Paragraph::paragraph_id = "
163 // << Paragraph::paragraph_id << endl;
167 void Paragraph::writeFile(Buffer const * buf, ostream & os,
168 BufferParams const & bparams,
169 depth_type dth) const
171 // The beginning or end of a deeper (i.e. nested) area?
172 if (dth != params().depth()) {
173 if (params().depth() > dth) {
174 while (params().depth() > dth) {
175 os << "\n\\begin_deeper ";
179 while (params().depth() < dth) {
180 os << "\n\\end_deeper ";
186 // First write the layout
188 << textclasslist.NameOfLayout(bparams.textclass, layout)
191 // Maybe some vertical spaces.
192 if (params().spaceTop().kind() != VSpace::NONE)
193 os << "\\added_space_top "
194 << params().spaceTop().asLyXCommand() << " ";
195 if (params().spaceBottom().kind() != VSpace::NONE)
196 os << "\\added_space_bottom "
197 << params().spaceBottom().asLyXCommand() << " ";
199 // Maybe the paragraph has special spacing
200 params().spacing().writeFile(os, true);
202 // The labelwidth string used in lists.
203 if (!params().labelWidthString().empty())
204 os << "\\labelwidthstring "
205 << params().labelWidthString() << '\n';
207 // Lines above or below?
208 if (params().lineTop())
210 if (params().lineBottom())
211 os << "\\line_bottom ";
213 // Pagebreaks above or below?
214 if (params().pagebreakTop())
215 os << "\\pagebreak_top ";
216 if (params().pagebreakBottom())
217 os << "\\pagebreak_bottom ";
219 // Start of appendix?
220 if (params().startOfAppendix())
221 os << "\\start_of_appendix ";
224 if (params().noindent())
228 if (params().align() != LYX_ALIGN_LAYOUT) {
230 switch (params().align()) {
231 case LYX_ALIGN_LEFT: h = 1; break;
232 case LYX_ALIGN_RIGHT: h = 2; break;
233 case LYX_ALIGN_CENTER: h = 3; break;
234 default: h = 0; break;
236 os << "\\align " << string_align[h] << " ";
241 bibkey->write(buf, os);
243 LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
246 for (size_type i = 0; i < size(); ++i) {
252 // Write font changes
253 LyXFont font2 = getFontSettings(bparams, i);
254 if (font2 != font1) {
255 #ifndef INHERIT_LANGUAGE
256 font2.lyxWriteChanges(font1, os);
258 font2.lyxWriteChanges(font1, bparams.language, os);
264 value_type const c = getChar(i);
268 Inset const * inset = getInset(i);
270 if (inset->directWrite()) {
271 // international char, let it write
272 // code directly so it's shorter in
274 inset->write(buf, os);
276 os << "\n\\begin_inset ";
277 inset->write(buf, os);
278 os << "\n\\end_inset \n\n";
284 os << "\n\\newline \n";
288 os << "\n\\hfill \n";
292 os << "\n\\backslash \n";
296 if (i + 1 < size() && getChar(i + 1) == ' ') {
303 if ((column > 70 && c == ' ')
308 // this check is to amend a bug. LyX sometimes
309 // inserts '\0' this could cause problems.
313 lyxerr << "ERROR (Paragraph::writeFile):"
314 " NULL char in structure." << endl;
320 // now write the next paragraph
322 next_->writeFile(buf, os, bparams, dth);
326 void Paragraph::validate(LaTeXFeatures & features) const
328 BufferParams const & bparams = features.bufferParams();
331 if (params().lineTop() || params().lineBottom())
332 features.require("lyxline");
333 if (!params().spacing().isDefault())
334 features.require("setspace");
337 features.useLayout(getLayout());
340 Language const * doc_language = bparams.language;
342 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
343 cit != pimpl_->fontlist.end(); ++cit) {
344 if (cit->font().noun() == LyXFont::ON) {
345 lyxerr[Debug::LATEX] << "font.noun: "
346 << cit->font().noun()
348 features.require("noun");
349 lyxerr[Debug::LATEX] << "Noun enabled. Font: "
350 << cit->font().stateText(0)
353 switch (cit->font().color()) {
355 case LColor::inherit:
357 // probably we should put here all interface colors used for
358 // font displaying! For now I just add this ones I know of (Jug)
363 features.require("color");
364 lyxerr[Debug::LATEX] << "Color enabled. Font: "
365 << cit->font().stateText(0)
369 Language const * language = cit->font().language();
370 if (language->babel() != doc_language->babel() &&
371 language != ignore_language &&
372 #ifdef INHERIT_LANGUAGE
373 language != inherit_language &&
375 language != latex_language)
377 features.useLanguage(language);
378 lyxerr[Debug::LATEX] << "Found language "
379 << language->babel() << endl;
384 LyXLayout const & layout =
385 textclasslist.Style(bparams.textclass,
388 for (InsetList::const_iterator cit = insetlist.begin();
389 cit != insetlist.end(); ++cit) {
391 cit->inset->validate(features);
392 if (layout.needprotect &&
393 cit->inset->lyxCode() == Inset::FOOT_CODE)
394 features.require("NeedLyXFootnoteCode");
400 // First few functions needed for cut and paste and paragraph breaking.
401 void Paragraph::copyIntoMinibuffer(Buffer const & buffer,
402 Paragraph::size_type pos) const
404 BufferParams bparams = buffer.params;
406 minibuffer_char = getChar(pos);
407 minibuffer_font = getFontSettings(bparams, pos);
408 minibuffer_inset = 0;
409 if (minibuffer_char == Paragraph::META_INSET) {
411 minibuffer_inset = getInset(pos)->clone(buffer);
413 minibuffer_inset = 0;
414 minibuffer_char = ' ';
415 // This reflects what GetInset() does (ARRae)
421 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams,
422 Paragraph::size_type pos)
424 minibuffer_char = getChar(pos);
425 minibuffer_font = getFontSettings(bparams, pos);
426 minibuffer_inset = 0;
427 if (minibuffer_char == Paragraph::META_INSET) {
429 minibuffer_inset = getInset(pos);
430 // This is a little hack since I want exactly
431 // the inset, not just a clone. Otherwise
432 // the inset would be deleted when calling Erase(pos)
434 InsetTable search_elem(pos, 0);
435 InsetList::iterator it =
436 lower_bound(insetlist.begin(),
438 search_elem, Pimpl::matchIT());
439 if (it != insetlist.end() && it->pos == pos)
442 minibuffer_inset = 0;
443 minibuffer_char = ' ';
444 // This reflects what GetInset() does (ARRae)
449 // Erase(pos); now the caller is responsible for that.
453 bool Paragraph::insertFromMinibuffer(Paragraph::size_type pos)
455 if (minibuffer_char == Paragraph::META_INSET) {
456 if (!insetAllowed(minibuffer_inset->lyxCode())) {
459 insertInset(pos, minibuffer_inset, minibuffer_font);
461 LyXFont f = minibuffer_font;
462 if (!checkInsertChar(f)) {
465 insertChar(pos, minibuffer_char, f);
474 void Paragraph::clear()
483 void Paragraph::erase(Paragraph::size_type pos)
489 bool Paragraph::checkInsertChar(LyXFont & font)
491 if (pimpl_->inset_owner)
492 return pimpl_->inset_owner->checkInsertChar(font);
497 void Paragraph::insertChar(Paragraph::size_type pos, Paragraph::value_type c)
499 LyXFont const f(LyXFont::ALL_INHERIT);
500 insertChar(pos, c, f);
504 void Paragraph::insertChar(Paragraph::size_type pos, Paragraph::value_type c,
505 LyXFont const & font)
507 pimpl_->insertChar(pos, c, font);
511 void Paragraph::insertInset(Paragraph::size_type pos, Inset * inset)
513 LyXFont const f(LyXFont::ALL_INHERIT);
514 insertInset(pos, inset, f);
518 void Paragraph::insertInset(Paragraph::size_type pos, Inset * inset,
519 LyXFont const & font)
521 pimpl_->insertInset(pos, inset, font);
525 bool Paragraph::insetAllowed(Inset::Code code)
527 //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
529 if (pimpl_->inset_owner)
530 return pimpl_->inset_owner->insetAllowed(code);
535 Inset * Paragraph::getInset(Paragraph::size_type pos)
537 lyx::Assert(pos < size());
540 InsetTable search_inset(pos, 0);
541 InsetList::iterator it = lower_bound(insetlist.begin(),
543 search_inset, Pimpl::matchIT());
544 if (it != insetlist.end() && it->pos == pos)
547 lyxerr << "ERROR (Paragraph::getInset): "
548 "Inset does not exist: " << pos << endl;
551 // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
552 // Did this commenting out introduce a bug? So far I have not
553 // see any, please enlighten me. (Lgb)
554 // My guess is that since the inset does not exist, we might
555 // as well replace it with a space to prevent craches. (Asger)
560 Inset const * Paragraph::getInset(Paragraph::size_type pos) const
562 lyx::Assert(pos < size());
565 InsetTable search_inset(pos, 0);
566 InsetList::const_iterator cit = lower_bound(insetlist.begin(),
568 search_inset, Pimpl::matchIT());
569 if (cit != insetlist.end() && cit->pos == pos)
572 lyxerr << "ERROR (Paragraph::GetInset): "
573 "Inset does not exist: " << pos << endl;
575 //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
576 // Did this commenting out introduce a bug? So far I have not
577 // see any, please enlighten me. (Lgb)
578 // My guess is that since the inset does not exist, we might
579 // as well replace it with a space to prevent craches. (Asger)
584 // Gets uninstantiated font setting at position.
585 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
586 Paragraph::size_type pos) const
588 lyx::Assert(pos <= size());
590 Pimpl::FontTable search_font(pos, LyXFont());
591 Pimpl::FontList::const_iterator cit = lower_bound(pimpl_->fontlist.begin(),
592 pimpl_->fontlist.end(),
593 search_font, Pimpl::matchFT());
595 if (cit != pimpl_->fontlist.end()) {
596 retfont = cit->font();
597 } else if (pos == size() && size()) {
598 retfont = getFontSettings(bparams, pos - 1);
600 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
601 #ifdef INHERIT_LANGUAGE
602 if (retfont.language() == inherit_language)
603 retfont.setLanguage(bparams.language);
610 // Gets uninstantiated font setting at position 0
611 LyXFont const Paragraph::getFirstFontSettings() const
614 if (!pimpl_->fontlist.empty())
615 return pimpl_->fontlist[0].font();
618 return LyXFont(LyXFont::ALL_INHERIT);
622 // Gets the fully instantiated font at a given position in a paragraph
623 // This is basically the same function as LyXText::GetFont() in text2.C.
624 // The difference is that this one is used for generating the LaTeX file,
625 // and thus cosmetic "improvements" are disallowed: This has to deliver
626 // the true picture of the buffer. (Asger)
627 // If position is -1, we get the layout font of the paragraph.
628 // If position is -2, we get the font of the manual label of the paragraph.
629 LyXFont const Paragraph::getFont(BufferParams const & bparams,
630 Paragraph::size_type pos) const
632 lyx::Assert(pos >= 0);
634 LyXLayout const & layout =
635 textclasslist.Style(bparams.textclass,
637 Paragraph::size_type main_body = 0;
638 if (layout.labeltype == LABEL_MANUAL)
639 main_body = beginningOfMainBody();
643 layoutfont = layout.labelfont;
645 layoutfont = layout.font;
647 LyXFont tmpfont = getFontSettings(bparams, pos);
648 #ifndef INHERIT_LANGUAGE
649 tmpfont.realize(layoutfont);
651 tmpfont.realize(layoutfont, bparams.language);
654 return pimpl_->realizeFont(tmpfont, bparams);
658 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
660 LyXLayout const & layout =
661 textclasslist.Style(bparams.textclass, getLayout());
663 LyXFont tmpfont = layout.labelfont;
664 tmpfont.setLanguage(getParLanguage(bparams));
666 return pimpl_->realizeFont(tmpfont, bparams);
670 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
672 LyXLayout const & layout =
673 textclasslist.Style(bparams.textclass,
676 LyXFont tmpfont = layout.font;
677 tmpfont.setLanguage(getParLanguage(bparams));
679 return pimpl_->realizeFont(tmpfont, bparams);
683 /// Returns the height of the highest font in range
685 Paragraph::highestFontInRange(Paragraph::size_type startpos,
686 Paragraph::size_type endpos,
687 LyXFont::FONT_SIZE const def_size) const
689 if (pimpl_->fontlist.empty())
692 LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
693 Pimpl::FontTable end_search(endpos, LyXFont());
694 Pimpl::FontList::const_iterator end_it =
695 lower_bound(pimpl_->fontlist.begin(),
696 pimpl_->fontlist.end(),
697 end_search, Pimpl::matchFT());
698 if (end_it != pimpl_->fontlist.end())
701 Pimpl::FontTable start_search(startpos, LyXFont());
702 Pimpl::FontList::const_iterator cit =
703 lower_bound(pimpl_->fontlist.begin(),
704 pimpl_->fontlist.end(),
705 start_search, Pimpl::matchFT());
707 for (; cit != end_it; ++cit) {
708 LyXFont::FONT_SIZE size = cit->font().size();
709 if (size == LyXFont::INHERIT_SIZE)
711 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
718 Paragraph::value_type
719 Paragraph::getUChar(BufferParams const & bparams,
720 Paragraph::size_type pos) const
722 value_type c = getChar(pos);
723 if (!lyxrc.rtl_support)
753 if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
760 void Paragraph::setFont(Paragraph::size_type pos,
761 LyXFont const & font)
763 lyx::Assert(pos <= size());
765 // First, reduce font against layout/label font
766 // Update: The SetCharFont() routine in text2.C already
767 // reduces font, so we don't need to do that here. (Asger)
768 // No need to simplify this because it will disappear
769 // in a new kernel. (Asger)
770 // Next search font table
772 Pimpl::FontTable search_font(pos, LyXFont());
773 Pimpl::FontList::iterator it = lower_bound(pimpl_->fontlist.begin(),
774 pimpl_->fontlist.end(),
775 search_font, Pimpl::matchFT());
776 unsigned int i = it - pimpl_->fontlist.begin();
777 bool notfound = it == pimpl_->fontlist.end();
779 if (!notfound && pimpl_->fontlist[i].font() == font)
782 bool begin = pos == 0 || notfound ||
783 (i > 0 && pimpl_->fontlist[i-1].pos() == pos - 1);
784 // Is position pos is a beginning of a font block?
785 bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
786 // Is position pos is the end of a font block?
787 if (begin && end) { // A single char block
788 if (i + 1 < pimpl_->fontlist.size() &&
789 pimpl_->fontlist[i + 1].font() == font) {
790 // Merge the singleton block with the next block
791 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
792 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
793 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
794 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
795 // Merge the singleton block with the previous block
796 pimpl_->fontlist[i - 1].pos(pos);
797 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
799 pimpl_->fontlist[i].font(font);
801 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
802 pimpl_->fontlist[i - 1].pos(pos);
804 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
805 Pimpl::FontTable(pos, font));
807 pimpl_->fontlist[i].pos(pos - 1);
808 if (!(i + 1 < pimpl_->fontlist.size() &&
809 pimpl_->fontlist[i + 1].font() == font))
810 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
811 Pimpl::FontTable(pos, font));
812 } else { // The general case. The block is splitted into 3 blocks
813 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
814 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
815 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
816 Pimpl::FontTable(pos, font));
822 void Paragraph::next(Paragraph * p)
828 // This function is able to hide closed footnotes.
829 Paragraph * Paragraph::next()
835 Paragraph const * Paragraph::next() const
841 void Paragraph::previous(Paragraph * p)
847 // This function is able to hide closed footnotes.
848 Paragraph * Paragraph::previous()
854 // This function is able to hide closed footnotes.
855 Paragraph const * Paragraph::previous() const
861 void Paragraph::breakParagraph(BufferParams const & bparams,
862 Paragraph::size_type pos,
865 // create a new paragraph
866 Paragraph * tmp = new Paragraph(this);
867 // remember to set the inset_owner
868 tmp->setInsetOwner(inInset());
870 // this is an idea for a more userfriendly layout handling, I will
871 // see what the users say
873 // layout stays the same with latex-environments
875 tmp->setOnlyLayout(layout);
876 tmp->setLabelWidthString(params().labelWidthString());
879 if (size() > pos || !size() || flag == 2) {
880 tmp->setOnlyLayout(layout);
881 tmp->params().align(params().align());
882 tmp->setLabelWidthString(params().labelWidthString());
884 tmp->params().lineBottom(params().lineBottom());
885 params().lineBottom(false);
886 tmp->params().pagebreakBottom(params().pagebreakBottom());
887 params().pagebreakBottom(false);
888 tmp->params().spaceBottom(params().spaceBottom());
889 params().spaceBottom(VSpace(VSpace::NONE));
891 tmp->params().depth(params().depth());
892 tmp->params().noindent(params().noindent());
894 // copy everything behind the break-position
895 // to the new paragraph
896 size_type pos_end = pimpl_->size() - 1;
899 for (; i <= pos_end; ++i) {
900 cutIntoMinibuffer(bparams, i);
901 if (tmp->insertFromMinibuffer(j - pos))
904 for (i = pos_end; i >= pos; --i) {
909 // just an idea of me
911 tmp->params().lineTop(params().lineTop());
912 tmp->params().pagebreakTop(params().pagebreakTop());
913 tmp->params().spaceTop(params().spaceTop());
914 tmp->bibkey = bibkey;
916 // layout stays the same with latex-environments
918 setOnlyLayout(tmp->layout);
919 setLabelWidthString(tmp->params().labelWidthString());
920 params().depth(tmp->params().depth());
926 void Paragraph::makeSameLayout(Paragraph const * par)
928 layout = par->layout;
930 params() = par->params();
934 int Paragraph::stripLeadingSpaces(LyXTextClassList::size_type tclass)
936 if (textclasslist.Style(tclass, getLayout()).free_spacing)
941 && (isNewline(0) || isLineSeparator(0))){
950 bool Paragraph::hasSameLayout(Paragraph const * par) const
953 par->layout == layout &&
954 params().sameLayout(par->params());
958 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
959 Paragraph::size_type pos)
961 // create a new paragraph
962 Paragraph * tmp = new Paragraph(this);
963 tmp->makeSameLayout(this);
965 // When can pos > Last()?
966 // I guess pos == Last() is possible.
968 // copy everything behind the break-position to the new
970 size_type pos_end = pimpl_->size() - 1;
974 for (size_type i = pos, j = pos; i <= pos_end; ++i) {
975 cutIntoMinibuffer(bparams, i);
976 if (tmp->insertFromMinibuffer(j - pos))
980 for (size_type k = pos_end; k >= pos; --k) {
987 // Be carefull, this does not make any check at all.
988 // This method has wrong name, it combined this par with the next par.
989 // In that sense it is the reverse of break paragraph. (Lgb)
990 void Paragraph::pasteParagraph(BufferParams const & bparams)
992 // copy the next paragraph to this one
993 Paragraph * the_next = next();
995 // first the DTP-stuff
996 params().lineBottom(the_next->params().lineBottom());
997 params().spaceBottom(the_next->params().spaceBottom());
998 params().pagebreakBottom(the_next->params().pagebreakBottom());
1000 size_type pos_end = the_next->pimpl_->size() - 1;
1001 size_type pos_insert = size();
1003 // ok, now copy the paragraph
1004 for (size_type i = 0, j = 0; i <= pos_end; ++i) {
1005 the_next->cutIntoMinibuffer(bparams, i);
1006 if (insertFromMinibuffer(pos_insert + j))
1010 // delete the next paragraph
1011 Paragraph * ppar = the_next->previous_;
1012 Paragraph * npar = the_next->next_;
1018 int Paragraph::getEndLabel(BufferParams const & bparams) const
1020 Paragraph const * par = this;
1021 depth_type par_depth = getDepth();
1023 LyXTextClass::LayoutList::size_type layout = par->getLayout();
1024 int const endlabeltype =
1025 textclasslist.Style(bparams.textclass,
1026 layout).endlabeltype;
1027 if (endlabeltype != END_LABEL_NO_LABEL) {
1029 return endlabeltype;
1031 depth_type const next_depth = next_->getDepth();
1032 if (par_depth > next_depth ||
1033 (par_depth == next_depth
1034 && layout != next_->getLayout()))
1035 return endlabeltype;
1040 par = par->outerHook();
1042 par_depth = par->getDepth();
1044 return END_LABEL_NO_LABEL;
1048 Paragraph::depth_type Paragraph::getDepth() const
1050 return params().depth();
1054 char Paragraph::getAlign() const
1056 return params().align();
1060 string const & Paragraph::getLabelstring() const
1062 return params().labelString();
1066 int Paragraph::getFirstCounter(int i) const
1068 return pimpl_->counter_[i];
1072 // the next two functions are for the manual labels
1073 string const Paragraph::getLabelWidthString() const
1075 if (!params().labelWidthString().empty())
1076 return params().labelWidthString();
1078 return _("Senseless with this layout!");
1082 void Paragraph::setLabelWidthString(string const & s)
1084 params().labelWidthString(s);
1088 void Paragraph::setOnlyLayout(LyXTextClass::size_type new_layout)
1090 layout = new_layout;
1094 void Paragraph::setLayout(LyXTextClass::size_type new_layout)
1096 layout = new_layout;
1097 params().labelWidthString(string());
1098 params().align(LYX_ALIGN_LAYOUT);
1099 params().spaceTop(VSpace(VSpace::NONE));
1100 params().spaceBottom(VSpace(VSpace::NONE));
1101 params().spacing(Spacing(Spacing::Default));
1105 // if the layout of a paragraph contains a manual label, the beginning of the
1106 // main body is the beginning of the second word. This is what the par-
1107 // function returns. If the layout does not contain a label, the main
1108 // body always starts with position 0. This differentiation is necessary,
1109 // because there cannot be a newline or a blank <= the beginning of the
1110 // main body in TeX.
1112 int Paragraph::beginningOfMainBody() const
1114 // Unroll the first two cycles of the loop
1115 // and remember the previous character to
1116 // remove unnecessary GetChar() calls
1119 && getChar(i) != Paragraph::META_NEWLINE) {
1121 char previous_char = 0;
1124 && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1125 // Yes, this ^ is supposed to be "= " not "=="
1128 && previous_char != ' '
1129 && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1131 previous_char = temp;
1140 Paragraph * Paragraph::depthHook(depth_type depth)
1142 Paragraph * newpar = this;
1145 newpar = newpar->previous();
1146 } while (newpar && newpar->getDepth() > depth);
1149 if (previous() || getDepth())
1150 lyxerr << "ERROR (Paragraph::DepthHook): "
1159 Paragraph const * Paragraph::depthHook(depth_type depth) const
1161 Paragraph const * newpar = this;
1164 newpar = newpar->previous();
1165 } while (newpar && newpar->getDepth() > depth);
1168 if (previous() || getDepth())
1169 lyxerr << "ERROR (Paragraph::DepthHook): "
1177 Paragraph * Paragraph::outerHook()
1181 return depthHook(depth_type(getDepth() - 1));
1184 Paragraph const * Paragraph::outerHook() const
1188 return depthHook(depth_type(getDepth() - 1));
1191 int Paragraph::autoDeleteInsets()
1194 InsetList::size_type index = 0;
1195 while (index < insetlist.size()) {
1196 if (insetlist[index].inset && insetlist[index].inset->autoDelete()) {
1197 erase(insetlist[index].pos);
1198 // Erase() calls to insetlist.erase(&insetlist[index])
1199 // so index shouldn't be increased.
1208 Paragraph::inset_iterator
1209 Paragraph::InsetIterator(Paragraph::size_type pos)
1211 InsetTable search_inset(pos, 0);
1212 InsetList::iterator it = lower_bound(insetlist.begin(),
1214 search_inset, Pimpl::matchIT());
1215 return inset_iterator(it);
1219 // returns -1 if inset not found
1220 int Paragraph::getPositionOfInset(Inset * inset) const
1223 for (InsetList::const_iterator cit = insetlist.begin();
1224 cit != insetlist.end(); ++cit) {
1225 if (cit->inset == inset) {
1229 if (inset == bibkey)
1236 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1237 BufferParams const & bparams,
1238 ostream & os, TexRow & texrow,
1241 lyxerr[Debug::LATEX] << "TeXOnePar... " << this << endl;
1242 LyXLayout const & style =
1243 textclasslist.Style(bparams.textclass,
1246 bool further_blank_line = false;
1248 if (params().startOfAppendix()) {
1249 os << "\\appendix\n";
1253 if (!params().spacing().isDefault()
1254 && (!previous() || !previous()->hasSameLayout(this))) {
1255 os << params().spacing().writeEnvirBegin() << "\n";
1259 if (tex_code_break_column && style.isCommand()){
1264 if (params().pagebreakTop()) {
1266 further_blank_line = true;
1268 if (params().spaceTop().kind() != VSpace::NONE) {
1269 os << params().spaceTop().asLatexCommand(bparams);
1270 further_blank_line = true;
1273 if (params().lineTop()) {
1274 os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1275 << "\\vspace{-1\\parskip}";
1276 further_blank_line = true;
1279 if (further_blank_line){
1284 Language const * language = getParLanguage(bparams);
1285 Language const * doc_language = bparams.language;
1286 Language const * previous_language = previous()
1287 ? previous()->getParLanguage(bparams) : doc_language;
1289 if (language->babel() != previous_language->babel()
1290 // check if we already put language command in TeXEnvironment()
1291 && !(textclasslist.Style(bparams.textclass, layout).isEnvironment()
1292 && (!previous() || previous()->layout != layout ||
1293 previous()->params().depth() != params().depth()))) {
1295 if (!lyxrc.language_command_end.empty() &&
1296 previous_language->babel() != doc_language->babel()) {
1297 os << subst(lyxrc.language_command_end, "$$lang",
1298 previous_language->babel())
1303 if (lyxrc.language_command_end.empty() ||
1304 language->babel() != doc_language->babel()) {
1305 os << subst(lyxrc.language_command_begin, "$$lang",
1312 if (bparams.inputenc == "auto" &&
1313 language->encoding() != previous_language->encoding()) {
1314 os << "\\inputencoding{"
1315 << language->encoding()->LatexName()
1320 switch (style.latextype) {
1323 << style.latexname()
1324 << style.latexparam();
1326 case LATEX_ITEM_ENVIRONMENT:
1328 bibkey->latex(buf, os, false, false);
1332 case LATEX_LIST_ENVIRONMENT:
1339 bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1341 // Make sure that \\par is done with the font of the last
1342 // character if this has another size as the default.
1343 // This is necessary because LaTeX (and LyX on the screen)
1344 // calculates the space between the baselines according
1345 // to this font. (Matthias)
1347 // Is this really needed ? (Dekel)
1348 // We do not need to use to change the font for the last paragraph
1349 // or for a command.
1350 LyXFont const font =
1352 ? getLayoutFont(bparams)
1353 : getFont(bparams, size() - 1));
1355 bool is_command = textclasslist.Style(bparams.textclass,
1356 getLayout()).isCommand();
1357 if (style.resfont.size() != font.size() && next_ && !is_command) {
1360 os << "\\" << font.latexSize() << " \\par}";
1361 } else if (need_par) {
1363 } else if (is_command)
1366 switch (style.latextype) {
1367 case LATEX_ITEM_ENVIRONMENT:
1368 case LATEX_LIST_ENVIRONMENT:
1369 if (next_ && (params().depth() < next_->params().depth())) {
1374 case LATEX_ENVIRONMENT:
1375 // if its the last paragraph of the current environment
1376 // skip it otherwise fall through
1378 && (next_->layout != layout
1379 || next_->params().depth() != params().depth()))
1382 // we don't need it for the last paragraph!!!
1389 further_blank_line = false;
1390 if (params().lineBottom()) {
1391 os << "\\lyxline{\\" << font.latexSize() << '}';
1392 further_blank_line = true;
1395 if (params().spaceBottom().kind() != VSpace::NONE) {
1396 os << params().spaceBottom().asLatexCommand(bparams);
1397 further_blank_line = true;
1400 if (params().pagebreakBottom()) {
1402 further_blank_line = true;
1405 if (further_blank_line){
1410 if (!params().spacing().isDefault()
1411 && (!next_ || !next_->hasSameLayout(this))) {
1412 os << params().spacing().writeEnvirEnd() << "\n";
1416 // we don't need it for the last paragraph!!!
1421 // Since \selectlanguage write the language to the aux file,
1422 // we need to reset the language at the end of footnote or
1425 if (language->babel() != doc_language->babel()) {
1426 if (lyxrc.language_command_end.empty())
1427 os << subst(lyxrc.language_command_begin,
1429 doc_language->babel())
1432 os << subst(lyxrc.language_command_end,
1440 lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1445 // This one spits out the text of the paragraph
1446 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1447 BufferParams const & bparams,
1448 ostream & os, TexRow & texrow,
1451 lyxerr[Debug::LATEX] << "SimpleTeXOnePar... " << this << endl;
1453 bool return_value = false;
1455 LyXLayout const & style =
1456 textclasslist.Style(bparams.textclass,
1460 // Maybe we have to create a optional argument.
1461 size_type main_body;
1462 if (style.labeltype != LABEL_MANUAL)
1465 main_body = beginningOfMainBody();
1469 if (main_body > 0) {
1472 basefont = getLabelFont(bparams);
1474 basefont = getLayoutFont(bparams);
1478 && !pimpl_->size()) {
1479 if (style.isCommand()) {
1485 moving_arg |= style.needprotect;
1487 // Which font is currently active?
1488 LyXFont running_font(basefont);
1489 // Do we have an open font change?
1490 bool open_font = false;
1492 texrow.start(this, 0);
1494 for (size_type i = 0; i < size(); ++i) {
1496 // First char in paragraph or after label?
1497 if (i == main_body) {
1498 if (main_body > 0) {
1500 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1503 basefont = getLayoutFont(bparams);
1504 running_font = basefont;
1508 if (style.isCommand()) {
1513 if (params().noindent()) {
1514 os << "\\noindent ";
1517 switch (params().align()) {
1518 case LYX_ALIGN_NONE:
1519 case LYX_ALIGN_BLOCK:
1520 case LYX_ALIGN_LAYOUT:
1521 case LYX_ALIGN_SPECIAL:
1523 case LYX_ALIGN_LEFT:
1524 if (getParLanguage(bparams)->babel() != "hebrew") {
1525 os << "\\begin{flushleft}";
1528 os << "\\begin{flushright}";
1532 case LYX_ALIGN_RIGHT:
1533 if (getParLanguage(bparams)->babel() != "hebrew") {
1534 os << "\\begin{flushright}";
1537 os << "\\begin{flushleft}";
1541 case LYX_ALIGN_CENTER:
1542 os << "\\begin{center}";
1548 value_type c = getChar(i);
1550 // Fully instantiated font
1551 LyXFont font = getFont(bparams, i);
1553 LyXFont const last_font = running_font;
1555 // Spaces at end of font change are simulated to be
1556 // outside font change, i.e. we write "\textXX{text} "
1557 // rather than "\textXX{text }". (Asger)
1558 if (open_font && c == ' ' && i <= size() - 2) {
1559 LyXFont const next_font = getFont(bparams, i + 1);
1560 if (next_font != running_font
1561 && next_font != font) {
1566 // We end font definition before blanks
1567 if (font != running_font && open_font) {
1568 column += running_font.latexWriteEndChanges(os,
1570 (i == main_body-1) ? basefont : font);
1571 running_font = basefont;
1575 // Blanks are printed before start of fontswitch
1577 // Do not print the separation of the optional argument
1578 if (i != main_body - 1) {
1579 pimpl_->simpleTeXBlanks(os, texrow, i,
1580 column, font, style);
1584 // Do we need to change font?
1585 if (font != running_font && i != main_body - 1) {
1586 column += font.latexWriteStartChanges(os, basefont,
1588 running_font = font;
1592 if (c == Paragraph::META_NEWLINE) {
1593 // newlines are handled differently here than
1594 // the default in SimpleTeXSpecialChars().
1595 if (!style.newline_allowed) {
1599 column += running_font.latexWriteEndChanges(os, basefont, basefont);
1602 basefont = getLayoutFont(bparams);
1603 running_font = basefont;
1604 if (font.family() ==
1605 LyXFont::TYPEWRITER_FAMILY) {
1613 texrow.start(this, i + 1);
1616 pimpl_->simpleTeXSpecialChars(buf, bparams,
1617 os, texrow, moving_arg,
1619 basefont, open_font,
1620 style, i, column, c);
1624 // If we have an open font definition, we have to close it
1626 #ifdef FIXED_LANGUAGE_END_DETECTION
1629 .latexWriteEndChanges(os, basefont,
1630 next_->getFont(bparams,
1633 running_font.latexWriteEndChanges(os, basefont,
1637 #ifdef WITH_WARNINGS
1638 //#warning For now we ALWAYS have to close the foreign font settings if they are
1639 //#warning there as we start another \selectlanguage with the next paragraph if
1640 //#warning we are in need of this. This should be fixed sometime (Jug)
1642 running_font.latexWriteEndChanges(os, basefont, basefont);
1646 // Needed if there is an optional argument but no contents.
1647 if (main_body > 0 && main_body == size()) {
1649 return_value = false;
1652 switch (params().align()) {
1653 case LYX_ALIGN_NONE:
1654 case LYX_ALIGN_BLOCK:
1655 case LYX_ALIGN_LAYOUT:
1656 case LYX_ALIGN_SPECIAL:
1658 case LYX_ALIGN_LEFT:
1659 if (getParLanguage(bparams)->babel() != "hebrew") {
1660 os << "\\end{flushleft}";
1663 os << "\\end{flushright}";
1667 case LYX_ALIGN_RIGHT:
1668 if (getParLanguage(bparams)->babel() != "hebrew") {
1669 os << "\\end{flushright}";
1672 os << "\\end{flushleft}";
1676 case LYX_ALIGN_CENTER:
1677 os << "\\end{center}";
1682 lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1683 return return_value;
1687 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1689 bool retval = false;
1691 case Paragraph::META_HFILL:
1692 sgml_string.erase();
1694 case Paragraph::META_NEWLINE:
1698 sgml_string = "&";
1701 sgml_string = "<";
1704 sgml_string = ">";
1707 sgml_string = "$";
1710 sgml_string = "#";
1713 sgml_string = "%";
1716 sgml_string = "[";
1719 sgml_string = "]";
1722 sgml_string = "{";
1725 sgml_string = "}";
1728 sgml_string = "˜";
1731 sgml_string = """;
1734 sgml_string = "\";
1740 case '\0': // Ignore :-)
1741 sgml_string.erase();
1751 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1752 BufferParams const & bparams,
1753 ostream & os, TexRow & texrow)
1755 lyxerr[Debug::LATEX] << "TeXEnvironment... " << this << endl;
1757 LyXLayout const & style =
1758 textclasslist.Style(bparams.textclass,
1761 Language const * language = getParLanguage(bparams);
1762 Language const * doc_language = bparams.language;
1763 Language const * previous_language = previous_
1764 ? previous_->getParLanguage(bparams) : doc_language;
1765 if (language->babel() != previous_language->babel()) {
1767 if (!lyxrc.language_command_end.empty() &&
1768 previous_language->babel() != doc_language->babel()) {
1769 os << subst(lyxrc.language_command_end, "$$lang",
1770 previous_language->babel())
1775 if (lyxrc.language_command_end.empty() ||
1776 language->babel() != doc_language->babel()) {
1777 os << subst(lyxrc.language_command_begin, "$$lang",
1784 if (style.isEnvironment()){
1785 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1786 os << "\\begin{" << style.latexname() << "}{"
1787 << params().labelWidthString() << "}\n";
1788 } else if (style.labeltype == LABEL_BIBLIO) {
1790 os << "\\begin{" << style.latexname() << "}{"
1791 << bibitemWidest(buf)
1793 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1794 os << "\\begin{" << style.latexname() << '}'
1795 << style.latexparam() << '\n';
1797 os << "\\begin{" << style.latexname() << '}'
1798 << style.latexparam() << '\n';
1801 Paragraph * par = this;
1803 par = par->TeXOnePar(buf, bparams,
1806 if (par && par->params().depth() > params().depth()) {
1807 if (textclasslist.Style(bparams.textclass,
1808 par->layout).isParagraph()
1810 // How to handle this? (Lgb)
1811 //&& !suffixIs(os, "\n\n")
1813 // There should be at least one '\n' already
1814 // but we need there to be two for Standard
1815 // paragraphs that are depth-increment'ed to be
1816 // output correctly. However, tables can
1817 // also be paragraphs so don't adjust them.
1820 // Will it ever harm to have one '\n' too
1821 // many? i.e. that we sometimes will have
1822 // three in a row. (Lgb)
1826 par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1829 && par->layout == layout
1830 && par->params().depth() == params().depth());
1832 if (style.isEnvironment()) {
1833 os << "\\end{" << style.latexname() << "}\n";
1836 lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1837 return par; // ale970302
1841 bool Paragraph::isHfill(size_type pos) const
1843 return IsHfillChar(getChar(pos));
1847 bool Paragraph::isInset(size_type pos) const
1849 return IsInsetChar(getChar(pos));
1853 bool Paragraph::isNewline(size_type pos) const
1855 return pos >= 0 && IsNewlineChar(getChar(pos));
1859 bool Paragraph::isSeparator(size_type pos) const
1861 return IsSeparatorChar(getChar(pos));
1865 bool Paragraph::isLineSeparator(size_type pos) const
1867 return IsLineSeparatorChar(getChar(pos));
1871 bool Paragraph::isKomma(size_type pos) const
1873 return IsKommaChar(getChar(pos));
1877 /// Used by the spellchecker
1878 bool Paragraph::isLetter(Paragraph::size_type pos) const
1880 value_type const c = getChar(pos);
1881 if (IsLetterChar(c))
1884 return getInset(pos)->isLetter();
1885 // We want to pass the ' and escape chars to ispell
1886 string const extra = lyxrc.isp_esc_chars + '\'';
1887 return contains(extra, c);
1891 bool Paragraph::isWord(size_type pos ) const
1893 return IsWordChar(getChar(pos)) ;
1898 Paragraph::getParLanguage(BufferParams const & bparams) const
1901 #ifndef INHERIT_LANGUAGE
1902 return getFirstFontSettings().language();
1904 Language const * lang = getFirstFontSettings().language();
1905 #warning We should make this somewhat better, any ideas? (Jug)
1906 if (lang == inherit_language || lang == ignore_language)
1907 lang = bparams.language;
1910 } else if (previous_)
1911 return previous_->getParLanguage(bparams);
1913 return bparams.language;
1917 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1919 return lyxrc.rtl_support
1920 && getParLanguage(bparams)->RightToLeft();
1924 void Paragraph::changeLanguage(BufferParams const & bparams,
1925 Language const * from, Language const * to)
1927 for (size_type i = 0; i < size(); ++i) {
1928 LyXFont font = getFontSettings(bparams, i);
1929 if (font.language() == from) {
1930 font.setLanguage(to);
1937 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1939 Language const * doc_language = bparams.language;
1940 for (Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1941 cit != pimpl_->fontlist.end(); ++cit)
1942 if (cit->font().language() != ignore_language &&
1943 cit->font().language() != latex_language &&
1944 #ifdef INHERIT_LANGUAGE
1945 cit->font().language() != inherit_language &&
1947 cit->font().language() != doc_language)
1953 // Convert the paragraph to a string.
1954 // Used for building the table of contents
1955 string const Paragraph::asString(Buffer const * buffer, bool label)
1957 BufferParams const & bparams = buffer->params;
1959 if (label && !params().labelString().empty())
1960 s += params().labelString() + ' ';
1961 string::size_type const len = s.size();
1963 for (Paragraph::size_type i = 0; i < size(); ++i) {
1964 value_type c = getChar(i);
1967 else if (c == META_INSET &&
1968 getInset(i)->lyxCode() == Inset::MATH_CODE) {
1970 getInset(i)->ascii(buffer, ost);
1971 s += subst(ost.str().c_str(),'\n',' ');
1975 if (isRightToLeftPar(bparams))
1976 reverse(s.begin() + len,s.end());
1982 string const Paragraph::asString(Buffer const * buffer,
1983 Paragraph::size_type beg,
1984 Paragraph::size_type end,
1989 if (beg == 0 && label && !params().labelString().empty())
1990 ost << params().labelString() << ' ';
1992 for (Paragraph::size_type i = beg; i < end; ++i) {
1993 value_type const c = getUChar(buffer->params, i);
1996 else if (c == META_NEWLINE)
1998 else if (c == META_HFILL)
2000 else if (c == META_INSET) {
2001 getInset(i)->ascii(buffer, ost);
2005 return ost.str().c_str();
2009 void Paragraph::setInsetOwner(Inset * i)
2011 pimpl_->inset_owner = i;
2012 for (InsetList::const_iterator cit = insetlist.begin();
2013 cit != insetlist.end(); ++cit) {
2015 cit->inset->setOwner(i);
2020 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2023 for (InsetList::const_iterator cit = insetlist.begin();
2024 cit != insetlist.end(); ++cit) {
2026 if (cit->inset->isTextInset()) {
2027 static_cast<UpdatableInset *>
2028 (cit->inset)->deleteLyXText(bv, true);
2035 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2038 for (InsetList::const_iterator cit = insetlist.begin();
2039 cit != insetlist.end(); ++cit)
2042 if (cit->inset->isTextInset()) {
2043 static_cast<UpdatableInset *>
2044 (cit->inset)->resizeLyXText(bv, true);
2051 void Paragraph::setContentsFromPar(Paragraph * par)
2053 pimpl_->setContentsFromPar(par);
2057 Paragraph::size_type Paragraph::size() const
2059 return pimpl_->size();
2063 Paragraph::value_type
2064 Paragraph::getChar(Paragraph::size_type pos) const
2066 return pimpl_->getChar(pos);
2070 int Paragraph::id() const
2076 void Paragraph::id(int id_arg)
2078 pimpl_->id_ = id_arg;
2082 LyXTextClass::LayoutList::size_type Paragraph::getLayout() const
2088 bool Paragraph::isFirstInSequence() const
2090 Paragraph const * dhook = depthHook(getDepth());
2091 return (dhook == this
2092 || dhook->getLayout() != getLayout()
2093 || dhook->getDepth() != getDepth());
2097 Inset * Paragraph::inInset() const
2099 return pimpl_->inset_owner;
2103 void Paragraph::clearContents()
2109 void Paragraph::setCounter(int i, int v)
2111 pimpl_->counter_[i] = v;
2115 int Paragraph::getCounter(int i) const
2117 return pimpl_->counter_[i];
2121 void Paragraph::incCounter(int i)
2123 pimpl_->counter_[i]++;
2127 void Paragraph::setChar(size_type pos, value_type c)
2129 pimpl_->setChar(pos, c);
2133 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2138 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2140 return inset_iterator(insetlist.begin());
2144 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2146 return inset_iterator(insetlist.end());
2150 ParagraphParameters & Paragraph::params()
2152 return pimpl_->params;
2156 ParagraphParameters const & Paragraph::params() const
2158 return pimpl_->params;
2162 Paragraph * Paragraph::getParFromID(int id) const
2164 return pimpl_->getParFromID(id);