]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Disentangle CiteEngines and Modules
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetQuotes.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "Encoding.h"
22 #include "Font.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
25 #include "Language.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "LyXRC.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
34 #include "texstream.h"
35
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
38
39 #include "support/debug.h"
40 #include "support/docstring.h"
41 #include "support/docstream.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/textutils.h"
45
46 #include <string.h>
47
48 using namespace std;
49 using namespace lyx::support;
50
51 namespace lyx {
52
53 namespace {
54
55 /* codes used to read/write quotes to LyX files
56  * available styles:
57  * e    ``english''  (`inner quotation')
58  * s    ''swedish''  ('inner quotation')
59  * g    ,,german``   (,inner quotation`)
60  * p    ,,polish''   (,inner quotation')
61  * c    <<swiss>>    (<inner quotation>)
62  * a    >>danish<<   (>inner quotation<)
63  * q    "plain"      ('inner quotation')
64  * b    `british'    (``inner quotation'')
65  * w    >>swedishg>> ('inner quotation') ["g" = Guillemets]
66  * f    <<french>>   (``inner quotation'')
67  * i    <<frenchin>> (<<inner quotation>>) ["in" = Imprimerie Nationale]
68  * r    <<russian>>  (,,inner quotation``)
69  * j    [U+300C]cjk[U+300D]  ([U+300E]inner quotation[U+300F]) [CORNER BRACKETS]
70  * k    [U+300A]cjkangle[U+300B]  ([U+3008]inner quotation[U+3009]) [ANGLE BRACKETS]
71  * x    dynamic style (inherits document settings)
72  */
73
74 char const * const style_char = "esgpcaqbwfirjkx";
75 char const * const side_char = "lr" ;
76 char const * const level_char = "sd";
77
78 } // namespace anon
79
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 // InsetQuotesParams
84 //
85 ///////////////////////////////////////////////////////////////////////
86
87 InsetQuotesParams quoteparams;
88
89
90 int InsetQuotesParams::stylescount() const
91 {
92         return strlen(style_char);
93 }
94
95
96 char_type InsetQuotesParams::getQuoteChar(QuoteStyle const & style, QuoteLevel const & level,
97                                     QuoteSide const & side) const
98 {
99         // main opening quotation mark
100         char_type left_primary;
101         // main closing quotation mark
102         char_type right_primary;
103         // secondary (inner, 'single') opening quotation mark
104         char_type left_secondary;
105         // secondary (inner, 'single') closing quotation mark
106         char_type right_secondary;
107
108         switch (style) {
109         case EnglishQuotes: {
110                 left_primary = 0x201c; // ``
111                 right_primary = 0x201d; // ''
112                 left_secondary = 0x2018; // `
113                 right_secondary = 0x2019; // '
114                 break;
115         }
116         case SwedishQuotes: {
117                 left_primary = 0x201d; // ''
118                 right_primary = 0x201d; // ''
119                 left_secondary = 0x2019; // '
120                 right_secondary = 0x2019; // '
121                 break;
122         }
123         case GermanQuotes: {
124                 left_primary = 0x201e; // ,,
125                 right_primary = 0x201c; // ``
126                 left_secondary = 0x201a; // ,
127                 right_secondary = 0x2018; // `
128                 break;
129         }
130         case PolishQuotes: {
131                 left_primary =  0x201e; // ,,
132                 right_primary = 0x201d; // ''
133                 left_secondary = 0x201a; // ,
134                 right_secondary = 0x2019; // '
135                 break;
136         }
137         case SwissQuotes: {
138                 left_primary = 0x00ab; // <<
139                 right_primary = 0x00bb; // >>
140                 left_secondary = 0x2039; // <
141                 right_secondary = 0x203a; // >
142                 break;
143         }
144         case DanishQuotes: {
145                 left_primary = 0x00bb; // >>
146                 right_primary = 0x00ab; // <<
147                 left_secondary = 0x203a; // >
148                 right_secondary = 0x2039; // <
149                 break;
150         }
151         case PlainQuotes: {
152                 left_primary = 0x0022; // "
153                 right_primary = 0x0022; // "
154                 left_secondary = 0x0027; // '
155                 right_secondary = 0x0027; // '
156                 break;
157         }
158         case BritishQuotes: {
159                 left_primary = 0x2018; // `
160                 right_primary = 0x2019; // '
161                 left_secondary = 0x201c; // ``
162                 right_secondary = 0x201d; // ''
163                 break;
164         }
165         case SwedishGQuotes: {
166                 left_primary = 0x00bb; // >>
167                 right_primary = 0x00bb; // >>
168                 left_secondary = 0x2019; // '
169                 right_secondary = 0x2019; // '
170                 break;
171         }
172         case FrenchQuotes: {
173                 left_primary = 0x00ab; // <<
174                 right_primary = 0x00bb; // >>
175                 left_secondary = 0x201c; // ``
176                 right_secondary = 0x201d; // ''
177                 break;
178         }
179         case FrenchINQuotes:{
180                 left_primary = 0x00ab; // <<
181                 right_primary = 0x00bb; // >>
182                 left_secondary =  0x00ab; // <<
183                 right_secondary = 0x00bb; // >>
184                 break;
185         }
186         case RussianQuotes:{
187                 left_primary = 0x00ab; // <<
188                 right_primary = 0x00bb; // >>
189                 left_secondary =  0x201e; // ,,
190                 right_secondary = 0x201c; // ``
191                 break;
192         }
193         case CJKQuotes:{
194                 left_primary = 0x300c; // LEFT CORNER BRACKET
195                 right_primary = 0x300d; // RIGHT CORNER BRACKET
196                 left_secondary =  0x300e; // LEFT WHITE CORNER BRACKET
197                 right_secondary = 0x300f; // RIGHT WHITE CORNER BRACKET
198                 break;
199         }
200         case CJKAngleQuotes:{
201                 left_primary = 0x300a; // LEFT DOUBLE ANGLE BRACKET
202                 right_primary = 0x300b; // RIGHT DOUBLE ANGLE BRACKET
203                 left_secondary =  0x3008; // LEFT ANGLE BRACKET
204                 right_secondary = 0x3009; // RIGHT ANGLE BRACKET
205                 break;
206         }
207         case DynamicQuotes:
208         default:
209                 // should not happen
210                 left_primary = 0x003f; // ?
211                 right_primary = 0x003f; // ?
212                 left_secondary =  0x003f; // ?
213                 right_secondary = 0x003f; // ?
214                 break;
215         }
216
217         switch (level) {
218         case SecondaryQuotes:
219                 return (side == OpeningQuote) ? left_secondary : right_secondary;
220         case PrimaryQuotes:
221                 return (side == OpeningQuote) ? left_primary : right_primary;
222         default:
223                 break;
224         }
225
226         // should not happen
227         return 0x003f;
228 }
229
230
231 docstring InsetQuotesParams::getLaTeXQuote(char_type c, string const & op) const
232 {
233         string res;
234
235         switch (c){
236         case 0x201a: {// ,
237                 if (op == "babel")
238                         res = "\\glq";
239                 else
240                         res = "\\quotesinglbase";
241                 break;
242         }
243         case 0x2019: {// '
244                 if (op == "int")
245                         res = "\\textquoteleft";
246                 else
247                         res = "'";
248                 break;
249         }
250         case 0x2018: {// `
251                 if (op == "int")
252                         res = "\\textquoteright";
253                 else
254                         res = "`";
255                 break;
256         }
257         case 0x2039: {// <
258                 if (op == "babel")
259                         res = "\\flq";
260                 else
261                         res = "\\guilsinglleft";
262                 break;
263         }
264         case 0x203a: {// >
265                 if (op == "babel")
266                         res = "\\frq";
267                 else
268                         res = "\\guilsinglright";
269                 break;
270         }
271         case 0x0027: {// ' (plain)
272                 res = "\\textquotesingle";
273                 break;
274         }
275         case 0x201e: {// ,,
276                 if (op == "t1")
277                         res = ",,";
278                 else if (op == "babel")
279                         res = "\\glqq";
280                 else
281                         res = "\\quotedblbase";
282                 break;
283         }
284         case 0x201d: {// ''
285                 if (op == "int")
286                         res = "\\textquotedblleft";
287                 else
288                         res = "''";
289                 break;
290         }
291         case 0x201c: {// ``
292                 if (op == "int")
293                         res = "\\textquotedblright";
294                 else
295                         res = "``";
296                 break;
297         }
298         case 0x00ab: {// <<
299                 if (op == "t1")
300                         res = "<<";
301                 else if (op == "babel")
302                         res = "\\flqq";
303                 else
304                         res = "\\guillemotleft";
305                 break;
306         }
307         case 0x00bb: {// >>
308                 if (op == "t1")
309                         res = ">>";
310                 else if (op == "babel")
311                         res = "\\frqq";
312                 else
313                         res = "\\guillemotright";
314                 break;
315         }
316         case 0x0022: {// "
317                 res = "\\textquotedbl";
318                 break;
319         }
320         // The following are fakes
321         // This is just to get something symbolic
322         // in encodings where this chars would not be used ayway
323         case 0x300c: // LEFT CORNER BRACKET
324                 res = "\\ensuremath{\\lceil}";
325                 break;
326         case 0x300d: // RIGHT CORNER BRACKET
327                 res = "\\ensuremath{\\rfloor}";
328                 break;
329         case 0x300e: // LEFT WHITE CORNER BRACKET
330                 res = "\\ensuremath{\\llceil}";
331                 break;
332         case 0x300f: // RIGHT WHITE CORNER BRACKET
333                 res = "\\ensuremath{\\rrfloor}";
334                 break;
335         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
336                 res = "\\ensuremath{\\langle\\kern-2.5pt\\langle}";
337                 break;
338         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
339                 res = "\\ensuremath{\\rangle\\kern-2.5pt\\rangle}";
340                 break;
341         case 0x3008: // LEFT ANGLE BRACKET
342                 res = "\\ensuremath{\\langle}";
343                 break;
344         case 0x3009: // RIGHT ANGLE BRACKET
345                 res = "\\ensuremath{\\rangle}";
346                 break;
347         default:
348                 break;
349         }
350         
351         return from_ascii(res);
352 }
353
354
355 docstring InsetQuotesParams::getHTMLQuote(char_type c) const
356 {
357         string res;
358
359         switch (c){
360         case 0x201a: // ,
361                 res = "&sbquo;";
362                 break;
363         case 0x2019: // '
364                 res = "&rsquo;";
365                 break;
366         case 0x2018: // `
367                 res = "&lsquo;";
368                 break;
369         case 0x2039: // <
370                 res = "&lsaquo;";
371                 break;
372         case 0x203a: // >
373                 res = "&rsaquo;";
374                 break;
375         case 0x0027: // ' (plain)
376                 res = "&#x27;";
377                 break;
378         case 0x201e: // ,,
379                 res = "&bdquo;";
380                 break;
381         case 0x201d: // ''
382                 res = "&rdquo;";
383                 break;
384         case 0x201c: // ``
385                 res = "&ldquo;";
386                 break;
387         case 0x00ab: // <<
388                 res = "&laquo;";
389                 break;
390         case 0x00bb: // >>
391                 res = "&raquo;";
392                 break;
393         case 0x0022: // "
394                 res = "&quot;";
395                 break;
396         case 0x300c: // LEFT CORNER BRACKET
397                 res = "&#x300c;";
398                 break;
399         case 0x300d: // RIGHT CORNER BRACKET
400                 res = "&#x300d;";
401                 break;
402         case 0x300e: // LEFT WHITE CORNER BRACKET
403                 res = "&#x300e;";
404                 break;
405         case 0x300f: // RIGHT WHITE CORNER BRACKET
406                 res = "&#x300f;";
407                 break;
408         case 0x300a: // LEFT DOUBLE ANGLE BRACKET
409                 res = "&#x300a;";
410                 break;
411         case 0x300b: // RIGHT DOUBLE ANGLE BRACKET
412                 res = "&#x300b;";
413                 break;
414         case 0x3008: // LEFT ANGLE BRACKET
415                 res = "&#x3008;";
416                 break;
417         case 0x3009: // RIGHT ANGLE BRACKET
418                 res = "&#x3009;";
419                 break;
420         default:
421                 break;
422         }
423         
424         return from_ascii(res);
425 }
426
427
428 map<string, docstring> InsetQuotesParams::getTypes() const
429 {
430         map<string, docstring> res;
431
432         int sty, sid, lev;
433         QuoteStyle style;
434         QuoteSide side;
435         QuoteLevel level;
436         string type;
437
438         // get all quote types
439         for (sty = 0; sty < stylescount(); ++sty) {
440                 style = QuoteStyle(sty);
441                 if (style == DynamicQuotes)
442                         continue;
443                 for (sid = 0; sid < 2; ++sid) {
444                         side = QuoteSide(sid);
445                         for (lev = 0; lev < 2; ++lev) {
446                                 type += style_char[style];
447                                 type += side_char[sid];
448                                 level = QuoteLevel(lev);
449                                 type += level_char[lev];
450                                 res[type] = docstring(1, getQuoteChar(style, level, side));
451                                 type.clear();
452                         }
453                 }
454         }
455         return res;
456 }
457
458
459 docstring const InsetQuotesParams::getGuiLabel(QuoteStyle const & qs)
460 {
461         return bformat(_("%1$souter%2$s and %3$sinner%4$s[[quotation marks]]"),
462                 docstring(1, quoteparams.getQuoteChar(qs, PrimaryQuotes, OpeningQuote)),
463                 docstring(1, quoteparams.getQuoteChar(qs, PrimaryQuotes, ClosingQuote)),
464                 docstring(1, quoteparams.getQuoteChar(qs, SecondaryQuotes, OpeningQuote)),
465                 docstring(1, quoteparams.getQuoteChar(qs, SecondaryQuotes, ClosingQuote))
466                 );
467 }
468
469
470 /////////////////////////////////////////////////////////////////////
471 //
472 // InsetQuotes
473 //
474 ///////////////////////////////////////////////////////////////////////
475
476 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
477 {
478         if (buf) {
479                 global_style_ = buf->masterBuffer()->params().quotes_style;
480                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
481         }
482         else {
483                 global_style_ = InsetQuotesParams::EnglishQuotes;
484                 fontspec_ = false;
485         }
486
487         parseString(str);
488 }
489
490
491 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
492                          string const & side, string const & style)
493         : Inset(buf), level_(level), pass_thru_(false), fontspec_(false)
494 {
495         bool dynamic = false;
496         if (buf) {
497                 global_style_ = buf->masterBuffer()->params().quotes_style;
498                 fontenc_ = (buf->masterBuffer()->params().fontenc == "global")
499                         ? lyxrc.fontenc : buf->params().fontenc;
500                 dynamic = buf->masterBuffer()->params().dynamic_quotes;
501                 fontspec_ = buf->masterBuffer()->params().useNonTeXFonts;
502         } else {
503                 global_style_ = InsetQuotesParams::EnglishQuotes;
504                 fontenc_ = lyxrc.fontenc;
505                 fontspec_ = false;
506         }
507         if (style.empty())
508                 style_ = dynamic ? InsetQuotesParams::DynamicQuotes : global_style_;
509         else
510                 style_ = getStyle(style);
511
512         if (side == "left" || side == "opening")
513                 side_ = InsetQuotesParams::OpeningQuote;
514         else if (side == "right" || side == "closing")
515                 side_ = InsetQuotesParams::ClosingQuote;
516         else
517                 setSide(c);
518 }
519
520
521 docstring InsetQuotes::layoutName() const
522 {
523         return from_ascii("Quotes");
524 }
525
526
527 void InsetQuotes::setSide(char_type c)
528 {
529         // Decide whether opening or closing quote
530         if (lyx::isSpace(c) || isOpenPunctuation(c))
531                 side_ = InsetQuotesParams::OpeningQuote;// opening quote
532         else
533                 side_ = InsetQuotesParams::ClosingQuote;// closing quote
534 }
535
536
537 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
538 {
539         string str = s;
540         if (str.length() != 3) {
541                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
542                         " bad string length." << endl;
543                 str = "eld";
544         }
545
546         int i;
547
548         // '.' wildcard means: keep current stylee
549         if (!allow_wildcards || str[0] != '.') {
550                 for (i = 0; i < quoteparams.stylescount(); ++i) {
551                         if (str[0] == style_char[i]) {
552                                 style_ = InsetQuotesParams::QuoteStyle(i);
553                                 break;
554                         }
555                 }
556                 if (i >= quoteparams.stylescount()) {
557                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
558                                 " bad style specification.");
559                         style_ = InsetQuotesParams::EnglishQuotes;
560                 }
561         }
562
563         // '.' wildcard means: keep current side
564         if (!allow_wildcards || str[1] != '.') {
565                 for (i = 0; i < 2; ++i) {
566                         if (str[1] == side_char[i]) {
567                                 side_ = InsetQuotesParams::QuoteSide(i);
568                                 break;
569                         }
570                 }
571                 if (i >= 2) {
572                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
573                                 " bad side specification.");
574                         side_ = InsetQuotesParams::OpeningQuote;
575                 }
576         }
577
578         // '.' wildcard means: keep current level
579         if (!allow_wildcards || str[2] != '.') {
580                 for (i = 0; i < 2; ++i) {
581                         if (str[2] == level_char[i]) {
582                                 level_ = InsetQuotesParams::QuoteLevel(i);
583                                 break;
584                         }
585                 }
586                 if (i >= 2) {
587                         LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
588                                 " bad level specification.");
589                         level_ = InsetQuotesParams::PrimaryQuotes;
590                 }
591         }
592 }
593
594
595 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
596 {
597         InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
598         
599         if (s == "english")
600                 qs = InsetQuotesParams::EnglishQuotes;
601         else if (s == "swedish")
602                 qs = InsetQuotesParams::SwedishQuotes;
603         else if (s == "german")
604                 qs = InsetQuotesParams::GermanQuotes;
605         else if (s == "polish")
606                 qs = InsetQuotesParams::PolishQuotes;
607         else if (s == "swiss")
608                 qs = InsetQuotesParams::SwissQuotes;
609         else if (s == "danish")
610                 qs = InsetQuotesParams::DanishQuotes;
611         else if (s == "plain")
612                 qs = InsetQuotesParams::PlainQuotes;
613         else if (s == "british")
614                 qs = InsetQuotesParams::BritishQuotes;
615         else if (s == "swedishg")
616                 qs = InsetQuotesParams::SwedishGQuotes;
617         else if (s == "french")
618                 qs = InsetQuotesParams::FrenchQuotes;
619         else if (s == "frenchin")
620                 qs = InsetQuotesParams::FrenchINQuotes;
621         else if (s == "russian")
622                 qs = InsetQuotesParams::RussianQuotes;
623         else if (s == "cjk")
624                 qs = InsetQuotesParams::CJKQuotes;
625         else if (s == "cjkangle")
626                 qs = InsetQuotesParams::CJKAngleQuotes;
627         else if (s == "dynamic")
628                 qs = InsetQuotesParams::DynamicQuotes;
629
630         return qs;
631 }
632
633
634 docstring InsetQuotes::displayString() const
635 {
636         // In PassThru, we use straight quotes
637         if (pass_thru_)
638                 return (level_ == InsetQuotesParams::PrimaryQuotes) ?
639                                         from_ascii("\"") : from_ascii("'");
640
641         InsetQuotesParams::QuoteStyle style =
642                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
643
644         docstring retdisp = docstring(1, quoteparams.getQuoteChar(style, level_, side_));
645
646         // in French, thin spaces are added inside double guillemets
647         if (prefixIs(context_lang_, "fr")
648             && level_ == InsetQuotesParams::PrimaryQuotes
649             && (style == InsetQuotesParams::SwissQuotes
650                 || style == InsetQuotesParams::FrenchQuotes
651                 || style == InsetQuotesParams::FrenchINQuotes)) {
652                 // THIN SPACE (U+2009)
653                 char_type const thin_space = 0x2009;
654                 if (side_ == InsetQuotesParams::OpeningQuote)
655                         retdisp += thin_space;
656                 else
657                         retdisp = thin_space + retdisp;
658         }
659
660         return retdisp;
661 }
662
663
664 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
665 {
666         FontInfo & font = mi.base.font;
667         frontend::FontMetrics const & fm = theFontMetrics(font);
668         dim.asc = fm.maxAscent();
669         dim.des = fm.maxDescent();
670         dim.wid = fm.width(displayString());
671 }
672
673
674 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
675 {
676         FontInfo font = pi.base.font;
677         if (style_ == InsetQuotesParams::DynamicQuotes)
678                 font.setPaintColor(Color_special);
679         else
680                 font.setPaintColor(pi.textColor(font.realColor()));
681         pi.pain.text(x, y, displayString(), font);
682 }
683
684
685 string InsetQuotes::getType() const
686 {
687         string text;
688         text += style_char[style_];
689         text += side_char[side_];
690         text += level_char[level_];
691         return text;
692 }
693
694
695 void InsetQuotes::write(ostream & os) const
696 {
697         os << "Quotes " << getType();
698 }
699
700
701 void InsetQuotes::read(Lexer & lex)
702 {
703         lex.setContext("InsetQuotes::read");
704         lex.next();
705         parseString(lex.getString());
706         lex >> "\\end_inset";
707 }
708
709
710 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
711 {
712         switch (cmd.action()) {
713         case LFUN_INSET_MODIFY: {
714                 string const first_arg = cmd.getArg(0);
715                 bool const change_type = first_arg == "changetype";
716                 if (!change_type) {
717                         // not for us
718                         // this will not be handled higher up
719                         cur.undispatched();
720                         return;
721                 }
722                 cur.recordUndoInset(this);
723                 parseString(cmd.getArg(1), true);
724                 cur.buffer()->updateBuffer();
725                 break;
726         }
727         default:
728                 Inset::doDispatch(cur, cmd);
729                 break;
730         }
731 }
732
733
734 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
735                 FuncStatus & flag) const
736 {
737         switch (cmd.action()) {
738
739         case LFUN_INSET_MODIFY: {
740                 string const first_arg = cmd.getArg(0);
741                 if (first_arg == "changetype") {
742                         string const type = cmd.getArg(1);
743                         flag.setOnOff(type == getType());
744                         flag.setEnabled(!pass_thru_);
745                         return true;
746                 }
747                 return Inset::getStatus(cur, cmd, flag);
748         }
749
750         default:
751                 return Inset::getStatus(cur, cmd, flag);
752         }
753 }
754
755
756 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
757 {
758         InsetQuotesParams::QuoteStyle style =
759                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
760         char_type quotechar = quoteparams.getQuoteChar(style, level_, side_);
761         docstring qstr;
762
763         // In pass-thru context, we output plain quotes
764         if (runparams.pass_thru)
765                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ? from_ascii("\"") : from_ascii("'");
766         else if (style == InsetQuotesParams::PlainQuotes && fontspec_) {
767                 // For XeTeX and LuaTeX,we need to disable mapping to get straight
768                 // quotes. We define our own commands that do this
769                 qstr = (level_ == InsetQuotesParams::PrimaryQuotes) ?
770                         from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
771         }
772         else if (runparams.use_polyglossia) {
773                 // For polyglossia, we directly output the respective unicode chars 
774                 // (spacing and kerning is then handled respectively)
775                 qstr = docstring(1, quotechar);
776         }
777         else if (style == InsetQuotesParams::CJKQuotes || style  == InsetQuotesParams::CJKAngleQuotes) {
778                 if (runparams.encoding && runparams.encoding->encodable(quotechar))
779                         qstr = docstring(1, quotechar);
780                 else
781                         qstr = quoteparams.getLaTeXQuote(quotechar, "int");
782         }
783         else if ((style == InsetQuotesParams::SwissQuotes
784                  || style == InsetQuotesParams::FrenchQuotes
785                  || style == InsetQuotesParams::FrenchINQuotes)
786                  && level_ == InsetQuotesParams::PrimaryQuotes
787                  && prefixIs(runparams.local_font->language()->code(), "fr")) {
788                 // Specific guillemets of French babel
789                 // including correct French spacing
790                 if (side_ == InsetQuotesParams::OpeningQuote)
791                         qstr = from_ascii("\\og");
792                 else
793                         qstr = from_ascii("\\fg");
794         } else if (fontenc_ == "T1"
795                    && !runparams.local_font->language()->internalFontEncoding()) {
796                 // Quotation marks for T1 font encoding
797                 // (using ligatures)
798                 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
799         } else if (runparams.local_font->language()->internalFontEncoding()) {
800                 // Quotation marks for internal font encodings
801                 // (ligatures not featured)
802                 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
803 #ifdef DO_USE_DEFAULT_LANGUAGE
804         } else if (doclang == "default") {
805 #else
806         } else if (!runparams.use_babel || runparams.isFullUnicode()) {
807 #endif
808                 // Standard quotation mark macros
809                 // These are also used by babel
810                 // without fontenc (XeTeX/LuaTeX)
811                 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
812         } else {
813                 // Babel shorthand quotation marks (for T1/OT1)
814                 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
815         }
816
817         if (!runparams.pass_thru) {
818                 // Guard against unwanted ligatures with preceding text
819                 char_type const lastchar = os.lastChar();
820                 // !` ?` => !{}` ?{}`
821                 if (prefixIs(qstr, from_ascii("`"))
822                     && (lastchar == '!' || lastchar == '?'))
823                         os << "{}";
824                 // ``` ''' ,,, <<< >>>
825                 // => `{}`` '{}'' ,{},, <{}<< >{}>>
826                 if (contains(from_ascii(",'`<>"), lastchar)
827                     && prefixIs(qstr, lastchar))
828                         os << "{}";
829         }
830
831         os << qstr;
832
833         if (prefixIs(qstr, from_ascii("\\")) && !suffixIs(qstr, '}'))
834                 // properly terminate the command depending on the context
835                 os << termcmd;
836 }
837
838
839 int InsetQuotes::plaintext(odocstringstream & os, 
840         OutputParams const &, size_t) const
841 {
842         docstring const str = displayString();
843         os << str;
844         return str.size();
845 }
846
847
848 docstring InsetQuotes::getQuoteEntity() const {
849         InsetQuotesParams::QuoteStyle style =
850                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
851         docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style, level_, side_));
852         // in French, thin spaces are added inside double guillemets
853         if (prefixIs(context_lang_, "fr")
854             && level_ == InsetQuotesParams::PrimaryQuotes
855             && (style == InsetQuotesParams::FrenchQuotes
856                 || style == InsetQuotesParams::FrenchINQuotes
857                 || style == InsetQuotesParams::SwissQuotes)) {
858                 // THIN SPACE (U+2009)
859                 docstring const thin_space = from_ascii("&#x2009;");
860                 if (side_ == InsetQuotesParams::OpeningQuote)
861                         res += thin_space;
862                 else
863                         res = thin_space + res;
864         }
865         return res;
866 }
867
868
869 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
870 {
871         os << getQuoteEntity();
872         return 0;
873 }
874
875
876 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
877 {
878         xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
879         return docstring();
880 }
881
882
883 void InsetQuotes::toString(odocstream & os) const
884 {
885         os << displayString();
886 }
887
888
889 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
890 {
891         os += displayString();
892 }
893
894
895 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
896 {
897         BufferParams const & bp = buffer().masterBuffer()->params();
898         pass_thru_ = it.paragraph().isPassThru();
899         context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
900         fontenc_ = (bp.fontenc == "global") ? lyxrc.fontenc : bp.fontenc;
901         global_style_ = bp.quotes_style;
902         fontspec_ = bp.useNonTeXFonts;
903 }
904
905
906 void InsetQuotes::validate(LaTeXFeatures & features) const
907 {
908         InsetQuotesParams::QuoteStyle style =
909                         (style_ == InsetQuotesParams::DynamicQuotes) ? global_style_ : style_;
910         char_type type = quoteparams.getQuoteChar(style, level_, side_);
911
912         // Handle characters that are not natively supported by
913         // specific font encodings (we roll our own definitions)
914 #ifdef DO_USE_DEFAULT_LANGUAGE
915         if (features.bufferParams().language->lang() == "default"
916 #else
917         if (!features.useBabel()
918 #endif
919             && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
920                 switch (type) {
921                 case 0x201a:
922                         features.require("quotesinglbase");
923                         break;
924                 case 0x2039:
925                         features.require("guilsinglleft");
926                         break;
927                 case 0x203a:
928                         features.require("guilsinglright");
929                         break;
930                 case 0x201e:
931                         features.require("quotedblbase");
932                         break;
933                 case 0x00ab:
934                         features.require("guillemotleft");
935                         break;
936                 case 0x00bb:
937                         features.require("guillemotright");
938                         break;
939                 default:
940                         break;
941                 }
942         }
943         // Handle straight quotation marks. These need special care
944         // in most output formats
945         switch (type) {
946         case 0x0027: {
947                 if (features.runparams().isFullUnicode() && fontspec_)
948                                 features.require("textquotesinglep");
949                         else
950                                 features.require("textcomp");
951                         break;
952         }
953         case 0x0022: {
954                 if (features.runparams().isFullUnicode() && fontspec_)
955                         features.require("textquotedblp");
956                 else if (fontenc_ != "T1")
957                         features.require("textquotedbl");
958                 break;
959         }
960         // we fake these from math
961         case 0x300e: // LEFT WHITE CORNER BRACKET
962         case 0x300f: // RIGHT WHITE CORNER BRACKET
963                 if (!features.runparams().encoding
964                     || !features.runparams().encoding->encodable(type))
965                         features.require("stmaryrd");
966                 break;
967         default:
968                 break;
969         }
970 }
971
972
973 string InsetQuotes::contextMenuName() const
974 {
975         return "context-quote";
976 }
977
978 } // namespace lyx