3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
16 #include "BufferView.h"
17 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
25 #include "insets/InsetBibitem.h"
26 #include "insets/InsetBibtex.h"
27 #include "insets/InsetCaption.h"
28 #include "insets/InsetCitation.h"
29 #include "insets/InsetCharStyle.h"
30 #include "insets/InsetEnvironment.h"
31 #include "insets/InsetERT.h"
32 #include "insets/InsetListings.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetFloatList.h"
36 #include "insets/InsetFoot.h"
37 #include "insets/InsetGraphics.h"
38 #include "insets/InsetHFill.h"
39 #include "insets/InsetInclude.h"
40 #include "insets/InsetIndex.h"
41 #include "insets/InsetNomencl.h"
42 #include "insets/InsetLabel.h"
43 #include "insets/InsetLine.h"
44 #include "insets/InsetMarginal.h"
45 #include "insets/InsetNote.h"
46 #include "insets/InsetBox.h"
47 #include "insets/InsetBranch.h"
48 #include "insets/InsetOptArg.h"
49 #include "insets/InsetPagebreak.h"
50 #include "insets/InsetRef.h"
51 #include "insets/InsetSpace.h"
52 #include "insets/InsetTabular.h"
53 #include "insets/InsetTOC.h"
54 #include "insets/InsetUrl.h"
55 #include "insets/InsetVSpace.h"
56 #include "insets/InsetWrap.h"
58 #include "mathed/MathMacroTemplate.h"
59 #include "mathed/InsetMathHull.h"
61 #include "frontends/alert.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
66 #include <boost/assert.hpp>
67 #include <boost/current_function.hpp>
78 namespace Alert = frontend::Alert;
80 using support::compare_ascii_no_case;
83 Inset * createInset(BufferView * bv, FuncRequest const & cmd)
85 BufferParams const & params = bv->buffer().params();
90 case LFUN_HFILL_INSERT:
91 return new InsetHFill;
93 case LFUN_LINE_INSERT:
96 case LFUN_PAGEBREAK_INSERT:
97 return new InsetPagebreak;
99 case LFUN_CLEARPAGE_INSERT:
100 return new InsetClearPage;
102 case LFUN_CLEARDOUBLEPAGE_INSERT:
103 return new InsetClearDoublePage;
105 case LFUN_CHARSTYLE_INSERT: {
106 string s = cmd.getArg(0);
107 TextClass tclass = params.getTextClass();
108 CharStyles::iterator found_cs = tclass.charstyle(s);
109 if (found_cs != tclass.charstyles().end())
110 return new InsetCharStyle(params, found_cs);
112 return new InsetCharStyle(params, s);
115 case LFUN_NOTE_INSERT: {
116 string arg = cmd.getArg(0);
119 return new InsetNote(params, arg);
122 case LFUN_BOX_INSERT: {
123 string arg = cmd.getArg(0);
126 return new InsetBox(params, arg);
129 case LFUN_BRANCH_INSERT: {
130 docstring arg = cmd.argument();
132 arg = from_ascii("none");
133 return new InsetBranch(params, InsetBranchParams(arg));
136 case LFUN_ERT_INSERT:
137 return new InsetERT(params);
139 case LFUN_LISTING_INSERT:
140 return new InsetListings(params);
142 case LFUN_FOOTNOTE_INSERT:
143 return new InsetFoot(params);
145 case LFUN_MARGINALNOTE_INSERT:
146 return new InsetMarginal(params);
148 case LFUN_OPTIONAL_INSERT:
149 return new InsetOptArg(params);
151 case LFUN_BIBITEM_INSERT:
152 return new InsetBibitem(InsetCommandParams("bibitem"));
154 case LFUN_FLOAT_INSERT: {
155 // check if the float type exists
156 string const argument = to_utf8(cmd.argument());
157 if (params.getTextClass().floats().typeExist(argument))
158 return new InsetFloat(params, argument);
159 lyxerr << "Non-existent float type: " << argument << endl;
163 case LFUN_FLOAT_WIDE_INSERT: {
164 // check if the float type exists
165 string const argument = to_utf8(cmd.argument());
166 if (params.getTextClass().floats().typeExist(argument)) {
167 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
168 p->wide(true, params);
171 lyxerr << "Non-existent float type: " << argument << endl;
175 case LFUN_WRAP_INSERT: {
176 string const argument = to_utf8(cmd.argument());
177 if (argument == "figure")
178 return new InsetWrap(params, argument);
179 lyxerr << "Non-existent floatflt type: " << argument << endl;
183 case LFUN_INDEX_INSERT: {
184 // Try and generate a valid index entry.
185 InsetCommandParams icp("index");
186 icp["name"] = cmd.argument().empty() ?
187 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
189 return new InsetIndex(icp);
192 case LFUN_NOMENCL_INSERT: {
193 InsetCommandParams icp("nomenclature");
194 icp["symbol"] = cmd.argument().empty() ?
195 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
197 return new InsetNomencl(icp);
200 case LFUN_TABULAR_INSERT: {
201 if (cmd.argument().empty())
203 std::istringstream ss(to_utf8(cmd.argument()));
210 return new InsetTabular(bv->buffer(), r, c);
213 case LFUN_CAPTION_INSERT: {
214 auto_ptr<InsetCaption> inset(new InsetCaption(params));
215 inset->setAutoBreakRows(true);
216 inset->setDrawFrame(true);
217 inset->setFrameColor(Color::captionframe);
218 return inset.release();
221 case LFUN_INDEX_PRINT:
222 return new InsetPrintIndex(InsetCommandParams("printindex"));
224 case LFUN_NOMENCL_PRINT:
225 return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
227 case LFUN_TOC_INSERT:
228 return new InsetTOC(InsetCommandParams("tableofcontents"));
230 case LFUN_ENVIRONMENT_INSERT:
231 return new InsetEnvironment(params, cmd.argument());
234 case LFUN_LIST_INSERT:
235 return new InsetList;
237 case LFUN_THEOREM_INSERT:
238 return new InsetTheorem;
241 case LFUN_INSET_INSERT: {
242 string const name = cmd.getArg(0);
244 if (name == "bibitem") {
245 InsetCommandParams icp(name);
246 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
248 return new InsetBibitem(icp);
250 } else if (name == "bibtex") {
251 InsetCommandParams icp(name);
252 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
254 return new InsetBibtex(icp);
256 } else if (name == "citation") {
257 InsetCommandParams icp("cite");
258 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
260 return new InsetCitation(icp);
262 } else if (name == "ert") {
263 InsetCollapsable::CollapseStatus st;
264 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
265 return new InsetERT(params, st);
267 } else if (name == "listings") {
268 InsetListingsParams par;
269 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
270 return new InsetListings(params, par);
272 } else if (name == "external") {
273 Buffer const & buffer = bv->buffer();
274 InsetExternalParams iep;
275 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
277 auto_ptr<InsetExternal> inset(new InsetExternal);
278 inset->setParams(iep, buffer);
279 return inset.release();
281 } else if (name == "graphics") {
282 Buffer const & buffer = bv->buffer();
283 InsetGraphicsParams igp;
284 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
286 auto_ptr<InsetGraphics> inset(new InsetGraphics);
287 inset->setParams(igp);
288 return inset.release();
290 } else if (name == "include") {
291 InsetCommandParams iip(name);
292 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
293 return new InsetInclude(iip);
295 } else if (name == "index") {
296 InsetCommandParams icp(name);
297 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
299 return new InsetIndex(icp);
301 } else if (name == "nomenclature") {
302 InsetCommandParams icp(name);
303 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
305 return new InsetNomencl(icp);
307 } else if (name == "label") {
308 InsetCommandParams icp(name);
309 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
311 return new InsetLabel(icp);
313 } else if (name == "ref") {
314 InsetCommandParams icp(name);
315 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
317 return new InsetRef(icp, bv->buffer());
319 } else if (name == "toc") {
320 InsetCommandParams icp("tableofcontents");
321 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
323 return new InsetTOC(icp);
325 } else if (name == "url") {
326 InsetCommandParams icp(name);
327 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
329 return new InsetUrl(icp);
331 } else if (name == "vspace") {
333 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
334 return new InsetVSpace(vspace);
338 case LFUN_SPACE_INSERT: {
339 string const name = to_utf8(cmd.argument());
340 if (name == "normal")
341 return new InsetSpace(InsetSpace::NORMAL);
342 else if (name == "protected")
343 return new InsetSpace(InsetSpace::PROTECTED);
344 else if (name == "thin")
345 return new InsetSpace(InsetSpace::THIN);
346 else if (name == "quad")
347 return new InsetSpace(InsetSpace::QUAD);
348 else if (name == "qquad")
349 return new InsetSpace(InsetSpace::QQUAD);
350 else if (name == "enspace")
351 return new InsetSpace(InsetSpace::ENSPACE);
352 else if (name == "enskip")
353 return new InsetSpace(InsetSpace::ENSKIP);
354 else if (name == "negthinspace")
355 return new InsetSpace(InsetSpace::NEGTHIN);
356 else if (name.empty())
357 lyxerr << "LyX function 'space' needs an argument." << endl;
359 lyxerr << "Wrong argument for LyX function 'space'." << endl;
367 } catch (support::ExceptionMessage const & message) {
368 if (message.type_ == support::ErrorException) {
369 Alert::error(message.title_, message.details_);
370 LyX::cref().emergencyCleanup();
372 } else if (message.type_ == support::WarningException) {
373 Alert::warning(message.title_, message.details_);
383 Inset * readInset(Lexer & lex, Buffer const & buf)
386 if (lex.getString() != "\\begin_inset") {
387 lyxerr << "Buffer::readInset: Consistency check failed."
391 auto_ptr<Inset> inset;
393 TextClass tclass = buf.params().getTextClass();
396 string tmptok = lex.getString();
398 // test the different insets
399 if (tmptok == "LatexCommand") {
401 string const cmdName = lex.getString();
402 lex.pushToken(cmdName);
404 InsetCommandParams inscmd(cmdName);
407 // This strange command allows LyX to recognize "natbib" style
408 // citations: citet, citep, Citet etc.
409 // FIXME: We already have partial support for \\fullcite and
410 // the various \\footcite commands. We should increase the
411 // file format number and read these commands here, too.
412 // Then we should use is_possible_cite_command() in
413 // src/frontends/controllers/frontend_helpers.cpp to test for valid cite
415 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
416 inset.reset(new InsetCitation(inscmd));
417 } else if (cmdName == "bibitem") {
418 inset.reset(new InsetBibitem(inscmd));
419 } else if (cmdName == "bibtex") {
420 inset.reset(new InsetBibtex(inscmd));
421 } else if (cmdName == "index") {
422 inset.reset(new InsetIndex(inscmd));
423 } else if (cmdName == "nomenclature") {
424 inset.reset(new InsetNomencl(inscmd));
425 } else if (cmdName == "include") {
426 inset.reset(new InsetInclude(inscmd));
427 } else if (cmdName == "label") {
428 inset.reset(new InsetLabel(inscmd));
429 } else if (cmdName == "url"
430 || cmdName == "htmlurl") {
431 inset.reset(new InsetUrl(inscmd));
432 } else if (cmdName == "ref"
433 || cmdName == "eqref"
434 || cmdName == "pageref"
436 || cmdName == "vpageref"
437 || cmdName == "prettyref") {
438 if (!inscmd["name"].empty()
439 || !inscmd["reference"].empty()) {
440 inset.reset(new InsetRef(inscmd, buf));
442 } else if (cmdName == "tableofcontents") {
443 inset.reset(new InsetTOC(inscmd));
444 } else if (cmdName == "listofalgorithms") {
445 inset.reset(new InsetFloatList("algorithm"));
446 } else if (cmdName == "listoffigures") {
447 inset.reset(new InsetFloatList("figure"));
448 } else if (cmdName == "listoftables") {
449 inset.reset(new InsetFloatList("table"));
450 } else if (cmdName == "printindex") {
451 inset.reset(new InsetPrintIndex(inscmd));
452 } else if (cmdName == "printnomenclature") {
453 inset.reset(new InsetPrintNomencl(inscmd));
455 lyxerr << "unknown CommandInset '" << cmdName
457 while (lex.isOK() && lex.getString() != "\\end_inset")
462 if (tmptok == "Quotes") {
463 inset.reset(new InsetQuotes);
464 } else if (tmptok == "External") {
465 inset.reset(new InsetExternal);
466 } else if (tmptok == "FormulaMacro") {
467 inset.reset(new MathMacroTemplate);
468 } else if (tmptok == "Formula") {
469 inset.reset(new InsetMathHull);
470 } else if (tmptok == "Graphics") {
471 inset.reset(new InsetGraphics);
472 } else if (tmptok == "Note") {
473 inset.reset(new InsetNote(buf.params(), tmptok));
474 } else if (tmptok == "Box") {
475 inset.reset(new InsetBox(buf.params(), tmptok));
476 } else if (tmptok == "CharStyle") {
478 string s = lex.getString();
479 CharStyles::iterator found_cs = tclass.charstyle(s);
480 if (found_cs != tclass.charstyles().end())
481 inset.reset(new InsetCharStyle(buf.params(), found_cs));
484 inset.reset(new InsetCharStyle(buf.params(), s));
486 } else if (tmptok == "Branch") {
487 inset.reset(new InsetBranch(buf.params(),
488 InsetBranchParams()));
489 } else if (tmptok == "Include") {
490 InsetCommandParams p("include");
491 inset.reset(new InsetInclude(p));
492 } else if (tmptok == "Environment") {
494 inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
495 } else if (tmptok == "ERT") {
496 inset.reset(new InsetERT(buf.params()));
497 } else if (tmptok == "listings") {
498 inset.reset(new InsetListings(buf.params()));
499 } else if (tmptok == "InsetSpace") {
500 inset.reset(new InsetSpace);
501 } else if (tmptok == "Tabular") {
502 inset.reset(new InsetTabular(buf));
503 } else if (tmptok == "Text") {
504 inset.reset(new InsetText(buf.params()));
505 } else if (tmptok == "VSpace") {
506 inset.reset(new InsetVSpace);
507 } else if (tmptok == "Foot") {
508 inset.reset(new InsetFoot(buf.params()));
509 } else if (tmptok == "Marginal") {
510 inset.reset(new InsetMarginal(buf.params()));
511 } else if (tmptok == "OptArg") {
512 inset.reset(new InsetOptArg(buf.params()));
513 } else if (tmptok == "Float") {
515 string tmptok = lex.getString();
516 inset.reset(new InsetFloat(buf.params(), tmptok));
517 } else if (tmptok == "Wrap") {
519 string tmptok = lex.getString();
520 inset.reset(new InsetWrap(buf.params(), tmptok));
522 } else if (tmptok == "List") {
523 inset.reset(new InsetList);
524 } else if (tmptok == "Theorem") {
525 inset.reset(new InsetList);
527 } else if (tmptok == "Caption") {
528 inset.reset(new InsetCaption(buf.params()));
529 } else if (tmptok == "FloatList") {
530 inset.reset(new InsetFloatList);
532 lyxerr << "unknown Inset type '" << tmptok
534 while (lex.isOK() && lex.getString() != "\\end_inset")
539 inset->read(buf, lex);
542 if (inset->lyxCode() == Inset::MATHMACRO_CODE) {
543 MathMacroTemplate const * tmpl =
544 static_cast<MathMacroTemplate*>(inset.get());
545 MacroTable::globalMacros().insert
546 (tmpl->name(), tmpl->asMacroData());
548 << BOOST_CURRENT_FUNCTION
549 << ": creating local macro " << to_utf8(tmpl->name())
554 return inset.release();