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"
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 "PosIterator.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"
70 #include <boost/bind.hpp>
72 #include "support/std_sstream.h"
84 using lyx::support::AddName;
85 using lyx::support::atoi;
86 using lyx::support::bformat;
87 using lyx::support::ChangeExtension;
88 using lyx::support::cmd_ret;
89 using lyx::support::createBufferTmpDir;
90 using lyx::support::destroyDir;
91 using lyx::support::FileInfo;
92 using lyx::support::FileInfo;
93 using lyx::support::getExtFromContents;
94 using lyx::support::IsDirWriteable;
95 using lyx::support::IsFileWriteable;
96 using lyx::support::LibFileSearch;
97 using lyx::support::ltrim;
98 using lyx::support::MakeAbsPath;
99 using lyx::support::MakeDisplayPath;
100 using lyx::support::MakeLatexName;
101 using lyx::support::OnlyFilename;
102 using lyx::support::OnlyPath;
103 using lyx::support::Path;
104 using lyx::support::QuoteName;
105 using lyx::support::removeAutosaveFile;
106 using lyx::support::rename;
107 using lyx::support::RunCommand;
108 using lyx::support::split;
109 using lyx::support::strToInt;
110 using lyx::support::subst;
111 using lyx::support::tempName;
112 using lyx::support::trim;
114 namespace os = lyx::support::os;
118 using std::make_pair;
123 using std::ostringstream;
131 // all these externs should eventually be removed.
132 extern BufferList bufferlist;
136 const int LYX_FORMAT = 229;
141 typedef std::map<string, bool> DepClean;
145 Impl(Buffer & parent, string const & file, bool readonly);
147 limited_stack<Undo> undostack;
148 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_)
189 : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
190 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
193 lyxvc.buffer(&parent);
194 temppath = createBufferTmpDir();
195 // FIXME: And now do something if temppath == string(), because we
196 // assume from now on that temppath points to a valid temp dir.
197 // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
201 Buffer::Buffer(string const & file, bool ronly)
202 : pimpl_(new Impl(*this, file, ronly))
204 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
210 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
211 // here the buffer should take care that it is
212 // saved properly, before it goes into the void.
216 if (!temppath().empty() && destroyDir(temppath()) != 0) {
217 Alert::warning(_("Could not remove temporary directory"),
218 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
221 // Remove any previewed LaTeX snippets associated with this buffer.
222 lyx::graphics::Previews::get().removeLoader(*this);
226 LyXText & Buffer::text() const
228 return const_cast<LyXText &>(pimpl_->text);
232 limited_stack<Undo> & Buffer::undostack()
234 return pimpl_->undostack;
238 limited_stack<Undo> const & Buffer::undostack() const
240 return pimpl_->undostack;
244 limited_stack<Undo> & Buffer::redostack()
246 return pimpl_->redostack;
250 limited_stack<Undo> const & Buffer::redostack() const
252 return pimpl_->redostack;
256 BufferParams & Buffer::params()
258 return pimpl_->params;
262 BufferParams const & Buffer::params() const
264 return pimpl_->params;
268 ParagraphList & Buffer::paragraphs()
270 return pimpl_->text.paragraphs();
274 ParagraphList const & Buffer::paragraphs() const
276 return pimpl_->text.paragraphs();
280 LyXVC & Buffer::lyxvc()
282 return pimpl_->lyxvc;
286 LyXVC const & Buffer::lyxvc() const
288 return pimpl_->lyxvc;
292 string const & Buffer::temppath() const
294 return pimpl_->temppath;
298 TexRow & Buffer::texrow()
300 return pimpl_->texrow;
304 TexRow const & Buffer::texrow() const
306 return pimpl_->texrow;
310 string const Buffer::getLatexName(bool no_path) const
312 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
313 return no_path ? OnlyFilename(name) : name;
317 pair<Buffer::LogType, string> const Buffer::getLogName() const
319 string const filename = getLatexName(false);
321 if (filename.empty())
322 return make_pair(Buffer::latexlog, string());
324 string const path = temppath();
326 string const fname = AddName(path,
327 OnlyFilename(ChangeExtension(filename,
330 AddName(path, OnlyFilename(
331 ChangeExtension(filename,
332 formats.extension("literate") + ".out")));
334 // If no Latex log or Build log is newer, show Build log
336 FileInfo const f_fi(fname);
337 FileInfo const b_fi(bname);
340 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
341 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
342 return make_pair(Buffer::buildlog, bname);
344 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
345 return make_pair(Buffer::latexlog, fname);
349 void Buffer::setReadonly(bool flag)
351 if (pimpl_->read_only != flag) {
352 pimpl_->read_only = flag;
358 void Buffer::setFileName(string const & newfile)
360 pimpl_->filename = MakeAbsPath(newfile);
361 pimpl_->filepath = OnlyPath(pimpl_->filename);
362 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
367 // We'll remove this later. (Lgb)
370 void unknownClass(string const & unknown)
372 Alert::warning(_("Unknown document class"),
373 bformat(_("Using the default document class, because the "
374 "class %1$s is unknown."), unknown));
380 int Buffer::readHeader(LyXLex & lex)
382 int unknown_tokens = 0;
386 string const token = lex.getString();
391 if (token == "\\end_header")
394 lyxerr[Debug::PARSER] << "Handling header token: `"
395 << token << '\'' << endl;
398 string unknown = params().readToken(lex, token);
399 if (!unknown.empty()) {
400 if (unknown[0] != '\\') {
401 unknownClass(unknown);
404 string const s = bformat(_("Unknown token: "
408 error(ErrorItem(_("Header error"), s,
413 return unknown_tokens;
417 // candidate for move to BufferView
418 // (at least some parts in the beginning of the func)
421 // changed to be public and have one parameter
422 // if par = 0 normal behavior
423 // else insert behavior
424 // Returns false if "\end_document" is not read (Asger)
425 bool Buffer::readBody(LyXLex & lex)
427 bool the_end_read = false;
429 if (paragraphs().empty()) {
431 if (!params().getLyXTextClass().load()) {
432 string theclass = params().getLyXTextClass().name();
433 Alert::error(_("Can't load document class"), bformat(
434 "Using the default document class, because the "
435 " class %1$s could not be loaded.", theclass));
436 params().textclass = 0;
439 // We don't want to adopt the parameters from the
440 // document we insert, so read them into a temporary buffer
441 // and then discard it
443 Buffer tmpbuf("", false);
444 tmpbuf.readHeader(lex);
447 if (text().read(*this, lex))
454 // needed to insert the selection
455 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
456 LyXFont const & fn, string const & str)
458 LyXLayout_ptr const & layout = par->layout();
462 par->checkInsertChar(font);
463 // insert the string, don't insert doublespace
464 bool space_inserted = true;
465 bool autobreakrows = !par->inInset() ||
466 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
467 for(string::const_iterator cit = str.begin();
468 cit != str.end(); ++cit) {
470 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
471 breakParagraph(params(), paragraphs(), par, pos,
472 layout->isEnvironment());
475 space_inserted = true;
479 // do not insert consecutive spaces if !free_spacing
480 } else if ((*cit == ' ' || *cit == '\t') &&
481 space_inserted && !par->isFreeSpacing()) {
483 } else if (*cit == '\t') {
484 if (!par->isFreeSpacing()) {
485 // tabs are like spaces here
486 par->insertChar(pos, ' ', font);
488 space_inserted = true;
490 const pos_type n = 8 - pos % 8;
491 for (pos_type i = 0; i < n; ++i) {
492 par->insertChar(pos, ' ', font);
495 space_inserted = true;
497 } else if (!IsPrintable(*cit)) {
498 // Ignore unprintables
501 // just insert the character
502 par->insertChar(pos, *cit, font);
504 space_inserted = (*cit == ' ');
511 bool Buffer::readFile(string const & filename)
513 // Check if the file is compressed.
514 string const format = getExtFromContents(filename);
515 if (format == "gzip" || format == "zip" || format == "compress") {
516 params().compressed = true;
519 bool ret = readFile(filename, paragraphs().begin());
521 // After we have read a file, we must ensure that the buffer
522 // language is set and used in the gui.
523 // If you know of a better place to put this, please tell me. (Lgb)
524 updateDocLang(params().language);
530 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
533 lex.setFile(filename);
534 return readFile(lex, filename, pit);
538 bool Buffer::fully_loaded() const
540 return pimpl_->file_fully_loaded;
544 void Buffer::fully_loaded(bool value)
546 pimpl_->file_fully_loaded = value;
550 bool Buffer::readFile(LyXLex & lex, string const & filename,
551 ParagraphList::iterator pit)
553 BOOST_ASSERT(!filename.empty());
556 Alert::error(_("Document could not be read"),
557 bformat(_("%1$s could not be read."), filename));
562 string const token(lex.getString());
565 Alert::error(_("Document could not be read"),
566 bformat(_("%1$s could not be read."), filename));
570 // the first token _must_ be...
571 if (token != "\\lyxformat") {
572 lyxerr << "Token: " << token << endl;
574 Alert::error(_("Document format failure"),
575 bformat(_("%1$s is not a LyX document."),
581 string tmp_format = lex.getString();
582 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
583 // if present remove ".," from string.
584 string::size_type dot = tmp_format.find_first_of(".,");
585 //lyxerr << " dot found at " << dot << endl;
586 if (dot != string::npos)
587 tmp_format.erase(dot, 1);
588 int file_format = strToInt(tmp_format);
589 //lyxerr << "format: " << file_format << endl;
591 if (file_format > LYX_FORMAT) {
592 Alert::warning(_("Document format failure"),
593 bformat(_("%1$s was created with a newer"
594 " version of LyX. This is likely to"
597 } else if (file_format < LYX_FORMAT) {
598 string const tmpfile = tempName();
599 if (tmpfile.empty()) {
600 Alert::error(_("Conversion failed"),
601 bformat(_("%1$s is from an earlier"
602 " version of LyX, but a temporary"
603 " file for converting it could"
608 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
609 if (command.empty()) {
610 Alert::error(_("Conversion script not found"),
611 bformat(_("%1$s is from an earlier"
612 " version of LyX, but the"
613 " conversion script lyx2lyx"
614 " could not be found."),
620 + " -o " + tmpfile + ' '
621 + QuoteName(filename);
622 lyxerr[Debug::INFO] << "Running '"
625 cmd_ret const ret = RunCommand(command);
626 if (ret.first != 0) {
627 Alert::error(_("Conversion script failed"),
628 bformat(_("%1$s is from an earlier version"
629 " of LyX, but the lyx2lyx script"
630 " failed to convert it."),
634 bool ret = readFile(tmpfile, pit);
635 // Do stuff with tmpfile name and buffer name here.
641 bool the_end = readBody(lex);
642 params().setPaperStuff();
645 Alert::error(_("Document format failure"),
646 bformat(_("%1$s ended unexpectedly, which means"
647 " that it is probably corrupted."),
650 pimpl_->file_fully_loaded = true;
655 // Should probably be moved to somewhere else: BufferView? LyXView?
656 bool Buffer::save() const
658 // We don't need autosaves in the immediate future. (Asger)
659 resetAutosaveTimers();
663 if (lyxrc.make_backup) {
664 s = fileName() + '~';
665 if (!lyxrc.backupdir_path.empty())
666 s = AddName(lyxrc.backupdir_path,
667 subst(os::slashify_path(s),'/','!'));
669 // Rename is the wrong way of making a backup,
670 // this is the correct way.
671 /* truss cp fil fil2:
672 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
673 stat("LyXVC.lyx", 0xEFFFF688) = 0
674 open("LyXVC.lyx", O_RDONLY) = 3
675 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
676 fstat(4, 0xEFFFF508) = 0
677 fstat(3, 0xEFFFF508) = 0
678 read(3, " # T h i s f i l e w".., 8192) = 5579
679 write(4, " # T h i s f i l e w".., 5579) = 5579
680 read(3, 0xEFFFD4A0, 8192) = 0
683 chmod("LyXVC3.lyx", 0100644) = 0
684 lseek(0, 0, SEEK_CUR) = 46440
688 // Should probably have some more error checking here.
689 // Doing it this way, also makes the inodes stay the same.
690 // This is still not a very good solution, in particular we
691 // might loose the owner of the backup.
692 FileInfo finfo(fileName());
694 mode_t fmode = finfo.getMode();
695 struct utimbuf times = {
696 finfo.getAccessTime(),
697 finfo.getModificationTime() };
699 ifstream ifs(fileName().c_str());
700 ofstream ofs(s.c_str(), ios::out|ios::trunc);
705 ::chmod(s.c_str(), fmode);
707 if (::utime(s.c_str(), ×)) {
708 lyxerr << "utime error." << endl;
711 lyxerr << "LyX was not able to make "
712 "backup copy. Beware." << endl;
717 if (writeFile(fileName())) {
719 removeAutosaveFile(fileName());
721 // Saving failed, so backup is not backup
722 if (lyxrc.make_backup)
723 rename(s, fileName());
730 bool Buffer::writeFile(string const & fname) const
732 if (pimpl_->read_only && fname == fileName())
735 FileInfo finfo(fname);
736 if (finfo.exist() && !finfo.writable())
741 if (params().compressed) {
742 gz::ogzstream ofs(fname.c_str());
746 retval = do_writeFile(ofs);
749 ofstream ofs(fname.c_str());
753 retval = do_writeFile(ofs);
760 bool Buffer::do_writeFile(ostream & ofs) const
763 // Use the standard "C" locale for file output.
764 ofs.imbue(std::locale::classic());
767 // The top of the file should not be written by params().
769 // write out a comment in the top of the file
770 ofs << "#LyX " << lyx_version
771 << " created this file. For more info see http://www.lyx.org/\n"
772 << "\\lyxformat " << LYX_FORMAT << "\n";
774 // now write out the buffer parameters.
775 params().writeFile(ofs);
777 ofs << "\\end_header\n";
780 text().write(*this, ofs);
782 // Write marker that shows file is complete
783 ofs << "\n\\end_document" << endl;
785 // Shouldn't really be needed....
788 // how to check if close went ok?
789 // Following is an attempt... (BE 20001011)
791 // good() returns false if any error occured, including some
793 // bad() returns true if something bad happened in the buffer,
794 // which should include file system full errors.
799 lyxerr << "File was not closed properly." << endl;
806 void Buffer::makeLaTeXFile(string const & fname,
807 string const & original_path,
808 OutputParams const & runparams,
809 bool output_preamble, bool output_body)
811 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
814 if (!openFileWrite(ofs, fname))
817 makeLaTeXFile(ofs, original_path,
818 runparams, output_preamble, output_body);
822 lyxerr << "File '" << fname << "' was not closed properly." << endl;
826 void Buffer::makeLaTeXFile(ostream & os,
827 string const & original_path,
828 OutputParams const & runparams_in,
829 bool output_preamble, bool output_body)
831 OutputParams runparams = runparams_in;
833 // validate the buffer.
834 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
835 LaTeXFeatures features(*this, params(), runparams.nice);
837 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
840 // The starting paragraph of the coming rows is the
841 // first paragraph of the document. (Asger)
842 texrow().start(paragraphs().begin()->id(), 0);
844 if (output_preamble && runparams.nice) {
845 os << "%% LyX " << lyx_version << " created this file. "
846 "For more info, see http://www.lyx.org/.\n"
847 "%% Do not edit unless you really know what "
852 lyxerr[Debug::INFO] << "lyx header finished" << endl;
853 // There are a few differences between nice LaTeX and usual files:
854 // usual is \batchmode and has a
855 // special input@path to allow the including of figures
856 // with either \input or \includegraphics (what figinsets do).
857 // input@path is set when the actual parameter
858 // original_path is set. This is done for usual tex-file, but not
859 // for nice-latex-file. (Matthias 250696)
860 if (output_preamble) {
861 if (!runparams.nice) {
862 // code for usual, NOT nice-latex-file
863 os << "\\batchmode\n"; // changed
867 if (!original_path.empty()) {
868 string inputpath = os::external_path(original_path);
869 subst(inputpath, "~", "\\string~");
870 os << "\\makeatletter\n"
871 << "\\def\\input@path{{"
872 << inputpath << "/}}\n"
873 << "\\makeatother\n";
879 // Write the preamble
880 runparams.use_babel = params().writeLaTeX(os, features, texrow());
886 os << "\\begin{document}\n";
889 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
891 if (!lyxrc.language_auto_begin) {
892 os << subst(lyxrc.language_command_begin, "$$lang",
893 params().language->babel())
898 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
900 // add this just in case after all the paragraphs
904 if (!lyxrc.language_auto_end) {
905 os << subst(lyxrc.language_command_end, "$$lang",
906 params().language->babel())
911 if (output_preamble) {
912 os << "\\end{document}\n";
915 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
917 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
921 // Just to be sure. (Asger)
924 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
925 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
930 bool Buffer::isLatex() const
932 return params().getLyXTextClass().outputType() == LATEX;
936 bool Buffer::isLinuxDoc() const
938 return params().getLyXTextClass().outputType() == LINUXDOC;
942 bool Buffer::isLiterate() const
944 return params().getLyXTextClass().outputType() == LITERATE;
948 bool Buffer::isDocBook() const
950 return params().getLyXTextClass().outputType() == DOCBOOK;
954 bool Buffer::isSGML() const
956 LyXTextClass const & tclass = params().getLyXTextClass();
958 return tclass.outputType() == LINUXDOC ||
959 tclass.outputType() == DOCBOOK;
963 void Buffer::makeLinuxDocFile(string const & fname,
964 OutputParams const & runparams,
968 if (!openFileWrite(ofs, fname))
971 LaTeXFeatures features(*this, params(), runparams.nice);
976 LyXTextClass const & tclass = params().getLyXTextClass();
978 string top_element = tclass.latexname();
981 ofs << tclass.class_header();
983 string preamble = params().preamble;
984 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
986 preamble += features.getIncludedFiles(name);
987 preamble += features.getLyXSGMLEntities();
989 if (!preamble.empty()) {
990 ofs << " [ " << preamble << " ]";
994 if (params().options.empty())
995 sgml::openTag(ofs, 0, false, top_element);
997 string top = top_element;
999 top += params().options;
1000 sgml::openTag(ofs, 0, false, top);
1004 ofs << "<!-- LyX " << lyx_version
1005 << " created this file. For more info see http://www.lyx.org/"
1008 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1012 sgml::closeTag(ofs, 0, false, top_element);
1017 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1021 void Buffer::makeDocBookFile(string const & fname,
1022 OutputParams const & runparams,
1026 if (!openFileWrite(ofs, fname))
1029 LaTeXFeatures features(*this, params(), runparams.nice);
1034 LyXTextClass const & tclass = params().getLyXTextClass();
1035 string top_element = tclass.latexname();
1038 ofs << subst(tclass.class_header(), "#", top_element);
1040 string preamble = params().preamble;
1041 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1043 preamble += features.getIncludedFiles(name);
1044 preamble += features.getLyXSGMLEntities();
1046 if (!preamble.empty()) {
1047 ofs << "\n [ " << preamble << " ]";
1052 string top = top_element;
1054 top += params().language->code();
1057 if (!params().options.empty()) {
1059 top += params().options;
1061 sgml::openTag(ofs, 0, false, top);
1063 ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1064 << "\n See http://www.lyx.org/ for more information -->\n";
1066 params().getLyXTextClass().counters().reset();
1067 docbookParagraphs(*this, paragraphs(), ofs, runparams);
1070 sgml::closeTag(ofs, 0, false, top_element);
1074 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1078 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1079 // Other flags: -wall -v0 -x
1080 int Buffer::runChktex()
1084 // get LaTeX-Filename
1085 string const name = getLatexName();
1086 string const path = temppath();
1087 string const org_path = filePath();
1089 Path p(path); // path to LaTeX file
1090 message(_("Running chktex..."));
1092 // Generate the LaTeX file if neccessary
1093 OutputParams runparams;
1094 runparams.flavor = OutputParams::LATEX;
1095 runparams.nice = false;
1096 makeLaTeXFile(name, org_path, runparams);
1099 Chktex chktex(lyxrc.chktex_command, name, filePath());
1100 int res = chktex.run(terr); // run chktex
1103 Alert::error(_("chktex failure"),
1104 _("Could not run chktex successfully."));
1105 } else if (res > 0) {
1106 // Insert all errors as errors boxes
1107 bufferErrors(*this, terr);
1116 void Buffer::validate(LaTeXFeatures & features) const
1118 LyXTextClass const & tclass = params().getLyXTextClass();
1120 if (params().tracking_changes) {
1121 features.require("dvipost");
1122 features.require("color");
1125 // AMS Style is at document level
1126 if (params().use_amsmath == BufferParams::AMS_ON
1127 || tclass.provides(LyXTextClass::amsmath))
1128 features.require("amsmath");
1130 for_each(paragraphs().begin(), paragraphs().end(),
1131 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1133 // the bullet shapes are buffer level not paragraph level
1134 // so they are tested here
1135 for (int i = 0; i < 4; ++i) {
1136 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1137 int const font = params().user_defined_bullet(i).getFont();
1139 int const c = params()
1140 .user_defined_bullet(i)
1147 features.require("latexsym");
1149 } else if (font == 1) {
1150 features.require("amssymb");
1151 } else if ((font >= 2 && font <= 5)) {
1152 features.require("pifont");
1157 if (lyxerr.debugging(Debug::LATEX)) {
1158 features.showStruct();
1163 void Buffer::getLabelList(std::vector<string> & list) const
1165 /// if this is a child document and the parent is already loaded
1166 /// Use the parent's list instead [ale990407]
1167 if (!params().parentname.empty()
1168 && bufferlist.exists(params().parentname)) {
1169 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1171 tmp->getLabelList(list);
1176 for (inset_iterator it = inset_const_iterator_begin();
1177 it != inset_const_iterator_end(); ++it) {
1178 it->getLabelList(*this, list);
1183 // This is also a buffer property (ale)
1184 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys)
1187 /// if this is a child document and the parent is already loaded
1188 /// use the parent's list instead [ale990412]
1189 if (!params().parentname.empty() &&
1190 bufferlist.exists(params().parentname)) {
1191 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1193 tmp->fillWithBibKeys(keys);
1198 for (inset_iterator it = inset_const_iterator_begin();
1199 it != inset_const_iterator_end(); ++it) {
1200 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
1201 InsetBibtex const & inset =
1202 dynamic_cast<InsetBibtex const &>(*it);
1203 inset.fillWithBibKeys(*this, keys);
1204 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1205 InsetInclude const & inset =
1206 dynamic_cast<InsetInclude const &>(*it);
1207 inset.fillWithBibKeys(*this, keys);
1208 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
1209 InsetBibitem const & inset =
1210 dynamic_cast<InsetBibitem const &>(*it);
1211 string const key = inset.getContents();
1212 string const opt = inset.getOptions();
1213 string const ref; // = pit->asString(this, false);
1214 string const info = opt + "TheBibliographyRef" + ref;
1215 keys.push_back(pair<string, string>(key, info));
1221 bool Buffer::isDepClean(string const & name) const
1223 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
1224 if (it == pimpl_->dep_clean.end())
1230 void Buffer::markDepClean(string const & name)
1232 pimpl_->dep_clean[name] = true;
1236 bool Buffer::dispatch(string const & command, bool * result)
1238 return dispatch(lyxaction.lookupFunc(command), result);
1242 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1244 bool dispatched = true;
1246 switch (func.action) {
1248 bool const tmp = Exporter::Export(this, func.argument, false);
1261 void Buffer::changeLanguage(Language const * from, Language const * to)
1263 lyxerr << "Changing Language!" << endl;
1265 // Take care of l10n/i18n
1268 ParIterator end = par_iterator_end();
1269 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1270 it->changeLanguage(params(), from, to);
1274 void Buffer::updateDocLang(Language const * nlang)
1276 pimpl_->messages.reset(new Messages(nlang->code()));
1280 bool Buffer::isMultiLingual() const
1282 ParConstIterator end = par_iterator_end();
1283 for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1284 if (it->isMultiLingual(params()))
1291 void Buffer::inset_iterator::setParagraph()
1293 while (pit != pend) {
1294 it = pit->insetlist.begin();
1295 if (it != pit->insetlist.end())
1302 ParIterator Buffer::getParFromID(int id) const
1304 #warning FIXME: const correctness! (Andre)
1305 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
1306 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
1308 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
1310 // John says this is called with id == -1 from undo
1311 lyxerr << "getParFromID(), id: " << id << endl;
1315 for (; it != end; ++it)
1323 bool Buffer::hasParWithID(int id) const
1325 ParConstIterator it = par_iterator_begin();
1326 ParConstIterator end = par_iterator_end();
1329 // John says this is called with id == -1 from undo
1330 lyxerr << "hasParWithID(), id: " << id << endl;
1334 for (; it != end; ++it)
1342 PosIterator Buffer::pos_iterator_begin()
1344 return PosIterator(¶graphs(), paragraphs().begin(), 0);
1348 PosIterator Buffer::pos_iterator_end()
1350 return PosIterator(¶graphs(), paragraphs().end(), 0);
1354 ParIterator Buffer::par_iterator_begin()
1356 return ParIterator(paragraphs().begin(), paragraphs());
1360 ParIterator Buffer::par_iterator_end()
1362 return ParIterator(paragraphs().end(), paragraphs());
1366 ParConstIterator Buffer::par_iterator_begin() const
1368 ParagraphList const & pars = paragraphs();
1369 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
1373 ParConstIterator Buffer::par_iterator_end() const
1375 ParagraphList const & pars = paragraphs();
1376 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
1380 Language const * Buffer::getLanguage() const
1382 return params().language;
1386 string const Buffer::B_(string const & l10n) const
1388 if (pimpl_->messages.get()) {
1389 return pimpl_->messages->get(l10n);
1396 bool Buffer::isClean() const
1398 return pimpl_->lyx_clean;
1402 bool Buffer::isBakClean() const
1404 return pimpl_->bak_clean;
1408 void Buffer::markClean() const
1410 if (!pimpl_->lyx_clean) {
1411 pimpl_->lyx_clean = true;
1414 // if the .lyx file has been saved, we don't need an
1416 pimpl_->bak_clean = true;
1420 void Buffer::markBakClean()
1422 pimpl_->bak_clean = true;
1426 void Buffer::setUnnamed(bool flag)
1428 pimpl_->unnamed = flag;
1432 bool Buffer::isUnnamed() const
1434 return pimpl_->unnamed;
1438 void Buffer::markDirty()
1440 if (pimpl_->lyx_clean) {
1441 pimpl_->lyx_clean = false;
1444 pimpl_->bak_clean = false;
1446 DepClean::iterator it = pimpl_->dep_clean.begin();
1447 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1449 for (; it != end; ++it) {
1455 string const & Buffer::fileName() const
1457 return pimpl_->filename;
1461 string const & Buffer::filePath() const
1463 return pimpl_->filepath;
1467 bool Buffer::isReadonly() const
1469 return pimpl_->read_only;
1473 void Buffer::setParentName(string const & name)
1475 params().parentname = name;
1479 Buffer::inset_iterator::inset_iterator()
1484 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
1491 Buffer::inset_iterator Buffer::inset_iterator_begin()
1493 return inset_iterator(paragraphs().begin(), paragraphs().end());
1497 Buffer::inset_iterator Buffer::inset_iterator_end()
1499 return inset_iterator(paragraphs().end(), paragraphs().end());
1503 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
1505 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1506 return inset_iterator(pars.begin(), pars.end());
1510 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
1512 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1513 return inset_iterator(pars.end(), pars.end());
1517 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
1521 if (it == pit->insetlist.end()) {
1530 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
1532 inset_iterator tmp = *this;
1538 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
1544 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
1550 ParagraphList::iterator Buffer::inset_iterator::getPar() const
1556 lyx::pos_type Buffer::inset_iterator::getPos() const
1562 bool operator==(Buffer::inset_iterator const & iter1,
1563 Buffer::inset_iterator const & iter2)
1565 return iter1.pit == iter2.pit
1566 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
1570 bool operator!=(Buffer::inset_iterator const & iter1,
1571 Buffer::inset_iterator const & iter2)
1573 return !(iter1 == iter2);