]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
f2af52459e0ceb7d9cddf3ebbc7b6bbb9a494461
[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_LINE_INSERT:
86                         return new InsetLine;
87
88                 case LFUN_NEWPAGE_INSERT: {
89                         string const name = cmd.getArg(0);
90                         InsetNewpageParams inp;
91                         if (name.empty() || name == "newpage")
92                                 inp.kind = InsetNewpageParams::NEWPAGE;
93                         else if (name == "pagebreak")
94                                 inp.kind = InsetNewpageParams::PAGEBREAK;
95                         else if (name == "clearpage")
96                                 inp.kind = InsetNewpageParams::CLEARPAGE;
97                         else if (name == "cleardoublepage")
98                                 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
99                         return new InsetNewpage(inp);
100                 }
101
102                 case LFUN_FLEX_INSERT: {
103                         string s = cmd.getArg(0);
104                         return new InsetFlex(buf, 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(buf, 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(buf, 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(buf, InsetBranchParams(arg));
126                 }
127
128                 case LFUN_PHANTOM_INSERT: {
129                         string arg = cmd.getArg(0);
130                         if (arg.empty())
131                                 arg = "Phantom";
132                         return new InsetPhantom(buf, arg);
133                 }
134
135                 case LFUN_ERT_INSERT:
136                         return new InsetERT(buf);
137
138                 case LFUN_LISTING_INSERT:
139                         return new InsetListings(buf);
140
141                 case LFUN_FOOTNOTE_INSERT:
142                         return new InsetFoot(buf);
143
144                 case LFUN_MARGINALNOTE_INSERT:
145                         return new InsetMarginal(buf);
146
147                 case LFUN_OPTIONAL_INSERT:
148                         return new InsetArgument(buf);
149
150                 case LFUN_FLOAT_INSERT:
151                         return new InsetFloat(buf, to_utf8(cmd.argument()));
152
153                 case LFUN_FLOAT_WIDE_INSERT: {
154                         InsetFloat * fl = new InsetFloat(buf, to_utf8(cmd.argument()));
155                         fl->setWide(true);
156                         return fl;
157                 }
158
159                 case LFUN_WRAP_INSERT: {
160                         string const argument = to_utf8(cmd.argument());
161                         if (argument == "figure" || argument == "table")
162                                 return new InsetWrap(buf, argument);
163                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
164                         return 0;
165                 }
166
167                 case LFUN_INDEX_INSERT: {
168                         docstring arg = cmd.argument();
169                         return new InsetIndex(buf, InsetIndexParams(arg));
170                 }
171
172                 case LFUN_NOMENCL_INSERT: {
173                         InsetCommandParams icp(NOMENCL_CODE);
174                         icp["symbol"] = cmd.argument();
175                         return new InsetNomencl(buf, icp);
176                 }
177
178                 case LFUN_TABULAR_INSERT: {
179                         if (cmd.argument().empty())
180                                 return 0;
181                         istringstream ss(to_utf8(cmd.argument()));
182                         int r = 0, c = 0;
183                         ss >> r >> c;
184                         if (r <= 0)
185                                 r = 2;
186                         if (c <= 0)
187                                 c = 2;
188                         return new InsetTabular(buf, r, c);
189                 }
190
191                 case LFUN_CAPTION_INSERT:
192                         return new InsetCaption(buf);
193
194                 case LFUN_INDEX_PRINT:  {
195                         InsetCommandParams icp(INDEX_PRINT_CODE);
196                         icp["type"] = cmd.argument();
197                         return new InsetPrintIndex(buf, icp);
198                 }
199
200                 case LFUN_NOMENCL_PRINT: {
201                         InsetCommandParams icp(NOMENCL_PRINT_CODE);
202                         icp["set_width"] = from_ascii("auto");
203                         return new InsetPrintNomencl(buf, icp);
204                 }
205
206                 case LFUN_TOC_INSERT:
207                         return new InsetTOC(buf, InsetCommandParams(TOC_CODE));
208
209                 case LFUN_INFO_INSERT: {
210                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
211                         inset->updateInfo();
212                         return inset;
213                 }
214
215                 case LFUN_PREVIEW_INSERT:
216                         return new InsetPreview(buf);
217
218                 case LFUN_INSET_INSERT: {
219                         string const name = cmd.getArg(0);
220                         InsetCode code = insetCode(name);
221                         switch (code) {
222                         case NO_CODE:
223                                 lyxerr << "No such inset '" << name << "'.";
224                                 return 0;
225                         
226                         case BIBITEM_CODE: {
227                                 InsetCommandParams icp(code);
228                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
229                                 return new InsetBibitem(buf, icp);
230                         }
231                         
232                         case BIBTEX_CODE: {
233                                 InsetCommandParams icp(code);
234                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
235                                 return new InsetBibtex(buf, icp);
236                         }
237                         
238                         case CITE_CODE: {
239                                 InsetCommandParams icp(code);
240                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
241                                 return new InsetCitation(buf, icp);
242                         }
243                         
244                         case ERT_CODE: {
245                                 return new InsetERT(buf,
246                                         InsetERT::string2params(to_utf8(cmd.argument())));
247                         }
248                                 
249                         case LISTINGS_CODE: {
250                                 InsetListingsParams par;
251                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
252                                 return new InsetListings(buf, par);
253                         }
254                         
255                         case EXTERNAL_CODE: {
256                                 InsetExternalParams iep;
257                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
258                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
259                                 inset->setBuffer(*buf);
260                                 inset->setParams(iep);
261                                 return inset.release();
262                         }
263                         
264                         case GRAPHICS_CODE: {
265                                 InsetGraphicsParams igp;
266                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
267                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
268                                 inset->setParams(igp);
269                                 return inset.release();
270                         }
271                         
272                         case HYPERLINK_CODE: {
273                                 InsetCommandParams icp(code);
274                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
275                                 return new InsetHyperlink(buf, icp);
276                         }
277                         
278                         case INCLUDE_CODE: {
279                                 InsetCommandParams icp(code);
280                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
281                                 return new InsetInclude(buf, icp);
282                         }
283                         
284                         case INDEX_CODE: {
285                                 docstring arg = cmd.argument();
286                                 return new InsetIndex(buf, InsetIndexParams(arg));
287                         }
288                         
289                         case INDEX_PRINT_CODE:  {
290                                 InsetCommandParams icp(code);
291                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
292                                 return new InsetPrintIndex(buf, icp);
293                         }
294                         
295                         case NOMENCL_CODE: {
296                                 InsetCommandParams icp(code);
297                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
298                                 return new InsetNomencl(buf, icp);
299                         }
300                         
301                         case LABEL_CODE: {
302                                 InsetCommandParams icp(code);
303                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
304                                 return new InsetLabel(buf, icp);
305                         }
306                         
307                         case REF_CODE: {
308                                 InsetCommandParams icp(code);
309                                 InsetCommand::string2params(name, 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(name, 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 NOMENCL_CODE:
494                                 inset.reset(new InsetNomencl(buf, inscmd));
495                                 break;
496                         case NOMENCL_PRINT_CODE:
497                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
498                                 break;
499                         case REF_CODE:
500                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
501                                         return 0;
502                                 inset.reset(new InsetRef(buf, inscmd));
503                                 break;
504                         case TOC_CODE:
505                                 inset.reset(new InsetTOC(buf, inscmd));
506                                 break;
507                         case NO_CODE:
508                         default:
509                                 lyxerr << "unknown CommandInset '" << insetType
510                                                         << "'" << endl;
511                                 while (lex.isOK() && lex.getString() != "\\end_inset")
512                                         lex.next();
513                                 return 0;
514                 }
515                 inset->setBuffer(*buf);
516         } else { 
517                 // FIXME This branch should be made to use inset codes as the preceding 
518                 // branch does. Unfortunately, that will take some doing. It requires
519                 // converting the representation of the insets in LyX files so that they
520                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
521                 // can be translated to inset codes using insetCode(). And the insets'
522                 // write() routines should use insetName() rather than hardcoding it.
523                 if (tmptok == "Quotes") {
524                         inset.reset(new InsetQuotes(buf));
525                 } else if (tmptok == "External") {
526                         inset.reset(new InsetExternal(buf));
527                 } else if (tmptok == "FormulaMacro") {
528                         inset.reset(new MathMacroTemplate(buf));
529                 } else if (tmptok == "Formula") {
530                         inset.reset(new InsetMathHull(buf));
531                 } else if (tmptok == "Graphics") {
532                         inset.reset(new InsetGraphics(buf));
533                 } else if (tmptok == "Note") {
534                         inset.reset(new InsetNote(buf, tmptok));
535                 } else if (tmptok == "Box") {
536                         inset.reset(new InsetBox(buf, tmptok));
537                 } else if (tmptok == "Flex") {
538                         lex.eatLine();
539                         string s = lex.getString();
540                         inset.reset(new InsetFlex(buf, s));
541                 } else if (tmptok == "Branch") {
542                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
543                 } else if (tmptok == "Phantom") {
544                         inset.reset(new InsetPhantom(buf, tmptok));
545                 } else if (tmptok == "ERT") {
546                         inset.reset(new InsetERT(buf));
547                 } else if (tmptok == "listings") {
548                         inset.reset(new InsetListings(buf));
549                 } else if (tmptok == "space") {
550                         inset.reset(new InsetSpace);
551                 } else if (tmptok == "Tabular") {
552                         inset.reset(new InsetTabular(buf));
553                 } else if (tmptok == "Text") {
554                         inset.reset(new InsetText(buf));
555                 } else if (tmptok == "VSpace") {
556                         inset.reset(new InsetVSpace);
557                 } else if (tmptok == "Foot") {
558                         inset.reset(new InsetFoot(buf));
559                 } else if (tmptok == "Marginal") {
560                         inset.reset(new InsetMarginal(buf));
561                 } else if (tmptok == "Newpage") {
562                         inset.reset(new InsetNewpage);
563                 } else if (tmptok == "Newline") {
564                         inset.reset(new InsetNewline);
565                 } else if (tmptok == "OptArg") {
566                         inset.reset(new InsetArgument(buf));
567                 } else if (tmptok == "Float") {
568                         inset.reset(new InsetFloat(buf, string()));
569                 } else if (tmptok == "Wrap") {
570                         lex.next();
571                         string tmptok = lex.getString();
572                         inset.reset(new InsetWrap(buf, tmptok));
573                 } else if (tmptok == "Caption") {
574                         inset.reset(new InsetCaption(buf));
575                 } else if (tmptok == "Index") {
576                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
577                 } else if (tmptok == "FloatList") {
578                         inset.reset(new InsetFloatList(buf));
579                 } else if (tmptok == "Info") {
580                         inset.reset(new InsetInfo(buf));
581                 } else if (tmptok == "Preview") {
582                         inset.reset(new InsetPreview(buf));
583                 } else {
584                         lyxerr << "unknown Inset type '" << tmptok
585                                << "'" << endl;
586                         while (lex.isOK() && lex.getString() != "\\end_inset")
587                                 lex.next();
588                         return 0;
589                 }
590
591                 // Set the buffer reference for proper parsing of some insets
592                 // (InsetCollapsable for example)
593                 inset->setBuffer(*buf);
594                 inset->read(lex);
595                 // Set again the buffer for insets that are created inside this inset
596                 // (InsetMathHull for example).
597                 inset->setBuffer(*buf);
598         }
599         return inset.release();
600 }
601
602
603 } // namespace lyx