]> git.lyx.org Git - lyx.git/blob - src/factory.C
add comment
[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/insetnomencl.h"
41 #include "insets/insetlabel.h"
42 #include "insets/insetline.h"
43 #include "insets/insetmarginal.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/insetvspace.h"
55 #include "insets/insetwrap.h"
56
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
59
60 #include "support/lstrings.h"
61
62 #include <boost/assert.hpp>
63 #include <boost/current_function.hpp>
64
65 #include <sstream>
66
67
68 namespace lyx {
69
70 using support::compare_ascii_no_case;
71
72 using std::auto_ptr;
73 using std::endl;
74 using std::string;
75
76
77 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
78 {
79         BufferParams const & params = bv->buffer()->params();
80
81         switch (cmd.action) {
82         case LFUN_HFILL_INSERT:
83                 return new InsetHFill;
84
85         case LFUN_LINE_INSERT:
86                 return new InsetLine;
87
88         case LFUN_PAGEBREAK_INSERT:
89                 return new InsetPagebreak;
90
91         case LFUN_CLEARPAGE_INSERT:
92                 return new InsetClearPage;
93
94         case LFUN_CLEARDOUBLEPAGE_INSERT:
95                 return new InsetClearDoublePage;
96
97         case LFUN_CHARSTYLE_INSERT: {
98                 string s = cmd.getArg(0);
99                 LyXTextClass tclass = params.getLyXTextClass();
100                 CharStyles::iterator found_cs = tclass.charstyle(s);
101                 if (found_cs != tclass.charstyles().end())
102                         return new InsetCharStyle(params, found_cs);
103                 else
104                         return new InsetCharStyle(params, s);
105         }
106
107         case LFUN_NOTE_INSERT: {
108                 string arg = cmd.getArg(0);
109                 if (arg.empty())
110                         arg = "Note";
111                 return new InsetNote(params, arg);
112         }
113
114         case LFUN_BOX_INSERT: {
115                 string arg = cmd.getArg(0);
116                 if (arg.empty())
117                         arg = "Boxed";
118                 return new InsetBox(params, arg);
119         }
120
121         case LFUN_BRANCH_INSERT: {
122                 docstring arg = cmd.argument();
123                 if (arg.empty())
124                         arg = from_ascii("none");
125                 return new InsetBranch(params, InsetBranchParams(arg));
126         }
127
128         case LFUN_ERT_INSERT:
129                 return new InsetERT(params);
130
131         case LFUN_FOOTNOTE_INSERT:
132                 return new InsetFoot(params);
133
134         case LFUN_MARGINALNOTE_INSERT:
135                 return new InsetMarginal(params);
136
137         case LFUN_OPTIONAL_INSERT:
138                 return new InsetOptArg(params);
139
140         case LFUN_BIBITEM_INSERT:
141                 return new InsetBibitem(InsetCommandParams("bibitem"));
142
143         case LFUN_FLOAT_INSERT: {
144                 // check if the float type exists
145                 string const argument = to_utf8(cmd.argument());
146                 if (params.getLyXTextClass().floats().typeExist(argument))
147                         return new InsetFloat(params, argument);
148                 lyxerr << "Non-existent float type: " << argument << endl;
149                 return 0;
150         }
151
152         case LFUN_FLOAT_WIDE_INSERT: {
153                 // check if the float type exists
154                 string const argument = to_utf8(cmd.argument());
155                 if (params.getLyXTextClass().floats().typeExist(argument)) {
156                         auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
157                         p->wide(true, params);
158                         return p.release();
159                 }
160                 lyxerr << "Non-existent float type: " << argument << endl;
161                 return 0;
162         }
163
164         case LFUN_WRAP_INSERT: {
165                 string const argument = to_utf8(cmd.argument());
166                 if (argument == "figure")
167                         return new InsetWrap(params, argument);
168                 lyxerr << "Non-existent floatflt type: " << argument << endl;
169                 return 0;
170         }
171
172         case LFUN_INDEX_INSERT: {
173                 // Try and generate a valid index entry.
174                 InsetCommandParams icp("index");
175                 icp["name"] = cmd.argument().empty() ?
176                         bv->getLyXText()->getStringToIndex(bv->cursor()) :
177                         cmd.argument();
178                 return new InsetIndex(icp);
179         }
180
181         case LFUN_NOMENCL_INSERT: {
182                 InsetCommandParams icp("nomenclature");
183                 icp["symbol"] = cmd.argument().empty() ?
184                         bv->getLyXText()->getStringToIndex(bv->cursor()) :
185                         cmd.argument();
186                 return new InsetNomencl(icp);
187         }
188
189         case LFUN_TABULAR_INSERT: {
190                 if (cmd.argument().empty())
191                         return 0;
192                 std::istringstream ss(to_utf8(cmd.argument()));
193                 int r = 0, c = 0;
194                 ss >> r >> c;
195                 if (r <= 0)
196                         r = 2;
197                 if (c <= 0)
198                         c = 2;
199                 return new InsetTabular(*bv->buffer(), r, c);
200         }
201
202         case LFUN_CAPTION_INSERT: {
203                 auto_ptr<InsetCaption> inset(new InsetCaption(params));
204                 inset->setAutoBreakRows(true);
205                 inset->setDrawFrame(true);
206                 inset->setFrameColor(LColor::captionframe);
207                 return inset.release();
208         }
209
210         case LFUN_INDEX_PRINT:
211                 return new InsetPrintIndex(InsetCommandParams("printindex"));
212
213         case LFUN_NOMENCL_PRINT:
214                 return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
215
216         case LFUN_TOC_INSERT:
217                 return new InsetTOC(InsetCommandParams("tableofcontents"));
218
219         case LFUN_ENVIRONMENT_INSERT:
220                 return new InsetEnvironment(params, to_utf8(cmd.argument()));
221
222 #if 0
223         case LFUN_LIST_INSERT:
224                 return new InsetList;
225
226         case LFUN_THEOREM_INSERT:
227                 return new InsetTheorem;
228 #endif
229
230         case LFUN_INSET_INSERT: {
231                 string const name = cmd.getArg(0);
232
233                 if (name == "bibitem") {
234                         InsetCommandParams icp(name);
235                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
236                                                           icp);
237                         return new InsetBibitem(icp);
238
239                 } else if (name == "bibtex") {
240                         InsetCommandParams icp(name);
241                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
242                                                           icp);
243                         return new InsetBibtex(icp);
244
245                 } else if (name == "citation") {
246                         InsetCommandParams icp("cite");
247                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
248                                                           icp);
249                         return new InsetCitation(icp);
250
251                 } else if (name == "ert") {
252                         InsetCollapsable::CollapseStatus st;
253                         InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
254                         return new InsetERT(params, st);
255
256                 } else if (name == "external") {
257                         Buffer const & buffer = *bv->buffer();
258                         InsetExternalParams iep;
259                         InsetExternalMailer::string2params(to_utf8(cmd.argument()),
260                                                            buffer, iep);
261                         auto_ptr<InsetExternal> inset(new InsetExternal);
262                         inset->setParams(iep, buffer);
263                         return inset.release();
264
265                 } else if (name == "graphics") {
266                         Buffer const & buffer = *bv->buffer();
267                         InsetGraphicsParams igp;
268                         InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
269                                                            buffer, igp);
270                         auto_ptr<InsetGraphics> inset(new InsetGraphics);
271                         inset->setParams(igp);
272                         return inset.release();
273
274                 } else if (name == "include") {
275                         InsetCommandParams iip(name);
276                         InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
277                         return new InsetInclude(iip);
278
279                 } else if (name == "index") {
280                         InsetCommandParams icp(name);
281                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
282                                                           icp);
283                         return new InsetIndex(icp);
284
285                 } else if (name == "nomenclature") {
286                         InsetCommandParams icp(name);
287                         InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
288                                                           icp);
289                         return new InsetNomencl(icp);
290
291                 } else if (name == "label") {
292                         InsetCommandParams icp(name);
293                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
294                                                           icp);
295                         return new InsetLabel(icp);
296
297                 } else if (name == "ref") {
298                         InsetCommandParams icp(name);
299                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
300                                                           icp);
301                         return new InsetRef(icp, *bv->buffer());
302
303                 } else if (name == "toc") {
304                         InsetCommandParams icp("tableofcontents");
305                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
306                                                           icp);
307                         return new InsetTOC(icp);
308
309                 } else if (name == "url") {
310                         InsetCommandParams icp(name);
311                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
312                                                           icp);
313                         return new InsetUrl(icp);
314
315                 } else if (name == "vspace") {
316                         VSpace vspace;
317                         InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
318                         return new InsetVSpace(vspace);
319                 }
320         }
321
322         case LFUN_SPACE_INSERT: {
323                 string const name = to_utf8(cmd.argument());
324                 if (name == "normal")
325                         return new InsetSpace(InsetSpace::NORMAL);
326                 else if (name == "protected")
327                         return new InsetSpace(InsetSpace::PROTECTED);
328                 else if (name == "thin")
329                         return new InsetSpace(InsetSpace::THIN);
330                 else if (name == "quad")
331                         return new InsetSpace(InsetSpace::QUAD);
332                 else if (name == "qquad")
333                         return new InsetSpace(InsetSpace::QQUAD);
334                 else if (name == "enspace")
335                         return new InsetSpace(InsetSpace::ENSPACE);
336                 else if (name == "enskip")
337                         return new InsetSpace(InsetSpace::ENSKIP);
338                 else if (name == "negthinspace")
339                         return new InsetSpace(InsetSpace::NEGTHIN);
340                 else if (name.empty())
341                         lyxerr << "LyX function 'space' needs an argument." << endl;
342                 else
343                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
344         }
345
346         break;
347
348         default:
349                 break;
350         }
351
352         return 0;
353 }
354
355
356 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
357 {
358         // consistency check
359         if (lex.getString() != "\\begin_inset") {
360                 lyxerr << "Buffer::readInset: Consistency check failed."
361                        << endl;
362         }
363
364         auto_ptr<InsetBase> inset;
365
366         LyXTextClass tclass = buf.params().getLyXTextClass();
367
368         lex.next();
369         string tmptok = lex.getString();
370
371         // test the different insets
372         if (tmptok == "LatexCommand") {
373                 lex.next();
374                 string const cmdName = lex.getString();
375                 lex.pushToken(cmdName);
376
377                 InsetCommandParams inscmd(cmdName);
378                 inscmd.read(lex);
379
380                 // This strange command allows LyX to recognize "natbib" style
381                 // citations: citet, citep, Citet etc.
382                 // FIXME: We already have partial support for \\fullcite and
383                 // the various \\footcite commands. We should increase the
384                 // file format number and read these commands here, too.
385                 // Then we should use is_possible_cite_command() in
386                 // src/frontends/controllers/biblio.C to test for valid cite
387                 // commands.
388                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
389                         inset.reset(new InsetCitation(inscmd));
390                 } else if (cmdName == "bibitem") {
391                         inset.reset(new InsetBibitem(inscmd));
392                 } else if (cmdName == "bibtex") {
393                         inset.reset(new InsetBibtex(inscmd));
394                 } else if (cmdName == "index") {
395                         inset.reset(new InsetIndex(inscmd));
396                 } else if (cmdName == "nomenclature") {
397                         inset.reset(new InsetNomencl(inscmd));
398                 } else if (cmdName == "include") {
399                         inset.reset(new InsetInclude(inscmd));
400                 } else if (cmdName == "label") {
401                         inset.reset(new InsetLabel(inscmd));
402                 } else if (cmdName == "url"
403                            || cmdName == "htmlurl") {
404                         inset.reset(new InsetUrl(inscmd));
405                 } else if (cmdName == "ref"
406                            || cmdName == "eqref"
407                            || cmdName == "pageref"
408                            || cmdName == "vref"
409                            || cmdName == "vpageref"
410                            || cmdName == "prettyref") {
411                         if (!inscmd["name"].empty()
412                             || !inscmd["reference"].empty()) {
413                                 inset.reset(new InsetRef(inscmd, buf));
414                         }
415                 } else if (cmdName == "tableofcontents") {
416                         inset.reset(new InsetTOC(inscmd));
417                 } else if (cmdName == "listofalgorithms") {
418                         inset.reset(new InsetFloatList("algorithm"));
419                 } else if (cmdName == "listoffigures") {
420                         inset.reset(new InsetFloatList("figure"));
421                 } else if (cmdName == "listoftables") {
422                         inset.reset(new InsetFloatList("table"));
423                 } else if (cmdName == "printindex") {
424                         inset.reset(new InsetPrintIndex(inscmd));
425                 } else if (cmdName == "printnomenclature") {
426                         inset.reset(new InsetPrintNomencl(inscmd));
427                 } else {
428                         lyxerr << "unknown CommandInset '" << cmdName
429                                << "'" << std::endl;
430                         while (lex.isOK() && lex.getString() != "\\end_inset")
431                                 lex.next();
432                         return 0;
433                 }
434         } else {
435                 if (tmptok == "Quotes") {
436                         inset.reset(new InsetQuotes);
437                 } else if (tmptok == "External") {
438                         inset.reset(new InsetExternal);
439                 } else if (tmptok == "FormulaMacro") {
440                         inset.reset(new MathMacroTemplate);
441                 } else if (tmptok == "Formula") {
442                         inset.reset(new InsetMathHull);
443                 } else if (tmptok == "Graphics") {
444                         inset.reset(new InsetGraphics);
445                 } else if (tmptok == "Note") {
446                         inset.reset(new InsetNote(buf.params(), tmptok));
447                 } else if (tmptok == "Box") {
448                         inset.reset(new InsetBox(buf.params(), tmptok));
449                 } else if (tmptok == "CharStyle") {
450                         lex.next();
451                         string s = lex.getString();
452                         CharStyles::iterator found_cs = tclass.charstyle(s);
453                         if (found_cs != tclass.charstyles().end())
454                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
455                         else {
456                                 // "Undefined" inset
457                                 inset.reset(new InsetCharStyle(buf.params(), s));
458                         }
459                 } else if (tmptok == "Branch") {
460                         inset.reset(new InsetBranch(buf.params(),
461                                                     InsetBranchParams()));
462                 } else if (tmptok == "Include") {
463                         InsetCommandParams p("include");
464                         inset.reset(new InsetInclude(p));
465                 } else if (tmptok == "Environment") {
466                         lex.next();
467                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
468                 } else if (tmptok == "ERT") {
469                         inset.reset(new InsetERT(buf.params()));
470                 } else if (tmptok == "InsetSpace") {
471                         inset.reset(new InsetSpace);
472                 } else if (tmptok == "Tabular") {
473                         inset.reset(new InsetTabular(buf));
474                 } else if (tmptok == "Text") {
475                         inset.reset(new InsetText(buf.params()));
476                 } else if (tmptok == "VSpace") {
477                         inset.reset(new InsetVSpace);
478                 } else if (tmptok == "Foot") {
479                         inset.reset(new InsetFoot(buf.params()));
480                 } else if (tmptok == "Marginal") {
481                         inset.reset(new InsetMarginal(buf.params()));
482                 } else if (tmptok == "OptArg") {
483                         inset.reset(new InsetOptArg(buf.params()));
484                 } else if (tmptok == "Float") {
485                         lex.next();
486                         string tmptok = lex.getString();
487                         inset.reset(new InsetFloat(buf.params(), tmptok));
488                 } else if (tmptok == "Wrap") {
489                         lex.next();
490                         string tmptok = lex.getString();
491                         inset.reset(new InsetWrap(buf.params(), tmptok));
492 #if 0
493                 } else if (tmptok == "List") {
494                         inset.reset(new InsetList);
495                 } else if (tmptok == "Theorem") {
496                         inset.reset(new InsetList);
497 #endif
498                 } else if (tmptok == "Caption") {
499                         inset.reset(new InsetCaption(buf.params()));
500                 } else if (tmptok == "FloatList") {
501                         inset.reset(new InsetFloatList);
502                 } else {
503                         lyxerr << "unknown Inset type '" << tmptok
504                                << "'" << std::endl;
505                         while (lex.isOK() && lex.getString() != "\\end_inset")
506                                 lex.next();
507                         return 0;
508                 }
509
510                 inset->read(buf, lex);
511
512 #ifdef WITH_WARNINGS
513 #warning hack..
514 #endif
515                 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
516                         MathMacroTemplate const * tmpl =
517                                 static_cast<MathMacroTemplate*>(inset.get());
518                         MacroTable::globalMacros().insert
519                                 (tmpl->name(), tmpl->asMacroData());
520                         lyxerr[Debug::DEBUG]
521                                 << BOOST_CURRENT_FUNCTION
522                                 << ": creating local macro " << to_utf8(tmpl->name())
523                                 << endl;
524                 }
525         }
526
527         return inset.release();
528 }
529
530
531 } // namespace lyx