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