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