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