]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Fix bug 3363: mark buffer dirty after middle button paste in plain text and table...
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetQuotes.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "debug.h"
18 #include "Language.h"
19 #include "LaTeXFeatures.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22 #include "MetricsInfo.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "paragraph_funcs.h"
26
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
29
30 #include "support/lstrings.h"
31
32
33 namespace lyx {
34
35 using support::prefixIs;
36
37 using std::endl;
38 using std::string;
39 using std::auto_ptr;
40 using std::ostream;
41
42
43 namespace {
44
45 /* codes used to read/write quotes to LyX files
46  * e    ``english''
47  * s    ''spanish''
48  * g    ,,german``
49  * p    ,,polish''
50  * f    <<french>>
51  * a    >>danish<<
52  */
53
54 char const * const language_char = "esgpfa";
55 char const * const side_char = "lr" ;
56 char const * const times_char = "sd";
57
58 // List of known quote chars
59 char const * const quote_char = ",'`<>";
60
61 // Index of chars used for the quote. Index is [side, language]
62 int quote_index[2][6] = {
63         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
64         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
65
66 // Corresponding LaTeX code, for double and single quotes.
67 char const * const latex_quote_t1[2][5] =
68 { { "\\quotesinglbase ",  "'", "`",
69     "\\guilsinglleft{}", "\\guilsinglright{}" },
70   { ",,", "''", "``", "<<", ">>" } };
71
72 char const * const latex_quote_ot1[2][5] =
73 { { "\\quotesinglbase ",  "'", "`",
74     "\\guilsinglleft{}", "\\guilsinglright{}" },
75   { "\\quotedblbase ", "''", "``",
76     "\\guillemotleft{}", "\\guillemotright{}" } };
77
78 char const * const latex_quote_babel[2][5] =
79 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
80   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
81
82 } // namespace anon
83
84
85 InsetQuotes::InsetQuotes(string const & str)
86 {
87         parseString(str);
88 }
89
90
91 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
92         : language_(l), side_(s), times_(t)
93 {
94 }
95
96
97 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
98         : language_(params.quotes_language), times_(params.quotes_times)
99 {
100         getPosition(c);
101 }
102
103
104 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
105         : language_(l), times_(t)
106 {
107         getPosition(c);
108 }
109
110
111 void InsetQuotes::getPosition(char_type c)
112 {
113         // Decide whether left or right
114         switch (c) {
115         case ' ': case '(': case '[':
116                 side_ = LeftQ;   // left quote
117                 break;
118         default:
119                 side_ = RightQ;  // right quote
120         }
121 }
122
123
124 void InsetQuotes::parseString(string const & s)
125 {
126         string str(s);
127         if (str.length() != 3) {
128                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129                         " bad string length." << endl;
130                 str = "eld";
131         }
132
133         int i;
134
135         for (i = 0; i < 6; ++i) {
136                 if (str[0] == language_char[i]) {
137                         language_ = quote_language(i);
138                         break;
139                 }
140         }
141         if (i >= 6) {
142                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
143                         " bad language specification." << endl;
144                 language_ = EnglishQ;
145         }
146
147         for (i = 0; i < 2; ++i) {
148                 if (str[1] == side_char[i]) {
149                         side_ = quote_side(i);
150                         break;
151                 }
152         }
153         if (i >= 2) {
154                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
155                         " bad side specification." << endl;
156                 side_ = LeftQ;
157         }
158
159         for (i = 0; i < 2; ++i) {
160                 if (str[2] == times_char[i]) {
161                         times_ = quote_times(i);
162                         break;
163                 }
164         }
165         if (i >= 2) {
166                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
167                         " bad times specification." << endl;
168                 times_ = DoubleQ;
169         }
170 }
171
172
173 docstring const InsetQuotes::dispString(Language const * loclang) const
174 {
175         string disp;
176         disp += quote_char[quote_index[side_][language_]];
177         if (times_ == DoubleQ)
178                 disp += disp;
179
180
181         docstring retdisp;
182         if (disp == "<<")
183                 retdisp = docstring(1, 0x00ab); //'«';
184         else if (disp == ">>")
185                 retdisp = docstring(1, 0x00bb); //'»';
186 #if 0
187         // The below are supposed to work, but something fails.
188         else if (disp == ",,")
189                 retdisp = docstring(1, 0x201e);
190         else if (disp == "''")
191                 retdisp == docstring(1, 0x201d);
192         else if (disp == "``")
193                 retdisp == docstring(1, 0x201c);
194         else if (disp == "<")
195                 retdisp = docstring(1, 0x2039);
196         else if (disp == ">")
197                 retdisp = docstring(1, 0x203a);
198         else if (disp == ",")
199                 retdisp = docstring(1, 0x201a);
200         else if (disp == "'")
201                 retdisp = docstring(1, 0x2019);
202         else if (disp == "`")
203                 retdisp = docstring(1, 0x2018);
204 #endif
205         else
206                 retdisp = lyx::from_ascii(disp);
207
208         // in french, spaces are added inside double quotes
209         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
210                 if (side_ == LeftQ)
211                         retdisp += ' ';
212                 else
213                         retdisp.insert(docstring::size_type(0), 1, ' ');
214         }
215
216         return retdisp;
217 }
218
219
220 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
221 {
222         Font & font = mi.base.font;
223         frontend::FontMetrics const & fm =
224                 theFontMetrics(font);
225         dim.asc = fm.maxAscent();
226         dim.des = fm.maxDescent();
227         dim.wid = 0;
228
229         docstring const text = dispString(font.language());
230         for (string::size_type i = 0; i < text.length(); ++i) {
231                 if (text[i] == ' ')
232                         dim.wid += fm.width('i');
233                 else if (i == 0 || text[i] != text[i - 1])
234                         dim.wid += fm.width(text[i]);
235                 else
236                         dim.wid += fm.width(',');
237         }
238         bool const changed = dim_ != dim;
239         dim_ = dim;
240         return changed;
241 }
242
243
244 #if 0
245 Font const InsetQuotes::convertFont(Font const & f) const
246 {
247 #if 1
248         return f;
249 #else
250         Font font(f);
251         return font;
252 #endif
253 }
254 #endif
255
256
257 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
258 {
259         docstring const text = dispString(pi.base.font.language());
260
261         if (text.length() == 2 && text[0] == text[1]) {
262                 pi.pain.text(x, y, text[0], pi.base.font);
263                 int const t = theFontMetrics(pi.base.font)
264                         .width(',');
265                 pi.pain.text(x + t, y, text[0], pi.base.font);
266         } else {
267                 pi.pain.text(x, y, text, pi.base.font);
268         }
269         setPosCache(pi, x, y);
270 }
271
272
273 void InsetQuotes::write(Buffer const &, ostream & os) const
274 {
275         string text;
276         text += language_char[language_];
277         text += side_char[side_];
278         text += times_char[times_];
279         os << "Quotes " << text;
280 }
281
282
283 void InsetQuotes::read(Buffer const &, Lexer & lex)
284 {
285         lex.next();
286         parseString(lex.getString());
287         lex.next();
288         if (lex.getString() != "\\end_inset") {
289                 lex.printError("Missing \\end_inset at this point");
290         }
291 }
292
293
294 int InsetQuotes::latex(Buffer const &, odocstream & os,
295                        OutputParams const & runparams) const
296 {
297         const int quoteind = quote_index[side_][language_];
298         string qstr;
299
300         if (language_ == FrenchQ && times_ == DoubleQ
301             && prefixIs(runparams.local_font->language()->code(), "fr")) {
302                 if (side_ == LeftQ)
303                         qstr = "\\og "; //the spaces are important here
304                 else
305                         qstr = " \\fg{}"; //and here
306         } else if (lyxrc.fontenc == "T1") {
307                 qstr = latex_quote_t1[times_][quoteind];
308 #ifdef DO_USE_DEFAULT_LANGUAGE
309         } else if (doclang == "default") {
310 #else
311         } else if (!runparams.use_babel) {
312 #endif
313                 qstr = latex_quote_ot1[times_][quoteind];
314         } else {
315                 qstr = latex_quote_babel[times_][quoteind];
316         }
317
318         // Always guard against unfortunate ligatures (!` ?`)
319         if (prefixIs(qstr, "`"))
320                 qstr.insert(0, "{}");
321
322         os << from_ascii(qstr);
323         return 0;
324 }
325
326
327 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
328                            OutputParams const &) const
329 {
330         docstring const str = dispString(buf.params().language);
331         os << str;
332         return str.size();
333 }
334
335
336 int InsetQuotes::docbook(Buffer const &, odocstream & os,
337                          OutputParams const &) const
338 {
339         if (times_ == DoubleQ) {
340                 if (side_ == LeftQ)
341                         os << "&ldquo;";
342                 else
343                         os << "&rdquo;";
344         } else {
345                 if (side_ == LeftQ)
346                         os << "&lsquo;";
347                 else
348                         os << "&rsquo;";
349         }
350         return 0;
351 }
352
353
354 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
355 {
356         os << dispString(buf.params().language);
357 }
358
359
360 void InsetQuotes::validate(LaTeXFeatures & features) const
361 {
362         bool const use_babel = features.useBabel();
363         char type = quote_char[quote_index[side_][language_]];
364
365 #ifdef DO_USE_DEFAULT_LANGUAGE
366         if (features.bufferParams().language->lang() == "default"
367 #else
368         if (!use_babel
369 #endif
370             && lyxrc.fontenc != "T1") {
371                 if (times_ == SingleQ)
372                         switch (type) {
373                                 case ',': features.require("quotesinglbase");  break;
374                         case '<': features.require("guilsinglleft");  break;
375                         case '>': features.require("guilsinglright"); break;
376                         default: break;
377                         }
378                 else
379                         switch (type) {
380                         case ',': features.require("quotedblbase");   break;
381                         case '<': features.require("guillemotleft");  break;
382                         case '>': features.require("guillemotright"); break;
383                         default: break;
384                         }
385         }
386 }
387
388
389 auto_ptr<Inset> InsetQuotes::doClone() const
390 {
391         return auto_ptr<Inset>(new InsetQuotes(language_, side_, times_));
392 }
393
394
395 Inset::Code InsetQuotes::lyxCode() const
396 {
397         return Inset::QUOTE_CODE;
398 }
399
400
401 } // namespace lyx