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