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"
23 #include "errorlist.h"
26 #include "funcrequest.h"
28 #include "insetiterator.h"
31 #include "LaTeXFeatures.h"
32 #include "LyXAction.h"
40 #include "output_docbook.h"
41 #include "output_latex.h"
42 #include "output_linuxdoc.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "pariterator.h"
52 #include "insets/insetbibitem.h"
53 #include "insets/insetbibtex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insettext.h"
57 #include "mathed/math_macrotemplate.h"
58 #include "mathed/math_macrotable.h"
59 #include "mathed/math_support.h"
61 #include "frontends/Alert.h"
63 #include "graphics/Previews.h"
65 #include "support/FileInfo.h"
66 #include "support/filetools.h"
67 #include "support/gzstream.h"
68 #include "support/lyxlib.h"
69 #include "support/os.h"
70 #include "support/path.h"
71 #include "support/textutils.h"
72 #include "support/tostr.h"
73 #include "support/std_sstream.h"
75 #include <boost/bind.hpp>
85 using lyx::support::AddName;
86 using lyx::support::atoi;
87 using lyx::support::bformat;
88 using lyx::support::ChangeExtension;
89 using lyx::support::cmd_ret;
90 using lyx::support::createBufferTmpDir;
91 using lyx::support::destroyDir;
92 using lyx::support::FileInfo;
93 using lyx::support::FileInfo;
94 using lyx::support::getExtFromContents;
95 using lyx::support::IsDirWriteable;
96 using lyx::support::IsFileWriteable;
97 using lyx::support::LibFileSearch;
98 using lyx::support::ltrim;
99 using lyx::support::MakeAbsPath;
100 using lyx::support::MakeDisplayPath;
101 using lyx::support::MakeLatexName;
102 using lyx::support::OnlyFilename;
103 using lyx::support::OnlyPath;
104 using lyx::support::Path;
105 using lyx::support::QuoteName;
106 using lyx::support::removeAutosaveFile;
107 using lyx::support::rename;
108 using lyx::support::RunCommand;
109 using lyx::support::split;
110 using lyx::support::strToInt;
111 using lyx::support::subst;
112 using lyx::support::tempName;
113 using lyx::support::trim;
115 namespace os = lyx::support::os;
119 using std::make_pair;
125 using std::ostringstream;
133 // all these externs should eventually be removed.
134 extern BufferList bufferlist;
138 const int LYX_FORMAT = 232;
143 typedef std::map<string, bool> DepClean;
147 Impl(Buffer & parent, string const & file, bool readonly);
149 limited_stack<Undo> undostack;
150 limited_stack<Undo> redostack;
156 /// need to regenerate .tex?
160 mutable bool lyx_clean;
162 /// is autosave needed?
163 mutable bool bak_clean;
165 /// is this a unnamed file (New...)?
171 /// name of the file the buffer is associated with.
174 /// The path to the document file.
177 boost::scoped_ptr<Messages> messages;
179 /** Set to true only when the file is fully loaded.
180 * Used to prevent the premature generation of previews
181 * and by the citation inset.
183 bool file_fully_loaded;
185 /// our LyXText that should be wrapped in an InsetText
193 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
194 : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
195 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
198 lyxvc.buffer(&parent);
199 temppath = createBufferTmpDir();
200 // FIXME: And now do something if temppath == string(), because we
201 // assume from now on that temppath points to a valid temp dir.
202 // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
206 Buffer::Buffer(string const & file, bool ronly)
207 : pimpl_(new Impl(*this, file, ronly))
209 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
215 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
216 // here the buffer should take care that it is
217 // saved properly, before it goes into the void.
221 if (!temppath().empty() && destroyDir(temppath()) != 0) {
222 Alert::warning(_("Could not remove temporary directory"),
223 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
226 // Remove any previewed LaTeX snippets associated with this buffer.
227 lyx::graphics::Previews::get().removeLoader(*this);
231 LyXText & Buffer::text() const
233 return const_cast<LyXText &>(pimpl_->inset.text_);
237 InsetBase & Buffer::inset() const
239 return const_cast<InsetText &>(pimpl_->inset);
243 limited_stack<Undo> & Buffer::undostack()
245 return pimpl_->undostack;
249 limited_stack<Undo> const & Buffer::undostack() const
251 return pimpl_->undostack;
255 limited_stack<Undo> & Buffer::redostack()
257 return pimpl_->redostack;
261 limited_stack<Undo> const & Buffer::redostack() const
263 return pimpl_->redostack;
267 BufferParams & Buffer::params()
269 return pimpl_->params;
273 BufferParams const & Buffer::params() const
275 return pimpl_->params;
279 ParagraphList & Buffer::paragraphs()
281 return text().paragraphs();
285 ParagraphList const & Buffer::paragraphs() const
287 return text().paragraphs();
291 LyXVC & Buffer::lyxvc()
293 return pimpl_->lyxvc;
297 LyXVC const & Buffer::lyxvc() const
299 return pimpl_->lyxvc;
303 string const & Buffer::temppath() const
305 return pimpl_->temppath;
309 TexRow & Buffer::texrow()
311 return pimpl_->texrow;
315 TexRow const & Buffer::texrow() const
317 return pimpl_->texrow;
321 string const Buffer::getLatexName(bool no_path) const
323 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
324 return no_path ? OnlyFilename(name) : name;
328 pair<Buffer::LogType, string> const Buffer::getLogName() const
330 string const filename = getLatexName(false);
332 if (filename.empty())
333 return make_pair(Buffer::latexlog, string());
335 string const path = temppath();
337 string const fname = AddName(path,
338 OnlyFilename(ChangeExtension(filename,
341 AddName(path, OnlyFilename(
342 ChangeExtension(filename,
343 formats.extension("literate") + ".out")));
345 // If no Latex log or Build log is newer, show Build log
347 FileInfo const f_fi(fname);
348 FileInfo const b_fi(bname);
351 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
352 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
353 return make_pair(Buffer::buildlog, bname);
355 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
356 return make_pair(Buffer::latexlog, fname);
360 void Buffer::setReadonly(bool flag)
362 if (pimpl_->read_only != flag) {
363 pimpl_->read_only = flag;
369 void Buffer::setFileName(string const & newfile)
371 pimpl_->filename = MakeAbsPath(newfile);
372 pimpl_->filepath = OnlyPath(pimpl_->filename);
373 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
378 // We'll remove this later. (Lgb)
381 void unknownClass(string const & unknown)
383 Alert::warning(_("Unknown document class"),
384 bformat(_("Using the default document class, because the "
385 "class %1$s is unknown."), unknown));
391 int Buffer::readHeader(LyXLex & lex)
393 int unknown_tokens = 0;
397 string const token = lex.getString();
402 if (token == "\\end_header")
405 lyxerr[Debug::PARSER] << "Handling header token: `"
406 << token << '\'' << endl;
409 string unknown = params().readToken(lex, token);
410 if (!unknown.empty()) {
411 if (unknown[0] != '\\') {
412 unknownClass(unknown);
415 string const s = bformat(_("Unknown token: "
419 error(ErrorItem(_("Header error"), s,
424 return unknown_tokens;
429 // changed to be public and have one parameter
430 // Returns false if "\end_document" is not read (Asger)
431 bool Buffer::readBody(LyXLex & lex)
433 if (paragraphs().empty()) {
435 if (!params().getLyXTextClass().load()) {
436 string theclass = params().getLyXTextClass().name();
437 Alert::error(_("Can't load document class"), bformat(
438 "Using the default document class, because the "
439 " class %1$s could not be loaded.", theclass));
440 params().textclass = 0;
443 // We don't want to adopt the parameters from the
444 // document we insert, so read them into a temporary buffer
445 // and then discard it
447 Buffer tmpbuf("", false);
448 tmpbuf.readHeader(lex);
451 return text().read(*this, lex);
455 // needed to insert the selection
456 void Buffer::insertStringAsLines(ParagraphList & pars,
457 par_type & par, pos_type & pos,
458 LyXFont const & fn, string const & str)
460 LyXLayout_ptr const & layout = pars[par].layout();
464 pars[par].checkInsertChar(font);
465 // insert the string, don't insert doublespace
466 bool space_inserted = true;
467 bool autobreakrows = pars[par].autoBreakRows();
468 for (string::const_iterator cit = str.begin();
469 cit != str.end(); ++cit) {
471 if (autobreakrows && (!pars[par].empty() || pars[par].allowEmpty())) {
472 breakParagraph(params(), paragraphs(), par, pos,
473 layout->isEnvironment());
476 space_inserted = true;
480 // do not insert consecutive spaces if !free_spacing
481 } else if ((*cit == ' ' || *cit == '\t') &&
482 space_inserted && !pars[par].isFreeSpacing()) {
484 } else if (*cit == '\t') {
485 if (!pars[par].isFreeSpacing()) {
486 // tabs are like spaces here
487 pars[par].insertChar(pos, ' ', font);
489 space_inserted = true;
491 const pos_type n = 8 - pos % 8;
492 for (pos_type i = 0; i < n; ++i) {
493 pars[par].insertChar(pos, ' ', font);
496 space_inserted = true;
498 } else if (!IsPrintable(*cit)) {
499 // Ignore unprintables
502 // just insert the character
503 pars[par].insertChar(pos, *cit, font);
505 space_inserted = (*cit == ' ');
512 bool Buffer::readFile(string const & filename)
514 // Check if the file is compressed.
515 string const format = getExtFromContents(filename);
516 if (format == "gzip" || format == "zip" || format == "compress") {
517 params().compressed = true;
520 // remove dummy empty par
521 paragraphs().clear();
522 bool ret = readFile(filename, paragraphs().size());
524 // After we have read a file, we must ensure that the buffer
525 // language is set and used in the gui.
526 // If you know of a better place to put this, please tell me. (Lgb)
527 updateDocLang(params().language);
533 bool Buffer::readFile(string const & filename, par_type pit)
536 lex.setFile(filename);
537 return readFile(lex, filename, pit);
541 bool Buffer::fully_loaded() const
543 return pimpl_->file_fully_loaded;
547 void Buffer::fully_loaded(bool value)
549 pimpl_->file_fully_loaded = value;
553 bool Buffer::readFile(LyXLex & lex, string const & filename, par_type pit)
555 BOOST_ASSERT(!filename.empty());
558 Alert::error(_("Document could not be read"),
559 bformat(_("%1$s could not be read."), filename));
564 string const token(lex.getString());
567 Alert::error(_("Document could not be read"),
568 bformat(_("%1$s could not be read."), filename));
572 // the first token _must_ be...
573 if (token != "\\lyxformat") {
574 lyxerr << "Token: " << token << endl;
576 Alert::error(_("Document format failure"),
577 bformat(_("%1$s is not a LyX document."),
583 string tmp_format = lex.getString();
584 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
585 // if present remove ".," from string.
586 string::size_type dot = tmp_format.find_first_of(".,");
587 //lyxerr << " dot found at " << dot << endl;
588 if (dot != string::npos)
589 tmp_format.erase(dot, 1);
590 int file_format = strToInt(tmp_format);
591 //lyxerr << "format: " << file_format << endl;
593 if (file_format > LYX_FORMAT) {
594 Alert::warning(_("Document format failure"),
595 bformat(_("%1$s was created with a newer"
596 " version of LyX. This is likely to"
599 } else if (file_format < LYX_FORMAT) {
600 string const tmpfile = tempName();
601 if (tmpfile.empty()) {
602 Alert::error(_("Conversion failed"),
603 bformat(_("%1$s is from an earlier"
604 " version of LyX, but a temporary"
605 " file for converting it could"
610 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
611 if (command.empty()) {
612 Alert::error(_("Conversion script not found"),
613 bformat(_("%1$s is from an earlier"
614 " version of LyX, but the"
615 " conversion script lyx2lyx"
616 " could not be found."),
622 + " -o " + tmpfile + ' '
623 + QuoteName(filename);
624 lyxerr[Debug::INFO] << "Running '"
627 cmd_ret const ret = RunCommand(command);
628 if (ret.first != 0) {
629 Alert::error(_("Conversion script failed"),
630 bformat(_("%1$s is from an earlier version"
631 " of LyX, but the lyx2lyx script"
632 " failed to convert it."),
636 bool ret = readFile(tmpfile, pit);
637 // Do stuff with tmpfile name and buffer name here.
643 bool the_end = readBody(lex);
644 params().setPaperStuff();
650 if (token == "\\end_document")
654 Alert::error(_("Document format failure"),
655 bformat(_("%1$s ended unexpectedly, which means"
656 " that it is probably corrupted."),
660 pimpl_->file_fully_loaded = true;
665 // Should probably be moved to somewhere else: BufferView? LyXView?
666 bool Buffer::save() const
668 // We don't need autosaves in the immediate future. (Asger)
669 resetAutosaveTimers();
673 if (lyxrc.make_backup) {
674 s = fileName() + '~';
675 if (!lyxrc.backupdir_path.empty())
676 s = AddName(lyxrc.backupdir_path,
677 subst(os::slashify_path(s),'/','!'));
679 // Rename is the wrong way of making a backup,
680 // this is the correct way.
681 /* truss cp fil fil2:
682 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
683 stat("LyXVC.lyx", 0xEFFFF688) = 0
684 open("LyXVC.lyx", O_RDONLY) = 3
685 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
686 fstat(4, 0xEFFFF508) = 0
687 fstat(3, 0xEFFFF508) = 0
688 read(3, " # T h i s f i l e w".., 8192) = 5579
689 write(4, " # T h i s f i l e w".., 5579) = 5579
690 read(3, 0xEFFFD4A0, 8192) = 0
693 chmod("LyXVC3.lyx", 0100644) = 0
694 lseek(0, 0, SEEK_CUR) = 46440
698 // Should probably have some more error checking here.
699 // Doing it this way, also makes the inodes stay the same.
700 // This is still not a very good solution, in particular we
701 // might loose the owner of the backup.
702 FileInfo finfo(fileName());
704 mode_t fmode = finfo.getMode();
705 struct utimbuf times = {
706 finfo.getAccessTime(),
707 finfo.getModificationTime() };
709 ifstream ifs(fileName().c_str());
710 ofstream ofs(s.c_str(), ios::out|ios::trunc);
715 ::chmod(s.c_str(), fmode);
717 if (::utime(s.c_str(), ×)) {
718 lyxerr << "utime error." << endl;
721 lyxerr << "LyX was not able to make "
722 "backup copy. Beware." << endl;
727 if (writeFile(fileName())) {
729 removeAutosaveFile(fileName());
731 // Saving failed, so backup is not backup
732 if (lyxrc.make_backup)
733 rename(s, fileName());
740 bool Buffer::writeFile(string const & fname) const
742 if (pimpl_->read_only && fname == fileName())
745 FileInfo finfo(fname);
746 if (finfo.exist() && !finfo.writable())
751 if (params().compressed) {
752 gz::ogzstream ofs(fname.c_str());
756 retval = do_writeFile(ofs);
759 ofstream ofs(fname.c_str());
763 retval = do_writeFile(ofs);
770 bool Buffer::do_writeFile(ostream & ofs) const
773 // Use the standard "C" locale for file output.
774 ofs.imbue(std::locale::classic());
777 // The top of the file should not be written by params().
779 // write out a comment in the top of the file
780 ofs << "#LyX " << lyx_version
781 << " created this file. For more info see http://www.lyx.org/\n"
782 << "\\lyxformat " << LYX_FORMAT << "\n";
784 // now write out the buffer parameters.
785 params().writeFile(ofs);
787 ofs << "\\end_header\n";
790 text().write(*this, ofs);
792 // Write marker that shows file is complete
793 ofs << "\n\\end_document" << endl;
795 // Shouldn't really be needed....
798 // how to check if close went ok?
799 // Following is an attempt... (BE 20001011)
801 // good() returns false if any error occured, including some
803 // bad() returns true if something bad happened in the buffer,
804 // which should include file system full errors.
809 lyxerr << "File was not closed properly." << endl;
816 void Buffer::makeLaTeXFile(string const & fname,
817 string const & original_path,
818 OutputParams const & runparams,
819 bool output_preamble, bool output_body)
821 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
824 if (!openFileWrite(ofs, fname))
827 makeLaTeXFile(ofs, original_path,
828 runparams, output_preamble, output_body);
832 lyxerr << "File '" << fname << "' was not closed properly." << endl;
836 void Buffer::makeLaTeXFile(ostream & os,
837 string const & original_path,
838 OutputParams const & runparams_in,
839 bool output_preamble, bool output_body)
841 OutputParams runparams = runparams_in;
843 // validate the buffer.
844 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
845 LaTeXFeatures features(*this, params(), runparams.nice);
847 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
850 // The starting paragraph of the coming rows is the
851 // first paragraph of the document. (Asger)
852 texrow().start(paragraphs().begin()->id(), 0);
854 if (output_preamble && runparams.nice) {
855 os << "%% LyX " << lyx_version << " created this file. "
856 "For more info, see http://www.lyx.org/.\n"
857 "%% Do not edit unless you really know what "
862 lyxerr[Debug::INFO] << "lyx header finished" << endl;
863 // There are a few differences between nice LaTeX and usual files:
864 // usual is \batchmode and has a
865 // special input@path to allow the including of figures
866 // with either \input or \includegraphics (what figinsets do).
867 // input@path is set when the actual parameter
868 // original_path is set. This is done for usual tex-file, but not
869 // for nice-latex-file. (Matthias 250696)
870 // Note that input@path is only needed for something the user does
871 // in the preamble, included .tex files or ERT, files included by
872 // LyX work without it.
873 if (output_preamble) {
874 if (!runparams.nice) {
875 // code for usual, NOT nice-latex-file
876 os << "\\batchmode\n"; // changed
880 if (!original_path.empty()) {
881 string inputpath = os::external_path(original_path);
882 subst(inputpath, "~", "\\string~");
883 os << "\\makeatletter\n"
884 << "\\def\\input@path{{"
885 << inputpath << "/}}\n"
886 << "\\makeatother\n";
892 // Write the preamble
893 runparams.use_babel = params().writeLaTeX(os, features, texrow());
899 os << "\\begin{document}\n";
902 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
904 if (!lyxrc.language_auto_begin) {
905 os << subst(lyxrc.language_command_begin, "$$lang",
906 params().language->babel())
911 // if we are doing a real file with body, even if this is the
912 // child of some other buffer, let's cut the link here.
913 // This happens for example if only a child document is printed.
914 string save_parentname;
915 if (output_preamble) {
916 save_parentname = params().parentname;
917 params().parentname.erase();
921 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
923 // Restore the parenthood if needed
925 params().parentname = save_parentname;
927 // add this just in case after all the paragraphs
931 if (!lyxrc.language_auto_end) {
932 os << subst(lyxrc.language_command_end, "$$lang",
933 params().language->babel())
938 if (output_preamble) {
939 os << "\\end{document}\n";
942 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
944 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
948 // Just to be sure. (Asger)
951 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
952 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
957 bool Buffer::isLatex() const
959 return params().getLyXTextClass().outputType() == LATEX;
963 bool Buffer::isLinuxDoc() const
965 return params().getLyXTextClass().outputType() == LINUXDOC;
969 bool Buffer::isLiterate() const
971 return params().getLyXTextClass().outputType() == LITERATE;
975 bool Buffer::isDocBook() const
977 return params().getLyXTextClass().outputType() == DOCBOOK;
981 bool Buffer::isSGML() const
983 LyXTextClass const & tclass = params().getLyXTextClass();
985 return tclass.outputType() == LINUXDOC ||
986 tclass.outputType() == DOCBOOK;
990 void Buffer::makeLinuxDocFile(string const & fname,
991 OutputParams const & runparams,
995 if (!openFileWrite(ofs, fname))
998 LaTeXFeatures features(*this, params(), runparams.nice);
1003 LyXTextClass const & tclass = params().getLyXTextClass();
1005 string top_element = tclass.latexname();
1008 ofs << tclass.class_header();
1010 string preamble = params().preamble;
1011 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1013 preamble += features.getIncludedFiles(name);
1014 preamble += features.getLyXSGMLEntities();
1016 if (!preamble.empty()) {
1017 ofs << " [ " << preamble << " ]";
1021 if (params().options.empty())
1022 sgml::openTag(ofs, 0, false, top_element);
1024 string top = top_element;
1026 top += params().options;
1027 sgml::openTag(ofs, 0, false, top);
1031 ofs << "<!-- LyX " << lyx_version
1032 << " created this file. For more info see http://www.lyx.org/"
1035 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1039 sgml::closeTag(ofs, 0, false, top_element);
1044 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1048 void Buffer::makeDocBookFile(string const & fname,
1049 OutputParams const & runparams,
1053 if (!openFileWrite(ofs, fname))
1056 LaTeXFeatures features(*this, params(), runparams.nice);
1061 LyXTextClass const & tclass = params().getLyXTextClass();
1062 string top_element = tclass.latexname();
1065 ofs << subst(tclass.class_header(), "#", top_element);
1067 string preamble = params().preamble;
1068 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1070 preamble += features.getIncludedFiles(name);
1071 preamble += features.getLyXSGMLEntities();
1073 if (!preamble.empty()) {
1074 ofs << "\n [ " << preamble << " ]";
1079 string top = top_element;
1081 top += params().language->code();
1084 if (!params().options.empty()) {
1086 top += params().options;
1088 sgml::openTag(ofs, 0, false, top);
1090 ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1091 << "\n See http://www.lyx.org/ for more information -->\n";
1093 params().getLyXTextClass().counters().reset();
1094 docbookParagraphs(*this, paragraphs(), ofs, runparams);
1097 sgml::closeTag(ofs, 0, false, top_element);
1101 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1105 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1106 // Other flags: -wall -v0 -x
1107 int Buffer::runChktex()
1111 // get LaTeX-Filename
1112 string const name = getLatexName();
1113 string const path = temppath();
1114 string const org_path = filePath();
1116 Path p(path); // path to LaTeX file
1117 message(_("Running chktex..."));
1119 // Generate the LaTeX file if neccessary
1120 OutputParams runparams;
1121 runparams.flavor = OutputParams::LATEX;
1122 runparams.nice = false;
1123 makeLaTeXFile(name, org_path, runparams);
1126 Chktex chktex(lyxrc.chktex_command, name, filePath());
1127 int res = chktex.run(terr); // run chktex
1130 Alert::error(_("chktex failure"),
1131 _("Could not run chktex successfully."));
1132 } else if (res > 0) {
1133 // Insert all errors as errors boxes
1134 bufferErrors(*this, terr);
1143 void Buffer::validate(LaTeXFeatures & features) const
1145 LyXTextClass const & tclass = params().getLyXTextClass();
1147 if (params().tracking_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(std::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(std::vector<std::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() == InsetOld::BIBTEX_CODE) {
1224 InsetBibtex const & inset =
1225 dynamic_cast<InsetBibtex const &>(*it);
1226 inset.fillWithBibKeys(*this, keys);
1227 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1228 InsetInclude const & inset =
1229 dynamic_cast<InsetInclude const &>(*it);
1230 inset.fillWithBibKeys(*this, keys);
1231 } else if (it->lyxCode() == InsetOld::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 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)
1286 lyxerr << "Changing Language!" << endl;
1288 // Take care of l10n/i18n
1291 ParIterator end = par_iterator_end();
1292 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1293 it->changeLanguage(params(), from, to);
1297 void Buffer::updateDocLang(Language const * nlang)
1299 pimpl_->messages.reset(new Messages(nlang->code()));
1303 bool Buffer::isMultiLingual() const
1305 ParConstIterator end = par_iterator_end();
1306 for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1307 if (it->isMultiLingual(params()))
1314 ParIterator Buffer::getParFromID(int id) const
1316 ParConstIterator it = par_iterator_begin();
1317 ParConstIterator end = par_iterator_end();
1320 // John says this is called with id == -1 from undo
1321 lyxerr << "getParFromID(), id: " << id << endl;
1325 for (; it != end; ++it)
1333 bool Buffer::hasParWithID(int id) const
1335 ParConstIterator it = par_iterator_begin();
1336 ParConstIterator end = par_iterator_end();
1339 // John says this is called with id == -1 from undo
1340 lyxerr << "hasParWithID(), id: " << id << endl;
1344 for (; it != end; ++it)
1352 ParIterator Buffer::par_iterator_begin()
1354 return ::par_iterator_begin(inset());
1358 ParIterator Buffer::par_iterator_end()
1360 return ::par_iterator_end(inset());
1364 ParConstIterator Buffer::par_iterator_begin() const
1366 return ::par_const_iterator_begin(inset());
1370 ParConstIterator Buffer::par_iterator_end() const
1372 return ::par_const_iterator_end(inset());
1376 Language const * Buffer::getLanguage() const
1378 return params().language;
1382 string const Buffer::B_(string const & l10n) const
1384 if (pimpl_->messages.get()) {
1385 return pimpl_->messages->get(l10n);
1392 bool Buffer::isClean() const
1394 return pimpl_->lyx_clean;
1398 bool Buffer::isBakClean() const
1400 return pimpl_->bak_clean;
1404 void Buffer::markClean() const
1406 if (!pimpl_->lyx_clean) {
1407 pimpl_->lyx_clean = true;
1410 // if the .lyx file has been saved, we don't need an
1412 pimpl_->bak_clean = true;
1416 void Buffer::markBakClean()
1418 pimpl_->bak_clean = true;
1422 void Buffer::setUnnamed(bool flag)
1424 pimpl_->unnamed = flag;
1428 bool Buffer::isUnnamed() const
1430 return pimpl_->unnamed;
1434 void Buffer::markDirty()
1436 if (pimpl_->lyx_clean) {
1437 pimpl_->lyx_clean = false;
1440 pimpl_->bak_clean = false;
1442 DepClean::iterator it = pimpl_->dep_clean.begin();
1443 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1445 for (; it != end; ++it) {
1451 string const & Buffer::fileName() const
1453 return pimpl_->filename;
1457 string const & Buffer::filePath() const
1459 return pimpl_->filepath;
1463 bool Buffer::isReadonly() const
1465 return pimpl_->read_only;
1469 void Buffer::setParentName(string const & name)
1471 params().parentname = name;
1475 Buffer const * Buffer::getMasterBuffer() const
1477 if (!params().parentname.empty()
1478 && bufferlist.exists(params().parentname)) {
1479 Buffer const * buf = bufferlist.getBuffer(params().parentname);
1481 return buf->getMasterBuffer();
1488 MacroData const & Buffer::getMacro(std::string const & name) const
1490 return pimpl_->macros.get(name);
1494 bool Buffer::hasMacro(string const & name) const
1496 return pimpl_->macros.has(name);
1500 void Buffer::insertMacro(string const & name, MacroData const & data)
1502 pimpl_->macros.insert(name, data);
1506 void Buffer::buildMacros()
1508 // Start with global table.
1509 pimpl_->macros = MacroTable::globalMacros();
1512 ParagraphList & pars = text().paragraphs();
1513 for (size_t i = 0, n = pars.size(); i != n; ++i) {
1514 //lyxerr << "searching main par " << i
1515 // << " for macro definitions" << std::endl;
1516 InsetList::iterator it = pars[i].insetlist.begin();
1517 InsetList::iterator end = pars[i].insetlist.end();
1518 for ( ; it != end; ++it) {
1519 //lyxerr << "found inset code " << it->inset->lyxCode() << std::endl;
1520 if (it->inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
1521 MathMacroTemplate & mac
1522 = static_cast<MathMacroTemplate &>(*it->inset);
1523 insertMacro(mac.name(), mac.asMacroData());