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 "frontends/Alert.h"
59 #include "graphics/Previews.h"
61 #include "support/FileInfo.h"
62 #include "support/filetools.h"
63 #include "support/gzstream.h"
64 #include "support/lyxlib.h"
65 #include "support/os.h"
66 #include "support/path.h"
67 #include "support/textutils.h"
68 #include "support/tostr.h"
69 #include "support/std_sstream.h"
71 #include <boost/bind.hpp>
81 using lyx::support::AddName;
82 using lyx::support::atoi;
83 using lyx::support::bformat;
84 using lyx::support::ChangeExtension;
85 using lyx::support::cmd_ret;
86 using lyx::support::createBufferTmpDir;
87 using lyx::support::destroyDir;
88 using lyx::support::FileInfo;
89 using lyx::support::FileInfo;
90 using lyx::support::getExtFromContents;
91 using lyx::support::IsDirWriteable;
92 using lyx::support::IsFileWriteable;
93 using lyx::support::LibFileSearch;
94 using lyx::support::ltrim;
95 using lyx::support::MakeAbsPath;
96 using lyx::support::MakeDisplayPath;
97 using lyx::support::MakeLatexName;
98 using lyx::support::OnlyFilename;
99 using lyx::support::OnlyPath;
100 using lyx::support::Path;
101 using lyx::support::QuoteName;
102 using lyx::support::removeAutosaveFile;
103 using lyx::support::rename;
104 using lyx::support::RunCommand;
105 using lyx::support::split;
106 using lyx::support::strToInt;
107 using lyx::support::subst;
108 using lyx::support::tempName;
109 using lyx::support::trim;
111 namespace os = lyx::support::os;
115 using std::make_pair;
120 using std::ostringstream;
128 // all these externs should eventually be removed.
129 extern BufferList bufferlist;
133 const int LYX_FORMAT = 232;
138 typedef std::map<string, bool> DepClean;
142 Impl(Buffer & parent, string const & file, bool readonly);
144 limited_stack<Undo> undostack;
145 limited_stack<Undo> redostack;
151 /// need to regenerate .tex?
155 mutable bool lyx_clean;
157 /// is autosave needed?
158 mutable bool bak_clean;
160 /// is this a unnamed file (New...)?
166 /// name of the file the buffer is associated with.
169 /// The path to the document file.
172 boost::scoped_ptr<Messages> messages;
174 /** Set to true only when the file is fully loaded.
175 * Used to prevent the premature generation of previews
176 * and by the citation inset.
178 bool file_fully_loaded;
180 /// our LyXText that should be wrapped in an InsetText
185 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
186 : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
187 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
190 lyxvc.buffer(&parent);
191 temppath = createBufferTmpDir();
192 // FIXME: And now do something if temppath == string(), because we
193 // assume from now on that temppath points to a valid temp dir.
194 // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
198 Buffer::Buffer(string const & file, bool ronly)
199 : pimpl_(new Impl(*this, file, ronly))
201 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
207 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
208 // here the buffer should take care that it is
209 // saved properly, before it goes into the void.
213 if (!temppath().empty() && destroyDir(temppath()) != 0) {
214 Alert::warning(_("Could not remove temporary directory"),
215 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
218 // Remove any previewed LaTeX snippets associated with this buffer.
219 lyx::graphics::Previews::get().removeLoader(*this);
223 LyXText & Buffer::text() const
225 return const_cast<LyXText &>(pimpl_->inset.text_);
229 InsetBase & Buffer::inset() const
231 return const_cast<InsetText &>(pimpl_->inset);
235 limited_stack<Undo> & Buffer::undostack()
237 return pimpl_->undostack;
241 limited_stack<Undo> const & Buffer::undostack() const
243 return pimpl_->undostack;
247 limited_stack<Undo> & Buffer::redostack()
249 return pimpl_->redostack;
253 limited_stack<Undo> const & Buffer::redostack() const
255 return pimpl_->redostack;
259 BufferParams & Buffer::params()
261 return pimpl_->params;
265 BufferParams const & Buffer::params() const
267 return pimpl_->params;
271 ParagraphList & Buffer::paragraphs()
273 return text().paragraphs();
277 ParagraphList const & Buffer::paragraphs() const
279 return text().paragraphs();
283 LyXVC & Buffer::lyxvc()
285 return pimpl_->lyxvc;
289 LyXVC const & Buffer::lyxvc() const
291 return pimpl_->lyxvc;
295 string const & Buffer::temppath() const
297 return pimpl_->temppath;
301 TexRow & Buffer::texrow()
303 return pimpl_->texrow;
307 TexRow const & Buffer::texrow() const
309 return pimpl_->texrow;
313 string const Buffer::getLatexName(bool no_path) const
315 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
316 return no_path ? OnlyFilename(name) : name;
320 pair<Buffer::LogType, string> const Buffer::getLogName() const
322 string const filename = getLatexName(false);
324 if (filename.empty())
325 return make_pair(Buffer::latexlog, string());
327 string const path = temppath();
329 string const fname = AddName(path,
330 OnlyFilename(ChangeExtension(filename,
333 AddName(path, OnlyFilename(
334 ChangeExtension(filename,
335 formats.extension("literate") + ".out")));
337 // If no Latex log or Build log is newer, show Build log
339 FileInfo const f_fi(fname);
340 FileInfo const b_fi(bname);
343 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
344 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
345 return make_pair(Buffer::buildlog, bname);
347 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
348 return make_pair(Buffer::latexlog, fname);
352 void Buffer::setReadonly(bool flag)
354 if (pimpl_->read_only != flag) {
355 pimpl_->read_only = flag;
361 void Buffer::setFileName(string const & newfile)
363 pimpl_->filename = MakeAbsPath(newfile);
364 pimpl_->filepath = OnlyPath(pimpl_->filename);
365 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
370 // We'll remove this later. (Lgb)
373 void unknownClass(string const & unknown)
375 Alert::warning(_("Unknown document class"),
376 bformat(_("Using the default document class, because the "
377 "class %1$s is unknown."), unknown));
383 int Buffer::readHeader(LyXLex & lex)
385 int unknown_tokens = 0;
389 string const token = lex.getString();
394 if (token == "\\end_header")
397 lyxerr[Debug::PARSER] << "Handling header token: `"
398 << token << '\'' << endl;
401 string unknown = params().readToken(lex, token);
402 if (!unknown.empty()) {
403 if (unknown[0] != '\\') {
404 unknownClass(unknown);
407 string const s = bformat(_("Unknown token: "
411 error(ErrorItem(_("Header error"), s,
416 return unknown_tokens;
421 // changed to be public and have one parameter
422 // Returns false if "\end_document" is not read (Asger)
423 bool Buffer::readBody(LyXLex & lex)
425 if (paragraphs().empty()) {
427 if (!params().getLyXTextClass().load()) {
428 string theclass = params().getLyXTextClass().name();
429 Alert::error(_("Can't load document class"), bformat(
430 "Using the default document class, because the "
431 " class %1$s could not be loaded.", theclass));
432 params().textclass = 0;
435 // We don't want to adopt the parameters from the
436 // document we insert, so read them into a temporary buffer
437 // and then discard it
439 Buffer tmpbuf("", false);
440 tmpbuf.readHeader(lex);
443 return text().read(*this, lex);
447 // needed to insert the selection
448 void Buffer::insertStringAsLines(ParagraphList & pars,
449 par_type & par, pos_type & pos,
450 LyXFont const & fn, string const & str)
452 LyXLayout_ptr const & layout = pars[par].layout();
456 pars[par].checkInsertChar(font);
457 // insert the string, don't insert doublespace
458 bool space_inserted = true;
459 bool autobreakrows = !pars[par].inInset() ||
460 static_cast<InsetText *>(pars[par].inInset())->getAutoBreakRows();
461 for (string::const_iterator cit = str.begin();
462 cit != str.end(); ++cit) {
464 if (autobreakrows && (!pars[par].empty() || pars[par].allowEmpty())) {
465 breakParagraph(params(), paragraphs(), par, pos,
466 layout->isEnvironment());
469 space_inserted = true;
473 // do not insert consecutive spaces if !free_spacing
474 } else if ((*cit == ' ' || *cit == '\t') &&
475 space_inserted && !pars[par].isFreeSpacing()) {
477 } else if (*cit == '\t') {
478 if (!pars[par].isFreeSpacing()) {
479 // tabs are like spaces here
480 pars[par].insertChar(pos, ' ', font);
482 space_inserted = true;
484 const pos_type n = 8 - pos % 8;
485 for (pos_type i = 0; i < n; ++i) {
486 pars[par].insertChar(pos, ' ', font);
489 space_inserted = true;
491 } else if (!IsPrintable(*cit)) {
492 // Ignore unprintables
495 // just insert the character
496 pars[par].insertChar(pos, *cit, font);
498 space_inserted = (*cit == ' ');
505 bool Buffer::readFile(string const & filename)
507 // Check if the file is compressed.
508 string const format = getExtFromContents(filename);
509 if (format == "gzip" || format == "zip" || format == "compress") {
510 params().compressed = true;
513 // remove dummy empty par
514 paragraphs().clear();
515 bool ret = readFile(filename, paragraphs().size());
517 // After we have read a file, we must ensure that the buffer
518 // language is set and used in the gui.
519 // If you know of a better place to put this, please tell me. (Lgb)
520 updateDocLang(params().language);
526 bool Buffer::readFile(string const & filename, par_type pit)
529 lex.setFile(filename);
530 return readFile(lex, filename, pit);
534 bool Buffer::fully_loaded() const
536 return pimpl_->file_fully_loaded;
540 void Buffer::fully_loaded(bool value)
542 pimpl_->file_fully_loaded = value;
546 bool Buffer::readFile(LyXLex & lex, string const & filename, par_type pit)
548 BOOST_ASSERT(!filename.empty());
551 Alert::error(_("Document could not be read"),
552 bformat(_("%1$s could not be read."), filename));
557 string const token(lex.getString());
560 Alert::error(_("Document could not be read"),
561 bformat(_("%1$s could not be read."), filename));
565 // the first token _must_ be...
566 if (token != "\\lyxformat") {
567 lyxerr << "Token: " << token << endl;
569 Alert::error(_("Document format failure"),
570 bformat(_("%1$s is not a LyX document."),
576 string tmp_format = lex.getString();
577 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
578 // if present remove ".," from string.
579 string::size_type dot = tmp_format.find_first_of(".,");
580 //lyxerr << " dot found at " << dot << endl;
581 if (dot != string::npos)
582 tmp_format.erase(dot, 1);
583 int file_format = strToInt(tmp_format);
584 //lyxerr << "format: " << file_format << endl;
586 if (file_format > LYX_FORMAT) {
587 Alert::warning(_("Document format failure"),
588 bformat(_("%1$s was created with a newer"
589 " version of LyX. This is likely to"
592 } else if (file_format < LYX_FORMAT) {
593 string const tmpfile = tempName();
594 if (tmpfile.empty()) {
595 Alert::error(_("Conversion failed"),
596 bformat(_("%1$s is from an earlier"
597 " version of LyX, but a temporary"
598 " file for converting it could"
603 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
604 if (command.empty()) {
605 Alert::error(_("Conversion script not found"),
606 bformat(_("%1$s is from an earlier"
607 " version of LyX, but the"
608 " conversion script lyx2lyx"
609 " could not be found."),
615 + " -o " + tmpfile + ' '
616 + QuoteName(filename);
617 lyxerr[Debug::INFO] << "Running '"
620 cmd_ret const ret = RunCommand(command);
621 if (ret.first != 0) {
622 Alert::error(_("Conversion script failed"),
623 bformat(_("%1$s is from an earlier version"
624 " of LyX, but the lyx2lyx script"
625 " failed to convert it."),
629 bool ret = readFile(tmpfile, pit);
630 // Do stuff with tmpfile name and buffer name here.
636 bool the_end = readBody(lex);
637 params().setPaperStuff();
643 if (token == "\\end_document")
647 Alert::error(_("Document format failure"),
648 bformat(_("%1$s ended unexpectedly, which means"
649 " that it is probably corrupted."),
653 pimpl_->file_fully_loaded = true;
658 // Should probably be moved to somewhere else: BufferView? LyXView?
659 bool Buffer::save() const
661 // We don't need autosaves in the immediate future. (Asger)
662 resetAutosaveTimers();
666 if (lyxrc.make_backup) {
667 s = fileName() + '~';
668 if (!lyxrc.backupdir_path.empty())
669 s = AddName(lyxrc.backupdir_path,
670 subst(os::slashify_path(s),'/','!'));
672 // Rename is the wrong way of making a backup,
673 // this is the correct way.
674 /* truss cp fil fil2:
675 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
676 stat("LyXVC.lyx", 0xEFFFF688) = 0
677 open("LyXVC.lyx", O_RDONLY) = 3
678 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
679 fstat(4, 0xEFFFF508) = 0
680 fstat(3, 0xEFFFF508) = 0
681 read(3, " # T h i s f i l e w".., 8192) = 5579
682 write(4, " # T h i s f i l e w".., 5579) = 5579
683 read(3, 0xEFFFD4A0, 8192) = 0
686 chmod("LyXVC3.lyx", 0100644) = 0
687 lseek(0, 0, SEEK_CUR) = 46440
691 // Should probably have some more error checking here.
692 // Doing it this way, also makes the inodes stay the same.
693 // This is still not a very good solution, in particular we
694 // might loose the owner of the backup.
695 FileInfo finfo(fileName());
697 mode_t fmode = finfo.getMode();
698 struct utimbuf times = {
699 finfo.getAccessTime(),
700 finfo.getModificationTime() };
702 ifstream ifs(fileName().c_str());
703 ofstream ofs(s.c_str(), ios::out|ios::trunc);
708 ::chmod(s.c_str(), fmode);
710 if (::utime(s.c_str(), ×)) {
711 lyxerr << "utime error." << endl;
714 lyxerr << "LyX was not able to make "
715 "backup copy. Beware." << endl;
720 if (writeFile(fileName())) {
722 removeAutosaveFile(fileName());
724 // Saving failed, so backup is not backup
725 if (lyxrc.make_backup)
726 rename(s, fileName());
733 bool Buffer::writeFile(string const & fname) const
735 if (pimpl_->read_only && fname == fileName())
738 FileInfo finfo(fname);
739 if (finfo.exist() && !finfo.writable())
744 if (params().compressed) {
745 gz::ogzstream ofs(fname.c_str());
749 retval = do_writeFile(ofs);
752 ofstream ofs(fname.c_str());
756 retval = do_writeFile(ofs);
763 bool Buffer::do_writeFile(ostream & ofs) const
766 // Use the standard "C" locale for file output.
767 ofs.imbue(std::locale::classic());
770 // The top of the file should not be written by params().
772 // write out a comment in the top of the file
773 ofs << "#LyX " << lyx_version
774 << " created this file. For more info see http://www.lyx.org/\n"
775 << "\\lyxformat " << LYX_FORMAT << "\n";
777 // now write out the buffer parameters.
778 params().writeFile(ofs);
780 ofs << "\\end_header\n";
783 text().write(*this, ofs);
785 // Write marker that shows file is complete
786 ofs << "\n\\end_document" << endl;
788 // Shouldn't really be needed....
791 // how to check if close went ok?
792 // Following is an attempt... (BE 20001011)
794 // good() returns false if any error occured, including some
796 // bad() returns true if something bad happened in the buffer,
797 // which should include file system full errors.
802 lyxerr << "File was not closed properly." << endl;
809 void Buffer::makeLaTeXFile(string const & fname,
810 string const & original_path,
811 OutputParams const & runparams,
812 bool output_preamble, bool output_body)
814 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
817 if (!openFileWrite(ofs, fname))
820 makeLaTeXFile(ofs, original_path,
821 runparams, output_preamble, output_body);
825 lyxerr << "File '" << fname << "' was not closed properly." << endl;
829 void Buffer::makeLaTeXFile(ostream & os,
830 string const & original_path,
831 OutputParams const & runparams_in,
832 bool output_preamble, bool output_body)
834 OutputParams runparams = runparams_in;
836 // validate the buffer.
837 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
838 LaTeXFeatures features(*this, params(), runparams.nice);
840 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
843 // The starting paragraph of the coming rows is the
844 // first paragraph of the document. (Asger)
845 texrow().start(paragraphs().begin()->id(), 0);
847 if (output_preamble && runparams.nice) {
848 os << "%% LyX " << lyx_version << " created this file. "
849 "For more info, see http://www.lyx.org/.\n"
850 "%% Do not edit unless you really know what "
855 lyxerr[Debug::INFO] << "lyx header finished" << endl;
856 // There are a few differences between nice LaTeX and usual files:
857 // usual is \batchmode and has a
858 // special input@path to allow the including of figures
859 // with either \input or \includegraphics (what figinsets do).
860 // input@path is set when the actual parameter
861 // original_path is set. This is done for usual tex-file, but not
862 // for nice-latex-file. (Matthias 250696)
863 // Note that input@path is only needed for something the user does
864 // in the preamble, included .tex files or ERT, files included by
865 // LyX work without it.
866 if (output_preamble) {
867 if (!runparams.nice) {
868 // code for usual, NOT nice-latex-file
869 os << "\\batchmode\n"; // changed
873 if (!original_path.empty()) {
874 string inputpath = os::external_path(original_path);
875 subst(inputpath, "~", "\\string~");
876 os << "\\makeatletter\n"
877 << "\\def\\input@path{{"
878 << inputpath << "/}}\n"
879 << "\\makeatother\n";
885 // Write the preamble
886 runparams.use_babel = params().writeLaTeX(os, features, texrow());
892 os << "\\begin{document}\n";
895 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
897 if (!lyxrc.language_auto_begin) {
898 os << subst(lyxrc.language_command_begin, "$$lang",
899 params().language->babel())
904 // if we are doing a real file with body, even if this is the
905 // child of some other buffer, let's cut the link here.
906 // This happens for example if only a child document is printed.
907 string save_parentname;
908 if (output_preamble) {
909 save_parentname = params().parentname;
910 params().parentname.erase();
914 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
916 // Restore the parenthood if needed
918 params().parentname = save_parentname;
920 // add this just in case after all the paragraphs
924 if (!lyxrc.language_auto_end) {
925 os << subst(lyxrc.language_command_end, "$$lang",
926 params().language->babel())
931 if (output_preamble) {
932 os << "\\end{document}\n";
935 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
937 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
941 // Just to be sure. (Asger)
944 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
945 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
950 bool Buffer::isLatex() const
952 return params().getLyXTextClass().outputType() == LATEX;
956 bool Buffer::isLinuxDoc() const
958 return params().getLyXTextClass().outputType() == LINUXDOC;
962 bool Buffer::isLiterate() const
964 return params().getLyXTextClass().outputType() == LITERATE;
968 bool Buffer::isDocBook() const
970 return params().getLyXTextClass().outputType() == DOCBOOK;
974 bool Buffer::isSGML() const
976 LyXTextClass const & tclass = params().getLyXTextClass();
978 return tclass.outputType() == LINUXDOC ||
979 tclass.outputType() == DOCBOOK;
983 void Buffer::makeLinuxDocFile(string const & fname,
984 OutputParams const & runparams,
988 if (!openFileWrite(ofs, fname))
991 LaTeXFeatures features(*this, params(), runparams.nice);
996 LyXTextClass const & tclass = params().getLyXTextClass();
998 string top_element = tclass.latexname();
1001 ofs << tclass.class_header();
1003 string preamble = params().preamble;
1004 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1006 preamble += features.getIncludedFiles(name);
1007 preamble += features.getLyXSGMLEntities();
1009 if (!preamble.empty()) {
1010 ofs << " [ " << preamble << " ]";
1014 if (params().options.empty())
1015 sgml::openTag(ofs, 0, false, top_element);
1017 string top = top_element;
1019 top += params().options;
1020 sgml::openTag(ofs, 0, false, top);
1024 ofs << "<!-- LyX " << lyx_version
1025 << " created this file. For more info see http://www.lyx.org/"
1028 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1032 sgml::closeTag(ofs, 0, false, top_element);
1037 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1041 void Buffer::makeDocBookFile(string const & fname,
1042 OutputParams const & runparams,
1046 if (!openFileWrite(ofs, fname))
1049 LaTeXFeatures features(*this, params(), runparams.nice);
1054 LyXTextClass const & tclass = params().getLyXTextClass();
1055 string top_element = tclass.latexname();
1058 ofs << subst(tclass.class_header(), "#", top_element);
1060 string preamble = params().preamble;
1061 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1063 preamble += features.getIncludedFiles(name);
1064 preamble += features.getLyXSGMLEntities();
1066 if (!preamble.empty()) {
1067 ofs << "\n [ " << preamble << " ]";
1072 string top = top_element;
1074 top += params().language->code();
1077 if (!params().options.empty()) {
1079 top += params().options;
1081 sgml::openTag(ofs, 0, false, top);
1083 ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1084 << "\n See http://www.lyx.org/ for more information -->\n";
1086 params().getLyXTextClass().counters().reset();
1087 docbookParagraphs(*this, paragraphs(), ofs, runparams);
1090 sgml::closeTag(ofs, 0, false, top_element);
1094 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1098 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1099 // Other flags: -wall -v0 -x
1100 int Buffer::runChktex()
1104 // get LaTeX-Filename
1105 string const name = getLatexName();
1106 string const path = temppath();
1107 string const org_path = filePath();
1109 Path p(path); // path to LaTeX file
1110 message(_("Running chktex..."));
1112 // Generate the LaTeX file if neccessary
1113 OutputParams runparams;
1114 runparams.flavor = OutputParams::LATEX;
1115 runparams.nice = false;
1116 makeLaTeXFile(name, org_path, runparams);
1119 Chktex chktex(lyxrc.chktex_command, name, filePath());
1120 int res = chktex.run(terr); // run chktex
1123 Alert::error(_("chktex failure"),
1124 _("Could not run chktex successfully."));
1125 } else if (res > 0) {
1126 // Insert all errors as errors boxes
1127 bufferErrors(*this, terr);
1136 void Buffer::validate(LaTeXFeatures & features) const
1138 LyXTextClass const & tclass = params().getLyXTextClass();
1140 if (params().tracking_changes) {
1141 features.require("dvipost");
1142 features.require("color");
1145 // AMS Style is at document level
1146 if (params().use_amsmath == BufferParams::AMS_ON
1147 || tclass.provides(LyXTextClass::amsmath))
1148 features.require("amsmath");
1150 for_each(paragraphs().begin(), paragraphs().end(),
1151 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1153 // the bullet shapes are buffer level not paragraph level
1154 // so they are tested here
1155 for (int i = 0; i < 4; ++i) {
1156 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1157 int const font = params().user_defined_bullet(i).getFont();
1159 int const c = params()
1160 .user_defined_bullet(i)
1167 features.require("latexsym");
1169 } else if (font == 1) {
1170 features.require("amssymb");
1171 } else if ((font >= 2 && font <= 5)) {
1172 features.require("pifont");
1177 if (lyxerr.debugging(Debug::LATEX)) {
1178 features.showStruct();
1183 void Buffer::getLabelList(std::vector<string> & list) const
1185 /// if this is a child document and the parent is already loaded
1186 /// Use the parent's list instead [ale990407]
1187 Buffer const * tmp = getMasterBuffer();
1189 lyxerr << "getMasterBuffer() failed!" << endl;
1193 tmp->getLabelList(list);
1197 for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
1198 it.nextInset()->getLabelList(*this, list);
1202 // This is also a buffer property (ale)
1203 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys)
1206 /// if this is a child document and the parent is already loaded
1207 /// use the parent's list instead [ale990412]
1208 Buffer const * tmp = getMasterBuffer();
1211 tmp->fillWithBibKeys(keys);
1215 for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1216 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
1217 InsetBibtex const & inset =
1218 dynamic_cast<InsetBibtex const &>(*it);
1219 inset.fillWithBibKeys(*this, keys);
1220 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1221 InsetInclude const & inset =
1222 dynamic_cast<InsetInclude const &>(*it);
1223 inset.fillWithBibKeys(*this, keys);
1224 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
1225 InsetBibitem const & inset =
1226 dynamic_cast<InsetBibitem const &>(*it);
1227 string const key = inset.getContents();
1228 string const opt = inset.getOptions();
1229 string const ref; // = pit->asString(this, false);
1230 string const info = opt + "TheBibliographyRef" + ref;
1231 keys.push_back(pair<string, string>(key, info));
1237 bool Buffer::isDepClean(string const & name) const
1239 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
1240 if (it == pimpl_->dep_clean.end())
1246 void Buffer::markDepClean(string const & name)
1248 pimpl_->dep_clean[name] = true;
1252 bool Buffer::dispatch(string const & command, bool * result)
1254 return dispatch(lyxaction.lookupFunc(command), result);
1258 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1260 bool dispatched = true;
1262 switch (func.action) {
1264 bool const tmp = Exporter::Export(this, func.argument, false);
1277 void Buffer::changeLanguage(Language const * from, Language const * to)
1279 lyxerr << "Changing Language!" << endl;
1281 // Take care of l10n/i18n
1284 ParIterator end = par_iterator_end();
1285 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1286 it->changeLanguage(params(), from, to);
1290 void Buffer::updateDocLang(Language const * nlang)
1292 pimpl_->messages.reset(new Messages(nlang->code()));
1296 bool Buffer::isMultiLingual() const
1298 ParConstIterator end = par_iterator_end();
1299 for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1300 if (it->isMultiLingual(params()))
1307 ParIterator Buffer::getParFromID(int id) const
1309 ParConstIterator it = par_iterator_begin();
1310 ParConstIterator end = par_iterator_end();
1313 // John says this is called with id == -1 from undo
1314 lyxerr << "getParFromID(), id: " << id << endl;
1318 for (; it != end; ++it)
1326 bool Buffer::hasParWithID(int id) const
1328 ParConstIterator it = par_iterator_begin();
1329 ParConstIterator end = par_iterator_end();
1332 // John says this is called with id == -1 from undo
1333 lyxerr << "hasParWithID(), id: " << id << endl;
1337 for (; it != end; ++it)
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 void Buffer::markDirty()
1429 if (pimpl_->lyx_clean) {
1430 pimpl_->lyx_clean = false;
1433 pimpl_->bak_clean = false;
1435 DepClean::iterator it = pimpl_->dep_clean.begin();
1436 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1438 for (; it != end; ++it) {
1444 string const & Buffer::fileName() const
1446 return pimpl_->filename;
1450 string const & Buffer::filePath() const
1452 return pimpl_->filepath;
1456 bool Buffer::isReadonly() const
1458 return pimpl_->read_only;
1462 void Buffer::setParentName(string const & name)
1464 params().parentname = name;
1468 Buffer const * Buffer::getMasterBuffer() const
1470 if (!params().parentname.empty()
1471 && bufferlist.exists(params().parentname)) {
1472 Buffer const * buf = bufferlist.getBuffer(params().parentname);
1474 return buf->getMasterBuffer();