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