]> git.lyx.org Git - lyx.git/blob - src/factory.cpp
Fix bug 4741: Pasting with middle mouse button into read only document works
[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/InsetCaption.h"
26 #include "insets/InsetCitation.h"
27 #include "insets/InsetFlex.h"
28 #include "insets/InsetERT.h"
29 #include "insets/InsetListings.h"
30 #include "insets/InsetExternal.h"
31 #include "insets/InsetFloat.h"
32 #include "insets/InsetFloatList.h"
33 #include "insets/InsetFoot.h"
34 #include "insets/InsetGraphics.h"
35 #include "insets/InsetInclude.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetInfo.h"
38 #include "insets/InsetNomencl.h"
39 #include "insets/InsetLabel.h"
40 #include "insets/InsetLine.h"
41 #include "insets/InsetMarginal.h"
42 #include "insets/InsetNewline.h"
43 #include "insets/InsetNewpage.h"
44 #include "insets/InsetNote.h"
45 #include "insets/InsetBox.h"
46 #include "insets/InsetBranch.h"
47 #include "insets/InsetOptArg.h"
48 #include "insets/InsetNewpage.h"
49 #include "insets/InsetRef.h"
50 #include "insets/InsetSpace.h"
51 #include "insets/InsetTabular.h"
52 #include "insets/InsetTOC.h"
53 #include "insets/InsetHyperlink.h"
54 #include "insets/InsetVSpace.h"
55 #include "insets/InsetWrap.h"
56
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
59
60 #include "frontends/alert.h"
61
62 #include "support/debug.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
65
66 #include "support/lassert.h"
67
68 #include <sstream>
69
70 using namespace std;
71 using namespace lyx::support;
72
73 namespace lyx {
74
75 namespace Alert = frontend::Alert;
76
77
78 Inset * createInsetHelper(Buffer & buf, FuncRequest const & cmd)
79 {
80         BufferParams const & params = buf.params();
81
82         try {
83
84                 switch (cmd.action) {
85
86                 case LFUN_LINE_INSERT:
87                         return new InsetLine;
88
89                 case LFUN_NEWPAGE_INSERT: {
90                         string const name = cmd.getArg(0);
91                         InsetNewpageParams inp;
92                         if (name.empty() || name == "newpage")
93                                 inp.kind = InsetNewpageParams::NEWPAGE;
94                         else if (name == "pagebreak")
95                                 inp.kind = InsetNewpageParams::PAGEBREAK;
96                         else if (name == "clearpage")
97                                 inp.kind = InsetNewpageParams::CLEARPAGE;
98                         else if (name == "cleardoublepage")
99                                 inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
100                         return new InsetNewpage(inp);
101                 }
102
103                 case LFUN_FLEX_INSERT: {
104                         string s = cmd.getArg(0);
105                         return new InsetFlex(buf, s);
106                 }
107
108                 case LFUN_NOTE_INSERT: {
109                         string arg = cmd.getArg(0);
110                         if (arg.empty())
111                                 arg = "Note";
112                         return new InsetNote(buf, arg);
113                 }
114
115                 case LFUN_BOX_INSERT: {
116                         string arg = cmd.getArg(0);
117                         if (arg.empty())
118                                 arg = "Boxed";
119                         return new InsetBox(buf, arg);
120                 }
121
122                 case LFUN_BRANCH_INSERT: {
123                         docstring arg = cmd.argument();
124                         if (arg.empty())
125                                 arg = from_ascii("none");
126                         return new InsetBranch(buf, InsetBranchParams(arg));
127                 }
128
129                 case LFUN_ERT_INSERT:
130                         return new InsetERT(buf);
131
132                 case LFUN_LISTING_INSERT:
133                         return new InsetListings(buf);
134
135                 case LFUN_FOOTNOTE_INSERT:
136                         return new InsetFoot(buf);
137
138                 case LFUN_MARGINALNOTE_INSERT:
139                         return new InsetMarginal(buf);
140
141                 case LFUN_OPTIONAL_INSERT:
142                         return new InsetOptArg(buf);
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->setWide(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_INFO_INSERT: {
207                         InsetInfo * inset = new InsetInfo(buf, to_utf8(cmd.argument()));
208                         inset->updateInfo();
209                         return inset;
210                 }
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                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
223                                 return new InsetBibitem(buf, icp);
224                         }
225                         
226                         case BIBTEX_CODE: {
227                                 InsetCommandParams icp(code);
228                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
229                                 return new InsetBibtex(buf, icp);
230                         }
231                         
232                         case CITE_CODE: {
233                                 InsetCommandParams icp(code);
234                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
235                                 return new InsetCitation(icp);
236                         }
237                         
238                         case ERT_CODE: {
239                                 return new InsetERT(buf,
240                                         InsetERT::string2params(to_utf8(cmd.argument())));
241                         }
242                                 
243                         case LISTINGS_CODE: {
244                                 InsetListingsParams par;
245                                 InsetListings::string2params(to_utf8(cmd.argument()), par);
246                                 return new InsetListings(buf, par);
247                         }
248                         
249                         case EXTERNAL_CODE: {
250                                 InsetExternalParams iep;
251                                 InsetExternal::string2params(to_utf8(cmd.argument()), buf, iep);
252                                 auto_ptr<InsetExternal> inset(new InsetExternal(buf));
253                                 inset->setBuffer(buf);
254                                 inset->setParams(iep);
255                                 return inset.release();
256                         }
257                         
258                         case GRAPHICS_CODE: {
259                                 InsetGraphicsParams igp;
260                                 InsetGraphics::string2params(to_utf8(cmd.argument()), buf, igp);
261                                 auto_ptr<InsetGraphics> inset(new InsetGraphics(buf));
262                                 inset->setParams(igp);
263                                 return inset.release();
264                         }
265                         
266                         case HYPERLINK_CODE: {
267                                 InsetCommandParams icp(code);
268                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
269                                 return new InsetHyperlink(icp);
270                         }
271                         
272                         case INCLUDE_CODE: {
273                                 InsetCommandParams icp(code);
274                                 InsetCommand::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                                 InsetCommand::string2params(name, lyx::to_utf8(cmd.argument()), icp);
284                                 return new InsetNomencl(icp);
285                         }
286                         
287                         case LABEL_CODE: {
288                                 InsetCommandParams icp(code);
289                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
290                                 return new InsetLabel(icp);
291                         }
292                         
293                         case REF_CODE: {
294                                 InsetCommandParams icp(code);
295                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
296                                 return new InsetRef(buf, icp);
297                         }
298
299                         case SPACE_CODE: {
300                                 InsetSpaceParams isp;
301                                 InsetSpace::string2params(to_utf8(cmd.argument()), isp);
302                                 return new InsetSpace(isp);
303                         }
304                         
305                         case TOC_CODE: {
306                                 InsetCommandParams icp(code);
307                                 InsetCommand::string2params(name, to_utf8(cmd.argument()), icp);
308                                 return new InsetTOC(icp);
309                         }
310                         
311                         case VSPACE_CODE: {
312                                 VSpace vspace;
313                                 InsetVSpace::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                         // The tests for isp.math might be disabled after a file format change
334                         if (name == "normal")
335                                 isp.kind = InsetSpaceParams::NORMAL;
336                         else if (name == "protected")
337                                 isp.kind = InsetSpaceParams::PROTECTED;
338                         else if (name == "thin")
339                                 isp.kind = InsetSpaceParams::THIN;
340                         else if (isp.math && name == "med")
341                                 isp.kind = InsetSpaceParams::MEDIUM;
342                         else if (isp.math && name == "thick")
343                                 isp.kind = InsetSpaceParams::THICK;
344                         else if (name == "quad")
345                                 isp.kind = InsetSpaceParams::QUAD;
346                         else if (name == "qquad")
347                                 isp.kind = InsetSpaceParams::QQUAD;
348                         else if (name == "enspace")
349                                 isp.kind = InsetSpaceParams::ENSPACE;
350                         else if (name == "enskip")
351                                 isp.kind = InsetSpaceParams::ENSKIP;
352                         else if (name == "negthinspace")
353                                 isp.kind = InsetSpaceParams::NEGTHIN;
354                         else if (isp.math && name == "negmedspace")
355                                 isp.kind = InsetSpaceParams::NEGMEDIUM;
356                         else if (isp.math && name == "negthickspace")
357                                 isp.kind = InsetSpaceParams::NEGTHICK;
358                         else if (name == "hfill")
359                                 isp.kind = InsetSpaceParams::HFILL;
360                         else if (name == "hfill*")
361                                 isp.kind = InsetSpaceParams::HFILL_PROTECTED;
362                         else if (name == "dotfill")
363                                 isp.kind = InsetSpaceParams::DOTFILL;
364                         else if (name == "hrulefill")
365                                 isp.kind = InsetSpaceParams::HRULEFILL;
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;
373                                 isp.length = Length(len);
374                         }
375                         else if (name == "hspace*") {
376                                 if (len.empty() || !isValidLength(len)) {
377                                         lyxerr << "LyX function 'space-insert hspace*' "
378                                                << "needs a valid length argument." << endl;
379                                         break;
380                                 }
381                                 isp.kind = InsetSpaceParams::CUSTOM_PROTECTED;
382                                 isp.length = Length(len);
383                         }
384                         else {
385                                 lyxerr << "Wrong argument for LyX function 'space-insert'." << endl;
386                                 break;
387                         }
388                         return new InsetSpace(isp);
389                 }
390                 break;
391
392                 default:
393                         break;
394                 }
395
396         } catch (ExceptionMessage const & message) {
397                 if (message.type_ == ErrorException) {
398                         // This should never happen!
399                         Alert::error(message.title_, message.details_);
400                         lyx_exit(1);
401                 } else if (message.type_ == WarningException) {
402                         Alert::warning(message.title_, message.details_);
403                         return 0;
404                 }
405         }
406
407         return 0;
408 }
409
410
411 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
412 {
413         Inset * inset = createInsetHelper(buf, cmd);
414         if (inset)
415                 inset->setBuffer(buf);
416         return inset;
417 }
418
419
420 Inset * readInset(Lexer & lex, Buffer const & buf)
421 {
422         // consistency check
423         if (lex.getString() != "\\begin_inset")
424                 LYXERR0("Buffer::readInset: Consistency check failed.");
425
426         auto_ptr<Inset> inset;
427
428         string tmptok;
429         lex >> tmptok;
430
431         // test the different insets
432         
433         // FIXME It would be better if we did not have this branch and could
434         // just do one massive switch for all insets. But at present, it's
435         // easier to do it this way, and we can't do the massive switch until
436         // the conversion mentioned below.  Note that if we do want to do a
437         // single switch, we need to remove this "CommandInset" line---or
438         // replace it with a single "InsetType" line that would be used in all
439         // insets.
440         if (tmptok == "CommandInset") {
441                 lex.next();
442                 string const insetType = lex.getString();
443                 lex.pushToken(insetType);
444                 
445                 InsetCode const code = insetCode(insetType);
446                 
447                 //FIXME If we do the one massive switch, we cannot do this here, since
448                 //we do not know in advance that we're dealing with a command inset.
449                 //Worst case, we could put it in each case below. Better, we could
450                 //pass the lexer to the constructor and let the params be built there.
451                 InsetCommandParams inscmd(code);
452                 inscmd.read(lex);
453
454                 switch (code) {
455                         case BIBITEM_CODE:
456                                 inset.reset(new InsetBibitem(buf, inscmd));
457                                 break;
458                         case BIBTEX_CODE:
459                                 inset.reset(new InsetBibtex(buf, inscmd));
460                                 break;
461                         case CITE_CODE: 
462                                 inset.reset(new InsetCitation(inscmd));
463                                 break;
464                         case HYPERLINK_CODE:
465                                 inset.reset(new InsetHyperlink(inscmd));
466                                 break;
467                         case INCLUDE_CODE:
468                                 inset.reset(new InsetInclude(inscmd));
469                                 break;
470                         case INDEX_PRINT_CODE:
471                                 inset.reset(new InsetPrintIndex(inscmd));
472                                 break;
473                         case LABEL_CODE:
474                                 inset.reset(new InsetLabel(inscmd));
475                                 break;
476                         case NOMENCL_CODE:
477                                 inset.reset(new InsetNomencl(inscmd));
478                                 break;
479                         case NOMENCL_PRINT_CODE:
480                                 inset.reset(new InsetPrintNomencl(inscmd));
481                                 break;
482                         case REF_CODE:
483                                 if (inscmd["name"].empty() && inscmd["reference"].empty())
484                                         return 0;
485                                 inset.reset(new InsetRef(buf, inscmd));
486                                 break;
487                         case TOC_CODE:
488                                 inset.reset(new InsetTOC(inscmd));
489                                 break;
490                         case NO_CODE:
491                         default:
492                                 lyxerr << "unknown CommandInset '" << insetType
493                                                         << "'" << endl;
494                                 while (lex.isOK() && lex.getString() != "\\end_inset")
495                                         lex.next();
496                                 return 0;
497                 }
498                 inset->setBuffer(const_cast<Buffer &>(buf));
499         } else { 
500                 // FIXME This branch should be made to use inset codes as the preceding 
501                 // branch does. Unfortunately, that will take some doing. It requires
502                 // converting the representation of the insets in LyX files so that they
503                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
504                 // can be translated to inset codes using insetCode(). And the insets'
505                 // write() routines should use insetName() rather than hardcoding it.
506                 if (tmptok == "Quotes") {
507                         inset.reset(new InsetQuotes(buf));
508                 } else if (tmptok == "External") {
509                         inset.reset(new InsetExternal(const_cast<Buffer &>(buf)));
510                 } else if (tmptok == "FormulaMacro") {
511                         inset.reset(new MathMacroTemplate);
512                 } else if (tmptok == "Formula") {
513                         inset.reset(new InsetMathHull);
514                 } else if (tmptok == "Graphics") {
515                         inset.reset(new InsetGraphics(const_cast<Buffer &>(buf)));
516                 } else if (tmptok == "Note") {
517                         inset.reset(new InsetNote(buf, tmptok));
518                 } else if (tmptok == "Box") {
519                         inset.reset(new InsetBox(buf, tmptok));
520                 } else if (tmptok == "Flex") {
521                         lex.next();
522                         string s = lex.getString();
523                         inset.reset(new InsetFlex(buf, s));
524                 } else if (tmptok == "Branch") {
525                         inset.reset(new InsetBranch(buf, InsetBranchParams()));
526                 } else if (tmptok == "ERT") {
527                         inset.reset(new InsetERT(buf));
528                 } else if (tmptok == "listings") {
529                         inset.reset(new InsetListings(buf));
530                 } else if (tmptok == "space") {
531                         inset.reset(new InsetSpace);
532                 } else if (tmptok == "Tabular") {
533                         inset.reset(new InsetTabular(const_cast<Buffer &>(buf)));
534                 } else if (tmptok == "Text") {
535                         inset.reset(new InsetText(buf));
536                 } else if (tmptok == "VSpace") {
537                         inset.reset(new InsetVSpace);
538                 } else if (tmptok == "Foot") {
539                         inset.reset(new InsetFoot(buf));
540                 } else if (tmptok == "Marginal") {
541                         inset.reset(new InsetMarginal(buf));
542                 } else if (tmptok == "Newpage") {
543                         inset.reset(new InsetNewpage);
544                 } else if (tmptok == "Newline") {
545                         inset.reset(new InsetNewline);
546                 } else if (tmptok == "OptArg") {
547                         inset.reset(new InsetOptArg(buf));
548                 } else if (tmptok == "Float") {
549                         lex.next();
550                         string tmptok = lex.getString();
551                         inset.reset(new InsetFloat(buf, tmptok));
552                 } else if (tmptok == "Wrap") {
553                         lex.next();
554                         string tmptok = lex.getString();
555                         inset.reset(new InsetWrap(buf, tmptok));
556                 } else if (tmptok == "Caption") {
557                         inset.reset(new InsetCaption(buf));
558                 } else if (tmptok == "Index") {
559                         inset.reset(new InsetIndex(buf));
560                 } else if (tmptok == "FloatList") {
561                         inset.reset(new InsetFloatList);
562                 } else if (tmptok == "Info") {
563                         inset.reset(new InsetInfo(buf));
564                 } else {
565                         lyxerr << "unknown Inset type '" << tmptok
566                                << "'" << endl;
567                         while (lex.isOK() && lex.getString() != "\\end_inset")
568                                 lex.next();
569                         return 0;
570                 }
571
572                 // Set the buffer reference for proper parsing of some insets
573                 // (InsetCollapsable for example)
574                 inset->setBuffer(const_cast<Buffer &>(buf));
575                 inset->read(lex);
576                 // Set again the buffer for insets that are created inside this inset
577                 // (InsetMathHull for example).
578                 inset->setBuffer(const_cast<Buffer &>(buf));
579         }
580         return inset.release();
581 }
582
583
584 } // namespace lyx