]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
document my LFUN changes in the past
[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 "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
19 #include "Lexer.h"
20 #include "LyX.h"
21 #include "TextClass.h"
22
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetCaption.h"
26 #include "insets/InsetCitation.h"
27 #include "insets/InsetFlex.h"
28 #include "insets/InsetEnvironment.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetListings.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetFloatList.h"
34 #include "insets/InsetFoot.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetHFill.h"
37 #include "insets/InsetInclude.h"
38 #include "insets/InsetIndex.h"
39 #include "insets/InsetInfo.h"
40 #include "insets/InsetNomencl.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNote.h"
45 #include "insets/InsetBox.h"
46 #include "insets/InsetBranch.h"
47 #include "insets/InsetOptArg.h"
48 #include "insets/InsetNewpage.h"
49 #include "insets/InsetRef.h"
50 #include "insets/InsetSpace.h"
51 #include "insets/InsetTabular.h"
52 #include "insets/InsetTOC.h"
53 #include "insets/InsetHyperlink.h"
54 #include "insets/InsetVSpace.h"
55 #include "insets/InsetWrap.h"
56
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
59
60 #include "frontends/alert.h"
61
62 #include "support/debug.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
65
66 #include <boost/assert.hpp>
67
68 #include <sstream>
69
70 using std::auto_ptr;
71 using std::endl;
72 using std::string;
73
74
75 namespace lyx {
76
77 namespace Alert = frontend::Alert;
78
79
80 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
81 {
82         BufferParams const & params = buf.params();
83
84         try {
85
86                 switch (cmd.action) {
87                 case LFUN_HFILL_INSERT:
88                         return new InsetHFill;
89
90                 case LFUN_LINE_INSERT:
91                         return new InsetLine;
92
93                 case LFUN_NEWPAGE_INSERT:
94                         return new InsetNewpage;
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 const & tclass = params.getTextClass();
108                         InsetLayout const & 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_CODE));
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                 }
158
159                 case LFUN_FLOAT_WIDE_INSERT: {
160                         // check if the float type exists
161                         string const argument = to_utf8(cmd.argument());
162                         if (params.getTextClass().floats().typeExist(argument)) {
163                                 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
164                                 p->wide(true, params);
165                                 return p.release();
166                         }
167                         lyxerr << "Non-existent float type: " << argument << endl;
168                         return 0;
169                 }
170
171                 case LFUN_WRAP_INSERT: {
172                         string const argument = to_utf8(cmd.argument());
173                         if (argument == "figure" || argument == "table")
174                                 return new InsetWrap(params, argument);
175                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
176                         return 0;
177                 }
178
179                 case LFUN_INDEX_INSERT:
180                         return new InsetIndex(params);
181
182                 case LFUN_NOMENCL_INSERT: {
183                         InsetCommandParams icp(NOMENCL_CODE);
184                         icp["symbol"] = cmd.argument();
185                         return new InsetNomencl(icp);
186                 }
187
188                 case LFUN_TABULAR_INSERT: {
189                         if (cmd.argument().empty())
190                                 return 0;
191                         std::istringstream ss(to_utf8(cmd.argument()));
192                         int r = 0, c = 0;
193                         ss >> r >> c;
194                         if (r <= 0)
195                                 r = 2;
196                         if (c <= 0)
197                                 c = 2;
198                         return new InsetTabular(buf, r, c);
199                 }
200
201                 case LFUN_CAPTION_INSERT: {
202                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
203                         inset->setAutoBreakRows(true);
204                         inset->setDrawFrame(true);
205                         inset->setFrameColor(Color_captionframe);
206                         return inset.release();
207                 }
208
209                 case LFUN_INDEX_PRINT:
210                         return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
211
212                 case LFUN_NOMENCL_PRINT:
213                         return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
214
215                 case LFUN_TOC_INSERT:
216                         return new InsetTOC(InsetCommandParams(TOC_CODE));
217
218                 case LFUN_ENVIRONMENT_INSERT:
219                         return new InsetEnvironment(params, cmd.argument());
220
221                 case LFUN_INFO_INSERT:
222                         return new InsetInfo(params, to_utf8(cmd.argument()));
223 #if 0
224                 case LFUN_THEOREM_INSERT:
225                         return new InsetTheorem;
226 #endif
227
228                 case LFUN_INSET_INSERT: {
229                         string const name = cmd.getArg(0);
230                         InsetCode code = insetCode(name);
231                         switch (code) {
232                         case NO_CODE:
233                                 lyxerr << "No such inset '" << name << "'.";
234                                 return 0;
235                         
236                         case BIBITEM_CODE: {
237                                 InsetCommandParams icp(code);
238                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
239                                 return new InsetBibitem(icp);
240                         }
241                         
242                         case BIBTEX_CODE: {
243                                 InsetCommandParams icp(code);
244                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
245                                 return new InsetBibtex(icp);
246                         }
247                         
248                         case CITE_CODE: {
249                                 InsetCommandParams icp(code);
250                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
251                                 return new InsetCitation(icp);
252                         }
253                         
254                         case ERT_CODE: {
255                                 InsetCollapsable::CollapseStatus st;
256                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
257                                 return new InsetERT(params, st);
258                         }
259                                 
260                         case LISTINGS_CODE: {
261                                 InsetListingsParams par;
262                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
263                                 return new InsetListings(params, par);
264                         }
265                         
266                         case EXTERNAL_CODE: {
267                                 InsetExternalParams iep;
268                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
269                                 auto_ptr<InsetExternal> inset(new InsetExternal);
270                                 inset->setParams(iep, buf);
271                                 return inset.release();
272                         }
273                         
274                         case GRAPHICS_CODE: {
275                                 InsetGraphicsParams igp;
276                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
277                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
278                                 inset->setParams(igp);
279                                 return inset.release();
280                         }
281                         
282                         case HYPERLINK_CODE: {
283                                 InsetCommandParams icp(code);
284                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
285                                 return new InsetHyperlink(icp);
286                         }
287                         
288                         case INCLUDE_CODE: {
289                                 InsetCommandParams icp(code);
290                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
291                                 return new InsetInclude(icp);
292                         }
293                         
294                         case INDEX_CODE:
295                                 return new InsetIndex(params);
296                         
297                         case NOMENCL_CODE: {
298                                 InsetCommandParams icp(code);
299                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
300                                 return new InsetNomencl(icp);
301                         }
302                         
303                         case LABEL_CODE: {
304                                 InsetCommandParams icp(code);
305                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
306                                 return new InsetLabel(icp);
307                         }
308                         
309                         case REF_CODE: {
310                                 InsetCommandParams icp(code);
311                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
312                                 return new InsetRef(icp, buf);
313                         }
314                         
315                         case TOC_CODE: {
316                                 InsetCommandParams icp(code);
317                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
318                                 return new InsetTOC(icp);
319                         }
320                         
321                         case VSPACE_CODE: {
322                                 VSpace vspace;
323                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
324                                 return new InsetVSpace(vspace);
325                         }
326                         
327                         default:
328                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
329                                                 << std::endl;
330                                 return 0;
331                         
332                         }
333                         } //end LFUN_INSET_INSERT
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                         if (name == "protected")
340                                 return new InsetSpace(InsetSpace::PROTECTED);
341                         if (name == "thin")
342                                 return new InsetSpace(InsetSpace::THIN);
343                         if (name == "quad")
344                                 return new InsetSpace(InsetSpace::QUAD);
345                         if (name == "qquad")
346                                 return new InsetSpace(InsetSpace::QQUAD);
347                         if (name == "enspace")
348                                 return new InsetSpace(InsetSpace::ENSPACE);
349                         if (name == "enskip")
350                                 return new InsetSpace(InsetSpace::ENSKIP);
351                         if (name == "negthinspace")
352                                 return new InsetSpace(InsetSpace::NEGTHIN);
353                         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 const & tclass = buf.params().getTextClass();
391
392         lex.next();
393         string tmptok = lex.getString();
394
395         // test the different insets
396         
397         //FIXME It would be better if we did not have this branch and could
398         //just do one massive switch for all insets. But at present, it's easier 
399         //to do it this way, and we can't do the massive switch until the conversion 
400         //mentioned below. 
401         //Note that if we do want to do a single switch, we need to remove
402         //this "CommandInset" line---or replace it with a single "InsetType" line
403         //that would be used in all insets.
404         if (tmptok == "CommandInset") {
405                 lex.next();
406                 string const insetType = lex.getString();
407                 lex.pushToken(insetType);
408                 
409                 InsetCode const code = insetCode(insetType);
410                 
411                 //FIXME If we do the one massive switch, we cannot do this here, since
412                 //we do not know in advance that we're dealing with a command inset.
413                 //Worst case, we could put it in each case below. Better, we could
414                 //pass the lexer to the constructor and let the params be built there.
415                 InsetCommandParams inscmd(code);
416                 inscmd.read(lex);
417
418                 switch (code) {
419                         case BIBITEM_CODE:
420                                 inset.reset(new InsetBibitem(inscmd));
421                                 break;
422                         case BIBTEX_CODE:
423                                 inset.reset(new InsetBibtex(inscmd));
424                                 break;
425                         case CITE_CODE: 
426                                 inset.reset(new InsetCitation(inscmd));
427                                 break;
428                         case HYPERLINK_CODE:
429                                 inset.reset(new InsetHyperlink(inscmd));
430                                 break;
431                         case INCLUDE_CODE:
432                                 inset.reset(new InsetInclude(inscmd));
433                                 break;
434                         case INDEX_PRINT_CODE:
435                                 inset.reset(new InsetPrintIndex(inscmd));
436                                 break;
437                         case LABEL_CODE:
438                                 inset.reset(new InsetLabel(inscmd));
439                                 break;
440                         case NOMENCL_CODE:
441                                 inset.reset(new InsetNomencl(inscmd));
442                                 break;
443                         case NOMENCL_PRINT_CODE:
444                                 inset.reset(new InsetPrintNomencl(inscmd));
445                                 break;
446                         case REF_CODE:
447                                 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
448                                         inset.reset(new InsetRef(inscmd, buf));
449                                 break;
450                         case TOC_CODE:
451                                 inset.reset(new InsetTOC(inscmd));
452                                 break;
453                         case NO_CODE:
454                         default:
455                                 lyxerr << "unknown CommandInset '" << insetType
456                                                         << "'" << std::endl;
457                                 while (lex.isOK() && lex.getString() != "\\end_inset")
458                                         lex.next();
459                                 return 0;
460                 }
461         } else { 
462                 // FIXME This branch should be made to use inset codes as the preceding 
463                 // branch does. Unfortunately, that will take some doing. It requires
464                 // converting the representation of the insets in LyX files so that they
465                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
466                 // can be translated to inset codes using insetCode(). And the insets'
467                 // write() routines should use insetName() rather than hardcoding it.
468                 if (tmptok == "Quotes") {
469                         inset.reset(new InsetQuotes);
470                 } else if (tmptok == "External") {
471                         inset.reset(new InsetExternal);
472                 } else if (tmptok == "FormulaMacro") {
473                         inset.reset(new MathMacroTemplate);
474                 } else if (tmptok == "Formula") {
475                         inset.reset(new InsetMathHull);
476                 } else if (tmptok == "Graphics") {
477                         inset.reset(new InsetGraphics);
478                 } else if (tmptok == "Note") {
479                         inset.reset(new InsetNote(buf.params(), tmptok));
480                 } else if (tmptok == "Box") {
481                         inset.reset(new InsetBox(buf.params(), tmptok));
482                 } else if (tmptok == "Flex") {
483                         lex.next();
484                         string s = lex.getString();
485                         InsetLayout const & il = tclass.insetlayout(from_utf8(s));
486                         inset.reset(new InsetFlex(buf.params(), il));
487                 } else if (tmptok == "Branch") {
488                         inset.reset(new InsetBranch(buf.params(),
489                                                     InsetBranchParams()));
490                 } else if (tmptok == "Environment") {
491                         lex.next();
492                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
493                 } else if (tmptok == "ERT") {
494                         inset.reset(new InsetERT(buf.params()));
495                 } else if (tmptok == "listings") {
496                         inset.reset(new InsetListings(buf.params()));
497                 } else if (tmptok == "InsetSpace") {
498                         inset.reset(new InsetSpace);
499                 } else if (tmptok == "Tabular") {
500                         inset.reset(new InsetTabular(buf));
501                 } else if (tmptok == "Text") {
502                         inset.reset(new InsetText(buf.params()));
503                 } else if (tmptok == "VSpace") {
504                         inset.reset(new InsetVSpace);
505                 } else if (tmptok == "Foot") {
506                         inset.reset(new InsetFoot(buf.params()));
507                 } else if (tmptok == "Marginal") {
508                         inset.reset(new InsetMarginal(buf.params()));
509                 } else if (tmptok == "OptArg") {
510                         inset.reset(new InsetOptArg(buf.params()));
511                 } else if (tmptok == "Float") {
512                         lex.next();
513                         string tmptok = lex.getString();
514                         inset.reset(new InsetFloat(buf.params(), tmptok));
515                 } else if (tmptok == "Wrap") {
516                         lex.next();
517                         string tmptok = lex.getString();
518                         inset.reset(new InsetWrap(buf.params(), tmptok));
519 #if 0
520                 } else if (tmptok == "Theorem") {
521                         inset.reset(new InsetList);
522 #endif
523                 } else if (tmptok == "Caption") {
524                         inset.reset(new InsetCaption(buf.params()));
525                 } else if (tmptok == "Index") {
526                         inset.reset(new InsetIndex(buf.params()));
527                 } else if (tmptok == "FloatList") {
528                         inset.reset(new InsetFloatList);
529                 } else if (tmptok == "Info") {
530                         inset.reset(new InsetInfo(buf.params()));
531                 } else {
532                         lyxerr << "unknown Inset type '" << tmptok
533                                << "'" << std::endl;
534                         while (lex.isOK() && lex.getString() != "\\end_inset")
535                                 lex.next();
536                         return 0;
537                 }
538
539                 inset->read(buf, lex);
540         }
541
542         return inset.release();
543 }
544
545
546 } // namespace lyx