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