]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
adjust
[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/InsetHyperlink.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_THEOREM_INSERT:
229                         return new InsetTheorem;
230 #endif
231
232                 case LFUN_INSET_INSERT: {
233                         string const name = cmd.getArg(0);
234                         InsetCode code = insetCode(name);
235                         switch (code) {
236                         case NO_CODE:
237                                 lyxerr << "No such inset '" << name << "'.";
238                                 return 0;
239                         
240                                 case BIBITEM_CODE: {
241                                         InsetCommandParams icp(name);
242                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
243                                         return new InsetBibitem(icp);
244                                 }
245                 
246                                 case BIBTEX_CODE: {
247                                         InsetCommandParams icp(name);
248                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
249                                         return new InsetBibtex(icp);
250                                 }
251
252                                 case CITE_CODE: {
253                                         InsetCommandParams icp("citation");
254                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
255                                         return new InsetCitation(icp);
256                                 }
257                 
258                                 case ERT_CODE: {
259                                         InsetCollapsable::CollapseStatus st;
260                                         InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
261                                         return new InsetERT(params, st);
262                                 }
263
264                                 case LISTINGS_CODE: {
265                                         InsetListingsParams par;
266                                         InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
267                                         return new InsetListings(params, par);
268                                 }
269
270                                 case EXTERNAL_CODE: {
271                                         Buffer const & buffer = bv->buffer();
272                                         InsetExternalParams iep;
273                                         InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, iep);
274                                         auto_ptr<InsetExternal> inset(new InsetExternal);
275                                         inset->setParams(iep, buffer);
276                                         return inset.release();
277                                 }
278
279                                 case GRAPHICS_CODE: {
280                                         Buffer const & buffer = bv->buffer();
281                                         InsetGraphicsParams igp;
282                                         InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, igp);
283                                         auto_ptr<InsetGraphics> inset(new InsetGraphics);
284                                         inset->setParams(igp);
285                                         return inset.release();
286                                 }
287
288                                 case HYPERLINK_CODE: {
289                                         InsetCommandParams icp(name);
290                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291                                         return new InsetHyperlink(icp);
292                                 }
293
294                                 case INCLUDE_CODE: {
295                                         InsetCommandParams iip(name);
296                                         InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
297                                         return new InsetInclude(iip);
298                                 }
299
300                         case INDEX_CODE:
301                                 return new InsetIndex(params);
302
303                                 case NOMENCL_CODE: {
304                                         InsetCommandParams icp(name);
305                                         InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
306                                         return new InsetNomencl(icp);
307                                 }
308
309                                 case LABEL_CODE: {
310                                         InsetCommandParams icp(name);
311                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
312                                         return new InsetLabel(icp);
313                                 }
314
315                                 case REF_CODE: {
316                                         InsetCommandParams icp(name);
317                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
318                                         return new InsetRef(icp, bv->buffer());
319                                 }
320
321                                 case TOC_CODE: {
322                                         InsetCommandParams icp("toc");
323                                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
324                                         return new InsetTOC(icp);
325                                 }
326
327                                 case VSPACE_CODE: {
328                                         VSpace vspace;
329                                         InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
330                                         return new InsetVSpace(vspace);
331                                 }
332                 
333                                 default:
334                                         lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
335                                                         << std::endl;
336                                         return 0;
337                         }
338                         } //end LFUN_INSET_INSERT
339
340                 case LFUN_SPACE_INSERT: {
341                         string const name = to_utf8(cmd.argument());
342                         if (name == "normal")
343                                 return new InsetSpace(InsetSpace::NORMAL);
344                         else if (name == "protected")
345                                 return new InsetSpace(InsetSpace::PROTECTED);
346                         else if (name == "thin")
347                                 return new InsetSpace(InsetSpace::THIN);
348                         else if (name == "quad")
349                                 return new InsetSpace(InsetSpace::QUAD);
350                         else if (name == "qquad")
351                                 return new InsetSpace(InsetSpace::QQUAD);
352                         else if (name == "enspace")
353                                 return new InsetSpace(InsetSpace::ENSPACE);
354                         else if (name == "enskip")
355                                 return new InsetSpace(InsetSpace::ENSKIP);
356                         else if (name == "negthinspace")
357                                 return new InsetSpace(InsetSpace::NEGTHIN);
358                         else if (name.empty())
359                                 lyxerr << "LyX function 'space' needs an argument." << endl;
360                         else
361                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
362                 }
363                 break;
364
365                 default:
366                         break;
367                 }
368
369         } catch (support::ExceptionMessage const & message) {
370                 if (message.type_ == support::ErrorException) {
371                         Alert::error(message.title_, message.details_);
372                         LyX::cref().emergencyCleanup();
373                         abort();
374                 } else if (message.type_ == support::WarningException) {
375                         Alert::warning(message.title_, message.details_);
376                         return 0;
377                 }
378         }
379
380
381         return 0;
382 }
383
384
385 Inset * readInset(Lexer & lex, Buffer const & buf)
386 {
387         // consistency check
388         if (lex.getString() != "\\begin_inset") {
389                 lyxerr << "Buffer::readInset: Consistency check failed."
390                        << endl;
391         }
392
393         auto_ptr<Inset> inset;
394
395         TextClass tclass = buf.params().getTextClass();
396
397         lex.next();
398         string tmptok = lex.getString();
399
400         // test the different insets
401         
402         //FIXME It would be better if we did not have this branch and could
403         //just do one massive switch for all insets. But at present, it's easier 
404         //to do it this way, and we can't do the massive switch until the conversion 
405         //mentioned below. 
406         //Note that if we do want to do a single switch, we need to remove
407         //this "CommandInset" line---or replace it with a single "InsetType" line
408         //that would be used in all insets.
409         if (tmptok == "CommandInset") {
410                 lex.next();
411                 string const insetType = lex.getString();
412                 lex.pushToken(insetType);
413                 
414                 InsetCode const code = insetCode(insetType);
415                 
416                 //FIXME If we do the one massive switch, we cannot do this here, since
417                 //we do not know in advance that we're dealing with a command inset.
418                 //Worst case, we could put it in each case below. Better, we could
419                 //pass the lexer to the constructor and let the params be built there.
420                 InsetCommandParams inscmd(insetType);
421                 inscmd.read(lex);
422
423                 switch (code) {
424                         case BIBITEM_CODE:
425                                 inset.reset(new InsetBibitem(inscmd));
426                                 break;
427                         case BIBTEX_CODE:
428                                 inset.reset(new InsetBibtex(inscmd));
429                                 break;
430                         case CITE_CODE: 
431                                 inset.reset(new InsetCitation(inscmd));
432                                 break;
433                         case HYPERLINK_CODE:
434                                 inset.reset(new InsetHyperlink(inscmd));
435                                 break;
436                         // FIXME Currently non-functional, since InsetInclude
437                         // does not write itself as a CommandInset.
438                         case INCLUDE_CODE:
439                                 inset.reset(new InsetInclude(inscmd));
440                                 break;
441                         case INDEX_CODE:
442                                 inset.reset(new InsetIndex(buf.params()));
443                                 break;
444                         case INDEX_PRINT_CODE:
445                                 inset.reset(new InsetPrintIndex(inscmd));
446                                 break;
447                         case LABEL_CODE:
448                                 inset.reset(new InsetLabel(inscmd));
449                                 break;
450                         case NOMENCL_CODE:
451                                 inset.reset(new InsetNomencl(inscmd));
452                                 break;
453                         case NOMENCL_PRINT_CODE:
454                                 inset.reset(new InsetPrintNomencl(inscmd));
455                                 break;
456                         case REF_CODE:
457                                 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
458                                         inset.reset(new InsetRef(inscmd, buf));
459                                 break;
460                         case TOC_CODE:
461                                 inset.reset(new InsetTOC(inscmd));
462                                 break;
463                         case NO_CODE:
464                         default:
465                                 lyxerr << "unknown CommandInset '" << insetType
466                                                         << "'" << std::endl;
467                                 while (lex.isOK() && lex.getString() != "\\end_inset")
468                                         lex.next();
469                                 return 0;
470                 }
471         } else { 
472                 // FIXME This branch should be made to use inset codes as the preceding 
473                 // branch does. Unfortunately, that will take some doing. It requires
474                 // converting the representation of the insets in LyX files so that they
475                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
476                 // can be translated to inset codes using insetCode(). And the insets'
477                 // write() routines should use insetName() rather than hardcoding it.
478                 if (tmptok == "Quotes") {
479                         inset.reset(new InsetQuotes);
480                 } else if (tmptok == "External") {
481                         inset.reset(new InsetExternal);
482                 } else if (tmptok == "FormulaMacro") {
483                         inset.reset(new MathMacroTemplate);
484                 } else if (tmptok == "Formula") {
485                         inset.reset(new InsetMathHull);
486                 } else if (tmptok == "Graphics") {
487                         inset.reset(new InsetGraphics);
488                 } else if (tmptok == "Note") {
489                         inset.reset(new InsetNote(buf.params(), tmptok));
490                 } else if (tmptok == "Box") {
491                         inset.reset(new InsetBox(buf.params(), tmptok));
492                 } else if (tmptok == "Flex") {
493                         lex.next();
494                         string s = lex.getString();
495                         InsetLayout il = tclass.insetlayout(from_utf8(s));
496                         inset.reset(new InsetFlex(buf.params(), il));
497                 } else if (tmptok == "Branch") {
498                         inset.reset(new InsetBranch(buf.params(),
499                                                     InsetBranchParams()));
500                 } else if (tmptok == "Include") {
501                         //FIXME
502                         InsetCommandParams p("include");
503                         inset.reset(new InsetInclude(p));
504                 } else if (tmptok == "Environment") {
505                         lex.next();
506                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
507                 } else if (tmptok == "ERT") {
508                         inset.reset(new InsetERT(buf.params()));
509                 } else if (tmptok == "listings") {
510                         inset.reset(new InsetListings(buf.params()));
511                 } else if (tmptok == "InsetSpace") {
512                         inset.reset(new InsetSpace);
513                 } else if (tmptok == "Tabular") {
514                         inset.reset(new InsetTabular(buf));
515                 } else if (tmptok == "Text") {
516                         inset.reset(new InsetText(buf.params()));
517                 } else if (tmptok == "VSpace") {
518                         inset.reset(new InsetVSpace);
519                 } else if (tmptok == "Foot") {
520                         inset.reset(new InsetFoot(buf.params()));
521                 } else if (tmptok == "Marginal") {
522                         inset.reset(new InsetMarginal(buf.params()));
523                 } else if (tmptok == "OptArg") {
524                         inset.reset(new InsetOptArg(buf.params()));
525                 } else if (tmptok == "Float") {
526                         lex.next();
527                         string tmptok = lex.getString();
528                         inset.reset(new InsetFloat(buf.params(), tmptok));
529                 } else if (tmptok == "Wrap") {
530                         lex.next();
531                         string tmptok = lex.getString();
532                         inset.reset(new InsetWrap(buf.params(), tmptok));
533 #if 0
534                 } else if (tmptok == "Theorem") {
535                         inset.reset(new InsetList);
536 #endif
537                 } else if (tmptok == "Caption") {
538                         inset.reset(new InsetCaption(buf.params()));
539                 } else if (tmptok == "Index") {
540                         inset.reset(new InsetIndex(buf.params()));
541                 } else if (tmptok == "FloatList") {
542                         inset.reset(new InsetFloatList);
543                 } else if (tmptok == "Info") {
544                         inset.reset(new InsetInfo(buf.params()));
545                 } else {
546                         lyxerr << "unknown Inset type '" << tmptok
547                                << "'" << std::endl;
548                         while (lex.isOK() && lex.getString() != "\\end_inset")
549                                 lex.next();
550                         return 0;
551                 }
552
553                 inset->read(buf, lex);
554
555 // FIXME: hack..
556                 if (inset->lyxCode() == MATHMACRO_CODE) {
557                         MathMacroTemplate const * tmpl =
558                                 static_cast<MathMacroTemplate*>(inset.get());
559                         MacroTable::globalMacros().insert
560                                 (tmpl->name(), tmpl->asMacroData());
561                         LYXERR(Debug::DEBUG)
562                                 << BOOST_CURRENT_FUNCTION
563                                 << ": creating local macro " << to_utf8(tmpl->name())
564                                 << endl;
565                 }
566         }
567
568         return inset.release();
569 }
570
571
572 } // namespace lyx