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