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