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