]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
e5e7353f0da40d93d3a0c12ce142114eeda56b9d
[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/InsetUrl.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"));
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("nomenclature");
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"));
215
216                 case LFUN_NOMENCL_PRINT:
217                         return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
218
219                 case LFUN_TOC_INSERT:
220                         return new InsetTOC(InsetCommandParams("toc"));
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_LIST_INSERT:
229                         return new InsetList;
230
231                 case LFUN_THEOREM_INSERT:
232                         return new InsetTheorem;
233 #endif
234
235                 case LFUN_INSET_INSERT: {
236                         string const name = cmd.getArg(0);
237
238                         if (name == "bibitem") {
239                                 InsetCommandParams icp(name);
240                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
241                                         icp);
242                                 return new InsetBibitem(icp);
243
244                         } else if (name == "bibtex") {
245                                 InsetCommandParams icp(name);
246                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
247                                         icp);
248                                 return new InsetBibtex(icp);
249
250                         } else if (name == "citation") {
251                                 InsetCommandParams icp("citation");
252                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
253                                         icp);
254                                 return new InsetCitation(icp);
255
256                         } else if (name == "ert") {
257                                 InsetCollapsable::CollapseStatus st;
258                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
259                                 return new InsetERT(params, st);
260
261                         } else if (name == "listings") {
262                                 InsetListingsParams par;
263                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
264                                 return new InsetListings(params, par);
265
266                         } else if (name == "external") {
267                                 Buffer const & buffer = bv->buffer();
268                                 InsetExternalParams iep;
269                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
270                                         buffer, iep);
271                                 auto_ptr<InsetExternal> inset(new InsetExternal);
272                                 inset->setParams(iep, buffer);
273                                 return inset.release();
274
275                         } else if (name == "graphics") {
276                                 Buffer const & buffer = bv->buffer();
277                                 InsetGraphicsParams igp;
278                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
279                                         buffer, igp);
280                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
281                                 inset->setParams(igp);
282                                 return inset.release();
283
284                         } else if (name == "include") {
285                                 InsetCommandParams iip(name);
286                                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
287                                 return new InsetInclude(iip);
288
289                         } else if (name == "index") {
290                                 return new InsetIndex(params);
291
292                         } else if (name == "nomenclature") {
293                                 InsetCommandParams icp(name);
294                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
295                                         icp);
296                                 return new InsetNomencl(icp);
297
298                         } else if (name == "label") {
299                                 InsetCommandParams icp(name);
300                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
301                                         icp);
302                                 return new InsetLabel(icp);
303
304                         } else if (name == "ref") {
305                                 InsetCommandParams icp(name);
306                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
307                                         icp);
308                                 return new InsetRef(icp, bv->buffer());
309
310                         } else if (name == "toc") {
311                                 InsetCommandParams icp("toc");
312                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
313                                         icp);
314                                 return new InsetTOC(icp);
315
316                         } else if (name == "url") {
317                                 InsetCommandParams icp(name);
318                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
319                                         icp);
320                                 return new InsetUrl(icp);
321
322                         } else if (name == "vspace") {
323                                 VSpace vspace;
324                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
325                                 return new InsetVSpace(vspace);
326                         }
327                 }
328
329                 case LFUN_SPACE_INSERT: {
330                         string const name = to_utf8(cmd.argument());
331                         if (name == "normal")
332                                 return new InsetSpace(InsetSpace::NORMAL);
333                         else if (name == "protected")
334                                 return new InsetSpace(InsetSpace::PROTECTED);
335                         else if (name == "thin")
336                                 return new InsetSpace(InsetSpace::THIN);
337                         else if (name == "quad")
338                                 return new InsetSpace(InsetSpace::QUAD);
339                         else if (name == "qquad")
340                                 return new InsetSpace(InsetSpace::QQUAD);
341                         else if (name == "enspace")
342                                 return new InsetSpace(InsetSpace::ENSPACE);
343                         else if (name == "enskip")
344                                 return new InsetSpace(InsetSpace::ENSKIP);
345                         else if (name == "negthinspace")
346                                 return new InsetSpace(InsetSpace::NEGTHIN);
347                         else if (name.empty())
348                                 lyxerr << "LyX function 'space' needs an argument." << endl;
349                         else
350                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
351                 }
352                 break;
353
354                 default:
355                         break;
356                 }
357
358         } catch (support::ExceptionMessage const & message) {
359                 if (message.type_ == support::ErrorException) {
360                         Alert::error(message.title_, message.details_);
361                         LyX::cref().emergencyCleanup();
362                         abort();
363                 } else if (message.type_ == support::WarningException) {
364                         Alert::warning(message.title_, message.details_);
365                         return 0;
366                 }
367         }
368
369
370         return 0;
371 }
372
373
374 Inset * readInset(Lexer & lex, Buffer const & buf)
375 {
376         // consistency check
377         if (lex.getString() != "\\begin_inset") {
378                 lyxerr << "Buffer::readInset: Consistency check failed."
379                        << endl;
380         }
381
382         auto_ptr<Inset> inset;
383
384         TextClass tclass = buf.params().getTextClass();
385
386         lex.next();
387         string tmptok = lex.getString();
388
389         // test the different insets
390         if (tmptok == "CommandInset") {
391                 lex.next();
392                 string const insetType = lex.getString();
393                 lex.pushToken(insetType);
394
395                 //FIXME 
396                 //InsetCode const code = Inset::translate(insetType);
397                 //if (code == NO_CODE) { choke as below; }
398                 //InsetCommandParams inscmd();
399                 InsetCommandParams inscmd(insetType);
400                 inscmd.read(lex);
401
402                 if (insetType == "citation") {
403                         inset.reset(new InsetCitation(inscmd));
404                 } else if (insetType == "bibitem") {
405                         inset.reset(new InsetBibitem(inscmd));
406                 } else if (insetType == "bibtex") {
407                         inset.reset(new InsetBibtex(inscmd));
408                 } else if (insetType == "index") {
409                         inset.reset(new InsetIndex(buf.params()));
410                 } else if (insetType == "nomenclature") {
411                         inset.reset(new InsetNomencl(inscmd));
412                 } else if (insetType == "include") {
413                         inset.reset(new InsetInclude(inscmd));
414                 } else if (insetType == "label") {
415                         inset.reset(new InsetLabel(inscmd));
416                 } else if (insetType == "url") {
417                         inset.reset(new InsetUrl(inscmd));
418                 } else if (insetType == "ref") {
419                         if (!inscmd["name"].empty()
420                             || !inscmd["reference"].empty()) {
421                                 inset.reset(new InsetRef(inscmd, buf));
422                         }
423                 } else if (insetType == "toc") {
424                         inset.reset(new InsetTOC(inscmd));
425                 } else if (insetType == "index_print") {
426                         inset.reset(new InsetPrintIndex(inscmd));
427                 } else if (insetType == "nomencl_print") {
428                         inset.reset(new InsetPrintNomencl(inscmd));
429                 } else {
430                         lyxerr << "unknown CommandInset '" << insetType
431                                << "'" << std::endl;
432                         while (lex.isOK() && lex.getString() != "\\end_inset")
433                                 lex.next();
434                         return 0;
435                 }
436         } else {
437                 if (tmptok == "Quotes") {
438                         inset.reset(new InsetQuotes);
439                 } else if (tmptok == "External") {
440                         inset.reset(new InsetExternal);
441                 } else if (tmptok == "FormulaMacro") {
442                         inset.reset(new MathMacroTemplate);
443                 } else if (tmptok == "Formula") {
444                         inset.reset(new InsetMathHull);
445                 } else if (tmptok == "Graphics") {
446                         inset.reset(new InsetGraphics);
447                 } else if (tmptok == "Note") {
448                         inset.reset(new InsetNote(buf.params(), tmptok));
449                 } else if (tmptok == "Box") {
450                         inset.reset(new InsetBox(buf.params(), tmptok));
451                 } else if (tmptok == "Flex") {
452                         lex.next();
453                         string s = lex.getString();
454                         InsetLayout il = tclass.insetlayout(from_utf8(s));
455                         inset.reset(new InsetFlex(buf.params(), il));
456                 } else if (tmptok == "Branch") {
457                         inset.reset(new InsetBranch(buf.params(),
458                                                     InsetBranchParams()));
459                 } else if (tmptok == "Include") {
460                         InsetCommandParams p("include");
461                         inset.reset(new InsetInclude(p));
462                 } else if (tmptok == "Environment") {
463                         lex.next();
464                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
465                 } else if (tmptok == "ERT") {
466                         inset.reset(new InsetERT(buf.params()));
467                 } else if (tmptok == "listings") {
468                         inset.reset(new InsetListings(buf.params()));
469                 } else if (tmptok == "InsetSpace") {
470                         inset.reset(new InsetSpace);
471                 } else if (tmptok == "Tabular") {
472                         inset.reset(new InsetTabular(buf));
473                 } else if (tmptok == "Text") {
474                         inset.reset(new InsetText(buf.params()));
475                 } else if (tmptok == "VSpace") {
476                         inset.reset(new InsetVSpace);
477                 } else if (tmptok == "Foot") {
478                         inset.reset(new InsetFoot(buf.params()));
479                 } else if (tmptok == "Marginal") {
480                         inset.reset(new InsetMarginal(buf.params()));
481                 } else if (tmptok == "OptArg") {
482                         inset.reset(new InsetOptArg(buf.params()));
483                 } else if (tmptok == "Float") {
484                         lex.next();
485                         string tmptok = lex.getString();
486                         inset.reset(new InsetFloat(buf.params(), tmptok));
487                 } else if (tmptok == "Wrap") {
488                         lex.next();
489                         string tmptok = lex.getString();
490                         inset.reset(new InsetWrap(buf.params(), tmptok));
491 #if 0
492                 } else if (tmptok == "List") {
493                         inset.reset(new InsetList);
494                 } else if (tmptok == "Theorem") {
495                         inset.reset(new InsetList);
496 #endif
497                 } else if (tmptok == "Caption") {
498                         inset.reset(new InsetCaption(buf.params()));
499                 } else if (tmptok == "Index") {
500                         inset.reset(new InsetIndex(buf.params()));
501                 } else if (tmptok == "FloatList") {
502                         inset.reset(new InsetFloatList);
503                 } else if (tmptok == "Info") {
504                         inset.reset(new InsetInfo(buf.params()));
505                 } else {
506                         lyxerr << "unknown Inset type '" << tmptok
507                                << "'" << std::endl;
508                         while (lex.isOK() && lex.getString() != "\\end_inset")
509                                 lex.next();
510                         return 0;
511                 }
512
513                 inset->read(buf, lex);
514
515 // FIXME: hack..
516                 if (inset->lyxCode() == MATHMACRO_CODE) {
517                         MathMacroTemplate const * tmpl =
518                                 static_cast<MathMacroTemplate*>(inset.get());
519                         MacroTable::globalMacros().insert
520                                 (tmpl->name(), tmpl->asMacroData());
521                         LYXERR(Debug::DEBUG)
522                                 << BOOST_CURRENT_FUNCTION
523                                 << ": creating local macro " << to_utf8(tmpl->name())
524                                 << endl;
525                 }
526         }
527
528         return inset.release();
529 }
530
531
532 } // namespace lyx