]> git.lyx.org Git - lyx.git/blob - src/Paragraph.cpp
Buffer::resetChildDocuments() Check for Buffer emptiness before proceeding.
[lyx.git] / src / Paragraph.cpp
1 /**
2  * \file Paragraph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "Paragraph.h"
21
22 #include "Buffer.h"
23 #include "BufferParams.h"
24 #include "Changes.h"
25 #include "Counters.h"
26 #include "Encoding.h"
27 #include "InsetList.h"
28 #include "Language.h"
29 #include "LaTeXFeatures.h"
30 #include "Layout.h"
31 #include "Length.h"
32 #include "Font.h"
33 #include "FontList.h"
34 #include "LyXRC.h"
35 #include "OutputParams.h"
36 #include "output_latex.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "sgml.h"
40 #include "TextClass.h"
41 #include "TexRow.h"
42 #include "VSpace.h"
43
44 #include "frontends/alert.h"
45
46 #include "insets/InsetBibitem.h"
47 #include "insets/InsetLabel.h"
48
49 #include "support/convert.h"
50 #include "support/debug.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
53 #include "support/Messages.h"
54 #include "support/textutils.h"
55
56 #include <sstream>
57 #include <vector>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 namespace lyx {
63
64 namespace {
65 /// Inset identifier (above 0x10ffff, for ucs-4)
66 char_type const META_INSET = 0x200001;
67 };
68
69 /////////////////////////////////////////////////////////////////////
70 //
71 // Paragraph::Private
72 //
73 /////////////////////////////////////////////////////////////////////
74
75 class Paragraph::Private
76 {
77 public:
78         ///
79         Private(Paragraph * owner);
80         /// "Copy constructor"
81         Private(Private const &, Paragraph * owner);
82
83         ///
84         void insertChar(pos_type pos, char_type c, Change const & change);
85
86         /// Output the surrogate pair formed by \p c and \p next to \p os.
87         /// \return the number of characters written.
88         int latexSurrogatePair(odocstream & os, char_type c, char_type next,
89                                Encoding const &);
90
91         /// Output a space in appropriate formatting (or a surrogate pair
92         /// if the next character is a combining character).
93         /// \return whether a surrogate pair was output.
94         bool simpleTeXBlanks(OutputParams const &,
95                              odocstream &, TexRow & texrow,
96                              pos_type i,
97                              unsigned int & column,
98                              Font const & font,
99                              Layout const & style);
100
101         /// Output consecutive unicode chars, belonging to the same script as
102         /// specified by the latex macro \p ltx, to \p os starting from \p i.
103         /// \return the number of characters written.
104         int writeScriptChars(odocstream & os, docstring const & ltx,
105                            Change &, Encoding const &, pos_type & i);
106
107         /// This could go to ParagraphParameters if we want to.
108         int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
109                               bool) const;
110
111         /// This could go to ParagraphParameters if we want to.
112         int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
113                             bool) const;
114
115         ///
116         void latexInset(Buffer const &, BufferParams const &,
117                                    odocstream &,
118                                    TexRow & texrow, OutputParams &,
119                                    Font & running_font,
120                                    Font & basefont,
121                                    Font const & outerfont,
122                                    bool & open_font,
123                                    Change & running_change,
124                                    Layout const & style,
125                                    pos_type & i,
126                                    unsigned int & column);
127
128         ///
129         void latexSpecialChar(
130                                    odocstream & os,
131                                    OutputParams & runparams,
132                                    Font & running_font,
133                                    Change & running_change,
134                                    Layout const & style,
135                                    pos_type & i,
136                                    unsigned int & column);
137
138         ///
139         bool latexSpecialT1(
140                 char_type const c,
141                 odocstream & os,
142                 pos_type & i,
143                 unsigned int & column);
144         ///
145         bool latexSpecialTypewriter(
146                 char_type const c,
147                 odocstream & os,
148                 pos_type & i,
149                 unsigned int & column);
150         ///
151         bool latexSpecialPhrase(
152                 odocstream & os,
153                 pos_type & i,
154                 unsigned int & column,
155                 OutputParams & runparams);
156
157         ///
158         void validate(LaTeXFeatures & features,
159                       Layout const & layout) const;
160
161         /// Checks if the paragraph contains only text and no inset or font change.
162         bool onlyText(Buffer const & buf, Font const & outerfont,
163                       pos_type initial) const;
164
165         /// match a string against a particular point in the paragraph
166         bool isTextAt(string const & str, pos_type pos) const;
167         
168         /// Which Paragraph owns us?
169         Paragraph * owner_;
170
171         /// In which Inset?
172         Inset * inset_owner_;
173
174         ///
175         FontList fontlist_;
176
177         ///
178         unsigned int id_;
179         ///
180         static unsigned int paragraph_id;
181         ///
182         ParagraphParameters params_;
183
184         /// for recording and looking up changes
185         Changes changes_;
186
187         ///
188         InsetList insetlist_;
189
190         ///
191         LayoutPtr layout_;
192
193         /// end of label
194         pos_type begin_of_body_;
195
196         typedef docstring TextContainer;
197         ///
198         TextContainer text_;
199 };
200
201
202 // Initialization of the counter for the paragraph id's,
203 unsigned int Paragraph::Private::paragraph_id = 0;
204
205 namespace {
206
207 struct special_phrase {
208         string phrase;
209         docstring macro;
210         bool builtin;
211 };
212
213 special_phrase const special_phrases[] = {
214         { "LyX", from_ascii("\\LyX{}"), false },
215         { "TeX", from_ascii("\\TeX{}"), true },
216         { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
217         { "LaTeX", from_ascii("\\LaTeX{}"), true },
218 };
219
220 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
221
222 } // namespace anon
223
224
225 Paragraph::Private::Private(Paragraph * owner)
226         : owner_(owner), inset_owner_(0), begin_of_body_(0)
227 {
228         id_ = paragraph_id++;
229         text_.reserve(100);
230 }
231
232
233 Paragraph::Private::Private(Private const & p, Paragraph * owner)
234         : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_), 
235           params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
236           layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_)
237 {
238         id_ = paragraph_id++;
239 }
240
241
242 bool Paragraph::isChanged(pos_type start, pos_type end) const
243 {
244         BOOST_ASSERT(start >= 0 && start <= size());
245         BOOST_ASSERT(end > start && end <= size() + 1);
246
247         return d->changes_.isChanged(start, end);
248 }
249
250
251 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
252 {
253         // keep the logic here in sync with the logic of eraseChars()
254         if (!trackChanges)
255                 return true;
256
257         Change const change = d->changes_.lookup(size());
258         return change.type == Change::INSERTED && change.author == 0;
259 }
260
261
262 void Paragraph::setChange(Change const & change)
263 {
264         // beware of the imaginary end-of-par character!
265         d->changes_.set(change, 0, size() + 1);
266
267         /*
268          * Propagate the change recursively - but not in case of DELETED!
269          *
270          * Imagine that your co-author makes changes in an existing inset. He
271          * sends your document to you and you come to the conclusion that the
272          * inset should go completely. If you erase it, LyX must not delete all
273          * text within the inset. Otherwise, the change tracked insertions of
274          * your co-author get lost and there is no way to restore them later.
275          *
276          * Conclusion: An inset's content should remain untouched if you delete it
277          */
278
279         if (change.type != Change::DELETED) {
280                 for (pos_type pos = 0; pos < size(); ++pos) {
281                         if (Inset * inset = getInset(pos))
282                                 inset->setChange(change);
283                 }
284         }
285 }
286
287
288 void Paragraph::setChange(pos_type pos, Change const & change)
289 {
290         BOOST_ASSERT(pos >= 0 && pos <= size());
291         d->changes_.set(change, pos);
292
293         // see comment in setChange(Change const &) above
294         if (change.type != Change::DELETED && pos < size())
295                         if (Inset * inset = getInset(pos))
296                                 inset->setChange(change);
297 }
298
299
300 Change const & Paragraph::lookupChange(pos_type pos) const
301 {
302         BOOST_ASSERT(pos >= 0 && pos <= size());
303         return d->changes_.lookup(pos);
304 }
305
306
307 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start,
308                 pos_type end)
309 {
310         BOOST_ASSERT(start >= 0 && start <= size());
311         BOOST_ASSERT(end > start && end <= size() + 1);
312
313         for (pos_type pos = start; pos < end; ++pos) {
314                 switch (lookupChange(pos).type) {
315                         case Change::UNCHANGED:
316                                 // accept changes in nested inset
317                                 if (Inset * inset = getInset(pos))
318                                         inset->acceptChanges(bparams);
319                                 break;
320
321                         case Change::INSERTED:
322                                 d->changes_.set(Change(Change::UNCHANGED), pos);
323                                 // also accept changes in nested inset
324                                 if (Inset * inset = getInset(pos))
325                                         inset->acceptChanges(bparams);
326                                 break;
327
328                         case Change::DELETED:
329                                 // Suppress access to non-existent
330                                 // "end-of-paragraph char"
331                                 if (pos < size()) {
332                                         eraseChar(pos, false);
333                                         --end;
334                                         --pos;
335                                 }
336                                 break;
337                 }
338
339         }
340 }
341
342
343 void Paragraph::rejectChanges(BufferParams const & bparams,
344                 pos_type start, pos_type end)
345 {
346         BOOST_ASSERT(start >= 0 && start <= size());
347         BOOST_ASSERT(end > start && end <= size() + 1);
348
349         for (pos_type pos = start; pos < end; ++pos) {
350                 switch (lookupChange(pos).type) {
351                         case Change::UNCHANGED:
352                                 // reject changes in nested inset
353                                 if (Inset * inset = getInset(pos))
354                                                 inset->rejectChanges(bparams);
355                                 break;
356
357                         case Change::INSERTED:
358                                 // Suppress access to non-existent
359                                 // "end-of-paragraph char"
360                                 if (pos < size()) {
361                                         eraseChar(pos, false);
362                                         --end;
363                                         --pos;
364                                 }
365                                 break;
366
367                         case Change::DELETED:
368                                 d->changes_.set(Change(Change::UNCHANGED), pos);
369
370                                 // Do NOT reject changes within a deleted inset!
371                                 // There may be insertions of a co-author inside of it!
372
373                                 break;
374                 }
375         }
376 }
377
378
379 void Paragraph::Private::insertChar(pos_type pos, char_type c,
380                 Change const & change)
381 {
382         BOOST_ASSERT(pos >= 0 && pos <= int(text_.size()));
383
384         // track change
385         changes_.insert(change, pos);
386
387         // This is actually very common when parsing buffers (and
388         // maybe inserting ascii text)
389         if (pos == pos_type(text_.size())) {
390                 // when appending characters, no need to update tables
391                 text_.push_back(c);
392                 return;
393         }
394
395         text_.insert(text_.begin() + pos, c);
396
397         // Update the font table.
398         fontlist_.increasePosAfterPos(pos);
399
400         // Update the insets
401         insetlist_.increasePosAfterPos(pos);
402 }
403
404
405 void Paragraph::insertInset(pos_type pos, Inset * inset,
406                                    Change const & change)
407 {
408         BOOST_ASSERT(inset);
409         BOOST_ASSERT(pos >= 0 && pos <= size());
410
411         d->insertChar(pos, META_INSET, change);
412         BOOST_ASSERT(d->text_[pos] == META_INSET);
413
414         // Add a new entry in the insetlist_.
415         d->insetlist_.insert(inset, pos);
416 }
417
418
419 bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
420 {
421         BOOST_ASSERT(pos >= 0 && pos <= size());
422
423         // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
424
425         if (trackChanges) {
426                 Change change = d->changes_.lookup(pos);
427
428                 // set the character to DELETED if
429                 //  a) it was previously unchanged or
430                 //  b) it was inserted by a co-author
431
432                 if (change.type == Change::UNCHANGED ||
433                     (change.type == Change::INSERTED && change.author != 0)) {
434                         setChange(pos, Change(Change::DELETED));
435                         return false;
436                 }
437
438                 if (change.type == Change::DELETED)
439                         return false;
440         }
441
442         // Don't physically access the imaginary end-of-paragraph character.
443         // eraseChar() can only mark it as DELETED. A physical deletion of
444         // end-of-par must be handled externally.
445         if (pos == size()) {
446                 return false;
447         }
448
449         // track change
450         d->changes_.erase(pos);
451
452         // if it is an inset, delete the inset entry
453         if (d->text_[pos] == META_INSET)
454                 d->insetlist_.erase(pos);
455
456         d->text_.erase(d->text_.begin() + pos);
457
458         // Update the fontlist_
459         d->fontlist_.erase(pos);
460
461         // Update the insetlist_
462         d->insetlist_.decreasePosAfterPos(pos);
463
464         return true;
465 }
466
467
468 int Paragraph::eraseChars(pos_type start, pos_type end, bool trackChanges)
469 {
470         BOOST_ASSERT(start >= 0 && start <= size());
471         BOOST_ASSERT(end >= start && end <= size() + 1);
472
473         pos_type i = start;
474         for (pos_type count = end - start; count; --count) {
475                 if (!eraseChar(i, trackChanges))
476                         ++i;
477         }
478         return end - i;
479 }
480
481
482 int Paragraph::Private::latexSurrogatePair(odocstream & os, char_type c,
483                 char_type next, Encoding const & encoding)
484 {
485         // Writing next here may circumvent a possible font change between
486         // c and next. Since next is only output if it forms a surrogate pair
487         // with c we can ignore this:
488         // A font change inside a surrogate pair does not make sense and is
489         // hopefully impossible to input.
490         // FIXME: change tracking
491         // Is this correct WRT change tracking?
492         docstring const latex1 = encoding.latexChar(next);
493         docstring const latex2 = encoding.latexChar(c);
494         os << latex1 << '{' << latex2 << '}';
495         return latex1.length() + latex2.length() + 2;
496 }
497
498
499 bool Paragraph::Private::simpleTeXBlanks(OutputParams const & runparams,
500                                        odocstream & os, TexRow & texrow,
501                                        pos_type i,
502                                        unsigned int & column,
503                                        Font const & font,
504                                        Layout const & style)
505 {
506         if (style.pass_thru || runparams.verbatim)
507                 return false;
508
509         if (i + 1 < int(text_.size())) {
510                 char_type next = text_[i + 1];
511                 if (Encodings::isCombiningChar(next)) {
512                         Encoding const & encoding = *(runparams.encoding);
513                         // This space has an accent, so we must always output it.
514                         column += latexSurrogatePair(os, ' ', next, encoding) - 1;
515                         return true;
516                 }
517         }
518
519         if (lyxrc.plaintext_linelen > 0
520             && column > lyxrc.plaintext_linelen
521             && i
522             && text_[i - 1] != ' '
523             && (i + 1 < int(text_.size()))
524             // same in FreeSpacing mode
525             && !owner_->isFreeSpacing()
526             // In typewriter mode, we want to avoid
527             // ! . ? : at the end of a line
528             && !(font.fontInfo().family() == TYPEWRITER_FAMILY
529                  && (text_[i - 1] == '.'
530                      || text_[i - 1] == '?'
531                      || text_[i - 1] == ':'
532                      || text_[i - 1] == '!'))) {
533                 os << '\n';
534                 texrow.newline();
535                 texrow.start(owner_->id(), i + 1);
536                 column = 0;
537         } else if (style.free_spacing) {
538                 os << '~';
539         } else {
540                 os << ' ';
541         }
542         return false;
543 }
544
545
546 int Paragraph::Private::writeScriptChars(odocstream & os,
547                                          docstring const & ltx,
548                                          Change & runningChange,
549                                          Encoding const & encoding,
550                                          pos_type & i)
551 {
552         // FIXME: modifying i here is not very nice...
553
554         // We only arrive here when a proper language for character text_[i] has
555         // not been specified (i.e., it could not be translated in the current
556         // latex encoding) and it belongs to a known script.
557         // Parameter ltx contains the latex translation of text_[i] as specified in
558         // the unicodesymbols file and is something like "\textXXX{<spec>}".
559         // The latex macro name "textXXX" specifies the script to which text_[i]
560         // belongs and we use it in order to check whether characters from the
561         // same script immediately follow, such that we can collect them in a
562         // single "\textXXX" macro. So, we have to retain "\textXXX{<spec>"
563         // for the first char but only "<spec>" for all subsequent chars.
564         docstring::size_type const brace1 = ltx.find_first_of(from_ascii("{"));
565         docstring::size_type const brace2 = ltx.find_last_of(from_ascii("}"));
566         string script = to_ascii(ltx.substr(1, brace1 - 1));
567         int length = ltx.substr(0, brace2).length();
568         os << ltx.substr(0, brace2);
569         int size = text_.size();
570         while (i + 1 < size) {
571                 char_type const next = text_[i + 1];
572                 // Stop here if next character belongs to another script
573                 // or there is a change in change tracking status.
574                 if (!Encodings::isKnownScriptChar(next, script) ||
575                     runningChange != owner_->lookupChange(i + 1))
576                         break;
577                 Font prev_font;
578                 bool found = false;
579                 FontList::const_iterator cit = fontlist_.begin();
580                 FontList::const_iterator end = fontlist_.end();
581                 for (; cit != end; ++cit) {
582                         if (cit->pos() >= i && !found) {
583                                 prev_font = cit->font();
584                                 found = true;
585                         }
586                         if (cit->pos() >= i + 1)
587                                 break;
588                 }
589                 // Stop here if there is a font attribute or encoding change.
590                 if (found && cit != end && prev_font != cit->font())
591                         break;
592                 docstring const latex = encoding.latexChar(next);
593                 docstring::size_type const b1 =
594                                         latex.find_first_of(from_ascii("{"));
595                 docstring::size_type const b2 =
596                                         latex.find_last_of(from_ascii("}"));
597                 int const len = b2 - b1 - 1;
598                 os << latex.substr(b1 + 1, len);
599                 length += len;
600                 ++i;
601         }
602         os << '}';
603         ++length;
604         return length;
605 }
606
607
608 bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
609 {
610         pos_type const len = str.length();
611
612         // is the paragraph large enough?
613         if (pos + len > int(text_.size()))
614                 return false;
615
616         // does the wanted text start at point?
617         for (string::size_type i = 0; i < str.length(); ++i) {
618                 // Caution: direct comparison of characters works only
619                 // because str is pure ASCII.
620                 if (str[i] != text_[pos + i])
621                         return false;
622         }
623
624         return fontlist_.hasChangeInRange(pos, len);
625 }
626
627
628 void Paragraph::Private::latexInset(Buffer const & buf,
629                                              BufferParams const & bparams,
630                                              odocstream & os,
631                                              TexRow & texrow,
632                                              OutputParams & runparams,
633                                              Font & running_font,
634                                              Font & basefont,
635                                              Font const & outerfont,
636                                              bool & open_font,
637                                              Change & running_change,
638                                              Layout const & style,
639                                              pos_type & i,
640                                              unsigned int & column)
641 {
642         Inset * inset = owner_->getInset(i);
643         BOOST_ASSERT(inset);
644
645         if (style.pass_thru) {
646                 inset->plaintext(buf, os, runparams);
647                 return;
648         }
649
650         // FIXME: move this to InsetNewline::latex
651         if (inset->lyxCode() == NEWLINE_CODE) {
652                 // newlines are handled differently here than
653                 // the default in simpleTeXSpecialChars().
654                 if (!style.newline_allowed) {
655                         os << '\n';
656                 } else {
657                         if (open_font) {
658                                 column += running_font.latexWriteEndChanges(
659                                         os, bparams, runparams,
660                                         basefont, basefont);
661                                 open_font = false;
662                         }
663
664                         if (running_font.fontInfo().family() == TYPEWRITER_FAMILY)
665                                 os << '~';
666
667                         basefont = owner_->getLayoutFont(bparams, outerfont);
668                         running_font = basefont;
669
670                         if (runparams.moving_arg)
671                                 os << "\\protect ";
672
673                 }
674                 texrow.newline();
675                 texrow.start(owner_->id(), i + 1);
676                 column = 0;
677         }
678
679         if (owner_->lookupChange(i).type == Change::DELETED) {
680                 if( ++runparams.inDeletedInset == 1)
681                         runparams.changeOfDeletedInset = owner_->lookupChange(i);
682         }
683
684         if (inset->canTrackChanges()) {
685                 column += Changes::latexMarkChange(os, bparams, running_change,
686                         Change(Change::UNCHANGED));
687                 running_change = Change(Change::UNCHANGED);
688         }
689
690         bool close = false;
691         odocstream::pos_type const len = os.tellp();
692
693         if (inset->forceLTR() 
694             && running_font.isRightToLeft()
695                 // ERT is an exception, it should be output with no decorations at all
696                 && inset->lyxCode() != ERT_CODE) {
697                 if (running_font.language()->lang() == "farsi")
698                         os << "\\beginL{}";
699                 else
700                         os << "\\L{";
701                 close = true;
702         }
703
704         // FIXME: Bug: we can have an empty font change here!
705         // if there has just been a font change, we are going to close it
706         // right now, which means stupid latex code like \textsf{}. AFAIK,
707         // this does not harm dvi output. A minor bug, thus (JMarc)
708
709         // Some insets cannot be inside a font change command.
710         // However, even such insets *can* be placed in \L or \R
711         // or their equivalents (for RTL language switches), so we don't
712         // close the language in those cases.
713         // ArabTeX, though, cannot handle this special behavior, it seems.
714         bool arabtex = basefont.language()->lang() == "arabic_arabtex"
715                 || running_font.language()->lang() == "arabic_arabtex";
716         if (open_font && inset->noFontChange()) {
717                 bool closeLanguage = arabtex
718                         || basefont.isRightToLeft() == running_font.isRightToLeft();
719                 unsigned int count = running_font.latexWriteEndChanges(os,
720                         bparams, runparams, basefont, basefont, closeLanguage);
721                 column += count;
722                 // if any font properties were closed, update the running_font, 
723                 // making sure, however, to leave the language as it was
724                 if (count > 0) {
725                         // FIXME: probably a better way to keep track of the old 
726                         // language, than copying the entire font?
727                         Font const copy_font(running_font);
728                         basefont = owner_->getLayoutFont(bparams, outerfont);
729                         running_font = basefont;
730                         if (!closeLanguage)
731                                 running_font.setLanguage(copy_font.language());
732                         // leave font open if language is still open
733                         open_font = (running_font.language() == basefont.language());
734                         if (closeLanguage)
735                                 runparams.local_font = &basefont;
736                 }
737         }
738
739         int tmp = inset->latex(buf, os, runparams);
740
741         if (close) {
742         if (running_font.language()->lang() == "farsi")
743                         os << "\\endL{}";
744                 else
745                         os << '}';
746         }
747
748         if (tmp) {
749                 for (int j = 0; j < tmp; ++j)
750                         texrow.newline();
751
752                 texrow.start(owner_->id(), i + 1);
753                 column = 0;
754         } else {
755                 column += os.tellp() - len;
756         }
757
758         if (owner_->lookupChange(i).type == Change::DELETED)
759                 --runparams.inDeletedInset;
760 }
761
762
763 void Paragraph::Private::latexSpecialChar(
764                                              odocstream & os,
765                                              OutputParams & runparams,
766                                              Font & running_font,
767                                              Change & running_change,
768                                              Layout const & style,
769                                              pos_type & i,
770                                              unsigned int & column)
771 {
772         char_type const c = text_[i];
773
774         if (style.pass_thru) {
775                 if (c != '\0')
776                         // FIXME UNICODE: This can fail if c cannot
777                         // be encoded in the current encoding.
778                         os.put(c);
779                 return;
780         }
781
782         if (runparams.verbatim) {
783                 os.put(c);
784                 return;
785         }
786
787         if (lyxrc.fontenc == "T1" && latexSpecialT1(c, os, i, column))
788                 return;
789
790         if (running_font.fontInfo().family() == TYPEWRITER_FAMILY
791                 && latexSpecialTypewriter(c, os, i, column))
792                 return;
793
794         // Otherwise, we use what LaTeX provides us.
795         switch (c) {
796         case '\\':
797                 os << "\\textbackslash{}";
798                 column += 15;
799                 break;
800         case '<':
801                 os << "\\textless{}";
802                 column += 10;
803                 break;
804         case '>':
805                 os << "\\textgreater{}";
806                 column += 13;
807                 break;
808         case '|':
809                 os << "\\textbar{}";
810                 column += 9;
811                 break;
812         case '-':
813                 os << '-';
814                 break;
815         case '\"':
816                 os << "\\char`\\\"{}";
817                 column += 9;
818                 break;
819
820         case '$': case '&':
821         case '%': case '#': case '{':
822         case '}': case '_':
823                 os << '\\';
824                 os.put(c);
825                 column += 1;
826                 break;
827
828         case '~':
829                 os << "\\textasciitilde{}";
830                 column += 16;
831                 break;
832
833         case '^':
834                 os << "\\textasciicircum{}";
835                 column += 17;
836                 break;
837
838         case '*': case '[':
839                 // avoid being mistaken for optional arguments
840                 os << '{';
841                 os.put(c);
842                 os << '}';
843                 column += 2;
844                 break;
845
846         case ' ':
847                 // Blanks are printed before font switching.
848                 // Sure? I am not! (try nice-latex)
849                 // I am sure it's correct. LyX might be smarter
850                 // in the future, but for now, nothing wrong is
851                 // written. (Asger)
852                 break;
853
854         default:
855
856                 // LyX, LaTeX etc.
857                 if (latexSpecialPhrase(os, i, column, runparams))
858                         return;
859
860                 if (c == '\0')
861                         return;
862
863                 Encoding const & encoding = *(runparams.encoding);
864                 if (i + 1 < int(text_.size())) {
865                         char_type next = text_[i + 1];
866                         if (Encodings::isCombiningChar(next)) {
867                                 column += latexSurrogatePair(os, c, next, encoding) - 1;
868                                 ++i;
869                                 break;
870                         }
871                 }
872                 string script;
873                 docstring const latex = encoding.latexChar(c);
874                 if (Encodings::isKnownScriptChar(c, script)
875                     && prefixIs(latex, from_ascii("\\" + script)))
876                         column += writeScriptChars(os, latex,
877                                         running_change, encoding, i) - 1;
878                 else if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
879                         // Prevent eating of a following
880                         // space or command corruption by
881                         // following characters
882                         column += latex.length() + 1;
883                         os << latex << "{}";
884                 } else {
885                         column += latex.length() - 1;
886                         os << latex;
887                 }
888                 break;
889         }
890 }
891
892
893 bool Paragraph::Private::latexSpecialT1(char_type const c, odocstream & os,
894         pos_type & i, unsigned int & column)
895 {
896         switch (c) {
897         case '>':
898         case '<':
899                 os.put(c);
900                 // In T1 encoding, these characters exist
901                 // but we should avoid ligatures
902                 if (i + 1 >= int(text_.size()) || text_[i + 1] != c)
903                         return true;
904                 os << "\\,{}";
905                 column += 3;
906                 // Alternative code:
907                 //os << "\\textcompwordmark{}";
908                 //column += 19;
909                 return true;
910         case '|':
911                 os.put(c);
912                 return true;
913         default:
914                 return false;
915         }
916 }
917
918
919 bool Paragraph::Private::latexSpecialTypewriter(char_type const c, odocstream & os,
920         pos_type & i, unsigned int & column)
921 {
922         switch (c) {
923         case '-':
924                 if (i + 1 < int(text_.size()) && text_[i + 1] == '-') {
925                         // "--" in Typewriter mode -> "-{}-"
926                         os << "-{}";
927                         column += 2;
928                 } else
929                         os << '-';
930                 return true;
931
932         // I assume this is hack treating typewriter as verbatim
933         // FIXME UNICODE: This can fail if c cannot be encoded
934         // in the current encoding.
935
936         case '\0':
937                 return true;
938
939         // Those characters are not directly supported.
940         case '\\':
941         case '\"':
942         case '$': case '&':
943         case '%': case '#': case '{':
944         case '}': case '_':
945         case '~':
946         case '^':
947         case '*': case '[':
948         case ' ':
949                 return false;
950
951         default:
952                 // With Typewriter font, these characters exist.
953                 os.put(c);
954                 return true;
955         }
956 }
957
958
959 bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
960         unsigned int & column, OutputParams & runparams)
961 {
962         // FIXME: if we have "LaTeX" with a font
963         // change in the middle (before the 'T', then
964         // the "TeX" part is still special cased.
965         // Really we should only operate this on
966         // "words" for some definition of word
967
968         for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
969                 if (!isTextAt(special_phrases[pnr].phrase, i))
970                         continue;
971                 if (runparams.moving_arg)
972                         os << "\\protect";
973                 os << special_phrases[pnr].macro;
974                 i += special_phrases[pnr].phrase.length() - 1;
975                 column += special_phrases[pnr].macro.length() - 1;
976                 return true;
977         }
978         return false;
979 }
980
981
982 void Paragraph::Private::validate(LaTeXFeatures & features,
983                                 Layout const & layout) const
984 {
985         // check the params.
986         if (!params_.spacing().isDefault())
987                 features.require("setspace");
988
989         // then the layouts
990         features.useLayout(layout.name());
991
992         // then the fonts
993         fontlist_.validate(features);
994
995         // then the indentation
996         if (!params_.leftIndent().zero())
997                 features.require("ParagraphLeftIndent");
998
999         // then the insets
1000         InsetList::const_iterator icit = insetlist_.begin();
1001         InsetList::const_iterator iend = insetlist_.end();
1002         for (; icit != iend; ++icit) {
1003                 if (icit->inset) {
1004                         icit->inset->validate(features);
1005                         if (layout.needprotect &&
1006                             icit->inset->lyxCode() == FOOT_CODE)
1007                                 features.require("NeedLyXFootnoteCode");
1008                 }
1009         }
1010
1011         // then the contents
1012         for (pos_type i = 0; i < int(text_.size()) ; ++i) {
1013                 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
1014                         if (!special_phrases[pnr].builtin
1015                             && isTextAt(special_phrases[pnr].phrase, i)) {
1016                                 features.require(special_phrases[pnr].phrase);
1017                                 break;
1018                         }
1019                 }
1020                 Encodings::validate(text_[i], features);
1021         }
1022 }
1023
1024 /////////////////////////////////////////////////////////////////////
1025 //
1026 // Paragraph
1027 //
1028 /////////////////////////////////////////////////////////////////////
1029
1030 Paragraph::Paragraph()
1031         : d(new Paragraph::Private(this))
1032 {
1033         itemdepth = 0;
1034         d->params_.clear();
1035 }
1036
1037
1038 Paragraph::Paragraph(Paragraph const & par)
1039         : itemdepth(par.itemdepth),
1040         d(new Paragraph::Private(*par.d, this))
1041 {
1042 }
1043
1044
1045 Paragraph & Paragraph::operator=(Paragraph const & par)
1046 {
1047         // needed as we will destroy the private part before copying it
1048         if (&par != this) {
1049                 itemdepth = par.itemdepth;
1050
1051                 delete d;
1052                 d = new Private(*par.d, this);
1053         }
1054         return *this;
1055 }
1056
1057
1058 Paragraph::~Paragraph()
1059 {
1060         delete d;
1061 }
1062
1063
1064 void Paragraph::write(Buffer const & buf, ostream & os,
1065                           BufferParams const & bparams,
1066                           depth_type & dth) const
1067 {
1068         // The beginning or end of a deeper (i.e. nested) area?
1069         if (dth != params().depth()) {
1070                 if (params().depth() > dth) {
1071                         while (params().depth() > dth) {
1072                                 os << "\n\\begin_deeper";
1073                                 ++dth;
1074                         }
1075                 } else {
1076                         while (params().depth() < dth) {
1077                                 os << "\n\\end_deeper";
1078                                 --dth;
1079                         }
1080                 }
1081         }
1082
1083         // First write the layout
1084         os << "\n\\begin_layout " << to_utf8(layout()->name()) << '\n';
1085
1086         params().write(os);
1087
1088         Font font1(inherit_font, bparams.language);
1089
1090         Change running_change = Change(Change::UNCHANGED);
1091
1092         int column = 0;
1093         for (pos_type i = 0; i <= size(); ++i) {
1094
1095                 Change change = lookupChange(i);
1096                 Changes::lyxMarkChange(os, column, running_change, change);
1097                 running_change = change;
1098
1099                 if (i == size())
1100                         break;
1101
1102                 // Write font changes
1103                 Font font2 = getFontSettings(bparams, i);
1104                 if (font2 != font1) {
1105                         font2.lyxWriteChanges(font1, os);
1106                         column = 0;
1107                         font1 = font2;
1108                 }
1109
1110                 char_type const c = d->text_[i];
1111                 switch (c) {
1112                 case META_INSET:
1113                         if (Inset const * inset = getInset(i)) {
1114                                 if (inset->directWrite()) {
1115                                         // international char, let it write
1116                                         // code directly so it's shorter in
1117                                         // the file
1118                                         inset->write(buf, os);
1119                                 } else {
1120                                         if (i)
1121                                                 os << '\n';
1122                                         os << "\\begin_inset ";
1123                                         inset->write(buf, os);
1124                                         os << "\n\\end_inset\n\n";
1125                                         column = 0;
1126                                 }
1127                         }
1128                         break;
1129                 case '\\':
1130                         os << "\n\\backslash\n";
1131                         column = 0;
1132                         break;
1133                 case '.':
1134                         if (i + 1 < size() && d->text_[i + 1] == ' ') {
1135                                 os << ".\n";
1136                                 column = 0;
1137                         } else
1138                                 os << '.';
1139                         break;
1140                 default:
1141                         if ((column > 70 && c == ' ')
1142                             || column > 79) {
1143                                 os << '\n';
1144                                 column = 0;
1145                         }
1146                         // this check is to amend a bug. LyX sometimes
1147                         // inserts '\0' this could cause problems.
1148                         if (c != '\0')
1149                                 os << to_utf8(docstring(1, c));
1150                         else
1151                                 lyxerr << "ERROR (Paragraph::writeFile):"
1152                                         " NULL char in structure." << endl;
1153                         ++column;
1154                         break;
1155                 }
1156         }
1157
1158         os << "\n\\end_layout\n";
1159 }
1160
1161
1162 void Paragraph::validate(LaTeXFeatures & features) const
1163 {
1164         d->validate(features, *layout());
1165 }
1166
1167
1168 void Paragraph::insert(pos_type start, docstring const & str,
1169                        Font const & font, Change const & change)
1170 {
1171         for (size_t i = 0, n = str.size(); i != n ; ++i)
1172                 insertChar(start + i, str[i], font, change);
1173 }
1174
1175
1176 void Paragraph::appendChar(char_type c, Font const & font,
1177                 Change const & change)
1178 {
1179         // track change
1180         d->changes_.insert(change, d->text_.size());
1181         // when appending characters, no need to update tables
1182         d->text_.push_back(c);
1183         setFont(d->text_.size() - 1, font);
1184 }
1185
1186
1187 void Paragraph::appendString(docstring const & s, Font const & font,
1188                 Change const & change)
1189 {
1190         pos_type end = s.size();
1191         size_t oldsize = d->text_.size();
1192         size_t newsize = oldsize + end;
1193         size_t capacity = d->text_.capacity();
1194         if (newsize >= capacity)
1195                 d->text_.reserve(max(capacity + 100, newsize));
1196
1197         // when appending characters, no need to update tables
1198         d->text_.append(s);
1199
1200         // FIXME: Optimize this!
1201         for (pos_type i = 0; i != end; ++i) {
1202                 // track change
1203                 d->changes_.insert(change, i);
1204         }
1205         d->fontlist_.set(oldsize, font);
1206         d->fontlist_.set(newsize - 1, font);
1207 }
1208
1209
1210 void Paragraph::insertChar(pos_type pos, char_type c,
1211                            bool trackChanges)
1212 {
1213         d->insertChar(pos, c, Change(trackChanges ?
1214                            Change::INSERTED : Change::UNCHANGED));
1215 }
1216
1217
1218 void Paragraph::insertChar(pos_type pos, char_type c,
1219                            Font const & font, bool trackChanges)
1220 {
1221         d->insertChar(pos, c, Change(trackChanges ?
1222                            Change::INSERTED : Change::UNCHANGED));
1223         setFont(pos, font);
1224 }
1225
1226
1227 void Paragraph::insertChar(pos_type pos, char_type c,
1228                            Font const & font, Change const & change)
1229 {
1230         d->insertChar(pos, c, change);
1231         setFont(pos, font);
1232 }
1233
1234
1235 void Paragraph::insertInset(pos_type pos, Inset * inset,
1236                             Font const & font, Change const & change)
1237 {
1238         insertInset(pos, inset, change);
1239         // Set the font/language of the inset...
1240         setFont(pos, font);
1241 }
1242
1243
1244 bool Paragraph::insetAllowed(InsetCode code)
1245 {
1246         return !d->inset_owner_ || d->inset_owner_->insetAllowed(code);
1247 }
1248
1249
1250 void Paragraph::resetFonts(Font const & font)
1251 {
1252         d->fontlist_.clear();
1253         d->fontlist_.set(0, font);
1254         d->fontlist_.set(d->text_.size() - 1, font);
1255 }
1256
1257 // Gets uninstantiated font setting at position.
1258 Font const Paragraph::getFontSettings(BufferParams const & bparams,
1259                                          pos_type pos) const
1260 {
1261         if (pos > size()) {
1262                 lyxerr << " pos: " << pos << " size: " << size() << endl;
1263                 BOOST_ASSERT(pos <= size());
1264         }
1265
1266         FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
1267         if (cit != d->fontlist_.end())
1268                 return cit->font();
1269
1270         if (pos == size() && !empty())
1271                 return getFontSettings(bparams, pos - 1);
1272
1273         return Font(inherit_font, getParLanguage(bparams));
1274 }
1275
1276
1277 FontSpan Paragraph::fontSpan(pos_type pos) const
1278 {
1279         BOOST_ASSERT(pos <= size());
1280         pos_type start = 0;
1281
1282         FontList::const_iterator cit = d->fontlist_.begin();
1283         FontList::const_iterator end = d->fontlist_.end();
1284         for (; cit != end; ++cit) {
1285                 if (cit->pos() >= pos) {
1286                         if (pos >= beginOfBody())
1287                                 return FontSpan(max(start, beginOfBody()),
1288                                                 cit->pos());
1289                         else
1290                                 return FontSpan(start,
1291                                                 min(beginOfBody() - 1,
1292                                                          cit->pos()));
1293                 }
1294                 start = cit->pos() + 1;
1295         }
1296
1297         // This should not happen, but if so, we take no chances.
1298         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
1299         //      << endl;
1300         return FontSpan(pos, pos);
1301 }
1302
1303
1304 // Gets uninstantiated font setting at position 0
1305 Font const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
1306 {
1307         if (!empty() && !d->fontlist_.empty())
1308                 return d->fontlist_.begin()->font();
1309
1310         return Font(inherit_font, bparams.language);
1311 }
1312
1313
1314 // Gets the fully instantiated font at a given position in a paragraph
1315 // This is basically the same function as Text::GetFont() in text2.cpp.
1316 // The difference is that this one is used for generating the LaTeX file,
1317 // and thus cosmetic "improvements" are disallowed: This has to deliver
1318 // the true picture of the buffer. (Asger)
1319 Font const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
1320                                  Font const & outerfont) const
1321 {
1322         BOOST_ASSERT(pos >= 0);
1323
1324         Font font = getFontSettings(bparams, pos);
1325
1326         pos_type const body_pos = beginOfBody();
1327         if (pos < body_pos)
1328                 font.fontInfo().realize(d->layout_->labelfont);
1329         else
1330                 font.fontInfo().realize(d->layout_->font);
1331
1332         font.fontInfo().realize(outerfont.fontInfo());
1333         font.fontInfo().realize(bparams.getFont().fontInfo());
1334
1335         return font;
1336 }
1337
1338
1339 Font const Paragraph::getLabelFont
1340         (BufferParams const & bparams, Font const & outerfont) const
1341 {
1342         FontInfo tmpfont = layout()->labelfont;
1343         tmpfont.realize(outerfont.fontInfo());
1344         tmpfont.realize(bparams.getFont().fontInfo());
1345         return Font(tmpfont, getParLanguage(bparams));
1346 }
1347
1348
1349 Font const Paragraph::getLayoutFont
1350         (BufferParams const & bparams, Font const & outerfont) const
1351 {
1352         FontInfo tmpfont = layout()->font;
1353         tmpfont.realize(outerfont.fontInfo());
1354         tmpfont.realize(bparams.getFont().fontInfo());
1355         return Font(tmpfont, getParLanguage(bparams));
1356 }
1357
1358
1359 /// Returns the height of the highest font in range
1360 FontSize Paragraph::highestFontInRange
1361         (pos_type startpos, pos_type endpos, FontSize def_size) const
1362 {
1363         return d->fontlist_.highestInRange(startpos, endpos, def_size);
1364 }
1365
1366
1367 char_type
1368 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
1369 {
1370         char_type c = d->text_[pos];
1371         if (!lyxrc.rtl_support)
1372                 return c;
1373
1374         char_type uc = c;
1375         switch (c) {
1376         case '(':
1377                 uc = ')';
1378                 break;
1379         case ')':
1380                 uc = '(';
1381                 break;
1382         case '[':
1383                 uc = ']';
1384                 break;
1385         case ']':
1386                 uc = '[';
1387                 break;
1388         case '{':
1389                 uc = '}';
1390                 break;
1391         case '}':
1392                 uc = '{';
1393                 break;
1394         case '<':
1395                 uc = '>';
1396                 break;
1397         case '>':
1398                 uc = '<';
1399                 break;
1400         }
1401         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
1402                 return uc;
1403         else
1404                 return c;
1405 }
1406
1407
1408 void Paragraph::setFont(pos_type pos, Font const & font)
1409 {
1410         BOOST_ASSERT(pos <= size());
1411
1412         // First, reduce font against layout/label font
1413         // Update: The setCharFont() routine in text2.cpp already
1414         // reduces font, so we don't need to do that here. (Asger)
1415         
1416         d->fontlist_.set(pos, font);
1417 }
1418
1419
1420 void Paragraph::makeSameLayout(Paragraph const & par)
1421 {
1422         layout(par.layout());
1423         // move to pimpl?
1424         d->params_ = par.params();
1425 }
1426
1427
1428 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1429 {
1430         if (isFreeSpacing())
1431                 return false;
1432
1433         int pos = 0;
1434         int count = 0;
1435
1436         while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1437                 if (eraseChar(pos, trackChanges))
1438                         ++count;
1439                 else
1440                         ++pos;
1441         }
1442
1443         return count > 0 || pos > 0;
1444 }
1445
1446
1447 bool Paragraph::hasSameLayout(Paragraph const & par) const
1448 {
1449         return par.layout() == layout() && d->params_.sameLayout(par.params());
1450 }
1451
1452
1453 depth_type Paragraph::getDepth() const
1454 {
1455         return params().depth();
1456 }
1457
1458
1459 depth_type Paragraph::getMaxDepthAfter() const
1460 {
1461         if (layout()->isEnvironment())
1462                 return params().depth() + 1;
1463         else
1464                 return params().depth();
1465 }
1466
1467
1468 char Paragraph::getAlign() const
1469 {
1470         if (params().align() == LYX_ALIGN_LAYOUT)
1471                 return layout()->align;
1472         else
1473                 return params().align();
1474 }
1475
1476
1477 docstring const & Paragraph::getLabelstring() const
1478 {
1479         return params().labelString();
1480 }
1481
1482
1483 // the next two functions are for the manual labels
1484 docstring const Paragraph::getLabelWidthString() const
1485 {
1486         if (layout()->margintype == MARGIN_MANUAL)
1487                 return params().labelWidthString();
1488         else
1489                 return _("Senseless with this layout!");
1490 }
1491
1492
1493 void Paragraph::setLabelWidthString(docstring const & s)
1494 {
1495         params().labelWidthString(s);
1496 }
1497
1498
1499 docstring const Paragraph::translateIfPossible(docstring const & s,
1500                 BufferParams const & bparams) const
1501 {
1502         if (!isAscii(s) || s.empty()) {
1503                 // This must be a user defined layout. We cannot translate
1504                 // this, since gettext accepts only ascii keys.
1505                 return s;
1506         }
1507         // Probably standard layout, try to translate
1508         Messages & m = getMessages(getParLanguage(bparams)->code());
1509         return m.get(to_ascii(s));
1510 }
1511
1512
1513 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1514                 BufferParams const & bparams, bool process_appendix) const
1515 {
1516         TextClass const & tclass = bparams.getTextClass();
1517
1518         docstring fmt;
1519         if (process_appendix && params().appendix())
1520                 fmt = translateIfPossible(layout->labelstring_appendix(),
1521                         bparams);
1522         else
1523                 fmt = translateIfPossible(layout->labelstring(), bparams);
1524
1525         if (fmt.empty() && layout->labeltype == LABEL_COUNTER 
1526             && !layout->counter.empty())
1527                 fmt = "\\the" + layout->counter;
1528
1529         // handle 'inherited level parts' in 'fmt',
1530         // i.e. the stuff between '@' in   '@Section@.\arabic{subsection}'
1531         size_t const i = fmt.find('@', 0);
1532         if (i != docstring::npos) {
1533                 size_t const j = fmt.find('@', i + 1);
1534                 if (j != docstring::npos) {
1535                         docstring parent(fmt, i + 1, j - i - 1);
1536                         docstring label = from_ascii("??");
1537                         if (tclass.hasLayout(parent))
1538                                 docstring label = expandLabel(tclass[parent], bparams,
1539                                                       process_appendix);
1540                         fmt = docstring(fmt, 0, i) + label 
1541                                 + docstring(fmt, j + 1, docstring::npos);
1542                 }
1543         }
1544
1545         return tclass.counters().counterLabel(fmt);
1546 }
1547
1548
1549 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1550 {
1551         layout(new_layout);
1552         LyXAlignment const oldAlign = params().align();
1553         
1554         if (!(oldAlign & layout()->alignpossible)) {
1555                 frontend::Alert::warning(_("Alignment not permitted"), 
1556                         _("The new layout does not permit the alignment previously used.\nSetting to default."));
1557                 params().align(LYX_ALIGN_LAYOUT);
1558         }
1559 }
1560
1561
1562 pos_type Paragraph::beginOfBody() const
1563 {
1564         return d->begin_of_body_;
1565 }
1566
1567
1568 void Paragraph::setBeginOfBody()
1569 {
1570         if (layout()->labeltype != LABEL_MANUAL) {
1571                 d->begin_of_body_ = 0;
1572                 return;
1573         }
1574
1575         // Unroll the first two cycles of the loop
1576         // and remember the previous character to
1577         // remove unnecessary getChar() calls
1578         pos_type i = 0;
1579         pos_type end = size();
1580         if (i < end && !isNewline(i)) {
1581                 ++i;
1582                 char_type previous_char = 0;
1583                 char_type temp = 0;
1584                 if (i < end) {
1585                         previous_char = d->text_[i];
1586                         if (!isNewline(i)) {
1587                                 ++i;
1588                                 while (i < end && previous_char != ' ') {
1589                                         temp = d->text_[i];
1590                                         if (isNewline(i))
1591                                                 break;
1592                                         ++i;
1593                                         previous_char = temp;
1594                                 }
1595                         }
1596                 }
1597         }
1598
1599         d->begin_of_body_ = i;
1600 }
1601
1602
1603 bool Paragraph::forceEmptyLayout() const
1604 {
1605         return inInset() && inInset()->forceEmptyLayout();
1606 }
1607
1608
1609 bool Paragraph::allowParagraphCustomization() const
1610 {
1611         return inInset() && inInset()->allowParagraphCustomization(0);
1612 }
1613
1614
1615 bool Paragraph::useEmptyLayout() const
1616 {
1617         return inInset() && inInset()->useEmptyLayout();
1618 }
1619
1620
1621 namespace {
1622
1623 // paragraphs inside floats need different alignment tags to avoid
1624 // unwanted space
1625
1626 bool noTrivlistCentering(InsetCode code)
1627 {
1628         return code == FLOAT_CODE || code == WRAP_CODE;
1629 }
1630
1631
1632 string correction(string const & orig)
1633 {
1634         if (orig == "flushleft")
1635                 return "raggedright";
1636         if (orig == "flushright")
1637                 return "raggedleft";
1638         if (orig == "center")
1639                 return "centering";
1640         return orig;
1641 }
1642
1643
1644 string const corrected_env(string const & suffix, string const & env,
1645         InsetCode code)
1646 {
1647         string output = suffix + "{";
1648         if (noTrivlistCentering(code))
1649                 output += correction(env);
1650         else
1651                 output += env;
1652         output += "}";
1653         if (suffix == "\\begin")
1654                 output += "\n";
1655         return output;
1656 }
1657
1658
1659 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1660 {
1661         if (!contains(str, "\n"))
1662                 column += str.size();
1663         else {
1664                 string tmp;
1665                 texrow.newline();
1666                 column = rsplit(str, tmp, '\n').size();
1667         }
1668 }
1669
1670 } // namespace anon
1671
1672
1673 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1674                                  odocstream & os, TexRow & texrow,
1675                                  bool moving_arg) const
1676 {
1677         int column = 0;
1678
1679         if (params_.noindent()) {
1680                 os << "\\noindent ";
1681                 column += 10;
1682         }
1683         
1684         LyXAlignment const curAlign = params_.align();
1685
1686         if (curAlign == layout_->align)
1687                 return column;
1688
1689         switch (curAlign) {
1690         case LYX_ALIGN_NONE:
1691         case LYX_ALIGN_BLOCK:
1692         case LYX_ALIGN_LAYOUT:
1693         case LYX_ALIGN_SPECIAL:
1694                 break;
1695         case LYX_ALIGN_LEFT:
1696         case LYX_ALIGN_RIGHT:
1697         case LYX_ALIGN_CENTER:
1698                 if (moving_arg) {
1699                         os << "\\protect";
1700                         column += 8;
1701                 }
1702                 break;
1703         }
1704
1705         switch (curAlign) {
1706         case LYX_ALIGN_NONE:
1707         case LYX_ALIGN_BLOCK:
1708         case LYX_ALIGN_LAYOUT:
1709         case LYX_ALIGN_SPECIAL:
1710                 break;
1711         case LYX_ALIGN_LEFT: {
1712                 string output;
1713                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1714                         output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1715                 else
1716                         output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1717                 os << from_ascii(output);
1718                 adjust_row_column(output, texrow, column);
1719                 break;
1720         } case LYX_ALIGN_RIGHT: {
1721                 string output;
1722                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1723                         output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1724                 else
1725                         output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1726                 os << from_ascii(output);
1727                 adjust_row_column(output, texrow, column);
1728                 break;
1729         } case LYX_ALIGN_CENTER: {
1730                 string output;
1731                 output = corrected_env("\\begin", "center", owner_->ownerCode());
1732                 os << from_ascii(output);
1733                 adjust_row_column(output, texrow, column);
1734                 break;
1735         }
1736         }
1737
1738         return column;
1739 }
1740
1741
1742 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1743                                odocstream & os, TexRow & texrow,
1744                                bool moving_arg) const
1745 {
1746         int column = 0;
1747
1748         switch (params_.align()) {
1749         case LYX_ALIGN_NONE:
1750         case LYX_ALIGN_BLOCK:
1751         case LYX_ALIGN_LAYOUT:
1752         case LYX_ALIGN_SPECIAL:
1753                 break;
1754         case LYX_ALIGN_LEFT:
1755         case LYX_ALIGN_RIGHT:
1756         case LYX_ALIGN_CENTER:
1757                 if (moving_arg) {
1758                         os << "\\protect";
1759                         column = 8;
1760                 }
1761                 break;
1762         }
1763
1764         switch (params_.align()) {
1765         case LYX_ALIGN_NONE:
1766         case LYX_ALIGN_BLOCK:
1767         case LYX_ALIGN_LAYOUT:
1768         case LYX_ALIGN_SPECIAL:
1769                 break;
1770         case LYX_ALIGN_LEFT: {
1771                 string output;
1772                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1773                         output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1774                 else
1775                         output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1776                 os << from_ascii(output);
1777                 adjust_row_column(output, texrow, column);
1778                 break;
1779         } case LYX_ALIGN_RIGHT: {
1780                 string output;
1781                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1782                         output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1783                 else
1784                         output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1785                 os << from_ascii(output);
1786                 adjust_row_column(output, texrow, column);
1787                 break;
1788         } case LYX_ALIGN_CENTER: {
1789                 string output;
1790                 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1791                 os << from_ascii(output);
1792                 adjust_row_column(output, texrow, column);
1793                 break;
1794         }
1795         }
1796
1797         return column;
1798 }
1799
1800
1801 // This one spits out the text of the paragraph
1802 bool Paragraph::latex(Buffer const & buf,
1803                                 BufferParams const & bparams,
1804                                 Font const & outerfont,
1805                                 odocstream & os, TexRow & texrow,
1806                                 OutputParams const & runparams) const
1807 {
1808         LYXERR(Debug::LATEX, "SimpleTeXOnePar...     " << this);
1809
1810         bool return_value = false;
1811
1812         LayoutPtr style;
1813
1814         // well we have to check if we are in an inset with unlimited
1815         // length (all in one row) if that is true then we don't allow
1816         // any special options in the paragraph and also we don't allow
1817         // any environment other than the default layout of the text class
1818         // to be valid!
1819         bool asdefault = forceEmptyLayout();
1820
1821         if (asdefault) {
1822                 style = bparams.getTextClass().defaultLayout();
1823         } else {
1824                 style = layout();
1825         }
1826
1827         // Current base font for all inherited font changes, without any
1828         // change caused by an individual character, except for the language:
1829         // It is set to the language of the first character.
1830         // As long as we are in the label, this font is the base font of the
1831         // label. Before the first body character it is set to the base font
1832         // of the body.
1833         Font basefont;
1834
1835         // Maybe we have to create a optional argument.
1836         pos_type body_pos = beginOfBody();
1837         unsigned int column = 0;
1838
1839         if (body_pos > 0) {
1840                 // the optional argument is kept in curly brackets in
1841                 // case it contains a ']'
1842                 os << "[{";
1843                 column += 2;
1844                 basefont = getLabelFont(bparams, outerfont);
1845         } else {
1846                 basefont = getLayoutFont(bparams, outerfont);
1847         }
1848
1849         // Which font is currently active?
1850         Font running_font(basefont);
1851         // Do we have an open font change?
1852         bool open_font = false;
1853
1854         Change runningChange = Change(Change::UNCHANGED);
1855
1856         texrow.start(id(), 0);
1857
1858         // if the paragraph is empty, the loop will not be entered at all
1859         if (empty()) {
1860                 if (style->isCommand()) {
1861                         os << '{';
1862                         ++column;
1863                 }
1864                 if (!asdefault)
1865                         column += d->startTeXParParams(bparams, os, texrow,
1866                                                     runparams.moving_arg);
1867         }
1868
1869         for (pos_type i = 0; i < size(); ++i) {
1870                 // First char in paragraph or after label?
1871                 if (i == body_pos) {
1872                         if (body_pos > 0) {
1873                                 if (open_font) {
1874                                         column += running_font.latexWriteEndChanges(
1875                                                 os, bparams, runparams,
1876                                                 basefont, basefont);
1877                                         open_font = false;
1878                                 }
1879                                 basefont = getLayoutFont(bparams, outerfont);
1880                                 running_font = basefont;
1881
1882                                 column += Changes::latexMarkChange(os, bparams,
1883                                                 runningChange, Change(Change::UNCHANGED));
1884                                 runningChange = Change(Change::UNCHANGED);
1885
1886                                 os << "}] ";
1887                                 column +=3;
1888                         }
1889                         if (style->isCommand()) {
1890                                 os << '{';
1891                                 ++column;
1892                         }
1893
1894                         if (!asdefault)
1895                                 column += d->startTeXParParams(bparams, os,
1896                                                             texrow,
1897                                                             runparams.moving_arg);
1898                 }
1899
1900                 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1901                                                                  : lookupChange(i);
1902
1903                 if (bparams.outputChanges && runningChange != change) {
1904                         if (open_font) {
1905                                 column += running_font.latexWriteEndChanges(
1906                                                 os, bparams, runparams, basefont, basefont);
1907                                 open_font = false;
1908                         }
1909                         basefont = getLayoutFont(bparams, outerfont);
1910                         running_font = basefont;
1911
1912                         column += Changes::latexMarkChange(os, bparams, runningChange, change);
1913                         runningChange = change;
1914                 }
1915
1916                 // do not output text which is marked deleted
1917                 // if change tracking output is disabled
1918                 if (!bparams.outputChanges && change.type == Change::DELETED) {
1919                         continue;
1920                 }
1921
1922                 ++column;
1923
1924                 // Fully instantiated font
1925                 Font const font = getFont(bparams, i, outerfont);
1926
1927                 Font const last_font = running_font;
1928
1929                 // Do we need to close the previous font?
1930                 if (open_font &&
1931                     (font != running_font ||
1932                      font.language() != running_font.language()))
1933                 {
1934                         column += running_font.latexWriteEndChanges(
1935                                         os, bparams, runparams, basefont,
1936                                         (i == body_pos-1) ? basefont : font);
1937                         running_font = basefont;
1938                         open_font = false;
1939                 }
1940
1941                 // close babel's font environment before opening CJK.
1942                 if (!running_font.language()->babel().empty() &&
1943                     font.language()->encoding()->package() == Encoding::CJK) {
1944                                 string end_tag = subst(lyxrc.language_command_end,
1945                                                         "$$lang",
1946                                                         running_font.language()->babel());
1947                                 os << from_ascii(end_tag);
1948                                 column += end_tag.length();
1949                 }
1950
1951                 // Switch file encoding if necessary (and allowed)
1952                 if (!runparams.verbatim && 
1953                     runparams.encoding->package() == Encoding::none &&
1954                     font.language()->encoding()->package() == Encoding::none) {
1955                         pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1956                                         runparams, *(font.language()->encoding()));
1957                         if (enc_switch.first) {
1958                                 column += enc_switch.second;
1959                                 runparams.encoding = font.language()->encoding();
1960                         }
1961                 }
1962
1963                 char_type const c = d->text_[i];
1964
1965                 // Do we need to change font?
1966                 if ((font != running_font ||
1967                      font.language() != running_font.language()) &&
1968                         i != body_pos - 1)
1969                 {
1970                         odocstringstream ods;
1971                         column += font.latexWriteStartChanges(ods, bparams,
1972                                                               runparams, basefont,
1973                                                               last_font);
1974                         running_font = font;
1975                         open_font = true;
1976                         docstring fontchange = ods.str();
1977                         // check if the fontchange ends with a trailing blank
1978                         // (like "\small " (see bug 3382)
1979                         if (suffixIs(fontchange, ' ') && c == ' ')
1980                                 os << fontchange.substr(0, fontchange.size() - 1) 
1981                                    << from_ascii("{}");
1982                         else
1983                                 os << fontchange;
1984                 }
1985
1986                 if (c == ' ') {
1987                         // FIXME: integrate this case in latexSpecialChar
1988                         // Do not print the separation of the optional argument
1989                         // if style->pass_thru is false. This works because
1990                         // latexSpecialChar ignores spaces if
1991                         // style->pass_thru is false.
1992                         if (i != body_pos - 1) {
1993                                 if (d->simpleTeXBlanks(
1994                                                 runparams, os, texrow,
1995                                                 i, column, font, *style)) {
1996                                         // A surrogate pair was output. We
1997                                         // must not call latexSpecialChar
1998                                         // in this iteration, since it would output
1999                                         // the combining character again.
2000                                         ++i;
2001                                         continue;
2002                                 }
2003                         }
2004                 }
2005
2006                 OutputParams rp = runparams;
2007                 rp.free_spacing = style->free_spacing;
2008                 rp.local_font = &font;
2009                 rp.intitle = style->intitle;
2010
2011                 // Two major modes:  LaTeX or plain
2012                 // Handle here those cases common to both modes
2013                 // and then split to handle the two modes separately.
2014                 if (c == META_INSET)
2015                         d->latexInset(buf, bparams, os,
2016                                         texrow, rp, running_font,
2017                                         basefont, outerfont, open_font,
2018                                         runningChange, *style, i, column);
2019                 else {
2020                         try {
2021                                 d->latexSpecialChar(os, rp, running_font, runningChange,
2022                                         *style, i, column);
2023                         } catch (EncodingException & e) {
2024                                 if (runparams.dryrun) {
2025                                         os << "<" << _("LyX Warning: ")
2026                                            << _("uncodable character") << " '";
2027                                         os.put(c);
2028                                         os << "'>";
2029                                 } else {
2030                                         // add location information and throw again.
2031                                         e.par_id = id();
2032                                         e.pos = i;
2033                                         throw(e);
2034                                 }
2035                         }
2036                 }
2037
2038                 // Set the encoding to that returned from simpleTeXSpecialChars (see
2039                 // comment for encoding member in OutputParams.h)
2040                 runparams.encoding = rp.encoding;
2041         }
2042
2043         // If we have an open font definition, we have to close it
2044         if (open_font) {
2045 #ifdef FIXED_LANGUAGE_END_DETECTION
2046                 if (next_) {
2047                         running_font
2048                                 .latexWriteEndChanges(os, bparams, runparams,
2049                                         basefont,
2050                                         next_->getFont(bparams, 0, outerfont));
2051                 } else {
2052                         running_font.latexWriteEndChanges(os, bparams,
2053                                         runparams, basefont, basefont);
2054                 }
2055 #else
2056 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2057 //FIXME: there as we start another \selectlanguage with the next paragraph if
2058 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2059                 running_font.latexWriteEndChanges(os, bparams, runparams,
2060                                 basefont, basefont);
2061 #endif
2062         }
2063
2064         column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2065
2066         // Needed if there is an optional argument but no contents.
2067         if (body_pos > 0 && body_pos == size()) {
2068                 os << "}]~";
2069                 return_value = false;
2070         }
2071
2072         if (!asdefault) {
2073                 column += d->endTeXParParams(bparams, os, texrow,
2074                                           runparams.moving_arg);
2075         }
2076
2077         LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2078         return return_value;
2079 }
2080
2081
2082 bool Paragraph::emptyTag() const
2083 {
2084         for (pos_type i = 0; i < size(); ++i) {
2085                 if (Inset const * inset = getInset(i)) {
2086                         InsetCode lyx_code = inset->lyxCode();
2087                         if (lyx_code != TOC_CODE &&
2088                             lyx_code != INCLUDE_CODE &&
2089                             lyx_code != GRAPHICS_CODE &&
2090                             lyx_code != ERT_CODE &&
2091                             lyx_code != LISTINGS_CODE &&
2092                             lyx_code != FLOAT_CODE &&
2093                             lyx_code != TABULAR_CODE) {
2094                                 return false;
2095                         }
2096                 } else {
2097                         char_type c = d->text_[i];
2098                         if (c != ' ' && c != '\t')
2099                                 return false;
2100                 }
2101         }
2102         return true;
2103 }
2104
2105
2106 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2107 {
2108         for (pos_type i = 0; i < size(); ++i) {
2109                 if (Inset const * inset = getInset(i)) {
2110                         InsetCode lyx_code = inset->lyxCode();
2111                         if (lyx_code == LABEL_CODE) {
2112                                 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2113                                 docstring const & id = il->getParam("name");
2114                                 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2115                         }
2116                 }
2117         }
2118         return string();
2119 }
2120
2121
2122 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2123 {
2124         pos_type i;
2125         for (i = 0; i < size(); ++i) {
2126                 if (Inset const * inset = getInset(i)) {
2127                         inset->docbook(buf, os, runparams);
2128                 } else {
2129                         char_type c = d->text_[i];
2130                         if (c == ' ')
2131                                 break;
2132                         os << sgml::escapeChar(c);
2133                 }
2134         }
2135         return i;
2136 }
2137
2138
2139 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2140 {
2141         Font font_old;
2142         pos_type size = text_.size();
2143         for (pos_type i = initial; i < size; ++i) {
2144                 Font font = owner_->getFont(buf.params(), i, outerfont);
2145                 if (text_[i] == META_INSET)
2146                         return false;
2147                 if (i != initial && font != font_old)
2148                         return false;
2149                 font_old = font;
2150         }
2151
2152         return true;
2153 }
2154
2155
2156 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2157                                     odocstream & os,
2158                                     OutputParams const & runparams,
2159                                     Font const & outerfont,
2160                                     pos_type initial) const
2161 {
2162         bool emph_flag = false;
2163
2164         LayoutPtr const & style = layout();
2165         FontInfo font_old =
2166                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2167
2168         if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2169                 os << "]]>";
2170
2171         // parsing main loop
2172         for (pos_type i = initial; i < size(); ++i) {
2173                 Font font = getFont(buf.params(), i, outerfont);
2174
2175                 // handle <emphasis> tag
2176                 if (font_old.emph() != font.fontInfo().emph()) {
2177                         if (font.fontInfo().emph() == FONT_ON) {
2178                                 os << "<emphasis>";
2179                                 emph_flag = true;
2180                         } else if (i != initial) {
2181                                 os << "</emphasis>";
2182                                 emph_flag = false;
2183                         }
2184                 }
2185
2186                 if (Inset const * inset = getInset(i)) {
2187                         inset->docbook(buf, os, runparams);
2188                 } else {
2189                         char_type c = d->text_[i];
2190
2191                         if (style->pass_thru)
2192                                 os.put(c);
2193                         else
2194                                 os << sgml::escapeChar(c);
2195                 }
2196                 font_old = font.fontInfo();
2197         }
2198
2199         if (emph_flag) {
2200                 os << "</emphasis>";
2201         }
2202
2203         if (style->free_spacing)
2204                 os << '\n';
2205         if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2206                 os << "<![CDATA[";
2207 }
2208
2209
2210 bool Paragraph::isHfill(pos_type pos) const
2211 {
2212         Inset const * inset = getInset(pos);
2213         return inset && inset->lyxCode() == HFILL_CODE;
2214 }
2215
2216
2217 bool Paragraph::isNewline(pos_type pos) const
2218 {
2219         Inset const * inset = getInset(pos);
2220         return inset && inset->lyxCode() == NEWLINE_CODE;
2221 }
2222
2223
2224 bool Paragraph::isLineSeparator(pos_type pos) const
2225 {
2226         char_type const c = d->text_[pos];
2227         if (isLineSeparatorChar(c))
2228                 return true;
2229         Inset const * inset = getInset(pos);
2230         return inset && inset->isLineSeparator();
2231 }
2232
2233
2234 /// Used by the spellchecker
2235 bool Paragraph::isLetter(pos_type pos) const
2236 {
2237         if (Inset const * inset = getInset(pos))
2238                 return inset->isLetter();
2239         char_type const c = d->text_[pos];
2240         return isLetterChar(c) || isDigit(c);
2241 }
2242
2243
2244 Language const *
2245 Paragraph::getParLanguage(BufferParams const & bparams) const
2246 {
2247         if (!empty())
2248                 return getFirstFontSettings(bparams).language();
2249         // FIXME: we should check the prev par as well (Lgb)
2250         return bparams.language;
2251 }
2252
2253
2254 bool Paragraph::isRTL(BufferParams const & bparams) const
2255 {
2256         return lyxrc.rtl_support
2257                 && getParLanguage(bparams)->rightToLeft()
2258                 && ownerCode() != ERT_CODE
2259                 && ownerCode() != LISTINGS_CODE;
2260 }
2261
2262
2263 void Paragraph::changeLanguage(BufferParams const & bparams,
2264                                Language const * from, Language const * to)
2265 {
2266         // change language including dummy font change at the end
2267         for (pos_type i = 0; i <= size(); ++i) {
2268                 Font font = getFontSettings(bparams, i);
2269                 if (font.language() == from) {
2270                         font.setLanguage(to);
2271                         setFont(i, font);
2272                 }
2273         }
2274 }
2275
2276
2277 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2278 {
2279         Language const * doc_language = bparams.language;
2280         FontList::const_iterator cit = d->fontlist_.begin();
2281         FontList::const_iterator end = d->fontlist_.end();
2282
2283         for (; cit != end; ++cit)
2284                 if (cit->font().language() != ignore_language &&
2285                     cit->font().language() != latex_language &&
2286                     cit->font().language() != doc_language)
2287                         return true;
2288         return false;
2289 }
2290
2291
2292 // Convert the paragraph to a string.
2293 // Used for building the table of contents
2294 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2295 {
2296         return asString(buffer, 0, size(), label);
2297 }
2298
2299
2300 docstring const Paragraph::asString(Buffer const & buffer,
2301                                  pos_type beg, pos_type end, bool label) const
2302 {
2303
2304         odocstringstream os;
2305
2306         if (beg == 0 && label && !params().labelString().empty())
2307                 os << params().labelString() << ' ';
2308
2309         for (pos_type i = beg; i < end; ++i) {
2310                 char_type const c = d->text_[i];
2311                 if (isPrintable(c))
2312                         os.put(c);
2313                 else if (c == META_INSET)
2314                         getInset(i)->textString(buffer, os);
2315         }
2316
2317         return os.str();
2318 }
2319
2320
2321 void Paragraph::setInsetOwner(Inset * inset)
2322 {
2323         d->inset_owner_ = inset;
2324 }
2325
2326
2327 int Paragraph::id() const
2328 {
2329         return d->id_;
2330 }
2331
2332
2333 LayoutPtr const & Paragraph::layout() const
2334 {
2335         return d->layout_;
2336 }
2337
2338
2339 void Paragraph::layout(LayoutPtr const & new_layout)
2340 {
2341         d->layout_ = new_layout;
2342 }
2343
2344
2345 Inset * Paragraph::inInset() const
2346 {
2347         return d->inset_owner_;
2348 }
2349
2350
2351 InsetCode Paragraph::ownerCode() const
2352 {
2353         return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2354 }
2355
2356
2357 ParagraphParameters & Paragraph::params()
2358 {
2359         return d->params_;
2360 }
2361
2362
2363 ParagraphParameters const & Paragraph::params() const
2364 {
2365         return d->params_;
2366 }
2367
2368
2369 bool Paragraph::isFreeSpacing() const
2370 {
2371         if (layout()->free_spacing)
2372                 return true;
2373         return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2374 }
2375
2376
2377 bool Paragraph::allowEmpty() const
2378 {
2379         if (layout()->keepempty)
2380                 return true;
2381         return d->inset_owner_ && d->inset_owner_->allowEmpty();
2382 }
2383
2384
2385 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2386 {
2387         if (!Encodings::is_arabic(c))
2388                 return c;
2389
2390         char_type prev_char = ' ';
2391         char_type next_char = ' ';
2392
2393         for (pos_type i = pos - 1; i >= 0; --i) {
2394                 char_type const par_char = d->text_[i];
2395                 if (!Encodings::isComposeChar_arabic(par_char)) {
2396                         prev_char = par_char;
2397                         break;
2398                 }
2399         }
2400
2401         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2402                 char_type const par_char = d->text_[i];
2403                 if (!Encodings::isComposeChar_arabic(par_char)) {
2404                         next_char = par_char;
2405                         break;
2406                 }
2407         }
2408
2409         if (Encodings::is_arabic(next_char)) {
2410                 if (Encodings::is_arabic(prev_char) &&
2411                         !Encodings::is_arabic_special(prev_char))
2412                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2413                 else
2414                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2415         } else {
2416                 if (Encodings::is_arabic(prev_char) &&
2417                         !Encodings::is_arabic_special(prev_char))
2418                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
2419                 else
2420                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2421         }
2422 }
2423
2424
2425 int Paragraph::checkBiblio(bool track_changes)
2426 {
2427         //FIXME From JS:
2428         //This is getting more and more a mess. ...We really should clean
2429         //up this bibitem issue for 1.6. See also bug 2743.
2430
2431         // Add bibitem insets if necessary
2432         if (layout()->labeltype != LABEL_BIBLIO)
2433                 return 0;
2434
2435         bool hasbibitem = !d->insetlist_.empty()
2436                 // Insist on it being in pos 0
2437                 && d->text_[0] == META_INSET
2438                 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2439
2440         docstring oldkey;
2441         docstring oldlabel;
2442
2443         // remove a bibitem in pos != 0
2444         // restore it later in pos 0 if necessary
2445         // (e.g. if a user inserts contents _before_ the item)
2446         // we're assuming there's only one of these, which there
2447         // should be.
2448         int erasedInsetPosition = -1;
2449         InsetList::iterator it = d->insetlist_.begin();
2450         InsetList::iterator end = d->insetlist_.end();
2451         for (; it != end; ++it)
2452                 if (it->inset->lyxCode() == BIBITEM_CODE
2453                     && it->pos > 0) {
2454                         InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2455                         oldkey = olditem->getParam("key");
2456                         oldlabel = olditem->getParam("label");
2457                         erasedInsetPosition = it->pos;
2458                         eraseChar(erasedInsetPosition, track_changes);
2459                         break;
2460         }
2461
2462         //There was an InsetBibitem at the beginning, and we didn't
2463         //have to erase one.
2464         if (hasbibitem && erasedInsetPosition < 0)
2465                         return 0;
2466
2467         //There was an InsetBibitem at the beginning and we did have to
2468         //erase one. So we give its properties to the beginning inset.
2469         if (hasbibitem) {
2470                 InsetBibitem * inset =
2471                         static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2472                 if (!oldkey.empty())
2473                         inset->setParam("key", oldkey);
2474                 inset->setParam("label", oldlabel);
2475                 return -erasedInsetPosition;
2476         }
2477
2478         //There was no inset at the beginning, so we need to create one with
2479         //the key and label of the one we erased.
2480         InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2481         // restore values of previously deleted item in this par.
2482         if (!oldkey.empty())
2483                 inset->setParam("key", oldkey);
2484         inset->setParam("label", oldlabel);
2485         insertInset(0, static_cast<Inset *>(inset),
2486                     Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2487
2488         return 1;
2489 }
2490
2491
2492 void Paragraph::checkAuthors(AuthorList const & authorList)
2493 {
2494         d->changes_.checkAuthors(authorList);
2495 }
2496
2497
2498 bool Paragraph::isUnchanged(pos_type pos) const
2499 {
2500         return lookupChange(pos).type == Change::UNCHANGED;
2501 }
2502
2503
2504 bool Paragraph::isInserted(pos_type pos) const
2505 {
2506         return lookupChange(pos).type == Change::INSERTED;
2507 }
2508
2509
2510 bool Paragraph::isDeleted(pos_type pos) const
2511 {
2512         return lookupChange(pos).type == Change::DELETED;
2513 }
2514
2515
2516 InsetList const & Paragraph::insetList() const
2517 {
2518         return d->insetlist_;
2519 }
2520
2521
2522 Inset * Paragraph::releaseInset(pos_type pos)
2523 {
2524         Inset * inset = d->insetlist_.release(pos);
2525         /// does not honour change tracking!
2526         eraseChar(pos, false);
2527         return inset;
2528 }
2529
2530
2531 Inset * Paragraph::getInset(pos_type pos)
2532 {
2533         return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2534                  ? d->insetlist_.get(pos) : 0;
2535 }
2536
2537
2538 Inset const * Paragraph::getInset(pos_type pos) const
2539 {
2540         return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2541                  ? d->insetlist_.get(pos) : 0;
2542 }
2543
2544
2545 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2546                 pos_type & right, TextCase action)
2547 {
2548         // process sequences of modified characters; in change
2549         // tracking mode, this approach results in much better
2550         // usability than changing case on a char-by-char basis
2551         docstring changes;
2552
2553         bool const trackChanges = bparams.trackChanges;
2554
2555         bool capitalize = true;
2556
2557         for (; pos < right; ++pos) {
2558                 char_type oldChar = d->text_[pos];
2559                 char_type newChar = oldChar;
2560
2561                 // ignore insets and don't play with deleted text!
2562                 if (oldChar != META_INSET && !isDeleted(pos)) {
2563                         switch (action) {
2564                                 case text_lowercase:
2565                                         newChar = lowercase(oldChar);
2566                                         break;
2567                                 case text_capitalization:
2568                                         if (capitalize) {
2569                                                 newChar = uppercase(oldChar);
2570                                                 capitalize = false;
2571                                         }
2572                                         break;
2573                                 case text_uppercase:
2574                                         newChar = uppercase(oldChar);
2575                                         break;
2576                         }
2577                 }
2578
2579                 if (!isLetter(pos) || isDeleted(pos)) {
2580                         // permit capitalization again
2581                         capitalize = true;
2582                 }
2583
2584                 if (oldChar != newChar)
2585                         changes += newChar;
2586
2587                 if (oldChar == newChar || pos == right - 1) {
2588                         if (oldChar != newChar) {
2589                                 // step behind the changing area
2590                                 pos++;
2591                         }
2592                         int erasePos = pos - changes.size();
2593                         for (size_t i = 0; i < changes.size(); i++) {
2594                                 insertChar(pos, changes[i],
2595                                         getFontSettings(bparams,
2596                                         erasePos),
2597                                         trackChanges);
2598                                 if (!eraseChar(erasePos, trackChanges)) {
2599                                         ++erasePos;
2600                                         ++pos; // advance
2601                                         ++right; // expand selection
2602                                 }
2603                         }
2604                         changes.clear();
2605                 }
2606         }
2607 }
2608
2609
2610 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2611                 pos_type pos, bool del) const
2612 {
2613         int const strsize = str.length();
2614         int i = 0;
2615         pos_type const parsize = d->text_.size();
2616         for (i = 0; pos + i < parsize; ++i) {
2617                 if (i >= strsize)
2618                         break;
2619                 if (cs && str[i] != d->text_[pos + i])
2620                         break;
2621                 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2622                         break;
2623                 if (!del && isDeleted(pos + i))
2624                         break;
2625         }
2626
2627         if (i != strsize)
2628                 return false;
2629
2630         // if necessary, check whether string matches word
2631         if (mw) {
2632                 if (pos > 0 && isLetter(pos - 1))
2633                         return false;
2634                 if (pos + strsize < parsize
2635                         && isLetter(pos + strsize))
2636                         return false;
2637         }
2638
2639         return true;
2640 }
2641
2642
2643 char_type Paragraph::getChar(pos_type pos) const
2644 {
2645         return d->text_[pos];
2646 }
2647
2648
2649 pos_type Paragraph::size() const
2650 {
2651         return d->text_.size();
2652 }
2653
2654
2655 bool Paragraph::empty() const
2656 {
2657         return d->text_.empty();
2658 }
2659
2660
2661 bool Paragraph::isInset(pos_type pos) const
2662 {
2663         return d->text_[pos] == META_INSET;
2664 }
2665
2666
2667 bool Paragraph::isSeparator(pos_type pos) const
2668 {
2669         //FIXME: Are we sure this can be the only separator?
2670         return d->text_[pos] == ' ';
2671 }
2672
2673
2674 } // namespace lyx