]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Fix crash when running lyx -dbg insets -e ...
[lyx.git] / src / insets / insetquotes.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetquotes.h"
18 #include "support/lyxlib.h"
19 #include "debug.h"
20 #include "lyxfont.h"
21 #include "lyxrc.h"
22 #include "buffer.h"
23 #include "LaTeXFeatures.h"
24 #include "support/lstrings.h"
25 #include "Painter.h"
26 #include "font.h"
27 #include "language.h"
28 #include "BufferView.h"
29
30 using std::ostream;
31 using std::endl;
32
33 // Quotes. Used for the various quotes. German, English, French,
34 // Danish, Polish, all either double or single.
35
36 namespace {
37
38 // codes used to read/write quotes to LyX files
39 char const * const language_char = "esgpfa";
40 char const * const side_char = "lr" ;
41 char const * const times_char = "sd";
42
43 // List of known quote chars
44 char const * const quote_char = ",'`<>";
45
46 // Index of chars used for the quote. Index is [side, language]
47 int quote_index[2][6] = {
48         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
49         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
50
51 // Corresponding LaTeX code, for double and single quotes.
52 char const * const latex_quote_t1[2][5] = 
53 { { "\\quotesinglbase{}",  "'", "`", 
54     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
55   { ",,", "''", "``", "<<", ">>" } };
56
57 char const * const latex_quote_ot1[2][5] = 
58 { { "\\quotesinglbase{}",  "'", "`", 
59     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
60   { "\\quotedblbase{}", "''", "``",
61     "\\guillemotleft{}", "\\guillemotright{}" } };
62
63 char const * const latex_quote_babel[2][5] = 
64 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
65   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
66
67 } // namespace anon
68
69
70 InsetQuotes::InsetQuotes(string const & str)
71 {
72         parseString(str);
73 }
74
75
76 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
77                          InsetQuotes::quote_side s,
78                          InsetQuotes::quote_times t)
79         : language(l), side(s), times(t)
80 {}
81
82
83 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
84         : language(params.quotes_language), times(params.quotes_times)
85 {
86         // Decide whether left or right 
87         switch (c) {
88         case ' ': case '(': case '{': case '[': case '-': case ':':
89         case Paragraph::META_HFILL:
90         case Paragraph::META_NEWLINE: 
91                 side = InsetQuotes::LeftQ;   // left quote 
92                 break;
93         default:
94                 side = InsetQuotes::RightQ;  // right quote
95         }
96 }
97
98
99 void InsetQuotes::parseString(string const & s)
100 {
101         string str(s);
102         if (str.length() != 3) {
103                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
104                         " bad string length." << endl;
105                 str = "eld";
106         }
107
108         int i;
109         
110         for (i = 0; i < 6; ++i) {
111                 if (str[0] == language_char[i]) {
112                         language = InsetQuotes::quote_language(i);
113                         break;
114                 }
115         }
116         if (i >= 6) {
117                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
118                         " bad language specification." << endl;
119                 language = InsetQuotes::EnglishQ; 
120         }
121
122         for (i = 0; i < 2; ++i) {
123                 if (str[1] == side_char[i]) {
124                         side = InsetQuotes::quote_side(i);
125                         break;
126                 }
127         }
128         if (i >= 2) {
129                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130                         " bad side specification." << endl;
131                 side = InsetQuotes::LeftQ; 
132         }
133
134         for (i = 0; i < 2; ++i) {
135                 if (str[2] == times_char[i]) {
136                         times = InsetQuotes::quote_times(i);
137                         break;
138                 }
139         }
140         if (i >= 2) {
141                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142                         " bad times specification." << endl;
143                 times = InsetQuotes::DoubleQ; 
144         }
145 }
146
147
148 string const InsetQuotes::dispString() const
149 {
150         string disp;
151         disp += quote_char[quote_index[side][language]];
152         if (times == InsetQuotes::DoubleQ)
153                 disp += disp;
154
155         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
156             || lyxrc.font_norm_type == LyXRC::ISO_8859_15)
157                 if (disp == "<<")
158                         disp = '«';
159                 else if (disp == ">>")
160                         disp = '»';
161         
162         return disp;
163 }
164
165
166 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
167 {
168         return lyxfont::maxAscent(font);
169 }
170
171
172 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
173 {
174         return lyxfont::maxDescent(font);
175 }
176
177
178 int InsetQuotes::width(BufferView *, LyXFont const & font) const
179 {
180         string text = dispString();
181         int w = 0;
182
183         for (string::size_type i = 0; i < text.length(); ++i) {
184                 if (text[i] == ' ')
185                         w += lyxfont::width('i', font);
186                 else if (i == 0 || text[i] != text[i-1])
187                         w += lyxfont::width(text[i], font);
188                 else
189                         w += lyxfont::width(',', font);
190         }
191
192         return w;
193 }
194
195
196 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
197 {
198         LyXFont font(f);
199 #ifndef NO_LATEX
200         // quotes-insets cannot be latex of any kind
201         font.setLatex(LyXFont::OFF);
202 #endif
203         return font;
204 }
205
206
207 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
208                        int baseline, float & x, bool) const
209 {
210         string text = dispString();
211
212         bv->painter().text(int(x), baseline, text, font);
213         x += width(bv, font);
214 }
215
216
217 void InsetQuotes::write(Buffer const *, ostream & os) const
218 {
219         string text;
220         text += language_char[language];
221         text += side_char[side];
222         text += times_char[times]; 
223         os << "Quotes " << text;
224 }
225
226
227 void InsetQuotes::read(Buffer const *, LyXLex & lex)
228 {
229         lex.nextToken();
230         parseString(lex.GetString());
231         lex.next();
232         string tmp(lex.GetString());
233         if (tmp != "\\end_inset")
234                 lyxerr << "LyX Warning: Missing \\end_inset "
235                         "in InsetQuotes::Read." << endl;
236 }
237
238
239 extern bool use_babel;
240
241 int InsetQuotes::latex(Buffer const * buf, ostream & os,
242                        bool /*fragile*/, bool) const
243 {
244         string const doclang = buf->getLanguage()->lang();
245         int quoteind = quote_index[side][language];
246         string qstr;
247         
248         if (lyxrc.fontenc == "T1") {
249                 qstr = latex_quote_t1[times][quoteind];
250 #ifdef DO_USE_DEFAULT_LANGUAGE
251         } else if (doclang == "default") {
252 #else
253         } else if (!use_babel) {
254 #endif
255                 qstr = latex_quote_ot1[times][quoteind];
256         } else if (language == InsetQuotes::FrenchQ 
257                  && times == InsetQuotes::DoubleQ
258                  && doclang == "frenchb") {
259                 if (side == InsetQuotes::LeftQ) 
260                         qstr = "\\og{}";
261                 else 
262                         qstr = " \\fg{}";
263         } else 
264                 qstr = latex_quote_babel[times][quoteind];
265
266         // Always guard against unfortunate ligatures (!` ?`)
267         if (prefixIs(qstr, "`"))
268                 qstr.insert(0, "{}");
269
270         os << qstr;
271         return 0;
272 }
273
274
275 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
276 {
277         os << "\"";
278         return 0;
279 }
280
281
282 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
283 {
284         os << "\"";
285         return 0;
286 }
287
288
289 int InsetQuotes::docBook(Buffer const *, ostream & os) const
290 {
291         if (times == InsetQuotes::DoubleQ) {
292                 if (side == InsetQuotes::LeftQ)
293                         os << "&ldquo;";
294                 else
295                         os << "&rdquo;";
296         } else {
297                 if (side == InsetQuotes::LeftQ)
298                         os << "&lsquo;";
299                 else
300                         os << "&rsquo;";
301         }
302         return 0;
303 }
304
305
306 void InsetQuotes::validate(LaTeXFeatures & features) const 
307 {
308         char type = quote_char[quote_index[side][language]];
309
310         if (features.bufferParams().language->lang() == "default" 
311             && lyxrc.fontenc != "T1") {
312                 if (times == InsetQuotes::SingleQ) 
313                         switch (type) {
314                         case ',': features.quotesinglbase = true; break;
315                         case '<': features.guilsinglleft = true; break;
316                         case '>': features.guilsinglright = true; break;
317                         default: break;
318                         }
319                 else 
320                         switch (type) {
321                         case ',': features.quotedblbase = true; break;
322                         case '<': features.guillemotleft = true; break;
323                         case '>': features.guillemotright = true; break;
324                         default: break;
325                         }
326         }
327 }
328
329
330 Inset * InsetQuotes::clone(Buffer const &, bool) const
331 {
332   return new InsetQuotes(language, side, times);
333 }
334
335
336 Inset::Code InsetQuotes::lyxCode() const
337 {
338   return Inset::QUOTE_CODE;
339 }