]> git.lyx.org Git - lyx.git/blob - src/factory.C
fix crash when collapsing ert with cursor inside
[lyx.git] / src / factory.C
1 /**
2  * \file factory.C
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 "LColor.h"
22 #include "lyxlex.h"
23 #include "paragraph.h"
24
25 #include "insets/insetbibitem.h"
26 #include "insets/insetbibtex.h"
27 #include "insets/insetcaption.h"
28 #include "insets/insetcite.h"
29 #include "insets/insetcharstyle.h"
30 #include "insets/insetenv.h"
31 #include "insets/insetert.h"
32 #include "insets/insetexternal.h"
33 #include "insets/insetfloat.h"
34 #include "insets/insetfloatlist.h"
35 #include "insets/insetfoot.h"
36 #include "insets/insetgraphics.h"
37 #include "insets/insethfill.h"
38 #include "insets/insetinclude.h"
39 #include "insets/insetindex.h"
40 #include "insets/insetlabel.h"
41 #include "insets/insetline.h"
42 #include "insets/insetmarginal.h"
43 #include "insets/insetnote.h"
44 #include "insets/insetbox.h"
45 #include "insets/insetbranch.h"
46 #include "insets/insetoptarg.h"
47 #include "insets/insetpagebreak.h"
48 #include "insets/insetref.h"
49 #include "insets/insetspace.h"
50 #include "insets/insettabular.h"
51 #include "insets/insettoc.h"
52 #include "insets/inseturl.h"
53 #include "insets/insetvspace.h"
54 #include "insets/insetwrap.h"
55
56 #include "mathed/math_macrotemplate.h"
57 #include "mathed/math_hullinset.h"
58
59 #include "frontends/Dialogs.h"
60 #include "frontends/LyXView.h"
61
62 #include "support/lstrings.h"
63
64 #include <boost/assert.hpp>
65
66 #include <sstream>
67
68 using lyx::support::compare_ascii_no_case;
69
70 using std::auto_ptr;
71 using std::endl;
72 using std::string;
73
74
75 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
76 {
77         BufferParams const & params = bv->buffer()->params();
78
79         switch (cmd.action) {
80         case LFUN_HFILL:
81                 return new InsetHFill;
82
83         case LFUN_INSERT_LINE:
84                 return new InsetLine;
85
86         case LFUN_INSERT_PAGEBREAK:
87                 return new InsetPagebreak;
88
89         case LFUN_INSERT_CHARSTYLE: {
90                 string s = cmd.getArg(0);
91                 LyXTextClass tclass = params.getLyXTextClass();
92                 CharStyles::iterator found_cs = tclass.charstyle(s);
93                 if (found_cs != tclass.charstyles().end())
94                         return new InsetCharStyle(params, found_cs);
95                 else
96                         return new InsetCharStyle(params, s);
97         }
98
99         case LFUN_INSERT_NOTE: {
100                 string arg = cmd.getArg(0);
101                 if (arg.empty())
102                         arg = "Note";
103                 return new InsetNote(params, arg);
104         }
105
106         case LFUN_INSERT_BOX: {
107                 string arg = cmd.getArg(0);
108                 if (arg.empty())
109                         arg = "Boxed";
110                 return new InsetBox(params, arg);
111         }
112
113         case LFUN_INSERT_BRANCH: {
114                 string arg = cmd.getArg(0);
115                 if (arg.empty())
116                         arg = "none";
117                 return new InsetBranch(params, InsetBranchParams(arg));
118         }
119
120         case LFUN_INSET_ERT:
121                 return new InsetERT(params);
122
123         case LFUN_INSET_FOOTNOTE:
124                 return new InsetFoot(params);
125
126         case LFUN_INSET_MARGINAL:
127                 return new InsetMarginal(params);
128
129         case LFUN_INSET_OPTARG:
130                 return new InsetOptArg(params);
131
132         case LFUN_INSERT_BIBITEM:
133                 return new InsetBibitem(InsetCommandParams("bibitem"));
134
135         case LFUN_INSET_FLOAT:
136                 // check if the float type exists
137                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
138                         return new InsetFloat(params, cmd.argument);
139                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
140                 return 0;
141
142         case LFUN_INSET_WIDE_FLOAT:
143                 // check if the float type exists
144                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
145                         auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
146                         p->wide(true, params);
147                         return p.release();
148                 }
149                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
150                 return 0;
151
152         case LFUN_INSET_WRAP:
153                 if (cmd.argument == "figure")
154                         return new InsetWrap(params, cmd.argument);
155                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
156                 return 0;
157
158         case LFUN_INDEX_INSERT: {
159                 // Try and generate a valid index entry.
160                 InsetCommandParams icp("index");
161                 string const contents = cmd.argument.empty() ?
162                         bv->getLyXText()->getStringToIndex(bv->cursor()) :
163                         cmd.argument;
164                 icp.setContents(contents);
165
166                 string data = InsetCommandMailer::params2string("index", icp);
167                 LyXView * lv = bv->owner();
168
169                 if (icp.getContents().empty()) {
170                         lv->getDialogs().show("index", data, 0);
171                 } else {
172                         lv->dispatch(FuncRequest(LFUN_INSET_APPLY, data));
173                 }
174                 return 0;
175         }
176
177         case LFUN_TABULAR_INSERT:
178                 if (!cmd.argument.empty()) {
179                         std::istringstream ss(cmd.argument);
180                         int r = 0, c = 0;
181                         ss >> r >> c;
182                         if (r <= 0) r = 2;
183                         if (c <= 0) c = 2;
184                         return new InsetTabular(*bv->buffer(), r, c);
185                 }
186                 bv->owner()->getDialogs().show("tabularcreate");
187                 return 0;
188
189         case LFUN_INSET_CAPTION: {
190                 UpdatableInset * up = bv->cursor().inset().asUpdatableInset();
191                 if (!up) {
192                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
193                         inset->setAutoBreakRows(true);
194                         inset->setDrawFrame(true);
195                         inset->setFrameColor(LColor::captionframe);
196                         return inset.release();
197                 }
198                 return 0;
199         }
200
201         case LFUN_INDEX_PRINT:
202                 return new InsetPrintIndex(InsetCommandParams("printindex"));
203
204         case LFUN_TOC_INSERT:
205                 return new InsetTOC(InsetCommandParams("tableofcontents"));
206
207         case LFUN_ENVIRONMENT_INSERT:
208                 return new InsetEnvironment(params, cmd.argument);
209
210 #if 0
211         case LFUN_INSET_LIST:
212                 return new InsetList;
213
214         case LFUN_INSET_THEOREM:
215                 return new InsetTheorem;
216 #endif
217
218         case LFUN_INSET_INSERT: {
219                 string const name = cmd.getArg(0);
220
221                 if (name == "bibitem") {
222                         InsetCommandParams icp;
223                         InsetCommandMailer::string2params(name, cmd.argument,
224                                                           icp);
225                         return new InsetBibitem(icp);
226
227                 } else if (name == "bibtex") {
228                         InsetCommandParams icp;
229                         InsetCommandMailer::string2params(name, cmd.argument,
230                                                           icp);
231                         return new InsetBibtex(icp);
232
233                 } else if (name == "citation") {
234                         InsetCommandParams icp;
235                         InsetCommandMailer::string2params(name, cmd.argument,
236                                                           icp);
237                         return new InsetCitation(icp);
238
239                 } else if (name == "ert") {
240                         InsetCollapsable::CollapseStatus st;
241                         InsetERTMailer::string2params(cmd.argument, st);
242                         return new InsetERT(params, st);
243
244                 } else if (name == "external") {
245                         Buffer const & buffer = *bv->buffer();
246                         InsetExternalParams iep;
247                         InsetExternalMailer::string2params(cmd.argument,
248                                                            buffer, iep);
249                         auto_ptr<InsetExternal> inset(new InsetExternal);
250                         inset->setParams(iep, buffer);
251                         return inset.release();
252
253                 } else if (name == "graphics") {
254                         Buffer const & buffer = *bv->buffer();
255                         InsetGraphicsParams igp;
256                         InsetGraphicsMailer::string2params(cmd.argument,
257                                                            buffer, igp);
258                         auto_ptr<InsetGraphics> inset(new InsetGraphics);
259                         inset->setParams(igp);
260                         return inset.release();
261
262                 } else if (name == "include") {
263                         InsetCommandParams iip;
264                         InsetIncludeMailer::string2params(cmd.argument, iip);
265                         return new InsetInclude(iip);
266
267                 } else if (name == "index") {
268                         InsetCommandParams icp;
269                         InsetCommandMailer::string2params(name, cmd.argument,
270                                                           icp);
271                         return new InsetIndex(icp);
272
273                 } else if (name == "label") {
274                         InsetCommandParams icp;
275                         InsetCommandMailer::string2params(name, cmd.argument,
276                                                           icp);
277                         return new InsetLabel(icp);
278
279                 } else if (name == "ref") {
280                         InsetCommandParams icp;
281                         InsetCommandMailer::string2params(name, cmd.argument,
282                                                           icp);
283                         return new InsetRef(icp, *bv->buffer());
284
285                 } else if (name == "toc") {
286                         InsetCommandParams icp;
287                         InsetCommandMailer::string2params(name, cmd.argument,
288                                                           icp);
289                         return new InsetTOC(icp);
290
291                 } else if (name == "url") {
292                         InsetCommandParams icp;
293                         InsetCommandMailer::string2params(name, cmd.argument,
294                                                           icp);
295                         return new InsetUrl(icp);
296
297                 } else if (name == "vspace") {
298                         VSpace vspace;
299                         InsetVSpaceMailer::string2params(cmd.argument, vspace);
300                         return new InsetVSpace(vspace);
301                 }
302         }
303
304         case LFUN_SPACE_INSERT: {
305                 string const name = cmd.argument;
306                 if (name == "normal")
307                         return new InsetSpace(InsetSpace::NORMAL);
308                 else if (name == "protected")
309                         return new InsetSpace(InsetSpace::PROTECTED);
310                 else if (name == "thin")
311                         return new InsetSpace(InsetSpace::THIN);
312                 else if (name == "quad")
313                         return new InsetSpace(InsetSpace::QUAD);
314                 else if (name == "qquad")
315                         return new InsetSpace(InsetSpace::QQUAD);
316                 else if (name == "enspace")
317                         return new InsetSpace(InsetSpace::ENSPACE);
318                 else if (name == "enskip")
319                         return new InsetSpace(InsetSpace::ENSKIP);
320                 else if (name == "negthinspace")
321                         return new InsetSpace(InsetSpace::NEGTHIN);
322                 else if (name.empty())
323                         lyxerr << "LyX function 'space' needs an argument." << endl;
324                 else
325                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
326         }
327
328         break;
329
330         default:
331                 break;
332         }
333
334         return 0;
335 }
336
337
338 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
339 {
340         // consistency check
341         if (lex.getString() != "\\begin_inset") {
342                 lyxerr << "Buffer::readInset: Consistency check failed."
343                        << endl;
344         }
345
346         auto_ptr<InsetBase> inset;
347
348         LyXTextClass tclass = buf.params().getLyXTextClass();
349
350         lex.next();
351         string tmptok = lex.getString();
352
353         // test the different insets
354         if (tmptok == "LatexCommand") {
355                 InsetCommandParams inscmd;
356                 inscmd.read(lex);
357
358                 string const cmdName = inscmd.getCmdName();
359
360                 // This strange command allows LyX to recognize "natbib" style
361                 // citations: citet, citep, Citet etc.
362                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
363                         inset.reset(new InsetCitation(inscmd));
364                 } else if (cmdName == "bibitem") {
365                         lex.printError("Wrong place for bibitem");
366                         inset.reset(new InsetBibitem(inscmd));
367                 } else if (cmdName == "bibtex") {
368                         inset.reset(new InsetBibtex(inscmd));
369                 } else if (cmdName == "index") {
370                         inset.reset(new InsetIndex(inscmd));
371                 } else if (cmdName == "include") {
372                         inset.reset(new InsetInclude(inscmd));
373                 } else if (cmdName == "label") {
374                         inset.reset(new InsetLabel(inscmd));
375                 } else if (cmdName == "url"
376                            || cmdName == "htmlurl") {
377                         inset.reset(new InsetUrl(inscmd));
378                 } else if (cmdName == "ref"
379                            || cmdName == "eqref"
380                            || cmdName == "pageref"
381                            || cmdName == "vref"
382                            || cmdName == "vpageref"
383                            || cmdName == "prettyref") {
384                         if (!inscmd.getOptions().empty()
385                             || !inscmd.getContents().empty()) {
386                                 inset.reset(new InsetRef(inscmd, buf));
387                         }
388                 } else if (cmdName == "tableofcontents") {
389                         inset.reset(new InsetTOC(inscmd));
390                 } else if (cmdName == "listofalgorithms") {
391                         inset.reset(new InsetFloatList("algorithm"));
392                 } else if (cmdName == "listoffigures") {
393                         inset.reset(new InsetFloatList("figure"));
394                 } else if (cmdName == "listoftables") {
395                         inset.reset(new InsetFloatList("table"));
396                 } else if (cmdName == "printindex") {
397                         inset.reset(new InsetPrintIndex(inscmd));
398                 } else {
399                         lyxerr << "unknown CommandInset '" << cmdName
400                                << "'" << std::endl;
401                         while (lex.isOK() && lex.getString() != "\\end_inset")
402                                 lex.next();
403                         return 0;
404                 }
405         } else {
406                 if (tmptok == "Quotes") {
407                         inset.reset(new InsetQuotes);
408                 } else if (tmptok == "External") {
409                         inset.reset(new InsetExternal);
410                 } else if (tmptok == "FormulaMacro") {
411                         inset.reset(new MathMacroTemplate);
412                 } else if (tmptok == "Formula") {
413                         inset.reset(new MathHullInset);
414                 } else if (tmptok == "Graphics") {
415                         inset.reset(new InsetGraphics);
416                 } else if (tmptok == "Note") {
417                         inset.reset(new InsetNote(buf.params(), tmptok));
418                 } else if (tmptok == "Box") {
419                         inset.reset(new InsetBox(buf.params(), tmptok));
420                 } else if (tmptok == "CharStyle") {
421                         lex.next();
422                         string s = lex.getString();
423                         CharStyles::iterator found_cs = tclass.charstyle(s);
424                         if (found_cs != tclass.charstyles().end())
425                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
426                         else {
427                                 // "Undefined" inset
428                                 inset.reset(new InsetCharStyle(buf.params(), s));
429                         }
430                 } else if (tmptok == "Branch") {
431                         inset.reset(new InsetBranch(buf.params(),
432                                                     InsetBranchParams()));
433                 } else if (tmptok == "Include") {
434                         InsetCommandParams p("Include");
435                         inset.reset(new InsetInclude(p));
436                 } else if (tmptok == "Environment") {
437                         lex.next();
438                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
439                 } else if (tmptok == "ERT") {
440                         inset.reset(new InsetERT(buf.params()));
441                 } else if (tmptok == "InsetSpace") {
442                         inset.reset(new InsetSpace);
443                 } else if (tmptok == "Tabular") {
444                         inset.reset(new InsetTabular(buf));
445                 } else if (tmptok == "Text") {
446                         inset.reset(new InsetText(buf.params()));
447                 } else if (tmptok == "VSpace") {
448                         inset.reset(new InsetVSpace);
449                 } else if (tmptok == "Foot") {
450                         inset.reset(new InsetFoot(buf.params()));
451                 } else if (tmptok == "Marginal") {
452                         inset.reset(new InsetMarginal(buf.params()));
453                 } else if (tmptok == "OptArg") {
454                         inset.reset(new InsetOptArg(buf.params()));
455                 } else if (tmptok == "Float") {
456                         lex.next();
457                         string tmptok = lex.getString();
458                         inset.reset(new InsetFloat(buf.params(), tmptok));
459                 } else if (tmptok == "Wrap") {
460                         lex.next();
461                         string tmptok = lex.getString();
462                         inset.reset(new InsetWrap(buf.params(), tmptok));
463 #if 0
464                 } else if (tmptok == "List") {
465                         inset.reset(new InsetList);
466                 } else if (tmptok == "Theorem") {
467                         inset.reset(new InsetList);
468 #endif
469                 } else if (tmptok == "Caption") {
470                         inset.reset(new InsetCaption(buf.params()));
471                 } else if (tmptok == "FloatList") {
472                         inset.reset(new InsetFloatList);
473                 } else {
474                         lyxerr << "unknown Inset type '" << tmptok
475                                << "'" << std::endl;
476                         while (lex.isOK() && lex.getString() != "\\end_inset")
477                                 lex.next();
478                         return 0;
479                 }
480
481                 inset->read(buf, lex);
482
483 #ifdef WITH_WARNINGS
484 #warning hack..
485 #endif
486                 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
487                         MathMacroTemplate const * tmpl =
488                                 static_cast<MathMacroTemplate*>(inset.get());
489                         MacroTable::globalMacros().insert
490                                 (tmpl->name(), tmpl->asMacroData());
491                         lyxerr << "creating local macro " << tmpl->name() << endl;
492                 }
493         }
494
495         return inset.release();
496 }