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