]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Rename .C ==> .cpp for files in src/support, part two
[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 "LColor.h"
22 #include "LyXLex.h"
23 #include "LyX.h"
24 #include "Paragraph.h"
25
26 #include "insets/InsetBibitem.h"
27 #include "insets/InsetBibtex.h"
28 #include "insets/InsetCaption.h"
29 #include "insets/InsetCitation.h"
30 #include "insets/InsetCharStyle.h"
31 #include "insets/InsetEnv.h"
32 #include "insets/InsetERT.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
72 namespace lyx {
73
74 namespace Alert = frontend::Alert;
75
76 using support::compare_ascii_no_case;
77
78 using std::auto_ptr;
79 using std::endl;
80 using std::string;
81
82
83 InsetBase * 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_CHARSTYLE_INSERT: {
106                         string s = cmd.getArg(0);
107                         LyXTextClass tclass = params.getLyXTextClass();
108                         CharStyles::iterator found_cs = tclass.charstyle(s);
109                         if (found_cs != tclass.charstyles().end())
110                                 return new InsetCharStyle(params, found_cs);
111                         else
112                                 return new InsetCharStyle(params, s);
113                 }
114
115                 case LFUN_NOTE_INSERT: {
116                         string arg = cmd.getArg(0);
117                         if (arg.empty())
118                                 arg = "Note";
119                         return new InsetNote(params, arg);
120                 }
121
122                 case LFUN_BOX_INSERT: {
123                         string arg = cmd.getArg(0);
124                         if (arg.empty())
125                                 arg = "Boxed";
126                         return new InsetBox(params, arg);
127                 }
128
129                 case LFUN_BRANCH_INSERT: {
130                         docstring arg = cmd.argument();
131                         if (arg.empty())
132                                 arg = from_ascii("none");
133                         return new InsetBranch(params, InsetBranchParams(arg));
134                 }
135
136                 case LFUN_ERT_INSERT:
137                         return new InsetERT(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.getLyXTextClass().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.getLyXTextClass().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 floatflt 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(LColor::captionframe);
215                         return inset.release();
216                 }
217
218                 case LFUN_INDEX_PRINT:
219                         return new InsetPrintIndex(InsetCommandParams("printindex"));
220
221                 case LFUN_NOMENCL_PRINT:
222                         return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
223
224                 case LFUN_TOC_INSERT:
225                         return new InsetTOC(InsetCommandParams("tableofcontents"));
226
227                 case LFUN_ENVIRONMENT_INSERT:
228                         return new InsetEnvironment(params, to_utf8(cmd.argument()));
229
230 #if 0
231                 case LFUN_LIST_INSERT:
232                         return new InsetList;
233
234                 case LFUN_THEOREM_INSERT:
235                         return new InsetTheorem;
236 #endif
237
238                 case LFUN_INSET_INSERT: {
239                         string const name = cmd.getArg(0);
240
241                         if (name == "bibitem") {
242                                 InsetCommandParams icp(name);
243                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
244                                         icp);
245                                 return new InsetBibitem(icp);
246
247                         } else if (name == "bibtex") {
248                                 InsetCommandParams icp(name);
249                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
250                                         icp);
251                                 return new InsetBibtex(icp);
252
253                         } else if (name == "citation") {
254                                 InsetCommandParams icp("cite");
255                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
256                                         icp);
257                                 return new InsetCitation(icp);
258
259                         } else if (name == "ert") {
260                                 InsetCollapsable::CollapseStatus st;
261                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
262                                 return new InsetERT(params, st);
263
264                         } else if (name == "external") {
265                                 Buffer const & buffer = *bv->buffer();
266                                 InsetExternalParams iep;
267                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
268                                         buffer, iep);
269                                 auto_ptr<InsetExternal> inset(new InsetExternal);
270                                 inset->setParams(iep, buffer);
271                                 return inset.release();
272
273                         } else if (name == "graphics") {
274                                 Buffer const & buffer = *bv->buffer();
275                                 InsetGraphicsParams igp;
276                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
277                                         buffer, igp);
278                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
279                                 inset->setParams(igp);
280                                 return inset.release();
281
282                         } else if (name == "include") {
283                                 InsetCommandParams iip(name);
284                                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
285                                 return new InsetInclude(iip);
286
287                         } else if (name == "index") {
288                                 InsetCommandParams icp(name);
289                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
290                                         icp);
291                                 return new InsetIndex(icp);
292
293                         } else if (name == "nomenclature") {
294                                 InsetCommandParams icp(name);
295                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
296                                         icp);
297                                 return new InsetNomencl(icp);
298
299                         } else if (name == "label") {
300                                 InsetCommandParams icp(name);
301                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
302                                         icp);
303                                 return new InsetLabel(icp);
304
305                         } else if (name == "ref") {
306                                 InsetCommandParams icp(name);
307                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
308                                         icp);
309                                 return new InsetRef(icp, *bv->buffer());
310
311                         } else if (name == "toc") {
312                                 InsetCommandParams icp("tableofcontents");
313                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
314                                         icp);
315                                 return new InsetTOC(icp);
316
317                         } else if (name == "url") {
318                                 InsetCommandParams icp(name);
319                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
320                                         icp);
321                                 return new InsetUrl(icp);
322
323                         } else if (name == "vspace") {
324                                 VSpace vspace;
325                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
326                                 return new InsetVSpace(vspace);
327                         }
328                 }
329
330                 case LFUN_SPACE_INSERT: {
331                         string const name = to_utf8(cmd.argument());
332                         if (name == "normal")
333                                 return new InsetSpace(InsetSpace::NORMAL);
334                         else if (name == "protected")
335                                 return new InsetSpace(InsetSpace::PROTECTED);
336                         else if (name == "thin")
337                                 return new InsetSpace(InsetSpace::THIN);
338                         else if (name == "quad")
339                                 return new InsetSpace(InsetSpace::QUAD);
340                         else if (name == "qquad")
341                                 return new InsetSpace(InsetSpace::QQUAD);
342                         else if (name == "enspace")
343                                 return new InsetSpace(InsetSpace::ENSPACE);
344                         else if (name == "enskip")
345                                 return new InsetSpace(InsetSpace::ENSKIP);
346                         else if (name == "negthinspace")
347                                 return new InsetSpace(InsetSpace::NEGTHIN);
348                         else if (name.empty())
349                                 lyxerr << "LyX function 'space' needs an argument." << endl;
350                         else
351                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
352                 }
353                 break;
354
355                 default:
356                         break;
357                 }
358
359         } catch (support::ExceptionMessage const & message) {
360                 if (message.type_ == support::ErrorException) {
361                         Alert::error(message.title_, message.details_);
362                         LyX::cref().emergencyCleanup();
363                         abort();
364                 } else if (message.type_ == support::WarningException) {
365                         Alert::warning(message.title_, message.details_);
366                         return 0;
367                 }
368         }
369
370
371         return 0;
372 }
373
374
375 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
376 {
377         // consistency check
378         if (lex.getString() != "\\begin_inset") {
379                 lyxerr << "Buffer::readInset: Consistency check failed."
380                        << endl;
381         }
382
383         auto_ptr<InsetBase> inset;
384
385         LyXTextClass tclass = buf.params().getLyXTextClass();
386
387         lex.next();
388         string tmptok = lex.getString();
389
390         // test the different insets
391         if (tmptok == "LatexCommand") {
392                 lex.next();
393                 string const cmdName = lex.getString();
394                 lex.pushToken(cmdName);
395
396                 InsetCommandParams inscmd(cmdName);
397                 inscmd.read(lex);
398
399                 // This strange command allows LyX to recognize "natbib" style
400                 // citations: citet, citep, Citet etc.
401                 // FIXME: We already have partial support for \\fullcite and
402                 // the various \\footcite commands. We should increase the
403                 // file format number and read these commands here, too.
404                 // Then we should use is_possible_cite_command() in
405                 // src/frontends/controllers/frontend_helpers.cpp to test for valid cite
406                 // commands.
407                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
408                         inset.reset(new InsetCitation(inscmd));
409                 } else if (cmdName == "bibitem") {
410                         inset.reset(new InsetBibitem(inscmd));
411                 } else if (cmdName == "bibtex") {
412                         inset.reset(new InsetBibtex(inscmd));
413                 } else if (cmdName == "index") {
414                         inset.reset(new InsetIndex(inscmd));
415                 } else if (cmdName == "nomenclature") {
416                         inset.reset(new InsetNomencl(inscmd));
417                 } else if (cmdName == "include") {
418                         inset.reset(new InsetInclude(inscmd));
419                 } else if (cmdName == "label") {
420                         inset.reset(new InsetLabel(inscmd));
421                 } else if (cmdName == "url"
422                            || cmdName == "htmlurl") {
423                         inset.reset(new InsetUrl(inscmd));
424                 } else if (cmdName == "ref"
425                            || cmdName == "eqref"
426                            || cmdName == "pageref"
427                            || cmdName == "vref"
428                            || cmdName == "vpageref"
429                            || cmdName == "prettyref") {
430                         if (!inscmd["name"].empty()
431                             || !inscmd["reference"].empty()) {
432                                 inset.reset(new InsetRef(inscmd, buf));
433                         }
434                 } else if (cmdName == "tableofcontents") {
435                         inset.reset(new InsetTOC(inscmd));
436                 } else if (cmdName == "listofalgorithms") {
437                         inset.reset(new InsetFloatList("algorithm"));
438                 } else if (cmdName == "listoffigures") {
439                         inset.reset(new InsetFloatList("figure"));
440                 } else if (cmdName == "listoftables") {
441                         inset.reset(new InsetFloatList("table"));
442                 } else if (cmdName == "printindex") {
443                         inset.reset(new InsetPrintIndex(inscmd));
444                 } else if (cmdName == "printnomenclature") {
445                         inset.reset(new InsetPrintNomencl(inscmd));
446                 } else {
447                         lyxerr << "unknown CommandInset '" << cmdName
448                                << "'" << std::endl;
449                         while (lex.isOK() && lex.getString() != "\\end_inset")
450                                 lex.next();
451                         return 0;
452                 }
453         } else {
454                 if (tmptok == "Quotes") {
455                         inset.reset(new InsetQuotes);
456                 } else if (tmptok == "External") {
457                         inset.reset(new InsetExternal);
458                 } else if (tmptok == "FormulaMacro") {
459                         inset.reset(new MathMacroTemplate);
460                 } else if (tmptok == "Formula") {
461                         inset.reset(new InsetMathHull);
462                 } else if (tmptok == "Graphics") {
463                         inset.reset(new InsetGraphics);
464                 } else if (tmptok == "Note") {
465                         inset.reset(new InsetNote(buf.params(), tmptok));
466                 } else if (tmptok == "Box") {
467                         inset.reset(new InsetBox(buf.params(), tmptok));
468                 } else if (tmptok == "CharStyle") {
469                         lex.next();
470                         string s = lex.getString();
471                         CharStyles::iterator found_cs = tclass.charstyle(s);
472                         if (found_cs != tclass.charstyles().end())
473                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
474                         else {
475                                 // "Undefined" inset
476                                 inset.reset(new InsetCharStyle(buf.params(), s));
477                         }
478                 } else if (tmptok == "Branch") {
479                         inset.reset(new InsetBranch(buf.params(),
480                                                     InsetBranchParams()));
481                 } else if (tmptok == "Include") {
482                         InsetCommandParams p("include");
483                         inset.reset(new InsetInclude(p));
484                 } else if (tmptok == "Environment") {
485                         lex.next();
486                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
487                 } else if (tmptok == "ERT") {
488                         inset.reset(new InsetERT(buf.params()));
489                 } else if (tmptok == "InsetSpace") {
490                         inset.reset(new InsetSpace);
491                 } else if (tmptok == "Tabular") {
492                         inset.reset(new InsetTabular(buf));
493                 } else if (tmptok == "Text") {
494                         inset.reset(new InsetText(buf.params()));
495                 } else if (tmptok == "VSpace") {
496                         inset.reset(new InsetVSpace);
497                 } else if (tmptok == "Foot") {
498                         inset.reset(new InsetFoot(buf.params()));
499                 } else if (tmptok == "Marginal") {
500                         inset.reset(new InsetMarginal(buf.params()));
501                 } else if (tmptok == "OptArg") {
502                         inset.reset(new InsetOptArg(buf.params()));
503                 } else if (tmptok == "Float") {
504                         lex.next();
505                         string tmptok = lex.getString();
506                         inset.reset(new InsetFloat(buf.params(), tmptok));
507                 } else if (tmptok == "Wrap") {
508                         lex.next();
509                         string tmptok = lex.getString();
510                         inset.reset(new InsetWrap(buf.params(), tmptok));
511 #if 0
512                 } else if (tmptok == "List") {
513                         inset.reset(new InsetList);
514                 } else if (tmptok == "Theorem") {
515                         inset.reset(new InsetList);
516 #endif
517                 } else if (tmptok == "Caption") {
518                         inset.reset(new InsetCaption(buf.params()));
519                 } else if (tmptok == "FloatList") {
520                         inset.reset(new InsetFloatList);
521                 } else {
522                         lyxerr << "unknown Inset type '" << tmptok
523                                << "'" << std::endl;
524                         while (lex.isOK() && lex.getString() != "\\end_inset")
525                                 lex.next();
526                         return 0;
527                 }
528
529                 inset->read(buf, lex);
530
531 #ifdef WITH_WARNINGS
532 #warning hack..
533 #endif
534                 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
535                         MathMacroTemplate const * tmpl =
536                                 static_cast<MathMacroTemplate*>(inset.get());
537                         MacroTable::globalMacros().insert
538                                 (tmpl->name(), tmpl->asMacroData());
539                         LYXERR(Debug::DEBUG)
540                                 << BOOST_CURRENT_FUNCTION
541                                 << ": creating local macro " << to_utf8(tmpl->name())
542                                 << endl;
543                 }
544         }
545
546         return inset.release();
547 }
548
549
550 } // namespace lyx