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