]> git.lyx.org Git - lyx.git/blob - src/factory.C
XML sanitation
[lyx.git] / src / factory.C
1 /**
2  * \file factory.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "factory.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "bufferparams.h"
18 #include "debug.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "paragraph.h"
24
25 #include "insets/insetbibitem.h"
26 #include "insets/insetbibtex.h"
27 #include "insets/insetcaption.h"
28 #include "insets/insetcite.h"
29 #include "insets/insetcharstyle.h"
30 #include "insets/insetenv.h"
31 #include "insets/insetert.h"
32 #include "insets/insetexternal.h"
33 #include "insets/insetfloat.h"
34 #include "insets/insetfloatlist.h"
35 #include "insets/insetfoot.h"
36 #include "insets/insetgraphics.h"
37 #include "insets/insethfill.h"
38 #include "insets/insetinclude.h"
39 #include "insets/insetindex.h"
40 #include "insets/insetlabel.h"
41 #include "insets/insetline.h"
42 #include "insets/insetmarginal.h"
43 #include "insets/insetminipage.h"
44 #include "insets/insetnote.h"
45 #include "insets/insetbox.h"
46 #include "insets/insetbranch.h"
47 #include "insets/insetoptarg.h"
48 #include "insets/insetpagebreak.h"
49 #include "insets/insetref.h"
50 #include "insets/insetspace.h"
51 #include "insets/insettabular.h"
52 #include "insets/insettoc.h"
53 #include "insets/inseturl.h"
54 #include "insets/insetwrap.h"
55 #include "mathed/formulamacro.h"
56 #include "mathed/formula.h"
57
58 #include "frontends/Dialogs.h"
59 #include "frontends/LyXView.h"
60 #include "support/lstrings.h"
61 #include "support/std_sstream.h"
62
63 #include <boost/assert.hpp>
64
65 using lyx::support::compare_ascii_no_case;
66
67 using std::auto_ptr;
68 using std::endl;
69 using std::string;
70
71
72 InsetOld * createInset(FuncRequest const & cmd)
73 {
74         BufferView * bv = cmd.view();
75         BufferParams const & params = bv->buffer()->params();
76
77         switch (cmd.action) {
78         case LFUN_HFILL:
79                 return new InsetHFill;
80
81         case LFUN_INSERT_LINE:
82                 return new InsetLine;
83
84         case LFUN_INSERT_PAGEBREAK:
85                 return new InsetPagebreak;
86
87         case LFUN_INSET_MINIPAGE:
88                 return new InsetMinipage(params);
89
90         case LFUN_INSERT_CHARSTYLE: {
91                 string s = cmd.getArg(0);
92                 CharStyles::iterator found_cs = params.getLyXTextClass().charstyle(s);
93                 return new InsetCharStyle(params, found_cs);
94         }
95
96         case LFUN_INSERT_NOTE: {
97                 string arg = cmd.getArg(0);
98                 if (arg.empty())
99                         arg = "Note";
100                 return new InsetNote(params, arg);
101         }
102
103         case LFUN_INSERT_BOX: {
104                 string arg = cmd.getArg(0);
105                 if (arg.empty())
106                         arg = "Boxed";
107                 return new InsetBox(params, arg);
108         }
109
110         case LFUN_INSERT_BRANCH: {
111                 string arg = cmd.getArg(0);
112                 if (arg.empty())
113                         arg = "none";
114                 return new InsetBranch(params, arg);
115         }
116
117         case LFUN_INSET_ERT:
118                 return new InsetERT(params);
119
120         case LFUN_INSET_FOOTNOTE:
121                 return new InsetFoot(params);
122
123         case LFUN_INSET_MARGINAL:
124                 return new InsetMarginal(params);
125
126         case LFUN_INSET_OPTARG:
127                 return new InsetOptArg(params);
128
129         case LFUN_INSERT_BIBITEM:
130                 return new InsetBibitem(InsetCommandParams("bibitem"));
131
132         case LFUN_INSET_FLOAT:
133                 // check if the float type exists
134                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
135                         return new InsetFloat(params, cmd.argument);
136                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
137                 return 0;
138
139         case LFUN_INSET_WIDE_FLOAT:
140                 // check if the float type exists
141                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
142                         auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
143                         p->wide(true, params);
144                         return p.release();
145                 }
146                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
147                 return 0;
148
149         case LFUN_INSET_WRAP:
150                 if (cmd.argument == "figure")
151                         return new InsetWrap(params, cmd.argument);
152                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
153                 return 0;
154
155         case LFUN_INDEX_INSERT: {
156                 // Try and generate a valid index entry.
157                 InsetCommandParams icp("index");
158                 string const contents = cmd.argument.empty() ?
159                         bv->getLyXText()->getStringToIndex() :
160                         cmd.argument;
161                 icp.setContents(contents);
162
163                 string data = InsetCommandMailer::params2string("index", icp);
164                 LyXView * lv = bv->owner();
165
166                 if (icp.getContents().empty()) {
167                         lv->getDialogs().show("index", data, 0);
168                 } else {
169                         lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
170                 }
171                 return 0;
172         }
173
174         case LFUN_TABULAR_INSERT:
175                 if (!cmd.argument.empty()) {
176                         std::istringstream ss(cmd.argument);
177                         int r, c;
178                         ss >> r >> c;
179                         if (r <= 0) r = 2;
180                         if (c <= 0) c = 2;
181                         return new InsetTabular(*bv->buffer(), r, c);
182                 }
183                 bv->owner()->getDialogs().show("tabularcreate");
184                 return 0;
185
186         case LFUN_INSET_CAPTION: 
187         if (bv->innerInset()) {
188                 auto_ptr<InsetCaption> inset(new InsetCaption(params));
189                 inset->setOwner(bv->innerInset());
190                 inset->setAutoBreakRows(true);
191                 inset->setDrawFrame(InsetText::LOCKED);
192                 inset->setFrameColor(LColor::captionframe);
193                 return inset.release();
194         }
195
196         case LFUN_INDEX_PRINT:
197                 return new InsetPrintIndex(InsetCommandParams("printindex"));
198
199         case LFUN_TOC_INSERT:
200                 return new InsetTOC(InsetCommandParams("tableofcontents"));
201
202         case LFUN_ENVIRONMENT_INSERT:
203                 return new InsetEnvironment(params, cmd.argument);
204
205 #if 0
206         case LFUN_INSET_LIST:
207                 return new InsetList;
208
209         case LFUN_INSET_THEOREM:
210                 return new InsetTheorem;
211 #endif
212
213         case LFUN_INSET_INSERT: {
214                 string const name = cmd.getArg(0);
215
216                 if (name == "bibitem") {
217                         InsetCommandParams icp;
218                         InsetCommandMailer::string2params(cmd.argument, icp);
219                         return new InsetBibitem(icp);
220
221                 } else if (name == "bibtex") {
222                         InsetCommandParams icp;
223                         InsetCommandMailer::string2params(cmd.argument, icp);
224                         return new InsetBibtex(icp);
225
226                 } else if (name == "citation") {
227                         InsetCommandParams icp;
228                         InsetCommandMailer::string2params(cmd.argument, icp);
229                         return new InsetCitation(icp);
230
231                 } else if (name == "ert") {
232                         InsetERT * inset = new InsetERT(params);
233                         InsetERT::ERTStatus s;
234                         InsetERTMailer::string2params(cmd.argument, s);
235                         inset->status(bv, s);
236                         return inset;
237
238                 } else if (name == "external") {
239                         Buffer const & buffer = *cmd.view()->buffer();
240                         InsetExternalParams iep;
241                         InsetExternalMailer::string2params(cmd.argument,
242                                                            buffer, iep);
243                         auto_ptr<InsetExternal> inset(new InsetExternal);
244                         inset->setParams(iep, buffer);
245                         return inset.release();
246
247                 } else if (name == "graphics") {
248                         Buffer const & buffer = *cmd.view()->buffer();
249                         InsetGraphicsParams igp;
250                         InsetGraphicsMailer::string2params(cmd.argument,
251                                                            buffer, igp);
252                         auto_ptr<InsetGraphics> inset(new InsetGraphics);
253                         inset->setParams(igp);
254                         return inset.release();
255
256                 } else if (name == "include") {
257                         InsetCommandParams iip;
258                         InsetIncludeMailer::string2params(cmd.argument, iip);
259                         return new InsetInclude(iip);
260
261                 } else if (name == "index") {
262                         InsetCommandParams icp;
263                         InsetCommandMailer::string2params(cmd.argument, icp);
264                         return new InsetIndex(icp);
265
266                 } else if (name == "label") {
267                         InsetCommandParams icp;
268                         InsetCommandMailer::string2params(cmd.argument, icp);
269                         return new InsetLabel(icp);
270
271                 } else if (name == "ref") {
272                         InsetCommandParams icp;
273                         InsetCommandMailer::string2params(cmd.argument, icp);
274                         return new InsetRef(icp, *bv->buffer());
275
276                 } else if (name == "toc") {
277                         InsetCommandParams icp;
278                         InsetCommandMailer::string2params(cmd.argument, icp);
279                         return new InsetTOC(icp);
280
281                 } else if (name == "url") {
282                         InsetCommandParams icp;
283                         InsetCommandMailer::string2params(cmd.argument, icp);
284                         return new InsetUrl(icp);
285                 }
286         }
287
288         case LFUN_SPACE_INSERT: {
289                 string const name = cmd.argument;
290                 if (name == "normal")
291                         return new InsetSpace(InsetSpace::NORMAL);
292                 else if (name == "protected")
293                         return new InsetSpace(InsetSpace::PROTECTED);
294                 else if (name == "thin")
295                         return new InsetSpace(InsetSpace::THIN);
296                 else if (name == "quad")
297                         return new InsetSpace(InsetSpace::QUAD);
298                 else if (name == "qquad")
299                         return new InsetSpace(InsetSpace::QQUAD);
300                 else if (name == "enspace")
301                         return new InsetSpace(InsetSpace::ENSPACE);
302                 else if (name == "enskip")
303                         return new InsetSpace(InsetSpace::ENSKIP);
304                 else if (name == "negthinspace")
305                         return new InsetSpace(InsetSpace::NEGTHIN);
306                 else if (name.empty())
307                         lyxerr << "LyX function 'space' needs an argument." << endl;
308                 else
309                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
310         }
311
312         break;
313
314         default:
315                 break;
316         }
317
318         return 0;
319 }
320
321
322 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
323 {
324         // consistency check
325         if (lex.getString() != "\\begin_inset") {
326                 lyxerr << "Buffer::readInset: Consistency check failed."
327                        << endl;
328         }
329
330         auto_ptr<InsetOld> inset;
331
332         LyXTextClass tclass = buf.params().getLyXTextClass();
333                 
334         lex.next();
335         string tmptok = lex.getString();
336         CharStyles::iterator found_cs = tclass.charstyle(tmptok);
337         if (found_cs != tclass.charstyles().end())
338                 tmptok = "CharStyle";
339
340         // test the different insets
341         if (tmptok == "LatexCommand") {
342                 InsetCommandParams inscmd;
343                 inscmd.read(lex);
344
345                 string const cmdName = inscmd.getCmdName();
346
347                 // This strange command allows LyX to recognize "natbib" style
348                 // citations: citet, citep, Citet etc.
349                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
350                         inset.reset(new InsetCitation(inscmd));
351                 } else if (cmdName == "bibitem") {
352                         lex.printError("Wrong place for bibitem");
353                         inset.reset(new InsetBibitem(inscmd));
354                 } else if (cmdName == "bibtex") {
355                         inset.reset(new InsetBibtex(inscmd));
356                 } else if (cmdName == "index") {
357                         inset.reset(new InsetIndex(inscmd));
358                 } else if (cmdName == "include") {
359                         inset.reset(new InsetInclude(inscmd));
360                 } else if (cmdName == "label") {
361                         inset.reset(new InsetLabel(inscmd));
362                 } else if (cmdName == "url"
363                            || cmdName == "htmlurl") {
364                         inset.reset(new InsetUrl(inscmd));
365                 } else if (cmdName == "ref"
366                            || cmdName == "eqref"
367                            || cmdName == "pageref"
368                            || cmdName == "vref"
369                            || cmdName == "vpageref"
370                            || cmdName == "prettyref") {
371                         if (!inscmd.getOptions().empty()
372                             || !inscmd.getContents().empty()) {
373                                 inset.reset(new InsetRef(inscmd, buf));
374                         }
375                 } else if (cmdName == "tableofcontents") {
376                         inset.reset(new InsetTOC(inscmd));
377                 } else if (cmdName == "listofalgorithms") {
378                         inset.reset(new InsetFloatList("algorithm"));
379                 } else if (cmdName == "listoffigures") {
380                         inset.reset(new InsetFloatList("figure"));
381                 } else if (cmdName == "listoftables") {
382                         inset.reset(new InsetFloatList("table"));
383                 } else if (cmdName == "printindex") {
384                         inset.reset(new InsetPrintIndex(inscmd));
385                 } else {
386                         lyxerr << "unknown CommandInset '" << cmdName
387                                << "'" << std::endl;
388                         while (lex.isOK() && lex.getString() != "\\end_inset")
389                                 lex.next();
390                         return 0;
391                 }
392         } else {
393                 if (tmptok == "Quotes") {
394                         inset.reset(new InsetQuotes);
395                 } else if (tmptok == "External") {
396                         inset.reset(new InsetExternal);
397                 } else if (tmptok == "FormulaMacro") {
398                         inset.reset(new InsetFormulaMacro);
399                 } else if (tmptok == "Formula") {
400                         inset.reset(new InsetFormula);
401                 } else if (tmptok == "Graphics") {
402                         inset.reset(new InsetGraphics);
403                 } else if (tmptok == "Note"     || tmptok == "Comment"
404                                 || tmptok == "Greyedout") {
405                         inset.reset(new InsetNote(buf.params(), tmptok));
406                 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
407                         || tmptok == "Shadowbox" || tmptok == "Doublebox"
408                         || tmptok == "Ovalbox" || tmptok == "Frameless") {
409                         inset.reset(new InsetBox(buf.params(), tmptok));
410                 } else if (tmptok == "CharStyle") {
411                         inset.reset(new InsetCharStyle(buf.params(), found_cs));
412                 } else if (tmptok == "Branch") {
413                         inset.reset(new InsetBranch(buf.params(), string()));
414                 } else if (tmptok == "Include") {
415                         InsetCommandParams p("Include");
416                         inset.reset(new InsetInclude(p));
417                 } else if (tmptok == "Environment") {
418                         lex.next();
419                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
420                 } else if (tmptok == "ERT") {
421                         inset.reset(new InsetERT(buf.params()));
422                 } else if (tmptok == "InsetSpace") {
423                         inset.reset(new InsetSpace);
424                 } else if (tmptok == "Tabular") {
425                         inset.reset(new InsetTabular(buf));
426                 } else if (tmptok == "Text") {
427                         inset.reset(new InsetText(buf.params()));
428                 } else if (tmptok == "Foot") {
429                         inset.reset(new InsetFoot(buf.params()));
430                 } else if (tmptok == "Marginal") {
431                         inset.reset(new InsetMarginal(buf.params()));
432                 } else if (tmptok == "OptArg") {
433                         inset.reset(new InsetOptArg(buf.params()));
434                 } else if (tmptok == "Minipage") {
435                         inset.reset(new InsetMinipage(buf.params()));
436                 } else if (tmptok == "Float") {
437                         lex.next();
438                         string tmptok = lex.getString();
439                         inset.reset(new InsetFloat(buf.params(), tmptok));
440                 } else if (tmptok == "Wrap") {
441                         lex.next();
442                         string tmptok = lex.getString();
443                         inset.reset(new InsetWrap(buf.params(), tmptok));
444 #if 0
445                 } else if (tmptok == "List") {
446                         inset.reset(new InsetList);
447                 } else if (tmptok == "Theorem") {
448                         inset.reset(new InsetList);
449 #endif
450                 } else if (tmptok == "Caption") {
451                         inset.reset(new InsetCaption(buf.params()));
452                 } else if (tmptok == "FloatList") {
453                         inset.reset(new InsetFloatList);
454                 } else {
455                         lyxerr << "unknown Inset type '" << tmptok
456                                << "'" << std::endl;
457                         while (lex.isOK() && lex.getString() != "\\end_inset")
458                                 lex.next();
459                         return 0;
460                 }
461
462                 inset->read(buf, lex);
463         }
464
465         return inset.release();
466 }