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::os;
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;
112 using std::make_pair;
118 using std::ostringstream;
125 // all these externs should eventually be removed.
126 extern BufferList bufferlist;
130 const int LYX_FORMAT = 225;
132 bool openFileWrite(ofstream & ofs, string const & fname)
134 ofs.open(fname.c_str());
136 string const file = MakeDisplayPath(fname, 50);
137 string text = bformat(_("Could not open the specified "
138 "document\n%1$s."), file);
139 Alert::error(_("Could not open file"), text);
148 typedef std::map<string, bool> DepClean;
152 Impl(Buffer & parent, string const & file, bool readonly);
154 limited_stack<Undo> undostack;
155 limited_stack<Undo> redostack;
157 ParagraphList paragraphs;
163 /// need to regenerate .tex ?
167 mutable bool lyx_clean;
169 /// is autosave needed
170 mutable bool bak_clean;
172 /// is this a unnamed file (New...)
178 /// name of the file the buffer is associated with.
181 /// The path to the document file.
184 boost::scoped_ptr<Messages> messages;
188 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
190 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
191 filename(file), filepath(OnlyPath(file))
193 lyxvc.buffer(&parent);
194 if (readonly_ || lyxrc.use_tempdir)
195 temppath = CreateBufferTmpDir();
199 Buffer::Buffer(string const & file, bool ronly)
200 : pimpl_(new Impl(*this, file, ronly))
202 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
208 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
209 // here the buffer should take care that it is
210 // saved properly, before it goes into the void.
214 if (!temppath().empty() && destroyDir(temppath()) != 0) {
215 Alert::warning(_("Could not remove temporary directory"),
216 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
219 paragraphs().clear();
221 // Remove any previewed LaTeX snippets associated with this buffer.
222 lyx::graphics::Previews::get().removeLoader(*this);
226 limited_stack<Undo> & Buffer::undostack()
228 return pimpl_->undostack;
232 limited_stack<Undo> const & Buffer::undostack() const
234 return pimpl_->undostack;
238 limited_stack<Undo> & Buffer::redostack()
240 return pimpl_->redostack;
244 limited_stack<Undo> const & Buffer::redostack() const
246 return pimpl_->redostack;
250 BufferParams & Buffer::params()
252 return pimpl_->params;
256 BufferParams const & Buffer::params() const
258 return pimpl_->params;
262 ParagraphList & Buffer::paragraphs()
264 return pimpl_->paragraphs;
268 ParagraphList const & Buffer::paragraphs() const
270 return pimpl_->paragraphs;
274 LyXVC & Buffer::lyxvc()
276 return pimpl_->lyxvc;
280 LyXVC const & Buffer::lyxvc() const
282 return pimpl_->lyxvc;
286 string const & Buffer::temppath() const
288 return pimpl_->temppath;
292 bool & Buffer::niceFile()
294 return pimpl_->nicefile;
298 bool Buffer::niceFile() const
300 return pimpl_->nicefile;
304 TexRow & Buffer::texrow()
306 return pimpl_->texrow;
310 TexRow const & Buffer::texrow() const
312 return pimpl_->texrow;
316 string const Buffer::getLatexName(bool no_path) const
318 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
319 return no_path ? OnlyFilename(name) : name;
323 pair<Buffer::LogType, string> const Buffer::getLogName() const
325 string const filename = getLatexName(false);
327 if (filename.empty())
328 return make_pair(Buffer::latexlog, string());
330 string path = OnlyPath(filename);
332 if (lyxrc.use_tempdir || !IsDirWriteable(path))
335 string const fname = AddName(path,
336 OnlyFilename(ChangeExtension(filename,
339 AddName(path, OnlyFilename(
340 ChangeExtension(filename,
341 formats.extension("literate") + ".out")));
343 // If no Latex log or Build log is newer, show Build log
345 FileInfo const f_fi(fname);
346 FileInfo const b_fi(bname);
349 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
350 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
351 return make_pair(Buffer::buildlog, bname);
353 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
354 return make_pair(Buffer::latexlog, fname);
358 void Buffer::setReadonly(bool flag)
360 if (pimpl_->read_only != flag) {
361 pimpl_->read_only = flag;
367 void Buffer::setFileName(string const & newfile)
369 pimpl_->filename = MakeAbsPath(newfile);
370 pimpl_->filepath = OnlyPath(pimpl_->filename);
371 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
376 // We'll remove this later. (Lgb)
379 void unknownClass(string const & unknown)
381 Alert::warning(_("Unknown document class"),
382 bformat(_("Using the default document class, because the "
383 "class %1$s is unknown."), unknown));
388 int Buffer::readHeader(LyXLex & lex)
390 int unknown_tokens = 0;
394 string const token = lex.getString();
399 if (token == "\\end_header")
402 lyxerr[Debug::PARSER] << "Handling header token: `"
403 << token << '\'' << endl;
406 string unknown = params().readToken(lex, token);
407 if (!unknown.empty()) {
408 if (unknown[0] != '\\') {
409 unknownClass(unknown);
412 string const s = bformat(_("Unknown token: "
416 error(ErrorItem(_("Header error"), s,
421 return unknown_tokens;
425 // candidate for move to BufferView
426 // (at least some parts in the beginning of the func)
429 // changed to be public and have one parameter
430 // if par = 0 normal behavior
431 // else insert behavior
432 // Returns false if "\end_document" is not read (Asger)
433 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
435 Paragraph::depth_type depth = 0;
436 bool the_end_read = false;
438 if (paragraphs().empty()) {
440 if (!params().getLyXTextClass().load()) {
441 string theclass = params().getLyXTextClass().name();
442 Alert::error(_("Can't load document class"), bformat(
443 "Using the default document class, because the "
444 " class %1$s could not be loaded.", theclass));
445 params().textclass = 0;
448 // We don't want to adopt the parameters from the
449 // document we insert, so read them into a temporary buffer
450 // and then discard it
452 Buffer tmpbuf("", false);
453 tmpbuf.readHeader(lex);
458 string const token = lex.getString();
463 lyxerr[Debug::PARSER] << "Handling token: `"
464 << token << '\'' << endl;
466 if (token == "\\end_document") {
471 readParagraph(lex, token, paragraphs(), pit, depth);
478 int Buffer::readParagraph(LyXLex & lex, string const & token,
479 ParagraphList & pars, ParagraphList::iterator & pit,
480 Paragraph::depth_type & depth)
482 static Change current_change;
485 if (token == "\\begin_layout") {
486 lex.pushToken(token);
489 par.params().depth(depth);
490 if (params().tracking_changes)
492 LyXFont f(LyXFont::ALL_INHERIT, params().language);
496 if (pit != pars.end())
499 pit = pars.insert(pit, par);
501 // FIXME: goddamn InsetTabular makes us pass a Buffer
503 ::readParagraph(*this, *pit, lex);
505 } else if (token == "\\begin_deeper") {
507 } else if (token == "\\end_deeper") {
509 lex.printError("\\end_deeper: " "depth is already null");
520 // needed to insert the selection
521 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
522 LyXFont const & fn,string const & str)
524 LyXLayout_ptr const & layout = par->layout();
528 par->checkInsertChar(font);
529 // insert the string, don't insert doublespace
530 bool space_inserted = true;
531 bool autobreakrows = !par->inInset() ||
532 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
533 for(string::const_iterator cit = str.begin();
534 cit != str.end(); ++cit) {
536 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
537 breakParagraph(params(), paragraphs(), par, pos,
538 layout->isEnvironment());
541 space_inserted = true;
545 // do not insert consecutive spaces if !free_spacing
546 } else if ((*cit == ' ' || *cit == '\t') &&
547 space_inserted && !par->isFreeSpacing()) {
549 } else if (*cit == '\t') {
550 if (!par->isFreeSpacing()) {
551 // tabs are like spaces here
552 par->insertChar(pos, ' ', font);
554 space_inserted = true;
556 const pos_type nb = 8 - pos % 8;
557 for (pos_type a = 0; a < nb ; ++a) {
558 par->insertChar(pos, ' ', font);
561 space_inserted = true;
563 } else if (!IsPrintable(*cit)) {
564 // Ignore unprintables
567 // just insert the character
568 par->insertChar(pos, *cit, font);
570 space_inserted = (*cit == ' ');
577 bool Buffer::readFile(string const & filename)
579 // Check if the file is compressed.
580 string const format = getExtFromContents(filename);
581 if (format == "gzip" || format == "zip" || format == "compress") {
582 params().compressed = true;
585 bool ret = readFile(filename, paragraphs().begin());
587 // After we have read a file, we must ensure that the buffer
588 // language is set and used in the gui.
589 // If you know of a better place to put this, please tell me. (Lgb)
590 updateDocLang(params().language);
596 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
599 lex.setFile(filename);
601 return readFile(lex, filename, pit);
605 bool Buffer::readFile(LyXLex & lex, string const & filename,
606 ParagraphList::iterator pit)
608 BOOST_ASSERT(!filename.empty());
611 Alert::error(_("Document could not be read"),
612 bformat(_("%1$s could not be read."), filename));
617 string const token(lex.getString());
620 Alert::error(_("Document could not be read"),
621 bformat(_("%1$s could not be read."), filename));
625 // the first token _must_ be...
626 if (token != "\\lyxformat") {
627 lyxerr << "Token: " << token << endl;
629 Alert::error(_("Document format failure"),
630 bformat(_("%1$s is not a LyX document."),
636 string tmp_format = lex.getString();
637 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
638 // if present remove ".," from string.
639 string::size_type dot = tmp_format.find_first_of(".,");
640 //lyxerr << " dot found at " << dot << endl;
641 if (dot != string::npos)
642 tmp_format.erase(dot, 1);
643 int file_format = strToInt(tmp_format);
644 //lyxerr << "format: " << file_format << endl;
646 if (file_format > LYX_FORMAT) {
647 Alert::warning(_("Document format failure"),
648 bformat(_("%1$s was created with a newer"
649 " version of LyX. This is likely to"
652 } else if (file_format < LYX_FORMAT) {
653 string const tmpfile = tempName();
654 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
655 if (command.empty()) {
656 Alert::error(_("Conversion script not found"),
657 bformat(_("%1$s is from an earlier"
658 " version of LyX, but the"
659 " conversion script lyx2lyx"
660 " could not be found."),
666 + " -o " + tmpfile + ' '
667 + QuoteName(filename);
668 lyxerr[Debug::INFO] << "Running '"
671 cmd_ret const ret = RunCommand(command);
672 if (ret.first != 0) {
673 Alert::error(_("Conversion script failed"),
674 bformat(_("%1$s is from an earlier version"
675 " of LyX, but the lyx2lyx script"
676 " failed to convert it."),
680 bool ret = readFile(tmpfile, pit);
681 // Do stuff with tmpfile name and buffer name here.
687 bool the_end = readBody(lex, pit);
688 params().setPaperStuff();
691 Alert::error(_("Document format failure"),
692 bformat(_("%1$s ended unexpectedly, which means"
693 " that it is probably corrupted."),
700 // Should probably be moved to somewhere else: BufferView? LyXView?
701 bool Buffer::save() const
703 // We don't need autosaves in the immediate future. (Asger)
704 resetAutosaveTimers();
708 if (lyxrc.make_backup) {
709 s = fileName() + '~';
710 if (!lyxrc.backupdir_path.empty())
711 s = AddName(lyxrc.backupdir_path,
712 subst(os::slashify_path(s),'/','!'));
714 // Rename is the wrong way of making a backup,
715 // this is the correct way.
716 /* truss cp fil fil2:
717 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
718 stat("LyXVC.lyx", 0xEFFFF688) = 0
719 open("LyXVC.lyx", O_RDONLY) = 3
720 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
721 fstat(4, 0xEFFFF508) = 0
722 fstat(3, 0xEFFFF508) = 0
723 read(3, " # T h i s f i l e w".., 8192) = 5579
724 write(4, " # T h i s f i l e w".., 5579) = 5579
725 read(3, 0xEFFFD4A0, 8192) = 0
728 chmod("LyXVC3.lyx", 0100644) = 0
729 lseek(0, 0, SEEK_CUR) = 46440
733 // Should probably have some more error checking here.
734 // Doing it this way, also makes the inodes stay the same.
735 // This is still not a very good solution, in particular we
736 // might loose the owner of the backup.
737 FileInfo finfo(fileName());
739 mode_t fmode = finfo.getMode();
740 struct utimbuf times = {
741 finfo.getAccessTime(),
742 finfo.getModificationTime() };
744 ifstream ifs(fileName().c_str());
745 ofstream ofs(s.c_str(), ios::out|ios::trunc);
750 ::chmod(s.c_str(), fmode);
752 if (::utime(s.c_str(), ×)) {
753 lyxerr << "utime error." << endl;
756 lyxerr << "LyX was not able to make "
757 "backup copy. Beware." << endl;
762 if (writeFile(fileName())) {
764 removeAutosaveFile(fileName());
766 // Saving failed, so backup is not backup
767 if (lyxrc.make_backup) {
768 rename(s, fileName());
776 bool Buffer::writeFile(string const & fname) const
778 if (pimpl_->read_only && (fname == fileName())) {
782 FileInfo finfo(fname);
783 if (finfo.exist() && !finfo.writable()) {
789 if (params().compressed) {
790 gz::ogzstream ofs(fname.c_str());
795 retval = do_writeFile(ofs);
798 ofstream ofs(fname.c_str());
802 retval = do_writeFile(ofs);
809 bool Buffer::do_writeFile(ostream & ofs) const
813 // Use the standard "C" locale for file output.
814 ofs.imbue(std::locale::classic());
817 // The top of the file should not be written by params().
819 // write out a comment in the top of the file
820 ofs << "#LyX " << lyx_version
821 << " created this file. For more info see http://www.lyx.org/\n"
822 << "\\lyxformat " << LYX_FORMAT << "\n";
824 // now write out the buffer paramters.
825 params().writeFile(ofs);
827 ofs << "\\end_header\n";
829 Paragraph::depth_type depth = 0;
831 // this will write out all the paragraphs
832 // using recursive descent.
833 ParagraphList::const_iterator pit = paragraphs().begin();
834 ParagraphList::const_iterator pend = paragraphs().end();
835 for (; pit != pend; ++pit)
836 pit->write(*this, ofs, params(), depth);
838 // Write marker that shows file is complete
839 ofs << "\n\\end_document" << endl;
841 // Shouldn't really be needed....
844 // how to check if close went ok?
845 // Following is an attempt... (BE 20001011)
847 // good() returns false if any error occured, including some
849 // bad() returns true if something bad happened in the buffer,
850 // which should include file system full errors.
857 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
859 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
871 pair<int, string> const addDepth(int depth, int ldepth)
875 d += (ldepth - depth) * 2;
876 return make_pair(d, string(d, ' '));
882 string const Buffer::asciiParagraph(Paragraph const & par,
883 unsigned int linelen,
884 bool noparbreak) const
886 ostringstream buffer;
888 Paragraph::depth_type ltype_depth = 0;
889 bool ref_printed = false;
890 Paragraph::depth_type depth = par.params().depth();
892 // First write the layout
893 string const & tmp = par.layout()->name();
894 if (compare_no_case(tmp, "itemize") == 0) {
896 ltype_depth = depth + 1;
897 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
899 ltype_depth = depth + 1;
900 } else if (contains(ascii_lowercase(tmp), "ection")) {
902 ltype_depth = depth + 1;
903 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
905 ltype_depth = depth + 1;
906 } else if (compare_ascii_no_case(tmp, "description") == 0) {
908 ltype_depth = depth + 1;
909 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
912 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
920 /* maybe some vertical spaces */
922 /* the labelwidthstring used in lists */
926 /* some pagebreaks? */
930 /* what about the alignment */
932 // linelen <= 0 is special and means we don't have paragraph breaks
934 string::size_type currlinelen = 0;
940 buffer << string(depth * 2, ' ');
941 currlinelen += depth * 2;
944 // we should probably change to the paragraph language in the
945 // gettext here (if possible) so that strings are outputted in
946 // the correct language! (20012712 Jug)
950 case 4: // (Sub)Paragraph
951 case 5: // Description
955 buffer << _("Abstract") << "\n\n";
958 string const abst = _("Abstract: ");
960 currlinelen += abst.length();
963 case 7: // Bibliography
966 buffer << _("References") << "\n\n";
969 string const refs = _("References: ");
971 currlinelen += refs.length();
979 string const parlab = par.params().labelString();
980 buffer << parlab << ' ';
981 currlinelen += parlab.length() + 1;
989 pair<int, string> p = addDepth(depth, ltype_depth);
991 currlinelen += p.first;
994 // this is to change the linebreak to do it by word a bit more
995 // intelligent hopefully! (only in the case where we have a
996 // max linelength!) (Jug)
1000 for (pos_type i = 0; i < par.size(); ++i) {
1001 char c = par.getUChar(params(), i);
1003 case Paragraph::META_INSET:
1005 InsetOld const * inset = par.getInset(i);
1009 currlinelen += word.length();
1012 if (inset->ascii(*this, buffer, linelen)) {
1013 // to be sure it breaks paragraph
1014 currlinelen += linelen;
1023 currlinelen + word.length() > linelen - 10) {
1025 pair<int, string> p = addDepth(depth, ltype_depth);
1027 currlinelen = p.first;
1030 buffer << word << ' ';
1031 currlinelen += word.length() + 1;
1038 lyxerr[Debug::INFO] <<
1039 "writeAsciiFile: NULL char in structure." << endl;
1041 if ((linelen > 0) &&
1042 (currlinelen + word.length()) > linelen)
1046 pair<int, string> p =
1047 addDepth(depth, ltype_depth);
1049 currlinelen = p.first;
1056 return buffer.str();
1060 void Buffer::writeFileAscii(string const & fname, int linelen)
1063 if (!::openFileWrite(ofs, fname))
1065 writeFileAscii(ofs, linelen);
1069 void Buffer::writeFileAscii(ostream & os, int linelen)
1071 ParagraphList::iterator beg = paragraphs().begin();
1072 ParagraphList::iterator end = paragraphs().end();
1073 ParagraphList::iterator it = beg;
1074 for (; it != end; ++it) {
1075 os << asciiParagraph(*it, linelen, it == beg);
1081 void Buffer::makeLaTeXFile(string const & fname,
1082 string const & original_path,
1083 LatexRunParams const & runparams,
1084 bool output_preamble, bool output_body)
1086 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1089 if (!::openFileWrite(ofs, fname))
1092 makeLaTeXFile(ofs, original_path,
1093 runparams, output_preamble, output_body);
1097 lyxerr << "File was not closed properly." << endl;
1102 void Buffer::makeLaTeXFile(ostream & os,
1103 string const & original_path,
1104 LatexRunParams const & runparams_in,
1105 bool output_preamble, bool output_body)
1107 LatexRunParams runparams = runparams_in;
1108 niceFile() = runparams.nice; // this will be used by Insetincludes.
1110 // validate the buffer.
1111 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1112 LaTeXFeatures features(params());
1114 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1117 // The starting paragraph of the coming rows is the
1118 // first paragraph of the document. (Asger)
1119 texrow().start(paragraphs().begin()->id(), 0);
1121 if (output_preamble && runparams.nice) {
1122 os << "%% LyX " << lyx_version << " created this file. "
1123 "For more info, see http://www.lyx.org/.\n"
1124 "%% Do not edit unless you really know what "
1129 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1130 // There are a few differences between nice LaTeX and usual files:
1131 // usual is \batchmode and has a
1132 // special input@path to allow the including of figures
1133 // with either \input or \includegraphics (what figinsets do).
1134 // input@path is set when the actual parameter
1135 // original_path is set. This is done for usual tex-file, but not
1136 // for nice-latex-file. (Matthias 250696)
1137 if (output_preamble) {
1138 if (!runparams.nice) {
1139 // code for usual, NOT nice-latex-file
1140 os << "\\batchmode\n"; // changed
1141 // from \nonstopmode
1144 if (!original_path.empty()) {
1145 string inputpath = os::external_path(original_path);
1146 subst(inputpath, "~", "\\string~");
1147 os << "\\makeatletter\n"
1148 << "\\def\\input@path{{"
1149 << inputpath << "/}}\n"
1150 << "\\makeatother\n";
1156 // Write the preamble
1157 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1163 os << "\\begin{document}\n";
1165 } // output_preamble
1166 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1168 if (!lyxrc.language_auto_begin) {
1169 os << subst(lyxrc.language_command_begin, "$$lang",
1170 params().language->babel())
1175 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1177 // add this just in case after all the paragraphs
1181 if (!lyxrc.language_auto_end) {
1182 os << subst(lyxrc.language_command_end, "$$lang",
1183 params().language->babel())
1188 if (output_preamble) {
1189 os << "\\end{document}\n";
1192 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1194 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1198 // Just to be sure. (Asger)
1201 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1202 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1205 // we want this to be true outside previews (for insetexternal)
1210 bool Buffer::isLatex() const
1212 return params().getLyXTextClass().outputType() == LATEX;
1216 bool Buffer::isLinuxDoc() const
1218 return params().getLyXTextClass().outputType() == LINUXDOC;
1222 bool Buffer::isLiterate() const
1224 return params().getLyXTextClass().outputType() == LITERATE;
1228 bool Buffer::isDocBook() const
1230 return params().getLyXTextClass().outputType() == DOCBOOK;
1234 bool Buffer::isSGML() const
1236 LyXTextClass const & tclass = params().getLyXTextClass();
1238 return tclass.outputType() == LINUXDOC ||
1239 tclass.outputType() == DOCBOOK;
1243 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1246 if (!::openFileWrite(ofs, fname))
1249 niceFile() = nice; // this will be used by included files.
1251 LaTeXFeatures features(params());
1257 LyXTextClass const & tclass = params().getLyXTextClass();
1259 string top_element = tclass.latexname();
1262 ofs << "<!doctype linuxdoc system";
1264 string preamble = params().preamble;
1265 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1267 preamble += features.getIncludedFiles(name);
1268 preamble += features.getLyXSGMLEntities();
1270 if (!preamble.empty()) {
1271 ofs << " [ " << preamble << " ]";
1275 if (params().options.empty())
1276 sgml::openTag(ofs, 0, false, top_element);
1278 string top = top_element;
1280 top += params().options;
1281 sgml::openTag(ofs, 0, false, top);
1285 ofs << "<!-- LyX " << lyx_version
1286 << " created this file. For more info see http://www.lyx.org/"
1289 Paragraph::depth_type depth = 0; // paragraph depth
1291 vector<string> environment_stack(5);
1293 ParagraphList::iterator pit = paragraphs().begin();
1294 ParagraphList::iterator pend = paragraphs().end();
1295 for (; pit != pend; ++pit) {
1296 LyXLayout_ptr const & style = pit->layout();
1297 // treat <toc> as a special case for compatibility with old code
1298 if (pit->isInset(0)) {
1299 InsetOld * inset = pit->getInset(0);
1300 InsetOld::Code lyx_code = inset->lyxCode();
1301 if (lyx_code == InsetOld::TOC_CODE) {
1302 string const temp = "toc";
1303 sgml::openTag(ofs, depth, false, temp);
1308 // environment tag closing
1309 for (; depth > pit->params().depth(); --depth) {
1310 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1311 environment_stack[depth].erase();
1314 // write opening SGML tags
1315 switch (style->latextype) {
1316 case LATEX_PARAGRAPH:
1317 if (depth == pit->params().depth()
1318 && !environment_stack[depth].empty()) {
1319 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1320 environment_stack[depth].erase();
1326 sgml::openTag(ofs, depth, false, style->latexname());
1331 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1333 if (!environment_stack[depth].empty()) {
1334 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1338 environment_stack[depth].erase();
1339 sgml::openTag(ofs, depth, false, style->latexname());
1342 case LATEX_ENVIRONMENT:
1343 case LATEX_ITEM_ENVIRONMENT:
1344 case LATEX_BIB_ENVIRONMENT:
1346 string const & latexname = style->latexname();
1348 if (depth == pit->params().depth()
1349 && environment_stack[depth] != latexname) {
1350 sgml::closeTag(ofs, depth, false,
1351 environment_stack[depth]);
1352 environment_stack[depth].erase();
1354 if (depth < pit->params().depth()) {
1355 depth = pit->params().depth();
1356 environment_stack[depth].erase();
1358 if (environment_stack[depth] != latexname) {
1360 sgml::openTag(ofs, depth, false, "p");
1362 sgml::openTag(ofs, depth, false, latexname);
1364 if (environment_stack.size() == depth + 1)
1365 environment_stack.push_back("!-- --");
1366 environment_stack[depth] = latexname;
1369 if (style->latexparam() == "CDATA")
1372 if (style->latextype == LATEX_ENVIRONMENT) break;
1374 if (style->labeltype == LABEL_MANUAL)
1379 sgml::openTag(ofs, depth + 1, false, item_name);
1384 sgml::openTag(ofs, depth, false, style->latexname());
1388 simpleLinuxDocOnePar(ofs, pit, depth);
1391 // write closing SGML tags
1392 switch (style->latextype) {
1395 case LATEX_ENVIRONMENT:
1396 case LATEX_ITEM_ENVIRONMENT:
1397 case LATEX_BIB_ENVIRONMENT:
1398 if (style->latexparam() == "CDATA")
1402 sgml::closeTag(ofs, depth, false, style->latexname());
1408 for (int i = depth; i >= 0; --i)
1409 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1413 sgml::closeTag(ofs, 0, false, top_element);
1417 // How to check for successful close
1419 // we want this to be true outside previews (for insetexternal)
1424 // checks, if newcol chars should be put into this line
1425 // writes newline, if necessary.
1428 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1429 string::size_type newcol)
1432 if (colcount > lyxrc.ascii_linelen) {
1434 colcount = newcol; // assume write after this call
1449 string tag_name(PAR_TAG const & pt) {
1451 case NONE: return "!-- --";
1452 case TT: return "tt";
1453 case SF: return "sf";
1454 case BF: return "bf";
1455 case IT: return "it";
1456 case SL: return "sl";
1457 case EM: return "em";
1464 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1466 p1 = static_cast<PAR_TAG>(p1 | p2);
1471 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1473 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1479 // Handle internal paragraph parsing -- layout already processed.
1480 void Buffer::simpleLinuxDocOnePar(ostream & os,
1481 ParagraphList::iterator par,
1482 Paragraph::depth_type /*depth*/) const
1484 LyXLayout_ptr const & style = par->layout();
1486 string::size_type char_line_count = 5; // Heuristic choice ;-)
1488 // gets paragraph main font
1491 if (style->labeltype == LABEL_MANUAL) {
1492 font_old = style->labelfont;
1495 font_old = style->font;
1499 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1500 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1501 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1504 stack<PAR_TAG> tag_state;
1505 // parsing main loop
1506 for (pos_type i = 0; i < par->size(); ++i) {
1508 PAR_TAG tag_close = NONE;
1509 list < PAR_TAG > tag_open;
1511 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1513 if (font_old.family() != font.family()) {
1514 switch (family_type) {
1515 case LyXFont::SANS_FAMILY:
1518 case LyXFont::TYPEWRITER_FAMILY:
1525 family_type = font.family();
1527 switch (family_type) {
1528 case LyXFont::SANS_FAMILY:
1529 tag_open.push_back(SF);
1531 case LyXFont::TYPEWRITER_FAMILY:
1532 tag_open.push_back(TT);
1539 if (font_old.series() != font.series()) {
1540 switch (series_type) {
1541 case LyXFont::BOLD_SERIES:
1548 series_type = font.series();
1550 switch (series_type) {
1551 case LyXFont::BOLD_SERIES:
1552 tag_open.push_back(BF);
1560 if (font_old.shape() != font.shape()) {
1561 switch (shape_type) {
1562 case LyXFont::ITALIC_SHAPE:
1565 case LyXFont::SLANTED_SHAPE:
1572 shape_type = font.shape();
1574 switch (shape_type) {
1575 case LyXFont::ITALIC_SHAPE:
1576 tag_open.push_back(IT);
1578 case LyXFont::SLANTED_SHAPE:
1579 tag_open.push_back(SL);
1586 if (font_old.emph() != font.emph()) {
1587 if (font.emph() == LyXFont::ON) {
1588 tag_open.push_back(EM);
1597 list < PAR_TAG > temp;
1598 while (!tag_state.empty() && tag_close) {
1599 PAR_TAG k = tag_state.top();
1601 os << "</" << tag_name(k) << '>';
1608 for(list< PAR_TAG >::const_iterator j = temp.begin();
1609 j != temp.end(); ++j) {
1611 os << '<' << tag_name(*j) << '>';
1614 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1615 j != tag_open.end(); ++j) {
1617 os << '<' << tag_name(*j) << '>';
1620 char c = par->getChar(i);
1622 if (c == Paragraph::META_INSET) {
1623 InsetOld * inset = par->getInset(i);
1624 inset->linuxdoc(*this, os);
1629 if (style->latexparam() == "CDATA") {
1630 // "TeX"-Mode on == > SGML-Mode on.
1637 boost::tie(ws, str) = sgml::escapeChar(c);
1638 if (ws && !par->isFreeSpacing()) {
1639 // in freespacing mode, spaces are
1640 // non-breaking characters
1641 if (desc_on) {// if char is ' ' then...
1644 sgmlLineBreak(os, char_line_count, 6);
1648 sgmlLineBreak(os, char_line_count, 1);
1653 char_line_count += str.length();
1659 while (!tag_state.empty()) {
1660 os << "</" << tag_name(tag_state.top()) << '>';
1664 // resets description flag correctly
1666 // <tag> not closed...
1667 sgmlLineBreak(os, char_line_count, 6);
1673 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1676 if (!::openFileWrite(ofs, fname))
1679 niceFile() = nice; // this will be used by Insetincludes.
1681 LaTeXFeatures features(params());
1686 LyXTextClass const & tclass = params().getLyXTextClass();
1687 string top_element = tclass.latexname();
1690 ofs << "<!DOCTYPE " << top_element
1691 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1693 string preamble = params().preamble;
1694 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1696 preamble += features.getIncludedFiles(name);
1697 preamble += features.getLyXSGMLEntities();
1699 if (!preamble.empty()) {
1700 ofs << "\n [ " << preamble << " ]";
1705 string top = top_element;
1707 top += params().language->code();
1710 if (!params().options.empty()) {
1712 top += params().options;
1714 sgml::openTag(ofs, 0, false, top);
1716 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1717 << "\n See http://www.lyx.org/ for more information -->\n";
1719 vector<string> environment_stack(10);
1720 vector<string> environment_inner(10);
1721 vector<string> command_stack(10);
1723 bool command_flag = false;
1724 Paragraph::depth_type command_depth = 0;
1725 Paragraph::depth_type command_base = 0;
1726 Paragraph::depth_type cmd_depth = 0;
1727 Paragraph::depth_type depth = 0; // paragraph depth
1730 string command_name;
1732 ParagraphList::iterator par = paragraphs().begin();
1733 ParagraphList::iterator pend = paragraphs().end();
1735 for (; par != pend; ++par) {
1739 int desc_on = 0; // description mode
1741 LyXLayout_ptr const & style = par->layout();
1743 // environment tag closing
1744 for (; depth > par->params().depth(); --depth) {
1745 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1746 item_name = "listitem";
1747 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1748 if (environment_inner[depth] == "varlistentry")
1749 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1751 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1752 environment_stack[depth].erase();
1753 environment_inner[depth].erase();
1756 if (depth == par->params().depth()
1757 && environment_stack[depth] != style->latexname()
1758 && !environment_stack[depth].empty()) {
1759 if (environment_inner[depth] != "!-- --") {
1760 item_name= "listitem";
1761 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1762 if (environment_inner[depth] == "varlistentry")
1763 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1766 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1768 environment_stack[depth].erase();
1769 environment_inner[depth].erase();
1772 // Write opening SGML tags.
1773 switch (style->latextype) {
1774 case LATEX_PARAGRAPH:
1775 sgml::openTag(ofs, depth + command_depth,
1776 false, style->latexname());
1781 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1783 command_name = style->latexname();
1785 sgmlparam = style->latexparam();
1786 c_params = split(sgmlparam, c_depth,'|');
1788 cmd_depth = atoi(c_depth);
1791 if (cmd_depth < command_base) {
1792 for (Paragraph::depth_type j = command_depth;
1793 j >= command_base; --j) {
1794 sgml::closeTag(ofs, j, false, command_stack[j]);
1797 command_depth = command_base = cmd_depth;
1798 } else if (cmd_depth <= command_depth) {
1799 for (int j = command_depth;
1800 j >= int(cmd_depth); --j) {
1801 sgml::closeTag(ofs, j, false, command_stack[j]);
1804 command_depth = cmd_depth;
1806 command_depth = cmd_depth;
1808 command_depth = command_base = cmd_depth;
1809 command_flag = true;
1811 if (command_stack.size() == command_depth + 1)
1812 command_stack.push_back(string());
1813 command_stack[command_depth] = command_name;
1815 // treat label as a special case for
1816 // more WYSIWYM handling.
1817 // This is a hack while paragraphs can't have
1818 // attributes, like id in this case.
1819 if (par->isInset(0)) {
1820 InsetOld * inset = par->getInset(0);
1821 InsetOld::Code lyx_code = inset->lyxCode();
1822 if (lyx_code == InsetOld::LABEL_CODE) {
1823 command_name += " id=\"";
1824 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1825 command_name += '"';
1830 sgml::openTag(ofs, depth + command_depth, false, command_name);
1832 item_name = c_params.empty() ? "title" : c_params;
1833 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1836 case LATEX_ENVIRONMENT:
1837 case LATEX_ITEM_ENVIRONMENT:
1838 if (depth < par->params().depth()) {
1839 depth = par->params().depth();
1840 environment_stack[depth].erase();
1843 if (environment_stack[depth] != style->latexname()) {
1844 if (environment_stack.size() == depth + 1) {
1845 environment_stack.push_back("!-- --");
1846 environment_inner.push_back("!-- --");
1848 environment_stack[depth] = style->latexname();
1849 environment_inner[depth] = "!-- --";
1850 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1852 if (environment_inner[depth] != "!-- --") {
1853 item_name= "listitem";
1854 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1855 if (environment_inner[depth] == "varlistentry")
1856 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1860 if (style->latextype == LATEX_ENVIRONMENT) {
1861 if (!style->latexparam().empty()) {
1862 if (style->latexparam() == "CDATA")
1865 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1870 desc_on = (style->labeltype == LABEL_MANUAL);
1872 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1873 sgml::openTag(ofs, depth + 1 + command_depth,
1874 false, environment_inner[depth]);
1876 item_name = desc_on ? "term" : "para";
1877 sgml::openTag(ofs, depth + 1 + command_depth,
1881 sgml::openTag(ofs, depth + command_depth,
1882 false, style->latexname());
1886 simpleDocBookOnePar(ofs, par, desc_on,
1887 depth + 1 + command_depth);
1890 // write closing SGML tags
1891 switch (style->latextype) {
1893 end_tag = c_params.empty() ? "title" : c_params;
1894 sgml::closeTag(ofs, depth + command_depth,
1897 case LATEX_ENVIRONMENT:
1898 if (!style->latexparam().empty()) {
1899 if (style->latexparam() == "CDATA")
1902 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1905 case LATEX_ITEM_ENVIRONMENT:
1906 if (desc_on == 1) break;
1908 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1910 case LATEX_PARAGRAPH:
1911 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1914 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1920 for (int d = depth; d >= 0; --d) {
1921 if (!environment_stack[depth].empty()) {
1922 if (environment_inner[depth] != "!-- --") {
1923 item_name = "listitem";
1924 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1925 if (environment_inner[depth] == "varlistentry")
1926 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1929 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1933 for (int j = command_depth; j >= 0 ; --j)
1934 if (!command_stack[j].empty()) {
1935 sgml::closeTag(ofs, j, false, command_stack[j]);
1940 sgml::closeTag(ofs, 0, false, top_element);
1943 // How to check for successful close
1945 // we want this to be true outside previews (for insetexternal)
1950 void Buffer::simpleDocBookOnePar(ostream & os,
1951 ParagraphList::iterator par, int & desc_on,
1952 Paragraph::depth_type depth) const
1954 bool emph_flag = false;
1956 LyXLayout_ptr const & style = par->layout();
1958 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1960 int char_line_count = depth;
1961 //if (!style.free_spacing)
1962 // os << string(depth,' ');
1964 // parsing main loop
1965 for (pos_type i = 0; i < par->size(); ++i) {
1966 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1968 // handle <emphasis> tag
1969 if (font_old.emph() != font.emph()) {
1970 if (font.emph() == LyXFont::ON) {
1971 if (style->latexparam() == "CDATA")
1974 if (style->latexparam() == "CDATA")
1978 if (style->latexparam() == "CDATA")
1980 os << "</emphasis>";
1981 if (style->latexparam() == "CDATA")
1988 if (par->isInset(i)) {
1989 InsetOld * inset = par->getInset(i);
1990 // don't print the inset in position 0 if desc_on == 3 (label)
1991 if (i || desc_on != 3) {
1992 if (style->latexparam() == "CDATA")
1994 inset->docbook(*this, os, false);
1995 if (style->latexparam() == "CDATA")
1999 char c = par->getChar(i);
2002 boost::tie(ws, str) = sgml::escapeChar(c);
2004 if (style->pass_thru) {
2006 } else if (par->isFreeSpacing() || c != ' ') {
2008 } else if (desc_on == 1) {
2010 os << "\n</term><listitem><para>";
2020 if (style->latexparam() == "CDATA")
2022 os << "</emphasis>";
2023 if (style->latexparam() == "CDATA")
2027 // resets description flag correctly
2029 // <term> not closed...
2030 os << "</term>\n<listitem><para> </para>";
2032 if (style->free_spacing)
2037 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2038 // Other flags: -wall -v0 -x
2039 int Buffer::runChktex()
2043 // get LaTeX-Filename
2044 string const name = getLatexName();
2045 string path = filePath();
2047 string const org_path = path;
2048 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2052 Path p(path); // path to LaTeX file
2053 message(_("Running chktex..."));
2055 // Generate the LaTeX file if neccessary
2056 LatexRunParams runparams;
2057 runparams.flavor = LatexRunParams::LATEX;
2058 runparams.nice = false;
2059 makeLaTeXFile(name, org_path, runparams);
2062 Chktex chktex(lyxrc.chktex_command, name, filePath());
2063 int res = chktex.run(terr); // run chktex
2066 Alert::error(_("chktex failure"),
2067 _("Could not run chktex successfully."));
2068 } else if (res > 0) {
2069 // Insert all errors as errors boxes
2070 bufferErrors(*this, terr);
2079 void Buffer::validate(LaTeXFeatures & features) const
2081 LyXTextClass const & tclass = params().getLyXTextClass();
2083 if (params().tracking_changes) {
2084 features.require("dvipost");
2085 features.require("color");
2088 // AMS Style is at document level
2089 if (params().use_amsmath == BufferParams::AMS_ON
2090 || tclass.provides(LyXTextClass::amsmath))
2091 features.require("amsmath");
2093 for_each(paragraphs().begin(), paragraphs().end(),
2094 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2096 // the bullet shapes are buffer level not paragraph level
2097 // so they are tested here
2098 for (int i = 0; i < 4; ++i) {
2099 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2100 int const font = params().user_defined_bullet(i).getFont();
2102 int const c = params()
2103 .user_defined_bullet(i)
2110 features.require("latexsym");
2112 } else if (font == 1) {
2113 features.require("amssymb");
2114 } else if ((font >= 2 && font <= 5)) {
2115 features.require("pifont");
2120 if (lyxerr.debugging(Debug::LATEX)) {
2121 features.showStruct();
2126 void Buffer::getLabelList(std::vector<string> & list) const
2128 /// if this is a child document and the parent is already loaded
2129 /// Use the parent's list instead [ale990407]
2130 if (!params().parentname.empty()
2131 && bufferlist.exists(params().parentname)) {
2132 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2134 tmp->getLabelList(list);
2139 for (inset_iterator it = inset_const_iterator_begin();
2140 it != inset_const_iterator_end(); ++it) {
2141 it->getLabelList(list);
2146 // This is also a buffer property (ale)
2147 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2149 /// if this is a child document and the parent is already loaded
2150 /// use the parent's list instead [ale990412]
2151 if (!params().parentname.empty() &&
2152 bufferlist.exists(params().parentname)) {
2153 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2155 tmp->fillWithBibKeys(keys);
2160 for (inset_iterator it = inset_const_iterator_begin();
2161 it != inset_const_iterator_end(); ++it) {
2162 if (it->lyxCode() == InsetOld::BIBTEX_CODE)
2163 static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
2164 else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
2165 static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
2166 else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2167 InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
2168 string const key = bib.getContents();
2169 string const opt = bib.getOptions();
2170 string const ref; // = pit->asString(this, false);
2171 string const info = opt + "TheBibliographyRef" + ref;
2172 keys.push_back(pair<string, string>(key, info));
2178 bool Buffer::isDepClean(string const & name) const
2180 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2181 if (it == pimpl_->dep_clean.end())
2187 void Buffer::markDepClean(string const & name)
2189 pimpl_->dep_clean[name] = true;
2193 bool Buffer::dispatch(string const & command, bool * result)
2195 // Split command string into command and argument
2197 string line = ltrim(command);
2198 string const arg = trim(split(line, cmd, ' '));
2200 return dispatch(lyxaction.LookupFunc(cmd), arg, result);
2204 bool Buffer::dispatch(int action, string const & argument, bool * result)
2206 bool dispatched = true;
2210 bool const tmp = Exporter::Export(this, argument, false);
2223 void Buffer::changeLanguage(Language const * from, Language const * to)
2225 lyxerr << "Changing Language!" << endl;
2227 // Take care of l10n/i18n
2230 ParIterator end = par_iterator_end();
2231 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2232 it->changeLanguage(params(), from, to);
2236 void Buffer::updateDocLang(Language const * nlang)
2238 pimpl_->messages.reset(new Messages(nlang->code()));
2242 bool Buffer::isMultiLingual()
2244 ParIterator end = par_iterator_end();
2245 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2246 if (it->isMultiLingual(params()))
2253 void Buffer::inset_iterator::setParagraph()
2255 while (pit != pend) {
2256 it = pit->insetlist.begin();
2257 if (it != pit->insetlist.end())
2264 InsetOld * Buffer::getInsetFromID(int id_arg) const
2266 for (inset_iterator it = inset_const_iterator_begin();
2267 it != inset_const_iterator_end(); ++it)
2269 if (it->id() == id_arg)
2271 InsetOld * in = it->getInsetFromID(id_arg);
2279 ParIterator Buffer::getParFromID(int id) const
2281 #warning FIXME: const correctness! (Andre)
2282 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2283 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2285 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2287 // John says this is called with id == -1 from undo
2288 lyxerr << "getParFromID(), id: " << id << endl;
2292 for (; it != end; ++it)
2300 bool Buffer::hasParWithID(int id) const
2302 ParConstIterator it = par_iterator_begin();
2303 ParConstIterator end = par_iterator_end();
2306 // John says this is called with id == -1 from undo
2307 lyxerr << "hasParWithID(), id: " << id << endl;
2311 for (; it != end; ++it)
2319 ParIterator Buffer::par_iterator_begin()
2321 return ParIterator(paragraphs().begin(), paragraphs());
2325 ParIterator Buffer::par_iterator_end()
2327 return ParIterator(paragraphs().end(), paragraphs());
2331 ParConstIterator Buffer::par_iterator_begin() const
2333 ParagraphList const & pars = paragraphs();
2334 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2338 ParConstIterator Buffer::par_iterator_end() const
2340 ParagraphList const & pars = paragraphs();
2341 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2345 Language const * Buffer::getLanguage() const
2347 return params().language;
2351 string const Buffer::B_(string const & l10n) const
2353 if (pimpl_->messages.get()) {
2354 return pimpl_->messages->get(l10n);
2361 bool Buffer::isClean() const
2363 return pimpl_->lyx_clean;
2367 bool Buffer::isBakClean() const
2369 return pimpl_->bak_clean;
2373 void Buffer::markClean() const
2375 if (!pimpl_->lyx_clean) {
2376 pimpl_->lyx_clean = true;
2379 // if the .lyx file has been saved, we don't need an
2381 pimpl_->bak_clean = true;
2385 void Buffer::markBakClean()
2387 pimpl_->bak_clean = true;
2391 void Buffer::setUnnamed(bool flag)
2393 pimpl_->unnamed = flag;
2397 bool Buffer::isUnnamed()
2399 return pimpl_->unnamed;
2403 void Buffer::markDirty()
2405 if (pimpl_->lyx_clean) {
2406 pimpl_->lyx_clean = false;
2409 pimpl_->bak_clean = false;
2411 DepClean::iterator it = pimpl_->dep_clean.begin();
2412 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2414 for (; it != end; ++it) {
2420 string const & Buffer::fileName() const
2422 return pimpl_->filename;
2426 string const & Buffer::filePath() const
2428 return pimpl_->filepath;
2432 bool Buffer::isReadonly() const
2434 return pimpl_->read_only;
2438 void Buffer::setParentName(string const & name)
2440 params().parentname = name;
2444 Buffer::inset_iterator::inset_iterator()
2449 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2456 Buffer::inset_iterator Buffer::inset_iterator_begin()
2458 return inset_iterator(paragraphs().begin(), paragraphs().end());
2462 Buffer::inset_iterator Buffer::inset_iterator_end()
2464 return inset_iterator(paragraphs().end(), paragraphs().end());
2468 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2470 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2471 return inset_iterator(pars.begin(), pars.end());
2475 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2477 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2478 return inset_iterator(pars.end(), pars.end());
2482 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2486 if (it == pit->insetlist.end()) {
2495 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2497 inset_iterator tmp = *this;
2503 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2509 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2515 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2521 lyx::pos_type Buffer::inset_iterator::getPos() const
2527 bool operator==(Buffer::inset_iterator const & iter1,
2528 Buffer::inset_iterator const & iter2)
2530 return iter1.pit == iter2.pit
2531 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2535 bool operator!=(Buffer::inset_iterator const & iter1,
2536 Buffer::inset_iterator const & iter2)
2538 return !(iter1 == iter2);