]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
9d39c94bf751efacb60f800d2b97dded52667c0e
[lyx.git] / src / insets / InsetLayout.cpp
1 // -*- C++ -*-
2 /**
3  * \file InsetLayout.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Martin Vermeer
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetLayout.h"
16
17 #include "ColorSet.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
24
25 #include <vector>
26
27 using std::string;
28 using std::set;
29 using std::vector;
30
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 InsetLayout::InsetLayout() :
36         name_(from_ascii("undefined")), lyxtype_(STANDARD),
37         labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
38         decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
39         labelfont_(sane_font), bgcolor_(Color_error),
40         fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
41         htmlisblock_(true), docbooksection_(false), multipar_(true),
42         custompars_(true), forceplain_(false), passthru_(false),
43         parbreakisnewline_(false), parbreakignored_(false), freespacing_(false),
44         keepempty_(false), forceltr_(false), forceownlines_(false),
45         needprotect_(false), needcprotect_(false), needmboxprotect_(false),
46         intoc_(false), spellcheck_(true), resetsfont_(false), display_(true),
47         forcelocalfontswitch_(false), add_to_toc_(false), is_toc_caption_(false),
48         edit_external_(false)
49 {
50         labelfont_.setColor(Color_error);
51 }
52
53
54 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
55 {
56         if (compare_ascii_no_case(str, "classic") == 0)
57                 return InsetLayout::CLASSIC;
58         if (compare_ascii_no_case(str, "minimalistic") == 0)
59                 return InsetLayout::MINIMALISTIC;
60         if (compare_ascii_no_case(str, "conglomerate") == 0)
61                 return InsetLayout::CONGLOMERATE;
62         return InsetLayout::DEFAULT;
63 }
64
65 namespace {
66
67 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
68 {
69         if (compare_ascii_no_case(str, "command") == 0)
70                 return InsetLayout::COMMAND;
71         if (compare_ascii_no_case(str, "environment") == 0)
72                 return InsetLayout::ENVIRONMENT;
73         if (compare_ascii_no_case(str, "none") == 0)
74                 return InsetLayout::NOLATEXTYPE;
75         return InsetLayout::ILT_ERROR;
76 }
77
78 } // namespace
79
80
81 bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
82         bool validating)
83 {
84         enum {
85                 IL_ADDTOTOC,
86                 IL_ARGUMENT,
87                 IL_BABELPREAMBLE,
88                 IL_BGCOLOR,
89                 IL_CONTENTASLABEL,
90                 IL_COPYSTYLE,
91                 IL_COUNTER,
92                 IL_CUSTOMPARS,
93                 IL_DECORATION,
94                 IL_DISPLAY,
95                 IL_EDITEXTERNAL,
96                 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
97                 IL_FONT,
98                 IL_FORCE_LOCAL_FONT_SWITCH,
99                 IL_FORCELTR,
100                 IL_FORCEOWNLINES,
101                 IL_FORCEPLAIN,
102                 IL_FREESPACING,
103                 IL_HTMLTAG,
104                 IL_HTMLATTR,
105                 IL_HTMLFORCECSS,
106                 IL_HTMLINNERTAG,
107                 IL_HTMLINNERATTR,
108                 IL_HTMLISBLOCK,
109                 IL_HTMLLABEL,
110                 IL_HTMLSTYLE,
111                 IL_HTMLPREAMBLE,
112                 IL_DOCBOOKTAG,
113                 IL_DOCBOOKTAGTYPE,
114                 IL_DOCBOOKATTR,
115                 IL_DOCBOOKSECTION,
116                 IL_DOCBOOKWRAPPERTAG,
117                 IL_DOCBOOKWRAPPERTAGTYPE,
118                 IL_DOCBOOKWRAPPERATTR,
119                 IL_INTOC,
120                 IL_ISTOCCAPTION,
121                 IL_LABELFONT,
122                 IL_LABELSTRING,
123                 IL_LANGPREAMBLE,
124                 IL_LATEXNAME,
125                 IL_LATEXPARAM,
126                 IL_LATEXTYPE,
127                 IL_LEFTDELIM,
128                 IL_LYXTYPE,
129                 IL_OBSOLETEDBY,
130                 IL_KEEPEMPTY,
131                 IL_MENUSTRING,
132                 IL_MULTIPAR,
133                 IL_NEEDCPROTECT,
134                 IL_NEEDMBOXPROTECT,
135                 IL_NEEDPROTECT,
136                 IL_NEWLINE_CMD,
137                 IL_PASSTHRU,
138                 IL_PASSTHRU_CHARS,
139                 IL_PARBREAKIGNORED,
140                 IL_PARBREAKISNEWLINE,
141                 IL_PREAMBLE,
142                 IL_REQUIRES,
143                 IL_RIGHTDELIM,
144                 IL_REFPREFIX,
145                 IL_RESETARGS,
146                 IL_RESETSFONT,
147                 IL_SPELLCHECK,
148                 IL_END
149         };
150
151
152         LexerKeyword elementTags[] = {
153                 { "addtotoc", IL_ADDTOTOC },
154                 { "argument", IL_ARGUMENT },
155                 { "babelpreamble", IL_BABELPREAMBLE },
156                 { "bgcolor", IL_BGCOLOR },
157                 { "contentaslabel", IL_CONTENTASLABEL },
158                 { "copystyle", IL_COPYSTYLE },
159                 { "counter", IL_COUNTER},
160                 { "custompars", IL_CUSTOMPARS },
161                 { "decoration", IL_DECORATION },
162                 { "display", IL_DISPLAY },
163                 { "docbookattr", IL_DOCBOOKATTR },
164                 { "docbooksection", IL_DOCBOOKSECTION },
165                 { "docbooktag", IL_DOCBOOKTAG },
166                 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
167                 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
168                 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
169                 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
170                 { "editexternal", IL_EDITEXTERNAL },
171                 { "end", IL_END },
172                 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
173                 { "font", IL_FONT },
174                 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
175                 { "forceltr", IL_FORCELTR },
176                 { "forceownlines", IL_FORCEOWNLINES },
177                 { "forceplain", IL_FORCEPLAIN },
178                 { "freespacing", IL_FREESPACING },
179                 { "htmlattr", IL_HTMLATTR },
180                 { "htmlforcecss", IL_HTMLFORCECSS },
181                 { "htmlinnerattr", IL_HTMLINNERATTR},
182                 { "htmlinnertag", IL_HTMLINNERTAG},
183                 { "htmlisblock", IL_HTMLISBLOCK},
184                 { "htmllabel", IL_HTMLLABEL },
185                 { "htmlpreamble", IL_HTMLPREAMBLE },
186                 { "htmlstyle", IL_HTMLSTYLE },
187                 { "htmltag", IL_HTMLTAG },
188                 { "intoc", IL_INTOC },
189                 { "istoccaption", IL_ISTOCCAPTION },
190                 { "keepempty", IL_KEEPEMPTY },
191                 { "labelfont", IL_LABELFONT },
192                 { "labelstring", IL_LABELSTRING },
193                 { "langpreamble", IL_LANGPREAMBLE },
194                 { "latexname", IL_LATEXNAME },
195                 { "latexparam", IL_LATEXPARAM },
196                 { "latextype", IL_LATEXTYPE },
197                 { "leftdelim", IL_LEFTDELIM },
198                 { "lyxtype", IL_LYXTYPE },
199                 { "menustring", IL_MENUSTRING },
200                 { "multipar", IL_MULTIPAR },
201                 { "needcprotect", IL_NEEDCPROTECT },
202                 { "needmboxprotect", IL_NEEDMBOXPROTECT },
203                 { "needprotect", IL_NEEDPROTECT },
204                 { "newlinecmd", IL_NEWLINE_CMD },
205                 { "obsoletedby", IL_OBSOLETEDBY },
206                 { "parbreakignored", IL_PARBREAKIGNORED },
207                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
208                 { "passthru", IL_PASSTHRU },
209                 { "passthruchars", IL_PASSTHRU_CHARS },
210                 { "preamble", IL_PREAMBLE },
211                 { "refprefix", IL_REFPREFIX },
212                 { "requires", IL_REQUIRES },
213                 { "resetargs", IL_RESETARGS },
214                 { "resetsfont", IL_RESETSFONT },
215                 { "rightdelim", IL_RIGHTDELIM },
216                 { "spellcheck", IL_SPELLCHECK }
217         };
218
219         lex.pushTable(elementTags);
220
221         labelfont_ = inherit_font;
222         bgcolor_ = Color_none;
223         bool getout = false;
224         // whether we've read the CustomPars or ForcePlain tag
225         // for issuing a warning in case MultiPars comes later
226         bool readCustomOrPlain = false;
227
228         string tmp;
229         while (!getout && lex.isOK()) {
230                 int le = lex.lex();
231                 switch (le) {
232                 case Lexer::LEX_UNDEF:
233                         lex.printError("Unknown InsetLayout tag");
234                         if (validating)
235                                 return false;
236                         continue;
237                 default:
238                         break;
239                 }
240                 switch (le) {
241                 // FIXME
242                 // Perhaps a more elegant way to deal with the next two would be the
243                 // way this sort of thing is handled in Layout::read(), namely, by
244                 // using the Lexer.
245                 case IL_LYXTYPE: {
246                         // make sure that we have the right sort of name.
247                         if (name_ != from_ascii("undefined")
248                             && name_.substr(0,5) != from_ascii("Flex:")) {
249                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
250                                         "This one has the name `" << to_utf8(name_) << "'\n"
251                                         "Ignoring LyXType declaration.");
252                                 // this is not really a reason to abort
253                                 if (validating)
254                                         return false;
255                                 break;
256                         }
257                         string lt;
258                         lex >> lt;
259                         lyxtype_ = translateLyXType(lt);
260                         if (lyxtype_  == NOLYXTYPE) {
261                                 LYXERR0("Unknown LyXType `" << lt << "'.");
262                                 // this is not really a reason to abort
263                                 if (validating)
264                                         return false;
265                         }
266                         if (lyxtype_ == CHARSTYLE) {
267                                 // by default, charstyles force the plain layout
268                                 multipar_ = false;
269                                 forceplain_ = true;
270                         }
271                         break;
272                 }
273                 case IL_LATEXTYPE:  {
274                         string lt;
275                         lex >> lt;
276                         latextype_ = translateLaTeXType(lt);
277                         if (latextype_  == ILT_ERROR) {
278                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
279                                 // this is not really a reason to abort
280                                 if (validating)
281                                         return false;
282                         }
283                         break;
284                 }
285                 case IL_LABELSTRING:
286                         lex >> labelstring_;
287                         break;
288                 case IL_MENUSTRING:
289                         lex >> menustring_;
290                         break;
291                 case IL_DECORATION:
292                         lex >> tmp;
293                         decoration_ = translateDecoration(tmp);
294                         break;
295                 case IL_LATEXNAME:
296                         lex >> latexname_;
297                         break;
298                 case IL_LATEXPARAM:
299                         lex >> tmp;
300                         latexparam_ = subst(tmp, "&quot;", "\"");
301                         break;
302                 case IL_LEFTDELIM:
303                         lex >> leftdelim_;
304                         leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
305                                                     from_ascii("\n"));
306                         break;
307                 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
308                         lex >> fixedwidthpreambleencoding_;
309                         break;
310                 case IL_FORCE_LOCAL_FONT_SWITCH:
311                         lex >> forcelocalfontswitch_;
312                         break;
313                 case IL_RIGHTDELIM:
314                         lex >> rightdelim_;
315                         rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
316                                                      from_ascii("\n"));
317                         break;
318                 case IL_LABELFONT:
319                         labelfont_ = lyxRead(lex, inherit_font);
320                         break;
321                 case IL_FORCELTR:
322                         lex >> forceltr_;
323                         break;
324                 case IL_FORCEOWNLINES:
325                         lex >> forceownlines_;
326                         break;
327                 case IL_INTOC:
328                         lex >> intoc_;
329                         break;
330                 case IL_MULTIPAR:
331                         lex >> multipar_;
332                         // the defaults for these depend upon multipar_
333                         if (readCustomOrPlain)
334                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
335                                         "Previous value may be overwritten!");
336                         readCustomOrPlain = false;
337                         custompars_ = multipar_;
338                         forceplain_ = !multipar_;
339                         break;
340                 case IL_COUNTER:
341                         lex >> counter_;
342                         break;
343                 case IL_CUSTOMPARS:
344                         lex >> custompars_;
345                         readCustomOrPlain = true;
346                         break;
347                 case IL_FORCEPLAIN:
348                         lex >> forceplain_;
349                         readCustomOrPlain = true;
350                         break;
351                 case IL_PASSTHRU:
352                         lex >> passthru_;
353                         break;
354                 case IL_PASSTHRU_CHARS:
355                         lex >> passthru_chars_;
356                         break;
357                 case IL_NEWLINE_CMD:
358                         lex >> newline_cmd_;
359                         break;
360                 case IL_PARBREAKIGNORED:
361                         lex >> parbreakignored_;
362                         break;
363                 case IL_PARBREAKISNEWLINE:
364                         lex >> parbreakisnewline_;
365                         break;
366                 case IL_KEEPEMPTY:
367                         lex >> keepempty_;
368                         break;
369                 case IL_FREESPACING:
370                         lex >> freespacing_;
371                         break;
372                 case IL_NEEDPROTECT:
373                         lex >> needprotect_;
374                         break;
375                 case IL_NEEDCPROTECT:
376                         lex >> needcprotect_;
377                         break;
378                 case IL_NEEDMBOXPROTECT:
379                         lex >> needmboxprotect_;
380                         break;
381                 case IL_CONTENTASLABEL:
382                         lex >> contentaslabel_;
383                         break;
384                 case IL_COPYSTYLE: {
385                         // initialize with a known style
386                         docstring style;
387                         lex >> style;
388                         style = subst(style, '_', ' ');
389
390                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
391                         // here. So we do it the long way.
392                         TextClass::InsetLayouts::const_iterator it =
393                                         tclass.insetLayouts().find(style);
394                         if (it != tclass.insetLayouts().end()) {
395                                 docstring const tmpname = name_;
396                                 this->operator=(it->second);
397                                 name_ = tmpname;
398                         } else {
399                                 LYXERR0("Cannot copy unknown InsetLayout `"
400                                         << style << "' to InsetLayout `"
401                                         << name() << "'\n"
402                                         << "All InsetLayouts so far:");
403                                 TextClass::InsetLayouts::const_iterator lit =
404                                                 tclass.insetLayouts().begin();
405                                 TextClass::InsetLayouts::const_iterator len =
406                                                 tclass.insetLayouts().end();
407                                 for (; lit != len; ++lit)
408                                         lyxerr << lit->second.name() << "\n";
409                                 // this is not really a reason to abort
410                                 if (validating)
411                                         return false;
412                         }
413                         break;
414                 }
415                 case IL_OBSOLETEDBY: {
416                         docstring style;
417                         lex >> style;
418                         style = subst(style, '_', ' ');
419
420                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
421                         // here. So we do it the long way.
422                         TextClass::InsetLayouts::const_iterator it =
423                                         tclass.insetLayouts().find(style);
424                         if (it != tclass.insetLayouts().end()) {
425                                 docstring const tmpname = name_;
426                                 this->operator=(it->second);
427                                 name_ = tmpname;
428                                 if (obsoleted_by().empty())
429                                         obsoleted_by_ = style;
430                         } else {
431                                 LYXERR0("Cannot replace InsetLayout `"
432                                         << name()
433                                         << "' with unknown InsetLayout `"
434                                         << style << "'\n"
435                                         << "All InsetLayouts so far:");
436                                 TextClass::InsetLayouts::const_iterator lit =
437                                                 tclass.insetLayouts().begin();
438                                 TextClass::InsetLayouts::const_iterator len =
439                                                 tclass.insetLayouts().end();
440                                 for (; lit != len; ++lit)
441                                         lyxerr << lit->second.name() << "\n";
442                                 // this is not really a reason to abort
443                                 if (validating)
444                                         return false;
445                         }
446                         break;
447                 }
448
449                 case IL_FONT: {
450                         font_ = lyxRead(lex, inherit_font);
451                         // If you want to define labelfont, you need to do so after
452                         // font is defined.
453                         labelfont_ = font_;
454                         break;
455                 }
456                 case IL_RESETARGS:
457                         bool reset;
458                         lex >> reset;
459                         if (reset) {
460                                 latexargs_.clear();
461                                 postcommandargs_.clear();
462                         }
463                         break;
464                 case IL_ARGUMENT:
465                         readArgument(lex);
466                         break;
467                 case IL_BGCOLOR:
468                         lex >> tmp;
469                         bgcolor_ = lcolor.getFromLyXName(tmp);
470                         break;
471                 case IL_PREAMBLE:
472                         preamble_ = lex.getLongString(from_ascii("EndPreamble"));
473                         break;
474                 case IL_BABELPREAMBLE:
475                         babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
476                         break;
477                 case IL_LANGPREAMBLE:
478                         langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
479                         break;
480                 case IL_REFPREFIX:
481                         lex >> refprefix_;
482                         break;
483                 case IL_HTMLTAG:
484                         lex >> htmltag_;
485                         break;
486                 case IL_HTMLATTR:
487                         lex >> htmlattr_;
488                         break;
489                 case IL_HTMLFORCECSS:
490                         lex >> htmlforcecss_;
491                         break;
492                 case IL_HTMLINNERTAG:
493                         lex >> htmlinnertag_;
494                         break;
495                 case IL_HTMLINNERATTR:
496                         lex >> htmlinnerattr_;
497                         break;
498                 case IL_HTMLLABEL:
499                         lex >> htmllabel_;
500                         break;
501                 case IL_HTMLISBLOCK:
502                         lex >> htmlisblock_;
503                         break;
504                 case IL_HTMLSTYLE:
505                         htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
506                         break;
507                 case IL_HTMLPREAMBLE:
508                         htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
509                         break;
510                 case IL_DOCBOOKTAG:
511                         lex >> docbooktag_;
512                         break;
513                 case IL_DOCBOOKTAGTYPE:
514                         lex >> docbooktagtype_;
515                         break;
516                 case IL_DOCBOOKATTR:
517                         lex >> docbookattr_;
518                         break;
519                 case IL_DOCBOOKSECTION:
520                         lex >> docbooksection_;
521                         break;
522                 case IL_DOCBOOKWRAPPERTAG:
523                         lex >> docbookwrappertag_;
524                         break;
525                 case IL_DOCBOOKWRAPPERTAGTYPE:
526                         lex >> docbookwrappertagtype_;
527                         break;
528                 case IL_DOCBOOKWRAPPERATTR:
529                         lex >> docbookwrapperattr_;
530                         break;
531                 case IL_REQUIRES: {
532                         lex.eatLine();
533                         vector<string> const req
534                                 = getVectorFromString(lex.getString(true));
535                         required_.insert(req.begin(), req.end());
536                         break;
537                 }
538                 case IL_SPELLCHECK:
539                         lex >> spellcheck_;
540                         break;
541                 case IL_RESETSFONT:
542                         lex >> resetsfont_;
543                         break;
544                 case IL_DISPLAY:
545                         lex >> display_;
546                         break;
547                 case IL_ADDTOTOC:
548                         lex >> toc_type_;
549                         add_to_toc_ = !toc_type_.empty();
550                         break;
551                 case IL_ISTOCCAPTION:
552                         lex >> is_toc_caption_;
553                         break;
554                 case IL_EDITEXTERNAL:
555                         lex >> edit_external_;
556                         break;
557                 case IL_END:
558                         getout = true;
559                         break;
560                 }
561         }
562
563         // Here add element to list if getout == true
564         if (!getout)
565                 return false;
566
567         // The label font is generally used as-is without
568         // any realization against a given context.
569         labelfont_.realize(sane_font);
570
571         lex.popTable();
572         return true;
573 }
574
575
576 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
577 {
578         if (compare_ascii_no_case(str, "charstyle") == 0)
579                 return InsetLayout::CHARSTYLE;
580         if (compare_ascii_no_case(str, "custom") == 0)
581                 return InsetLayout::CUSTOM;
582         if (compare_ascii_no_case(str, "end") == 0)
583                 return InsetLayout::END;
584         if (compare_ascii_no_case(str, "standard") == 0)
585                 return InsetLayout::STANDARD;
586         return InsetLayout::NOLYXTYPE;
587 }
588
589
590 string const & InsetLayout::htmltag() const
591 {
592         if (htmltag_.empty())
593                 htmltag_ = multipar_ ? "div" : "span";
594         return htmltag_;
595 }
596
597
598 string const & InsetLayout::htmlattr() const
599 {
600         if (htmlattr_.empty())
601                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
602         return htmlattr_;
603 }
604
605
606 string const & InsetLayout::htmlinnerattr() const
607 {
608         if (htmlinnerattr_.empty())
609                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
610         return htmlinnerattr_;
611 }
612
613
614 string InsetLayout::defaultCSSClass() const
615 {
616         if (!defaultcssclass_.empty())
617                 return defaultcssclass_;
618         string d;
619         string n = to_utf8(name());
620         string::const_iterator it = n.begin();
621         string::const_iterator en = n.end();
622         for (; it != en; ++it) {
623                 if (!isAlphaASCII(*it))
624                         d += "_";
625                 else if (isLower(*it))
626                         d += *it;
627                 else
628                         d += lowercase(*it);
629         }
630         // are there other characters we need to remove?
631         defaultcssclass_ = d;
632         return defaultcssclass_;
633 }
634
635
636 void InsetLayout::makeDefaultCSS() const
637 {
638         if (!htmldefaultstyle_.empty())
639                 return;
640         docstring const mainfontCSS = font_.asCSS();
641         if (!mainfontCSS.empty())
642                 htmldefaultstyle_ =
643                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
644                                 mainfontCSS + from_ascii("\n}\n");
645 }
646
647
648 docstring InsetLayout::htmlstyle() const
649 {
650         if (!htmlstyle_.empty() && !htmlforcecss_)
651                 return htmlstyle_;
652         if (htmldefaultstyle_.empty())
653                 makeDefaultCSS();
654         docstring retval = htmldefaultstyle_;
655         if (!htmlstyle_.empty())
656                 retval += '\n' + htmlstyle_ + '\n';
657         return retval;
658 }
659
660 void InsetLayout::readArgument(Lexer & lex)
661 {
662         Layout::latexarg arg;
663         arg.mandatory = false;
664         arg.autoinsert = false;
665         arg.insertcotext = false;
666         arg.insertonnewline = false;
667         bool error = false;
668         bool finished = false;
669         arg.font = inherit_font;
670         arg.labelfont = inherit_font;
671         arg.is_toc_caption = false;
672         arg.free_spacing = false;
673         arg.passthru = PT_INHERITED;
674         arg.nodelims = false;
675         string nr;
676         lex >> nr;
677         bool const postcmd = prefixIs(nr, "post:");
678         while (!finished && lex.isOK() && !error) {
679                 lex.next();
680                 string const tok = ascii_lowercase(lex.getString());
681
682                 if (tok.empty()) {
683                         continue;
684                 } else if (tok == "endargument") {
685                         finished = true;
686                 } else if (tok == "labelstring") {
687                         lex.next();
688                         arg.labelstring = lex.getDocString();
689                 } else if (tok == "menustring") {
690                         lex.next();
691                         arg.menustring = lex.getDocString();
692                 } else if (tok == "mandatory") {
693                         lex.next();
694                         arg.mandatory = lex.getBool();
695                 } else if (tok == "autoinsert") {
696                         lex.next();
697                         arg.autoinsert = lex.getBool();
698                 } else if (tok == "insertcotext") {
699                         lex.next();
700                         arg.insertcotext = lex.getBool();
701                 } else if (tok == "insertonnewline") {
702                         lex.next();
703                         arg.insertonnewline = lex.getBool();
704                 } else if (tok == "leftdelim") {
705                         lex.next();
706                         arg.ldelim = lex.getDocString();
707                         arg.ldelim = subst(arg.ldelim,
708                                                     from_ascii("<br/>"), from_ascii("\n"));
709                 } else if (tok == "rightdelim") {
710                         lex.next();
711                         arg.rdelim = lex.getDocString();
712                         arg.rdelim = subst(arg.rdelim,
713                                                     from_ascii("<br/>"), from_ascii("\n"));
714                 } else if (tok == "defaultarg") {
715                         lex.next();
716                         arg.defaultarg = lex.getDocString();
717                 } else if (tok == "presetarg") {
718                         lex.next();
719                         arg.presetarg = lex.getDocString();
720                 } else if (tok == "tooltip") {
721                         lex.next();
722                         arg.tooltip = lex.getDocString();
723                 } else if (tok == "requires") {
724                         lex.next();
725                         arg.required = lex.getString();
726                 } else if (tok == "decoration") {
727                         lex.next();
728                         arg.decoration = lex.getString();
729                 } else if (tok == "newlinecmd") {
730                         lex.next();
731                         arg.newlinecmd = lex.getString();
732                 } else if (tok == "font") {
733                         arg.font = lyxRead(lex, arg.font);
734                 } else if (tok == "labelfont") {
735                         arg.labelfont = lyxRead(lex, arg.labelfont);
736                 } else if (tok == "passthruchars") {
737                         lex.next();
738                         arg.pass_thru_chars = lex.getDocString();
739                 } else if (tok == "passthru") {
740                         lex.next();
741                         docstring value = lex.getDocString();
742                         if (value == "true" || value == "1")
743                                 arg.passthru = PT_TRUE;
744                         else if (value == "false" || value == "0")
745                                 arg.passthru = PT_FALSE;
746                         else
747                                 arg.passthru = PT_INHERITED;
748                 } else if (tok == "istoccaption") {
749                         lex.next();
750                         arg.is_toc_caption = lex.getBool();
751                 } else if (tok == "freespacing") {
752                         lex.next();
753                         arg.free_spacing = lex.getBool();
754                 } else {
755                         lex.printError("Unknown tag");
756                         error = true;
757                 }
758         }
759         if (arg.labelstring.empty())
760                 LYXERR0("Incomplete Argument definition!");
761         else if (postcmd)
762                 postcommandargs_[nr] = arg;
763         else
764                 latexargs_[nr] = arg;
765 }
766
767
768 Layout::LaTeXArgMap InsetLayout::args() const
769 {
770         Layout::LaTeXArgMap args = latexargs_;
771         if (!postcommandargs_.empty())
772                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
773         return args;
774 }
775
776
777 int InsetLayout::optArgs() const
778 {
779         int nr = 0;
780         Layout::LaTeXArgMap const args = InsetLayout::args();
781         Layout::LaTeXArgMap::const_iterator it = args.begin();
782         for (; it != args.end(); ++it) {
783                 if (!(*it).second.mandatory)
784                         ++nr;
785         }
786         return nr;
787 }
788
789
790 int InsetLayout::requiredArgs() const
791 {
792         int nr = 0;
793         Layout::LaTeXArgMap const args = InsetLayout::args();
794         Layout::LaTeXArgMap::const_iterator it = args.begin();
795         for (; it != args.end(); ++it) {
796                 if ((*it).second.mandatory)
797                         ++nr;
798         }
799         return nr;
800 }
801
802
803 } //namespace lyx