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"
22 #include "errorlist.h"
26 #include "iterators.h"
29 #include "LaTeXFeatures.h"
30 #include "LyXAction.h"
35 #include "paragraph.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphParameters.h"
43 #include "insets/insetbibitem.h"
44 #include "insets/insetbibtex.h"
45 #include "insets/insetinclude.h"
46 #include "insets/insettext.h"
48 #include "frontends/Alert.h"
50 #include "graphics/Previews.h"
52 #include "support/FileInfo.h"
53 #include "support/filetools.h"
54 #include "support/gzstream.h"
55 #include "support/lyxlib.h"
56 #include "support/os.h"
57 #include "support/path.h"
58 #include "support/textutils.h"
59 #include "support/tostr.h"
61 #include <boost/bind.hpp>
62 #include <boost/tuple/tuple.hpp>
64 #include "support/std_sstream.h"
76 using lyx::support::AddName;
77 using lyx::support::ascii_lowercase;
78 using lyx::support::atoi;
79 using lyx::support::bformat;
80 using lyx::support::ChangeExtension;
81 using lyx::support::cmd_ret;
82 using lyx::support::compare_ascii_no_case;
83 using lyx::support::compare_no_case;
84 using lyx::support::contains;
85 using lyx::support::CreateBufferTmpDir;
86 using lyx::support::destroyDir;
87 using lyx::support::FileInfo;
88 using lyx::support::FileInfo;
89 using lyx::support::getExtFromContents;
90 using lyx::support::IsDirWriteable;
91 using lyx::support::IsFileWriteable;
92 using lyx::support::LibFileSearch;
93 using lyx::support::ltrim;
94 using lyx::support::MakeAbsPath;
95 using lyx::support::MakeDisplayPath;
96 using lyx::support::MakeLatexName;
97 using lyx::support::OnlyFilename;
98 using lyx::support::OnlyPath;
99 using lyx::support::Path;
100 using lyx::support::QuoteName;
101 using lyx::support::removeAutosaveFile;
102 using lyx::support::rename;
103 using lyx::support::RunCommand;
104 using lyx::support::split;
105 using lyx::support::strToInt;
106 using lyx::support::subst;
107 using lyx::support::tempName;
108 using lyx::support::trim;
110 namespace os = lyx::support::os;
114 using std::make_pair;
120 using std::ostringstream;
127 // all these externs should eventually be removed.
128 extern BufferList bufferlist;
132 const int LYX_FORMAT = 225;
134 bool openFileWrite(ofstream & ofs, string const & fname)
136 ofs.open(fname.c_str());
138 string const file = MakeDisplayPath(fname, 50);
139 string text = bformat(_("Could not open the specified "
140 "document\n%1$s."), file);
141 Alert::error(_("Could not open file"), text);
150 typedef std::map<string, bool> DepClean;
154 Impl(Buffer & parent, string const & file, bool readonly);
156 limited_stack<Undo> undostack;
157 limited_stack<Undo> redostack;
159 ParagraphList paragraphs;
165 /// need to regenerate .tex ?
169 mutable bool lyx_clean;
171 /// is autosave needed
172 mutable bool bak_clean;
174 /// is this a unnamed file (New...)
180 /// name of the file the buffer is associated with.
183 /// The path to the document file.
186 boost::scoped_ptr<Messages> messages;
190 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
192 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
193 filename(file), filepath(OnlyPath(file))
195 lyxvc.buffer(&parent);
196 if (readonly_ || lyxrc.use_tempdir)
197 temppath = CreateBufferTmpDir();
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 paragraphs().clear();
223 // Remove any previewed LaTeX snippets associated with this buffer.
224 lyx::graphics::Previews::get().removeLoader(*this);
228 limited_stack<Undo> & Buffer::undostack()
230 return pimpl_->undostack;
234 limited_stack<Undo> const & Buffer::undostack() const
236 return pimpl_->undostack;
240 limited_stack<Undo> & Buffer::redostack()
242 return pimpl_->redostack;
246 limited_stack<Undo> const & Buffer::redostack() const
248 return pimpl_->redostack;
252 BufferParams & Buffer::params()
254 return pimpl_->params;
258 BufferParams const & Buffer::params() const
260 return pimpl_->params;
264 ParagraphList & Buffer::paragraphs()
266 return pimpl_->paragraphs;
270 ParagraphList const & Buffer::paragraphs() const
272 return pimpl_->paragraphs;
276 LyXVC & Buffer::lyxvc()
278 return pimpl_->lyxvc;
282 LyXVC const & Buffer::lyxvc() const
284 return pimpl_->lyxvc;
288 string const & Buffer::temppath() const
290 return pimpl_->temppath;
294 bool & Buffer::niceFile()
296 return pimpl_->nicefile;
300 bool Buffer::niceFile() const
302 return pimpl_->nicefile;
306 TexRow & Buffer::texrow()
308 return pimpl_->texrow;
312 TexRow const & Buffer::texrow() const
314 return pimpl_->texrow;
318 string const Buffer::getLatexName(bool no_path) const
320 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
321 return no_path ? OnlyFilename(name) : name;
325 pair<Buffer::LogType, string> const Buffer::getLogName() const
327 string const filename = getLatexName(false);
329 if (filename.empty())
330 return make_pair(Buffer::latexlog, string());
332 string path = OnlyPath(filename);
334 if (lyxrc.use_tempdir || !IsDirWriteable(path))
337 string const fname = AddName(path,
338 OnlyFilename(ChangeExtension(filename,
341 AddName(path, OnlyFilename(
342 ChangeExtension(filename,
343 formats.extension("literate") + ".out")));
345 // If no Latex log or Build log is newer, show Build log
347 FileInfo const f_fi(fname);
348 FileInfo const b_fi(bname);
351 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
352 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
353 return make_pair(Buffer::buildlog, bname);
355 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
356 return make_pair(Buffer::latexlog, fname);
360 void Buffer::setReadonly(bool flag)
362 if (pimpl_->read_only != flag) {
363 pimpl_->read_only = flag;
369 void Buffer::setFileName(string const & newfile)
371 pimpl_->filename = MakeAbsPath(newfile);
372 pimpl_->filepath = OnlyPath(pimpl_->filename);
373 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
378 // We'll remove this later. (Lgb)
381 void unknownClass(string const & unknown)
383 Alert::warning(_("Unknown document class"),
384 bformat(_("Using the default document class, because the "
385 "class %1$s is unknown."), unknown));
390 int Buffer::readHeader(LyXLex & lex)
392 int unknown_tokens = 0;
396 string const token = lex.getString();
401 if (token == "\\end_header")
404 lyxerr[Debug::PARSER] << "Handling header token: `"
405 << token << '\'' << endl;
408 string unknown = params().readToken(lex, token);
409 if (!unknown.empty()) {
410 if (unknown[0] != '\\') {
411 unknownClass(unknown);
414 string const s = bformat(_("Unknown token: "
418 error(ErrorItem(_("Header error"), s,
423 return unknown_tokens;
427 // candidate for move to BufferView
428 // (at least some parts in the beginning of the func)
431 // changed to be public and have one parameter
432 // if par = 0 normal behavior
433 // else insert behavior
434 // Returns false if "\end_document" is not read (Asger)
435 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
437 Paragraph::depth_type depth = 0;
438 bool the_end_read = false;
440 if (paragraphs().empty()) {
442 if (!params().getLyXTextClass().load()) {
443 string theclass = params().getLyXTextClass().name();
444 Alert::error(_("Can't load document class"), bformat(
445 "Using the default document class, because the "
446 " class %1$s could not be loaded.", theclass));
447 params().textclass = 0;
450 // We don't want to adopt the parameters from the
451 // document we insert, so read them into a temporary buffer
452 // and then discard it
454 Buffer tmpbuf("", false);
455 tmpbuf.readHeader(lex);
460 string const token = lex.getString();
465 lyxerr[Debug::PARSER] << "Handling token: `"
466 << token << '\'' << endl;
468 if (token == "\\end_document") {
473 readParagraph(lex, token, paragraphs(), pit, depth);
480 int Buffer::readParagraph(LyXLex & lex, string const & token,
481 ParagraphList & pars, ParagraphList::iterator & pit,
482 Paragraph::depth_type & depth)
484 static Change current_change;
487 if (token == "\\begin_layout") {
488 lex.pushToken(token);
491 par.params().depth(depth);
492 if (params().tracking_changes)
494 LyXFont f(LyXFont::ALL_INHERIT, params().language);
498 if (pit != pars.end())
501 pit = pars.insert(pit, par);
503 // FIXME: goddamn InsetTabular makes us pass a Buffer
505 ::readParagraph(*this, *pit, lex);
507 } else if (token == "\\begin_deeper") {
509 } else if (token == "\\end_deeper") {
511 lex.printError("\\end_deeper: " "depth is already null");
522 // needed to insert the selection
523 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
524 LyXFont const & fn,string const & str)
526 LyXLayout_ptr const & layout = par->layout();
530 par->checkInsertChar(font);
531 // insert the string, don't insert doublespace
532 bool space_inserted = true;
533 bool autobreakrows = !par->inInset() ||
534 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
535 for(string::const_iterator cit = str.begin();
536 cit != str.end(); ++cit) {
538 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
539 breakParagraph(params(), paragraphs(), par, pos,
540 layout->isEnvironment());
543 space_inserted = true;
547 // do not insert consecutive spaces if !free_spacing
548 } else if ((*cit == ' ' || *cit == '\t') &&
549 space_inserted && !par->isFreeSpacing()) {
551 } else if (*cit == '\t') {
552 if (!par->isFreeSpacing()) {
553 // tabs are like spaces here
554 par->insertChar(pos, ' ', font);
556 space_inserted = true;
558 const pos_type nb = 8 - pos % 8;
559 for (pos_type a = 0; a < nb ; ++a) {
560 par->insertChar(pos, ' ', font);
563 space_inserted = true;
565 } else if (!IsPrintable(*cit)) {
566 // Ignore unprintables
569 // just insert the character
570 par->insertChar(pos, *cit, font);
572 space_inserted = (*cit == ' ');
579 bool Buffer::readFile(string const & filename)
581 // Check if the file is compressed.
582 string const format = getExtFromContents(filename);
583 if (format == "gzip" || format == "zip" || format == "compress") {
584 params().compressed = true;
587 bool ret = readFile(filename, paragraphs().begin());
589 // After we have read a file, we must ensure that the buffer
590 // language is set and used in the gui.
591 // If you know of a better place to put this, please tell me. (Lgb)
592 updateDocLang(params().language);
598 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
601 lex.setFile(filename);
603 return readFile(lex, filename, pit);
607 bool Buffer::readFile(LyXLex & lex, string const & filename,
608 ParagraphList::iterator pit)
610 BOOST_ASSERT(!filename.empty());
613 Alert::error(_("Document could not be read"),
614 bformat(_("%1$s could not be read."), filename));
619 string const token(lex.getString());
622 Alert::error(_("Document could not be read"),
623 bformat(_("%1$s could not be read."), filename));
627 // the first token _must_ be...
628 if (token != "\\lyxformat") {
629 lyxerr << "Token: " << token << endl;
631 Alert::error(_("Document format failure"),
632 bformat(_("%1$s is not a LyX document."),
638 string tmp_format = lex.getString();
639 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
640 // if present remove ".," from string.
641 string::size_type dot = tmp_format.find_first_of(".,");
642 //lyxerr << " dot found at " << dot << endl;
643 if (dot != string::npos)
644 tmp_format.erase(dot, 1);
645 int file_format = strToInt(tmp_format);
646 //lyxerr << "format: " << file_format << endl;
648 if (file_format > LYX_FORMAT) {
649 Alert::warning(_("Document format failure"),
650 bformat(_("%1$s was created with a newer"
651 " version of LyX. This is likely to"
654 } else if (file_format < LYX_FORMAT) {
655 string const tmpfile = tempName();
656 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
657 if (command.empty()) {
658 Alert::error(_("Conversion script not found"),
659 bformat(_("%1$s is from an earlier"
660 " version of LyX, but the"
661 " conversion script lyx2lyx"
662 " could not be found."),
668 + " -o " + tmpfile + ' '
669 + QuoteName(filename);
670 lyxerr[Debug::INFO] << "Running '"
673 cmd_ret const ret = RunCommand(command);
674 if (ret.first != 0) {
675 Alert::error(_("Conversion script failed"),
676 bformat(_("%1$s is from an earlier version"
677 " of LyX, but the lyx2lyx script"
678 " failed to convert it."),
682 bool ret = readFile(tmpfile, pit);
683 // Do stuff with tmpfile name and buffer name here.
689 bool the_end = readBody(lex, pit);
690 params().setPaperStuff();
693 Alert::error(_("Document format failure"),
694 bformat(_("%1$s ended unexpectedly, which means"
695 " that it is probably corrupted."),
702 // Should probably be moved to somewhere else: BufferView? LyXView?
703 bool Buffer::save() const
705 // We don't need autosaves in the immediate future. (Asger)
706 resetAutosaveTimers();
710 if (lyxrc.make_backup) {
711 s = fileName() + '~';
712 if (!lyxrc.backupdir_path.empty())
713 s = AddName(lyxrc.backupdir_path,
714 subst(os::slashify_path(s),'/','!'));
716 // Rename is the wrong way of making a backup,
717 // this is the correct way.
718 /* truss cp fil fil2:
719 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
720 stat("LyXVC.lyx", 0xEFFFF688) = 0
721 open("LyXVC.lyx", O_RDONLY) = 3
722 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
723 fstat(4, 0xEFFFF508) = 0
724 fstat(3, 0xEFFFF508) = 0
725 read(3, " # T h i s f i l e w".., 8192) = 5579
726 write(4, " # T h i s f i l e w".., 5579) = 5579
727 read(3, 0xEFFFD4A0, 8192) = 0
730 chmod("LyXVC3.lyx", 0100644) = 0
731 lseek(0, 0, SEEK_CUR) = 46440
735 // Should probably have some more error checking here.
736 // Doing it this way, also makes the inodes stay the same.
737 // This is still not a very good solution, in particular we
738 // might loose the owner of the backup.
739 FileInfo finfo(fileName());
741 mode_t fmode = finfo.getMode();
742 struct utimbuf times = {
743 finfo.getAccessTime(),
744 finfo.getModificationTime() };
746 ifstream ifs(fileName().c_str());
747 ofstream ofs(s.c_str(), ios::out|ios::trunc);
752 ::chmod(s.c_str(), fmode);
754 if (::utime(s.c_str(), ×)) {
755 lyxerr << "utime error." << endl;
758 lyxerr << "LyX was not able to make "
759 "backup copy. Beware." << endl;
764 if (writeFile(fileName())) {
766 removeAutosaveFile(fileName());
768 // Saving failed, so backup is not backup
769 if (lyxrc.make_backup) {
770 rename(s, fileName());
778 bool Buffer::writeFile(string const & fname) const
780 if (pimpl_->read_only && (fname == fileName())) {
784 FileInfo finfo(fname);
785 if (finfo.exist() && !finfo.writable()) {
791 if (params().compressed) {
792 gz::ogzstream ofs(fname.c_str());
797 retval = do_writeFile(ofs);
800 ofstream ofs(fname.c_str());
804 retval = do_writeFile(ofs);
811 bool Buffer::do_writeFile(ostream & ofs) const
815 // Use the standard "C" locale for file output.
816 ofs.imbue(std::locale::classic());
819 // The top of the file should not be written by params().
821 // write out a comment in the top of the file
822 ofs << "#LyX " << lyx_version
823 << " created this file. For more info see http://www.lyx.org/\n"
824 << "\\lyxformat " << LYX_FORMAT << "\n";
826 // now write out the buffer paramters.
827 params().writeFile(ofs);
829 ofs << "\\end_header\n";
831 Paragraph::depth_type depth = 0;
833 // this will write out all the paragraphs
834 // using recursive descent.
835 ParagraphList::const_iterator pit = paragraphs().begin();
836 ParagraphList::const_iterator pend = paragraphs().end();
837 for (; pit != pend; ++pit)
838 pit->write(*this, ofs, params(), depth);
840 // Write marker that shows file is complete
841 ofs << "\n\\end_document" << endl;
843 // Shouldn't really be needed....
846 // how to check if close went ok?
847 // Following is an attempt... (BE 20001011)
849 // good() returns false if any error occured, including some
851 // bad() returns true if something bad happened in the buffer,
852 // which should include file system full errors.
859 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
861 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
873 pair<int, string> const addDepth(int depth, int ldepth)
877 d += (ldepth - depth) * 2;
878 return make_pair(d, string(d, ' '));
884 string const Buffer::asciiParagraph(Paragraph const & par,
885 unsigned int linelen,
886 bool noparbreak) const
888 ostringstream buffer;
890 Paragraph::depth_type ltype_depth = 0;
891 bool ref_printed = false;
892 Paragraph::depth_type depth = par.params().depth();
894 // First write the layout
895 string const & tmp = par.layout()->name();
896 if (compare_no_case(tmp, "itemize") == 0) {
898 ltype_depth = depth + 1;
899 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
901 ltype_depth = depth + 1;
902 } else if (contains(ascii_lowercase(tmp), "ection")) {
904 ltype_depth = depth + 1;
905 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
907 ltype_depth = depth + 1;
908 } else if (compare_ascii_no_case(tmp, "description") == 0) {
910 ltype_depth = depth + 1;
911 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
914 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
922 /* maybe some vertical spaces */
924 /* the labelwidthstring used in lists */
928 /* some pagebreaks? */
932 /* what about the alignment */
934 // linelen <= 0 is special and means we don't have paragraph breaks
936 string::size_type currlinelen = 0;
942 buffer << string(depth * 2, ' ');
943 currlinelen += depth * 2;
946 // we should probably change to the paragraph language in the
947 // gettext here (if possible) so that strings are outputted in
948 // the correct language! (20012712 Jug)
952 case 4: // (Sub)Paragraph
953 case 5: // Description
957 buffer << _("Abstract") << "\n\n";
960 string const abst = _("Abstract: ");
962 currlinelen += abst.length();
965 case 7: // Bibliography
968 buffer << _("References") << "\n\n";
971 string const refs = _("References: ");
973 currlinelen += refs.length();
981 string const parlab = par.params().labelString();
982 buffer << parlab << ' ';
983 currlinelen += parlab.length() + 1;
991 pair<int, string> p = addDepth(depth, ltype_depth);
993 currlinelen += p.first;
996 // this is to change the linebreak to do it by word a bit more
997 // intelligent hopefully! (only in the case where we have a
998 // max linelength!) (Jug)
1002 for (pos_type i = 0; i < par.size(); ++i) {
1003 char c = par.getUChar(params(), i);
1005 case Paragraph::META_INSET:
1007 InsetOld const * inset = par.getInset(i);
1011 currlinelen += word.length();
1014 if (inset->ascii(*this, buffer, linelen)) {
1015 // to be sure it breaks paragraph
1016 currlinelen += linelen;
1025 currlinelen + word.length() > linelen - 10) {
1027 pair<int, string> p = addDepth(depth, ltype_depth);
1029 currlinelen = p.first;
1032 buffer << word << ' ';
1033 currlinelen += word.length() + 1;
1040 lyxerr[Debug::INFO] <<
1041 "writeAsciiFile: NULL char in structure." << endl;
1043 if ((linelen > 0) &&
1044 (currlinelen + word.length()) > linelen)
1048 pair<int, string> p =
1049 addDepth(depth, ltype_depth);
1051 currlinelen = p.first;
1058 return buffer.str();
1062 void Buffer::writeFileAscii(string const & fname, int linelen)
1065 if (!::openFileWrite(ofs, fname))
1067 writeFileAscii(ofs, linelen);
1071 void Buffer::writeFileAscii(ostream & os, int linelen)
1073 ParagraphList::iterator beg = paragraphs().begin();
1074 ParagraphList::iterator end = paragraphs().end();
1075 ParagraphList::iterator it = beg;
1076 for (; it != end; ++it) {
1077 os << asciiParagraph(*it, linelen, it == beg);
1083 void Buffer::makeLaTeXFile(string const & fname,
1084 string const & original_path,
1085 LatexRunParams const & runparams,
1086 bool output_preamble, bool output_body)
1088 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1091 if (!::openFileWrite(ofs, fname))
1094 makeLaTeXFile(ofs, original_path,
1095 runparams, output_preamble, output_body);
1099 lyxerr << "File was not closed properly." << endl;
1104 void Buffer::makeLaTeXFile(ostream & os,
1105 string const & original_path,
1106 LatexRunParams const & runparams_in,
1107 bool output_preamble, bool output_body)
1109 LatexRunParams runparams = runparams_in;
1110 niceFile() = runparams.nice; // this will be used by Insetincludes.
1112 // validate the buffer.
1113 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1114 LaTeXFeatures features(*this, params());
1116 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1119 // The starting paragraph of the coming rows is the
1120 // first paragraph of the document. (Asger)
1121 texrow().start(paragraphs().begin()->id(), 0);
1123 if (output_preamble && runparams.nice) {
1124 os << "%% LyX " << lyx_version << " created this file. "
1125 "For more info, see http://www.lyx.org/.\n"
1126 "%% Do not edit unless you really know what "
1131 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1132 // There are a few differences between nice LaTeX and usual files:
1133 // usual is \batchmode and has a
1134 // special input@path to allow the including of figures
1135 // with either \input or \includegraphics (what figinsets do).
1136 // input@path is set when the actual parameter
1137 // original_path is set. This is done for usual tex-file, but not
1138 // for nice-latex-file. (Matthias 250696)
1139 if (output_preamble) {
1140 if (!runparams.nice) {
1141 // code for usual, NOT nice-latex-file
1142 os << "\\batchmode\n"; // changed
1143 // from \nonstopmode
1146 if (!original_path.empty()) {
1147 string inputpath = os::external_path(original_path);
1148 subst(inputpath, "~", "\\string~");
1149 os << "\\makeatletter\n"
1150 << "\\def\\input@path{{"
1151 << inputpath << "/}}\n"
1152 << "\\makeatother\n";
1158 // Write the preamble
1159 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1165 os << "\\begin{document}\n";
1167 } // output_preamble
1168 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1170 if (!lyxrc.language_auto_begin) {
1171 os << subst(lyxrc.language_command_begin, "$$lang",
1172 params().language->babel())
1177 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1179 // add this just in case after all the paragraphs
1183 if (!lyxrc.language_auto_end) {
1184 os << subst(lyxrc.language_command_end, "$$lang",
1185 params().language->babel())
1190 if (output_preamble) {
1191 os << "\\end{document}\n";
1194 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1196 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1200 // Just to be sure. (Asger)
1203 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1204 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1207 // we want this to be true outside previews (for insetexternal)
1212 bool Buffer::isLatex() const
1214 return params().getLyXTextClass().outputType() == LATEX;
1218 bool Buffer::isLinuxDoc() const
1220 return params().getLyXTextClass().outputType() == LINUXDOC;
1224 bool Buffer::isLiterate() const
1226 return params().getLyXTextClass().outputType() == LITERATE;
1230 bool Buffer::isDocBook() const
1232 return params().getLyXTextClass().outputType() == DOCBOOK;
1236 bool Buffer::isSGML() const
1238 LyXTextClass const & tclass = params().getLyXTextClass();
1240 return tclass.outputType() == LINUXDOC ||
1241 tclass.outputType() == DOCBOOK;
1245 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1248 if (!::openFileWrite(ofs, fname))
1251 niceFile() = nice; // this will be used by included files.
1253 LaTeXFeatures features(*this, params());
1259 LyXTextClass const & tclass = params().getLyXTextClass();
1261 string top_element = tclass.latexname();
1264 ofs << "<!doctype linuxdoc system";
1266 string preamble = params().preamble;
1267 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1269 preamble += features.getIncludedFiles(name);
1270 preamble += features.getLyXSGMLEntities();
1272 if (!preamble.empty()) {
1273 ofs << " [ " << preamble << " ]";
1277 if (params().options.empty())
1278 sgml::openTag(ofs, 0, false, top_element);
1280 string top = top_element;
1282 top += params().options;
1283 sgml::openTag(ofs, 0, false, top);
1287 ofs << "<!-- LyX " << lyx_version
1288 << " created this file. For more info see http://www.lyx.org/"
1291 Paragraph::depth_type depth = 0; // paragraph depth
1293 vector<string> environment_stack(5);
1295 ParagraphList::iterator pit = paragraphs().begin();
1296 ParagraphList::iterator pend = paragraphs().end();
1297 for (; pit != pend; ++pit) {
1298 LyXLayout_ptr const & style = pit->layout();
1299 // treat <toc> as a special case for compatibility with old code
1300 if (pit->isInset(0)) {
1301 InsetOld * inset = pit->getInset(0);
1302 InsetOld::Code lyx_code = inset->lyxCode();
1303 if (lyx_code == InsetOld::TOC_CODE) {
1304 string const temp = "toc";
1305 sgml::openTag(ofs, depth, false, temp);
1310 // environment tag closing
1311 for (; depth > pit->params().depth(); --depth) {
1312 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1313 environment_stack[depth].erase();
1316 // write opening SGML tags
1317 switch (style->latextype) {
1318 case LATEX_PARAGRAPH:
1319 if (depth == pit->params().depth()
1320 && !environment_stack[depth].empty()) {
1321 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1322 environment_stack[depth].erase();
1328 sgml::openTag(ofs, depth, false, style->latexname());
1333 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1335 if (!environment_stack[depth].empty()) {
1336 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1340 environment_stack[depth].erase();
1341 sgml::openTag(ofs, depth, false, style->latexname());
1344 case LATEX_ENVIRONMENT:
1345 case LATEX_ITEM_ENVIRONMENT:
1346 case LATEX_BIB_ENVIRONMENT:
1348 string const & latexname = style->latexname();
1350 if (depth == pit->params().depth()
1351 && environment_stack[depth] != latexname) {
1352 sgml::closeTag(ofs, depth, false,
1353 environment_stack[depth]);
1354 environment_stack[depth].erase();
1356 if (depth < pit->params().depth()) {
1357 depth = pit->params().depth();
1358 environment_stack[depth].erase();
1360 if (environment_stack[depth] != latexname) {
1362 sgml::openTag(ofs, depth, false, "p");
1364 sgml::openTag(ofs, depth, false, latexname);
1366 if (environment_stack.size() == depth + 1)
1367 environment_stack.push_back("!-- --");
1368 environment_stack[depth] = latexname;
1371 if (style->latexparam() == "CDATA")
1374 if (style->latextype == LATEX_ENVIRONMENT) break;
1376 if (style->labeltype == LABEL_MANUAL)
1381 sgml::openTag(ofs, depth + 1, false, item_name);
1386 sgml::openTag(ofs, depth, false, style->latexname());
1390 simpleLinuxDocOnePar(ofs, pit, depth);
1393 // write closing SGML tags
1394 switch (style->latextype) {
1397 case LATEX_ENVIRONMENT:
1398 case LATEX_ITEM_ENVIRONMENT:
1399 case LATEX_BIB_ENVIRONMENT:
1400 if (style->latexparam() == "CDATA")
1404 sgml::closeTag(ofs, depth, false, style->latexname());
1410 for (int i = depth; i >= 0; --i)
1411 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1415 sgml::closeTag(ofs, 0, false, top_element);
1419 // How to check for successful close
1421 // we want this to be true outside previews (for insetexternal)
1426 // checks, if newcol chars should be put into this line
1427 // writes newline, if necessary.
1430 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1431 string::size_type newcol)
1434 if (colcount > lyxrc.ascii_linelen) {
1436 colcount = newcol; // assume write after this call
1451 string tag_name(PAR_TAG const & pt) {
1453 case NONE: return "!-- --";
1454 case TT: return "tt";
1455 case SF: return "sf";
1456 case BF: return "bf";
1457 case IT: return "it";
1458 case SL: return "sl";
1459 case EM: return "em";
1466 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1468 p1 = static_cast<PAR_TAG>(p1 | p2);
1473 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1475 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1481 // Handle internal paragraph parsing -- layout already processed.
1482 void Buffer::simpleLinuxDocOnePar(ostream & os,
1483 ParagraphList::iterator par,
1484 Paragraph::depth_type /*depth*/) const
1486 LyXLayout_ptr const & style = par->layout();
1488 string::size_type char_line_count = 5; // Heuristic choice ;-)
1490 // gets paragraph main font
1493 if (style->labeltype == LABEL_MANUAL) {
1494 font_old = style->labelfont;
1497 font_old = style->font;
1501 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1502 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1503 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1506 stack<PAR_TAG> tag_state;
1507 // parsing main loop
1508 for (pos_type i = 0; i < par->size(); ++i) {
1510 PAR_TAG tag_close = NONE;
1511 list < PAR_TAG > tag_open;
1513 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1515 if (font_old.family() != font.family()) {
1516 switch (family_type) {
1517 case LyXFont::SANS_FAMILY:
1520 case LyXFont::TYPEWRITER_FAMILY:
1527 family_type = font.family();
1529 switch (family_type) {
1530 case LyXFont::SANS_FAMILY:
1531 tag_open.push_back(SF);
1533 case LyXFont::TYPEWRITER_FAMILY:
1534 tag_open.push_back(TT);
1541 if (font_old.series() != font.series()) {
1542 switch (series_type) {
1543 case LyXFont::BOLD_SERIES:
1550 series_type = font.series();
1552 switch (series_type) {
1553 case LyXFont::BOLD_SERIES:
1554 tag_open.push_back(BF);
1562 if (font_old.shape() != font.shape()) {
1563 switch (shape_type) {
1564 case LyXFont::ITALIC_SHAPE:
1567 case LyXFont::SLANTED_SHAPE:
1574 shape_type = font.shape();
1576 switch (shape_type) {
1577 case LyXFont::ITALIC_SHAPE:
1578 tag_open.push_back(IT);
1580 case LyXFont::SLANTED_SHAPE:
1581 tag_open.push_back(SL);
1588 if (font_old.emph() != font.emph()) {
1589 if (font.emph() == LyXFont::ON) {
1590 tag_open.push_back(EM);
1599 list < PAR_TAG > temp;
1600 while (!tag_state.empty() && tag_close) {
1601 PAR_TAG k = tag_state.top();
1603 os << "</" << tag_name(k) << '>';
1610 for(list< PAR_TAG >::const_iterator j = temp.begin();
1611 j != temp.end(); ++j) {
1613 os << '<' << tag_name(*j) << '>';
1616 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1617 j != tag_open.end(); ++j) {
1619 os << '<' << tag_name(*j) << '>';
1622 char c = par->getChar(i);
1624 if (c == Paragraph::META_INSET) {
1625 InsetOld * inset = par->getInset(i);
1626 inset->linuxdoc(*this, os);
1631 if (style->latexparam() == "CDATA") {
1632 // "TeX"-Mode on == > SGML-Mode on.
1639 boost::tie(ws, str) = sgml::escapeChar(c);
1640 if (ws && !par->isFreeSpacing()) {
1641 // in freespacing mode, spaces are
1642 // non-breaking characters
1643 if (desc_on) {// if char is ' ' then...
1646 sgmlLineBreak(os, char_line_count, 6);
1650 sgmlLineBreak(os, char_line_count, 1);
1655 char_line_count += str.length();
1661 while (!tag_state.empty()) {
1662 os << "</" << tag_name(tag_state.top()) << '>';
1666 // resets description flag correctly
1668 // <tag> not closed...
1669 sgmlLineBreak(os, char_line_count, 6);
1675 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1678 if (!::openFileWrite(ofs, fname))
1681 niceFile() = nice; // this will be used by Insetincludes.
1683 LaTeXFeatures features(*this, params());
1688 LyXTextClass const & tclass = params().getLyXTextClass();
1689 string top_element = tclass.latexname();
1692 ofs << "<!DOCTYPE " << top_element
1693 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1695 string preamble = params().preamble;
1696 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1698 preamble += features.getIncludedFiles(name);
1699 preamble += features.getLyXSGMLEntities();
1701 if (!preamble.empty()) {
1702 ofs << "\n [ " << preamble << " ]";
1707 string top = top_element;
1709 top += params().language->code();
1712 if (!params().options.empty()) {
1714 top += params().options;
1716 sgml::openTag(ofs, 0, false, top);
1718 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1719 << "\n See http://www.lyx.org/ for more information -->\n";
1721 vector<string> environment_stack(10);
1722 vector<string> environment_inner(10);
1723 vector<string> command_stack(10);
1725 bool command_flag = false;
1726 Paragraph::depth_type command_depth = 0;
1727 Paragraph::depth_type command_base = 0;
1728 Paragraph::depth_type cmd_depth = 0;
1729 Paragraph::depth_type depth = 0; // paragraph depth
1732 string command_name;
1734 ParagraphList::iterator par = paragraphs().begin();
1735 ParagraphList::iterator pend = paragraphs().end();
1737 for (; par != pend; ++par) {
1741 int desc_on = 0; // description mode
1743 LyXLayout_ptr const & style = par->layout();
1745 // environment tag closing
1746 for (; depth > par->params().depth(); --depth) {
1747 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1748 item_name = "listitem";
1749 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1750 if (environment_inner[depth] == "varlistentry")
1751 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1753 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1754 environment_stack[depth].erase();
1755 environment_inner[depth].erase();
1758 if (depth == par->params().depth()
1759 && environment_stack[depth] != style->latexname()
1760 && !environment_stack[depth].empty()) {
1761 if (environment_inner[depth] != "!-- --") {
1762 item_name= "listitem";
1763 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1764 if (environment_inner[depth] == "varlistentry")
1765 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1768 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1770 environment_stack[depth].erase();
1771 environment_inner[depth].erase();
1774 // Write opening SGML tags.
1775 switch (style->latextype) {
1776 case LATEX_PARAGRAPH:
1777 sgml::openTag(ofs, depth + command_depth,
1778 false, style->latexname());
1783 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1785 command_name = style->latexname();
1787 sgmlparam = style->latexparam();
1788 c_params = split(sgmlparam, c_depth,'|');
1790 cmd_depth = atoi(c_depth);
1793 if (cmd_depth < command_base) {
1794 for (Paragraph::depth_type j = command_depth;
1795 j >= command_base; --j) {
1796 sgml::closeTag(ofs, j, false, command_stack[j]);
1799 command_depth = command_base = cmd_depth;
1800 } else if (cmd_depth <= command_depth) {
1801 for (int j = command_depth;
1802 j >= int(cmd_depth); --j) {
1803 sgml::closeTag(ofs, j, false, command_stack[j]);
1806 command_depth = cmd_depth;
1808 command_depth = cmd_depth;
1810 command_depth = command_base = cmd_depth;
1811 command_flag = true;
1813 if (command_stack.size() == command_depth + 1)
1814 command_stack.push_back(string());
1815 command_stack[command_depth] = command_name;
1817 // treat label as a special case for
1818 // more WYSIWYM handling.
1819 // This is a hack while paragraphs can't have
1820 // attributes, like id in this case.
1821 if (par->isInset(0)) {
1822 InsetOld * inset = par->getInset(0);
1823 InsetOld::Code lyx_code = inset->lyxCode();
1824 if (lyx_code == InsetOld::LABEL_CODE) {
1825 command_name += " id=\"";
1826 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1827 command_name += '"';
1832 sgml::openTag(ofs, depth + command_depth, false, command_name);
1834 item_name = c_params.empty() ? "title" : c_params;
1835 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1838 case LATEX_ENVIRONMENT:
1839 case LATEX_ITEM_ENVIRONMENT:
1840 if (depth < par->params().depth()) {
1841 depth = par->params().depth();
1842 environment_stack[depth].erase();
1845 if (environment_stack[depth] != style->latexname()) {
1846 if (environment_stack.size() == depth + 1) {
1847 environment_stack.push_back("!-- --");
1848 environment_inner.push_back("!-- --");
1850 environment_stack[depth] = style->latexname();
1851 environment_inner[depth] = "!-- --";
1852 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1854 if (environment_inner[depth] != "!-- --") {
1855 item_name= "listitem";
1856 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1857 if (environment_inner[depth] == "varlistentry")
1858 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1862 if (style->latextype == LATEX_ENVIRONMENT) {
1863 if (!style->latexparam().empty()) {
1864 if (style->latexparam() == "CDATA")
1867 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1872 desc_on = (style->labeltype == LABEL_MANUAL);
1874 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1875 sgml::openTag(ofs, depth + 1 + command_depth,
1876 false, environment_inner[depth]);
1878 item_name = desc_on ? "term" : "para";
1879 sgml::openTag(ofs, depth + 1 + command_depth,
1883 sgml::openTag(ofs, depth + command_depth,
1884 false, style->latexname());
1888 simpleDocBookOnePar(ofs, par, desc_on,
1889 depth + 1 + command_depth);
1892 // write closing SGML tags
1893 switch (style->latextype) {
1895 end_tag = c_params.empty() ? "title" : c_params;
1896 sgml::closeTag(ofs, depth + command_depth,
1899 case LATEX_ENVIRONMENT:
1900 if (!style->latexparam().empty()) {
1901 if (style->latexparam() == "CDATA")
1904 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1907 case LATEX_ITEM_ENVIRONMENT:
1908 if (desc_on == 1) break;
1910 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1912 case LATEX_PARAGRAPH:
1913 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1916 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1922 for (int d = depth; d >= 0; --d) {
1923 if (!environment_stack[depth].empty()) {
1924 if (environment_inner[depth] != "!-- --") {
1925 item_name = "listitem";
1926 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1927 if (environment_inner[depth] == "varlistentry")
1928 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1931 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1935 for (int j = command_depth; j >= 0 ; --j)
1936 if (!command_stack[j].empty()) {
1937 sgml::closeTag(ofs, j, false, command_stack[j]);
1942 sgml::closeTag(ofs, 0, false, top_element);
1945 // How to check for successful close
1947 // we want this to be true outside previews (for insetexternal)
1952 void Buffer::simpleDocBookOnePar(ostream & os,
1953 ParagraphList::iterator par, int & desc_on,
1954 Paragraph::depth_type depth) const
1956 bool emph_flag = false;
1958 LyXLayout_ptr const & style = par->layout();
1960 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1962 int char_line_count = depth;
1963 //if (!style.free_spacing)
1964 // os << string(depth,' ');
1966 // parsing main loop
1967 for (pos_type i = 0; i < par->size(); ++i) {
1968 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1970 // handle <emphasis> tag
1971 if (font_old.emph() != font.emph()) {
1972 if (font.emph() == LyXFont::ON) {
1973 if (style->latexparam() == "CDATA")
1976 if (style->latexparam() == "CDATA")
1980 if (style->latexparam() == "CDATA")
1982 os << "</emphasis>";
1983 if (style->latexparam() == "CDATA")
1990 if (par->isInset(i)) {
1991 InsetOld * inset = par->getInset(i);
1992 // don't print the inset in position 0 if desc_on == 3 (label)
1993 if (i || desc_on != 3) {
1994 if (style->latexparam() == "CDATA")
1996 inset->docbook(*this, os, false);
1997 if (style->latexparam() == "CDATA")
2001 char c = par->getChar(i);
2004 boost::tie(ws, str) = sgml::escapeChar(c);
2006 if (style->pass_thru) {
2008 } else if (par->isFreeSpacing() || c != ' ') {
2010 } else if (desc_on == 1) {
2012 os << "\n</term><listitem><para>";
2022 if (style->latexparam() == "CDATA")
2024 os << "</emphasis>";
2025 if (style->latexparam() == "CDATA")
2029 // resets description flag correctly
2031 // <term> not closed...
2032 os << "</term>\n<listitem><para> </para>";
2034 if (style->free_spacing)
2039 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2040 // Other flags: -wall -v0 -x
2041 int Buffer::runChktex()
2045 // get LaTeX-Filename
2046 string const name = getLatexName();
2047 string path = filePath();
2049 string const org_path = path;
2050 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2054 Path p(path); // path to LaTeX file
2055 message(_("Running chktex..."));
2057 // Generate the LaTeX file if neccessary
2058 LatexRunParams runparams;
2059 runparams.flavor = LatexRunParams::LATEX;
2060 runparams.nice = false;
2061 makeLaTeXFile(name, org_path, runparams);
2064 Chktex chktex(lyxrc.chktex_command, name, filePath());
2065 int res = chktex.run(terr); // run chktex
2068 Alert::error(_("chktex failure"),
2069 _("Could not run chktex successfully."));
2070 } else if (res > 0) {
2071 // Insert all errors as errors boxes
2072 bufferErrors(*this, terr);
2081 void Buffer::validate(LaTeXFeatures & features) const
2083 LyXTextClass const & tclass = params().getLyXTextClass();
2085 if (params().tracking_changes) {
2086 features.require("dvipost");
2087 features.require("color");
2090 // AMS Style is at document level
2091 if (params().use_amsmath == BufferParams::AMS_ON
2092 || tclass.provides(LyXTextClass::amsmath))
2093 features.require("amsmath");
2095 for_each(paragraphs().begin(), paragraphs().end(),
2096 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2098 // the bullet shapes are buffer level not paragraph level
2099 // so they are tested here
2100 for (int i = 0; i < 4; ++i) {
2101 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2102 int const font = params().user_defined_bullet(i).getFont();
2104 int const c = params()
2105 .user_defined_bullet(i)
2112 features.require("latexsym");
2114 } else if (font == 1) {
2115 features.require("amssymb");
2116 } else if ((font >= 2 && font <= 5)) {
2117 features.require("pifont");
2122 if (lyxerr.debugging(Debug::LATEX)) {
2123 features.showStruct();
2128 void Buffer::getLabelList(std::vector<string> & list) const
2130 /// if this is a child document and the parent is already loaded
2131 /// Use the parent's list instead [ale990407]
2132 if (!params().parentname.empty()
2133 && bufferlist.exists(params().parentname)) {
2134 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2136 tmp->getLabelList(list);
2141 for (inset_iterator it = inset_const_iterator_begin();
2142 it != inset_const_iterator_end(); ++it) {
2143 it->getLabelList(list);
2148 // This is also a buffer property (ale)
2149 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2151 /// if this is a child document and the parent is already loaded
2152 /// use the parent's list instead [ale990412]
2153 if (!params().parentname.empty() &&
2154 bufferlist.exists(params().parentname)) {
2155 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2157 tmp->fillWithBibKeys(keys);
2162 for (inset_iterator it = inset_const_iterator_begin();
2163 it != inset_const_iterator_end(); ++it) {
2164 if (it->lyxCode() == InsetOld::BIBTEX_CODE)
2165 static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
2166 else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
2167 static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
2168 else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2169 InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
2170 string const key = bib.getContents();
2171 string const opt = bib.getOptions();
2172 string const ref; // = pit->asString(this, false);
2173 string const info = opt + "TheBibliographyRef" + ref;
2174 keys.push_back(pair<string, string>(key, info));
2180 bool Buffer::isDepClean(string const & name) const
2182 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2183 if (it == pimpl_->dep_clean.end())
2189 void Buffer::markDepClean(string const & name)
2191 pimpl_->dep_clean[name] = true;
2195 bool Buffer::dispatch(string const & command, bool * result)
2197 // Split command string into command and argument
2199 string line = ltrim(command);
2200 string const arg = trim(split(line, cmd, ' '));
2202 return dispatch(lyxaction.LookupFunc(cmd), arg, result);
2206 bool Buffer::dispatch(int action, string const & argument, bool * result)
2208 bool dispatched = true;
2212 bool const tmp = Exporter::Export(this, argument, false);
2225 void Buffer::changeLanguage(Language const * from, Language const * to)
2227 lyxerr << "Changing Language!" << endl;
2229 // Take care of l10n/i18n
2232 ParIterator end = par_iterator_end();
2233 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2234 it->changeLanguage(params(), from, to);
2238 void Buffer::updateDocLang(Language const * nlang)
2240 pimpl_->messages.reset(new Messages(nlang->code()));
2244 bool Buffer::isMultiLingual()
2246 ParIterator end = par_iterator_end();
2247 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2248 if (it->isMultiLingual(params()))
2255 void Buffer::inset_iterator::setParagraph()
2257 while (pit != pend) {
2258 it = pit->insetlist.begin();
2259 if (it != pit->insetlist.end())
2266 InsetOld * Buffer::getInsetFromID(int id_arg) const
2268 for (inset_iterator it = inset_const_iterator_begin();
2269 it != inset_const_iterator_end(); ++it)
2271 if (it->id() == id_arg)
2273 InsetOld * in = it->getInsetFromID(id_arg);
2281 ParIterator Buffer::getParFromID(int id) const
2283 #warning FIXME: const correctness! (Andre)
2284 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2285 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2287 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2289 // John says this is called with id == -1 from undo
2290 lyxerr << "getParFromID(), id: " << id << endl;
2294 for (; it != end; ++it)
2302 bool Buffer::hasParWithID(int id) const
2304 ParConstIterator it = par_iterator_begin();
2305 ParConstIterator end = par_iterator_end();
2308 // John says this is called with id == -1 from undo
2309 lyxerr << "hasParWithID(), id: " << id << endl;
2313 for (; it != end; ++it)
2321 ParIterator Buffer::par_iterator_begin()
2323 return ParIterator(paragraphs().begin(), paragraphs());
2327 ParIterator Buffer::par_iterator_end()
2329 return ParIterator(paragraphs().end(), paragraphs());
2333 ParConstIterator Buffer::par_iterator_begin() const
2335 ParagraphList const & pars = paragraphs();
2336 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2340 ParConstIterator Buffer::par_iterator_end() const
2342 ParagraphList const & pars = paragraphs();
2343 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2347 Language const * Buffer::getLanguage() const
2349 return params().language;
2353 string const Buffer::B_(string const & l10n) const
2355 if (pimpl_->messages.get()) {
2356 return pimpl_->messages->get(l10n);
2363 bool Buffer::isClean() const
2365 return pimpl_->lyx_clean;
2369 bool Buffer::isBakClean() const
2371 return pimpl_->bak_clean;
2375 void Buffer::markClean() const
2377 if (!pimpl_->lyx_clean) {
2378 pimpl_->lyx_clean = true;
2381 // if the .lyx file has been saved, we don't need an
2383 pimpl_->bak_clean = true;
2387 void Buffer::markBakClean()
2389 pimpl_->bak_clean = true;
2393 void Buffer::setUnnamed(bool flag)
2395 pimpl_->unnamed = flag;
2399 bool Buffer::isUnnamed()
2401 return pimpl_->unnamed;
2405 void Buffer::markDirty()
2407 if (pimpl_->lyx_clean) {
2408 pimpl_->lyx_clean = false;
2411 pimpl_->bak_clean = false;
2413 DepClean::iterator it = pimpl_->dep_clean.begin();
2414 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2416 for (; it != end; ++it) {
2422 string const & Buffer::fileName() const
2424 return pimpl_->filename;
2428 string const & Buffer::filePath() const
2430 return pimpl_->filepath;
2434 bool Buffer::isReadonly() const
2436 return pimpl_->read_only;
2440 void Buffer::setParentName(string const & name)
2442 params().parentname = name;
2446 Buffer::inset_iterator::inset_iterator()
2451 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2458 Buffer::inset_iterator Buffer::inset_iterator_begin()
2460 return inset_iterator(paragraphs().begin(), paragraphs().end());
2464 Buffer::inset_iterator Buffer::inset_iterator_end()
2466 return inset_iterator(paragraphs().end(), paragraphs().end());
2470 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2472 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2473 return inset_iterator(pars.begin(), pars.end());
2477 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2479 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2480 return inset_iterator(pars.end(), pars.end());
2484 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2488 if (it == pit->insetlist.end()) {
2497 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2499 inset_iterator tmp = *this;
2505 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2511 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2517 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2523 lyx::pos_type Buffer::inset_iterator::getPos() const
2529 bool operator==(Buffer::inset_iterator const & iter1,
2530 Buffer::inset_iterator const & iter2)
2532 return iter1.pit == iter2.pit
2533 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2537 bool operator!=(Buffer::inset_iterator const & iter1,
2538 Buffer::inset_iterator const & iter2)
2540 return !(iter1 == iter2);