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