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