]> git.lyx.org Git - lyx.git/blob - src/paragraph_pimpl.C
1d9512a328e87d4da8f2cfd9ff783066401d6dfd
[lyx.git] / src / paragraph_pimpl.C
1 /**
2  * \file paragraph_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "paragraph_pimpl.h"
17 #include "paragraph.h"
18
19 #include "bufferparams.h"
20 #include "debug.h"
21 #include "encoding.h"
22 #include "language.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlength.h"
26 #include "lyxrc.h"
27 #include "outputparams.h"
28 #include "texrow.h"
29
30 #include <boost/next_prior.hpp>
31
32
33 namespace lyx {
34
35 using std::endl;
36 using std::upper_bound;
37 using std::lower_bound;
38 using std::string;
39
40
41 // Initialization of the counter for the paragraph id's,
42 unsigned int Paragraph::Pimpl::paragraph_id = 0;
43
44 namespace {
45
46 struct special_phrase {
47         string phrase;
48         docstring macro;
49         bool builtin;
50 };
51
52 special_phrase const special_phrases[] = {
53         { "LyX", from_ascii("\\LyX{}"), false },
54         { "TeX", from_ascii("\\TeX{}"), true },
55         { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
56         { "LaTeX", from_ascii("\\LaTeX{}"), true },
57 };
58
59 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
60
61 } // namespace anon
62
63
64 Paragraph::Pimpl::Pimpl(Paragraph * owner)
65         : owner_(owner)
66 {
67         inset_owner = 0;
68         id_ = paragraph_id++;
69 }
70
71
72 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
73         : params(p.params), changes_(p.changes_), owner_(owner)
74 {
75         inset_owner = p.inset_owner;
76         fontlist = p.fontlist;
77         id_ = paragraph_id++;
78 }
79
80
81 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
82 {
83         owner_->text_ = par.text_;
84         // FIXME: change tracking (MG)
85         // check whether this method is really needed
86         changes_ = par.pimpl_->changes_;
87 }
88
89
90 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
91 {
92         return changes_.isChanged(start, end);
93 }
94
95
96 void Paragraph::Pimpl::setChange(Change const & change)
97 {
98         // FIXME: change tracking (MG)
99         // how about end-of-line? size()+1?
100         changes_.set(change, 0, size());
101
102         if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
103                 for (pos_type i = 0; i < size(); ++i) {
104                         if (owner_->isInset(i)) {
105                                 owner_->getInset(i)->setChange(change);
106                         }
107                 }
108         }
109 }
110
111
112 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
113 {
114         changes_.set(change, pos);
115
116         // FIXME: change tracking (MG)
117         // do we have to set the change recursively?
118         if (pos < size() && owner_->isInset(pos)) {
119                 owner_->getInset(pos)->setChange(change);
120         }
121 }
122
123
124 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
125 {
126         return changes_.lookup(pos);
127 }
128
129
130 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
131 {
132         for (pos_type pos = start; pos < end; ++pos) {
133                 switch (lookupChange(pos).type) {
134                         case Change::UNCHANGED:
135                                 break;
136
137                         case Change::INSERTED:
138                                 changes_.set(Change(Change::UNCHANGED), pos);
139                                 break;
140
141                         case Change::DELETED:
142                                 // Suppress access to nonexistent
143                                 // "end-of-paragraph char":
144                                 if (pos < size()) {
145                                         eraseChar(pos, false);
146                                         --end;
147                                         --pos;
148                                 }
149                                 break;
150                 }
151
152                 // also accept changes in nested insets
153                 if (pos < size() && owner_->isInset(pos)) {
154                         owner_->getInset(pos)->acceptChanges();
155                 }
156         }
157 }
158
159
160 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
161 {
162         // FIXME: change tracking (MG)
163         return;
164
165         // care for empty pars
166
167         pos_type i = start;
168
169         for (; i < end; ++i) {
170                 switch (lookupChange(i).type) {
171                         case Change::UNCHANGED:
172                                 break;
173
174                         case Change::INSERTED:
175                                 if (i < size()) {
176                                         eraseChar(i, false);
177                                         --end;
178                                         --i;
179                                 }
180                                 break;
181
182                         case Change::DELETED:
183                                 // FIXME: change tracking (MG)
184                                 changes_.set(Change(Change::UNCHANGED), i);
185                                 // No real char at position size():
186                                 if (i < size() && owner_->isInset(i))
187                                         // FIXME: change tracking (MG)
188                                         owner_->getInset(i)->setChange(Change(Change::UNCHANGED));
189                                 break;
190                 }
191         }
192         // FIXME: change tracking (MG)
193         // changes_.reset(Change::UNCHANGED);
194 }
195
196
197 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
198 {
199         return owner_->getChar(pos);
200 }
201
202
203 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
204 {
205         BOOST_ASSERT(pos <= size());
206
207         // track change
208         changes_.insert(change, pos);
209
210         // This is actually very common when parsing buffers (and
211         // maybe inserting ascii text)
212         if (pos == size()) {
213                 // when appending characters, no need to update tables
214                 owner_->text_.push_back(c);
215                 return;
216         }
217
218         owner_->text_.insert(owner_->text_.begin() + pos, c);
219
220         // Update the font table.
221         FontTable search_font(pos, LyXFont());
222         for (FontList::iterator it 
223               = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
224              it != fontlist.end(); ++it)
225         {
226                 it->pos(it->pos() + 1);
227         }
228
229         // Update the insets
230         owner_->insetlist.increasePosAfterPos(pos);
231 }
232
233
234 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
235                                    Change const & change)
236 {
237         BOOST_ASSERT(inset);
238         BOOST_ASSERT(pos <= size());
239
240         insertChar(pos, META_INSET, change);
241         BOOST_ASSERT(owner_->text_[pos] == META_INSET);
242
243         // Add a new entry in the insetlist.
244         owner_->insetlist.insert(inset, pos);
245 }
246
247
248 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
249 {
250         BOOST_ASSERT(pos <= size());
251
252         if (trackChanges) {
253                 Change::Type changetype(changes_.lookup(pos).type);
254
255                 if (changetype == Change::UNCHANGED) {
256                         setChange(pos, Change(Change::DELETED));
257                         return false;
258                 }
259
260                 if (changetype == Change::DELETED)
261                         return false;
262         }
263
264         // Don't physically access nonexistent end-of-paragraph char
265         if (pos == size()) {
266                 // FIXME: change tracking (MG)
267                 // how do we handle end-of-pars previously marked inserted?
268                 return false;
269         }
270
271         // track change
272         changes_.erase(pos);
273
274         // if it is an inset, delete the inset entry
275         if (owner_->text_[pos] == Paragraph::META_INSET) {
276                 owner_->insetlist.erase(pos);
277         }
278
279         owner_->text_.erase(owner_->text_.begin() + pos);
280
281         // Erase entries in the tables.
282         FontTable search_font(pos, LyXFont());
283
284         FontList::iterator it =
285                 lower_bound(fontlist.begin(),
286                             fontlist.end(),
287                             search_font, matchFT());
288         if (it != fontlist.end() && it->pos() == pos &&
289             (pos == 0 ||
290              (it != fontlist.begin()
291               && boost::prior(it)->pos() == pos - 1))) {
292                 // If it is a multi-character font
293                 // entry, we just make it smaller
294                 // (see update below), otherwise we
295                 // should delete it.
296                 unsigned int const i = it - fontlist.begin();
297                 fontlist.erase(fontlist.begin() + i);
298                 it = fontlist.begin() + i;
299                 if (i > 0 && i < fontlist.size() &&
300                     fontlist[i - 1].font() == fontlist[i].font()) {
301                         fontlist.erase(fontlist.begin() + i - 1);
302                         it = fontlist.begin() + i - 1;
303                 }
304         }
305
306         // Update all other entries
307         FontList::iterator fend = fontlist.end();
308         for (; it != fend; ++it)
309                 it->pos(it->pos() - 1);
310
311         // Update the insetlist
312         owner_->insetlist.decreasePosAfterPos(pos);
313
314         return true;
315 }
316
317
318 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
319 {
320         pos_type i = start;
321         for (pos_type count = end - start; count; --count) {
322                 if (!eraseChar(i, trackChanges))
323                         ++i;
324         }
325         return end - i;
326 }
327
328
329 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
330                                        pos_type const i,
331                                        unsigned int & column,
332                                        LyXFont const & font,
333                                        LyXLayout const & style)
334 {
335         if (style.pass_thru)
336                 return;
337
338         if (column > lyxrc.ascii_linelen
339             && i
340             && getChar(i - 1) != ' '
341             && (i < size() - 1)
342             // same in FreeSpacing mode
343             && !owner_->isFreeSpacing()
344             // In typewriter mode, we want to avoid
345             // ! . ? : at the end of a line
346             && !(font.family() == LyXFont::TYPEWRITER_FAMILY
347                  && (getChar(i - 1) == '.'
348                      || getChar(i - 1) == '?'
349                      || getChar(i - 1) == ':'
350                      || getChar(i - 1) == '!'))) {
351                 os << '\n';
352                 texrow.newline();
353                 texrow.start(owner_->id(), i + 1);
354                 column = 0;
355         } else if (style.free_spacing) {
356                 os << '~';
357         } else {
358                 os << ' ';
359         }
360 }
361
362
363 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
364 {
365         pos_type const len = str.length();
366
367         // is the paragraph large enough?
368         if (pos + len > size())
369                 return false;
370
371         // does the wanted text start at point?
372         for (string::size_type i = 0; i < str.length(); ++i) {
373                 if (str[i] != owner_->text_[pos + i])
374                         return false;
375         }
376
377         // is there a font change in middle of the word?
378         FontList::const_iterator cit = fontlist.begin();
379         FontList::const_iterator end = fontlist.end();
380         for (; cit != end; ++cit) {
381                 if (cit->pos() >= pos)
382                         break;
383         }
384         if (cit != end && pos + len - 1 > cit->pos())
385                 return false;
386
387         return true;
388 }
389
390
391 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
392                                              BufferParams const & bparams,
393                                              odocstream & os,
394                                              TexRow & texrow,
395                                              OutputParams const & runparams,
396                                              LyXFont & font,
397                                              LyXFont & running_font,
398                                              LyXFont & basefont,
399                                              LyXFont const & outerfont,
400                                              bool & open_font,
401                                              Change::Type & running_change,
402                                              LyXLayout const & style,
403                                              pos_type & i,
404                                              unsigned int & column,
405                                              value_type const c)
406 {
407         if (style.pass_thru) {
408                 if (c != Paragraph::META_INSET) {
409                         if (c != '\0')
410                                 os.put(c);
411                 } else
412                         owner_->getInset(i)->plaintext(buf, os, runparams);
413                 return;
414         }
415
416         // Two major modes:  LaTeX or plain
417         // Handle here those cases common to both modes
418         // and then split to handle the two modes separately.
419         switch (c) {
420         case Paragraph::META_INSET: {
421                 InsetBase * inset = owner_->getInset(i);
422
423                 // FIXME: remove this check
424                 if (!inset)
425                         break;
426
427                 // FIXME: move this to InsetNewline::latex
428                 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
429                         // newlines are handled differently here than
430                         // the default in simpleTeXSpecialChars().
431                         if (!style.newline_allowed) {
432                                 os << '\n';
433                         } else {
434                                 if (open_font) {
435                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
436                                         open_font = false;
437                                 }
438                                 basefont = owner_->getLayoutFont(bparams, outerfont);
439                                 running_font = basefont;
440
441                                 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
442                                         os << '~';
443
444                                 if (runparams.moving_arg)
445                                         os << "\\protect ";
446
447                                 os << "\\\\\n";
448                         }
449                         texrow.newline();
450                         texrow.start(owner_->id(), i + 1);
451                         column = 0;
452                         break;
453                 }
454
455                 // output change tracking marks only if desired,
456                 // if dvipost is installed,
457                 // and with dvi/ps (other formats don't work)
458                 LaTeXFeatures features(buf, bparams, runparams);
459                 bool const output = bparams.outputChanges
460                         && runparams.flavor == OutputParams::LATEX
461                         && features.isAvailable("dvipost");
462
463                 if (inset->canTrackChanges()) {
464                         column += Changes::latexMarkChange(os, running_change,
465                                 Change::UNCHANGED, output);
466                         running_change = Change::UNCHANGED;
467                 }
468
469                 bool close = false;
470                 odocstream::pos_type const len = os.tellp();
471
472                 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
473                      || inset->lyxCode() == InsetBase::MATH_CODE
474                      || inset->lyxCode() == InsetBase::URL_CODE)
475                     && running_font.isRightToLeft()) {
476                         os << "\\L{";
477                         close = true;
478                 }
479
480 #ifdef WITH_WARNINGS
481 #warning Bug: we can have an empty font change here!
482 // if there has just been a font change, we are going to close it
483 // right now, which means stupid latex code like \textsf{}. AFAIK,
484 // this does not harm dvi output. A minor bug, thus (JMarc)
485 #endif
486                 // some insets cannot be inside a font change command
487                 if (open_font && inset->noFontChange()) {
488                         column +=running_font.
489                                 latexWriteEndChanges(os,
490                                                      basefont,
491                                                      basefont);
492                         open_font = false;
493                         basefont = owner_->getLayoutFont(bparams, outerfont);
494                         running_font = basefont;
495                 }
496
497                 int tmp = inset->latex(buf, os, runparams);
498
499                 if (close)
500                         os << '}';
501
502                 if (tmp) {
503                         for (int j = 0; j < tmp; ++j) {
504                                 texrow.newline();
505                         }
506                         texrow.start(owner_->id(), i + 1);
507                         column = 0;
508                 } else {
509                         column += os.tellp() - len;
510                 }
511         }
512         break;
513
514         default:
515                 // And now for the special cases within each mode
516
517                 switch (c) {
518                 case '\\':
519                         os << "\\textbackslash{}";
520                         column += 15;
521                         break;
522
523                 // The following characters could be written literally in latin1, but they
524                 // would be wrongly converted on systems where char is signed, so we give
525                 // the code points.
526                 // This also makes us independant from the encoding of this source file.
527                 case 0xb1:    // ± PLUS-MINUS SIGN
528                 case 0xb2:    // ² SUPERSCRIPT TWO
529                 case 0xb3:    // ³ SUPERSCRIPT THREE
530                 case 0xd7:    // × MULTIPLICATION SIGN
531                 case 0xf7:    // ÷ DIVISION SIGN
532                 case 0xb9:    // ¹ SUPERSCRIPT ONE
533                 case 0xac:    // ¬ NOT SIGN
534                 case 0xb5:    // µ MICRO SIGN
535                         if ((bparams.inputenc == "latin1" ||
536                              bparams.inputenc == "latin9") ||
537                             (bparams.inputenc == "auto" &&
538                              (font.language()->encoding()->latexName()
539                               == "latin1" ||
540                               font.language()->encoding()->latexName()
541                               == "latin9"))) {
542                                 os << "\\ensuremath{";
543                                 os.put(c);
544                                 os << '}';
545                                 column += 13;
546                         } else {
547                                 os.put(c);
548                         }
549                         break;
550
551                 case '|': case '<': case '>':
552                         // In T1 encoding, these characters exist
553                         if (lyxrc.fontenc == "T1") {
554                                 os.put(c);
555                                 //... but we should avoid ligatures
556                                 if ((c == '>' || c == '<')
557                                     && i <= size() - 2
558                                     && getChar(i + 1) == c) {
559                                         //os << "\\textcompwordmark{}";
560                                         // Jean-Marc, have a look at
561                                         // this. I think this works
562                                         // equally well:
563                                         os << "\\,{}";
564                                         // Lgb
565                                         column += 19;
566                                 }
567                                 break;
568                         }
569                         // Typewriter font also has them
570                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
571                                 os.put(c);
572                                 break;
573                         }
574                         // Otherwise, we use what LaTeX
575                         // provides us.
576                         switch (c) {
577                         case '<':
578                                 os << "\\textless{}";
579                                 column += 10;
580                                 break;
581                         case '>':
582                                 os << "\\textgreater{}";
583                                 column += 13;
584                                 break;
585                         case '|':
586                                 os << "\\textbar{}";
587                                 column += 9;
588                                 break;
589                         }
590                         break;
591
592                 case '-': // "--" in Typewriter mode -> "-{}-"
593                         if (i <= size() - 2
594                             && getChar(i + 1) == '-'
595                             && font.family() == LyXFont::TYPEWRITER_FAMILY) {
596                                 os << "-{}";
597                                 column += 2;
598                         } else {
599                                 os << '-';
600                         }
601                         break;
602
603                 case '\"':
604                         os << "\\char`\\\"{}";
605                         column += 9;
606                         break;
607
608                 case 0xa3:    // £ POUND SIGN
609                         if (bparams.inputenc == "default") {
610                                 os << "\\pounds{}";
611                                 column += 8;
612                         } else {
613                                 os.put(c);
614                         }
615                         break;
616
617                 case '$': case '&':
618                 case '%': case '#': case '{':
619                 case '}': case '_':
620                         os << '\\';
621                         os.put(c);
622                         column += 1;
623                         break;
624
625                 case '~':
626                         os << "\\textasciitilde{}";
627                         column += 16;
628                         break;
629
630                 case '^':
631                         os << "\\textasciicircum{}";
632                         column += 17;
633                         break;
634
635                 case '*': case '[':
636                         // avoid being mistaken for optional arguments
637                         os << '{';
638                         os.put(c);
639                         os << '}';
640                         column += 2;
641                         break;
642
643                 case ' ':
644                         // Blanks are printed before font switching.
645                         // Sure? I am not! (try nice-latex)
646                         // I am sure it's correct. LyX might be smarter
647                         // in the future, but for now, nothing wrong is
648                         // written. (Asger)
649                         break;
650
651                 default:
652
653                         // I assume this is hack treating typewriter as verbatim
654                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
655                                 if (c != '\0') {
656                                         os.put(c);
657                                 }
658                                 break;
659                         }
660
661                         // LyX, LaTeX etc.
662
663                         // FIXME: if we have "LaTeX" with a font
664                         // change in the middle (before the 'T', then
665                         // the "TeX" part is still special cased.
666                         // Really we should only operate this on
667                         // "words" for some definition of word
668
669                         size_t pnr = 0;
670
671                         for (; pnr < phrases_nr; ++pnr) {
672                                 if (isTextAt(special_phrases[pnr].phrase, i)) {
673                                         os << special_phrases[pnr].macro;
674                                         i += special_phrases[pnr].phrase.length() - 1;
675                                         column += special_phrases[pnr].macro.length() - 1;
676                                         break;
677                                 }
678                         }
679
680                         if (pnr == phrases_nr && c != '\0') {
681                                 os.put(c);
682                         }
683                         break;
684                 }
685         }
686 }
687
688
689 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
690                                 LyXLayout const & layout) const
691 {
692         BufferParams const & bparams = features.bufferParams();
693
694         // check the params.
695         if (!params.spacing().isDefault())
696                 features.require("setspace");
697
698         // then the layouts
699         features.useLayout(layout.name());
700
701         // then the fonts
702         Language const * doc_language = bparams.language;
703
704         FontList::const_iterator fcit = fontlist.begin();
705         FontList::const_iterator fend = fontlist.end();
706         for (; fcit != fend; ++fcit) {
707                 if (fcit->font().noun() == LyXFont::ON) {
708                         lyxerr[Debug::LATEX] << "font.noun: "
709                                              << fcit->font().noun()
710                                              << endl;
711                         features.require("noun");
712                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
713                                              << fcit->font().stateText(0)
714                                              << endl;
715                 }
716                 switch (fcit->font().color()) {
717                 case LColor::none:
718                 case LColor::inherit:
719                 case LColor::ignore:
720                         // probably we should put here all interface colors used for
721                         // font displaying! For now I just add this ones I know of (Jug)
722                 case LColor::latex:
723                 case LColor::note:
724                         break;
725                 default:
726                         features.require("color");
727                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
728                                              << fcit->font().stateText(0)
729                                              << endl;
730                 }
731
732                 Language const * language = fcit->font().language();
733                 if (language->babel() != doc_language->babel() &&
734                     language != ignore_language &&
735                     language != latex_language)
736                 {
737                         features.useLanguage(language);
738                         lyxerr[Debug::LATEX] << "Found language "
739                                              << language->babel() << endl;
740                 }
741         }
742
743         if (!params.leftIndent().zero())
744                 features.require("ParagraphLeftIndent");
745
746         // then the insets
747         InsetList::const_iterator icit = owner_->insetlist.begin();
748         InsetList::const_iterator iend = owner_->insetlist.end();
749         for (; icit != iend; ++icit) {
750                 if (icit->inset) {
751                         icit->inset->validate(features);
752                         if (layout.needprotect &&
753                             icit->inset->lyxCode() == InsetBase::FOOT_CODE)
754                                 features.require("NeedLyXFootnoteCode");
755                 }
756         }
757
758         // then the contents
759         for (pos_type i = 0; i < size() ; ++i) {
760                 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
761                         if (!special_phrases[pnr].builtin
762                             && isTextAt(special_phrases[pnr].phrase, i)) {
763                                 features.require(special_phrases[pnr].phrase);
764                                 break;
765                         }
766                 }
767         }
768 }
769
770
771 } // namespace lyx