]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[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                         // Try and generate a valid index entry.
182                         InsetCommandParams icp("index");
183                         icp["name"] = cmd.argument().empty() ?
184                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
185                                 cmd.argument();
186                         return new InsetIndex(icp);
187                 }
188
189                 case LFUN_NOMENCL_INSERT: {
190                         InsetCommandParams icp("nomenclature");
191                         icp["symbol"] = cmd.argument().empty() ?
192                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
193                                 cmd.argument();
194                         return new InsetNomencl(icp);
195                 }
196
197                 case LFUN_TABULAR_INSERT: {
198                         if (cmd.argument().empty())
199                                 return 0;
200                         std::istringstream ss(to_utf8(cmd.argument()));
201                         int r = 0, c = 0;
202                         ss >> r >> c;
203                         if (r <= 0)
204                                 r = 2;
205                         if (c <= 0)
206                                 c = 2;
207                         return new InsetTabular(bv->buffer(), r, c);
208                 }
209
210                 case LFUN_CAPTION_INSERT: {
211                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
212                         inset->setAutoBreakRows(true);
213                         inset->setDrawFrame(true);
214                         inset->setFrameColor(Color::captionframe);
215                         return inset.release();
216                 }
217
218                 case LFUN_INDEX_PRINT:
219                         return new InsetPrintIndex(InsetCommandParams("printindex"));
220
221                 case LFUN_NOMENCL_PRINT:
222                         return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
223
224                 case LFUN_TOC_INSERT:
225                         return new InsetTOC(InsetCommandParams("tableofcontents"));
226
227                 case LFUN_ENVIRONMENT_INSERT:
228                         return new InsetEnvironment(params, cmd.argument());
229
230 #if 0
231                 case LFUN_LIST_INSERT:
232                         return new InsetList;
233
234                 case LFUN_THEOREM_INSERT:
235                         return new InsetTheorem;
236 #endif
237
238                 case LFUN_INSET_INSERT: {
239                         string const name = cmd.getArg(0);
240
241                         if (name == "bibitem") {
242                                 InsetCommandParams icp(name);
243                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
244                                         icp);
245                                 return new InsetBibitem(icp);
246
247                         } else if (name == "bibtex") {
248                                 InsetCommandParams icp(name);
249                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
250                                         icp);
251                                 return new InsetBibtex(icp);
252
253                         } else if (name == "citation") {
254                                 InsetCommandParams icp("cite");
255                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
256                                         icp);
257                                 return new InsetCitation(icp);
258
259                         } else if (name == "ert") {
260                                 InsetCollapsable::CollapseStatus st;
261                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
262                                 return new InsetERT(params, st);
263
264                         } else if (name == "listings") {
265                                 InsetListingsParams par;
266                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
267                                 return new InsetListings(params, par);
268
269                         } else if (name == "external") {
270                                 Buffer const & buffer = bv->buffer();
271                                 InsetExternalParams iep;
272                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
273                                         buffer, iep);
274                                 auto_ptr<InsetExternal> inset(new InsetExternal);
275                                 inset->setParams(iep, buffer);
276                                 return inset.release();
277
278                         } else if (name == "graphics") {
279                                 Buffer const & buffer = bv->buffer();
280                                 InsetGraphicsParams igp;
281                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
282                                         buffer, igp);
283                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
284                                 inset->setParams(igp);
285                                 return inset.release();
286
287                         } else if (name == "include") {
288                                 InsetCommandParams iip(name);
289                                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
290                                 return new InsetInclude(iip);
291
292                         } else if (name == "index") {
293                                 InsetCommandParams icp(name);
294                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
295                                         icp);
296                                 return new InsetIndex(icp);
297
298                         } else if (name == "nomenclature") {
299                                 InsetCommandParams icp(name);
300                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
301                                         icp);
302                                 return new InsetNomencl(icp);
303
304                         } else if (name == "label") {
305                                 InsetCommandParams icp(name);
306                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
307                                         icp);
308                                 return new InsetLabel(icp);
309
310                         } else if (name == "ref") {
311                                 InsetCommandParams icp(name);
312                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
313                                         icp);
314                                 return new InsetRef(icp, bv->buffer());
315
316                         } else if (name == "toc") {
317                                 InsetCommandParams icp("tableofcontents");
318                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
319                                         icp);
320                                 return new InsetTOC(icp);
321
322                         } else if (name == "url") {
323                                 InsetCommandParams icp(name);
324                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
325                                         icp);
326                                 return new InsetUrl(icp);
327
328                         } else if (name == "vspace") {
329                                 VSpace vspace;
330                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
331                                 return new InsetVSpace(vspace);
332                         }
333                 }
334
335                 case LFUN_SPACE_INSERT: {
336                         string const name = to_utf8(cmd.argument());
337                         if (name == "normal")
338                                 return new InsetSpace(InsetSpace::NORMAL);
339                         else if (name == "protected")
340                                 return new InsetSpace(InsetSpace::PROTECTED);
341                         else if (name == "thin")
342                                 return new InsetSpace(InsetSpace::THIN);
343                         else if (name == "quad")
344                                 return new InsetSpace(InsetSpace::QUAD);
345                         else if (name == "qquad")
346                                 return new InsetSpace(InsetSpace::QQUAD);
347                         else if (name == "enspace")
348                                 return new InsetSpace(InsetSpace::ENSPACE);
349                         else if (name == "enskip")
350                                 return new InsetSpace(InsetSpace::ENSKIP);
351                         else if (name == "negthinspace")
352                                 return new InsetSpace(InsetSpace::NEGTHIN);
353                         else if (name.empty())
354                                 lyxerr << "LyX function 'space' needs an argument." << endl;
355                         else
356                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
357                 }
358                 break;
359
360                 default:
361                         break;
362                 }
363
364         } catch (support::ExceptionMessage const & message) {
365                 if (message.type_ == support::ErrorException) {
366                         Alert::error(message.title_, message.details_);
367                         LyX::cref().emergencyCleanup();
368                         abort();
369                 } else if (message.type_ == support::WarningException) {
370                         Alert::warning(message.title_, message.details_);
371                         return 0;
372                 }
373         }
374
375
376         return 0;
377 }
378
379
380 Inset * readInset(Lexer & lex, Buffer const & buf)
381 {
382         // consistency check
383         if (lex.getString() != "\\begin_inset") {
384                 lyxerr << "Buffer::readInset: Consistency check failed."
385                        << endl;
386         }
387
388         auto_ptr<Inset> inset;
389
390         TextClass tclass = buf.params().getTextClass();
391
392         lex.next();
393         string tmptok = lex.getString();
394
395         // test the different insets
396         if (tmptok == "LatexCommand") {
397                 lex.next();
398                 string const cmdName = lex.getString();
399                 lex.pushToken(cmdName);
400
401                 InsetCommandParams inscmd(cmdName);
402                 inscmd.read(lex);
403
404                 // This strange command allows LyX to recognize "natbib" style
405                 // citations: citet, citep, Citet etc.
406                 // FIXME: We already have partial support for \\fullcite and
407                 // the various \\footcite commands. We should increase the
408                 // file format number and read these commands here, too.
409                 // Then we should use is_possible_cite_command() in
410                 // InsetCitation to test for valid cite commands.
411                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
412                         inset.reset(new InsetCitation(inscmd));
413                 } else if (cmdName == "bibitem") {
414                         inset.reset(new InsetBibitem(inscmd));
415                 } else if (cmdName == "bibtex") {
416                         inset.reset(new InsetBibtex(inscmd));
417                 } else if (cmdName == "index") {
418                         inset.reset(new InsetIndex(inscmd));
419                 } else if (cmdName == "nomenclature") {
420                         inset.reset(new InsetNomencl(inscmd));
421                 } else if (cmdName == "include") {
422                         inset.reset(new InsetInclude(inscmd));
423                 } else if (cmdName == "label") {
424                         inset.reset(new InsetLabel(inscmd));
425                 } else if (cmdName == "url"
426                            || cmdName == "htmlurl") {
427                         inset.reset(new InsetUrl(inscmd));
428                 } else if (cmdName == "ref"
429                            || cmdName == "eqref"
430                            || cmdName == "pageref"
431                            || cmdName == "vref"
432                            || cmdName == "vpageref"
433                            || cmdName == "prettyref") {
434                         if (!inscmd["name"].empty()
435                             || !inscmd["reference"].empty()) {
436                                 inset.reset(new InsetRef(inscmd, buf));
437                         }
438                 } else if (cmdName == "tableofcontents") {
439                         inset.reset(new InsetTOC(inscmd));
440                 } else if (cmdName == "listofalgorithms") {
441                         inset.reset(new InsetFloatList("algorithm"));
442                 } else if (cmdName == "listoffigures") {
443                         inset.reset(new InsetFloatList("figure"));
444                 } else if (cmdName == "listoftables") {
445                         inset.reset(new InsetFloatList("table"));
446                 } else if (cmdName == "printindex") {
447                         inset.reset(new InsetPrintIndex(inscmd));
448                 } else if (cmdName == "printnomenclature") {
449                         inset.reset(new InsetPrintNomencl(inscmd));
450                 } else {
451                         lyxerr << "unknown CommandInset '" << cmdName
452                                << "'" << std::endl;
453                         while (lex.isOK() && lex.getString() != "\\end_inset")
454                                 lex.next();
455                         return 0;
456                 }
457         } else {
458                 if (tmptok == "Quotes") {
459                         inset.reset(new InsetQuotes);
460                 } else if (tmptok == "External") {
461                         inset.reset(new InsetExternal);
462                 } else if (tmptok == "FormulaMacro") {
463                         inset.reset(new MathMacroTemplate);
464                 } else if (tmptok == "Formula") {
465                         inset.reset(new InsetMathHull);
466                 } else if (tmptok == "Graphics") {
467                         inset.reset(new InsetGraphics);
468                 } else if (tmptok == "Note") {
469                         inset.reset(new InsetNote(buf.params(), tmptok));
470                 } else if (tmptok == "Box") {
471                         inset.reset(new InsetBox(buf.params(), tmptok));
472                 } else if (tmptok == "Flex") {
473                         lex.next();
474                         string s = lex.getString();
475                         InsetLayout il = tclass.insetlayout(from_utf8(s));
476                         inset.reset(new InsetFlex(buf.params(), il));
477                 } else if (tmptok == "Branch") {
478                         inset.reset(new InsetBranch(buf.params(),
479                                                     InsetBranchParams()));
480                 } else if (tmptok == "Include") {
481                         InsetCommandParams p("include");
482                         inset.reset(new InsetInclude(p));
483                 } else if (tmptok == "Environment") {
484                         lex.next();
485                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
486                 } else if (tmptok == "ERT") {
487                         inset.reset(new InsetERT(buf.params()));
488                 } else if (tmptok == "listings") {
489                         inset.reset(new InsetListings(buf.params()));
490                 } else if (tmptok == "InsetSpace") {
491                         inset.reset(new InsetSpace);
492                 } else if (tmptok == "Tabular") {
493                         inset.reset(new InsetTabular(buf));
494                 } else if (tmptok == "Text") {
495                         inset.reset(new InsetText(buf.params()));
496                 } else if (tmptok == "VSpace") {
497                         inset.reset(new InsetVSpace);
498                 } else if (tmptok == "Foot") {
499                         inset.reset(new InsetFoot(buf.params()));
500                 } else if (tmptok == "Marginal") {
501                         inset.reset(new InsetMarginal(buf.params()));
502                 } else if (tmptok == "OptArg") {
503                         inset.reset(new InsetOptArg(buf.params()));
504                 } else if (tmptok == "Float") {
505                         lex.next();
506                         string tmptok = lex.getString();
507                         inset.reset(new InsetFloat(buf.params(), tmptok));
508                 } else if (tmptok == "Wrap") {
509                         lex.next();
510                         string tmptok = lex.getString();
511                         inset.reset(new InsetWrap(buf.params(), tmptok));
512 #if 0
513                 } else if (tmptok == "List") {
514                         inset.reset(new InsetList);
515                 } else if (tmptok == "Theorem") {
516                         inset.reset(new InsetList);
517 #endif
518                 } else if (tmptok == "Caption") {
519                         inset.reset(new InsetCaption(buf.params()));
520                 } else if (tmptok == "FloatList") {
521                         inset.reset(new InsetFloatList);
522                 } else {
523                         lyxerr << "unknown Inset type '" << tmptok
524                                << "'" << std::endl;
525                         while (lex.isOK() && lex.getString() != "\\end_inset")
526                                 lex.next();
527                         return 0;
528                 }
529
530                 inset->read(buf, lex);
531
532 // FIXME: hack..
533                 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
534                         MathMacroTemplate const * tmpl =
535                                 static_cast<MathMacroTemplate*>(inset.get());
536                         MacroTable::globalMacros().insert
537                                 (tmpl->name(), tmpl->asMacroData());
538                         LYXERR(Debug::DEBUG)
539                                 << BOOST_CURRENT_FUNCTION
540                                 << ": creating local macro " << to_utf8(tmpl->name())
541                                 << endl;
542                 }
543         }
544
545         return inset.release();
546 }
547
548
549 } // namespace lyx