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