3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
16 #include "buffer_funcs.h"
17 #include "bufferlist.h"
18 #include "bufferparams.h"
24 #include "errorlist.h"
27 #include "funcrequest.h"
29 #include "insetiterator.h"
32 #include "LaTeXFeatures.h"
33 #include "LyXAction.h"
41 #include "output_docbook.h"
42 #include "output_latex.h"
43 #include "output_linuxdoc.h"
44 #include "paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "pariterator.h"
53 #include "insets/insetbibitem.h"
54 #include "insets/insetbibtex.h"
55 #include "insets/insetinclude.h"
56 #include "insets/insettext.h"
58 #include "mathed/math_macrotemplate.h"
59 #include "mathed/math_macrotable.h"
60 #include "mathed/math_support.h"
62 #include "frontends/Alert.h"
64 #include "graphics/Previews.h"
66 #include "support/filetools.h"
67 #include "support/fs_extras.h"
68 #ifdef USE_COMPRESSION
69 # include "support/gzstream.h"
71 #include "support/lyxlib.h"
72 #include "support/os.h"
73 #include "support/path.h"
74 #include "support/textutils.h"
75 #include "support/convert.h"
77 #include <boost/bind.hpp>
78 #include <boost/filesystem/operations.hpp>
91 using lyx::support::AddName;
92 using lyx::support::bformat;
93 using lyx::support::ChangeExtension;
94 using lyx::support::cmd_ret;
95 using lyx::support::createBufferTmpDir;
96 using lyx::support::destroyDir;
97 using lyx::support::getFormatFromContents;
98 using lyx::support::IsDirWriteable;
99 using lyx::support::LibFileSearch;
100 using lyx::support::ltrim;
101 using lyx::support::MakeAbsPath;
102 using lyx::support::MakeDisplayPath;
103 using lyx::support::MakeLatexName;
104 using lyx::support::OnlyFilename;
105 using lyx::support::OnlyPath;
106 using lyx::support::Path;
107 using lyx::support::QuoteName;
108 using lyx::support::removeAutosaveFile;
109 using lyx::support::rename;
110 using lyx::support::RunCommand;
111 using lyx::support::split;
112 using lyx::support::subst;
113 using lyx::support::tempName;
114 using lyx::support::trim;
116 namespace os = lyx::support::os;
117 namespace fs = boost::filesystem;
121 using std::make_pair;
127 using std::ostringstream;
135 // all these externs should eventually be removed.
136 extern BufferList bufferlist;
140 int const LYX_FORMAT = 241;
145 typedef std::map<string, bool> DepClean;
150 Impl(Buffer & parent, string const & file, bool readonly);
152 limited_stack<Undo> undostack;
153 limited_stack<Undo> redostack;
159 /// need to regenerate .tex?
163 mutable bool lyx_clean;
165 /// is autosave needed?
166 mutable bool bak_clean;
168 /// is this a unnamed file (New...)?
174 /// name of the file the buffer is associated with.
177 /// The path to the document file.
180 boost::scoped_ptr<Messages> messages;
182 /** Set to true only when the file is fully loaded.
183 * Used to prevent the premature generation of previews
184 * and by the citation inset.
186 bool file_fully_loaded;
188 /// our LyXText that should be wrapped in an InsetText
196 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
197 : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
198 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
201 inset.setAutoBreakRows(true);
202 lyxvc.buffer(&parent);
203 temppath = createBufferTmpDir();
204 // FIXME: And now do something if temppath == string(), because we
205 // assume from now on that temppath points to a valid temp dir.
206 // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
210 Buffer::Buffer(string const & file, bool ronly)
211 : pimpl_(new Impl(*this, file, ronly))
213 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
219 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
220 // here the buffer should take care that it is
221 // saved properly, before it goes into the void.
225 if (!temppath().empty() && !destroyDir(temppath())) {
226 Alert::warning(_("Could not remove temporary directory"),
227 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
230 // Remove any previewed LaTeX snippets associated with this buffer.
231 lyx::graphics::Previews::get().removeLoader(*this);
235 LyXText & Buffer::text() const
237 return const_cast<LyXText &>(pimpl_->inset.text_);
241 InsetBase & Buffer::inset() const
243 return const_cast<InsetText &>(pimpl_->inset);
247 limited_stack<Undo> & Buffer::undostack()
249 return pimpl_->undostack;
253 limited_stack<Undo> const & Buffer::undostack() const
255 return pimpl_->undostack;
259 limited_stack<Undo> & Buffer::redostack()
261 return pimpl_->redostack;
265 limited_stack<Undo> const & Buffer::redostack() const
267 return pimpl_->redostack;
271 BufferParams & Buffer::params()
273 return pimpl_->params;
277 BufferParams const & Buffer::params() const
279 return pimpl_->params;
283 ParagraphList & Buffer::paragraphs()
285 return text().paragraphs();
289 ParagraphList const & Buffer::paragraphs() const
291 return text().paragraphs();
295 LyXVC & Buffer::lyxvc()
297 return pimpl_->lyxvc;
301 LyXVC const & Buffer::lyxvc() const
303 return pimpl_->lyxvc;
307 string const & Buffer::temppath() const
309 return pimpl_->temppath;
313 TexRow & Buffer::texrow()
315 return pimpl_->texrow;
319 TexRow const & Buffer::texrow() const
321 return pimpl_->texrow;
325 string const Buffer::getLatexName(bool const no_path) const
327 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
328 return no_path ? OnlyFilename(name) : name;
332 pair<Buffer::LogType, string> const Buffer::getLogName() const
334 string const filename = getLatexName(false);
336 if (filename.empty())
337 return make_pair(Buffer::latexlog, string());
339 string const path = temppath();
341 string const fname = AddName(path,
342 OnlyFilename(ChangeExtension(filename,
345 AddName(path, OnlyFilename(
346 ChangeExtension(filename,
347 formats.extension("literate") + ".out")));
349 // If no Latex log or Build log is newer, show Build log
351 if (fs::exists(bname) &&
352 (!fs::exists(fname) || fs::last_write_time(fname) < fs::last_write_time(bname))) {
353 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
354 return make_pair(Buffer::buildlog, bname);
356 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
357 return make_pair(Buffer::latexlog, fname);
361 void Buffer::setReadonly(bool const flag)
363 if (pimpl_->read_only != flag) {
364 pimpl_->read_only = flag;
370 void Buffer::setFileName(string const & newfile)
372 pimpl_->filename = MakeAbsPath(newfile);
373 pimpl_->filepath = OnlyPath(pimpl_->filename);
374 setReadonly(fs::is_readonly(pimpl_->filename));
379 // We'll remove this later. (Lgb)
382 void unknownClass(string const & unknown)
384 Alert::warning(_("Unknown document class"),
385 bformat(_("Using the default document class, because the "
386 "class %1$s is unknown."), unknown));
392 int Buffer::readHeader(LyXLex & lex)
394 int unknown_tokens = 0;
396 int begin_header_line = -1;
400 string const token = lex.getString();
405 if (token == "\\end_header")
409 if (token == "\\begin_header") {
410 begin_header_line = line;
414 lyxerr[Debug::PARSER] << "Handling header token: `"
415 << token << '\'' << endl;
417 string unknown = params().readToken(lex, token);
418 if (!unknown.empty()) {
419 if (unknown[0] != '\\' && token == "\\textclass") {
420 unknownClass(unknown);
423 string const s = bformat(_("Unknown token: "
427 error(ErrorItem(_("Header error"), s,
432 if (begin_header_line) {
433 string const s = _("\\begin_header is missing");
434 error(ErrorItem(_("Header error"), s, -1, 0, 0));
436 return unknown_tokens;
441 // changed to be public and have one parameter
442 // Returns false if "\end_document" is not read (Asger)
443 bool Buffer::readDocument(LyXLex & lex)
446 string const token = lex.getString();
447 if (token != "\\begin_document") {
448 string const s = _("\\begin_document is missing");
449 error(ErrorItem(_("Header error"), s, -1, 0, 0));
452 if (paragraphs().empty()) {
454 if (!params().getLyXTextClass().load()) {
455 string theclass = params().getLyXTextClass().name();
456 Alert::error(_("Can't load document class"), bformat(
457 "Using the default document class, because the "
458 " class %1$s could not be loaded.", theclass));
459 params().textclass = 0;
462 // We don't want to adopt the parameters from the
463 // document we insert, so read them into a temporary buffer
464 // and then discard it
466 Buffer tmpbuf("", false);
467 tmpbuf.readHeader(lex);
470 return text().read(*this, lex);
474 // needed to insert the selection
475 void Buffer::insertStringAsLines(ParagraphList & pars,
476 pit_type & pit, pos_type & pos,
477 LyXFont const & fn, string const & str, bool autobreakrows)
481 pars[pit].checkInsertChar(font);
482 // insert the string, don't insert doublespace
483 bool space_inserted = true;
484 for (string::const_iterator cit = str.begin();
485 cit != str.end(); ++cit) {
486 Paragraph & par = pars[pit];
488 if (autobreakrows && (!par.empty() || par.allowEmpty())) {
489 breakParagraph(params(), pars, pit, pos,
490 par.layout()->isEnvironment());
493 space_inserted = true;
497 // do not insert consecutive spaces if !free_spacing
498 } else if ((*cit == ' ' || *cit == '\t') &&
499 space_inserted && !par.isFreeSpacing()) {
501 } else if (*cit == '\t') {
502 if (!par.isFreeSpacing()) {
503 // tabs are like spaces here
504 par.insertChar(pos, ' ', font);
506 space_inserted = true;
508 const pos_type n = 8 - pos % 8;
509 for (pos_type i = 0; i < n; ++i) {
510 par.insertChar(pos, ' ', font);
513 space_inserted = true;
515 } else if (!IsPrintable(*cit)) {
516 // Ignore unprintables
519 // just insert the character
520 par.insertChar(pos, *cit, font);
522 space_inserted = (*cit == ' ');
529 bool Buffer::readFile(string const & filename)
531 // Check if the file is compressed.
532 string const format = getFormatFromContents(filename);
533 if (format == "gzip" || format == "zip" || format == "compress") {
534 params().compressed = true;
537 // remove dummy empty par
538 paragraphs().clear();
539 bool ret = readFile(filename, paragraphs().size());
541 // After we have read a file, we must ensure that the buffer
542 // language is set and used in the gui.
543 // If you know of a better place to put this, please tell me. (Lgb)
544 updateDocLang(params().language);
550 bool Buffer::readFile(string const & filename, pit_type const pit)
553 lex.setFile(filename);
554 return readFile(lex, filename, pit);
558 bool Buffer::fully_loaded() const
560 return pimpl_->file_fully_loaded;
564 void Buffer::fully_loaded(bool const value)
566 pimpl_->file_fully_loaded = value;
570 bool Buffer::readFile(LyXLex & lex, string const & filename, pit_type const pit)
572 BOOST_ASSERT(!filename.empty());
575 Alert::error(_("Document could not be read"),
576 bformat(_("%1$s could not be read."), filename));
581 string const token(lex.getString());
584 Alert::error(_("Document could not be read"),
585 bformat(_("%1$s could not be read."), filename));
589 // the first token _must_ be...
590 if (token != "\\lyxformat") {
591 lyxerr << "Token: " << token << endl;
593 Alert::error(_("Document format failure"),
594 bformat(_("%1$s is not a LyX document."),
600 string tmp_format = lex.getString();
601 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
602 // if present remove ".," from string.
603 string::size_type dot = tmp_format.find_first_of(".,");
604 //lyxerr << " dot found at " << dot << endl;
605 if (dot != string::npos)
606 tmp_format.erase(dot, 1);
607 int const file_format = convert<int>(tmp_format);
608 //lyxerr << "format: " << file_format << endl;
610 if (file_format != LYX_FORMAT) {
611 string const tmpfile = tempName();
612 if (tmpfile.empty()) {
613 Alert::error(_("Conversion failed"),
614 bformat(_("%1$s is from an earlier"
615 " version of LyX, but a temporary"
616 " file for converting it could"
622 "python " + LibFileSearch("lyx2lyx", "lyx2lyx");
623 if (command.empty()) {
624 Alert::error(_("Conversion script not found"),
625 bformat(_("%1$s is from an earlier"
626 " version of LyX, but the"
627 " conversion script lyx2lyx"
628 " could not be found."),
633 + convert<string>(LYX_FORMAT)
634 + " -o " + tmpfile + ' '
635 + QuoteName(filename);
636 lyxerr[Debug::INFO] << "Running '"
639 cmd_ret const ret = RunCommand(command);
640 if (ret.first != 0) {
641 Alert::error(_("Conversion script failed"),
642 bformat(_("%1$s is from an earlier version"
643 " of LyX, but the lyx2lyx script"
644 " failed to convert it."),
648 bool const ret = readFile(tmpfile, pit);
649 // Do stuff with tmpfile name and buffer name here.
655 if (readDocument(lex)) {
656 Alert::error(_("Document format failure"),
657 bformat(_("%1$s ended unexpectedly, which means"
658 " that it is probably corrupted."),
662 //lyxerr << "removing " << MacroTable::localMacros().size()
663 // << " temporary macro entries" << endl;
664 //MacroTable::localMacros().clear();
665 params().setPaperStuff();
667 pimpl_->file_fully_loaded = true;
672 // Should probably be moved to somewhere else: BufferView? LyXView?
673 bool Buffer::save() const
675 // We don't need autosaves in the immediate future. (Asger)
676 resetAutosaveTimers();
680 if (lyxrc.make_backup) {
681 s = fileName() + '~';
682 if (!lyxrc.backupdir_path.empty())
683 s = AddName(lyxrc.backupdir_path,
684 subst(os::internal_path(s),'/','!'));
686 // It might very well be that this variant is just
687 // good enough. (Lgb)
688 // But to use this we need fs::copy_file to actually do a copy,
689 // even when the target file exists. (Lgb)
690 if (fs::exists(fileName())) {
692 fs::copy_file(fileName(), s, false);
694 //catch (fs::filesystem_error const & fe) {
695 //lyxerr << "LyX was not able to make backup copy. Beware.\n"
696 // << fe.what() << endl;
701 if (writeFile(fileName())) {
703 removeAutosaveFile(fileName());
705 // Saving failed, so backup is not backup
706 if (lyxrc.make_backup)
707 rename(s, fileName());
714 bool Buffer::writeFile(string const & fname) const
716 if (pimpl_->read_only && fname == fileName())
721 if (params().compressed) {
722 #ifdef USE_COMPRESSION
723 gz::ogzstream ofs(fname.c_str(), ios::out|ios::trunc);
727 retval = do_writeFile(ofs);
732 ofstream ofs(fname.c_str(), ios::out|ios::trunc);
736 retval = do_writeFile(ofs);
743 bool Buffer::do_writeFile(ostream & ofs) const
746 // Use the standard "C" locale for file output.
747 ofs.imbue(std::locale::classic());
750 // The top of the file should not be written by params().
752 // write out a comment in the top of the file
753 ofs << "#LyX " << lyx_version
754 << " created this file. For more info see http://www.lyx.org/\n"
755 << "\\lyxformat " << LYX_FORMAT << "\n"
756 << "\\begin_document\n";
758 // now write out the buffer parameters.
759 ofs << "\\begin_header\n";
760 params().writeFile(ofs);
761 ofs << "\\end_header\n";
764 ofs << "\n\\begin_body\n";
765 text().write(*this, ofs);
766 ofs << "\n\\end_body\n";
768 // Write marker that shows file is complete
769 ofs << "\\end_document" << endl;
771 // Shouldn't really be needed....
774 // how to check if close went ok?
775 // Following is an attempt... (BE 20001011)
777 // good() returns false if any error occured, including some
779 // bad() returns true if something bad happened in the buffer,
780 // which should include file system full errors.
785 lyxerr << "File was not closed properly." << endl;
792 void Buffer::makeLaTeXFile(string const & fname,
793 string const & original_path,
794 OutputParams const & runparams,
795 bool output_preamble, bool output_body)
797 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
800 if (!openFileWrite(ofs, fname))
803 makeLaTeXFile(ofs, original_path,
804 runparams, output_preamble, output_body);
808 lyxerr << "File '" << fname << "' was not closed properly." << endl;
812 void Buffer::makeLaTeXFile(ostream & os,
813 string const & original_path,
814 OutputParams const & runparams_in,
815 bool const output_preamble, bool const output_body)
817 OutputParams runparams = runparams_in;
819 // validate the buffer.
820 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
821 LaTeXFeatures features(*this, params(), runparams.nice);
823 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
827 // The starting paragraph of the coming rows is the
828 // first paragraph of the document. (Asger)
829 texrow().start(paragraphs().begin()->id(), 0);
831 if (output_preamble && runparams.nice) {
832 os << "%% LyX " << lyx_version << " created this file. "
833 "For more info, see http://www.lyx.org/.\n"
834 "%% Do not edit unless you really know what "
839 lyxerr[Debug::INFO] << "lyx header finished" << endl;
840 // There are a few differences between nice LaTeX and usual files:
841 // usual is \batchmode and has a
842 // special input@path to allow the including of figures
843 // with either \input or \includegraphics (what figinsets do).
844 // input@path is set when the actual parameter
845 // original_path is set. This is done for usual tex-file, but not
846 // for nice-latex-file. (Matthias 250696)
847 // Note that input@path is only needed for something the user does
848 // in the preamble, included .tex files or ERT, files included by
849 // LyX work without it.
850 if (output_preamble) {
851 if (!runparams.nice) {
852 // code for usual, NOT nice-latex-file
853 os << "\\batchmode\n"; // changed
857 if (!original_path.empty()) {
858 string inputpath = os::external_path(original_path);
859 subst(inputpath, "~", "\\string~");
860 os << "\\makeatletter\n"
861 << "\\def\\input@path{{"
862 << inputpath << "/}}\n"
863 << "\\makeatother\n";
869 // Write the preamble
870 runparams.use_babel = params().writeLaTeX(os, features, texrow());
876 os << "\\begin{document}\n";
879 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
881 if (!lyxrc.language_auto_begin) {
882 os << subst(lyxrc.language_command_begin, "$$lang",
883 params().language->babel())
888 // if we are doing a real file with body, even if this is the
889 // child of some other buffer, let's cut the link here.
890 // This happens for example if only a child document is printed.
891 string save_parentname;
892 if (output_preamble) {
893 save_parentname = params().parentname;
894 params().parentname.erase();
898 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
900 // Restore the parenthood if needed
902 params().parentname = save_parentname;
904 // add this just in case after all the paragraphs
908 if (!lyxrc.language_auto_end) {
909 os << subst(lyxrc.language_command_end, "$$lang",
910 params().language->babel())
915 if (output_preamble) {
916 os << "\\end{document}\n";
919 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
921 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
925 // Just to be sure. (Asger)
928 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
929 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
934 bool Buffer::isLatex() const
936 return params().getLyXTextClass().outputType() == LATEX;
940 bool Buffer::isLinuxDoc() const
942 return params().getLyXTextClass().outputType() == LINUXDOC;
946 bool Buffer::isLiterate() const
948 return params().getLyXTextClass().outputType() == LITERATE;
952 bool Buffer::isDocBook() const
954 return params().getLyXTextClass().outputType() == DOCBOOK;
958 bool Buffer::isSGML() const
960 LyXTextClass const & tclass = params().getLyXTextClass();
962 return tclass.outputType() == LINUXDOC ||
963 tclass.outputType() == DOCBOOK;
967 void Buffer::makeLinuxDocFile(string const & fname,
968 OutputParams const & runparams,
969 bool const body_only)
972 if (!openFileWrite(ofs, fname))
975 LaTeXFeatures features(*this, params(), runparams.nice);
980 LyXTextClass const & tclass = params().getLyXTextClass();
982 string const & top_element = tclass.latexname();
985 ofs << tclass.class_header();
987 string preamble = params().preamble;
988 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
990 preamble += features.getIncludedFiles(name);
991 preamble += features.getLyXSGMLEntities();
993 if (!preamble.empty()) {
994 ofs << " [ " << preamble << " ]";
998 if (params().options.empty())
999 sgml::openTag(ofs, top_element);
1001 string top = top_element;
1003 top += params().options;
1004 sgml::openTag(ofs, top);
1008 ofs << "<!-- LyX " << lyx_version
1009 << " created this file. For more info see http://www.lyx.org/"
1012 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1016 sgml::closeTag(ofs, top_element);
1021 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1025 void Buffer::makeDocBookFile(string const & fname,
1026 OutputParams const & runparams,
1027 bool const only_body)
1030 if (!openFileWrite(ofs, fname))
1033 LaTeXFeatures features(*this, params(), runparams.nice);
1038 LyXTextClass const & tclass = params().getLyXTextClass();
1039 string const & top_element = tclass.latexname();
1042 if (runparams.flavor == OutputParams::XML)
1043 ofs << "<?xml version=\"1.0\" encoding=\""
1044 << params().language->encoding()->Name() << "\"?>\n";
1046 ofs << "<!DOCTYPE " << top_element << " ";
1048 if (! tclass.class_header().empty()) ofs << tclass.class_header();
1049 else if (runparams.flavor == OutputParams::XML)
1050 ofs << "PUBLIC \"-//OASIS//DTD DocBook XML//EN\" "
1051 << "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\"";
1053 ofs << " PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"";
1055 string preamble = params().preamble;
1056 if (runparams.flavor != OutputParams::XML ) {
1057 preamble += "<!ENTITY % output.print.png \"IGNORE\">\n";
1058 preamble += "<!ENTITY % output.print.pdf \"IGNORE\">\n";
1059 preamble += "<!ENTITY % output.print.eps \"IGNORE\">\n";
1060 preamble += "<!ENTITY % output.print.bmp \"IGNORE\">\n";
1063 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1065 preamble += features.getIncludedFiles(name);
1066 preamble += features.getLyXSGMLEntities();
1068 if (!preamble.empty()) {
1069 ofs << "\n [ " << preamble << " ]";
1074 string top = top_element;
1076 if (runparams.flavor == OutputParams::XML)
1077 top += params().language->code();
1079 top += params().language->code().substr(0,2);
1082 if (!params().options.empty()) {
1084 top += params().options;
1087 ofs << "<!-- " << ((runparams.flavor == OutputParams::XML)? "XML" : "SGML")
1088 << " file was created by LyX " << lyx_version
1089 << "\n See http://www.lyx.org/ for more information -->\n";
1091 params().getLyXTextClass().counters().reset();
1093 sgml::openTag(ofs, top);
1095 docbookParagraphs(paragraphs(), *this, ofs, runparams);
1096 sgml::closeTag(ofs, top_element);
1100 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1104 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1105 // Other flags: -wall -v0 -x
1106 int Buffer::runChktex()
1110 // get LaTeX-Filename
1111 string const name = getLatexName();
1112 string const path = temppath();
1113 string const org_path = filePath();
1115 Path p(path); // path to LaTeX file
1116 message(_("Running chktex..."));
1118 // Generate the LaTeX file if neccessary
1119 OutputParams runparams;
1120 runparams.flavor = OutputParams::LATEX;
1121 runparams.nice = false;
1122 makeLaTeXFile(name, org_path, runparams);
1125 Chktex chktex(lyxrc.chktex_command, name, filePath());
1126 int const res = chktex.run(terr); // run chktex
1129 Alert::error(_("chktex failure"),
1130 _("Could not run chktex successfully."));
1131 } else if (res > 0) {
1132 // Insert all errors as errors boxes
1133 bufferErrors(*this, terr);
1142 void Buffer::validate(LaTeXFeatures & features) const
1144 LyXTextClass const & tclass = params().getLyXTextClass();
1146 if (features.isAvailable("dvipost") && params().tracking_changes
1147 && params().output_changes) {
1148 features.require("dvipost");
1149 features.require("color");
1152 // AMS Style is at document level
1153 if (params().use_amsmath == BufferParams::AMS_ON
1154 || tclass.provides(LyXTextClass::amsmath))
1155 features.require("amsmath");
1157 for_each(paragraphs().begin(), paragraphs().end(),
1158 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1160 // the bullet shapes are buffer level not paragraph level
1161 // so they are tested here
1162 for (int i = 0; i < 4; ++i) {
1163 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1164 int const font = params().user_defined_bullet(i).getFont();
1166 int const c = params()
1167 .user_defined_bullet(i)
1174 features.require("latexsym");
1176 } else if (font == 1) {
1177 features.require("amssymb");
1178 } else if ((font >= 2 && font <= 5)) {
1179 features.require("pifont");
1184 if (lyxerr.debugging(Debug::LATEX)) {
1185 features.showStruct();
1190 void Buffer::getLabelList(vector<string> & list) const
1192 /// if this is a child document and the parent is already loaded
1193 /// Use the parent's list instead [ale990407]
1194 Buffer const * tmp = getMasterBuffer();
1196 lyxerr << "getMasterBuffer() failed!" << endl;
1200 tmp->getLabelList(list);
1204 for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
1205 it.nextInset()->getLabelList(*this, list);
1209 // This is also a buffer property (ale)
1210 void Buffer::fillWithBibKeys(vector<pair<string, string> > & keys)
1213 /// if this is a child document and the parent is already loaded
1214 /// use the parent's list instead [ale990412]
1215 Buffer const * tmp = getMasterBuffer();
1218 tmp->fillWithBibKeys(keys);
1222 for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1223 if (it->lyxCode() == InsetBase::BIBTEX_CODE) {
1224 InsetBibtex const & inset =
1225 dynamic_cast<InsetBibtex const &>(*it);
1226 inset.fillWithBibKeys(*this, keys);
1227 } else if (it->lyxCode() == InsetBase::INCLUDE_CODE) {
1228 InsetInclude const & inset =
1229 dynamic_cast<InsetInclude const &>(*it);
1230 inset.fillWithBibKeys(*this, keys);
1231 } else if (it->lyxCode() == InsetBase::BIBITEM_CODE) {
1232 InsetBibitem const & inset =
1233 dynamic_cast<InsetBibitem const &>(*it);
1234 string const key = inset.getContents();
1235 string const opt = inset.getOptions();
1236 string const ref; // = pit->asString(this, false);
1237 string const info = opt + "TheBibliographyRef" + ref;
1238 keys.push_back(pair<string, string>(key, info));
1244 bool Buffer::isDepClean(string const & name) const
1246 DepClean::const_iterator const it = pimpl_->dep_clean.find(name);
1247 if (it == pimpl_->dep_clean.end())
1253 void Buffer::markDepClean(string const & name)
1255 pimpl_->dep_clean[name] = true;
1259 bool Buffer::dispatch(string const & command, bool * result)
1261 return dispatch(lyxaction.lookupFunc(command), result);
1265 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1267 bool dispatched = true;
1269 switch (func.action) {
1271 bool const tmp = Exporter::Export(this, func.argument, false);
1284 void Buffer::changeLanguage(Language const * from, Language const * to)
1289 lyxerr << "Changing Language!" << endl;
1291 // Take care of l10n/i18n
1294 for_each(par_iterator_begin(),
1296 bind(&Paragraph::changeLanguage, _1, params(), from, to));
1300 void Buffer::updateDocLang(Language const * nlang)
1302 BOOST_ASSERT(nlang);
1304 pimpl_->messages.reset(new Messages(nlang->code()));
1308 bool Buffer::isMultiLingual() const
1310 ParConstIterator end = par_iterator_end();
1311 for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1312 if (it->isMultiLingual(params()))
1319 ParIterator Buffer::getParFromID(int const id) const
1321 ParConstIterator it = par_iterator_begin();
1322 ParConstIterator const end = par_iterator_end();
1325 // John says this is called with id == -1 from undo
1326 lyxerr << "getParFromID(), id: " << id << endl;
1330 for (; it != end; ++it)
1338 bool Buffer::hasParWithID(int const id) const
1340 ParConstIterator const it = getParFromID(id);
1341 return it != par_iterator_end();
1345 ParIterator Buffer::par_iterator_begin()
1347 return ::par_iterator_begin(inset());
1351 ParIterator Buffer::par_iterator_end()
1353 return ::par_iterator_end(inset());
1357 ParConstIterator Buffer::par_iterator_begin() const
1359 return ::par_const_iterator_begin(inset());
1363 ParConstIterator Buffer::par_iterator_end() const
1365 return ::par_const_iterator_end(inset());
1369 Language const * Buffer::getLanguage() const
1371 return params().language;
1375 string const Buffer::B_(string const & l10n) const
1377 if (pimpl_->messages.get()) {
1378 return pimpl_->messages->get(l10n);
1385 bool Buffer::isClean() const
1387 return pimpl_->lyx_clean;
1391 bool Buffer::isBakClean() const
1393 return pimpl_->bak_clean;
1397 void Buffer::markClean() const
1399 if (!pimpl_->lyx_clean) {
1400 pimpl_->lyx_clean = true;
1403 // if the .lyx file has been saved, we don't need an
1405 pimpl_->bak_clean = true;
1409 void Buffer::markBakClean()
1411 pimpl_->bak_clean = true;
1415 void Buffer::setUnnamed(bool flag)
1417 pimpl_->unnamed = flag;
1421 bool Buffer::isUnnamed() const
1423 return pimpl_->unnamed;
1427 #ifdef WITH_WARNINGS
1428 #warning this function should be moved to buffer_pimpl.C
1430 void Buffer::markDirty()
1432 if (pimpl_->lyx_clean) {
1433 pimpl_->lyx_clean = false;
1436 pimpl_->bak_clean = false;
1438 DepClean::iterator it = pimpl_->dep_clean.begin();
1439 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1441 for (; it != end; ++it) {
1447 string const & Buffer::fileName() const
1449 return pimpl_->filename;
1453 string const & Buffer::filePath() const
1455 return pimpl_->filepath;
1459 bool Buffer::isReadonly() const
1461 return pimpl_->read_only;
1465 void Buffer::setParentName(string const & name)
1467 params().parentname = name;
1471 Buffer const * Buffer::getMasterBuffer() const
1473 if (!params().parentname.empty()
1474 && bufferlist.exists(params().parentname)) {
1475 Buffer const * buf = bufferlist.getBuffer(params().parentname);
1477 return buf->getMasterBuffer();
1484 MacroData const & Buffer::getMacro(std::string const & name) const
1486 return pimpl_->macros.get(name);
1490 bool Buffer::hasMacro(string const & name) const
1492 return pimpl_->macros.has(name);
1496 void Buffer::insertMacro(string const & name, MacroData const & data)
1498 MacroTable::globalMacros().insert(name, data);
1499 pimpl_->macros.insert(name, data);
1503 void Buffer::buildMacros()
1505 // Start with global table.
1506 pimpl_->macros = MacroTable::globalMacros();
1509 ParagraphList & pars = text().paragraphs();
1510 for (size_t i = 0, n = pars.size(); i != n; ++i) {
1511 //lyxerr << "searching main par " << i
1512 // << " for macro definitions" << std::endl;
1513 InsetList::iterator it = pars[i].insetlist.begin();
1514 InsetList::iterator end = pars[i].insetlist.end();
1515 for ( ; it != end; ++it) {
1516 //lyxerr << "found inset code " << it->inset->lyxCode() << std::endl;
1517 if (it->inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
1518 MathMacroTemplate & mac
1519 = static_cast<MathMacroTemplate &>(*it->inset);
1520 insertMacro(mac.name(), mac.asMacroData());