]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
last commit was incomplete... not sure how I managed this..
[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/InsetNomencl.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPagebreak.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetUrl.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/lstrings.h"
64 #include "support/ExceptionMessage.h"
65
66 #include <boost/assert.hpp>
67 #include <boost/current_function.hpp>
68
69 #include <sstream>
70
71 using std::auto_ptr;
72 using std::endl;
73 using std::string;
74
75
76 namespace lyx {
77
78 namespace Alert = frontend::Alert;
79
80 using support::compare_ascii_no_case;
81
82
83 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
84 {
85         BufferParams const & params = bv->buffer().params();
86
87         try {
88
89                 switch (cmd.action) {
90                 case LFUN_HFILL_INSERT:
91                         return new InsetHFill;
92
93                 case LFUN_LINE_INSERT:
94                         return new InsetLine;
95
96                 case LFUN_PAGEBREAK_INSERT:
97                         return new InsetPagebreak;
98
99                 case LFUN_CLEARPAGE_INSERT:
100                         return new InsetClearPage;
101
102                 case LFUN_CLEARDOUBLEPAGE_INSERT:
103                         return new InsetClearDoublePage;
104
105                 case LFUN_FLEX_INSERT: {
106                         string s = cmd.getArg(0);
107                         TextClass tclass = params.getTextClass();
108                         InsetLayout il = tclass.insetlayout(from_utf8(s));
109                         return new InsetFlex(params, il);
110                 }
111
112                 case LFUN_NOTE_INSERT: {
113                         string arg = cmd.getArg(0);
114                         if (arg.empty())
115                                 arg = "Note";
116                         return new InsetNote(params, arg);
117                 }
118
119                 case LFUN_BOX_INSERT: {
120                         string arg = cmd.getArg(0);
121                         if (arg.empty())
122                                 arg = "Boxed";
123                         return new InsetBox(params, arg);
124                 }
125
126                 case LFUN_BRANCH_INSERT: {
127                         docstring arg = cmd.argument();
128                         if (arg.empty())
129                                 arg = from_ascii("none");
130                         return new InsetBranch(params, InsetBranchParams(arg));
131                 }
132
133                 case LFUN_ERT_INSERT:
134                         return new InsetERT(params);
135
136                 case LFUN_LISTING_INSERT:
137                         return new InsetListings(params);
138
139                 case LFUN_FOOTNOTE_INSERT:
140                         return new InsetFoot(params);
141
142                 case LFUN_MARGINALNOTE_INSERT:
143                         return new InsetMarginal(params);
144
145                 case LFUN_OPTIONAL_INSERT:
146                         return new InsetOptArg(params);
147
148                 case LFUN_BIBITEM_INSERT:
149                         return new InsetBibitem(InsetCommandParams("bibitem"));
150
151                 case LFUN_FLOAT_INSERT: {
152                         // check if the float type exists
153                         string const argument = to_utf8(cmd.argument());
154                         if (params.getTextClass().floats().typeExist(argument))
155                                 return new InsetFloat(params, argument);
156                         lyxerr << "Non-existent float type: " << argument << endl;
157                         return 0;
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.getTextClass().floats().typeExist(argument)) {
164                                 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
165                                 p->wide(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")
175                                 return new InsetWrap(params, argument);
176                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
177                         return 0;
178                 }
179
180                 case LFUN_INDEX_INSERT:
181                         return new InsetIndex(params);
182
183                 case LFUN_NOMENCL_INSERT: {
184                         InsetCommandParams icp("nomenclature");
185                         icp["symbol"] = cmd.argument().empty() ?
186                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
187                                 cmd.argument();
188                         return new InsetNomencl(icp);
189                 }
190
191                 case LFUN_TABULAR_INSERT: {
192                         if (cmd.argument().empty())
193                                 return 0;
194                         std::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(bv->buffer(), r, c);
202                 }
203
204                 case LFUN_CAPTION_INSERT: {
205                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
206                         inset->setAutoBreakRows(true);
207                         inset->setDrawFrame(true);
208                         inset->setFrameColor(Color::captionframe);
209                         return inset.release();
210                 }
211
212                 case LFUN_INDEX_PRINT:
213                         return new InsetPrintIndex(InsetCommandParams("index_print"));
214
215                 case LFUN_NOMENCL_PRINT:
216                         return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
217
218                 case LFUN_TOC_INSERT:
219                         return new InsetTOC(InsetCommandParams("toc"));
220
221                 case LFUN_ENVIRONMENT_INSERT:
222                         return new InsetEnvironment(params, cmd.argument());
223
224 #if 0
225                 case LFUN_LIST_INSERT:
226                         return new InsetList;
227
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
235                         if (name == "bibitem") {
236                                 InsetCommandParams icp(name);
237                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
238                                         icp);
239                                 return new InsetBibitem(icp);
240
241                         } else if (name == "bibtex") {
242                                 InsetCommandParams icp(name);
243                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
244                                         icp);
245                                 return new InsetBibtex(icp);
246
247                         } else if (name == "citation") {
248                                 InsetCommandParams icp("citation");
249                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
250                                         icp);
251                                 return new InsetCitation(icp);
252
253                         } else if (name == "ert") {
254                                 InsetCollapsable::CollapseStatus st;
255                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
256                                 return new InsetERT(params, st);
257
258                         } else if (name == "listings") {
259                                 InsetListingsParams par;
260                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
261                                 return new InsetListings(params, par);
262
263                         } else if (name == "external") {
264                                 Buffer const & buffer = bv->buffer();
265                                 InsetExternalParams iep;
266                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
267                                         buffer, iep);
268                                 auto_ptr<InsetExternal> inset(new InsetExternal);
269                                 inset->setParams(iep, buffer);
270                                 return inset.release();
271
272                         } else if (name == "graphics") {
273                                 Buffer const & buffer = bv->buffer();
274                                 InsetGraphicsParams igp;
275                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
276                                         buffer, igp);
277                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
278                                 inset->setParams(igp);
279                                 return inset.release();
280
281                         } else if (name == "include") {
282                                 InsetCommandParams iip(name);
283                                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
284                                 return new InsetInclude(iip);
285
286                         } else if (name == "index") {
287                                 return new InsetIndex(params);
288
289                         } else if (name == "nomenclature") {
290                                 InsetCommandParams icp(name);
291                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
292                                         icp);
293                                 return new InsetNomencl(icp);
294
295                         } else if (name == "label") {
296                                 InsetCommandParams icp(name);
297                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
298                                         icp);
299                                 return new InsetLabel(icp);
300
301                         } else if (name == "ref") {
302                                 InsetCommandParams icp(name);
303                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
304                                         icp);
305                                 return new InsetRef(icp, bv->buffer());
306
307                         } else if (name == "toc") {
308                                 InsetCommandParams icp("toc");
309                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
310                                         icp);
311                                 return new InsetTOC(icp);
312
313                         } else if (name == "url") {
314                                 InsetCommandParams icp(name);
315                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
316                                         icp);
317                                 return new InsetUrl(icp);
318
319                         } else if (name == "vspace") {
320                                 VSpace vspace;
321                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
322                                 return new InsetVSpace(vspace);
323                         }
324                 }
325
326                 case LFUN_SPACE_INSERT: {
327                         string const name = to_utf8(cmd.argument());
328                         if (name == "normal")
329                                 return new InsetSpace(InsetSpace::NORMAL);
330                         else if (name == "protected")
331                                 return new InsetSpace(InsetSpace::PROTECTED);
332                         else if (name == "thin")
333                                 return new InsetSpace(InsetSpace::THIN);
334                         else if (name == "quad")
335                                 return new InsetSpace(InsetSpace::QUAD);
336                         else if (name == "qquad")
337                                 return new InsetSpace(InsetSpace::QQUAD);
338                         else if (name == "enspace")
339                                 return new InsetSpace(InsetSpace::ENSPACE);
340                         else if (name == "enskip")
341                                 return new InsetSpace(InsetSpace::ENSKIP);
342                         else if (name == "negthinspace")
343                                 return new InsetSpace(InsetSpace::NEGTHIN);
344                         else if (name.empty())
345                                 lyxerr << "LyX function 'space' needs an argument." << endl;
346                         else
347                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
348                 }
349                 break;
350
351                 default:
352                         break;
353                 }
354
355         } catch (support::ExceptionMessage const & message) {
356                 if (message.type_ == support::ErrorException) {
357                         Alert::error(message.title_, message.details_);
358                         LyX::cref().emergencyCleanup();
359                         abort();
360                 } else if (message.type_ == support::WarningException) {
361                         Alert::warning(message.title_, message.details_);
362                         return 0;
363                 }
364         }
365
366
367         return 0;
368 }
369
370
371 Inset * readInset(Lexer & lex, Buffer const & buf)
372 {
373         // consistency check
374         if (lex.getString() != "\\begin_inset") {
375                 lyxerr << "Buffer::readInset: Consistency check failed."
376                        << endl;
377         }
378
379         auto_ptr<Inset> inset;
380
381         TextClass tclass = buf.params().getTextClass();
382
383         lex.next();
384         string tmptok = lex.getString();
385
386         // test the different insets
387         if (tmptok == "CommandInset") {
388                 lex.next();
389                 string const insetType = lex.getString();
390                 lex.pushToken(insetType);
391
392                 //FIXME 
393                 //Inset::Code const code = Inset::translate(insetType);
394                 //if (code == Inset::NO_CODE) { choke as below; }
395                 //InsetCommandParams inscmd();
396                 InsetCommandParams inscmd(insetType);
397                 inscmd.read(lex);
398
399                 if (insetType == "citation") {
400                         inset.reset(new InsetCitation(inscmd));
401                 } else if (insetType == "bibitem") {
402                         inset.reset(new InsetBibitem(inscmd));
403                 } else if (insetType == "bibtex") {
404                         inset.reset(new InsetBibtex(inscmd));
405                 } else if (insetType == "index") {
406                         inset.reset(new InsetIndex(buf.params()));
407                 } else if (insetType == "nomenclature") {
408                         inset.reset(new InsetNomencl(inscmd));
409                 } else if (insetType == "include") {
410                         inset.reset(new InsetInclude(inscmd));
411                 } else if (insetType == "label") {
412                         inset.reset(new InsetLabel(inscmd));
413                 } else if (insetType == "url") {
414                         inset.reset(new InsetUrl(inscmd));
415                 } else if (insetType == "ref") {
416                         if (!inscmd["name"].empty()
417                             || !inscmd["reference"].empty()) {
418                                 inset.reset(new InsetRef(inscmd, buf));
419                         }
420                 } else if (insetType == "toc") {
421                         inset.reset(new InsetTOC(inscmd));
422                 } else if (insetType == "index_print") {
423                         inset.reset(new InsetPrintIndex(inscmd));
424                 } else if (insetType == "nomencl_print") {
425                         inset.reset(new InsetPrintNomencl(inscmd));
426                 } else {
427                         lyxerr << "unknown CommandInset '" << insetType
428                                << "'" << std::endl;
429                         while (lex.isOK() && lex.getString() != "\\end_inset")
430                                 lex.next();
431                         return 0;
432                 }
433         } else {
434                 if (tmptok == "Quotes") {
435                         inset.reset(new InsetQuotes);
436                 } else if (tmptok == "External") {
437                         inset.reset(new InsetExternal);
438                 } else if (tmptok == "FormulaMacro") {
439                         inset.reset(new MathMacroTemplate);
440                 } else if (tmptok == "Formula") {
441                         inset.reset(new InsetMathHull);
442                 } else if (tmptok == "Graphics") {
443                         inset.reset(new InsetGraphics);
444                 } else if (tmptok == "Note") {
445                         inset.reset(new InsetNote(buf.params(), tmptok));
446                 } else if (tmptok == "Box") {
447                         inset.reset(new InsetBox(buf.params(), tmptok));
448                 } else if (tmptok == "Flex") {
449                         lex.next();
450                         string s = lex.getString();
451                         InsetLayout il = tclass.insetlayout(from_utf8(s));
452                         inset.reset(new InsetFlex(buf.params(), il));
453                 } else if (tmptok == "Branch") {
454                         inset.reset(new InsetBranch(buf.params(),
455                                                     InsetBranchParams()));
456                 } else if (tmptok == "Include") {
457                         InsetCommandParams p("include");
458                         inset.reset(new InsetInclude(p));
459                 } else if (tmptok == "Environment") {
460                         lex.next();
461                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
462                 } else if (tmptok == "ERT") {
463                         inset.reset(new InsetERT(buf.params()));
464                 } else if (tmptok == "listings") {
465                         inset.reset(new InsetListings(buf.params()));
466                 } else if (tmptok == "InsetSpace") {
467                         inset.reset(new InsetSpace);
468                 } else if (tmptok == "Tabular") {
469                         inset.reset(new InsetTabular(buf));
470                 } else if (tmptok == "Text") {
471                         inset.reset(new InsetText(buf.params()));
472                 } else if (tmptok == "VSpace") {
473                         inset.reset(new InsetVSpace);
474                 } else if (tmptok == "Foot") {
475                         inset.reset(new InsetFoot(buf.params()));
476                 } else if (tmptok == "Marginal") {
477                         inset.reset(new InsetMarginal(buf.params()));
478                 } else if (tmptok == "OptArg") {
479                         inset.reset(new InsetOptArg(buf.params()));
480                 } else if (tmptok == "Float") {
481                         lex.next();
482                         string tmptok = lex.getString();
483                         inset.reset(new InsetFloat(buf.params(), tmptok));
484                 } else if (tmptok == "Wrap") {
485                         lex.next();
486                         string tmptok = lex.getString();
487                         inset.reset(new InsetWrap(buf.params(), tmptok));
488 #if 0
489                 } else if (tmptok == "List") {
490                         inset.reset(new InsetList);
491                 } else if (tmptok == "Theorem") {
492                         inset.reset(new InsetList);
493 #endif
494                 } else if (tmptok == "Caption") {
495                         inset.reset(new InsetCaption(buf.params()));
496                 } else if (tmptok == "Index") {
497                         inset.reset(new InsetIndex(buf.params()));
498                 } else if (tmptok == "FloatList") {
499                         inset.reset(new InsetFloatList);
500                 } else {
501                         lyxerr << "unknown Inset type '" << tmptok
502                                << "'" << std::endl;
503                         while (lex.isOK() && lex.getString() != "\\end_inset")
504                                 lex.next();
505                         return 0;
506                 }
507
508                 inset->read(buf, lex);
509
510 // FIXME: hack..
511                 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
512                         MathMacroTemplate const * tmpl =
513                                 static_cast<MathMacroTemplate*>(inset.get());
514                         MacroTable::globalMacros().insert
515                                 (tmpl->name(), tmpl->asMacroData());
516                         LYXERR(Debug::DEBUG)
517                                 << BOOST_CURRENT_FUNCTION
518                                 << ": creating local macro " << to_utf8(tmpl->name())
519                                 << endl;
520                 }
521         }
522
523         return inset.release();
524 }
525
526
527 } // namespace lyx