]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Add ObsoletedBy tag to InsetLayout
[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 "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 << "'\n"
325                                         << "All InsetLayouts so far:");
326                                 TextClass::InsetLayouts::const_iterator lit = 
327                                                 tclass.insetLayouts().begin();
328                                 TextClass::InsetLayouts::const_iterator len = 
329                                                 tclass.insetLayouts().end();
330                                 for (; lit != len; ++lit)
331                                         lyxerr << lit->second.name() << "\n";
332                         }
333                         break;
334                 }
335                 case IL_OBSOLETEDBY: {
336                         docstring style;
337                         lex >> style;
338                         style = support::subst(style, '_', ' ');
339
340                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
341                         // here. So we do it the long way.
342                         TextClass::InsetLayouts::const_iterator it =
343                                         tclass.insetLayouts().find(style);
344                         if (it != tclass.insetLayouts().end()) {
345                                 docstring const tmpname = name_;
346                                 this->operator=(it->second);
347                                 name_ = tmpname;
348                                 if (obsoleted_by().empty())
349                                         obsoleted_by_ = style;
350                         } else {
351                                 LYXERR0("Cannot replace with unknown InsetLayout `"
352                                         << style << '\'');
353                         }
354                         break;
355                 }
356
357                 case IL_FONT: {
358                         font_ = lyxRead(lex, inherit_font);
359                         // If you want to define labelfont, you need to do so after
360                         // font is defined.
361                         labelfont_ = font_;
362                         break;
363                 }
364                 case IL_RESETARGS:
365                         bool reset;
366                         lex >> reset;
367                         if (reset) {
368                                 latexargs_.clear();
369                                 postcommandargs_.clear();
370                         }
371                         break;
372                 case IL_ARGUMENT:
373                         readArgument(lex);
374                         break;
375                 case IL_BGCOLOR:
376                         lex >> tmp;
377                         bgcolor_ = lcolor.getFromLyXName(tmp);
378                         break;
379                 case IL_PREAMBLE:
380                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
381                         break;
382                 case IL_BABELPREAMBLE:
383                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
384                         break;
385                 case IL_LANGPREAMBLE:
386                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
387                         break;
388                 case IL_REFPREFIX:
389                         lex >> refprefix_;
390                         break;
391                 case IL_HTMLTAG:
392                         lex >> htmltag_;
393                         break;
394                 case IL_HTMLATTR:
395                         lex >> htmlattr_;
396                         break;
397                 case IL_HTMLFORCECSS:
398                         lex >> htmlforcecss_;
399                         break;
400                 case IL_HTMLINNERTAG:
401                         lex >> htmlinnertag_;
402                         break;
403                 case IL_HTMLINNERATTR:
404                         lex >> htmlinnerattr_;
405                         break;
406                 case IL_HTMLLABEL:
407                         lex >> htmllabel_;
408                         break;
409                 case IL_HTMLISBLOCK:
410                         lex >> htmlisblock_;
411                         break;
412                 case IL_HTMLSTYLE:
413                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
414                         break;
415                 case IL_HTMLPREAMBLE:
416                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
417                         break;
418                 case IL_REQUIRES: {
419                         lex.eatLine();
420                         vector<string> const req 
421                                 = support::getVectorFromString(lex.getString());
422                         requires_.insert(req.begin(), req.end());
423                         break;
424                 }
425                 case IL_SPELLCHECK:
426                         lex >> spellcheck_;
427                         break;
428                 case IL_RESETSFONT:
429                         lex >> resetsfont_;
430                         break;
431                 case IL_DISPLAY:
432                         lex >> display_;
433                         break;
434                 case IL_END:
435                         getout = true;
436                         break;
437                 }
438         }
439
440         // Here add element to list if getout == true
441         if (!getout)
442                 return false;
443         
444         // The label font is generally used as-is without
445         // any realization against a given context.
446         labelfont_.realize(sane_font);
447
448         lex.popTable();
449         return true;
450 }
451
452
453 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
454 {
455         
456         if (support::compare_ascii_no_case(str, "charstyle") == 0)
457                 return InsetLayout::CHARSTYLE;
458         if (support::compare_ascii_no_case(str, "custom") == 0)
459                 return InsetLayout::CUSTOM;
460         if (support::compare_ascii_no_case(str, "element") == 0)
461                 return InsetLayout::ELEMENT;
462         if (support::compare_ascii_no_case(str, "end") == 0)
463                 return InsetLayout::END;
464         if (support::compare_ascii_no_case(str, "standard") == 0)
465                 return InsetLayout::STANDARD;
466         return InsetLayout::NOLYXTYPE;
467 }
468
469
470 string const & InsetLayout::htmltag() const
471 {
472         if (htmltag_.empty())
473                 htmltag_ = multipar_ ? "div" : "span";
474         return htmltag_; 
475 }
476
477
478 string const & InsetLayout::htmlattr() const
479 {
480         if (htmlattr_.empty())
481                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
482         return htmlattr_; 
483 }
484
485
486 string const & InsetLayout::htmlinnerattr() const
487 {
488         if (htmlinnerattr_.empty())
489                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
490         return htmlinnerattr_; 
491 }
492
493
494 string InsetLayout::defaultCSSClass() const
495
496         if (!defaultcssclass_.empty())
497                 return defaultcssclass_;
498         string d;
499         string n = to_utf8(name());
500         string::const_iterator it = n.begin();
501         string::const_iterator en = n.end();
502         for (; it != en; ++it) {
503                 if (!isAlphaASCII(*it))
504                         d += "_";
505                 else if (isLower(*it))
506                         d += *it;
507                 else
508                         d += support::lowercase(*it);
509         }
510         // are there other characters we need to remove?
511         defaultcssclass_ = d;
512         return defaultcssclass_;
513 }
514
515
516 void InsetLayout::makeDefaultCSS() const
517 {
518         if (!htmldefaultstyle_.empty()) 
519                 return;
520         docstring const mainfontCSS = font_.asCSS();
521         if (!mainfontCSS.empty())
522                 htmldefaultstyle_ = 
523                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
524                                 mainfontCSS + from_ascii("\n}\n");
525 }
526
527
528 docstring InsetLayout::htmlstyle() const 
529
530         if (!htmlstyle_.empty() && !htmlforcecss_)
531                 return htmlstyle_;
532         if (htmldefaultstyle_.empty())
533                 makeDefaultCSS();
534         docstring retval = htmldefaultstyle_;
535         if (!htmlstyle_.empty())
536                 retval += '\n' + htmlstyle_ + '\n';
537         return retval;
538 }
539
540 void InsetLayout::readArgument(Lexer & lex)
541 {
542         Layout::latexarg arg;
543         arg.mandatory = false;
544         arg.autoinsert = false;
545         bool error = false;
546         bool finished = false;
547         arg.font = inherit_font;
548         arg.labelfont = inherit_font;
549         string nr;
550         lex >> nr;
551         bool const postcmd = support::prefixIs(nr, "post:");
552         while (!finished && lex.isOK() && !error) {
553                 lex.next();
554                 string const tok = support::ascii_lowercase(lex.getString());
555
556                 if (tok.empty()) {
557                         continue;
558                 } else if (tok == "endargument") {
559                         finished = true;
560                 } else if (tok == "labelstring") {
561                         lex.next();
562                         arg.labelstring = lex.getDocString();
563                 } else if (tok == "menustring") {
564                         lex.next();
565                         arg.menustring = lex.getDocString();
566                 } else if (tok == "mandatory") {
567                         lex.next();
568                         arg.mandatory = lex.getBool();
569                 } else if (tok == "autoinsert") {
570                         lex.next();
571                         arg.autoinsert = lex.getBool();
572                 } else if (tok == "leftdelim") {
573                         lex.next();
574                         arg.ldelim = lex.getDocString();
575                         arg.ldelim = support::subst(arg.ldelim,
576                                                     from_ascii("<br/>"), from_ascii("\n"));
577                 } else if (tok == "rightdelim") {
578                         lex.next();
579                         arg.rdelim = lex.getDocString();
580                         arg.rdelim = support::subst(arg.rdelim,
581                                                     from_ascii("<br/>"), from_ascii("\n"));
582                 } else if (tok == "defaultarg") {
583                         lex.next();
584                         arg.defaultarg = lex.getDocString();
585                 } else if (tok == "presetarg") {
586                         lex.next();
587                         arg.presetarg = lex.getDocString();
588                 } else if (tok == "tooltip") {
589                         lex.next();
590                         arg.tooltip = lex.getDocString();
591                 } else if (tok == "requires") {
592                         lex.next();
593                         arg.requires = lex.getString();
594                 } else if (tok == "decoration") {
595                         lex.next();
596                         arg.decoration = lex.getString();
597                 } else if (tok == "font") {
598                         arg.font = lyxRead(lex, arg.font);
599                 } else if (tok == "labelfont") {
600                         arg.labelfont = lyxRead(lex, arg.labelfont);
601                 } else {
602                         lex.printError("Unknown tag");
603                         error = true;
604                 }
605         }
606         if (arg.labelstring.empty())
607                 LYXERR0("Incomplete Argument definition!");
608         else if (postcmd)
609                 postcommandargs_[nr] = arg;
610         else
611                 latexargs_[nr] = arg;
612 }
613
614
615 Layout::LaTeXArgMap InsetLayout::args() const
616 {
617         Layout::LaTeXArgMap args = latexargs_;
618         if (!postcommandargs_.empty())
619                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
620         return args;
621 }
622
623
624 unsigned int InsetLayout::optArgs() const
625 {
626         unsigned int nr = 0;
627         Layout::LaTeXArgMap const args = InsetLayout::args();
628         Layout::LaTeXArgMap::const_iterator it = args.begin();
629         for (; it != args.end(); ++it) {
630                 if (!(*it).second.mandatory)
631                         ++nr;
632         }
633         return nr;
634 }
635
636
637 unsigned int InsetLayout::requiredArgs() const
638 {
639         unsigned int nr = 0;
640         Layout::LaTeXArgMap const args = InsetLayout::args();
641         Layout::LaTeXArgMap::const_iterator it = args.begin();
642         for (; it != args.end(); ++it) {
643                 if ((*it).second.mandatory)
644                         ++nr;
645         }
646         return nr;
647 }
648
649
650 } //namespace lyx