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_funcs.h"
36 #include "ParagraphParameters.h"
42 #include "insets/insetbibitem.h"
43 #include "insets/insetbibtex.h"
44 #include "insets/insetinclude.h"
45 #include "insets/insettext.h"
47 #include "frontends/Alert.h"
49 #include "graphics/Previews.h"
51 #include "support/FileInfo.h"
52 #include "support/filetools.h"
53 #include "support/gzstream.h"
54 #include "support/lyxlib.h"
55 #include "support/os.h"
56 #include "support/path.h"
57 #include "support/textutils.h"
58 #include "support/tostr.h"
60 #include <boost/bind.hpp>
61 #include <boost/tuple/tuple.hpp>
63 #include "support/std_sstream.h"
75 using lyx::support::AddName;
76 using lyx::support::ascii_lowercase;
77 using lyx::support::atoi;
78 using lyx::support::bformat;
79 using lyx::support::ChangeExtension;
80 using lyx::support::cmd_ret;
81 using lyx::support::compare_ascii_no_case;
82 using lyx::support::compare_no_case;
83 using lyx::support::contains;
84 using lyx::support::CreateBufferTmpDir;
85 using lyx::support::destroyDir;
86 using lyx::support::FileInfo;
87 using lyx::support::FileInfo;
88 using lyx::support::getExtFromContents;
89 using lyx::support::IsDirWriteable;
90 using lyx::support::IsFileWriteable;
91 using lyx::support::LibFileSearch;
92 using lyx::support::ltrim;
93 using lyx::support::MakeAbsPath;
94 using lyx::support::MakeDisplayPath;
95 using lyx::support::MakeLatexName;
96 using lyx::support::OnlyFilename;
97 using lyx::support::OnlyPath;
98 using lyx::support::Path;
99 using lyx::support::QuoteName;
100 using lyx::support::removeAutosaveFile;
101 using lyx::support::rename;
102 using lyx::support::RunCommand;
103 using lyx::support::split;
104 using lyx::support::strToInt;
105 using lyx::support::subst;
106 using lyx::support::tempName;
107 using lyx::support::trim;
109 namespace os = lyx::support::os;
113 using std::make_pair;
119 using std::ostringstream;
126 // all these externs should eventually be removed.
127 extern BufferList bufferlist;
131 const int LYX_FORMAT = 225;
133 bool openFileWrite(ofstream & ofs, string const & fname)
135 ofs.open(fname.c_str());
137 string const file = MakeDisplayPath(fname, 50);
138 string text = bformat(_("Could not open the specified "
139 "document\n%1$s."), file);
140 Alert::error(_("Could not open file"), text);
149 typedef std::map<string, bool> DepClean;
153 Impl(Buffer & parent, string const & file, bool readonly);
155 limited_stack<Undo> undostack;
156 limited_stack<Undo> redostack;
158 ParagraphList paragraphs;
164 /// need to regenerate .tex ?
168 mutable bool lyx_clean;
170 /// is autosave needed
171 mutable bool bak_clean;
173 /// is this a unnamed file (New...)
179 /// name of the file the buffer is associated with.
182 /// The path to the document file.
185 boost::scoped_ptr<Messages> messages;
189 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
191 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
192 filename(file), filepath(OnlyPath(file))
194 lyxvc.buffer(&parent);
195 if (readonly_ || lyxrc.use_tempdir)
196 temppath = CreateBufferTmpDir();
200 Buffer::Buffer(string const & file, bool ronly)
201 : pimpl_(new Impl(*this, file, ronly))
203 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
209 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
210 // here the buffer should take care that it is
211 // saved properly, before it goes into the void.
215 if (!temppath().empty() && destroyDir(temppath()) != 0) {
216 Alert::warning(_("Could not remove temporary directory"),
217 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
220 paragraphs().clear();
222 // Remove any previewed LaTeX snippets associated with this buffer.
223 lyx::graphics::Previews::get().removeLoader(*this);
227 limited_stack<Undo> & Buffer::undostack()
229 return pimpl_->undostack;
233 limited_stack<Undo> const & Buffer::undostack() const
235 return pimpl_->undostack;
239 limited_stack<Undo> & Buffer::redostack()
241 return pimpl_->redostack;
245 limited_stack<Undo> const & Buffer::redostack() const
247 return pimpl_->redostack;
251 BufferParams & Buffer::params()
253 return pimpl_->params;
257 BufferParams const & Buffer::params() const
259 return pimpl_->params;
263 ParagraphList & Buffer::paragraphs()
265 return pimpl_->paragraphs;
269 ParagraphList const & Buffer::paragraphs() const
271 return pimpl_->paragraphs;
275 LyXVC & Buffer::lyxvc()
277 return pimpl_->lyxvc;
281 LyXVC const & Buffer::lyxvc() const
283 return pimpl_->lyxvc;
287 string const & Buffer::temppath() const
289 return pimpl_->temppath;
293 bool & Buffer::niceFile()
295 return pimpl_->nicefile;
299 bool Buffer::niceFile() const
301 return pimpl_->nicefile;
305 TexRow & Buffer::texrow()
307 return pimpl_->texrow;
311 TexRow const & Buffer::texrow() const
313 return pimpl_->texrow;
317 string const Buffer::getLatexName(bool no_path) const
319 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
320 return no_path ? OnlyFilename(name) : name;
324 pair<Buffer::LogType, string> const Buffer::getLogName() const
326 string const filename = getLatexName(false);
328 if (filename.empty())
329 return make_pair(Buffer::latexlog, string());
331 string path = OnlyPath(filename);
333 if (lyxrc.use_tempdir || !IsDirWriteable(path))
336 string const fname = AddName(path,
337 OnlyFilename(ChangeExtension(filename,
340 AddName(path, OnlyFilename(
341 ChangeExtension(filename,
342 formats.extension("literate") + ".out")));
344 // If no Latex log or Build log is newer, show Build log
346 FileInfo const f_fi(fname);
347 FileInfo const b_fi(bname);
350 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
351 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
352 return make_pair(Buffer::buildlog, bname);
354 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
355 return make_pair(Buffer::latexlog, fname);
359 void Buffer::setReadonly(bool flag)
361 if (pimpl_->read_only != flag) {
362 pimpl_->read_only = flag;
368 void Buffer::setFileName(string const & newfile)
370 pimpl_->filename = MakeAbsPath(newfile);
371 pimpl_->filepath = OnlyPath(pimpl_->filename);
372 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
377 // We'll remove this later. (Lgb)
380 void unknownClass(string const & unknown)
382 Alert::warning(_("Unknown document class"),
383 bformat(_("Using the default document class, because the "
384 "class %1$s is unknown."), unknown));
389 int Buffer::readHeader(LyXLex & lex)
391 int unknown_tokens = 0;
395 string const token = lex.getString();
400 if (token == "\\end_header")
403 lyxerr[Debug::PARSER] << "Handling header token: `"
404 << token << '\'' << endl;
407 string unknown = params().readToken(lex, token);
408 if (!unknown.empty()) {
409 if (unknown[0] != '\\') {
410 unknownClass(unknown);
413 string const s = bformat(_("Unknown token: "
417 error(ErrorItem(_("Header error"), s,
422 return unknown_tokens;
426 // candidate for move to BufferView
427 // (at least some parts in the beginning of the func)
430 // changed to be public and have one parameter
431 // if par = 0 normal behavior
432 // else insert behavior
433 // Returns false if "\end_document" is not read (Asger)
434 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
436 Paragraph::depth_type depth = 0;
437 bool the_end_read = false;
439 if (paragraphs().empty()) {
441 if (!params().getLyXTextClass().load()) {
442 string theclass = params().getLyXTextClass().name();
443 Alert::error(_("Can't load document class"), bformat(
444 "Using the default document class, because the "
445 " class %1$s could not be loaded.", theclass));
446 params().textclass = 0;
449 // We don't want to adopt the parameters from the
450 // document we insert, so read them into a temporary buffer
451 // and then discard it
453 Buffer tmpbuf("", false);
454 tmpbuf.readHeader(lex);
459 string const token = lex.getString();
464 lyxerr[Debug::PARSER] << "Handling token: `"
465 << token << '\'' << endl;
467 if (token == "\\end_document") {
472 readParagraph(lex, token, paragraphs(), pit, depth);
479 int Buffer::readParagraph(LyXLex & lex, string const & token,
480 ParagraphList & pars, ParagraphList::iterator & pit,
481 Paragraph::depth_type & depth)
483 static Change current_change;
486 if (token == "\\begin_layout") {
487 lex.pushToken(token);
490 par.params().depth(depth);
491 if (params().tracking_changes)
493 LyXFont f(LyXFont::ALL_INHERIT, params().language);
497 if (pit != pars.end())
500 pit = pars.insert(pit, par);
502 // FIXME: goddamn InsetTabular makes us pass a Buffer
504 ::readParagraph(*this, *pit, lex);
506 } else if (token == "\\begin_deeper") {
508 } else if (token == "\\end_deeper") {
510 lex.printError("\\end_deeper: " "depth is already null");
521 // needed to insert the selection
522 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
523 LyXFont const & fn,string const & str)
525 LyXLayout_ptr const & layout = par->layout();
529 par->checkInsertChar(font);
530 // insert the string, don't insert doublespace
531 bool space_inserted = true;
532 bool autobreakrows = !par->inInset() ||
533 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
534 for(string::const_iterator cit = str.begin();
535 cit != str.end(); ++cit) {
537 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
538 breakParagraph(params(), paragraphs(), par, pos,
539 layout->isEnvironment());
542 space_inserted = true;
546 // do not insert consecutive spaces if !free_spacing
547 } else if ((*cit == ' ' || *cit == '\t') &&
548 space_inserted && !par->isFreeSpacing()) {
550 } else if (*cit == '\t') {
551 if (!par->isFreeSpacing()) {
552 // tabs are like spaces here
553 par->insertChar(pos, ' ', font);
555 space_inserted = true;
557 const pos_type nb = 8 - pos % 8;
558 for (pos_type a = 0; a < nb ; ++a) {
559 par->insertChar(pos, ' ', font);
562 space_inserted = true;
564 } else if (!IsPrintable(*cit)) {
565 // Ignore unprintables
568 // just insert the character
569 par->insertChar(pos, *cit, font);
571 space_inserted = (*cit == ' ');
578 bool Buffer::readFile(string const & filename)
580 // Check if the file is compressed.
581 string const format = getExtFromContents(filename);
582 if (format == "gzip" || format == "zip" || format == "compress") {
583 params().compressed = true;
586 bool ret = readFile(filename, paragraphs().begin());
588 // After we have read a file, we must ensure that the buffer
589 // language is set and used in the gui.
590 // If you know of a better place to put this, please tell me. (Lgb)
591 updateDocLang(params().language);
597 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
600 lex.setFile(filename);
602 return readFile(lex, filename, pit);
606 bool Buffer::readFile(LyXLex & lex, string const & filename,
607 ParagraphList::iterator pit)
609 BOOST_ASSERT(!filename.empty());
612 Alert::error(_("Document could not be read"),
613 bformat(_("%1$s could not be read."), filename));
618 string const token(lex.getString());
621 Alert::error(_("Document could not be read"),
622 bformat(_("%1$s could not be read."), filename));
626 // the first token _must_ be...
627 if (token != "\\lyxformat") {
628 lyxerr << "Token: " << token << endl;
630 Alert::error(_("Document format failure"),
631 bformat(_("%1$s is not a LyX document."),
637 string tmp_format = lex.getString();
638 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
639 // if present remove ".," from string.
640 string::size_type dot = tmp_format.find_first_of(".,");
641 //lyxerr << " dot found at " << dot << endl;
642 if (dot != string::npos)
643 tmp_format.erase(dot, 1);
644 int file_format = strToInt(tmp_format);
645 //lyxerr << "format: " << file_format << endl;
647 if (file_format > LYX_FORMAT) {
648 Alert::warning(_("Document format failure"),
649 bformat(_("%1$s was created with a newer"
650 " version of LyX. This is likely to"
653 } else if (file_format < LYX_FORMAT) {
654 string const tmpfile = tempName();
655 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
656 if (command.empty()) {
657 Alert::error(_("Conversion script not found"),
658 bformat(_("%1$s is from an earlier"
659 " version of LyX, but the"
660 " conversion script lyx2lyx"
661 " could not be found."),
667 + " -o " + tmpfile + ' '
668 + QuoteName(filename);
669 lyxerr[Debug::INFO] << "Running '"
672 cmd_ret const ret = RunCommand(command);
673 if (ret.first != 0) {
674 Alert::error(_("Conversion script failed"),
675 bformat(_("%1$s is from an earlier version"
676 " of LyX, but the lyx2lyx script"
677 " failed to convert it."),
681 bool ret = readFile(tmpfile, pit);
682 // Do stuff with tmpfile name and buffer name here.
688 bool the_end = readBody(lex, pit);
689 params().setPaperStuff();
692 Alert::error(_("Document format failure"),
693 bformat(_("%1$s ended unexpectedly, which means"
694 " that it is probably corrupted."),
701 // Should probably be moved to somewhere else: BufferView? LyXView?
702 bool Buffer::save() const
704 // We don't need autosaves in the immediate future. (Asger)
705 resetAutosaveTimers();
709 if (lyxrc.make_backup) {
710 s = fileName() + '~';
711 if (!lyxrc.backupdir_path.empty())
712 s = AddName(lyxrc.backupdir_path,
713 subst(os::slashify_path(s),'/','!'));
715 // Rename is the wrong way of making a backup,
716 // this is the correct way.
717 /* truss cp fil fil2:
718 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
719 stat("LyXVC.lyx", 0xEFFFF688) = 0
720 open("LyXVC.lyx", O_RDONLY) = 3
721 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
722 fstat(4, 0xEFFFF508) = 0
723 fstat(3, 0xEFFFF508) = 0
724 read(3, " # T h i s f i l e w".., 8192) = 5579
725 write(4, " # T h i s f i l e w".., 5579) = 5579
726 read(3, 0xEFFFD4A0, 8192) = 0
729 chmod("LyXVC3.lyx", 0100644) = 0
730 lseek(0, 0, SEEK_CUR) = 46440
734 // Should probably have some more error checking here.
735 // Doing it this way, also makes the inodes stay the same.
736 // This is still not a very good solution, in particular we
737 // might loose the owner of the backup.
738 FileInfo finfo(fileName());
740 mode_t fmode = finfo.getMode();
741 struct utimbuf times = {
742 finfo.getAccessTime(),
743 finfo.getModificationTime() };
745 ifstream ifs(fileName().c_str());
746 ofstream ofs(s.c_str(), ios::out|ios::trunc);
751 ::chmod(s.c_str(), fmode);
753 if (::utime(s.c_str(), ×)) {
754 lyxerr << "utime error." << endl;
757 lyxerr << "LyX was not able to make "
758 "backup copy. Beware." << endl;
763 if (writeFile(fileName())) {
765 removeAutosaveFile(fileName());
767 // Saving failed, so backup is not backup
768 if (lyxrc.make_backup) {
769 rename(s, fileName());
777 bool Buffer::writeFile(string const & fname) const
779 if (pimpl_->read_only && (fname == fileName())) {
783 FileInfo finfo(fname);
784 if (finfo.exist() && !finfo.writable()) {
790 if (params().compressed) {
791 gz::ogzstream ofs(fname.c_str());
796 retval = do_writeFile(ofs);
799 ofstream ofs(fname.c_str());
803 retval = do_writeFile(ofs);
810 bool Buffer::do_writeFile(ostream & ofs) const
814 // Use the standard "C" locale for file output.
815 ofs.imbue(std::locale::classic());
818 // The top of the file should not be written by params().
820 // write out a comment in the top of the file
821 ofs << "#LyX " << lyx_version
822 << " created this file. For more info see http://www.lyx.org/\n"
823 << "\\lyxformat " << LYX_FORMAT << "\n";
825 // now write out the buffer paramters.
826 params().writeFile(ofs);
828 ofs << "\\end_header\n";
830 Paragraph::depth_type depth = 0;
832 // this will write out all the paragraphs
833 // using recursive descent.
834 ParagraphList::const_iterator pit = paragraphs().begin();
835 ParagraphList::const_iterator pend = paragraphs().end();
836 for (; pit != pend; ++pit)
837 pit->write(*this, ofs, params(), depth);
839 // Write marker that shows file is complete
840 ofs << "\n\\end_document" << endl;
842 // Shouldn't really be needed....
845 // how to check if close went ok?
846 // Following is an attempt... (BE 20001011)
848 // good() returns false if any error occured, including some
850 // bad() returns true if something bad happened in the buffer,
851 // which should include file system full errors.
858 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
860 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
872 pair<int, string> const addDepth(int depth, int ldepth)
876 d += (ldepth - depth) * 2;
877 return make_pair(d, string(d, ' '));
883 string const Buffer::asciiParagraph(Paragraph const & par,
884 unsigned int linelen,
885 bool noparbreak) const
887 ostringstream buffer;
889 Paragraph::depth_type ltype_depth = 0;
890 bool ref_printed = false;
891 Paragraph::depth_type depth = par.params().depth();
893 // First write the layout
894 string const & tmp = par.layout()->name();
895 if (compare_no_case(tmp, "itemize") == 0) {
897 ltype_depth = depth + 1;
898 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
900 ltype_depth = depth + 1;
901 } else if (contains(ascii_lowercase(tmp), "ection")) {
903 ltype_depth = depth + 1;
904 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
906 ltype_depth = depth + 1;
907 } else if (compare_ascii_no_case(tmp, "description") == 0) {
909 ltype_depth = depth + 1;
910 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
913 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
921 /* maybe some vertical spaces */
923 /* the labelwidthstring used in lists */
927 /* some pagebreaks? */
931 /* what about the alignment */
933 // linelen <= 0 is special and means we don't have paragraph breaks
935 string::size_type currlinelen = 0;
941 buffer << string(depth * 2, ' ');
942 currlinelen += depth * 2;
945 // we should probably change to the paragraph language in the
946 // gettext here (if possible) so that strings are outputted in
947 // the correct language! (20012712 Jug)
951 case 4: // (Sub)Paragraph
952 case 5: // Description
956 buffer << _("Abstract") << "\n\n";
959 string const abst = _("Abstract: ");
961 currlinelen += abst.length();
964 case 7: // Bibliography
967 buffer << _("References") << "\n\n";
970 string const refs = _("References: ");
972 currlinelen += refs.length();
980 string const parlab = par.params().labelString();
981 buffer << parlab << ' ';
982 currlinelen += parlab.length() + 1;
990 pair<int, string> p = addDepth(depth, ltype_depth);
992 currlinelen += p.first;
995 // this is to change the linebreak to do it by word a bit more
996 // intelligent hopefully! (only in the case where we have a
997 // max linelength!) (Jug)
1001 for (pos_type i = 0; i < par.size(); ++i) {
1002 char c = par.getUChar(params(), i);
1004 case Paragraph::META_INSET:
1006 InsetOld const * inset = par.getInset(i);
1010 currlinelen += word.length();
1013 if (inset->ascii(*this, buffer, linelen)) {
1014 // to be sure it breaks paragraph
1015 currlinelen += linelen;
1024 currlinelen + word.length() > linelen - 10) {
1026 pair<int, string> p = addDepth(depth, ltype_depth);
1028 currlinelen = p.first;
1031 buffer << word << ' ';
1032 currlinelen += word.length() + 1;
1039 lyxerr[Debug::INFO] <<
1040 "writeAsciiFile: NULL char in structure." << endl;
1042 if ((linelen > 0) &&
1043 (currlinelen + word.length()) > linelen)
1047 pair<int, string> p =
1048 addDepth(depth, ltype_depth);
1050 currlinelen = p.first;
1057 return buffer.str();
1061 void Buffer::writeFileAscii(string const & fname, int linelen)
1064 if (!::openFileWrite(ofs, fname))
1066 writeFileAscii(ofs, linelen);
1070 void Buffer::writeFileAscii(ostream & os, int linelen)
1072 ParagraphList::iterator beg = paragraphs().begin();
1073 ParagraphList::iterator end = paragraphs().end();
1074 ParagraphList::iterator it = beg;
1075 for (; it != end; ++it) {
1076 os << asciiParagraph(*it, linelen, it == beg);
1082 void Buffer::makeLaTeXFile(string const & fname,
1083 string const & original_path,
1084 LatexRunParams const & runparams,
1085 bool output_preamble, bool output_body)
1087 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1090 if (!::openFileWrite(ofs, fname))
1093 makeLaTeXFile(ofs, original_path,
1094 runparams, output_preamble, output_body);
1098 lyxerr << "File was not closed properly." << endl;
1103 void Buffer::makeLaTeXFile(ostream & os,
1104 string const & original_path,
1105 LatexRunParams const & runparams_in,
1106 bool output_preamble, bool output_body)
1108 LatexRunParams runparams = runparams_in;
1109 niceFile() = runparams.nice; // this will be used by Insetincludes.
1111 // validate the buffer.
1112 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1113 LaTeXFeatures features(params());
1115 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1118 // The starting paragraph of the coming rows is the
1119 // first paragraph of the document. (Asger)
1120 texrow().start(paragraphs().begin()->id(), 0);
1122 if (output_preamble && runparams.nice) {
1123 os << "%% LyX " << lyx_version << " created this file. "
1124 "For more info, see http://www.lyx.org/.\n"
1125 "%% Do not edit unless you really know what "
1130 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1131 // There are a few differences between nice LaTeX and usual files:
1132 // usual is \batchmode and has a
1133 // special input@path to allow the including of figures
1134 // with either \input or \includegraphics (what figinsets do).
1135 // input@path is set when the actual parameter
1136 // original_path is set. This is done for usual tex-file, but not
1137 // for nice-latex-file. (Matthias 250696)
1138 if (output_preamble) {
1139 if (!runparams.nice) {
1140 // code for usual, NOT nice-latex-file
1141 os << "\\batchmode\n"; // changed
1142 // from \nonstopmode
1145 if (!original_path.empty()) {
1146 string inputpath = os::external_path(original_path);
1147 subst(inputpath, "~", "\\string~");
1148 os << "\\makeatletter\n"
1149 << "\\def\\input@path{{"
1150 << inputpath << "/}}\n"
1151 << "\\makeatother\n";
1157 // Write the preamble
1158 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1164 os << "\\begin{document}\n";
1166 } // output_preamble
1167 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1169 if (!lyxrc.language_auto_begin) {
1170 os << subst(lyxrc.language_command_begin, "$$lang",
1171 params().language->babel())
1176 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1178 // add this just in case after all the paragraphs
1182 if (!lyxrc.language_auto_end) {
1183 os << subst(lyxrc.language_command_end, "$$lang",
1184 params().language->babel())
1189 if (output_preamble) {
1190 os << "\\end{document}\n";
1193 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1195 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1199 // Just to be sure. (Asger)
1202 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1203 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1206 // we want this to be true outside previews (for insetexternal)
1211 bool Buffer::isLatex() const
1213 return params().getLyXTextClass().outputType() == LATEX;
1217 bool Buffer::isLinuxDoc() const
1219 return params().getLyXTextClass().outputType() == LINUXDOC;
1223 bool Buffer::isLiterate() const
1225 return params().getLyXTextClass().outputType() == LITERATE;
1229 bool Buffer::isDocBook() const
1231 return params().getLyXTextClass().outputType() == DOCBOOK;
1235 bool Buffer::isSGML() const
1237 LyXTextClass const & tclass = params().getLyXTextClass();
1239 return tclass.outputType() == LINUXDOC ||
1240 tclass.outputType() == DOCBOOK;
1244 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1247 if (!::openFileWrite(ofs, fname))
1250 niceFile() = nice; // this will be used by included files.
1252 LaTeXFeatures features(params());
1258 LyXTextClass const & tclass = params().getLyXTextClass();
1260 string top_element = tclass.latexname();
1263 ofs << "<!doctype linuxdoc system";
1265 string preamble = params().preamble;
1266 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1268 preamble += features.getIncludedFiles(name);
1269 preamble += features.getLyXSGMLEntities();
1271 if (!preamble.empty()) {
1272 ofs << " [ " << preamble << " ]";
1276 if (params().options.empty())
1277 sgml::openTag(ofs, 0, false, top_element);
1279 string top = top_element;
1281 top += params().options;
1282 sgml::openTag(ofs, 0, false, top);
1286 ofs << "<!-- LyX " << lyx_version
1287 << " created this file. For more info see http://www.lyx.org/"
1290 Paragraph::depth_type depth = 0; // paragraph depth
1292 vector<string> environment_stack(5);
1294 ParagraphList::iterator pit = paragraphs().begin();
1295 ParagraphList::iterator pend = paragraphs().end();
1296 for (; pit != pend; ++pit) {
1297 LyXLayout_ptr const & style = pit->layout();
1298 // treat <toc> as a special case for compatibility with old code
1299 if (pit->isInset(0)) {
1300 InsetOld * inset = pit->getInset(0);
1301 InsetOld::Code lyx_code = inset->lyxCode();
1302 if (lyx_code == InsetOld::TOC_CODE) {
1303 string const temp = "toc";
1304 sgml::openTag(ofs, depth, false, temp);
1309 // environment tag closing
1310 for (; depth > pit->params().depth(); --depth) {
1311 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1312 environment_stack[depth].erase();
1315 // write opening SGML tags
1316 switch (style->latextype) {
1317 case LATEX_PARAGRAPH:
1318 if (depth == pit->params().depth()
1319 && !environment_stack[depth].empty()) {
1320 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1321 environment_stack[depth].erase();
1327 sgml::openTag(ofs, depth, false, style->latexname());
1332 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1334 if (!environment_stack[depth].empty()) {
1335 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1339 environment_stack[depth].erase();
1340 sgml::openTag(ofs, depth, false, style->latexname());
1343 case LATEX_ENVIRONMENT:
1344 case LATEX_ITEM_ENVIRONMENT:
1345 case LATEX_BIB_ENVIRONMENT:
1347 string const & latexname = style->latexname();
1349 if (depth == pit->params().depth()
1350 && environment_stack[depth] != latexname) {
1351 sgml::closeTag(ofs, depth, false,
1352 environment_stack[depth]);
1353 environment_stack[depth].erase();
1355 if (depth < pit->params().depth()) {
1356 depth = pit->params().depth();
1357 environment_stack[depth].erase();
1359 if (environment_stack[depth] != latexname) {
1361 sgml::openTag(ofs, depth, false, "p");
1363 sgml::openTag(ofs, depth, false, latexname);
1365 if (environment_stack.size() == depth + 1)
1366 environment_stack.push_back("!-- --");
1367 environment_stack[depth] = latexname;
1370 if (style->latexparam() == "CDATA")
1373 if (style->latextype == LATEX_ENVIRONMENT) break;
1375 if (style->labeltype == LABEL_MANUAL)
1380 sgml::openTag(ofs, depth + 1, false, item_name);
1385 sgml::openTag(ofs, depth, false, style->latexname());
1389 simpleLinuxDocOnePar(ofs, pit, depth);
1392 // write closing SGML tags
1393 switch (style->latextype) {
1396 case LATEX_ENVIRONMENT:
1397 case LATEX_ITEM_ENVIRONMENT:
1398 case LATEX_BIB_ENVIRONMENT:
1399 if (style->latexparam() == "CDATA")
1403 sgml::closeTag(ofs, depth, false, style->latexname());
1409 for (int i = depth; i >= 0; --i)
1410 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1414 sgml::closeTag(ofs, 0, false, top_element);
1418 // How to check for successful close
1420 // we want this to be true outside previews (for insetexternal)
1425 // checks, if newcol chars should be put into this line
1426 // writes newline, if necessary.
1429 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1430 string::size_type newcol)
1433 if (colcount > lyxrc.ascii_linelen) {
1435 colcount = newcol; // assume write after this call
1450 string tag_name(PAR_TAG const & pt) {
1452 case NONE: return "!-- --";
1453 case TT: return "tt";
1454 case SF: return "sf";
1455 case BF: return "bf";
1456 case IT: return "it";
1457 case SL: return "sl";
1458 case EM: return "em";
1465 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1467 p1 = static_cast<PAR_TAG>(p1 | p2);
1472 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1474 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1480 // Handle internal paragraph parsing -- layout already processed.
1481 void Buffer::simpleLinuxDocOnePar(ostream & os,
1482 ParagraphList::iterator par,
1483 Paragraph::depth_type /*depth*/) const
1485 LyXLayout_ptr const & style = par->layout();
1487 string::size_type char_line_count = 5; // Heuristic choice ;-)
1489 // gets paragraph main font
1492 if (style->labeltype == LABEL_MANUAL) {
1493 font_old = style->labelfont;
1496 font_old = style->font;
1500 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1501 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1502 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1505 stack<PAR_TAG> tag_state;
1506 // parsing main loop
1507 for (pos_type i = 0; i < par->size(); ++i) {
1509 PAR_TAG tag_close = NONE;
1510 list < PAR_TAG > tag_open;
1512 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1514 if (font_old.family() != font.family()) {
1515 switch (family_type) {
1516 case LyXFont::SANS_FAMILY:
1519 case LyXFont::TYPEWRITER_FAMILY:
1526 family_type = font.family();
1528 switch (family_type) {
1529 case LyXFont::SANS_FAMILY:
1530 tag_open.push_back(SF);
1532 case LyXFont::TYPEWRITER_FAMILY:
1533 tag_open.push_back(TT);
1540 if (font_old.series() != font.series()) {
1541 switch (series_type) {
1542 case LyXFont::BOLD_SERIES:
1549 series_type = font.series();
1551 switch (series_type) {
1552 case LyXFont::BOLD_SERIES:
1553 tag_open.push_back(BF);
1561 if (font_old.shape() != font.shape()) {
1562 switch (shape_type) {
1563 case LyXFont::ITALIC_SHAPE:
1566 case LyXFont::SLANTED_SHAPE:
1573 shape_type = font.shape();
1575 switch (shape_type) {
1576 case LyXFont::ITALIC_SHAPE:
1577 tag_open.push_back(IT);
1579 case LyXFont::SLANTED_SHAPE:
1580 tag_open.push_back(SL);
1587 if (font_old.emph() != font.emph()) {
1588 if (font.emph() == LyXFont::ON) {
1589 tag_open.push_back(EM);
1598 list < PAR_TAG > temp;
1599 while (!tag_state.empty() && tag_close) {
1600 PAR_TAG k = tag_state.top();
1602 os << "</" << tag_name(k) << '>';
1609 for(list< PAR_TAG >::const_iterator j = temp.begin();
1610 j != temp.end(); ++j) {
1612 os << '<' << tag_name(*j) << '>';
1615 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1616 j != tag_open.end(); ++j) {
1618 os << '<' << tag_name(*j) << '>';
1621 char c = par->getChar(i);
1623 if (c == Paragraph::META_INSET) {
1624 InsetOld * inset = par->getInset(i);
1625 inset->linuxdoc(*this, os);
1630 if (style->latexparam() == "CDATA") {
1631 // "TeX"-Mode on == > SGML-Mode on.
1638 boost::tie(ws, str) = sgml::escapeChar(c);
1639 if (ws && !par->isFreeSpacing()) {
1640 // in freespacing mode, spaces are
1641 // non-breaking characters
1642 if (desc_on) {// if char is ' ' then...
1645 sgmlLineBreak(os, char_line_count, 6);
1649 sgmlLineBreak(os, char_line_count, 1);
1654 char_line_count += str.length();
1660 while (!tag_state.empty()) {
1661 os << "</" << tag_name(tag_state.top()) << '>';
1665 // resets description flag correctly
1667 // <tag> not closed...
1668 sgmlLineBreak(os, char_line_count, 6);
1674 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1677 if (!::openFileWrite(ofs, fname))
1680 niceFile() = nice; // this will be used by Insetincludes.
1682 LaTeXFeatures features(params());
1687 LyXTextClass const & tclass = params().getLyXTextClass();
1688 string top_element = tclass.latexname();
1691 ofs << "<!DOCTYPE " << top_element
1692 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1694 string preamble = params().preamble;
1695 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1697 preamble += features.getIncludedFiles(name);
1698 preamble += features.getLyXSGMLEntities();
1700 if (!preamble.empty()) {
1701 ofs << "\n [ " << preamble << " ]";
1706 string top = top_element;
1708 top += params().language->code();
1711 if (!params().options.empty()) {
1713 top += params().options;
1715 sgml::openTag(ofs, 0, false, top);
1717 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1718 << "\n See http://www.lyx.org/ for more information -->\n";
1720 vector<string> environment_stack(10);
1721 vector<string> environment_inner(10);
1722 vector<string> command_stack(10);
1724 bool command_flag = false;
1725 Paragraph::depth_type command_depth = 0;
1726 Paragraph::depth_type command_base = 0;
1727 Paragraph::depth_type cmd_depth = 0;
1728 Paragraph::depth_type depth = 0; // paragraph depth
1731 string command_name;
1733 ParagraphList::iterator par = paragraphs().begin();
1734 ParagraphList::iterator pend = paragraphs().end();
1736 for (; par != pend; ++par) {
1740 int desc_on = 0; // description mode
1742 LyXLayout_ptr const & style = par->layout();
1744 // environment tag closing
1745 for (; depth > par->params().depth(); --depth) {
1746 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1747 item_name = "listitem";
1748 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1749 if (environment_inner[depth] == "varlistentry")
1750 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1752 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1753 environment_stack[depth].erase();
1754 environment_inner[depth].erase();
1757 if (depth == par->params().depth()
1758 && environment_stack[depth] != style->latexname()
1759 && !environment_stack[depth].empty()) {
1760 if (environment_inner[depth] != "!-- --") {
1761 item_name= "listitem";
1762 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1763 if (environment_inner[depth] == "varlistentry")
1764 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1767 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1769 environment_stack[depth].erase();
1770 environment_inner[depth].erase();
1773 // Write opening SGML tags.
1774 switch (style->latextype) {
1775 case LATEX_PARAGRAPH:
1776 sgml::openTag(ofs, depth + command_depth,
1777 false, style->latexname());
1782 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1784 command_name = style->latexname();
1786 sgmlparam = style->latexparam();
1787 c_params = split(sgmlparam, c_depth,'|');
1789 cmd_depth = atoi(c_depth);
1792 if (cmd_depth < command_base) {
1793 for (Paragraph::depth_type j = command_depth;
1794 j >= command_base; --j) {
1795 sgml::closeTag(ofs, j, false, command_stack[j]);
1798 command_depth = command_base = cmd_depth;
1799 } else if (cmd_depth <= command_depth) {
1800 for (int j = command_depth;
1801 j >= int(cmd_depth); --j) {
1802 sgml::closeTag(ofs, j, false, command_stack[j]);
1805 command_depth = cmd_depth;
1807 command_depth = cmd_depth;
1809 command_depth = command_base = cmd_depth;
1810 command_flag = true;
1812 if (command_stack.size() == command_depth + 1)
1813 command_stack.push_back(string());
1814 command_stack[command_depth] = command_name;
1816 // treat label as a special case for
1817 // more WYSIWYM handling.
1818 // This is a hack while paragraphs can't have
1819 // attributes, like id in this case.
1820 if (par->isInset(0)) {
1821 InsetOld * inset = par->getInset(0);
1822 InsetOld::Code lyx_code = inset->lyxCode();
1823 if (lyx_code == InsetOld::LABEL_CODE) {
1824 command_name += " id=\"";
1825 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1826 command_name += '"';
1831 sgml::openTag(ofs, depth + command_depth, false, command_name);
1833 item_name = c_params.empty() ? "title" : c_params;
1834 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1837 case LATEX_ENVIRONMENT:
1838 case LATEX_ITEM_ENVIRONMENT:
1839 if (depth < par->params().depth()) {
1840 depth = par->params().depth();
1841 environment_stack[depth].erase();
1844 if (environment_stack[depth] != style->latexname()) {
1845 if (environment_stack.size() == depth + 1) {
1846 environment_stack.push_back("!-- --");
1847 environment_inner.push_back("!-- --");
1849 environment_stack[depth] = style->latexname();
1850 environment_inner[depth] = "!-- --";
1851 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1853 if (environment_inner[depth] != "!-- --") {
1854 item_name= "listitem";
1855 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1856 if (environment_inner[depth] == "varlistentry")
1857 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1861 if (style->latextype == LATEX_ENVIRONMENT) {
1862 if (!style->latexparam().empty()) {
1863 if (style->latexparam() == "CDATA")
1866 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1871 desc_on = (style->labeltype == LABEL_MANUAL);
1873 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1874 sgml::openTag(ofs, depth + 1 + command_depth,
1875 false, environment_inner[depth]);
1877 item_name = desc_on ? "term" : "para";
1878 sgml::openTag(ofs, depth + 1 + command_depth,
1882 sgml::openTag(ofs, depth + command_depth,
1883 false, style->latexname());
1887 simpleDocBookOnePar(ofs, par, desc_on,
1888 depth + 1 + command_depth);
1891 // write closing SGML tags
1892 switch (style->latextype) {
1894 end_tag = c_params.empty() ? "title" : c_params;
1895 sgml::closeTag(ofs, depth + command_depth,
1898 case LATEX_ENVIRONMENT:
1899 if (!style->latexparam().empty()) {
1900 if (style->latexparam() == "CDATA")
1903 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1906 case LATEX_ITEM_ENVIRONMENT:
1907 if (desc_on == 1) break;
1909 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1911 case LATEX_PARAGRAPH:
1912 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1915 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1921 for (int d = depth; d >= 0; --d) {
1922 if (!environment_stack[depth].empty()) {
1923 if (environment_inner[depth] != "!-- --") {
1924 item_name = "listitem";
1925 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1926 if (environment_inner[depth] == "varlistentry")
1927 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1930 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1934 for (int j = command_depth; j >= 0 ; --j)
1935 if (!command_stack[j].empty()) {
1936 sgml::closeTag(ofs, j, false, command_stack[j]);
1941 sgml::closeTag(ofs, 0, false, top_element);
1944 // How to check for successful close
1946 // we want this to be true outside previews (for insetexternal)
1951 void Buffer::simpleDocBookOnePar(ostream & os,
1952 ParagraphList::iterator par, int & desc_on,
1953 Paragraph::depth_type depth) const
1955 bool emph_flag = false;
1957 LyXLayout_ptr const & style = par->layout();
1959 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1961 int char_line_count = depth;
1962 //if (!style.free_spacing)
1963 // os << string(depth,' ');
1965 // parsing main loop
1966 for (pos_type i = 0; i < par->size(); ++i) {
1967 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1969 // handle <emphasis> tag
1970 if (font_old.emph() != font.emph()) {
1971 if (font.emph() == LyXFont::ON) {
1972 if (style->latexparam() == "CDATA")
1975 if (style->latexparam() == "CDATA")
1979 if (style->latexparam() == "CDATA")
1981 os << "</emphasis>";
1982 if (style->latexparam() == "CDATA")
1989 if (par->isInset(i)) {
1990 InsetOld * inset = par->getInset(i);
1991 // don't print the inset in position 0 if desc_on == 3 (label)
1992 if (i || desc_on != 3) {
1993 if (style->latexparam() == "CDATA")
1995 inset->docbook(*this, os, false);
1996 if (style->latexparam() == "CDATA")
2000 char c = par->getChar(i);
2003 boost::tie(ws, str) = sgml::escapeChar(c);
2005 if (style->pass_thru) {
2007 } else if (par->isFreeSpacing() || c != ' ') {
2009 } else if (desc_on == 1) {
2011 os << "\n</term><listitem><para>";
2021 if (style->latexparam() == "CDATA")
2023 os << "</emphasis>";
2024 if (style->latexparam() == "CDATA")
2028 // resets description flag correctly
2030 // <term> not closed...
2031 os << "</term>\n<listitem><para> </para>";
2033 if (style->free_spacing)
2038 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2039 // Other flags: -wall -v0 -x
2040 int Buffer::runChktex()
2044 // get LaTeX-Filename
2045 string const name = getLatexName();
2046 string path = filePath();
2048 string const org_path = path;
2049 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2053 Path p(path); // path to LaTeX file
2054 message(_("Running chktex..."));
2056 // Generate the LaTeX file if neccessary
2057 LatexRunParams runparams;
2058 runparams.flavor = LatexRunParams::LATEX;
2059 runparams.nice = false;
2060 makeLaTeXFile(name, org_path, runparams);
2063 Chktex chktex(lyxrc.chktex_command, name, filePath());
2064 int res = chktex.run(terr); // run chktex
2067 Alert::error(_("chktex failure"),
2068 _("Could not run chktex successfully."));
2069 } else if (res > 0) {
2070 // Insert all errors as errors boxes
2071 bufferErrors(*this, terr);
2080 void Buffer::validate(LaTeXFeatures & features) const
2082 LyXTextClass const & tclass = params().getLyXTextClass();
2084 if (params().tracking_changes) {
2085 features.require("dvipost");
2086 features.require("color");
2089 // AMS Style is at document level
2090 if (params().use_amsmath == BufferParams::AMS_ON
2091 || tclass.provides(LyXTextClass::amsmath))
2092 features.require("amsmath");
2094 for_each(paragraphs().begin(), paragraphs().end(),
2095 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2097 // the bullet shapes are buffer level not paragraph level
2098 // so they are tested here
2099 for (int i = 0; i < 4; ++i) {
2100 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2101 int const font = params().user_defined_bullet(i).getFont();
2103 int const c = params()
2104 .user_defined_bullet(i)
2111 features.require("latexsym");
2113 } else if (font == 1) {
2114 features.require("amssymb");
2115 } else if ((font >= 2 && font <= 5)) {
2116 features.require("pifont");
2121 if (lyxerr.debugging(Debug::LATEX)) {
2122 features.showStruct();
2127 void Buffer::getLabelList(std::vector<string> & list) const
2129 /// if this is a child document and the parent is already loaded
2130 /// Use the parent's list instead [ale990407]
2131 if (!params().parentname.empty()
2132 && bufferlist.exists(params().parentname)) {
2133 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2135 tmp->getLabelList(list);
2140 for (inset_iterator it = inset_const_iterator_begin();
2141 it != inset_const_iterator_end(); ++it) {
2142 it->getLabelList(list);
2147 // This is also a buffer property (ale)
2148 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2150 /// if this is a child document and the parent is already loaded
2151 /// use the parent's list instead [ale990412]
2152 if (!params().parentname.empty() &&
2153 bufferlist.exists(params().parentname)) {
2154 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2156 tmp->fillWithBibKeys(keys);
2161 for (inset_iterator it = inset_const_iterator_begin();
2162 it != inset_const_iterator_end(); ++it) {
2163 if (it->lyxCode() == InsetOld::BIBTEX_CODE)
2164 static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
2165 else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
2166 static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
2167 else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2168 InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
2169 string const key = bib.getContents();
2170 string const opt = bib.getOptions();
2171 string const ref; // = pit->asString(this, false);
2172 string const info = opt + "TheBibliographyRef" + ref;
2173 keys.push_back(pair<string, string>(key, info));
2179 bool Buffer::isDepClean(string const & name) const
2181 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2182 if (it == pimpl_->dep_clean.end())
2188 void Buffer::markDepClean(string const & name)
2190 pimpl_->dep_clean[name] = true;
2194 bool Buffer::dispatch(string const & command, bool * result)
2196 // Split command string into command and argument
2198 string line = ltrim(command);
2199 string const arg = trim(split(line, cmd, ' '));
2201 return dispatch(lyxaction.LookupFunc(cmd), arg, result);
2205 bool Buffer::dispatch(int action, string const & argument, bool * result)
2207 bool dispatched = true;
2211 bool const tmp = Exporter::Export(this, argument, false);
2224 void Buffer::changeLanguage(Language const * from, Language const * to)
2226 lyxerr << "Changing Language!" << endl;
2228 // Take care of l10n/i18n
2231 ParIterator end = par_iterator_end();
2232 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2233 it->changeLanguage(params(), from, to);
2237 void Buffer::updateDocLang(Language const * nlang)
2239 pimpl_->messages.reset(new Messages(nlang->code()));
2243 bool Buffer::isMultiLingual()
2245 ParIterator end = par_iterator_end();
2246 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2247 if (it->isMultiLingual(params()))
2254 void Buffer::inset_iterator::setParagraph()
2256 while (pit != pend) {
2257 it = pit->insetlist.begin();
2258 if (it != pit->insetlist.end())
2265 InsetOld * Buffer::getInsetFromID(int id_arg) const
2267 for (inset_iterator it = inset_const_iterator_begin();
2268 it != inset_const_iterator_end(); ++it)
2270 if (it->id() == id_arg)
2272 InsetOld * in = it->getInsetFromID(id_arg);
2280 ParIterator Buffer::getParFromID(int id) const
2282 #warning FIXME: const correctness! (Andre)
2283 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2284 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2286 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2288 // John says this is called with id == -1 from undo
2289 lyxerr << "getParFromID(), id: " << id << endl;
2293 for (; it != end; ++it)
2301 bool Buffer::hasParWithID(int id) const
2303 ParConstIterator it = par_iterator_begin();
2304 ParConstIterator end = par_iterator_end();
2307 // John says this is called with id == -1 from undo
2308 lyxerr << "hasParWithID(), id: " << id << endl;
2312 for (; it != end; ++it)
2320 ParIterator Buffer::par_iterator_begin()
2322 return ParIterator(paragraphs().begin(), paragraphs());
2326 ParIterator Buffer::par_iterator_end()
2328 return ParIterator(paragraphs().end(), paragraphs());
2332 ParConstIterator Buffer::par_iterator_begin() const
2334 ParagraphList const & pars = paragraphs();
2335 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2339 ParConstIterator Buffer::par_iterator_end() const
2341 ParagraphList const & pars = paragraphs();
2342 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2346 Language const * Buffer::getLanguage() const
2348 return params().language;
2352 string const Buffer::B_(string const & l10n) const
2354 if (pimpl_->messages.get()) {
2355 return pimpl_->messages->get(l10n);
2362 bool Buffer::isClean() const
2364 return pimpl_->lyx_clean;
2368 bool Buffer::isBakClean() const
2370 return pimpl_->bak_clean;
2374 void Buffer::markClean() const
2376 if (!pimpl_->lyx_clean) {
2377 pimpl_->lyx_clean = true;
2380 // if the .lyx file has been saved, we don't need an
2382 pimpl_->bak_clean = true;
2386 void Buffer::markBakClean()
2388 pimpl_->bak_clean = true;
2392 void Buffer::setUnnamed(bool flag)
2394 pimpl_->unnamed = flag;
2398 bool Buffer::isUnnamed()
2400 return pimpl_->unnamed;
2404 void Buffer::markDirty()
2406 if (pimpl_->lyx_clean) {
2407 pimpl_->lyx_clean = false;
2410 pimpl_->bak_clean = false;
2412 DepClean::iterator it = pimpl_->dep_clean.begin();
2413 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2415 for (; it != end; ++it) {
2421 string const & Buffer::fileName() const
2423 return pimpl_->filename;
2427 string const & Buffer::filePath() const
2429 return pimpl_->filepath;
2433 bool Buffer::isReadonly() const
2435 return pimpl_->read_only;
2439 void Buffer::setParentName(string const & name)
2441 params().parentname = name;
2445 Buffer::inset_iterator::inset_iterator()
2450 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2457 Buffer::inset_iterator Buffer::inset_iterator_begin()
2459 return inset_iterator(paragraphs().begin(), paragraphs().end());
2463 Buffer::inset_iterator Buffer::inset_iterator_end()
2465 return inset_iterator(paragraphs().end(), paragraphs().end());
2469 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2471 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2472 return inset_iterator(pars.begin(), pars.end());
2476 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2478 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2479 return inset_iterator(pars.end(), pars.end());
2483 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2487 if (it == pit->insetlist.end()) {
2496 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2498 inset_iterator tmp = *this;
2504 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2510 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2516 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2522 lyx::pos_type Buffer::inset_iterator::getPos() const
2528 bool operator==(Buffer::inset_iterator const & iter1,
2529 Buffer::inset_iterator const & iter2)
2531 return iter1.pit == iter2.pit
2532 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2536 bool operator!=(Buffer::inset_iterator const & iter1,
2537 Buffer::inset_iterator const & iter2)
2539 return !(iter1 == iter2);