2 * \file InsetQuotes.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetQuotes.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "Dimension.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
28 #include "MetricsInfo.h"
29 #include "OutputParams.h"
30 #include "output_xhtml.h"
31 #include "Paragraph.h"
32 #include "ParIterator.h"
33 #include "texstream.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
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"
47 using namespace lyx::support;
53 /* codes used to read/write quotes to LyX files
55 * e ``english'' (`inner quotation')
56 * s ''swedish'' ('inner quotation')
57 * g ,,german`` (,inner quotation`)
58 * p ,,polish'' (,inner quotation')
59 * f <<french>> (<inner quotation>)
60 * a >>danish<< (>inner quotation<)
61 * q "plain" ('inner quotation')
64 char const * const style_char = "esgpfaq";
65 char const * const side_char = "lr" ;
66 char const * const level_char = "sd";
71 /////////////////////////////////////////////////////////////////////
75 ///////////////////////////////////////////////////////////////////////
77 InsetQuotesParams quoteparams;
80 int InsetQuotesParams::stylescount() const
82 return strlen(style_char);
86 char_type InsetQuotesParams::getQuoteChar(QuoteStyle const & style, QuoteLevel const & level,
87 QuoteSide const & side) const
89 // main opening quotation mark
90 char_type left_primary;
91 // main closing quotation mark
92 char_type right_primary;
93 // secondary (inner, 'single') opening quotation mark
94 char_type left_secondary;
95 // secondary (inner, 'single') closing quotation mark
96 char_type right_secondary;
100 left_primary = 0x201c; // ``
101 right_primary = 0x201d; // ''
102 left_secondary = 0x2018; // `
103 right_secondary = 0x2019; // '
106 case SwedishQuotes: {
107 left_primary = 0x201d; // ''
108 right_primary = 0x201d; // ''
109 left_secondary = 0x2019; // '
110 right_secondary = 0x2019; // '
114 left_primary = 0x201e; // ,,
115 right_primary = 0x201c; // ``
116 left_secondary = 0x201a; // ,
117 right_secondary = 0x2018; // `
121 left_primary = 0x201e; // ,,
122 right_primary = 0x201d; // ''
123 left_secondary = 0x201a; // ,
124 right_secondary = 0x2019; // '
128 left_primary = 0x00ab; // <<
129 right_primary = 0x00bb; // >>
130 left_secondary = 0x2039; // <
131 right_secondary = 0x203a; // >
135 left_primary = 0x00bb; // >>
136 right_primary = 0x00ab; // <<
137 left_secondary = 0x203a; // >
138 right_secondary = 0x2039; // <
142 left_primary = 0x0022; // "
143 right_primary = 0x0022; // "
144 left_secondary = 0x0027; // '
145 right_secondary = 0x0027; // '
150 left_primary = 0x003f; // ?
151 right_primary = 0x003f; // ?
152 left_secondary = 0x003f; // ?
153 right_secondary = 0x003f; // ?
159 return (side == LeftQuote) ? left_secondary : right_secondary;
161 return (side == LeftQuote) ? left_primary : right_primary;
171 docstring InsetQuotesParams::getLaTeXQuote(char_type c, string const & op) const
180 res = "\\quotesinglbase";
185 res = "\\textquoteleft";
192 res = "\\textquoteright";
201 res = "\\guilsinglleft";
208 res = "\\guilsinglright";
211 case 0x0027: {// ' (plain)
212 res = "\\textquotesingle";
218 else if (op == "babel")
221 res = "\\quotedblbase";
226 res = "\\textquotedblleft";
233 res = "\\textquotedblright";
241 else if (op == "babel")
244 res = "\\guillemotleft";
250 else if (op == "babel")
253 res = "\\guillemotright";
257 res = "\\textquotedbl";
264 return from_ascii(res);
268 docstring InsetQuotesParams::getHTMLQuote(char_type c) const
288 case 0x0027: // ' (plain)
313 return from_ascii(res);
317 map<string, docstring> InsetQuotesParams::getTypes() const
319 map<string, docstring> res;
327 // get all quote types
328 for (sty = 0; sty < stylescount(); ++sty) {
329 style = QuoteStyle(sty);
330 for (sid = 0; sid < 2; ++sid) {
331 side = QuoteSide(sid);
332 for (lev = 0; lev < 2; ++lev) {
333 type += style_char[style];
334 type += side_char[sid];
335 level = QuoteLevel(lev);
336 type += level_char[lev];
337 res[type] = docstring(1, getQuoteChar(style, level, side));
346 docstring const InsetQuotesParams::getGuiLabel(QuoteStyle const & qs)
348 return bformat(_("%1$souter%2$s and %3$sinner%4$s[[quotation marks]]"),
349 docstring(1, quoteparams.getQuoteChar(qs, DoubleQuotes, LeftQuote)),
350 docstring(1, quoteparams.getQuoteChar(qs, DoubleQuotes, RightQuote)),
351 docstring(1, quoteparams.getQuoteChar(qs, SingleQuotes, LeftQuote)),
352 docstring(1, quoteparams.getQuoteChar(qs, SingleQuotes, RightQuote))
357 /////////////////////////////////////////////////////////////////////
361 ///////////////////////////////////////////////////////////////////////
363 InsetQuotes::InsetQuotes(Buffer * buf, string const & str) : Inset(buf)
369 InsetQuotes::InsetQuotes(Buffer * buf, char_type c, InsetQuotesParams::QuoteLevel level,
370 string const & side, string const & style)
371 : Inset(buf), level_(level), pass_thru_(false)
374 style_ = style.empty() ? buf->params().quotes_style : getStyle(style);
375 fontenc_ = (buf->params().fontenc == "global")
376 ? lyxrc.fontenc : buf->params().fontenc;
378 style_ = style.empty() ? InsetQuotesParams::EnglishQuotes : getStyle(style);
379 fontenc_ = lyxrc.fontenc;
383 side_ = InsetQuotesParams::LeftQuote;
384 else if (side == "right")
385 side_ = InsetQuotesParams::RightQuote;
391 docstring InsetQuotes::layoutName() const
393 return from_ascii("Quotes");
397 void InsetQuotes::setSide(char_type c)
399 // Decide whether left or right
404 side_ = InsetQuotesParams::LeftQuote; // left quote
407 side_ = InsetQuotesParams::RightQuote; // right quote
412 void InsetQuotes::parseString(string const & s, bool const allow_wildcards)
415 if (str.length() != 3) {
416 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
417 " bad string length." << endl;
423 // '.' wildcard means: keep current stylee
424 if (!allow_wildcards || str[0] != '.') {
425 for (i = 0; i < quoteparams.stylescount(); ++i) {
426 if (str[0] == style_char[i]) {
427 style_ = InsetQuotesParams::QuoteStyle(i);
431 if (i >= quoteparams.stylescount()) {
432 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
433 " bad style specification.");
434 style_ = InsetQuotesParams::EnglishQuotes;
438 // '.' wildcard means: keep current side
439 if (!allow_wildcards || str[1] != '.') {
440 for (i = 0; i < 2; ++i) {
441 if (str[1] == side_char[i]) {
442 side_ = InsetQuotesParams::QuoteSide(i);
447 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
448 " bad side specification.");
449 side_ = InsetQuotesParams::LeftQuote;
453 // '.' wildcard means: keep current level
454 if (!allow_wildcards || str[2] != '.') {
455 for (i = 0; i < 2; ++i) {
456 if (str[2] == level_char[i]) {
457 level_ = InsetQuotesParams::QuoteLevel(i);
462 LYXERR0("ERROR (InsetQuotes::InsetQuotes):"
463 " bad level specification.");
464 level_ = InsetQuotesParams::DoubleQuotes;
470 InsetQuotesParams::QuoteStyle InsetQuotes::getStyle(string const & s)
472 InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::EnglishQuotes;
475 qs = InsetQuotesParams::EnglishQuotes;
476 else if (s == "swedish")
477 qs = InsetQuotesParams::SwedishQuotes;
478 else if (s == "german")
479 qs = InsetQuotesParams::GermanQuotes;
480 else if (s == "polish")
481 qs = InsetQuotesParams::PolishQuotes;
482 else if (s == "french")
483 qs = InsetQuotesParams::FrenchQuotes;
484 else if (s == "danish")
485 qs = InsetQuotesParams::DanishQuotes;
486 else if (s == "plain")
487 qs = InsetQuotesParams::PlainQuotes;
493 docstring InsetQuotes::displayString() const
495 // In PassThru, we use straight quotes
497 return (level_ == InsetQuotesParams::DoubleQuotes) ?
498 from_ascii("\"") : from_ascii("'");
500 docstring retdisp = docstring(1, quoteparams.getQuoteChar(style_, level_, side_));
502 // in French, thin spaces are added inside double guillemets
503 if (prefixIs(context_lang_, "fr")
504 && level_ == InsetQuotesParams::DoubleQuotes
505 && style_ == InsetQuotesParams::FrenchQuotes) {
506 // THIN SPACE (U+2009)
507 char_type const thin_space = 0x2009;
508 if (side_ == InsetQuotesParams::LeftQuote)
509 retdisp += thin_space;
511 retdisp = thin_space + retdisp;
518 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
520 FontInfo & font = mi.base.font;
521 frontend::FontMetrics const & fm = theFontMetrics(font);
522 dim.asc = fm.maxAscent();
523 dim.des = fm.maxDescent();
524 dim.wid = fm.width(displayString());
528 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
530 FontInfo font = pi.base.font;
531 font.setPaintColor(pi.textColor(font.realColor()));
532 pi.pain.text(x, y, displayString(), font);
536 string InsetQuotes::getType() const
539 text += style_char[style_];
540 text += side_char[side_];
541 text += level_char[level_];
546 void InsetQuotes::write(ostream & os) const
548 os << "Quotes " << getType();
552 void InsetQuotes::read(Lexer & lex)
554 lex.setContext("InsetQuotes::read");
556 parseString(lex.getString());
557 lex >> "\\end_inset";
561 void InsetQuotes::doDispatch(Cursor & cur, FuncRequest & cmd)
563 switch (cmd.action()) {
564 case LFUN_INSET_MODIFY: {
565 string const first_arg = cmd.getArg(0);
566 bool const change_type = first_arg == "changetype";
569 // this will not be handled higher up
573 cur.recordUndoInset(this);
574 parseString(cmd.getArg(1), true);
575 cur.buffer()->updateBuffer();
579 Inset::doDispatch(cur, cmd);
585 bool InsetQuotes::getStatus(Cursor & cur, FuncRequest const & cmd,
586 FuncStatus & flag) const
588 switch (cmd.action()) {
590 case LFUN_INSET_MODIFY: {
591 string const first_arg = cmd.getArg(0);
592 if (first_arg == "changetype") {
593 string const type = cmd.getArg(1);
594 flag.setOnOff(type == getType());
595 flag.setEnabled(!pass_thru_);
598 return Inset::getStatus(cur, cmd, flag);
602 return Inset::getStatus(cur, cmd, flag);
607 void InsetQuotes::latex(otexstream & os, OutputParams const & runparams) const
609 char_type quotechar = quoteparams.getQuoteChar(style_, level_, side_);
612 // In pass-thru context, we output plain quotes
613 if (runparams.pass_thru)
614 qstr = (level_ == InsetQuotesParams::DoubleQuotes) ? from_ascii("\"") : from_ascii("'");
615 else if (style_ == InsetQuotesParams::PlainQuotes && runparams.isFullUnicode()) {
616 // For XeTeX and LuaTeX,we need to disable mapping to get straight
617 // quotes. We define our own commands that do this
618 qstr = (level_ == InsetQuotesParams::DoubleQuotes) ?
619 from_ascii("\\textquotedblplain") : from_ascii("\\textquotesingleplain");
621 else if (runparams.use_polyglossia) {
622 // For polyglossia, we directly output the respective unicode chars
623 // (spacing and kerning is then handled respectively)
624 qstr = docstring(1, quotechar);
626 else if (style_ == InsetQuotesParams::FrenchQuotes
627 && level_ == InsetQuotesParams::DoubleQuotes
628 && prefixIs(runparams.local_font->language()->code(), "fr")) {
629 // Specific guillemets of French babel
630 // including correct French spacing
631 if (side_ == InsetQuotesParams::LeftQuote)
632 qstr = from_ascii("\\og");
634 qstr = from_ascii("\\fg");
635 } else if (fontenc_ == "T1"
636 && !runparams.local_font->language()->internalFontEncoding()) {
637 // Quotation marks for T1 font encoding
639 qstr = quoteparams.getLaTeXQuote(quotechar, "t1");
640 } else if (runparams.local_font->language()->internalFontEncoding()) {
641 // Quotation marks for internal font encodings
642 // (ligatures not featured)
643 qstr = quoteparams.getLaTeXQuote(quotechar, "int");
644 #ifdef DO_USE_DEFAULT_LANGUAGE
645 } else if (doclang == "default") {
647 } else if (!runparams.use_babel || runparams.isFullUnicode()) {
649 // Standard quotation mark macros
650 // These are also used by babel
651 // without fontenc (XeTeX/LuaTeX)
652 qstr = quoteparams.getLaTeXQuote(quotechar, "ot1");
654 // Babel shorthand quotation marks (for T1/OT1)
655 qstr = quoteparams.getLaTeXQuote(quotechar, "babel");
658 if (!runparams.pass_thru) {
659 // Guard against unwanted ligatures with preceding text
660 char_type const lastchar = os.lastChar();
661 // !` ?` => !{}` ?{}`
662 if (prefixIs(qstr, from_ascii("`"))
663 && (lastchar == '!' || lastchar == '?'))
665 // ``` ''' ,,, <<< >>>
666 // => `{}`` '{}'' ,{},, <{}<< >{}>>
667 if (contains(from_ascii(",'`<>"), lastchar)
668 && prefixIs(qstr, lastchar))
674 if (prefixIs(qstr, from_ascii("\\")))
675 // properly terminate the command depending on the context
680 int InsetQuotes::plaintext(odocstringstream & os,
681 OutputParams const &, size_t) const
683 docstring const str = displayString();
689 docstring InsetQuotes::getQuoteEntity() const {
690 docstring res = quoteparams.getHTMLQuote(quoteparams.getQuoteChar(style_, level_, side_));
691 // in French, thin spaces are added inside double guillemets
692 if (prefixIs(context_lang_, "fr")
693 && level_ == InsetQuotesParams::DoubleQuotes
694 && style_ == InsetQuotesParams::FrenchQuotes) {
695 // THIN SPACE (U+2009)
696 docstring const thin_space = from_ascii(" ");
697 if (side_ == InsetQuotesParams::LeftQuote)
700 res = thin_space + res;
706 int InsetQuotes::docbook(odocstream & os, OutputParams const &) const
708 os << getQuoteEntity();
713 docstring InsetQuotes::xhtml(XHTMLStream & xs, OutputParams const &) const
715 xs << XHTMLStream::ESCAPE_NONE << getQuoteEntity();
720 void InsetQuotes::toString(odocstream & os) const
722 os << displayString();
726 void InsetQuotes::forOutliner(docstring & os, size_t const, bool const) const
728 os += displayString();
732 void InsetQuotes::updateBuffer(ParIterator const & it, UpdateType /* utype*/)
734 BufferParams const & bp = buffer().masterBuffer()->params();
735 pass_thru_ = it.paragraph().isPassThru();
736 context_lang_ = it.paragraph().getFontSettings(bp, it.pos()).language()->code();
737 fontenc_ = (bp.fontenc == "global") ? lyxrc.fontenc : bp.fontenc;
741 void InsetQuotes::validate(LaTeXFeatures & features) const
743 char_type type = quoteparams.getQuoteChar(style_, level_, side_);
745 // Handle characters that are not natively supported by
746 // specific font encodings (we roll our own definitions)
747 #ifdef DO_USE_DEFAULT_LANGUAGE
748 if (features.bufferParams().language->lang() == "default"
750 if (!features.useBabel()
752 && !features.runparams().isFullUnicode() && fontenc_ != "T1") {
755 features.require("quotesinglbase");
758 features.require("guilsinglleft");
761 features.require("guilsinglright");
764 features.require("quotedblbase");
767 features.require("guillemotleft");
770 features.require("guillemotright");
776 // Handle straight quotation marks. These need special care
777 // in most output formats
780 if (features.runparams().isFullUnicode())
781 features.require("textquotesinglep");
783 features.require("textcomp");
787 if (features.runparams().isFullUnicode())
788 features.require("textquotedblp");
789 else if (fontenc_ != "T1")
790 features.require("textquotedbl");
799 string InsetQuotes::contextMenuName() const
801 return "context-quote";