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"
24 #include "Paragraph.h"
26 #include "insets/InsetBibitem.h"
27 #include "insets/InsetBibtex.h"
28 #include "insets/InsetCaption.h"
29 #include "insets/InsetCitation.h"
30 #include "insets/InsetCharStyle.h"
31 #include "insets/InsetEnv.h"
32 #include "insets/InsetERT.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>
74 namespace Alert = frontend::Alert;
76 using support::compare_ascii_no_case;
83 InsetBase * 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 LyXTextClass tclass = params.getLyXTextClass();
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_FOOTNOTE_INSERT:
140 return new InsetFoot(params);
142 case LFUN_MARGINALNOTE_INSERT:
143 return new InsetMarginal(params);
145 case LFUN_OPTIONAL_INSERT:
146 return new InsetOptArg(params);
148 case LFUN_BIBITEM_INSERT:
149 return new InsetBibitem(InsetCommandParams("bibitem"));
151 case LFUN_FLOAT_INSERT: {
152 // check if the float type exists
153 string const argument = to_utf8(cmd.argument());
154 if (params.getLyXTextClass().floats().typeExist(argument))
155 return new InsetFloat(params, argument);
156 lyxerr << "Non-existent float type: " << argument << endl;
160 case LFUN_FLOAT_WIDE_INSERT: {
161 // check if the float type exists
162 string const argument = to_utf8(cmd.argument());
163 if (params.getLyXTextClass().floats().typeExist(argument)) {
164 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
165 p->wide(true, params);
168 lyxerr << "Non-existent float type: " << argument << endl;
172 case LFUN_WRAP_INSERT: {
173 string const argument = to_utf8(cmd.argument());
174 if (argument == "figure")
175 return new InsetWrap(params, argument);
176 lyxerr << "Non-existent floatflt type: " << argument << endl;
180 case LFUN_INDEX_INSERT: {
181 // Try and generate a valid index entry.
182 InsetCommandParams icp("index");
183 icp["name"] = cmd.argument().empty() ?
184 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
186 return new InsetIndex(icp);
189 case LFUN_NOMENCL_INSERT: {
190 InsetCommandParams icp("nomenclature");
191 icp["symbol"] = cmd.argument().empty() ?
192 bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
194 return new InsetNomencl(icp);
197 case LFUN_TABULAR_INSERT: {
198 if (cmd.argument().empty())
200 std::istringstream ss(to_utf8(cmd.argument()));
207 return new InsetTabular(*bv->buffer(), r, c);
210 case LFUN_CAPTION_INSERT: {
211 auto_ptr<InsetCaption> inset(new InsetCaption(params));
212 inset->setAutoBreakRows(true);
213 inset->setDrawFrame(true);
214 inset->setFrameColor(LColor::captionframe);
215 return inset.release();
218 case LFUN_INDEX_PRINT:
219 return new InsetPrintIndex(InsetCommandParams("printindex"));
221 case LFUN_NOMENCL_PRINT:
222 return new InsetPrintNomencl(InsetCommandParams("printnomenclature"));
224 case LFUN_TOC_INSERT:
225 return new InsetTOC(InsetCommandParams("tableofcontents"));
227 case LFUN_ENVIRONMENT_INSERT:
228 return new InsetEnvironment(params, to_utf8(cmd.argument()));
231 case LFUN_LIST_INSERT:
232 return new InsetList;
234 case LFUN_THEOREM_INSERT:
235 return new InsetTheorem;
238 case LFUN_INSET_INSERT: {
239 string const name = cmd.getArg(0);
241 if (name == "bibitem") {
242 InsetCommandParams icp(name);
243 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
245 return new InsetBibitem(icp);
247 } else if (name == "bibtex") {
248 InsetCommandParams icp(name);
249 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
251 return new InsetBibtex(icp);
253 } else if (name == "citation") {
254 InsetCommandParams icp("cite");
255 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
257 return new InsetCitation(icp);
259 } else if (name == "ert") {
260 InsetCollapsable::CollapseStatus st;
261 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
262 return new InsetERT(params, st);
264 } else if (name == "external") {
265 Buffer const & buffer = *bv->buffer();
266 InsetExternalParams iep;
267 InsetExternalMailer::string2params(to_utf8(cmd.argument()),
269 auto_ptr<InsetExternal> inset(new InsetExternal);
270 inset->setParams(iep, buffer);
271 return inset.release();
273 } else if (name == "graphics") {
274 Buffer const & buffer = *bv->buffer();
275 InsetGraphicsParams igp;
276 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()),
278 auto_ptr<InsetGraphics> inset(new InsetGraphics);
279 inset->setParams(igp);
280 return inset.release();
282 } else if (name == "include") {
283 InsetCommandParams iip(name);
284 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
285 return new InsetInclude(iip);
287 } else if (name == "index") {
288 InsetCommandParams icp(name);
289 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
291 return new InsetIndex(icp);
293 } else if (name == "nomenclature") {
294 InsetCommandParams icp(name);
295 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()),
297 return new InsetNomencl(icp);
299 } else if (name == "label") {
300 InsetCommandParams icp(name);
301 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
303 return new InsetLabel(icp);
305 } else if (name == "ref") {
306 InsetCommandParams icp(name);
307 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
309 return new InsetRef(icp, *bv->buffer());
311 } else if (name == "toc") {
312 InsetCommandParams icp("tableofcontents");
313 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
315 return new InsetTOC(icp);
317 } else if (name == "url") {
318 InsetCommandParams icp(name);
319 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
321 return new InsetUrl(icp);
323 } else if (name == "vspace") {
325 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
326 return new InsetVSpace(vspace);
330 case LFUN_SPACE_INSERT: {
331 string const name = to_utf8(cmd.argument());
332 if (name == "normal")
333 return new InsetSpace(InsetSpace::NORMAL);
334 else if (name == "protected")
335 return new InsetSpace(InsetSpace::PROTECTED);
336 else if (name == "thin")
337 return new InsetSpace(InsetSpace::THIN);
338 else if (name == "quad")
339 return new InsetSpace(InsetSpace::QUAD);
340 else if (name == "qquad")
341 return new InsetSpace(InsetSpace::QQUAD);
342 else if (name == "enspace")
343 return new InsetSpace(InsetSpace::ENSPACE);
344 else if (name == "enskip")
345 return new InsetSpace(InsetSpace::ENSKIP);
346 else if (name == "negthinspace")
347 return new InsetSpace(InsetSpace::NEGTHIN);
348 else if (name.empty())
349 lyxerr << "LyX function 'space' needs an argument." << endl;
351 lyxerr << "Wrong argument for LyX function 'space'." << endl;
359 } catch (support::ExceptionMessage const & message) {
360 if (message.type_ == support::ErrorException) {
361 Alert::error(message.title_, message.details_);
362 LyX::cref().emergencyCleanup();
364 } else if (message.type_ == support::WarningException) {
365 Alert::warning(message.title_, message.details_);
375 InsetBase * readInset(Lexer & lex, Buffer const & buf)
378 if (lex.getString() != "\\begin_inset") {
379 lyxerr << "Buffer::readInset: Consistency check failed."
383 auto_ptr<InsetBase> inset;
385 LyXTextClass tclass = buf.params().getLyXTextClass();
388 string tmptok = lex.getString();
390 // test the different insets
391 if (tmptok == "LatexCommand") {
393 string const cmdName = lex.getString();
394 lex.pushToken(cmdName);
396 InsetCommandParams inscmd(cmdName);
399 // This strange command allows LyX to recognize "natbib" style
400 // citations: citet, citep, Citet etc.
401 // FIXME: We already have partial support for \\fullcite and
402 // the various \\footcite commands. We should increase the
403 // file format number and read these commands here, too.
404 // Then we should use is_possible_cite_command() in
405 // src/frontends/controllers/frontend_helpers.cpp to test for valid cite
407 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
408 inset.reset(new InsetCitation(inscmd));
409 } else if (cmdName == "bibitem") {
410 inset.reset(new InsetBibitem(inscmd));
411 } else if (cmdName == "bibtex") {
412 inset.reset(new InsetBibtex(inscmd));
413 } else if (cmdName == "index") {
414 inset.reset(new InsetIndex(inscmd));
415 } else if (cmdName == "nomenclature") {
416 inset.reset(new InsetNomencl(inscmd));
417 } else if (cmdName == "include") {
418 inset.reset(new InsetInclude(inscmd));
419 } else if (cmdName == "label") {
420 inset.reset(new InsetLabel(inscmd));
421 } else if (cmdName == "url"
422 || cmdName == "htmlurl") {
423 inset.reset(new InsetUrl(inscmd));
424 } else if (cmdName == "ref"
425 || cmdName == "eqref"
426 || cmdName == "pageref"
428 || cmdName == "vpageref"
429 || cmdName == "prettyref") {
430 if (!inscmd["name"].empty()
431 || !inscmd["reference"].empty()) {
432 inset.reset(new InsetRef(inscmd, buf));
434 } else if (cmdName == "tableofcontents") {
435 inset.reset(new InsetTOC(inscmd));
436 } else if (cmdName == "listofalgorithms") {
437 inset.reset(new InsetFloatList("algorithm"));
438 } else if (cmdName == "listoffigures") {
439 inset.reset(new InsetFloatList("figure"));
440 } else if (cmdName == "listoftables") {
441 inset.reset(new InsetFloatList("table"));
442 } else if (cmdName == "printindex") {
443 inset.reset(new InsetPrintIndex(inscmd));
444 } else if (cmdName == "printnomenclature") {
445 inset.reset(new InsetPrintNomencl(inscmd));
447 lyxerr << "unknown CommandInset '" << cmdName
449 while (lex.isOK() && lex.getString() != "\\end_inset")
454 if (tmptok == "Quotes") {
455 inset.reset(new InsetQuotes);
456 } else if (tmptok == "External") {
457 inset.reset(new InsetExternal);
458 } else if (tmptok == "FormulaMacro") {
459 inset.reset(new MathMacroTemplate);
460 } else if (tmptok == "Formula") {
461 inset.reset(new InsetMathHull);
462 } else if (tmptok == "Graphics") {
463 inset.reset(new InsetGraphics);
464 } else if (tmptok == "Note") {
465 inset.reset(new InsetNote(buf.params(), tmptok));
466 } else if (tmptok == "Box") {
467 inset.reset(new InsetBox(buf.params(), tmptok));
468 } else if (tmptok == "CharStyle") {
470 string s = lex.getString();
471 CharStyles::iterator found_cs = tclass.charstyle(s);
472 if (found_cs != tclass.charstyles().end())
473 inset.reset(new InsetCharStyle(buf.params(), found_cs));
476 inset.reset(new InsetCharStyle(buf.params(), s));
478 } else if (tmptok == "Branch") {
479 inset.reset(new InsetBranch(buf.params(),
480 InsetBranchParams()));
481 } else if (tmptok == "Include") {
482 InsetCommandParams p("include");
483 inset.reset(new InsetInclude(p));
484 } else if (tmptok == "Environment") {
486 inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
487 } else if (tmptok == "ERT") {
488 inset.reset(new InsetERT(buf.params()));
489 } else if (tmptok == "InsetSpace") {
490 inset.reset(new InsetSpace);
491 } else if (tmptok == "Tabular") {
492 inset.reset(new InsetTabular(buf));
493 } else if (tmptok == "Text") {
494 inset.reset(new InsetText(buf.params()));
495 } else if (tmptok == "VSpace") {
496 inset.reset(new InsetVSpace);
497 } else if (tmptok == "Foot") {
498 inset.reset(new InsetFoot(buf.params()));
499 } else if (tmptok == "Marginal") {
500 inset.reset(new InsetMarginal(buf.params()));
501 } else if (tmptok == "OptArg") {
502 inset.reset(new InsetOptArg(buf.params()));
503 } else if (tmptok == "Float") {
505 string tmptok = lex.getString();
506 inset.reset(new InsetFloat(buf.params(), tmptok));
507 } else if (tmptok == "Wrap") {
509 string tmptok = lex.getString();
510 inset.reset(new InsetWrap(buf.params(), tmptok));
512 } else if (tmptok == "List") {
513 inset.reset(new InsetList);
514 } else if (tmptok == "Theorem") {
515 inset.reset(new InsetList);
517 } else if (tmptok == "Caption") {
518 inset.reset(new InsetCaption(buf.params()));
519 } else if (tmptok == "FloatList") {
520 inset.reset(new InsetFloatList);
522 lyxerr << "unknown Inset type '" << tmptok
524 while (lex.isOK() && lex.getString() != "\\end_inset")
529 inset->read(buf, lex);
534 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
535 MathMacroTemplate const * tmpl =
536 static_cast<MathMacroTemplate*>(inset.get());
537 MacroTable::globalMacros().insert
538 (tmpl->name(), tmpl->asMacroData());
540 << BOOST_CURRENT_FUNCTION
541 << ": creating local macro " << to_utf8(tmpl->name())
546 return inset.release();