]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
fix bug #8211: \setotherlanguage not set for non-babel languages
[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/InsetBox.h"
26 #include "insets/InsetBranch.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetFlex.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetListings.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/InsetHyperlink.h"
38 #include "insets/InsetInclude.h"
39 #include "insets/InsetIndex.h"
40 #include "insets/InsetInfo.h"
41 #include "insets/InsetIPA.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNewline.h"
46 #include "insets/InsetNewpage.h"
47 #include "insets/InsetNomencl.h"
48 #include "insets/InsetNote.h"
49 #include "insets/InsetArgument.h"
50 #include "insets/InsetPhantom.h"
51 #include "insets/InsetPreview.h"
52 #include "insets/InsetRef.h"
53 #include "insets/InsetScript.h"
54 #include "insets/InsetSpace.h"
55 #include "insets/InsetTabular.h"
56 #include "insets/InsetTOC.h"
57 #include "insets/InsetVSpace.h"
58 #include "insets/InsetWrap.h"
59
60 #include "mathed/MathMacroTemplate.h"
61 #include "mathed/InsetMathHull.h"
62
63 #include "frontends/alert.h"
64
65 #include "support/debug.h"
66 #include "support/lstrings.h"
67 #include "support/ExceptionMessage.h"
68
69 #include "support/lassert.h"
70
71 #include <sstream>
72
73 using namespace std;
74 using namespace lyx::support;
75
76 namespace lyx {
77
78 namespace Alert = frontend::Alert;
79
80
81 Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
82 {
83         try {
84
85                 switch (cmd.action()) {
86
87                 case LFUN_NEWPAGE_INSERT: {
88                         string const name = cmd.getArg(0);
89                         InsetNewpageParams inp;
90                         if (name.empty() || name == "newpage")
91                                 inp.kind = InsetNewpageParams::NEWPAGE;
92                         else if (name == "pagebreak")
93                                 inp.kind = InsetNewpageParams::PAGEBREAK;
94                         else if (name == "clearpage")
95                                 inp.kind = InsetNewpageParams::CLEARPAGE;
96                         else if (name == "cleardoublepage")
97                                 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
98                         return new InsetNewpage(inp);
99                 }
100
101                 case LFUN_FLEX_INSERT: {
102                         string s = cmd.getArg(0);
103                         return new InsetFlex(buf, s);
104                 }
105
106                 case LFUN_NOTE_INSERT: {
107                         string arg = cmd.getArg(0);
108                         if (arg.empty())
109                                 arg = "Note";
110                         return new InsetNote(buf, arg);
111                 }
112
113                 case LFUN_BOX_INSERT: {
114                         string arg = cmd.getArg(0);
115                         if (arg.empty())
116                                 arg = "Boxed";
117                         return new InsetBox(buf, arg);
118                 }
119
120                 case LFUN_BRANCH_INSERT: {
121                         docstring arg = cmd.argument();
122                         if (arg.empty())
123                                 arg = from_ascii("none");
124                         return new InsetBranch(buf, InsetBranchParams(arg));
125                 }
126
127                 case LFUN_PHANTOM_INSERT: {
128                         string arg = cmd.getArg(0);
129                         if (arg.empty())
130                                 arg = "Phantom";
131                         return new InsetPhantom(buf, arg);
132                 }
133
134                 case LFUN_ERT_INSERT:
135                         return new InsetERT(buf);
136
137                 case LFUN_LISTING_INSERT:
138                         return new InsetListings(buf);
139
140                 case LFUN_FOOTNOTE_INSERT:
141                         return new InsetFoot(buf);
142
143                 case LFUN_MARGINALNOTE_INSERT:
144                         return new InsetMarginal(buf);
145
146                 case LFUN_ARGUMENT_INSERT:
147                         return new InsetArgument(buf);
148
149                 case LFUN_FLOAT_INSERT: {
150                         string argument = to_utf8(cmd.argument());
151                         if (!argument.empty()) {
152                                 if (!contains(argument, "sideways")) {
153                                         if (!contains(argument, "wide"))
154                                                 argument += "\nwide false";
155                                         argument += "\nsideways false";
156                                 }
157                         }
158                         return new InsetFloat(buf, argument);
159                 }
160
161                 case LFUN_FLOAT_WIDE_INSERT: {
162                         string argument = to_utf8(cmd.argument());
163                         if (!argument.empty()) {
164                                 if (!contains(argument, "sideways")) {
165                                         if (!contains(argument, "wide"))
166                                                 argument += "\nwide true";
167                                         argument += "\nsideways false";
168                                 }
169                         }
170                         InsetFloat * fl = new InsetFloat(buf, argument);
171                         fl->setWide(true);
172                         return fl;
173                 }
174
175                 case LFUN_WRAP_INSERT: {
176                         string const argument = to_utf8(cmd.argument());
177                         if (argument == "figure" || argument == "table")
178                                 return new InsetWrap(buf, argument);
179                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
180                         return 0;
181                 }
182
183                 case LFUN_INDEX_INSERT: {
184                         docstring arg = cmd.argument();
185                         return new InsetIndex(buf, InsetIndexParams(arg));
186                 }
187
188                 case LFUN_IPA_INSERT:
189                         return new InsetIPA(buf);
190
191                 case LFUN_NOMENCL_INSERT: {
192                         InsetCommandParams icp(NOMENCL_CODE);
193                         icp["symbol"] = cmd.argument();
194                         return new InsetNomencl(buf, icp);
195                 }
196
197                 case LFUN_TABULAR_INSERT: {
198                         if (cmd.argument().empty())
199                                 return 0;
200                         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(buf, r, c);
208                 }
209
210                 case LFUN_CAPTION_INSERT:
211                         return new InsetCaption(buf);
212
213                 case LFUN_INDEX_PRINT:  {
214                         InsetCommandParams icp(INDEX_PRINT_CODE);
215                         icp["type"] = cmd.argument();
216                         return new InsetPrintIndex(buf, icp);
217                 }
218
219                 case LFUN_NOMENCL_PRINT: {
220                         InsetCommandParams icp(NOMENCL_PRINT_CODE);
221                         icp["set_width"] = from_ascii("auto");
222                         return new InsetPrintNomencl(buf, icp);
223                 }
224
225                 case LFUN_INFO_INSERT: {
226                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
227                         inset->updateInfo();
228                         return inset;
229                 }
230
231                 case LFUN_PREVIEW_INSERT:
232                         return new InsetPreview(buf);
233
234                 case LFUN_SCRIPT_INSERT: {
235                         InsetScriptParams isp;
236                         InsetScript::string2params("script script " + to_utf8(cmd.argument()), isp);
237                         return new InsetScript(buf, isp);
238                 }
239
240                 case LFUN_INSET_INSERT: {
241                         string const name = cmd.getArg(0);
242                         InsetCode code = insetCode(name);
243                         switch (code) {
244                         case NO_CODE:
245                                 lyxerr << "No such inset '" << name << "'.";
246                                 return 0;
247                         
248                         case BIBITEM_CODE: {
249                                 InsetCommandParams icp(code);
250                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
251                                 return new InsetBibitem(buf, icp);
252                         }
253                         
254                         case BIBTEX_CODE: {
255                                 InsetCommandParams icp(code);
256                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
257                                 return new InsetBibtex(buf, icp);
258                         }
259                         
260                         case CITE_CODE: {
261                                 InsetCommandParams icp(code);
262                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
263                                 return new InsetCitation(buf, icp);
264                         }
265                         
266                         case ERT_CODE: {
267                                 return new InsetERT(buf,
268                                         InsetERT::string2params(to_utf8(cmd.argument())));
269                         }
270                         
271                         case EXTERNAL_CODE: {
272                                 InsetExternalParams iep;
273                                 InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep);
274                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
275                                 inset->setBuffer(*buf);
276                                 inset->setParams(iep);
277                                 return inset.release();
278                         }
279                         
280                         case GRAPHICS_CODE: {
281                                 InsetGraphicsParams igp;
282                                 InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp);
283                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
284                                 inset->setParams(igp);
285                                 return inset.release();
286                         }
287                         
288                         case HYPERLINK_CODE: {
289                                 InsetCommandParams icp(code);
290                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
291                                 return new InsetHyperlink(buf, icp);
292                         }
293                         
294                         case INCLUDE_CODE: {
295                                 InsetCommandParams icp(code);
296                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
297                                 return new InsetInclude(buf, icp);
298                         }
299                         
300                         case INDEX_CODE: {
301                                 docstring arg = cmd.argument();
302                                 return new InsetIndex(buf, InsetIndexParams(arg));
303                         }
304                         
305                         case INDEX_PRINT_CODE:  {
306                                 InsetCommandParams icp(code);
307                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
308                                 return new InsetPrintIndex(buf, icp);
309                         }
310                         
311                         case LABEL_CODE: {
312                                 InsetCommandParams icp(code);
313                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
314                                 return new InsetLabel(buf, icp);
315                         }
316                         
317                         case LINE_CODE: {
318                                 InsetCommandParams icp(code);
319                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
320                                 return new InsetLine(buf, icp);
321                         }
322                                 
323                         case LISTINGS_CODE: {
324                                 InsetListingsParams par;
325                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
326                                 return new InsetListings(buf, par);
327                         }
328                         
329                         case NOMENCL_CODE: {
330                                 InsetCommandParams icp(code);
331                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
332                                 return new InsetNomencl(buf, icp);
333                         }
334                         
335                         case REF_CODE: {
336                                 InsetCommandParams icp(code);
337                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
338                                 return new InsetRef(buf, icp);
339                         }
340
341                         case SCRIPT_CODE: {
342                                 InsetScriptParams isp;
343                                 InsetScript::string2params(to_utf8(cmd.argument()), isp);
344                                 return new InsetScript(buf, isp);
345                         }
346
347                         case SPACE_CODE: {
348                                 InsetSpaceParams isp;
349                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
350                                 return new InsetSpace(isp);
351                         }
352                         
353                         case TOC_CODE: {
354                                 InsetCommandParams icp(code);
355                                 InsetCommand::string2params(to_utf8(cmd.argument()), icp);
356                                 return new InsetTOC(buf, icp);
357                         }
358                         
359                         case VSPACE_CODE: {
360                                 VSpace vspace;
361                                 InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
362                                 return new InsetVSpace(vspace);
363                         }
364
365                         case PREVIEW_CODE:
366                                 return new InsetPreview(buf);
367                         
368                         default:
369                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
370                                                 << endl;
371                                 return 0;
372                         
373                         }
374                 } //end LFUN_INSET_INSERT
375
376                 case LFUN_SPACE_INSERT: {
377                         string const name = cmd.getArg(0);
378                         string const len = cmd.getArg(1);
379                         if (name.empty()) {
380                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
381                                 break;
382                         }
383                         InsetSpaceParams isp;
384                         // The tests for isp.math might be disabled after a file format change
385                         if (name == "normal")
386                                 isp.kind = InsetSpaceParams::NORMAL;
387                         else if (name == "protected")
388                                 isp.kind = InsetSpaceParams::PROTECTED;
389                         else if (name == "visible")
390                                 isp.kind = InsetSpaceParams::VISIBLE;
391                         else if (name == "thin")
392                                 isp.kind = InsetSpaceParams::THIN;
393                         else if (isp.math && name == "med")
394                                 isp.kind = InsetSpaceParams::MEDIUM;
395                         else if (isp.math && name == "thick")
396                                 isp.kind = InsetSpaceParams::THICK;
397                         else if (name == "quad")
398                                 isp.kind = InsetSpaceParams::QUAD;
399                         else if (name == "qquad")
400                                 isp.kind = InsetSpaceParams::QQUAD;
401                         else if (name == "enspace")
402                                 isp.kind = InsetSpaceParams::ENSPACE;
403                         else if (name == "enskip")
404                                 isp.kind = InsetSpaceParams::ENSKIP;
405                         else if (name == "negthinspace")
406                                 isp.kind = InsetSpaceParams::NEGTHIN;
407                         else if (name == "negmedspace")
408                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
409                         else if (name == "negthickspace")
410                                 isp.kind = InsetSpaceParams::NEGTHICK;
411                         else if (name == "hfill")
412                                 isp.kind = InsetSpaceParams::HFILL;
413                         else if (name == "hfill*")
414                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
415                         else if (name == "dotfill")
416                                 isp.kind = InsetSpaceParams::DOTFILL;
417                         else if (name == "hrulefill")
418                                 isp.kind = InsetSpaceParams::HRULEFILL;
419                         else if (name == "hspace") {
420                                 if (len.empty() || !isValidGlueLength(len)) {
421                                         lyxerr << "LyX function 'space-insert hspace' "
422                                                << "needs a valid length argument." << endl;
423                                         break;
424                                 }
425                                 isp.kind = InsetSpaceParams::CUSTOM;
426                                 isp.length = GlueLength(len);
427                         }
428                         else if (name == "hspace*") {
429                                 if (len.empty() || !isValidGlueLength(len)) {
430                                         lyxerr << "LyX function 'space-insert hspace*' "
431                                                << "needs a valid length argument." << endl;
432                                         break;
433                                 }
434                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
435                                 isp.length = GlueLength(len);
436                         }
437                         else {
438                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
439                                 break;
440                         }
441                         return new InsetSpace(isp);
442                 }
443                 break;
444
445                 default:
446                         break;
447                 }
448
449         } catch (ExceptionMessage const & message) {
450                 if (message.type_ == ErrorException) {
451                         // This should never happen!
452                         Alert::error(message.title_, message.details_);
453                         lyx_exit(1);
454                 } else if (message.type_ == WarningException) {
455                         Alert::warning(message.title_, message.details_);
456                         return 0;
457                 }
458         }
459
460         return 0;
461 }
462
463
464 Inset * createInset(Buffer * buf, FuncRequest const & cmd)
465 {
466         Inset * inset = createInsetHelper(buf, cmd);
467         if (inset)
468                 inset->setBuffer(*buf);
469         return inset;
470 }
471
472
473 Inset * readInset(Lexer & lex, Buffer * buf)
474 {
475         // consistency check
476         if (lex.getString() != "\\begin_inset")
477                 LYXERR0("Buffer::readInset: Consistency check failed.");
478
479         auto_ptr<Inset> inset;
480
481         string tmptok;
482         lex >> tmptok;
483
484         // test the different insets
485         
486         // FIXME It would be better if we did not have this branch and could
487         // just do one massive switch for all insets. But at present, it's
488         // easier to do it this way, and we can't do the massive switch until
489         // the conversion mentioned below.  Note that if we do want to do a
490         // single switch, we need to remove this "CommandInset" line---or
491         // replace it with a single "InsetType" line that would be used in all
492         // insets.
493         if (tmptok == "CommandInset") {
494                 lex.next();
495                 string const insetType = lex.getString();
496                 lex.pushToken(insetType);
497                 
498                 InsetCode const code = insetCode(insetType);
499                 
500                 //FIXME If we do the one massive switch, we cannot do this here, since
501                 //we do not know in advance that we're dealing with a command inset.
502                 //Worst case, we could put it in each case below. Better, we could
503                 //pass the lexer to the constructor and let the params be built there.
504                 InsetCommandParams inscmd(code);
505                 inscmd.read(lex);
506
507                 switch (code) {
508                         case BIBITEM_CODE:
509                                 inset.reset(new InsetBibitem(buf, inscmd));
510                                 break;
511                         case BIBTEX_CODE:
512                                 inset.reset(new InsetBibtex(buf, inscmd));
513                                 break;
514                         case CITE_CODE: 
515                                 inset.reset(new InsetCitation(buf, inscmd));
516                                 break;
517                         case HYPERLINK_CODE:
518                                 inset.reset(new InsetHyperlink(buf, inscmd));
519                                 break;
520                         case INCLUDE_CODE:
521                                 inset.reset(new InsetInclude(buf, inscmd));
522                                 break;
523                         case INDEX_PRINT_CODE:
524                                 inset.reset(new InsetPrintIndex(buf, inscmd));
525                                 break;
526                         case LABEL_CODE:
527                                 inset.reset(new InsetLabel(buf, inscmd));
528                                 break;
529                         case LINE_CODE:
530                                 inset.reset(new InsetLine(buf, inscmd));
531                                 break;
532                         case NOMENCL_CODE:
533                                 inset.reset(new InsetNomencl(buf, inscmd));
534                                 break;
535                         case NOMENCL_PRINT_CODE:
536                                 inset.reset(new InsetPrintNomencl(buf, inscmd));
537                                 break;
538                         case REF_CODE:
539                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
540                                         return 0;
541                                 inset.reset(new InsetRef(buf, inscmd));
542                                 break;
543                         case TOC_CODE:
544                                 inset.reset(new InsetTOC(buf, inscmd));
545                                 break;
546                         case NO_CODE:
547                         default:
548                                 lyxerr << "unknown CommandInset '" << insetType
549                                                         << "'" << endl;
550                                 while (lex.isOK() && lex.getString() != "\\end_inset")
551                                         lex.next();
552                                 return 0;
553                 }
554                 inset->setBuffer(*buf);
555         } else { 
556                 // FIXME This branch should be made to use inset codes as the preceding 
557                 // branch does. Unfortunately, that will take some doing. It requires
558                 // converting the representation of the insets in LyX files so that they
559                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
560                 // can be translated to inset codes using insetCode(). And the insets'
561                 // write() routines should use insetName() rather than hardcoding it.
562                 if (tmptok == "Quotes") {
563                         inset.reset(new InsetQuotes(buf));
564                 } else if (tmptok == "External") {
565                         inset.reset(new InsetExternal(buf));
566                 } else if (tmptok == "FormulaMacro") {
567                         inset.reset(new MathMacroTemplate(buf));
568                 } else if (tmptok == "Formula") {
569                         inset.reset(new InsetMathHull(buf));
570                 } else if (tmptok == "Graphics") {
571                         inset.reset(new InsetGraphics(buf));
572                 } else if (tmptok == "Note") {
573                         inset.reset(new InsetNote(buf, tmptok));
574                 } else if (tmptok == "Box") {
575                         inset.reset(new InsetBox(buf, tmptok));
576                 } else if (tmptok == "Flex") {
577                         lex.eatLine();
578                         string s = lex.getString();
579                         inset.reset(new InsetFlex(buf, s));
580                 } else if (tmptok == "Branch") {
581                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
582                 } else if (tmptok == "Phantom") {
583                         inset.reset(new InsetPhantom(buf, tmptok));
584                 } else if (tmptok == "ERT") {
585                         inset.reset(new InsetERT(buf));
586                 } else if (tmptok == "listings") {
587                         inset.reset(new InsetListings(buf));
588                 } else if (tmptok == "script") {
589                         inset.reset(new InsetScript(buf));
590                 } else if (tmptok == "space") {
591                         inset.reset(new InsetSpace);
592                 } else if (tmptok == "Tabular") {
593                         inset.reset(new InsetTabular(buf));
594                 } else if (tmptok == "Text") {
595                         inset.reset(new InsetText(buf));
596                 } else if (tmptok == "VSpace") {
597                         inset.reset(new InsetVSpace);
598                 } else if (tmptok == "Foot") {
599                         inset.reset(new InsetFoot(buf));
600                 } else if (tmptok == "Marginal") {
601                         inset.reset(new InsetMarginal(buf));
602                 } else if (tmptok == "Newpage") {
603                         inset.reset(new InsetNewpage);
604                 } else if (tmptok == "Newline") {
605                         inset.reset(new InsetNewline);
606                 } else if (tmptok == "Argument") {
607                         inset.reset(new InsetArgument(buf));
608                 } else if (tmptok == "Float") {
609                         inset.reset(new InsetFloat(buf, string()));
610                 } else if (tmptok == "Wrap") {
611                         lex.next();
612                         string tmptok = lex.getString();
613                         inset.reset(new InsetWrap(buf, tmptok));
614                 } else if (tmptok == "Caption") {
615                         inset.reset(new InsetCaption(buf));
616                 } else if (tmptok == "Index") {
617                         inset.reset(new InsetIndex(buf, InsetIndexParams()));
618                 } else if (tmptok == "FloatList") {
619                         inset.reset(new InsetFloatList(buf));
620                 } else if (tmptok == "Info") {
621                         inset.reset(new InsetInfo(buf));
622                 } else if (tmptok == "IPA") {
623                         inset.reset(new InsetIPA(buf));
624                 } else if (tmptok == "Preview") {
625                         inset.reset(new InsetPreview(buf));
626                 } else {
627                         lyxerr << "unknown Inset type '" << tmptok
628                                << "'" << endl;
629                         while (lex.isOK() && lex.getString() != "\\end_inset")
630                                 lex.next();
631                         return 0;
632                 }
633
634                 // Set the buffer reference for proper parsing of some insets
635                 // (InsetCollapsable for example)
636                 inset->setBuffer(*buf);
637                 inset->read(lex);
638                 // Set again the buffer for insets that are created inside this inset
639                 // (InsetMathHull for example).
640                 inset->setBuffer(*buf);
641         }
642         return inset.release();
643 }
644
645
646 } // namespace lyx