]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
a1a375f9b56004a637afe1ffa56baca9dcc3865c
[lyx.git] / src / factory.cpp
1 /**
2  * \file factory.cpp
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 "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
19 #include "Lexer.h"
20 #include "LyX.h"
21 #include "TextClass.h"
22
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetListings.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/InsetHyperlink.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNewline.h"
45 #include "insets/InsetNewpage.h"
46 #include "insets/InsetNomencl.h"
47 #include "insets/InsetNote.h"
48 #include "insets/InsetArgument.h"
49 #include "insets/InsetPhantom.h"
50 #include "insets/InsetPreview.h"
51 #include "insets/InsetRef.h"
52 #include "insets/InsetSpace.h"
53 #include "insets/InsetTabular.h"
54 #include "insets/InsetTOC.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
57
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
60
61 #include "frontends/alert.h"
62
63 #include "support/debug.h"
64 #include "support/lstrings.h"
65 #include "support/ExceptionMessage.h"
66
67 #include "support/lassert.h"
68
69 #include <sstream>
70
71 using namespace std;
72 using namespace lyx::support;
73
74 namespace lyx {
75
76 namespace Alert = frontend::Alert;
77
78
79 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
80 {
81         try {
82
83                 switch (cmd.action()) {
84
85                 case LFUN_NEWPAGE_INSERT: {
86                         string const name = cmd.getArg(0);
87                         InsetNewpageParams inp;
88                         if (name.empty() || name == "newpage")
89                                 inp.kind = InsetNewpageParams::NEWPAGE;
90                         else if (name == "pagebreak")
91                                 inp.kind = InsetNewpageParams::PAGEBREAK;
92                         else if (name == "clearpage")
93                                 inp.kind = InsetNewpageParams::CLEARPAGE;
94                         else if (name == "cleardoublepage")
95                                 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
96                         return new InsetNewpage(inp);
97                 }
98
99                 case LFUN_FLEX_INSERT: {
100                         string s = cmd.getArg(0);
101                         return new InsetFlex(buf, s);
102                 }
103
104                 case LFUN_NOTE_INSERT: {
105                         string arg = cmd.getArg(0);
106                         if (arg.empty())
107                                 arg = "Note";
108                         return new InsetNote(buf, arg);
109                 }
110
111                 case LFUN_BOX_INSERT: {
112                         string arg = cmd.getArg(0);
113                         if (arg.empty())
114                                 arg = "Boxed";
115                         return new InsetBox(buf, arg);
116                 }
117
118                 case LFUN_BRANCH_INSERT: {
119                         docstring arg = cmd.argument();
120                         if (arg.empty())
121                                 arg = from_ascii("none");
122                         return new InsetBranch(buf, InsetBranchParams(arg));
123                 }
124
125                 case LFUN_PHANTOM_INSERT: {
126                         string arg = cmd.getArg(0);
127                         if (arg.empty())
128                                 arg = "Phantom";
129                         return new InsetPhantom(buf, arg);
130                 }
131
132                 case LFUN_ERT_INSERT:
133                         return new InsetERT(buf);
134
135                 case LFUN_LISTING_INSERT:
136                         return new InsetListings(buf);
137
138                 case LFUN_FOOTNOTE_INSERT:
139                         return new InsetFoot(buf);
140
141                 case LFUN_MARGINALNOTE_INSERT:
142                         return new InsetMarginal(buf);
143
144                 case LFUN_ARGUMENT_INSERT:
145                         return new InsetArgument(buf);
146
147                 case LFUN_FLOAT_INSERT:
148                         return new InsetFloat(buf, to_utf8(cmd.argument()));
149
150                 case LFUN_FLOAT_WIDE_INSERT: {
151                         InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
152                         fl->setWide(true);
153                         return fl;
154                 }
155
156                 case LFUN_WRAP_INSERT: {
157                         string const argument = to_utf8(cmd.argument());
158                         if (argument == "figure" || argument == "table")
159                                 return new InsetWrap(buf, argument);
160                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
161                         return 0;
162                 }
163
164                 case LFUN_INDEX_INSERT: {
165                         docstring arg = cmd.argument();
166                         return new InsetIndex(buf, InsetIndexParams(arg));
167                 }
168
169                 case LFUN_NOMENCL_INSERT: {
170                         InsetCommandParams icp(NOMENCL_CODE);
171                         icp["symbol"] = cmd.argument();
172                         return new InsetNomencl(buf, icp);
173                 }
174
175                 case LFUN_TABULAR_INSERT: {
176                         if (cmd.argument().empty())
177                                 return 0;
178                         istringstream ss(to_utf8(cmd.argument()));
179                         int r = 0, c = 0;
180                         ss >> r >> c;
181                         if (r <= 0)
182                                 r = 2;
183                         if (c <= 0)
184                                 c = 2;
185                         return new InsetTabular(buf, r, c);
186                 }
187
188                 case LFUN_CAPTION_INSERT:
189                         return new InsetCaption(buf);
190
191                 case LFUN_INDEX_PRINT:  {
192                         InsetCommandParams icp(INDEX_PRINT_CODE);
193                         icp["type"] = cmd.argument();
194                         return new InsetPrintIndex(buf, icp);
195                 }
196
197                 case LFUN_NOMENCL_PRINT: {
198                         InsetCommandParams icp(NOMENCL_PRINT_CODE);
199                         icp["set_width"] = from_ascii("auto");
200                         return new InsetPrintNomencl(buf, icp);
201                 }
202
203                 case LFUN_TOC_INSERT:
204                         return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
205
206                 case LFUN_INFO_INSERT: {
207                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
208                         inset->updateInfo();
209                         return inset;
210                 }
211
212                 case LFUN_PREVIEW_INSERT:
213                         return new InsetPreview(buf);
214
215                 case LFUN_INSET_INSERT: {
216                         string const name = cmd.getArg(0);
217                         InsetCode code = insetCode(name);
218                         switch (code) {
219                         case NO_CODE:
220                                 lyxerr << "No such inset '" << name << "'.";
221                                 return 0;
222                         
223                         case BIBITEM_CODE: {
224                                 InsetCommandParams icp(code);
225                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
226                                 return new InsetBibitem(buf, icp);
227                         }
228                         
229                         case BIBTEX_CODE: {
230                                 InsetCommandParams icp(code);
231                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
232                                 return new InsetBibtex(buf, icp);
233                         }
234                         
235                         case CITE_CODE: {
236                                 InsetCommandParams icp(code);
237                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
238                                 return new InsetCitation(buf, icp);
239                         }
240                         
241                         case ERT_CODE: {
242                                 return new InsetERT(buf,
243                                         InsetERT::string2params(to_utf8(cmd.argument())));
244                         }
245                         
246                         case EXTERNAL_CODE: {
247                                 InsetExternalParams iep;
248                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
249                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
250                                 inset->setBuffer(*buf);
251                                 inset->setParams(iep);
252                                 return inset.release();
253                         }
254                         
255                         case GRAPHICS_CODE: {
256                                 InsetGraphicsParams igp;
257                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
258                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
259                                 inset->setParams(igp);
260                                 return inset.release();
261                         }
262                         
263                         case HYPERLINK_CODE: {
264                                 InsetCommandParams icp(code);
265                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
266                                 return new InsetHyperlink(buf, icp);
267                         }
268                         
269                         case INCLUDE_CODE: {
270                                 InsetCommandParams icp(code);
271                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
272                                 return new InsetInclude(buf, icp);
273                         }
274                         
275                         case INDEX_CODE: {
276                                 docstring arg = cmd.argument();
277                                 return new InsetIndex(buf, InsetIndexParams(arg));
278                         }
279                         
280                         case INDEX_PRINT_CODE:  {
281                                 InsetCommandParams icp(code);
282                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
283                                 return new InsetPrintIndex(buf, icp);
284                         }
285                         
286                         case LABEL_CODE: {
287                                 InsetCommandParams icp(code);
288                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
289                                 return new InsetLabel(buf, icp);
290                         }
291                         
292                         case LINE_CODE: {
293                                 InsetCommandParams icp(code);
294                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
295                                 return new InsetLine(buf, icp);
296                         }
297                                 
298                         case LISTINGS_CODE: {
299                                 InsetListingsParams par;
300                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
301                                 return new InsetListings(buf, par);
302                         }
303                         
304                         case NOMENCL_CODE: {
305                                 InsetCommandParams icp(code);
306                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
307                                 return new InsetNomencl(buf, icp);
308                         }
309                         
310                         case REF_CODE: {
311                                 InsetCommandParams icp(code);
312                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
313                                 return new InsetRef(buf, icp);
314                         }
315
316                         case SPACE_CODE: {
317                                 InsetSpaceParams isp;
318                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
319                                 return new InsetSpace(isp);
320                         }
321                         
322                         case TOC_CODE: {
323                                 InsetCommandParams icp(code);
324                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
325                                 return new InsetTOC(buf, icp);
326                         }
327                         
328                         case VSPACE_CODE: {
329                                 VSpace vspace;
330                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
331                                 return new InsetVSpace(vspace);
332                         }
333
334                         case PREVIEW_CODE:
335                                 return new InsetPreview(buf);
336                         
337                         default:
338                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
339                                                 << endl;
340                                 return 0;
341                         
342                         }
343                 } //end LFUN_INSET_INSERT
344
345                 case LFUN_SPACE_INSERT: {
346                         string const name = cmd.getArg(0);
347                         string const len = cmd.getArg(1);
348                         if (name.empty()) {
349                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
350                                 break;
351                         }
352                         InsetSpaceParams isp;
353                         // The tests for isp.math might be disabled after a file format change
354                         if (name == "normal")
355                                 isp.kind = InsetSpaceParams::NORMAL;
356                         else if (name == "protected")
357                                 isp.kind = InsetSpaceParams::PROTECTED;
358                         else if (name == "thin")
359                                 isp.kind = InsetSpaceParams::THIN;
360                         else if (isp.math && name == "med")
361                                 isp.kind = InsetSpaceParams::MEDIUM;
362                         else if (isp.math && name == "thick")
363                                 isp.kind = InsetSpaceParams::THICK;
364                         else if (name == "quad")
365                                 isp.kind = InsetSpaceParams::QUAD;
366                         else if (name == "qquad")
367                                 isp.kind = InsetSpaceParams::QQUAD;
368                         else if (name == "enspace")
369                                 isp.kind = InsetSpaceParams::ENSPACE;
370                         else if (name == "enskip")
371                                 isp.kind = InsetSpaceParams::ENSKIP;
372                         else if (name == "negthinspace")
373                                 isp.kind = InsetSpaceParams::NEGTHIN;
374                         else if (isp.math && name == "negmedspace")
375                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
376                         else if (isp.math && name == "negthickspace")
377                                 isp.kind = InsetSpaceParams::NEGTHICK;
378                         else if (name == "hfill")
379                                 isp.kind = InsetSpaceParams::HFILL;
380                         else if (name == "hfill*")
381                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
382                         else if (name == "dotfill")
383                                 isp.kind = InsetSpaceParams::DOTFILL;
384                         else if (name == "hrulefill")
385                                 isp.kind = InsetSpaceParams::HRULEFILL;
386                         else if (name == "hspace") {
387                                 if (len.empty() || !isValidGlueLength(len)) {
388                                         lyxerr << "LyX function 'space-insert hspace' "
389                                                << "needs a valid length argument." << endl;
390                                         break;
391                                 }
392                                 isp.kind = InsetSpaceParams::CUSTOM;
393                                 isp.length = GlueLength(len);
394                         }
395                         else if (name == "hspace*") {
396                                 if (len.empty() || !isValidGlueLength(len)) {
397                                         lyxerr << "LyX function 'space-insert hspace*' "
398                                                << "needs a valid length argument." << endl;
399                                         break;
400                                 }
401                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
402                                 isp.length = GlueLength(len);
403                         }
404                         else {
405                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
406                                 break;
407                         }
408                         return new InsetSpace(isp);
409                 }
410                 break;
411
412                 default:
413                         break;
414                 }
415
416         } catch (ExceptionMessage const & message) {
417                 if (message.type_ == ErrorException) {
418                         // This should never happen!
419                         Alert::error(message.title_, message.details_);
420                         lyx_exit(1);
421                 } else if (message.type_ == WarningException) {
422                         Alert::warning(message.title_, message.details_);
423                         return 0;
424                 }
425         }
426
427         return 0;
428 }
429
430
431 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
432 {
433         Inset * inset = createInsetHelper(buf, cmd);
434         if (inset)
435                 inset->setBuffer(*buf);
436         return inset;
437 }
438
439
440 Inset * readInset(Lexer & lex, Buffer * buf)
441 {
442         // consistency check
443         if (lex.getString() != "\\begin_inset")
444                 LYXERR0("Buffer::readInset: Consistency check failed.");
445
446         auto_ptr<Inset> inset;
447
448         string tmptok;
449         lex >> tmptok;
450
451         // test the different insets
452         
453         // FIXME It would be better if we did not have this branch and could
454         // just do one massive switch for all insets. But at present, it's
455         // easier to do it this way, and we can't do the massive switch until
456         // the conversion mentioned below.  Note that if we do want to do a
457         // single switch, we need to remove this "CommandInset" line---or
458         // replace it with a single "InsetType" line that would be used in all
459         // insets.
460         if (tmptok == "CommandInset") {
461                 lex.next();
462                 string const insetType = lex.getString();
463                 lex.pushToken(insetType);
464                 
465                 InsetCode const code = insetCode(insetType);
466                 
467                 //FIXME If we do the one massive switch, we cannot do this here, since
468                 //we do not know in advance that we're dealing with a command inset.
469                 //Worst case, we could put it in each case below. Better, we could
470                 //pass the lexer to the constructor and let the params be built there.
471                 InsetCommandParams inscmd(code);
472                 inscmd.read(lex);
473
474                 switch (code) {
475                         case BIBITEM_CODE:
476                                 inset.reset(new InsetBibitem(buf, inscmd));
477                                 break;
478                         case BIBTEX_CODE:
479                                 inset.reset(new InsetBibtex(buf, inscmd));
480                                 break;
481                         case CITE_CODE: 
482                                 inset.reset(new InsetCitation(buf, inscmd));
483                                 break;
484                         case HYPERLINK_CODE:
485                                 inset.reset(new InsetHyperlink(buf, inscmd));
486                                 break;
487                         case INCLUDE_CODE:
488                                 inset.reset(new InsetInclude(buf, inscmd));
489                                 break;
490                         case INDEX_PRINT_CODE:
491                                 inset.reset(new InsetPrintIndex(buf, inscmd));
492                                 break;
493                         case LABEL_CODE:
494                                 inset.reset(new InsetLabel(buf, inscmd));
495                                 break;
496                         case LINE_CODE:
497                                 inset.reset(new InsetLine(buf, inscmd));
498                                 break;
499                         case NOMENCL_CODE:
500                                 inset.reset(new InsetNomencl(buf, inscmd));
501                                 break;
502                         case NOMENCL_PRINT_CODE:
503                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
504                                 break;
505                         case REF_CODE:
506                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
507                                         return 0;
508                                 inset.reset(new InsetRef(buf, inscmd));
509                                 break;
510                         case TOC_CODE:
511                                 inset.reset(new InsetTOC(buf, inscmd));
512                                 break;
513                         case NO_CODE:
514                         default:
515                                 lyxerr << "unknown CommandInset '" << insetType
516                                                         << "'" << endl;
517                                 while (lex.isOK() && lex.getString() != "\\end_inset")
518                                         lex.next();
519                                 return 0;
520                 }
521                 inset->setBuffer(*buf);
522         } else { 
523                 // FIXME This branch should be made to use inset codes as the preceding 
524                 // branch does. Unfortunately, that will take some doing. It requires
525                 // converting the representation of the insets in LyX files so that they
526                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
527                 // can be translated to inset codes using insetCode(). And the insets'
528                 // write() routines should use insetName() rather than hardcoding it.
529                 if (tmptok == "Quotes") {
530                         inset.reset(new InsetQuotes(buf));
531                 } else if (tmptok == "External") {
532                         inset.reset(new InsetExternal(buf));
533                 } else if (tmptok == "FormulaMacro") {
534                         inset.reset(new MathMacroTemplate(buf));
535                 } else if (tmptok == "Formula") {
536                         inset.reset(new InsetMathHull(buf));
537                 } else if (tmptok == "Graphics") {
538                         inset.reset(new InsetGraphics(buf));
539                 } else if (tmptok == "Note") {
540                         inset.reset(new InsetNote(buf, tmptok));
541                 } else if (tmptok == "Box") {
542                         inset.reset(new InsetBox(buf, tmptok));
543                 } else if (tmptok == "Flex") {
544                         lex.eatLine();
545                         string s = lex.getString();
546                         inset.reset(new InsetFlex(buf, s));
547                 } else if (tmptok == "Branch") {
548                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
549                 } else if (tmptok == "Phantom") {
550                         inset.reset(new InsetPhantom(buf, tmptok));
551                 } else if (tmptok == "ERT") {
552                         inset.reset(new InsetERT(buf));
553                 } else if (tmptok == "listings") {
554                         inset.reset(new InsetListings(buf));
555                 } else if (tmptok == "space") {
556                         inset.reset(new InsetSpace);
557                 } else if (tmptok == "Tabular") {
558                         inset.reset(new InsetTabular(buf));
559                 } else if (tmptok == "Text") {
560                         inset.reset(new InsetText(buf));
561                 } else if (tmptok == "VSpace") {
562                         inset.reset(new InsetVSpace);
563                 } else if (tmptok == "Foot") {
564                         inset.reset(new InsetFoot(buf));
565                 } else if (tmptok == "Marginal") {
566                         inset.reset(new InsetMarginal(buf));
567                 } else if (tmptok == "Newpage") {
568                         inset.reset(new InsetNewpage);
569                 } else if (tmptok == "Newline") {
570                         inset.reset(new InsetNewline);
571                 } else if (tmptok == "Argument") {
572                         inset.reset(new InsetArgument(buf));
573                 } else if (tmptok == "Float") {
574                         inset.reset(new InsetFloat(buf, string()));
575                 } else if (tmptok == "Wrap") {
576                         lex.next();
577                         string tmptok = lex.getString();
578                         inset.reset(new InsetWrap(buf, tmptok));
579                 } else if (tmptok == "Caption") {
580                         inset.reset(new InsetCaption(buf));
581                 } else if (tmptok == "Index") {
582                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
583                 } else if (tmptok == "FloatList") {
584                         inset.reset(new InsetFloatList(buf));
585                 } else if (tmptok == "Info") {
586                         inset.reset(new InsetInfo(buf));
587                 } else if (tmptok == "Preview") {
588                         inset.reset(new InsetPreview(buf));
589                 } else {
590                         lyxerr << "unknown Inset type '" << tmptok
591                                << "'" << endl;
592                         while (lex.isOK() && lex.getString() != "\\end_inset")
593                                 lex.next();
594                         return 0;
595                 }
596
597                 // Set the buffer reference for proper parsing of some insets
598                 // (InsetCollapsable for example)
599                 inset->setBuffer(*buf);
600                 inset->read(lex);
601                 // Set again the buffer for insets that are created inside this inset
602                 // (InsetMathHull for example).
603                 inset->setBuffer(*buf);
604         }
605         return inset.release();
606 }
607
608
609 } // namespace lyx