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