]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Add comment.
[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_INFO_INSERT: {
204                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
205                         inset->updateInfo();
206                         return inset;
207                 }
208
209                 case LFUN_PREVIEW_INSERT:
210                         return new InsetPreview(buf);
211
212                 case LFUN_INSET_INSERT: {
213                         string const name = cmd.getArg(0);
214                         InsetCode code = insetCode(name);
215                         switch (code) {
216                         case NO_CODE:
217                                 lyxerr << "No such inset '" << name << "'.";
218                                 return 0;
219                         
220                         case BIBITEM_CODE: {
221                                 InsetCommandParams icp(code);
222                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
223                                 return new InsetBibitem(buf, icp);
224                         }
225                         
226                         case BIBTEX_CODE: {
227                                 InsetCommandParams icp(code);
228                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
229                                 return new InsetBibtex(buf, icp);
230                         }
231                         
232                         case CITE_CODE: {
233                                 InsetCommandParams icp(code);
234                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
235                                 return new InsetCitation(buf, icp);
236                         }
237                         
238                         case ERT_CODE: {
239                                 return new InsetERT(buf,
240                                         InsetERT::string2params(to_utf8(cmd.argument())));
241                         }
242                         
243                         case EXTERNAL_CODE: {
244                                 InsetExternalParams iep;
245                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
246                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
247                                 inset->setBuffer(*buf);
248                                 inset->setParams(iep);
249                                 return inset.release();
250                         }
251                         
252                         case GRAPHICS_CODE: {
253                                 InsetGraphicsParams igp;
254                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
255                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
256                                 inset->setParams(igp);
257                                 return inset.release();
258                         }
259                         
260                         case HYPERLINK_CODE: {
261                                 InsetCommandParams icp(code);
262                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
263                                 return new InsetHyperlink(buf, icp);
264                         }
265                         
266                         case INCLUDE_CODE: {
267                                 InsetCommandParams icp(code);
268                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
269                                 return new InsetInclude(buf, icp);
270                         }
271                         
272                         case INDEX_CODE: {
273                                 docstring arg = cmd.argument();
274                                 return new InsetIndex(buf, InsetIndexParams(arg));
275                         }
276                         
277                         case INDEX_PRINT_CODE:  {
278                                 InsetCommandParams icp(code);
279                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
280                                 return new InsetPrintIndex(buf, icp);
281                         }
282                         
283                         case LABEL_CODE: {
284                                 InsetCommandParams icp(code);
285                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
286                                 return new InsetLabel(buf, icp);
287                         }
288                         
289                         case LINE_CODE: {
290                                 InsetCommandParams icp(code);
291                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
292                                 return new InsetLine(buf, icp);
293                         }
294                                 
295                         case LISTINGS_CODE: {
296                                 InsetListingsParams par;
297                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
298                                 return new InsetListings(buf, par);
299                         }
300                         
301                         case NOMENCL_CODE: {
302                                 InsetCommandParams icp(code);
303                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
304                                 return new InsetNomencl(buf, icp);
305                         }
306                         
307                         case REF_CODE: {
308                                 InsetCommandParams icp(code);
309                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
310                                 return new InsetRef(buf, icp);
311                         }
312
313                         case SPACE_CODE: {
314                                 InsetSpaceParams isp;
315                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
316                                 return new InsetSpace(isp);
317                         }
318                         
319                         case TOC_CODE: {
320                                 InsetCommandParams icp(code);
321                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
322                                 return new InsetTOC(buf, icp);
323                         }
324                         
325                         case VSPACE_CODE: {
326                                 VSpace vspace;
327                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
328                                 return new InsetVSpace(vspace);
329                         }
330
331                         case PREVIEW_CODE:
332                                 return new InsetPreview(buf);
333                         
334                         default:
335                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
336                                                 << endl;
337                                 return 0;
338                         
339                         }
340                 } //end LFUN_INSET_INSERT
341
342                 case LFUN_SPACE_INSERT: {
343                         string const name = cmd.getArg(0);
344                         string const len = cmd.getArg(1);
345                         if (name.empty()) {
346                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
347                                 break;
348                         }
349                         InsetSpaceParams isp;
350                         // The tests for isp.math might be disabled after a file format change
351                         if (name == "normal")
352                                 isp.kind = InsetSpaceParams::NORMAL;
353                         else if (name == "protected")
354                                 isp.kind = InsetSpaceParams::PROTECTED;
355                         else if (name == "thin")
356                                 isp.kind = InsetSpaceParams::THIN;
357                         else if (isp.math && name == "med")
358                                 isp.kind = InsetSpaceParams::MEDIUM;
359                         else if (isp.math && name == "thick")
360                                 isp.kind = InsetSpaceParams::THICK;
361                         else if (name == "quad")
362                                 isp.kind = InsetSpaceParams::QUAD;
363                         else if (name == "qquad")
364                                 isp.kind = InsetSpaceParams::QQUAD;
365                         else if (name == "enspace")
366                                 isp.kind = InsetSpaceParams::ENSPACE;
367                         else if (name == "enskip")
368                                 isp.kind = InsetSpaceParams::ENSKIP;
369                         else if (name == "negthinspace")
370                                 isp.kind = InsetSpaceParams::NEGTHIN;
371                         else if (isp.math && name == "negmedspace")
372                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
373                         else if (isp.math && name == "negthickspace")
374                                 isp.kind = InsetSpaceParams::NEGTHICK;
375                         else if (name == "hfill")
376                                 isp.kind = InsetSpaceParams::HFILL;
377                         else if (name == "hfill*")
378                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
379                         else if (name == "dotfill")
380                                 isp.kind = InsetSpaceParams::DOTFILL;
381                         else if (name == "hrulefill")
382                                 isp.kind = InsetSpaceParams::HRULEFILL;
383                         else if (name == "hspace") {
384                                 if (len.empty() || !isValidGlueLength(len)) {
385                                         lyxerr << "LyX function 'space-insert hspace' "
386                                                << "needs a valid length argument." << endl;
387                                         break;
388                                 }
389                                 isp.kind = InsetSpaceParams::CUSTOM;
390                                 isp.length = GlueLength(len);
391                         }
392                         else if (name == "hspace*") {
393                                 if (len.empty() || !isValidGlueLength(len)) {
394                                         lyxerr << "LyX function 'space-insert hspace*' "
395                                                << "needs a valid length argument." << endl;
396                                         break;
397                                 }
398                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
399                                 isp.length = GlueLength(len);
400                         }
401                         else {
402                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
403                                 break;
404                         }
405                         return new InsetSpace(isp);
406                 }
407                 break;
408
409                 default:
410                         break;
411                 }
412
413         } catch (ExceptionMessage const & message) {
414                 if (message.type_ == ErrorException) {
415                         // This should never happen!
416                         Alert::error(message.title_, message.details_);
417                         lyx_exit(1);
418                 } else if (message.type_ == WarningException) {
419                         Alert::warning(message.title_, message.details_);
420                         return 0;
421                 }
422         }
423
424         return 0;
425 }
426
427
428 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
429 {
430         Inset * inset = createInsetHelper(buf, cmd);
431         if (inset)
432                 inset->setBuffer(*buf);
433         return inset;
434 }
435
436
437 Inset * readInset(Lexer & lex, Buffer * buf)
438 {
439         // consistency check
440         if (lex.getString() != "\\begin_inset")
441                 LYXERR0("Buffer::readInset: Consistency check failed.");
442
443         auto_ptr<Inset> inset;
444
445         string tmptok;
446         lex >> tmptok;
447
448         // test the different insets
449         
450         // FIXME It would be better if we did not have this branch and could
451         // just do one massive switch for all insets. But at present, it's
452         // easier to do it this way, and we can't do the massive switch until
453         // the conversion mentioned below.  Note that if we do want to do a
454         // single switch, we need to remove this "CommandInset" line---or
455         // replace it with a single "InsetType" line that would be used in all
456         // insets.
457         if (tmptok == "CommandInset") {
458                 lex.next();
459                 string const insetType = lex.getString();
460                 lex.pushToken(insetType);
461                 
462                 InsetCode const code = insetCode(insetType);
463                 
464                 //FIXME If we do the one massive switch, we cannot do this here, since
465                 //we do not know in advance that we're dealing with a command inset.
466                 //Worst case, we could put it in each case below. Better, we could
467                 //pass the lexer to the constructor and let the params be built there.
468                 InsetCommandParams inscmd(code);
469                 inscmd.read(lex);
470
471                 switch (code) {
472                         case BIBITEM_CODE:
473                                 inset.reset(new InsetBibitem(buf, inscmd));
474                                 break;
475                         case BIBTEX_CODE:
476                                 inset.reset(new InsetBibtex(buf, inscmd));
477                                 break;
478                         case CITE_CODE: 
479                                 inset.reset(new InsetCitation(buf, inscmd));
480                                 break;
481                         case HYPERLINK_CODE:
482                                 inset.reset(new InsetHyperlink(buf, inscmd));
483                                 break;
484                         case INCLUDE_CODE:
485                                 inset.reset(new InsetInclude(buf, inscmd));
486                                 break;
487                         case INDEX_PRINT_CODE:
488                                 inset.reset(new InsetPrintIndex(buf, inscmd));
489                                 break;
490                         case LABEL_CODE:
491                                 inset.reset(new InsetLabel(buf, inscmd));
492                                 break;
493                         case LINE_CODE:
494                                 inset.reset(new InsetLine(buf, inscmd));
495                                 break;
496                         case NOMENCL_CODE:
497                                 inset.reset(new InsetNomencl(buf, inscmd));
498                                 break;
499                         case NOMENCL_PRINT_CODE:
500                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
501                                 break;
502                         case REF_CODE:
503                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
504                                         return 0;
505                                 inset.reset(new InsetRef(buf, inscmd));
506                                 break;
507                         case TOC_CODE:
508                                 inset.reset(new InsetTOC(buf, inscmd));
509                                 break;
510                         case NO_CODE:
511                         default:
512                                 lyxerr << "unknown CommandInset '" << insetType
513                                                         << "'" << endl;
514                                 while (lex.isOK() && lex.getString() != "\\end_inset")
515                                         lex.next();
516                                 return 0;
517                 }
518                 inset->setBuffer(*buf);
519         } else { 
520                 // FIXME This branch should be made to use inset codes as the preceding 
521                 // branch does. Unfortunately, that will take some doing. It requires
522                 // converting the representation of the insets in LyX files so that they
523                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
524                 // can be translated to inset codes using insetCode(). And the insets'
525                 // write() routines should use insetName() rather than hardcoding it.
526                 if (tmptok == "Quotes") {
527                         inset.reset(new InsetQuotes(buf));
528                 } else if (tmptok == "External") {
529                         inset.reset(new InsetExternal(buf));
530                 } else if (tmptok == "FormulaMacro") {
531                         inset.reset(new MathMacroTemplate(buf));
532                 } else if (tmptok == "Formula") {
533                         inset.reset(new InsetMathHull(buf));
534                 } else if (tmptok == "Graphics") {
535                         inset.reset(new InsetGraphics(buf));
536                 } else if (tmptok == "Note") {
537                         inset.reset(new InsetNote(buf, tmptok));
538                 } else if (tmptok == "Box") {
539                         inset.reset(new InsetBox(buf, tmptok));
540                 } else if (tmptok == "Flex") {
541                         lex.eatLine();
542                         string s = lex.getString();
543                         inset.reset(new InsetFlex(buf, s));
544                 } else if (tmptok == "Branch") {
545                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
546                 } else if (tmptok == "Phantom") {
547                         inset.reset(new InsetPhantom(buf, tmptok));
548                 } else if (tmptok == "ERT") {
549                         inset.reset(new InsetERT(buf));
550                 } else if (tmptok == "listings") {
551                         inset.reset(new InsetListings(buf));
552                 } else if (tmptok == "space") {
553                         inset.reset(new InsetSpace);
554                 } else if (tmptok == "Tabular") {
555                         inset.reset(new InsetTabular(buf));
556                 } else if (tmptok == "Text") {
557                         inset.reset(new InsetText(buf));
558                 } else if (tmptok == "VSpace") {
559                         inset.reset(new InsetVSpace);
560                 } else if (tmptok == "Foot") {
561                         inset.reset(new InsetFoot(buf));
562                 } else if (tmptok == "Marginal") {
563                         inset.reset(new InsetMarginal(buf));
564                 } else if (tmptok == "Newpage") {
565                         inset.reset(new InsetNewpage);
566                 } else if (tmptok == "Newline") {
567                         inset.reset(new InsetNewline);
568                 } else if (tmptok == "Argument") {
569                         inset.reset(new InsetArgument(buf));
570                 } else if (tmptok == "Float") {
571                         inset.reset(new InsetFloat(buf, string()));
572                 } else if (tmptok == "Wrap") {
573                         lex.next();
574                         string tmptok = lex.getString();
575                         inset.reset(new InsetWrap(buf, tmptok));
576                 } else if (tmptok == "Caption") {
577                         inset.reset(new InsetCaption(buf));
578                 } else if (tmptok == "Index") {
579                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
580                 } else if (tmptok == "FloatList") {
581                         inset.reset(new InsetFloatList(buf));
582                 } else if (tmptok == "Info") {
583                         inset.reset(new InsetInfo(buf));
584                 } else if (tmptok == "Preview") {
585                         inset.reset(new InsetPreview(buf));
586                 } else {
587                         lyxerr << "unknown Inset type '" << tmptok
588                                << "'" << endl;
589                         while (lex.isOK() && lex.getString() != "\\end_inset")
590                                 lex.next();
591                         return 0;
592                 }
593
594                 // Set the buffer reference for proper parsing of some insets
595                 // (InsetCollapsable for example)
596                 inset->setBuffer(*buf);
597                 inset->read(lex);
598                 // Set again the buffer for insets that are created inside this inset
599                 // (InsetMathHull for example).
600                 inset->setBuffer(*buf);
601         }
602         return inset.release();
603 }
604
605
606 } // namespace lyx