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