]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Enable convertDefault.sh to run even if its executable bit is not set.
[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 "BufferView.h"
17 #include "debug.h"
18 #include "dimension.h"
19 #include "language.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
22 #include "lyxfont.h"
23 #include "lyxlex.h"
24 #include "lyxrc.h"
25 #include "paragraph.h"
26 #include "metricsinfo.h"
27 #include "paragraph_funcs.h"
28
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/LAssert.h"
33 #include "support/lstrings.h"
34
35 using namespace lyx::support;
36
37 using std::ostream;
38 using std::endl;
39 using std::auto_ptr;
40
41 namespace {
42
43 /* codes used to read/write quotes to LyX files
44  * e    ``english''
45  * s    ''spanish''
46  * g    ,,german``
47  * p    ,,polish''
48  * f    <<french>>
49  * a    >>danish<<
50  */
51
52 char const * const language_char = "esgpfa";
53 char const * const side_char = "lr" ;
54 char const * const times_char = "sd";
55
56 // List of known quote chars
57 char const * const quote_char = ",'`<>";
58
59 // Index of chars used for the quote. Index is [side, language]
60 int quote_index[2][6] = {
61         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
62         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
63
64 // Corresponding LaTeX code, for double and single quotes.
65 char const * const latex_quote_t1[2][5] =
66 { { "\\quotesinglbase ",  "'", "`",
67     "\\guilsinglleft{}", "\\guilsinglright{}" },
68   { ",,", "''", "``", "<<", ">>" } };
69
70 char const * const latex_quote_ot1[2][5] =
71 { { "\\quotesinglbase ",  "'", "`",
72     "\\guilsinglleft{}", "\\guilsinglright{}" },
73   { "\\quotedblbase ", "''", "``",
74     "\\guillemotleft{}", "\\guillemotright{}" } };
75
76 char const * const latex_quote_babel[2][5] =
77 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
78   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
79
80 } // namespace anon
81
82
83 InsetQuotes::InsetQuotes(string const & str)
84 {
85         parseString(str);
86 }
87
88
89 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
90         : language_(l), side_(s), times_(t)
91 {}
92
93
94 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
95         : language_(params.quotes_language), times_(params.quotes_times)
96 {
97         // Decide whether left or right
98         switch (c) {
99         case ' ': case '(':
100 #warning eh ? I am lost here ...
101         //case Paragraph::META_HFILL:
102         // case Paragraph::META_NEWLINE:
103                 side_ = LeftQ;   // left quote
104                 break;
105         default:
106                 side_ = RightQ;  // right quote
107         }
108 }
109
110
111 void InsetQuotes::parseString(string const & s)
112 {
113         string str(s);
114         if (str.length() != 3) {
115                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
116                         " bad string length." << endl;
117                 str = "eld";
118         }
119
120         int i;
121
122         for (i = 0; i < 6; ++i) {
123                 if (str[0] == language_char[i]) {
124                         language_ = quote_language(i);
125                         break;
126                 }
127         }
128         if (i >= 6) {
129                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130                         " bad language specification." << endl;
131                 language_ = EnglishQ;
132         }
133
134         for (i = 0; i < 2; ++i) {
135                 if (str[1] == side_char[i]) {
136                         side_ = quote_side(i);
137                         break;
138                 }
139         }
140         if (i >= 2) {
141                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142                         " bad side specification." << endl;
143                 side_ = LeftQ;
144         }
145
146         for (i = 0; i < 2; ++i) {
147                 if (str[2] == times_char[i]) {
148                         times_ = quote_times(i);
149                         break;
150                 }
151         }
152         if (i >= 2) {
153                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154                         " bad times specification." << endl;
155                 times_ = DoubleQ;
156         }
157 }
158
159
160 string const InsetQuotes::dispString(Language const * loclang) const
161 {
162         string disp;
163         disp += quote_char[quote_index[side_][language_]];
164         if (times_ == DoubleQ)
165                 disp += disp;
166
167         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
168             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
169             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
170                 if (disp == "<<")
171                         disp = '«';
172                 else if (disp == ">>")
173                         disp = '»';
174         }
175
176         // in french, spaces are added inside double quotes
177         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
178                 if (side_ == LeftQ)
179                         disp += ' ';
180                 else
181                         disp.insert(string::size_type(0), 1, ' ');
182         }
183
184         return disp;
185 }
186
187
188 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
189 {
190         LyXFont & font = mi.base.font;
191         dim.asc = font_metrics::maxAscent(font);
192         dim.des = font_metrics::maxDescent(font);
193         dim.wid = 0;
194
195         string const text = dispString(font.language());
196         for (string::size_type i = 0; i < text.length(); ++i) {
197                 if (text[i] == ' ')
198                         dim.wid += font_metrics::width('i', font);
199                 else if (i == 0 || text[i] != text[i - 1])
200                         dim.wid += font_metrics::width(text[i], font);
201                 else
202                         dim.wid += font_metrics::width(',', font);
203         }
204         dim_ = dim;
205 }
206
207
208 #if 0
209 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
210 {
211 #if 1
212         return f;
213 #else
214         LyXFont font(f);
215         return font;
216 #endif
217 }
218 #endif
219
220
221 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
222 {
223         string const text = dispString(pi.base.font.language());
224
225         if (text.length() == 2 && text[0] == text[1]) {
226                 pi.pain.text(x, y, text[0], pi.base.font);
227                 int const t = font_metrics::width(',', pi.base.font);
228                 pi.pain.text(x + t, y, text[0], pi.base.font);
229         } else {
230                 pi.pain.text(x, y, text, pi.base.font);
231         }
232 }
233
234
235 void InsetQuotes::write(Buffer const &, ostream & os) const
236 {
237         string text;
238         text += language_char[language_];
239         text += side_char[side_];
240         text += times_char[times_];
241         os << "Quotes " << text;
242 }
243
244
245 void InsetQuotes::read(Buffer const &, LyXLex & lex)
246 {
247         lex.nextToken();
248         parseString(lex.getString());
249         lex.next();
250         if (lex.getString() != "\\end_inset") {
251                 lex.printError("Missing \\end_inset at this point");
252         }
253 }
254
255
256 int InsetQuotes::latex(Buffer const & buf, ostream & os,
257                        LatexRunParams const & runparams) const
258 {
259         // How do we get the local language here??
260         lyx::pos_type curr_pos = ownerPar(buf, this).getPositionOfInset(this);
261         Assert(curr_pos != -1);
262
263 #warning FIXME. We _must_ find another way to get the language. (Lgb)
264 #if 0
265         // This cannot be used. (Lgb)
266         string const curr_lang =
267                 parOwner()->getFont(buf->params, curr_pos).language()->babel();
268 #else
269         // And this is not the way... (Lgb)
270         string const curr_lang = buf.params.language->lang();
271 #endif
272         const int quoteind = quote_index[side_][language_];
273         string qstr;
274
275         if (language_ == FrenchQ && times_ == DoubleQ
276             && curr_lang == "frenchb") {
277                 if (side_ == LeftQ)
278                         qstr = "\\og "; //the spaces are important here
279                 else
280                         qstr = " \\fg{}"; //and here
281         } else if (language_ == FrenchQ && times_ == DoubleQ
282                    && curr_lang == "french") {
283                 if (side_ == LeftQ)
284                         qstr = "<< "; //the spaces are important here
285                 else
286                         qstr = " >>"; //and here
287         } else if (lyxrc.fontenc == "T1") {
288                 qstr = latex_quote_t1[times_][quoteind];
289 #ifdef DO_USE_DEFAULT_LANGUAGE
290         } else if (doclang == "default") {
291 #else
292         } else if (!runparams.use_babel) {
293 #endif
294                 qstr = latex_quote_ot1[times_][quoteind];
295         } else {
296                 qstr = latex_quote_babel[times_][quoteind];
297         }
298
299         // Always guard against unfortunate ligatures (!` ?`)
300         if (prefixIs(qstr, "`"))
301                 qstr.insert(0, "{}");
302
303         os << qstr;
304         return 0;
305 }
306
307
308 int InsetQuotes::ascii(Buffer const &, ostream & os, int) const
309 {
310         os << '"';
311         return 0;
312 }
313
314
315 int InsetQuotes::linuxdoc(Buffer const &, ostream & os) const
316 {
317         os << '"';
318         return 0;
319 }
320
321
322 int InsetQuotes::docbook(Buffer const &, ostream & os, bool) const
323 {
324         if (times_ == DoubleQ) {
325                 if (side_ == LeftQ)
326                         os << "&ldquo;";
327                 else
328                         os << "&rdquo;";
329         } else {
330                 if (side_ == LeftQ)
331                         os << "&lsquo;";
332                 else
333                         os << "&rsquo;";
334         }
335         return 0;
336 }
337
338
339 void InsetQuotes::validate(LaTeXFeatures & features) const
340 {
341         bool const use_babel = features.useBabel();
342         char type = quote_char[quote_index[side_][language_]];
343
344 #ifdef DO_USE_DEFAULT_LANGUAGE
345         if (features.bufferParams().language->lang() == "default"
346 #else
347         if (!use_babel
348 #endif
349             && lyxrc.fontenc != "T1") {
350                 if (times_ == SingleQ)
351                         switch (type) {
352                                 case ',': features.require("quotesinglbase");  break;
353                         case '<': features.require("guilsinglleft");  break;
354                         case '>': features.require("guilsinglright"); break;
355                         default: break;
356                         }
357                 else
358                         switch (type) {
359                         case ',': features.require("quotedblbase");   break;
360                         case '<': features.require("guillemotleft");  break;
361                         case '>': features.require("guillemotright"); break;
362                         default: break;
363                         }
364         }
365 }
366
367
368 auto_ptr<InsetBase> InsetQuotes::clone() const
369 {
370         return auto_ptr<InsetBase>(new InsetQuotes(language_, side_, times_));
371 }
372
373
374 InsetOld::Code InsetQuotes::lyxCode() const
375 {
376   return InsetOld::QUOTE_CODE;
377 }