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