]> git.lyx.org Git - lyx.git/blob - src/Paragraph.cpp
* completion infrastructure
[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 != d->params_.depth()) {
1070                 if (d->params_.depth() > dth) {
1071                         while (d->params_.depth() > dth) {
1072                                 os << "\n\\begin_deeper";
1073                                 ++dth;
1074                         }
1075                 } else {
1076                         while (d->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(d->layout_->name()) << '\n';
1085
1086         d->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, *d->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 = d->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 = d->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         d->layout_ = par.d->layout_;
1423         d->params_ = par.d->params_;
1424 }
1425
1426
1427 bool Paragraph::stripLeadingSpaces(bool trackChanges)
1428 {
1429         if (isFreeSpacing())
1430                 return false;
1431
1432         int pos = 0;
1433         int count = 0;
1434
1435         while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
1436                 if (eraseChar(pos, trackChanges))
1437                         ++count;
1438                 else
1439                         ++pos;
1440         }
1441
1442         return count > 0 || pos > 0;
1443 }
1444
1445
1446 bool Paragraph::hasSameLayout(Paragraph const & par) const
1447 {
1448         return par.d->layout_ == d->layout_ && d->params_.sameLayout(par.d->params_);
1449 }
1450
1451
1452 depth_type Paragraph::getDepth() const
1453 {
1454         return d->params_.depth();
1455 }
1456
1457
1458 depth_type Paragraph::getMaxDepthAfter() const
1459 {
1460         if (d->layout_->isEnvironment())
1461                 return d->params_.depth() + 1;
1462         else
1463                 return d->params_.depth();
1464 }
1465
1466
1467 char Paragraph::getAlign() const
1468 {
1469         if (d->params_.align() == LYX_ALIGN_LAYOUT)
1470                 return d->layout_->align;
1471         else
1472                 return d->params_.align();
1473 }
1474
1475
1476 docstring const & Paragraph::getLabelstring() const
1477 {
1478         return d->params_.labelString();
1479 }
1480
1481
1482 // the next two functions are for the manual labels
1483 docstring const Paragraph::getLabelWidthString() const
1484 {
1485         if (d->layout_->margintype == MARGIN_MANUAL)
1486                 return d->params_.labelWidthString();
1487         else
1488                 return _("Senseless with this layout!");
1489 }
1490
1491
1492 void Paragraph::setLabelWidthString(docstring const & s)
1493 {
1494         d->params_.labelWidthString(s);
1495 }
1496
1497
1498 docstring const Paragraph::translateIfPossible(docstring const & s,
1499                 BufferParams const & bparams) const
1500 {
1501         if (!isAscii(s) || s.empty()) {
1502                 // This must be a user defined layout. We cannot translate
1503                 // this, since gettext accepts only ascii keys.
1504                 return s;
1505         }
1506         // Probably standard layout, try to translate
1507         Messages & m = getMessages(getParLanguage(bparams)->code());
1508         return m.get(to_ascii(s));
1509 }
1510
1511
1512 docstring Paragraph::expandLabel(LayoutPtr const & layout,
1513                 BufferParams const & bparams, bool process_appendix) const
1514 {
1515         TextClass const & tclass = bparams.getTextClass();
1516
1517         docstring fmt;
1518         if (process_appendix && d->params_.appendix())
1519                 fmt = translateIfPossible(layout->labelstring_appendix(),
1520                         bparams);
1521         else
1522                 fmt = translateIfPossible(layout->labelstring(), bparams);
1523
1524         if (fmt.empty() && layout->labeltype == LABEL_COUNTER 
1525             && !layout->counter.empty())
1526                 fmt = "\\the" + layout->counter;
1527
1528         // handle 'inherited level parts' in 'fmt',
1529         // i.e. the stuff between '@' in   '@Section@.\arabic{subsection}'
1530         size_t const i = fmt.find('@', 0);
1531         if (i != docstring::npos) {
1532                 size_t const j = fmt.find('@', i + 1);
1533                 if (j != docstring::npos) {
1534                         docstring parent(fmt, i + 1, j - i - 1);
1535                         docstring label = from_ascii("??");
1536                         if (tclass.hasLayout(parent))
1537                                 docstring label = expandLabel(tclass[parent], bparams,
1538                                                       process_appendix);
1539                         fmt = docstring(fmt, 0, i) + label 
1540                                 + docstring(fmt, j + 1, docstring::npos);
1541                 }
1542         }
1543
1544         return tclass.counters().counterLabel(fmt);
1545 }
1546
1547
1548 void Paragraph::applyLayout(LayoutPtr const & new_layout)
1549 {
1550         d->layout_ = new_layout;
1551         LyXAlignment const oldAlign = d->params_.align();
1552         
1553         if (!(oldAlign & d->layout_->alignpossible)) {
1554                 frontend::Alert::warning(_("Alignment not permitted"), 
1555                         _("The new layout does not permit the alignment previously used.\nSetting to default."));
1556                 d->params_.align(LYX_ALIGN_LAYOUT);
1557         }
1558 }
1559
1560
1561 pos_type Paragraph::beginOfBody() const
1562 {
1563         return d->begin_of_body_;
1564 }
1565
1566
1567 void Paragraph::setBeginOfBody()
1568 {
1569         if (d->layout_->labeltype != LABEL_MANUAL) {
1570                 d->begin_of_body_ = 0;
1571                 return;
1572         }
1573
1574         // Unroll the first two cycles of the loop
1575         // and remember the previous character to
1576         // remove unnecessary getChar() calls
1577         pos_type i = 0;
1578         pos_type end = size();
1579         if (i < end && !isNewline(i)) {
1580                 ++i;
1581                 char_type previous_char = 0;
1582                 char_type temp = 0;
1583                 if (i < end) {
1584                         previous_char = d->text_[i];
1585                         if (!isNewline(i)) {
1586                                 ++i;
1587                                 while (i < end && previous_char != ' ') {
1588                                         temp = d->text_[i];
1589                                         if (isNewline(i))
1590                                                 break;
1591                                         ++i;
1592                                         previous_char = temp;
1593                                 }
1594                         }
1595                 }
1596         }
1597
1598         d->begin_of_body_ = i;
1599 }
1600
1601
1602 bool Paragraph::forceEmptyLayout() const
1603 {
1604         return inInset() && inInset()->forceEmptyLayout();
1605 }
1606
1607
1608 bool Paragraph::allowParagraphCustomization() const
1609 {
1610         return inInset() && inInset()->allowParagraphCustomization(0);
1611 }
1612
1613
1614 bool Paragraph::useEmptyLayout() const
1615 {
1616         return inInset() && inInset()->useEmptyLayout();
1617 }
1618
1619
1620 namespace {
1621
1622 // paragraphs inside floats need different alignment tags to avoid
1623 // unwanted space
1624
1625 bool noTrivlistCentering(InsetCode code)
1626 {
1627         return code == FLOAT_CODE || code == WRAP_CODE;
1628 }
1629
1630
1631 string correction(string const & orig)
1632 {
1633         if (orig == "flushleft")
1634                 return "raggedright";
1635         if (orig == "flushright")
1636                 return "raggedleft";
1637         if (orig == "center")
1638                 return "centering";
1639         return orig;
1640 }
1641
1642
1643 string const corrected_env(string const & suffix, string const & env,
1644         InsetCode code)
1645 {
1646         string output = suffix + "{";
1647         if (noTrivlistCentering(code))
1648                 output += correction(env);
1649         else
1650                 output += env;
1651         output += "}";
1652         if (suffix == "\\begin")
1653                 output += "\n";
1654         return output;
1655 }
1656
1657
1658 void adjust_row_column(string const & str, TexRow & texrow, int & column)
1659 {
1660         if (!contains(str, "\n"))
1661                 column += str.size();
1662         else {
1663                 string tmp;
1664                 texrow.newline();
1665                 column = rsplit(str, tmp, '\n').size();
1666         }
1667 }
1668
1669 } // namespace anon
1670
1671
1672 int Paragraph::Private::startTeXParParams(BufferParams const & bparams,
1673                                  odocstream & os, TexRow & texrow,
1674                                  bool moving_arg) const
1675 {
1676         int column = 0;
1677
1678         if (params_.noindent()) {
1679                 os << "\\noindent ";
1680                 column += 10;
1681         }
1682         
1683         LyXAlignment const curAlign = params_.align();
1684
1685         if (curAlign == layout_->align)
1686                 return column;
1687
1688         switch (curAlign) {
1689         case LYX_ALIGN_NONE:
1690         case LYX_ALIGN_BLOCK:
1691         case LYX_ALIGN_LAYOUT:
1692         case LYX_ALIGN_SPECIAL:
1693                 break;
1694         case LYX_ALIGN_LEFT:
1695         case LYX_ALIGN_RIGHT:
1696         case LYX_ALIGN_CENTER:
1697                 if (moving_arg) {
1698                         os << "\\protect";
1699                         column += 8;
1700                 }
1701                 break;
1702         }
1703
1704         switch (curAlign) {
1705         case LYX_ALIGN_NONE:
1706         case LYX_ALIGN_BLOCK:
1707         case LYX_ALIGN_LAYOUT:
1708         case LYX_ALIGN_SPECIAL:
1709                 break;
1710         case LYX_ALIGN_LEFT: {
1711                 string output;
1712                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1713                         output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1714                 else
1715                         output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1716                 os << from_ascii(output);
1717                 adjust_row_column(output, texrow, column);
1718                 break;
1719         } case LYX_ALIGN_RIGHT: {
1720                 string output;
1721                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1722                         output = corrected_env("\\begin", "flushright", owner_->ownerCode());
1723                 else
1724                         output = corrected_env("\\begin", "flushleft", owner_->ownerCode());
1725                 os << from_ascii(output);
1726                 adjust_row_column(output, texrow, column);
1727                 break;
1728         } case LYX_ALIGN_CENTER: {
1729                 string output;
1730                 output = corrected_env("\\begin", "center", owner_->ownerCode());
1731                 os << from_ascii(output);
1732                 adjust_row_column(output, texrow, column);
1733                 break;
1734         }
1735         }
1736
1737         return column;
1738 }
1739
1740
1741 int Paragraph::Private::endTeXParParams(BufferParams const & bparams,
1742                                odocstream & os, TexRow & texrow,
1743                                bool moving_arg) const
1744 {
1745         int column = 0;
1746
1747         switch (params_.align()) {
1748         case LYX_ALIGN_NONE:
1749         case LYX_ALIGN_BLOCK:
1750         case LYX_ALIGN_LAYOUT:
1751         case LYX_ALIGN_SPECIAL:
1752                 break;
1753         case LYX_ALIGN_LEFT:
1754         case LYX_ALIGN_RIGHT:
1755         case LYX_ALIGN_CENTER:
1756                 if (moving_arg) {
1757                         os << "\\protect";
1758                         column = 8;
1759                 }
1760                 break;
1761         }
1762
1763         switch (params_.align()) {
1764         case LYX_ALIGN_NONE:
1765         case LYX_ALIGN_BLOCK:
1766         case LYX_ALIGN_LAYOUT:
1767         case LYX_ALIGN_SPECIAL:
1768                 break;
1769         case LYX_ALIGN_LEFT: {
1770                 string output;
1771                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1772                         output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1773                 else
1774                         output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1775                 os << from_ascii(output);
1776                 adjust_row_column(output, texrow, column);
1777                 break;
1778         } case LYX_ALIGN_RIGHT: {
1779                 string output;
1780                 if (owner_->getParLanguage(bparams)->babel() != "hebrew")
1781                         output = corrected_env("\n\\par\\end", "flushright", owner_->ownerCode());
1782                 else
1783                         output = corrected_env("\n\\par\\end", "flushleft", owner_->ownerCode());
1784                 os << from_ascii(output);
1785                 adjust_row_column(output, texrow, column);
1786                 break;
1787         } case LYX_ALIGN_CENTER: {
1788                 string output;
1789                 output = corrected_env("\n\\par\\end", "center", owner_->ownerCode());
1790                 os << from_ascii(output);
1791                 adjust_row_column(output, texrow, column);
1792                 break;
1793         }
1794         }
1795
1796         return column;
1797 }
1798
1799
1800 // This one spits out the text of the paragraph
1801 bool Paragraph::latex(Buffer const & buf,
1802                                 BufferParams const & bparams,
1803                                 Font const & outerfont,
1804                                 odocstream & os, TexRow & texrow,
1805                                 OutputParams const & runparams) const
1806 {
1807         LYXERR(Debug::LATEX, "SimpleTeXOnePar...     " << this);
1808
1809         bool return_value = false;
1810
1811         LayoutPtr style;
1812
1813         // well we have to check if we are in an inset with unlimited
1814         // length (all in one row) if that is true then we don't allow
1815         // any special options in the paragraph and also we don't allow
1816         // any environment other than the default layout of the text class
1817         // to be valid!
1818         bool asdefault = forceEmptyLayout();
1819
1820         if (asdefault) {
1821                 style = bparams.getTextClass().defaultLayout();
1822         } else {
1823                 style = d->layout_;
1824         }
1825
1826         // Current base font for all inherited font changes, without any
1827         // change caused by an individual character, except for the language:
1828         // It is set to the language of the first character.
1829         // As long as we are in the label, this font is the base font of the
1830         // label. Before the first body character it is set to the base font
1831         // of the body.
1832         Font basefont;
1833
1834         // Maybe we have to create a optional argument.
1835         pos_type body_pos = beginOfBody();
1836         unsigned int column = 0;
1837
1838         if (body_pos > 0) {
1839                 // the optional argument is kept in curly brackets in
1840                 // case it contains a ']'
1841                 os << "[{";
1842                 column += 2;
1843                 basefont = getLabelFont(bparams, outerfont);
1844         } else {
1845                 basefont = getLayoutFont(bparams, outerfont);
1846         }
1847
1848         // Which font is currently active?
1849         Font running_font(basefont);
1850         // Do we have an open font change?
1851         bool open_font = false;
1852
1853         Change runningChange = Change(Change::UNCHANGED);
1854
1855         texrow.start(id(), 0);
1856
1857         // if the paragraph is empty, the loop will not be entered at all
1858         if (empty()) {
1859                 if (style->isCommand()) {
1860                         os << '{';
1861                         ++column;
1862                 }
1863                 if (!asdefault)
1864                         column += d->startTeXParParams(bparams, os, texrow,
1865                                                     runparams.moving_arg);
1866         }
1867
1868         for (pos_type i = 0; i < size(); ++i) {
1869                 // First char in paragraph or after label?
1870                 if (i == body_pos) {
1871                         if (body_pos > 0) {
1872                                 if (open_font) {
1873                                         column += running_font.latexWriteEndChanges(
1874                                                 os, bparams, runparams,
1875                                                 basefont, basefont);
1876                                         open_font = false;
1877                                 }
1878                                 basefont = getLayoutFont(bparams, outerfont);
1879                                 running_font = basefont;
1880
1881                                 column += Changes::latexMarkChange(os, bparams,
1882                                                 runningChange, Change(Change::UNCHANGED));
1883                                 runningChange = Change(Change::UNCHANGED);
1884
1885                                 os << "}] ";
1886                                 column +=3;
1887                         }
1888                         if (style->isCommand()) {
1889                                 os << '{';
1890                                 ++column;
1891                         }
1892
1893                         if (!asdefault)
1894                                 column += d->startTeXParParams(bparams, os,
1895                                                             texrow,
1896                                                             runparams.moving_arg);
1897                 }
1898
1899                 Change const & change = runparams.inDeletedInset ? runparams.changeOfDeletedInset
1900                                                                  : lookupChange(i);
1901
1902                 if (bparams.outputChanges && runningChange != change) {
1903                         if (open_font) {
1904                                 column += running_font.latexWriteEndChanges(
1905                                                 os, bparams, runparams, basefont, basefont);
1906                                 open_font = false;
1907                         }
1908                         basefont = getLayoutFont(bparams, outerfont);
1909                         running_font = basefont;
1910
1911                         column += Changes::latexMarkChange(os, bparams, runningChange, change);
1912                         runningChange = change;
1913                 }
1914
1915                 // do not output text which is marked deleted
1916                 // if change tracking output is disabled
1917                 if (!bparams.outputChanges && change.type == Change::DELETED) {
1918                         continue;
1919                 }
1920
1921                 ++column;
1922
1923                 // Fully instantiated font
1924                 Font const font = getFont(bparams, i, outerfont);
1925
1926                 Font const last_font = running_font;
1927
1928                 // Do we need to close the previous font?
1929                 if (open_font &&
1930                     (font != running_font ||
1931                      font.language() != running_font.language()))
1932                 {
1933                         column += running_font.latexWriteEndChanges(
1934                                         os, bparams, runparams, basefont,
1935                                         (i == body_pos-1) ? basefont : font);
1936                         running_font = basefont;
1937                         open_font = false;
1938                 }
1939
1940                 // close babel's font environment before opening CJK.
1941                 if (!running_font.language()->babel().empty() &&
1942                     font.language()->encoding()->package() == Encoding::CJK) {
1943                                 string end_tag = subst(lyxrc.language_command_end,
1944                                                         "$$lang",
1945                                                         running_font.language()->babel());
1946                                 os << from_ascii(end_tag);
1947                                 column += end_tag.length();
1948                 }
1949
1950                 // Switch file encoding if necessary (and allowed)
1951                 if (!runparams.verbatim && 
1952                     runparams.encoding->package() == Encoding::none &&
1953                     font.language()->encoding()->package() == Encoding::none) {
1954                         pair<bool, int> const enc_switch = switchEncoding(os, bparams,
1955                                         runparams, *(font.language()->encoding()));
1956                         if (enc_switch.first) {
1957                                 column += enc_switch.second;
1958                                 runparams.encoding = font.language()->encoding();
1959                         }
1960                 }
1961
1962                 char_type const c = d->text_[i];
1963
1964                 // Do we need to change font?
1965                 if ((font != running_font ||
1966                      font.language() != running_font.language()) &&
1967                         i != body_pos - 1)
1968                 {
1969                         odocstringstream ods;
1970                         column += font.latexWriteStartChanges(ods, bparams,
1971                                                               runparams, basefont,
1972                                                               last_font);
1973                         running_font = font;
1974                         open_font = true;
1975                         docstring fontchange = ods.str();
1976                         // check if the fontchange ends with a trailing blank
1977                         // (like "\small " (see bug 3382)
1978                         if (suffixIs(fontchange, ' ') && c == ' ')
1979                                 os << fontchange.substr(0, fontchange.size() - 1) 
1980                                    << from_ascii("{}");
1981                         else
1982                                 os << fontchange;
1983                 }
1984
1985                 if (c == ' ') {
1986                         // FIXME: integrate this case in latexSpecialChar
1987                         // Do not print the separation of the optional argument
1988                         // if style->pass_thru is false. This works because
1989                         // latexSpecialChar ignores spaces if
1990                         // style->pass_thru is false.
1991                         if (i != body_pos - 1) {
1992                                 if (d->simpleTeXBlanks(
1993                                                 runparams, os, texrow,
1994                                                 i, column, font, *style)) {
1995                                         // A surrogate pair was output. We
1996                                         // must not call latexSpecialChar
1997                                         // in this iteration, since it would output
1998                                         // the combining character again.
1999                                         ++i;
2000                                         continue;
2001                                 }
2002                         }
2003                 }
2004
2005                 OutputParams rp = runparams;
2006                 rp.free_spacing = style->free_spacing;
2007                 rp.local_font = &font;
2008                 rp.intitle = style->intitle;
2009
2010                 // Two major modes:  LaTeX or plain
2011                 // Handle here those cases common to both modes
2012                 // and then split to handle the two modes separately.
2013                 if (c == META_INSET)
2014                         d->latexInset(buf, bparams, os,
2015                                         texrow, rp, running_font,
2016                                         basefont, outerfont, open_font,
2017                                         runningChange, *style, i, column);
2018                 else {
2019                         try {
2020                                 d->latexSpecialChar(os, rp, running_font, runningChange,
2021                                         *style, i, column);
2022                         } catch (EncodingException & e) {
2023                                 if (runparams.dryrun) {
2024                                         os << "<" << _("LyX Warning: ")
2025                                            << _("uncodable character") << " '";
2026                                         os.put(c);
2027                                         os << "'>";
2028                                 } else {
2029                                         // add location information and throw again.
2030                                         e.par_id = id();
2031                                         e.pos = i;
2032                                         throw(e);
2033                                 }
2034                         }
2035                 }
2036
2037                 // Set the encoding to that returned from simpleTeXSpecialChars (see
2038                 // comment for encoding member in OutputParams.h)
2039                 runparams.encoding = rp.encoding;
2040         }
2041
2042         // If we have an open font definition, we have to close it
2043         if (open_font) {
2044 #ifdef FIXED_LANGUAGE_END_DETECTION
2045                 if (next_) {
2046                         running_font
2047                                 .latexWriteEndChanges(os, bparams, runparams,
2048                                         basefont,
2049                                         next_->getFont(bparams, 0, outerfont));
2050                 } else {
2051                         running_font.latexWriteEndChanges(os, bparams,
2052                                         runparams, basefont, basefont);
2053                 }
2054 #else
2055 //FIXME: For now we ALWAYS have to close the foreign font settings if they are
2056 //FIXME: there as we start another \selectlanguage with the next paragraph if
2057 //FIXME: we are in need of this. This should be fixed sometime (Jug)
2058                 running_font.latexWriteEndChanges(os, bparams, runparams,
2059                                 basefont, basefont);
2060 #endif
2061         }
2062
2063         column += Changes::latexMarkChange(os, bparams, runningChange, Change(Change::UNCHANGED));
2064
2065         // Needed if there is an optional argument but no contents.
2066         if (body_pos > 0 && body_pos == size()) {
2067                 os << "}]~";
2068                 return_value = false;
2069         }
2070
2071         if (!asdefault) {
2072                 column += d->endTeXParParams(bparams, os, texrow,
2073                                           runparams.moving_arg);
2074         }
2075
2076         LYXERR(Debug::LATEX, "SimpleTeXOnePar...done " << this);
2077         return return_value;
2078 }
2079
2080
2081 bool Paragraph::emptyTag() const
2082 {
2083         for (pos_type i = 0; i < size(); ++i) {
2084                 if (Inset const * inset = getInset(i)) {
2085                         InsetCode lyx_code = inset->lyxCode();
2086                         if (lyx_code != TOC_CODE &&
2087                             lyx_code != INCLUDE_CODE &&
2088                             lyx_code != GRAPHICS_CODE &&
2089                             lyx_code != ERT_CODE &&
2090                             lyx_code != LISTINGS_CODE &&
2091                             lyx_code != FLOAT_CODE &&
2092                             lyx_code != TABULAR_CODE) {
2093                                 return false;
2094                         }
2095                 } else {
2096                         char_type c = d->text_[i];
2097                         if (c != ' ' && c != '\t')
2098                                 return false;
2099                 }
2100         }
2101         return true;
2102 }
2103
2104
2105 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
2106 {
2107         for (pos_type i = 0; i < size(); ++i) {
2108                 if (Inset const * inset = getInset(i)) {
2109                         InsetCode lyx_code = inset->lyxCode();
2110                         if (lyx_code == LABEL_CODE) {
2111                                 InsetLabel const * const il = static_cast<InsetLabel const *>(inset);
2112                                 docstring const & id = il->getParam("name");
2113                                 return "id='" + to_utf8(sgml::cleanID(buf, runparams, id)) + "'";
2114                         }
2115                 }
2116         }
2117         return string();
2118 }
2119
2120
2121 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
2122 {
2123         pos_type i;
2124         for (i = 0; i < size(); ++i) {
2125                 if (Inset const * inset = getInset(i)) {
2126                         inset->docbook(buf, os, runparams);
2127                 } else {
2128                         char_type c = d->text_[i];
2129                         if (c == ' ')
2130                                 break;
2131                         os << sgml::escapeChar(c);
2132                 }
2133         }
2134         return i;
2135 }
2136
2137
2138 bool Paragraph::Private::onlyText(Buffer const & buf, Font const & outerfont, pos_type initial) const
2139 {
2140         Font font_old;
2141         pos_type size = text_.size();
2142         for (pos_type i = initial; i < size; ++i) {
2143                 Font font = owner_->getFont(buf.params(), i, outerfont);
2144                 if (text_[i] == META_INSET)
2145                         return false;
2146                 if (i != initial && font != font_old)
2147                         return false;
2148                 font_old = font;
2149         }
2150
2151         return true;
2152 }
2153
2154
2155 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
2156                                     odocstream & os,
2157                                     OutputParams const & runparams,
2158                                     Font const & outerfont,
2159                                     pos_type initial) const
2160 {
2161         bool emph_flag = false;
2162
2163         LayoutPtr const & style = d->layout_;
2164         FontInfo font_old =
2165                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
2166
2167         if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2168                 os << "]]>";
2169
2170         // parsing main loop
2171         for (pos_type i = initial; i < size(); ++i) {
2172                 Font font = getFont(buf.params(), i, outerfont);
2173
2174                 // handle <emphasis> tag
2175                 if (font_old.emph() != font.fontInfo().emph()) {
2176                         if (font.fontInfo().emph() == FONT_ON) {
2177                                 os << "<emphasis>";
2178                                 emph_flag = true;
2179                         } else if (i != initial) {
2180                                 os << "</emphasis>";
2181                                 emph_flag = false;
2182                         }
2183                 }
2184
2185                 if (Inset const * inset = getInset(i)) {
2186                         inset->docbook(buf, os, runparams);
2187                 } else {
2188                         char_type c = d->text_[i];
2189
2190                         if (style->pass_thru)
2191                                 os.put(c);
2192                         else
2193                                 os << sgml::escapeChar(c);
2194                 }
2195                 font_old = font.fontInfo();
2196         }
2197
2198         if (emph_flag) {
2199                 os << "</emphasis>";
2200         }
2201
2202         if (style->free_spacing)
2203                 os << '\n';
2204         if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
2205                 os << "<![CDATA[";
2206 }
2207
2208
2209 bool Paragraph::isHfill(pos_type pos) const
2210 {
2211         Inset const * inset = getInset(pos);
2212         return inset && inset->lyxCode() == HFILL_CODE;
2213 }
2214
2215
2216 bool Paragraph::isNewline(pos_type pos) const
2217 {
2218         Inset const * inset = getInset(pos);
2219         return inset && inset->lyxCode() == NEWLINE_CODE;
2220 }
2221
2222
2223 bool Paragraph::isLineSeparator(pos_type pos) const
2224 {
2225         char_type const c = d->text_[pos];
2226         if (isLineSeparatorChar(c))
2227                 return true;
2228         Inset const * inset = getInset(pos);
2229         return inset && inset->isLineSeparator();
2230 }
2231
2232
2233 /// Used by the spellchecker
2234 bool Paragraph::isLetter(pos_type pos) const
2235 {
2236         if (Inset const * inset = getInset(pos))
2237                 return inset->isLetter();
2238         char_type const c = d->text_[pos];
2239         return isLetterChar(c) || isDigit(c);
2240 }
2241
2242
2243 Language const *
2244 Paragraph::getParLanguage(BufferParams const & bparams) const
2245 {
2246         if (!empty())
2247                 return getFirstFontSettings(bparams).language();
2248         // FIXME: we should check the prev par as well (Lgb)
2249         return bparams.language;
2250 }
2251
2252
2253 bool Paragraph::isRTL(BufferParams const & bparams) const
2254 {
2255         return lyxrc.rtl_support
2256                 && getParLanguage(bparams)->rightToLeft()
2257                 && ownerCode() != ERT_CODE
2258                 && ownerCode() != LISTINGS_CODE;
2259 }
2260
2261
2262 void Paragraph::changeLanguage(BufferParams const & bparams,
2263                                Language const * from, Language const * to)
2264 {
2265         // change language including dummy font change at the end
2266         for (pos_type i = 0; i <= size(); ++i) {
2267                 Font font = getFontSettings(bparams, i);
2268                 if (font.language() == from) {
2269                         font.setLanguage(to);
2270                         setFont(i, font);
2271                 }
2272         }
2273 }
2274
2275
2276 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
2277 {
2278         Language const * doc_language = bparams.language;
2279         FontList::const_iterator cit = d->fontlist_.begin();
2280         FontList::const_iterator end = d->fontlist_.end();
2281
2282         for (; cit != end; ++cit)
2283                 if (cit->font().language() != ignore_language &&
2284                     cit->font().language() != latex_language &&
2285                     cit->font().language() != doc_language)
2286                         return true;
2287         return false;
2288 }
2289
2290
2291 // Convert the paragraph to a string.
2292 // Used for building the table of contents
2293 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
2294 {
2295         return asString(buffer, 0, size(), label);
2296 }
2297
2298
2299 docstring const Paragraph::asString(Buffer const & buffer,
2300                                  pos_type beg, pos_type end, bool label) const
2301 {
2302
2303         odocstringstream os;
2304
2305         if (beg == 0 && label && !d->params_.labelString().empty())
2306                 os << d->params_.labelString() << ' ';
2307
2308         for (pos_type i = beg; i < end; ++i) {
2309                 char_type const c = d->text_[i];
2310                 if (isPrintable(c))
2311                         os.put(c);
2312                 else if (c == META_INSET)
2313                         getInset(i)->textString(buffer, os);
2314         }
2315
2316         return os.str();
2317 }
2318
2319
2320 void Paragraph::setInsetOwner(Inset * inset)
2321 {
2322         d->inset_owner_ = inset;
2323 }
2324
2325
2326 int Paragraph::id() const
2327 {
2328         return d->id_;
2329 }
2330
2331
2332 LayoutPtr const & Paragraph::layout() const
2333 {
2334         return d->layout_;
2335 }
2336
2337
2338 void Paragraph::layout(LayoutPtr const & new_layout)
2339 {
2340         d->layout_ = new_layout;
2341 }
2342
2343
2344 Inset * Paragraph::inInset() const
2345 {
2346         return d->inset_owner_;
2347 }
2348
2349
2350 InsetCode Paragraph::ownerCode() const
2351 {
2352         return d->inset_owner_ ? d->inset_owner_->lyxCode() : NO_CODE;
2353 }
2354
2355
2356 ParagraphParameters & Paragraph::params()
2357 {
2358         return d->params_;
2359 }
2360
2361
2362 ParagraphParameters const & Paragraph::params() const
2363 {
2364         return d->params_;
2365 }
2366
2367
2368 bool Paragraph::isFreeSpacing() const
2369 {
2370         if (d->layout_->free_spacing)
2371                 return true;
2372         return d->inset_owner_ && d->inset_owner_->isFreeSpacing();
2373 }
2374
2375
2376 bool Paragraph::allowEmpty() const
2377 {
2378         if (d->layout_->keepempty)
2379                 return true;
2380         return d->inset_owner_ && d->inset_owner_->allowEmpty();
2381 }
2382
2383
2384 char_type Paragraph::transformChar(char_type c, pos_type pos) const
2385 {
2386         if (!Encodings::is_arabic(c))
2387                 return c;
2388
2389         char_type prev_char = ' ';
2390         char_type next_char = ' ';
2391
2392         for (pos_type i = pos - 1; i >= 0; --i) {
2393                 char_type const par_char = d->text_[i];
2394                 if (!Encodings::isComposeChar_arabic(par_char)) {
2395                         prev_char = par_char;
2396                         break;
2397                 }
2398         }
2399
2400         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
2401                 char_type const par_char = d->text_[i];
2402                 if (!Encodings::isComposeChar_arabic(par_char)) {
2403                         next_char = par_char;
2404                         break;
2405                 }
2406         }
2407
2408         if (Encodings::is_arabic(next_char)) {
2409                 if (Encodings::is_arabic(prev_char) &&
2410                         !Encodings::is_arabic_special(prev_char))
2411                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
2412                 else
2413                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
2414         } else {
2415                 if (Encodings::is_arabic(prev_char) &&
2416                         !Encodings::is_arabic_special(prev_char))
2417                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
2418                 else
2419                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
2420         }
2421 }
2422
2423
2424 int Paragraph::checkBiblio(bool track_changes)
2425 {
2426         //FIXME From JS:
2427         //This is getting more and more a mess. ...We really should clean
2428         //up this bibitem issue for 1.6. See also bug 2743.
2429
2430         // Add bibitem insets if necessary
2431         if (d->layout_->labeltype != LABEL_BIBLIO)
2432                 return 0;
2433
2434         bool hasbibitem = !d->insetlist_.empty()
2435                 // Insist on it being in pos 0
2436                 && d->text_[0] == META_INSET
2437                 && d->insetlist_.begin()->inset->lyxCode() == BIBITEM_CODE;
2438
2439         docstring oldkey;
2440         docstring oldlabel;
2441
2442         // remove a bibitem in pos != 0
2443         // restore it later in pos 0 if necessary
2444         // (e.g. if a user inserts contents _before_ the item)
2445         // we're assuming there's only one of these, which there
2446         // should be.
2447         int erasedInsetPosition = -1;
2448         InsetList::iterator it = d->insetlist_.begin();
2449         InsetList::iterator end = d->insetlist_.end();
2450         for (; it != end; ++it)
2451                 if (it->inset->lyxCode() == BIBITEM_CODE
2452                     && it->pos > 0) {
2453                         InsetBibitem * olditem = static_cast<InsetBibitem *>(it->inset);
2454                         oldkey = olditem->getParam("key");
2455                         oldlabel = olditem->getParam("label");
2456                         erasedInsetPosition = it->pos;
2457                         eraseChar(erasedInsetPosition, track_changes);
2458                         break;
2459         }
2460
2461         //There was an InsetBibitem at the beginning, and we didn't
2462         //have to erase one.
2463         if (hasbibitem && erasedInsetPosition < 0)
2464                         return 0;
2465
2466         //There was an InsetBibitem at the beginning and we did have to
2467         //erase one. So we give its properties to the beginning inset.
2468         if (hasbibitem) {
2469                 InsetBibitem * inset =
2470                         static_cast<InsetBibitem *>(d->insetlist_.begin()->inset);
2471                 if (!oldkey.empty())
2472                         inset->setParam("key", oldkey);
2473                 inset->setParam("label", oldlabel);
2474                 return -erasedInsetPosition;
2475         }
2476
2477         //There was no inset at the beginning, so we need to create one with
2478         //the key and label of the one we erased.
2479         InsetBibitem * inset(new InsetBibitem(InsetCommandParams(BIBITEM_CODE)));
2480         // restore values of previously deleted item in this par.
2481         if (!oldkey.empty())
2482                 inset->setParam("key", oldkey);
2483         inset->setParam("label", oldlabel);
2484         insertInset(0, static_cast<Inset *>(inset),
2485                     Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
2486
2487         return 1;
2488 }
2489
2490
2491 void Paragraph::checkAuthors(AuthorList const & authorList)
2492 {
2493         d->changes_.checkAuthors(authorList);
2494 }
2495
2496
2497 bool Paragraph::isUnchanged(pos_type pos) const
2498 {
2499         return lookupChange(pos).type == Change::UNCHANGED;
2500 }
2501
2502
2503 bool Paragraph::isInserted(pos_type pos) const
2504 {
2505         return lookupChange(pos).type == Change::INSERTED;
2506 }
2507
2508
2509 bool Paragraph::isDeleted(pos_type pos) const
2510 {
2511         return lookupChange(pos).type == Change::DELETED;
2512 }
2513
2514
2515 InsetList const & Paragraph::insetList() const
2516 {
2517         return d->insetlist_;
2518 }
2519
2520
2521 Inset * Paragraph::releaseInset(pos_type pos)
2522 {
2523         Inset * inset = d->insetlist_.release(pos);
2524         /// does not honour change tracking!
2525         eraseChar(pos, false);
2526         return inset;
2527 }
2528
2529
2530 Inset * Paragraph::getInset(pos_type pos)
2531 {
2532         return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2533                  ? d->insetlist_.get(pos) : 0;
2534 }
2535
2536
2537 Inset const * Paragraph::getInset(pos_type pos) const
2538 {
2539         return (pos < pos_type(d->text_.size()) && d->text_[pos] == META_INSET)
2540                  ? d->insetlist_.get(pos) : 0;
2541 }
2542
2543
2544 void Paragraph::changeCase(BufferParams const & bparams, pos_type pos,
2545                 pos_type & right, TextCase action)
2546 {
2547         // process sequences of modified characters; in change
2548         // tracking mode, this approach results in much better
2549         // usability than changing case on a char-by-char basis
2550         docstring changes;
2551
2552         bool const trackChanges = bparams.trackChanges;
2553
2554         bool capitalize = true;
2555
2556         for (; pos < right; ++pos) {
2557                 char_type oldChar = d->text_[pos];
2558                 char_type newChar = oldChar;
2559
2560                 // ignore insets and don't play with deleted text!
2561                 if (oldChar != META_INSET && !isDeleted(pos)) {
2562                         switch (action) {
2563                                 case text_lowercase:
2564                                         newChar = lowercase(oldChar);
2565                                         break;
2566                                 case text_capitalization:
2567                                         if (capitalize) {
2568                                                 newChar = uppercase(oldChar);
2569                                                 capitalize = false;
2570                                         }
2571                                         break;
2572                                 case text_uppercase:
2573                                         newChar = uppercase(oldChar);
2574                                         break;
2575                         }
2576                 }
2577
2578                 if (!isLetter(pos) || isDeleted(pos)) {
2579                         // permit capitalization again
2580                         capitalize = true;
2581                 }
2582
2583                 if (oldChar != newChar)
2584                         changes += newChar;
2585
2586                 if (oldChar == newChar || pos == right - 1) {
2587                         if (oldChar != newChar) {
2588                                 // step behind the changing area
2589                                 pos++;
2590                         }
2591                         int erasePos = pos - changes.size();
2592                         for (size_t i = 0; i < changes.size(); i++) {
2593                                 insertChar(pos, changes[i],
2594                                         getFontSettings(bparams,
2595                                         erasePos),
2596                                         trackChanges);
2597                                 if (!eraseChar(erasePos, trackChanges)) {
2598                                         ++erasePos;
2599                                         ++pos; // advance
2600                                         ++right; // expand selection
2601                                 }
2602                         }
2603                         changes.clear();
2604                 }
2605         }
2606 }
2607
2608
2609 bool Paragraph::find(docstring const & str, bool cs, bool mw,
2610                 pos_type pos, bool del) const
2611 {
2612         int const strsize = str.length();
2613         int i = 0;
2614         pos_type const parsize = d->text_.size();
2615         for (i = 0; pos + i < parsize; ++i) {
2616                 if (i >= strsize)
2617                         break;
2618                 if (cs && str[i] != d->text_[pos + i])
2619                         break;
2620                 if (!cs && uppercase(str[i]) != uppercase(d->text_[pos + i]))
2621                         break;
2622                 if (!del && isDeleted(pos + i))
2623                         break;
2624         }
2625
2626         if (i != strsize)
2627                 return false;
2628
2629         // if necessary, check whether string matches word
2630         if (mw) {
2631                 if (pos > 0 && isLetter(pos - 1))
2632                         return false;
2633                 if (pos + strsize < parsize
2634                         && isLetter(pos + strsize))
2635                         return false;
2636         }
2637
2638         return true;
2639 }
2640
2641
2642 char_type Paragraph::getChar(pos_type pos) const
2643 {
2644         return d->text_[pos];
2645 }
2646
2647
2648 pos_type Paragraph::size() const
2649 {
2650         return d->text_.size();
2651 }
2652
2653
2654 bool Paragraph::empty() const
2655 {
2656         return d->text_.empty();
2657 }
2658
2659
2660 bool Paragraph::isInset(pos_type pos) const
2661 {
2662         return d->text_[pos] == META_INSET;
2663 }
2664
2665
2666 bool Paragraph::isSeparator(pos_type pos) const
2667 {
2668         //FIXME: Are we sure this can be the only separator?
2669         return d->text_[pos] == ' ';
2670 }
2671
2672
2673 } // namespace lyx