]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / insetquotes.C
1 /**
2  * \file insetquotes.C
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 "lyxlex.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         setInsetName(from_utf8("InsetQuotes"));
89 }
90
91
92 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
93         : language_(l), side_(s), times_(t)
94 {
95         setInsetName(from_utf8("InsetQuotes"));
96 }
97
98
99 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
100         : language_(params.quotes_language), times_(params.quotes_times)
101 {
102         getPosition(c);
103         setInsetName(from_utf8("InsetQuotes"));
104 }
105
106
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108         : language_(l), times_(t)
109 {
110         getPosition(c);
111         setInsetName(from_utf8("InsetQuotes"));
112 }
113
114
115 void InsetQuotes::getPosition(char_type c)
116 {
117         // Decide whether left or right
118         switch (c) {
119         case ' ': case '(': case '[':
120                 side_ = LeftQ;   // left quote
121                 break;
122         default:
123                 side_ = RightQ;  // right quote
124         }
125 }
126
127
128 void InsetQuotes::parseString(string const & s)
129 {
130         string str(s);
131         if (str.length() != 3) {
132                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
133                         " bad string length." << endl;
134                 str = "eld";
135         }
136
137         int i;
138
139         for (i = 0; i < 6; ++i) {
140                 if (str[0] == language_char[i]) {
141                         language_ = quote_language(i);
142                         break;
143                 }
144         }
145         if (i >= 6) {
146                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
147                         " bad language specification." << endl;
148                 language_ = EnglishQ;
149         }
150
151         for (i = 0; i < 2; ++i) {
152                 if (str[1] == side_char[i]) {
153                         side_ = quote_side(i);
154                         break;
155                 }
156         }
157         if (i >= 2) {
158                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
159                         " bad side specification." << endl;
160                 side_ = LeftQ;
161         }
162
163         for (i = 0; i < 2; ++i) {
164                 if (str[2] == times_char[i]) {
165                         times_ = quote_times(i);
166                         break;
167                 }
168         }
169         if (i >= 2) {
170                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
171                         " bad times specification." << endl;
172                 times_ = DoubleQ;
173         }
174 }
175
176
177 docstring const InsetQuotes::dispString(Language const * loclang) const
178 {
179         string disp;
180         disp += quote_char[quote_index[side_][language_]];
181         if (times_ == DoubleQ)
182                 disp += disp;
183
184
185         docstring retdisp;
186         if (disp == "<<")
187                 retdisp = docstring(1, 0x00ab); //'«';
188         else if (disp == ">>")
189                 retdisp = docstring(1, 0x00bb); //'»';
190 #if 0
191         // The below are supposed to work, but something fails.
192         else if (disp == ",,")
193                 retdisp = docstring(1, 0x201e);
194         else if (disp == "''")
195                 retdisp == docstring(1, 0x201d);
196         else if (disp == "``")
197                 retdisp == docstring(1, 0x201c);
198         else if (disp == "<")
199                 retdisp = docstring(1, 0x2039);
200         else if (disp == ">")
201                 retdisp = docstring(1, 0x203a);
202         else if (disp == ",")
203                 retdisp = docstring(1, 0x201a);
204         else if (disp == "'")
205                 retdisp = docstring(1, 0x2019);
206         else if (disp == "`")
207                 retdisp = docstring(1, 0x2018);
208 #endif
209         else
210                 retdisp = lyx::from_ascii(disp);
211         
212         // in french, spaces are added inside double quotes
213         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
214                 if (side_ == LeftQ)
215                         retdisp += ' ';
216                 else
217                         retdisp.insert(docstring::size_type(0), 1, ' ');
218         }
219
220         return retdisp;
221 }
222
223
224 bool InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
225 {
226         LyXFont & font = mi.base.font;
227         frontend::FontMetrics const & fm =
228                 theFontMetrics(font);
229         dim.asc = fm.maxAscent();
230         dim.des = fm.maxDescent();
231         dim.wid = 0;
232
233         docstring const text = dispString(font.language());
234         for (string::size_type i = 0; i < text.length(); ++i) {
235                 if (text[i] == ' ')
236                         dim.wid += fm.width('i');
237                 else if (i == 0 || text[i] != text[i - 1])
238                         dim.wid += fm.width(text[i]);
239                 else
240                         dim.wid += fm.width(',');
241         }
242         bool const changed = dim_ != dim;
243         dim_ = dim;
244         return changed;
245 }
246
247
248 #if 0
249 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
250 {
251 #if 1
252         return f;
253 #else
254         LyXFont font(f);
255         return font;
256 #endif
257 }
258 #endif
259
260
261 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
262 {
263         docstring const text = dispString(pi.base.font.language());
264
265         if (text.length() == 2 && text[0] == text[1]) {
266                 pi.pain.text(x, y, text[0], pi.base.font);
267                 int const t = theFontMetrics(pi.base.font)
268                         .width(',');
269                 pi.pain.text(x + t, y, text[0], pi.base.font);
270         } else {
271                 pi.pain.text(x, y, text, pi.base.font);
272         }
273         setPosCache(pi, x, y);
274 }
275
276
277 void InsetQuotes::write(Buffer const &, ostream & os) const
278 {
279         string text;
280         text += language_char[language_];
281         text += side_char[side_];
282         text += times_char[times_];
283         os << "Quotes " << text;
284 }
285
286
287 void InsetQuotes::read(Buffer const &, LyXLex & lex)
288 {
289         lex.next();
290         parseString(lex.getString());
291         lex.next();
292         if (lex.getString() != "\\end_inset") {
293                 lex.printError("Missing \\end_inset at this point");
294         }
295 }
296
297
298 int InsetQuotes::latex(Buffer const &, odocstream & os,
299                        OutputParams const & runparams) const
300 {
301         const int quoteind = quote_index[side_][language_];
302         string qstr;
303
304         if (language_ == FrenchQ && times_ == DoubleQ
305             && prefixIs(runparams.local_font->language()->code(), "fr")) {
306                 if (side_ == LeftQ)
307                         qstr = "\\og "; //the spaces are important here
308                 else
309                         qstr = " \\fg{}"; //and here
310         } else if (lyxrc.fontenc == "T1") {
311                 qstr = latex_quote_t1[times_][quoteind];
312 #ifdef DO_USE_DEFAULT_LANGUAGE
313         } else if (doclang == "default") {
314 #else
315         } else if (!runparams.use_babel) {
316 #endif
317                 qstr = latex_quote_ot1[times_][quoteind];
318         } else {
319                 qstr = latex_quote_babel[times_][quoteind];
320         }
321
322         // Always guard against unfortunate ligatures (!` ?`)
323         if (prefixIs(qstr, "`"))
324                 qstr.insert(0, "{}");
325
326         os << from_ascii(qstr);
327         return 0;
328 }
329
330
331 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
332                            OutputParams const &) const
333 {
334         os << dispString(buf.params().language);
335         return 0;
336 }
337
338
339 int InsetQuotes::docbook(Buffer const &, odocstream & os,
340                          OutputParams const &) const
341 {
342         if (times_ == DoubleQ) {
343                 if (side_ == LeftQ)
344                         os << "&ldquo;";
345                 else
346                         os << "&rdquo;";
347         } else {
348                 if (side_ == LeftQ)
349                         os << "&lsquo;";
350                 else
351                         os << "&rsquo;";
352         }
353         return 0;
354 }
355
356
357 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
358 {
359         os << dispString(buf.params().language);
360 }
361
362
363 void InsetQuotes::validate(LaTeXFeatures & features) const
364 {
365         bool const use_babel = features.useBabel();
366         char type = quote_char[quote_index[side_][language_]];
367
368 #ifdef DO_USE_DEFAULT_LANGUAGE
369         if (features.bufferParams().language->lang() == "default"
370 #else
371         if (!use_babel
372 #endif
373             && lyxrc.fontenc != "T1") {
374                 if (times_ == SingleQ)
375                         switch (type) {
376                                 case ',': features.require("quotesinglbase");  break;
377                         case '<': features.require("guilsinglleft");  break;
378                         case '>': features.require("guilsinglright"); break;
379                         default: break;
380                         }
381                 else
382                         switch (type) {
383                         case ',': features.require("quotedblbase");   break;
384                         case '<': features.require("guillemotleft");  break;
385                         case '>': features.require("guillemotright"); break;
386                         default: break;
387                         }
388         }
389 }
390
391
392 auto_ptr<InsetBase> InsetQuotes::doClone() const
393 {
394         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
395 }
396
397
398 InsetBase::Code InsetQuotes::lyxCode() const
399 {
400         return InsetBase::QUOTE_CODE;
401 }
402
403
404 } // namespace lyx