]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
cosmetics
[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("index_print"));
220
221                 case LFUN_NOMENCL_PRINT:
222                         return new InsetPrintNomencl(InsetCommandParams("nomencl_print"));
223
224                 case LFUN_TOC_INSERT:
225                         return new InsetTOC(InsetCommandParams("toc"));
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("citation");
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("toc");
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 == "CommandInset") {
397                 lex.next();
398                 string const insetType = lex.getString();
399                 lex.pushToken(insetType);
400
401                 //FIXME 
402                 //Inset::Code const code = Inset::translate(insetType);
403                 //if (code == Inset::NO_CODE) { choke as below; }
404                 //InsetCommandParams inscmd();
405                 InsetCommandParams inscmd(insetType);
406                 inscmd.read(lex);
407
408                 if (insetType == "citation") {
409                         inset.reset(new InsetCitation(inscmd));
410                 } else if (insetType == "bibitem") {
411                         inset.reset(new InsetBibitem(inscmd));
412                 } else if (insetType == "bibtex") {
413                         inset.reset(new InsetBibtex(inscmd));
414                 } else if (insetType == "index") {
415                         inset.reset(new InsetIndex(inscmd));
416                 } else if (insetType == "nomenclature") {
417                         inset.reset(new InsetNomencl(inscmd));
418                 } else if (insetType == "include") {
419                         inset.reset(new InsetInclude(inscmd));
420                 } else if (insetType == "label") {
421                         inset.reset(new InsetLabel(inscmd));
422                 } else if (insetType == "url") {
423                         inset.reset(new InsetUrl(inscmd));
424                 } else if (insetType == "ref") {
425                         if (!inscmd["name"].empty()
426                             || !inscmd["reference"].empty()) {
427                                 inset.reset(new InsetRef(inscmd, buf));
428                         }
429                 } else if (insetType == "toc") {
430                         inset.reset(new InsetTOC(inscmd));
431                 } else if (insetType == "index_print") {
432                         inset.reset(new InsetPrintIndex(inscmd));
433                 } else if (insetType == "nomencl_print") {
434                         inset.reset(new InsetPrintNomencl(inscmd));
435                 } else {
436                         lyxerr << "unknown CommandInset '" << insetType
437                                << "'" << std::endl;
438                         while (lex.isOK() && lex.getString() != "\\end_inset")
439                                 lex.next();
440                         return 0;
441                 }
442         } else {
443                 if (tmptok == "Quotes") {
444                         inset.reset(new InsetQuotes);
445                 } else if (tmptok == "External") {
446                         inset.reset(new InsetExternal);
447                 } else if (tmptok == "FormulaMacro") {
448                         inset.reset(new MathMacroTemplate);
449                 } else if (tmptok == "Formula") {
450                         inset.reset(new InsetMathHull);
451                 } else if (tmptok == "Graphics") {
452                         inset.reset(new InsetGraphics);
453                 } else if (tmptok == "Note") {
454                         inset.reset(new InsetNote(buf.params(), tmptok));
455                 } else if (tmptok == "Box") {
456                         inset.reset(new InsetBox(buf.params(), tmptok));
457                 } else if (tmptok == "Flex") {
458                         lex.next();
459                         string s = lex.getString();
460                         InsetLayout il = tclass.insetlayout(from_utf8(s));
461                         inset.reset(new InsetFlex(buf.params(), il));
462                 } else if (tmptok == "Branch") {
463                         inset.reset(new InsetBranch(buf.params(),
464                                                     InsetBranchParams()));
465                 } else if (tmptok == "Include") {
466                         InsetCommandParams p("include");
467                         inset.reset(new InsetInclude(p));
468                 } else if (tmptok == "Environment") {
469                         lex.next();
470                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
471                 } else if (tmptok == "ERT") {
472                         inset.reset(new InsetERT(buf.params()));
473                 } else if (tmptok == "listings") {
474                         inset.reset(new InsetListings(buf.params()));
475                 } else if (tmptok == "InsetSpace") {
476                         inset.reset(new InsetSpace);
477                 } else if (tmptok == "Tabular") {
478                         inset.reset(new InsetTabular(buf));
479                 } else if (tmptok == "Text") {
480                         inset.reset(new InsetText(buf.params()));
481                 } else if (tmptok == "VSpace") {
482                         inset.reset(new InsetVSpace);
483                 } else if (tmptok == "Foot") {
484                         inset.reset(new InsetFoot(buf.params()));
485                 } else if (tmptok == "Marginal") {
486                         inset.reset(new InsetMarginal(buf.params()));
487                 } else if (tmptok == "OptArg") {
488                         inset.reset(new InsetOptArg(buf.params()));
489                 } else if (tmptok == "Float") {
490                         lex.next();
491                         string tmptok = lex.getString();
492                         inset.reset(new InsetFloat(buf.params(), tmptok));
493                 } else if (tmptok == "Wrap") {
494                         lex.next();
495                         string tmptok = lex.getString();
496                         inset.reset(new InsetWrap(buf.params(), tmptok));
497 #if 0
498                 } else if (tmptok == "List") {
499                         inset.reset(new InsetList);
500                 } else if (tmptok == "Theorem") {
501                         inset.reset(new InsetList);
502 #endif
503                 } else if (tmptok == "Caption") {
504                         inset.reset(new InsetCaption(buf.params()));
505                 } else if (tmptok == "FloatList") {
506                         inset.reset(new InsetFloatList);
507                 } else {
508                         lyxerr << "unknown Inset type '" << tmptok
509                                << "'" << std::endl;
510                         while (lex.isOK() && lex.getString() != "\\end_inset")
511                                 lex.next();
512                         return 0;
513                 }
514
515                 inset->read(buf, lex);
516
517 // FIXME: hack..
518                 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
519                         MathMacroTemplate const * tmpl =
520                                 static_cast<MathMacroTemplate*>(inset.get());
521                         MacroTable::globalMacros().insert
522                                 (tmpl->name(), tmpl->asMacroData());
523                         LYXERR(Debug::DEBUG)
524                                 << BOOST_CURRENT_FUNCTION
525                                 << ": creating local macro " << to_utf8(tmpl->name())
526                                 << endl;
527                 }
528         }
529
530         return inset.release();
531 }
532
533
534 } // namespace lyx