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 = 229;
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;
153 /// need to regenerate .tex ?
157 mutable bool lyx_clean;
159 /// is autosave needed
160 mutable bool bak_clean;
162 /// is this a unnamed file (New...)
168 /// name of the file the buffer is associated with.
171 /// The path to the document file.
174 boost::scoped_ptr<Messages> messages;
176 /** set to true only when the file is fully loaded.
177 * Used to prevent the premature generation of previews
178 * and by the citation inset.
180 bool file_fully_loaded;
187 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
188 : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
189 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
192 lyxvc.buffer(&parent);
193 if (readonly_ || lyxrc.use_tempdir)
194 temppath = CreateBufferTmpDir();
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_->text);
229 limited_stack<Undo> & Buffer::undostack()
231 return pimpl_->undostack;
235 limited_stack<Undo> const & Buffer::undostack() const
237 return pimpl_->undostack;
241 limited_stack<Undo> & Buffer::redostack()
243 return pimpl_->redostack;
247 limited_stack<Undo> const & Buffer::redostack() const
249 return pimpl_->redostack;
253 BufferParams & Buffer::params()
255 return pimpl_->params;
259 BufferParams const & Buffer::params() const
261 return pimpl_->params;
265 ParagraphList & Buffer::paragraphs()
267 return pimpl_->text.paragraphs();
271 ParagraphList const & Buffer::paragraphs() const
273 return pimpl_->text.paragraphs();
277 LyXVC & Buffer::lyxvc()
279 return pimpl_->lyxvc;
283 LyXVC const & Buffer::lyxvc() const
285 return pimpl_->lyxvc;
289 string const & Buffer::temppath() const
291 return pimpl_->temppath;
295 TexRow & Buffer::texrow()
297 return pimpl_->texrow;
301 TexRow const & Buffer::texrow() const
303 return pimpl_->texrow;
307 string const Buffer::getLatexName(bool no_path) const
309 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
310 return no_path ? OnlyFilename(name) : name;
314 pair<Buffer::LogType, string> const Buffer::getLogName() const
316 string const filename = getLatexName(false);
318 if (filename.empty())
319 return make_pair(Buffer::latexlog, string());
321 string path = OnlyPath(filename);
323 if (lyxrc.use_tempdir || !IsDirWriteable(path))
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 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
600 if (command.empty()) {
601 Alert::error(_("Conversion script not found"),
602 bformat(_("%1$s is from an earlier"
603 " version of LyX, but the"
604 " conversion script lyx2lyx"
605 " could not be found."),
611 + " -o " + tmpfile + ' '
612 + QuoteName(filename);
613 lyxerr[Debug::INFO] << "Running '"
616 cmd_ret const ret = RunCommand(command);
617 if (ret.first != 0) {
618 Alert::error(_("Conversion script failed"),
619 bformat(_("%1$s is from an earlier version"
620 " of LyX, but the lyx2lyx script"
621 " failed to convert it."),
625 bool ret = readFile(tmpfile, pit);
626 // Do stuff with tmpfile name and buffer name here.
632 bool the_end = readBody(lex);
633 params().setPaperStuff();
636 Alert::error(_("Document format failure"),
637 bformat(_("%1$s ended unexpectedly, which means"
638 " that it is probably corrupted."),
641 pimpl_->file_fully_loaded = true;
646 // Should probably be moved to somewhere else: BufferView? LyXView?
647 bool Buffer::save() const
649 // We don't need autosaves in the immediate future. (Asger)
650 resetAutosaveTimers();
654 if (lyxrc.make_backup) {
655 s = fileName() + '~';
656 if (!lyxrc.backupdir_path.empty())
657 s = AddName(lyxrc.backupdir_path,
658 subst(os::slashify_path(s),'/','!'));
660 // Rename is the wrong way of making a backup,
661 // this is the correct way.
662 /* truss cp fil fil2:
663 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
664 stat("LyXVC.lyx", 0xEFFFF688) = 0
665 open("LyXVC.lyx", O_RDONLY) = 3
666 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
667 fstat(4, 0xEFFFF508) = 0
668 fstat(3, 0xEFFFF508) = 0
669 read(3, " # T h i s f i l e w".., 8192) = 5579
670 write(4, " # T h i s f i l e w".., 5579) = 5579
671 read(3, 0xEFFFD4A0, 8192) = 0
674 chmod("LyXVC3.lyx", 0100644) = 0
675 lseek(0, 0, SEEK_CUR) = 46440
679 // Should probably have some more error checking here.
680 // Doing it this way, also makes the inodes stay the same.
681 // This is still not a very good solution, in particular we
682 // might loose the owner of the backup.
683 FileInfo finfo(fileName());
685 mode_t fmode = finfo.getMode();
686 struct utimbuf times = {
687 finfo.getAccessTime(),
688 finfo.getModificationTime() };
690 ifstream ifs(fileName().c_str());
691 ofstream ofs(s.c_str(), ios::out|ios::trunc);
696 ::chmod(s.c_str(), fmode);
698 if (::utime(s.c_str(), ×)) {
699 lyxerr << "utime error." << endl;
702 lyxerr << "LyX was not able to make "
703 "backup copy. Beware." << endl;
708 if (writeFile(fileName())) {
710 removeAutosaveFile(fileName());
712 // Saving failed, so backup is not backup
713 if (lyxrc.make_backup)
714 rename(s, fileName());
721 bool Buffer::writeFile(string const & fname) const
723 if (pimpl_->read_only && fname == fileName())
726 FileInfo finfo(fname);
727 if (finfo.exist() && !finfo.writable())
732 if (params().compressed) {
733 gz::ogzstream ofs(fname.c_str());
737 retval = do_writeFile(ofs);
740 ofstream ofs(fname.c_str());
744 retval = do_writeFile(ofs);
751 bool Buffer::do_writeFile(ostream & ofs) const
754 // Use the standard "C" locale for file output.
755 ofs.imbue(std::locale::classic());
758 // The top of the file should not be written by params().
760 // write out a comment in the top of the file
761 ofs << "#LyX " << lyx_version
762 << " created this file. For more info see http://www.lyx.org/\n"
763 << "\\lyxformat " << LYX_FORMAT << "\n";
765 // now write out the buffer parameters.
766 params().writeFile(ofs);
768 ofs << "\\end_header\n";
771 text().write(*this, ofs);
773 // Write marker that shows file is complete
774 ofs << "\n\\end_document" << endl;
776 // Shouldn't really be needed....
779 // how to check if close went ok?
780 // Following is an attempt... (BE 20001011)
782 // good() returns false if any error occured, including some
784 // bad() returns true if something bad happened in the buffer,
785 // which should include file system full errors.
790 lyxerr << "File was not closed properly." << endl;
797 void Buffer::makeLaTeXFile(string const & fname,
798 string const & original_path,
799 OutputParams const & runparams,
800 bool output_preamble, bool output_body)
802 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
805 if (!openFileWrite(ofs, fname))
808 makeLaTeXFile(ofs, original_path,
809 runparams, output_preamble, output_body);
813 lyxerr << "File '" << fname << "' was not closed properly." << endl;
817 void Buffer::makeLaTeXFile(ostream & os,
818 string const & original_path,
819 OutputParams const & runparams_in,
820 bool output_preamble, bool output_body)
822 OutputParams runparams = runparams_in;
824 // validate the buffer.
825 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
826 LaTeXFeatures features(*this, params(), runparams.nice);
828 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
831 // The starting paragraph of the coming rows is the
832 // first paragraph of the document. (Asger)
833 texrow().start(paragraphs().begin()->id(), 0);
835 if (output_preamble && runparams.nice) {
836 os << "%% LyX " << lyx_version << " created this file. "
837 "For more info, see http://www.lyx.org/.\n"
838 "%% Do not edit unless you really know what "
843 lyxerr[Debug::INFO] << "lyx header finished" << endl;
844 // There are a few differences between nice LaTeX and usual files:
845 // usual is \batchmode and has a
846 // special input@path to allow the including of figures
847 // with either \input or \includegraphics (what figinsets do).
848 // input@path is set when the actual parameter
849 // original_path is set. This is done for usual tex-file, but not
850 // for nice-latex-file. (Matthias 250696)
851 if (output_preamble) {
852 if (!runparams.nice) {
853 // code for usual, NOT nice-latex-file
854 os << "\\batchmode\n"; // changed
858 if (!original_path.empty()) {
859 string inputpath = os::external_path(original_path);
860 subst(inputpath, "~", "\\string~");
861 os << "\\makeatletter\n"
862 << "\\def\\input@path{{"
863 << inputpath << "/}}\n"
864 << "\\makeatother\n";
870 // Write the preamble
871 runparams.use_babel = params().writeLaTeX(os, features, texrow());
877 os << "\\begin{document}\n";
880 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
882 if (!lyxrc.language_auto_begin) {
883 os << subst(lyxrc.language_command_begin, "$$lang",
884 params().language->babel())
889 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
891 // add this just in case after all the paragraphs
895 if (!lyxrc.language_auto_end) {
896 os << subst(lyxrc.language_command_end, "$$lang",
897 params().language->babel())
902 if (output_preamble) {
903 os << "\\end{document}\n";
906 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
908 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
912 // Just to be sure. (Asger)
915 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
916 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
921 bool Buffer::isLatex() const
923 return params().getLyXTextClass().outputType() == LATEX;
927 bool Buffer::isLinuxDoc() const
929 return params().getLyXTextClass().outputType() == LINUXDOC;
933 bool Buffer::isLiterate() const
935 return params().getLyXTextClass().outputType() == LITERATE;
939 bool Buffer::isDocBook() const
941 return params().getLyXTextClass().outputType() == DOCBOOK;
945 bool Buffer::isSGML() const
947 LyXTextClass const & tclass = params().getLyXTextClass();
949 return tclass.outputType() == LINUXDOC ||
950 tclass.outputType() == DOCBOOK;
954 void Buffer::makeLinuxDocFile(string const & fname,
955 OutputParams const & runparams,
959 if (!openFileWrite(ofs, fname))
962 LaTeXFeatures features(*this, params(), runparams.nice);
967 LyXTextClass const & tclass = params().getLyXTextClass();
969 string top_element = tclass.latexname();
972 ofs << tclass.class_header();
974 string preamble = params().preamble;
975 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
977 preamble += features.getIncludedFiles(name);
978 preamble += features.getLyXSGMLEntities();
980 if (!preamble.empty()) {
981 ofs << " [ " << preamble << " ]";
985 if (params().options.empty())
986 sgml::openTag(ofs, 0, false, top_element);
988 string top = top_element;
990 top += params().options;
991 sgml::openTag(ofs, 0, false, top);
995 ofs << "<!-- LyX " << lyx_version
996 << " created this file. For more info see http://www.lyx.org/"
999 linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1003 sgml::closeTag(ofs, 0, false, top_element);
1008 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1012 void Buffer::makeDocBookFile(string const & fname,
1013 OutputParams const & runparams,
1017 if (!openFileWrite(ofs, fname))
1020 LaTeXFeatures features(*this, params(), runparams.nice);
1025 LyXTextClass const & tclass = params().getLyXTextClass();
1026 string top_element = tclass.latexname();
1029 ofs << subst(tclass.class_header(), "#", top_element);
1031 string preamble = params().preamble;
1032 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1034 preamble += features.getIncludedFiles(name);
1035 preamble += features.getLyXSGMLEntities();
1037 if (!preamble.empty()) {
1038 ofs << "\n [ " << preamble << " ]";
1043 string top = top_element;
1045 top += params().language->code();
1048 if (!params().options.empty()) {
1050 top += params().options;
1052 sgml::openTag(ofs, 0, false, top);
1054 ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1055 << "\n See http://www.lyx.org/ for more information -->\n";
1057 params().getLyXTextClass().counters().reset();
1058 docbookParagraphs(*this, paragraphs(), ofs, runparams);
1061 sgml::closeTag(ofs, 0, false, top_element);
1065 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1069 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1070 // Other flags: -wall -v0 -x
1071 int Buffer::runChktex()
1075 // get LaTeX-Filename
1076 string const name = getLatexName();
1077 string path = filePath();
1079 string const org_path = path;
1080 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
1084 Path p(path); // path to LaTeX file
1085 message(_("Running chktex..."));
1087 // Generate the LaTeX file if neccessary
1088 OutputParams runparams;
1089 runparams.flavor = OutputParams::LATEX;
1090 runparams.nice = false;
1091 makeLaTeXFile(name, org_path, runparams);
1094 Chktex chktex(lyxrc.chktex_command, name, filePath());
1095 int res = chktex.run(terr); // run chktex
1098 Alert::error(_("chktex failure"),
1099 _("Could not run chktex successfully."));
1100 } else if (res > 0) {
1101 // Insert all errors as errors boxes
1102 bufferErrors(*this, terr);
1111 void Buffer::validate(LaTeXFeatures & features) const
1113 LyXTextClass const & tclass = params().getLyXTextClass();
1115 if (params().tracking_changes) {
1116 features.require("dvipost");
1117 features.require("color");
1120 // AMS Style is at document level
1121 if (params().use_amsmath == BufferParams::AMS_ON
1122 || tclass.provides(LyXTextClass::amsmath))
1123 features.require("amsmath");
1125 for_each(paragraphs().begin(), paragraphs().end(),
1126 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1128 // the bullet shapes are buffer level not paragraph level
1129 // so they are tested here
1130 for (int i = 0; i < 4; ++i) {
1131 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1132 int const font = params().user_defined_bullet(i).getFont();
1134 int const c = params()
1135 .user_defined_bullet(i)
1142 features.require("latexsym");
1144 } else if (font == 1) {
1145 features.require("amssymb");
1146 } else if ((font >= 2 && font <= 5)) {
1147 features.require("pifont");
1152 if (lyxerr.debugging(Debug::LATEX)) {
1153 features.showStruct();
1158 void Buffer::getLabelList(std::vector<string> & list) const
1160 /// if this is a child document and the parent is already loaded
1161 /// Use the parent's list instead [ale990407]
1162 if (!params().parentname.empty()
1163 && bufferlist.exists(params().parentname)) {
1164 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1166 tmp->getLabelList(list);
1171 for (inset_iterator it = inset_const_iterator_begin();
1172 it != inset_const_iterator_end(); ++it) {
1173 it->getLabelList(*this, list);
1178 // This is also a buffer property (ale)
1179 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys)
1182 /// if this is a child document and the parent is already loaded
1183 /// use the parent's list instead [ale990412]
1184 if (!params().parentname.empty() &&
1185 bufferlist.exists(params().parentname)) {
1186 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1188 tmp->fillWithBibKeys(keys);
1193 for (inset_iterator it = inset_const_iterator_begin();
1194 it != inset_const_iterator_end(); ++it) {
1195 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
1196 InsetBibtex const & inset =
1197 dynamic_cast<InsetBibtex const &>(*it);
1198 inset.fillWithBibKeys(*this, keys);
1199 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1200 InsetInclude const & inset =
1201 dynamic_cast<InsetInclude const &>(*it);
1202 inset.fillWithBibKeys(*this, keys);
1203 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
1204 InsetBibitem const & inset =
1205 dynamic_cast<InsetBibitem const &>(*it);
1206 string const key = inset.getContents();
1207 string const opt = inset.getOptions();
1208 string const ref; // = pit->asString(this, false);
1209 string const info = opt + "TheBibliographyRef" + ref;
1210 keys.push_back(pair<string, string>(key, info));
1216 bool Buffer::isDepClean(string const & name) const
1218 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
1219 if (it == pimpl_->dep_clean.end())
1225 void Buffer::markDepClean(string const & name)
1227 pimpl_->dep_clean[name] = true;
1231 bool Buffer::dispatch(string const & command, bool * result)
1233 return dispatch(lyxaction.lookupFunc(command), result);
1237 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1239 bool dispatched = true;
1241 switch (func.action) {
1243 bool const tmp = Exporter::Export(this, func.argument, false);
1256 void Buffer::changeLanguage(Language const * from, Language const * to)
1258 lyxerr << "Changing Language!" << endl;
1260 // Take care of l10n/i18n
1263 ParIterator end = par_iterator_end();
1264 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1265 it->changeLanguage(params(), from, to);
1269 void Buffer::updateDocLang(Language const * nlang)
1271 pimpl_->messages.reset(new Messages(nlang->code()));
1275 bool Buffer::isMultiLingual()
1277 ParIterator end = par_iterator_end();
1278 for (ParIterator it = par_iterator_begin(); it != end; ++it)
1279 if (it->isMultiLingual(params()))
1286 void Buffer::inset_iterator::setParagraph()
1288 while (pit != pend) {
1289 it = pit->insetlist.begin();
1290 if (it != pit->insetlist.end())
1297 ParIterator Buffer::getParFromID(int id) const
1299 #warning FIXME: const correctness! (Andre)
1300 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
1301 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
1303 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
1305 // John says this is called with id == -1 from undo
1306 lyxerr << "getParFromID(), id: " << id << endl;
1310 for (; it != end; ++it)
1318 bool Buffer::hasParWithID(int id) const
1320 ParConstIterator it = par_iterator_begin();
1321 ParConstIterator end = par_iterator_end();
1324 // John says this is called with id == -1 from undo
1325 lyxerr << "hasParWithID(), id: " << id << endl;
1329 for (; it != end; ++it)
1337 PosIterator Buffer::pos_iterator_begin()
1339 return PosIterator(¶graphs(), paragraphs().begin(), 0);
1343 PosIterator Buffer::pos_iterator_end()
1345 return PosIterator(¶graphs(), paragraphs().end(), 0);
1349 ParIterator Buffer::par_iterator_begin()
1351 return ParIterator(paragraphs().begin(), paragraphs());
1355 ParIterator Buffer::par_iterator_end()
1357 return ParIterator(paragraphs().end(), paragraphs());
1361 ParConstIterator Buffer::par_iterator_begin() const
1363 ParagraphList const & pars = paragraphs();
1364 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
1368 ParConstIterator Buffer::par_iterator_end() const
1370 ParagraphList const & pars = paragraphs();
1371 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
1375 Language const * Buffer::getLanguage() const
1377 return params().language;
1381 string const Buffer::B_(string const & l10n) const
1383 if (pimpl_->messages.get()) {
1384 return pimpl_->messages->get(l10n);
1391 bool Buffer::isClean() const
1393 return pimpl_->lyx_clean;
1397 bool Buffer::isBakClean() const
1399 return pimpl_->bak_clean;
1403 void Buffer::markClean() const
1405 if (!pimpl_->lyx_clean) {
1406 pimpl_->lyx_clean = true;
1409 // if the .lyx file has been saved, we don't need an
1411 pimpl_->bak_clean = true;
1415 void Buffer::markBakClean()
1417 pimpl_->bak_clean = true;
1421 void Buffer::setUnnamed(bool flag)
1423 pimpl_->unnamed = flag;
1427 bool Buffer::isUnnamed()
1429 return pimpl_->unnamed;
1433 void Buffer::markDirty()
1435 if (pimpl_->lyx_clean) {
1436 pimpl_->lyx_clean = false;
1439 pimpl_->bak_clean = false;
1441 DepClean::iterator it = pimpl_->dep_clean.begin();
1442 DepClean::const_iterator const end = pimpl_->dep_clean.end();
1444 for (; it != end; ++it) {
1450 string const & Buffer::fileName() const
1452 return pimpl_->filename;
1456 string const & Buffer::filePath() const
1458 return pimpl_->filepath;
1462 bool Buffer::isReadonly() const
1464 return pimpl_->read_only;
1468 void Buffer::setParentName(string const & name)
1470 params().parentname = name;
1474 Buffer::inset_iterator::inset_iterator()
1479 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
1486 Buffer::inset_iterator Buffer::inset_iterator_begin()
1488 return inset_iterator(paragraphs().begin(), paragraphs().end());
1492 Buffer::inset_iterator Buffer::inset_iterator_end()
1494 return inset_iterator(paragraphs().end(), paragraphs().end());
1498 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
1500 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1501 return inset_iterator(pars.begin(), pars.end());
1505 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
1507 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1508 return inset_iterator(pars.end(), pars.end());
1512 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
1516 if (it == pit->insetlist.end()) {
1525 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
1527 inset_iterator tmp = *this;
1533 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
1539 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
1545 ParagraphList::iterator Buffer::inset_iterator::getPar() const
1551 lyx::pos_type Buffer::inset_iterator::getPos() const
1557 bool operator==(Buffer::inset_iterator const & iter1,
1558 Buffer::inset_iterator const & iter2)
1560 return iter1.pit == iter2.pit
1561 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
1565 bool operator!=(Buffer::inset_iterator const & iter1,
1566 Buffer::inset_iterator const & iter2)
1568 return !(iter1 == iter2);