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 "iterators.h"
31 #include "LaTeXFeatures.h"
32 #include "LyXAction.h"
39 #include "output_docbook.h"
40 #include "output_latex.h"
41 #include "output_linuxdoc.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "PosIterator.h"
51 #include "insets/insetbibitem.h"
52 #include "insets/insetbibtex.h"
53 #include "insets/insetinclude.h"
54 #include "insets/insettext.h"
56 #include "frontends/Alert.h"
58 #include "graphics/Previews.h"
60 #include "support/FileInfo.h"
61 #include "support/filetools.h"
62 #include "support/gzstream.h"
63 #include "support/lyxlib.h"
64 #include "support/os.h"
65 #include "support/path.h"
66 #include "support/textutils.h"
67 #include "support/tostr.h"
69 #include <boost/bind.hpp>
71 #include "support/std_sstream.h"
83 using lyx::support::AddName;
84 using lyx::support::atoi;
85 using lyx::support::bformat;
86 using lyx::support::ChangeExtension;
87 using lyx::support::cmd_ret;
88 using lyx::support::CreateBufferTmpDir;
89 using lyx::support::destroyDir;
90 using lyx::support::FileInfo;
91 using lyx::support::FileInfo;
92 using lyx::support::getExtFromContents;
93 using lyx::support::IsDirWriteable;
94 using lyx::support::IsFileWriteable;
95 using lyx::support::LibFileSearch;
96 using lyx::support::ltrim;
97 using lyx::support::MakeAbsPath;
98 using lyx::support::MakeDisplayPath;
99 using lyx::support::MakeLatexName;
100 using lyx::support::OnlyFilename;
101 using lyx::support::OnlyPath;
102 using lyx::support::Path;
103 using lyx::support::QuoteName;
104 using lyx::support::removeAutosaveFile;
105 using lyx::support::rename;
106 using lyx::support::RunCommand;
107 using lyx::support::split;
108 using lyx::support::strToInt;
109 using lyx::support::subst;
110 using lyx::support::tempName;
111 using lyx::support::trim;
113 namespace os = lyx::support::os;
117 using std::make_pair;
122 using std::ostringstream;
130 // all these externs should eventually be removed.
131 extern BufferList bufferlist;
135 const int LYX_FORMAT = 225;
140 typedef std::map<string, bool> DepClean;
144 Impl(Buffer & parent, string const & file, bool readonly);
146 limited_stack<Undo> undostack;
147 limited_stack<Undo> redostack;
154 /// need to regenerate .tex ?
158 mutable bool lyx_clean;
160 /// is autosave needed
161 mutable bool bak_clean;
163 /// is this a unnamed file (New...)
169 /// name of the file the buffer is associated with.
172 /// The path to the document file.
175 boost::scoped_ptr<Messages> messages;
177 /** set to true only when the file is fully loaded.
178 * Used to prevent the premature generation of previews
179 * and by the citation inset.
181 bool file_fully_loaded;
188 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
190 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
191 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
194 lyxvc.buffer(&parent);
195 if (readonly_ || lyxrc.use_tempdir)
196 temppath = CreateBufferTmpDir();
200 Buffer::Buffer(string const & file, bool ronly)
201 : pimpl_(new Impl(*this, file, ronly))
203 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
209 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
210 // here the buffer should take care that it is
211 // saved properly, before it goes into the void.
215 if (!temppath().empty() && destroyDir(temppath()) != 0) {
216 Alert::warning(_("Could not remove temporary directory"),
217 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
220 // Remove any previewed LaTeX snippets associated with this buffer.
221 lyx::graphics::Previews::get().removeLoader(*this);
225 LyXText & Buffer::text() const
227 return const_cast<LyXText &>(pimpl_->text);
231 limited_stack<Undo> & Buffer::undostack()
233 return pimpl_->undostack;
237 limited_stack<Undo> const & Buffer::undostack() const
239 return pimpl_->undostack;
243 limited_stack<Undo> & Buffer::redostack()
245 return pimpl_->redostack;
249 limited_stack<Undo> const & Buffer::redostack() const
251 return pimpl_->redostack;
255 BufferParams & Buffer::params()
257 return pimpl_->params;
261 BufferParams const & Buffer::params() const
263 return pimpl_->params;
267 ParagraphList & Buffer::paragraphs()
269 return pimpl_->text.paragraphs();
273 ParagraphList const & Buffer::paragraphs() const
275 return pimpl_->text.paragraphs();
279 LyXVC & Buffer::lyxvc()
281 return pimpl_->lyxvc;
285 LyXVC const & Buffer::lyxvc() const
287 return pimpl_->lyxvc;
291 string const & Buffer::temppath() const
293 return pimpl_->temppath;
297 bool & Buffer::niceFile()
299 return pimpl_->nicefile;
303 bool Buffer::niceFile() const
305 return pimpl_->nicefile;
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 path = OnlyPath(filename);
337 if (lyxrc.use_tempdir || !IsDirWriteable(path))
340 string const fname = AddName(path,
341 OnlyFilename(ChangeExtension(filename,
344 AddName(path, OnlyFilename(
345 ChangeExtension(filename,
346 formats.extension("literate") + ".out")));
348 // If no Latex log or Build log is newer, show Build log
350 FileInfo const f_fi(fname);
351 FileInfo const b_fi(bname);
354 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
355 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
356 return make_pair(Buffer::buildlog, bname);
358 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
359 return make_pair(Buffer::latexlog, fname);
363 void Buffer::setReadonly(bool flag)
365 if (pimpl_->read_only != flag) {
366 pimpl_->read_only = flag;
372 void Buffer::setFileName(string const & newfile)
374 pimpl_->filename = MakeAbsPath(newfile);
375 pimpl_->filepath = OnlyPath(pimpl_->filename);
376 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
381 // We'll remove this later. (Lgb)
384 void unknownClass(string const & unknown)
386 Alert::warning(_("Unknown document class"),
387 bformat(_("Using the default document class, because the "
388 "class %1$s is unknown."), unknown));
393 int Buffer::readHeader(LyXLex & lex)
395 int unknown_tokens = 0;
399 string const token = lex.getString();
404 if (token == "\\end_header")
407 lyxerr[Debug::PARSER] << "Handling header token: `"
408 << token << '\'' << endl;
411 string unknown = params().readToken(lex, token);
412 if (!unknown.empty()) {
413 if (unknown[0] != '\\') {
414 unknownClass(unknown);
417 string const s = bformat(_("Unknown token: "
421 error(ErrorItem(_("Header error"), s,
426 return unknown_tokens;
430 // candidate for move to BufferView
431 // (at least some parts in the beginning of the func)
434 // changed to be public and have one parameter
435 // if par = 0 normal behavior
436 // else insert behavior
437 // Returns false if "\end_document" is not read (Asger)
438 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
440 Paragraph::depth_type depth = 0;
441 bool the_end_read = false;
443 if (paragraphs().empty()) {
445 if (!params().getLyXTextClass().load()) {
446 string theclass = params().getLyXTextClass().name();
447 Alert::error(_("Can't load document class"), bformat(
448 "Using the default document class, because the "
449 " class %1$s could not be loaded.", theclass));
450 params().textclass = 0;
453 // We don't want to adopt the parameters from the
454 // document we insert, so read them into a temporary buffer
455 // and then discard it
457 Buffer tmpbuf("", false);
458 tmpbuf.readHeader(lex);
463 string const token = lex.getString();
468 lyxerr[Debug::PARSER] << "Handling token: `"
469 << token << '\'' << endl;
471 if (token == "\\end_document") {
476 readParagraph(lex, token, paragraphs(), pit, depth);
483 int Buffer::readParagraph(LyXLex & lex, string const & token,
484 ParagraphList & pars, ParagraphList::iterator & pit,
485 lyx::depth_type & depth)
487 static Change current_change;
490 if (token == "\\begin_layout") {
491 lex.pushToken(token);
494 par.params().depth(depth);
495 if (params().tracking_changes)
497 LyXFont f(LyXFont::ALL_INHERIT, params().language);
501 if (pit != pars.end())
504 pit = pars.insert(pit, par);
506 // FIXME: goddamn InsetTabular makes us pass a Buffer
508 ::readParagraph(*this, *pit, lex);
510 } else if (token == "\\begin_deeper") {
512 } else if (token == "\\end_deeper") {
514 lex.printError("\\end_deeper: " "depth is already null");
525 // needed to insert the selection
526 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
527 LyXFont const & fn, string const & str)
529 LyXLayout_ptr const & layout = par->layout();
533 par->checkInsertChar(font);
534 // insert the string, don't insert doublespace
535 bool space_inserted = true;
536 bool autobreakrows = !par->inInset() ||
537 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
538 for(string::const_iterator cit = str.begin();
539 cit != str.end(); ++cit) {
541 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
542 breakParagraph(params(), paragraphs(), par, pos,
543 layout->isEnvironment());
546 space_inserted = true;
550 // do not insert consecutive spaces if !free_spacing
551 } else if ((*cit == ' ' || *cit == '\t') &&
552 space_inserted && !par->isFreeSpacing()) {
554 } else if (*cit == '\t') {
555 if (!par->isFreeSpacing()) {
556 // tabs are like spaces here
557 par->insertChar(pos, ' ', font);
559 space_inserted = true;
561 const pos_type nb = 8 - pos % 8;
562 for (pos_type a = 0; a < nb ; ++a) {
563 par->insertChar(pos, ' ', font);
566 space_inserted = true;
568 } else if (!IsPrintable(*cit)) {
569 // Ignore unprintables
572 // just insert the character
573 par->insertChar(pos, *cit, font);
575 space_inserted = (*cit == ' ');
582 bool Buffer::readFile(string const & filename)
584 // Check if the file is compressed.
585 string const format = getExtFromContents(filename);
586 if (format == "gzip" || format == "zip" || format == "compress") {
587 params().compressed = true;
590 bool ret = readFile(filename, paragraphs().begin());
592 // After we have read a file, we must ensure that the buffer
593 // language is set and used in the gui.
594 // If you know of a better place to put this, please tell me. (Lgb)
595 updateDocLang(params().language);
601 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
604 lex.setFile(filename);
606 return readFile(lex, filename, pit);
610 bool Buffer::fully_loaded() const
612 return pimpl_->file_fully_loaded;
616 void Buffer::fully_loaded(bool value)
618 pimpl_->file_fully_loaded = value;
622 bool Buffer::readFile(LyXLex & lex, string const & filename,
623 ParagraphList::iterator pit)
625 BOOST_ASSERT(!filename.empty());
628 Alert::error(_("Document could not be read"),
629 bformat(_("%1$s could not be read."), filename));
634 string const token(lex.getString());
637 Alert::error(_("Document could not be read"),
638 bformat(_("%1$s could not be read."), filename));
642 // the first token _must_ be...
643 if (token != "\\lyxformat") {
644 lyxerr << "Token: " << token << endl;
646 Alert::error(_("Document format failure"),
647 bformat(_("%1$s is not a LyX document."),
653 string tmp_format = lex.getString();
654 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
655 // if present remove ".," from string.
656 string::size_type dot = tmp_format.find_first_of(".,");
657 //lyxerr << " dot found at " << dot << endl;
658 if (dot != string::npos)
659 tmp_format.erase(dot, 1);
660 int file_format = strToInt(tmp_format);
661 //lyxerr << "format: " << file_format << endl;
663 if (file_format > LYX_FORMAT) {
664 Alert::warning(_("Document format failure"),
665 bformat(_("%1$s was created with a newer"
666 " version of LyX. This is likely to"
669 } else if (file_format < LYX_FORMAT) {
670 string const tmpfile = tempName();
671 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
672 if (command.empty()) {
673 Alert::error(_("Conversion script not found"),
674 bformat(_("%1$s is from an earlier"
675 " version of LyX, but the"
676 " conversion script lyx2lyx"
677 " could not be found."),
683 + " -o " + tmpfile + ' '
684 + QuoteName(filename);
685 lyxerr[Debug::INFO] << "Running '"
688 cmd_ret const ret = RunCommand(command);
689 if (ret.first != 0) {
690 Alert::error(_("Conversion script failed"),
691 bformat(_("%1$s is from an earlier version"
692 " of LyX, but the lyx2lyx script"
693 " failed to convert it."),
697 bool ret = readFile(tmpfile, pit);
698 // Do stuff with tmpfile name and buffer name here.
704 bool the_end = readBody(lex, pit);
705 params().setPaperStuff();
708 Alert::error(_("Document format failure"),
709 bformat(_("%1$s ended unexpectedly, which means"
710 " that it is probably corrupted."),
713 pimpl_->file_fully_loaded = true;
718 // Should probably be moved to somewhere else: BufferView? LyXView?
719 bool Buffer::save() const
721 // We don't need autosaves in the immediate future. (Asger)
722 resetAutosaveTimers();
726 if (lyxrc.make_backup) {
727 s = fileName() + '~';
728 if (!lyxrc.backupdir_path.empty())
729 s = AddName(lyxrc.backupdir_path,
730 subst(os::slashify_path(s),'/','!'));
732 // Rename is the wrong way of making a backup,
733 // this is the correct way.
734 /* truss cp fil fil2:
735 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
736 stat("LyXVC.lyx", 0xEFFFF688) = 0
737 open("LyXVC.lyx", O_RDONLY) = 3
738 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
739 fstat(4, 0xEFFFF508) = 0
740 fstat(3, 0xEFFFF508) = 0
741 read(3, " # T h i s f i l e w".., 8192) = 5579
742 write(4, " # T h i s f i l e w".., 5579) = 5579
743 read(3, 0xEFFFD4A0, 8192) = 0
746 chmod("LyXVC3.lyx", 0100644) = 0
747 lseek(0, 0, SEEK_CUR) = 46440
751 // Should probably have some more error checking here.
752 // Doing it this way, also makes the inodes stay the same.
753 // This is still not a very good solution, in particular we
754 // might loose the owner of the backup.
755 FileInfo finfo(fileName());
757 mode_t fmode = finfo.getMode();
758 struct utimbuf times = {
759 finfo.getAccessTime(),
760 finfo.getModificationTime() };
762 ifstream ifs(fileName().c_str());
763 ofstream ofs(s.c_str(), ios::out|ios::trunc);
768 ::chmod(s.c_str(), fmode);
770 if (::utime(s.c_str(), ×)) {
771 lyxerr << "utime error." << endl;
774 lyxerr << "LyX was not able to make "
775 "backup copy. Beware." << endl;
780 if (writeFile(fileName())) {
782 removeAutosaveFile(fileName());
784 // Saving failed, so backup is not backup
785 if (lyxrc.make_backup) {
786 rename(s, fileName());
794 bool Buffer::writeFile(string const & fname) const
796 if (pimpl_->read_only && (fname == fileName())) {
800 FileInfo finfo(fname);
801 if (finfo.exist() && !finfo.writable()) {
807 if (params().compressed) {
808 gz::ogzstream ofs(fname.c_str());
813 retval = do_writeFile(ofs);
816 ofstream ofs(fname.c_str());
820 retval = do_writeFile(ofs);
827 bool Buffer::do_writeFile(ostream & ofs) const
831 // Use the standard "C" locale for file output.
832 ofs.imbue(std::locale::classic());
835 // The top of the file should not be written by params().
837 // write out a comment in the top of the file
838 ofs << "#LyX " << lyx_version
839 << " created this file. For more info see http://www.lyx.org/\n"
840 << "\\lyxformat " << LYX_FORMAT << "\n";
842 // now write out the buffer paramters.
843 params().writeFile(ofs);
845 ofs << "\\end_header\n";
847 Paragraph::depth_type depth = 0;
849 // this will write out all the paragraphs
850 // using recursive descent.
851 ParagraphList::const_iterator pit = paragraphs().begin();
852 ParagraphList::const_iterator pend = paragraphs().end();
853 for (; pit != pend; ++pit)
854 pit->write(*this, ofs, params(), depth);
856 // Write marker that shows file is complete
857 ofs << "\n\\end_document" << endl;
859 // Shouldn't really be needed....
862 // how to check if close went ok?
863 // Following is an attempt... (BE 20001011)
865 // good() returns false if any error occured, including some
867 // bad() returns true if something bad happened in the buffer,
868 // which should include file system full errors.
875 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
877 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
887 void Buffer::makeLaTeXFile(string const & fname,
888 string const & original_path,
889 OutputParams const & runparams,
890 bool output_preamble, bool output_body)
892 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
895 if (!openFileWrite(ofs, fname))
898 makeLaTeXFile(ofs, original_path,
899 runparams, output_preamble, output_body);
903 lyxerr << "File was not closed properly." << endl;
908 void Buffer::makeLaTeXFile(ostream & os,
909 string const & original_path,
910 OutputParams const & runparams_in,
911 bool output_preamble, bool output_body)
913 OutputParams runparams = runparams_in;
914 niceFile() = runparams.nice; // this will be used by Insetincludes.
916 // validate the buffer.
917 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
918 LaTeXFeatures features(*this, params());
920 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
923 // The starting paragraph of the coming rows is the
924 // first paragraph of the document. (Asger)
925 texrow().start(paragraphs().begin()->id(), 0);
927 if (output_preamble && runparams.nice) {
928 os << "%% LyX " << lyx_version << " created this file. "
929 "For more info, see http://www.lyx.org/.\n"
930 "%% Do not edit unless you really know what "
935 lyxerr[Debug::INFO] << "lyx header finished" << endl;
936 // There are a few differences between nice LaTeX and usual files:
937 // usual is \batchmode and has a
938 // special input@path to allow the including of figures
939 // with either \input or \includegraphics (what figinsets do).
940 // input@path is set when the actual parameter
941 // original_path is set. This is done for usual tex-file, but not
942 // for nice-latex-file. (Matthias 250696)
943 if (output_preamble) {
944 if (!runparams.nice) {
945 // code for usual, NOT nice-latex-file
946 os << "\\batchmode\n"; // changed
950 if (!original_path.empty()) {
951 string inputpath = os::external_path(original_path);
952 subst(inputpath, "~", "\\string~");
953 os << "\\makeatletter\n"
954 << "\\def\\input@path{{"
955 << inputpath << "/}}\n"
956 << "\\makeatother\n";
962 // Write the preamble
963 runparams.use_babel = params().writeLaTeX(os, features, texrow());
969 os << "\\begin{document}\n";
972 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
974 if (!lyxrc.language_auto_begin) {
975 os << subst(lyxrc.language_command_begin, "$$lang",
976 params().language->babel())
981 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
983 // add this just in case after all the paragraphs
987 if (!lyxrc.language_auto_end) {
988 os << subst(lyxrc.language_command_end, "$$lang",
989 params().language->babel())
994 if (output_preamble) {
995 os << "\\end{document}\n";
998 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1000 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1004 // Just to be sure. (Asger)
1007 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1008 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1011 // we want this to be true outside previews (for insetexternal)
1016 bool Buffer::isLatex() const
1018 return params().getLyXTextClass().outputType() == LATEX;
1022 bool Buffer::isLinuxDoc() const
1024 return params().getLyXTextClass().outputType() == LINUXDOC;
1028 bool Buffer::isLiterate() const
1030 return params().getLyXTextClass().outputType() == LITERATE;
1034 bool Buffer::isDocBook() const
1036 return params().getLyXTextClass().outputType() == DOCBOOK;
1040 bool Buffer::isSGML() const
1042 LyXTextClass const & tclass = params().getLyXTextClass();
1044 return tclass.outputType() == LINUXDOC ||
1045 tclass.outputType() == DOCBOOK;
1049 void Buffer::makeLinuxDocFile(string const & fname,
1050 OutputParams const & runparams,
1054 if (!openFileWrite(ofs, fname))
1057 niceFile() = runparams.nice; // this will be used by included files.
1059 LaTeXFeatures features(*this, params());
1065 LyXTextClass const & tclass = params().getLyXTextClass();
1067 string top_element = tclass.latexname();
1070 ofs << tclass.class_header();
1072 string preamble = params().preamble;
1073 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1075 preamble += features.getIncludedFiles(name);
1076 preamble += features.getLyXSGMLEntities();
1078 if (!preamble.empty()) {
1079 ofs << " [ " << preamble << " ]";
1083 if (params().options.empty())
1084 sgml::openTag(ofs, 0, false, top_element);
1086 string top = top_element;
1088 top += params().options;
1089 sgml::openTag(ofs, 0, false, top);
1093 ofs << "<!-- LyX " << lyx_version
1094 << " created this file. For more info see http://www.lyx.org/"
1097 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1101 sgml::closeTag(ofs, 0, false, top_element);
1105 // How to check for successful close
1107 // we want this to be true outside previews (for insetexternal)
1112 void Buffer::makeDocBookFile(string const & fname,
1113 OutputParams const & runparams,
1117 if (!openFileWrite(ofs, fname))
1120 niceFile() = runparams.nice; // this will be used by Insetincludes.
1122 LaTeXFeatures features(*this, params());
1127 LyXTextClass const & tclass = params().getLyXTextClass();
1128 string top_element = tclass.latexname();
1131 ofs << subst(tclass.class_header(), "#", top_element);
1133 string preamble = params().preamble;
1134 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1136 preamble += features.getIncludedFiles(name);
1137 preamble += features.getLyXSGMLEntities();
1139 if (!preamble.empty()) {
1140 ofs << "\n [ " << preamble << " ]";
1145 string top = top_element;
1147 top += params().language->code();
1150 if (!params().options.empty()) {
1152 top += params().options;
1154 sgml::openTag(ofs, 0, false, top);
1156 ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1157 << "\n See http://www.lyx.org/ for more information -->\n";
1159 params().getLyXTextClass().counters().reset();
1160 docbookParagraphs(*this, paragraphs(), ofs, runparams);
1163 sgml::closeTag(ofs, 0, false, top_element);
1166 // How to check for successful close
1168 // we want this to be true outside previews (for insetexternal)
1173 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1174 // Other flags: -wall -v0 -x
1175 int Buffer::runChktex()
1179 // get LaTeX-Filename
1180 string const name = getLatexName();
1181 string path = filePath();
1183 string const org_path = path;
1184 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
1188 Path p(path); // path to LaTeX file
1189 message(_("Running chktex..."));
1191 // Generate the LaTeX file if neccessary
1192 OutputParams runparams;
1193 runparams.flavor = OutputParams::LATEX;
1194 runparams.nice = false;
1195 makeLaTeXFile(name, org_path, runparams);
1198 Chktex chktex(lyxrc.chktex_command, name, filePath());
1199 int res = chktex.run(terr); // run chktex
1202 Alert::error(_("chktex failure"),
1203 _("Could not run chktex successfully."));
1204 } else if (res > 0) {
1205 // Insert all errors as errors boxes
1206 bufferErrors(*this, terr);
1215 void Buffer::validate(LaTeXFeatures & features) const
1217 LyXTextClass const & tclass = params().getLyXTextClass();
1219 if (params().tracking_changes) {
1220 features.require("dvipost");
1221 features.require("color");
1224 // AMS Style is at document level
1225 if (params().use_amsmath == BufferParams::AMS_ON
1226 || tclass.provides(LyXTextClass::amsmath))
1227 features.require("amsmath");
1229 for_each(paragraphs().begin(), paragraphs().end(),
1230 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1232 // the bullet shapes are buffer level not paragraph level
1233 // so they are tested here
1234 for (int i = 0; i < 4; ++i) {
1235 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1236 int const font = params().user_defined_bullet(i).getFont();
1238 int const c = params()
1239 .user_defined_bullet(i)
1246 features.require("latexsym");
1248 } else if (font == 1) {
1249 features.require("amssymb");
1250 } else if ((font >= 2 && font <= 5)) {
1251 features.require("pifont");
1256 if (lyxerr.debugging(Debug::LATEX)) {
1257 features.showStruct();
1262 void Buffer::getLabelList(std::vector<string> & list) const
1264 /// if this is a child document and the parent is already loaded
1265 /// Use the parent's list instead [ale990407]
1266 if (!params().parentname.empty()
1267 && bufferlist.exists(params().parentname)) {
1268 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1270 tmp->getLabelList(list);
1275 for (inset_iterator it = inset_const_iterator_begin();
1276 it != inset_const_iterator_end(); ++it) {
1277 it->getLabelList(*this, list);
1282 // This is also a buffer property (ale)
1283 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
1285 /// if this is a child document and the parent is already loaded
1286 /// use the parent's list instead [ale990412]
1287 if (!params().parentname.empty() &&
1288 bufferlist.exists(params().parentname)) {
1289 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1291 tmp->fillWithBibKeys(keys);
1296 for (inset_iterator it = inset_const_iterator_begin();
1297 it != inset_const_iterator_end(); ++it) {
1298 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
1299 InsetBibtex const & inset =
1300 dynamic_cast<InsetBibtex const &>(*it);
1301 inset.fillWithBibKeys(*this, keys);
1302 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1303 InsetInclude const & inset =
1304 dynamic_cast<InsetInclude const &>(*it);
1305 inset.fillWithBibKeys(*this, keys);
1306 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
1307 InsetBibitem const & inset =
1308 dynamic_cast<InsetBibitem const &>(*it);
1309 string const key = inset.getContents();
1310 string const opt = inset.getOptions();
1311 string const ref; // = pit->asString(this, false);
1312 string const info = opt + "TheBibliographyRef" + ref;
1313 keys.push_back(pair<string, string>(key, info));
1319 bool Buffer::isDepClean(string const & name) const
1321 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
1322 if (it == pimpl_->dep_clean.end())
1328 void Buffer::markDepClean(string const & name)
1330 pimpl_->dep_clean[name] = true;
1334 bool Buffer::dispatch(string const & command, bool * result)
1336 return dispatch(lyxaction.lookupFunc(command), result);
1340 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1342 bool dispatched = true;
1344 switch (func.action) {
1346 bool const tmp = Exporter::Export(this, func.argument, false);
1359 void Buffer::changeLanguage(Language const * from, Language const * to)
1361 lyxerr << "Changing Language!" << endl;
1363 // Take care of l10n/i18n
1366 ParIterator end = par_iterator_end();
1367 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1368 it->changeLanguage(params(), from, to);
1372 void Buffer::updateDocLang(Language const * nlang)
1374 pimpl_->messages.reset(new Messages(nlang->code()));
1378 bool Buffer::isMultiLingual()
1380 ParIterator end = par_iterator_end();
1381 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1382 if (it->isMultiLingual(params()))
1389 void Buffer::inset_iterator::setParagraph()
1391 while (pit != pend) {
1392 it = pit->insetlist.begin();
1393 if (it != pit->insetlist.end())
1400 ParIterator Buffer::getParFromID(int id) const
1402 #warning FIXME: const correctness! (Andre)
1403 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
1404 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
1406 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
1408 // John says this is called with id == -1 from undo
1409 lyxerr << "getParFromID(), id: " << id << endl;
1413 for (; it != end; ++it)
1421 bool Buffer::hasParWithID(int id) const
1423 ParConstIterator it = par_iterator_begin();
1424 ParConstIterator end = par_iterator_end();
1427 // John says this is called with id == -1 from undo
1428 lyxerr << "hasParWithID(), id: " << id << endl;
1432 for (; it != end; ++it)
1440 PosIterator Buffer::pos_iterator_begin()
1442 return PosIterator(¶graphs(), paragraphs().begin(), 0);
1446 PosIterator Buffer::pos_iterator_end()
1448 return PosIterator(¶graphs(), paragraphs().end(), 0);
1452 ParIterator Buffer::par_iterator_begin()
1454 return ParIterator(paragraphs().begin(), paragraphs());
1458 ParIterator Buffer::par_iterator_end()
1460 return ParIterator(paragraphs().end(), paragraphs());
1464 ParConstIterator Buffer::par_iterator_begin() const
1466 ParagraphList const & pars = paragraphs();
1467 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
1471 ParConstIterator Buffer::par_iterator_end() const
1473 ParagraphList const & pars = paragraphs();
1474 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
1478 Language const * Buffer::getLanguage() const
1480 return params().language;
1484 string const Buffer::B_(string const & l10n) const
1486 if (pimpl_->messages.get()) {
1487 return pimpl_->messages->get(l10n);
1494 bool Buffer::isClean() const
1496 return pimpl_->lyx_clean;
1500 bool Buffer::isBakClean() const
1502 return pimpl_->bak_clean;
1506 void Buffer::markClean() const
1508 if (!pimpl_->lyx_clean) {
1509 pimpl_->lyx_clean = true;
1512 // if the .lyx file has been saved, we don't need an
1514 pimpl_->bak_clean = true;
1518 void Buffer::markBakClean()
1520 pimpl_->bak_clean = true;
1524 void Buffer::setUnnamed(bool flag)
1526 pimpl_->unnamed = flag;
1530 bool Buffer::isUnnamed()
1532 return pimpl_->unnamed;
1536 void Buffer::markDirty()
1538 if (pimpl_->lyx_clean) {
1539 pimpl_->lyx_clean = false;
1542 pimpl_->bak_clean = false;
1544 DepClean::iterator it = pimpl_->dep_clean.begin();
1545 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1547 for (; it != end; ++it) {
1553 string const & Buffer::fileName() const
1555 return pimpl_->filename;
1559 string const & Buffer::filePath() const
1561 return pimpl_->filepath;
1565 bool Buffer::isReadonly() const
1567 return pimpl_->read_only;
1571 void Buffer::setParentName(string const & name)
1573 params().parentname = name;
1577 Buffer::inset_iterator::inset_iterator()
1582 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
1589 Buffer::inset_iterator Buffer::inset_iterator_begin()
1591 return inset_iterator(paragraphs().begin(), paragraphs().end());
1595 Buffer::inset_iterator Buffer::inset_iterator_end()
1597 return inset_iterator(paragraphs().end(), paragraphs().end());
1601 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
1603 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1604 return inset_iterator(pars.begin(), pars.end());
1608 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
1610 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1611 return inset_iterator(pars.end(), pars.end());
1615 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
1619 if (it == pit->insetlist.end()) {
1628 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
1630 inset_iterator tmp = *this;
1636 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
1642 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
1648 ParagraphList::iterator Buffer::inset_iterator::getPar() const
1654 lyx::pos_type Buffer::inset_iterator::getPos() const
1660 bool operator==(Buffer::inset_iterator const & iter1,
1661 Buffer::inset_iterator const & iter2)
1663 return iter1.pit == iter2.pit
1664 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
1668 bool operator!=(Buffer::inset_iterator const & iter1,
1669 Buffer::inset_iterator const & iter2)
1671 return !(iter1 == iter2);