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