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