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