]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
25bf6ea63f46736fe3d10d868de328781e831637
[lyx.git] / src / paragraph.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "paragraph.h"
14 #include "paragraph_pimpl.h"
15 #include "lyxrc.h"
16 #include "layout.h"
17 #include "language.h"
18 #include "buffer.h"
19 #include "bufferparams.h"
20 #include "debug.h"
21 #include "texrow.h"
22 #include "BufferView.h"
23 #include "encoding.h"
24 #include "ParameterStruct.h"
25 #include "gettext.h"
26 #include "changes.h"
27
28 #include "insets/insetbibitem.h"
29 #include "insets/insetoptarg.h"
30
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/FileInfo.h"
35 #include "support/LAssert.h"
36 #include "support/textutils.h"
37
38 #include <algorithm>
39 #include <fstream>
40 #include <csignal>
41 #include <ctime>
42
43 using std::ostream;
44 using std::endl;
45 using std::fstream;
46 using std::ios;
47 using std::lower_bound;
48 using std::upper_bound;
49
50 using lyx::pos_type;
51
52
53 // this is a minibuffer
54
55 namespace {
56
57 char minibuffer_char;
58 LyXFont minibuffer_font;
59 Inset * minibuffer_inset;
60
61 } // namespace anon
62
63
64 extern BufferView * current_view;
65
66
67 Paragraph::Paragraph()
68         : pimpl_(new Paragraph::Pimpl(this))
69 {
70 #ifndef NO_NEXT
71         next_ = 0;
72         previous_ = 0;
73 #endif
74         enumdepth = 0;
75         itemdepth = 0;
76         params().clear();
77 }
78
79
80 #ifndef NO_NEXT
81 // This constructor inserts the new paragraph in a list.
82 // It is placed after par.
83 Paragraph::Paragraph(Paragraph * par)
84         : pimpl_(new Paragraph::Pimpl(this))
85 {
86         enumdepth = 0;
87         itemdepth = 0;
88
89         // double linked list begin
90         next_ = par->next_;
91         if (next_)
92                 next_->previous_ = this;
93         previous_ = par;
94         previous_->next_ = this;
95         // end
96
97         params().clear();
98 }
99 #endif
100
101
102 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
103         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
104 {
105         enumdepth = 0;
106         itemdepth = 0;
107 #ifndef NO_NEXT
108         next_     = 0;
109         previous_ = 0;
110 #endif
111         // this is because of the dummy layout of the paragraphs that
112         // follow footnotes
113         layout_ = lp.layout();
114
115         // copy everything behind the break-position to the new paragraph
116         insetlist = lp.insetlist;
117         InsetList::iterator it = insetlist.begin();
118         InsetList::iterator end = insetlist.end();
119         for (; it != end; ++it) {
120                 it.setInset(it.getInset()->clone(*current_view->buffer(),
121                                                  same_ids));
122                 // tell the new inset who is the boss now
123                 it.getInset()->parOwner(this);
124         }
125 }
126
127
128 // the destructor removes the new paragraph from the list
129 Paragraph::~Paragraph()
130 {
131 #ifndef NO_NEXT
132         if (previous_)
133                 previous_->next_ = next_;
134         if (next_)
135                 next_->previous_ = previous_;
136 #endif
137
138         delete pimpl_;
139         //
140         //lyxerr << "Paragraph::paragraph_id = "
141         //       << Paragraph::paragraph_id << endl;
142 }
143
144
145 void Paragraph::write(Buffer const * buf, ostream & os,
146                           BufferParams const & bparams,
147                           depth_type & dth) const
148 {
149         // The beginning or end of a deeper (i.e. nested) area?
150         if (dth != params().depth()) {
151                 if (params().depth() > dth) {
152                         while (params().depth() > dth) {
153                                 os << "\n\\begin_deeper ";
154                                 ++dth;
155                         }
156                 } else {
157                         while (params().depth() < dth) {
158                                 os << "\n\\end_deeper ";
159                                 --dth;
160                         }
161                 }
162         }
163
164         // First write the layout
165         os << "\n\\layout " << layout()->name() << '\n';
166
167         params().write(os);
168
169         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
170
171         Change running_change = Change(Change::UNCHANGED);
172         lyx::time_type const curtime(lyx::current_time());
173
174         int column = 0;
175         for (pos_type i = 0; i < size(); ++i) {
176                 if (!i) {
177                         os << '\n';
178                         column = 0;
179                 }
180
181                 Change change = pimpl_->lookupChangeFull(i);
182                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
183                 running_change = change;
184
185                 // Write font changes
186                 LyXFont font2 = getFontSettings(bparams, i);
187                 if (font2 != font1) {
188                         font2.lyxWriteChanges(font1, os);
189                         column = 0;
190                         font1 = font2;
191                 }
192
193                 value_type const c = getChar(i);
194                 switch (c) {
195                 case META_INSET:
196                 {
197                         Inset const * inset = getInset(i);
198                         if (inset)
199                                 if (inset->directWrite()) {
200                                         // international char, let it write
201                                         // code directly so it's shorter in
202                                         // the file
203                                         inset->write(buf, os);
204                                 } else {
205                                         os << "\n\\begin_inset ";
206                                         inset->write(buf, os);
207                                         os << "\n\\end_inset \n\n";
208                                         column = 0;
209                                 }
210                 }
211                 break;
212                 case '\\':
213                         os << "\n\\backslash \n";
214                         column = 0;
215                         break;
216                 case '.':
217                         if (i + 1 < size() && getChar(i + 1) == ' ') {
218                                 os << ".\n";
219                                 column = 0;
220                         } else
221                                 os << '.';
222                         break;
223                 default:
224                         if ((column > 70 && c == ' ')
225                             || column > 79) {
226                                 os << '\n';
227                                 column = 0;
228                         }
229                         // this check is to amend a bug. LyX sometimes
230                         // inserts '\0' this could cause problems.
231                         if (c != '\0')
232                                 os << c;
233                         else
234                                 lyxerr << "ERROR (Paragraph::writeFile):"
235                                         " NULL char in structure." << endl;
236                         ++column;
237                         break;
238                 }
239         }
240
241         // to make reading work properly
242         if (!size()) {
243                 running_change = pimpl_->lookupChange(0);
244                 Changes::lyxMarkChange(os, column, curtime,
245                         Change(Change::UNCHANGED), running_change);
246         }
247         Changes::lyxMarkChange(os, column, curtime,
248                 running_change, Change(Change::UNCHANGED));
249 }
250
251
252 void Paragraph::validate(LaTeXFeatures & features) const
253 {
254         pimpl_->validate(features, *layout());
255 }
256
257
258 // First few functions needed for cut and paste and paragraph breaking.
259 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
260 {
261         BufferParams bparams = buffer.params;
262
263         minibuffer_char = getChar(pos);
264         minibuffer_font = getFontSettings(bparams, pos);
265         minibuffer_inset = 0;
266         if (minibuffer_char == Paragraph::META_INSET) {
267                 if (getInset(pos)) {
268                         minibuffer_inset = getInset(pos)->clone(buffer);
269                 } else {
270                         minibuffer_inset = 0;
271                         minibuffer_char = ' ';
272                         // This reflects what GetInset() does (ARRae)
273                 }
274         }
275 }
276
277
278 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
279 {
280         minibuffer_char = getChar(pos);
281         minibuffer_font = getFontSettings(bparams, pos);
282         minibuffer_inset = 0;
283         if (minibuffer_char == Paragraph::META_INSET) {
284                 if (getInset(pos)) {
285                         // the inset is not in a paragraph anymore
286                         minibuffer_inset = insetlist.release(pos);
287                         minibuffer_inset->parOwner(0);
288                 } else {
289                         minibuffer_inset = 0;
290                         minibuffer_char = ' ';
291                         // This reflects what GetInset() does (ARRae)
292                 }
293
294         }
295
296         // Erase(pos); now the caller is responsible for that.
297 }
298
299
300 bool Paragraph::insertFromMinibuffer(pos_type pos)
301 {
302         if (minibuffer_char == Paragraph::META_INSET) {
303                 if (!insetAllowed(minibuffer_inset->lyxCode())) {
304                         return false;
305                 }
306                 insertInset(pos, minibuffer_inset, minibuffer_font);
307         } else {
308                 LyXFont f = minibuffer_font;
309                 if (!checkInsertChar(f)) {
310                         return false;
311                 }
312                 insertChar(pos, minibuffer_char, f);
313         }
314         return true;
315 }
316
317 // end of minibuffer
318
319
320 void Paragraph::eraseIntern(lyx::pos_type pos)
321 {
322         pimpl_->eraseIntern(pos);
323 }
324
325
326 void Paragraph::erase(pos_type pos)
327 {
328         pimpl_->erase(pos);
329 }
330
331
332 bool Paragraph::erase(pos_type start, pos_type end)
333 {
334         return pimpl_->erase(start, end);
335 }
336
337
338 bool Paragraph::checkInsertChar(LyXFont & font)
339 {
340         if (pimpl_->inset_owner)
341                 return pimpl_->inset_owner->checkInsertChar(font);
342         return true;
343 }
344
345
346 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
347 {
348         LyXFont const f(LyXFont::ALL_INHERIT);
349         insertChar(pos, c, f);
350 }
351
352
353 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
354                            LyXFont const & font, Change change)
355 {
356         pimpl_->insertChar(pos, c, font, change);
357 }
358
359
360 void Paragraph::insertInset(pos_type pos, Inset * inset)
361 {
362         LyXFont const f(LyXFont::ALL_INHERIT);
363         insertInset(pos, inset, f);
364 }
365
366
367 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
368 {
369         pimpl_->insertInset(pos, inset, font, change);
370 }
371
372
373 bool Paragraph::insetAllowed(Inset::Code code)
374 {
375         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
376         if (pimpl_->inset_owner)
377                 return pimpl_->inset_owner->insetAllowed(code);
378         return true;
379 }
380
381
382 Inset * Paragraph::getInset(pos_type pos)
383 {
384         lyx::Assert(pos < size());
385         return insetlist.get(pos);
386 }
387
388
389 Inset const * Paragraph::getInset(pos_type pos) const
390 {
391         lyx::Assert(pos < size());
392         return insetlist.get(pos);
393 }
394
395
396 // Gets uninstantiated font setting at position.
397 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
398                                          pos_type pos) const
399 {
400         lyx::Assert(pos <= size());
401
402         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
403         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
404         for (; cit != end; ++cit) {
405                 if (cit->pos() >= pos)
406                         break;
407         }
408
409         LyXFont retfont;
410         if (cit != end)
411                 retfont = cit->font();
412         else if (pos == size() && !empty())
413                 retfont = getFontSettings(bparams, pos - 1);
414         else
415                 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
416
417         return retfont;
418 }
419
420
421 // Gets uninstantiated font setting at position 0
422 LyXFont const Paragraph::getFirstFontSettings() const
423 {
424         if (!empty() && !pimpl_->fontlist.empty())
425                 return pimpl_->fontlist[0].font();
426
427         return LyXFont(LyXFont::ALL_INHERIT);
428 }
429
430
431 // Gets the fully instantiated font at a given position in a paragraph
432 // This is basically the same function as LyXText::GetFont() in text2.C.
433 // The difference is that this one is used for generating the LaTeX file,
434 // and thus cosmetic "improvements" are disallowed: This has to deliver
435 // the true picture of the buffer. (Asger)
436 LyXFont const Paragraph::getFont(BufferParams const & bparams,
437                                  pos_type pos) const
438 {
439         lyx::Assert(pos >= 0);
440
441         LyXLayout_ptr const & lout = layout();
442
443         pos_type const body_pos = beginningOfBody();
444
445         LyXFont layoutfont;
446         if (pos < body_pos)
447                 layoutfont = lout->labelfont;
448         else
449                 layoutfont = lout->font;
450
451         LyXFont tmpfont = getFontSettings(bparams, pos);
452         tmpfont.realize(layoutfont);
453
454         return pimpl_->realizeFont(tmpfont, bparams);
455 }
456
457
458 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
459 {
460         LyXLayout_ptr const & lout = layout();
461
462         LyXFont tmpfont = lout->labelfont;
463         tmpfont.setLanguage(getParLanguage(bparams));
464
465         return pimpl_->realizeFont(tmpfont, bparams);
466 }
467
468
469 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
470 {
471         LyXLayout_ptr const & lout = layout();
472
473         LyXFont tmpfont = lout->font;
474         tmpfont.setLanguage(getParLanguage(bparams));
475
476         return pimpl_->realizeFont(tmpfont, bparams);
477 }
478
479
480 /// Returns the height of the highest font in range
481 LyXFont::FONT_SIZE
482 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
483                               LyXFont::FONT_SIZE const def_size) const
484 {
485         if (pimpl_->fontlist.empty())
486                 return def_size;
487
488         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
489         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
490         for (; end_it != end; ++end_it) {
491                 if (end_it->pos() >= endpos)
492                         break;
493         }
494
495         if (end_it != end)
496                 ++end_it;
497
498         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
499         for (; cit != end; ++cit) {
500                 if (cit->pos() >= startpos)
501                         break;
502         }
503
504         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
505         for (; cit != end_it; ++cit) {
506                 LyXFont::FONT_SIZE size = cit->font().size();
507                 if (size == LyXFont::INHERIT_SIZE)
508                         size = def_size;
509                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
510                         maxsize = size;
511         }
512         return maxsize;
513 }
514
515
516 Paragraph::value_type
517 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
518 {
519         value_type c = getChar(pos);
520         if (!lyxrc.rtl_support)
521                 return c;
522
523         value_type uc = c;
524         switch (c) {
525         case '(':
526                 uc = ')';
527                 break;
528         case ')':
529                 uc = '(';
530                 break;
531         case '[':
532                 uc = ']';
533                 break;
534         case ']':
535                 uc = '[';
536                 break;
537         case '{':
538                 uc = '}';
539                 break;
540         case '}':
541                 uc = '{';
542                 break;
543         case '<':
544                 uc = '>';
545                 break;
546         case '>':
547                 uc = '<';
548                 break;
549         }
550         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
551                 return uc;
552         else
553                 return c;
554 }
555
556
557 void Paragraph::setFont(pos_type pos, LyXFont const & font)
558 {
559         lyx::Assert(pos <= size());
560
561         // First, reduce font against layout/label font
562         // Update: The SetCharFont() routine in text2.C already
563         // reduces font, so we don't need to do that here. (Asger)
564         // No need to simplify this because it will disappear
565         // in a new kernel. (Asger)
566         // Next search font table
567
568         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
569         Pimpl::FontList::iterator it = beg;
570         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
571         for (; it != endit; ++it) {
572                 if (it->pos() >= pos)
573                         break;
574         }
575         unsigned int i = std::distance(beg, it);
576         bool notfound = (it == endit);
577
578         if (!notfound && pimpl_->fontlist[i].font() == font)
579                 return;
580
581         bool begin = pos == 0 || notfound ||
582                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
583         // Is position pos is a beginning of a font block?
584         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
585         // Is position pos is the end of a font block?
586         if (begin && end) { // A single char block
587                 if (i + 1 < pimpl_->fontlist.size() &&
588                     pimpl_->fontlist[i + 1].font() == font) {
589                         // Merge the singleton block with the next block
590                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
591                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
592                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
593                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
594                         // Merge the singleton block with the previous block
595                         pimpl_->fontlist[i - 1].pos(pos);
596                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
597                 } else
598                         pimpl_->fontlist[i].font(font);
599         } else if (begin) {
600                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
601                         pimpl_->fontlist[i - 1].pos(pos);
602                 else
603                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
604                                         Pimpl::FontTable(pos, font));
605         } else if (end) {
606                 pimpl_->fontlist[i].pos(pos - 1);
607                 if (!(i + 1 < pimpl_->fontlist.size() &&
608                       pimpl_->fontlist[i + 1].font() == font))
609                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
610                                         Pimpl::FontTable(pos, font));
611         } else { // The general case. The block is splitted into 3 blocks
612                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
613                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
614                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
615                                 Pimpl::FontTable(pos, font));
616         }
617 }
618
619
620 #ifndef NO_NEXT
621 void Paragraph::next(Paragraph * p)
622 {
623         next_ = p;
624 }
625
626
627 // This function is able to hide closed footnotes.
628 Paragraph * Paragraph::next()
629 {
630         return next_;
631 }
632
633
634 Paragraph const * Paragraph::next() const
635 {
636         return next_;
637 }
638
639
640 void Paragraph::previous(Paragraph * p)
641 {
642         previous_ = p;
643 }
644
645
646 // This function is able to hide closed footnotes.
647 Paragraph * Paragraph::previous()
648 {
649         return previous_;
650 }
651
652
653 // This function is able to hide closed footnotes.
654 Paragraph const * Paragraph::previous() const
655 {
656         return previous_;
657 }
658 #endif
659
660
661 void Paragraph::makeSameLayout(Paragraph const * par)
662 {
663         layout(par->layout());
664         // move to pimpl?
665         params() = par->params();
666 }
667
668
669 int Paragraph::stripLeadingSpaces()
670 {
671         if (layout()->free_spacing || isFreeSpacing())
672                 return 0;
673
674         int i = 0;
675         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
676                 pimpl_->eraseIntern(0);
677                 ++i;
678         }
679
680         return i;
681 }
682
683
684 bool Paragraph::hasSameLayout(Paragraph const * par) const
685 {
686         return
687                 par->layout() == layout() &&
688                 params().sameLayout(par->params());
689 }
690
691
692 int Paragraph::getEndLabel() const
693 {
694         Paragraph const * par = this;
695         depth_type par_depth = getDepth();
696         while (par) {
697                 LyXLayout_ptr const & layout = par->layout();
698                 int const endlabeltype = layout->endlabeltype;
699
700                 if (endlabeltype != END_LABEL_NO_LABEL) {
701                         if (!next_)
702                                 return endlabeltype;
703
704                         depth_type const next_depth = next_->getDepth();
705                         if (par_depth > next_depth ||
706                             (par_depth == next_depth
707                              && layout != next_->layout()))
708                                 return endlabeltype;
709                         break;
710                 }
711                 if (par_depth == 0)
712                         break;
713                 par = par->outerHook();
714                 if (par)
715                         par_depth = par->getDepth();
716         }
717         return END_LABEL_NO_LABEL;
718 }
719
720
721 Paragraph::depth_type Paragraph::getDepth() const
722 {
723         return params().depth();
724 }
725
726
727 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
728 {
729         if (layout()->isEnvironment())
730                 return params().depth() + 1;
731         else
732                 return params().depth();
733 }
734
735
736 char Paragraph::getAlign() const
737 {
738         return params().align();
739 }
740
741
742 string const & Paragraph::getLabelstring() const
743 {
744         return params().labelString();
745 }
746
747
748 // the next two functions are for the manual labels
749 string const Paragraph::getLabelWidthString() const
750 {
751         if (!params().labelWidthString().empty())
752                 return params().labelWidthString();
753         else
754                 return _("Senseless with this layout!");
755 }
756
757
758 void Paragraph::setLabelWidthString(string const & s)
759 {
760         params().labelWidthString(s);
761 }
762
763
764 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
765 {
766         layout(new_layout);
767         params().labelWidthString(string());
768         params().align(LYX_ALIGN_LAYOUT);
769         params().spaceTop(VSpace(VSpace::NONE));
770         params().spaceBottom(VSpace(VSpace::NONE));
771         params().spacing(Spacing(Spacing::Default));
772 }
773
774
775 int Paragraph::beginningOfBody() const
776 {
777         if (layout()->labeltype != LABEL_MANUAL)
778                 return 0;
779
780         // Unroll the first two cycles of the loop
781         // and remember the previous character to
782         // remove unnecessary GetChar() calls
783         pos_type i = 0;
784         if (i < size() && !isNewline(i)) {
785                 ++i;
786                 char previous_char = 0;
787                 char temp = 0;
788                 if (i < size()) {
789                         previous_char = getChar(i);
790                         if (!isNewline(i)) {
791                                 ++i;
792                                 while (i < size() && previous_char != ' ') {
793                                         temp = getChar(i);
794                                         if (isNewline(i))
795                                                 break;
796
797                                         ++i;
798                                         previous_char = temp;
799                                 }
800                         }
801                 }
802         }
803
804         return i;
805 }
806
807
808 Paragraph * Paragraph::depthHook(depth_type depth)
809 {
810         Paragraph * newpar = this;
811
812         do {
813                 newpar = newpar->previous();
814         } while (newpar && newpar->getDepth() > depth);
815
816         if (!newpar) {
817                 if (previous() || getDepth())
818                         lyxerr << "ERROR (Paragraph::DepthHook): "
819                                 "no hook." << endl;
820                 newpar = this;
821         }
822
823         return newpar;
824 }
825
826
827 Paragraph const * Paragraph::depthHook(depth_type depth) const
828 {
829         Paragraph const * newpar = this;
830
831         do {
832                 newpar = newpar->previous();
833         } while (newpar && newpar->getDepth() > depth);
834
835         if (!newpar) {
836                 if (previous() || getDepth())
837                         lyxerr << "ERROR (Paragraph::DepthHook): "
838                                 "no hook." << endl;
839                 newpar = this;
840         }
841
842         return newpar;
843 }
844
845
846 Paragraph * Paragraph::outerHook()
847 {
848         if (!getDepth())
849                 return 0;
850         return depthHook(depth_type(getDepth() - 1));
851 }
852
853
854 Paragraph const * Paragraph::outerHook() const
855 {
856         if (!getDepth())
857                 return 0;
858         return depthHook(depth_type(getDepth() - 1));
859 }
860
861
862 // returns -1 if inset not found
863 int Paragraph::getPositionOfInset(Inset const * inset) const
864 {
865         // Find the entry.
866         InsetList::iterator it = insetlist.begin();
867         InsetList::iterator end = insetlist.end();
868         for (; it != end; ++it)
869                 if (it.getInset() == inset)
870                         return it.getPos();
871         return -1;
872 }
873
874
875 InsetBibitem * Paragraph::bibitem()
876 {
877         InsetList::iterator it = insetlist.begin();
878         if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE)
879                 return static_cast<InsetBibitem *>(it.getInset());
880         return 0;
881 }
882
883
884
885 // This could go to ParagraphParameters if we want to
886 int Paragraph::startTeXParParams(BufferParams const & bparams,
887                                  ostream & os, bool moving_arg) const
888 {
889         int column = 0;
890
891         if (params().noindent()) {
892                 os << "\\noindent ";
893                 column += 10;
894         }
895
896         switch (params().align()) {
897         case LYX_ALIGN_NONE:
898         case LYX_ALIGN_BLOCK:
899         case LYX_ALIGN_LAYOUT:
900         case LYX_ALIGN_SPECIAL:
901                 break;
902         case LYX_ALIGN_LEFT:
903         case LYX_ALIGN_RIGHT:
904         case LYX_ALIGN_CENTER:
905                 if (moving_arg) {
906                         os << "\\protect";
907                         column = 8;
908                 }
909                 break;
910         }
911
912         switch (params().align()) {
913         case LYX_ALIGN_NONE:
914         case LYX_ALIGN_BLOCK:
915         case LYX_ALIGN_LAYOUT:
916         case LYX_ALIGN_SPECIAL:
917                 break;
918         case LYX_ALIGN_LEFT:
919                 if (getParLanguage(bparams)->babel() != "hebrew") {
920                         os << "\\begin{flushleft}";
921                         column += 17;
922                 } else {
923                         os << "\\begin{flushright}";
924                         column += 18;
925                 }
926                 break;
927         case LYX_ALIGN_RIGHT:
928                 if (getParLanguage(bparams)->babel() != "hebrew") {
929                         os << "\\begin{flushright}";
930                         column += 18;
931                 } else {
932                         os << "\\begin{flushleft}";
933                         column += 17;
934                 }
935                 break;
936         case LYX_ALIGN_CENTER:
937                 os << "\\begin{center}";
938                 column += 14;
939                 break;
940         }
941
942         return column;
943 }
944
945
946 // This could go to ParagraphParameters if we want to
947 int Paragraph::endTeXParParams(BufferParams const & bparams,
948                                ostream & os, bool moving_arg) const
949 {
950         int column = 0;
951
952         switch (params().align()) {
953         case LYX_ALIGN_NONE:
954         case LYX_ALIGN_BLOCK:
955         case LYX_ALIGN_LAYOUT:
956         case LYX_ALIGN_SPECIAL:
957                 break;
958         case LYX_ALIGN_LEFT:
959         case LYX_ALIGN_RIGHT:
960         case LYX_ALIGN_CENTER:
961                 if (moving_arg) {
962                         os << "\\protect";
963                         column = 8;
964                 }
965                 break;
966         }
967
968         switch (params().align()) {
969         case LYX_ALIGN_NONE:
970         case LYX_ALIGN_BLOCK:
971         case LYX_ALIGN_LAYOUT:
972         case LYX_ALIGN_SPECIAL:
973                 break;
974         case LYX_ALIGN_LEFT:
975                 if (getParLanguage(bparams)->babel() != "hebrew") {
976                         os << "\\end{flushleft}";
977                         column = 15;
978                 } else {
979                         os << "\\end{flushright}";
980                         column = 16;
981                 }
982                 break;
983         case LYX_ALIGN_RIGHT:
984                 if (getParLanguage(bparams)->babel() != "hebrew") {
985                         os << "\\end{flushright}";
986                         column+= 16;
987                 } else {
988                         os << "\\end{flushleft}";
989                         column = 15;
990                 }
991                 break;
992         case LYX_ALIGN_CENTER:
993                 os << "\\end{center}";
994                 column = 12;
995                 break;
996         }
997         return column;
998 }
999
1000
1001 // This one spits out the text of the paragraph
1002 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1003                                 BufferParams const & bparams,
1004                                 ostream & os, TexRow & texrow,
1005                                 bool moving_arg)
1006 {
1007         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1008
1009         bool return_value = false;
1010
1011         LyXLayout_ptr style;
1012
1013         // well we have to check if we are in an inset with unlimited
1014         // lenght (all in one row) if that is true then we don't allow
1015         // any special options in the paragraph and also we don't allow
1016         // any environment other then "Standard" to be valid!
1017         bool asdefault =
1018                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1019
1020         if (asdefault) {
1021                 style = bparams.getLyXTextClass().defaultLayout();
1022         } else {
1023                 style = layout();
1024         }
1025
1026         LyXFont basefont;
1027
1028         // Maybe we have to create a optional argument.
1029         pos_type body_pos;
1030
1031         // FIXME: can we actually skip this check and just call
1032         // beginningOfBody() ??
1033         if (style->labeltype != LABEL_MANUAL) {
1034                 body_pos = 0;
1035         } else {
1036                 body_pos = beginningOfBody();
1037         }
1038
1039         unsigned int column = 0;
1040
1041         if (body_pos > 0) {
1042                 os << '[';
1043                 ++column;
1044                 basefont = getLabelFont(bparams);
1045         } else {
1046                 basefont = getLayoutFont(bparams);
1047         }
1048
1049         moving_arg |= style->needprotect;
1050
1051         // Which font is currently active?
1052         LyXFont running_font(basefont);
1053         // Do we have an open font change?
1054         bool open_font = false;
1055
1056         Change::Type running_change = Change::UNCHANGED;
1057
1058         texrow.start(this, 0);
1059
1060         // if the paragraph is empty, the loop will not be entered at all
1061         if (empty()) {
1062                 if (style->isCommand()) {
1063                         os << '{';
1064                         ++column;
1065                 }
1066                 if (!asdefault)
1067                         column += startTeXParParams(bparams, os, moving_arg);
1068
1069         }
1070
1071         for (pos_type i = 0; i < size(); ++i) {
1072                 ++column;
1073                 // First char in paragraph or after label?
1074                 if (i == body_pos) {
1075                         if (body_pos > 0) {
1076                                 if (open_font) {
1077                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1078                                         open_font = false;
1079                                 }
1080                                 basefont = getLayoutFont(bparams);
1081                                 running_font = basefont;
1082                                 os << ']';
1083                                 ++column;
1084                         }
1085                         if (style->isCommand()) {
1086                                 os << '{';
1087                                 ++column;
1088                         }
1089
1090                         if (!asdefault)
1091                                 column += startTeXParParams(bparams, os,
1092                                                             moving_arg);
1093                 }
1094
1095                 value_type c = getChar(i);
1096
1097                 // Fully instantiated font
1098                 LyXFont font = getFont(bparams, i);
1099
1100                 LyXFont const last_font = running_font;
1101
1102                 // Spaces at end of font change are simulated to be
1103                 // outside font change, i.e. we write "\textXX{text} "
1104                 // rather than "\textXX{text }". (Asger)
1105                 if (open_font && c == ' ' && i <= size() - 2) {
1106                         LyXFont const & next_font = getFont(bparams, i + 1);
1107                         if (next_font != running_font
1108                             && next_font != font) {
1109                                 font = next_font;
1110                         }
1111                 }
1112
1113                 // We end font definition before blanks
1114                 if (open_font &&
1115                     (font != running_font ||
1116                      font.language() != running_font.language()))
1117                 {
1118                         column += running_font.latexWriteEndChanges(os,
1119                                                                     basefont,
1120                                                                     (i == body_pos-1) ? basefont : font);
1121                         running_font = basefont;
1122                         open_font = false;
1123                 }
1124
1125                 // Blanks are printed before start of fontswitch
1126                 if (c == ' ') {
1127                         // Do not print the separation of the optional argument
1128                         if (i != body_pos - 1) {
1129                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1130                                                        column, font, *style);
1131                         }
1132                 }
1133
1134                 // Do we need to change font?
1135                 if ((font != running_font ||
1136                      font.language() != running_font.language()) &&
1137                         i != body_pos - 1)
1138                 {
1139                         column += font.latexWriteStartChanges(os, basefont,
1140                                                               last_font);
1141                         running_font = font;
1142                         open_font = true;
1143                 }
1144
1145                 Change::Type change = pimpl_->lookupChange(i);
1146
1147                 column += Changes::latexMarkChange(os, running_change, change);
1148                 running_change = change;
1149
1150                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1151                                               os, texrow, moving_arg,
1152                                               font, running_font,
1153                                               basefont, open_font,
1154                                               running_change,
1155                                               *style, i, column, c);
1156         }
1157
1158         column += Changes::latexMarkChange(os,
1159                         running_change, Change::UNCHANGED);
1160
1161         // If we have an open font definition, we have to close it
1162         if (open_font) {
1163 #ifdef FIXED_LANGUAGE_END_DETECTION
1164                 if (next_) {
1165                         running_font
1166                                 .latexWriteEndChanges(os, basefont,
1167                                                       next_->getFont(bparams,
1168                                                       0));
1169                 } else {
1170                         running_font.latexWriteEndChanges(os, basefont,
1171                                                           basefont);
1172                 }
1173 #else
1174 #ifdef WITH_WARNINGS
1175 //#warning For now we ALWAYS have to close the foreign font settings if they are
1176 //#warning there as we start another \selectlanguage with the next paragraph if
1177 //#warning we are in need of this. This should be fixed sometime (Jug)
1178 #endif
1179                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1180 #endif
1181         }
1182
1183         // Needed if there is an optional argument but no contents.
1184         if (body_pos > 0 && body_pos == size()) {
1185                 os << "]~";
1186                 return_value = false;
1187         }
1188
1189         if (!asdefault) {
1190                 column += endTeXParParams(bparams, os, moving_arg);
1191         }
1192
1193         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1194         return return_value;
1195 }
1196
1197
1198
1199
1200 bool Paragraph::isHfill(pos_type pos) const
1201 {
1202         return IsInsetChar(getChar(pos))
1203                && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
1204 }
1205
1206
1207 bool Paragraph::isInset(pos_type pos) const
1208 {
1209         return IsInsetChar(getChar(pos));
1210 }
1211
1212
1213 bool Paragraph::isNewline(pos_type pos) const
1214 {
1215         return IsInsetChar(getChar(pos))
1216                && getInset(pos)->lyxCode() == Inset::NEWLINE_CODE;
1217 }
1218
1219
1220 bool Paragraph::isSeparator(pos_type pos) const
1221 {
1222         return IsSeparatorChar(getChar(pos));
1223 }
1224
1225
1226 bool Paragraph::isLineSeparator(pos_type pos) const
1227 {
1228         value_type const c = getChar(pos);
1229         return IsLineSeparatorChar(c)
1230                 || (IsInsetChar(c) && getInset(pos) &&
1231                 getInset(pos)->isLineSeparator());
1232 }
1233
1234
1235 bool Paragraph::isKomma(pos_type pos) const
1236 {
1237         return IsKommaChar(getChar(pos));
1238 }
1239
1240
1241 /// Used by the spellchecker
1242 bool Paragraph::isLetter(pos_type pos) const
1243 {
1244         value_type const c = getChar(pos);
1245         if (IsLetterChar(c))
1246                 return true;
1247         if (isInset(pos))
1248                 return getInset(pos)->isLetter();
1249         // We want to pass the ' and escape chars to ispell
1250         string const extra = lyxrc.isp_esc_chars + '\'';
1251         return contains(extra, c);
1252 }
1253
1254
1255 bool Paragraph::isWord(pos_type pos) const
1256 {
1257         return IsWordChar(getChar(pos)) ;
1258 }
1259
1260
1261 Language const *
1262 Paragraph::getParLanguage(BufferParams const & bparams) const
1263 {
1264         if (!empty()) {
1265                 return getFirstFontSettings().language();
1266         } else if (previous_)
1267                 return previous_->getParLanguage(bparams);
1268         else
1269                 return bparams.language;
1270 }
1271
1272
1273 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1274 {
1275         return lyxrc.rtl_support
1276                 && getParLanguage(bparams)->RightToLeft()
1277                 && !(inInset() && inInset()->owner() &&
1278                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1279 }
1280
1281
1282 void Paragraph::changeLanguage(BufferParams const & bparams,
1283                                   Language const * from, Language const * to)
1284 {
1285         for (pos_type i = 0; i < size(); ++i) {
1286                 LyXFont font = getFontSettings(bparams, i);
1287                 if (font.language() == from) {
1288                         font.setLanguage(to);
1289                         setFont(i, font);
1290                 }
1291         }
1292 }
1293
1294
1295 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1296 {
1297         Language const * doc_language = bparams.language;
1298         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1299         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1300
1301         for (; cit != end; ++cit)
1302                 if (cit->font().language() != ignore_language &&
1303                     cit->font().language() != latex_language &&
1304                         cit->font().language() != doc_language)
1305                         return true;
1306         return false;
1307 }
1308
1309
1310 // Convert the paragraph to a string.
1311 // Used for building the table of contents
1312 string const Paragraph::asString(Buffer const * buffer, bool label) const
1313 {
1314         string s;
1315         if (label && !params().labelString().empty())
1316                 s += params().labelString() + ' ';
1317
1318         for (pos_type i = 0; i < size(); ++i) {
1319                 value_type c = getChar(i);
1320                 if (IsPrintable(c))
1321                         s += c;
1322                 else if (c == META_INSET &&
1323                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
1324                         ostringstream ost;
1325                         getInset(i)->ascii(buffer, ost);
1326                         s += subst(STRCONV(ost.str()),'\n',' ');
1327                 }
1328         }
1329
1330         return s;
1331 }
1332
1333
1334 string const Paragraph::asString(Buffer const * buffer,
1335                                  pos_type beg, pos_type end, bool label) const
1336 {
1337         ostringstream os;
1338
1339         if (beg == 0 && label && !params().labelString().empty())
1340                 os << params().labelString() << ' ';
1341
1342         for (pos_type i = beg; i < end; ++i) {
1343                 value_type const c = getUChar(buffer->params, i);
1344                 if (IsPrintable(c))
1345                         os << c;
1346                 else if (c == META_INSET)
1347                         getInset(i)->ascii(buffer, os);
1348         }
1349
1350         return STRCONV(os.str());
1351 }
1352
1353
1354 void Paragraph::setInsetOwner(Inset * i)
1355 {
1356         pimpl_->inset_owner = i;
1357         InsetList::iterator it = insetlist.begin();
1358         InsetList::iterator end = insetlist.end();
1359         for (; it != end; ++it)
1360                 if (it.getInset())
1361                         it.getInset()->setOwner(i);
1362 }
1363
1364
1365 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1366 {
1367         // then the insets
1368         insetlist.deleteInsetsLyXText(bv);
1369 }
1370
1371
1372 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1373 {
1374         // then the insets
1375         insetlist.resizeInsetsLyXText(bv);
1376 }
1377
1378
1379 void Paragraph::setContentsFromPar(Paragraph * par)
1380 {
1381         pimpl_->setContentsFromPar(par);
1382 }
1383
1384
1385 void Paragraph::trackChanges(Change::Type type)
1386 {
1387         pimpl_->trackChanges(type);
1388 }
1389
1390
1391 void Paragraph::untrackChanges()
1392 {
1393         pimpl_->untrackChanges();
1394 }
1395
1396
1397 void Paragraph::cleanChanges()
1398 {
1399         pimpl_->cleanChanges();
1400 }
1401
1402
1403 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1404 {
1405         lyx::Assert(!size() || pos < size());
1406         return pimpl_->lookupChange(pos);
1407 }
1408
1409
1410 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1411 {
1412         lyx::Assert(!size() || pos < size());
1413         return pimpl_->lookupChangeFull(pos);
1414 }
1415
1416
1417 bool Paragraph::isChanged(pos_type start, pos_type end) const
1418 {
1419         return pimpl_->isChanged(start, end);
1420 }
1421
1422
1423 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1424 {
1425         return pimpl_->isChangeEdited(start, end);
1426 }
1427
1428
1429 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1430 {
1431         pimpl_->setChange(pos, type);
1432
1433 }
1434
1435
1436 void Paragraph::markErased()
1437 {
1438         pimpl_->markErased();
1439 }
1440
1441
1442 void Paragraph::acceptChange(pos_type start, pos_type end)
1443 {
1444         return pimpl_->acceptChange(start, end);
1445 }
1446
1447
1448 void Paragraph::rejectChange(pos_type start, pos_type end)
1449 {
1450         return pimpl_->rejectChange(start, end);
1451 }
1452
1453
1454 lyx::pos_type Paragraph::size() const
1455 {
1456         return pimpl_->size();
1457 }
1458
1459
1460 bool Paragraph::empty() const
1461 {
1462         return pimpl_->empty();
1463 }
1464
1465
1466 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1467 {
1468         return pimpl_->getChar(pos);
1469 }
1470
1471
1472 int Paragraph::id() const
1473 {
1474         return pimpl_->id_;
1475 }
1476
1477
1478 LyXLayout_ptr const & Paragraph::layout() const
1479 {
1480         return layout_;
1481 }
1482
1483
1484 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1485 {
1486         layout_ = new_layout;
1487 }
1488
1489
1490 bool Paragraph::isFirstInSequence() const
1491 {
1492         Paragraph const * dhook = depthHook(getDepth());
1493         return (dhook == this
1494                 || dhook->layout() != layout()
1495                 || dhook->getDepth() != getDepth());
1496 }
1497
1498
1499 Inset * Paragraph::inInset() const
1500 {
1501         return pimpl_->inset_owner;
1502 }
1503
1504
1505 void Paragraph::clearContents()
1506 {
1507         pimpl_->clear();
1508 }
1509
1510 void Paragraph::setChar(pos_type pos, value_type c)
1511 {
1512         pimpl_->setChar(pos, c);
1513 }
1514
1515
1516 ParagraphParameters & Paragraph::params()
1517 {
1518         return pimpl_->params;
1519 }
1520
1521
1522 ParagraphParameters const & Paragraph::params() const
1523 {
1524         return pimpl_->params;
1525 }
1526
1527
1528 bool Paragraph::isFreeSpacing() const
1529 {
1530         // for now we just need this, later should we need this in some
1531         // other way we can always add a function to Inset::() too.
1532         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1533                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
1534         return false;
1535 }