]> git.lyx.org Git - features.git/blob - src/factory.cpp
** add support for protected horizontal fill. FILE FORMAT CHANGE. **
[features.git] / src / factory.cpp
1 /**
2  * \file factory.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "factory.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
19 #include "Lexer.h"
20 #include "LyX.h"
21 #include "TextClass.h"
22
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetCaption.h"
26 #include "insets/InsetCitation.h"
27 #include "insets/InsetFlex.h"
28 #include "insets/InsetEnvironment.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetListings.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetFloatList.h"
34 #include "insets/InsetFoot.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetInclude.h"
37 #include "insets/InsetIndex.h"
38 #include "insets/InsetInfo.h"
39 #include "insets/InsetNomencl.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/InsetNewpage.h"
48 #include "insets/InsetRef.h"
49 #include "insets/InsetSpace.h"
50 #include "insets/InsetTabular.h"
51 #include "insets/InsetTOC.h"
52 #include "insets/InsetHyperlink.h"
53 #include "insets/InsetVSpace.h"
54 #include "insets/InsetWrap.h"
55
56 #include "mathed/MathMacroTemplate.h"
57 #include "mathed/InsetMathHull.h"
58
59 #include "frontends/alert.h"
60
61 #include "support/debug.h"
62 #include "support/lstrings.h"
63 #include "support/ExceptionMessage.h"
64
65 #include <boost/assert.hpp>
66
67 #include <sstream>
68
69 using namespace std;
70 using namespace lyx::support;
71
72 namespace lyx {
73
74 namespace Alert = frontend::Alert;
75
76
77 Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
78 {
79         BufferParams const & params = buf.params();
80
81         try {
82
83                 switch (cmd.action) {
84
85                 case LFUN_LINE_INSERT:
86                         return new InsetLine;
87
88                 case LFUN_NEWPAGE_INSERT:
89                         return new InsetNewpage;
90
91                 case LFUN_PAGEBREAK_INSERT:
92                         return new InsetPagebreak;
93
94                 case LFUN_CLEARPAGE_INSERT:
95                         return new InsetClearPage;
96
97                 case LFUN_CLEARDOUBLEPAGE_INSERT:
98                         return new InsetClearDoublePage;
99
100                 case LFUN_FLEX_INSERT: {
101                         string s = cmd.getArg(0);
102                         return new InsetFlex(buf, buf.params().documentClassPtr(), s);
103                 }
104
105                 case LFUN_NOTE_INSERT: {
106                         string arg = cmd.getArg(0);
107                         if (arg.empty())
108                                 arg = "Note";
109                         return new InsetNote(buf, arg);
110                 }
111
112                 case LFUN_BOX_INSERT: {
113                         string arg = cmd.getArg(0);
114                         if (arg.empty())
115                                 arg = "Boxed";
116                         return new InsetBox(buf, arg);
117                 }
118
119                 case LFUN_BRANCH_INSERT: {
120                         docstring arg = cmd.argument();
121                         if (arg.empty())
122                                 arg = from_ascii("none");
123                         return new InsetBranch(buf, InsetBranchParams(arg));
124                 }
125
126                 case LFUN_ERT_INSERT:
127                         return new InsetERT(buf);
128
129                 case LFUN_LISTING_INSERT:
130                         return new InsetListings(buf);
131
132                 case LFUN_FOOTNOTE_INSERT:
133                         return new InsetFoot(buf);
134
135                 case LFUN_MARGINALNOTE_INSERT:
136                         return new InsetMarginal(buf);
137
138                 case LFUN_OPTIONAL_INSERT:
139                         return new InsetOptArg(buf);
140
141                 case LFUN_BIBITEM_INSERT:
142                         return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
143
144                 case LFUN_FLOAT_INSERT: {
145                         // check if the float type exists
146                         string const argument = to_utf8(cmd.argument());
147                         if (buf.params().documentClass().floats().typeExist(argument))
148                                 return new InsetFloat(buf, argument);
149                         lyxerr << "Non-existent float type: " << argument << endl;
150                 }
151
152                 case LFUN_FLOAT_WIDE_INSERT: {
153                         // check if the float type exists
154                         string const argument = to_utf8(cmd.argument());
155                         if (params.documentClass().floats().typeExist(argument)) {
156                                 auto_ptr<InsetFloat> p(new InsetFloat(buf, argument));
157                                 p->wide(true, params);
158                                 return p.release();
159                         }
160                         lyxerr << "Non-existent float type: " << argument << endl;
161                         return 0;
162                 }
163
164                 case LFUN_WRAP_INSERT: {
165                         string const argument = to_utf8(cmd.argument());
166                         if (argument == "figure" || argument == "table")
167                                 return new InsetWrap(buf, argument);
168                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
169                         return 0;
170                 }
171
172                 case LFUN_INDEX_INSERT:
173                         return new InsetIndex(buf);
174
175                 case LFUN_NOMENCL_INSERT: {
176                         InsetCommandParams icp(NOMENCL_CODE);
177                         icp["symbol"] = cmd.argument();
178                         return new InsetNomencl(icp);
179                 }
180
181                 case LFUN_TABULAR_INSERT: {
182                         if (cmd.argument().empty())
183                                 return 0;
184                         istringstream ss(to_utf8(cmd.argument()));
185                         int r = 0, c = 0;
186                         ss >> r >> c;
187                         if (r <= 0)
188                                 r = 2;
189                         if (c <= 0)
190                                 c = 2;
191                         return new InsetTabular(buf, r, c);
192                 }
193
194                 case LFUN_CAPTION_INSERT:
195                         return new InsetCaption(buf);
196
197                 case LFUN_INDEX_PRINT:
198                         return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
199
200                 case LFUN_NOMENCL_PRINT:
201                         return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
202
203                 case LFUN_TOC_INSERT:
204                         return new InsetTOC(InsetCommandParams(TOC_CODE));
205
206                 case LFUN_ENVIRONMENT_INSERT:
207                         return new InsetEnvironment(buf, cmd.argument());
208
209                 case LFUN_INFO_INSERT:
210                         return new InsetInfo(buf, to_utf8(cmd.argument()));
211
212                 case LFUN_INSET_INSERT: {
213                         string const name = cmd.getArg(0);
214                         InsetCode code = insetCode(name);
215                         switch (code) {
216                         case NO_CODE:
217                                 lyxerr << "No such inset '" << name << "'.";
218                                 return 0;
219                         
220                         case BIBITEM_CODE: {
221                                 InsetCommandParams icp(code);
222                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
223                                 return new InsetBibitem(icp);
224                         }
225                         
226                         case BIBTEX_CODE: {
227                                 InsetCommandParams icp(code);
228                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
229                                 return new InsetBibtex(icp);
230                         }
231                         
232                         case CITE_CODE: {
233                                 InsetCommandParams icp(code);
234                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
235                                 return new InsetCitation(icp);
236                         }
237                         
238                         case ERT_CODE: {
239                                 InsetCollapsable::CollapseStatus st;
240                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
241                                 return new InsetERT(buf, st);
242                         }
243                                 
244                         case LISTINGS_CODE: {
245                                 InsetListingsParams par;
246                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
247                                 return new InsetListings(buf, par);
248                         }
249                         
250                         case EXTERNAL_CODE: {
251                                 InsetExternalParams iep;
252                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
253                                 auto_ptr<InsetExternal> inset(new InsetExternal);
254                                 inset->setParams(iep);
255                                 return inset.release();
256                         }
257                         
258                         case GRAPHICS_CODE: {
259                                 InsetGraphicsParams igp;
260                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
261                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
262                                 inset->setParams(igp);
263                                 return inset.release();
264                         }
265                         
266                         case HYPERLINK_CODE: {
267                                 InsetCommandParams icp(code);
268                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
269                                 return new InsetHyperlink(icp);
270                         }
271                         
272                         case INCLUDE_CODE: {
273                                 InsetCommandParams icp(code);
274                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
275                                 return new InsetInclude(icp);
276                         }
277                         
278                         case INDEX_CODE:
279                                 return new InsetIndex(buf);
280                         
281                         case NOMENCL_CODE: {
282                                 InsetCommandParams icp(code);
283                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
284                                 return new InsetNomencl(icp);
285                         }
286                         
287                         case LABEL_CODE: {
288                                 InsetCommandParams icp(code);
289                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
290                                 return new InsetLabel(icp);
291                         }
292                         
293                         case REF_CODE: {
294                                 InsetCommandParams icp(code);
295                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
296                                 return new InsetRef(buf, icp);
297                         }
298
299                         case SPACE_CODE: {
300                                 InsetSpaceParams isp;
301                                 InsetSpaceMailer::string2params(to_utf8(cmd.argument()), isp);
302                                 return new InsetSpace(isp);
303                         }
304                         
305                         case TOC_CODE: {
306                                 InsetCommandParams icp(code);
307                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
308                                 return new InsetTOC(icp);
309                         }
310                         
311                         case VSPACE_CODE: {
312                                 VSpace vspace;
313                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
314                                 return new InsetVSpace(vspace);
315                         }
316                         
317                         default:
318                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
319                                                 << endl;
320                                 return 0;
321                         
322                         }
323                         } //end LFUN_INSET_INSERT
324
325                 case LFUN_SPACE_INSERT: {
326                         string const name = cmd.getArg(0);
327                         string const len = cmd.getArg(1);
328                         if (name.empty()) {
329                                 lyxerr << "LyX function 'space-insert' needs an argument." << endl;
330                                 break;
331                         }
332                         InsetSpaceParams isp;
333                         if (name == "normal")
334                                 isp.kind = InsetSpaceParams::NORMAL;
335                         else if (name == "protected")
336                                 isp.kind = InsetSpaceParams::PROTECTED;
337                         else if (name == "thin")
338                                 isp.kind = InsetSpaceParams::THIN;
339                         else if (name == "quad")
340                                 isp.kind = InsetSpaceParams::QUAD;
341                         else if (name == "qquad")
342                                 isp.kind = InsetSpaceParams::QQUAD;
343                         else if (name == "enspace")
344                                 isp.kind = InsetSpaceParams::ENSPACE;
345                         else if (name == "enskip")
346                                 isp.kind = InsetSpaceParams::ENSKIP;
347                         else if (name == "negthinspace")
348                                 isp.kind = InsetSpaceParams::NEGTHIN;
349                         else if (name == "hfill")
350                                 isp.kind = InsetSpaceParams::HFILL;
351                         else if (name == "hfill*")
352                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
353                         else if (name == "dotfill")
354                                 isp.kind = InsetSpaceParams::DOTFILL;
355                         else if (name == "hrulefill")
356                                 isp.kind = InsetSpaceParams::HRULEFILL;
357                         else if (name == "hspace") {
358                                 if (len.empty() || !isValidLength(len)) {
359                                         lyxerr << "LyX function 'space-insert hspace' "
360                                                << "needs a valid length argument." << endl;
361                                         break;
362                                 }
363                                 isp.kind = InsetSpaceParams::CUSTOM;
364                                 isp.length = Length(len);
365                         }
366                         else if (name == "hspace*") {
367                                 if (len.empty() || !isValidLength(len)) {
368                                         lyxerr << "LyX function 'space-insert hspace*' "
369                                                << "needs a valid length argument." << endl;
370                                         break;
371                                 }
372                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
373                                 isp.length = Length(len);
374                         }
375                         else {
376                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
377                                 break;
378                         }
379                         return new InsetSpace(isp);
380                 }
381                 break;
382
383                 default:
384                         break;
385                 }
386
387         } catch (ExceptionMessage const & message) {
388                 if (message.type_ == ErrorException) {
389                         // This should never happen!
390                         Alert::error(message.title_, message.details_);
391                         LyX::cref().exit(1);
392                 } else if (message.type_ == WarningException) {
393                         Alert::warning(message.title_, message.details_);
394                         return 0;
395                 }
396         }
397
398
399         return 0;
400 }
401
402 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
403 {
404         Inset * inset = createInsetHelper(buf, cmd);
405         if (inset)
406                 inset->setBuffer(buf);
407         return inset;
408 }
409
410 Inset * readInset(Lexer & lex, Buffer const & buf)
411 {
412         // consistency check
413         if (lex.getString() != "\\begin_inset") {
414                 lyxerr << "Buffer::readInset: Consistency check failed."
415                        << endl;
416         }
417
418         auto_ptr<Inset> inset;
419
420         lex.next();
421         string tmptok = lex.getString();
422
423         // test the different insets
424         
425         //FIXME It would be better if we did not have this branch and could
426         //just do one massive switch for all insets. But at present, it's easier 
427         //to do it this way, and we can't do the massive switch until the conversion 
428         //mentioned below. 
429         //Note that if we do want to do a single switch, we need to remove
430         //this "CommandInset" line---or replace it with a single "InsetType" line
431         //that would be used in all insets.
432         if (tmptok == "CommandInset") {
433                 lex.next();
434                 string const insetType = lex.getString();
435                 lex.pushToken(insetType);
436                 
437                 InsetCode const code = insetCode(insetType);
438                 
439                 //FIXME If we do the one massive switch, we cannot do this here, since
440                 //we do not know in advance that we're dealing with a command inset.
441                 //Worst case, we could put it in each case below. Better, we could
442                 //pass the lexer to the constructor and let the params be built there.
443                 InsetCommandParams inscmd(code);
444                 inscmd.read(lex);
445
446                 switch (code) {
447                         case BIBITEM_CODE:
448                                 inset.reset(new InsetBibitem(inscmd));
449                                 break;
450                         case BIBTEX_CODE:
451                                 inset.reset(new InsetBibtex(inscmd));
452                                 break;
453                         case CITE_CODE: 
454                                 inset.reset(new InsetCitation(inscmd));
455                                 break;
456                         case HYPERLINK_CODE:
457                                 inset.reset(new InsetHyperlink(inscmd));
458                                 break;
459                         case INCLUDE_CODE:
460                                 inset.reset(new InsetInclude(inscmd));
461                                 break;
462                         case INDEX_PRINT_CODE:
463                                 inset.reset(new InsetPrintIndex(inscmd));
464                                 break;
465                         case LABEL_CODE:
466                                 inset.reset(new InsetLabel(inscmd));
467                                 break;
468                         case NOMENCL_CODE:
469                                 inset.reset(new InsetNomencl(inscmd));
470                                 break;
471                         case NOMENCL_PRINT_CODE:
472                                 inset.reset(new InsetPrintNomencl(inscmd));
473                                 break;
474                         case REF_CODE:
475                                 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
476                                         inset.reset(new InsetRef(buf, inscmd));
477                                 break;
478                         case TOC_CODE:
479                                 inset.reset(new InsetTOC(inscmd));
480                                 break;
481                         case NO_CODE:
482                         default:
483                                 lyxerr << "unknown CommandInset '" << insetType
484                                                         << "'" << endl;
485                                 while (lex.isOK() && lex.getString() != "\\end_inset")
486                                         lex.next();
487                                 return 0;
488                 }
489                 inset->setBuffer(const_cast<Buffer &>(buf));
490         } else { 
491                 // FIXME This branch should be made to use inset codes as the preceding 
492                 // branch does. Unfortunately, that will take some doing. It requires
493                 // converting the representation of the insets in LyX files so that they
494                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
495                 // can be translated to inset codes using insetCode(). And the insets'
496                 // write() routines should use insetName() rather than hardcoding it.
497                 if (tmptok == "Quotes") {
498                         inset.reset(new InsetQuotes);
499                 } else if (tmptok == "External") {
500                         inset.reset(new InsetExternal);
501                 } else if (tmptok == "FormulaMacro") {
502                         inset.reset(new MathMacroTemplate);
503                 } else if (tmptok == "Formula") {
504                         inset.reset(new InsetMathHull);
505                 } else if (tmptok == "Graphics") {
506                         inset.reset(new InsetGraphics);
507                 } else if (tmptok == "Note") {
508                         inset.reset(new InsetNote(buf, tmptok));
509                 } else if (tmptok == "Box") {
510                         inset.reset(new InsetBox(buf, tmptok));
511                 } else if (tmptok == "Flex") {
512                         lex.next();
513                         string s = lex.getString();
514                         inset.reset(new InsetFlex(buf, 
515                                 buf.params().documentClassPtr(), s));
516                 } else if (tmptok == "Branch") {
517                         inset.reset(new InsetBranch(buf,
518                                                     InsetBranchParams()));
519                 } else if (tmptok == "Environment") {
520                         lex.next();
521                         inset.reset(new InsetEnvironment(buf, lex.getDocString()));
522                 } else if (tmptok == "ERT") {
523                         inset.reset(new InsetERT(buf));
524                 } else if (tmptok == "listings") {
525                         inset.reset(new InsetListings(buf));
526                 } else if (tmptok == "Space") {
527                         inset.reset(new InsetSpace);
528                 } else if (tmptok == "Tabular") {
529                         inset.reset(new InsetTabular(buf));
530                 } else if (tmptok == "Text") {
531                         inset.reset(new InsetText(buf));
532                 } else if (tmptok == "VSpace") {
533                         inset.reset(new InsetVSpace);
534                 } else if (tmptok == "Foot") {
535                         inset.reset(new InsetFoot(buf));
536                 } else if (tmptok == "Marginal") {
537                         inset.reset(new InsetMarginal(buf));
538                 } else if (tmptok == "OptArg") {
539                         inset.reset(new InsetOptArg(buf));
540                 } else if (tmptok == "Float") {
541                         lex.next();
542                         string tmptok = lex.getString();
543                         inset.reset(new InsetFloat(buf, tmptok));
544                 } else if (tmptok == "Wrap") {
545                         lex.next();
546                         string tmptok = lex.getString();
547                         inset.reset(new InsetWrap(buf, tmptok));
548 #if 0
549                 } else if (tmptok == "Theorem") {
550                         inset.reset(new InsetList);
551 #endif
552                 } else if (tmptok == "Caption") {
553                         inset.reset(new InsetCaption(buf));
554                 } else if (tmptok == "Index") {
555                         inset.reset(new InsetIndex(buf));
556                 } else if (tmptok == "FloatList") {
557                         inset.reset(new InsetFloatList);
558                 } else if (tmptok == "Info") {
559                         inset.reset(new InsetInfo(buf));
560                 } else {
561                         lyxerr << "unknown Inset type '" << tmptok
562                                << "'" << endl;
563                         while (lex.isOK() && lex.getString() != "\\end_inset")
564                                 lex.next();
565                         return 0;
566                 }
567
568                 // Set the buffer reference for proper parsing of some insets
569                 // (InsetCollapsable for example)
570                 inset->setBuffer(const_cast<Buffer &>(buf));
571                 inset->read(lex);
572                 // Set again the buffer for insets that are created inside this inset
573                 // (InsetMathHull for example).
574                 inset->setBuffer(const_cast<Buffer &>(buf));
575         }
576         return inset.release();
577 }
578
579
580 } // namespace lyx