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"
73 using namespace lyx::support;
83 using std::ostringstream;
92 // all these externs should eventually be removed.
93 extern BufferList bufferlist;
97 const int LYX_FORMAT = 225;
99 bool openFileWrite(ofstream & ofs, string const & fname)
101 ofs.open(fname.c_str());
103 string const file = MakeDisplayPath(fname, 50);
104 string text = bformat(_("Could not open the specified "
105 "document\n%1$s."), file);
106 Alert::error(_("Could not open file"), text);
115 typedef std::map<string, bool> DepClean;
119 Impl(Buffer & parent, string const & file, bool readonly);
121 limited_stack<Undo> undostack;
122 limited_stack<Undo> redostack;
124 ParagraphList paragraphs;
130 /// need to regenerate .tex ?
134 mutable bool lyx_clean;
136 /// is autosave needed
137 mutable bool bak_clean;
139 /// is this a unnamed file (New...)
145 /// name of the file the buffer is associated with.
148 /// The path to the document file.
151 boost::scoped_ptr<Messages> messages;
155 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
157 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
158 filename(file), filepath(OnlyPath(file))
160 lyxvc.buffer(&parent);
161 if (readonly_ || lyxrc.use_tempdir)
162 temppath = CreateBufferTmpDir();
166 Buffer::Buffer(string const & file, bool ronly)
167 : pimpl_(new Impl(*this, file, ronly))
169 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
175 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
176 // here the buffer should take care that it is
177 // saved properly, before it goes into the void.
181 if (!temppath().empty() && destroyDir(temppath()) != 0) {
182 Alert::warning(_("Could not remove temporary directory"),
183 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
186 paragraphs().clear();
188 // Remove any previewed LaTeX snippets associated with this buffer.
189 lyx::graphics::Previews::get().removeLoader(*this);
193 limited_stack<Undo> & Buffer::undostack()
195 return pimpl_->undostack;
199 limited_stack<Undo> const & Buffer::undostack() const
201 return pimpl_->undostack;
205 limited_stack<Undo> & Buffer::redostack()
207 return pimpl_->redostack;
211 limited_stack<Undo> const & Buffer::redostack() const
213 return pimpl_->redostack;
217 BufferParams & Buffer::params()
219 return pimpl_->params;
223 BufferParams const & Buffer::params() const
225 return pimpl_->params;
229 ParagraphList & Buffer::paragraphs()
231 return pimpl_->paragraphs;
235 ParagraphList const & Buffer::paragraphs() const
237 return pimpl_->paragraphs;
241 LyXVC & Buffer::lyxvc()
243 return pimpl_->lyxvc;
247 LyXVC const & Buffer::lyxvc() const
249 return pimpl_->lyxvc;
253 string const & Buffer::temppath() const
255 return pimpl_->temppath;
259 bool & Buffer::niceFile()
261 return pimpl_->nicefile;
265 bool Buffer::niceFile() const
267 return pimpl_->nicefile;
271 TexRow & Buffer::texrow()
273 return pimpl_->texrow;
277 TexRow const & Buffer::texrow() const
279 return pimpl_->texrow;
283 string const Buffer::getLatexName(bool no_path) const
285 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
286 return no_path ? OnlyFilename(name) : name;
290 pair<Buffer::LogType, string> const Buffer::getLogName() const
292 string const filename = getLatexName(false);
294 if (filename.empty())
295 return make_pair(Buffer::latexlog, string());
297 string path = OnlyPath(filename);
299 if (lyxrc.use_tempdir || !IsDirWriteable(path))
302 string const fname = AddName(path,
303 OnlyFilename(ChangeExtension(filename,
306 AddName(path, OnlyFilename(
307 ChangeExtension(filename,
308 formats.extension("literate") + ".out")));
310 // If no Latex log or Build log is newer, show Build log
312 FileInfo const f_fi(fname);
313 FileInfo const b_fi(bname);
316 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
317 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
318 return make_pair(Buffer::buildlog, bname);
320 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
321 return make_pair(Buffer::latexlog, fname);
325 void Buffer::setReadonly(bool flag)
327 if (pimpl_->read_only != flag) {
328 pimpl_->read_only = flag;
334 void Buffer::setFileName(string const & newfile)
336 pimpl_->filename = MakeAbsPath(newfile);
337 pimpl_->filepath = OnlyPath(pimpl_->filename);
338 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
343 // We'll remove this later. (Lgb)
346 void unknownClass(string const & unknown)
348 Alert::warning(_("Unknown document class"),
349 bformat(_("Using the default document class, because the "
350 "class %1$s is unknown."), unknown));
355 int Buffer::readHeader(LyXLex & lex)
357 int unknown_tokens = 0;
361 string const token = lex.getString();
366 if (token == "\\end_header")
369 lyxerr[Debug::PARSER] << "Handling header token: `"
370 << token << '\'' << endl;
373 string unknown = params().readToken(lex, token);
374 if (!unknown.empty()) {
375 if (unknown[0] != '\\') {
376 unknownClass(unknown);
379 string const s = bformat(_("Unknown token: "
383 error(ErrorItem(_("Header error"), s,
388 return unknown_tokens;
392 // candidate for move to BufferView
393 // (at least some parts in the beginning of the func)
396 // changed to be public and have one parameter
397 // if par = 0 normal behavior
398 // else insert behavior
399 // Returns false if "\end_document" is not read (Asger)
400 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
402 Paragraph::depth_type depth = 0;
403 bool the_end_read = false;
405 if (paragraphs().empty()) {
407 if (!params().getLyXTextClass().load()) {
408 string theclass = params().getLyXTextClass().name();
409 Alert::error(_("Can't load document class"), bformat(
410 "Using the default document class, because the "
411 " class %1$s could not be loaded.", theclass));
412 params().textclass = 0;
415 // We don't want to adopt the parameters from the
416 // document we insert, so read them into a temporary buffer
417 // and then discard it
419 Buffer tmpbuf("", false);
420 tmpbuf.readHeader(lex);
425 string const token = lex.getString();
430 lyxerr[Debug::PARSER] << "Handling token: `"
431 << token << '\'' << endl;
433 if (token == "\\end_document") {
438 readParagraph(lex, token, paragraphs(), pit, depth);
445 int Buffer::readParagraph(LyXLex & lex, string const & token,
446 ParagraphList & pars, ParagraphList::iterator & pit,
447 Paragraph::depth_type & depth)
449 static Change current_change;
452 if (token == "\\begin_layout") {
453 lex.pushToken(token);
456 par.params().depth(depth);
457 if (params().tracking_changes)
459 LyXFont f(LyXFont::ALL_INHERIT, params().language);
463 if (pit != pars.end())
466 pit = pars.insert(pit, par);
468 // FIXME: goddamn InsetTabular makes us pass a Buffer
470 ::readParagraph(*this, *pit, lex);
472 } else if (token == "\\begin_deeper") {
474 } else if (token == "\\end_deeper") {
476 lex.printError("\\end_deeper: " "depth is already null");
487 // needed to insert the selection
488 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
489 LyXFont const & fn,string const & str)
491 LyXLayout_ptr const & layout = par->layout();
495 par->checkInsertChar(font);
496 // insert the string, don't insert doublespace
497 bool space_inserted = true;
498 bool autobreakrows = !par->inInset() ||
499 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
500 for(string::const_iterator cit = str.begin();
501 cit != str.end(); ++cit) {
503 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
504 breakParagraph(params(), paragraphs(), par, pos,
505 layout->isEnvironment());
508 space_inserted = true;
512 // do not insert consecutive spaces if !free_spacing
513 } else if ((*cit == ' ' || *cit == '\t') &&
514 space_inserted && !par->isFreeSpacing()) {
516 } else if (*cit == '\t') {
517 if (!par->isFreeSpacing()) {
518 // tabs are like spaces here
519 par->insertChar(pos, ' ', font);
521 space_inserted = true;
523 const pos_type nb = 8 - pos % 8;
524 for (pos_type a = 0; a < nb ; ++a) {
525 par->insertChar(pos, ' ', font);
528 space_inserted = true;
530 } else if (!IsPrintable(*cit)) {
531 // Ignore unprintables
534 // just insert the character
535 par->insertChar(pos, *cit, font);
537 space_inserted = (*cit == ' ');
544 bool Buffer::readFile(string const & filename)
546 // Check if the file is compressed.
547 string const format = getExtFromContents(filename);
548 if (format == "gzip" || format == "zip" || format == "compress") {
549 params().compressed = true;
552 bool ret = readFile(filename, paragraphs().begin());
554 // After we have read a file, we must ensure that the buffer
555 // language is set and used in the gui.
556 // If you know of a better place to put this, please tell me. (Lgb)
557 updateDocLang(params().language);
563 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
566 lex.setFile(filename);
568 return readFile(lex, filename, pit);
572 bool Buffer::readFile(LyXLex & lex, string const & filename,
573 ParagraphList::iterator pit)
575 BOOST_ASSERT(!filename.empty());
578 Alert::error(_("Document could not be read"),
579 bformat(_("%1$s could not be read."), filename));
584 string const token(lex.getString());
587 Alert::error(_("Document could not be read"),
588 bformat(_("%1$s could not be read."), filename));
592 // the first token _must_ be...
593 if (token != "\\lyxformat") {
594 lyxerr << "Token: " << token << endl;
596 Alert::error(_("Document format failure"),
597 bformat(_("%1$s is not a LyX document."),
603 string tmp_format = lex.getString();
604 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
605 // if present remove ".," from string.
606 string::size_type dot = tmp_format.find_first_of(".,");
607 //lyxerr << " dot found at " << dot << endl;
608 if (dot != string::npos)
609 tmp_format.erase(dot, 1);
610 int file_format = strToInt(tmp_format);
611 //lyxerr << "format: " << file_format << endl;
613 if (file_format > LYX_FORMAT) {
614 Alert::warning(_("Document format failure"),
615 bformat(_("%1$s was created with a newer"
616 " version of LyX. This is likely to"
619 } else if (file_format < LYX_FORMAT) {
620 string const tmpfile = tempName();
621 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
622 if (command.empty()) {
623 Alert::error(_("Conversion script not found"),
624 bformat(_("%1$s is from an earlier"
625 " version of LyX, but the"
626 " conversion script lyx2lyx"
627 " could not be found."),
633 + " -o " + tmpfile + ' '
634 + QuoteName(filename);
635 lyxerr[Debug::INFO] << "Running '"
638 cmd_ret const ret = RunCommand(command);
639 if (ret.first != 0) {
640 Alert::error(_("Conversion script failed"),
641 bformat(_("%1$s is from an earlier version"
642 " of LyX, but the lyx2lyx script"
643 " failed to convert it."),
647 bool ret = readFile(tmpfile, pit);
648 // Do stuff with tmpfile name and buffer name here.
654 bool the_end = readBody(lex, pit);
655 params().setPaperStuff();
658 Alert::error(_("Document format failure"),
659 bformat(_("%1$s ended unexpectedly, which means"
660 " that it is probably corrupted."),
667 // Should probably be moved to somewhere else: BufferView? LyXView?
668 bool Buffer::save() const
670 // We don't need autosaves in the immediate future. (Asger)
671 resetAutosaveTimers();
675 if (lyxrc.make_backup) {
676 s = fileName() + '~';
677 if (!lyxrc.backupdir_path.empty())
678 s = AddName(lyxrc.backupdir_path,
679 subst(os::slashify_path(s),'/','!'));
681 // Rename is the wrong way of making a backup,
682 // this is the correct way.
683 /* truss cp fil fil2:
684 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
685 stat("LyXVC.lyx", 0xEFFFF688) = 0
686 open("LyXVC.lyx", O_RDONLY) = 3
687 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
688 fstat(4, 0xEFFFF508) = 0
689 fstat(3, 0xEFFFF508) = 0
690 read(3, " # T h i s f i l e w".., 8192) = 5579
691 write(4, " # T h i s f i l e w".., 5579) = 5579
692 read(3, 0xEFFFD4A0, 8192) = 0
695 chmod("LyXVC3.lyx", 0100644) = 0
696 lseek(0, 0, SEEK_CUR) = 46440
700 // Should probably have some more error checking here.
701 // Doing it this way, also makes the inodes stay the same.
702 // This is still not a very good solution, in particular we
703 // might loose the owner of the backup.
704 FileInfo finfo(fileName());
706 mode_t fmode = finfo.getMode();
707 struct utimbuf times = {
708 finfo.getAccessTime(),
709 finfo.getModificationTime() };
711 ifstream ifs(fileName().c_str());
712 ofstream ofs(s.c_str(), ios::out|ios::trunc);
717 ::chmod(s.c_str(), fmode);
719 if (::utime(s.c_str(), ×)) {
720 lyxerr << "utime error." << endl;
723 lyxerr << "LyX was not able to make "
724 "backup copy. Beware." << endl;
729 if (writeFile(fileName())) {
731 removeAutosaveFile(fileName());
733 // Saving failed, so backup is not backup
734 if (lyxrc.make_backup) {
735 rename(s, fileName());
743 bool Buffer::writeFile(string const & fname) const
745 if (pimpl_->read_only && (fname == fileName())) {
749 FileInfo finfo(fname);
750 if (finfo.exist() && !finfo.writable()) {
756 if (params().compressed) {
757 gz::ogzstream ofs(fname.c_str());
762 retval = do_writeFile(ofs);
765 ofstream ofs(fname.c_str());
769 retval = do_writeFile(ofs);
776 bool Buffer::do_writeFile(ostream & ofs) const
780 // Use the standard "C" locale for file output.
781 ofs.imbue(std::locale::classic());
784 // The top of the file should not be written by params().
786 // write out a comment in the top of the file
787 ofs << "#LyX " << lyx_version
788 << " created this file. For more info see http://www.lyx.org/\n"
789 << "\\lyxformat " << LYX_FORMAT << "\n";
791 // now write out the buffer paramters.
792 params().writeFile(ofs);
794 ofs << "\\end_header\n";
796 Paragraph::depth_type depth = 0;
798 // this will write out all the paragraphs
799 // using recursive descent.
800 ParagraphList::const_iterator pit = paragraphs().begin();
801 ParagraphList::const_iterator pend = paragraphs().end();
802 for (; pit != pend; ++pit)
803 pit->write(*this, ofs, params(), depth);
805 // Write marker that shows file is complete
806 ofs << "\n\\end_document" << endl;
808 // Shouldn't really be needed....
811 // how to check if close went ok?
812 // Following is an attempt... (BE 20001011)
814 // good() returns false if any error occured, including some
816 // bad() returns true if something bad happened in the buffer,
817 // which should include file system full errors.
824 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
826 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
838 pair<int, string> const addDepth(int depth, int ldepth)
842 d += (ldepth - depth) * 2;
843 return make_pair(d, string(d, ' '));
849 string const Buffer::asciiParagraph(Paragraph const & par,
850 unsigned int linelen,
851 bool noparbreak) const
853 ostringstream buffer;
855 Paragraph::depth_type ltype_depth = 0;
856 bool ref_printed = false;
857 Paragraph::depth_type depth = par.params().depth();
859 // First write the layout
860 string const & tmp = par.layout()->name();
861 if (compare_no_case(tmp, "itemize") == 0) {
863 ltype_depth = depth + 1;
864 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
866 ltype_depth = depth + 1;
867 } else if (contains(ascii_lowercase(tmp), "ection")) {
869 ltype_depth = depth + 1;
870 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
872 ltype_depth = depth + 1;
873 } else if (compare_ascii_no_case(tmp, "description") == 0) {
875 ltype_depth = depth + 1;
876 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
879 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
887 /* maybe some vertical spaces */
889 /* the labelwidthstring used in lists */
893 /* some pagebreaks? */
897 /* what about the alignment */
899 // linelen <= 0 is special and means we don't have paragraph breaks
901 string::size_type currlinelen = 0;
907 buffer << string(depth * 2, ' ');
908 currlinelen += depth * 2;
911 // we should probably change to the paragraph language in the
912 // gettext here (if possible) so that strings are outputted in
913 // the correct language! (20012712 Jug)
917 case 4: // (Sub)Paragraph
918 case 5: // Description
922 buffer << _("Abstract") << "\n\n";
925 string const abst = _("Abstract: ");
927 currlinelen += abst.length();
930 case 7: // Bibliography
933 buffer << _("References") << "\n\n";
936 string const refs = _("References: ");
938 currlinelen += refs.length();
946 string const parlab = par.params().labelString();
947 buffer << parlab << ' ';
948 currlinelen += parlab.length() + 1;
956 pair<int, string> p = addDepth(depth, ltype_depth);
958 currlinelen += p.first;
961 // this is to change the linebreak to do it by word a bit more
962 // intelligent hopefully! (only in the case where we have a
963 // max linelength!) (Jug)
967 for (pos_type i = 0; i < par.size(); ++i) {
968 char c = par.getUChar(params(), i);
970 case Paragraph::META_INSET:
972 InsetOld const * inset = par.getInset(i);
976 currlinelen += word.length();
979 if (inset->ascii(*this, buffer, linelen)) {
980 // to be sure it breaks paragraph
981 currlinelen += linelen;
990 currlinelen + word.length() > linelen - 10) {
992 pair<int, string> p = addDepth(depth, ltype_depth);
994 currlinelen = p.first;
997 buffer << word << ' ';
998 currlinelen += word.length() + 1;
1005 lyxerr[Debug::INFO] <<
1006 "writeAsciiFile: NULL char in structure." << endl;
1008 if ((linelen > 0) &&
1009 (currlinelen + word.length()) > linelen)
1013 pair<int, string> p =
1014 addDepth(depth, ltype_depth);
1016 currlinelen = p.first;
1023 return STRCONV(buffer.str());
1027 void Buffer::writeFileAscii(string const & fname, int linelen)
1030 if (!::openFileWrite(ofs, fname))
1032 writeFileAscii(ofs, linelen);
1036 void Buffer::writeFileAscii(ostream & os, int linelen)
1038 ParagraphList::iterator beg = paragraphs().begin();
1039 ParagraphList::iterator end = paragraphs().end();
1040 ParagraphList::iterator it = beg;
1041 for (; it != end; ++it) {
1042 os << asciiParagraph(*it, linelen, it == beg);
1048 void Buffer::makeLaTeXFile(string const & fname,
1049 string const & original_path,
1050 LatexRunParams const & runparams,
1051 bool output_preamble, bool output_body)
1053 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1056 if (!::openFileWrite(ofs, fname))
1059 makeLaTeXFile(ofs, original_path,
1060 runparams, output_preamble, output_body);
1064 lyxerr << "File was not closed properly." << endl;
1069 void Buffer::makeLaTeXFile(ostream & os,
1070 string const & original_path,
1071 LatexRunParams const & runparams_in,
1072 bool output_preamble, bool output_body)
1074 LatexRunParams runparams = runparams_in;
1075 niceFile() = runparams.nice; // this will be used by Insetincludes.
1077 // validate the buffer.
1078 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1079 LaTeXFeatures features(params());
1081 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1084 // The starting paragraph of the coming rows is the
1085 // first paragraph of the document. (Asger)
1086 texrow().start(paragraphs().begin()->id(), 0);
1088 if (output_preamble && runparams.nice) {
1089 os << "%% LyX " << lyx_version << " created this file. "
1090 "For more info, see http://www.lyx.org/.\n"
1091 "%% Do not edit unless you really know what "
1096 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1097 // There are a few differences between nice LaTeX and usual files:
1098 // usual is \batchmode and has a
1099 // special input@path to allow the including of figures
1100 // with either \input or \includegraphics (what figinsets do).
1101 // input@path is set when the actual parameter
1102 // original_path is set. This is done for usual tex-file, but not
1103 // for nice-latex-file. (Matthias 250696)
1104 if (output_preamble) {
1105 if (!runparams.nice) {
1106 // code for usual, NOT nice-latex-file
1107 os << "\\batchmode\n"; // changed
1108 // from \nonstopmode
1111 if (!original_path.empty()) {
1112 string inputpath = os::external_path(original_path);
1113 subst(inputpath, "~", "\\string~");
1114 os << "\\makeatletter\n"
1115 << "\\def\\input@path{{"
1116 << inputpath << "/}}\n"
1117 << "\\makeatother\n";
1123 // Write the preamble
1124 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1130 os << "\\begin{document}\n";
1132 } // output_preamble
1133 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1135 if (!lyxrc.language_auto_begin) {
1136 os << subst(lyxrc.language_command_begin, "$$lang",
1137 params().language->babel())
1142 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1144 // add this just in case after all the paragraphs
1148 if (!lyxrc.language_auto_end) {
1149 os << subst(lyxrc.language_command_end, "$$lang",
1150 params().language->babel())
1155 if (output_preamble) {
1156 os << "\\end{document}\n";
1159 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1161 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1165 // Just to be sure. (Asger)
1168 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1169 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1172 // we want this to be true outside previews (for insetexternal)
1177 bool Buffer::isLatex() const
1179 return params().getLyXTextClass().outputType() == LATEX;
1183 bool Buffer::isLinuxDoc() const
1185 return params().getLyXTextClass().outputType() == LINUXDOC;
1189 bool Buffer::isLiterate() const
1191 return params().getLyXTextClass().outputType() == LITERATE;
1195 bool Buffer::isDocBook() const
1197 return params().getLyXTextClass().outputType() == DOCBOOK;
1201 bool Buffer::isSGML() const
1203 LyXTextClass const & tclass = params().getLyXTextClass();
1205 return tclass.outputType() == LINUXDOC ||
1206 tclass.outputType() == DOCBOOK;
1210 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1213 if (!::openFileWrite(ofs, fname))
1216 niceFile() = nice; // this will be used by included files.
1218 LaTeXFeatures features(params());
1224 LyXTextClass const & tclass = params().getLyXTextClass();
1226 string top_element = tclass.latexname();
1229 ofs << "<!doctype linuxdoc system";
1231 string preamble = params().preamble;
1232 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1234 preamble += features.getIncludedFiles(name);
1235 preamble += features.getLyXSGMLEntities();
1237 if (!preamble.empty()) {
1238 ofs << " [ " << preamble << " ]";
1242 if (params().options.empty())
1243 sgml::openTag(ofs, 0, false, top_element);
1245 string top = top_element;
1247 top += params().options;
1248 sgml::openTag(ofs, 0, false, top);
1252 ofs << "<!-- LyX " << lyx_version
1253 << " created this file. For more info see http://www.lyx.org/"
1256 Paragraph::depth_type depth = 0; // paragraph depth
1258 vector<string> environment_stack(5);
1260 ParagraphList::iterator pit = paragraphs().begin();
1261 ParagraphList::iterator pend = paragraphs().end();
1262 for (; pit != pend; ++pit) {
1263 LyXLayout_ptr const & style = pit->layout();
1264 // treat <toc> as a special case for compatibility with old code
1265 if (pit->isInset(0)) {
1266 InsetOld * inset = pit->getInset(0);
1267 InsetOld::Code lyx_code = inset->lyxCode();
1268 if (lyx_code == InsetOld::TOC_CODE) {
1269 string const temp = "toc";
1270 sgml::openTag(ofs, depth, false, temp);
1275 // environment tag closing
1276 for (; depth > pit->params().depth(); --depth) {
1277 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1278 environment_stack[depth].erase();
1281 // write opening SGML tags
1282 switch (style->latextype) {
1283 case LATEX_PARAGRAPH:
1284 if (depth == pit->params().depth()
1285 && !environment_stack[depth].empty()) {
1286 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1287 environment_stack[depth].erase();
1293 sgml::openTag(ofs, depth, false, style->latexname());
1298 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1300 if (!environment_stack[depth].empty()) {
1301 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1305 environment_stack[depth].erase();
1306 sgml::openTag(ofs, depth, false, style->latexname());
1309 case LATEX_ENVIRONMENT:
1310 case LATEX_ITEM_ENVIRONMENT:
1311 case LATEX_BIB_ENVIRONMENT:
1313 string const & latexname = style->latexname();
1315 if (depth == pit->params().depth()
1316 && environment_stack[depth] != latexname) {
1317 sgml::closeTag(ofs, depth, false,
1318 environment_stack[depth]);
1319 environment_stack[depth].erase();
1321 if (depth < pit->params().depth()) {
1322 depth = pit->params().depth();
1323 environment_stack[depth].erase();
1325 if (environment_stack[depth] != latexname) {
1327 sgml::openTag(ofs, depth, false, "p");
1329 sgml::openTag(ofs, depth, false, latexname);
1331 if (environment_stack.size() == depth + 1)
1332 environment_stack.push_back("!-- --");
1333 environment_stack[depth] = latexname;
1336 if (style->latexparam() == "CDATA")
1339 if (style->latextype == LATEX_ENVIRONMENT) break;
1341 if (style->labeltype == LABEL_MANUAL)
1346 sgml::openTag(ofs, depth + 1, false, item_name);
1351 sgml::openTag(ofs, depth, false, style->latexname());
1355 simpleLinuxDocOnePar(ofs, pit, depth);
1358 // write closing SGML tags
1359 switch (style->latextype) {
1362 case LATEX_ENVIRONMENT:
1363 case LATEX_ITEM_ENVIRONMENT:
1364 case LATEX_BIB_ENVIRONMENT:
1365 if (style->latexparam() == "CDATA")
1369 sgml::closeTag(ofs, depth, false, style->latexname());
1375 for (int i = depth; i >= 0; --i)
1376 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1380 sgml::closeTag(ofs, 0, false, top_element);
1384 // How to check for successful close
1386 // we want this to be true outside previews (for insetexternal)
1391 // checks, if newcol chars should be put into this line
1392 // writes newline, if necessary.
1395 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1396 string::size_type newcol)
1399 if (colcount > lyxrc.ascii_linelen) {
1401 colcount = newcol; // assume write after this call
1416 string tag_name(PAR_TAG const & pt) {
1418 case NONE: return "!-- --";
1419 case TT: return "tt";
1420 case SF: return "sf";
1421 case BF: return "bf";
1422 case IT: return "it";
1423 case SL: return "sl";
1424 case EM: return "em";
1431 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1433 p1 = static_cast<PAR_TAG>(p1 | p2);
1438 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1440 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1446 // Handle internal paragraph parsing -- layout already processed.
1447 void Buffer::simpleLinuxDocOnePar(ostream & os,
1448 ParagraphList::iterator par,
1449 Paragraph::depth_type /*depth*/) const
1451 LyXLayout_ptr const & style = par->layout();
1453 string::size_type char_line_count = 5; // Heuristic choice ;-)
1455 // gets paragraph main font
1458 if (style->labeltype == LABEL_MANUAL) {
1459 font_old = style->labelfont;
1462 font_old = style->font;
1466 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1467 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1468 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1471 stack<PAR_TAG> tag_state;
1472 // parsing main loop
1473 for (pos_type i = 0; i < par->size(); ++i) {
1475 PAR_TAG tag_close = NONE;
1476 list < PAR_TAG > tag_open;
1478 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1480 if (font_old.family() != font.family()) {
1481 switch (family_type) {
1482 case LyXFont::SANS_FAMILY:
1485 case LyXFont::TYPEWRITER_FAMILY:
1492 family_type = font.family();
1494 switch (family_type) {
1495 case LyXFont::SANS_FAMILY:
1496 tag_open.push_back(SF);
1498 case LyXFont::TYPEWRITER_FAMILY:
1499 tag_open.push_back(TT);
1506 if (font_old.series() != font.series()) {
1507 switch (series_type) {
1508 case LyXFont::BOLD_SERIES:
1515 series_type = font.series();
1517 switch (series_type) {
1518 case LyXFont::BOLD_SERIES:
1519 tag_open.push_back(BF);
1527 if (font_old.shape() != font.shape()) {
1528 switch (shape_type) {
1529 case LyXFont::ITALIC_SHAPE:
1532 case LyXFont::SLANTED_SHAPE:
1539 shape_type = font.shape();
1541 switch (shape_type) {
1542 case LyXFont::ITALIC_SHAPE:
1543 tag_open.push_back(IT);
1545 case LyXFont::SLANTED_SHAPE:
1546 tag_open.push_back(SL);
1553 if (font_old.emph() != font.emph()) {
1554 if (font.emph() == LyXFont::ON) {
1555 tag_open.push_back(EM);
1564 list < PAR_TAG > temp;
1565 while (!tag_state.empty() && tag_close) {
1566 PAR_TAG k = tag_state.top();
1568 os << "</" << tag_name(k) << '>';
1575 for(list< PAR_TAG >::const_iterator j = temp.begin();
1576 j != temp.end(); ++j) {
1578 os << '<' << tag_name(*j) << '>';
1581 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1582 j != tag_open.end(); ++j) {
1584 os << '<' << tag_name(*j) << '>';
1587 char c = par->getChar(i);
1589 if (c == Paragraph::META_INSET) {
1590 InsetOld * inset = par->getInset(i);
1591 inset->linuxdoc(*this, os);
1596 if (style->latexparam() == "CDATA") {
1597 // "TeX"-Mode on == > SGML-Mode on.
1604 boost::tie(ws, str) = sgml::escapeChar(c);
1605 if (ws && !par->isFreeSpacing()) {
1606 // in freespacing mode, spaces are
1607 // non-breaking characters
1608 if (desc_on) {// if char is ' ' then...
1611 sgmlLineBreak(os, char_line_count, 6);
1615 sgmlLineBreak(os, char_line_count, 1);
1620 char_line_count += str.length();
1626 while (!tag_state.empty()) {
1627 os << "</" << tag_name(tag_state.top()) << '>';
1631 // resets description flag correctly
1633 // <tag> not closed...
1634 sgmlLineBreak(os, char_line_count, 6);
1640 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1643 if (!::openFileWrite(ofs, fname))
1646 niceFile() = nice; // this will be used by Insetincludes.
1648 LaTeXFeatures features(params());
1653 LyXTextClass const & tclass = params().getLyXTextClass();
1654 string top_element = tclass.latexname();
1657 ofs << "<!DOCTYPE " << top_element
1658 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1660 string preamble = params().preamble;
1661 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1663 preamble += features.getIncludedFiles(name);
1664 preamble += features.getLyXSGMLEntities();
1666 if (!preamble.empty()) {
1667 ofs << "\n [ " << preamble << " ]";
1672 string top = top_element;
1674 top += params().language->code();
1677 if (!params().options.empty()) {
1679 top += params().options;
1681 sgml::openTag(ofs, 0, false, top);
1683 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1684 << "\n See http://www.lyx.org/ for more information -->\n";
1686 vector<string> environment_stack(10);
1687 vector<string> environment_inner(10);
1688 vector<string> command_stack(10);
1690 bool command_flag = false;
1691 Paragraph::depth_type command_depth = 0;
1692 Paragraph::depth_type command_base = 0;
1693 Paragraph::depth_type cmd_depth = 0;
1694 Paragraph::depth_type depth = 0; // paragraph depth
1697 string command_name;
1699 ParagraphList::iterator par = paragraphs().begin();
1700 ParagraphList::iterator pend = paragraphs().end();
1702 for (; par != pend; ++par) {
1706 int desc_on = 0; // description mode
1708 LyXLayout_ptr const & style = par->layout();
1710 // environment tag closing
1711 for (; depth > par->params().depth(); --depth) {
1712 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1713 item_name = "listitem";
1714 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1715 if (environment_inner[depth] == "varlistentry")
1716 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1718 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1719 environment_stack[depth].erase();
1720 environment_inner[depth].erase();
1723 if (depth == par->params().depth()
1724 && environment_stack[depth] != style->latexname()
1725 && !environment_stack[depth].empty()) {
1726 if (environment_inner[depth] != "!-- --") {
1727 item_name= "listitem";
1728 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1729 if (environment_inner[depth] == "varlistentry")
1730 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1733 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1735 environment_stack[depth].erase();
1736 environment_inner[depth].erase();
1739 // Write opening SGML tags.
1740 switch (style->latextype) {
1741 case LATEX_PARAGRAPH:
1742 sgml::openTag(ofs, depth + command_depth,
1743 false, style->latexname());
1748 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1750 command_name = style->latexname();
1752 sgmlparam = style->latexparam();
1753 c_params = split(sgmlparam, c_depth,'|');
1755 cmd_depth = atoi(c_depth);
1758 if (cmd_depth < command_base) {
1759 for (Paragraph::depth_type j = command_depth;
1760 j >= command_base; --j) {
1761 sgml::closeTag(ofs, j, false, command_stack[j]);
1764 command_depth = command_base = cmd_depth;
1765 } else if (cmd_depth <= command_depth) {
1766 for (int j = command_depth;
1767 j >= int(cmd_depth); --j) {
1768 sgml::closeTag(ofs, j, false, command_stack[j]);
1771 command_depth = cmd_depth;
1773 command_depth = cmd_depth;
1775 command_depth = command_base = cmd_depth;
1776 command_flag = true;
1778 if (command_stack.size() == command_depth + 1)
1779 command_stack.push_back(string());
1780 command_stack[command_depth] = command_name;
1782 // treat label as a special case for
1783 // more WYSIWYM handling.
1784 // This is a hack while paragraphs can't have
1785 // attributes, like id in this case.
1786 if (par->isInset(0)) {
1787 InsetOld * inset = par->getInset(0);
1788 InsetOld::Code lyx_code = inset->lyxCode();
1789 if (lyx_code == InsetOld::LABEL_CODE) {
1790 command_name += " id=\"";
1791 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1792 command_name += '"';
1797 sgml::openTag(ofs, depth + command_depth, false, command_name);
1799 item_name = c_params.empty() ? "title" : c_params;
1800 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1803 case LATEX_ENVIRONMENT:
1804 case LATEX_ITEM_ENVIRONMENT:
1805 if (depth < par->params().depth()) {
1806 depth = par->params().depth();
1807 environment_stack[depth].erase();
1810 if (environment_stack[depth] != style->latexname()) {
1811 if (environment_stack.size() == depth + 1) {
1812 environment_stack.push_back("!-- --");
1813 environment_inner.push_back("!-- --");
1815 environment_stack[depth] = style->latexname();
1816 environment_inner[depth] = "!-- --";
1817 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1819 if (environment_inner[depth] != "!-- --") {
1820 item_name= "listitem";
1821 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1822 if (environment_inner[depth] == "varlistentry")
1823 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1827 if (style->latextype == LATEX_ENVIRONMENT) {
1828 if (!style->latexparam().empty()) {
1829 if (style->latexparam() == "CDATA")
1832 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1837 desc_on = (style->labeltype == LABEL_MANUAL);
1839 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1840 sgml::openTag(ofs, depth + 1 + command_depth,
1841 false, environment_inner[depth]);
1843 item_name = desc_on ? "term" : "para";
1844 sgml::openTag(ofs, depth + 1 + command_depth,
1848 sgml::openTag(ofs, depth + command_depth,
1849 false, style->latexname());
1853 simpleDocBookOnePar(ofs, par, desc_on,
1854 depth + 1 + command_depth);
1857 // write closing SGML tags
1858 switch (style->latextype) {
1860 end_tag = c_params.empty() ? "title" : c_params;
1861 sgml::closeTag(ofs, depth + command_depth,
1864 case LATEX_ENVIRONMENT:
1865 if (!style->latexparam().empty()) {
1866 if (style->latexparam() == "CDATA")
1869 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1872 case LATEX_ITEM_ENVIRONMENT:
1873 if (desc_on == 1) break;
1875 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1877 case LATEX_PARAGRAPH:
1878 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1881 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1887 for (int d = depth; d >= 0; --d) {
1888 if (!environment_stack[depth].empty()) {
1889 if (environment_inner[depth] != "!-- --") {
1890 item_name = "listitem";
1891 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1892 if (environment_inner[depth] == "varlistentry")
1893 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1896 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1900 for (int j = command_depth; j >= 0 ; --j)
1901 if (!command_stack[j].empty()) {
1902 sgml::closeTag(ofs, j, false, command_stack[j]);
1907 sgml::closeTag(ofs, 0, false, top_element);
1910 // How to check for successful close
1912 // we want this to be true outside previews (for insetexternal)
1917 void Buffer::simpleDocBookOnePar(ostream & os,
1918 ParagraphList::iterator par, int & desc_on,
1919 Paragraph::depth_type depth) const
1921 bool emph_flag = false;
1923 LyXLayout_ptr const & style = par->layout();
1925 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1927 int char_line_count = depth;
1928 //if (!style.free_spacing)
1929 // os << string(depth,' ');
1931 // parsing main loop
1932 for (pos_type i = 0; i < par->size(); ++i) {
1933 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1935 // handle <emphasis> tag
1936 if (font_old.emph() != font.emph()) {
1937 if (font.emph() == LyXFont::ON) {
1938 if (style->latexparam() == "CDATA")
1941 if (style->latexparam() == "CDATA")
1945 if (style->latexparam() == "CDATA")
1947 os << "</emphasis>";
1948 if (style->latexparam() == "CDATA")
1955 if (par->isInset(i)) {
1956 InsetOld * inset = par->getInset(i);
1957 // don't print the inset in position 0 if desc_on == 3 (label)
1958 if (i || desc_on != 3) {
1959 if (style->latexparam() == "CDATA")
1961 inset->docbook(*this, os, false);
1962 if (style->latexparam() == "CDATA")
1966 char c = par->getChar(i);
1969 boost::tie(ws, str) = sgml::escapeChar(c);
1971 if (style->pass_thru) {
1973 } else if (par->isFreeSpacing() || c != ' ') {
1975 } else if (desc_on == 1) {
1977 os << "\n</term><listitem><para>";
1987 if (style->latexparam() == "CDATA")
1989 os << "</emphasis>";
1990 if (style->latexparam() == "CDATA")
1994 // resets description flag correctly
1996 // <term> not closed...
1997 os << "</term>\n<listitem><para> </para>";
1999 if (style->free_spacing)
2004 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2005 // Other flags: -wall -v0 -x
2006 int Buffer::runChktex()
2010 // get LaTeX-Filename
2011 string const name = getLatexName();
2012 string path = filePath();
2014 string const org_path = path;
2015 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2019 Path p(path); // path to LaTeX file
2020 message(_("Running chktex..."));
2022 // Generate the LaTeX file if neccessary
2023 LatexRunParams runparams;
2024 runparams.flavor = LatexRunParams::LATEX;
2025 runparams.nice = false;
2026 makeLaTeXFile(name, org_path, runparams);
2029 Chktex chktex(lyxrc.chktex_command, name, filePath());
2030 int res = chktex.run(terr); // run chktex
2033 Alert::error(_("chktex failure"),
2034 _("Could not run chktex successfully."));
2035 } else if (res > 0) {
2036 // Insert all errors as errors boxes
2037 bufferErrors(*this, terr);
2046 void Buffer::validate(LaTeXFeatures & features) const
2048 LyXTextClass const & tclass = params().getLyXTextClass();
2050 if (params().tracking_changes) {
2051 features.require("dvipost");
2052 features.require("color");
2055 // AMS Style is at document level
2056 if (params().use_amsmath == BufferParams::AMS_ON
2057 || tclass.provides(LyXTextClass::amsmath))
2058 features.require("amsmath");
2060 for_each(paragraphs().begin(), paragraphs().end(),
2061 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2063 // the bullet shapes are buffer level not paragraph level
2064 // so they are tested here
2065 for (int i = 0; i < 4; ++i) {
2066 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2067 int const font = params().user_defined_bullet(i).getFont();
2069 int const c = params()
2070 .user_defined_bullet(i)
2077 features.require("latexsym");
2079 } else if (font == 1) {
2080 features.require("amssymb");
2081 } else if ((font >= 2 && font <= 5)) {
2082 features.require("pifont");
2087 if (lyxerr.debugging(Debug::LATEX)) {
2088 features.showStruct();
2093 void Buffer::getLabelList(std::vector<string> & list) const
2095 /// if this is a child document and the parent is already loaded
2096 /// Use the parent's list instead [ale990407]
2097 if (!params().parentname.empty()
2098 && bufferlist.exists(params().parentname)) {
2099 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2101 tmp->getLabelList(list);
2106 for (inset_iterator it = inset_const_iterator_begin();
2107 it != inset_const_iterator_end(); ++it) {
2108 it->getLabelList(list);
2113 // This is also a buffer property (ale)
2114 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2116 /// if this is a child document and the parent is already loaded
2117 /// use the parent's list instead [ale990412]
2118 if (!params().parentname.empty() &&
2119 bufferlist.exists(params().parentname)) {
2120 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2122 tmp->fillWithBibKeys(keys);
2127 for (inset_iterator it = inset_const_iterator_begin();
2128 it != inset_const_iterator_end(); ++it) {
2129 if (it->lyxCode() == InsetOld::BIBTEX_CODE)
2130 static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
2131 else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
2132 static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
2133 else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2134 InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
2135 string const key = bib.getContents();
2136 string const opt = bib.getOptions();
2137 string const ref; // = pit->asString(this, false);
2138 string const info = opt + "TheBibliographyRef" + ref;
2139 keys.push_back(pair<string, string>(key, info));
2145 bool Buffer::isDepClean(string const & name) const
2147 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2148 if (it == pimpl_->dep_clean.end())
2154 void Buffer::markDepClean(string const & name)
2156 pimpl_->dep_clean[name] = true;
2160 bool Buffer::dispatch(string const & command, bool * result)
2162 // Split command string into command and argument
2164 string line = ltrim(command);
2165 string const arg = trim(split(line, cmd, ' '));
2167 return dispatch(lyxaction.LookupFunc(cmd), arg, result);
2171 bool Buffer::dispatch(int action, string const & argument, bool * result)
2173 bool dispatched = true;
2177 bool const tmp = Exporter::Export(this, argument, false);
2190 void Buffer::changeLanguage(Language const * from, Language const * to)
2192 lyxerr << "Changing Language!" << endl;
2194 // Take care of l10n/i18n
2197 ParIterator end = par_iterator_end();
2198 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2199 it->changeLanguage(params(), from, to);
2203 void Buffer::updateDocLang(Language const * nlang)
2205 pimpl_->messages.reset(new Messages(nlang->code()));
2209 bool Buffer::isMultiLingual()
2211 ParIterator end = par_iterator_end();
2212 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2213 if (it->isMultiLingual(params()))
2220 void Buffer::inset_iterator::setParagraph()
2222 while (pit != pend) {
2223 it = pit->insetlist.begin();
2224 if (it != pit->insetlist.end())
2231 InsetOld * Buffer::getInsetFromID(int id_arg) const
2233 for (inset_iterator it = inset_const_iterator_begin();
2234 it != inset_const_iterator_end(); ++it)
2236 if (it->id() == id_arg)
2238 InsetOld * in = it->getInsetFromID(id_arg);
2246 ParIterator Buffer::getParFromID(int id) const
2248 #warning FIXME: const correctness! (Andre)
2249 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2250 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2252 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2254 // John says this is called with id == -1 from undo
2255 lyxerr << "getParFromID(), id: " << id << endl;
2259 for (; it != end; ++it)
2267 bool Buffer::hasParWithID(int id) const
2269 ParConstIterator it = par_iterator_begin();
2270 ParConstIterator end = par_iterator_end();
2273 // John says this is called with id == -1 from undo
2274 lyxerr << "hasParWithID(), id: " << id << endl;
2278 for (; it != end; ++it)
2286 ParIterator Buffer::par_iterator_begin()
2288 return ParIterator(paragraphs().begin(), paragraphs());
2292 ParIterator Buffer::par_iterator_end()
2294 return ParIterator(paragraphs().end(), paragraphs());
2298 ParConstIterator Buffer::par_iterator_begin() const
2300 ParagraphList const & pars = paragraphs();
2301 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2305 ParConstIterator Buffer::par_iterator_end() const
2307 ParagraphList const & pars = paragraphs();
2308 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2312 Language const * Buffer::getLanguage() const
2314 return params().language;
2318 string const Buffer::B_(string const & l10n) const
2320 if (pimpl_->messages.get()) {
2321 return pimpl_->messages->get(l10n);
2328 bool Buffer::isClean() const
2330 return pimpl_->lyx_clean;
2334 bool Buffer::isBakClean() const
2336 return pimpl_->bak_clean;
2340 void Buffer::markClean() const
2342 if (!pimpl_->lyx_clean) {
2343 pimpl_->lyx_clean = true;
2346 // if the .lyx file has been saved, we don't need an
2348 pimpl_->bak_clean = true;
2352 void Buffer::markBakClean()
2354 pimpl_->bak_clean = true;
2358 void Buffer::setUnnamed(bool flag)
2360 pimpl_->unnamed = flag;
2364 bool Buffer::isUnnamed()
2366 return pimpl_->unnamed;
2370 void Buffer::markDirty()
2372 if (pimpl_->lyx_clean) {
2373 pimpl_->lyx_clean = false;
2376 pimpl_->bak_clean = false;
2378 DepClean::iterator it = pimpl_->dep_clean.begin();
2379 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2381 for (; it != end; ++it) {
2387 string const & Buffer::fileName() const
2389 return pimpl_->filename;
2393 string const & Buffer::filePath() const
2395 return pimpl_->filepath;
2399 bool Buffer::isReadonly() const
2401 return pimpl_->read_only;
2405 void Buffer::setParentName(string const & name)
2407 params().parentname = name;
2411 Buffer::inset_iterator::inset_iterator()
2416 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2423 Buffer::inset_iterator Buffer::inset_iterator_begin()
2425 return inset_iterator(paragraphs().begin(), paragraphs().end());
2429 Buffer::inset_iterator Buffer::inset_iterator_end()
2431 return inset_iterator(paragraphs().end(), paragraphs().end());
2435 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2437 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2438 return inset_iterator(pars.begin(), pars.end());
2442 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2444 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2445 return inset_iterator(pars.end(), pars.end());
2449 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2453 if (it == pit->insetlist.end()) {
2462 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2464 inset_iterator tmp = *this;
2470 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2476 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2482 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2488 lyx::pos_type Buffer::inset_iterator::getPos() const
2494 bool operator==(Buffer::inset_iterator const & iter1,
2495 Buffer::inset_iterator const & iter2)
2497 return iter1.pit == iter2.pit
2498 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2502 bool operator!=(Buffer::inset_iterator const & iter1,
2503 Buffer::inset_iterator const & iter2)
2505 return !(iter1 == iter2);