]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Fixed some lines that were too long. It compiled afterwards.
[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/InsetCharStyle.h"
30 #include "insets/InsetEnvironment.h"
31 #include "insets/InsetERT.h"
32 #include "insets/InsetListings.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetFloatList.h"
36 #include "insets/InsetFoot.h"
37 #include "insets/InsetGraphics.h"
38 #include "insets/InsetHFill.h"
39 #include "insets/InsetInclude.h"
40 #include "insets/InsetIndex.h"
41 #include "insets/InsetNomencl.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPagebreak.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetUrl.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
57
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
60
61 #include "frontends/alert.h"
62
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
65
66 #include <boost/assert.hpp>
67 #include <boost/current_function.hpp>
68
69 #include <sstream>
70
71 using std::auto_ptr;
72 using std::endl;
73 using std::string;
74
75
76 namespace lyx {
77
78 namespace Alert = frontend::Alert;
79
80 using support::compare_ascii_no_case;
81
82
83 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
84 {
85         BufferParams const & params = bv->buffer()->params();
86
87         try {
88
89                 switch (cmd.action) {
90                 case LFUN_HFILL_INSERT:
91                         return new InsetHFill;
92
93                 case LFUN_LINE_INSERT:
94                         return new InsetLine;
95
96                 case LFUN_PAGEBREAK_INSERT:
97                         return new InsetPagebreak;
98
99                 case LFUN_CLEARPAGE_INSERT:
100                         return new InsetClearPage;
101
102                 case LFUN_CLEARDOUBLEPAGE_INSERT:
103                         return new InsetClearDoublePage;
104
105                 case LFUN_CHARSTYLE_INSERT: {
106                         string s = cmd.getArg(0);
107                         TextClass tclass = params.getTextClass();
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_LISTING_INSERT:
140                         return new InsetListings(params);
141
142                 case LFUN_FOOTNOTE_INSERT:
143                         return new InsetFoot(params);
144
145                 case LFUN_MARGINALNOTE_INSERT:
146                         return new InsetMarginal(params);
147
148                 case LFUN_OPTIONAL_INSERT:
149                         return new InsetOptArg(params);
150
151                 case LFUN_BIBITEM_INSERT:
152                         return new InsetBibitem(InsetCommandParams("bibitem"));
153
154                 case LFUN_FLOAT_INSERT: {
155                         // check if the float type exists
156                         string const argument = to_utf8(cmd.argument());
157                         if (params.getTextClass().floats().typeExist(argument))
158                                 return new InsetFloat(params, argument);
159                         lyxerr << "Non-existent float type: " << argument << endl;
160                         return 0;
161                 }
162
163                 case LFUN_FLOAT_WIDE_INSERT: {
164                         // check if the float type exists
165                         string const argument = to_utf8(cmd.argument());
166                         if (params.getTextClass().floats().typeExist(argument)) {
167                                 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
168                                 p->wide(true, params);
169                                 return p.release();
170                         }
171                         lyxerr << "Non-existent float type: " << argument << endl;
172                         return 0;
173                 }
174
175                 case LFUN_WRAP_INSERT: {
176                         string const argument = to_utf8(cmd.argument());
177                         if (argument == "figure")
178                                 return new InsetWrap(params, argument);
179                         lyxerr << "Non-existent floatflt type: " << argument << endl;
180                         return 0;
181                 }
182
183                 case LFUN_INDEX_INSERT: {
184                         // Try and generate a valid index entry.
185                         InsetCommandParams icp("index");
186                         icp["name"] = cmd.argument().empty() ?
187                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
188                                 cmd.argument();
189                         return new InsetIndex(icp);
190                 }
191
192                 case LFUN_NOMENCL_INSERT: {
193                         InsetCommandParams icp("nomenclature");
194                         icp["symbol"] = cmd.argument().empty() ?
195                                 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
196                                 cmd.argument();
197                         return new InsetNomencl(icp);
198                 }
199
200                 case LFUN_TABULAR_INSERT: {
201                         if (cmd.argument().empty())
202                                 return 0;
203                         std::istringstream ss(to_utf8(cmd.argument()));
204                         int r = 0, c = 0;
205                         ss >> r >> c;
206                         if (r <= 0)
207                                 r = 2;
208                         if (c <= 0)
209                                 c = 2;
210                         return new InsetTabular(*bv->buffer(), r, c);
211                 }
212
213                 case LFUN_CAPTION_INSERT: {
214                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
215                         inset->setAutoBreakRows(true);
216                         inset->setDrawFrame(true);
217                         inset->setFrameColor(Color::captionframe);
218                         return inset.release();
219                 }
220
221                 case LFUN_INDEX_PRINT:
222                         return new InsetPrintIndex(InsetCommandParams("printindex"));
223
224                 case LFUN_NOMENCL_PRINT:
225                         return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
226
227                 case LFUN_TOC_INSERT:
228                         return new InsetTOC(InsetCommandParams("tableofcontents"));
229
230                 case LFUN_ENVIRONMENT_INSERT:
231                         return new InsetEnvironment(params, cmd.argument());
232
233 #if 0
234                 case LFUN_LIST_INSERT:
235                         return new InsetList;
236
237                 case LFUN_THEOREM_INSERT:
238                         return new InsetTheorem;
239 #endif
240
241                 case LFUN_INSET_INSERT: {
242                         string const name = cmd.getArg(0);
243
244                         if (name == "bibitem") {
245                                 InsetCommandParams icp(name);
246                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
247                                         icp);
248                                 return new InsetBibitem(icp);
249
250                         } else if (name == "bibtex") {
251                                 InsetCommandParams icp(name);
252                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
253                                         icp);
254                                 return new InsetBibtex(icp);
255
256                         } else if (name == "citation") {
257                                 InsetCommandParams icp("cite");
258                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
259                                         icp);
260                                 return new InsetCitation(icp);
261
262                         } else if (name == "ert") {
263                                 InsetCollapsable::CollapseStatus st;
264                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
265                                 return new InsetERT(params, st);
266
267                         } else if (name == "listings") {
268                                 InsetListingsParams par;
269                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
270                                 return new InsetListings(params, par);
271
272                         } else if (name == "external") {
273                                 Buffer const & buffer = *bv->buffer();
274                                 InsetExternalParams iep;
275                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
276                                         buffer, iep);
277                                 auto_ptr<InsetExternal> inset(new InsetExternal);
278                                 inset->setParams(iep, buffer);
279                                 return inset.release();
280
281                         } else if (name == "graphics") {
282                                 Buffer const & buffer = *bv->buffer();
283                                 InsetGraphicsParams igp;
284                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
285                                         buffer, igp);
286                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
287                                 inset->setParams(igp);
288                                 return inset.release();
289
290                         } else if (name == "include") {
291                                 InsetCommandParams iip(name);
292                                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
293                                 return new InsetInclude(iip);
294
295                         } else if (name == "index") {
296                                 InsetCommandParams icp(name);
297                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
298                                         icp);
299                                 return new InsetIndex(icp);
300
301                         } else if (name == "nomenclature") {
302                                 InsetCommandParams icp(name);
303                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
304                                         icp);
305                                 return new InsetNomencl(icp);
306
307                         } else if (name == "label") {
308                                 InsetCommandParams icp(name);
309                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
310                                         icp);
311                                 return new InsetLabel(icp);
312
313                         } else if (name == "ref") {
314                                 InsetCommandParams icp(name);
315                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
316                                         icp);
317                                 return new InsetRef(icp, *bv->buffer());
318
319                         } else if (name == "toc") {
320                                 InsetCommandParams icp("tableofcontents");
321                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
322                                         icp);
323                                 return new InsetTOC(icp);
324
325                         } else if (name == "url") {
326                                 InsetCommandParams icp(name);
327                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
328                                         icp);
329                                 return new InsetUrl(icp);
330
331                         } else if (name == "vspace") {
332                                 VSpace vspace;
333                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
334                                 return new InsetVSpace(vspace);
335                         }
336                 }
337
338                 case LFUN_SPACE_INSERT: {
339                         string const name = to_utf8(cmd.argument());
340                         if (name == "normal")
341                                 return new InsetSpace(InsetSpace::NORMAL);
342                         else if (name == "protected")
343                                 return new InsetSpace(InsetSpace::PROTECTED);
344                         else if (name == "thin")
345                                 return new InsetSpace(InsetSpace::THIN);
346                         else if (name == "quad")
347                                 return new InsetSpace(InsetSpace::QUAD);
348                         else if (name == "qquad")
349                                 return new InsetSpace(InsetSpace::QQUAD);
350                         else if (name == "enspace")
351                                 return new InsetSpace(InsetSpace::ENSPACE);
352                         else if (name == "enskip")
353                                 return new InsetSpace(InsetSpace::ENSKIP);
354                         else if (name == "negthinspace")
355                                 return new InsetSpace(InsetSpace::NEGTHIN);
356                         else if (name.empty())
357                                 lyxerr << "LyX function 'space' needs an argument." << endl;
358                         else
359                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
360                 }
361                 break;
362
363                 default:
364                         break;
365                 }
366
367         } catch (support::ExceptionMessage const & message) {
368                 if (message.type_ == support::ErrorException) {
369                         Alert::error(message.title_, message.details_);
370                         LyX::cref().emergencyCleanup();
371                         abort();
372                 } else if (message.type_ == support::WarningException) {
373                         Alert::warning(message.title_, message.details_);
374                         return 0;
375                 }
376         }
377
378
379         return 0;
380 }
381
382
383 Inset * readInset(Lexer & lex, Buffer const & buf)
384 {
385         // consistency check
386         if (lex.getString() != "\\begin_inset") {
387                 lyxerr << "Buffer::readInset: Consistency check failed."
388                        << endl;
389         }
390
391         auto_ptr<Inset> inset;
392
393         TextClass tclass = buf.params().getTextClass();
394
395         lex.next();
396         string tmptok = lex.getString();
397
398         // test the different insets
399         if (tmptok == "LatexCommand") {
400                 lex.next();
401                 string const cmdName = lex.getString();
402                 lex.pushToken(cmdName);
403
404                 InsetCommandParams inscmd(cmdName);
405                 inscmd.read(lex);
406
407                 // This strange command allows LyX to recognize "natbib" style
408                 // citations: citet, citep, Citet etc.
409                 // FIXME: We already have partial support for \\fullcite and
410                 // the various \\footcite commands. We should increase the
411                 // file format number and read these commands here, too.
412                 // Then we should use is_possible_cite_command() in
413                 // src/frontends/controllers/frontend_helpers.cpp to test for valid cite
414                 // commands.
415                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
416                         inset.reset(new InsetCitation(inscmd));
417                 } else if (cmdName == "bibitem") {
418                         inset.reset(new InsetBibitem(inscmd));
419                 } else if (cmdName == "bibtex") {
420                         inset.reset(new InsetBibtex(inscmd));
421                 } else if (cmdName == "index") {
422                         inset.reset(new InsetIndex(inscmd));
423                 } else if (cmdName == "nomenclature") {
424                         inset.reset(new InsetNomencl(inscmd));
425                 } else if (cmdName == "include") {
426                         inset.reset(new InsetInclude(inscmd));
427                 } else if (cmdName == "label") {
428                         inset.reset(new InsetLabel(inscmd));
429                 } else if (cmdName == "url"
430                            || cmdName == "htmlurl") {
431                         inset.reset(new InsetUrl(inscmd));
432                 } else if (cmdName == "ref"
433                            || cmdName == "eqref"
434                            || cmdName == "pageref"
435                            || cmdName == "vref"
436                            || cmdName == "vpageref"
437                            || cmdName == "prettyref") {
438                         if (!inscmd["name"].empty()
439                             || !inscmd["reference"].empty()) {
440                                 inset.reset(new InsetRef(inscmd, buf));
441                         }
442                 } else if (cmdName == "tableofcontents") {
443                         inset.reset(new InsetTOC(inscmd));
444                 } else if (cmdName == "listofalgorithms") {
445                         inset.reset(new InsetFloatList("algorithm"));
446                 } else if (cmdName == "listoffigures") {
447                         inset.reset(new InsetFloatList("figure"));
448                 } else if (cmdName == "listoftables") {
449                         inset.reset(new InsetFloatList("table"));
450                 } else if (cmdName == "printindex") {
451                         inset.reset(new InsetPrintIndex(inscmd));
452                 } else if (cmdName == "printnomenclature") {
453                         inset.reset(new InsetPrintNomencl(inscmd));
454                 } else {
455                         lyxerr << "unknown CommandInset '" << cmdName
456                                << "'" << std::endl;
457                         while (lex.isOK() && lex.getString() != "\\end_inset")
458                                 lex.next();
459                         return 0;
460                 }
461         } else {
462                 if (tmptok == "Quotes") {
463                         inset.reset(new InsetQuotes);
464                 } else if (tmptok == "External") {
465                         inset.reset(new InsetExternal);
466                 } else if (tmptok == "FormulaMacro") {
467                         inset.reset(new MathMacroTemplate);
468                 } else if (tmptok == "Formula") {
469                         inset.reset(new InsetMathHull);
470                 } else if (tmptok == "Graphics") {
471                         inset.reset(new InsetGraphics);
472                 } else if (tmptok == "Note") {
473                         inset.reset(new InsetNote(buf.params(), tmptok));
474                 } else if (tmptok == "Box") {
475                         inset.reset(new InsetBox(buf.params(), tmptok));
476                 } else if (tmptok == "CharStyle") {
477                         lex.next();
478                         string s = lex.getString();
479                         CharStyles::iterator found_cs = tclass.charstyle(s);
480                         if (found_cs != tclass.charstyles().end())
481                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
482                         else {
483                                 // "Undefined" inset
484                                 inset.reset(new InsetCharStyle(buf.params(), s));
485                         }
486                 } else if (tmptok == "Branch") {
487                         inset.reset(new InsetBranch(buf.params(),
488                                                     InsetBranchParams()));
489                 } else if (tmptok == "Include") {
490                         InsetCommandParams p("include");
491                         inset.reset(new InsetInclude(p));
492                 } else if (tmptok == "Environment") {
493                         lex.next();
494                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
495                 } else if (tmptok == "ERT") {
496                         inset.reset(new InsetERT(buf.params()));
497                 } else if (tmptok == "listings") {
498                         inset.reset(new InsetListings(buf.params()));
499                 } else if (tmptok == "InsetSpace") {
500                         inset.reset(new InsetSpace);
501                 } else if (tmptok == "Tabular") {
502                         inset.reset(new InsetTabular(buf));
503                 } else if (tmptok == "Text") {
504                         inset.reset(new InsetText(buf.params()));
505                 } else if (tmptok == "VSpace") {
506                         inset.reset(new InsetVSpace);
507                 } else if (tmptok == "Foot") {
508                         inset.reset(new InsetFoot(buf.params()));
509                 } else if (tmptok == "Marginal") {
510                         inset.reset(new InsetMarginal(buf.params()));
511                 } else if (tmptok == "OptArg") {
512                         inset.reset(new InsetOptArg(buf.params()));
513                 } else if (tmptok == "Float") {
514                         lex.next();
515                         string tmptok = lex.getString();
516                         inset.reset(new InsetFloat(buf.params(), tmptok));
517                 } else if (tmptok == "Wrap") {
518                         lex.next();
519                         string tmptok = lex.getString();
520                         inset.reset(new InsetWrap(buf.params(), tmptok));
521 #if 0
522                 } else if (tmptok == "List") {
523                         inset.reset(new InsetList);
524                 } else if (tmptok == "Theorem") {
525                         inset.reset(new InsetList);
526 #endif
527                 } else if (tmptok == "Caption") {
528                         inset.reset(new InsetCaption(buf.params()));
529                 } else if (tmptok == "FloatList") {
530                         inset.reset(new InsetFloatList);
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 // FIXME: hack..
542                 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
543                         MathMacroTemplate const * tmpl =
544                                 static_cast<MathMacroTemplate*>(inset.get());
545                         MacroTable::globalMacros().insert
546                                 (tmpl->name(), tmpl->asMacroData());
547                         LYXERR(Debug::DEBUG)
548                                 << BOOST_CURRENT_FUNCTION
549                                 << ": creating local macro " << to_utf8(tmpl->name())
550                                 << endl;
551                 }
552         }
553
554         return inset.release();
555 }
556
557
558 } // namespace lyx